1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/video/omap2/dss/sdi.c
5 * Copyright (C) 2009 Nokia Corporation
6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
9 #define DSS_SUBSYS_NAME "SDI"
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/export.h>
16 #include <linux/platform_device.h>
17 #include <linux/string.h>
19 #include <linux/component.h>
21 #include <video/omapfb_dss.h>
25 struct platform_device
*pdev
;
28 struct regulator
*vdds_sdi_reg
;
30 struct dss_lcd_mgr_config mgr_config
;
31 struct omap_video_timings timings
;
34 struct omap_dss_device output
;
36 bool port_initialized
;
39 struct sdi_clk_calc_ctx
{
40 unsigned long pck_min
, pck_max
;
43 struct dispc_clock_info dispc_cinfo
;
46 static bool dpi_calc_dispc_cb(int lckd
, int pckd
, unsigned long lck
,
47 unsigned long pck
, void *data
)
49 struct sdi_clk_calc_ctx
*ctx
= data
;
51 ctx
->dispc_cinfo
.lck_div
= lckd
;
52 ctx
->dispc_cinfo
.pck_div
= pckd
;
53 ctx
->dispc_cinfo
.lck
= lck
;
54 ctx
->dispc_cinfo
.pck
= pck
;
59 static bool dpi_calc_dss_cb(unsigned long fck
, void *data
)
61 struct sdi_clk_calc_ctx
*ctx
= data
;
65 return dispc_div_calc(fck
, ctx
->pck_min
, ctx
->pck_max
,
66 dpi_calc_dispc_cb
, ctx
);
69 static int sdi_calc_clock_div(unsigned long pclk
,
71 struct dispc_clock_info
*dispc_cinfo
)
74 struct sdi_clk_calc_ctx ctx
;
77 * DSS fclk gives us very few possibilities, so finding a good pixel
78 * clock may not be possible. We try multiple times to find the clock,
79 * each time widening the pixel clock range we look for, up to
83 for (i
= 0; i
< 10; ++i
) {
86 memset(&ctx
, 0, sizeof(ctx
));
87 if (pclk
> 1000 * i
* i
* i
)
88 ctx
.pck_min
= max(pclk
- 1000 * i
* i
* i
, 0lu);
91 ctx
.pck_max
= pclk
+ 1000 * i
* i
* i
;
93 ok
= dss_div_calc(pclk
, ctx
.pck_min
, dpi_calc_dss_cb
, &ctx
);
96 *dispc_cinfo
= ctx
.dispc_cinfo
;
104 static void sdi_config_lcd_manager(struct omap_dss_device
*dssdev
)
106 struct omap_overlay_manager
*mgr
= sdi
.output
.manager
;
108 sdi
.mgr_config
.io_pad_mode
= DSS_IO_PAD_MODE_BYPASS
;
110 sdi
.mgr_config
.stallmode
= false;
111 sdi
.mgr_config
.fifohandcheck
= false;
113 sdi
.mgr_config
.video_port_width
= 24;
114 sdi
.mgr_config
.lcden_sig_polarity
= 1;
116 dss_mgr_set_lcd_config(mgr
, &sdi
.mgr_config
);
119 static int sdi_display_enable(struct omap_dss_device
*dssdev
)
121 struct omap_dss_device
*out
= &sdi
.output
;
122 struct omap_video_timings
*t
= &sdi
.timings
;
124 struct dispc_clock_info dispc_cinfo
;
128 if (out
->manager
== NULL
) {
129 DSSERR("failed to enable display: no output/manager\n");
133 r
= regulator_enable(sdi
.vdds_sdi_reg
);
137 r
= dispc_runtime_get();
142 t
->data_pclk_edge
= OMAPDSS_DRIVE_SIG_RISING_EDGE
;
143 t
->sync_pclk_edge
= OMAPDSS_DRIVE_SIG_RISING_EDGE
;
145 r
= sdi_calc_clock_div(t
->pixelclock
, &fck
, &dispc_cinfo
);
147 goto err_calc_clock_div
;
149 sdi
.mgr_config
.clock_info
= dispc_cinfo
;
151 pck
= fck
/ dispc_cinfo
.lck_div
/ dispc_cinfo
.pck_div
;
153 if (pck
!= t
->pixelclock
) {
154 DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
161 dss_mgr_set_timings(out
->manager
, t
);
163 r
= dss_set_fck_rate(fck
);
165 goto err_set_dss_clock_div
;
167 sdi_config_lcd_manager(dssdev
);
170 * LCLK and PCLK divisors are located in shadow registers, and we
171 * normally write them to DISPC registers when enabling the output.
172 * However, SDI uses pck-free as source clock for its PLL, and pck-free
173 * is affected by the divisors. And as we need the PLL before enabling
174 * the output, we need to write the divisors early.
176 * It seems just writing to the DISPC register is enough, and we don't
177 * need to care about the shadow register mechanism for pck-free. The
178 * exact reason for this is unknown.
180 dispc_mgr_set_clock_div(out
->manager
->id
, &sdi
.mgr_config
.clock_info
);
182 dss_sdi_init(sdi
.datapairs
);
183 r
= dss_sdi_enable();
188 r
= dss_mgr_enable(out
->manager
);
197 err_set_dss_clock_div
:
201 regulator_disable(sdi
.vdds_sdi_reg
);
206 static void sdi_display_disable(struct omap_dss_device
*dssdev
)
208 struct omap_overlay_manager
*mgr
= sdi
.output
.manager
;
210 dss_mgr_disable(mgr
);
216 regulator_disable(sdi
.vdds_sdi_reg
);
219 static void sdi_set_timings(struct omap_dss_device
*dssdev
,
220 struct omap_video_timings
*timings
)
222 sdi
.timings
= *timings
;
225 static void sdi_get_timings(struct omap_dss_device
*dssdev
,
226 struct omap_video_timings
*timings
)
228 *timings
= sdi
.timings
;
231 static int sdi_check_timings(struct omap_dss_device
*dssdev
,
232 struct omap_video_timings
*timings
)
234 struct omap_overlay_manager
*mgr
= sdi
.output
.manager
;
236 if (mgr
&& !dispc_mgr_timings_ok(mgr
->id
, timings
))
239 if (timings
->pixelclock
== 0)
245 static void sdi_set_datapairs(struct omap_dss_device
*dssdev
, int datapairs
)
247 sdi
.datapairs
= datapairs
;
250 static int sdi_init_regulator(void)
252 struct regulator
*vdds_sdi
;
254 if (sdi
.vdds_sdi_reg
)
257 vdds_sdi
= devm_regulator_get(&sdi
.pdev
->dev
, "vdds_sdi");
258 if (IS_ERR(vdds_sdi
)) {
259 if (PTR_ERR(vdds_sdi
) != -EPROBE_DEFER
)
260 DSSERR("can't get VDDS_SDI regulator\n");
261 return PTR_ERR(vdds_sdi
);
264 sdi
.vdds_sdi_reg
= vdds_sdi
;
269 static int sdi_connect(struct omap_dss_device
*dssdev
,
270 struct omap_dss_device
*dst
)
272 struct omap_overlay_manager
*mgr
;
275 r
= sdi_init_regulator();
279 mgr
= omap_dss_get_overlay_manager(dssdev
->dispc_channel
);
283 r
= dss_mgr_connect(mgr
, dssdev
);
287 r
= omapdss_output_set_device(dssdev
, dst
);
289 DSSERR("failed to connect output to new device: %s\n",
291 dss_mgr_disconnect(mgr
, dssdev
);
298 static void sdi_disconnect(struct omap_dss_device
*dssdev
,
299 struct omap_dss_device
*dst
)
301 WARN_ON(dst
!= dssdev
->dst
);
303 if (dst
!= dssdev
->dst
)
306 omapdss_output_unset_device(dssdev
);
309 dss_mgr_disconnect(dssdev
->manager
, dssdev
);
312 static const struct omapdss_sdi_ops sdi_ops
= {
313 .connect
= sdi_connect
,
314 .disconnect
= sdi_disconnect
,
316 .enable
= sdi_display_enable
,
317 .disable
= sdi_display_disable
,
319 .check_timings
= sdi_check_timings
,
320 .set_timings
= sdi_set_timings
,
321 .get_timings
= sdi_get_timings
,
323 .set_datapairs
= sdi_set_datapairs
,
326 static void sdi_init_output(struct platform_device
*pdev
)
328 struct omap_dss_device
*out
= &sdi
.output
;
330 out
->dev
= &pdev
->dev
;
331 out
->id
= OMAP_DSS_OUTPUT_SDI
;
332 out
->output_type
= OMAP_DISPLAY_TYPE_SDI
;
334 out
->dispc_channel
= OMAP_DSS_CHANNEL_LCD
;
335 /* We have SDI only on OMAP3, where it's on port 1 */
337 out
->ops
.sdi
= &sdi_ops
;
338 out
->owner
= THIS_MODULE
;
340 omapdss_register_output(out
);
343 static void sdi_uninit_output(struct platform_device
*pdev
)
345 struct omap_dss_device
*out
= &sdi
.output
;
347 omapdss_unregister_output(out
);
350 static int sdi_bind(struct device
*dev
, struct device
*master
, void *data
)
352 struct platform_device
*pdev
= to_platform_device(dev
);
356 sdi_init_output(pdev
);
361 static void sdi_unbind(struct device
*dev
, struct device
*master
, void *data
)
363 struct platform_device
*pdev
= to_platform_device(dev
);
365 sdi_uninit_output(pdev
);
368 static const struct component_ops sdi_component_ops
= {
370 .unbind
= sdi_unbind
,
373 static int sdi_probe(struct platform_device
*pdev
)
375 return component_add(&pdev
->dev
, &sdi_component_ops
);
378 static int sdi_remove(struct platform_device
*pdev
)
380 component_del(&pdev
->dev
, &sdi_component_ops
);
384 static struct platform_driver omap_sdi_driver
= {
386 .remove
= sdi_remove
,
388 .name
= "omapdss_sdi",
389 .suppress_bind_attrs
= true,
393 int __init
sdi_init_platform_driver(void)
395 return platform_driver_register(&omap_sdi_driver
);
398 void sdi_uninit_platform_driver(void)
400 platform_driver_unregister(&omap_sdi_driver
);
403 int sdi_init_port(struct platform_device
*pdev
, struct device_node
*port
)
405 struct device_node
*ep
;
409 ep
= omapdss_of_get_next_endpoint(port
, NULL
);
413 r
= of_property_read_u32(ep
, "datapairs", &datapairs
);
415 DSSERR("failed to parse datapairs\n");
419 sdi
.datapairs
= datapairs
;
425 sdi_init_output(pdev
);
427 sdi
.port_initialized
= true;
437 void sdi_uninit_port(struct device_node
*port
)
439 if (!sdi
.port_initialized
)
442 sdi_uninit_output(sdi
.pdev
);