1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * thinkpad_acpi.c - ThinkPad ACPI Extras
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
28 * 2005-01-16 0.9 use MODULE_VERSION
30 * fix parameter passing on module loading
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
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/module.h>
54 #include <linux/mutex.h>
55 #include <linux/nvram.h>
56 #include <linux/pci.h>
57 #include <linux/platform_device.h>
58 #include <linux/platform_profile.h>
59 #include <linux/power_supply.h>
60 #include <linux/proc_fs.h>
61 #include <linux/rfkill.h>
62 #include <linux/sched.h>
63 #include <linux/sched/signal.h>
64 #include <linux/seq_file.h>
65 #include <linux/slab.h>
66 #include <linux/string.h>
67 #include <linux/string_helpers.h>
68 #include <linux/sysfs.h>
69 #include <linux/types.h>
70 #include <linux/uaccess.h>
71 #include <linux/workqueue.h>
73 #include <acpi/battery.h>
74 #include <acpi/video.h>
76 #include <drm/drm_privacy_screen_driver.h>
78 #include <sound/control.h>
79 #include <sound/core.h>
80 #include <sound/initval.h>
82 #include "dual_accel_detect.h"
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* Misc NVRAM-related */
121 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
125 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
126 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
127 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
128 #define TPACPI_ACPI_EC_HID "PNP0C09"
131 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
132 #define TPACPI_HKEY_INPUT_VERSION 0x4101
134 /* ACPI \WGSV commands */
136 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
137 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
138 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
139 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
142 /* TP_ACPI_WGSV_GET_STATE bits */
144 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
145 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
146 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
147 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
148 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
149 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
150 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
151 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
152 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
153 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
157 enum tpacpi_hkey_event_t {
159 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
160 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
161 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
162 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
163 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
164 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
165 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
166 TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
167 TP_HKEY_EV_AMT_TOGGLE = 0x131a, /* Toggle AMT on/off */
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
183 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
184 or port replicator */
185 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
186 dock or port replicator */
188 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
189 * when keyboard cover is attached, detached or folded onto the back
191 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
192 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
194 /* User-interface events */
195 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
196 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
197 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
198 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
199 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
200 * enter/leave tablet mode
202 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
203 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
204 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
206 /* Key-related user-interface events */
207 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
208 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
209 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
212 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
213 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
214 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
215 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
216 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
217 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
218 * command completed. Related to
220 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
221 * changed. Related to AML GMTS */
223 /* AC-related events */
224 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
226 /* Further user-interface events */
227 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
228 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
231 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
234 /****************************************************************************
238 #define TPACPI_NAME "thinkpad"
239 #define TPACPI_DESC "ThinkPad ACPI Extras"
240 #define TPACPI_FILE TPACPI_NAME "_acpi"
241 #define TPACPI_URL "http://ibm-acpi.sf.net/"
244 #define TPACPI_PROC_DIR "ibm"
245 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
246 #define TPACPI_DRVR_NAME TPACPI_FILE
247 #define TPACPI_DRVR_SHORTNAME "tpacpi"
248 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
250 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
251 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
253 #define TPACPI_MAX_ACPI_ARGS 3
255 /* Debugging printk groups */
256 #define TPACPI_DBG_ALL 0xffff
257 #define TPACPI_DBG_DISCLOSETASK 0x8000
258 #define TPACPI_DBG_INIT 0x0001
259 #define TPACPI_DBG_EXIT 0x0002
260 #define TPACPI_DBG_RFKILL 0x0004
261 #define TPACPI_DBG_HKEY 0x0008
262 #define TPACPI_DBG_FAN 0x0010
263 #define TPACPI_DBG_BRGHT 0x0020
264 #define TPACPI_DBG_MIXER 0x0040
266 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
269 /****************************************************************************
270 * Driver-wide structs and misc. variables
275 struct tp_acpi_drv_struct {
276 const struct acpi_device_id *hid;
277 struct acpi_driver *driver;
279 void (*notify) (struct ibm_struct *, u32);
282 struct acpi_device *device;
288 int (*read) (struct seq_file *);
289 int (*write) (char *);
291 void (*resume) (void);
292 void (*suspend) (void);
293 void (*shutdown) (void);
295 struct list_head all_drivers;
297 struct tp_acpi_drv_struct *acpi;
300 u8 acpi_driver_registered:1;
301 u8 acpi_notify_installed:1;
308 struct ibm_init_struct {
311 int (*init) (struct ibm_init_struct *);
312 umode_t base_procfs_mode;
313 struct ibm_struct *data;
322 static struct quirk_entry quirk_btusb_bug = {
326 static struct quirk_entry quirk_s2idle_bug = {
327 .s2idle_bug_mmio = 0xfed80380,
336 TP_HOTKEY_TABLET_NONE = 0,
337 TP_HOTKEY_TABLET_USES_MHKG,
338 TP_HOTKEY_TABLET_USES_GMMS,
343 u32 bright_acpimode:1;
347 u32 fan_ctrl_status_undef:1;
349 u32 second_fan_ctl:1;
350 u32 beep_needs_two_args:1;
351 u32 mixer_no_level_control:1;
352 u32 battery_force_primary:1;
353 u32 input_device_registered:1;
354 u32 platform_drv_registered:1;
355 u32 sensors_pdrv_registered:1;
356 u32 hotkey_poll_active:1;
357 u32 has_adaptive_kbd:1;
359 struct quirk_entry *quirks;
363 u16 hotkey_mask_ff:1;
364 u16 volume_ctrl_forbidden:1;
367 struct thinkpad_id_data {
368 unsigned int vendor; /* ThinkPad vendor:
369 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
371 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
372 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
374 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
376 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
379 char *model_str; /* ThinkPad T43 */
380 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
382 static struct thinkpad_id_data thinkpad_id;
385 TPACPI_LIFE_INIT = 0,
390 static int experimental;
391 static u32 dbg_level;
393 static struct workqueue_struct *tpacpi_wq;
401 /* tpacpi LED class */
402 struct tpacpi_led_classdev {
403 struct led_classdev led_classdev;
407 /* brightness level capabilities */
408 static unsigned int bright_maxlvl; /* 0 = unknown */
410 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
411 static int dbg_wlswemul;
412 static bool tpacpi_wlsw_emulstate;
413 static int dbg_bluetoothemul;
414 static bool tpacpi_bluetooth_emulstate;
415 static int dbg_wwanemul;
416 static bool tpacpi_wwan_emulstate;
417 static int dbg_uwbemul;
418 static bool tpacpi_uwb_emulstate;
422 /*************************************************************************
426 #define dbg_printk(a_dbg_level, format, arg...) \
428 if (dbg_level & (a_dbg_level)) \
429 printk(KERN_DEBUG pr_fmt("%s: " format), \
433 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
434 #define vdbg_printk dbg_printk
435 static const char *str_supported(int is_supported);
437 static inline const char *str_supported(int is_supported) { return ""; }
438 #define vdbg_printk(a_dbg_level, format, arg...) \
439 do { if (0) no_printk(format, ##arg); } while (0)
442 static void tpacpi_log_usertask(const char * const what)
444 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
445 what, task_tgid_vnr(current));
448 #define tpacpi_disclose_usertask(what, format, arg...) \
450 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
451 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
452 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
453 what, task_tgid_vnr(current), ## arg); \
458 * Quirk handling helpers
460 * ThinkPad IDs and versions seen in the field so far are
461 * two or three characters from the set [0-9A-Z], i.e. base 36.
463 * We use values well outside that range as specials.
466 #define TPACPI_MATCH_ANY 0xffffffffU
467 #define TPACPI_MATCH_ANY_VERSION 0xffffU
468 #define TPACPI_MATCH_UNKNOWN 0U
470 /* TPID('1', 'Y') == 0x3159 */
471 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
472 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
475 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
476 { .vendor = PCI_VENDOR_ID_IBM, \
477 .bios = TPID(__id1, __id2), \
478 .ec = TPACPI_MATCH_ANY, \
479 .quirks = (__quirk) }
481 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
482 { .vendor = PCI_VENDOR_ID_LENOVO, \
483 .bios = TPID(__id1, __id2), \
484 .ec = TPACPI_MATCH_ANY, \
485 .quirks = (__quirk) }
487 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
488 { .vendor = PCI_VENDOR_ID_LENOVO, \
489 .bios = TPID3(__id1, __id2, __id3), \
490 .ec = TPACPI_MATCH_ANY, \
491 .quirks = (__quirk) }
493 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
494 { .vendor = PCI_VENDOR_ID_IBM, \
495 .bios = TPACPI_MATCH_ANY, \
496 .ec = TPID(__id1, __id2), \
497 .quirks = (__quirk) }
499 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
500 { .vendor = PCI_VENDOR_ID_LENOVO, \
501 .bios = TPACPI_MATCH_ANY, \
502 .ec = TPID(__id1, __id2), \
503 .quirks = (__quirk) }
505 struct tpacpi_quirk {
509 unsigned long quirks;
513 * tpacpi_check_quirks() - search BIOS/EC version on a list
514 * @qlist: array of &struct tpacpi_quirk
515 * @qlist_size: number of elements in @qlist
517 * Iterates over a quirks list until one is found that matches the
518 * ThinkPad's vendor, BIOS and EC model.
520 * Returns 0 if nothing matches, otherwise returns the quirks field of
521 * the matching &struct tpacpi_quirk entry.
523 * The match criteria is: vendor, ec and bios much match.
525 static unsigned long __init tpacpi_check_quirks(
526 const struct tpacpi_quirk *qlist,
527 unsigned int qlist_size)
530 if ((qlist->vendor == thinkpad_id.vendor ||
531 qlist->vendor == TPACPI_MATCH_ANY) &&
532 (qlist->bios == thinkpad_id.bios_model ||
533 qlist->bios == TPACPI_MATCH_ANY) &&
534 (qlist->ec == thinkpad_id.ec_model ||
535 qlist->ec == TPACPI_MATCH_ANY))
536 return qlist->quirks;
544 static inline bool __pure __init tpacpi_is_lenovo(void)
546 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
549 static inline bool __pure __init tpacpi_is_ibm(void)
551 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
554 /****************************************************************************
555 ****************************************************************************
557 * ACPI Helpers and device model
559 ****************************************************************************
560 ****************************************************************************/
562 /*************************************************************************
566 static acpi_handle root_handle;
567 static acpi_handle ec_handle;
569 #define TPACPI_HANDLE(object, parent, paths...) \
570 static acpi_handle object##_handle; \
571 static const acpi_handle * const object##_parent __initconst = \
573 static char *object##_paths[] __initdata = { paths }
575 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
576 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
578 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
580 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
581 "\\CMS", /* R40, R40e */
584 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
585 "^HKEY", /* R30, R31 */
586 "HKEY", /* all others */
589 /*************************************************************************
593 static int acpi_evalf(acpi_handle handle,
594 int *res, char *method, char *fmt, ...)
597 struct acpi_object_list params;
598 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
599 struct acpi_buffer result, *resultp;
600 union acpi_object out_obj;
608 pr_err("acpi_evalf() called with empty format\n");
621 params.pointer = &in_objs[0];
628 in_objs[params.count].integer.value = va_arg(ap, int);
629 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
631 /* add more types as needed */
633 pr_err("acpi_evalf() called with invalid format character '%c'\n",
641 if (res_type != 'v') {
642 result.length = sizeof(out_obj);
643 result.pointer = &out_obj;
648 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
652 success = (status == AE_OK &&
653 out_obj.type == ACPI_TYPE_INTEGER);
655 *res = out_obj.integer.value;
658 success = status == AE_OK;
660 /* add more types as needed */
662 pr_err("acpi_evalf() called with invalid format character '%c'\n",
667 if (!success && !quiet)
668 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
669 method, fmt0, acpi_format_exception(status));
674 static int acpi_ec_read(int i, u8 *p)
679 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
683 if (ec_read(i, p) < 0)
690 static int acpi_ec_write(int i, u8 v)
693 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
696 if (ec_write(i, v) < 0)
703 static int issue_thinkpad_cmos_command(int cmos_cmd)
708 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
714 /*************************************************************************
718 #define TPACPI_ACPIHANDLE_INIT(object) \
719 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
720 object##_paths, ARRAY_SIZE(object##_paths))
722 static void __init drv_acpi_handle_init(const char *name,
723 acpi_handle *handle, const acpi_handle parent,
724 char **paths, const int num_paths)
729 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
732 for (i = 0; i < num_paths; i++) {
733 status = acpi_get_handle(parent, paths[i], handle);
734 if (ACPI_SUCCESS(status)) {
735 dbg_printk(TPACPI_DBG_INIT,
736 "Found ACPI handle %s for %s\n",
742 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
747 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
748 u32 level, void *context, void **return_value)
750 if (!strcmp(context, "video")) {
751 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
753 if (!dev || strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
757 *(acpi_handle *)return_value = handle;
759 return AE_CTRL_TERMINATE;
762 static void __init tpacpi_acpi_handle_locate(const char *name,
767 acpi_handle device_found;
769 BUG_ON(!name || !handle);
770 vdbg_printk(TPACPI_DBG_INIT,
771 "trying to locate ACPI handle for %s, using HID %s\n",
772 name, hid ? hid : "NULL");
774 memset(&device_found, 0, sizeof(device_found));
775 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
776 (void *)name, &device_found);
780 if (ACPI_SUCCESS(status)) {
781 *handle = device_found;
782 dbg_printk(TPACPI_DBG_INIT,
783 "Found ACPI handle for %s\n", name);
785 vdbg_printk(TPACPI_DBG_INIT,
786 "Could not locate an ACPI handle for %s: %s\n",
787 name, acpi_format_exception(status));
791 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
793 struct ibm_struct *ibm = data;
795 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
798 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
801 ibm->acpi->notify(ibm, event);
804 static int __init setup_acpi_notify(struct ibm_struct *ibm)
810 if (!*ibm->acpi->handle)
813 vdbg_printk(TPACPI_DBG_INIT,
814 "setting up ACPI notify for %s\n", ibm->name);
816 ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
817 if (!ibm->acpi->device) {
818 pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
822 ibm->acpi->device->driver_data = ibm;
823 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
824 TPACPI_ACPI_EVENT_PREFIX,
827 status = acpi_install_notify_handler(*ibm->acpi->handle,
828 ibm->acpi->type, dispatch_acpi_notify, ibm);
829 if (ACPI_FAILURE(status)) {
830 if (status == AE_ALREADY_EXISTS) {
831 pr_notice("another device driver is already handling %s events\n",
834 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
835 ibm->name, acpi_format_exception(status));
839 ibm->flags.acpi_notify_installed = 1;
843 static int __init tpacpi_device_add(struct acpi_device *device)
848 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
852 dbg_printk(TPACPI_DBG_INIT,
853 "registering %s as an ACPI driver\n", ibm->name);
857 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
858 if (!ibm->acpi->driver) {
859 pr_err("failed to allocate memory for ibm->acpi->driver\n");
863 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
864 ibm->acpi->driver->ids = ibm->acpi->hid;
866 ibm->acpi->driver->ops.add = &tpacpi_device_add;
868 rc = acpi_bus_register_driver(ibm->acpi->driver);
870 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
872 kfree(ibm->acpi->driver);
873 ibm->acpi->driver = NULL;
875 ibm->flags.acpi_driver_registered = 1;
881 /****************************************************************************
882 ****************************************************************************
886 ****************************************************************************
887 ****************************************************************************/
889 static int dispatch_proc_show(struct seq_file *m, void *v)
891 struct ibm_struct *ibm = m->private;
893 if (!ibm || !ibm->read)
898 static int dispatch_proc_open(struct inode *inode, struct file *file)
900 return single_open(file, dispatch_proc_show, pde_data(inode));
903 static ssize_t dispatch_proc_write(struct file *file,
904 const char __user *userbuf,
905 size_t count, loff_t *pos)
907 struct ibm_struct *ibm = pde_data(file_inode(file));
911 if (!ibm || !ibm->write)
913 if (count > PAGE_SIZE - 1)
916 kernbuf = kmalloc(count + 1, GFP_KERNEL);
920 if (copy_from_user(kernbuf, userbuf, count)) {
926 ret = ibm->write(kernbuf);
935 static const struct proc_ops dispatch_proc_ops = {
936 .proc_open = dispatch_proc_open,
937 .proc_read = seq_read,
938 .proc_lseek = seq_lseek,
939 .proc_release = single_release,
940 .proc_write = dispatch_proc_write,
943 /****************************************************************************
944 ****************************************************************************
946 * Device model: input, hwmon and platform
948 ****************************************************************************
949 ****************************************************************************/
951 static struct platform_device *tpacpi_pdev;
952 static struct platform_device *tpacpi_sensors_pdev;
953 static struct device *tpacpi_hwmon;
954 static struct input_dev *tpacpi_inputdev;
955 static struct mutex tpacpi_inputdev_send_mutex;
956 static LIST_HEAD(tpacpi_all_drivers);
958 #ifdef CONFIG_PM_SLEEP
959 static int tpacpi_suspend_handler(struct device *dev)
961 struct ibm_struct *ibm, *itmp;
963 list_for_each_entry_safe(ibm, itmp,
973 static int tpacpi_resume_handler(struct device *dev)
975 struct ibm_struct *ibm, *itmp;
977 list_for_each_entry_safe(ibm, itmp,
988 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
989 tpacpi_suspend_handler, tpacpi_resume_handler);
991 static void tpacpi_shutdown_handler(struct platform_device *pdev)
993 struct ibm_struct *ibm, *itmp;
995 list_for_each_entry_safe(ibm, itmp,
1003 /*************************************************************************
1004 * sysfs support helpers
1007 static int parse_strtoul(const char *buf,
1008 unsigned long max, unsigned long *value)
1012 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1013 endp = skip_spaces(endp);
1014 if (*endp || *value > max)
1020 static void tpacpi_disable_brightness_delay(void)
1022 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1023 pr_notice("ACPI backlight control delay disabled\n");
1026 static void printk_deprecated_attribute(const char * const what,
1027 const char * const details)
1029 tpacpi_log_usertask("deprecated sysfs attribute");
1030 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1034 /*************************************************************************
1035 * rfkill and radio control support helpers
1039 * ThinkPad-ACPI firmware handling model:
1041 * WLSW (master wireless switch) is event-driven, and is common to all
1042 * firmware-controlled radios. It cannot be controlled, just monitored,
1043 * as expected. It overrides all radio state in firmware
1045 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1046 * (TODO: verify how WLSW interacts with the returned radio state).
1048 * The only time there are shadow radio state changes, is when
1049 * masked-off hotkeys are used.
1053 * Internal driver API for radio state:
1055 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1056 * bool: true means radio blocked (off)
1058 enum tpacpi_rfkill_state {
1059 TPACPI_RFK_RADIO_OFF = 0,
1063 /* rfkill switches */
1064 enum tpacpi_rfk_id {
1065 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1066 TPACPI_RFK_WWAN_SW_ID,
1067 TPACPI_RFK_UWB_SW_ID,
1071 static const char *tpacpi_rfkill_names[] = {
1072 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1073 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1074 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1075 [TPACPI_RFK_SW_MAX] = NULL
1078 /* ThinkPad-ACPI rfkill subdriver */
1080 struct rfkill *rfkill;
1081 enum tpacpi_rfk_id id;
1082 const struct tpacpi_rfk_ops *ops;
1085 struct tpacpi_rfk_ops {
1086 /* firmware interface */
1087 int (*get_status)(void);
1088 int (*set_status)(const enum tpacpi_rfkill_state);
1091 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1093 /* Query FW and update rfkill sw state for a given rfkill switch */
1094 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1101 status = (tp_rfk->ops->get_status)();
1105 rfkill_set_sw_state(tp_rfk->rfkill,
1106 (status == TPACPI_RFK_RADIO_OFF));
1112 * Sync the HW-blocking state of all rfkill switches,
1113 * do notice it causes the rfkill core to schedule uevents
1115 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1118 struct tpacpi_rfk *tp_rfk;
1120 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1121 tp_rfk = tpacpi_rfkill_switches[i];
1123 if (rfkill_set_hw_state(tp_rfk->rfkill,
1125 /* ignore -- we track sw block */
1131 /* Call to get the WLSW state from the firmware */
1132 static int hotkey_get_wlsw(void);
1134 /* Call to query WLSW state and update all rfkill switches */
1135 static bool tpacpi_rfk_check_hwblock_state(void)
1137 int res = hotkey_get_wlsw();
1140 /* When unknown or unsupported, we have to assume it is unblocked */
1144 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1145 tpacpi_rfk_update_hwblock_state(hw_blocked);
1150 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1152 struct tpacpi_rfk *tp_rfk = data;
1155 dbg_printk(TPACPI_DBG_RFKILL,
1156 "request to change radio state to %s\n",
1157 blocked ? "blocked" : "unblocked");
1159 /* try to set radio state */
1160 res = (tp_rfk->ops->set_status)(blocked ?
1161 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1163 /* and update the rfkill core with whatever the FW really did */
1164 tpacpi_rfk_update_swstate(tp_rfk);
1166 return (res < 0) ? res : 0;
1169 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1170 .set_block = tpacpi_rfk_hook_set_block,
1173 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1174 const struct tpacpi_rfk_ops *tp_rfkops,
1175 const enum rfkill_type rfktype,
1177 const bool set_default)
1179 struct tpacpi_rfk *atp_rfk;
1181 bool sw_state = false;
1185 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1187 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1189 atp_rfk->rfkill = rfkill_alloc(name,
1192 &tpacpi_rfk_rfkill_ops,
1194 if (!atp_rfk || !atp_rfk->rfkill) {
1195 pr_err("failed to allocate memory for rfkill class\n");
1201 atp_rfk->ops = tp_rfkops;
1203 sw_status = (tp_rfkops->get_status)();
1204 if (sw_status < 0) {
1205 pr_err("failed to read initial state for %s, error %d\n",
1208 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1210 /* try to keep the initial state, since we ask the
1211 * firmware to preserve it across S5 in NVRAM */
1212 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1215 hw_state = tpacpi_rfk_check_hwblock_state();
1216 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1218 res = rfkill_register(atp_rfk->rfkill);
1220 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1221 rfkill_destroy(atp_rfk->rfkill);
1226 tpacpi_rfkill_switches[id] = atp_rfk;
1228 pr_info("rfkill switch %s: radio is %sblocked\n",
1229 name, (sw_state || hw_state) ? "" : "un");
1233 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1235 struct tpacpi_rfk *tp_rfk;
1237 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1239 tp_rfk = tpacpi_rfkill_switches[id];
1241 rfkill_unregister(tp_rfk->rfkill);
1242 rfkill_destroy(tp_rfk->rfkill);
1243 tpacpi_rfkill_switches[id] = NULL;
1248 static void printk_deprecated_rfkill_attribute(const char * const what)
1250 printk_deprecated_attribute(what,
1251 "Please switch to generic rfkill before year 2010");
1254 /* sysfs <radio> enable ------------------------------------------------ */
1255 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1256 struct device_attribute *attr,
1261 printk_deprecated_rfkill_attribute(attr->attr.name);
1263 /* This is in the ABI... */
1264 if (tpacpi_rfk_check_hwblock_state()) {
1265 status = TPACPI_RFK_RADIO_OFF;
1267 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1272 return sysfs_emit(buf, "%d\n",
1273 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1276 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1277 struct device_attribute *attr,
1278 const char *buf, size_t count)
1283 printk_deprecated_rfkill_attribute(attr->attr.name);
1285 if (parse_strtoul(buf, 1, &t))
1288 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1290 /* This is in the ABI... */
1291 if (tpacpi_rfk_check_hwblock_state() && !!t)
1294 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1295 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1296 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1298 return (res < 0) ? res : count;
1301 /* procfs -------------------------------------------------------------- */
1302 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1304 if (id >= TPACPI_RFK_SW_MAX)
1305 seq_printf(m, "status:\t\tnot supported\n");
1309 /* This is in the ABI... */
1310 if (tpacpi_rfk_check_hwblock_state()) {
1311 status = TPACPI_RFK_RADIO_OFF;
1313 status = tpacpi_rfk_update_swstate(
1314 tpacpi_rfkill_switches[id]);
1319 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
1320 seq_printf(m, "commands:\tenable, disable\n");
1326 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1332 if (id >= TPACPI_RFK_SW_MAX)
1335 while ((cmd = strsep(&buf, ","))) {
1336 if (strlencmp(cmd, "enable") == 0)
1337 status = TPACPI_RFK_RADIO_ON;
1338 else if (strlencmp(cmd, "disable") == 0)
1339 status = TPACPI_RFK_RADIO_OFF;
1345 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1346 str_enable_disable(status == TPACPI_RFK_RADIO_ON),
1347 tpacpi_rfkill_names[id]);
1348 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1349 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1355 /*************************************************************************
1356 * thinkpad-acpi driver attributes
1359 /* interface_version --------------------------------------------------- */
1360 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1362 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1364 static DRIVER_ATTR_RO(interface_version);
1366 /* debug_level --------------------------------------------------------- */
1367 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1369 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1372 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1377 if (parse_strtoul(buf, 0xffff, &t))
1384 static DRIVER_ATTR_RW(debug_level);
1386 /* version ------------------------------------------------------------- */
1387 static ssize_t version_show(struct device_driver *drv, char *buf)
1389 return sysfs_emit(buf, "%s v%s\n",
1390 TPACPI_DESC, TPACPI_VERSION);
1392 static DRIVER_ATTR_RO(version);
1394 /* --------------------------------------------------------------------- */
1396 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1398 /* wlsw_emulstate ------------------------------------------------------ */
1399 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1401 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1404 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1409 if (parse_strtoul(buf, 1, &t))
1412 if (tpacpi_wlsw_emulstate != !!t) {
1413 tpacpi_wlsw_emulstate = !!t;
1414 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1419 static DRIVER_ATTR_RW(wlsw_emulstate);
1421 /* bluetooth_emulstate ------------------------------------------------- */
1422 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1424 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1427 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1428 const char *buf, size_t count)
1432 if (parse_strtoul(buf, 1, &t))
1435 tpacpi_bluetooth_emulstate = !!t;
1439 static DRIVER_ATTR_RW(bluetooth_emulstate);
1441 /* wwan_emulstate ------------------------------------------------- */
1442 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1444 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1447 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1452 if (parse_strtoul(buf, 1, &t))
1455 tpacpi_wwan_emulstate = !!t;
1459 static DRIVER_ATTR_RW(wwan_emulstate);
1461 /* uwb_emulstate ------------------------------------------------- */
1462 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1464 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1467 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1472 if (parse_strtoul(buf, 1, &t))
1475 tpacpi_uwb_emulstate = !!t;
1479 static DRIVER_ATTR_RW(uwb_emulstate);
1482 /*************************************************************************
1487 * Table of recommended minimum BIOS versions
1489 * Reasons for listing:
1490 * 1. Stable BIOS, listed because the unknown amount of
1491 * bugs and bad ACPI behaviour on older versions
1493 * 2. BIOS or EC fw with known bugs that trigger on Linux
1495 * 3. BIOS with known reduced functionality in older versions
1497 * We recommend the latest BIOS and EC version.
1498 * We only support the latest BIOS and EC fw version as a rule.
1500 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1501 * Information from users in ThinkWiki
1503 * WARNING: we use this table also to detect that the machine is
1504 * a ThinkPad in some cases, so don't remove entries lightly.
1507 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1508 { .vendor = (__v), \
1509 .bios = TPID(__id1, __id2), \
1510 .ec = TPACPI_MATCH_ANY, \
1511 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1512 | TPVER(__bv1, __bv2) }
1514 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1515 __eid, __ev1, __ev2) \
1516 { .vendor = (__v), \
1517 .bios = TPID(__bid1, __bid2), \
1519 .quirks = TPVER(__ev1, __ev2) << 16 \
1520 | TPVER(__bv1, __bv2) }
1522 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1523 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1525 /* Outdated IBM BIOSes often lack the EC id string */
1526 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1527 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1528 __bv1, __bv2, TPID(__id1, __id2), \
1530 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1531 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1534 /* Outdated IBM BIOSes often lack the EC id string */
1535 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1536 __eid1, __eid2, __ev1, __ev2) \
1537 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1538 __bv1, __bv2, TPID(__eid1, __eid2), \
1540 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1541 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1544 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1545 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1547 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1548 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1549 __bv1, __bv2, TPID(__id1, __id2), \
1552 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1553 __eid1, __eid2, __ev1, __ev2) \
1554 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1555 __bv1, __bv2, TPID(__eid1, __eid2), \
1558 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1559 /* Numeric models ------------------ */
1560 /* FW MODEL BIOS VERS */
1561 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1562 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1563 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1564 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1565 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1566 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1567 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1568 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1569 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1571 /* A-series ------------------------- */
1572 /* FW MODEL BIOS VERS EC VERS */
1573 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1574 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1575 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1576 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1577 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1578 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1579 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1580 TPV_QI0('1', '3', '2', '0'), /* A22m */
1581 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1582 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1583 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1585 /* G-series ------------------------- */
1586 /* FW MODEL BIOS VERS */
1587 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1588 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1590 /* R-series, T-series --------------- */
1591 /* FW MODEL BIOS VERS EC VERS */
1592 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1593 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1594 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1595 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1596 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1597 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1598 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1599 T40/p, T41/p, T42/p (1) */
1600 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1601 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1602 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1603 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1605 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1606 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1607 TPV_QI0('1', '6', '3', '2'), /* T22 */
1608 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1609 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1610 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1612 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1613 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1614 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1616 /* BIOS FW BIOS VERS EC FW EC VERS */
1617 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1618 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1620 /* X-series ------------------------- */
1621 /* FW MODEL BIOS VERS EC VERS */
1622 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1623 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1624 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1625 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1626 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1627 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1628 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1630 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1631 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1633 /* (0) - older versions lack DMI EC fw string and functionality */
1634 /* (1) - older versions known to lack functionality */
1645 static void __init tpacpi_check_outdated_fw(void)
1647 unsigned long fwvers;
1648 u16 ec_version, bios_version;
1650 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1651 ARRAY_SIZE(tpacpi_bios_version_qtable));
1656 bios_version = fwvers & 0xffffU;
1657 ec_version = (fwvers >> 16) & 0xffffU;
1659 /* note that unknown versions are set to 0x0000 and we use that */
1660 if ((bios_version > thinkpad_id.bios_release) ||
1661 (ec_version > thinkpad_id.ec_release &&
1662 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1664 * The changelogs would let us track down the exact
1665 * reason, but it is just too much of a pain to track
1666 * it. We only list BIOSes that are either really
1667 * broken, or really stable to begin with, so it is
1668 * best if the user upgrades the firmware anyway.
1670 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1671 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1675 static bool __init tpacpi_is_fw_known(void)
1677 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1678 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1681 /****************************************************************************
1682 ****************************************************************************
1686 ****************************************************************************
1687 ****************************************************************************/
1689 /*************************************************************************
1690 * thinkpad-acpi metadata subdriver
1693 static int thinkpad_acpi_driver_read(struct seq_file *m)
1695 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1696 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1700 static struct ibm_struct thinkpad_acpi_driver_data = {
1702 .read = thinkpad_acpi_driver_read,
1705 /*************************************************************************
1710 * ThinkPad firmware event model
1712 * The ThinkPad firmware has two main event interfaces: normal ACPI
1713 * notifications (which follow the ACPI standard), and a private event
1716 * The private event interface also issues events for the hotkeys. As
1717 * the driver gained features, the event handling code ended up being
1718 * built around the hotkey subdriver. This will need to be refactored
1719 * to a more formal event API eventually.
1721 * Some "hotkeys" are actually supposed to be used as event reports,
1722 * such as "brightness has changed", "volume has changed", depending on
1723 * the ThinkPad model and how the firmware is operating.
1725 * Unlike other classes, hotkey-class events have mask/unmask control on
1726 * non-ancient firmware. However, how it behaves changes a lot with the
1727 * firmware model and version.
1730 enum { /* hot key scan codes (derived from ACPI DSDT) */
1731 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1732 TP_ACPI_HOTKEYSCAN_FNF2,
1733 TP_ACPI_HOTKEYSCAN_FNF3,
1734 TP_ACPI_HOTKEYSCAN_FNF4,
1735 TP_ACPI_HOTKEYSCAN_FNF5,
1736 TP_ACPI_HOTKEYSCAN_FNF6,
1737 TP_ACPI_HOTKEYSCAN_FNF7,
1738 TP_ACPI_HOTKEYSCAN_FNF8,
1739 TP_ACPI_HOTKEYSCAN_FNF9,
1740 TP_ACPI_HOTKEYSCAN_FNF10,
1741 TP_ACPI_HOTKEYSCAN_FNF11,
1742 TP_ACPI_HOTKEYSCAN_FNF12,
1743 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1744 TP_ACPI_HOTKEYSCAN_FNINSERT,
1745 TP_ACPI_HOTKEYSCAN_FNDELETE,
1746 TP_ACPI_HOTKEYSCAN_FNHOME,
1747 TP_ACPI_HOTKEYSCAN_FNEND,
1748 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1749 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1750 TP_ACPI_HOTKEYSCAN_FNSPACE,
1751 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1752 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1753 TP_ACPI_HOTKEYSCAN_MUTE,
1754 TP_ACPI_HOTKEYSCAN_THINKPAD,
1755 TP_ACPI_HOTKEYSCAN_UNK1,
1756 TP_ACPI_HOTKEYSCAN_UNK2,
1757 TP_ACPI_HOTKEYSCAN_UNK3,
1758 TP_ACPI_HOTKEYSCAN_UNK4,
1759 TP_ACPI_HOTKEYSCAN_UNK5,
1760 TP_ACPI_HOTKEYSCAN_UNK6,
1761 TP_ACPI_HOTKEYSCAN_UNK7,
1762 TP_ACPI_HOTKEYSCAN_UNK8,
1764 /* Adaptive keyboard keycodes */
1765 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1766 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1767 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1768 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1769 TP_ACPI_HOTKEYSCAN_CLOUD,
1770 TP_ACPI_HOTKEYSCAN_UNK9,
1771 TP_ACPI_HOTKEYSCAN_VOICE,
1772 TP_ACPI_HOTKEYSCAN_UNK10,
1773 TP_ACPI_HOTKEYSCAN_GESTURES,
1774 TP_ACPI_HOTKEYSCAN_UNK11,
1775 TP_ACPI_HOTKEYSCAN_UNK12,
1776 TP_ACPI_HOTKEYSCAN_UNK13,
1777 TP_ACPI_HOTKEYSCAN_CONFIG,
1778 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1779 TP_ACPI_HOTKEYSCAN_RELOAD,
1780 TP_ACPI_HOTKEYSCAN_BACK,
1781 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1782 TP_ACPI_HOTKEYSCAN_MIC_UP,
1783 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1784 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1785 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1787 /* Lenovo extended keymap, starting at 0x1300 */
1788 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1789 /* first new observed key (star, favorites) is 0x1311 */
1790 TP_ACPI_HOTKEYSCAN_STAR = 69,
1791 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1792 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1793 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1794 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1795 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1796 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1797 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1798 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1800 /* Hotkey keymap size */
1801 TPACPI_HOTKEY_MAP_LEN
1804 enum { /* Keys/events available through NVRAM polling */
1805 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1806 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1809 enum { /* Positions of some of the keys in hotkey masks */
1810 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1811 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1812 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1813 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1814 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1815 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1816 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1817 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1818 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1819 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1820 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1823 enum { /* NVRAM to ACPI HKEY group map */
1824 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1825 TP_ACPI_HKEY_ZOOM_MASK |
1826 TP_ACPI_HKEY_DISPSWTCH_MASK |
1827 TP_ACPI_HKEY_HIBERNATE_MASK,
1828 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1829 TP_ACPI_HKEY_BRGHTDWN_MASK,
1830 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1831 TP_ACPI_HKEY_VOLDWN_MASK |
1832 TP_ACPI_HKEY_MUTE_MASK,
1835 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1836 struct tp_nvram_state {
1837 u16 thinkpad_toggle:1;
1839 u16 display_toggle:1;
1840 u16 thinklight_toggle:1;
1841 u16 hibernate_toggle:1;
1842 u16 displayexp_toggle:1;
1843 u16 display_state:1;
1844 u16 brightness_toggle:1;
1845 u16 volume_toggle:1;
1848 u8 brightness_level;
1852 /* kthread for the hotkey poller */
1853 static struct task_struct *tpacpi_hotkey_task;
1856 * Acquire mutex to write poller control variables as an
1859 * Increment hotkey_config_change when changing them if you
1860 * want the kthread to forget old state.
1862 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1864 static struct mutex hotkey_thread_data_mutex;
1865 static unsigned int hotkey_config_change;
1868 * hotkey poller control variables
1870 * Must be atomic or readers will also need to acquire mutex
1872 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1873 * should be used only when the changes need to be taken as
1874 * a block, OR when one needs to force the kthread to forget
1877 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1878 static unsigned int hotkey_poll_freq = 10; /* Hz */
1880 #define HOTKEY_CONFIG_CRITICAL_START \
1882 mutex_lock(&hotkey_thread_data_mutex); \
1883 hotkey_config_change++; \
1885 #define HOTKEY_CONFIG_CRITICAL_END \
1886 mutex_unlock(&hotkey_thread_data_mutex);
1888 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1890 #define hotkey_source_mask 0U
1891 #define HOTKEY_CONFIG_CRITICAL_START
1892 #define HOTKEY_CONFIG_CRITICAL_END
1894 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1896 static struct mutex hotkey_mutex;
1898 static enum { /* Reasons for waking up */
1899 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1900 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1901 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1902 } hotkey_wakeup_reason;
1904 static int hotkey_autosleep_ack;
1906 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1907 static u32 hotkey_all_mask; /* all events supported in fw */
1908 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1909 static u32 hotkey_reserved_mask; /* events better left disabled */
1910 static u32 hotkey_driver_mask; /* events needed by the driver */
1911 static u32 hotkey_user_mask; /* events visible to userspace */
1912 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1914 static u16 *hotkey_keycode_map;
1916 static void tpacpi_driver_event(const unsigned int hkey_event);
1917 static void hotkey_driver_event(const unsigned int scancode);
1918 static void hotkey_poll_setup(const bool may_warn);
1920 /* HKEY.MHKG() return bits */
1921 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1923 TP_ACPI_MULTI_MODE_INVALID = 0,
1924 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1925 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1926 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1927 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1928 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1929 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1930 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1934 /* The following modes are considered tablet mode for the purpose of
1935 * reporting the status to userspace. i.e. in all these modes it makes
1936 * sense to disable the laptop input devices such as touchpad and
1939 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1940 TP_ACPI_MULTI_MODE_STAND |
1941 TP_ACPI_MULTI_MODE_TENT |
1942 TP_ACPI_MULTI_MODE_STAND_TENT,
1945 static int hotkey_get_wlsw(void)
1949 if (!tp_features.hotkey_wlsw)
1952 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1954 return (tpacpi_wlsw_emulstate) ?
1955 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1958 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1961 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1964 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1966 int type = (s >> 16) & 0xffff;
1967 int value = s & 0xffff;
1968 int mode = TP_ACPI_MULTI_MODE_INVALID;
1969 int valid_modes = 0;
1971 if (has_tablet_mode)
1972 *has_tablet_mode = 0;
1976 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1977 TP_ACPI_MULTI_MODE_TABLET |
1978 TP_ACPI_MULTI_MODE_STAND_TENT;
1981 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1982 TP_ACPI_MULTI_MODE_FLAT |
1983 TP_ACPI_MULTI_MODE_TABLET |
1984 TP_ACPI_MULTI_MODE_STAND |
1985 TP_ACPI_MULTI_MODE_TENT;
1988 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1989 TP_ACPI_MULTI_MODE_FLAT;
1993 /* In mode 4, FLAT is not specified as a valid mode. However,
1994 * it can be seen at least on the X1 Yoga 2nd Generation.
1996 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1997 TP_ACPI_MULTI_MODE_FLAT |
1998 TP_ACPI_MULTI_MODE_TABLET |
1999 TP_ACPI_MULTI_MODE_STAND |
2000 TP_ACPI_MULTI_MODE_TENT;
2003 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2004 type, value, TPACPI_MAIL);
2008 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2009 *has_tablet_mode = 1;
2013 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2016 mode = TP_ACPI_MULTI_MODE_FLAT;
2019 mode = TP_ACPI_MULTI_MODE_TABLET;
2023 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2025 mode = TP_ACPI_MULTI_MODE_STAND;
2028 mode = TP_ACPI_MULTI_MODE_TENT;
2031 if (type == 5 && value == 0xffff) {
2032 pr_warn("Multi mode status is undetected, assuming laptop\n");
2037 if (!(mode & valid_modes)) {
2038 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2039 value, type, TPACPI_MAIL);
2043 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2046 static int hotkey_get_tablet_mode(int *status)
2050 switch (tp_features.hotkey_tablet) {
2051 case TP_HOTKEY_TABLET_USES_MHKG:
2052 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2055 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2057 case TP_HOTKEY_TABLET_USES_GMMS:
2058 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2061 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2071 * Reads current event mask from firmware, and updates
2072 * hotkey_acpi_mask accordingly. Also resets any bits
2073 * from hotkey_user_mask that are unavailable to be
2074 * delivered (shadow requirement of the userspace ABI).
2076 * Call with hotkey_mutex held
2078 static int hotkey_mask_get(void)
2080 if (tp_features.hotkey_mask) {
2083 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2086 hotkey_acpi_mask = m;
2088 /* no mask support doesn't mean no event support... */
2089 hotkey_acpi_mask = hotkey_all_mask;
2092 /* sync userspace-visible mask */
2093 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2098 static void hotkey_mask_warn_incomplete_mask(void)
2100 /* log only what the user can fix... */
2101 const u32 wantedmask = hotkey_driver_mask &
2102 ~(hotkey_acpi_mask | hotkey_source_mask) &
2103 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2106 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2110 * Set the firmware mask when supported
2112 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2114 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2116 * Call with hotkey_mutex held
2118 static int hotkey_mask_set(u32 mask)
2123 const u32 fwmask = mask & ~hotkey_source_mask;
2125 if (tp_features.hotkey_mask) {
2126 for (i = 0; i < 32; i++) {
2127 if (!acpi_evalf(hkey_handle,
2128 NULL, "MHKM", "vdd", i + 1,
2129 !!(mask & (1 << i)))) {
2137 * We *must* make an inconditional call to hotkey_mask_get to
2138 * refresh hotkey_acpi_mask and update hotkey_user_mask
2140 * Take the opportunity to also log when we cannot _enable_
2143 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2144 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2145 fwmask, hotkey_acpi_mask);
2148 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2149 hotkey_mask_warn_incomplete_mask();
2155 * Sets hotkey_user_mask and tries to set the firmware mask
2157 * Call with hotkey_mutex held
2159 static int hotkey_user_mask_set(const u32 mask)
2163 /* Give people a chance to notice they are doing something that
2164 * is bound to go boom on their users sooner or later */
2165 if (!tp_warned.hotkey_mask_ff &&
2166 (mask == 0xffff || mask == 0xffffff ||
2167 mask == 0xffffffff)) {
2168 tp_warned.hotkey_mask_ff = 1;
2169 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2171 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2174 /* Try to enable what the user asked for, plus whatever we need.
2175 * this syncs everything but won't enable bits in hotkey_user_mask */
2176 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2178 /* Enable the available bits in hotkey_user_mask */
2179 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2185 * Sets the driver hotkey mask.
2187 * Can be called even if the hotkey subdriver is inactive
2189 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2193 /* Do the right thing if hotkey_init has not been called yet */
2194 if (!tp_features.hotkey) {
2195 hotkey_driver_mask = mask;
2199 mutex_lock(&hotkey_mutex);
2201 HOTKEY_CONFIG_CRITICAL_START
2202 hotkey_driver_mask = mask;
2203 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2204 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2206 HOTKEY_CONFIG_CRITICAL_END
2208 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2209 ~hotkey_source_mask);
2210 hotkey_poll_setup(true);
2212 mutex_unlock(&hotkey_mutex);
2217 static int hotkey_status_get(int *status)
2219 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2225 static int hotkey_status_set(bool enable)
2227 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2233 static void tpacpi_input_send_tabletsw(void)
2237 if (tp_features.hotkey_tablet &&
2238 !hotkey_get_tablet_mode(&state)) {
2239 mutex_lock(&tpacpi_inputdev_send_mutex);
2241 input_report_switch(tpacpi_inputdev,
2242 SW_TABLET_MODE, !!state);
2243 input_sync(tpacpi_inputdev);
2245 mutex_unlock(&tpacpi_inputdev_send_mutex);
2249 /* Do NOT call without validating scancode first */
2250 static void tpacpi_input_send_key(const unsigned int scancode)
2252 const unsigned int keycode = hotkey_keycode_map[scancode];
2254 if (keycode != KEY_RESERVED) {
2255 mutex_lock(&tpacpi_inputdev_send_mutex);
2257 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2258 input_report_key(tpacpi_inputdev, keycode, 1);
2259 input_sync(tpacpi_inputdev);
2261 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2262 input_report_key(tpacpi_inputdev, keycode, 0);
2263 input_sync(tpacpi_inputdev);
2265 mutex_unlock(&tpacpi_inputdev_send_mutex);
2269 /* Do NOT call without validating scancode first */
2270 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2272 hotkey_driver_event(scancode);
2273 if (hotkey_user_mask & (1 << scancode))
2274 tpacpi_input_send_key(scancode);
2277 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2278 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2280 /* Do NOT call without validating scancode first */
2281 static void tpacpi_hotkey_send_key(unsigned int scancode)
2283 tpacpi_input_send_key_masked(scancode);
2286 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2290 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2291 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2292 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2293 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2294 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2295 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2297 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2298 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2299 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2301 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2302 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2303 n->displayexp_toggle =
2304 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2306 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2308 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2309 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2310 n->brightness_toggle =
2311 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2313 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2315 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2316 >> TP_NVRAM_POS_LEVEL_VOLUME;
2317 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2318 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2322 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2324 if ((event_mask & (1 << __scancode)) && \
2325 oldn->__member != newn->__member) \
2326 tpacpi_hotkey_send_key(__scancode); \
2329 #define TPACPI_MAY_SEND_KEY(__scancode) \
2331 if (event_mask & (1 << __scancode)) \
2332 tpacpi_hotkey_send_key(__scancode); \
2335 static void issue_volchange(const unsigned int oldvol,
2336 const unsigned int newvol,
2337 const u32 event_mask)
2339 unsigned int i = oldvol;
2341 while (i > newvol) {
2342 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2345 while (i < newvol) {
2346 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2351 static void issue_brightnesschange(const unsigned int oldbrt,
2352 const unsigned int newbrt,
2353 const u32 event_mask)
2355 unsigned int i = oldbrt;
2357 while (i > newbrt) {
2358 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2361 while (i < newbrt) {
2362 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2367 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2368 struct tp_nvram_state *newn,
2369 const u32 event_mask)
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2384 * This code is supposed to duplicate the IBM firmware behaviour:
2385 * - Pressing MUTE issues mute hotkey message, even when already mute
2386 * - Pressing Volume up/down issues volume up/down hotkey messages,
2387 * even when already at maximum or minimum volume
2388 * - The act of unmuting issues volume up/down notification,
2389 * depending which key was used to unmute
2391 * We are constrained to what the NVRAM can tell us, which is not much
2392 * and certainly not enough if more than one volume hotkey was pressed
2393 * since the last poll cycle.
2395 * Just to make our life interesting, some newer Lenovo ThinkPads have
2396 * bugs in the BIOS and may fail to update volume_toggle properly.
2401 oldn->volume_toggle != newn->volume_toggle ||
2402 oldn->volume_level != newn->volume_level) {
2403 /* recently muted, or repeated mute keypress, or
2404 * multiple presses ending in mute */
2405 issue_volchange(oldn->volume_level, newn->volume_level,
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2412 /* recently unmuted, issue 'unmute' keypress */
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2415 if (oldn->volume_level != newn->volume_level) {
2416 issue_volchange(oldn->volume_level, newn->volume_level,
2418 } else if (oldn->volume_toggle != newn->volume_toggle) {
2419 /* repeated vol up/down keypress at end of scale ? */
2420 if (newn->volume_level == 0)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2422 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2427 /* handle brightness */
2428 if (oldn->brightness_level != newn->brightness_level) {
2429 issue_brightnesschange(oldn->brightness_level,
2430 newn->brightness_level, event_mask);
2431 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2432 /* repeated key presses that didn't change state */
2433 if (newn->brightness_level == 0)
2434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2435 else if (newn->brightness_level >= bright_maxlvl
2436 && !tp_features.bright_unkfw)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2440 #undef TPACPI_COMPARE_KEY
2441 #undef TPACPI_MAY_SEND_KEY
2447 * We track all events in hotkey_source_mask all the time, since
2448 * most of them are edge-based. We only issue those requested by
2449 * hotkey_user_mask or hotkey_driver_mask, though.
2451 static int hotkey_kthread(void *data)
2453 struct tp_nvram_state s[2] = { 0 };
2454 u32 poll_mask, event_mask;
2455 unsigned int si, so;
2457 unsigned int change_detector;
2458 unsigned int poll_freq;
2461 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2470 /* Initial state for compares */
2471 mutex_lock(&hotkey_thread_data_mutex);
2472 change_detector = hotkey_config_change;
2473 poll_mask = hotkey_source_mask;
2474 event_mask = hotkey_source_mask &
2475 (hotkey_driver_mask | hotkey_user_mask);
2476 poll_freq = hotkey_poll_freq;
2477 mutex_unlock(&hotkey_thread_data_mutex);
2478 hotkey_read_nvram(&s[so], poll_mask);
2480 while (!kthread_should_stop()) {
2482 if (likely(poll_freq))
2485 t = 100; /* should never happen... */
2487 t = msleep_interruptible(t);
2488 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2491 if (t > 0 && !was_frozen)
2494 mutex_lock(&hotkey_thread_data_mutex);
2495 if (was_frozen || hotkey_config_change != change_detector) {
2496 /* forget old state on thaw or config change */
2499 change_detector = hotkey_config_change;
2501 poll_mask = hotkey_source_mask;
2502 event_mask = hotkey_source_mask &
2503 (hotkey_driver_mask | hotkey_user_mask);
2504 poll_freq = hotkey_poll_freq;
2505 mutex_unlock(&hotkey_thread_data_mutex);
2507 if (likely(poll_mask)) {
2508 hotkey_read_nvram(&s[si], poll_mask);
2509 if (likely(si != so)) {
2510 hotkey_compare_and_issue_event(&s[so], &s[si],
2523 /* call with hotkey_mutex held */
2524 static void hotkey_poll_stop_sync(void)
2526 if (tpacpi_hotkey_task) {
2527 kthread_stop(tpacpi_hotkey_task);
2528 tpacpi_hotkey_task = NULL;
2532 /* call with hotkey_mutex held */
2533 static void hotkey_poll_setup(const bool may_warn)
2535 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2536 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2538 if (hotkey_poll_freq > 0 &&
2539 (poll_driver_mask ||
2540 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2541 if (!tpacpi_hotkey_task) {
2542 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2543 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2544 if (IS_ERR(tpacpi_hotkey_task)) {
2545 tpacpi_hotkey_task = NULL;
2546 pr_err("could not create kernel thread for hotkey polling\n");
2550 hotkey_poll_stop_sync();
2551 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2552 hotkey_poll_freq == 0) {
2553 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2554 poll_user_mask, poll_driver_mask);
2559 static void hotkey_poll_setup_safe(const bool may_warn)
2561 mutex_lock(&hotkey_mutex);
2562 hotkey_poll_setup(may_warn);
2563 mutex_unlock(&hotkey_mutex);
2566 /* call with hotkey_mutex held */
2567 static void hotkey_poll_set_freq(unsigned int freq)
2570 hotkey_poll_stop_sync();
2572 hotkey_poll_freq = freq;
2575 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2577 static void hotkey_poll_setup(const bool __unused)
2581 static void hotkey_poll_setup_safe(const bool __unused)
2585 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2587 static int hotkey_inputdev_open(struct input_dev *dev)
2589 switch (tpacpi_lifecycle) {
2590 case TPACPI_LIFE_INIT:
2591 case TPACPI_LIFE_RUNNING:
2592 hotkey_poll_setup_safe(false);
2594 case TPACPI_LIFE_EXITING:
2598 /* Should only happen if tpacpi_lifecycle is corrupt */
2603 static void hotkey_inputdev_close(struct input_dev *dev)
2605 /* disable hotkey polling when possible */
2606 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2607 !(hotkey_source_mask & hotkey_driver_mask))
2608 hotkey_poll_setup_safe(false);
2611 /* sysfs hotkey enable ------------------------------------------------- */
2612 static ssize_t hotkey_enable_show(struct device *dev,
2613 struct device_attribute *attr,
2618 printk_deprecated_attribute("hotkey_enable",
2619 "Hotkey reporting is always enabled");
2621 res = hotkey_status_get(&status);
2625 return sysfs_emit(buf, "%d\n", status);
2628 static ssize_t hotkey_enable_store(struct device *dev,
2629 struct device_attribute *attr,
2630 const char *buf, size_t count)
2634 printk_deprecated_attribute("hotkey_enable",
2635 "Hotkeys can be disabled through hotkey_mask");
2637 if (parse_strtoul(buf, 1, &t))
2646 static DEVICE_ATTR_RW(hotkey_enable);
2648 /* sysfs hotkey mask --------------------------------------------------- */
2649 static ssize_t hotkey_mask_show(struct device *dev,
2650 struct device_attribute *attr,
2653 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2656 static ssize_t hotkey_mask_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2663 if (parse_strtoul(buf, 0xffffffffUL, &t))
2666 if (mutex_lock_killable(&hotkey_mutex))
2667 return -ERESTARTSYS;
2669 res = hotkey_user_mask_set(t);
2671 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2672 hotkey_poll_setup(true);
2675 mutex_unlock(&hotkey_mutex);
2677 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2679 return (res) ? res : count;
2682 static DEVICE_ATTR_RW(hotkey_mask);
2684 /* sysfs hotkey bios_enabled ------------------------------------------- */
2685 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2686 struct device_attribute *attr,
2689 return sprintf(buf, "0\n");
2692 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2694 /* sysfs hotkey bios_mask ---------------------------------------------- */
2695 static ssize_t hotkey_bios_mask_show(struct device *dev,
2696 struct device_attribute *attr,
2699 printk_deprecated_attribute("hotkey_bios_mask",
2700 "This attribute is useless.");
2701 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2704 static DEVICE_ATTR_RO(hotkey_bios_mask);
2706 /* sysfs hotkey all_mask ----------------------------------------------- */
2707 static ssize_t hotkey_all_mask_show(struct device *dev,
2708 struct device_attribute *attr,
2711 return sysfs_emit(buf, "0x%08x\n",
2712 hotkey_all_mask | hotkey_source_mask);
2715 static DEVICE_ATTR_RO(hotkey_all_mask);
2717 /* sysfs hotkey all_mask ----------------------------------------------- */
2718 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2719 struct device_attribute *attr,
2722 return sysfs_emit(buf, "0x%08x\n",
2723 hotkey_adaptive_all_mask | hotkey_source_mask);
2726 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2728 /* sysfs hotkey recommended_mask --------------------------------------- */
2729 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2730 struct device_attribute *attr,
2733 return sysfs_emit(buf, "0x%08x\n",
2734 (hotkey_all_mask | hotkey_source_mask)
2735 & ~hotkey_reserved_mask);
2738 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2740 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2742 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2743 static ssize_t hotkey_source_mask_show(struct device *dev,
2744 struct device_attribute *attr,
2747 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2750 static ssize_t hotkey_source_mask_store(struct device *dev,
2751 struct device_attribute *attr,
2752 const char *buf, size_t count)
2758 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2759 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2762 if (mutex_lock_killable(&hotkey_mutex))
2763 return -ERESTARTSYS;
2765 HOTKEY_CONFIG_CRITICAL_START
2766 hotkey_source_mask = t;
2767 HOTKEY_CONFIG_CRITICAL_END
2769 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2770 ~hotkey_source_mask);
2771 hotkey_poll_setup(true);
2773 /* check if events needed by the driver got disabled */
2774 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2775 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2777 mutex_unlock(&hotkey_mutex);
2780 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2783 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2786 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2788 return (rc < 0) ? rc : count;
2791 static DEVICE_ATTR_RW(hotkey_source_mask);
2793 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2794 static ssize_t hotkey_poll_freq_show(struct device *dev,
2795 struct device_attribute *attr,
2798 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2801 static ssize_t hotkey_poll_freq_store(struct device *dev,
2802 struct device_attribute *attr,
2803 const char *buf, size_t count)
2807 if (parse_strtoul(buf, 25, &t))
2810 if (mutex_lock_killable(&hotkey_mutex))
2811 return -ERESTARTSYS;
2813 hotkey_poll_set_freq(t);
2814 hotkey_poll_setup(true);
2816 mutex_unlock(&hotkey_mutex);
2818 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2823 static DEVICE_ATTR_RW(hotkey_poll_freq);
2825 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2827 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2828 static ssize_t hotkey_radio_sw_show(struct device *dev,
2829 struct device_attribute *attr,
2833 res = hotkey_get_wlsw();
2837 /* Opportunistic update */
2838 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2840 return sysfs_emit(buf, "%d\n",
2841 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2844 static DEVICE_ATTR_RO(hotkey_radio_sw);
2846 static void hotkey_radio_sw_notify_change(void)
2848 if (tp_features.hotkey_wlsw)
2849 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2853 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2854 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2855 struct device_attribute *attr,
2859 res = hotkey_get_tablet_mode(&s);
2863 return sysfs_emit(buf, "%d\n", !!s);
2866 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2868 static void hotkey_tablet_mode_notify_change(void)
2870 if (tp_features.hotkey_tablet)
2871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2872 "hotkey_tablet_mode");
2875 /* sysfs wakeup reason (pollable) -------------------------------------- */
2876 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2877 struct device_attribute *attr,
2880 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2883 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2885 static void hotkey_wakeup_reason_notify_change(void)
2887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2891 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2892 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2893 struct device_attribute *attr,
2896 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2899 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2900 hotkey_wakeup_hotunplug_complete_show, NULL);
2902 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "wakeup_hotunplug_complete");
2908 /* sysfs adaptive kbd mode --------------------------------------------- */
2910 static int adaptive_keyboard_get_mode(void);
2911 static int adaptive_keyboard_set_mode(int new_mode);
2913 enum ADAPTIVE_KEY_MODE {
2916 WEB_CONFERENCE_MODE,
2921 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2922 struct device_attribute *attr,
2927 current_mode = adaptive_keyboard_get_mode();
2928 if (current_mode < 0)
2929 return current_mode;
2931 return sysfs_emit(buf, "%d\n", current_mode);
2934 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2935 struct device_attribute *attr,
2936 const char *buf, size_t count)
2941 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2944 res = adaptive_keyboard_set_mode(t);
2945 return (res < 0) ? res : count;
2948 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2950 static struct attribute *adaptive_kbd_attributes[] = {
2951 &dev_attr_adaptive_kbd_mode.attr,
2955 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2956 struct attribute *attr, int n)
2958 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2961 static const struct attribute_group adaptive_kbd_attr_group = {
2962 .is_visible = hadaptive_kbd_attr_is_visible,
2963 .attrs = adaptive_kbd_attributes,
2966 /* --------------------------------------------------------------------- */
2968 static struct attribute *hotkey_attributes[] = {
2969 &dev_attr_hotkey_enable.attr,
2970 &dev_attr_hotkey_bios_enabled.attr,
2971 &dev_attr_hotkey_bios_mask.attr,
2972 &dev_attr_wakeup_reason.attr,
2973 &dev_attr_wakeup_hotunplug_complete.attr,
2974 &dev_attr_hotkey_mask.attr,
2975 &dev_attr_hotkey_all_mask.attr,
2976 &dev_attr_hotkey_adaptive_all_mask.attr,
2977 &dev_attr_hotkey_recommended_mask.attr,
2978 &dev_attr_hotkey_tablet_mode.attr,
2979 &dev_attr_hotkey_radio_sw.attr,
2980 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2981 &dev_attr_hotkey_source_mask.attr,
2982 &dev_attr_hotkey_poll_freq.attr,
2987 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2988 struct attribute *attr, int n)
2990 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2991 if (!tp_features.hotkey_tablet)
2993 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2994 if (!tp_features.hotkey_wlsw)
3001 static const struct attribute_group hotkey_attr_group = {
3002 .is_visible = hotkey_attr_is_visible,
3003 .attrs = hotkey_attributes,
3007 * Sync both the hw and sw blocking state of all switches
3009 static void tpacpi_send_radiosw_update(void)
3014 * We must sync all rfkill controllers *before* issuing any
3015 * rfkill input events, or we will race the rfkill core input
3018 * tpacpi_inputdev_send_mutex works as a synchronization point
3021 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3024 wlsw = hotkey_get_wlsw();
3026 /* Sync hw blocking state first if it is hw-blocked */
3027 if (wlsw == TPACPI_RFK_RADIO_OFF)
3028 tpacpi_rfk_update_hwblock_state(true);
3030 /* Sync hw blocking state last if it is hw-unblocked */
3031 if (wlsw == TPACPI_RFK_RADIO_ON)
3032 tpacpi_rfk_update_hwblock_state(false);
3034 /* Issue rfkill input event for WLSW switch */
3036 mutex_lock(&tpacpi_inputdev_send_mutex);
3038 input_report_switch(tpacpi_inputdev,
3039 SW_RFKILL_ALL, (wlsw > 0));
3040 input_sync(tpacpi_inputdev);
3042 mutex_unlock(&tpacpi_inputdev_send_mutex);
3046 * this can be unconditional, as we will poll state again
3047 * if userspace uses the notify to read data
3049 hotkey_radio_sw_notify_change();
3052 static void hotkey_exit(void)
3054 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3055 mutex_lock(&hotkey_mutex);
3056 hotkey_poll_stop_sync();
3057 mutex_unlock(&hotkey_mutex);
3059 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3060 "restoring original HKEY status and mask\n");
3061 /* yes, there is a bitwise or below, we want the
3062 * functions to be called even if one of them fail */
3063 if (((tp_features.hotkey_mask &&
3064 hotkey_mask_set(hotkey_orig_mask)) |
3065 hotkey_status_set(false)) != 0)
3066 pr_err("failed to restore hot key mask to BIOS defaults\n");
3069 static void __init hotkey_unmap(const unsigned int scancode)
3071 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3072 clear_bit(hotkey_keycode_map[scancode],
3073 tpacpi_inputdev->keybit);
3074 hotkey_keycode_map[scancode] = KEY_RESERVED;
3080 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3083 #define TPACPI_HK_Q_INIMASK 0x0001
3085 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3086 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3087 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3088 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3089 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3090 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3091 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3092 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3093 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3094 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3095 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3096 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3097 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3098 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3099 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3100 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3101 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3102 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3103 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3104 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3107 typedef u16 tpacpi_keymap_entry_t;
3108 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3110 static int hotkey_init_tablet_mode(void)
3112 int in_tablet_mode = 0, res;
3115 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3116 int has_tablet_mode;
3118 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3121 * The Yoga 11e series has 2 accelerometers described by a
3122 * BOSC0200 ACPI node. This setup relies on a Windows service
3123 * which calls special ACPI methods on this node to report
3124 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3125 * does not support this, so skip the hotkey on these models.
3127 if (has_tablet_mode && !dual_accel_detect())
3128 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3130 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3131 /* For X41t, X60t, X61t Tablets... */
3132 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3133 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3137 if (!tp_features.hotkey_tablet)
3140 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3141 type, in_tablet_mode ? "tablet" : "laptop");
3143 return in_tablet_mode;
3146 static int __init hotkey_init(struct ibm_init_struct *iibm)
3148 /* Requirements for changing the default keymaps:
3150 * 1. Many of the keys are mapped to KEY_RESERVED for very
3151 * good reasons. Do not change them unless you have deep
3152 * knowledge on the IBM and Lenovo ThinkPad firmware for
3153 * the various ThinkPad models. The driver behaves
3154 * differently for KEY_RESERVED: such keys have their
3155 * hot key mask *unset* in mask_recommended, and also
3156 * in the initial hot key mask programmed into the
3157 * firmware at driver load time, which means the firm-
3158 * ware may react very differently if you change them to
3161 * 2. You must be subscribed to the linux-thinkpad and
3162 * ibm-acpi-devel mailing lists, and you should read the
3163 * list archives since 2007 if you want to change the
3164 * keymaps. This requirement exists so that you will
3165 * know the past history of problems with the thinkpad-
3166 * acpi driver keymaps, and also that you will be
3167 * listening to any bug reports;
3169 * 3. Do not send thinkpad-acpi specific patches directly to
3170 * for merging, *ever*. Send them to the linux-acpi
3171 * mailinglist for comments. Merging is to be done only
3172 * through acpi-test and the ACPI maintainer.
3174 * If the above is too much to ask, don't change the keymap.
3175 * Ask the thinkpad-acpi maintainer to do it, instead.
3179 TPACPI_KEYMAP_IBM_GENERIC = 0,
3180 TPACPI_KEYMAP_LENOVO_GENERIC,
3183 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3184 /* Generic keymap for IBM ThinkPads */
3185 [TPACPI_KEYMAP_IBM_GENERIC] = {
3186 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3187 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3188 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3189 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3191 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3192 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3193 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3194 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3196 /* brightness: firmware always reacts to them */
3197 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3198 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3200 /* Thinklight: firmware always react to it */
3201 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3203 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3204 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3206 /* Volume: firmware always react to it and reprograms
3207 * the built-in *extra* mixer. Never map it to control
3208 * another mixer by default. */
3209 KEY_RESERVED, /* 0x14: VOLUME UP */
3210 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3211 KEY_RESERVED, /* 0x16: MUTE */
3213 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3215 /* (assignments unknown, please report if found) */
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 /* No assignments, only used for Adaptive keyboards. */
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,
3226 /* No assignment, used for newer Lenovo models */
3227 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3228 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3229 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3230 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3231 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3232 KEY_UNKNOWN, KEY_UNKNOWN
3236 /* Generic keymap for Lenovo ThinkPads */
3237 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3238 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3239 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3240 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3241 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3243 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3244 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3245 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3246 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3248 /* These should be enabled --only-- when ACPI video
3249 * is disabled (i.e. in "vendor" mode), and are handled
3250 * in a special way by the init code */
3251 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3252 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3254 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3256 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3257 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3259 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3260 * react to it and reprograms the built-in *extra* mixer.
3261 * Never map it to control another mixer by default.
3263 * T60?, T61, R60?, R61: firmware and EC tries to send
3264 * these over the regular keyboard, so these are no-ops,
3265 * but there are still weird bugs re. MUTE, so do not
3266 * change unless you get test reports from all Lenovo
3267 * models. May cause the BIOS to interfere with the
3270 KEY_RESERVED, /* 0x14: VOLUME UP */
3271 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3272 KEY_RESERVED, /* 0x16: MUTE */
3274 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3276 /* (assignments unknown, please report if found) */
3277 KEY_UNKNOWN, KEY_UNKNOWN,
3280 * The mic mute button only sends 0x1a. It does not
3281 * automatically mute the mic or change the mute light.
3283 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3285 /* (assignments unknown, please report if found) */
3288 /* Extra keys in use since the X240 / T440 / T540 */
3289 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3292 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3293 * The first item in this list is the Mute button which is
3294 * emitted with 0x103 through
3295 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3297 * We'll need to offset those by 0x20.
3299 KEY_RESERVED, /* Mute held, 0x103 */
3300 KEY_BRIGHTNESS_MIN, /* Backlight off */
3301 KEY_RESERVED, /* Clipping tool */
3302 KEY_RESERVED, /* Cloud */
3304 KEY_VOICECOMMAND, /* Voice */
3306 KEY_RESERVED, /* Gestures */
3310 KEY_CONFIG, /* Settings */
3311 KEY_RESERVED, /* New tab */
3312 KEY_REFRESH, /* Reload */
3313 KEY_BACK, /* Back */
3314 KEY_RESERVED, /* Microphone down */
3315 KEY_RESERVED, /* Microphone up */
3316 KEY_RESERVED, /* Microphone cancellation */
3317 KEY_RESERVED, /* Camera mode */
3318 KEY_RESERVED, /* Rotate display, 0x116 */
3321 * These are found in 2017 models (e.g. T470s, X270).
3322 * The lowest known value is 0x311, which according to
3323 * the manual should launch a user defined favorite
3326 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3327 * corresponding to 0x34.
3330 /* (assignments unknown, please report if found) */
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3338 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3339 KEY_CALC, /* Calculator (above numpad, P52) */
3340 KEY_BLUETOOTH, /* Bluetooth */
3341 KEY_KEYBOARD, /* Keyboard, 0x315 */
3342 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3343 KEY_NOTIFICATION_CENTER, /* Notification Center */
3344 KEY_PICKUP_PHONE, /* Answer incoming call */
3345 KEY_HANGUP_PHONE, /* Decline incoming call */
3349 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3350 /* Generic maps (fallback) */
3352 .vendor = PCI_VENDOR_ID_IBM,
3353 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3354 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3357 .vendor = PCI_VENDOR_ID_LENOVO,
3358 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3359 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3363 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3364 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3369 bool radiosw_state = false;
3370 bool tabletsw_state = false;
3372 unsigned long quirks;
3373 unsigned long keymap_id;
3375 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3376 "initializing hotkey subdriver\n");
3378 BUG_ON(!tpacpi_inputdev);
3379 BUG_ON(tpacpi_inputdev->open != NULL ||
3380 tpacpi_inputdev->close != NULL);
3382 TPACPI_ACPIHANDLE_INIT(hkey);
3383 mutex_init(&hotkey_mutex);
3385 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3386 mutex_init(&hotkey_thread_data_mutex);
3389 /* hotkey not supported on 570 */
3390 tp_features.hotkey = hkey_handle != NULL;
3392 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3394 str_supported(tp_features.hotkey));
3396 if (!tp_features.hotkey)
3399 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3400 ARRAY_SIZE(tpacpi_hotkey_qtable));
3402 tpacpi_disable_brightness_delay();
3404 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3405 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3406 for HKEY interface version 0x100 */
3407 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3408 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3409 "firmware HKEY interface version: 0x%x\n",
3412 switch (hkeyv >> 8) {
3415 * MHKV 0x100 in A31, R40, R40e,
3416 * T4x, X31, and later
3419 /* Paranoia check AND init hotkey_all_mask */
3420 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3422 pr_err("missing MHKA handler, please report this to %s\n",
3424 /* Fallback: pre-init for FN+F3,F4,F12 */
3425 hotkey_all_mask = 0x080cU;
3427 tp_features.hotkey_mask = 1;
3433 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3436 /* Paranoia check AND init hotkey_all_mask */
3437 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3439 pr_err("missing MHKA handler, please report this to %s\n",
3441 /* Fallback: pre-init for FN+F3,F4,F12 */
3442 hotkey_all_mask = 0x080cU;
3444 tp_features.hotkey_mask = 1;
3448 * Check if we have an adaptive keyboard, like on the
3449 * Lenovo Carbon X1 2014 (2nd Gen).
3451 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3453 if (hotkey_adaptive_all_mask != 0)
3454 tp_features.has_adaptive_kbd = true;
3456 tp_features.has_adaptive_kbd = false;
3457 hotkey_adaptive_all_mask = 0x0U;
3462 pr_err("unknown version of the HKEY interface: 0x%x\n",
3464 pr_err("please report this to %s\n", TPACPI_MAIL);
3469 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3470 "hotkey masks are %s\n",
3471 str_supported(tp_features.hotkey_mask));
3473 /* Init hotkey_all_mask if not initialized yet */
3474 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3475 (quirks & TPACPI_HK_Q_INIMASK))
3476 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3478 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3479 if (tp_features.hotkey_mask) {
3480 /* hotkey_source_mask *must* be zero for
3481 * the first hotkey_mask_get to return hotkey_orig_mask */
3482 res = hotkey_mask_get();
3486 hotkey_orig_mask = hotkey_acpi_mask;
3488 hotkey_orig_mask = hotkey_all_mask;
3489 hotkey_acpi_mask = hotkey_all_mask;
3492 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3494 tp_features.hotkey_wlsw = 1;
3495 radiosw_state = !!tpacpi_wlsw_emulstate;
3496 pr_info("radio switch emulation enabled\n");
3499 /* Not all thinkpads have a hardware radio switch */
3500 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3501 tp_features.hotkey_wlsw = 1;
3502 radiosw_state = !!status;
3503 pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
3506 tabletsw_state = hotkey_init_tablet_mode();
3508 /* Set up key map */
3509 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3510 ARRAY_SIZE(tpacpi_keymap_qtable));
3511 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3512 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3513 "using keymap number %lu\n", keymap_id);
3515 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3516 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3517 if (!hotkey_keycode_map) {
3518 pr_err("failed to allocate memory for key map\n");
3522 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3523 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3524 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3525 tpacpi_inputdev->keycode = hotkey_keycode_map;
3526 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3527 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3528 input_set_capability(tpacpi_inputdev, EV_KEY,
3529 hotkey_keycode_map[i]);
3531 if (i < sizeof(hotkey_reserved_mask)*8)
3532 hotkey_reserved_mask |= 1 << i;
3536 if (tp_features.hotkey_wlsw) {
3537 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3538 input_report_switch(tpacpi_inputdev,
3539 SW_RFKILL_ALL, radiosw_state);
3541 if (tp_features.hotkey_tablet) {
3542 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3543 input_report_switch(tpacpi_inputdev,
3544 SW_TABLET_MODE, tabletsw_state);
3547 /* Do not issue duplicate brightness change events to
3548 * userspace. tpacpi_detect_brightness_capabilities() must have
3549 * been called before this point */
3550 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3551 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3552 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3554 /* Disable brightness up/down on Lenovo thinkpads when
3555 * ACPI is handling them, otherwise it is plain impossible
3556 * for userspace to do something even remotely sane */
3557 hotkey_reserved_mask |=
3558 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3559 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3560 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3561 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3564 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3565 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3567 & ~hotkey_reserved_mask;
3569 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3570 "hotkey source mask 0x%08x, polling freq %u\n",
3571 hotkey_source_mask, hotkey_poll_freq);
3574 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3575 "enabling firmware HKEY event interface...\n");
3576 res = hotkey_status_set(true);
3581 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3582 | hotkey_driver_mask)
3583 & ~hotkey_source_mask);
3584 if (res < 0 && res != -ENXIO) {
3588 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3589 & ~hotkey_reserved_mask;
3590 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3591 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3592 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3594 tpacpi_inputdev->open = &hotkey_inputdev_open;
3595 tpacpi_inputdev->close = &hotkey_inputdev_close;
3597 hotkey_poll_setup_safe(true);
3602 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3603 * mode, Web conference mode, Function mode and Lay-flat mode.
3604 * We support Home mode and Function mode currently.
3606 * Will consider support rest of modes in future.
3609 static const int adaptive_keyboard_modes[] = {
3611 /* WEB_BROWSER_MODE = 2,
3612 WEB_CONFERENCE_MODE = 3, */
3616 #define DFR_CHANGE_ROW 0x101
3617 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3618 #define FIRST_ADAPTIVE_KEY 0x103
3620 /* press Fn key a while second, it will switch to Function Mode. Then
3621 * release Fn key, previous mode be restored.
3623 static bool adaptive_keyboard_mode_is_saved;
3624 static int adaptive_keyboard_prev_mode;
3626 static int adaptive_keyboard_get_mode(void)
3630 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3631 pr_err("Cannot read adaptive keyboard mode\n");
3638 static int adaptive_keyboard_set_mode(int new_mode)
3641 new_mode > LAYFLAT_MODE)
3644 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3645 pr_err("Cannot set adaptive keyboard mode\n");
3652 static int adaptive_keyboard_get_next_mode(int mode)
3655 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3657 for (i = 0; i <= max_mode; i++) {
3658 if (adaptive_keyboard_modes[i] == mode)
3667 return adaptive_keyboard_modes[i];
3670 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3672 int current_mode = 0;
3677 case DFR_CHANGE_ROW:
3678 if (adaptive_keyboard_mode_is_saved) {
3679 new_mode = adaptive_keyboard_prev_mode;
3680 adaptive_keyboard_mode_is_saved = false;
3682 current_mode = adaptive_keyboard_get_mode();
3683 if (current_mode < 0)
3685 new_mode = adaptive_keyboard_get_next_mode(
3689 if (adaptive_keyboard_set_mode(new_mode) < 0)
3694 case DFR_SHOW_QUICKVIEW_ROW:
3695 current_mode = adaptive_keyboard_get_mode();
3696 if (current_mode < 0)
3699 adaptive_keyboard_prev_mode = current_mode;
3700 adaptive_keyboard_mode_is_saved = true;
3702 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3707 if (scancode < FIRST_ADAPTIVE_KEY ||
3708 scancode >= FIRST_ADAPTIVE_KEY +
3709 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3710 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3711 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3715 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3716 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3717 if (keycode != KEY_RESERVED) {
3718 mutex_lock(&tpacpi_inputdev_send_mutex);
3720 input_report_key(tpacpi_inputdev, keycode, 1);
3721 input_sync(tpacpi_inputdev);
3723 input_report_key(tpacpi_inputdev, keycode, 0);
3724 input_sync(tpacpi_inputdev);
3726 mutex_unlock(&tpacpi_inputdev_send_mutex);
3732 static bool hotkey_notify_extended_hotkey(const u32 hkey)
3734 unsigned int scancode;
3737 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3738 case TP_HKEY_EV_AMT_TOGGLE:
3739 tpacpi_driver_event(hkey);
3743 /* Extended keycodes start at 0x300 and our offset into the map
3744 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3745 * will be positive, but might not be in the correct range.
3747 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3748 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3749 scancode < TPACPI_HOTKEY_MAP_LEN) {
3750 tpacpi_input_send_key(scancode);
3757 static bool hotkey_notify_hotkey(const u32 hkey,
3759 bool *ignore_acpi_ev)
3761 /* 0x1000-0x1FFF: key presses */
3762 unsigned int scancode = hkey & 0xfff;
3763 *send_acpi_ev = true;
3764 *ignore_acpi_ev = false;
3767 * Original events are in the 0x10XX range, the adaptive keyboard
3768 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3769 * models, additional keys are emitted through 0x13XX.
3771 switch ((hkey >> 8) & 0xf) {
3774 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3775 /* HKEY event 0x1001 is scancode 0x00 */
3777 if (!(hotkey_source_mask & (1 << scancode))) {
3778 tpacpi_input_send_key_masked(scancode);
3779 *send_acpi_ev = false;
3781 *ignore_acpi_ev = true;
3788 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3791 return hotkey_notify_extended_hotkey(hkey);
3797 static bool hotkey_notify_wakeup(const u32 hkey,
3799 bool *ignore_acpi_ev)
3801 /* 0x2000-0x2FFF: Wakeup reason */
3802 *send_acpi_ev = true;
3803 *ignore_acpi_ev = false;
3806 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3807 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3808 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3809 *ignore_acpi_ev = true;
3812 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3813 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3814 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3815 *ignore_acpi_ev = true;
3818 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3819 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3820 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3821 /* how to auto-heal: */
3822 /* 2313: woke up from S3, go to S4/S5 */
3823 /* 2413: woke up from S4, go to S5 */
3830 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3831 pr_info("woke up due to a hot-unplug request...\n");
3832 hotkey_wakeup_reason_notify_change();
3837 static bool hotkey_notify_dockevent(const u32 hkey,
3839 bool *ignore_acpi_ev)
3841 /* 0x4000-0x4FFF: dock-related events */
3842 *send_acpi_ev = true;
3843 *ignore_acpi_ev = false;
3846 case TP_HKEY_EV_UNDOCK_ACK:
3847 /* ACPI undock operation completed after wakeup */
3848 hotkey_autosleep_ack = 1;
3849 pr_info("undocked\n");
3850 hotkey_wakeup_hotunplug_complete_notify_change();
3853 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3854 pr_info("docked into hotplug port replicator\n");
3856 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3857 pr_info("undocked from hotplug port replicator\n");
3861 * Deliberately ignore attaching and detaching the keybord cover to avoid
3862 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3865 * Please refer to the following thread for more information and a preliminary
3866 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3867 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3868 * the Pico cartridge dock module:
3871 case TP_HKEY_EV_KBD_COVER_ATTACH:
3872 case TP_HKEY_EV_KBD_COVER_DETACH:
3873 *send_acpi_ev = false;
3874 *ignore_acpi_ev = true;
3882 static bool hotkey_notify_usrevent(const u32 hkey,
3884 bool *ignore_acpi_ev)
3886 /* 0x5000-0x5FFF: human interface helpers */
3887 *send_acpi_ev = true;
3888 *ignore_acpi_ev = false;
3891 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3892 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3895 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3896 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3897 tpacpi_input_send_tabletsw();
3898 hotkey_tablet_mode_notify_change();
3899 *send_acpi_ev = false;
3902 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3903 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3904 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3905 /* do not propagate these events */
3906 *ignore_acpi_ev = true;
3914 static void thermal_dump_all_sensors(void);
3915 static void palmsensor_refresh(void);
3917 static bool hotkey_notify_6xxx(const u32 hkey,
3919 bool *ignore_acpi_ev)
3921 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3922 *send_acpi_ev = true;
3923 *ignore_acpi_ev = false;
3926 case TP_HKEY_EV_THM_TABLE_CHANGED:
3927 pr_debug("EC reports: Thermal Table has changed\n");
3928 /* recommended action: do nothing, we don't have
3929 * Lenovo ATM information */
3931 case TP_HKEY_EV_THM_CSM_COMPLETED:
3932 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3933 /* Thermal event - pass on to event handler */
3934 tpacpi_driver_event(hkey);
3936 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3937 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3938 /* recommended action: do nothing, we don't have
3939 * Lenovo ATM information */
3941 case TP_HKEY_EV_ALARM_BAT_HOT:
3942 pr_crit("THERMAL ALARM: battery is too hot!\n");
3943 /* recommended action: warn user through gui */
3945 case TP_HKEY_EV_ALARM_BAT_XHOT:
3946 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3947 /* recommended action: immediate sleep/hibernate */
3949 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3950 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3951 /* recommended action: warn user through gui, that */
3952 /* some internal component is too hot */
3954 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3955 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3956 /* recommended action: immediate sleep/hibernate */
3958 case TP_HKEY_EV_AC_CHANGED:
3959 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3960 * AC status changed; can be triggered by plugging or
3961 * unplugging AC adapter, docking or undocking. */
3965 case TP_HKEY_EV_KEY_NUMLOCK:
3966 case TP_HKEY_EV_KEY_FN:
3967 /* key press events, we just ignore them as long as the EC
3968 * is still reporting them in the normal keyboard stream */
3969 *send_acpi_ev = false;
3970 *ignore_acpi_ev = true;
3973 case TP_HKEY_EV_KEY_FN_ESC:
3974 /* Get the media key status to force the status LED to update */
3975 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3976 *send_acpi_ev = false;
3977 *ignore_acpi_ev = true;
3980 case TP_HKEY_EV_TABLET_CHANGED:
3981 tpacpi_input_send_tabletsw();
3982 hotkey_tablet_mode_notify_change();
3983 *send_acpi_ev = false;
3986 case TP_HKEY_EV_PALM_DETECTED:
3987 case TP_HKEY_EV_PALM_UNDETECTED:
3988 /* palm detected - pass on to event handler */
3989 palmsensor_refresh();
3993 /* report simply as unknown, no sensor dump */
3997 thermal_dump_all_sensors();
4001 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4005 bool ignore_acpi_ev;
4008 if (event != 0x80) {
4009 pr_err("unknown HKEY notification event %d\n", event);
4010 /* forward it to userspace, maybe it knows how to handle it */
4011 acpi_bus_generate_netlink_event(
4012 ibm->acpi->device->pnp.device_class,
4013 dev_name(&ibm->acpi->device->dev),
4019 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4020 pr_err("failed to retrieve HKEY event\n");
4029 send_acpi_ev = true;
4030 ignore_acpi_ev = false;
4032 switch (hkey >> 12) {
4034 /* 0x1000-0x1FFF: key presses */
4035 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4039 /* 0x2000-0x2FFF: Wakeup reason */
4040 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4044 /* 0x3000-0x3FFF: bay-related wakeups */
4046 case TP_HKEY_EV_BAYEJ_ACK:
4047 hotkey_autosleep_ack = 1;
4048 pr_info("bay ejected\n");
4049 hotkey_wakeup_hotunplug_complete_notify_change();
4052 case TP_HKEY_EV_OPTDRV_EJ:
4053 /* FIXME: kick libata if SATA link offline */
4061 /* 0x4000-0x4FFF: dock-related events */
4062 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4066 /* 0x5000-0x5FFF: human interface helpers */
4067 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4071 /* 0x6000-0x6FFF: thermal alarms/notices and
4072 * keyboard events */
4073 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4077 /* 0x7000-0x7FFF: misc */
4078 if (tp_features.hotkey_wlsw &&
4079 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4080 tpacpi_send_radiosw_update();
4085 fallthrough; /* to default */
4090 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4091 pr_notice("please report the conditions when this event happened to %s\n",
4095 /* netlink events */
4096 if (!ignore_acpi_ev && send_acpi_ev) {
4097 acpi_bus_generate_netlink_event(
4098 ibm->acpi->device->pnp.device_class,
4099 dev_name(&ibm->acpi->device->dev),
4105 static void hotkey_suspend(void)
4107 /* Do these on suspend, we get the events on early resume! */
4108 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4109 hotkey_autosleep_ack = 0;
4111 /* save previous mode of adaptive keyboard of X1 Carbon */
4112 if (tp_features.has_adaptive_kbd) {
4113 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4115 pr_err("Cannot read adaptive keyboard mode.\n");
4120 static void hotkey_resume(void)
4122 tpacpi_disable_brightness_delay();
4124 if (hotkey_status_set(true) < 0 ||
4125 hotkey_mask_set(hotkey_acpi_mask) < 0)
4126 pr_err("error while attempting to reset the event firmware interface\n");
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 (strlencmp(cmd, "enable") == 0) {
4200 hotkey_enabledisable_warn(1);
4201 } else if (strlencmp(cmd, "disable") == 0) {
4202 hotkey_enabledisable_warn(0);
4204 } else if (strlencmp(cmd, "reset") == 0) {
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"),
4427 .ident = "L14 Gen2 AMD",
4428 .driver_data = &quirk_s2idle_bug,
4430 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4431 DMI_MATCH(DMI_PRODUCT_NAME, "20X5"),
4435 .ident = "T14s Gen2 AMD",
4436 .driver_data = &quirk_s2idle_bug,
4438 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4439 DMI_MATCH(DMI_PRODUCT_NAME, "20XF"),
4443 .ident = "X13 Gen2 AMD",
4444 .driver_data = &quirk_s2idle_bug,
4446 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4447 DMI_MATCH(DMI_PRODUCT_NAME, "20XH"),
4451 .ident = "T14 Gen2 AMD",
4452 .driver_data = &quirk_s2idle_bug,
4454 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4455 DMI_MATCH(DMI_PRODUCT_NAME, "20XK"),
4459 .ident = "T14 Gen1 AMD",
4460 .driver_data = &quirk_s2idle_bug,
4462 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4463 DMI_MATCH(DMI_PRODUCT_NAME, "20UD"),
4467 .ident = "T14 Gen1 AMD",
4468 .driver_data = &quirk_s2idle_bug,
4470 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4471 DMI_MATCH(DMI_PRODUCT_NAME, "20UE"),
4475 .ident = "T14s Gen1 AMD",
4476 .driver_data = &quirk_s2idle_bug,
4478 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4479 DMI_MATCH(DMI_PRODUCT_NAME, "20UH"),
4483 .ident = "P14s Gen1 AMD",
4484 .driver_data = &quirk_s2idle_bug,
4486 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4487 DMI_MATCH(DMI_PRODUCT_NAME, "20Y1"),
4491 .ident = "P14s Gen2 AMD",
4492 .driver_data = &quirk_s2idle_bug,
4494 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4495 DMI_MATCH(DMI_PRODUCT_NAME, "21A0"),
4501 #ifdef CONFIG_SUSPEND
4503 * Lenovo laptops from a variety of generations run a SMI handler during the D3->D0
4504 * transition that occurs specifically when exiting suspend to idle which can cause
4505 * large delays during resume when the IOMMU translation layer is enabled (the default
4506 * behavior) for NVME devices:
4508 * To avoid this firmware problem, skip the SMI handler on these machines before the
4509 * D0 transition occurs.
4511 static void thinkpad_acpi_amd_s2idle_restore(void)
4513 struct resource *res;
4517 res = request_mem_region_muxed(tp_features.quirks->s2idle_bug_mmio, 1,
4518 "thinkpad_acpi_pm80");
4522 addr = ioremap(tp_features.quirks->s2idle_bug_mmio, 1);
4524 goto cleanup_resource;
4526 val = ioread8(addr);
4527 iowrite8(val & ~BIT(0), addr);
4531 release_resource(res);
4535 static struct acpi_s2idle_dev_ops thinkpad_acpi_s2idle_dev_ops = {
4536 .restore = thinkpad_acpi_amd_s2idle_restore,
4540 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4541 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4542 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4543 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4548 static int __init have_bt_fwbug(void)
4551 * Some AMD based ThinkPads have a firmware bug that calling
4552 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4554 if (tp_features.quirks && tp_features.quirks->btusb_bug &&
4555 pci_dev_present(fwbug_cards_ids)) {
4556 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4557 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4563 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4568 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4569 "initializing bluetooth subdriver\n");
4571 TPACPI_ACPIHANDLE_INIT(hkey);
4573 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4574 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4575 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4576 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4578 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4579 "bluetooth is %s, status 0x%02x\n",
4580 str_supported(tp_features.bluetooth),
4583 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4584 if (dbg_bluetoothemul) {
4585 tp_features.bluetooth = 1;
4586 pr_info("bluetooth switch emulation enabled\n");
4589 if (tp_features.bluetooth &&
4590 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4591 /* no bluetooth hardware present in system */
4592 tp_features.bluetooth = 0;
4593 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4594 "bluetooth hardware not installed\n");
4597 if (!tp_features.bluetooth)
4600 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4601 &bluetooth_tprfk_ops,
4602 RFKILL_TYPE_BLUETOOTH,
4603 TPACPI_RFK_BLUETOOTH_SW_NAME,
4608 /* procfs -------------------------------------------------------------- */
4609 static int bluetooth_read(struct seq_file *m)
4611 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4614 static int bluetooth_write(char *buf)
4616 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4619 static struct ibm_struct bluetooth_driver_data = {
4620 .name = "bluetooth",
4621 .read = bluetooth_read,
4622 .write = bluetooth_write,
4623 .exit = bluetooth_exit,
4624 .shutdown = bluetooth_shutdown,
4627 /*************************************************************************
4632 /* ACPI GWAN/SWAN bits */
4633 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4634 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4635 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4636 0 = disable, 1 = enable */
4639 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4641 static int wan_get_status(void)
4645 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4647 return (tpacpi_wwan_emulstate) ?
4648 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4651 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4654 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4655 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4658 static int wan_set_status(enum tpacpi_rfkill_state state)
4662 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
4663 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4665 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4667 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4672 if (state == TPACPI_RFK_RADIO_ON)
4673 status = TP_ACPI_WANCARD_RADIOSSW
4674 | TP_ACPI_WANCARD_RESUMECTRL;
4678 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4684 /* sysfs wan enable ---------------------------------------------------- */
4685 static ssize_t wan_enable_show(struct device *dev,
4686 struct device_attribute *attr,
4689 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4693 static ssize_t wan_enable_store(struct device *dev,
4694 struct device_attribute *attr,
4695 const char *buf, size_t count)
4697 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4701 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4702 wan_enable_show, wan_enable_store);
4704 /* --------------------------------------------------------------------- */
4706 static struct attribute *wan_attributes[] = {
4707 &dev_attr_wwan_enable.attr,
4711 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4714 return tp_features.wan ? attr->mode : 0;
4717 static const struct attribute_group wan_attr_group = {
4718 .is_visible = wan_attr_is_visible,
4719 .attrs = wan_attributes,
4722 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4723 .get_status = wan_get_status,
4724 .set_status = wan_set_status,
4727 static void wan_shutdown(void)
4729 /* Order firmware to save current state to NVRAM */
4730 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4731 TP_ACPI_WGSV_SAVE_STATE))
4732 pr_notice("failed to save WWAN state to NVRAM\n");
4734 vdbg_printk(TPACPI_DBG_RFKILL,
4735 "WWAN state saved to NVRAM\n");
4738 static void wan_exit(void)
4740 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4744 static int __init wan_init(struct ibm_init_struct *iibm)
4749 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4750 "initializing wan subdriver\n");
4752 TPACPI_ACPIHANDLE_INIT(hkey);
4754 tp_features.wan = hkey_handle &&
4755 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4757 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4758 "wan is %s, status 0x%02x\n",
4759 str_supported(tp_features.wan),
4762 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4764 tp_features.wan = 1;
4765 pr_info("wwan switch emulation enabled\n");
4768 if (tp_features.wan &&
4769 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4770 /* no wan hardware present in system */
4771 tp_features.wan = 0;
4772 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4773 "wan hardware not installed\n");
4776 if (!tp_features.wan)
4779 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4782 TPACPI_RFK_WWAN_SW_NAME,
4787 /* procfs -------------------------------------------------------------- */
4788 static int wan_read(struct seq_file *m)
4790 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4793 static int wan_write(char *buf)
4795 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4798 static struct ibm_struct wan_driver_data = {
4803 .shutdown = wan_shutdown,
4806 /*************************************************************************
4811 /* ACPI GUWB/SUWB bits */
4812 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4813 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4816 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4818 static int uwb_get_status(void)
4822 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4824 return (tpacpi_uwb_emulstate) ?
4825 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4828 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4831 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4832 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4835 static int uwb_set_status(enum tpacpi_rfkill_state state)
4839 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
4840 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4842 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4844 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4849 if (state == TPACPI_RFK_RADIO_ON)
4850 status = TP_ACPI_UWB_RADIOSSW;
4854 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4860 /* --------------------------------------------------------------------- */
4862 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4863 .get_status = uwb_get_status,
4864 .set_status = uwb_set_status,
4867 static void uwb_exit(void)
4869 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4872 static int __init uwb_init(struct ibm_init_struct *iibm)
4877 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4878 "initializing uwb subdriver\n");
4880 TPACPI_ACPIHANDLE_INIT(hkey);
4882 tp_features.uwb = hkey_handle &&
4883 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4885 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4886 "uwb is %s, status 0x%02x\n",
4887 str_supported(tp_features.uwb),
4890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4892 tp_features.uwb = 1;
4893 pr_info("uwb switch emulation enabled\n");
4896 if (tp_features.uwb &&
4897 !(status & TP_ACPI_UWB_HWPRESENT)) {
4898 /* no uwb hardware present in system */
4899 tp_features.uwb = 0;
4900 dbg_printk(TPACPI_DBG_INIT,
4901 "uwb hardware not installed\n");
4904 if (!tp_features.uwb)
4907 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4910 TPACPI_RFK_UWB_SW_NAME,
4915 static struct ibm_struct uwb_driver_data = {
4918 .flags.experimental = 1,
4921 /*************************************************************************
4925 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4927 enum video_access_mode {
4928 TPACPI_VIDEO_NONE = 0,
4929 TPACPI_VIDEO_570, /* 570 */
4930 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4931 TPACPI_VIDEO_NEW, /* all others */
4934 enum { /* video status flags, based on VIDEO_570 */
4935 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4936 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4937 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4940 enum { /* TPACPI_VIDEO_570 constants */
4941 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4942 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4943 * video_status_flags */
4944 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4945 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4948 static enum video_access_mode video_supported;
4949 static int video_orig_autosw;
4951 static int video_autosw_get(void);
4952 static int video_autosw_set(int enable);
4954 TPACPI_HANDLE(vid, root,
4955 "\\_SB.PCI.AGP.VGA", /* 570 */
4956 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4957 "\\_SB.PCI0.VID0", /* 770e */
4958 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4959 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4960 "\\_SB.PCI0.AGP.VID", /* all others */
4963 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4965 static int __init video_init(struct ibm_init_struct *iibm)
4969 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4971 TPACPI_ACPIHANDLE_INIT(vid);
4972 if (tpacpi_is_ibm())
4973 TPACPI_ACPIHANDLE_INIT(vid2);
4975 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4976 /* G41, assume IVGA doesn't change */
4977 vid_handle = vid2_handle;
4980 /* video switching not supported on R30, R31 */
4981 video_supported = TPACPI_VIDEO_NONE;
4982 else if (tpacpi_is_ibm() &&
4983 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4985 video_supported = TPACPI_VIDEO_570;
4986 else if (tpacpi_is_ibm() &&
4987 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4988 /* 600e/x, 770e, 770x */
4989 video_supported = TPACPI_VIDEO_770;
4992 video_supported = TPACPI_VIDEO_NEW;
4994 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4995 str_supported(video_supported != TPACPI_VIDEO_NONE),
4998 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
5001 static void video_exit(void)
5003 dbg_printk(TPACPI_DBG_EXIT,
5004 "restoring original video autoswitch mode\n");
5005 if (video_autosw_set(video_orig_autosw))
5006 pr_err("error while trying to restore original video autoswitch mode\n");
5009 static int video_outputsw_get(void)
5014 switch (video_supported) {
5015 case TPACPI_VIDEO_570:
5016 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5017 TP_ACPI_VIDEO_570_PHSCMD))
5019 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5021 case TPACPI_VIDEO_770:
5022 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5025 status |= TP_ACPI_VIDEO_S_LCD;
5026 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5029 status |= TP_ACPI_VIDEO_S_CRT;
5031 case TPACPI_VIDEO_NEW:
5032 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5033 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5036 status |= TP_ACPI_VIDEO_S_CRT;
5038 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5039 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5042 status |= TP_ACPI_VIDEO_S_LCD;
5043 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5046 status |= TP_ACPI_VIDEO_S_DVI;
5055 static int video_outputsw_set(int status)
5060 switch (video_supported) {
5061 case TPACPI_VIDEO_570:
5062 res = acpi_evalf(NULL, NULL,
5063 "\\_SB.PHS2", "vdd",
5064 TP_ACPI_VIDEO_570_PHS2CMD,
5065 status | TP_ACPI_VIDEO_570_PHS2SET);
5067 case TPACPI_VIDEO_770:
5068 autosw = video_autosw_get();
5072 res = video_autosw_set(1);
5075 res = acpi_evalf(vid_handle, NULL,
5076 "ASWT", "vdd", status * 0x100, 0);
5077 if (!autosw && video_autosw_set(autosw)) {
5078 pr_err("video auto-switch left enabled due to error\n");
5082 case TPACPI_VIDEO_NEW:
5083 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5084 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5090 return (res) ? 0 : -EIO;
5093 static int video_autosw_get(void)
5097 switch (video_supported) {
5098 case TPACPI_VIDEO_570:
5099 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5102 case TPACPI_VIDEO_770:
5103 case TPACPI_VIDEO_NEW:
5104 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5114 static int video_autosw_set(int enable)
5116 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5121 static int video_outputsw_cycle(void)
5123 int autosw = video_autosw_get();
5129 switch (video_supported) {
5130 case TPACPI_VIDEO_570:
5131 res = video_autosw_set(1);
5134 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5136 case TPACPI_VIDEO_770:
5137 case TPACPI_VIDEO_NEW:
5138 res = video_autosw_set(1);
5141 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5146 if (!autosw && video_autosw_set(autosw)) {
5147 pr_err("video auto-switch left enabled due to error\n");
5151 return (res) ? 0 : -EIO;
5154 static int video_expand_toggle(void)
5156 switch (video_supported) {
5157 case TPACPI_VIDEO_570:
5158 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5160 case TPACPI_VIDEO_770:
5161 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5163 case TPACPI_VIDEO_NEW:
5164 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5172 static int video_read(struct seq_file *m)
5176 if (video_supported == TPACPI_VIDEO_NONE) {
5177 seq_printf(m, "status:\t\tnot supported\n");
5181 /* Even reads can crash X.org, so... */
5182 if (!capable(CAP_SYS_ADMIN))
5185 status = video_outputsw_get();
5189 autosw = video_autosw_get();
5193 seq_printf(m, "status:\t\tsupported\n");
5194 seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
5195 seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
5196 if (video_supported == TPACPI_VIDEO_NEW)
5197 seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
5198 seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
5199 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5200 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5201 if (video_supported == TPACPI_VIDEO_NEW)
5202 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5203 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5204 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5209 static int video_write(char *buf)
5212 int enable, disable, status;
5215 if (video_supported == TPACPI_VIDEO_NONE)
5218 /* Even reads can crash X.org, let alone writes... */
5219 if (!capable(CAP_SYS_ADMIN))
5225 while ((cmd = strsep(&buf, ","))) {
5226 if (strlencmp(cmd, "lcd_enable") == 0) {
5227 enable |= TP_ACPI_VIDEO_S_LCD;
5228 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5229 disable |= TP_ACPI_VIDEO_S_LCD;
5230 } else if (strlencmp(cmd, "crt_enable") == 0) {
5231 enable |= TP_ACPI_VIDEO_S_CRT;
5232 } else if (strlencmp(cmd, "crt_disable") == 0) {
5233 disable |= TP_ACPI_VIDEO_S_CRT;
5234 } else if (video_supported == TPACPI_VIDEO_NEW &&
5235 strlencmp(cmd, "dvi_enable") == 0) {
5236 enable |= TP_ACPI_VIDEO_S_DVI;
5237 } else if (video_supported == TPACPI_VIDEO_NEW &&
5238 strlencmp(cmd, "dvi_disable") == 0) {
5239 disable |= TP_ACPI_VIDEO_S_DVI;
5240 } else if (strlencmp(cmd, "auto_enable") == 0) {
5241 res = video_autosw_set(1);
5244 } else if (strlencmp(cmd, "auto_disable") == 0) {
5245 res = video_autosw_set(0);
5248 } else if (strlencmp(cmd, "video_switch") == 0) {
5249 res = video_outputsw_cycle();
5252 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5253 res = video_expand_toggle();
5260 if (enable || disable) {
5261 status = video_outputsw_get();
5264 res = video_outputsw_set((status & ~disable) | enable);
5272 static struct ibm_struct video_driver_data = {
5275 .write = video_write,
5279 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5281 /*************************************************************************
5282 * Keyboard backlight subdriver
5285 static enum led_brightness kbdlight_brightness;
5286 static DEFINE_MUTEX(kbdlight_mutex);
5288 static int kbdlight_set_level(int level)
5295 mutex_lock(&kbdlight_mutex);
5297 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5300 kbdlight_brightness = level;
5302 mutex_unlock(&kbdlight_mutex);
5307 static int kbdlight_get_level(void)
5314 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5320 return status & 0x3;
5323 static bool kbdlight_is_supported(void)
5330 if (!acpi_has_method(hkey_handle, "MLCG")) {
5331 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5335 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5336 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5341 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5345 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5347 * Guessed test for keyboard backlight:
5349 * Machines with backlight keyboard return:
5350 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5351 * b110100010010000000XX - ThinkPad x230
5352 * b010100000010000000XX - ThinkPad x240
5353 * b010100000010000000XX - ThinkPad W541
5354 * (XX is current backlight level)
5356 * Machines without backlight keyboard return:
5357 * b10100001000000000000 - ThinkPad x230
5358 * b10110001000000000000 - ThinkPad E430
5359 * b00000000000000000000 - ThinkPad E450
5361 * Candidate BITs for detection test (XOR):
5362 * b01000000001000000000
5365 return status & BIT(9);
5368 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5369 enum led_brightness brightness)
5371 return kbdlight_set_level(brightness);
5374 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5378 level = kbdlight_get_level();
5385 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5387 .name = "tpacpi::kbd_backlight",
5388 .max_brightness = 2,
5389 .flags = LED_BRIGHT_HW_CHANGED,
5390 .brightness_set_blocking = &kbdlight_sysfs_set,
5391 .brightness_get = &kbdlight_sysfs_get,
5395 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5399 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5401 TPACPI_ACPIHANDLE_INIT(hkey);
5403 if (!kbdlight_is_supported()) {
5404 tp_features.kbdlight = 0;
5405 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5409 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5410 tp_features.kbdlight = 1;
5412 rc = led_classdev_register(&tpacpi_pdev->dev,
5413 &tpacpi_led_kbdlight.led_classdev);
5415 tp_features.kbdlight = 0;
5419 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5420 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5424 static void kbdlight_exit(void)
5426 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5429 static int kbdlight_set_level_and_update(int level)
5432 struct led_classdev *led_cdev;
5434 ret = kbdlight_set_level(level);
5435 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5437 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5438 led_cdev->brightness = level;
5443 static int kbdlight_read(struct seq_file *m)
5447 if (!tp_features.kbdlight) {
5448 seq_printf(m, "status:\t\tnot supported\n");
5450 level = kbdlight_get_level();
5452 seq_printf(m, "status:\t\terror %d\n", level);
5454 seq_printf(m, "status:\t\t%d\n", level);
5455 seq_printf(m, "commands:\t0, 1, 2\n");
5461 static int kbdlight_write(char *buf)
5464 int res, level = -EINVAL;
5466 if (!tp_features.kbdlight)
5469 while ((cmd = strsep(&buf, ","))) {
5470 res = kstrtoint(cmd, 10, &level);
5475 if (level >= 3 || level < 0)
5478 return kbdlight_set_level_and_update(level);
5481 static void kbdlight_suspend(void)
5483 struct led_classdev *led_cdev;
5485 if (!tp_features.kbdlight)
5488 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5489 led_update_brightness(led_cdev);
5490 led_classdev_suspend(led_cdev);
5493 static void kbdlight_resume(void)
5495 if (!tp_features.kbdlight)
5498 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5501 static struct ibm_struct kbdlight_driver_data = {
5503 .read = kbdlight_read,
5504 .write = kbdlight_write,
5505 .suspend = kbdlight_suspend,
5506 .resume = kbdlight_resume,
5507 .exit = kbdlight_exit,
5510 /*************************************************************************
5511 * Light (thinklight) subdriver
5514 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5515 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5517 static int light_get_status(void)
5521 if (tp_features.light_status) {
5522 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5530 static int light_set_status(int status)
5534 if (tp_features.light) {
5536 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5538 TP_CMOS_THINKLIGHT_ON :
5539 TP_CMOS_THINKLIGHT_OFF);
5541 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5544 return (rc) ? 0 : -EIO;
5550 static int light_sysfs_set(struct led_classdev *led_cdev,
5551 enum led_brightness brightness)
5553 return light_set_status((brightness != LED_OFF) ?
5554 TPACPI_LED_ON : TPACPI_LED_OFF);
5557 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5559 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5562 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5564 .name = "tpacpi::thinklight",
5565 .brightness_set_blocking = &light_sysfs_set,
5566 .brightness_get = &light_sysfs_get,
5570 static int __init light_init(struct ibm_init_struct *iibm)
5574 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5576 if (tpacpi_is_ibm()) {
5577 TPACPI_ACPIHANDLE_INIT(ledb);
5578 TPACPI_ACPIHANDLE_INIT(lght);
5580 TPACPI_ACPIHANDLE_INIT(cmos);
5582 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5583 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5585 if (tp_features.light)
5586 /* light status not supported on
5587 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5588 tp_features.light_status =
5589 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5591 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5592 str_supported(tp_features.light),
5593 str_supported(tp_features.light_status));
5595 if (!tp_features.light)
5598 rc = led_classdev_register(&tpacpi_pdev->dev,
5599 &tpacpi_led_thinklight.led_classdev);
5602 tp_features.light = 0;
5603 tp_features.light_status = 0;
5611 static void light_exit(void)
5613 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5616 static int light_read(struct seq_file *m)
5620 if (!tp_features.light) {
5621 seq_printf(m, "status:\t\tnot supported\n");
5622 } else if (!tp_features.light_status) {
5623 seq_printf(m, "status:\t\tunknown\n");
5624 seq_printf(m, "commands:\ton, off\n");
5626 status = light_get_status();
5629 seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
5630 seq_printf(m, "commands:\ton, off\n");
5636 static int light_write(char *buf)
5641 if (!tp_features.light)
5644 while ((cmd = strsep(&buf, ","))) {
5645 if (strlencmp(cmd, "on") == 0) {
5647 } else if (strlencmp(cmd, "off") == 0) {
5653 return light_set_status(newstatus);
5656 static struct ibm_struct light_driver_data = {
5659 .write = light_write,
5663 /*************************************************************************
5667 /* sysfs cmos_command -------------------------------------------------- */
5668 static ssize_t cmos_command_store(struct device *dev,
5669 struct device_attribute *attr,
5670 const char *buf, size_t count)
5672 unsigned long cmos_cmd;
5675 if (parse_strtoul(buf, 21, &cmos_cmd))
5678 res = issue_thinkpad_cmos_command(cmos_cmd);
5679 return (res) ? res : count;
5682 static DEVICE_ATTR_WO(cmos_command);
5684 static struct attribute *cmos_attributes[] = {
5685 &dev_attr_cmos_command.attr,
5689 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5690 struct attribute *attr, int n)
5692 return cmos_handle ? attr->mode : 0;
5695 static const struct attribute_group cmos_attr_group = {
5696 .is_visible = cmos_attr_is_visible,
5697 .attrs = cmos_attributes,
5700 /* --------------------------------------------------------------------- */
5702 static int __init cmos_init(struct ibm_init_struct *iibm)
5704 vdbg_printk(TPACPI_DBG_INIT,
5705 "initializing cmos commands subdriver\n");
5707 TPACPI_ACPIHANDLE_INIT(cmos);
5709 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5710 str_supported(cmos_handle != NULL));
5712 return cmos_handle ? 0 : -ENODEV;
5715 static int cmos_read(struct seq_file *m)
5717 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5718 R30, R31, T20-22, X20-21 */
5720 seq_printf(m, "status:\t\tnot supported\n");
5722 seq_printf(m, "status:\t\tsupported\n");
5723 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5729 static int cmos_write(char *buf)
5734 while ((cmd = strsep(&buf, ","))) {
5735 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5736 cmos_cmd >= 0 && cmos_cmd <= 21) {
5741 res = issue_thinkpad_cmos_command(cmos_cmd);
5749 static struct ibm_struct cmos_driver_data = {
5752 .write = cmos_write,
5755 /*************************************************************************
5759 enum led_access_mode {
5760 TPACPI_LED_NONE = 0,
5761 TPACPI_LED_570, /* 570 */
5762 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5763 TPACPI_LED_NEW, /* all others */
5766 enum { /* For TPACPI_LED_OLD */
5767 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5768 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5769 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5772 static enum led_access_mode led_supported;
5774 static acpi_handle led_handle;
5776 #define TPACPI_LED_NUMLEDS 16
5777 static struct tpacpi_led_classdev *tpacpi_leds;
5778 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5779 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5780 /* there's a limit of 19 chars + NULL before 2.6.26 */
5782 "tpacpi:orange:batt",
5783 "tpacpi:green:batt",
5784 "tpacpi::dock_active",
5785 "tpacpi::bay_active",
5786 "tpacpi::dock_batt",
5787 "tpacpi::unknown_led",
5789 "tpacpi::dock_status1",
5790 "tpacpi::dock_status2",
5791 "tpacpi::lid_logo_dot",
5792 "tpacpi::unknown_led3",
5793 "tpacpi::thinkvantage",
5795 #define TPACPI_SAFE_LEDS 0x1481U
5797 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5799 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5802 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5806 static int led_get_status(const unsigned int led)
5809 enum led_status_t led_s;
5811 switch (led_supported) {
5812 case TPACPI_LED_570:
5813 if (!acpi_evalf(ec_handle,
5814 &status, "GLED", "dd", 1 << led))
5816 led_s = (status == 0) ?
5821 tpacpi_led_state_cache[led] = led_s;
5830 static int led_set_status(const unsigned int led,
5831 const enum led_status_t ledstatus)
5833 /* off, on, blink. Index is led_status_t */
5834 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5835 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5839 switch (led_supported) {
5840 case TPACPI_LED_570:
5842 if (unlikely(led > 7))
5844 if (unlikely(tpacpi_is_led_restricted(led)))
5846 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5847 (1 << led), led_sled_arg1[ledstatus]))
5850 case TPACPI_LED_OLD:
5851 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5852 if (unlikely(led > 7))
5854 if (unlikely(tpacpi_is_led_restricted(led)))
5856 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5858 rc = ec_write(TPACPI_LED_EC_HLBL,
5859 (ledstatus == TPACPI_LED_BLINK) << led);
5861 rc = ec_write(TPACPI_LED_EC_HLCL,
5862 (ledstatus != TPACPI_LED_OFF) << led);
5864 case TPACPI_LED_NEW:
5866 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5868 if (unlikely(tpacpi_is_led_restricted(led)))
5870 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5871 led, led_led_arg1[ledstatus]))
5879 tpacpi_led_state_cache[led] = ledstatus;
5884 static int led_sysfs_set(struct led_classdev *led_cdev,
5885 enum led_brightness brightness)
5887 struct tpacpi_led_classdev *data = container_of(led_cdev,
5888 struct tpacpi_led_classdev, led_classdev);
5889 enum led_status_t new_state;
5891 if (brightness == LED_OFF)
5892 new_state = TPACPI_LED_OFF;
5893 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5894 new_state = TPACPI_LED_ON;
5896 new_state = TPACPI_LED_BLINK;
5898 return led_set_status(data->led, new_state);
5901 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5902 unsigned long *delay_on, unsigned long *delay_off)
5904 struct tpacpi_led_classdev *data = container_of(led_cdev,
5905 struct tpacpi_led_classdev, led_classdev);
5907 /* Can we choose the flash rate? */
5908 if (*delay_on == 0 && *delay_off == 0) {
5909 /* yes. set them to the hardware blink rate (1 Hz) */
5910 *delay_on = 500; /* ms */
5911 *delay_off = 500; /* ms */
5912 } else if ((*delay_on != 500) || (*delay_off != 500))
5915 return led_set_status(data->led, TPACPI_LED_BLINK);
5918 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5922 struct tpacpi_led_classdev *data = container_of(led_cdev,
5923 struct tpacpi_led_classdev, led_classdev);
5925 rc = led_get_status(data->led);
5927 if (rc == TPACPI_LED_OFF || rc < 0)
5928 rc = LED_OFF; /* no error handling in led class :( */
5935 static void led_exit(void)
5939 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5940 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5945 static int __init tpacpi_init_led(unsigned int led)
5947 /* LEDs with no name don't get registered */
5948 if (!tpacpi_led_names[led])
5951 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5952 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5953 if (led_supported == TPACPI_LED_570)
5954 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5956 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5957 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5958 tpacpi_leds[led].led = led;
5960 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5963 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5964 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5965 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5966 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5968 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5969 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5970 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5971 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5972 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5973 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5974 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5975 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5977 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5978 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5979 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5980 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5981 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5983 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5984 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5985 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5986 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5988 /* (1) - may have excess leds enabled on MSB */
5990 /* Defaults (order matters, keep last, don't reorder!) */
5992 .vendor = PCI_VENDOR_ID_LENOVO,
5993 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5996 { /* IBM ThinkPads with no EC version string */
5997 .vendor = PCI_VENDOR_ID_IBM,
5998 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6001 { /* IBM ThinkPads with EC version string */
6002 .vendor = PCI_VENDOR_ID_IBM,
6003 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6008 static enum led_access_mode __init led_init_detect_mode(void)
6012 if (tpacpi_is_ibm()) {
6014 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6015 if (ACPI_SUCCESS(status))
6016 return TPACPI_LED_570;
6018 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6019 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6020 if (ACPI_SUCCESS(status))
6021 return TPACPI_LED_OLD;
6025 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6026 if (ACPI_SUCCESS(status))
6027 return TPACPI_LED_NEW;
6029 /* R30, R31, and unknown firmwares */
6031 return TPACPI_LED_NONE;
6034 static int __init led_init(struct ibm_init_struct *iibm)
6038 unsigned long useful_leds;
6040 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6042 led_supported = led_init_detect_mode();
6044 if (led_supported != TPACPI_LED_NONE) {
6045 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6046 ARRAY_SIZE(led_useful_qtable));
6050 led_supported = TPACPI_LED_NONE;
6054 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6055 str_supported(led_supported), led_supported);
6057 if (led_supported == TPACPI_LED_NONE)
6060 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6063 pr_err("Out of memory for LED data\n");
6067 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6068 tpacpi_leds[i].led = -1;
6070 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
6071 rc = tpacpi_init_led(i);
6079 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6080 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6085 #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
6087 static int led_read(struct seq_file *m)
6089 if (!led_supported) {
6090 seq_printf(m, "status:\t\tnot supported\n");
6093 seq_printf(m, "status:\t\tsupported\n");
6095 if (led_supported == TPACPI_LED_570) {
6098 for (i = 0; i < 8; i++) {
6099 status = led_get_status(i);
6102 seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
6106 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6111 static int led_write(char *buf)
6115 enum led_status_t s;
6120 while ((cmd = strsep(&buf, ","))) {
6121 if (sscanf(cmd, "%d", &led) != 1)
6124 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6127 if (tpacpi_leds[led].led < 0)
6130 if (strstr(cmd, "off")) {
6132 } else if (strstr(cmd, "on")) {
6134 } else if (strstr(cmd, "blink")) {
6135 s = TPACPI_LED_BLINK;
6140 rc = led_set_status(led, s);
6148 static struct ibm_struct led_driver_data = {
6155 /*************************************************************************
6159 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6161 #define TPACPI_BEEP_Q1 0x0001
6163 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6164 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6165 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6168 static int __init beep_init(struct ibm_init_struct *iibm)
6170 unsigned long quirks;
6172 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6174 TPACPI_ACPIHANDLE_INIT(beep);
6176 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6177 str_supported(beep_handle != NULL));
6179 quirks = tpacpi_check_quirks(beep_quirk_table,
6180 ARRAY_SIZE(beep_quirk_table));
6182 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6184 return (beep_handle) ? 0 : -ENODEV;
6187 static int beep_read(struct seq_file *m)
6190 seq_printf(m, "status:\t\tnot supported\n");
6192 seq_printf(m, "status:\t\tsupported\n");
6193 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6199 static int beep_write(char *buf)
6207 while ((cmd = strsep(&buf, ","))) {
6208 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6209 beep_cmd >= 0 && beep_cmd <= 17) {
6213 if (tp_features.beep_needs_two_args) {
6214 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6218 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6227 static struct ibm_struct beep_driver_data = {
6230 .write = beep_write,
6233 /*************************************************************************
6237 enum thermal_access_mode {
6238 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6239 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6240 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6241 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6242 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6245 enum { /* TPACPI_THERMAL_TPEC_* */
6246 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6247 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6248 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6249 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6251 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6255 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6256 struct ibm_thermal_sensors_struct {
6257 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6260 static enum thermal_access_mode thermal_read_mode;
6261 static bool thermal_use_labels;
6263 /* idx is zero-based */
6264 static int thermal_get_sensor(int idx, s32 *value)
6270 t = TP_EC_THERMAL_TMP0;
6272 switch (thermal_read_mode) {
6273 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6274 case TPACPI_THERMAL_TPEC_16:
6275 if (idx >= 8 && idx <= 15) {
6276 t = TP_EC_THERMAL_TMP8;
6281 case TPACPI_THERMAL_TPEC_8:
6283 if (!acpi_ec_read(t + idx, &tmp))
6285 *value = tmp * 1000;
6290 case TPACPI_THERMAL_ACPI_UPDT:
6292 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6293 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6295 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6297 *value = (t - 2732) * 100;
6302 case TPACPI_THERMAL_ACPI_TMP07:
6304 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6305 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6307 if (t > 127 || t < -127)
6308 t = TP_EC_THERMAL_TMP_NA;
6314 case TPACPI_THERMAL_NONE:
6322 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6333 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6336 for (i = 0 ; i < n; i++) {
6337 res = thermal_get_sensor(i, &s->temp[i]);
6345 static void thermal_dump_all_sensors(void)
6348 struct ibm_thermal_sensors_struct t;
6350 n = thermal_get_sensors(&t);
6354 pr_notice("temperatures (Celsius):");
6356 for (i = 0; i < n; i++) {
6357 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6358 pr_cont(" %d", (int)(t.temp[i] / 1000));
6366 /* sysfs temp##_input -------------------------------------------------- */
6368 static ssize_t thermal_temp_input_show(struct device *dev,
6369 struct device_attribute *attr,
6372 struct sensor_device_attribute *sensor_attr =
6373 to_sensor_dev_attr(attr);
6374 int idx = sensor_attr->index;
6378 res = thermal_get_sensor(idx, &value);
6381 if (value == TPACPI_THERMAL_SENSOR_NA)
6384 return sysfs_emit(buf, "%d\n", value);
6387 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6388 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6389 thermal_temp_input_show, NULL, _idxB)
6391 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6392 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6393 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6394 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6395 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6396 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6397 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6398 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6399 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6400 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6401 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6402 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6403 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6404 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6405 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6406 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6407 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6410 #define THERMAL_ATTRS(X) \
6411 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6413 static struct attribute *thermal_temp_input_attr[] = {
6433 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6434 struct attribute *attr, int n)
6436 if (thermal_read_mode == TPACPI_THERMAL_NONE)
6439 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
6440 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
6441 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
6442 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
6443 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
6450 static const struct attribute_group thermal_attr_group = {
6451 .is_visible = thermal_attr_is_visible,
6452 .attrs = thermal_temp_input_attr,
6455 #undef THERMAL_SENSOR_ATTR_TEMP
6456 #undef THERMAL_ATTRS
6458 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6460 return sysfs_emit(buf, "CPU\n");
6462 static DEVICE_ATTR_RO(temp1_label);
6464 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6466 return sysfs_emit(buf, "GPU\n");
6468 static DEVICE_ATTR_RO(temp2_label);
6470 static struct attribute *temp_label_attributes[] = {
6471 &dev_attr_temp1_label.attr,
6472 &dev_attr_temp2_label.attr,
6476 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6477 struct attribute *attr, int n)
6479 return thermal_use_labels ? attr->mode : 0;
6482 static const struct attribute_group temp_label_attr_group = {
6483 .is_visible = temp_label_attr_is_visible,
6484 .attrs = temp_label_attributes,
6487 /* --------------------------------------------------------------------- */
6489 static int __init thermal_init(struct ibm_init_struct *iibm)
6491 u8 t, ta1, ta2, ver = 0;
6495 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6497 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6499 if (thinkpad_id.ec_model) {
6501 * Direct EC access mode: sensors at registers
6502 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6503 * non-implemented, thermal sensors return 0x80 when
6505 * The above rule is unfortunately flawed. This has been seen with
6506 * 0xC2 (power supply ID) causing thermal control problems.
6507 * The EC version can be determined by offset 0xEF and at least for
6508 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6509 * are not thermal registers.
6511 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6512 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6515 for (i = 0; i < 8; i++) {
6516 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6523 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6532 /* This is sheer paranoia, but we handle it anyway */
6534 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6535 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6537 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6538 thermal_read_mode = TPACPI_THERMAL_NONE;
6542 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6543 thermal_use_labels = true;
6547 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6550 } else if (acpi_tmp7) {
6551 if (tpacpi_is_ibm() &&
6552 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6553 /* 600e/x, 770e, 770x */
6554 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6556 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6557 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6560 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6561 thermal_read_mode = TPACPI_THERMAL_NONE;
6564 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6565 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6568 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6571 static int thermal_read(struct seq_file *m)
6574 struct ibm_thermal_sensors_struct t;
6576 n = thermal_get_sensors(&t);
6577 if (unlikely(n < 0))
6580 seq_printf(m, "temperatures:\t");
6583 for (i = 0; i < (n - 1); i++)
6584 seq_printf(m, "%d ", t.temp[i] / 1000);
6585 seq_printf(m, "%d\n", t.temp[i] / 1000);
6587 seq_printf(m, "not supported\n");
6592 static struct ibm_struct thermal_driver_data = {
6594 .read = thermal_read,
6597 /*************************************************************************
6598 * Backlight/brightness subdriver
6601 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6604 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6605 * CMOS NVRAM byte 0x5E, bits 0-3.
6607 * EC HBRV (0x31) has the following layout
6608 * Bit 7: unknown function
6609 * Bit 6: unknown function
6610 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6611 * Bit 4: must be set to zero to avoid problems
6612 * Bit 3-0: backlight brightness level
6614 * brightness_get_raw returns status data in the HBRV layout
6616 * WARNING: The X61 has been verified to use HBRV for something else, so
6617 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6618 * testing on the very early *60 Lenovo models...
6622 TP_EC_BACKLIGHT = 0x31,
6624 /* TP_EC_BACKLIGHT bitmasks */
6625 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6626 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6627 TP_EC_BACKLIGHT_MAPSW = 0x20,
6630 enum tpacpi_brightness_access_mode {
6631 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6632 TPACPI_BRGHT_MODE_EC, /* EC control */
6633 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6634 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6635 TPACPI_BRGHT_MODE_MAX
6638 static struct backlight_device *ibm_backlight_device;
6640 static enum tpacpi_brightness_access_mode brightness_mode =
6641 TPACPI_BRGHT_MODE_MAX;
6643 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6645 static struct mutex brightness_mutex;
6647 /* NVRAM brightness access,
6648 * call with brightness_mutex held! */
6649 static unsigned int tpacpi_brightness_nvram_get(void)
6653 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6654 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6655 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6656 lnvram &= bright_maxlvl;
6661 static void tpacpi_brightness_checkpoint_nvram(void)
6666 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6669 vdbg_printk(TPACPI_DBG_BRGHT,
6670 "trying to checkpoint backlight level to NVRAM...\n");
6672 if (mutex_lock_killable(&brightness_mutex) < 0)
6675 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6677 lec &= TP_EC_BACKLIGHT_LVLMSK;
6678 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6680 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6681 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6682 /* NVRAM needs update */
6683 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6684 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6686 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6687 dbg_printk(TPACPI_DBG_BRGHT,
6688 "updated NVRAM backlight level to %u (0x%02x)\n",
6689 (unsigned int) lec, (unsigned int) b_nvram);
6691 vdbg_printk(TPACPI_DBG_BRGHT,
6692 "NVRAM backlight level already is %u (0x%02x)\n",
6693 (unsigned int) lec, (unsigned int) b_nvram);
6696 mutex_unlock(&brightness_mutex);
6700 /* call with brightness_mutex held! */
6701 static int tpacpi_brightness_get_raw(int *status)
6705 switch (brightness_mode) {
6706 case TPACPI_BRGHT_MODE_UCMS_STEP:
6707 *status = tpacpi_brightness_nvram_get();
6709 case TPACPI_BRGHT_MODE_EC:
6710 case TPACPI_BRGHT_MODE_ECNVRAM:
6711 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6720 /* call with brightness_mutex held! */
6721 /* do NOT call with illegal backlight level value */
6722 static int tpacpi_brightness_set_ec(unsigned int value)
6726 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6729 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6730 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6731 (value & TP_EC_BACKLIGHT_LVLMSK))))
6737 /* call with brightness_mutex held! */
6738 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6741 unsigned int current_value, i;
6743 current_value = tpacpi_brightness_nvram_get();
6745 if (value == current_value)
6748 cmos_cmd = (value > current_value) ?
6749 TP_CMOS_BRIGHTNESS_UP :
6750 TP_CMOS_BRIGHTNESS_DOWN;
6751 inc = (value > current_value) ? 1 : -1;
6753 for (i = current_value; i != value; i += inc)
6754 if (issue_thinkpad_cmos_command(cmos_cmd))
6760 /* May return EINTR which can always be mapped to ERESTARTSYS */
6761 static int brightness_set(unsigned int value)
6765 if (value > bright_maxlvl)
6768 vdbg_printk(TPACPI_DBG_BRGHT,
6769 "set backlight level to %d\n", value);
6771 res = mutex_lock_killable(&brightness_mutex);
6775 switch (brightness_mode) {
6776 case TPACPI_BRGHT_MODE_EC:
6777 case TPACPI_BRGHT_MODE_ECNVRAM:
6778 res = tpacpi_brightness_set_ec(value);
6780 case TPACPI_BRGHT_MODE_UCMS_STEP:
6781 res = tpacpi_brightness_set_ucmsstep(value);
6787 mutex_unlock(&brightness_mutex);
6791 /* sysfs backlight class ----------------------------------------------- */
6793 static int brightness_update_status(struct backlight_device *bd)
6795 int level = backlight_get_brightness(bd);
6797 dbg_printk(TPACPI_DBG_BRGHT,
6798 "backlight: attempt to set level to %d\n",
6801 /* it is the backlight class's job (caller) to handle
6802 * EINTR and other errors properly */
6803 return brightness_set(level);
6806 static int brightness_get(struct backlight_device *bd)
6810 res = mutex_lock_killable(&brightness_mutex);
6814 res = tpacpi_brightness_get_raw(&status);
6816 mutex_unlock(&brightness_mutex);
6821 return status & TP_EC_BACKLIGHT_LVLMSK;
6824 static void tpacpi_brightness_notify_change(void)
6826 backlight_force_update(ibm_backlight_device,
6827 BACKLIGHT_UPDATE_HOTKEY);
6830 static const struct backlight_ops ibm_backlight_data = {
6831 .get_brightness = brightness_get,
6832 .update_status = brightness_update_status,
6835 /* --------------------------------------------------------------------- */
6837 static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
6839 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6840 union acpi_object *obj;
6844 status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
6845 if (ACPI_FAILURE(status))
6848 obj = buffer.pointer;
6849 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
6850 acpi_handle_info(adev->handle,
6851 "Unknown _BCL data, please report this to %s\n",
6855 rc = obj->package.count;
6863 * Call _BCL method of video device. On some ThinkPads this will
6864 * switch the firmware to the ACPI brightness control mode.
6867 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6869 struct acpi_device *device;
6871 device = acpi_fetch_acpi_dev(handle);
6875 return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
6880 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6882 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6884 acpi_handle video_device;
6887 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6889 bcl_levels = tpacpi_query_bcl_levels(video_device);
6891 tp_features.bright_acpimode = (bcl_levels > 0);
6893 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6897 * These are only useful for models that have only one possibility
6898 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6901 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6902 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6903 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6905 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6906 /* Models with ATI GPUs known to require ECNVRAM mode */
6907 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6909 /* Models with ATI GPUs that can use ECNVRAM */
6910 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6911 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6912 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6913 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6915 /* Models with Intel Extreme Graphics 2 */
6916 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6917 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6918 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6920 /* Models with Intel GMA900 */
6921 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6922 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6923 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6927 * Returns < 0 for error, otherwise sets tp_features.bright_*
6928 * and bright_maxlvl.
6930 static void __init tpacpi_detect_brightness_capabilities(void)
6934 vdbg_printk(TPACPI_DBG_INIT,
6935 "detecting firmware brightness interface capabilities\n");
6937 /* we could run a quirks check here (same table used by
6938 * brightness_init) if needed */
6941 * We always attempt to detect acpi support, so as to switch
6942 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6943 * going to publish a backlight interface
6945 b = tpacpi_check_std_acpi_brightness_support();
6955 tp_features.bright_unkfw = 1;
6956 bright_maxlvl = b - 1;
6958 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6961 static int __init brightness_init(struct ibm_init_struct *iibm)
6963 struct backlight_properties props;
6965 unsigned long quirks;
6967 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6969 mutex_init(&brightness_mutex);
6971 quirks = tpacpi_check_quirks(brightness_quirk_table,
6972 ARRAY_SIZE(brightness_quirk_table));
6974 /* tpacpi_detect_brightness_capabilities() must have run already */
6976 /* if it is unknown, we don't handle it: it wouldn't be safe */
6977 if (tp_features.bright_unkfw)
6980 if (!brightness_enable) {
6981 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6982 "brightness support disabled by module parameter\n");
6986 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6987 if (brightness_enable > 1) {
6988 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6990 } else if (brightness_enable == 1) {
6991 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6994 } else if (!tp_features.bright_acpimode) {
6995 pr_notice("ACPI backlight interface not available\n");
6999 pr_notice("ACPI native brightness control enabled\n");
7002 * Check for module parameter bogosity, note that we
7003 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7004 * able to detect "unspecified"
7006 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7009 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7010 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7011 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7012 if (quirks & TPACPI_BRGHT_Q_EC)
7013 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7015 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7017 dbg_printk(TPACPI_DBG_BRGHT,
7018 "driver auto-selected brightness_mode=%d\n",
7023 if (!tpacpi_is_ibm() &&
7024 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7025 brightness_mode == TPACPI_BRGHT_MODE_EC))
7028 if (tpacpi_brightness_get_raw(&b) < 0)
7031 memset(&props, 0, sizeof(struct backlight_properties));
7032 props.type = BACKLIGHT_PLATFORM;
7033 props.max_brightness = bright_maxlvl;
7034 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7035 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7037 &ibm_backlight_data,
7039 if (IS_ERR(ibm_backlight_device)) {
7040 int rc = PTR_ERR(ibm_backlight_device);
7041 ibm_backlight_device = NULL;
7042 pr_err("Could not register backlight device\n");
7045 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7046 "brightness is supported\n");
7048 if (quirks & TPACPI_BRGHT_Q_ASK) {
7049 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7051 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7055 /* Added by mistake in early 2007. Probably useless, but it could
7056 * be working around some unknown firmware problem where the value
7057 * read at startup doesn't match the real hardware state... so leave
7058 * it in place just in case */
7059 backlight_update_status(ibm_backlight_device);
7061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7062 "brightness: registering brightness hotkeys as change notification\n");
7063 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7064 | TP_ACPI_HKEY_BRGHTUP_MASK
7065 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7069 static void brightness_suspend(void)
7071 tpacpi_brightness_checkpoint_nvram();
7074 static void brightness_shutdown(void)
7076 tpacpi_brightness_checkpoint_nvram();
7079 static void brightness_exit(void)
7081 if (ibm_backlight_device) {
7082 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7083 "calling backlight_device_unregister()\n");
7084 backlight_device_unregister(ibm_backlight_device);
7087 tpacpi_brightness_checkpoint_nvram();
7090 static int brightness_read(struct seq_file *m)
7094 level = brightness_get(NULL);
7096 seq_printf(m, "level:\t\tunreadable\n");
7098 seq_printf(m, "level:\t\t%d\n", level);
7099 seq_printf(m, "commands:\tup, down\n");
7100 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7107 static int brightness_write(char *buf)
7113 level = brightness_get(NULL);
7117 while ((cmd = strsep(&buf, ","))) {
7118 if (strlencmp(cmd, "up") == 0) {
7119 if (level < bright_maxlvl)
7121 } else if (strlencmp(cmd, "down") == 0) {
7124 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7125 level >= 0 && level <= bright_maxlvl) {
7131 tpacpi_disclose_usertask("procfs brightness",
7132 "set level to %d\n", level);
7135 * Now we know what the final level should be, so we try to set it.
7136 * Doing it this way makes the syscall restartable in case of EINTR
7138 rc = brightness_set(level);
7139 if (!rc && ibm_backlight_device)
7140 backlight_force_update(ibm_backlight_device,
7141 BACKLIGHT_UPDATE_SYSFS);
7142 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7145 static struct ibm_struct brightness_driver_data = {
7146 .name = "brightness",
7147 .read = brightness_read,
7148 .write = brightness_write,
7149 .exit = brightness_exit,
7150 .suspend = brightness_suspend,
7151 .shutdown = brightness_shutdown,
7154 /*************************************************************************
7159 * IBM ThinkPads have a simple volume controller with MUTE gating.
7160 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7162 * Since the *61 series (and probably also the later *60 series), Lenovo
7163 * ThinkPads only implement the MUTE gate.
7166 * Bit 6: MUTE (1 mutes sound)
7168 * Other bits should be zero as far as we know.
7170 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7171 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7172 * such as bit 7 which is used to detect repeated presses of MUTE,
7173 * and we leave them unchanged.
7175 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7176 * in response to user input. Unfortunately, this rarely works well.
7177 * The laptop changes the state of its internal MUTE gate and, on some
7178 * models, sends KEY_MUTE, causing any user code that responds to the
7179 * mute button to get confused. The hardware MUTE gate is also
7180 * unnecessary, since user code can handle the mute button without
7181 * kernel or EC help.
7183 * To avoid confusing userspace, we simply disable all EC-based mute
7184 * and volume controls when possible.
7187 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7189 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7190 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7191 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7193 #if SNDRV_CARDS <= 32
7194 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7196 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7198 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7199 static char *alsa_id = "ThinkPadEC";
7200 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7202 struct tpacpi_alsa_data {
7203 struct snd_card *card;
7204 struct snd_ctl_elem_id *ctl_mute_id;
7205 struct snd_ctl_elem_id *ctl_vol_id;
7208 static struct snd_card *alsa_card;
7213 /* TP_EC_AUDIO bits */
7214 TP_EC_AUDIO_MUTESW = 6,
7216 /* TP_EC_AUDIO bitmasks */
7217 TP_EC_AUDIO_LVL_MSK = 0x0F,
7218 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7220 /* Maximum volume */
7221 TP_EC_VOLUME_MAX = 14,
7224 enum tpacpi_volume_access_mode {
7225 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7226 TPACPI_VOL_MODE_EC, /* Pure EC control */
7227 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7228 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7232 enum tpacpi_volume_capabilities {
7233 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7234 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7235 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7239 enum tpacpi_mute_btn_mode {
7240 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7241 /* We don't know what mode 1 is. */
7242 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7243 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7246 static enum tpacpi_volume_access_mode volume_mode =
7247 TPACPI_VOL_MODE_MAX;
7249 static enum tpacpi_volume_capabilities volume_capabilities;
7250 static bool volume_control_allowed;
7251 static bool software_mute_requested = true;
7252 static bool software_mute_active;
7253 static int software_mute_orig_mode;
7256 * Used to syncronize writers to TP_EC_AUDIO and
7257 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7259 static struct mutex volume_mutex;
7261 static void tpacpi_volume_checkpoint_nvram(void)
7267 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7269 if (!volume_control_allowed)
7271 if (software_mute_active)
7274 vdbg_printk(TPACPI_DBG_MIXER,
7275 "trying to checkpoint mixer state to NVRAM...\n");
7277 if (tp_features.mixer_no_level_control)
7278 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7280 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7282 if (mutex_lock_killable(&volume_mutex) < 0)
7285 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7288 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7290 if (lec != (b_nvram & ec_mask)) {
7291 /* NVRAM needs update */
7292 b_nvram &= ~ec_mask;
7294 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7295 dbg_printk(TPACPI_DBG_MIXER,
7296 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7297 (unsigned int) lec, (unsigned int) b_nvram);
7299 vdbg_printk(TPACPI_DBG_MIXER,
7300 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7301 (unsigned int) lec, (unsigned int) b_nvram);
7305 mutex_unlock(&volume_mutex);
7308 static int volume_get_status_ec(u8 *status)
7312 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7317 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7322 static int volume_get_status(u8 *status)
7324 return volume_get_status_ec(status);
7327 static int volume_set_status_ec(const u8 status)
7329 if (!acpi_ec_write(TP_EC_AUDIO, status))
7332 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7335 * On X200s, and possibly on others, it can take a while for
7336 * reads to become correct.
7343 static int volume_set_status(const u8 status)
7345 return volume_set_status_ec(status);
7348 /* returns < 0 on error, 0 on no change, 1 on change */
7349 static int __volume_set_mute_ec(const bool mute)
7354 if (mutex_lock_killable(&volume_mutex) < 0)
7357 rc = volume_get_status_ec(&s);
7361 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7362 s & ~TP_EC_AUDIO_MUTESW_MSK;
7365 rc = volume_set_status_ec(n);
7371 mutex_unlock(&volume_mutex);
7375 static int volume_alsa_set_mute(const bool mute)
7377 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7378 (mute) ? "" : "un");
7379 return __volume_set_mute_ec(mute);
7382 static int volume_set_mute(const bool mute)
7386 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7387 (mute) ? "" : "un");
7389 rc = __volume_set_mute_ec(mute);
7390 return (rc < 0) ? rc : 0;
7393 /* returns < 0 on error, 0 on no change, 1 on change */
7394 static int __volume_set_volume_ec(const u8 vol)
7399 if (vol > TP_EC_VOLUME_MAX)
7402 if (mutex_lock_killable(&volume_mutex) < 0)
7405 rc = volume_get_status_ec(&s);
7409 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7412 rc = volume_set_status_ec(n);
7418 mutex_unlock(&volume_mutex);
7422 static int volume_set_software_mute(bool startup)
7426 if (!tpacpi_is_lenovo())
7430 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7434 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7435 "Initial HAUM setting was %d\n",
7436 software_mute_orig_mode);
7439 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7440 (int)TP_EC_MUTE_BTN_NONE))
7443 if (result != TP_EC_MUTE_BTN_NONE)
7444 pr_warn("Unexpected SAUM result %d\n",
7448 * In software mute mode, the standard codec controls take
7449 * precendence, so we unmute the ThinkPad HW switch at
7450 * startup. Just on case there are SAUM-capable ThinkPads
7451 * with level controls, set max HW volume as well.
7453 if (tp_features.mixer_no_level_control)
7454 result = volume_set_mute(false);
7456 result = volume_set_status(TP_EC_VOLUME_MAX);
7459 pr_warn("Failed to unmute the HW mute switch\n");
7464 static void volume_exit_software_mute(void)
7468 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7469 || r != software_mute_orig_mode)
7470 pr_warn("Failed to restore mute mode\n");
7473 static int volume_alsa_set_volume(const u8 vol)
7475 dbg_printk(TPACPI_DBG_MIXER,
7476 "ALSA: trying to set volume level to %hu\n", vol);
7477 return __volume_set_volume_ec(vol);
7480 static void volume_alsa_notify_change(void)
7482 struct tpacpi_alsa_data *d;
7484 if (alsa_card && alsa_card->private_data) {
7485 d = alsa_card->private_data;
7487 snd_ctl_notify(alsa_card,
7488 SNDRV_CTL_EVENT_MASK_VALUE,
7491 snd_ctl_notify(alsa_card,
7492 SNDRV_CTL_EVENT_MASK_VALUE,
7497 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7498 struct snd_ctl_elem_info *uinfo)
7500 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7502 uinfo->value.integer.min = 0;
7503 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7507 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7508 struct snd_ctl_elem_value *ucontrol)
7513 rc = volume_get_status(&s);
7517 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7521 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7522 struct snd_ctl_elem_value *ucontrol)
7524 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7525 ucontrol->value.integer.value[0]);
7526 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7529 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7531 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7532 struct snd_ctl_elem_value *ucontrol)
7537 rc = volume_get_status(&s);
7541 ucontrol->value.integer.value[0] =
7542 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7546 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7547 struct snd_ctl_elem_value *ucontrol)
7549 tpacpi_disclose_usertask("ALSA", "%smute\n",
7550 ucontrol->value.integer.value[0] ?
7552 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7555 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7557 .name = "Console Playback Volume",
7559 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7560 .info = volume_alsa_vol_info,
7561 .get = volume_alsa_vol_get,
7564 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7566 .name = "Console Playback Switch",
7568 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7569 .info = volume_alsa_mute_info,
7570 .get = volume_alsa_mute_get,
7573 static void volume_suspend(void)
7575 tpacpi_volume_checkpoint_nvram();
7578 static void volume_resume(void)
7580 if (software_mute_active) {
7581 if (volume_set_software_mute(false) < 0)
7582 pr_warn("Failed to restore software mute\n");
7584 volume_alsa_notify_change();
7588 static void volume_shutdown(void)
7590 tpacpi_volume_checkpoint_nvram();
7593 static void volume_exit(void)
7596 snd_card_free(alsa_card);
7600 tpacpi_volume_checkpoint_nvram();
7602 if (software_mute_active)
7603 volume_exit_software_mute();
7606 static int __init volume_create_alsa_mixer(void)
7608 struct snd_card *card;
7609 struct tpacpi_alsa_data *data;
7610 struct snd_kcontrol *ctl_vol;
7611 struct snd_kcontrol *ctl_mute;
7614 rc = snd_card_new(&tpacpi_pdev->dev,
7615 alsa_index, alsa_id, THIS_MODULE,
7616 sizeof(struct tpacpi_alsa_data), &card);
7617 if (rc < 0 || !card) {
7618 pr_err("Failed to create ALSA card structures: %d\n", rc);
7622 BUG_ON(!card->private_data);
7623 data = card->private_data;
7626 strscpy(card->driver, TPACPI_ALSA_DRVNAME,
7627 sizeof(card->driver));
7628 strscpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7629 sizeof(card->shortname));
7630 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7631 (thinkpad_id.ec_version_str) ?
7632 thinkpad_id.ec_version_str : "(unknown)");
7633 snprintf(card->longname, sizeof(card->longname),
7634 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7635 (thinkpad_id.ec_version_str) ?
7636 thinkpad_id.ec_version_str : "unknown");
7638 if (volume_control_allowed) {
7639 volume_alsa_control_vol.put = volume_alsa_vol_put;
7640 volume_alsa_control_vol.access =
7641 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7643 volume_alsa_control_mute.put = volume_alsa_mute_put;
7644 volume_alsa_control_mute.access =
7645 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7648 if (!tp_features.mixer_no_level_control) {
7649 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7650 rc = snd_ctl_add(card, ctl_vol);
7652 pr_err("Failed to create ALSA volume control: %d\n",
7656 data->ctl_vol_id = &ctl_vol->id;
7659 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7660 rc = snd_ctl_add(card, ctl_mute);
7662 pr_err("Failed to create ALSA mute control: %d\n", rc);
7665 data->ctl_mute_id = &ctl_mute->id;
7667 rc = snd_card_register(card);
7669 pr_err("Failed to register ALSA card: %d\n", rc);
7677 snd_card_free(card);
7681 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7682 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7684 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7685 /* Whitelist volume level on all IBM by default */
7686 { .vendor = PCI_VENDOR_ID_IBM,
7687 .bios = TPACPI_MATCH_ANY,
7688 .ec = TPACPI_MATCH_ANY,
7689 .quirks = TPACPI_VOL_Q_LEVEL },
7691 /* Lenovo models with volume control (needs confirmation) */
7692 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7693 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7694 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7695 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7696 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7697 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7698 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7700 /* Whitelist mute-only on all Lenovo by default */
7701 { .vendor = PCI_VENDOR_ID_LENOVO,
7702 .bios = TPACPI_MATCH_ANY,
7703 .ec = TPACPI_MATCH_ANY,
7704 .quirks = TPACPI_VOL_Q_MUTEONLY }
7707 static int __init volume_init(struct ibm_init_struct *iibm)
7709 unsigned long quirks;
7712 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7714 mutex_init(&volume_mutex);
7717 * Check for module parameter bogosity, note that we
7718 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7719 * able to detect "unspecified"
7721 if (volume_mode > TPACPI_VOL_MODE_MAX)
7724 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7725 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7730 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7734 * The ALSA mixer is our primary interface.
7735 * When disabled, don't install the subdriver at all
7738 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7739 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7743 quirks = tpacpi_check_quirks(volume_quirk_table,
7744 ARRAY_SIZE(volume_quirk_table));
7746 switch (volume_capabilities) {
7747 case TPACPI_VOL_CAP_AUTO:
7748 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7749 tp_features.mixer_no_level_control = 1;
7750 else if (quirks & TPACPI_VOL_Q_LEVEL)
7751 tp_features.mixer_no_level_control = 0;
7753 return -ENODEV; /* no mixer */
7755 case TPACPI_VOL_CAP_VOLMUTE:
7756 tp_features.mixer_no_level_control = 0;
7758 case TPACPI_VOL_CAP_MUTEONLY:
7759 tp_features.mixer_no_level_control = 1;
7765 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7766 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7767 "using user-supplied volume_capabilities=%d\n",
7768 volume_capabilities);
7770 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7771 volume_mode == TPACPI_VOL_MODE_MAX) {
7772 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7774 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7775 "driver auto-selected volume_mode=%d\n",
7778 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7779 "using user-supplied volume_mode=%d\n",
7783 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7784 "mute is supported, volume control is %s\n",
7785 str_supported(!tp_features.mixer_no_level_control));
7787 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7788 software_mute_active = true;
7790 rc = volume_create_alsa_mixer();
7792 pr_err("Could not create the ALSA mixer interface\n");
7796 pr_info("Console audio control enabled, mode: %s\n",
7797 (volume_control_allowed) ?
7798 "override (read/write)" :
7799 "monitor (read only)");
7802 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7803 "registering volume hotkeys as change notification\n");
7804 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7805 | TP_ACPI_HKEY_VOLUP_MASK
7806 | TP_ACPI_HKEY_VOLDWN_MASK
7807 | TP_ACPI_HKEY_MUTE_MASK);
7812 static int volume_read(struct seq_file *m)
7816 if (volume_get_status(&status) < 0) {
7817 seq_printf(m, "level:\t\tunreadable\n");
7819 if (tp_features.mixer_no_level_control)
7820 seq_printf(m, "level:\t\tunsupported\n");
7822 seq_printf(m, "level:\t\t%d\n",
7823 status & TP_EC_AUDIO_LVL_MSK);
7825 seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
7827 if (volume_control_allowed) {
7828 seq_printf(m, "commands:\tunmute, mute\n");
7829 if (!tp_features.mixer_no_level_control) {
7830 seq_printf(m, "commands:\tup, down\n");
7831 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7840 static int volume_write(char *buf)
7843 u8 new_level, new_mute;
7849 * We do allow volume control at driver startup, so that the
7850 * user can set initial state through the volume=... parameter hack.
7852 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7853 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7854 tp_warned.volume_ctrl_forbidden = 1;
7855 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7856 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7861 rc = volume_get_status(&s);
7865 new_level = s & TP_EC_AUDIO_LVL_MSK;
7866 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7868 while ((cmd = strsep(&buf, ","))) {
7869 if (!tp_features.mixer_no_level_control) {
7870 if (strlencmp(cmd, "up") == 0) {
7873 else if (new_level < TP_EC_VOLUME_MAX)
7876 } else if (strlencmp(cmd, "down") == 0) {
7879 else if (new_level > 0)
7882 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7883 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7888 if (strlencmp(cmd, "mute") == 0)
7889 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7890 else if (strlencmp(cmd, "unmute") == 0)
7896 if (tp_features.mixer_no_level_control) {
7897 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7898 new_mute ? "" : "un");
7899 rc = volume_set_mute(!!new_mute);
7901 tpacpi_disclose_usertask("procfs volume",
7902 "%smute and set level to %d\n",
7903 new_mute ? "" : "un", new_level);
7904 rc = volume_set_status(new_mute | new_level);
7906 volume_alsa_notify_change();
7908 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7911 static struct ibm_struct volume_driver_data = {
7913 .read = volume_read,
7914 .write = volume_write,
7915 .exit = volume_exit,
7916 .suspend = volume_suspend,
7917 .resume = volume_resume,
7918 .shutdown = volume_shutdown,
7921 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7923 #define alsa_card NULL
7925 static inline void volume_alsa_notify_change(void)
7929 static int __init volume_init(struct ibm_init_struct *iibm)
7931 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7936 static struct ibm_struct volume_driver_data = {
7940 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7942 /*************************************************************************
7949 * TPACPI_FAN_RD_ACPI_GFAN:
7950 * ACPI GFAN method: returns fan level
7952 * see TPACPI_FAN_WR_ACPI_SFAN
7953 * EC 0x2f (HFSP) not available if GFAN exists
7955 * TPACPI_FAN_WR_ACPI_SFAN:
7956 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7958 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7961 * TPACPI_FAN_WR_TPEC:
7962 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7963 * Supported on almost all ThinkPads
7965 * Fan speed changes of any sort (including those caused by the
7966 * disengaged mode) are usually done slowly by the firmware as the
7967 * maximum amount of fan duty cycle change per second seems to be
7970 * Reading is not available if GFAN exists.
7971 * Writing is not available if SFAN exists.
7974 * 7 automatic mode engaged;
7975 * (default operation mode of the ThinkPad)
7976 * fan level is ignored in this mode.
7977 * 6 full speed mode (takes precedence over bit 7);
7978 * not available on all thinkpads. May disable
7979 * the tachometer while the fan controller ramps up
7980 * the speed (which can take up to a few *minutes*).
7981 * Speeds up fan to 100% duty-cycle, which is far above
7982 * the standard RPM levels. It is not impossible that
7983 * it could cause hardware damage.
7984 * 5-3 unused in some models. Extra bits for fan level
7985 * in others, but still useless as all values above
7986 * 7 map to the same speed as level 7 in these models.
7987 * 2-0 fan level (0..7 usually)
7989 * 0x07 = max (set when temperatures critical)
7990 * Some ThinkPads may have other levels, see
7991 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7993 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7994 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7995 * does so, its initial value is meaningless (0x07).
7997 * For firmware bugs, refer to:
7998 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8002 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8003 * Main fan tachometer reading (in RPM)
8005 * This register is present on all ThinkPads with a new-style EC, and
8006 * it is known not to be present on the A21m/e, and T22, as there is
8007 * something else in offset 0x84 according to the ACPI DSDT. Other
8008 * ThinkPads from this same time period (and earlier) probably lack the
8009 * tachometer as well.
8011 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8012 * was never fixed by IBM to report the EC firmware version string
8013 * probably support the tachometer (like the early X models), so
8014 * detecting it is quite hard. We need more data to know for sure.
8016 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8019 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8022 * For firmware bugs, refer to:
8023 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8027 * ThinkPad EC register 0x31 bit 0 (only on select models)
8029 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8030 * show the speed of the main fan. When bit 0 of EC register 0x31
8031 * is one, the tachometer registers show the speed of the auxiliary
8034 * Fan control seems to affect both fans, regardless of the state
8037 * So far, only the firmware for the X60/X61 non-tablet versions
8038 * seem to support this (firmware TP-7M).
8040 * TPACPI_FAN_WR_ACPI_FANS:
8041 * ThinkPad X31, X40, X41. Not available in the X60.
8043 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8044 * high speed. ACPI DSDT seems to map these three speeds to levels
8045 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8046 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8048 * The speeds are stored on handles
8049 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8051 * There are three default speed sets, accessible as handles:
8052 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8054 * ACPI DSDT switches which set is in use depending on various
8057 * TPACPI_FAN_WR_TPEC is also available and should be used to
8058 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8059 * but the ACPI tables just mention level 7.
8062 enum { /* Fan control constants */
8063 fan_status_offset = 0x2f, /* EC register 0x2f */
8064 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8065 * 0x84 must be read before 0x85 */
8066 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8067 bit 0 selects which fan is active */
8069 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8070 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8072 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8075 enum fan_status_access_mode {
8076 TPACPI_FAN_NONE = 0, /* No fan status or control */
8077 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8078 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8081 enum fan_control_access_mode {
8082 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8083 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8084 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8085 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8088 enum fan_control_commands {
8089 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8090 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8091 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8092 * and also watchdog cmd */
8095 static bool fan_control_allowed;
8097 static enum fan_status_access_mode fan_status_access_mode;
8098 static enum fan_control_access_mode fan_control_access_mode;
8099 static enum fan_control_commands fan_control_commands;
8101 static u8 fan_control_initial_status;
8102 static u8 fan_control_desired_level;
8103 static u8 fan_control_resume_level;
8104 static int fan_watchdog_maxinterval;
8106 static struct mutex fan_mutex;
8108 static void fan_watchdog_fire(struct work_struct *ignored);
8109 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8111 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8112 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8113 "\\FSPD", /* 600e/x, 770e, 770x */
8115 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8116 "JFNS", /* 770x-JL */
8120 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8121 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8122 * be in auto mode (0x80).
8124 * This is corrected by any write to HFSP either by the driver, or
8127 * We assume 0x07 really means auto mode while this quirk is active,
8128 * as this is far more likely than the ThinkPad being in level 7,
8129 * which is only used by the firmware during thermal emergencies.
8131 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8132 * TP-70 (T43, R52), which are known to be buggy.
8135 static void fan_quirk1_setup(void)
8137 if (fan_control_initial_status == 0x07) {
8138 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8139 tp_features.fan_ctrl_status_undef = 1;
8143 static void fan_quirk1_handle(u8 *fan_status)
8145 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8146 if (*fan_status != fan_control_initial_status) {
8147 /* something changed the HFSP regisnter since
8148 * driver init time, so it is not undefined
8150 tp_features.fan_ctrl_status_undef = 0;
8152 /* Return most likely status. In fact, it
8153 * might be the only possible status */
8154 *fan_status = TP_EC_FAN_AUTO;
8159 /* Select main fan on X60/X61, NOOP on others */
8160 static bool fan_select_fan1(void)
8162 if (tp_features.second_fan) {
8165 if (ec_read(fan_select_offset, &val) < 0)
8168 if (ec_write(fan_select_offset, val) < 0)
8174 /* Select secondary fan on X60/X61 */
8175 static bool fan_select_fan2(void)
8179 if (!tp_features.second_fan)
8182 if (ec_read(fan_select_offset, &val) < 0)
8185 if (ec_write(fan_select_offset, val) < 0)
8192 * Call with fan_mutex held
8194 static void fan_update_desired_level(u8 status)
8197 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8199 fan_control_desired_level = 7;
8201 fan_control_desired_level = status;
8205 static int fan_get_status(u8 *status)
8210 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8212 switch (fan_status_access_mode) {
8213 case TPACPI_FAN_RD_ACPI_GFAN: {
8214 /* 570, 600e/x, 770e, 770x */
8217 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8221 *status = res & 0x07;
8225 case TPACPI_FAN_RD_TPEC:
8226 /* all except 570, 600e/x, 770e, 770x */
8227 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8230 if (likely(status)) {
8232 fan_quirk1_handle(status);
8244 static int fan_get_status_safe(u8 *status)
8249 if (mutex_lock_killable(&fan_mutex))
8250 return -ERESTARTSYS;
8251 rc = fan_get_status(&s);
8253 fan_update_desired_level(s);
8254 mutex_unlock(&fan_mutex);
8264 static int fan_get_speed(unsigned int *speed)
8268 switch (fan_status_access_mode) {
8269 case TPACPI_FAN_RD_TPEC:
8270 /* all except 570, 600e/x, 770e, 770x */
8271 if (unlikely(!fan_select_fan1()))
8273 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8274 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8278 *speed = (hi << 8) | lo;
8289 static int fan2_get_speed(unsigned int *speed)
8294 switch (fan_status_access_mode) {
8295 case TPACPI_FAN_RD_TPEC:
8296 /* all except 570, 600e/x, 770e, 770x */
8297 if (unlikely(!fan_select_fan2()))
8299 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8300 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8301 fan_select_fan1(); /* play it safe */
8306 *speed = (hi << 8) | lo;
8317 static int fan_set_level(int level)
8319 if (!fan_control_allowed)
8322 switch (fan_control_access_mode) {
8323 case TPACPI_FAN_WR_ACPI_SFAN:
8324 if ((level < 0) || (level > 7))
8327 if (tp_features.second_fan_ctl) {
8328 if (!fan_select_fan2() ||
8329 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8330 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8331 tp_features.second_fan_ctl = 0;
8335 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8339 case TPACPI_FAN_WR_ACPI_FANS:
8340 case TPACPI_FAN_WR_TPEC:
8341 if (!(level & TP_EC_FAN_AUTO) &&
8342 !(level & TP_EC_FAN_FULLSPEED) &&
8343 ((level < 0) || (level > 7)))
8346 /* safety net should the EC not support AUTO
8347 * or FULLSPEED mode bits and just ignore them */
8348 if (level & TP_EC_FAN_FULLSPEED)
8349 level |= 7; /* safety min speed 7 */
8350 else if (level & TP_EC_FAN_AUTO)
8351 level |= 4; /* safety min speed 4 */
8353 if (tp_features.second_fan_ctl) {
8354 if (!fan_select_fan2() ||
8355 !acpi_ec_write(fan_status_offset, level)) {
8356 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8357 tp_features.second_fan_ctl = 0;
8362 if (!acpi_ec_write(fan_status_offset, level))
8365 tp_features.fan_ctrl_status_undef = 0;
8372 vdbg_printk(TPACPI_DBG_FAN,
8373 "fan control: set fan control register to 0x%02x\n", level);
8377 static int fan_set_level_safe(int level)
8381 if (!fan_control_allowed)
8384 if (mutex_lock_killable(&fan_mutex))
8385 return -ERESTARTSYS;
8387 if (level == TPACPI_FAN_LAST_LEVEL)
8388 level = fan_control_desired_level;
8390 rc = fan_set_level(level);
8392 fan_update_desired_level(level);
8394 mutex_unlock(&fan_mutex);
8398 static int fan_set_enable(void)
8403 if (!fan_control_allowed)
8406 if (mutex_lock_killable(&fan_mutex))
8407 return -ERESTARTSYS;
8409 switch (fan_control_access_mode) {
8410 case TPACPI_FAN_WR_ACPI_FANS:
8411 case TPACPI_FAN_WR_TPEC:
8412 rc = fan_get_status(&s);
8416 /* Don't go out of emergency fan mode */
8419 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8422 if (!acpi_ec_write(fan_status_offset, s))
8425 tp_features.fan_ctrl_status_undef = 0;
8430 case TPACPI_FAN_WR_ACPI_SFAN:
8431 rc = fan_get_status(&s);
8437 /* Set fan to at least level 4 */
8440 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8450 mutex_unlock(&fan_mutex);
8453 vdbg_printk(TPACPI_DBG_FAN,
8454 "fan control: set fan control register to 0x%02x\n",
8459 static int fan_set_disable(void)
8463 if (!fan_control_allowed)
8466 if (mutex_lock_killable(&fan_mutex))
8467 return -ERESTARTSYS;
8470 switch (fan_control_access_mode) {
8471 case TPACPI_FAN_WR_ACPI_FANS:
8472 case TPACPI_FAN_WR_TPEC:
8473 if (!acpi_ec_write(fan_status_offset, 0x00))
8476 fan_control_desired_level = 0;
8477 tp_features.fan_ctrl_status_undef = 0;
8481 case TPACPI_FAN_WR_ACPI_SFAN:
8482 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8485 fan_control_desired_level = 0;
8493 vdbg_printk(TPACPI_DBG_FAN,
8494 "fan control: set fan control register to 0\n");
8496 mutex_unlock(&fan_mutex);
8500 static int fan_set_speed(int speed)
8504 if (!fan_control_allowed)
8507 if (mutex_lock_killable(&fan_mutex))
8508 return -ERESTARTSYS;
8511 switch (fan_control_access_mode) {
8512 case TPACPI_FAN_WR_ACPI_FANS:
8513 if (speed >= 0 && speed <= 65535) {
8514 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8515 speed, speed, speed))
8525 mutex_unlock(&fan_mutex);
8529 static void fan_watchdog_reset(void)
8531 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8534 if (fan_watchdog_maxinterval > 0 &&
8535 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8536 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8537 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8539 cancel_delayed_work(&fan_watchdog_task);
8542 static void fan_watchdog_fire(struct work_struct *ignored)
8546 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8549 pr_notice("fan watchdog: enabling fan\n");
8550 rc = fan_set_enable();
8552 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8554 /* reschedule for later */
8555 fan_watchdog_reset();
8560 * SYSFS fan layout: hwmon compatible (device)
8563 * 0: "disengaged" mode
8565 * 2: native EC "auto" mode (recommended, hardware default)
8567 * pwm*: set speed in manual mode, ignored otherwise.
8568 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8571 * fan*_input: tachometer reading, RPM
8574 * SYSFS fan layout: extensions
8576 * fan_watchdog (driver):
8577 * fan watchdog interval in seconds, 0 disables (default), max 120
8580 /* sysfs fan pwm1_enable ----------------------------------------------- */
8581 static ssize_t fan_pwm1_enable_show(struct device *dev,
8582 struct device_attribute *attr,
8588 res = fan_get_status_safe(&status);
8592 if (status & TP_EC_FAN_FULLSPEED) {
8594 } else if (status & TP_EC_FAN_AUTO) {
8599 return sysfs_emit(buf, "%d\n", mode);
8602 static ssize_t fan_pwm1_enable_store(struct device *dev,
8603 struct device_attribute *attr,
8604 const char *buf, size_t count)
8609 if (parse_strtoul(buf, 2, &t))
8612 tpacpi_disclose_usertask("hwmon pwm1_enable",
8613 "set fan mode to %lu\n", t);
8617 level = TP_EC_FAN_FULLSPEED;
8620 level = TPACPI_FAN_LAST_LEVEL;
8623 level = TP_EC_FAN_AUTO;
8626 /* reserved for software-controlled auto mode */
8632 res = fan_set_level_safe(level);
8638 fan_watchdog_reset();
8643 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8644 fan_pwm1_enable_show, fan_pwm1_enable_store);
8646 /* sysfs fan pwm1 ------------------------------------------------------ */
8647 static ssize_t fan_pwm1_show(struct device *dev,
8648 struct device_attribute *attr,
8654 res = fan_get_status_safe(&status);
8659 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8660 status = fan_control_desired_level;
8665 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8668 static ssize_t fan_pwm1_store(struct device *dev,
8669 struct device_attribute *attr,
8670 const char *buf, size_t count)
8674 u8 status, newlevel;
8676 if (parse_strtoul(buf, 255, &s))
8679 tpacpi_disclose_usertask("hwmon pwm1",
8680 "set fan speed to %lu\n", s);
8682 /* scale down from 0-255 to 0-7 */
8683 newlevel = (s >> 5) & 0x07;
8685 if (mutex_lock_killable(&fan_mutex))
8686 return -ERESTARTSYS;
8688 rc = fan_get_status(&status);
8689 if (!rc && (status &
8690 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8691 rc = fan_set_level(newlevel);
8695 fan_update_desired_level(newlevel);
8696 fan_watchdog_reset();
8700 mutex_unlock(&fan_mutex);
8701 return (rc) ? rc : count;
8704 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8706 /* sysfs fan fan1_input ------------------------------------------------ */
8707 static ssize_t fan_fan1_input_show(struct device *dev,
8708 struct device_attribute *attr,
8714 res = fan_get_speed(&speed);
8718 return sysfs_emit(buf, "%u\n", speed);
8721 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8723 /* sysfs fan fan2_input ------------------------------------------------ */
8724 static ssize_t fan_fan2_input_show(struct device *dev,
8725 struct device_attribute *attr,
8731 res = fan2_get_speed(&speed);
8735 return sysfs_emit(buf, "%u\n", speed);
8738 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8740 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8741 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8743 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8746 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8751 if (parse_strtoul(buf, 120, &t))
8754 if (!fan_control_allowed)
8757 fan_watchdog_maxinterval = t;
8758 fan_watchdog_reset();
8760 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8764 static DRIVER_ATTR_RW(fan_watchdog);
8766 /* --------------------------------------------------------------------- */
8768 static struct attribute *fan_attributes[] = {
8769 &dev_attr_pwm1_enable.attr,
8770 &dev_attr_pwm1.attr,
8771 &dev_attr_fan1_input.attr,
8772 &dev_attr_fan2_input.attr,
8776 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8779 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8780 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8783 if (attr == &dev_attr_fan2_input.attr) {
8784 if (!tp_features.second_fan)
8791 static const struct attribute_group fan_attr_group = {
8792 .is_visible = fan_attr_is_visible,
8793 .attrs = fan_attributes,
8796 static struct attribute *fan_driver_attributes[] = {
8797 &driver_attr_fan_watchdog.attr,
8801 static const struct attribute_group fan_driver_attr_group = {
8802 .is_visible = fan_attr_is_visible,
8803 .attrs = fan_driver_attributes,
8806 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8807 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8808 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8809 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8811 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8812 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8813 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8814 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8815 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8816 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8817 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8818 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8819 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8820 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8821 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8822 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8823 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8824 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8825 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8826 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8827 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8828 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8831 static int __init fan_init(struct ibm_init_struct *iibm)
8833 unsigned long quirks;
8835 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8836 "initializing fan subdriver\n");
8838 mutex_init(&fan_mutex);
8839 fan_status_access_mode = TPACPI_FAN_NONE;
8840 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8841 fan_control_commands = 0;
8842 fan_watchdog_maxinterval = 0;
8843 tp_features.fan_ctrl_status_undef = 0;
8844 tp_features.second_fan = 0;
8845 tp_features.second_fan_ctl = 0;
8846 fan_control_desired_level = 7;
8848 if (tpacpi_is_ibm()) {
8849 TPACPI_ACPIHANDLE_INIT(fans);
8850 TPACPI_ACPIHANDLE_INIT(gfan);
8851 TPACPI_ACPIHANDLE_INIT(sfan);
8854 quirks = tpacpi_check_quirks(fan_quirk_table,
8855 ARRAY_SIZE(fan_quirk_table));
8857 if (quirks & TPACPI_FAN_NOFAN) {
8858 pr_info("No integrated ThinkPad fan available\n");
8863 /* 570, 600e/x, 770e, 770x */
8864 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8866 /* all other ThinkPads: note that even old-style
8867 * ThinkPad ECs supports the fan control register */
8868 if (likely(acpi_ec_read(fan_status_offset,
8869 &fan_control_initial_status))) {
8873 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8874 if (quirks & TPACPI_FAN_Q1)
8876 /* Try and probe the 2nd fan */
8877 tp_features.second_fan = 1; /* needed for get_speed to work */
8878 res = fan2_get_speed(&speed);
8880 /* It responded - so let's assume it's there */
8881 tp_features.second_fan = 1;
8882 tp_features.second_fan_ctl = 1;
8883 pr_info("secondary fan control detected & enabled\n");
8885 /* Fan not auto-detected */
8886 tp_features.second_fan = 0;
8887 if (quirks & TPACPI_FAN_2FAN) {
8888 tp_features.second_fan = 1;
8889 pr_info("secondary fan support enabled\n");
8891 if (quirks & TPACPI_FAN_2CTL) {
8892 tp_features.second_fan = 1;
8893 tp_features.second_fan_ctl = 1;
8894 pr_info("secondary fan control enabled\n");
8898 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8905 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8906 fan_control_commands |=
8907 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8910 /* gfan without sfan means no fan control */
8911 /* all other models implement TP EC 0x2f control */
8915 fan_control_access_mode =
8916 TPACPI_FAN_WR_ACPI_FANS;
8917 fan_control_commands |=
8918 TPACPI_FAN_CMD_SPEED |
8919 TPACPI_FAN_CMD_LEVEL |
8920 TPACPI_FAN_CMD_ENABLE;
8922 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8923 fan_control_commands |=
8924 TPACPI_FAN_CMD_LEVEL |
8925 TPACPI_FAN_CMD_ENABLE;
8930 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8931 "fan is %s, modes %d, %d\n",
8932 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8933 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8934 fan_status_access_mode, fan_control_access_mode);
8936 /* fan control master switch */
8937 if (!fan_control_allowed) {
8938 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8939 fan_control_commands = 0;
8940 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8941 "fan control features disabled by parameter\n");
8944 /* update fan_control_desired_level */
8945 if (fan_status_access_mode != TPACPI_FAN_NONE)
8946 fan_get_status_safe(NULL);
8948 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8949 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8955 static void fan_exit(void)
8957 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8958 "cancelling any pending fan watchdog tasks\n");
8960 cancel_delayed_work(&fan_watchdog_task);
8961 flush_workqueue(tpacpi_wq);
8964 static void fan_suspend(void)
8968 if (!fan_control_allowed)
8971 /* Store fan status in cache */
8972 fan_control_resume_level = 0;
8973 rc = fan_get_status_safe(&fan_control_resume_level);
8975 pr_notice("failed to read fan level for later restore during resume: %d\n",
8978 /* if it is undefined, don't attempt to restore it.
8980 if (tp_features.fan_ctrl_status_undef)
8981 fan_control_resume_level = 0;
8984 static void fan_resume(void)
8986 u8 current_level = 7;
8987 bool do_set = false;
8990 /* DSDT *always* updates status on resume */
8991 tp_features.fan_ctrl_status_undef = 0;
8993 if (!fan_control_allowed ||
8994 !fan_control_resume_level ||
8995 fan_get_status_safe(¤t_level))
8998 switch (fan_control_access_mode) {
8999 case TPACPI_FAN_WR_ACPI_SFAN:
9000 /* never decrease fan level */
9001 do_set = (fan_control_resume_level > current_level);
9003 case TPACPI_FAN_WR_ACPI_FANS:
9004 case TPACPI_FAN_WR_TPEC:
9005 /* never decrease fan level, scale is:
9006 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
9008 * We expect the firmware to set either 7 or AUTO, but we
9009 * handle FULLSPEED out of paranoia.
9011 * So, we can safely only restore FULLSPEED or 7, anything
9012 * else could slow the fan. Restoring AUTO is useless, at
9013 * best that's exactly what the DSDT already set (it is the
9016 * Always keep in mind that the DSDT *will* have set the
9017 * fans to what the vendor supposes is the best level. We
9018 * muck with it only to speed the fan up.
9020 if (fan_control_resume_level != 7 &&
9021 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
9024 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
9025 (current_level != fan_control_resume_level);
9031 pr_notice("restoring fan level to 0x%02x\n",
9032 fan_control_resume_level);
9033 rc = fan_set_level_safe(fan_control_resume_level);
9035 pr_notice("failed to restore fan level: %d\n", rc);
9039 static int fan_read(struct seq_file *m)
9043 unsigned int speed = 0;
9045 switch (fan_status_access_mode) {
9046 case TPACPI_FAN_RD_ACPI_GFAN:
9047 /* 570, 600e/x, 770e, 770x */
9048 rc = fan_get_status_safe(&status);
9052 seq_printf(m, "status:\t\t%s\n"
9054 str_enabled_disabled(status), status);
9057 case TPACPI_FAN_RD_TPEC:
9058 /* all except 570, 600e/x, 770e, 770x */
9059 rc = fan_get_status_safe(&status);
9063 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
9065 rc = fan_get_speed(&speed);
9069 seq_printf(m, "speed:\t\t%d\n", speed);
9071 if (status & TP_EC_FAN_FULLSPEED)
9072 /* Disengaged mode takes precedence */
9073 seq_printf(m, "level:\t\tdisengaged\n");
9074 else if (status & TP_EC_FAN_AUTO)
9075 seq_printf(m, "level:\t\tauto\n");
9077 seq_printf(m, "level:\t\t%d\n", status);
9080 case TPACPI_FAN_NONE:
9082 seq_printf(m, "status:\t\tnot supported\n");
9085 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9086 seq_printf(m, "commands:\tlevel <level>");
9088 switch (fan_control_access_mode) {
9089 case TPACPI_FAN_WR_ACPI_SFAN:
9090 seq_printf(m, " (<level> is 0-7)\n");
9094 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9099 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9100 seq_printf(m, "commands:\tenable, disable\n"
9101 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9103 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9104 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9109 static int fan_write_cmd_level(const char *cmd, int *rc)
9113 if (strlencmp(cmd, "level auto") == 0)
9114 level = TP_EC_FAN_AUTO;
9115 else if ((strlencmp(cmd, "level disengaged") == 0) ||
9116 (strlencmp(cmd, "level full-speed") == 0))
9117 level = TP_EC_FAN_FULLSPEED;
9118 else if (sscanf(cmd, "level %d", &level) != 1)
9121 *rc = fan_set_level_safe(level);
9123 pr_err("level command accepted for unsupported access mode %d\n",
9124 fan_control_access_mode);
9126 tpacpi_disclose_usertask("procfs fan",
9127 "set level to %d\n", level);
9132 static int fan_write_cmd_enable(const char *cmd, int *rc)
9134 if (strlencmp(cmd, "enable") != 0)
9137 *rc = fan_set_enable();
9139 pr_err("enable command accepted for unsupported access mode %d\n",
9140 fan_control_access_mode);
9142 tpacpi_disclose_usertask("procfs fan", "enable\n");
9147 static int fan_write_cmd_disable(const char *cmd, int *rc)
9149 if (strlencmp(cmd, "disable") != 0)
9152 *rc = fan_set_disable();
9154 pr_err("disable command accepted for unsupported access mode %d\n",
9155 fan_control_access_mode);
9157 tpacpi_disclose_usertask("procfs fan", "disable\n");
9162 static int fan_write_cmd_speed(const char *cmd, int *rc)
9167 * Support speed <low> <medium> <high> ? */
9169 if (sscanf(cmd, "speed %d", &speed) != 1)
9172 *rc = fan_set_speed(speed);
9174 pr_err("speed command accepted for unsupported access mode %d\n",
9175 fan_control_access_mode);
9177 tpacpi_disclose_usertask("procfs fan",
9178 "set speed to %d\n", speed);
9183 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9187 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9190 if (interval < 0 || interval > 120)
9193 fan_watchdog_maxinterval = interval;
9194 tpacpi_disclose_usertask("procfs fan",
9195 "set watchdog timer to %d\n",
9202 static int fan_write(char *buf)
9207 while (!rc && (cmd = strsep(&buf, ","))) {
9208 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9209 fan_write_cmd_level(cmd, &rc)) &&
9210 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9211 (fan_write_cmd_enable(cmd, &rc) ||
9212 fan_write_cmd_disable(cmd, &rc) ||
9213 fan_write_cmd_watchdog(cmd, &rc))) &&
9214 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9215 fan_write_cmd_speed(cmd, &rc))
9219 fan_watchdog_reset();
9225 static struct ibm_struct fan_driver_data = {
9230 .suspend = fan_suspend,
9231 .resume = fan_resume,
9234 /*************************************************************************
9235 * Mute LED subdriver
9238 #define TPACPI_LED_MAX 2
9240 struct tp_led_table {
9247 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9248 [LED_AUDIO_MUTE] = {
9253 [LED_AUDIO_MICMUTE] = {
9260 static int mute_led_on_off(struct tp_led_table *t, bool state)
9265 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9266 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9270 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9271 state ? t->on_value : t->off_value))
9278 static int tpacpi_led_set(int whichled, bool on)
9280 struct tp_led_table *t;
9282 t = &led_tables[whichled];
9283 if (t->state < 0 || t->state == on)
9285 return mute_led_on_off(t, on);
9288 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9289 enum led_brightness brightness)
9291 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9294 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9295 enum led_brightness brightness)
9297 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9300 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9301 [LED_AUDIO_MUTE] = {
9302 .name = "platform::mute",
9303 .max_brightness = 1,
9304 .brightness_set_blocking = tpacpi_led_mute_set,
9305 .default_trigger = "audio-mute",
9307 [LED_AUDIO_MICMUTE] = {
9308 .name = "platform::micmute",
9309 .max_brightness = 1,
9310 .brightness_set_blocking = tpacpi_led_micmute_set,
9311 .default_trigger = "audio-micmute",
9315 static int mute_led_init(struct ibm_init_struct *iibm)
9320 for (i = 0; i < TPACPI_LED_MAX; i++) {
9321 struct tp_led_table *t = &led_tables[i];
9322 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9327 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9328 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9331 led_classdev_unregister(&mute_led_cdev[i]);
9338 static void mute_led_exit(void)
9342 for (i = 0; i < TPACPI_LED_MAX; i++) {
9343 led_classdev_unregister(&mute_led_cdev[i]);
9344 tpacpi_led_set(i, false);
9348 static void mute_led_resume(void)
9352 for (i = 0; i < TPACPI_LED_MAX; i++) {
9353 struct tp_led_table *t = &led_tables[i];
9355 mute_led_on_off(t, t->state);
9359 static struct ibm_struct mute_led_driver_data = {
9361 .exit = mute_led_exit,
9362 .resume = mute_led_resume,
9366 * Battery Wear Control Driver
9372 #define GET_START "BCTG"
9373 #define SET_START "BCCS"
9374 #define GET_STOP "BCSG"
9375 #define SET_STOP "BCSS"
9376 #define GET_DISCHARGE "BDSG"
9377 #define SET_DISCHARGE "BDSS"
9378 #define GET_INHIBIT "BICG"
9379 #define SET_INHIBIT "BICS"
9388 /* Error condition bit */
9389 METHOD_ERR = BIT(31),
9393 /* This is used in the get/set helpers */
9400 struct tpacpi_battery_data {
9405 unsigned int charge_behaviours;
9408 struct tpacpi_battery_driver_data {
9409 struct tpacpi_battery_data batteries[3];
9410 int individual_addressing;
9413 static struct tpacpi_battery_driver_data battery_info;
9415 /* ACPI helpers/functions/probes */
9418 * This evaluates a ACPI method call specific to the battery
9419 * ACPI extension. The specifics are that an error is marked
9420 * in the 32rd bit of the response, so we just check that here.
9422 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9426 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9427 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9430 if (response & METHOD_ERR) {
9431 acpi_handle_err(hkey_handle,
9432 "%s evaluated but flagged as error", method);
9439 static int tpacpi_battery_get(int what, int battery, int *ret)
9442 case THRESHOLD_START:
9443 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9446 /* The value is in the low 8 bits of the response */
9449 case THRESHOLD_STOP:
9450 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9452 /* Value is in lower 8 bits */
9455 * On the stop value, if we return 0 that
9456 * does not make any sense. 0 means Default, which
9457 * means that charging stops at 100%, so we return
9463 case FORCE_DISCHARGE:
9464 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9466 /* The force discharge status is in bit 0 */
9469 case INHIBIT_CHARGE:
9470 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9472 /* The inhibit charge status is in bit 0 */
9476 pr_crit("wrong parameter: %d", what);
9481 static int tpacpi_battery_set(int what, int battery, int value)
9484 /* The first 8 bits are the value of the threshold */
9486 /* The battery ID is in bits 8-9, 2 bits */
9487 param |= battery << 8;
9490 case THRESHOLD_START:
9491 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9492 pr_err("failed to set charge threshold on battery %d",
9497 case THRESHOLD_STOP:
9498 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9499 pr_err("failed to set stop threshold: %d", battery);
9503 case FORCE_DISCHARGE:
9504 /* Force discharge is in bit 0,
9505 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9506 * battery ID is in bits 8-9, 2 bits.
9508 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9509 pr_err("failed to set force discharge on %d", battery);
9513 case INHIBIT_CHARGE:
9514 /* When setting inhibit charge, we set a default value of
9515 * always breaking on AC detach and the effective time is set to
9517 * The battery ID is in bits 4-5, 2 bits,
9518 * the effective time is in bits 8-23, 2 bytes.
9519 * A time of FFFF indicates forever.
9522 param |= battery << 4;
9523 param |= 0xFFFF << 8;
9524 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9525 pr_err("failed to set inhibit charge on %d", battery);
9530 pr_crit("wrong parameter: %d", what);
9535 static int tpacpi_battery_set_validate(int what, int battery, int value)
9539 ret = tpacpi_battery_set(what, battery, value);
9543 ret = tpacpi_battery_get(what, battery, &v);
9552 ret = tpacpi_battery_get(what, battery, &v);
9562 static int tpacpi_battery_probe(int battery)
9566 memset(&battery_info.batteries[battery], 0,
9567 sizeof(battery_info.batteries[battery]));
9570 * 1) Get the current start threshold
9571 * 2) Check for support
9572 * 3) Get the current stop threshold
9573 * 4) Check for support
9574 * 5) Get the current force discharge status
9575 * 6) Check for support
9576 * 7) Get the current inhibit charge status
9577 * 8) Check for support
9579 if (acpi_has_method(hkey_handle, GET_START)) {
9580 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9581 pr_err("Error probing battery %d\n", battery);
9584 /* Individual addressing is in bit 9 */
9586 battery_info.individual_addressing = true;
9587 /* Support is marked in bit 8 */
9589 battery_info.batteries[battery].start_support = 1;
9592 if (tpacpi_battery_get(THRESHOLD_START, battery,
9593 &battery_info.batteries[battery].charge_start)) {
9594 pr_err("Error probing battery %d\n", battery);
9598 if (acpi_has_method(hkey_handle, GET_STOP)) {
9599 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9600 pr_err("Error probing battery stop; %d\n", battery);
9603 /* Support is marked in bit 8 */
9605 battery_info.batteries[battery].stop_support = 1;
9608 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9609 &battery_info.batteries[battery].charge_stop)) {
9610 pr_err("Error probing battery stop: %d\n", battery);
9614 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9615 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9616 pr_err("Error probing battery discharge; %d\n", battery);
9619 /* Support is marked in bit 8 */
9621 battery_info.batteries[battery].charge_behaviours |=
9622 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9624 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9625 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9626 pr_err("Error probing battery inhibit charge; %d\n", battery);
9629 /* Support is marked in bit 5 */
9631 battery_info.batteries[battery].charge_behaviours |=
9632 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9635 battery_info.batteries[battery].charge_behaviours |=
9636 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9638 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9640 battery_info.batteries[battery].charge_start,
9641 battery_info.batteries[battery].charge_stop,
9642 battery_info.batteries[battery].charge_behaviours);
9647 /* General helper functions */
9649 static int tpacpi_battery_get_id(const char *battery_name)
9652 if (strcmp(battery_name, "BAT0") == 0 ||
9653 tp_features.battery_force_primary)
9655 if (strcmp(battery_name, "BAT1") == 0)
9656 return BAT_SECONDARY;
9658 * If for some reason the battery is not BAT0 nor is it
9659 * BAT1, we will assume it's the default, first battery,
9662 pr_warn("unknown battery %s, assuming primary", battery_name);
9666 /* sysfs interface */
9668 static ssize_t tpacpi_battery_store(int what,
9670 const char *buf, size_t count)
9672 struct power_supply *supply = to_power_supply(dev);
9673 unsigned long value;
9676 * Some systems have support for more than
9677 * one battery. If that is the case,
9678 * tpacpi_battery_probe marked that addressing
9679 * them individually is supported, so we do that
9680 * based on the device struct.
9682 * On systems that are not supported, we assume
9683 * the primary as most of the ACPI calls fail
9684 * with "Any Battery" as the parameter.
9686 if (battery_info.individual_addressing)
9687 /* BAT_PRIMARY or BAT_SECONDARY */
9688 battery = tpacpi_battery_get_id(supply->desc->name);
9690 battery = BAT_PRIMARY;
9692 rval = kstrtoul(buf, 10, &value);
9697 case THRESHOLD_START:
9698 if (!battery_info.batteries[battery].start_support)
9700 /* valid values are [0, 99] */
9703 if (value > battery_info.batteries[battery].charge_stop)
9705 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9707 battery_info.batteries[battery].charge_start = value;
9710 case THRESHOLD_STOP:
9711 if (!battery_info.batteries[battery].stop_support)
9713 /* valid values are [1, 100] */
9714 if (value < 1 || value > 100)
9716 if (value < battery_info.batteries[battery].charge_start)
9718 battery_info.batteries[battery].charge_stop = value;
9720 * When 100 is passed to stop, we need to flip
9721 * it to 0 as that the EC understands that as
9722 * "Default", which will charge to 100%
9726 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9730 pr_crit("Wrong parameter: %d", what);
9736 static ssize_t tpacpi_battery_show(int what,
9740 struct power_supply *supply = to_power_supply(dev);
9743 * Some systems have support for more than
9744 * one battery. If that is the case,
9745 * tpacpi_battery_probe marked that addressing
9746 * them individually is supported, so we;
9747 * based on the device struct.
9749 * On systems that are not supported, we assume
9750 * the primary as most of the ACPI calls fail
9751 * with "Any Battery" as the parameter.
9753 if (battery_info.individual_addressing)
9754 /* BAT_PRIMARY or BAT_SECONDARY */
9755 battery = tpacpi_battery_get_id(supply->desc->name);
9757 battery = BAT_PRIMARY;
9758 if (tpacpi_battery_get(what, battery, &ret))
9760 return sprintf(buf, "%d\n", ret);
9763 static ssize_t charge_control_start_threshold_show(struct device *device,
9764 struct device_attribute *attr,
9767 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9770 static ssize_t charge_control_end_threshold_show(struct device *device,
9771 struct device_attribute *attr,
9774 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9777 static ssize_t charge_behaviour_show(struct device *dev,
9778 struct device_attribute *attr,
9781 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9782 struct power_supply *supply = to_power_supply(dev);
9783 unsigned int available;
9786 battery = tpacpi_battery_get_id(supply->desc->name);
9787 available = battery_info.batteries[battery].charge_behaviours;
9789 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9790 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9793 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9798 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9799 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9802 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9808 return power_supply_charge_behaviour_show(dev, available, active, buf);
9811 static ssize_t charge_control_start_threshold_store(struct device *dev,
9812 struct device_attribute *attr,
9813 const char *buf, size_t count)
9815 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9818 static ssize_t charge_control_end_threshold_store(struct device *dev,
9819 struct device_attribute *attr,
9820 const char *buf, size_t count)
9822 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9825 static ssize_t charge_behaviour_store(struct device *dev,
9826 struct device_attribute *attr,
9827 const char *buf, size_t count)
9829 struct power_supply *supply = to_power_supply(dev);
9830 int selected, battery, ret = 0;
9831 unsigned int available;
9833 battery = tpacpi_battery_get_id(supply->desc->name);
9834 available = battery_info.batteries[battery].charge_behaviours;
9835 selected = power_supply_charge_behaviour_parse(available, buf);
9841 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9842 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9843 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9844 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9845 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9849 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9850 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9851 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9852 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9856 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9857 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9858 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9859 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9864 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9871 static DEVICE_ATTR_RW(charge_control_start_threshold);
9872 static DEVICE_ATTR_RW(charge_control_end_threshold);
9873 static DEVICE_ATTR_RW(charge_behaviour);
9874 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9875 charge_start_threshold,
9877 charge_control_start_threshold_show,
9878 charge_control_start_threshold_store
9880 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9881 charge_stop_threshold,
9883 charge_control_end_threshold_show,
9884 charge_control_end_threshold_store
9887 static struct attribute *tpacpi_battery_attrs[] = {
9888 &dev_attr_charge_control_start_threshold.attr,
9889 &dev_attr_charge_control_end_threshold.attr,
9890 &dev_attr_charge_start_threshold.attr,
9891 &dev_attr_charge_stop_threshold.attr,
9892 &dev_attr_charge_behaviour.attr,
9896 ATTRIBUTE_GROUPS(tpacpi_battery);
9898 /* ACPI battery hooking */
9900 static int tpacpi_battery_add(struct power_supply *battery)
9902 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9904 if (tpacpi_battery_probe(batteryid))
9906 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9911 static int tpacpi_battery_remove(struct power_supply *battery)
9913 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9917 static struct acpi_battery_hook battery_hook = {
9918 .add_battery = tpacpi_battery_add,
9919 .remove_battery = tpacpi_battery_remove,
9920 .name = "ThinkPad Battery Extension",
9923 /* Subdriver init/exit */
9925 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9927 * Individual addressing is broken on models that expose the
9928 * primary battery as BAT1.
9930 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9931 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9932 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9933 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9934 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9935 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9938 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9940 memset(&battery_info, 0, sizeof(battery_info));
9942 tp_features.battery_force_primary = tpacpi_check_quirks(
9943 battery_quirk_table,
9944 ARRAY_SIZE(battery_quirk_table));
9946 battery_hook_register(&battery_hook);
9950 static void tpacpi_battery_exit(void)
9952 battery_hook_unregister(&battery_hook);
9955 static struct ibm_struct battery_driver_data = {
9957 .exit = tpacpi_battery_exit,
9960 /*************************************************************************
9961 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9964 static struct drm_privacy_screen *lcdshadow_dev;
9965 static acpi_handle lcdshadow_get_handle;
9966 static acpi_handle lcdshadow_set_handle;
9968 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9969 enum drm_privacy_screen_status state)
9973 if (WARN_ON(!mutex_is_locked(&priv->lock)))
9976 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
9979 priv->hw_state = priv->sw_state = state;
9983 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
9987 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9990 priv->hw_state = priv->sw_state = output & 0x1;
9993 static const struct drm_privacy_screen_ops lcdshadow_ops = {
9994 .set_sw_state = lcdshadow_set_sw_state,
9995 .get_hw_state = lcdshadow_get_hw_state,
9998 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
10000 acpi_status status1, status2;
10003 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
10004 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
10005 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
10008 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10011 if (!(output & 0x10000))
10014 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
10015 &lcdshadow_ops, NULL);
10016 if (IS_ERR(lcdshadow_dev))
10017 return PTR_ERR(lcdshadow_dev);
10022 static void lcdshadow_exit(void)
10024 drm_privacy_screen_unregister(lcdshadow_dev);
10027 static void lcdshadow_resume(void)
10029 if (!lcdshadow_dev)
10032 mutex_lock(&lcdshadow_dev->lock);
10033 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
10034 mutex_unlock(&lcdshadow_dev->lock);
10037 static int lcdshadow_read(struct seq_file *m)
10039 if (!lcdshadow_dev) {
10040 seq_puts(m, "status:\t\tnot supported\n");
10042 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
10043 seq_puts(m, "commands:\t0, 1\n");
10049 static int lcdshadow_write(char *buf)
10052 int res, state = -EINVAL;
10054 if (!lcdshadow_dev)
10057 while ((cmd = strsep(&buf, ","))) {
10058 res = kstrtoint(cmd, 10, &state);
10063 if (state >= 2 || state < 0)
10066 mutex_lock(&lcdshadow_dev->lock);
10067 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
10068 mutex_unlock(&lcdshadow_dev->lock);
10070 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10075 static struct ibm_struct lcdshadow_driver_data = {
10076 .name = "lcdshadow",
10077 .exit = lcdshadow_exit,
10078 .resume = lcdshadow_resume,
10079 .read = lcdshadow_read,
10080 .write = lcdshadow_write,
10083 /*************************************************************************
10084 * Thinkpad sensor interfaces
10087 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
10088 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
10089 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
10090 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
10092 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
10093 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
10095 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
10096 #define PALMSENSOR_ON_BIT 1 /* psensor status */
10098 static bool has_palmsensor;
10099 static bool has_lapsensor;
10100 static bool palm_state;
10101 static bool lap_state;
10102 static int dytc_version;
10104 static int dytc_command(int command, int *output)
10106 acpi_handle dytc_handle;
10108 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
10109 /* Platform doesn't support DYTC */
10112 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
10117 static int lapsensor_get(bool *present, bool *state)
10122 err = dytc_command(DYTC_CMD_GET, &output);
10126 *present = true; /*If we get his far, we have lapmode support*/
10127 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
10131 static int palmsensor_get(bool *present, bool *state)
10133 acpi_handle psensor_handle;
10137 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10139 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10142 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10143 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10147 static void lapsensor_refresh(void)
10152 if (has_lapsensor) {
10153 err = lapsensor_get(&has_lapsensor, &state);
10156 if (lap_state != state) {
10158 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10163 static void palmsensor_refresh(void)
10168 if (has_palmsensor) {
10169 err = palmsensor_get(&has_palmsensor, &state);
10172 if (palm_state != state) {
10173 palm_state = state;
10174 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10179 static ssize_t dytc_lapmode_show(struct device *dev,
10180 struct device_attribute *attr,
10184 return sysfs_emit(buf, "%d\n", lap_state);
10185 return sysfs_emit(buf, "\n");
10187 static DEVICE_ATTR_RO(dytc_lapmode);
10189 static ssize_t palmsensor_show(struct device *dev,
10190 struct device_attribute *attr,
10193 if (has_palmsensor)
10194 return sysfs_emit(buf, "%d\n", palm_state);
10195 return sysfs_emit(buf, "\n");
10197 static DEVICE_ATTR_RO(palmsensor);
10199 static struct attribute *proxsensor_attributes[] = {
10200 &dev_attr_dytc_lapmode.attr,
10201 &dev_attr_palmsensor.attr,
10205 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10206 struct attribute *attr, int n)
10208 if (attr == &dev_attr_dytc_lapmode.attr) {
10210 * Platforms before DYTC version 5 claim to have a lap sensor,
10211 * but it doesn't work, so we ignore them.
10213 if (!has_lapsensor || dytc_version < 5)
10215 } else if (attr == &dev_attr_palmsensor.attr) {
10216 if (!has_palmsensor)
10223 static const struct attribute_group proxsensor_attr_group = {
10224 .is_visible = proxsensor_attr_is_visible,
10225 .attrs = proxsensor_attributes,
10228 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10230 int palm_err, lap_err;
10232 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10233 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10234 /* If support isn't available for both devices return -ENODEV */
10235 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10237 /* Otherwise, if there was an error return it */
10238 if (palm_err && (palm_err != -ENODEV))
10240 if (lap_err && (lap_err != -ENODEV))
10246 static struct ibm_struct proxsensor_driver_data = {
10247 .name = "proximity-sensor",
10250 /*************************************************************************
10251 * DYTC Platform Profile interface
10254 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10255 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10256 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10258 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10259 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10260 #define DYTC_FC_PSC 29 /* PSC Mode supported */
10261 #define DYTC_FC_AMT 31 /* AMT mode supported */
10263 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10264 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10266 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10267 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10268 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10270 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10271 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10272 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
10273 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
10274 #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
10276 #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
10277 #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
10279 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
10280 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
10281 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
10282 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
10284 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
10285 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
10286 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
10288 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10289 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10291 #define DYTC_SET_COMMAND(function, mode, on) \
10292 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10293 (mode) << DYTC_SET_MODE_BIT | \
10294 (on) << DYTC_SET_VALID_BIT)
10296 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
10297 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
10298 static int dytc_control_amt(bool enable);
10299 static bool dytc_amt_active;
10301 static enum platform_profile_option dytc_current_profile;
10302 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10303 static DEFINE_MUTEX(dytc_mutex);
10304 static int dytc_capabilities;
10305 static bool dytc_mmc_get_available;
10307 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10309 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10310 switch (dytcmode) {
10311 case DYTC_MODE_MMC_LOWPOWER:
10312 *profile = PLATFORM_PROFILE_LOW_POWER;
10314 case DYTC_MODE_MMC_DEFAULT:
10315 case DYTC_MODE_MMC_BALANCE:
10316 *profile = PLATFORM_PROFILE_BALANCED;
10318 case DYTC_MODE_MMC_PERFORM:
10319 *profile = PLATFORM_PROFILE_PERFORMANCE;
10321 default: /* Unknown mode */
10326 if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10327 switch (dytcmode) {
10328 case DYTC_MODE_PSC_LOWPOWER:
10329 *profile = PLATFORM_PROFILE_LOW_POWER;
10331 case DYTC_MODE_PSC_BALANCE:
10332 *profile = PLATFORM_PROFILE_BALANCED;
10334 case DYTC_MODE_PSC_PERFORM:
10335 *profile = PLATFORM_PROFILE_PERFORMANCE;
10337 default: /* Unknown mode */
10344 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10347 case PLATFORM_PROFILE_LOW_POWER:
10348 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10349 *perfmode = DYTC_MODE_MMC_LOWPOWER;
10350 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10351 *perfmode = DYTC_MODE_PSC_LOWPOWER;
10353 case PLATFORM_PROFILE_BALANCED:
10354 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10355 *perfmode = DYTC_MODE_MMC_BALANCE;
10356 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10357 *perfmode = DYTC_MODE_PSC_BALANCE;
10359 case PLATFORM_PROFILE_PERFORMANCE:
10360 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10361 *perfmode = DYTC_MODE_MMC_PERFORM;
10362 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10363 *perfmode = DYTC_MODE_PSC_PERFORM;
10365 default: /* Unknown profile */
10366 return -EOPNOTSUPP;
10372 * dytc_profile_get: Function to register with platform_profile
10373 * handler. Returns current platform profile.
10375 static int dytc_profile_get(struct platform_profile_handler *pprof,
10376 enum platform_profile_option *profile)
10378 *profile = dytc_current_profile;
10382 static int dytc_control_amt(bool enable)
10388 if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
10389 pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
10394 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
10396 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
10398 pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
10399 err = dytc_command(cmd, &dummy);
10402 dytc_amt_active = enable;
10407 * Helper function - check if we are in CQL mode and if we are
10409 * - run the command
10411 * If not in CQL mode, just run the command
10413 static int dytc_cql_command(int command, int *output)
10415 int err, cmd_err, dummy;
10418 /* Determine if we are in CQL mode. This alters the commands we do */
10419 err = dytc_command(DYTC_CMD_GET, output);
10423 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10424 /* Check if we're OK to return immediately */
10425 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10428 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10429 atomic_inc(&dytc_ignore_event);
10430 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10435 cmd_err = dytc_command(command, output);
10436 /* Check return condition after we've restored CQL state */
10438 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10439 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10447 * dytc_profile_set: Function to register with platform_profile
10448 * handler. Sets current platform profile.
10450 static int dytc_profile_set(struct platform_profile_handler *pprof,
10451 enum platform_profile_option profile)
10457 err = mutex_lock_interruptible(&dytc_mutex);
10461 err = convert_profile_to_dytc(profile, &perfmode);
10465 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10466 if (profile == PLATFORM_PROFILE_BALANCED) {
10468 * To get back to balanced mode we need to issue a reset command.
10469 * Note we still need to disable CQL mode before hand and re-enable
10470 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10471 * stuck at 0 for aprox. 30 minutes.
10473 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10477 /* Determine if we are in CQL mode. This alters the commands we do */
10478 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
10484 if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10485 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
10488 /* system supports AMT, activate it when on balanced */
10489 if (dytc_capabilities & BIT(DYTC_FC_AMT))
10490 dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
10492 /* Success - update current profile */
10493 dytc_current_profile = profile;
10495 mutex_unlock(&dytc_mutex);
10499 static void dytc_profile_refresh(void)
10501 enum platform_profile_option profile;
10502 int output, err = 0;
10505 mutex_lock(&dytc_mutex);
10506 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10507 if (dytc_mmc_get_available)
10508 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10510 err = dytc_cql_command(DYTC_CMD_GET, &output);
10511 } else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10512 err = dytc_command(DYTC_CMD_GET, &output);
10514 mutex_unlock(&dytc_mutex);
10518 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10519 convert_dytc_to_profile(perfmode, &profile);
10520 if (profile != dytc_current_profile) {
10521 dytc_current_profile = profile;
10522 platform_profile_notify();
10526 static struct platform_profile_handler dytc_profile = {
10527 .profile_get = dytc_profile_get,
10528 .profile_set = dytc_profile_set,
10531 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10535 /* Setup supported modes */
10536 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10537 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10538 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10540 err = dytc_command(DYTC_CMD_QUERY, &output);
10544 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10545 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10547 /* Check DYTC is enabled and supports mode setting */
10548 if (dytc_version < 5)
10551 /* Check what capabilities are supported */
10552 err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
10556 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
10557 pr_debug("MMC is supported\n");
10559 * Check if MMC_GET functionality available
10560 * Version > 6 and return success from MMC_GET command
10562 dytc_mmc_get_available = false;
10563 if (dytc_version >= 6) {
10564 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10565 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10566 dytc_mmc_get_available = true;
10568 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
10569 /* Support for this only works on AMD platforms */
10570 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
10571 dbg_printk(TPACPI_DBG_INIT, "PSC not support on Intel platforms\n");
10574 pr_debug("PSC is supported\n");
10576 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
10580 dbg_printk(TPACPI_DBG_INIT,
10581 "DYTC version %d: thermal mode available\n", dytc_version);
10583 /* Create platform_profile structure and register */
10584 err = platform_profile_register(&dytc_profile);
10586 * If for some reason platform_profiles aren't enabled
10587 * don't quit terminally.
10592 /* Ensure initial values are correct */
10593 dytc_profile_refresh();
10595 /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
10596 if (dytc_capabilities & BIT(DYTC_FC_PSC))
10597 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
10602 static void dytc_profile_exit(void)
10604 platform_profile_remove();
10607 static struct ibm_struct dytc_profile_driver_data = {
10608 .name = "dytc-profile",
10609 .exit = dytc_profile_exit,
10612 /*************************************************************************
10613 * Keyboard language interface
10616 struct keyboard_lang_data {
10617 const char *lang_str;
10621 static const struct keyboard_lang_data keyboard_lang_data[] = {
10643 static int set_keyboard_lang_command(int command)
10645 acpi_handle sskl_handle;
10648 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10649 /* Platform doesn't support SSKL */
10653 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10659 static int get_keyboard_lang(int *output)
10661 acpi_handle gskl_handle;
10664 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10665 /* Platform doesn't support GSKL */
10669 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10673 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10674 * '(' and ')') keys which use layout dependent key-press emulation.
10676 if (kbd_lang & METHOD_ERR)
10679 *output = kbd_lang;
10684 /* sysfs keyboard language entry */
10685 static ssize_t keyboard_lang_show(struct device *dev,
10686 struct device_attribute *attr,
10689 int output, err, i, len = 0;
10691 err = get_keyboard_lang(&output);
10695 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10697 len += sysfs_emit_at(buf, len, "%s", " ");
10699 if (output == keyboard_lang_data[i].lang_code) {
10700 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10702 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10705 len += sysfs_emit_at(buf, len, "\n");
10710 static ssize_t keyboard_lang_store(struct device *dev,
10711 struct device_attribute *attr,
10712 const char *buf, size_t count)
10715 bool lang_found = false;
10718 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10719 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10720 lang_code = keyboard_lang_data[i].lang_code;
10727 lang_code = lang_code | 1 << 24;
10729 /* Set language code */
10730 err = set_keyboard_lang_command(lang_code);
10734 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10738 tpacpi_disclose_usertask(attr->attr.name,
10739 "keyboard language is set to %s\n", buf);
10741 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10745 static DEVICE_ATTR_RW(keyboard_lang);
10747 static struct attribute *kbdlang_attributes[] = {
10748 &dev_attr_keyboard_lang.attr,
10752 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10753 struct attribute *attr, int n)
10755 return tp_features.kbd_lang ? attr->mode : 0;
10758 static const struct attribute_group kbdlang_attr_group = {
10759 .is_visible = kbdlang_attr_is_visible,
10760 .attrs = kbdlang_attributes,
10763 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10767 err = get_keyboard_lang(&output);
10768 tp_features.kbd_lang = !err;
10772 static struct ibm_struct kbdlang_driver_data = {
10776 /*************************************************************************
10777 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10778 * and WLAN feature.
10780 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10781 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10782 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10783 static bool has_antennatype;
10784 static int wwan_antennatype;
10786 static int dprc_command(int command, int *output)
10788 acpi_handle dprc_handle;
10790 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10791 /* Platform doesn't support DPRC */
10795 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10799 * METHOD_ERR gets returned on devices where few commands are not supported
10800 * for example command to get WWAN Antenna type command is not supported on
10803 if (*output & METHOD_ERR)
10809 static int get_wwan_antenna(int *wwan_antennatype)
10813 /* Get current Antenna type */
10814 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10818 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10819 *wwan_antennatype = 1;
10820 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10821 *wwan_antennatype = 2;
10828 /* sysfs wwan antenna type entry */
10829 static ssize_t wwan_antenna_type_show(struct device *dev,
10830 struct device_attribute *attr,
10833 switch (wwan_antennatype) {
10835 return sysfs_emit(buf, "type a\n");
10837 return sysfs_emit(buf, "type b\n");
10842 static DEVICE_ATTR_RO(wwan_antenna_type);
10844 static struct attribute *dprc_attributes[] = {
10845 &dev_attr_wwan_antenna_type.attr,
10849 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10850 struct attribute *attr, int n)
10852 return has_antennatype ? attr->mode : 0;
10855 static const struct attribute_group dprc_attr_group = {
10856 .is_visible = dprc_attr_is_visible,
10857 .attrs = dprc_attributes,
10860 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10864 err = get_wwan_antenna(&wwan_antennatype);
10868 has_antennatype = true;
10872 static struct ibm_struct dprc_driver_data = {
10876 /* --------------------------------------------------------------------- */
10878 static struct attribute *tpacpi_driver_attributes[] = {
10879 &driver_attr_debug_level.attr,
10880 &driver_attr_version.attr,
10881 &driver_attr_interface_version.attr,
10882 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10883 &driver_attr_wlsw_emulstate.attr,
10884 &driver_attr_bluetooth_emulstate.attr,
10885 &driver_attr_wwan_emulstate.attr,
10886 &driver_attr_uwb_emulstate.attr,
10891 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10892 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
10893 struct attribute *attr, int n)
10895 if (attr == &driver_attr_wlsw_emulstate.attr) {
10898 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
10899 if (!dbg_bluetoothemul)
10901 } else if (attr == &driver_attr_wwan_emulstate.attr) {
10904 } else if (attr == &driver_attr_uwb_emulstate.attr) {
10913 static const struct attribute_group tpacpi_driver_attr_group = {
10914 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10915 .is_visible = tpacpi_attr_is_visible,
10917 .attrs = tpacpi_driver_attributes,
10920 static const struct attribute_group *tpacpi_driver_groups[] = {
10921 &tpacpi_driver_attr_group,
10925 static const struct attribute_group *tpacpi_groups[] = {
10926 &adaptive_kbd_attr_group,
10927 &hotkey_attr_group,
10928 &bluetooth_attr_group,
10931 &proxsensor_attr_group,
10932 &kbdlang_attr_group,
10937 static const struct attribute_group *tpacpi_hwmon_groups[] = {
10938 &thermal_attr_group,
10939 &temp_label_attr_group,
10944 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
10945 &fan_driver_attr_group,
10949 /****************************************************************************
10950 ****************************************************************************
10954 ****************************************************************************
10955 ****************************************************************************/
10957 static struct platform_driver tpacpi_pdriver = {
10959 .name = TPACPI_DRVR_NAME,
10961 .groups = tpacpi_driver_groups,
10962 .dev_groups = tpacpi_groups,
10964 .shutdown = tpacpi_shutdown_handler,
10967 static struct platform_driver tpacpi_hwmon_pdriver = {
10969 .name = TPACPI_HWMON_DRVR_NAME,
10970 .groups = tpacpi_hwmon_driver_groups,
10974 /****************************************************************************
10975 ****************************************************************************
10979 ****************************************************************************
10980 ****************************************************************************/
10983 * HKEY event callout for other subdrivers go here
10984 * (yes, it is ugly, but it is quick, safe, and gets the job done
10986 static void tpacpi_driver_event(const unsigned int hkey_event)
10988 if (ibm_backlight_device) {
10989 switch (hkey_event) {
10990 case TP_HKEY_EV_BRGHT_UP:
10991 case TP_HKEY_EV_BRGHT_DOWN:
10992 tpacpi_brightness_notify_change();
10996 switch (hkey_event) {
10997 case TP_HKEY_EV_VOL_UP:
10998 case TP_HKEY_EV_VOL_DOWN:
10999 case TP_HKEY_EV_VOL_MUTE:
11000 volume_alsa_notify_change();
11003 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
11004 enum led_brightness brightness;
11006 mutex_lock(&kbdlight_mutex);
11009 * Check the brightness actually changed, setting the brightness
11010 * through kbdlight_set_level() also triggers this event.
11012 brightness = kbdlight_sysfs_get(NULL);
11013 if (kbdlight_brightness != brightness) {
11014 kbdlight_brightness = brightness;
11015 led_classdev_notify_brightness_hw_changed(
11016 &tpacpi_led_kbdlight.led_classdev, brightness);
11019 mutex_unlock(&kbdlight_mutex);
11022 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
11023 lapsensor_refresh();
11024 /* If we are already accessing DYTC then skip dytc update */
11025 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
11026 dytc_profile_refresh();
11029 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
11030 enum drm_privacy_screen_status old_hw_state;
11033 mutex_lock(&lcdshadow_dev->lock);
11034 old_hw_state = lcdshadow_dev->hw_state;
11035 lcdshadow_get_hw_state(lcdshadow_dev);
11036 changed = lcdshadow_dev->hw_state != old_hw_state;
11037 mutex_unlock(&lcdshadow_dev->lock);
11040 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
11042 if (hkey_event == TP_HKEY_EV_AMT_TOGGLE) {
11043 /* If we're enabling AMT we need to force balanced mode */
11044 if (!dytc_amt_active)
11045 /* This will also set AMT mode enabled */
11046 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
11048 dytc_control_amt(!dytc_amt_active);
11053 static void hotkey_driver_event(const unsigned int scancode)
11055 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
11058 /* --------------------------------------------------------------------- */
11060 /* /proc support */
11061 static struct proc_dir_entry *proc_dir;
11064 * Module and infrastructure proble, init and exit handling
11067 static bool force_load;
11069 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
11070 static const char * __init str_supported(int is_supported)
11072 static char text_unsupported[] __initdata = "not supported";
11074 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
11076 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
11078 static void ibm_exit(struct ibm_struct *ibm)
11080 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
11082 list_del_init(&ibm->all_drivers);
11084 if (ibm->flags.acpi_notify_installed) {
11085 dbg_printk(TPACPI_DBG_EXIT,
11086 "%s: acpi_remove_notify_handler\n", ibm->name);
11087 BUG_ON(!ibm->acpi);
11088 acpi_remove_notify_handler(*ibm->acpi->handle,
11090 dispatch_acpi_notify);
11091 ibm->flags.acpi_notify_installed = 0;
11094 if (ibm->flags.proc_created) {
11095 dbg_printk(TPACPI_DBG_EXIT,
11096 "%s: remove_proc_entry\n", ibm->name);
11097 remove_proc_entry(ibm->name, proc_dir);
11098 ibm->flags.proc_created = 0;
11101 if (ibm->flags.acpi_driver_registered) {
11102 dbg_printk(TPACPI_DBG_EXIT,
11103 "%s: acpi_bus_unregister_driver\n", ibm->name);
11104 BUG_ON(!ibm->acpi);
11105 acpi_bus_unregister_driver(ibm->acpi->driver);
11106 kfree(ibm->acpi->driver);
11107 ibm->acpi->driver = NULL;
11108 ibm->flags.acpi_driver_registered = 0;
11111 if (ibm->flags.init_called && ibm->exit) {
11113 ibm->flags.init_called = 0;
11116 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
11119 static int __init ibm_init(struct ibm_init_struct *iibm)
11122 struct ibm_struct *ibm = iibm->data;
11123 struct proc_dir_entry *entry;
11125 BUG_ON(ibm == NULL);
11127 INIT_LIST_HEAD(&ibm->all_drivers);
11129 if (ibm->flags.experimental && !experimental)
11132 dbg_printk(TPACPI_DBG_INIT,
11133 "probing for %s\n", ibm->name);
11136 ret = iibm->init(iibm);
11137 if (ret > 0 || ret == -ENODEV)
11138 return 0; /* subdriver functionality not available */
11142 ibm->flags.init_called = 1;
11146 if (ibm->acpi->hid) {
11147 ret = register_tpacpi_subdriver(ibm);
11152 if (ibm->acpi->notify) {
11153 ret = setup_acpi_notify(ibm);
11154 if (ret == -ENODEV) {
11155 pr_notice("disabling subdriver %s\n",
11165 dbg_printk(TPACPI_DBG_INIT,
11166 "%s installed\n", ibm->name);
11169 umode_t mode = iibm->base_procfs_mode;
11175 entry = proc_create_data(ibm->name, mode, proc_dir,
11176 &dispatch_proc_ops, ibm);
11178 pr_err("unable to create proc entry %s\n", ibm->name);
11182 ibm->flags.proc_created = 1;
11185 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
11190 dbg_printk(TPACPI_DBG_INIT,
11191 "%s: at error exit path with result %d\n",
11195 return (ret < 0) ? ret : 0;
11200 static char __init tpacpi_parse_fw_id(const char * const s,
11201 u32 *model, u16 *release)
11205 if (!s || strlen(s) < 8)
11208 for (i = 0; i < 8; i++)
11209 if (!((s[i] >= '0' && s[i] <= '9') ||
11210 (s[i] >= 'A' && s[i] <= 'Z')))
11214 * Most models: xxyTkkWW (#.##c)
11215 * Ancient 570/600 and -SL lacks (#.##c)
11217 if (s[3] == 'T' || s[3] == 'N') {
11218 *model = TPID(s[0], s[1]);
11219 *release = TPVER(s[4], s[5]);
11222 /* New models: xxxyTkkW (#.##c); T550 and some others */
11223 } else if (s[4] == 'T' || s[4] == 'N') {
11224 *model = TPID3(s[0], s[1], s[2]);
11225 *release = TPVER(s[5], s[6]);
11233 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
11235 char *ec_fw_string = (char *) private;
11236 const char *dmi_data = (const char *)dm;
11238 * ThinkPad Embedded Controller Program Table on newer models
11240 * Offset | Name | Width | Description
11241 * ----------------------------------------------------
11242 * 0x00 | Type | BYTE | 0x8C
11243 * 0x01 | Length | BYTE |
11244 * 0x02 | Handle | WORD | Varies
11245 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11246 * 0x0A | OEM struct offset | BYTE | 0x0B
11247 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11248 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11249 * 0x0D | ECP version ID | STR ID |
11250 * 0x0E | ECP release date | STR ID |
11253 /* Return if data structure not match */
11254 if (dm->type != 140 || dm->length < 0x0F ||
11255 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11256 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11257 dmi_data[0x0C] != 0x01)
11260 /* fwstr is the first 8byte string */
11261 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
11264 /* returns 0 - probe ok, or < 0 - probe error.
11265 * Probe ok doesn't mean thinkpad found.
11266 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11267 static int __must_check __init get_thinkpad_model_data(
11268 struct thinkpad_id_data *tp)
11270 const struct dmi_device *dev = NULL;
11271 char ec_fw_string[18] = {0};
11278 memset(tp, 0, sizeof(*tp));
11280 if (dmi_name_in_vendors("IBM"))
11281 tp->vendor = PCI_VENDOR_ID_IBM;
11282 else if (dmi_name_in_vendors("LENOVO"))
11283 tp->vendor = PCI_VENDOR_ID_LENOVO;
11287 s = dmi_get_system_info(DMI_BIOS_VERSION);
11288 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11289 if (s && !tp->bios_version_str)
11292 /* Really ancient ThinkPad 240X will fail this, which is fine */
11293 t = tpacpi_parse_fw_id(tp->bios_version_str,
11294 &tp->bios_model, &tp->bios_release);
11295 if (t != 'E' && t != 'C')
11299 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11300 * X32 or newer, all Z series; Some models must have an
11301 * up-to-date BIOS or they will not be detected.
11303 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11305 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11306 if (sscanf(dev->name,
11307 "IBM ThinkPad Embedded Controller -[%17c",
11308 ec_fw_string) == 1) {
11309 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11310 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11315 /* Newer ThinkPads have different EC program info table */
11316 if (!ec_fw_string[0])
11317 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11319 if (ec_fw_string[0]) {
11320 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11321 if (!tp->ec_version_str)
11324 t = tpacpi_parse_fw_id(ec_fw_string,
11325 &tp->ec_model, &tp->ec_release);
11327 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11329 pr_notice("please report this to %s\n", TPACPI_MAIL);
11333 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11334 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11335 tp->model_str = kstrdup(s, GFP_KERNEL);
11336 if (!tp->model_str)
11339 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11340 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11341 tp->model_str = kstrdup(s, GFP_KERNEL);
11342 if (!tp->model_str)
11347 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11348 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11349 if (s && !tp->nummodel_str)
11355 static int __init probe_for_thinkpad(void)
11362 /* It would be dangerous to run the driver in this case */
11363 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11367 * Non-ancient models have better DMI tagging, but very old models
11368 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11370 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11371 (thinkpad_id.ec_model != 0) ||
11372 tpacpi_is_fw_known();
11374 /* The EC handler is required */
11375 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11378 pr_err("Not yet supported ThinkPad detected!\n");
11382 if (!is_thinkpad && !force_load)
11388 static void __init thinkpad_acpi_init_banner(void)
11390 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11391 pr_info("%s\n", TPACPI_URL);
11393 pr_info("ThinkPad BIOS %s, EC %s\n",
11394 (thinkpad_id.bios_version_str) ?
11395 thinkpad_id.bios_version_str : "unknown",
11396 (thinkpad_id.ec_version_str) ?
11397 thinkpad_id.ec_version_str : "unknown");
11399 BUG_ON(!thinkpad_id.vendor);
11401 if (thinkpad_id.model_str)
11402 pr_info("%s %s, model %s\n",
11403 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11404 "IBM" : ((thinkpad_id.vendor ==
11405 PCI_VENDOR_ID_LENOVO) ?
11406 "Lenovo" : "Unknown vendor"),
11407 thinkpad_id.model_str,
11408 (thinkpad_id.nummodel_str) ?
11409 thinkpad_id.nummodel_str : "unknown");
11412 /* Module init, exit, parameters */
11414 static struct ibm_init_struct ibms_init[] __initdata = {
11416 .data = &thinkpad_acpi_driver_data,
11419 .init = hotkey_init,
11420 .data = &hotkey_driver_data,
11423 .init = bluetooth_init,
11424 .data = &bluetooth_driver_data,
11428 .data = &wan_driver_data,
11432 .data = &uwb_driver_data,
11434 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11436 .init = video_init,
11437 .base_procfs_mode = S_IRUSR,
11438 .data = &video_driver_data,
11442 .init = kbdlight_init,
11443 .data = &kbdlight_driver_data,
11446 .init = light_init,
11447 .data = &light_driver_data,
11451 .data = &cmos_driver_data,
11455 .data = &led_driver_data,
11459 .data = &beep_driver_data,
11462 .init = thermal_init,
11463 .data = &thermal_driver_data,
11466 .init = brightness_init,
11467 .data = &brightness_driver_data,
11470 .init = volume_init,
11471 .data = &volume_driver_data,
11475 .data = &fan_driver_data,
11478 .init = mute_led_init,
11479 .data = &mute_led_driver_data,
11482 .init = tpacpi_battery_init,
11483 .data = &battery_driver_data,
11486 .init = tpacpi_lcdshadow_init,
11487 .data = &lcdshadow_driver_data,
11490 .init = tpacpi_proxsensor_init,
11491 .data = &proxsensor_driver_data,
11494 .init = tpacpi_dytc_profile_init,
11495 .data = &dytc_profile_driver_data,
11498 .init = tpacpi_kbdlang_init,
11499 .data = &kbdlang_driver_data,
11502 .init = tpacpi_dprc_init,
11503 .data = &dprc_driver_data,
11507 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11510 struct ibm_struct *ibm;
11512 if (!kp || !kp->name || !val)
11515 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11516 ibm = ibms_init[i].data;
11517 if (!ibm || !ibm->name)
11520 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11521 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11523 strcpy(ibms_init[i].param, val);
11531 module_param(experimental, int, 0444);
11532 MODULE_PARM_DESC(experimental,
11533 "Enables experimental features when non-zero");
11535 module_param_named(debug, dbg_level, uint, 0);
11536 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11538 module_param(force_load, bool, 0444);
11539 MODULE_PARM_DESC(force_load,
11540 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11542 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11543 MODULE_PARM_DESC(fan_control,
11544 "Enables setting fan parameters features when true");
11546 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11547 MODULE_PARM_DESC(brightness_mode,
11548 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11550 module_param(brightness_enable, uint, 0444);
11551 MODULE_PARM_DESC(brightness_enable,
11552 "Enables backlight control when 1, disables when 0");
11554 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11555 module_param_named(volume_mode, volume_mode, uint, 0444);
11556 MODULE_PARM_DESC(volume_mode,
11557 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11559 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11560 MODULE_PARM_DESC(volume_capabilities,
11561 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11563 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11564 MODULE_PARM_DESC(volume_control,
11565 "Enables software override for the console audio control when true");
11567 module_param_named(software_mute, software_mute_requested, bool, 0444);
11568 MODULE_PARM_DESC(software_mute,
11569 "Request full software mute control");
11571 /* ALSA module API parameters */
11572 module_param_named(index, alsa_index, int, 0444);
11573 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11574 module_param_named(id, alsa_id, charp, 0444);
11575 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11576 module_param_named(enable, alsa_enable, bool, 0444);
11577 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11578 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11580 /* The module parameter can't be read back, that's why 0 is used here */
11581 #define TPACPI_PARAM(feature) \
11582 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11583 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11585 TPACPI_PARAM(hotkey);
11586 TPACPI_PARAM(bluetooth);
11587 TPACPI_PARAM(video);
11588 TPACPI_PARAM(light);
11589 TPACPI_PARAM(cmos);
11591 TPACPI_PARAM(beep);
11592 TPACPI_PARAM(brightness);
11593 TPACPI_PARAM(volume);
11596 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11597 module_param(dbg_wlswemul, uint, 0444);
11598 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11599 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11600 MODULE_PARM_DESC(wlsw_state,
11601 "Initial state of the emulated WLSW switch");
11603 module_param(dbg_bluetoothemul, uint, 0444);
11604 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11605 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11606 MODULE_PARM_DESC(bluetooth_state,
11607 "Initial state of the emulated bluetooth switch");
11609 module_param(dbg_wwanemul, uint, 0444);
11610 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11611 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11612 MODULE_PARM_DESC(wwan_state,
11613 "Initial state of the emulated WWAN switch");
11615 module_param(dbg_uwbemul, uint, 0444);
11616 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11617 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11618 MODULE_PARM_DESC(uwb_state,
11619 "Initial state of the emulated UWB switch");
11622 static void thinkpad_acpi_module_exit(void)
11624 struct ibm_struct *ibm, *itmp;
11626 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11628 #ifdef CONFIG_SUSPEND
11629 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio)
11630 acpi_unregister_lps0_dev(&thinkpad_acpi_s2idle_dev_ops);
11633 hwmon_device_unregister(tpacpi_hwmon);
11634 if (tp_features.sensors_pdrv_registered)
11635 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11636 if (tp_features.platform_drv_registered)
11637 platform_driver_unregister(&tpacpi_pdriver);
11639 list_for_each_entry_safe_reverse(ibm, itmp,
11640 &tpacpi_all_drivers,
11645 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11647 if (tpacpi_inputdev) {
11648 if (tp_features.input_device_registered)
11649 input_unregister_device(tpacpi_inputdev);
11651 input_free_device(tpacpi_inputdev);
11652 kfree(hotkey_keycode_map);
11655 if (tpacpi_sensors_pdev)
11656 platform_device_unregister(tpacpi_sensors_pdev);
11658 platform_device_unregister(tpacpi_pdev);
11660 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11662 destroy_workqueue(tpacpi_wq);
11664 kfree(thinkpad_id.bios_version_str);
11665 kfree(thinkpad_id.ec_version_str);
11666 kfree(thinkpad_id.model_str);
11667 kfree(thinkpad_id.nummodel_str);
11671 static int __init thinkpad_acpi_module_init(void)
11673 const struct dmi_system_id *dmi_id;
11676 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11678 /* Driver-level probe */
11680 ret = get_thinkpad_model_data(&thinkpad_id);
11682 pr_err("unable to get DMI data: %d\n", ret);
11683 thinkpad_acpi_module_exit();
11686 ret = probe_for_thinkpad();
11688 thinkpad_acpi_module_exit();
11692 /* Driver initialization */
11694 thinkpad_acpi_init_banner();
11695 tpacpi_check_outdated_fw();
11697 TPACPI_ACPIHANDLE_INIT(ecrd);
11698 TPACPI_ACPIHANDLE_INIT(ecwr);
11700 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11702 thinkpad_acpi_module_exit();
11706 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11708 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11709 thinkpad_acpi_module_exit();
11713 dmi_id = dmi_first_match(fwbug_list);
11715 tp_features.quirks = dmi_id->driver_data;
11717 /* Device initialization */
11718 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
11720 if (IS_ERR(tpacpi_pdev)) {
11721 ret = PTR_ERR(tpacpi_pdev);
11722 tpacpi_pdev = NULL;
11723 pr_err("unable to register platform device\n");
11724 thinkpad_acpi_module_exit();
11727 tpacpi_sensors_pdev = platform_device_register_simple(
11728 TPACPI_HWMON_DRVR_NAME,
11729 PLATFORM_DEVID_NONE, NULL, 0);
11730 if (IS_ERR(tpacpi_sensors_pdev)) {
11731 ret = PTR_ERR(tpacpi_sensors_pdev);
11732 tpacpi_sensors_pdev = NULL;
11733 pr_err("unable to register hwmon platform device\n");
11734 thinkpad_acpi_module_exit();
11738 mutex_init(&tpacpi_inputdev_send_mutex);
11739 tpacpi_inputdev = input_allocate_device();
11740 if (!tpacpi_inputdev) {
11741 thinkpad_acpi_module_exit();
11744 /* Prepare input device, but don't register */
11745 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11746 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11747 tpacpi_inputdev->id.bustype = BUS_HOST;
11748 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11749 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11750 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11751 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11754 /* Init subdriver dependencies */
11755 tpacpi_detect_brightness_capabilities();
11757 /* Init subdrivers */
11758 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11759 ret = ibm_init(&ibms_init[i]);
11760 if (ret >= 0 && *ibms_init[i].param)
11761 ret = ibms_init[i].data->write(ibms_init[i].param);
11763 thinkpad_acpi_module_exit();
11768 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11770 ret = platform_driver_register(&tpacpi_pdriver);
11772 pr_err("unable to register main platform driver\n");
11773 thinkpad_acpi_module_exit();
11776 tp_features.platform_drv_registered = 1;
11778 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11780 pr_err("unable to register hwmon platform driver\n");
11781 thinkpad_acpi_module_exit();
11784 tp_features.sensors_pdrv_registered = 1;
11786 tpacpi_hwmon = hwmon_device_register_with_groups(
11787 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11788 if (IS_ERR(tpacpi_hwmon)) {
11789 ret = PTR_ERR(tpacpi_hwmon);
11790 tpacpi_hwmon = NULL;
11791 pr_err("unable to register hwmon device\n");
11792 thinkpad_acpi_module_exit();
11796 ret = input_register_device(tpacpi_inputdev);
11798 pr_err("unable to register input device\n");
11799 thinkpad_acpi_module_exit();
11802 tp_features.input_device_registered = 1;
11805 #ifdef CONFIG_SUSPEND
11806 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio) {
11807 if (!acpi_register_lps0_dev(&thinkpad_acpi_s2idle_dev_ops))
11808 pr_info("Using s2idle quirk to avoid %s platform firmware bug\n",
11809 (dmi_id && dmi_id->ident) ? dmi_id->ident : "");
11815 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11818 * This will autoload the driver in almost every ThinkPad
11819 * in widespread use.
11821 * Only _VERY_ old models, like the 240, 240x and 570 lack
11822 * the HKEY event interface.
11824 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11827 * DMI matching for module autoloading
11829 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11830 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
11832 * Only models listed in thinkwiki will be supported, so add yours
11833 * if it is not there yet.
11835 #define IBM_BIOS_MODULE_ALIAS(__type) \
11836 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
11838 /* Ancient thinkpad BIOSes have to be identified by
11839 * BIOS type or model number, and there are far less
11840 * BIOS types than model numbers... */
11841 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
11845 MODULE_DESCRIPTION(TPACPI_DESC);
11846 MODULE_VERSION(TPACPI_VERSION);
11847 MODULE_LICENSE("GPL");
11849 module_init(thinkpad_acpi_module_init);
11850 module_exit(thinkpad_acpi_module_exit);