dpi.c 18.3 KB
Newer Older
T
Tomi Valkeinen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * linux/drivers/video/omap2/dss/dpi.c
 *
 * Copyright (C) 2009 Nokia Corporation
 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
 *
 * Some code and ideas taken from drivers/video/omap/ driver
 * by Imre Deak.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define DSS_SUBSYS_NAME "DPI"

#include <linux/kernel.h>
#include <linux/delay.h>
27
#include <linux/export.h>
28
#include <linux/err.h>
T
Tomi Valkeinen 已提交
29
#include <linux/errno.h>
30 31
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
32
#include <linux/string.h>
T
Tomi Valkeinen 已提交
33
#include <linux/of.h>
34
#include <linux/clk.h>
T
Tomi Valkeinen 已提交
35
#include <linux/component.h>
T
Tomi Valkeinen 已提交
36

37
#include <video/omapdss.h>
T
Tomi Valkeinen 已提交
38 39

#include "dss.h"
40
#include "dss_features.h"
T
Tomi Valkeinen 已提交
41

A
Archit Taneja 已提交
42
struct dpi_data {
43 44
	struct platform_device *pdev;

45
	struct regulator *vdds_dsi_reg;
46
	enum dss_clk_source clk_src;
47
	struct dss_pll *pll;
48

49 50
	struct mutex lock;

51
	struct omap_video_timings timings;
52
	struct dss_lcd_mgr_config mgr_config;
53
	int data_lines;
54

55
	struct omap_dss_device output;
T
Tomi Valkeinen 已提交
56 57

	bool port_initialized;
A
Archit Taneja 已提交
58 59
};

A
Archit Taneja 已提交
60 61 62 63 64
static struct dpi_data *dpi_get_data_from_dssdev(struct omap_dss_device *dssdev)
{
	return container_of(dssdev, struct dpi_data, output);
}

65
/* only used in non-DT mode */
A
Archit Taneja 已提交
66 67 68 69
static struct dpi_data *dpi_get_data_from_pdev(struct platform_device *pdev)
{
	return dev_get_drvdata(&pdev->dev);
}
T
Tomi Valkeinen 已提交
70

71
static enum dss_clk_source dpi_get_clk_src(enum omap_channel channel)
72
{
73 74 75 76 77 78 79 80 81 82 83
	/*
	 * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL
	 * would also be used for DISPC fclk. Meaning, when the DPI output is
	 * disabled, DISPC clock will be disabled, and TV out will stop.
	 */
	switch (omapdss_get_version()) {
	case OMAPDSS_VER_OMAP24xx:
	case OMAPDSS_VER_OMAP34xx_ES1:
	case OMAPDSS_VER_OMAP34xx_ES3:
	case OMAPDSS_VER_OMAP3630:
	case OMAPDSS_VER_AM35xx:
84
	case OMAPDSS_VER_AM43xx:
85
		return DSS_CLK_SRC_FCK;
86

87 88 89 90 91
	case OMAPDSS_VER_OMAP4430_ES1:
	case OMAPDSS_VER_OMAP4430_ES2:
	case OMAPDSS_VER_OMAP4:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
92
			return DSS_CLK_SRC_PLL1_1;
93
		case OMAP_DSS_CHANNEL_LCD2:
94
			return DSS_CLK_SRC_PLL2_1;
95
		default:
96
			return DSS_CLK_SRC_FCK;
97 98 99 100 101
		}

	case OMAPDSS_VER_OMAP5:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
102
			return DSS_CLK_SRC_PLL1_1;
103
		case OMAP_DSS_CHANNEL_LCD3:
104 105
			return DSS_CLK_SRC_PLL2_1;
		case OMAP_DSS_CHANNEL_LCD2:
106
		default:
107
			return DSS_CLK_SRC_FCK;
108 109
		}

T
Tomi Valkeinen 已提交
110 111 112
	case OMAPDSS_VER_DRA7xx:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
113
			return DSS_CLK_SRC_PLL1_1;
T
Tomi Valkeinen 已提交
114
		case OMAP_DSS_CHANNEL_LCD2:
115
			return DSS_CLK_SRC_PLL1_3;
T
Tomi Valkeinen 已提交
116
		case OMAP_DSS_CHANNEL_LCD3:
117
			return DSS_CLK_SRC_PLL2_1;
