aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/video/omap2/displays/panel-ltn.c
blob: 5794aa7ea769c89b14900ee9e3968b86f8a15303 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * Samsung LTN070NL01/LTN101AL03 LCD panel driver.
 *
 * Author: Donghwa Lee  <dh09.lee@samsung.com>
 *
 * Derived from drivers/video/omap/lcd-apollon.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <linux/wait.h>
#include <linux/fb.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/lcd.h>
#include <linux/backlight.h>
#include <linux/serial_core.h>
#include <linux/platform_data/panel-ltn.h>
#include <linux/platform_device.h>
#include <plat/hardware.h>
#include <video/omapdss.h>
#include <asm/mach-types.h>
#include <mach/omap4-common.h>

#include <plat/dmtimer.h>

struct ltn {
	struct device *dev;
	struct omap_dss_device *dssdev;
	struct ltn_panel_data *pdata;
	bool enabled;
	unsigned int current_brightness;
	unsigned int bl;
	struct mutex lock;
	struct backlight_device *bd;
	struct omap_dm_timer *gptimer;	/*For OMAP4430 "gptimer" */
};

static struct brightness_data ltn_brightness_data;

static void backlight_gptimer_start(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret;

	dev_dbg(&dssdev->dev, "%s\n", __func__);

	ret = omap_dm_timer_start(lcd->gptimer);
	if (ret)
		dev_err(&dssdev->dev, "failed to start pwm timer (%d)\n", ret);
}

static void backlight_gptimer_update(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);

	dev_dbg(&dssdev->dev, "%s\n", __func__);

	omap_dm_timer_set_load(lcd->gptimer, 1, -lcd->pdata->pwm_duty_max);
	omap_dm_timer_set_match(lcd->gptimer, 1,	/* 0~25 */
				-lcd->pdata->pwm_duty_max + lcd->current_brightness);
	omap_dm_timer_set_pwm(lcd->gptimer, 0, 1,
			      OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE);
	omap_dm_timer_enable(lcd->gptimer);
	omap_dm_timer_write_counter(lcd->gptimer, -2);
	omap_dm_timer_disable(lcd->gptimer);

	omap_dm_timer_start(lcd->gptimer);
}

static void backlight_gptimer_stop(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret;

	dev_dbg(&dssdev->dev, "%s\n", __func__);

	ret = omap_dm_timer_stop(lcd->gptimer);
	if (ret)
		dev_err(&dssdev->dev, "failed to stop pwm timer. ret=%d\n", ret);
}

static int backlight_gptimer_init(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret;

	dev_dbg(&dssdev->dev, "%s\n", __func__);

	lcd->gptimer =
	    omap_dm_timer_request_specific(lcd->pdata->backlight_gptimer_num);

	if (lcd->gptimer == NULL) {
		dev_err(&dssdev->dev, "failed to request pwm timer\n");
		ret = -ENODEV;
		goto err_dm_timer_request;
	}

	ret = omap_dm_timer_set_source(lcd->gptimer, OMAP_TIMER_SRC_SYS_CLK);
	if (ret < 0)
		goto err_dm_timer_src;

	return ret;

err_dm_timer_src:
	omap_dm_timer_free(lcd->gptimer);
	lcd->gptimer = NULL;
err_dm_timer_request:
	return ret;
}

static int ltn_hw_reset(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);

	dev_dbg(&dssdev->dev, "hw_reset\n");

	gpio_set_value(lcd->pdata->led_backlight_reset_gpio, 0);
	mdelay(1);
	gpio_set_value(lcd->pdata->led_backlight_reset_gpio, 1);
	usleep_range(10000, 11000);

	gpio_set_value(lcd->pdata->lvds_nshdn_gpio, 0);
	mdelay(1);
	gpio_set_value(lcd->pdata->lvds_nshdn_gpio, 1);
	msleep(200);

	return 0;
}

static int get_gamma_value_from_bl(struct omap_dss_device *dssdev, int bl)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int gamma_value = 0;
	int i;

	if (bl == ltn_brightness_data.platform_value[0])
		gamma_value = ltn_brightness_data.kernel_value[0];
	for (i = 1 ; i < NUM_BRIGHTNESS_LEVEL ; i++) {
		if (bl > ltn_brightness_data.platform_value[i])
			continue;
		else {
			gamma_value =
			 (bl - ltn_brightness_data.platform_value[i-1])
			 * ((lcd->pdata->pwm_duty_max
			 * ltn_brightness_data.kernel_value[i])
			 - (lcd->pdata->pwm_duty_max
			 * ltn_brightness_data.kernel_value[i-1]))
			 / (ltn_brightness_data.platform_value[i]
			 - ltn_brightness_data.platform_value[i-1])
			 + (lcd->pdata->pwm_duty_max
			 * ltn_brightness_data.kernel_value[i-1]);
			break;
		}
	}

	return gamma_value/100;
}

