dpi.c 18.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 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 timings;
51
	struct dss_lcd_mgr_config mgr_config;
52
	int data_lines;
53

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

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

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

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

70
static 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 173 174 175 176 177
		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.
	 */
178
	if (m_dispc > 1 && m_dispc % 2 != 0 && ctx->pck_min >= 100000000)
179 180
		return false;

181 182
	ctx->pll_cinfo.mX[ctx->clkout_idx] = m_dispc;
	ctx->pll_cinfo.clkout[ctx->clkout_idx] = dispc;
183 184 185 186 187 188

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


189 190
static bool dpi_calc_pll_cb(int n, int m, unsigned long fint,
		unsigned long clkdco,
191 192 193 194
		void *data)
{
	struct dpi_clk_calc_ctx *ctx = data;

195 196 197 198
	ctx->pll_cinfo.n = n;
	ctx->pll_cinfo.m = m;
	ctx->pll_cinfo.fint = fint;
	ctx->pll_cinfo.clkdco = clkdco;
199

200
	return dss_pll_hsdiv_calc_a(ctx->pll, clkdco,
201 202
		ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK),
		dpi_calc_hsdiv_cb, ctx);
203 204
}

205
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
206 207 208
{
	struct dpi_clk_calc_ctx *ctx = data;

209
	ctx->fck = fck;
210 211 212 213 214

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

215
static bool dpi_pll_clk_calc(struct dpi_data *dpi, unsigned long pck,
A
Archit Taneja 已提交
216
		struct dpi_clk_calc_ctx *ctx)
217 218 219 220
{
	unsigned long clkin;

	memset(ctx, 0, sizeof(*ctx));
221
	ctx->pll = dpi->pll;
222
	ctx->clkout_idx = dss_pll_get_clkout_idx_for_src(dpi->clk_src);
223

224
	clkin = clk_get_rate(dpi->pll->clkin);
225

226 227
	if (dpi->pll->hw->type == DSS_PLL_TYPE_A) {
		unsigned long pll_min, pll_max;
228

229 230 231 232 233 234 235 236 237 238
		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 */
239
		dss_pll_calc_b(dpi->pll, clkin, pck, &ctx->pll_cinfo);
240 241 242

		ctx->dispc_cinfo.lck_div = 1;
		ctx->dispc_cinfo.pck_div = 1;
243
		ctx->dispc_cinfo.lck = ctx->pll_cinfo.clkout[0];
244 245 246 247
		ctx->dispc_cinfo.pck = ctx->dispc_cinfo.lck;

		return true;
	}
248 249 250 251 252 253 254 255 256 257
}

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;
}



281
static int dpi_set_pll_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

289
	ok = dpi_pll_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.pll_cinfo);
T
Tomi Valkeinen 已提交
294 295 296
	if (r)
		return r;

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

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

301
	*fck = ctx.pll_cinfo.clkout[ctx.clkout_idx];
302 303
	*lck_div = ctx.dispc_cinfo.lck_div;
	*pck_div = ctx.dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
304 305 306

	return 0;
}
307

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

	dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
371

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

T
Tomi Valkeinen 已提交
437 438
	return 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

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

569
	dss_pll_disable(pll);
570 571 572 573

	return 0;
}

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

	if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
		return 0;

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

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

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

	return 0;
}

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

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

603 604 605
	dpi->clk_src = dpi_get_clk_src(dpi->output.dispc_channel);

	pll = dss_pll_find_by_src(dpi->clk_src);
606
	if (!pll)
607 608
		return;

609 610
	if (dpi_verify_pll(pll)) {
		DSSWARN("PLL not operational\n");
611 612 613
		return;
	}

614
	dpi->pll = pll;
615 616
}

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

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

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

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

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

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

	return 0;
}

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

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

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

	omapdss_output_unset_device(dssdev);

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

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

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

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

714
	.set_data_lines = dpi_set_data_lines,
T
Tomi Valkeinen 已提交
715 716
};

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

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

730
	omapdss_register_output(out);
731 732
}

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

738
	omapdss_unregister_output(out);
739 740
}

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

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

	omapdss_register_output(out);
}

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

	omapdss_unregister_output(out);
}

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

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

	dpi->pdev = pdev;
795

A
Archit Taneja 已提交
796 797 798
	dev_set_drvdata(&pdev->dev, dpi);

	mutex_init(&dpi->lock);
799

800 801
	dpi_init_output(pdev);

802 803 804
	return 0;
}

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

809
	dpi_uninit_output(pdev);
T
Tomi Valkeinen 已提交
810 811 812 813 814 815
}

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

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

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

T
Tomi Valkeinen 已提交
837
int __init dpi_init_platform_driver(void)
838
{
839
	return platform_driver_register(&omap_dpi_driver);
840 841
}

842
void dpi_uninit_platform_driver(void)
843 844 845
{
	platform_driver_unregister(&omap_dpi_driver);
}
T
Tomi Valkeinen 已提交
846

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

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

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

	of_node_put(ep);

A
Archit Taneja 已提交
872
	dpi->pdev = pdev;
873
	port->data = dpi;
T
Tomi Valkeinen 已提交
874

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

877
	dpi_init_output_port(pdev, port);
T
Tomi Valkeinen 已提交
878

A
Archit Taneja 已提交
879
	dpi->port_initialized = true;
T
Tomi Valkeinen 已提交
880 881 882 883 884 885 886 887 888

	return 0;

err_datalines:
	of_node_put(ep);

	return r;
}

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

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

896
	dpi_uninit_output_port(port);
T
Tomi Valkeinen 已提交
897
}