dpi.c 18.2 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

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

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

48 49
	struct mutex lock;

50
	struct videomode vm;
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 enum dss_clk_source dpi_get_clk_src(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
		return DSS_CLK_SRC_FCK;
85

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

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

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

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

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

	/* inputs */

	unsigned long pck_min, pck_max;

	/* outputs */

136
	struct dss_pll_clock_info pll_cinfo;
T
Tomi Valkeinen 已提交
137
	unsigned long fck;
138 139 140 141 142 143 144 145 146 147 148 149 150
	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.
	 */
151
	if (ctx->pck_min >= 100000000) {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		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;
}


168
static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc,
169 170 171 172
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

173 174
	ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
	ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
175 176 177 178 179 180

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


181 182
static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
		unsigned long clkdco,
183 184 185 186
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

187 188 189 190
	ctx->pll_cinfo.n = n;
	ctx->pll_cinfo.m = m;
	ctx->pll_cinfo.fint = fint;
	ctx->pll_cinfo.clkdco = clkdco;
191

192
	return dss_pll_hsdiv_calc_a(ctx->pll, clkdco,
193 194
		ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK),
		dpi_calc_hsdiv_cb, ctx);
195 196
}

197
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
198 199 200
{
	struct dpi_clk_calc_ctx *ctx = data;

201
	ctx->fck = fck;
202 203 204 205 206

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

207
static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
A
Archit Taneja 已提交
208
		struct dpi_clk_calc_ctx *ctx)
209 210 211 212
{
	unsigned long clkin;

	memset(ctx, 0, sizeof(*ctx));
213
	ctx->pll = dpi->pll;
214
	ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
215

216
	clkin = clk_get_rate(dpi->pll->clkin);
217

218 219
	if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
		unsigned long pll_min, pll_max;
220

221 222 223 224 225 226 227 228 229 230
		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 */
231
		dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
232 233 234

		ctx->dispc_cinfo.lck_div = 1;
		ctx->dispc_cinfo.pck_div = 1;
235
		ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
236 237 238 239
		ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;

		return true;
	}
240 241 242 243 244 245 246 247 248 249
}

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
250
	 * +/- ~15MHz.
251 252
	 */

253
	for (i = 0; i < 25; ++i) {
254 255 256 257 258 259 260 261 262
		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;

263
		ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx);
264 265 266 267 268 269 270 271 272
		if (ok)
			return ok;
	}

	return false;
}



273
static int dpi_set_pll_clk(struct dpi_data *dpi, enum omap_channel channel,
274 275
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
T
Tomi Valkeinen 已提交
276
{
277
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
278
	int r;
279
	bool ok;
T
Tomi Valkeinen 已提交
280

281
	ok = dpi_pll_clk_calc(dpi, pck_req, &ctx);
282 283
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
284

285
	r = dss_pll_set_config(dpi->pll, &ctx.pll_cinfo);
T
Tomi Valkeinen 已提交
286 287 288
	if (r)
		return r;

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

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

293
	*fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
294 295
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
296 297 298

	return 0;
}
299

A
Archit Taneja 已提交
300 301
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 已提交
302
{
303
	struct dpi_clk_calc_ctx ctx;
T
Tomi Valkeinen 已提交
304
	int r;
305
	bool ok;
T
Tomi Valkeinen 已提交
306

307 308 309
	ok = dpi_dss_clk_calc(pck_req, &ctx);
	if (!ok)
		return -EINVAL;
T
Tomi Valkeinen 已提交
310

311
	r = dss_set_fck_rate(ctx.fck);
T
Tomi Valkeinen 已提交
312 313 314
	if (r)
		return r;

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

317
	*fck = ctx.fck;
318 319
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
320 321 322 323

	return 0;
}

A
Archit Taneja 已提交
324
static int dpi_set_mode(struct dpi_data *dpi)
T
Tomi Valkeinen 已提交
325
{
A
Archit Taneja 已提交
326
	struct omap_dss_device *out = &dpi->output;
327
	enum omap_channel channel = out->dispc_channel;
328
	struct videomode *vm = &dpi->vm;
329 330
	int lck_div = 0, pck_div = 0;
	unsigned long fck = 0;
T
Tomi Valkeinen 已提交
331 332 333
	unsigned long pck;
	int r = 0;

334
	if (dpi->pll)
335
		r = dpi_set_pll_clk(dpi, channel, vm->pixelclock, &fck,
336
				&lck_div, &pck_div);
337
	else
338
		r = dpi_set_dispc_clk(dpi, vm->pixelclock, &fck,
339
				&lck_div, &pck_div);
T
Tomi Valkeinen 已提交
340
	if (r)
341
		return r;
T
Tomi Valkeinen 已提交
342

343
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
344

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

349
		vm->pixelclock = pck;
T
Tomi Valkeinen 已提交
350 351
	}

352
	dss_mgr_set_timings(channel, vm);
T
Tomi Valkeinen 已提交
353

354
	return 0;
T
Tomi Valkeinen 已提交
355 356
}

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

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
363

