drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / platform / x86 / dell-laptop.c
bloba561f653cf134ceae3ca4c4fc02236fd16d5b01e
1 /*
2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
25 #include <linux/io.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
29 #include <linux/mm.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <acpi/video.h>
34 #include "dell-rbtn.h"
35 #include "dell-smbios.h"
37 struct quirk_entry {
38 bool touchpad_led;
39 bool kbd_led_levels_off_1;
40 bool kbd_missing_ac_tag;
42 bool needs_kbd_timeouts;
44 * Ordered list of timeouts expressed in seconds.
45 * The list must end with -1
47 int kbd_timeouts[];
50 static struct quirk_entry *quirks;
52 static struct quirk_entry quirk_dell_vostro_v130 = {
53 .touchpad_led = true,
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
58 quirks = dmi->driver_data;
59 return 1;
63 * These values come from Windows utility provided by Dell. If any other value
64 * is used then BIOS silently set timeout to 0 without any error message.
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 .needs_kbd_timeouts = true,
68 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
71 static struct quirk_entry quirk_dell_xps13_9370 = {
72 .kbd_missing_ac_tag = true,
75 static struct quirk_entry quirk_dell_latitude_e6410 = {
76 .kbd_led_levels_off_1 = true,
79 static struct platform_driver platform_driver = {
80 .driver = {
81 .name = "dell-laptop",
85 static struct platform_device *platform_device;
86 static struct backlight_device *dell_backlight_device;
87 static struct rfkill *wifi_rfkill;
88 static struct rfkill *bluetooth_rfkill;
89 static struct rfkill *wwan_rfkill;
90 static bool force_rfkill;
92 module_param(force_rfkill, bool, 0444);
93 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
95 static const struct dmi_system_id dell_device_table[] __initconst = {
97 .ident = "Dell laptop",
98 .matches = {
99 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
100 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
104 .matches = {
105 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
106 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
110 .matches = {
111 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
112 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
116 .matches = {
117 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
118 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
122 .matches = {
123 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
124 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
128 .matches = {
129 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
130 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
134 .ident = "Dell Computer Corporation",
135 .matches = {
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
137 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
142 MODULE_DEVICE_TABLE(dmi, dell_device_table);
144 static const struct dmi_system_id dell_quirks[] __initconst = {
146 .callback = dmi_matched,
147 .ident = "Dell Vostro V130",
148 .matches = {
149 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
150 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
152 .driver_data = &quirk_dell_vostro_v130,
155 .callback = dmi_matched,
156 .ident = "Dell Vostro V131",
157 .matches = {
158 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
159 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
161 .driver_data = &quirk_dell_vostro_v130,
164 .callback = dmi_matched,
165 .ident = "Dell Vostro 3350",
166 .matches = {
167 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
168 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
170 .driver_data = &quirk_dell_vostro_v130,
173 .callback = dmi_matched,
174 .ident = "Dell Vostro 3555",
175 .matches = {
176 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
177 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
179 .driver_data = &quirk_dell_vostro_v130,
182 .callback = dmi_matched,
183 .ident = "Dell Inspiron N311z",
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
186 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
188 .driver_data = &quirk_dell_vostro_v130,
191 .callback = dmi_matched,
192 .ident = "Dell Inspiron M5110",
193 .matches = {
194 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
195 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
197 .driver_data = &quirk_dell_vostro_v130,
200 .callback = dmi_matched,
201 .ident = "Dell Vostro 3360",
202 .matches = {
203 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
204 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
206 .driver_data = &quirk_dell_vostro_v130,
209 .callback = dmi_matched,
210 .ident = "Dell Vostro 3460",
211 .matches = {
212 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
213 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
215 .driver_data = &quirk_dell_vostro_v130,
218 .callback = dmi_matched,
219 .ident = "Dell Vostro 3560",
220 .matches = {
221 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
224 .driver_data = &quirk_dell_vostro_v130,
227 .callback = dmi_matched,
228 .ident = "Dell Vostro 3450",
229 .matches = {
230 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
231 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
233 .driver_data = &quirk_dell_vostro_v130,
236 .callback = dmi_matched,
237 .ident = "Dell Inspiron 5420",
238 .matches = {
239 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
240 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
242 .driver_data = &quirk_dell_vostro_v130,
245 .callback = dmi_matched,
246 .ident = "Dell Inspiron 5520",
247 .matches = {
248 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
249 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
251 .driver_data = &quirk_dell_vostro_v130,
254 .callback = dmi_matched,
255 .ident = "Dell Inspiron 5720",
256 .matches = {
257 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
258 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
260 .driver_data = &quirk_dell_vostro_v130,
263 .callback = dmi_matched,
264 .ident = "Dell Inspiron 7420",
265 .matches = {
266 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
267 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
269 .driver_data = &quirk_dell_vostro_v130,
272 .callback = dmi_matched,
273 .ident = "Dell Inspiron 7520",
274 .matches = {
275 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
276 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
278 .driver_data = &quirk_dell_vostro_v130,
281 .callback = dmi_matched,
282 .ident = "Dell Inspiron 7720",
283 .matches = {
284 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
285 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
287 .driver_data = &quirk_dell_vostro_v130,
290 .callback = dmi_matched,
291 .ident = "Dell XPS13 9333",
292 .matches = {
293 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
294 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
296 .driver_data = &quirk_dell_xps13_9333,
299 .callback = dmi_matched,
300 .ident = "Dell XPS 13 9370",
301 .matches = {
302 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
303 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
305 .driver_data = &quirk_dell_xps13_9370,
308 .callback = dmi_matched,
309 .ident = "Dell Latitude E6410",
310 .matches = {
311 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
312 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
314 .driver_data = &quirk_dell_latitude_e6410,
319 static void dell_fill_request(struct calling_interface_buffer *buffer,
320 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
322 memset(buffer, 0, sizeof(struct calling_interface_buffer));
323 buffer->input[0] = arg0;
324 buffer->input[1] = arg1;
325 buffer->input[2] = arg2;
326 buffer->input[3] = arg3;
329 static int dell_send_request(struct calling_interface_buffer *buffer,
330 u16 class, u16 select)
332 int ret;
334 buffer->cmd_class = class;
335 buffer->cmd_select = select;
336 ret = dell_smbios_call(buffer);
337 if (ret != 0)
338 return ret;
339 return dell_smbios_error(buffer->output[0]);
343 * Derived from information in smbios-wireless-ctl:
345 * cbSelect 17, Value 11
347 * Return Wireless Info
348 * cbArg1, byte0 = 0x00
350 * cbRes1 Standard return codes (0, -1, -2)
351 * cbRes2 Info bit flags:
353 * 0 Hardware switch supported (1)
354 * 1 WiFi locator supported (1)
355 * 2 WLAN supported (1)
356 * 3 Bluetooth (BT) supported (1)
357 * 4 WWAN supported (1)
358 * 5 Wireless KBD supported (1)
359 * 6 Uw b supported (1)
360 * 7 WiGig supported (1)
361 * 8 WLAN installed (1)
362 * 9 BT installed (1)
363 * 10 WWAN installed (1)
364 * 11 Uw b installed (1)
365 * 12 WiGig installed (1)
366 * 13-15 Reserved (0)
367 * 16 Hardware (HW) switch is On (1)
368 * 17 WLAN disabled (1)
369 * 18 BT disabled (1)
370 * 19 WWAN disabled (1)
371 * 20 Uw b disabled (1)
372 * 21 WiGig disabled (1)
373 * 20-31 Reserved (0)
375 * cbRes3 NVRAM size in bytes
376 * cbRes4, byte 0 NVRAM format version number
379 * Set QuickSet Radio Disable Flag
380 * cbArg1, byte0 = 0x01
381 * cbArg1, byte1
382 * Radio ID value:
383 * 0 Radio Status
384 * 1 WLAN ID
385 * 2 BT ID
386 * 3 WWAN ID
387 * 4 UWB ID
388 * 5 WIGIG ID
389 * cbArg1, byte2 Flag bits:
390 * 0 QuickSet disables radio (1)
391 * 1-7 Reserved (0)
393 * cbRes1 Standard return codes (0, -1, -2)
394 * cbRes2 QuickSet (QS) radio disable bit map:
395 * 0 QS disables WLAN
396 * 1 QS disables BT
397 * 2 QS disables WWAN
398 * 3 QS disables UWB
399 * 4 QS disables WIGIG
400 * 5-31 Reserved (0)
402 * Wireless Switch Configuration
403 * cbArg1, byte0 = 0x02
405 * cbArg1, byte1
406 * Subcommand:
407 * 0 Get config
408 * 1 Set config
409 * 2 Set WiFi locator enable/disable
410 * cbArg1,byte2
411 * Switch settings (if byte 1==1):
412 * 0 WLAN sw itch control (1)
413 * 1 BT sw itch control (1)
414 * 2 WWAN sw itch control (1)
415 * 3 UWB sw itch control (1)
416 * 4 WiGig sw itch control (1)
417 * 5-7 Reserved (0)
418 * cbArg1, byte2 Enable bits (if byte 1==2):
419 * 0 Enable WiFi locator (1)
421 * cbRes1 Standard return codes (0, -1, -2)
422 * cbRes2 QuickSet radio disable bit map:
423 * 0 WLAN controlled by sw itch (1)
424 * 1 BT controlled by sw itch (1)
425 * 2 WWAN controlled by sw itch (1)
426 * 3 UWB controlled by sw itch (1)
427 * 4 WiGig controlled by sw itch (1)
428 * 5-6 Reserved (0)
429 * 7 Wireless sw itch config locked (1)
430 * 8 WiFi locator enabled (1)
431 * 9-14 Reserved (0)
432 * 15 WiFi locator setting locked (1)
433 * 16-31 Reserved (0)
435 * Read Local Config Data (LCD)
436 * cbArg1, byte0 = 0x10
437 * cbArg1, byte1 NVRAM index low byte
438 * cbArg1, byte2 NVRAM index high byte
439 * cbRes1 Standard return codes (0, -1, -2)
440 * cbRes2 4 bytes read from LCD[index]
441 * cbRes3 4 bytes read from LCD[index+4]
442 * cbRes4 4 bytes read from LCD[index+8]
444 * Write Local Config Data (LCD)
445 * cbArg1, byte0 = 0x11
446 * cbArg1, byte1 NVRAM index low byte
447 * cbArg1, byte2 NVRAM index high byte
448 * cbArg2 4 bytes to w rite at LCD[index]
449 * cbArg3 4 bytes to w rite at LCD[index+4]
450 * cbArg4 4 bytes to w rite at LCD[index+8]
451 * cbRes1 Standard return codes (0, -1, -2)
453 * Populate Local Config Data from NVRAM
454 * cbArg1, byte0 = 0x12
455 * cbRes1 Standard return codes (0, -1, -2)
457 * Commit Local Config Data to NVRAM
458 * cbArg1, byte0 = 0x13
459 * cbRes1 Standard return codes (0, -1, -2)
462 static int dell_rfkill_set(void *data, bool blocked)
464 int disable = blocked ? 1 : 0;
465 unsigned long radio = (unsigned long)data;
466 int hwswitch_bit = (unsigned long)data - 1;
467 struct calling_interface_buffer buffer;
468 int hwswitch;
469 int status;
470 int ret;
472 dell_fill_request(&buffer, 0, 0, 0, 0);
473 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
474 if (ret)
475 return ret;
476 status = buffer.output[1];
478 dell_fill_request(&buffer, 0x2, 0, 0, 0);
479 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
480 if (ret)
481 return ret;
482 hwswitch = buffer.output[1];
484 /* If the hardware switch controls this radio, and the hardware
485 switch is disabled, always disable the radio */
486 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
487 (status & BIT(0)) && !(status & BIT(16)))
488 disable = 1;
490 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
491 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
492 return ret;
495 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
496 int status)
498 if (status & BIT(0)) {
499 /* Has hw-switch, sync sw_state to BIOS */
500 struct calling_interface_buffer buffer;
501 int block = rfkill_blocked(rfkill);
502 dell_fill_request(&buffer,
503 1 | (radio << 8) | (block << 16), 0, 0, 0);
504 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
505 } else {
506 /* No hw-switch, sync BIOS state to sw_state */
507 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
511 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
512 int status, int hwswitch)
514 if (hwswitch & (BIT(radio - 1)))
515 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
518 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
520 int radio = ((unsigned long)data & 0xF);
521 struct calling_interface_buffer buffer;
522 int hwswitch;
523 int status;
524 int ret;
526 dell_fill_request(&buffer, 0, 0, 0, 0);
527 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
528 status = buffer.output[1];
530 if (ret != 0 || !(status & BIT(0))) {
531 return;
534 dell_fill_request(&buffer, 0x2, 0, 0, 0);
535 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
536 hwswitch = buffer.output[1];
538 if (ret != 0)
539 return;
541 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
544 static const struct rfkill_ops dell_rfkill_ops = {
545 .set_block = dell_rfkill_set,
546 .query = dell_rfkill_query,
549 static struct dentry *dell_laptop_dir;
551 static int dell_debugfs_show(struct seq_file *s, void *data)
553 struct calling_interface_buffer buffer;
554 int hwswitch_state;
555 int hwswitch_ret;
556 int status;
557 int ret;
559 dell_fill_request(&buffer, 0, 0, 0, 0);
560 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
561 if (ret)
562 return ret;
563 status = buffer.output[1];
565 dell_fill_request(&buffer, 0x2, 0, 0, 0);
566 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
567 if (hwswitch_ret)
568 return hwswitch_ret;
569 hwswitch_state = buffer.output[1];
571 seq_printf(s, "return:\t%d\n", ret);
572 seq_printf(s, "status:\t0x%X\n", status);
573 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
574 status & BIT(0));
575 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
576 (status & BIT(1)) >> 1);
577 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
578 (status & BIT(2)) >> 2);
579 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
580 (status & BIT(3)) >> 3);
581 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
582 (status & BIT(4)) >> 4);
583 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
584 (status & BIT(5)) >> 5);
585 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
586 (status & BIT(6)) >> 6);
587 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
588 (status & BIT(7)) >> 7);
589 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
590 (status & BIT(8)) >> 8);
591 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
592 (status & BIT(9)) >> 9);
593 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
594 (status & BIT(10)) >> 10);
595 seq_printf(s, "Bit 11: UWB installed: %lu\n",
596 (status & BIT(11)) >> 11);
597 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
598 (status & BIT(12)) >> 12);
600 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
601 (status & BIT(16)) >> 16);
602 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
603 (status & BIT(17)) >> 17);
604 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
605 (status & BIT(18)) >> 18);
606 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
607 (status & BIT(19)) >> 19);
608 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
609 (status & BIT(20)) >> 20);
610 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
611 (status & BIT(21)) >> 21);
613 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
614 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
615 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
616 hwswitch_state & BIT(0));
617 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
618 (hwswitch_state & BIT(1)) >> 1);
619 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
620 (hwswitch_state & BIT(2)) >> 2);
621 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
622 (hwswitch_state & BIT(3)) >> 3);
623 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
624 (hwswitch_state & BIT(4)) >> 4);
625 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
626 (hwswitch_state & BIT(7)) >> 7);
627 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
628 (hwswitch_state & BIT(8)) >> 8);
629 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
630 (hwswitch_state & BIT(15)) >> 15);
632 return 0;
634 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
636 static void dell_update_rfkill(struct work_struct *ignored)
638 struct calling_interface_buffer buffer;
639 int hwswitch = 0;
640 int status;
641 int ret;
643 dell_fill_request(&buffer, 0, 0, 0, 0);
644 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
645 status = buffer.output[1];
647 if (ret != 0)
648 return;
650 dell_fill_request(&buffer, 0x2, 0, 0, 0);
651 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
653 if (ret == 0 && (status & BIT(0)))
654 hwswitch = buffer.output[1];
656 if (wifi_rfkill) {
657 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
658 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
660 if (bluetooth_rfkill) {
661 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
662 hwswitch);
663 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
665 if (wwan_rfkill) {
666 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
667 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
670 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
672 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
673 struct serio *port)
675 static bool extended;
677 if (str & I8042_STR_AUXDATA)
678 return false;
680 if (unlikely(data == 0xe0)) {
681 extended = true;
682 return false;
683 } else if (unlikely(extended)) {
684 switch (data) {
685 case 0x8:
686 schedule_delayed_work(&dell_rfkill_work,
687 round_jiffies_relative(HZ / 4));
688 break;
690 extended = false;
693 return false;
696 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
697 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
699 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
700 unsigned long action, void *data)
702 schedule_delayed_work(&dell_rfkill_work, 0);
703 return NOTIFY_OK;
706 static struct notifier_block dell_laptop_rbtn_notifier = {
707 .notifier_call = dell_laptop_rbtn_notifier_call,
710 static int __init dell_setup_rfkill(void)
712 struct calling_interface_buffer buffer;
713 int status, ret, whitelisted;
714 const char *product;
717 * rfkill support causes trouble on various models, mostly Inspirons.
718 * So we whitelist certain series, and don't support rfkill on others.
720 whitelisted = 0;
721 product = dmi_get_system_info(DMI_PRODUCT_NAME);
722 if (product && (strncmp(product, "Latitude", 8) == 0 ||
723 strncmp(product, "Precision", 9) == 0))
724 whitelisted = 1;
725 if (!force_rfkill && !whitelisted)
726 return 0;
728 dell_fill_request(&buffer, 0, 0, 0, 0);
729 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
730 status = buffer.output[1];
732 /* dell wireless info smbios call is not supported */
733 if (ret != 0)
734 return 0;
736 /* rfkill is only tested on laptops with a hwswitch */
737 if (!(status & BIT(0)) && !force_rfkill)
738 return 0;
740 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
741 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
742 RFKILL_TYPE_WLAN,
743 &dell_rfkill_ops, (void *) 1);
744 if (!wifi_rfkill) {
745 ret = -ENOMEM;
746 goto err_wifi;
748 ret = rfkill_register(wifi_rfkill);
749 if (ret)
750 goto err_wifi;
753 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
754 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
755 &platform_device->dev,
756 RFKILL_TYPE_BLUETOOTH,
757 &dell_rfkill_ops, (void *) 2);
758 if (!bluetooth_rfkill) {
759 ret = -ENOMEM;
760 goto err_bluetooth;
762 ret = rfkill_register(bluetooth_rfkill);
763 if (ret)
764 goto err_bluetooth;
767 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
768 wwan_rfkill = rfkill_alloc("dell-wwan",
769 &platform_device->dev,
770 RFKILL_TYPE_WWAN,
771 &dell_rfkill_ops, (void *) 3);
772 if (!wwan_rfkill) {
773 ret = -ENOMEM;
774 goto err_wwan;
776 ret = rfkill_register(wwan_rfkill);
777 if (ret)
778 goto err_wwan;
782 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
783 * which can receive events from HW slider switch.
785 * Dell SMBIOS on whitelisted models supports controlling radio devices
786 * but does not support receiving HW button switch events. We can use
787 * i8042 filter hook function to receive keyboard data and handle
788 * keycode for HW button.
790 * So if it is possible we will use Dell Airplane Mode Switch ACPI
791 * driver for receiving HW events and Dell SMBIOS for setting rfkill
792 * states. If ACPI driver or device is not available we will fallback to
793 * i8042 filter hook function.
795 * To prevent duplicate rfkill devices which control and do same thing,
796 * dell-rbtn driver will automatically remove its own rfkill devices
797 * once function dell_rbtn_notifier_register() is called.
800 dell_rbtn_notifier_register_func =
801 symbol_request(dell_rbtn_notifier_register);
802 if (dell_rbtn_notifier_register_func) {
803 dell_rbtn_notifier_unregister_func =
804 symbol_request(dell_rbtn_notifier_unregister);
805 if (!dell_rbtn_notifier_unregister_func) {
806 symbol_put(dell_rbtn_notifier_register);
807 dell_rbtn_notifier_register_func = NULL;
811 if (dell_rbtn_notifier_register_func) {
812 ret = dell_rbtn_notifier_register_func(
813 &dell_laptop_rbtn_notifier);
814 symbol_put(dell_rbtn_notifier_register);
815 dell_rbtn_notifier_register_func = NULL;
816 if (ret != 0) {
817 symbol_put(dell_rbtn_notifier_unregister);
818 dell_rbtn_notifier_unregister_func = NULL;
820 } else {
821 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
822 ret = -ENODEV;
825 if (ret == 0) {
826 pr_info("Using dell-rbtn acpi driver for receiving events\n");
827 } else if (ret != -ENODEV) {
828 pr_warn("Unable to register dell rbtn notifier\n");
829 goto err_filter;
830 } else {
831 ret = i8042_install_filter(dell_laptop_i8042_filter);
832 if (ret) {
833 pr_warn("Unable to install key filter\n");
834 goto err_filter;
836 pr_info("Using i8042 filter function for receiving events\n");
839 return 0;
840 err_filter:
841 if (wwan_rfkill)
842 rfkill_unregister(wwan_rfkill);
843 err_wwan:
844 rfkill_destroy(wwan_rfkill);
845 if (bluetooth_rfkill)
846 rfkill_unregister(bluetooth_rfkill);
847 err_bluetooth:
848 rfkill_destroy(bluetooth_rfkill);
849 if (wifi_rfkill)
850 rfkill_unregister(wifi_rfkill);
851 err_wifi:
852 rfkill_destroy(wifi_rfkill);
854 return ret;
857 static void dell_cleanup_rfkill(void)
859 if (dell_rbtn_notifier_unregister_func) {
860 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
861 symbol_put(dell_rbtn_notifier_unregister);
862 dell_rbtn_notifier_unregister_func = NULL;
863 } else {
864 i8042_remove_filter(dell_laptop_i8042_filter);
866 cancel_delayed_work_sync(&dell_rfkill_work);
867 if (wifi_rfkill) {
868 rfkill_unregister(wifi_rfkill);
869 rfkill_destroy(wifi_rfkill);
871 if (bluetooth_rfkill) {
872 rfkill_unregister(bluetooth_rfkill);
873 rfkill_destroy(bluetooth_rfkill);
875 if (wwan_rfkill) {
876 rfkill_unregister(wwan_rfkill);
877 rfkill_destroy(wwan_rfkill);
881 static int dell_send_intensity(struct backlight_device *bd)
883 struct calling_interface_buffer buffer;
884 struct calling_interface_token *token;
885 int ret;
887 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
888 if (!token)
889 return -ENODEV;
891 dell_fill_request(&buffer,
892 token->location, bd->props.brightness, 0, 0);
893 if (power_supply_is_system_supplied() > 0)
894 ret = dell_send_request(&buffer,
895 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
896 else
897 ret = dell_send_request(&buffer,
898 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
900 return ret;
903 static int dell_get_intensity(struct backlight_device *bd)
905 struct calling_interface_buffer buffer;
906 struct calling_interface_token *token;
907 int ret;
909 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
910 if (!token)
911 return -ENODEV;
913 dell_fill_request(&buffer, token->location, 0, 0, 0);
914 if (power_supply_is_system_supplied() > 0)
915 ret = dell_send_request(&buffer,
916 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
917 else
918 ret = dell_send_request(&buffer,
919 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
921 if (ret == 0)
922 ret = buffer.output[1];
924 return ret;
927 static const struct backlight_ops dell_ops = {
928 .get_brightness = dell_get_intensity,
929 .update_status = dell_send_intensity,
932 static void touchpad_led_on(void)
934 int command = 0x97;
935 char data = 1;
936 i8042_command(&data, command | 1 << 12);
939 static void touchpad_led_off(void)
941 int command = 0x97;
942 char data = 2;
943 i8042_command(&data, command | 1 << 12);
946 static void touchpad_led_set(struct led_classdev *led_cdev,
947 enum led_brightness value)
949 if (value > 0)
950 touchpad_led_on();
951 else
952 touchpad_led_off();
955 static struct led_classdev touchpad_led = {
956 .name = "dell-laptop::touchpad",
957 .brightness_set = touchpad_led_set,
958 .flags = LED_CORE_SUSPENDRESUME,
961 static int __init touchpad_led_init(struct device *dev)
963 return led_classdev_register(dev, &touchpad_led);
966 static void touchpad_led_exit(void)
968 led_classdev_unregister(&touchpad_led);
972 * Derived from information in smbios-keyboard-ctl:
974 * cbClass 4
975 * cbSelect 11
976 * Keyboard illumination
977 * cbArg1 determines the function to be performed
979 * cbArg1 0x0 = Get Feature Information
980 * cbRES1 Standard return codes (0, -1, -2)
981 * cbRES2, word0 Bitmap of user-selectable modes
982 * bit 0 Always off (All systems)
983 * bit 1 Always on (Travis ATG, Siberia)
984 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
985 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
986 * bit 4 Auto: Input-activity-based On; input-activity based Off
987 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
988 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
989 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
990 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
991 * bits 9-15 Reserved for future use
992 * cbRES2, byte2 Reserved for future use
993 * cbRES2, byte3 Keyboard illumination type
994 * 0 Reserved
995 * 1 Tasklight
996 * 2 Backlight
997 * 3-255 Reserved for future use
998 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
999 * bit 0 Any keystroke
1000 * bit 1 Touchpad activity
1001 * bit 2 Pointing stick
1002 * bit 3 Any mouse
1003 * bits 4-7 Reserved for future use
1004 * cbRES3, byte1 Supported timeout unit bitmap
1005 * bit 0 Seconds
1006 * bit 1 Minutes
1007 * bit 2 Hours
1008 * bit 3 Days
1009 * bits 4-7 Reserved for future use
1010 * cbRES3, byte2 Number of keyboard light brightness levels
1011 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1012 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1013 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1014 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1016 * cbArg1 0x1 = Get Current State
1017 * cbRES1 Standard return codes (0, -1, -2)
1018 * cbRES2, word0 Bitmap of current mode state
1019 * bit 0 Always off (All systems)
1020 * bit 1 Always on (Travis ATG, Siberia)
1021 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1022 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1023 * bit 4 Auto: Input-activity-based On; input-activity based Off
1024 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1025 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1026 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1027 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1028 * bits 9-15 Reserved for future use
1029 * Note: Only One bit can be set
1030 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1031 * bit 0 Any keystroke
1032 * bit 1 Touchpad activity
1033 * bit 2 Pointing stick
1034 * bit 3 Any mouse
1035 * bits 4-7 Reserved for future use
1036 * cbRES2, byte3 Current Timeout on battery
1037 * bits 7:6 Timeout units indicator:
1038 * 00b Seconds
1039 * 01b Minutes
1040 * 10b Hours
1041 * 11b Days
1042 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1043 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1044 * are set upon return from the [Get feature information] call.
1045 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1046 * cbRES3, byte1 Current ALS reading
1047 * cbRES3, byte2 Current keyboard light level.
1048 * cbRES3, byte3 Current timeout on AC Power
1049 * bits 7:6 Timeout units indicator:
1050 * 00b Seconds
1051 * 01b Minutes
1052 * 10b Hours
1053 * 11b Days
1054 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1055 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1056 * are set upon return from the upon return from the [Get Feature information] call.
1058 * cbArg1 0x2 = Set New State
1059 * cbRES1 Standard return codes (0, -1, -2)
1060 * cbArg2, word0 Bitmap of current mode state
1061 * bit 0 Always off (All systems)
1062 * bit 1 Always on (Travis ATG, Siberia)
1063 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1064 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1065 * bit 4 Auto: Input-activity-based On; input-activity based Off
1066 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1067 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1068 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1069 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1070 * bits 9-15 Reserved for future use
1071 * Note: Only One bit can be set
1072 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1073 * keyboard to turn off automatically.
1074 * bit 0 Any keystroke
1075 * bit 1 Touchpad activity
1076 * bit 2 Pointing stick
1077 * bit 3 Any mouse
1078 * bits 4-7 Reserved for future use
1079 * cbArg2, byte3 Desired Timeout on battery
1080 * bits 7:6 Timeout units indicator:
1081 * 00b Seconds
1082 * 01b Minutes
1083 * 10b Hours
1084 * 11b Days
1085 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1086 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1087 * cbArg3, byte2 Desired keyboard light level.
1088 * cbArg3, byte3 Desired Timeout on AC power
1089 * bits 7:6 Timeout units indicator:
1090 * 00b Seconds
1091 * 01b Minutes
1092 * 10b Hours
1093 * 11b Days
1094 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1098 enum kbd_timeout_unit {
1099 KBD_TIMEOUT_SECONDS = 0,
1100 KBD_TIMEOUT_MINUTES,
1101 KBD_TIMEOUT_HOURS,
1102 KBD_TIMEOUT_DAYS,
1105 enum kbd_mode_bit {
1106 KBD_MODE_BIT_OFF = 0,
1107 KBD_MODE_BIT_ON,
1108 KBD_MODE_BIT_ALS,
1109 KBD_MODE_BIT_TRIGGER_ALS,
1110 KBD_MODE_BIT_TRIGGER,
1111 KBD_MODE_BIT_TRIGGER_25,
1112 KBD_MODE_BIT_TRIGGER_50,
1113 KBD_MODE_BIT_TRIGGER_75,
1114 KBD_MODE_BIT_TRIGGER_100,
1117 #define kbd_is_als_mode_bit(bit) \
1118 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1119 #define kbd_is_trigger_mode_bit(bit) \
1120 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1121 #define kbd_is_level_mode_bit(bit) \
1122 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1124 struct kbd_info {
1125 u16 modes;
1126 u8 type;
1127 u8 triggers;
1128 u8 levels;
1129 u8 seconds;
1130 u8 minutes;
1131 u8 hours;
1132 u8 days;
1135 struct kbd_state {
1136 u8 mode_bit;
1137 u8 triggers;
1138 u8 timeout_value;
1139 u8 timeout_unit;
1140 u8 timeout_value_ac;
1141 u8 timeout_unit_ac;
1142 u8 als_setting;
1143 u8 als_value;
1144 u8 level;
1147 static const int kbd_tokens[] = {
1148 KBD_LED_OFF_TOKEN,
1149 KBD_LED_AUTO_25_TOKEN,
1150 KBD_LED_AUTO_50_TOKEN,
1151 KBD_LED_AUTO_75_TOKEN,
1152 KBD_LED_AUTO_100_TOKEN,
1153 KBD_LED_ON_TOKEN,
1156 static u16 kbd_token_bits;
1158 static struct kbd_info kbd_info;
1159 static bool kbd_als_supported;
1160 static bool kbd_triggers_supported;
1161 static bool kbd_timeout_ac_supported;
1163 static u8 kbd_mode_levels[16];
1164 static int kbd_mode_levels_count;
1166 static u8 kbd_previous_level;
1167 static u8 kbd_previous_mode_bit;
1169 static bool kbd_led_present;
1170 static DEFINE_MUTEX(kbd_led_mutex);
1171 static enum led_brightness kbd_led_level;
1174 * NOTE: there are three ways to set the keyboard backlight level.
1175 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1176 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1177 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1179 * There are laptops which support only one of these methods. If we want to
1180 * support as many machines as possible we need to implement all three methods.
1181 * The first two methods use the kbd_state structure. The third uses SMBIOS
1182 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1183 * keyboard backlight level via kbd_state.level.
1186 static int kbd_get_info(struct kbd_info *info)
1188 struct calling_interface_buffer buffer;
1189 u8 units;
1190 int ret;
1192 dell_fill_request(&buffer, 0, 0, 0, 0);
1193 ret = dell_send_request(&buffer,
1194 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1195 if (ret)
1196 return ret;
1198 info->modes = buffer.output[1] & 0xFFFF;
1199 info->type = (buffer.output[1] >> 24) & 0xFF;
1200 info->triggers = buffer.output[2] & 0xFF;
1201 units = (buffer.output[2] >> 8) & 0xFF;
1202 info->levels = (buffer.output[2] >> 16) & 0xFF;
1204 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1205 info->levels--;
1207 if (units & BIT(0))
1208 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1209 if (units & BIT(1))
1210 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1211 if (units & BIT(2))
1212 info->hours = (buffer.output[3] >> 16) & 0xFF;
1213 if (units & BIT(3))
1214 info->days = (buffer.output[3] >> 24) & 0xFF;
1216 return ret;
1219 static unsigned int kbd_get_max_level(void)
1221 if (kbd_info.levels != 0)
1222 return kbd_info.levels;
1223 if (kbd_mode_levels_count > 0)
1224 return kbd_mode_levels_count - 1;
1225 return 0;
1228 static int kbd_get_level(struct kbd_state *state)
1230 int i;
1232 if (kbd_info.levels != 0)
1233 return state->level;
1235 if (kbd_mode_levels_count > 0) {
1236 for (i = 0; i < kbd_mode_levels_count; ++i)
1237 if (kbd_mode_levels[i] == state->mode_bit)
1238 return i;
1239 return 0;
1242 return -EINVAL;
1245 static int kbd_set_level(struct kbd_state *state, u8 level)
1247 if (kbd_info.levels != 0) {
1248 if (level != 0)
1249 kbd_previous_level = level;
1250 if (state->level == level)
1251 return 0;
1252 state->level = level;
1253 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1254 state->mode_bit = kbd_previous_mode_bit;
1255 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1256 kbd_previous_mode_bit = state->mode_bit;
1257 state->mode_bit = KBD_MODE_BIT_OFF;
1259 return 0;
1262 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1263 if (level != 0)
1264 kbd_previous_level = level;
1265 state->mode_bit = kbd_mode_levels[level];
1266 return 0;
1269 return -EINVAL;
1272 static int kbd_get_state(struct kbd_state *state)
1274 struct calling_interface_buffer buffer;
1275 int ret;
1277 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1278 ret = dell_send_request(&buffer,
1279 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1280 if (ret)
1281 return ret;
1283 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1284 if (state->mode_bit != 0)
1285 state->mode_bit--;
1287 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1288 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1289 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1290 state->als_setting = buffer.output[2] & 0xFF;
1291 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1292 state->level = (buffer.output[2] >> 16) & 0xFF;
1293 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1294 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1296 return ret;
1299 static int kbd_set_state(struct kbd_state *state)
1301 struct calling_interface_buffer buffer;
1302 int ret;
1303 u32 input1;
1304 u32 input2;
1306 input1 = BIT(state->mode_bit) & 0xFFFF;
1307 input1 |= (state->triggers & 0xFF) << 16;
1308 input1 |= (state->timeout_value & 0x3F) << 24;
1309 input1 |= (state->timeout_unit & 0x3) << 30;
1310 input2 = state->als_setting & 0xFF;
1311 input2 |= (state->level & 0xFF) << 16;
1312 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1313 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1314 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1315 ret = dell_send_request(&buffer,
1316 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1318 return ret;
1321 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1323 int ret;
1325 ret = kbd_set_state(state);
1326 if (ret == 0)
1327 return 0;
1330 * When setting the new state fails,try to restore the previous one.
1331 * This is needed on some machines where BIOS sets a default state when
1332 * setting a new state fails. This default state could be all off.
1335 if (kbd_set_state(old))
1336 pr_err("Setting old previous keyboard state failed\n");
1338 return ret;
1341 static int kbd_set_token_bit(u8 bit)
1343 struct calling_interface_buffer buffer;
1344 struct calling_interface_token *token;
1345 int ret;
1347 if (bit >= ARRAY_SIZE(kbd_tokens))
1348 return -EINVAL;
1350 token = dell_smbios_find_token(kbd_tokens[bit]);
1351 if (!token)
1352 return -EINVAL;
1354 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1355 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1357 return ret;
1360 static int kbd_get_token_bit(u8 bit)
1362 struct calling_interface_buffer buffer;
1363 struct calling_interface_token *token;
1364 int ret;
1365 int val;
1367 if (bit >= ARRAY_SIZE(kbd_tokens))
1368 return -EINVAL;
1370 token = dell_smbios_find_token(kbd_tokens[bit]);
1371 if (!token)
1372 return -EINVAL;
1374 dell_fill_request(&buffer, token->location, 0, 0, 0);
1375 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1376 val = buffer.output[1];
1378 if (ret)
1379 return ret;
1381 return (val == token->value);
1384 static int kbd_get_first_active_token_bit(void)
1386 int i;
1387 int ret;
1389 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1390 ret = kbd_get_token_bit(i);
1391 if (ret == 1)
1392 return i;
1395 return ret;
1398 static int kbd_get_valid_token_counts(void)
1400 return hweight16(kbd_token_bits);
1403 static inline int kbd_init_info(void)
1405 struct kbd_state state;
1406 int ret;
1407 int i;
1409 ret = kbd_get_info(&kbd_info);
1410 if (ret)
1411 return ret;
1413 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1414 * timeout value which is shared for both battery and AC power
1415 * settings. So do not try to set AC values on old models.
1417 if ((quirks && quirks->kbd_missing_ac_tag) ||
1418 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1419 kbd_timeout_ac_supported = true;
1421 kbd_get_state(&state);
1423 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1424 if (kbd_info.seconds > 63)
1425 kbd_info.seconds = 63;
1426 if (kbd_info.minutes > 63)
1427 kbd_info.minutes = 63;
1428 if (kbd_info.hours > 63)
1429 kbd_info.hours = 63;
1430 if (kbd_info.days > 63)
1431 kbd_info.days = 63;
1433 /* NOTE: On tested machines ON mode did not work and caused
1434 * problems (turned backlight off) so do not use it
1436 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1438 kbd_previous_level = kbd_get_level(&state);
1439 kbd_previous_mode_bit = state.mode_bit;
1441 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1442 kbd_previous_level = 1;
1444 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1445 kbd_previous_mode_bit =
1446 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1447 if (kbd_previous_mode_bit != 0)
1448 kbd_previous_mode_bit--;
1451 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1452 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1453 kbd_als_supported = true;
1455 if (kbd_info.modes & (
1456 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1457 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1458 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1460 kbd_triggers_supported = true;
1462 /* kbd_mode_levels[0] is reserved, see below */
1463 for (i = 0; i < 16; ++i)
1464 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1465 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1468 * Find the first supported mode and assign to kbd_mode_levels[0].
1469 * This should be 0 (off), but we cannot depend on the BIOS to
1470 * support 0.
1472 if (kbd_mode_levels_count > 0) {
1473 for (i = 0; i < 16; ++i) {
1474 if (BIT(i) & kbd_info.modes) {
1475 kbd_mode_levels[0] = i;
1476 break;
1479 kbd_mode_levels_count++;
1482 return 0;
1486 static inline void kbd_init_tokens(void)
1488 int i;
1490 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1491 if (dell_smbios_find_token(kbd_tokens[i]))
1492 kbd_token_bits |= BIT(i);
1495 static void kbd_init(void)
1497 int ret;
1499 ret = kbd_init_info();
1500 kbd_init_tokens();
1503 * Only supports keyboard backlight when it has at least two modes.
1505 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1506 || kbd_get_valid_token_counts() >= 2)
1507 kbd_led_present = true;
1510 static ssize_t kbd_led_timeout_store(struct device *dev,
1511 struct device_attribute *attr,
1512 const char *buf, size_t count)
1514 struct kbd_state new_state;
1515 struct kbd_state state;
1516 bool convert;
1517 int value;
1518 int ret;
1519 char ch;
1520 u8 unit;
1521 int i;
1523 ret = sscanf(buf, "%d %c", &value, &ch);
1524 if (ret < 1)
1525 return -EINVAL;
1526 else if (ret == 1)
1527 ch = 's';
1529 if (value < 0)
1530 return -EINVAL;
1532 convert = false;
1534 switch (ch) {
1535 case 's':
1536 if (value > kbd_info.seconds)
1537 convert = true;
1538 unit = KBD_TIMEOUT_SECONDS;
1539 break;
1540 case 'm':
1541 if (value > kbd_info.minutes)
1542 convert = true;
1543 unit = KBD_TIMEOUT_MINUTES;
1544 break;
1545 case 'h':
1546 if (value > kbd_info.hours)
1547 convert = true;
1548 unit = KBD_TIMEOUT_HOURS;
1549 break;
1550 case 'd':
1551 if (value > kbd_info.days)
1552 convert = true;
1553 unit = KBD_TIMEOUT_DAYS;
1554 break;
1555 default:
1556 return -EINVAL;
1559 if (quirks && quirks->needs_kbd_timeouts)
1560 convert = true;
1562 if (convert) {
1563 /* Convert value from current units to seconds */
1564 switch (unit) {
1565 case KBD_TIMEOUT_DAYS:
1566 value *= 24;
1567 /* fall through */
1568 case KBD_TIMEOUT_HOURS:
1569 value *= 60;
1570 /* fall through */
1571 case KBD_TIMEOUT_MINUTES:
1572 value *= 60;
1573 unit = KBD_TIMEOUT_SECONDS;
1576 if (quirks && quirks->needs_kbd_timeouts) {
1577 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1578 if (value <= quirks->kbd_timeouts[i]) {
1579 value = quirks->kbd_timeouts[i];
1580 break;
1585 if (value <= kbd_info.seconds && kbd_info.seconds) {
1586 unit = KBD_TIMEOUT_SECONDS;
1587 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1588 value /= 60;
1589 unit = KBD_TIMEOUT_MINUTES;
1590 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1591 value /= (60 * 60);
1592 unit = KBD_TIMEOUT_HOURS;
1593 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1594 value /= (60 * 60 * 24);
1595 unit = KBD_TIMEOUT_DAYS;
1596 } else {
1597 return -EINVAL;
1601 mutex_lock(&kbd_led_mutex);
1603 ret = kbd_get_state(&state);
1604 if (ret)
1605 goto out;
1607 new_state = state;
1609 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1610 new_state.timeout_value_ac = value;
1611 new_state.timeout_unit_ac = unit;
1612 } else {
1613 new_state.timeout_value = value;
1614 new_state.timeout_unit = unit;
1617 ret = kbd_set_state_safe(&new_state, &state);
1618 if (ret)
1619 goto out;
1621 ret = count;
1622 out:
1623 mutex_unlock(&kbd_led_mutex);
1624 return ret;
1627 static ssize_t kbd_led_timeout_show(struct device *dev,
1628 struct device_attribute *attr, char *buf)
1630 struct kbd_state state;
1631 int value;
1632 int ret;
1633 int len;
1634 u8 unit;
1636 ret = kbd_get_state(&state);
1637 if (ret)
1638 return ret;
1640 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1641 value = state.timeout_value_ac;
1642 unit = state.timeout_unit_ac;
1643 } else {
1644 value = state.timeout_value;
1645 unit = state.timeout_unit;
1648 len = sprintf(buf, "%d", value);
1650 switch (unit) {
1651 case KBD_TIMEOUT_SECONDS:
1652 return len + sprintf(buf+len, "s\n");
1653 case KBD_TIMEOUT_MINUTES:
1654 return len + sprintf(buf+len, "m\n");
1655 case KBD_TIMEOUT_HOURS:
1656 return len + sprintf(buf+len, "h\n");
1657 case KBD_TIMEOUT_DAYS:
1658 return len + sprintf(buf+len, "d\n");
1659 default:
1660 return -EINVAL;
1663 return len;
1666 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1667 kbd_led_timeout_show, kbd_led_timeout_store);
1669 static const char * const kbd_led_triggers[] = {
1670 "keyboard",
1671 "touchpad",
1672 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1673 "mouse",
1676 static ssize_t kbd_led_triggers_store(struct device *dev,
1677 struct device_attribute *attr,
1678 const char *buf, size_t count)
1680 struct kbd_state new_state;
1681 struct kbd_state state;
1682 bool triggers_enabled = false;
1683 int trigger_bit = -1;
1684 char trigger[21];
1685 int i, ret;
1687 ret = sscanf(buf, "%20s", trigger);
1688 if (ret != 1)
1689 return -EINVAL;
1691 if (trigger[0] != '+' && trigger[0] != '-')
1692 return -EINVAL;
1694 mutex_lock(&kbd_led_mutex);
1696 ret = kbd_get_state(&state);
1697 if (ret)
1698 goto out;
1700 if (kbd_triggers_supported)
1701 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1703 if (kbd_triggers_supported) {
1704 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1705 if (!(kbd_info.triggers & BIT(i)))
1706 continue;
1707 if (!kbd_led_triggers[i])
1708 continue;
1709 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1710 continue;
1711 if (trigger[0] == '+' &&
1712 triggers_enabled && (state.triggers & BIT(i))) {
1713 ret = count;
1714 goto out;
1716 if (trigger[0] == '-' &&
1717 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1718 ret = count;
1719 goto out;
1721 trigger_bit = i;
1722 break;
1726 if (trigger_bit == -1) {
1727 ret = -EINVAL;
1728 goto out;
1731 new_state = state;
1732 if (trigger[0] == '+')
1733 new_state.triggers |= BIT(trigger_bit);
1734 else {
1735 new_state.triggers &= ~BIT(trigger_bit);
1737 * NOTE: trackstick bit (2) must be disabled when
1738 * disabling touchpad bit (1), otherwise touchpad
1739 * bit (1) will not be disabled
1741 if (trigger_bit == 1)
1742 new_state.triggers &= ~BIT(2);
1744 if ((kbd_info.triggers & new_state.triggers) !=
1745 new_state.triggers) {
1746 ret = -EINVAL;
1747 goto out;
1749 if (new_state.triggers && !triggers_enabled) {
1750 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1751 kbd_set_level(&new_state, kbd_previous_level);
1752 } else if (new_state.triggers == 0) {
1753 kbd_set_level(&new_state, 0);
1755 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1756 ret = -EINVAL;
1757 goto out;
1759 ret = kbd_set_state_safe(&new_state, &state);
1760 if (ret)
1761 goto out;
1762 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1763 kbd_previous_mode_bit = new_state.mode_bit;
1764 ret = count;
1765 out:
1766 mutex_unlock(&kbd_led_mutex);
1767 return ret;
1770 static ssize_t kbd_led_triggers_show(struct device *dev,
1771 struct device_attribute *attr, char *buf)
1773 struct kbd_state state;
1774 bool triggers_enabled;
1775 int level, i, ret;
1776 int len = 0;
1778 ret = kbd_get_state(&state);
1779 if (ret)
1780 return ret;
1782 len = 0;
1784 if (kbd_triggers_supported) {
1785 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1786 level = kbd_get_level(&state);
1787 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1788 if (!(kbd_info.triggers & BIT(i)))
1789 continue;
1790 if (!kbd_led_triggers[i])
1791 continue;
1792 if ((triggers_enabled || level <= 0) &&
1793 (state.triggers & BIT(i)))
1794 buf[len++] = '+';
1795 else
1796 buf[len++] = '-';
1797 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1801 if (len)
1802 buf[len - 1] = '\n';
1804 return len;
1807 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1808 kbd_led_triggers_show, kbd_led_triggers_store);
1810 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1811 struct device_attribute *attr,
1812 const char *buf, size_t count)
1814 struct kbd_state new_state;
1815 struct kbd_state state;
1816 bool triggers_enabled = false;
1817 int enable;
1818 int ret;
1820 ret = kstrtoint(buf, 0, &enable);
1821 if (ret)
1822 return ret;
1824 mutex_lock(&kbd_led_mutex);
1826 ret = kbd_get_state(&state);
1827 if (ret)
1828 goto out;
1830 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1831 ret = count;
1832 goto out;
1835 new_state = state;
1837 if (kbd_triggers_supported)
1838 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1840 if (enable) {
1841 if (triggers_enabled)
1842 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1843 else
1844 new_state.mode_bit = KBD_MODE_BIT_ALS;
1845 } else {
1846 if (triggers_enabled) {
1847 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1848 kbd_set_level(&new_state, kbd_previous_level);
1849 } else {
1850 new_state.mode_bit = KBD_MODE_BIT_ON;
1853 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1854 ret = -EINVAL;
1855 goto out;
1858 ret = kbd_set_state_safe(&new_state, &state);
1859 if (ret)
1860 goto out;
1861 kbd_previous_mode_bit = new_state.mode_bit;
1863 ret = count;
1864 out:
1865 mutex_unlock(&kbd_led_mutex);
1866 return ret;
1869 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1870 struct device_attribute *attr,
1871 char *buf)
1873 struct kbd_state state;
1874 bool enabled = false;
1875 int ret;
1877 ret = kbd_get_state(&state);
1878 if (ret)
1879 return ret;
1880 enabled = kbd_is_als_mode_bit(state.mode_bit);
1882 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1885 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1886 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1888 static ssize_t kbd_led_als_setting_store(struct device *dev,
1889 struct device_attribute *attr,
1890 const char *buf, size_t count)
1892 struct kbd_state state;
1893 struct kbd_state new_state;
1894 u8 setting;
1895 int ret;
1897 ret = kstrtou8(buf, 10, &setting);
1898 if (ret)
1899 return ret;
1901 mutex_lock(&kbd_led_mutex);
1903 ret = kbd_get_state(&state);
1904 if (ret)
1905 goto out;
1907 new_state = state;
1908 new_state.als_setting = setting;
1910 ret = kbd_set_state_safe(&new_state, &state);
1911 if (ret)
1912 goto out;
1914 ret = count;
1915 out:
1916 mutex_unlock(&kbd_led_mutex);
1917 return ret;
1920 static ssize_t kbd_led_als_setting_show(struct device *dev,
1921 struct device_attribute *attr,
1922 char *buf)
1924 struct kbd_state state;
1925 int ret;
1927 ret = kbd_get_state(&state);
1928 if (ret)
1929 return ret;
1931 return sprintf(buf, "%d\n", state.als_setting);
1934 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1935 kbd_led_als_setting_show, kbd_led_als_setting_store);
1937 static struct attribute *kbd_led_attrs[] = {
1938 &dev_attr_stop_timeout.attr,
1939 &dev_attr_start_triggers.attr,
1940 NULL,
1943 static const struct attribute_group kbd_led_group = {
1944 .attrs = kbd_led_attrs,
1947 static struct attribute *kbd_led_als_attrs[] = {
1948 &dev_attr_als_enabled.attr,
1949 &dev_attr_als_setting.attr,
1950 NULL,
1953 static const struct attribute_group kbd_led_als_group = {
1954 .attrs = kbd_led_als_attrs,
1957 static const struct attribute_group *kbd_led_groups[] = {
1958 &kbd_led_group,
1959 &kbd_led_als_group,
1960 NULL,
1963 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1965 int ret;
1966 u16 num;
1967 struct kbd_state state;
1969 if (kbd_get_max_level()) {
1970 ret = kbd_get_state(&state);
1971 if (ret)
1972 return 0;
1973 ret = kbd_get_level(&state);
1974 if (ret < 0)
1975 return 0;
1976 return ret;
1979 if (kbd_get_valid_token_counts()) {
1980 ret = kbd_get_first_active_token_bit();
1981 if (ret < 0)
1982 return 0;
1983 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1984 num &= num - 1; /* clear the first bit set */
1985 if (num == 0)
1986 return 0;
1987 return ffs(num) - 1;
1990 pr_warn("Keyboard brightness level control not supported\n");
1991 return 0;
1994 static int kbd_led_level_set(struct led_classdev *led_cdev,
1995 enum led_brightness value)
1997 enum led_brightness new_value = value;
1998 struct kbd_state state;
1999 struct kbd_state new_state;
2000 u16 num;
2001 int ret;
2003 mutex_lock(&kbd_led_mutex);
2005 if (kbd_get_max_level()) {
2006 ret = kbd_get_state(&state);
2007 if (ret)
2008 goto out;
2009 new_state = state;
2010 ret = kbd_set_level(&new_state, value);
2011 if (ret)
2012 goto out;
2013 ret = kbd_set_state_safe(&new_state, &state);
2014 } else if (kbd_get_valid_token_counts()) {
2015 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2016 num &= num - 1; /* clear the first bit set */
2017 if (num == 0)
2018 ret = 0;
2019 else
2020 ret = kbd_set_token_bit(ffs(num) - 1);
2021 } else {
2022 pr_warn("Keyboard brightness level control not supported\n");
2023 ret = -ENXIO;
2026 out:
2027 if (ret == 0)
2028 kbd_led_level = new_value;
2030 mutex_unlock(&kbd_led_mutex);
2031 return ret;
2034 static struct led_classdev kbd_led = {
2035 .name = "dell::kbd_backlight",
2036 .flags = LED_BRIGHT_HW_CHANGED,
2037 .brightness_set_blocking = kbd_led_level_set,
2038 .brightness_get = kbd_led_level_get,
2039 .groups = kbd_led_groups,
2042 static int __init kbd_led_init(struct device *dev)
2044 int ret;
2046 kbd_init();
2047 if (!kbd_led_present)
2048 return -ENODEV;
2049 if (!kbd_als_supported)
2050 kbd_led_groups[1] = NULL;
2051 kbd_led.max_brightness = kbd_get_max_level();
2052 if (!kbd_led.max_brightness) {
2053 kbd_led.max_brightness = kbd_get_valid_token_counts();
2054 if (kbd_led.max_brightness)
2055 kbd_led.max_brightness--;
2058 kbd_led_level = kbd_led_level_get(NULL);
2060 ret = led_classdev_register(dev, &kbd_led);
2061 if (ret)
2062 kbd_led_present = false;
2064 return ret;
2067 static void brightness_set_exit(struct led_classdev *led_cdev,
2068 enum led_brightness value)
2070 /* Don't change backlight level on exit */
2073 static void kbd_led_exit(void)
2075 if (!kbd_led_present)
2076 return;
2077 kbd_led.brightness_set = brightness_set_exit;
2078 led_classdev_unregister(&kbd_led);
2081 static int dell_laptop_notifier_call(struct notifier_block *nb,
2082 unsigned long action, void *data)
2084 bool changed = false;
2085 enum led_brightness new_kbd_led_level;
2087 switch (action) {
2088 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2089 if (!kbd_led_present)
2090 break;
2092 mutex_lock(&kbd_led_mutex);
2093 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2094 if (kbd_led_level != new_kbd_led_level) {
2095 kbd_led_level = new_kbd_led_level;
2096 changed = true;
2098 mutex_unlock(&kbd_led_mutex);
2100 if (changed)
2101 led_classdev_notify_brightness_hw_changed(&kbd_led,
2102 kbd_led_level);
2103 break;
2106 return NOTIFY_OK;
2109 static struct notifier_block dell_laptop_notifier = {
2110 .notifier_call = dell_laptop_notifier_call,
2113 static int micmute_led_set(struct led_classdev *led_cdev,
2114 enum led_brightness brightness)
2116 struct calling_interface_buffer buffer;
2117 struct calling_interface_token *token;
2118 int state = brightness != LED_OFF;
2120 if (state == 0)
2121 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2122 else
2123 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2125 if (!token)
2126 return -ENODEV;
2128 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2129 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2131 return 0;
2134 static struct led_classdev micmute_led_cdev = {
2135 .name = "platform::micmute",
2136 .max_brightness = 1,
2137 .brightness_set_blocking = micmute_led_set,
2138 .default_trigger = "audio-micmute",
2141 static int __init dell_init(void)
2143 struct calling_interface_token *token;
2144 int max_intensity = 0;
2145 int ret;
2147 if (!dmi_check_system(dell_device_table))
2148 return -ENODEV;
2150 quirks = NULL;
2151 /* find if this machine support other functions */
2152 dmi_check_system(dell_quirks);
2154 ret = platform_driver_register(&platform_driver);
2155 if (ret)
2156 goto fail_platform_driver;
2157 platform_device = platform_device_alloc("dell-laptop", -1);
2158 if (!platform_device) {
2159 ret = -ENOMEM;
2160 goto fail_platform_device1;
2162 ret = platform_device_add(platform_device);
2163 if (ret)
2164 goto fail_platform_device2;
2166 ret = dell_setup_rfkill();
2168 if (ret) {
2169 pr_warn("Unable to setup rfkill\n");
2170 goto fail_rfkill;
2173 if (quirks && quirks->touchpad_led)
2174 touchpad_led_init(&platform_device->dev);
2176 kbd_led_init(&platform_device->dev);
2178 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2179 if (dell_laptop_dir != NULL)
2180 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2181 &dell_debugfs_fops);
2183 dell_laptop_register_notifier(&dell_laptop_notifier);
2185 micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2186 ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2187 if (ret < 0)
2188 goto fail_led;
2190 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2191 return 0;
2193 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2194 if (token) {
2195 struct calling_interface_buffer buffer;
2197 dell_fill_request(&buffer, token->location, 0, 0, 0);
2198 ret = dell_send_request(&buffer,
2199 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2200 if (ret == 0)
2201 max_intensity = buffer.output[3];
2204 if (max_intensity) {
2205 struct backlight_properties props;
2206 memset(&props, 0, sizeof(struct backlight_properties));
2207 props.type = BACKLIGHT_PLATFORM;
2208 props.max_brightness = max_intensity;
2209 dell_backlight_device = backlight_device_register("dell_backlight",
2210 &platform_device->dev,
2211 NULL,
2212 &dell_ops,
2213 &props);
2215 if (IS_ERR(dell_backlight_device)) {
2216 ret = PTR_ERR(dell_backlight_device);
2217 dell_backlight_device = NULL;
2218 goto fail_backlight;
2221 dell_backlight_device->props.brightness =
2222 dell_get_intensity(dell_backlight_device);
2223 if (dell_backlight_device->props.brightness < 0) {
2224 ret = dell_backlight_device->props.brightness;
2225 goto fail_get_brightness;
2227 backlight_update_status(dell_backlight_device);
2230 return 0;
2232 fail_get_brightness:
2233 backlight_device_unregister(dell_backlight_device);
2234 fail_backlight:
2235 led_classdev_unregister(&micmute_led_cdev);
2236 fail_led:
2237 dell_cleanup_rfkill();
2238 fail_rfkill:
2239 platform_device_del(platform_device);
2240 fail_platform_device2:
2241 platform_device_put(platform_device);
2242 fail_platform_device1:
2243 platform_driver_unregister(&platform_driver);
2244 fail_platform_driver:
2245 return ret;
2248 static void __exit dell_exit(void)
2250 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2251 debugfs_remove_recursive(dell_laptop_dir);
2252 if (quirks && quirks->touchpad_led)
2253 touchpad_led_exit();
2254 kbd_led_exit();
2255 backlight_device_unregister(dell_backlight_device);
2256 led_classdev_unregister(&micmute_led_cdev);
2257 dell_cleanup_rfkill();
2258 if (platform_device) {
2259 platform_device_unregister(platform_device);
2260 platform_driver_unregister(&platform_driver);
2264 /* dell-rbtn.c driver export functions which will not work correctly (and could
2265 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2266 * not problem when dell-rbtn.c is compiled as external module. When both files
2267 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2268 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2269 * This can be achieved by late_initcall() instead module_init().
2271 late_initcall(dell_init);
2272 module_exit(dell_exit);
2274 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2275 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2276 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2277 MODULE_DESCRIPTION("Dell laptop driver");
2278 MODULE_LICENSE("GPL");