dpi.c 17.7 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>
T
Tomi Valkeinen 已提交
34

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

#include "dss.h"
38
#include "dss_features.h"
T
Tomi Valkeinen 已提交
39

40 41
#define HSDIV_DISPC	0

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

45
	struct regulator *vdds_dsi_reg;
46
	struct platform_device *dsidev;
47

48 49
	struct mutex lock;

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

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

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

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

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

70
static struct platform_device *dpi_get_dsidev(enum omap_channel channel)
71
{
72 73 74 75 76 77 78 79 80 81 82
	/*
	 * 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:
83
	case OMAPDSS_VER_AM43xx:
84 85
		return NULL;

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	case OMAPDSS_VER_OMAP4430_ES1:
	case OMAPDSS_VER_OMAP4430_ES2:
	case OMAPDSS_VER_OMAP4:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
			return dsi_get_dsidev_from_id(0);
		case OMAP_DSS_CHANNEL_LCD2:
			return dsi_get_dsidev_from_id(1);
		default:
			return NULL;
		}

	case OMAPDSS_VER_OMAP5:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
			return dsi_get_dsidev_from_id(0);
		case OMAP_DSS_CHANNEL_LCD3:
			return dsi_get_dsidev_from_id(1);
		default:
			return NULL;
		}

108 109 110
	default:
		return NULL;
	}
111 112
}

113
static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
114
{
115 116 117 118 119
	switch (channel) {
	case OMAP_DSS_CHANNEL_LCD:
		return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC;
	case OMAP_DSS_CHANNEL_LCD2:
		return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
120 121
	case OMAP_DSS_CHANNEL_LCD3:
		return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
122 123 124 125 126
	default:
		/* this shouldn't happen */
		WARN_ON(1);
		return OMAP_DSS_CLK_SRC_FCK;
	}
127 128
}

129 130 131 132 133 134 135 136 137 138
struct dpi_clk_calc_ctx {
	struct platform_device *dsidev;

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

	struct dsi_clock_info dsi_cinfo;
T
Tomi Valkeinen 已提交
139
	unsigned long fck;
140 141 142 143 144 145 146 147 148 149 150 151 152
	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.
	 */
153
	if (ctx->pck_min >= 100000000) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		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;
}


