2 * Copyright (C) 2009 Nokia Corporation
3 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
5 * Some code and ideas taken from drivers/video/omap/ driver
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #define DSS_SUBSYS_NAME "DPI"
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/export.h>
26 #include <linux/err.h>
27 #include <linux/errno.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/string.h>
32 #include <linux/clk.h>
33 #include <linux/sys_soc.h>
39 struct platform_device *pdev;
40 enum dss_model dss_model;
41 struct dss_device *dss;
43 struct regulator *vdds_dsi_reg;
44 enum dss_clk_source clk_src;
50 struct dss_lcd_mgr_config mgr_config;
53 struct omap_dss_device output;
56 static struct dpi_data *dpi_get_data_from_dssdev(struct omap_dss_device *dssdev)
58 return container_of(dssdev, struct dpi_data, output);
61 static enum dss_clk_source dpi_get_clk_src_dra7xx(struct dpi_data *dpi,
62 enum omap_channel channel)
65 * Possible clock sources:
66 * LCD1: FCK/PLL1_1/HDMI_PLL
67 * LCD2: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_3)
68 * LCD3: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_1)
72 case OMAP_DSS_CHANNEL_LCD:
74 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_1))
75 return DSS_CLK_SRC_PLL1_1;
78 case OMAP_DSS_CHANNEL_LCD2:
80 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
81 return DSS_CLK_SRC_PLL1_3;
82 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_3))
83 return DSS_CLK_SRC_PLL2_3;
86 case OMAP_DSS_CHANNEL_LCD3:
88 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_1))
89 return DSS_CLK_SRC_PLL2_1;
90 if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
91 return DSS_CLK_SRC_PLL1_3;
98 return DSS_CLK_SRC_FCK;
101 static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
103 enum omap_channel channel = dpi->output.dispc_channel;
106 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
107 * would also be used for DISPC fclk. Meaning, when the DPI output is
108 * disabled, DISPC clock will be disabled, and TV out will stop.
110 switch (dpi->dss_model) {
111 case DSS_MODEL_OMAP2:
112 case DSS_MODEL_OMAP3:
113 return DSS_CLK_SRC_FCK;
115 case DSS_MODEL_OMAP4:
117 case OMAP_DSS_CHANNEL_LCD:
118 return DSS_CLK_SRC_PLL1_1;
119 case OMAP_DSS_CHANNEL_LCD2:
120 return DSS_CLK_SRC_PLL2_1;
122 return DSS_CLK_SRC_FCK;
125 case DSS_MODEL_OMAP5:
127 case OMAP_DSS_CHANNEL_LCD:
128 return DSS_CLK_SRC_PLL1_1;
129 case OMAP_DSS_CHANNEL_LCD3:
130 return DSS_CLK_SRC_PLL2_1;
131 case OMAP_DSS_CHANNEL_LCD2:
133 return DSS_CLK_SRC_FCK;
137 return dpi_get_clk_src_dra7xx(dpi, channel);
140 return DSS_CLK_SRC_FCK;
144 struct dpi_clk_calc_ctx {
145 struct dpi_data *dpi;
146 unsigned int clkout_idx;
150 unsigned long pck_min, pck_max;
154 struct dss_pll_clock_info pll_cinfo;
156 struct dispc_clock_info dispc_cinfo;
159 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
160 unsigned long pck, void *data)
162 struct dpi_clk_calc_ctx *ctx = data;
165 * Odd dividers give us uneven duty cycle, causing problem when level
166 * shifted. So skip all odd dividers when the pixel clock is on the
169 if (ctx->pck_min >= 100000000) {
170 if (lckd > 1 && lckd % 2 != 0)
173 if (pckd > 1 && pckd % 2 != 0)
177 ctx->dispc_cinfo.lck_div = lckd;
178 ctx->dispc_cinfo.pck_div = pckd;
179 ctx->dispc_cinfo.lck = lck;
180 ctx->dispc_cinfo.pck = pck;
186 static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
189 struct dpi_clk_calc_ctx *ctx = data;
191 ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
192 ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
194 return dispc_div_calc(ctx->dpi->dss->dispc, dispc,
195 ctx->pck_min, ctx->pck_max,
196 dpi_calc_dispc_cb, ctx);
200 static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
201 unsigned long clkdco,
204 struct dpi_clk_calc_ctx *ctx = data;
206 ctx->pll_cinfo.n = n;
207 ctx->pll_cinfo.m = m;
208 ctx->pll_cinfo.fint = fint;
209 ctx->pll_cinfo.clkdco = clkdco;
211 return dss_pll_hsdiv_calc_a(ctx->dpi->pll, clkdco,
212 ctx->pck_min, dss_get_max_fck_rate(ctx->dpi->dss),
213 dpi_calc_hsdiv_cb, ctx);
216 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
218 struct dpi_clk_calc_ctx *ctx = data;
222 return dispc_div_calc(ctx->dpi->dss->dispc, fck,
223 ctx->pck_min, ctx->pck_max,
224 dpi_calc_dispc_cb, ctx);
227 static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
228 struct dpi_clk_calc_ctx *ctx)
232 memset(ctx, 0, sizeof(*ctx));
234 ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
236 clkin = clk_get_rate(dpi->pll->clkin);
238 if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
239 unsigned long pll_min, pll_max;
241 ctx->pck_min = pck - 1000;
242 ctx->pck_max = pck + 1000;
247 return dss_pll_calc_a(ctx->dpi->pll, clkin,
249 dpi_calc_pll_cb, ctx);
250 } else { /* DSS_PLL_TYPE_B */
251 dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
253 ctx->dispc_cinfo.lck_div = 1;
254 ctx->dispc_cinfo.pck_div = 1;
255 ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
256 ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;
262 static bool dpi_dss_clk_calc(struct dpi_data *dpi, unsigned long pck,
263 struct dpi_clk_calc_ctx *ctx)
268 * DSS fck gives us very few possibilities, so finding a good pixel
269 * clock may not be possible. We try multiple times to find the clock,
270 * each time widening the pixel clock range we look for, up to
274 for (i = 0; i < 25; ++i) {
277 memset(ctx, 0, sizeof(*ctx));
279 if (pck > 1000 * i * i * i)
280 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
283 ctx->pck_max = pck + 1000 * i * i * i;
285 ok = dss_div_calc(dpi->dss, pck, ctx->pck_min,
286 dpi_calc_dss_cb, ctx);
296 static int dpi_set_pll_clk(struct dpi_data *dpi, enum omap_channel channel,
297 unsigned long pck_req, unsigned long *fck, int *lck_div,
300 struct dpi_clk_calc_ctx ctx;
304 ok = dpi_pll_clk_calc(dpi, pck_req, &ctx);
308 r = dss_pll_set_config(dpi->pll, &ctx.pll_cinfo);
312 dss_select_lcd_clk_source(dpi->dss, channel, dpi->clk_src);
314 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
316 *fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
317 *lck_div = ctx.dispc_cinfo.lck_div;
318 *pck_div = ctx.dispc_cinfo.pck_div;
323 static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req,
324 unsigned long *fck, int *lck_div, int *pck_div)
326 struct dpi_clk_calc_ctx ctx;
330 ok = dpi_dss_clk_calc(dpi, pck_req, &ctx);
334 r = dss_set_fck_rate(dpi->dss, ctx.fck);
338 dpi->mgr_config.clock_info = ctx.dispc_cinfo;
341 *lck_div = ctx.dispc_cinfo.lck_div;
342 *pck_div = ctx.dispc_cinfo.pck_div;
347 static int dpi_set_mode(struct dpi_data *dpi)
349 struct videomode *vm = &dpi->vm;
350 int lck_div = 0, pck_div = 0;
351 unsigned long fck = 0;
356 r = dpi_set_pll_clk(dpi, dpi->output.dispc_channel,
357 vm->pixelclock, &fck, &lck_div, &pck_div);
359 r = dpi_set_dispc_clk(dpi, vm->pixelclock, &fck,
364 pck = fck / lck_div / pck_div;
366 if (pck != vm->pixelclock) {
367 DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
368 vm->pixelclock, pck);
370 vm->pixelclock = pck;
373 dss_mgr_set_timings(&dpi->output, vm);
378 static void dpi_config_lcd_manager(struct dpi_data *dpi)
380 dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
382 dpi->mgr_config.stallmode = false;
383 dpi->mgr_config.fifohandcheck = false;
385 dpi->mgr_config.video_port_width = dpi->data_lines;
387 dpi->mgr_config.lcden_sig_polarity = 0;
389 dss_mgr_set_lcd_config(&dpi->output, &dpi->mgr_config);
392 static int dpi_display_enable(struct omap_dss_device *dssdev)
394 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
395 struct omap_dss_device *out = &dpi->output;
398 mutex_lock(&dpi->lock);
400 if (!out->dispc_channel_connected) {
401 DSSERR("failed to enable display: no output/manager\n");
406 if (dpi->vdds_dsi_reg) {
407 r = regulator_enable(dpi->vdds_dsi_reg);
412 r = dispc_runtime_get(dpi->dss->dispc);
416 r = dss_dpi_select_source(dpi->dss, out->port_num, out->dispc_channel);
421 r = dss_pll_enable(dpi->pll);
426 r = dpi_set_mode(dpi);
430 dpi_config_lcd_manager(dpi);
434 r = dss_mgr_enable(&dpi->output);
438 mutex_unlock(&dpi->lock);
445 dss_pll_disable(dpi->pll);
448 dispc_runtime_put(dpi->dss->dispc);
450 if (dpi->vdds_dsi_reg)
451 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);
462 mutex_lock(&dpi->lock);
464 dss_mgr_disable(&dpi->output);
467 dss_select_lcd_clk_source(dpi->dss, dpi->output.dispc_channel,
469 dss_pll_disable(dpi->pll);
472 dispc_runtime_put(dpi->dss->dispc);
474 if (dpi->vdds_dsi_reg)
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 videomode *vm)
483 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
485 DSSDBG("dpi_set_timings\n");
487 mutex_lock(&dpi->lock);
491 mutex_unlock(&dpi->lock);
494 static void dpi_get_timings(struct omap_dss_device *dssdev,
495 struct videomode *vm)
497 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
499 mutex_lock(&dpi->lock);
503 mutex_unlock(&dpi->lock);
506 static int dpi_check_timings(struct omap_dss_device *dssdev,
507 struct videomode *vm)
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 (vm->hactive % 8 != 0)
520 if (!dispc_mgr_timings_ok(dpi->dss->dispc, channel, vm))
523 if (vm->pixelclock == 0)
527 ok = dpi_pll_clk_calc(dpi, vm->pixelclock, &ctx);
531 fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
533 ok = dpi_dss_clk_calc(dpi, vm->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 vm->pixelclock = pck;
550 static int dpi_verify_pll(struct dss_pll *pll)
554 /* do initial setup with the PLL to see if it is operational */
556 r = dss_pll_enable(pll);
560 dss_pll_disable(pll);
565 static const struct soc_device_attribute dpi_soc_devices[] = {
566 { .machine = "OMAP3[456]*" },
567 { .machine = "[AD]M37*" },
571 static int dpi_init_regulator(struct dpi_data *dpi)
573 struct regulator *vdds_dsi;
576 * The DPI uses the DSI VDDS on OMAP34xx, OMAP35xx, OMAP36xx, AM37xx and
579 if (!soc_device_match(dpi_soc_devices))
582 if (dpi->vdds_dsi_reg)
585 vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
586 if (IS_ERR(vdds_dsi)) {
587 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
588 DSSERR("can't get VDDS_DSI regulator\n");
589 return PTR_ERR(vdds_dsi);
592 dpi->vdds_dsi_reg = vdds_dsi;
597 static void dpi_init_pll(struct dpi_data *dpi)
604 dpi->clk_src = dpi_get_clk_src(dpi);
606 pll = dss_pll_find_by_src(dpi->dss, dpi->clk_src);
610 if (dpi_verify_pll(pll)) {
611 DSSWARN("PLL not operational\n");
619 * Return a hardcoded channel for the DPI output. This should work for
620 * current use cases, but this can be later expanded to either resolve
621 * the channel in some more dynamic manner, or get the channel as a user
624 static enum omap_channel dpi_get_channel(struct dpi_data *dpi, int port_num)
626 switch (dpi->dss_model) {
627 case DSS_MODEL_OMAP2:
628 case DSS_MODEL_OMAP3:
629 return OMAP_DSS_CHANNEL_LCD;
634 return OMAP_DSS_CHANNEL_LCD3;
636 return OMAP_DSS_CHANNEL_LCD2;
639 return OMAP_DSS_CHANNEL_LCD;
642 case DSS_MODEL_OMAP4:
643 return OMAP_DSS_CHANNEL_LCD2;
645 case DSS_MODEL_OMAP5:
646 return OMAP_DSS_CHANNEL_LCD3;
649 DSSWARN("unsupported DSS version\n");
650 return OMAP_DSS_CHANNEL_LCD;
654 static int dpi_connect(struct omap_dss_device *dssdev,
655 struct omap_dss_device *dst)
657 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
660 r = dpi_init_regulator(dpi);
666 r = dss_mgr_connect(&dpi->output, dssdev);
670 r = omapdss_output_set_device(dssdev, dst);
672 DSSERR("failed to connect output to new device: %s\n",
674 dss_mgr_disconnect(&dpi->output, dssdev);
681 static void dpi_disconnect(struct omap_dss_device *dssdev,
682 struct omap_dss_device *dst)
684 struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
686 WARN_ON(dst != dssdev->dst);
688 if (dst != dssdev->dst)
691 omapdss_output_unset_device(dssdev);
693 dss_mgr_disconnect(&dpi->output, dssdev);
696 static const struct omapdss_dpi_ops dpi_ops = {
697 .connect = dpi_connect,
698 .disconnect = dpi_disconnect,
700 .enable = dpi_display_enable,
701 .disable = dpi_display_disable,
703 .check_timings = dpi_check_timings,
704 .set_timings = dpi_set_timings,
705 .get_timings = dpi_get_timings,
708 static void dpi_init_output_port(struct dpi_data *dpi, struct device_node *port)
710 struct omap_dss_device *out = &dpi->output;
714 r = of_property_read_u32(port, "reg", &port_num);
731 out->dev = &dpi->pdev->dev;
732 out->id = OMAP_DSS_OUTPUT_DPI;
733 out->output_type = OMAP_DISPLAY_TYPE_DPI;
734 out->dispc_channel = dpi_get_channel(dpi, port_num);
735 out->port_num = port_num;
736 out->ops.dpi = &dpi_ops;
737 out->owner = THIS_MODULE;
739 omapdss_register_output(out);
742 static void dpi_uninit_output_port(struct device_node *port)
744 struct dpi_data *dpi = port->data;
745 struct omap_dss_device *out = &dpi->output;
747 omapdss_unregister_output(out);
750 int dpi_init_port(struct dss_device *dss, struct platform_device *pdev,
751 struct device_node *port, enum dss_model dss_model)
753 struct dpi_data *dpi;
754 struct device_node *ep;
758 dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
762 ep = of_get_next_child(port, NULL);
766 r = of_property_read_u32(ep, "data-lines", &datalines);
768 DSSERR("failed to parse datalines\n");
772 dpi->data_lines = datalines;
777 dpi->dss_model = dss_model;
781 mutex_init(&dpi->lock);
783 dpi_init_output_port(dpi, port);
793 void dpi_uninit_port(struct device_node *port)
795 struct dpi_data *dpi = port->data;
800 dpi_uninit_output_port(port);