2 * ams369fg06 AMOLED LCD panel driver.
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Author: Jingoo Han <jg1.han@samsung.com>
7 * Derived from drivers/video/s6e63m0.c
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/backlight.h>
16 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/lcd.h>
20 #include <linux/module.h>
21 #include <linux/spi/spi.h>
22 #include <linux/wait.h>
24 #define SLEEPMSEC 0x1000
26 #define DEFMASK 0xFF00
27 #define COMMAND_ONLY 0xFE
28 #define DATA_ONLY 0xFF
30 #define MAX_GAMMA_LEVEL 5
31 #define GAMMA_TABLE_COUNT 21
33 #define MIN_BRIGHTNESS 0
34 #define MAX_BRIGHTNESS 255
35 #define DEFAULT_BRIGHTNESS 150
39 struct spi_device *spi;
41 struct lcd_device *ld;
42 struct backlight_device *bd;
43 struct lcd_platform_data *lcd_pd;
46 static const unsigned short seq_display_on[] = {
51 static const unsigned short seq_display_off[] = {
56 static const unsigned short seq_stand_by_on[] = {
62 static const unsigned short seq_stand_by_off[] = {
68 static const unsigned short seq_setting[] = {
120 /* gamma value: 2.2 */
121 static const unsigned int ams369fg06_22_250[] = {
122 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
123 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
124 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
127 static const unsigned int ams369fg06_22_200[] = {
128 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
129 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
130 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
133 static const unsigned int ams369fg06_22_150[] = {
134 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
135 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
136 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
139 static const unsigned int ams369fg06_22_100[] = {
140 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
141 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
142 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
145 static const unsigned int ams369fg06_22_50[] = {
146 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
147 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
148 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
151 struct ams369fg06_gamma {
152 unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
155 static struct ams369fg06_gamma gamma_table = {
156 .gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
157 .gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
158 .gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
159 .gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
160 .gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
163 static int ams369fg06_spi_write_byte(struct ams369fg06 *lcd, int addr, int data)
166 struct spi_message msg;
168 struct spi_transfer xfer = {
173 buf[0] = (addr << 8) | data;
175 spi_message_init(&msg);
176 spi_message_add_tail(&xfer, &msg);
178 return spi_sync(lcd->spi, &msg);
181 static int ams369fg06_spi_write(struct ams369fg06 *lcd, unsigned char address,
182 unsigned char command)
186 if (address != DATA_ONLY)
187 ret = ams369fg06_spi_write_byte(lcd, 0x70, address);
188 if (command != COMMAND_ONLY)
189 ret = ams369fg06_spi_write_byte(lcd, 0x72, command);
194 static int ams369fg06_panel_send_sequence(struct ams369fg06 *lcd,
195 const unsigned short *wbuf)
199 while ((wbuf[i] & DEFMASK) != ENDDEF) {
200 if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
201 ret = ams369fg06_spi_write(lcd, wbuf[i], wbuf[i+1]);
213 static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
214 const unsigned int *gamma)
219 for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
220 ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
221 ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
222 ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
224 dev_err(lcd->dev, "failed to set gamma table.\n");
233 static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
238 if ((brightness >= 0) && (brightness <= 50))
240 else if ((brightness > 50) && (brightness <= 100))
242 else if ((brightness > 100) && (brightness <= 150))
244 else if ((brightness > 150) && (brightness <= 200))
246 else if ((brightness > 200) && (brightness <= 255))
249 ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
254 static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
257 static const unsigned short *init_seq[] = {
262 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
263 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
271 static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
274 static const unsigned short *init_seq[] = {
279 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
280 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
288 static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
292 static const unsigned short *init_seq[] = {
297 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
298 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
306 static int ams369fg06_power_is_on(int power)
308 return power <= FB_BLANK_NORMAL;
311 static int ams369fg06_power_on(struct ams369fg06 *lcd)
314 struct lcd_platform_data *pd;
315 struct backlight_device *bd;
321 dev_err(lcd->dev, "power_on is NULL.\n");
324 pd->power_on(lcd->ld, 1);
325 msleep(pd->power_on_delay);
329 dev_err(lcd->dev, "reset is NULL.\n");
333 msleep(pd->reset_delay);
336 ret = ams369fg06_ldi_init(lcd);
338 dev_err(lcd->dev, "failed to initialize ldi.\n");
342 ret = ams369fg06_ldi_enable(lcd);
344 dev_err(lcd->dev, "failed to enable ldi.\n");
348 /* set brightness to current value after power on or resume. */
349 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
351 dev_err(lcd->dev, "lcd gamma setting failed.\n");
358 static int ams369fg06_power_off(struct ams369fg06 *lcd)
361 struct lcd_platform_data *pd;
365 ret = ams369fg06_ldi_disable(lcd);
367 dev_err(lcd->dev, "lcd setting failed.\n");
371 msleep(pd->power_off_delay);
373 pd->power_on(lcd->ld, 0);
378 static int ams369fg06_power(struct ams369fg06 *lcd, int power)
382 if (ams369fg06_power_is_on(power) &&
383 !ams369fg06_power_is_on(lcd->power))
384 ret = ams369fg06_power_on(lcd);
385 else if (!ams369fg06_power_is_on(power) &&
386 ams369fg06_power_is_on(lcd->power))
387 ret = ams369fg06_power_off(lcd);
395 static int ams369fg06_get_power(struct lcd_device *ld)
397 struct ams369fg06 *lcd = lcd_get_data(ld);
402 static int ams369fg06_set_power(struct lcd_device *ld, int power)
404 struct ams369fg06 *lcd = lcd_get_data(ld);
406 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
407 power != FB_BLANK_NORMAL) {
408 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
412 return ams369fg06_power(lcd, power);
415 static int ams369fg06_get_brightness(struct backlight_device *bd)
417 return bd->props.brightness;
420 static int ams369fg06_set_brightness(struct backlight_device *bd)
423 int brightness = bd->props.brightness;
424 struct ams369fg06 *lcd = dev_get_drvdata(&bd->dev);
426 if (brightness < MIN_BRIGHTNESS ||
427 brightness > bd->props.max_brightness) {
428 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
429 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
433 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
435 dev_err(&bd->dev, "lcd brightness setting failed.\n");
442 static struct lcd_ops ams369fg06_lcd_ops = {
443 .get_power = ams369fg06_get_power,
444 .set_power = ams369fg06_set_power,
447 static const struct backlight_ops ams369fg06_backlight_ops = {
448 .get_brightness = ams369fg06_get_brightness,
449 .update_status = ams369fg06_set_brightness,
452 static int ams369fg06_probe(struct spi_device *spi)
455 struct ams369fg06 *lcd = NULL;
456 struct lcd_device *ld = NULL;
457 struct backlight_device *bd = NULL;
458 struct backlight_properties props;
460 lcd = devm_kzalloc(&spi->dev, sizeof(struct ams369fg06), GFP_KERNEL);
464 /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
465 spi->bits_per_word = 16;
467 ret = spi_setup(spi);
469 dev_err(&spi->dev, "spi setup failed.\n");
474 lcd->dev = &spi->dev;
476 lcd->lcd_pd = spi->dev.platform_data;
478 dev_err(&spi->dev, "platform data is NULL\n");
482 ld = lcd_device_register("ams369fg06", &spi->dev, lcd,
483 &ams369fg06_lcd_ops);
489 memset(&props, 0, sizeof(struct backlight_properties));
490 props.type = BACKLIGHT_RAW;
491 props.max_brightness = MAX_BRIGHTNESS;
493 bd = backlight_device_register("ams369fg06-bl", &spi->dev, lcd,
494 &ams369fg06_backlight_ops, &props);
497 goto out_lcd_unregister;
500 bd->props.brightness = DEFAULT_BRIGHTNESS;
503 if (!lcd->lcd_pd->lcd_enabled) {
505 * if lcd panel was off from bootloader then
506 * current lcd status is powerdown and then
507 * it enables lcd panel.
509 lcd->power = FB_BLANK_POWERDOWN;
511 ams369fg06_power(lcd, FB_BLANK_UNBLANK);
513 lcd->power = FB_BLANK_UNBLANK;
516 dev_set_drvdata(&spi->dev, lcd);
518 dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
523 lcd_device_unregister(ld);
527 static int ams369fg06_remove(struct spi_device *spi)
529 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
531 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
532 backlight_device_unregister(lcd->bd);
533 lcd_device_unregister(lcd->ld);
538 #if defined(CONFIG_PM)
539 static int ams369fg06_suspend(struct spi_device *spi, pm_message_t mesg)
541 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
543 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
546 * when lcd panel is suspend, lcd panel becomes off
547 * regardless of status.
549 return ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
552 static int ams369fg06_resume(struct spi_device *spi)
554 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
556 lcd->power = FB_BLANK_POWERDOWN;
558 return ams369fg06_power(lcd, FB_BLANK_UNBLANK);
561 #define ams369fg06_suspend NULL
562 #define ams369fg06_resume NULL
565 static void ams369fg06_shutdown(struct spi_device *spi)
567 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
569 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
572 static struct spi_driver ams369fg06_driver = {
574 .name = "ams369fg06",
575 .owner = THIS_MODULE,
577 .probe = ams369fg06_probe,
578 .remove = ams369fg06_remove,
579 .shutdown = ams369fg06_shutdown,
580 .suspend = ams369fg06_suspend,
581 .resume = ams369fg06_resume,
584 module_spi_driver(ams369fg06_driver);
586 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
587 MODULE_DESCRIPTION("ams369fg06 LCD Driver");
588 MODULE_LICENSE("GPL");