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 = strict_strtoul(buf, 0, (unsigned long *)&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 = kzalloc(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);
774 memset(&props, 0, sizeof(struct backlight_properties));
775 props.type = BACKLIGHT_RAW;
776 props.max_brightness = MAX_BRIGHTNESS;
778 bd = backlight_device_register("s6e63m0bl-bl", &spi->dev, lcd,
779 &s6e63m0_backlight_ops, &props);
782 goto out_lcd_unregister;
785 bd->props.brightness = MAX_BRIGHTNESS;
789 * it gets gamma table count available so it gets user
792 lcd->gamma_table_count =
793 sizeof(gamma_table) / (MAX_GAMMA_LEVEL * sizeof(int));
795 ret = device_create_file(&(spi->dev), &dev_attr_gamma_mode);
797 dev_err(&(spi->dev), "failed to add sysfs entries\n");
799 ret = device_create_file(&(spi->dev), &dev_attr_gamma_table);
801 dev_err(&(spi->dev), "failed to add sysfs entries\n");
804 * if lcd panel was on from bootloader like u-boot then
807 if (!lcd->lcd_pd->lcd_enabled) {
809 * if lcd panel was off from bootloader then
810 * current lcd status is powerdown and then
811 * it enables lcd panel.
813 lcd->power = FB_BLANK_POWERDOWN;
815 s6e63m0_power(lcd, FB_BLANK_UNBLANK);
817 lcd->power = FB_BLANK_UNBLANK;
819 dev_set_drvdata(&spi->dev, lcd);
821 dev_info(&spi->dev, "s6e63m0 panel driver has been probed.\n");
826 lcd_device_unregister(ld);
832 static int __devexit s6e63m0_remove(struct spi_device *spi)
834 struct s6e63m0 *lcd = dev_get_drvdata(&spi->dev);
836 s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
837 device_remove_file(&spi->dev, &dev_attr_gamma_table);
838 device_remove_file(&spi->dev, &dev_attr_gamma_mode);
839 backlight_device_unregister(lcd->bd);
840 lcd_device_unregister(lcd->ld);
846 #if defined(CONFIG_PM)
847 static unsigned int before_power;
849 static int s6e63m0_suspend(struct spi_device *spi, pm_message_t mesg)
852 struct s6e63m0 *lcd = dev_get_drvdata(&spi->dev);
854 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
856 before_power = lcd->power;
859 * when lcd panel is suspend, lcd panel becomes off
860 * regardless of status.
862 ret = s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
867 static int s6e63m0_resume(struct spi_device *spi)
870 struct s6e63m0 *lcd = dev_get_drvdata(&spi->dev);
873 * after suspended, if lcd panel status is FB_BLANK_UNBLANK
874 * (at that time, before_power is FB_BLANK_UNBLANK) then
875 * it changes that status to FB_BLANK_POWERDOWN to get lcd on.
877 if (before_power == FB_BLANK_UNBLANK)
878 lcd->power = FB_BLANK_POWERDOWN;
880 dev_dbg(&spi->dev, "before_power = %d\n", before_power);
882 ret = s6e63m0_power(lcd, before_power);
887 #define s6e63m0_suspend NULL
888 #define s6e63m0_resume NULL
891 /* Power down all displays on reboot, poweroff or halt. */
892 static void s6e63m0_shutdown(struct spi_device *spi)
894 struct s6e63m0 *lcd = dev_get_drvdata(&spi->dev);
896 s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
899 static struct spi_driver s6e63m0_driver = {
902 .bus = &spi_bus_type,
903 .owner = THIS_MODULE,
905 .probe = s6e63m0_probe,
906 .remove = __devexit_p(s6e63m0_remove),
907 .shutdown = s6e63m0_shutdown,
908 .suspend = s6e63m0_suspend,
909 .resume = s6e63m0_resume,
912 static int __init s6e63m0_init(void)
914 return spi_register_driver(&s6e63m0_driver);
917 static void __exit s6e63m0_exit(void)
919 spi_unregister_driver(&s6e63m0_driver);
922 module_init(s6e63m0_init);
923 module_exit(s6e63m0_exit);
925 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
926 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
927 MODULE_LICENSE("GPL");