A
Archit Taneja 已提交
364 365
	dpi->mgr_config.stallmode = false;
	dpi->mgr_config.fifohandcheck = false;
366

A
Archit Taneja 已提交
367
	dpi->mgr_config.video_port_width = dpi->data_lines;
368

A
Archit Taneja 已提交
369
	dpi->mgr_config.lcden_sig_polarity = 0;
370

371
	dss_mgr_set_lcd_config(channel, &dpi->mgr_config);
T
Tomi Valkeinen 已提交
372 373
}

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

A
Archit Taneja 已提交
381
	mutex_lock(&dpi->lock);
382

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

389
	if (!out->dispc_channel_connected) {
390
		DSSERR("failed to enable display: no output/manager\n");
391
		r = -ENODEV;
392
		goto err_no_out_mgr;
393 394
	}

395
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
A
Archit Taneja 已提交
396
		r = regulator_enable(dpi->vdds_dsi_reg);
397
		if (r)
398
			goto err_reg_enable;
399 400
	}

401
	r = dispc_runtime_get();
T
Tomi Valkeinen 已提交
402
	if (r)
403 404
		goto err_get_dispc;

405
	r = dss_dpi_select_source(out->port_num, channel);
406 407 408
	if (r)
		goto err_src_sel;

409 410
	if (dpi->pll) {
		r = dss_pll_enable(dpi->pll);
411
		if (r)
412
			goto err_pll_init;
413 414
	}

A
Archit Taneja 已提交
415
	r = dpi_set_mode(dpi);
T
Tomi Valkeinen 已提交
416
	if (r)
417
		goto err_set_mode;
T
Tomi Valkeinen 已提交
418

A
Archit Taneja 已提交
419
	dpi_config_lcd_manager(dpi);
420

T
Tomi Valkeinen 已提交
421 422
	mdelay(2);

423
	r = dss_mgr_enable(channel);
424 425
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
426

A
Archit Taneja 已提交
427
	mutex_unlock(&dpi->lock);
428

T
Tomi Valkeinen 已提交
429 430
	return 0;

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

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

A
Archit Taneja 已提交
453
	mutex_lock(&dpi->lock);
454

455
	dss_mgr_disable(channel);
T
Tomi Valkeinen 已提交
456

457
	if (dpi->pll) {
458
		dss_select_lcd_clk_source(channel, DSS_CLK_SRC_FCK);
459
		dss_pll_disable(dpi->pll);
460
	}
T
Tomi Valkeinen 已提交
461

462
	dispc_runtime_put();
T
Tomi Valkeinen 已提交
463

464
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
A
Archit Taneja 已提交
465
		regulator_disable(dpi->vdds_dsi_reg);
466

A
Archit Taneja 已提交
467
	mutex_unlock(&dpi->lock);
T
Tomi Valkeinen 已提交
468 469
}

470
static void dpi_set_timings(struct omap_dss_device *dssdev,
471
			    struct videomode *vm)
