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>
30 * This driver is needed because a number of Samsung laptops do not hook
31 * their control settings through ACPI. So we have to poke around in the
32 * BIOS to do things like brightness values, and "special" key controls.
36 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
37 * be reserved by the BIOS (which really doesn't make much sense), we tell
38 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
40 #define MAX_BRIGHT 0x07
43 #define SABI_IFACE_MAIN 0x00
44 #define SABI_IFACE_SUB 0x02
45 #define SABI_IFACE_COMPLETE 0x04
46 #define SABI_IFACE_DATA 0x05
48 #define WL_STATUS_WLAN 0x0
49 #define WL_STATUS_BT 0x2
51 /* Structure get/set data using sabi */
64 struct sabi_header_offsets {
73 struct sabi_commands {
75 * Brightness is 0 - 8, as described above.
76 * Value 0 is for the BIOS to use
83 * 0x00 - wireless is off
84 * 0x01 - wireless is on
88 * TODO, verify 3G is correct, that doesn't seem right...
90 u16 get_wireless_button;
91 u16 set_wireless_button;
93 /* 0 is off, 1 is on */
98 * 0x80 or 0x00 - no action
99 * 0x81 - recovery key pressed
101 u16 get_recovery_mode;
102 u16 set_recovery_mode;
105 * on seclinux: 0 is low, 1 is high,
106 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
108 u16 get_performance_level;
109 u16 set_performance_level;
111 /* 0x80 is off, 0x81 is on */
112 u16 get_battery_life_extender;
113 u16 set_battery_life_extender;
115 /* 0x80 is off, 0x81 is on */
119 /* the first byte is for bluetooth and the third one is for wlan */
120 u16 get_wireless_status;
121 u16 set_wireless_status;
123 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
127 * Tell the BIOS that Linux is running on this machine.
128 * 81 is on, 80 is off
133 struct sabi_performance_level {
140 const char *test_string;
142 const struct sabi_header_offsets header_offsets;
143 const struct sabi_commands commands;
144 const struct sabi_performance_level performance_levels[4];
149 static const struct sabi_config sabi_configs[] = {
151 /* I don't know if it is really 2, but it it is
152 * less than 3 anyway */
155 .test_string = "SECLINUX",
157 .main_function = 0x4c49,
165 .data_segment = 0x07,
169 .get_brightness = 0x00,
170 .set_brightness = 0x01,
172 .get_wireless_button = 0x02,
173 .set_wireless_button = 0x03,
175 .get_backlight = 0x04,
176 .set_backlight = 0x05,
178 .get_recovery_mode = 0x06,
179 .set_recovery_mode = 0x07,
181 .get_performance_level = 0x08,
182 .set_performance_level = 0x09,
184 .get_battery_life_extender = 0xFFFF,
185 .set_battery_life_extender = 0xFFFF,
187 .get_usb_charge = 0xFFFF,
188 .set_usb_charge = 0xFFFF,
190 .get_wireless_status = 0xFFFF,
191 .set_wireless_status = 0xFFFF,
193 .kbd_backlight = 0xFFFF,
198 .performance_levels = {
215 .test_string = "SwSmi@",
217 .main_function = 0x5843,
225 .data_segment = 0x07,
229 .get_brightness = 0x10,
230 .set_brightness = 0x11,
232 .get_wireless_button = 0x12,
233 .set_wireless_button = 0x13,
235 .get_backlight = 0x2d,
236 .set_backlight = 0x2e,
238 .get_recovery_mode = 0xff,
239 .set_recovery_mode = 0xff,
241 .get_performance_level = 0x31,
242 .set_performance_level = 0x32,
244 .get_battery_life_extender = 0x65,
245 .set_battery_life_extender = 0x66,
247 .get_usb_charge = 0x67,
248 .set_usb_charge = 0x68,
250 .get_wireless_status = 0x69,
251 .set_wireless_status = 0x6a,
253 .kbd_backlight = 0x78,
258 .performance_levels = {
280 * samsung-laptop/ - debugfs root directory
281 * f0000_segment - dump f0000 segment
282 * command - current command
283 * data - current data
284 * d0, d1, d2, d3 - data fields
285 * call - call SABI using command and data
287 * This allow to call arbitrary sabi commands wihout
288 * modifying the driver at all.
289 * For example, setting the keyboard backlight brightness to 5
291 * echo 0x78 > command
299 struct samsung_laptop_debug {
301 struct sabi_data data;
304 struct debugfs_blob_wrapper f0000_wrapper;
305 struct debugfs_blob_wrapper data_wrapper;
306 struct debugfs_blob_wrapper sdiag_wrapper;
309 struct samsung_laptop;
311 struct samsung_rfkill {
312 struct samsung_laptop *samsung;
313 struct rfkill *rfkill;
314 enum rfkill_type type;
317 struct samsung_laptop {
318 const struct sabi_config *config;
321 void __iomem *sabi_iface;
322 void __iomem *f0000_segment;
324 struct mutex sabi_mutex;
326 struct platform_device *platform_device;
327 struct backlight_device *backlight_device;
329 struct samsung_rfkill wlan;
330 struct samsung_rfkill bluetooth;
332 struct led_classdev kbd_led;
334 struct workqueue_struct *led_workqueue;
335 struct work_struct kbd_led_work;
337 struct samsung_laptop_debug debug;
339 bool handle_backlight;
340 bool has_stepping_quirk;
348 module_param(force, bool, 0);
349 MODULE_PARM_DESC(force,
350 "Disable the DMI check and forces the driver to be loaded");
353 module_param(debug, bool, S_IRUGO | S_IWUSR);
354 MODULE_PARM_DESC(debug, "Debug enabled or not");
356 static int sabi_command(struct samsung_laptop *samsung, u16 command,
357 struct sabi_data *in,
358 struct sabi_data *out)
360 const struct sabi_config *config = samsung->config;
362 u16 port = readw(samsung->sabi + config->header_offsets.port);
363 u8 complete, iface_data;
365 mutex_lock(&samsung->sabi_mutex);
369 pr_info("SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
370 command, in->d0, in->d1, in->d2, in->d3);
372 pr_info("SABI 0x%04x", command);
375 /* enable memory to be able to write to it */
376 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
378 /* write out the command */
379 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
380 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
381 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
383 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
384 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
385 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
386 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
388 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
390 /* write protect memory to make it safe */
391 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
393 /* see if the command actually succeeded */
394 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
395 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
396 if (complete != 0xaa || iface_data == 0xff) {
397 pr_warn("SABI command 0x%04x failed with"
398 " completion flag 0x%02x and interface data 0x%02x",
399 command, complete, iface_data);
405 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
406 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
407 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
408 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
412 pr_info("SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
413 out->d0, out->d1, out->d2, out->d3);
417 mutex_unlock(&samsung->sabi_mutex);
421 /* simple wrappers usable with most commands */
422 static int sabi_set_commandb(struct samsung_laptop *samsung,
423 u16 command, u8 data)
425 struct sabi_data in = { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 };
428 return sabi_command(samsung, command, &in, NULL);
431 static int read_brightness(struct samsung_laptop *samsung)
433 const struct sabi_config *config = samsung->config;
434 const struct sabi_commands *commands = &samsung->config->commands;
435 struct sabi_data sretval;
436 int user_brightness = 0;
439 retval = sabi_command(samsung, commands->get_brightness,
444 user_brightness = sretval.data[0];
445 if (user_brightness > config->min_brightness)
446 user_brightness -= config->min_brightness;
450 return user_brightness;
453 static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
455 const struct sabi_config *config = samsung->config;
456 const struct sabi_commands *commands = &samsung->config->commands;
457 u8 user_level = user_brightness + config->min_brightness;
459 if (samsung->has_stepping_quirk && user_level != 0) {
461 * short circuit if the specified level is what's already set
462 * to prevent the screen from flickering needlessly
464 if (user_brightness == read_brightness(samsung))
467 sabi_set_commandb(samsung, commands->set_brightness, 0);
470 sabi_set_commandb(samsung, commands->set_brightness, user_level);
473 static int get_brightness(struct backlight_device *bd)
475 struct samsung_laptop *samsung = bl_get_data(bd);
477 return read_brightness(samsung);
480 static void check_for_stepping_quirk(struct samsung_laptop *samsung)
484 int orig_level = read_brightness(samsung);
487 * Some laptops exhibit the strange behaviour of stepping toward
488 * (rather than setting) the brightness except when changing to/from
489 * brightness level 0. This behaviour is checked for here and worked
490 * around in set_brightness.
494 set_brightness(samsung, 1);
496 initial_level = read_brightness(samsung);
498 if (initial_level <= 2)
499 check_level = initial_level + 2;
501 check_level = initial_level - 2;
503 samsung->has_stepping_quirk = false;
504 set_brightness(samsung, check_level);
506 if (read_brightness(samsung) != check_level) {
507 samsung->has_stepping_quirk = true;
508 pr_info("enabled workaround for brightness stepping quirk\n");
511 set_brightness(samsung, orig_level);
514 static int update_status(struct backlight_device *bd)
516 struct samsung_laptop *samsung = bl_get_data(bd);
517 const struct sabi_commands *commands = &samsung->config->commands;
519 set_brightness(samsung, bd->props.brightness);
521 if (bd->props.power == FB_BLANK_UNBLANK)
522 sabi_set_commandb(samsung, commands->set_backlight, 1);
524 sabi_set_commandb(samsung, commands->set_backlight, 0);
529 static const struct backlight_ops backlight_ops = {
530 .get_brightness = get_brightness,
531 .update_status = update_status,
534 static int seclinux_rfkill_set(void *data, bool blocked)
536 struct samsung_laptop *samsung = data;
537 const struct sabi_commands *commands = &samsung->config->commands;
539 return sabi_set_commandb(samsung, commands->set_wireless_button,
543 static struct rfkill_ops seclinux_rfkill_ops = {
544 .set_block = seclinux_rfkill_set,
547 static int swsmi_wireless_status(struct samsung_laptop *samsung,
548 struct sabi_data *data)
550 const struct sabi_commands *commands = &samsung->config->commands;
552 return sabi_command(samsung, commands->get_wireless_status,
556 static int swsmi_rfkill_set(void *priv, bool blocked)
558 struct samsung_rfkill *srfkill = priv;
559 struct samsung_laptop *samsung = srfkill->samsung;
560 const struct sabi_commands *commands = &samsung->config->commands;
561 struct sabi_data data;
564 ret = swsmi_wireless_status(samsung, &data);
568 /* Don't set the state for non-present devices */
569 for (i = 0; i < 4; i++)
570 if (data.data[i] == 0x02)
573 if (srfkill->type == RFKILL_TYPE_WLAN)
574 data.data[WL_STATUS_WLAN] = !blocked;
575 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
576 data.data[WL_STATUS_BT] = !blocked;
578 return sabi_command(samsung, commands->set_wireless_status,
582 static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
584 struct samsung_rfkill *srfkill = priv;
585 struct samsung_laptop *samsung = srfkill->samsung;
586 struct sabi_data data;
589 ret = swsmi_wireless_status(samsung, &data);
593 if (srfkill->type == RFKILL_TYPE_WLAN)
594 ret = data.data[WL_STATUS_WLAN];
595 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
596 ret = data.data[WL_STATUS_BT];
600 rfkill_set_sw_state(rfkill, !ret);
603 static struct rfkill_ops swsmi_rfkill_ops = {
604 .set_block = swsmi_rfkill_set,
605 .query = swsmi_rfkill_query,
608 static ssize_t get_performance_level(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct samsung_laptop *samsung = dev_get_drvdata(dev);
612 const struct sabi_config *config = samsung->config;
613 const struct sabi_commands *commands = &config->commands;
614 struct sabi_data sretval;
619 retval = sabi_command(samsung, commands->get_performance_level,
624 /* The logic is backwards, yeah, lots of fun... */
625 for (i = 0; config->performance_levels[i].name; ++i) {
626 if (sretval.data[0] == config->performance_levels[i].value)
627 return sprintf(buf, "%s\n", config->performance_levels[i].name);
629 return sprintf(buf, "%s\n", "unknown");
632 static ssize_t set_performance_level(struct device *dev,
633 struct device_attribute *attr, const char *buf,
636 struct samsung_laptop *samsung = dev_get_drvdata(dev);
637 const struct sabi_config *config = samsung->config;
638 const struct sabi_commands *commands = &config->commands;
644 for (i = 0; config->performance_levels[i].name; ++i) {
645 const struct sabi_performance_level *level =
646 &config->performance_levels[i];
647 if (!strncasecmp(level->name, buf, strlen(level->name))) {
648 sabi_set_commandb(samsung,
649 commands->set_performance_level,
655 if (!config->performance_levels[i].name)
661 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
662 get_performance_level, set_performance_level);
664 static int read_battery_life_extender(struct samsung_laptop *samsung)
666 const struct sabi_commands *commands = &samsung->config->commands;
667 struct sabi_data data;
670 if (commands->get_battery_life_extender == 0xFFFF)
673 memset(&data, 0, sizeof(data));
675 retval = sabi_command(samsung, commands->get_battery_life_extender,
681 if (data.data[0] != 0 && data.data[0] != 1)
687 static int write_battery_life_extender(struct samsung_laptop *samsung,
690 const struct sabi_commands *commands = &samsung->config->commands;
691 struct sabi_data data;
693 memset(&data, 0, sizeof(data));
694 data.data[0] = 0x80 | enabled;
695 return sabi_command(samsung, commands->set_battery_life_extender,
699 static ssize_t get_battery_life_extender(struct device *dev,
700 struct device_attribute *attr,
703 struct samsung_laptop *samsung = dev_get_drvdata(dev);
706 ret = read_battery_life_extender(samsung);
710 return sprintf(buf, "%d\n", ret);
713 static ssize_t set_battery_life_extender(struct device *dev,
714 struct device_attribute *attr,
715 const char *buf, size_t count)
717 struct samsung_laptop *samsung = dev_get_drvdata(dev);
720 if (!count || sscanf(buf, "%i", &value) != 1)
723 ret = write_battery_life_extender(samsung, !!value);
730 static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
731 get_battery_life_extender, set_battery_life_extender);
733 static int read_usb_charge(struct samsung_laptop *samsung)
735 const struct sabi_commands *commands = &samsung->config->commands;
736 struct sabi_data data;
739 if (commands->get_usb_charge == 0xFFFF)
742 memset(&data, 0, sizeof(data));
744 retval = sabi_command(samsung, commands->get_usb_charge,
750 if (data.data[0] != 0 && data.data[0] != 1)
756 static int write_usb_charge(struct samsung_laptop *samsung,
759 const struct sabi_commands *commands = &samsung->config->commands;
760 struct sabi_data data;
762 memset(&data, 0, sizeof(data));
763 data.data[0] = 0x80 | enabled;
764 return sabi_command(samsung, commands->set_usb_charge,
768 static ssize_t get_usb_charge(struct device *dev,
769 struct device_attribute *attr,
772 struct samsung_laptop *samsung = dev_get_drvdata(dev);
775 ret = read_usb_charge(samsung);
779 return sprintf(buf, "%d\n", ret);
782 static ssize_t set_usb_charge(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
786 struct samsung_laptop *samsung = dev_get_drvdata(dev);
789 if (!count || sscanf(buf, "%i", &value) != 1)
792 ret = write_usb_charge(samsung, !!value);
799 static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
800 get_usb_charge, set_usb_charge);
802 static struct attribute *platform_attributes[] = {
803 &dev_attr_performance_level.attr,
804 &dev_attr_battery_life_extender.attr,
805 &dev_attr_usb_charge.attr,
809 static int find_signature(void __iomem *memcheck, const char *testStr)
814 for (loca = 0; loca < 0xffff; loca++) {
815 char temp = readb(memcheck + loca);
817 if (temp == testStr[i]) {
818 if (i == strlen(testStr)-1)
828 static void samsung_rfkill_exit(struct samsung_laptop *samsung)
830 if (samsung->wlan.rfkill) {
831 rfkill_unregister(samsung->wlan.rfkill);
832 rfkill_destroy(samsung->wlan.rfkill);
833 samsung->wlan.rfkill = NULL;
835 if (samsung->bluetooth.rfkill) {
836 rfkill_unregister(samsung->bluetooth.rfkill);
837 rfkill_destroy(samsung->bluetooth.rfkill);
838 samsung->bluetooth.rfkill = NULL;
842 static int samsung_new_rfkill(struct samsung_laptop *samsung,
843 struct samsung_rfkill *arfkill,
844 const char *name, enum rfkill_type type,
845 const struct rfkill_ops *ops,
848 struct rfkill **rfkill = &arfkill->rfkill;
851 arfkill->type = type;
852 arfkill->samsung = samsung;
854 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
861 rfkill_init_sw_state(*rfkill, blocked);
863 ret = rfkill_register(*rfkill);
865 rfkill_destroy(*rfkill);
872 static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
874 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
875 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
878 static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
880 struct sabi_data data;
883 ret = swsmi_wireless_status(samsung, &data);
887 /* 0x02 seems to mean that the device is no present/available */
889 if (data.data[WL_STATUS_WLAN] != 0x02)
890 ret = samsung_new_rfkill(samsung, &samsung->wlan,
894 !data.data[WL_STATUS_WLAN]);
898 if (data.data[WL_STATUS_BT] != 0x02)
899 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
901 RFKILL_TYPE_BLUETOOTH,
903 !data.data[WL_STATUS_BT]);
909 samsung_rfkill_exit(samsung);
914 static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
916 if (samsung->config->sabi_version == 2)
917 return samsung_rfkill_init_seclinux(samsung);
918 if (samsung->config->sabi_version == 3)
919 return samsung_rfkill_init_swsmi(samsung);
923 static int kbd_backlight_enable(struct samsung_laptop *samsung)
925 const struct sabi_commands *commands = &samsung->config->commands;
926 struct sabi_data data;
929 if (commands->kbd_backlight == 0xFFFF)
932 memset(&data, 0, sizeof(data));
934 retval = sabi_command(samsung, commands->kbd_backlight,
940 if (data.d0 != 0xccdd)
945 static int kbd_backlight_read(struct samsung_laptop *samsung)
947 const struct sabi_commands *commands = &samsung->config->commands;
948 struct sabi_data data;
951 memset(&data, 0, sizeof(data));
953 retval = sabi_command(samsung, commands->kbd_backlight,
962 static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
964 const struct sabi_commands *commands = &samsung->config->commands;
965 struct sabi_data data;
967 memset(&data, 0, sizeof(data));
968 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
969 return sabi_command(samsung, commands->kbd_backlight,
973 static void kbd_led_update(struct work_struct *work)
975 struct samsung_laptop *samsung;
977 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
978 kbd_backlight_write(samsung, samsung->kbd_led_wk);
981 static void kbd_led_set(struct led_classdev *led_cdev,
982 enum led_brightness value)
984 struct samsung_laptop *samsung;
986 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
988 if (value > samsung->kbd_led.max_brightness)
989 value = samsung->kbd_led.max_brightness;
993 samsung->kbd_led_wk = value;
994 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
997 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
999 struct samsung_laptop *samsung;
1001 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1002 return kbd_backlight_read(samsung);
1005 static void samsung_leds_exit(struct samsung_laptop *samsung)
1007 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1008 led_classdev_unregister(&samsung->kbd_led);
1009 if (samsung->led_workqueue)
1010 destroy_workqueue(samsung->led_workqueue);
1013 static int __init samsung_leds_init(struct samsung_laptop *samsung)
1017 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1018 if (!samsung->led_workqueue)
1021 if (kbd_backlight_enable(samsung) >= 0) {
1022 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1024 samsung->kbd_led.name = "samsung::kbd_backlight";
1025 samsung->kbd_led.brightness_set = kbd_led_set;
1026 samsung->kbd_led.brightness_get = kbd_led_get;
1027 samsung->kbd_led.max_brightness = 8;
1029 ret = led_classdev_register(&samsung->platform_device->dev,
1034 samsung_leds_exit(samsung);
1039 static void samsung_backlight_exit(struct samsung_laptop *samsung)
1041 if (samsung->backlight_device) {
1042 backlight_device_unregister(samsung->backlight_device);
1043 samsung->backlight_device = NULL;
1047 static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1049 struct backlight_device *bd;
1050 struct backlight_properties props;
1052 if (!samsung->handle_backlight)
1055 memset(&props, 0, sizeof(struct backlight_properties));
1056 props.type = BACKLIGHT_PLATFORM;
1057 props.max_brightness = samsung->config->max_brightness -
1058 samsung->config->min_brightness;
1060 bd = backlight_device_register("samsung",
1061 &samsung->platform_device->dev,
1062 samsung, &backlight_ops,
1067 samsung->backlight_device = bd;
1068 samsung->backlight_device->props.brightness = read_brightness(samsung);
1069 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1070 backlight_update_status(samsung->backlight_device);
1075 static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
1076 struct attribute *attr, int idx)
1078 struct device *dev = container_of(kobj, struct device, kobj);
1079 struct platform_device *pdev = to_platform_device(dev);
1080 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1083 if (attr == &dev_attr_performance_level.attr)
1084 ok = !!samsung->config->performance_levels[0].name;
1085 if (attr == &dev_attr_battery_life_extender.attr)
1086 ok = !!(read_battery_life_extender(samsung) >= 0);
1087 if (attr == &dev_attr_usb_charge.attr)
1088 ok = !!(read_usb_charge(samsung) >= 0);
1090 return ok ? attr->mode : 0;
1093 static struct attribute_group platform_attribute_group = {
1094 .is_visible = samsung_sysfs_is_visible,
1095 .attrs = platform_attributes
1098 static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1100 struct platform_device *device = samsung->platform_device;
1102 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1105 static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1107 struct platform_device *device = samsung->platform_device;
1109 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1113 static int show_call(struct seq_file *m, void *data)
1115 struct samsung_laptop *samsung = m->private;
1116 struct sabi_data *sdata = &samsung->debug.data;
1119 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1120 samsung->debug.command,
1121 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1123 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1126 seq_printf(m, "SABI command 0x%04x failed\n",
1127 samsung->debug.command);
1131 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1132 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1136 static int samsung_debugfs_open(struct inode *inode, struct file *file)
1138 return single_open(file, show_call, inode->i_private);
1141 static const struct file_operations samsung_laptop_call_io_ops = {
1142 .owner = THIS_MODULE,
1143 .open = samsung_debugfs_open,
1145 .llseek = seq_lseek,
1146 .release = single_release,
1149 static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1151 debugfs_remove_recursive(samsung->debug.root);
1154 static int samsung_debugfs_init(struct samsung_laptop *samsung)
1156 struct dentry *dent;
1158 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1159 if (!samsung->debug.root) {
1160 pr_err("failed to create debugfs directory");
1164 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1165 samsung->debug.f0000_wrapper.size = 0xffff;
1167 samsung->debug.data_wrapper.data = &samsung->debug.data;
1168 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1170 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1171 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1173 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1174 samsung->debug.root, &samsung->debug.command);
1178 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1179 &samsung->debug.data.d0);
1183 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1184 &samsung->debug.data.d1);
1188 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1189 &samsung->debug.data.d2);
1193 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1194 &samsung->debug.data.d3);
1198 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1199 samsung->debug.root,
1200 &samsung->debug.data_wrapper);
1204 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1205 samsung->debug.root,
1206 &samsung->debug.f0000_wrapper);
1210 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1211 samsung->debug.root, samsung,
1212 &samsung_laptop_call_io_ops);
1216 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1217 samsung->debug.root,
1218 &samsung->debug.sdiag_wrapper);
1225 samsung_debugfs_exit(samsung);
1229 static void samsung_sabi_exit(struct samsung_laptop *samsung)
1231 const struct sabi_config *config = samsung->config;
1233 /* Turn off "Linux" mode in the BIOS */
1234 if (config && config->commands.set_linux != 0xff)
1235 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
1237 if (samsung->sabi_iface) {
1238 iounmap(samsung->sabi_iface);
1239 samsung->sabi_iface = NULL;
1241 if (samsung->f0000_segment) {
1242 iounmap(samsung->f0000_segment);
1243 samsung->f0000_segment = NULL;
1246 samsung->config = NULL;
1249 static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1250 unsigned int ifaceP)
1252 const struct sabi_config *config = samsung->config;
1254 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1255 loca + 0xf0000 - 6);
1257 printk(KERN_DEBUG "SABI header:\n");
1258 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1259 readw(samsung->sabi + config->header_offsets.port));
1260 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1261 readb(samsung->sabi + config->header_offsets.iface_func));
1262 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1263 readb(samsung->sabi + config->header_offsets.en_mem));
1264 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1265 readb(samsung->sabi + config->header_offsets.re_mem));
1266 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1267 readw(samsung->sabi + config->header_offsets.data_offset));
1268 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1269 readw(samsung->sabi + config->header_offsets.data_segment));
1271 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
1274 static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1276 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1283 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1285 * Product name: 90X3A
1286 * BIOS Version: 07HL
1289 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1290 char temp = readb(samsung->f0000_segment + loca);
1292 if (isalnum(temp) || temp == '/' || temp == '-')
1293 samsung->sdiag[i++] = temp;
1298 if (debug && samsung->sdiag[0])
1299 pr_info("sdiag: %s", samsung->sdiag);
1302 static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1304 const struct sabi_config *config = NULL;
1305 const struct sabi_commands *commands;
1306 unsigned int ifaceP;
1311 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1312 if (!samsung->f0000_segment) {
1314 pr_err("Can't map the segment at 0xf0000\n");
1319 samsung_sabi_diag(samsung);
1321 /* Try to find one of the signatures in memory to find the header */
1322 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1323 samsung->config = &sabi_configs[i];
1324 loca = find_signature(samsung->f0000_segment,
1325 samsung->config->test_string);
1330 if (loca == 0xffff) {
1332 pr_err("This computer does not support SABI\n");
1337 config = samsung->config;
1338 commands = &config->commands;
1340 /* point to the SMI port Number */
1342 samsung->sabi = (samsung->f0000_segment + loca);
1344 /* Get a pointer to the SABI Interface */
1345 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1346 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
1349 samsung_sabi_infos(samsung, loca, ifaceP);
1351 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1352 if (!samsung->sabi_iface) {
1353 pr_err("Can't remap %x\n", ifaceP);
1358 /* Turn on "Linux" mode in the BIOS */
1359 if (commands->set_linux != 0xff) {
1360 int retval = sabi_set_commandb(samsung,
1361 commands->set_linux, 0x81);
1363 pr_warn("Linux mode was not set!\n");
1369 /* Check for stepping quirk */
1370 if (samsung->handle_backlight)
1371 check_for_stepping_quirk(samsung);
1375 samsung_sabi_exit(samsung);
1380 static void samsung_platform_exit(struct samsung_laptop *samsung)
1382 if (samsung->platform_device) {
1383 platform_device_unregister(samsung->platform_device);
1384 samsung->platform_device = NULL;
1388 static int __init samsung_platform_init(struct samsung_laptop *samsung)
1390 struct platform_device *pdev;
1392 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1394 return PTR_ERR(pdev);
1396 samsung->platform_device = pdev;
1397 platform_set_drvdata(samsung->platform_device, samsung);
1401 static struct dmi_system_id __initdata samsung_dmi_table[] = {
1404 DMI_MATCH(DMI_SYS_VENDOR,
1405 "SAMSUNG ELECTRONICS CO., LTD."),
1406 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1411 DMI_MATCH(DMI_SYS_VENDOR,
1412 "SAMSUNG ELECTRONICS CO., LTD."),
1413 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
1418 DMI_MATCH(DMI_SYS_VENDOR,
1419 "SAMSUNG ELECTRONICS CO., LTD."),
1420 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
1425 DMI_MATCH(DMI_SYS_VENDOR,
1426 "SAMSUNG ELECTRONICS CO., LTD."),
1427 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
1432 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1434 static struct platform_device *samsung_platform_device;
1436 static int __init samsung_init(void)
1438 struct samsung_laptop *samsung;
1441 if (!force && !dmi_check_system(samsung_dmi_table))
1444 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1448 mutex_init(&samsung->sabi_mutex);
1449 samsung->handle_backlight = true;
1452 /* Don't handle backlight here if the acpi video already handle it */
1453 if (acpi_video_backlight_support())
1454 samsung->handle_backlight = false;
1456 ret = samsung_platform_init(samsung);
1458 goto error_platform;
1460 ret = samsung_sabi_init(samsung);
1465 /* Only log that if we are really on a sabi platform */
1466 if (acpi_video_backlight_support())
1467 pr_info("Backlight controlled by ACPI video driver\n");
1470 ret = samsung_sysfs_init(samsung);
1474 ret = samsung_backlight_init(samsung);
1476 goto error_backlight;
1478 ret = samsung_rfkill_init(samsung);
1482 ret = samsung_leds_init(samsung);
1486 ret = samsung_debugfs_init(samsung);
1490 samsung_platform_device = samsung->platform_device;
1494 samsung_leds_exit(samsung);
1496 samsung_rfkill_exit(samsung);
1498 samsung_backlight_exit(samsung);
1500 samsung_sysfs_exit(samsung);
1502 samsung_sabi_exit(samsung);
1504 samsung_platform_exit(samsung);
1510 static void __exit samsung_exit(void)
1512 struct samsung_laptop *samsung;
1514 samsung = platform_get_drvdata(samsung_platform_device);
1516 samsung_debugfs_exit(samsung);
1517 samsung_leds_exit(samsung);
1518 samsung_rfkill_exit(samsung);
1519 samsung_backlight_exit(samsung);
1520 samsung_sysfs_exit(samsung);
1521 samsung_sabi_exit(samsung);
1522 samsung_platform_exit(samsung);
1525 samsung_platform_device = NULL;
1528 module_init(samsung_init);
1529 module_exit(samsung_exit);
1531 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1532 MODULE_DESCRIPTION("Samsung Backlight driver");
1533 MODULE_LICENSE("GPL");