dpi.c 11.8 KB
Newer Older
Tomi Valkeinen's avatar
Tomi Valkeinen committed
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>
Tomi Valkeinen's avatar
Tomi Valkeinen committed
29
#include <linux/errno.h>
30
31
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
32
#include <linux/string.h>
Tomi Valkeinen's avatar
Tomi Valkeinen committed
33

34
#include <video/omapdss.h>
Tomi Valkeinen's avatar
Tomi Valkeinen committed
35
36

#include "dss.h"
37
#include "dss_features.h"
Tomi Valkeinen's avatar
Tomi Valkeinen committed
38
39

static struct {
40
	struct regulator *vdds_dsi_reg;
41
	struct platform_device *dsidev;
42

43
44
	struct mutex lock;

45
	struct omap_video_timings timings;
46
	struct dss_lcd_mgr_config mgr_config;
47
	int data_lines;
48
49

	struct omap_dss_output output;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
50
51
} dpi;

52
static struct platform_device *dpi_get_dsidev(enum omap_channel channel)
53
{
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	/*
	 * 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:
		return NULL;
	default:
		break;
	}

70
71
72
73
74
75
76
77
	switch (channel) {
	case OMAP_DSS_CHANNEL_LCD:
		return dsi_get_dsidev_from_id(0);
	case OMAP_DSS_CHANNEL_LCD2:
		return dsi_get_dsidev_from_id(1);
	default:
		return NULL;
	}
78
79
}

80
static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel)
81
{
82
83
84
85
86
87
88
89
90
91
	switch (channel) {
	case OMAP_DSS_CHANNEL_LCD:
		return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC;
	case OMAP_DSS_CHANNEL_LCD2:
		return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC;
	default:
		/* this shouldn't happen */
		WARN_ON(1);
		return OMAP_DSS_CLK_SRC_FCK;
	}
92
93
}

94
static int dpi_set_dsi_clk(struct omap_dss_device *dssdev,
95
96
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
97
{
98
	struct omap_overlay_manager *mgr = dssdev->output->manager;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
99
100
101
102
	struct dsi_clock_info dsi_cinfo;
	struct dispc_clock_info dispc_cinfo;
	int r;

103
104
	r = dsi_pll_calc_clock_div_pck(dpi.dsidev, pck_req, &dsi_cinfo,
			&dispc_cinfo);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
105
106
107
	if (r)
		return r;

108
	r = dsi_pll_set_clock_div(dpi.dsidev, &dsi_cinfo);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
109
110
111
	if (r)
		return r;

112
	dss_select_lcd_clk_source(mgr->id,
113
			dpi_get_alt_clk_src(mgr->id));
Tomi Valkeinen's avatar
Tomi Valkeinen committed
114

115
	dpi.mgr_config.clock_info = dispc_cinfo;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
116

117
	*fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
118
119
120
121
122
	*lck_div = dispc_cinfo.lck_div;
	*pck_div = dispc_cinfo.pck_div;

	return 0;
}
123

124
static int dpi_set_dispc_clk(struct omap_dss_device *dssdev,
125
126
		unsigned long pck_req, unsigned long *fck, int *lck_div,
		int *pck_div)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
127
128
129
130
131
{
	struct dss_clock_info dss_cinfo;
	struct dispc_clock_info dispc_cinfo;
	int r;

132
	r = dss_calc_clock_div(pck_req, &dss_cinfo, &dispc_cinfo);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
133
134
135
136
137
138
139
	if (r)
		return r;

	r = dss_set_clock_div(&dss_cinfo);
	if (r)
		return r;

140
	dpi.mgr_config.clock_info = dispc_cinfo;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
141
142
143
144
145
146
147
148
149
150

	*fck = dss_cinfo.fck;
	*lck_div = dispc_cinfo.lck_div;
	*pck_div = dispc_cinfo.pck_div;

	return 0;
}

static int dpi_set_mode(struct omap_dss_device *dssdev)
{
151
	struct omap_video_timings *t = &dpi.timings;
152
	struct omap_overlay_manager *mgr = dssdev->output->manager;
153
154
	int lck_div = 0, pck_div = 0;
	unsigned long fck = 0;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
155
156
157
	unsigned long pck;
	int r = 0;

158
	if (dpi.dsidev)
159
160
		r = dpi_set_dsi_clk(dssdev, t->pixel_clock * 1000, &fck,
				&lck_div, &pck_div);
161
	else
162
163
		r = dpi_set_dispc_clk(dssdev, t->pixel_clock * 1000, &fck,
				&lck_div, &pck_div);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
164
	if (r)
165
		return r;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
166
167
168
169
170
171
172
173
174
175
176

	pck = fck / lck_div / pck_div / 1000;

	if (pck != t->pixel_clock) {
		DSSWARN("Could not find exact pixel clock. "
				"Requested %d kHz, got %lu kHz\n",
				t->pixel_clock, pck);

		t->pixel_clock = pck;
	}

177
	dss_mgr_set_timings(mgr, t);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
178

179
	return 0;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
180
181
}

182
static void dpi_config_lcd_manager(struct omap_dss_device *dssdev)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
183
{
184
185
	struct omap_overlay_manager *mgr = dssdev->output->manager;

186
	dpi.mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
187

188
189
190
	dpi.mgr_config.stallmode = false;
	dpi.mgr_config.fifohandcheck = false;

191
	dpi.mgr_config.video_port_width = dpi.data_lines;
192
193
194

	dpi.mgr_config.lcden_sig_polarity = 0;

195
	dss_mgr_set_lcd_config(mgr, &dpi.mgr_config);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
196
197
}

198
int omapdss_dpi_display_enable(struct omap_dss_device *dssdev)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
199
{
200
	struct omap_dss_output *out = dssdev->output;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
201
202
	int r;

203
204
	mutex_lock(&dpi.lock);

205
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi.vdds_dsi_reg) {
206
		DSSERR("no VDSS_DSI regulator\n");
207
208
		r = -ENODEV;
		goto err_no_reg;
209
210
	}

211
212
	if (out == NULL || out->manager == NULL) {
		DSSERR("failed to enable display: no output/manager\n");
213
		r = -ENODEV;
214
		goto err_no_out_mgr;
215
216
	}

Tomi Valkeinen's avatar
Tomi Valkeinen committed
217
218
219
	r = omap_dss_start_device(dssdev);
	if (r) {
		DSSERR("failed to start device\n");
220
		goto err_start_dev;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
221
222
	}

223
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) {
224
225
		r = regulator_enable(dpi.vdds_dsi_reg);
		if (r)
226
			goto err_reg_enable;
227
228
	}

