blob: b41c7b4f9c7177e0335cb3e5c938aee7e632ed2c [file] [log] [blame]
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001/*
2 * Samsung Laptop driver
3 *
4 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009,2011 Novell Inc.
6 *
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.
10 *
11 */
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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>
Corentin Charyf674ebf2011-11-26 11:00:08 +010020#include <linux/leds.h>
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050021#include <linux/fb.h>
22#include <linux/dmi.h>
23#include <linux/platform_device.h>
24#include <linux/rfkill.h>
Corentin Charyf34cd9c2011-11-26 11:00:00 +010025#include <linux/acpi.h>
Corentin Chary5b80fc42011-11-26 11:00:03 +010026#include <linux/seq_file.h>
27#include <linux/debugfs.h>
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050028
29/*
30 * This driver is needed because a number of Samsung laptops do not hook
31 * their control settings through ACPI. So we have to poke around in the
32 * BIOS to do things like brightness values, and "special" key controls.
33 */
34
35/*
36 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
37 * be reserved by the BIOS (which really doesn't make much sense), we tell
38 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
39 */
40#define MAX_BRIGHT 0x07
41
42
43#define SABI_IFACE_MAIN 0x00
44#define SABI_IFACE_SUB 0x02
45#define SABI_IFACE_COMPLETE 0x04
46#define SABI_IFACE_DATA 0x05
47
Corentin Chary84d482f2011-11-26 11:00:09 +010048#define WL_STATUS_WLAN 0x0
49#define WL_STATUS_BT 0x2
50
Corentin Chary7e960712011-11-26 11:00:02 +010051/* Structure get/set data using sabi */
52struct sabi_data {
53 union {
54 struct {
55 u32 d0;
56 u32 d1;
57 u16 d2;
58 u8 d3;
59 };
60 u8 data[11];
61 };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050062};
63
64struct sabi_header_offsets {
65 u8 port;
66 u8 re_mem;
67 u8 iface_func;
68 u8 en_mem;
69 u8 data_offset;
70 u8 data_segment;
71};
72
73struct sabi_commands {
74 /*
75 * Brightness is 0 - 8, as described above.
76 * Value 0 is for the BIOS to use
77 */
Corentin Chary7e960712011-11-26 11:00:02 +010078 u16 get_brightness;
79 u16 set_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050080
81 /*
82 * first byte:
83 * 0x00 - wireless is off
84 * 0x01 - wireless is on
85 * second byte:
86 * 0x02 - 3G is off
87 * 0x03 - 3G is on
88 * TODO, verify 3G is correct, that doesn't seem right...
89 */
Corentin Chary7e960712011-11-26 11:00:02 +010090 u16 get_wireless_button;
91 u16 set_wireless_button;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050092
93 /* 0 is off, 1 is on */
Corentin Chary7e960712011-11-26 11:00:02 +010094 u16 get_backlight;
95 u16 set_backlight;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050096
97 /*
98 * 0x80 or 0x00 - no action
99 * 0x81 - recovery key pressed
100 */
Corentin Chary7e960712011-11-26 11:00:02 +0100101 u16 get_recovery_mode;
102 u16 set_recovery_mode;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500103
104 /*
105 * on seclinux: 0 is low, 1 is high,
106 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
107 */
Corentin Chary7e960712011-11-26 11:00:02 +0100108 u16 get_performance_level;
109 u16 set_performance_level;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500110
Corentin Charycb5b5c92011-11-26 11:00:05 +0100111 /* 0x80 is off, 0x81 is on */
112 u16 get_battery_life_extender;
113 u16 set_battery_life_extender;
114
Corentin Chary3a75d372011-11-26 11:00:06 +0100115 /* 0x80 is off, 0x81 is on */
116 u16 get_usb_charge;
117 u16 set_usb_charge;
118
Corentin Chary84d482f2011-11-26 11:00:09 +0100119 /* the first byte is for bluetooth and the third one is for wlan */
120 u16 get_wireless_status;
121 u16 set_wireless_status;
122
Corentin Charyf674ebf2011-11-26 11:00:08 +0100123 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
124 u16 kbd_backlight;
125
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500126 /*
127 * Tell the BIOS that Linux is running on this machine.
128 * 81 is on, 80 is off
129 */
Corentin Chary7e960712011-11-26 11:00:02 +0100130 u16 set_linux;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500131};
132
133struct sabi_performance_level {
134 const char *name;
Corentin Chary7e960712011-11-26 11:00:02 +0100135 u16 value;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500136};
137
138struct sabi_config {
Corentin Chary84d482f2011-11-26 11:00:09 +0100139 int sabi_version;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500140 const char *test_string;
141 u16 main_function;
142 const struct sabi_header_offsets header_offsets;
143 const struct sabi_commands commands;
144 const struct sabi_performance_level performance_levels[4];
145 u8 min_brightness;
146 u8 max_brightness;
147};
148
149static const struct sabi_config sabi_configs[] = {
150 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100151 /* I don't know if it is really 2, but it it is
152 * less than 3 anyway */
153 .sabi_version = 2,
154
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500155 .test_string = "SECLINUX",
156
157 .main_function = 0x4c49,
158
159 .header_offsets = {
160 .port = 0x00,
161 .re_mem = 0x02,
162 .iface_func = 0x03,
163 .en_mem = 0x04,
164 .data_offset = 0x05,
165 .data_segment = 0x07,
166 },
167
168 .commands = {
169 .get_brightness = 0x00,
170 .set_brightness = 0x01,
171
172 .get_wireless_button = 0x02,
173 .set_wireless_button = 0x03,
174
175 .get_backlight = 0x04,
176 .set_backlight = 0x05,
177
178 .get_recovery_mode = 0x06,
179 .set_recovery_mode = 0x07,
180
181 .get_performance_level = 0x08,
182 .set_performance_level = 0x09,
183
Corentin Charycb5b5c92011-11-26 11:00:05 +0100184 .get_battery_life_extender = 0xFFFF,
185 .set_battery_life_extender = 0xFFFF,
186
Corentin Chary3a75d372011-11-26 11:00:06 +0100187 .get_usb_charge = 0xFFFF,
188 .set_usb_charge = 0xFFFF,
189
Corentin Chary84d482f2011-11-26 11:00:09 +0100190 .get_wireless_status = 0xFFFF,
191 .set_wireless_status = 0xFFFF,
192
Corentin Charyf674ebf2011-11-26 11:00:08 +0100193 .kbd_backlight = 0xFFFF,
194
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500195 .set_linux = 0x0a,
196 },
197
198 .performance_levels = {
199 {
200 .name = "silent",
201 .value = 0,
202 },
203 {
204 .name = "normal",
205 .value = 1,
206 },
207 { },
208 },
209 .min_brightness = 1,
210 .max_brightness = 8,
211 },
212 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100213 .sabi_version = 3,
214
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500215 .test_string = "SwSmi@",
216
217 .main_function = 0x5843,
218
219 .header_offsets = {
220 .port = 0x00,
221 .re_mem = 0x04,
222 .iface_func = 0x02,
223 .en_mem = 0x03,
224 .data_offset = 0x05,
225 .data_segment = 0x07,
226 },
227
228 .commands = {
229 .get_brightness = 0x10,
230 .set_brightness = 0x11,
231
232 .get_wireless_button = 0x12,
233 .set_wireless_button = 0x13,
234
235 .get_backlight = 0x2d,
236 .set_backlight = 0x2e,
237
238 .get_recovery_mode = 0xff,
239 .set_recovery_mode = 0xff,
240
241 .get_performance_level = 0x31,
242 .set_performance_level = 0x32,
243
Corentin Charycb5b5c92011-11-26 11:00:05 +0100244 .get_battery_life_extender = 0x65,
245 .set_battery_life_extender = 0x66,
246
Corentin Chary3a75d372011-11-26 11:00:06 +0100247 .get_usb_charge = 0x67,
248 .set_usb_charge = 0x68,
249
Corentin Chary84d482f2011-11-26 11:00:09 +0100250 .get_wireless_status = 0x69,
251 .set_wireless_status = 0x6a,
252
Corentin Charyf674ebf2011-11-26 11:00:08 +0100253 .kbd_backlight = 0x78,
254
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500255 .set_linux = 0xff,
256 },
257
258 .performance_levels = {
259 {
260 .name = "normal",
261 .value = 0,
262 },
263 {
264 .name = "silent",
265 .value = 1,
266 },
267 {
268 .name = "overclock",
269 .value = 2,
270 },
271 { },
272 },
273 .min_brightness = 0,
274 .max_brightness = 8,
275 },
276 { },
277};
278
Corentin Chary5b80fc42011-11-26 11:00:03 +0100279/*
280 * samsung-laptop/ - debugfs root directory
281 * f0000_segment - dump f0000 segment
282 * command - current command
283 * data - current data
284 * d0, d1, d2, d3 - data fields
285 * call - call SABI using command and data
286 *
287 * This allow to call arbitrary sabi commands wihout
288 * modifying the driver at all.
289 * For example, setting the keyboard backlight brightness to 5
290 *
291 * echo 0x78 > command
292 * echo 0x0582 > d0
293 * echo 0 > d1
294 * echo 0 > d2
295 * echo 0 > d3
296 * cat call
297 */
298
299struct samsung_laptop_debug {
300 struct dentry *root;
301 struct sabi_data data;
302 u16 command;
303
304 struct debugfs_blob_wrapper f0000_wrapper;
305 struct debugfs_blob_wrapper data_wrapper;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100306 struct debugfs_blob_wrapper sdiag_wrapper;
Corentin Chary5b80fc42011-11-26 11:00:03 +0100307};
308
Corentin Chary84d482f2011-11-26 11:00:09 +0100309struct samsung_laptop;
310
311struct samsung_rfkill {
312 struct samsung_laptop *samsung;
313 struct rfkill *rfkill;
314 enum rfkill_type type;
315};
316
Corentin Charya6df4892011-11-26 10:59:58 +0100317struct samsung_laptop {
318 const struct sabi_config *config;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500319
Corentin Charya6df4892011-11-26 10:59:58 +0100320 void __iomem *sabi;
321 void __iomem *sabi_iface;
322 void __iomem *f0000_segment;
323
324 struct mutex sabi_mutex;
325
Corentin Chary5dea7a22011-11-26 10:59:59 +0100326 struct platform_device *platform_device;
Corentin Charya6df4892011-11-26 10:59:58 +0100327 struct backlight_device *backlight_device;
Corentin Chary84d482f2011-11-26 11:00:09 +0100328
329 struct samsung_rfkill wlan;
330 struct samsung_rfkill bluetooth;
Corentin Charya6df4892011-11-26 10:59:58 +0100331
Corentin Charyf674ebf2011-11-26 11:00:08 +0100332 struct led_classdev kbd_led;
333 int kbd_led_wk;
334 struct workqueue_struct *led_workqueue;
335 struct work_struct kbd_led_work;
336
Corentin Chary5b80fc42011-11-26 11:00:03 +0100337 struct samsung_laptop_debug debug;
338
Corentin Charyf34cd9c2011-11-26 11:00:00 +0100339 bool handle_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +0100340 bool has_stepping_quirk;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100341
342 char sdiag[64];
Corentin Charya6df4892011-11-26 10:59:58 +0100343};
344
Corentin Chary5dea7a22011-11-26 10:59:59 +0100345
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500346
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030347static bool force;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500348module_param(force, bool, 0);
349MODULE_PARM_DESC(force,
350 "Disable the DMI check and forces the driver to be loaded");
351
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030352static bool debug;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500353module_param(debug, bool, S_IRUGO | S_IWUSR);
354MODULE_PARM_DESC(debug, "Debug enabled or not");
355
Corentin Chary7e960712011-11-26 11:00:02 +0100356static int sabi_command(struct samsung_laptop *samsung, u16 command,
357 struct sabi_data *in,
358 struct sabi_data *out)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500359{
Corentin Charya6df4892011-11-26 10:59:58 +0100360 const struct sabi_config *config = samsung->config;
Corentin Chary7e960712011-11-26 11:00:02 +0100361 int ret = 0;
Corentin Charya6df4892011-11-26 10:59:58 +0100362 u16 port = readw(samsung->sabi + config->header_offsets.port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500363 u8 complete, iface_data;
364
Corentin Charya6df4892011-11-26 10:59:58 +0100365 mutex_lock(&samsung->sabi_mutex);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500366
Corentin Chary7e960712011-11-26 11:00:02 +0100367 if (debug) {
368 if (in)
369 pr_info("SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
370 command, in->d0, in->d1, in->d2, in->d3);
371 else
372 pr_info("SABI 0x%04x", command);
373 }
374
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500375 /* enable memory to be able to write to it */
Corentin Charya6df4892011-11-26 10:59:58 +0100376 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500377
378 /* write out the command */
Corentin Charya6df4892011-11-26 10:59:58 +0100379 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
380 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
381 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
Corentin Chary7e960712011-11-26 11:00:02 +0100382 if (in) {
383 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
384 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
385 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
386 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
387 }
Corentin Charya6df4892011-11-26 10:59:58 +0100388 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500389
390 /* write protect memory to make it safe */
Corentin Charya6df4892011-11-26 10:59:58 +0100391 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500392
393 /* see if the command actually succeeded */
Corentin Charya6df4892011-11-26 10:59:58 +0100394 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
395 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500396 if (complete != 0xaa || iface_data == 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +0100397 pr_warn("SABI command 0x%04x failed with"
398 " completion flag 0x%02x and interface data 0x%02x",
399 command, complete, iface_data);
400 ret = -EINVAL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500401 goto exit;
402 }
Corentin Chary7e960712011-11-26 11:00:02 +0100403
404 if (out) {
405 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
406 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
407 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
408 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
409 }
410
411 if (debug && out) {
412 pr_info("SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
413 out->d0, out->d1, out->d2, out->d3);
414 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500415
416exit:
Corentin Charya6df4892011-11-26 10:59:58 +0100417 mutex_unlock(&samsung->sabi_mutex);
Corentin Chary7e960712011-11-26 11:00:02 +0100418 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500419}
420
Corentin Chary7e960712011-11-26 11:00:02 +0100421/* simple wrappers usable with most commands */
422static int sabi_set_commandb(struct samsung_laptop *samsung,
423 u16 command, u8 data)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500424{
Corentin Chary7e960712011-11-26 11:00:02 +0100425 struct sabi_data in = { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500426
Corentin Chary7e960712011-11-26 11:00:02 +0100427 in.data[0] = data;
428 return sabi_command(samsung, command, &in, NULL);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500429}
430
Corentin Chary5dea7a22011-11-26 10:59:59 +0100431static int read_brightness(struct samsung_laptop *samsung)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500432{
Corentin Charya6df4892011-11-26 10:59:58 +0100433 const struct sabi_config *config = samsung->config;
434 const struct sabi_commands *commands = &samsung->config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100435 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500436 int user_brightness = 0;
437 int retval;
438
Corentin Chary7e960712011-11-26 11:00:02 +0100439 retval = sabi_command(samsung, commands->get_brightness,
440 NULL, &sretval);
441 if (retval)
442 return retval;
443
444 user_brightness = sretval.data[0];
445 if (user_brightness > config->min_brightness)
446 user_brightness -= config->min_brightness;
447 else
448 user_brightness = 0;
449
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500450 return user_brightness;
451}
452
Corentin Chary5dea7a22011-11-26 10:59:59 +0100453static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500454{
Corentin Charya6df4892011-11-26 10:59:58 +0100455 const struct sabi_config *config = samsung->config;
456 const struct sabi_commands *commands = &samsung->config->commands;
457 u8 user_level = user_brightness + config->min_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500458
Corentin Charya6df4892011-11-26 10:59:58 +0100459 if (samsung->has_stepping_quirk && user_level != 0) {
Jason Stubbsac080522011-09-20 09:16:13 -0700460 /*
461 * short circuit if the specified level is what's already set
462 * to prevent the screen from flickering needlessly
463 */
Corentin Chary5dea7a22011-11-26 10:59:59 +0100464 if (user_brightness == read_brightness(samsung))
Jason Stubbsac080522011-09-20 09:16:13 -0700465 return;
466
Corentin Chary7e960712011-11-26 11:00:02 +0100467 sabi_set_commandb(samsung, commands->set_brightness, 0);
Jason Stubbsac080522011-09-20 09:16:13 -0700468 }
469
Corentin Chary7e960712011-11-26 11:00:02 +0100470 sabi_set_commandb(samsung, commands->set_brightness, user_level);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500471}
472
473static int get_brightness(struct backlight_device *bd)
474{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100475 struct samsung_laptop *samsung = bl_get_data(bd);
476
477 return read_brightness(samsung);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500478}
479
Corentin Chary5dea7a22011-11-26 10:59:59 +0100480static void check_for_stepping_quirk(struct samsung_laptop *samsung)
Jason Stubbsac080522011-09-20 09:16:13 -0700481{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100482 int initial_level;
483 int check_level;
484 int orig_level = read_brightness(samsung);
Jason Stubbsac080522011-09-20 09:16:13 -0700485
486 /*
487 * Some laptops exhibit the strange behaviour of stepping toward
488 * (rather than setting) the brightness except when changing to/from
489 * brightness level 0. This behaviour is checked for here and worked
490 * around in set_brightness.
491 */
492
John Serockba05b232011-10-13 06:42:01 -0400493 if (orig_level == 0)
Corentin Chary5dea7a22011-11-26 10:59:59 +0100494 set_brightness(samsung, 1);
John Serockba05b232011-10-13 06:42:01 -0400495
Corentin Chary5dea7a22011-11-26 10:59:59 +0100496 initial_level = read_brightness(samsung);
John Serockba05b232011-10-13 06:42:01 -0400497
Jason Stubbsac080522011-09-20 09:16:13 -0700498 if (initial_level <= 2)
499 check_level = initial_level + 2;
500 else
501 check_level = initial_level - 2;
502
Corentin Charya6df4892011-11-26 10:59:58 +0100503 samsung->has_stepping_quirk = false;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100504 set_brightness(samsung, check_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700505
Corentin Chary5dea7a22011-11-26 10:59:59 +0100506 if (read_brightness(samsung) != check_level) {
Corentin Charya6df4892011-11-26 10:59:58 +0100507 samsung->has_stepping_quirk = true;
Jason Stubbsac080522011-09-20 09:16:13 -0700508 pr_info("enabled workaround for brightness stepping quirk\n");
509 }
510
Corentin Chary5dea7a22011-11-26 10:59:59 +0100511 set_brightness(samsung, orig_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700512}
513
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500514static int update_status(struct backlight_device *bd)
515{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100516 struct samsung_laptop *samsung = bl_get_data(bd);
Corentin Charya6df4892011-11-26 10:59:58 +0100517 const struct sabi_commands *commands = &samsung->config->commands;
518
Corentin Chary5dea7a22011-11-26 10:59:59 +0100519 set_brightness(samsung, bd->props.brightness);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500520
521 if (bd->props.power == FB_BLANK_UNBLANK)
Corentin Chary7e960712011-11-26 11:00:02 +0100522 sabi_set_commandb(samsung, commands->set_backlight, 1);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500523 else
Corentin Chary7e960712011-11-26 11:00:02 +0100524 sabi_set_commandb(samsung, commands->set_backlight, 0);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100525
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500526 return 0;
527}
528
529static const struct backlight_ops backlight_ops = {
530 .get_brightness = get_brightness,
531 .update_status = update_status,
532};
533
Corentin Chary84d482f2011-11-26 11:00:09 +0100534static int seclinux_rfkill_set(void *data, bool blocked)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500535{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100536 struct samsung_laptop *samsung = data;
Corentin Charya6df4892011-11-26 10:59:58 +0100537 const struct sabi_commands *commands = &samsung->config->commands;
538
Corentin Chary84d482f2011-11-26 11:00:09 +0100539 return sabi_set_commandb(samsung, commands->set_wireless_button,
540 !blocked);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500541}
542
Corentin Chary84d482f2011-11-26 11:00:09 +0100543static struct rfkill_ops seclinux_rfkill_ops = {
544 .set_block = seclinux_rfkill_set,
545};
546
547static int swsmi_wireless_status(struct samsung_laptop *samsung,
548 struct sabi_data *data)
549{
550 const struct sabi_commands *commands = &samsung->config->commands;
551
552 return sabi_command(samsung, commands->get_wireless_status,
553 NULL, data);
554}
555
556static int swsmi_rfkill_set(void *priv, bool blocked)
557{
558 struct samsung_rfkill *srfkill = priv;
559 struct samsung_laptop *samsung = srfkill->samsung;
560 const struct sabi_commands *commands = &samsung->config->commands;
561 struct sabi_data data;
562 int ret, i;
563
564 ret = swsmi_wireless_status(samsung, &data);
565 if (ret)
566 return ret;
567
568 /* Don't set the state for non-present devices */
569 for (i = 0; i < 4; i++)
570 if (data.data[i] == 0x02)
571 data.data[1] = 0;
572
573 if (srfkill->type == RFKILL_TYPE_WLAN)
574 data.data[WL_STATUS_WLAN] = !blocked;
575 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
576 data.data[WL_STATUS_BT] = !blocked;
577
578 return sabi_command(samsung, commands->set_wireless_status,
579 &data, &data);
580}
581
582static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
583{
584 struct samsung_rfkill *srfkill = priv;
585 struct samsung_laptop *samsung = srfkill->samsung;
586 struct sabi_data data;
587 int ret;
588
589 ret = swsmi_wireless_status(samsung, &data);
590 if (ret)
591 return ;
592
593 if (srfkill->type == RFKILL_TYPE_WLAN)
594 ret = data.data[WL_STATUS_WLAN];
595 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
596 ret = data.data[WL_STATUS_BT];
597 else
598 return ;
599
600 rfkill_set_sw_state(rfkill, !ret);
601}
602
603static struct rfkill_ops swsmi_rfkill_ops = {
604 .set_block = swsmi_rfkill_set,
605 .query = swsmi_rfkill_query,
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500606};
607
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500608static ssize_t get_performance_level(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100611 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100612 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100613 const struct sabi_commands *commands = &config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100614 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500615 int retval;
616 int i;
617
618 /* Read the state */
Corentin Chary7e960712011-11-26 11:00:02 +0100619 retval = sabi_command(samsung, commands->get_performance_level,
620 NULL, &sretval);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500621 if (retval)
622 return retval;
623
624 /* The logic is backwards, yeah, lots of fun... */
Corentin Charya6df4892011-11-26 10:59:58 +0100625 for (i = 0; config->performance_levels[i].name; ++i) {
Corentin Chary7e960712011-11-26 11:00:02 +0100626 if (sretval.data[0] == config->performance_levels[i].value)
Corentin Charya6df4892011-11-26 10:59:58 +0100627 return sprintf(buf, "%s\n", config->performance_levels[i].name);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500628 }
629 return sprintf(buf, "%s\n", "unknown");
630}
631
632static ssize_t set_performance_level(struct device *dev,
633 struct device_attribute *attr, const char *buf,
634 size_t count)
635{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100636 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100637 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100638 const struct sabi_commands *commands = &config->commands;
639 int i;
Corentin Charya6df4892011-11-26 10:59:58 +0100640
Corentin Chary5dea7a22011-11-26 10:59:59 +0100641 if (count < 1)
642 return count;
643
644 for (i = 0; config->performance_levels[i].name; ++i) {
645 const struct sabi_performance_level *level =
646 &config->performance_levels[i];
647 if (!strncasecmp(level->name, buf, strlen(level->name))) {
Corentin Chary7e960712011-11-26 11:00:02 +0100648 sabi_set_commandb(samsung,
649 commands->set_performance_level,
650 level->value);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100651 break;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500652 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500653 }
Corentin Chary5dea7a22011-11-26 10:59:59 +0100654
655 if (!config->performance_levels[i].name)
656 return -EINVAL;
657
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500658 return count;
659}
Corentin Chary5dea7a22011-11-26 10:59:59 +0100660
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500661static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
662 get_performance_level, set_performance_level);
663
Corentin Charycb5b5c92011-11-26 11:00:05 +0100664static int read_battery_life_extender(struct samsung_laptop *samsung)
665{
666 const struct sabi_commands *commands = &samsung->config->commands;
667 struct sabi_data data;
668 int retval;
669
670 if (commands->get_battery_life_extender == 0xFFFF)
671 return -ENODEV;
672
673 memset(&data, 0, sizeof(data));
674 data.data[0] = 0x80;
675 retval = sabi_command(samsung, commands->get_battery_life_extender,
676 &data, &data);
677
678 if (retval)
679 return retval;
680
681 if (data.data[0] != 0 && data.data[0] != 1)
682 return -ENODEV;
683
684 return data.data[0];
685}
686
687static int write_battery_life_extender(struct samsung_laptop *samsung,
688 int enabled)
689{
690 const struct sabi_commands *commands = &samsung->config->commands;
691 struct sabi_data data;
692
693 memset(&data, 0, sizeof(data));
694 data.data[0] = 0x80 | enabled;
695 return sabi_command(samsung, commands->set_battery_life_extender,
696 &data, NULL);
697}
698
699static ssize_t get_battery_life_extender(struct device *dev,
700 struct device_attribute *attr,
701 char *buf)
702{
703 struct samsung_laptop *samsung = dev_get_drvdata(dev);
704 int ret;
705
706 ret = read_battery_life_extender(samsung);
707 if (ret < 0)
708 return ret;
709
710 return sprintf(buf, "%d\n", ret);
711}
712
713static ssize_t set_battery_life_extender(struct device *dev,
714 struct device_attribute *attr,
715 const char *buf, size_t count)
716{
717 struct samsung_laptop *samsung = dev_get_drvdata(dev);
718 int ret, value;
719
720 if (!count || sscanf(buf, "%i", &value) != 1)
721 return -EINVAL;
722
723 ret = write_battery_life_extender(samsung, !!value);
724 if (ret < 0)
725 return ret;
726
727 return count;
728}
729
730static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
731 get_battery_life_extender, set_battery_life_extender);
732
Corentin Chary3a75d372011-11-26 11:00:06 +0100733static int read_usb_charge(struct samsung_laptop *samsung)
734{
735 const struct sabi_commands *commands = &samsung->config->commands;
736 struct sabi_data data;
737 int retval;
738
739 if (commands->get_usb_charge == 0xFFFF)
740 return -ENODEV;
741
742 memset(&data, 0, sizeof(data));
743 data.data[0] = 0x80;
744 retval = sabi_command(samsung, commands->get_usb_charge,
745 &data, &data);
746
747 if (retval)
748 return retval;
749
750 if (data.data[0] != 0 && data.data[0] != 1)
751 return -ENODEV;
752
753 return data.data[0];
754}
755
756static int write_usb_charge(struct samsung_laptop *samsung,
757 int enabled)
758{
759 const struct sabi_commands *commands = &samsung->config->commands;
760 struct sabi_data data;
761
762 memset(&data, 0, sizeof(data));
763 data.data[0] = 0x80 | enabled;
764 return sabi_command(samsung, commands->set_usb_charge,
765 &data, NULL);
766}
767
768static ssize_t get_usb_charge(struct device *dev,
769 struct device_attribute *attr,
770 char *buf)
771{
772 struct samsung_laptop *samsung = dev_get_drvdata(dev);
773 int ret;
774
775 ret = read_usb_charge(samsung);
776 if (ret < 0)
777 return ret;
778
779 return sprintf(buf, "%d\n", ret);
780}
781
782static ssize_t set_usb_charge(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
785{
786 struct samsung_laptop *samsung = dev_get_drvdata(dev);
787 int ret, value;
788
789 if (!count || sscanf(buf, "%i", &value) != 1)
790 return -EINVAL;
791
792 ret = write_usb_charge(samsung, !!value);
793 if (ret < 0)
794 return ret;
795
796 return count;
797}
798
799static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
800 get_usb_charge, set_usb_charge);
801
Corentin Charya66c1662011-11-26 11:00:01 +0100802static struct attribute *platform_attributes[] = {
803 &dev_attr_performance_level.attr,
Corentin Charycb5b5c92011-11-26 11:00:05 +0100804 &dev_attr_battery_life_extender.attr,
Corentin Chary3a75d372011-11-26 11:00:06 +0100805 &dev_attr_usb_charge.attr,
Corentin Charya66c1662011-11-26 11:00:01 +0100806 NULL
807};
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500808
Corentin Chary5dea7a22011-11-26 10:59:59 +0100809static int find_signature(void __iomem *memcheck, const char *testStr)
810{
811 int i = 0;
812 int loca;
813
814 for (loca = 0; loca < 0xffff; loca++) {
815 char temp = readb(memcheck + loca);
816
817 if (temp == testStr[i]) {
818 if (i == strlen(testStr)-1)
819 break;
820 ++i;
821 } else {
822 i = 0;
823 }
824 }
825 return loca;
826}
827
828static void samsung_rfkill_exit(struct samsung_laptop *samsung)
829{
Corentin Chary84d482f2011-11-26 11:00:09 +0100830 if (samsung->wlan.rfkill) {
831 rfkill_unregister(samsung->wlan.rfkill);
832 rfkill_destroy(samsung->wlan.rfkill);
833 samsung->wlan.rfkill = NULL;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100834 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100835 if (samsung->bluetooth.rfkill) {
836 rfkill_unregister(samsung->bluetooth.rfkill);
837 rfkill_destroy(samsung->bluetooth.rfkill);
838 samsung->bluetooth.rfkill = NULL;
839 }
840}
841
842static int samsung_new_rfkill(struct samsung_laptop *samsung,
843 struct samsung_rfkill *arfkill,
844 const char *name, enum rfkill_type type,
845 const struct rfkill_ops *ops,
846 int blocked)
847{
848 struct rfkill **rfkill = &arfkill->rfkill;
849 int ret;
850
851 arfkill->type = type;
852 arfkill->samsung = samsung;
853
854 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
855 type, ops, arfkill);
856
857 if (!*rfkill)
858 return -EINVAL;
859
860 if (blocked != -1)
861 rfkill_init_sw_state(*rfkill, blocked);
862
863 ret = rfkill_register(*rfkill);
864 if (ret) {
865 rfkill_destroy(*rfkill);
866 *rfkill = NULL;
867 return ret;
868 }
869 return 0;
870}
871
872static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
873{
874 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
875 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
876}
877
878static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
879{
880 struct sabi_data data;
881 int ret;
882
883 ret = swsmi_wireless_status(samsung, &data);
884 if (ret)
885 return ret;
886
887 /* 0x02 seems to mean that the device is no present/available */
888
889 if (data.data[WL_STATUS_WLAN] != 0x02)
890 ret = samsung_new_rfkill(samsung, &samsung->wlan,
891 "samsung-wlan",
892 RFKILL_TYPE_WLAN,
893 &swsmi_rfkill_ops,
894 !data.data[WL_STATUS_WLAN]);
895 if (ret)
896 goto exit;
897
898 if (data.data[WL_STATUS_BT] != 0x02)
899 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
900 "samsung-bluetooth",
901 RFKILL_TYPE_BLUETOOTH,
902 &swsmi_rfkill_ops,
903 !data.data[WL_STATUS_BT]);
904 if (ret)
905 goto exit;
906
907exit:
908 if (ret)
909 samsung_rfkill_exit(samsung);
910
911 return ret;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100912}
913
914static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
915{
Corentin Chary84d482f2011-11-26 11:00:09 +0100916 if (samsung->config->sabi_version == 2)
917 return samsung_rfkill_init_seclinux(samsung);
918 if (samsung->config->sabi_version == 3)
919 return samsung_rfkill_init_swsmi(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100920 return 0;
921}
922
Corentin Charyf674ebf2011-11-26 11:00:08 +0100923static int kbd_backlight_enable(struct samsung_laptop *samsung)
924{
925 const struct sabi_commands *commands = &samsung->config->commands;
926 struct sabi_data data;
927 int retval;
928
929 if (commands->kbd_backlight == 0xFFFF)
930 return -ENODEV;
931
932 memset(&data, 0, sizeof(data));
933 data.d0 = 0xaabb;
934 retval = sabi_command(samsung, commands->kbd_backlight,
935 &data, &data);
936
937 if (retval)
938 return retval;
939
940 if (data.d0 != 0xccdd)
941 return -ENODEV;
942 return 0;
943}
944
945static int kbd_backlight_read(struct samsung_laptop *samsung)
946{
947 const struct sabi_commands *commands = &samsung->config->commands;
948 struct sabi_data data;
949 int retval;
950
951 memset(&data, 0, sizeof(data));
952 data.data[0] = 0x81;
953 retval = sabi_command(samsung, commands->kbd_backlight,
954 &data, &data);
955
956 if (retval)
957 return retval;
958
959 return data.data[0];
960}
961
962static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
963{
964 const struct sabi_commands *commands = &samsung->config->commands;
965 struct sabi_data data;
966
967 memset(&data, 0, sizeof(data));
968 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
969 return sabi_command(samsung, commands->kbd_backlight,
970 &data, NULL);
971}
972
973static void kbd_led_update(struct work_struct *work)
974{
975 struct samsung_laptop *samsung;
976
977 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
978 kbd_backlight_write(samsung, samsung->kbd_led_wk);
979}
980
981static void kbd_led_set(struct led_classdev *led_cdev,
982 enum led_brightness value)
983{
984 struct samsung_laptop *samsung;
985
986 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
987
988 if (value > samsung->kbd_led.max_brightness)
989 value = samsung->kbd_led.max_brightness;
990 else if (value < 0)
991 value = 0;
992
993 samsung->kbd_led_wk = value;
994 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
995}
996
997static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
998{
999 struct samsung_laptop *samsung;
1000
1001 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1002 return kbd_backlight_read(samsung);
1003}
1004
1005static void samsung_leds_exit(struct samsung_laptop *samsung)
1006{
1007 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1008 led_classdev_unregister(&samsung->kbd_led);
1009 if (samsung->led_workqueue)
1010 destroy_workqueue(samsung->led_workqueue);
1011}
1012
1013static int __init samsung_leds_init(struct samsung_laptop *samsung)
1014{
1015 int ret = 0;
1016
1017 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1018 if (!samsung->led_workqueue)
1019 return -ENOMEM;
1020
1021 if (kbd_backlight_enable(samsung) >= 0) {
1022 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1023
1024 samsung->kbd_led.name = "samsung::kbd_backlight";
1025 samsung->kbd_led.brightness_set = kbd_led_set;
1026 samsung->kbd_led.brightness_get = kbd_led_get;
1027 samsung->kbd_led.max_brightness = 8;
1028
1029 ret = led_classdev_register(&samsung->platform_device->dev,
1030 &samsung->kbd_led);
1031 }
1032
1033 if (ret)
1034 samsung_leds_exit(samsung);
1035
1036 return ret;
1037}
1038
Corentin Chary5dea7a22011-11-26 10:59:59 +01001039static void samsung_backlight_exit(struct samsung_laptop *samsung)
1040{
1041 if (samsung->backlight_device) {
1042 backlight_device_unregister(samsung->backlight_device);
1043 samsung->backlight_device = NULL;
1044 }
1045}
1046
1047static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1048{
1049 struct backlight_device *bd;
1050 struct backlight_properties props;
1051
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001052 if (!samsung->handle_backlight)
1053 return 0;
1054
Corentin Chary5dea7a22011-11-26 10:59:59 +01001055 memset(&props, 0, sizeof(struct backlight_properties));
1056 props.type = BACKLIGHT_PLATFORM;
1057 props.max_brightness = samsung->config->max_brightness -
1058 samsung->config->min_brightness;
1059
1060 bd = backlight_device_register("samsung",
1061 &samsung->platform_device->dev,
1062 samsung, &backlight_ops,
1063 &props);
1064 if (IS_ERR(bd))
1065 return PTR_ERR(bd);
1066
1067 samsung->backlight_device = bd;
1068 samsung->backlight_device->props.brightness = read_brightness(samsung);
1069 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1070 backlight_update_status(samsung->backlight_device);
1071
1072 return 0;
1073}
1074
Corentin Charya66c1662011-11-26 11:00:01 +01001075static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
1076 struct attribute *attr, int idx)
1077{
1078 struct device *dev = container_of(kobj, struct device, kobj);
1079 struct platform_device *pdev = to_platform_device(dev);
1080 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1081 bool ok = true;
1082
1083 if (attr == &dev_attr_performance_level.attr)
1084 ok = !!samsung->config->performance_levels[0].name;
Corentin Charycb5b5c92011-11-26 11:00:05 +01001085 if (attr == &dev_attr_battery_life_extender.attr)
1086 ok = !!(read_battery_life_extender(samsung) >= 0);
Corentin Chary3a75d372011-11-26 11:00:06 +01001087 if (attr == &dev_attr_usb_charge.attr)
1088 ok = !!(read_usb_charge(samsung) >= 0);
Corentin Charya66c1662011-11-26 11:00:01 +01001089
1090 return ok ? attr->mode : 0;
1091}
1092
1093static struct attribute_group platform_attribute_group = {
1094 .is_visible = samsung_sysfs_is_visible,
1095 .attrs = platform_attributes
1096};
1097
Corentin Chary5dea7a22011-11-26 10:59:59 +01001098static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1099{
Corentin Charya66c1662011-11-26 11:00:01 +01001100 struct platform_device *device = samsung->platform_device;
1101
1102 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001103}
1104
1105static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1106{
Corentin Charya66c1662011-11-26 11:00:01 +01001107 struct platform_device *device = samsung->platform_device;
1108
1109 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1110
Corentin Chary5dea7a22011-11-26 10:59:59 +01001111}
1112
Corentin Chary5b80fc42011-11-26 11:00:03 +01001113static int show_call(struct seq_file *m, void *data)
1114{
1115 struct samsung_laptop *samsung = m->private;
1116 struct sabi_data *sdata = &samsung->debug.data;
1117 int ret;
1118
1119 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1120 samsung->debug.command,
1121 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1122
1123 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1124
1125 if (ret) {
1126 seq_printf(m, "SABI command 0x%04x failed\n",
1127 samsung->debug.command);
1128 return ret;
1129 }
1130
1131 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1132 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1133 return 0;
1134}
1135
1136static int samsung_debugfs_open(struct inode *inode, struct file *file)
1137{
1138 return single_open(file, show_call, inode->i_private);
1139}
1140
1141static const struct file_operations samsung_laptop_call_io_ops = {
1142 .owner = THIS_MODULE,
1143 .open = samsung_debugfs_open,
1144 .read = seq_read,
1145 .llseek = seq_lseek,
1146 .release = single_release,
1147};
1148
1149static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1150{
1151 debugfs_remove_recursive(samsung->debug.root);
1152}
1153
1154static int samsung_debugfs_init(struct samsung_laptop *samsung)
1155{
1156 struct dentry *dent;
1157
1158 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1159 if (!samsung->debug.root) {
1160 pr_err("failed to create debugfs directory");
1161 goto error_debugfs;
1162 }
1163
1164 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1165 samsung->debug.f0000_wrapper.size = 0xffff;
1166
1167 samsung->debug.data_wrapper.data = &samsung->debug.data;
1168 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1169
Corentin Chary6f6ae062011-11-26 11:00:11 +01001170 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1171 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1172
Corentin Chary5b80fc42011-11-26 11:00:03 +01001173 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1174 samsung->debug.root, &samsung->debug.command);
1175 if (!dent)
1176 goto error_debugfs;
1177
1178 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1179 &samsung->debug.data.d0);
1180 if (!dent)
1181 goto error_debugfs;
1182
1183 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1184 &samsung->debug.data.d1);
1185 if (!dent)
1186 goto error_debugfs;
1187
1188 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1189 &samsung->debug.data.d2);
1190 if (!dent)
1191 goto error_debugfs;
1192
1193 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1194 &samsung->debug.data.d3);
1195 if (!dent)
1196 goto error_debugfs;
1197
1198 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1199 samsung->debug.root,
1200 &samsung->debug.data_wrapper);
1201 if (!dent)
1202 goto error_debugfs;
1203
1204 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1205 samsung->debug.root,
1206 &samsung->debug.f0000_wrapper);
1207 if (!dent)
1208 goto error_debugfs;
1209
1210 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1211 samsung->debug.root, samsung,
1212 &samsung_laptop_call_io_ops);
1213 if (!dent)
1214 goto error_debugfs;
1215
Corentin Chary6f6ae062011-11-26 11:00:11 +01001216 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1217 samsung->debug.root,
1218 &samsung->debug.sdiag_wrapper);
1219 if (!dent)
1220 goto error_debugfs;
1221
Corentin Chary5b80fc42011-11-26 11:00:03 +01001222 return 0;
1223
1224error_debugfs:
1225 samsung_debugfs_exit(samsung);
1226 return -ENOMEM;
1227}
1228
Corentin Chary5dea7a22011-11-26 10:59:59 +01001229static void samsung_sabi_exit(struct samsung_laptop *samsung)
1230{
1231 const struct sabi_config *config = samsung->config;
1232
1233 /* Turn off "Linux" mode in the BIOS */
1234 if (config && config->commands.set_linux != 0xff)
Corentin Chary7e960712011-11-26 11:00:02 +01001235 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001236
1237 if (samsung->sabi_iface) {
1238 iounmap(samsung->sabi_iface);
1239 samsung->sabi_iface = NULL;
1240 }
1241 if (samsung->f0000_segment) {
1242 iounmap(samsung->f0000_segment);
1243 samsung->f0000_segment = NULL;
1244 }
1245
1246 samsung->config = NULL;
1247}
1248
Corentin Chary49dd7732011-11-26 11:00:04 +01001249static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1250 unsigned int ifaceP)
Corentin Chary5dea7a22011-11-26 10:59:59 +01001251{
1252 const struct sabi_config *config = samsung->config;
1253
1254 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1255 loca + 0xf0000 - 6);
Corentin Chary49dd7732011-11-26 11:00:04 +01001256
Corentin Chary5dea7a22011-11-26 10:59:59 +01001257 printk(KERN_DEBUG "SABI header:\n");
1258 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1259 readw(samsung->sabi + config->header_offsets.port));
1260 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1261 readb(samsung->sabi + config->header_offsets.iface_func));
1262 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1263 readb(samsung->sabi + config->header_offsets.en_mem));
1264 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1265 readb(samsung->sabi + config->header_offsets.re_mem));
1266 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1267 readw(samsung->sabi + config->header_offsets.data_offset));
1268 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1269 readw(samsung->sabi + config->header_offsets.data_segment));
Corentin Chary5dea7a22011-11-26 10:59:59 +01001270
Corentin Chary49dd7732011-11-26 11:00:04 +01001271 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001272}
1273
Corentin Chary6f6ae062011-11-26 11:00:11 +01001274static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1275{
1276 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1277 int i;
1278
1279 if (loca == 0xffff)
1280 return ;
1281
1282 /* Example:
1283 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1284 *
1285 * Product name: 90X3A
1286 * BIOS Version: 07HL
1287 */
1288 loca += 1;
1289 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1290 char temp = readb(samsung->f0000_segment + loca);
1291
1292 if (isalnum(temp) || temp == '/' || temp == '-')
1293 samsung->sdiag[i++] = temp;
1294 else
1295 break ;
1296 }
1297
1298 if (debug && samsung->sdiag[0])
1299 pr_info("sdiag: %s", samsung->sdiag);
1300}
1301
Corentin Chary5dea7a22011-11-26 10:59:59 +01001302static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1303{
1304 const struct sabi_config *config = NULL;
1305 const struct sabi_commands *commands;
1306 unsigned int ifaceP;
1307 int ret = 0;
1308 int i;
1309 int loca;
1310
1311 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1312 if (!samsung->f0000_segment) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001313 if (debug || force)
1314 pr_err("Can't map the segment at 0xf0000\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001315 ret = -EINVAL;
1316 goto exit;
1317 }
1318
Corentin Chary6f6ae062011-11-26 11:00:11 +01001319 samsung_sabi_diag(samsung);
1320
Corentin Chary5dea7a22011-11-26 10:59:59 +01001321 /* Try to find one of the signatures in memory to find the header */
1322 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1323 samsung->config = &sabi_configs[i];
1324 loca = find_signature(samsung->f0000_segment,
1325 samsung->config->test_string);
1326 if (loca != 0xffff)
1327 break;
1328 }
1329
1330 if (loca == 0xffff) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001331 if (debug || force)
1332 pr_err("This computer does not support SABI\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001333 ret = -ENODEV;
1334 goto exit;
1335 }
1336
1337 config = samsung->config;
1338 commands = &config->commands;
1339
1340 /* point to the SMI port Number */
1341 loca += 1;
1342 samsung->sabi = (samsung->f0000_segment + loca);
1343
Corentin Chary5dea7a22011-11-26 10:59:59 +01001344 /* Get a pointer to the SABI Interface */
1345 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1346 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
Corentin Chary49dd7732011-11-26 11:00:04 +01001347
1348 if (debug)
1349 samsung_sabi_infos(samsung, loca, ifaceP);
1350
Corentin Chary5dea7a22011-11-26 10:59:59 +01001351 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1352 if (!samsung->sabi_iface) {
1353 pr_err("Can't remap %x\n", ifaceP);
1354 ret = -EINVAL;
1355 goto exit;
1356 }
1357
Corentin Chary5dea7a22011-11-26 10:59:59 +01001358 /* Turn on "Linux" mode in the BIOS */
1359 if (commands->set_linux != 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +01001360 int retval = sabi_set_commandb(samsung,
1361 commands->set_linux, 0x81);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001362 if (retval) {
1363 pr_warn("Linux mode was not set!\n");
1364 ret = -ENODEV;
1365 goto exit;
1366 }
1367 }
1368
1369 /* Check for stepping quirk */
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001370 if (samsung->handle_backlight)
1371 check_for_stepping_quirk(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001372
1373exit:
1374 if (ret)
1375 samsung_sabi_exit(samsung);
1376
1377 return ret;
1378}
1379
1380static void samsung_platform_exit(struct samsung_laptop *samsung)
1381{
1382 if (samsung->platform_device) {
1383 platform_device_unregister(samsung->platform_device);
1384 samsung->platform_device = NULL;
1385 }
1386}
1387
1388static int __init samsung_platform_init(struct samsung_laptop *samsung)
1389{
1390 struct platform_device *pdev;
1391
1392 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1393 if (IS_ERR(pdev))
1394 return PTR_ERR(pdev);
1395
1396 samsung->platform_device = pdev;
1397 platform_set_drvdata(samsung->platform_device, samsung);
1398 return 0;
1399}
1400
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001401static struct dmi_system_id __initdata samsung_dmi_table[] = {
1402 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001403 .matches = {
1404 DMI_MATCH(DMI_SYS_VENDOR,
1405 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001406 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001407 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001408 },
1409 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001410 .matches = {
1411 DMI_MATCH(DMI_SYS_VENDOR,
1412 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001413 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001414 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001415 },
1416 {
J Witteveen4e2441c2011-07-03 13:15:44 +02001417 .matches = {
1418 DMI_MATCH(DMI_SYS_VENDOR,
1419 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001420 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
J Witteveen4e2441c2011-07-03 13:15:44 +02001421 },
J Witteveen4e2441c2011-07-03 13:15:44 +02001422 },
1423 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001424 .matches = {
1425 DMI_MATCH(DMI_SYS_VENDOR,
1426 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001427 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001428 },
Tommaso Massimi7500eeb2011-09-20 09:16:09 -07001429 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001430 { },
1431};
1432MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1433
Corentin Chary5dea7a22011-11-26 10:59:59 +01001434static struct platform_device *samsung_platform_device;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001435
1436static int __init samsung_init(void)
1437{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001438 struct samsung_laptop *samsung;
1439 int ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001440
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001441 if (!force && !dmi_check_system(samsung_dmi_table))
1442 return -ENODEV;
1443
Corentin Charya6df4892011-11-26 10:59:58 +01001444 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1445 if (!samsung)
1446 return -ENOMEM;
1447
1448 mutex_init(&samsung->sabi_mutex);
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001449 samsung->handle_backlight = true;
1450
1451#ifdef CONFIG_ACPI
1452 /* Don't handle backlight here if the acpi video already handle it */
Corentin Chary3be324a2011-11-26 11:00:10 +01001453 if (acpi_video_backlight_support())
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001454 samsung->handle_backlight = false;
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001455#endif
Corentin Chary5dea7a22011-11-26 10:59:59 +01001456 ret = samsung_platform_init(samsung);
1457 if (ret)
1458 goto error_platform;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001459
Corentin Chary5dea7a22011-11-26 10:59:59 +01001460 ret = samsung_sabi_init(samsung);
1461 if (ret)
1462 goto error_sabi;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001463
Corentin Chary3be324a2011-11-26 11:00:10 +01001464#ifdef CONFIG_ACPI
1465 /* Only log that if we are really on a sabi platform */
1466 if (acpi_video_backlight_support())
1467 pr_info("Backlight controlled by ACPI video driver\n");
1468#endif
1469
Corentin Chary5dea7a22011-11-26 10:59:59 +01001470 ret = samsung_sysfs_init(samsung);
1471 if (ret)
1472 goto error_sysfs;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001473
Corentin Chary5dea7a22011-11-26 10:59:59 +01001474 ret = samsung_backlight_init(samsung);
1475 if (ret)
1476 goto error_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +01001477
Corentin Chary5dea7a22011-11-26 10:59:59 +01001478 ret = samsung_rfkill_init(samsung);
1479 if (ret)
1480 goto error_rfkill;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001481
Corentin Charyf674ebf2011-11-26 11:00:08 +01001482 ret = samsung_leds_init(samsung);
1483 if (ret)
1484 goto error_leds;
1485
Corentin Chary5b80fc42011-11-26 11:00:03 +01001486 ret = samsung_debugfs_init(samsung);
1487 if (ret)
1488 goto error_debugfs;
1489
Corentin Chary5dea7a22011-11-26 10:59:59 +01001490 samsung_platform_device = samsung->platform_device;
1491 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001492
Corentin Chary5b80fc42011-11-26 11:00:03 +01001493error_debugfs:
Corentin Charyf674ebf2011-11-26 11:00:08 +01001494 samsung_leds_exit(samsung);
1495error_leds:
Corentin Chary5b80fc42011-11-26 11:00:03 +01001496 samsung_rfkill_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001497error_rfkill:
1498 samsung_backlight_exit(samsung);
1499error_backlight:
1500 samsung_sysfs_exit(samsung);
1501error_sysfs:
1502 samsung_sabi_exit(samsung);
1503error_sabi:
1504 samsung_platform_exit(samsung);
1505error_platform:
Corentin Charya6df4892011-11-26 10:59:58 +01001506 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001507 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001508}
1509
1510static void __exit samsung_exit(void)
1511{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001512 struct samsung_laptop *samsung;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001513
Corentin Chary5dea7a22011-11-26 10:59:59 +01001514 samsung = platform_get_drvdata(samsung_platform_device);
Corentin Charya6df4892011-11-26 10:59:58 +01001515
Corentin Chary5b80fc42011-11-26 11:00:03 +01001516 samsung_debugfs_exit(samsung);
Corentin Charyf674ebf2011-11-26 11:00:08 +01001517 samsung_leds_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001518 samsung_rfkill_exit(samsung);
1519 samsung_backlight_exit(samsung);
1520 samsung_sysfs_exit(samsung);
1521 samsung_sabi_exit(samsung);
1522 samsung_platform_exit(samsung);
1523
Corentin Charya6df4892011-11-26 10:59:58 +01001524 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001525 samsung_platform_device = NULL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001526}
1527
1528module_init(samsung_init);
1529module_exit(samsung_exit);
1530
1531MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1532MODULE_DESCRIPTION("Samsung Backlight driver");
1533MODULE_LICENSE("GPL");