static void update_brightness(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int prev_brightness = lcd->current_brightness;

	lcd->current_brightness = lcd->bl;

	if (lcd->current_brightness == BRIGHTNESS_OFF &&
		prev_brightness != BRIGHTNESS_OFF) {
		backlight_gptimer_stop(dssdev);
	} else if (lcd->current_brightness != BRIGHTNESS_OFF) {
		if (prev_brightness == BRIGHTNESS_OFF) {
			backlight_gptimer_start(dssdev);
			usleep_range(2000, 2100);
		}
		backlight_gptimer_update(dssdev);
	}
}

static int ltn_power_on(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret = 0;

	dev_dbg(&dssdev->dev, "%s\n", __func__);

	if (lcd->enabled != 1) {
		if (lcd->pdata->set_power)
			lcd->pdata->set_power(true);

		ret = omapdss_dpi_display_enable(dssdev);
		if (ret) {
			dev_err(&dssdev->dev, "failed to enable DPI\n");
			goto err;
		}

		/* reset ltn bridge */
		ltn_hw_reset(dssdev);
		msleep(100);
		update_brightness(dssdev);

		lcd->enabled = 1;
	}

err:
	return ret;
}

static int ltn_power_off(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);

	dev_dbg(&dssdev->dev, "power_off\n");

	lcd->enabled = 0;

	if (lcd->current_brightness != BRIGHTNESS_OFF) {
		lcd->bl = BRIGHTNESS_OFF;
		update_brightness(dssdev);
		msleep(50);
	}

	gpio_set_value(lcd->pdata->lvds_nshdn_gpio, 0);

	omapdss_dpi_display_disable(dssdev);

	gpio_set_value(lcd->pdata->led_backlight_reset_gpio, 0);

	if (lcd->pdata->set_power)
		lcd->pdata->set_power(false);

	msleep(300);

	return 0;
}

static int ltn_get_brightness(struct backlight_device *bd)
{
	return bd->props.brightness;
}

static int ltn_set_brightness(struct backlight_device *bd)
{
	struct omap_dss_device *dssdev = dev_get_drvdata(&bd->dev);
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int bl = bd->props.brightness;

	if (bl < BRIGHTNESS_OFF)
		bl = BRIGHTNESS_OFF;
	else if (bl > BRIGHTNESS_MAX)
		bl = BRIGHTNESS_MAX;

	lcd->bl = get_gamma_value_from_bl(dssdev, bl);

	mutex_lock(&lcd->lock);
	if ((dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) &&
		(lcd->enabled) &&
		(lcd->current_brightness != lcd->bl)) {
		update_brightness(dssdev);
		dev_dbg(&bd->dev, "brightness=%d, bl=%d\n",
			bd->props.brightness, lcd->bl);
	}

	mutex_unlock(&lcd->lock);

	return 0;
}

static const struct backlight_ops ltn_backlight_ops = {
	.get_brightness = ltn_get_brightness,
	.update_status = ltn_set_brightness,
};

static int ltn_start(struct omap_dss_device *dssdev);
static void ltn_stop(struct omap_dss_device *dssdev);

static int ltn_panel_probe(struct omap_dss_device *dssdev)
{
	int ret = 0;
	struct ltn *lcd = NULL;

	struct backlight_properties props = {
		.brightness = BRIGHTNESS_DEFAULT,
		.max_brightness = 255,
		.type = BACKLIGHT_RAW,
	};

	dev_dbg(&dssdev->dev, "probing\n");

	lcd = kzalloc(sizeof(*lcd), GFP_KERNEL);
	if (!lcd)
		return -ENOMEM;

	if (dssdev->data == NULL) {
		dev_err(&dssdev->dev, "no platform data!\n");
		ret = -EINVAL;
		goto err_no_platform_data;
	}

	dssdev->panel.config = OMAP_DSS_LCD_TFT
			     | OMAP_DSS_LCD_IVS
			     /*| OMAP_DSS_LCD_IEO */
			     | OMAP_DSS_LCD_IPC
			     | OMAP_DSS_LCD_IHS
			     | OMAP_DSS_LCD_ONOFF;

	dssdev->panel.acb = 0;

	lcd->dssdev = dssdev;
	lcd->pdata = dssdev->data;

	ltn_brightness_data = lcd->pdata->brightness_table;

	ret = gpio_request(lcd->pdata->lvds_nshdn_gpio, "lvds_nshdn");
	if (ret < 0) {
		dev_err(&dssdev->dev, "gpio_request %d failed!\n",
			lcd->pdata->lvds_nshdn_gpio);
		goto err_no_platform_data;
	}
	gpio_direction_output(lcd->pdata->lvds_nshdn_gpio, 1);

	ret = gpio_request(lcd->pdata->led_backlight_reset_gpio,
		"led_backlight_reset");
	if (ret < 0) {
		dev_err(&dssdev->dev, "gpio_request %d failed!\n",
			lcd->pdata->led_backlight_reset_gpio);
		goto err_backlight_reset_gpio_request;
	}
	gpio_direction_output(lcd->pdata->led_backlight_reset_gpio, 1);

	mutex_init(&lcd->lock);

	dev_set_drvdata(&dssdev->dev, lcd);

	lcd->bl = get_gamma_value_from_bl(dssdev, props.brightness);

	/* Register DSI backlight control */
	lcd->bd = backlight_device_register("panel", &dssdev->dev, dssdev,
					    &ltn_backlight_ops, &props);
	if (IS_ERR(lcd->bd)) {
		ret = PTR_ERR(lcd->bd);
		goto err_backlight_device_register;
	}

	ret = backlight_gptimer_init(dssdev);
	if (ret < 0) {
		dev_err(&dssdev->dev,
			"backlight_gptimer_init failed!\n");
		goto err_gptimer_init;
	}

	update_brightness(dssdev);

	dev_dbg(&dssdev->dev, "probed\n");
	return ret;

err_gptimer_init:
	backlight_device_unregister(lcd->bd);
err_backlight_device_register:
	mutex_destroy(&lcd->lock);
	gpio_free(lcd->pdata->led_backlight_reset_gpio);
err_backlight_reset_gpio_request:
	gpio_free(lcd->pdata->lvds_nshdn_gpio);
err_no_platform_data:
	kfree(lcd);

	return ret;
}

