display-connector.c 12 KB

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