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>
33 #include <linux/module.h>
35 #include "s6e63m0_gamma.h"
37 #define SLEEPMSEC 0x1000
39 #define DEFMASK 0xFF00
40 #define COMMAND_ONLY 0xFE
41 #define DATA_ONLY 0xFF
43 #define MIN_BRIGHTNESS 0
44 #define MAX_BRIGHTNESS 10
46 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
50 struct spi_device
*spi
;
52 unsigned int current_brightness
;
53 unsigned int gamma_mode
;
54 unsigned int gamma_table_count
;
55 struct lcd_device
*ld
;
56 struct backlight_device
*bd
;
57 struct lcd_platform_data
*lcd_pd
;
60 static const unsigned short SEQ_PANEL_CONDITION_SET
[] = {
79 static const unsigned short SEQ_DISPLAY_CONDITION_SET
[] = {
93 static const unsigned short SEQ_GAMMA_SETTING
[] = {
122 static const unsigned short SEQ_ETC_CONDITION_SET
[] = {
321 static const unsigned short SEQ_ACL_ON
[] = {
328 static const unsigned short SEQ_ACL_OFF
[] = {
335 static const unsigned short SEQ_ELVSS_ON
[] = {
342 static const unsigned short SEQ_ELVSS_OFF
[] = {
349 static const unsigned short SEQ_STAND_BY_OFF
[] = {
355 static const unsigned short SEQ_STAND_BY_ON
[] = {
361 static const unsigned short SEQ_DISPLAY_ON
[] = {
368 static int s6e63m0_spi_write_byte(struct s6e63m0
*lcd
, int addr
, int data
)
371 struct spi_message msg
;
373 struct spi_transfer xfer
= {
378 buf
[0] = (addr
<< 8) | data
;
380 spi_message_init(&msg
);
381 spi_message_add_tail(&xfer
, &msg
);
383 return spi_sync(lcd
->spi
, &msg
);
386 static int s6e63m0_spi_write(struct s6e63m0
*lcd
, unsigned char address
,
387 unsigned char command
)
391 if (address
!= DATA_ONLY
)
392 ret
= s6e63m0_spi_write_byte(lcd
, 0x0, address
);
393 if (command
!= COMMAND_ONLY
)
394 ret
= s6e63m0_spi_write_byte(lcd
, 0x1, command
);
399 static int s6e63m0_panel_send_sequence(struct s6e63m0
*lcd
,
400 const unsigned short *wbuf
)
404 while ((wbuf
[i
] & DEFMASK
) != ENDDEF
) {
405 if ((wbuf
[i
] & DEFMASK
) != SLEEPMSEC
) {
406 ret
= s6e63m0_spi_write(lcd
, wbuf
[i
], wbuf
[i
+1]);
410 udelay(wbuf
[i
+1]*1000);
417 static int _s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, const unsigned int *gamma
)
422 /* disable gamma table updating. */
423 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x00);
425 dev_err(lcd
->dev
, "failed to disable gamma table updating.\n");
429 for (i
= 0 ; i
< GAMMA_TABLE_COUNT
; i
++) {
430 ret
= s6e63m0_spi_write(lcd
, DATA_ONLY
, gamma
[i
]);
432 dev_err(lcd
->dev
, "failed to set gamma table.\n");
437 /* update gamma table. */
438 ret
= s6e63m0_spi_write(lcd
, 0xfa, 0x01);
440 dev_err(lcd
->dev
, "failed to update gamma table.\n");
446 static int s6e63m0_gamma_ctl(struct s6e63m0
*lcd
, int gamma
)
450 ret
= _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[gamma
]);
456 static int s6e63m0_ldi_init(struct s6e63m0
*lcd
)
459 const unsigned short *init_seq
[] = {
460 SEQ_PANEL_CONDITION_SET
,
461 SEQ_DISPLAY_CONDITION_SET
,
463 SEQ_ETC_CONDITION_SET
,
468 for (i
= 0; i
< ARRAY_SIZE(init_seq
); i
++) {
469 ret
= s6e63m0_panel_send_sequence(lcd
, init_seq
[i
]);
477 static int s6e63m0_ldi_enable(struct s6e63m0
*lcd
)
480 const unsigned short *enable_seq
[] = {
485 for (i
= 0; i
< ARRAY_SIZE(enable_seq
); i
++) {
486 ret
= s6e63m0_panel_send_sequence(lcd
, enable_seq
[i
]);
494 static int s6e63m0_ldi_disable(struct s6e63m0
*lcd
)
498 ret
= s6e63m0_panel_send_sequence(lcd
, SEQ_STAND_BY_ON
);
503 static int s6e63m0_power_on(struct s6e63m0
*lcd
)
506 struct lcd_platform_data
*pd
= NULL
;
507 struct backlight_device
*bd
= NULL
;
511 dev_err(lcd
->dev
, "platform data is NULL.\n");
517 dev_err(lcd
->dev
, "backlight device is NULL.\n");
522 dev_err(lcd
->dev
, "power_on is NULL.\n");
525 pd
->power_on(lcd
->ld
, 1);
526 mdelay(pd
->power_on_delay
);
530 dev_err(lcd
->dev
, "reset is NULL.\n");
534 mdelay(pd
->reset_delay
);
537 ret
= s6e63m0_ldi_init(lcd
);
539 dev_err(lcd
->dev
, "failed to initialize ldi.\n");
543 ret
= s6e63m0_ldi_enable(lcd
);
545 dev_err(lcd
->dev
, "failed to enable ldi.\n");
549 /* set brightness to current value after power on or resume. */
550 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
552 dev_err(lcd
->dev
, "lcd gamma setting failed.\n");
559 static int s6e63m0_power_off(struct s6e63m0
*lcd
)
562 struct lcd_platform_data
*pd
= NULL
;
566 dev_err(lcd
->dev
, "platform data is NULL.\n");
570 ret
= s6e63m0_ldi_disable(lcd
);
572 dev_err(lcd
->dev
, "lcd setting failed.\n");
576 mdelay(pd
->power_off_delay
);
579 dev_err(lcd
->dev
, "power_on is NULL.\n");
582 pd
->power_on(lcd
->ld
, 0);
587 static int s6e63m0_power(struct s6e63m0
*lcd
, int power
)
591 if (POWER_IS_ON(power
) && !POWER_IS_ON(lcd
->power
))
592 ret
= s6e63m0_power_on(lcd
);
593 else if (!POWER_IS_ON(power
) && POWER_IS_ON(lcd
->power
))
594 ret
= s6e63m0_power_off(lcd
);
602 static int s6e63m0_set_power(struct lcd_device
*ld
, int power
)
604 struct s6e63m0
*lcd
= lcd_get_data(ld
);
606 if (power
!= FB_BLANK_UNBLANK
&& power
!= FB_BLANK_POWERDOWN
&&
607 power
!= FB_BLANK_NORMAL
) {
608 dev_err(lcd
->dev
, "power value should be 0, 1 or 4.\n");
612 return s6e63m0_power(lcd
, power
);
615 static int s6e63m0_get_power(struct lcd_device
*ld
)
617 struct s6e63m0
*lcd
= lcd_get_data(ld
);
622 static int s6e63m0_get_brightness(struct backlight_device
*bd
)
624 return bd
->props
.brightness
;
627 static int s6e63m0_set_brightness(struct backlight_device
*bd
)
629 int ret
= 0, brightness
= bd
->props
.brightness
;
630 struct s6e63m0
*lcd
= bl_get_data(bd
);
632 if (brightness
< MIN_BRIGHTNESS
||
633 brightness
> bd
->props
.max_brightness
) {
634 dev_err(&bd
->dev
, "lcd brightness should be %d to %d.\n",
635 MIN_BRIGHTNESS
, MAX_BRIGHTNESS
);
639 ret
= s6e63m0_gamma_ctl(lcd
, bd
->props
.brightness
);
641 dev_err(&bd
->dev
, "lcd brightness setting failed.\n");
648 static struct lcd_ops s6e63m0_lcd_ops
= {
649 .set_power
= s6e63m0_set_power
,
650 .get_power
= s6e63m0_get_power
,
653 static const struct backlight_ops s6e63m0_backlight_ops
= {
654 .get_brightness
= s6e63m0_get_brightness
,
655 .update_status
= s6e63m0_set_brightness
,
658 static ssize_t
s6e63m0_sysfs_show_gamma_mode(struct device
*dev
,
659 struct device_attribute
*attr
, char *buf
)
661 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
664 switch (lcd
->gamma_mode
) {
666 sprintf(temp
, "2.2 mode\n");
670 sprintf(temp
, "1.9 mode\n");
674 sprintf(temp
, "1.7 mode\n");
678 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7)n");
685 static ssize_t
s6e63m0_sysfs_store_gamma_mode(struct device
*dev
,
686 struct device_attribute
*attr
,
687 const char *buf
, size_t len
)
689 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
690 struct backlight_device
*bd
= NULL
;
693 rc
= kstrtouint(buf
, 0, &lcd
->gamma_mode
);
699 brightness
= bd
->props
.brightness
;
701 switch (lcd
->gamma_mode
) {
703 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
706 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_19_table
[brightness
]);
709 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_17_table
[brightness
]);
712 dev_info(dev
, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7\n");
713 _s6e63m0_gamma_ctl(lcd
, gamma_table
.gamma_22_table
[brightness
]);
719 static DEVICE_ATTR(gamma_mode
, 0644,
720 s6e63m0_sysfs_show_gamma_mode
, s6e63m0_sysfs_store_gamma_mode
);
722 static ssize_t
s6e63m0_sysfs_show_gamma_table(struct device
*dev
,
723 struct device_attribute
*attr
, char *buf
)
725 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
728 sprintf(temp
, "%d\n", lcd
->gamma_table_count
);
733 static DEVICE_ATTR(gamma_table
, 0444,
734 s6e63m0_sysfs_show_gamma_table
, NULL
);
736 static int __devinit
s6e63m0_probe(struct spi_device
*spi
)
739 struct s6e63m0
*lcd
= NULL
;
740 struct lcd_device
*ld
= NULL
;
741 struct backlight_device
*bd
= NULL
;
742 struct backlight_properties props
;
744 lcd
= devm_kzalloc(&spi
->dev
, sizeof(struct s6e63m0
), GFP_KERNEL
);
748 /* s6e63m0 lcd panel uses 3-wire 9bits SPI Mode. */
749 spi
->bits_per_word
= 9;
751 ret
= spi_setup(spi
);
753 dev_err(&spi
->dev
, "spi setup failed.\n");
758 lcd
->dev
= &spi
->dev
;
760 lcd
->lcd_pd
= (struct lcd_platform_data
*)spi
->dev
.platform_data
;
762 dev_err(&spi
->dev
, "platform data is NULL.\n");
766 ld
= lcd_device_register("s6e63m0", &spi
->dev
, lcd
, &s6e63m0_lcd_ops
);
772 memset(&props
, 0, sizeof(struct backlight_properties
));
773 props
.type
= BACKLIGHT_RAW
;
774 props
.max_brightness
= MAX_BRIGHTNESS
;
776 bd
= backlight_device_register("s6e63m0bl-bl", &spi
->dev
, lcd
,
777 &s6e63m0_backlight_ops
, &props
);
780 goto out_lcd_unregister
;
783 bd
->props
.brightness
= MAX_BRIGHTNESS
;
787 * it gets gamma table count available so it gets user
790 lcd
->gamma_table_count
=
791 sizeof(gamma_table
) / (MAX_GAMMA_LEVEL
* sizeof(int));
793 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_mode
);
795 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
797 ret
= device_create_file(&(spi
->dev
), &dev_attr_gamma_table
);
799 dev_err(&(spi
->dev
), "failed to add sysfs entries\n");
802 * if lcd panel was on from bootloader like u-boot then
805 if (!lcd
->lcd_pd
->lcd_enabled
) {
807 * if lcd panel was off from bootloader then
808 * current lcd status is powerdown and then
809 * it enables lcd panel.
811 lcd
->power
= FB_BLANK_POWERDOWN
;
813 s6e63m0_power(lcd
, FB_BLANK_UNBLANK
);
815 lcd
->power
= FB_BLANK_UNBLANK
;
817 dev_set_drvdata(&spi
->dev
, lcd
);
819 dev_info(&spi
->dev
, "s6e63m0 panel driver has been probed.\n");
824 lcd_device_unregister(ld
);
828 static int __devexit
s6e63m0_remove(struct spi_device
*spi
)
830 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
832 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
833 device_remove_file(&spi
->dev
, &dev_attr_gamma_table
);
834 device_remove_file(&spi
->dev
, &dev_attr_gamma_mode
);
835 backlight_device_unregister(lcd
->bd
);
836 lcd_device_unregister(lcd
->ld
);
841 #if defined(CONFIG_PM)
842 static unsigned int before_power
;
844 static int s6e63m0_suspend(struct spi_device
*spi
, pm_message_t mesg
)
847 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
849 dev_dbg(&spi
->dev
, "lcd->power = %d\n", lcd
->power
);
851 before_power
= lcd
->power
;
854 * when lcd panel is suspend, lcd panel becomes off
855 * regardless of status.
857 ret
= s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
862 static int s6e63m0_resume(struct spi_device
*spi
)
865 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
868 * after suspended, if lcd panel status is FB_BLANK_UNBLANK
869 * (at that time, before_power is FB_BLANK_UNBLANK) then
870 * it changes that status to FB_BLANK_POWERDOWN to get lcd on.
872 if (before_power
== FB_BLANK_UNBLANK
)
873 lcd
->power
= FB_BLANK_POWERDOWN
;
875 dev_dbg(&spi
->dev
, "before_power = %d\n", before_power
);
877 ret
= s6e63m0_power(lcd
, before_power
);
882 #define s6e63m0_suspend NULL
883 #define s6e63m0_resume NULL
886 /* Power down all displays on reboot, poweroff or halt. */
887 static void s6e63m0_shutdown(struct spi_device
*spi
)
889 struct s6e63m0
*lcd
= dev_get_drvdata(&spi
->dev
);
891 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
894 static struct spi_driver s6e63m0_driver
= {
897 .owner
= THIS_MODULE
,
899 .probe
= s6e63m0_probe
,
900 .remove
= __devexit_p(s6e63m0_remove
),
901 .shutdown
= s6e63m0_shutdown
,
902 .suspend
= s6e63m0_suspend
,
903 .resume
= s6e63m0_resume
,
906 module_spi_driver(s6e63m0_driver
);
908 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
909 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
910 MODULE_LICENSE("GPL");