blob: 157921db447a4ef3f3d0032feb56e83922867c94 [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 Valkeinen2ecef242013-12-16 15:13:24 +020033#include <linux/of.h>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030034
Tomi Valkeinena0b38cc2011-05-11 14:05:07 +030035#include <video/omapdss.h>
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030036
37#include "dss.h"
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +053038#include "dss_features.h"
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030039
40static struct {
Tomi Valkeinen00df43b2013-03-19 11:33:52 +020041 struct platform_device *pdev;
42
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +020043 struct regulator *vdds_dsi_reg;
Archit Tanejaa72b64b2011-05-12 17:26:26 +053044 struct platform_device *dsidev;
Archit Taneja5cf9a262012-06-29 14:19:13 +053045
Archit Tanejac8a5e4e2012-07-05 12:52:46 +053046 struct mutex lock;
47
Archit Tanejac4991442012-08-08 14:28:54 +053048 struct omap_video_timings timings;
Archit Taneja5cf9a262012-06-29 14:19:13 +053049 struct dss_lcd_mgr_config mgr_config;
Archit Tanejac6b393d2012-07-06 15:30:52 +053050 int data_lines;
Archit Taneja81b87f52012-09-26 16:30:49 +053051
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +030052 struct omap_dss_device output;
Tomi Valkeinen2ecef242013-12-16 15:13:24 +020053
54 bool port_initialized;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +030055} dpi;
56
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030057static struct platform_device *dpi_get_dsidev(enum omap_channel channel)
Archit Tanejaa72b64b2011-05-12 17:26:26 +053058{
Tomi Valkeinenbd0f5cc2012-12-13 14:21:30 +020059 /*
60 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
61 * would also be used for DISPC fclk. Meaning, when the DPI output is
62 * disabled, DISPC clock will be disabled, and TV out will stop.
63 */
64 switch (omapdss_get_version()) {
65 case OMAPDSS_VER_OMAP24xx:
66 case OMAPDSS_VER_OMAP34xx_ES1:
67 case OMAPDSS_VER_OMAP34xx_ES3:
68 case OMAPDSS_VER_OMAP3630:
69 case OMAPDSS_VER_AM35xx:
70 return NULL;
Tomi Valkeinenbd0f5cc2012-12-13 14:21:30 +020071
Tomi Valkeinenf8ad9842013-03-11 13:57:38 +020072 case OMAPDSS_VER_OMAP4430_ES1:
73 case OMAPDSS_VER_OMAP4430_ES2:
74 case OMAPDSS_VER_OMAP4:
75 switch (channel) {
76 case OMAP_DSS_CHANNEL_LCD:
77 return dsi_get_dsidev_from_id(0);
78 case OMAP_DSS_CHANNEL_LCD2:
79 return dsi_get_dsidev_from_id(1);
80 default:
81 return NULL;
82 }
83
84 case OMAPDSS_VER_OMAP5:
85 switch (channel) {
86 case OMAP_DSS_CHANNEL_LCD:
87 return dsi_get_dsidev_from_id(0);
88 case OMAP_DSS_CHANNEL_LCD3:
89 return dsi_get_dsidev_from_id(1);
90 default:
91 return NULL;
92 }
93
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030094 default:
95 return NULL;
96 }
Archit Tanejaa72b64b2011-05-12 17:26:26 +053097}
98
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +030099static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
Archit Taneja7636b3b2011-04-12 13:52:26 +0530100{
Tomi Valkeinen0e8276e2012-10-22 16:12:58 +0300101 switch (channel) {
102 case OMAP_DSS_CHANNEL_LCD:
103 return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC;
104 case OMAP_DSS_CHANNEL_LCD2:
105 return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
106 default:
107 /* this shouldn't happen */
108 WARN_ON(1);
109 return OMAP_DSS_CLK_SRC_FCK;
110 }
Archit Taneja7636b3b2011-04-12 13:52:26 +0530111}
112
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200113struct dpi_clk_calc_ctx {
114 struct platform_device *dsidev;
115
116 /* inputs */
117
118 unsigned long pck_min, pck_max;
119
120 /* outputs */
121
122 struct dsi_clock_info dsi_cinfo;
Tomi Valkeinenc56812f2014-01-28 08:50:47 +0200123 unsigned long fck;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200124 struct dispc_clock_info dispc_cinfo;
125};
126
127static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
128 unsigned long pck, void *data)
129{
130 struct dpi_clk_calc_ctx *ctx = data;
131
132 /*
133 * Odd dividers give us uneven duty cycle, causing problem when level
134 * shifted. So skip all odd dividers when the pixel clock is on the
135 * higher side.
136 */
Tomi Valkeinen72e55122013-06-12 09:44:52 +0300137 if (ctx->pck_min >= 100000000) {
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200138 if (lckd > 1 && lckd % 2 != 0)
139 return false;
140
141 if (pckd > 1 && pckd % 2 != 0)
142 return false;
143 }
144
145 ctx->dispc_cinfo.lck_div = lckd;
146 ctx->dispc_cinfo.pck_div = pckd;
147 ctx->dispc_cinfo.lck = lck;
148 ctx->dispc_cinfo.pck = pck;
149
150 return true;
151}
152
153
154static bool dpi_calc_hsdiv_cb(int regm_dispc, unsigned long dispc,
155 void *data)
156{
157 struct dpi_clk_calc_ctx *ctx = data;
158
159 /*
160 * Odd dividers give us uneven duty cycle, causing problem when level
161 * shifted. So skip all odd dividers when the pixel clock is on the
162 * higher side.
163 */
Tomi Valkeinen72e55122013-06-12 09:44:52 +0300164 if (regm_dispc > 1 && regm_dispc % 2 != 0 && ctx->pck_min >= 100000000)
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200165 return false;
166
167 ctx->dsi_cinfo.regm_dispc = regm_dispc;
168 ctx->dsi_cinfo.dsi_pll_hsdiv_dispc_clk = dispc;
169
170 return dispc_div_calc(dispc, ctx->pck_min, ctx->pck_max,
171 dpi_calc_dispc_cb, ctx);
172}
173
174
175static bool dpi_calc_pll_cb(int regn, int regm, unsigned long fint,
176 unsigned long pll,
177 void *data)
178{
179 struct dpi_clk_calc_ctx *ctx = data;
180
181 ctx->dsi_cinfo.regn = regn;
182 ctx->dsi_cinfo.regm = regm;
183 ctx->dsi_cinfo.fint = fint;
184 ctx->dsi_cinfo.clkin4ddr = pll;
185
186 return dsi_hsdiv_calc(ctx->dsidev, pll, ctx->pck_min,
187 dpi_calc_hsdiv_cb, ctx);
188}
189
Tomi Valkeinend0f58bd2013-10-31 14:44:23 +0200190static bool dpi_calc_dss_cb(unsigned long fck, void *data)
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200191{
192 struct dpi_clk_calc_ctx *ctx = data;
193
Tomi Valkeinend0f58bd2013-10-31 14:44:23 +0200194 ctx->fck = fck;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200195
196 return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
197 dpi_calc_dispc_cb, ctx);
198}
199
200static bool dpi_dsi_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
201{
202 unsigned long clkin;
203 unsigned long pll_min, pll_max;
204
205 clkin = dsi_get_pll_clkin(dpi.dsidev);
206
207 memset(ctx, 0, sizeof(*ctx));
208 ctx->dsidev = dpi.dsidev;
209 ctx->pck_min = pck - 1000;
210 ctx->pck_max = pck + 1000;
211 ctx->dsi_cinfo.clkin = clkin;
212
213 pll_min = 0;
214 pll_max = 0;
215
216 return dsi_pll_calc(dpi.dsidev, clkin,
217 pll_min, pll_max,
218 dpi_calc_pll_cb, ctx);
219}
220
221static bool dpi_dss_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
222{
223 int i;
224
225 /*
226 * DSS fck gives us very few possibilities, so finding a good pixel
227 * clock may not be possible. We try multiple times to find the clock,
228 * each time widening the pixel clock range we look for, up to
Tomi Valkeinen2c6360f2013-04-10 14:54:54 +0300229 * +/- ~15MHz.
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200230 */
231
Tomi Valkeinen2c6360f2013-04-10 14:54:54 +0300232 for (i = 0; i < 25; ++i) {
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200233 bool ok;
234
235 memset(ctx, 0, sizeof(*ctx));
236 if (pck > 1000 * i * i * i)
237 ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
238 else
239 ctx->pck_min = 0;
240 ctx->pck_max = pck + 1000 * i * i * i;
241
Tomi Valkeinen688af022013-10-31 16:41:57 +0200242 ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200243 if (ok)
244 return ok;
245 }
246
247 return false;
248}
249
250
251
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200252static int dpi_set_dsi_clk(enum omap_channel channel,
Sumit Semwalff1b2cd2010-12-02 11:27:11 +0000253 unsigned long pck_req, unsigned long *fck, int *lck_div,
254 int *pck_div)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300255{
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200256 struct dpi_clk_calc_ctx ctx;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300257 int r;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200258 bool ok;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300259
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200260 ok = dpi_dsi_clk_calc(pck_req, &ctx);
261 if (!ok)
262 return -EINVAL;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300263
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200264 r = dsi_pll_set_clock_div(dpi.dsidev, &ctx.dsi_cinfo);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300265 if (r)
266 return r;
267
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200268 dss_select_lcd_clk_source(channel,
269 dpi_get_alt_clk_src(channel));
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300270
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200271 dpi.mgr_config.clock_info = ctx.dispc_cinfo;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300272
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200273 *fck = ctx.dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
274 *lck_div = ctx.dispc_cinfo.lck_div;
275 *pck_div = ctx.dispc_cinfo.pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300276
277 return 0;
278}
Archit Taneja7636b3b2011-04-12 13:52:26 +0530279
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200280static int dpi_set_dispc_clk(unsigned long pck_req, unsigned long *fck,
281 int *lck_div, int *pck_div)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300282{
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200283 struct dpi_clk_calc_ctx ctx;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300284 int r;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200285 bool ok;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300286
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200287 ok = dpi_dss_clk_calc(pck_req, &ctx);
288 if (!ok)
289 return -EINVAL;
290
Tomi Valkeinend0f58bd2013-10-31 14:44:23 +0200291 r = dss_set_fck_rate(ctx.fck);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300292 if (r)
293 return r;
294
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200295 dpi.mgr_config.clock_info = ctx.dispc_cinfo;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300296
Tomi Valkeinend0f58bd2013-10-31 14:44:23 +0200297 *fck = ctx.fck;
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200298 *lck_div = ctx.dispc_cinfo.lck_div;
299 *pck_div = ctx.dispc_cinfo.pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300300
301 return 0;
302}
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300303
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200304static int dpi_set_mode(struct omap_overlay_manager *mgr)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300305{
Archit Tanejac4991442012-08-08 14:28:54 +0530306 struct omap_video_timings *t = &dpi.timings;
Archit Taneja7636b3b2011-04-12 13:52:26 +0530307 int lck_div = 0, pck_div = 0;
308 unsigned long fck = 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300309 unsigned long pck;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300310 int r = 0;
311
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300312 if (dpi.dsidev)
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300313 r = dpi_set_dsi_clk(mgr->id, t->pixelclock, &fck,
Archit Taneja6d523e72012-06-21 09:33:55 +0530314 &lck_div, &pck_div);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530315 else
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300316 r = dpi_set_dispc_clk(t->pixelclock, &fck,
Archit Taneja6d523e72012-06-21 09:33:55 +0530317 &lck_div, &pck_div);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300318 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300319 return r;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300320
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300321 pck = fck / lck_div / pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300322
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300323 if (pck != t->pixelclock) {
324 DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
325 t->pixelclock, pck);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300326
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300327 t->pixelclock = pck;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300328 }
329
Archit Taneja5d512fc2012-09-07 17:53:38 +0530330 dss_mgr_set_timings(mgr, t);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300331
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300332 return 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300333}
334
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200335static void dpi_config_lcd_manager(struct omap_overlay_manager *mgr)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300336{
Archit Taneja5cf9a262012-06-29 14:19:13 +0530337 dpi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
Archit Taneja569969d2011-08-22 17:41:57 +0530338
Archit Taneja5cf9a262012-06-29 14:19:13 +0530339 dpi.mgr_config.stallmode = false;
340 dpi.mgr_config.fifohandcheck = false;
341
Archit Tanejac6b393d2012-07-06 15:30:52 +0530342 dpi.mgr_config.video_port_width = dpi.data_lines;
Archit Taneja5cf9a262012-06-29 14:19:13 +0530343
344 dpi.mgr_config.lcden_sig_polarity = 0;
345
Archit Taneja5d512fc2012-09-07 17:53:38 +0530346 dss_mgr_set_lcd_config(mgr, &dpi.mgr_config);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300347}
348
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300349static int dpi_display_enable(struct omap_dss_device *dssdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300350{
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300351 struct omap_dss_device *out = &dpi.output;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300352 int r;
353
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530354 mutex_lock(&dpi.lock);
355
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530356 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi.vdds_dsi_reg) {
Russell King40410712012-02-07 09:44:55 +0000357 DSSERR("no VDSS_DSI regulator\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530358 r = -ENODEV;
359 goto err_no_reg;
Russell King40410712012-02-07 09:44:55 +0000360 }
361
Archit Taneja5d512fc2012-09-07 17:53:38 +0530362 if (out == NULL || out->manager == NULL) {
363 DSSERR("failed to enable display: no output/manager\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530364 r = -ENODEV;
Archit Taneja5d512fc2012-09-07 17:53:38 +0530365 goto err_no_out_mgr;
Tomi Valkeinen05e1d602011-06-23 16:38:21 +0300366 }
367
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530368 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200369 r = regulator_enable(dpi.vdds_dsi_reg);
370 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300371 goto err_reg_enable;
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200372 }
373
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300374 r = dispc_runtime_get();
375 if (r)
376 goto err_get_dispc;
377
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200378 r = dss_dpi_select_source(out->manager->id);
Tomi Valkeinende09e452012-09-21 12:09:54 +0300379 if (r)
380 goto err_src_sel;
381
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300382 if (dpi.dsidev) {
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300383 r = dsi_runtime_get(dpi.dsidev);
384 if (r)
385 goto err_get_dsi;
386
Archit Tanejaa72b64b2011-05-12 17:26:26 +0530387 r = dsi_pll_init(dpi.dsidev, 0, 1);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530388 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300389 goto err_dsi_pll_init;
Archit Taneja7636b3b2011-04-12 13:52:26 +0530390 }
391
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200392 r = dpi_set_mode(out->manager);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300393 if (r)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300394 goto err_set_mode;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300395
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200396 dpi_config_lcd_manager(out->manager);
Archit Taneja5cf9a262012-06-29 14:19:13 +0530397
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300398 mdelay(2);
399
Archit Taneja5d512fc2012-09-07 17:53:38 +0530400 r = dss_mgr_enable(out->manager);
Tomi Valkeinen33ca2372011-11-21 13:42:58 +0200401 if (r)
402 goto err_mgr_enable;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300403
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530404 mutex_unlock(&dpi.lock);
405
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300406 return 0;
407
Tomi Valkeinen33ca2372011-11-21 13:42:58 +0200408err_mgr_enable:
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300409err_set_mode:
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300410 if (dpi.dsidev)
Tomi Valkeinen19077a72011-05-18 11:33:44 +0300411 dsi_pll_uninit(dpi.dsidev, true);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300412err_dsi_pll_init:
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300413 if (dpi.dsidev)
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300414 dsi_runtime_put(dpi.dsidev);
415err_get_dsi:
Tomi Valkeinende09e452012-09-21 12:09:54 +0300416err_src_sel:
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300417 dispc_runtime_put();
418err_get_dispc:
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530419 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200420 regulator_disable(dpi.vdds_dsi_reg);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300421err_reg_enable:
Archit Taneja5d512fc2012-09-07 17:53:38 +0530422err_no_out_mgr:
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530423err_no_reg:
424 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300425 return r;
426}
427
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300428static void dpi_display_disable(struct omap_dss_device *dssdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300429{
Tomi Valkeinen03a0d1e2012-11-27 16:41:16 +0200430 struct omap_overlay_manager *mgr = dpi.output.manager;
Archit Taneja5d512fc2012-09-07 17:53:38 +0530431
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530432 mutex_lock(&dpi.lock);
433
Archit Taneja5d512fc2012-09-07 17:53:38 +0530434 dss_mgr_disable(mgr);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300435
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300436 if (dpi.dsidev) {
Tomi Valkeinena5b83992012-10-22 16:58:36 +0300437 dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
Archit Tanejaa72b64b2011-05-12 17:26:26 +0530438 dsi_pll_uninit(dpi.dsidev, true);
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300439 dsi_runtime_put(dpi.dsidev);
Archit Taneja7636b3b2011-04-12 13:52:26 +0530440 }
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300441
Tomi Valkeinen4fbafaf2011-05-27 10:52:19 +0300442 dispc_runtime_put();
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300443
Chandrabhanu Mahapatra195e6722012-08-22 11:44:06 +0530444 if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
Tomi Valkeinen8a2cfea2010-02-04 17:03:41 +0200445 regulator_disable(dpi.vdds_dsi_reg);
446
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530447 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300448}
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300449
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300450static void dpi_set_timings(struct omap_dss_device *dssdev,
Archit Tanejac4991442012-08-08 14:28:54 +0530451 struct omap_video_timings *timings)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300452{
453 DSSDBG("dpi_set_timings\n");
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530454
455 mutex_lock(&dpi.lock);
456
Archit Tanejac4991442012-08-08 14:28:54 +0530457 dpi.timings = *timings;
Archit Tanejac4991442012-08-08 14:28:54 +0530458
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530459 mutex_unlock(&dpi.lock);
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300460}
461
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300462static void dpi_get_timings(struct omap_dss_device *dssdev,
463 struct omap_video_timings *timings)
464{
465 mutex_lock(&dpi.lock);
466
467 *timings = dpi.timings;
468
469 mutex_unlock(&dpi.lock);
470}
471
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300472static int 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
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300485 if (timings->pixelclock == 0)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300486 return -EINVAL;
487
Tomi Valkeinen8a3db402012-10-22 16:03:39 +0300488 if (dpi.dsidev) {
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300489 ok = dpi_dsi_clk_calc(timings->pixelclock, &ctx);
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200490 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 Valkeinend8d789412013-04-10 14:12:14 +0300495 ok = dpi_dss_clk_calc(timings->pixelclock, &ctx);
Tomi Valkeinen100c8262013-03-05 17:07:16 +0200496 if (!ok)
497 return -EINVAL;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300498
Tomi Valkeinend0f58bd2013-10-31 14:44:23 +0200499 fck = ctx.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
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300505 pck = fck / lck_div / pck_div;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300506
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300507 timings->pixelclock = pck;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300508
509 return 0;
510}
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300511
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300512static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
Archit Tanejac6b393d2012-07-06 15:30:52 +0530513{
514 mutex_lock(&dpi.lock);
515
516 dpi.data_lines = data_lines;
517
518 mutex_unlock(&dpi.lock);
519}
Archit Tanejac6b393d2012-07-06 15:30:52 +0530520
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300521static int dpi_verify_dsi_pll(struct platform_device *dsidev)
Tomi Valkeinen60616752012-10-30 12:57:43 +0200522{
523 int r;
524
525 /* do initial setup with the PLL to see if it is operational */
526
527 r = dsi_runtime_get(dsidev);
528 if (r)
529 return r;
530
531 r = dsi_pll_init(dsidev, 0, 1);
532 if (r) {
533 dsi_runtime_put(dsidev);
534 return r;
535 }
536
537 dsi_pll_uninit(dsidev, true);
538 dsi_runtime_put(dsidev);
539
540 return 0;
541}
542
Tomi Valkeinen2795f642013-04-19 16:52:27 +0300543static int dpi_init_regulator(void)
544{
545 struct regulator *vdds_dsi;
546
547 if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
548 return 0;
549
550 if (dpi.vdds_dsi_reg)
551 return 0;
552
Tomi Valkeinen4123de22013-08-29 10:06:55 +0300553 vdds_dsi = devm_regulator_get(&dpi.pdev->dev, "vdds_dsi");
Tomi Valkeinen2795f642013-04-19 16:52:27 +0300554 if (IS_ERR(vdds_dsi)) {
Tomi Valkeinen40359a92013-12-19 16:15:34 +0200555 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
556 DSSERR("can't get VDDS_DSI regulator\n");
Tomi Valkeinen4123de22013-08-29 10:06:55 +0300557 return PTR_ERR(vdds_dsi);
Tomi Valkeinen2795f642013-04-19 16:52:27 +0300558 }
559
560 dpi.vdds_dsi_reg = vdds_dsi;
561
562 return 0;
563}
564
565static void dpi_init_pll(void)
566{
567 struct platform_device *dsidev;
568
569 if (dpi.dsidev)
570 return;
571
572 dsidev = dpi_get_dsidev(dpi.output.dispc_channel);
573 if (!dsidev)
574 return;
575
576 if (dpi_verify_dsi_pll(dsidev)) {
577 DSSWARN("DSI PLL not operational\n");
578 return;
579 }
580
581 dpi.dsidev = dsidev;
582}
583
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +0200584/*
585 * Return a hardcoded channel for the DPI output. This should work for
586 * current use cases, but this can be later expanded to either resolve
587 * the channel in some more dynamic manner, or get the channel as a user
588 * parameter.
589 */
590static enum omap_channel dpi_get_channel(void)
591{
592 switch (omapdss_get_version()) {
593 case OMAPDSS_VER_OMAP24xx:
594 case OMAPDSS_VER_OMAP34xx_ES1:
595 case OMAPDSS_VER_OMAP34xx_ES3:
596 case OMAPDSS_VER_OMAP3630:
597 case OMAPDSS_VER_AM35xx:
598 return OMAP_DSS_CHANNEL_LCD;
599
600 case OMAPDSS_VER_OMAP4430_ES1:
601 case OMAPDSS_VER_OMAP4430_ES2:
602 case OMAPDSS_VER_OMAP4:
603 return OMAP_DSS_CHANNEL_LCD2;
604
605 case OMAPDSS_VER_OMAP5:
606 return OMAP_DSS_CHANNEL_LCD3;
607
608 default:
609 DSSWARN("unsupported DSS version\n");
610 return OMAP_DSS_CHANNEL_LCD;
611 }
612}
613
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300614static int dpi_connect(struct omap_dss_device *dssdev,
615 struct omap_dss_device *dst)
616{
617 struct omap_overlay_manager *mgr;
618 int r;
619
620 r = dpi_init_regulator();
621 if (r)
622 return r;
623
624 dpi_init_pll();
625
626 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
627 if (!mgr)
628 return -ENODEV;
629
630 r = dss_mgr_connect(mgr, dssdev);
631 if (r)
632 return r;
633
634 r = omapdss_output_set_device(dssdev, dst);
635 if (r) {
636 DSSERR("failed to connect output to new device: %s\n",
637 dst->name);
638 dss_mgr_disconnect(mgr, dssdev);
639 return r;
640 }
641
642 return 0;
643}
644
645static void dpi_disconnect(struct omap_dss_device *dssdev,
646 struct omap_dss_device *dst)
647{
Tomi Valkeinen9560dc102013-07-24 13:06:54 +0300648 WARN_ON(dst != dssdev->dst);
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300649
Tomi Valkeinen9560dc102013-07-24 13:06:54 +0300650 if (dst != dssdev->dst)
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300651 return;
652
653 omapdss_output_unset_device(dssdev);
654
655 if (dssdev->manager)
656 dss_mgr_disconnect(dssdev->manager, dssdev);
657}
658
659static const struct omapdss_dpi_ops dpi_ops = {
660 .connect = dpi_connect,
661 .disconnect = dpi_disconnect,
662
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300663 .enable = dpi_display_enable,
664 .disable = dpi_display_disable,
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300665
666 .check_timings = dpi_check_timings,
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300667 .set_timings = dpi_set_timings,
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300668 .get_timings = dpi_get_timings,
669
Tomi Valkeinen86a3efe2013-05-15 10:40:15 +0300670 .set_data_lines = dpi_set_data_lines,
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300671};
672
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300673static void dpi_init_output(struct platform_device *pdev)
Archit Taneja81b87f52012-09-26 16:30:49 +0530674{
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300675 struct omap_dss_device *out = &dpi.output;
Archit Taneja81b87f52012-09-26 16:30:49 +0530676
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300677 out->dev = &pdev->dev;
Archit Taneja81b87f52012-09-26 16:30:49 +0530678 out->id = OMAP_DSS_OUTPUT_DPI;
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300679 out->output_type = OMAP_DISPLAY_TYPE_DPI;
Tomi Valkeinen7286a082013-02-18 13:06:01 +0200680 out->name = "dpi.0";
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +0200681 out->dispc_channel = dpi_get_channel();
Tomi Valkeinen0b24edb2013-05-24 13:18:52 +0300682 out->ops.dpi = &dpi_ops;
Tomi Valkeinenb7328e12013-05-03 11:42:18 +0300683 out->owner = THIS_MODULE;
Archit Taneja81b87f52012-09-26 16:30:49 +0530684
Tomi Valkeinen5d47dbc2013-04-24 13:32:51 +0300685 omapdss_register_output(out);
Archit Taneja81b87f52012-09-26 16:30:49 +0530686}
687
688static void __exit dpi_uninit_output(struct platform_device *pdev)
689{
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300690 struct omap_dss_device *out = &dpi.output;
Archit Taneja81b87f52012-09-26 16:30:49 +0530691
Tomi Valkeinen5d47dbc2013-04-24 13:32:51 +0300692 omapdss_unregister_output(out);
Archit Taneja81b87f52012-09-26 16:30:49 +0530693}
694
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300695static int omap_dpi_probe(struct platform_device *pdev)
Tomi Valkeinen38f3daf2012-05-02 14:55:12 +0300696{
Tomi Valkeinen00df43b2013-03-19 11:33:52 +0200697 dpi.pdev = pdev;
698
Archit Tanejac8a5e4e2012-07-05 12:52:46 +0530699 mutex_init(&dpi.lock);
700
Archit Taneja81b87f52012-09-26 16:30:49 +0530701 dpi_init_output(pdev);
702
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300703 return 0;
704}
705
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200706static int __exit omap_dpi_remove(struct platform_device *pdev)
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300707{
Archit Taneja81b87f52012-09-26 16:30:49 +0530708 dpi_uninit_output(pdev);
709
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200710 return 0;
Tomi Valkeinen553c48c2009-08-07 13:15:50 +0300711}
712
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200713static struct platform_driver omap_dpi_driver = {
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300714 .probe = omap_dpi_probe,
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200715 .remove = __exit_p(omap_dpi_remove),
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200716 .driver = {
717 .name = "omapdss_dpi",
718 .owner = THIS_MODULE,
719 },
720};
721
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200722int __init dpi_init_platform_driver(void)
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200723{
Tomi Valkeinen94cf3942013-04-26 14:27:44 +0300724 return platform_driver_register(&omap_dpi_driver);
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200725}
726
Tomi Valkeinen6e7e8f02012-02-17 17:41:13 +0200727void __exit dpi_uninit_platform_driver(void)
Tomi Valkeinena57dd4f2012-02-20 16:57:37 +0200728{
729 platform_driver_unregister(&omap_dpi_driver);
730}
Tomi Valkeinen2ecef242013-12-16 15:13:24 +0200731
732int __init dpi_init_port(struct platform_device *pdev, struct device_node *port)
733{
734 struct device_node *ep;
735 u32 datalines;
736 int r;
737
738 ep = omapdss_of_get_next_endpoint(port, NULL);
739 if (!ep)
740 return 0;
741
742 r = of_property_read_u32(ep, "data-lines", &datalines);
743 if (r) {
744 DSSERR("failed to parse datalines\n");
745 goto err_datalines;
746 }
747
748 dpi.data_lines = datalines;
749
750 of_node_put(ep);
751
752 dpi.pdev = pdev;
753
754 mutex_init(&dpi.lock);
755
756 dpi_init_output(pdev);
757
758 dpi.port_initialized = true;
759
760 return 0;
761
762err_datalines:
763 of_node_put(ep);
764
765 return r;
766}
767
768void __exit dpi_uninit_port(void)
769{
770 if (!dpi.port_initialized)
771 return;
772
773 dpi_uninit_output(dpi.pdev);
774}