T
Tomi Valkeinen 已提交
118
		default:
119
			return DSS_CLK_SRC_FCK;
T
Tomi Valkeinen 已提交
120 121
		}

122
	default:
123
		return DSS_CLK_SRC_FCK;
124
	}
125 126
}

127
struct dpi_clk_calc_ctx {
128
	struct dss_pll *pll;
129
	unsigned clkout_idx;
130 131 132 133 134 135 136

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

137
	struct dss_pll_clock_info dsi_cinfo;
T
Tomi Valkeinen 已提交
138
	unsigned long fck;
139 140 141 142 143 144 145 146 147 148 149 150 151
	struct dispc_clock_info dispc_cinfo;
};

static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
		unsigned long pck, void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

	/*
	 * Odd dividers give us uneven duty cycle, causing problem when level
	 * shifted. So skip all odd dividers when the pixel clock is on the
	 * higher side.
	 */
152
	if (ctx->pck_min >= 100000000) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		if (lckd > 1 && lckd % 2 != 0)
			return false;

		if (pckd > 1 && pckd % 2 != 0)
			return false;
	}

	ctx->dispc_cinfo.lck_div = lckd;
	ctx->dispc_cinfo.pck_div = pckd;
	ctx->dispc_cinfo.lck = lck;
	ctx->dispc_cinfo.pck = pck;

	return true;
}


169
static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
170 171 172 173 174 175 176 177 178
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

	/*
	 * Odd dividers give us uneven duty cycle, causing problem when level
	 * shifted. So skip all odd dividers when the pixel clock is on the
	 * higher side.
	 */
179
	if (m_dispc > 1 && m_dispc % 2 != 0 && ctx->pck_min >= 100000000)
180 181
		return false;

182 183
	ctx->dsi_cinfo.mX[ctx->clkout_idx] = m_dispc;
	ctx->dsi_cinfo.clkout[ctx->clkout_idx] = dispc;
184 185 186 187 188 189

	return dispc_div_calc(dispc, ctx->pck_min, ctx->pck_max,
			dpi_calc_dispc_cb, ctx);
}


190 191
static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
		unsigned long clkdco,
192 193 194 195
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

196 197
	ctx->dsi_cinfo.n = n;
	ctx->dsi_cinfo.m = m;
198
	ctx->dsi_cinfo.fint = fint;
199
	ctx->dsi_cinfo.clkdco = clkdco;
200

201 202 203
	return dss_pll_hsdiv_calc(ctx->pll, clkdco,
		ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK),
		dpi_calc_hsdiv_cb, ctx);
204 205
}

206
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
207 208 209
{
	struct dpi_clk_calc_ctx *ctx = data;

210
	ctx->fck = fck;
211 212 213 214 215

	return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max,
			dpi_calc_dispc_cb, ctx);
}

A
Archit Taneja 已提交
216 217
static bool dpi_dsi_clk_calc(struct dpi_data *dpi, unsigned long pck,
		struct dpi_clk_calc_ctx *ctx)
218 219 220 221 222
{
	unsigned long clkin;
	unsigned long pll_min, pll_max;

	memset(ctx, 0, sizeof(*ctx));
223
	ctx->pll = dpi->pll;
224
	ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
225 226 227 228 229 230
	ctx->pck_min = pck - 1000;
	ctx->pck_max = pck + 1000;

	pll_min = 0;
	pll_max = 0;

231 232 233
	clkin = clk_get_rate(ctx->pll->clkin);

	return dss_pll_calc(ctx->pll, clkin,
234 235 236 237 238 239 240 241 242 243 244 245
			pll_min, pll_max,
			dpi_calc_pll_cb, ctx);
}

static bool dpi_dss_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx)
{
	int i;

	/*
	 * DSS fck gives us very few possibilities, so finding a good pixel
	 * clock may not be possible. We try multiple times to find the clock,
	 * each time widening the pixel clock range we look for, up to
246
	 * +/- ~15MHz.
247 248
	 */

249
	for (i = 0; i < 25; ++i) {
250 251 252 253 254 255 256 257 258
		bool ok;

		memset(ctx, 0, sizeof(*ctx));
		if (pck > 1000 * i * i * i)
			ctx->pck_min = max(pck - 1000 * i * i * i, 0lu);
		else
			ctx->pck_min = 0;
		ctx->pck_max = pck + 1000 * i * i * i;

259
		ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
260 261 262 263 264 265 266 267 268
		if (ok)
			return ok;
	}

	return false;
}



