0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/init.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/of_device.h>
0019 #include <linux/pm_domain.h>
0020 #include <linux/pm_runtime.h>
0021
0022 #include <drm/display/drm_dp_aux_bus.h>
0023 #include <drm/display/drm_dp_helper.h>
0024
0025 struct dp_aux_ep_device_with_data {
0026 struct dp_aux_ep_device aux_ep;
0027 int (*done_probing)(struct drm_dp_aux *aux);
0028 };
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
0040 {
0041 return !!of_match_device(drv->of_match_table, dev);
0042 }
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 static int dp_aux_ep_probe(struct device *dev)
0053 {
0054 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0055 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0056 struct dp_aux_ep_device_with_data *aux_ep_with_data =
0057 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
0058 int ret;
0059
0060 ret = dev_pm_domain_attach(dev, true);
0061 if (ret)
0062 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
0063
0064 ret = aux_ep_drv->probe(aux_ep);
0065 if (ret)
0066 goto err_attached;
0067
0068 if (aux_ep_with_data->done_probing) {
0069 ret = aux_ep_with_data->done_probing(aux_ep->aux);
0070 if (ret) {
0071
0072
0073
0074
0075
0076
0077 if (ret == -EPROBE_DEFER) {
0078 dev_err(dev,
0079 "DP AUX done_probing() can't defer\n");
0080 ret = -EINVAL;
0081 }
0082 goto err_probed;
0083 }
0084 }
0085
0086 return 0;
0087 err_probed:
0088 if (aux_ep_drv->remove)
0089 aux_ep_drv->remove(aux_ep);
0090 err_attached:
0091 dev_pm_domain_detach(dev, true);
0092
0093 return ret;
0094 }
0095
0096
0097
0098
0099
0100
0101
0102 static void dp_aux_ep_remove(struct device *dev)
0103 {
0104 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0105 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0106
0107 if (aux_ep_drv->remove)
0108 aux_ep_drv->remove(aux_ep);
0109 dev_pm_domain_detach(dev, true);
0110 }
0111
0112
0113
0114
0115
0116
0117
0118 static void dp_aux_ep_shutdown(struct device *dev)
0119 {
0120 struct dp_aux_ep_driver *aux_ep_drv;
0121
0122 if (!dev->driver)
0123 return;
0124
0125 aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0126 if (aux_ep_drv->shutdown)
0127 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
0128 }
0129
0130 static struct bus_type dp_aux_bus_type = {
0131 .name = "dp-aux",
0132 .match = dp_aux_ep_match,
0133 .probe = dp_aux_ep_probe,
0134 .remove = dp_aux_ep_remove,
0135 .shutdown = dp_aux_ep_shutdown,
0136 };
0137
0138 static ssize_t modalias_show(struct device *dev,
0139 struct device_attribute *attr, char *buf)
0140 {
0141 return of_device_modalias(dev, buf, PAGE_SIZE);
0142 }
0143 static DEVICE_ATTR_RO(modalias);
0144
0145 static struct attribute *dp_aux_ep_dev_attrs[] = {
0146 &dev_attr_modalias.attr,
0147 NULL,
0148 };
0149 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
0150
0151
0152
0153
0154
0155 static void dp_aux_ep_dev_release(struct device *dev)
0156 {
0157 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0158 struct dp_aux_ep_device_with_data *aux_ep_with_data =
0159 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
0160
0161 kfree(aux_ep_with_data);
0162 }
0163
0164 static struct device_type dp_aux_device_type_type = {
0165 .groups = dp_aux_ep_dev_groups,
0166 .uevent = of_device_uevent_modalias,
0167 .release = dp_aux_ep_dev_release,
0168 };
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
0187 {
0188 struct device_node *np = dev->of_node;
0189
0190 if (dev->bus != &dp_aux_bus_type)
0191 return 0;
0192
0193 if (!of_node_check_flag(np, OF_POPULATED))
0194 return 0;
0195
0196 of_node_clear_flag(np, OF_POPULATED);
0197 of_node_put(np);
0198
0199 device_unregister(dev);
0200
0201 return 0;
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211 void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)
0212 {
0213 device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
0214 }
0215 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249 int of_dp_aux_populate_bus(struct drm_dp_aux *aux,
0250 int (*done_probing)(struct drm_dp_aux *aux))
0251 {
0252 struct device_node *bus = NULL, *np = NULL;
0253 struct dp_aux_ep_device *aux_ep;
0254 struct dp_aux_ep_device_with_data *aux_ep_with_data;
0255 int ret;
0256
0257
0258 WARN_ON_ONCE(!aux->ddc.algo);
0259
0260 if (!aux->dev->of_node)
0261 return -ENODEV;
0262 bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
0263 if (!bus)
0264 return -ENODEV;
0265
0266 np = of_get_next_available_child(bus, NULL);
0267 of_node_put(bus);
0268 if (!np)
0269 return -ENODEV;
0270
0271 if (of_node_test_and_set_flag(np, OF_POPULATED)) {
0272 dev_err(aux->dev, "DP AUX EP device already populated\n");
0273 ret = -EINVAL;
0274 goto err_did_get_np;
0275 }
0276
0277 aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);
0278 if (!aux_ep_with_data) {
0279 ret = -ENOMEM;
0280 goto err_did_set_populated;
0281 }
0282
0283 aux_ep_with_data->done_probing = done_probing;
0284
0285 aux_ep = &aux_ep_with_data->aux_ep;
0286 aux_ep->aux = aux;
0287 aux_ep->dev.parent = aux->dev;
0288 aux_ep->dev.bus = &dp_aux_bus_type;
0289 aux_ep->dev.type = &dp_aux_device_type_type;
0290 aux_ep->dev.of_node = of_node_get(np);
0291 dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
0292
0293 ret = device_register(&aux_ep->dev);
0294 if (ret) {
0295 dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
0296
0297
0298
0299
0300
0301 put_device(&aux_ep->dev);
0302
0303 goto err_did_set_populated;
0304 }
0305
0306 return 0;
0307
0308 err_did_set_populated:
0309 of_node_clear_flag(np, OF_POPULATED);
0310
0311 err_did_get_np:
0312 of_node_put(np);
0313
0314 return ret;
0315 }
0316 EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);
0317
0318 static void of_dp_aux_depopulate_bus_void(void *data)
0319 {
0320 of_dp_aux_depopulate_bus(data);
0321 }
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336 int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,
0337 int (*done_probing)(struct drm_dp_aux *aux))
0338 {
0339 int ret;
0340
0341 ret = of_dp_aux_populate_bus(aux, done_probing);
0342 if (ret)
0343 return ret;
0344
0345 return devm_add_action_or_reset(aux->dev,
0346 of_dp_aux_depopulate_bus_void, aux);
0347 }
0348 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);
0349
0350 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
0351 {
0352 drv->driver.owner = owner;
0353 drv->driver.bus = &dp_aux_bus_type;
0354
0355 return driver_register(&drv->driver);
0356 }
0357 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
0358
0359 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
0360 {
0361 driver_unregister(&drv->driver);
0362 }
0363 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
0364
0365 static int __init dp_aux_bus_init(void)
0366 {
0367 int ret;
0368
0369 ret = bus_register(&dp_aux_bus_type);
0370 if (ret)
0371 return ret;
0372
0373 return 0;
0374 }
0375
0376 static void __exit dp_aux_bus_exit(void)
0377 {
0378 bus_unregister(&dp_aux_bus_type);
0379 }
0380
0381 subsys_initcall(dp_aux_bus_init);
0382 module_exit(dp_aux_bus_exit);
0383
0384 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
0385 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
0386 MODULE_LICENSE("GPL v2");