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>
21 #include <linux/dmi.h>
22 #include <linux/platform_device.h>
23 #include <linux/rfkill.h>
24 #include <linux/acpi.h>
27 * This driver is needed because a number of Samsung laptops do not hook
28 * their control settings through ACPI. So we have to poke around in the
29 * BIOS to do things like brightness values, and "special" key controls.
33 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
34 * be reserved by the BIOS (which really doesn't make much sense), we tell
35 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
37 #define MAX_BRIGHT 0x07
40 #define SABI_IFACE_MAIN 0x00
41 #define SABI_IFACE_SUB 0x02
42 #define SABI_IFACE_COMPLETE 0x04
43 #define SABI_IFACE_DATA 0x05
45 /* Structure to get data back to the calling function */
50 struct sabi_header_offsets {
59 struct sabi_commands {
61 * Brightness is 0 - 8, as described above.
62 * Value 0 is for the BIOS to use
69 * 0x00 - wireless is off
70 * 0x01 - wireless is on
74 * TODO, verify 3G is correct, that doesn't seem right...
76 u8 get_wireless_button;
77 u8 set_wireless_button;
79 /* 0 is off, 1 is on */
84 * 0x80 or 0x00 - no action
85 * 0x81 - recovery key pressed
91 * on seclinux: 0 is low, 1 is high,
92 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
94 u8 get_performance_level;
95 u8 set_performance_level;
98 * Tell the BIOS that Linux is running on this machine.
104 struct sabi_performance_level {
110 const char *test_string;
112 const struct sabi_header_offsets header_offsets;
113 const struct sabi_commands commands;
114 const struct sabi_performance_level performance_levels[4];
119 static const struct sabi_config sabi_configs[] = {
121 .test_string = "SECLINUX",
123 .main_function = 0x4c49,
131 .data_segment = 0x07,
135 .get_brightness = 0x00,
136 .set_brightness = 0x01,
138 .get_wireless_button = 0x02,
139 .set_wireless_button = 0x03,
141 .get_backlight = 0x04,
142 .set_backlight = 0x05,
144 .get_recovery_mode = 0x06,
145 .set_recovery_mode = 0x07,
147 .get_performance_level = 0x08,
148 .set_performance_level = 0x09,
153 .performance_levels = {
168 .test_string = "SwSmi@",
170 .main_function = 0x5843,
178 .data_segment = 0x07,
182 .get_brightness = 0x10,
183 .set_brightness = 0x11,
185 .get_wireless_button = 0x12,
186 .set_wireless_button = 0x13,
188 .get_backlight = 0x2d,
189 .set_backlight = 0x2e,
191 .get_recovery_mode = 0xff,
192 .set_recovery_mode = 0xff,
194 .get_performance_level = 0x31,
195 .set_performance_level = 0x32,
200 .performance_levels = {
221 static const struct sabi_config *sabi_config;
223 static void __iomem *sabi;
224 static void __iomem *sabi_iface;
225 static void __iomem *f0000_segment;
226 static struct backlight_device *backlight_device;
227 static struct mutex sabi_mutex;
228 static struct platform_device *sdev;
229 static struct rfkill *rfk;
230 static bool handle_backlight;
231 static bool has_stepping_quirk;
234 module_param(force, bool, 0);
235 MODULE_PARM_DESC(force,
236 "Disable the DMI check and forces the driver to be loaded");
239 module_param(debug, bool, S_IRUGO | S_IWUSR);
240 MODULE_PARM_DESC(debug, "Debug enabled or not");
242 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
245 u16 port = readw(sabi + sabi_config->header_offsets.port);
246 u8 complete, iface_data;
248 mutex_lock(&sabi_mutex);
250 /* enable memory to be able to write to it */
251 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
253 /* write out the command */
254 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
255 writew(command, sabi_iface + SABI_IFACE_SUB);
256 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
257 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
259 /* write protect memory to make it safe */
260 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
262 /* see if the command actually succeeded */
263 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
264 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
265 if (complete != 0xaa || iface_data == 0xff) {
266 pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
267 command, complete, iface_data);
272 * Save off the data into a structure so the caller use it.
273 * Right now we only want the first 4 bytes,
274 * There are commands that need more, but not for the ones we
275 * currently care about.
277 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
278 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
279 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
280 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
283 mutex_unlock(&sabi_mutex);
288 static int sabi_set_command(u8 command, u8 data)
291 u16 port = readw(sabi + sabi_config->header_offsets.port);
292 u8 complete, iface_data;
294 mutex_lock(&sabi_mutex);
296 /* enable memory to be able to write to it */
297 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
299 /* write out the command */
300 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
301 writew(command, sabi_iface + SABI_IFACE_SUB);
302 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
303 writeb(data, sabi_iface + SABI_IFACE_DATA);
304 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
306 /* write protect memory to make it safe */
307 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
309 /* see if the command actually succeeded */
310 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
311 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
312 if (complete != 0xaa || iface_data == 0xff) {
313 pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
314 command, complete, iface_data);
318 mutex_unlock(&sabi_mutex);
322 static void test_backlight(void)
324 struct sabi_retval sretval;
326 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
327 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
329 sabi_set_command(sabi_config->commands.set_backlight, 0);
330 printk(KERN_DEBUG "backlight should be off\n");
332 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
333 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
337 sabi_set_command(sabi_config->commands.set_backlight, 1);
338 printk(KERN_DEBUG "backlight should be on\n");
340 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
341 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
344 static void test_wireless(void)
346 struct sabi_retval sretval;
348 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
349 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
351 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
352 printk(KERN_DEBUG "wireless led should be off\n");
354 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
355 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
359 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
360 printk(KERN_DEBUG "wireless led should be on\n");
362 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
363 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
366 static u8 read_brightness(void)
368 struct sabi_retval sretval;
369 int user_brightness = 0;
372 retval = sabi_get_command(sabi_config->commands.get_brightness,
375 user_brightness = sretval.retval[0];
376 if (user_brightness > sabi_config->min_brightness)
377 user_brightness -= sabi_config->min_brightness;
381 return user_brightness;
384 static void set_brightness(u8 user_brightness)
386 u8 user_level = user_brightness + sabi_config->min_brightness;
388 if (has_stepping_quirk && user_level != 0) {
390 * short circuit if the specified level is what's already set
391 * to prevent the screen from flickering needlessly
393 if (user_brightness == read_brightness())
396 sabi_set_command(sabi_config->commands.set_brightness, 0);
399 sabi_set_command(sabi_config->commands.set_brightness, user_level);
402 static int get_brightness(struct backlight_device *bd)
404 return (int)read_brightness();
407 static void check_for_stepping_quirk(void)
411 u8 orig_level = read_brightness();
414 * Some laptops exhibit the strange behaviour of stepping toward
415 * (rather than setting) the brightness except when changing to/from
416 * brightness level 0. This behaviour is checked for here and worked
417 * around in set_brightness.
423 initial_level = read_brightness();
425 if (initial_level <= 2)
426 check_level = initial_level + 2;
428 check_level = initial_level - 2;
430 has_stepping_quirk = false;
431 set_brightness(check_level);
433 if (read_brightness() != check_level) {
434 has_stepping_quirk = true;
435 pr_info("enabled workaround for brightness stepping quirk\n");
438 set_brightness(orig_level);
441 static int update_status(struct backlight_device *bd)
443 set_brightness(bd->props.brightness);
445 if (bd->props.power == FB_BLANK_UNBLANK)
446 sabi_set_command(sabi_config->commands.set_backlight, 1);
448 sabi_set_command(sabi_config->commands.set_backlight, 0);
452 static const struct backlight_ops backlight_ops = {
453 .get_brightness = get_brightness,
454 .update_status = update_status,
457 static int rfkill_set(void *data, bool blocked)
459 /* Do something with blocked...*/
461 * blocked == false is on
462 * blocked == true is off
465 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
467 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
472 static struct rfkill_ops rfkill_ops = {
473 .set_block = rfkill_set,
476 static int init_wireless(struct platform_device *sdev)
480 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
485 retval = rfkill_register(rfk);
494 static void destroy_wireless(void)
496 rfkill_unregister(rfk);
500 static ssize_t get_performance_level(struct device *dev,
501 struct device_attribute *attr, char *buf)
503 struct sabi_retval sretval;
508 retval = sabi_get_command(sabi_config->commands.get_performance_level,
513 /* The logic is backwards, yeah, lots of fun... */
514 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
515 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
516 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
518 return sprintf(buf, "%s\n", "unknown");
521 static ssize_t set_performance_level(struct device *dev,
522 struct device_attribute *attr, const char *buf,
527 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
528 const struct sabi_performance_level *level =
529 &sabi_config->performance_levels[i];
530 if (!strncasecmp(level->name, buf, strlen(level->name))) {
531 sabi_set_command(sabi_config->commands.set_performance_level,
536 if (!sabi_config->performance_levels[i].name)
541 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
542 get_performance_level, set_performance_level);
545 static struct dmi_system_id __initdata samsung_dmi_table[] = {
548 DMI_MATCH(DMI_SYS_VENDOR,
549 "SAMSUNG ELECTRONICS CO., LTD."),
550 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
555 DMI_MATCH(DMI_SYS_VENDOR,
556 "SAMSUNG ELECTRONICS CO., LTD."),
557 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
562 DMI_MATCH(DMI_SYS_VENDOR,
563 "SAMSUNG ELECTRONICS CO., LTD."),
564 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
569 DMI_MATCH(DMI_SYS_VENDOR,
570 "SAMSUNG ELECTRONICS CO., LTD."),
571 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
576 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
578 static int find_signature(void __iomem *memcheck, const char *testStr)
583 for (loca = 0; loca < 0xffff; loca++) {
584 char temp = readb(memcheck + loca);
586 if (temp == testStr[i]) {
587 if (i == strlen(testStr)-1)
597 static int __init samsung_init(void)
599 struct backlight_properties props;
600 struct sabi_retval sretval;
606 mutex_init(&sabi_mutex);
607 handle_backlight = true;
610 /* Don't handle backlight here if the acpi video already handle it */
611 if (acpi_video_backlight_support())
612 handle_backlight = false;
615 if (!force && !dmi_check_system(samsung_dmi_table))
618 f0000_segment = ioremap_nocache(0xf0000, 0xffff);
619 if (!f0000_segment) {
621 pr_err("Can't map the segment at 0xf0000\n");
625 /* Try to find one of the signatures in memory to find the header */
626 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
627 sabi_config = &sabi_configs[i];
628 loca = find_signature(f0000_segment, sabi_config->test_string);
633 if (loca == 0xffff) {
635 pr_err("This computer does not support SABI\n");
636 goto error_no_signature;
639 /* point to the SMI port Number */
641 sabi = (f0000_segment + loca);
644 printk(KERN_DEBUG "This computer supports SABI==%x\n",
646 printk(KERN_DEBUG "SABI header:\n");
647 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
648 readw(sabi + sabi_config->header_offsets.port));
649 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
650 readb(sabi + sabi_config->header_offsets.iface_func));
651 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
652 readb(sabi + sabi_config->header_offsets.en_mem));
653 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
654 readb(sabi + sabi_config->header_offsets.re_mem));
655 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
656 readw(sabi + sabi_config->header_offsets.data_offset));
657 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
658 readw(sabi + sabi_config->header_offsets.data_segment));
661 /* Get a pointer to the SABI Interface */
662 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
663 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
664 sabi_iface = ioremap_nocache(ifaceP, 16);
666 pr_err("Can't remap %x\n", ifaceP);
667 goto error_no_signature;
670 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
671 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
673 if (handle_backlight)
677 retval = sabi_get_command(sabi_config->commands.get_brightness,
679 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
682 /* Turn on "Linux" mode in the BIOS */
683 if (sabi_config->commands.set_linux != 0xff) {
684 retval = sabi_set_command(sabi_config->commands.set_linux,
687 pr_warn("Linux mode was not set!\n");
688 goto error_no_platform;
692 /* Check for stepping quirk */
693 if (handle_backlight)
694 check_for_stepping_quirk();
697 /* Only log that if we are really on a sabi platform */
698 if (acpi_video_backlight_support())
699 pr_info("Backlight controlled by ACPI video driver\n");
702 /* knock up a platform device to hang stuff off of */
703 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
705 goto error_no_platform;
707 if (!handle_backlight)
710 /* create a backlight device to talk to this one */
711 memset(&props, 0, sizeof(struct backlight_properties));
712 props.type = BACKLIGHT_PLATFORM;
713 props.max_brightness = sabi_config->max_brightness -
714 sabi_config->min_brightness;
715 backlight_device = backlight_device_register("samsung", &sdev->dev,
716 NULL, &backlight_ops,
718 if (IS_ERR(backlight_device))
719 goto error_no_backlight;
721 backlight_device->props.brightness = read_brightness();
722 backlight_device->props.power = FB_BLANK_UNBLANK;
723 backlight_update_status(backlight_device);
726 retval = init_wireless(sdev);
730 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
732 goto error_file_create;
740 backlight_device_unregister(backlight_device);
743 platform_device_unregister(sdev);
749 iounmap(f0000_segment);
753 static void __exit samsung_exit(void)
755 /* Turn off "Linux" mode in the BIOS */
756 if (sabi_config->commands.set_linux != 0xff)
757 sabi_set_command(sabi_config->commands.set_linux, 0x80);
759 device_remove_file(&sdev->dev, &dev_attr_performance_level);
760 backlight_device_unregister(backlight_device);
763 iounmap(f0000_segment);
764 platform_device_unregister(sdev);
767 module_init(samsung_init);
768 module_exit(samsung_exit);
770 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
771 MODULE_DESCRIPTION("Samsung Backlight driver");
772 MODULE_LICENSE("GPL");