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/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/export.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/string.h>
21 #include <linux/clk.h>
22 #include <linux/sys_soc.h>
28 struct platform_device *pdev;
29 enum dss_model dss_model;
30 struct dss_device *dss;
33 struct regulator *vdds_dsi_reg;
34 enum dss_clk_source clk_src;
39 struct dss_lcd_mgr_config mgr_config;
40 unsigned long pixelclock;
43 struct omap_dss_device output;
46 static struct dpi_data *dpi_get_data_from_dssdev(struct omap_dss_device *dssdev)
48 return container_of(dssdev, struct dpi_data, output);
51 static enum dss_clk_source dpi_get_clk_src_dra7xx(struct dpi_data *dpi,
52 enum omap_channel channel)
55 * Possible clock sources:
56 * LCD1: FCK/PLL1_1/HDMI_PLL
57 * LCD2: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_3)
58 * LCD3: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_1)
62 case OMAP_DSS_CHANNEL_LCD:
64 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_1))
65 return DSS_CLK_SRC_PLL1_1;
68 case OMAP_DSS_CHANNEL_LCD2:
70 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
71 return DSS_CLK_SRC_PLL1_3;
72 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_3))
73 return DSS_CLK_SRC_PLL2_3;
76 case OMAP_DSS_CHANNEL_LCD3:
78 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_1))
79 return DSS_CLK_SRC_PLL2_1;
80 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
81 return DSS_CLK_SRC_PLL1_3;
88 return DSS_CLK_SRC_FCK;
91 static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
93 enum omap_channel channel = dpi->output.dispc_channel;
96 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
97 * would also be used for DISPC fclk. Meaning, when the DPI output is
98 * disabled, DISPC clock will be disabled, and TV out will stop.
100 switch (dpi->dss_model) {
101 case DSS_MODEL_OMAP2:
102 case DSS_MODEL_OMAP3:
103 return DSS_CLK_SRC_FCK;
105 case DSS_MODEL_OMAP4:
107 case OMAP_DSS_CHANNEL_LCD:
108 return DSS_CLK_SRC_PLL1_1;
109 case OMAP_DSS_CHANNEL_LCD2:
110 return DSS_CLK_SRC_PLL2_1;
112 return DSS_CLK_SRC_FCK;
115 case DSS_MODEL_OMAP5:
117 case OMAP_DSS_CHANNEL_LCD:
118 return DSS_CLK_SRC_PLL1_1;
119 case OMAP_DSS_CHANNEL_LCD3:
120 return DSS_CLK_SRC_PLL2_1;
121 case OMAP_DSS_CHANNEL_LCD2:
123 return DSS_CLK_SRC_FCK;
127 return dpi_get_clk_src_dra7xx(dpi, channel);
130 return DSS_CLK_SRC_FCK;
134 struct dpi_clk_calc_ctx {
135 struct dpi_data *dpi;
136 unsigned int clkout_idx;
140 unsigned long pck_min, pck_max;
144 struct dss_pll_clock_info pll_cinfo;
146 struct dispc_clock_info dispc_cinfo;
149 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
150 unsigned long pck, void *data)
152 struct dpi_clk_calc_ctx *ctx = data;
155 * Odd dividers give us uneven duty cycle, causing problem when level
156 * shifted. So skip all odd dividers when the pixel clock is on the
159 if (ctx->pck_min >= 100000000) {
160 if (lckd > 1 && lckd % 2 != 0)
163 if (pckd > 1 && pckd % 2 != 0)
167 ctx->dispc_cinfo.lck_div = lckd;
168 ctx->dispc_cinfo.pck_div = pckd;
169 ctx->dispc_cinfo.lck = lck;
170 ctx->dispc_cinfo.pck = pck;
176 static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
179 struct dpi_clk_calc_ctx *ctx = data;
181 ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
182 ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
184 return dispc_div_calc(ctx->dpi->dss->dispc, dispc,
185 ctx->pck_min, ctx->pck_max,
186 dpi_calc_dispc_cb, ctx);
190 static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
191 unsigned long clkdco,
194 struct dpi_clk_calc_ctx *ctx = data;
196 ctx->pll_cinfo.n = n;
197 ctx->pll_cinfo.m = m;
198 ctx->pll_cinfo.fint = fint;
199 ctx->pll_cinfo.clkdco = clkdco;
201 return dss_pll_hsdiv_calc_a(ctx->dpi->pll, clkdco,
202 ctx->pck_min, dss_get_max_fck_rate(ctx->dpi->dss),
203 dpi_calc_hsdiv_cb, ctx);
206 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
208 struct dpi_clk_calc_ctx *ctx = data;
212 return dispc_div_calc(ctx->dpi->dss->dispc, fck,
213 ctx->pck_min, ctx->pck_max,
214 dpi_calc_dispc_cb, ctx);
217 static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
218 struct dpi_clk_calc_ctx *ctx)
222 memset(ctx, 0, sizeof(*ctx));
224 ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
226 clkin = clk_get_rate(dpi->pll->clkin);
228 if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
229 unsigned long pll_min, pll_max;
231 ctx->pck_min = pck - 1000;
232 ctx->pck_max = pck + 1000;
237 return dss_pll_calc_a(ctx->dpi->pll, clkin,
239 dpi_calc_pll_cb, ctx);
240 } else { /* DSS_PLL_TYPE_B */
241 dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
243 ctx->dispc_cinfo.lck_div = 1;
244 ctx->dispc_cinfo.pck_div = 1;
245 ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
246 ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;
252 static bool dpi_dss_clk_calc(struct dpi_data *dpi, unsigned long pck,
253 struct dpi_clk_calc_ctx *ctx)
258 * DSS fck gives us very few possibilities, so finding a good pixel
259 * clock may not be possible. We try multiple times to find the clock,
260 * each time widening the pixel clock range we look for, up to
264 for (i = 0; i < 25; ++i) {
267 memset(ctx, 0, sizeof(*ctx));
269 if (pck > 1000 * i * i * i)
270 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
273 ctx->pck_max = pck + 1000 * i * i * i;
275 ok = dss_div_calc(dpi->dss, pck, ctx->pck_min,
276 dpi_calc_dss_cb, ctx);
286 static int dpi_set_pll_clk(struct dpi_data *dpi, enum omap_channel channel,
287 unsigned long pck_req, unsigned long *fck, int *lck_div,
290 struct dpi_clk_calc_ctx ctx;
294 ok = dpi_pll_clk_calc(dpi, pck_req, &ctx);
298 r = dss_pll_set_config(dpi->pll, &ctx.pll_cinfo);
302 dss_select_lcd_clk_source(dpi->dss, channel, dpi->clk_src);
304 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
306 *fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
307 *lck_div = ctx.dispc_cinfo.lck_div;
308 *pck_div = ctx.dispc_cinfo.pck_div;
313 static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req,
314 unsigned long *fck, int *lck_div, int *pck_div)
316 struct dpi_clk_calc_ctx ctx;
320 ok = dpi_dss_clk_calc(dpi, pck_req, &ctx);
324 r = dss_set_fck_rate(dpi->dss, ctx.fck);
328 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
331 *lck_div = ctx.dispc_cinfo.lck_div;
332 *pck_div = ctx.dispc_cinfo.pck_div;
337 static int dpi_set_mode(struct dpi_data *dpi)
339 int lck_div = 0, pck_div = 0;
340 unsigned long fck = 0;
344 r = dpi_set_pll_clk(dpi, dpi->output.dispc_channel,
345 dpi->pixelclock, &fck, &lck_div, &pck_div);
347 r = dpi_set_dispc_clk(dpi, dpi->pixelclock, &fck,
355 static void dpi_config_lcd_manager(struct dpi_data *dpi)
357 dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
359 dpi->mgr_config.stallmode = false;
360 dpi->mgr_config.fifohandcheck = false;
362 dpi->mgr_config.video_port_width = dpi->data_lines;
364 dpi->mgr_config.lcden_sig_polarity = 0;
366 dss_mgr_set_lcd_config(&dpi->output, &dpi->mgr_config);
369 static void dpi_display_enable(struct omap_dss_device *dssdev)
371 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
372 struct omap_dss_device *out = &dpi->output;
375 mutex_lock(&dpi->lock);
377 if (dpi->vdds_dsi_reg) {
378 r = regulator_enable(dpi->vdds_dsi_reg);
383 r = dispc_runtime_get(dpi->dss->dispc);
387 r = dss_dpi_select_source(dpi->dss, dpi->id, out->dispc_channel);
392 r = dss_pll_enable(dpi->pll);
397 r = dpi_set_mode(dpi);
401 dpi_config_lcd_manager(dpi);
405 r = dss_mgr_enable(&dpi->output);
409 mutex_unlock(&dpi->lock);
416 dss_pll_disable(dpi->pll);
419 dispc_runtime_put(dpi->dss->dispc);
421 if (dpi->vdds_dsi_reg)
422 regulator_disable(dpi->vdds_dsi_reg);
424 mutex_unlock(&dpi->lock);
427 static void dpi_display_disable(struct omap_dss_device *dssdev)
429 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
431 mutex_lock(&dpi->lock);
433 dss_mgr_disable(&dpi->output);
436 dss_select_lcd_clk_source(dpi->dss, dpi->output.dispc_channel,
438 dss_pll_disable(dpi->pll);
441 dispc_runtime_put(dpi->dss->dispc);
443 if (dpi->vdds_dsi_reg)
444 regulator_disable(dpi->vdds_dsi_reg);
446 mutex_unlock(&dpi->lock);
449 static void dpi_set_timings(struct omap_dss_device *dssdev,
450 const struct drm_display_mode *mode)
452 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
454 DSSDBG("dpi_set_timings\n");
456 mutex_lock(&dpi->lock);
458 dpi->pixelclock = mode->clock * 1000;
460 mutex_unlock(&dpi->lock);
463 static int dpi_check_timings(struct omap_dss_device *dssdev,
464 struct drm_display_mode *mode)
466 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
467 int lck_div, pck_div;
470 struct dpi_clk_calc_ctx ctx;
473 if (mode->hdisplay % 8 != 0)
476 if (mode->clock == 0)
480 ok = dpi_pll_clk_calc(dpi, mode->clock * 1000, &ctx);
484 fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
486 ok = dpi_dss_clk_calc(dpi, mode->clock * 1000, &ctx);
493 lck_div = ctx.dispc_cinfo.lck_div;
494 pck_div = ctx.dispc_cinfo.pck_div;
496 pck = fck / lck_div / pck_div;
498 mode->clock = pck / 1000;
503 static int dpi_verify_pll(struct dss_pll *pll)
507 /* do initial setup with the PLL to see if it is operational */
509 r = dss_pll_enable(pll);
513 dss_pll_disable(pll);
518 static void dpi_init_pll(struct dpi_data *dpi)
525 dpi->clk_src = dpi_get_clk_src(dpi);
527 pll = dss_pll_find_by_src(dpi->dss, dpi->clk_src);
531 if (dpi_verify_pll(pll)) {
532 DSSWARN("PLL not operational\n");
540 * Return a hardcoded channel for the DPI output. This should work for
541 * current use cases, but this can be later expanded to either resolve
542 * the channel in some more dynamic manner, or get the channel as a user
545 static enum omap_channel dpi_get_channel(struct dpi_data *dpi)
547 switch (dpi->dss_model) {
548 case DSS_MODEL_OMAP2:
549 case DSS_MODEL_OMAP3:
550 return OMAP_DSS_CHANNEL_LCD;
555 return OMAP_DSS_CHANNEL_LCD3;
557 return OMAP_DSS_CHANNEL_LCD2;
560 return OMAP_DSS_CHANNEL_LCD;
563 case DSS_MODEL_OMAP4:
564 return OMAP_DSS_CHANNEL_LCD2;
566 case DSS_MODEL_OMAP5:
567 return OMAP_DSS_CHANNEL_LCD3;
570 DSSWARN("unsupported DSS version\n");
571 return OMAP_DSS_CHANNEL_LCD;
575 static int dpi_connect(struct omap_dss_device *src,
576 struct omap_dss_device *dst)
578 struct dpi_data *dpi = dpi_get_data_from_dssdev(dst);
582 return omapdss_device_connect(dst->dss, dst, dst->next);
585 static void dpi_disconnect(struct omap_dss_device *src,
586 struct omap_dss_device *dst)
588 omapdss_device_disconnect(dst, dst->next);
591 static const struct omap_dss_device_ops dpi_ops = {
592 .connect = dpi_connect,
593 .disconnect = dpi_disconnect,
595 .enable = dpi_display_enable,
596 .disable = dpi_display_disable,
598 .check_timings = dpi_check_timings,
599 .set_timings = dpi_set_timings,
602 static int dpi_init_output_port(struct dpi_data *dpi, struct device_node *port)
604 struct omap_dss_device *out = &dpi->output;
608 of_property_read_u32(port, "reg", &port_num);
609 dpi->id = port_num <= 2 ? port_num : 0;
624 out->dev = &dpi->pdev->dev;
625 out->id = OMAP_DSS_OUTPUT_DPI;
626 out->type = OMAP_DISPLAY_TYPE_DPI;
627 out->dispc_channel = dpi_get_channel(dpi);
628 out->of_ports = BIT(port_num);
630 out->owner = THIS_MODULE;
632 r = omapdss_device_init_output(out);
636 omapdss_device_register(out);
641 static void dpi_uninit_output_port(struct device_node *port)
643 struct dpi_data *dpi = port->data;
644 struct omap_dss_device *out = &dpi->output;
646 omapdss_device_unregister(out);
647 omapdss_device_cleanup_output(out);
650 static const struct soc_device_attribute dpi_soc_devices[] = {
651 { .machine = "OMAP3[456]*" },
652 { .machine = "[AD]M37*" },
656 static int dpi_init_regulator(struct dpi_data *dpi)
658 struct regulator *vdds_dsi;
661 * The DPI uses the DSI VDDS on OMAP34xx, OMAP35xx, OMAP36xx, AM37xx and
664 if (!soc_device_match(dpi_soc_devices))
667 vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
668 if (IS_ERR(vdds_dsi)) {
669 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
670 DSSERR("can't get VDDS_DSI regulator\n");
671 return PTR_ERR(vdds_dsi);
674 dpi->vdds_dsi_reg = vdds_dsi;
679 int dpi_init_port(struct dss_device *dss, struct platform_device *pdev,
680 struct device_node *port, enum dss_model dss_model)
682 struct dpi_data *dpi;
683 struct device_node *ep;
687 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
691 ep = of_get_next_child(port, NULL);
695 r = of_property_read_u32(ep, "data-lines", &datalines);
698 DSSERR("failed to parse datalines\n");
702 dpi->data_lines = datalines;
705 dpi->dss_model = dss_model;
709 mutex_init(&dpi->lock);
711 r = dpi_init_regulator(dpi);
715 return dpi_init_output_port(dpi, port);
718 void dpi_uninit_port(struct device_node *port)
720 struct dpi_data *dpi = port->data;
725 dpi_uninit_output_port(port);