T
Tomi Valkeinen 已提交
472
{
A
Archit Taneja 已提交
473
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
A
Archit Taneja 已提交
474

T
Tomi Valkeinen 已提交
475
	DSSDBG("dpi_set_timings\n");
476

A
Archit Taneja 已提交
477
	mutex_lock(&dpi->lock);
478

479
	dpi->vm = *vm;
480

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

T
Tomi Valkeinen 已提交
484
static void dpi_get_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 489

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

491
	*vm = dpi->vm;
T
Tomi Valkeinen 已提交
492

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

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

507
	if (vm->hactive % 8 != 0)
508 509
		return -EINVAL;

510
	if (!dispc_mgr_timings_ok(channel, vm))
T
Tomi Valkeinen 已提交
511 512
		return -EINVAL;

513
	if (vm->pixelclock == 0)
T
Tomi Valkeinen 已提交
514 515
		return -EINVAL;

516
	if (dpi->pll) {
517
		ok = dpi_pll_clk_calc(dpi, vm->pixelclock, &ctx);
518 519
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
520

521
		fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
522
	} else {
523
		ok = dpi_dss_clk_calc(vm->pixelclock, &ctx);
524 525
		if (!ok)
			return -EINVAL;
T
Tomi Valkeinen 已提交
526

527
		fck = ctx.fck;
T
Tomi Valkeinen 已提交
528
	}
529

530 531
	lck_div = ctx.dispc_cinfo.lck_div;
	pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
532

533
	pck = fck / lck_div / pck_div;
T
Tomi Valkeinen 已提交
534

535
	vm->pixelclock = pck;
T
Tomi Valkeinen 已提交
536 537 538 539

	return 0;
}

540
static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
541
{
A
Archit Taneja 已提交
542
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
543

A
Archit Taneja 已提交
544
	mutex_lock(&dpi->lock);
545

A
Archit Taneja 已提交
546 547 548
	dpi->data_lines = data_lines;

	mutex_unlock(&dpi->lock);
549 550
}

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

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

557
	r = dss_pll_enable(pll);
558
	if (r)
559 560
		return r;

561
	dss_pll_disable(pll);
562 563 564 565

	return 0;
}

A
Archit Taneja 已提交
566
static int dpi_init_regulator(struct dpi_data *dpi)
567 568 569 570 571 572
{
	struct regulator *vdds_dsi;

	if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
		return 0;

A
Archit Taneja 已提交
573
	if (dpi->vdds_dsi_reg)
574 575
		return 0;

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

A
Archit Taneja 已提交
583
	dpi->vdds_dsi_reg = vdds_dsi;
584 585 586 587

	return 0;
}

A
Archit Taneja 已提交
588
static void dpi_init_pll(struct dpi_data *dpi)
589
{
590
	struct dss_pll *pll;
591

592
	if (dpi->pll)
593 594
		return;

595 596 597
	dpi->clk_src = dpi_get_clk_src(dpi->output.dispc_channel);

	pll = dss_pll_find_by_src(dpi->clk_src);
598
	if (!pll)
599 600
		return;

601 602
	if (dpi_verify_pll(pll)) {
		DSSWARN("PLL not operational\n");
603 604 605
		return;
	}

606
	dpi->pll = pll;
607 608
}

609 610 611 612 613 614
/*
 * 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.
 */
615
static enum omap_channel dpi_get_channel(int port_num)
616 617 618 619 620 621 622
{
	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:
623
	case OMAPDSS_VER_AM43xx:
624 625
		return OMAP_DSS_CHANNEL_LCD;

T
Tomi Valkeinen 已提交
626 627 628 629 630 631 632 633 634 635 636
	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;
		}

637 638 639 640 641 642 643 644 645 646 647 648 649 650
	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 已提交
651 652 653
static int dpi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
A
Archit Taneja 已提交
654
	struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev);
655
	enum omap_channel channel = dpi->output.dispc_channel;
T
Tomi Valkeinen 已提交
656 657
	int r;

A
Archit Taneja 已提交
658
	r = dpi_init_regulator(dpi);
T
Tomi Valkeinen 已提交
659 660 661
	if (r)
		return r;

A
Archit Taneja 已提交
662
	dpi_init_pll(dpi);
T
Tomi Valkeinen 已提交
663

664
	r = dss_mgr_connect(channel, dssdev);
T
Tomi Valkeinen 已提交
665 666 667 668 669 670 671
	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);
672
		dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
673 674 675 676 677 678 679 680 681
		return r;
	}

	return 0;
}

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

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

687
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
688 689 690 691
		return;

	omapdss_output_unset_device(dssdev);

692
	dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
693 694 695 696 697 698
}

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

