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.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/wait.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/spi/spi.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/lcd.h>
32 #include <linux/backlight.h>
34 #include "s6e63m0_gamma.h"
36 #define SLEEPMSEC 0x1000
38 #define DEFMASK 0xFF00
39 #define COMMAND_ONLY 0xFE
40 #define DATA_ONLY 0xFF
42 #define MIN_BRIGHTNESS 0
43 #define MAX_BRIGHTNESS 10
45 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
49 struct spi_device
*spi
;
51 unsigned int current_brightness
;
52 unsigned int gamma_mode
;
53 unsigned int gamma_table_count
;
54 struct lcd_device
*ld
;
55 struct backlight_device
*bd
;
56 struct lcd_platform_data
*lcd_pd
;
59 static const unsigned short SEQ_PANEL_CONDITION_SET
[] = {
78 static const unsigned short SEQ_DISPLAY_CONDITION_SET
[] = {
92 static const unsigned short SEQ_GAMMA_SETTING
[] = {
121 static const unsigned short SEQ_ETC_CONDITION_SET
[] = {
320 static const unsigned short SEQ_ACL_ON
[] = {
327 static const unsigned short SEQ_ACL_OFF
[] = {
334 static const unsigned short SEQ_ELVSS_ON
[] = {
341 static const unsigned short SEQ_ELVSS_OFF
[] = {
348 static const unsigned short SEQ_STAND_BY_OFF
[] = {
354 static const unsigned short SEQ_STAND_BY_ON
[] = {
360 static const unsigned short SEQ_DISPLAY_ON
[] = {
367 static int s6e63m0_spi_write_byte(struct s6e63m0
*lcd
, int addr
, int data
)
370 struct spi_message msg
;
372 struct spi_transfer xfer
= {
377 buf
[0] = (addr
<< 8) | data
;
379 spi_message_init(&msg
);
380 spi_message_add_tail(&xfer
, &msg
);
382 return spi_sync(lcd
->spi
, &msg
);
385 static int s6e63m0_spi_write(struct s6e63m0
*lcd
, unsigned char address
,
386 unsigned char command
)
390 if (address
!= DATA_ONLY
)
391 ret
= s6e63m0_spi_write_byte(lcd
, 0x0, address
);
392 if (command
!= COMMAND_ONLY
)
393 ret
= s6e63m0_spi_write_byte(lcd
, 0x1, command
);
398 static int s6e63m0_panel_send_sequence(struct s6e63m0
*lcd
,
399 const unsigned short *wbuf
)
403 while ((wbuf
[i
] & DEFMASK
) != ENDDEF
) {
404 if ((wbuf
[i
] & DEFMASK
) != SLEEPMSEC
) {
405 ret
= s6e63m0_spi_write(lcd
, wbuf
[i
], wbuf
[i
+1]);
409 udelay(wbuf
[i
+1]*1000);
416 static int _s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, const unsigned int *gamma
)
421 /* disable gamma table updating. */
422 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x00);
424 dev_err(lcd
->dev
, "failed to disable gamma table updating.\n");
428 for (i
= 0 ; i
< GAMMA_TABLE_COUNT
; i
++) {
429 ret
= s6e63m0_spi_write(lcd
, DATA_ONLY
, gamma
[i
]);
431 dev_err(lcd
->dev
, "failed to set gamma table.\n");
436 /* update gamma table. */
437 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x01);
439 dev_err(lcd
->dev
, "failed to update gamma table.\n");
445 static int s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, int gamma
)
449 ret
= _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[gamma
]);
455 static int s6e63m0_ldi_init(struct s6e63m0
*lcd
)
458 const unsigned short *init_seq
[] = {
459 SEQ_PANEL_CONDITION_SET
,
460 SEQ_DISPLAY_CONDITION_SET
,
462 SEQ_ETC_CONDITION_SET
,
467 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
468 ret
= s6e63m0_panel_send_sequence(lcd
, init_seq
[i
]);
476 static int s6e63m0_ldi_enable(struct s6e63m0
*lcd
)
479 const unsigned short *enable_seq
[] = {
484 for (i
= 0; i
< ARRAY_SIZE(enable_seq
); i
++) {
485 ret
= s6e63m0_panel_send_sequence(lcd
, enable_seq
[i
]);
493 static int s6e63m0_ldi_disable(struct s6e63m0
*lcd
)
497 ret
= s6e63m0_panel_send_sequence(lcd
, SEQ_STAND_BY_ON
);
502 static int s6e63m0_power_on(struct s6e63m0
*lcd
)
505 struct lcd_platform_data
*pd
= NULL
;
506 struct backlight_device
*bd
= NULL
;
510 dev_err(lcd
->dev
, "platform data is NULL.\n");
516 dev_err(lcd
->dev
, "backlight device is NULL.\n");
521 dev_err(lcd
->dev
, "power_on is NULL.\n");
524 pd
->power_on(lcd
->ld
, 1);
525 mdelay(pd
->power_on_delay
);
529 dev_err(lcd
->dev
, "reset is NULL.\n");
533 mdelay(pd
->reset_delay
);
536 ret
= s6e63m0_ldi_init(lcd
);
538 dev_err(lcd
->dev
, "failed to initialize ldi.\n");
542 ret
= s6e63m0_ldi_enable(lcd
);
544 dev_err(lcd
->dev
, "failed to enable ldi.\n");
548 /* set brightness to current value after power on or resume. */
549 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
551 dev_err(lcd
->dev
, "lcd gamma setting failed.\n");
558 static int s6e63m0_power_off(struct s6e63m0
*lcd
)
561 struct lcd_platform_data
*pd
= NULL
;
565 dev_err(lcd
->dev
, "platform data is NULL.\n");
569 ret
= s6e63m0_ldi_disable(lcd
);
571 dev_err(lcd
->dev
, "lcd setting failed.\n");
575 mdelay(pd
->power_off_delay
);
578 dev_err(lcd
->dev
, "power_on is NULL.\n");
581 pd
->power_on(lcd
->ld
, 0);
586 static int s6e63m0_power(struct s6e63m0
*lcd
, int power
)
590 if (POWER_IS_ON(power
) && !POWER_IS_ON(lcd
->power
))
591 ret
= s6e63m0_power_on(lcd
);
592 else if (!POWER_IS_ON(power
) && POWER_IS_ON(lcd
->power
))
593 ret
= s6e63m0_power_off(lcd
);
601 static int s6e63m0_set_power(struct lcd_device
*ld
, int power
)
603 struct s6e63m0
*lcd
= lcd_get_data(ld
);
605 if (power
!= FB_BLANK_UNBLANK
&& power
!= FB_BLANK_POWERDOWN
&&
606 power
!= FB_BLANK_NORMAL
) {
607 dev_err(lcd
->dev
, "power value should be 0, 1 or 4.\n");
611 return s6e63m0_power(lcd
, power
);
614 static int s6e63m0_get_power(struct lcd_device
*ld
)
616 struct s6e63m0
*lcd
= lcd_get_data(ld
);
621 static int s6e63m0_get_brightness(struct backlight_device
*bd
)
623 return bd
->props
.brightness
;
626 static int s6e63m0_set_brightness(struct backlight_device
*bd
)
628 int ret
= 0, brightness
= bd
->props
.brightness
;
629 struct s6e63m0
*lcd
= bl_get_data(bd
);
631 if (brightness
< MIN_BRIGHTNESS
||
632 brightness
> bd
->props
.max_brightness
) {
633 dev_err(&bd
->dev
, "lcd brightness should be %d to %d.\n",
634 MIN_BRIGHTNESS
, MAX_BRIGHTNESS
);
638 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
640 dev_err(&bd
->dev
, "lcd brightness setting failed.\n");
647 static struct lcd_ops s6e63m0_lcd_ops
= {
648 .set_power
= s6e63m0_set_power
,
649 .get_power
= s6e63m0_get_power
,
652 static const struct backlight_ops s6e63m0_backlight_ops
= {
653 .get_brightness
= s6e63m0_get_brightness
,
654 .update_status
= s6e63m0_set_brightness
,
657 static ssize_t
s6e63m0_sysfs_show_gamma_mode(struct device
*dev
,
658 struct device_attribute
*attr
, char *buf
)
660 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
663 switch (lcd
->gamma_mode
) {
665 sprintf(temp
, "2.2 mode\n");
669 sprintf(temp
, "1.9 mode\n");
673 sprintf(temp
, "1.7 mode\n");
677 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7)n");
684 static ssize_t
s6e63m0_sysfs_store_gamma_mode(struct device
*dev
,
685 struct device_attribute
*attr
,
686 const char *buf
, size_t len
)
688 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
689 struct backlight_device
*bd
= NULL
;
692 rc
= strict_strtoul(buf
, 0, (unsigned long *)&lcd
->gamma_mode
);
698 brightness
= bd
->props
.brightness
;
700 switch (lcd
->gamma_mode
) {
702 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
705 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_19_table
[brightness
]);
708 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_17_table
[brightness
]);
711 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7\n");
712 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
718 static DEVICE_ATTR(gamma_mode
, 0644,
719 s6e63m0_sysfs_show_gamma_mode
, s6e63m0_sysfs_store_gamma_mode
);
721 static ssize_t
s6e63m0_sysfs_show_gamma_table(struct device
*dev
,
722 struct device_attribute
*attr
, char *buf
)
724 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
727 sprintf(temp
, "%d\n", lcd
->gamma_table_count
);
732 static DEVICE_ATTR(gamma_table
, 0444,
733 s6e63m0_sysfs_show_gamma_table
, NULL
);
735 static int __devinit
s6e63m0_probe(struct spi_device
*spi
)
738 struct s6e63m0
*lcd
= NULL
;
739 struct lcd_device
*ld
= NULL
;
740 struct backlight_device
*bd
= NULL
;
741 struct backlight_properties props
;
743 lcd
= kzalloc(sizeof(struct s6e63m0
), GFP_KERNEL
);
747 /* s6e63m0 lcd panel uses 3-wire 9bits SPI Mode. */
748 spi
->bits_per_word
= 9;
750 ret
= spi_setup(spi
);
752 dev_err(&spi
->dev
, "spi setup failed.\n");
757 lcd
->dev
= &spi
->dev
;
759 lcd
->lcd_pd
= (struct lcd_platform_data
*)spi
->dev
.platform_data
;
761 dev_err(&spi
->dev
, "platform data is NULL.\n");
765 ld
= lcd_device_register("s6e63m0", &spi
->dev
, lcd
, &s6e63m0_lcd_ops
);
773 memset(&props
, 0, sizeof(struct backlight_properties
));
774 props
.type
= BACKLIGHT_RAW
;
775 props
.max_brightness
= MAX_BRIGHTNESS
;
777 bd
= backlight_device_register("s6e63m0bl-bl", &spi
->dev
, lcd
,
778 &s6e63m0_backlight_ops
, &props
);
781 goto out_lcd_unregister
;
784 bd
->props
.brightness
= MAX_BRIGHTNESS
;
788 * it gets gamma table count available so it gets user
791 lcd
->gamma_table_count
=
792 sizeof(gamma_table
) / (MAX_GAMMA_LEVEL
* sizeof(int));
794 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_mode
);
796 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
798 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_table
);
800 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
803 * if lcd panel was on from bootloader like u-boot then
806 if (!lcd
->lcd_pd
->lcd_enabled
) {
808 * if lcd panel was off from bootloader then
809 * current lcd status is powerdown and then
810 * it enables lcd panel.
812 lcd
->power
= FB_BLANK_POWERDOWN
;
814 s6e63m0_power(lcd
, FB_BLANK_UNBLANK
);
816 lcd
->power
= FB_BLANK_UNBLANK
;
818 dev_set_drvdata(&spi
->dev
, lcd
);
820 dev_info(&spi
->dev
, "s6e63m0 panel driver has been probed.\n");
825 lcd_device_unregister(ld
);
831 static int __devexit
s6e63m0_remove(struct spi_device
*spi
)
833 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
835 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
836 device_remove_file(&spi
->dev
, &dev_attr_gamma_table
);
837 device_remove_file(&spi
->dev
, &dev_attr_gamma_mode
);
838 backlight_device_unregister(lcd
->bd
);
839 lcd_device_unregister(lcd
->ld
);
845 #if defined(CONFIG_PM)
846 static unsigned int before_power
;
848 static int s6e63m0_suspend(struct spi_device
*spi
, pm_message_t mesg
)
851 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
853 dev_dbg(&spi
->dev
, "lcd->power = %d\n", lcd
->power
);
855 before_power
= lcd
->power
;
858 * when lcd panel is suspend, lcd panel becomes off
859 * regardless of status.
861 ret
= s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
866 static int s6e63m0_resume(struct spi_device
*spi
)
869 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
872 * after suspended, if lcd panel status is FB_BLANK_UNBLANK
873 * (at that time, before_power is FB_BLANK_UNBLANK) then
874 * it changes that status to FB_BLANK_POWERDOWN to get lcd on.
876 if (before_power
== FB_BLANK_UNBLANK
)
877 lcd
->power
= FB_BLANK_POWERDOWN
;
879 dev_dbg(&spi
->dev
, "before_power = %d\n", before_power
);
881 ret
= s6e63m0_power(lcd
, before_power
);
886 #define s6e63m0_suspend NULL
887 #define s6e63m0_resume NULL
890 /* Power down all displays on reboot, poweroff or halt. */
891 static void s6e63m0_shutdown(struct spi_device
*spi
)
893 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
895 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
898 static struct spi_driver s6e63m0_driver
= {
901 .bus
= &spi_bus_type
,
902 .owner
= THIS_MODULE
,
904 .probe
= s6e63m0_probe
,
905 .remove
= __devexit_p(s6e63m0_remove
),
906 .shutdown
= s6e63m0_shutdown
,
907 .suspend
= s6e63m0_suspend
,
908 .resume
= s6e63m0_resume
,
911 static int __init
s6e63m0_init(void)
913 return spi_register_driver(&s6e63m0_driver
);
916 static void __exit
s6e63m0_exit(void)
918 spi_unregister_driver(&s6e63m0_driver
);
921 module_init(s6e63m0_init
);
922 module_exit(s6e63m0_exit
);
924 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
925 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
926 MODULE_LICENSE("GPL");