1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Nokia Corporation
7 #define DSS_SUBSYS_NAME "SDI"
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/export.h>
14 #include <linux/platform_device.h>
15 #include <linux/string.h>
22 struct platform_device *pdev;
23 struct dss_device *dss;
26 struct regulator *vdds_sdi_reg;
28 struct dss_lcd_mgr_config mgr_config;
29 unsigned long pixelclock;
32 struct omap_dss_device output;
35 #define dssdev_to_sdi(dssdev) container_of(dssdev, struct sdi_device, output)
37 struct sdi_clk_calc_ctx {
38 struct sdi_device *sdi;
39 unsigned long pck_min, pck_max;
42 struct dispc_clock_info dispc_cinfo;
45 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
46 unsigned long pck, void *data)
48 struct sdi_clk_calc_ctx *ctx = data;
50 ctx->dispc_cinfo.lck_div = lckd;
51 ctx->dispc_cinfo.pck_div = pckd;
52 ctx->dispc_cinfo.lck = lck;
53 ctx->dispc_cinfo.pck = pck;
58 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
60 struct sdi_clk_calc_ctx *ctx = data;
64 return dispc_div_calc(ctx->sdi->dss->dispc, fck,
65 ctx->pck_min, ctx->pck_max,
66 dpi_calc_dispc_cb, ctx);
69 static int sdi_calc_clock_div(struct sdi_device *sdi, 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));
90 if (pclk > 1000 * i * i * i)
91 ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
94 ctx.pck_max = pclk + 1000 * i * i * i;
96 ok = dss_div_calc(sdi->dss, pclk, ctx.pck_min,
97 dpi_calc_dss_cb, &ctx);
100 *dispc_cinfo = ctx.dispc_cinfo;
108 static void sdi_config_lcd_manager(struct sdi_device *sdi)
110 sdi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
112 sdi->mgr_config.stallmode = false;
113 sdi->mgr_config.fifohandcheck = false;
115 sdi->mgr_config.video_port_width = 24;
116 sdi->mgr_config.lcden_sig_polarity = 1;
118 dss_mgr_set_lcd_config(&sdi->output, &sdi->mgr_config);
121 static void sdi_display_enable(struct omap_dss_device *dssdev)
123 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
124 struct dispc_clock_info dispc_cinfo;
128 r = regulator_enable(sdi->vdds_sdi_reg);
132 r = dispc_runtime_get(sdi->dss->dispc);
136 r = sdi_calc_clock_div(sdi, sdi->pixelclock, &fck, &dispc_cinfo);
138 goto err_calc_clock_div;
140 sdi->mgr_config.clock_info = dispc_cinfo;
142 r = dss_set_fck_rate(sdi->dss, fck);
144 goto err_set_dss_clock_div;
146 sdi_config_lcd_manager(sdi);
149 * LCLK and PCLK divisors are located in shadow registers, and we
150 * normally write them to DISPC registers when enabling the output.
151 * However, SDI uses pck-free as source clock for its PLL, and pck-free
152 * is affected by the divisors. And as we need the PLL before enabling
153 * the output, we need to write the divisors early.
155 * It seems just writing to the DISPC register is enough, and we don't
156 * need to care about the shadow register mechanism for pck-free. The
157 * exact reason for this is unknown.
159 dispc_mgr_set_clock_div(sdi->dss->dispc, sdi->output.dispc_channel,
160 &sdi->mgr_config.clock_info);
162 dss_sdi_init(sdi->dss, sdi->datapairs);
163 r = dss_sdi_enable(sdi->dss);
168 r = dss_mgr_enable(&sdi->output);
175 dss_sdi_disable(sdi->dss);
177 err_set_dss_clock_div:
179 dispc_runtime_put(sdi->dss->dispc);
181 regulator_disable(sdi->vdds_sdi_reg);
184 static void sdi_display_disable(struct omap_dss_device *dssdev)
186 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
188 dss_mgr_disable(&sdi->output);
190 dss_sdi_disable(sdi->dss);
192 dispc_runtime_put(sdi->dss->dispc);
194 regulator_disable(sdi->vdds_sdi_reg);
197 static void sdi_set_timings(struct omap_dss_device *dssdev,
198 const struct drm_display_mode *mode)
200 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
202 sdi->pixelclock = mode->clock * 1000;
205 static int sdi_check_timings(struct omap_dss_device *dssdev,
206 struct drm_display_mode *mode)
208 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
209 struct dispc_clock_info dispc_cinfo;
210 unsigned long pixelclock = mode->clock * 1000;
218 r = sdi_calc_clock_div(sdi, pixelclock, &fck, &dispc_cinfo);
222 pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
224 if (pck != pixelclock) {
225 DSSWARN("Pixel clock adjusted from %lu Hz to %lu Hz\n",
228 mode->clock = pck / 1000;
234 static int sdi_connect(struct omap_dss_device *src,
235 struct omap_dss_device *dst)
237 return omapdss_device_connect(dst->dss, dst, dst->next);
240 static void sdi_disconnect(struct omap_dss_device *src,
241 struct omap_dss_device *dst)
243 omapdss_device_disconnect(dst, dst->next);
246 static const struct omap_dss_device_ops sdi_ops = {
247 .connect = sdi_connect,
248 .disconnect = sdi_disconnect,
250 .enable = sdi_display_enable,
251 .disable = sdi_display_disable,
253 .check_timings = sdi_check_timings,
254 .set_timings = sdi_set_timings,
257 static int sdi_init_output(struct sdi_device *sdi)
259 struct omap_dss_device *out = &sdi->output;
262 out->dev = &sdi->pdev->dev;
263 out->id = OMAP_DSS_OUTPUT_SDI;
264 out->type = OMAP_DISPLAY_TYPE_SDI;
266 out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
267 /* We have SDI only on OMAP3, where it's on port 1 */
268 out->of_ports = BIT(1);
270 out->owner = THIS_MODULE;
271 out->bus_flags = DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE /* 15.5.9.1.2 */
272 | DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
274 r = omapdss_device_init_output(out);
278 omapdss_device_register(out);
283 static void sdi_uninit_output(struct sdi_device *sdi)
285 omapdss_device_unregister(&sdi->output);
286 omapdss_device_cleanup_output(&sdi->output);
289 int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
290 struct device_node *port)
292 struct sdi_device *sdi;
293 struct device_node *ep;
297 sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
301 ep = of_get_next_child(port, NULL);
307 r = of_property_read_u32(ep, "datapairs", &datapairs);
310 DSSERR("failed to parse datapairs\n");
314 sdi->datapairs = datapairs;
320 sdi->vdds_sdi_reg = devm_regulator_get(&pdev->dev, "vdds_sdi");
321 if (IS_ERR(sdi->vdds_sdi_reg)) {
322 r = PTR_ERR(sdi->vdds_sdi_reg);
323 if (r != -EPROBE_DEFER)
324 DSSERR("can't get VDDS_SDI regulator\n");
328 r = sdi_init_output(sdi);
340 void sdi_uninit_port(struct device_node *port)
342 struct sdi_device *sdi = port->data;
347 sdi_uninit_output(sdi);