thc63lvd1024.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * THC63LVD1024 LVDS to parallel data DRM bridge driver.
  4. *
  5. * Copyright (C) 2018 Jacopo Mondi <jacopo+renesas@jmondi.org>
  6. */
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_graph.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/slab.h>
  14. #include <drm/drm_bridge.h>
  15. #include <drm/drm_panel.h>
  16. enum thc63_ports {
  17. THC63_LVDS_IN0,
  18. THC63_LVDS_IN1,
  19. THC63_RGB_OUT0,
  20. THC63_RGB_OUT1,
  21. };
  22. struct thc63_dev {
  23. struct device *dev;
  24. struct regulator *vcc;
  25. struct gpio_desc *pdwn;
  26. struct gpio_desc *oe;
  27. struct drm_bridge bridge;
  28. struct drm_bridge *next;
  29. struct drm_bridge_timings timings;
  30. };
  31. static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)
  32. {
  33. return container_of(bridge, struct thc63_dev, bridge);
  34. }
  35. static int thc63_attach(struct drm_bridge *bridge,
  36. enum drm_bridge_attach_flags flags)
  37. {
  38. struct thc63_dev *thc63 = to_thc63(bridge);
  39. return drm_bridge_attach(bridge->encoder, thc63->next, bridge, flags);
  40. }
  41. static enum drm_mode_status thc63_mode_valid(struct drm_bridge *bridge,
  42. const struct drm_display_info *info,
  43. const struct drm_display_mode *mode)
  44. {
  45. struct thc63_dev *thc63 = to_thc63(bridge);
  46. unsigned int min_freq;
  47. unsigned int max_freq;
  48. /*
  49. * The THC63LVD1024 pixel rate range is 8 to 135 MHz in all modes but
  50. * dual-in, single-out where it is 40 to 150 MHz. As dual-in, dual-out
  51. * isn't supported by the driver yet, simply derive the limits from the
  52. * input mode.
  53. */
  54. if (thc63->timings.dual_link) {
  55. min_freq = 40000;
  56. max_freq = 150000;
  57. } else {
  58. min_freq = 8000;
  59. max_freq = 135000;
  60. }
  61. if (mode->clock < min_freq)
  62. return MODE_CLOCK_LOW;
  63. if (mode->clock > max_freq)
  64. return MODE_CLOCK_HIGH;
  65. return MODE_OK;
  66. }
  67. static void thc63_enable(struct drm_bridge *bridge)
  68. {
  69. struct thc63_dev *thc63 = to_thc63(bridge);
  70. int ret;
  71. ret = regulator_enable(thc63->vcc);
  72. if (ret) {
  73. dev_err(thc63->dev,
  74. "Failed to enable regulator \"vcc\": %d\n", ret);
  75. return;
  76. }
  77. gpiod_set_value(thc63->pdwn, 0);
  78. gpiod_set_value(thc63->oe, 1);
  79. }
  80. static void thc63_disable(struct drm_bridge *bridge)
  81. {
  82. struct thc63_dev *thc63 = to_thc63(bridge);
  83. int ret;
  84. gpiod_set_value(thc63->oe, 0);
  85. gpiod_set_value(thc63->pdwn, 1);
  86. ret = regulator_disable(thc63->vcc);
  87. if (ret)
  88. dev_err(thc63->dev,
  89. "Failed to disable regulator \"vcc\": %d\n", ret);
  90. }
  91. static const struct drm_bridge_funcs thc63_bridge_func = {
  92. .attach = thc63_attach,
  93. .mode_valid = thc63_mode_valid,
  94. .enable = thc63_enable,
  95. .disable = thc63_disable,
  96. };
  97. static int thc63_parse_dt(struct thc63_dev *thc63)
  98. {
  99. struct device_node *endpoint;
  100. struct device_node *remote;
  101. endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
  102. THC63_RGB_OUT0, -1);
  103. if (!endpoint) {
  104. dev_err(thc63->dev, "Missing endpoint in port@%u\n",
  105. THC63_RGB_OUT0);
  106. return -ENODEV;
  107. }
  108. remote = of_graph_get_remote_port_parent(endpoint);
  109. of_node_put(endpoint);
  110. if (!remote) {
  111. dev_err(thc63->dev, "Endpoint in port@%u unconnected\n",
  112. THC63_RGB_OUT0);
  113. return -ENODEV;
  114. }
  115. if (!of_device_is_available(remote)) {
  116. dev_err(thc63->dev, "port@%u remote endpoint is disabled\n",
  117. THC63_RGB_OUT0);
  118. of_node_put(remote);
  119. return -ENODEV;
  120. }
  121. thc63->next = of_drm_find_bridge(remote);
  122. of_node_put(remote);
  123. if (!thc63->next)
  124. return -EPROBE_DEFER;
  125. endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
  126. THC63_LVDS_IN1, -1);
  127. if (endpoint) {
  128. remote = of_graph_get_remote_port_parent(endpoint);
  129. of_node_put(endpoint);
  130. if (remote) {
  131. if (of_device_is_available(remote))
  132. thc63->timings.dual_link = true;
  133. of_node_put(remote);
  134. }
  135. }
  136. dev_dbg(thc63->dev, "operating in %s-link mode\n",
  137. thc63->timings.dual_link ? "dual" : "single");
  138. return 0;
  139. }
  140. static int thc63_gpio_init(struct thc63_dev *thc63)
  141. {
  142. thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);
  143. if (IS_ERR(thc63->oe)) {
  144. dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",
  145. PTR_ERR(thc63->oe));
  146. return PTR_ERR(thc63->oe);
  147. }
  148. thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",
  149. GPIOD_OUT_HIGH);
  150. if (IS_ERR(thc63->pdwn)) {
  151. dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",
  152. PTR_ERR(thc63->pdwn));
  153. return PTR_ERR(thc63->pdwn);
  154. }
  155. return 0;
  156. }
  157. static int thc63_probe(struct platform_device *pdev)
  158. {
  159. struct thc63_dev *thc63;
  160. int ret;
  161. thc63 = devm_kzalloc(&pdev->dev, sizeof(*thc63), GFP_KERNEL);
  162. if (!thc63)
  163. return -ENOMEM;
  164. thc63->dev = &pdev->dev;
  165. platform_set_drvdata(pdev, thc63);
  166. thc63->vcc = devm_regulator_get_optional(thc63->dev, "vcc");
  167. if (IS_ERR(thc63->vcc)) {
  168. if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)
  169. return -EPROBE_DEFER;
  170. dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",
  171. PTR_ERR(thc63->vcc));
  172. return PTR_ERR(thc63->vcc);
  173. }
  174. ret = thc63_gpio_init(thc63);
  175. if (ret)
  176. return ret;
  177. ret = thc63_parse_dt(thc63);
  178. if (ret)
  179. return ret;
  180. thc63->bridge.driver_private = thc63;
  181. thc63->bridge.of_node = pdev->dev.of_node;
  182. thc63->bridge.funcs = &thc63_bridge_func;
  183. thc63->bridge.timings = &thc63->timings;
  184. drm_bridge_add(&thc63->bridge);
  185. return 0;
  186. }
  187. static int thc63_remove(struct platform_device *pdev)
  188. {
  189. struct thc63_dev *thc63 = platform_get_drvdata(pdev);
  190. drm_bridge_remove(&thc63->bridge);
  191. return 0;
  192. }
  193. static const struct of_device_id thc63_match[] = {
  194. { .compatible = "thine,thc63lvd1024", },
  195. { },
  196. };
  197. MODULE_DEVICE_TABLE(of, thc63_match);
  198. static struct platform_driver thc63_driver = {
  199. .probe = thc63_probe,
  200. .remove = thc63_remove,
  201. .driver = {
  202. .name = "thc63lvd1024",
  203. .of_match_table = thc63_match,
  204. },
  205. };
  206. module_platform_driver(thc63_driver);
  207. MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
  208. MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");
  209. MODULE_LICENSE("GPL v2");