OMAPDSS: DPI: Maintain copy of number of data lines in driver data
[linux-3.10.git] / drivers / video / omap2 / dss / dpi.c
1 /*
2  * linux/drivers/video/omap2/dss/dpi.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * Some code and ideas taken from drivers/video/omap/ driver
8  * by Imre Deak.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published by
12  * the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #define DSS_SUBSYS_NAME "DPI"
24
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/export.h>
28 #include <linux/err.h>
29 #include <linux/errno.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/consumer.h>
32
33 #include <video/omapdss.h>
34 #include <plat/cpu.h>
35
36 #include "dss.h"
37
38 static struct {
39         struct regulator *vdds_dsi_reg;
40         struct platform_device *dsidev;
41
42         struct mutex lock;
43
44         struct omap_video_timings timings;
45         struct dss_lcd_mgr_config mgr_config;
46         int data_lines;
47 } dpi;
48
49 static struct platform_device *dpi_get_dsidev(enum omap_dss_clk_source clk)
50 {
51         int dsi_module;
52
53         dsi_module = clk == OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ? 0 : 1;
54
55         return dsi_get_dsidev_from_id(dsi_module);
56 }
57
58 static bool dpi_use_dsi_pll(struct omap_dss_device *dssdev)
59 {
60         if (dssdev->clocks.dispc.dispc_fclk_src ==
61                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
62                         dssdev->clocks.dispc.dispc_fclk_src ==
63                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC ||
64                         dssdev->clocks.dispc.channel.lcd_clk_src ==
65                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
66                         dssdev->clocks.dispc.channel.lcd_clk_src ==
67                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC)
68                 return true;
69         else
70                 return false;
71 }
72
73 static int dpi_set_dsi_clk(struct omap_dss_device *dssdev,
74                 unsigned long pck_req, unsigned long *fck, int *lck_div,
75                 int *pck_div)
76 {
77         struct dsi_clock_info dsi_cinfo;
78         struct dispc_clock_info dispc_cinfo;
79         int r;
80
81         r = dsi_pll_calc_clock_div_pck(dpi.dsidev, pck_req, &dsi_cinfo,
82                         &dispc_cinfo);
83         if (r)
84                 return r;
85
86         r = dsi_pll_set_clock_div(dpi.dsidev, &dsi_cinfo);
87         if (r)
88                 return r;
89
90         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
91
92         dpi.mgr_config.clock_info = dispc_cinfo;
93
94         *fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
95         *lck_div = dispc_cinfo.lck_div;
96         *pck_div = dispc_cinfo.pck_div;
97
98         return 0;
99 }
100
101 static int dpi_set_dispc_clk(struct omap_dss_device *dssdev,
102                 unsigned long pck_req, unsigned long *fck, int *lck_div,
103                 int *pck_div)
104 {
105         struct dss_clock_info dss_cinfo;
106         struct dispc_clock_info dispc_cinfo;
107         int r;
108
109         r = dss_calc_clock_div(pck_req, &dss_cinfo, &dispc_cinfo);
110         if (r)
111                 return r;
112
113         r = dss_set_clock_div(&dss_cinfo);
114         if (r)
115                 return r;
116
117         dpi.mgr_config.clock_info = dispc_cinfo;
118
119         *fck = dss_cinfo.fck;
120         *lck_div = dispc_cinfo.lck_div;
121         *pck_div = dispc_cinfo.pck_div;
122
123         return 0;
124 }
125
126 static int dpi_set_mode(struct omap_dss_device *dssdev)
127 {
128         struct omap_video_timings *t = &dpi.timings;
129         int lck_div = 0, pck_div = 0;
130         unsigned long fck = 0;
131         unsigned long pck;
132         int r = 0;
133
134         if (dpi_use_dsi_pll(dssdev))
135                 r = dpi_set_dsi_clk(dssdev, t->pixel_clock * 1000, &fck,
136                                 &lck_div, &pck_div);
137         else
138                 r = dpi_set_dispc_clk(dssdev, t->pixel_clock * 1000, &fck,
139                                 &lck_div, &pck_div);
140         if (r)
141                 return r;
142
143         pck = fck / lck_div / pck_div / 1000;
144
145         if (pck != t->pixel_clock) {
146                 DSSWARN("Could not find exact pixel clock. "
147                                 "Requested %d kHz, got %lu kHz\n",
148                                 t->pixel_clock, pck);
149
150                 t->pixel_clock = pck;
151         }
152
153         dss_mgr_set_timings(dssdev->manager, t);
154
155         return 0;
156 }
157
158 static void dpi_config_lcd_manager(struct omap_dss_device *dssdev)
159 {
160         dpi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
161
162         dpi.mgr_config.stallmode = false;
163         dpi.mgr_config.fifohandcheck = false;
164
165         dpi.mgr_config.video_port_width = dpi.data_lines;
166
167         dpi.mgr_config.lcden_sig_polarity = 0;
168
169         dss_mgr_set_lcd_config(dssdev->manager, &dpi.mgr_config);
170 }
171
172 int omapdss_dpi_display_enable(struct omap_dss_device *dssdev)
173 {
174         int r;
175
176         mutex_lock(&dpi.lock);
177
178         if (cpu_is_omap34xx() && !dpi.vdds_dsi_reg) {
179                 DSSERR("no VDSS_DSI regulator\n");
180                 r = -ENODEV;
181                 goto err_no_reg;
182         }
183
184         if (dssdev->manager == NULL) {
185                 DSSERR("failed to enable display: no manager\n");
186                 r = -ENODEV;
187                 goto err_no_mgr;
188         }
189
190         r = omap_dss_start_device(dssdev);
191         if (r) {
192                 DSSERR("failed to start device\n");
193                 goto err_start_dev;
194         }
195
196         if (cpu_is_omap34xx()) {
197                 r = regulator_enable(dpi.vdds_dsi_reg);
198                 if (r)
199                         goto err_reg_enable;
200         }
201
202         r = dispc_runtime_get();
203         if (r)
204                 goto err_get_dispc;
205
206         if (dpi_use_dsi_pll(dssdev)) {
207                 r = dsi_runtime_get(dpi.dsidev);
208                 if (r)
209                         goto err_get_dsi;
210
211                 r = dsi_pll_init(dpi.dsidev, 0, 1);
212                 if (r)
213                         goto err_dsi_pll_init;
214         }
215
216         r = dpi_set_mode(dssdev);
217         if (r)
218                 goto err_set_mode;
219
220         dpi_config_lcd_manager(dssdev);
221
222         mdelay(2);
223
224         r = dss_mgr_enable(dssdev->manager);
225         if (r)
226                 goto err_mgr_enable;
227
228         mutex_unlock(&dpi.lock);
229
230         return 0;
231
232 err_mgr_enable:
233 err_set_mode:
234         if (dpi_use_dsi_pll(dssdev))
235                 dsi_pll_uninit(dpi.dsidev, true);
236 err_dsi_pll_init:
237         if (dpi_use_dsi_pll(dssdev))
238                 dsi_runtime_put(dpi.dsidev);
239 err_get_dsi:
240         dispc_runtime_put();
241 err_get_dispc:
242         if (cpu_is_omap34xx())
243                 regulator_disable(dpi.vdds_dsi_reg);
244 err_reg_enable:
245         omap_dss_stop_device(dssdev);
246 err_start_dev:
247 err_no_mgr:
248 err_no_reg:
249         mutex_unlock(&dpi.lock);
250         return r;
251 }
252 EXPORT_SYMBOL(omapdss_dpi_display_enable);
253
254 void omapdss_dpi_display_disable(struct omap_dss_device *dssdev)
255 {
256         mutex_lock(&dpi.lock);
257
258         dss_mgr_disable(dssdev->manager);
259
260         if (dpi_use_dsi_pll(dssdev)) {
261                 dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
262                 dsi_pll_uninit(dpi.dsidev, true);
263                 dsi_runtime_put(dpi.dsidev);
264         }
265
266         dispc_runtime_put();
267
268         if (cpu_is_omap34xx())
269                 regulator_disable(dpi.vdds_dsi_reg);
270
271         omap_dss_stop_device(dssdev);
272
273         mutex_unlock(&dpi.lock);
274 }
275 EXPORT_SYMBOL(omapdss_dpi_display_disable);
276
277 void omapdss_dpi_set_timings(struct omap_dss_device *dssdev,
278                 struct omap_video_timings *timings)
279 {
280         int r;
281
282         DSSDBG("dpi_set_timings\n");
283
284         mutex_lock(&dpi.lock);
285
286         dpi.timings = *timings;
287
288         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
289                 r = dispc_runtime_get();
290                 if (r)
291                         return;
292
293                 dpi_set_mode(dssdev);
294
295                 dispc_runtime_put();
296         } else {
297                 dss_mgr_set_timings(dssdev->manager, timings);
298         }
299
300         mutex_unlock(&dpi.lock);
301 }
302 EXPORT_SYMBOL(omapdss_dpi_set_timings);
303
304 int dpi_check_timings(struct omap_dss_device *dssdev,
305                         struct omap_video_timings *timings)
306 {
307         int r;
308         int lck_div, pck_div;
309         unsigned long fck;
310         unsigned long pck;
311         struct dispc_clock_info dispc_cinfo;
312
313         if (dss_mgr_check_timings(dssdev->manager, timings))
314                 return -EINVAL;
315
316         if (timings->pixel_clock == 0)
317                 return -EINVAL;
318
319         if (dpi_use_dsi_pll(dssdev)) {
320                 struct dsi_clock_info dsi_cinfo;
321                 r = dsi_pll_calc_clock_div_pck(dpi.dsidev,
322                                 timings->pixel_clock * 1000,
323                                 &dsi_cinfo, &dispc_cinfo);
324
325                 if (r)
326                         return r;
327
328                 fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
329         } else {
330                 struct dss_clock_info dss_cinfo;
331                 r = dss_calc_clock_div(timings->pixel_clock * 1000,
332                                 &dss_cinfo, &dispc_cinfo);
333
334                 if (r)
335                         return r;
336
337                 fck = dss_cinfo.fck;
338         }
339
340         lck_div = dispc_cinfo.lck_div;
341         pck_div = dispc_cinfo.pck_div;
342
343         pck = fck / lck_div / pck_div / 1000;
344
345         timings->pixel_clock = pck;
346
347         return 0;
348 }
349 EXPORT_SYMBOL(dpi_check_timings);
350
351 void omapdss_dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
352 {
353         mutex_lock(&dpi.lock);
354
355         dpi.data_lines = data_lines;
356
357         mutex_unlock(&dpi.lock);
358 }
359 EXPORT_SYMBOL(omapdss_dpi_set_data_lines);
360
361 static int __init dpi_init_display(struct omap_dss_device *dssdev)
362 {
363         DSSDBG("init_display\n");
364
365         if (cpu_is_omap34xx() && dpi.vdds_dsi_reg == NULL) {
366                 struct regulator *vdds_dsi;
367
368                 vdds_dsi = dss_get_vdds_dsi();
369
370                 if (IS_ERR(vdds_dsi)) {
371                         DSSERR("can't get VDDS_DSI regulator\n");
372                         return PTR_ERR(vdds_dsi);
373                 }
374
375                 dpi.vdds_dsi_reg = vdds_dsi;
376         }
377
378         if (dpi_use_dsi_pll(dssdev)) {
379                 enum omap_dss_clk_source dispc_fclk_src =
380                         dssdev->clocks.dispc.dispc_fclk_src;
381                 dpi.dsidev = dpi_get_dsidev(dispc_fclk_src);
382         }
383
384         return 0;
385 }
386
387 static void __init dpi_probe_pdata(struct platform_device *pdev)
388 {
389         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
390         int i, r;
391
392         for (i = 0; i < pdata->num_devices; ++i) {
393                 struct omap_dss_device *dssdev = pdata->devices[i];
394
395                 if (dssdev->type != OMAP_DISPLAY_TYPE_DPI)
396                         continue;
397
398                 r = dpi_init_display(dssdev);
399                 if (r) {
400                         DSSERR("device %s init failed: %d\n", dssdev->name, r);
401                         continue;
402                 }
403
404                 r = omap_dss_register_device(dssdev, &pdev->dev, i);
405                 if (r)
406                         DSSERR("device %s register failed: %d\n",
407                                         dssdev->name, r);
408         }
409 }
410
411 static int __init omap_dpi_probe(struct platform_device *pdev)
412 {
413         mutex_init(&dpi.lock);
414
415         dpi_probe_pdata(pdev);
416
417         return 0;
418 }
419
420 static int __exit omap_dpi_remove(struct platform_device *pdev)
421 {
422         omap_dss_unregister_child_devices(&pdev->dev);
423
424         return 0;
425 }
426
427 static struct platform_driver omap_dpi_driver = {
428         .remove         = __exit_p(omap_dpi_remove),
429         .driver         = {
430                 .name   = "omapdss_dpi",
431                 .owner  = THIS_MODULE,
432         },
433 };
434
435 int __init dpi_init_platform_driver(void)
436 {
437         return platform_driver_probe(&omap_dpi_driver, omap_dpi_probe);
438 }
439
440 void __exit dpi_uninit_platform_driver(void)
441 {
442         platform_driver_unregister(&omap_dpi_driver);
443 }