sdi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. static struct {
  29. struct platform_device *pdev;
  30. bool update_enabled;
  31. struct regulator *vdds_sdi_reg;
  32. struct dss_lcd_mgr_config mgr_config;
  33. struct videomode vm;
  34. int datapairs;
  35. struct omap_dss_device output;
  36. bool port_initialized;
  37. } sdi;
  38. struct sdi_clk_calc_ctx {
  39. unsigned long pck_min, pck_max;
  40. unsigned long fck;
  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(unsigned long fck, void *data)
  54. {
  55. struct sdi_clk_calc_ctx *ctx = data;
  56. ctx->fck = fck;
  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. unsigned long *fck,
  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(pclk, ctx.pck_min, dpi_calc_dss_cb, &ctx);
  81. if (ok) {
  82. *fck = ctx.fck;
  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. enum omap_channel channel = dssdev->dispc_channel;
  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(channel, &sdi.mgr_config);
  98. }
  99. static int sdi_display_enable(struct omap_dss_device *dssdev)
  100. {
  101. struct omap_dss_device *out = &sdi.output;
  102. enum omap_channel channel = dssdev->dispc_channel;
  103. struct videomode *vm = &sdi.vm;
  104. unsigned long fck;
  105. struct dispc_clock_info dispc_cinfo;
  106. unsigned long pck;
  107. int r;
  108. if (!out->dispc_channel_connected) {
  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. vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE | DISPLAY_FLAGS_SYNC_POSEDGE;
  120. r = sdi_calc_clock_div(vm->pixelclock, &fck, &dispc_cinfo);
  121. if (r)
  122. goto err_calc_clock_div;
  123. sdi.mgr_config.clock_info = dispc_cinfo;
  124. pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
  125. if (pck != vm->pixelclock) {
  126. DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
  127. vm->pixelclock, pck);
  128. vm->pixelclock = pck;
  129. }
  130. dss_mgr_set_timings(channel, vm);
  131. r = dss_set_fck_rate(fck);
  132. if (r)
  133. goto err_set_dss_clock_div;
  134. sdi_config_lcd_manager(dssdev);
  135. /*
  136. * LCLK and PCLK divisors are located in shadow registers, and we
  137. * normally write them to DISPC registers when enabling the output.
  138. * However, SDI uses pck-free as source clock for its PLL, and pck-free
  139. * is affected by the divisors. And as we need the PLL before enabling
  140. * the output, we need to write the divisors early.
  141. *
  142. * It seems just writing to the DISPC register is enough, and we don't
  143. * need to care about the shadow register mechanism for pck-free. The
  144. * exact reason for this is unknown.
  145. */
  146. dispc_mgr_set_clock_div(channel, &sdi.mgr_config.clock_info);
  147. dss_sdi_init(sdi.datapairs);
  148. r = dss_sdi_enable();
  149. if (r)
  150. goto err_sdi_enable;
  151. mdelay(2);
  152. r = dss_mgr_enable(channel);
  153. if (r)
  154. goto err_mgr_enable;
  155. return 0;
  156. err_mgr_enable:
  157. dss_sdi_disable();
  158. err_sdi_enable:
  159. err_set_dss_clock_div:
  160. err_calc_clock_div:
  161. dispc_runtime_put();
  162. err_get_dispc:
  163. regulator_disable(sdi.vdds_sdi_reg);
  164. err_reg_enable:
  165. return r;
  166. }
  167. static void sdi_display_disable(struct omap_dss_device *dssdev)
  168. {
  169. enum omap_channel channel = dssdev->dispc_channel;
  170. dss_mgr_disable(channel);
  171. dss_sdi_disable();
  172. dispc_runtime_put();
  173. regulator_disable(sdi.vdds_sdi_reg);
  174. }
  175. static void sdi_set_timings(struct omap_dss_device *dssdev,
  176. struct videomode *vm)
  177. {
  178. sdi.vm = *vm;
  179. }
  180. static void sdi_get_timings(struct omap_dss_device *dssdev,
  181. struct videomode *vm)
  182. {
  183. *vm = sdi.vm;
  184. }
  185. static int sdi_check_timings(struct omap_dss_device *dssdev,
  186. struct videomode *vm)
  187. {
  188. enum omap_channel channel = dssdev->dispc_channel;
  189. if (!dispc_mgr_timings_ok(channel, vm))
  190. return -EINVAL;
  191. if (vm->pixelclock == 0)
  192. return -EINVAL;
  193. return 0;
  194. }
  195. static int sdi_init_regulator(void)
  196. {
  197. struct regulator *vdds_sdi;
  198. if (sdi.vdds_sdi_reg)
  199. return 0;
  200. vdds_sdi = devm_regulator_get(&sdi.pdev->dev, "vdds_sdi");
  201. if (IS_ERR(vdds_sdi)) {
  202. if (PTR_ERR(vdds_sdi) != -EPROBE_DEFER)
  203. DSSERR("can't get VDDS_SDI regulator\n");
  204. return PTR_ERR(vdds_sdi);
  205. }
  206. sdi.vdds_sdi_reg = vdds_sdi;
  207. return 0;
  208. }
  209. static int sdi_connect(struct omap_dss_device *dssdev,
  210. struct omap_dss_device *dst)
  211. {
  212. enum omap_channel channel = dssdev->dispc_channel;
  213. int r;
  214. r = sdi_init_regulator();
  215. if (r)
  216. return r;
  217. r = dss_mgr_connect(channel, dssdev);
  218. if (r)
  219. return r;
  220. r = omapdss_output_set_device(dssdev, dst);
  221. if (r) {
  222. DSSERR("failed to connect output to new device: %s\n",
  223. dst->name);
  224. dss_mgr_disconnect(channel, dssdev);
  225. return r;
  226. }
  227. return 0;
  228. }
  229. static void sdi_disconnect(struct omap_dss_device *dssdev,
  230. struct omap_dss_device *dst)
  231. {
  232. enum omap_channel channel = dssdev->dispc_channel;
  233. WARN_ON(dst != dssdev->dst);
  234. if (dst != dssdev->dst)
  235. return;
  236. omapdss_output_unset_device(dssdev);
  237. dss_mgr_disconnect(channel, dssdev);
  238. }
  239. static const struct omapdss_sdi_ops sdi_ops = {
  240. .connect = sdi_connect,
  241. .disconnect = sdi_disconnect,
  242. .enable = sdi_display_enable,
  243. .disable = sdi_display_disable,
  244. .check_timings = sdi_check_timings,
  245. .set_timings = sdi_set_timings,
  246. .get_timings = sdi_get_timings,
  247. };
  248. static void sdi_init_output(struct platform_device *pdev)
  249. {
  250. struct omap_dss_device *out = &sdi.output;
  251. out->dev = &pdev->dev;
  252. out->id = OMAP_DSS_OUTPUT_SDI;
  253. out->output_type = OMAP_DISPLAY_TYPE_SDI;
  254. out->name = "sdi.0";
  255. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  256. /* We have SDI only on OMAP3, where it's on port 1 */
  257. out->port_num = 1;
  258. out->ops.sdi = &sdi_ops;
  259. out->owner = THIS_MODULE;
  260. omapdss_register_output(out);
  261. }
  262. static void sdi_uninit_output(struct platform_device *pdev)
  263. {
  264. struct omap_dss_device *out = &sdi.output;
  265. omapdss_unregister_output(out);
  266. }
  267. int sdi_init_port(struct platform_device *pdev, struct device_node *port)
  268. {
  269. struct device_node *ep;
  270. u32 datapairs;
  271. int r;
  272. ep = of_get_next_child(port, NULL);
  273. if (!ep)
  274. return 0;
  275. r = of_property_read_u32(ep, "datapairs", &datapairs);
  276. if (r) {
  277. DSSERR("failed to parse datapairs\n");
  278. goto err_datapairs;
  279. }
  280. sdi.datapairs = datapairs;
  281. of_node_put(ep);
  282. sdi.pdev = pdev;
  283. sdi_init_output(pdev);
  284. sdi.port_initialized = true;
  285. return 0;
  286. err_datapairs:
  287. of_node_put(ep);
  288. return r;
  289. }
  290. void sdi_uninit_port(struct device_node *port)
  291. {
  292. if (!sdi.port_initialized)
  293. return;
  294. sdi_uninit_output(sdi.pdev);
  295. }