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
36 /* Brightness is 0 - 8, as described above. Value 0 is for the BIOS to use */
37 #define GET_BRIGHTNESS 0x00
38 #define SET_BRIGHTNESS 0x01
41 * 0x00 - wireless is off
42 * 0x01 - wireless is on
46 * TODO, verify 3G is correct, that doesn't seem right...
48 #define GET_WIRELESS_BUTTON 0x02
49 #define SET_WIRELESS_BUTTON 0x03
51 /* 0 is off, 1 is on */
52 #define GET_BACKLIGHT 0x04
53 #define SET_BACKLIGHT 0x05
56 * 0x80 or 0x00 - no action
57 * 0x81 - recovery key pressed
59 #define GET_RECOVERY_METHOD 0x06
60 #define SET_RECOVERY_METHOD 0x07
62 /* 0 is low, 1 is high */
63 #define GET_PERFORMANCE_LEVEL 0x08
64 #define SET_PERFORMANCE_LEVEL 0x09
67 * Tell the BIOS that Linux is running on this machine.
70 #define SET_LINUX 0x0a
73 #define MAIN_FUNCTION 0x4c49
75 #define SABI_HEADER_PORT 0x00
76 #define SABI_HEADER_RE_MEM 0x02
77 #define SABI_HEADER_IFACEFUNC 0x03
78 #define SABI_HEADER_EN_MEM 0x04
79 #define SABI_HEADER_DATA_OFFSET 0x05
80 #define SABI_HEADER_DATA_SEGMENT 0x07
82 #define SABI_IFACE_MAIN 0x00
83 #define SABI_IFACE_SUB 0x02
84 #define SABI_IFACE_COMPLETE 0x04
85 #define SABI_IFACE_DATA 0x05
87 /* Structure to get data back to the calling function */
92 static void __iomem
*sabi
;
93 static void __iomem
*sabi_iface
;
94 static void __iomem
*f0000_segment
;
95 static struct backlight_device
*backlight_device
;
96 static struct mutex sabi_mutex
;
97 static struct platform_device
*sdev
;
98 static struct rfkill
*rfk
;
101 module_param(force
, bool, 0);
102 MODULE_PARM_DESC(force
,
103 "Disable the DMI check and forces the driver to be loaded");
106 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
107 MODULE_PARM_DESC(debug
, "Debug enabled or not");
109 static int sabi_get_command(u8 command
, struct sabi_retval
*sretval
)
112 u16 port
= readw(sabi
+ SABI_HEADER_PORT
);
114 mutex_lock(&sabi_mutex
);
116 /* enable memory to be able to write to it */
117 outb(readb(sabi
+ SABI_HEADER_EN_MEM
), port
);
119 /* write out the command */
120 writew(MAIN_FUNCTION
, sabi_iface
+ SABI_IFACE_MAIN
);
121 writew(command
, sabi_iface
+ SABI_IFACE_SUB
);
122 writeb(0, sabi_iface
+ SABI_IFACE_COMPLETE
);
123 outb(readb(sabi
+ SABI_HEADER_IFACEFUNC
), port
);
125 /* write protect memory to make it safe */
126 outb(readb(sabi
+ SABI_HEADER_RE_MEM
), port
);
128 /* see if the command actually succeeded */
129 if (readb(sabi_iface
+ SABI_IFACE_COMPLETE
) == 0xaa &&
130 readb(sabi_iface
+ SABI_IFACE_DATA
) != 0xff) {
133 * Save off the data into a structure so the caller use it.
134 * Right now we only care about the first 4 bytes,
135 * I suppose there are commands that need more, but I don't
138 sretval
->retval
[0] = readb(sabi_iface
+ SABI_IFACE_DATA
);
139 sretval
->retval
[1] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 1);
140 sretval
->retval
[2] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 2);
141 sretval
->retval
[3] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 3);
145 /* Something bad happened, so report it and error out */
146 printk(KERN_WARNING
"SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
147 command
, readb(sabi_iface
+ SABI_IFACE_COMPLETE
),
148 readb(sabi_iface
+ SABI_IFACE_DATA
));
151 mutex_unlock(&sabi_mutex
);
156 static int sabi_set_command(u8 command
, u8 data
)
159 u16 port
= readw(sabi
+ SABI_HEADER_PORT
);
161 mutex_lock(&sabi_mutex
);
163 /* enable memory to be able to write to it */
164 outb(readb(sabi
+ SABI_HEADER_EN_MEM
), port
);
166 /* write out the command */
167 writew(MAIN_FUNCTION
, sabi_iface
+ SABI_IFACE_MAIN
);
168 writew(command
, sabi_iface
+ SABI_IFACE_SUB
);
169 writeb(0, sabi_iface
+ SABI_IFACE_COMPLETE
);
170 writeb(data
, sabi_iface
+ SABI_IFACE_DATA
);
171 outb(readb(sabi
+ SABI_HEADER_IFACEFUNC
), port
);
173 /* write protect memory to make it safe */
174 outb(readb(sabi
+ SABI_HEADER_RE_MEM
), port
);
176 /* see if the command actually succeeded */
177 if (readb(sabi_iface
+ SABI_IFACE_COMPLETE
) == 0xaa &&
178 readb(sabi_iface
+ SABI_IFACE_DATA
) != 0xff) {
183 /* Something bad happened, so report it and error out */
184 printk(KERN_WARNING
"SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
185 command
, readb(sabi_iface
+ SABI_IFACE_COMPLETE
),
186 readb(sabi_iface
+ SABI_IFACE_DATA
));
189 mutex_unlock(&sabi_mutex
);
193 static void test_backlight(void)
195 struct sabi_retval sretval
;
197 sabi_get_command(GET_BACKLIGHT
, &sretval
);
198 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
200 sabi_set_command(SET_BACKLIGHT
, 0);
201 printk(KERN_DEBUG
"backlight should be off\n");
203 sabi_get_command(GET_BACKLIGHT
, &sretval
);
204 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
208 sabi_set_command(SET_BACKLIGHT
, 1);
209 printk(KERN_DEBUG
"backlight should be on\n");
211 sabi_get_command(GET_BACKLIGHT
, &sretval
);
212 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
215 static void test_wireless(void)
217 struct sabi_retval sretval
;
219 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
220 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
222 sabi_set_command(SET_WIRELESS_BUTTON
, 0);
223 printk(KERN_DEBUG
"wireless led should be off\n");
225 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
226 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
230 sabi_set_command(SET_WIRELESS_BUTTON
, 1);
231 printk(KERN_DEBUG
"wireless led should be on\n");
233 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
234 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
237 static u8
read_brightness(void)
239 struct sabi_retval sretval
;
240 int user_brightness
= 0;
243 retval
= sabi_get_command(GET_BRIGHTNESS
, &sretval
);
245 user_brightness
= sretval
.retval
[0];
246 if (user_brightness
!= 0)
248 return user_brightness
;
251 static void set_brightness(u8 user_brightness
)
253 sabi_set_command(SET_BRIGHTNESS
, user_brightness
+ 1);
256 static int get_brightness(struct backlight_device
*bd
)
258 return (int)read_brightness();
261 static int update_status(struct backlight_device
*bd
)
263 set_brightness(bd
->props
.brightness
);
265 if (bd
->props
.power
== FB_BLANK_UNBLANK
)
266 sabi_set_command(SET_BACKLIGHT
, 1);
268 sabi_set_command(SET_BACKLIGHT
, 0);
272 static struct backlight_ops backlight_ops
= {
273 .get_brightness
= get_brightness
,
274 .update_status
= update_status
,
277 static int rfkill_set(void *data
, bool blocked
)
279 /* Do something with blocked...*/
281 * blocked == false is on
282 * blocked == true is off
285 sabi_set_command(SET_WIRELESS_BUTTON
, 0);
287 sabi_set_command(SET_WIRELESS_BUTTON
, 1);
292 static struct rfkill_ops rfkill_ops
= {
293 .set_block
= rfkill_set
,
296 static int init_wireless(struct platform_device
*sdev
)
300 rfk
= rfkill_alloc("samsung-wifi", &sdev
->dev
, RFKILL_TYPE_WLAN
,
305 retval
= rfkill_register(rfk
);
314 static void destroy_wireless(void)
316 rfkill_unregister(rfk
);
320 static ssize_t
get_silent_state(struct device
*dev
,
321 struct device_attribute
*attr
, char *buf
)
323 struct sabi_retval sretval
;
327 retval
= sabi_get_command(GET_PERFORMANCE_LEVEL
, &sretval
);
331 /* The logic is backwards, yeah, lots of fun... */
332 if (sretval
.retval
[0] == 0)
336 return sprintf(buf
, "%d\n", retval
);
339 static ssize_t
set_silent_state(struct device
*dev
,
340 struct device_attribute
*attr
, const char *buf
,
347 if ((value
== '0') || (value
== 'n') || (value
== 'N')) {
349 sabi_set_command(SET_PERFORMANCE_LEVEL
, 0x01);
350 } else if ((value
== '1') || (value
== 'y') || (value
== 'Y')) {
351 /* Turn speed down */
352 sabi_set_command(SET_PERFORMANCE_LEVEL
, 0x00);
359 static DEVICE_ATTR(silent
, S_IWUGO
| S_IRUGO
,
360 get_silent_state
, set_silent_state
);
363 static int __init
dmi_check_cb(const struct dmi_system_id
*id
)
365 printk(KERN_INFO KBUILD_MODNAME
": found laptop model '%s'\n",
370 static struct dmi_system_id __initdata samsung_dmi_table
[] = {
374 DMI_MATCH(DMI_SYS_VENDOR
,
375 "SAMSUNG ELECTRONICS CO., LTD."),
376 DMI_MATCH(DMI_PRODUCT_NAME
, "N128"),
377 DMI_MATCH(DMI_BOARD_NAME
, "N128"),
379 .callback
= dmi_check_cb
,
384 DMI_MATCH(DMI_SYS_VENDOR
,
385 "SAMSUNG ELECTRONICS CO., LTD."),
386 DMI_MATCH(DMI_PRODUCT_NAME
, "N130"),
387 DMI_MATCH(DMI_BOARD_NAME
, "N130"),
389 .callback
= dmi_check_cb
,
393 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
395 static int __init
samsung_init(void)
397 struct backlight_properties props
;
398 struct sabi_retval sretval
;
399 const char *testStr
= "SECLINUX";
400 void __iomem
*memcheck
;
406 mutex_init(&sabi_mutex
);
408 if (!force
&& !dmi_check_system(samsung_dmi_table
))
411 f0000_segment
= ioremap(0xf0000, 0xffff);
412 if (!f0000_segment
) {
413 printk(KERN_ERR
"Can't map the segment at 0xf0000\n");
417 /* Try to find the signature "SECLINUX" in memory to find the header */
419 memcheck
= f0000_segment
;
420 for (loca
= 0; loca
< 0xffff; loca
++) {
421 char temp
= readb(memcheck
+ loca
);
423 if (temp
== testStr
[pStr
]) {
424 if (pStr
== strlen(testStr
)-1)
431 if (loca
== 0xffff) {
432 printk(KERN_ERR
"This computer does not support SABI\n");
433 goto error_no_signature
;
436 /* point to the SMI port Number */
438 sabi
= (memcheck
+ loca
);
441 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
443 printk(KERN_DEBUG
"SABI header:\n");
444 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
445 readw(sabi
+ SABI_HEADER_PORT
));
446 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
447 readb(sabi
+ SABI_HEADER_IFACEFUNC
));
448 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
449 readb(sabi
+ SABI_HEADER_EN_MEM
));
450 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
451 readb(sabi
+ SABI_HEADER_RE_MEM
));
452 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
453 readw(sabi
+ SABI_HEADER_DATA_OFFSET
));
454 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
455 readw(sabi
+ SABI_HEADER_DATA_SEGMENT
));
458 /* Get a pointer to the SABI Interface */
459 ifaceP
= (readw(sabi
+ SABI_HEADER_DATA_SEGMENT
) & 0x0ffff) << 4;
460 ifaceP
+= readw(sabi
+ SABI_HEADER_DATA_OFFSET
) & 0x0ffff;
461 sabi_iface
= ioremap(ifaceP
, 16);
463 printk(KERN_ERR
"Can't remap %x\n", ifaceP
);
467 printk(KERN_DEBUG
"ifaceP = 0x%08x\n", ifaceP
);
468 printk(KERN_DEBUG
"sabi_iface = %p\n", sabi_iface
);
473 retval
= sabi_get_command(GET_BRIGHTNESS
, &sretval
);
474 printk(KERN_DEBUG
"brightness = 0x%02x\n", sretval
.retval
[0]);
477 /* Turn on "Linux" mode in the BIOS */
478 retval
= sabi_set_command(SET_LINUX
, 0x81);
480 printk(KERN_ERR KBUILD_MODNAME
": Linux mode was not set!\n");
481 goto error_no_platform
;
484 /* knock up a platform device to hang stuff off of */
485 sdev
= platform_device_register_simple("samsung", -1, NULL
, 0);
487 goto error_no_platform
;
489 /* create a backlight device to talk to this one */
490 memset(&props
, 0, sizeof(struct backlight_properties
));
491 props
.max_brightness
= MAX_BRIGHT
;
492 backlight_device
= backlight_device_register("samsung", &sdev
->dev
,
493 NULL
, &backlight_ops
,
495 if (IS_ERR(backlight_device
))
496 goto error_no_backlight
;
498 backlight_device
->props
.brightness
= read_brightness();
499 backlight_device
->props
.power
= FB_BLANK_UNBLANK
;
500 backlight_update_status(backlight_device
);
502 retval
= init_wireless(sdev
);
506 retval
= device_create_file(&sdev
->dev
, &dev_attr_silent
);
508 goto error_file_create
;
517 backlight_device_unregister(backlight_device
);
520 platform_device_unregister(sdev
);
526 iounmap(f0000_segment
);
530 static void __exit
samsung_exit(void)
532 /* Turn off "Linux" mode in the BIOS */
533 sabi_set_command(SET_LINUX
, 0x80);
535 device_remove_file(&sdev
->dev
, &dev_attr_silent
);
536 backlight_device_unregister(backlight_device
);
539 iounmap(f0000_segment
);
540 platform_device_unregister(sdev
);
543 module_init(samsung_init
);
544 module_exit(samsung_exit
);
546 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
547 MODULE_DESCRIPTION("Samsung Backlight driver");
548 MODULE_LICENSE("GPL");