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>
13 #include <linux/lcd.h>
14 #include <linux/module.h>
15 #include <linux/spi/spi.h>
16 #include <linux/wait.h>
18 #define SLEEPMSEC 0x1000
20 #define DEFMASK 0xFF00
21 #define COMMAND_ONLY 0xFE
22 #define DATA_ONLY 0xFF
24 #define MAX_GAMMA_LEVEL 5
25 #define GAMMA_TABLE_COUNT 21
27 #define MIN_BRIGHTNESS 0
28 #define MAX_BRIGHTNESS 255
29 #define DEFAULT_BRIGHTNESS 150
33 struct spi_device
*spi
;
35 struct lcd_device
*ld
;
36 struct backlight_device
*bd
;
37 struct lcd_platform_data
*lcd_pd
;
40 static const unsigned short seq_display_on
[] = {
45 static const unsigned short seq_display_off
[] = {
50 static const unsigned short seq_stand_by_on
[] = {
56 static const unsigned short seq_stand_by_off
[] = {
62 static const unsigned short seq_setting
[] = {
114 /* gamma value: 2.2 */
115 static const unsigned int ams369fg06_22_250
[] = {
116 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
117 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
118 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
121 static const unsigned int ams369fg06_22_200
[] = {
122 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
123 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
124 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
127 static const unsigned int ams369fg06_22_150
[] = {
128 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
129 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
130 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
133 static const unsigned int ams369fg06_22_100
[] = {
134 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
135 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
136 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
139 static const unsigned int ams369fg06_22_50
[] = {
140 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
141 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
142 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
145 struct ams369fg06_gamma
{
146 unsigned int *gamma_22_table
[MAX_GAMMA_LEVEL
];
149 static struct ams369fg06_gamma gamma_table
= {
150 .gamma_22_table
[0] = (unsigned int *)&ams369fg06_22_50
,
151 .gamma_22_table
[1] = (unsigned int *)&ams369fg06_22_100
,
152 .gamma_22_table
[2] = (unsigned int *)&ams369fg06_22_150
,
153 .gamma_22_table
[3] = (unsigned int *)&ams369fg06_22_200
,
154 .gamma_22_table
[4] = (unsigned int *)&ams369fg06_22_250
,
157 static int ams369fg06_spi_write_byte(struct ams369fg06
*lcd
, int addr
, int data
)
160 struct spi_message msg
;
162 struct spi_transfer xfer
= {
167 buf
[0] = (addr
<< 8) | data
;
169 spi_message_init(&msg
);
170 spi_message_add_tail(&xfer
, &msg
);
172 return spi_sync(lcd
->spi
, &msg
);
175 static int ams369fg06_spi_write(struct ams369fg06
*lcd
, unsigned char address
,
176 unsigned char command
)
180 if (address
!= DATA_ONLY
)
181 ret
= ams369fg06_spi_write_byte(lcd
, 0x70, address
);
182 if (command
!= COMMAND_ONLY
)
183 ret
= ams369fg06_spi_write_byte(lcd
, 0x72, command
);
188 static int ams369fg06_panel_send_sequence(struct ams369fg06
*lcd
,
189 const unsigned short *wbuf
)
193 while ((wbuf
[i
] & DEFMASK
) != ENDDEF
) {
194 if ((wbuf
[i
] & DEFMASK
) != SLEEPMSEC
) {
195 ret
= ams369fg06_spi_write(lcd
, wbuf
[i
], wbuf
[i
+1]);
207 static int _ams369fg06_gamma_ctl(struct ams369fg06
*lcd
,
208 const unsigned int *gamma
)
213 for (i
= 0 ; i
< GAMMA_TABLE_COUNT
/ 3; i
++) {
214 ret
= ams369fg06_spi_write(lcd
, 0x40 + i
, gamma
[i
]);
215 ret
= ams369fg06_spi_write(lcd
, 0x50 + i
, gamma
[i
+7*1]);
216 ret
= ams369fg06_spi_write(lcd
, 0x60 + i
, gamma
[i
+7*2]);
218 dev_err(lcd
->dev
, "failed to set gamma table.\n");
227 static int ams369fg06_gamma_ctl(struct ams369fg06
*lcd
, int brightness
)
232 if ((brightness
>= 0) && (brightness
<= 50))
234 else if ((brightness
> 50) && (brightness
<= 100))
236 else if ((brightness
> 100) && (brightness
<= 150))
238 else if ((brightness
> 150) && (brightness
<= 200))
240 else if ((brightness
> 200) && (brightness
<= 255))
243 ret
= _ams369fg06_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[gamma
]);
248 static int ams369fg06_ldi_init(struct ams369fg06
*lcd
)
251 static const unsigned short *init_seq
[] = {
256 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
257 ret
= ams369fg06_panel_send_sequence(lcd
, init_seq
[i
]);
265 static int ams369fg06_ldi_enable(struct ams369fg06
*lcd
)
268 static const unsigned short *init_seq
[] = {
273 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
274 ret
= ams369fg06_panel_send_sequence(lcd
, init_seq
[i
]);
282 static int ams369fg06_ldi_disable(struct ams369fg06
*lcd
)
286 static const unsigned short *init_seq
[] = {
291 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
292 ret
= ams369fg06_panel_send_sequence(lcd
, init_seq
[i
]);
300 static int ams369fg06_power_is_on(int power
)
302 return power
<= BACKLIGHT_POWER_REDUCED
;
305 static int ams369fg06_power_on(struct ams369fg06
*lcd
)
308 struct lcd_platform_data
*pd
;
309 struct backlight_device
*bd
;
315 pd
->power_on(lcd
->ld
, 1);
316 msleep(pd
->power_on_delay
);
320 dev_err(lcd
->dev
, "reset is NULL.\n");
325 msleep(pd
->reset_delay
);
327 ret
= ams369fg06_ldi_init(lcd
);
329 dev_err(lcd
->dev
, "failed to initialize ldi.\n");
333 ret
= ams369fg06_ldi_enable(lcd
);
335 dev_err(lcd
->dev
, "failed to enable ldi.\n");
339 /* set brightness to current value after power on or resume. */
340 ret
= ams369fg06_gamma_ctl(lcd
, bd
->props
.brightness
);
342 dev_err(lcd
->dev
, "lcd gamma setting failed.\n");
349 static int ams369fg06_power_off(struct ams369fg06
*lcd
)
352 struct lcd_platform_data
*pd
;
356 ret
= ams369fg06_ldi_disable(lcd
);
358 dev_err(lcd
->dev
, "lcd setting failed.\n");
362 msleep(pd
->power_off_delay
);
365 pd
->power_on(lcd
->ld
, 0);
370 static int ams369fg06_power(struct ams369fg06
*lcd
, int power
)
374 if (ams369fg06_power_is_on(power
) &&
375 !ams369fg06_power_is_on(lcd
->power
))
376 ret
= ams369fg06_power_on(lcd
);
377 else if (!ams369fg06_power_is_on(power
) &&
378 ams369fg06_power_is_on(lcd
->power
))
379 ret
= ams369fg06_power_off(lcd
);
387 static int ams369fg06_get_power(struct lcd_device
*ld
)
389 struct ams369fg06
*lcd
= lcd_get_data(ld
);
394 static int ams369fg06_set_power(struct lcd_device
*ld
, int power
)
396 struct ams369fg06
*lcd
= lcd_get_data(ld
);
398 if (power
!= BACKLIGHT_POWER_ON
&& power
!= BACKLIGHT_POWER_OFF
&&
399 power
!= BACKLIGHT_POWER_REDUCED
) {
400 dev_err(lcd
->dev
, "power value should be 0, 1 or 4.\n");
404 return ams369fg06_power(lcd
, power
);
407 static int ams369fg06_set_brightness(struct backlight_device
*bd
)
410 int brightness
= bd
->props
.brightness
;
411 struct ams369fg06
*lcd
= bl_get_data(bd
);
413 if (brightness
< MIN_BRIGHTNESS
||
414 brightness
> bd
->props
.max_brightness
) {
415 dev_err(&bd
->dev
, "lcd brightness should be %d to %d.\n",
416 MIN_BRIGHTNESS
, MAX_BRIGHTNESS
);
420 ret
= ams369fg06_gamma_ctl(lcd
, bd
->props
.brightness
);
422 dev_err(&bd
->dev
, "lcd brightness setting failed.\n");
429 static const struct lcd_ops ams369fg06_lcd_ops
= {
430 .get_power
= ams369fg06_get_power
,
431 .set_power
= ams369fg06_set_power
,
434 static const struct backlight_ops ams369fg06_backlight_ops
= {
435 .update_status
= ams369fg06_set_brightness
,
438 static int ams369fg06_probe(struct spi_device
*spi
)
441 struct ams369fg06
*lcd
= NULL
;
442 struct lcd_device
*ld
= NULL
;
443 struct backlight_device
*bd
= NULL
;
444 struct backlight_properties props
;
446 lcd
= devm_kzalloc(&spi
->dev
, sizeof(struct ams369fg06
), GFP_KERNEL
);
450 /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
451 spi
->bits_per_word
= 16;
453 ret
= spi_setup(spi
);
455 dev_err(&spi
->dev
, "spi setup failed.\n");
460 lcd
->dev
= &spi
->dev
;
462 lcd
->lcd_pd
= dev_get_platdata(&spi
->dev
);
464 dev_err(&spi
->dev
, "platform data is NULL\n");
468 ld
= devm_lcd_device_register(&spi
->dev
, "ams369fg06", &spi
->dev
, lcd
,
469 &ams369fg06_lcd_ops
);
475 memset(&props
, 0, sizeof(struct backlight_properties
));
476 props
.type
= BACKLIGHT_RAW
;
477 props
.max_brightness
= MAX_BRIGHTNESS
;
479 bd
= devm_backlight_device_register(&spi
->dev
, "ams369fg06-bl",
481 &ams369fg06_backlight_ops
, &props
);
485 bd
->props
.brightness
= DEFAULT_BRIGHTNESS
;
488 if (!lcd
->lcd_pd
->lcd_enabled
) {
490 * if lcd panel was off from bootloader then
491 * current lcd status is powerdown and then
492 * it enables lcd panel.
494 lcd
->power
= BACKLIGHT_POWER_OFF
;
496 ams369fg06_power(lcd
, BACKLIGHT_POWER_ON
);
498 lcd
->power
= BACKLIGHT_POWER_ON
;
501 spi_set_drvdata(spi
, lcd
);
503 dev_info(&spi
->dev
, "ams369fg06 panel driver has been probed.\n");
508 static void ams369fg06_remove(struct spi_device
*spi
)
510 struct ams369fg06
*lcd
= spi_get_drvdata(spi
);
512 ams369fg06_power(lcd
, BACKLIGHT_POWER_OFF
);
515 #ifdef CONFIG_PM_SLEEP
516 static int ams369fg06_suspend(struct device
*dev
)
518 struct ams369fg06
*lcd
= dev_get_drvdata(dev
);
520 dev_dbg(dev
, "lcd->power = %d\n", lcd
->power
);
523 * when lcd panel is suspend, lcd panel becomes off
524 * regardless of status.
526 return ams369fg06_power(lcd
, BACKLIGHT_POWER_OFF
);
529 static int ams369fg06_resume(struct device
*dev
)
531 struct ams369fg06
*lcd
= dev_get_drvdata(dev
);
533 lcd
->power
= BACKLIGHT_POWER_OFF
;
535 return ams369fg06_power(lcd
, BACKLIGHT_POWER_ON
);
539 static SIMPLE_DEV_PM_OPS(ams369fg06_pm_ops
, ams369fg06_suspend
,
542 static void ams369fg06_shutdown(struct spi_device
*spi
)
544 struct ams369fg06
*lcd
= spi_get_drvdata(spi
);
546 ams369fg06_power(lcd
, BACKLIGHT_POWER_OFF
);
549 static struct spi_driver ams369fg06_driver
= {
551 .name
= "ams369fg06",
552 .pm
= &ams369fg06_pm_ops
,
554 .probe
= ams369fg06_probe
,
555 .remove
= ams369fg06_remove
,
556 .shutdown
= ams369fg06_shutdown
,
559 module_spi_driver(ams369fg06_driver
);
561 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
562 MODULE_DESCRIPTION("ams369fg06 LCD Driver");
563 MODULE_LICENSE("GPL");