dpi.c 18.6 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 "omapdss.h"
T
Tomi Valkeinen 已提交
38
#include "dss.h"
39
#include "dss_features.h"
T
Tomi Valkeinen 已提交
40

41 42
#define HSDIV_DISPC	0

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

46
	struct regulator *vdds_dsi_reg;
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 struct dss_pll *dpi_get_pll(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 86
		return NULL;

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_pll_find("dsi0");
93
		case OMAP_DSS_CHANNEL_LCD2:
94
			return dss_pll_find("dsi1");
95 96 97 98 99 100 101
		default:
			return NULL;
		}

	case OMAPDSS_VER_OMAP5:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
102
			return dss_pll_find("dsi0");
103
		case OMAP_DSS_CHANNEL_LCD3:
104
			return dss_pll_find("dsi1");
105 106 107 108
		default:
			return NULL;
		}

T
Tomi Valkeinen 已提交
109 110 111 112 113 114 115 116 117 118 119
	case OMAPDSS_VER_DRA7xx:
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
		case OMAP_DSS_CHANNEL_LCD2:
			return dss_pll_find("video0");
		case OMAP_DSS_CHANNEL_LCD3:
			return dss_pll_find("video1");
		default:
			return NULL;
		}

120 121 122
	default:
		return NULL;
	}
123 124
}

125
static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
126
{
127 128 129 130 131
	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;
132 133
	case OMAP_DSS_CHANNEL_LCD3:
		return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
134 135 136 137 138
	default:
		/* this shouldn't happen */
		WARN_ON(1);
		return OMAP_DSS_CLK_SRC_FCK;
	}
139 140
}

141
struct dpi_clk_calc_ctx {
142
	struct dss_pll *pll;
143 144 145 146 147 148 149

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

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


182
static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
183 184 185 186 187 188 189 190 191
		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.
	 */
192
	if (m_dispc > 1 && m_dispc % 2 != 0 && ctx->pck_min >= 100000000)
193 194
		return false;

195
	ctx->dsi_cinfo.mX[HSDIV_DISPC] = m_dispc;
196
	ctx->dsi_cinfo.clkout[HSDIV_DISPC] = dispc;
197 198 199 200 201 202

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


203 204
static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
		unsigned long clkdco,
205 206 207 208
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

209 210
	ctx->dsi_cinfo.n = n;
	ctx->dsi_cinfo.m = m;
211
	ctx->dsi_cinfo.fint = fint;
212
	ctx->dsi_cinfo.clkdco = clkdco;
213

214 215 216
	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);
217 218
}

