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