2 * linux/drivers/video/omap2/dss/dpi.c
4 * Copyright (C) 2009 Nokia Corporation
7 * Some code and ideas taken from drivers/video/omap/ driver
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
23 #define DSS_SUBSYS_NAME "DPI"
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/export.h>
28 #include <linux/err.h>
29 #include <linux/errno.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/string.h>
34 #include <linux/clk.h>
35 #include <linux/component.h>
37 #include <video/omapdss.h>
40 #include "dss_features.h"
45 struct platform_device *pdev;
47 struct regulator *vdds_dsi_reg;
52 struct omap_video_timings timings;
53 struct dss_lcd_mgr_config mgr_config;
56 struct omap_dss_device output;
58 bool port_initialized;
61 static struct dpi_data *dpi_get_data_from_dssdev(struct omap_dss_device *dssdev)
63 return container_of(dssdev, struct dpi_data, output);
66 /* only used in non-DT mode */
67 static struct dpi_data *dpi_get_data_from_pdev(struct platform_device *pdev)
69 return dev_get_drvdata(&pdev->dev);
72 static struct dss_pll *dpi_get_pll(enum omap_channel channel)
75 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
76 * would also be used for DISPC fclk. Meaning, when the DPI output is
77 * disabled, DISPC clock will be disabled, and TV out will stop.
79 switch (omapdss_get_version()) {
80 case OMAPDSS_VER_OMAP24xx:
81 case OMAPDSS_VER_OMAP34xx_ES1:
82 case OMAPDSS_VER_OMAP34xx_ES3:
83 case OMAPDSS_VER_OMAP3630:
84 case OMAPDSS_VER_AM35xx:
85 case OMAPDSS_VER_AM43xx:
88 case OMAPDSS_VER_OMAP4430_ES1:
89 case OMAPDSS_VER_OMAP4430_ES2:
90 case OMAPDSS_VER_OMAP4:
92 case OMAP_DSS_CHANNEL_LCD:
93 return dss_pll_find("dsi0");
94 case OMAP_DSS_CHANNEL_LCD2:
95 return dss_pll_find("dsi1");
100 case OMAPDSS_VER_OMAP5:
102 case OMAP_DSS_CHANNEL_LCD:
103 return dss_pll_find("dsi0");
104 case OMAP_DSS_CHANNEL_LCD3:
105 return dss_pll_find("dsi1");
110 case OMAPDSS_VER_DRA7xx:
112 case OMAP_DSS_CHANNEL_LCD:
113 case OMAP_DSS_CHANNEL_LCD2:
114 return dss_pll_find("video0");
115 case OMAP_DSS_CHANNEL_LCD3:
116 return dss_pll_find("video1");
126 static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
129 case OMAP_DSS_CHANNEL_LCD:
130 return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC;
131 case OMAP_DSS_CHANNEL_LCD2:
132 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
133 case OMAP_DSS_CHANNEL_LCD3:
134 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
136 /* this shouldn't happen */
138 return OMAP_DSS_CLK_SRC_FCK;
142 struct dpi_clk_calc_ctx {
147 unsigned long pck_min, pck_max;
151 struct dss_pll_clock_info dsi_cinfo;
153 struct dispc_clock_info dispc_cinfo;
156 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
157 unsigned long pck, void *data)
159 struct dpi_clk_calc_ctx *ctx = data;
162 * Odd dividers give us uneven duty cycle, causing problem when level
163 * shifted. So skip all odd dividers when the pixel clock is on the
166 if (ctx->pck_min >= 100000000) {
167 if (lckd > 1 && lckd % 2 != 0)
170 if (pckd > 1 && pckd % 2 != 0)
174 ctx->dispc_cinfo.lck_div = lckd;
175 ctx->dispc_cinfo.pck_div = pckd;
176 ctx->dispc_cinfo.lck = lck;
177 ctx->dispc_cinfo.pck = pck;
183 static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
186 struct dpi_clk_calc_ctx *ctx = data;
189 * Odd dividers give us uneven duty cycle, causing problem when level
190 * shifted. So skip all odd dividers when the pixel clock is on the
193 if (m_dispc > 1 && m_dispc % 2 != 0 && ctx->pck_min >= 100000000)
196 ctx->dsi_cinfo.mX[HSDIV_DISPC] = m_dispc;
197 ctx->dsi_cinfo.clkout[HSDIV_DISPC] = dispc;
199 return dispc_div_calc(dispc, ctx->pck_min, ctx->pck_max,
200 dpi_calc_dispc_cb, ctx);
204 static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
205 unsigned long clkdco,
208 struct dpi_clk_calc_ctx *ctx = data;
210 ctx->dsi_cinfo.n = n;
211 ctx->dsi_cinfo.m = m;
212 ctx->dsi_cinfo.fint = fint;
213 ctx->dsi_cinfo.clkdco = clkdco;
215 return dss_pll_hsdiv_calc(ctx->pll, clkdco,
216 ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK),
217 dpi_calc_hsdiv_cb, ctx);
220 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
222 struct dpi_clk_calc_ctx *ctx = data;
226 return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
227 dpi_calc_dispc_cb, ctx);
230 static bool dpi_dsi_clk_calc(struct dpi_data *dpi, unsigned long pck,
231 struct dpi_clk_calc_ctx *ctx)
234 unsigned long pll_min, pll_max;
236 memset(ctx, 0, sizeof(*ctx));
238 ctx->pck_min = pck - 1000;
239 ctx->pck_max = pck + 1000;
244 clkin = clk_get_rate(ctx->pll->clkin);
246 return dss_pll_calc(ctx->pll, clkin,
248 dpi_calc_pll_cb, ctx);
251 static bool dpi_dss_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
256 * DSS fck gives us very few possibilities, so finding a good pixel
257 * clock may not be possible. We try multiple times to find the clock,
258 * each time widening the pixel clock range we look for, up to
262 for (i = 0; i < 25; ++i) {
265 memset(ctx, 0, sizeof(*ctx));
266 if (pck > 1000 * i * i * i)
267 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
270 ctx->pck_max = pck + 1000 * i * i * i;
272 ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
282 static int dpi_set_dsi_clk(struct dpi_data *dpi, enum omap_channel channel,
283 unsigned long pck_req, unsigned long *fck, int *lck_div,
286 struct dpi_clk_calc_ctx ctx;
290 ok = dpi_dsi_clk_calc(dpi, pck_req, &ctx);
294 r = dss_pll_set_config(dpi->pll, &ctx.dsi_cinfo);
298 dss_select_lcd_clk_source(channel,
299 dpi_get_alt_clk_src(channel));
301 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
303 *fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC];
304 *lck_div = ctx.dispc_cinfo.lck_div;
305 *pck_div = ctx.dispc_cinfo.pck_div;
310 static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req,
311 unsigned long *fck, int *lck_div, int *pck_div)
313 struct dpi_clk_calc_ctx ctx;
317 ok = dpi_dss_clk_calc(pck_req, &ctx);
321 r = dss_set_fck_rate(ctx.fck);
325 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
328 *lck_div = ctx.dispc_cinfo.lck_div;
329 *pck_div = ctx.dispc_cinfo.pck_div;
334 static int dpi_set_mode(struct dpi_data *dpi)
336 struct omap_dss_device *out = &dpi->output;
337 enum omap_channel channel = out->dispc_channel;
338 struct omap_video_timings *t = &dpi->timings;
339 int lck_div = 0, pck_div = 0;
340 unsigned long fck = 0;
345 r = dpi_set_dsi_clk(dpi, channel, t->pixelclock, &fck,
348 r = dpi_set_dispc_clk(dpi, t->pixelclock, &fck,
353 pck = fck / lck_div / pck_div;
355 if (pck != t->pixelclock) {
356 DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
362 dss_mgr_set_timings(channel, t);
367 static void dpi_config_lcd_manager(struct dpi_data *dpi)
369 struct omap_dss_device *out = &dpi->output;
370 enum omap_channel channel = out->dispc_channel;
372 dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
374 dpi->mgr_config.stallmode = false;
375 dpi->mgr_config.fifohandcheck = false;
377 dpi->mgr_config.video_port_width = dpi->data_lines;
379 dpi->mgr_config.lcden_sig_polarity = 0;
381 dss_mgr_set_lcd_config(channel, &dpi->mgr_config);
384 static int dpi_display_enable(struct omap_dss_device *dssdev)
386 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
387 struct omap_dss_device *out = &dpi->output;
388 enum omap_channel channel = out->dispc_channel;
391 mutex_lock(&dpi->lock);
393 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi->vdds_dsi_reg) {
394 DSSERR("no VDSS_DSI regulator\n");
399 if (!out->dispc_channel_connected) {
400 DSSERR("failed to enable display: no output/manager\n");
405 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
406 r = regulator_enable(dpi->vdds_dsi_reg);
411 r = dispc_runtime_get();
415 r = dss_dpi_select_source(out->port_num, channel);
420 r = dss_pll_enable(dpi->pll);
422 goto err_dsi_pll_init;
425 r = dpi_set_mode(dpi);
429 dpi_config_lcd_manager(dpi);
433 r = dss_mgr_enable(channel);
437 mutex_unlock(&dpi->lock);
444 dss_pll_disable(dpi->pll);
449 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
450 regulator_disable(dpi->vdds_dsi_reg);
454 mutex_unlock(&dpi->lock);
458 static void dpi_display_disable(struct omap_dss_device *dssdev)
460 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
461 enum omap_channel channel = dpi->output.dispc_channel;
463 mutex_lock(&dpi->lock);
465 dss_mgr_disable(channel);
468 dss_select_lcd_clk_source(channel, OMAP_DSS_CLK_SRC_FCK);
469 dss_pll_disable(dpi->pll);
474 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
475 regulator_disable(dpi->vdds_dsi_reg);
477 mutex_unlock(&dpi->lock);
480 static void dpi_set_timings(struct omap_dss_device *dssdev,
481 struct omap_video_timings *timings)
483 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
485 DSSDBG("dpi_set_timings\n");
487 mutex_lock(&dpi->lock);
489 dpi->timings = *timings;
491 mutex_unlock(&dpi->lock);
494 static void dpi_get_timings(struct omap_dss_device *dssdev,
495 struct omap_video_timings *timings)
497 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
499 mutex_lock(&dpi->lock);
501 *timings = dpi->timings;
503 mutex_unlock(&dpi->lock);
506 static int dpi_check_timings(struct omap_dss_device *dssdev,
507 struct omap_video_timings *timings)
509 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
510 enum omap_channel channel = dpi->output.dispc_channel;
511 int lck_div, pck_div;
514 struct dpi_clk_calc_ctx ctx;
517 if (timings->x_res % 8 != 0)
520 if (!dispc_mgr_timings_ok(channel, timings))
523 if (timings->pixelclock == 0)
527 ok = dpi_dsi_clk_calc(dpi, timings->pixelclock, &ctx);
531 fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC];
533 ok = dpi_dss_clk_calc(timings->pixelclock, &ctx);
540 lck_div = ctx.dispc_cinfo.lck_div;
541 pck_div = ctx.dispc_cinfo.pck_div;
543 pck = fck / lck_div / pck_div;
545 timings->pixelclock = pck;
550 static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
552 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
554 mutex_lock(&dpi->lock);
556 dpi->data_lines = data_lines;
558 mutex_unlock(&dpi->lock);
561 static int dpi_verify_dsi_pll(struct dss_pll *pll)
565 /* do initial setup with the PLL to see if it is operational */
567 r = dss_pll_enable(pll);
571 dss_pll_disable(pll);
576 static int dpi_init_regulator(struct dpi_data *dpi)
578 struct regulator *vdds_dsi;
580 if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
583 if (dpi->vdds_dsi_reg)
586 vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
587 if (IS_ERR(vdds_dsi)) {
588 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
589 DSSERR("can't get VDDS_DSI regulator\n");
590 return PTR_ERR(vdds_dsi);
593 dpi->vdds_dsi_reg = vdds_dsi;
598 static void dpi_init_pll(struct dpi_data *dpi)
605 pll = dpi_get_pll(dpi->output.dispc_channel);
609 /* On DRA7 we need to set a mux to use the PLL */
610 if (omapdss_get_version() == OMAPDSS_VER_DRA7xx)
611 dss_ctrl_pll_set_control_mux(pll->id, dpi->output.dispc_channel);
613 if (dpi_verify_dsi_pll(pll)) {
614 DSSWARN("DSI PLL not operational\n");
622 * Return a hardcoded channel for the DPI output. This should work for
623 * current use cases, but this can be later expanded to either resolve
624 * the channel in some more dynamic manner, or get the channel as a user
627 static enum omap_channel dpi_get_channel(int port_num)
629 switch (omapdss_get_version()) {
630 case OMAPDSS_VER_OMAP24xx:
631 case OMAPDSS_VER_OMAP34xx_ES1:
632 case OMAPDSS_VER_OMAP34xx_ES3:
633 case OMAPDSS_VER_OMAP3630:
634 case OMAPDSS_VER_AM35xx:
635 case OMAPDSS_VER_AM43xx:
636 return OMAP_DSS_CHANNEL_LCD;
638 case OMAPDSS_VER_DRA7xx:
641 return OMAP_DSS_CHANNEL_LCD3;
643 return OMAP_DSS_CHANNEL_LCD2;
646 return OMAP_DSS_CHANNEL_LCD;
649 case OMAPDSS_VER_OMAP4430_ES1:
650 case OMAPDSS_VER_OMAP4430_ES2:
651 case OMAPDSS_VER_OMAP4:
652 return OMAP_DSS_CHANNEL_LCD2;
654 case OMAPDSS_VER_OMAP5:
655 return OMAP_DSS_CHANNEL_LCD3;
658 DSSWARN("unsupported DSS version\n");
659 return OMAP_DSS_CHANNEL_LCD;
663 static int dpi_connect(struct omap_dss_device *dssdev,
664 struct omap_dss_device *dst)
666 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
667 enum omap_channel channel = dpi->output.dispc_channel;
670 r = dpi_init_regulator(dpi);
676 r = dss_mgr_connect(channel, dssdev);
680 r = omapdss_output_set_device(dssdev, dst);
682 DSSERR("failed to connect output to new device: %s\n",
684 dss_mgr_disconnect(channel, dssdev);
691 static void dpi_disconnect(struct omap_dss_device *dssdev,
692 struct omap_dss_device *dst)
694 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
695 enum omap_channel channel = dpi->output.dispc_channel;
697 WARN_ON(dst != dssdev->dst);
699 if (dst != dssdev->dst)
702 omapdss_output_unset_device(dssdev);
704 dss_mgr_disconnect(channel, dssdev);
707 static const struct omapdss_dpi_ops dpi_ops = {
708 .connect = dpi_connect,
709 .disconnect = dpi_disconnect,
711 .enable = dpi_display_enable,
712 .disable = dpi_display_disable,
714 .check_timings = dpi_check_timings,
715 .set_timings = dpi_set_timings,
716 .get_timings = dpi_get_timings,
718 .set_data_lines = dpi_set_data_lines,
721 static void dpi_init_output(struct platform_device *pdev)
723 struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
724 struct omap_dss_device *out = &dpi->output;
726 out->dev = &pdev->dev;
727 out->id = OMAP_DSS_OUTPUT_DPI;
728 out->output_type = OMAP_DISPLAY_TYPE_DPI;
730 out->dispc_channel = dpi_get_channel(0);
731 out->ops.dpi = &dpi_ops;
732 out->owner = THIS_MODULE;
734 omapdss_register_output(out);
737 static void dpi_uninit_output(struct platform_device *pdev)
739 struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
740 struct omap_dss_device *out = &dpi->output;
742 omapdss_unregister_output(out);
745 static void dpi_init_output_port(struct platform_device *pdev,
746 struct device_node *port)
748 struct dpi_data *dpi = port->data;
749 struct omap_dss_device *out = &dpi->output;
753 r = of_property_read_u32(port, "reg", &port_num);
770 out->dev = &pdev->dev;
771 out->id = OMAP_DSS_OUTPUT_DPI;
772 out->output_type = OMAP_DISPLAY_TYPE_DPI;
773 out->dispc_channel = dpi_get_channel(port_num);
774 out->port_num = port_num;
775 out->ops.dpi = &dpi_ops;
776 out->owner = THIS_MODULE;
778 omapdss_register_output(out);
781 static void dpi_uninit_output_port(struct device_node *port)
783 struct dpi_data *dpi = port->data;
784 struct omap_dss_device *out = &dpi->output;
786 omapdss_unregister_output(out);
789 static int dpi_bind(struct device *dev, struct device *master, void *data)
791 struct platform_device *pdev = to_platform_device(dev);
792 struct dpi_data *dpi;
794 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
800 dev_set_drvdata(&pdev->dev, dpi);
802 mutex_init(&dpi->lock);
804 dpi_init_output(pdev);
809 static void dpi_unbind(struct device *dev, struct device *master, void *data)
811 struct platform_device *pdev = to_platform_device(dev);
813 dpi_uninit_output(pdev);
816 static const struct component_ops dpi_component_ops = {
818 .unbind = dpi_unbind,
821 static int dpi_probe(struct platform_device *pdev)
823 return component_add(&pdev->dev, &dpi_component_ops);
826 static int dpi_remove(struct platform_device *pdev)
828 component_del(&pdev->dev, &dpi_component_ops);
832 static struct platform_driver omap_dpi_driver = {
834 .remove = dpi_remove,
836 .name = "omapdss_dpi",
837 .suppress_bind_attrs = true,
841 int __init dpi_init_platform_driver(void)
843 return platform_driver_register(&omap_dpi_driver);
846 void dpi_uninit_platform_driver(void)
848 platform_driver_unregister(&omap_dpi_driver);
851 int dpi_init_port(struct platform_device *pdev, struct device_node *port)
853 struct dpi_data *dpi;
854 struct device_node *ep;
858 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
862 ep = omapdss_of_get_next_endpoint(port, NULL);
866 r = of_property_read_u32(ep, "data-lines", &datalines);
868 DSSERR("failed to parse datalines\n");
872 dpi->data_lines = datalines;
879 mutex_init(&dpi->lock);
881 dpi_init_output_port(pdev, port);
883 dpi->port_initialized = true;
893 void dpi_uninit_port(struct device_node *port)
895 struct dpi_data *dpi = port->data;
897 if (!dpi->port_initialized)
900 dpi_uninit_output_port(port);