219
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
220 221 222
{
	struct dpi_clk_calc_ctx *ctx = data;

223
	ctx->fck = fck;
224 225 226 227 228

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

A
Archit Taneja 已提交
229 230
static bool dpi_dsi_clk_calc(struct dpi_data *dpi, unsigned long pck,
		struct dpi_clk_calc_ctx *ctx)
231 232 233 234 235
{
	unsigned long clkin;
	unsigned long pll_min, pll_max;

	memset(ctx, 0, sizeof(*ctx));
236
	ctx->pll = dpi->pll;
237 238 239 240 241 242
	ctx->pck_min = pck - 1000;
	ctx->pck_max = pck + 1000;

	pll_min = 0;
	pll_max = 0;

243 244 245
	clkin = clk_get_rate(ctx->pll->clkin);

	return dss_pll_calc(ctx->pll, clkin,
246 247 248 249 250 251 252 253 254 255 256 257
			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
258
	 * +/- ~15MHz.
259 260
	 */

261
	for (i = 0; i < 25; ++i) {
262 263 264 265 266 267 268 269 270
		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;

271
		ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
272 273 274 275 276 277 278 279 280
		if (ok)
			return ok;
	}

	return false;
}



A
Archit Taneja 已提交
281
static int dpi_set_dsi_clk(struct dpi_data *dpi, enum omap_channel channel,
282 283
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
T
Tomi Valkeinen 已提交
284
{
285
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
286
	int r;
287
	bool ok;
T
Tomi Valkeinen 已提交
288

A
Archit Taneja 已提交
289
	ok = dpi_dsi_clk_calc(dpi, pck_req, &ctx);
290 291
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
292

293
	r = dss_pll_set_config(dpi->pll, &ctx.dsi_cinfo);
T
Tomi Valkeinen 已提交
294 295 296
	if (r)
		return r;

297 298
	dss_select_lcd_clk_source(channel,
			dpi_get_alt_clk_src(channel));
T
Tomi Valkeinen 已提交
299

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

302
	*fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC];
303 304
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
305 306 307

	return 0;
}
308

A
Archit Taneja 已提交
309 310
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 已提交
311
{
312
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
313
	int r;
314
	bool ok;
T
Tomi Valkeinen 已提交
315

316 317 318
	ok = dpi_dss_clk_calc(pck_req, &ctx);
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
319

320
	r = dss_set_fck_rate(ctx.fck);
T
Tomi Valkeinen 已提交
321 322 323
	if (r)
		return r;

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

326
	*fck = ctx.fck;
327 328
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
329 330 331 332

	return 0;
}

A
Archit Taneja 已提交
333
static int dpi_set_mode(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
334
{
A
Archit Taneja 已提交
335
	struct omap_dss_device *out = &dpi->output;
336
	enum omap_channel channel = out->dispc_channel;
A
Archit Taneja 已提交
337
	struct omap_video_timings *t = &dpi->timings;
338 339
	int lck_div = 0, pck_div = 0;
	unsigned long fck = 0;
T
Tomi Valkeinen 已提交
340 341 342
	unsigned long pck;
	int r = 0;

343
	if (dpi->pll)
344
		r = dpi_set_dsi_clk(dpi, channel, t->pixelclock, &fck,
345
				&lck_div, &pck_div);
346
	else
A
Archit Taneja 已提交
347
		r = dpi_set_dispc_clk(dpi, t->pixelclock, &fck,
348
				&lck_div, &pck_div);
T
Tomi Valkeinen 已提交
349
	if (r)
350
		return r;
T
Tomi Valkeinen 已提交
351

352
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
353

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

358
		t->pixelclock = pck;
T
Tomi Valkeinen 已提交
359 360
	}

361
	dss_mgr_set_timings(channel, t);
T
Tomi Valkeinen 已提交
362

363
	return 0;
T
Tomi Valkeinen 已提交
364 365
}

A
Archit Taneja 已提交
366
static void dpi_config_lcd_manager(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
367
{
A
Archit Taneja 已提交
368
	struct omap_dss_device *out = &dpi->output;
369
	enum omap_channel channel = out->dispc_channel;
A
Archit Taneja 已提交
370 371

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
372

A
Archit Taneja 已提交
373 374
	dpi->mgr_config.stallmode = false;
	dpi->mgr_config.fifohandcheck = false;
375

A
Archit Taneja 已提交
376
	dpi->mgr_config.video_port_width = dpi->data_lines;
377

A
Archit Taneja 已提交
378
	dpi->mgr_config.lcden_sig_polarity = 0;
379

380
	dss_mgr_set_lcd_config(channel, &dpi->mgr_config);
T
Tomi Valkeinen 已提交
381 382
}

383
static int dpi_display_enable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
384
{
A
Archit Taneja 已提交
385
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
386
	struct omap_dss_device *out = &dpi->output;
387
	enum omap_channel channel = out->dispc_channel;
T
Tomi Valkeinen 已提交
388 389
	int r;

A
Archit Taneja 已提交
390
	mutex_lock(&dpi->lock);
391

A
Archit Taneja 已提交
392
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi->vdds_dsi_reg) {
393
		DSSERR("no VDSS_DSI regulator\n");
394 395
		r = -ENODEV;
		goto err_no_reg;
396 397
	}

398
	if (!out->dispc_channel_connected) {
399
		DSSERR("failed to enable display: no output/manager\n");
400
		r = -ENODEV;
401
		goto err_no_out_mgr;
402 403
	}

404
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
A
Archit Taneja 已提交
405
		r = regulator_enable(dpi->vdds_dsi_reg);
406
		if (r)
407
			goto err_reg_enable;
408 409
	}

410
	r = dispc_runtime_get();
T
Tomi Valkeinen 已提交
411
	if (r)
412 413
		goto err_get_dispc;

414
	r = dss_dpi_select_source(out->port_num, channel);
415 416 417
	if (r)
		goto err_src_sel;

418 419
	if (dpi->pll) {
		r = dss_pll_enable(dpi->pll);
420
		if (r)
421
			goto err_dsi_pll_init;
422 423
	}

A
Archit Taneja 已提交
424
	r = dpi_set_mode(dpi);
T
Tomi Valkeinen 已提交
425
	if (r)
426
		goto err_set_mode;
T
Tomi Valkeinen 已提交
427

A
Archit Taneja 已提交
428
	dpi_config_lcd_manager(dpi);
429

T
Tomi Valkeinen 已提交
430 431
	mdelay(2);

432
	r = dss_mgr_enable(channel);
433 434
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
435

A
Archit Taneja 已提交
436
	mutex_unlock(&dpi->lock);
437

T
Tomi Valkeinen 已提交
438 439
	return 0;

440
err_mgr_enable:
441
err_set_mode:
442 443
	if (dpi->pll)
		dss_pll_disable(dpi->pll);
444
err_dsi_pll_init:
445
err_src_sel:
446 447
	dispc_runtime_put();
err_get_dispc:
448
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
A
Archit Taneja 已提交
449
		regulator_disable(dpi->vdds_dsi_reg);
450
err_reg_enable:
451
err_no_out_mgr:
452
err_no_reg:
A
Archit Taneja 已提交
453
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
454 455 456
	return r;
}

457
static void dpi_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
458
{
A
Archit Taneja 已提交
459
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
460
	enum omap_channel channel = dpi->output.dispc_channel;
461

A
Archit Taneja 已提交
462
	mutex_lock(&dpi->lock);
463

464
	dss_mgr_disable(channel);
T
Tomi Valkeinen 已提交
465

466
	if (dpi->pll) {
467
		dss_select_lcd_clk_source(channel, OMAP_DSS_CLK_SRC_FCK);
468
		dss_pll_disable(dpi->pll);
469
	}
T
Tomi Valkeinen 已提交
470

471
	dispc_runtime_put();
T
Tomi Valkeinen 已提交
472

473
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
A
Archit Taneja 已提交
474
		regulator_disable(dpi->vdds_dsi_reg);
475

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

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

T
Tomi Valkeinen 已提交
484
	DSSDBG("dpi_set_timings\n");
485

A
Archit Taneja 已提交
486
	mutex_lock(&dpi->lock);
487

A
Archit Taneja 已提交
488
	dpi->timings = *timings;
489

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

T
Tomi Valkeinen 已提交
493 494 495
static void dpi_get_timings(struct omap_dss_device *dssdev,
		struct omap_video_timings *timings)
{
A
Archit Taneja 已提交
496
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
497 498

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

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

A
Archit Taneja 已提交
502
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
503 504
}

505
static int dpi_check_timings(struct omap_dss_device *dssdev,
T
Tomi Valkeinen 已提交
506 507
			struct omap_video_timings *timings)
{
A
Archit Taneja 已提交
508
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
509
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
510 511 512
	int lck_div, pck_div;
	unsigned long fck;
	unsigned long pck;
513 514
	struct dpi_clk_calc_ctx ctx;
	bool ok;
T
Tomi Valkeinen 已提交
515

516 517 518
	if (timings->x_res % 8 != 0)
		return -EINVAL;

519
	if (!dispc_mgr_timings_ok(channel, timings))
T
Tomi Valkeinen 已提交
520 521
		return -EINVAL;

522
	if (timings->pixelclock == 0)
T
Tomi Valkeinen 已提交
523 524
		return -EINVAL;

525
	if (dpi->pll) {
A
Archit Taneja 已提交
526
		ok = dpi_dsi_clk_calc(dpi, timings->pixelclock, &ctx);
527 528
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
529

530
		fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC];
531
	} else {
532
		ok = dpi_dss_clk_calc(timings->pixelclock, &ctx);
533 534
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
535

536
		fck = ctx.fck;
T
Tomi Valkeinen 已提交
537
	}
538

539 540
	lck_div = ctx.dispc_cinfo.lck_div;
	pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
541

542
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
543

544
	timings->pixelclock = pck;
T
Tomi Valkeinen 已提交
545 546 547 548

	return 0;
}

549
static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
550
{
A
Archit Taneja 已提交
551
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
552

A
Archit Taneja 已提交
553
	mutex_lock(&dpi->lock);
554

A
Archit Taneja 已提交
555 556 557
	dpi->data_lines = data_lines;

	mutex_unlock(&dpi->lock);
558 559
}

560
static int dpi_verify_dsi_pll(struct dss_pll *pll)
561 562 563 564 565
{
	int r;

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

566
	r = dss_pll_enable(pll);
567
	if (r)
568 569
		return r;

570
	dss_pll_disable(pll);
571 572 573 574

	return 0;
}

A
Archit Taneja 已提交
575
static int dpi_init_regulator(struct dpi_data *dpi)
576 577 578 579 580 581
{
	struct regulator *vdds_dsi;

	if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
		return 0;

A
Archit Taneja 已提交
582
	if (dpi->vdds_dsi_reg)
583 584
		return 0;

A
Archit Taneja 已提交
585
	vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi");
586
	if (IS_ERR(vdds_dsi)) {
587 588
		if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
			DSSERR("can't get VDDS_DSI regulator\n");
589
		return PTR_ERR(vdds_dsi);
590 591
	}

A
Archit Taneja 已提交
592
	dpi->vdds_dsi_reg = vdds_dsi;
593 594 595 596

	return 0;
}

A
Archit Taneja 已提交
597
static void dpi_init_pll(struct dpi_data *dpi)
598
{
599
	struct dss_pll *pll;
600

601
	if (dpi->pll)
602 603
		return;

604 605
	pll = dpi_get_pll(dpi->output.dispc_channel);
	if (!pll)
606 607
		return;

T
Tomi Valkeinen 已提交
608 609 610 611
	/* On DRA7 we need to set a mux to use the PLL */
	if (omapdss_get_version() == OMAPDSS_VER_DRA7xx)
		dss_ctrl_pll_set_control_mux(pll->id, dpi->output.dispc_channel);

612
	if (dpi_verify_dsi_pll(pll)) {
613 614 615 616
		DSSWARN("DSI PLL not operational\n");
		return;
	}

617
	dpi->pll = pll;
618 619
}

620 621 622 623 624 625
/*
 * 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.
 */
626
static enum omap_channel dpi_get_channel(int port_num)
627 628 629 630 631 632 633
{
	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:
634
	case OMAPDSS_VER_AM43xx:
635 636
		return OMAP_DSS_CHANNEL_LCD;

T
Tomi Valkeinen 已提交
637 638 639 640 641 642 643 644 645 646 647
	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;
		}

648 649 650 651 652 653 654 655 656 657 658 659 660 661
	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 已提交
662 663 664
static int dpi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
A
Archit Taneja 已提交
665
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
666
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
667 668
	int r;

A
Archit Taneja 已提交
669
	r = dpi_init_regulator(dpi);
T
Tomi Valkeinen 已提交
670 671 672
	if (r)
		return r;

A
Archit Taneja 已提交
673
	dpi_init_pll(dpi);
T
Tomi Valkeinen 已提交
674

675
	r = dss_mgr_connect(channel, dssdev);
T
Tomi Valkeinen 已提交
676 677 678 679 680 681 682
	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);
