aux-bridge.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <drm/drm_bridge.h>
  11. #include <drm/bridge/aux-bridge.h>
  12. static DEFINE_IDA(drm_aux_bridge_ida);
  13. static void drm_aux_bridge_release(struct device *dev)
  14. {
  15. struct auxiliary_device *adev = to_auxiliary_dev(dev);
  16. ida_free(&drm_aux_bridge_ida, adev->id);
  17. kfree(adev);
  18. }
  19. static void drm_aux_bridge_unregister_adev(void *_adev)
  20. {
  21. struct auxiliary_device *adev = _adev;
  22. auxiliary_device_delete(adev);
  23. auxiliary_device_uninit(adev);
  24. }
  25. /**
  26. * drm_aux_bridge_register - Create a simple bridge device to link the chain
  27. * @parent: device instance providing this bridge
  28. *
  29. * Creates a simple DRM bridge that doesn't implement any drm_bridge
  30. * operations. Such bridges merely fill a place in the bridge chain linking
  31. * surrounding DRM bridges.
  32. *
  33. * Return: zero on success, negative error code on failure
  34. */
  35. int drm_aux_bridge_register(struct device *parent)
  36. {
  37. struct auxiliary_device *adev;
  38. int ret;
  39. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  40. if (!adev)
  41. return -ENOMEM;
  42. ret = ida_alloc(&drm_aux_bridge_ida, GFP_KERNEL);
  43. if (ret < 0) {
  44. kfree(adev);
  45. return ret;
  46. }
  47. adev->id = ret;
  48. adev->name = "aux_bridge";
  49. adev->dev.parent = parent;
  50. adev->dev.release = drm_aux_bridge_release;
  51. device_set_of_node_from_dev(&adev->dev, parent);
  52. ret = auxiliary_device_init(adev);
  53. if (ret) {
  54. ida_free(&drm_aux_bridge_ida, adev->id);
  55. kfree(adev);
  56. return ret;
  57. }
  58. ret = auxiliary_device_add(adev);
  59. if (ret) {
  60. auxiliary_device_uninit(adev);
  61. return ret;
  62. }
  63. return devm_add_action_or_reset(parent, drm_aux_bridge_unregister_adev, adev);
  64. }
  65. EXPORT_SYMBOL_GPL(drm_aux_bridge_register);
  66. struct drm_aux_bridge_data {
  67. struct drm_bridge bridge;
  68. struct drm_bridge *next_bridge;
  69. struct device *dev;
  70. };
  71. static int drm_aux_bridge_attach(struct drm_bridge *bridge,
  72. enum drm_bridge_attach_flags flags)
  73. {
  74. struct drm_aux_bridge_data *data;
  75. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
  76. return -EINVAL;
  77. data = container_of(bridge, struct drm_aux_bridge_data, bridge);
  78. return drm_bridge_attach(bridge->encoder, data->next_bridge, bridge,
  79. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  80. }
  81. static const struct drm_bridge_funcs drm_aux_bridge_funcs = {
  82. .attach = drm_aux_bridge_attach,
  83. };
  84. static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,
  85. const struct auxiliary_device_id *id)
  86. {
  87. struct drm_aux_bridge_data *data;
  88. data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);
  89. if (!data)
  90. return -ENOMEM;
  91. data->dev = &auxdev->dev;
  92. data->next_bridge = devm_drm_of_get_bridge(&auxdev->dev, auxdev->dev.of_node, 0, 0);
  93. if (IS_ERR(data->next_bridge))
  94. return dev_err_probe(&auxdev->dev, PTR_ERR(data->next_bridge),
  95. "failed to acquire drm_bridge\n");
  96. data->bridge.funcs = &drm_aux_bridge_funcs;
  97. data->bridge.of_node = data->dev->of_node;
  98. return devm_drm_bridge_add(data->dev, &data->bridge);
  99. }
  100. static const struct auxiliary_device_id drm_aux_bridge_table[] = {
  101. { .name = KBUILD_MODNAME ".aux_bridge" },
  102. {},
  103. };
  104. MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);
  105. static struct auxiliary_driver drm_aux_bridge_drv = {
  106. .name = "aux_bridge",
  107. .id_table = drm_aux_bridge_table,
  108. .probe = drm_aux_bridge_probe,
  109. };
  110. module_auxiliary_driver(drm_aux_bridge_drv);
  111. MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
  112. MODULE_DESCRIPTION("DRM transparent bridge");
  113. MODULE_LICENSE("GPL");