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