dpi.c 16.4 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>
35
#include <linux/sys_soc.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

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

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 videomode vm;
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static enum dss_clk_source dpi_get_clk_src_dra7xx(enum omap_channel channel)
{
	/*
	 * Possible clock sources:
	 * LCD1: FCK/PLL1_1/HDMI_PLL
	 * LCD2: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_3)
	 * LCD3: FCK/PLL1_3/HDMI_PLL (DRA74x: PLL2_1)
	 */

	switch (channel) {
	case OMAP_DSS_CHANNEL_LCD:
	{
		if (dss_pll_find_by_src(DSS_CLK_SRC_PLL1_1))
			return DSS_CLK_SRC_PLL1_1;
		break;
	}
	case OMAP_DSS_CHANNEL_LCD2:
	{
		if (dss_pll_find_by_src(DSS_CLK_SRC_PLL1_3))
			return DSS_CLK_SRC_PLL1_3;
		if (dss_pll_find_by_src(DSS_CLK_SRC_PLL2_3))
			return DSS_CLK_SRC_PLL2_3;
		break;
	}
	case OMAP_DSS_CHANNEL_LCD3:
	{
		if (dss_pll_find_by_src(DSS_CLK_SRC_PLL2_1))
			return DSS_CLK_SRC_PLL2_1;
		if (dss_pll_find_by_src(DSS_CLK_SRC_PLL1_3))
			return DSS_CLK_SRC_PLL1_3;
		break;
	}
	default:
		break;
	}

	return DSS_CLK_SRC_FCK;
}

104
static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
105
{
106 107
	enum omap_channel channel = dpi->output.dispc_channel;

108 109 110 111 112
	/*
	 * 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.
	 */
113 114 115
	switch (dpi->dss_model) {
	case DSS_MODEL_OMAP2:
	case DSS_MODEL_OMAP3:
116
		return DSS_CLK_SRC_FCK;
117

118
	case DSS_MODEL_OMAP4:
119 120
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
121
			return DSS_CLK_SRC_PLL1_1;
122
		case OMAP_DSS_CHANNEL_LCD2:
123
			return DSS_CLK_SRC_PLL2_1;
124
		default:
125
			return DSS_CLK_SRC_FCK;
126 127
		}

128
	case DSS_MODEL_OMAP5:
129 130
		switch (channel) {
		case OMAP_DSS_CHANNEL_LCD:
131
			return DSS_CLK_SRC_PLL1_1;
132
		case OMAP_DSS_CHANNEL_LCD3:
133 134
			return DSS_CLK_SRC_PLL2_1;
		case OMAP_DSS_CHANNEL_LCD2:
135
		default:
136
			return DSS_CLK_SRC_FCK;
137 138
		}

139
	case DSS_MODEL_DRA7:
140
		return dpi_get_clk_src_dra7xx(channel);
T
Tomi Valkeinen 已提交
141

142
	default:
143
		return DSS_CLK_SRC_FCK;
144
	}
145 146
}

147
struct dpi_clk_calc_ctx {
148
	struct dss_pll *pll;
149
	unsigned clkout_idx;
150 151 152 153 154 155 156

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

157
	struct dss_pll_clock_info pll_cinfo;
T
Tomi Valkeinen 已提交
158
	unsigned long fck;
159 160 161 162 163 164 165 166 167 168 169 170 171
	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.
	 */
172
	if (ctx->pck_min >= 100000000) {
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		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;
}


189
static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
190 191 192 193
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

194 195
	ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
	ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
196 197 198 199 200 201

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


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

208 209 210 211
	ctx->pll_cinfo.n = n;
	ctx->pll_cinfo.m = m;
	ctx->pll_cinfo.fint = fint;
	ctx->pll_cinfo.clkdco = clkdco;
212

213
	return dss_pll_hsdiv_calc_a(ctx->pll, clkdco,
214 215
		ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK),
		dpi_calc_hsdiv_cb, ctx);
216 217
}

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

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

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

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

	memset(ctx, 0, sizeof(*ctx));
234
	ctx->pll = dpi->pll;
235
	ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
236

237
	clkin = clk_get_rate(dpi->pll->clkin);
238

239 240
	if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
		unsigned long pll_min, pll_max;
241

242 243 244 245 246 247 248 249 250 251
		ctx->pck_min = pck - 1000;
		ctx->pck_max = pck + 1000;

		pll_min = 0;
		pll_max = 0;

		return dss_pll_calc_a(ctx->pll, clkin,
				pll_min, pll_max,
				dpi_calc_pll_cb, ctx);
	} else { /* DSS_PLL_TYPE_B */
252
		dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
253 254 255

		ctx->dispc_cinfo.lck_div = 1;
		ctx->dispc_cinfo.pck_div = 1;
256
		ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
257 258 259 260
		ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;

		return true;
	}
