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>
25 #include <linux/efi.h>
28 * This driver is needed because a number of Samsung laptops do not hook
29 * their control settings through ACPI. So we have to poke around in the
30 * BIOS to do things like brightness values, and "special" key controls.
34 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
35 * be reserved by the BIOS (which really doesn't make much sense), we tell
36 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
38 #define MAX_BRIGHT 0x07
41 #define SABI_IFACE_MAIN 0x00
42 #define SABI_IFACE_SUB 0x02
43 #define SABI_IFACE_COMPLETE 0x04
44 #define SABI_IFACE_DATA 0x05
46 /* Structure to get data back to the calling function */
51 struct sabi_header_offsets {
60 struct sabi_commands {
62 * Brightness is 0 - 8, as described above.
63 * Value 0 is for the BIOS to use
70 * 0x00 - wireless is off
71 * 0x01 - wireless is on
75 * TODO, verify 3G is correct, that doesn't seem right...
77 u8 get_wireless_button;
78 u8 set_wireless_button;
80 /* 0 is off, 1 is on */
85 * 0x80 or 0x00 - no action
86 * 0x81 - recovery key pressed
92 * on seclinux: 0 is low, 1 is high,
93 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
95 u8 get_performance_level;
96 u8 set_performance_level;
99 * Tell the BIOS that Linux is running on this machine.
100 * 81 is on, 80 is off
105 struct sabi_performance_level {
111 const char *test_string;
113 const struct sabi_header_offsets header_offsets;
114 const struct sabi_commands commands;
115 const struct sabi_performance_level performance_levels[4];
120 static const struct sabi_config sabi_configs[] = {
122 .test_string = "SECLINUX",
124 .main_function = 0x4c49,
132 .data_segment = 0x07,
136 .get_brightness = 0x00,
137 .set_brightness = 0x01,
139 .get_wireless_button = 0x02,
140 .set_wireless_button = 0x03,
142 .get_backlight = 0x04,
143 .set_backlight = 0x05,
145 .get_recovery_mode = 0x06,
146 .set_recovery_mode = 0x07,
148 .get_performance_level = 0x08,
149 .set_performance_level = 0x09,
154 .performance_levels = {
169 .test_string = "SwSmi@",
171 .main_function = 0x5843,
179 .data_segment = 0x07,
183 .get_brightness = 0x10,
184 .set_brightness = 0x11,
186 .get_wireless_button = 0x12,
187 .set_wireless_button = 0x13,
189 .get_backlight = 0x2d,
190 .set_backlight = 0x2e,
192 .get_recovery_mode = 0xff,
193 .set_recovery_mode = 0xff,
195 .get_performance_level = 0x31,
196 .set_performance_level = 0x32,
201 .performance_levels = {
222 static const struct sabi_config *sabi_config;
224 static void __iomem *sabi;
225 static void __iomem *sabi_iface;
226 static void __iomem *f0000_segment;
227 static struct backlight_device *backlight_device;
228 static struct mutex sabi_mutex;
229 static struct platform_device *sdev;
230 static struct rfkill *rfk;
231 static bool handle_backlight;
232 static bool has_stepping_quirk;
235 module_param(force, bool, 0);
236 MODULE_PARM_DESC(force,
237 "Disable the DMI check and forces the driver to be loaded");
240 module_param(debug, bool, S_IRUGO | S_IWUSR);
241 MODULE_PARM_DESC(debug, "Debug enabled or not");
243 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
246 u16 port = readw(sabi + sabi_config->header_offsets.port);
247 u8 complete, iface_data;
249 mutex_lock(&sabi_mutex);
251 /* enable memory to be able to write to it */
252 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
254 /* write out the command */
255 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
256 writew(command, sabi_iface + SABI_IFACE_SUB);
257 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
258 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
260 /* write protect memory to make it safe */
261 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
263 /* see if the command actually succeeded */
264 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
265 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
266 if (complete != 0xaa || iface_data == 0xff) {
267 pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
268 command, complete, iface_data);
273 * Save off the data into a structure so the caller use it.
274 * Right now we only want the first 4 bytes,
275 * There are commands that need more, but not for the ones we
276 * currently care about.
278 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
279 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
280 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
281 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
284 mutex_unlock(&sabi_mutex);
289 static int sabi_set_command(u8 command, u8 data)
292 u16 port = readw(sabi + sabi_config->header_offsets.port);
293 u8 complete, iface_data;
295 mutex_lock(&sabi_mutex);
297 /* enable memory to be able to write to it */
298 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
300 /* write out the command */
301 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
302 writew(command, sabi_iface + SABI_IFACE_SUB);
303 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
304 writeb(data, sabi_iface + SABI_IFACE_DATA);
305 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
307 /* write protect memory to make it safe */
308 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
310 /* see if the command actually succeeded */
311 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
312 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
313 if (complete != 0xaa || iface_data == 0xff) {
314 pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
315 command, complete, iface_data);
319 mutex_unlock(&sabi_mutex);
323 static void test_backlight(void)
325 struct sabi_retval sretval;
327 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
328 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
330 sabi_set_command(sabi_config->commands.set_backlight, 0);
331 printk(KERN_DEBUG "backlight should be off\n");
333 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
334 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
338 sabi_set_command(sabi_config->commands.set_backlight, 1);
339 printk(KERN_DEBUG "backlight should be on\n");
341 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
342 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
345 static void test_wireless(void)
347 struct sabi_retval sretval;
349 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
350 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
352 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
353 printk(KERN_DEBUG "wireless led should be off\n");
355 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
356 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
360 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
361 printk(KERN_DEBUG "wireless led should be on\n");
363 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
364 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
367 static u8 read_brightness(void)
369 struct sabi_retval sretval;
370 int user_brightness = 0;
373 retval = sabi_get_command(sabi_config->commands.get_brightness,
376 user_brightness = sretval.retval[0];
377 if (user_brightness > sabi_config->min_brightness)
378 user_brightness -= sabi_config->min_brightness;
382 return user_brightness;
385 static void set_brightness(u8 user_brightness)
387 u8 user_level = user_brightness + sabi_config->min_brightness;
389 if (has_stepping_quirk && user_level != 0) {
391 * short circuit if the specified level is what's already set
392 * to prevent the screen from flickering needlessly
394 if (user_brightness == read_brightness())
397 sabi_set_command(sabi_config->commands.set_brightness, 0);
400 sabi_set_command(sabi_config->commands.set_brightness, user_level);
403 static int get_brightness(struct backlight_device *bd)
405 return (int)read_brightness();
408 static void check_for_stepping_quirk(void)
412 u8 orig_level = read_brightness();
415 * Some laptops exhibit the strange behaviour of stepping toward
416 * (rather than setting) the brightness except when changing to/from
417 * brightness level 0. This behaviour is checked for here and worked
418 * around in set_brightness.
424 initial_level = read_brightness();
426 if (initial_level <= 2)
427 check_level = initial_level + 2;
429 check_level = initial_level - 2;
431 has_stepping_quirk = false;
432 set_brightness(check_level);
434 if (read_brightness() != check_level) {
435 has_stepping_quirk = true;
436 pr_info("enabled workaround for brightness stepping quirk\n");
439 set_brightness(orig_level);
442 static int update_status(struct backlight_device *bd)
444 set_brightness(bd->props.brightness);
446 if (bd->props.power == FB_BLANK_UNBLANK)
447 sabi_set_command(sabi_config->commands.set_backlight, 1);
449 sabi_set_command(sabi_config->commands.set_backlight, 0);
453 static const struct backlight_ops backlight_ops = {
454 .get_brightness = get_brightness,
455 .update_status = update_status,
458 static int rfkill_set(void *data, bool blocked)
460 /* Do something with blocked...*/
462 * blocked == false is on
463 * blocked == true is off
466 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
468 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
473 static struct rfkill_ops rfkill_ops = {
474 .set_block = rfkill_set,
477 static int init_wireless(struct platform_device *sdev)
481 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
486 retval = rfkill_register(rfk);
495 static void destroy_wireless(void)
497 rfkill_unregister(rfk);
501 static ssize_t get_performance_level(struct device *dev,
502 struct device_attribute *attr, char *buf)
504 struct sabi_retval sretval;
509 retval = sabi_get_command(sabi_config->commands.get_performance_level,
514 /* The logic is backwards, yeah, lots of fun... */
515 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
516 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
517 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
519 return sprintf(buf, "%s\n", "unknown");
522 static ssize_t set_performance_level(struct device *dev,
523 struct device_attribute *attr, const char *buf,
528 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
529 const struct sabi_performance_level *level =
530 &sabi_config->performance_levels[i];
531 if (!strncasecmp(level->name, buf, strlen(level->name))) {
532 sabi_set_command(sabi_config->commands.set_performance_level,
537 if (!sabi_config->performance_levels[i].name)
542 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
543 get_performance_level, set_performance_level);
546 static struct dmi_system_id __initdata samsung_dmi_table[] = {
549 DMI_MATCH(DMI_SYS_VENDOR,
550 "SAMSUNG ELECTRONICS CO., LTD."),
551 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
556 DMI_MATCH(DMI_SYS_VENDOR,
557 "SAMSUNG ELECTRONICS CO., LTD."),
558 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
563 DMI_MATCH(DMI_SYS_VENDOR,
564 "SAMSUNG ELECTRONICS CO., LTD."),
565 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
570 DMI_MATCH(DMI_SYS_VENDOR,
571 "SAMSUNG ELECTRONICS CO., LTD."),
572 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
577 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
579 static int find_signature(void __iomem *memcheck, const char *testStr)
584 for (loca = 0; loca < 0xffff; loca++) {
585 char temp = readb(memcheck + loca);
587 if (temp == testStr[i]) {
588 if (i == strlen(testStr)-1)
598 static int __init samsung_init(void)
600 struct backlight_properties props;
601 struct sabi_retval sretval;
607 if (efi_enabled(EFI_BOOT))
610 mutex_init(&sabi_mutex);
611 handle_backlight = true;
614 /* Don't handle backlight here if the acpi video already handle it */
615 if (acpi_video_backlight_support())
616 handle_backlight = false;
619 if (!force && !dmi_check_system(samsung_dmi_table))
622 f0000_segment = ioremap_nocache(0xf0000, 0xffff);
623 if (!f0000_segment) {
625 pr_err("Can't map the segment at 0xf0000\n");
629 /* Try to find one of the signatures in memory to find the header */
630 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
631 sabi_config = &sabi_configs[i];
632 loca = find_signature(f0000_segment, sabi_config->test_string);
637 if (loca == 0xffff) {
639 pr_err("This computer does not support SABI\n");
640 goto error_no_signature;
643 /* point to the SMI port Number */
645 sabi = (f0000_segment + loca);
648 printk(KERN_DEBUG "This computer supports SABI==%x\n",
650 printk(KERN_DEBUG "SABI header:\n");
651 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
652 readw(sabi + sabi_config->header_offsets.port));
653 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
654 readb(sabi + sabi_config->header_offsets.iface_func));
655 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
656 readb(sabi + sabi_config->header_offsets.en_mem));
657 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
658 readb(sabi + sabi_config->header_offsets.re_mem));
659 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
660 readw(sabi + sabi_config->header_offsets.data_offset));
661 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
662 readw(sabi + sabi_config->header_offsets.data_segment));
665 /* Get a pointer to the SABI Interface */
666 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
667 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
668 sabi_iface = ioremap_nocache(ifaceP, 16);
670 pr_err("Can't remap %x\n", ifaceP);
671 goto error_no_signature;
674 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
675 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
677 if (handle_backlight)
681 retval = sabi_get_command(sabi_config->commands.get_brightness,
683 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
686 /* Turn on "Linux" mode in the BIOS */
687 if (sabi_config->commands.set_linux != 0xff) {
688 retval = sabi_set_command(sabi_config->commands.set_linux,
691 pr_warn("Linux mode was not set!\n");
692 goto error_no_platform;
696 /* Check for stepping quirk */
697 if (handle_backlight)
698 check_for_stepping_quirk();
701 /* Only log that if we are really on a sabi platform */
702 if (acpi_video_backlight_support())
703 pr_info("Backlight controlled by ACPI video driver\n");
706 /* knock up a platform device to hang stuff off of */
707 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
709 goto error_no_platform;
711 if (!handle_backlight)
714 /* create a backlight device to talk to this one */
715 memset(&props, 0, sizeof(struct backlight_properties));
716 props.type = BACKLIGHT_PLATFORM;
717 props.max_brightness = sabi_config->max_brightness -
718 sabi_config->min_brightness;
719 backlight_device = backlight_device_register("samsung", &sdev->dev,
720 NULL, &backlight_ops,
722 if (IS_ERR(backlight_device))
723 goto error_no_backlight;
725 backlight_device->props.brightness = read_brightness();
726 backlight_device->props.power = FB_BLANK_UNBLANK;
727 backlight_update_status(backlight_device);
730 retval = init_wireless(sdev);
734 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
736 goto error_file_create;
744 backlight_device_unregister(backlight_device);
747 platform_device_unregister(sdev);
753 iounmap(f0000_segment);
757 static void __exit samsung_exit(void)
759 /* Turn off "Linux" mode in the BIOS */
760 if (sabi_config->commands.set_linux != 0xff)
761 sabi_set_command(sabi_config->commands.set_linux, 0x80);
763 device_remove_file(&sdev->dev, &dev_attr_performance_level);
764 backlight_device_unregister(backlight_device);
767 iounmap(f0000_segment);
768 platform_device_unregister(sdev);
771 module_init(samsung_init);
772 module_exit(samsung_exit);
774 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
775 MODULE_DESCRIPTION("Samsung Backlight driver");
776 MODULE_LICENSE("GPL");