683
		dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
684 685 686 687 688 689 690 691 692
		return r;
	}

	return 0;
}

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

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

698
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
699 700 701 702
		return;

	omapdss_output_unset_device(dssdev);

703
	dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
704 705 706 707 708 709
}

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

710 711
	.enable = dpi_display_enable,
	.disable = dpi_display_disable,
T
Tomi Valkeinen 已提交
712 713

	.check_timings = dpi_check_timings,
714
	.set_timings = dpi_set_timings,
T
Tomi Valkeinen 已提交
715 716
	.get_timings = dpi_get_timings,

717
	.set_data_lines = dpi_set_data_lines,
T
Tomi Valkeinen 已提交
718 719
};

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

725
	out->dev = &pdev->dev;
726
	out->id = OMAP_DSS_OUTPUT_DPI;
727
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
T
Tomi Valkeinen 已提交
728
	out->name = "dpi.0";
729
	out->dispc_channel = dpi_get_channel(0);
T
Tomi Valkeinen 已提交
730
	out->ops.dpi = &dpi_ops;
731
	out->owner = THIS_MODULE;
732

733
	omapdss_register_output(out);
734 735
}

736
static void dpi_uninit_output(struct platform_device *pdev)
737
{
A
Archit Taneja 已提交
738
	struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
A
Archit Taneja 已提交
739
	struct omap_dss_device *out = &dpi->output;
740

741
	omapdss_unregister_output(out);
742 743
}