A
Archit Taneja 已提交
269
static int dpi_set_dsi_clk(struct dpi_data *dpi, enum omap_channel channel,
270 271
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
T
Tomi Valkeinen 已提交
272
{
273
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
274
	int r;
275
	bool ok;
T
Tomi Valkeinen 已提交
276

A
Archit Taneja 已提交
277
	ok = dpi_dsi_clk_calc(dpi, pck_req, &ctx);
278 279
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
280

281
	r = dss_pll_set_config(dpi->pll, &ctx.dsi_cinfo);
T
Tomi Valkeinen 已提交
282 283 284
	if (r)
		return r;

285
	dss_select_lcd_clk_source(channel, dpi->clk_src);
T
Tomi Valkeinen 已提交
286

A
Archit Taneja 已提交
287
	dpi->mgr_config.clock_info = ctx.dispc_cinfo;
T
Tomi Valkeinen 已提交
288

289
	*fck = ctx.dsi_cinfo.clkout[ctx.clkout_idx];
290 291
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
292 293 294

	return 0;
}
295

A
Archit Taneja 已提交
296 297
static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req,
		unsigned long *fck, int *lck_div, int *pck_div)
T
Tomi Valkeinen 已提交
298
{
299
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
300
	int r;
301
	bool ok;
T
Tomi Valkeinen 已提交
302

303 304 305
	ok = dpi_dss_clk_calc(pck_req, &ctx);
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
306

307
	r = dss_set_fck_rate(ctx.fck);
T
Tomi Valkeinen 已提交
308 309 310
	if (r)
		return r;

A
Archit Taneja 已提交
311
	dpi->mgr_config.clock_info = ctx.dispc_cinfo;
T
Tomi Valkeinen 已提交
312

313
	*fck = ctx.fck;
314 315
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
316 317 318 319

	return 0;
}

A
Archit Taneja 已提交
320
static int dpi_set_mode(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
321
{
A
Archit Taneja 已提交
322
	struct omap_dss_device *out = &dpi->output;
323
	enum omap_channel channel = out->dispc_channel;
A
Archit Taneja 已提交
324
	struct omap_video_timings *t = &dpi->timings;
325 326
	int lck_div = 0, pck_div = 0;
	unsigned long fck = 0;
T
Tomi Valkeinen 已提交
327 328 329
	unsigned long pck;
	int r = 0;

330
	if (dpi->pll)
331
		r = dpi_set_dsi_clk(dpi, channel, t->pixelclock, &fck,
332
				&lck_div, &pck_div);
333
	else
A
Archit Taneja 已提交
334
		r = dpi_set_dispc_clk(dpi, t->pixelclock, &fck,
335
				&lck_div, &pck_div);
T
Tomi Valkeinen 已提交
336
	if (r)
337
		return r;
T
Tomi Valkeinen 已提交
338

339
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
340

341 342 343
	if (pck != t->pixelclock) {
		DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n",
			t->pixelclock, pck);
T
Tomi Valkeinen 已提交
344

345
		t->pixelclock = pck;
T
Tomi Valkeinen 已提交
346 347
	}

348
	dss_mgr_set_timings(channel, t);
T
Tomi Valkeinen 已提交
349

350
	return 0;
T
Tomi Valkeinen 已提交
351 352
}

A
Archit Taneja 已提交
353
static void dpi_config_lcd_manager(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
354
{
A
Archit Taneja 已提交
355
	struct omap_dss_device *out = &dpi->output;
356
	enum omap_channel channel = out->dispc_channel;
A
Archit Taneja 已提交
357 358

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
359

A
Archit Taneja 已提交
360 361
	dpi->mgr_config.stallmode = false;
	dpi->mgr_config.fifohandcheck = false;
362

A
Archit Taneja 已提交
363
	dpi->mgr_config.video_port_width = dpi->data_lines;
364

A
Archit Taneja 已提交
365
	dpi->mgr_config.lcden_sig_polarity = 0;
366

367
	dss_mgr_set_lcd_config(channel, &dpi->mgr_config);
T
Tomi Valkeinen 已提交
368 369
}

370
static int dpi_display_enable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
371
{
A
Archit Taneja 已提交
372
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
373
	struct omap_dss_device *out = &dpi->output;
374
	enum omap_channel channel = out->dispc_channel;
T
Tomi Valkeinen 已提交
375 376
	int r;

A
Archit Taneja 已提交
377
	mutex_lock(&dpi->lock);
378

A
Archit Taneja 已提交
379
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi->vdds_dsi_reg) {
380
		DSSERR("no VDSS_DSI regulator\n");
381 382
		r = -ENODEV;
		goto err_no_reg;
383 384
	}

385
	if (!out->dispc_channel_connected) {
386
		DSSERR("failed to enable display: no output/manager\n");
387
		r = -ENODEV;
388
		goto err_no_out_mgr;
389 390
	}

391
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
A
Archit Taneja 已提交
392
		r = regulator_enable(dpi->vdds_dsi_reg);
393
		if (r)
394
			goto err_reg_enable;
395 396
	}

