2 * Samsung Laptop driver
4 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009,2011 Novell Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/pci.h>
19 #include <linux/backlight.h>
20 #include <linux/leds.h>
22 #include <linux/dmi.h>
23 #include <linux/platform_device.h>
24 #include <linux/rfkill.h>
25 #include <linux/acpi.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
28 #include <linux/ctype.h>
29 #include <linux/efi.h>
30 #if (defined CONFIG_ACPI_VIDEO || defined CONFIG_ACPI_VIDEO_MODULE)
31 #include <acpi/video.h>
35 * This driver is needed because a number of Samsung laptops do not hook
36 * their control settings through ACPI. So we have to poke around in the
37 * BIOS to do things like brightness values, and "special" key controls.
41 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
42 * be reserved by the BIOS (which really doesn't make much sense), we tell
43 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
45 #define MAX_BRIGHT 0x07
48 #define SABI_IFACE_MAIN 0x00
49 #define SABI_IFACE_SUB 0x02
50 #define SABI_IFACE_COMPLETE 0x04
51 #define SABI_IFACE_DATA 0x05
53 #define WL_STATUS_WLAN 0x0
54 #define WL_STATUS_BT 0x2
56 /* Structure get/set data using sabi */
69 struct sabi_header_offsets
{
78 struct sabi_commands
{
80 * Brightness is 0 - 8, as described above.
81 * Value 0 is for the BIOS to use
88 * 0x00 - wireless is off
89 * 0x01 - wireless is on
93 * TODO, verify 3G is correct, that doesn't seem right...
95 u16 get_wireless_button
;
96 u16 set_wireless_button
;
98 /* 0 is off, 1 is on */
103 * 0x80 or 0x00 - no action
104 * 0x81 - recovery key pressed
106 u16 get_recovery_mode
;
107 u16 set_recovery_mode
;
110 * on seclinux: 0 is low, 1 is high,
111 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
113 u16 get_performance_level
;
114 u16 set_performance_level
;
116 /* 0x80 is off, 0x81 is on */
117 u16 get_battery_life_extender
;
118 u16 set_battery_life_extender
;
120 /* 0x80 is off, 0x81 is on */
124 /* the first byte is for bluetooth and the third one is for wlan */
125 u16 get_wireless_status
;
126 u16 set_wireless_status
;
128 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
132 * Tell the BIOS that Linux is running on this machine.
133 * 81 is on, 80 is off
138 struct sabi_performance_level
{
145 const char *test_string
;
147 const struct sabi_header_offsets header_offsets
;
148 const struct sabi_commands commands
;
149 const struct sabi_performance_level performance_levels
[4];
154 static const struct sabi_config sabi_configs
[] = {
156 /* I don't know if it is really 2, but it it is
157 * less than 3 anyway */
160 .test_string
= "SECLINUX",
162 .main_function
= 0x4c49,
170 .data_segment
= 0x07,
174 .get_brightness
= 0x00,
175 .set_brightness
= 0x01,
177 .get_wireless_button
= 0x02,
178 .set_wireless_button
= 0x03,
180 .get_backlight
= 0x04,
181 .set_backlight
= 0x05,
183 .get_recovery_mode
= 0x06,
184 .set_recovery_mode
= 0x07,
186 .get_performance_level
= 0x08,
187 .set_performance_level
= 0x09,
189 .get_battery_life_extender
= 0xFFFF,
190 .set_battery_life_extender
= 0xFFFF,
192 .get_usb_charge
= 0xFFFF,
193 .set_usb_charge
= 0xFFFF,
195 .get_wireless_status
= 0xFFFF,
196 .set_wireless_status
= 0xFFFF,
198 .kbd_backlight
= 0xFFFF,
203 .performance_levels
= {
220 .test_string
= "SwSmi@",
222 .main_function
= 0x5843,
230 .data_segment
= 0x07,
234 .get_brightness
= 0x10,
235 .set_brightness
= 0x11,
237 .get_wireless_button
= 0x12,
238 .set_wireless_button
= 0x13,
240 .get_backlight
= 0x2d,
241 .set_backlight
= 0x2e,
243 .get_recovery_mode
= 0xff,
244 .set_recovery_mode
= 0xff,
246 .get_performance_level
= 0x31,
247 .set_performance_level
= 0x32,
249 .get_battery_life_extender
= 0x65,
250 .set_battery_life_extender
= 0x66,
252 .get_usb_charge
= 0x67,
253 .set_usb_charge
= 0x68,
255 .get_wireless_status
= 0x69,
256 .set_wireless_status
= 0x6a,
258 .kbd_backlight
= 0x78,
263 .performance_levels
= {
285 * samsung-laptop/ - debugfs root directory
286 * f0000_segment - dump f0000 segment
287 * command - current command
288 * data - current data
289 * d0, d1, d2, d3 - data fields
290 * call - call SABI using command and data
292 * This allow to call arbitrary sabi commands wihout
293 * modifying the driver at all.
294 * For example, setting the keyboard backlight brightness to 5
296 * echo 0x78 > command
304 struct samsung_laptop_debug
{
306 struct sabi_data data
;
309 struct debugfs_blob_wrapper f0000_wrapper
;
310 struct debugfs_blob_wrapper data_wrapper
;
311 struct debugfs_blob_wrapper sdiag_wrapper
;
314 struct samsung_laptop
;
316 struct samsung_rfkill
{
317 struct samsung_laptop
*samsung
;
318 struct rfkill
*rfkill
;
319 enum rfkill_type type
;
322 struct samsung_laptop
{
323 const struct sabi_config
*config
;
326 void __iomem
*sabi_iface
;
327 void __iomem
*f0000_segment
;
329 struct mutex sabi_mutex
;
331 struct platform_device
*platform_device
;
332 struct backlight_device
*backlight_device
;
334 struct samsung_rfkill wlan
;
335 struct samsung_rfkill bluetooth
;
337 struct led_classdev kbd_led
;
339 struct workqueue_struct
*led_workqueue
;
340 struct work_struct kbd_led_work
;
342 struct samsung_laptop_debug debug
;
343 struct samsung_quirks
*quirks
;
345 bool handle_backlight
;
346 bool has_stepping_quirk
;
351 struct samsung_quirks
{
352 bool broken_acpi_video
;
355 static struct samsung_quirks samsung_unknown
= {};
357 static struct samsung_quirks samsung_broken_acpi_video
= {
358 .broken_acpi_video
= true,
362 module_param(force
, bool, 0);
363 MODULE_PARM_DESC(force
,
364 "Disable the DMI check and forces the driver to be loaded");
367 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
368 MODULE_PARM_DESC(debug
, "Debug enabled or not");
370 static int sabi_command(struct samsung_laptop
*samsung
, u16 command
,
371 struct sabi_data
*in
,
372 struct sabi_data
*out
)
374 const struct sabi_config
*config
= samsung
->config
;
376 u16 port
= readw(samsung
->sabi
+ config
->header_offsets
.port
);
377 u8 complete
, iface_data
;
379 mutex_lock(&samsung
->sabi_mutex
);
383 pr_info("SABI command:0x%04x "
384 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
385 command
, in
->d0
, in
->d1
, in
->d2
, in
->d3
);
387 pr_info("SABI command:0x%04x", command
);
390 /* enable memory to be able to write to it */
391 outb(readb(samsung
->sabi
+ config
->header_offsets
.en_mem
), port
);
393 /* write out the command */
394 writew(config
->main_function
, samsung
->sabi_iface
+ SABI_IFACE_MAIN
);
395 writew(command
, samsung
->sabi_iface
+ SABI_IFACE_SUB
);
396 writeb(0, samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
398 writel(in
->d0
, samsung
->sabi_iface
+ SABI_IFACE_DATA
);
399 writel(in
->d1
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
400 writew(in
->d2
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 8);
401 writeb(in
->d3
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 10);
403 outb(readb(samsung
->sabi
+ config
->header_offsets
.iface_func
), port
);
405 /* write protect memory to make it safe */
406 outb(readb(samsung
->sabi
+ config
->header_offsets
.re_mem
), port
);
408 /* see if the command actually succeeded */
409 complete
= readb(samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
410 iface_data
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
412 /* iface_data = 0xFF happens when a command is not known
413 * so we only add a warning in debug mode since we will
414 * probably issue some unknown command at startup to find
415 * out which features are supported */
416 if (complete
!= 0xaa || (iface_data
== 0xff && debug
))
417 pr_warn("SABI command 0x%04x failed with"
418 " completion flag 0x%02x and interface data 0x%02x",
419 command
, complete
, iface_data
);
421 if (complete
!= 0xaa || iface_data
== 0xff) {
427 out
->d0
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
428 out
->d1
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
429 out
->d2
= readw(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 2);
430 out
->d3
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 1);
434 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
435 out
->d0
, out
->d1
, out
->d2
, out
->d3
);
439 mutex_unlock(&samsung
->sabi_mutex
);
443 /* simple wrappers usable with most commands */
444 static int sabi_set_commandb(struct samsung_laptop
*samsung
,
445 u16 command
, u8 data
)
447 struct sabi_data in
= { { { .d0
= 0, .d1
= 0, .d2
= 0, .d3
= 0 } } };
450 return sabi_command(samsung
, command
, &in
, NULL
);
453 static int read_brightness(struct samsung_laptop
*samsung
)
455 const struct sabi_config
*config
= samsung
->config
;
456 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
457 struct sabi_data sretval
;
458 int user_brightness
= 0;
461 retval
= sabi_command(samsung
, commands
->get_brightness
,
466 user_brightness
= sretval
.data
[0];
467 if (user_brightness
> config
->min_brightness
)
468 user_brightness
-= config
->min_brightness
;
472 return user_brightness
;
475 static void set_brightness(struct samsung_laptop
*samsung
, u8 user_brightness
)
477 const struct sabi_config
*config
= samsung
->config
;
478 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
479 u8 user_level
= user_brightness
+ config
->min_brightness
;
481 if (samsung
->has_stepping_quirk
&& user_level
!= 0) {
483 * short circuit if the specified level is what's already set
484 * to prevent the screen from flickering needlessly
486 if (user_brightness
== read_brightness(samsung
))
489 sabi_set_commandb(samsung
, commands
->set_brightness
, 0);
492 sabi_set_commandb(samsung
, commands
->set_brightness
, user_level
);
495 static int get_brightness(struct backlight_device
*bd
)
497 struct samsung_laptop
*samsung
= bl_get_data(bd
);
499 return read_brightness(samsung
);
502 static void check_for_stepping_quirk(struct samsung_laptop
*samsung
)
506 int orig_level
= read_brightness(samsung
);
509 * Some laptops exhibit the strange behaviour of stepping toward
510 * (rather than setting) the brightness except when changing to/from
511 * brightness level 0. This behaviour is checked for here and worked
512 * around in set_brightness.
516 set_brightness(samsung
, 1);
518 initial_level
= read_brightness(samsung
);
520 if (initial_level
<= 2)
521 check_level
= initial_level
+ 2;
523 check_level
= initial_level
- 2;
525 samsung
->has_stepping_quirk
= false;
526 set_brightness(samsung
, check_level
);
528 if (read_brightness(samsung
) != check_level
) {
529 samsung
->has_stepping_quirk
= true;
530 pr_info("enabled workaround for brightness stepping quirk\n");
533 set_brightness(samsung
, orig_level
);
536 static int update_status(struct backlight_device
*bd
)
538 struct samsung_laptop
*samsung
= bl_get_data(bd
);
539 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
541 set_brightness(samsung
, bd
->props
.brightness
);
543 if (bd
->props
.power
== FB_BLANK_UNBLANK
)
544 sabi_set_commandb(samsung
, commands
->set_backlight
, 1);
546 sabi_set_commandb(samsung
, commands
->set_backlight
, 0);
551 static const struct backlight_ops backlight_ops
= {
552 .get_brightness
= get_brightness
,
553 .update_status
= update_status
,
556 static int seclinux_rfkill_set(void *data
, bool blocked
)
558 struct samsung_rfkill
*srfkill
= data
;
559 struct samsung_laptop
*samsung
= srfkill
->samsung
;
560 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
562 return sabi_set_commandb(samsung
, commands
->set_wireless_button
,
566 static struct rfkill_ops seclinux_rfkill_ops
= {
567 .set_block
= seclinux_rfkill_set
,
570 static int swsmi_wireless_status(struct samsung_laptop
*samsung
,
571 struct sabi_data
*data
)
573 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
575 return sabi_command(samsung
, commands
->get_wireless_status
,
579 static int swsmi_rfkill_set(void *priv
, bool blocked
)
581 struct samsung_rfkill
*srfkill
= priv
;
582 struct samsung_laptop
*samsung
= srfkill
->samsung
;
583 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
584 struct sabi_data data
;
587 ret
= swsmi_wireless_status(samsung
, &data
);
591 /* Don't set the state for non-present devices */
592 for (i
= 0; i
< 4; i
++)
593 if (data
.data
[i
] == 0x02)
596 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
597 data
.data
[WL_STATUS_WLAN
] = !blocked
;
598 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
599 data
.data
[WL_STATUS_BT
] = !blocked
;
601 return sabi_command(samsung
, commands
->set_wireless_status
,
605 static void swsmi_rfkill_query(struct rfkill
*rfkill
, void *priv
)
607 struct samsung_rfkill
*srfkill
= priv
;
608 struct samsung_laptop
*samsung
= srfkill
->samsung
;
609 struct sabi_data data
;
612 ret
= swsmi_wireless_status(samsung
, &data
);
616 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
617 ret
= data
.data
[WL_STATUS_WLAN
];
618 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
619 ret
= data
.data
[WL_STATUS_BT
];
623 rfkill_set_sw_state(rfkill
, !ret
);
626 static struct rfkill_ops swsmi_rfkill_ops
= {
627 .set_block
= swsmi_rfkill_set
,
628 .query
= swsmi_rfkill_query
,
631 static ssize_t
get_performance_level(struct device
*dev
,
632 struct device_attribute
*attr
, char *buf
)
634 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
635 const struct sabi_config
*config
= samsung
->config
;
636 const struct sabi_commands
*commands
= &config
->commands
;
637 struct sabi_data sretval
;
642 retval
= sabi_command(samsung
, commands
->get_performance_level
,
647 /* The logic is backwards, yeah, lots of fun... */
648 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
649 if (sretval
.data
[0] == config
->performance_levels
[i
].value
)
650 return sprintf(buf
, "%s\n", config
->performance_levels
[i
].name
);
652 return sprintf(buf
, "%s\n", "unknown");
655 static ssize_t
set_performance_level(struct device
*dev
,
656 struct device_attribute
*attr
, const char *buf
,
659 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
660 const struct sabi_config
*config
= samsung
->config
;
661 const struct sabi_commands
*commands
= &config
->commands
;
667 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
668 const struct sabi_performance_level
*level
=
669 &config
->performance_levels
[i
];
670 if (!strncasecmp(level
->name
, buf
, strlen(level
->name
))) {
671 sabi_set_commandb(samsung
,
672 commands
->set_performance_level
,
678 if (!config
->performance_levels
[i
].name
)
684 static DEVICE_ATTR(performance_level
, S_IWUSR
| S_IRUGO
,
685 get_performance_level
, set_performance_level
);
687 static int read_battery_life_extender(struct samsung_laptop
*samsung
)
689 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
690 struct sabi_data data
;
693 if (commands
->get_battery_life_extender
== 0xFFFF)
696 memset(&data
, 0, sizeof(data
));
698 retval
= sabi_command(samsung
, commands
->get_battery_life_extender
,
704 if (data
.data
[0] != 0 && data
.data
[0] != 1)
710 static int write_battery_life_extender(struct samsung_laptop
*samsung
,
713 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
714 struct sabi_data data
;
716 memset(&data
, 0, sizeof(data
));
717 data
.data
[0] = 0x80 | enabled
;
718 return sabi_command(samsung
, commands
->set_battery_life_extender
,
722 static ssize_t
get_battery_life_extender(struct device
*dev
,
723 struct device_attribute
*attr
,
726 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
729 ret
= read_battery_life_extender(samsung
);
733 return sprintf(buf
, "%d\n", ret
);
736 static ssize_t
set_battery_life_extender(struct device
*dev
,
737 struct device_attribute
*attr
,
738 const char *buf
, size_t count
)
740 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
743 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
746 ret
= write_battery_life_extender(samsung
, !!value
);
753 static DEVICE_ATTR(battery_life_extender
, S_IWUSR
| S_IRUGO
,
754 get_battery_life_extender
, set_battery_life_extender
);
756 static int read_usb_charge(struct samsung_laptop
*samsung
)
758 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
759 struct sabi_data data
;
762 if (commands
->get_usb_charge
== 0xFFFF)
765 memset(&data
, 0, sizeof(data
));
767 retval
= sabi_command(samsung
, commands
->get_usb_charge
,
773 if (data
.data
[0] != 0 && data
.data
[0] != 1)
779 static int write_usb_charge(struct samsung_laptop
*samsung
,
782 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
783 struct sabi_data data
;
785 memset(&data
, 0, sizeof(data
));
786 data
.data
[0] = 0x80 | enabled
;
787 return sabi_command(samsung
, commands
->set_usb_charge
,
791 static ssize_t
get_usb_charge(struct device
*dev
,
792 struct device_attribute
*attr
,
795 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
798 ret
= read_usb_charge(samsung
);
802 return sprintf(buf
, "%d\n", ret
);
805 static ssize_t
set_usb_charge(struct device
*dev
,
806 struct device_attribute
*attr
,
807 const char *buf
, size_t count
)
809 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
812 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
815 ret
= write_usb_charge(samsung
, !!value
);
822 static DEVICE_ATTR(usb_charge
, S_IWUSR
| S_IRUGO
,
823 get_usb_charge
, set_usb_charge
);
825 static struct attribute
*platform_attributes
[] = {
826 &dev_attr_performance_level
.attr
,
827 &dev_attr_battery_life_extender
.attr
,
828 &dev_attr_usb_charge
.attr
,
832 static int find_signature(void __iomem
*memcheck
, const char *testStr
)
837 for (loca
= 0; loca
< 0xffff; loca
++) {
838 char temp
= readb(memcheck
+ loca
);
840 if (temp
== testStr
[i
]) {
841 if (i
== strlen(testStr
)-1)
851 static void samsung_rfkill_exit(struct samsung_laptop
*samsung
)
853 if (samsung
->wlan
.rfkill
) {
854 rfkill_unregister(samsung
->wlan
.rfkill
);
855 rfkill_destroy(samsung
->wlan
.rfkill
);
856 samsung
->wlan
.rfkill
= NULL
;
858 if (samsung
->bluetooth
.rfkill
) {
859 rfkill_unregister(samsung
->bluetooth
.rfkill
);
860 rfkill_destroy(samsung
->bluetooth
.rfkill
);
861 samsung
->bluetooth
.rfkill
= NULL
;
865 static int samsung_new_rfkill(struct samsung_laptop
*samsung
,
866 struct samsung_rfkill
*arfkill
,
867 const char *name
, enum rfkill_type type
,
868 const struct rfkill_ops
*ops
,
871 struct rfkill
**rfkill
= &arfkill
->rfkill
;
874 arfkill
->type
= type
;
875 arfkill
->samsung
= samsung
;
877 *rfkill
= rfkill_alloc(name
, &samsung
->platform_device
->dev
,
884 rfkill_init_sw_state(*rfkill
, blocked
);
886 ret
= rfkill_register(*rfkill
);
888 rfkill_destroy(*rfkill
);
895 static int __init
samsung_rfkill_init_seclinux(struct samsung_laptop
*samsung
)
897 return samsung_new_rfkill(samsung
, &samsung
->wlan
, "samsung-wlan",
898 RFKILL_TYPE_WLAN
, &seclinux_rfkill_ops
, -1);
901 static int __init
samsung_rfkill_init_swsmi(struct samsung_laptop
*samsung
)
903 struct sabi_data data
;
906 ret
= swsmi_wireless_status(samsung
, &data
);
908 /* Some swsmi laptops use the old seclinux way to control
909 * wireless devices */
911 ret
= samsung_rfkill_init_seclinux(samsung
);
915 /* 0x02 seems to mean that the device is no present/available */
917 if (data
.data
[WL_STATUS_WLAN
] != 0x02)
918 ret
= samsung_new_rfkill(samsung
, &samsung
->wlan
,
922 !data
.data
[WL_STATUS_WLAN
]);
926 if (data
.data
[WL_STATUS_BT
] != 0x02)
927 ret
= samsung_new_rfkill(samsung
, &samsung
->bluetooth
,
929 RFKILL_TYPE_BLUETOOTH
,
931 !data
.data
[WL_STATUS_BT
]);
937 samsung_rfkill_exit(samsung
);
942 static int __init
samsung_rfkill_init(struct samsung_laptop
*samsung
)
944 if (samsung
->config
->sabi_version
== 2)
945 return samsung_rfkill_init_seclinux(samsung
);
946 if (samsung
->config
->sabi_version
== 3)
947 return samsung_rfkill_init_swsmi(samsung
);
951 static int kbd_backlight_enable(struct samsung_laptop
*samsung
)
953 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
954 struct sabi_data data
;
957 if (commands
->kbd_backlight
== 0xFFFF)
960 memset(&data
, 0, sizeof(data
));
962 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
968 if (data
.d0
!= 0xccdd)
973 static int kbd_backlight_read(struct samsung_laptop
*samsung
)
975 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
976 struct sabi_data data
;
979 memset(&data
, 0, sizeof(data
));
981 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
990 static int kbd_backlight_write(struct samsung_laptop
*samsung
, int brightness
)
992 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
993 struct sabi_data data
;
995 memset(&data
, 0, sizeof(data
));
996 data
.d0
= 0x82 | ((brightness
& 0xFF) << 8);
997 return sabi_command(samsung
, commands
->kbd_backlight
,
1001 static void kbd_led_update(struct work_struct
*work
)
1003 struct samsung_laptop
*samsung
;
1005 samsung
= container_of(work
, struct samsung_laptop
, kbd_led_work
);
1006 kbd_backlight_write(samsung
, samsung
->kbd_led_wk
);
1009 static void kbd_led_set(struct led_classdev
*led_cdev
,
1010 enum led_brightness value
)
1012 struct samsung_laptop
*samsung
;
1014 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1016 if (value
> samsung
->kbd_led
.max_brightness
)
1017 value
= samsung
->kbd_led
.max_brightness
;
1021 samsung
->kbd_led_wk
= value
;
1022 queue_work(samsung
->led_workqueue
, &samsung
->kbd_led_work
);
1025 static enum led_brightness
kbd_led_get(struct led_classdev
*led_cdev
)
1027 struct samsung_laptop
*samsung
;
1029 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1030 return kbd_backlight_read(samsung
);
1033 static void samsung_leds_exit(struct samsung_laptop
*samsung
)
1035 if (!IS_ERR_OR_NULL(samsung
->kbd_led
.dev
))
1036 led_classdev_unregister(&samsung
->kbd_led
);
1037 if (samsung
->led_workqueue
)
1038 destroy_workqueue(samsung
->led_workqueue
);
1041 static int __init
samsung_leds_init(struct samsung_laptop
*samsung
)
1045 samsung
->led_workqueue
= create_singlethread_workqueue("led_workqueue");
1046 if (!samsung
->led_workqueue
)
1049 if (kbd_backlight_enable(samsung
) >= 0) {
1050 INIT_WORK(&samsung
->kbd_led_work
, kbd_led_update
);
1052 samsung
->kbd_led
.name
= "samsung::kbd_backlight";
1053 samsung
->kbd_led
.brightness_set
= kbd_led_set
;
1054 samsung
->kbd_led
.brightness_get
= kbd_led_get
;
1055 samsung
->kbd_led
.max_brightness
= 8;
1057 ret
= led_classdev_register(&samsung
->platform_device
->dev
,
1062 samsung_leds_exit(samsung
);
1067 static void samsung_backlight_exit(struct samsung_laptop
*samsung
)
1069 if (samsung
->backlight_device
) {
1070 backlight_device_unregister(samsung
->backlight_device
);
1071 samsung
->backlight_device
= NULL
;
1075 static int __init
samsung_backlight_init(struct samsung_laptop
*samsung
)
1077 struct backlight_device
*bd
;
1078 struct backlight_properties props
;
1080 if (!samsung
->handle_backlight
)
1083 memset(&props
, 0, sizeof(struct backlight_properties
));
1084 props
.type
= BACKLIGHT_PLATFORM
;
1085 props
.max_brightness
= samsung
->config
->max_brightness
-
1086 samsung
->config
->min_brightness
;
1088 bd
= backlight_device_register("samsung",
1089 &samsung
->platform_device
->dev
,
1090 samsung
, &backlight_ops
,
1095 samsung
->backlight_device
= bd
;
1096 samsung
->backlight_device
->props
.brightness
= read_brightness(samsung
);
1097 samsung
->backlight_device
->props
.power
= FB_BLANK_UNBLANK
;
1098 backlight_update_status(samsung
->backlight_device
);
1103 static umode_t
samsung_sysfs_is_visible(struct kobject
*kobj
,
1104 struct attribute
*attr
, int idx
)
1106 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1107 struct platform_device
*pdev
= to_platform_device(dev
);
1108 struct samsung_laptop
*samsung
= platform_get_drvdata(pdev
);
1111 if (attr
== &dev_attr_performance_level
.attr
)
1112 ok
= !!samsung
->config
->performance_levels
[0].name
;
1113 if (attr
== &dev_attr_battery_life_extender
.attr
)
1114 ok
= !!(read_battery_life_extender(samsung
) >= 0);
1115 if (attr
== &dev_attr_usb_charge
.attr
)
1116 ok
= !!(read_usb_charge(samsung
) >= 0);
1118 return ok
? attr
->mode
: 0;
1121 static struct attribute_group platform_attribute_group
= {
1122 .is_visible
= samsung_sysfs_is_visible
,
1123 .attrs
= platform_attributes
1126 static void samsung_sysfs_exit(struct samsung_laptop
*samsung
)
1128 struct platform_device
*device
= samsung
->platform_device
;
1130 sysfs_remove_group(&device
->dev
.kobj
, &platform_attribute_group
);
1133 static int __init
samsung_sysfs_init(struct samsung_laptop
*samsung
)
1135 struct platform_device
*device
= samsung
->platform_device
;
1137 return sysfs_create_group(&device
->dev
.kobj
, &platform_attribute_group
);
1141 static int show_call(struct seq_file
*m
, void *data
)
1143 struct samsung_laptop
*samsung
= m
->private;
1144 struct sabi_data
*sdata
= &samsung
->debug
.data
;
1147 seq_printf(m
, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1148 samsung
->debug
.command
,
1149 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1151 ret
= sabi_command(samsung
, samsung
->debug
.command
, sdata
, sdata
);
1154 seq_printf(m
, "SABI command 0x%04x failed\n",
1155 samsung
->debug
.command
);
1159 seq_printf(m
, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1160 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1164 static int samsung_debugfs_open(struct inode
*inode
, struct file
*file
)
1166 return single_open(file
, show_call
, inode
->i_private
);
1169 static const struct file_operations samsung_laptop_call_io_ops
= {
1170 .owner
= THIS_MODULE
,
1171 .open
= samsung_debugfs_open
,
1173 .llseek
= seq_lseek
,
1174 .release
= single_release
,
1177 static void samsung_debugfs_exit(struct samsung_laptop
*samsung
)
1179 debugfs_remove_recursive(samsung
->debug
.root
);
1182 static int samsung_debugfs_init(struct samsung_laptop
*samsung
)
1184 struct dentry
*dent
;
1186 samsung
->debug
.root
= debugfs_create_dir("samsung-laptop", NULL
);
1187 if (!samsung
->debug
.root
) {
1188 pr_err("failed to create debugfs directory");
1192 samsung
->debug
.f0000_wrapper
.data
= samsung
->f0000_segment
;
1193 samsung
->debug
.f0000_wrapper
.size
= 0xffff;
1195 samsung
->debug
.data_wrapper
.data
= &samsung
->debug
.data
;
1196 samsung
->debug
.data_wrapper
.size
= sizeof(samsung
->debug
.data
);
1198 samsung
->debug
.sdiag_wrapper
.data
= samsung
->sdiag
;
1199 samsung
->debug
.sdiag_wrapper
.size
= strlen(samsung
->sdiag
);
1201 dent
= debugfs_create_u16("command", S_IRUGO
| S_IWUSR
,
1202 samsung
->debug
.root
, &samsung
->debug
.command
);
1206 dent
= debugfs_create_u32("d0", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1207 &samsung
->debug
.data
.d0
);
1211 dent
= debugfs_create_u32("d1", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1212 &samsung
->debug
.data
.d1
);
1216 dent
= debugfs_create_u16("d2", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1217 &samsung
->debug
.data
.d2
);
1221 dent
= debugfs_create_u8("d3", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1222 &samsung
->debug
.data
.d3
);
1226 dent
= debugfs_create_blob("data", S_IRUGO
| S_IWUSR
,
1227 samsung
->debug
.root
,
1228 &samsung
->debug
.data_wrapper
);
1232 dent
= debugfs_create_blob("f0000_segment", S_IRUSR
| S_IWUSR
,
1233 samsung
->debug
.root
,
1234 &samsung
->debug
.f0000_wrapper
);
1238 dent
= debugfs_create_file("call", S_IFREG
| S_IRUGO
,
1239 samsung
->debug
.root
, samsung
,
1240 &samsung_laptop_call_io_ops
);
1244 dent
= debugfs_create_blob("sdiag", S_IRUGO
| S_IWUSR
,
1245 samsung
->debug
.root
,
1246 &samsung
->debug
.sdiag_wrapper
);
1253 samsung_debugfs_exit(samsung
);
1257 static void samsung_sabi_exit(struct samsung_laptop
*samsung
)
1259 const struct sabi_config
*config
= samsung
->config
;
1261 /* Turn off "Linux" mode in the BIOS */
1262 if (config
&& config
->commands
.set_linux
!= 0xff)
1263 sabi_set_commandb(samsung
, config
->commands
.set_linux
, 0x80);
1265 if (samsung
->sabi_iface
) {
1266 iounmap(samsung
->sabi_iface
);
1267 samsung
->sabi_iface
= NULL
;
1269 if (samsung
->f0000_segment
) {
1270 iounmap(samsung
->f0000_segment
);
1271 samsung
->f0000_segment
= NULL
;
1274 samsung
->config
= NULL
;
1277 static __init
void samsung_sabi_infos(struct samsung_laptop
*samsung
, int loca
,
1278 unsigned int ifaceP
)
1280 const struct sabi_config
*config
= samsung
->config
;
1282 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
1283 loca
+ 0xf0000 - 6);
1285 printk(KERN_DEBUG
"SABI header:\n");
1286 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
1287 readw(samsung
->sabi
+ config
->header_offsets
.port
));
1288 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
1289 readb(samsung
->sabi
+ config
->header_offsets
.iface_func
));
1290 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
1291 readb(samsung
->sabi
+ config
->header_offsets
.en_mem
));
1292 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
1293 readb(samsung
->sabi
+ config
->header_offsets
.re_mem
));
1294 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
1295 readw(samsung
->sabi
+ config
->header_offsets
.data_offset
));
1296 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
1297 readw(samsung
->sabi
+ config
->header_offsets
.data_segment
));
1299 printk(KERN_DEBUG
" SABI pointer = 0x%08x\n", ifaceP
);
1302 static void __init
samsung_sabi_diag(struct samsung_laptop
*samsung
)
1304 int loca
= find_signature(samsung
->f0000_segment
, "SDiaG@");
1311 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1313 * Product name: 90X3A
1314 * BIOS Version: 07HL
1317 for (i
= 0; loca
< 0xffff && i
< sizeof(samsung
->sdiag
) - 1; loca
++) {
1318 char temp
= readb(samsung
->f0000_segment
+ loca
);
1320 if (isalnum(temp
) || temp
== '/' || temp
== '-')
1321 samsung
->sdiag
[i
++] = temp
;
1326 if (debug
&& samsung
->sdiag
[0])
1327 pr_info("sdiag: %s", samsung
->sdiag
);
1330 static int __init
samsung_sabi_init(struct samsung_laptop
*samsung
)
1332 const struct sabi_config
*config
= NULL
;
1333 const struct sabi_commands
*commands
;
1334 unsigned int ifaceP
;
1339 samsung
->f0000_segment
= ioremap_nocache(0xf0000, 0xffff);
1340 if (!samsung
->f0000_segment
) {
1342 pr_err("Can't map the segment at 0xf0000\n");
1347 samsung_sabi_diag(samsung
);
1349 /* Try to find one of the signatures in memory to find the header */
1350 for (i
= 0; sabi_configs
[i
].test_string
!= 0; ++i
) {
1351 samsung
->config
= &sabi_configs
[i
];
1352 loca
= find_signature(samsung
->f0000_segment
,
1353 samsung
->config
->test_string
);
1358 if (loca
== 0xffff) {
1360 pr_err("This computer does not support SABI\n");
1365 config
= samsung
->config
;
1366 commands
= &config
->commands
;
1368 /* point to the SMI port Number */
1370 samsung
->sabi
= (samsung
->f0000_segment
+ loca
);
1372 /* Get a pointer to the SABI Interface */
1373 ifaceP
= (readw(samsung
->sabi
+ config
->header_offsets
.data_segment
) & 0x0ffff) << 4;
1374 ifaceP
+= readw(samsung
->sabi
+ config
->header_offsets
.data_offset
) & 0x0ffff;
1377 samsung_sabi_infos(samsung
, loca
, ifaceP
);
1379 samsung
->sabi_iface
= ioremap_nocache(ifaceP
, 16);
1380 if (!samsung
->sabi_iface
) {
1381 pr_err("Can't remap %x\n", ifaceP
);
1386 /* Turn on "Linux" mode in the BIOS */
1387 if (commands
->set_linux
!= 0xff) {
1388 int retval
= sabi_set_commandb(samsung
,
1389 commands
->set_linux
, 0x81);
1391 pr_warn("Linux mode was not set!\n");
1397 /* Check for stepping quirk */
1398 if (samsung
->handle_backlight
)
1399 check_for_stepping_quirk(samsung
);
1401 pr_info("detected SABI interface: %s\n",
1402 samsung
->config
->test_string
);
1406 samsung_sabi_exit(samsung
);
1411 static void samsung_platform_exit(struct samsung_laptop
*samsung
)
1413 if (samsung
->platform_device
) {
1414 platform_device_unregister(samsung
->platform_device
);
1415 samsung
->platform_device
= NULL
;
1419 static int __init
samsung_platform_init(struct samsung_laptop
*samsung
)
1421 struct platform_device
*pdev
;
1423 pdev
= platform_device_register_simple("samsung", -1, NULL
, 0);
1425 return PTR_ERR(pdev
);
1427 samsung
->platform_device
= pdev
;
1428 platform_set_drvdata(samsung
->platform_device
, samsung
);
1432 static struct samsung_quirks
*quirks
;
1434 static int __init
samsung_dmi_matched(const struct dmi_system_id
*d
)
1436 quirks
= d
->driver_data
;
1440 static struct dmi_system_id __initdata samsung_dmi_table
[] = {
1443 DMI_MATCH(DMI_SYS_VENDOR
,
1444 "SAMSUNG ELECTRONICS CO., LTD."),
1445 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"), /* Portable */
1450 DMI_MATCH(DMI_SYS_VENDOR
,
1451 "SAMSUNG ELECTRONICS CO., LTD."),
1452 DMI_MATCH(DMI_CHASSIS_TYPE
, "9"), /* Laptop */
1457 DMI_MATCH(DMI_SYS_VENDOR
,
1458 "SAMSUNG ELECTRONICS CO., LTD."),
1459 DMI_MATCH(DMI_CHASSIS_TYPE
, "10"), /* Notebook */
1464 DMI_MATCH(DMI_SYS_VENDOR
,
1465 "SAMSUNG ELECTRONICS CO., LTD."),
1466 DMI_MATCH(DMI_CHASSIS_TYPE
, "14"), /* Sub-Notebook */
1469 /* Specific DMI ids for laptop with quirks */
1471 .callback
= samsung_dmi_matched
,
1474 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1475 DMI_MATCH(DMI_PRODUCT_NAME
, "N150P"),
1476 DMI_MATCH(DMI_BOARD_NAME
, "N150P"),
1478 .driver_data
= &samsung_broken_acpi_video
,
1481 .callback
= samsung_dmi_matched
,
1482 .ident
= "N145P/N250P/N260P",
1484 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1485 DMI_MATCH(DMI_PRODUCT_NAME
, "N145P/N250P/N260P"),
1486 DMI_MATCH(DMI_BOARD_NAME
, "N145P/N250P/N260P"),
1488 .driver_data
= &samsung_broken_acpi_video
,
1491 .callback
= samsung_dmi_matched
,
1492 .ident
= "N150/N210/N220",
1494 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1495 DMI_MATCH(DMI_PRODUCT_NAME
, "N150/N210/N220"),
1496 DMI_MATCH(DMI_BOARD_NAME
, "N150/N210/N220"),
1498 .driver_data
= &samsung_broken_acpi_video
,
1501 .callback
= samsung_dmi_matched
,
1502 .ident
= "NF110/NF210/NF310",
1504 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1505 DMI_MATCH(DMI_PRODUCT_NAME
, "NF110/NF210/NF310"),
1506 DMI_MATCH(DMI_BOARD_NAME
, "NF110/NF210/NF310"),
1508 .driver_data
= &samsung_broken_acpi_video
,
1511 .callback
= samsung_dmi_matched
,
1514 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1515 DMI_MATCH(DMI_PRODUCT_NAME
, "N250P"),
1516 DMI_MATCH(DMI_BOARD_NAME
, "N250P"),
1518 .driver_data
= &samsung_broken_acpi_video
,
1522 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
1524 static struct platform_device
*samsung_platform_device
;
1526 static int __init
samsung_init(void)
1528 struct samsung_laptop
*samsung
;
1531 if (efi_enabled(EFI_BOOT
))
1534 quirks
= &samsung_unknown
;
1535 if (!force
&& !dmi_check_system(samsung_dmi_table
))
1538 samsung
= kzalloc(sizeof(*samsung
), GFP_KERNEL
);
1542 mutex_init(&samsung
->sabi_mutex
);
1543 samsung
->handle_backlight
= true;
1544 samsung
->quirks
= quirks
;
1547 #if (defined CONFIG_ACPI_VIDEO || defined CONFIG_ACPI_VIDEO_MODULE)
1548 /* Don't handle backlight here if the acpi video already handle it */
1549 if (acpi_video_backlight_support()) {
1550 if (samsung
->quirks
->broken_acpi_video
) {
1551 pr_info("Disabling ACPI video driver\n");
1552 acpi_video_unregister();
1554 samsung
->handle_backlight
= false;
1559 ret
= samsung_platform_init(samsung
);
1561 goto error_platform
;
1563 ret
= samsung_sabi_init(samsung
);
1568 /* Only log that if we are really on a sabi platform */
1569 if (acpi_video_backlight_support() &&
1570 !samsung
->quirks
->broken_acpi_video
)
1571 pr_info("Backlight controlled by ACPI video driver\n");
1574 ret
= samsung_sysfs_init(samsung
);
1578 ret
= samsung_backlight_init(samsung
);
1580 goto error_backlight
;
1582 ret
= samsung_rfkill_init(samsung
);
1586 ret
= samsung_leds_init(samsung
);
1590 ret
= samsung_debugfs_init(samsung
);
1594 samsung_platform_device
= samsung
->platform_device
;
1598 samsung_leds_exit(samsung
);
1600 samsung_rfkill_exit(samsung
);
1602 samsung_backlight_exit(samsung
);
1604 samsung_sysfs_exit(samsung
);
1606 samsung_sabi_exit(samsung
);
1608 samsung_platform_exit(samsung
);
1614 static void __exit
samsung_exit(void)
1616 struct samsung_laptop
*samsung
;
1618 samsung
= platform_get_drvdata(samsung_platform_device
);
1620 samsung_debugfs_exit(samsung
);
1621 samsung_leds_exit(samsung
);
1622 samsung_rfkill_exit(samsung
);
1623 samsung_backlight_exit(samsung
);
1624 samsung_sysfs_exit(samsung
);
1625 samsung_sabi_exit(samsung
);
1626 samsung_platform_exit(samsung
);
1629 samsung_platform_device
= NULL
;
1632 module_init(samsung_init
);
1633 module_exit(samsung_exit
);
1635 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1636 MODULE_DESCRIPTION("Samsung Backlight driver");
1637 MODULE_LICENSE("GPL");