744 745 746 747 748
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;
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	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;
	}
768 769 770 771

	out->dev = &pdev->dev;
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
772 773
	out->dispc_channel = dpi_get_channel(port_num);
	out->port_num = port_num;
774 775 776 777 778 779
	out->ops.dpi = &dpi_ops;
	out->owner = THIS_MODULE;

	omapdss_register_output(out);
}

780
static void dpi_uninit_output_port(struct device_node *port)
781 782 783 784 785 786 787
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;

	omapdss_unregister_output(out);
}

T
Tomi Valkeinen 已提交
788
static int dpi_bind(struct device *dev, struct device *master, void *data)
789
{
T
Tomi Valkeinen 已提交
790
	struct platform_device *pdev = to_platform_device(dev);
A
Archit Taneja 已提交
791 792 793 794 795
	struct dpi_data *dpi;

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

	dpi->pdev = pdev;
798

A
Archit Taneja 已提交
799 800 801
	dev_set_drvdata(&pdev->dev, dpi);

	mutex_init(&dpi->lock);
802

803 804
	dpi_init_output(pdev);

805 806 807
	return 0;
}

T
Tomi Valkeinen 已提交
808
static void dpi_unbind(struct device *dev, struct device *master, void *data)
T
Tomi Valkeinen 已提交
809
{
T
Tomi Valkeinen 已提交
810 811
	struct platform_device *pdev = to_platform_device(dev);

812
	dpi_uninit_output(pdev);
T
Tomi Valkeinen 已提交
813 814 815 816 817 818
}

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

