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_hpd_bridge_ida); 15 16 struct drm_aux_hpd_bridge_data { 17 struct drm_bridge bridge; 18 struct device *dev; 19 }; 20 21 static void drm_aux_hpd_bridge_release(struct device *dev) 22 { 23 struct auxiliary_device *adev = to_auxiliary_dev(dev); 24 25 ida_free(&drm_aux_hpd_bridge_ida, adev->id); 26 27 of_node_put(adev->dev.platform_data); 28 of_node_put(adev->dev.of_node); 29 30 kfree(adev); 31 } 32 33 static void drm_aux_hpd_bridge_free_adev(void *_adev) 34 { 35 auxiliary_device_uninit(_adev); 36 } 37 38 /** 39 * devm_drm_dp_hpd_bridge_alloc - allocate a HPD DisplayPort bridge 40 * @parent: device instance providing this bridge 41 * @np: device node pointer corresponding to this bridge instance 42 * 43 * Creates a simple DRM bridge with the type set to 44 * DRM_MODE_CONNECTOR_DisplayPort, which terminates the bridge chain and is 45 * able to send the HPD events. 46 * 47 * Return: bridge auxiliary device pointer or an error pointer 48 */ 49 struct auxiliary_device *devm_drm_dp_hpd_bridge_alloc(struct device *parent, struct device_node *np) 50 { 51 struct auxiliary_device *adev; 52 int ret; 53 54 adev = kzalloc(sizeof(*adev), GFP_KERNEL); 55 if (!adev) 56 return ERR_PTR(-ENOMEM); 57 58 ret = ida_alloc(&drm_aux_hpd_bridge_ida, GFP_KERNEL); 59 if (ret < 0) { 60 kfree(adev); 61 return ERR_PTR(ret); 62 } 63 64 adev->id = ret; 65 adev->name = "dp_hpd_bridge"; 66 adev->dev.parent = parent; 67 adev->dev.release = drm_aux_hpd_bridge_release; 68 adev->dev.platform_data = of_node_get(np); 69 70 device_set_of_node_from_dev(&adev->dev, parent); 71 72 ret = auxiliary_device_init(adev); 73 if (ret) { 74 of_node_put(adev->dev.platform_data); 75 of_node_put(adev->dev.of_node); 76 ida_free(&drm_aux_hpd_bridge_ida, adev->id); 77 kfree(adev); 78 return ERR_PTR(ret); 79 } 80 81 ret = devm_add_action_or_reset(parent, drm_aux_hpd_bridge_free_adev, adev); 82 if (ret) 83 return ERR_PTR(ret); 84 85 return adev; 86 } 87 EXPORT_SYMBOL_GPL(devm_drm_dp_hpd_bridge_alloc); 88 89 static void drm_aux_hpd_bridge_del_adev(void *_adev) 90 { 91 auxiliary_device_delete(_adev); 92 } 93 94 /** 95 * devm_drm_dp_hpd_bridge_add - register a HDP DisplayPort bridge 96 * @dev: struct device to tie registration lifetime to 97 * @adev: bridge auxiliary device to be registered 98 * 99 * Returns: zero on success or a negative errno 100 */ 101 int devm_drm_dp_hpd_bridge_add(struct device *dev, struct auxiliary_device *adev) 102 { 103 int ret; 104 105 ret = auxiliary_device_add(adev); 106 if (ret) 107 return ret; 108 109 return devm_add_action_or_reset(dev, drm_aux_hpd_bridge_del_adev, adev); 110 } 111 EXPORT_SYMBOL_GPL(devm_drm_dp_hpd_bridge_add); 112 113 /** 114 * drm_dp_hpd_bridge_register - allocate and register a HDP DisplayPort bridge 115 * @parent: device instance providing this bridge 116 * @np: device node pointer corresponding to this bridge instance 117 * 118 * Return: device instance that will handle created bridge or an error pointer 119 */ 120 struct device *drm_dp_hpd_bridge_register(struct device *parent, struct device_node *np) 121 { 122 struct auxiliary_device *adev; 123 int ret; 124 125 adev = devm_drm_dp_hpd_bridge_alloc(parent, np); 126 if (IS_ERR(adev)) 127 return ERR_CAST(adev); 128 129 ret = devm_drm_dp_hpd_bridge_add(parent, adev); 130 if (ret) 131 return ERR_PTR(ret); 132 133 return &adev->dev; 134 } 135 EXPORT_SYMBOL_GPL(drm_dp_hpd_bridge_register); 136 137 /** 138 * drm_aux_hpd_bridge_notify - notify hot plug detection events 139 * @dev: device created for the HPD bridge 140 * @status: output connection status 141 * 142 * A wrapper around drm_bridge_hpd_notify() that is used to report hot plug 143 * detection events for bridges created via drm_dp_hpd_bridge_register(). 144 * 145 * This function shall be called in a context that can sleep. 146 */ 147 void drm_aux_hpd_bridge_notify(struct device *dev, enum drm_connector_status status) 148 { 149 struct auxiliary_device *adev = to_auxiliary_dev(dev); 150 struct drm_aux_hpd_bridge_data *data = auxiliary_get_drvdata(adev); 151 152 if (!data) 153 return; 154 155 drm_bridge_hpd_notify(&data->bridge, status); 156 } 157 EXPORT_SYMBOL_GPL(drm_aux_hpd_bridge_notify); 158 159 static int drm_aux_hpd_bridge_attach(struct drm_bridge *bridge, 160 struct drm_encoder *encoder, 161 enum drm_bridge_attach_flags flags) 162 { 163 return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL; 164 } 165 166 static const struct drm_bridge_funcs drm_aux_hpd_bridge_funcs = { 167 .attach = drm_aux_hpd_bridge_attach, 168 }; 169 170 static int drm_aux_hpd_bridge_probe(struct auxiliary_device *auxdev, 171 const struct auxiliary_device_id *id) 172 { 173 struct drm_aux_hpd_bridge_data *data; 174 175 data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL); 176 if (!data) 177 return -ENOMEM; 178 179 data->dev = &auxdev->dev; 180 data->bridge.funcs = &drm_aux_hpd_bridge_funcs; 181 data->bridge.of_node = dev_get_platdata(data->dev); 182 data->bridge.ops = DRM_BRIDGE_OP_HPD; 183 data->bridge.type = id->driver_data; 184 185 /* passthrough data, allow everything */ 186 data->bridge.interlace_allowed = true; 187 data->bridge.ycbcr_420_allowed = true; 188 189 auxiliary_set_drvdata(auxdev, data); 190 191 return devm_drm_bridge_add(data->dev, &data->bridge); 192 } 193 194 static const struct auxiliary_device_id drm_aux_hpd_bridge_table[] = { 195 { .name = KBUILD_MODNAME ".dp_hpd_bridge", .driver_data = DRM_MODE_CONNECTOR_DisplayPort, }, 196 {}, 197 }; 198 MODULE_DEVICE_TABLE(auxiliary, drm_aux_hpd_bridge_table); 199 200 static struct auxiliary_driver drm_aux_hpd_bridge_drv = { 201 .name = "aux_hpd_bridge", 202 .id_table = drm_aux_hpd_bridge_table, 203 .probe = drm_aux_hpd_bridge_probe, 204 }; 205 module_auxiliary_driver(drm_aux_hpd_bridge_drv); 206 207 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>"); 208 MODULE_DESCRIPTION("DRM HPD bridge"); 209 MODULE_LICENSE("GPL"); 210