blob: ef8fca211abe7f5b4242abe548e5e1a426f743a5 [file] [log] [blame]
Tomi Valkeinen553c48c2009-08-07 13:15:50 +03001/*
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>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030026#include <linux/delay.h>
Paul Gortmakera8a35932011-07-10 13:20:26 -040027#include <linux/export.h>
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +020028#include <linux/err.h>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030029#include <linux/errno.h>
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +020030#include <linux/platform_device.h>
31#include <linux/regulator/consumer.h>
Tomi Valkeinen13b1ba72012-09-28 10:03:03 +030032#include <linux/string.h>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030033
Tomi Valkeinena0b38cc2011-05-11 14:05:07 +030034#include <video/omapdss.h>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030035
36#include "dss.h"
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +053037#include "dss_features.h"
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030038
39static struct {
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +020040 struct regulator *vdds_dsi_reg;
Archit Tanejaa72b64b2011-05-12 17:26:26 +053041 struct platform_device *dsidev;
Archit Taneja5cf9a262012-06-29 14:19:13 +053042
Archit Tanejac8a5e4e2012-07-05 12:52:46 +053043 struct mutex lock;
44
Archit Tanejac4991442012-08-08 14:28:54 +053045 struct omap_video_timings timings;
Archit Taneja5cf9a262012-06-29 14:19:13 +053046 struct dss_lcd_mgr_config mgr_config;
Archit Tanejac6b393d2012-07-06 15:30:52 +053047 int data_lines;
Archit Taneja81b87f52012-09-26 16:30:49 +053048
49 struct omap_dss_output output;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030050} dpi;
51
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030052static struct platform_device *dpi_get_dsidev(enum omap_channel channel)
Archit Tanejaa72b64b2011-05-12 17:26:26 +053053{
Tomi Valkeinenbd0f5cc2012-12-13 14:21:30 +020054 /*
55 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
56 * would also be used for DISPC fclk. Meaning, when the DPI output is
57 * disabled, DISPC clock will be disabled, and TV out will stop.
58 */
59 switch (omapdss_get_version()) {
60 case OMAPDSS_VER_OMAP24xx:
61 case OMAPDSS_VER_OMAP34xx_ES1:
62 case OMAPDSS_VER_OMAP34xx_ES3:
63 case OMAPDSS_VER_OMAP3630:
64 case OMAPDSS_VER_AM35xx:
65 return NULL;
Tomi Valkeinenbd0f5cc2012-12-13 14:21:30 +020066
Tomi Valkeinenf8ad9842013-03-11 13:57:38 +020067 case OMAPDSS_VER_OMAP4430_ES1:
68 case OMAPDSS_VER_OMAP4430_ES2:
69 case OMAPDSS_VER_OMAP4:
70 switch (channel) {
71 case OMAP_DSS_CHANNEL_LCD:
72 return dsi_get_dsidev_from_id(0);
73 case OMAP_DSS_CHANNEL_LCD2:
74 return dsi_get_dsidev_from_id(1);
75 default:
76 return NULL;
77 }
78
79 case OMAPDSS_VER_OMAP5:
80 switch (channel) {
81 case OMAP_DSS_CHANNEL_LCD:
82 return dsi_get_dsidev_from_id(0);
83 case OMAP_DSS_CHANNEL_LCD3:
84 return dsi_get_dsidev_from_id(1);
85 default:
86 return NULL;
87 }
88
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030089 default:
90 return NULL;
91 }
Archit Tanejaa72b64b2011-05-12 17:26:26 +053092}
93
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030094static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
Archit Taneja7636b3b2011-04-12 13:52:26 +053095{
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030096 switch (channel) {
97 case OMAP_DSS_CHANNEL_LCD:
98 return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC;
99 case OMAP_DSS_CHANNEL_LCD2:
100 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
101 default:
102 /* this shouldn't happen */
103 WARN_ON(1);
104 return OMAP_DSS_CLK_SRC_FCK;
105 }
Archit Taneja7636b3b2011-04-12 13:52:26 +0530106}
107
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200108struct dpi_clk_calc_ctx {
109 struct platform_device *dsidev;
110
111 /* inputs */
112
113 unsigned long pck_min, pck_max;
114
115 /* outputs */
116
117 struct dsi_clock_info dsi_cinfo;
118 struct dss_clock_info dss_cinfo;
119 struct dispc_clock_info dispc_cinfo;
120};
121
122static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
123 unsigned long pck, void *data)
124{
125 struct dpi_clk_calc_ctx *ctx = data;
126
127 /*
128 * Odd dividers give us uneven duty cycle, causing problem when level
129 * shifted. So skip all odd dividers when the pixel clock is on the
130 * higher side.
131 */
132 if (ctx->pck_min >= 1000000) {
133 if (lckd > 1 && lckd % 2 != 0)
134 return false;
135
136 if (pckd > 1 && pckd % 2 != 0)
137 return false;
138 }
139
140 ctx->dispc_cinfo.lck_div = lckd;
141 ctx->dispc_cinfo.pck_div = pckd;
142 ctx->dispc_cinfo.lck = lck;
143 ctx->dispc_cinfo.pck = pck;
144
145 return true;
146}
147
148
149static bool dpi_calc_hsdiv_cb(int regm_dispc, unsigned long dispc,
150 void *data)
151{
152 struct dpi_clk_calc_ctx *ctx = data;
153
154 /*
155 * Odd dividers give us uneven duty cycle, causing problem when level
156 * shifted. So skip all odd dividers when the pixel clock is on the
157 * higher side.
158 */
159 if (regm_dispc > 1 && regm_dispc % 2 != 0 && ctx->pck_min >= 1000000)
160 return false;
161
162 ctx->dsi_cinfo.regm_dispc = regm_dispc;
163 ctx->dsi_cinfo.dsi_pll_hsdiv_dispc_clk = dispc;
164
165 return dispc_div_calc(dispc, ctx->pck_min, ctx->pck_max,
166 dpi_calc_dispc_cb, ctx);
167}
168
169
170static bool dpi_calc_pll_cb(int regn, int regm, unsigned long fint,
171 unsigned long pll,
172 void *data)
173{
174 struct dpi_clk_calc_ctx *ctx = data;
175
176 ctx->dsi_cinfo.regn = regn;
177 ctx->dsi_cinfo.regm = regm;
178 ctx->dsi_cinfo.fint = fint;
179 ctx->dsi_cinfo.clkin4ddr = pll;
180
181 return dsi_hsdiv_calc(ctx->dsidev, pll, ctx->pck_min,
182 dpi_calc_hsdiv_cb, ctx);
183}
184
185static bool dpi_calc_dss_cb(int fckd, unsigned long fck, void *data)
186{
187 struct dpi_clk_calc_ctx *ctx = data;
188
189 ctx->dss_cinfo.fck = fck;
190 ctx->dss_cinfo.fck_div = fckd;
191
192 return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
193 dpi_calc_dispc_cb, ctx);
194}
195
196static bool dpi_dsi_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
197{
198 unsigned long clkin;
199 unsigned long pll_min, pll_max;
200
201 clkin = dsi_get_pll_clkin(dpi.dsidev);
202
203 memset(ctx, 0, sizeof(*ctx));
204 ctx->dsidev = dpi.dsidev;
205 ctx->pck_min = pck - 1000;
206 ctx->pck_max = pck + 1000;
207 ctx->dsi_cinfo.clkin = clkin;
208
209 pll_min = 0;
210 pll_max = 0;
211
212 return dsi_pll_calc(dpi.dsidev, clkin,
213 pll_min, pll_max,
214 dpi_calc_pll_cb, ctx);
215}
216
217static bool dpi_dss_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
218{
219 int i;
220
221 /*
222 * DSS fck gives us very few possibilities, so finding a good pixel
223 * clock may not be possible. We try multiple times to find the clock,
224 * each time widening the pixel clock range we look for, up to
Tomi Valkeinen2c6360f2013-04-10 14:54:54 +0300225 * +/- ~15MHz.
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200226 */
227
Tomi Valkeinen2c6360f2013-04-10 14:54:54 +0300228 for (i = 0; i < 25; ++i) {
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200229 bool ok;
230
231 memset(ctx, 0, sizeof(*ctx));
232 if (pck > 1000 * i * i * i)
233 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
234 else
235 ctx->pck_min = 0;
236 ctx->pck_max = pck + 1000 * i * i * i;
237
238 ok = dss_div_calc(ctx->pck_min, dpi_calc_dss_cb, ctx);
239 if (ok)
240 return ok;
241 }
242
243 return false;
244}
245
246
247
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200248static int dpi_set_dsi_clk(enum omap_channel channel,
Sumit Semwalff1b2cd2010-12-02 11:27:11 +0000249 unsigned long pck_req, unsigned long *fck, int *lck_div,
250 int *pck_div)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300251{
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200252 struct dpi_clk_calc_ctx ctx;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300253 int r;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200254 bool ok;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300255
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200256 ok = dpi_dsi_clk_calc(pck_req, &ctx);
257 if (!ok)
258 return -EINVAL;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300259
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200260 r = dsi_pll_set_clock_div(dpi.dsidev, &ctx.dsi_cinfo);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300261 if (r)
262 return r;
263
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200264 dss_select_lcd_clk_source(channel,
265 dpi_get_alt_clk_src(channel));
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300266
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200267 dpi.mgr_config.clock_info = ctx.dispc_cinfo;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300268
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200269 *fck = ctx.dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
270 *lck_div = ctx.dispc_cinfo.lck_div;
271 *pck_div = ctx.dispc_cinfo.pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300272
273 return 0;
274}
Archit Taneja7636b3b2011-04-12 13:52:26 +0530275
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200276static int dpi_set_dispc_clk(unsigned long pck_req, unsigned long *fck,
277 int *lck_div, int *pck_div)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300278{
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200279 struct dpi_clk_calc_ctx ctx;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300280 int r;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200281 bool ok;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300282
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200283 ok = dpi_dss_clk_calc(pck_req, &ctx);
284 if (!ok)
285 return -EINVAL;
286
287 r = dss_set_clock_div(&ctx.dss_cinfo);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300288 if (r)
289 return r;
290
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200291 dpi.mgr_config.clock_info = ctx.dispc_cinfo;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300292
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200293 *fck = ctx.dss_cinfo.fck;
294 *lck_div = ctx.dispc_cinfo.lck_div;
295 *pck_div = ctx.dispc_cinfo.pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300296
297 return 0;
298}
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300299
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200300static int dpi_set_mode(struct omap_overlay_manager *mgr)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300301{
Archit Tanejac4991442012-08-08 14:28:54 +0530302 struct omap_video_timings *t = &dpi.timings;
Archit Taneja7636b3b2011-04-12 13:52:26 +0530303 int lck_div = 0, pck_div = 0;
304 unsigned long fck = 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300305 unsigned long pck;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300306 int r = 0;
307
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300308 if (dpi.dsidev)
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200309 r = dpi_set_dsi_clk(mgr->id, t->pixel_clock * 1000, &fck,
Archit Taneja6d523e72012-06-21 09:33:55 +0530310 &lck_div, &pck_div);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530311 else
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200312 r = dpi_set_dispc_clk(t->pixel_clock * 1000, &fck,
Archit Taneja6d523e72012-06-21 09:33:55 +0530313 &lck_div, &pck_div);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300314 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300315 return r;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300316
317 pck = fck / lck_div / pck_div / 1000;
318
319 if (pck != t->pixel_clock) {
320 DSSWARN("Could not find exact pixel clock. "
321 "Requested %d kHz, got %lu kHz\n",
322 t->pixel_clock, pck);
323
324 t->pixel_clock = pck;
325 }
326
Archit Taneja5d512fc2012-09-07 17:53:38 +0530327 dss_mgr_set_timings(mgr, t);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300328
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300329 return 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300330}
331
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200332static void dpi_config_lcd_manager(struct omap_overlay_manager *mgr)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300333{
Archit Taneja5cf9a262012-06-29 14:19:13 +0530334 dpi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
Archit Taneja569969d2011-08-22 17:41:57 +0530335
Archit Taneja5cf9a262012-06-29 14:19:13 +0530336 dpi.mgr_config.stallmode = false;
337 dpi.mgr_config.fifohandcheck = false;
338
Archit Tanejac6b393d2012-07-06 15:30:52 +0530339 dpi.mgr_config.video_port_width = dpi.data_lines;
Archit Taneja5cf9a262012-06-29 14:19:13 +0530340
341 dpi.mgr_config.lcden_sig_polarity = 0;
342
Archit Taneja5d512fc2012-09-07 17:53:38 +0530343 dss_mgr_set_lcd_config(mgr, &dpi.mgr_config);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300344}
345
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +0200346int omapdss_dpi_display_enable(struct omap_dss_device *dssdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300347{
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200348 struct omap_dss_output *out = &dpi.output;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300349 int r;
350
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530351 mutex_lock(&dpi.lock);
352
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530353 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi.vdds_dsi_reg) {
Russell King40410712012-02-07 09:44:55 +0000354 DSSERR("no VDSS_DSI regulator\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530355 r = -ENODEV;
356 goto err_no_reg;
Russell King40410712012-02-07 09:44:55 +0000357 }
358
Archit Taneja5d512fc2012-09-07 17:53:38 +0530359 if (out == NULL || out->manager == NULL) {
360 DSSERR("failed to enable display: no output/manager\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530361 r = -ENODEV;
Archit Taneja5d512fc2012-09-07 17:53:38 +0530362 goto err_no_out_mgr;
Tomi Valkeinen05e1d602011-06-23 16:38:21 +0300363 }
364
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300365 r = omap_dss_start_device(dssdev);
366 if (r) {
367 DSSERR("failed to start device\n");
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300368 goto err_start_dev;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300369 }
370
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530371 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200372 r = regulator_enable(dpi.vdds_dsi_reg);
373 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300374 goto err_reg_enable;
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200375 }
376
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300377 r = dispc_runtime_get();
378 if (r)
379 goto err_get_dispc;
380
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200381 r = dss_dpi_select_source(out->manager->id);
Tomi Valkeinende09e452012-09-21 12:09:54 +0300382 if (r)
383 goto err_src_sel;
384
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300385 if (dpi.dsidev) {
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300386 r = dsi_runtime_get(dpi.dsidev);
387 if (r)
388 goto err_get_dsi;
389
Archit Tanejaa72b64b2011-05-12 17:26:26 +0530390 r = dsi_pll_init(dpi.dsidev, 0, 1);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530391 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300392 goto err_dsi_pll_init;
Archit Taneja7636b3b2011-04-12 13:52:26 +0530393 }
394
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200395 r = dpi_set_mode(out->manager);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300396 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300397 goto err_set_mode;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300398
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200399 dpi_config_lcd_manager(out->manager);
Archit Taneja5cf9a262012-06-29 14:19:13 +0530400
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300401 mdelay(2);
402
Archit Taneja5d512fc2012-09-07 17:53:38 +0530403 r = dss_mgr_enable(out->manager);
Tomi Valkeinen33ca2372011-11-21 13:42:58 +0200404 if (r)
405 goto err_mgr_enable;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300406
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530407 mutex_unlock(&dpi.lock);
408
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300409 return 0;
410
Tomi Valkeinen33ca2372011-11-21 13:42:58 +0200411err_mgr_enable:
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300412err_set_mode:
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300413 if (dpi.dsidev)
Tomi Valkeinen19077a72011-05-18 11:33:44 +0300414 dsi_pll_uninit(dpi.dsidev, true);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300415err_dsi_pll_init:
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300416 if (dpi.dsidev)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300417 dsi_runtime_put(dpi.dsidev);
418err_get_dsi:
Tomi Valkeinende09e452012-09-21 12:09:54 +0300419err_src_sel:
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300420 dispc_runtime_put();
421err_get_dispc:
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530422 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200423 regulator_disable(dpi.vdds_dsi_reg);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300424err_reg_enable:
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300425 omap_dss_stop_device(dssdev);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300426err_start_dev:
Archit Taneja5d512fc2012-09-07 17:53:38 +0530427err_no_out_mgr:
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530428err_no_reg:
429 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300430 return r;
431}
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +0200432EXPORT_SYMBOL(omapdss_dpi_display_enable);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300433
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +0200434void omapdss_dpi_display_disable(struct omap_dss_device *dssdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300435{
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200436 struct omap_overlay_manager *mgr = dpi.output.manager;
Archit Taneja5d512fc2012-09-07 17:53:38 +0530437
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530438 mutex_lock(&dpi.lock);
439
Archit Taneja5d512fc2012-09-07 17:53:38 +0530440 dss_mgr_disable(mgr);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300441
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300442 if (dpi.dsidev) {
Tomi Valkeinena5b83992012-10-22 16:58:36 +0300443 dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
Archit Tanejaa72b64b2011-05-12 17:26:26 +0530444 dsi_pll_uninit(dpi.dsidev, true);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300445 dsi_runtime_put(dpi.dsidev);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530446 }
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300447
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300448 dispc_runtime_put();
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300449
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530450 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200451 regulator_disable(dpi.vdds_dsi_reg);
452
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300453 omap_dss_stop_device(dssdev);
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530454
455 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300456}
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +0200457EXPORT_SYMBOL(omapdss_dpi_display_disable);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300458
Archit Tanejac4991442012-08-08 14:28:54 +0530459void omapdss_dpi_set_timings(struct omap_dss_device *dssdev,
460 struct omap_video_timings *timings)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300461{
462 DSSDBG("dpi_set_timings\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530463
464 mutex_lock(&dpi.lock);
465
Archit Tanejac4991442012-08-08 14:28:54 +0530466 dpi.timings = *timings;
Archit Tanejac4991442012-08-08 14:28:54 +0530467
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530468 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300469}
Archit Tanejac4991442012-08-08 14:28:54 +0530470EXPORT_SYMBOL(omapdss_dpi_set_timings);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300471
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200472int dpi_check_timings(struct omap_dss_device *dssdev,
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300473 struct omap_video_timings *timings)
474{
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200475 struct omap_overlay_manager *mgr = dpi.output.manager;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300476 int lck_div, pck_div;
477 unsigned long fck;
478 unsigned long pck;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200479 struct dpi_clk_calc_ctx ctx;
480 bool ok;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300481
Tomi Valkeinen8b095512012-10-24 13:27:02 +0300482 if (mgr && !dispc_mgr_timings_ok(mgr->id, timings))
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300483 return -EINVAL;
484
485 if (timings->pixel_clock == 0)
486 return -EINVAL;
487
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300488 if (dpi.dsidev) {
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200489 ok = dpi_dsi_clk_calc(timings->pixel_clock * 1000, &ctx);
490 if (!ok)
491 return -EINVAL;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300492
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200493 fck = ctx.dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
Archit Taneja7636b3b2011-04-12 13:52:26 +0530494 } else {
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200495 ok = dpi_dss_clk_calc(timings->pixel_clock * 1000, &ctx);
496 if (!ok)
497 return -EINVAL;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300498
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200499 fck = ctx.dss_cinfo.fck;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300500 }
Archit Taneja7636b3b2011-04-12 13:52:26 +0530501
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200502 lck_div = ctx.dispc_cinfo.lck_div;
503 pck_div = ctx.dispc_cinfo.pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300504
505 pck = fck / lck_div / pck_div / 1000;
506
507 timings->pixel_clock = pck;
508
509 return 0;
510}
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200511EXPORT_SYMBOL(dpi_check_timings);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300512
Archit Tanejac6b393d2012-07-06 15:30:52 +0530513void omapdss_dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
514{
515 mutex_lock(&dpi.lock);
516
517 dpi.data_lines = data_lines;
518
519 mutex_unlock(&dpi.lock);
520}
521EXPORT_SYMBOL(omapdss_dpi_set_data_lines);
522
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300523static int dpi_verify_dsi_pll(struct platform_device *dsidev)
Tomi Valkeinen60616752012-10-30 12:57:43 +0200524{
525 int r;
526
527 /* do initial setup with the PLL to see if it is operational */
528
529 r = dsi_runtime_get(dsidev);
530 if (r)
531 return r;
532
533 r = dsi_pll_init(dsidev, 0, 1);
534 if (r) {
535 dsi_runtime_put(dsidev);
536 return r;
537 }
538
539 dsi_pll_uninit(dsidev, true);
540 dsi_runtime_put(dsidev);
541
542 return 0;
543}
544
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +0200545/*
546 * Return a hardcoded channel for the DPI output. This should work for
547 * current use cases, but this can be later expanded to either resolve
548 * the channel in some more dynamic manner, or get the channel as a user
549 * parameter.
550 */
551static enum omap_channel dpi_get_channel(void)
552{
553 switch (omapdss_get_version()) {
554 case OMAPDSS_VER_OMAP24xx:
555 case OMAPDSS_VER_OMAP34xx_ES1:
556 case OMAPDSS_VER_OMAP34xx_ES3:
557 case OMAPDSS_VER_OMAP3630:
558 case OMAPDSS_VER_AM35xx:
559 return OMAP_DSS_CHANNEL_LCD;
560
561 case OMAPDSS_VER_OMAP4430_ES1:
562 case OMAPDSS_VER_OMAP4430_ES2:
563 case OMAPDSS_VER_OMAP4:
564 return OMAP_DSS_CHANNEL_LCD2;
565
566 case OMAPDSS_VER_OMAP5:
567 return OMAP_DSS_CHANNEL_LCD3;
568
569 default:
570 DSSWARN("unsupported DSS version\n");
571 return OMAP_DSS_CHANNEL_LCD;
572 }
573}
574
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300575static int dpi_init_display(struct omap_dss_device *dssdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300576{
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +0300577 struct platform_device *dsidev;
578
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300579 DSSDBG("init_display\n");
580
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530581 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) &&
582 dpi.vdds_dsi_reg == NULL) {
Tomi Valkeinen5f42f2c2011-02-22 15:53:46 +0200583 struct regulator *vdds_dsi;
584
585 vdds_dsi = dss_get_vdds_dsi();
586
587 if (IS_ERR(vdds_dsi)) {
588 DSSERR("can't get VDDS_DSI regulator\n");
589 return PTR_ERR(vdds_dsi);
590 }
591
592 dpi.vdds_dsi_reg = vdds_dsi;
593 }
594
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +0200595 dsidev = dpi_get_dsidev(dpi.output.dispc_channel);
Tomi Valkeinen60616752012-10-30 12:57:43 +0200596
Tomi Valkeinen1de8e122012-11-12 16:52:11 +0200597 if (dsidev && dpi_verify_dsi_pll(dsidev)) {
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +0300598 dsidev = NULL;
599 DSSWARN("DSI PLL not operational\n");
Archit Tanejaa72b64b2011-05-12 17:26:26 +0530600 }
601
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +0300602 if (dsidev)
603 DSSDBG("using DSI PLL for DPI clock\n");
604
605 dpi.dsidev = dsidev;
606
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300607 return 0;
608}
609
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300610static struct omap_dss_device *dpi_find_dssdev(struct platform_device *pdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300611{
Tomi Valkeinen35deca32012-03-01 15:45:53 +0200612 struct omap_dss_board_info *pdata = pdev->dev.platform_data;
Tomi Valkeinen2bbcce52012-10-29 12:40:46 +0200613 const char *def_disp_name = omapdss_get_default_display_name();
Tomi Valkeinen15216532012-09-06 14:29:31 +0300614 struct omap_dss_device *def_dssdev;
615 int i;
616
617 def_dssdev = NULL;
Tomi Valkeinen35deca32012-03-01 15:45:53 +0200618
619 for (i = 0; i < pdata->num_devices; ++i) {
620 struct omap_dss_device *dssdev = pdata->devices[i];
621
622 if (dssdev->type != OMAP_DISPLAY_TYPE_DPI)
623 continue;
624
Tomi Valkeinen15216532012-09-06 14:29:31 +0300625 if (def_dssdev == NULL)
626 def_dssdev = dssdev;
Tomi Valkeinen9d8232a2012-03-01 16:58:39 +0200627
Tomi Valkeinen15216532012-09-06 14:29:31 +0300628 if (def_disp_name != NULL &&
629 strcmp(dssdev->name, def_disp_name) == 0) {
630 def_dssdev = dssdev;
631 break;
632 }
633 }
634
635 return def_dssdev;
636}
637
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300638static int dpi_probe_pdata(struct platform_device *dpidev)
Tomi Valkeinen15216532012-09-06 14:29:31 +0300639{
Tomi Valkeinen52744842012-09-10 13:58:29 +0300640 struct omap_dss_device *plat_dssdev;
Tomi Valkeinen15216532012-09-06 14:29:31 +0300641 struct omap_dss_device *dssdev;
642 int r;
643
Tomi Valkeinen52744842012-09-10 13:58:29 +0300644 plat_dssdev = dpi_find_dssdev(dpidev);
Tomi Valkeinen15216532012-09-06 14:29:31 +0300645
Tomi Valkeinen52744842012-09-10 13:58:29 +0300646 if (!plat_dssdev)
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300647 return 0;
Tomi Valkeinen52744842012-09-10 13:58:29 +0300648
649 dssdev = dss_alloc_and_init_device(&dpidev->dev);
Tomi Valkeinen15216532012-09-06 14:29:31 +0300650 if (!dssdev)
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300651 return -ENOMEM;
Tomi Valkeinen15216532012-09-06 14:29:31 +0300652
Tomi Valkeinen52744842012-09-10 13:58:29 +0300653 dss_copy_device_pdata(dssdev, plat_dssdev);
654
Tomi Valkeinen15216532012-09-06 14:29:31 +0300655 r = dpi_init_display(dssdev);
656 if (r) {
657 DSSERR("device %s init failed: %d\n", dssdev->name, r);
Tomi Valkeinen52744842012-09-10 13:58:29 +0300658 dss_put_device(dssdev);
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300659 return r;
Tomi Valkeinen15216532012-09-06 14:29:31 +0300660 }
661
Tomi Valkeinen486c0e12012-12-07 12:50:08 +0200662 r = omapdss_output_set_device(&dpi.output, dssdev);
663 if (r) {
664 DSSERR("failed to connect output to new device: %s\n",
665 dssdev->name);
666 dss_put_device(dssdev);
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300667 return r;
Tomi Valkeinen486c0e12012-12-07 12:50:08 +0200668 }
669
Tomi Valkeinen52744842012-09-10 13:58:29 +0300670 r = dss_add_device(dssdev);
Tomi Valkeinen15216532012-09-06 14:29:31 +0300671 if (r) {
672 DSSERR("device %s register failed: %d\n", dssdev->name, r);
Tomi Valkeinen486c0e12012-12-07 12:50:08 +0200673 omapdss_output_unset_device(&dpi.output);
Tomi Valkeinen52744842012-09-10 13:58:29 +0300674 dss_put_device(dssdev);
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300675 return r;
Tomi Valkeinen35deca32012-03-01 15:45:53 +0200676 }
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300677
678 return 0;
Tomi Valkeinen38f3daf2012-05-02 14:55:12 +0300679}
680
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300681static void dpi_init_output(struct platform_device *pdev)
Archit Taneja81b87f52012-09-26 16:30:49 +0530682{
683 struct omap_dss_output *out = &dpi.output;
684
685 out->pdev = pdev;
686 out->id = OMAP_DSS_OUTPUT_DPI;
687 out->type = OMAP_DISPLAY_TYPE_DPI;
Tomi Valkeinen7286a082013-02-18 13:06:01 +0200688 out->name = "dpi.0";
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +0200689 out->dispc_channel = dpi_get_channel();
Archit Taneja81b87f52012-09-26 16:30:49 +0530690
691 dss_register_output(out);
692}
693
694static void __exit dpi_uninit_output(struct platform_device *pdev)
695{
696 struct omap_dss_output *out = &dpi.output;
697
698 dss_unregister_output(out);
699}
700
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300701static int omap_dpi_probe(struct platform_device *pdev)
Tomi Valkeinen38f3daf2012-05-02 14:55:12 +0300702{
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300703 int r;
704
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530705 mutex_init(&dpi.lock);
706
Archit Taneja81b87f52012-09-26 16:30:49 +0530707 dpi_init_output(pdev);
708
Tomi Valkeinenc6ca5b22013-03-14 15:47:29 +0200709 if (pdev->dev.platform_data) {
710 r = dpi_probe_pdata(pdev);
711 if (r)
712 goto err_probe;
Tomi Valkeinenbcb734d2013-04-26 14:33:05 +0300713 }
Tomi Valkeinen35deca32012-03-01 15:45:53 +0200714
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300715 return 0;
Tomi Valkeinenc6ca5b22013-03-14 15:47:29 +0200716
717err_probe:
718 dpi_uninit_output(pdev);
719 return r;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300720}
721
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200722static int __exit omap_dpi_remove(struct platform_device *pdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300723{
Tomi Valkeinen52744842012-09-10 13:58:29 +0300724 dss_unregister_child_devices(&pdev->dev);
Tomi Valkeinen35deca32012-03-01 15:45:53 +0200725
Archit Taneja81b87f52012-09-26 16:30:49 +0530726 dpi_uninit_output(pdev);
727
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200728 return 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300729}
730
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200731static struct platform_driver omap_dpi_driver = {
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300732 .probe = omap_dpi_probe,
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200733 .remove = __exit_p(omap_dpi_remove),
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200734 .driver = {
735 .name = "omapdss_dpi",
736 .owner = THIS_MODULE,
737 },
738};
739
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200740int __init dpi_init_platform_driver(void)
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200741{
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300742 return platform_driver_register(&omap_dpi_driver);
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200743}
744
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200745void __exit dpi_uninit_platform_driver(void)
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200746{
747 platform_driver_unregister(&omap_dpi_driver);
748}