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>
26 * This driver is needed because a number of Samsung laptops do not hook
27 * their control settings through ACPI. So we have to poke around in the
28 * BIOS to do things like brightness values, and "special" key controls.
32 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
33 * be reserved by the BIOS (which really doesn't make much sense), we tell
34 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
36 #define MAX_BRIGHT 0x07
39 #define SABI_IFACE_MAIN 0x00
40 #define SABI_IFACE_SUB 0x02
41 #define SABI_IFACE_COMPLETE 0x04
42 #define SABI_IFACE_DATA 0x05
44 /* Structure to get data back to the calling function */
49 struct sabi_header_offsets {
58 struct sabi_commands {
60 * Brightness is 0 - 8, as described above.
61 * Value 0 is for the BIOS to use
68 * 0x00 - wireless is off
69 * 0x01 - wireless is on
73 * TODO, verify 3G is correct, that doesn't seem right...
75 u8 get_wireless_button;
76 u8 set_wireless_button;
78 /* 0 is off, 1 is on */
83 * 0x80 or 0x00 - no action
84 * 0x81 - recovery key pressed
90 * on seclinux: 0 is low, 1 is high,
91 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
93 u8 get_performance_level;
94 u8 set_performance_level;
97 * Tell the BIOS that Linux is running on this machine.
103 struct sabi_performance_level {
109 const char *test_string;
111 const struct sabi_header_offsets header_offsets;
112 const struct sabi_commands commands;
113 const struct sabi_performance_level performance_levels[4];
118 static const struct sabi_config sabi_configs[] = {
120 .test_string = "SECLINUX",
122 .main_function = 0x4c49,
130 .data_segment = 0x07,
134 .get_brightness = 0x00,
135 .set_brightness = 0x01,
137 .get_wireless_button = 0x02,
138 .set_wireless_button = 0x03,
140 .get_backlight = 0x04,
141 .set_backlight = 0x05,
143 .get_recovery_mode = 0x06,
144 .set_recovery_mode = 0x07,
146 .get_performance_level = 0x08,
147 .set_performance_level = 0x09,
152 .performance_levels = {
167 .test_string = "SwSmi@",
169 .main_function = 0x5843,
177 .data_segment = 0x07,
181 .get_brightness = 0x10,
182 .set_brightness = 0x11,
184 .get_wireless_button = 0x12,
185 .set_wireless_button = 0x13,
187 .get_backlight = 0x2d,
188 .set_backlight = 0x2e,
190 .get_recovery_mode = 0xff,
191 .set_recovery_mode = 0xff,
193 .get_performance_level = 0x31,
194 .set_performance_level = 0x32,
199 .performance_levels = {
220 static const struct sabi_config *sabi_config;
222 static void __iomem *sabi;
223 static void __iomem *sabi_iface;
224 static void __iomem *f0000_segment;
225 static struct backlight_device *backlight_device;
226 static struct mutex sabi_mutex;
227 static struct platform_device *sdev;
228 static struct rfkill *rfk;
229 static bool has_stepping_quirk;
232 module_param(force, bool, 0);
233 MODULE_PARM_DESC(force,
234 "Disable the DMI check and forces the driver to be loaded");
237 module_param(debug, bool, S_IRUGO | S_IWUSR);
238 MODULE_PARM_DESC(debug, "Debug enabled or not");
240 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
243 u16 port = readw(sabi + sabi_config->header_offsets.port);
244 u8 complete, iface_data;
246 mutex_lock(&sabi_mutex);
248 /* enable memory to be able to write to it */
249 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
251 /* write out the command */
252 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
253 writew(command, sabi_iface + SABI_IFACE_SUB);
254 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
255 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
257 /* write protect memory to make it safe */
258 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
260 /* see if the command actually succeeded */
261 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
262 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
263 if (complete != 0xaa || iface_data == 0xff) {
264 pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
265 command, complete, iface_data);
270 * Save off the data into a structure so the caller use it.
271 * Right now we only want the first 4 bytes,
272 * There are commands that need more, but not for the ones we
273 * currently care about.
275 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
276 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
277 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
278 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
281 mutex_unlock(&sabi_mutex);
286 static int sabi_set_command(u8 command, u8 data)
289 u16 port = readw(sabi + sabi_config->header_offsets.port);
290 u8 complete, iface_data;
292 mutex_lock(&sabi_mutex);
294 /* enable memory to be able to write to it */
295 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
297 /* write out the command */
298 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
299 writew(command, sabi_iface + SABI_IFACE_SUB);
300 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
301 writeb(data, sabi_iface + SABI_IFACE_DATA);
302 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
304 /* write protect memory to make it safe */
305 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
307 /* see if the command actually succeeded */
308 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
309 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
310 if (complete != 0xaa || iface_data == 0xff) {
311 pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
312 command, complete, iface_data);
316 mutex_unlock(&sabi_mutex);
320 static void test_backlight(void)
322 struct sabi_retval sretval;
324 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
325 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
327 sabi_set_command(sabi_config->commands.set_backlight, 0);
328 printk(KERN_DEBUG "backlight should be off\n");
330 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
331 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
335 sabi_set_command(sabi_config->commands.set_backlight, 1);
336 printk(KERN_DEBUG "backlight should be on\n");
338 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
339 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
342 static void test_wireless(void)
344 struct sabi_retval sretval;
346 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
347 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
349 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
350 printk(KERN_DEBUG "wireless led should be off\n");
352 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
353 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
357 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
358 printk(KERN_DEBUG "wireless led should be on\n");
360 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
361 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
364 static u8 read_brightness(void)
366 struct sabi_retval sretval;
367 int user_brightness = 0;
370 retval = sabi_get_command(sabi_config->commands.get_brightness,
373 user_brightness = sretval.retval[0];
374 if (user_brightness > sabi_config->min_brightness)
375 user_brightness -= sabi_config->min_brightness;
379 return user_brightness;
382 static void set_brightness(u8 user_brightness)
384 u8 user_level = user_brightness + sabi_config->min_brightness;
386 if (has_stepping_quirk && user_level != 0) {
388 * short circuit if the specified level is what's already set
389 * to prevent the screen from flickering needlessly
391 if (user_brightness == read_brightness())
394 sabi_set_command(sabi_config->commands.set_brightness, 0);
397 sabi_set_command(sabi_config->commands.set_brightness, user_level);
400 static int get_brightness(struct backlight_device *bd)
402 return (int)read_brightness();
405 static void check_for_stepping_quirk(void)
409 u8 orig_level = read_brightness();
412 * Some laptops exhibit the strange behaviour of stepping toward
413 * (rather than setting) the brightness except when changing to/from
414 * brightness level 0. This behaviour is checked for here and worked
415 * around in set_brightness.
421 initial_level = read_brightness();
423 if (initial_level <= 2)
424 check_level = initial_level + 2;
426 check_level = initial_level - 2;
428 has_stepping_quirk = false;
429 set_brightness(check_level);
431 if (read_brightness() != check_level) {
432 has_stepping_quirk = true;
433 pr_info("enabled workaround for brightness stepping quirk\n");
436 set_brightness(orig_level);
439 static int update_status(struct backlight_device *bd)
441 set_brightness(bd->props.brightness);
443 if (bd->props.power == FB_BLANK_UNBLANK)
444 sabi_set_command(sabi_config->commands.set_backlight, 1);
446 sabi_set_command(sabi_config->commands.set_backlight, 0);
450 static const struct backlight_ops backlight_ops = {
451 .get_brightness = get_brightness,
452 .update_status = update_status,
455 static int rfkill_set(void *data, bool blocked)
457 /* Do something with blocked...*/
459 * blocked == false is on
460 * blocked == true is off
463 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
465 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
470 static struct rfkill_ops rfkill_ops = {
471 .set_block = rfkill_set,
474 static int init_wireless(struct platform_device *sdev)
478 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
483 retval = rfkill_register(rfk);
492 static void destroy_wireless(void)
494 rfkill_unregister(rfk);
498 static ssize_t get_performance_level(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct sabi_retval sretval;
506 retval = sabi_get_command(sabi_config->commands.get_performance_level,
511 /* The logic is backwards, yeah, lots of fun... */
512 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
513 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
514 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
516 return sprintf(buf, "%s\n", "unknown");
519 static ssize_t set_performance_level(struct device *dev,
520 struct device_attribute *attr, const char *buf,
525 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
526 const struct sabi_performance_level *level =
527 &sabi_config->performance_levels[i];
528 if (!strncasecmp(level->name, buf, strlen(level->name))) {
529 sabi_set_command(sabi_config->commands.set_performance_level,
534 if (!sabi_config->performance_levels[i].name)
539 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
540 get_performance_level, set_performance_level);
543 static struct dmi_system_id __initdata samsung_dmi_table[] = {
546 DMI_MATCH(DMI_SYS_VENDOR,
547 "SAMSUNG ELECTRONICS CO., LTD."),
548 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
553 DMI_MATCH(DMI_SYS_VENDOR,
554 "SAMSUNG ELECTRONICS CO., LTD."),
555 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
560 DMI_MATCH(DMI_SYS_VENDOR,
561 "SAMSUNG ELECTRONICS CO., LTD."),
562 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
567 DMI_MATCH(DMI_SYS_VENDOR,
568 "SAMSUNG ELECTRONICS CO., LTD."),
569 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
574 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
576 static int find_signature(void __iomem *memcheck, const char *testStr)
581 for (loca = 0; loca < 0xffff; loca++) {
582 char temp = readb(memcheck + loca);
584 if (temp == testStr[i]) {
585 if (i == strlen(testStr)-1)
595 static int __init samsung_init(void)
597 struct backlight_properties props;
598 struct sabi_retval sretval;
604 mutex_init(&sabi_mutex);
606 if (!force && !dmi_check_system(samsung_dmi_table))
609 f0000_segment = ioremap_nocache(0xf0000, 0xffff);
610 if (!f0000_segment) {
612 pr_err("Can't map the segment at 0xf0000\n");
616 /* Try to find one of the signatures in memory to find the header */
617 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
618 sabi_config = &sabi_configs[i];
619 loca = find_signature(f0000_segment, sabi_config->test_string);
624 if (loca == 0xffff) {
626 pr_err("This computer does not support SABI\n");
627 goto error_no_signature;
630 /* point to the SMI port Number */
632 sabi = (f0000_segment + loca);
635 printk(KERN_DEBUG "This computer supports SABI==%x\n",
637 printk(KERN_DEBUG "SABI header:\n");
638 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
639 readw(sabi + sabi_config->header_offsets.port));
640 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
641 readb(sabi + sabi_config->header_offsets.iface_func));
642 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
643 readb(sabi + sabi_config->header_offsets.en_mem));
644 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
645 readb(sabi + sabi_config->header_offsets.re_mem));
646 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
647 readw(sabi + sabi_config->header_offsets.data_offset));
648 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
649 readw(sabi + sabi_config->header_offsets.data_segment));
652 /* Get a pointer to the SABI Interface */
653 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
654 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
655 sabi_iface = ioremap_nocache(ifaceP, 16);
657 pr_err("Can't remap %x\n", ifaceP);
658 goto error_no_signature;
661 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
662 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
667 retval = sabi_get_command(sabi_config->commands.get_brightness,
669 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
672 /* Turn on "Linux" mode in the BIOS */
673 if (sabi_config->commands.set_linux != 0xff) {
674 retval = sabi_set_command(sabi_config->commands.set_linux,
677 pr_warn("Linux mode was not set!\n");
678 goto error_no_platform;
682 /* Check for stepping quirk */
683 check_for_stepping_quirk();
685 /* knock up a platform device to hang stuff off of */
686 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
688 goto error_no_platform;
690 /* create a backlight device to talk to this one */
691 memset(&props, 0, sizeof(struct backlight_properties));
692 props.type = BACKLIGHT_PLATFORM;
693 props.max_brightness = sabi_config->max_brightness -
694 sabi_config->min_brightness;
695 backlight_device = backlight_device_register("samsung", &sdev->dev,
696 NULL, &backlight_ops,
698 if (IS_ERR(backlight_device))
699 goto error_no_backlight;
701 backlight_device->props.brightness = read_brightness();
702 backlight_device->props.power = FB_BLANK_UNBLANK;
703 backlight_update_status(backlight_device);
705 retval = init_wireless(sdev);
709 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
711 goto error_file_create;
719 backlight_device_unregister(backlight_device);
722 platform_device_unregister(sdev);
728 iounmap(f0000_segment);
732 static void __exit samsung_exit(void)
734 /* Turn off "Linux" mode in the BIOS */
735 if (sabi_config->commands.set_linux != 0xff)
736 sabi_set_command(sabi_config->commands.set_linux, 0x80);
738 device_remove_file(&sdev->dev, &dev_attr_performance_level);
739 backlight_device_unregister(backlight_device);
742 iounmap(f0000_segment);
743 platform_device_unregister(sdev);
746 module_init(samsung_init);
747 module_exit(samsung_exit);
749 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
750 MODULE_DESCRIPTION("Samsung Backlight driver");
751 MODULE_LICENSE("GPL");