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
= dev_get_platdata(&spi
->dev
);
740 dev_err(&spi
->dev
, "platform data is NULL.\n");
744 ld
= devm_lcd_device_register(&spi
->dev
, "s6e63m0", &spi
->dev
, lcd
,
751 memset(&props
, 0, sizeof(struct backlight_properties
));
752 props
.type
= BACKLIGHT_RAW
;
753 props
.max_brightness
= MAX_BRIGHTNESS
;
755 bd
= devm_backlight_device_register(&spi
->dev
, "s6e63m0bl-bl",
756 &spi
->dev
, lcd
, &s6e63m0_backlight_ops
,
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 static int s6e63m0_remove(struct spi_device
*spi
)
805 struct s6e63m0
*lcd
= spi_get_drvdata(spi
);
807 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
808 device_remove_file(&spi
->dev
, &dev_attr_gamma_table
);
809 device_remove_file(&spi
->dev
, &dev_attr_gamma_mode
);
814 #ifdef CONFIG_PM_SLEEP
815 static int s6e63m0_suspend(struct device
*dev
)
817 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
819 dev_dbg(dev
, "lcd->power = %d\n", lcd
->power
);
822 * when lcd panel is suspend, lcd panel becomes off
823 * regardless of status.
825 return s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
828 static int s6e63m0_resume(struct device
*dev
)
830 struct s6e63m0
*lcd
= dev_get_drvdata(dev
);
832 lcd
->power
= FB_BLANK_POWERDOWN
;
834 return s6e63m0_power(lcd
, FB_BLANK_UNBLANK
);
838 static SIMPLE_DEV_PM_OPS(s6e63m0_pm_ops
, s6e63m0_suspend
, s6e63m0_resume
);
840 /* Power down all displays on reboot, poweroff or halt. */
841 static void s6e63m0_shutdown(struct spi_device
*spi
)
843 struct s6e63m0
*lcd
= spi_get_drvdata(spi
);
845 s6e63m0_power(lcd
, FB_BLANK_POWERDOWN
);
848 static struct spi_driver s6e63m0_driver
= {
851 .owner
= THIS_MODULE
,
852 .pm
= &s6e63m0_pm_ops
,
854 .probe
= s6e63m0_probe
,
855 .remove
= s6e63m0_remove
,
856 .shutdown
= s6e63m0_shutdown
,
859 module_spi_driver(s6e63m0_driver
);
861 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
862 MODULE_DESCRIPTION("S6E63M0 LCD Driver");
863 MODULE_LICENSE("GPL");