1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Nokia Corporation
6 * Some code and ideas taken from drivers/video/omap/ driver
10 #define DSS_SUBSYS_NAME "DPI"
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/export.h>
17 #include <linux/kernel.h>
19 #include <linux/of_graph.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/string.h>
23 #include <linux/sys_soc.h>
25 #include <drm/drm_bridge.h>
31 struct platform_device *pdev;
32 enum dss_model dss_model;
33 struct dss_device *dss;
36 struct regulator *vdds_dsi_reg;
37 enum dss_clk_source clk_src;
40 struct dss_lcd_mgr_config mgr_config;
41 unsigned long pixelclock;
44 struct omap_dss_device output;
45 struct drm_bridge bridge;
48 #define drm_bridge_to_dpi(bridge) container_of(bridge, struct dpi_data, bridge)
50 /* -----------------------------------------------------------------------------
51 * Clock Handling and PLL
54 static enum dss_clk_source dpi_get_clk_src_dra7xx(struct dpi_data *dpi,
55 enum omap_channel channel)
58 * Possible clock sources:
59 * LCD1: FCK/PLL1_1/HDMI_PLL
60 * LCD2: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_3)
61 * LCD3: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_1)
65 case OMAP_DSS_CHANNEL_LCD:
67 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_1))
68 return DSS_CLK_SRC_PLL1_1;
71 case OMAP_DSS_CHANNEL_LCD2:
73 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
74 return DSS_CLK_SRC_PLL1_3;
75 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_3))
76 return DSS_CLK_SRC_PLL2_3;
79 case OMAP_DSS_CHANNEL_LCD3:
81 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_1))
82 return DSS_CLK_SRC_PLL2_1;
83 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
84 return DSS_CLK_SRC_PLL1_3;
91 return DSS_CLK_SRC_FCK;
94 static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
96 enum omap_channel channel = dpi->output.dispc_channel;
99 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
100 * would also be used for DISPC fclk. Meaning, when the DPI output is
101 * disabled, DISPC clock will be disabled, and TV out will stop.
103 switch (dpi->dss_model) {
104 case DSS_MODEL_OMAP2:
105 case DSS_MODEL_OMAP3:
106 return DSS_CLK_SRC_FCK;
108 case DSS_MODEL_OMAP4:
110 case OMAP_DSS_CHANNEL_LCD:
111 return DSS_CLK_SRC_PLL1_1;
112 case OMAP_DSS_CHANNEL_LCD2:
113 return DSS_CLK_SRC_PLL2_1;
115 return DSS_CLK_SRC_FCK;
118 case DSS_MODEL_OMAP5:
120 case OMAP_DSS_CHANNEL_LCD:
121 return DSS_CLK_SRC_PLL1_1;
122 case OMAP_DSS_CHANNEL_LCD3:
123 return DSS_CLK_SRC_PLL2_1;
124 case OMAP_DSS_CHANNEL_LCD2:
126 return DSS_CLK_SRC_FCK;
130 return dpi_get_clk_src_dra7xx(dpi, channel);
133 return DSS_CLK_SRC_FCK;
137 struct dpi_clk_calc_ctx {
138 struct dpi_data *dpi;
139 unsigned int clkout_idx;
143 unsigned long pck_min, pck_max;
147 struct dss_pll_clock_info pll_cinfo;
149 struct dispc_clock_info dispc_cinfo;
152 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
153 unsigned long pck, void *data)
155 struct dpi_clk_calc_ctx *ctx = data;
158 * Odd dividers give us uneven duty cycle, causing problem when level
159 * shifted. So skip all odd dividers when the pixel clock is on the
162 if (ctx->pck_min >= 100000000) {
163 if (lckd > 1 && lckd % 2 != 0)
166 if (pckd > 1 && pckd % 2 != 0)
170 ctx->dispc_cinfo.lck_div = lckd;
171 ctx->dispc_cinfo.pck_div = pckd;
172 ctx->dispc_cinfo.lck = lck;
173 ctx->dispc_cinfo.pck = pck;
179 static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
182 struct dpi_clk_calc_ctx *ctx = data;
184 ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
185 ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
187 return dispc_div_calc(ctx->dpi->dss->dispc, dispc,
188 ctx->pck_min, ctx->pck_max,
189 dpi_calc_dispc_cb, ctx);
193 static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
194 unsigned long clkdco,
197 struct dpi_clk_calc_ctx *ctx = data;
199 ctx->pll_cinfo.n = n;
200 ctx->pll_cinfo.m = m;
201 ctx->pll_cinfo.fint = fint;
202 ctx->pll_cinfo.clkdco = clkdco;
204 return dss_pll_hsdiv_calc_a(ctx->dpi->pll, clkdco,
205 ctx->pck_min, dss_get_max_fck_rate(ctx->dpi->dss),
206 dpi_calc_hsdiv_cb, ctx);
209 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
211 struct dpi_clk_calc_ctx *ctx = data;
215 return dispc_div_calc(ctx->dpi->dss->dispc, fck,
216 ctx->pck_min, ctx->pck_max,
217 dpi_calc_dispc_cb, ctx);
220 static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
221 struct dpi_clk_calc_ctx *ctx)
225 memset(ctx, 0, sizeof(*ctx));
227 ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
229 clkin = clk_get_rate(dpi->pll->clkin);
231 if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
232 unsigned long pll_min, pll_max;
234 ctx->pck_min = pck - 1000;
235 ctx->pck_max = pck + 1000;
240 return dss_pll_calc_a(ctx->dpi->pll, clkin,
242 dpi_calc_pll_cb, ctx);
243 } else { /* DSS_PLL_TYPE_B */
244 dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
246 ctx->dispc_cinfo.lck_div = 1;
247 ctx->dispc_cinfo.pck_div = 1;
248 ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
249 ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;
255 static bool dpi_dss_clk_calc(struct dpi_data *dpi, unsigned long pck,
256 struct dpi_clk_calc_ctx *ctx)
261 * DSS fck gives us very few possibilities, so finding a good pixel
262 * clock may not be possible. We try multiple times to find the clock,
263 * each time widening the pixel clock range we look for, up to
267 for (i = 0; i < 25; ++i) {
270 memset(ctx, 0, sizeof(*ctx));
272 if (pck > 1000 * i * i * i)
273 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
276 ctx->pck_max = pck + 1000 * i * i * i;
278 ok = dss_div_calc(dpi->dss, pck, ctx->pck_min,
279 dpi_calc_dss_cb, ctx);
289 static int dpi_set_pll_clk(struct dpi_data *dpi, unsigned long pck_req)
291 struct dpi_clk_calc_ctx ctx;
295 ok = dpi_pll_clk_calc(dpi, pck_req, &ctx);
299 r = dss_pll_set_config(dpi->pll, &ctx.pll_cinfo);
303 dss_select_lcd_clk_source(dpi->dss, dpi->output.dispc_channel,
306 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
311 static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req)
313 struct dpi_clk_calc_ctx ctx;
317 ok = dpi_dss_clk_calc(dpi, pck_req, &ctx);
321 r = dss_set_fck_rate(dpi->dss, ctx.fck);
325 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
330 static int dpi_set_mode(struct dpi_data *dpi)
335 r = dpi_set_pll_clk(dpi, dpi->pixelclock);
337 r = dpi_set_dispc_clk(dpi, dpi->pixelclock);
342 static void dpi_config_lcd_manager(struct dpi_data *dpi)
344 dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
346 dpi->mgr_config.stallmode = false;
347 dpi->mgr_config.fifohandcheck = false;
349 dpi->mgr_config.video_port_width = dpi->data_lines;
351 dpi->mgr_config.lcden_sig_polarity = 0;
353 dss_mgr_set_lcd_config(&dpi->output, &dpi->mgr_config);
356 static int dpi_clock_update(struct dpi_data *dpi, unsigned long *clock)
358 int lck_div, pck_div;
360 struct dpi_clk_calc_ctx ctx;
363 if (!dpi_pll_clk_calc(dpi, *clock, &ctx))
366 fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
368 if (!dpi_dss_clk_calc(dpi, *clock, &ctx))
374 lck_div = ctx.dispc_cinfo.lck_div;
375 pck_div = ctx.dispc_cinfo.pck_div;
377 *clock = fck / lck_div / pck_div;
382 static int dpi_verify_pll(struct dss_pll *pll)
386 /* do initial setup with the PLL to see if it is operational */
388 r = dss_pll_enable(pll);
392 dss_pll_disable(pll);
397 static void dpi_init_pll(struct dpi_data *dpi)
404 dpi->clk_src = dpi_get_clk_src(dpi);
406 pll = dss_pll_find_by_src(dpi->dss, dpi->clk_src);
410 if (dpi_verify_pll(pll)) {
411 DSSWARN("PLL not operational\n");
418 /* -----------------------------------------------------------------------------
419 * DRM Bridge Operations
422 static int dpi_bridge_attach(struct drm_bridge *bridge,
423 enum drm_bridge_attach_flags flags)
425 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
427 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
432 return drm_bridge_attach(bridge->encoder, dpi->output.next_bridge,
436 static enum drm_mode_status
437 dpi_bridge_mode_valid(struct drm_bridge *bridge,
438 const struct drm_display_info *info,
439 const struct drm_display_mode *mode)
441 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
442 unsigned long clock = mode->clock * 1000;
445 if (mode->hdisplay % 8 != 0)
446 return MODE_BAD_WIDTH;
448 if (mode->clock == 0)
451 ret = dpi_clock_update(dpi, &clock);
453 return MODE_CLOCK_RANGE;
458 static bool dpi_bridge_mode_fixup(struct drm_bridge *bridge,
459 const struct drm_display_mode *mode,
460 struct drm_display_mode *adjusted_mode)
462 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
463 unsigned long clock = mode->clock * 1000;
466 ret = dpi_clock_update(dpi, &clock);
470 adjusted_mode->clock = clock / 1000;
475 static void dpi_bridge_mode_set(struct drm_bridge *bridge,
476 const struct drm_display_mode *mode,
477 const struct drm_display_mode *adjusted_mode)
479 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
481 dpi->pixelclock = adjusted_mode->clock * 1000;
484 static void dpi_bridge_enable(struct drm_bridge *bridge)
486 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
489 if (dpi->vdds_dsi_reg) {
490 r = regulator_enable(dpi->vdds_dsi_reg);
495 r = dispc_runtime_get(dpi->dss->dispc);
499 r = dss_dpi_select_source(dpi->dss, dpi->id, dpi->output.dispc_channel);
504 r = dss_pll_enable(dpi->pll);
509 r = dpi_set_mode(dpi);
513 dpi_config_lcd_manager(dpi);
517 r = dss_mgr_enable(&dpi->output);
526 dss_pll_disable(dpi->pll);
529 dispc_runtime_put(dpi->dss->dispc);
531 if (dpi->vdds_dsi_reg)
532 regulator_disable(dpi->vdds_dsi_reg);
535 static void dpi_bridge_disable(struct drm_bridge *bridge)
537 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
539 dss_mgr_disable(&dpi->output);
542 dss_select_lcd_clk_source(dpi->dss, dpi->output.dispc_channel,
544 dss_pll_disable(dpi->pll);
547 dispc_runtime_put(dpi->dss->dispc);
549 if (dpi->vdds_dsi_reg)
550 regulator_disable(dpi->vdds_dsi_reg);
553 static const struct drm_bridge_funcs dpi_bridge_funcs = {
554 .attach = dpi_bridge_attach,
555 .mode_valid = dpi_bridge_mode_valid,
556 .mode_fixup = dpi_bridge_mode_fixup,
557 .mode_set = dpi_bridge_mode_set,
558 .enable = dpi_bridge_enable,
559 .disable = dpi_bridge_disable,
562 static void dpi_bridge_init(struct dpi_data *dpi)
564 dpi->bridge.funcs = &dpi_bridge_funcs;
565 dpi->bridge.of_node = dpi->pdev->dev.of_node;
566 dpi->bridge.type = DRM_MODE_CONNECTOR_DPI;
568 drm_bridge_add(&dpi->bridge);
571 static void dpi_bridge_cleanup(struct dpi_data *dpi)
573 drm_bridge_remove(&dpi->bridge);
576 /* -----------------------------------------------------------------------------
577 * Initialisation and Cleanup
581 * Return a hardcoded channel for the DPI output. This should work for
582 * current use cases, but this can be later expanded to either resolve
583 * the channel in some more dynamic manner, or get the channel as a user
586 static enum omap_channel dpi_get_channel(struct dpi_data *dpi)
588 switch (dpi->dss_model) {
589 case DSS_MODEL_OMAP2:
590 case DSS_MODEL_OMAP3:
591 return OMAP_DSS_CHANNEL_LCD;
596 return OMAP_DSS_CHANNEL_LCD3;
598 return OMAP_DSS_CHANNEL_LCD2;
601 return OMAP_DSS_CHANNEL_LCD;
604 case DSS_MODEL_OMAP4:
605 return OMAP_DSS_CHANNEL_LCD2;
607 case DSS_MODEL_OMAP5:
608 return OMAP_DSS_CHANNEL_LCD3;
611 DSSWARN("unsupported DSS version\n");
612 return OMAP_DSS_CHANNEL_LCD;
616 static int dpi_init_output_port(struct dpi_data *dpi, struct device_node *port)
618 struct omap_dss_device *out = &dpi->output;
622 dpi_bridge_init(dpi);
624 of_property_read_u32(port, "reg", &port_num);
625 dpi->id = port_num <= 2 ? port_num : 0;
640 out->dev = &dpi->pdev->dev;
641 out->id = OMAP_DSS_OUTPUT_DPI;
642 out->type = OMAP_DISPLAY_TYPE_DPI;
643 out->dispc_channel = dpi_get_channel(dpi);
644 out->of_port = port_num;
646 r = omapdss_device_init_output(out, &dpi->bridge);
648 dpi_bridge_cleanup(dpi);
652 omapdss_device_register(out);
657 static void dpi_uninit_output_port(struct device_node *port)
659 struct dpi_data *dpi = port->data;
660 struct omap_dss_device *out = &dpi->output;
662 omapdss_device_unregister(out);
663 omapdss_device_cleanup_output(out);
665 dpi_bridge_cleanup(dpi);
668 /* -----------------------------------------------------------------------------
669 * Initialisation and Cleanup
672 static const struct soc_device_attribute dpi_soc_devices[] = {
673 { .machine = "OMAP3[456]*" },
674 { .machine = "[AD]M37*" },
678 static int dpi_init_regulator(struct dpi_data *dpi)
680 struct regulator *vdds_dsi;
683 * The DPI uses the DSI VDDS on OMAP34xx, OMAP35xx, OMAP36xx, AM37xx and
686 if (!soc_device_match(dpi_soc_devices))
689 vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
690 if (IS_ERR(vdds_dsi)) {
691 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
692 DSSERR("can't get VDDS_DSI regulator\n");
693 return PTR_ERR(vdds_dsi);
696 dpi->vdds_dsi_reg = vdds_dsi;
701 int dpi_init_port(struct dss_device *dss, struct platform_device *pdev,
702 struct device_node *port, enum dss_model dss_model)
704 struct dpi_data *dpi;
705 struct device_node *ep;
709 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
713 ep = of_graph_get_next_port_endpoint(port, NULL);
717 r = of_property_read_u32(ep, "data-lines", &datalines);
720 DSSERR("failed to parse datalines\n");
724 dpi->data_lines = datalines;
727 dpi->dss_model = dss_model;
731 r = dpi_init_regulator(dpi);
735 return dpi_init_output_port(dpi, port);
738 void dpi_uninit_port(struct device_node *port)
740 struct dpi_data *dpi = port->data;
745 dpi_uninit_output_port(port);