dpi.c 16.5 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
/*
 * 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>
25
#include <linux/export.h>
26
#include <linux/err.h>
T
Tomi Valkeinen 已提交
27
#include <linux/errno.h>
28 29
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
30
#include <linux/string.h>
T
Tomi Valkeinen 已提交
31
#include <linux/of.h>
32
#include <linux/clk.h>
33
#include <linux/sys_soc.h>
T
Tomi Valkeinen 已提交
34

35
#include "omapdss.h"
T
Tomi Valkeinen 已提交
36 37
#include "dss.h"

A
Archit Taneja 已提交
38
struct dpi_data {
39
	struct platform_device *pdev;
40
	enum dss_model dss_model;
41
	struct dss_device *dss;
42

43
	struct regulator *vdds_dsi_reg;
44
	enum dss_clk_source clk_src;
45
	struct dss_pll *pll;
46

47 48
	struct mutex lock;

49
	struct videomode vm;
50
	struct dss_lcd_mgr_config mgr_config;
51
	int data_lines;
52

53
	struct omap_dss_device output;
A
Archit Taneja 已提交
54 55
};

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

61 62
static enum dss_clk_source dpi_get_clk_src_dra7xx(struct dpi_data *dpi,
						  enum omap_channel channel)
63 64 65 66 67 68 69 70 71 72 73
{
	/*
	 * 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:
	{
74
		if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_1))
75 76 77 78 79
			return DSS_CLK_SRC_PLL1_1;
		break;
	}
	case OMAP_DSS_CHANNEL_LCD2:
	{
80
		if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
81
			return DSS_CLK_SRC_PLL1_3;
82
		if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_3))
83 84 85 86 87
			return DSS_CLK_SRC_PLL2_3;
		break;
	}
	case OMAP_DSS_CHANNEL_LCD3:
	{
88
		if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL2_1))
89
			return DSS_CLK_SRC_PLL2_1;
90
		if (dss_pll_find_by_src(dpi->dss, DSS_CLK_SRC_PLL1_3))
91 92 93 94 95 96 97 98 99 100
			return DSS_CLK_SRC_PLL1_3;
		break;
	}
	default:
		break;
	}

	return DSS_CLK_SRC_FCK;
}

101
static enum dss_clk_source dpi_get_clk_src(struct dpi_data *dpi)
102
{
103 104
	enum omap_channel channel = dpi->output.dispc_channel;

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

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

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

136
	case DSS_MODEL_DRA7:
137
		return dpi_get_clk_src_dra7xx(dpi, channel);
T
Tomi Valkeinen 已提交
138

139
	default:
140
		return DSS_CLK_SRC_FCK;
141
	}
142 143
}

144
struct dpi_clk_calc_ctx {
145
	struct dss_pll *pll;
146
	unsigned int clkout_idx;
147 148 149 150 151 152 153

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

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


186
static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
187 188 189 190
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

191 192
	ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
	ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
193 194 195 196 197 198

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


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

205 206 207 208
	ctx->pll_cinfo.n = n;
	ctx->pll_cinfo.m = m;
	ctx->pll_cinfo.fint = fint;
	ctx->pll_cinfo.clkdco = clkdco;
209

210
	return dss_pll_hsdiv_calc_a(ctx->pll, clkdco,
211
		ctx->pck_min, dss_get_max_fck_rate(ctx->pll->dss),
212
		dpi_calc_hsdiv_cb, ctx);
213 214
}

215
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
216 217 218
{
	struct dpi_clk_calc_ctx *ctx = data;

219
	ctx->fck = fck;
220 221 222 223 224

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

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

	memset(ctx, 0, sizeof(*ctx));
231
	ctx->pll = dpi->pll;
232
	ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
233

234
	clkin = clk_get_rate(dpi->pll->clkin);
235

236 237
	if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
		unsigned long pll_min, pll_max;
238

239 240 241 242 243 244 245 246 247 248
		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 */
249
		dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
250 251 252

		ctx->dispc_cinfo.lck_div = 1;
		ctx->dispc_cinfo.pck_div = 1;
253
		ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
254 255 256 257
		ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;

		return true;
	}
258 259
}

260 261
static bool dpi_dss_clk_calc(struct dpi_data *dpi, unsigned long pck,
			     struct dpi_clk_calc_ctx *ctx)
262 263 264 265 266 267 268
{
	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
269
	 * +/- ~15MHz.
270 271
	 */

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

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

	return false;
}



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

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

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

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

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

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

	return 0;
}
319

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

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

331
	r = dss_set_fck_rate(dpi->dss, ctx.fck);
T
Tomi Valkeinen 已提交
332 333 334
	if (r)
		return r;

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

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

	return 0;
}

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

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

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

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

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

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

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

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

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
383

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

T
Tomi Valkeinen 已提交
443 444
	return 0;

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

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

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

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

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

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

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

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

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

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

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

492
	dpi->vm = *vm;
493

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
}

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

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

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

563
	dss_pll_disable(pll);
564 565 566 567

	return 0;
}

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

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

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

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

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

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

	return 0;
}

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

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

607
	dpi->clk_src = dpi_get_clk_src(dpi);
608

609
	pll = dss_pll_find_by_src(dpi->dss, dpi->clk_src);
610
	if (!pll)
611 612
		return;

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

618
	dpi->pll = pll;
619 620
}

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

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

645
	case DSS_MODEL_OMAP4:
646 647
		return OMAP_DSS_CHANNEL_LCD2;

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

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

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

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

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

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

	return 0;
}

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

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

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

	omapdss_output_unset_device(dssdev);

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

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

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

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

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

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

	omapdss_register_output(out);
}

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

	omapdss_unregister_output(out);
}

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

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

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

	of_node_put(ep);

A
Archit Taneja 已提交
781
	dpi->pdev = pdev;
782
	dpi->dss_model = dss_model;
783
	dpi->dss = dss;
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 790 791 792 793 794 795 796 797

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

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

802
	if (!dpi)
T
Tomi Valkeinen 已提交
803 804
		return;

805
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
806
}