T
Tomi Valkeinen 已提交
820 821 822 823 824 825 826 827
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);
828
	return 0;
T
Tomi Valkeinen 已提交
829 830
}

831
static struct platform_driver omap_dpi_driver = {
T
Tomi Valkeinen 已提交
832 833
	.probe		= dpi_probe,
	.remove		= dpi_remove,
834 835
	.driver         = {
		.name   = "omapdss_dpi",
T
Tomi Valkeinen 已提交
836
		.suppress_bind_attrs = true,
837 838 839
	},
};

T
Tomi Valkeinen 已提交
840
int __init dpi_init_platform_driver(void)
841
{
842
	return platform_driver_register(&omap_dpi_driver);
843 844
}

845
void dpi_uninit_platform_driver(void)
846 847 848
{
	platform_driver_unregister(&omap_dpi_driver);
}
T
Tomi Valkeinen 已提交
849

850
int dpi_init_port(struct platform_device *pdev, struct device_node *port)
T
Tomi Valkeinen 已提交
851
{
A
Archit Taneja 已提交
852
	struct dpi_data *dpi;
T
Tomi Valkeinen 已提交
853 854 855 856
	struct device_node *ep;
	u32 datalines;
	int r;

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

T
Tomi Valkeinen 已提交
861 862 863 864 865 866 867 868 869 870
	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 已提交
871
	dpi->data_lines = datalines;
T
Tomi Valkeinen 已提交
872 873 874

	of_node_put(ep);

A
Archit Taneja 已提交
875
	dpi->pdev = pdev;
876
	port->data = dpi;
T
Tomi Valkeinen 已提交
877

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

880
	dpi_init_output_port(pdev, port);
T
Tomi Valkeinen 已提交
881

A
Archit Taneja 已提交
882
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
883 884 885 886 887 888 889 890 891

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

892
void dpi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
893
{
894
	struct dpi_data *dpi = port->data;
A
Archit Taneja 已提交
895 896

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

899
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
900
}