45fb1a673a2aa9447d77c85ae041b7cf492235a3
[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 } dpi;
42
43 static struct platform_device *dpi_get_dsidev(enum omap_dss_clk_source clk)
44 {
45         int dsi_module;
46
47         dsi_module = clk == OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ? 0 : 1;
48
49         return dsi_get_dsidev_from_id(dsi_module);
50 }
51
52 static bool dpi_use_dsi_pll(struct omap_dss_device *dssdev)
53 {
54         if (dssdev->clocks.dispc.dispc_fclk_src ==
55                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
56                         dssdev->clocks.dispc.dispc_fclk_src ==
57                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC ||
58                         dssdev->clocks.dispc.channel.lcd_clk_src ==
59                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC ||
60                         dssdev->clocks.dispc.channel.lcd_clk_src ==
61                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC)
62                 return true;
63         else
64                 return false;
65 }
66
67 static int dpi_set_dsi_clk(struct omap_dss_device *dssdev,
68                 unsigned long pck_req, unsigned long *fck, int *lck_div,
69                 int *pck_div)
70 {
71         struct dsi_clock_info dsi_cinfo;
72         struct dispc_clock_info dispc_cinfo;
73         int r;
74
75         r = dsi_pll_calc_clock_div_pck(dpi.dsidev, pck_req, &dsi_cinfo,
76                         &dispc_cinfo);
77         if (r)
78                 return r;
79
80         r = dsi_pll_set_clock_div(dpi.dsidev, &dsi_cinfo);
81         if (r)
82                 return r;
83
84         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
85
86         r = dispc_mgr_set_clock_div(dssdev->manager->id, &dispc_cinfo);
87         if (r) {
88                 dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
89                 return r;
90         }
91
92         *fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
93         *lck_div = dispc_cinfo.lck_div;
94         *pck_div = dispc_cinfo.pck_div;
95
96         return 0;
97 }
98
99 static int dpi_set_dispc_clk(struct omap_dss_device *dssdev,
100                 unsigned long pck_req, unsigned long *fck, int *lck_div,
101                 int *pck_div)
102 {
103         struct dss_clock_info dss_cinfo;
104         struct dispc_clock_info dispc_cinfo;
105         int r;
106
107         r = dss_calc_clock_div(pck_req, &dss_cinfo, &dispc_cinfo);
108         if (r)
109                 return r;
110
111         r = dss_set_clock_div(&dss_cinfo);
112         if (r)
113                 return r;
114
115         r = dispc_mgr_set_clock_div(dssdev->manager->id, &dispc_cinfo);
116         if (r)
117                 return r;
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 = &dssdev->panel.timings;
129         int lck_div = 0, pck_div = 0;
130         unsigned long fck = 0;
131         unsigned long pck;
132         int r = 0;
133
134         dispc_mgr_set_pol_freq(dssdev->manager->id, dssdev->panel.config,
135                         dssdev->panel.acbi, dssdev->panel.acb);
136
137         if (dpi_use_dsi_pll(dssdev))
138                 r = dpi_set_dsi_clk(dssdev, t->pixel_clock * 1000, &fck,
139                                 &lck_div, &pck_div);
140         else
141                 r = dpi_set_dispc_clk(dssdev, t->pixel_clock * 1000, &fck,
142                                 &lck_div, &pck_div);
143         if (r)
144                 return r;
145
146         pck = fck / lck_div / pck_div / 1000;
147
148         if (pck != t->pixel_clock) {
149                 DSSWARN("Could not find exact pixel clock. "
150                                 "Requested %d kHz, got %lu kHz\n",
151                                 t->pixel_clock, pck);
152
153                 t->pixel_clock = pck;
154         }
155
156         dss_mgr_set_timings(dssdev->manager, t);
157
158         return 0;
159 }
160
161 static void dpi_basic_init(struct omap_dss_device *dssdev)
162 {
163         dispc_mgr_set_io_pad_mode(DSS_IO_PAD_MODE_BYPASS);
164         dispc_mgr_enable_stallmode(dssdev->manager->id, false);
165
166         dispc_mgr_set_lcd_type_tft(dssdev->manager->id);
167
168         dispc_mgr_set_tft_data_lines(dssdev->manager->id,
169                         dssdev->phy.dpi.data_lines);
170 }
171
172 int omapdss_dpi_display_enable(struct omap_dss_device *dssdev)
173 {
174         int r;
175
176         if (cpu_is_omap34xx() && !dpi.vdds_dsi_reg) {
177                 DSSERR("no VDSS_DSI regulator\n");
178                 return -ENODEV;
179         }
180
181         if (dssdev->manager == NULL) {
182                 DSSERR("failed to enable display: no manager\n");
183                 return -ENODEV;
184         }
185
186         r = omap_dss_start_device(dssdev);
187         if (r) {
188                 DSSERR("failed to start device\n");
189                 goto err_start_dev;
190         }
191
192         if (cpu_is_omap34xx()) {
193                 r = regulator_enable(dpi.vdds_dsi_reg);
194                 if (r)
195                         goto err_reg_enable;
196         }
197
198         r = dispc_runtime_get();
199         if (r)
200                 goto err_get_dispc;
201
202         dpi_basic_init(dssdev);
203
204         if (dpi_use_dsi_pll(dssdev)) {
205                 r = dsi_runtime_get(dpi.dsidev);
206                 if (r)
207                         goto err_get_dsi;
208
209                 r = dsi_pll_init(dpi.dsidev, 0, 1);
210                 if (r)
211                         goto err_dsi_pll_init;
212         }
213
214         r = dpi_set_mode(dssdev);
215         if (r)
216                 goto err_set_mode;
217
218         mdelay(2);
219
220         r = dss_mgr_enable(dssdev->manager);
221         if (r)
222                 goto err_mgr_enable;
223
224         return 0;
225
226 err_mgr_enable:
227 err_set_mode:
228         if (dpi_use_dsi_pll(dssdev))
229                 dsi_pll_uninit(dpi.dsidev, true);
230 err_dsi_pll_init:
231         if (dpi_use_dsi_pll(dssdev))
232                 dsi_runtime_put(dpi.dsidev);
233 err_get_dsi:
234         dispc_runtime_put();
235 err_get_dispc:
236         if (cpu_is_omap34xx())
237                 regulator_disable(dpi.vdds_dsi_reg);
238 err_reg_enable:
239         omap_dss_stop_device(dssdev);
240 err_start_dev:
241         return r;
242 }
243 EXPORT_SYMBOL(omapdss_dpi_display_enable);
244
245 void omapdss_dpi_display_disable(struct omap_dss_device *dssdev)
246 {
247         dss_mgr_disable(dssdev->manager);
248
249         if (dpi_use_dsi_pll(dssdev)) {
250                 dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
251                 dsi_pll_uninit(dpi.dsidev, true);
252                 dsi_runtime_put(dpi.dsidev);
253         }
254
255         dispc_runtime_put();
256
257         if (cpu_is_omap34xx())
258                 regulator_disable(dpi.vdds_dsi_reg);
259
260         omap_dss_stop_device(dssdev);
261 }
262 EXPORT_SYMBOL(omapdss_dpi_display_disable);
263
264 void dpi_set_timings(struct omap_dss_device *dssdev,
265                         struct omap_video_timings *timings)
266 {
267         int r;
268
269         DSSDBG("dpi_set_timings\n");
270         dssdev->panel.timings = *timings;
271         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
272                 r = dispc_runtime_get();
273                 if (r)
274                         return;
275
276                 dpi_set_mode(dssdev);
277
278                 dispc_runtime_put();
279         } else {
280                 dss_mgr_set_timings(dssdev->manager, timings);
281         }
282 }
283 EXPORT_SYMBOL(dpi_set_timings);
284
285 int dpi_check_timings(struct omap_dss_device *dssdev,
286                         struct omap_video_timings *timings)
287 {
288         int r;
289         int lck_div, pck_div;
290         unsigned long fck;
291         unsigned long pck;
292         struct dispc_clock_info dispc_cinfo;
293
294         if (dss_mgr_check_timings(dssdev->manager, timings))
295                 return -EINVAL;
296
297         if (timings->pixel_clock == 0)
298                 return -EINVAL;
299
300         if (dpi_use_dsi_pll(dssdev)) {
301                 struct dsi_clock_info dsi_cinfo;
302                 r = dsi_pll_calc_clock_div_pck(dpi.dsidev,
303                                 timings->pixel_clock * 1000,
304                                 &dsi_cinfo, &dispc_cinfo);
305
306                 if (r)
307                         return r;
308
309                 fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
310         } else {
311                 struct dss_clock_info dss_cinfo;
312                 r = dss_calc_clock_div(timings->pixel_clock * 1000,
313                                 &dss_cinfo, &dispc_cinfo);
314
315                 if (r)
316                         return r;
317
318                 fck = dss_cinfo.fck;
319         }
320
321         lck_div = dispc_cinfo.lck_div;
322         pck_div = dispc_cinfo.pck_div;
323
324         pck = fck / lck_div / pck_div / 1000;
325
326         timings->pixel_clock = pck;
327
328         return 0;
329 }
330 EXPORT_SYMBOL(dpi_check_timings);
331
332 static int __init dpi_init_display(struct omap_dss_device *dssdev)
333 {
334         DSSDBG("init_display\n");
335
336         if (cpu_is_omap34xx() && dpi.vdds_dsi_reg == NULL) {
337                 struct regulator *vdds_dsi;
338
339                 vdds_dsi = dss_get_vdds_dsi();
340
341                 if (IS_ERR(vdds_dsi)) {
342                         DSSERR("can't get VDDS_DSI regulator\n");
343                         return PTR_ERR(vdds_dsi);
344                 }
345
346                 dpi.vdds_dsi_reg = vdds_dsi;
347         }
348
349         if (dpi_use_dsi_pll(dssdev)) {
350                 enum omap_dss_clk_source dispc_fclk_src =
351                         dssdev->clocks.dispc.dispc_fclk_src;
352                 dpi.dsidev = dpi_get_dsidev(dispc_fclk_src);
353         }
354
355         return 0;
356 }
357
358 static void __init dpi_probe_pdata(struct platform_device *pdev)
359 {
360         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
361         int i, r;
362
363         for (i = 0; i < pdata->num_devices; ++i) {
364                 struct omap_dss_device *dssdev = pdata->devices[i];
365
366                 if (dssdev->type != OMAP_DISPLAY_TYPE_DPI)
367                         continue;
368
369                 r = dpi_init_display(dssdev);
370                 if (r) {
371                         DSSERR("device %s init failed: %d\n", dssdev->name, r);
372                         continue;
373                 }
374
375                 r = omap_dss_register_device(dssdev, &pdev->dev, i);
376                 if (r)
377                         DSSERR("device %s register failed: %d\n",
378                                         dssdev->name, r);
379         }
380 }
381
382 static int __init omap_dpi_probe(struct platform_device *pdev)
383 {
384         dpi_probe_pdata(pdev);
385
386         return 0;
387 }
388
389 static int __exit omap_dpi_remove(struct platform_device *pdev)
390 {
391         omap_dss_unregister_child_devices(&pdev->dev);
392
393         return 0;
394 }
395
396 static struct platform_driver omap_dpi_driver = {
397         .remove         = __exit_p(omap_dpi_remove),
398         .driver         = {
399                 .name   = "omapdss_dpi",
400                 .owner  = THIS_MODULE,
401         },
402 };
403
404 int __init dpi_init_platform_driver(void)
405 {
406         return platform_driver_probe(&omap_dpi_driver, omap_dpi_probe);
407 }
408
409 void __exit dpi_uninit_platform_driver(void)
410 {
411         platform_driver_unregister(&omap_dpi_driver);
412 }