connector-hdmi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_audio_enable(struct omap_dss_device *dssdev)
  129. {
  130. struct panel_drv_data *ddata = to_panel_data(dssdev);
  131. struct omap_dss_device *in = ddata->in;
  132. int r;
  133. /* enable audio only if the display is active */
  134. if (!omapdss_device_is_enabled(dssdev))
  135. return -EPERM;
  136. r = in->ops.hdmi->audio_enable(in);
  137. if (r)
  138. return r;
  139. dssdev->audio_state = OMAP_DSS_AUDIO_ENABLED;
  140. return 0;
  141. }
  142. static void hdmic_audio_disable(struct omap_dss_device *dssdev)
  143. {
  144. struct panel_drv_data *ddata = to_panel_data(dssdev);
  145. struct omap_dss_device *in = ddata->in;
  146. in->ops.hdmi->audio_disable(in);
  147. dssdev->audio_state = OMAP_DSS_AUDIO_DISABLED;
  148. }
  149. static int hdmic_audio_start(struct omap_dss_device *dssdev)
  150. {
  151. struct panel_drv_data *ddata = to_panel_data(dssdev);
  152. struct omap_dss_device *in = ddata->in;
  153. int r;
  154. /*
  155. * No need to check the panel state. It was checked when trasitioning
  156. * to AUDIO_ENABLED.
  157. */
  158. if (dssdev->audio_state != OMAP_DSS_AUDIO_ENABLED)
  159. return -EPERM;
  160. r = in->ops.hdmi->audio_start(in);
  161. if (r)
  162. return r;
  163. dssdev->audio_state = OMAP_DSS_AUDIO_PLAYING;
  164. return 0;
  165. }
  166. static void hdmic_audio_stop(struct omap_dss_device *dssdev)
  167. {
  168. struct panel_drv_data *ddata = to_panel_data(dssdev);
  169. struct omap_dss_device *in = ddata->in;
  170. in->ops.hdmi->audio_stop(in);
  171. dssdev->audio_state = OMAP_DSS_AUDIO_ENABLED;
  172. }
  173. static bool hdmic_audio_supported(struct omap_dss_device *dssdev)
  174. {
  175. struct panel_drv_data *ddata = to_panel_data(dssdev);
  176. struct omap_dss_device *in = ddata->in;
  177. if (!omapdss_device_is_enabled(dssdev))
  178. return false;
  179. return in->ops.hdmi->audio_supported(in);
  180. }
  181. static int hdmic_audio_config(struct omap_dss_device *dssdev,
  182. struct omap_dss_audio *audio)
  183. {
  184. struct panel_drv_data *ddata = to_panel_data(dssdev);
  185. struct omap_dss_device *in = ddata->in;
  186. int r;
  187. /* config audio only if the display is active */
  188. if (!omapdss_device_is_enabled(dssdev))
  189. return -EPERM;
  190. r = in->ops.hdmi->audio_config(in, audio);
  191. if (r)
  192. return r;
  193. dssdev->audio_state = OMAP_DSS_AUDIO_CONFIGURED;
  194. return 0;
  195. }
  196. static int hdmic_set_hdmi_mode(struct omap_dss_device *dssdev, bool hdmi_mode)
  197. {
  198. struct panel_drv_data *ddata = to_panel_data(dssdev);
  199. struct omap_dss_device *in = ddata->in;
  200. return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
  201. }
  202. static int hdmic_set_infoframe(struct omap_dss_device *dssdev,
  203. const struct hdmi_avi_infoframe *avi)
  204. {
  205. struct panel_drv_data *ddata = to_panel_data(dssdev);
  206. struct omap_dss_device *in = ddata->in;
  207. return in->ops.hdmi->set_infoframe(in, avi);
  208. }
  209. static struct omap_dss_driver hdmic_driver = {
  210. .connect = hdmic_connect,
  211. .disconnect = hdmic_disconnect,
  212. .enable = hdmic_enable,
  213. .disable = hdmic_disable,
  214. .set_timings = hdmic_set_timings,
  215. .get_timings = hdmic_get_timings,
  216. .check_timings = hdmic_check_timings,
  217. .get_resolution = omapdss_default_get_resolution,
  218. .read_edid = hdmic_read_edid,
  219. .detect = hdmic_detect,
  220. .set_hdmi_mode = hdmic_set_hdmi_mode,
  221. .set_hdmi_infoframe = hdmic_set_infoframe,
  222. .audio_enable = hdmic_audio_enable,
  223. .audio_disable = hdmic_audio_disable,
  224. .audio_start = hdmic_audio_start,
  225. .audio_stop = hdmic_audio_stop,
  226. .audio_supported = hdmic_audio_supported,
  227. .audio_config = hdmic_audio_config,
  228. };
  229. static int hdmic_probe_pdata(struct platform_device *pdev)
  230. {
  231. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  232. struct connector_hdmi_platform_data *pdata;
  233. struct omap_dss_device *in, *dssdev;
  234. pdata = dev_get_platdata(&pdev->dev);
  235. ddata->hpd_gpio = -ENODEV;
  236. in = omap_dss_find_output(pdata->source);
  237. if (in == NULL) {
  238. dev_err(&pdev->dev, "Failed to find video source\n");
  239. return -EPROBE_DEFER;
  240. }
  241. ddata->in = in;
  242. dssdev = &ddata->dssdev;
  243. dssdev->name = pdata->name;
  244. return 0;
  245. }
  246. static int hdmic_probe_of(struct platform_device *pdev)
  247. {
  248. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  249. struct device_node *node = pdev->dev.of_node;
  250. struct omap_dss_device *in;
  251. int gpio;
  252. /* HPD GPIO */
  253. gpio = of_get_named_gpio(node, "hpd-gpios", 0);
  254. if (gpio_is_valid(gpio))
  255. ddata->hpd_gpio = gpio;
  256. else
  257. ddata->hpd_gpio = -ENODEV;
  258. in = omapdss_of_find_source_for_first_ep(node);
  259. if (IS_ERR(in)) {
  260. dev_err(&pdev->dev, "failed to find video source\n");
  261. return PTR_ERR(in);
  262. }
  263. ddata->in = in;
  264. return 0;
  265. }
  266. static int hdmic_probe(struct platform_device *pdev)
  267. {
  268. struct panel_drv_data *ddata;
  269. struct omap_dss_device *dssdev;
  270. int r;
  271. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  272. if (!ddata)
  273. return -ENOMEM;
  274. platform_set_drvdata(pdev, ddata);
  275. ddata->dev = &pdev->dev;
  276. if (dev_get_platdata(&pdev->dev)) {
  277. r = hdmic_probe_pdata(pdev);
  278. if (r)
  279. return r;
  280. } else if (pdev->dev.of_node) {
  281. r = hdmic_probe_of(pdev);
  282. if (r)
  283. return r;
  284. } else {
  285. return -ENODEV;
  286. }
  287. if (gpio_is_valid(ddata->hpd_gpio)) {
  288. r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
  289. GPIOF_DIR_IN, "hdmi_hpd");
  290. if (r)
  291. goto err_reg;
  292. }
  293. ddata->timings = hdmic_default_timings;
  294. dssdev = &ddata->dssdev;
  295. dssdev->driver = &hdmic_driver;
  296. dssdev->dev = &pdev->dev;
  297. dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
  298. dssdev->owner = THIS_MODULE;
  299. dssdev->panel.timings = hdmic_default_timings;
  300. r = omapdss_register_display(dssdev);
  301. if (r) {
  302. dev_err(&pdev->dev, "Failed to register panel\n");
  303. goto err_reg;
  304. }
  305. return 0;
  306. err_reg:
  307. omap_dss_put_device(ddata->in);
  308. return r;
  309. }
  310. static int __exit hdmic_remove(struct platform_device *pdev)
  311. {
  312. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  313. struct omap_dss_device *dssdev = &ddata->dssdev;
  314. struct omap_dss_device *in = ddata->in;
  315. omapdss_unregister_display(&ddata->dssdev);
  316. hdmic_disable(dssdev);
  317. hdmic_disconnect(dssdev);
  318. omap_dss_put_device(in);
  319. return 0;
  320. }
  321. static const struct of_device_id hdmic_of_match[] = {
  322. { .compatible = "omapdss,hdmi-connector", },
  323. {},
  324. };
  325. MODULE_DEVICE_TABLE(of, hdmic_of_match);
  326. static struct platform_driver hdmi_connector_driver = {
  327. .probe = hdmic_probe,
  328. .remove = __exit_p(hdmic_remove),
  329. .driver = {
  330. .name = "connector-hdmi",
  331. .owner = THIS_MODULE,
  332. .of_match_table = hdmic_of_match,
  333. },
  334. };
  335. module_platform_driver(hdmi_connector_driver);
  336. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  337. MODULE_DESCRIPTION("HDMI Connector driver");
  338. MODULE_LICENSE("GPL");