connector-hdmi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * HDMI 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/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <drm/drm_edid.h>
  17. #include <video/omapdss.h>
  18. #include <video/omap-panel-data.h>
  19. static const struct omap_video_timings hdmic_default_timings = {
  20. .x_res = 640,
  21. .y_res = 480,
  22. .pixelclock = 25175000,
  23. .hsw = 96,
  24. .hfp = 16,
  25. .hbp = 48,
  26. .vsw = 2,
  27. .vfp = 11,
  28. .vbp = 31,
  29. .vsync_level = OMAPDSS_SIG_ACTIVE_LOW,
  30. .hsync_level = OMAPDSS_SIG_ACTIVE_LOW,
  31. .interlace = false,
  32. };
  33. struct panel_drv_data {
  34. struct omap_dss_device dssdev;
  35. struct omap_dss_device *in;
  36. struct device *dev;
  37. struct omap_video_timings timings;
  38. int hpd_gpio;
  39. };
  40. #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
  41. static int hdmic_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. dev_dbg(ddata->dev, "connect\n");
  47. if (omapdss_device_is_connected(dssdev))
  48. return 0;
  49. r = in->ops.hdmi->connect(in, dssdev);
  50. if (r)
  51. return r;
  52. return 0;
  53. }
  54. static void hdmic_disconnect(struct omap_dss_device *dssdev)
  55. {
  56. struct panel_drv_data *ddata = to_panel_data(dssdev);
  57. struct omap_dss_device *in = ddata->in;
  58. dev_dbg(ddata->dev, "disconnect\n");
  59. if (!omapdss_device_is_connected(dssdev))
  60. return;
  61. in->ops.hdmi->disconnect(in, dssdev);
  62. }
  63. static int hdmic_enable(struct omap_dss_device *dssdev)
  64. {
  65. struct panel_drv_data *ddata = to_panel_data(dssdev);
  66. struct omap_dss_device *in = ddata->in;
  67. int r;
  68. dev_dbg(ddata->dev, "enable\n");
  69. if (!omapdss_device_is_connected(dssdev))
  70. return -ENODEV;
  71. if (omapdss_device_is_enabled(dssdev))
  72. return 0;
  73. in->ops.hdmi->set_timings(in, &ddata->timings);
  74. r = in->ops.hdmi->enable(in);
  75. if (r)
  76. return r;
  77. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  78. return r;
  79. }
  80. static void hdmic_disable(struct omap_dss_device *dssdev)
  81. {
  82. struct panel_drv_data *ddata = to_panel_data(dssdev);
  83. struct omap_dss_device *in = ddata->in;
  84. dev_dbg(ddata->dev, "disable\n");
  85. if (!omapdss_device_is_enabled(dssdev))
  86. return;
  87. in->ops.hdmi->disable(in);
  88. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  89. }
  90. static void hdmic_set_timings(struct omap_dss_device *dssdev,
  91. struct omap_video_timings *timings)
  92. {
  93. struct panel_drv_data *ddata = to_panel_data(dssdev);
  94. struct omap_dss_device *in = ddata->in;
  95. ddata->timings = *timings;
  96. dssdev->panel.timings = *timings;
  97. in->ops.hdmi->set_timings(in, timings);
  98. }
  99. static void hdmic_get_timings(struct omap_dss_device *dssdev,
  100. struct omap_video_timings *timings)
  101. {
  102. struct panel_drv_data *ddata = to_panel_data(dssdev);
  103. *timings = ddata->timings;
  104. }
  105. static int hdmic_check_timings(struct omap_dss_device *dssdev,
  106. struct omap_video_timings *timings)
  107. {
  108. struct panel_drv_data *ddata = to_panel_data(dssdev);
  109. struct omap_dss_device *in = ddata->in;
  110. return in->ops.hdmi->check_timings(in, timings);
  111. }
  112. static int hdmic_read_edid(struct omap_dss_device *dssdev,
  113. u8 *edid, int len)
  114. {
  115. struct panel_drv_data *ddata = to_panel_data(dssdev);
  116. struct omap_dss_device *in = ddata->in;
  117. return in->ops.hdmi->read_edid(in, edid, len);
  118. }
  119. static bool hdmic_detect(struct omap_dss_device *dssdev)
  120. {
  121. struct panel_drv_data *ddata = to_panel_data(dssdev);
  122. struct omap_dss_device *in = ddata->in;
  123. if (gpio_is_valid(ddata->hpd_gpio))
  124. return gpio_get_value_cansleep(ddata->hpd_gpio);
  125. else
  126. return in->ops.hdmi->detect(in);
  127. }
  128. static int hdmic_set_hdmi_mode(struct omap_dss_device *dssdev, bool hdmi_mode)
  129. {
  130. struct panel_drv_data *ddata = to_panel_data(dssdev);
  131. struct omap_dss_device *in = ddata->in;
  132. return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
  133. }
  134. static int hdmic_set_infoframe(struct omap_dss_device *dssdev,
  135. const struct hdmi_avi_infoframe *avi)
  136. {
  137. struct panel_drv_data *ddata = to_panel_data(dssdev);
  138. struct omap_dss_device *in = ddata->in;
  139. return in->ops.hdmi->set_infoframe(in, avi);
  140. }
  141. static struct omap_dss_driver hdmic_driver = {
  142. .connect = hdmic_connect,
  143. .disconnect = hdmic_disconnect,
  144. .enable = hdmic_enable,
  145. .disable = hdmic_disable,
  146. .set_timings = hdmic_set_timings,
  147. .get_timings = hdmic_get_timings,
  148. .check_timings = hdmic_check_timings,
  149. .get_resolution = omapdss_default_get_resolution,
  150. .read_edid = hdmic_read_edid,
  151. .detect = hdmic_detect,
  152. .set_hdmi_mode = hdmic_set_hdmi_mode,
  153. .set_hdmi_infoframe = hdmic_set_infoframe,
  154. };
  155. static int hdmic_probe_pdata(struct platform_device *pdev)
  156. {
  157. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  158. struct connector_hdmi_platform_data *pdata;
  159. struct omap_dss_device *in, *dssdev;
  160. pdata = dev_get_platdata(&pdev->dev);
  161. ddata->hpd_gpio = -ENODEV;
  162. in = omap_dss_find_output(pdata->source);
  163. if (in == NULL) {
  164. dev_err(&pdev->dev, "Failed to find video source\n");
  165. return -EPROBE_DEFER;
  166. }
  167. ddata->in = in;
  168. dssdev = &ddata->dssdev;
  169. dssdev->name = pdata->name;
  170. return 0;
  171. }
  172. static int hdmic_probe_of(struct platform_device *pdev)
  173. {
  174. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  175. struct device_node *node = pdev->dev.of_node;
  176. struct omap_dss_device *in;
  177. int gpio;
  178. /* HPD GPIO */
  179. gpio = of_get_named_gpio(node, "hpd-gpios", 0);
  180. if (gpio_is_valid(gpio))
  181. ddata->hpd_gpio = gpio;
  182. else
  183. ddata->hpd_gpio = -ENODEV;
  184. in = omapdss_of_find_source_for_first_ep(node);
  185. if (IS_ERR(in)) {
  186. dev_err(&pdev->dev, "failed to find video source\n");
  187. return PTR_ERR(in);
  188. }
  189. ddata->in = in;
  190. return 0;
  191. }
  192. static int hdmic_probe(struct platform_device *pdev)
  193. {
  194. struct panel_drv_data *ddata;
  195. struct omap_dss_device *dssdev;
  196. int r;
  197. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  198. if (!ddata)
  199. return -ENOMEM;
  200. platform_set_drvdata(pdev, ddata);
  201. ddata->dev = &pdev->dev;
  202. if (dev_get_platdata(&pdev->dev)) {
  203. r = hdmic_probe_pdata(pdev);
  204. if (r)
  205. return r;
  206. } else if (pdev->dev.of_node) {
  207. r = hdmic_probe_of(pdev);
  208. if (r)
  209. return r;
  210. } else {
  211. return -ENODEV;
  212. }
  213. if (gpio_is_valid(ddata->hpd_gpio)) {
  214. r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
  215. GPIOF_DIR_IN, "hdmi_hpd");
  216. if (r)
  217. goto err_reg;
  218. }
  219. ddata->timings = hdmic_default_timings;
  220. dssdev = &ddata->dssdev;
  221. dssdev->driver = &hdmic_driver;
  222. dssdev->dev = &pdev->dev;
  223. dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
  224. dssdev->owner = THIS_MODULE;
  225. dssdev->panel.timings = hdmic_default_timings;
  226. r = omapdss_register_display(dssdev);
  227. if (r) {
  228. dev_err(&pdev->dev, "Failed to register panel\n");
  229. goto err_reg;
  230. }
  231. return 0;
  232. err_reg:
  233. omap_dss_put_device(ddata->in);
  234. return r;
  235. }
  236. static int __exit hdmic_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. hdmic_disable(dssdev);
  243. hdmic_disconnect(dssdev);
  244. omap_dss_put_device(in);
  245. return 0;
  246. }
  247. static const struct of_device_id hdmic_of_match[] = {
  248. { .compatible = "omapdss,hdmi-connector", },
  249. {},
  250. };
  251. MODULE_DEVICE_TABLE(of, hdmic_of_match);
  252. static struct platform_driver hdmi_connector_driver = {
  253. .probe = hdmic_probe,
  254. .remove = __exit_p(hdmic_remove),
  255. .driver = {
  256. .name = "connector-hdmi",
  257. .of_match_table = hdmic_of_match,
  258. .suppress_bind_attrs = true,
  259. },
  260. };
  261. module_platform_driver(hdmi_connector_driver);
  262. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  263. MODULE_DESCRIPTION("HDMI Connector driver");
  264. MODULE_LICENSE("GPL");