xref: /linux/drivers/gpu/drm/bridge/aux-bridge.c (revision 0b364cf53b20204e92bac7c6ebd1ee7d3ec62931)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2023 Linaro Ltd.
4  *
5  * Author: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
6  */
7 #include <linux/auxiliary_bus.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 
11 #include <drm/drm_bridge.h>
12 #include <drm/bridge/aux-bridge.h>
13 
14 static DEFINE_IDA(drm_aux_bridge_ida);
15 
16 static void drm_aux_bridge_release(struct device *dev)
17 {
18 	struct auxiliary_device *adev = to_auxiliary_dev(dev);
19 
20 	ida_free(&drm_aux_bridge_ida, adev->id);
21 
22 	kfree(adev);
23 }
24 
25 static void drm_aux_bridge_unregister_adev(void *_adev)
26 {
27 	struct auxiliary_device *adev = _adev;
28 
29 	auxiliary_device_delete(adev);
30 	auxiliary_device_uninit(adev);
31 }
32 
33 /**
34  * drm_aux_bridge_register - Create a simple bridge device to link the chain
35  * @parent: device instance providing this bridge
36  *
37  * Creates a simple DRM bridge that doesn't implement any drm_bridge
38  * operations. Such bridges merely fill a place in the bridge chain linking
39  * surrounding DRM bridges.
40  *
41  * Return: zero on success, negative error code on failure
42  */
43 int drm_aux_bridge_register(struct device *parent)
44 {
45 	struct auxiliary_device *adev;
46 	int ret;
47 
48 	adev = kzalloc(sizeof(*adev), GFP_KERNEL);
49 	if (!adev)
50 		return -ENOMEM;
51 
52 	ret = ida_alloc(&drm_aux_bridge_ida, GFP_KERNEL);
53 	if (ret < 0) {
54 		kfree(adev);
55 		return ret;
56 	}
57 
58 	adev->id = ret;
59 	adev->name = "aux_bridge";
60 	adev->dev.parent = parent;
61 	adev->dev.release = drm_aux_bridge_release;
62 
63 	device_set_of_node_from_dev(&adev->dev, parent);
64 
65 	ret = auxiliary_device_init(adev);
66 	if (ret) {
67 		ida_free(&drm_aux_bridge_ida, adev->id);
68 		kfree(adev);
69 		return ret;
70 	}
71 
72 	ret = auxiliary_device_add(adev);
73 	if (ret) {
74 		auxiliary_device_uninit(adev);
75 		return ret;
76 	}
77 
78 	return devm_add_action_or_reset(parent, drm_aux_bridge_unregister_adev, adev);
79 }
80 EXPORT_SYMBOL_GPL(drm_aux_bridge_register);
81 
82 struct drm_aux_bridge_data {
83 	struct drm_bridge bridge;
84 	struct drm_bridge *next_bridge;
85 	struct device *dev;
86 };
87 
88 static int drm_aux_bridge_attach(struct drm_bridge *bridge,
89 				 enum drm_bridge_attach_flags flags)
90 {
91 	struct drm_aux_bridge_data *data;
92 
93 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
94 		return -EINVAL;
95 
96 	data = container_of(bridge, struct drm_aux_bridge_data, bridge);
97 
98 	return drm_bridge_attach(bridge->encoder, data->next_bridge, bridge,
99 				 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
100 }
101 
102 static const struct drm_bridge_funcs drm_aux_bridge_funcs = {
103 	.attach	= drm_aux_bridge_attach,
104 };
105 
106 static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,
107 				const struct auxiliary_device_id *id)
108 {
109 	struct drm_aux_bridge_data *data;
110 
111 	data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
112 	if (!data)
113 		return -ENOMEM;
114 
115 	data->dev = &auxdev->dev;
116 	data->next_bridge = devm_drm_of_get_bridge(&auxdev->dev, auxdev->dev.of_node, 0, 0);
117 	if (IS_ERR(data->next_bridge))
118 		return dev_err_probe(&auxdev->dev, PTR_ERR(data->next_bridge),
119 				     "failed to acquire drm_bridge\n");
120 
121 	data->bridge.funcs = &drm_aux_bridge_funcs;
122 	data->bridge.of_node = data->dev->of_node;
123 
124 	return devm_drm_bridge_add(data->dev, &data->bridge);
125 }
126 
127 static const struct auxiliary_device_id drm_aux_bridge_table[] = {
128 	{ .name = KBUILD_MODNAME ".aux_bridge" },
129 	{},
130 };
131 MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);
132 
133 static struct auxiliary_driver drm_aux_bridge_drv = {
134 	.name = "aux_bridge",
135 	.id_table = drm_aux_bridge_table,
136 	.probe = drm_aux_bridge_probe,
137 };
138 module_auxiliary_driver(drm_aux_bridge_drv);
139 
140 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
141 MODULE_DESCRIPTION("DRM transparent bridge");
142 MODULE_LICENSE("GPL");
143