1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ams369fg06 AMOLED LCD panel driver.
5 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Author: Jingoo Han <jg1.han@samsung.com>
8 * Derived from drivers/video/s6e63m0.c
11 #include <linux/backlight.h>
12 #include <linux/delay.h>
14 #include <linux/gpio.h>
15 #include <linux/lcd.h>
16 #include <linux/module.h>
17 #include <linux/spi/spi.h>
18 #include <linux/wait.h>
20 #define SLEEPMSEC 0x1000
22 #define DEFMASK 0xFF00
23 #define COMMAND_ONLY 0xFE
24 #define DATA_ONLY 0xFF
26 #define MAX_GAMMA_LEVEL 5
27 #define GAMMA_TABLE_COUNT 21
29 #define MIN_BRIGHTNESS 0
30 #define MAX_BRIGHTNESS 255
31 #define DEFAULT_BRIGHTNESS 150
35 struct spi_device *spi;
37 struct lcd_device *ld;
38 struct backlight_device *bd;
39 struct lcd_platform_data *lcd_pd;
42 static const unsigned short seq_display_on[] = {
47 static const unsigned short seq_display_off[] = {
52 static const unsigned short seq_stand_by_on[] = {
58 static const unsigned short seq_stand_by_off[] = {
64 static const unsigned short seq_setting[] = {
116 /* gamma value: 2.2 */
117 static const unsigned int ams369fg06_22_250[] = {
118 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
119 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
120 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
123 static const unsigned int ams369fg06_22_200[] = {
124 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
125 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
126 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
129 static const unsigned int ams369fg06_22_150[] = {
130 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
131 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
132 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
135 static const unsigned int ams369fg06_22_100[] = {
136 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
137 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
138 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
141 static const unsigned int ams369fg06_22_50[] = {
142 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
143 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
144 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
147 struct ams369fg06_gamma {
148 unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
151 static struct ams369fg06_gamma gamma_table = {
152 .gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
153 .gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
154 .gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
155 .gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
156 .gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
159 static int ams369fg06_spi_write_byte(struct ams369fg06 *lcd, int addr, int data)
162 struct spi_message msg;
164 struct spi_transfer xfer = {
169 buf[0] = (addr << 8) | data;
171 spi_message_init(&msg);
172 spi_message_add_tail(&xfer, &msg);
174 return spi_sync(lcd->spi, &msg);
177 static int ams369fg06_spi_write(struct ams369fg06 *lcd, unsigned char address,
178 unsigned char command)
182 if (address != DATA_ONLY)
183 ret = ams369fg06_spi_write_byte(lcd, 0x70, address);
184 if (command != COMMAND_ONLY)
185 ret = ams369fg06_spi_write_byte(lcd, 0x72, command);
190 static int ams369fg06_panel_send_sequence(struct ams369fg06 *lcd,
191 const unsigned short *wbuf)
195 while ((wbuf[i] & DEFMASK) != ENDDEF) {
196 if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
197 ret = ams369fg06_spi_write(lcd, wbuf[i], wbuf[i+1]);
209 static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
210 const unsigned int *gamma)
215 for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
216 ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
217 ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
218 ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
220 dev_err(lcd->dev, "failed to set gamma table.\n");
229 static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
234 if ((brightness >= 0) && (brightness <= 50))
236 else if ((brightness > 50) && (brightness <= 100))
238 else if ((brightness > 100) && (brightness <= 150))
240 else if ((brightness > 150) && (brightness <= 200))
242 else if ((brightness > 200) && (brightness <= 255))
245 ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
250 static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
253 static const unsigned short *init_seq[] = {
258 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
259 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
267 static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
270 static const unsigned short *init_seq[] = {
275 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
276 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
284 static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
288 static const unsigned short *init_seq[] = {
293 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
294 ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
302 static int ams369fg06_power_is_on(int power)
304 return power <= FB_BLANK_NORMAL;
307 static int ams369fg06_power_on(struct ams369fg06 *lcd)
310 struct lcd_platform_data *pd;
311 struct backlight_device *bd;
317 pd->power_on(lcd->ld, 1);
318 msleep(pd->power_on_delay);
322 dev_err(lcd->dev, "reset is NULL.\n");
327 msleep(pd->reset_delay);
329 ret = ams369fg06_ldi_init(lcd);
331 dev_err(lcd->dev, "failed to initialize ldi.\n");
335 ret = ams369fg06_ldi_enable(lcd);
337 dev_err(lcd->dev, "failed to enable ldi.\n");
341 /* set brightness to current value after power on or resume. */
342 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
344 dev_err(lcd->dev, "lcd gamma setting failed.\n");
351 static int ams369fg06_power_off(struct ams369fg06 *lcd)
354 struct lcd_platform_data *pd;
358 ret = ams369fg06_ldi_disable(lcd);
360 dev_err(lcd->dev, "lcd setting failed.\n");
364 msleep(pd->power_off_delay);
367 pd->power_on(lcd->ld, 0);
372 static int ams369fg06_power(struct ams369fg06 *lcd, int power)
376 if (ams369fg06_power_is_on(power) &&
377 !ams369fg06_power_is_on(lcd->power))
378 ret = ams369fg06_power_on(lcd);
379 else if (!ams369fg06_power_is_on(power) &&
380 ams369fg06_power_is_on(lcd->power))
381 ret = ams369fg06_power_off(lcd);
389 static int ams369fg06_get_power(struct lcd_device *ld)
391 struct ams369fg06 *lcd = lcd_get_data(ld);
396 static int ams369fg06_set_power(struct lcd_device *ld, int power)
398 struct ams369fg06 *lcd = lcd_get_data(ld);
400 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
401 power != FB_BLANK_NORMAL) {
402 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
406 return ams369fg06_power(lcd, power);
409 static int ams369fg06_set_brightness(struct backlight_device *bd)
412 int brightness = bd->props.brightness;
413 struct ams369fg06 *lcd = bl_get_data(bd);
415 if (brightness < MIN_BRIGHTNESS ||
416 brightness > bd->props.max_brightness) {
417 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
418 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
422 ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
424 dev_err(&bd->dev, "lcd brightness setting failed.\n");
431 static struct lcd_ops ams369fg06_lcd_ops = {
432 .get_power = ams369fg06_get_power,
433 .set_power = ams369fg06_set_power,
436 static const struct backlight_ops ams369fg06_backlight_ops = {
437 .update_status = ams369fg06_set_brightness,
440 static int ams369fg06_probe(struct spi_device *spi)
443 struct ams369fg06 *lcd = NULL;
444 struct lcd_device *ld = NULL;
445 struct backlight_device *bd = NULL;
446 struct backlight_properties props;
448 lcd = devm_kzalloc(&spi->dev, sizeof(struct ams369fg06), GFP_KERNEL);
452 /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
453 spi->bits_per_word = 16;
455 ret = spi_setup(spi);
457 dev_err(&spi->dev, "spi setup failed.\n");
462 lcd->dev = &spi->dev;
464 lcd->lcd_pd = dev_get_platdata(&spi->dev);
466 dev_err(&spi->dev, "platform data is NULL\n");
470 ld = devm_lcd_device_register(&spi->dev, "ams369fg06", &spi->dev, lcd,
471 &ams369fg06_lcd_ops);
477 memset(&props, 0, sizeof(struct backlight_properties));
478 props.type = BACKLIGHT_RAW;
479 props.max_brightness = MAX_BRIGHTNESS;
481 bd = devm_backlight_device_register(&spi->dev, "ams369fg06-bl",
483 &ams369fg06_backlight_ops, &props);
487 bd->props.brightness = DEFAULT_BRIGHTNESS;
490 if (!lcd->lcd_pd->lcd_enabled) {
492 * if lcd panel was off from bootloader then
493 * current lcd status is powerdown and then
494 * it enables lcd panel.
496 lcd->power = FB_BLANK_POWERDOWN;
498 ams369fg06_power(lcd, FB_BLANK_UNBLANK);
500 lcd->power = FB_BLANK_UNBLANK;
503 spi_set_drvdata(spi, lcd);
505 dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
510 static int ams369fg06_remove(struct spi_device *spi)
512 struct ams369fg06 *lcd = spi_get_drvdata(spi);
514 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
518 #ifdef CONFIG_PM_SLEEP
519 static int ams369fg06_suspend(struct device *dev)
521 struct ams369fg06 *lcd = dev_get_drvdata(dev);
523 dev_dbg(dev, "lcd->power = %d\n", lcd->power);
526 * when lcd panel is suspend, lcd panel becomes off
527 * regardless of status.
529 return ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
532 static int ams369fg06_resume(struct device *dev)
534 struct ams369fg06 *lcd = dev_get_drvdata(dev);
536 lcd->power = FB_BLANK_POWERDOWN;
538 return ams369fg06_power(lcd, FB_BLANK_UNBLANK);
542 static SIMPLE_DEV_PM_OPS(ams369fg06_pm_ops, ams369fg06_suspend,
545 static void ams369fg06_shutdown(struct spi_device *spi)
547 struct ams369fg06 *lcd = spi_get_drvdata(spi);
549 ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
552 static struct spi_driver ams369fg06_driver = {
554 .name = "ams369fg06",
555 .pm = &ams369fg06_pm_ops,
557 .probe = ams369fg06_probe,
558 .remove = ams369fg06_remove,
559 .shutdown = ams369fg06_shutdown,
562 module_spi_driver(ams369fg06_driver);
564 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
565 MODULE_DESCRIPTION("ams369fg06 LCD Driver");
566 MODULE_LICENSE("GPL");