Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / dss / sdi.c
blob002f07f5480f41ada09fccb3ce8a0e9548195398
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/video/omap2/dss/sdi.c
5 * Copyright (C) 2009 Nokia Corporation
6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 */
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>
18 #include <linux/of.h>
19 #include <linux/component.h>
21 #include <video/omapfb_dss.h>
22 #include "dss.h"
24 static struct {
25 struct platform_device *pdev;
27 bool update_enabled;
28 struct regulator *vdds_sdi_reg;
30 struct dss_lcd_mgr_config mgr_config;
31 struct omap_video_timings timings;
32 int datapairs;
34 struct omap_dss_device output;
36 bool port_initialized;
37 } sdi;
39 struct sdi_clk_calc_ctx {
40 unsigned long pck_min, pck_max;
42 unsigned long fck;
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;
56 return true;
59 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
61 struct sdi_clk_calc_ctx *ctx = data;
63 ctx->fck = fck;
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,
70 unsigned long *fck,
71 struct dispc_clock_info *dispc_cinfo)
73 int i;
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
80 * +/- 1MHz.
83 for (i = 0; i < 10; ++i) {
84 bool ok;
86 memset(&ctx, 0, sizeof(ctx));
87 if (pclk > 1000 * i * i * i)
88 ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
89 else
90 ctx.pck_min = 0;
91 ctx.pck_max = pclk + 1000 * i * i * i;
93 ok = dss_div_calc(pclk, ctx.pck_min, dpi_calc_dss_cb, &ctx);
94 if (ok) {
95 *fck = ctx.fck;
96 *dispc_cinfo = ctx.dispc_cinfo;
97 return 0;
101 return -EINVAL;
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;
123 unsigned long fck;
124 struct dispc_clock_info dispc_cinfo;
125 unsigned long pck;
126 int r;
128 if (out->manager == NULL) {
129 DSSERR("failed to enable display: no output/manager\n");
130 return -ENODEV;
133 r = regulator_enable(sdi.vdds_sdi_reg);
134 if (r)
135 goto err_reg_enable;
137 r = dispc_runtime_get();
138 if (r)
139 goto err_get_dispc;
141 /* 15.5.9.1.2 */
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);
146 if (r)
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",
155 t->pixelclock, pck);
157 t->pixelclock = pck;
161 dss_mgr_set_timings(out->manager, t);
163 r = dss_set_fck_rate(fck);
164 if (r)
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();
184 if (r)
185 goto err_sdi_enable;
186 mdelay(2);
188 r = dss_mgr_enable(out->manager);
189 if (r)
190 goto err_mgr_enable;
192 return 0;
194 err_mgr_enable:
195 dss_sdi_disable();
196 err_sdi_enable:
197 err_set_dss_clock_div:
198 err_calc_clock_div:
199 dispc_runtime_put();
200 err_get_dispc:
201 regulator_disable(sdi.vdds_sdi_reg);
202 err_reg_enable:
203 return r;
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);
212 dss_sdi_disable();
214 dispc_runtime_put();
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))
237 return -EINVAL;
239 if (timings->pixelclock == 0)
240 return -EINVAL;
242 return 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)
255 return 0;
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;
266 return 0;
269 static int sdi_connect(struct omap_dss_device *dssdev,
270 struct omap_dss_device *dst)
272 struct omap_overlay_manager *mgr;
273 int r;
275 r = sdi_init_regulator();
276 if (r)
277 return r;
279 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
280 if (!mgr)
281 return -ENODEV;
283 r = dss_mgr_connect(mgr, dssdev);
284 if (r)
285 return r;
287 r = omapdss_output_set_device(dssdev, dst);
288 if (r) {
289 DSSERR("failed to connect output to new device: %s\n",
290 dst->name);
291 dss_mgr_disconnect(mgr, dssdev);
292 return r;
295 return 0;
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)
304 return;
306 omapdss_output_unset_device(dssdev);
308 if (dssdev->manager)
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;
333 out->name = "sdi.0";
334 out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
335 /* We have SDI only on OMAP3, where it's on port 1 */
336 out->port_num = 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);
354 sdi.pdev = pdev;
356 sdi_init_output(pdev);
358 return 0;
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 = {
369 .bind = sdi_bind,
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);
381 return 0;
384 static struct platform_driver omap_sdi_driver = {
385 .probe = sdi_probe,
386 .remove = sdi_remove,
387 .driver = {
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;
406 u32 datapairs;
407 int r;
409 ep = omapdss_of_get_next_endpoint(port, NULL);
410 if (!ep)
411 return 0;
413 r = of_property_read_u32(ep, "datapairs", &datapairs);
414 if (r) {
415 DSSERR("failed to parse datapairs\n");
416 goto err_datapairs;
419 sdi.datapairs = datapairs;
421 of_node_put(ep);
423 sdi.pdev = pdev;
425 sdi_init_output(pdev);
427 sdi.port_initialized = true;
429 return 0;
431 err_datapairs:
432 of_node_put(ep);
434 return r;
437 void sdi_uninit_port(struct device_node *port)
439 if (!sdi.port_initialized)
440 return;
442 sdi_uninit_output(sdi.pdev);