blob: 4787afdf11dc20e69af8fe5324366d4bd65aeb98 [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>
David Rientjes82c333a2012-03-20 09:53:06 +010028#include <linux/ctype.h>
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050029
30/*
31 * This driver is needed because a number of Samsung laptops do not hook
32 * their control settings through ACPI. So we have to poke around in the
33 * BIOS to do things like brightness values, and "special" key controls.
34 */
35
36/*
37 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
38 * be reserved by the BIOS (which really doesn't make much sense), we tell
39 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
40 */
41#define MAX_BRIGHT 0x07
42
43
44#define SABI_IFACE_MAIN 0x00
45#define SABI_IFACE_SUB 0x02
46#define SABI_IFACE_COMPLETE 0x04
47#define SABI_IFACE_DATA 0x05
48
Corentin Chary84d482f2011-11-26 11:00:09 +010049#define WL_STATUS_WLAN 0x0
50#define WL_STATUS_BT 0x2
51
Corentin Chary7e960712011-11-26 11:00:02 +010052/* Structure get/set data using sabi */
53struct sabi_data {
54 union {
55 struct {
56 u32 d0;
57 u32 d1;
58 u16 d2;
59 u8 d3;
60 };
61 u8 data[11];
62 };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050063};
64
65struct sabi_header_offsets {
66 u8 port;
67 u8 re_mem;
68 u8 iface_func;
69 u8 en_mem;
70 u8 data_offset;
71 u8 data_segment;
72};
73
74struct sabi_commands {
75 /*
76 * Brightness is 0 - 8, as described above.
77 * Value 0 is for the BIOS to use
78 */
Corentin Chary7e960712011-11-26 11:00:02 +010079 u16 get_brightness;
80 u16 set_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050081
82 /*
83 * first byte:
84 * 0x00 - wireless is off
85 * 0x01 - wireless is on
86 * second byte:
87 * 0x02 - 3G is off
88 * 0x03 - 3G is on
89 * TODO, verify 3G is correct, that doesn't seem right...
90 */
Corentin Chary7e960712011-11-26 11:00:02 +010091 u16 get_wireless_button;
92 u16 set_wireless_button;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050093
94 /* 0 is off, 1 is on */
Corentin Chary7e960712011-11-26 11:00:02 +010095 u16 get_backlight;
96 u16 set_backlight;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050097
98 /*
99 * 0x80 or 0x00 - no action
100 * 0x81 - recovery key pressed
101 */
Corentin Chary7e960712011-11-26 11:00:02 +0100102 u16 get_recovery_mode;
103 u16 set_recovery_mode;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500104
105 /*
106 * on seclinux: 0 is low, 1 is high,
107 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
108 */
Corentin Chary7e960712011-11-26 11:00:02 +0100109 u16 get_performance_level;
110 u16 set_performance_level;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500111
Corentin Charycb5b5c92011-11-26 11:00:05 +0100112 /* 0x80 is off, 0x81 is on */
113 u16 get_battery_life_extender;
114 u16 set_battery_life_extender;
115
Corentin Chary3a75d372011-11-26 11:00:06 +0100116 /* 0x80 is off, 0x81 is on */
117 u16 get_usb_charge;
118 u16 set_usb_charge;
119
Corentin Chary84d482f2011-11-26 11:00:09 +0100120 /* the first byte is for bluetooth and the third one is for wlan */
121 u16 get_wireless_status;
122 u16 set_wireless_status;
123
Corentin Charyf674ebf2011-11-26 11:00:08 +0100124 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
125 u16 kbd_backlight;
126
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500127 /*
128 * Tell the BIOS that Linux is running on this machine.
129 * 81 is on, 80 is off
130 */
Corentin Chary7e960712011-11-26 11:00:02 +0100131 u16 set_linux;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500132};
133
134struct sabi_performance_level {
135 const char *name;
Corentin Chary7e960712011-11-26 11:00:02 +0100136 u16 value;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500137};
138
139struct sabi_config {
Corentin Chary84d482f2011-11-26 11:00:09 +0100140 int sabi_version;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500141 const char *test_string;
142 u16 main_function;
143 const struct sabi_header_offsets header_offsets;
144 const struct sabi_commands commands;
145 const struct sabi_performance_level performance_levels[4];
146 u8 min_brightness;
147 u8 max_brightness;
148};
149
150static const struct sabi_config sabi_configs[] = {
151 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100152 /* I don't know if it is really 2, but it it is
153 * less than 3 anyway */
154 .sabi_version = 2,
155
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500156 .test_string = "SECLINUX",
157
158 .main_function = 0x4c49,
159
160 .header_offsets = {
161 .port = 0x00,
162 .re_mem = 0x02,
163 .iface_func = 0x03,
164 .en_mem = 0x04,
165 .data_offset = 0x05,
166 .data_segment = 0x07,
167 },
168
169 .commands = {
170 .get_brightness = 0x00,
171 .set_brightness = 0x01,
172
173 .get_wireless_button = 0x02,
174 .set_wireless_button = 0x03,
175
176 .get_backlight = 0x04,
177 .set_backlight = 0x05,
178
179 .get_recovery_mode = 0x06,
180 .set_recovery_mode = 0x07,
181
182 .get_performance_level = 0x08,
183 .set_performance_level = 0x09,
184
Corentin Charycb5b5c92011-11-26 11:00:05 +0100185 .get_battery_life_extender = 0xFFFF,
186 .set_battery_life_extender = 0xFFFF,
187
Corentin Chary3a75d372011-11-26 11:00:06 +0100188 .get_usb_charge = 0xFFFF,
189 .set_usb_charge = 0xFFFF,
190
Corentin Chary84d482f2011-11-26 11:00:09 +0100191 .get_wireless_status = 0xFFFF,
192 .set_wireless_status = 0xFFFF,
193
Corentin Charyf674ebf2011-11-26 11:00:08 +0100194 .kbd_backlight = 0xFFFF,
195
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500196 .set_linux = 0x0a,
197 },
198
199 .performance_levels = {
200 {
201 .name = "silent",
202 .value = 0,
203 },
204 {
205 .name = "normal",
206 .value = 1,
207 },
208 { },
209 },
210 .min_brightness = 1,
211 .max_brightness = 8,
212 },
213 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100214 .sabi_version = 3,
215
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500216 .test_string = "SwSmi@",
217
218 .main_function = 0x5843,
219
220 .header_offsets = {
221 .port = 0x00,
222 .re_mem = 0x04,
223 .iface_func = 0x02,
224 .en_mem = 0x03,
225 .data_offset = 0x05,
226 .data_segment = 0x07,
227 },
228
229 .commands = {
230 .get_brightness = 0x10,
231 .set_brightness = 0x11,
232
233 .get_wireless_button = 0x12,
234 .set_wireless_button = 0x13,
235
236 .get_backlight = 0x2d,
237 .set_backlight = 0x2e,
238
239 .get_recovery_mode = 0xff,
240 .set_recovery_mode = 0xff,
241
242 .get_performance_level = 0x31,
243 .set_performance_level = 0x32,
244
Corentin Charycb5b5c92011-11-26 11:00:05 +0100245 .get_battery_life_extender = 0x65,
246 .set_battery_life_extender = 0x66,
247
Corentin Chary3a75d372011-11-26 11:00:06 +0100248 .get_usb_charge = 0x67,
249 .set_usb_charge = 0x68,
250
Corentin Chary84d482f2011-11-26 11:00:09 +0100251 .get_wireless_status = 0x69,
252 .set_wireless_status = 0x6a,
253
Corentin Charyf674ebf2011-11-26 11:00:08 +0100254 .kbd_backlight = 0x78,
255
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500256 .set_linux = 0xff,
257 },
258
259 .performance_levels = {
260 {
261 .name = "normal",
262 .value = 0,
263 },
264 {
265 .name = "silent",
266 .value = 1,
267 },
268 {
269 .name = "overclock",
270 .value = 2,
271 },
272 { },
273 },
274 .min_brightness = 0,
275 .max_brightness = 8,
276 },
277 { },
278};
279
Corentin Chary5b80fc42011-11-26 11:00:03 +0100280/*
281 * samsung-laptop/ - debugfs root directory
282 * f0000_segment - dump f0000 segment
283 * command - current command
284 * data - current data
285 * d0, d1, d2, d3 - data fields
286 * call - call SABI using command and data
287 *
288 * This allow to call arbitrary sabi commands wihout
289 * modifying the driver at all.
290 * For example, setting the keyboard backlight brightness to 5
291 *
292 * echo 0x78 > command
293 * echo 0x0582 > d0
294 * echo 0 > d1
295 * echo 0 > d2
296 * echo 0 > d3
297 * cat call
298 */
299
300struct samsung_laptop_debug {
301 struct dentry *root;
302 struct sabi_data data;
303 u16 command;
304
305 struct debugfs_blob_wrapper f0000_wrapper;
306 struct debugfs_blob_wrapper data_wrapper;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100307 struct debugfs_blob_wrapper sdiag_wrapper;
Corentin Chary5b80fc42011-11-26 11:00:03 +0100308};
309
Corentin Chary84d482f2011-11-26 11:00:09 +0100310struct samsung_laptop;
311
312struct samsung_rfkill {
313 struct samsung_laptop *samsung;
314 struct rfkill *rfkill;
315 enum rfkill_type type;
316};
317
Corentin Charya6df4892011-11-26 10:59:58 +0100318struct samsung_laptop {
319 const struct sabi_config *config;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500320
Corentin Charya6df4892011-11-26 10:59:58 +0100321 void __iomem *sabi;
322 void __iomem *sabi_iface;
323 void __iomem *f0000_segment;
324
325 struct mutex sabi_mutex;
326
Corentin Chary5dea7a22011-11-26 10:59:59 +0100327 struct platform_device *platform_device;
Corentin Charya6df4892011-11-26 10:59:58 +0100328 struct backlight_device *backlight_device;
Corentin Chary84d482f2011-11-26 11:00:09 +0100329
330 struct samsung_rfkill wlan;
331 struct samsung_rfkill bluetooth;
Corentin Charya6df4892011-11-26 10:59:58 +0100332
Corentin Charyf674ebf2011-11-26 11:00:08 +0100333 struct led_classdev kbd_led;
334 int kbd_led_wk;
335 struct workqueue_struct *led_workqueue;
336 struct work_struct kbd_led_work;
337
Corentin Chary5b80fc42011-11-26 11:00:03 +0100338 struct samsung_laptop_debug debug;
339
Corentin Charyf34cd9c2011-11-26 11:00:00 +0100340 bool handle_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +0100341 bool has_stepping_quirk;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100342
343 char sdiag[64];
Corentin Charya6df4892011-11-26 10:59:58 +0100344};
345
Corentin Chary5dea7a22011-11-26 10:59:59 +0100346
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500347
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030348static bool force;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500349module_param(force, bool, 0);
350MODULE_PARM_DESC(force,
351 "Disable the DMI check and forces the driver to be loaded");
352
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030353static bool debug;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500354module_param(debug, bool, S_IRUGO | S_IWUSR);
355MODULE_PARM_DESC(debug, "Debug enabled or not");
356
Corentin Chary7e960712011-11-26 11:00:02 +0100357static int sabi_command(struct samsung_laptop *samsung, u16 command,
358 struct sabi_data *in,
359 struct sabi_data *out)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500360{
Corentin Charya6df4892011-11-26 10:59:58 +0100361 const struct sabi_config *config = samsung->config;
Corentin Chary7e960712011-11-26 11:00:02 +0100362 int ret = 0;
Corentin Charya6df4892011-11-26 10:59:58 +0100363 u16 port = readw(samsung->sabi + config->header_offsets.port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500364 u8 complete, iface_data;
365
Corentin Charya6df4892011-11-26 10:59:58 +0100366 mutex_lock(&samsung->sabi_mutex);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500367
Corentin Chary7e960712011-11-26 11:00:02 +0100368 if (debug) {
369 if (in)
Corentin Chary2e777182011-11-26 11:00:12 +0100370 pr_info("SABI command:0x%04x "
371 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100372 command, in->d0, in->d1, in->d2, in->d3);
373 else
Corentin Chary2e777182011-11-26 11:00:12 +0100374 pr_info("SABI command:0x%04x", command);
Corentin Chary7e960712011-11-26 11:00:02 +0100375 }
376
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500377 /* enable memory to be able to write to it */
Corentin Charya6df4892011-11-26 10:59:58 +0100378 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500379
380 /* write out the command */
Corentin Charya6df4892011-11-26 10:59:58 +0100381 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
382 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
383 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
Corentin Chary7e960712011-11-26 11:00:02 +0100384 if (in) {
385 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
386 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
387 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
388 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
389 }
Corentin Charya6df4892011-11-26 10:59:58 +0100390 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500391
392 /* write protect memory to make it safe */
Corentin Charya6df4892011-11-26 10:59:58 +0100393 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500394
395 /* see if the command actually succeeded */
Corentin Charya6df4892011-11-26 10:59:58 +0100396 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
397 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
Corentin Chary2e777182011-11-26 11:00:12 +0100398
399 /* iface_data = 0xFF happens when a command is not known
400 * so we only add a warning in debug mode since we will
401 * probably issue some unknown command at startup to find
402 * out which features are supported */
403 if (complete != 0xaa || (iface_data == 0xff && debug))
Corentin Chary7e960712011-11-26 11:00:02 +0100404 pr_warn("SABI command 0x%04x failed with"
405 " completion flag 0x%02x and interface data 0x%02x",
406 command, complete, iface_data);
Corentin Chary2e777182011-11-26 11:00:12 +0100407
408 if (complete != 0xaa || iface_data == 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +0100409 ret = -EINVAL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500410 goto exit;
411 }
Corentin Chary7e960712011-11-26 11:00:02 +0100412
413 if (out) {
414 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
415 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
416 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
417 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
418 }
419
420 if (debug && out) {
Corentin Chary2e777182011-11-26 11:00:12 +0100421 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100422 out->d0, out->d1, out->d2, out->d3);
423 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500424
425exit:
Corentin Charya6df4892011-11-26 10:59:58 +0100426 mutex_unlock(&samsung->sabi_mutex);
Corentin Chary7e960712011-11-26 11:00:02 +0100427 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500428}
429
Corentin Chary7e960712011-11-26 11:00:02 +0100430/* simple wrappers usable with most commands */
431static int sabi_set_commandb(struct samsung_laptop *samsung,
432 u16 command, u8 data)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500433{
David Rientjes85229442012-03-20 09:53:05 +0100434 struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500435
Corentin Chary7e960712011-11-26 11:00:02 +0100436 in.data[0] = data;
437 return sabi_command(samsung, command, &in, NULL);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500438}
439
Corentin Chary5dea7a22011-11-26 10:59:59 +0100440static int read_brightness(struct samsung_laptop *samsung)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500441{
Corentin Charya6df4892011-11-26 10:59:58 +0100442 const struct sabi_config *config = samsung->config;
443 const struct sabi_commands *commands = &samsung->config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100444 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500445 int user_brightness = 0;
446 int retval;
447
Corentin Chary7e960712011-11-26 11:00:02 +0100448 retval = sabi_command(samsung, commands->get_brightness,
449 NULL, &sretval);
450 if (retval)
451 return retval;
452
453 user_brightness = sretval.data[0];
454 if (user_brightness > config->min_brightness)
455 user_brightness -= config->min_brightness;
456 else
457 user_brightness = 0;
458
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500459 return user_brightness;
460}
461
Corentin Chary5dea7a22011-11-26 10:59:59 +0100462static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500463{
Corentin Charya6df4892011-11-26 10:59:58 +0100464 const struct sabi_config *config = samsung->config;
465 const struct sabi_commands *commands = &samsung->config->commands;
466 u8 user_level = user_brightness + config->min_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500467
Corentin Charya6df4892011-11-26 10:59:58 +0100468 if (samsung->has_stepping_quirk && user_level != 0) {
Jason Stubbsac080522011-09-20 09:16:13 -0700469 /*
470 * short circuit if the specified level is what's already set
471 * to prevent the screen from flickering needlessly
472 */
Corentin Chary5dea7a22011-11-26 10:59:59 +0100473 if (user_brightness == read_brightness(samsung))
Jason Stubbsac080522011-09-20 09:16:13 -0700474 return;
475
Corentin Chary7e960712011-11-26 11:00:02 +0100476 sabi_set_commandb(samsung, commands->set_brightness, 0);
Jason Stubbsac080522011-09-20 09:16:13 -0700477 }
478
Corentin Chary7e960712011-11-26 11:00:02 +0100479 sabi_set_commandb(samsung, commands->set_brightness, user_level);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500480}
481
482static int get_brightness(struct backlight_device *bd)
483{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100484 struct samsung_laptop *samsung = bl_get_data(bd);
485
486 return read_brightness(samsung);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500487}
488
Corentin Chary5dea7a22011-11-26 10:59:59 +0100489static void check_for_stepping_quirk(struct samsung_laptop *samsung)
Jason Stubbsac080522011-09-20 09:16:13 -0700490{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100491 int initial_level;
492 int check_level;
493 int orig_level = read_brightness(samsung);
Jason Stubbsac080522011-09-20 09:16:13 -0700494
495 /*
496 * Some laptops exhibit the strange behaviour of stepping toward
497 * (rather than setting) the brightness except when changing to/from
498 * brightness level 0. This behaviour is checked for here and worked
499 * around in set_brightness.
500 */
501
John Serockba05b232011-10-13 06:42:01 -0400502 if (orig_level == 0)
Corentin Chary5dea7a22011-11-26 10:59:59 +0100503 set_brightness(samsung, 1);
John Serockba05b232011-10-13 06:42:01 -0400504
Corentin Chary5dea7a22011-11-26 10:59:59 +0100505 initial_level = read_brightness(samsung);
John Serockba05b232011-10-13 06:42:01 -0400506
Jason Stubbsac080522011-09-20 09:16:13 -0700507 if (initial_level <= 2)
508 check_level = initial_level + 2;
509 else
510 check_level = initial_level - 2;
511
Corentin Charya6df4892011-11-26 10:59:58 +0100512 samsung->has_stepping_quirk = false;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100513 set_brightness(samsung, check_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700514
Corentin Chary5dea7a22011-11-26 10:59:59 +0100515 if (read_brightness(samsung) != check_level) {
Corentin Charya6df4892011-11-26 10:59:58 +0100516 samsung->has_stepping_quirk = true;
Jason Stubbsac080522011-09-20 09:16:13 -0700517 pr_info("enabled workaround for brightness stepping quirk\n");
518 }
519
Corentin Chary5dea7a22011-11-26 10:59:59 +0100520 set_brightness(samsung, orig_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700521}
522
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500523static int update_status(struct backlight_device *bd)
524{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100525 struct samsung_laptop *samsung = bl_get_data(bd);
Corentin Charya6df4892011-11-26 10:59:58 +0100526 const struct sabi_commands *commands = &samsung->config->commands;
527
Corentin Chary5dea7a22011-11-26 10:59:59 +0100528 set_brightness(samsung, bd->props.brightness);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500529
530 if (bd->props.power == FB_BLANK_UNBLANK)
Corentin Chary7e960712011-11-26 11:00:02 +0100531 sabi_set_commandb(samsung, commands->set_backlight, 1);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500532 else
Corentin Chary7e960712011-11-26 11:00:02 +0100533 sabi_set_commandb(samsung, commands->set_backlight, 0);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100534
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500535 return 0;
536}
537
538static const struct backlight_ops backlight_ops = {
539 .get_brightness = get_brightness,
540 .update_status = update_status,
541};
542
Corentin Chary84d482f2011-11-26 11:00:09 +0100543static int seclinux_rfkill_set(void *data, bool blocked)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500544{
Corentin Chary20db88e2011-12-15 08:27:39 +0100545 struct samsung_rfkill *srfkill = data;
546 struct samsung_laptop *samsung = srfkill->samsung;
Corentin Charya6df4892011-11-26 10:59:58 +0100547 const struct sabi_commands *commands = &samsung->config->commands;
548
Corentin Chary84d482f2011-11-26 11:00:09 +0100549 return sabi_set_commandb(samsung, commands->set_wireless_button,
550 !blocked);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500551}
552
Corentin Chary84d482f2011-11-26 11:00:09 +0100553static struct rfkill_ops seclinux_rfkill_ops = {
554 .set_block = seclinux_rfkill_set,
555};
556
557static int swsmi_wireless_status(struct samsung_laptop *samsung,
558 struct sabi_data *data)
559{
560 const struct sabi_commands *commands = &samsung->config->commands;
561
562 return sabi_command(samsung, commands->get_wireless_status,
563 NULL, data);
564}
565
566static int swsmi_rfkill_set(void *priv, bool blocked)
567{
568 struct samsung_rfkill *srfkill = priv;
569 struct samsung_laptop *samsung = srfkill->samsung;
570 const struct sabi_commands *commands = &samsung->config->commands;
571 struct sabi_data data;
572 int ret, i;
573
574 ret = swsmi_wireless_status(samsung, &data);
575 if (ret)
576 return ret;
577
578 /* Don't set the state for non-present devices */
579 for (i = 0; i < 4; i++)
580 if (data.data[i] == 0x02)
581 data.data[1] = 0;
582
583 if (srfkill->type == RFKILL_TYPE_WLAN)
584 data.data[WL_STATUS_WLAN] = !blocked;
585 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
586 data.data[WL_STATUS_BT] = !blocked;
587
588 return sabi_command(samsung, commands->set_wireless_status,
589 &data, &data);
590}
591
592static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
593{
594 struct samsung_rfkill *srfkill = priv;
595 struct samsung_laptop *samsung = srfkill->samsung;
596 struct sabi_data data;
597 int ret;
598
599 ret = swsmi_wireless_status(samsung, &data);
600 if (ret)
601 return ;
602
603 if (srfkill->type == RFKILL_TYPE_WLAN)
604 ret = data.data[WL_STATUS_WLAN];
605 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
606 ret = data.data[WL_STATUS_BT];
607 else
608 return ;
609
610 rfkill_set_sw_state(rfkill, !ret);
611}
612
613static struct rfkill_ops swsmi_rfkill_ops = {
614 .set_block = swsmi_rfkill_set,
615 .query = swsmi_rfkill_query,
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500616};
617
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500618static ssize_t get_performance_level(struct device *dev,
619 struct device_attribute *attr, char *buf)
620{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100621 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100622 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100623 const struct sabi_commands *commands = &config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100624 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500625 int retval;
626 int i;
627
628 /* Read the state */
Corentin Chary7e960712011-11-26 11:00:02 +0100629 retval = sabi_command(samsung, commands->get_performance_level,
630 NULL, &sretval);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500631 if (retval)
632 return retval;
633
634 /* The logic is backwards, yeah, lots of fun... */
Corentin Charya6df4892011-11-26 10:59:58 +0100635 for (i = 0; config->performance_levels[i].name; ++i) {
Corentin Chary7e960712011-11-26 11:00:02 +0100636 if (sretval.data[0] == config->performance_levels[i].value)
Corentin Charya6df4892011-11-26 10:59:58 +0100637 return sprintf(buf, "%s\n", config->performance_levels[i].name);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500638 }
639 return sprintf(buf, "%s\n", "unknown");
640}
641
642static ssize_t set_performance_level(struct device *dev,
643 struct device_attribute *attr, const char *buf,
644 size_t count)
645{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100646 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100647 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100648 const struct sabi_commands *commands = &config->commands;
649 int i;
Corentin Charya6df4892011-11-26 10:59:58 +0100650
Corentin Chary5dea7a22011-11-26 10:59:59 +0100651 if (count < 1)
652 return count;
653
654 for (i = 0; config->performance_levels[i].name; ++i) {
655 const struct sabi_performance_level *level =
656 &config->performance_levels[i];
657 if (!strncasecmp(level->name, buf, strlen(level->name))) {
Corentin Chary7e960712011-11-26 11:00:02 +0100658 sabi_set_commandb(samsung,
659 commands->set_performance_level,
660 level->value);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100661 break;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500662 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500663 }
Corentin Chary5dea7a22011-11-26 10:59:59 +0100664
665 if (!config->performance_levels[i].name)
666 return -EINVAL;
667
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500668 return count;
669}
Corentin Chary5dea7a22011-11-26 10:59:59 +0100670
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500671static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
672 get_performance_level, set_performance_level);
673
Corentin Charycb5b5c92011-11-26 11:00:05 +0100674static int read_battery_life_extender(struct samsung_laptop *samsung)
675{
676 const struct sabi_commands *commands = &samsung->config->commands;
677 struct sabi_data data;
678 int retval;
679
680 if (commands->get_battery_life_extender == 0xFFFF)
681 return -ENODEV;
682
683 memset(&data, 0, sizeof(data));
684 data.data[0] = 0x80;
685 retval = sabi_command(samsung, commands->get_battery_life_extender,
686 &data, &data);
687
688 if (retval)
689 return retval;
690
691 if (data.data[0] != 0 && data.data[0] != 1)
692 return -ENODEV;
693
694 return data.data[0];
695}
696
697static int write_battery_life_extender(struct samsung_laptop *samsung,
698 int enabled)
699{
700 const struct sabi_commands *commands = &samsung->config->commands;
701 struct sabi_data data;
702
703 memset(&data, 0, sizeof(data));
704 data.data[0] = 0x80 | enabled;
705 return sabi_command(samsung, commands->set_battery_life_extender,
706 &data, NULL);
707}
708
709static ssize_t get_battery_life_extender(struct device *dev,
710 struct device_attribute *attr,
711 char *buf)
712{
713 struct samsung_laptop *samsung = dev_get_drvdata(dev);
714 int ret;
715
716 ret = read_battery_life_extender(samsung);
717 if (ret < 0)
718 return ret;
719
720 return sprintf(buf, "%d\n", ret);
721}
722
723static ssize_t set_battery_life_extender(struct device *dev,
724 struct device_attribute *attr,
725 const char *buf, size_t count)
726{
727 struct samsung_laptop *samsung = dev_get_drvdata(dev);
728 int ret, value;
729
730 if (!count || sscanf(buf, "%i", &value) != 1)
731 return -EINVAL;
732
733 ret = write_battery_life_extender(samsung, !!value);
734 if (ret < 0)
735 return ret;
736
737 return count;
738}
739
740static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
741 get_battery_life_extender, set_battery_life_extender);
742
Corentin Chary3a75d372011-11-26 11:00:06 +0100743static int read_usb_charge(struct samsung_laptop *samsung)
744{
745 const struct sabi_commands *commands = &samsung->config->commands;
746 struct sabi_data data;
747 int retval;
748
749 if (commands->get_usb_charge == 0xFFFF)
750 return -ENODEV;
751
752 memset(&data, 0, sizeof(data));
753 data.data[0] = 0x80;
754 retval = sabi_command(samsung, commands->get_usb_charge,
755 &data, &data);
756
757 if (retval)
758 return retval;
759
760 if (data.data[0] != 0 && data.data[0] != 1)
761 return -ENODEV;
762
763 return data.data[0];
764}
765
766static int write_usb_charge(struct samsung_laptop *samsung,
767 int enabled)
768{
769 const struct sabi_commands *commands = &samsung->config->commands;
770 struct sabi_data data;
771
772 memset(&data, 0, sizeof(data));
773 data.data[0] = 0x80 | enabled;
774 return sabi_command(samsung, commands->set_usb_charge,
775 &data, NULL);
776}
777
778static ssize_t get_usb_charge(struct device *dev,
779 struct device_attribute *attr,
780 char *buf)
781{
782 struct samsung_laptop *samsung = dev_get_drvdata(dev);
783 int ret;
784
785 ret = read_usb_charge(samsung);
786 if (ret < 0)
787 return ret;
788
789 return sprintf(buf, "%d\n", ret);
790}
791
792static ssize_t set_usb_charge(struct device *dev,
793 struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct samsung_laptop *samsung = dev_get_drvdata(dev);
797 int ret, value;
798
799 if (!count || sscanf(buf, "%i", &value) != 1)
800 return -EINVAL;
801
802 ret = write_usb_charge(samsung, !!value);
803 if (ret < 0)
804 return ret;
805
806 return count;
807}
808
809static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
810 get_usb_charge, set_usb_charge);
811
Corentin Charya66c1662011-11-26 11:00:01 +0100812static struct attribute *platform_attributes[] = {
813 &dev_attr_performance_level.attr,
Corentin Charycb5b5c92011-11-26 11:00:05 +0100814 &dev_attr_battery_life_extender.attr,
Corentin Chary3a75d372011-11-26 11:00:06 +0100815 &dev_attr_usb_charge.attr,
Corentin Charya66c1662011-11-26 11:00:01 +0100816 NULL
817};
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500818
Corentin Chary5dea7a22011-11-26 10:59:59 +0100819static int find_signature(void __iomem *memcheck, const char *testStr)
820{
821 int i = 0;
822 int loca;
823
824 for (loca = 0; loca < 0xffff; loca++) {
825 char temp = readb(memcheck + loca);
826
827 if (temp == testStr[i]) {
828 if (i == strlen(testStr)-1)
829 break;
830 ++i;
831 } else {
832 i = 0;
833 }
834 }
835 return loca;
836}
837
838static void samsung_rfkill_exit(struct samsung_laptop *samsung)
839{
Corentin Chary84d482f2011-11-26 11:00:09 +0100840 if (samsung->wlan.rfkill) {
841 rfkill_unregister(samsung->wlan.rfkill);
842 rfkill_destroy(samsung->wlan.rfkill);
843 samsung->wlan.rfkill = NULL;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100844 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100845 if (samsung->bluetooth.rfkill) {
846 rfkill_unregister(samsung->bluetooth.rfkill);
847 rfkill_destroy(samsung->bluetooth.rfkill);
848 samsung->bluetooth.rfkill = NULL;
849 }
850}
851
852static int samsung_new_rfkill(struct samsung_laptop *samsung,
853 struct samsung_rfkill *arfkill,
854 const char *name, enum rfkill_type type,
855 const struct rfkill_ops *ops,
856 int blocked)
857{
858 struct rfkill **rfkill = &arfkill->rfkill;
859 int ret;
860
861 arfkill->type = type;
862 arfkill->samsung = samsung;
863
864 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
865 type, ops, arfkill);
866
867 if (!*rfkill)
868 return -EINVAL;
869
870 if (blocked != -1)
871 rfkill_init_sw_state(*rfkill, blocked);
872
873 ret = rfkill_register(*rfkill);
874 if (ret) {
875 rfkill_destroy(*rfkill);
876 *rfkill = NULL;
877 return ret;
878 }
879 return 0;
880}
881
882static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
883{
884 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
885 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
886}
887
888static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
889{
890 struct sabi_data data;
891 int ret;
892
893 ret = swsmi_wireless_status(samsung, &data);
Corentin Chary20db88e2011-12-15 08:27:39 +0100894 if (ret) {
895 /* Some swsmi laptops use the old seclinux way to control
896 * wireless devices */
897 if (ret == -EINVAL)
898 ret = samsung_rfkill_init_seclinux(samsung);
Corentin Chary84d482f2011-11-26 11:00:09 +0100899 return ret;
Corentin Chary20db88e2011-12-15 08:27:39 +0100900 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100901
902 /* 0x02 seems to mean that the device is no present/available */
903
904 if (data.data[WL_STATUS_WLAN] != 0x02)
905 ret = samsung_new_rfkill(samsung, &samsung->wlan,
906 "samsung-wlan",
907 RFKILL_TYPE_WLAN,
908 &swsmi_rfkill_ops,
909 !data.data[WL_STATUS_WLAN]);
910 if (ret)
911 goto exit;
912
913 if (data.data[WL_STATUS_BT] != 0x02)
914 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
915 "samsung-bluetooth",
916 RFKILL_TYPE_BLUETOOTH,
917 &swsmi_rfkill_ops,
918 !data.data[WL_STATUS_BT]);
919 if (ret)
920 goto exit;
921
922exit:
923 if (ret)
924 samsung_rfkill_exit(samsung);
925
926 return ret;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100927}
928
929static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
930{
Corentin Chary84d482f2011-11-26 11:00:09 +0100931 if (samsung->config->sabi_version == 2)
932 return samsung_rfkill_init_seclinux(samsung);
933 if (samsung->config->sabi_version == 3)
934 return samsung_rfkill_init_swsmi(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100935 return 0;
936}
937
Corentin Charyf674ebf2011-11-26 11:00:08 +0100938static int kbd_backlight_enable(struct samsung_laptop *samsung)
939{
940 const struct sabi_commands *commands = &samsung->config->commands;
941 struct sabi_data data;
942 int retval;
943
944 if (commands->kbd_backlight == 0xFFFF)
945 return -ENODEV;
946
947 memset(&data, 0, sizeof(data));
948 data.d0 = 0xaabb;
949 retval = sabi_command(samsung, commands->kbd_backlight,
950 &data, &data);
951
952 if (retval)
953 return retval;
954
955 if (data.d0 != 0xccdd)
956 return -ENODEV;
957 return 0;
958}
959
960static int kbd_backlight_read(struct samsung_laptop *samsung)
961{
962 const struct sabi_commands *commands = &samsung->config->commands;
963 struct sabi_data data;
964 int retval;
965
966 memset(&data, 0, sizeof(data));
967 data.data[0] = 0x81;
968 retval = sabi_command(samsung, commands->kbd_backlight,
969 &data, &data);
970
971 if (retval)
972 return retval;
973
974 return data.data[0];
975}
976
977static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
978{
979 const struct sabi_commands *commands = &samsung->config->commands;
980 struct sabi_data data;
981
982 memset(&data, 0, sizeof(data));
983 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
984 return sabi_command(samsung, commands->kbd_backlight,
985 &data, NULL);
986}
987
988static void kbd_led_update(struct work_struct *work)
989{
990 struct samsung_laptop *samsung;
991
992 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
993 kbd_backlight_write(samsung, samsung->kbd_led_wk);
994}
995
996static void kbd_led_set(struct led_classdev *led_cdev,
997 enum led_brightness value)
998{
999 struct samsung_laptop *samsung;
1000
1001 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1002
1003 if (value > samsung->kbd_led.max_brightness)
1004 value = samsung->kbd_led.max_brightness;
1005 else if (value < 0)
1006 value = 0;
1007
1008 samsung->kbd_led_wk = value;
1009 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1010}
1011
1012static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1013{
1014 struct samsung_laptop *samsung;
1015
1016 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1017 return kbd_backlight_read(samsung);
1018}
1019
1020static void samsung_leds_exit(struct samsung_laptop *samsung)
1021{
1022 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1023 led_classdev_unregister(&samsung->kbd_led);
1024 if (samsung->led_workqueue)
1025 destroy_workqueue(samsung->led_workqueue);
1026}
1027
1028static int __init samsung_leds_init(struct samsung_laptop *samsung)
1029{
1030 int ret = 0;
1031
1032 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1033 if (!samsung->led_workqueue)
1034 return -ENOMEM;
1035
1036 if (kbd_backlight_enable(samsung) >= 0) {
1037 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1038
1039 samsung->kbd_led.name = "samsung::kbd_backlight";
1040 samsung->kbd_led.brightness_set = kbd_led_set;
1041 samsung->kbd_led.brightness_get = kbd_led_get;
1042 samsung->kbd_led.max_brightness = 8;
1043
1044 ret = led_classdev_register(&samsung->platform_device->dev,
1045 &samsung->kbd_led);
1046 }
1047
1048 if (ret)
1049 samsung_leds_exit(samsung);
1050
1051 return ret;
1052}
1053
Corentin Chary5dea7a22011-11-26 10:59:59 +01001054static void samsung_backlight_exit(struct samsung_laptop *samsung)
1055{
1056 if (samsung->backlight_device) {
1057 backlight_device_unregister(samsung->backlight_device);
1058 samsung->backlight_device = NULL;
1059 }
1060}
1061
1062static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1063{
1064 struct backlight_device *bd;
1065 struct backlight_properties props;
1066
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001067 if (!samsung->handle_backlight)
1068 return 0;
1069
Corentin Chary5dea7a22011-11-26 10:59:59 +01001070 memset(&props, 0, sizeof(struct backlight_properties));
1071 props.type = BACKLIGHT_PLATFORM;
1072 props.max_brightness = samsung->config->max_brightness -
1073 samsung->config->min_brightness;
1074
1075 bd = backlight_device_register("samsung",
1076 &samsung->platform_device->dev,
1077 samsung, &backlight_ops,
1078 &props);
1079 if (IS_ERR(bd))
1080 return PTR_ERR(bd);
1081
1082 samsung->backlight_device = bd;
1083 samsung->backlight_device->props.brightness = read_brightness(samsung);
1084 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1085 backlight_update_status(samsung->backlight_device);
1086
1087 return 0;
1088}
1089
Dan Carpenterbde9e502012-03-20 09:53:07 +01001090static umode_t samsung_sysfs_is_visible(struct kobject *kobj,
Corentin Charya66c1662011-11-26 11:00:01 +01001091 struct attribute *attr, int idx)
1092{
1093 struct device *dev = container_of(kobj, struct device, kobj);
1094 struct platform_device *pdev = to_platform_device(dev);
1095 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1096 bool ok = true;
1097
1098 if (attr == &dev_attr_performance_level.attr)
1099 ok = !!samsung->config->performance_levels[0].name;
Corentin Charycb5b5c92011-11-26 11:00:05 +01001100 if (attr == &dev_attr_battery_life_extender.attr)
1101 ok = !!(read_battery_life_extender(samsung) >= 0);
Corentin Chary3a75d372011-11-26 11:00:06 +01001102 if (attr == &dev_attr_usb_charge.attr)
1103 ok = !!(read_usb_charge(samsung) >= 0);
Corentin Charya66c1662011-11-26 11:00:01 +01001104
1105 return ok ? attr->mode : 0;
1106}
1107
1108static struct attribute_group platform_attribute_group = {
1109 .is_visible = samsung_sysfs_is_visible,
1110 .attrs = platform_attributes
1111};
1112
Corentin Chary5dea7a22011-11-26 10:59:59 +01001113static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1114{
Corentin Charya66c1662011-11-26 11:00:01 +01001115 struct platform_device *device = samsung->platform_device;
1116
1117 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001118}
1119
1120static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1121{
Corentin Charya66c1662011-11-26 11:00:01 +01001122 struct platform_device *device = samsung->platform_device;
1123
1124 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1125
Corentin Chary5dea7a22011-11-26 10:59:59 +01001126}
1127
Corentin Chary5b80fc42011-11-26 11:00:03 +01001128static int show_call(struct seq_file *m, void *data)
1129{
1130 struct samsung_laptop *samsung = m->private;
1131 struct sabi_data *sdata = &samsung->debug.data;
1132 int ret;
1133
1134 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1135 samsung->debug.command,
1136 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1137
1138 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1139
1140 if (ret) {
1141 seq_printf(m, "SABI command 0x%04x failed\n",
1142 samsung->debug.command);
1143 return ret;
1144 }
1145
1146 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1147 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1148 return 0;
1149}
1150
1151static int samsung_debugfs_open(struct inode *inode, struct file *file)
1152{
1153 return single_open(file, show_call, inode->i_private);
1154}
1155
1156static const struct file_operations samsung_laptop_call_io_ops = {
1157 .owner = THIS_MODULE,
1158 .open = samsung_debugfs_open,
1159 .read = seq_read,
1160 .llseek = seq_lseek,
1161 .release = single_release,
1162};
1163
1164static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1165{
1166 debugfs_remove_recursive(samsung->debug.root);
1167}
1168
1169static int samsung_debugfs_init(struct samsung_laptop *samsung)
1170{
1171 struct dentry *dent;
1172
1173 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1174 if (!samsung->debug.root) {
1175 pr_err("failed to create debugfs directory");
1176 goto error_debugfs;
1177 }
1178
1179 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1180 samsung->debug.f0000_wrapper.size = 0xffff;
1181
1182 samsung->debug.data_wrapper.data = &samsung->debug.data;
1183 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1184
Corentin Chary6f6ae062011-11-26 11:00:11 +01001185 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1186 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1187
Corentin Chary5b80fc42011-11-26 11:00:03 +01001188 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1189 samsung->debug.root, &samsung->debug.command);
1190 if (!dent)
1191 goto error_debugfs;
1192
1193 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1194 &samsung->debug.data.d0);
1195 if (!dent)
1196 goto error_debugfs;
1197
1198 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1199 &samsung->debug.data.d1);
1200 if (!dent)
1201 goto error_debugfs;
1202
1203 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1204 &samsung->debug.data.d2);
1205 if (!dent)
1206 goto error_debugfs;
1207
1208 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1209 &samsung->debug.data.d3);
1210 if (!dent)
1211 goto error_debugfs;
1212
1213 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1214 samsung->debug.root,
1215 &samsung->debug.data_wrapper);
1216 if (!dent)
1217 goto error_debugfs;
1218
1219 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1220 samsung->debug.root,
1221 &samsung->debug.f0000_wrapper);
1222 if (!dent)
1223 goto error_debugfs;
1224
1225 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1226 samsung->debug.root, samsung,
1227 &samsung_laptop_call_io_ops);
1228 if (!dent)
1229 goto error_debugfs;
1230
Corentin Chary6f6ae062011-11-26 11:00:11 +01001231 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1232 samsung->debug.root,
1233 &samsung->debug.sdiag_wrapper);
1234 if (!dent)
1235 goto error_debugfs;
1236
Corentin Chary5b80fc42011-11-26 11:00:03 +01001237 return 0;
1238
1239error_debugfs:
1240 samsung_debugfs_exit(samsung);
1241 return -ENOMEM;
1242}
1243
Corentin Chary5dea7a22011-11-26 10:59:59 +01001244static void samsung_sabi_exit(struct samsung_laptop *samsung)
1245{
1246 const struct sabi_config *config = samsung->config;
1247
1248 /* Turn off "Linux" mode in the BIOS */
1249 if (config && config->commands.set_linux != 0xff)
Corentin Chary7e960712011-11-26 11:00:02 +01001250 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001251
1252 if (samsung->sabi_iface) {
1253 iounmap(samsung->sabi_iface);
1254 samsung->sabi_iface = NULL;
1255 }
1256 if (samsung->f0000_segment) {
1257 iounmap(samsung->f0000_segment);
1258 samsung->f0000_segment = NULL;
1259 }
1260
1261 samsung->config = NULL;
1262}
1263
Corentin Chary49dd7732011-11-26 11:00:04 +01001264static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1265 unsigned int ifaceP)
Corentin Chary5dea7a22011-11-26 10:59:59 +01001266{
1267 const struct sabi_config *config = samsung->config;
1268
1269 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1270 loca + 0xf0000 - 6);
Corentin Chary49dd7732011-11-26 11:00:04 +01001271
Corentin Chary5dea7a22011-11-26 10:59:59 +01001272 printk(KERN_DEBUG "SABI header:\n");
1273 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1274 readw(samsung->sabi + config->header_offsets.port));
1275 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1276 readb(samsung->sabi + config->header_offsets.iface_func));
1277 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1278 readb(samsung->sabi + config->header_offsets.en_mem));
1279 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1280 readb(samsung->sabi + config->header_offsets.re_mem));
1281 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1282 readw(samsung->sabi + config->header_offsets.data_offset));
1283 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1284 readw(samsung->sabi + config->header_offsets.data_segment));
Corentin Chary5dea7a22011-11-26 10:59:59 +01001285
Corentin Chary49dd7732011-11-26 11:00:04 +01001286 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001287}
1288
Corentin Chary6f6ae062011-11-26 11:00:11 +01001289static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1290{
1291 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1292 int i;
1293
1294 if (loca == 0xffff)
1295 return ;
1296
1297 /* Example:
1298 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1299 *
1300 * Product name: 90X3A
1301 * BIOS Version: 07HL
1302 */
1303 loca += 1;
1304 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1305 char temp = readb(samsung->f0000_segment + loca);
1306
1307 if (isalnum(temp) || temp == '/' || temp == '-')
1308 samsung->sdiag[i++] = temp;
1309 else
1310 break ;
1311 }
1312
1313 if (debug && samsung->sdiag[0])
1314 pr_info("sdiag: %s", samsung->sdiag);
1315}
1316
Corentin Chary5dea7a22011-11-26 10:59:59 +01001317static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1318{
1319 const struct sabi_config *config = NULL;
1320 const struct sabi_commands *commands;
1321 unsigned int ifaceP;
1322 int ret = 0;
1323 int i;
1324 int loca;
1325
1326 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1327 if (!samsung->f0000_segment) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001328 if (debug || force)
1329 pr_err("Can't map the segment at 0xf0000\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001330 ret = -EINVAL;
1331 goto exit;
1332 }
1333
Corentin Chary6f6ae062011-11-26 11:00:11 +01001334 samsung_sabi_diag(samsung);
1335
Corentin Chary5dea7a22011-11-26 10:59:59 +01001336 /* Try to find one of the signatures in memory to find the header */
1337 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1338 samsung->config = &sabi_configs[i];
1339 loca = find_signature(samsung->f0000_segment,
1340 samsung->config->test_string);
1341 if (loca != 0xffff)
1342 break;
1343 }
1344
1345 if (loca == 0xffff) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001346 if (debug || force)
1347 pr_err("This computer does not support SABI\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001348 ret = -ENODEV;
1349 goto exit;
1350 }
1351
1352 config = samsung->config;
1353 commands = &config->commands;
1354
1355 /* point to the SMI port Number */
1356 loca += 1;
1357 samsung->sabi = (samsung->f0000_segment + loca);
1358
Corentin Chary5dea7a22011-11-26 10:59:59 +01001359 /* Get a pointer to the SABI Interface */
1360 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1361 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
Corentin Chary49dd7732011-11-26 11:00:04 +01001362
1363 if (debug)
1364 samsung_sabi_infos(samsung, loca, ifaceP);
1365
Corentin Chary5dea7a22011-11-26 10:59:59 +01001366 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1367 if (!samsung->sabi_iface) {
1368 pr_err("Can't remap %x\n", ifaceP);
1369 ret = -EINVAL;
1370 goto exit;
1371 }
1372
Corentin Chary5dea7a22011-11-26 10:59:59 +01001373 /* Turn on "Linux" mode in the BIOS */
1374 if (commands->set_linux != 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +01001375 int retval = sabi_set_commandb(samsung,
1376 commands->set_linux, 0x81);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001377 if (retval) {
1378 pr_warn("Linux mode was not set!\n");
1379 ret = -ENODEV;
1380 goto exit;
1381 }
1382 }
1383
1384 /* Check for stepping quirk */
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001385 if (samsung->handle_backlight)
1386 check_for_stepping_quirk(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001387
Corentin Chary2e777182011-11-26 11:00:12 +01001388 pr_info("detected SABI interface: %s\n",
1389 samsung->config->test_string);
1390
Corentin Chary5dea7a22011-11-26 10:59:59 +01001391exit:
1392 if (ret)
1393 samsung_sabi_exit(samsung);
1394
1395 return ret;
1396}
1397
1398static void samsung_platform_exit(struct samsung_laptop *samsung)
1399{
1400 if (samsung->platform_device) {
1401 platform_device_unregister(samsung->platform_device);
1402 samsung->platform_device = NULL;
1403 }
1404}
1405
1406static int __init samsung_platform_init(struct samsung_laptop *samsung)
1407{
1408 struct platform_device *pdev;
1409
1410 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1411 if (IS_ERR(pdev))
1412 return PTR_ERR(pdev);
1413
1414 samsung->platform_device = pdev;
1415 platform_set_drvdata(samsung->platform_device, samsung);
1416 return 0;
1417}
1418
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001419static struct dmi_system_id __initdata samsung_dmi_table[] = {
1420 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001421 .matches = {
1422 DMI_MATCH(DMI_SYS_VENDOR,
1423 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001424 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001425 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001426 },
1427 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001428 .matches = {
1429 DMI_MATCH(DMI_SYS_VENDOR,
1430 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001431 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001432 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001433 },
1434 {
J Witteveen4e2441c2011-07-03 13:15:44 +02001435 .matches = {
1436 DMI_MATCH(DMI_SYS_VENDOR,
1437 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001438 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
J Witteveen4e2441c2011-07-03 13:15:44 +02001439 },
J Witteveen4e2441c2011-07-03 13:15:44 +02001440 },
1441 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001442 .matches = {
1443 DMI_MATCH(DMI_SYS_VENDOR,
1444 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001445 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001446 },
Tommaso Massimi7500eeb2011-09-20 09:16:09 -07001447 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001448 { },
1449};
1450MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1451
Corentin Chary5dea7a22011-11-26 10:59:59 +01001452static struct platform_device *samsung_platform_device;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001453
1454static int __init samsung_init(void)
1455{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001456 struct samsung_laptop *samsung;
1457 int ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001458
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001459 if (!force && !dmi_check_system(samsung_dmi_table))
1460 return -ENODEV;
1461
Corentin Charya6df4892011-11-26 10:59:58 +01001462 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1463 if (!samsung)
1464 return -ENOMEM;
1465
1466 mutex_init(&samsung->sabi_mutex);
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001467 samsung->handle_backlight = true;
1468
1469#ifdef CONFIG_ACPI
1470 /* Don't handle backlight here if the acpi video already handle it */
Corentin Chary3be324a2011-11-26 11:00:10 +01001471 if (acpi_video_backlight_support())
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001472 samsung->handle_backlight = false;
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001473#endif
Corentin Chary5dea7a22011-11-26 10:59:59 +01001474 ret = samsung_platform_init(samsung);
1475 if (ret)
1476 goto error_platform;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001477
Corentin Chary5dea7a22011-11-26 10:59:59 +01001478 ret = samsung_sabi_init(samsung);
1479 if (ret)
1480 goto error_sabi;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001481
Corentin Chary3be324a2011-11-26 11:00:10 +01001482#ifdef CONFIG_ACPI
1483 /* Only log that if we are really on a sabi platform */
1484 if (acpi_video_backlight_support())
1485 pr_info("Backlight controlled by ACPI video driver\n");
1486#endif
1487
Corentin Chary5dea7a22011-11-26 10:59:59 +01001488 ret = samsung_sysfs_init(samsung);
1489 if (ret)
1490 goto error_sysfs;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001491
Corentin Chary5dea7a22011-11-26 10:59:59 +01001492 ret = samsung_backlight_init(samsung);
1493 if (ret)
1494 goto error_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +01001495
Corentin Chary5dea7a22011-11-26 10:59:59 +01001496 ret = samsung_rfkill_init(samsung);
1497 if (ret)
1498 goto error_rfkill;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001499
Corentin Charyf674ebf2011-11-26 11:00:08 +01001500 ret = samsung_leds_init(samsung);
1501 if (ret)
1502 goto error_leds;
1503
Corentin Chary5b80fc42011-11-26 11:00:03 +01001504 ret = samsung_debugfs_init(samsung);
1505 if (ret)
1506 goto error_debugfs;
1507
Corentin Chary5dea7a22011-11-26 10:59:59 +01001508 samsung_platform_device = samsung->platform_device;
1509 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001510
Corentin Chary5b80fc42011-11-26 11:00:03 +01001511error_debugfs:
Corentin Charyf674ebf2011-11-26 11:00:08 +01001512 samsung_leds_exit(samsung);
1513error_leds:
Corentin Chary5b80fc42011-11-26 11:00:03 +01001514 samsung_rfkill_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001515error_rfkill:
1516 samsung_backlight_exit(samsung);
1517error_backlight:
1518 samsung_sysfs_exit(samsung);
1519error_sysfs:
1520 samsung_sabi_exit(samsung);
1521error_sabi:
1522 samsung_platform_exit(samsung);
1523error_platform:
Corentin Charya6df4892011-11-26 10:59:58 +01001524 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001525 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001526}
1527
1528static void __exit samsung_exit(void)
1529{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001530 struct samsung_laptop *samsung;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001531
Corentin Chary5dea7a22011-11-26 10:59:59 +01001532 samsung = platform_get_drvdata(samsung_platform_device);
Corentin Charya6df4892011-11-26 10:59:58 +01001533
Corentin Chary5b80fc42011-11-26 11:00:03 +01001534 samsung_debugfs_exit(samsung);
Corentin Charyf674ebf2011-11-26 11:00:08 +01001535 samsung_leds_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001536 samsung_rfkill_exit(samsung);
1537 samsung_backlight_exit(samsung);
1538 samsung_sysfs_exit(samsung);
1539 samsung_sabi_exit(samsung);
1540 samsung_platform_exit(samsung);
1541
Corentin Charya6df4892011-11-26 10:59:58 +01001542 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001543 samsung_platform_device = NULL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001544}
1545
1546module_init(samsung_init);
1547module_exit(samsung_exit);
1548
1549MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1550MODULE_DESCRIPTION("Samsung Backlight driver");
1551MODULE_LICENSE("GPL");