sdi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2009 Nokia Corporation
  4. * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
  5. */
  6. #define DSS_SUBSYS_NAME "SDI"
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/string.h>
  15. #include <drm/drm_bridge.h>
  16. #include "dss.h"
  17. #include "omapdss.h"
  18. struct sdi_device {
  19. struct platform_device *pdev;
  20. struct dss_device *dss;
  21. bool update_enabled;
  22. struct regulator *vdds_sdi_reg;
  23. struct dss_lcd_mgr_config mgr_config;
  24. unsigned long pixelclock;
  25. int datapairs;
  26. struct omap_dss_device output;
  27. struct drm_bridge bridge;
  28. };
  29. #define drm_bridge_to_sdi(bridge) \
  30. container_of(bridge, struct sdi_device, bridge)
  31. struct sdi_clk_calc_ctx {
  32. struct sdi_device *sdi;
  33. unsigned long pck_min, pck_max;
  34. unsigned long fck;
  35. struct dispc_clock_info dispc_cinfo;
  36. };
  37. static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
  38. unsigned long pck, void *data)
  39. {
  40. struct sdi_clk_calc_ctx *ctx = data;
  41. ctx->dispc_cinfo.lck_div = lckd;
  42. ctx->dispc_cinfo.pck_div = pckd;
  43. ctx->dispc_cinfo.lck = lck;
  44. ctx->dispc_cinfo.pck = pck;
  45. return true;
  46. }
  47. static bool dpi_calc_dss_cb(unsigned long fck, void *data)
  48. {
  49. struct sdi_clk_calc_ctx *ctx = data;
  50. ctx->fck = fck;
  51. return dispc_div_calc(ctx->sdi->dss->dispc, fck,
  52. ctx->pck_min, ctx->pck_max,
  53. dpi_calc_dispc_cb, ctx);
  54. }
  55. static int sdi_calc_clock_div(struct sdi_device *sdi, unsigned long pclk,
  56. unsigned long *fck,
  57. struct dispc_clock_info *dispc_cinfo)
  58. {
  59. int i;
  60. struct sdi_clk_calc_ctx ctx;
  61. /*
  62. * DSS fclk gives us very few possibilities, so finding a good pixel
  63. * clock may not be possible. We try multiple times to find the clock,
  64. * each time widening the pixel clock range we look for, up to
  65. * +/- 1MHz.
  66. */
  67. for (i = 0; i < 10; ++i) {
  68. bool ok;
  69. memset(&ctx, 0, sizeof(ctx));
  70. ctx.sdi = sdi;
  71. if (pclk > 1000 * i * i * i)
  72. ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
  73. else
  74. ctx.pck_min = 0;
  75. ctx.pck_max = pclk + 1000 * i * i * i;
  76. ok = dss_div_calc(sdi->dss, pclk, ctx.pck_min,
  77. dpi_calc_dss_cb, &ctx);
  78. if (ok) {
  79. *fck = ctx.fck;
  80. *dispc_cinfo = ctx.dispc_cinfo;
  81. return 0;
  82. }
  83. }
  84. return -EINVAL;
  85. }
  86. static void sdi_config_lcd_manager(struct sdi_device *sdi)
  87. {
  88. sdi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
  89. sdi->mgr_config.stallmode = false;
  90. sdi->mgr_config.fifohandcheck = false;
  91. sdi->mgr_config.video_port_width = 24;
  92. sdi->mgr_config.lcden_sig_polarity = 1;
  93. dss_mgr_set_lcd_config(&sdi->output, &sdi->mgr_config);
  94. }
  95. /* -----------------------------------------------------------------------------
  96. * DRM Bridge Operations
  97. */
  98. static int sdi_bridge_attach(struct drm_bridge *bridge,
  99. enum drm_bridge_attach_flags flags)
  100. {
  101. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  102. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
  103. return -EINVAL;
  104. return drm_bridge_attach(bridge->encoder, sdi->output.next_bridge,
  105. bridge, flags);
  106. }
  107. static enum drm_mode_status
  108. sdi_bridge_mode_valid(struct drm_bridge *bridge,
  109. const struct drm_display_info *info,
  110. const struct drm_display_mode *mode)
  111. {
  112. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  113. unsigned long pixelclock = mode->clock * 1000;
  114. struct dispc_clock_info dispc_cinfo;
  115. unsigned long fck;
  116. int ret;
  117. if (pixelclock == 0)
  118. return MODE_NOCLOCK;
  119. ret = sdi_calc_clock_div(sdi, pixelclock, &fck, &dispc_cinfo);
  120. if (ret < 0)
  121. return MODE_CLOCK_RANGE;
  122. return MODE_OK;
  123. }
  124. static bool sdi_bridge_mode_fixup(struct drm_bridge *bridge,
  125. const struct drm_display_mode *mode,
  126. struct drm_display_mode *adjusted_mode)
  127. {
  128. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  129. unsigned long pixelclock = mode->clock * 1000;
  130. struct dispc_clock_info dispc_cinfo;
  131. unsigned long fck;
  132. unsigned long pck;
  133. int ret;
  134. ret = sdi_calc_clock_div(sdi, pixelclock, &fck, &dispc_cinfo);
  135. if (ret < 0)
  136. return false;
  137. pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
  138. if (pck != pixelclock)
  139. dev_dbg(&sdi->pdev->dev,
  140. "pixel clock adjusted from %lu Hz to %lu Hz\n",
  141. pixelclock, pck);
  142. adjusted_mode->clock = pck / 1000;
  143. return true;
  144. }
  145. static void sdi_bridge_mode_set(struct drm_bridge *bridge,
  146. const struct drm_display_mode *mode,
  147. const struct drm_display_mode *adjusted_mode)
  148. {
  149. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  150. sdi->pixelclock = adjusted_mode->clock * 1000;
  151. }
  152. static void sdi_bridge_enable(struct drm_bridge *bridge)
  153. {
  154. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  155. struct dispc_clock_info dispc_cinfo;
  156. unsigned long fck;
  157. int r;
  158. r = regulator_enable(sdi->vdds_sdi_reg);
  159. if (r)
  160. return;
  161. r = dispc_runtime_get(sdi->dss->dispc);
  162. if (r)
  163. goto err_get_dispc;
  164. r = sdi_calc_clock_div(sdi, sdi->pixelclock, &fck, &dispc_cinfo);
  165. if (r)
  166. goto err_calc_clock_div;
  167. sdi->mgr_config.clock_info = dispc_cinfo;
  168. r = dss_set_fck_rate(sdi->dss, fck);
  169. if (r)
  170. goto err_set_dss_clock_div;
  171. sdi_config_lcd_manager(sdi);
  172. /*
  173. * LCLK and PCLK divisors are located in shadow registers, and we
  174. * normally write them to DISPC registers when enabling the output.
  175. * However, SDI uses pck-free as source clock for its PLL, and pck-free
  176. * is affected by the divisors. And as we need the PLL before enabling
  177. * the output, we need to write the divisors early.
  178. *
  179. * It seems just writing to the DISPC register is enough, and we don't
  180. * need to care about the shadow register mechanism for pck-free. The
  181. * exact reason for this is unknown.
  182. */
  183. dispc_mgr_set_clock_div(sdi->dss->dispc, sdi->output.dispc_channel,
  184. &sdi->mgr_config.clock_info);
  185. dss_sdi_init(sdi->dss, sdi->datapairs);
  186. r = dss_sdi_enable(sdi->dss);
  187. if (r)
  188. goto err_sdi_enable;
  189. mdelay(2);
  190. r = dss_mgr_enable(&sdi->output);
  191. if (r)
  192. goto err_mgr_enable;
  193. return;
  194. err_mgr_enable:
  195. dss_sdi_disable(sdi->dss);
  196. err_sdi_enable:
  197. err_set_dss_clock_div:
  198. err_calc_clock_div:
  199. dispc_runtime_put(sdi->dss->dispc);
  200. err_get_dispc:
  201. regulator_disable(sdi->vdds_sdi_reg);
  202. }
  203. static void sdi_bridge_disable(struct drm_bridge *bridge)
  204. {
  205. struct sdi_device *sdi = drm_bridge_to_sdi(bridge);
  206. dss_mgr_disable(&sdi->output);
  207. dss_sdi_disable(sdi->dss);
  208. dispc_runtime_put(sdi->dss->dispc);
  209. regulator_disable(sdi->vdds_sdi_reg);
  210. }
  211. static const struct drm_bridge_funcs sdi_bridge_funcs = {
  212. .attach = sdi_bridge_attach,
  213. .mode_valid = sdi_bridge_mode_valid,
  214. .mode_fixup = sdi_bridge_mode_fixup,
  215. .mode_set = sdi_bridge_mode_set,
  216. .enable = sdi_bridge_enable,
  217. .disable = sdi_bridge_disable,
  218. };
  219. static void sdi_bridge_init(struct sdi_device *sdi)
  220. {
  221. sdi->bridge.funcs = &sdi_bridge_funcs;
  222. sdi->bridge.of_node = sdi->pdev->dev.of_node;
  223. sdi->bridge.type = DRM_MODE_CONNECTOR_LVDS;
  224. drm_bridge_add(&sdi->bridge);
  225. }
  226. static void sdi_bridge_cleanup(struct sdi_device *sdi)
  227. {
  228. drm_bridge_remove(&sdi->bridge);
  229. }
  230. /* -----------------------------------------------------------------------------
  231. * Initialisation and Cleanup
  232. */
  233. static int sdi_init_output(struct sdi_device *sdi)
  234. {
  235. struct omap_dss_device *out = &sdi->output;
  236. int r;
  237. sdi_bridge_init(sdi);
  238. out->dev = &sdi->pdev->dev;
  239. out->id = OMAP_DSS_OUTPUT_SDI;
  240. out->type = OMAP_DISPLAY_TYPE_SDI;
  241. out->name = "sdi.0";
  242. out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
  243. /* We have SDI only on OMAP3, where it's on port 1 */
  244. out->of_port = 1;
  245. out->bus_flags = DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE /* 15.5.9.1.2 */
  246. | DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
  247. r = omapdss_device_init_output(out, &sdi->bridge);
  248. if (r < 0) {
  249. sdi_bridge_cleanup(sdi);
  250. return r;
  251. }
  252. omapdss_device_register(out);
  253. return 0;
  254. }
  255. static void sdi_uninit_output(struct sdi_device *sdi)
  256. {
  257. omapdss_device_unregister(&sdi->output);
  258. omapdss_device_cleanup_output(&sdi->output);
  259. sdi_bridge_cleanup(sdi);
  260. }
  261. int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
  262. struct device_node *port)
  263. {
  264. struct sdi_device *sdi;
  265. struct device_node *ep;
  266. u32 datapairs;
  267. int r;
  268. sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
  269. if (!sdi)
  270. return -ENOMEM;
  271. ep = of_get_next_child(port, NULL);
  272. if (!ep) {
  273. r = 0;
  274. goto err_free;
  275. }
  276. r = of_property_read_u32(ep, "datapairs", &datapairs);
  277. of_node_put(ep);
  278. if (r) {
  279. DSSERR("failed to parse datapairs\n");
  280. goto err_free;
  281. }
  282. sdi->datapairs = datapairs;
  283. sdi->dss = dss;
  284. sdi->pdev = pdev;
  285. port->data = sdi;
  286. sdi->vdds_sdi_reg = devm_regulator_get(&pdev->dev, "vdds_sdi");
  287. if (IS_ERR(sdi->vdds_sdi_reg)) {
  288. r = PTR_ERR(sdi->vdds_sdi_reg);
  289. if (r != -EPROBE_DEFER)
  290. DSSERR("can't get VDDS_SDI regulator\n");
  291. goto err_free;
  292. }
  293. r = sdi_init_output(sdi);
  294. if (r)
  295. goto err_free;
  296. return 0;
  297. err_free:
  298. kfree(sdi);
  299. return r;
  300. }
  301. void sdi_uninit_port(struct device_node *port)
  302. {
  303. struct sdi_device *sdi = port->data;
  304. if (!sdi)
  305. return;
  306. sdi_uninit_output(sdi);
  307. kfree(sdi);
  308. }