static bool dpi_calc_hsdiv_cb(int regm_dispc, unsigned long dispc,
		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.
	 */
180
	if (regm_dispc > 1 && regm_dispc % 2 != 0 && ctx->pck_min >= 100000000)
181 182
		return false;

183 184
	ctx->dsi_cinfo.regm_hsdiv[HSDIV_DISPC] = regm_dispc;
	ctx->dsi_cinfo.clkout[HSDIV_DISPC] = dispc;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

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


static bool dpi_calc_pll_cb(int regn, int regm, unsigned long fint,
		unsigned long pll,
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

	ctx->dsi_cinfo.regn = regn;
	ctx->dsi_cinfo.regm = regm;
	ctx->dsi_cinfo.fint = fint;
200
	ctx->dsi_cinfo.clkdco = pll;
201 202 203 204 205

	return dsi_hsdiv_calc(ctx->dsidev, pll, ctx->pck_min,
			dpi_calc_hsdiv_cb, ctx);
}

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
{
	unsigned long clkin;
	unsigned long pll_min, pll_max;

A
Archit Taneja 已提交
222
	clkin = dsi_get_pll_clkin(dpi->dsidev);
223 224

	memset(ctx, 0, sizeof(*ctx));
A
Archit Taneja 已提交
225
	ctx->dsidev = dpi->dsidev;
226 227 228 229 230 231
	ctx->pck_min = pck - 1000;
	ctx->pck_max = pck + 1000;

	pll_min = 0;
	pll_max = 0;

A
Archit Taneja 已提交
232
	return dsi_pll_calc(dpi->dsidev, clkin,
233 234 235 236 237 238 239 240 241 242 243 244
			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
245
	 * +/- ~15MHz.
246 247
	 */

248
	for (i = 0; i < 25; ++i) {
249 250 251 252 253 254 255 256 257
		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;

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

	return false;
}



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

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

A
Archit Taneja 已提交
280
	r = dsi_pll_set_clock_div(dpi->dsidev, &ctx.dsi_cinfo);
T
Tomi Valkeinen 已提交
281 282 283
	if (r)
		return r;

284 285
	dss_select_lcd_clk_source(channel,
			dpi_get_alt_clk_src(channel));
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[HSDIV_DISPC];
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 323 324
	struct omap_dss_device *out = &dpi->output;
	struct omap_overlay_manager *mgr = out->manager;
	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;

A
Archit Taneja 已提交
330 331
	if (dpi->dsidev)
		r = dpi_set_dsi_clk(dpi, mgr->id, 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(mgr, 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 356 357 358
	struct omap_dss_device *out = &dpi->output;
	struct omap_overlay_manager *mgr = out->manager;

	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

A
Archit Taneja 已提交
367
	dss_mgr_set_lcd_config(mgr, &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;
T
Tomi Valkeinen 已提交
374 375
	int r;

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

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

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

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

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

400
	r = dss_dpi_select_source(out->port_num, out->manager->id);
401 402 403
	if (r)
		goto err_src_sel;

A
Archit Taneja 已提交
404
	if (dpi->dsidev) {
405
		r = dsi_pll_init(dpi->dsidev);
406
		if (r)
407
			goto err_dsi_pll_init;
408 409
	}

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

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

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

418
	r = dss_mgr_enable(out->manager);
419 420
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
421

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

T
Tomi Valkeinen 已提交
424 425
	return 0;

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

443
static void dpi_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
444
{
A
Archit Taneja 已提交
445
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
446
	struct omap_overlay_manager *mgr = dpi->output.manager;
447

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

450
	dss_mgr_disable(mgr);
T
Tomi Valkeinen 已提交
451

A
Archit Taneja 已提交
452
	if (dpi->dsidev) {
453
		dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
A
Archit Taneja 已提交
454
		dsi_pll_uninit(dpi->dsidev, true);
455
	}
T
Tomi Valkeinen 已提交
456

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

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

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

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

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

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

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

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

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

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

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

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

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

502
	if (mgr && !dispc_mgr_timings_ok(mgr->id, timings))
T
Tomi Valkeinen 已提交
503 504
		return -EINVAL;

505
	if (timings->pixelclock == 0)
T
Tomi Valkeinen 已提交
506 507
		return -EINVAL;

A
Archit Taneja 已提交
508 509
	if (dpi->dsidev) {
		ok = dpi_dsi_clk_calc(dpi, timings->pixelclock, &ctx);
510 511
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
512

513
		fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC];
514
	} else {
515
		ok = dpi_dss_clk_calc(timings->pixelclock, &ctx);
516 517
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
518

519
		fck = ctx.fck;
T
Tomi Valkeinen 已提交
520
	}
521

522 523
	lck_div = ctx.dispc_cinfo.lck_div;
	pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
524

525
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
526

527
	timings->pixelclock = pck;
T
Tomi Valkeinen 已提交
528 529 530 531

	return 0;
}

532
static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
533
{
A
Archit Taneja 已提交
534
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
535

A
Archit Taneja 已提交
536
	mutex_lock(&dpi->lock);
537

A
Archit Taneja 已提交
538 539 540
	dpi->data_lines = data_lines;

	mutex_unlock(&dpi->lock);
541 542
}

543
static int dpi_verify_dsi_pll(struct platform_device *dsidev)
544 545 546 547 548
{
	int r;

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

549
	r = dsi_pll_init(dsidev);
550
	if (r)
551 552 553 554 555 556 557
		return r;

	dsi_pll_uninit(dsidev, true);

	return 0;
}

A
Archit Taneja 已提交
558
static int dpi_init_regulator(struct dpi_data *dpi)
559 560 561 562 563 564
{
	struct regulator *vdds_dsi;

	if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
		return 0;

A
Archit Taneja 已提交
565
	if (dpi->vdds_dsi_reg)
566 567
		return 0;

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

A
Archit Taneja 已提交
575
	dpi->vdds_dsi_reg = vdds_dsi;
576 577 578 579

	return 0;
}

A
Archit Taneja 已提交
580
static void dpi_init_pll(struct dpi_data *dpi)
581 582 583
{
	struct platform_device *dsidev;

A
Archit Taneja 已提交
584
	if (dpi->dsidev)
585 586
		return;

A
Archit Taneja 已提交
587
	dsidev = dpi_get_dsidev(dpi->output.dispc_channel);
588 589 590 591 592 593 594 595
	if (!dsidev)
		return;

	if (dpi_verify_dsi_pll(dsidev)) {
		DSSWARN("DSI PLL not operational\n");
		return;
	}

A
Archit Taneja 已提交
596
	dpi->dsidev = dsidev;
597 598
}

599 600 601 602 603 604
/*
 * 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.
 */
605
static enum omap_channel dpi_get_channel(int port_num)
606 607 608 609 610 611 612
{
	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:
613
	case OMAPDSS_VER_AM43xx:
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
		return OMAP_DSS_CHANNEL_LCD;

	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 已提交
630 631 632
static int dpi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
A
Archit Taneja 已提交
633
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
T
Tomi Valkeinen 已提交
634 635 636
	struct omap_overlay_manager *mgr;
	int r;

A
Archit Taneja 已提交
637
	r = dpi_init_regulator(dpi);
T
Tomi Valkeinen 已提交
638 639 640
	if (r)
		return r;

A
Archit Taneja 已提交
641
	dpi_init_pll(dpi);
T
Tomi Valkeinen 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

	mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
	if (!mgr)
		return -ENODEV;

	r = dss_mgr_connect(mgr, dssdev);
	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);
		dss_mgr_disconnect(mgr, dssdev);
		return r;
	}

	return 0;
}

static void dpi_disconnect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
665
	WARN_ON(dst != dssdev->dst);
T
Tomi Valkeinen 已提交
666

667
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
668 669 670 671 672 673 674 675 676 677 678 679
		return;

	omapdss_output_unset_device(dssdev);

	if (dssdev->manager)
		dss_mgr_disconnect(dssdev->manager, dssdev);
}

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

680 681
	.enable = dpi_display_enable,
	.disable = dpi_display_disable,
T
Tomi Valkeinen 已提交
682 683

	.check_timings = dpi_check_timings,
684
	.set_timings = dpi_set_timings,
T
Tomi Valkeinen 已提交
685 686
	.get_timings = dpi_get_timings,

687
	.set_data_lines = dpi_set_data_lines,
T
Tomi Valkeinen 已提交
688 689
};

690
static void dpi_init_output(struct platform_device *pdev)
691
{
A
Archit Taneja 已提交
692
	struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
A
Archit Taneja 已提交
693
	struct omap_dss_device *out = &dpi->output;
694

695
	out->dev = &pdev->dev;
696
	out->id = OMAP_DSS_OUTPUT_DPI;
697
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
T
Tomi Valkeinen 已提交
698
	out->name = "dpi.0";
699
	out->dispc_channel = dpi_get_channel(0);
T
Tomi Valkeinen 已提交
700
	out->ops.dpi = &dpi_ops;
701
	out->owner = THIS_MODULE;
702

703
	omapdss_register_output(out);
704 705 706 707
}

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

711
	omapdss_unregister_output(out);
712 713
}

