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 #include <linux/suspend.h>
31 #include <acpi/video.h>
34 * This driver is needed because a number of Samsung laptops do not hook
35 * their control settings through ACPI. So we have to poke around in the
36 * BIOS to do things like brightness values, and "special" key controls.
40 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
41 * be reserved by the BIOS (which really doesn't make much sense), we tell
42 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
44 #define MAX_BRIGHT 0x07
47 #define SABI_IFACE_MAIN 0x00
48 #define SABI_IFACE_SUB 0x02
49 #define SABI_IFACE_COMPLETE 0x04
50 #define SABI_IFACE_DATA 0x05
52 #define WL_STATUS_WLAN 0x0
53 #define WL_STATUS_BT 0x2
55 /* Structure get/set data using sabi */
68 struct sabi_header_offsets {
77 struct sabi_commands {
79 * Brightness is 0 - 8, as described above.
80 * Value 0 is for the BIOS to use
87 * 0x00 - wireless is off
88 * 0x01 - wireless is on
92 * TODO, verify 3G is correct, that doesn't seem right...
94 u16 get_wireless_button;
95 u16 set_wireless_button;
97 /* 0 is off, 1 is on */
102 * 0x80 or 0x00 - no action
103 * 0x81 - recovery key pressed
105 u16 get_recovery_mode;
106 u16 set_recovery_mode;
109 * on seclinux: 0 is low, 1 is high,
110 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
112 u16 get_performance_level;
113 u16 set_performance_level;
115 /* 0x80 is off, 0x81 is on */
116 u16 get_battery_life_extender;
117 u16 set_battery_life_extender;
119 /* 0x80 is off, 0x81 is on */
123 /* the first byte is for bluetooth and the third one is for wlan */
124 u16 get_wireless_status;
125 u16 set_wireless_status;
127 /* 0x80 is off, 0x81 is on */
128 u16 get_lid_handling;
129 u16 set_lid_handling;
131 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
135 * Tell the BIOS that Linux is running on this machine.
136 * 81 is on, 80 is off
141 struct sabi_performance_level {
148 const char *test_string;
150 const struct sabi_header_offsets header_offsets;
151 const struct sabi_commands commands;
152 const struct sabi_performance_level performance_levels[4];
157 static const struct sabi_config sabi_configs[] = {
159 /* I don't know if it is really 2, but it it is
160 * less than 3 anyway */
163 .test_string = "SECLINUX",
165 .main_function = 0x4c49,
173 .data_segment = 0x07,
177 .get_brightness = 0x00,
178 .set_brightness = 0x01,
180 .get_wireless_button = 0x02,
181 .set_wireless_button = 0x03,
183 .get_backlight = 0x04,
184 .set_backlight = 0x05,
186 .get_recovery_mode = 0x06,
187 .set_recovery_mode = 0x07,
189 .get_performance_level = 0x08,
190 .set_performance_level = 0x09,
192 .get_battery_life_extender = 0xFFFF,
193 .set_battery_life_extender = 0xFFFF,
195 .get_usb_charge = 0xFFFF,
196 .set_usb_charge = 0xFFFF,
198 .get_wireless_status = 0xFFFF,
199 .set_wireless_status = 0xFFFF,
201 .get_lid_handling = 0xFFFF,
202 .set_lid_handling = 0xFFFF,
204 .kbd_backlight = 0xFFFF,
209 .performance_levels = {
226 .test_string = "SwSmi@",
228 .main_function = 0x5843,
236 .data_segment = 0x07,
240 .get_brightness = 0x10,
241 .set_brightness = 0x11,
243 .get_wireless_button = 0x12,
244 .set_wireless_button = 0x13,
246 .get_backlight = 0x2d,
247 .set_backlight = 0x2e,
249 .get_recovery_mode = 0xff,
250 .set_recovery_mode = 0xff,
252 .get_performance_level = 0x31,
253 .set_performance_level = 0x32,
255 .get_battery_life_extender = 0x65,
256 .set_battery_life_extender = 0x66,
258 .get_usb_charge = 0x67,
259 .set_usb_charge = 0x68,
261 .get_wireless_status = 0x69,
262 .set_wireless_status = 0x6a,
264 .get_lid_handling = 0x6d,
265 .set_lid_handling = 0x6e,
267 .kbd_backlight = 0x78,
272 .performance_levels = {
294 * samsung-laptop/ - debugfs root directory
295 * f0000_segment - dump f0000 segment
296 * command - current command
297 * data - current data
298 * d0, d1, d2, d3 - data fields
299 * call - call SABI using command and data
301 * This allow to call arbitrary sabi commands wihout
302 * modifying the driver at all.
303 * For example, setting the keyboard backlight brightness to 5
305 * echo 0x78 > command
313 struct samsung_laptop_debug {
315 struct sabi_data data;
318 struct debugfs_blob_wrapper f0000_wrapper;
319 struct debugfs_blob_wrapper data_wrapper;
320 struct debugfs_blob_wrapper sdiag_wrapper;
323 struct samsung_laptop;
325 struct samsung_rfkill {
326 struct samsung_laptop *samsung;
327 struct rfkill *rfkill;
328 enum rfkill_type type;
331 struct samsung_laptop {
332 const struct sabi_config *config;
335 void __iomem *sabi_iface;
336 void __iomem *f0000_segment;
338 struct mutex sabi_mutex;
340 struct platform_device *platform_device;
341 struct backlight_device *backlight_device;
343 struct samsung_rfkill wlan;
344 struct samsung_rfkill bluetooth;
346 struct led_classdev kbd_led;
348 struct workqueue_struct *led_workqueue;
349 struct work_struct kbd_led_work;
351 struct samsung_laptop_debug debug;
352 struct samsung_quirks *quirks;
354 struct notifier_block pm_nb;
356 bool handle_backlight;
357 bool has_stepping_quirk;
362 struct samsung_quirks {
363 bool broken_acpi_video;
364 bool four_kbd_backlight_levels;
365 bool enable_kbd_backlight;
366 bool use_native_backlight;
370 static struct samsung_quirks samsung_unknown = {};
372 static struct samsung_quirks samsung_broken_acpi_video = {
373 .broken_acpi_video = true,
376 static struct samsung_quirks samsung_use_native_backlight = {
377 .use_native_backlight = true,
380 static struct samsung_quirks samsung_np740u3e = {
381 .four_kbd_backlight_levels = true,
382 .enable_kbd_backlight = true,
385 static struct samsung_quirks samsung_lid_handling = {
386 .lid_handling = true,
390 module_param(force, bool, 0);
391 MODULE_PARM_DESC(force,
392 "Disable the DMI check and forces the driver to be loaded");
395 module_param(debug, bool, S_IRUGO | S_IWUSR);
396 MODULE_PARM_DESC(debug, "Debug enabled or not");
398 static int sabi_command(struct samsung_laptop *samsung, u16 command,
399 struct sabi_data *in,
400 struct sabi_data *out)
402 const struct sabi_config *config = samsung->config;
404 u16 port = readw(samsung->sabi + config->header_offsets.port);
405 u8 complete, iface_data;
407 mutex_lock(&samsung->sabi_mutex);
411 pr_info("SABI command:0x%04x "
412 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
413 command, in->d0, in->d1, in->d2, in->d3);
415 pr_info("SABI command:0x%04x", command);
418 /* enable memory to be able to write to it */
419 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
421 /* write out the command */
422 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
423 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
424 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
426 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
427 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
428 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
429 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
431 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
433 /* write protect memory to make it safe */
434 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
436 /* see if the command actually succeeded */
437 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
438 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
440 /* iface_data = 0xFF happens when a command is not known
441 * so we only add a warning in debug mode since we will
442 * probably issue some unknown command at startup to find
443 * out which features are supported */
444 if (complete != 0xaa || (iface_data == 0xff && debug))
445 pr_warn("SABI command 0x%04x failed with"
446 " completion flag 0x%02x and interface data 0x%02x",
447 command, complete, iface_data);
449 if (complete != 0xaa || iface_data == 0xff) {
455 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
456 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
457 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
458 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
462 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
463 out->d0, out->d1, out->d2, out->d3);
467 mutex_unlock(&samsung->sabi_mutex);
471 /* simple wrappers usable with most commands */
472 static int sabi_set_commandb(struct samsung_laptop *samsung,
473 u16 command, u8 data)
475 struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
478 return sabi_command(samsung, command, &in, NULL);
481 static int read_brightness(struct samsung_laptop *samsung)
483 const struct sabi_config *config = samsung->config;
484 const struct sabi_commands *commands = &samsung->config->commands;
485 struct sabi_data sretval;
486 int user_brightness = 0;
489 retval = sabi_command(samsung, commands->get_brightness,
494 user_brightness = sretval.data[0];
495 if (user_brightness > config->min_brightness)
496 user_brightness -= config->min_brightness;
500 return user_brightness;
503 static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
505 const struct sabi_config *config = samsung->config;
506 const struct sabi_commands *commands = &samsung->config->commands;
507 u8 user_level = user_brightness + config->min_brightness;
509 if (samsung->has_stepping_quirk && user_level != 0) {
511 * short circuit if the specified level is what's already set
512 * to prevent the screen from flickering needlessly
514 if (user_brightness == read_brightness(samsung))
517 sabi_set_commandb(samsung, commands->set_brightness, 0);
520 sabi_set_commandb(samsung, commands->set_brightness, user_level);
523 static int get_brightness(struct backlight_device *bd)
525 struct samsung_laptop *samsung = bl_get_data(bd);
527 return read_brightness(samsung);
530 static void check_for_stepping_quirk(struct samsung_laptop *samsung)
534 int orig_level = read_brightness(samsung);
537 * Some laptops exhibit the strange behaviour of stepping toward
538 * (rather than setting) the brightness except when changing to/from
539 * brightness level 0. This behaviour is checked for here and worked
540 * around in set_brightness.
544 set_brightness(samsung, 1);
546 initial_level = read_brightness(samsung);
548 if (initial_level <= 2)
549 check_level = initial_level + 2;
551 check_level = initial_level - 2;
553 samsung->has_stepping_quirk = false;
554 set_brightness(samsung, check_level);
556 if (read_brightness(samsung) != check_level) {
557 samsung->has_stepping_quirk = true;
558 pr_info("enabled workaround for brightness stepping quirk\n");
561 set_brightness(samsung, orig_level);
564 static int update_status(struct backlight_device *bd)
566 struct samsung_laptop *samsung = bl_get_data(bd);
567 const struct sabi_commands *commands = &samsung->config->commands;
569 set_brightness(samsung, bd->props.brightness);
571 if (bd->props.power == FB_BLANK_UNBLANK)
572 sabi_set_commandb(samsung, commands->set_backlight, 1);
574 sabi_set_commandb(samsung, commands->set_backlight, 0);
579 static const struct backlight_ops backlight_ops = {
580 .get_brightness = get_brightness,
581 .update_status = update_status,
584 static int seclinux_rfkill_set(void *data, bool blocked)
586 struct samsung_rfkill *srfkill = data;
587 struct samsung_laptop *samsung = srfkill->samsung;
588 const struct sabi_commands *commands = &samsung->config->commands;
590 return sabi_set_commandb(samsung, commands->set_wireless_button,
594 static struct rfkill_ops seclinux_rfkill_ops = {
595 .set_block = seclinux_rfkill_set,
598 static int swsmi_wireless_status(struct samsung_laptop *samsung,
599 struct sabi_data *data)
601 const struct sabi_commands *commands = &samsung->config->commands;
603 return sabi_command(samsung, commands->get_wireless_status,
607 static int swsmi_rfkill_set(void *priv, bool blocked)
609 struct samsung_rfkill *srfkill = priv;
610 struct samsung_laptop *samsung = srfkill->samsung;
611 const struct sabi_commands *commands = &samsung->config->commands;
612 struct sabi_data data;
615 ret = swsmi_wireless_status(samsung, &data);
619 /* Don't set the state for non-present devices */
620 for (i = 0; i < 4; i++)
621 if (data.data[i] == 0x02)
624 if (srfkill->type == RFKILL_TYPE_WLAN)
625 data.data[WL_STATUS_WLAN] = !blocked;
626 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
627 data.data[WL_STATUS_BT] = !blocked;
629 return sabi_command(samsung, commands->set_wireless_status,
633 static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
635 struct samsung_rfkill *srfkill = priv;
636 struct samsung_laptop *samsung = srfkill->samsung;
637 struct sabi_data data;
640 ret = swsmi_wireless_status(samsung, &data);
644 if (srfkill->type == RFKILL_TYPE_WLAN)
645 ret = data.data[WL_STATUS_WLAN];
646 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
647 ret = data.data[WL_STATUS_BT];
651 rfkill_set_sw_state(rfkill, !ret);
654 static struct rfkill_ops swsmi_rfkill_ops = {
655 .set_block = swsmi_rfkill_set,
656 .query = swsmi_rfkill_query,
659 static ssize_t get_performance_level(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct samsung_laptop *samsung = dev_get_drvdata(dev);
663 const struct sabi_config *config = samsung->config;
664 const struct sabi_commands *commands = &config->commands;
665 struct sabi_data sretval;
670 retval = sabi_command(samsung, commands->get_performance_level,
675 /* The logic is backwards, yeah, lots of fun... */
676 for (i = 0; config->performance_levels[i].name; ++i) {
677 if (sretval.data[0] == config->performance_levels[i].value)
678 return sprintf(buf, "%s\n", config->performance_levels[i].name);
680 return sprintf(buf, "%s\n", "unknown");
683 static ssize_t set_performance_level(struct device *dev,
684 struct device_attribute *attr, const char *buf,
687 struct samsung_laptop *samsung = dev_get_drvdata(dev);
688 const struct sabi_config *config = samsung->config;
689 const struct sabi_commands *commands = &config->commands;
695 for (i = 0; config->performance_levels[i].name; ++i) {
696 const struct sabi_performance_level *level =
697 &config->performance_levels[i];
698 if (!strncasecmp(level->name, buf, strlen(level->name))) {
699 sabi_set_commandb(samsung,
700 commands->set_performance_level,
706 if (!config->performance_levels[i].name)
712 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
713 get_performance_level, set_performance_level);
715 static int read_battery_life_extender(struct samsung_laptop *samsung)
717 const struct sabi_commands *commands = &samsung->config->commands;
718 struct sabi_data data;
721 if (commands->get_battery_life_extender == 0xFFFF)
724 memset(&data, 0, sizeof(data));
726 retval = sabi_command(samsung, commands->get_battery_life_extender,
732 if (data.data[0] != 0 && data.data[0] != 1)
738 static int write_battery_life_extender(struct samsung_laptop *samsung,
741 const struct sabi_commands *commands = &samsung->config->commands;
742 struct sabi_data data;
744 memset(&data, 0, sizeof(data));
745 data.data[0] = 0x80 | enabled;
746 return sabi_command(samsung, commands->set_battery_life_extender,
750 static ssize_t get_battery_life_extender(struct device *dev,
751 struct device_attribute *attr,
754 struct samsung_laptop *samsung = dev_get_drvdata(dev);
757 ret = read_battery_life_extender(samsung);
761 return sprintf(buf, "%d\n", ret);
764 static ssize_t set_battery_life_extender(struct device *dev,
765 struct device_attribute *attr,
766 const char *buf, size_t count)
768 struct samsung_laptop *samsung = dev_get_drvdata(dev);
771 if (!count || kstrtoint(buf, 0, &value) != 0)
774 ret = write_battery_life_extender(samsung, !!value);
781 static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
782 get_battery_life_extender, set_battery_life_extender);
784 static int read_usb_charge(struct samsung_laptop *samsung)
786 const struct sabi_commands *commands = &samsung->config->commands;
787 struct sabi_data data;
790 if (commands->get_usb_charge == 0xFFFF)
793 memset(&data, 0, sizeof(data));
795 retval = sabi_command(samsung, commands->get_usb_charge,
801 if (data.data[0] != 0 && data.data[0] != 1)
807 static int write_usb_charge(struct samsung_laptop *samsung,
810 const struct sabi_commands *commands = &samsung->config->commands;
811 struct sabi_data data;
813 memset(&data, 0, sizeof(data));
814 data.data[0] = 0x80 | enabled;
815 return sabi_command(samsung, commands->set_usb_charge,
819 static ssize_t get_usb_charge(struct device *dev,
820 struct device_attribute *attr,
823 struct samsung_laptop *samsung = dev_get_drvdata(dev);
826 ret = read_usb_charge(samsung);
830 return sprintf(buf, "%d\n", ret);
833 static ssize_t set_usb_charge(struct device *dev,
834 struct device_attribute *attr,
835 const char *buf, size_t count)
837 struct samsung_laptop *samsung = dev_get_drvdata(dev);
840 if (!count || kstrtoint(buf, 0, &value) != 0)
843 ret = write_usb_charge(samsung, !!value);
850 static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
851 get_usb_charge, set_usb_charge);
853 static int read_lid_handling(struct samsung_laptop *samsung)
855 const struct sabi_commands *commands = &samsung->config->commands;
856 struct sabi_data data;
859 if (commands->get_lid_handling == 0xFFFF)
862 memset(&data, 0, sizeof(data));
863 retval = sabi_command(samsung, commands->get_lid_handling,
869 return data.data[0] & 0x1;
872 static int write_lid_handling(struct samsung_laptop *samsung,
875 const struct sabi_commands *commands = &samsung->config->commands;
876 struct sabi_data data;
878 memset(&data, 0, sizeof(data));
879 data.data[0] = 0x80 | enabled;
880 return sabi_command(samsung, commands->set_lid_handling,
884 static ssize_t get_lid_handling(struct device *dev,
885 struct device_attribute *attr,
888 struct samsung_laptop *samsung = dev_get_drvdata(dev);
891 ret = read_lid_handling(samsung);
895 return sprintf(buf, "%d\n", ret);
898 static ssize_t set_lid_handling(struct device *dev,
899 struct device_attribute *attr,
900 const char *buf, size_t count)
902 struct samsung_laptop *samsung = dev_get_drvdata(dev);
905 if (!count || kstrtoint(buf, 0, &value) != 0)
908 ret = write_lid_handling(samsung, !!value);
915 static DEVICE_ATTR(lid_handling, S_IWUSR | S_IRUGO,
916 get_lid_handling, set_lid_handling);
918 static struct attribute *platform_attributes[] = {
919 &dev_attr_performance_level.attr,
920 &dev_attr_battery_life_extender.attr,
921 &dev_attr_usb_charge.attr,
922 &dev_attr_lid_handling.attr,
926 static int find_signature(void __iomem *memcheck, const char *testStr)
931 for (loca = 0; loca < 0xffff; loca++) {
932 char temp = readb(memcheck + loca);
934 if (temp == testStr[i]) {
935 if (i == strlen(testStr)-1)
945 static void samsung_rfkill_exit(struct samsung_laptop *samsung)
947 if (samsung->wlan.rfkill) {
948 rfkill_unregister(samsung->wlan.rfkill);
949 rfkill_destroy(samsung->wlan.rfkill);
950 samsung->wlan.rfkill = NULL;
952 if (samsung->bluetooth.rfkill) {
953 rfkill_unregister(samsung->bluetooth.rfkill);
954 rfkill_destroy(samsung->bluetooth.rfkill);
955 samsung->bluetooth.rfkill = NULL;
959 static int samsung_new_rfkill(struct samsung_laptop *samsung,
960 struct samsung_rfkill *arfkill,
961 const char *name, enum rfkill_type type,
962 const struct rfkill_ops *ops,
965 struct rfkill **rfkill = &arfkill->rfkill;
968 arfkill->type = type;
969 arfkill->samsung = samsung;
971 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
978 rfkill_init_sw_state(*rfkill, blocked);
980 ret = rfkill_register(*rfkill);
982 rfkill_destroy(*rfkill);
989 static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
991 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
992 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
995 static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
997 struct sabi_data data;
1000 ret = swsmi_wireless_status(samsung, &data);
1002 /* Some swsmi laptops use the old seclinux way to control
1003 * wireless devices */
1005 ret = samsung_rfkill_init_seclinux(samsung);
1009 /* 0x02 seems to mean that the device is no present/available */
1011 if (data.data[WL_STATUS_WLAN] != 0x02)
1012 ret = samsung_new_rfkill(samsung, &samsung->wlan,
1016 !data.data[WL_STATUS_WLAN]);
1020 if (data.data[WL_STATUS_BT] != 0x02)
1021 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
1022 "samsung-bluetooth",
1023 RFKILL_TYPE_BLUETOOTH,
1025 !data.data[WL_STATUS_BT]);
1031 samsung_rfkill_exit(samsung);
1036 static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
1038 if (samsung->config->sabi_version == 2)
1039 return samsung_rfkill_init_seclinux(samsung);
1040 if (samsung->config->sabi_version == 3)
1041 return samsung_rfkill_init_swsmi(samsung);
1045 static void samsung_lid_handling_exit(struct samsung_laptop *samsung)
1047 if (samsung->quirks->lid_handling)
1048 write_lid_handling(samsung, 0);
1051 static int __init samsung_lid_handling_init(struct samsung_laptop *samsung)
1055 if (samsung->quirks->lid_handling)
1056 retval = write_lid_handling(samsung, 1);
1061 static int kbd_backlight_enable(struct samsung_laptop *samsung)
1063 const struct sabi_commands *commands = &samsung->config->commands;
1064 struct sabi_data data;
1067 if (commands->kbd_backlight == 0xFFFF)
1070 memset(&data, 0, sizeof(data));
1072 retval = sabi_command(samsung, commands->kbd_backlight,
1078 if (data.d0 != 0xccdd)
1083 static int kbd_backlight_read(struct samsung_laptop *samsung)
1085 const struct sabi_commands *commands = &samsung->config->commands;
1086 struct sabi_data data;
1089 memset(&data, 0, sizeof(data));
1090 data.data[0] = 0x81;
1091 retval = sabi_command(samsung, commands->kbd_backlight,
1097 return data.data[0];
1100 static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
1102 const struct sabi_commands *commands = &samsung->config->commands;
1103 struct sabi_data data;
1105 memset(&data, 0, sizeof(data));
1106 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
1107 return sabi_command(samsung, commands->kbd_backlight,
1111 static void kbd_led_update(struct work_struct *work)
1113 struct samsung_laptop *samsung;
1115 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
1116 kbd_backlight_write(samsung, samsung->kbd_led_wk);
1119 static void kbd_led_set(struct led_classdev *led_cdev,
1120 enum led_brightness value)
1122 struct samsung_laptop *samsung;
1124 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1126 if (value > samsung->kbd_led.max_brightness)
1127 value = samsung->kbd_led.max_brightness;
1129 samsung->kbd_led_wk = value;
1130 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1133 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1135 struct samsung_laptop *samsung;
1137 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1138 return kbd_backlight_read(samsung);
1141 static void samsung_leds_exit(struct samsung_laptop *samsung)
1143 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1144 led_classdev_unregister(&samsung->kbd_led);
1145 if (samsung->led_workqueue)
1146 destroy_workqueue(samsung->led_workqueue);
1149 static int __init samsung_leds_init(struct samsung_laptop *samsung)
1153 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1154 if (!samsung->led_workqueue)
1157 if (kbd_backlight_enable(samsung) >= 0) {
1158 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1160 samsung->kbd_led.name = "samsung::kbd_backlight";
1161 samsung->kbd_led.brightness_set = kbd_led_set;
1162 samsung->kbd_led.brightness_get = kbd_led_get;
1163 samsung->kbd_led.max_brightness = 8;
1164 if (samsung->quirks->four_kbd_backlight_levels)
1165 samsung->kbd_led.max_brightness = 4;
1167 ret = led_classdev_register(&samsung->platform_device->dev,
1172 samsung_leds_exit(samsung);
1177 static void samsung_backlight_exit(struct samsung_laptop *samsung)
1179 if (samsung->backlight_device) {
1180 backlight_device_unregister(samsung->backlight_device);
1181 samsung->backlight_device = NULL;
1185 static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1187 struct backlight_device *bd;
1188 struct backlight_properties props;
1190 if (!samsung->handle_backlight)
1193 memset(&props, 0, sizeof(struct backlight_properties));
1194 props.type = BACKLIGHT_PLATFORM;
1195 props.max_brightness = samsung->config->max_brightness -
1196 samsung->config->min_brightness;
1198 bd = backlight_device_register("samsung",
1199 &samsung->platform_device->dev,
1200 samsung, &backlight_ops,
1205 samsung->backlight_device = bd;
1206 samsung->backlight_device->props.brightness = read_brightness(samsung);
1207 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1208 backlight_update_status(samsung->backlight_device);
1213 static umode_t samsung_sysfs_is_visible(struct kobject *kobj,
1214 struct attribute *attr, int idx)
1216 struct device *dev = container_of(kobj, struct device, kobj);
1217 struct platform_device *pdev = to_platform_device(dev);
1218 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1221 if (attr == &dev_attr_performance_level.attr)
1222 ok = !!samsung->config->performance_levels[0].name;
1223 if (attr == &dev_attr_battery_life_extender.attr)
1224 ok = !!(read_battery_life_extender(samsung) >= 0);
1225 if (attr == &dev_attr_usb_charge.attr)
1226 ok = !!(read_usb_charge(samsung) >= 0);
1227 if (attr == &dev_attr_lid_handling.attr)
1228 ok = !!(read_lid_handling(samsung) >= 0);
1230 return ok ? attr->mode : 0;
1233 static struct attribute_group platform_attribute_group = {
1234 .is_visible = samsung_sysfs_is_visible,
1235 .attrs = platform_attributes
1238 static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1240 struct platform_device *device = samsung->platform_device;
1242 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1245 static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1247 struct platform_device *device = samsung->platform_device;
1249 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1253 static int show_call(struct seq_file *m, void *data)
1255 struct samsung_laptop *samsung = m->private;
1256 struct sabi_data *sdata = &samsung->debug.data;
1259 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1260 samsung->debug.command,
1261 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1263 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1266 seq_printf(m, "SABI command 0x%04x failed\n",
1267 samsung->debug.command);
1271 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1272 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1276 static int samsung_debugfs_open(struct inode *inode, struct file *file)
1278 return single_open(file, show_call, inode->i_private);
1281 static const struct file_operations samsung_laptop_call_io_ops = {
1282 .owner = THIS_MODULE,
1283 .open = samsung_debugfs_open,
1285 .llseek = seq_lseek,
1286 .release = single_release,
1289 static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1291 debugfs_remove_recursive(samsung->debug.root);
1294 static int samsung_debugfs_init(struct samsung_laptop *samsung)
1296 struct dentry *dent;
1298 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1299 if (!samsung->debug.root) {
1300 pr_err("failed to create debugfs directory");
1304 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1305 samsung->debug.f0000_wrapper.size = 0xffff;
1307 samsung->debug.data_wrapper.data = &samsung->debug.data;
1308 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1310 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1311 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1313 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1314 samsung->debug.root, &samsung->debug.command);
1318 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1319 &samsung->debug.data.d0);
1323 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1324 &samsung->debug.data.d1);
1328 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1329 &samsung->debug.data.d2);
1333 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1334 &samsung->debug.data.d3);
1338 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1339 samsung->debug.root,
1340 &samsung->debug.data_wrapper);
1344 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1345 samsung->debug.root,
1346 &samsung->debug.f0000_wrapper);
1350 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1351 samsung->debug.root, samsung,
1352 &samsung_laptop_call_io_ops);
1356 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1357 samsung->debug.root,
1358 &samsung->debug.sdiag_wrapper);
1365 samsung_debugfs_exit(samsung);
1369 static void samsung_sabi_exit(struct samsung_laptop *samsung)
1371 const struct sabi_config *config = samsung->config;
1373 /* Turn off "Linux" mode in the BIOS */
1374 if (config && config->commands.set_linux != 0xff)
1375 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
1377 if (samsung->sabi_iface) {
1378 iounmap(samsung->sabi_iface);
1379 samsung->sabi_iface = NULL;
1381 if (samsung->f0000_segment) {
1382 iounmap(samsung->f0000_segment);
1383 samsung->f0000_segment = NULL;
1386 samsung->config = NULL;
1389 static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1390 unsigned int ifaceP)
1392 const struct sabi_config *config = samsung->config;
1394 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1395 loca + 0xf0000 - 6);
1397 printk(KERN_DEBUG "SABI header:\n");
1398 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1399 readw(samsung->sabi + config->header_offsets.port));
1400 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1401 readb(samsung->sabi + config->header_offsets.iface_func));
1402 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1403 readb(samsung->sabi + config->header_offsets.en_mem));
1404 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1405 readb(samsung->sabi + config->header_offsets.re_mem));
1406 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1407 readw(samsung->sabi + config->header_offsets.data_offset));
1408 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1409 readw(samsung->sabi + config->header_offsets.data_segment));
1411 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
1414 static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1416 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1423 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1425 * Product name: 90X3A
1426 * BIOS Version: 07HL
1429 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1430 char temp = readb(samsung->f0000_segment + loca);
1432 if (isalnum(temp) || temp == '/' || temp == '-')
1433 samsung->sdiag[i++] = temp;
1438 if (debug && samsung->sdiag[0])
1439 pr_info("sdiag: %s", samsung->sdiag);
1442 static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1444 const struct sabi_config *config = NULL;
1445 const struct sabi_commands *commands;
1446 unsigned int ifaceP;
1451 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1452 if (!samsung->f0000_segment) {
1454 pr_err("Can't map the segment at 0xf0000\n");
1459 samsung_sabi_diag(samsung);
1461 /* Try to find one of the signatures in memory to find the header */
1462 for (i = 0; sabi_configs[i].test_string != NULL; ++i) {
1463 samsung->config = &sabi_configs[i];
1464 loca = find_signature(samsung->f0000_segment,
1465 samsung->config->test_string);
1470 if (loca == 0xffff) {
1472 pr_err("This computer does not support SABI\n");
1477 config = samsung->config;
1478 commands = &config->commands;
1480 /* point to the SMI port Number */
1482 samsung->sabi = (samsung->f0000_segment + loca);
1484 /* Get a pointer to the SABI Interface */
1485 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1486 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
1489 samsung_sabi_infos(samsung, loca, ifaceP);
1491 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1492 if (!samsung->sabi_iface) {
1493 pr_err("Can't remap %x\n", ifaceP);
1498 /* Turn on "Linux" mode in the BIOS */
1499 if (commands->set_linux != 0xff) {
1500 int retval = sabi_set_commandb(samsung,
1501 commands->set_linux, 0x81);
1503 pr_warn("Linux mode was not set!\n");
1509 /* Check for stepping quirk */
1510 if (samsung->handle_backlight)
1511 check_for_stepping_quirk(samsung);
1513 pr_info("detected SABI interface: %s\n",
1514 samsung->config->test_string);
1518 samsung_sabi_exit(samsung);
1523 static void samsung_platform_exit(struct samsung_laptop *samsung)
1525 if (samsung->platform_device) {
1526 platform_device_unregister(samsung->platform_device);
1527 samsung->platform_device = NULL;
1531 static int samsung_pm_notification(struct notifier_block *nb,
1532 unsigned long val, void *ptr)
1534 struct samsung_laptop *samsung;
1536 samsung = container_of(nb, struct samsung_laptop, pm_nb);
1537 if (val == PM_POST_HIBERNATION &&
1538 samsung->quirks->enable_kbd_backlight)
1539 kbd_backlight_enable(samsung);
1541 if (val == PM_POST_HIBERNATION && samsung->quirks->lid_handling)
1542 write_lid_handling(samsung, 1);
1547 static int __init samsung_platform_init(struct samsung_laptop *samsung)
1549 struct platform_device *pdev;
1551 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1553 return PTR_ERR(pdev);
1555 samsung->platform_device = pdev;
1556 platform_set_drvdata(samsung->platform_device, samsung);
1560 static struct samsung_quirks *quirks;
1562 static int __init samsung_dmi_matched(const struct dmi_system_id *d)
1564 quirks = d->driver_data;
1568 static struct dmi_system_id __initdata samsung_dmi_table[] = {
1571 DMI_MATCH(DMI_SYS_VENDOR,
1572 "SAMSUNG ELECTRONICS CO., LTD."),
1573 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1578 DMI_MATCH(DMI_SYS_VENDOR,
1579 "SAMSUNG ELECTRONICS CO., LTD."),
1580 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
1585 DMI_MATCH(DMI_SYS_VENDOR,
1586 "SAMSUNG ELECTRONICS CO., LTD."),
1587 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
1592 DMI_MATCH(DMI_SYS_VENDOR,
1593 "SAMSUNG ELECTRONICS CO., LTD."),
1594 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
1597 /* DMI ids for laptops with bad Chassis Type */
1601 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1602 DMI_MATCH(DMI_PRODUCT_NAME, "R40/R41"),
1603 DMI_MATCH(DMI_BOARD_NAME, "R40/R41"),
1606 /* Specific DMI ids for laptop with quirks */
1608 .callback = samsung_dmi_matched,
1611 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1612 DMI_MATCH(DMI_PRODUCT_NAME, "N150P"),
1613 DMI_MATCH(DMI_BOARD_NAME, "N150P"),
1615 .driver_data = &samsung_use_native_backlight,
1618 .callback = samsung_dmi_matched,
1619 .ident = "N145P/N250P/N260P",
1621 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1622 DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
1623 DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
1625 .driver_data = &samsung_use_native_backlight,
1628 .callback = samsung_dmi_matched,
1629 .ident = "N150/N210/N220",
1631 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1632 DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
1633 DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
1635 .driver_data = &samsung_broken_acpi_video,
1638 .callback = samsung_dmi_matched,
1639 .ident = "NF110/NF210/NF310",
1641 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1642 DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
1643 DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
1645 .driver_data = &samsung_broken_acpi_video,
1648 .callback = samsung_dmi_matched,
1651 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1652 DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
1653 DMI_MATCH(DMI_BOARD_NAME, "X360"),
1655 .driver_data = &samsung_broken_acpi_video,
1658 .callback = samsung_dmi_matched,
1661 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1662 DMI_MATCH(DMI_PRODUCT_NAME, "N250P"),
1663 DMI_MATCH(DMI_BOARD_NAME, "N250P"),
1665 .driver_data = &samsung_use_native_backlight,
1668 .callback = samsung_dmi_matched,
1671 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1672 DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
1673 DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
1675 .driver_data = &samsung_broken_acpi_video,
1678 .callback = samsung_dmi_matched,
1679 .ident = "730U3E/740U3E",
1681 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1682 DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
1684 .driver_data = &samsung_np740u3e,
1687 .callback = samsung_dmi_matched,
1688 .ident = "300V3Z/300V4Z/300V5Z",
1690 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1691 DMI_MATCH(DMI_PRODUCT_NAME, "300V3Z/300V4Z/300V5Z"),
1693 .driver_data = &samsung_lid_handling,
1697 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1699 static struct platform_device *samsung_platform_device;
1701 static int __init samsung_init(void)
1703 struct samsung_laptop *samsung;
1706 if (efi_enabled(EFI_BOOT))
1709 quirks = &samsung_unknown;
1710 if (!force && !dmi_check_system(samsung_dmi_table))
1713 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1717 mutex_init(&samsung->sabi_mutex);
1718 samsung->handle_backlight = true;
1719 samsung->quirks = quirks;
1722 if (samsung->quirks->broken_acpi_video)
1723 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
1724 if (samsung->quirks->use_native_backlight)
1725 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
1727 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
1728 samsung->handle_backlight = false;
1731 ret = samsung_platform_init(samsung);
1733 goto error_platform;
1735 ret = samsung_sabi_init(samsung);
1739 ret = samsung_sysfs_init(samsung);
1743 ret = samsung_backlight_init(samsung);
1745 goto error_backlight;
1747 ret = samsung_rfkill_init(samsung);
1751 ret = samsung_leds_init(samsung);
1755 ret = samsung_lid_handling_init(samsung);
1757 goto error_lid_handling;
1759 ret = samsung_debugfs_init(samsung);
1763 samsung->pm_nb.notifier_call = samsung_pm_notification;
1764 register_pm_notifier(&samsung->pm_nb);
1766 samsung_platform_device = samsung->platform_device;
1770 samsung_lid_handling_exit(samsung);
1772 samsung_leds_exit(samsung);
1774 samsung_rfkill_exit(samsung);
1776 samsung_backlight_exit(samsung);
1778 samsung_sysfs_exit(samsung);
1780 samsung_sabi_exit(samsung);
1782 samsung_platform_exit(samsung);
1788 static void __exit samsung_exit(void)
1790 struct samsung_laptop *samsung;
1792 samsung = platform_get_drvdata(samsung_platform_device);
1793 unregister_pm_notifier(&samsung->pm_nb);
1795 samsung_debugfs_exit(samsung);
1796 samsung_lid_handling_exit(samsung);
1797 samsung_leds_exit(samsung);
1798 samsung_rfkill_exit(samsung);
1799 samsung_backlight_exit(samsung);
1800 samsung_sysfs_exit(samsung);
1801 samsung_sabi_exit(samsung);
1802 samsung_platform_exit(samsung);
1805 samsung_platform_device = NULL;
1808 module_init(samsung_init);
1809 module_exit(samsung_exit);
1811 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1812 MODULE_DESCRIPTION("Samsung Backlight driver");
1813 MODULE_LICENSE("GPL");