aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/samsung-laptop.c
diff options
context:
space:
mode:
authorCorentin Chary <corentincj@iksaif.net>2011-11-26 05:00:02 -0500
committerMatthew Garrett <mjg@redhat.com>2012-03-20 12:02:07 -0400
commit7e9607118b08bd4e1dae65d56e425e8de7f3da1c (patch)
tree7abeea849ad1851908edb94176dfda0e9f4b159e /drivers/platform/x86/samsung-laptop.c
parenta66c16627cfba7cd1da1f8bd04cab4219d09154b (diff)
samsung-laptop: ehance SABI support
* SABI command are on 16 bits, not 8 * SABI can read/write up to 11 byte of data * There is not real difference between "get" and "set" commands, so refactorise the code of both functions Signed-off-by: Corentin Chary <corentincj@iksaif.net> Acked-by: Greg Kroah-Hartman <gregkh@suse.de> Signed-off-by: Matthew Garrett <mjg@redhat.com>
Diffstat (limited to 'drivers/platform/x86/samsung-laptop.c')
-rw-r--r--drivers/platform/x86/samsung-laptop.c225
1 files changed, 113 insertions, 112 deletions
diff --git a/drivers/platform/x86/samsung-laptop.c b/drivers/platform/x86/samsung-laptop.c
index 9fbf5c0d3646..134444f176e6 100644
--- a/drivers/platform/x86/samsung-laptop.c
+++ b/drivers/platform/x86/samsung-laptop.c
@@ -42,9 +42,17 @@
42#define SABI_IFACE_COMPLETE 0x04 42#define SABI_IFACE_COMPLETE 0x04
43#define SABI_IFACE_DATA 0x05 43#define SABI_IFACE_DATA 0x05
44 44
45/* Structure to get data back to the calling function */ 45/* Structure get/set data using sabi */
46struct sabi_retval { 46struct sabi_data {
47 u8 retval[20]; 47 union {
48 struct {
49 u32 d0;
50 u32 d1;
51 u16 d2;
52 u8 d3;
53 };
54 u8 data[11];
55 };
48}; 56};
49 57
50struct sabi_header_offsets { 58struct sabi_header_offsets {
@@ -61,8 +69,8 @@ struct sabi_commands {
61 * Brightness is 0 - 8, as described above. 69 * Brightness is 0 - 8, as described above.
62 * Value 0 is for the BIOS to use 70 * Value 0 is for the BIOS to use
63 */ 71 */
64 u8 get_brightness; 72 u16 get_brightness;
65 u8 set_brightness; 73 u16 set_brightness;
66 74
67 /* 75 /*
68 * first byte: 76 * first byte:
@@ -73,37 +81,37 @@ struct sabi_commands {
73 * 0x03 - 3G is on 81 * 0x03 - 3G is on
74 * TODO, verify 3G is correct, that doesn't seem right... 82 * TODO, verify 3G is correct, that doesn't seem right...
75 */ 83 */
76 u8 get_wireless_button; 84 u16 get_wireless_button;
77 u8 set_wireless_button; 85 u16 set_wireless_button;
78 86
79 /* 0 is off, 1 is on */ 87 /* 0 is off, 1 is on */
80 u8 get_backlight; 88 u16 get_backlight;
81 u8 set_backlight; 89 u16 set_backlight;
82 90
83 /* 91 /*
84 * 0x80 or 0x00 - no action 92 * 0x80 or 0x00 - no action
85 * 0x81 - recovery key pressed 93 * 0x81 - recovery key pressed
86 */ 94 */
87 u8 get_recovery_mode; 95 u16 get_recovery_mode;
88 u8 set_recovery_mode; 96 u16 set_recovery_mode;
89 97
90 /* 98 /*
91 * on seclinux: 0 is low, 1 is high, 99 * on seclinux: 0 is low, 1 is high,
92 * on swsmi: 0 is normal, 1 is silent, 2 is turbo 100 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
93 */ 101 */
94 u8 get_performance_level; 102 u16 get_performance_level;
95 u8 set_performance_level; 103 u16 set_performance_level;
96 104
97 /* 105 /*
98 * Tell the BIOS that Linux is running on this machine. 106 * Tell the BIOS that Linux is running on this machine.
99 * 81 is on, 80 is off 107 * 81 is on, 80 is off
100 */ 108 */
101 u8 set_linux; 109 u16 set_linux;
102}; 110};
103 111
104struct sabi_performance_level { 112struct sabi_performance_level {
105 const char *name; 113 const char *name;
106 u8 value; 114 u16 value;
107}; 115};
108 116
109struct sabi_config { 117struct sabi_config {
@@ -246,16 +254,25 @@ static bool debug;
246module_param(debug, bool, S_IRUGO | S_IWUSR); 254module_param(debug, bool, S_IRUGO | S_IWUSR);
247MODULE_PARM_DESC(debug, "Debug enabled or not"); 255MODULE_PARM_DESC(debug, "Debug enabled or not");
248 256
249static int sabi_get_command(struct samsung_laptop *samsung, 257static int sabi_command(struct samsung_laptop *samsung, u16 command,
250 u8 command, struct sabi_retval *sretval) 258 struct sabi_data *in,
259 struct sabi_data *out)
251{ 260{
252 const struct sabi_config *config = samsung->config; 261 const struct sabi_config *config = samsung->config;
253 int retval = 0; 262 int ret = 0;
254 u16 port = readw(samsung->sabi + config->header_offsets.port); 263 u16 port = readw(samsung->sabi + config->header_offsets.port);
255 u8 complete, iface_data; 264 u8 complete, iface_data;
256 265
257 mutex_lock(&samsung->sabi_mutex); 266 mutex_lock(&samsung->sabi_mutex);
258 267
268 if (debug) {
269 if (in)
270 pr_info("SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
271 command, in->d0, in->d1, in->d2, in->d3);
272 else
273 pr_info("SABI 0x%04x", command);
274 }
275
259 /* enable memory to be able to write to it */ 276 /* enable memory to be able to write to it */
260 outb(readb(samsung->sabi + config->header_offsets.en_mem), port); 277 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
261 278
@@ -263,6 +280,12 @@ static int sabi_get_command(struct samsung_laptop *samsung,
263 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN); 280 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
264 writew(command, samsung->sabi_iface + SABI_IFACE_SUB); 281 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
265 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE); 282 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
283 if (in) {
284 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
285 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
286 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
287 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
288 }
266 outb(readb(samsung->sabi + config->header_offsets.iface_func), port); 289 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
267 290
268 /* write protect memory to make it safe */ 291 /* write protect memory to make it safe */
@@ -272,127 +295,105 @@ static int sabi_get_command(struct samsung_laptop *samsung,
272 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE); 295 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
273 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA); 296 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
274 if (complete != 0xaa || iface_data == 0xff) { 297 if (complete != 0xaa || iface_data == 0xff) {
275 pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n", 298 pr_warn("SABI command 0x%04x failed with"
276 command, complete, iface_data); 299 " completion flag 0x%02x and interface data 0x%02x",
277 retval = -EINVAL; 300 command, complete, iface_data);
301 ret = -EINVAL;
278 goto exit; 302 goto exit;
279 } 303 }
280 /* 304
281 * Save off the data into a structure so the caller use it. 305 if (out) {
282 * Right now we only want the first 4 bytes, 306 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
283 * There are commands that need more, but not for the ones we 307 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
284 * currently care about. 308 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
285 */ 309 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
286 sretval->retval[0] = readb(samsung->sabi_iface + SABI_IFACE_DATA); 310 }
287 sretval->retval[1] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1); 311
288 sretval->retval[2] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 2); 312 if (debug && out) {
289 sretval->retval[3] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 3); 313 pr_info("SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
314 out->d0, out->d1, out->d2, out->d3);
315 }
290 316
291exit: 317exit:
292 mutex_unlock(&samsung->sabi_mutex); 318 mutex_unlock(&samsung->sabi_mutex);
293 return retval; 319 return ret;
294
295} 320}
296 321
297static int sabi_set_command(struct samsung_laptop *samsung, 322/* simple wrappers usable with most commands */
298 u8 command, u8 data) 323static int sabi_set_commandb(struct samsung_laptop *samsung,
324 u16 command, u8 data)
299{ 325{
300 const struct sabi_config *config = samsung->config; 326 struct sabi_data in = { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 };
301 int retval = 0;
302 u16 port = readw(samsung->sabi + config->header_offsets.port);
303 u8 complete, iface_data;
304 327
305 mutex_lock(&samsung->sabi_mutex); 328 in.data[0] = data;
306 329 return sabi_command(samsung, command, &in, NULL);
307 /* enable memory to be able to write to it */
308 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
309
310 /* write out the command */
311 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
312 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
313 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
314 writeb(data, samsung->sabi_iface + SABI_IFACE_DATA);
315 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
316
317 /* write protect memory to make it safe */
318 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
319
320 /* see if the command actually succeeded */
321 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
322 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
323 if (complete != 0xaa || iface_data == 0xff) {
324 pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
325 command, complete, iface_data);
326 retval = -EINVAL;
327 }
328
329 mutex_unlock(&samsung->sabi_mutex);
330 return retval;
331} 330}
332 331
333static void test_backlight(struct samsung_laptop *samsung) 332static void test_backlight(struct samsung_laptop *samsung)
334{ 333{
335 const struct sabi_commands *commands = &samsung->config->commands; 334 const struct sabi_commands *commands = &samsung->config->commands;
336 struct sabi_retval sretval; 335 struct sabi_data sretval;
337 336
338 sabi_get_command(samsung, commands->get_backlight, &sretval); 337 sabi_command(samsung, commands->get_backlight, NULL, &sretval);
339 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]); 338 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.data[0]);
340 339
341 sabi_set_command(samsung, commands->set_backlight, 0); 340 sabi_set_commandb(samsung, commands->set_backlight, 0);
342 printk(KERN_DEBUG "backlight should be off\n"); 341 printk(KERN_DEBUG "backlight should be off\n");
343 342
344 sabi_get_command(samsung, commands->get_backlight, &sretval); 343 sabi_command(samsung, commands->get_backlight, NULL, &sretval);
345 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]); 344 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.data[0]);
346 345
347 msleep(1000); 346 msleep(1000);
348 347
349 sabi_set_command(samsung, commands->set_backlight, 1); 348 sabi_set_commandb(samsung, commands->set_backlight, 1);
350 printk(KERN_DEBUG "backlight should be on\n"); 349 printk(KERN_DEBUG "backlight should be on\n");
351 350
352 sabi_get_command(samsung, commands->get_backlight, &sretval); 351 sabi_command(samsung, commands->get_backlight, NULL, &sretval);
353 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]); 352 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.data[0]);
354} 353}
355 354
356static void test_wireless(struct samsung_laptop *samsung) 355static void test_wireless(struct samsung_laptop *samsung)
357{ 356{
358 const struct sabi_commands *commands = &samsung->config->commands; 357 const struct sabi_commands *commands = &samsung->config->commands;
359 struct sabi_retval sretval; 358 struct sabi_data sretval;
360 359
361 sabi_get_command(samsung, commands->get_wireless_button, &sretval); 360 sabi_command(samsung, commands->get_wireless_button, NULL, &sretval);
362 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]); 361 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.data[0]);
363 362
364 sabi_set_command(samsung, commands->set_wireless_button, 0); 363 sabi_set_commandb(samsung, commands->set_wireless_button, 0);
365 printk(KERN_DEBUG "wireless led should be off\n"); 364 printk(KERN_DEBUG "wireless led should be off\n");
366 365
367 sabi_get_command(samsung, commands->get_wireless_button, &sretval); 366 sabi_command(samsung, commands->get_wireless_button, NULL, &sretval);
368 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]); 367 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.data[0]);
369 368
370 msleep(1000); 369 msleep(1000);
371 370
372 sabi_set_command(samsung, commands->set_wireless_button, 1); 371 sabi_set_commandb(samsung, commands->set_wireless_button, 1);
373 printk(KERN_DEBUG "wireless led should be on\n"); 372 printk(KERN_DEBUG "wireless led should be on\n");
374 373
375 sabi_get_command(samsung, commands->get_wireless_button, &sretval); 374 sabi_command(samsung, commands->get_wireless_button, NULL, &sretval);
376 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]); 375 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.data[0]);
377} 376}
378 377
379static int read_brightness(struct samsung_laptop *samsung) 378static int read_brightness(struct samsung_laptop *samsung)
380{ 379{
381 const struct sabi_config *config = samsung->config; 380 const struct sabi_config *config = samsung->config;
382 const struct sabi_commands *commands = &samsung->config->commands; 381 const struct sabi_commands *commands = &samsung->config->commands;
383 struct sabi_retval sretval; 382 struct sabi_data sretval;
384 int user_brightness = 0; 383 int user_brightness = 0;
385 int retval; 384 int retval;
386 385
387 retval = sabi_get_command(samsung, commands->get_brightness, 386 retval = sabi_command(samsung, commands->get_brightness,
388 &sretval); 387 NULL, &sretval);
389 if (!retval) { 388 if (retval)
390 user_brightness = sretval.retval[0]; 389 return retval;
391 if (user_brightness > config->min_brightness) 390
392 user_brightness -= config->min_brightness; 391 user_brightness = sretval.data[0];
393 else 392 if (user_brightness > config->min_brightness)
394 user_brightness = 0; 393 user_brightness -= config->min_brightness;
395 } 394 else
395 user_brightness = 0;
396
396 return user_brightness; 397 return user_brightness;
397} 398}
398 399
@@ -410,10 +411,10 @@ static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
410 if (user_brightness == read_brightness(samsung)) 411 if (user_brightness == read_brightness(samsung))
411 return; 412 return;
412 413
413 sabi_set_command(samsung, commands->set_brightness, 0); 414 sabi_set_commandb(samsung, commands->set_brightness, 0);
414 } 415 }
415 416
416 sabi_set_command(samsung, commands->set_brightness, user_level); 417 sabi_set_commandb(samsung, commands->set_brightness, user_level);
417} 418}
418 419
419static int get_brightness(struct backlight_device *bd) 420static int get_brightness(struct backlight_device *bd)
@@ -465,9 +466,9 @@ static int update_status(struct backlight_device *bd)
465 set_brightness(samsung, bd->props.brightness); 466 set_brightness(samsung, bd->props.brightness);
466 467
467 if (bd->props.power == FB_BLANK_UNBLANK) 468 if (bd->props.power == FB_BLANK_UNBLANK)
468 sabi_set_command(samsung, commands->set_backlight, 1); 469 sabi_set_commandb(samsung, commands->set_backlight, 1);
469 else 470 else
470 sabi_set_command(samsung, commands->set_backlight, 0); 471 sabi_set_commandb(samsung, commands->set_backlight, 0);
471 472
472 return 0; 473 return 0;
473} 474}
@@ -488,9 +489,9 @@ static int rfkill_set(void *data, bool blocked)
488 * blocked == true is off 489 * blocked == true is off
489 */ 490 */
490 if (blocked) 491 if (blocked)
491 sabi_set_command(samsung, commands->set_wireless_button, 0); 492 sabi_set_commandb(samsung, commands->set_wireless_button, 0);
492 else 493 else
493 sabi_set_command(samsung, commands->set_wireless_button, 1); 494 sabi_set_commandb(samsung, commands->set_wireless_button, 1);
494 495
495 return 0; 496 return 0;
496} 497}
@@ -505,19 +506,19 @@ static ssize_t get_performance_level(struct device *dev,
505 struct samsung_laptop *samsung = dev_get_drvdata(dev); 506 struct samsung_laptop *samsung = dev_get_drvdata(dev);
506 const struct sabi_config *config = samsung->config; 507 const struct sabi_config *config = samsung->config;
507 const struct sabi_commands *commands = &config->commands; 508 const struct sabi_commands *commands = &config->commands;
508 struct sabi_retval sretval; 509 struct sabi_data sretval;
509 int retval; 510 int retval;
510 int i; 511 int i;
511 512
512 /* Read the state */ 513 /* Read the state */
513 retval = sabi_get_command(samsung, commands->get_performance_level, 514 retval = sabi_command(samsung, commands->get_performance_level,
514 &sretval); 515 NULL, &sretval);
515 if (retval) 516 if (retval)
516 return retval; 517 return retval;
517 518
518 /* The logic is backwards, yeah, lots of fun... */ 519 /* The logic is backwards, yeah, lots of fun... */
519 for (i = 0; config->performance_levels[i].name; ++i) { 520 for (i = 0; config->performance_levels[i].name; ++i) {
520 if (sretval.retval[0] == config->performance_levels[i].value) 521 if (sretval.data[0] == config->performance_levels[i].value)
521 return sprintf(buf, "%s\n", config->performance_levels[i].name); 522 return sprintf(buf, "%s\n", config->performance_levels[i].name);
522 } 523 }
523 return sprintf(buf, "%s\n", "unknown"); 524 return sprintf(buf, "%s\n", "unknown");
@@ -539,9 +540,9 @@ static ssize_t set_performance_level(struct device *dev,
539 const struct sabi_performance_level *level = 540 const struct sabi_performance_level *level =
540 &config->performance_levels[i]; 541 &config->performance_levels[i];
541 if (!strncasecmp(level->name, buf, strlen(level->name))) { 542 if (!strncasecmp(level->name, buf, strlen(level->name))) {
542 sabi_set_command(samsung, 543 sabi_set_commandb(samsung,
543 commands->set_performance_level, 544 commands->set_performance_level,
544 level->value); 545 level->value);
545 break; 546 break;
546 } 547 }
547 } 548 }
@@ -685,7 +686,7 @@ static void samsung_sabi_exit(struct samsung_laptop *samsung)
685 686
686 /* Turn off "Linux" mode in the BIOS */ 687 /* Turn off "Linux" mode in the BIOS */
687 if (config && config->commands.set_linux != 0xff) 688 if (config && config->commands.set_linux != 0xff)
688 sabi_set_command(samsung, config->commands.set_linux, 0x80); 689 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
689 690
690 if (samsung->sabi_iface) { 691 if (samsung->sabi_iface) {
691 iounmap(samsung->sabi_iface); 692 iounmap(samsung->sabi_iface);
@@ -724,7 +725,7 @@ static void __init samsung_sabi_selftest(struct samsung_laptop *samsung,
724 unsigned int ifaceP) 725 unsigned int ifaceP)
725{ 726{
726 const struct sabi_config *config = samsung->config; 727 const struct sabi_config *config = samsung->config;
727 struct sabi_retval sretval; 728 struct sabi_data sretval;
728 729
729 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP); 730 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
730 printk(KERN_DEBUG "sabi_iface = %p\n", samsung->sabi_iface); 731 printk(KERN_DEBUG "sabi_iface = %p\n", samsung->sabi_iface);
@@ -733,8 +734,8 @@ static void __init samsung_sabi_selftest(struct samsung_laptop *samsung,
733 test_backlight(samsung); 734 test_backlight(samsung);
734 test_wireless(samsung); 735 test_wireless(samsung);
735 736
736 sabi_get_command(samsung, config->commands.get_brightness, &sretval); 737 sabi_command(samsung, config->commands.get_brightness, NULL, &sretval);
737 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]); 738 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.data[0]);
738} 739}
739 740
740static int __init samsung_sabi_init(struct samsung_laptop *samsung) 741static int __init samsung_sabi_init(struct samsung_laptop *samsung)
@@ -793,8 +794,8 @@ static int __init samsung_sabi_init(struct samsung_laptop *samsung)
793 794
794 /* Turn on "Linux" mode in the BIOS */ 795 /* Turn on "Linux" mode in the BIOS */
795 if (commands->set_linux != 0xff) { 796 if (commands->set_linux != 0xff) {
796 int retval = sabi_set_command(samsung, 797 int retval = sabi_set_commandb(samsung,
797 commands->set_linux, 0x81); 798 commands->set_linux, 0x81);
798 if (retval) { 799 if (retval) {
799 pr_warn("Linux mode was not set!\n"); 800 pr_warn("Linux mode was not set!\n");
800 ret = -ENODEV; 801 ret = -ENODEV;