1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2023 Linaro Ltd.
7 #include <linux/auxiliary_bus.h>
8 #include <linux/module.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/bridge/aux-bridge.h>
14 static DEFINE_IDA(drm_aux_bridge_ida);
16 static void drm_aux_bridge_release(struct device *dev)
18 struct auxiliary_device *adev = to_auxiliary_dev(dev);
20 ida_free(&drm_aux_bridge_ida, adev->id);
25 static void drm_aux_bridge_unregister_adev(void *_adev)
27 struct auxiliary_device *adev = _adev;
29 auxiliary_device_delete(adev);
30 auxiliary_device_uninit(adev);
34 * drm_aux_bridge_register - Create a simple bridge device to link the chain
35 * @parent: device instance providing this bridge
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.
41 * Return: zero on success, negative error code on failure
43 int drm_aux_bridge_register(struct device *parent)
45 struct auxiliary_device *adev;
48 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
52 ret = ida_alloc(&drm_aux_bridge_ida, GFP_KERNEL);
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;
64 ret = auxiliary_device_init(adev);
66 ida_free(&drm_aux_bridge_ida, adev->id);
71 ret = auxiliary_device_add(adev);
73 auxiliary_device_uninit(adev);
77 return devm_add_action_or_reset(parent, drm_aux_bridge_unregister_adev, adev);
79 EXPORT_SYMBOL_GPL(drm_aux_bridge_register);
81 struct drm_aux_bridge_data {
82 struct drm_bridge bridge;
83 struct drm_bridge *next_bridge;
87 static int drm_aux_bridge_attach(struct drm_bridge *bridge,
88 enum drm_bridge_attach_flags flags)
90 struct drm_aux_bridge_data *data;
92 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
95 data = container_of(bridge, struct drm_aux_bridge_data, bridge);
97 return drm_bridge_attach(bridge->encoder, data->next_bridge, bridge,
98 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
101 static const struct drm_bridge_funcs drm_aux_bridge_funcs = {
102 .attach = drm_aux_bridge_attach,
105 static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,
106 const struct auxiliary_device_id *id)
108 struct drm_aux_bridge_data *data;
110 data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
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");
120 data->bridge.funcs = &drm_aux_bridge_funcs;
121 data->bridge.of_node = data->dev->of_node;
123 return devm_drm_bridge_add(data->dev, &data->bridge);
126 static const struct auxiliary_device_id drm_aux_bridge_table[] = {
127 { .name = KBUILD_MODNAME ".aux_bridge" },
130 MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);
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,
137 module_auxiliary_driver(drm_aux_bridge_drv);
140 MODULE_DESCRIPTION("DRM transparent bridge");
141 MODULE_LICENSE("GPL");