sdi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 = { .sdi = sdi };
  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(sdi->dss, pclk, ctx.pck_min,
  84. dpi_calc_dss_cb, &ctx);
  85. if (ok) {
  86. *fck = ctx.fck;
  87. *dispc_cinfo = ctx.dispc_cinfo;
  88. return 0;
  89. }
  90. }
  91. return -EINVAL;
  92. }
  93. static void sdi_config_lcd_manager(struct sdi_device *sdi)
  94. {
  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(&sdi->output, &sdi->mgr_config);
  101. }
  102. static int sdi_display_enable(struct omap_dss_device *dssdev)
  103. {
  104. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  105. struct videomode *vm = &sdi->vm;
  106. unsigned long fck;
  107. struct dispc_clock_info dispc_cinfo;
  108. unsigned long pck;
  109. int r;
  110. if (!sdi->output.dispc_channel_connected) {
  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(sdi->dss->dispc);
  118. if (r)
  119. goto err_get_dispc;
  120. /* 15.5.9.1.2 */
  121. vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE | DISPLAY_FLAGS_SYNC_POSEDGE;
  122. r = sdi_calc_clock_div(sdi, vm->pixelclock, &fck, &dispc_cinfo);
  123. if (r)
  124. goto err_calc_clock_div;
  125. sdi->mgr_config.clock_info = dispc_cinfo;
  126. pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
  127. if (pck != vm->pixelclock) {
  128. DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
  129. vm->pixelclock, pck);
  130. vm->pixelclock = pck;
  131. }
  132. dss_mgr_set_timings(&sdi->output, vm);
  133. r = dss_set_fck_rate(sdi->dss, fck);
  134. if (r)
  135. goto err_set_dss_clock_div;
  136. sdi_config_lcd_manager(sdi);
  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(sdi->dss->dispc, sdi->output.dispc_channel,
  149. &sdi->mgr_config.clock_info);
  150. dss_sdi_init(sdi->dss, sdi->datapairs);
  151. r = dss_sdi_enable(sdi->dss);
  152. if (r)
  153. goto err_sdi_enable;
  154. mdelay(2);
  155. r = dss_mgr_enable(&sdi->output);
  156. if (r)
  157. goto err_mgr_enable;
  158. return 0;
  159. err_mgr_enable:
  160. dss_sdi_disable(sdi->dss);
  161. err_sdi_enable:
  162. err_set_dss_clock_div:
  163. err_calc_clock_div:
  164. dispc_runtime_put(sdi->dss->dispc);
  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 sdi_device *sdi = dssdev_to_sdi(dssdev);
  173. dss_mgr_disable(&sdi->output);
  174. dss_sdi_disable(sdi->dss);
  175. dispc_runtime_put(sdi->dss->dispc);
  176. regulator_disable(sdi->vdds_sdi_reg);
  177. }
  178. static void sdi_set_timings(struct omap_dss_device *dssdev,
  179. struct videomode *vm)
  180. {
  181. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  182. sdi->vm = *vm;
  183. }
  184. static void sdi_get_timings(struct omap_dss_device *dssdev,
  185. struct videomode *vm)
  186. {
  187. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  188. *vm = sdi->vm;
  189. }
  190. static int sdi_check_timings(struct omap_dss_device *dssdev,
  191. struct videomode *vm)
  192. {
  193. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  194. enum omap_channel channel = dssdev->dispc_channel;
  195. if (!dispc_mgr_timings_ok(sdi->dss->dispc, channel, vm))
  196. return -EINVAL;
  197. if (vm->pixelclock == 0)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. static int sdi_init_regulator(struct sdi_device *sdi)
  202. {
  203. struct regulator *vdds_sdi;
  204. if (sdi->vdds_sdi_reg)
  205. return 0;
  206. vdds_sdi = devm_regulator_get(&sdi->pdev->dev, "vdds_sdi");
  207. if (IS_ERR(vdds_sdi)) {
  208. if (PTR_ERR(vdds_sdi) != -EPROBE_DEFER)
  209. DSSERR("can't get VDDS_SDI regulator\n");
  210. return PTR_ERR(vdds_sdi);
  211. }
  212. sdi->vdds_sdi_reg = vdds_sdi;
  213. return 0;
  214. }
  215. static int sdi_connect(struct omap_dss_device *dssdev,
  216. struct omap_dss_device *dst)
  217. {
  218. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  219. int r;
  220. r = sdi_init_regulator(sdi);
  221. if (r)
  222. return r;
  223. r = dss_mgr_connect(&sdi->output, dssdev);
  224. if (r)
  225. return r;
  226. r = omapdss_output_set_device(dssdev, dst);
  227. if (r) {
  228. DSSERR("failed to connect output to new device: %s\n",
  229. dst->name);
  230. dss_mgr_disconnect(&sdi->output, dssdev);
  231. return r;
  232. }
  233. return 0;
  234. }
  235. static void sdi_disconnect(struct omap_dss_device *dssdev,
  236. struct omap_dss_device *dst)
  237. {
  238. struct sdi_device *sdi = dssdev_to_sdi(dssdev);
  239. WARN_ON(dst != dssdev->dst);
  240. if (dst != dssdev->dst)
  241. return;
  242. omapdss_output_unset_device(dssdev);
  243. dss_mgr_disconnect(&sdi->output, dssdev);
  244. }
  245. static const struct omapdss_sdi_ops sdi_ops = {
  246. .connect = sdi_connect,
  247. .disconnect = sdi_disconnect,
  248. .enable = sdi_display_enable,
  249. .disable = sdi_display_disable,
  250. .check_timings = sdi_check_timings,
  251. .set_timings = sdi_set_timings,
  252. .get_timings = sdi_get_timings,
  253. };
  254. static void sdi_init_output(struct sdi_device *sdi)
  255. {
  256. struct omap_dss_device *out = &sdi->output;
  257. out->dev = &sdi->pdev->dev;
  258. out->id = OMAP_DSS_OUTPUT_SDI;
  259. out->output_type = OMAP_DISPLAY_TYPE_SDI;
  260. out->name = "sdi.0";
  261. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  262. /* We have SDI only on OMAP3, where it's on port 1 */
  263. out->port_num = 1;
  264. out->ops.sdi = &sdi_ops;
  265. out->owner = THIS_MODULE;
  266. omapdss_register_output(out);
  267. }
  268. static void sdi_uninit_output(struct sdi_device *sdi)
  269. {
  270. omapdss_unregister_output(&sdi->output);
  271. }
  272. int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
  273. struct device_node *port)
  274. {
  275. struct sdi_device *sdi;
  276. struct device_node *ep;
  277. u32 datapairs;
  278. int r;
  279. sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
  280. if (!sdi)
  281. return -ENOMEM;
  282. ep = of_get_next_child(port, NULL);
  283. if (!ep) {
  284. r = 0;
  285. goto err_free;
  286. }
  287. r = of_property_read_u32(ep, "datapairs", &datapairs);
  288. if (r) {
  289. DSSERR("failed to parse datapairs\n");
  290. goto err_datapairs;
  291. }
  292. sdi->datapairs = datapairs;
  293. sdi->dss = dss;
  294. of_node_put(ep);
  295. sdi->pdev = pdev;
  296. port->data = sdi;
  297. sdi_init_output(sdi);
  298. return 0;
  299. err_datapairs:
  300. of_node_put(ep);
  301. err_free:
  302. kfree(sdi);
  303. return r;
  304. }
  305. void sdi_uninit_port(struct device_node *port)
  306. {
  307. struct sdi_device *sdi = port->data;
  308. if (!sdi)
  309. return;
  310. sdi_uninit_output(sdi);
  311. kfree(sdi);
  312. }