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.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/wait.h>
26 #include <linux/delay.h>
27 #include <linux/gpio.h>
28 #include <linux/spi/spi.h>
29 #include <linux/lcd.h>
30 #include <linux/backlight.h>
32 #define SLEEPMSEC 0x1000
34 #define DEFMASK 0xFF00
35 #define COMMAND_ONLY 0xFE
36 #define DATA_ONLY 0xFF
38 #define MAX_GAMMA_LEVEL 5
39 #define GAMMA_TABLE_COUNT 21
41 #define MIN_BRIGHTNESS 0
42 #define MAX_BRIGHTNESS 255
43 #define DEFAULT_BRIGHTNESS 150
47 struct spi_device *spi;
49 struct lcd_device *ld;
50 struct backlight_device *bd;
51 struct lcd_platform_data *lcd_pd;
54 static const unsigned short seq_display_on[] = {
59 static const unsigned short seq_display_off[] = {
64 static const unsigned short seq_stand_by_on[] = {
70 static const unsigned short seq_stand_by_off[] = {
76 static const unsigned short seq_setting[] = {
128 /* gamma value: 2.2 */
129 static const unsigned int ams369fg06_22_250[] = {
130 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
131 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
132 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
135 static const unsigned int ams369fg06_22_200[] = {
136 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
137 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
138 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
141 static const unsigned int ams369fg06_22_150[] = {
142 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
143 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
144 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
147 static const unsigned int ams369fg06_22_100[] = {
148 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
149 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
150 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
153 static const unsigned int ams369fg06_22_50[] = {
154 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
155 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
156 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
159 struct ams369fg06_gamma {
160 unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
163 static struct ams369fg06_gamma gamma_table = {
164 .gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
165 .gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
166 .gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
167 .gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
168 .gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
171 static int ams369fg06_spi_write_byte(struct ams369fg06 *lcd, int addr, int data)
174 struct spi_message msg;
176 struct spi_transfer xfer = {
181 buf[0] = (addr << 8) | data;
183 spi_message_init(&msg);
184 spi_message_add_tail(&xfer, &msg);
186 return spi_sync(lcd->spi, &msg);
189 static int ams369fg06_spi_write(struct ams369fg06 *lcd, unsigned char address,
190 unsigned char command)
194 if (address != DATA_ONLY)
195 ret = ams369fg06_spi_write_byte(lcd, 0x70, address);
196 if (command != COMMAND_ONLY)
197 ret = ams369fg06_spi_write_byte(lcd, 0x72, command);
202 static int ams369fg06_panel_send_sequence(struct ams369fg06 *lcd,
203 const unsigned short *wbuf)
207 while ((wbuf[i] & DEFMASK) != ENDDEF) {
208 if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
209 ret = ams369fg06_spi_write(lcd, wbuf[i], wbuf[i+1]);
220 static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
221 const unsigned int *gamma)
226 for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
227 ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
228 ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
229 ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
231 dev_err(lcd->dev, "failed to set gamma table.\n");
240 static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
245 if ((brightness >= 0) && (brightness <= 50))
247 else if ((brightness > 50) && (brightness <= 100))
249 else if ((brightness > 100) && (brightness <= 150))
251 else if ((brightness > 150) && (brightness <= 200))
253 else if ((brightness > 200) && (brightness <= 255))
256 ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
261 static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
264 static const unsigned short *init_seq[] = {
269 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
270 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
278 static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
281 static const unsigned short *init_seq[] = {
286 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
287 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
295 static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
299 static const unsigned short *init_seq[] = {
304 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
305 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
313 static int ams369fg06_power_is_on(int power)
315 return ((power) <= FB_BLANK_NORMAL);
318 static int ams369fg06_power_on(struct ams369fg06 *lcd)
321 struct lcd_platform_data *pd = NULL;
322 struct backlight_device *bd = NULL;
326 dev_err(lcd->dev, "platform data is NULL.\n");
332 dev_err(lcd->dev, "backlight device is NULL.\n");
337 dev_err(lcd->dev, "power_on is NULL.\n");
340 pd->power_on(lcd->ld, 1);
341 mdelay(pd->power_on_delay);
345 dev_err(lcd->dev, "reset is NULL.\n");
349 mdelay(pd->reset_delay);
352 ret = ams369fg06_ldi_init(lcd);
354 dev_err(lcd->dev, "failed to initialize ldi.\n");
358 ret = ams369fg06_ldi_enable(lcd);
360 dev_err(lcd->dev, "failed to enable ldi.\n");
364 /* set brightness to current value after power on or resume. */
365 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
367 dev_err(lcd->dev, "lcd gamma setting failed.\n");
374 static int ams369fg06_power_off(struct ams369fg06 *lcd)
377 struct lcd_platform_data *pd = NULL;
381 dev_err(lcd->dev, "platform data is NULL\n");
385 ret = ams369fg06_ldi_disable(lcd);
387 dev_err(lcd->dev, "lcd setting failed.\n");
391 mdelay(pd->power_off_delay);
394 dev_err(lcd->dev, "power_on is NULL.\n");
397 pd->power_on(lcd->ld, 0);
402 static int ams369fg06_power(struct ams369fg06 *lcd, int power)
406 if (ams369fg06_power_is_on(power) &&
407 !ams369fg06_power_is_on(lcd->power))
408 ret = ams369fg06_power_on(lcd);
409 else if (!ams369fg06_power_is_on(power) &&
410 ams369fg06_power_is_on(lcd->power))
411 ret = ams369fg06_power_off(lcd);
419 static int ams369fg06_get_power(struct lcd_device *ld)
421 struct ams369fg06 *lcd = lcd_get_data(ld);
426 static int ams369fg06_set_power(struct lcd_device *ld, int power)
428 struct ams369fg06 *lcd = lcd_get_data(ld);
430 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
431 power != FB_BLANK_NORMAL) {
432 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
436 return ams369fg06_power(lcd, power);
439 static int ams369fg06_get_brightness(struct backlight_device *bd)
441 return bd->props.brightness;
444 static int ams369fg06_set_brightness(struct backlight_device *bd)
447 int brightness = bd->props.brightness;
448 struct ams369fg06 *lcd = dev_get_drvdata(&bd->dev);
450 if (brightness < MIN_BRIGHTNESS ||
451 brightness > bd->props.max_brightness) {
452 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
453 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
457 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
459 dev_err(&bd->dev, "lcd brightness setting failed.\n");
466 static struct lcd_ops ams369fg06_lcd_ops = {
467 .get_power = ams369fg06_get_power,
468 .set_power = ams369fg06_set_power,
471 static const struct backlight_ops ams369fg06_backlight_ops = {
472 .get_brightness = ams369fg06_get_brightness,
473 .update_status = ams369fg06_set_brightness,
476 static int __devinit ams369fg06_probe(struct spi_device *spi)
479 struct ams369fg06 *lcd = NULL;
480 struct lcd_device *ld = NULL;
481 struct backlight_device *bd = NULL;
482 struct backlight_properties props;
484 lcd = kzalloc(sizeof(struct ams369fg06), GFP_KERNEL);
488 /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
489 spi->bits_per_word = 16;
491 ret = spi_setup(spi);
493 dev_err(&spi->dev, "spi setup failed.\n");
498 lcd->dev = &spi->dev;
500 lcd->lcd_pd = spi->dev.platform_data;
502 dev_err(&spi->dev, "platform data is NULL\n");
506 ld = lcd_device_register("ams369fg06", &spi->dev, lcd,
507 &ams369fg06_lcd_ops);
515 memset(&props, 0, sizeof(struct backlight_properties));
516 props.type = BACKLIGHT_RAW;
517 props.max_brightness = MAX_BRIGHTNESS;
519 bd = backlight_device_register("ams369fg06-bl", &spi->dev, lcd,
520 &ams369fg06_backlight_ops, &props);
523 goto out_lcd_unregister;
526 bd->props.brightness = DEFAULT_BRIGHTNESS;
529 if (!lcd->lcd_pd->lcd_enabled) {
531 * if lcd panel was off from bootloader then
532 * current lcd status is powerdown and then
533 * it enables lcd panel.
535 lcd->power = FB_BLANK_POWERDOWN;
537 ams369fg06_power(lcd, FB_BLANK_UNBLANK);
539 lcd->power = FB_BLANK_UNBLANK;
541 dev_set_drvdata(&spi->dev, lcd);
543 dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
548 lcd_device_unregister(ld);
554 static int __devexit ams369fg06_remove(struct spi_device *spi)
556 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
558 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
559 backlight_device_unregister(lcd->bd);
560 lcd_device_unregister(lcd->ld);
566 #if defined(CONFIG_PM)
567 static unsigned int before_power;
569 static int ams369fg06_suspend(struct spi_device *spi, pm_message_t mesg)
572 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
574 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
576 before_power = lcd->power;
579 * when lcd panel is suspend, lcd panel becomes off
580 * regardless of status.
582 ret = ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
587 static int ams369fg06_resume(struct spi_device *spi)
590 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
593 * after suspended, if lcd panel status is FB_BLANK_UNBLANK
594 * (at that time, before_power is FB_BLANK_UNBLANK) then
595 * it changes that status to FB_BLANK_POWERDOWN to get lcd on.
597 if (before_power == FB_BLANK_UNBLANK)
598 lcd->power = FB_BLANK_POWERDOWN;
600 dev_dbg(&spi->dev, "before_power = %d\n", before_power);
602 ret = ams369fg06_power(lcd, before_power);
607 #define ams369fg06_suspend NULL
608 #define ams369fg06_resume NULL
611 static void ams369fg06_shutdown(struct spi_device *spi)
613 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
615 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
618 static struct spi_driver ams369fg06_driver = {
620 .name = "ams369fg06",
621 .bus = &spi_bus_type,
622 .owner = THIS_MODULE,
624 .probe = ams369fg06_probe,
625 .remove = __devexit_p(ams369fg06_remove),
626 .shutdown = ams369fg06_shutdown,
627 .suspend = ams369fg06_suspend,
628 .resume = ams369fg06_resume,
631 static int __init ams369fg06_init(void)
633 return spi_register_driver(&ams369fg06_driver);
636 static void __exit ams369fg06_exit(void)
638 spi_unregister_driver(&ams369fg06_driver);
641 module_init(ams369fg06_init);
642 module_exit(ams369fg06_exit);
644 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
645 MODULE_DESCRIPTION("ams369fg06 LCD Driver");
646 MODULE_LICENSE("GPL");