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");
511 pd->power_on(lcd->ld, 1);
512 msleep(pd->power_on_delay);
516 dev_err(lcd->dev, "reset is NULL.\n");
520 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_get_brightness(struct backlight_device *bd)
602 return bd->props.brightness;
605 static int s6e63m0_set_brightness(struct backlight_device *bd)
607 int ret = 0, brightness = bd->props.brightness;
608 struct s6e63m0 *lcd = bl_get_data(bd);
610 if (brightness < MIN_BRIGHTNESS ||
611 brightness > bd->props.max_brightness) {
612 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
613 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
617 ret = s6e63m0_gamma_ctl(lcd, bd->props.brightness);
619 dev_err(&bd->dev, "lcd brightness setting failed.\n");
626 static struct lcd_ops s6e63m0_lcd_ops = {
627 .set_power = s6e63m0_set_power,
628 .get_power = s6e63m0_get_power,
631 static const struct backlight_ops s6e63m0_backlight_ops = {
632 .get_brightness = s6e63m0_get_brightness,
633 .update_status = s6e63m0_set_brightness,
636 static ssize_t s6e63m0_sysfs_show_gamma_mode(struct device *dev,
637 struct device_attribute *attr, char *buf)
639 struct s6e63m0 *lcd = dev_get_drvdata(dev);
642 switch (lcd->gamma_mode) {
644 sprintf(temp, "2.2 mode\n");
648 sprintf(temp, "1.9 mode\n");
652 sprintf(temp, "1.7 mode\n");
656 dev_info(dev, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7)n");
663 static ssize_t s6e63m0_sysfs_store_gamma_mode(struct device *dev,
664 struct device_attribute *attr,
665 const char *buf, size_t len)
667 struct s6e63m0 *lcd = dev_get_drvdata(dev);
668 struct backlight_device *bd = NULL;
671 rc = kstrtouint(buf, 0, &lcd->gamma_mode);
677 brightness = bd->props.brightness;
679 switch (lcd->gamma_mode) {
681 _s6e63m0_gamma_ctl(lcd, gamma_table.gamma_22_table[brightness]);
684 _s6e63m0_gamma_ctl(lcd, gamma_table.gamma_19_table[brightness]);
687 _s6e63m0_gamma_ctl(lcd, gamma_table.gamma_17_table[brightness]);
690 dev_info(dev, "gamma mode could be 0:2.2, 1:1.9 or 2:1.7\n");
691 _s6e63m0_gamma_ctl(lcd, gamma_table.gamma_22_table[brightness]);
697 static DEVICE_ATTR(gamma_mode, 0644,
698 s6e63m0_sysfs_show_gamma_mode, s6e63m0_sysfs_store_gamma_mode);
700 static ssize_t s6e63m0_sysfs_show_gamma_table(struct device *dev,
701 struct device_attribute *attr, char *buf)
703 struct s6e63m0 *lcd = dev_get_drvdata(dev);
706 sprintf(temp, "%d\n", lcd->gamma_table_count);
711 static DEVICE_ATTR(gamma_table, 0444,
712 s6e63m0_sysfs_show_gamma_table, NULL);
714 static int s6e63m0_probe(struct spi_device *spi)
717 struct s6e63m0 *lcd = NULL;
718 struct lcd_device *ld = NULL;
719 struct backlight_device *bd = NULL;
720 struct backlight_properties props;
722 lcd = devm_kzalloc(&spi->dev, sizeof(struct s6e63m0), GFP_KERNEL);
726 /* s6e63m0 lcd panel uses 3-wire 9bits SPI Mode. */
727 spi->bits_per_word = 9;
729 ret = spi_setup(spi);
731 dev_err(&spi->dev, "spi setup failed.\n");
736 lcd->dev = &spi->dev;
738 lcd->lcd_pd = spi->dev.platform_data;
740 dev_err(&spi->dev, "platform data is NULL.\n");
744 ld = lcd_device_register("s6e63m0", &spi->dev, lcd, &s6e63m0_lcd_ops);
750 memset(&props, 0, sizeof(struct backlight_properties));
751 props.type = BACKLIGHT_RAW;
752 props.max_brightness = MAX_BRIGHTNESS;
754 bd = backlight_device_register("s6e63m0bl-bl", &spi->dev, lcd,
755 &s6e63m0_backlight_ops, &props);
758 goto out_lcd_unregister;
761 bd->props.brightness = MAX_BRIGHTNESS;
765 * it gets gamma table count available so it gets user
768 lcd->gamma_table_count =
769 sizeof(gamma_table) / (MAX_GAMMA_LEVEL * sizeof(int *));
771 ret = device_create_file(&(spi->dev), &dev_attr_gamma_mode);
773 dev_err(&(spi->dev), "failed to add sysfs entries\n");
775 ret = device_create_file(&(spi->dev), &dev_attr_gamma_table);
777 dev_err(&(spi->dev), "failed to add sysfs entries\n");
780 * if lcd panel was on from bootloader like u-boot then
783 if (!lcd->lcd_pd->lcd_enabled) {
785 * if lcd panel was off from bootloader then
786 * current lcd status is powerdown and then
787 * it enables lcd panel.
789 lcd->power = FB_BLANK_POWERDOWN;
791 s6e63m0_power(lcd, FB_BLANK_UNBLANK);
793 lcd->power = FB_BLANK_UNBLANK;
796 spi_set_drvdata(spi, lcd);
798 dev_info(&spi->dev, "s6e63m0 panel driver has been probed.\n");
803 lcd_device_unregister(ld);
807 static int s6e63m0_remove(struct spi_device *spi)
809 struct s6e63m0 *lcd = spi_get_drvdata(spi);
811 s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
812 device_remove_file(&spi->dev, &dev_attr_gamma_table);
813 device_remove_file(&spi->dev, &dev_attr_gamma_mode);
814 backlight_device_unregister(lcd->bd);
815 lcd_device_unregister(lcd->ld);
820 #if defined(CONFIG_PM)
821 static int s6e63m0_suspend(struct spi_device *spi, pm_message_t mesg)
823 struct s6e63m0 *lcd = spi_get_drvdata(spi);
825 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
828 * when lcd panel is suspend, lcd panel becomes off
829 * regardless of status.
831 return s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
834 static int s6e63m0_resume(struct spi_device *spi)
836 struct s6e63m0 *lcd = spi_get_drvdata(spi);
838 lcd->power = FB_BLANK_POWERDOWN;
840 return s6e63m0_power(lcd, FB_BLANK_UNBLANK);
843 #define s6e63m0_suspend NULL
844 #define s6e63m0_resume NULL
847 /* Power down all displays on reboot, poweroff or halt. */
848 static void s6e63m0_shutdown(struct spi_device *spi)
850 struct s6e63m0 *lcd = spi_get_drvdata(spi);
852 s6e63m0_power(lcd, FB_BLANK_POWERDOWN);
855 static struct spi_driver s6e63m0_driver = {
858 .owner = THIS_MODULE,
860 .probe = s6e63m0_probe,
861 .remove = s6e63m0_remove,
862 .shutdown = s6e63m0_shutdown,
863 .suspend = s6e63m0_suspend,
864 .resume = s6e63m0_resume,
867 module_spi_driver(s6e63m0_driver);
869 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
870 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
871 MODULE_LICENSE("GPL");