699 700
	.enable = dpi_display_enable,
	.disable = dpi_display_disable,
T
Tomi Valkeinen 已提交
701 702

	.check_timings = dpi_check_timings,
703
	.set_timings = dpi_set_timings,
T
Tomi Valkeinen 已提交
704 705
	.get_timings = dpi_get_timings,

706
	.set_data_lines = dpi_set_data_lines,
T
Tomi Valkeinen 已提交
707 708
};

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

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

722
	omapdss_register_output(out);
723 724
}

725
static void dpi_uninit_output(struct platform_device *pdev)
726
{
A
Archit Taneja 已提交
727
	struct dpi_data *dpi = dpi_get_data_from_pdev(pdev);
A
Archit Taneja 已提交
728
	struct omap_dss_device *out = &dpi->output;
729

730
	omapdss_unregister_output(out);
731 732
}

733 734 735 736 737
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;
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	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;
	}
757 758 759 760

	out->dev = &pdev->dev;
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->output_type = OMAP_DISPLAY_TYPE_DPI;
761 762
	out->dispc_channel = dpi_get_channel(port_num);
	out->port_num = port_num;
763 764 765 766 767 768
	out->ops.dpi = &dpi_ops;
	out->owner = THIS_MODULE;

	omapdss_register_output(out);
}

769
static void dpi_uninit_output_port(struct device_node *port)
770 771 772 773 774 775 776
{
	struct dpi_data *dpi = port->data;
	struct omap_dss_device *out = &dpi->output;

	omapdss_unregister_output(out);
}

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

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

	dpi->pdev = pdev;
787

A
Archit Taneja 已提交
788 789 790
	dev_set_drvdata(&pdev->dev, dpi);

	mutex_init(&dpi->lock);
791

792 793
	dpi_init_output(pdev);

794 795 796
	return 0;
}

T
Tomi Valkeinen 已提交
797
static void dpi_unbind(struct device *dev, struct device *master, void *data)
T
Tomi Valkeinen 已提交
798
{
T
Tomi Valkeinen 已提交
799 800
	struct platform_device *pdev = to_platform_device(dev);

801
	dpi_uninit_output(pdev);
T
Tomi Valkeinen 已提交
802 803 804 805 806 807
}

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

T
Tomi Valkeinen 已提交
809 810 811 812 813 814 815 816
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);
817
	return 0;
T
Tomi Valkeinen 已提交
818 819
}

820
static struct platform_driver omap_dpi_driver = {
T
Tomi Valkeinen 已提交
821 822
	.probe		= dpi_probe,
	.remove		= dpi_remove,
823 824
	.driver         = {
		.name   = "omapdss_dpi",
T
Tomi Valkeinen 已提交
825
		.suppress_bind_attrs = true,
826 827 828
	},
};

T
Tomi Valkeinen 已提交
829
int __init dpi_init_platform_driver(void)
830
{
831
	return platform_driver_register(&omap_dpi_driver);
832 833
}

834
void dpi_uninit_platform_driver(void)
835 836 837
{
	platform_driver_unregister(&omap_dpi_driver);
}
T
Tomi Valkeinen 已提交
838

839
int dpi_init_port(struct platform_device *pdev, struct device_node *port)
T
Tomi Valkeinen 已提交
840
{
A
Archit Taneja 已提交
841
	struct dpi_data *dpi;
T
Tomi Valkeinen 已提交
842 843 844 845
	struct device_node *ep;
	u32 datalines;
	int r;

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

T
Tomi Valkeinen 已提交
850 851 852 853 854 855 856 857 858 859
	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 已提交
860
	dpi->data_lines = datalines;
T
Tomi Valkeinen 已提交
861 862 863

	of_node_put(ep);

A
Archit Taneja 已提交
864
	dpi->pdev = pdev;
865
	port->data = dpi;
T
Tomi Valkeinen 已提交
866

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

869
	dpi_init_output_port(pdev, port);
T
Tomi Valkeinen 已提交
870

A
Archit Taneja 已提交
871
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
872 873 874 875 876 877 878 879 880

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

881
void dpi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
882
{
883
	struct dpi_data *dpi = port->data;
A
Archit Taneja 已提交
884 885

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

888
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
889
}