229
	r = dispc_runtime_get();
Tomi Valkeinen's avatar
Tomi Valkeinen committed
230
	if (r)
231
232
		goto err_get_dispc;

233
234
235
236
	r = dss_dpi_select_source(dssdev->channel);
	if (r)
		goto err_src_sel;

237
	if (dpi.dsidev) {
238
239
240
241
		r = dsi_runtime_get(dpi.dsidev);
		if (r)
			goto err_get_dsi;

242
		r = dsi_pll_init(dpi.dsidev, 0, 1);
243
		if (r)
244
			goto err_dsi_pll_init;
245
246
	}

Tomi Valkeinen's avatar
Tomi Valkeinen committed
247
248
	r = dpi_set_mode(dssdev);
	if (r)
249
		goto err_set_mode;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
250

251
252
	dpi_config_lcd_manager(dssdev);

Tomi Valkeinen's avatar
Tomi Valkeinen committed
253
254
	mdelay(2);

255
	r = dss_mgr_enable(out->manager);
256
257
	if (r)
		goto err_mgr_enable;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
258

259
260
	mutex_unlock(&dpi.lock);

Tomi Valkeinen's avatar
Tomi Valkeinen committed
261
262
	return 0;

263
err_mgr_enable:
264
err_set_mode:
265
	if (dpi.dsidev)
266
		dsi_pll_uninit(dpi.dsidev, true);
267
err_dsi_pll_init:
268
	if (dpi.dsidev)
269
270
		dsi_runtime_put(dpi.dsidev);
err_get_dsi:
271
err_src_sel:
272
273
	dispc_runtime_put();
err_get_dispc:
274
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
275
		regulator_disable(dpi.vdds_dsi_reg);
276
err_reg_enable:
Tomi Valkeinen's avatar
Tomi Valkeinen committed
277
	omap_dss_stop_device(dssdev);
278
err_start_dev:
279
err_no_out_mgr:
280
281
err_no_reg:
	mutex_unlock(&dpi.lock);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
282
283
	return r;
}
284
EXPORT_SYMBOL(omapdss_dpi_display_enable);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
285

286
void omapdss_dpi_display_disable(struct omap_dss_device *dssdev)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
287
{
288
289
	struct omap_overlay_manager *mgr = dssdev->output->manager;

290
291
	mutex_lock(&dpi.lock);

292
	dss_mgr_disable(mgr);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
293

294
	if (dpi.dsidev) {
295
		dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
296
		dsi_pll_uninit(dpi.dsidev, true);
297
		dsi_runtime_put(dpi.dsidev);
298
	}
Tomi Valkeinen's avatar
Tomi Valkeinen committed
299

300
	dispc_runtime_put();
Tomi Valkeinen's avatar
Tomi Valkeinen committed
301

302
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI))
303
304
		regulator_disable(dpi.vdds_dsi_reg);