261 262 263 264 265 266 267 268 269 270
}

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
271
	 * +/- ~15MHz.
272 273
	 */

274
	for (i = 0; i < 25; ++i) {
275 276 277 278 279 280 281 282 283
		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;

284
		ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
285 286 287 288 289 290 291 292 293
		if (ok)
			return ok;
	}

	return false;
}



294
static int dpi_set_pll_clk(struct dpi_data *dpi, enum omap_channel channel,
295 296
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
T
Tomi Valkeinen 已提交
297
{
298
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
299
	int r;
300
	bool ok;
T
Tomi Valkeinen 已提交
301

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

306
	r = dss_pll_set_config(dpi->pll, &ctx.pll_cinfo);
T
Tomi Valkeinen 已提交
307 308 309
	if (r)
		return r;

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

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

314
	*fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
315 316
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
317 318 319

	return 0;
}
320

A
Archit Taneja 已提交
321 322
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 已提交
323
{
324
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
325
	int r;
326
	bool ok;
T
Tomi Valkeinen 已提交
327

328 329 330
	ok = dpi_dss_clk_calc(pck_req, &ctx);
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
331

332
	r = dss_set_fck_rate(ctx.fck);
T
Tomi Valkeinen 已提交
333 334 335
	if (r)
		return r;

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

338
	*fck = ctx.fck;
339 340
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
341 342 343 344

	return 0;
}

A
Archit Taneja 已提交
345
static int dpi_set_mode(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
346
{
A
Archit Taneja 已提交
347
	struct omap_dss_device *out = &dpi->output;
348
	enum omap_channel channel = out->dispc_channel;
349
	struct videomode *vm = &dpi->vm;
350 351
	int lck_div = 0, pck_div = 0;
	unsigned long fck = 0;
T
Tomi Valkeinen 已提交
352 353 354
	unsigned long pck;
	int r = 0;

355
	if (dpi->pll)
356
		r = dpi_set_pll_clk(dpi, channel, vm->pixelclock, &fck,
357
				&lck_div, &pck_div);
358
	else
359
		r = dpi_set_dispc_clk(dpi, vm->pixelclock, &fck,
360
				&lck_div, &pck_div);
T
Tomi Valkeinen 已提交
361
	if (r)
362
		return r;
T
Tomi Valkeinen 已提交
363

364
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
365

366
	if (pck != vm->pixelclock) {
367
		DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
368
			vm->pixelclock, pck);
T
Tomi Valkeinen 已提交
369

370
		vm->pixelclock = pck;
T
Tomi Valkeinen 已提交
371 372
	}

373
	dss_mgr_set_timings(channel, vm);
T
Tomi Valkeinen 已提交
374

375
	return 0;
T
Tomi Valkeinen 已提交
376 377
}

A
Archit Taneja 已提交
378
static void dpi_config_lcd_manager(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
379
{
A
Archit Taneja 已提交
380
	struct omap_dss_device *out = &dpi->output;
381
	enum omap_channel channel = out->dispc_channel;
A
Archit Taneja 已提交
382 383

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
384

A
Archit Taneja 已提交
385 386
	dpi->mgr_config.stallmode = false;
	dpi->mgr_config.fifohandcheck = false;
387

A
Archit Taneja 已提交
388
	dpi->mgr_config.video_port_width = dpi->data_lines;
389

A
Archit Taneja 已提交
390
	dpi->mgr_config.lcden_sig_polarity = 0;
391

392
	dss_mgr_set_lcd_config(channel, &dpi->mgr_config);
T
Tomi Valkeinen 已提交
393 394
}

395
static int dpi_display_enable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
396
{
A
Archit Taneja 已提交
397
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
398
	struct omap_dss_device *out = &dpi->output;
399
	enum omap_channel channel = out->dispc_channel;
T
Tomi Valkeinen 已提交
400 401
	int r;

A
Archit Taneja 已提交
402
	mutex_lock(&dpi->lock);
403

404
	if (!out->dispc_channel_connected) {
405
		DSSERR("failed to enable display: no output/manager\n");
406
		r = -ENODEV;
407
		goto err_no_out_mgr;
408 409
	}

410
	if (dpi->vdds_dsi_reg) {
A
Archit Taneja 已提交
411
		r = regulator_enable(dpi->vdds_dsi_reg);
412
		if (r)
413
			goto err_reg_enable;
414 415
	}

416
	r = dispc_runtime_get();
T
Tomi Valkeinen 已提交
417
	if (r)
418 419
		goto err_get_dispc;

420
	r = dss_dpi_select_source(out->port_num, channel);
421 422 423
	if (r)
		goto err_src_sel;

424 425
	if (dpi->pll) {
		r = dss_pll_enable(dpi->pll);
426
		if (r)
427
			goto err_pll_init;
428 429
	}

A
Archit Taneja 已提交
430
	r = dpi_set_mode(dpi);
T
Tomi Valkeinen 已提交
431
	if (r)
432
		goto err_set_mode;
T
Tomi Valkeinen 已提交
433

A
Archit Taneja 已提交
434
	dpi_config_lcd_manager(dpi);
435

T
Tomi Valkeinen 已提交
436 437
	mdelay(2);

438
	r = dss_mgr_enable(channel);
439 440
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
441

A
Archit Taneja 已提交
442
	mutex_unlock(&dpi->lock);
443

T
Tomi Valkeinen 已提交
444 445
	return 0;

446
err_mgr_enable:
447
err_set_mode:
448 449
	if (dpi->pll)
		dss_pll_disable(dpi->pll);
450
err_pll_init:
451
err_src_sel:
452 453
	dispc_runtime_put();
err_get_dispc:
454
	if (dpi->vdds_dsi_reg)
A
Archit Taneja 已提交
455
		regulator_disable(dpi->vdds_dsi_reg);
456
err_reg_enable:
457
err_no_out_mgr:
A
Archit Taneja 已提交
458
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
459 460 461
	return r;
}

462
static void dpi_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
463
{
A
Archit Taneja 已提交
464
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
465
	enum omap_channel channel = dpi->output.dispc_channel;
466

A
Archit Taneja 已提交
467
	mutex_lock(&dpi->lock);
468

469
	dss_mgr_disable(channel);
T
Tomi Valkeinen 已提交
470

471
	if (dpi->pll) {
472
		dss_select_lcd_clk_source(channel, DSS_CLK_SRC_FCK);
473
		dss_pll_disable(dpi->pll);
474
	}
T
Tomi Valkeinen 已提交
475

476
	dispc_runtime_put();
T
Tomi Valkeinen 已提交
477

478
	if (dpi->vdds_dsi_reg)
A
Archit Taneja 已提交
479
		regulator_disable(dpi->vdds_dsi_reg);
480

A
Archit Taneja 已提交
481
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
482 483
}

484
static void dpi_set_timings(struct omap_dss_device *dssdev,
485
			    struct videomode *vm)
T
Tomi Valkeinen 已提交
486
{
A
Archit Taneja 已提交
487
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
488

T
Tomi Valkeinen 已提交
489
	DSSDBG("dpi_set_timings\n");
490

A
Archit Taneja 已提交
491
	mutex_lock(&dpi->lock);
492

493
	dpi->vm = *vm;
494

A
Archit Taneja 已提交
495
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
496 497
}

T
Tomi Valkeinen 已提交
498
static void dpi_get_timings(struct omap_dss_device *dssdev,
499
			    struct videomode *vm)
T
Tomi Valkeinen 已提交
500
{
A
Archit Taneja 已提交
501
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
502 503

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

505
	*vm = dpi->vm;
T
Tomi Valkeinen 已提交
506

A
Archit Taneja 已提交
507
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
508 509
}

510
static int dpi_check_timings(struct omap_dss_device *dssdev,
511
			     struct videomode *vm)
T
Tomi Valkeinen 已提交
512
{
A
Archit Taneja 已提交
513
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
514
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
515 516 517
	int lck_div, pck_div;
	unsigned long fck;
	unsigned long pck;
518 519
	struct dpi_clk_calc_ctx ctx;
	bool ok;
T
Tomi Valkeinen 已提交
520

521
	if (vm->hactive % 8 != 0)
522 523
		return -EINVAL;

524
	if (!dispc_mgr_timings_ok(channel, vm))
T
Tomi Valkeinen 已提交
525 526
		return -EINVAL;

527
	if (vm->pixelclock == 0)
T
Tomi Valkeinen 已提交
528 529
		return -EINVAL;

530
	if (dpi->pll) {
531
		ok = dpi_pll_clk_calc(dpi, vm->pixelclock, &ctx);
532 533
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
534

535
		fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
536
	} else {
537
		ok = dpi_dss_clk_calc(vm->pixelclock, &ctx);
538 539
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
540

541
		fck = ctx.fck;
T
Tomi Valkeinen 已提交
542
	}
543

544 545
	lck_div = ctx.dispc_cinfo.lck_div;
	pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
546

547
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
548

549
	vm->pixelclock = pck;
T
Tomi Valkeinen 已提交
550 551 552 553

	return 0;
}

554
static int dpi_verify_pll(struct dss_pll *pll)
555 556 557 558 559
{
	int r;

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

560
	r = dss_pll_enable(pll);
561
	if (r)
562 563
		return r;

564
	dss_pll_disable(pll);
565 566 567 568

	return 0;
}

569 570 571 572 573 574
static const struct soc_device_attribute dpi_soc_devices[] = {
	{ .family = "OMAP3[456]*" },
	{ .family = "[AD]M37*" },
	{ /* sentinel */ }
};

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

579 580 581 582 583
	/*
	 * The DPI uses the DSI VDDS on OMAP34xx, OMAP35xx, OMAP36xx, AM37xx and
	 * DM37xx only.
	 */
	if (!soc_device_match(dpi_soc_devices))
584 585
		return 0;

A
Archit Taneja 已提交
586
	if (dpi->vdds_dsi_reg)
587 588
		return 0;

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

A
Archit Taneja 已提交
596
	dpi->vdds_dsi_reg = vdds_dsi;
597 598 599 600

	return 0;
}

A
Archit Taneja 已提交
601
static void dpi_init_pll(struct dpi_data *dpi)
602
{
603
	struct dss_pll *pll;
604

605
	if (dpi->pll)
606 607
		return;

608
	dpi->clk_src = dpi_get_clk_src(dpi);
609 610

	pll = dss_pll_find_by_src(dpi->clk_src);
611
	if (!pll)
612 613
		return;

614 615
	if (dpi_verify_pll(pll)) {
		DSSWARN("PLL not operational\n");
616 617 618
		return;
	}

619
	dpi->pll = pll;
620 621
}

622 623 624 625 626 627
/*
 * 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.
 */
628
static enum omap_channel dpi_get_channel(struct dpi_data *dpi, int port_num)
629
{
630 631 632
	switch (dpi->dss_model) {
	case DSS_MODEL_OMAP2:
	case DSS_MODEL_OMAP3:
633 634
		return OMAP_DSS_CHANNEL_LCD;

635
	case DSS_MODEL_DRA7:
T
Tomi Valkeinen 已提交
636 637 638 639 640 641 642 643 644 645
		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;
		}

646
	case DSS_MODEL_OMAP4:
647 648
		return OMAP_DSS_CHANNEL_LCD2;

649
	case DSS_MODEL_OMAP5:
650 651 652 653 654 655 656 657
		return OMAP_DSS_CHANNEL_LCD3;

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

T
Tomi Valkeinen 已提交
658 659 660
static int dpi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
A
Archit Taneja 已提交
661
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
662
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
663 664
	int r;

A
Archit Taneja 已提交
665
	r = dpi_init_regulator(dpi);
T
Tomi Valkeinen 已提交
666 667 668
	if (r)
		return r;

A
Archit Taneja 已提交
669
	dpi_init_pll(dpi);
T
Tomi Valkeinen 已提交
670

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

	return 0;
}

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

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

694
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
695 696 697 698
		return;

	omapdss_output_unset_device(dssdev);

699
	dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
700 701 702 703 704 705
}

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

