blob: b7c67c87f47b96006428014e560bceca7be97925 [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)
Corentin Chary2e777182011-11-26 11:00:12 +0100369 pr_info("SABI command:0x%04x "
370 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100371 command, in->d0, in->d1, in->d2, in->d3);
372 else
Corentin Chary2e777182011-11-26 11:00:12 +0100373 pr_info("SABI command:0x%04x", command);
Corentin Chary7e960712011-11-26 11:00:02 +0100374 }
375
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500376 /* enable memory to be able to write to it */
Corentin Charya6df4892011-11-26 10:59:58 +0100377 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500378
379 /* write out the command */
Corentin Charya6df4892011-11-26 10:59:58 +0100380 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
381 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
382 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
Corentin Chary7e960712011-11-26 11:00:02 +0100383 if (in) {
384 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
385 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
386 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
387 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
388 }
Corentin Charya6df4892011-11-26 10:59:58 +0100389 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500390
391 /* write protect memory to make it safe */
Corentin Charya6df4892011-11-26 10:59:58 +0100392 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500393
394 /* see if the command actually succeeded */
Corentin Charya6df4892011-11-26 10:59:58 +0100395 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
396 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
Corentin Chary2e777182011-11-26 11:00:12 +0100397
398 /* iface_data = 0xFF happens when a command is not known
399 * so we only add a warning in debug mode since we will
400 * probably issue some unknown command at startup to find
401 * out which features are supported */
402 if (complete != 0xaa || (iface_data == 0xff && debug))
Corentin Chary7e960712011-11-26 11:00:02 +0100403 pr_warn("SABI command 0x%04x failed with"
404 " completion flag 0x%02x and interface data 0x%02x",
405 command, complete, iface_data);
Corentin Chary2e777182011-11-26 11:00:12 +0100406
407 if (complete != 0xaa || iface_data == 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +0100408 ret = -EINVAL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500409 goto exit;
410 }
Corentin Chary7e960712011-11-26 11:00:02 +0100411
412 if (out) {
413 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
414 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
415 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
416 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
417 }
418
419 if (debug && out) {
Corentin Chary2e777182011-11-26 11:00:12 +0100420 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100421 out->d0, out->d1, out->d2, out->d3);
422 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500423
424exit:
Corentin Charya6df4892011-11-26 10:59:58 +0100425 mutex_unlock(&samsung->sabi_mutex);
Corentin Chary7e960712011-11-26 11:00:02 +0100426 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500427}
428
Corentin Chary7e960712011-11-26 11:00:02 +0100429/* simple wrappers usable with most commands */
430static int sabi_set_commandb(struct samsung_laptop *samsung,
431 u16 command, u8 data)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500432{
David Rientjes85229442012-03-20 09:53:05 +0100433 struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500434
Corentin Chary7e960712011-11-26 11:00:02 +0100435 in.data[0] = data;
436 return sabi_command(samsung, command, &in, NULL);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500437}
438
Corentin Chary5dea7a22011-11-26 10:59:59 +0100439static int read_brightness(struct samsung_laptop *samsung)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500440{
Corentin Charya6df4892011-11-26 10:59:58 +0100441 const struct sabi_config *config = samsung->config;
442 const struct sabi_commands *commands = &samsung->config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100443 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500444 int user_brightness = 0;
445 int retval;
446
Corentin Chary7e960712011-11-26 11:00:02 +0100447 retval = sabi_command(samsung, commands->get_brightness,
448 NULL, &sretval);
449 if (retval)
450 return retval;
451
452 user_brightness = sretval.data[0];
453 if (user_brightness > config->min_brightness)
454 user_brightness -= config->min_brightness;
455 else
456 user_brightness = 0;
457
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500458 return user_brightness;
459}
460
Corentin Chary5dea7a22011-11-26 10:59:59 +0100461static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500462{
Corentin Charya6df4892011-11-26 10:59:58 +0100463 const struct sabi_config *config = samsung->config;
464 const struct sabi_commands *commands = &samsung->config->commands;
465 u8 user_level = user_brightness + config->min_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500466
Corentin Charya6df4892011-11-26 10:59:58 +0100467 if (samsung->has_stepping_quirk && user_level != 0) {
Jason Stubbsac080522011-09-20 09:16:13 -0700468 /*
469 * short circuit if the specified level is what's already set
470 * to prevent the screen from flickering needlessly
471 */
Corentin Chary5dea7a22011-11-26 10:59:59 +0100472 if (user_brightness == read_brightness(samsung))
Jason Stubbsac080522011-09-20 09:16:13 -0700473 return;
474
Corentin Chary7e960712011-11-26 11:00:02 +0100475 sabi_set_commandb(samsung, commands->set_brightness, 0);
Jason Stubbsac080522011-09-20 09:16:13 -0700476 }
477
Corentin Chary7e960712011-11-26 11:00:02 +0100478 sabi_set_commandb(samsung, commands->set_brightness, user_level);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500479}
480
481static int get_brightness(struct backlight_device *bd)
482{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100483 struct samsung_laptop *samsung = bl_get_data(bd);
484
485 return read_brightness(samsung);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500486}
487
Corentin Chary5dea7a22011-11-26 10:59:59 +0100488static void check_for_stepping_quirk(struct samsung_laptop *samsung)
Jason Stubbsac080522011-09-20 09:16:13 -0700489{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100490 int initial_level;
491 int check_level;
492 int orig_level = read_brightness(samsung);
Jason Stubbsac080522011-09-20 09:16:13 -0700493
494 /*
495 * Some laptops exhibit the strange behaviour of stepping toward
496 * (rather than setting) the brightness except when changing to/from
497 * brightness level 0. This behaviour is checked for here and worked
498 * around in set_brightness.
499 */
500
John Serockba05b232011-10-13 06:42:01 -0400501 if (orig_level == 0)
Corentin Chary5dea7a22011-11-26 10:59:59 +0100502 set_brightness(samsung, 1);
John Serockba05b232011-10-13 06:42:01 -0400503
Corentin Chary5dea7a22011-11-26 10:59:59 +0100504 initial_level = read_brightness(samsung);
John Serockba05b232011-10-13 06:42:01 -0400505
Jason Stubbsac080522011-09-20 09:16:13 -0700506 if (initial_level <= 2)
507 check_level = initial_level + 2;
508 else
509 check_level = initial_level - 2;
510
Corentin Charya6df4892011-11-26 10:59:58 +0100511 samsung->has_stepping_quirk = false;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100512 set_brightness(samsung, check_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700513
Corentin Chary5dea7a22011-11-26 10:59:59 +0100514 if (read_brightness(samsung) != check_level) {
Corentin Charya6df4892011-11-26 10:59:58 +0100515 samsung->has_stepping_quirk = true;
Jason Stubbsac080522011-09-20 09:16:13 -0700516 pr_info("enabled workaround for brightness stepping quirk\n");
517 }
518
Corentin Chary5dea7a22011-11-26 10:59:59 +0100519 set_brightness(samsung, orig_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700520}
521
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500522static int update_status(struct backlight_device *bd)
523{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100524 struct samsung_laptop *samsung = bl_get_data(bd);
Corentin Charya6df4892011-11-26 10:59:58 +0100525 const struct sabi_commands *commands = &samsung->config->commands;
526
Corentin Chary5dea7a22011-11-26 10:59:59 +0100527 set_brightness(samsung, bd->props.brightness);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500528
529 if (bd->props.power == FB_BLANK_UNBLANK)
Corentin Chary7e960712011-11-26 11:00:02 +0100530 sabi_set_commandb(samsung, commands->set_backlight, 1);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500531 else
Corentin Chary7e960712011-11-26 11:00:02 +0100532 sabi_set_commandb(samsung, commands->set_backlight, 0);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100533
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500534 return 0;
535}
536
537static const struct backlight_ops backlight_ops = {
538 .get_brightness = get_brightness,
539 .update_status = update_status,
540};
541
Corentin Chary84d482f2011-11-26 11:00:09 +0100542static int seclinux_rfkill_set(void *data, bool blocked)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500543{
Corentin Chary20db88e2011-12-15 08:27:39 +0100544 struct samsung_rfkill *srfkill = data;
545 struct samsung_laptop *samsung = srfkill->samsung;
Corentin Charya6df4892011-11-26 10:59:58 +0100546 const struct sabi_commands *commands = &samsung->config->commands;
547
Corentin Chary84d482f2011-11-26 11:00:09 +0100548 return sabi_set_commandb(samsung, commands->set_wireless_button,
549 !blocked);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500550}
551
Corentin Chary84d482f2011-11-26 11:00:09 +0100552static struct rfkill_ops seclinux_rfkill_ops = {
553 .set_block = seclinux_rfkill_set,
554};
555
556static int swsmi_wireless_status(struct samsung_laptop *samsung,
557 struct sabi_data *data)
558{
559 const struct sabi_commands *commands = &samsung->config->commands;
560
561 return sabi_command(samsung, commands->get_wireless_status,
562 NULL, data);
563}
564
565static int swsmi_rfkill_set(void *priv, bool blocked)
566{
567 struct samsung_rfkill *srfkill = priv;
568 struct samsung_laptop *samsung = srfkill->samsung;
569 const struct sabi_commands *commands = &samsung->config->commands;
570 struct sabi_data data;
571 int ret, i;
572
573 ret = swsmi_wireless_status(samsung, &data);
574 if (ret)
575 return ret;
576
577 /* Don't set the state for non-present devices */
578 for (i = 0; i < 4; i++)
579 if (data.data[i] == 0x02)
580 data.data[1] = 0;
581
582 if (srfkill->type == RFKILL_TYPE_WLAN)
583 data.data[WL_STATUS_WLAN] = !blocked;
584 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
585 data.data[WL_STATUS_BT] = !blocked;
586
587 return sabi_command(samsung, commands->set_wireless_status,
588 &data, &data);
589}
590
591static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
592{
593 struct samsung_rfkill *srfkill = priv;
594 struct samsung_laptop *samsung = srfkill->samsung;
595 struct sabi_data data;
596 int ret;
597
598 ret = swsmi_wireless_status(samsung, &data);
599 if (ret)
600 return ;
601
602 if (srfkill->type == RFKILL_TYPE_WLAN)
603 ret = data.data[WL_STATUS_WLAN];
604 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
605 ret = data.data[WL_STATUS_BT];
606 else
607 return ;
608
609 rfkill_set_sw_state(rfkill, !ret);
610}
611
612static struct rfkill_ops swsmi_rfkill_ops = {
613 .set_block = swsmi_rfkill_set,
614 .query = swsmi_rfkill_query,
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500615};
616
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500617static ssize_t get_performance_level(struct device *dev,
618 struct device_attribute *attr, char *buf)
619{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100620 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100621 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100622 const struct sabi_commands *commands = &config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100623 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500624 int retval;
625 int i;
626
627 /* Read the state */
Corentin Chary7e960712011-11-26 11:00:02 +0100628 retval = sabi_command(samsung, commands->get_performance_level,
629 NULL, &sretval);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500630 if (retval)
631 return retval;
632
633 /* The logic is backwards, yeah, lots of fun... */
Corentin Charya6df4892011-11-26 10:59:58 +0100634 for (i = 0; config->performance_levels[i].name; ++i) {
Corentin Chary7e960712011-11-26 11:00:02 +0100635 if (sretval.data[0] == config->performance_levels[i].value)
Corentin Charya6df4892011-11-26 10:59:58 +0100636 return sprintf(buf, "%s\n", config->performance_levels[i].name);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500637 }
638 return sprintf(buf, "%s\n", "unknown");
639}
640
641static ssize_t set_performance_level(struct device *dev,
642 struct device_attribute *attr, const char *buf,
643 size_t count)
644{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100645 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100646 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100647 const struct sabi_commands *commands = &config->commands;
648 int i;
Corentin Charya6df4892011-11-26 10:59:58 +0100649
Corentin Chary5dea7a22011-11-26 10:59:59 +0100650 if (count < 1)
651 return count;
652
653 for (i = 0; config->performance_levels[i].name; ++i) {
654 const struct sabi_performance_level *level =
655 &config->performance_levels[i];
656 if (!strncasecmp(level->name, buf, strlen(level->name))) {
Corentin Chary7e960712011-11-26 11:00:02 +0100657 sabi_set_commandb(samsung,
658 commands->set_performance_level,
659 level->value);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100660 break;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500661 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500662 }
Corentin Chary5dea7a22011-11-26 10:59:59 +0100663
664 if (!config->performance_levels[i].name)
665 return -EINVAL;
666
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500667 return count;
668}
Corentin Chary5dea7a22011-11-26 10:59:59 +0100669
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500670static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
671 get_performance_level, set_performance_level);
672
Corentin Charycb5b5c92011-11-26 11:00:05 +0100673static int read_battery_life_extender(struct samsung_laptop *samsung)
674{
675 const struct sabi_commands *commands = &samsung->config->commands;
676 struct sabi_data data;
677 int retval;
678
679 if (commands->get_battery_life_extender == 0xFFFF)
680 return -ENODEV;
681
682 memset(&data, 0, sizeof(data));
683 data.data[0] = 0x80;
684 retval = sabi_command(samsung, commands->get_battery_life_extender,
685 &data, &data);
686
687 if (retval)
688 return retval;
689
690 if (data.data[0] != 0 && data.data[0] != 1)
691 return -ENODEV;
692
693 return data.data[0];
694}
695
696static int write_battery_life_extender(struct samsung_laptop *samsung,
697 int enabled)
698{
699 const struct sabi_commands *commands = &samsung->config->commands;
700 struct sabi_data data;
701
702 memset(&data, 0, sizeof(data));
703 data.data[0] = 0x80 | enabled;
704 return sabi_command(samsung, commands->set_battery_life_extender,
705 &data, NULL);
706}
707
708static ssize_t get_battery_life_extender(struct device *dev,
709 struct device_attribute *attr,
710 char *buf)
711{
712 struct samsung_laptop *samsung = dev_get_drvdata(dev);
713 int ret;
714
715 ret = read_battery_life_extender(samsung);
716 if (ret < 0)
717 return ret;
718
719 return sprintf(buf, "%d\n", ret);
720}
721
722static ssize_t set_battery_life_extender(struct device *dev,
723 struct device_attribute *attr,
724 const char *buf, size_t count)
725{
726 struct samsung_laptop *samsung = dev_get_drvdata(dev);
727 int ret, value;
728
729 if (!count || sscanf(buf, "%i", &value) != 1)
730 return -EINVAL;
731
732 ret = write_battery_life_extender(samsung, !!value);
733 if (ret < 0)
734 return ret;
735
736 return count;
737}
738
739static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
740 get_battery_life_extender, set_battery_life_extender);
741
Corentin Chary3a75d372011-11-26 11:00:06 +0100742static int read_usb_charge(struct samsung_laptop *samsung)
743{
744 const struct sabi_commands *commands = &samsung->config->commands;
745 struct sabi_data data;
746 int retval;
747
748 if (commands->get_usb_charge == 0xFFFF)
749 return -ENODEV;
750
751 memset(&data, 0, sizeof(data));
752 data.data[0] = 0x80;
753 retval = sabi_command(samsung, commands->get_usb_charge,
754 &data, &data);
755
756 if (retval)
757 return retval;
758
759 if (data.data[0] != 0 && data.data[0] != 1)
760 return -ENODEV;
761
762 return data.data[0];
763}
764
765static int write_usb_charge(struct samsung_laptop *samsung,
766 int enabled)
767{
768 const struct sabi_commands *commands = &samsung->config->commands;
769 struct sabi_data data;
770
771 memset(&data, 0, sizeof(data));
772 data.data[0] = 0x80 | enabled;
773 return sabi_command(samsung, commands->set_usb_charge,
774 &data, NULL);
775}
776
777static ssize_t get_usb_charge(struct device *dev,
778 struct device_attribute *attr,
779 char *buf)
780{
781 struct samsung_laptop *samsung = dev_get_drvdata(dev);
782 int ret;
783
784 ret = read_usb_charge(samsung);
785 if (ret < 0)
786 return ret;
787
788 return sprintf(buf, "%d\n", ret);
789}
790
791static ssize_t set_usb_charge(struct device *dev,
792 struct device_attribute *attr,
793 const char *buf, size_t count)
794{
795 struct samsung_laptop *samsung = dev_get_drvdata(dev);
796 int ret, value;
797
798 if (!count || sscanf(buf, "%i", &value) != 1)
799 return -EINVAL;
800
801 ret = write_usb_charge(samsung, !!value);
802 if (ret < 0)
803 return ret;
804
805 return count;
806}
807
808static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
809 get_usb_charge, set_usb_charge);
810
Corentin Charya66c1662011-11-26 11:00:01 +0100811static struct attribute *platform_attributes[] = {
812 &dev_attr_performance_level.attr,
Corentin Charycb5b5c92011-11-26 11:00:05 +0100813 &dev_attr_battery_life_extender.attr,
Corentin Chary3a75d372011-11-26 11:00:06 +0100814 &dev_attr_usb_charge.attr,
Corentin Charya66c1662011-11-26 11:00:01 +0100815 NULL
816};
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500817
Corentin Chary5dea7a22011-11-26 10:59:59 +0100818static int find_signature(void __iomem *memcheck, const char *testStr)
819{
820 int i = 0;
821 int loca;
822
823 for (loca = 0; loca < 0xffff; loca++) {
824 char temp = readb(memcheck + loca);
825
826 if (temp == testStr[i]) {
827 if (i == strlen(testStr)-1)
828 break;
829 ++i;
830 } else {
831 i = 0;
832 }
833 }
834 return loca;
835}
836
837static void samsung_rfkill_exit(struct samsung_laptop *samsung)
838{
Corentin Chary84d482f2011-11-26 11:00:09 +0100839 if (samsung->wlan.rfkill) {
840 rfkill_unregister(samsung->wlan.rfkill);
841 rfkill_destroy(samsung->wlan.rfkill);
842 samsung->wlan.rfkill = NULL;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100843 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100844 if (samsung->bluetooth.rfkill) {
845 rfkill_unregister(samsung->bluetooth.rfkill);
846 rfkill_destroy(samsung->bluetooth.rfkill);
847 samsung->bluetooth.rfkill = NULL;
848 }
849}
850
851static int samsung_new_rfkill(struct samsung_laptop *samsung,
852 struct samsung_rfkill *arfkill,
853 const char *name, enum rfkill_type type,
854 const struct rfkill_ops *ops,
855 int blocked)
856{
857 struct rfkill **rfkill = &arfkill->rfkill;
858 int ret;
859
860 arfkill->type = type;
861 arfkill->samsung = samsung;
862
863 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
864 type, ops, arfkill);
865
866 if (!*rfkill)
867 return -EINVAL;
868
869 if (blocked != -1)
870 rfkill_init_sw_state(*rfkill, blocked);
871
872 ret = rfkill_register(*rfkill);
873 if (ret) {
874 rfkill_destroy(*rfkill);
875 *rfkill = NULL;
876 return ret;
877 }
878 return 0;
879}
880
881static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
882{
883 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
884 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
885}
886
887static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
888{
889 struct sabi_data data;
890 int ret;
891
892 ret = swsmi_wireless_status(samsung, &data);
Corentin Chary20db88e2011-12-15 08:27:39 +0100893 if (ret) {
894 /* Some swsmi laptops use the old seclinux way to control
895 * wireless devices */
896 if (ret == -EINVAL)
897 ret = samsung_rfkill_init_seclinux(samsung);
Corentin Chary84d482f2011-11-26 11:00:09 +0100898 return ret;
Corentin Chary20db88e2011-12-15 08:27:39 +0100899 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100900
901 /* 0x02 seems to mean that the device is no present/available */
902
903 if (data.data[WL_STATUS_WLAN] != 0x02)
904 ret = samsung_new_rfkill(samsung, &samsung->wlan,
905 "samsung-wlan",
906 RFKILL_TYPE_WLAN,
907 &swsmi_rfkill_ops,
908 !data.data[WL_STATUS_WLAN]);
909 if (ret)
910 goto exit;
911
912 if (data.data[WL_STATUS_BT] != 0x02)
913 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
914 "samsung-bluetooth",
915 RFKILL_TYPE_BLUETOOTH,
916 &swsmi_rfkill_ops,
917 !data.data[WL_STATUS_BT]);
918 if (ret)
919 goto exit;
920
921exit:
922 if (ret)
923 samsung_rfkill_exit(samsung);
924
925 return ret;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100926}
927
928static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
929{
Corentin Chary84d482f2011-11-26 11:00:09 +0100930 if (samsung->config->sabi_version == 2)
931 return samsung_rfkill_init_seclinux(samsung);
932 if (samsung->config->sabi_version == 3)
933 return samsung_rfkill_init_swsmi(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100934 return 0;
935}
936
Corentin Charyf674ebf2011-11-26 11:00:08 +0100937static int kbd_backlight_enable(struct samsung_laptop *samsung)
938{
939 const struct sabi_commands *commands = &samsung->config->commands;
940 struct sabi_data data;
941 int retval;
942
943 if (commands->kbd_backlight == 0xFFFF)
944 return -ENODEV;
945
946 memset(&data, 0, sizeof(data));
947 data.d0 = 0xaabb;
948 retval = sabi_command(samsung, commands->kbd_backlight,
949 &data, &data);
950
951 if (retval)
952 return retval;
953
954 if (data.d0 != 0xccdd)
955 return -ENODEV;
956 return 0;
957}
958
959static int kbd_backlight_read(struct samsung_laptop *samsung)
960{
961 const struct sabi_commands *commands = &samsung->config->commands;
962 struct sabi_data data;
963 int retval;
964
965 memset(&data, 0, sizeof(data));
966 data.data[0] = 0x81;
967 retval = sabi_command(samsung, commands->kbd_backlight,
968 &data, &data);
969
970 if (retval)
971 return retval;
972
973 return data.data[0];
974}
975
976static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
977{
978 const struct sabi_commands *commands = &samsung->config->commands;
979 struct sabi_data data;
980
981 memset(&data, 0, sizeof(data));
982 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
983 return sabi_command(samsung, commands->kbd_backlight,
984 &data, NULL);
985}
986
987static void kbd_led_update(struct work_struct *work)
988{
989 struct samsung_laptop *samsung;
990
991 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
992 kbd_backlight_write(samsung, samsung->kbd_led_wk);
993}
994
995static void kbd_led_set(struct led_classdev *led_cdev,
996 enum led_brightness value)
997{
998 struct samsung_laptop *samsung;
999
1000 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1001
1002 if (value > samsung->kbd_led.max_brightness)
1003 value = samsung->kbd_led.max_brightness;
1004 else if (value < 0)
1005 value = 0;
1006
1007 samsung->kbd_led_wk = value;
1008 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1009}
1010
1011static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1012{
1013 struct samsung_laptop *samsung;
1014
1015 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1016 return kbd_backlight_read(samsung);
1017}
1018
1019static void samsung_leds_exit(struct samsung_laptop *samsung)
1020{
1021 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1022 led_classdev_unregister(&samsung->kbd_led);
1023 if (samsung->led_workqueue)
1024 destroy_workqueue(samsung->led_workqueue);
1025}
1026
1027static int __init samsung_leds_init(struct samsung_laptop *samsung)
1028{
1029 int ret = 0;
1030
1031 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1032 if (!samsung->led_workqueue)
1033 return -ENOMEM;
1034
1035 if (kbd_backlight_enable(samsung) >= 0) {
1036 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1037
1038 samsung->kbd_led.name = "samsung::kbd_backlight";
1039 samsung->kbd_led.brightness_set = kbd_led_set;
1040 samsung->kbd_led.brightness_get = kbd_led_get;
1041 samsung->kbd_led.max_brightness = 8;
1042
1043 ret = led_classdev_register(&samsung->platform_device->dev,
1044 &samsung->kbd_led);
1045 }
1046
1047 if (ret)
1048 samsung_leds_exit(samsung);
1049
1050 return ret;
1051}
1052
Corentin Chary5dea7a22011-11-26 10:59:59 +01001053static void samsung_backlight_exit(struct samsung_laptop *samsung)
1054{
1055 if (samsung->backlight_device) {
1056 backlight_device_unregister(samsung->backlight_device);
1057 samsung->backlight_device = NULL;
1058 }
1059}
1060
1061static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1062{
1063 struct backlight_device *bd;
1064 struct backlight_properties props;
1065
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001066 if (!samsung->handle_backlight)
1067 return 0;
1068
Corentin Chary5dea7a22011-11-26 10:59:59 +01001069 memset(&props, 0, sizeof(struct backlight_properties));
1070 props.type = BACKLIGHT_PLATFORM;
1071 props.max_brightness = samsung->config->max_brightness -
1072 samsung->config->min_brightness;
1073
1074 bd = backlight_device_register("samsung",
1075 &samsung->platform_device->dev,
1076 samsung, &backlight_ops,
1077 &props);
1078 if (IS_ERR(bd))
1079 return PTR_ERR(bd);
1080
1081 samsung->backlight_device = bd;
1082 samsung->backlight_device->props.brightness = read_brightness(samsung);
1083 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1084 backlight_update_status(samsung->backlight_device);
1085
1086 return 0;
1087}
1088
Corentin Charya66c1662011-11-26 11:00:01 +01001089static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
1090 struct attribute *attr, int idx)
1091{
1092 struct device *dev = container_of(kobj, struct device, kobj);
1093 struct platform_device *pdev = to_platform_device(dev);
1094 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1095 bool ok = true;
1096
1097 if (attr == &dev_attr_performance_level.attr)
1098 ok = !!samsung->config->performance_levels[0].name;
Corentin Charycb5b5c92011-11-26 11:00:05 +01001099 if (attr == &dev_attr_battery_life_extender.attr)
1100 ok = !!(read_battery_life_extender(samsung) >= 0);
Corentin Chary3a75d372011-11-26 11:00:06 +01001101 if (attr == &dev_attr_usb_charge.attr)
1102 ok = !!(read_usb_charge(samsung) >= 0);
Corentin Charya66c1662011-11-26 11:00:01 +01001103
1104 return ok ? attr->mode : 0;
1105}
1106
1107static struct attribute_group platform_attribute_group = {
1108 .is_visible = samsung_sysfs_is_visible,
1109 .attrs = platform_attributes
1110};
1111
Corentin Chary5dea7a22011-11-26 10:59:59 +01001112static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1113{
Corentin Charya66c1662011-11-26 11:00:01 +01001114 struct platform_device *device = samsung->platform_device;
1115
1116 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001117}
1118
1119static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1120{
Corentin Charya66c1662011-11-26 11:00:01 +01001121 struct platform_device *device = samsung->platform_device;
1122
1123 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1124
Corentin Chary5dea7a22011-11-26 10:59:59 +01001125}
1126
Corentin Chary5b80fc42011-11-26 11:00:03 +01001127static int show_call(struct seq_file *m, void *data)
1128{
1129 struct samsung_laptop *samsung = m->private;
1130 struct sabi_data *sdata = &samsung->debug.data;
1131 int ret;
1132
1133 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1134 samsung->debug.command,
1135 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1136
1137 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1138
1139 if (ret) {
1140 seq_printf(m, "SABI command 0x%04x failed\n",
1141 samsung->debug.command);
1142 return ret;
1143 }
1144
1145 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1146 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1147 return 0;
1148}
1149
1150static int samsung_debugfs_open(struct inode *inode, struct file *file)
1151{
1152 return single_open(file, show_call, inode->i_private);
1153}
1154
1155static const struct file_operations samsung_laptop_call_io_ops = {
1156 .owner = THIS_MODULE,
1157 .open = samsung_debugfs_open,
1158 .read = seq_read,
1159 .llseek = seq_lseek,
1160 .release = single_release,
1161};
1162
1163static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1164{
1165 debugfs_remove_recursive(samsung->debug.root);
1166}
1167
1168static int samsung_debugfs_init(struct samsung_laptop *samsung)
1169{
1170 struct dentry *dent;
1171
1172 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1173 if (!samsung->debug.root) {
1174 pr_err("failed to create debugfs directory");
1175 goto error_debugfs;
1176 }
1177
1178 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1179 samsung->debug.f0000_wrapper.size = 0xffff;
1180
1181 samsung->debug.data_wrapper.data = &samsung->debug.data;
1182 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1183
Corentin Chary6f6ae062011-11-26 11:00:11 +01001184 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1185 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1186
Corentin Chary5b80fc42011-11-26 11:00:03 +01001187 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1188 samsung->debug.root, &samsung->debug.command);
1189 if (!dent)
1190 goto error_debugfs;
1191
1192 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1193 &samsung->debug.data.d0);
1194 if (!dent)
1195 goto error_debugfs;
1196
1197 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1198 &samsung->debug.data.d1);
1199 if (!dent)
1200 goto error_debugfs;
1201
1202 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1203 &samsung->debug.data.d2);
1204 if (!dent)
1205 goto error_debugfs;
1206
1207 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1208 &samsung->debug.data.d3);
1209 if (!dent)
1210 goto error_debugfs;
1211
1212 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1213 samsung->debug.root,
1214 &samsung->debug.data_wrapper);
1215 if (!dent)
1216 goto error_debugfs;
1217
1218 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1219 samsung->debug.root,
1220 &samsung->debug.f0000_wrapper);
1221 if (!dent)
1222 goto error_debugfs;
1223
1224 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1225 samsung->debug.root, samsung,
1226 &samsung_laptop_call_io_ops);
1227 if (!dent)
1228 goto error_debugfs;
1229
Corentin Chary6f6ae062011-11-26 11:00:11 +01001230 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1231 samsung->debug.root,
1232 &samsung->debug.sdiag_wrapper);
1233 if (!dent)
1234 goto error_debugfs;
1235
Corentin Chary5b80fc42011-11-26 11:00:03 +01001236 return 0;
1237
1238error_debugfs:
1239 samsung_debugfs_exit(samsung);
1240 return -ENOMEM;
1241}
1242
Corentin Chary5dea7a22011-11-26 10:59:59 +01001243static void samsung_sabi_exit(struct samsung_laptop *samsung)
1244{
1245 const struct sabi_config *config = samsung->config;
1246
1247 /* Turn off "Linux" mode in the BIOS */
1248 if (config && config->commands.set_linux != 0xff)
Corentin Chary7e960712011-11-26 11:00:02 +01001249 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001250
1251 if (samsung->sabi_iface) {
1252 iounmap(samsung->sabi_iface);
1253 samsung->sabi_iface = NULL;
1254 }
1255 if (samsung->f0000_segment) {
1256 iounmap(samsung->f0000_segment);
1257 samsung->f0000_segment = NULL;
1258 }
1259
1260 samsung->config = NULL;
1261}
1262
Corentin Chary49dd7732011-11-26 11:00:04 +01001263static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1264 unsigned int ifaceP)
Corentin Chary5dea7a22011-11-26 10:59:59 +01001265{
1266 const struct sabi_config *config = samsung->config;
1267
1268 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1269 loca + 0xf0000 - 6);
Corentin Chary49dd7732011-11-26 11:00:04 +01001270
Corentin Chary5dea7a22011-11-26 10:59:59 +01001271 printk(KERN_DEBUG "SABI header:\n");
1272 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1273 readw(samsung->sabi + config->header_offsets.port));
1274 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1275 readb(samsung->sabi + config->header_offsets.iface_func));
1276 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1277 readb(samsung->sabi + config->header_offsets.en_mem));
1278 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1279 readb(samsung->sabi + config->header_offsets.re_mem));
1280 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1281 readw(samsung->sabi + config->header_offsets.data_offset));
1282 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1283 readw(samsung->sabi + config->header_offsets.data_segment));
Corentin Chary5dea7a22011-11-26 10:59:59 +01001284
Corentin Chary49dd7732011-11-26 11:00:04 +01001285 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001286}
1287
Corentin Chary6f6ae062011-11-26 11:00:11 +01001288static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1289{
1290 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1291 int i;
1292
1293 if (loca == 0xffff)
1294 return ;
1295
1296 /* Example:
1297 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1298 *
1299 * Product name: 90X3A
1300 * BIOS Version: 07HL
1301 */
1302 loca += 1;
1303 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1304 char temp = readb(samsung->f0000_segment + loca);
1305
1306 if (isalnum(temp) || temp == '/' || temp == '-')
1307 samsung->sdiag[i++] = temp;
1308 else
1309 break ;
1310 }
1311
1312 if (debug && samsung->sdiag[0])
1313 pr_info("sdiag: %s", samsung->sdiag);
1314}
1315
Corentin Chary5dea7a22011-11-26 10:59:59 +01001316static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1317{
1318 const struct sabi_config *config = NULL;
1319 const struct sabi_commands *commands;
1320 unsigned int ifaceP;
1321 int ret = 0;
1322 int i;
1323 int loca;
1324
1325 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1326 if (!samsung->f0000_segment) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001327 if (debug || force)
1328 pr_err("Can't map the segment at 0xf0000\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001329 ret = -EINVAL;
1330 goto exit;
1331 }
1332
Corentin Chary6f6ae062011-11-26 11:00:11 +01001333 samsung_sabi_diag(samsung);
1334
Corentin Chary5dea7a22011-11-26 10:59:59 +01001335 /* Try to find one of the signatures in memory to find the header */
1336 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1337 samsung->config = &sabi_configs[i];
1338 loca = find_signature(samsung->f0000_segment,
1339 samsung->config->test_string);
1340 if (loca != 0xffff)
1341 break;
1342 }
1343
1344 if (loca == 0xffff) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001345 if (debug || force)
1346 pr_err("This computer does not support SABI\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001347 ret = -ENODEV;
1348 goto exit;
1349 }
1350
1351 config = samsung->config;
1352 commands = &config->commands;
1353
1354 /* point to the SMI port Number */
1355 loca += 1;
1356 samsung->sabi = (samsung->f0000_segment + loca);
1357
Corentin Chary5dea7a22011-11-26 10:59:59 +01001358 /* Get a pointer to the SABI Interface */
1359 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1360 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
Corentin Chary49dd7732011-11-26 11:00:04 +01001361
1362 if (debug)
1363 samsung_sabi_infos(samsung, loca, ifaceP);
1364
Corentin Chary5dea7a22011-11-26 10:59:59 +01001365 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1366 if (!samsung->sabi_iface) {
1367 pr_err("Can't remap %x\n", ifaceP);
1368 ret = -EINVAL;
1369 goto exit;
1370 }
1371
Corentin Chary5dea7a22011-11-26 10:59:59 +01001372 /* Turn on "Linux" mode in the BIOS */
1373 if (commands->set_linux != 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +01001374 int retval = sabi_set_commandb(samsung,
1375 commands->set_linux, 0x81);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001376 if (retval) {
1377 pr_warn("Linux mode was not set!\n");
1378 ret = -ENODEV;
1379 goto exit;
1380 }
1381 }
1382
1383 /* Check for stepping quirk */
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001384 if (samsung->handle_backlight)
1385 check_for_stepping_quirk(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001386
Corentin Chary2e777182011-11-26 11:00:12 +01001387 pr_info("detected SABI interface: %s\n",
1388 samsung->config->test_string);
1389
Corentin Chary5dea7a22011-11-26 10:59:59 +01001390exit:
1391 if (ret)
1392 samsung_sabi_exit(samsung);
1393
1394 return ret;
1395}
1396
1397static void samsung_platform_exit(struct samsung_laptop *samsung)
1398{
1399 if (samsung->platform_device) {
1400 platform_device_unregister(samsung->platform_device);
1401 samsung->platform_device = NULL;
1402 }
1403}
1404
1405static int __init samsung_platform_init(struct samsung_laptop *samsung)
1406{
1407 struct platform_device *pdev;
1408
1409 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1410 if (IS_ERR(pdev))
1411 return PTR_ERR(pdev);
1412
1413 samsung->platform_device = pdev;
1414 platform_set_drvdata(samsung->platform_device, samsung);
1415 return 0;
1416}
1417
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001418static struct dmi_system_id __initdata samsung_dmi_table[] = {
1419 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001420 .matches = {
1421 DMI_MATCH(DMI_SYS_VENDOR,
1422 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001423 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001424 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001425 },
1426 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001427 .matches = {
1428 DMI_MATCH(DMI_SYS_VENDOR,
1429 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001430 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001431 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001432 },
1433 {
J Witteveen4e2441c2011-07-03 13:15:44 +02001434 .matches = {
1435 DMI_MATCH(DMI_SYS_VENDOR,
1436 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001437 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
J Witteveen4e2441c2011-07-03 13:15:44 +02001438 },
J Witteveen4e2441c2011-07-03 13:15:44 +02001439 },
1440 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001441 .matches = {
1442 DMI_MATCH(DMI_SYS_VENDOR,
1443 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001444 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001445 },
Tommaso Massimi7500eeb2011-09-20 09:16:09 -07001446 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001447 { },
1448};
1449MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1450
Corentin Chary5dea7a22011-11-26 10:59:59 +01001451static struct platform_device *samsung_platform_device;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001452
1453static int __init samsung_init(void)
1454{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001455 struct samsung_laptop *samsung;
1456 int ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001457
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001458 if (!force && !dmi_check_system(samsung_dmi_table))
1459 return -ENODEV;
1460
Corentin Charya6df4892011-11-26 10:59:58 +01001461 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1462 if (!samsung)
1463 return -ENOMEM;
1464
1465 mutex_init(&samsung->sabi_mutex);
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001466 samsung->handle_backlight = true;
1467
1468#ifdef CONFIG_ACPI
1469 /* Don't handle backlight here if the acpi video already handle it */
Corentin Chary3be324a2011-11-26 11:00:10 +01001470 if (acpi_video_backlight_support())
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001471 samsung->handle_backlight = false;
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001472#endif
Corentin Chary5dea7a22011-11-26 10:59:59 +01001473 ret = samsung_platform_init(samsung);
1474 if (ret)
1475 goto error_platform;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001476
Corentin Chary5dea7a22011-11-26 10:59:59 +01001477 ret = samsung_sabi_init(samsung);
1478 if (ret)
1479 goto error_sabi;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001480
Corentin Chary3be324a2011-11-26 11:00:10 +01001481#ifdef CONFIG_ACPI
1482 /* Only log that if we are really on a sabi platform */
1483 if (acpi_video_backlight_support())
1484 pr_info("Backlight controlled by ACPI video driver\n");
1485#endif
1486
Corentin Chary5dea7a22011-11-26 10:59:59 +01001487 ret = samsung_sysfs_init(samsung);
1488 if (ret)
1489 goto error_sysfs;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001490
Corentin Chary5dea7a22011-11-26 10:59:59 +01001491 ret = samsung_backlight_init(samsung);
1492 if (ret)
1493 goto error_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +01001494
Corentin Chary5dea7a22011-11-26 10:59:59 +01001495 ret = samsung_rfkill_init(samsung);
1496 if (ret)
1497 goto error_rfkill;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001498
Corentin Charyf674ebf2011-11-26 11:00:08 +01001499 ret = samsung_leds_init(samsung);
1500 if (ret)
1501 goto error_leds;
1502
Corentin Chary5b80fc42011-11-26 11:00:03 +01001503 ret = samsung_debugfs_init(samsung);
1504 if (ret)
1505 goto error_debugfs;
1506
Corentin Chary5dea7a22011-11-26 10:59:59 +01001507 samsung_platform_device = samsung->platform_device;
1508 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001509
Corentin Chary5b80fc42011-11-26 11:00:03 +01001510error_debugfs:
Corentin Charyf674ebf2011-11-26 11:00:08 +01001511 samsung_leds_exit(samsung);
1512error_leds:
Corentin Chary5b80fc42011-11-26 11:00:03 +01001513 samsung_rfkill_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001514error_rfkill:
1515 samsung_backlight_exit(samsung);
1516error_backlight:
1517 samsung_sysfs_exit(samsung);
1518error_sysfs:
1519 samsung_sabi_exit(samsung);
1520error_sabi:
1521 samsung_platform_exit(samsung);
1522error_platform:
Corentin Charya6df4892011-11-26 10:59:58 +01001523 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001524 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001525}
1526
1527static void __exit samsung_exit(void)
1528{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001529 struct samsung_laptop *samsung;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001530
Corentin Chary5dea7a22011-11-26 10:59:59 +01001531 samsung = platform_get_drvdata(samsung_platform_device);
Corentin Charya6df4892011-11-26 10:59:58 +01001532
Corentin Chary5b80fc42011-11-26 11:00:03 +01001533 samsung_debugfs_exit(samsung);
Corentin Charyf674ebf2011-11-26 11:00:08 +01001534 samsung_leds_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001535 samsung_rfkill_exit(samsung);
1536 samsung_backlight_exit(samsung);
1537 samsung_sysfs_exit(samsung);
1538 samsung_sabi_exit(samsung);
1539 samsung_platform_exit(samsung);
1540
Corentin Charya6df4892011-11-26 10:59:58 +01001541 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001542 samsung_platform_device = NULL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001543}
1544
1545module_init(samsung_init);
1546module_exit(samsung_exit);
1547
1548MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1549MODULE_DESCRIPTION("Samsung Backlight driver");
1550MODULE_LICENSE("GPL");