Tomi Valkeinen's avatar
Tomi Valkeinen committed
305
	omap_dss_stop_device(dssdev);
306
307

	mutex_unlock(&dpi.lock);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
308
}
309
EXPORT_SYMBOL(omapdss_dpi_display_disable);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
310

311
312
void omapdss_dpi_set_timings(struct omap_dss_device *dssdev,
		struct omap_video_timings *timings)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
313
314
{
	DSSDBG("dpi_set_timings\n");
315
316
317

	mutex_lock(&dpi.lock);

318
319
	dpi.timings = *timings;

320
	mutex_unlock(&dpi.lock);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
321
}
322
EXPORT_SYMBOL(omapdss_dpi_set_timings);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
323

324
int dpi_check_timings(struct omap_dss_device *dssdev,
Tomi Valkeinen's avatar
Tomi Valkeinen committed
325
326
327
			struct omap_video_timings *timings)
{
	int r;
328
	struct omap_overlay_manager *mgr = dssdev->output->manager;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
329
330
331
	int lck_div, pck_div;
	unsigned long fck;
	unsigned long pck;
332
	struct dispc_clock_info dispc_cinfo;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
333

334
	if (mgr && !dispc_mgr_timings_ok(mgr->id, timings))
Tomi Valkeinen's avatar
Tomi Valkeinen committed
335
336
337
338
339
		return -EINVAL;

	if (timings->pixel_clock == 0)
		return -EINVAL;

340
	if (dpi.dsidev) {
Tomi Valkeinen's avatar
Tomi Valkeinen committed
341
		struct dsi_clock_info dsi_cinfo;
342
		r = dsi_pll_calc_clock_div_pck(dpi.dsidev,
Tomi Valkeinen's avatar
Tomi Valkeinen committed
343
344
345
346
347
348
				timings->pixel_clock * 1000,
				&dsi_cinfo, &dispc_cinfo);

		if (r)
			return r;

349
		fck = dsi_cinfo.dsi_pll_hsdiv_dispc_clk;
350
	} else {
Tomi Valkeinen's avatar
Tomi Valkeinen committed
351
		struct dss_clock_info dss_cinfo;
352
		r = dss_calc_clock_div(timings->pixel_clock * 1000,
Tomi Valkeinen's avatar
Tomi Valkeinen committed
353
354
355
356
357
358
359
				&dss_cinfo, &dispc_cinfo);

		if (r)
			return r;

		fck = dss_cinfo.fck;
	}
360
361
362

	lck_div = dispc_cinfo.lck_div;
	pck_div = dispc_cinfo.pck_div;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
363
364
365
366
367
368
369

	pck = fck / lck_div / pck_div / 1000;

	timings->pixel_clock = pck;

	return 0;
}
370
EXPORT_SYMBOL(dpi_check_timings);
Tomi Valkeinen's avatar
Tomi Valkeinen committed
371

372
373
374
375
376
377
378
379
380
381
void omapdss_dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines)
{
	mutex_lock(&dpi.lock);

	dpi.data_lines = data_lines;

	mutex_unlock(&dpi.lock);
}
EXPORT_SYMBOL(omapdss_dpi_set_data_lines);

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
static int __init dpi_verify_dsi_pll(struct platform_device *dsidev)
{
	int r;

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

	r = dsi_runtime_get(dsidev);
	if (r)
		return r;

	r = dsi_pll_init(dsidev, 0, 1);
	if (r) {
		dsi_runtime_put(dsidev);
		return r;
	}

	dsi_pll_uninit(dsidev, true);
	dsi_runtime_put(dsidev);

	return 0;
}

404
static int __init dpi_init_display(struct omap_dss_device *dssdev)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
405
{
406
407
	struct platform_device *dsidev;

Tomi Valkeinen's avatar
Tomi Valkeinen committed
408
409
	DSSDBG("init_display\n");

410
411
	if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) &&
					dpi.vdds_dsi_reg == NULL) {
412
		struct regulator *vdds_dsi;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
413

414
415
416
		vdds_dsi = dss_get_vdds_dsi();

		if (IS_ERR(vdds_dsi)) {
417
			DSSERR("can't get VDDS_DSI regulator\n");
418
			return PTR_ERR(vdds_dsi);
419
		}
420
421

		dpi.vdds_dsi_reg = vdds_dsi;
422
423
	}

424
425
426
427
428
429
	/*
	 * XXX We shouldn't need dssdev->channel for this. The dsi pll clock
	 * source for DPI is SoC integration detail, not something that should
	 * be configured in the dssdev
	 */
	dsidev = dpi_get_dsidev(dssdev->channel);
