2 * Samsung N130 Laptop driver
4 * Copyright (C) 2009 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009 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 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
17 #include <linux/backlight.h>
19 #include <linux/dmi.h>
20 #include <linux/platform_device.h>
21 #include <linux/rfkill.h>
24 * This driver is needed because a number of Samsung laptops do not hook
25 * their control settings through ACPI. So we have to poke around in the
26 * BIOS to do things like brightness values, and "special" key controls.
30 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
31 * be reserved by the BIOS (which really doesn't make much sense), we tell
32 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
34 #define MAX_BRIGHT 0x07
37 #define SABI_IFACE_MAIN 0x00
38 #define SABI_IFACE_SUB 0x02
39 #define SABI_IFACE_COMPLETE 0x04
40 #define SABI_IFACE_DATA 0x05
42 /* Structure to get data back to the calling function */
47 struct sabi_header_offsets {
56 struct sabi_commands {
57 /* Brightness is 0 - 8, as described above. Value 0 is for the BIOS to use */
62 * 0x00 - wireless is off
63 * 0x01 - wireless is on
67 * TODO, verify 3G is correct, that doesn't seem right...
69 u8 get_wireless_button;
70 u8 set_wireless_button;
72 /* 0 is off, 1 is on */
77 * 0x80 or 0x00 - no action
78 * 0x81 - recovery key pressed
84 * on seclinux: 0 is low, 1 is high,
85 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
87 u8 get_performance_level;
88 u8 set_performance_level;
91 * Tell the BIOS that Linux is running on this machine.
97 struct sabi_performance_level {
103 const char *test_string;
105 struct sabi_header_offsets header_offsets;
106 struct sabi_commands commands;
107 struct sabi_performance_level performance_levels[4];
110 static struct sabi_config sabi_configs[] = {
112 .test_string = "SECLINUX",
114 .main_function = 0x4c59,
122 .data_segment = 0x07,
126 .get_brightness = 0x00,
127 .set_brightness = 0x01,
129 .get_wireless_button = 0x02,
130 .set_wireless_button = 0x03,
132 .get_backlight = 0x04,
133 .set_backlight = 0x05,
135 .get_recovery_mode = 0x06,
136 .set_recovery_mode = 0x07,
138 .get_performance_level = 0x08,
139 .set_performance_level = 0x09,
144 .performance_levels = {
157 .test_string = "SwSmi@",
159 .main_function = 0x5843,
167 .data_segment = 0x07,
171 .get_brightness = 0x10,
172 .set_brightness = 0x11,
174 .get_wireless_button = 0x12,
175 .set_wireless_button = 0x13,
177 .get_backlight = 0x2d,
178 .set_backlight = 0x2e,
180 .get_recovery_mode = 0xff,
181 .set_recovery_mode = 0xff,
183 .get_performance_level = 0x31,
184 .set_performance_level = 0x32,
189 .performance_levels = {
208 static struct sabi_config *sabi_config;
210 static void __iomem *sabi;
211 static void __iomem *sabi_iface;
212 static void __iomem *f0000_segment;
213 static struct backlight_device *backlight_device;
214 static struct mutex sabi_mutex;
215 static struct platform_device *sdev;
216 static struct rfkill *rfk;
219 module_param(force, bool, 0);
220 MODULE_PARM_DESC(force,
221 "Disable the DMI check and forces the driver to be loaded");
224 module_param(debug, bool, S_IRUGO | S_IWUSR);
225 MODULE_PARM_DESC(debug, "Debug enabled or not");
227 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
230 u16 port = readw(sabi + sabi_config->header_offsets.port);
232 mutex_lock(&sabi_mutex);
234 /* enable memory to be able to write to it */
235 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
237 /* write out the command */
238 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
239 writew(command, sabi_iface + SABI_IFACE_SUB);
240 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
241 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
243 /* write protect memory to make it safe */
244 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
246 /* see if the command actually succeeded */
247 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
248 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
251 * Save off the data into a structure so the caller use it.
252 * Right now we only care about the first 4 bytes,
253 * I suppose there are commands that need more, but I don't
256 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
257 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
258 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
259 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
263 /* Something bad happened, so report it and error out */
264 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
265 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
266 readb(sabi_iface + SABI_IFACE_DATA));
269 mutex_unlock(&sabi_mutex);
274 static int sabi_set_command(u8 command, u8 data)
277 u16 port = readw(sabi + sabi_config->header_offsets.port);
279 mutex_lock(&sabi_mutex);
281 /* enable memory to be able to write to it */
282 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
284 /* write out the command */
285 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
286 writew(command, sabi_iface + SABI_IFACE_SUB);
287 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
288 writeb(data, sabi_iface + SABI_IFACE_DATA);
289 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
291 /* write protect memory to make it safe */
292 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
294 /* see if the command actually succeeded */
295 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
296 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
301 /* Something bad happened, so report it and error out */
302 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
303 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
304 readb(sabi_iface + SABI_IFACE_DATA));
307 mutex_unlock(&sabi_mutex);
311 static void test_backlight(void)
313 struct sabi_retval sretval;
315 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
316 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
318 sabi_set_command(sabi_config->commands.set_backlight, 0);
319 printk(KERN_DEBUG "backlight should be off\n");
321 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
322 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
326 sabi_set_command(sabi_config->commands.set_backlight, 1);
327 printk(KERN_DEBUG "backlight should be on\n");
329 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
330 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
333 static void test_wireless(void)
335 struct sabi_retval sretval;
337 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
338 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
340 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
341 printk(KERN_DEBUG "wireless led should be off\n");
343 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
344 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
348 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
349 printk(KERN_DEBUG "wireless led should be on\n");
351 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
352 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
355 static u8 read_brightness(void)
357 struct sabi_retval sretval;
358 int user_brightness = 0;
361 retval = sabi_get_command(sabi_config->commands.get_brightness, &sretval);
363 user_brightness = sretval.retval[0];
364 if (user_brightness != 0)
366 return user_brightness;
369 static void set_brightness(u8 user_brightness)
371 sabi_set_command(sabi_config->commands.set_brightness, user_brightness + 1);
374 static int get_brightness(struct backlight_device *bd)
376 return (int)read_brightness();
379 static int update_status(struct backlight_device *bd)
381 set_brightness(bd->props.brightness);
383 if (bd->props.power == FB_BLANK_UNBLANK)
384 sabi_set_command(sabi_config->commands.set_backlight, 1);
386 sabi_set_command(sabi_config->commands.set_backlight, 0);
390 static const struct backlight_ops backlight_ops = {
391 .get_brightness = get_brightness,
392 .update_status = update_status,
395 static int rfkill_set(void *data, bool blocked)
397 /* Do something with blocked...*/
399 * blocked == false is on
400 * blocked == true is off
403 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
405 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
410 static struct rfkill_ops rfkill_ops = {
411 .set_block = rfkill_set,
414 static int init_wireless(struct platform_device *sdev)
418 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
423 retval = rfkill_register(rfk);
432 static void destroy_wireless(void)
434 rfkill_unregister(rfk);
438 static ssize_t get_performance_level(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct sabi_retval sretval;
446 retval = sabi_get_command(sabi_config->commands.get_performance_level, &sretval);
450 /* The logic is backwards, yeah, lots of fun... */
451 for (pLevel = 0; sabi_config->performance_levels[pLevel].name; ++pLevel) {
452 if (sretval.retval[0] == sabi_config->performance_levels[pLevel].value)
453 return sprintf(buf, "%s\n", sabi_config->performance_levels[pLevel].name);
455 return sprintf(buf, "%s\n", "unknown");
458 static ssize_t set_performance_level(struct device *dev,
459 struct device_attribute *attr, const char *buf,
464 for (pLevel = 0; sabi_config->performance_levels[pLevel].name; ++pLevel) {
465 struct sabi_performance_level *level =
466 &sabi_config->performance_levels[pLevel];
467 if (!strncasecmp(level->name, buf, strlen(level->name))) {
468 sabi_set_command(sabi_config->commands.set_performance_level,
473 if (!sabi_config->performance_levels[pLevel].name)
478 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
479 get_performance_level, set_performance_level);
482 static int __init dmi_check_cb(const struct dmi_system_id *id)
484 printk(KERN_INFO KBUILD_MODNAME ": found laptop model '%s'\n",
489 static struct dmi_system_id __initdata samsung_dmi_table[] = {
493 DMI_MATCH(DMI_SYS_VENDOR,
494 "SAMSUNG ELECTRONICS CO., LTD."),
495 DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
496 DMI_MATCH(DMI_BOARD_NAME, "N128"),
498 .callback = dmi_check_cb,
503 DMI_MATCH(DMI_SYS_VENDOR,
504 "SAMSUNG ELECTRONICS CO., LTD."),
505 DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
506 DMI_MATCH(DMI_BOARD_NAME, "N130"),
508 .callback = dmi_check_cb,
513 DMI_MATCH(DMI_SYS_VENDOR,
514 "SAMSUNG ELECTRONICS CO., LTD."),
515 DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
516 DMI_MATCH(DMI_BOARD_NAME, "X125"),
518 .callback = dmi_check_cb,
522 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
524 static int find_signature(void __iomem *memcheck, const char *testStr)
529 for (loca = 0; loca < 0xffff; loca++) {
530 char temp = readb(memcheck + loca);
532 if (temp == testStr[pStr]) {
533 if (pStr == strlen(testStr)-1)
543 static int __init samsung_init(void)
545 struct backlight_properties props;
546 struct sabi_retval sretval;
552 mutex_init(&sabi_mutex);
554 if (!force && !dmi_check_system(samsung_dmi_table))
557 f0000_segment = ioremap(0xf0000, 0xffff);
558 if (!f0000_segment) {
559 printk(KERN_ERR "Can't map the segment at 0xf0000\n");
563 /* Try to find one of the signatures in memory to find the header */
564 for (pConfig = 0; sabi_configs[pConfig].test_string != 0; ++pConfig) {
565 sabi_config = &sabi_configs[pConfig];
566 loca = find_signature(f0000_segment, sabi_config->test_string);
571 if (loca == 0xffff) {
572 printk(KERN_ERR "This computer does not support SABI\n");
573 goto error_no_signature;
576 /* point to the SMI port Number */
578 sabi = (f0000_segment + loca);
581 printk(KERN_DEBUG "This computer supports SABI==%x\n",
583 printk(KERN_DEBUG "SABI header:\n");
584 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
585 readw(sabi + sabi_config->header_offsets.port));
586 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
587 readb(sabi + sabi_config->header_offsets.iface_func));
588 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
589 readb(sabi + sabi_config->header_offsets.en_mem));
590 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
591 readb(sabi + sabi_config->header_offsets.re_mem));
592 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
593 readw(sabi + sabi_config->header_offsets.data_offset));
594 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
595 readw(sabi + sabi_config->header_offsets.data_segment));
598 /* Get a pointer to the SABI Interface */
599 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
600 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
601 sabi_iface = ioremap(ifaceP, 16);
603 printk(KERN_ERR "Can't remap %x\n", ifaceP);
607 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
608 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
613 retval = sabi_get_command(sabi_config->commands.get_brightness, &sretval);
614 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
617 /* Turn on "Linux" mode in the BIOS */
618 if (sabi_config->commands.set_linux != 0xff) {
619 retval = sabi_set_command(sabi_config->commands.set_linux, 0x81);
621 printk(KERN_ERR KBUILD_MODNAME ": Linux mode was not set!\n");
622 goto error_no_platform;
626 /* knock up a platform device to hang stuff off of */
627 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
629 goto error_no_platform;
631 /* create a backlight device to talk to this one */
632 memset(&props, 0, sizeof(struct backlight_properties));
633 props.max_brightness = MAX_BRIGHT;
634 backlight_device = backlight_device_register("samsung", &sdev->dev,
635 NULL, &backlight_ops,
637 if (IS_ERR(backlight_device))
638 goto error_no_backlight;
640 backlight_device->props.brightness = read_brightness();
641 backlight_device->props.power = FB_BLANK_UNBLANK;
642 backlight_update_status(backlight_device);
644 retval = init_wireless(sdev);
648 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
650 goto error_file_create;
659 backlight_device_unregister(backlight_device);
662 platform_device_unregister(sdev);
668 iounmap(f0000_segment);
672 static void __exit samsung_exit(void)
674 /* Turn off "Linux" mode in the BIOS */
675 if (sabi_config->commands.set_linux != 0xff)
676 sabi_set_command(sabi_config->commands.set_linux, 0x80);
678 device_remove_file(&sdev->dev, &dev_attr_performance_level);
679 backlight_device_unregister(backlight_device);
682 iounmap(f0000_segment);
683 platform_device_unregister(sdev);
686 module_init(samsung_init);
687 module_exit(samsung_exit);
689 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
690 MODULE_DESCRIPTION("Samsung Backlight driver");
691 MODULE_LICENSE("GPL");