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/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/string.h>
22 #include <linux/sys_soc.h>
24 #include <drm/drm_bridge.h>
30 struct platform_device *pdev;
31 enum dss_model dss_model;
32 struct dss_device *dss;
35 struct regulator *vdds_dsi_reg;
36 enum dss_clk_source clk_src;
39 struct dss_lcd_mgr_config mgr_config;
40 unsigned long pixelclock;
43 struct omap_dss_device output;
44 struct drm_bridge bridge;
47 #define drm_bridge_to_dpi(bridge) container_of(bridge, struct dpi_data, bridge)
49 /* -----------------------------------------------------------------------------
50 * Clock Handling and PLL
53 static enum dss_clk_source dpi_get_clk_src_dra7xx(struct dpi_data *dpi,
54 enum omap_channel channel)
57 * Possible clock sources:
58 * LCD1: FCK/PLL1_1/HDMI_PLL
59 * LCD2: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_3)
60 * LCD3: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_1)
64 case OMAP_DSS_CHANNEL_LCD:
66 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_1))
67 return DSS_CLK_SRC_PLL1_1;
70 case OMAP_DSS_CHANNEL_LCD2:
72 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
73 return DSS_CLK_SRC_PLL1_3;
74 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_3))
75 return DSS_CLK_SRC_PLL2_3;
78 case OMAP_DSS_CHANNEL_LCD3:
80 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_1))
81 return DSS_CLK_SRC_PLL2_1;
82 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
83 return DSS_CLK_SRC_PLL1_3;
90 return DSS_CLK_SRC_FCK;
93 static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
95 enum omap_channel channel = dpi->output.dispc_channel;
98 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
99 * would also be used for DISPC fclk. Meaning, when the DPI output is
100 * disabled, DISPC clock will be disabled, and TV out will stop.
102 switch (dpi->dss_model) {
103 case DSS_MODEL_OMAP2:
104 case DSS_MODEL_OMAP3:
105 return DSS_CLK_SRC_FCK;
107 case DSS_MODEL_OMAP4:
109 case OMAP_DSS_CHANNEL_LCD:
110 return DSS_CLK_SRC_PLL1_1;
111 case OMAP_DSS_CHANNEL_LCD2:
112 return DSS_CLK_SRC_PLL2_1;
114 return DSS_CLK_SRC_FCK;
117 case DSS_MODEL_OMAP5:
119 case OMAP_DSS_CHANNEL_LCD:
120 return DSS_CLK_SRC_PLL1_1;
121 case OMAP_DSS_CHANNEL_LCD3:
122 return DSS_CLK_SRC_PLL2_1;
123 case OMAP_DSS_CHANNEL_LCD2:
125 return DSS_CLK_SRC_FCK;
129 return dpi_get_clk_src_dra7xx(dpi, channel);
132 return DSS_CLK_SRC_FCK;
136 struct dpi_clk_calc_ctx {
137 struct dpi_data *dpi;
138 unsigned int clkout_idx;
142 unsigned long pck_min, pck_max;
146 struct dss_pll_clock_info pll_cinfo;
148 struct dispc_clock_info dispc_cinfo;
151 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
152 unsigned long pck, void *data)
154 struct dpi_clk_calc_ctx *ctx = data;
157 * Odd dividers give us uneven duty cycle, causing problem when level
158 * shifted. So skip all odd dividers when the pixel clock is on the
161 if (ctx->pck_min >= 100000000) {
162 if (lckd > 1 && lckd % 2 != 0)
165 if (pckd > 1 && pckd % 2 != 0)
169 ctx->dispc_cinfo.lck_div = lckd;
170 ctx->dispc_cinfo.pck_div = pckd;
171 ctx->dispc_cinfo.lck = lck;
172 ctx->dispc_cinfo.pck = pck;
178 static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
181 struct dpi_clk_calc_ctx *ctx = data;
183 ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
184 ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
186 return dispc_div_calc(ctx->dpi->dss->dispc, dispc,
187 ctx->pck_min, ctx->pck_max,
188 dpi_calc_dispc_cb, ctx);
192 static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
193 unsigned long clkdco,
196 struct dpi_clk_calc_ctx *ctx = data;
198 ctx->pll_cinfo.n = n;
199 ctx->pll_cinfo.m = m;
200 ctx->pll_cinfo.fint = fint;
201 ctx->pll_cinfo.clkdco = clkdco;
203 return dss_pll_hsdiv_calc_a(ctx->dpi->pll, clkdco,
204 ctx->pck_min, dss_get_max_fck_rate(ctx->dpi->dss),
205 dpi_calc_hsdiv_cb, ctx);
208 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
210 struct dpi_clk_calc_ctx *ctx = data;
214 return dispc_div_calc(ctx->dpi->dss->dispc, fck,
215 ctx->pck_min, ctx->pck_max,
216 dpi_calc_dispc_cb, ctx);
219 static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
220 struct dpi_clk_calc_ctx *ctx)
224 memset(ctx, 0, sizeof(*ctx));
226 ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
228 clkin = clk_get_rate(dpi->pll->clkin);
230 if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
231 unsigned long pll_min, pll_max;
233 ctx->pck_min = pck - 1000;
234 ctx->pck_max = pck + 1000;
239 return dss_pll_calc_a(ctx->dpi->pll, clkin,
241 dpi_calc_pll_cb, ctx);
242 } else { /* DSS_PLL_TYPE_B */
243 dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
245 ctx->dispc_cinfo.lck_div = 1;
246 ctx->dispc_cinfo.pck_div = 1;
247 ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
248 ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;
254 static bool dpi_dss_clk_calc(struct dpi_data *dpi, unsigned long pck,
255 struct dpi_clk_calc_ctx *ctx)
260 * DSS fck gives us very few possibilities, so finding a good pixel
261 * clock may not be possible. We try multiple times to find the clock,
262 * each time widening the pixel clock range we look for, up to
266 for (i = 0; i < 25; ++i) {
269 memset(ctx, 0, sizeof(*ctx));
271 if (pck > 1000 * i * i * i)
272 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
275 ctx->pck_max = pck + 1000 * i * i * i;
277 ok = dss_div_calc(dpi->dss, pck, ctx->pck_min,
278 dpi_calc_dss_cb, ctx);
288 static int dpi_set_pll_clk(struct dpi_data *dpi, unsigned long pck_req)
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, dpi->output.dispc_channel,
305 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
310 static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req)
312 struct dpi_clk_calc_ctx ctx;
316 ok = dpi_dss_clk_calc(dpi, pck_req, &ctx);
320 r = dss_set_fck_rate(dpi->dss, ctx.fck);
324 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
329 static int dpi_set_mode(struct dpi_data *dpi)
334 r = dpi_set_pll_clk(dpi, dpi->pixelclock);
336 r = dpi_set_dispc_clk(dpi, dpi->pixelclock);
341 static void dpi_config_lcd_manager(struct dpi_data *dpi)
343 dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
345 dpi->mgr_config.stallmode = false;
346 dpi->mgr_config.fifohandcheck = false;
348 dpi->mgr_config.video_port_width = dpi->data_lines;
350 dpi->mgr_config.lcden_sig_polarity = 0;
352 dss_mgr_set_lcd_config(&dpi->output, &dpi->mgr_config);
355 static int dpi_clock_update(struct dpi_data *dpi, unsigned long *clock)
357 int lck_div, pck_div;
359 struct dpi_clk_calc_ctx ctx;
362 if (!dpi_pll_clk_calc(dpi, *clock, &ctx))
365 fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
367 if (!dpi_dss_clk_calc(dpi, *clock, &ctx))
373 lck_div = ctx.dispc_cinfo.lck_div;
374 pck_div = ctx.dispc_cinfo.pck_div;
376 *clock = fck / lck_div / pck_div;
381 static int dpi_verify_pll(struct dss_pll *pll)
385 /* do initial setup with the PLL to see if it is operational */
387 r = dss_pll_enable(pll);
391 dss_pll_disable(pll);
396 static void dpi_init_pll(struct dpi_data *dpi)
403 dpi->clk_src = dpi_get_clk_src(dpi);
405 pll = dss_pll_find_by_src(dpi->dss, dpi->clk_src);
409 if (dpi_verify_pll(pll)) {
410 DSSWARN("PLL not operational\n");
417 /* -----------------------------------------------------------------------------
418 * DRM Bridge Operations
421 static int dpi_bridge_attach(struct drm_bridge *bridge,
422 enum drm_bridge_attach_flags flags)
424 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
426 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
431 return drm_bridge_attach(bridge->encoder, dpi->output.next_bridge,
435 static enum drm_mode_status
436 dpi_bridge_mode_valid(struct drm_bridge *bridge,
437 const struct drm_display_info *info,
438 const struct drm_display_mode *mode)
440 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
441 unsigned long clock = mode->clock * 1000;
444 if (mode->hdisplay % 8 != 0)
445 return MODE_BAD_WIDTH;
447 if (mode->clock == 0)
450 ret = dpi_clock_update(dpi, &clock);
452 return MODE_CLOCK_RANGE;
457 static bool dpi_bridge_mode_fixup(struct drm_bridge *bridge,
458 const struct drm_display_mode *mode,
459 struct drm_display_mode *adjusted_mode)
461 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
462 unsigned long clock = mode->clock * 1000;
465 ret = dpi_clock_update(dpi, &clock);
469 adjusted_mode->clock = clock / 1000;
474 static void dpi_bridge_mode_set(struct drm_bridge *bridge,
475 const struct drm_display_mode *mode,
476 const struct drm_display_mode *adjusted_mode)
478 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
480 dpi->pixelclock = adjusted_mode->clock * 1000;
483 static void dpi_bridge_enable(struct drm_bridge *bridge)
485 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
488 if (dpi->vdds_dsi_reg) {
489 r = regulator_enable(dpi->vdds_dsi_reg);
494 r = dispc_runtime_get(dpi->dss->dispc);
498 r = dss_dpi_select_source(dpi->dss, dpi->id, dpi->output.dispc_channel);
503 r = dss_pll_enable(dpi->pll);
508 r = dpi_set_mode(dpi);
512 dpi_config_lcd_manager(dpi);
516 r = dss_mgr_enable(&dpi->output);
525 dss_pll_disable(dpi->pll);
528 dispc_runtime_put(dpi->dss->dispc);
530 if (dpi->vdds_dsi_reg)
531 regulator_disable(dpi->vdds_dsi_reg);
534 static void dpi_bridge_disable(struct drm_bridge *bridge)
536 struct dpi_data *dpi = drm_bridge_to_dpi(bridge);
538 dss_mgr_disable(&dpi->output);
541 dss_select_lcd_clk_source(dpi->dss, dpi->output.dispc_channel,
543 dss_pll_disable(dpi->pll);
546 dispc_runtime_put(dpi->dss->dispc);
548 if (dpi->vdds_dsi_reg)
549 regulator_disable(dpi->vdds_dsi_reg);
552 static const struct drm_bridge_funcs dpi_bridge_funcs = {
553 .attach = dpi_bridge_attach,
554 .mode_valid = dpi_bridge_mode_valid,
555 .mode_fixup = dpi_bridge_mode_fixup,
556 .mode_set = dpi_bridge_mode_set,
557 .enable = dpi_bridge_enable,
558 .disable = dpi_bridge_disable,
561 static void dpi_bridge_init(struct dpi_data *dpi)
563 dpi->bridge.funcs = &dpi_bridge_funcs;
564 dpi->bridge.of_node = dpi->pdev->dev.of_node;
565 dpi->bridge.type = DRM_MODE_CONNECTOR_DPI;
567 drm_bridge_add(&dpi->bridge);
570 static void dpi_bridge_cleanup(struct dpi_data *dpi)
572 drm_bridge_remove(&dpi->bridge);
575 /* -----------------------------------------------------------------------------
576 * Initialisation and Cleanup
580 * Return a hardcoded channel for the DPI output. This should work for
581 * current use cases, but this can be later expanded to either resolve
582 * the channel in some more dynamic manner, or get the channel as a user
585 static enum omap_channel dpi_get_channel(struct dpi_data *dpi)
587 switch (dpi->dss_model) {
588 case DSS_MODEL_OMAP2:
589 case DSS_MODEL_OMAP3:
590 return OMAP_DSS_CHANNEL_LCD;
595 return OMAP_DSS_CHANNEL_LCD3;
597 return OMAP_DSS_CHANNEL_LCD2;
600 return OMAP_DSS_CHANNEL_LCD;
603 case DSS_MODEL_OMAP4:
604 return OMAP_DSS_CHANNEL_LCD2;
606 case DSS_MODEL_OMAP5:
607 return OMAP_DSS_CHANNEL_LCD3;
610 DSSWARN("unsupported DSS version\n");
611 return OMAP_DSS_CHANNEL_LCD;
615 static int dpi_init_output_port(struct dpi_data *dpi, struct device_node *port)
617 struct omap_dss_device *out = &dpi->output;
621 dpi_bridge_init(dpi);
623 of_property_read_u32(port, "reg", &port_num);
624 dpi->id = port_num <= 2 ? port_num : 0;
639 out->dev = &dpi->pdev->dev;
640 out->id = OMAP_DSS_OUTPUT_DPI;
641 out->type = OMAP_DISPLAY_TYPE_DPI;
642 out->dispc_channel = dpi_get_channel(dpi);
643 out->of_port = port_num;
645 r = omapdss_device_init_output(out, &dpi->bridge);
647 dpi_bridge_cleanup(dpi);
651 omapdss_device_register(out);
656 static void dpi_uninit_output_port(struct device_node *port)
658 struct dpi_data *dpi = port->data;
659 struct omap_dss_device *out = &dpi->output;
661 omapdss_device_unregister(out);
662 omapdss_device_cleanup_output(out);
664 dpi_bridge_cleanup(dpi);
667 /* -----------------------------------------------------------------------------
668 * Initialisation and Cleanup
671 static const struct soc_device_attribute dpi_soc_devices[] = {
672 { .machine = "OMAP3[456]*" },
673 { .machine = "[AD]M37*" },
677 static int dpi_init_regulator(struct dpi_data *dpi)
679 struct regulator *vdds_dsi;
682 * The DPI uses the DSI VDDS on OMAP34xx, OMAP35xx, OMAP36xx, AM37xx and
685 if (!soc_device_match(dpi_soc_devices))
688 vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
689 if (IS_ERR(vdds_dsi)) {
690 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
691 DSSERR("can't get VDDS_DSI regulator\n");
692 return PTR_ERR(vdds_dsi);
695 dpi->vdds_dsi_reg = vdds_dsi;
700 int dpi_init_port(struct dss_device *dss, struct platform_device *pdev,
701 struct device_node *port, enum dss_model dss_model)
703 struct dpi_data *dpi;
704 struct device_node *ep;
708 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
712 ep = of_get_next_child(port, NULL);
716 r = of_property_read_u32(ep, "data-lines", &datalines);
719 DSSERR("failed to parse datalines\n");
723 dpi->data_lines = datalines;
726 dpi->dss_model = dss_model;
730 r = dpi_init_regulator(dpi);
734 return dpi_init_output_port(dpi, port);
737 void dpi_uninit_port(struct device_node *port)
739 struct dpi_data *dpi = port->data;
744 dpi_uninit_output_port(port);