397
	r = dispc_runtime_get();
T
Tomi Valkeinen 已提交
398
	if (r)
399 400
		goto err_get_dispc;

401
	r = dss_dpi_select_source(out->port_num, channel);
402 403 404
	if (r)
		goto err_src_sel;

405 406
	if (dpi->pll) {
		r = dss_pll_enable(dpi->pll);
407
		if (r)
408
			goto err_dsi_pll_init;
409 410
	}

A
Archit Taneja 已提交
411
	r = dpi_set_mode(dpi);
T
Tomi Valkeinen 已提交
412
	if (r)
413
		goto err_set_mode;
T
Tomi Valkeinen 已提交
414

A
Archit Taneja 已提交
415
	dpi_config_lcd_manager(dpi);
416

T
Tomi Valkeinen 已提交
417 418
	mdelay(2);

419
	r = dss_mgr_enable(channel);
420 421
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
422

A
Archit Taneja 已提交
423
	mutex_unlock(&dpi->lock);
424

T
Tomi Valkeinen 已提交
425 426
	return 0;

427
err_mgr_enable:
428
err_set_mode:
429 430
	if (dpi->pll)
		dss_pll_disable(dpi->pll);
431
err_dsi_pll_init:
432
err_src_sel:
433 434
	dispc_runtime_put();
err_get_dispc:
435
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
A
Archit Taneja 已提交
436
		regulator_disable(dpi->vdds_dsi_reg);
437
err_reg_enable:
438
err_no_out_mgr:
439
err_no_reg:
A
Archit Taneja 已提交
440
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
441 442 443
	return r;
}

444
static void dpi_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
445
{
A
Archit Taneja 已提交
446
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
447
	enum omap_channel channel = dpi->output.dispc_channel;
448

A
Archit Taneja 已提交
449
	mutex_lock(&dpi->lock);
450

451
	dss_mgr_disable(channel);
T
Tomi Valkeinen 已提交
452

453
	if (dpi->pll) {
454
		dss_select_lcd_clk_source(channel, DSS_CLK_SRC_FCK);
455
		dss_pll_disable(dpi->pll);
456
	}
T
Tomi Valkeinen 已提交
457

458
	dispc_runtime_put();
T
Tomi Valkeinen 已提交
459

460
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
A
Archit Taneja 已提交
461
		regulator_disable(dpi->vdds_dsi_reg);
462

A
Archit Taneja 已提交
463
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
464 465
}

466
static void dpi_set_timings(struct omap_dss_device *dssdev,
467
		struct omap_video_timings *timings)
T
Tomi Valkeinen 已提交
468
{
A
Archit Taneja 已提交
469
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
470

T
Tomi Valkeinen 已提交
471
	DSSDBG("dpi_set_timings\n");
472

A
Archit Taneja 已提交
473
	mutex_lock(&dpi->lock);
474

A
Archit Taneja 已提交
475
	dpi->timings = *timings;
476

A
Archit Taneja 已提交
477
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
478 479
}

T
Tomi Valkeinen 已提交
480 481 482
static void dpi_get_timings(struct omap_dss_device *dssdev,
		struct omap_video_timings *timings)
{
A
Archit Taneja 已提交
483
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
484 485

	mutex_lock(&dpi->lock);
T
Tomi Valkeinen 已提交
486

A
Archit Taneja 已提交
487
	*timings = dpi->timings;
T
Tomi Valkeinen 已提交
488

A
Archit Taneja 已提交
489
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
490 491
}

