1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define TPACPI_VERSION "0.26"
12 #define TPACPI_SYSFS_VERSION 0x030000
16 * 2007-10-20 changelog trimmed down
18 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
21 * 2006-11-22 0.13 new maintainer
22 * changelog now lives in git commit history, and will
23 * not be updated further in-file.
25 * 2005-03-17 0.11 support for 600e, 770x
26 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
28 * 2005-01-16 0.9 use MODULE_VERSION
29 * thanks to Henrik Brix Andersen <brix@gentoo.org>
30 * fix parameter passing on module loading
31 * thanks to Rusty Russell <rusty@rustcorp.com.au>
32 * thanks to Jim Radford <radford@blackbean.org>
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
34 * thanks to Chris Wright <chrisw@osdl.org>
37 #include <linux/acpi.h>
38 #include <linux/backlight.h>
39 #include <linux/bitops.h>
40 #include <linux/delay.h>
41 #include <linux/dmi.h>
43 #include <linux/freezer.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/init.h>
47 #include <linux/input.h>
48 #include <linux/jiffies.h>
49 #include <linux/kernel.h>
50 #include <linux/kthread.h>
51 #include <linux/leds.h>
52 #include <linux/list.h>
53 #include <linux/lockdep.h>
54 #include <linux/module.h>
55 #include <linux/mutex.h>
56 #include <linux/nvram.h>
57 #include <linux/pci.h>
58 #include <linux/platform_device.h>
59 #include <linux/platform_profile.h>
60 #include <linux/power_supply.h>
61 #include <linux/proc_fs.h>
62 #include <linux/rfkill.h>
63 #include <linux/sched.h>
64 #include <linux/sched/signal.h>
65 #include <linux/seq_file.h>
66 #include <linux/slab.h>
67 #include <linux/string.h>
68 #include <linux/string_helpers.h>
69 #include <linux/sysfs.h>
70 #include <linux/types.h>
71 #include <linux/uaccess.h>
72 #include <linux/units.h>
73 #include <linux/workqueue.h>
75 #include <acpi/battery.h>
76 #include <acpi/video.h>
78 #include <drm/drm_privacy_screen_driver.h>
80 #include <sound/control.h>
81 #include <sound/core.h>
82 #include <sound/initval.h>
84 #include "dual_accel_detect.h"
86 /* ThinkPad CMOS commands */
87 #define TP_CMOS_VOLUME_DOWN 0
88 #define TP_CMOS_VOLUME_UP 1
89 #define TP_CMOS_VOLUME_MUTE 2
90 #define TP_CMOS_BRIGHTNESS_UP 4
91 #define TP_CMOS_BRIGHTNESS_DOWN 5
92 #define TP_CMOS_THINKLIGHT_ON 12
93 #define TP_CMOS_THINKLIGHT_OFF 13
97 TP_NVRAM_ADDR_HK2 = 0x57,
98 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
99 TP_NVRAM_ADDR_VIDEO = 0x59,
100 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
101 TP_NVRAM_ADDR_MIXER = 0x60,
104 /* NVRAM bit masks */
106 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
107 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
108 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
109 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
110 TP_NVRAM_MASK_THINKLIGHT = 0x10,
111 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
112 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
113 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
114 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
115 TP_NVRAM_MASK_MUTE = 0x40,
116 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
117 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
118 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121 /* Misc NVRAM-related */
123 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
127 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
128 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
129 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
130 #define TPACPI_ACPI_EC_HID "PNP0C09"
133 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
134 #define TPACPI_HKEY_INPUT_VERSION 0x4101
136 /* ACPI \WGSV commands */
138 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
139 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
140 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
141 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144 /* TP_ACPI_WGSV_GET_STATE bits */
146 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
147 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
148 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
149 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
150 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
151 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
152 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
153 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
154 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
155 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
159 enum tpacpi_hkey_event_t {
161 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
162 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
163 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
164 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
168 TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
169 TP_HKEY_EV_AMT_TOGGLE = 0x131a, /* Toggle AMT on/off */
170 TP_HKEY_EV_PROFILE_TOGGLE = 0x131f, /* Toggle platform profile */
172 /* Reasons for waking up from S3/S4 */
173 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
174 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
175 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
176 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
177 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
178 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
180 /* Auto-sleep after eject request */
181 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
182 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
184 /* Misc bay events */
185 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
186 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
187 or port replicator */
188 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
189 dock or port replicator */
191 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
192 * when keyboard cover is attached, detached or folded onto the back
194 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
195 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
197 /* User-interface events */
198 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
199 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
200 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
201 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
202 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
203 * enter/leave tablet mode
205 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
206 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
207 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
209 /* Key-related user-interface events */
210 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
211 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
212 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
215 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
216 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
217 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
218 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
219 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
220 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
221 * command completed. Related to
223 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
224 * changed. Related to AML GMTS */
226 /* AC-related events */
227 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
229 /* Further user-interface events */
230 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
231 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
234 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
237 /****************************************************************************
241 #define TPACPI_NAME "thinkpad"
242 #define TPACPI_DESC "ThinkPad ACPI Extras"
243 #define TPACPI_FILE TPACPI_NAME "_acpi"
244 #define TPACPI_URL "http://ibm-acpi.sf.net/"
245 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
247 #define TPACPI_PROC_DIR "ibm"
248 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
249 #define TPACPI_DRVR_NAME TPACPI_FILE
250 #define TPACPI_DRVR_SHORTNAME "tpacpi"
251 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
253 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
254 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
256 #define TPACPI_MAX_ACPI_ARGS 3
258 /* Debugging printk groups */
259 #define TPACPI_DBG_ALL 0xffff
260 #define TPACPI_DBG_DISCLOSETASK 0x8000
261 #define TPACPI_DBG_INIT 0x0001
262 #define TPACPI_DBG_EXIT 0x0002
263 #define TPACPI_DBG_RFKILL 0x0004
264 #define TPACPI_DBG_HKEY 0x0008
265 #define TPACPI_DBG_FAN 0x0010
266 #define TPACPI_DBG_BRGHT 0x0020
267 #define TPACPI_DBG_MIXER 0x0040
269 #define FAN_NOT_PRESENT 65535
271 /****************************************************************************
272 * Driver-wide structs and misc. variables
277 struct tp_acpi_drv_struct {
278 const struct acpi_device_id *hid;
279 struct acpi_driver *driver;
281 void (*notify) (struct ibm_struct *, u32);
284 struct acpi_device *device;
290 int (*read) (struct seq_file *);
291 int (*write) (char *);
293 void (*resume) (void);
294 void (*suspend) (void);
295 void (*shutdown) (void);
297 struct list_head all_drivers;
299 struct tp_acpi_drv_struct *acpi;
302 u8 acpi_driver_registered:1;
303 u8 acpi_notify_installed:1;
310 struct ibm_init_struct {
313 int (*init) (struct ibm_init_struct *);
314 umode_t base_procfs_mode;
315 struct ibm_struct *data;
323 static struct quirk_entry quirk_btusb_bug = {
333 TP_HOTKEY_TABLET_NONE = 0,
334 TP_HOTKEY_TABLET_USES_MHKG,
335 TP_HOTKEY_TABLET_USES_GMMS,
340 u32 bright_acpimode:1;
344 u32 fan_ctrl_status_undef:1;
346 u32 second_fan_ctl:1;
347 u32 beep_needs_two_args:1;
348 u32 mixer_no_level_control:1;
349 u32 battery_force_primary:1;
350 u32 input_device_registered:1;
351 u32 platform_drv_registered:1;
352 u32 sensors_pdrv_registered:1;
353 u32 hotkey_poll_active:1;
354 u32 has_adaptive_kbd:1;
356 struct quirk_entry *quirks;
360 u16 hotkey_mask_ff:1;
361 u16 volume_ctrl_forbidden:1;
364 struct thinkpad_id_data {
365 unsigned int vendor; /* ThinkPad vendor:
366 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
368 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
369 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
371 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
373 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
376 char *model_str; /* ThinkPad T43 */
377 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
379 static struct thinkpad_id_data thinkpad_id;
382 TPACPI_LIFE_INIT = 0,
387 static int experimental;
388 static u32 dbg_level;
390 static struct workqueue_struct *tpacpi_wq;
398 /* tpacpi LED class */
399 struct tpacpi_led_classdev {
400 struct led_classdev led_classdev;
404 /* brightness level capabilities */
405 static unsigned int bright_maxlvl; /* 0 = unknown */
407 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
408 static int dbg_wlswemul;
409 static bool tpacpi_wlsw_emulstate;
410 static int dbg_bluetoothemul;
411 static bool tpacpi_bluetooth_emulstate;
412 static int dbg_wwanemul;
413 static bool tpacpi_wwan_emulstate;
414 static int dbg_uwbemul;
415 static bool tpacpi_uwb_emulstate;
419 /*************************************************************************
423 #define dbg_printk(a_dbg_level, format, arg...) \
425 if (dbg_level & (a_dbg_level)) \
426 printk(KERN_DEBUG pr_fmt("%s: " format), \
430 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
431 #define vdbg_printk dbg_printk
432 static const char *str_supported(int is_supported);
434 static inline const char *str_supported(int is_supported) { return ""; }
435 #define vdbg_printk(a_dbg_level, format, arg...) \
436 do { if (0) no_printk(format, ##arg); } while (0)
439 static void tpacpi_log_usertask(const char * const what)
441 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
442 what, task_tgid_vnr(current));
445 #define tpacpi_disclose_usertask(what, format, arg...) \
447 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
448 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
449 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
450 what, task_tgid_vnr(current), ## arg); \
455 * Quirk handling helpers
457 * ThinkPad IDs and versions seen in the field so far are
458 * two or three characters from the set [0-9A-Z], i.e. base 36.
460 * We use values well outside that range as specials.
463 #define TPACPI_MATCH_ANY 0xffffffffU
464 #define TPACPI_MATCH_ANY_VERSION 0xffffU
465 #define TPACPI_MATCH_UNKNOWN 0U
467 /* TPID('1', 'Y') == 0x3159 */
468 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
469 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
472 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
473 { .vendor = PCI_VENDOR_ID_IBM, \
474 .bios = TPID(__id1, __id2), \
475 .ec = TPACPI_MATCH_ANY, \
476 .quirks = (__quirk) }
478 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
479 { .vendor = PCI_VENDOR_ID_LENOVO, \
480 .bios = TPID(__id1, __id2), \
481 .ec = TPACPI_MATCH_ANY, \
482 .quirks = (__quirk) }
484 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
485 { .vendor = PCI_VENDOR_ID_LENOVO, \
486 .bios = TPID3(__id1, __id2, __id3), \
487 .ec = TPACPI_MATCH_ANY, \
488 .quirks = (__quirk) }
490 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
491 { .vendor = PCI_VENDOR_ID_IBM, \
492 .bios = TPACPI_MATCH_ANY, \
493 .ec = TPID(__id1, __id2), \
494 .quirks = (__quirk) }
496 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
497 { .vendor = PCI_VENDOR_ID_LENOVO, \
498 .bios = TPACPI_MATCH_ANY, \
499 .ec = TPID(__id1, __id2), \
500 .quirks = (__quirk) }
502 struct tpacpi_quirk {
506 unsigned long quirks;
510 * tpacpi_check_quirks() - search BIOS/EC version on a list
511 * @qlist: array of &struct tpacpi_quirk
512 * @qlist_size: number of elements in @qlist
514 * Iterates over a quirks list until one is found that matches the
515 * ThinkPad's vendor, BIOS and EC model.
517 * Returns: %0 if nothing matches, otherwise returns the quirks field of
518 * the matching &struct tpacpi_quirk entry.
520 * The match criteria is: vendor, ec and bios must match.
522 static unsigned long __init tpacpi_check_quirks(
523 const struct tpacpi_quirk *qlist,
524 unsigned int qlist_size)
527 if ((qlist->vendor == thinkpad_id.vendor ||
528 qlist->vendor == TPACPI_MATCH_ANY) &&
529 (qlist->bios == thinkpad_id.bios_model ||
530 qlist->bios == TPACPI_MATCH_ANY) &&
531 (qlist->ec == thinkpad_id.ec_model ||
532 qlist->ec == TPACPI_MATCH_ANY))
533 return qlist->quirks;
541 static inline bool __pure __init tpacpi_is_lenovo(void)
543 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
546 static inline bool __pure __init tpacpi_is_ibm(void)
548 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
551 /****************************************************************************
552 ****************************************************************************
554 * ACPI Helpers and device model
556 ****************************************************************************
557 ****************************************************************************/
559 /*************************************************************************
563 static acpi_handle root_handle;
564 static acpi_handle ec_handle;
566 #define TPACPI_HANDLE(object, parent, paths...) \
567 static acpi_handle object##_handle; \
568 static const acpi_handle * const object##_parent __initconst = \
570 static char *object##_paths[] __initdata = { paths }
572 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
573 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
575 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
577 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
578 "\\CMS", /* R40, R40e */
581 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
582 "^HKEY", /* R30, R31 */
583 "HKEY", /* all others */
586 /*************************************************************************
590 static int acpi_evalf(acpi_handle handle,
591 int *res, char *method, char *fmt, ...)
594 struct acpi_object_list params;
595 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
596 struct acpi_buffer result, *resultp;
597 union acpi_object out_obj;
605 pr_err("acpi_evalf() called with empty format\n");
618 params.pointer = &in_objs[0];
625 in_objs[params.count].integer.value = va_arg(ap, int);
626 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
628 /* add more types as needed */
630 pr_err("acpi_evalf() called with invalid format character '%c'\n",
638 if (res_type != 'v') {
639 result.length = sizeof(out_obj);
640 result.pointer = &out_obj;
645 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
649 success = (status == AE_OK &&
650 out_obj.type == ACPI_TYPE_INTEGER);
652 *res = out_obj.integer.value;
655 success = status == AE_OK;
657 /* add more types as needed */
659 pr_err("acpi_evalf() called with invalid format character '%c'\n",
664 if (!success && !quiet)
665 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
666 method, fmt0, acpi_format_exception(status));
671 static int acpi_ec_read(int i, u8 *p)
676 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
680 if (ec_read(i, p) < 0)
687 static int acpi_ec_write(int i, u8 v)
690 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
693 if (ec_write(i, v) < 0)
700 static int issue_thinkpad_cmos_command(int cmos_cmd)
705 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
711 /*************************************************************************
715 #define TPACPI_ACPIHANDLE_INIT(object) \
716 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
717 object##_paths, ARRAY_SIZE(object##_paths))
719 static void __init drv_acpi_handle_init(const char *name,
720 acpi_handle *handle, const acpi_handle parent,
721 char **paths, const int num_paths)
726 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
729 for (i = 0; i < num_paths; i++) {
730 status = acpi_get_handle(parent, paths[i], handle);
731 if (ACPI_SUCCESS(status)) {
732 dbg_printk(TPACPI_DBG_INIT,
733 "Found ACPI handle %s for %s\n",
739 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
744 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
745 u32 level, void *context, void **return_value)
747 if (!strcmp(context, "video")) {
748 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
750 if (!dev || strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
754 *(acpi_handle *)return_value = handle;
756 return AE_CTRL_TERMINATE;
759 static void __init tpacpi_acpi_handle_locate(const char *name,
764 acpi_handle device_found;
766 BUG_ON(!name || !handle);
767 vdbg_printk(TPACPI_DBG_INIT,
768 "trying to locate ACPI handle for %s, using HID %s\n",
769 name, hid ? hid : "NULL");
771 memset(&device_found, 0, sizeof(device_found));
772 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
773 (void *)name, &device_found);
777 if (ACPI_SUCCESS(status)) {
778 *handle = device_found;
779 dbg_printk(TPACPI_DBG_INIT,
780 "Found ACPI handle for %s\n", name);
782 vdbg_printk(TPACPI_DBG_INIT,
783 "Could not locate an ACPI handle for %s: %s\n",
784 name, acpi_format_exception(status));
788 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
790 struct ibm_struct *ibm = data;
792 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
795 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
798 ibm->acpi->notify(ibm, event);
801 static int __init setup_acpi_notify(struct ibm_struct *ibm)
807 if (!*ibm->acpi->handle)
810 vdbg_printk(TPACPI_DBG_INIT,
811 "setting up ACPI notify for %s\n", ibm->name);
813 ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
814 if (!ibm->acpi->device) {
815 pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
819 ibm->acpi->device->driver_data = ibm;
820 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
821 TPACPI_ACPI_EVENT_PREFIX,
824 status = acpi_install_notify_handler(*ibm->acpi->handle,
825 ibm->acpi->type, dispatch_acpi_notify, ibm);
826 if (ACPI_FAILURE(status)) {
827 if (status == AE_ALREADY_EXISTS) {
828 pr_notice("another device driver is already handling %s events\n",
831 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
832 ibm->name, acpi_format_exception(status));
836 ibm->flags.acpi_notify_installed = 1;
840 static int __init tpacpi_device_add(struct acpi_device *device)
845 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
849 dbg_printk(TPACPI_DBG_INIT,
850 "registering %s as an ACPI driver\n", ibm->name);
854 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
855 if (!ibm->acpi->driver) {
856 pr_err("failed to allocate memory for ibm->acpi->driver\n");
860 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
861 ibm->acpi->driver->ids = ibm->acpi->hid;
863 ibm->acpi->driver->ops.add = &tpacpi_device_add;
865 rc = acpi_bus_register_driver(ibm->acpi->driver);
867 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
869 kfree(ibm->acpi->driver);
870 ibm->acpi->driver = NULL;
872 ibm->flags.acpi_driver_registered = 1;
878 /****************************************************************************
879 ****************************************************************************
883 ****************************************************************************
884 ****************************************************************************/
886 static int dispatch_proc_show(struct seq_file *m, void *v)
888 struct ibm_struct *ibm = m->private;
890 if (!ibm || !ibm->read)
895 static int dispatch_proc_open(struct inode *inode, struct file *file)
897 return single_open(file, dispatch_proc_show, pde_data(inode));
900 static ssize_t dispatch_proc_write(struct file *file,
901 const char __user *userbuf,
902 size_t count, loff_t *pos)
904 struct ibm_struct *ibm = pde_data(file_inode(file));
908 if (!ibm || !ibm->write)
910 if (count > PAGE_SIZE - 1)
913 kernbuf = memdup_user_nul(userbuf, count);
915 return PTR_ERR(kernbuf);
916 ret = ibm->write(kernbuf);
925 static const struct proc_ops dispatch_proc_ops = {
926 .proc_open = dispatch_proc_open,
927 .proc_read = seq_read,
928 .proc_lseek = seq_lseek,
929 .proc_release = single_release,
930 .proc_write = dispatch_proc_write,
933 /****************************************************************************
934 ****************************************************************************
936 * Device model: input, hwmon and platform
938 ****************************************************************************
939 ****************************************************************************/
941 static struct platform_device *tpacpi_pdev;
942 static struct platform_device *tpacpi_sensors_pdev;
943 static struct device *tpacpi_hwmon;
944 static struct input_dev *tpacpi_inputdev;
945 static struct mutex tpacpi_inputdev_send_mutex;
946 static LIST_HEAD(tpacpi_all_drivers);
948 #ifdef CONFIG_PM_SLEEP
949 static int tpacpi_suspend_handler(struct device *dev)
951 struct ibm_struct *ibm, *itmp;
953 list_for_each_entry_safe(ibm, itmp,
963 static int tpacpi_resume_handler(struct device *dev)
965 struct ibm_struct *ibm, *itmp;
967 list_for_each_entry_safe(ibm, itmp,
978 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
979 tpacpi_suspend_handler, tpacpi_resume_handler);
981 static void tpacpi_shutdown_handler(struct platform_device *pdev)
983 struct ibm_struct *ibm, *itmp;
985 list_for_each_entry_safe(ibm, itmp,
993 /*************************************************************************
994 * sysfs support helpers
997 static int parse_strtoul(const char *buf,
998 unsigned long max, unsigned long *value)
1002 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1003 endp = skip_spaces(endp);
1004 if (*endp || *value > max)
1010 static void tpacpi_disable_brightness_delay(void)
1012 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1013 pr_notice("ACPI backlight control delay disabled\n");
1016 static void printk_deprecated_attribute(const char * const what,
1017 const char * const details)
1019 tpacpi_log_usertask("deprecated sysfs attribute");
1020 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1024 /*************************************************************************
1025 * rfkill and radio control support helpers
1029 * ThinkPad-ACPI firmware handling model:
1031 * WLSW (master wireless switch) is event-driven, and is common to all
1032 * firmware-controlled radios. It cannot be controlled, just monitored,
1033 * as expected. It overrides all radio state in firmware
1035 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1036 * (TODO: verify how WLSW interacts with the returned radio state).
1038 * The only time there are shadow radio state changes, is when
1039 * masked-off hotkeys are used.
1043 * Internal driver API for radio state:
1045 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1046 * bool: true means radio blocked (off)
1048 enum tpacpi_rfkill_state {
1049 TPACPI_RFK_RADIO_OFF = 0,
1053 /* rfkill switches */
1054 enum tpacpi_rfk_id {
1055 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1056 TPACPI_RFK_WWAN_SW_ID,
1057 TPACPI_RFK_UWB_SW_ID,
1061 static const char *tpacpi_rfkill_names[] = {
1062 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1063 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1064 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1065 [TPACPI_RFK_SW_MAX] = NULL
1068 /* ThinkPad-ACPI rfkill subdriver */
1070 struct rfkill *rfkill;
1071 enum tpacpi_rfk_id id;
1072 const struct tpacpi_rfk_ops *ops;
1075 struct tpacpi_rfk_ops {
1076 /* firmware interface */
1077 int (*get_status)(void);
1078 int (*set_status)(const enum tpacpi_rfkill_state);
1081 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1083 /* Query FW and update rfkill sw state for a given rfkill switch */
1084 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1091 status = (tp_rfk->ops->get_status)();
1095 rfkill_set_sw_state(tp_rfk->rfkill,
1096 (status == TPACPI_RFK_RADIO_OFF));
1102 * Sync the HW-blocking state of all rfkill switches,
1103 * do notice it causes the rfkill core to schedule uevents
1105 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1108 struct tpacpi_rfk *tp_rfk;
1110 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1111 tp_rfk = tpacpi_rfkill_switches[i];
1113 if (rfkill_set_hw_state(tp_rfk->rfkill,
1115 /* ignore -- we track sw block */
1121 /* Call to get the WLSW state from the firmware */
1122 static int hotkey_get_wlsw(void);
1124 /* Call to query WLSW state and update all rfkill switches */
1125 static bool tpacpi_rfk_check_hwblock_state(void)
1127 int res = hotkey_get_wlsw();
1130 /* When unknown or unsupported, we have to assume it is unblocked */
1134 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1135 tpacpi_rfk_update_hwblock_state(hw_blocked);
1140 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1142 struct tpacpi_rfk *tp_rfk = data;
1145 dbg_printk(TPACPI_DBG_RFKILL,
1146 "request to change radio state to %s\n",
1147 blocked ? "blocked" : "unblocked");
1149 /* try to set radio state */
1150 res = (tp_rfk->ops->set_status)(blocked ?
1151 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1153 /* and update the rfkill core with whatever the FW really did */
1154 tpacpi_rfk_update_swstate(tp_rfk);
1156 return (res < 0) ? res : 0;
1159 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1160 .set_block = tpacpi_rfk_hook_set_block,
1163 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1164 const struct tpacpi_rfk_ops *tp_rfkops,
1165 const enum rfkill_type rfktype,
1167 const bool set_default)
1169 struct tpacpi_rfk *atp_rfk;
1171 bool sw_state = false;
1175 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1177 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1179 atp_rfk->rfkill = rfkill_alloc(name,
1182 &tpacpi_rfk_rfkill_ops,
1184 if (!atp_rfk || !atp_rfk->rfkill) {
1185 pr_err("failed to allocate memory for rfkill class\n");
1191 atp_rfk->ops = tp_rfkops;
1193 sw_status = (tp_rfkops->get_status)();
1194 if (sw_status < 0) {
1195 pr_err("failed to read initial state for %s, error %d\n",
1198 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1200 /* try to keep the initial state, since we ask the
1201 * firmware to preserve it across S5 in NVRAM */
1202 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1205 hw_state = tpacpi_rfk_check_hwblock_state();
1206 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1208 res = rfkill_register(atp_rfk->rfkill);
1210 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1211 rfkill_destroy(atp_rfk->rfkill);
1216 tpacpi_rfkill_switches[id] = atp_rfk;
1218 pr_info("rfkill switch %s: radio is %sblocked\n",
1219 name, (sw_state || hw_state) ? "" : "un");
1223 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1225 struct tpacpi_rfk *tp_rfk;
1227 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1229 tp_rfk = tpacpi_rfkill_switches[id];
1231 rfkill_unregister(tp_rfk->rfkill);
1232 rfkill_destroy(tp_rfk->rfkill);
1233 tpacpi_rfkill_switches[id] = NULL;
1238 static void printk_deprecated_rfkill_attribute(const char * const what)
1240 printk_deprecated_attribute(what,
1241 "Please switch to generic rfkill before year 2010");
1244 /* sysfs <radio> enable ------------------------------------------------ */
1245 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1246 struct device_attribute *attr,
1251 printk_deprecated_rfkill_attribute(attr->attr.name);
1253 /* This is in the ABI... */
1254 if (tpacpi_rfk_check_hwblock_state()) {
1255 status = TPACPI_RFK_RADIO_OFF;
1257 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1262 return sysfs_emit(buf, "%d\n",
1263 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1266 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1267 struct device_attribute *attr,
1268 const char *buf, size_t count)
1273 printk_deprecated_rfkill_attribute(attr->attr.name);
1275 if (parse_strtoul(buf, 1, &t))
1278 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1280 /* This is in the ABI... */
1281 if (tpacpi_rfk_check_hwblock_state() && !!t)
1284 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1285 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1286 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1288 return (res < 0) ? res : count;
1291 /* procfs -------------------------------------------------------------- */
1292 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1294 if (id >= TPACPI_RFK_SW_MAX)
1295 seq_printf(m, "status:\t\tnot supported\n");
1299 /* This is in the ABI... */
1300 if (tpacpi_rfk_check_hwblock_state()) {
1301 status = TPACPI_RFK_RADIO_OFF;
1303 status = tpacpi_rfk_update_swstate(
1304 tpacpi_rfkill_switches[id]);
1309 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
1310 seq_printf(m, "commands:\tenable, disable\n");
1316 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1322 if (id >= TPACPI_RFK_SW_MAX)
1325 while ((cmd = strsep(&buf, ","))) {
1326 if (strstarts(cmd, "enable"))
1327 status = TPACPI_RFK_RADIO_ON;
1328 else if (strstarts(cmd, "disable"))
1329 status = TPACPI_RFK_RADIO_OFF;
1335 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1336 str_enable_disable(status == TPACPI_RFK_RADIO_ON),
1337 tpacpi_rfkill_names[id]);
1338 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1339 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1345 /*************************************************************************
1346 * thinkpad-acpi driver attributes
1349 /* interface_version --------------------------------------------------- */
1350 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1352 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1354 static DRIVER_ATTR_RO(interface_version);
1356 /* debug_level --------------------------------------------------------- */
1357 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1359 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1362 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1367 if (parse_strtoul(buf, 0xffff, &t))
1374 static DRIVER_ATTR_RW(debug_level);
1376 /* version ------------------------------------------------------------- */
1377 static ssize_t version_show(struct device_driver *drv, char *buf)
1379 return sysfs_emit(buf, "%s v%s\n",
1380 TPACPI_DESC, TPACPI_VERSION);
1382 static DRIVER_ATTR_RO(version);
1384 /* --------------------------------------------------------------------- */
1386 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1388 /* wlsw_emulstate ------------------------------------------------------ */
1389 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1391 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1394 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1399 if (parse_strtoul(buf, 1, &t))
1402 if (tpacpi_wlsw_emulstate != !!t) {
1403 tpacpi_wlsw_emulstate = !!t;
1404 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1409 static DRIVER_ATTR_RW(wlsw_emulstate);
1411 /* bluetooth_emulstate ------------------------------------------------- */
1412 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1414 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1417 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1418 const char *buf, size_t count)
1422 if (parse_strtoul(buf, 1, &t))
1425 tpacpi_bluetooth_emulstate = !!t;
1429 static DRIVER_ATTR_RW(bluetooth_emulstate);
1431 /* wwan_emulstate ------------------------------------------------- */
1432 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1434 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1437 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1442 if (parse_strtoul(buf, 1, &t))
1445 tpacpi_wwan_emulstate = !!t;
1449 static DRIVER_ATTR_RW(wwan_emulstate);
1451 /* uwb_emulstate ------------------------------------------------- */
1452 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1454 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1457 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1462 if (parse_strtoul(buf, 1, &t))
1465 tpacpi_uwb_emulstate = !!t;
1469 static DRIVER_ATTR_RW(uwb_emulstate);
1472 /*************************************************************************
1477 * Table of recommended minimum BIOS versions
1479 * Reasons for listing:
1480 * 1. Stable BIOS, listed because the unknown amount of
1481 * bugs and bad ACPI behaviour on older versions
1483 * 2. BIOS or EC fw with known bugs that trigger on Linux
1485 * 3. BIOS with known reduced functionality in older versions
1487 * We recommend the latest BIOS and EC version.
1488 * We only support the latest BIOS and EC fw version as a rule.
1490 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1491 * Information from users in ThinkWiki
1493 * WARNING: we use this table also to detect that the machine is
1494 * a ThinkPad in some cases, so don't remove entries lightly.
1497 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1498 { .vendor = (__v), \
1499 .bios = TPID(__id1, __id2), \
1500 .ec = TPACPI_MATCH_ANY, \
1501 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1502 | TPVER(__bv1, __bv2) }
1504 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1505 __eid, __ev1, __ev2) \
1506 { .vendor = (__v), \
1507 .bios = TPID(__bid1, __bid2), \
1509 .quirks = TPVER(__ev1, __ev2) << 16 \
1510 | TPVER(__bv1, __bv2) }
1512 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1513 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1515 /* Outdated IBM BIOSes often lack the EC id string */
1516 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1517 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1518 __bv1, __bv2, TPID(__id1, __id2), \
1520 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1521 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1524 /* Outdated IBM BIOSes often lack the EC id string */
1525 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1526 __eid1, __eid2, __ev1, __ev2) \
1527 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1528 __bv1, __bv2, TPID(__eid1, __eid2), \
1530 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1531 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1534 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1535 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1537 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1538 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1539 __bv1, __bv2, TPID(__id1, __id2), \
1542 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1543 __eid1, __eid2, __ev1, __ev2) \
1544 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1545 __bv1, __bv2, TPID(__eid1, __eid2), \
1548 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1549 /* Numeric models ------------------ */
1550 /* FW MODEL BIOS VERS */
1551 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1552 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1553 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1554 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1555 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1556 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1557 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1558 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1559 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1561 /* A-series ------------------------- */
1562 /* FW MODEL BIOS VERS EC VERS */
1563 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1564 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1565 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1566 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1567 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1568 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1569 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1570 TPV_QI0('1', '3', '2', '0'), /* A22m */
1571 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1572 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1573 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1575 /* G-series ------------------------- */
1576 /* FW MODEL BIOS VERS */
1577 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1578 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1580 /* R-series, T-series --------------- */
1581 /* FW MODEL BIOS VERS EC VERS */
1582 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1583 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1584 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1585 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1586 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1587 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1588 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1589 T40/p, T41/p, T42/p (1) */
1590 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1591 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1592 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1593 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1595 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1596 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1597 TPV_QI0('1', '6', '3', '2'), /* T22 */
1598 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1599 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1600 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1602 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1603 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1604 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1606 /* BIOS FW BIOS VERS EC FW EC VERS */
1607 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1608 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1610 /* X-series ------------------------- */
1611 /* FW MODEL BIOS VERS EC VERS */
1612 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1613 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1614 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1615 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1616 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1617 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1618 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1620 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1621 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1623 /* (0) - older versions lack DMI EC fw string and functionality */
1624 /* (1) - older versions known to lack functionality */
1635 static void __init tpacpi_check_outdated_fw(void)
1637 unsigned long fwvers;
1638 u16 ec_version, bios_version;
1640 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1641 ARRAY_SIZE(tpacpi_bios_version_qtable));
1646 bios_version = fwvers & 0xffffU;
1647 ec_version = (fwvers >> 16) & 0xffffU;
1649 /* note that unknown versions are set to 0x0000 and we use that */
1650 if ((bios_version > thinkpad_id.bios_release) ||
1651 (ec_version > thinkpad_id.ec_release &&
1652 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1654 * The changelogs would let us track down the exact
1655 * reason, but it is just too much of a pain to track
1656 * it. We only list BIOSes that are either really
1657 * broken, or really stable to begin with, so it is
1658 * best if the user upgrades the firmware anyway.
1660 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1661 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1665 static bool __init tpacpi_is_fw_known(void)
1667 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1668 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1671 /****************************************************************************
1672 ****************************************************************************
1676 ****************************************************************************
1677 ****************************************************************************/
1679 /*************************************************************************
1680 * thinkpad-acpi metadata subdriver
1683 static int thinkpad_acpi_driver_read(struct seq_file *m)
1685 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1686 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1690 static struct ibm_struct thinkpad_acpi_driver_data = {
1692 .read = thinkpad_acpi_driver_read,
1695 /*************************************************************************
1700 * ThinkPad firmware event model
1702 * The ThinkPad firmware has two main event interfaces: normal ACPI
1703 * notifications (which follow the ACPI standard), and a private event
1706 * The private event interface also issues events for the hotkeys. As
1707 * the driver gained features, the event handling code ended up being
1708 * built around the hotkey subdriver. This will need to be refactored
1709 * to a more formal event API eventually.
1711 * Some "hotkeys" are actually supposed to be used as event reports,
1712 * such as "brightness has changed", "volume has changed", depending on
1713 * the ThinkPad model and how the firmware is operating.
1715 * Unlike other classes, hotkey-class events have mask/unmask control on
1716 * non-ancient firmware. However, how it behaves changes a lot with the
1717 * firmware model and version.
1720 enum { /* hot key scan codes (derived from ACPI DSDT) */
1721 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1722 TP_ACPI_HOTKEYSCAN_FNF2,
1723 TP_ACPI_HOTKEYSCAN_FNF3,
1724 TP_ACPI_HOTKEYSCAN_FNF4,
1725 TP_ACPI_HOTKEYSCAN_FNF5,
1726 TP_ACPI_HOTKEYSCAN_FNF6,
1727 TP_ACPI_HOTKEYSCAN_FNF7,
1728 TP_ACPI_HOTKEYSCAN_FNF8,
1729 TP_ACPI_HOTKEYSCAN_FNF9,
1730 TP_ACPI_HOTKEYSCAN_FNF10,
1731 TP_ACPI_HOTKEYSCAN_FNF11,
1732 TP_ACPI_HOTKEYSCAN_FNF12,
1733 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1734 TP_ACPI_HOTKEYSCAN_FNINSERT,
1735 TP_ACPI_HOTKEYSCAN_FNDELETE,
1736 TP_ACPI_HOTKEYSCAN_FNHOME,
1737 TP_ACPI_HOTKEYSCAN_FNEND,
1738 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1739 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1740 TP_ACPI_HOTKEYSCAN_FNSPACE,
1741 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1742 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1743 TP_ACPI_HOTKEYSCAN_MUTE,
1744 TP_ACPI_HOTKEYSCAN_THINKPAD,
1745 TP_ACPI_HOTKEYSCAN_UNK1,
1746 TP_ACPI_HOTKEYSCAN_UNK2,
1747 TP_ACPI_HOTKEYSCAN_UNK3,
1748 TP_ACPI_HOTKEYSCAN_UNK4,
1749 TP_ACPI_HOTKEYSCAN_UNK5,
1750 TP_ACPI_HOTKEYSCAN_UNK6,
1751 TP_ACPI_HOTKEYSCAN_UNK7,
1752 TP_ACPI_HOTKEYSCAN_UNK8,
1754 /* Adaptive keyboard keycodes */
1755 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1756 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1757 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1758 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1759 TP_ACPI_HOTKEYSCAN_CLOUD,
1760 TP_ACPI_HOTKEYSCAN_UNK9,
1761 TP_ACPI_HOTKEYSCAN_VOICE,
1762 TP_ACPI_HOTKEYSCAN_UNK10,
1763 TP_ACPI_HOTKEYSCAN_GESTURES,
1764 TP_ACPI_HOTKEYSCAN_UNK11,
1765 TP_ACPI_HOTKEYSCAN_UNK12,
1766 TP_ACPI_HOTKEYSCAN_UNK13,
1767 TP_ACPI_HOTKEYSCAN_CONFIG,
1768 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1769 TP_ACPI_HOTKEYSCAN_RELOAD,
1770 TP_ACPI_HOTKEYSCAN_BACK,
1771 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1772 TP_ACPI_HOTKEYSCAN_MIC_UP,
1773 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1774 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1775 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1777 /* Lenovo extended keymap, starting at 0x1300 */
1778 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1779 /* first new observed key (star, favorites) is 0x1311 */
1780 TP_ACPI_HOTKEYSCAN_STAR = 69,
1781 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1782 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1783 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1784 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1785 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1786 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1787 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1788 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1790 /* Hotkey keymap size */
1791 TPACPI_HOTKEY_MAP_LEN
1794 enum { /* Keys/events available through NVRAM polling */
1795 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1796 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1799 enum { /* Positions of some of the keys in hotkey masks */
1800 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1801 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1802 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1803 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1804 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1805 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1806 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1807 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1808 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1809 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1810 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1813 enum { /* NVRAM to ACPI HKEY group map */
1814 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1815 TP_ACPI_HKEY_ZOOM_MASK |
1816 TP_ACPI_HKEY_DISPSWTCH_MASK |
1817 TP_ACPI_HKEY_HIBERNATE_MASK,
1818 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1819 TP_ACPI_HKEY_BRGHTDWN_MASK,
1820 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1821 TP_ACPI_HKEY_VOLDWN_MASK |
1822 TP_ACPI_HKEY_MUTE_MASK,
1825 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1826 struct tp_nvram_state {
1827 u16 thinkpad_toggle:1;
1829 u16 display_toggle:1;
1830 u16 thinklight_toggle:1;
1831 u16 hibernate_toggle:1;
1832 u16 displayexp_toggle:1;
1833 u16 display_state:1;
1834 u16 brightness_toggle:1;
1835 u16 volume_toggle:1;
1838 u8 brightness_level;
1842 /* kthread for the hotkey poller */
1843 static struct task_struct *tpacpi_hotkey_task;
1846 * Acquire mutex to write poller control variables as an
1849 * Increment hotkey_config_change when changing them if you
1850 * want the kthread to forget old state.
1852 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1854 static struct mutex hotkey_thread_data_mutex;
1855 static unsigned int hotkey_config_change;
1858 * hotkey poller control variables
1860 * Must be atomic or readers will also need to acquire mutex
1862 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1863 * should be used only when the changes need to be taken as
1864 * a block, OR when one needs to force the kthread to forget
1867 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1868 static unsigned int hotkey_poll_freq = 10; /* Hz */
1870 #define HOTKEY_CONFIG_CRITICAL_START \
1872 mutex_lock(&hotkey_thread_data_mutex); \
1873 hotkey_config_change++; \
1875 #define HOTKEY_CONFIG_CRITICAL_END \
1876 mutex_unlock(&hotkey_thread_data_mutex);
1878 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1880 #define hotkey_source_mask 0U
1881 #define HOTKEY_CONFIG_CRITICAL_START
1882 #define HOTKEY_CONFIG_CRITICAL_END
1884 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1886 static struct mutex hotkey_mutex;
1888 static enum { /* Reasons for waking up */
1889 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1890 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1891 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1892 } hotkey_wakeup_reason;
1894 static int hotkey_autosleep_ack;
1896 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1897 static u32 hotkey_all_mask; /* all events supported in fw */
1898 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1899 static u32 hotkey_reserved_mask; /* events better left disabled */
1900 static u32 hotkey_driver_mask; /* events needed by the driver */
1901 static u32 hotkey_user_mask; /* events visible to userspace */
1902 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1904 static u16 *hotkey_keycode_map;
1906 static void tpacpi_driver_event(const unsigned int hkey_event);
1907 static void hotkey_driver_event(const unsigned int scancode);
1908 static void hotkey_poll_setup(const bool may_warn);
1910 /* HKEY.MHKG() return bits */
1911 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1913 TP_ACPI_MULTI_MODE_INVALID = 0,
1914 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1915 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1916 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1917 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1918 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1919 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1920 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1924 /* The following modes are considered tablet mode for the purpose of
1925 * reporting the status to userspace. i.e. in all these modes it makes
1926 * sense to disable the laptop input devices such as touchpad and
1929 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1930 TP_ACPI_MULTI_MODE_STAND |
1931 TP_ACPI_MULTI_MODE_TENT |
1932 TP_ACPI_MULTI_MODE_STAND_TENT,
1935 static int hotkey_get_wlsw(void)
1939 if (!tp_features.hotkey_wlsw)
1942 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1944 return (tpacpi_wlsw_emulstate) ?
1945 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1948 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1951 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1954 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1956 int type = (s >> 16) & 0xffff;
1957 int value = s & 0xffff;
1958 int mode = TP_ACPI_MULTI_MODE_INVALID;
1959 int valid_modes = 0;
1961 if (has_tablet_mode)
1962 *has_tablet_mode = 0;
1966 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1967 TP_ACPI_MULTI_MODE_TABLET |
1968 TP_ACPI_MULTI_MODE_STAND_TENT;
1971 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1972 TP_ACPI_MULTI_MODE_FLAT |
1973 TP_ACPI_MULTI_MODE_TABLET |
1974 TP_ACPI_MULTI_MODE_STAND |
1975 TP_ACPI_MULTI_MODE_TENT;
1978 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1979 TP_ACPI_MULTI_MODE_FLAT;
1983 /* In mode 4, FLAT is not specified as a valid mode. However,
1984 * it can be seen at least on the X1 Yoga 2nd Generation.
1986 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1987 TP_ACPI_MULTI_MODE_FLAT |
1988 TP_ACPI_MULTI_MODE_TABLET |
1989 TP_ACPI_MULTI_MODE_STAND |
1990 TP_ACPI_MULTI_MODE_TENT;
1993 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
1994 type, value, TPACPI_MAIL);
1998 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
1999 *has_tablet_mode = 1;
2003 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2006 mode = TP_ACPI_MULTI_MODE_FLAT;
2009 mode = TP_ACPI_MULTI_MODE_TABLET;
2013 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2015 mode = TP_ACPI_MULTI_MODE_STAND;
2018 mode = TP_ACPI_MULTI_MODE_TENT;
2021 if (type == 5 && value == 0xffff) {
2022 pr_warn("Multi mode status is undetected, assuming laptop\n");
2027 if (!(mode & valid_modes)) {
2028 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2029 value, type, TPACPI_MAIL);
2033 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2036 static int hotkey_get_tablet_mode(int *status)
2040 switch (tp_features.hotkey_tablet) {
2041 case TP_HOTKEY_TABLET_USES_MHKG:
2042 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2045 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2047 case TP_HOTKEY_TABLET_USES_GMMS:
2048 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2051 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2061 * Reads current event mask from firmware, and updates
2062 * hotkey_acpi_mask accordingly. Also resets any bits
2063 * from hotkey_user_mask that are unavailable to be
2064 * delivered (shadow requirement of the userspace ABI).
2066 static int hotkey_mask_get(void)
2068 lockdep_assert_held(&hotkey_mutex);
2070 if (tp_features.hotkey_mask) {
2073 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2076 hotkey_acpi_mask = m;
2078 /* no mask support doesn't mean no event support... */
2079 hotkey_acpi_mask = hotkey_all_mask;
2082 /* sync userspace-visible mask */
2083 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2088 static void hotkey_mask_warn_incomplete_mask(void)
2090 /* log only what the user can fix... */
2091 const u32 wantedmask = hotkey_driver_mask &
2092 ~(hotkey_acpi_mask | hotkey_source_mask) &
2093 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2096 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2100 * Set the firmware mask when supported
2102 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2104 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2106 static int hotkey_mask_set(u32 mask)
2111 const u32 fwmask = mask & ~hotkey_source_mask;
2113 lockdep_assert_held(&hotkey_mutex);
2115 if (tp_features.hotkey_mask) {
2116 for (i = 0; i < 32; i++) {
2117 if (!acpi_evalf(hkey_handle,
2118 NULL, "MHKM", "vdd", i + 1,
2119 !!(mask & (1 << i)))) {
2127 * We *must* make an inconditional call to hotkey_mask_get to
2128 * refresh hotkey_acpi_mask and update hotkey_user_mask
2130 * Take the opportunity to also log when we cannot _enable_
2133 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2134 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2135 fwmask, hotkey_acpi_mask);
2138 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2139 hotkey_mask_warn_incomplete_mask();
2145 * Sets hotkey_user_mask and tries to set the firmware mask
2147 static int hotkey_user_mask_set(const u32 mask)
2151 lockdep_assert_held(&hotkey_mutex);
2153 /* Give people a chance to notice they are doing something that
2154 * is bound to go boom on their users sooner or later */
2155 if (!tp_warned.hotkey_mask_ff &&
2156 (mask == 0xffff || mask == 0xffffff ||
2157 mask == 0xffffffff)) {
2158 tp_warned.hotkey_mask_ff = 1;
2159 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2161 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2164 /* Try to enable what the user asked for, plus whatever we need.
2165 * this syncs everything but won't enable bits in hotkey_user_mask */
2166 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2168 /* Enable the available bits in hotkey_user_mask */
2169 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2175 * Sets the driver hotkey mask.
2177 * Can be called even if the hotkey subdriver is inactive
2179 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2183 /* Do the right thing if hotkey_init has not been called yet */
2184 if (!tp_features.hotkey) {
2185 hotkey_driver_mask = mask;
2189 mutex_lock(&hotkey_mutex);
2191 HOTKEY_CONFIG_CRITICAL_START
2192 hotkey_driver_mask = mask;
2193 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2194 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2196 HOTKEY_CONFIG_CRITICAL_END
2198 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2199 ~hotkey_source_mask);
2200 hotkey_poll_setup(true);
2202 mutex_unlock(&hotkey_mutex);
2207 static int hotkey_status_get(int *status)
2209 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2215 static int hotkey_status_set(bool enable)
2217 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2223 static void tpacpi_input_send_tabletsw(void)
2227 if (tp_features.hotkey_tablet &&
2228 !hotkey_get_tablet_mode(&state)) {
2229 mutex_lock(&tpacpi_inputdev_send_mutex);
2231 input_report_switch(tpacpi_inputdev,
2232 SW_TABLET_MODE, !!state);
2233 input_sync(tpacpi_inputdev);
2235 mutex_unlock(&tpacpi_inputdev_send_mutex);
2239 /* Do NOT call without validating scancode first */
2240 static void tpacpi_input_send_key(const unsigned int scancode)
2242 const unsigned int keycode = hotkey_keycode_map[scancode];
2244 if (keycode != KEY_RESERVED) {
2245 mutex_lock(&tpacpi_inputdev_send_mutex);
2247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2248 input_report_key(tpacpi_inputdev, keycode, 1);
2249 input_sync(tpacpi_inputdev);
2251 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2252 input_report_key(tpacpi_inputdev, keycode, 0);
2253 input_sync(tpacpi_inputdev);
2255 mutex_unlock(&tpacpi_inputdev_send_mutex);
2259 /* Do NOT call without validating scancode first */
2260 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2262 hotkey_driver_event(scancode);
2263 if (hotkey_user_mask & (1 << scancode))
2264 tpacpi_input_send_key(scancode);
2267 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2268 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2270 /* Do NOT call without validating scancode first */
2271 static void tpacpi_hotkey_send_key(unsigned int scancode)
2273 tpacpi_input_send_key_masked(scancode);
2276 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2280 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2281 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2282 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2283 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2284 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2285 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2287 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2288 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2289 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2291 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2292 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2293 n->displayexp_toggle =
2294 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2296 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2297 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2298 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2299 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2300 n->brightness_toggle =
2301 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2303 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2304 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2305 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2306 >> TP_NVRAM_POS_LEVEL_VOLUME;
2307 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2308 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2312 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2314 if ((event_mask & (1 << __scancode)) && \
2315 oldn->__member != newn->__member) \
2316 tpacpi_hotkey_send_key(__scancode); \
2319 #define TPACPI_MAY_SEND_KEY(__scancode) \
2321 if (event_mask & (1 << __scancode)) \
2322 tpacpi_hotkey_send_key(__scancode); \
2325 static void issue_volchange(const unsigned int oldvol,
2326 const unsigned int newvol,
2327 const u32 event_mask)
2329 unsigned int i = oldvol;
2331 while (i > newvol) {
2332 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2335 while (i < newvol) {
2336 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2341 static void issue_brightnesschange(const unsigned int oldbrt,
2342 const unsigned int newbrt,
2343 const u32 event_mask)
2345 unsigned int i = oldbrt;
2347 while (i > newbrt) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2351 while (i < newbrt) {
2352 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2357 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2358 struct tp_nvram_state *newn,
2359 const u32 event_mask)
2362 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2364 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2367 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2374 * This code is supposed to duplicate the IBM firmware behaviour:
2375 * - Pressing MUTE issues mute hotkey message, even when already mute
2376 * - Pressing Volume up/down issues volume up/down hotkey messages,
2377 * even when already at maximum or minimum volume
2378 * - The act of unmuting issues volume up/down notification,
2379 * depending which key was used to unmute
2381 * We are constrained to what the NVRAM can tell us, which is not much
2382 * and certainly not enough if more than one volume hotkey was pressed
2383 * since the last poll cycle.
2385 * Just to make our life interesting, some newer Lenovo ThinkPads have
2386 * bugs in the BIOS and may fail to update volume_toggle properly.
2391 oldn->volume_toggle != newn->volume_toggle ||
2392 oldn->volume_level != newn->volume_level) {
2393 /* recently muted, or repeated mute keypress, or
2394 * multiple presses ending in mute */
2395 issue_volchange(oldn->volume_level, newn->volume_level,
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2402 /* recently unmuted, issue 'unmute' keypress */
2403 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2405 if (oldn->volume_level != newn->volume_level) {
2406 issue_volchange(oldn->volume_level, newn->volume_level,
2408 } else if (oldn->volume_toggle != newn->volume_toggle) {
2409 /* repeated vol up/down keypress at end of scale ? */
2410 if (newn->volume_level == 0)
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2412 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2417 /* handle brightness */
2418 if (oldn->brightness_level != newn->brightness_level) {
2419 issue_brightnesschange(oldn->brightness_level,
2420 newn->brightness_level, event_mask);
2421 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->brightness_level == 0)
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2425 else if (newn->brightness_level >= bright_maxlvl
2426 && !tp_features.bright_unkfw)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2430 #undef TPACPI_COMPARE_KEY
2431 #undef TPACPI_MAY_SEND_KEY
2437 * We track all events in hotkey_source_mask all the time, since
2438 * most of them are edge-based. We only issue those requested by
2439 * hotkey_user_mask or hotkey_driver_mask, though.
2441 static int hotkey_kthread(void *data)
2443 struct tp_nvram_state s[2] = { 0 };
2444 u32 poll_mask, event_mask;
2445 unsigned int si, so;
2447 unsigned int change_detector;
2448 unsigned int poll_freq;
2451 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2460 /* Initial state for compares */
2461 mutex_lock(&hotkey_thread_data_mutex);
2462 change_detector = hotkey_config_change;
2463 poll_mask = hotkey_source_mask;
2464 event_mask = hotkey_source_mask &
2465 (hotkey_driver_mask | hotkey_user_mask);
2466 poll_freq = hotkey_poll_freq;
2467 mutex_unlock(&hotkey_thread_data_mutex);
2468 hotkey_read_nvram(&s[so], poll_mask);
2470 while (!kthread_should_stop()) {
2472 if (likely(poll_freq))
2475 t = 100; /* should never happen... */
2477 t = msleep_interruptible(t);
2478 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2481 if (t > 0 && !was_frozen)
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 if (was_frozen || hotkey_config_change != change_detector) {
2486 /* forget old state on thaw or config change */
2489 change_detector = hotkey_config_change;
2491 poll_mask = hotkey_source_mask;
2492 event_mask = hotkey_source_mask &
2493 (hotkey_driver_mask | hotkey_user_mask);
2494 poll_freq = hotkey_poll_freq;
2495 mutex_unlock(&hotkey_thread_data_mutex);
2497 if (likely(poll_mask)) {
2498 hotkey_read_nvram(&s[si], poll_mask);
2499 if (likely(si != so)) {
2500 hotkey_compare_and_issue_event(&s[so], &s[si],
2513 static void hotkey_poll_stop_sync(void)
2515 lockdep_assert_held(&hotkey_mutex);
2517 if (tpacpi_hotkey_task) {
2518 kthread_stop(tpacpi_hotkey_task);
2519 tpacpi_hotkey_task = NULL;
2523 static void hotkey_poll_setup(const bool may_warn)
2525 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2526 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2528 lockdep_assert_held(&hotkey_mutex);
2530 if (hotkey_poll_freq > 0 &&
2531 (poll_driver_mask ||
2532 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2533 if (!tpacpi_hotkey_task) {
2534 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2535 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2536 if (IS_ERR(tpacpi_hotkey_task)) {
2537 tpacpi_hotkey_task = NULL;
2538 pr_err("could not create kernel thread for hotkey polling\n");
2542 hotkey_poll_stop_sync();
2543 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2544 hotkey_poll_freq == 0) {
2545 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2546 poll_user_mask, poll_driver_mask);
2551 static void hotkey_poll_setup_safe(const bool may_warn)
2553 mutex_lock(&hotkey_mutex);
2554 hotkey_poll_setup(may_warn);
2555 mutex_unlock(&hotkey_mutex);
2558 static void hotkey_poll_set_freq(unsigned int freq)
2560 lockdep_assert_held(&hotkey_mutex);
2563 hotkey_poll_stop_sync();
2565 hotkey_poll_freq = freq;
2568 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2570 static void hotkey_poll_setup(const bool __unused)
2574 static void hotkey_poll_setup_safe(const bool __unused)
2578 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2580 static int hotkey_inputdev_open(struct input_dev *dev)
2582 switch (tpacpi_lifecycle) {
2583 case TPACPI_LIFE_INIT:
2584 case TPACPI_LIFE_RUNNING:
2585 hotkey_poll_setup_safe(false);
2587 case TPACPI_LIFE_EXITING:
2591 /* Should only happen if tpacpi_lifecycle is corrupt */
2596 static void hotkey_inputdev_close(struct input_dev *dev)
2598 /* disable hotkey polling when possible */
2599 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2600 !(hotkey_source_mask & hotkey_driver_mask))
2601 hotkey_poll_setup_safe(false);
2604 /* sysfs hotkey enable ------------------------------------------------- */
2605 static ssize_t hotkey_enable_show(struct device *dev,
2606 struct device_attribute *attr,
2611 printk_deprecated_attribute("hotkey_enable",
2612 "Hotkey reporting is always enabled");
2614 res = hotkey_status_get(&status);
2618 return sysfs_emit(buf, "%d\n", status);
2621 static ssize_t hotkey_enable_store(struct device *dev,
2622 struct device_attribute *attr,
2623 const char *buf, size_t count)
2627 printk_deprecated_attribute("hotkey_enable",
2628 "Hotkeys can be disabled through hotkey_mask");
2630 if (parse_strtoul(buf, 1, &t))
2639 static DEVICE_ATTR_RW(hotkey_enable);
2641 /* sysfs hotkey mask --------------------------------------------------- */
2642 static ssize_t hotkey_mask_show(struct device *dev,
2643 struct device_attribute *attr,
2646 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2649 static ssize_t hotkey_mask_store(struct device *dev,
2650 struct device_attribute *attr,
2651 const char *buf, size_t count)
2656 if (parse_strtoul(buf, 0xffffffffUL, &t))
2659 if (mutex_lock_killable(&hotkey_mutex))
2660 return -ERESTARTSYS;
2662 res = hotkey_user_mask_set(t);
2664 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2665 hotkey_poll_setup(true);
2668 mutex_unlock(&hotkey_mutex);
2670 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2672 return (res) ? res : count;
2675 static DEVICE_ATTR_RW(hotkey_mask);
2677 /* sysfs hotkey bios_enabled ------------------------------------------- */
2678 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2679 struct device_attribute *attr,
2682 return sprintf(buf, "0\n");
2685 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2687 /* sysfs hotkey bios_mask ---------------------------------------------- */
2688 static ssize_t hotkey_bios_mask_show(struct device *dev,
2689 struct device_attribute *attr,
2692 printk_deprecated_attribute("hotkey_bios_mask",
2693 "This attribute is useless.");
2694 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2697 static DEVICE_ATTR_RO(hotkey_bios_mask);
2699 /* sysfs hotkey all_mask ----------------------------------------------- */
2700 static ssize_t hotkey_all_mask_show(struct device *dev,
2701 struct device_attribute *attr,
2704 return sysfs_emit(buf, "0x%08x\n",
2705 hotkey_all_mask | hotkey_source_mask);
2708 static DEVICE_ATTR_RO(hotkey_all_mask);
2710 /* sysfs hotkey all_mask ----------------------------------------------- */
2711 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2715 return sysfs_emit(buf, "0x%08x\n",
2716 hotkey_adaptive_all_mask | hotkey_source_mask);
2719 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2721 /* sysfs hotkey recommended_mask --------------------------------------- */
2722 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2723 struct device_attribute *attr,
2726 return sysfs_emit(buf, "0x%08x\n",
2727 (hotkey_all_mask | hotkey_source_mask)
2728 & ~hotkey_reserved_mask);
2731 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2733 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2735 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2736 static ssize_t hotkey_source_mask_show(struct device *dev,
2737 struct device_attribute *attr,
2740 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2743 static ssize_t hotkey_source_mask_store(struct device *dev,
2744 struct device_attribute *attr,
2745 const char *buf, size_t count)
2751 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2752 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2755 if (mutex_lock_killable(&hotkey_mutex))
2756 return -ERESTARTSYS;
2758 HOTKEY_CONFIG_CRITICAL_START
2759 hotkey_source_mask = t;
2760 HOTKEY_CONFIG_CRITICAL_END
2762 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2763 ~hotkey_source_mask);
2764 hotkey_poll_setup(true);
2766 /* check if events needed by the driver got disabled */
2767 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2768 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2770 mutex_unlock(&hotkey_mutex);
2773 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2776 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2779 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2781 return (rc < 0) ? rc : count;
2784 static DEVICE_ATTR_RW(hotkey_source_mask);
2786 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2787 static ssize_t hotkey_poll_freq_show(struct device *dev,
2788 struct device_attribute *attr,
2791 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2794 static ssize_t hotkey_poll_freq_store(struct device *dev,
2795 struct device_attribute *attr,
2796 const char *buf, size_t count)
2800 if (parse_strtoul(buf, 25, &t))
2803 if (mutex_lock_killable(&hotkey_mutex))
2804 return -ERESTARTSYS;
2806 hotkey_poll_set_freq(t);
2807 hotkey_poll_setup(true);
2809 mutex_unlock(&hotkey_mutex);
2811 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2816 static DEVICE_ATTR_RW(hotkey_poll_freq);
2818 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2820 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2821 static ssize_t hotkey_radio_sw_show(struct device *dev,
2822 struct device_attribute *attr,
2826 res = hotkey_get_wlsw();
2830 /* Opportunistic update */
2831 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2833 return sysfs_emit(buf, "%d\n",
2834 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2837 static DEVICE_ATTR_RO(hotkey_radio_sw);
2839 static void hotkey_radio_sw_notify_change(void)
2841 if (tp_features.hotkey_wlsw)
2842 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2846 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2847 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2848 struct device_attribute *attr,
2852 res = hotkey_get_tablet_mode(&s);
2856 return sysfs_emit(buf, "%d\n", !!s);
2859 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2861 static void hotkey_tablet_mode_notify_change(void)
2863 if (tp_features.hotkey_tablet)
2864 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2865 "hotkey_tablet_mode");
2868 /* sysfs wakeup reason (pollable) -------------------------------------- */
2869 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2870 struct device_attribute *attr,
2873 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2876 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2878 static void hotkey_wakeup_reason_notify_change(void)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2884 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2885 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2886 struct device_attribute *attr,
2889 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2892 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2893 hotkey_wakeup_hotunplug_complete_show, NULL);
2895 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2897 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2898 "wakeup_hotunplug_complete");
2901 /* sysfs adaptive kbd mode --------------------------------------------- */
2903 static int adaptive_keyboard_get_mode(void);
2904 static int adaptive_keyboard_set_mode(int new_mode);
2906 enum ADAPTIVE_KEY_MODE {
2909 WEB_CONFERENCE_MODE,
2914 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2915 struct device_attribute *attr,
2920 current_mode = adaptive_keyboard_get_mode();
2921 if (current_mode < 0)
2922 return current_mode;
2924 return sysfs_emit(buf, "%d\n", current_mode);
2927 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2928 struct device_attribute *attr,
2929 const char *buf, size_t count)
2934 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2937 res = adaptive_keyboard_set_mode(t);
2938 return (res < 0) ? res : count;
2941 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2943 static struct attribute *adaptive_kbd_attributes[] = {
2944 &dev_attr_adaptive_kbd_mode.attr,
2948 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2949 struct attribute *attr, int n)
2951 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2954 static const struct attribute_group adaptive_kbd_attr_group = {
2955 .is_visible = hadaptive_kbd_attr_is_visible,
2956 .attrs = adaptive_kbd_attributes,
2959 /* --------------------------------------------------------------------- */
2961 static struct attribute *hotkey_attributes[] = {
2962 &dev_attr_hotkey_enable.attr,
2963 &dev_attr_hotkey_bios_enabled.attr,
2964 &dev_attr_hotkey_bios_mask.attr,
2965 &dev_attr_wakeup_reason.attr,
2966 &dev_attr_wakeup_hotunplug_complete.attr,
2967 &dev_attr_hotkey_mask.attr,
2968 &dev_attr_hotkey_all_mask.attr,
2969 &dev_attr_hotkey_adaptive_all_mask.attr,
2970 &dev_attr_hotkey_recommended_mask.attr,
2971 &dev_attr_hotkey_tablet_mode.attr,
2972 &dev_attr_hotkey_radio_sw.attr,
2973 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2974 &dev_attr_hotkey_source_mask.attr,
2975 &dev_attr_hotkey_poll_freq.attr,
2980 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2981 struct attribute *attr, int n)
2983 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2984 if (!tp_features.hotkey_tablet)
2986 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2987 if (!tp_features.hotkey_wlsw)
2994 static const struct attribute_group hotkey_attr_group = {
2995 .is_visible = hotkey_attr_is_visible,
2996 .attrs = hotkey_attributes,
3000 * Sync both the hw and sw blocking state of all switches
3002 static void tpacpi_send_radiosw_update(void)
3007 * We must sync all rfkill controllers *before* issuing any
3008 * rfkill input events, or we will race the rfkill core input
3011 * tpacpi_inputdev_send_mutex works as a synchronization point
3014 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3017 wlsw = hotkey_get_wlsw();
3019 /* Sync hw blocking state first if it is hw-blocked */
3020 if (wlsw == TPACPI_RFK_RADIO_OFF)
3021 tpacpi_rfk_update_hwblock_state(true);
3023 /* Sync hw blocking state last if it is hw-unblocked */
3024 if (wlsw == TPACPI_RFK_RADIO_ON)
3025 tpacpi_rfk_update_hwblock_state(false);
3027 /* Issue rfkill input event for WLSW switch */
3029 mutex_lock(&tpacpi_inputdev_send_mutex);
3031 input_report_switch(tpacpi_inputdev,
3032 SW_RFKILL_ALL, (wlsw > 0));
3033 input_sync(tpacpi_inputdev);
3035 mutex_unlock(&tpacpi_inputdev_send_mutex);
3039 * this can be unconditional, as we will poll state again
3040 * if userspace uses the notify to read data
3042 hotkey_radio_sw_notify_change();
3045 static void hotkey_exit(void)
3047 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3048 mutex_lock(&hotkey_mutex);
3049 hotkey_poll_stop_sync();
3050 mutex_unlock(&hotkey_mutex);
3052 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3053 "restoring original HKEY status and mask\n");
3054 /* yes, there is a bitwise or below, we want the
3055 * functions to be called even if one of them fail */
3056 if (((tp_features.hotkey_mask &&
3057 hotkey_mask_set(hotkey_orig_mask)) |
3058 hotkey_status_set(false)) != 0)
3059 pr_err("failed to restore hot key mask to BIOS defaults\n");
3062 static void __init hotkey_unmap(const unsigned int scancode)
3064 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3065 clear_bit(hotkey_keycode_map[scancode],
3066 tpacpi_inputdev->keybit);
3067 hotkey_keycode_map[scancode] = KEY_RESERVED;
3073 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3076 #define TPACPI_HK_Q_INIMASK 0x0001
3078 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3079 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3080 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3081 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3082 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3083 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3084 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3085 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3086 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3087 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3088 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3089 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3090 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3091 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3092 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3093 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3094 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3095 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3096 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3097 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3100 typedef u16 tpacpi_keymap_entry_t;
3101 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3103 static int hotkey_init_tablet_mode(void)
3105 int in_tablet_mode = 0, res;
3108 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3109 int has_tablet_mode;
3111 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3114 * The Yoga 11e series has 2 accelerometers described by a
3115 * BOSC0200 ACPI node. This setup relies on a Windows service
3116 * which calls special ACPI methods on this node to report
3117 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3118 * does not support this, so skip the hotkey on these models.
3120 if (has_tablet_mode && !dual_accel_detect())
3121 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3123 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3124 /* For X41t, X60t, X61t Tablets... */
3125 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3126 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3130 if (!tp_features.hotkey_tablet)
3133 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3134 type, in_tablet_mode ? "tablet" : "laptop");
3136 return in_tablet_mode;
3139 static int __init hotkey_init(struct ibm_init_struct *iibm)
3141 /* Requirements for changing the default keymaps:
3143 * 1. Many of the keys are mapped to KEY_RESERVED for very
3144 * good reasons. Do not change them unless you have deep
3145 * knowledge on the IBM and Lenovo ThinkPad firmware for
3146 * the various ThinkPad models. The driver behaves
3147 * differently for KEY_RESERVED: such keys have their
3148 * hot key mask *unset* in mask_recommended, and also
3149 * in the initial hot key mask programmed into the
3150 * firmware at driver load time, which means the firm-
3151 * ware may react very differently if you change them to
3154 * 2. You must be subscribed to the linux-thinkpad and
3155 * ibm-acpi-devel mailing lists, and you should read the
3156 * list archives since 2007 if you want to change the
3157 * keymaps. This requirement exists so that you will
3158 * know the past history of problems with the thinkpad-
3159 * acpi driver keymaps, and also that you will be
3160 * listening to any bug reports;
3162 * 3. Do not send thinkpad-acpi specific patches directly to
3163 * for merging, *ever*. Send them to the linux-acpi
3164 * mailinglist for comments. Merging is to be done only
3165 * through acpi-test and the ACPI maintainer.
3167 * If the above is too much to ask, don't change the keymap.
3168 * Ask the thinkpad-acpi maintainer to do it, instead.
3172 TPACPI_KEYMAP_IBM_GENERIC = 0,
3173 TPACPI_KEYMAP_LENOVO_GENERIC,
3176 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3177 /* Generic keymap for IBM ThinkPads */
3178 [TPACPI_KEYMAP_IBM_GENERIC] = {
3179 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3180 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3181 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3182 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3184 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3185 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3186 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3187 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3189 /* brightness: firmware always reacts to them */
3190 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3191 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3193 /* Thinklight: firmware always react to it */
3194 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3196 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3197 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3199 /* Volume: firmware always react to it and reprograms
3200 * the built-in *extra* mixer. Never map it to control
3201 * another mixer by default. */
3202 KEY_RESERVED, /* 0x14: VOLUME UP */
3203 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3204 KEY_RESERVED, /* 0x16: MUTE */
3206 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3208 /* (assignments unknown, please report if found) */
3209 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3210 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3212 /* No assignments, only used for Adaptive keyboards. */
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3214 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3215 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 /* No assignment, used for newer Lenovo models */
3220 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3221 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3222 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3223 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3224 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3225 KEY_UNKNOWN, KEY_UNKNOWN
3229 /* Generic keymap for Lenovo ThinkPads */
3230 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3231 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3232 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3233 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3234 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3236 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3237 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3238 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3239 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3241 /* These should be enabled --only-- when ACPI video
3242 * is disabled (i.e. in "vendor" mode), and are handled
3243 * in a special way by the init code */
3244 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3245 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3247 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3249 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3250 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3252 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3253 * react to it and reprograms the built-in *extra* mixer.
3254 * Never map it to control another mixer by default.
3256 * T60?, T61, R60?, R61: firmware and EC tries to send
3257 * these over the regular keyboard, so these are no-ops,
3258 * but there are still weird bugs re. MUTE, so do not
3259 * change unless you get test reports from all Lenovo
3260 * models. May cause the BIOS to interfere with the
3263 KEY_RESERVED, /* 0x14: VOLUME UP */
3264 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3265 KEY_RESERVED, /* 0x16: MUTE */
3267 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3269 /* (assignments unknown, please report if found) */
3270 KEY_UNKNOWN, KEY_UNKNOWN,
3273 * The mic mute button only sends 0x1a. It does not
3274 * automatically mute the mic or change the mute light.
3276 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3278 /* (assignments unknown, please report if found) */
3281 /* Extra keys in use since the X240 / T440 / T540 */
3282 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3285 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3286 * The first item in this list is the Mute button which is
3287 * emitted with 0x103 through
3288 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3290 * We'll need to offset those by 0x20.
3292 KEY_RESERVED, /* Mute held, 0x103 */
3293 KEY_BRIGHTNESS_MIN, /* Backlight off */
3294 KEY_RESERVED, /* Clipping tool */
3295 KEY_RESERVED, /* Cloud */
3297 KEY_VOICECOMMAND, /* Voice */
3299 KEY_RESERVED, /* Gestures */
3303 KEY_CONFIG, /* Settings */
3304 KEY_RESERVED, /* New tab */
3305 KEY_REFRESH, /* Reload */
3306 KEY_BACK, /* Back */
3307 KEY_RESERVED, /* Microphone down */
3308 KEY_RESERVED, /* Microphone up */
3309 KEY_RESERVED, /* Microphone cancellation */
3310 KEY_RESERVED, /* Camera mode */
3311 KEY_RESERVED, /* Rotate display, 0x116 */
3314 * These are found in 2017 models (e.g. T470s, X270).
3315 * The lowest known value is 0x311, which according to
3316 * the manual should launch a user defined favorite
3319 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3320 * corresponding to 0x34.
3323 /* (assignments unknown, please report if found) */
3324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3327 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3331 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3332 KEY_CALC, /* Calculator (above numpad, P52) */
3333 KEY_BLUETOOTH, /* Bluetooth */
3334 KEY_KEYBOARD, /* Keyboard, 0x315 */
3335 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3336 KEY_NOTIFICATION_CENTER, /* Notification Center */
3337 KEY_PICKUP_PHONE, /* Answer incoming call */
3338 KEY_HANGUP_PHONE, /* Decline incoming call */
3342 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3343 /* Generic maps (fallback) */
3345 .vendor = PCI_VENDOR_ID_IBM,
3346 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3347 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3350 .vendor = PCI_VENDOR_ID_LENOVO,
3351 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3352 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3356 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3357 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3362 bool radiosw_state = false;
3363 bool tabletsw_state = false;
3365 unsigned long quirks;
3366 unsigned long keymap_id;
3368 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3369 "initializing hotkey subdriver\n");
3371 BUG_ON(!tpacpi_inputdev);
3372 BUG_ON(tpacpi_inputdev->open != NULL ||
3373 tpacpi_inputdev->close != NULL);
3375 TPACPI_ACPIHANDLE_INIT(hkey);
3376 mutex_init(&hotkey_mutex);
3378 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3379 mutex_init(&hotkey_thread_data_mutex);
3382 /* hotkey not supported on 570 */
3383 tp_features.hotkey = hkey_handle != NULL;
3385 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3387 str_supported(tp_features.hotkey));
3389 if (!tp_features.hotkey)
3392 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3393 ARRAY_SIZE(tpacpi_hotkey_qtable));
3395 tpacpi_disable_brightness_delay();
3397 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3398 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3399 for HKEY interface version 0x100 */
3400 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3401 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3402 "firmware HKEY interface version: 0x%x\n",
3405 switch (hkeyv >> 8) {
3408 * MHKV 0x100 in A31, R40, R40e,
3409 * T4x, X31, and later
3412 /* Paranoia check AND init hotkey_all_mask */
3413 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3415 pr_err("missing MHKA handler, please report this to %s\n",
3417 /* Fallback: pre-init for FN+F3,F4,F12 */
3418 hotkey_all_mask = 0x080cU;
3420 tp_features.hotkey_mask = 1;
3426 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3429 /* Paranoia check AND init hotkey_all_mask */
3430 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3432 pr_err("missing MHKA handler, please report this to %s\n",
3434 /* Fallback: pre-init for FN+F3,F4,F12 */
3435 hotkey_all_mask = 0x080cU;
3437 tp_features.hotkey_mask = 1;
3441 * Check if we have an adaptive keyboard, like on the
3442 * Lenovo Carbon X1 2014 (2nd Gen).
3444 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3446 if (hotkey_adaptive_all_mask != 0)
3447 tp_features.has_adaptive_kbd = true;
3449 tp_features.has_adaptive_kbd = false;
3450 hotkey_adaptive_all_mask = 0x0U;
3455 pr_err("unknown version of the HKEY interface: 0x%x\n",
3457 pr_err("please report this to %s\n", TPACPI_MAIL);
3462 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3463 "hotkey masks are %s\n",
3464 str_supported(tp_features.hotkey_mask));
3466 /* Init hotkey_all_mask if not initialized yet */
3467 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3468 (quirks & TPACPI_HK_Q_INIMASK))
3469 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3471 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3472 if (tp_features.hotkey_mask) {
3473 /* hotkey_source_mask *must* be zero for
3474 * the first hotkey_mask_get to return hotkey_orig_mask */
3475 mutex_lock(&hotkey_mutex);
3476 res = hotkey_mask_get();
3477 mutex_unlock(&hotkey_mutex);
3481 hotkey_orig_mask = hotkey_acpi_mask;
3483 hotkey_orig_mask = hotkey_all_mask;
3484 hotkey_acpi_mask = hotkey_all_mask;
3487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3489 tp_features.hotkey_wlsw = 1;
3490 radiosw_state = !!tpacpi_wlsw_emulstate;
3491 pr_info("radio switch emulation enabled\n");
3494 /* Not all thinkpads have a hardware radio switch */
3495 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3496 tp_features.hotkey_wlsw = 1;
3497 radiosw_state = !!status;
3498 pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
3501 tabletsw_state = hotkey_init_tablet_mode();
3503 /* Set up key map */
3504 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3505 ARRAY_SIZE(tpacpi_keymap_qtable));
3506 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3507 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3508 "using keymap number %lu\n", keymap_id);
3510 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3511 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3512 if (!hotkey_keycode_map) {
3513 pr_err("failed to allocate memory for key map\n");
3517 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3518 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3519 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3520 tpacpi_inputdev->keycode = hotkey_keycode_map;
3521 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3522 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3523 input_set_capability(tpacpi_inputdev, EV_KEY,
3524 hotkey_keycode_map[i]);
3526 if (i < sizeof(hotkey_reserved_mask)*8)
3527 hotkey_reserved_mask |= 1 << i;
3531 if (tp_features.hotkey_wlsw) {
3532 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3533 input_report_switch(tpacpi_inputdev,
3534 SW_RFKILL_ALL, radiosw_state);
3536 if (tp_features.hotkey_tablet) {
3537 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3538 input_report_switch(tpacpi_inputdev,
3539 SW_TABLET_MODE, tabletsw_state);
3542 /* Do not issue duplicate brightness change events to
3543 * userspace. tpacpi_detect_brightness_capabilities() must have
3544 * been called before this point */
3545 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3546 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3547 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3549 /* Disable brightness up/down on Lenovo thinkpads when
3550 * ACPI is handling them, otherwise it is plain impossible
3551 * for userspace to do something even remotely sane */
3552 hotkey_reserved_mask |=
3553 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3554 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3555 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3556 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3559 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3560 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3562 & ~hotkey_reserved_mask;
3564 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3565 "hotkey source mask 0x%08x, polling freq %u\n",
3566 hotkey_source_mask, hotkey_poll_freq);
3569 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3570 "enabling firmware HKEY event interface...\n");
3571 res = hotkey_status_set(true);
3576 mutex_lock(&hotkey_mutex);
3577 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3578 | hotkey_driver_mask)
3579 & ~hotkey_source_mask);
3580 mutex_unlock(&hotkey_mutex);
3581 if (res < 0 && res != -ENXIO) {
3585 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3586 & ~hotkey_reserved_mask;
3587 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3588 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3589 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3591 tpacpi_inputdev->open = &hotkey_inputdev_open;
3592 tpacpi_inputdev->close = &hotkey_inputdev_close;
3594 hotkey_poll_setup_safe(true);
3599 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3600 * mode, Web conference mode, Function mode and Lay-flat mode.
3601 * We support Home mode and Function mode currently.
3603 * Will consider support rest of modes in future.
3606 static const int adaptive_keyboard_modes[] = {
3608 /* WEB_BROWSER_MODE = 2,
3609 WEB_CONFERENCE_MODE = 3, */
3613 #define DFR_CHANGE_ROW 0x101
3614 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3615 #define FIRST_ADAPTIVE_KEY 0x103
3617 /* press Fn key a while second, it will switch to Function Mode. Then
3618 * release Fn key, previous mode be restored.
3620 static bool adaptive_keyboard_mode_is_saved;
3621 static int adaptive_keyboard_prev_mode;
3623 static int adaptive_keyboard_get_mode(void)
3627 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3628 pr_err("Cannot read adaptive keyboard mode\n");
3635 static int adaptive_keyboard_set_mode(int new_mode)
3638 new_mode > LAYFLAT_MODE)
3641 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3642 pr_err("Cannot set adaptive keyboard mode\n");
3649 static int adaptive_keyboard_get_next_mode(int mode)
3652 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3654 for (i = 0; i <= max_mode; i++) {
3655 if (adaptive_keyboard_modes[i] == mode)
3664 return adaptive_keyboard_modes[i];
3667 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3669 int current_mode = 0;
3674 case DFR_CHANGE_ROW:
3675 if (adaptive_keyboard_mode_is_saved) {
3676 new_mode = adaptive_keyboard_prev_mode;
3677 adaptive_keyboard_mode_is_saved = false;
3679 current_mode = adaptive_keyboard_get_mode();
3680 if (current_mode < 0)
3682 new_mode = adaptive_keyboard_get_next_mode(
3686 if (adaptive_keyboard_set_mode(new_mode) < 0)
3691 case DFR_SHOW_QUICKVIEW_ROW:
3692 current_mode = adaptive_keyboard_get_mode();
3693 if (current_mode < 0)
3696 adaptive_keyboard_prev_mode = current_mode;
3697 adaptive_keyboard_mode_is_saved = true;
3699 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3704 if (scancode < FIRST_ADAPTIVE_KEY ||
3705 scancode >= FIRST_ADAPTIVE_KEY +
3706 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3707 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3708 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3712 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3713 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3714 if (keycode != KEY_RESERVED) {
3715 mutex_lock(&tpacpi_inputdev_send_mutex);
3717 input_report_key(tpacpi_inputdev, keycode, 1);
3718 input_sync(tpacpi_inputdev);
3720 input_report_key(tpacpi_inputdev, keycode, 0);
3721 input_sync(tpacpi_inputdev);
3723 mutex_unlock(&tpacpi_inputdev_send_mutex);
3729 static bool hotkey_notify_extended_hotkey(const u32 hkey)
3731 unsigned int scancode;
3734 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3735 case TP_HKEY_EV_AMT_TOGGLE:
3736 case TP_HKEY_EV_PROFILE_TOGGLE:
3737 tpacpi_driver_event(hkey);
3741 /* Extended keycodes start at 0x300 and our offset into the map
3742 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3743 * will be positive, but might not be in the correct range.
3745 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3746 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3747 scancode < TPACPI_HOTKEY_MAP_LEN) {
3748 tpacpi_input_send_key(scancode);
3755 static bool hotkey_notify_hotkey(const u32 hkey,
3757 bool *ignore_acpi_ev)
3759 /* 0x1000-0x1FFF: key presses */
3760 unsigned int scancode = hkey & 0xfff;
3761 *send_acpi_ev = true;
3762 *ignore_acpi_ev = false;
3765 * Original events are in the 0x10XX range, the adaptive keyboard
3766 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3767 * models, additional keys are emitted through 0x13XX.
3769 switch ((hkey >> 8) & 0xf) {
3772 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3773 /* HKEY event 0x1001 is scancode 0x00 */
3775 if (!(hotkey_source_mask & (1 << scancode))) {
3776 tpacpi_input_send_key_masked(scancode);
3777 *send_acpi_ev = false;
3779 *ignore_acpi_ev = true;
3786 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3789 return hotkey_notify_extended_hotkey(hkey);
3795 static bool hotkey_notify_wakeup(const u32 hkey,
3797 bool *ignore_acpi_ev)
3799 /* 0x2000-0x2FFF: Wakeup reason */
3800 *send_acpi_ev = true;
3801 *ignore_acpi_ev = false;
3804 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3805 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3806 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3807 *ignore_acpi_ev = true;
3810 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3811 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3812 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3813 *ignore_acpi_ev = true;
3816 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3817 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3818 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3819 /* how to auto-heal: */
3820 /* 2313: woke up from S3, go to S4/S5 */
3821 /* 2413: woke up from S4, go to S5 */
3828 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3829 pr_info("woke up due to a hot-unplug request...\n");
3830 hotkey_wakeup_reason_notify_change();
3835 static bool hotkey_notify_dockevent(const u32 hkey,
3837 bool *ignore_acpi_ev)
3839 /* 0x4000-0x4FFF: dock-related events */
3840 *send_acpi_ev = true;
3841 *ignore_acpi_ev = false;
3844 case TP_HKEY_EV_UNDOCK_ACK:
3845 /* ACPI undock operation completed after wakeup */
3846 hotkey_autosleep_ack = 1;
3847 pr_info("undocked\n");
3848 hotkey_wakeup_hotunplug_complete_notify_change();
3851 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3852 pr_info("docked into hotplug port replicator\n");
3854 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3855 pr_info("undocked from hotplug port replicator\n");
3859 * Deliberately ignore attaching and detaching the keybord cover to avoid
3860 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3863 * Please refer to the following thread for more information and a preliminary
3864 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3865 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3866 * the Pico cartridge dock module:
3867 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3869 case TP_HKEY_EV_KBD_COVER_ATTACH:
3870 case TP_HKEY_EV_KBD_COVER_DETACH:
3871 *send_acpi_ev = false;
3872 *ignore_acpi_ev = true;
3880 static bool hotkey_notify_usrevent(const u32 hkey,
3882 bool *ignore_acpi_ev)
3884 /* 0x5000-0x5FFF: human interface helpers */
3885 *send_acpi_ev = true;
3886 *ignore_acpi_ev = false;
3889 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3890 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3893 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3894 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3895 tpacpi_input_send_tabletsw();
3896 hotkey_tablet_mode_notify_change();
3897 *send_acpi_ev = false;
3900 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3901 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3902 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3903 /* do not propagate these events */
3904 *ignore_acpi_ev = true;
3912 static void thermal_dump_all_sensors(void);
3913 static void palmsensor_refresh(void);
3915 static bool hotkey_notify_6xxx(const u32 hkey,
3917 bool *ignore_acpi_ev)
3919 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3920 *send_acpi_ev = true;
3921 *ignore_acpi_ev = false;
3924 case TP_HKEY_EV_THM_TABLE_CHANGED:
3925 pr_debug("EC reports: Thermal Table has changed\n");
3926 /* recommended action: do nothing, we don't have
3927 * Lenovo ATM information */
3929 case TP_HKEY_EV_THM_CSM_COMPLETED:
3930 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3931 /* Thermal event - pass on to event handler */
3932 tpacpi_driver_event(hkey);
3934 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3935 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3936 /* recommended action: do nothing, we don't have
3937 * Lenovo ATM information */
3939 case TP_HKEY_EV_ALARM_BAT_HOT:
3940 pr_crit("THERMAL ALARM: battery is too hot!\n");
3941 /* recommended action: warn user through gui */
3943 case TP_HKEY_EV_ALARM_BAT_XHOT:
3944 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3945 /* recommended action: immediate sleep/hibernate */
3947 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3948 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3949 /* recommended action: warn user through gui, that */
3950 /* some internal component is too hot */
3952 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3953 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3954 /* recommended action: immediate sleep/hibernate */
3956 case TP_HKEY_EV_AC_CHANGED:
3957 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3958 * AC status changed; can be triggered by plugging or
3959 * unplugging AC adapter, docking or undocking. */
3963 case TP_HKEY_EV_KEY_NUMLOCK:
3964 case TP_HKEY_EV_KEY_FN:
3965 /* key press events, we just ignore them as long as the EC
3966 * is still reporting them in the normal keyboard stream */
3967 *send_acpi_ev = false;
3968 *ignore_acpi_ev = true;
3971 case TP_HKEY_EV_KEY_FN_ESC:
3972 /* Get the media key status to force the status LED to update */
3973 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3974 *send_acpi_ev = false;
3975 *ignore_acpi_ev = true;
3978 case TP_HKEY_EV_TABLET_CHANGED:
3979 tpacpi_input_send_tabletsw();
3980 hotkey_tablet_mode_notify_change();
3981 *send_acpi_ev = false;
3984 case TP_HKEY_EV_PALM_DETECTED:
3985 case TP_HKEY_EV_PALM_UNDETECTED:
3986 /* palm detected - pass on to event handler */
3987 palmsensor_refresh();
3991 /* report simply as unknown, no sensor dump */
3995 thermal_dump_all_sensors();
3999 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4003 bool ignore_acpi_ev;
4006 if (event != 0x80) {
4007 pr_err("unknown HKEY notification event %d\n", event);
4008 /* forward it to userspace, maybe it knows how to handle it */
4009 acpi_bus_generate_netlink_event(
4010 ibm->acpi->device->pnp.device_class,
4011 dev_name(&ibm->acpi->device->dev),
4017 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4018 pr_err("failed to retrieve HKEY event\n");
4027 send_acpi_ev = true;
4028 ignore_acpi_ev = false;
4030 switch (hkey >> 12) {
4032 /* 0x1000-0x1FFF: key presses */
4033 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4037 /* 0x2000-0x2FFF: Wakeup reason */
4038 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4042 /* 0x3000-0x3FFF: bay-related wakeups */
4044 case TP_HKEY_EV_BAYEJ_ACK:
4045 hotkey_autosleep_ack = 1;
4046 pr_info("bay ejected\n");
4047 hotkey_wakeup_hotunplug_complete_notify_change();
4050 case TP_HKEY_EV_OPTDRV_EJ:
4051 /* FIXME: kick libata if SATA link offline */
4059 /* 0x4000-0x4FFF: dock-related events */
4060 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4064 /* 0x5000-0x5FFF: human interface helpers */
4065 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4069 /* 0x6000-0x6FFF: thermal alarms/notices and
4070 * keyboard events */
4071 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4075 /* 0x7000-0x7FFF: misc */
4076 if (tp_features.hotkey_wlsw &&
4077 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4078 tpacpi_send_radiosw_update();
4083 fallthrough; /* to default */
4088 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4089 pr_notice("please report the conditions when this event happened to %s\n",
4093 /* netlink events */
4094 if (!ignore_acpi_ev && send_acpi_ev) {
4095 acpi_bus_generate_netlink_event(
4096 ibm->acpi->device->pnp.device_class,
4097 dev_name(&ibm->acpi->device->dev),
4103 static void hotkey_suspend(void)
4105 /* Do these on suspend, we get the events on early resume! */
4106 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4107 hotkey_autosleep_ack = 0;
4109 /* save previous mode of adaptive keyboard of X1 Carbon */
4110 if (tp_features.has_adaptive_kbd) {
4111 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4113 pr_err("Cannot read adaptive keyboard mode.\n");
4118 static void hotkey_resume(void)
4120 tpacpi_disable_brightness_delay();
4122 mutex_lock(&hotkey_mutex);
4123 if (hotkey_status_set(true) < 0 ||
4124 hotkey_mask_set(hotkey_acpi_mask) < 0)
4125 pr_err("error while attempting to reset the event firmware interface\n");
4126 mutex_unlock(&hotkey_mutex);
4128 tpacpi_send_radiosw_update();
4129 tpacpi_input_send_tabletsw();
4130 hotkey_tablet_mode_notify_change();
4131 hotkey_wakeup_reason_notify_change();
4132 hotkey_wakeup_hotunplug_complete_notify_change();
4133 hotkey_poll_setup_safe(false);
4135 /* restore previous mode of adapive keyboard of X1 Carbon */
4136 if (tp_features.has_adaptive_kbd) {
4137 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4138 adaptive_keyboard_prev_mode)) {
4139 pr_err("Cannot set adaptive keyboard mode.\n");
4144 /* procfs -------------------------------------------------------------- */
4145 static int hotkey_read(struct seq_file *m)
4149 if (!tp_features.hotkey) {
4150 seq_printf(m, "status:\t\tnot supported\n");
4154 if (mutex_lock_killable(&hotkey_mutex))
4155 return -ERESTARTSYS;
4156 res = hotkey_status_get(&status);
4158 res = hotkey_mask_get();
4159 mutex_unlock(&hotkey_mutex);
4163 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
4164 if (hotkey_all_mask) {
4165 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4166 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4168 seq_printf(m, "mask:\t\tnot supported\n");
4169 seq_printf(m, "commands:\tenable, disable, reset\n");
4175 static void hotkey_enabledisable_warn(bool enable)
4177 tpacpi_log_usertask("procfs hotkey enable/disable");
4178 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4179 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4180 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4183 static int hotkey_write(char *buf)
4189 if (!tp_features.hotkey)
4192 if (mutex_lock_killable(&hotkey_mutex))
4193 return -ERESTARTSYS;
4195 mask = hotkey_user_mask;
4198 while ((cmd = strsep(&buf, ","))) {
4199 if (strstarts(cmd, "enable")) {
4200 hotkey_enabledisable_warn(1);
4201 } else if (strstarts(cmd, "disable")) {
4202 hotkey_enabledisable_warn(0);
4204 } else if (strstarts(cmd, "reset")) {
4205 mask = (hotkey_all_mask | hotkey_source_mask)
4206 & ~hotkey_reserved_mask;
4207 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4209 } else if (sscanf(cmd, "%x", &mask) == 1) {
4218 tpacpi_disclose_usertask("procfs hotkey",
4219 "set mask to 0x%08x\n", mask);
4220 res = hotkey_user_mask_set(mask);
4224 mutex_unlock(&hotkey_mutex);
4228 static const struct acpi_device_id ibm_htk_device_ids[] = {
4229 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4230 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4231 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4235 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4236 .hid = ibm_htk_device_ids,
4237 .notify = hotkey_notify,
4238 .handle = &hkey_handle,
4239 .type = ACPI_DEVICE_NOTIFY,
4242 static struct ibm_struct hotkey_driver_data = {
4244 .read = hotkey_read,
4245 .write = hotkey_write,
4246 .exit = hotkey_exit,
4247 .resume = hotkey_resume,
4248 .suspend = hotkey_suspend,
4249 .acpi = &ibm_hotkey_acpidriver,
4252 /*************************************************************************
4253 * Bluetooth subdriver
4257 /* ACPI GBDC/SBDC bits */
4258 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4259 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4260 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4261 0 = disable, 1 = enable */
4265 /* ACPI \BLTH commands */
4266 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4267 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4268 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4269 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4270 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4273 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4275 static int bluetooth_get_status(void)
4279 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4280 if (dbg_bluetoothemul)
4281 return (tpacpi_bluetooth_emulstate) ?
4282 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4285 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4288 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4289 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4292 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4296 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s bluetooth\n",
4297 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4299 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4300 if (dbg_bluetoothemul) {
4301 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4306 if (state == TPACPI_RFK_RADIO_ON)
4307 status = TP_ACPI_BLUETOOTH_RADIOSSW
4308 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4312 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4318 /* sysfs bluetooth enable ---------------------------------------------- */
4319 static ssize_t bluetooth_enable_show(struct device *dev,
4320 struct device_attribute *attr,
4323 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4327 static ssize_t bluetooth_enable_store(struct device *dev,
4328 struct device_attribute *attr,
4329 const char *buf, size_t count)
4331 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4335 static DEVICE_ATTR_RW(bluetooth_enable);
4337 /* --------------------------------------------------------------------- */
4339 static struct attribute *bluetooth_attributes[] = {
4340 &dev_attr_bluetooth_enable.attr,
4344 static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4345 struct attribute *attr, int n)
4347 return tp_features.bluetooth ? attr->mode : 0;
4350 static const struct attribute_group bluetooth_attr_group = {
4351 .is_visible = bluetooth_attr_is_visible,
4352 .attrs = bluetooth_attributes,
4355 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4356 .get_status = bluetooth_get_status,
4357 .set_status = bluetooth_set_status,
4360 static void bluetooth_shutdown(void)
4362 /* Order firmware to save current state to NVRAM */
4363 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4364 TP_ACPI_BLTH_SAVE_STATE))
4365 pr_notice("failed to save bluetooth state to NVRAM\n");
4367 vdbg_printk(TPACPI_DBG_RFKILL,
4368 "bluetooth state saved to NVRAM\n");
4371 static void bluetooth_exit(void)
4373 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4374 bluetooth_shutdown();
4377 static const struct dmi_system_id fwbug_list[] __initconst = {
4379 .ident = "ThinkPad E485",
4380 .driver_data = &quirk_btusb_bug,
4382 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4383 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4387 .ident = "ThinkPad E585",
4388 .driver_data = &quirk_btusb_bug,
4390 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4391 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4395 .ident = "ThinkPad A285 - 20MW",
4396 .driver_data = &quirk_btusb_bug,
4398 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4399 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4403 .ident = "ThinkPad A285 - 20MX",
4404 .driver_data = &quirk_btusb_bug,
4406 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4407 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4411 .ident = "ThinkPad A485 - 20MU",
4412 .driver_data = &quirk_btusb_bug,
4414 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4415 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4419 .ident = "ThinkPad A485 - 20MV",
4420 .driver_data = &quirk_btusb_bug,
4422 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4423 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4429 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4430 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4431 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4432 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4437 static int __init have_bt_fwbug(void)
4440 * Some AMD based ThinkPads have a firmware bug that calling
4441 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4443 if (tp_features.quirks && tp_features.quirks->btusb_bug &&
4444 pci_dev_present(fwbug_cards_ids)) {
4445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4446 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4452 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4457 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4458 "initializing bluetooth subdriver\n");
4460 TPACPI_ACPIHANDLE_INIT(hkey);
4462 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4463 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4464 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4465 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4467 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4468 "bluetooth is %s, status 0x%02x\n",
4469 str_supported(tp_features.bluetooth),
4472 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4473 if (dbg_bluetoothemul) {
4474 tp_features.bluetooth = 1;
4475 pr_info("bluetooth switch emulation enabled\n");
4478 if (tp_features.bluetooth &&
4479 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4480 /* no bluetooth hardware present in system */
4481 tp_features.bluetooth = 0;
4482 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4483 "bluetooth hardware not installed\n");
4486 if (!tp_features.bluetooth)
4489 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4490 &bluetooth_tprfk_ops,
4491 RFKILL_TYPE_BLUETOOTH,
4492 TPACPI_RFK_BLUETOOTH_SW_NAME,
4497 /* procfs -------------------------------------------------------------- */
4498 static int bluetooth_read(struct seq_file *m)
4500 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4503 static int bluetooth_write(char *buf)
4505 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4508 static struct ibm_struct bluetooth_driver_data = {
4509 .name = "bluetooth",
4510 .read = bluetooth_read,
4511 .write = bluetooth_write,
4512 .exit = bluetooth_exit,
4513 .shutdown = bluetooth_shutdown,
4516 /*************************************************************************
4521 /* ACPI GWAN/SWAN bits */
4522 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4523 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4524 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4525 0 = disable, 1 = enable */
4528 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4530 static int wan_get_status(void)
4534 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4536 return (tpacpi_wwan_emulstate) ?
4537 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4540 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4543 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4544 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4547 static int wan_set_status(enum tpacpi_rfkill_state state)
4551 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
4552 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4554 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4556 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4561 if (state == TPACPI_RFK_RADIO_ON)
4562 status = TP_ACPI_WANCARD_RADIOSSW
4563 | TP_ACPI_WANCARD_RESUMECTRL;
4567 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4573 /* sysfs wan enable ---------------------------------------------------- */
4574 static ssize_t wan_enable_show(struct device *dev,
4575 struct device_attribute *attr,
4578 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4582 static ssize_t wan_enable_store(struct device *dev,
4583 struct device_attribute *attr,
4584 const char *buf, size_t count)
4586 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4590 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4591 wan_enable_show, wan_enable_store);
4593 /* --------------------------------------------------------------------- */
4595 static struct attribute *wan_attributes[] = {
4596 &dev_attr_wwan_enable.attr,
4600 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4603 return tp_features.wan ? attr->mode : 0;
4606 static const struct attribute_group wan_attr_group = {
4607 .is_visible = wan_attr_is_visible,
4608 .attrs = wan_attributes,
4611 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4612 .get_status = wan_get_status,
4613 .set_status = wan_set_status,
4616 static void wan_shutdown(void)
4618 /* Order firmware to save current state to NVRAM */
4619 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4620 TP_ACPI_WGSV_SAVE_STATE))
4621 pr_notice("failed to save WWAN state to NVRAM\n");
4623 vdbg_printk(TPACPI_DBG_RFKILL,
4624 "WWAN state saved to NVRAM\n");
4627 static void wan_exit(void)
4629 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4633 static int __init wan_init(struct ibm_init_struct *iibm)
4638 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4639 "initializing wan subdriver\n");
4641 TPACPI_ACPIHANDLE_INIT(hkey);
4643 tp_features.wan = hkey_handle &&
4644 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4646 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4647 "wan is %s, status 0x%02x\n",
4648 str_supported(tp_features.wan),
4651 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4653 tp_features.wan = 1;
4654 pr_info("wwan switch emulation enabled\n");
4657 if (tp_features.wan &&
4658 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4659 /* no wan hardware present in system */
4660 tp_features.wan = 0;
4661 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4662 "wan hardware not installed\n");
4665 if (!tp_features.wan)
4668 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4671 TPACPI_RFK_WWAN_SW_NAME,
4676 /* procfs -------------------------------------------------------------- */
4677 static int wan_read(struct seq_file *m)
4679 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4682 static int wan_write(char *buf)
4684 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4687 static struct ibm_struct wan_driver_data = {
4692 .shutdown = wan_shutdown,
4695 /*************************************************************************
4700 /* ACPI GUWB/SUWB bits */
4701 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4702 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4705 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4707 static int uwb_get_status(void)
4711 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4713 return (tpacpi_uwb_emulstate) ?
4714 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4717 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4720 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4721 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4724 static int uwb_set_status(enum tpacpi_rfkill_state state)
4728 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
4729 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4731 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4733 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4738 if (state == TPACPI_RFK_RADIO_ON)
4739 status = TP_ACPI_UWB_RADIOSSW;
4743 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4749 /* --------------------------------------------------------------------- */
4751 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4752 .get_status = uwb_get_status,
4753 .set_status = uwb_set_status,
4756 static void uwb_exit(void)
4758 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4761 static int __init uwb_init(struct ibm_init_struct *iibm)
4766 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4767 "initializing uwb subdriver\n");
4769 TPACPI_ACPIHANDLE_INIT(hkey);
4771 tp_features.uwb = hkey_handle &&
4772 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4774 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4775 "uwb is %s, status 0x%02x\n",
4776 str_supported(tp_features.uwb),
4779 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4781 tp_features.uwb = 1;
4782 pr_info("uwb switch emulation enabled\n");
4785 if (tp_features.uwb &&
4786 !(status & TP_ACPI_UWB_HWPRESENT)) {
4787 /* no uwb hardware present in system */
4788 tp_features.uwb = 0;
4789 dbg_printk(TPACPI_DBG_INIT,
4790 "uwb hardware not installed\n");
4793 if (!tp_features.uwb)
4796 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4799 TPACPI_RFK_UWB_SW_NAME,
4804 static struct ibm_struct uwb_driver_data = {
4807 .flags.experimental = 1,
4810 /*************************************************************************
4814 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4816 enum video_access_mode {
4817 TPACPI_VIDEO_NONE = 0,
4818 TPACPI_VIDEO_570, /* 570 */
4819 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4820 TPACPI_VIDEO_NEW, /* all others */
4823 enum { /* video status flags, based on VIDEO_570 */
4824 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4825 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4826 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4829 enum { /* TPACPI_VIDEO_570 constants */
4830 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4831 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4832 * video_status_flags */
4833 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4834 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4837 static enum video_access_mode video_supported;
4838 static int video_orig_autosw;
4840 static int video_autosw_get(void);
4841 static int video_autosw_set(int enable);
4843 TPACPI_HANDLE(vid, root,
4844 "\\_SB.PCI.AGP.VGA", /* 570 */
4845 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4846 "\\_SB.PCI0.VID0", /* 770e */
4847 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4848 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4849 "\\_SB.PCI0.AGP.VID", /* all others */
4852 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4854 static int __init video_init(struct ibm_init_struct *iibm)
4858 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4860 TPACPI_ACPIHANDLE_INIT(vid);
4861 if (tpacpi_is_ibm())
4862 TPACPI_ACPIHANDLE_INIT(vid2);
4864 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4865 /* G41, assume IVGA doesn't change */
4866 vid_handle = vid2_handle;
4869 /* video switching not supported on R30, R31 */
4870 video_supported = TPACPI_VIDEO_NONE;
4871 else if (tpacpi_is_ibm() &&
4872 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4874 video_supported = TPACPI_VIDEO_570;
4875 else if (tpacpi_is_ibm() &&
4876 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4877 /* 600e/x, 770e, 770x */
4878 video_supported = TPACPI_VIDEO_770;
4881 video_supported = TPACPI_VIDEO_NEW;
4883 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4884 str_supported(video_supported != TPACPI_VIDEO_NONE),
4887 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
4890 static void video_exit(void)
4892 dbg_printk(TPACPI_DBG_EXIT,
4893 "restoring original video autoswitch mode\n");
4894 if (video_autosw_set(video_orig_autosw))
4895 pr_err("error while trying to restore original video autoswitch mode\n");
4898 static int video_outputsw_get(void)
4903 switch (video_supported) {
4904 case TPACPI_VIDEO_570:
4905 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4906 TP_ACPI_VIDEO_570_PHSCMD))
4908 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4910 case TPACPI_VIDEO_770:
4911 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4914 status |= TP_ACPI_VIDEO_S_LCD;
4915 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4918 status |= TP_ACPI_VIDEO_S_CRT;
4920 case TPACPI_VIDEO_NEW:
4921 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4922 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4925 status |= TP_ACPI_VIDEO_S_CRT;
4927 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4928 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4931 status |= TP_ACPI_VIDEO_S_LCD;
4932 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4935 status |= TP_ACPI_VIDEO_S_DVI;
4944 static int video_outputsw_set(int status)
4949 switch (video_supported) {
4950 case TPACPI_VIDEO_570:
4951 res = acpi_evalf(NULL, NULL,
4952 "\\_SB.PHS2", "vdd",
4953 TP_ACPI_VIDEO_570_PHS2CMD,
4954 status | TP_ACPI_VIDEO_570_PHS2SET);
4956 case TPACPI_VIDEO_770:
4957 autosw = video_autosw_get();
4961 res = video_autosw_set(1);
4964 res = acpi_evalf(vid_handle, NULL,
4965 "ASWT", "vdd", status * 0x100, 0);
4966 if (!autosw && video_autosw_set(autosw)) {
4967 pr_err("video auto-switch left enabled due to error\n");
4971 case TPACPI_VIDEO_NEW:
4972 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4973 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4979 return (res) ? 0 : -EIO;
4982 static int video_autosw_get(void)
4986 switch (video_supported) {
4987 case TPACPI_VIDEO_570:
4988 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4991 case TPACPI_VIDEO_770:
4992 case TPACPI_VIDEO_NEW:
4993 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5003 static int video_autosw_set(int enable)
5005 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5010 static int video_outputsw_cycle(void)
5012 int autosw = video_autosw_get();
5018 switch (video_supported) {
5019 case TPACPI_VIDEO_570:
5020 res = video_autosw_set(1);
5023 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5025 case TPACPI_VIDEO_770:
5026 case TPACPI_VIDEO_NEW:
5027 res = video_autosw_set(1);
5030 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5035 if (!autosw && video_autosw_set(autosw)) {
5036 pr_err("video auto-switch left enabled due to error\n");
5040 return (res) ? 0 : -EIO;
5043 static int video_expand_toggle(void)
5045 switch (video_supported) {
5046 case TPACPI_VIDEO_570:
5047 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5049 case TPACPI_VIDEO_770:
5050 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5052 case TPACPI_VIDEO_NEW:
5053 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5061 static int video_read(struct seq_file *m)
5065 if (video_supported == TPACPI_VIDEO_NONE) {
5066 seq_printf(m, "status:\t\tnot supported\n");
5070 /* Even reads can crash X.org, so... */
5071 if (!capable(CAP_SYS_ADMIN))
5074 status = video_outputsw_get();
5078 autosw = video_autosw_get();
5082 seq_printf(m, "status:\t\tsupported\n");
5083 seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
5084 seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
5085 if (video_supported == TPACPI_VIDEO_NEW)
5086 seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
5087 seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
5088 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5089 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5090 if (video_supported == TPACPI_VIDEO_NEW)
5091 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5092 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5093 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5098 static int video_write(char *buf)
5101 int enable, disable, status;
5104 if (video_supported == TPACPI_VIDEO_NONE)
5107 /* Even reads can crash X.org, let alone writes... */
5108 if (!capable(CAP_SYS_ADMIN))
5114 while ((cmd = strsep(&buf, ","))) {
5115 if (strstarts(cmd, "lcd_enable")) {
5116 enable |= TP_ACPI_VIDEO_S_LCD;
5117 } else if (strstarts(cmd, "lcd_disable")) {
5118 disable |= TP_ACPI_VIDEO_S_LCD;
5119 } else if (strstarts(cmd, "crt_enable")) {
5120 enable |= TP_ACPI_VIDEO_S_CRT;
5121 } else if (strstarts(cmd, "crt_disable")) {
5122 disable |= TP_ACPI_VIDEO_S_CRT;
5123 } else if (video_supported == TPACPI_VIDEO_NEW &&
5124 strstarts(cmd, "dvi_enable")) {
5125 enable |= TP_ACPI_VIDEO_S_DVI;
5126 } else if (video_supported == TPACPI_VIDEO_NEW &&
5127 strstarts(cmd, "dvi_disable")) {
5128 disable |= TP_ACPI_VIDEO_S_DVI;
5129 } else if (strstarts(cmd, "auto_enable")) {
5130 res = video_autosw_set(1);
5133 } else if (strstarts(cmd, "auto_disable")) {
5134 res = video_autosw_set(0);
5137 } else if (strstarts(cmd, "video_switch")) {
5138 res = video_outputsw_cycle();
5141 } else if (strstarts(cmd, "expand_toggle")) {
5142 res = video_expand_toggle();
5149 if (enable || disable) {
5150 status = video_outputsw_get();
5153 res = video_outputsw_set((status & ~disable) | enable);
5161 static struct ibm_struct video_driver_data = {
5164 .write = video_write,
5168 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5170 /*************************************************************************
5171 * Keyboard backlight subdriver
5174 static enum led_brightness kbdlight_brightness;
5175 static DEFINE_MUTEX(kbdlight_mutex);
5177 static int kbdlight_set_level(int level)
5184 mutex_lock(&kbdlight_mutex);
5186 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5189 kbdlight_brightness = level;
5191 mutex_unlock(&kbdlight_mutex);
5196 static int kbdlight_get_level(void)
5203 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5209 return status & 0x3;
5212 static bool kbdlight_is_supported(void)
5219 if (!acpi_has_method(hkey_handle, "MLCG")) {
5220 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5224 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5225 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5230 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5234 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5236 * Guessed test for keyboard backlight:
5238 * Machines with backlight keyboard return:
5239 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5240 * b110100010010000000XX - ThinkPad x230
5241 * b010100000010000000XX - ThinkPad x240
5242 * b010100000010000000XX - ThinkPad W541
5243 * (XX is current backlight level)
5245 * Machines without backlight keyboard return:
5246 * b10100001000000000000 - ThinkPad x230
5247 * b10110001000000000000 - ThinkPad E430
5248 * b00000000000000000000 - ThinkPad E450
5250 * Candidate BITs for detection test (XOR):
5251 * b01000000001000000000
5254 return status & BIT(9);
5257 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5258 enum led_brightness brightness)
5260 return kbdlight_set_level(brightness);
5263 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5267 level = kbdlight_get_level();
5274 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5276 .name = "tpacpi::kbd_backlight",
5277 .max_brightness = 2,
5278 .flags = LED_BRIGHT_HW_CHANGED,
5279 .brightness_set_blocking = &kbdlight_sysfs_set,
5280 .brightness_get = &kbdlight_sysfs_get,
5284 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5288 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5290 TPACPI_ACPIHANDLE_INIT(hkey);
5292 if (!kbdlight_is_supported()) {
5293 tp_features.kbdlight = 0;
5294 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5298 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5299 tp_features.kbdlight = 1;
5301 rc = led_classdev_register(&tpacpi_pdev->dev,
5302 &tpacpi_led_kbdlight.led_classdev);
5304 tp_features.kbdlight = 0;
5308 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5309 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5313 static void kbdlight_exit(void)
5315 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5318 static int kbdlight_set_level_and_update(int level)
5321 struct led_classdev *led_cdev;
5323 ret = kbdlight_set_level(level);
5324 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5326 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5327 led_cdev->brightness = level;
5332 static int kbdlight_read(struct seq_file *m)
5336 if (!tp_features.kbdlight) {
5337 seq_printf(m, "status:\t\tnot supported\n");
5339 level = kbdlight_get_level();
5341 seq_printf(m, "status:\t\terror %d\n", level);
5343 seq_printf(m, "status:\t\t%d\n", level);
5344 seq_printf(m, "commands:\t0, 1, 2\n");
5350 static int kbdlight_write(char *buf)
5353 int res, level = -EINVAL;
5355 if (!tp_features.kbdlight)
5358 while ((cmd = strsep(&buf, ","))) {
5359 res = kstrtoint(cmd, 10, &level);
5364 if (level >= 3 || level < 0)
5367 return kbdlight_set_level_and_update(level);
5370 static void kbdlight_suspend(void)
5372 struct led_classdev *led_cdev;
5374 if (!tp_features.kbdlight)
5377 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5378 led_update_brightness(led_cdev);
5379 led_classdev_suspend(led_cdev);
5382 static void kbdlight_resume(void)
5384 if (!tp_features.kbdlight)
5387 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5390 static struct ibm_struct kbdlight_driver_data = {
5392 .read = kbdlight_read,
5393 .write = kbdlight_write,
5394 .suspend = kbdlight_suspend,
5395 .resume = kbdlight_resume,
5396 .exit = kbdlight_exit,
5399 /*************************************************************************
5400 * Light (thinklight) subdriver
5403 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5404 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5406 static int light_get_status(void)
5410 if (tp_features.light_status) {
5411 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5419 static int light_set_status(int status)
5423 if (tp_features.light) {
5425 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5427 TP_CMOS_THINKLIGHT_ON :
5428 TP_CMOS_THINKLIGHT_OFF);
5430 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5433 return (rc) ? 0 : -EIO;
5439 static int light_sysfs_set(struct led_classdev *led_cdev,
5440 enum led_brightness brightness)
5442 return light_set_status((brightness != LED_OFF) ?
5443 TPACPI_LED_ON : TPACPI_LED_OFF);
5446 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5448 return (light_get_status() == 1) ? LED_ON : LED_OFF;
5451 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5453 .name = "tpacpi::thinklight",
5454 .max_brightness = 1,
5455 .brightness_set_blocking = &light_sysfs_set,
5456 .brightness_get = &light_sysfs_get,
5460 static int __init light_init(struct ibm_init_struct *iibm)
5464 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5466 if (tpacpi_is_ibm()) {
5467 TPACPI_ACPIHANDLE_INIT(ledb);
5468 TPACPI_ACPIHANDLE_INIT(lght);
5470 TPACPI_ACPIHANDLE_INIT(cmos);
5472 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5473 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5475 if (tp_features.light)
5476 /* light status not supported on
5477 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5478 tp_features.light_status =
5479 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5481 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5482 str_supported(tp_features.light),
5483 str_supported(tp_features.light_status));
5485 if (!tp_features.light)
5488 rc = led_classdev_register(&tpacpi_pdev->dev,
5489 &tpacpi_led_thinklight.led_classdev);
5492 tp_features.light = 0;
5493 tp_features.light_status = 0;
5501 static void light_exit(void)
5503 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5506 static int light_read(struct seq_file *m)
5510 if (!tp_features.light) {
5511 seq_printf(m, "status:\t\tnot supported\n");
5512 } else if (!tp_features.light_status) {
5513 seq_printf(m, "status:\t\tunknown\n");
5514 seq_printf(m, "commands:\ton, off\n");
5516 status = light_get_status();
5519 seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
5520 seq_printf(m, "commands:\ton, off\n");
5526 static int light_write(char *buf)
5531 if (!tp_features.light)
5534 while ((cmd = strsep(&buf, ","))) {
5535 if (strstarts(cmd, "on")) {
5537 } else if (strstarts(cmd, "off")) {
5543 return light_set_status(newstatus);
5546 static struct ibm_struct light_driver_data = {
5549 .write = light_write,
5553 /*************************************************************************
5557 /* sysfs cmos_command -------------------------------------------------- */
5558 static ssize_t cmos_command_store(struct device *dev,
5559 struct device_attribute *attr,
5560 const char *buf, size_t count)
5562 unsigned long cmos_cmd;
5565 if (parse_strtoul(buf, 21, &cmos_cmd))
5568 res = issue_thinkpad_cmos_command(cmos_cmd);
5569 return (res) ? res : count;
5572 static DEVICE_ATTR_WO(cmos_command);
5574 static struct attribute *cmos_attributes[] = {
5575 &dev_attr_cmos_command.attr,
5579 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5580 struct attribute *attr, int n)
5582 return cmos_handle ? attr->mode : 0;
5585 static const struct attribute_group cmos_attr_group = {
5586 .is_visible = cmos_attr_is_visible,
5587 .attrs = cmos_attributes,
5590 /* --------------------------------------------------------------------- */
5592 static int __init cmos_init(struct ibm_init_struct *iibm)
5594 vdbg_printk(TPACPI_DBG_INIT,
5595 "initializing cmos commands subdriver\n");
5597 TPACPI_ACPIHANDLE_INIT(cmos);
5599 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5600 str_supported(cmos_handle != NULL));
5602 return cmos_handle ? 0 : -ENODEV;
5605 static int cmos_read(struct seq_file *m)
5607 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5608 R30, R31, T20-22, X20-21 */
5610 seq_printf(m, "status:\t\tnot supported\n");
5612 seq_printf(m, "status:\t\tsupported\n");
5613 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5619 static int cmos_write(char *buf)
5624 while ((cmd = strsep(&buf, ","))) {
5625 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5626 cmos_cmd >= 0 && cmos_cmd <= 21) {
5631 res = issue_thinkpad_cmos_command(cmos_cmd);
5639 static struct ibm_struct cmos_driver_data = {
5642 .write = cmos_write,
5645 /*************************************************************************
5649 enum led_access_mode {
5650 TPACPI_LED_NONE = 0,
5651 TPACPI_LED_570, /* 570 */
5652 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5653 TPACPI_LED_NEW, /* all others */
5656 enum { /* For TPACPI_LED_OLD */
5657 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5658 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5659 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5662 static enum led_access_mode led_supported;
5664 static acpi_handle led_handle;
5666 #define TPACPI_LED_NUMLEDS 16
5667 static struct tpacpi_led_classdev *tpacpi_leds;
5668 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5669 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5670 /* there's a limit of 19 chars + NULL before 2.6.26 */
5672 "tpacpi:orange:batt",
5673 "tpacpi:green:batt",
5674 "tpacpi::dock_active",
5675 "tpacpi::bay_active",
5676 "tpacpi::dock_batt",
5677 "tpacpi::unknown_led",
5679 "tpacpi::dock_status1",
5680 "tpacpi::dock_status2",
5681 "tpacpi::lid_logo_dot",
5682 "tpacpi::unknown_led3",
5683 "tpacpi::thinkvantage",
5685 #define TPACPI_SAFE_LEDS 0x1481U
5687 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5689 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5692 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5696 static int led_get_status(const unsigned int led)
5699 enum led_status_t led_s;
5701 switch (led_supported) {
5702 case TPACPI_LED_570:
5703 if (!acpi_evalf(ec_handle,
5704 &status, "GLED", "dd", 1 << led))
5706 led_s = (status == 0) ?
5711 tpacpi_led_state_cache[led] = led_s;
5720 static int led_set_status(const unsigned int led,
5721 const enum led_status_t ledstatus)
5723 /* off, on, blink. Index is led_status_t */
5724 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5725 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5729 switch (led_supported) {
5730 case TPACPI_LED_570:
5732 if (unlikely(led > 7))
5734 if (unlikely(tpacpi_is_led_restricted(led)))
5736 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5737 (1 << led), led_sled_arg1[ledstatus]))
5740 case TPACPI_LED_OLD:
5741 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5742 if (unlikely(led > 7))
5744 if (unlikely(tpacpi_is_led_restricted(led)))
5746 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5748 rc = ec_write(TPACPI_LED_EC_HLBL,
5749 (ledstatus == TPACPI_LED_BLINK) << led);
5751 rc = ec_write(TPACPI_LED_EC_HLCL,
5752 (ledstatus != TPACPI_LED_OFF) << led);
5754 case TPACPI_LED_NEW:
5756 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5758 if (unlikely(tpacpi_is_led_restricted(led)))
5760 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5761 led, led_led_arg1[ledstatus]))
5769 tpacpi_led_state_cache[led] = ledstatus;
5774 static int led_sysfs_set(struct led_classdev *led_cdev,
5775 enum led_brightness brightness)
5777 struct tpacpi_led_classdev *data = container_of(led_cdev,
5778 struct tpacpi_led_classdev, led_classdev);
5779 enum led_status_t new_state;
5781 if (brightness == LED_OFF)
5782 new_state = TPACPI_LED_OFF;
5783 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5784 new_state = TPACPI_LED_ON;
5786 new_state = TPACPI_LED_BLINK;
5788 return led_set_status(data->led, new_state);
5791 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5792 unsigned long *delay_on, unsigned long *delay_off)
5794 struct tpacpi_led_classdev *data = container_of(led_cdev,
5795 struct tpacpi_led_classdev, led_classdev);
5797 /* Can we choose the flash rate? */
5798 if (*delay_on == 0 && *delay_off == 0) {
5799 /* yes. set them to the hardware blink rate (1 Hz) */
5800 *delay_on = 500; /* ms */
5801 *delay_off = 500; /* ms */
5802 } else if ((*delay_on != 500) || (*delay_off != 500))
5805 return led_set_status(data->led, TPACPI_LED_BLINK);
5808 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5812 struct tpacpi_led_classdev *data = container_of(led_cdev,
5813 struct tpacpi_led_classdev, led_classdev);
5815 rc = led_get_status(data->led);
5817 if (rc == TPACPI_LED_OFF || rc < 0)
5818 rc = LED_OFF; /* no error handling in led class :( */
5825 static void led_exit(void)
5829 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5830 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5835 static int __init tpacpi_init_led(unsigned int led)
5837 /* LEDs with no name don't get registered */
5838 if (!tpacpi_led_names[led])
5841 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5842 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5843 if (led_supported == TPACPI_LED_570)
5844 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5846 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5847 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5848 tpacpi_leds[led].led = led;
5850 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5853 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5854 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5855 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5856 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5858 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5859 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5860 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5861 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5862 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5863 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5864 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5865 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5867 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5868 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5869 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5870 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5871 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5873 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5874 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5875 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5876 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5878 /* (1) - may have excess leds enabled on MSB */
5880 /* Defaults (order matters, keep last, don't reorder!) */
5882 .vendor = PCI_VENDOR_ID_LENOVO,
5883 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5886 { /* IBM ThinkPads with no EC version string */
5887 .vendor = PCI_VENDOR_ID_IBM,
5888 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5891 { /* IBM ThinkPads with EC version string */
5892 .vendor = PCI_VENDOR_ID_IBM,
5893 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5898 static enum led_access_mode __init led_init_detect_mode(void)
5902 if (tpacpi_is_ibm()) {
5904 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5905 if (ACPI_SUCCESS(status))
5906 return TPACPI_LED_570;
5908 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5909 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5910 if (ACPI_SUCCESS(status))
5911 return TPACPI_LED_OLD;
5915 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5916 if (ACPI_SUCCESS(status))
5917 return TPACPI_LED_NEW;
5919 /* R30, R31, and unknown firmwares */
5921 return TPACPI_LED_NONE;
5924 static int __init led_init(struct ibm_init_struct *iibm)
5928 unsigned long useful_leds;
5930 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5932 led_supported = led_init_detect_mode();
5934 if (led_supported != TPACPI_LED_NONE) {
5935 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5936 ARRAY_SIZE(led_useful_qtable));
5940 led_supported = TPACPI_LED_NONE;
5944 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5945 str_supported(led_supported), led_supported);
5947 if (led_supported == TPACPI_LED_NONE)
5950 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
5953 pr_err("Out of memory for LED data\n");
5957 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5958 tpacpi_leds[i].led = -1;
5960 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
5961 rc = tpacpi_init_led(i);
5969 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5970 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5975 #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
5977 static int led_read(struct seq_file *m)
5979 if (!led_supported) {
5980 seq_printf(m, "status:\t\tnot supported\n");
5983 seq_printf(m, "status:\t\tsupported\n");
5985 if (led_supported == TPACPI_LED_570) {
5988 for (i = 0; i < 8; i++) {
5989 status = led_get_status(i);
5992 seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
5996 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6001 static int led_write(char *buf)
6005 enum led_status_t s;
6010 while ((cmd = strsep(&buf, ","))) {
6011 if (sscanf(cmd, "%d", &led) != 1)
6014 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6017 if (tpacpi_leds[led].led < 0)
6020 if (strstr(cmd, "off")) {
6022 } else if (strstr(cmd, "on")) {
6024 } else if (strstr(cmd, "blink")) {
6025 s = TPACPI_LED_BLINK;
6030 rc = led_set_status(led, s);
6038 static struct ibm_struct led_driver_data = {
6045 /*************************************************************************
6049 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6051 #define TPACPI_BEEP_Q1 0x0001
6053 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6054 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6055 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6058 static int __init beep_init(struct ibm_init_struct *iibm)
6060 unsigned long quirks;
6062 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6064 TPACPI_ACPIHANDLE_INIT(beep);
6066 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6067 str_supported(beep_handle != NULL));
6069 quirks = tpacpi_check_quirks(beep_quirk_table,
6070 ARRAY_SIZE(beep_quirk_table));
6072 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6074 return (beep_handle) ? 0 : -ENODEV;
6077 static int beep_read(struct seq_file *m)
6080 seq_printf(m, "status:\t\tnot supported\n");
6082 seq_printf(m, "status:\t\tsupported\n");
6083 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6089 static int beep_write(char *buf)
6097 while ((cmd = strsep(&buf, ","))) {
6098 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6099 beep_cmd >= 0 && beep_cmd <= 17) {
6103 if (tp_features.beep_needs_two_args) {
6104 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6108 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6117 static struct ibm_struct beep_driver_data = {
6120 .write = beep_write,
6123 /*************************************************************************
6127 enum thermal_access_mode {
6128 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6129 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6130 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6131 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6132 TPACPI_THERMAL_TPEC_12, /* Use ACPI EC regs, 12 sensors */
6133 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6136 enum { /* TPACPI_THERMAL_TPEC_* */
6137 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6138 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6139 TP_EC_THERMAL_TMP0_NS = 0xA8, /* ACPI EC Non-Standard regs TMP 0..7 */
6140 TP_EC_THERMAL_TMP8_NS = 0xB8, /* ACPI EC Non-standard regs TMP 8..11 */
6141 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6142 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6144 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6148 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6149 struct ibm_thermal_sensors_struct {
6150 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6153 static const struct tpacpi_quirk thermal_quirk_table[] __initconst = {
6154 /* Non-standard address for thermal registers on some ThinkPads */
6155 TPACPI_Q_LNV3('R', '1', 'F', true), /* L13 Yoga Gen 2 */
6156 TPACPI_Q_LNV3('N', '2', 'U', true), /* X13 Yoga Gen 2*/
6157 TPACPI_Q_LNV3('R', '0', 'R', true), /* L380 */
6158 TPACPI_Q_LNV3('R', '1', '5', true), /* L13 Yoga Gen 1*/
6159 TPACPI_Q_LNV3('R', '1', '0', true), /* L390 */
6160 TPACPI_Q_LNV3('N', '2', 'L', true), /* X13 Yoga Gen 1*/
6161 TPACPI_Q_LNV3('R', '0', 'T', true), /* 11e Gen5 GL*/
6162 TPACPI_Q_LNV3('R', '1', 'D', true), /* 11e Gen5 GL-R*/
6163 TPACPI_Q_LNV3('R', '0', 'V', true), /* 11e Gen5 KL-Y*/
6166 static enum thermal_access_mode thermal_read_mode;
6167 static bool thermal_use_labels;
6168 static bool thermal_with_ns_address; /* Non-standard thermal reg address */
6170 /* Function to check thermal read mode */
6171 static enum thermal_access_mode __init thermal_read_mode_check(void)
6173 u8 t, ta1, ta2, ver = 0;
6177 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6179 if (thinkpad_id.ec_model) {
6181 * Direct EC access mode: sensors at registers 0x78-0x7F,
6182 * 0xC0-0xC7. Registers return 0x00 for non-implemented,
6183 * thermal sensors return 0x80 when not available.
6185 * In some special cases (when Power Supply ID is 0xC2)
6186 * above rule causes thermal control issues. Offset 0xEF
6187 * determines EC version. 0xC0-0xC7 are not thermal registers
6190 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6191 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6193 /* Quirks to check non-standard EC */
6194 thermal_with_ns_address = tpacpi_check_quirks(thermal_quirk_table,
6195 ARRAY_SIZE(thermal_quirk_table));
6197 /* Support for Thinkpads with non-standard address */
6198 if (thermal_with_ns_address) {
6199 pr_info("ECFW with non-standard thermal registers found\n");
6200 return TPACPI_THERMAL_TPEC_12;
6204 for (i = 0; i < 8; i++) {
6205 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6212 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6222 /* This is sheer paranoia, but we handle it anyway */
6224 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6225 return TPACPI_THERMAL_ACPI_TMP07;
6227 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6228 return TPACPI_THERMAL_NONE;
6232 thermal_use_labels = true;
6233 return TPACPI_THERMAL_TPEC_8;
6236 return (ta2 != 0) ? TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6240 if (tpacpi_is_ibm() && acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6241 /* 600e/x, 770e, 770x */
6242 return TPACPI_THERMAL_ACPI_UPDT;
6244 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6245 return TPACPI_THERMAL_ACPI_TMP07;
6248 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6249 return TPACPI_THERMAL_NONE;
6252 /* idx is zero-based */
6253 static int thermal_get_sensor(int idx, s32 *value)
6259 t = TP_EC_THERMAL_TMP0;
6261 switch (thermal_read_mode) {
6262 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6263 case TPACPI_THERMAL_TPEC_16:
6264 if (idx >= 8 && idx <= 15) {
6265 t = TP_EC_THERMAL_TMP8;
6270 case TPACPI_THERMAL_TPEC_8:
6272 if (!acpi_ec_read(t + idx, &tmp))
6274 *value = tmp * 1000;
6279 /* The Non-standard EC uses 12 Thermal areas */
6280 case TPACPI_THERMAL_TPEC_12:
6284 t = idx < 8 ? TP_EC_THERMAL_TMP0_NS + idx :
6285 TP_EC_THERMAL_TMP8_NS + (idx - 8);
6287 if (!acpi_ec_read(t, &tmp))
6290 *value = tmp * MILLIDEGREE_PER_DEGREE;
6293 case TPACPI_THERMAL_ACPI_UPDT:
6295 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6296 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6298 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6300 *value = (t - 2732) * 100;
6305 case TPACPI_THERMAL_ACPI_TMP07:
6307 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6308 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6310 if (t > 127 || t < -127)
6311 t = TP_EC_THERMAL_TMP_NA;
6317 case TPACPI_THERMAL_NONE:
6325 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6332 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6334 else if (thermal_read_mode == TPACPI_THERMAL_TPEC_12)
6339 for (i = 0 ; i < n; i++) {
6340 res = thermal_get_sensor(i, &s->temp[i]);
6348 static void thermal_dump_all_sensors(void)
6351 struct ibm_thermal_sensors_struct t;
6353 n = thermal_get_sensors(&t);
6357 pr_notice("temperatures (Celsius):");
6359 for (i = 0; i < n; i++) {
6360 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6361 pr_cont(" %d", (int)(t.temp[i] / 1000));
6369 /* sysfs temp##_input -------------------------------------------------- */
6371 static ssize_t thermal_temp_input_show(struct device *dev,
6372 struct device_attribute *attr,
6375 struct sensor_device_attribute *sensor_attr =
6376 to_sensor_dev_attr(attr);
6377 int idx = sensor_attr->index;
6381 res = thermal_get_sensor(idx, &value);
6384 if (value == TPACPI_THERMAL_SENSOR_NA)
6387 return sysfs_emit(buf, "%d\n", value);
6390 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6391 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6392 thermal_temp_input_show, NULL, _idxB)
6394 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6395 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6396 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6397 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6398 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6399 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6400 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6401 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6402 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6403 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6404 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6405 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6406 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6407 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6408 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6409 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6410 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6413 #define THERMAL_ATTRS(X) \
6414 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6416 static struct attribute *thermal_temp_input_attr[] = {
6436 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
6438 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6439 struct attribute *attr, int n)
6441 struct device_attribute *dev_attr = to_dev_attr(attr);
6442 struct sensor_device_attribute *sensor_attr =
6443 to_sensor_dev_attr(dev_attr);
6445 int idx = sensor_attr->index;
6447 switch (thermal_read_mode) {
6448 case TPACPI_THERMAL_NONE:
6451 case TPACPI_THERMAL_ACPI_TMP07:
6452 case TPACPI_THERMAL_ACPI_UPDT:
6453 case TPACPI_THERMAL_TPEC_8:
6458 case TPACPI_THERMAL_TPEC_12:
6471 static const struct attribute_group thermal_attr_group = {
6472 .is_visible = thermal_attr_is_visible,
6473 .attrs = thermal_temp_input_attr,
6476 #undef THERMAL_SENSOR_ATTR_TEMP
6477 #undef THERMAL_ATTRS
6479 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6481 return sysfs_emit(buf, "CPU\n");
6483 static DEVICE_ATTR_RO(temp1_label);
6485 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6487 return sysfs_emit(buf, "GPU\n");
6489 static DEVICE_ATTR_RO(temp2_label);
6491 static struct attribute *temp_label_attributes[] = {
6492 &dev_attr_temp1_label.attr,
6493 &dev_attr_temp2_label.attr,
6497 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6498 struct attribute *attr, int n)
6500 return thermal_use_labels ? attr->mode : 0;
6503 static const struct attribute_group temp_label_attr_group = {
6504 .is_visible = temp_label_attr_is_visible,
6505 .attrs = temp_label_attributes,
6508 /* --------------------------------------------------------------------- */
6510 static int __init thermal_init(struct ibm_init_struct *iibm)
6512 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6514 thermal_read_mode = thermal_read_mode_check();
6516 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6517 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6520 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6523 static int thermal_read(struct seq_file *m)
6526 struct ibm_thermal_sensors_struct t;
6528 n = thermal_get_sensors(&t);
6529 if (unlikely(n < 0))
6532 seq_printf(m, "temperatures:\t");
6535 for (i = 0; i < (n - 1); i++)
6536 seq_printf(m, "%d ", t.temp[i] / 1000);
6537 seq_printf(m, "%d\n", t.temp[i] / 1000);
6539 seq_printf(m, "not supported\n");
6544 static struct ibm_struct thermal_driver_data = {
6546 .read = thermal_read,
6549 /*************************************************************************
6550 * Backlight/brightness subdriver
6553 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6556 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6557 * CMOS NVRAM byte 0x5E, bits 0-3.
6559 * EC HBRV (0x31) has the following layout
6560 * Bit 7: unknown function
6561 * Bit 6: unknown function
6562 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6563 * Bit 4: must be set to zero to avoid problems
6564 * Bit 3-0: backlight brightness level
6566 * brightness_get_raw returns status data in the HBRV layout
6568 * WARNING: The X61 has been verified to use HBRV for something else, so
6569 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6570 * testing on the very early *60 Lenovo models...
6574 TP_EC_BACKLIGHT = 0x31,
6576 /* TP_EC_BACKLIGHT bitmasks */
6577 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6578 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6579 TP_EC_BACKLIGHT_MAPSW = 0x20,
6582 enum tpacpi_brightness_access_mode {
6583 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6584 TPACPI_BRGHT_MODE_EC, /* EC control */
6585 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6586 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6587 TPACPI_BRGHT_MODE_MAX
6590 static struct backlight_device *ibm_backlight_device;
6592 static enum tpacpi_brightness_access_mode brightness_mode =
6593 TPACPI_BRGHT_MODE_MAX;
6595 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6597 static struct mutex brightness_mutex;
6599 /* NVRAM brightness access */
6600 static unsigned int tpacpi_brightness_nvram_get(void)
6604 lockdep_assert_held(&brightness_mutex);
6606 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6607 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6608 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6609 lnvram &= bright_maxlvl;
6614 static void tpacpi_brightness_checkpoint_nvram(void)
6619 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6622 vdbg_printk(TPACPI_DBG_BRGHT,
6623 "trying to checkpoint backlight level to NVRAM...\n");
6625 if (mutex_lock_killable(&brightness_mutex) < 0)
6628 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6630 lec &= TP_EC_BACKLIGHT_LVLMSK;
6631 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6633 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6634 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6635 /* NVRAM needs update */
6636 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6637 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6639 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6640 dbg_printk(TPACPI_DBG_BRGHT,
6641 "updated NVRAM backlight level to %u (0x%02x)\n",
6642 (unsigned int) lec, (unsigned int) b_nvram);
6644 vdbg_printk(TPACPI_DBG_BRGHT,
6645 "NVRAM backlight level already is %u (0x%02x)\n",
6646 (unsigned int) lec, (unsigned int) b_nvram);
6649 mutex_unlock(&brightness_mutex);
6653 static int tpacpi_brightness_get_raw(int *status)
6657 lockdep_assert_held(&brightness_mutex);
6659 switch (brightness_mode) {
6660 case TPACPI_BRGHT_MODE_UCMS_STEP:
6661 *status = tpacpi_brightness_nvram_get();
6663 case TPACPI_BRGHT_MODE_EC:
6664 case TPACPI_BRGHT_MODE_ECNVRAM:
6665 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6674 /* do NOT call with illegal backlight level value */
6675 static int tpacpi_brightness_set_ec(unsigned int value)
6679 lockdep_assert_held(&brightness_mutex);
6681 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6684 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6685 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6686 (value & TP_EC_BACKLIGHT_LVLMSK))))
6692 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6695 unsigned int current_value, i;
6697 lockdep_assert_held(&brightness_mutex);
6699 current_value = tpacpi_brightness_nvram_get();
6701 if (value == current_value)
6704 cmos_cmd = (value > current_value) ?
6705 TP_CMOS_BRIGHTNESS_UP :
6706 TP_CMOS_BRIGHTNESS_DOWN;
6707 inc = (value > current_value) ? 1 : -1;
6709 for (i = current_value; i != value; i += inc)
6710 if (issue_thinkpad_cmos_command(cmos_cmd))
6716 /* May return EINTR which can always be mapped to ERESTARTSYS */
6717 static int brightness_set(unsigned int value)
6721 if (value > bright_maxlvl)
6724 vdbg_printk(TPACPI_DBG_BRGHT,
6725 "set backlight level to %d\n", value);
6727 res = mutex_lock_killable(&brightness_mutex);
6731 switch (brightness_mode) {
6732 case TPACPI_BRGHT_MODE_EC:
6733 case TPACPI_BRGHT_MODE_ECNVRAM:
6734 res = tpacpi_brightness_set_ec(value);
6736 case TPACPI_BRGHT_MODE_UCMS_STEP:
6737 res = tpacpi_brightness_set_ucmsstep(value);
6743 mutex_unlock(&brightness_mutex);
6747 /* sysfs backlight class ----------------------------------------------- */
6749 static int brightness_update_status(struct backlight_device *bd)
6751 int level = backlight_get_brightness(bd);
6753 dbg_printk(TPACPI_DBG_BRGHT,
6754 "backlight: attempt to set level to %d\n",
6757 /* it is the backlight class's job (caller) to handle
6758 * EINTR and other errors properly */
6759 return brightness_set(level);
6762 static int brightness_get(struct backlight_device *bd)
6766 res = mutex_lock_killable(&brightness_mutex);
6770 res = tpacpi_brightness_get_raw(&status);
6772 mutex_unlock(&brightness_mutex);
6777 return status & TP_EC_BACKLIGHT_LVLMSK;
6780 static void tpacpi_brightness_notify_change(void)
6782 backlight_force_update(ibm_backlight_device,
6783 BACKLIGHT_UPDATE_HOTKEY);
6786 static const struct backlight_ops ibm_backlight_data = {
6787 .get_brightness = brightness_get,
6788 .update_status = brightness_update_status,
6791 /* --------------------------------------------------------------------- */
6793 static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
6795 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6796 union acpi_object *obj;
6800 status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
6801 if (ACPI_FAILURE(status))
6804 obj = buffer.pointer;
6805 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
6806 acpi_handle_info(adev->handle,
6807 "Unknown _BCL data, please report this to %s\n",
6811 rc = obj->package.count;
6819 * Call _BCL method of video device. On some ThinkPads this will
6820 * switch the firmware to the ACPI brightness control mode.
6823 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6825 struct acpi_device *device;
6827 device = acpi_fetch_acpi_dev(handle);
6831 return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
6836 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6838 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6840 acpi_handle video_device;
6843 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6845 bcl_levels = tpacpi_query_bcl_levels(video_device);
6847 tp_features.bright_acpimode = (bcl_levels > 0);
6849 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6853 * These are only useful for models that have only one possibility
6854 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6857 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6858 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6859 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6861 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6862 /* Models with ATI GPUs known to require ECNVRAM mode */
6863 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6865 /* Models with ATI GPUs that can use ECNVRAM */
6866 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6867 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6868 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6869 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6871 /* Models with Intel Extreme Graphics 2 */
6872 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6873 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6874 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6876 /* Models with Intel GMA900 */
6877 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6878 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6879 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6883 * Returns < 0 for error, otherwise sets tp_features.bright_*
6884 * and bright_maxlvl.
6886 static void __init tpacpi_detect_brightness_capabilities(void)
6890 vdbg_printk(TPACPI_DBG_INIT,
6891 "detecting firmware brightness interface capabilities\n");
6893 /* we could run a quirks check here (same table used by
6894 * brightness_init) if needed */
6897 * We always attempt to detect acpi support, so as to switch
6898 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6899 * going to publish a backlight interface
6901 b = tpacpi_check_std_acpi_brightness_support();
6911 tp_features.bright_unkfw = 1;
6912 bright_maxlvl = b - 1;
6914 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6917 static int __init brightness_init(struct ibm_init_struct *iibm)
6919 struct backlight_properties props;
6921 unsigned long quirks;
6923 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6925 mutex_init(&brightness_mutex);
6927 quirks = tpacpi_check_quirks(brightness_quirk_table,
6928 ARRAY_SIZE(brightness_quirk_table));
6930 /* tpacpi_detect_brightness_capabilities() must have run already */
6932 /* if it is unknown, we don't handle it: it wouldn't be safe */
6933 if (tp_features.bright_unkfw)
6936 if (!brightness_enable) {
6937 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6938 "brightness support disabled by module parameter\n");
6942 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6943 if (brightness_enable > 1) {
6944 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6946 } else if (brightness_enable == 1) {
6947 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6950 } else if (!tp_features.bright_acpimode) {
6951 pr_notice("ACPI backlight interface not available\n");
6955 pr_notice("ACPI native brightness control enabled\n");
6958 * Check for module parameter bogosity, note that we
6959 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6960 * able to detect "unspecified"
6962 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6965 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6966 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6967 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6968 if (quirks & TPACPI_BRGHT_Q_EC)
6969 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6971 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6973 dbg_printk(TPACPI_DBG_BRGHT,
6974 "driver auto-selected brightness_mode=%d\n",
6979 if (!tpacpi_is_ibm() &&
6980 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6981 brightness_mode == TPACPI_BRGHT_MODE_EC))
6984 if (tpacpi_brightness_get_raw(&b) < 0)
6987 memset(&props, 0, sizeof(struct backlight_properties));
6988 props.type = BACKLIGHT_PLATFORM;
6989 props.max_brightness = bright_maxlvl;
6990 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6991 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6993 &ibm_backlight_data,
6995 if (IS_ERR(ibm_backlight_device)) {
6996 int rc = PTR_ERR(ibm_backlight_device);
6997 ibm_backlight_device = NULL;
6998 pr_err("Could not register backlight device\n");
7001 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7002 "brightness is supported\n");
7004 if (quirks & TPACPI_BRGHT_Q_ASK) {
7005 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7007 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7011 /* Added by mistake in early 2007. Probably useless, but it could
7012 * be working around some unknown firmware problem where the value
7013 * read at startup doesn't match the real hardware state... so leave
7014 * it in place just in case */
7015 backlight_update_status(ibm_backlight_device);
7017 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7018 "brightness: registering brightness hotkeys as change notification\n");
7019 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7020 | TP_ACPI_HKEY_BRGHTUP_MASK
7021 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7025 static void brightness_suspend(void)
7027 tpacpi_brightness_checkpoint_nvram();
7030 static void brightness_shutdown(void)
7032 tpacpi_brightness_checkpoint_nvram();
7035 static void brightness_exit(void)
7037 if (ibm_backlight_device) {
7038 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7039 "calling backlight_device_unregister()\n");
7040 backlight_device_unregister(ibm_backlight_device);
7043 tpacpi_brightness_checkpoint_nvram();
7046 static int brightness_read(struct seq_file *m)
7050 level = brightness_get(NULL);
7052 seq_printf(m, "level:\t\tunreadable\n");
7054 seq_printf(m, "level:\t\t%d\n", level);
7055 seq_printf(m, "commands:\tup, down\n");
7056 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7063 static int brightness_write(char *buf)
7069 level = brightness_get(NULL);
7073 while ((cmd = strsep(&buf, ","))) {
7074 if (strstarts(cmd, "up")) {
7075 if (level < bright_maxlvl)
7077 } else if (strstarts(cmd, "down")) {
7080 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7081 level >= 0 && level <= bright_maxlvl) {
7087 tpacpi_disclose_usertask("procfs brightness",
7088 "set level to %d\n", level);
7091 * Now we know what the final level should be, so we try to set it.
7092 * Doing it this way makes the syscall restartable in case of EINTR
7094 rc = brightness_set(level);
7095 if (!rc && ibm_backlight_device)
7096 backlight_force_update(ibm_backlight_device,
7097 BACKLIGHT_UPDATE_SYSFS);
7098 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7101 static struct ibm_struct brightness_driver_data = {
7102 .name = "brightness",
7103 .read = brightness_read,
7104 .write = brightness_write,
7105 .exit = brightness_exit,
7106 .suspend = brightness_suspend,
7107 .shutdown = brightness_shutdown,
7110 /*************************************************************************
7115 * IBM ThinkPads have a simple volume controller with MUTE gating.
7116 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7118 * Since the *61 series (and probably also the later *60 series), Lenovo
7119 * ThinkPads only implement the MUTE gate.
7122 * Bit 6: MUTE (1 mutes sound)
7124 * Other bits should be zero as far as we know.
7126 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7127 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7128 * such as bit 7 which is used to detect repeated presses of MUTE,
7129 * and we leave them unchanged.
7131 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7132 * in response to user input. Unfortunately, this rarely works well.
7133 * The laptop changes the state of its internal MUTE gate and, on some
7134 * models, sends KEY_MUTE, causing any user code that responds to the
7135 * mute button to get confused. The hardware MUTE gate is also
7136 * unnecessary, since user code can handle the mute button without
7137 * kernel or EC help.
7139 * To avoid confusing userspace, we simply disable all EC-based mute
7140 * and volume controls when possible.
7143 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7145 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7146 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7147 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7149 #if SNDRV_CARDS <= 32
7150 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7152 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7154 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7155 static char *alsa_id = "ThinkPadEC";
7156 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7158 struct tpacpi_alsa_data {
7159 struct snd_card *card;
7160 struct snd_ctl_elem_id *ctl_mute_id;
7161 struct snd_ctl_elem_id *ctl_vol_id;
7164 static struct snd_card *alsa_card;
7169 /* TP_EC_AUDIO bits */
7170 TP_EC_AUDIO_MUTESW = 6,
7172 /* TP_EC_AUDIO bitmasks */
7173 TP_EC_AUDIO_LVL_MSK = 0x0F,
7174 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7176 /* Maximum volume */
7177 TP_EC_VOLUME_MAX = 14,
7180 enum tpacpi_volume_access_mode {
7181 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7182 TPACPI_VOL_MODE_EC, /* Pure EC control */
7183 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7184 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7188 enum tpacpi_volume_capabilities {
7189 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7190 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7191 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7195 enum tpacpi_mute_btn_mode {
7196 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7197 /* We don't know what mode 1 is. */
7198 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7199 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7202 static enum tpacpi_volume_access_mode volume_mode =
7203 TPACPI_VOL_MODE_MAX;
7205 static enum tpacpi_volume_capabilities volume_capabilities;
7206 static bool volume_control_allowed;
7207 static bool software_mute_requested = true;
7208 static bool software_mute_active;
7209 static int software_mute_orig_mode;
7212 * Used to syncronize writers to TP_EC_AUDIO and
7213 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7215 static struct mutex volume_mutex;
7217 static void tpacpi_volume_checkpoint_nvram(void)
7223 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7225 if (!volume_control_allowed)
7227 if (software_mute_active)
7230 vdbg_printk(TPACPI_DBG_MIXER,
7231 "trying to checkpoint mixer state to NVRAM...\n");
7233 if (tp_features.mixer_no_level_control)
7234 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7236 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7238 if (mutex_lock_killable(&volume_mutex) < 0)
7241 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7244 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7246 if (lec != (b_nvram & ec_mask)) {
7247 /* NVRAM needs update */
7248 b_nvram &= ~ec_mask;
7250 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7251 dbg_printk(TPACPI_DBG_MIXER,
7252 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7253 (unsigned int) lec, (unsigned int) b_nvram);
7255 vdbg_printk(TPACPI_DBG_MIXER,
7256 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7257 (unsigned int) lec, (unsigned int) b_nvram);
7261 mutex_unlock(&volume_mutex);
7264 static int volume_get_status_ec(u8 *status)
7268 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7273 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7278 static int volume_get_status(u8 *status)
7280 return volume_get_status_ec(status);
7283 static int volume_set_status_ec(const u8 status)
7285 if (!acpi_ec_write(TP_EC_AUDIO, status))
7288 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7291 * On X200s, and possibly on others, it can take a while for
7292 * reads to become correct.
7299 static int volume_set_status(const u8 status)
7301 return volume_set_status_ec(status);
7304 /* returns < 0 on error, 0 on no change, 1 on change */
7305 static int __volume_set_mute_ec(const bool mute)
7310 if (mutex_lock_killable(&volume_mutex) < 0)
7313 rc = volume_get_status_ec(&s);
7317 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7318 s & ~TP_EC_AUDIO_MUTESW_MSK;
7321 rc = volume_set_status_ec(n);
7327 mutex_unlock(&volume_mutex);
7331 static int volume_alsa_set_mute(const bool mute)
7333 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7334 (mute) ? "" : "un");
7335 return __volume_set_mute_ec(mute);
7338 static int volume_set_mute(const bool mute)
7342 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7343 (mute) ? "" : "un");
7345 rc = __volume_set_mute_ec(mute);
7346 return (rc < 0) ? rc : 0;
7349 /* returns < 0 on error, 0 on no change, 1 on change */
7350 static int __volume_set_volume_ec(const u8 vol)
7355 if (vol > TP_EC_VOLUME_MAX)
7358 if (mutex_lock_killable(&volume_mutex) < 0)
7361 rc = volume_get_status_ec(&s);
7365 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7368 rc = volume_set_status_ec(n);
7374 mutex_unlock(&volume_mutex);
7378 static int volume_set_software_mute(bool startup)
7382 if (!tpacpi_is_lenovo())
7386 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7390 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7391 "Initial HAUM setting was %d\n",
7392 software_mute_orig_mode);
7395 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7396 (int)TP_EC_MUTE_BTN_NONE))
7399 if (result != TP_EC_MUTE_BTN_NONE)
7400 pr_warn("Unexpected SAUM result %d\n",
7404 * In software mute mode, the standard codec controls take
7405 * precendence, so we unmute the ThinkPad HW switch at
7406 * startup. Just on case there are SAUM-capable ThinkPads
7407 * with level controls, set max HW volume as well.
7409 if (tp_features.mixer_no_level_control)
7410 result = volume_set_mute(false);
7412 result = volume_set_status(TP_EC_VOLUME_MAX);
7415 pr_warn("Failed to unmute the HW mute switch\n");
7420 static void volume_exit_software_mute(void)
7424 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7425 || r != software_mute_orig_mode)
7426 pr_warn("Failed to restore mute mode\n");
7429 static int volume_alsa_set_volume(const u8 vol)
7431 dbg_printk(TPACPI_DBG_MIXER,
7432 "ALSA: trying to set volume level to %hu\n", vol);
7433 return __volume_set_volume_ec(vol);
7436 static void volume_alsa_notify_change(void)
7438 struct tpacpi_alsa_data *d;
7440 if (alsa_card && alsa_card->private_data) {
7441 d = alsa_card->private_data;
7443 snd_ctl_notify(alsa_card,
7444 SNDRV_CTL_EVENT_MASK_VALUE,
7447 snd_ctl_notify(alsa_card,
7448 SNDRV_CTL_EVENT_MASK_VALUE,
7453 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7454 struct snd_ctl_elem_info *uinfo)
7456 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7458 uinfo->value.integer.min = 0;
7459 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7463 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7464 struct snd_ctl_elem_value *ucontrol)
7469 rc = volume_get_status(&s);
7473 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7477 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7478 struct snd_ctl_elem_value *ucontrol)
7480 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7481 ucontrol->value.integer.value[0]);
7482 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7485 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7487 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7488 struct snd_ctl_elem_value *ucontrol)
7493 rc = volume_get_status(&s);
7497 ucontrol->value.integer.value[0] =
7498 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7502 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7503 struct snd_ctl_elem_value *ucontrol)
7505 tpacpi_disclose_usertask("ALSA", "%smute\n",
7506 ucontrol->value.integer.value[0] ?
7508 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7511 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7512 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7513 .name = "Console Playback Volume",
7515 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7516 .info = volume_alsa_vol_info,
7517 .get = volume_alsa_vol_get,
7520 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7521 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7522 .name = "Console Playback Switch",
7524 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7525 .info = volume_alsa_mute_info,
7526 .get = volume_alsa_mute_get,
7529 static void volume_suspend(void)
7531 tpacpi_volume_checkpoint_nvram();
7534 static void volume_resume(void)
7536 if (software_mute_active) {
7537 if (volume_set_software_mute(false) < 0)
7538 pr_warn("Failed to restore software mute\n");
7540 volume_alsa_notify_change();
7544 static void volume_shutdown(void)
7546 tpacpi_volume_checkpoint_nvram();
7549 static void volume_exit(void)
7552 snd_card_free(alsa_card);
7556 tpacpi_volume_checkpoint_nvram();
7558 if (software_mute_active)
7559 volume_exit_software_mute();
7562 static int __init volume_create_alsa_mixer(void)
7564 struct snd_card *card;
7565 struct tpacpi_alsa_data *data;
7566 struct snd_kcontrol *ctl_vol;
7567 struct snd_kcontrol *ctl_mute;
7570 rc = snd_card_new(&tpacpi_pdev->dev,
7571 alsa_index, alsa_id, THIS_MODULE,
7572 sizeof(struct tpacpi_alsa_data), &card);
7573 if (rc < 0 || !card) {
7574 pr_err("Failed to create ALSA card structures: %d\n", rc);
7578 BUG_ON(!card->private_data);
7579 data = card->private_data;
7582 strscpy(card->driver, TPACPI_ALSA_DRVNAME,
7583 sizeof(card->driver));
7584 strscpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7585 sizeof(card->shortname));
7586 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7587 (thinkpad_id.ec_version_str) ?
7588 thinkpad_id.ec_version_str : "(unknown)");
7589 snprintf(card->longname, sizeof(card->longname),
7590 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7591 (thinkpad_id.ec_version_str) ?
7592 thinkpad_id.ec_version_str : "unknown");
7594 if (volume_control_allowed) {
7595 volume_alsa_control_vol.put = volume_alsa_vol_put;
7596 volume_alsa_control_vol.access =
7597 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7599 volume_alsa_control_mute.put = volume_alsa_mute_put;
7600 volume_alsa_control_mute.access =
7601 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7604 if (!tp_features.mixer_no_level_control) {
7605 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7606 rc = snd_ctl_add(card, ctl_vol);
7608 pr_err("Failed to create ALSA volume control: %d\n",
7612 data->ctl_vol_id = &ctl_vol->id;
7615 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7616 rc = snd_ctl_add(card, ctl_mute);
7618 pr_err("Failed to create ALSA mute control: %d\n", rc);
7621 data->ctl_mute_id = &ctl_mute->id;
7623 rc = snd_card_register(card);
7625 pr_err("Failed to register ALSA card: %d\n", rc);
7633 snd_card_free(card);
7637 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7638 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7640 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7641 /* Whitelist volume level on all IBM by default */
7642 { .vendor = PCI_VENDOR_ID_IBM,
7643 .bios = TPACPI_MATCH_ANY,
7644 .ec = TPACPI_MATCH_ANY,
7645 .quirks = TPACPI_VOL_Q_LEVEL },
7647 /* Lenovo models with volume control (needs confirmation) */
7648 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7649 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7650 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7651 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7652 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7653 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7654 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7656 /* Whitelist mute-only on all Lenovo by default */
7657 { .vendor = PCI_VENDOR_ID_LENOVO,
7658 .bios = TPACPI_MATCH_ANY,
7659 .ec = TPACPI_MATCH_ANY,
7660 .quirks = TPACPI_VOL_Q_MUTEONLY }
7663 static int __init volume_init(struct ibm_init_struct *iibm)
7665 unsigned long quirks;
7668 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7670 mutex_init(&volume_mutex);
7673 * Check for module parameter bogosity, note that we
7674 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7675 * able to detect "unspecified"
7677 if (volume_mode > TPACPI_VOL_MODE_MAX)
7680 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7681 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7686 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7690 * The ALSA mixer is our primary interface.
7691 * When disabled, don't install the subdriver at all
7694 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7695 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7699 quirks = tpacpi_check_quirks(volume_quirk_table,
7700 ARRAY_SIZE(volume_quirk_table));
7702 switch (volume_capabilities) {
7703 case TPACPI_VOL_CAP_AUTO:
7704 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7705 tp_features.mixer_no_level_control = 1;
7706 else if (quirks & TPACPI_VOL_Q_LEVEL)
7707 tp_features.mixer_no_level_control = 0;
7709 return -ENODEV; /* no mixer */
7711 case TPACPI_VOL_CAP_VOLMUTE:
7712 tp_features.mixer_no_level_control = 0;
7714 case TPACPI_VOL_CAP_MUTEONLY:
7715 tp_features.mixer_no_level_control = 1;
7721 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7722 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7723 "using user-supplied volume_capabilities=%d\n",
7724 volume_capabilities);
7726 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7727 volume_mode == TPACPI_VOL_MODE_MAX) {
7728 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7730 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7731 "driver auto-selected volume_mode=%d\n",
7734 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7735 "using user-supplied volume_mode=%d\n",
7739 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7740 "mute is supported, volume control is %s\n",
7741 str_supported(!tp_features.mixer_no_level_control));
7743 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7744 software_mute_active = true;
7746 rc = volume_create_alsa_mixer();
7748 pr_err("Could not create the ALSA mixer interface\n");
7752 pr_info("Console audio control enabled, mode: %s\n",
7753 (volume_control_allowed) ?
7754 "override (read/write)" :
7755 "monitor (read only)");
7758 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7759 "registering volume hotkeys as change notification\n");
7760 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7761 | TP_ACPI_HKEY_VOLUP_MASK
7762 | TP_ACPI_HKEY_VOLDWN_MASK
7763 | TP_ACPI_HKEY_MUTE_MASK);
7768 static int volume_read(struct seq_file *m)
7772 if (volume_get_status(&status) < 0) {
7773 seq_printf(m, "level:\t\tunreadable\n");
7775 if (tp_features.mixer_no_level_control)
7776 seq_printf(m, "level:\t\tunsupported\n");
7778 seq_printf(m, "level:\t\t%d\n",
7779 status & TP_EC_AUDIO_LVL_MSK);
7781 seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
7783 if (volume_control_allowed) {
7784 seq_printf(m, "commands:\tunmute, mute\n");
7785 if (!tp_features.mixer_no_level_control) {
7786 seq_printf(m, "commands:\tup, down\n");
7787 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7796 static int volume_write(char *buf)
7799 u8 new_level, new_mute;
7805 * We do allow volume control at driver startup, so that the
7806 * user can set initial state through the volume=... parameter hack.
7808 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7809 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7810 tp_warned.volume_ctrl_forbidden = 1;
7811 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7812 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7817 rc = volume_get_status(&s);
7821 new_level = s & TP_EC_AUDIO_LVL_MSK;
7822 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7824 while ((cmd = strsep(&buf, ","))) {
7825 if (!tp_features.mixer_no_level_control) {
7826 if (strstarts(cmd, "up")) {
7829 else if (new_level < TP_EC_VOLUME_MAX)
7832 } else if (strstarts(cmd, "down")) {
7835 else if (new_level > 0)
7838 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7839 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7844 if (strstarts(cmd, "mute"))
7845 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7846 else if (strstarts(cmd, "unmute"))
7852 if (tp_features.mixer_no_level_control) {
7853 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7854 new_mute ? "" : "un");
7855 rc = volume_set_mute(!!new_mute);
7857 tpacpi_disclose_usertask("procfs volume",
7858 "%smute and set level to %d\n",
7859 new_mute ? "" : "un", new_level);
7860 rc = volume_set_status(new_mute | new_level);
7862 volume_alsa_notify_change();
7864 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7867 static struct ibm_struct volume_driver_data = {
7869 .read = volume_read,
7870 .write = volume_write,
7871 .exit = volume_exit,
7872 .suspend = volume_suspend,
7873 .resume = volume_resume,
7874 .shutdown = volume_shutdown,
7877 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7879 #define alsa_card NULL
7881 static inline void volume_alsa_notify_change(void)
7885 static int __init volume_init(struct ibm_init_struct *iibm)
7887 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7892 static struct ibm_struct volume_driver_data = {
7896 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7898 /*************************************************************************
7905 * TPACPI_FAN_RD_ACPI_GFAN:
7906 * ACPI GFAN method: returns fan level
7908 * see TPACPI_FAN_WR_ACPI_SFAN
7909 * EC 0x2f (HFSP) not available if GFAN exists
7911 * TPACPI_FAN_WR_ACPI_SFAN:
7912 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7914 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7917 * TPACPI_FAN_WR_TPEC:
7918 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7919 * Supported on almost all ThinkPads
7921 * Fan speed changes of any sort (including those caused by the
7922 * disengaged mode) are usually done slowly by the firmware as the
7923 * maximum amount of fan duty cycle change per second seems to be
7926 * Reading is not available if GFAN exists.
7927 * Writing is not available if SFAN exists.
7930 * 7 automatic mode engaged;
7931 * (default operation mode of the ThinkPad)
7932 * fan level is ignored in this mode.
7933 * 6 full speed mode (takes precedence over bit 7);
7934 * not available on all thinkpads. May disable
7935 * the tachometer while the fan controller ramps up
7936 * the speed (which can take up to a few *minutes*).
7937 * Speeds up fan to 100% duty-cycle, which is far above
7938 * the standard RPM levels. It is not impossible that
7939 * it could cause hardware damage.
7940 * 5-3 unused in some models. Extra bits for fan level
7941 * in others, but still useless as all values above
7942 * 7 map to the same speed as level 7 in these models.
7943 * 2-0 fan level (0..7 usually)
7945 * 0x07 = max (set when temperatures critical)
7946 * Some ThinkPads may have other levels, see
7947 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7949 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7950 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7951 * does so, its initial value is meaningless (0x07).
7953 * For firmware bugs, refer to:
7954 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7958 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7959 * Main fan tachometer reading (in RPM)
7961 * This register is present on all ThinkPads with a new-style EC, and
7962 * it is known not to be present on the A21m/e, and T22, as there is
7963 * something else in offset 0x84 according to the ACPI DSDT. Other
7964 * ThinkPads from this same time period (and earlier) probably lack the
7965 * tachometer as well.
7967 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7968 * was never fixed by IBM to report the EC firmware version string
7969 * probably support the tachometer (like the early X models), so
7970 * detecting it is quite hard. We need more data to know for sure.
7972 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7975 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7978 * For firmware bugs, refer to:
7979 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7983 * ThinkPad EC register 0x31 bit 0 (only on select models)
7985 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7986 * show the speed of the main fan. When bit 0 of EC register 0x31
7987 * is one, the tachometer registers show the speed of the auxiliary
7990 * Fan control seems to affect both fans, regardless of the state
7993 * So far, only the firmware for the X60/X61 non-tablet versions
7994 * seem to support this (firmware TP-7M).
7996 * TPACPI_FAN_WR_ACPI_FANS:
7997 * ThinkPad X31, X40, X41. Not available in the X60.
7999 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8000 * high speed. ACPI DSDT seems to map these three speeds to levels
8001 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8002 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8004 * The speeds are stored on handles
8005 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8007 * There are three default speed sets, accessible as handles:
8008 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8010 * ACPI DSDT switches which set is in use depending on various
8013 * TPACPI_FAN_WR_TPEC is also available and should be used to
8014 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8015 * but the ACPI tables just mention level 7.
8017 * TPACPI_FAN_RD_TPEC_NS:
8018 * This mode is used for a few ThinkPads (L13 Yoga Gen2, X13 Yoga Gen2 etc.)
8019 * that are using non-standard EC locations for reporting fan speeds.
8020 * Currently these platforms only provide fan rpm reporting.
8024 #define FAN_RPM_CAL_CONST 491520 /* FAN RPM calculation offset for some non-standard ECFW */
8026 #define FAN_NS_CTRL_STATUS BIT(2) /* Bit which determines control is enabled or not */
8027 #define FAN_NS_CTRL BIT(4) /* Bit which determines control is by host or EC */
8029 enum { /* Fan control constants */
8030 fan_status_offset = 0x2f, /* EC register 0x2f */
8031 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8032 * 0x84 must be read before 0x85 */
8033 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8034 bit 0 selects which fan is active */
8036 fan_status_offset_ns = 0x93, /* Special status/control offset for non-standard EC Fan1 */
8037 fan2_status_offset_ns = 0x96, /* Special status/control offset for non-standard EC Fan2 */
8038 fan_rpm_status_ns = 0x95, /* Special offset for Fan1 RPM status for non-standard EC */
8039 fan2_rpm_status_ns = 0x98, /* Special offset for Fan2 RPM status for non-standard EC */
8041 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8042 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8044 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8047 enum fan_status_access_mode {
8048 TPACPI_FAN_NONE = 0, /* No fan status or control */
8049 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8050 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8051 TPACPI_FAN_RD_TPEC_NS, /* Use non-standard ACPI EC regs (eg: L13 Yoga gen2 etc.) */
8054 enum fan_control_access_mode {
8055 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8056 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8057 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8058 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8061 enum fan_control_commands {
8062 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8063 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8064 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8065 * and also watchdog cmd */
8068 static bool fan_control_allowed;
8070 static enum fan_status_access_mode fan_status_access_mode;
8071 static enum fan_control_access_mode fan_control_access_mode;
8072 static enum fan_control_commands fan_control_commands;
8074 static u8 fan_control_initial_status;
8075 static u8 fan_control_desired_level;
8076 static u8 fan_control_resume_level;
8077 static int fan_watchdog_maxinterval;
8079 static bool fan_with_ns_addr;
8081 static struct mutex fan_mutex;
8083 static void fan_watchdog_fire(struct work_struct *ignored);
8084 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8086 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8087 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8088 "\\FSPD", /* 600e/x, 770e, 770x */
8090 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8091 "JFNS", /* 770x-JL */
8095 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8096 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8097 * be in auto mode (0x80).
8099 * This is corrected by any write to HFSP either by the driver, or
8102 * We assume 0x07 really means auto mode while this quirk is active,
8103 * as this is far more likely than the ThinkPad being in level 7,
8104 * which is only used by the firmware during thermal emergencies.
8106 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8107 * TP-70 (T43, R52), which are known to be buggy.
8110 static void fan_quirk1_setup(void)
8112 if (fan_control_initial_status == 0x07) {
8113 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8114 tp_features.fan_ctrl_status_undef = 1;
8118 static void fan_quirk1_handle(u8 *fan_status)
8120 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8121 if (*fan_status != fan_control_initial_status) {
8122 /* something changed the HFSP regisnter since
8123 * driver init time, so it is not undefined
8125 tp_features.fan_ctrl_status_undef = 0;
8127 /* Return most likely status. In fact, it
8128 * might be the only possible status */
8129 *fan_status = TP_EC_FAN_AUTO;
8134 /* Select main fan on X60/X61, NOOP on others */
8135 static bool fan_select_fan1(void)
8137 if (tp_features.second_fan) {
8140 if (ec_read(fan_select_offset, &val) < 0)
8143 if (ec_write(fan_select_offset, val) < 0)
8149 /* Select secondary fan on X60/X61 */
8150 static bool fan_select_fan2(void)
8154 if (!tp_features.second_fan)
8157 if (ec_read(fan_select_offset, &val) < 0)
8160 if (ec_write(fan_select_offset, val) < 0)
8166 static void fan_update_desired_level(u8 status)
8168 lockdep_assert_held(&fan_mutex);
8171 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8173 fan_control_desired_level = 7;
8175 fan_control_desired_level = status;
8179 static int fan_get_status(u8 *status)
8184 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8186 switch (fan_status_access_mode) {
8187 case TPACPI_FAN_RD_ACPI_GFAN: {
8188 /* 570, 600e/x, 770e, 770x */
8191 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8195 *status = res & 0x07;
8199 case TPACPI_FAN_RD_TPEC:
8200 /* all except 570, 600e/x, 770e, 770x */
8201 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8204 if (likely(status)) {
8206 fan_quirk1_handle(status);
8210 case TPACPI_FAN_RD_TPEC_NS:
8211 /* Default mode is AUTO which means controlled by EC */
8212 if (!acpi_ec_read(fan_status_offset_ns, &s))
8227 static int fan_get_status_safe(u8 *status)
8232 if (mutex_lock_killable(&fan_mutex))
8233 return -ERESTARTSYS;
8234 rc = fan_get_status(&s);
8235 /* NS EC doesn't have register with level settings */
8236 if (!rc && !fan_with_ns_addr)
8237 fan_update_desired_level(s);
8238 mutex_unlock(&fan_mutex);
8248 static int fan_get_speed(unsigned int *speed)
8252 switch (fan_status_access_mode) {
8253 case TPACPI_FAN_RD_TPEC:
8254 /* all except 570, 600e/x, 770e, 770x */
8255 if (unlikely(!fan_select_fan1()))
8257 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8258 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8262 *speed = (hi << 8) | lo;
8264 case TPACPI_FAN_RD_TPEC_NS:
8265 if (!acpi_ec_read(fan_rpm_status_ns, &lo))
8269 *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
8279 static int fan2_get_speed(unsigned int *speed)
8284 switch (fan_status_access_mode) {
8285 case TPACPI_FAN_RD_TPEC:
8286 /* all except 570, 600e/x, 770e, 770x */
8287 if (unlikely(!fan_select_fan2()))
8289 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8290 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8291 fan_select_fan1(); /* play it safe */
8296 *speed = (hi << 8) | lo;
8299 case TPACPI_FAN_RD_TPEC_NS:
8300 rc = !acpi_ec_read(fan2_status_offset_ns, &status);
8303 if (!(status & FAN_NS_CTRL_STATUS)) {
8304 pr_info("secondary fan control not supported\n");
8307 rc = !acpi_ec_read(fan2_rpm_status_ns, &lo);
8311 *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
8321 static int fan_set_level(int level)
8323 if (!fan_control_allowed)
8326 switch (fan_control_access_mode) {
8327 case TPACPI_FAN_WR_ACPI_SFAN:
8328 if ((level < 0) || (level > 7))
8331 if (tp_features.second_fan_ctl) {
8332 if (!fan_select_fan2() ||
8333 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8334 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8335 tp_features.second_fan_ctl = 0;
8339 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8343 case TPACPI_FAN_WR_ACPI_FANS:
8344 case TPACPI_FAN_WR_TPEC:
8345 if (!(level & TP_EC_FAN_AUTO) &&
8346 !(level & TP_EC_FAN_FULLSPEED) &&
8347 ((level < 0) || (level > 7)))
8350 /* safety net should the EC not support AUTO
8351 * or FULLSPEED mode bits and just ignore them */
8352 if (level & TP_EC_FAN_FULLSPEED)
8353 level |= 7; /* safety min speed 7 */
8354 else if (level & TP_EC_FAN_AUTO)
8355 level |= 4; /* safety min speed 4 */
8357 if (tp_features.second_fan_ctl) {
8358 if (!fan_select_fan2() ||
8359 !acpi_ec_write(fan_status_offset, level)) {
8360 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8361 tp_features.second_fan_ctl = 0;
8366 if (!acpi_ec_write(fan_status_offset, level))
8369 tp_features.fan_ctrl_status_undef = 0;
8376 vdbg_printk(TPACPI_DBG_FAN,
8377 "fan control: set fan control register to 0x%02x\n", level);
8381 static int fan_set_level_safe(int level)
8385 if (!fan_control_allowed)
8388 if (mutex_lock_killable(&fan_mutex))
8389 return -ERESTARTSYS;
8391 if (level == TPACPI_FAN_LAST_LEVEL)
8392 level = fan_control_desired_level;
8394 rc = fan_set_level(level);
8396 fan_update_desired_level(level);
8398 mutex_unlock(&fan_mutex);
8402 static int fan_set_enable(void)
8407 if (!fan_control_allowed)
8410 if (mutex_lock_killable(&fan_mutex))
8411 return -ERESTARTSYS;
8413 switch (fan_control_access_mode) {
8414 case TPACPI_FAN_WR_ACPI_FANS:
8415 case TPACPI_FAN_WR_TPEC:
8416 rc = fan_get_status(&s);
8420 /* Don't go out of emergency fan mode */
8423 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8426 if (!acpi_ec_write(fan_status_offset, s))
8429 tp_features.fan_ctrl_status_undef = 0;
8434 case TPACPI_FAN_WR_ACPI_SFAN:
8435 rc = fan_get_status(&s);
8441 /* Set fan to at least level 4 */
8444 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8454 mutex_unlock(&fan_mutex);
8457 vdbg_printk(TPACPI_DBG_FAN,
8458 "fan control: set fan control register to 0x%02x\n",
8463 static int fan_set_disable(void)
8467 if (!fan_control_allowed)
8470 if (mutex_lock_killable(&fan_mutex))
8471 return -ERESTARTSYS;
8474 switch (fan_control_access_mode) {
8475 case TPACPI_FAN_WR_ACPI_FANS:
8476 case TPACPI_FAN_WR_TPEC:
8477 if (!acpi_ec_write(fan_status_offset, 0x00))
8480 fan_control_desired_level = 0;
8481 tp_features.fan_ctrl_status_undef = 0;
8485 case TPACPI_FAN_WR_ACPI_SFAN:
8486 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8489 fan_control_desired_level = 0;
8497 vdbg_printk(TPACPI_DBG_FAN,
8498 "fan control: set fan control register to 0\n");
8500 mutex_unlock(&fan_mutex);
8504 static int fan_set_speed(int speed)
8508 if (!fan_control_allowed)
8511 if (mutex_lock_killable(&fan_mutex))
8512 return -ERESTARTSYS;
8515 switch (fan_control_access_mode) {
8516 case TPACPI_FAN_WR_ACPI_FANS:
8517 if (speed >= 0 && speed <= 65535) {
8518 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8519 speed, speed, speed))
8529 mutex_unlock(&fan_mutex);
8533 static void fan_watchdog_reset(void)
8535 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8538 if (fan_watchdog_maxinterval > 0 &&
8539 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8540 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8541 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8543 cancel_delayed_work(&fan_watchdog_task);
8546 static void fan_watchdog_fire(struct work_struct *ignored)
8550 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8553 pr_notice("fan watchdog: enabling fan\n");
8554 rc = fan_set_enable();
8556 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8558 /* reschedule for later */
8559 fan_watchdog_reset();
8564 * SYSFS fan layout: hwmon compatible (device)
8567 * 0: "disengaged" mode
8569 * 2: native EC "auto" mode (recommended, hardware default)
8571 * pwm*: set speed in manual mode, ignored otherwise.
8572 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8575 * fan*_input: tachometer reading, RPM
8578 * SYSFS fan layout: extensions
8580 * fan_watchdog (driver):
8581 * fan watchdog interval in seconds, 0 disables (default), max 120
8584 /* sysfs fan pwm1_enable ----------------------------------------------- */
8585 static ssize_t fan_pwm1_enable_show(struct device *dev,
8586 struct device_attribute *attr,
8592 res = fan_get_status_safe(&status);
8596 if (status & TP_EC_FAN_FULLSPEED) {
8598 } else if (status & TP_EC_FAN_AUTO) {
8603 return sysfs_emit(buf, "%d\n", mode);
8606 static ssize_t fan_pwm1_enable_store(struct device *dev,
8607 struct device_attribute *attr,
8608 const char *buf, size_t count)
8613 if (parse_strtoul(buf, 2, &t))
8616 tpacpi_disclose_usertask("hwmon pwm1_enable",
8617 "set fan mode to %lu\n", t);
8621 level = TP_EC_FAN_FULLSPEED;
8624 level = TPACPI_FAN_LAST_LEVEL;
8627 level = TP_EC_FAN_AUTO;
8630 /* reserved for software-controlled auto mode */
8636 res = fan_set_level_safe(level);
8642 fan_watchdog_reset();
8647 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8648 fan_pwm1_enable_show, fan_pwm1_enable_store);
8650 /* sysfs fan pwm1 ------------------------------------------------------ */
8651 static ssize_t fan_pwm1_show(struct device *dev,
8652 struct device_attribute *attr,
8658 res = fan_get_status_safe(&status);
8663 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8664 status = fan_control_desired_level;
8669 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8672 static ssize_t fan_pwm1_store(struct device *dev,
8673 struct device_attribute *attr,
8674 const char *buf, size_t count)
8678 u8 status, newlevel;
8680 if (parse_strtoul(buf, 255, &s))
8683 tpacpi_disclose_usertask("hwmon pwm1",
8684 "set fan speed to %lu\n", s);
8686 /* scale down from 0-255 to 0-7 */
8687 newlevel = (s >> 5) & 0x07;
8689 if (mutex_lock_killable(&fan_mutex))
8690 return -ERESTARTSYS;
8692 rc = fan_get_status(&status);
8693 if (!rc && (status &
8694 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8695 rc = fan_set_level(newlevel);
8699 fan_update_desired_level(newlevel);
8700 fan_watchdog_reset();
8704 mutex_unlock(&fan_mutex);
8705 return (rc) ? rc : count;
8708 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8710 /* sysfs fan fan1_input ------------------------------------------------ */
8711 static ssize_t fan_fan1_input_show(struct device *dev,
8712 struct device_attribute *attr,
8718 res = fan_get_speed(&speed);
8722 return sysfs_emit(buf, "%u\n", speed);
8725 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8727 /* sysfs fan fan2_input ------------------------------------------------ */
8728 static ssize_t fan_fan2_input_show(struct device *dev,
8729 struct device_attribute *attr,
8735 res = fan2_get_speed(&speed);
8739 return sysfs_emit(buf, "%u\n", speed);
8742 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8744 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8745 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8747 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8750 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8755 if (parse_strtoul(buf, 120, &t))
8758 if (!fan_control_allowed)
8761 fan_watchdog_maxinterval = t;
8762 fan_watchdog_reset();
8764 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8768 static DRIVER_ATTR_RW(fan_watchdog);
8770 /* --------------------------------------------------------------------- */
8772 static struct attribute *fan_attributes[] = {
8773 &dev_attr_pwm1_enable.attr,
8774 &dev_attr_pwm1.attr,
8775 &dev_attr_fan1_input.attr,
8776 &dev_attr_fan2_input.attr,
8780 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8783 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8784 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8787 if (attr == &dev_attr_fan2_input.attr) {
8788 if (!tp_features.second_fan)
8795 static const struct attribute_group fan_attr_group = {
8796 .is_visible = fan_attr_is_visible,
8797 .attrs = fan_attributes,
8800 static struct attribute *fan_driver_attributes[] = {
8801 &driver_attr_fan_watchdog.attr,
8805 static const struct attribute_group fan_driver_attr_group = {
8806 .is_visible = fan_attr_is_visible,
8807 .attrs = fan_driver_attributes,
8810 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8811 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8812 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8813 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8814 #define TPACPI_FAN_NS 0x0010 /* For EC with non-Standard register addresses */
8816 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8817 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8818 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8819 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8820 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8821 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8822 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8823 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8824 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8825 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8826 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8827 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8828 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8829 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8830 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8831 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8832 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8833 TPACPI_Q_LNV3('R', '1', 'F', TPACPI_FAN_NS), /* L13 Yoga Gen 2 */
8834 TPACPI_Q_LNV3('N', '2', 'U', TPACPI_FAN_NS), /* X13 Yoga Gen 2*/
8835 TPACPI_Q_LNV3('R', '0', 'R', TPACPI_FAN_NS), /* L380 */
8836 TPACPI_Q_LNV3('R', '1', '5', TPACPI_FAN_NS), /* L13 Yoga Gen 1 */
8837 TPACPI_Q_LNV3('R', '1', '0', TPACPI_FAN_NS), /* L390 */
8838 TPACPI_Q_LNV3('N', '2', 'L', TPACPI_FAN_NS), /* X13 Yoga Gen 1 */
8839 TPACPI_Q_LNV3('R', '0', 'T', TPACPI_FAN_NS), /* 11e Gen5 GL */
8840 TPACPI_Q_LNV3('R', '1', 'D', TPACPI_FAN_NS), /* 11e Gen5 GL-R */
8841 TPACPI_Q_LNV3('R', '0', 'V', TPACPI_FAN_NS), /* 11e Gen5 KL-Y */
8842 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8845 static int __init fan_init(struct ibm_init_struct *iibm)
8847 unsigned long quirks;
8849 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8850 "initializing fan subdriver\n");
8852 mutex_init(&fan_mutex);
8853 fan_status_access_mode = TPACPI_FAN_NONE;
8854 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8855 fan_control_commands = 0;
8856 fan_watchdog_maxinterval = 0;
8857 tp_features.fan_ctrl_status_undef = 0;
8858 tp_features.second_fan = 0;
8859 tp_features.second_fan_ctl = 0;
8860 fan_control_desired_level = 7;
8862 if (tpacpi_is_ibm()) {
8863 TPACPI_ACPIHANDLE_INIT(fans);
8864 TPACPI_ACPIHANDLE_INIT(gfan);
8865 TPACPI_ACPIHANDLE_INIT(sfan);
8868 quirks = tpacpi_check_quirks(fan_quirk_table,
8869 ARRAY_SIZE(fan_quirk_table));
8871 if (quirks & TPACPI_FAN_NOFAN) {
8872 pr_info("No integrated ThinkPad fan available\n");
8876 if (quirks & TPACPI_FAN_NS) {
8877 pr_info("ECFW with non-standard fan reg control found\n");
8878 fan_with_ns_addr = 1;
8879 /* Fan ctrl support from host is undefined for now */
8880 tp_features.fan_ctrl_status_undef = 1;
8884 /* 570, 600e/x, 770e, 770x */
8885 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8887 /* all other ThinkPads: note that even old-style
8888 * ThinkPad ECs supports the fan control register */
8889 if (fan_with_ns_addr ||
8890 likely(acpi_ec_read(fan_status_offset, &fan_control_initial_status))) {
8894 fan_status_access_mode = fan_with_ns_addr ?
8895 TPACPI_FAN_RD_TPEC_NS : TPACPI_FAN_RD_TPEC;
8897 if (quirks & TPACPI_FAN_Q1)
8899 /* Try and probe the 2nd fan */
8900 tp_features.second_fan = 1; /* needed for get_speed to work */
8901 res = fan2_get_speed(&speed);
8902 if (res >= 0 && speed != FAN_NOT_PRESENT) {
8903 /* It responded - so let's assume it's there */
8904 tp_features.second_fan = 1;
8905 /* fan control not currently available for ns ECFW */
8906 tp_features.second_fan_ctl = !fan_with_ns_addr;
8907 pr_info("secondary fan control detected & enabled\n");
8909 /* Fan not auto-detected */
8910 tp_features.second_fan = 0;
8911 if (quirks & TPACPI_FAN_2FAN) {
8912 tp_features.second_fan = 1;
8913 pr_info("secondary fan support enabled\n");
8915 if (quirks & TPACPI_FAN_2CTL) {
8916 tp_features.second_fan = 1;
8917 tp_features.second_fan_ctl = 1;
8918 pr_info("secondary fan control enabled\n");
8922 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8929 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8930 fan_control_commands |=
8931 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8934 /* gfan without sfan means no fan control */
8935 /* all other models implement TP EC 0x2f control */
8939 fan_control_access_mode =
8940 TPACPI_FAN_WR_ACPI_FANS;
8941 fan_control_commands |=
8942 TPACPI_FAN_CMD_SPEED |
8943 TPACPI_FAN_CMD_LEVEL |
8944 TPACPI_FAN_CMD_ENABLE;
8946 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8947 fan_control_commands |=
8948 TPACPI_FAN_CMD_LEVEL |
8949 TPACPI_FAN_CMD_ENABLE;
8954 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8955 "fan is %s, modes %d, %d\n",
8956 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8957 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8958 fan_status_access_mode, fan_control_access_mode);
8960 /* fan control master switch */
8961 if (!fan_control_allowed) {
8962 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8963 fan_control_commands = 0;
8964 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8965 "fan control features disabled by parameter\n");
8968 /* update fan_control_desired_level */
8969 if (fan_status_access_mode != TPACPI_FAN_NONE)
8970 fan_get_status_safe(NULL);
8972 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8973 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8979 static void fan_exit(void)
8981 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8982 "cancelling any pending fan watchdog tasks\n");
8984 cancel_delayed_work(&fan_watchdog_task);
8985 flush_workqueue(tpacpi_wq);
8988 static void fan_suspend(void)
8992 if (!fan_control_allowed)
8995 /* Store fan status in cache */
8996 fan_control_resume_level = 0;
8997 rc = fan_get_status_safe(&fan_control_resume_level);
8999 pr_notice("failed to read fan level for later restore during resume: %d\n",
9002 /* if it is undefined, don't attempt to restore it.
9004 if (tp_features.fan_ctrl_status_undef)
9005 fan_control_resume_level = 0;
9008 static void fan_resume(void)
9010 u8 current_level = 7;
9011 bool do_set = false;
9014 /* DSDT *always* updates status on resume */
9015 tp_features.fan_ctrl_status_undef = 0;
9017 if (!fan_control_allowed ||
9018 !fan_control_resume_level ||
9019 fan_get_status_safe(¤t_level))
9022 switch (fan_control_access_mode) {
9023 case TPACPI_FAN_WR_ACPI_SFAN:
9024 /* never decrease fan level */
9025 do_set = (fan_control_resume_level > current_level);
9027 case TPACPI_FAN_WR_ACPI_FANS:
9028 case TPACPI_FAN_WR_TPEC:
9029 /* never decrease fan level, scale is:
9030 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
9032 * We expect the firmware to set either 7 or AUTO, but we
9033 * handle FULLSPEED out of paranoia.
9035 * So, we can safely only restore FULLSPEED or 7, anything
9036 * else could slow the fan. Restoring AUTO is useless, at
9037 * best that's exactly what the DSDT already set (it is the
9040 * Always keep in mind that the DSDT *will* have set the
9041 * fans to what the vendor supposes is the best level. We
9042 * muck with it only to speed the fan up.
9044 if (fan_control_resume_level != 7 &&
9045 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
9048 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
9049 (current_level != fan_control_resume_level);
9055 pr_notice("restoring fan level to 0x%02x\n",
9056 fan_control_resume_level);
9057 rc = fan_set_level_safe(fan_control_resume_level);
9059 pr_notice("failed to restore fan level: %d\n", rc);
9063 static int fan_read(struct seq_file *m)
9067 unsigned int speed = 0;
9069 switch (fan_status_access_mode) {
9070 case TPACPI_FAN_RD_ACPI_GFAN:
9071 /* 570, 600e/x, 770e, 770x */
9072 rc = fan_get_status_safe(&status);
9076 seq_printf(m, "status:\t\t%s\n"
9078 str_enabled_disabled(status), status);
9081 case TPACPI_FAN_RD_TPEC_NS:
9082 case TPACPI_FAN_RD_TPEC:
9083 /* all except 570, 600e/x, 770e, 770x */
9084 rc = fan_get_status_safe(&status);
9088 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
9090 rc = fan_get_speed(&speed);
9094 seq_printf(m, "speed:\t\t%d\n", speed);
9096 if (fan_status_access_mode == TPACPI_FAN_RD_TPEC_NS) {
9098 * No full speed bit in NS EC
9099 * EC Auto mode is set by default.
9100 * No other levels settings available
9102 seq_printf(m, "level:\t\t%s\n", status & FAN_NS_CTRL ? "unknown" : "auto");
9104 if (status & TP_EC_FAN_FULLSPEED)
9105 /* Disengaged mode takes precedence */
9106 seq_printf(m, "level:\t\tdisengaged\n");
9107 else if (status & TP_EC_FAN_AUTO)
9108 seq_printf(m, "level:\t\tauto\n");
9110 seq_printf(m, "level:\t\t%d\n", status);
9114 case TPACPI_FAN_NONE:
9116 seq_printf(m, "status:\t\tnot supported\n");
9119 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9120 seq_printf(m, "commands:\tlevel <level>");
9122 switch (fan_control_access_mode) {
9123 case TPACPI_FAN_WR_ACPI_SFAN:
9124 seq_printf(m, " (<level> is 0-7)\n");
9128 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9133 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9134 seq_printf(m, "commands:\tenable, disable\n"
9135 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9137 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9138 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9143 static int fan_write_cmd_level(const char *cmd, int *rc)
9147 if (strstarts(cmd, "level auto"))
9148 level = TP_EC_FAN_AUTO;
9149 else if (strstarts(cmd, "level disengaged") || strstarts(cmd, "level full-speed"))
9150 level = TP_EC_FAN_FULLSPEED;
9151 else if (sscanf(cmd, "level %d", &level) != 1)
9154 *rc = fan_set_level_safe(level);
9156 pr_err("level command accepted for unsupported access mode %d\n",
9157 fan_control_access_mode);
9159 tpacpi_disclose_usertask("procfs fan",
9160 "set level to %d\n", level);
9165 static int fan_write_cmd_enable(const char *cmd, int *rc)
9167 if (!strstarts(cmd, "enable"))
9170 *rc = fan_set_enable();
9172 pr_err("enable command accepted for unsupported access mode %d\n",
9173 fan_control_access_mode);
9175 tpacpi_disclose_usertask("procfs fan", "enable\n");
9180 static int fan_write_cmd_disable(const char *cmd, int *rc)
9182 if (!strstarts(cmd, "disable"))
9185 *rc = fan_set_disable();
9187 pr_err("disable command accepted for unsupported access mode %d\n",
9188 fan_control_access_mode);
9190 tpacpi_disclose_usertask("procfs fan", "disable\n");
9195 static int fan_write_cmd_speed(const char *cmd, int *rc)
9200 * Support speed <low> <medium> <high> ? */
9202 if (sscanf(cmd, "speed %d", &speed) != 1)
9205 *rc = fan_set_speed(speed);
9207 pr_err("speed command accepted for unsupported access mode %d\n",
9208 fan_control_access_mode);
9210 tpacpi_disclose_usertask("procfs fan",
9211 "set speed to %d\n", speed);
9216 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9220 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9223 if (interval < 0 || interval > 120)
9226 fan_watchdog_maxinterval = interval;
9227 tpacpi_disclose_usertask("procfs fan",
9228 "set watchdog timer to %d\n",
9235 static int fan_write(char *buf)
9240 while (!rc && (cmd = strsep(&buf, ","))) {
9241 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9242 fan_write_cmd_level(cmd, &rc)) &&
9243 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9244 (fan_write_cmd_enable(cmd, &rc) ||
9245 fan_write_cmd_disable(cmd, &rc) ||
9246 fan_write_cmd_watchdog(cmd, &rc))) &&
9247 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9248 fan_write_cmd_speed(cmd, &rc))
9252 fan_watchdog_reset();
9258 static struct ibm_struct fan_driver_data = {
9263 .suspend = fan_suspend,
9264 .resume = fan_resume,
9267 /*************************************************************************
9268 * Mute LED subdriver
9271 #define TPACPI_LED_MAX 2
9273 struct tp_led_table {
9280 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9281 [LED_AUDIO_MUTE] = {
9286 [LED_AUDIO_MICMUTE] = {
9293 static int mute_led_on_off(struct tp_led_table *t, bool state)
9298 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9299 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9303 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9304 state ? t->on_value : t->off_value))
9311 static int tpacpi_led_set(int whichled, bool on)
9313 struct tp_led_table *t;
9315 t = &led_tables[whichled];
9316 if (t->state < 0 || t->state == on)
9318 return mute_led_on_off(t, on);
9321 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9322 enum led_brightness brightness)
9324 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9327 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9328 enum led_brightness brightness)
9330 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9333 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9334 [LED_AUDIO_MUTE] = {
9335 .name = "platform::mute",
9336 .max_brightness = 1,
9337 .brightness_set_blocking = tpacpi_led_mute_set,
9338 .default_trigger = "audio-mute",
9340 [LED_AUDIO_MICMUTE] = {
9341 .name = "platform::micmute",
9342 .max_brightness = 1,
9343 .brightness_set_blocking = tpacpi_led_micmute_set,
9344 .default_trigger = "audio-micmute",
9348 static int mute_led_init(struct ibm_init_struct *iibm)
9353 for (i = 0; i < TPACPI_LED_MAX; i++) {
9354 struct tp_led_table *t = &led_tables[i];
9355 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9360 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9363 led_classdev_unregister(&mute_led_cdev[i]);
9370 static void mute_led_exit(void)
9374 for (i = 0; i < TPACPI_LED_MAX; i++) {
9375 led_classdev_unregister(&mute_led_cdev[i]);
9376 tpacpi_led_set(i, false);
9380 static void mute_led_resume(void)
9384 for (i = 0; i < TPACPI_LED_MAX; i++) {
9385 struct tp_led_table *t = &led_tables[i];
9387 mute_led_on_off(t, t->state);
9391 static struct ibm_struct mute_led_driver_data = {
9393 .exit = mute_led_exit,
9394 .resume = mute_led_resume,
9398 * Battery Wear Control Driver
9399 * Contact: Ognjen Galic <smclt30p@gmail.com>
9404 #define GET_START "BCTG"
9405 #define SET_START "BCCS"
9406 #define GET_STOP "BCSG"
9407 #define SET_STOP "BCSS"
9408 #define GET_DISCHARGE "BDSG"
9409 #define SET_DISCHARGE "BDSS"
9410 #define GET_INHIBIT "BICG"
9411 #define SET_INHIBIT "BICS"
9420 /* Error condition bit */
9421 METHOD_ERR = BIT(31),
9425 /* This is used in the get/set helpers */
9432 struct tpacpi_battery_data {
9437 unsigned int charge_behaviours;
9440 struct tpacpi_battery_driver_data {
9441 struct tpacpi_battery_data batteries[3];
9442 int individual_addressing;
9445 static struct tpacpi_battery_driver_data battery_info;
9447 /* ACPI helpers/functions/probes */
9450 * This evaluates a ACPI method call specific to the battery
9451 * ACPI extension. The specifics are that an error is marked
9452 * in the 32rd bit of the response, so we just check that here.
9454 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9458 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9459 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9462 if (response & METHOD_ERR) {
9463 acpi_handle_err(hkey_handle,
9464 "%s evaluated but flagged as error", method);
9471 static int tpacpi_battery_get(int what, int battery, int *ret)
9474 case THRESHOLD_START:
9475 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9478 /* The value is in the low 8 bits of the response */
9481 case THRESHOLD_STOP:
9482 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9484 /* Value is in lower 8 bits */
9487 * On the stop value, if we return 0 that
9488 * does not make any sense. 0 means Default, which
9489 * means that charging stops at 100%, so we return
9495 case FORCE_DISCHARGE:
9496 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9498 /* The force discharge status is in bit 0 */
9501 case INHIBIT_CHARGE:
9502 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9504 /* The inhibit charge status is in bit 0 */
9508 pr_crit("wrong parameter: %d", what);
9513 static int tpacpi_battery_set(int what, int battery, int value)
9516 /* The first 8 bits are the value of the threshold */
9518 /* The battery ID is in bits 8-9, 2 bits */
9519 param |= battery << 8;
9522 case THRESHOLD_START:
9523 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9524 pr_err("failed to set charge threshold on battery %d",
9529 case THRESHOLD_STOP:
9530 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9531 pr_err("failed to set stop threshold: %d", battery);
9535 case FORCE_DISCHARGE:
9536 /* Force discharge is in bit 0,
9537 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9538 * battery ID is in bits 8-9, 2 bits.
9540 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9541 pr_err("failed to set force discharge on %d", battery);
9545 case INHIBIT_CHARGE:
9546 /* When setting inhibit charge, we set a default value of
9547 * always breaking on AC detach and the effective time is set to
9549 * The battery ID is in bits 4-5, 2 bits,
9550 * the effective time is in bits 8-23, 2 bytes.
9551 * A time of FFFF indicates forever.
9554 param |= battery << 4;
9555 param |= 0xFFFF << 8;
9556 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9557 pr_err("failed to set inhibit charge on %d", battery);
9562 pr_crit("wrong parameter: %d", what);
9567 static int tpacpi_battery_set_validate(int what, int battery, int value)
9571 ret = tpacpi_battery_set(what, battery, value);
9575 ret = tpacpi_battery_get(what, battery, &v);
9584 ret = tpacpi_battery_get(what, battery, &v);
9594 static int tpacpi_battery_probe(int battery)
9598 memset(&battery_info.batteries[battery], 0,
9599 sizeof(battery_info.batteries[battery]));
9602 * 1) Get the current start threshold
9603 * 2) Check for support
9604 * 3) Get the current stop threshold
9605 * 4) Check for support
9606 * 5) Get the current force discharge status
9607 * 6) Check for support
9608 * 7) Get the current inhibit charge status
9609 * 8) Check for support
9611 if (acpi_has_method(hkey_handle, GET_START)) {
9612 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9613 pr_err("Error probing battery %d\n", battery);
9616 /* Individual addressing is in bit 9 */
9618 battery_info.individual_addressing = true;
9619 /* Support is marked in bit 8 */
9621 battery_info.batteries[battery].start_support = 1;
9624 if (tpacpi_battery_get(THRESHOLD_START, battery,
9625 &battery_info.batteries[battery].charge_start)) {
9626 pr_err("Error probing battery %d\n", battery);
9630 if (acpi_has_method(hkey_handle, GET_STOP)) {
9631 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9632 pr_err("Error probing battery stop; %d\n", battery);
9635 /* Support is marked in bit 8 */
9637 battery_info.batteries[battery].stop_support = 1;
9640 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9641 &battery_info.batteries[battery].charge_stop)) {
9642 pr_err("Error probing battery stop: %d\n", battery);
9646 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9647 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9648 pr_err("Error probing battery discharge; %d\n", battery);
9651 /* Support is marked in bit 8 */
9653 battery_info.batteries[battery].charge_behaviours |=
9654 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9656 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9657 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9658 pr_err("Error probing battery inhibit charge; %d\n", battery);
9661 /* Support is marked in bit 5 */
9663 battery_info.batteries[battery].charge_behaviours |=
9664 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9667 battery_info.batteries[battery].charge_behaviours |=
9668 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9670 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9672 battery_info.batteries[battery].charge_start,
9673 battery_info.batteries[battery].charge_stop,
9674 battery_info.batteries[battery].charge_behaviours);
9679 /* General helper functions */
9681 static int tpacpi_battery_get_id(const char *battery_name)
9684 if (strcmp(battery_name, "BAT0") == 0 ||
9685 tp_features.battery_force_primary)
9687 if (strcmp(battery_name, "BAT1") == 0)
9688 return BAT_SECONDARY;
9690 * If for some reason the battery is not BAT0 nor is it
9691 * BAT1, we will assume it's the default, first battery,
9694 pr_warn("unknown battery %s, assuming primary", battery_name);
9698 /* sysfs interface */
9700 static ssize_t tpacpi_battery_store(int what,
9702 const char *buf, size_t count)
9704 struct power_supply *supply = to_power_supply(dev);
9705 unsigned long value;
9708 * Some systems have support for more than
9709 * one battery. If that is the case,
9710 * tpacpi_battery_probe marked that addressing
9711 * them individually is supported, so we do that
9712 * based on the device struct.
9714 * On systems that are not supported, we assume
9715 * the primary as most of the ACPI calls fail
9716 * with "Any Battery" as the parameter.
9718 if (battery_info.individual_addressing)
9719 /* BAT_PRIMARY or BAT_SECONDARY */
9720 battery = tpacpi_battery_get_id(supply->desc->name);
9722 battery = BAT_PRIMARY;
9724 rval = kstrtoul(buf, 10, &value);
9729 case THRESHOLD_START:
9730 if (!battery_info.batteries[battery].start_support)
9732 /* valid values are [0, 99] */
9735 if (value > battery_info.batteries[battery].charge_stop)
9737 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9739 battery_info.batteries[battery].charge_start = value;
9742 case THRESHOLD_STOP:
9743 if (!battery_info.batteries[battery].stop_support)
9745 /* valid values are [1, 100] */
9746 if (value < 1 || value > 100)
9748 if (value < battery_info.batteries[battery].charge_start)
9750 battery_info.batteries[battery].charge_stop = value;
9752 * When 100 is passed to stop, we need to flip
9753 * it to 0 as that the EC understands that as
9754 * "Default", which will charge to 100%
9758 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9762 pr_crit("Wrong parameter: %d", what);
9768 static ssize_t tpacpi_battery_show(int what,
9772 struct power_supply *supply = to_power_supply(dev);
9775 * Some systems have support for more than
9776 * one battery. If that is the case,
9777 * tpacpi_battery_probe marked that addressing
9778 * them individually is supported, so we;
9779 * based on the device struct.
9781 * On systems that are not supported, we assume
9782 * the primary as most of the ACPI calls fail
9783 * with "Any Battery" as the parameter.
9785 if (battery_info.individual_addressing)
9786 /* BAT_PRIMARY or BAT_SECONDARY */
9787 battery = tpacpi_battery_get_id(supply->desc->name);
9789 battery = BAT_PRIMARY;
9790 if (tpacpi_battery_get(what, battery, &ret))
9792 return sprintf(buf, "%d\n", ret);
9795 static ssize_t charge_control_start_threshold_show(struct device *device,
9796 struct device_attribute *attr,
9799 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9802 static ssize_t charge_control_end_threshold_show(struct device *device,
9803 struct device_attribute *attr,
9806 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9809 static ssize_t charge_behaviour_show(struct device *dev,
9810 struct device_attribute *attr,
9813 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9814 struct power_supply *supply = to_power_supply(dev);
9815 unsigned int available;
9818 battery = tpacpi_battery_get_id(supply->desc->name);
9819 available = battery_info.batteries[battery].charge_behaviours;
9821 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9822 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9825 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9830 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9831 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9834 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9840 return power_supply_charge_behaviour_show(dev, available, active, buf);
9843 static ssize_t charge_control_start_threshold_store(struct device *dev,
9844 struct device_attribute *attr,
9845 const char *buf, size_t count)
9847 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9850 static ssize_t charge_control_end_threshold_store(struct device *dev,
9851 struct device_attribute *attr,
9852 const char *buf, size_t count)
9854 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9857 static ssize_t charge_behaviour_store(struct device *dev,
9858 struct device_attribute *attr,
9859 const char *buf, size_t count)
9861 struct power_supply *supply = to_power_supply(dev);
9862 int selected, battery, ret = 0;
9863 unsigned int available;
9865 battery = tpacpi_battery_get_id(supply->desc->name);
9866 available = battery_info.batteries[battery].charge_behaviours;
9867 selected = power_supply_charge_behaviour_parse(available, buf);
9873 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9874 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9875 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9876 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9877 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9881 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9882 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9883 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9884 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9888 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9889 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9890 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9891 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9896 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9903 static DEVICE_ATTR_RW(charge_control_start_threshold);
9904 static DEVICE_ATTR_RW(charge_control_end_threshold);
9905 static DEVICE_ATTR_RW(charge_behaviour);
9906 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9907 charge_start_threshold,
9909 charge_control_start_threshold_show,
9910 charge_control_start_threshold_store
9912 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9913 charge_stop_threshold,
9915 charge_control_end_threshold_show,
9916 charge_control_end_threshold_store
9919 static struct attribute *tpacpi_battery_attrs[] = {
9920 &dev_attr_charge_control_start_threshold.attr,
9921 &dev_attr_charge_control_end_threshold.attr,
9922 &dev_attr_charge_start_threshold.attr,
9923 &dev_attr_charge_stop_threshold.attr,
9924 &dev_attr_charge_behaviour.attr,
9928 ATTRIBUTE_GROUPS(tpacpi_battery);
9930 /* ACPI battery hooking */
9932 static int tpacpi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
9934 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9936 if (tpacpi_battery_probe(batteryid))
9938 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9943 static int tpacpi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
9945 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9949 static struct acpi_battery_hook battery_hook = {
9950 .add_battery = tpacpi_battery_add,
9951 .remove_battery = tpacpi_battery_remove,
9952 .name = "ThinkPad Battery Extension",
9955 /* Subdriver init/exit */
9957 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9959 * Individual addressing is broken on models that expose the
9960 * primary battery as BAT1.
9962 TPACPI_Q_LNV('8', 'F', true), /* Thinkpad X120e */
9963 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9964 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9965 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9966 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9967 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9968 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9971 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9973 memset(&battery_info, 0, sizeof(battery_info));
9975 tp_features.battery_force_primary = tpacpi_check_quirks(
9976 battery_quirk_table,
9977 ARRAY_SIZE(battery_quirk_table));
9979 battery_hook_register(&battery_hook);
9983 static void tpacpi_battery_exit(void)
9985 battery_hook_unregister(&battery_hook);
9988 static struct ibm_struct battery_driver_data = {
9990 .exit = tpacpi_battery_exit,
9993 /*************************************************************************
9994 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9997 static struct drm_privacy_screen *lcdshadow_dev;
9998 static acpi_handle lcdshadow_get_handle;
9999 static acpi_handle lcdshadow_set_handle;
10001 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
10002 enum drm_privacy_screen_status state)
10006 if (WARN_ON(!mutex_is_locked(&priv->lock)))
10009 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
10012 priv->hw_state = priv->sw_state = state;
10016 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
10020 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10023 priv->hw_state = priv->sw_state = output & 0x1;
10026 static const struct drm_privacy_screen_ops lcdshadow_ops = {
10027 .set_sw_state = lcdshadow_set_sw_state,
10028 .get_hw_state = lcdshadow_get_hw_state,
10031 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
10033 acpi_status status1, status2;
10036 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
10037 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
10038 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
10041 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10044 if (!(output & 0x10000))
10047 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
10048 &lcdshadow_ops, NULL);
10049 if (IS_ERR(lcdshadow_dev))
10050 return PTR_ERR(lcdshadow_dev);
10055 static void lcdshadow_exit(void)
10057 drm_privacy_screen_unregister(lcdshadow_dev);
10060 static void lcdshadow_resume(void)
10062 if (!lcdshadow_dev)
10065 mutex_lock(&lcdshadow_dev->lock);
10066 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
10067 mutex_unlock(&lcdshadow_dev->lock);
10070 static int lcdshadow_read(struct seq_file *m)
10072 if (!lcdshadow_dev) {
10073 seq_puts(m, "status:\t\tnot supported\n");
10075 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
10076 seq_puts(m, "commands:\t0, 1\n");
10082 static int lcdshadow_write(char *buf)
10085 int res, state = -EINVAL;
10087 if (!lcdshadow_dev)
10090 while ((cmd = strsep(&buf, ","))) {
10091 res = kstrtoint(cmd, 10, &state);
10096 if (state >= 2 || state < 0)
10099 mutex_lock(&lcdshadow_dev->lock);
10100 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
10101 mutex_unlock(&lcdshadow_dev->lock);
10103 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10108 static struct ibm_struct lcdshadow_driver_data = {
10109 .name = "lcdshadow",
10110 .exit = lcdshadow_exit,
10111 .resume = lcdshadow_resume,
10112 .read = lcdshadow_read,
10113 .write = lcdshadow_write,
10116 /*************************************************************************
10117 * Thinkpad sensor interfaces
10120 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
10121 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
10122 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
10123 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
10125 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
10126 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
10128 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
10129 #define PALMSENSOR_ON_BIT 1 /* psensor status */
10131 static bool has_palmsensor;
10132 static bool has_lapsensor;
10133 static bool palm_state;
10134 static bool lap_state;
10135 static int dytc_version;
10137 static int dytc_command(int command, int *output)
10139 acpi_handle dytc_handle;
10141 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
10142 /* Platform doesn't support DYTC */
10145 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
10150 static int lapsensor_get(bool *present, bool *state)
10155 err = dytc_command(DYTC_CMD_GET, &output);
10159 *present = true; /*If we get his far, we have lapmode support*/
10160 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
10164 static int palmsensor_get(bool *present, bool *state)
10166 acpi_handle psensor_handle;
10170 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10172 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10175 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10176 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10180 static void lapsensor_refresh(void)
10185 if (has_lapsensor) {
10186 err = lapsensor_get(&has_lapsensor, &state);
10189 if (lap_state != state) {
10191 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10196 static void palmsensor_refresh(void)
10201 if (has_palmsensor) {
10202 err = palmsensor_get(&has_palmsensor, &state);
10205 if (palm_state != state) {
10206 palm_state = state;
10207 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10212 static ssize_t dytc_lapmode_show(struct device *dev,
10213 struct device_attribute *attr,
10217 return sysfs_emit(buf, "%d\n", lap_state);
10218 return sysfs_emit(buf, "\n");
10220 static DEVICE_ATTR_RO(dytc_lapmode);
10222 static ssize_t palmsensor_show(struct device *dev,
10223 struct device_attribute *attr,
10226 if (has_palmsensor)
10227 return sysfs_emit(buf, "%d\n", palm_state);
10228 return sysfs_emit(buf, "\n");
10230 static DEVICE_ATTR_RO(palmsensor);
10232 static struct attribute *proxsensor_attributes[] = {
10233 &dev_attr_dytc_lapmode.attr,
10234 &dev_attr_palmsensor.attr,
10238 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10239 struct attribute *attr, int n)
10241 if (attr == &dev_attr_dytc_lapmode.attr) {
10243 * Platforms before DYTC version 5 claim to have a lap sensor,
10244 * but it doesn't work, so we ignore them.
10246 if (!has_lapsensor || dytc_version < 5)
10248 } else if (attr == &dev_attr_palmsensor.attr) {
10249 if (!has_palmsensor)
10256 static const struct attribute_group proxsensor_attr_group = {
10257 .is_visible = proxsensor_attr_is_visible,
10258 .attrs = proxsensor_attributes,
10261 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10263 int palm_err, lap_err;
10265 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10266 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10267 /* If support isn't available for both devices return -ENODEV */
10268 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10270 /* Otherwise, if there was an error return it */
10271 if (palm_err && (palm_err != -ENODEV))
10273 if (lap_err && (lap_err != -ENODEV))
10279 static struct ibm_struct proxsensor_driver_data = {
10280 .name = "proximity-sensor",
10283 /*************************************************************************
10284 * DYTC Platform Profile interface
10287 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10288 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10289 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10291 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10292 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10293 #define DYTC_FC_PSC 29 /* PSC Mode supported */
10294 #define DYTC_FC_AMT 31 /* AMT mode supported */
10296 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10297 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10299 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10300 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10301 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10303 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10304 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10305 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
10306 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
10307 #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
10309 #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
10310 #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
10312 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
10313 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
10314 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
10315 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
10317 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
10318 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
10319 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
10321 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10322 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10324 #define DYTC_SET_COMMAND(function, mode, on) \
10325 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10326 (mode) << DYTC_SET_MODE_BIT | \
10327 (on) << DYTC_SET_VALID_BIT)
10329 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
10330 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
10331 static int dytc_control_amt(bool enable);
10332 static bool dytc_amt_active;
10334 static enum platform_profile_option dytc_current_profile;
10335 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10336 static DEFINE_MUTEX(dytc_mutex);
10337 static int dytc_capabilities;
10338 static bool dytc_mmc_get_available;
10339 static int profile_force;
10341 static int convert_dytc_to_profile(int funcmode, int dytcmode,
10342 enum platform_profile_option *profile)
10344 switch (funcmode) {
10345 case DYTC_FUNCTION_MMC:
10346 switch (dytcmode) {
10347 case DYTC_MODE_MMC_LOWPOWER:
10348 *profile = PLATFORM_PROFILE_LOW_POWER;
10350 case DYTC_MODE_MMC_DEFAULT:
10351 case DYTC_MODE_MMC_BALANCE:
10352 *profile = PLATFORM_PROFILE_BALANCED;
10354 case DYTC_MODE_MMC_PERFORM:
10355 *profile = PLATFORM_PROFILE_PERFORMANCE;
10357 default: /* Unknown mode */
10361 case DYTC_FUNCTION_PSC:
10362 switch (dytcmode) {
10363 case DYTC_MODE_PSC_LOWPOWER:
10364 *profile = PLATFORM_PROFILE_LOW_POWER;
10366 case DYTC_MODE_PSC_BALANCE:
10367 *profile = PLATFORM_PROFILE_BALANCED;
10369 case DYTC_MODE_PSC_PERFORM:
10370 *profile = PLATFORM_PROFILE_PERFORMANCE;
10372 default: /* Unknown mode */
10376 case DYTC_FUNCTION_AMT:
10377 /* For now return balanced. It's the closest we have to 'auto' */
10378 *profile = PLATFORM_PROFILE_BALANCED;
10381 /* Unknown function */
10382 pr_debug("unknown function 0x%x\n", funcmode);
10383 return -EOPNOTSUPP;
10388 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10391 case PLATFORM_PROFILE_LOW_POWER:
10392 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10393 *perfmode = DYTC_MODE_MMC_LOWPOWER;
10394 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10395 *perfmode = DYTC_MODE_PSC_LOWPOWER;
10397 case PLATFORM_PROFILE_BALANCED:
10398 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10399 *perfmode = DYTC_MODE_MMC_BALANCE;
10400 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10401 *perfmode = DYTC_MODE_PSC_BALANCE;
10403 case PLATFORM_PROFILE_PERFORMANCE:
10404 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10405 *perfmode = DYTC_MODE_MMC_PERFORM;
10406 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10407 *perfmode = DYTC_MODE_PSC_PERFORM;
10409 default: /* Unknown profile */
10410 return -EOPNOTSUPP;
10416 * dytc_profile_get: Function to register with platform_profile
10417 * handler. Returns current platform profile.
10419 static int dytc_profile_get(struct platform_profile_handler *pprof,
10420 enum platform_profile_option *profile)
10422 *profile = dytc_current_profile;
10426 static int dytc_control_amt(bool enable)
10432 if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
10433 pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
10438 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
10440 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
10442 pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
10443 err = dytc_command(cmd, &dummy);
10446 dytc_amt_active = enable;
10451 * Helper function - check if we are in CQL mode and if we are
10453 * - run the command
10455 * If not in CQL mode, just run the command
10457 static int dytc_cql_command(int command, int *output)
10459 int err, cmd_err, dummy;
10462 /* Determine if we are in CQL mode. This alters the commands we do */
10463 err = dytc_command(DYTC_CMD_GET, output);
10467 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10468 /* Check if we're OK to return immediately */
10469 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10472 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10473 atomic_inc(&dytc_ignore_event);
10474 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10479 cmd_err = dytc_command(command, output);
10480 /* Check return condition after we've restored CQL state */
10482 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10483 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10491 * dytc_profile_set: Function to register with platform_profile
10492 * handler. Sets current platform profile.
10494 static int dytc_profile_set(struct platform_profile_handler *pprof,
10495 enum platform_profile_option profile)
10501 err = mutex_lock_interruptible(&dytc_mutex);
10505 err = convert_profile_to_dytc(profile, &perfmode);
10509 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10510 if (profile == PLATFORM_PROFILE_BALANCED) {
10512 * To get back to balanced mode we need to issue a reset command.
10513 * Note we still need to disable CQL mode before hand and re-enable
10514 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10515 * stuck at 0 for aprox. 30 minutes.
10517 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10521 /* Determine if we are in CQL mode. This alters the commands we do */
10522 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
10527 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10528 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
10532 /* system supports AMT, activate it when on balanced */
10533 if (dytc_capabilities & BIT(DYTC_FC_AMT))
10534 dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
10536 /* Success - update current profile */
10537 dytc_current_profile = profile;
10539 mutex_unlock(&dytc_mutex);
10543 static void dytc_profile_refresh(void)
10545 enum platform_profile_option profile;
10546 int output = 0, err = 0;
10547 int perfmode, funcmode = 0;
10549 mutex_lock(&dytc_mutex);
10550 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10551 if (dytc_mmc_get_available)
10552 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10554 err = dytc_cql_command(DYTC_CMD_GET, &output);
10555 funcmode = DYTC_FUNCTION_MMC;
10556 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10557 err = dytc_command(DYTC_CMD_GET, &output);
10558 /* Check if we are PSC mode, or have AMT enabled */
10559 funcmode = (output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10560 } else { /* Unknown profile mode */
10563 mutex_unlock(&dytc_mutex);
10567 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10568 err = convert_dytc_to_profile(funcmode, perfmode, &profile);
10569 if (!err && profile != dytc_current_profile) {
10570 dytc_current_profile = profile;
10571 platform_profile_notify();
10575 static struct platform_profile_handler dytc_profile = {
10576 .profile_get = dytc_profile_get,
10577 .profile_set = dytc_profile_set,
10580 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10584 /* Setup supported modes */
10585 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10586 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10587 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10589 err = dytc_command(DYTC_CMD_QUERY, &output);
10593 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10594 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10596 /* Check DYTC is enabled and supports mode setting */
10597 if (dytc_version < 5)
10600 /* Check what capabilities are supported */
10601 err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
10605 /* Check if user wants to override the profile selection */
10606 if (profile_force) {
10607 switch (profile_force) {
10609 dytc_capabilities = 0;
10612 dytc_capabilities = BIT(DYTC_FC_MMC);
10615 dytc_capabilities = BIT(DYTC_FC_PSC);
10618 pr_debug("Profile selection forced: 0x%x\n", dytc_capabilities);
10620 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
10621 pr_debug("MMC is supported\n");
10623 * Check if MMC_GET functionality available
10624 * Version > 6 and return success from MMC_GET command
10626 dytc_mmc_get_available = false;
10627 if (dytc_version >= 6) {
10628 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10629 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10630 dytc_mmc_get_available = true;
10632 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
10633 pr_debug("PSC is supported\n");
10635 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
10639 dbg_printk(TPACPI_DBG_INIT,
10640 "DYTC version %d: thermal mode available\n", dytc_version);
10642 /* Create platform_profile structure and register */
10643 err = platform_profile_register(&dytc_profile);
10645 * If for some reason platform_profiles aren't enabled
10646 * don't quit terminally.
10651 /* Ensure initial values are correct */
10652 dytc_profile_refresh();
10654 /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
10655 if (dytc_capabilities & BIT(DYTC_FC_PSC))
10656 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
10661 static void dytc_profile_exit(void)
10663 platform_profile_remove();
10666 static struct ibm_struct dytc_profile_driver_data = {
10667 .name = "dytc-profile",
10668 .exit = dytc_profile_exit,
10671 /*************************************************************************
10672 * Keyboard language interface
10675 struct keyboard_lang_data {
10676 const char *lang_str;
10680 static const struct keyboard_lang_data keyboard_lang_data[] = {
10702 static int set_keyboard_lang_command(int command)
10704 acpi_handle sskl_handle;
10707 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10708 /* Platform doesn't support SSKL */
10712 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10718 static int get_keyboard_lang(int *output)
10720 acpi_handle gskl_handle;
10723 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10724 /* Platform doesn't support GSKL */
10728 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10732 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10733 * '(' and ')') keys which use layout dependent key-press emulation.
10735 if (kbd_lang & METHOD_ERR)
10738 *output = kbd_lang;
10743 /* sysfs keyboard language entry */
10744 static ssize_t keyboard_lang_show(struct device *dev,
10745 struct device_attribute *attr,
10748 int output, err, i, len = 0;
10750 err = get_keyboard_lang(&output);
10754 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10756 len += sysfs_emit_at(buf, len, "%s", " ");
10758 if (output == keyboard_lang_data[i].lang_code) {
10759 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10761 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10764 len += sysfs_emit_at(buf, len, "\n");
10769 static ssize_t keyboard_lang_store(struct device *dev,
10770 struct device_attribute *attr,
10771 const char *buf, size_t count)
10774 bool lang_found = false;
10777 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10778 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10779 lang_code = keyboard_lang_data[i].lang_code;
10786 lang_code = lang_code | 1 << 24;
10788 /* Set language code */
10789 err = set_keyboard_lang_command(lang_code);
10793 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10797 tpacpi_disclose_usertask(attr->attr.name,
10798 "keyboard language is set to %s\n", buf);
10800 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10804 static DEVICE_ATTR_RW(keyboard_lang);
10806 static struct attribute *kbdlang_attributes[] = {
10807 &dev_attr_keyboard_lang.attr,
10811 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10812 struct attribute *attr, int n)
10814 return tp_features.kbd_lang ? attr->mode : 0;
10817 static const struct attribute_group kbdlang_attr_group = {
10818 .is_visible = kbdlang_attr_is_visible,
10819 .attrs = kbdlang_attributes,
10822 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10826 err = get_keyboard_lang(&output);
10827 tp_features.kbd_lang = !err;
10831 static struct ibm_struct kbdlang_driver_data = {
10835 /*************************************************************************
10836 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10837 * and WLAN feature.
10839 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10840 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10841 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10842 static bool has_antennatype;
10843 static int wwan_antennatype;
10845 static int dprc_command(int command, int *output)
10847 acpi_handle dprc_handle;
10849 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10850 /* Platform doesn't support DPRC */
10854 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10858 * METHOD_ERR gets returned on devices where few commands are not supported
10859 * for example command to get WWAN Antenna type command is not supported on
10862 if (*output & METHOD_ERR)
10868 static int get_wwan_antenna(int *wwan_antennatype)
10872 /* Get current Antenna type */
10873 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10877 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10878 *wwan_antennatype = 1;
10879 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10880 *wwan_antennatype = 2;
10887 /* sysfs wwan antenna type entry */
10888 static ssize_t wwan_antenna_type_show(struct device *dev,
10889 struct device_attribute *attr,
10892 switch (wwan_antennatype) {
10894 return sysfs_emit(buf, "type a\n");
10896 return sysfs_emit(buf, "type b\n");
10901 static DEVICE_ATTR_RO(wwan_antenna_type);
10903 static struct attribute *dprc_attributes[] = {
10904 &dev_attr_wwan_antenna_type.attr,
10908 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10909 struct attribute *attr, int n)
10911 return has_antennatype ? attr->mode : 0;
10914 static const struct attribute_group dprc_attr_group = {
10915 .is_visible = dprc_attr_is_visible,
10916 .attrs = dprc_attributes,
10919 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10923 err = get_wwan_antenna(&wwan_antennatype);
10927 has_antennatype = true;
10931 static struct ibm_struct dprc_driver_data = {
10938 * This auxiliary mac address is enabled in the bios through the
10939 * MAC Address Pass-through feature. In most cases, there are three
10940 * possibilities: Internal Mac, Second Mac, and disabled.
10944 #define AUXMAC_LEN 12
10945 #define AUXMAC_START 9
10946 #define AUXMAC_STRLEN 22
10947 #define AUXMAC_BEGIN_MARKER 8
10948 #define AUXMAC_END_MARKER 21
10950 static char auxmac[AUXMAC_LEN + 1];
10952 static int auxmac_init(struct ibm_init_struct *iibm)
10954 acpi_status status;
10955 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
10956 union acpi_object *obj;
10958 status = acpi_evaluate_object(NULL, "\\MACA", NULL, &buffer);
10960 if (ACPI_FAILURE(status))
10963 obj = buffer.pointer;
10965 if (obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
10966 pr_info("Invalid buffer for MAC address pass-through.\n");
10967 goto auxmacinvalid;
10970 if (obj->string.pointer[AUXMAC_BEGIN_MARKER] != '#' ||
10971 obj->string.pointer[AUXMAC_END_MARKER] != '#') {
10972 pr_info("Invalid header for MAC address pass-through.\n");
10973 goto auxmacinvalid;
10976 if (strncmp(obj->string.pointer + AUXMAC_START, "XXXXXXXXXXXX", AUXMAC_LEN) != 0)
10977 strscpy(auxmac, obj->string.pointer + AUXMAC_START, sizeof(auxmac));
10979 strscpy(auxmac, "disabled", sizeof(auxmac));
10986 strscpy(auxmac, "unavailable", sizeof(auxmac));
10990 static struct ibm_struct auxmac_data = {
10994 static ssize_t auxmac_show(struct device *dev,
10995 struct device_attribute *attr,
10998 return sysfs_emit(buf, "%s\n", auxmac);
11000 static DEVICE_ATTR_RO(auxmac);
11002 static umode_t auxmac_attr_is_visible(struct kobject *kobj,
11003 struct attribute *attr, int n)
11005 return auxmac[0] == 0 ? 0 : attr->mode;
11008 static struct attribute *auxmac_attributes[] = {
11009 &dev_attr_auxmac.attr,
11013 static const struct attribute_group auxmac_attr_group = {
11014 .is_visible = auxmac_attr_is_visible,
11015 .attrs = auxmac_attributes,
11018 /* --------------------------------------------------------------------- */
11020 static struct attribute *tpacpi_driver_attributes[] = {
11021 &driver_attr_debug_level.attr,
11022 &driver_attr_version.attr,
11023 &driver_attr_interface_version.attr,
11024 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11025 &driver_attr_wlsw_emulstate.attr,
11026 &driver_attr_bluetooth_emulstate.attr,
11027 &driver_attr_wwan_emulstate.attr,
11028 &driver_attr_uwb_emulstate.attr,
11033 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11034 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
11035 struct attribute *attr, int n)
11037 if (attr == &driver_attr_wlsw_emulstate.attr) {
11040 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
11041 if (!dbg_bluetoothemul)
11043 } else if (attr == &driver_attr_wwan_emulstate.attr) {
11046 } else if (attr == &driver_attr_uwb_emulstate.attr) {
11055 static const struct attribute_group tpacpi_driver_attr_group = {
11056 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11057 .is_visible = tpacpi_attr_is_visible,
11059 .attrs = tpacpi_driver_attributes,
11062 static const struct attribute_group *tpacpi_driver_groups[] = {
11063 &tpacpi_driver_attr_group,
11067 static const struct attribute_group *tpacpi_groups[] = {
11068 &adaptive_kbd_attr_group,
11069 &hotkey_attr_group,
11070 &bluetooth_attr_group,
11073 &proxsensor_attr_group,
11074 &kbdlang_attr_group,
11076 &auxmac_attr_group,
11080 static const struct attribute_group *tpacpi_hwmon_groups[] = {
11081 &thermal_attr_group,
11082 &temp_label_attr_group,
11087 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
11088 &fan_driver_attr_group,
11092 /****************************************************************************
11093 ****************************************************************************
11097 ****************************************************************************
11098 ****************************************************************************/
11100 static struct platform_driver tpacpi_pdriver = {
11102 .name = TPACPI_DRVR_NAME,
11104 .groups = tpacpi_driver_groups,
11105 .dev_groups = tpacpi_groups,
11107 .shutdown = tpacpi_shutdown_handler,
11110 static struct platform_driver tpacpi_hwmon_pdriver = {
11112 .name = TPACPI_HWMON_DRVR_NAME,
11113 .groups = tpacpi_hwmon_driver_groups,
11117 /****************************************************************************
11118 ****************************************************************************
11122 ****************************************************************************
11123 ****************************************************************************/
11126 * HKEY event callout for other subdrivers go here
11127 * (yes, it is ugly, but it is quick, safe, and gets the job done
11129 static void tpacpi_driver_event(const unsigned int hkey_event)
11131 if (ibm_backlight_device) {
11132 switch (hkey_event) {
11133 case TP_HKEY_EV_BRGHT_UP:
11134 case TP_HKEY_EV_BRGHT_DOWN:
11135 tpacpi_brightness_notify_change();
11139 switch (hkey_event) {
11140 case TP_HKEY_EV_VOL_UP:
11141 case TP_HKEY_EV_VOL_DOWN:
11142 case TP_HKEY_EV_VOL_MUTE:
11143 volume_alsa_notify_change();
11146 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
11147 enum led_brightness brightness;
11149 mutex_lock(&kbdlight_mutex);
11152 * Check the brightness actually changed, setting the brightness
11153 * through kbdlight_set_level() also triggers this event.
11155 brightness = kbdlight_sysfs_get(NULL);
11156 if (kbdlight_brightness != brightness) {
11157 kbdlight_brightness = brightness;
11158 led_classdev_notify_brightness_hw_changed(
11159 &tpacpi_led_kbdlight.led_classdev, brightness);
11162 mutex_unlock(&kbdlight_mutex);
11165 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
11166 lapsensor_refresh();
11167 /* If we are already accessing DYTC then skip dytc update */
11168 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
11169 dytc_profile_refresh();
11172 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
11173 enum drm_privacy_screen_status old_hw_state;
11176 mutex_lock(&lcdshadow_dev->lock);
11177 old_hw_state = lcdshadow_dev->hw_state;
11178 lcdshadow_get_hw_state(lcdshadow_dev);
11179 changed = lcdshadow_dev->hw_state != old_hw_state;
11180 mutex_unlock(&lcdshadow_dev->lock);
11183 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
11185 if (hkey_event == TP_HKEY_EV_AMT_TOGGLE) {
11186 /* If we're enabling AMT we need to force balanced mode */
11187 if (!dytc_amt_active)
11188 /* This will also set AMT mode enabled */
11189 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
11191 dytc_control_amt(!dytc_amt_active);
11193 if (hkey_event == TP_HKEY_EV_PROFILE_TOGGLE) {
11194 switch (dytc_current_profile) {
11195 case PLATFORM_PROFILE_LOW_POWER:
11196 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
11198 case PLATFORM_PROFILE_BALANCED:
11199 dytc_profile_set(NULL, PLATFORM_PROFILE_PERFORMANCE);
11201 case PLATFORM_PROFILE_PERFORMANCE:
11202 dytc_profile_set(NULL, PLATFORM_PROFILE_LOW_POWER);
11205 pr_warn("Profile HKEY unexpected profile %d", dytc_current_profile);
11207 /* Notify user space the profile changed */
11208 platform_profile_notify();
11212 static void hotkey_driver_event(const unsigned int scancode)
11214 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
11217 /* --------------------------------------------------------------------- */
11219 /* /proc support */
11220 static struct proc_dir_entry *proc_dir;
11223 * Module and infrastructure proble, init and exit handling
11226 static bool force_load;
11228 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
11229 static const char * __init str_supported(int is_supported)
11231 static char text_unsupported[] __initdata = "not supported";
11233 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
11235 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
11237 static void ibm_exit(struct ibm_struct *ibm)
11239 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
11241 list_del_init(&ibm->all_drivers);
11243 if (ibm->flags.acpi_notify_installed) {
11244 dbg_printk(TPACPI_DBG_EXIT,
11245 "%s: acpi_remove_notify_handler\n", ibm->name);
11246 BUG_ON(!ibm->acpi);
11247 acpi_remove_notify_handler(*ibm->acpi->handle,
11249 dispatch_acpi_notify);
11250 ibm->flags.acpi_notify_installed = 0;
11253 if (ibm->flags.proc_created) {
11254 dbg_printk(TPACPI_DBG_EXIT,
11255 "%s: remove_proc_entry\n", ibm->name);
11256 remove_proc_entry(ibm->name, proc_dir);
11257 ibm->flags.proc_created = 0;
11260 if (ibm->flags.acpi_driver_registered) {
11261 dbg_printk(TPACPI_DBG_EXIT,
11262 "%s: acpi_bus_unregister_driver\n", ibm->name);
11263 BUG_ON(!ibm->acpi);
11264 acpi_bus_unregister_driver(ibm->acpi->driver);
11265 kfree(ibm->acpi->driver);
11266 ibm->acpi->driver = NULL;
11267 ibm->flags.acpi_driver_registered = 0;
11270 if (ibm->flags.init_called && ibm->exit) {
11272 ibm->flags.init_called = 0;
11275 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
11278 static int __init ibm_init(struct ibm_init_struct *iibm)
11281 struct ibm_struct *ibm = iibm->data;
11282 struct proc_dir_entry *entry;
11284 BUG_ON(ibm == NULL);
11286 INIT_LIST_HEAD(&ibm->all_drivers);
11288 if (ibm->flags.experimental && !experimental)
11291 dbg_printk(TPACPI_DBG_INIT,
11292 "probing for %s\n", ibm->name);
11295 ret = iibm->init(iibm);
11296 if (ret > 0 || ret == -ENODEV)
11297 return 0; /* subdriver functionality not available */
11301 ibm->flags.init_called = 1;
11305 if (ibm->acpi->hid) {
11306 ret = register_tpacpi_subdriver(ibm);
11311 if (ibm->acpi->notify) {
11312 ret = setup_acpi_notify(ibm);
11313 if (ret == -ENODEV) {
11314 pr_notice("disabling subdriver %s\n",
11324 dbg_printk(TPACPI_DBG_INIT,
11325 "%s installed\n", ibm->name);
11328 umode_t mode = iibm->base_procfs_mode;
11334 entry = proc_create_data(ibm->name, mode, proc_dir,
11335 &dispatch_proc_ops, ibm);
11337 pr_err("unable to create proc entry %s\n", ibm->name);
11341 ibm->flags.proc_created = 1;
11344 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
11349 dbg_printk(TPACPI_DBG_INIT,
11350 "%s: at error exit path with result %d\n",
11354 return (ret < 0) ? ret : 0;
11359 static char __init tpacpi_parse_fw_id(const char * const s,
11360 u32 *model, u16 *release)
11364 if (!s || strlen(s) < 8)
11367 for (i = 0; i < 8; i++)
11368 if (!((s[i] >= '0' && s[i] <= '9') ||
11369 (s[i] >= 'A' && s[i] <= 'Z')))
11373 * Most models: xxyTkkWW (#.##c)
11374 * Ancient 570/600 and -SL lacks (#.##c)
11376 if (s[3] == 'T' || s[3] == 'N') {
11377 *model = TPID(s[0], s[1]);
11378 *release = TPVER(s[4], s[5]);
11381 /* New models: xxxyTkkW (#.##c); T550 and some others */
11382 } else if (s[4] == 'T' || s[4] == 'N') {
11383 *model = TPID3(s[0], s[1], s[2]);
11384 *release = TPVER(s[5], s[6]);
11392 #define EC_FW_STRING_LEN 18
11394 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
11396 char *ec_fw_string = (char *) private;
11397 const char *dmi_data = (const char *)dm;
11399 * ThinkPad Embedded Controller Program Table on newer models
11401 * Offset | Name | Width | Description
11402 * ----------------------------------------------------
11403 * 0x00 | Type | BYTE | 0x8C
11404 * 0x01 | Length | BYTE |
11405 * 0x02 | Handle | WORD | Varies
11406 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11407 * 0x0A | OEM struct offset | BYTE | 0x0B
11408 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11409 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11410 * 0x0D | ECP version ID | STR ID |
11411 * 0x0E | ECP release date | STR ID |
11414 /* Return if data structure not match */
11415 if (dm->type != 140 || dm->length < 0x0F ||
11416 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11417 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11418 dmi_data[0x0C] != 0x01)
11421 /* fwstr is the first 8byte string */
11422 BUILD_BUG_ON(EC_FW_STRING_LEN <= 8);
11423 memcpy(ec_fw_string, dmi_data + 0x0F, 8);
11426 /* returns 0 - probe ok, or < 0 - probe error.
11427 * Probe ok doesn't mean thinkpad found.
11428 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11429 static int __must_check __init get_thinkpad_model_data(
11430 struct thinkpad_id_data *tp)
11432 const struct dmi_device *dev = NULL;
11433 char ec_fw_string[EC_FW_STRING_LEN] = {0};
11440 memset(tp, 0, sizeof(*tp));
11442 if (dmi_name_in_vendors("IBM"))
11443 tp->vendor = PCI_VENDOR_ID_IBM;
11444 else if (dmi_name_in_vendors("LENOVO"))
11445 tp->vendor = PCI_VENDOR_ID_LENOVO;
11449 s = dmi_get_system_info(DMI_BIOS_VERSION);
11450 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11451 if (s && !tp->bios_version_str)
11454 /* Really ancient ThinkPad 240X will fail this, which is fine */
11455 t = tpacpi_parse_fw_id(tp->bios_version_str,
11456 &tp->bios_model, &tp->bios_release);
11457 if (t != 'E' && t != 'C')
11461 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11462 * X32 or newer, all Z series; Some models must have an
11463 * up-to-date BIOS or they will not be detected.
11465 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11467 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11468 if (sscanf(dev->name,
11469 "IBM ThinkPad Embedded Controller -[%17c",
11470 ec_fw_string) == 1) {
11471 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11472 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11477 /* Newer ThinkPads have different EC program info table */
11478 if (!ec_fw_string[0])
11479 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11481 if (ec_fw_string[0]) {
11482 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11483 if (!tp->ec_version_str)
11486 t = tpacpi_parse_fw_id(ec_fw_string,
11487 &tp->ec_model, &tp->ec_release);
11489 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11491 pr_notice("please report this to %s\n", TPACPI_MAIL);
11495 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11496 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11497 tp->model_str = kstrdup(s, GFP_KERNEL);
11498 if (!tp->model_str)
11501 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11502 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11503 tp->model_str = kstrdup(s, GFP_KERNEL);
11504 if (!tp->model_str)
11509 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11510 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11511 if (s && !tp->nummodel_str)
11517 static int __init probe_for_thinkpad(void)
11524 /* It would be dangerous to run the driver in this case */
11525 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11529 * Non-ancient models have better DMI tagging, but very old models
11530 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11532 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11533 (thinkpad_id.ec_model != 0) ||
11534 tpacpi_is_fw_known();
11536 /* The EC handler is required */
11537 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11540 pr_err("Not yet supported ThinkPad detected!\n");
11544 if (!is_thinkpad && !force_load)
11550 static void __init thinkpad_acpi_init_banner(void)
11552 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11553 pr_info("%s\n", TPACPI_URL);
11555 pr_info("ThinkPad BIOS %s, EC %s\n",
11556 (thinkpad_id.bios_version_str) ?
11557 thinkpad_id.bios_version_str : "unknown",
11558 (thinkpad_id.ec_version_str) ?
11559 thinkpad_id.ec_version_str : "unknown");
11561 BUG_ON(!thinkpad_id.vendor);
11563 if (thinkpad_id.model_str)
11564 pr_info("%s %s, model %s\n",
11565 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11566 "IBM" : ((thinkpad_id.vendor ==
11567 PCI_VENDOR_ID_LENOVO) ?
11568 "Lenovo" : "Unknown vendor"),
11569 thinkpad_id.model_str,
11570 (thinkpad_id.nummodel_str) ?
11571 thinkpad_id.nummodel_str : "unknown");
11574 /* Module init, exit, parameters */
11576 static struct ibm_init_struct ibms_init[] __initdata = {
11578 .data = &thinkpad_acpi_driver_data,
11581 .init = hotkey_init,
11582 .data = &hotkey_driver_data,
11585 .init = bluetooth_init,
11586 .data = &bluetooth_driver_data,
11590 .data = &wan_driver_data,
11594 .data = &uwb_driver_data,
11596 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11598 .init = video_init,
11599 .base_procfs_mode = S_IRUSR,
11600 .data = &video_driver_data,
11604 .init = kbdlight_init,
11605 .data = &kbdlight_driver_data,
11608 .init = light_init,
11609 .data = &light_driver_data,
11613 .data = &cmos_driver_data,
11617 .data = &led_driver_data,
11621 .data = &beep_driver_data,
11624 .init = thermal_init,
11625 .data = &thermal_driver_data,
11628 .init = brightness_init,
11629 .data = &brightness_driver_data,
11632 .init = volume_init,
11633 .data = &volume_driver_data,
11637 .data = &fan_driver_data,
11640 .init = mute_led_init,
11641 .data = &mute_led_driver_data,
11644 .init = tpacpi_battery_init,
11645 .data = &battery_driver_data,
11648 .init = tpacpi_lcdshadow_init,
11649 .data = &lcdshadow_driver_data,
11652 .init = tpacpi_proxsensor_init,
11653 .data = &proxsensor_driver_data,
11656 .init = tpacpi_dytc_profile_init,
11657 .data = &dytc_profile_driver_data,
11660 .init = tpacpi_kbdlang_init,
11661 .data = &kbdlang_driver_data,
11664 .init = tpacpi_dprc_init,
11665 .data = &dprc_driver_data,
11668 .init = auxmac_init,
11669 .data = &auxmac_data,
11673 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11676 struct ibm_struct *ibm;
11678 if (!kp || !kp->name || !val)
11681 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11682 ibm = ibms_init[i].data;
11683 if (!ibm || !ibm->name)
11686 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11687 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11689 strcpy(ibms_init[i].param, val);
11697 module_param(experimental, int, 0444);
11698 MODULE_PARM_DESC(experimental,
11699 "Enables experimental features when non-zero");
11701 module_param_named(debug, dbg_level, uint, 0);
11702 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11704 module_param(force_load, bool, 0444);
11705 MODULE_PARM_DESC(force_load,
11706 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11708 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11709 MODULE_PARM_DESC(fan_control,
11710 "Enables setting fan parameters features when true");
11712 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11713 MODULE_PARM_DESC(brightness_mode,
11714 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11716 module_param(brightness_enable, uint, 0444);
11717 MODULE_PARM_DESC(brightness_enable,
11718 "Enables backlight control when 1, disables when 0");
11720 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11721 module_param_named(volume_mode, volume_mode, uint, 0444);
11722 MODULE_PARM_DESC(volume_mode,
11723 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11725 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11726 MODULE_PARM_DESC(volume_capabilities,
11727 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11729 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11730 MODULE_PARM_DESC(volume_control,
11731 "Enables software override for the console audio control when true");
11733 module_param_named(software_mute, software_mute_requested, bool, 0444);
11734 MODULE_PARM_DESC(software_mute,
11735 "Request full software mute control");
11737 /* ALSA module API parameters */
11738 module_param_named(index, alsa_index, int, 0444);
11739 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11740 module_param_named(id, alsa_id, charp, 0444);
11741 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11742 module_param_named(enable, alsa_enable, bool, 0444);
11743 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11744 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11746 /* The module parameter can't be read back, that's why 0 is used here */
11747 #define TPACPI_PARAM(feature) \
11748 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11749 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11751 TPACPI_PARAM(hotkey);
11752 TPACPI_PARAM(bluetooth);
11753 TPACPI_PARAM(video);
11754 TPACPI_PARAM(light);
11755 TPACPI_PARAM(cmos);
11757 TPACPI_PARAM(beep);
11758 TPACPI_PARAM(brightness);
11759 TPACPI_PARAM(volume);
11762 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11763 module_param(dbg_wlswemul, uint, 0444);
11764 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11765 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11766 MODULE_PARM_DESC(wlsw_state,
11767 "Initial state of the emulated WLSW switch");
11769 module_param(dbg_bluetoothemul, uint, 0444);
11770 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11771 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11772 MODULE_PARM_DESC(bluetooth_state,
11773 "Initial state of the emulated bluetooth switch");
11775 module_param(dbg_wwanemul, uint, 0444);
11776 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11777 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11778 MODULE_PARM_DESC(wwan_state,
11779 "Initial state of the emulated WWAN switch");
11781 module_param(dbg_uwbemul, uint, 0444);
11782 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11783 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11784 MODULE_PARM_DESC(uwb_state,
11785 "Initial state of the emulated UWB switch");
11788 module_param(profile_force, int, 0444);
11789 MODULE_PARM_DESC(profile_force, "Force profile mode. -1=off, 1=MMC, 2=PSC");
11791 static void thinkpad_acpi_module_exit(void)
11793 struct ibm_struct *ibm, *itmp;
11795 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11798 hwmon_device_unregister(tpacpi_hwmon);
11799 if (tp_features.sensors_pdrv_registered)
11800 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11801 if (tp_features.platform_drv_registered)
11802 platform_driver_unregister(&tpacpi_pdriver);
11804 list_for_each_entry_safe_reverse(ibm, itmp,
11805 &tpacpi_all_drivers,
11810 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11812 if (tpacpi_inputdev) {
11813 if (tp_features.input_device_registered)
11814 input_unregister_device(tpacpi_inputdev);
11816 input_free_device(tpacpi_inputdev);
11817 kfree(hotkey_keycode_map);
11820 if (tpacpi_sensors_pdev)
11821 platform_device_unregister(tpacpi_sensors_pdev);
11823 platform_device_unregister(tpacpi_pdev);
11825 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11827 destroy_workqueue(tpacpi_wq);
11829 kfree(thinkpad_id.bios_version_str);
11830 kfree(thinkpad_id.ec_version_str);
11831 kfree(thinkpad_id.model_str);
11832 kfree(thinkpad_id.nummodel_str);
11836 static int __init thinkpad_acpi_module_init(void)
11838 const struct dmi_system_id *dmi_id;
11840 acpi_object_type obj_type;
11842 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11844 /* Driver-level probe */
11846 ret = get_thinkpad_model_data(&thinkpad_id);
11848 pr_err("unable to get DMI data: %d\n", ret);
11849 thinkpad_acpi_module_exit();
11852 ret = probe_for_thinkpad();
11854 thinkpad_acpi_module_exit();
11858 /* Driver initialization */
11860 thinkpad_acpi_init_banner();
11861 tpacpi_check_outdated_fw();
11863 TPACPI_ACPIHANDLE_INIT(ecrd);
11864 TPACPI_ACPIHANDLE_INIT(ecwr);
11867 * Quirk: in some models (e.g. X380 Yoga), an object named ECRD
11868 * exists, but it is a register, not a method.
11871 acpi_get_type(ecrd_handle, &obj_type);
11872 if (obj_type != ACPI_TYPE_METHOD)
11873 ecrd_handle = NULL;
11876 acpi_get_type(ecwr_handle, &obj_type);
11877 if (obj_type != ACPI_TYPE_METHOD)
11878 ecwr_handle = NULL;
11881 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11883 thinkpad_acpi_module_exit();
11887 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11889 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11890 thinkpad_acpi_module_exit();
11894 dmi_id = dmi_first_match(fwbug_list);
11896 tp_features.quirks = dmi_id->driver_data;
11898 /* Device initialization */
11899 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
11901 if (IS_ERR(tpacpi_pdev)) {
11902 ret = PTR_ERR(tpacpi_pdev);
11903 tpacpi_pdev = NULL;
11904 pr_err("unable to register platform device\n");
11905 thinkpad_acpi_module_exit();
11908 tpacpi_sensors_pdev = platform_device_register_simple(
11909 TPACPI_HWMON_DRVR_NAME,
11910 PLATFORM_DEVID_NONE, NULL, 0);
11911 if (IS_ERR(tpacpi_sensors_pdev)) {
11912 ret = PTR_ERR(tpacpi_sensors_pdev);
11913 tpacpi_sensors_pdev = NULL;
11914 pr_err("unable to register hwmon platform device\n");
11915 thinkpad_acpi_module_exit();
11919 mutex_init(&tpacpi_inputdev_send_mutex);
11920 tpacpi_inputdev = input_allocate_device();
11921 if (!tpacpi_inputdev) {
11922 thinkpad_acpi_module_exit();
11925 /* Prepare input device, but don't register */
11926 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11927 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11928 tpacpi_inputdev->id.bustype = BUS_HOST;
11929 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11930 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11931 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11932 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11935 /* Init subdriver dependencies */
11936 tpacpi_detect_brightness_capabilities();
11938 /* Init subdrivers */
11939 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11940 ret = ibm_init(&ibms_init[i]);
11941 if (ret >= 0 && *ibms_init[i].param)
11942 ret = ibms_init[i].data->write(ibms_init[i].param);
11944 thinkpad_acpi_module_exit();
11949 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11951 ret = platform_driver_register(&tpacpi_pdriver);
11953 pr_err("unable to register main platform driver\n");
11954 thinkpad_acpi_module_exit();
11957 tp_features.platform_drv_registered = 1;
11959 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11961 pr_err("unable to register hwmon platform driver\n");
11962 thinkpad_acpi_module_exit();
11965 tp_features.sensors_pdrv_registered = 1;
11967 tpacpi_hwmon = hwmon_device_register_with_groups(
11968 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11969 if (IS_ERR(tpacpi_hwmon)) {
11970 ret = PTR_ERR(tpacpi_hwmon);
11971 tpacpi_hwmon = NULL;
11972 pr_err("unable to register hwmon device\n");
11973 thinkpad_acpi_module_exit();
11977 ret = input_register_device(tpacpi_inputdev);
11979 pr_err("unable to register input device\n");
11980 thinkpad_acpi_module_exit();
11983 tp_features.input_device_registered = 1;
11989 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11992 * This will autoload the driver in almost every ThinkPad
11993 * in widespread use.
11995 * Only _VERY_ old models, like the 240, 240x and 570 lack
11996 * the HKEY event interface.
11998 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
12001 * DMI matching for module autoloading
12003 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
12004 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
12006 * Only models listed in thinkwiki will be supported, so add yours
12007 * if it is not there yet.
12009 #define IBM_BIOS_MODULE_ALIAS(__type) \
12010 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
12012 /* Ancient thinkpad BIOSes have to be identified by
12013 * BIOS type or model number, and there are far less
12014 * BIOS types than model numbers... */
12015 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
12017 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
12018 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
12019 MODULE_DESCRIPTION(TPACPI_DESC);
12020 MODULE_VERSION(TPACPI_VERSION);
12021 MODULE_LICENSE("GPL");
12023 module_init(thinkpad_acpi_module_init);
12024 module_exit(thinkpad_acpi_module_exit);