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