Merge tag 'drm-misc-next-2021-08-05' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_dp_aux_bus.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2021 Google Inc.
4  *
5  * The DP AUX bus is used for devices that are connected over a DisplayPort
6  * AUX bus. The devices on the far side of the bus are referred to as
7  * endpoints in this code.
8  *
9  * Commonly there is only one device connected to the DP AUX bus: a panel.
10  * Though historically panels (even DP panels) have been modeled as simple
11  * platform devices, putting them under the DP AUX bus allows the panel driver
12  * to perform transactions on that bus.
13  */
14
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_domain.h>
20 #include <linux/pm_runtime.h>
21
22 #include <drm/drm_dp_aux_bus.h>
23 #include <drm/drm_dp_helper.h>
24
25 /**
26  * dp_aux_ep_match() - The match function for the dp_aux_bus.
27  * @dev: The device to match.
28  * @drv: The driver to try to match against.
29  *
30  * At the moment, we just match on device tree.
31  *
32  * Return: True if this driver matches this device; false otherwise.
33  */
34 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
35 {
36         return !!of_match_device(drv->of_match_table, dev);
37 }
38
39 /**
40  * dp_aux_ep_probe() - The probe function for the dp_aux_bus.
41  * @dev: The device to probe.
42  *
43  * Calls through to the endpoint driver probe.
44  *
45  * Return: 0 if no error or negative error code.
46  */
47 static int dp_aux_ep_probe(struct device *dev)
48 {
49         struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
50         struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
51         int ret;
52
53         ret = dev_pm_domain_attach(dev, true);
54         if (ret)
55                 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
56
57         ret = aux_ep_drv->probe(aux_ep);
58         if (ret)
59                 dev_pm_domain_detach(dev, true);
60
61         return ret;
62 }
63
64 /**
65  * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
66  * @dev: The device to remove.
67  *
68  * Calls through to the endpoint driver remove.
69  *
70  * Return: 0 if no error or negative error code.
71  */
72 static int dp_aux_ep_remove(struct device *dev)
73 {
74         struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
75         struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
76
77         if (aux_ep_drv->remove)
78                 aux_ep_drv->remove(aux_ep);
79         dev_pm_domain_detach(dev, true);
80
81         return 0;
82 }
83
84 /**
85  * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
86  * @dev: The device to shutdown.
87  *
88  * Calls through to the endpoint driver shutdown.
89  */
90 static void dp_aux_ep_shutdown(struct device *dev)
91 {
92         struct dp_aux_ep_driver *aux_ep_drv;
93
94         if (!dev->driver)
95                 return;
96
97         aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
98         if (aux_ep_drv->shutdown)
99                 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
100 }
101
102 static struct bus_type dp_aux_bus_type = {
103         .name           = "dp-aux",
104         .match          = dp_aux_ep_match,
105         .probe          = dp_aux_ep_probe,
106         .remove         = dp_aux_ep_remove,
107         .shutdown       = dp_aux_ep_shutdown,
108 };
109
110 static ssize_t modalias_show(struct device *dev,
111                              struct device_attribute *attr, char *buf)
112 {
113         return of_device_modalias(dev, buf, PAGE_SIZE);
114 }
115 static DEVICE_ATTR_RO(modalias);
116
117 static struct attribute *dp_aux_ep_dev_attrs[] = {
118         &dev_attr_modalias.attr,
119         NULL,
120 };
121 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
122
123 /**
124  * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
125  * @dev: The device to free.
126  *
127  * Return: 0 if no error or negative error code.
128  */
129 static void dp_aux_ep_dev_release(struct device *dev)
130 {
131         kfree(to_dp_aux_ep_dev(dev));
132 }
133
134 static struct device_type dp_aux_device_type_type = {
135         .groups         = dp_aux_ep_dev_groups,
136         .uevent         = of_device_uevent_modalias,
137         .release        = dp_aux_ep_dev_release,
138 };
139
140 /**
141  * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
142  * @dev: The device to destroy.
143  * @data: Not used
144  *
145  * This is just used as a callback by of_dp_aux_depopulate_ep_devices() and
146  * is called for _all_ of the child devices of the device providing the AUX bus.
147  * We'll only act on those that are of type "dp_aux_bus_type".
148  *
149  * This function is effectively an inverse of what's in the loop
150  * in of_dp_aux_populate_ep_devices().
151  *
152  * Return: 0 if no error or negative error code.
153  */
154 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
155 {
156         struct device_node *np = dev->of_node;
157
158         if (dev->bus != &dp_aux_bus_type)
159                 return 0;
160
161         if (!of_node_check_flag(np, OF_POPULATED))
162                 return 0;
163
164         of_node_clear_flag(np, OF_POPULATED);
165         of_node_put(np);
166
167         device_unregister(dev);
168
169         return 0;
170 }
171
172 /**
173  * of_dp_aux_depopulate_ep_devices() - Undo of_dp_aux_populate_ep_devices
174  * @aux: The AUX channel whose devices we want to depopulate
175  *
176  * This will destroy all devices that were created
177  * by of_dp_aux_populate_ep_devices().
178  */
179 void of_dp_aux_depopulate_ep_devices(struct drm_dp_aux *aux)
180 {
181         device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
182 }
183 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_ep_devices);
184
185 /**
186  * of_dp_aux_populate_ep_devices() - Populate the endpoint devices on the DP AUX
187  * @aux: The AUX channel whose devices we want to populate. It is required that
188  *       drm_dp_aux_init() has already been called for this AUX channel.
189  *
190  * This will populate all the devices under the "aux-bus" node of the device
191  * providing the AUX channel (AKA aux->dev).
192  *
193  * When this function finishes, it is _possible_ (but not guaranteed) that
194  * our sub-devices will have finished probing. It should be noted that if our
195  * sub-devices return -EPROBE_DEFER that we will not return any error codes
196  * ourselves but our sub-devices will _not_ have actually probed successfully
197  * yet. There may be other cases (maybe added in the future?) where sub-devices
198  * won't have been probed yet when this function returns, so it's best not to
199  * rely on that.
200  *
201  * If this function succeeds you should later make sure you call
202  * of_dp_aux_depopulate_ep_devices() to undo it, or just use the devm version
203  * of this function.
204  *
205  * Return: 0 if no error or negative error code.
206  */
207 int of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux)
208 {
209         struct device_node *bus, *np;
210         struct dp_aux_ep_device *aux_ep;
211         int ret;
212
213         /* drm_dp_aux_init() should have been called already; warn if not */
214         WARN_ON_ONCE(!aux->ddc.algo);
215
216         if (!aux->dev->of_node)
217                 return 0;
218
219         bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
220         if (!bus)
221                 return 0;
222
223         for_each_available_child_of_node(bus, np) {
224                 if (of_node_test_and_set_flag(np, OF_POPULATED))
225                         continue;
226
227                 aux_ep = kzalloc(sizeof(*aux_ep), GFP_KERNEL);
228                 if (!aux_ep)
229                         continue;
230                 aux_ep->aux = aux;
231
232                 aux_ep->dev.parent = aux->dev;
233                 aux_ep->dev.bus = &dp_aux_bus_type;
234                 aux_ep->dev.type = &dp_aux_device_type_type;
235                 aux_ep->dev.of_node = of_node_get(np);
236                 dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
237
238                 ret = device_register(&aux_ep->dev);
239                 if (ret) {
240                         dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
241                         of_node_clear_flag(np, OF_POPULATED);
242                         of_node_put(np);
243
244                         /*
245                          * As per docs of device_register(), call this instead
246                          * of kfree() directly for error cases.
247                          */
248                         put_device(&aux_ep->dev);
249
250                         /*
251                          * Following in the footsteps of of_i2c_register_devices(),
252                          * we won't fail the whole function here--we'll just
253                          * continue registering any other devices we find.
254                          */
255                 }
256         }
257
258         of_node_put(bus);
259
260         return 0;
261 }
262
263 static void of_dp_aux_depopulate_ep_devices_void(void *data)
264 {
265         of_dp_aux_depopulate_ep_devices(data);
266 }
267
268 /**
269  * devm_of_dp_aux_populate_ep_devices() - devm wrapper for of_dp_aux_populate_ep_devices()
270  * @aux: The AUX channel whose devices we want to populate
271  *
272  * Handles freeing w/ devm on the device "aux->dev".
273  *
274  * Return: 0 if no error or negative error code.
275  */
276 int devm_of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux)
277 {
278         int ret;
279
280         ret = of_dp_aux_populate_ep_devices(aux);
281         if (ret)
282                 return ret;
283
284         return devm_add_action_or_reset(aux->dev,
285                                         of_dp_aux_depopulate_ep_devices_void,
286                                         aux);
287 }
288 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_ep_devices);
289
290 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
291 {
292         drv->driver.owner = owner;
293         drv->driver.bus = &dp_aux_bus_type;
294
295         return driver_register(&drv->driver);
296 }
297 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
298
299 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
300 {
301         driver_unregister(&drv->driver);
302 }
303 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
304
305 static int __init dp_aux_bus_init(void)
306 {
307         int ret;
308
309         ret = bus_register(&dp_aux_bus_type);
310         if (ret)
311                 return ret;
312
313         return 0;
314 }
315
316 static void __exit dp_aux_bus_exit(void)
317 {
318         bus_unregister(&dp_aux_bus_type);
319 }
320
321 subsys_initcall(dp_aux_bus_init);
322 module_exit(dp_aux_bus_exit);
323
324 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
325 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
326 MODULE_LICENSE("GPL v2");