706 707
	.enable = dpi_display_enable,
	.disable = dpi_display_disable,
T
Tomi Valkeinen 已提交
708 709

	.check_timings = dpi_check_timings,
710
	.set_timings = dpi_set_timings,
T
Tomi Valkeinen 已提交
711 712 713
	.get_timings = dpi_get_timings,
};

714
static void dpi_init_output_port(struct dpi_data *dpi, struct device_node *port)
715 716
{
	struct omap_dss_device *out = &dpi->output;
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	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;
	}
736

737
	out->dev = &dpi->pdev->dev;
738 739
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
740
	out->dispc_channel = dpi_get_channel(dpi, port_num);
741
	out->port_num = port_num;
742 743 744 745 746 747
	out->ops.dpi = &dpi_ops;
	out->owner = THIS_MODULE;

	omapdss_register_output(out);
}

748
static void dpi_uninit_output_port(struct device_node *port)
749 750 751 752 753 754 755
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;

	omapdss_unregister_output(out);
}

756 757
int dpi_init_port(struct platform_device *pdev, struct device_node *port,
		  enum dss_model dss_model)
T
Tomi Valkeinen 已提交
758
{
A
Archit Taneja 已提交
759
	struct dpi_data *dpi;
T
Tomi Valkeinen 已提交
760 761 762 763
	struct device_node *ep;
	u32 datalines;
	int r;

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

768
	ep = of_get_next_child(port, NULL);
T
Tomi Valkeinen 已提交
769 770 771 772 773 774 775 776 777
	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 已提交
778
	dpi->data_lines = datalines;
T
Tomi Valkeinen 已提交
779 780 781

	of_node_put(ep);

A
Archit Taneja 已提交
782
	dpi->pdev = pdev;
783
	dpi->dss_model = dss_model;
784
	port->data = dpi;
T
Tomi Valkeinen 已提交
785

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

788
	dpi_init_output_port(dpi, port);
T
Tomi Valkeinen 已提交
789

A
Archit Taneja 已提交
790
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
791 792 793 794 795 796 797 798 799

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

800
void dpi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
801
{
802
	struct dpi_data *dpi = port->data;
A
Archit Taneja 已提交
803 804

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

807
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
808
}