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
, "Disable the DMI check and forces the driver to be loaded");
105 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
106 MODULE_PARM_DESC(debug
, "Debug enabled or not");
108 static int sabi_get_command(u8 command
, struct sabi_retval
*sretval
)
111 u16 port
= readw(sabi
+ SABI_HEADER_PORT
);
113 mutex_lock(&sabi_mutex
);
115 /* enable memory to be able to write to it */
116 outb(readb(sabi
+ SABI_HEADER_EN_MEM
), port
);
118 /* write out the command */
119 writew(MAIN_FUNCTION
, sabi_iface
+ SABI_IFACE_MAIN
);
120 writew(command
, sabi_iface
+ SABI_IFACE_SUB
);
121 writeb(0, sabi_iface
+ SABI_IFACE_COMPLETE
);
122 outb(readb(sabi
+ SABI_HEADER_IFACEFUNC
), port
);
124 /* write protect memory to make it safe */
125 outb(readb(sabi
+ SABI_HEADER_RE_MEM
), port
);
127 /* see if the command actually succeeded */
128 if (readb(sabi_iface
+ SABI_IFACE_COMPLETE
) == 0xaa &&
129 readb(sabi_iface
+ SABI_IFACE_DATA
) != 0xff) {
132 * Save off the data into a structure so the caller use it.
133 * Right now we only care about the first 4 bytes,
134 * I suppose there are commands that need more, but I don't
137 sretval
->retval
[0] = readb(sabi_iface
+ SABI_IFACE_DATA
);
138 sretval
->retval
[1] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 1);
139 sretval
->retval
[2] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 2);
140 sretval
->retval
[3] = readb(sabi_iface
+ SABI_IFACE_DATA
+ 3);
144 /* Something bad happened, so report it and error out */
145 printk(KERN_WARNING
"SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
146 command
, readb(sabi_iface
+ SABI_IFACE_COMPLETE
),
147 readb(sabi_iface
+ SABI_IFACE_DATA
));
150 mutex_unlock(&sabi_mutex
);
155 static int sabi_set_command(u8 command
, u8 data
)
158 u16 port
= readw(sabi
+ SABI_HEADER_PORT
);
160 mutex_lock(&sabi_mutex
);
162 /* enable memory to be able to write to it */
163 outb(readb(sabi
+ SABI_HEADER_EN_MEM
), port
);
165 /* write out the command */
166 writew(MAIN_FUNCTION
, sabi_iface
+ SABI_IFACE_MAIN
);
167 writew(command
, sabi_iface
+ SABI_IFACE_SUB
);
168 writeb(0, sabi_iface
+ SABI_IFACE_COMPLETE
);
169 writeb(data
, sabi_iface
+ SABI_IFACE_DATA
);
170 outb(readb(sabi
+ SABI_HEADER_IFACEFUNC
), port
);
172 /* write protect memory to make it safe */
173 outb(readb(sabi
+ SABI_HEADER_RE_MEM
), port
);
175 /* see if the command actually succeeded */
176 if (readb(sabi_iface
+ SABI_IFACE_COMPLETE
) == 0xaa &&
177 readb(sabi_iface
+ SABI_IFACE_DATA
) != 0xff) {
182 /* Something bad happened, so report it and error out */
183 printk(KERN_WARNING
"SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
184 command
, readb(sabi_iface
+ SABI_IFACE_COMPLETE
),
185 readb(sabi_iface
+ SABI_IFACE_DATA
));
188 mutex_unlock(&sabi_mutex
);
192 static void test_backlight(void)
194 struct sabi_retval sretval
;
196 sabi_get_command(GET_BACKLIGHT
, &sretval
);
197 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
199 sabi_set_command(SET_BACKLIGHT
, 0);
200 printk(KERN_DEBUG
"backlight should be off\n");
202 sabi_get_command(GET_BACKLIGHT
, &sretval
);
203 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
207 sabi_set_command(SET_BACKLIGHT
, 1);
208 printk(KERN_DEBUG
"backlight should be on\n");
210 sabi_get_command(GET_BACKLIGHT
, &sretval
);
211 printk(KERN_DEBUG
"backlight = 0x%02x\n", sretval
.retval
[0]);
214 static void test_wireless(void)
216 struct sabi_retval sretval
;
218 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
219 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
221 sabi_set_command(SET_WIRELESS_BUTTON
, 0);
222 printk(KERN_DEBUG
"wireless led should be off\n");
224 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
225 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
229 sabi_set_command(SET_WIRELESS_BUTTON
, 1);
230 printk(KERN_DEBUG
"wireless led should be on\n");
232 sabi_get_command(GET_WIRELESS_BUTTON
, &sretval
);
233 printk(KERN_DEBUG
"wireless led = 0x%02x\n", sretval
.retval
[0]);
236 static u8
read_brightness(void)
238 struct sabi_retval sretval
;
239 int user_brightness
= 0;
242 retval
= sabi_get_command(GET_BRIGHTNESS
, &sretval
);
244 user_brightness
= sretval
.retval
[0];
245 if (user_brightness
!= 0)
247 return user_brightness
;
250 static void set_brightness(u8 user_brightness
)
252 sabi_set_command(SET_BRIGHTNESS
, user_brightness
+ 1);
255 static int get_brightness(struct backlight_device
*bd
)
257 return (int)read_brightness();
260 static int update_status(struct backlight_device
*bd
)
262 set_brightness(bd
->props
.brightness
);
264 if (bd
->props
.power
== FB_BLANK_UNBLANK
)
265 sabi_set_command(SET_BACKLIGHT
, 1);
267 sabi_set_command(SET_BACKLIGHT
, 0);
271 static struct backlight_ops backlight_ops
= {
272 .get_brightness
= get_brightness
,
273 .update_status
= update_status
,
276 static int rfkill_set(void *data
, bool blocked
)
278 /* Do something with blocked...*/
280 * blocked == false is on
281 * blocked == true is off
284 sabi_set_command(SET_WIRELESS_BUTTON
, 0);
286 sabi_set_command(SET_WIRELESS_BUTTON
, 1);
291 static struct rfkill_ops rfkill_ops
= {
292 .set_block
= rfkill_set
,
295 static int init_wireless(struct platform_device
*sdev
)
299 rfk
= rfkill_alloc("samsung-wifi", &sdev
->dev
, RFKILL_TYPE_WLAN
,
304 retval
= rfkill_register(rfk
);
313 static void destroy_wireless(void)
315 rfkill_unregister(rfk
);
319 static ssize_t
get_silent_state(struct device
*dev
,
320 struct device_attribute
*attr
, char *buf
)
322 struct sabi_retval sretval
;
326 retval
= sabi_get_command(GET_PERFORMANCE_LEVEL
, &sretval
);
330 /* The logic is backwards, yeah, lots of fun... */
331 if (sretval
.retval
[0] == 0)
335 return sprintf(buf
, "%d\n", retval
);
338 static ssize_t
set_silent_state(struct device
*dev
,
339 struct device_attribute
*attr
, const char *buf
,
346 if ((value
== '0') || (value
== 'n') || (value
== 'N')) {
348 sabi_set_command(SET_PERFORMANCE_LEVEL
, 0x01);
349 } else if ((value
== '1') || (value
== 'y') || (value
== 'Y')) {
350 /* Turn speed down */
351 sabi_set_command(SET_PERFORMANCE_LEVEL
, 0x00);
358 static DEVICE_ATTR(silent
, S_IWUGO
| S_IRUGO
,
359 get_silent_state
, set_silent_state
);
362 static int __init
dmi_check_cb(const struct dmi_system_id
*id
)
364 printk(KERN_INFO KBUILD_MODNAME
": found laptop model '%s'\n",
369 static struct dmi_system_id __initdata samsung_dmi_table
[] = {
373 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
374 DMI_MATCH(DMI_PRODUCT_NAME
, "N128"),
375 DMI_MATCH(DMI_BOARD_NAME
, "N128"),
377 .callback
= dmi_check_cb
,
382 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
383 DMI_MATCH(DMI_PRODUCT_NAME
, "N130"),
384 DMI_MATCH(DMI_BOARD_NAME
, "N130"),
386 .callback
= dmi_check_cb
,
390 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
392 static int __init
samsung_init(void)
394 struct sabi_retval sretval
;
395 const char *testStr
= "SECLINUX";
396 void __iomem
*memcheck
;
402 mutex_init(&sabi_mutex
);
404 if (!force
&& !dmi_check_system(samsung_dmi_table
))
407 f0000_segment
= ioremap(0xf0000, 0xffff);
408 if (!f0000_segment
) {
409 printk(KERN_ERR
"Can't map the segment at 0xf0000\n");
413 /* Try to find the signature "SECLINUX" in memory to find the header */
415 memcheck
= f0000_segment
;
416 for (loca
= 0; loca
< 0xffff; loca
++) {
417 char temp
= readb(memcheck
+ loca
);
419 if (temp
== testStr
[pStr
]) {
420 if (pStr
== strlen(testStr
)-1)
427 if (loca
== 0xffff) {
428 printk(KERN_ERR
"This computer does not support SABI\n");
429 goto error_no_signature
;
432 /* point to the SMI port Number */
434 sabi
= (memcheck
+ loca
);
437 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
439 printk(KERN_DEBUG
"SABI header:\n");
440 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
441 readw(sabi
+ SABI_HEADER_PORT
));
442 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
443 readb(sabi
+ SABI_HEADER_IFACEFUNC
));
444 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
445 readb(sabi
+ SABI_HEADER_EN_MEM
));
446 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
447 readb(sabi
+ SABI_HEADER_RE_MEM
));
448 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
449 readw(sabi
+ SABI_HEADER_DATA_OFFSET
));
450 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
451 readw(sabi
+ SABI_HEADER_DATA_SEGMENT
));
454 /* Get a pointer to the SABI Interface */
455 ifaceP
= (readw(sabi
+ SABI_HEADER_DATA_SEGMENT
) & 0x0ffff) << 4;
456 ifaceP
+= readw(sabi
+ SABI_HEADER_DATA_OFFSET
) & 0x0ffff;
457 sabi_iface
= ioremap(ifaceP
, 16);
459 printk(KERN_ERR
"Can't remap %x\n", ifaceP
);
463 printk(KERN_DEBUG
"ifaceP = 0x%08x\n", ifaceP
);
464 printk(KERN_DEBUG
"sabi_iface = %p\n", sabi_iface
);
469 retval
= sabi_get_command(GET_BRIGHTNESS
, &sretval
);
470 printk(KERN_DEBUG
"brightness = 0x%02x\n", sretval
.retval
[0]);
473 /* Turn on "Linux" mode in the BIOS */
474 retval
= sabi_set_command(SET_LINUX
, 0x81);
476 printk(KERN_ERR KBUILD_MODNAME
": Linux mode was not set!\n");
477 goto error_no_platform
;
480 /* knock up a platform device to hang stuff off of */
481 sdev
= platform_device_register_simple("samsung", -1, NULL
, 0);
483 goto error_no_platform
;
485 /* create a backlight device to talk to this one */
486 backlight_device
= backlight_device_register("samsung", &sdev
->dev
,
487 NULL
, &backlight_ops
);
488 if (IS_ERR(backlight_device
))
489 goto error_no_backlight
;
491 backlight_device
->props
.max_brightness
= MAX_BRIGHT
;
492 backlight_device
->props
.brightness
= read_brightness();
493 backlight_device
->props
.power
= FB_BLANK_UNBLANK
;
494 backlight_update_status(backlight_device
);
496 retval
= init_wireless(sdev
);
500 retval
= device_create_file(&sdev
->dev
, &dev_attr_silent
);
502 goto error_file_create
;
511 backlight_device_unregister(backlight_device
);
514 platform_device_unregister(sdev
);
520 iounmap(f0000_segment
);
524 static void __exit
samsung_exit(void)
526 /* Turn off "Linux" mode in the BIOS */
527 sabi_set_command(SET_LINUX
, 0x80);
529 device_remove_file(&sdev
->dev
, &dev_attr_silent
);
530 backlight_device_unregister(backlight_device
);
533 iounmap(f0000_segment
);
534 platform_device_unregister(sdev
);
537 module_init(samsung_init
);
538 module_exit(samsung_exit
);
540 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
541 MODULE_DESCRIPTION("Samsung Backlight driver");
542 MODULE_LICENSE("GPL");