connector-dvi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Generic DVI Connector driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments
  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/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <drm/drm_edid.h>
  16. #include <video/omapdss.h>
  17. #include <video/omap-panel-data.h>
  18. static const struct omap_video_timings dvic_default_timings = {
  19. .x_res = 640,
  20. .y_res = 480,
  21. .pixelclock = 23500000,
  22. .hfp = 48,
  23. .hsw = 32,
  24. .hbp = 80,
  25. .vfp = 3,
  26. .vsw = 4,
  27. .vbp = 7,
  28. .vsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
  29. .hsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
  30. .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
  31. .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
  32. .sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
  33. };
  34. struct panel_drv_data {
  35. struct omap_dss_device dssdev;
  36. struct omap_dss_device *in;
  37. struct omap_video_timings timings;
  38. struct i2c_adapter *i2c_adapter;
  39. };
  40. #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
  41. static int dvic_connect(struct omap_dss_device *dssdev)
  42. {
  43. struct panel_drv_data *ddata = to_panel_data(dssdev);
  44. struct omap_dss_device *in = ddata->in;
  45. int r;
  46. if (omapdss_device_is_connected(dssdev))
  47. return 0;
  48. r = in->ops.dvi->connect(in, dssdev);
  49. if (r)
  50. return r;
  51. return 0;
  52. }
  53. static void dvic_disconnect(struct omap_dss_device *dssdev)
  54. {
  55. struct panel_drv_data *ddata = to_panel_data(dssdev);
  56. struct omap_dss_device *in = ddata->in;
  57. if (!omapdss_device_is_connected(dssdev))
  58. return;
  59. in->ops.dvi->disconnect(in, dssdev);
  60. }
  61. static int dvic_enable(struct omap_dss_device *dssdev)
  62. {
  63. struct panel_drv_data *ddata = to_panel_data(dssdev);
  64. struct omap_dss_device *in = ddata->in;
  65. int r;
  66. if (!omapdss_device_is_connected(dssdev))
  67. return -ENODEV;
  68. if (omapdss_device_is_enabled(dssdev))
  69. return 0;
  70. in->ops.dvi->set_timings(in, &ddata->timings);
  71. r = in->ops.dvi->enable(in);
  72. if (r)
  73. return r;
  74. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  75. return 0;
  76. }
  77. static void dvic_disable(struct omap_dss_device *dssdev)
  78. {
  79. struct panel_drv_data *ddata = to_panel_data(dssdev);
  80. struct omap_dss_device *in = ddata->in;
  81. if (!omapdss_device_is_enabled(dssdev))
  82. return;
  83. in->ops.dvi->disable(in);
  84. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  85. }
  86. static void dvic_set_timings(struct omap_dss_device *dssdev,
  87. struct omap_video_timings *timings)
  88. {
  89. struct panel_drv_data *ddata = to_panel_data(dssdev);
  90. struct omap_dss_device *in = ddata->in;
  91. ddata->timings = *timings;
  92. dssdev->panel.timings = *timings;
  93. in->ops.dvi->set_timings(in, timings);
  94. }
  95. static void dvic_get_timings(struct omap_dss_device *dssdev,
  96. struct omap_video_timings *timings)
  97. {
  98. struct panel_drv_data *ddata = to_panel_data(dssdev);
  99. *timings = ddata->timings;
  100. }
  101. static int dvic_check_timings(struct omap_dss_device *dssdev,
  102. struct omap_video_timings *timings)
  103. {
  104. struct panel_drv_data *ddata = to_panel_data(dssdev);
  105. struct omap_dss_device *in = ddata->in;
  106. return in->ops.dvi->check_timings(in, timings);
  107. }
  108. static int dvic_ddc_read(struct i2c_adapter *adapter,
  109. unsigned char *buf, u16 count, u8 offset)
  110. {
  111. int r, retries;
  112. for (retries = 3; retries > 0; retries--) {
  113. struct i2c_msg msgs[] = {
  114. {
  115. .addr = DDC_ADDR,
  116. .flags = 0,
  117. .len = 1,
  118. .buf = &offset,
  119. }, {
  120. .addr = DDC_ADDR,
  121. .flags = I2C_M_RD,
  122. .len = count,
  123. .buf = buf,
  124. }
  125. };
  126. r = i2c_transfer(adapter, msgs, 2);
  127. if (r == 2)
  128. return 0;
  129. if (r != -EAGAIN)
  130. break;
  131. }
  132. return r < 0 ? r : -EIO;
  133. }
  134. static int dvic_read_edid(struct omap_dss_device *dssdev,
  135. u8 *edid, int len)
  136. {
  137. struct panel_drv_data *ddata = to_panel_data(dssdev);
  138. int r, l, bytes_read;
  139. if (!ddata->i2c_adapter)
  140. return -ENODEV;
  141. l = min(EDID_LENGTH, len);
  142. r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
  143. if (r)
  144. return r;
  145. bytes_read = l;
  146. /* if there are extensions, read second block */
  147. if (len > EDID_LENGTH && edid[0x7e] > 0) {
  148. l = min(EDID_LENGTH, len - EDID_LENGTH);
  149. r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
  150. l, EDID_LENGTH);
  151. if (r)
  152. return r;
  153. bytes_read += l;
  154. }
  155. return bytes_read;
  156. }
  157. static bool dvic_detect(struct omap_dss_device *dssdev)
  158. {
  159. struct panel_drv_data *ddata = to_panel_data(dssdev);
  160. unsigned char out;
  161. int r;
  162. if (!ddata->i2c_adapter)
  163. return true;
  164. r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
  165. return r == 0;
  166. }
  167. static struct omap_dss_driver dvic_driver = {
  168. .connect = dvic_connect,
  169. .disconnect = dvic_disconnect,
  170. .enable = dvic_enable,
  171. .disable = dvic_disable,
  172. .set_timings = dvic_set_timings,
  173. .get_timings = dvic_get_timings,
  174. .check_timings = dvic_check_timings,
  175. .get_resolution = omapdss_default_get_resolution,
  176. .read_edid = dvic_read_edid,
  177. .detect = dvic_detect,
  178. };
  179. static int dvic_probe_of(struct platform_device *pdev)
  180. {
  181. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  182. struct device_node *node = pdev->dev.of_node;
  183. struct omap_dss_device *in;
  184. struct device_node *adapter_node;
  185. struct i2c_adapter *adapter;
  186. in = omapdss_of_find_source_for_first_ep(node);
  187. if (IS_ERR(in)) {
  188. dev_err(&pdev->dev, "failed to find video source\n");
  189. return PTR_ERR(in);
  190. }
  191. ddata->in = in;
  192. adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
  193. if (adapter_node) {
  194. adapter = of_get_i2c_adapter_by_node(adapter_node);
  195. if (adapter == NULL) {
  196. dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
  197. omap_dss_put_device(ddata->in);
  198. return -EPROBE_DEFER;
  199. }
  200. ddata->i2c_adapter = adapter;
  201. }
  202. return 0;
  203. }
  204. static int dvic_probe(struct platform_device *pdev)
  205. {
  206. struct panel_drv_data *ddata;
  207. struct omap_dss_device *dssdev;
  208. int r;
  209. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  210. if (!ddata)
  211. return -ENOMEM;
  212. platform_set_drvdata(pdev, ddata);
  213. if (!pdev->dev.of_node)
  214. return -ENODEV;
  215. r = dvic_probe_of(pdev);
  216. if (r)
  217. return r;
  218. ddata->timings = dvic_default_timings;
  219. dssdev = &ddata->dssdev;
  220. dssdev->driver = &dvic_driver;
  221. dssdev->dev = &pdev->dev;
  222. dssdev->type = OMAP_DISPLAY_TYPE_DVI;
  223. dssdev->owner = THIS_MODULE;
  224. dssdev->panel.timings = dvic_default_timings;
  225. r = omapdss_register_display(dssdev);
  226. if (r) {
  227. dev_err(&pdev->dev, "Failed to register panel\n");
  228. goto err_reg;
  229. }
  230. return 0;
  231. err_reg:
  232. omap_dss_put_device(ddata->in);
  233. i2c_put_adapter(ddata->i2c_adapter);
  234. return r;
  235. }
  236. static int __exit dvic_remove(struct platform_device *pdev)
  237. {
  238. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  239. struct omap_dss_device *dssdev = &ddata->dssdev;
  240. struct omap_dss_device *in = ddata->in;
  241. omapdss_unregister_display(&ddata->dssdev);
  242. dvic_disable(dssdev);
  243. dvic_disconnect(dssdev);
  244. omap_dss_put_device(in);
  245. i2c_put_adapter(ddata->i2c_adapter);
  246. return 0;
  247. }
  248. static const struct of_device_id dvic_of_match[] = {
  249. { .compatible = "omapdss,dvi-connector", },
  250. {},
  251. };
  252. MODULE_DEVICE_TABLE(of, dvic_of_match);
  253. static struct platform_driver dvi_connector_driver = {
  254. .probe = dvic_probe,
  255. .remove = __exit_p(dvic_remove),
  256. .driver = {
  257. .name = "connector-dvi",
  258. .of_match_table = dvic_of_match,
  259. .suppress_bind_attrs = true,
  260. },
  261. };
  262. module_platform_driver(dvi_connector_driver);
  263. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  264. MODULE_DESCRIPTION("Generic DVI Connector driver");
  265. MODULE_LICENSE("GPL");