sdi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * linux/drivers/video/omap2/dss/sdi.c
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.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. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define DSS_SUBSYS_NAME "SDI"
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/export.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/string.h>
  27. #include <linux/of.h>
  28. #include <linux/component.h>
  29. #include <video/omapdss.h>
  30. #include "dss.h"
  31. static struct {
  32. struct platform_device *pdev;
  33. bool update_enabled;
  34. struct regulator *vdds_sdi_reg;
  35. struct dss_lcd_mgr_config mgr_config;
  36. struct omap_video_timings timings;
  37. int datapairs;
  38. struct omap_dss_device output;
  39. bool port_initialized;
  40. } sdi;
  41. struct sdi_clk_calc_ctx {
  42. unsigned long pck_min, pck_max;
  43. unsigned long fck;
  44. struct dispc_clock_info dispc_cinfo;
  45. };
  46. static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
  47. unsigned long pck, void *data)
  48. {
  49. struct sdi_clk_calc_ctx *ctx = data;
  50. ctx->dispc_cinfo.lck_div = lckd;
  51. ctx->dispc_cinfo.pck_div = pckd;
  52. ctx->dispc_cinfo.lck = lck;
  53. ctx->dispc_cinfo.pck = pck;
  54. return true;
  55. }
  56. static bool dpi_calc_dss_cb(unsigned long fck, void *data)
  57. {
  58. struct sdi_clk_calc_ctx *ctx = data;
  59. ctx->fck = fck;
  60. return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
  61. dpi_calc_dispc_cb, ctx);
  62. }
  63. static int sdi_calc_clock_div(unsigned long pclk,
  64. unsigned long *fck,
  65. struct dispc_clock_info *dispc_cinfo)
  66. {
  67. int i;
  68. struct sdi_clk_calc_ctx ctx;
  69. /*
  70. * DSS fclk gives us very few possibilities, so finding a good pixel
  71. * clock may not be possible. We try multiple times to find the clock,
  72. * each time widening the pixel clock range we look for, up to
  73. * +/- 1MHz.
  74. */
  75. for (i = 0; i < 10; ++i) {
  76. bool ok;
  77. memset(&ctx, 0, sizeof(ctx));
  78. if (pclk > 1000 * i * i * i)
  79. ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
  80. else
  81. ctx.pck_min = 0;
  82. ctx.pck_max = pclk + 1000 * i * i * i;
  83. ok = dss_div_calc(pclk, ctx.pck_min, dpi_calc_dss_cb, &ctx);
  84. if (ok) {
  85. *fck = ctx.fck;
  86. *dispc_cinfo = ctx.dispc_cinfo;
  87. return 0;
  88. }
  89. }
  90. return -EINVAL;
  91. }
  92. static void sdi_config_lcd_manager(struct omap_dss_device *dssdev)
  93. {
  94. struct omap_overlay_manager *mgr = sdi.output.manager;
  95. sdi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
  96. sdi.mgr_config.stallmode = false;
  97. sdi.mgr_config.fifohandcheck = false;
  98. sdi.mgr_config.video_port_width = 24;
  99. sdi.mgr_config.lcden_sig_polarity = 1;
  100. dss_mgr_set_lcd_config(mgr, &sdi.mgr_config);
  101. }
  102. static int sdi_display_enable(struct omap_dss_device *dssdev)
  103. {
  104. struct omap_dss_device *out = &sdi.output;
  105. struct omap_video_timings *t = &sdi.timings;
  106. unsigned long fck;
  107. struct dispc_clock_info dispc_cinfo;
  108. unsigned long pck;
  109. int r;
  110. if (out == NULL || out->manager == NULL) {
  111. DSSERR("failed to enable display: no output/manager\n");
  112. return -ENODEV;
  113. }
  114. r = regulator_enable(sdi.vdds_sdi_reg);
  115. if (r)
  116. goto err_reg_enable;
  117. r = dispc_runtime_get();
  118. if (r)
  119. goto err_get_dispc;
  120. /* 15.5.9.1.2 */
  121. t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
  122. t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
  123. r = sdi_calc_clock_div(t->pixelclock, &fck, &dispc_cinfo);
  124. if (r)
  125. goto err_calc_clock_div;
  126. sdi.mgr_config.clock_info = dispc_cinfo;
  127. pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
  128. if (pck != t->pixelclock) {
  129. DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
  130. t->pixelclock, pck);
  131. t->pixelclock = pck;
  132. }
  133. dss_mgr_set_timings(out->manager, t);
  134. r = dss_set_fck_rate(fck);
  135. if (r)
  136. goto err_set_dss_clock_div;
  137. sdi_config_lcd_manager(dssdev);
  138. /*
  139. * LCLK and PCLK divisors are located in shadow registers, and we
  140. * normally write them to DISPC registers when enabling the output.
  141. * However, SDI uses pck-free as source clock for its PLL, and pck-free
  142. * is affected by the divisors. And as we need the PLL before enabling
  143. * the output, we need to write the divisors early.
  144. *
  145. * It seems just writing to the DISPC register is enough, and we don't
  146. * need to care about the shadow register mechanism for pck-free. The
  147. * exact reason for this is unknown.
  148. */
  149. dispc_mgr_set_clock_div(out->manager->id, &sdi.mgr_config.clock_info);
  150. dss_sdi_init(sdi.datapairs);
  151. r = dss_sdi_enable();
  152. if (r)
  153. goto err_sdi_enable;
  154. mdelay(2);
  155. r = dss_mgr_enable(out->manager);
  156. if (r)
  157. goto err_mgr_enable;
  158. return 0;
  159. err_mgr_enable:
  160. dss_sdi_disable();
  161. err_sdi_enable:
  162. err_set_dss_clock_div:
  163. err_calc_clock_div:
  164. dispc_runtime_put();
  165. err_get_dispc:
  166. regulator_disable(sdi.vdds_sdi_reg);
  167. err_reg_enable:
  168. return r;
  169. }
  170. static void sdi_display_disable(struct omap_dss_device *dssdev)
  171. {
  172. struct omap_overlay_manager *mgr = sdi.output.manager;
  173. dss_mgr_disable(mgr);
  174. dss_sdi_disable();
  175. dispc_runtime_put();
  176. regulator_disable(sdi.vdds_sdi_reg);
  177. }
  178. static void sdi_set_timings(struct omap_dss_device *dssdev,
  179. struct omap_video_timings *timings)
  180. {
  181. sdi.timings = *timings;
  182. }
  183. static void sdi_get_timings(struct omap_dss_device *dssdev,
  184. struct omap_video_timings *timings)
  185. {
  186. *timings = sdi.timings;
  187. }
  188. static int sdi_check_timings(struct omap_dss_device *dssdev,
  189. struct omap_video_timings *timings)
  190. {
  191. struct omap_overlay_manager *mgr = sdi.output.manager;
  192. if (mgr && !dispc_mgr_timings_ok(mgr->id, timings))
  193. return -EINVAL;
  194. if (timings->pixelclock == 0)
  195. return -EINVAL;
  196. return 0;
  197. }
  198. static void sdi_set_datapairs(struct omap_dss_device *dssdev, int datapairs)
  199. {
  200. sdi.datapairs = datapairs;
  201. }
  202. static int sdi_init_regulator(void)
  203. {
  204. struct regulator *vdds_sdi;
  205. if (sdi.vdds_sdi_reg)
  206. return 0;
  207. vdds_sdi = devm_regulator_get(&sdi.pdev->dev, "vdds_sdi");
  208. if (IS_ERR(vdds_sdi)) {
  209. if (PTR_ERR(vdds_sdi) != -EPROBE_DEFER)
  210. DSSERR("can't get VDDS_SDI regulator\n");
  211. return PTR_ERR(vdds_sdi);
  212. }
  213. sdi.vdds_sdi_reg = vdds_sdi;
  214. return 0;
  215. }
  216. static int sdi_connect(struct omap_dss_device *dssdev,
  217. struct omap_dss_device *dst)
  218. {
  219. struct omap_overlay_manager *mgr;
  220. int r;
  221. r = sdi_init_regulator();
  222. if (r)
  223. return r;
  224. mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
  225. if (!mgr)
  226. return -ENODEV;
  227. r = dss_mgr_connect(mgr, dssdev);
  228. if (r)
  229. return r;
  230. r = omapdss_output_set_device(dssdev, dst);
  231. if (r) {
  232. DSSERR("failed to connect output to new device: %s\n",
  233. dst->name);
  234. dss_mgr_disconnect(mgr, dssdev);
  235. return r;
  236. }
  237. return 0;
  238. }
  239. static void sdi_disconnect(struct omap_dss_device *dssdev,
  240. struct omap_dss_device *dst)
  241. {
  242. WARN_ON(dst != dssdev->dst);
  243. if (dst != dssdev->dst)
  244. return;
  245. omapdss_output_unset_device(dssdev);
  246. if (dssdev->manager)
  247. dss_mgr_disconnect(dssdev->manager, dssdev);
  248. }
  249. static const struct omapdss_sdi_ops sdi_ops = {
  250. .connect = sdi_connect,
  251. .disconnect = sdi_disconnect,
  252. .enable = sdi_display_enable,
  253. .disable = sdi_display_disable,
  254. .check_timings = sdi_check_timings,
  255. .set_timings = sdi_set_timings,
  256. .get_timings = sdi_get_timings,
  257. .set_datapairs = sdi_set_datapairs,
  258. };
  259. static void sdi_init_output(struct platform_device *pdev)
  260. {
  261. struct omap_dss_device *out = &sdi.output;
  262. out->dev = &pdev->dev;
  263. out->id = OMAP_DSS_OUTPUT_SDI;
  264. out->output_type = OMAP_DISPLAY_TYPE_SDI;
  265. out->name = "sdi.0";
  266. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  267. /* We have SDI only on OMAP3, where it's on port 1 */
  268. out->port_num = 1;
  269. out->ops.sdi = &sdi_ops;
  270. out->owner = THIS_MODULE;
  271. omapdss_register_output(out);
  272. }
  273. static void sdi_uninit_output(struct platform_device *pdev)
  274. {
  275. struct omap_dss_device *out = &sdi.output;
  276. omapdss_unregister_output(out);
  277. }
  278. static int sdi_bind(struct device *dev, struct device *master, void *data)
  279. {
  280. struct platform_device *pdev = to_platform_device(dev);
  281. sdi.pdev = pdev;
  282. sdi_init_output(pdev);
  283. return 0;
  284. }
  285. static void sdi_unbind(struct device *dev, struct device *master, void *data)
  286. {
  287. struct platform_device *pdev = to_platform_device(dev);
  288. sdi_uninit_output(pdev);
  289. }
  290. static const struct component_ops sdi_component_ops = {
  291. .bind = sdi_bind,
  292. .unbind = sdi_unbind,
  293. };
  294. static int sdi_probe(struct platform_device *pdev)
  295. {
  296. return component_add(&pdev->dev, &sdi_component_ops);
  297. }
  298. static int sdi_remove(struct platform_device *pdev)
  299. {
  300. component_del(&pdev->dev, &sdi_component_ops);
  301. return 0;
  302. }
  303. static struct platform_driver omap_sdi_driver = {
  304. .probe = sdi_probe,
  305. .remove = sdi_remove,
  306. .driver = {
  307. .name = "omapdss_sdi",
  308. .suppress_bind_attrs = true,
  309. },
  310. };
  311. int __init sdi_init_platform_driver(void)
  312. {
  313. return platform_driver_register(&omap_sdi_driver);
  314. }
  315. void sdi_uninit_platform_driver(void)
  316. {
  317. platform_driver_unregister(&omap_sdi_driver);
  318. }
  319. int sdi_init_port(struct platform_device *pdev, struct device_node *port)
  320. {
  321. struct device_node *ep;
  322. u32 datapairs;
  323. int r;
  324. ep = omapdss_of_get_next_endpoint(port, NULL);
  325. if (!ep)
  326. return 0;
  327. r = of_property_read_u32(ep, "datapairs", &datapairs);
  328. if (r) {
  329. DSSERR("failed to parse datapairs\n");
  330. goto err_datapairs;
  331. }
  332. sdi.datapairs = datapairs;
  333. of_node_put(ep);
  334. sdi.pdev = pdev;
  335. sdi_init_output(pdev);
  336. sdi.port_initialized = true;
  337. return 0;
  338. err_datapairs:
  339. of_node_put(ep);
  340. return r;
  341. }
  342. void sdi_uninit_port(struct device_node *port)
  343. {
  344. if (!sdi.port_initialized)
  345. return;
  346. sdi_uninit_output(sdi.pdev);
  347. }