connector-dvi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Generic DVI Connector driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
  5. * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <drm/drm_edid.h>
  17. #include "../dss/omapdss.h"
  18. struct panel_drv_data {
  19. struct omap_dss_device dssdev;
  20. struct i2c_adapter *i2c_adapter;
  21. struct gpio_desc *hpd_gpio;
  22. void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
  23. void *hpd_cb_data;
  24. bool hpd_enabled;
  25. /* mutex for hpd fields above */
  26. struct mutex hpd_lock;
  27. };
  28. #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
  29. static int dvic_connect(struct omap_dss_device *src,
  30. struct omap_dss_device *dst)
  31. {
  32. return 0;
  33. }
  34. static void dvic_disconnect(struct omap_dss_device *src,
  35. struct omap_dss_device *dst)
  36. {
  37. }
  38. static int dvic_enable(struct omap_dss_device *dssdev)
  39. {
  40. struct omap_dss_device *src = dssdev->src;
  41. int r;
  42. if (!omapdss_device_is_connected(dssdev))
  43. return -ENODEV;
  44. if (omapdss_device_is_enabled(dssdev))
  45. return 0;
  46. r = src->ops->enable(src);
  47. if (r)
  48. return r;
  49. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  50. return 0;
  51. }
  52. static void dvic_disable(struct omap_dss_device *dssdev)
  53. {
  54. struct omap_dss_device *src = dssdev->src;
  55. if (!omapdss_device_is_enabled(dssdev))
  56. return;
  57. src->ops->disable(src);
  58. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  59. }
  60. static int dvic_ddc_read(struct i2c_adapter *adapter,
  61. unsigned char *buf, u16 count, u8 offset)
  62. {
  63. int r, retries;
  64. for (retries = 3; retries > 0; retries--) {
  65. struct i2c_msg msgs[] = {
  66. {
  67. .addr = DDC_ADDR,
  68. .flags = 0,
  69. .len = 1,
  70. .buf = &offset,
  71. }, {
  72. .addr = DDC_ADDR,
  73. .flags = I2C_M_RD,
  74. .len = count,
  75. .buf = buf,
  76. }
  77. };
  78. r = i2c_transfer(adapter, msgs, 2);
  79. if (r == 2)
  80. return 0;
  81. if (r != -EAGAIN)
  82. break;
  83. }
  84. return r < 0 ? r : -EIO;
  85. }
  86. static int dvic_read_edid(struct omap_dss_device *dssdev,
  87. u8 *edid, int len)
  88. {
  89. struct panel_drv_data *ddata = to_panel_data(dssdev);
  90. int r, l, bytes_read;
  91. l = min(EDID_LENGTH, len);
  92. r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
  93. if (r)
  94. return r;
  95. bytes_read = l;
  96. /* if there are extensions, read second block */
  97. if (len > EDID_LENGTH && edid[0x7e] > 0) {
  98. l = min(EDID_LENGTH, len - EDID_LENGTH);
  99. r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
  100. l, EDID_LENGTH);
  101. if (r)
  102. return r;
  103. bytes_read += l;
  104. }
  105. return bytes_read;
  106. }
  107. static bool dvic_detect(struct omap_dss_device *dssdev)
  108. {
  109. struct panel_drv_data *ddata = to_panel_data(dssdev);
  110. unsigned char out;
  111. int r;
  112. if (ddata->hpd_gpio)
  113. return gpiod_get_value_cansleep(ddata->hpd_gpio);
  114. if (!ddata->i2c_adapter)
  115. return true;
  116. r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
  117. return r == 0;
  118. }
  119. static void dvic_register_hpd_cb(struct omap_dss_device *dssdev,
  120. void (*cb)(void *cb_data,
  121. enum drm_connector_status status),
  122. void *cb_data)
  123. {
  124. struct panel_drv_data *ddata = to_panel_data(dssdev);
  125. mutex_lock(&ddata->hpd_lock);
  126. ddata->hpd_cb = cb;
  127. ddata->hpd_cb_data = cb_data;
  128. mutex_unlock(&ddata->hpd_lock);
  129. }
  130. static void dvic_unregister_hpd_cb(struct omap_dss_device *dssdev)
  131. {
  132. struct panel_drv_data *ddata = to_panel_data(dssdev);
  133. mutex_lock(&ddata->hpd_lock);
  134. ddata->hpd_cb = NULL;
  135. ddata->hpd_cb_data = NULL;
  136. mutex_unlock(&ddata->hpd_lock);
  137. }
  138. static const struct omap_dss_device_ops dvic_ops = {
  139. .connect = dvic_connect,
  140. .disconnect = dvic_disconnect,
  141. .enable = dvic_enable,
  142. .disable = dvic_disable,
  143. .read_edid = dvic_read_edid,
  144. .detect = dvic_detect,
  145. .register_hpd_cb = dvic_register_hpd_cb,
  146. .unregister_hpd_cb = dvic_unregister_hpd_cb,
  147. };
  148. static irqreturn_t dvic_hpd_isr(int irq, void *data)
  149. {
  150. struct panel_drv_data *ddata = data;
  151. mutex_lock(&ddata->hpd_lock);
  152. if (ddata->hpd_enabled && ddata->hpd_cb) {
  153. enum drm_connector_status status;
  154. if (dvic_detect(&ddata->dssdev))
  155. status = connector_status_connected;
  156. else
  157. status = connector_status_disconnected;
  158. ddata->hpd_cb(ddata->hpd_cb_data, status);
  159. }
  160. mutex_unlock(&ddata->hpd_lock);
  161. return IRQ_HANDLED;
  162. }
  163. static int dvic_probe_of(struct platform_device *pdev)
  164. {
  165. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  166. struct device_node *node = pdev->dev.of_node;
  167. struct device_node *adapter_node;
  168. struct i2c_adapter *adapter;
  169. struct gpio_desc *gpio;
  170. int r;
  171. gpio = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
  172. if (IS_ERR(gpio)) {
  173. dev_err(&pdev->dev, "failed to parse HPD gpio\n");
  174. return PTR_ERR(gpio);
  175. }
  176. ddata->hpd_gpio = gpio;
  177. mutex_init(&ddata->hpd_lock);
  178. if (ddata->hpd_gpio) {
  179. r = devm_request_threaded_irq(&pdev->dev,
  180. gpiod_to_irq(ddata->hpd_gpio), NULL, dvic_hpd_isr,
  181. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  182. "DVI HPD", ddata);
  183. if (r)
  184. return r;
  185. }
  186. adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
  187. if (adapter_node) {
  188. adapter = of_get_i2c_adapter_by_node(adapter_node);
  189. of_node_put(adapter_node);
  190. if (adapter == NULL) {
  191. dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
  192. return -EPROBE_DEFER;
  193. }
  194. ddata->i2c_adapter = adapter;
  195. }
  196. return 0;
  197. }
  198. static int dvic_probe(struct platform_device *pdev)
  199. {
  200. struct panel_drv_data *ddata;
  201. struct omap_dss_device *dssdev;
  202. int r;
  203. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  204. if (!ddata)
  205. return -ENOMEM;
  206. platform_set_drvdata(pdev, ddata);
  207. r = dvic_probe_of(pdev);
  208. if (r)
  209. return r;
  210. dssdev = &ddata->dssdev;
  211. dssdev->ops = &dvic_ops;
  212. dssdev->dev = &pdev->dev;
  213. dssdev->type = OMAP_DISPLAY_TYPE_DVI;
  214. dssdev->owner = THIS_MODULE;
  215. dssdev->of_ports = BIT(0);
  216. if (ddata->hpd_gpio)
  217. dssdev->ops_flags |= OMAP_DSS_DEVICE_OP_DETECT
  218. | OMAP_DSS_DEVICE_OP_HPD;
  219. if (ddata->i2c_adapter)
  220. dssdev->ops_flags |= OMAP_DSS_DEVICE_OP_DETECT
  221. | OMAP_DSS_DEVICE_OP_EDID;
  222. omapdss_display_init(dssdev);
  223. omapdss_device_register(dssdev);
  224. return 0;
  225. }
  226. static int __exit dvic_remove(struct platform_device *pdev)
  227. {
  228. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  229. struct omap_dss_device *dssdev = &ddata->dssdev;
  230. omapdss_device_unregister(&ddata->dssdev);
  231. dvic_disable(dssdev);
  232. i2c_put_adapter(ddata->i2c_adapter);
  233. mutex_destroy(&ddata->hpd_lock);
  234. return 0;
  235. }
  236. static const struct of_device_id dvic_of_match[] = {
  237. { .compatible = "omapdss,dvi-connector", },
  238. {},
  239. };
  240. MODULE_DEVICE_TABLE(of, dvic_of_match);
  241. static struct platform_driver dvi_connector_driver = {
  242. .probe = dvic_probe,
  243. .remove = __exit_p(dvic_remove),
  244. .driver = {
  245. .name = "connector-dvi",
  246. .of_match_table = dvic_of_match,
  247. .suppress_bind_attrs = true,
  248. },
  249. };
  250. module_platform_driver(dvi_connector_driver);
  251. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  252. MODULE_DESCRIPTION("Generic DVI Connector driver");
  253. MODULE_LICENSE("GPL");