display-connector.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  4. */
  5. #include <linux/gpio/consumer.h>
  6. #include <linux/i2c.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/media-bus-format.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_bridge.h>
  16. #include <drm/drm_edid.h>
  17. struct display_connector {
  18. struct drm_bridge bridge;
  19. struct gpio_desc *hpd_gpio;
  20. int hpd_irq;
  21. struct regulator *supply;
  22. struct gpio_desc *ddc_en;
  23. };
  24. static inline struct display_connector *
  25. to_display_connector(struct drm_bridge *bridge)
  26. {
  27. return container_of(bridge, struct display_connector, bridge);
  28. }
  29. static int display_connector_attach(struct drm_bridge *bridge,
  30. enum drm_bridge_attach_flags flags)
  31. {
  32. return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
  33. }
  34. static enum drm_connector_status
  35. display_connector_detect(struct drm_bridge *bridge)
  36. {
  37. struct display_connector *conn = to_display_connector(bridge);
  38. if (conn->hpd_gpio) {
  39. if (gpiod_get_value_cansleep(conn->hpd_gpio))
  40. return connector_status_connected;
  41. else
  42. return connector_status_disconnected;
  43. }
  44. if (conn->bridge.ddc && drm_probe_ddc(conn->bridge.ddc))
  45. return connector_status_connected;
  46. switch (conn->bridge.type) {
  47. case DRM_MODE_CONNECTOR_DVIA:
  48. case DRM_MODE_CONNECTOR_DVID:
  49. case DRM_MODE_CONNECTOR_DVII:
  50. case DRM_MODE_CONNECTOR_HDMIA:
  51. case DRM_MODE_CONNECTOR_HDMIB:
  52. /*
  53. * For DVI and HDMI connectors a DDC probe failure indicates
  54. * that no cable is connected.
  55. */
  56. return connector_status_disconnected;
  57. case DRM_MODE_CONNECTOR_Composite:
  58. case DRM_MODE_CONNECTOR_SVIDEO:
  59. case DRM_MODE_CONNECTOR_VGA:
  60. default:
  61. /*
  62. * Composite and S-Video connectors have no other detection
  63. * mean than the HPD GPIO. For VGA connectors, even if we have
  64. * an I2C bus, we can't assume that the cable is disconnected
  65. * if drm_probe_ddc fails, as some cables don't wire the DDC
  66. * pins.
  67. */
  68. return connector_status_unknown;
  69. }
  70. }
  71. static const struct drm_edid *display_connector_edid_read(struct drm_bridge *bridge,
  72. struct drm_connector *connector)
  73. {
  74. struct display_connector *conn = to_display_connector(bridge);
  75. return drm_edid_read_ddc(connector, conn->bridge.ddc);
  76. }
  77. /*
  78. * Since this bridge is tied to the connector, it acts like a passthrough,
  79. * so concerning the output bus formats, either pass the bus formats from the
  80. * previous bridge or return fallback data like done in the bridge function:
  81. * drm_atomic_bridge_chain_select_bus_fmts().
  82. * This supports negotiation if the bridge chain has all bits in place.
  83. */
  84. static u32 *display_connector_get_output_bus_fmts(struct drm_bridge *bridge,
  85. struct drm_bridge_state *bridge_state,
  86. struct drm_crtc_state *crtc_state,
  87. struct drm_connector_state *conn_state,
  88. unsigned int *num_output_fmts)
  89. {
  90. struct drm_bridge *prev_bridge = drm_bridge_get_prev_bridge(bridge);
  91. struct drm_bridge_state *prev_bridge_state;
  92. if (!prev_bridge || !prev_bridge->funcs->atomic_get_output_bus_fmts) {
  93. struct drm_connector *conn = conn_state->connector;
  94. u32 *out_bus_fmts;
  95. *num_output_fmts = 1;
  96. out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
  97. if (!out_bus_fmts)
  98. return NULL;
  99. if (conn->display_info.num_bus_formats &&
  100. conn->display_info.bus_formats)
  101. out_bus_fmts[0] = conn->display_info.bus_formats[0];
  102. else
  103. out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
  104. return out_bus_fmts;
  105. }
  106. prev_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
  107. prev_bridge);
  108. return prev_bridge->funcs->atomic_get_output_bus_fmts(prev_bridge, prev_bridge_state,
  109. crtc_state, conn_state,
  110. num_output_fmts);
  111. }
  112. /*
  113. * Since this bridge is tied to the connector, it acts like a passthrough,
  114. * so concerning the input bus formats, either pass the bus formats from the
  115. * previous bridge or MEDIA_BUS_FMT_FIXED (like select_bus_fmt_recursive())
  116. * when atomic_get_input_bus_fmts is not supported.
  117. * This supports negotiation if the bridge chain has all bits in place.
  118. */
  119. static u32 *display_connector_get_input_bus_fmts(struct drm_bridge *bridge,
  120. struct drm_bridge_state *bridge_state,
  121. struct drm_crtc_state *crtc_state,
  122. struct drm_connector_state *conn_state,
  123. u32 output_fmt,
  124. unsigned int *num_input_fmts)
  125. {
  126. struct drm_bridge *prev_bridge = drm_bridge_get_prev_bridge(bridge);
  127. struct drm_bridge_state *prev_bridge_state;
  128. if (!prev_bridge || !prev_bridge->funcs->atomic_get_input_bus_fmts) {
  129. u32 *in_bus_fmts;
  130. *num_input_fmts = 1;
  131. in_bus_fmts = kmalloc(sizeof(*in_bus_fmts), GFP_KERNEL);
  132. if (!in_bus_fmts)
  133. return NULL;
  134. in_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
  135. return in_bus_fmts;
  136. }
  137. prev_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
  138. prev_bridge);
  139. return prev_bridge->funcs->atomic_get_input_bus_fmts(prev_bridge, prev_bridge_state,
  140. crtc_state, conn_state, output_fmt,
  141. num_input_fmts);
  142. }
  143. static const struct drm_bridge_funcs display_connector_bridge_funcs = {
  144. .attach = display_connector_attach,
  145. .detect = display_connector_detect,
  146. .edid_read = display_connector_edid_read,
  147. .atomic_get_output_bus_fmts = display_connector_get_output_bus_fmts,
  148. .atomic_get_input_bus_fmts = display_connector_get_input_bus_fmts,
  149. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  150. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  151. .atomic_reset = drm_atomic_helper_bridge_reset,
  152. };
  153. static irqreturn_t display_connector_hpd_irq(int irq, void *arg)
  154. {
  155. struct display_connector *conn = arg;
  156. struct drm_bridge *bridge = &conn->bridge;
  157. drm_bridge_hpd_notify(bridge, display_connector_detect(bridge));
  158. return IRQ_HANDLED;
  159. }
  160. static int display_connector_get_supply(struct platform_device *pdev,
  161. struct display_connector *conn,
  162. const char *name)
  163. {
  164. conn->supply = devm_regulator_get_optional(&pdev->dev, name);
  165. if (conn->supply == ERR_PTR(-ENODEV))
  166. conn->supply = NULL;
  167. return PTR_ERR_OR_ZERO(conn->supply);
  168. }
  169. static int display_connector_probe(struct platform_device *pdev)
  170. {
  171. struct display_connector *conn;
  172. unsigned int type;
  173. const char *label = NULL;
  174. int ret;
  175. conn = devm_kzalloc(&pdev->dev, sizeof(*conn), GFP_KERNEL);
  176. if (!conn)
  177. return -ENOMEM;
  178. platform_set_drvdata(pdev, conn);
  179. type = (uintptr_t)of_device_get_match_data(&pdev->dev);
  180. /* Get the exact connector type. */
  181. switch (type) {
  182. case DRM_MODE_CONNECTOR_DVII: {
  183. bool analog, digital;
  184. analog = of_property_read_bool(pdev->dev.of_node, "analog");
  185. digital = of_property_read_bool(pdev->dev.of_node, "digital");
  186. if (analog && !digital) {
  187. conn->bridge.type = DRM_MODE_CONNECTOR_DVIA;
  188. } else if (!analog && digital) {
  189. conn->bridge.type = DRM_MODE_CONNECTOR_DVID;
  190. } else if (analog && digital) {
  191. conn->bridge.type = DRM_MODE_CONNECTOR_DVII;
  192. } else {
  193. dev_err(&pdev->dev, "DVI connector with no type\n");
  194. return -EINVAL;
  195. }
  196. break;
  197. }
  198. case DRM_MODE_CONNECTOR_HDMIA: {
  199. const char *hdmi_type;
  200. ret = of_property_read_string(pdev->dev.of_node, "type",
  201. &hdmi_type);
  202. if (ret < 0) {
  203. dev_err(&pdev->dev, "HDMI connector with no type\n");
  204. return -EINVAL;
  205. }
  206. if (!strcmp(hdmi_type, "a") || !strcmp(hdmi_type, "c") ||
  207. !strcmp(hdmi_type, "d") || !strcmp(hdmi_type, "e")) {
  208. conn->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
  209. } else if (!strcmp(hdmi_type, "b")) {
  210. conn->bridge.type = DRM_MODE_CONNECTOR_HDMIB;
  211. } else {
  212. dev_err(&pdev->dev,
  213. "Unsupported HDMI connector type '%s'\n",
  214. hdmi_type);
  215. return -EINVAL;
  216. }
  217. break;
  218. }
  219. default:
  220. conn->bridge.type = type;
  221. break;
  222. }
  223. /* All the supported connector types support interlaced modes. */
  224. conn->bridge.interlace_allowed = true;
  225. /* Get the optional connector label. */
  226. of_property_read_string(pdev->dev.of_node, "label", &label);
  227. /*
  228. * Get the HPD GPIO for DVI, HDMI and DP connectors. If the GPIO can provide
  229. * edge interrupts, register an interrupt handler.
  230. */
  231. if (type == DRM_MODE_CONNECTOR_DVII ||
  232. type == DRM_MODE_CONNECTOR_HDMIA ||
  233. type == DRM_MODE_CONNECTOR_DisplayPort) {
  234. conn->hpd_gpio = devm_gpiod_get_optional(&pdev->dev, "hpd",
  235. GPIOD_IN);
  236. if (IS_ERR(conn->hpd_gpio))
  237. return dev_err_probe(&pdev->dev, PTR_ERR(conn->hpd_gpio),
  238. "Unable to retrieve HPD GPIO\n");
  239. conn->hpd_irq = gpiod_to_irq(conn->hpd_gpio);
  240. } else {
  241. conn->hpd_irq = -EINVAL;
  242. }
  243. if (conn->hpd_irq >= 0) {
  244. ret = devm_request_threaded_irq(&pdev->dev, conn->hpd_irq,
  245. NULL, display_connector_hpd_irq,
  246. IRQF_TRIGGER_RISING |
  247. IRQF_TRIGGER_FALLING |
  248. IRQF_ONESHOT,
  249. "HPD", conn);
  250. if (ret) {
  251. dev_info(&pdev->dev,
  252. "Failed to request HPD edge interrupt, falling back to polling\n");
  253. conn->hpd_irq = -EINVAL;
  254. }
  255. }
  256. /* Retrieve the DDC I2C adapter for DVI, HDMI and VGA connectors. */
  257. if (type == DRM_MODE_CONNECTOR_DVII ||
  258. type == DRM_MODE_CONNECTOR_HDMIA ||
  259. type == DRM_MODE_CONNECTOR_VGA) {
  260. struct device_node *phandle;
  261. phandle = of_parse_phandle(pdev->dev.of_node, "ddc-i2c-bus", 0);
  262. if (phandle) {
  263. conn->bridge.ddc = of_get_i2c_adapter_by_node(phandle);
  264. of_node_put(phandle);
  265. if (!conn->bridge.ddc)
  266. return -EPROBE_DEFER;
  267. } else {
  268. dev_dbg(&pdev->dev,
  269. "No I2C bus specified, disabling EDID readout\n");
  270. }
  271. }
  272. /* Get the DP PWR for DP connector. */
  273. if (type == DRM_MODE_CONNECTOR_DisplayPort) {
  274. int ret;
  275. ret = display_connector_get_supply(pdev, conn, "dp-pwr");
  276. if (ret < 0)
  277. return dev_err_probe(&pdev->dev, ret, "failed to get DP PWR regulator\n");
  278. }
  279. /* enable DDC */
  280. if (type == DRM_MODE_CONNECTOR_HDMIA) {
  281. int ret;
  282. conn->ddc_en = devm_gpiod_get_optional(&pdev->dev, "ddc-en",
  283. GPIOD_OUT_HIGH);
  284. if (IS_ERR(conn->ddc_en)) {
  285. dev_err(&pdev->dev, "Couldn't get ddc-en gpio\n");
  286. return PTR_ERR(conn->ddc_en);
  287. }
  288. ret = display_connector_get_supply(pdev, conn, "hdmi-pwr");
  289. if (ret < 0)
  290. return dev_err_probe(&pdev->dev, ret, "failed to get HDMI +5V Power regulator\n");
  291. }
  292. if (conn->supply) {
  293. ret = regulator_enable(conn->supply);
  294. if (ret) {
  295. dev_err(&pdev->dev, "failed to enable PWR regulator: %d\n", ret);
  296. return ret;
  297. }
  298. }
  299. conn->bridge.funcs = &display_connector_bridge_funcs;
  300. conn->bridge.of_node = pdev->dev.of_node;
  301. if (conn->bridge.ddc)
  302. conn->bridge.ops |= DRM_BRIDGE_OP_EDID
  303. | DRM_BRIDGE_OP_DETECT;
  304. if (conn->hpd_gpio)
  305. conn->bridge.ops |= DRM_BRIDGE_OP_DETECT;
  306. if (conn->hpd_irq >= 0)
  307. conn->bridge.ops |= DRM_BRIDGE_OP_HPD;
  308. dev_dbg(&pdev->dev,
  309. "Found %s display connector '%s' %s DDC bus and %s HPD GPIO (ops 0x%x)\n",
  310. drm_get_connector_type_name(conn->bridge.type),
  311. label ? label : "<unlabelled>",
  312. conn->bridge.ddc ? "with" : "without",
  313. conn->hpd_gpio ? "with" : "without",
  314. conn->bridge.ops);
  315. drm_bridge_add(&conn->bridge);
  316. return 0;
  317. }
  318. static void display_connector_remove(struct platform_device *pdev)
  319. {
  320. struct display_connector *conn = platform_get_drvdata(pdev);
  321. if (conn->ddc_en)
  322. gpiod_set_value(conn->ddc_en, 0);
  323. if (conn->supply)
  324. regulator_disable(conn->supply);
  325. drm_bridge_remove(&conn->bridge);
  326. if (!IS_ERR(conn->bridge.ddc))
  327. i2c_put_adapter(conn->bridge.ddc);
  328. }
  329. static const struct of_device_id display_connector_match[] = {
  330. {
  331. .compatible = "composite-video-connector",
  332. .data = (void *)DRM_MODE_CONNECTOR_Composite,
  333. }, {
  334. .compatible = "dvi-connector",
  335. .data = (void *)DRM_MODE_CONNECTOR_DVII,
  336. }, {
  337. .compatible = "hdmi-connector",
  338. .data = (void *)DRM_MODE_CONNECTOR_HDMIA,
  339. }, {
  340. .compatible = "svideo-connector",
  341. .data = (void *)DRM_MODE_CONNECTOR_SVIDEO,
  342. }, {
  343. .compatible = "vga-connector",
  344. .data = (void *)DRM_MODE_CONNECTOR_VGA,
  345. }, {
  346. .compatible = "dp-connector",
  347. .data = (void *)DRM_MODE_CONNECTOR_DisplayPort,
  348. },
  349. {},
  350. };
  351. MODULE_DEVICE_TABLE(of, display_connector_match);
  352. static struct platform_driver display_connector_driver = {
  353. .probe = display_connector_probe,
  354. .remove_new = display_connector_remove,
  355. .driver = {
  356. .name = "display-connector",
  357. .of_match_table = display_connector_match,
  358. },
  359. };
  360. module_platform_driver(display_connector_driver);
  361. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  362. MODULE_DESCRIPTION("Display connector driver");
  363. MODULE_LICENSE("GPL");