sdi.c 8.3 KB

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