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