2 * Copyright (C) 2009 Nokia Corporation
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #define DSS_SUBSYS_NAME "SDI"
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/export.h>
25 #include <linux/platform_device.h>
26 #include <linux/string.h>
33 struct platform_device *pdev;
34 struct dss_device *dss;
37 struct regulator *vdds_sdi_reg;
39 struct dss_lcd_mgr_config mgr_config;
43 struct omap_dss_device output;
46 #define dssdev_to_sdi(dssdev) container_of(dssdev, struct sdi_device, output)
48 struct sdi_clk_calc_ctx {
49 struct sdi_device *sdi;
50 unsigned long pck_min, pck_max;
53 struct dispc_clock_info dispc_cinfo;
56 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
57 unsigned long pck, void *data)
59 struct sdi_clk_calc_ctx *ctx = data;
61 ctx->dispc_cinfo.lck_div = lckd;
62 ctx->dispc_cinfo.pck_div = pckd;
63 ctx->dispc_cinfo.lck = lck;
64 ctx->dispc_cinfo.pck = pck;
69 static bool dpi_calc_dss_cb(unsigned long fck, void *data)
71 struct sdi_clk_calc_ctx *ctx = data;
75 return dispc_div_calc(ctx->sdi->dss->dispc, fck,
76 ctx->pck_min, ctx->pck_max,
77 dpi_calc_dispc_cb, ctx);
80 static int sdi_calc_clock_div(struct sdi_device *sdi, unsigned long pclk,
82 struct dispc_clock_info *dispc_cinfo)
85 struct sdi_clk_calc_ctx ctx;
88 * DSS fclk gives us very few possibilities, so finding a good pixel
89 * clock may not be possible. We try multiple times to find the clock,
90 * each time widening the pixel clock range we look for, up to
94 for (i = 0; i < 10; ++i) {
97 memset(&ctx, 0, sizeof(ctx));
101 if (pclk > 1000 * i * i * i)
102 ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
105 ctx.pck_max = pclk + 1000 * i * i * i;
107 ok = dss_div_calc(sdi->dss, pclk, ctx.pck_min,
108 dpi_calc_dss_cb, &ctx);
111 *dispc_cinfo = ctx.dispc_cinfo;
119 static void sdi_config_lcd_manager(struct sdi_device *sdi)
121 sdi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
123 sdi->mgr_config.stallmode = false;
124 sdi->mgr_config.fifohandcheck = false;
126 sdi->mgr_config.video_port_width = 24;
127 sdi->mgr_config.lcden_sig_polarity = 1;
129 dss_mgr_set_lcd_config(&sdi->output, &sdi->mgr_config);
132 static int sdi_display_enable(struct omap_dss_device *dssdev)
134 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
135 struct dispc_clock_info dispc_cinfo;
139 if (!sdi->output.dispc_channel_connected) {
140 DSSERR("failed to enable display: no output/manager\n");
144 r = regulator_enable(sdi->vdds_sdi_reg);
148 r = dispc_runtime_get(sdi->dss->dispc);
152 r = sdi_calc_clock_div(sdi, sdi->vm.pixelclock, &fck, &dispc_cinfo);
154 goto err_calc_clock_div;
156 sdi->mgr_config.clock_info = dispc_cinfo;
158 r = dss_set_fck_rate(sdi->dss, fck);
160 goto err_set_dss_clock_div;
162 sdi_config_lcd_manager(sdi);
165 * LCLK and PCLK divisors are located in shadow registers, and we
166 * normally write them to DISPC registers when enabling the output.
167 * However, SDI uses pck-free as source clock for its PLL, and pck-free
168 * is affected by the divisors. And as we need the PLL before enabling
169 * the output, we need to write the divisors early.
171 * It seems just writing to the DISPC register is enough, and we don't
172 * need to care about the shadow register mechanism for pck-free. The
173 * exact reason for this is unknown.
175 dispc_mgr_set_clock_div(sdi->dss->dispc, sdi->output.dispc_channel,
176 &sdi->mgr_config.clock_info);
178 dss_sdi_init(sdi->dss, sdi->datapairs);
179 r = dss_sdi_enable(sdi->dss);
184 r = dss_mgr_enable(&sdi->output);
191 dss_sdi_disable(sdi->dss);
193 err_set_dss_clock_div:
195 dispc_runtime_put(sdi->dss->dispc);
197 regulator_disable(sdi->vdds_sdi_reg);
202 static void sdi_display_disable(struct omap_dss_device *dssdev)
204 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
206 dss_mgr_disable(&sdi->output);
208 dss_sdi_disable(sdi->dss);
210 dispc_runtime_put(sdi->dss->dispc);
212 regulator_disable(sdi->vdds_sdi_reg);
215 static void sdi_set_timings(struct omap_dss_device *dssdev,
216 const struct videomode *vm)
218 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
223 static int sdi_check_timings(struct omap_dss_device *dssdev,
224 struct videomode *vm)
226 struct sdi_device *sdi = dssdev_to_sdi(dssdev);
227 struct dispc_clock_info dispc_cinfo;
232 if (vm->pixelclock == 0)
235 r = sdi_calc_clock_div(sdi, vm->pixelclock, &fck, &dispc_cinfo);
239 pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
241 if (pck != vm->pixelclock) {
242 DSSWARN("Pixel clock adjusted from %lu Hz to %lu Hz\n",
243 vm->pixelclock, pck);
245 vm->pixelclock = pck;
251 static int sdi_connect(struct omap_dss_device *src,
252 struct omap_dss_device *dst)
256 r = omapdss_device_connect(dst->dss, dst, dst->next);
260 dst->dispc_channel_connected = true;
264 static void sdi_disconnect(struct omap_dss_device *src,
265 struct omap_dss_device *dst)
267 dst->dispc_channel_connected = false;
269 omapdss_device_disconnect(dst, dst->next);
272 static const struct omap_dss_device_ops sdi_ops = {
273 .connect = sdi_connect,
274 .disconnect = sdi_disconnect,
276 .enable = sdi_display_enable,
277 .disable = sdi_display_disable,
279 .check_timings = sdi_check_timings,
280 .set_timings = sdi_set_timings,
283 static int sdi_init_output(struct sdi_device *sdi)
285 struct omap_dss_device *out = &sdi->output;
288 out->dev = &sdi->pdev->dev;
289 out->id = OMAP_DSS_OUTPUT_SDI;
290 out->output_type = OMAP_DISPLAY_TYPE_SDI;
292 out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
293 /* We have SDI only on OMAP3, where it's on port 1 */
294 out->of_ports = BIT(1);
296 out->owner = THIS_MODULE;
297 out->bus_flags = DRM_BUS_FLAG_PIXDATA_POSEDGE /* 15.5.9.1.2 */
298 | DRM_BUS_FLAG_SYNC_POSEDGE;
300 out->next = omapdss_of_find_connected_device(out->dev->of_node, 1);
301 if (IS_ERR(out->next)) {
302 if (PTR_ERR(out->next) != -EPROBE_DEFER)
303 dev_err(out->dev, "failed to find video sink\n");
304 return PTR_ERR(out->next);
307 r = omapdss_output_validate(out);
309 omapdss_device_put(out->next);
314 omapdss_device_register(out);
319 static void sdi_uninit_output(struct sdi_device *sdi)
321 if (sdi->output.next)
322 omapdss_device_put(sdi->output.next);
323 omapdss_device_unregister(&sdi->output);
326 int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
327 struct device_node *port)
329 struct sdi_device *sdi;
330 struct device_node *ep;
334 sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
338 ep = of_get_next_child(port, NULL);
344 r = of_property_read_u32(ep, "datapairs", &datapairs);
347 DSSERR("failed to parse datapairs\n");
351 sdi->datapairs = datapairs;
357 sdi->vdds_sdi_reg = devm_regulator_get(&pdev->dev, "vdds_sdi");
358 if (IS_ERR(sdi->vdds_sdi_reg)) {
359 r = PTR_ERR(sdi->vdds_sdi_reg);
360 if (r != -EPROBE_DEFER)
361 DSSERR("can't get VDDS_SDI regulator\n");
365 r = sdi_init_output(sdi);
377 void sdi_uninit_port(struct device_node *port)
379 struct sdi_device *sdi = port->data;
384 sdi_uninit_output(sdi);