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