sdi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <linux/of.h>
  28. #include "omapdss.h"
  29. #include "dss.h"
  30. static struct {
  31. struct platform_device *pdev;
  32. bool update_enabled;
  33. struct regulator *vdds_sdi_reg;
  34. struct dss_lcd_mgr_config mgr_config;
  35. struct videomode vm;
  36. int datapairs;
  37. struct omap_dss_device output;
  38. bool port_initialized;
  39. } sdi;
  40. struct sdi_clk_calc_ctx {
  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(fck, ctx->pck_min, ctx->pck_max,
  60. dpi_calc_dispc_cb, ctx);
  61. }
  62. static int sdi_calc_clock_div(unsigned long pclk,
  63. unsigned long *fck,
  64. struct dispc_clock_info *dispc_cinfo)
  65. {
  66. int i;
  67. struct sdi_clk_calc_ctx ctx;
  68. /*
  69. * DSS fclk gives us very few possibilities, so finding a good pixel
  70. * clock may not be possible. We try multiple times to find the clock,
  71. * each time widening the pixel clock range we look for, up to
  72. * +/- 1MHz.
  73. */
  74. for (i = 0; i < 10; ++i) {
  75. bool ok;
  76. memset(&ctx, 0, sizeof(ctx));
  77. if (pclk > 1000 * i * i * i)
  78. ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
  79. else
  80. ctx.pck_min = 0;
  81. ctx.pck_max = pclk + 1000 * i * i * i;
  82. ok = dss_div_calc(pclk, ctx.pck_min, dpi_calc_dss_cb, &ctx);
  83. if (ok) {
  84. *fck = ctx.fck;
  85. *dispc_cinfo = ctx.dispc_cinfo;
  86. return 0;
  87. }
  88. }
  89. return -EINVAL;
  90. }
  91. static void sdi_config_lcd_manager(struct omap_dss_device *dssdev)
  92. {
  93. enum omap_channel channel = dssdev->dispc_channel;
  94. sdi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
  95. sdi.mgr_config.stallmode = false;
  96. sdi.mgr_config.fifohandcheck = false;
  97. sdi.mgr_config.video_port_width = 24;
  98. sdi.mgr_config.lcden_sig_polarity = 1;
  99. dss_mgr_set_lcd_config(channel, &sdi.mgr_config);
  100. }
  101. static int sdi_display_enable(struct omap_dss_device *dssdev)
  102. {
  103. struct omap_dss_device *out = &sdi.output;
  104. enum omap_channel channel = dssdev->dispc_channel;
  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 (!out->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();
  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(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(channel, vm);
  133. r = dss_set_fck_rate(fck);
  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(channel, &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(channel);
  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. static void sdi_display_disable(struct omap_dss_device *dssdev)
  170. {
  171. enum omap_channel channel = dssdev->dispc_channel;
  172. dss_mgr_disable(channel);
  173. dss_sdi_disable();
  174. dispc_runtime_put();
  175. regulator_disable(sdi.vdds_sdi_reg);
  176. }
  177. static void sdi_set_timings(struct omap_dss_device *dssdev,
  178. struct videomode *vm)
  179. {
  180. sdi.vm = *vm;
  181. }
  182. static void sdi_get_timings(struct omap_dss_device *dssdev,
  183. struct videomode *vm)
  184. {
  185. *vm = sdi.vm;
  186. }
  187. static int sdi_check_timings(struct omap_dss_device *dssdev,
  188. struct videomode *vm)
  189. {
  190. enum omap_channel channel = dssdev->dispc_channel;
  191. if (!dispc_mgr_timings_ok(channel, vm))
  192. return -EINVAL;
  193. if (vm->pixelclock == 0)
  194. return -EINVAL;
  195. return 0;
  196. }
  197. static int sdi_init_regulator(void)
  198. {
  199. struct regulator *vdds_sdi;
  200. if (sdi.vdds_sdi_reg)
  201. return 0;
  202. vdds_sdi = devm_regulator_get(&sdi.pdev->dev, "vdds_sdi");
  203. if (IS_ERR(vdds_sdi)) {
  204. if (PTR_ERR(vdds_sdi) != -EPROBE_DEFER)
  205. DSSERR("can't get VDDS_SDI regulator\n");
  206. return PTR_ERR(vdds_sdi);
  207. }
  208. sdi.vdds_sdi_reg = vdds_sdi;
  209. return 0;
  210. }
  211. static int sdi_connect(struct omap_dss_device *dssdev,
  212. struct omap_dss_device *dst)
  213. {
  214. enum omap_channel channel = dssdev->dispc_channel;
  215. int r;
  216. r = sdi_init_regulator();
  217. if (r)
  218. return r;
  219. r = dss_mgr_connect(channel, dssdev);
  220. if (r)
  221. return r;
  222. r = omapdss_output_set_device(dssdev, dst);
  223. if (r) {
  224. DSSERR("failed to connect output to new device: %s\n",
  225. dst->name);
  226. dss_mgr_disconnect(channel, dssdev);
  227. return r;
  228. }
  229. return 0;
  230. }
  231. static void sdi_disconnect(struct omap_dss_device *dssdev,
  232. struct omap_dss_device *dst)
  233. {
  234. enum omap_channel channel = dssdev->dispc_channel;
  235. WARN_ON(dst != dssdev->dst);
  236. if (dst != dssdev->dst)
  237. return;
  238. omapdss_output_unset_device(dssdev);
  239. dss_mgr_disconnect(channel, dssdev);
  240. }
  241. static const struct omapdss_sdi_ops sdi_ops = {
  242. .connect = sdi_connect,
  243. .disconnect = sdi_disconnect,
  244. .enable = sdi_display_enable,
  245. .disable = sdi_display_disable,
  246. .check_timings = sdi_check_timings,
  247. .set_timings = sdi_set_timings,
  248. .get_timings = sdi_get_timings,
  249. };
  250. static void sdi_init_output(struct platform_device *pdev)
  251. {
  252. struct omap_dss_device *out = &sdi.output;
  253. out->dev = &pdev->dev;
  254. out->id = OMAP_DSS_OUTPUT_SDI;
  255. out->output_type = OMAP_DISPLAY_TYPE_SDI;
  256. out->name = "sdi.0";
  257. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  258. /* We have SDI only on OMAP3, where it's on port 1 */
  259. out->port_num = 1;
  260. out->ops.sdi = &sdi_ops;
  261. out->owner = THIS_MODULE;
  262. omapdss_register_output(out);
  263. }
  264. static void sdi_uninit_output(struct platform_device *pdev)
  265. {
  266. struct omap_dss_device *out = &sdi.output;
  267. omapdss_unregister_output(out);
  268. }
  269. int sdi_init_port(struct platform_device *pdev, 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. of_node_put(ep);
  284. sdi.pdev = pdev;
  285. sdi_init_output(pdev);
  286. sdi.port_initialized = true;
  287. return 0;
  288. err_datapairs:
  289. of_node_put(ep);
  290. return r;
  291. }
  292. void sdi_uninit_port(struct device_node *port)
  293. {
  294. if (!sdi.port_initialized)
  295. return;
  296. sdi_uninit_output(sdi.pdev);
  297. }