sdi.c 10 KB

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