714 715 716 717 718
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;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	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;
	}
738 739 740 741

	out->dev = &pdev->dev;
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
742 743
	out->dispc_channel = dpi_get_channel(port_num);
	out->port_num = port_num;
744 745 746 747 748 749 750 751 752 753 754 755 756 757
	out->ops.dpi = &dpi_ops;
	out->owner = THIS_MODULE;

	omapdss_register_output(out);
}

static void __exit dpi_uninit_output_port(struct device_node *port)
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;

	omapdss_unregister_output(out);
}

758
static int omap_dpi_probe(struct platform_device *pdev)
759
{
A
Archit Taneja 已提交
760 761 762 763 764
	struct dpi_data *dpi;

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

	dpi->pdev = pdev;
767

A
Archit Taneja 已提交
768 769 770
	dev_set_drvdata(&pdev->dev, dpi);

	mutex_init(&dpi->lock);
771

772 773
	dpi_init_output(pdev);

774 775 776
	return 0;
}

T
Tomi Valkeinen 已提交
777
static int __exit omap_dpi_remove(struct platform_device *pdev)
T
Tomi Valkeinen 已提交
778
{
779 780
	dpi_uninit_output(pdev);

781
	return 0;
T
Tomi Valkeinen 已提交
782 783
}

784
static struct platform_driver omap_dpi_driver = {
785
	.probe		= omap_dpi_probe,
T
Tomi Valkeinen 已提交
786
	.remove         = __exit_p(omap_dpi_remove),
787 788 789
	.driver         = {
		.name   = "omapdss_dpi",
		.owner  = THIS_MODULE,
T
Tomi Valkeinen 已提交
790
		.suppress_bind_attrs = true,
791 792 793
	},
};

T
Tomi Valkeinen 已提交
794
int __init dpi_init_platform_driver(void)
795
{
796
	return platform_driver_register(&omap_dpi_driver);
797 798
}

T
Tomi Valkeinen 已提交
799
void __exit dpi_uninit_platform_driver(void)
800 801 802
{
	platform_driver_unregister(&omap_dpi_driver);
}
T
Tomi Valkeinen 已提交
803 804 805

int __init dpi_init_port(struct platform_device *pdev, struct device_node *port)
{
A
Archit Taneja 已提交
806
	struct dpi_data *dpi;
T
Tomi Valkeinen 已提交
807 808 809 810
	struct device_node *ep;
	u32 datalines;
	int r;

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

T
Tomi Valkeinen 已提交
815 816 817 818 819 820 821 822 823 824
	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 已提交
825
	dpi->data_lines = datalines;
T
Tomi Valkeinen 已提交
826 827 828

	of_node_put(ep);

A
Archit Taneja 已提交
829
	dpi->pdev = pdev;
830
	port->data = dpi;
T
Tomi Valkeinen 已提交
831

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

834
	dpi_init_output_port(pdev, port);
T
Tomi Valkeinen 已提交
835

A
Archit Taneja 已提交
836
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
837 838 839 840 841 842 843 844 845

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

846
void __exit dpi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
847
{
848
	struct dpi_data *dpi = port->data;
A
Archit Taneja 已提交
849 850

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

853
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
854
}