430

431
	if (dsidev && dpi_verify_dsi_pll(dsidev)) {
432
433
		dsidev = NULL;
		DSSWARN("DSI PLL not operational\n");
434
435
	}

436
437
438
439
440
	if (dsidev)
		DSSDBG("using DSI PLL for DPI clock\n");

	dpi.dsidev = dsidev;

Tomi Valkeinen's avatar
Tomi Valkeinen committed
441
442
443
	return 0;
}

444
static struct omap_dss_device * __init dpi_find_dssdev(struct platform_device *pdev)
445
{
446
	struct omap_dss_board_info *pdata = pdev->dev.platform_data;
447
	const char *def_disp_name = omapdss_get_default_display_name();
448
449
450
451
	struct omap_dss_device *def_dssdev;
	int i;

	def_dssdev = NULL;
452
453
454
455
456
457
458

	for (i = 0; i < pdata->num_devices; ++i) {
		struct omap_dss_device *dssdev = pdata->devices[i];

		if (dssdev->type != OMAP_DISPLAY_TYPE_DPI)
			continue;

459
460
461
462
463
464
465
		if (def_dssdev == NULL)
			def_dssdev = dssdev;

		if (def_disp_name != NULL &&
				strcmp(dssdev->name, def_disp_name) == 0) {
			def_dssdev = dssdev;
			break;
466
		}
467
	}
468

469
470
471
	return def_dssdev;
}

472
static void __init dpi_probe_pdata(struct platform_device *dpidev)
473
{
474
	struct omap_dss_device *plat_dssdev;
475
476
477
	struct omap_dss_device *dssdev;
	int r;

478
	plat_dssdev = dpi_find_dssdev(dpidev);
479

480
481
482
483
	if (!plat_dssdev)
		return;

	dssdev = dss_alloc_and_init_device(&dpidev->dev);
484
485
486
	if (!dssdev)
		return;

487
488
	dss_copy_device_pdata(dssdev, plat_dssdev);

489
490
491
	r = dpi_init_display(dssdev);
	if (r) {
		DSSERR("device %s init failed: %d\n", dssdev->name, r);
492
		dss_put_device(dssdev);
493
494
495
		return;
	}

496
497
498
499
500
501
502
503
	r = omapdss_output_set_device(&dpi.output, dssdev);
	if (r) {
		DSSERR("failed to connect output to new device: %s\n",
				dssdev->name);
		dss_put_device(dssdev);
		return;
	}

504
	r = dss_add_device(dssdev);
505
506
	if (r) {
		DSSERR("device %s register failed: %d\n", dssdev->name, r);
507
		omapdss_output_unset_device(&dpi.output);
508
		dss_put_device(dssdev);
509
		return;
510
	}
511
512
}

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
static void __init dpi_init_output(struct platform_device *pdev)
{
	struct omap_dss_output *out = &dpi.output;

	out->pdev = pdev;
	out->id = OMAP_DSS_OUTPUT_DPI;
	out->type = OMAP_DISPLAY_TYPE_DPI;

	dss_register_output(out);
}

static void __exit dpi_uninit_output(struct platform_device *pdev)
{
	struct omap_dss_output *out = &dpi.output;

	dss_unregister_output(out);
}

531
532
static int __init omap_dpi_probe(struct platform_device *pdev)
{
533
534
	mutex_init(&dpi.lock);

535
536
	dpi_init_output(pdev);

537
	dpi_probe_pdata(pdev);
538

539
540
541
	return 0;
}

Tomi Valkeinen's avatar
Tomi Valkeinen committed
542
static int __exit omap_dpi_remove(struct platform_device *pdev)
Tomi Valkeinen's avatar
Tomi Valkeinen committed
543
{
544
	dss_unregister_child_devices(&pdev->dev);
545

546
547
	dpi_uninit_output(pdev);

548
	return 0;
Tomi Valkeinen's avatar
Tomi Valkeinen committed
549
550
}

551
static struct platform_driver omap_dpi_driver = {
Tomi Valkeinen's avatar
Tomi Valkeinen committed
552
	.remove         = __exit_p(omap_dpi_remove),
553
554
555
556
557
558
	.driver         = {
		.name   = "omapdss_dpi",
		.owner  = THIS_MODULE,
	},
};

Tomi Valkeinen's avatar
Tomi Valkeinen committed
559
int __init dpi_init_platform_driver(void)
560
{
561
	return platform_driver_probe(&omap_dpi_driver, omap_dpi_probe);
562
563
}

Tomi Valkeinen's avatar
Tomi Valkeinen committed
564
void __exit dpi_uninit_platform_driver(void)
565
566
567
{
	platform_driver_unregister(&omap_dpi_driver);
}