sdi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2009 Nokia Corporation
  3. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define DSS_SUBSYS_NAME "SDI"
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/export.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/string.h>
  25. #include <linux/of.h>
  26. #include "omapdss.h"
  27. #include "dss.h"
  28. struct sdi_device {
  29. struct platform_device *pdev;
  30. struct dss_device *dss;
  31. bool update_enabled;
  32. struct regulator *vdds_sdi_reg;
  33. struct dss_lcd_mgr_config mgr_config;
  34. struct videomode vm;
  35. int datapairs;
  36. struct omap_dss_device output;
  37. };
  38. #define dssdev_to_sdi(dssdev) container_of(dssdev, struct sdi_device, output)
  39. struct sdi_clk_calc_ctx {
  40. struct sdi_device *sdi;
  41. unsigned long pck_min, pck_max;
  42. unsigned long fck;
  43. struct dispc_clock_info dispc_cinfo;
  44. };
  45. static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
  46. unsigned long pck, void *data)
  47. {
  48. struct sdi_clk_calc_ctx *ctx = data;
  49. ctx->dispc_cinfo.lck_div = lckd;
  50. ctx->dispc_cinfo.pck_div = pckd;
  51. ctx->dispc_cinfo.lck = lck;
  52. ctx->dispc_cinfo.pck = pck;
  53. return true;
  54. }
  55. static bool dpi_calc_dss_cb(unsigned long fck, void *data)
  56. {
  57. struct sdi_clk_calc_ctx *ctx = data;
  58. ctx->fck = fck;
  59. return dispc_div_calc(ctx->sdi->dss->dispc, fck,
  60. ctx->pck_min, ctx->pck_max,
  61. dpi_calc_dispc_cb, ctx);
  62. }
  63. static int sdi_calc_clock_div(struct sdi_device *sdi, 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. ctx.sdi = sdi;
  79. if (pclk > 1000 * i * i * i)
  80. ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
  81. else
  82. ctx.pck_min = 0;
  83. ctx.pck_max = pclk + 1000 * i * i * i;
  84. ok = dss_div_calc(sdi->dss, pclk, ctx.pck_min,
  85. dpi_calc_dss_cb, &ctx);
  86. if (ok) {
  87. *fck = ctx.fck;
  88. *dispc_cinfo = ctx.dispc_cinfo;
  89. return 0;
  90. }
  91. }
  92. return -EINVAL;
  93. }
  94. static void sdi_config_lcd_manager(struct sdi_device *sdi)
  95. {
  96. sdi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
  97. sdi->mgr_config.stallmode = false;
  98. sdi->mgr_config.fifohandcheck = false;
  99. sdi->mgr_config.video_port_width = 24;
  100. sdi->mgr_config.lcden_sig_polarity = 1;
  101. dss_mgr_set_lcd_config(&sdi->output, &sdi->mgr_config);
  102. }
  103. static int sdi_display_enable(struct omap_dss_device *dssdev)
  104. {
  105. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  106. struct videomode *vm = &sdi->vm;
  107. unsigned long fck;
  108. struct dispc_clock_info dispc_cinfo;
  109. unsigned long pck;
  110. int r;
  111. if (!sdi->output.dispc_channel_connected) {
  112. DSSERR("failed to enable display: no output/manager\n");
  113. return -ENODEV;
  114. }
  115. r = regulator_enable(sdi->vdds_sdi_reg);
  116. if (r)
  117. goto err_reg_enable;
  118. r = dispc_runtime_get(sdi->dss->dispc);
  119. if (r)
  120. goto err_get_dispc;
  121. /* 15.5.9.1.2 */
  122. vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE | DISPLAY_FLAGS_SYNC_POSEDGE;
  123. r = sdi_calc_clock_div(sdi, vm->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 != vm->pixelclock) {
  129. DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
  130. vm->pixelclock, pck);
  131. vm->pixelclock = pck;
  132. }
  133. dss_mgr_set_timings(&sdi->output, vm);
  134. r = dss_set_fck_rate(sdi->dss, fck);
  135. if (r)
  136. goto err_set_dss_clock_div;
  137. sdi_config_lcd_manager(sdi);
  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(sdi->dss->dispc, sdi->output.dispc_channel,
  150. &sdi->mgr_config.clock_info);
  151. dss_sdi_init(sdi->dss, sdi->datapairs);
  152. r = dss_sdi_enable(sdi->dss);
  153. if (r)
  154. goto err_sdi_enable;
  155. mdelay(2);
  156. r = dss_mgr_enable(&sdi->output);
  157. if (r)
  158. goto err_mgr_enable;
  159. return 0;
  160. err_mgr_enable:
  161. dss_sdi_disable(sdi->dss);
  162. err_sdi_enable:
  163. err_set_dss_clock_div:
  164. err_calc_clock_div:
  165. dispc_runtime_put(sdi->dss->dispc);
  166. err_get_dispc:
  167. regulator_disable(sdi->vdds_sdi_reg);
  168. err_reg_enable:
  169. return r;
  170. }
  171. static void sdi_display_disable(struct omap_dss_device *dssdev)
  172. {
  173. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  174. dss_mgr_disable(&sdi->output);
  175. dss_sdi_disable(sdi->dss);
  176. dispc_runtime_put(sdi->dss->dispc);
  177. regulator_disable(sdi->vdds_sdi_reg);
  178. }
  179. static void sdi_set_timings(struct omap_dss_device *dssdev,
  180. struct videomode *vm)
  181. {
  182. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  183. sdi->vm = *vm;
  184. }
  185. static void sdi_get_timings(struct omap_dss_device *dssdev,
  186. struct videomode *vm)
  187. {
  188. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  189. *vm = sdi->vm;
  190. }
  191. static int sdi_check_timings(struct omap_dss_device *dssdev,
  192. struct videomode *vm)
  193. {
  194. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  195. enum omap_channel channel = dssdev->dispc_channel;
  196. if (!dispc_mgr_timings_ok(sdi->dss->dispc, channel, vm))
  197. return -EINVAL;
  198. if (vm->pixelclock == 0)
  199. return -EINVAL;
  200. return 0;
  201. }
  202. static int sdi_init_regulator(struct sdi_device *sdi)
  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 sdi_device *sdi = dssdev_to_sdi(dssdev);
  220. int r;
  221. r = sdi_init_regulator(sdi);
  222. if (r)
  223. return r;
  224. r = dss_mgr_connect(&sdi->output, dssdev);
  225. if (r)
  226. return r;
  227. r = omapdss_output_set_device(dssdev, dst);
  228. if (r) {
  229. DSSERR("failed to connect output to new device: %s\n",
  230. dst->name);
  231. dss_mgr_disconnect(&sdi->output, dssdev);
  232. return r;
  233. }
  234. return 0;
  235. }
  236. static void sdi_disconnect(struct omap_dss_device *dssdev,
  237. struct omap_dss_device *dst)
  238. {
  239. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  240. WARN_ON(dst != dssdev->dst);
  241. if (dst != dssdev->dst)
  242. return;
  243. omapdss_output_unset_device(dssdev);
  244. dss_mgr_disconnect(&sdi->output, dssdev);
  245. }
  246. static const struct omapdss_sdi_ops sdi_ops = {
  247. .connect = sdi_connect,
  248. .disconnect = sdi_disconnect,
  249. .enable = sdi_display_enable,
  250. .disable = sdi_display_disable,
  251. .check_timings = sdi_check_timings,
  252. .set_timings = sdi_set_timings,
  253. .get_timings = sdi_get_timings,
  254. };
  255. static void sdi_init_output(struct sdi_device *sdi)
  256. {
  257. struct omap_dss_device *out = &sdi->output;
  258. out->dev = &sdi->pdev->dev;
  259. out->id = OMAP_DSS_OUTPUT_SDI;
  260. out->output_type = OMAP_DISPLAY_TYPE_SDI;
  261. out->name = "sdi.0";
  262. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  263. /* We have SDI only on OMAP3, where it's on port 1 */
  264. out->port_num = 1;
  265. out->ops.sdi = &sdi_ops;
  266. out->owner = THIS_MODULE;
  267. omapdss_register_output(out);
  268. }
  269. static void sdi_uninit_output(struct sdi_device *sdi)
  270. {
  271. omapdss_unregister_output(&sdi->output);
  272. }
  273. int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
  274. struct device_node *port)
  275. {
  276. struct sdi_device *sdi;
  277. struct device_node *ep;
  278. u32 datapairs;
  279. int r;
  280. sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
  281. if (!sdi)
  282. return -ENOMEM;
  283. ep = of_get_next_child(port, NULL);
  284. if (!ep) {
  285. r = 0;
  286. goto err_free;
  287. }
  288. r = of_property_read_u32(ep, "datapairs", &datapairs);
  289. if (r) {
  290. DSSERR("failed to parse datapairs\n");
  291. goto err_datapairs;
  292. }
  293. sdi->datapairs = datapairs;
  294. sdi->dss = dss;
  295. of_node_put(ep);
  296. sdi->pdev = pdev;
  297. port->data = sdi;
  298. sdi_init_output(sdi);
  299. return 0;
  300. err_datapairs:
  301. of_node_put(ep);
  302. err_free:
  303. kfree(sdi);
  304. return r;
  305. }
  306. void sdi_uninit_port(struct device_node *port)
  307. {
  308. struct sdi_device *sdi = port->data;
  309. if (!sdi)
  310. return;
  311. sdi_uninit_output(sdi);
  312. kfree(sdi);
  313. }