2 * S6E63M0 AMOLED LCD panel driver.
4 * Author: InKi Dae <inki.dae@samsung.com>
6 * Derived from drivers/video/omap/lcd-apollon.c
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/backlight.h>
15 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kernel.h>
21 #include <linux/lcd.h>
22 #include <linux/module.h>
23 #include <linux/spi/spi.h>
24 #include <linux/wait.h>
26 #include "s6e63m0_gamma.h"
28 #define SLEEPMSEC 0x1000
30 #define DEFMASK 0xFF00
31 #define COMMAND_ONLY 0xFE
32 #define DATA_ONLY 0xFF
34 #define MIN_BRIGHTNESS 0
35 #define MAX_BRIGHTNESS 10
39 struct spi_device
*spi
;
41 unsigned int current_brightness
;
42 unsigned int gamma_mode
;
43 unsigned int gamma_table_count
;
44 struct lcd_device
*ld
;
45 struct backlight_device
*bd
;
46 struct lcd_platform_data
*lcd_pd
;
49 static const unsigned short seq_panel_condition_set
[] = {
68 static const unsigned short seq_display_condition_set
[] = {
82 static const unsigned short seq_gamma_setting
[] = {
111 static const unsigned short seq_etc_condition_set
[] = {
310 static const unsigned short seq_acl_on
[] = {
317 static const unsigned short seq_acl_off
[] = {
324 static const unsigned short seq_elvss_on
[] = {
331 static const unsigned short seq_elvss_off
[] = {
338 static const unsigned short seq_stand_by_off
[] = {
344 static const unsigned short seq_stand_by_on
[] = {
350 static const unsigned short seq_display_on
[] = {
357 static int s6e63m0_spi_write_byte(struct s6e63m0
*lcd
, int addr
, int data
)
360 struct spi_message msg
;
362 struct spi_transfer xfer
= {
367 buf
[0] = (addr
<< 8) | data
;
369 spi_message_init(&msg
);
370 spi_message_add_tail(&xfer
, &msg
);
372 return spi_sync(lcd
->spi
, &msg
);
375 static int s6e63m0_spi_write(struct s6e63m0
*lcd
, unsigned char address
,
376 unsigned char command
)
380 if (address
!= DATA_ONLY
)
381 ret
= s6e63m0_spi_write_byte(lcd
, 0x0, address
);
382 if (command
!= COMMAND_ONLY
)
383 ret
= s6e63m0_spi_write_byte(lcd
, 0x1, command
);
388 static int s6e63m0_panel_send_sequence(struct s6e63m0
*lcd
,
389 const unsigned short *wbuf
)
393 while ((wbuf
[i
] & DEFMASK
) != ENDDEF
) {
394 if ((wbuf
[i
] & DEFMASK
) != SLEEPMSEC
) {
395 ret
= s6e63m0_spi_write(lcd
, wbuf
[i
], wbuf
[i
+1]);
407 static int _s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, const unsigned int *gamma
)
412 /* disable gamma table updating. */
413 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x00);
415 dev_err(lcd
->dev
, "failed to disable gamma table updating.\n");
419 for (i
= 0 ; i
< GAMMA_TABLE_COUNT
; i
++) {
420 ret
= s6e63m0_spi_write(lcd
, DATA_ONLY
, gamma
[i
]);
422 dev_err(lcd
->dev
, "failed to set gamma table.\n");
427 /* update gamma table. */
428 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x01);
430 dev_err(lcd
->dev
, "failed to update gamma table.\n");
436 static int s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, int gamma
)
440 ret
= _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[gamma
]);
446 static int s6e63m0_ldi_init(struct s6e63m0
*lcd
)
449 const unsigned short *init_seq
[] = {
450 seq_panel_condition_set
,
451 seq_display_condition_set
,
453 seq_etc_condition_set
,
458 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
459 ret
= s6e63m0_panel_send_sequence(lcd
, init_seq
[i
]);
467 static int s6e63m0_ldi_enable(struct s6e63m0
*lcd
)
470 const unsigned short *enable_seq
[] = {
475 for (i
= 0; i
< ARRAY_SIZE(enable_seq
); i
++) {
476 ret
= s6e63m0_panel_send_sequence(lcd
, enable_seq
[i
]);
484 static int s6e63m0_ldi_disable(struct s6e63m0
*lcd
)
488 ret
= s6e63m0_panel_send_sequence(lcd
, seq_stand_by_on
);
493 static int s6e63m0_power_is_on(int power
)
495 return power
<= FB_BLANK_NORMAL
;
498 static int s6e63m0_power_on(struct s6e63m0
*lcd
)
501 struct lcd_platform_data
*pd
;
502 struct backlight_device
*bd
;
508 dev_err(lcd
->dev
, "power_on is NULL.\n");
512 pd
->power_on(lcd
->ld
, 1);
513 msleep(pd
->power_on_delay
);
516 dev_err(lcd
->dev
, "reset is NULL.\n");
521 msleep(pd
->reset_delay
);
523 ret
= s6e63m0_ldi_init(lcd
);
525 dev_err(lcd
->dev
, "failed to initialize ldi.\n");
529 ret
= s6e63m0_ldi_enable(lcd
);
531 dev_err(lcd
->dev
, "failed to enable ldi.\n");
535 /* set brightness to current value after power on or resume. */
536 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
538 dev_err(lcd
->dev
, "lcd gamma setting failed.\n");
545 static int s6e63m0_power_off(struct s6e63m0
*lcd
)
548 struct lcd_platform_data
*pd
;
552 ret
= s6e63m0_ldi_disable(lcd
);
554 dev_err(lcd
->dev
, "lcd setting failed.\n");
558 msleep(pd
->power_off_delay
);
560 pd
->power_on(lcd
->ld
, 0);
565 static int s6e63m0_power(struct s6e63m0
*lcd
, int power
)
569 if (s6e63m0_power_is_on(power
) && !s6e63m0_power_is_on(lcd
->power
))
570 ret
= s6e63m0_power_on(lcd
);
571 else if (!s6e63m0_power_is_on(power
) && s6e63m0_power_is_on(lcd
->power
))
572 ret
= s6e63m0_power_off(lcd
);
580 static int s6e63m0_set_power(struct lcd_device
*ld
, int power
)
582 struct s6e63m0
*lcd
= lcd_get_data(ld
);
584 if (power
!= FB_BLANK_UNBLANK
&& power
!= FB_BLANK_POWERDOWN
&&
585 power
!= FB_BLANK_NORMAL
) {
586 dev_err(lcd
->dev
, "power value should be 0, 1 or 4.\n");
590 return s6e63m0_power(lcd
, power
);
593 static int s6e63m0_get_power(struct lcd_device
*ld
)
595 struct s6e63m0
*lcd
= lcd_get_data(ld
);
600 static int s6e63m0_set_brightness(struct backlight_device
*bd
)
602 int ret
= 0, brightness
= bd
->props
.brightness
;
603 struct s6e63m0
*lcd
= bl_get_data(bd
);
605 if (brightness
< MIN_BRIGHTNESS
||
606 brightness
> bd
->props
.max_brightness
) {
607 dev_err(&bd
->dev
, "lcd brightness should be %d to %d.\n",
608 MIN_BRIGHTNESS
, MAX_BRIGHTNESS
);
612 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
614 dev_err(&bd
->dev
, "lcd brightness setting failed.\n");
621 static struct lcd_ops s6e63m0_lcd_ops
= {
622 .set_power
= s6e63m0_set_power
,
623 .get_power
= s6e63m0_get_power
,
626 static const struct backlight_ops s6e63m0_backlight_ops
= {
627 .update_status
= s6e63m0_set_brightness
,
630 static ssize_t
s6e63m0_sysfs_show_gamma_mode(struct device
*dev
,
631 struct device_attribute
*attr
, char *buf
)
633 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
636 switch (lcd
->gamma_mode
) {
638 sprintf(temp
, "2.2 mode\n");
642 sprintf(temp
, "1.9 mode\n");
646 sprintf(temp
, "1.7 mode\n");
650 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7)n");
657 static ssize_t
s6e63m0_sysfs_store_gamma_mode(struct device
*dev
,
658 struct device_attribute
*attr
,
659 const char *buf
, size_t len
)
661 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
662 struct backlight_device
*bd
= NULL
;
665 rc
= kstrtouint(buf
, 0, &lcd
->gamma_mode
);
671 brightness
= bd
->props
.brightness
;
673 switch (lcd
->gamma_mode
) {
675 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
678 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_19_table
[brightness
]);
681 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_17_table
[brightness
]);
684 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7\n");
685 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
691 static DEVICE_ATTR(gamma_mode
, 0644,
692 s6e63m0_sysfs_show_gamma_mode
, s6e63m0_sysfs_store_gamma_mode
);
694 static ssize_t
s6e63m0_sysfs_show_gamma_table(struct device
*dev
,
695 struct device_attribute
*attr
, char *buf
)
697 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
700 sprintf(temp
, "%u\n", lcd
->gamma_table_count
);
705 static DEVICE_ATTR(gamma_table
, 0444,
706 s6e63m0_sysfs_show_gamma_table
, NULL
);
708 static int s6e63m0_probe(struct spi_device
*spi
)
711 struct s6e63m0
*lcd
= NULL
;
712 struct lcd_device
*ld
= NULL
;
713 struct backlight_device
*bd
= NULL
;
714 struct backlight_properties props
;
716 lcd
= devm_kzalloc(&spi
->dev
, sizeof(struct s6e63m0
), GFP_KERNEL
);
720 /* s6e63m0 lcd panel uses 3-wire 9bits SPI Mode. */
721 spi
->bits_per_word
= 9;
723 ret
= spi_setup(spi
);
725 dev_err(&spi
->dev
, "spi setup failed.\n");
730 lcd
->dev
= &spi
->dev
;
732 lcd
->lcd_pd
= dev_get_platdata(&spi
->dev
);
734 dev_err(&spi
->dev
, "platform data is NULL.\n");
738 ld
= devm_lcd_device_register(&spi
->dev
, "s6e63m0", &spi
->dev
, lcd
,
745 memset(&props
, 0, sizeof(struct backlight_properties
));
746 props
.type
= BACKLIGHT_RAW
;
747 props
.max_brightness
= MAX_BRIGHTNESS
;
749 bd
= devm_backlight_device_register(&spi
->dev
, "s6e63m0bl-bl",
750 &spi
->dev
, lcd
, &s6e63m0_backlight_ops
,
755 bd
->props
.brightness
= MAX_BRIGHTNESS
;
759 * it gets gamma table count available so it gets user
762 lcd
->gamma_table_count
=
763 sizeof(gamma_table
) / (MAX_GAMMA_LEVEL
* sizeof(int *));
765 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_mode
);
767 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
769 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_table
);
771 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
774 * if lcd panel was on from bootloader like u-boot then
777 if (!lcd
->lcd_pd
->lcd_enabled
) {
779 * if lcd panel was off from bootloader then
780 * current lcd status is powerdown and then
781 * it enables lcd panel.
783 lcd
->power
= FB_BLANK_POWERDOWN
;
785 s6e63m0_power(lcd
, FB_BLANK_UNBLANK
);
787 lcd
->power
= FB_BLANK_UNBLANK
;
790 spi_set_drvdata(spi
, lcd
);
792 dev_info(&spi
->dev
, "s6e63m0 panel driver has been probed.\n");
797 static int s6e63m0_remove(struct spi_device
*spi
)
799 struct s6e63m0
*lcd
= spi_get_drvdata(spi
);
801 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
802 device_remove_file(&spi
->dev
, &dev_attr_gamma_table
);
803 device_remove_file(&spi
->dev
, &dev_attr_gamma_mode
);
808 #ifdef CONFIG_PM_SLEEP
809 static int s6e63m0_suspend(struct device
*dev
)
811 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
813 dev_dbg(dev
, "lcd->power = %d\n", lcd
->power
);
816 * when lcd panel is suspend, lcd panel becomes off
817 * regardless of status.
819 return s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
822 static int s6e63m0_resume(struct device
*dev
)
824 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
826 lcd
->power
= FB_BLANK_POWERDOWN
;
828 return s6e63m0_power(lcd
, FB_BLANK_UNBLANK
);
832 static SIMPLE_DEV_PM_OPS(s6e63m0_pm_ops
, s6e63m0_suspend
, s6e63m0_resume
);
834 /* Power down all displays on reboot, poweroff or halt. */
835 static void s6e63m0_shutdown(struct spi_device
*spi
)
837 struct s6e63m0
*lcd
= spi_get_drvdata(spi
);
839 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
842 static struct spi_driver s6e63m0_driver
= {
845 .pm
= &s6e63m0_pm_ops
,
847 .probe
= s6e63m0_probe
,
848 .remove
= s6e63m0_remove
,
849 .shutdown
= s6e63m0_shutdown
,
852 module_spi_driver(s6e63m0_driver
);
854 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
855 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
856 MODULE_LICENSE("GPL");