]> Git Repo - linux.git/blob - drivers/gpu/drm/display/drm_dp_aux_bus.c
Merge tag 'driver-core-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / gpu / drm / display / 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 device on the far side of the bus is referred to as an
7  * endpoint in this code.
8  *
9  * There is only one device connected to the DP AUX bus: an eDP 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/display/drm_dp_aux_bus.h>
23 #include <drm/display/drm_dp_helper.h>
24
25 struct dp_aux_ep_device_with_data {
26         struct dp_aux_ep_device aux_ep;
27         int (*done_probing)(struct drm_dp_aux *aux);
28 };
29
30 /**
31  * dp_aux_ep_match() - The match function for the dp_aux_bus.
32  * @dev: The device to match.
33  * @drv: The driver to try to match against.
34  *
35  * At the moment, we just match on device tree.
36  *
37  * Return: True if this driver matches this device; false otherwise.
38  */
39 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
40 {
41         return !!of_match_device(drv->of_match_table, dev);
42 }
43
44 /**
45  * dp_aux_ep_probe() - The probe function for the dp_aux_bus.
46  * @dev: The device to probe.
47  *
48  * Calls through to the endpoint driver probe.
49  *
50  * Return: 0 if no error or negative error code.
51  */
52 static int dp_aux_ep_probe(struct device *dev)
53 {
54         struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
55         struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
56         struct dp_aux_ep_device_with_data *aux_ep_with_data =
57                 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
58         int ret;
59
60         ret = dev_pm_domain_attach(dev, true);
61         if (ret)
62                 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
63
64         ret = aux_ep_drv->probe(aux_ep);
65         if (ret)
66                 goto err_attached;
67
68         if (aux_ep_with_data->done_probing) {
69                 ret = aux_ep_with_data->done_probing(aux_ep->aux);
70                 if (ret) {
71                         /*
72                          * The done_probing() callback should not return
73                          * -EPROBE_DEFER to us. If it does, we treat it as an
74                          * error. Passing it on as-is would cause the _panel_
75                          * to defer.
76                          */
77                         if (ret == -EPROBE_DEFER) {
78                                 dev_err(dev,
79                                         "DP AUX done_probing() can't defer\n");
80                                 ret = -EINVAL;
81                         }
82                         goto err_probed;
83                 }
84         }
85
86         return 0;
87 err_probed:
88         if (aux_ep_drv->remove)
89                 aux_ep_drv->remove(aux_ep);
90 err_attached:
91         dev_pm_domain_detach(dev, true);
92
93         return ret;
94 }
95
96 /**
97  * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
98  * @dev: The device to remove.
99  *
100  * Calls through to the endpoint driver remove.
101  */
102 static void dp_aux_ep_remove(struct device *dev)
103 {
104         struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
105         struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
106
107         if (aux_ep_drv->remove)
108                 aux_ep_drv->remove(aux_ep);
109         dev_pm_domain_detach(dev, true);
110 }
111
112 /**
113  * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
114  * @dev: The device to shutdown.
115  *
116  * Calls through to the endpoint driver shutdown.
117  */
118 static void dp_aux_ep_shutdown(struct device *dev)
119 {
120         struct dp_aux_ep_driver *aux_ep_drv;
121
122         if (!dev->driver)
123                 return;
124
125         aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
126         if (aux_ep_drv->shutdown)
127                 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
128 }
129
130 static struct bus_type dp_aux_bus_type = {
131         .name           = "dp-aux",
132         .match          = dp_aux_ep_match,
133         .probe          = dp_aux_ep_probe,
134         .remove         = dp_aux_ep_remove,
135         .shutdown       = dp_aux_ep_shutdown,
136 };
137
138 static ssize_t modalias_show(struct device *dev,
139                              struct device_attribute *attr, char *buf)
140 {
141         return of_device_modalias(dev, buf, PAGE_SIZE);
142 }
143 static DEVICE_ATTR_RO(modalias);
144
145 static struct attribute *dp_aux_ep_dev_attrs[] = {
146         &dev_attr_modalias.attr,
147         NULL,
148 };
149 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
150
151 /**
152  * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
153  * @dev: The device to free.
154  */
155 static void dp_aux_ep_dev_release(struct device *dev)
156 {
157         struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
158         struct dp_aux_ep_device_with_data *aux_ep_with_data =
159                 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
160
161         kfree(aux_ep_with_data);
162 }
163
164 static struct device_type dp_aux_device_type_type = {
165         .groups         = dp_aux_ep_dev_groups,
166         .uevent         = of_device_uevent_modalias,
167         .release        = dp_aux_ep_dev_release,
168 };
169
170 /**
171  * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
172  * @dev: The device to destroy.
173  * @data: Not used
174  *
175  * This is just used as a callback by of_dp_aux_depopulate_bus() and
176  * is called for _all_ of the child devices of the device providing the AUX bus.
177  * We'll only act on those that are of type "dp_aux_bus_type".
178  *
179  * This function is effectively an inverse of what's in
180  * of_dp_aux_populate_bus(). NOTE: since we only populate one child
181  * then it's expected that only one device will match all the "if" tests in
182  * this function and get to the device_unregister().
183  *
184  * Return: 0 if no error or negative error code.
185  */
186 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
187 {
188         struct device_node *np = dev->of_node;
189
190         if (dev->bus != &dp_aux_bus_type)
191                 return 0;
192
193         if (!of_node_check_flag(np, OF_POPULATED))
194                 return 0;
195
196         of_node_clear_flag(np, OF_POPULATED);
197         of_node_put(np);
198
199         device_unregister(dev);
200
201         return 0;
202 }
203
204 /**
205  * of_dp_aux_depopulate_bus() - Undo of_dp_aux_populate_bus
206  * @aux: The AUX channel whose device we want to depopulate
207  *
208  * This will destroy the device that was created
209  * by of_dp_aux_populate_bus().
210  */
211 void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)
212 {
213         device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
214 }
215 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);
216
217 /**
218  * of_dp_aux_populate_bus() - Populate the endpoint device on the DP AUX
219  * @aux: The AUX channel whose device we want to populate. It is required that
220  *       drm_dp_aux_init() has already been called for this AUX channel.
221  * @done_probing: Callback functions to call after EP device finishes probing.
222  *                Will not be called if there are no EP devices and this
223  *                function will return -ENODEV.
224  *
225  * This will populate the device (expected to be an eDP panel) under the
226  * "aux-bus" node of the device providing the AUX channel (AKA aux->dev).
227  *
228  * When this function finishes, it is _possible_ (but not guaranteed) that
229  * our sub-device will have finished probing. It should be noted that if our
230  * sub-device returns -EPROBE_DEFER or is probing asynchronously for some
231  * reason that we will not return any error codes ourselves but our
232  * sub-device will _not_ have actually probed successfully yet.
233  *
234  * In many cases it's important for the caller of this function to be notified
235  * when our sub device finishes probing. Our sub device is expected to be an
236  * eDP panel and the caller is expected to be an eDP controller. The eDP
237  * controller needs to be able to get a reference to the panel when it finishes
238  * probing. For this reason the caller can pass in a function pointer that
239  * will be called when our sub-device finishes probing.
240  *
241  * If this function succeeds you should later make sure you call
242  * of_dp_aux_depopulate_bus() to undo it, or just use the devm version
243  * of this function.
244  *
245  * Return: 0 if no error or negative error code; returns -ENODEV if there are
246  *         no children. The done_probing() function won't be called in that
247  *         case.
248  */
249 int of_dp_aux_populate_bus(struct drm_dp_aux *aux,
250                            int (*done_probing)(struct drm_dp_aux *aux))
251 {
252         struct device_node *bus = NULL, *np = NULL;
253         struct dp_aux_ep_device *aux_ep;
254         struct dp_aux_ep_device_with_data *aux_ep_with_data;
255         int ret;
256
257         /* drm_dp_aux_init() should have been called already; warn if not */
258         WARN_ON_ONCE(!aux->ddc.algo);
259
260         if (!aux->dev->of_node)
261                 return -ENODEV;
262         bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
263         if (!bus)
264                 return -ENODEV;
265
266         np = of_get_next_available_child(bus, NULL);
267         of_node_put(bus);
268         if (!np)
269                 return -ENODEV;
270
271         if (of_node_test_and_set_flag(np, OF_POPULATED)) {
272                 dev_err(aux->dev, "DP AUX EP device already populated\n");
273                 ret = -EINVAL;
274                 goto err_did_get_np;
275         }
276
277         aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);
278         if (!aux_ep_with_data) {
279                 ret = -ENOMEM;
280                 goto err_did_set_populated;
281         }
282
283         aux_ep_with_data->done_probing = done_probing;
284
285         aux_ep = &aux_ep_with_data->aux_ep;
286         aux_ep->aux = aux;
287         aux_ep->dev.parent = aux->dev;
288         aux_ep->dev.bus = &dp_aux_bus_type;
289         aux_ep->dev.type = &dp_aux_device_type_type;
290         aux_ep->dev.of_node = of_node_get(np);
291         dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
292
293         ret = device_register(&aux_ep->dev);
294         if (ret) {
295                 dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
296
297                 /*
298                  * As per docs of device_register(), call this instead
299                  * of kfree() directly for error cases.
300                  */
301                 put_device(&aux_ep->dev);
302
303                 goto err_did_set_populated;
304         }
305
306         return 0;
307
308 err_did_set_populated:
309         of_node_clear_flag(np, OF_POPULATED);
310
311 err_did_get_np:
312         of_node_put(np);
313
314         return ret;
315 }
316 EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);
317
318 static void of_dp_aux_depopulate_bus_void(void *data)
319 {
320         of_dp_aux_depopulate_bus(data);
321 }
322
323 /**
324  * devm_of_dp_aux_populate_bus() - devm wrapper for of_dp_aux_populate_bus()
325  * @aux: The AUX channel whose device we want to populate
326  * @done_probing: Callback functions to call after EP device finishes probing.
327  *                Will not be called if there are no EP devices and this
328  *                function will return -ENODEV.
329  *
330  * Handles freeing w/ devm on the device "aux->dev".
331  *
332  * Return: 0 if no error or negative error code; returns -ENODEV if there are
333  *         no children. The done_probing() function won't be called in that
334  *         case.
335  */
336 int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,
337                                 int (*done_probing)(struct drm_dp_aux *aux))
338 {
339         int ret;
340
341         ret = of_dp_aux_populate_bus(aux, done_probing);
342         if (ret)
343                 return ret;
344
345         return devm_add_action_or_reset(aux->dev,
346                                         of_dp_aux_depopulate_bus_void, aux);
347 }
348 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);
349
350 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
351 {
352         drv->driver.owner = owner;
353         drv->driver.bus = &dp_aux_bus_type;
354
355         return driver_register(&drv->driver);
356 }
357 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
358
359 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
360 {
361         driver_unregister(&drv->driver);
362 }
363 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
364
365 static int __init dp_aux_bus_init(void)
366 {
367         int ret;
368
369         ret = bus_register(&dp_aux_bus_type);
370         if (ret)
371                 return ret;
372
373         return 0;
374 }
375
376 static void __exit dp_aux_bus_exit(void)
377 {
378         bus_unregister(&dp_aux_bus_type);
379 }
380
381 subsys_initcall(dp_aux_bus_init);
382 module_exit(dp_aux_bus_exit);
383
384 MODULE_AUTHOR("Douglas Anderson <[email protected]>");
385 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
386 MODULE_LICENSE("GPL v2");
This page took 0.063622 seconds and 4 git commands to generate.