sdi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <video/omapdss.h>
  28. #include "dss.h"
  29. static struct {
  30. bool update_enabled;
  31. struct regulator *vdds_sdi_reg;
  32. struct dss_lcd_mgr_config mgr_config;
  33. struct omap_video_timings timings;
  34. int datapairs;
  35. struct omap_dss_output output;
  36. } sdi;
  37. struct sdi_clk_calc_ctx {
  38. unsigned long pck_min, pck_max;
  39. struct dss_clock_info dss_cinfo;
  40. struct dispc_clock_info dispc_cinfo;
  41. };
  42. static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
  43. unsigned long pck, void *data)
  44. {
  45. struct sdi_clk_calc_ctx *ctx = data;
  46. ctx->dispc_cinfo.lck_div = lckd;
  47. ctx->dispc_cinfo.pck_div = pckd;
  48. ctx->dispc_cinfo.lck = lck;
  49. ctx->dispc_cinfo.pck = pck;
  50. return true;
  51. }
  52. static bool dpi_calc_dss_cb(int fckd, unsigned long fck, void *data)
  53. {
  54. struct sdi_clk_calc_ctx *ctx = data;
  55. ctx->dss_cinfo.fck = fck;
  56. ctx->dss_cinfo.fck_div = fckd;
  57. return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
  58. dpi_calc_dispc_cb, ctx);
  59. }
  60. static int sdi_calc_clock_div(unsigned long pclk,
  61. struct dss_clock_info *dss_cinfo,
  62. struct dispc_clock_info *dispc_cinfo)
  63. {
  64. int i;
  65. struct sdi_clk_calc_ctx ctx;
  66. /*
  67. * DSS fclk gives us very few possibilities, so finding a good pixel
  68. * clock may not be possible. We try multiple times to find the clock,
  69. * each time widening the pixel clock range we look for, up to
  70. * +/- 1MHz.
  71. */
  72. for (i = 0; i < 10; ++i) {
  73. bool ok;
  74. memset(&ctx, 0, sizeof(ctx));
  75. if (pclk > 1000 * i * i * i)
  76. ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
  77. else
  78. ctx.pck_min = 0;
  79. ctx.pck_max = pclk + 1000 * i * i * i;
  80. ok = dss_div_calc(ctx.pck_min, dpi_calc_dss_cb, &ctx);
  81. if (ok) {
  82. *dss_cinfo = ctx.dss_cinfo;
  83. *dispc_cinfo = ctx.dispc_cinfo;
  84. return 0;
  85. }
  86. }
  87. return -EINVAL;
  88. }
  89. static void sdi_config_lcd_manager(struct omap_dss_device *dssdev)
  90. {
  91. struct omap_overlay_manager *mgr = dssdev->output->manager;
  92. sdi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
  93. sdi.mgr_config.stallmode = false;
  94. sdi.mgr_config.fifohandcheck = false;
  95. sdi.mgr_config.video_port_width = 24;
  96. sdi.mgr_config.lcden_sig_polarity = 1;
  97. dss_mgr_set_lcd_config(mgr, &sdi.mgr_config);
  98. }
  99. int omapdss_sdi_display_enable(struct omap_dss_device *dssdev)
  100. {
  101. struct omap_dss_output *out = dssdev->output;
  102. struct omap_video_timings *t = &sdi.timings;
  103. struct dss_clock_info dss_cinfo;
  104. struct dispc_clock_info dispc_cinfo;
  105. unsigned long pck;
  106. int r;
  107. if (out == NULL || out->manager == NULL) {
  108. DSSERR("failed to enable display: no output/manager\n");
  109. return -ENODEV;
  110. }
  111. r = omap_dss_start_device(dssdev);
  112. if (r) {
  113. DSSERR("failed to start device\n");
  114. goto err_start_dev;
  115. }
  116. r = regulator_enable(sdi.vdds_sdi_reg);
  117. if (r)
  118. goto err_reg_enable;
  119. r = dispc_runtime_get();
  120. if (r)
  121. goto err_get_dispc;
  122. /* 15.5.9.1.2 */
  123. t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
  124. t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
  125. r = sdi_calc_clock_div(t->pixel_clock * 1000, &dss_cinfo, &dispc_cinfo);
  126. if (r)
  127. goto err_calc_clock_div;
  128. sdi.mgr_config.clock_info = dispc_cinfo;
  129. pck = dss_cinfo.fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div / 1000;
  130. if (pck != t->pixel_clock) {
  131. DSSWARN("Could not find exact pixel clock. Requested %d kHz, "
  132. "got %lu kHz\n",
  133. t->pixel_clock, pck);
  134. t->pixel_clock = pck;
  135. }
  136. dss_mgr_set_timings(out->manager, t);
  137. r = dss_set_clock_div(&dss_cinfo);
  138. if (r)
  139. goto err_set_dss_clock_div;
  140. sdi_config_lcd_manager(dssdev);
  141. /*
  142. * LCLK and PCLK divisors are located in shadow registers, and we
  143. * normally write them to DISPC registers when enabling the output.
  144. * However, SDI uses pck-free as source clock for its PLL, and pck-free
  145. * is affected by the divisors. And as we need the PLL before enabling
  146. * the output, we need to write the divisors early.
  147. *
  148. * It seems just writing to the DISPC register is enough, and we don't
  149. * need to care about the shadow register mechanism for pck-free. The
  150. * exact reason for this is unknown.
  151. */
  152. dispc_mgr_set_clock_div(out->manager->id, &sdi.mgr_config.clock_info);
  153. dss_sdi_init(sdi.datapairs);
  154. r = dss_sdi_enable();
  155. if (r)
  156. goto err_sdi_enable;
  157. mdelay(2);
  158. r = dss_mgr_enable(out->manager);
  159. if (r)
  160. goto err_mgr_enable;
  161. return 0;
  162. err_mgr_enable:
  163. dss_sdi_disable();
  164. err_sdi_enable:
  165. err_set_dss_clock_div:
  166. err_calc_clock_div:
  167. dispc_runtime_put();
  168. err_get_dispc:
  169. regulator_disable(sdi.vdds_sdi_reg);
  170. err_reg_enable:
  171. omap_dss_stop_device(dssdev);
  172. err_start_dev:
  173. return r;
  174. }
  175. EXPORT_SYMBOL(omapdss_sdi_display_enable);
  176. void omapdss_sdi_display_disable(struct omap_dss_device *dssdev)
  177. {
  178. struct omap_overlay_manager *mgr = dssdev->output->manager;
  179. dss_mgr_disable(mgr);
  180. dss_sdi_disable();
  181. dispc_runtime_put();
  182. regulator_disable(sdi.vdds_sdi_reg);
  183. omap_dss_stop_device(dssdev);
  184. }
  185. EXPORT_SYMBOL(omapdss_sdi_display_disable);
  186. void omapdss_sdi_set_timings(struct omap_dss_device *dssdev,
  187. struct omap_video_timings *timings)
  188. {
  189. sdi.timings = *timings;
  190. }
  191. EXPORT_SYMBOL(omapdss_sdi_set_timings);
  192. void omapdss_sdi_set_datapairs(struct omap_dss_device *dssdev, int datapairs)
  193. {
  194. sdi.datapairs = datapairs;
  195. }
  196. EXPORT_SYMBOL(omapdss_sdi_set_datapairs);
  197. static int sdi_init_display(struct omap_dss_device *dssdev)
  198. {
  199. DSSDBG("SDI init\n");
  200. if (sdi.vdds_sdi_reg == NULL) {
  201. struct regulator *vdds_sdi;
  202. vdds_sdi = dss_get_vdds_sdi();
  203. if (IS_ERR(vdds_sdi)) {
  204. DSSERR("can't get VDDS_SDI regulator\n");
  205. return PTR_ERR(vdds_sdi);
  206. }
  207. sdi.vdds_sdi_reg = vdds_sdi;
  208. }
  209. return 0;
  210. }
  211. static struct omap_dss_device *sdi_find_dssdev(struct platform_device *pdev)
  212. {
  213. struct omap_dss_board_info *pdata = pdev->dev.platform_data;
  214. const char *def_disp_name = omapdss_get_default_display_name();
  215. struct omap_dss_device *def_dssdev;
  216. int i;
  217. def_dssdev = NULL;
  218. for (i = 0; i < pdata->num_devices; ++i) {
  219. struct omap_dss_device *dssdev = pdata->devices[i];
  220. if (dssdev->type != OMAP_DISPLAY_TYPE_SDI)
  221. continue;
  222. if (def_dssdev == NULL)
  223. def_dssdev = dssdev;
  224. if (def_disp_name != NULL &&
  225. strcmp(dssdev->name, def_disp_name) == 0) {
  226. def_dssdev = dssdev;
  227. break;
  228. }
  229. }
  230. return def_dssdev;
  231. }
  232. static int sdi_probe_pdata(struct platform_device *sdidev)
  233. {
  234. struct omap_dss_device *plat_dssdev;
  235. struct omap_dss_device *dssdev;
  236. int r;
  237. plat_dssdev = sdi_find_dssdev(sdidev);
  238. if (!plat_dssdev)
  239. return 0;
  240. dssdev = dss_alloc_and_init_device(&sdidev->dev);
  241. if (!dssdev)
  242. return -ENOMEM;
  243. dss_copy_device_pdata(dssdev, plat_dssdev);
  244. r = sdi_init_display(dssdev);
  245. if (r) {
  246. DSSERR("device %s init failed: %d\n", dssdev->name, r);
  247. dss_put_device(dssdev);
  248. return r;
  249. }
  250. r = omapdss_output_set_device(&sdi.output, dssdev);
  251. if (r) {
  252. DSSERR("failed to connect output to new device: %s\n",
  253. dssdev->name);
  254. dss_put_device(dssdev);
  255. return r;
  256. }
  257. r = dss_add_device(dssdev);
  258. if (r) {
  259. DSSERR("device %s register failed: %d\n", dssdev->name, r);
  260. omapdss_output_unset_device(&sdi.output);
  261. dss_put_device(dssdev);
  262. return r;
  263. }
  264. return 0;
  265. }
  266. static void sdi_init_output(struct platform_device *pdev)
  267. {
  268. struct omap_dss_output *out = &sdi.output;
  269. out->pdev = pdev;
  270. out->id = OMAP_DSS_OUTPUT_SDI;
  271. out->type = OMAP_DISPLAY_TYPE_SDI;
  272. out->name = "sdi.0";
  273. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  274. dss_register_output(out);
  275. }
  276. static void __exit sdi_uninit_output(struct platform_device *pdev)
  277. {
  278. struct omap_dss_output *out = &sdi.output;
  279. dss_unregister_output(out);
  280. }
  281. static int omap_sdi_probe(struct platform_device *pdev)
  282. {
  283. int r;
  284. sdi_init_output(pdev);
  285. r = sdi_probe_pdata(pdev);
  286. if (r) {
  287. sdi_uninit_output(pdev);
  288. return r;
  289. }
  290. return 0;
  291. }
  292. static int __exit omap_sdi_remove(struct platform_device *pdev)
  293. {
  294. dss_unregister_child_devices(&pdev->dev);
  295. sdi_uninit_output(pdev);
  296. return 0;
  297. }
  298. static struct platform_driver omap_sdi_driver = {
  299. .probe = omap_sdi_probe,
  300. .remove = __exit_p(omap_sdi_remove),
  301. .driver = {
  302. .name = "omapdss_sdi",
  303. .owner = THIS_MODULE,
  304. },
  305. };
  306. int __init sdi_init_platform_driver(void)
  307. {
  308. return platform_driver_register(&omap_sdi_driver);
  309. }
  310. void __exit sdi_uninit_platform_driver(void)
  311. {
  312. platform_driver_unregister(&omap_sdi_driver);
  313. }