Merge git://git.infradead.org/users/eparis/audit
[cascardo/linux.git] / drivers / video / omap2 / dss / sdi.c
1 /*
2  * linux/drivers/video/omap2/dss/sdi.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #define DSS_SUBSYS_NAME "SDI"
21
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/export.h>
27 #include <linux/platform_device.h>
28 #include <linux/string.h>
29
30 #include <video/omapdss.h>
31 #include "dss.h"
32
33 static struct {
34         bool update_enabled;
35         struct regulator *vdds_sdi_reg;
36
37         struct dss_lcd_mgr_config mgr_config;
38         struct omap_video_timings timings;
39         int datapairs;
40
41         struct omap_dss_output output;
42 } sdi;
43
44 struct sdi_clk_calc_ctx {
45         unsigned long pck_min, pck_max;
46
47         struct dss_clock_info dss_cinfo;
48         struct dispc_clock_info dispc_cinfo;
49 };
50
51 static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
52                 unsigned long pck, void *data)
53 {
54         struct sdi_clk_calc_ctx *ctx = data;
55
56         ctx->dispc_cinfo.lck_div = lckd;
57         ctx->dispc_cinfo.pck_div = pckd;
58         ctx->dispc_cinfo.lck = lck;
59         ctx->dispc_cinfo.pck = pck;
60
61         return true;
62 }
63
64 static bool dpi_calc_dss_cb(int fckd, unsigned long fck, void *data)
65 {
66         struct sdi_clk_calc_ctx *ctx = data;
67
68         ctx->dss_cinfo.fck = fck;
69         ctx->dss_cinfo.fck_div = fckd;
70
71         return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
72                         dpi_calc_dispc_cb, ctx);
73 }
74
75 static int sdi_calc_clock_div(unsigned long pclk,
76                 struct dss_clock_info *dss_cinfo,
77                 struct dispc_clock_info *dispc_cinfo)
78 {
79         int i;
80         struct sdi_clk_calc_ctx ctx;
81
82         /*
83          * DSS fclk gives us very few possibilities, so finding a good pixel
84          * clock may not be possible. We try multiple times to find the clock,
85          * each time widening the pixel clock range we look for, up to
86          * +/- 1MHz.
87          */
88
89         for (i = 0; i < 10; ++i) {
90                 bool ok;
91
92                 memset(&ctx, 0, sizeof(ctx));
93                 if (pclk > 1000 * i * i * i)
94                         ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
95                 else
96                         ctx.pck_min = 0;
97                 ctx.pck_max = pclk + 1000 * i * i * i;
98
99                 ok = dss_div_calc(ctx.pck_min, dpi_calc_dss_cb, &ctx);
100                 if (ok) {
101                         *dss_cinfo = ctx.dss_cinfo;
102                         *dispc_cinfo = ctx.dispc_cinfo;
103                         return 0;
104                 }
105         }
106
107         return -EINVAL;
108 }
109
110 static void sdi_config_lcd_manager(struct omap_dss_device *dssdev)
111 {
112         struct omap_overlay_manager *mgr = dssdev->output->manager;
113
114         sdi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
115
116         sdi.mgr_config.stallmode = false;
117         sdi.mgr_config.fifohandcheck = false;
118
119         sdi.mgr_config.video_port_width = 24;
120         sdi.mgr_config.lcden_sig_polarity = 1;
121
122         dss_mgr_set_lcd_config(mgr, &sdi.mgr_config);
123 }
124
125 int omapdss_sdi_display_enable(struct omap_dss_device *dssdev)
126 {
127         struct omap_dss_output *out = dssdev->output;
128         struct omap_video_timings *t = &sdi.timings;
129         struct dss_clock_info dss_cinfo;
130         struct dispc_clock_info dispc_cinfo;
131         unsigned long pck;
132         int r;
133
134         if (out == NULL || out->manager == NULL) {
135                 DSSERR("failed to enable display: no output/manager\n");
136                 return -ENODEV;
137         }
138
139         r = omap_dss_start_device(dssdev);
140         if (r) {
141                 DSSERR("failed to start device\n");
142                 goto err_start_dev;
143         }
144
145         r = regulator_enable(sdi.vdds_sdi_reg);
146         if (r)
147                 goto err_reg_enable;
148
149         r = dispc_runtime_get();
150         if (r)
151                 goto err_get_dispc;
152
153         /* 15.5.9.1.2 */
154         t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
155         t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
156
157         r = sdi_calc_clock_div(t->pixel_clock * 1000, &dss_cinfo, &dispc_cinfo);
158         if (r)
159                 goto err_calc_clock_div;
160
161         sdi.mgr_config.clock_info = dispc_cinfo;
162
163         pck = dss_cinfo.fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div / 1000;
164
165         if (pck != t->pixel_clock) {
166                 DSSWARN("Could not find exact pixel clock. Requested %d kHz, "
167                                 "got %lu kHz\n",
168                                 t->pixel_clock, pck);
169
170                 t->pixel_clock = pck;
171         }
172
173
174         dss_mgr_set_timings(out->manager, t);
175
176         r = dss_set_clock_div(&dss_cinfo);
177         if (r)
178                 goto err_set_dss_clock_div;
179
180         sdi_config_lcd_manager(dssdev);
181
182         /*
183          * LCLK and PCLK divisors are located in shadow registers, and we
184          * normally write them to DISPC registers when enabling the output.
185          * However, SDI uses pck-free as source clock for its PLL, and pck-free
186          * is affected by the divisors. And as we need the PLL before enabling
187          * the output, we need to write the divisors early.
188          *
189          * It seems just writing to the DISPC register is enough, and we don't
190          * need to care about the shadow register mechanism for pck-free. The
191          * exact reason for this is unknown.
192          */
193         dispc_mgr_set_clock_div(out->manager->id, &sdi.mgr_config.clock_info);
194
195         dss_sdi_init(sdi.datapairs);
196         r = dss_sdi_enable();
197         if (r)
198                 goto err_sdi_enable;
199         mdelay(2);
200
201         r = dss_mgr_enable(out->manager);
202         if (r)
203                 goto err_mgr_enable;
204
205         return 0;
206
207 err_mgr_enable:
208         dss_sdi_disable();
209 err_sdi_enable:
210 err_set_dss_clock_div:
211 err_calc_clock_div:
212         dispc_runtime_put();
213 err_get_dispc:
214         regulator_disable(sdi.vdds_sdi_reg);
215 err_reg_enable:
216         omap_dss_stop_device(dssdev);
217 err_start_dev:
218         return r;
219 }
220 EXPORT_SYMBOL(omapdss_sdi_display_enable);
221
222 void omapdss_sdi_display_disable(struct omap_dss_device *dssdev)
223 {
224         struct omap_overlay_manager *mgr = dssdev->output->manager;
225
226         dss_mgr_disable(mgr);
227
228         dss_sdi_disable();
229
230         dispc_runtime_put();
231
232         regulator_disable(sdi.vdds_sdi_reg);
233
234         omap_dss_stop_device(dssdev);
235 }
236 EXPORT_SYMBOL(omapdss_sdi_display_disable);
237
238 void omapdss_sdi_set_timings(struct omap_dss_device *dssdev,
239                 struct omap_video_timings *timings)
240 {
241         sdi.timings = *timings;
242 }
243 EXPORT_SYMBOL(omapdss_sdi_set_timings);
244
245 void omapdss_sdi_set_datapairs(struct omap_dss_device *dssdev, int datapairs)
246 {
247         sdi.datapairs = datapairs;
248 }
249 EXPORT_SYMBOL(omapdss_sdi_set_datapairs);
250
251 static int sdi_init_display(struct omap_dss_device *dssdev)
252 {
253         DSSDBG("SDI init\n");
254
255         if (sdi.vdds_sdi_reg == NULL) {
256                 struct regulator *vdds_sdi;
257
258                 vdds_sdi = dss_get_vdds_sdi();
259
260                 if (IS_ERR(vdds_sdi)) {
261                         DSSERR("can't get VDDS_SDI regulator\n");
262                         return PTR_ERR(vdds_sdi);
263                 }
264
265                 sdi.vdds_sdi_reg = vdds_sdi;
266         }
267
268         return 0;
269 }
270
271 static struct omap_dss_device *sdi_find_dssdev(struct platform_device *pdev)
272 {
273         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
274         const char *def_disp_name = omapdss_get_default_display_name();
275         struct omap_dss_device *def_dssdev;
276         int i;
277
278         def_dssdev = NULL;
279
280         for (i = 0; i < pdata->num_devices; ++i) {
281                 struct omap_dss_device *dssdev = pdata->devices[i];
282
283                 if (dssdev->type != OMAP_DISPLAY_TYPE_SDI)
284                         continue;
285
286                 if (def_dssdev == NULL)
287                         def_dssdev = dssdev;
288
289                 if (def_disp_name != NULL &&
290                                 strcmp(dssdev->name, def_disp_name) == 0) {
291                         def_dssdev = dssdev;
292                         break;
293                 }
294         }
295
296         return def_dssdev;
297 }
298
299 static int sdi_probe_pdata(struct platform_device *sdidev)
300 {
301         struct omap_dss_device *plat_dssdev;
302         struct omap_dss_device *dssdev;
303         int r;
304
305         plat_dssdev = sdi_find_dssdev(sdidev);
306
307         if (!plat_dssdev)
308                 return 0;
309
310         dssdev = dss_alloc_and_init_device(&sdidev->dev);
311         if (!dssdev)
312                 return -ENOMEM;
313
314         dss_copy_device_pdata(dssdev, plat_dssdev);
315
316         r = sdi_init_display(dssdev);
317         if (r) {
318                 DSSERR("device %s init failed: %d\n", dssdev->name, r);
319                 dss_put_device(dssdev);
320                 return r;
321         }
322
323         r = omapdss_output_set_device(&sdi.output, dssdev);
324         if (r) {
325                 DSSERR("failed to connect output to new device: %s\n",
326                                 dssdev->name);
327                 dss_put_device(dssdev);
328                 return r;
329         }
330
331         r = dss_add_device(dssdev);
332         if (r) {
333                 DSSERR("device %s register failed: %d\n", dssdev->name, r);
334                 omapdss_output_unset_device(&sdi.output);
335                 dss_put_device(dssdev);
336                 return r;
337         }
338
339         return 0;
340 }
341
342 static void sdi_init_output(struct platform_device *pdev)
343 {
344         struct omap_dss_output *out = &sdi.output;
345
346         out->pdev = pdev;
347         out->id = OMAP_DSS_OUTPUT_SDI;
348         out->type = OMAP_DISPLAY_TYPE_SDI;
349         out->name = "sdi.0";
350         out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
351
352         dss_register_output(out);
353 }
354
355 static void __exit sdi_uninit_output(struct platform_device *pdev)
356 {
357         struct omap_dss_output *out = &sdi.output;
358
359         dss_unregister_output(out);
360 }
361
362 static int omap_sdi_probe(struct platform_device *pdev)
363 {
364         int r;
365
366         sdi_init_output(pdev);
367
368         r = sdi_probe_pdata(pdev);
369         if (r) {
370                 sdi_uninit_output(pdev);
371                 return r;
372         }
373
374         return 0;
375 }
376
377 static int __exit omap_sdi_remove(struct platform_device *pdev)
378 {
379         dss_unregister_child_devices(&pdev->dev);
380
381         sdi_uninit_output(pdev);
382
383         return 0;
384 }
385
386 static struct platform_driver omap_sdi_driver = {
387         .probe          = omap_sdi_probe,
388         .remove         = __exit_p(omap_sdi_remove),
389         .driver         = {
390                 .name   = "omapdss_sdi",
391                 .owner  = THIS_MODULE,
392         },
393 };
394
395 int __init sdi_init_platform_driver(void)
396 {
397         return platform_driver_register(&omap_sdi_driver);
398 }
399
400 void __exit sdi_uninit_platform_driver(void)
401 {
402         platform_driver_unregister(&omap_sdi_driver);
403 }