1717#include <linux/delay.h>
1818#include <linux/pci.h>
1919#include <linux/backlight.h>
20+ #include <linux/leds.h>
2021#include <linux/fb.h>
2122#include <linux/dmi.h>
2223#include <linux/platform_device.h>
@@ -112,6 +113,9 @@ struct sabi_commands {
112113 u16 get_usb_charge ;
113114 u16 set_usb_charge ;
114115
116+ /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
117+ u16 kbd_backlight ;
118+
115119 /*
116120 * Tell the BIOS that Linux is running on this machine.
117121 * 81 is on, 80 is off
@@ -171,6 +175,8 @@ static const struct sabi_config sabi_configs[] = {
171175 .get_usb_charge = 0xFFFF ,
172176 .set_usb_charge = 0xFFFF ,
173177
178+ .kbd_backlight = 0xFFFF ,
179+
174180 .set_linux = 0x0a ,
175181 },
176182
@@ -224,6 +230,8 @@ static const struct sabi_config sabi_configs[] = {
224230 .get_usb_charge = 0x67 ,
225231 .set_usb_charge = 0x68 ,
226232
233+ .kbd_backlight = 0x78 ,
234+
227235 .set_linux = 0xff ,
228236 },
229237
@@ -290,6 +298,11 @@ struct samsung_laptop {
290298 struct backlight_device * backlight_device ;
291299 struct rfkill * rfk ;
292300
301+ struct led_classdev kbd_led ;
302+ int kbd_led_wk ;
303+ struct workqueue_struct * led_workqueue ;
304+ struct work_struct kbd_led_work ;
305+
293306 struct samsung_laptop_debug debug ;
294307
295308 bool handle_backlight ;
@@ -757,6 +770,122 @@ static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
757770 return 0 ;
758771}
759772
773+ static int kbd_backlight_enable (struct samsung_laptop * samsung )
774+ {
775+ const struct sabi_commands * commands = & samsung -> config -> commands ;
776+ struct sabi_data data ;
777+ int retval ;
778+
779+ if (commands -> kbd_backlight == 0xFFFF )
780+ return - ENODEV ;
781+
782+ memset (& data , 0 , sizeof (data ));
783+ data .d0 = 0xaabb ;
784+ retval = sabi_command (samsung , commands -> kbd_backlight ,
785+ & data , & data );
786+
787+ if (retval )
788+ return retval ;
789+
790+ if (data .d0 != 0xccdd )
791+ return - ENODEV ;
792+ return 0 ;
793+ }
794+
795+ static int kbd_backlight_read (struct samsung_laptop * samsung )
796+ {
797+ const struct sabi_commands * commands = & samsung -> config -> commands ;
798+ struct sabi_data data ;
799+ int retval ;
800+
801+ memset (& data , 0 , sizeof (data ));
802+ data .data [0 ] = 0x81 ;
803+ retval = sabi_command (samsung , commands -> kbd_backlight ,
804+ & data , & data );
805+
806+ if (retval )
807+ return retval ;
808+
809+ return data .data [0 ];
810+ }
811+
812+ static int kbd_backlight_write (struct samsung_laptop * samsung , int brightness )
813+ {
814+ const struct sabi_commands * commands = & samsung -> config -> commands ;
815+ struct sabi_data data ;
816+
817+ memset (& data , 0 , sizeof (data ));
818+ data .d0 = 0x82 | ((brightness & 0xFF ) << 8 );
819+ return sabi_command (samsung , commands -> kbd_backlight ,
820+ & data , NULL );
821+ }
822+
823+ static void kbd_led_update (struct work_struct * work )
824+ {
825+ struct samsung_laptop * samsung ;
826+
827+ samsung = container_of (work , struct samsung_laptop , kbd_led_work );
828+ kbd_backlight_write (samsung , samsung -> kbd_led_wk );
829+ }
830+
831+ static void kbd_led_set (struct led_classdev * led_cdev ,
832+ enum led_brightness value )
833+ {
834+ struct samsung_laptop * samsung ;
835+
836+ samsung = container_of (led_cdev , struct samsung_laptop , kbd_led );
837+
838+ if (value > samsung -> kbd_led .max_brightness )
839+ value = samsung -> kbd_led .max_brightness ;
840+ else if (value < 0 )
841+ value = 0 ;
842+
843+ samsung -> kbd_led_wk = value ;
844+ queue_work (samsung -> led_workqueue , & samsung -> kbd_led_work );
845+ }
846+
847+ static enum led_brightness kbd_led_get (struct led_classdev * led_cdev )
848+ {
849+ struct samsung_laptop * samsung ;
850+
851+ samsung = container_of (led_cdev , struct samsung_laptop , kbd_led );
852+ return kbd_backlight_read (samsung );
853+ }
854+
855+ static void samsung_leds_exit (struct samsung_laptop * samsung )
856+ {
857+ if (!IS_ERR_OR_NULL (samsung -> kbd_led .dev ))
858+ led_classdev_unregister (& samsung -> kbd_led );
859+ if (samsung -> led_workqueue )
860+ destroy_workqueue (samsung -> led_workqueue );
861+ }
862+
863+ static int __init samsung_leds_init (struct samsung_laptop * samsung )
864+ {
865+ int ret = 0 ;
866+
867+ samsung -> led_workqueue = create_singlethread_workqueue ("led_workqueue" );
868+ if (!samsung -> led_workqueue )
869+ return - ENOMEM ;
870+
871+ if (kbd_backlight_enable (samsung ) >= 0 ) {
872+ INIT_WORK (& samsung -> kbd_led_work , kbd_led_update );
873+
874+ samsung -> kbd_led .name = "samsung::kbd_backlight" ;
875+ samsung -> kbd_led .brightness_set = kbd_led_set ;
876+ samsung -> kbd_led .brightness_get = kbd_led_get ;
877+ samsung -> kbd_led .max_brightness = 8 ;
878+
879+ ret = led_classdev_register (& samsung -> platform_device -> dev ,
880+ & samsung -> kbd_led );
881+ }
882+
883+ if (ret )
884+ samsung_leds_exit (samsung );
885+
886+ return ret ;
887+ }
888+
760889static void samsung_backlight_exit (struct samsung_laptop * samsung )
761890{
762891 if (samsung -> backlight_device ) {
@@ -1366,6 +1495,10 @@ static int __init samsung_init(void)
13661495 if (ret )
13671496 goto error_rfkill ;
13681497
1498+ ret = samsung_leds_init (samsung );
1499+ if (ret )
1500+ goto error_leds ;
1501+
13691502 ret = samsung_debugfs_init (samsung );
13701503 if (ret )
13711504 goto error_debugfs ;
@@ -1374,6 +1507,8 @@ static int __init samsung_init(void)
13741507 return ret ;
13751508
13761509error_debugfs :
1510+ samsung_leds_exit (samsung );
1511+ error_leds :
13771512 samsung_rfkill_exit (samsung );
13781513error_rfkill :
13791514 samsung_backlight_exit (samsung );
@@ -1395,6 +1530,7 @@ static void __exit samsung_exit(void)
13951530 samsung = platform_get_drvdata (samsung_platform_device );
13961531
13971532 samsung_debugfs_exit (samsung );
1533+ samsung_leds_exit (samsung );
13981534 samsung_rfkill_exit (samsung );
13991535 samsung_backlight_exit (samsung );
14001536 samsung_sysfs_exit (samsung );
0 commit comments