OMAPDSS: DPI: Maintain our own timings field 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 } dpi;
47
48 static struct platform_device *dpi_get_dsidev(enum omap_dss_clk_source clk)
49 {
50         int dsi_module;
51
52         dsi_module = clk == OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ? 0 : 1;
53
54         return dsi_get_dsidev_from_id(dsi_module);
55 }
56
57 static bool dpi_use_dsi_pll(struct omap_dss_device *dssdev)
58 {
59         if (dssdev->clocks.dispc.dispc_fclk_src ==
60                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
61                         dssdev->clocks.dispc.dispc_fclk_src ==
62                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC ||
63                         dssdev->clocks.dispc.channel.lcd_clk_src ==
64                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
65                         dssdev->clocks.dispc.channel.lcd_clk_src ==
66                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC)
67                 return true;
68         else
69                 return false;
70 }
71
72 static int dpi_set_dsi_clk(struct omap_dss_device *dssdev,
73                 unsigned long pck_req, unsigned long *fck, int *lck_div,
74                 int *pck_div)
75 {
76         struct dsi_clock_info dsi_cinfo;
77         struct dispc_clock_info dispc_cinfo;
78         int r;
79
80         r = dsi_pll_calc_clock_div_pck(dpi.dsidev, pck_req, &dsi_cinfo,
81                         &dispc_cinfo);
82         if (r)
83                 return r;
84
85         r = dsi_pll_set_clock_div(dpi.dsidev, &dsi_cinfo);
86         if (r)
87                 return r;
88
89         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
90
91         dpi.mgr_config.clock_info = dispc_cinfo;
92
93         *fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
94         *lck_div = dispc_cinfo.lck_div;
95         *pck_div = dispc_cinfo.pck_div;
96
97         return 0;
98 }
99
100 static int dpi_set_dispc_clk(struct omap_dss_device *dssdev,
101                 unsigned long pck_req, unsigned long *fck, int *lck_div,
102                 int *pck_div)
103 {
104         struct dss_clock_info dss_cinfo;
105         struct dispc_clock_info dispc_cinfo;
106         int r;
107
108         r = dss_calc_clock_div(pck_req, &dss_cinfo, &dispc_cinfo);
109         if (r)
110                 return r;
111
112         r = dss_set_clock_div(&dss_cinfo);
113         if (r)
114                 return r;
115
116         dpi.mgr_config.clock_info = dispc_cinfo;
117
118         *fck = dss_cinfo.fck;
119         *lck_div = dispc_cinfo.lck_div;
120         *pck_div = dispc_cinfo.pck_div;
121
122         return 0;
123 }
124
125 static int dpi_set_mode(struct omap_dss_device *dssdev)
126 {
127         struct omap_video_timings *t = &dpi.timings;
128         int lck_div = 0, pck_div = 0;
129         unsigned long fck = 0;
130         unsigned long pck;
131         int r = 0;
132
133         if (dpi_use_dsi_pll(dssdev))
134                 r = dpi_set_dsi_clk(dssdev, t->pixel_clock * 1000, &fck,
135                                 &lck_div, &pck_div);
136         else
137                 r = dpi_set_dispc_clk(dssdev, t->pixel_clock * 1000, &fck,
138                                 &lck_div, &pck_div);
139         if (r)
140                 return r;
141
142         pck = fck / lck_div / pck_div / 1000;
143
144         if (pck != t->pixel_clock) {
145                 DSSWARN("Could not find exact pixel clock. "
146                                 "Requested %d kHz, got %lu kHz\n",
147                                 t->pixel_clock, pck);
148
149                 t->pixel_clock = pck;
150         }
151
152         dss_mgr_set_timings(dssdev->manager, t);
153
154         return 0;
155 }
156
157 static void dpi_config_lcd_manager(struct omap_dss_device *dssdev)
158 {
159         dpi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
160
161         dpi.mgr_config.stallmode = false;
162         dpi.mgr_config.fifohandcheck = false;
163
164         dpi.mgr_config.video_port_width = dssdev->phy.dpi.data_lines;
165
166         dpi.mgr_config.lcden_sig_polarity = 0;
167
168         dss_mgr_set_lcd_config(dssdev->manager, &dpi.mgr_config);
169 }
170
171 int omapdss_dpi_display_enable(struct omap_dss_device *dssdev)
172 {
173         int r;
174
175         mutex_lock(&dpi.lock);
176
177         if (cpu_is_omap34xx() && !dpi.vdds_dsi_reg) {
178                 DSSERR("no VDSS_DSI regulator\n");
179                 r = -ENODEV;
180                 goto err_no_reg;
181         }
182
183         if (dssdev->manager == NULL) {
184                 DSSERR("failed to enable display: no manager\n");
185                 r = -ENODEV;
186                 goto err_no_mgr;
187         }
188
189         r = omap_dss_start_device(dssdev);
190         if (r) {
191                 DSSERR("failed to start device\n");
192                 goto err_start_dev;
193         }
194
195         if (cpu_is_omap34xx()) {
196                 r = regulator_enable(dpi.vdds_dsi_reg);
197                 if (r)
198                         goto err_reg_enable;
199         }
200
201         r = dispc_runtime_get();
202         if (r)
203                 goto err_get_dispc;
204
205         if (dpi_use_dsi_pll(dssdev)) {
206                 r = dsi_runtime_get(dpi.dsidev);
207                 if (r)
208                         goto err_get_dsi;
209
210                 r = dsi_pll_init(dpi.dsidev, 0, 1);
211                 if (r)
212                         goto err_dsi_pll_init;
213         }
214
215         r = dpi_set_mode(dssdev);
216         if (r)
217                 goto err_set_mode;
218
219         dpi_config_lcd_manager(dssdev);
220
221         mdelay(2);
222
223         r = dss_mgr_enable(dssdev->manager);
224         if (r)
225                 goto err_mgr_enable;
226
227         mutex_unlock(&dpi.lock);
228
229         return 0;
230
231 err_mgr_enable:
232 err_set_mode:
233         if (dpi_use_dsi_pll(dssdev))
234                 dsi_pll_uninit(dpi.dsidev, true);
235 err_dsi_pll_init:
236         if (dpi_use_dsi_pll(dssdev))
237                 dsi_runtime_put(dpi.dsidev);
238 err_get_dsi:
239         dispc_runtime_put();
240 err_get_dispc:
241         if (cpu_is_omap34xx())
242                 regulator_disable(dpi.vdds_dsi_reg);
243 err_reg_enable:
244         omap_dss_stop_device(dssdev);
245 err_start_dev:
246 err_no_mgr:
247 err_no_reg:
248         mutex_unlock(&dpi.lock);
249         return r;
250 }
251 EXPORT_SYMBOL(omapdss_dpi_display_enable);
252
253 void omapdss_dpi_display_disable(struct omap_dss_device *dssdev)
254 {
255         mutex_lock(&dpi.lock);
256
257         dss_mgr_disable(dssdev->manager);
258
259         if (dpi_use_dsi_pll(dssdev)) {
260                 dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
261                 dsi_pll_uninit(dpi.dsidev, true);
262                 dsi_runtime_put(dpi.dsidev);
263         }
264
265         dispc_runtime_put();
266
267         if (cpu_is_omap34xx())
268                 regulator_disable(dpi.vdds_dsi_reg);
269
270         omap_dss_stop_device(dssdev);
271
272         mutex_unlock(&dpi.lock);
273 }
274 EXPORT_SYMBOL(omapdss_dpi_display_disable);
275
276 void omapdss_dpi_set_timings(struct omap_dss_device *dssdev,
277                 struct omap_video_timings *timings)
278 {
279         int r;
280
281         DSSDBG("dpi_set_timings\n");
282
283         mutex_lock(&dpi.lock);
284
285         dpi.timings = *timings;
286         dssdev->panel.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 static int __init dpi_init_display(struct omap_dss_device *dssdev)
352 {
353         DSSDBG("init_display\n");
354
355         if (cpu_is_omap34xx() && dpi.vdds_dsi_reg == NULL) {
356                 struct regulator *vdds_dsi;
357
358                 vdds_dsi = dss_get_vdds_dsi();
359
360                 if (IS_ERR(vdds_dsi)) {
361                         DSSERR("can't get VDDS_DSI regulator\n");
362                         return PTR_ERR(vdds_dsi);
363                 }
364
365                 dpi.vdds_dsi_reg = vdds_dsi;
366         }
367
368         if (dpi_use_dsi_pll(dssdev)) {
369                 enum omap_dss_clk_source dispc_fclk_src =
370                         dssdev->clocks.dispc.dispc_fclk_src;
371                 dpi.dsidev = dpi_get_dsidev(dispc_fclk_src);
372         }
373
374         return 0;
375 }
376
377 static void __init dpi_probe_pdata(struct platform_device *pdev)
378 {
379         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
380         int i, r;
381
382         for (i = 0; i < pdata->num_devices; ++i) {
383                 struct omap_dss_device *dssdev = pdata->devices[i];
384
385                 if (dssdev->type != OMAP_DISPLAY_TYPE_DPI)
386                         continue;
387
388                 r = dpi_init_display(dssdev);
389                 if (r) {
390                         DSSERR("device %s init failed: %d\n", dssdev->name, r);
391                         continue;
392                 }
393
394                 r = omap_dss_register_device(dssdev, &pdev->dev, i);
395                 if (r)
396                         DSSERR("device %s register failed: %d\n",
397                                         dssdev->name, r);
398         }
399 }
400
401 static int __init omap_dpi_probe(struct platform_device *pdev)
402 {
403         mutex_init(&dpi.lock);
404
405         dpi_probe_pdata(pdev);
406
407         return 0;
408 }
409
410 static int __exit omap_dpi_remove(struct platform_device *pdev)
411 {
412         omap_dss_unregister_child_devices(&pdev->dev);
413
414         return 0;
415 }
416
417 static struct platform_driver omap_dpi_driver = {
418         .remove         = __exit_p(omap_dpi_remove),
419         .driver         = {
420                 .name   = "omapdss_dpi",
421                 .owner  = THIS_MODULE,
422         },
423 };
424
425 int __init dpi_init_platform_driver(void)
426 {
427         return platform_driver_probe(&omap_dpi_driver, omap_dpi_probe);
428 }
429
430 void __exit dpi_uninit_platform_driver(void)
431 {
432         platform_driver_unregister(&omap_dpi_driver);
433 }