492
static int dpi_check_timings(struct omap_dss_device *dssdev,
T
Tomi Valkeinen 已提交
493 494
			struct omap_video_timings *timings)
{
A
Archit Taneja 已提交
495
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
496
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
497 498 499
	int lck_div, pck_div;
	unsigned long fck;
	unsigned long pck;
500 501
	struct dpi_clk_calc_ctx ctx;
	bool ok;
T
Tomi Valkeinen 已提交
502

503 504 505
	if (timings->x_res % 8 != 0)
		return -EINVAL;

506
	if (!dispc_mgr_timings_ok(channel, timings))
T
Tomi Valkeinen 已提交
507 508
		return -EINVAL;

509
	if (timings->pixelclock == 0)
T
Tomi Valkeinen 已提交
510 511
		return -EINVAL;

512
	if (dpi->pll) {
A
Archit Taneja 已提交
513
		ok = dpi_dsi_clk_calc(dpi, timings->pixelclock, &ctx);
514 515
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
516

517
		fck = ctx.dsi_cinfo.clkout[ctx.clkout_idx];
518
	} else {
519
		ok = dpi_dss_clk_calc(timings->pixelclock, &ctx);
520 521
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
522

523
		fck = ctx.fck;
T
Tomi Valkeinen 已提交
524
	}
525

526 527
	lck_div = ctx.dispc_cinfo.lck_div;
	pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
528

529
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
530

531
	timings->pixelclock = pck;
T
Tomi Valkeinen 已提交
532 533 534 535

	return 0;
}

536
static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
537
{
A
Archit Taneja 已提交
538
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
539

A
Archit Taneja 已提交
540
	mutex_lock(&dpi->lock);
541

A
Archit Taneja 已提交
542 543 544
	dpi->data_lines = data_lines;

	mutex_unlock(&dpi->lock);
545 546
}

547
static int dpi_verify_dsi_pll(struct dss_pll *pll)
548 549 550 551 552
{
	int r;

	/* do initial setup with the PLL to see if it is operational */

553
	r = dss_pll_enable(pll);
554
	if (r)
555 556
		return r;

557
	dss_pll_disable(pll);
558 559 560 561

	return 0;
}

A
Archit Taneja 已提交
562
static int dpi_init_regulator(struct dpi_data *dpi)
563 564 565 566 567 568
{
	struct regulator *vdds_dsi;

	if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
		return 0;

A
Archit Taneja 已提交
569
	if (dpi->vdds_dsi_reg)
570 571
		return 0;

A
Archit Taneja 已提交
572
	vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
573
	if (IS_ERR(vdds_dsi)) {
574 575
		if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
			DSSERR("can't get VDDS_DSI regulator\n");
576
		return PTR_ERR(vdds_dsi);
577 578
	}

A
Archit Taneja 已提交
579
	dpi->vdds_dsi_reg = vdds_dsi;
580 581 582 583

	return 0;
}

A
Archit Taneja 已提交
584
static void dpi_init_pll(struct dpi_data *dpi)
585
{
586
	struct dss_pll *pll;
587

588
	if (dpi->pll)
589 590
		return;

591 592 593
	dpi->clk_src = dpi_get_clk_src(dpi->output.dispc_channel);

	pll = dss_pll_find_by_src(dpi->clk_src);
594
	if (!pll)
595 596
		return;

597
	if (dpi_verify_dsi_pll(pll)) {
598 599 600 601
		DSSWARN("DSI PLL not operational\n");
		return;
	}

602
	dpi->pll = pll;
603 604
}

605 606 607 608 609 610
/*
 * Return a hardcoded channel for the DPI output. This should work for
 * current use cases, but this can be later expanded to either resolve
 * the channel in some more dynamic manner, or get the channel as a user
 * parameter.
 */
