xref: /linux/drivers/gpu/drm/bridge/aux-bridge.c (revision 119ff04864a24470b1e531bb53e5c141aa8fefb0)
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.of_node = of_node_get(parent->of_node);
62 	adev->dev.release = drm_aux_bridge_release;
63 
64 	ret = auxiliary_device_init(adev);
65 	if (ret) {
66 		ida_free(&drm_aux_bridge_ida, adev->id);
67 		kfree(adev);
68 		return ret;
69 	}
70 
71 	ret = auxiliary_device_add(adev);
72 	if (ret) {
73 		auxiliary_device_uninit(adev);
74 		return ret;
75 	}
76 
77 	return devm_add_action_or_reset(parent, drm_aux_bridge_unregister_adev, adev);
78 }
79 EXPORT_SYMBOL_GPL(drm_aux_bridge_register);
80 
81 struct drm_aux_bridge_data {
82 	struct drm_bridge bridge;
83 	struct drm_bridge *next_bridge;
84 	struct device *dev;
85 };
86 
87 static int drm_aux_bridge_attach(struct drm_bridge *bridge,
88 				 enum drm_bridge_attach_flags flags)
89 {
90 	struct drm_aux_bridge_data *data;
91 
92 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
93 		return -EINVAL;
94 
95 	data = container_of(bridge, struct drm_aux_bridge_data, bridge);
96 
97 	return drm_bridge_attach(bridge->encoder, data->next_bridge, bridge,
98 				 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
99 }
100 
101 static const struct drm_bridge_funcs drm_aux_bridge_funcs = {
102 	.attach	= drm_aux_bridge_attach,
103 };
104 
105 static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,
106 				const struct auxiliary_device_id *id)
107 {
108 	struct drm_aux_bridge_data *data;
109 
110 	data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
111 	if (!data)
112 		return -ENOMEM;
113 
114 	data->dev = &auxdev->dev;
115 	data->next_bridge = devm_drm_of_get_bridge(&auxdev->dev, auxdev->dev.of_node, 0, 0);
116 	if (IS_ERR(data->next_bridge))
117 		return dev_err_probe(&auxdev->dev, PTR_ERR(data->next_bridge),
118 				     "failed to acquire drm_bridge\n");
119 
120 	data->bridge.funcs = &drm_aux_bridge_funcs;
121 	data->bridge.of_node = data->dev->of_node;
122 
123 	return devm_drm_bridge_add(data->dev, &data->bridge);
124 }
125 
126 static const struct auxiliary_device_id drm_aux_bridge_table[] = {
127 	{ .name = KBUILD_MODNAME ".aux_bridge" },
128 	{},
129 };
130 MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);
131 
132 static struct auxiliary_driver drm_aux_bridge_drv = {
133 	.name = "aux_bridge",
134 	.id_table = drm_aux_bridge_table,
135 	.probe = drm_aux_bridge_probe,
136 };
137 module_auxiliary_driver(drm_aux_bridge_drv);
138 
139 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
140 MODULE_DESCRIPTION("DRM transparent bridge");
141 MODULE_LICENSE("GPL");
142