1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/video/omap2/dss/dpi.c
5 * Copyright (C) 2009 Nokia Corporation
6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
8 * Some code and ideas taken from drivers/video/omap/ driver
12 #define DSS_SUBSYS_NAME "DPI"
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/export.h>
17 #include <linux/err.h>
18 #include <linux/errno.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/string.h>
23 #include <linux/of_graph.h>
24 #include <linux/clk.h>
25 #include <linux/component.h>
27 #include <video/omapfb_dss.h>
30 #include "dss_features.h"
35 struct platform_device
*pdev
;
37 struct regulator
*vdds_dsi_reg
;
42 struct omap_video_timings timings
;
43 struct dss_lcd_mgr_config mgr_config
;
46 struct omap_dss_device output
;
48 bool port_initialized
;
51 static struct dpi_data
*dpi_get_data_from_dssdev(struct omap_dss_device
*dssdev
)
53 return container_of(dssdev
, struct dpi_data
, output
);
56 /* only used in non-DT mode */
57 static struct dpi_data
*dpi_get_data_from_pdev(struct platform_device
*pdev
)
59 return platform_get_drvdata(pdev
);
62 static struct dss_pll
*dpi_get_pll(enum omap_channel channel
)
65 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
66 * would also be used for DISPC fclk. Meaning, when the DPI output is
67 * disabled, DISPC clock will be disabled, and TV out will stop.
69 switch (omapdss_get_version()) {
70 case OMAPDSS_VER_OMAP24xx
:
71 case OMAPDSS_VER_OMAP34xx_ES1
:
72 case OMAPDSS_VER_OMAP34xx_ES3
:
73 case OMAPDSS_VER_OMAP3630
:
74 case OMAPDSS_VER_AM35xx
:
75 case OMAPDSS_VER_AM43xx
:
78 case OMAPDSS_VER_OMAP4430_ES1
:
79 case OMAPDSS_VER_OMAP4430_ES2
:
80 case OMAPDSS_VER_OMAP4
:
82 case OMAP_DSS_CHANNEL_LCD
:
83 return dss_pll_find("dsi0");
84 case OMAP_DSS_CHANNEL_LCD2
:
85 return dss_pll_find("dsi1");
90 case OMAPDSS_VER_OMAP5
:
92 case OMAP_DSS_CHANNEL_LCD
:
93 return dss_pll_find("dsi0");
94 case OMAP_DSS_CHANNEL_LCD3
:
95 return dss_pll_find("dsi1");
100 case OMAPDSS_VER_DRA7xx
:
102 case OMAP_DSS_CHANNEL_LCD
:
103 case OMAP_DSS_CHANNEL_LCD2
:
104 return dss_pll_find("video0");
105 case OMAP_DSS_CHANNEL_LCD3
:
106 return dss_pll_find("video1");
116 static enum omap_dss_clk_source
dpi_get_alt_clk_src(enum omap_channel channel
)
119 case OMAP_DSS_CHANNEL_LCD
:
120 return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC
;
121 case OMAP_DSS_CHANNEL_LCD2
:
122 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC
;
123 case OMAP_DSS_CHANNEL_LCD3
:
124 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC
;
126 /* this shouldn't happen */
128 return OMAP_DSS_CLK_SRC_FCK
;
132 struct dpi_clk_calc_ctx
{
137 unsigned long pck_min
, pck_max
;
141 struct dss_pll_clock_info dsi_cinfo
;
143 struct dispc_clock_info dispc_cinfo
;
146 static bool dpi_calc_dispc_cb(int lckd
, int pckd
, unsigned long lck
,
147 unsigned long pck
, void *data
)
149 struct dpi_clk_calc_ctx
*ctx
= data
;
152 * Odd dividers give us uneven duty cycle, causing problem when level
153 * shifted. So skip all odd dividers when the pixel clock is on the
156 if (ctx
->pck_min
>= 100000000) {
157 if (lckd
> 1 && lckd
% 2 != 0)
160 if (pckd
> 1 && pckd
% 2 != 0)
164 ctx
->dispc_cinfo
.lck_div
= lckd
;
165 ctx
->dispc_cinfo
.pck_div
= pckd
;
166 ctx
->dispc_cinfo
.lck
= lck
;
167 ctx
->dispc_cinfo
.pck
= pck
;
173 static bool dpi_calc_hsdiv_cb(int m_dispc
, unsigned long dispc
,
176 struct dpi_clk_calc_ctx
*ctx
= data
;
179 * Odd dividers give us uneven duty cycle, causing problem when level
180 * shifted. So skip all odd dividers when the pixel clock is on the
183 if (m_dispc
> 1 && m_dispc
% 2 != 0 && ctx
->pck_min
>= 100000000)
186 ctx
->dsi_cinfo
.mX
[HSDIV_DISPC
] = m_dispc
;
187 ctx
->dsi_cinfo
.clkout
[HSDIV_DISPC
] = dispc
;
189 return dispc_div_calc(dispc
, ctx
->pck_min
, ctx
->pck_max
,
190 dpi_calc_dispc_cb
, ctx
);
194 static bool dpi_calc_pll_cb(int n
, int m
, unsigned long fint
,
195 unsigned long clkdco
,
198 struct dpi_clk_calc_ctx
*ctx
= data
;
200 ctx
->dsi_cinfo
.n
= n
;
201 ctx
->dsi_cinfo
.m
= m
;
202 ctx
->dsi_cinfo
.fint
= fint
;
203 ctx
->dsi_cinfo
.clkdco
= clkdco
;
205 return dss_pll_hsdiv_calc(ctx
->pll
, clkdco
,
206 ctx
->pck_min
, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK
),
207 dpi_calc_hsdiv_cb
, ctx
);
210 static bool dpi_calc_dss_cb(unsigned long fck
, void *data
)
212 struct dpi_clk_calc_ctx
*ctx
= data
;
216 return dispc_div_calc(fck
, ctx
->pck_min
, ctx
->pck_max
,
217 dpi_calc_dispc_cb
, ctx
);
220 static bool dpi_dsi_clk_calc(struct dpi_data
*dpi
, unsigned long pck
,
221 struct dpi_clk_calc_ctx
*ctx
)
224 unsigned long pll_min
, pll_max
;
226 memset(ctx
, 0, sizeof(*ctx
));
228 ctx
->pck_min
= pck
- 1000;
229 ctx
->pck_max
= pck
+ 1000;
234 clkin
= clk_get_rate(ctx
->pll
->clkin
);
236 return dss_pll_calc(ctx
->pll
, clkin
,
238 dpi_calc_pll_cb
, ctx
);
241 static bool dpi_dss_clk_calc(unsigned long pck
, struct dpi_clk_calc_ctx
*ctx
)
246 * DSS fck gives us very few possibilities, so finding a good pixel
247 * clock may not be possible. We try multiple times to find the clock,
248 * each time widening the pixel clock range we look for, up to
252 for (i
= 0; i
< 25; ++i
) {
255 memset(ctx
, 0, sizeof(*ctx
));
256 if (pck
> 1000 * i
* i
* i
)
257 ctx
->pck_min
= max(pck
- 1000 * i
* i
* i
, 0lu);
260 ctx
->pck_max
= pck
+ 1000 * i
* i
* i
;
262 ok
= dss_div_calc(pck
, ctx
->pck_min
, dpi_calc_dss_cb
, ctx
);
272 static int dpi_set_dsi_clk(struct dpi_data
*dpi
, enum omap_channel channel
,
273 unsigned long pck_req
, unsigned long *fck
, int *lck_div
,
276 struct dpi_clk_calc_ctx ctx
;
280 ok
= dpi_dsi_clk_calc(dpi
, pck_req
, &ctx
);
284 r
= dss_pll_set_config(dpi
->pll
, &ctx
.dsi_cinfo
);
288 dss_select_lcd_clk_source(channel
,
289 dpi_get_alt_clk_src(channel
));
291 dpi
->mgr_config
.clock_info
= ctx
.dispc_cinfo
;
293 *fck
= ctx
.dsi_cinfo
.clkout
[HSDIV_DISPC
];
294 *lck_div
= ctx
.dispc_cinfo
.lck_div
;
295 *pck_div
= ctx
.dispc_cinfo
.pck_div
;
300 static int dpi_set_dispc_clk(struct dpi_data
*dpi
, unsigned long pck_req
,
301 unsigned long *fck
, int *lck_div
, int *pck_div
)
303 struct dpi_clk_calc_ctx ctx
;
307 ok
= dpi_dss_clk_calc(pck_req
, &ctx
);
311 r
= dss_set_fck_rate(ctx
.fck
);
315 dpi
->mgr_config
.clock_info
= ctx
.dispc_cinfo
;
318 *lck_div
= ctx
.dispc_cinfo
.lck_div
;
319 *pck_div
= ctx
.dispc_cinfo
.pck_div
;
324 static int dpi_set_mode(struct dpi_data
*dpi
)
326 struct omap_dss_device
*out
= &dpi
->output
;
327 struct omap_overlay_manager
*mgr
= out
->manager
;
328 struct omap_video_timings
*t
= &dpi
->timings
;
329 int lck_div
= 0, pck_div
= 0;
330 unsigned long fck
= 0;
335 r
= dpi_set_dsi_clk(dpi
, mgr
->id
, t
->pixelclock
, &fck
,
338 r
= dpi_set_dispc_clk(dpi
, t
->pixelclock
, &fck
,
343 pck
= fck
/ lck_div
/ pck_div
;
345 if (pck
!= t
->pixelclock
) {
346 DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
352 dss_mgr_set_timings(mgr
, t
);
357 static void dpi_config_lcd_manager(struct dpi_data
*dpi
)
359 struct omap_dss_device
*out
= &dpi
->output
;
360 struct omap_overlay_manager
*mgr
= out
->manager
;
362 dpi
->mgr_config
.io_pad_mode
= DSS_IO_PAD_MODE_BYPASS
;
364 dpi
->mgr_config
.stallmode
= false;
365 dpi
->mgr_config
.fifohandcheck
= false;
367 dpi
->mgr_config
.video_port_width
= dpi
->data_lines
;
369 dpi
->mgr_config
.lcden_sig_polarity
= 0;
371 dss_mgr_set_lcd_config(mgr
, &dpi
->mgr_config
);
374 static int dpi_display_enable(struct omap_dss_device
*dssdev
)
376 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
377 struct omap_dss_device
*out
= &dpi
->output
;
380 mutex_lock(&dpi
->lock
);
382 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
) && !dpi
->vdds_dsi_reg
) {
383 DSSERR("no VDSS_DSI regulator\n");
388 if (out
->manager
== NULL
) {
389 DSSERR("failed to enable display: no output/manager\n");
394 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
)) {
395 r
= regulator_enable(dpi
->vdds_dsi_reg
);
400 r
= dispc_runtime_get();
404 r
= dss_dpi_select_source(out
->port_num
, out
->manager
->id
);
409 r
= dss_pll_enable(dpi
->pll
);
411 goto err_dsi_pll_init
;
414 r
= dpi_set_mode(dpi
);
418 dpi_config_lcd_manager(dpi
);
422 r
= dss_mgr_enable(out
->manager
);
426 mutex_unlock(&dpi
->lock
);
433 dss_pll_disable(dpi
->pll
);
438 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
439 regulator_disable(dpi
->vdds_dsi_reg
);
443 mutex_unlock(&dpi
->lock
);
447 static void dpi_display_disable(struct omap_dss_device
*dssdev
)
449 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
450 struct omap_overlay_manager
*mgr
= dpi
->output
.manager
;
452 mutex_lock(&dpi
->lock
);
454 dss_mgr_disable(mgr
);
457 dss_select_lcd_clk_source(mgr
->id
, OMAP_DSS_CLK_SRC_FCK
);
458 dss_pll_disable(dpi
->pll
);
463 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
464 regulator_disable(dpi
->vdds_dsi_reg
);
466 mutex_unlock(&dpi
->lock
);
469 static void dpi_set_timings(struct omap_dss_device
*dssdev
,
470 struct omap_video_timings
*timings
)
472 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
474 DSSDBG("dpi_set_timings\n");
476 mutex_lock(&dpi
->lock
);
478 dpi
->timings
= *timings
;
480 mutex_unlock(&dpi
->lock
);
483 static void dpi_get_timings(struct omap_dss_device
*dssdev
,
484 struct omap_video_timings
*timings
)
486 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
488 mutex_lock(&dpi
->lock
);
490 *timings
= dpi
->timings
;
492 mutex_unlock(&dpi
->lock
);
495 static int dpi_check_timings(struct omap_dss_device
*dssdev
,
496 struct omap_video_timings
*timings
)
498 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
499 struct omap_overlay_manager
*mgr
= dpi
->output
.manager
;
500 int lck_div
, pck_div
;
503 struct dpi_clk_calc_ctx ctx
;
506 if (mgr
&& !dispc_mgr_timings_ok(mgr
->id
, timings
))
509 if (timings
->pixelclock
== 0)
513 ok
= dpi_dsi_clk_calc(dpi
, timings
->pixelclock
, &ctx
);
517 fck
= ctx
.dsi_cinfo
.clkout
[HSDIV_DISPC
];
519 ok
= dpi_dss_clk_calc(timings
->pixelclock
, &ctx
);
526 lck_div
= ctx
.dispc_cinfo
.lck_div
;
527 pck_div
= ctx
.dispc_cinfo
.pck_div
;
529 pck
= fck
/ lck_div
/ pck_div
;
531 timings
->pixelclock
= pck
;
536 static void dpi_set_data_lines(struct omap_dss_device
*dssdev
, int data_lines
)
538 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
540 mutex_lock(&dpi
->lock
);
542 dpi
->data_lines
= data_lines
;
544 mutex_unlock(&dpi
->lock
);
547 static int dpi_verify_dsi_pll(struct dss_pll
*pll
)
551 /* do initial setup with the PLL to see if it is operational */
553 r
= dss_pll_enable(pll
);
557 dss_pll_disable(pll
);
562 static int dpi_init_regulator(struct dpi_data
*dpi
)
564 struct regulator
*vdds_dsi
;
566 if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI
))
569 if (dpi
->vdds_dsi_reg
)
572 vdds_dsi
= devm_regulator_get(&dpi
->pdev
->dev
, "vdds_dsi");
573 if (IS_ERR(vdds_dsi
)) {
574 if (PTR_ERR(vdds_dsi
) != -EPROBE_DEFER
)
575 DSSERR("can't get VDDS_DSI regulator\n");
576 return PTR_ERR(vdds_dsi
);
579 dpi
->vdds_dsi_reg
= vdds_dsi
;
584 static void dpi_init_pll(struct dpi_data
*dpi
)
591 pll
= dpi_get_pll(dpi
->output
.dispc_channel
);
595 /* On DRA7 we need to set a mux to use the PLL */
596 if (omapdss_get_version() == OMAPDSS_VER_DRA7xx
)
597 dss_ctrl_pll_set_control_mux(pll
->id
, dpi
->output
.dispc_channel
);
599 if (dpi_verify_dsi_pll(pll
)) {
600 DSSWARN("DSI PLL not operational\n");
608 * Return a hardcoded channel for the DPI output. This should work for
609 * current use cases, but this can be later expanded to either resolve
610 * the channel in some more dynamic manner, or get the channel as a user
613 static enum omap_channel
dpi_get_channel(int port_num
)
615 switch (omapdss_get_version()) {
616 case OMAPDSS_VER_OMAP24xx
:
617 case OMAPDSS_VER_OMAP34xx_ES1
:
618 case OMAPDSS_VER_OMAP34xx_ES3
:
619 case OMAPDSS_VER_OMAP3630
:
620 case OMAPDSS_VER_AM35xx
:
621 case OMAPDSS_VER_AM43xx
:
622 return OMAP_DSS_CHANNEL_LCD
;
624 case OMAPDSS_VER_DRA7xx
:
627 return OMAP_DSS_CHANNEL_LCD3
;
629 return OMAP_DSS_CHANNEL_LCD2
;
632 return OMAP_DSS_CHANNEL_LCD
;
635 case OMAPDSS_VER_OMAP4430_ES1
:
636 case OMAPDSS_VER_OMAP4430_ES2
:
637 case OMAPDSS_VER_OMAP4
:
638 return OMAP_DSS_CHANNEL_LCD2
;
640 case OMAPDSS_VER_OMAP5
:
641 return OMAP_DSS_CHANNEL_LCD3
;
644 DSSWARN("unsupported DSS version\n");
645 return OMAP_DSS_CHANNEL_LCD
;
649 static int dpi_connect(struct omap_dss_device
*dssdev
,
650 struct omap_dss_device
*dst
)
652 struct dpi_data
*dpi
= dpi_get_data_from_dssdev(dssdev
);
653 struct omap_overlay_manager
*mgr
;
656 r
= dpi_init_regulator(dpi
);
662 mgr
= omap_dss_get_overlay_manager(dssdev
->dispc_channel
);
666 r
= dss_mgr_connect(mgr
, dssdev
);
670 r
= omapdss_output_set_device(dssdev
, dst
);
672 DSSERR("failed to connect output to new device: %s\n",
674 dss_mgr_disconnect(mgr
, dssdev
);
681 static void dpi_disconnect(struct omap_dss_device
*dssdev
,
682 struct omap_dss_device
*dst
)
684 WARN_ON(dst
!= dssdev
->dst
);
686 if (dst
!= dssdev
->dst
)
689 omapdss_output_unset_device(dssdev
);
692 dss_mgr_disconnect(dssdev
->manager
, dssdev
);
695 static const struct omapdss_dpi_ops dpi_ops
= {
696 .connect
= dpi_connect
,
697 .disconnect
= dpi_disconnect
,
699 .enable
= dpi_display_enable
,
700 .disable
= dpi_display_disable
,
702 .check_timings
= dpi_check_timings
,
703 .set_timings
= dpi_set_timings
,
704 .get_timings
= dpi_get_timings
,
706 .set_data_lines
= dpi_set_data_lines
,
709 static void dpi_init_output(struct platform_device
*pdev
)
711 struct dpi_data
*dpi
= dpi_get_data_from_pdev(pdev
);
712 struct omap_dss_device
*out
= &dpi
->output
;
714 out
->dev
= &pdev
->dev
;
715 out
->id
= OMAP_DSS_OUTPUT_DPI
;
716 out
->output_type
= OMAP_DISPLAY_TYPE_DPI
;
718 out
->dispc_channel
= dpi_get_channel(0);
719 out
->ops
.dpi
= &dpi_ops
;
720 out
->owner
= THIS_MODULE
;
722 omapdss_register_output(out
);
725 static void dpi_uninit_output(struct platform_device
*pdev
)
727 struct dpi_data
*dpi
= dpi_get_data_from_pdev(pdev
);
728 struct omap_dss_device
*out
= &dpi
->output
;
730 omapdss_unregister_output(out
);
733 static void dpi_init_output_port(struct platform_device
*pdev
,
734 struct device_node
*port
)
736 struct dpi_data
*dpi
= port
->data
;
737 struct omap_dss_device
*out
= &dpi
->output
;
741 r
= of_property_read_u32(port
, "reg", &port_num
);
758 out
->dev
= &pdev
->dev
;
759 out
->id
= OMAP_DSS_OUTPUT_DPI
;
760 out
->output_type
= OMAP_DISPLAY_TYPE_DPI
;
761 out
->dispc_channel
= dpi_get_channel(port_num
);
762 out
->port_num
= port_num
;
763 out
->ops
.dpi
= &dpi_ops
;
764 out
->owner
= THIS_MODULE
;
766 omapdss_register_output(out
);
769 static void dpi_uninit_output_port(struct device_node
*port
)
771 struct dpi_data
*dpi
= port
->data
;
772 struct omap_dss_device
*out
= &dpi
->output
;
774 omapdss_unregister_output(out
);
777 static int dpi_bind(struct device
*dev
, struct device
*master
, void *data
)
779 struct platform_device
*pdev
= to_platform_device(dev
);
780 struct dpi_data
*dpi
;
782 dpi
= devm_kzalloc(&pdev
->dev
, sizeof(*dpi
), GFP_KERNEL
);
788 platform_set_drvdata(pdev
, dpi
);
790 mutex_init(&dpi
->lock
);
792 dpi_init_output(pdev
);
797 static void dpi_unbind(struct device
*dev
, struct device
*master
, void *data
)
799 struct platform_device
*pdev
= to_platform_device(dev
);
801 dpi_uninit_output(pdev
);
804 static const struct component_ops dpi_component_ops
= {
806 .unbind
= dpi_unbind
,
809 static int dpi_probe(struct platform_device
*pdev
)
811 return component_add(&pdev
->dev
, &dpi_component_ops
);
814 static void dpi_remove(struct platform_device
*pdev
)
816 component_del(&pdev
->dev
, &dpi_component_ops
);
819 static struct platform_driver omap_dpi_driver
= {
821 .remove
= dpi_remove
,
823 .name
= "omapdss_dpi",
824 .suppress_bind_attrs
= true,
828 int __init
dpi_init_platform_driver(void)
830 return platform_driver_register(&omap_dpi_driver
);
833 void dpi_uninit_platform_driver(void)
835 platform_driver_unregister(&omap_dpi_driver
);
838 int dpi_init_port(struct platform_device
*pdev
, struct device_node
*port
)
840 struct dpi_data
*dpi
;
841 struct device_node
*ep
;
845 dpi
= devm_kzalloc(&pdev
->dev
, sizeof(*dpi
), GFP_KERNEL
);
849 ep
= of_graph_get_next_port_endpoint(port
, NULL
);
853 r
= of_property_read_u32(ep
, "data-lines", &datalines
);
855 DSSERR("failed to parse datalines\n");
859 dpi
->data_lines
= datalines
;
866 mutex_init(&dpi
->lock
);
868 dpi_init_output_port(pdev
, port
);
870 dpi
->port_initialized
= true;
880 void dpi_uninit_port(struct device_node
*port
)
882 struct dpi_data
*dpi
= port
->data
;
884 if (!dpi
->port_initialized
)
887 dpi_uninit_output_port(port
);