611
static enum omap_channel dpi_get_channel(int port_num)
612 613 614 615 616 617 618
{
	switch (omapdss_get_version()) {
	case OMAPDSS_VER_OMAP24xx:
	case OMAPDSS_VER_OMAP34xx_ES1:
	case OMAPDSS_VER_OMAP34xx_ES3:
	case OMAPDSS_VER_OMAP3630:
	case OMAPDSS_VER_AM35xx:
619
	case OMAPDSS_VER_AM43xx:
620 621
		return OMAP_DSS_CHANNEL_LCD;

T
Tomi Valkeinen 已提交
622 623 624 625 626 627 628 629 630 631 632
	case OMAPDSS_VER_DRA7xx:
		switch (port_num) {
		case 2:
			return OMAP_DSS_CHANNEL_LCD3;
		case 1:
			return OMAP_DSS_CHANNEL_LCD2;
		case 0:
		default:
			return OMAP_DSS_CHANNEL_LCD;
		}

633 634 635 636 637 638 639 640 641 642 643 644 645 646
	case OMAPDSS_VER_OMAP4430_ES1:
	case OMAPDSS_VER_OMAP4430_ES2:
	case OMAPDSS_VER_OMAP4:
		return OMAP_DSS_CHANNEL_LCD2;

	case OMAPDSS_VER_OMAP5:
		return OMAP_DSS_CHANNEL_LCD3;

	default:
		DSSWARN("unsupported DSS version\n");
		return OMAP_DSS_CHANNEL_LCD;
	}
}

T
Tomi Valkeinen 已提交
647 648 649
static int dpi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
A
Archit Taneja 已提交
650
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
651
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
652 653
	int r;

A
Archit Taneja 已提交
654
	r = dpi_init_regulator(dpi);
T
Tomi Valkeinen 已提交
655 656 657
	if (r)
		return r;

A
Archit Taneja 已提交
658
	dpi_init_pll(dpi);
T
Tomi Valkeinen 已提交
659

660
	r = dss_mgr_connect(channel, dssdev);
T
Tomi Valkeinen 已提交
661 662 663 664 665 666 667
	if (r)
		return r;

	r = omapdss_output_set_device(dssdev, dst);
	if (r) {
		DSSERR("failed to connect output to new device: %s\n",
				dst->name);
668
		dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
669 670 671 672 673 674 675 676 677
		return r;
	}

	return 0;
}

static void dpi_disconnect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
678 679 680
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
	enum omap_channel channel = dpi->output.dispc_channel;

681
	WARN_ON(dst != dssdev->dst);
T
Tomi Valkeinen 已提交
682

683
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
684 685 686 687
		return;

	omapdss_output_unset_device(dssdev);

688
	dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
689 690 691 692 693 694
}

static const struct omapdss_dpi_ops dpi_ops = {
	.connect = dpi_connect,
	.disconnect = dpi_disconnect,

695 696
	.enable = dpi_display_enable,
	.disable = dpi_display_disable,
T
Tomi Valkeinen 已提交
697 698

	.check_timings = dpi_check_timings,
699
	.set_timings = dpi_set_timings,
T
Tomi Valkeinen 已提交
700 701
	.get_timings = dpi_get_timings,

702
	.set_data_lines = dpi_set_data_lines,
T
Tomi Valkeinen 已提交
703 704
};

705
static void dpi_init_output(struct platform_device *pdev)
706
{
A
Archit Taneja 已提交
707
	struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
A
Archit Taneja 已提交
708
	struct omap_dss_device *out = &dpi->output;
709

710
	out->dev = &pdev->dev;
711
	out->id = OMAP_DSS_OUTPUT_DPI;
712
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
T
Tomi Valkeinen 已提交
713
	out->name = "dpi.0";
714
	out->dispc_channel = dpi_get_channel(0);
T
Tomi Valkeinen 已提交
715
	out->ops.dpi = &dpi_ops;
716
	out->owner = THIS_MODULE;
717

718
	omapdss_register_output(out);
719 720
}

721
static void dpi_uninit_output(struct platform_device *pdev)
722
{
A
Archit Taneja 已提交
723
	struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
A
Archit Taneja 已提交
724
	struct omap_dss_device *out = &dpi->output;
725

726
	omapdss_unregister_output(out);
727 728
}

729 730 731 732 733
static void dpi_init_output_port(struct platform_device *pdev,
	struct device_node *port)
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	int r;
	u32 port_num;

	r = of_property_read_u32(port, "reg", &port_num);
	if (r)
		port_num = 0;

	switch (port_num) {
	case 2:
		out->name = "dpi.2";
		break;
	case 1:
		out->name = "dpi.1";
		break;
	case 0:
	default:
		out->name = "dpi.0";
		break;
	}
753 754 755 756

	out->dev = &pdev->dev;
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
757 758
	out->dispc_channel = dpi_get_channel(port_num);
	out->port_num = port_num;
