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_hpd_bridge_ida);
16 struct drm_aux_hpd_bridge_data {
17 struct drm_bridge bridge;
21 static void drm_aux_hpd_bridge_release(struct device *dev)
23 struct auxiliary_device *adev = to_auxiliary_dev(dev);
25 ida_free(&drm_aux_hpd_bridge_ida, adev->id);
27 of_node_put(adev->dev.platform_data);
32 static void drm_aux_hpd_bridge_unregister_adev(void *_adev)
34 struct auxiliary_device *adev = _adev;
36 auxiliary_device_delete(adev);
37 auxiliary_device_uninit(adev);
41 * drm_dp_hpd_bridge_register - Create a simple HPD DisplayPort bridge
42 * @parent: device instance providing this bridge
43 * @np: device node pointer corresponding to this bridge instance
45 * Creates a simple DRM bridge with the type set to
46 * DRM_MODE_CONNECTOR_DisplayPort, which terminates the bridge chain and is
47 * able to send the HPD events.
49 * Return: device instance that will handle created bridge or an error code
50 * encoded into the pointer.
52 struct device *drm_dp_hpd_bridge_register(struct device *parent,
53 struct device_node *np)
55 struct auxiliary_device *adev;
58 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
60 return ERR_PTR(-ENOMEM);
62 ret = ida_alloc(&drm_aux_hpd_bridge_ida, GFP_KERNEL);
69 adev->name = "dp_hpd_bridge";
70 adev->dev.parent = parent;
71 adev->dev.of_node = of_node_get(parent->of_node);
72 adev->dev.release = drm_aux_hpd_bridge_release;
73 adev->dev.platform_data = of_node_get(np);
75 ret = auxiliary_device_init(adev);
77 ida_free(&drm_aux_hpd_bridge_ida, adev->id);
82 ret = auxiliary_device_add(adev);
84 auxiliary_device_uninit(adev);
88 ret = devm_add_action_or_reset(parent, drm_aux_hpd_bridge_unregister_adev, adev);
94 EXPORT_SYMBOL_GPL(drm_dp_hpd_bridge_register);
97 * drm_aux_hpd_bridge_notify - notify hot plug detection events
98 * @dev: device created for the HPD bridge
99 * @status: output connection status
101 * A wrapper around drm_bridge_hpd_notify() that is used to report hot plug
102 * detection events for bridges created via drm_dp_hpd_bridge_register().
104 * This function shall be called in a context that can sleep.
106 void drm_aux_hpd_bridge_notify(struct device *dev, enum drm_connector_status status)
108 struct auxiliary_device *adev = to_auxiliary_dev(dev);
109 struct drm_aux_hpd_bridge_data *data = auxiliary_get_drvdata(adev);
114 drm_bridge_hpd_notify(&data->bridge, status);
116 EXPORT_SYMBOL_GPL(drm_aux_hpd_bridge_notify);
118 static int drm_aux_hpd_bridge_attach(struct drm_bridge *bridge,
119 enum drm_bridge_attach_flags flags)
121 return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
124 static const struct drm_bridge_funcs drm_aux_hpd_bridge_funcs = {
125 .attach = drm_aux_hpd_bridge_attach,
128 static int drm_aux_hpd_bridge_probe(struct auxiliary_device *auxdev,
129 const struct auxiliary_device_id *id)
131 struct drm_aux_hpd_bridge_data *data;
133 data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
137 data->dev = &auxdev->dev;
138 data->bridge.funcs = &drm_aux_hpd_bridge_funcs;
139 data->bridge.of_node = dev_get_platdata(data->dev);
140 data->bridge.ops = DRM_BRIDGE_OP_HPD;
141 data->bridge.type = id->driver_data;
143 auxiliary_set_drvdata(auxdev, data);
145 return devm_drm_bridge_add(data->dev, &data->bridge);
148 static const struct auxiliary_device_id drm_aux_hpd_bridge_table[] = {
149 { .name = KBUILD_MODNAME ".dp_hpd_bridge", .driver_data = DRM_MODE_CONNECTOR_DisplayPort, },
152 MODULE_DEVICE_TABLE(auxiliary, drm_aux_hpd_bridge_table);
154 static struct auxiliary_driver drm_aux_hpd_bridge_drv = {
155 .name = "aux_hpd_bridge",
156 .id_table = drm_aux_hpd_bridge_table,
157 .probe = drm_aux_hpd_bridge_probe,
159 module_auxiliary_driver(drm_aux_hpd_bridge_drv);
162 MODULE_DESCRIPTION("DRM HPD bridge");
163 MODULE_LICENSE("GPL");