static void ltn_panel_remove(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	backlight_device_unregister(lcd->bd);
	mutex_destroy(&lcd->lock);
	gpio_free(lcd->pdata->led_backlight_reset_gpio);
	gpio_free(lcd->pdata->lvds_nshdn_gpio);
	kfree(lcd);
}

static int ltn_start(struct omap_dss_device *dssdev)
{
	int r = 0;

	dev_dbg(&dssdev->dev, "start\n");

	r = ltn_power_on(dssdev);

	if (r) {
		dev_dbg(&dssdev->dev, "enable failed\n");
		dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
	} else {
		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
		dssdev->manager->enable(dssdev->manager);
	}

	return r;
}

static void ltn_stop(struct omap_dss_device *dssdev)
{
	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
		return;

	dev_dbg(&dssdev->dev, "stop\n");

	dssdev->manager->disable(dssdev->manager);

	ltn_power_off(dssdev);
}

static int ltn_panel_enable(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret;

	dev_dbg(&dssdev->dev, "enable\n");

	mutex_lock(&lcd->lock);
	if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
		ret = -EINVAL;
		goto out;
	}

	ret = ltn_start(dssdev);
out:
	mutex_unlock(&lcd->lock);
	return ret;
}

static void ltn_panel_disable(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);

	dev_dbg(&dssdev->dev, "disable\n");

	mutex_lock(&lcd->lock);
	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
		ltn_stop(dssdev);

	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
	mutex_unlock(&lcd->lock);
}

static int ltn_panel_suspend(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);

	dev_dbg(&dssdev->dev, "suspend\n");

	mutex_lock(&lcd->lock);

	ltn_stop(dssdev);
	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;

	mutex_unlock(&lcd->lock);
	return 0;
}

static int ltn_panel_resume(struct omap_dss_device *dssdev)
{
	struct ltn *lcd = dev_get_drvdata(&dssdev->dev);
	int ret;

	dev_dbg(&dssdev->dev, "resume\n");

	mutex_lock(&lcd->lock);
	if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
		ret = -EINVAL;
		goto out;
	}

	ret = ltn_start(dssdev);
out:
	mutex_unlock(&lcd->lock);
	return ret;
}

static void ltn_panel_get_resolution(struct omap_dss_device *dssdev,
					    u16 *xres, u16 *yres)
{
	*xres = dssdev->panel.timings.x_res;
	*yres = dssdev->panel.timings.y_res;
}

static void ltn_panel_set_timings(struct omap_dss_device *dssdev,
					 struct omap_video_timings *timings)
{
	dpi_set_timings(dssdev, timings);
}

static void ltn_panel_get_timings(struct omap_dss_device *dssdev,
					 struct omap_video_timings *timings)
{
	*timings = dssdev->panel.timings;
}

static int ltn_panel_check_timings(struct omap_dss_device *dssdev,
					  struct omap_video_timings *timings)
{
	return dpi_check_timings(dssdev, timings);
}

static struct omap_dss_driver ltn_omap_dss_driver = {
	.probe		= ltn_panel_probe,
	.remove		= ltn_panel_remove,

	.enable		= ltn_panel_enable,
	.disable	= ltn_panel_disable,
	.get_resolution	= ltn_panel_get_resolution,
	.suspend	= ltn_panel_suspend,
	.resume		= ltn_panel_resume,

	.set_timings	= ltn_panel_set_timings,
	.get_timings	= ltn_panel_get_timings,
	.check_timings	= ltn_panel_check_timings,

	.driver = {
		.name	= "ltn_panel",
		.owner	= THIS_MODULE,
	},
};

static int __init ltn_init(void)
{
	omap_dss_register_driver(&ltn_omap_dss_driver);

	return 0;
}

static void __exit ltn_exit(void)
{
	omap_dss_unregister_driver(&ltn_omap_dss_driver);
}

module_init(ltn_init);
module_exit(ltn_exit);

MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
MODULE_DESCRIPTION("Samsung LTN070NL01/LTN101AL03 LCD Driver");
MODULE_LICENSE("GPL");