xref: /linux/drivers/gpu/drm/bridge/aux-bridge.c (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
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 				 struct drm_encoder *encoder,
90 				 enum drm_bridge_attach_flags flags)
91 {
92 	struct drm_aux_bridge_data *data;
93 
94 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
95 		return -EINVAL;
96 
97 	data = container_of(bridge, struct drm_aux_bridge_data, bridge);
98 
99 	return drm_bridge_attach(encoder, data->next_bridge, bridge,
100 				 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
101 }
102 
103 static const struct drm_bridge_funcs drm_aux_bridge_funcs = {
104 	.attach	= drm_aux_bridge_attach,
105 };
106 
107 static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,
108 				const struct auxiliary_device_id *id)
109 {
110 	struct drm_aux_bridge_data *data;
111 
112 	data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
113 	if (!data)
114 		return -ENOMEM;
115 
116 	data->dev = &auxdev->dev;
117 	data->next_bridge = devm_drm_of_get_bridge(&auxdev->dev, auxdev->dev.of_node, 0, 0);
118 	if (IS_ERR(data->next_bridge))
119 		return dev_err_probe(&auxdev->dev, PTR_ERR(data->next_bridge),
120 				     "failed to acquire drm_bridge\n");
121 
122 	data->bridge.funcs = &drm_aux_bridge_funcs;
123 	data->bridge.of_node = data->dev->of_node;
124 
125 	/* passthrough data, allow everything */
126 	data->bridge.interlace_allowed = true;
127 	data->bridge.ycbcr_420_allowed = true;
128 
129 	return devm_drm_bridge_add(data->dev, &data->bridge);
130 }
131 
132 static const struct auxiliary_device_id drm_aux_bridge_table[] = {
133 	{ .name = KBUILD_MODNAME ".aux_bridge" },
134 	{},
135 };
136 MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);
137 
138 static struct auxiliary_driver drm_aux_bridge_drv = {
139 	.name = "aux_bridge",
140 	.id_table = drm_aux_bridge_table,
141 	.probe = drm_aux_bridge_probe,
142 };
143 module_auxiliary_driver(drm_aux_bridge_drv);
144 
145 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
146 MODULE_DESCRIPTION("DRM transparent bridge");
147 MODULE_LICENSE("GPL");
148