759 760 761 762 763 764
	out->ops.dpi = &dpi_ops;
	out->owner = THIS_MODULE;

	omapdss_register_output(out);
}

765
static void dpi_uninit_output_port(struct device_node *port)
766 767 768 769 770 771 772
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;

	omapdss_unregister_output(out);
}

T
Tomi Valkeinen 已提交
773
static int dpi_bind(struct device *dev, struct device *master, void *data)
774
{
T
Tomi Valkeinen 已提交
775
	struct platform_device *pdev = to_platform_device(dev);
A
Archit Taneja 已提交
776 777 778 779 780
	struct dpi_data *dpi;

	dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
	if (!dpi)
		return -ENOMEM;
A
Archit Taneja 已提交
781 782

	dpi->pdev = pdev;
783

A
Archit Taneja 已提交
784 785 786
	dev_set_drvdata(&pdev->dev, dpi);

	mutex_init(&dpi->lock);
787

788 789
	dpi_init_output(pdev);

790 791 792
	return 0;
}

T
Tomi Valkeinen 已提交
793
static void dpi_unbind(struct device *dev, struct device *master, void *data)
T
Tomi Valkeinen 已提交
794
{
T
Tomi Valkeinen 已提交
795 796
	struct platform_device *pdev = to_platform_device(dev);

797
	dpi_uninit_output(pdev);
T
Tomi Valkeinen 已提交
798 799 800 801 802 803
}

static const struct component_ops dpi_component_ops = {
	.bind	= dpi_bind,
	.unbind	= dpi_unbind,
};
804

T
Tomi Valkeinen 已提交
805 806 807 808 809 810 811 812
static int dpi_probe(struct platform_device *pdev)
{
	return component_add(&pdev->dev, &dpi_component_ops);
}

static int dpi_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &dpi_component_ops);
813
	return 0;
T
Tomi Valkeinen 已提交
814 815
}

816
static struct platform_driver omap_dpi_driver = {
T
Tomi Valkeinen 已提交
817 818
	.probe		= dpi_probe,
	.remove		= dpi_remove,
819 820
	.driver         = {
		.name   = "omapdss_dpi",
T
Tomi Valkeinen 已提交
821
		.suppress_bind_attrs = true,
822 823 824
	},
};

T
Tomi Valkeinen 已提交
825
int __init dpi_init_platform_driver(void)
826
{
827
	return platform_driver_register(&omap_dpi_driver);
828 829
}

830
void dpi_uninit_platform_driver(void)
831 832 833
{
	platform_driver_unregister(&omap_dpi_driver);
}
T
Tomi Valkeinen 已提交
834

835
int dpi_init_port(struct platform_device *pdev, struct device_node *port)
T
Tomi Valkeinen 已提交
836
{
A
Archit Taneja 已提交
837
	struct dpi_data *dpi;
T
Tomi Valkeinen 已提交
838 839 840 841
	struct device_node *ep;
	u32 datalines;
	int r;

A
Archit Taneja 已提交
842 843 844 845
	dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL);
	if (!dpi)
		return -ENOMEM;

T
Tomi Valkeinen 已提交
846 847 848 849 850 851 852 853 854 855
	ep = omapdss_of_get_next_endpoint(port, NULL);
	if (!ep)
		return 0;

	r = of_property_read_u32(ep, "data-lines", &datalines);
	if (r) {
		DSSERR("failed to parse datalines\n");
		goto err_datalines;
	}

A
Archit Taneja 已提交
856
	dpi->data_lines = datalines;
T
Tomi Valkeinen 已提交
857 858 859

	of_node_put(ep);

A
Archit Taneja 已提交
860
	dpi->pdev = pdev;
861
	port->data = dpi;
T
Tomi Valkeinen 已提交
862

A
Archit Taneja 已提交
863
	mutex_init(&dpi->lock);
T
Tomi Valkeinen 已提交
864

865
	dpi_init_output_port(pdev, port);
T
Tomi Valkeinen 已提交
866

A
Archit Taneja 已提交
867
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
868 869 870 871 872 873 874 875 876

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

877
void dpi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
878
{
879
	struct dpi_data *dpi = port->data;
A
Archit Taneja 已提交
880 881

	if (!dpi->port_initialized)
T
Tomi Valkeinen 已提交
882 883
		return;

884
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
885
}