2 * linux/drivers/video/omap2/dss/dpi.c
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
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 <video/omapdss.h>
37 #include "dss_features.h"
40 struct platform_device
*pdev
;
42 struct regulator
*vdds_dsi_reg
;
43 struct platform_device
*dsidev
;
47 struct omap_video_timings timings
;
48 struct dss_lcd_mgr_config mgr_config
;
51 struct omap_dss_device output
;
54 static struct platform_device
*dpi_get_dsidev(enum omap_channel channel
)
57 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
58 * would also be used for DISPC fclk. Meaning, when the DPI output is
59 * disabled, DISPC clock will be disabled, and TV out will stop.
61 switch (omapdss_get_version()) {
62 case OMAPDSS_VER_OMAP24xx
:
63 case OMAPDSS_VER_OMAP34xx_ES1
:
64 case OMAPDSS_VER_OMAP34xx_ES3
:
65 case OMAPDSS_VER_OMAP3630
:
66 case OMAPDSS_VER_AM35xx
:
69 case OMAPDSS_VER_OMAP4430_ES1
:
70 case OMAPDSS_VER_OMAP4430_ES2
:
71 case OMAPDSS_VER_OMAP4
:
73 case OMAP_DSS_CHANNEL_LCD
:
74 return dsi_get_dsidev_from_id(0);
75 case OMAP_DSS_CHANNEL_LCD2
:
76 return dsi_get_dsidev_from_id(1);
81 case OMAPDSS_VER_OMAP5
:
83 case OMAP_DSS_CHANNEL_LCD
:
84 return dsi_get_dsidev_from_id(0);
85 case OMAP_DSS_CHANNEL_LCD3
:
86 return dsi_get_dsidev_from_id(1);
96 static enum omap_dss_clk_source
dpi_get_alt_clk_src(enum omap_channel channel
)
99 case OMAP_DSS_CHANNEL_LCD
:
100 return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC
;
101 case OMAP_DSS_CHANNEL_LCD2
:
102 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC
;
104 /* this shouldn't happen */
106 return OMAP_DSS_CLK_SRC_FCK
;
110 struct dpi_clk_calc_ctx
{
111 struct platform_device
*dsidev
;
115 unsigned long pck_min
, pck_max
;
119 struct dsi_clock_info dsi_cinfo
;
120 unsigned long long fck
;
121 struct dispc_clock_info dispc_cinfo
;
124 static bool dpi_calc_dispc_cb(int lckd
, int pckd
, unsigned long lck
,
125 unsigned long pck
, void *data
)
127 struct dpi_clk_calc_ctx
*ctx
= data
;
130 * Odd dividers give us uneven duty cycle, causing problem when level
131 * shifted. So skip all odd dividers when the pixel clock is on the
134 if (ctx
->pck_min
>= 100000000) {
135 if (lckd
> 1 && lckd
% 2 != 0)
138 if (pckd
> 1 && pckd
% 2 != 0)
142 ctx
->dispc_cinfo
.lck_div
= lckd
;
143 ctx
->dispc_cinfo
.pck_div
= pckd
;
144 ctx
->dispc_cinfo
.lck
= lck
;
145 ctx
->dispc_cinfo
.pck
= pck
;
151 static bool dpi_calc_hsdiv_cb(int regm_dispc
, unsigned long dispc
,
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 (regm_dispc
> 1 && regm_dispc
% 2 != 0 && ctx
->pck_min
>= 100000000)
164 ctx
->dsi_cinfo
.regm_dispc
= regm_dispc
;
165 ctx
->dsi_cinfo
.dsi_pll_hsdiv_dispc_clk
= dispc
;
167 return dispc_div_calc(dispc
, ctx
->pck_min
, ctx
->pck_max
,
168 dpi_calc_dispc_cb
, ctx
);
172 static bool dpi_calc_pll_cb(int regn
, int regm
, unsigned long fint
,
176 struct dpi_clk_calc_ctx
*ctx
= data
;
178 ctx
->dsi_cinfo
.regn
= regn
;
179 ctx
->dsi_cinfo
.regm
= regm
;
180 ctx
->dsi_cinfo
.fint
= fint
;
181 ctx
->dsi_cinfo
.clkin4ddr
= pll
;
183 return dsi_hsdiv_calc(ctx
->dsidev
, pll
, ctx
->pck_min
,
184 dpi_calc_hsdiv_cb
, ctx
);
187 static bool dpi_calc_dss_cb(unsigned long fck
, void *data
)
189 struct dpi_clk_calc_ctx
*ctx
= data
;
193 return dispc_div_calc(fck
, ctx
->pck_min
, ctx
->pck_max
,
194 dpi_calc_dispc_cb
, ctx
);
197 static bool dpi_dsi_clk_calc(unsigned long pck
, struct dpi_clk_calc_ctx
*ctx
)
200 unsigned long pll_min
, pll_max
;
202 clkin
= dsi_get_pll_clkin(dpi
.dsidev
);
204 memset(ctx
, 0, sizeof(*ctx
));
205 ctx
->dsidev
= dpi
.dsidev
;
206 ctx
->pck_min
= pck
- 1000;
207 ctx
->pck_max
= pck
+ 1000;
208 ctx
->dsi_cinfo
.clkin
= clkin
;
213 return dsi_pll_calc(dpi
.dsidev
, clkin
,
215 dpi_calc_pll_cb
, ctx
);
218 static bool dpi_dss_clk_calc(unsigned long pck
, struct dpi_clk_calc_ctx
*ctx
)
223 * DSS fck gives us very few possibilities, so finding a good pixel
224 * clock may not be possible. We try multiple times to find the clock,
225 * each time widening the pixel clock range we look for, up to
229 for (i
= 0; i
< 25; ++i
) {
232 memset(ctx
, 0, sizeof(*ctx
));
233 if (pck
> 1000 * i
* i
* i
)
234 ctx
->pck_min
= max(pck
- 1000 * i
* i
* i
, 0lu);
237 ctx
->pck_max
= pck
+ 1000 * i
* i
* i
;
239 ok
= dss_div_calc(pck
, ctx
->pck_min
, dpi_calc_dss_cb
, ctx
);
249 static int dpi_set_dsi_clk(enum omap_channel channel
,
250 unsigned long pck_req
, unsigned long *fck
, int *lck_div
,
253 struct dpi_clk_calc_ctx ctx
;
257 ok
= dpi_dsi_clk_calc(pck_req
, &ctx
);
261 r
= dsi_pll_set_clock_div(dpi
.dsidev
, &ctx
.dsi_cinfo
);
265 dss_select_lcd_clk_source(channel
,
266 dpi_get_alt_clk_src(channel
));
268 dpi
.mgr_config
.clock_info
= ctx
.dispc_cinfo
;
270 *fck
= ctx
.dsi_cinfo
.dsi_pll_hsdiv_dispc_clk
;
271 *lck_div
= ctx
.dispc_cinfo
.lck_div
;
272 *pck_div
= ctx
.dispc_cinfo
.pck_div
;
277 static int dpi_set_dispc_clk(unsigned long pck_req
, unsigned long *fck
,
278 int *lck_div
, int *pck_div
)
280 struct dpi_clk_calc_ctx ctx
;
284 ok
= dpi_dss_clk_calc(pck_req
, &ctx
);
288 r
= dss_set_fck_rate(ctx
.fck
);
292 dpi
.mgr_config
.clock_info
= ctx
.dispc_cinfo
;
295 *lck_div
= ctx
.dispc_cinfo
.lck_div
;
296 *pck_div
= ctx
.dispc_cinfo
.pck_div
;
301 static int dpi_set_mode(struct omap_overlay_manager
*mgr
)
303 struct omap_video_timings
*t
= &dpi
.timings
;
304 int lck_div
= 0, pck_div
= 0;
305 unsigned long fck
= 0;
310 r
= dpi_set_dsi_clk(mgr
->id
, t
->pixel_clock
* 1000, &fck
,
313 r
= dpi_set_dispc_clk(t
->pixel_clock
* 1000, &fck
,
318 pck
= fck
/ lck_div
/ pck_div
/ 1000;
320 if (pck
!= t
->pixel_clock
) {
321 DSSWARN("Could not find exact pixel clock. "
322 "Requested %d kHz, got %lu kHz\n",
323 t
->pixel_clock
, pck
);
325 t
->pixel_clock
= pck
;
328 dss_mgr_set_timings(mgr
, t
);
333 static void dpi_config_lcd_manager(struct omap_overlay_manager
*mgr
)
335 dpi
.mgr_config
.io_pad_mode
= DSS_IO_PAD_MODE_BYPASS
;
337 dpi
.mgr_config
.stallmode
= false;
338 dpi
.mgr_config
.fifohandcheck
= false;
340 dpi
.mgr_config
.video_port_width
= dpi
.data_lines
;
342 dpi
.mgr_config
.lcden_sig_polarity
= 0;
344 dss_mgr_set_lcd_config(mgr
, &dpi
.mgr_config
);
347 static int dpi_display_enable(struct omap_dss_device
*dssdev
)
349 struct omap_dss_device
*out
= &dpi
.output
;
352 mutex_lock(&dpi
.lock
);
354 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
) && !dpi
.vdds_dsi_reg
) {
355 DSSERR("no VDSS_DSI regulator\n");
360 if (out
== NULL
|| out
->manager
== NULL
) {
361 DSSERR("failed to enable display: no output/manager\n");
366 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
)) {
367 r
= regulator_enable(dpi
.vdds_dsi_reg
);
372 r
= dispc_runtime_get();
376 r
= dss_dpi_select_source(out
->manager
->id
);
381 r
= dsi_runtime_get(dpi
.dsidev
);
385 r
= dsi_pll_init(dpi
.dsidev
, 0, 1);
387 goto err_dsi_pll_init
;
390 r
= dpi_set_mode(out
->manager
);
394 dpi_config_lcd_manager(out
->manager
);
398 r
= dss_mgr_enable(out
->manager
);
402 mutex_unlock(&dpi
.lock
);
409 dsi_pll_uninit(dpi
.dsidev
, true);
412 dsi_runtime_put(dpi
.dsidev
);
417 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
418 regulator_disable(dpi
.vdds_dsi_reg
);
422 mutex_unlock(&dpi
.lock
);
426 static void dpi_display_disable(struct omap_dss_device
*dssdev
)
428 struct omap_overlay_manager
*mgr
= dpi
.output
.manager
;
430 mutex_lock(&dpi
.lock
);
432 dss_mgr_disable(mgr
);
435 dss_select_lcd_clk_source(mgr
->id
, OMAP_DSS_CLK_SRC_FCK
);
436 dsi_pll_uninit(dpi
.dsidev
, true);
437 dsi_runtime_put(dpi
.dsidev
);
442 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
443 regulator_disable(dpi
.vdds_dsi_reg
);
445 mutex_unlock(&dpi
.lock
);
448 static void dpi_set_timings(struct omap_dss_device
*dssdev
,
449 struct omap_video_timings
*timings
)
451 DSSDBG("dpi_set_timings\n");
453 mutex_lock(&dpi
.lock
);
455 dpi
.timings
= *timings
;
457 mutex_unlock(&dpi
.lock
);
460 static void dpi_get_timings(struct omap_dss_device
*dssdev
,
461 struct omap_video_timings
*timings
)
463 mutex_lock(&dpi
.lock
);
465 *timings
= dpi
.timings
;
467 mutex_unlock(&dpi
.lock
);
470 static int dpi_check_timings(struct omap_dss_device
*dssdev
,
471 struct omap_video_timings
*timings
)
473 struct omap_overlay_manager
*mgr
= dpi
.output
.manager
;
474 int lck_div
, pck_div
;
477 struct dpi_clk_calc_ctx ctx
;
480 if (mgr
&& !dispc_mgr_timings_ok(mgr
->id
, timings
))
483 if (timings
->pixel_clock
== 0)
487 ok
= dpi_dsi_clk_calc(timings
->pixel_clock
* 1000, &ctx
);
491 fck
= ctx
.dsi_cinfo
.dsi_pll_hsdiv_dispc_clk
;
493 ok
= dpi_dss_clk_calc(timings
->pixel_clock
* 1000, &ctx
);
500 lck_div
= ctx
.dispc_cinfo
.lck_div
;
501 pck_div
= ctx
.dispc_cinfo
.pck_div
;
503 pck
= fck
/ lck_div
/ pck_div
/ 1000;
505 timings
->pixel_clock
= pck
;
510 static void dpi_set_data_lines(struct omap_dss_device
*dssdev
, int data_lines
)
512 mutex_lock(&dpi
.lock
);
514 dpi
.data_lines
= data_lines
;
516 mutex_unlock(&dpi
.lock
);
519 static int dpi_verify_dsi_pll(struct platform_device
*dsidev
)
523 /* do initial setup with the PLL to see if it is operational */
525 r
= dsi_runtime_get(dsidev
);
529 r
= dsi_pll_init(dsidev
, 0, 1);
531 dsi_runtime_put(dsidev
);
535 dsi_pll_uninit(dsidev
, true);
536 dsi_runtime_put(dsidev
);
541 static int dpi_init_regulator(void)
543 struct regulator
*vdds_dsi
;
545 if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
548 if (dpi
.vdds_dsi_reg
)
551 vdds_dsi
= devm_regulator_get(&dpi
.pdev
->dev
, "vdds_dsi");
552 if (IS_ERR(vdds_dsi
)) {
553 if (PTR_ERR(vdds_dsi
) != -EPROBE_DEFER
)
554 DSSERR("can't get VDDS_DSI regulator\n");
555 return PTR_ERR(vdds_dsi
);
558 dpi
.vdds_dsi_reg
= vdds_dsi
;
563 static void dpi_init_pll(void)
565 struct platform_device
*dsidev
;
570 dsidev
= dpi_get_dsidev(dpi
.output
.dispc_channel
);
574 if (dpi_verify_dsi_pll(dsidev
)) {
575 DSSWARN("DSI PLL not operational\n");
583 * Return a hardcoded channel for the DPI output. This should work for
584 * current use cases, but this can be later expanded to either resolve
585 * the channel in some more dynamic manner, or get the channel as a user
588 static enum omap_channel
dpi_get_channel(void)
590 switch (omapdss_get_version()) {
591 case OMAPDSS_VER_OMAP24xx
:
592 case OMAPDSS_VER_OMAP34xx_ES1
:
593 case OMAPDSS_VER_OMAP34xx_ES3
:
594 case OMAPDSS_VER_OMAP3630
:
595 case OMAPDSS_VER_AM35xx
:
596 return OMAP_DSS_CHANNEL_LCD
;
598 case OMAPDSS_VER_OMAP4430_ES1
:
599 case OMAPDSS_VER_OMAP4430_ES2
:
600 case OMAPDSS_VER_OMAP4
:
601 return OMAP_DSS_CHANNEL_LCD2
;
603 case OMAPDSS_VER_OMAP5
:
604 return OMAP_DSS_CHANNEL_LCD3
;
607 DSSWARN("unsupported DSS version\n");
608 return OMAP_DSS_CHANNEL_LCD
;
612 static int dpi_connect(struct omap_dss_device
*dssdev
,
613 struct omap_dss_device
*dst
)
615 struct omap_overlay_manager
*mgr
;
618 r
= dpi_init_regulator();
624 mgr
= omap_dss_get_overlay_manager(dssdev
->dispc_channel
);
628 r
= dss_mgr_connect(mgr
, dssdev
);
632 r
= omapdss_output_set_device(dssdev
, dst
);
634 DSSERR("failed to connect output to new device: %s\n",
636 dss_mgr_disconnect(mgr
, dssdev
);
643 static void dpi_disconnect(struct omap_dss_device
*dssdev
,
644 struct omap_dss_device
*dst
)
646 WARN_ON(dst
!= dssdev
->dst
);
648 if (dst
!= dssdev
->dst
)
651 omapdss_output_unset_device(dssdev
);
654 dss_mgr_disconnect(dssdev
->manager
, dssdev
);
657 static const struct omapdss_dpi_ops dpi_ops
= {
658 .connect
= dpi_connect
,
659 .disconnect
= dpi_disconnect
,
661 .enable
= dpi_display_enable
,
662 .disable
= dpi_display_disable
,
664 .check_timings
= dpi_check_timings
,
665 .set_timings
= dpi_set_timings
,
666 .get_timings
= dpi_get_timings
,
668 .set_data_lines
= dpi_set_data_lines
,
671 static void dpi_init_output(struct platform_device
*pdev
)
673 struct omap_dss_device
*out
= &dpi
.output
;
675 out
->dev
= &pdev
->dev
;
676 out
->id
= OMAP_DSS_OUTPUT_DPI
;
677 out
->output_type
= OMAP_DISPLAY_TYPE_DPI
;
679 out
->dispc_channel
= dpi_get_channel();
680 out
->ops
.dpi
= &dpi_ops
;
681 out
->owner
= THIS_MODULE
;
683 omapdss_register_output(out
);
686 static void __exit
dpi_uninit_output(struct platform_device
*pdev
)
688 struct omap_dss_device
*out
= &dpi
.output
;
690 omapdss_unregister_output(out
);
693 static int omap_dpi_probe(struct platform_device
*pdev
)
697 mutex_init(&dpi
.lock
);
699 dpi_init_output(pdev
);
704 static int __exit
omap_dpi_remove(struct platform_device
*pdev
)
706 dpi_uninit_output(pdev
);
711 static struct platform_driver omap_dpi_driver
= {
712 .probe
= omap_dpi_probe
,
713 .remove
= __exit_p(omap_dpi_remove
),
715 .name
= "omapdss_dpi",
716 .owner
= THIS_MODULE
,
720 int __init
dpi_init_platform_driver(void)
722 return platform_driver_register(&omap_dpi_driver
);
725 void __exit
dpi_uninit_platform_driver(void)
727 platform_driver_unregister(&omap_dpi_driver
);