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>
25 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/gpio.h>
29 #include <linux/spi/spi.h>
30 #include <linux/lcd.h>
31 #include <linux/backlight.h>
33 #define SLEEPMSEC 0x1000
35 #define DEFMASK 0xFF00
36 #define COMMAND_ONLY 0xFE
37 #define DATA_ONLY 0xFF
39 #define MAX_GAMMA_LEVEL 5
40 #define GAMMA_TABLE_COUNT 21
42 #define MIN_BRIGHTNESS 0
43 #define MAX_BRIGHTNESS 255
44 #define DEFAULT_BRIGHTNESS 150
48 struct spi_device *spi;
50 struct lcd_device *ld;
51 struct backlight_device *bd;
52 struct lcd_platform_data *lcd_pd;
55 static const unsigned short seq_display_on[] = {
60 static const unsigned short seq_display_off[] = {
65 static const unsigned short seq_stand_by_on[] = {
71 static const unsigned short seq_stand_by_off[] = {
77 static const unsigned short seq_setting[] = {
129 /* gamma value: 2.2 */
130 static const unsigned int ams369fg06_22_250[] = {
131 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
132 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
133 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
136 static const unsigned int ams369fg06_22_200[] = {
137 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
138 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
139 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
142 static const unsigned int ams369fg06_22_150[] = {
143 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
144 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
145 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
148 static const unsigned int ams369fg06_22_100[] = {
149 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
150 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
151 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
154 static const unsigned int ams369fg06_22_50[] = {
155 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
156 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
157 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
160 struct ams369fg06_gamma {
161 unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
164 static struct ams369fg06_gamma gamma_table = {
165 .gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
166 .gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
167 .gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
168 .gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
169 .gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
172 static int ams369fg06_spi_write_byte(struct ams369fg06 *lcd, int addr, int data)
175 struct spi_message msg;
177 struct spi_transfer xfer = {
182 buf[0] = (addr << 8) | data;
184 spi_message_init(&msg);
185 spi_message_add_tail(&xfer, &msg);
187 return spi_sync(lcd->spi, &msg);
190 static int ams369fg06_spi_write(struct ams369fg06 *lcd, unsigned char address,
191 unsigned char command)
195 if (address != DATA_ONLY)
196 ret = ams369fg06_spi_write_byte(lcd, 0x70, address);
197 if (command != COMMAND_ONLY)
198 ret = ams369fg06_spi_write_byte(lcd, 0x72, command);
203 static int ams369fg06_panel_send_sequence(struct ams369fg06 *lcd,
204 const unsigned short *wbuf)
208 while ((wbuf[i] & DEFMASK) != ENDDEF) {
209 if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
210 ret = ams369fg06_spi_write(lcd, wbuf[i], wbuf[i+1]);
222 static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
223 const unsigned int *gamma)
228 for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
229 ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
230 ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
231 ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
233 dev_err(lcd->dev, "failed to set gamma table.\n");
242 static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
247 if ((brightness >= 0) && (brightness <= 50))
249 else if ((brightness > 50) && (brightness <= 100))
251 else if ((brightness > 100) && (brightness <= 150))
253 else if ((brightness > 150) && (brightness <= 200))
255 else if ((brightness > 200) && (brightness <= 255))
258 ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
263 static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
266 static const unsigned short *init_seq[] = {
271 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
272 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
280 static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
283 static const unsigned short *init_seq[] = {
288 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
289 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
297 static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
301 static const unsigned short *init_seq[] = {
306 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
307 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
315 static int ams369fg06_power_is_on(int power)
317 return power <= FB_BLANK_NORMAL;
320 static int ams369fg06_power_on(struct ams369fg06 *lcd)
323 struct lcd_platform_data *pd;
324 struct backlight_device *bd;
330 dev_err(lcd->dev, "power_on is NULL.\n");
333 pd->power_on(lcd->ld, 1);
334 msleep(pd->power_on_delay);
338 dev_err(lcd->dev, "reset is NULL.\n");
342 msleep(pd->reset_delay);
345 ret = ams369fg06_ldi_init(lcd);
347 dev_err(lcd->dev, "failed to initialize ldi.\n");
351 ret = ams369fg06_ldi_enable(lcd);
353 dev_err(lcd->dev, "failed to enable ldi.\n");
357 /* set brightness to current value after power on or resume. */
358 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
360 dev_err(lcd->dev, "lcd gamma setting failed.\n");
367 static int ams369fg06_power_off(struct ams369fg06 *lcd)
370 struct lcd_platform_data *pd;
374 ret = ams369fg06_ldi_disable(lcd);
376 dev_err(lcd->dev, "lcd setting failed.\n");
380 msleep(pd->power_off_delay);
382 pd->power_on(lcd->ld, 0);
387 static int ams369fg06_power(struct ams369fg06 *lcd, int power)
391 if (ams369fg06_power_is_on(power) &&
392 !ams369fg06_power_is_on(lcd->power))
393 ret = ams369fg06_power_on(lcd);
394 else if (!ams369fg06_power_is_on(power) &&
395 ams369fg06_power_is_on(lcd->power))
396 ret = ams369fg06_power_off(lcd);
404 static int ams369fg06_get_power(struct lcd_device *ld)
406 struct ams369fg06 *lcd = lcd_get_data(ld);
411 static int ams369fg06_set_power(struct lcd_device *ld, int power)
413 struct ams369fg06 *lcd = lcd_get_data(ld);
415 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
416 power != FB_BLANK_NORMAL) {
417 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
421 return ams369fg06_power(lcd, power);
424 static int ams369fg06_get_brightness(struct backlight_device *bd)
426 return bd->props.brightness;
429 static int ams369fg06_set_brightness(struct backlight_device *bd)
432 int brightness = bd->props.brightness;
433 struct ams369fg06 *lcd = dev_get_drvdata(&bd->dev);
435 if (brightness < MIN_BRIGHTNESS ||
436 brightness > bd->props.max_brightness) {
437 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
438 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
442 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
444 dev_err(&bd->dev, "lcd brightness setting failed.\n");
451 static struct lcd_ops ams369fg06_lcd_ops = {
452 .get_power = ams369fg06_get_power,
453 .set_power = ams369fg06_set_power,
456 static const struct backlight_ops ams369fg06_backlight_ops = {
457 .get_brightness = ams369fg06_get_brightness,
458 .update_status = ams369fg06_set_brightness,
461 static int ams369fg06_probe(struct spi_device *spi)
464 struct ams369fg06 *lcd = NULL;
465 struct lcd_device *ld = NULL;
466 struct backlight_device *bd = NULL;
467 struct backlight_properties props;
469 lcd = devm_kzalloc(&spi->dev, sizeof(struct ams369fg06), GFP_KERNEL);
473 /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
474 spi->bits_per_word = 16;
476 ret = spi_setup(spi);
478 dev_err(&spi->dev, "spi setup failed.\n");
483 lcd->dev = &spi->dev;
485 lcd->lcd_pd = spi->dev.platform_data;
487 dev_err(&spi->dev, "platform data is NULL\n");
491 ld = lcd_device_register("ams369fg06", &spi->dev, lcd,
492 &ams369fg06_lcd_ops);
498 memset(&props, 0, sizeof(struct backlight_properties));
499 props.type = BACKLIGHT_RAW;
500 props.max_brightness = MAX_BRIGHTNESS;
502 bd = backlight_device_register("ams369fg06-bl", &spi->dev, lcd,
503 &ams369fg06_backlight_ops, &props);
506 goto out_lcd_unregister;
509 bd->props.brightness = DEFAULT_BRIGHTNESS;
512 if (!lcd->lcd_pd->lcd_enabled) {
514 * if lcd panel was off from bootloader then
515 * current lcd status is powerdown and then
516 * it enables lcd panel.
518 lcd->power = FB_BLANK_POWERDOWN;
520 ams369fg06_power(lcd, FB_BLANK_UNBLANK);
522 lcd->power = FB_BLANK_UNBLANK;
525 dev_set_drvdata(&spi->dev, lcd);
527 dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
532 lcd_device_unregister(ld);
536 static int ams369fg06_remove(struct spi_device *spi)
538 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
540 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
541 backlight_device_unregister(lcd->bd);
542 lcd_device_unregister(lcd->ld);
547 #if defined(CONFIG_PM)
548 static int ams369fg06_suspend(struct spi_device *spi, pm_message_t mesg)
550 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
552 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
555 * when lcd panel is suspend, lcd panel becomes off
556 * regardless of status.
558 return ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
561 static int ams369fg06_resume(struct spi_device *spi)
563 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
565 lcd->power = FB_BLANK_POWERDOWN;
567 return ams369fg06_power(lcd, FB_BLANK_UNBLANK);
570 #define ams369fg06_suspend NULL
571 #define ams369fg06_resume NULL
574 static void ams369fg06_shutdown(struct spi_device *spi)
576 struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
578 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
581 static struct spi_driver ams369fg06_driver = {
583 .name = "ams369fg06",
584 .owner = THIS_MODULE,
586 .probe = ams369fg06_probe,
587 .remove = ams369fg06_remove,
588 .shutdown = ams369fg06_shutdown,
589 .suspend = ams369fg06_suspend,
590 .resume = ams369fg06_resume,
593 module_spi_driver(ams369fg06_driver);
595 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
596 MODULE_DESCRIPTION("ams369fg06 LCD Driver");
597 MODULE_LICENSE("GPL");