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 FAN_NOT_PRESENT 65535
268 /****************************************************************************
269 * Driver-wide structs and misc. variables
274 struct tp_acpi_drv_struct {
275 const struct acpi_device_id *hid;
276 struct acpi_driver *driver;
278 void (*notify) (struct ibm_struct *, u32);
281 struct acpi_device *device;
287 int (*read) (struct seq_file *);
288 int (*write) (char *);
290 void (*resume) (void);
291 void (*suspend) (void);
292 void (*shutdown) (void);
294 struct list_head all_drivers;
296 struct tp_acpi_drv_struct *acpi;
299 u8 acpi_driver_registered:1;
300 u8 acpi_notify_installed:1;
307 struct ibm_init_struct {
310 int (*init) (struct ibm_init_struct *);
311 umode_t base_procfs_mode;
312 struct ibm_struct *data;
321 static struct quirk_entry quirk_btusb_bug = {
325 static struct quirk_entry quirk_s2idle_bug = {
326 .s2idle_bug_mmio = 0xfed80380,
335 TP_HOTKEY_TABLET_NONE = 0,
336 TP_HOTKEY_TABLET_USES_MHKG,
337 TP_HOTKEY_TABLET_USES_GMMS,
342 u32 bright_acpimode:1;
346 u32 fan_ctrl_status_undef:1;
348 u32 second_fan_ctl:1;
349 u32 beep_needs_two_args:1;
350 u32 mixer_no_level_control:1;
351 u32 battery_force_primary:1;
352 u32 input_device_registered:1;
353 u32 platform_drv_registered:1;
354 u32 sensors_pdrv_registered:1;
355 u32 hotkey_poll_active:1;
356 u32 has_adaptive_kbd:1;
358 struct quirk_entry *quirks;
362 u16 hotkey_mask_ff:1;
363 u16 volume_ctrl_forbidden:1;
366 struct thinkpad_id_data {
367 unsigned int vendor; /* ThinkPad vendor:
368 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
370 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
371 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
373 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
375 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
378 char *model_str; /* ThinkPad T43 */
379 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
381 static struct thinkpad_id_data thinkpad_id;
384 TPACPI_LIFE_INIT = 0,
389 static int experimental;
390 static u32 dbg_level;
392 static struct workqueue_struct *tpacpi_wq;
400 /* tpacpi LED class */
401 struct tpacpi_led_classdev {
402 struct led_classdev led_classdev;
406 /* brightness level capabilities */
407 static unsigned int bright_maxlvl; /* 0 = unknown */
409 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
410 static int dbg_wlswemul;
411 static bool tpacpi_wlsw_emulstate;
412 static int dbg_bluetoothemul;
413 static bool tpacpi_bluetooth_emulstate;
414 static int dbg_wwanemul;
415 static bool tpacpi_wwan_emulstate;
416 static int dbg_uwbemul;
417 static bool tpacpi_uwb_emulstate;
421 /*************************************************************************
425 #define dbg_printk(a_dbg_level, format, arg...) \
427 if (dbg_level & (a_dbg_level)) \
428 printk(KERN_DEBUG pr_fmt("%s: " format), \
432 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
433 #define vdbg_printk dbg_printk
434 static const char *str_supported(int is_supported);
436 static inline const char *str_supported(int is_supported) { return ""; }
437 #define vdbg_printk(a_dbg_level, format, arg...) \
438 do { if (0) no_printk(format, ##arg); } while (0)
441 static void tpacpi_log_usertask(const char * const what)
443 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
444 what, task_tgid_vnr(current));
447 #define tpacpi_disclose_usertask(what, format, arg...) \
449 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
450 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
451 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
452 what, task_tgid_vnr(current), ## arg); \
457 * Quirk handling helpers
459 * ThinkPad IDs and versions seen in the field so far are
460 * two or three characters from the set [0-9A-Z], i.e. base 36.
462 * We use values well outside that range as specials.
465 #define TPACPI_MATCH_ANY 0xffffffffU
466 #define TPACPI_MATCH_ANY_VERSION 0xffffU
467 #define TPACPI_MATCH_UNKNOWN 0U
469 /* TPID('1', 'Y') == 0x3159 */
470 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
471 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
474 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
475 { .vendor = PCI_VENDOR_ID_IBM, \
476 .bios = TPID(__id1, __id2), \
477 .ec = TPACPI_MATCH_ANY, \
478 .quirks = (__quirk) }
480 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_LENOVO, \
482 .bios = TPID(__id1, __id2), \
483 .ec = TPACPI_MATCH_ANY, \
484 .quirks = (__quirk) }
486 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
487 { .vendor = PCI_VENDOR_ID_LENOVO, \
488 .bios = TPID3(__id1, __id2, __id3), \
489 .ec = TPACPI_MATCH_ANY, \
490 .quirks = (__quirk) }
492 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
493 { .vendor = PCI_VENDOR_ID_IBM, \
494 .bios = TPACPI_MATCH_ANY, \
495 .ec = TPID(__id1, __id2), \
496 .quirks = (__quirk) }
498 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
499 { .vendor = PCI_VENDOR_ID_LENOVO, \
500 .bios = TPACPI_MATCH_ANY, \
501 .ec = TPID(__id1, __id2), \
502 .quirks = (__quirk) }
504 struct tpacpi_quirk {
508 unsigned long quirks;
512 * tpacpi_check_quirks() - search BIOS/EC version on a list
513 * @qlist: array of &struct tpacpi_quirk
514 * @qlist_size: number of elements in @qlist
516 * Iterates over a quirks list until one is found that matches the
517 * ThinkPad's vendor, BIOS and EC model.
519 * Returns 0 if nothing matches, otherwise returns the quirks field of
520 * the matching &struct tpacpi_quirk entry.
522 * The match criteria is: vendor, ec and bios much match.
524 static unsigned long __init tpacpi_check_quirks(
525 const struct tpacpi_quirk *qlist,
526 unsigned int qlist_size)
529 if ((qlist->vendor == thinkpad_id.vendor ||
530 qlist->vendor == TPACPI_MATCH_ANY) &&
531 (qlist->bios == thinkpad_id.bios_model ||
532 qlist->bios == TPACPI_MATCH_ANY) &&
533 (qlist->ec == thinkpad_id.ec_model ||
534 qlist->ec == TPACPI_MATCH_ANY))
535 return qlist->quirks;
543 static inline bool __pure __init tpacpi_is_lenovo(void)
545 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
548 static inline bool __pure __init tpacpi_is_ibm(void)
550 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
553 /****************************************************************************
554 ****************************************************************************
556 * ACPI Helpers and device model
558 ****************************************************************************
559 ****************************************************************************/
561 /*************************************************************************
565 static acpi_handle root_handle;
566 static acpi_handle ec_handle;
568 #define TPACPI_HANDLE(object, parent, paths...) \
569 static acpi_handle object##_handle; \
570 static const acpi_handle * const object##_parent __initconst = \
572 static char *object##_paths[] __initdata = { paths }
574 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
575 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
577 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
579 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
580 "\\CMS", /* R40, R40e */
583 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
584 "^HKEY", /* R30, R31 */
585 "HKEY", /* all others */
588 /*************************************************************************
592 static int acpi_evalf(acpi_handle handle,
593 int *res, char *method, char *fmt, ...)
596 struct acpi_object_list params;
597 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
598 struct acpi_buffer result, *resultp;
599 union acpi_object out_obj;
607 pr_err("acpi_evalf() called with empty format\n");
620 params.pointer = &in_objs[0];
627 in_objs[params.count].integer.value = va_arg(ap, int);
628 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
630 /* add more types as needed */
632 pr_err("acpi_evalf() called with invalid format character '%c'\n",
640 if (res_type != 'v') {
641 result.length = sizeof(out_obj);
642 result.pointer = &out_obj;
647 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
651 success = (status == AE_OK &&
652 out_obj.type == ACPI_TYPE_INTEGER);
654 *res = out_obj.integer.value;
657 success = status == AE_OK;
659 /* add more types as needed */
661 pr_err("acpi_evalf() called with invalid format character '%c'\n",
666 if (!success && !quiet)
667 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
668 method, fmt0, acpi_format_exception(status));
673 static int acpi_ec_read(int i, u8 *p)
678 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
682 if (ec_read(i, p) < 0)
689 static int acpi_ec_write(int i, u8 v)
692 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
695 if (ec_write(i, v) < 0)
702 static int issue_thinkpad_cmos_command(int cmos_cmd)
707 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
713 /*************************************************************************
717 #define TPACPI_ACPIHANDLE_INIT(object) \
718 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
719 object##_paths, ARRAY_SIZE(object##_paths))
721 static void __init drv_acpi_handle_init(const char *name,
722 acpi_handle *handle, const acpi_handle parent,
723 char **paths, const int num_paths)
728 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
731 for (i = 0; i < num_paths; i++) {
732 status = acpi_get_handle(parent, paths[i], handle);
733 if (ACPI_SUCCESS(status)) {
734 dbg_printk(TPACPI_DBG_INIT,
735 "Found ACPI handle %s for %s\n",
741 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
746 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
747 u32 level, void *context, void **return_value)
749 if (!strcmp(context, "video")) {
750 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
752 if (!dev || strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
756 *(acpi_handle *)return_value = handle;
758 return AE_CTRL_TERMINATE;
761 static void __init tpacpi_acpi_handle_locate(const char *name,
766 acpi_handle device_found;
768 BUG_ON(!name || !handle);
769 vdbg_printk(TPACPI_DBG_INIT,
770 "trying to locate ACPI handle for %s, using HID %s\n",
771 name, hid ? hid : "NULL");
773 memset(&device_found, 0, sizeof(device_found));
774 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
775 (void *)name, &device_found);
779 if (ACPI_SUCCESS(status)) {
780 *handle = device_found;
781 dbg_printk(TPACPI_DBG_INIT,
782 "Found ACPI handle for %s\n", name);
784 vdbg_printk(TPACPI_DBG_INIT,
785 "Could not locate an ACPI handle for %s: %s\n",
786 name, acpi_format_exception(status));
790 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
792 struct ibm_struct *ibm = data;
794 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
797 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
800 ibm->acpi->notify(ibm, event);
803 static int __init setup_acpi_notify(struct ibm_struct *ibm)
809 if (!*ibm->acpi->handle)
812 vdbg_printk(TPACPI_DBG_INIT,
813 "setting up ACPI notify for %s\n", ibm->name);
815 ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
816 if (!ibm->acpi->device) {
817 pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
821 ibm->acpi->device->driver_data = ibm;
822 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
823 TPACPI_ACPI_EVENT_PREFIX,
826 status = acpi_install_notify_handler(*ibm->acpi->handle,
827 ibm->acpi->type, dispatch_acpi_notify, ibm);
828 if (ACPI_FAILURE(status)) {
829 if (status == AE_ALREADY_EXISTS) {
830 pr_notice("another device driver is already handling %s events\n",
833 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
834 ibm->name, acpi_format_exception(status));
838 ibm->flags.acpi_notify_installed = 1;
842 static int __init tpacpi_device_add(struct acpi_device *device)
847 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
851 dbg_printk(TPACPI_DBG_INIT,
852 "registering %s as an ACPI driver\n", ibm->name);
856 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
857 if (!ibm->acpi->driver) {
858 pr_err("failed to allocate memory for ibm->acpi->driver\n");
862 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
863 ibm->acpi->driver->ids = ibm->acpi->hid;
865 ibm->acpi->driver->ops.add = &tpacpi_device_add;
867 rc = acpi_bus_register_driver(ibm->acpi->driver);
869 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
871 kfree(ibm->acpi->driver);
872 ibm->acpi->driver = NULL;
874 ibm->flags.acpi_driver_registered = 1;
880 /****************************************************************************
881 ****************************************************************************
885 ****************************************************************************
886 ****************************************************************************/
888 static int dispatch_proc_show(struct seq_file *m, void *v)
890 struct ibm_struct *ibm = m->private;
892 if (!ibm || !ibm->read)
897 static int dispatch_proc_open(struct inode *inode, struct file *file)
899 return single_open(file, dispatch_proc_show, pde_data(inode));
902 static ssize_t dispatch_proc_write(struct file *file,
903 const char __user *userbuf,
904 size_t count, loff_t *pos)
906 struct ibm_struct *ibm = pde_data(file_inode(file));
910 if (!ibm || !ibm->write)
912 if (count > PAGE_SIZE - 1)
915 kernbuf = kmalloc(count + 1, GFP_KERNEL);
919 if (copy_from_user(kernbuf, userbuf, count)) {
925 ret = ibm->write(kernbuf);
934 static const struct proc_ops dispatch_proc_ops = {
935 .proc_open = dispatch_proc_open,
936 .proc_read = seq_read,
937 .proc_lseek = seq_lseek,
938 .proc_release = single_release,
939 .proc_write = dispatch_proc_write,
942 /****************************************************************************
943 ****************************************************************************
945 * Device model: input, hwmon and platform
947 ****************************************************************************
948 ****************************************************************************/
950 static struct platform_device *tpacpi_pdev;
951 static struct platform_device *tpacpi_sensors_pdev;
952 static struct device *tpacpi_hwmon;
953 static struct input_dev *tpacpi_inputdev;
954 static struct mutex tpacpi_inputdev_send_mutex;
955 static LIST_HEAD(tpacpi_all_drivers);
957 #ifdef CONFIG_PM_SLEEP
958 static int tpacpi_suspend_handler(struct device *dev)
960 struct ibm_struct *ibm, *itmp;
962 list_for_each_entry_safe(ibm, itmp,
972 static int tpacpi_resume_handler(struct device *dev)
974 struct ibm_struct *ibm, *itmp;
976 list_for_each_entry_safe(ibm, itmp,
987 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
988 tpacpi_suspend_handler, tpacpi_resume_handler);
990 static void tpacpi_shutdown_handler(struct platform_device *pdev)
992 struct ibm_struct *ibm, *itmp;
994 list_for_each_entry_safe(ibm, itmp,
1002 /*************************************************************************
1003 * sysfs support helpers
1006 static int parse_strtoul(const char *buf,
1007 unsigned long max, unsigned long *value)
1011 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1012 endp = skip_spaces(endp);
1013 if (*endp || *value > max)
1019 static void tpacpi_disable_brightness_delay(void)
1021 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1022 pr_notice("ACPI backlight control delay disabled\n");
1025 static void printk_deprecated_attribute(const char * const what,
1026 const char * const details)
1028 tpacpi_log_usertask("deprecated sysfs attribute");
1029 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1033 /*************************************************************************
1034 * rfkill and radio control support helpers
1038 * ThinkPad-ACPI firmware handling model:
1040 * WLSW (master wireless switch) is event-driven, and is common to all
1041 * firmware-controlled radios. It cannot be controlled, just monitored,
1042 * as expected. It overrides all radio state in firmware
1044 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1045 * (TODO: verify how WLSW interacts with the returned radio state).
1047 * The only time there are shadow radio state changes, is when
1048 * masked-off hotkeys are used.
1052 * Internal driver API for radio state:
1054 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1055 * bool: true means radio blocked (off)
1057 enum tpacpi_rfkill_state {
1058 TPACPI_RFK_RADIO_OFF = 0,
1062 /* rfkill switches */
1063 enum tpacpi_rfk_id {
1064 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1065 TPACPI_RFK_WWAN_SW_ID,
1066 TPACPI_RFK_UWB_SW_ID,
1070 static const char *tpacpi_rfkill_names[] = {
1071 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1072 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1073 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1074 [TPACPI_RFK_SW_MAX] = NULL
1077 /* ThinkPad-ACPI rfkill subdriver */
1079 struct rfkill *rfkill;
1080 enum tpacpi_rfk_id id;
1081 const struct tpacpi_rfk_ops *ops;
1084 struct tpacpi_rfk_ops {
1085 /* firmware interface */
1086 int (*get_status)(void);
1087 int (*set_status)(const enum tpacpi_rfkill_state);
1090 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1092 /* Query FW and update rfkill sw state for a given rfkill switch */
1093 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1100 status = (tp_rfk->ops->get_status)();
1104 rfkill_set_sw_state(tp_rfk->rfkill,
1105 (status == TPACPI_RFK_RADIO_OFF));
1111 * Sync the HW-blocking state of all rfkill switches,
1112 * do notice it causes the rfkill core to schedule uevents
1114 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1117 struct tpacpi_rfk *tp_rfk;
1119 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1120 tp_rfk = tpacpi_rfkill_switches[i];
1122 if (rfkill_set_hw_state(tp_rfk->rfkill,
1124 /* ignore -- we track sw block */
1130 /* Call to get the WLSW state from the firmware */
1131 static int hotkey_get_wlsw(void);
1133 /* Call to query WLSW state and update all rfkill switches */
1134 static bool tpacpi_rfk_check_hwblock_state(void)
1136 int res = hotkey_get_wlsw();
1139 /* When unknown or unsupported, we have to assume it is unblocked */
1143 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1144 tpacpi_rfk_update_hwblock_state(hw_blocked);
1149 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1151 struct tpacpi_rfk *tp_rfk = data;
1154 dbg_printk(TPACPI_DBG_RFKILL,
1155 "request to change radio state to %s\n",
1156 blocked ? "blocked" : "unblocked");
1158 /* try to set radio state */
1159 res = (tp_rfk->ops->set_status)(blocked ?
1160 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1162 /* and update the rfkill core with whatever the FW really did */
1163 tpacpi_rfk_update_swstate(tp_rfk);
1165 return (res < 0) ? res : 0;
1168 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1169 .set_block = tpacpi_rfk_hook_set_block,
1172 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1173 const struct tpacpi_rfk_ops *tp_rfkops,
1174 const enum rfkill_type rfktype,
1176 const bool set_default)
1178 struct tpacpi_rfk *atp_rfk;
1180 bool sw_state = false;
1184 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1186 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1188 atp_rfk->rfkill = rfkill_alloc(name,
1191 &tpacpi_rfk_rfkill_ops,
1193 if (!atp_rfk || !atp_rfk->rfkill) {
1194 pr_err("failed to allocate memory for rfkill class\n");
1200 atp_rfk->ops = tp_rfkops;
1202 sw_status = (tp_rfkops->get_status)();
1203 if (sw_status < 0) {
1204 pr_err("failed to read initial state for %s, error %d\n",
1207 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1209 /* try to keep the initial state, since we ask the
1210 * firmware to preserve it across S5 in NVRAM */
1211 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1214 hw_state = tpacpi_rfk_check_hwblock_state();
1215 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1217 res = rfkill_register(atp_rfk->rfkill);
1219 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1220 rfkill_destroy(atp_rfk->rfkill);
1225 tpacpi_rfkill_switches[id] = atp_rfk;
1227 pr_info("rfkill switch %s: radio is %sblocked\n",
1228 name, (sw_state || hw_state) ? "" : "un");
1232 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1234 struct tpacpi_rfk *tp_rfk;
1236 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1238 tp_rfk = tpacpi_rfkill_switches[id];
1240 rfkill_unregister(tp_rfk->rfkill);
1241 rfkill_destroy(tp_rfk->rfkill);
1242 tpacpi_rfkill_switches[id] = NULL;
1247 static void printk_deprecated_rfkill_attribute(const char * const what)
1249 printk_deprecated_attribute(what,
1250 "Please switch to generic rfkill before year 2010");
1253 /* sysfs <radio> enable ------------------------------------------------ */
1254 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1255 struct device_attribute *attr,
1260 printk_deprecated_rfkill_attribute(attr->attr.name);
1262 /* This is in the ABI... */
1263 if (tpacpi_rfk_check_hwblock_state()) {
1264 status = TPACPI_RFK_RADIO_OFF;
1266 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1271 return sysfs_emit(buf, "%d\n",
1272 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1275 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1276 struct device_attribute *attr,
1277 const char *buf, size_t count)
1282 printk_deprecated_rfkill_attribute(attr->attr.name);
1284 if (parse_strtoul(buf, 1, &t))
1287 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1289 /* This is in the ABI... */
1290 if (tpacpi_rfk_check_hwblock_state() && !!t)
1293 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1294 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1295 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1297 return (res < 0) ? res : count;
1300 /* procfs -------------------------------------------------------------- */
1301 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1303 if (id >= TPACPI_RFK_SW_MAX)
1304 seq_printf(m, "status:\t\tnot supported\n");
1308 /* This is in the ABI... */
1309 if (tpacpi_rfk_check_hwblock_state()) {
1310 status = TPACPI_RFK_RADIO_OFF;
1312 status = tpacpi_rfk_update_swstate(
1313 tpacpi_rfkill_switches[id]);
1318 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
1319 seq_printf(m, "commands:\tenable, disable\n");
1325 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1331 if (id >= TPACPI_RFK_SW_MAX)
1334 while ((cmd = strsep(&buf, ","))) {
1335 if (strstarts(cmd, "enable"))
1336 status = TPACPI_RFK_RADIO_ON;
1337 else if (strstarts(cmd, "disable"))
1338 status = TPACPI_RFK_RADIO_OFF;
1344 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1345 str_enable_disable(status == TPACPI_RFK_RADIO_ON),
1346 tpacpi_rfkill_names[id]);
1347 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1348 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1354 /*************************************************************************
1355 * thinkpad-acpi driver attributes
1358 /* interface_version --------------------------------------------------- */
1359 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1361 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1363 static DRIVER_ATTR_RO(interface_version);
1365 /* debug_level --------------------------------------------------------- */
1366 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1368 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1371 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1376 if (parse_strtoul(buf, 0xffff, &t))
1383 static DRIVER_ATTR_RW(debug_level);
1385 /* version ------------------------------------------------------------- */
1386 static ssize_t version_show(struct device_driver *drv, char *buf)
1388 return sysfs_emit(buf, "%s v%s\n",
1389 TPACPI_DESC, TPACPI_VERSION);
1391 static DRIVER_ATTR_RO(version);
1393 /* --------------------------------------------------------------------- */
1395 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1397 /* wlsw_emulstate ------------------------------------------------------ */
1398 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1400 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1403 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1408 if (parse_strtoul(buf, 1, &t))
1411 if (tpacpi_wlsw_emulstate != !!t) {
1412 tpacpi_wlsw_emulstate = !!t;
1413 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1418 static DRIVER_ATTR_RW(wlsw_emulstate);
1420 /* bluetooth_emulstate ------------------------------------------------- */
1421 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1423 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1426 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1427 const char *buf, size_t count)
1431 if (parse_strtoul(buf, 1, &t))
1434 tpacpi_bluetooth_emulstate = !!t;
1438 static DRIVER_ATTR_RW(bluetooth_emulstate);
1440 /* wwan_emulstate ------------------------------------------------- */
1441 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1443 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1446 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1451 if (parse_strtoul(buf, 1, &t))
1454 tpacpi_wwan_emulstate = !!t;
1458 static DRIVER_ATTR_RW(wwan_emulstate);
1460 /* uwb_emulstate ------------------------------------------------- */
1461 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1463 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1466 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1471 if (parse_strtoul(buf, 1, &t))
1474 tpacpi_uwb_emulstate = !!t;
1478 static DRIVER_ATTR_RW(uwb_emulstate);
1481 /*************************************************************************
1486 * Table of recommended minimum BIOS versions
1488 * Reasons for listing:
1489 * 1. Stable BIOS, listed because the unknown amount of
1490 * bugs and bad ACPI behaviour on older versions
1492 * 2. BIOS or EC fw with known bugs that trigger on Linux
1494 * 3. BIOS with known reduced functionality in older versions
1496 * We recommend the latest BIOS and EC version.
1497 * We only support the latest BIOS and EC fw version as a rule.
1499 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1500 * Information from users in ThinkWiki
1502 * WARNING: we use this table also to detect that the machine is
1503 * a ThinkPad in some cases, so don't remove entries lightly.
1506 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1507 { .vendor = (__v), \
1508 .bios = TPID(__id1, __id2), \
1509 .ec = TPACPI_MATCH_ANY, \
1510 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1511 | TPVER(__bv1, __bv2) }
1513 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1514 __eid, __ev1, __ev2) \
1515 { .vendor = (__v), \
1516 .bios = TPID(__bid1, __bid2), \
1518 .quirks = TPVER(__ev1, __ev2) << 16 \
1519 | TPVER(__bv1, __bv2) }
1521 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1522 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1524 /* Outdated IBM BIOSes often lack the EC id string */
1525 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1526 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1527 __bv1, __bv2, TPID(__id1, __id2), \
1529 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1530 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1533 /* Outdated IBM BIOSes often lack the EC id string */
1534 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1535 __eid1, __eid2, __ev1, __ev2) \
1536 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1537 __bv1, __bv2, TPID(__eid1, __eid2), \
1539 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1540 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1543 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1544 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1546 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1547 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1548 __bv1, __bv2, TPID(__id1, __id2), \
1551 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1552 __eid1, __eid2, __ev1, __ev2) \
1553 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1554 __bv1, __bv2, TPID(__eid1, __eid2), \
1557 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1558 /* Numeric models ------------------ */
1559 /* FW MODEL BIOS VERS */
1560 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1561 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1562 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1563 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1564 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1565 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1566 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1567 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1568 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1570 /* A-series ------------------------- */
1571 /* FW MODEL BIOS VERS EC VERS */
1572 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1573 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1574 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1575 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1576 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1577 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1578 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1579 TPV_QI0('1', '3', '2', '0'), /* A22m */
1580 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1581 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1582 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1584 /* G-series ------------------------- */
1585 /* FW MODEL BIOS VERS */
1586 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1587 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1589 /* R-series, T-series --------------- */
1590 /* FW MODEL BIOS VERS EC VERS */
1591 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1592 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1593 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1594 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1595 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1596 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1597 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1598 T40/p, T41/p, T42/p (1) */
1599 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1600 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1601 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1602 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1604 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1605 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1606 TPV_QI0('1', '6', '3', '2'), /* T22 */
1607 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1608 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1609 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1611 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1612 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1613 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1615 /* BIOS FW BIOS VERS EC FW EC VERS */
1616 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1617 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1619 /* X-series ------------------------- */
1620 /* FW MODEL BIOS VERS EC VERS */
1621 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1622 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1623 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1624 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1625 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1626 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1627 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1629 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1630 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1632 /* (0) - older versions lack DMI EC fw string and functionality */
1633 /* (1) - older versions known to lack functionality */
1644 static void __init tpacpi_check_outdated_fw(void)
1646 unsigned long fwvers;
1647 u16 ec_version, bios_version;
1649 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1650 ARRAY_SIZE(tpacpi_bios_version_qtable));
1655 bios_version = fwvers & 0xffffU;
1656 ec_version = (fwvers >> 16) & 0xffffU;
1658 /* note that unknown versions are set to 0x0000 and we use that */
1659 if ((bios_version > thinkpad_id.bios_release) ||
1660 (ec_version > thinkpad_id.ec_release &&
1661 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1663 * The changelogs would let us track down the exact
1664 * reason, but it is just too much of a pain to track
1665 * it. We only list BIOSes that are either really
1666 * broken, or really stable to begin with, so it is
1667 * best if the user upgrades the firmware anyway.
1669 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1670 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1674 static bool __init tpacpi_is_fw_known(void)
1676 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1677 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1680 /****************************************************************************
1681 ****************************************************************************
1685 ****************************************************************************
1686 ****************************************************************************/
1688 /*************************************************************************
1689 * thinkpad-acpi metadata subdriver
1692 static int thinkpad_acpi_driver_read(struct seq_file *m)
1694 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1695 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1699 static struct ibm_struct thinkpad_acpi_driver_data = {
1701 .read = thinkpad_acpi_driver_read,
1704 /*************************************************************************
1709 * ThinkPad firmware event model
1711 * The ThinkPad firmware has two main event interfaces: normal ACPI
1712 * notifications (which follow the ACPI standard), and a private event
1715 * The private event interface also issues events for the hotkeys. As
1716 * the driver gained features, the event handling code ended up being
1717 * built around the hotkey subdriver. This will need to be refactored
1718 * to a more formal event API eventually.
1720 * Some "hotkeys" are actually supposed to be used as event reports,
1721 * such as "brightness has changed", "volume has changed", depending on
1722 * the ThinkPad model and how the firmware is operating.
1724 * Unlike other classes, hotkey-class events have mask/unmask control on
1725 * non-ancient firmware. However, how it behaves changes a lot with the
1726 * firmware model and version.
1729 enum { /* hot key scan codes (derived from ACPI DSDT) */
1730 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1731 TP_ACPI_HOTKEYSCAN_FNF2,
1732 TP_ACPI_HOTKEYSCAN_FNF3,
1733 TP_ACPI_HOTKEYSCAN_FNF4,
1734 TP_ACPI_HOTKEYSCAN_FNF5,
1735 TP_ACPI_HOTKEYSCAN_FNF6,
1736 TP_ACPI_HOTKEYSCAN_FNF7,
1737 TP_ACPI_HOTKEYSCAN_FNF8,
1738 TP_ACPI_HOTKEYSCAN_FNF9,
1739 TP_ACPI_HOTKEYSCAN_FNF10,
1740 TP_ACPI_HOTKEYSCAN_FNF11,
1741 TP_ACPI_HOTKEYSCAN_FNF12,
1742 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1743 TP_ACPI_HOTKEYSCAN_FNINSERT,
1744 TP_ACPI_HOTKEYSCAN_FNDELETE,
1745 TP_ACPI_HOTKEYSCAN_FNHOME,
1746 TP_ACPI_HOTKEYSCAN_FNEND,
1747 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1748 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1749 TP_ACPI_HOTKEYSCAN_FNSPACE,
1750 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1751 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1752 TP_ACPI_HOTKEYSCAN_MUTE,
1753 TP_ACPI_HOTKEYSCAN_THINKPAD,
1754 TP_ACPI_HOTKEYSCAN_UNK1,
1755 TP_ACPI_HOTKEYSCAN_UNK2,
1756 TP_ACPI_HOTKEYSCAN_UNK3,
1757 TP_ACPI_HOTKEYSCAN_UNK4,
1758 TP_ACPI_HOTKEYSCAN_UNK5,
1759 TP_ACPI_HOTKEYSCAN_UNK6,
1760 TP_ACPI_HOTKEYSCAN_UNK7,
1761 TP_ACPI_HOTKEYSCAN_UNK8,
1763 /* Adaptive keyboard keycodes */
1764 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1765 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1766 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1767 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1768 TP_ACPI_HOTKEYSCAN_CLOUD,
1769 TP_ACPI_HOTKEYSCAN_UNK9,
1770 TP_ACPI_HOTKEYSCAN_VOICE,
1771 TP_ACPI_HOTKEYSCAN_UNK10,
1772 TP_ACPI_HOTKEYSCAN_GESTURES,
1773 TP_ACPI_HOTKEYSCAN_UNK11,
1774 TP_ACPI_HOTKEYSCAN_UNK12,
1775 TP_ACPI_HOTKEYSCAN_UNK13,
1776 TP_ACPI_HOTKEYSCAN_CONFIG,
1777 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1778 TP_ACPI_HOTKEYSCAN_RELOAD,
1779 TP_ACPI_HOTKEYSCAN_BACK,
1780 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1781 TP_ACPI_HOTKEYSCAN_MIC_UP,
1782 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1783 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1784 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1786 /* Lenovo extended keymap, starting at 0x1300 */
1787 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1788 /* first new observed key (star, favorites) is 0x1311 */
1789 TP_ACPI_HOTKEYSCAN_STAR = 69,
1790 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1791 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1792 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1793 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1794 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1795 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1796 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1797 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1799 /* Hotkey keymap size */
1800 TPACPI_HOTKEY_MAP_LEN
1803 enum { /* Keys/events available through NVRAM polling */
1804 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1805 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1808 enum { /* Positions of some of the keys in hotkey masks */
1809 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1810 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1811 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1812 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1813 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1814 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1815 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1816 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1817 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1818 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1819 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1822 enum { /* NVRAM to ACPI HKEY group map */
1823 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1824 TP_ACPI_HKEY_ZOOM_MASK |
1825 TP_ACPI_HKEY_DISPSWTCH_MASK |
1826 TP_ACPI_HKEY_HIBERNATE_MASK,
1827 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1828 TP_ACPI_HKEY_BRGHTDWN_MASK,
1829 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1830 TP_ACPI_HKEY_VOLDWN_MASK |
1831 TP_ACPI_HKEY_MUTE_MASK,
1834 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1835 struct tp_nvram_state {
1836 u16 thinkpad_toggle:1;
1838 u16 display_toggle:1;
1839 u16 thinklight_toggle:1;
1840 u16 hibernate_toggle:1;
1841 u16 displayexp_toggle:1;
1842 u16 display_state:1;
1843 u16 brightness_toggle:1;
1844 u16 volume_toggle:1;
1847 u8 brightness_level;
1851 /* kthread for the hotkey poller */
1852 static struct task_struct *tpacpi_hotkey_task;
1855 * Acquire mutex to write poller control variables as an
1858 * Increment hotkey_config_change when changing them if you
1859 * want the kthread to forget old state.
1861 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1863 static struct mutex hotkey_thread_data_mutex;
1864 static unsigned int hotkey_config_change;
1867 * hotkey poller control variables
1869 * Must be atomic or readers will also need to acquire mutex
1871 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1872 * should be used only when the changes need to be taken as
1873 * a block, OR when one needs to force the kthread to forget
1876 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1877 static unsigned int hotkey_poll_freq = 10; /* Hz */
1879 #define HOTKEY_CONFIG_CRITICAL_START \
1881 mutex_lock(&hotkey_thread_data_mutex); \
1882 hotkey_config_change++; \
1884 #define HOTKEY_CONFIG_CRITICAL_END \
1885 mutex_unlock(&hotkey_thread_data_mutex);
1887 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1889 #define hotkey_source_mask 0U
1890 #define HOTKEY_CONFIG_CRITICAL_START
1891 #define HOTKEY_CONFIG_CRITICAL_END
1893 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1895 static struct mutex hotkey_mutex;
1897 static enum { /* Reasons for waking up */
1898 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1899 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1900 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1901 } hotkey_wakeup_reason;
1903 static int hotkey_autosleep_ack;
1905 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1906 static u32 hotkey_all_mask; /* all events supported in fw */
1907 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1908 static u32 hotkey_reserved_mask; /* events better left disabled */
1909 static u32 hotkey_driver_mask; /* events needed by the driver */
1910 static u32 hotkey_user_mask; /* events visible to userspace */
1911 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1913 static u16 *hotkey_keycode_map;
1915 static void tpacpi_driver_event(const unsigned int hkey_event);
1916 static void hotkey_driver_event(const unsigned int scancode);
1917 static void hotkey_poll_setup(const bool may_warn);
1919 /* HKEY.MHKG() return bits */
1920 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1922 TP_ACPI_MULTI_MODE_INVALID = 0,
1923 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1924 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1925 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1926 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1927 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1928 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1929 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1933 /* The following modes are considered tablet mode for the purpose of
1934 * reporting the status to userspace. i.e. in all these modes it makes
1935 * sense to disable the laptop input devices such as touchpad and
1938 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1939 TP_ACPI_MULTI_MODE_STAND |
1940 TP_ACPI_MULTI_MODE_TENT |
1941 TP_ACPI_MULTI_MODE_STAND_TENT,
1944 static int hotkey_get_wlsw(void)
1948 if (!tp_features.hotkey_wlsw)
1951 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1953 return (tpacpi_wlsw_emulstate) ?
1954 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1957 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1960 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1963 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1965 int type = (s >> 16) & 0xffff;
1966 int value = s & 0xffff;
1967 int mode = TP_ACPI_MULTI_MODE_INVALID;
1968 int valid_modes = 0;
1970 if (has_tablet_mode)
1971 *has_tablet_mode = 0;
1975 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1976 TP_ACPI_MULTI_MODE_TABLET |
1977 TP_ACPI_MULTI_MODE_STAND_TENT;
1980 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1981 TP_ACPI_MULTI_MODE_FLAT |
1982 TP_ACPI_MULTI_MODE_TABLET |
1983 TP_ACPI_MULTI_MODE_STAND |
1984 TP_ACPI_MULTI_MODE_TENT;
1987 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1988 TP_ACPI_MULTI_MODE_FLAT;
1992 /* In mode 4, FLAT is not specified as a valid mode. However,
1993 * it can be seen at least on the X1 Yoga 2nd Generation.
1995 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1996 TP_ACPI_MULTI_MODE_FLAT |
1997 TP_ACPI_MULTI_MODE_TABLET |
1998 TP_ACPI_MULTI_MODE_STAND |
1999 TP_ACPI_MULTI_MODE_TENT;
2002 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2003 type, value, TPACPI_MAIL);
2007 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2008 *has_tablet_mode = 1;
2012 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2015 mode = TP_ACPI_MULTI_MODE_FLAT;
2018 mode = TP_ACPI_MULTI_MODE_TABLET;
2022 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2024 mode = TP_ACPI_MULTI_MODE_STAND;
2027 mode = TP_ACPI_MULTI_MODE_TENT;
2030 if (type == 5 && value == 0xffff) {
2031 pr_warn("Multi mode status is undetected, assuming laptop\n");
2036 if (!(mode & valid_modes)) {
2037 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2038 value, type, TPACPI_MAIL);
2042 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2045 static int hotkey_get_tablet_mode(int *status)
2049 switch (tp_features.hotkey_tablet) {
2050 case TP_HOTKEY_TABLET_USES_MHKG:
2051 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2054 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2056 case TP_HOTKEY_TABLET_USES_GMMS:
2057 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2060 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2070 * Reads current event mask from firmware, and updates
2071 * hotkey_acpi_mask accordingly. Also resets any bits
2072 * from hotkey_user_mask that are unavailable to be
2073 * delivered (shadow requirement of the userspace ABI).
2075 * Call with hotkey_mutex held
2077 static int hotkey_mask_get(void)
2079 if (tp_features.hotkey_mask) {
2082 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2085 hotkey_acpi_mask = m;
2087 /* no mask support doesn't mean no event support... */
2088 hotkey_acpi_mask = hotkey_all_mask;
2091 /* sync userspace-visible mask */
2092 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2097 static void hotkey_mask_warn_incomplete_mask(void)
2099 /* log only what the user can fix... */
2100 const u32 wantedmask = hotkey_driver_mask &
2101 ~(hotkey_acpi_mask | hotkey_source_mask) &
2102 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2105 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2109 * Set the firmware mask when supported
2111 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2113 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2115 * Call with hotkey_mutex held
2117 static int hotkey_mask_set(u32 mask)
2122 const u32 fwmask = mask & ~hotkey_source_mask;
2124 if (tp_features.hotkey_mask) {
2125 for (i = 0; i < 32; i++) {
2126 if (!acpi_evalf(hkey_handle,
2127 NULL, "MHKM", "vdd", i + 1,
2128 !!(mask & (1 << i)))) {
2136 * We *must* make an inconditional call to hotkey_mask_get to
2137 * refresh hotkey_acpi_mask and update hotkey_user_mask
2139 * Take the opportunity to also log when we cannot _enable_
2142 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2143 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2144 fwmask, hotkey_acpi_mask);
2147 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2148 hotkey_mask_warn_incomplete_mask();
2154 * Sets hotkey_user_mask and tries to set the firmware mask
2156 * Call with hotkey_mutex held
2158 static int hotkey_user_mask_set(const u32 mask)
2162 /* Give people a chance to notice they are doing something that
2163 * is bound to go boom on their users sooner or later */
2164 if (!tp_warned.hotkey_mask_ff &&
2165 (mask == 0xffff || mask == 0xffffff ||
2166 mask == 0xffffffff)) {
2167 tp_warned.hotkey_mask_ff = 1;
2168 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2170 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2173 /* Try to enable what the user asked for, plus whatever we need.
2174 * this syncs everything but won't enable bits in hotkey_user_mask */
2175 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2177 /* Enable the available bits in hotkey_user_mask */
2178 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2184 * Sets the driver hotkey mask.
2186 * Can be called even if the hotkey subdriver is inactive
2188 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2192 /* Do the right thing if hotkey_init has not been called yet */
2193 if (!tp_features.hotkey) {
2194 hotkey_driver_mask = mask;
2198 mutex_lock(&hotkey_mutex);
2200 HOTKEY_CONFIG_CRITICAL_START
2201 hotkey_driver_mask = mask;
2202 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2203 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2205 HOTKEY_CONFIG_CRITICAL_END
2207 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2208 ~hotkey_source_mask);
2209 hotkey_poll_setup(true);
2211 mutex_unlock(&hotkey_mutex);
2216 static int hotkey_status_get(int *status)
2218 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2224 static int hotkey_status_set(bool enable)
2226 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2232 static void tpacpi_input_send_tabletsw(void)
2236 if (tp_features.hotkey_tablet &&
2237 !hotkey_get_tablet_mode(&state)) {
2238 mutex_lock(&tpacpi_inputdev_send_mutex);
2240 input_report_switch(tpacpi_inputdev,
2241 SW_TABLET_MODE, !!state);
2242 input_sync(tpacpi_inputdev);
2244 mutex_unlock(&tpacpi_inputdev_send_mutex);
2248 /* Do NOT call without validating scancode first */
2249 static void tpacpi_input_send_key(const unsigned int scancode)
2251 const unsigned int keycode = hotkey_keycode_map[scancode];
2253 if (keycode != KEY_RESERVED) {
2254 mutex_lock(&tpacpi_inputdev_send_mutex);
2256 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2257 input_report_key(tpacpi_inputdev, keycode, 1);
2258 input_sync(tpacpi_inputdev);
2260 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2261 input_report_key(tpacpi_inputdev, keycode, 0);
2262 input_sync(tpacpi_inputdev);
2264 mutex_unlock(&tpacpi_inputdev_send_mutex);
2268 /* Do NOT call without validating scancode first */
2269 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2271 hotkey_driver_event(scancode);
2272 if (hotkey_user_mask & (1 << scancode))
2273 tpacpi_input_send_key(scancode);
2276 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2277 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2279 /* Do NOT call without validating scancode first */
2280 static void tpacpi_hotkey_send_key(unsigned int scancode)
2282 tpacpi_input_send_key_masked(scancode);
2285 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2289 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2290 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2291 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2292 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2293 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2294 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2296 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2297 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2298 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2300 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2302 n->displayexp_toggle =
2303 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2305 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2306 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2307 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2308 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2309 n->brightness_toggle =
2310 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2312 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2313 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2314 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2315 >> TP_NVRAM_POS_LEVEL_VOLUME;
2316 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2317 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2321 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2323 if ((event_mask & (1 << __scancode)) && \
2324 oldn->__member != newn->__member) \
2325 tpacpi_hotkey_send_key(__scancode); \
2328 #define TPACPI_MAY_SEND_KEY(__scancode) \
2330 if (event_mask & (1 << __scancode)) \
2331 tpacpi_hotkey_send_key(__scancode); \
2334 static void issue_volchange(const unsigned int oldvol,
2335 const unsigned int newvol,
2336 const u32 event_mask)
2338 unsigned int i = oldvol;
2340 while (i > newvol) {
2341 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2344 while (i < newvol) {
2345 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2350 static void issue_brightnesschange(const unsigned int oldbrt,
2351 const unsigned int newbrt,
2352 const u32 event_mask)
2354 unsigned int i = oldbrt;
2356 while (i > newbrt) {
2357 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2360 while (i < newbrt) {
2361 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2366 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2367 struct tp_nvram_state *newn,
2368 const u32 event_mask)
2371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2378 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2383 * This code is supposed to duplicate the IBM firmware behaviour:
2384 * - Pressing MUTE issues mute hotkey message, even when already mute
2385 * - Pressing Volume up/down issues volume up/down hotkey messages,
2386 * even when already at maximum or minimum volume
2387 * - The act of unmuting issues volume up/down notification,
2388 * depending which key was used to unmute
2390 * We are constrained to what the NVRAM can tell us, which is not much
2391 * and certainly not enough if more than one volume hotkey was pressed
2392 * since the last poll cycle.
2394 * Just to make our life interesting, some newer Lenovo ThinkPads have
2395 * bugs in the BIOS and may fail to update volume_toggle properly.
2400 oldn->volume_toggle != newn->volume_toggle ||
2401 oldn->volume_level != newn->volume_level) {
2402 /* recently muted, or repeated mute keypress, or
2403 * multiple presses ending in mute */
2404 issue_volchange(oldn->volume_level, newn->volume_level,
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2411 /* recently unmuted, issue 'unmute' keypress */
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2414 if (oldn->volume_level != newn->volume_level) {
2415 issue_volchange(oldn->volume_level, newn->volume_level,
2417 } else if (oldn->volume_toggle != newn->volume_toggle) {
2418 /* repeated vol up/down keypress at end of scale ? */
2419 if (newn->volume_level == 0)
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2421 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2426 /* handle brightness */
2427 if (oldn->brightness_level != newn->brightness_level) {
2428 issue_brightnesschange(oldn->brightness_level,
2429 newn->brightness_level, event_mask);
2430 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2431 /* repeated key presses that didn't change state */
2432 if (newn->brightness_level == 0)
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2434 else if (newn->brightness_level >= bright_maxlvl
2435 && !tp_features.bright_unkfw)
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2439 #undef TPACPI_COMPARE_KEY
2440 #undef TPACPI_MAY_SEND_KEY
2446 * We track all events in hotkey_source_mask all the time, since
2447 * most of them are edge-based. We only issue those requested by
2448 * hotkey_user_mask or hotkey_driver_mask, though.
2450 static int hotkey_kthread(void *data)
2452 struct tp_nvram_state s[2] = { 0 };
2453 u32 poll_mask, event_mask;
2454 unsigned int si, so;
2456 unsigned int change_detector;
2457 unsigned int poll_freq;
2460 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2469 /* Initial state for compares */
2470 mutex_lock(&hotkey_thread_data_mutex);
2471 change_detector = hotkey_config_change;
2472 poll_mask = hotkey_source_mask;
2473 event_mask = hotkey_source_mask &
2474 (hotkey_driver_mask | hotkey_user_mask);
2475 poll_freq = hotkey_poll_freq;
2476 mutex_unlock(&hotkey_thread_data_mutex);
2477 hotkey_read_nvram(&s[so], poll_mask);
2479 while (!kthread_should_stop()) {
2481 if (likely(poll_freq))
2484 t = 100; /* should never happen... */
2486 t = msleep_interruptible(t);
2487 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2490 if (t > 0 && !was_frozen)
2493 mutex_lock(&hotkey_thread_data_mutex);
2494 if (was_frozen || hotkey_config_change != change_detector) {
2495 /* forget old state on thaw or config change */
2498 change_detector = hotkey_config_change;
2500 poll_mask = hotkey_source_mask;
2501 event_mask = hotkey_source_mask &
2502 (hotkey_driver_mask | hotkey_user_mask);
2503 poll_freq = hotkey_poll_freq;
2504 mutex_unlock(&hotkey_thread_data_mutex);
2506 if (likely(poll_mask)) {
2507 hotkey_read_nvram(&s[si], poll_mask);
2508 if (likely(si != so)) {
2509 hotkey_compare_and_issue_event(&s[so], &s[si],
2522 /* call with hotkey_mutex held */
2523 static void hotkey_poll_stop_sync(void)
2525 if (tpacpi_hotkey_task) {
2526 kthread_stop(tpacpi_hotkey_task);
2527 tpacpi_hotkey_task = NULL;
2531 /* call with hotkey_mutex held */
2532 static void hotkey_poll_setup(const bool may_warn)
2534 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2535 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2537 if (hotkey_poll_freq > 0 &&
2538 (poll_driver_mask ||
2539 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2540 if (!tpacpi_hotkey_task) {
2541 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2542 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2543 if (IS_ERR(tpacpi_hotkey_task)) {
2544 tpacpi_hotkey_task = NULL;
2545 pr_err("could not create kernel thread for hotkey polling\n");
2549 hotkey_poll_stop_sync();
2550 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2551 hotkey_poll_freq == 0) {
2552 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2553 poll_user_mask, poll_driver_mask);
2558 static void hotkey_poll_setup_safe(const bool may_warn)
2560 mutex_lock(&hotkey_mutex);
2561 hotkey_poll_setup(may_warn);
2562 mutex_unlock(&hotkey_mutex);
2565 /* call with hotkey_mutex held */
2566 static void hotkey_poll_set_freq(unsigned int freq)
2569 hotkey_poll_stop_sync();
2571 hotkey_poll_freq = freq;
2574 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2576 static void hotkey_poll_setup(const bool __unused)
2580 static void hotkey_poll_setup_safe(const bool __unused)
2584 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2586 static int hotkey_inputdev_open(struct input_dev *dev)
2588 switch (tpacpi_lifecycle) {
2589 case TPACPI_LIFE_INIT:
2590 case TPACPI_LIFE_RUNNING:
2591 hotkey_poll_setup_safe(false);
2593 case TPACPI_LIFE_EXITING:
2597 /* Should only happen if tpacpi_lifecycle is corrupt */
2602 static void hotkey_inputdev_close(struct input_dev *dev)
2604 /* disable hotkey polling when possible */
2605 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2606 !(hotkey_source_mask & hotkey_driver_mask))
2607 hotkey_poll_setup_safe(false);
2610 /* sysfs hotkey enable ------------------------------------------------- */
2611 static ssize_t hotkey_enable_show(struct device *dev,
2612 struct device_attribute *attr,
2617 printk_deprecated_attribute("hotkey_enable",
2618 "Hotkey reporting is always enabled");
2620 res = hotkey_status_get(&status);
2624 return sysfs_emit(buf, "%d\n", status);
2627 static ssize_t hotkey_enable_store(struct device *dev,
2628 struct device_attribute *attr,
2629 const char *buf, size_t count)
2633 printk_deprecated_attribute("hotkey_enable",
2634 "Hotkeys can be disabled through hotkey_mask");
2636 if (parse_strtoul(buf, 1, &t))
2645 static DEVICE_ATTR_RW(hotkey_enable);
2647 /* sysfs hotkey mask --------------------------------------------------- */
2648 static ssize_t hotkey_mask_show(struct device *dev,
2649 struct device_attribute *attr,
2652 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2655 static ssize_t hotkey_mask_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2662 if (parse_strtoul(buf, 0xffffffffUL, &t))
2665 if (mutex_lock_killable(&hotkey_mutex))
2666 return -ERESTARTSYS;
2668 res = hotkey_user_mask_set(t);
2670 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2671 hotkey_poll_setup(true);
2674 mutex_unlock(&hotkey_mutex);
2676 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2678 return (res) ? res : count;
2681 static DEVICE_ATTR_RW(hotkey_mask);
2683 /* sysfs hotkey bios_enabled ------------------------------------------- */
2684 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2685 struct device_attribute *attr,
2688 return sprintf(buf, "0\n");
2691 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2693 /* sysfs hotkey bios_mask ---------------------------------------------- */
2694 static ssize_t hotkey_bios_mask_show(struct device *dev,
2695 struct device_attribute *attr,
2698 printk_deprecated_attribute("hotkey_bios_mask",
2699 "This attribute is useless.");
2700 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2703 static DEVICE_ATTR_RO(hotkey_bios_mask);
2705 /* sysfs hotkey all_mask ----------------------------------------------- */
2706 static ssize_t hotkey_all_mask_show(struct device *dev,
2707 struct device_attribute *attr,
2710 return sysfs_emit(buf, "0x%08x\n",
2711 hotkey_all_mask | hotkey_source_mask);
2714 static DEVICE_ATTR_RO(hotkey_all_mask);
2716 /* sysfs hotkey all_mask ----------------------------------------------- */
2717 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2718 struct device_attribute *attr,
2721 return sysfs_emit(buf, "0x%08x\n",
2722 hotkey_adaptive_all_mask | hotkey_source_mask);
2725 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2727 /* sysfs hotkey recommended_mask --------------------------------------- */
2728 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2732 return sysfs_emit(buf, "0x%08x\n",
2733 (hotkey_all_mask | hotkey_source_mask)
2734 & ~hotkey_reserved_mask);
2737 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2739 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2741 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2742 static ssize_t hotkey_source_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2746 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2749 static ssize_t hotkey_source_mask_store(struct device *dev,
2750 struct device_attribute *attr,
2751 const char *buf, size_t count)
2757 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2758 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2761 if (mutex_lock_killable(&hotkey_mutex))
2762 return -ERESTARTSYS;
2764 HOTKEY_CONFIG_CRITICAL_START
2765 hotkey_source_mask = t;
2766 HOTKEY_CONFIG_CRITICAL_END
2768 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2769 ~hotkey_source_mask);
2770 hotkey_poll_setup(true);
2772 /* check if events needed by the driver got disabled */
2773 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2774 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2776 mutex_unlock(&hotkey_mutex);
2779 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2782 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2785 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2787 return (rc < 0) ? rc : count;
2790 static DEVICE_ATTR_RW(hotkey_source_mask);
2792 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2793 static ssize_t hotkey_poll_freq_show(struct device *dev,
2794 struct device_attribute *attr,
2797 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2800 static ssize_t hotkey_poll_freq_store(struct device *dev,
2801 struct device_attribute *attr,
2802 const char *buf, size_t count)
2806 if (parse_strtoul(buf, 25, &t))
2809 if (mutex_lock_killable(&hotkey_mutex))
2810 return -ERESTARTSYS;
2812 hotkey_poll_set_freq(t);
2813 hotkey_poll_setup(true);
2815 mutex_unlock(&hotkey_mutex);
2817 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2822 static DEVICE_ATTR_RW(hotkey_poll_freq);
2824 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2826 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2827 static ssize_t hotkey_radio_sw_show(struct device *dev,
2828 struct device_attribute *attr,
2832 res = hotkey_get_wlsw();
2836 /* Opportunistic update */
2837 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2839 return sysfs_emit(buf, "%d\n",
2840 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2843 static DEVICE_ATTR_RO(hotkey_radio_sw);
2845 static void hotkey_radio_sw_notify_change(void)
2847 if (tp_features.hotkey_wlsw)
2848 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2852 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2853 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2854 struct device_attribute *attr,
2858 res = hotkey_get_tablet_mode(&s);
2862 return sysfs_emit(buf, "%d\n", !!s);
2865 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2867 static void hotkey_tablet_mode_notify_change(void)
2869 if (tp_features.hotkey_tablet)
2870 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2871 "hotkey_tablet_mode");
2874 /* sysfs wakeup reason (pollable) -------------------------------------- */
2875 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2876 struct device_attribute *attr,
2879 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2882 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2884 static void hotkey_wakeup_reason_notify_change(void)
2886 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2890 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2891 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2892 struct device_attribute *attr,
2895 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2898 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2899 hotkey_wakeup_hotunplug_complete_show, NULL);
2901 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "wakeup_hotunplug_complete");
2907 /* sysfs adaptive kbd mode --------------------------------------------- */
2909 static int adaptive_keyboard_get_mode(void);
2910 static int adaptive_keyboard_set_mode(int new_mode);
2912 enum ADAPTIVE_KEY_MODE {
2915 WEB_CONFERENCE_MODE,
2920 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2921 struct device_attribute *attr,
2926 current_mode = adaptive_keyboard_get_mode();
2927 if (current_mode < 0)
2928 return current_mode;
2930 return sysfs_emit(buf, "%d\n", current_mode);
2933 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2934 struct device_attribute *attr,
2935 const char *buf, size_t count)
2940 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2943 res = adaptive_keyboard_set_mode(t);
2944 return (res < 0) ? res : count;
2947 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2949 static struct attribute *adaptive_kbd_attributes[] = {
2950 &dev_attr_adaptive_kbd_mode.attr,
2954 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2955 struct attribute *attr, int n)
2957 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2960 static const struct attribute_group adaptive_kbd_attr_group = {
2961 .is_visible = hadaptive_kbd_attr_is_visible,
2962 .attrs = adaptive_kbd_attributes,
2965 /* --------------------------------------------------------------------- */
2967 static struct attribute *hotkey_attributes[] = {
2968 &dev_attr_hotkey_enable.attr,
2969 &dev_attr_hotkey_bios_enabled.attr,
2970 &dev_attr_hotkey_bios_mask.attr,
2971 &dev_attr_wakeup_reason.attr,
2972 &dev_attr_wakeup_hotunplug_complete.attr,
2973 &dev_attr_hotkey_mask.attr,
2974 &dev_attr_hotkey_all_mask.attr,
2975 &dev_attr_hotkey_adaptive_all_mask.attr,
2976 &dev_attr_hotkey_recommended_mask.attr,
2977 &dev_attr_hotkey_tablet_mode.attr,
2978 &dev_attr_hotkey_radio_sw.attr,
2979 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2980 &dev_attr_hotkey_source_mask.attr,
2981 &dev_attr_hotkey_poll_freq.attr,
2986 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2987 struct attribute *attr, int n)
2989 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2990 if (!tp_features.hotkey_tablet)
2992 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2993 if (!tp_features.hotkey_wlsw)
3000 static const struct attribute_group hotkey_attr_group = {
3001 .is_visible = hotkey_attr_is_visible,
3002 .attrs = hotkey_attributes,
3006 * Sync both the hw and sw blocking state of all switches
3008 static void tpacpi_send_radiosw_update(void)
3013 * We must sync all rfkill controllers *before* issuing any
3014 * rfkill input events, or we will race the rfkill core input
3017 * tpacpi_inputdev_send_mutex works as a synchronization point
3020 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3023 wlsw = hotkey_get_wlsw();
3025 /* Sync hw blocking state first if it is hw-blocked */
3026 if (wlsw == TPACPI_RFK_RADIO_OFF)
3027 tpacpi_rfk_update_hwblock_state(true);
3029 /* Sync hw blocking state last if it is hw-unblocked */
3030 if (wlsw == TPACPI_RFK_RADIO_ON)
3031 tpacpi_rfk_update_hwblock_state(false);
3033 /* Issue rfkill input event for WLSW switch */
3035 mutex_lock(&tpacpi_inputdev_send_mutex);
3037 input_report_switch(tpacpi_inputdev,
3038 SW_RFKILL_ALL, (wlsw > 0));
3039 input_sync(tpacpi_inputdev);
3041 mutex_unlock(&tpacpi_inputdev_send_mutex);
3045 * this can be unconditional, as we will poll state again
3046 * if userspace uses the notify to read data
3048 hotkey_radio_sw_notify_change();
3051 static void hotkey_exit(void)
3053 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3054 mutex_lock(&hotkey_mutex);
3055 hotkey_poll_stop_sync();
3056 mutex_unlock(&hotkey_mutex);
3058 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3059 "restoring original HKEY status and mask\n");
3060 /* yes, there is a bitwise or below, we want the
3061 * functions to be called even if one of them fail */
3062 if (((tp_features.hotkey_mask &&
3063 hotkey_mask_set(hotkey_orig_mask)) |
3064 hotkey_status_set(false)) != 0)
3065 pr_err("failed to restore hot key mask to BIOS defaults\n");
3068 static void __init hotkey_unmap(const unsigned int scancode)
3070 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3071 clear_bit(hotkey_keycode_map[scancode],
3072 tpacpi_inputdev->keybit);
3073 hotkey_keycode_map[scancode] = KEY_RESERVED;
3079 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3082 #define TPACPI_HK_Q_INIMASK 0x0001
3084 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3085 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3086 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3087 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3088 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3089 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3090 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3091 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3092 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3093 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3094 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3095 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3096 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3097 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3098 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3099 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3100 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3101 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3102 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3103 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3106 typedef u16 tpacpi_keymap_entry_t;
3107 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3109 static int hotkey_init_tablet_mode(void)
3111 int in_tablet_mode = 0, res;
3114 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3115 int has_tablet_mode;
3117 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3120 * The Yoga 11e series has 2 accelerometers described by a
3121 * BOSC0200 ACPI node. This setup relies on a Windows service
3122 * which calls special ACPI methods on this node to report
3123 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3124 * does not support this, so skip the hotkey on these models.
3126 if (has_tablet_mode && !dual_accel_detect())
3127 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3129 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3130 /* For X41t, X60t, X61t Tablets... */
3131 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3132 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3136 if (!tp_features.hotkey_tablet)
3139 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3140 type, in_tablet_mode ? "tablet" : "laptop");
3142 return in_tablet_mode;
3145 static int __init hotkey_init(struct ibm_init_struct *iibm)
3147 /* Requirements for changing the default keymaps:
3149 * 1. Many of the keys are mapped to KEY_RESERVED for very
3150 * good reasons. Do not change them unless you have deep
3151 * knowledge on the IBM and Lenovo ThinkPad firmware for
3152 * the various ThinkPad models. The driver behaves
3153 * differently for KEY_RESERVED: such keys have their
3154 * hot key mask *unset* in mask_recommended, and also
3155 * in the initial hot key mask programmed into the
3156 * firmware at driver load time, which means the firm-
3157 * ware may react very differently if you change them to
3160 * 2. You must be subscribed to the linux-thinkpad and
3161 * ibm-acpi-devel mailing lists, and you should read the
3162 * list archives since 2007 if you want to change the
3163 * keymaps. This requirement exists so that you will
3164 * know the past history of problems with the thinkpad-
3165 * acpi driver keymaps, and also that you will be
3166 * listening to any bug reports;
3168 * 3. Do not send thinkpad-acpi specific patches directly to
3169 * for merging, *ever*. Send them to the linux-acpi
3170 * mailinglist for comments. Merging is to be done only
3171 * through acpi-test and the ACPI maintainer.
3173 * If the above is too much to ask, don't change the keymap.
3174 * Ask the thinkpad-acpi maintainer to do it, instead.
3178 TPACPI_KEYMAP_IBM_GENERIC = 0,
3179 TPACPI_KEYMAP_LENOVO_GENERIC,
3182 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3183 /* Generic keymap for IBM ThinkPads */
3184 [TPACPI_KEYMAP_IBM_GENERIC] = {
3185 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3186 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3187 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3188 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3190 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3191 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3192 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3193 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3195 /* brightness: firmware always reacts to them */
3196 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3197 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3199 /* Thinklight: firmware always react to it */
3200 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3202 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3203 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3205 /* Volume: firmware always react to it and reprograms
3206 * the built-in *extra* mixer. Never map it to control
3207 * another mixer by default. */
3208 KEY_RESERVED, /* 0x14: VOLUME UP */
3209 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3210 KEY_RESERVED, /* 0x16: MUTE */
3212 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3214 /* (assignments unknown, please report if found) */
3215 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3218 /* No assignments, only used for Adaptive keyboards. */
3219 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
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,
3225 /* No assignment, used for newer Lenovo models */
3226 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
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
3235 /* Generic keymap for Lenovo ThinkPads */
3236 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3237 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3238 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3239 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3240 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3242 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3243 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3244 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3245 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3247 /* These should be enabled --only-- when ACPI video
3248 * is disabled (i.e. in "vendor" mode), and are handled
3249 * in a special way by the init code */
3250 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3251 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3253 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3255 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3256 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3258 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3259 * react to it and reprograms the built-in *extra* mixer.
3260 * Never map it to control another mixer by default.
3262 * T60?, T61, R60?, R61: firmware and EC tries to send
3263 * these over the regular keyboard, so these are no-ops,
3264 * but there are still weird bugs re. MUTE, so do not
3265 * change unless you get test reports from all Lenovo
3266 * models. May cause the BIOS to interfere with the
3269 KEY_RESERVED, /* 0x14: VOLUME UP */
3270 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3271 KEY_RESERVED, /* 0x16: MUTE */
3273 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3275 /* (assignments unknown, please report if found) */
3276 KEY_UNKNOWN, KEY_UNKNOWN,
3279 * The mic mute button only sends 0x1a. It does not
3280 * automatically mute the mic or change the mute light.
3282 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3284 /* (assignments unknown, please report if found) */
3287 /* Extra keys in use since the X240 / T440 / T540 */
3288 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3291 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3292 * The first item in this list is the Mute button which is
3293 * emitted with 0x103 through
3294 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3296 * We'll need to offset those by 0x20.
3298 KEY_RESERVED, /* Mute held, 0x103 */
3299 KEY_BRIGHTNESS_MIN, /* Backlight off */
3300 KEY_RESERVED, /* Clipping tool */
3301 KEY_RESERVED, /* Cloud */
3303 KEY_VOICECOMMAND, /* Voice */
3305 KEY_RESERVED, /* Gestures */
3309 KEY_CONFIG, /* Settings */
3310 KEY_RESERVED, /* New tab */
3311 KEY_REFRESH, /* Reload */
3312 KEY_BACK, /* Back */
3313 KEY_RESERVED, /* Microphone down */
3314 KEY_RESERVED, /* Microphone up */
3315 KEY_RESERVED, /* Microphone cancellation */
3316 KEY_RESERVED, /* Camera mode */
3317 KEY_RESERVED, /* Rotate display, 0x116 */
3320 * These are found in 2017 models (e.g. T470s, X270).
3321 * The lowest known value is 0x311, which according to
3322 * the manual should launch a user defined favorite
3325 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3326 * corresponding to 0x34.
3329 /* (assignments unknown, please report if found) */
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
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,
3336 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3337 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3338 KEY_CALC, /* Calculator (above numpad, P52) */
3339 KEY_BLUETOOTH, /* Bluetooth */
3340 KEY_KEYBOARD, /* Keyboard, 0x315 */
3341 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3342 KEY_NOTIFICATION_CENTER, /* Notification Center */
3343 KEY_PICKUP_PHONE, /* Answer incoming call */
3344 KEY_HANGUP_PHONE, /* Decline incoming call */
3348 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3349 /* Generic maps (fallback) */
3351 .vendor = PCI_VENDOR_ID_IBM,
3352 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3353 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3356 .vendor = PCI_VENDOR_ID_LENOVO,
3357 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3358 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3362 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3363 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3368 bool radiosw_state = false;
3369 bool tabletsw_state = false;
3371 unsigned long quirks;
3372 unsigned long keymap_id;
3374 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3375 "initializing hotkey subdriver\n");
3377 BUG_ON(!tpacpi_inputdev);
3378 BUG_ON(tpacpi_inputdev->open != NULL ||
3379 tpacpi_inputdev->close != NULL);
3381 TPACPI_ACPIHANDLE_INIT(hkey);
3382 mutex_init(&hotkey_mutex);
3384 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3385 mutex_init(&hotkey_thread_data_mutex);
3388 /* hotkey not supported on 570 */
3389 tp_features.hotkey = hkey_handle != NULL;
3391 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3393 str_supported(tp_features.hotkey));
3395 if (!tp_features.hotkey)
3398 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3399 ARRAY_SIZE(tpacpi_hotkey_qtable));
3401 tpacpi_disable_brightness_delay();
3403 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3404 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3405 for HKEY interface version 0x100 */
3406 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3407 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3408 "firmware HKEY interface version: 0x%x\n",
3411 switch (hkeyv >> 8) {
3414 * MHKV 0x100 in A31, R40, R40e,
3415 * T4x, X31, and later
3418 /* Paranoia check AND init hotkey_all_mask */
3419 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3421 pr_err("missing MHKA handler, please report this to %s\n",
3423 /* Fallback: pre-init for FN+F3,F4,F12 */
3424 hotkey_all_mask = 0x080cU;
3426 tp_features.hotkey_mask = 1;
3432 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3435 /* Paranoia check AND init hotkey_all_mask */
3436 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3438 pr_err("missing MHKA handler, please report this to %s\n",
3440 /* Fallback: pre-init for FN+F3,F4,F12 */
3441 hotkey_all_mask = 0x080cU;
3443 tp_features.hotkey_mask = 1;
3447 * Check if we have an adaptive keyboard, like on the
3448 * Lenovo Carbon X1 2014 (2nd Gen).
3450 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3452 if (hotkey_adaptive_all_mask != 0)
3453 tp_features.has_adaptive_kbd = true;
3455 tp_features.has_adaptive_kbd = false;
3456 hotkey_adaptive_all_mask = 0x0U;
3461 pr_err("unknown version of the HKEY interface: 0x%x\n",
3463 pr_err("please report this to %s\n", TPACPI_MAIL);
3468 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3469 "hotkey masks are %s\n",
3470 str_supported(tp_features.hotkey_mask));
3472 /* Init hotkey_all_mask if not initialized yet */
3473 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3474 (quirks & TPACPI_HK_Q_INIMASK))
3475 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3477 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3478 if (tp_features.hotkey_mask) {
3479 /* hotkey_source_mask *must* be zero for
3480 * the first hotkey_mask_get to return hotkey_orig_mask */
3481 res = hotkey_mask_get();
3485 hotkey_orig_mask = hotkey_acpi_mask;
3487 hotkey_orig_mask = hotkey_all_mask;
3488 hotkey_acpi_mask = hotkey_all_mask;
3491 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3493 tp_features.hotkey_wlsw = 1;
3494 radiosw_state = !!tpacpi_wlsw_emulstate;
3495 pr_info("radio switch emulation enabled\n");
3498 /* Not all thinkpads have a hardware radio switch */
3499 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3500 tp_features.hotkey_wlsw = 1;
3501 radiosw_state = !!status;
3502 pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
3505 tabletsw_state = hotkey_init_tablet_mode();
3507 /* Set up key map */
3508 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3509 ARRAY_SIZE(tpacpi_keymap_qtable));
3510 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3511 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3512 "using keymap number %lu\n", keymap_id);
3514 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3515 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3516 if (!hotkey_keycode_map) {
3517 pr_err("failed to allocate memory for key map\n");
3521 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3522 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3523 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3524 tpacpi_inputdev->keycode = hotkey_keycode_map;
3525 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3526 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3527 input_set_capability(tpacpi_inputdev, EV_KEY,
3528 hotkey_keycode_map[i]);
3530 if (i < sizeof(hotkey_reserved_mask)*8)
3531 hotkey_reserved_mask |= 1 << i;
3535 if (tp_features.hotkey_wlsw) {
3536 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3537 input_report_switch(tpacpi_inputdev,
3538 SW_RFKILL_ALL, radiosw_state);
3540 if (tp_features.hotkey_tablet) {
3541 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3542 input_report_switch(tpacpi_inputdev,
3543 SW_TABLET_MODE, tabletsw_state);
3546 /* Do not issue duplicate brightness change events to
3547 * userspace. tpacpi_detect_brightness_capabilities() must have
3548 * been called before this point */
3549 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3550 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3551 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3553 /* Disable brightness up/down on Lenovo thinkpads when
3554 * ACPI is handling them, otherwise it is plain impossible
3555 * for userspace to do something even remotely sane */
3556 hotkey_reserved_mask |=
3557 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3558 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3559 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3560 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3563 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3564 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3566 & ~hotkey_reserved_mask;
3568 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3569 "hotkey source mask 0x%08x, polling freq %u\n",
3570 hotkey_source_mask, hotkey_poll_freq);
3573 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3574 "enabling firmware HKEY event interface...\n");
3575 res = hotkey_status_set(true);
3580 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3581 | hotkey_driver_mask)
3582 & ~hotkey_source_mask);
3583 if (res < 0 && res != -ENXIO) {
3587 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3588 & ~hotkey_reserved_mask;
3589 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3590 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3591 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3593 tpacpi_inputdev->open = &hotkey_inputdev_open;
3594 tpacpi_inputdev->close = &hotkey_inputdev_close;
3596 hotkey_poll_setup_safe(true);
3601 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3602 * mode, Web conference mode, Function mode and Lay-flat mode.
3603 * We support Home mode and Function mode currently.
3605 * Will consider support rest of modes in future.
3608 static const int adaptive_keyboard_modes[] = {
3610 /* WEB_BROWSER_MODE = 2,
3611 WEB_CONFERENCE_MODE = 3, */
3615 #define DFR_CHANGE_ROW 0x101
3616 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3617 #define FIRST_ADAPTIVE_KEY 0x103
3619 /* press Fn key a while second, it will switch to Function Mode. Then
3620 * release Fn key, previous mode be restored.
3622 static bool adaptive_keyboard_mode_is_saved;
3623 static int adaptive_keyboard_prev_mode;
3625 static int adaptive_keyboard_get_mode(void)
3629 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3630 pr_err("Cannot read adaptive keyboard mode\n");
3637 static int adaptive_keyboard_set_mode(int new_mode)
3640 new_mode > LAYFLAT_MODE)
3643 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3644 pr_err("Cannot set adaptive keyboard mode\n");
3651 static int adaptive_keyboard_get_next_mode(int mode)
3654 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3656 for (i = 0; i <= max_mode; i++) {
3657 if (adaptive_keyboard_modes[i] == mode)
3666 return adaptive_keyboard_modes[i];
3669 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3671 int current_mode = 0;
3676 case DFR_CHANGE_ROW:
3677 if (adaptive_keyboard_mode_is_saved) {
3678 new_mode = adaptive_keyboard_prev_mode;
3679 adaptive_keyboard_mode_is_saved = false;
3681 current_mode = adaptive_keyboard_get_mode();
3682 if (current_mode < 0)
3684 new_mode = adaptive_keyboard_get_next_mode(
3688 if (adaptive_keyboard_set_mode(new_mode) < 0)
3693 case DFR_SHOW_QUICKVIEW_ROW:
3694 current_mode = adaptive_keyboard_get_mode();
3695 if (current_mode < 0)
3698 adaptive_keyboard_prev_mode = current_mode;
3699 adaptive_keyboard_mode_is_saved = true;
3701 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3706 if (scancode < FIRST_ADAPTIVE_KEY ||
3707 scancode >= FIRST_ADAPTIVE_KEY +
3708 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3709 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3710 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3714 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3715 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3716 if (keycode != KEY_RESERVED) {
3717 mutex_lock(&tpacpi_inputdev_send_mutex);
3719 input_report_key(tpacpi_inputdev, keycode, 1);
3720 input_sync(tpacpi_inputdev);
3722 input_report_key(tpacpi_inputdev, keycode, 0);
3723 input_sync(tpacpi_inputdev);
3725 mutex_unlock(&tpacpi_inputdev_send_mutex);
3731 static bool hotkey_notify_extended_hotkey(const u32 hkey)
3733 unsigned int scancode;
3736 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3737 case TP_HKEY_EV_AMT_TOGGLE:
3738 tpacpi_driver_event(hkey);
3742 /* Extended keycodes start at 0x300 and our offset into the map
3743 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3744 * will be positive, but might not be in the correct range.
3746 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3747 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3748 scancode < TPACPI_HOTKEY_MAP_LEN) {
3749 tpacpi_input_send_key(scancode);
3756 static bool hotkey_notify_hotkey(const u32 hkey,
3758 bool *ignore_acpi_ev)
3760 /* 0x1000-0x1FFF: key presses */
3761 unsigned int scancode = hkey & 0xfff;
3762 *send_acpi_ev = true;
3763 *ignore_acpi_ev = false;
3766 * Original events are in the 0x10XX range, the adaptive keyboard
3767 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3768 * models, additional keys are emitted through 0x13XX.
3770 switch ((hkey >> 8) & 0xf) {
3773 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3774 /* HKEY event 0x1001 is scancode 0x00 */
3776 if (!(hotkey_source_mask & (1 << scancode))) {
3777 tpacpi_input_send_key_masked(scancode);
3778 *send_acpi_ev = false;
3780 *ignore_acpi_ev = true;
3787 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3790 return hotkey_notify_extended_hotkey(hkey);
3796 static bool hotkey_notify_wakeup(const u32 hkey,
3798 bool *ignore_acpi_ev)
3800 /* 0x2000-0x2FFF: Wakeup reason */
3801 *send_acpi_ev = true;
3802 *ignore_acpi_ev = false;
3805 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3806 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3807 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3808 *ignore_acpi_ev = true;
3811 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3812 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3813 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3814 *ignore_acpi_ev = true;
3817 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3818 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3819 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3820 /* how to auto-heal: */
3821 /* 2313: woke up from S3, go to S4/S5 */
3822 /* 2413: woke up from S4, go to S5 */
3829 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3830 pr_info("woke up due to a hot-unplug request...\n");
3831 hotkey_wakeup_reason_notify_change();
3836 static bool hotkey_notify_dockevent(const u32 hkey,
3838 bool *ignore_acpi_ev)
3840 /* 0x4000-0x4FFF: dock-related events */
3841 *send_acpi_ev = true;
3842 *ignore_acpi_ev = false;
3845 case TP_HKEY_EV_UNDOCK_ACK:
3846 /* ACPI undock operation completed after wakeup */
3847 hotkey_autosleep_ack = 1;
3848 pr_info("undocked\n");
3849 hotkey_wakeup_hotunplug_complete_notify_change();
3852 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3853 pr_info("docked into hotplug port replicator\n");
3855 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3856 pr_info("undocked from hotplug port replicator\n");
3860 * Deliberately ignore attaching and detaching the keybord cover to avoid
3861 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3864 * Please refer to the following thread for more information and a preliminary
3865 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3866 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3867 * the Pico cartridge dock module:
3870 case TP_HKEY_EV_KBD_COVER_ATTACH:
3871 case TP_HKEY_EV_KBD_COVER_DETACH:
3872 *send_acpi_ev = false;
3873 *ignore_acpi_ev = true;
3881 static bool hotkey_notify_usrevent(const u32 hkey,
3883 bool *ignore_acpi_ev)
3885 /* 0x5000-0x5FFF: human interface helpers */
3886 *send_acpi_ev = true;
3887 *ignore_acpi_ev = false;
3890 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3891 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3894 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3895 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3896 tpacpi_input_send_tabletsw();
3897 hotkey_tablet_mode_notify_change();
3898 *send_acpi_ev = false;
3901 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3902 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3903 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3904 /* do not propagate these events */
3905 *ignore_acpi_ev = true;
3913 static void thermal_dump_all_sensors(void);
3914 static void palmsensor_refresh(void);
3916 static bool hotkey_notify_6xxx(const u32 hkey,
3918 bool *ignore_acpi_ev)
3920 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3921 *send_acpi_ev = true;
3922 *ignore_acpi_ev = false;
3925 case TP_HKEY_EV_THM_TABLE_CHANGED:
3926 pr_debug("EC reports: Thermal Table has changed\n");
3927 /* recommended action: do nothing, we don't have
3928 * Lenovo ATM information */
3930 case TP_HKEY_EV_THM_CSM_COMPLETED:
3931 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3932 /* Thermal event - pass on to event handler */
3933 tpacpi_driver_event(hkey);
3935 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3936 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3937 /* recommended action: do nothing, we don't have
3938 * Lenovo ATM information */
3940 case TP_HKEY_EV_ALARM_BAT_HOT:
3941 pr_crit("THERMAL ALARM: battery is too hot!\n");
3942 /* recommended action: warn user through gui */
3944 case TP_HKEY_EV_ALARM_BAT_XHOT:
3945 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3946 /* recommended action: immediate sleep/hibernate */
3948 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3949 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3950 /* recommended action: warn user through gui, that */
3951 /* some internal component is too hot */
3953 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3954 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3955 /* recommended action: immediate sleep/hibernate */
3957 case TP_HKEY_EV_AC_CHANGED:
3958 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3959 * AC status changed; can be triggered by plugging or
3960 * unplugging AC adapter, docking or undocking. */
3964 case TP_HKEY_EV_KEY_NUMLOCK:
3965 case TP_HKEY_EV_KEY_FN:
3966 /* key press events, we just ignore them as long as the EC
3967 * is still reporting them in the normal keyboard stream */
3968 *send_acpi_ev = false;
3969 *ignore_acpi_ev = true;
3972 case TP_HKEY_EV_KEY_FN_ESC:
3973 /* Get the media key status to force the status LED to update */
3974 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3975 *send_acpi_ev = false;
3976 *ignore_acpi_ev = true;
3979 case TP_HKEY_EV_TABLET_CHANGED:
3980 tpacpi_input_send_tabletsw();
3981 hotkey_tablet_mode_notify_change();
3982 *send_acpi_ev = false;
3985 case TP_HKEY_EV_PALM_DETECTED:
3986 case TP_HKEY_EV_PALM_UNDETECTED:
3987 /* palm detected - pass on to event handler */
3988 palmsensor_refresh();
3992 /* report simply as unknown, no sensor dump */
3996 thermal_dump_all_sensors();
4000 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4004 bool ignore_acpi_ev;
4007 if (event != 0x80) {
4008 pr_err("unknown HKEY notification event %d\n", event);
4009 /* forward it to userspace, maybe it knows how to handle it */
4010 acpi_bus_generate_netlink_event(
4011 ibm->acpi->device->pnp.device_class,
4012 dev_name(&ibm->acpi->device->dev),
4018 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4019 pr_err("failed to retrieve HKEY event\n");
4028 send_acpi_ev = true;
4029 ignore_acpi_ev = false;
4031 switch (hkey >> 12) {
4033 /* 0x1000-0x1FFF: key presses */
4034 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4038 /* 0x2000-0x2FFF: Wakeup reason */
4039 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4043 /* 0x3000-0x3FFF: bay-related wakeups */
4045 case TP_HKEY_EV_BAYEJ_ACK:
4046 hotkey_autosleep_ack = 1;
4047 pr_info("bay ejected\n");
4048 hotkey_wakeup_hotunplug_complete_notify_change();
4051 case TP_HKEY_EV_OPTDRV_EJ:
4052 /* FIXME: kick libata if SATA link offline */
4060 /* 0x4000-0x4FFF: dock-related events */
4061 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4065 /* 0x5000-0x5FFF: human interface helpers */
4066 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4070 /* 0x6000-0x6FFF: thermal alarms/notices and
4071 * keyboard events */
4072 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4076 /* 0x7000-0x7FFF: misc */
4077 if (tp_features.hotkey_wlsw &&
4078 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4079 tpacpi_send_radiosw_update();
4084 fallthrough; /* to default */
4089 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4090 pr_notice("please report the conditions when this event happened to %s\n",
4094 /* netlink events */
4095 if (!ignore_acpi_ev && send_acpi_ev) {
4096 acpi_bus_generate_netlink_event(
4097 ibm->acpi->device->pnp.device_class,
4098 dev_name(&ibm->acpi->device->dev),
4104 static void hotkey_suspend(void)
4106 /* Do these on suspend, we get the events on early resume! */
4107 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4108 hotkey_autosleep_ack = 0;
4110 /* save previous mode of adaptive keyboard of X1 Carbon */
4111 if (tp_features.has_adaptive_kbd) {
4112 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4114 pr_err("Cannot read adaptive keyboard mode.\n");
4119 static void hotkey_resume(void)
4121 tpacpi_disable_brightness_delay();
4123 if (hotkey_status_set(true) < 0 ||
4124 hotkey_mask_set(hotkey_acpi_mask) < 0)
4125 pr_err("error while attempting to reset the event firmware interface\n");
4127 tpacpi_send_radiosw_update();
4128 tpacpi_input_send_tabletsw();
4129 hotkey_tablet_mode_notify_change();
4130 hotkey_wakeup_reason_notify_change();
4131 hotkey_wakeup_hotunplug_complete_notify_change();
4132 hotkey_poll_setup_safe(false);
4134 /* restore previous mode of adapive keyboard of X1 Carbon */
4135 if (tp_features.has_adaptive_kbd) {
4136 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4137 adaptive_keyboard_prev_mode)) {
4138 pr_err("Cannot set adaptive keyboard mode.\n");
4143 /* procfs -------------------------------------------------------------- */
4144 static int hotkey_read(struct seq_file *m)
4148 if (!tp_features.hotkey) {
4149 seq_printf(m, "status:\t\tnot supported\n");
4153 if (mutex_lock_killable(&hotkey_mutex))
4154 return -ERESTARTSYS;
4155 res = hotkey_status_get(&status);
4157 res = hotkey_mask_get();
4158 mutex_unlock(&hotkey_mutex);
4162 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
4163 if (hotkey_all_mask) {
4164 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4165 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4167 seq_printf(m, "mask:\t\tnot supported\n");
4168 seq_printf(m, "commands:\tenable, disable, reset\n");
4174 static void hotkey_enabledisable_warn(bool enable)
4176 tpacpi_log_usertask("procfs hotkey enable/disable");
4177 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4178 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4179 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4182 static int hotkey_write(char *buf)
4188 if (!tp_features.hotkey)
4191 if (mutex_lock_killable(&hotkey_mutex))
4192 return -ERESTARTSYS;
4194 mask = hotkey_user_mask;
4197 while ((cmd = strsep(&buf, ","))) {
4198 if (strstarts(cmd, "enable")) {
4199 hotkey_enabledisable_warn(1);
4200 } else if (strstarts(cmd, "disable")) {
4201 hotkey_enabledisable_warn(0);
4203 } else if (strstarts(cmd, "reset")) {
4204 mask = (hotkey_all_mask | hotkey_source_mask)
4205 & ~hotkey_reserved_mask;
4206 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4208 } else if (sscanf(cmd, "%x", &mask) == 1) {
4217 tpacpi_disclose_usertask("procfs hotkey",
4218 "set mask to 0x%08x\n", mask);
4219 res = hotkey_user_mask_set(mask);
4223 mutex_unlock(&hotkey_mutex);
4227 static const struct acpi_device_id ibm_htk_device_ids[] = {
4228 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4229 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4230 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4234 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4235 .hid = ibm_htk_device_ids,
4236 .notify = hotkey_notify,
4237 .handle = &hkey_handle,
4238 .type = ACPI_DEVICE_NOTIFY,
4241 static struct ibm_struct hotkey_driver_data = {
4243 .read = hotkey_read,
4244 .write = hotkey_write,
4245 .exit = hotkey_exit,
4246 .resume = hotkey_resume,
4247 .suspend = hotkey_suspend,
4248 .acpi = &ibm_hotkey_acpidriver,
4251 /*************************************************************************
4252 * Bluetooth subdriver
4256 /* ACPI GBDC/SBDC bits */
4257 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4258 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4259 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4260 0 = disable, 1 = enable */
4264 /* ACPI \BLTH commands */
4265 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4266 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4267 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4268 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4269 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4272 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4274 static int bluetooth_get_status(void)
4278 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4279 if (dbg_bluetoothemul)
4280 return (tpacpi_bluetooth_emulstate) ?
4281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4284 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4287 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4288 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4291 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4295 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s bluetooth\n",
4296 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4298 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4299 if (dbg_bluetoothemul) {
4300 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4305 if (state == TPACPI_RFK_RADIO_ON)
4306 status = TP_ACPI_BLUETOOTH_RADIOSSW
4307 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4311 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4317 /* sysfs bluetooth enable ---------------------------------------------- */
4318 static ssize_t bluetooth_enable_show(struct device *dev,
4319 struct device_attribute *attr,
4322 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4326 static ssize_t bluetooth_enable_store(struct device *dev,
4327 struct device_attribute *attr,
4328 const char *buf, size_t count)
4330 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4334 static DEVICE_ATTR_RW(bluetooth_enable);
4336 /* --------------------------------------------------------------------- */
4338 static struct attribute *bluetooth_attributes[] = {
4339 &dev_attr_bluetooth_enable.attr,
4343 static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4344 struct attribute *attr, int n)
4346 return tp_features.bluetooth ? attr->mode : 0;
4349 static const struct attribute_group bluetooth_attr_group = {
4350 .is_visible = bluetooth_attr_is_visible,
4351 .attrs = bluetooth_attributes,
4354 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4355 .get_status = bluetooth_get_status,
4356 .set_status = bluetooth_set_status,
4359 static void bluetooth_shutdown(void)
4361 /* Order firmware to save current state to NVRAM */
4362 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4363 TP_ACPI_BLTH_SAVE_STATE))
4364 pr_notice("failed to save bluetooth state to NVRAM\n");
4366 vdbg_printk(TPACPI_DBG_RFKILL,
4367 "bluetooth state saved to NVRAM\n");
4370 static void bluetooth_exit(void)
4372 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4373 bluetooth_shutdown();
4376 static const struct dmi_system_id fwbug_list[] __initconst = {
4378 .ident = "ThinkPad E485",
4379 .driver_data = &quirk_btusb_bug,
4381 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4382 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4386 .ident = "ThinkPad E585",
4387 .driver_data = &quirk_btusb_bug,
4389 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4390 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4394 .ident = "ThinkPad A285 - 20MW",
4395 .driver_data = &quirk_btusb_bug,
4397 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4398 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4402 .ident = "ThinkPad A285 - 20MX",
4403 .driver_data = &quirk_btusb_bug,
4405 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4406 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4410 .ident = "ThinkPad A485 - 20MU",
4411 .driver_data = &quirk_btusb_bug,
4413 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4414 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4418 .ident = "ThinkPad A485 - 20MV",
4419 .driver_data = &quirk_btusb_bug,
4421 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4422 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4426 .ident = "L14 Gen2 AMD",
4427 .driver_data = &quirk_s2idle_bug,
4429 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4430 DMI_MATCH(DMI_PRODUCT_NAME, "20X5"),
4434 .ident = "T14s Gen2 AMD",
4435 .driver_data = &quirk_s2idle_bug,
4437 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4438 DMI_MATCH(DMI_PRODUCT_NAME, "20XF"),
4442 .ident = "X13 Gen2 AMD",
4443 .driver_data = &quirk_s2idle_bug,
4445 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4446 DMI_MATCH(DMI_PRODUCT_NAME, "20XH"),
4450 .ident = "T14 Gen2 AMD",
4451 .driver_data = &quirk_s2idle_bug,
4453 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4454 DMI_MATCH(DMI_PRODUCT_NAME, "20XK"),
4458 .ident = "T14 Gen1 AMD",
4459 .driver_data = &quirk_s2idle_bug,
4461 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4462 DMI_MATCH(DMI_PRODUCT_NAME, "20UD"),
4466 .ident = "T14 Gen1 AMD",
4467 .driver_data = &quirk_s2idle_bug,
4469 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4470 DMI_MATCH(DMI_PRODUCT_NAME, "20UE"),
4474 .ident = "T14s Gen1 AMD",
4475 .driver_data = &quirk_s2idle_bug,
4477 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4478 DMI_MATCH(DMI_PRODUCT_NAME, "20UH"),
4482 .ident = "T14s Gen1 AMD",
4483 .driver_data = &quirk_s2idle_bug,
4485 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4486 DMI_MATCH(DMI_PRODUCT_NAME, "20UJ"),
4490 .ident = "P14s Gen1 AMD",
4491 .driver_data = &quirk_s2idle_bug,
4493 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4494 DMI_MATCH(DMI_PRODUCT_NAME, "20Y1"),
4498 .ident = "P14s Gen2 AMD",
4499 .driver_data = &quirk_s2idle_bug,
4501 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4502 DMI_MATCH(DMI_PRODUCT_NAME, "21A0"),
4506 .ident = "P14s Gen2 AMD",
4507 .driver_data = &quirk_s2idle_bug,
4509 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
4510 DMI_MATCH(DMI_PRODUCT_NAME, "21A1"),
4516 #ifdef CONFIG_SUSPEND
4518 * Lenovo laptops from a variety of generations run a SMI handler during the D3->D0
4519 * transition that occurs specifically when exiting suspend to idle which can cause
4520 * large delays during resume when the IOMMU translation layer is enabled (the default
4521 * behavior) for NVME devices:
4523 * To avoid this firmware problem, skip the SMI handler on these machines before the
4524 * D0 transition occurs.
4526 static void thinkpad_acpi_amd_s2idle_restore(void)
4528 struct resource *res;
4532 res = request_mem_region_muxed(tp_features.quirks->s2idle_bug_mmio, 1,
4533 "thinkpad_acpi_pm80");
4537 addr = ioremap(tp_features.quirks->s2idle_bug_mmio, 1);
4539 goto cleanup_resource;
4541 val = ioread8(addr);
4542 iowrite8(val & ~BIT(0), addr);
4546 release_resource(res);
4550 static struct acpi_s2idle_dev_ops thinkpad_acpi_s2idle_dev_ops = {
4551 .restore = thinkpad_acpi_amd_s2idle_restore,
4555 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4556 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4557 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4558 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4563 static int __init have_bt_fwbug(void)
4566 * Some AMD based ThinkPads have a firmware bug that calling
4567 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4569 if (tp_features.quirks && tp_features.quirks->btusb_bug &&
4570 pci_dev_present(fwbug_cards_ids)) {
4571 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4572 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4578 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4583 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4584 "initializing bluetooth subdriver\n");
4586 TPACPI_ACPIHANDLE_INIT(hkey);
4588 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4589 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4590 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4591 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4593 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4594 "bluetooth is %s, status 0x%02x\n",
4595 str_supported(tp_features.bluetooth),
4598 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4599 if (dbg_bluetoothemul) {
4600 tp_features.bluetooth = 1;
4601 pr_info("bluetooth switch emulation enabled\n");
4604 if (tp_features.bluetooth &&
4605 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4606 /* no bluetooth hardware present in system */
4607 tp_features.bluetooth = 0;
4608 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4609 "bluetooth hardware not installed\n");
4612 if (!tp_features.bluetooth)
4615 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4616 &bluetooth_tprfk_ops,
4617 RFKILL_TYPE_BLUETOOTH,
4618 TPACPI_RFK_BLUETOOTH_SW_NAME,
4623 /* procfs -------------------------------------------------------------- */
4624 static int bluetooth_read(struct seq_file *m)
4626 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4629 static int bluetooth_write(char *buf)
4631 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4634 static struct ibm_struct bluetooth_driver_data = {
4635 .name = "bluetooth",
4636 .read = bluetooth_read,
4637 .write = bluetooth_write,
4638 .exit = bluetooth_exit,
4639 .shutdown = bluetooth_shutdown,
4642 /*************************************************************************
4647 /* ACPI GWAN/SWAN bits */
4648 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4649 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4650 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4651 0 = disable, 1 = enable */
4654 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4656 static int wan_get_status(void)
4660 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4662 return (tpacpi_wwan_emulstate) ?
4663 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4666 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4669 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4670 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4673 static int wan_set_status(enum tpacpi_rfkill_state state)
4677 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
4678 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4680 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4682 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4687 if (state == TPACPI_RFK_RADIO_ON)
4688 status = TP_ACPI_WANCARD_RADIOSSW
4689 | TP_ACPI_WANCARD_RESUMECTRL;
4693 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4699 /* sysfs wan enable ---------------------------------------------------- */
4700 static ssize_t wan_enable_show(struct device *dev,
4701 struct device_attribute *attr,
4704 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4708 static ssize_t wan_enable_store(struct device *dev,
4709 struct device_attribute *attr,
4710 const char *buf, size_t count)
4712 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4716 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4717 wan_enable_show, wan_enable_store);
4719 /* --------------------------------------------------------------------- */
4721 static struct attribute *wan_attributes[] = {
4722 &dev_attr_wwan_enable.attr,
4726 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4729 return tp_features.wan ? attr->mode : 0;
4732 static const struct attribute_group wan_attr_group = {
4733 .is_visible = wan_attr_is_visible,
4734 .attrs = wan_attributes,
4737 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4738 .get_status = wan_get_status,
4739 .set_status = wan_set_status,
4742 static void wan_shutdown(void)
4744 /* Order firmware to save current state to NVRAM */
4745 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4746 TP_ACPI_WGSV_SAVE_STATE))
4747 pr_notice("failed to save WWAN state to NVRAM\n");
4749 vdbg_printk(TPACPI_DBG_RFKILL,
4750 "WWAN state saved to NVRAM\n");
4753 static void wan_exit(void)
4755 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4759 static int __init wan_init(struct ibm_init_struct *iibm)
4764 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4765 "initializing wan subdriver\n");
4767 TPACPI_ACPIHANDLE_INIT(hkey);
4769 tp_features.wan = hkey_handle &&
4770 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4772 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4773 "wan is %s, status 0x%02x\n",
4774 str_supported(tp_features.wan),
4777 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4779 tp_features.wan = 1;
4780 pr_info("wwan switch emulation enabled\n");
4783 if (tp_features.wan &&
4784 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4785 /* no wan hardware present in system */
4786 tp_features.wan = 0;
4787 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4788 "wan hardware not installed\n");
4791 if (!tp_features.wan)
4794 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4797 TPACPI_RFK_WWAN_SW_NAME,
4802 /* procfs -------------------------------------------------------------- */
4803 static int wan_read(struct seq_file *m)
4805 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4808 static int wan_write(char *buf)
4810 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4813 static struct ibm_struct wan_driver_data = {
4818 .shutdown = wan_shutdown,
4821 /*************************************************************************
4826 /* ACPI GUWB/SUWB bits */
4827 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4828 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4831 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4833 static int uwb_get_status(void)
4837 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4839 return (tpacpi_uwb_emulstate) ?
4840 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4843 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4846 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4847 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4850 static int uwb_set_status(enum tpacpi_rfkill_state state)
4854 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
4855 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4857 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4859 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4864 if (state == TPACPI_RFK_RADIO_ON)
4865 status = TP_ACPI_UWB_RADIOSSW;
4869 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4875 /* --------------------------------------------------------------------- */
4877 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4878 .get_status = uwb_get_status,
4879 .set_status = uwb_set_status,
4882 static void uwb_exit(void)
4884 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4887 static int __init uwb_init(struct ibm_init_struct *iibm)
4892 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4893 "initializing uwb subdriver\n");
4895 TPACPI_ACPIHANDLE_INIT(hkey);
4897 tp_features.uwb = hkey_handle &&
4898 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4900 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4901 "uwb is %s, status 0x%02x\n",
4902 str_supported(tp_features.uwb),
4905 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4907 tp_features.uwb = 1;
4908 pr_info("uwb switch emulation enabled\n");
4911 if (tp_features.uwb &&
4912 !(status & TP_ACPI_UWB_HWPRESENT)) {
4913 /* no uwb hardware present in system */
4914 tp_features.uwb = 0;
4915 dbg_printk(TPACPI_DBG_INIT,
4916 "uwb hardware not installed\n");
4919 if (!tp_features.uwb)
4922 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4925 TPACPI_RFK_UWB_SW_NAME,
4930 static struct ibm_struct uwb_driver_data = {
4933 .flags.experimental = 1,
4936 /*************************************************************************
4940 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4942 enum video_access_mode {
4943 TPACPI_VIDEO_NONE = 0,
4944 TPACPI_VIDEO_570, /* 570 */
4945 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4946 TPACPI_VIDEO_NEW, /* all others */
4949 enum { /* video status flags, based on VIDEO_570 */
4950 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4951 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4952 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4955 enum { /* TPACPI_VIDEO_570 constants */
4956 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4957 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4958 * video_status_flags */
4959 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4960 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4963 static enum video_access_mode video_supported;
4964 static int video_orig_autosw;
4966 static int video_autosw_get(void);
4967 static int video_autosw_set(int enable);
4969 TPACPI_HANDLE(vid, root,
4970 "\\_SB.PCI.AGP.VGA", /* 570 */
4971 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4972 "\\_SB.PCI0.VID0", /* 770e */
4973 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4974 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4975 "\\_SB.PCI0.AGP.VID", /* all others */
4978 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4980 static int __init video_init(struct ibm_init_struct *iibm)
4984 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4986 TPACPI_ACPIHANDLE_INIT(vid);
4987 if (tpacpi_is_ibm())
4988 TPACPI_ACPIHANDLE_INIT(vid2);
4990 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4991 /* G41, assume IVGA doesn't change */
4992 vid_handle = vid2_handle;
4995 /* video switching not supported on R30, R31 */
4996 video_supported = TPACPI_VIDEO_NONE;
4997 else if (tpacpi_is_ibm() &&
4998 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
5000 video_supported = TPACPI_VIDEO_570;
5001 else if (tpacpi_is_ibm() &&
5002 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
5003 /* 600e/x, 770e, 770x */
5004 video_supported = TPACPI_VIDEO_770;
5007 video_supported = TPACPI_VIDEO_NEW;
5009 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5010 str_supported(video_supported != TPACPI_VIDEO_NONE),
5013 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
5016 static void video_exit(void)
5018 dbg_printk(TPACPI_DBG_EXIT,
5019 "restoring original video autoswitch mode\n");
5020 if (video_autosw_set(video_orig_autosw))
5021 pr_err("error while trying to restore original video autoswitch mode\n");
5024 static int video_outputsw_get(void)
5029 switch (video_supported) {
5030 case TPACPI_VIDEO_570:
5031 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5032 TP_ACPI_VIDEO_570_PHSCMD))
5034 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5036 case TPACPI_VIDEO_770:
5037 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5040 status |= TP_ACPI_VIDEO_S_LCD;
5041 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5044 status |= TP_ACPI_VIDEO_S_CRT;
5046 case TPACPI_VIDEO_NEW:
5047 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5048 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5051 status |= TP_ACPI_VIDEO_S_CRT;
5053 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5054 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5057 status |= TP_ACPI_VIDEO_S_LCD;
5058 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5061 status |= TP_ACPI_VIDEO_S_DVI;
5070 static int video_outputsw_set(int status)
5075 switch (video_supported) {
5076 case TPACPI_VIDEO_570:
5077 res = acpi_evalf(NULL, NULL,
5078 "\\_SB.PHS2", "vdd",
5079 TP_ACPI_VIDEO_570_PHS2CMD,
5080 status | TP_ACPI_VIDEO_570_PHS2SET);
5082 case TPACPI_VIDEO_770:
5083 autosw = video_autosw_get();
5087 res = video_autosw_set(1);
5090 res = acpi_evalf(vid_handle, NULL,
5091 "ASWT", "vdd", status * 0x100, 0);
5092 if (!autosw && video_autosw_set(autosw)) {
5093 pr_err("video auto-switch left enabled due to error\n");
5097 case TPACPI_VIDEO_NEW:
5098 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5099 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5105 return (res) ? 0 : -EIO;
5108 static int video_autosw_get(void)
5112 switch (video_supported) {
5113 case TPACPI_VIDEO_570:
5114 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5117 case TPACPI_VIDEO_770:
5118 case TPACPI_VIDEO_NEW:
5119 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5129 static int video_autosw_set(int enable)
5131 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5136 static int video_outputsw_cycle(void)
5138 int autosw = video_autosw_get();
5144 switch (video_supported) {
5145 case TPACPI_VIDEO_570:
5146 res = video_autosw_set(1);
5149 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5151 case TPACPI_VIDEO_770:
5152 case TPACPI_VIDEO_NEW:
5153 res = video_autosw_set(1);
5156 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5161 if (!autosw && video_autosw_set(autosw)) {
5162 pr_err("video auto-switch left enabled due to error\n");
5166 return (res) ? 0 : -EIO;
5169 static int video_expand_toggle(void)
5171 switch (video_supported) {
5172 case TPACPI_VIDEO_570:
5173 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5175 case TPACPI_VIDEO_770:
5176 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5178 case TPACPI_VIDEO_NEW:
5179 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5187 static int video_read(struct seq_file *m)
5191 if (video_supported == TPACPI_VIDEO_NONE) {
5192 seq_printf(m, "status:\t\tnot supported\n");
5196 /* Even reads can crash X.org, so... */
5197 if (!capable(CAP_SYS_ADMIN))
5200 status = video_outputsw_get();
5204 autosw = video_autosw_get();
5208 seq_printf(m, "status:\t\tsupported\n");
5209 seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
5210 seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
5211 if (video_supported == TPACPI_VIDEO_NEW)
5212 seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
5213 seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
5214 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5215 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5216 if (video_supported == TPACPI_VIDEO_NEW)
5217 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5218 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5219 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5224 static int video_write(char *buf)
5227 int enable, disable, status;
5230 if (video_supported == TPACPI_VIDEO_NONE)
5233 /* Even reads can crash X.org, let alone writes... */
5234 if (!capable(CAP_SYS_ADMIN))
5240 while ((cmd = strsep(&buf, ","))) {
5241 if (strstarts(cmd, "lcd_enable")) {
5242 enable |= TP_ACPI_VIDEO_S_LCD;
5243 } else if (strstarts(cmd, "lcd_disable")) {
5244 disable |= TP_ACPI_VIDEO_S_LCD;
5245 } else if (strstarts(cmd, "crt_enable")) {
5246 enable |= TP_ACPI_VIDEO_S_CRT;
5247 } else if (strstarts(cmd, "crt_disable")) {
5248 disable |= TP_ACPI_VIDEO_S_CRT;
5249 } else if (video_supported == TPACPI_VIDEO_NEW &&
5250 strstarts(cmd, "dvi_enable")) {
5251 enable |= TP_ACPI_VIDEO_S_DVI;
5252 } else if (video_supported == TPACPI_VIDEO_NEW &&
5253 strstarts(cmd, "dvi_disable")) {
5254 disable |= TP_ACPI_VIDEO_S_DVI;
5255 } else if (strstarts(cmd, "auto_enable")) {
5256 res = video_autosw_set(1);
5259 } else if (strstarts(cmd, "auto_disable")) {
5260 res = video_autosw_set(0);
5263 } else if (strstarts(cmd, "video_switch")) {
5264 res = video_outputsw_cycle();
5267 } else if (strstarts(cmd, "expand_toggle")) {
5268 res = video_expand_toggle();
5275 if (enable || disable) {
5276 status = video_outputsw_get();
5279 res = video_outputsw_set((status & ~disable) | enable);
5287 static struct ibm_struct video_driver_data = {
5290 .write = video_write,
5294 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5296 /*************************************************************************
5297 * Keyboard backlight subdriver
5300 static enum led_brightness kbdlight_brightness;
5301 static DEFINE_MUTEX(kbdlight_mutex);
5303 static int kbdlight_set_level(int level)
5310 mutex_lock(&kbdlight_mutex);
5312 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5315 kbdlight_brightness = level;
5317 mutex_unlock(&kbdlight_mutex);
5322 static int kbdlight_get_level(void)
5329 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5335 return status & 0x3;
5338 static bool kbdlight_is_supported(void)
5345 if (!acpi_has_method(hkey_handle, "MLCG")) {
5346 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5350 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5351 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5356 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5360 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5362 * Guessed test for keyboard backlight:
5364 * Machines with backlight keyboard return:
5365 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5366 * b110100010010000000XX - ThinkPad x230
5367 * b010100000010000000XX - ThinkPad x240
5368 * b010100000010000000XX - ThinkPad W541
5369 * (XX is current backlight level)
5371 * Machines without backlight keyboard return:
5372 * b10100001000000000000 - ThinkPad x230
5373 * b10110001000000000000 - ThinkPad E430
5374 * b00000000000000000000 - ThinkPad E450
5376 * Candidate BITs for detection test (XOR):
5377 * b01000000001000000000
5380 return status & BIT(9);
5383 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5384 enum led_brightness brightness)
5386 return kbdlight_set_level(brightness);
5389 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5393 level = kbdlight_get_level();
5400 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5402 .name = "tpacpi::kbd_backlight",
5403 .max_brightness = 2,
5404 .flags = LED_BRIGHT_HW_CHANGED,
5405 .brightness_set_blocking = &kbdlight_sysfs_set,
5406 .brightness_get = &kbdlight_sysfs_get,
5410 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5414 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5416 TPACPI_ACPIHANDLE_INIT(hkey);
5418 if (!kbdlight_is_supported()) {
5419 tp_features.kbdlight = 0;
5420 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5424 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5425 tp_features.kbdlight = 1;
5427 rc = led_classdev_register(&tpacpi_pdev->dev,
5428 &tpacpi_led_kbdlight.led_classdev);
5430 tp_features.kbdlight = 0;
5434 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5435 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5439 static void kbdlight_exit(void)
5441 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5444 static int kbdlight_set_level_and_update(int level)
5447 struct led_classdev *led_cdev;
5449 ret = kbdlight_set_level(level);
5450 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5452 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5453 led_cdev->brightness = level;
5458 static int kbdlight_read(struct seq_file *m)
5462 if (!tp_features.kbdlight) {
5463 seq_printf(m, "status:\t\tnot supported\n");
5465 level = kbdlight_get_level();
5467 seq_printf(m, "status:\t\terror %d\n", level);
5469 seq_printf(m, "status:\t\t%d\n", level);
5470 seq_printf(m, "commands:\t0, 1, 2\n");
5476 static int kbdlight_write(char *buf)
5479 int res, level = -EINVAL;
5481 if (!tp_features.kbdlight)
5484 while ((cmd = strsep(&buf, ","))) {
5485 res = kstrtoint(cmd, 10, &level);
5490 if (level >= 3 || level < 0)
5493 return kbdlight_set_level_and_update(level);
5496 static void kbdlight_suspend(void)
5498 struct led_classdev *led_cdev;
5500 if (!tp_features.kbdlight)
5503 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5504 led_update_brightness(led_cdev);
5505 led_classdev_suspend(led_cdev);
5508 static void kbdlight_resume(void)
5510 if (!tp_features.kbdlight)
5513 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5516 static struct ibm_struct kbdlight_driver_data = {
5518 .read = kbdlight_read,
5519 .write = kbdlight_write,
5520 .suspend = kbdlight_suspend,
5521 .resume = kbdlight_resume,
5522 .exit = kbdlight_exit,
5525 /*************************************************************************
5526 * Light (thinklight) subdriver
5529 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5530 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5532 static int light_get_status(void)
5536 if (tp_features.light_status) {
5537 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5545 static int light_set_status(int status)
5549 if (tp_features.light) {
5551 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5553 TP_CMOS_THINKLIGHT_ON :
5554 TP_CMOS_THINKLIGHT_OFF);
5556 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5559 return (rc) ? 0 : -EIO;
5565 static int light_sysfs_set(struct led_classdev *led_cdev,
5566 enum led_brightness brightness)
5568 return light_set_status((brightness != LED_OFF) ?
5569 TPACPI_LED_ON : TPACPI_LED_OFF);
5572 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5574 return (light_get_status() == 1) ? LED_ON : LED_OFF;
5577 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5579 .name = "tpacpi::thinklight",
5580 .max_brightness = 1,
5581 .brightness_set_blocking = &light_sysfs_set,
5582 .brightness_get = &light_sysfs_get,
5586 static int __init light_init(struct ibm_init_struct *iibm)
5590 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5592 if (tpacpi_is_ibm()) {
5593 TPACPI_ACPIHANDLE_INIT(ledb);
5594 TPACPI_ACPIHANDLE_INIT(lght);
5596 TPACPI_ACPIHANDLE_INIT(cmos);
5598 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5599 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5601 if (tp_features.light)
5602 /* light status not supported on
5603 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5604 tp_features.light_status =
5605 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5607 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5608 str_supported(tp_features.light),
5609 str_supported(tp_features.light_status));
5611 if (!tp_features.light)
5614 rc = led_classdev_register(&tpacpi_pdev->dev,
5615 &tpacpi_led_thinklight.led_classdev);
5618 tp_features.light = 0;
5619 tp_features.light_status = 0;
5627 static void light_exit(void)
5629 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5632 static int light_read(struct seq_file *m)
5636 if (!tp_features.light) {
5637 seq_printf(m, "status:\t\tnot supported\n");
5638 } else if (!tp_features.light_status) {
5639 seq_printf(m, "status:\t\tunknown\n");
5640 seq_printf(m, "commands:\ton, off\n");
5642 status = light_get_status();
5645 seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
5646 seq_printf(m, "commands:\ton, off\n");
5652 static int light_write(char *buf)
5657 if (!tp_features.light)
5660 while ((cmd = strsep(&buf, ","))) {
5661 if (strstarts(cmd, "on")) {
5663 } else if (strstarts(cmd, "off")) {
5669 return light_set_status(newstatus);
5672 static struct ibm_struct light_driver_data = {
5675 .write = light_write,
5679 /*************************************************************************
5683 /* sysfs cmos_command -------------------------------------------------- */
5684 static ssize_t cmos_command_store(struct device *dev,
5685 struct device_attribute *attr,
5686 const char *buf, size_t count)
5688 unsigned long cmos_cmd;
5691 if (parse_strtoul(buf, 21, &cmos_cmd))
5694 res = issue_thinkpad_cmos_command(cmos_cmd);
5695 return (res) ? res : count;
5698 static DEVICE_ATTR_WO(cmos_command);
5700 static struct attribute *cmos_attributes[] = {
5701 &dev_attr_cmos_command.attr,
5705 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5706 struct attribute *attr, int n)
5708 return cmos_handle ? attr->mode : 0;
5711 static const struct attribute_group cmos_attr_group = {
5712 .is_visible = cmos_attr_is_visible,
5713 .attrs = cmos_attributes,
5716 /* --------------------------------------------------------------------- */
5718 static int __init cmos_init(struct ibm_init_struct *iibm)
5720 vdbg_printk(TPACPI_DBG_INIT,
5721 "initializing cmos commands subdriver\n");
5723 TPACPI_ACPIHANDLE_INIT(cmos);
5725 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5726 str_supported(cmos_handle != NULL));
5728 return cmos_handle ? 0 : -ENODEV;
5731 static int cmos_read(struct seq_file *m)
5733 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5734 R30, R31, T20-22, X20-21 */
5736 seq_printf(m, "status:\t\tnot supported\n");
5738 seq_printf(m, "status:\t\tsupported\n");
5739 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5745 static int cmos_write(char *buf)
5750 while ((cmd = strsep(&buf, ","))) {
5751 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5752 cmos_cmd >= 0 && cmos_cmd <= 21) {
5757 res = issue_thinkpad_cmos_command(cmos_cmd);
5765 static struct ibm_struct cmos_driver_data = {
5768 .write = cmos_write,
5771 /*************************************************************************
5775 enum led_access_mode {
5776 TPACPI_LED_NONE = 0,
5777 TPACPI_LED_570, /* 570 */
5778 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5779 TPACPI_LED_NEW, /* all others */
5782 enum { /* For TPACPI_LED_OLD */
5783 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5784 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5785 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5788 static enum led_access_mode led_supported;
5790 static acpi_handle led_handle;
5792 #define TPACPI_LED_NUMLEDS 16
5793 static struct tpacpi_led_classdev *tpacpi_leds;
5794 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5795 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5796 /* there's a limit of 19 chars + NULL before 2.6.26 */
5798 "tpacpi:orange:batt",
5799 "tpacpi:green:batt",
5800 "tpacpi::dock_active",
5801 "tpacpi::bay_active",
5802 "tpacpi::dock_batt",
5803 "tpacpi::unknown_led",
5805 "tpacpi::dock_status1",
5806 "tpacpi::dock_status2",
5807 "tpacpi::lid_logo_dot",
5808 "tpacpi::unknown_led3",
5809 "tpacpi::thinkvantage",
5811 #define TPACPI_SAFE_LEDS 0x1481U
5813 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5815 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5818 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5822 static int led_get_status(const unsigned int led)
5825 enum led_status_t led_s;
5827 switch (led_supported) {
5828 case TPACPI_LED_570:
5829 if (!acpi_evalf(ec_handle,
5830 &status, "GLED", "dd", 1 << led))
5832 led_s = (status == 0) ?
5837 tpacpi_led_state_cache[led] = led_s;
5846 static int led_set_status(const unsigned int led,
5847 const enum led_status_t ledstatus)
5849 /* off, on, blink. Index is led_status_t */
5850 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5851 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5855 switch (led_supported) {
5856 case TPACPI_LED_570:
5858 if (unlikely(led > 7))
5860 if (unlikely(tpacpi_is_led_restricted(led)))
5862 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5863 (1 << led), led_sled_arg1[ledstatus]))
5866 case TPACPI_LED_OLD:
5867 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5868 if (unlikely(led > 7))
5870 if (unlikely(tpacpi_is_led_restricted(led)))
5872 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5874 rc = ec_write(TPACPI_LED_EC_HLBL,
5875 (ledstatus == TPACPI_LED_BLINK) << led);
5877 rc = ec_write(TPACPI_LED_EC_HLCL,
5878 (ledstatus != TPACPI_LED_OFF) << led);
5880 case TPACPI_LED_NEW:
5882 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5884 if (unlikely(tpacpi_is_led_restricted(led)))
5886 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5887 led, led_led_arg1[ledstatus]))
5895 tpacpi_led_state_cache[led] = ledstatus;
5900 static int led_sysfs_set(struct led_classdev *led_cdev,
5901 enum led_brightness brightness)
5903 struct tpacpi_led_classdev *data = container_of(led_cdev,
5904 struct tpacpi_led_classdev, led_classdev);
5905 enum led_status_t new_state;
5907 if (brightness == LED_OFF)
5908 new_state = TPACPI_LED_OFF;
5909 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5910 new_state = TPACPI_LED_ON;
5912 new_state = TPACPI_LED_BLINK;
5914 return led_set_status(data->led, new_state);
5917 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5918 unsigned long *delay_on, unsigned long *delay_off)
5920 struct tpacpi_led_classdev *data = container_of(led_cdev,
5921 struct tpacpi_led_classdev, led_classdev);
5923 /* Can we choose the flash rate? */
5924 if (*delay_on == 0 && *delay_off == 0) {
5925 /* yes. set them to the hardware blink rate (1 Hz) */
5926 *delay_on = 500; /* ms */
5927 *delay_off = 500; /* ms */
5928 } else if ((*delay_on != 500) || (*delay_off != 500))
5931 return led_set_status(data->led, TPACPI_LED_BLINK);
5934 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5938 struct tpacpi_led_classdev *data = container_of(led_cdev,
5939 struct tpacpi_led_classdev, led_classdev);
5941 rc = led_get_status(data->led);
5943 if (rc == TPACPI_LED_OFF || rc < 0)
5944 rc = LED_OFF; /* no error handling in led class :( */
5951 static void led_exit(void)
5955 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5956 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5961 static int __init tpacpi_init_led(unsigned int led)
5963 /* LEDs with no name don't get registered */
5964 if (!tpacpi_led_names[led])
5967 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5968 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5969 if (led_supported == TPACPI_LED_570)
5970 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5972 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5973 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5974 tpacpi_leds[led].led = led;
5976 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5979 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5980 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5981 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5982 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5984 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5985 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5986 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5987 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5988 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5989 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5990 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5991 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5993 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5994 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5995 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5996 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5997 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5999 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6000 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6001 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6002 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6004 /* (1) - may have excess leds enabled on MSB */
6006 /* Defaults (order matters, keep last, don't reorder!) */
6008 .vendor = PCI_VENDOR_ID_LENOVO,
6009 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6012 { /* IBM ThinkPads with no EC version string */
6013 .vendor = PCI_VENDOR_ID_IBM,
6014 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6017 { /* IBM ThinkPads with EC version string */
6018 .vendor = PCI_VENDOR_ID_IBM,
6019 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6024 static enum led_access_mode __init led_init_detect_mode(void)
6028 if (tpacpi_is_ibm()) {
6030 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6031 if (ACPI_SUCCESS(status))
6032 return TPACPI_LED_570;
6034 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6035 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6036 if (ACPI_SUCCESS(status))
6037 return TPACPI_LED_OLD;
6041 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6042 if (ACPI_SUCCESS(status))
6043 return TPACPI_LED_NEW;
6045 /* R30, R31, and unknown firmwares */
6047 return TPACPI_LED_NONE;
6050 static int __init led_init(struct ibm_init_struct *iibm)
6054 unsigned long useful_leds;
6056 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6058 led_supported = led_init_detect_mode();
6060 if (led_supported != TPACPI_LED_NONE) {
6061 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6062 ARRAY_SIZE(led_useful_qtable));
6066 led_supported = TPACPI_LED_NONE;
6070 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6071 str_supported(led_supported), led_supported);
6073 if (led_supported == TPACPI_LED_NONE)
6076 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6079 pr_err("Out of memory for LED data\n");
6083 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6084 tpacpi_leds[i].led = -1;
6086 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
6087 rc = tpacpi_init_led(i);
6095 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6096 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6101 #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
6103 static int led_read(struct seq_file *m)
6105 if (!led_supported) {
6106 seq_printf(m, "status:\t\tnot supported\n");
6109 seq_printf(m, "status:\t\tsupported\n");
6111 if (led_supported == TPACPI_LED_570) {
6114 for (i = 0; i < 8; i++) {
6115 status = led_get_status(i);
6118 seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
6122 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6127 static int led_write(char *buf)
6131 enum led_status_t s;
6136 while ((cmd = strsep(&buf, ","))) {
6137 if (sscanf(cmd, "%d", &led) != 1)
6140 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6143 if (tpacpi_leds[led].led < 0)
6146 if (strstr(cmd, "off")) {
6148 } else if (strstr(cmd, "on")) {
6150 } else if (strstr(cmd, "blink")) {
6151 s = TPACPI_LED_BLINK;
6156 rc = led_set_status(led, s);
6164 static struct ibm_struct led_driver_data = {
6171 /*************************************************************************
6175 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6177 #define TPACPI_BEEP_Q1 0x0001
6179 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6180 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6181 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6184 static int __init beep_init(struct ibm_init_struct *iibm)
6186 unsigned long quirks;
6188 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6190 TPACPI_ACPIHANDLE_INIT(beep);
6192 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6193 str_supported(beep_handle != NULL));
6195 quirks = tpacpi_check_quirks(beep_quirk_table,
6196 ARRAY_SIZE(beep_quirk_table));
6198 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6200 return (beep_handle) ? 0 : -ENODEV;
6203 static int beep_read(struct seq_file *m)
6206 seq_printf(m, "status:\t\tnot supported\n");
6208 seq_printf(m, "status:\t\tsupported\n");
6209 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6215 static int beep_write(char *buf)
6223 while ((cmd = strsep(&buf, ","))) {
6224 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6225 beep_cmd >= 0 && beep_cmd <= 17) {
6229 if (tp_features.beep_needs_two_args) {
6230 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6234 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6243 static struct ibm_struct beep_driver_data = {
6246 .write = beep_write,
6249 /*************************************************************************
6253 enum thermal_access_mode {
6254 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6255 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6256 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6257 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6258 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6261 enum { /* TPACPI_THERMAL_TPEC_* */
6262 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6263 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6264 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6265 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6267 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6271 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6272 struct ibm_thermal_sensors_struct {
6273 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6276 static enum thermal_access_mode thermal_read_mode;
6277 static bool thermal_use_labels;
6279 /* idx is zero-based */
6280 static int thermal_get_sensor(int idx, s32 *value)
6286 t = TP_EC_THERMAL_TMP0;
6288 switch (thermal_read_mode) {
6289 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6290 case TPACPI_THERMAL_TPEC_16:
6291 if (idx >= 8 && idx <= 15) {
6292 t = TP_EC_THERMAL_TMP8;
6297 case TPACPI_THERMAL_TPEC_8:
6299 if (!acpi_ec_read(t + idx, &tmp))
6301 *value = tmp * 1000;
6306 case TPACPI_THERMAL_ACPI_UPDT:
6308 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6309 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6311 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6313 *value = (t - 2732) * 100;
6318 case TPACPI_THERMAL_ACPI_TMP07:
6320 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6321 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6323 if (t > 127 || t < -127)
6324 t = TP_EC_THERMAL_TMP_NA;
6330 case TPACPI_THERMAL_NONE:
6338 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6349 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6352 for (i = 0 ; i < n; i++) {
6353 res = thermal_get_sensor(i, &s->temp[i]);
6361 static void thermal_dump_all_sensors(void)
6364 struct ibm_thermal_sensors_struct t;
6366 n = thermal_get_sensors(&t);
6370 pr_notice("temperatures (Celsius):");
6372 for (i = 0; i < n; i++) {
6373 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6374 pr_cont(" %d", (int)(t.temp[i] / 1000));
6382 /* sysfs temp##_input -------------------------------------------------- */
6384 static ssize_t thermal_temp_input_show(struct device *dev,
6385 struct device_attribute *attr,
6388 struct sensor_device_attribute *sensor_attr =
6389 to_sensor_dev_attr(attr);
6390 int idx = sensor_attr->index;
6394 res = thermal_get_sensor(idx, &value);
6397 if (value == TPACPI_THERMAL_SENSOR_NA)
6400 return sysfs_emit(buf, "%d\n", value);
6403 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6404 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6405 thermal_temp_input_show, NULL, _idxB)
6407 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6408 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6409 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6410 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6411 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6412 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6413 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6414 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6415 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6416 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6417 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6418 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6419 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6420 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6421 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6422 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6423 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6426 #define THERMAL_ATTRS(X) \
6427 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6429 static struct attribute *thermal_temp_input_attr[] = {
6449 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6450 struct attribute *attr, int n)
6452 if (thermal_read_mode == TPACPI_THERMAL_NONE)
6455 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
6456 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
6457 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
6458 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
6459 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
6466 static const struct attribute_group thermal_attr_group = {
6467 .is_visible = thermal_attr_is_visible,
6468 .attrs = thermal_temp_input_attr,
6471 #undef THERMAL_SENSOR_ATTR_TEMP
6472 #undef THERMAL_ATTRS
6474 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6476 return sysfs_emit(buf, "CPU\n");
6478 static DEVICE_ATTR_RO(temp1_label);
6480 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6482 return sysfs_emit(buf, "GPU\n");
6484 static DEVICE_ATTR_RO(temp2_label);
6486 static struct attribute *temp_label_attributes[] = {
6487 &dev_attr_temp1_label.attr,
6488 &dev_attr_temp2_label.attr,
6492 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6493 struct attribute *attr, int n)
6495 return thermal_use_labels ? attr->mode : 0;
6498 static const struct attribute_group temp_label_attr_group = {
6499 .is_visible = temp_label_attr_is_visible,
6500 .attrs = temp_label_attributes,
6503 /* --------------------------------------------------------------------- */
6505 static int __init thermal_init(struct ibm_init_struct *iibm)
6507 u8 t, ta1, ta2, ver = 0;
6511 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6513 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6515 if (thinkpad_id.ec_model) {
6517 * Direct EC access mode: sensors at registers
6518 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6519 * non-implemented, thermal sensors return 0x80 when
6521 * The above rule is unfortunately flawed. This has been seen with
6522 * 0xC2 (power supply ID) causing thermal control problems.
6523 * The EC version can be determined by offset 0xEF and at least for
6524 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6525 * are not thermal registers.
6527 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6528 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6531 for (i = 0; i < 8; i++) {
6532 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6539 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6548 /* This is sheer paranoia, but we handle it anyway */
6550 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6551 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6553 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6554 thermal_read_mode = TPACPI_THERMAL_NONE;
6558 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6559 thermal_use_labels = true;
6563 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6566 } else if (acpi_tmp7) {
6567 if (tpacpi_is_ibm() &&
6568 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6569 /* 600e/x, 770e, 770x */
6570 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6572 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6573 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6576 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6577 thermal_read_mode = TPACPI_THERMAL_NONE;
6580 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6581 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6584 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6587 static int thermal_read(struct seq_file *m)
6590 struct ibm_thermal_sensors_struct t;
6592 n = thermal_get_sensors(&t);
6593 if (unlikely(n < 0))
6596 seq_printf(m, "temperatures:\t");
6599 for (i = 0; i < (n - 1); i++)
6600 seq_printf(m, "%d ", t.temp[i] / 1000);
6601 seq_printf(m, "%d\n", t.temp[i] / 1000);
6603 seq_printf(m, "not supported\n");
6608 static struct ibm_struct thermal_driver_data = {
6610 .read = thermal_read,
6613 /*************************************************************************
6614 * Backlight/brightness subdriver
6617 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6620 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6621 * CMOS NVRAM byte 0x5E, bits 0-3.
6623 * EC HBRV (0x31) has the following layout
6624 * Bit 7: unknown function
6625 * Bit 6: unknown function
6626 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6627 * Bit 4: must be set to zero to avoid problems
6628 * Bit 3-0: backlight brightness level
6630 * brightness_get_raw returns status data in the HBRV layout
6632 * WARNING: The X61 has been verified to use HBRV for something else, so
6633 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6634 * testing on the very early *60 Lenovo models...
6638 TP_EC_BACKLIGHT = 0x31,
6640 /* TP_EC_BACKLIGHT bitmasks */
6641 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6642 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6643 TP_EC_BACKLIGHT_MAPSW = 0x20,
6646 enum tpacpi_brightness_access_mode {
6647 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6648 TPACPI_BRGHT_MODE_EC, /* EC control */
6649 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6650 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6651 TPACPI_BRGHT_MODE_MAX
6654 static struct backlight_device *ibm_backlight_device;
6656 static enum tpacpi_brightness_access_mode brightness_mode =
6657 TPACPI_BRGHT_MODE_MAX;
6659 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6661 static struct mutex brightness_mutex;
6663 /* NVRAM brightness access,
6664 * call with brightness_mutex held! */
6665 static unsigned int tpacpi_brightness_nvram_get(void)
6669 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6670 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6671 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6672 lnvram &= bright_maxlvl;
6677 static void tpacpi_brightness_checkpoint_nvram(void)
6682 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6685 vdbg_printk(TPACPI_DBG_BRGHT,
6686 "trying to checkpoint backlight level to NVRAM...\n");
6688 if (mutex_lock_killable(&brightness_mutex) < 0)
6691 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6693 lec &= TP_EC_BACKLIGHT_LVLMSK;
6694 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6696 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6697 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6698 /* NVRAM needs update */
6699 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6700 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6702 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6703 dbg_printk(TPACPI_DBG_BRGHT,
6704 "updated NVRAM backlight level to %u (0x%02x)\n",
6705 (unsigned int) lec, (unsigned int) b_nvram);
6707 vdbg_printk(TPACPI_DBG_BRGHT,
6708 "NVRAM backlight level already is %u (0x%02x)\n",
6709 (unsigned int) lec, (unsigned int) b_nvram);
6712 mutex_unlock(&brightness_mutex);
6716 /* call with brightness_mutex held! */
6717 static int tpacpi_brightness_get_raw(int *status)
6721 switch (brightness_mode) {
6722 case TPACPI_BRGHT_MODE_UCMS_STEP:
6723 *status = tpacpi_brightness_nvram_get();
6725 case TPACPI_BRGHT_MODE_EC:
6726 case TPACPI_BRGHT_MODE_ECNVRAM:
6727 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6736 /* call with brightness_mutex held! */
6737 /* do NOT call with illegal backlight level value */
6738 static int tpacpi_brightness_set_ec(unsigned int value)
6742 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6745 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6746 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6747 (value & TP_EC_BACKLIGHT_LVLMSK))))
6753 /* call with brightness_mutex held! */
6754 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6757 unsigned int current_value, i;
6759 current_value = tpacpi_brightness_nvram_get();
6761 if (value == current_value)
6764 cmos_cmd = (value > current_value) ?
6765 TP_CMOS_BRIGHTNESS_UP :
6766 TP_CMOS_BRIGHTNESS_DOWN;
6767 inc = (value > current_value) ? 1 : -1;
6769 for (i = current_value; i != value; i += inc)
6770 if (issue_thinkpad_cmos_command(cmos_cmd))
6776 /* May return EINTR which can always be mapped to ERESTARTSYS */
6777 static int brightness_set(unsigned int value)
6781 if (value > bright_maxlvl)
6784 vdbg_printk(TPACPI_DBG_BRGHT,
6785 "set backlight level to %d\n", value);
6787 res = mutex_lock_killable(&brightness_mutex);
6791 switch (brightness_mode) {
6792 case TPACPI_BRGHT_MODE_EC:
6793 case TPACPI_BRGHT_MODE_ECNVRAM:
6794 res = tpacpi_brightness_set_ec(value);
6796 case TPACPI_BRGHT_MODE_UCMS_STEP:
6797 res = tpacpi_brightness_set_ucmsstep(value);
6803 mutex_unlock(&brightness_mutex);
6807 /* sysfs backlight class ----------------------------------------------- */
6809 static int brightness_update_status(struct backlight_device *bd)
6811 int level = backlight_get_brightness(bd);
6813 dbg_printk(TPACPI_DBG_BRGHT,
6814 "backlight: attempt to set level to %d\n",
6817 /* it is the backlight class's job (caller) to handle
6818 * EINTR and other errors properly */
6819 return brightness_set(level);
6822 static int brightness_get(struct backlight_device *bd)
6826 res = mutex_lock_killable(&brightness_mutex);
6830 res = tpacpi_brightness_get_raw(&status);
6832 mutex_unlock(&brightness_mutex);
6837 return status & TP_EC_BACKLIGHT_LVLMSK;
6840 static void tpacpi_brightness_notify_change(void)
6842 backlight_force_update(ibm_backlight_device,
6843 BACKLIGHT_UPDATE_HOTKEY);
6846 static const struct backlight_ops ibm_backlight_data = {
6847 .get_brightness = brightness_get,
6848 .update_status = brightness_update_status,
6851 /* --------------------------------------------------------------------- */
6853 static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
6855 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6856 union acpi_object *obj;
6860 status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
6861 if (ACPI_FAILURE(status))
6864 obj = buffer.pointer;
6865 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
6866 acpi_handle_info(adev->handle,
6867 "Unknown _BCL data, please report this to %s\n",
6871 rc = obj->package.count;
6879 * Call _BCL method of video device. On some ThinkPads this will
6880 * switch the firmware to the ACPI brightness control mode.
6883 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6885 struct acpi_device *device;
6887 device = acpi_fetch_acpi_dev(handle);
6891 return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
6896 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6898 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6900 acpi_handle video_device;
6903 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6905 bcl_levels = tpacpi_query_bcl_levels(video_device);
6907 tp_features.bright_acpimode = (bcl_levels > 0);
6909 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6913 * These are only useful for models that have only one possibility
6914 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6917 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6918 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6919 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6921 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6922 /* Models with ATI GPUs known to require ECNVRAM mode */
6923 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6925 /* Models with ATI GPUs that can use ECNVRAM */
6926 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6927 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6928 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6929 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6931 /* Models with Intel Extreme Graphics 2 */
6932 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6933 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6934 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6936 /* Models with Intel GMA900 */
6937 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6938 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6939 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6943 * Returns < 0 for error, otherwise sets tp_features.bright_*
6944 * and bright_maxlvl.
6946 static void __init tpacpi_detect_brightness_capabilities(void)
6950 vdbg_printk(TPACPI_DBG_INIT,
6951 "detecting firmware brightness interface capabilities\n");
6953 /* we could run a quirks check here (same table used by
6954 * brightness_init) if needed */
6957 * We always attempt to detect acpi support, so as to switch
6958 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6959 * going to publish a backlight interface
6961 b = tpacpi_check_std_acpi_brightness_support();
6971 tp_features.bright_unkfw = 1;
6972 bright_maxlvl = b - 1;
6974 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6977 static int __init brightness_init(struct ibm_init_struct *iibm)
6979 struct backlight_properties props;
6981 unsigned long quirks;
6983 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6985 mutex_init(&brightness_mutex);
6987 quirks = tpacpi_check_quirks(brightness_quirk_table,
6988 ARRAY_SIZE(brightness_quirk_table));
6990 /* tpacpi_detect_brightness_capabilities() must have run already */
6992 /* if it is unknown, we don't handle it: it wouldn't be safe */
6993 if (tp_features.bright_unkfw)
6996 if (!brightness_enable) {
6997 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6998 "brightness support disabled by module parameter\n");
7002 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
7003 if (brightness_enable > 1) {
7004 pr_info("Standard ACPI backlight interface available, not loading native one\n");
7006 } else if (brightness_enable == 1) {
7007 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
7010 } else if (!tp_features.bright_acpimode) {
7011 pr_notice("ACPI backlight interface not available\n");
7015 pr_notice("ACPI native brightness control enabled\n");
7018 * Check for module parameter bogosity, note that we
7019 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7020 * able to detect "unspecified"
7022 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7025 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7026 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7027 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7028 if (quirks & TPACPI_BRGHT_Q_EC)
7029 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7031 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7033 dbg_printk(TPACPI_DBG_BRGHT,
7034 "driver auto-selected brightness_mode=%d\n",
7039 if (!tpacpi_is_ibm() &&
7040 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7041 brightness_mode == TPACPI_BRGHT_MODE_EC))
7044 if (tpacpi_brightness_get_raw(&b) < 0)
7047 memset(&props, 0, sizeof(struct backlight_properties));
7048 props.type = BACKLIGHT_PLATFORM;
7049 props.max_brightness = bright_maxlvl;
7050 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7051 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7053 &ibm_backlight_data,
7055 if (IS_ERR(ibm_backlight_device)) {
7056 int rc = PTR_ERR(ibm_backlight_device);
7057 ibm_backlight_device = NULL;
7058 pr_err("Could not register backlight device\n");
7061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7062 "brightness is supported\n");
7064 if (quirks & TPACPI_BRGHT_Q_ASK) {
7065 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7067 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7071 /* Added by mistake in early 2007. Probably useless, but it could
7072 * be working around some unknown firmware problem where the value
7073 * read at startup doesn't match the real hardware state... so leave
7074 * it in place just in case */
7075 backlight_update_status(ibm_backlight_device);
7077 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7078 "brightness: registering brightness hotkeys as change notification\n");
7079 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7080 | TP_ACPI_HKEY_BRGHTUP_MASK
7081 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7085 static void brightness_suspend(void)
7087 tpacpi_brightness_checkpoint_nvram();
7090 static void brightness_shutdown(void)
7092 tpacpi_brightness_checkpoint_nvram();
7095 static void brightness_exit(void)
7097 if (ibm_backlight_device) {
7098 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7099 "calling backlight_device_unregister()\n");
7100 backlight_device_unregister(ibm_backlight_device);
7103 tpacpi_brightness_checkpoint_nvram();
7106 static int brightness_read(struct seq_file *m)
7110 level = brightness_get(NULL);
7112 seq_printf(m, "level:\t\tunreadable\n");
7114 seq_printf(m, "level:\t\t%d\n", level);
7115 seq_printf(m, "commands:\tup, down\n");
7116 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7123 static int brightness_write(char *buf)
7129 level = brightness_get(NULL);
7133 while ((cmd = strsep(&buf, ","))) {
7134 if (strstarts(cmd, "up")) {
7135 if (level < bright_maxlvl)
7137 } else if (strstarts(cmd, "down")) {
7140 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7141 level >= 0 && level <= bright_maxlvl) {
7147 tpacpi_disclose_usertask("procfs brightness",
7148 "set level to %d\n", level);
7151 * Now we know what the final level should be, so we try to set it.
7152 * Doing it this way makes the syscall restartable in case of EINTR
7154 rc = brightness_set(level);
7155 if (!rc && ibm_backlight_device)
7156 backlight_force_update(ibm_backlight_device,
7157 BACKLIGHT_UPDATE_SYSFS);
7158 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7161 static struct ibm_struct brightness_driver_data = {
7162 .name = "brightness",
7163 .read = brightness_read,
7164 .write = brightness_write,
7165 .exit = brightness_exit,
7166 .suspend = brightness_suspend,
7167 .shutdown = brightness_shutdown,
7170 /*************************************************************************
7175 * IBM ThinkPads have a simple volume controller with MUTE gating.
7176 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7178 * Since the *61 series (and probably also the later *60 series), Lenovo
7179 * ThinkPads only implement the MUTE gate.
7182 * Bit 6: MUTE (1 mutes sound)
7184 * Other bits should be zero as far as we know.
7186 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7187 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7188 * such as bit 7 which is used to detect repeated presses of MUTE,
7189 * and we leave them unchanged.
7191 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7192 * in response to user input. Unfortunately, this rarely works well.
7193 * The laptop changes the state of its internal MUTE gate and, on some
7194 * models, sends KEY_MUTE, causing any user code that responds to the
7195 * mute button to get confused. The hardware MUTE gate is also
7196 * unnecessary, since user code can handle the mute button without
7197 * kernel or EC help.
7199 * To avoid confusing userspace, we simply disable all EC-based mute
7200 * and volume controls when possible.
7203 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7205 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7206 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7207 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7209 #if SNDRV_CARDS <= 32
7210 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7212 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7214 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7215 static char *alsa_id = "ThinkPadEC";
7216 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7218 struct tpacpi_alsa_data {
7219 struct snd_card *card;
7220 struct snd_ctl_elem_id *ctl_mute_id;
7221 struct snd_ctl_elem_id *ctl_vol_id;
7224 static struct snd_card *alsa_card;
7229 /* TP_EC_AUDIO bits */
7230 TP_EC_AUDIO_MUTESW = 6,
7232 /* TP_EC_AUDIO bitmasks */
7233 TP_EC_AUDIO_LVL_MSK = 0x0F,
7234 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7236 /* Maximum volume */
7237 TP_EC_VOLUME_MAX = 14,
7240 enum tpacpi_volume_access_mode {
7241 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7242 TPACPI_VOL_MODE_EC, /* Pure EC control */
7243 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7244 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7248 enum tpacpi_volume_capabilities {
7249 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7250 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7251 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7255 enum tpacpi_mute_btn_mode {
7256 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7257 /* We don't know what mode 1 is. */
7258 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7259 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7262 static enum tpacpi_volume_access_mode volume_mode =
7263 TPACPI_VOL_MODE_MAX;
7265 static enum tpacpi_volume_capabilities volume_capabilities;
7266 static bool volume_control_allowed;
7267 static bool software_mute_requested = true;
7268 static bool software_mute_active;
7269 static int software_mute_orig_mode;
7272 * Used to syncronize writers to TP_EC_AUDIO and
7273 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7275 static struct mutex volume_mutex;
7277 static void tpacpi_volume_checkpoint_nvram(void)
7283 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7285 if (!volume_control_allowed)
7287 if (software_mute_active)
7290 vdbg_printk(TPACPI_DBG_MIXER,
7291 "trying to checkpoint mixer state to NVRAM...\n");
7293 if (tp_features.mixer_no_level_control)
7294 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7296 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7298 if (mutex_lock_killable(&volume_mutex) < 0)
7301 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7304 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7306 if (lec != (b_nvram & ec_mask)) {
7307 /* NVRAM needs update */
7308 b_nvram &= ~ec_mask;
7310 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7311 dbg_printk(TPACPI_DBG_MIXER,
7312 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7313 (unsigned int) lec, (unsigned int) b_nvram);
7315 vdbg_printk(TPACPI_DBG_MIXER,
7316 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7317 (unsigned int) lec, (unsigned int) b_nvram);
7321 mutex_unlock(&volume_mutex);
7324 static int volume_get_status_ec(u8 *status)
7328 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7333 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7338 static int volume_get_status(u8 *status)
7340 return volume_get_status_ec(status);
7343 static int volume_set_status_ec(const u8 status)
7345 if (!acpi_ec_write(TP_EC_AUDIO, status))
7348 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7351 * On X200s, and possibly on others, it can take a while for
7352 * reads to become correct.
7359 static int volume_set_status(const u8 status)
7361 return volume_set_status_ec(status);
7364 /* returns < 0 on error, 0 on no change, 1 on change */
7365 static int __volume_set_mute_ec(const bool mute)
7370 if (mutex_lock_killable(&volume_mutex) < 0)
7373 rc = volume_get_status_ec(&s);
7377 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7378 s & ~TP_EC_AUDIO_MUTESW_MSK;
7381 rc = volume_set_status_ec(n);
7387 mutex_unlock(&volume_mutex);
7391 static int volume_alsa_set_mute(const bool mute)
7393 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7394 (mute) ? "" : "un");
7395 return __volume_set_mute_ec(mute);
7398 static int volume_set_mute(const bool mute)
7402 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7403 (mute) ? "" : "un");
7405 rc = __volume_set_mute_ec(mute);
7406 return (rc < 0) ? rc : 0;
7409 /* returns < 0 on error, 0 on no change, 1 on change */
7410 static int __volume_set_volume_ec(const u8 vol)
7415 if (vol > TP_EC_VOLUME_MAX)
7418 if (mutex_lock_killable(&volume_mutex) < 0)
7421 rc = volume_get_status_ec(&s);
7425 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7428 rc = volume_set_status_ec(n);
7434 mutex_unlock(&volume_mutex);
7438 static int volume_set_software_mute(bool startup)
7442 if (!tpacpi_is_lenovo())
7446 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7450 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7451 "Initial HAUM setting was %d\n",
7452 software_mute_orig_mode);
7455 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7456 (int)TP_EC_MUTE_BTN_NONE))
7459 if (result != TP_EC_MUTE_BTN_NONE)
7460 pr_warn("Unexpected SAUM result %d\n",
7464 * In software mute mode, the standard codec controls take
7465 * precendence, so we unmute the ThinkPad HW switch at
7466 * startup. Just on case there are SAUM-capable ThinkPads
7467 * with level controls, set max HW volume as well.
7469 if (tp_features.mixer_no_level_control)
7470 result = volume_set_mute(false);
7472 result = volume_set_status(TP_EC_VOLUME_MAX);
7475 pr_warn("Failed to unmute the HW mute switch\n");
7480 static void volume_exit_software_mute(void)
7484 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7485 || r != software_mute_orig_mode)
7486 pr_warn("Failed to restore mute mode\n");
7489 static int volume_alsa_set_volume(const u8 vol)
7491 dbg_printk(TPACPI_DBG_MIXER,
7492 "ALSA: trying to set volume level to %hu\n", vol);
7493 return __volume_set_volume_ec(vol);
7496 static void volume_alsa_notify_change(void)
7498 struct tpacpi_alsa_data *d;
7500 if (alsa_card && alsa_card->private_data) {
7501 d = alsa_card->private_data;
7503 snd_ctl_notify(alsa_card,
7504 SNDRV_CTL_EVENT_MASK_VALUE,
7507 snd_ctl_notify(alsa_card,
7508 SNDRV_CTL_EVENT_MASK_VALUE,
7513 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7514 struct snd_ctl_elem_info *uinfo)
7516 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7518 uinfo->value.integer.min = 0;
7519 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7523 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7524 struct snd_ctl_elem_value *ucontrol)
7529 rc = volume_get_status(&s);
7533 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7537 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7538 struct snd_ctl_elem_value *ucontrol)
7540 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7541 ucontrol->value.integer.value[0]);
7542 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7545 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7547 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7548 struct snd_ctl_elem_value *ucontrol)
7553 rc = volume_get_status(&s);
7557 ucontrol->value.integer.value[0] =
7558 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7562 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7563 struct snd_ctl_elem_value *ucontrol)
7565 tpacpi_disclose_usertask("ALSA", "%smute\n",
7566 ucontrol->value.integer.value[0] ?
7568 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7571 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7573 .name = "Console Playback Volume",
7575 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7576 .info = volume_alsa_vol_info,
7577 .get = volume_alsa_vol_get,
7580 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7581 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7582 .name = "Console Playback Switch",
7584 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7585 .info = volume_alsa_mute_info,
7586 .get = volume_alsa_mute_get,
7589 static void volume_suspend(void)
7591 tpacpi_volume_checkpoint_nvram();
7594 static void volume_resume(void)
7596 if (software_mute_active) {
7597 if (volume_set_software_mute(false) < 0)
7598 pr_warn("Failed to restore software mute\n");
7600 volume_alsa_notify_change();
7604 static void volume_shutdown(void)
7606 tpacpi_volume_checkpoint_nvram();
7609 static void volume_exit(void)
7612 snd_card_free(alsa_card);
7616 tpacpi_volume_checkpoint_nvram();
7618 if (software_mute_active)
7619 volume_exit_software_mute();
7622 static int __init volume_create_alsa_mixer(void)
7624 struct snd_card *card;
7625 struct tpacpi_alsa_data *data;
7626 struct snd_kcontrol *ctl_vol;
7627 struct snd_kcontrol *ctl_mute;
7630 rc = snd_card_new(&tpacpi_pdev->dev,
7631 alsa_index, alsa_id, THIS_MODULE,
7632 sizeof(struct tpacpi_alsa_data), &card);
7633 if (rc < 0 || !card) {
7634 pr_err("Failed to create ALSA card structures: %d\n", rc);
7638 BUG_ON(!card->private_data);
7639 data = card->private_data;
7642 strscpy(card->driver, TPACPI_ALSA_DRVNAME,
7643 sizeof(card->driver));
7644 strscpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7645 sizeof(card->shortname));
7646 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7647 (thinkpad_id.ec_version_str) ?
7648 thinkpad_id.ec_version_str : "(unknown)");
7649 snprintf(card->longname, sizeof(card->longname),
7650 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7651 (thinkpad_id.ec_version_str) ?
7652 thinkpad_id.ec_version_str : "unknown");
7654 if (volume_control_allowed) {
7655 volume_alsa_control_vol.put = volume_alsa_vol_put;
7656 volume_alsa_control_vol.access =
7657 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7659 volume_alsa_control_mute.put = volume_alsa_mute_put;
7660 volume_alsa_control_mute.access =
7661 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7664 if (!tp_features.mixer_no_level_control) {
7665 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7666 rc = snd_ctl_add(card, ctl_vol);
7668 pr_err("Failed to create ALSA volume control: %d\n",
7672 data->ctl_vol_id = &ctl_vol->id;
7675 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7676 rc = snd_ctl_add(card, ctl_mute);
7678 pr_err("Failed to create ALSA mute control: %d\n", rc);
7681 data->ctl_mute_id = &ctl_mute->id;
7683 rc = snd_card_register(card);
7685 pr_err("Failed to register ALSA card: %d\n", rc);
7693 snd_card_free(card);
7697 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7698 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7700 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7701 /* Whitelist volume level on all IBM by default */
7702 { .vendor = PCI_VENDOR_ID_IBM,
7703 .bios = TPACPI_MATCH_ANY,
7704 .ec = TPACPI_MATCH_ANY,
7705 .quirks = TPACPI_VOL_Q_LEVEL },
7707 /* Lenovo models with volume control (needs confirmation) */
7708 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7709 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7710 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7711 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7712 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7713 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7714 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7716 /* Whitelist mute-only on all Lenovo by default */
7717 { .vendor = PCI_VENDOR_ID_LENOVO,
7718 .bios = TPACPI_MATCH_ANY,
7719 .ec = TPACPI_MATCH_ANY,
7720 .quirks = TPACPI_VOL_Q_MUTEONLY }
7723 static int __init volume_init(struct ibm_init_struct *iibm)
7725 unsigned long quirks;
7728 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7730 mutex_init(&volume_mutex);
7733 * Check for module parameter bogosity, note that we
7734 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7735 * able to detect "unspecified"
7737 if (volume_mode > TPACPI_VOL_MODE_MAX)
7740 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7741 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7746 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7750 * The ALSA mixer is our primary interface.
7751 * When disabled, don't install the subdriver at all
7754 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7755 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7759 quirks = tpacpi_check_quirks(volume_quirk_table,
7760 ARRAY_SIZE(volume_quirk_table));
7762 switch (volume_capabilities) {
7763 case TPACPI_VOL_CAP_AUTO:
7764 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7765 tp_features.mixer_no_level_control = 1;
7766 else if (quirks & TPACPI_VOL_Q_LEVEL)
7767 tp_features.mixer_no_level_control = 0;
7769 return -ENODEV; /* no mixer */
7771 case TPACPI_VOL_CAP_VOLMUTE:
7772 tp_features.mixer_no_level_control = 0;
7774 case TPACPI_VOL_CAP_MUTEONLY:
7775 tp_features.mixer_no_level_control = 1;
7781 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7782 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7783 "using user-supplied volume_capabilities=%d\n",
7784 volume_capabilities);
7786 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7787 volume_mode == TPACPI_VOL_MODE_MAX) {
7788 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7790 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7791 "driver auto-selected volume_mode=%d\n",
7794 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7795 "using user-supplied volume_mode=%d\n",
7799 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7800 "mute is supported, volume control is %s\n",
7801 str_supported(!tp_features.mixer_no_level_control));
7803 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7804 software_mute_active = true;
7806 rc = volume_create_alsa_mixer();
7808 pr_err("Could not create the ALSA mixer interface\n");
7812 pr_info("Console audio control enabled, mode: %s\n",
7813 (volume_control_allowed) ?
7814 "override (read/write)" :
7815 "monitor (read only)");
7818 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7819 "registering volume hotkeys as change notification\n");
7820 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7821 | TP_ACPI_HKEY_VOLUP_MASK
7822 | TP_ACPI_HKEY_VOLDWN_MASK
7823 | TP_ACPI_HKEY_MUTE_MASK);
7828 static int volume_read(struct seq_file *m)
7832 if (volume_get_status(&status) < 0) {
7833 seq_printf(m, "level:\t\tunreadable\n");
7835 if (tp_features.mixer_no_level_control)
7836 seq_printf(m, "level:\t\tunsupported\n");
7838 seq_printf(m, "level:\t\t%d\n",
7839 status & TP_EC_AUDIO_LVL_MSK);
7841 seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
7843 if (volume_control_allowed) {
7844 seq_printf(m, "commands:\tunmute, mute\n");
7845 if (!tp_features.mixer_no_level_control) {
7846 seq_printf(m, "commands:\tup, down\n");
7847 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7856 static int volume_write(char *buf)
7859 u8 new_level, new_mute;
7865 * We do allow volume control at driver startup, so that the
7866 * user can set initial state through the volume=... parameter hack.
7868 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7869 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7870 tp_warned.volume_ctrl_forbidden = 1;
7871 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7872 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7877 rc = volume_get_status(&s);
7881 new_level = s & TP_EC_AUDIO_LVL_MSK;
7882 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7884 while ((cmd = strsep(&buf, ","))) {
7885 if (!tp_features.mixer_no_level_control) {
7886 if (strstarts(cmd, "up")) {
7889 else if (new_level < TP_EC_VOLUME_MAX)
7892 } else if (strstarts(cmd, "down")) {
7895 else if (new_level > 0)
7898 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7899 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7904 if (strstarts(cmd, "mute"))
7905 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7906 else if (strstarts(cmd, "unmute"))
7912 if (tp_features.mixer_no_level_control) {
7913 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7914 new_mute ? "" : "un");
7915 rc = volume_set_mute(!!new_mute);
7917 tpacpi_disclose_usertask("procfs volume",
7918 "%smute and set level to %d\n",
7919 new_mute ? "" : "un", new_level);
7920 rc = volume_set_status(new_mute | new_level);
7922 volume_alsa_notify_change();
7924 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7927 static struct ibm_struct volume_driver_data = {
7929 .read = volume_read,
7930 .write = volume_write,
7931 .exit = volume_exit,
7932 .suspend = volume_suspend,
7933 .resume = volume_resume,
7934 .shutdown = volume_shutdown,
7937 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7939 #define alsa_card NULL
7941 static inline void volume_alsa_notify_change(void)
7945 static int __init volume_init(struct ibm_init_struct *iibm)
7947 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7952 static struct ibm_struct volume_driver_data = {
7956 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7958 /*************************************************************************
7965 * TPACPI_FAN_RD_ACPI_GFAN:
7966 * ACPI GFAN method: returns fan level
7968 * see TPACPI_FAN_WR_ACPI_SFAN
7969 * EC 0x2f (HFSP) not available if GFAN exists
7971 * TPACPI_FAN_WR_ACPI_SFAN:
7972 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7974 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7977 * TPACPI_FAN_WR_TPEC:
7978 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7979 * Supported on almost all ThinkPads
7981 * Fan speed changes of any sort (including those caused by the
7982 * disengaged mode) are usually done slowly by the firmware as the
7983 * maximum amount of fan duty cycle change per second seems to be
7986 * Reading is not available if GFAN exists.
7987 * Writing is not available if SFAN exists.
7990 * 7 automatic mode engaged;
7991 * (default operation mode of the ThinkPad)
7992 * fan level is ignored in this mode.
7993 * 6 full speed mode (takes precedence over bit 7);
7994 * not available on all thinkpads. May disable
7995 * the tachometer while the fan controller ramps up
7996 * the speed (which can take up to a few *minutes*).
7997 * Speeds up fan to 100% duty-cycle, which is far above
7998 * the standard RPM levels. It is not impossible that
7999 * it could cause hardware damage.
8000 * 5-3 unused in some models. Extra bits for fan level
8001 * in others, but still useless as all values above
8002 * 7 map to the same speed as level 7 in these models.
8003 * 2-0 fan level (0..7 usually)
8005 * 0x07 = max (set when temperatures critical)
8006 * Some ThinkPads may have other levels, see
8007 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
8009 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
8010 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
8011 * does so, its initial value is meaningless (0x07).
8013 * For firmware bugs, refer to:
8014 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8018 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8019 * Main fan tachometer reading (in RPM)
8021 * This register is present on all ThinkPads with a new-style EC, and
8022 * it is known not to be present on the A21m/e, and T22, as there is
8023 * something else in offset 0x84 according to the ACPI DSDT. Other
8024 * ThinkPads from this same time period (and earlier) probably lack the
8025 * tachometer as well.
8027 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8028 * was never fixed by IBM to report the EC firmware version string
8029 * probably support the tachometer (like the early X models), so
8030 * detecting it is quite hard. We need more data to know for sure.
8032 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8035 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8038 * For firmware bugs, refer to:
8039 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8043 * ThinkPad EC register 0x31 bit 0 (only on select models)
8045 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8046 * show the speed of the main fan. When bit 0 of EC register 0x31
8047 * is one, the tachometer registers show the speed of the auxiliary
8050 * Fan control seems to affect both fans, regardless of the state
8053 * So far, only the firmware for the X60/X61 non-tablet versions
8054 * seem to support this (firmware TP-7M).
8056 * TPACPI_FAN_WR_ACPI_FANS:
8057 * ThinkPad X31, X40, X41. Not available in the X60.
8059 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8060 * high speed. ACPI DSDT seems to map these three speeds to levels
8061 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8062 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8064 * The speeds are stored on handles
8065 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8067 * There are three default speed sets, accessible as handles:
8068 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8070 * ACPI DSDT switches which set is in use depending on various
8073 * TPACPI_FAN_WR_TPEC is also available and should be used to
8074 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8075 * but the ACPI tables just mention level 7.
8078 enum { /* Fan control constants */
8079 fan_status_offset = 0x2f, /* EC register 0x2f */
8080 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8081 * 0x84 must be read before 0x85 */
8082 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8083 bit 0 selects which fan is active */
8085 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8086 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8088 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8091 enum fan_status_access_mode {
8092 TPACPI_FAN_NONE = 0, /* No fan status or control */
8093 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8094 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8097 enum fan_control_access_mode {
8098 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8099 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8100 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8101 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8104 enum fan_control_commands {
8105 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8106 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8107 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8108 * and also watchdog cmd */
8111 static bool fan_control_allowed;
8113 static enum fan_status_access_mode fan_status_access_mode;
8114 static enum fan_control_access_mode fan_control_access_mode;
8115 static enum fan_control_commands fan_control_commands;
8117 static u8 fan_control_initial_status;
8118 static u8 fan_control_desired_level;
8119 static u8 fan_control_resume_level;
8120 static int fan_watchdog_maxinterval;
8122 static struct mutex fan_mutex;
8124 static void fan_watchdog_fire(struct work_struct *ignored);
8125 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8127 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8128 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8129 "\\FSPD", /* 600e/x, 770e, 770x */
8131 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8132 "JFNS", /* 770x-JL */
8136 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8137 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8138 * be in auto mode (0x80).
8140 * This is corrected by any write to HFSP either by the driver, or
8143 * We assume 0x07 really means auto mode while this quirk is active,
8144 * as this is far more likely than the ThinkPad being in level 7,
8145 * which is only used by the firmware during thermal emergencies.
8147 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8148 * TP-70 (T43, R52), which are known to be buggy.
8151 static void fan_quirk1_setup(void)
8153 if (fan_control_initial_status == 0x07) {
8154 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8155 tp_features.fan_ctrl_status_undef = 1;
8159 static void fan_quirk1_handle(u8 *fan_status)
8161 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8162 if (*fan_status != fan_control_initial_status) {
8163 /* something changed the HFSP regisnter since
8164 * driver init time, so it is not undefined
8166 tp_features.fan_ctrl_status_undef = 0;
8168 /* Return most likely status. In fact, it
8169 * might be the only possible status */
8170 *fan_status = TP_EC_FAN_AUTO;
8175 /* Select main fan on X60/X61, NOOP on others */
8176 static bool fan_select_fan1(void)
8178 if (tp_features.second_fan) {
8181 if (ec_read(fan_select_offset, &val) < 0)
8184 if (ec_write(fan_select_offset, val) < 0)
8190 /* Select secondary fan on X60/X61 */
8191 static bool fan_select_fan2(void)
8195 if (!tp_features.second_fan)
8198 if (ec_read(fan_select_offset, &val) < 0)
8201 if (ec_write(fan_select_offset, val) < 0)
8208 * Call with fan_mutex held
8210 static void fan_update_desired_level(u8 status)
8213 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8215 fan_control_desired_level = 7;
8217 fan_control_desired_level = status;
8221 static int fan_get_status(u8 *status)
8226 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8228 switch (fan_status_access_mode) {
8229 case TPACPI_FAN_RD_ACPI_GFAN: {
8230 /* 570, 600e/x, 770e, 770x */
8233 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8237 *status = res & 0x07;
8241 case TPACPI_FAN_RD_TPEC:
8242 /* all except 570, 600e/x, 770e, 770x */
8243 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8246 if (likely(status)) {
8248 fan_quirk1_handle(status);
8260 static int fan_get_status_safe(u8 *status)
8265 if (mutex_lock_killable(&fan_mutex))
8266 return -ERESTARTSYS;
8267 rc = fan_get_status(&s);
8269 fan_update_desired_level(s);
8270 mutex_unlock(&fan_mutex);
8280 static int fan_get_speed(unsigned int *speed)
8284 switch (fan_status_access_mode) {
8285 case TPACPI_FAN_RD_TPEC:
8286 /* all except 570, 600e/x, 770e, 770x */
8287 if (unlikely(!fan_select_fan1()))
8289 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8290 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8294 *speed = (hi << 8) | lo;
8305 static int fan2_get_speed(unsigned int *speed)
8310 switch (fan_status_access_mode) {
8311 case TPACPI_FAN_RD_TPEC:
8312 /* all except 570, 600e/x, 770e, 770x */
8313 if (unlikely(!fan_select_fan2()))
8315 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8316 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8317 fan_select_fan1(); /* play it safe */
8322 *speed = (hi << 8) | lo;
8333 static int fan_set_level(int level)
8335 if (!fan_control_allowed)
8338 switch (fan_control_access_mode) {
8339 case TPACPI_FAN_WR_ACPI_SFAN:
8340 if ((level < 0) || (level > 7))
8343 if (tp_features.second_fan_ctl) {
8344 if (!fan_select_fan2() ||
8345 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8346 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8347 tp_features.second_fan_ctl = 0;
8351 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8355 case TPACPI_FAN_WR_ACPI_FANS:
8356 case TPACPI_FAN_WR_TPEC:
8357 if (!(level & TP_EC_FAN_AUTO) &&
8358 !(level & TP_EC_FAN_FULLSPEED) &&
8359 ((level < 0) || (level > 7)))
8362 /* safety net should the EC not support AUTO
8363 * or FULLSPEED mode bits and just ignore them */
8364 if (level & TP_EC_FAN_FULLSPEED)
8365 level |= 7; /* safety min speed 7 */
8366 else if (level & TP_EC_FAN_AUTO)
8367 level |= 4; /* safety min speed 4 */
8369 if (tp_features.second_fan_ctl) {
8370 if (!fan_select_fan2() ||
8371 !acpi_ec_write(fan_status_offset, level)) {
8372 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8373 tp_features.second_fan_ctl = 0;
8378 if (!acpi_ec_write(fan_status_offset, level))
8381 tp_features.fan_ctrl_status_undef = 0;
8388 vdbg_printk(TPACPI_DBG_FAN,
8389 "fan control: set fan control register to 0x%02x\n", level);
8393 static int fan_set_level_safe(int level)
8397 if (!fan_control_allowed)
8400 if (mutex_lock_killable(&fan_mutex))
8401 return -ERESTARTSYS;
8403 if (level == TPACPI_FAN_LAST_LEVEL)
8404 level = fan_control_desired_level;
8406 rc = fan_set_level(level);
8408 fan_update_desired_level(level);
8410 mutex_unlock(&fan_mutex);
8414 static int fan_set_enable(void)
8419 if (!fan_control_allowed)
8422 if (mutex_lock_killable(&fan_mutex))
8423 return -ERESTARTSYS;
8425 switch (fan_control_access_mode) {
8426 case TPACPI_FAN_WR_ACPI_FANS:
8427 case TPACPI_FAN_WR_TPEC:
8428 rc = fan_get_status(&s);
8432 /* Don't go out of emergency fan mode */
8435 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8438 if (!acpi_ec_write(fan_status_offset, s))
8441 tp_features.fan_ctrl_status_undef = 0;
8446 case TPACPI_FAN_WR_ACPI_SFAN:
8447 rc = fan_get_status(&s);
8453 /* Set fan to at least level 4 */
8456 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8466 mutex_unlock(&fan_mutex);
8469 vdbg_printk(TPACPI_DBG_FAN,
8470 "fan control: set fan control register to 0x%02x\n",
8475 static int fan_set_disable(void)
8479 if (!fan_control_allowed)
8482 if (mutex_lock_killable(&fan_mutex))
8483 return -ERESTARTSYS;
8486 switch (fan_control_access_mode) {
8487 case TPACPI_FAN_WR_ACPI_FANS:
8488 case TPACPI_FAN_WR_TPEC:
8489 if (!acpi_ec_write(fan_status_offset, 0x00))
8492 fan_control_desired_level = 0;
8493 tp_features.fan_ctrl_status_undef = 0;
8497 case TPACPI_FAN_WR_ACPI_SFAN:
8498 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8501 fan_control_desired_level = 0;
8509 vdbg_printk(TPACPI_DBG_FAN,
8510 "fan control: set fan control register to 0\n");
8512 mutex_unlock(&fan_mutex);
8516 static int fan_set_speed(int speed)
8520 if (!fan_control_allowed)
8523 if (mutex_lock_killable(&fan_mutex))
8524 return -ERESTARTSYS;
8527 switch (fan_control_access_mode) {
8528 case TPACPI_FAN_WR_ACPI_FANS:
8529 if (speed >= 0 && speed <= 65535) {
8530 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8531 speed, speed, speed))
8541 mutex_unlock(&fan_mutex);
8545 static void fan_watchdog_reset(void)
8547 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8550 if (fan_watchdog_maxinterval > 0 &&
8551 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8552 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8553 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8555 cancel_delayed_work(&fan_watchdog_task);
8558 static void fan_watchdog_fire(struct work_struct *ignored)
8562 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8565 pr_notice("fan watchdog: enabling fan\n");
8566 rc = fan_set_enable();
8568 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8570 /* reschedule for later */
8571 fan_watchdog_reset();
8576 * SYSFS fan layout: hwmon compatible (device)
8579 * 0: "disengaged" mode
8581 * 2: native EC "auto" mode (recommended, hardware default)
8583 * pwm*: set speed in manual mode, ignored otherwise.
8584 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8587 * fan*_input: tachometer reading, RPM
8590 * SYSFS fan layout: extensions
8592 * fan_watchdog (driver):
8593 * fan watchdog interval in seconds, 0 disables (default), max 120
8596 /* sysfs fan pwm1_enable ----------------------------------------------- */
8597 static ssize_t fan_pwm1_enable_show(struct device *dev,
8598 struct device_attribute *attr,
8604 res = fan_get_status_safe(&status);
8608 if (status & TP_EC_FAN_FULLSPEED) {
8610 } else if (status & TP_EC_FAN_AUTO) {
8615 return sysfs_emit(buf, "%d\n", mode);
8618 static ssize_t fan_pwm1_enable_store(struct device *dev,
8619 struct device_attribute *attr,
8620 const char *buf, size_t count)
8625 if (parse_strtoul(buf, 2, &t))
8628 tpacpi_disclose_usertask("hwmon pwm1_enable",
8629 "set fan mode to %lu\n", t);
8633 level = TP_EC_FAN_FULLSPEED;
8636 level = TPACPI_FAN_LAST_LEVEL;
8639 level = TP_EC_FAN_AUTO;
8642 /* reserved for software-controlled auto mode */
8648 res = fan_set_level_safe(level);
8654 fan_watchdog_reset();
8659 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8660 fan_pwm1_enable_show, fan_pwm1_enable_store);
8662 /* sysfs fan pwm1 ------------------------------------------------------ */
8663 static ssize_t fan_pwm1_show(struct device *dev,
8664 struct device_attribute *attr,
8670 res = fan_get_status_safe(&status);
8675 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8676 status = fan_control_desired_level;
8681 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8684 static ssize_t fan_pwm1_store(struct device *dev,
8685 struct device_attribute *attr,
8686 const char *buf, size_t count)
8690 u8 status, newlevel;
8692 if (parse_strtoul(buf, 255, &s))
8695 tpacpi_disclose_usertask("hwmon pwm1",
8696 "set fan speed to %lu\n", s);
8698 /* scale down from 0-255 to 0-7 */
8699 newlevel = (s >> 5) & 0x07;
8701 if (mutex_lock_killable(&fan_mutex))
8702 return -ERESTARTSYS;
8704 rc = fan_get_status(&status);
8705 if (!rc && (status &
8706 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8707 rc = fan_set_level(newlevel);
8711 fan_update_desired_level(newlevel);
8712 fan_watchdog_reset();
8716 mutex_unlock(&fan_mutex);
8717 return (rc) ? rc : count;
8720 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8722 /* sysfs fan fan1_input ------------------------------------------------ */
8723 static ssize_t fan_fan1_input_show(struct device *dev,
8724 struct device_attribute *attr,
8730 res = fan_get_speed(&speed);
8734 return sysfs_emit(buf, "%u\n", speed);
8737 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8739 /* sysfs fan fan2_input ------------------------------------------------ */
8740 static ssize_t fan_fan2_input_show(struct device *dev,
8741 struct device_attribute *attr,
8747 res = fan2_get_speed(&speed);
8751 return sysfs_emit(buf, "%u\n", speed);
8754 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8756 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8757 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8759 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8762 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8767 if (parse_strtoul(buf, 120, &t))
8770 if (!fan_control_allowed)
8773 fan_watchdog_maxinterval = t;
8774 fan_watchdog_reset();
8776 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8780 static DRIVER_ATTR_RW(fan_watchdog);
8782 /* --------------------------------------------------------------------- */
8784 static struct attribute *fan_attributes[] = {
8785 &dev_attr_pwm1_enable.attr,
8786 &dev_attr_pwm1.attr,
8787 &dev_attr_fan1_input.attr,
8788 &dev_attr_fan2_input.attr,
8792 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8795 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8796 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8799 if (attr == &dev_attr_fan2_input.attr) {
8800 if (!tp_features.second_fan)
8807 static const struct attribute_group fan_attr_group = {
8808 .is_visible = fan_attr_is_visible,
8809 .attrs = fan_attributes,
8812 static struct attribute *fan_driver_attributes[] = {
8813 &driver_attr_fan_watchdog.attr,
8817 static const struct attribute_group fan_driver_attr_group = {
8818 .is_visible = fan_attr_is_visible,
8819 .attrs = fan_driver_attributes,
8822 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8823 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8824 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8825 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8827 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8828 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8829 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8830 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8831 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8832 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8833 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8834 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8835 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8836 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8837 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8838 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8839 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8840 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8841 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8842 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8843 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8844 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8847 static int __init fan_init(struct ibm_init_struct *iibm)
8849 unsigned long quirks;
8851 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8852 "initializing fan subdriver\n");
8854 mutex_init(&fan_mutex);
8855 fan_status_access_mode = TPACPI_FAN_NONE;
8856 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8857 fan_control_commands = 0;
8858 fan_watchdog_maxinterval = 0;
8859 tp_features.fan_ctrl_status_undef = 0;
8860 tp_features.second_fan = 0;
8861 tp_features.second_fan_ctl = 0;
8862 fan_control_desired_level = 7;
8864 if (tpacpi_is_ibm()) {
8865 TPACPI_ACPIHANDLE_INIT(fans);
8866 TPACPI_ACPIHANDLE_INIT(gfan);
8867 TPACPI_ACPIHANDLE_INIT(sfan);
8870 quirks = tpacpi_check_quirks(fan_quirk_table,
8871 ARRAY_SIZE(fan_quirk_table));
8873 if (quirks & TPACPI_FAN_NOFAN) {
8874 pr_info("No integrated ThinkPad fan available\n");
8879 /* 570, 600e/x, 770e, 770x */
8880 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8882 /* all other ThinkPads: note that even old-style
8883 * ThinkPad ECs supports the fan control register */
8884 if (likely(acpi_ec_read(fan_status_offset,
8885 &fan_control_initial_status))) {
8889 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8890 if (quirks & TPACPI_FAN_Q1)
8892 /* Try and probe the 2nd fan */
8893 tp_features.second_fan = 1; /* needed for get_speed to work */
8894 res = fan2_get_speed(&speed);
8895 if (res >= 0 && speed != FAN_NOT_PRESENT) {
8896 /* It responded - so let's assume it's there */
8897 tp_features.second_fan = 1;
8898 tp_features.second_fan_ctl = 1;
8899 pr_info("secondary fan control detected & enabled\n");
8901 /* Fan not auto-detected */
8902 tp_features.second_fan = 0;
8903 if (quirks & TPACPI_FAN_2FAN) {
8904 tp_features.second_fan = 1;
8905 pr_info("secondary fan support enabled\n");
8907 if (quirks & TPACPI_FAN_2CTL) {
8908 tp_features.second_fan = 1;
8909 tp_features.second_fan_ctl = 1;
8910 pr_info("secondary fan control enabled\n");
8914 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8921 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8922 fan_control_commands |=
8923 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8926 /* gfan without sfan means no fan control */
8927 /* all other models implement TP EC 0x2f control */
8931 fan_control_access_mode =
8932 TPACPI_FAN_WR_ACPI_FANS;
8933 fan_control_commands |=
8934 TPACPI_FAN_CMD_SPEED |
8935 TPACPI_FAN_CMD_LEVEL |
8936 TPACPI_FAN_CMD_ENABLE;
8938 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8939 fan_control_commands |=
8940 TPACPI_FAN_CMD_LEVEL |
8941 TPACPI_FAN_CMD_ENABLE;
8946 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8947 "fan is %s, modes %d, %d\n",
8948 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8949 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8950 fan_status_access_mode, fan_control_access_mode);
8952 /* fan control master switch */
8953 if (!fan_control_allowed) {
8954 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8955 fan_control_commands = 0;
8956 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8957 "fan control features disabled by parameter\n");
8960 /* update fan_control_desired_level */
8961 if (fan_status_access_mode != TPACPI_FAN_NONE)
8962 fan_get_status_safe(NULL);
8964 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8965 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8971 static void fan_exit(void)
8973 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8974 "cancelling any pending fan watchdog tasks\n");
8976 cancel_delayed_work(&fan_watchdog_task);
8977 flush_workqueue(tpacpi_wq);
8980 static void fan_suspend(void)
8984 if (!fan_control_allowed)
8987 /* Store fan status in cache */
8988 fan_control_resume_level = 0;
8989 rc = fan_get_status_safe(&fan_control_resume_level);
8991 pr_notice("failed to read fan level for later restore during resume: %d\n",
8994 /* if it is undefined, don't attempt to restore it.
8996 if (tp_features.fan_ctrl_status_undef)
8997 fan_control_resume_level = 0;
9000 static void fan_resume(void)
9002 u8 current_level = 7;
9003 bool do_set = false;
9006 /* DSDT *always* updates status on resume */
9007 tp_features.fan_ctrl_status_undef = 0;
9009 if (!fan_control_allowed ||
9010 !fan_control_resume_level ||
9011 fan_get_status_safe(¤t_level))
9014 switch (fan_control_access_mode) {
9015 case TPACPI_FAN_WR_ACPI_SFAN:
9016 /* never decrease fan level */
9017 do_set = (fan_control_resume_level > current_level);
9019 case TPACPI_FAN_WR_ACPI_FANS:
9020 case TPACPI_FAN_WR_TPEC:
9021 /* never decrease fan level, scale is:
9022 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
9024 * We expect the firmware to set either 7 or AUTO, but we
9025 * handle FULLSPEED out of paranoia.
9027 * So, we can safely only restore FULLSPEED or 7, anything
9028 * else could slow the fan. Restoring AUTO is useless, at
9029 * best that's exactly what the DSDT already set (it is the
9032 * Always keep in mind that the DSDT *will* have set the
9033 * fans to what the vendor supposes is the best level. We
9034 * muck with it only to speed the fan up.
9036 if (fan_control_resume_level != 7 &&
9037 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
9040 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
9041 (current_level != fan_control_resume_level);
9047 pr_notice("restoring fan level to 0x%02x\n",
9048 fan_control_resume_level);
9049 rc = fan_set_level_safe(fan_control_resume_level);
9051 pr_notice("failed to restore fan level: %d\n", rc);
9055 static int fan_read(struct seq_file *m)
9059 unsigned int speed = 0;
9061 switch (fan_status_access_mode) {
9062 case TPACPI_FAN_RD_ACPI_GFAN:
9063 /* 570, 600e/x, 770e, 770x */
9064 rc = fan_get_status_safe(&status);
9068 seq_printf(m, "status:\t\t%s\n"
9070 str_enabled_disabled(status), status);
9073 case TPACPI_FAN_RD_TPEC:
9074 /* all except 570, 600e/x, 770e, 770x */
9075 rc = fan_get_status_safe(&status);
9079 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
9081 rc = fan_get_speed(&speed);
9085 seq_printf(m, "speed:\t\t%d\n", speed);
9087 if (status & TP_EC_FAN_FULLSPEED)
9088 /* Disengaged mode takes precedence */
9089 seq_printf(m, "level:\t\tdisengaged\n");
9090 else if (status & TP_EC_FAN_AUTO)
9091 seq_printf(m, "level:\t\tauto\n");
9093 seq_printf(m, "level:\t\t%d\n", status);
9096 case TPACPI_FAN_NONE:
9098 seq_printf(m, "status:\t\tnot supported\n");
9101 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9102 seq_printf(m, "commands:\tlevel <level>");
9104 switch (fan_control_access_mode) {
9105 case TPACPI_FAN_WR_ACPI_SFAN:
9106 seq_printf(m, " (<level> is 0-7)\n");
9110 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9115 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9116 seq_printf(m, "commands:\tenable, disable\n"
9117 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9119 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9120 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9125 static int fan_write_cmd_level(const char *cmd, int *rc)
9129 if (strstarts(cmd, "level auto"))
9130 level = TP_EC_FAN_AUTO;
9131 else if (strstarts(cmd, "level disengaged") || strstarts(cmd, "level full-speed"))
9132 level = TP_EC_FAN_FULLSPEED;
9133 else if (sscanf(cmd, "level %d", &level) != 1)
9136 *rc = fan_set_level_safe(level);
9138 pr_err("level command accepted for unsupported access mode %d\n",
9139 fan_control_access_mode);
9141 tpacpi_disclose_usertask("procfs fan",
9142 "set level to %d\n", level);
9147 static int fan_write_cmd_enable(const char *cmd, int *rc)
9149 if (!strstarts(cmd, "enable"))
9152 *rc = fan_set_enable();
9154 pr_err("enable command accepted for unsupported access mode %d\n",
9155 fan_control_access_mode);
9157 tpacpi_disclose_usertask("procfs fan", "enable\n");
9162 static int fan_write_cmd_disable(const char *cmd, int *rc)
9164 if (!strstarts(cmd, "disable"))
9167 *rc = fan_set_disable();
9169 pr_err("disable command accepted for unsupported access mode %d\n",
9170 fan_control_access_mode);
9172 tpacpi_disclose_usertask("procfs fan", "disable\n");
9177 static int fan_write_cmd_speed(const char *cmd, int *rc)
9182 * Support speed <low> <medium> <high> ? */
9184 if (sscanf(cmd, "speed %d", &speed) != 1)
9187 *rc = fan_set_speed(speed);
9189 pr_err("speed command accepted for unsupported access mode %d\n",
9190 fan_control_access_mode);
9192 tpacpi_disclose_usertask("procfs fan",
9193 "set speed to %d\n", speed);
9198 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9202 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9205 if (interval < 0 || interval > 120)
9208 fan_watchdog_maxinterval = interval;
9209 tpacpi_disclose_usertask("procfs fan",
9210 "set watchdog timer to %d\n",
9217 static int fan_write(char *buf)
9222 while (!rc && (cmd = strsep(&buf, ","))) {
9223 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9224 fan_write_cmd_level(cmd, &rc)) &&
9225 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9226 (fan_write_cmd_enable(cmd, &rc) ||
9227 fan_write_cmd_disable(cmd, &rc) ||
9228 fan_write_cmd_watchdog(cmd, &rc))) &&
9229 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9230 fan_write_cmd_speed(cmd, &rc))
9234 fan_watchdog_reset();
9240 static struct ibm_struct fan_driver_data = {
9245 .suspend = fan_suspend,
9246 .resume = fan_resume,
9249 /*************************************************************************
9250 * Mute LED subdriver
9253 #define TPACPI_LED_MAX 2
9255 struct tp_led_table {
9262 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9263 [LED_AUDIO_MUTE] = {
9268 [LED_AUDIO_MICMUTE] = {
9275 static int mute_led_on_off(struct tp_led_table *t, bool state)
9280 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9281 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9285 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9286 state ? t->on_value : t->off_value))
9293 static int tpacpi_led_set(int whichled, bool on)
9295 struct tp_led_table *t;
9297 t = &led_tables[whichled];
9298 if (t->state < 0 || t->state == on)
9300 return mute_led_on_off(t, on);
9303 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9304 enum led_brightness brightness)
9306 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9309 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9310 enum led_brightness brightness)
9312 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9315 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9316 [LED_AUDIO_MUTE] = {
9317 .name = "platform::mute",
9318 .max_brightness = 1,
9319 .brightness_set_blocking = tpacpi_led_mute_set,
9320 .default_trigger = "audio-mute",
9322 [LED_AUDIO_MICMUTE] = {
9323 .name = "platform::micmute",
9324 .max_brightness = 1,
9325 .brightness_set_blocking = tpacpi_led_micmute_set,
9326 .default_trigger = "audio-micmute",
9330 static int mute_led_init(struct ibm_init_struct *iibm)
9335 for (i = 0; i < TPACPI_LED_MAX; i++) {
9336 struct tp_led_table *t = &led_tables[i];
9337 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9342 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9343 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9346 led_classdev_unregister(&mute_led_cdev[i]);
9353 static void mute_led_exit(void)
9357 for (i = 0; i < TPACPI_LED_MAX; i++) {
9358 led_classdev_unregister(&mute_led_cdev[i]);
9359 tpacpi_led_set(i, false);
9363 static void mute_led_resume(void)
9367 for (i = 0; i < TPACPI_LED_MAX; i++) {
9368 struct tp_led_table *t = &led_tables[i];
9370 mute_led_on_off(t, t->state);
9374 static struct ibm_struct mute_led_driver_data = {
9376 .exit = mute_led_exit,
9377 .resume = mute_led_resume,
9381 * Battery Wear Control Driver
9387 #define GET_START "BCTG"
9388 #define SET_START "BCCS"
9389 #define GET_STOP "BCSG"
9390 #define SET_STOP "BCSS"
9391 #define GET_DISCHARGE "BDSG"
9392 #define SET_DISCHARGE "BDSS"
9393 #define GET_INHIBIT "BICG"
9394 #define SET_INHIBIT "BICS"
9403 /* Error condition bit */
9404 METHOD_ERR = BIT(31),
9408 /* This is used in the get/set helpers */
9415 struct tpacpi_battery_data {
9420 unsigned int charge_behaviours;
9423 struct tpacpi_battery_driver_data {
9424 struct tpacpi_battery_data batteries[3];
9425 int individual_addressing;
9428 static struct tpacpi_battery_driver_data battery_info;
9430 /* ACPI helpers/functions/probes */
9433 * This evaluates a ACPI method call specific to the battery
9434 * ACPI extension. The specifics are that an error is marked
9435 * in the 32rd bit of the response, so we just check that here.
9437 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9441 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9442 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9445 if (response & METHOD_ERR) {
9446 acpi_handle_err(hkey_handle,
9447 "%s evaluated but flagged as error", method);
9454 static int tpacpi_battery_get(int what, int battery, int *ret)
9457 case THRESHOLD_START:
9458 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9461 /* The value is in the low 8 bits of the response */
9464 case THRESHOLD_STOP:
9465 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9467 /* Value is in lower 8 bits */
9470 * On the stop value, if we return 0 that
9471 * does not make any sense. 0 means Default, which
9472 * means that charging stops at 100%, so we return
9478 case FORCE_DISCHARGE:
9479 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9481 /* The force discharge status is in bit 0 */
9484 case INHIBIT_CHARGE:
9485 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9487 /* The inhibit charge status is in bit 0 */
9491 pr_crit("wrong parameter: %d", what);
9496 static int tpacpi_battery_set(int what, int battery, int value)
9499 /* The first 8 bits are the value of the threshold */
9501 /* The battery ID is in bits 8-9, 2 bits */
9502 param |= battery << 8;
9505 case THRESHOLD_START:
9506 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9507 pr_err("failed to set charge threshold on battery %d",
9512 case THRESHOLD_STOP:
9513 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9514 pr_err("failed to set stop threshold: %d", battery);
9518 case FORCE_DISCHARGE:
9519 /* Force discharge is in bit 0,
9520 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9521 * battery ID is in bits 8-9, 2 bits.
9523 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9524 pr_err("failed to set force discharge on %d", battery);
9528 case INHIBIT_CHARGE:
9529 /* When setting inhibit charge, we set a default value of
9530 * always breaking on AC detach and the effective time is set to
9532 * The battery ID is in bits 4-5, 2 bits,
9533 * the effective time is in bits 8-23, 2 bytes.
9534 * A time of FFFF indicates forever.
9537 param |= battery << 4;
9538 param |= 0xFFFF << 8;
9539 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9540 pr_err("failed to set inhibit charge on %d", battery);
9545 pr_crit("wrong parameter: %d", what);
9550 static int tpacpi_battery_set_validate(int what, int battery, int value)
9554 ret = tpacpi_battery_set(what, battery, value);
9558 ret = tpacpi_battery_get(what, battery, &v);
9567 ret = tpacpi_battery_get(what, battery, &v);
9577 static int tpacpi_battery_probe(int battery)
9581 memset(&battery_info.batteries[battery], 0,
9582 sizeof(battery_info.batteries[battery]));
9585 * 1) Get the current start threshold
9586 * 2) Check for support
9587 * 3) Get the current stop threshold
9588 * 4) Check for support
9589 * 5) Get the current force discharge status
9590 * 6) Check for support
9591 * 7) Get the current inhibit charge status
9592 * 8) Check for support
9594 if (acpi_has_method(hkey_handle, GET_START)) {
9595 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9596 pr_err("Error probing battery %d\n", battery);
9599 /* Individual addressing is in bit 9 */
9601 battery_info.individual_addressing = true;
9602 /* Support is marked in bit 8 */
9604 battery_info.batteries[battery].start_support = 1;
9607 if (tpacpi_battery_get(THRESHOLD_START, battery,
9608 &battery_info.batteries[battery].charge_start)) {
9609 pr_err("Error probing battery %d\n", battery);
9613 if (acpi_has_method(hkey_handle, GET_STOP)) {
9614 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9615 pr_err("Error probing battery stop; %d\n", battery);
9618 /* Support is marked in bit 8 */
9620 battery_info.batteries[battery].stop_support = 1;
9623 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9624 &battery_info.batteries[battery].charge_stop)) {
9625 pr_err("Error probing battery stop: %d\n", battery);
9629 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9630 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9631 pr_err("Error probing battery discharge; %d\n", battery);
9634 /* Support is marked in bit 8 */
9636 battery_info.batteries[battery].charge_behaviours |=
9637 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9639 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9640 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9641 pr_err("Error probing battery inhibit charge; %d\n", battery);
9644 /* Support is marked in bit 5 */
9646 battery_info.batteries[battery].charge_behaviours |=
9647 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9650 battery_info.batteries[battery].charge_behaviours |=
9651 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9653 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9655 battery_info.batteries[battery].charge_start,
9656 battery_info.batteries[battery].charge_stop,
9657 battery_info.batteries[battery].charge_behaviours);
9662 /* General helper functions */
9664 static int tpacpi_battery_get_id(const char *battery_name)
9667 if (strcmp(battery_name, "BAT0") == 0 ||
9668 tp_features.battery_force_primary)
9670 if (strcmp(battery_name, "BAT1") == 0)
9671 return BAT_SECONDARY;
9673 * If for some reason the battery is not BAT0 nor is it
9674 * BAT1, we will assume it's the default, first battery,
9677 pr_warn("unknown battery %s, assuming primary", battery_name);
9681 /* sysfs interface */
9683 static ssize_t tpacpi_battery_store(int what,
9685 const char *buf, size_t count)
9687 struct power_supply *supply = to_power_supply(dev);
9688 unsigned long value;
9691 * Some systems have support for more than
9692 * one battery. If that is the case,
9693 * tpacpi_battery_probe marked that addressing
9694 * them individually is supported, so we do that
9695 * based on the device struct.
9697 * On systems that are not supported, we assume
9698 * the primary as most of the ACPI calls fail
9699 * with "Any Battery" as the parameter.
9701 if (battery_info.individual_addressing)
9702 /* BAT_PRIMARY or BAT_SECONDARY */
9703 battery = tpacpi_battery_get_id(supply->desc->name);
9705 battery = BAT_PRIMARY;
9707 rval = kstrtoul(buf, 10, &value);
9712 case THRESHOLD_START:
9713 if (!battery_info.batteries[battery].start_support)
9715 /* valid values are [0, 99] */
9718 if (value > battery_info.batteries[battery].charge_stop)
9720 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9722 battery_info.batteries[battery].charge_start = value;
9725 case THRESHOLD_STOP:
9726 if (!battery_info.batteries[battery].stop_support)
9728 /* valid values are [1, 100] */
9729 if (value < 1 || value > 100)
9731 if (value < battery_info.batteries[battery].charge_start)
9733 battery_info.batteries[battery].charge_stop = value;
9735 * When 100 is passed to stop, we need to flip
9736 * it to 0 as that the EC understands that as
9737 * "Default", which will charge to 100%
9741 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9745 pr_crit("Wrong parameter: %d", what);
9751 static ssize_t tpacpi_battery_show(int what,
9755 struct power_supply *supply = to_power_supply(dev);
9758 * Some systems have support for more than
9759 * one battery. If that is the case,
9760 * tpacpi_battery_probe marked that addressing
9761 * them individually is supported, so we;
9762 * based on the device struct.
9764 * On systems that are not supported, we assume
9765 * the primary as most of the ACPI calls fail
9766 * with "Any Battery" as the parameter.
9768 if (battery_info.individual_addressing)
9769 /* BAT_PRIMARY or BAT_SECONDARY */
9770 battery = tpacpi_battery_get_id(supply->desc->name);
9772 battery = BAT_PRIMARY;
9773 if (tpacpi_battery_get(what, battery, &ret))
9775 return sprintf(buf, "%d\n", ret);
9778 static ssize_t charge_control_start_threshold_show(struct device *device,
9779 struct device_attribute *attr,
9782 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9785 static ssize_t charge_control_end_threshold_show(struct device *device,
9786 struct device_attribute *attr,
9789 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9792 static ssize_t charge_behaviour_show(struct device *dev,
9793 struct device_attribute *attr,
9796 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9797 struct power_supply *supply = to_power_supply(dev);
9798 unsigned int available;
9801 battery = tpacpi_battery_get_id(supply->desc->name);
9802 available = battery_info.batteries[battery].charge_behaviours;
9804 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9805 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9808 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9813 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9814 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9817 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9823 return power_supply_charge_behaviour_show(dev, available, active, buf);
9826 static ssize_t charge_control_start_threshold_store(struct device *dev,
9827 struct device_attribute *attr,
9828 const char *buf, size_t count)
9830 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9833 static ssize_t charge_control_end_threshold_store(struct device *dev,
9834 struct device_attribute *attr,
9835 const char *buf, size_t count)
9837 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9840 static ssize_t charge_behaviour_store(struct device *dev,
9841 struct device_attribute *attr,
9842 const char *buf, size_t count)
9844 struct power_supply *supply = to_power_supply(dev);
9845 int selected, battery, ret = 0;
9846 unsigned int available;
9848 battery = tpacpi_battery_get_id(supply->desc->name);
9849 available = battery_info.batteries[battery].charge_behaviours;
9850 selected = power_supply_charge_behaviour_parse(available, buf);
9856 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9857 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9858 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9859 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9860 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9864 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9865 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9866 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9867 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9871 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9872 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9873 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9874 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9879 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9886 static DEVICE_ATTR_RW(charge_control_start_threshold);
9887 static DEVICE_ATTR_RW(charge_control_end_threshold);
9888 static DEVICE_ATTR_RW(charge_behaviour);
9889 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9890 charge_start_threshold,
9892 charge_control_start_threshold_show,
9893 charge_control_start_threshold_store
9895 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9896 charge_stop_threshold,
9898 charge_control_end_threshold_show,
9899 charge_control_end_threshold_store
9902 static struct attribute *tpacpi_battery_attrs[] = {
9903 &dev_attr_charge_control_start_threshold.attr,
9904 &dev_attr_charge_control_end_threshold.attr,
9905 &dev_attr_charge_start_threshold.attr,
9906 &dev_attr_charge_stop_threshold.attr,
9907 &dev_attr_charge_behaviour.attr,
9911 ATTRIBUTE_GROUPS(tpacpi_battery);
9913 /* ACPI battery hooking */
9915 static int tpacpi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
9917 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9919 if (tpacpi_battery_probe(batteryid))
9921 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9926 static int tpacpi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
9928 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9932 static struct acpi_battery_hook battery_hook = {
9933 .add_battery = tpacpi_battery_add,
9934 .remove_battery = tpacpi_battery_remove,
9935 .name = "ThinkPad Battery Extension",
9938 /* Subdriver init/exit */
9940 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9942 * Individual addressing is broken on models that expose the
9943 * primary battery as BAT1.
9945 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9946 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9947 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9948 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9949 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9950 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9953 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9955 memset(&battery_info, 0, sizeof(battery_info));
9957 tp_features.battery_force_primary = tpacpi_check_quirks(
9958 battery_quirk_table,
9959 ARRAY_SIZE(battery_quirk_table));
9961 battery_hook_register(&battery_hook);
9965 static void tpacpi_battery_exit(void)
9967 battery_hook_unregister(&battery_hook);
9970 static struct ibm_struct battery_driver_data = {
9972 .exit = tpacpi_battery_exit,
9975 /*************************************************************************
9976 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9979 static struct drm_privacy_screen *lcdshadow_dev;
9980 static acpi_handle lcdshadow_get_handle;
9981 static acpi_handle lcdshadow_set_handle;
9983 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9984 enum drm_privacy_screen_status state)
9988 if (WARN_ON(!mutex_is_locked(&priv->lock)))
9991 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
9994 priv->hw_state = priv->sw_state = state;
9998 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
10002 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10005 priv->hw_state = priv->sw_state = output & 0x1;
10008 static const struct drm_privacy_screen_ops lcdshadow_ops = {
10009 .set_sw_state = lcdshadow_set_sw_state,
10010 .get_hw_state = lcdshadow_get_hw_state,
10013 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
10015 acpi_status status1, status2;
10018 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
10019 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
10020 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
10023 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10026 if (!(output & 0x10000))
10029 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
10030 &lcdshadow_ops, NULL);
10031 if (IS_ERR(lcdshadow_dev))
10032 return PTR_ERR(lcdshadow_dev);
10037 static void lcdshadow_exit(void)
10039 drm_privacy_screen_unregister(lcdshadow_dev);
10042 static void lcdshadow_resume(void)
10044 if (!lcdshadow_dev)
10047 mutex_lock(&lcdshadow_dev->lock);
10048 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
10049 mutex_unlock(&lcdshadow_dev->lock);
10052 static int lcdshadow_read(struct seq_file *m)
10054 if (!lcdshadow_dev) {
10055 seq_puts(m, "status:\t\tnot supported\n");
10057 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
10058 seq_puts(m, "commands:\t0, 1\n");
10064 static int lcdshadow_write(char *buf)
10067 int res, state = -EINVAL;
10069 if (!lcdshadow_dev)
10072 while ((cmd = strsep(&buf, ","))) {
10073 res = kstrtoint(cmd, 10, &state);
10078 if (state >= 2 || state < 0)
10081 mutex_lock(&lcdshadow_dev->lock);
10082 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
10083 mutex_unlock(&lcdshadow_dev->lock);
10085 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10090 static struct ibm_struct lcdshadow_driver_data = {
10091 .name = "lcdshadow",
10092 .exit = lcdshadow_exit,
10093 .resume = lcdshadow_resume,
10094 .read = lcdshadow_read,
10095 .write = lcdshadow_write,
10098 /*************************************************************************
10099 * Thinkpad sensor interfaces
10102 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
10103 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
10104 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
10105 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
10107 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
10108 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
10110 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
10111 #define PALMSENSOR_ON_BIT 1 /* psensor status */
10113 static bool has_palmsensor;
10114 static bool has_lapsensor;
10115 static bool palm_state;
10116 static bool lap_state;
10117 static int dytc_version;
10119 static int dytc_command(int command, int *output)
10121 acpi_handle dytc_handle;
10123 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
10124 /* Platform doesn't support DYTC */
10127 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
10132 static int lapsensor_get(bool *present, bool *state)
10137 err = dytc_command(DYTC_CMD_GET, &output);
10141 *present = true; /*If we get his far, we have lapmode support*/
10142 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
10146 static int palmsensor_get(bool *present, bool *state)
10148 acpi_handle psensor_handle;
10152 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10154 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10157 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10158 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10162 static void lapsensor_refresh(void)
10167 if (has_lapsensor) {
10168 err = lapsensor_get(&has_lapsensor, &state);
10171 if (lap_state != state) {
10173 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10178 static void palmsensor_refresh(void)
10183 if (has_palmsensor) {
10184 err = palmsensor_get(&has_palmsensor, &state);
10187 if (palm_state != state) {
10188 palm_state = state;
10189 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10194 static ssize_t dytc_lapmode_show(struct device *dev,
10195 struct device_attribute *attr,
10199 return sysfs_emit(buf, "%d\n", lap_state);
10200 return sysfs_emit(buf, "\n");
10202 static DEVICE_ATTR_RO(dytc_lapmode);
10204 static ssize_t palmsensor_show(struct device *dev,
10205 struct device_attribute *attr,
10208 if (has_palmsensor)
10209 return sysfs_emit(buf, "%d\n", palm_state);
10210 return sysfs_emit(buf, "\n");
10212 static DEVICE_ATTR_RO(palmsensor);
10214 static struct attribute *proxsensor_attributes[] = {
10215 &dev_attr_dytc_lapmode.attr,
10216 &dev_attr_palmsensor.attr,
10220 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10221 struct attribute *attr, int n)
10223 if (attr == &dev_attr_dytc_lapmode.attr) {
10225 * Platforms before DYTC version 5 claim to have a lap sensor,
10226 * but it doesn't work, so we ignore them.
10228 if (!has_lapsensor || dytc_version < 5)
10230 } else if (attr == &dev_attr_palmsensor.attr) {
10231 if (!has_palmsensor)
10238 static const struct attribute_group proxsensor_attr_group = {
10239 .is_visible = proxsensor_attr_is_visible,
10240 .attrs = proxsensor_attributes,
10243 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10245 int palm_err, lap_err;
10247 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10248 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10249 /* If support isn't available for both devices return -ENODEV */
10250 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10252 /* Otherwise, if there was an error return it */
10253 if (palm_err && (palm_err != -ENODEV))
10255 if (lap_err && (lap_err != -ENODEV))
10261 static struct ibm_struct proxsensor_driver_data = {
10262 .name = "proximity-sensor",
10265 /*************************************************************************
10266 * DYTC Platform Profile interface
10269 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10270 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10271 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10273 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10274 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10275 #define DYTC_FC_PSC 29 /* PSC Mode supported */
10276 #define DYTC_FC_AMT 31 /* AMT mode supported */
10278 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10279 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10281 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10282 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10283 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10285 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10286 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10287 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
10288 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
10289 #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
10291 #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
10292 #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
10294 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
10295 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
10296 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
10297 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
10299 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
10300 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
10301 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
10303 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10304 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10306 #define DYTC_SET_COMMAND(function, mode, on) \
10307 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10308 (mode) << DYTC_SET_MODE_BIT | \
10309 (on) << DYTC_SET_VALID_BIT)
10311 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
10312 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
10313 static int dytc_control_amt(bool enable);
10314 static bool dytc_amt_active;
10316 static enum platform_profile_option dytc_current_profile;
10317 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10318 static DEFINE_MUTEX(dytc_mutex);
10319 static int dytc_capabilities;
10320 static bool dytc_mmc_get_available;
10321 static int profile_force;
10323 static int convert_dytc_to_profile(int funcmode, int dytcmode,
10324 enum platform_profile_option *profile)
10326 switch (funcmode) {
10327 case DYTC_FUNCTION_MMC:
10328 switch (dytcmode) {
10329 case DYTC_MODE_MMC_LOWPOWER:
10330 *profile = PLATFORM_PROFILE_LOW_POWER;
10332 case DYTC_MODE_MMC_DEFAULT:
10333 case DYTC_MODE_MMC_BALANCE:
10334 *profile = PLATFORM_PROFILE_BALANCED;
10336 case DYTC_MODE_MMC_PERFORM:
10337 *profile = PLATFORM_PROFILE_PERFORMANCE;
10339 default: /* Unknown mode */
10343 case DYTC_FUNCTION_PSC:
10344 switch (dytcmode) {
10345 case DYTC_MODE_PSC_LOWPOWER:
10346 *profile = PLATFORM_PROFILE_LOW_POWER;
10348 case DYTC_MODE_PSC_BALANCE:
10349 *profile = PLATFORM_PROFILE_BALANCED;
10351 case DYTC_MODE_PSC_PERFORM:
10352 *profile = PLATFORM_PROFILE_PERFORMANCE;
10354 default: /* Unknown mode */
10358 case DYTC_FUNCTION_AMT:
10359 /* For now return balanced. It's the closest we have to 'auto' */
10360 *profile = PLATFORM_PROFILE_BALANCED;
10363 /* Unknown function */
10364 return -EOPNOTSUPP;
10369 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10372 case PLATFORM_PROFILE_LOW_POWER:
10373 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10374 *perfmode = DYTC_MODE_MMC_LOWPOWER;
10375 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10376 *perfmode = DYTC_MODE_PSC_LOWPOWER;
10378 case PLATFORM_PROFILE_BALANCED:
10379 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10380 *perfmode = DYTC_MODE_MMC_BALANCE;
10381 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10382 *perfmode = DYTC_MODE_PSC_BALANCE;
10384 case PLATFORM_PROFILE_PERFORMANCE:
10385 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10386 *perfmode = DYTC_MODE_MMC_PERFORM;
10387 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10388 *perfmode = DYTC_MODE_PSC_PERFORM;
10390 default: /* Unknown profile */
10391 return -EOPNOTSUPP;
10397 * dytc_profile_get: Function to register with platform_profile
10398 * handler. Returns current platform profile.
10400 static int dytc_profile_get(struct platform_profile_handler *pprof,
10401 enum platform_profile_option *profile)
10403 *profile = dytc_current_profile;
10407 static int dytc_control_amt(bool enable)
10413 if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
10414 pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
10419 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
10421 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
10423 pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
10424 err = dytc_command(cmd, &dummy);
10427 dytc_amt_active = enable;
10432 * Helper function - check if we are in CQL mode and if we are
10434 * - run the command
10436 * If not in CQL mode, just run the command
10438 static int dytc_cql_command(int command, int *output)
10440 int err, cmd_err, dummy;
10443 /* Determine if we are in CQL mode. This alters the commands we do */
10444 err = dytc_command(DYTC_CMD_GET, output);
10448 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10449 /* Check if we're OK to return immediately */
10450 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10453 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10454 atomic_inc(&dytc_ignore_event);
10455 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10460 cmd_err = dytc_command(command, output);
10461 /* Check return condition after we've restored CQL state */
10463 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10464 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10472 * dytc_profile_set: Function to register with platform_profile
10473 * handler. Sets current platform profile.
10475 static int dytc_profile_set(struct platform_profile_handler *pprof,
10476 enum platform_profile_option profile)
10482 err = mutex_lock_interruptible(&dytc_mutex);
10486 err = convert_profile_to_dytc(profile, &perfmode);
10490 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10491 if (profile == PLATFORM_PROFILE_BALANCED) {
10493 * To get back to balanced mode we need to issue a reset command.
10494 * Note we still need to disable CQL mode before hand and re-enable
10495 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10496 * stuck at 0 for aprox. 30 minutes.
10498 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10502 /* Determine if we are in CQL mode. This alters the commands we do */
10503 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
10508 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10509 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
10513 /* system supports AMT, activate it when on balanced */
10514 if (dytc_capabilities & BIT(DYTC_FC_AMT))
10515 dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
10517 /* Success - update current profile */
10518 dytc_current_profile = profile;
10520 mutex_unlock(&dytc_mutex);
10524 static void dytc_profile_refresh(void)
10526 enum platform_profile_option profile;
10527 int output, err = 0;
10528 int perfmode, funcmode;
10530 mutex_lock(&dytc_mutex);
10531 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10532 if (dytc_mmc_get_available)
10533 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10535 err = dytc_cql_command(DYTC_CMD_GET, &output);
10536 funcmode = DYTC_FUNCTION_MMC;
10537 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10538 err = dytc_command(DYTC_CMD_GET, &output);
10539 /* Check if we are PSC mode, or have AMT enabled */
10540 funcmode = (output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10542 mutex_unlock(&dytc_mutex);
10546 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10547 convert_dytc_to_profile(funcmode, perfmode, &profile);
10548 if (profile != dytc_current_profile) {
10549 dytc_current_profile = profile;
10550 platform_profile_notify();
10554 static struct platform_profile_handler dytc_profile = {
10555 .profile_get = dytc_profile_get,
10556 .profile_set = dytc_profile_set,
10559 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10563 /* Setup supported modes */
10564 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10565 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10566 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10568 err = dytc_command(DYTC_CMD_QUERY, &output);
10572 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10573 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10575 /* Check DYTC is enabled and supports mode setting */
10576 if (dytc_version < 5)
10579 /* Check what capabilities are supported */
10580 err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
10584 /* Check if user wants to override the profile selection */
10585 if (profile_force) {
10586 switch (profile_force) {
10588 dytc_capabilities = 0;
10591 dytc_capabilities = BIT(DYTC_FC_MMC);
10594 dytc_capabilities = BIT(DYTC_FC_PSC);
10597 pr_debug("Profile selection forced: 0x%x\n", dytc_capabilities);
10599 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
10600 pr_debug("MMC is supported\n");
10602 * Check if MMC_GET functionality available
10603 * Version > 6 and return success from MMC_GET command
10605 dytc_mmc_get_available = false;
10606 if (dytc_version >= 6) {
10607 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10608 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10609 dytc_mmc_get_available = true;
10611 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
10612 pr_debug("PSC is supported\n");
10614 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
10618 dbg_printk(TPACPI_DBG_INIT,
10619 "DYTC version %d: thermal mode available\n", dytc_version);
10621 /* Create platform_profile structure and register */
10622 err = platform_profile_register(&dytc_profile);
10624 * If for some reason platform_profiles aren't enabled
10625 * don't quit terminally.
10630 /* Ensure initial values are correct */
10631 dytc_profile_refresh();
10633 /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
10634 if (dytc_capabilities & BIT(DYTC_FC_PSC))
10635 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
10640 static void dytc_profile_exit(void)
10642 platform_profile_remove();
10645 static struct ibm_struct dytc_profile_driver_data = {
10646 .name = "dytc-profile",
10647 .exit = dytc_profile_exit,
10650 /*************************************************************************
10651 * Keyboard language interface
10654 struct keyboard_lang_data {
10655 const char *lang_str;
10659 static const struct keyboard_lang_data keyboard_lang_data[] = {
10681 static int set_keyboard_lang_command(int command)
10683 acpi_handle sskl_handle;
10686 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10687 /* Platform doesn't support SSKL */
10691 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10697 static int get_keyboard_lang(int *output)
10699 acpi_handle gskl_handle;
10702 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10703 /* Platform doesn't support GSKL */
10707 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10711 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10712 * '(' and ')') keys which use layout dependent key-press emulation.
10714 if (kbd_lang & METHOD_ERR)
10717 *output = kbd_lang;
10722 /* sysfs keyboard language entry */
10723 static ssize_t keyboard_lang_show(struct device *dev,
10724 struct device_attribute *attr,
10727 int output, err, i, len = 0;
10729 err = get_keyboard_lang(&output);
10733 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10735 len += sysfs_emit_at(buf, len, "%s", " ");
10737 if (output == keyboard_lang_data[i].lang_code) {
10738 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10740 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10743 len += sysfs_emit_at(buf, len, "\n");
10748 static ssize_t keyboard_lang_store(struct device *dev,
10749 struct device_attribute *attr,
10750 const char *buf, size_t count)
10753 bool lang_found = false;
10756 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10757 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10758 lang_code = keyboard_lang_data[i].lang_code;
10765 lang_code = lang_code | 1 << 24;
10767 /* Set language code */
10768 err = set_keyboard_lang_command(lang_code);
10772 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10776 tpacpi_disclose_usertask(attr->attr.name,
10777 "keyboard language is set to %s\n", buf);
10779 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10783 static DEVICE_ATTR_RW(keyboard_lang);
10785 static struct attribute *kbdlang_attributes[] = {
10786 &dev_attr_keyboard_lang.attr,
10790 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10791 struct attribute *attr, int n)
10793 return tp_features.kbd_lang ? attr->mode : 0;
10796 static const struct attribute_group kbdlang_attr_group = {
10797 .is_visible = kbdlang_attr_is_visible,
10798 .attrs = kbdlang_attributes,
10801 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10805 err = get_keyboard_lang(&output);
10806 tp_features.kbd_lang = !err;
10810 static struct ibm_struct kbdlang_driver_data = {
10814 /*************************************************************************
10815 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10816 * and WLAN feature.
10818 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10819 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10820 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10821 static bool has_antennatype;
10822 static int wwan_antennatype;
10824 static int dprc_command(int command, int *output)
10826 acpi_handle dprc_handle;
10828 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10829 /* Platform doesn't support DPRC */
10833 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10837 * METHOD_ERR gets returned on devices where few commands are not supported
10838 * for example command to get WWAN Antenna type command is not supported on
10841 if (*output & METHOD_ERR)
10847 static int get_wwan_antenna(int *wwan_antennatype)
10851 /* Get current Antenna type */
10852 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10856 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10857 *wwan_antennatype = 1;
10858 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10859 *wwan_antennatype = 2;
10866 /* sysfs wwan antenna type entry */
10867 static ssize_t wwan_antenna_type_show(struct device *dev,
10868 struct device_attribute *attr,
10871 switch (wwan_antennatype) {
10873 return sysfs_emit(buf, "type a\n");
10875 return sysfs_emit(buf, "type b\n");
10880 static DEVICE_ATTR_RO(wwan_antenna_type);
10882 static struct attribute *dprc_attributes[] = {
10883 &dev_attr_wwan_antenna_type.attr,
10887 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10888 struct attribute *attr, int n)
10890 return has_antennatype ? attr->mode : 0;
10893 static const struct attribute_group dprc_attr_group = {
10894 .is_visible = dprc_attr_is_visible,
10895 .attrs = dprc_attributes,
10898 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10902 err = get_wwan_antenna(&wwan_antennatype);
10906 has_antennatype = true;
10910 static struct ibm_struct dprc_driver_data = {
10914 /* --------------------------------------------------------------------- */
10916 static struct attribute *tpacpi_driver_attributes[] = {
10917 &driver_attr_debug_level.attr,
10918 &driver_attr_version.attr,
10919 &driver_attr_interface_version.attr,
10920 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10921 &driver_attr_wlsw_emulstate.attr,
10922 &driver_attr_bluetooth_emulstate.attr,
10923 &driver_attr_wwan_emulstate.attr,
10924 &driver_attr_uwb_emulstate.attr,
10929 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10930 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
10931 struct attribute *attr, int n)
10933 if (attr == &driver_attr_wlsw_emulstate.attr) {
10936 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
10937 if (!dbg_bluetoothemul)
10939 } else if (attr == &driver_attr_wwan_emulstate.attr) {
10942 } else if (attr == &driver_attr_uwb_emulstate.attr) {
10951 static const struct attribute_group tpacpi_driver_attr_group = {
10952 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10953 .is_visible = tpacpi_attr_is_visible,
10955 .attrs = tpacpi_driver_attributes,
10958 static const struct attribute_group *tpacpi_driver_groups[] = {
10959 &tpacpi_driver_attr_group,
10963 static const struct attribute_group *tpacpi_groups[] = {
10964 &adaptive_kbd_attr_group,
10965 &hotkey_attr_group,
10966 &bluetooth_attr_group,
10969 &proxsensor_attr_group,
10970 &kbdlang_attr_group,
10975 static const struct attribute_group *tpacpi_hwmon_groups[] = {
10976 &thermal_attr_group,
10977 &temp_label_attr_group,
10982 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
10983 &fan_driver_attr_group,
10987 /****************************************************************************
10988 ****************************************************************************
10992 ****************************************************************************
10993 ****************************************************************************/
10995 static struct platform_driver tpacpi_pdriver = {
10997 .name = TPACPI_DRVR_NAME,
10999 .groups = tpacpi_driver_groups,
11000 .dev_groups = tpacpi_groups,
11002 .shutdown = tpacpi_shutdown_handler,
11005 static struct platform_driver tpacpi_hwmon_pdriver = {
11007 .name = TPACPI_HWMON_DRVR_NAME,
11008 .groups = tpacpi_hwmon_driver_groups,
11012 /****************************************************************************
11013 ****************************************************************************
11017 ****************************************************************************
11018 ****************************************************************************/
11021 * HKEY event callout for other subdrivers go here
11022 * (yes, it is ugly, but it is quick, safe, and gets the job done
11024 static void tpacpi_driver_event(const unsigned int hkey_event)
11026 if (ibm_backlight_device) {
11027 switch (hkey_event) {
11028 case TP_HKEY_EV_BRGHT_UP:
11029 case TP_HKEY_EV_BRGHT_DOWN:
11030 tpacpi_brightness_notify_change();
11034 switch (hkey_event) {
11035 case TP_HKEY_EV_VOL_UP:
11036 case TP_HKEY_EV_VOL_DOWN:
11037 case TP_HKEY_EV_VOL_MUTE:
11038 volume_alsa_notify_change();
11041 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
11042 enum led_brightness brightness;
11044 mutex_lock(&kbdlight_mutex);
11047 * Check the brightness actually changed, setting the brightness
11048 * through kbdlight_set_level() also triggers this event.
11050 brightness = kbdlight_sysfs_get(NULL);
11051 if (kbdlight_brightness != brightness) {
11052 kbdlight_brightness = brightness;
11053 led_classdev_notify_brightness_hw_changed(
11054 &tpacpi_led_kbdlight.led_classdev, brightness);
11057 mutex_unlock(&kbdlight_mutex);
11060 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
11061 lapsensor_refresh();
11062 /* If we are already accessing DYTC then skip dytc update */
11063 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
11064 dytc_profile_refresh();
11067 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
11068 enum drm_privacy_screen_status old_hw_state;
11071 mutex_lock(&lcdshadow_dev->lock);
11072 old_hw_state = lcdshadow_dev->hw_state;
11073 lcdshadow_get_hw_state(lcdshadow_dev);
11074 changed = lcdshadow_dev->hw_state != old_hw_state;
11075 mutex_unlock(&lcdshadow_dev->lock);
11078 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
11080 if (hkey_event == TP_HKEY_EV_AMT_TOGGLE) {
11081 /* If we're enabling AMT we need to force balanced mode */
11082 if (!dytc_amt_active)
11083 /* This will also set AMT mode enabled */
11084 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
11086 dytc_control_amt(!dytc_amt_active);
11091 static void hotkey_driver_event(const unsigned int scancode)
11093 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
11096 /* --------------------------------------------------------------------- */
11098 /* /proc support */
11099 static struct proc_dir_entry *proc_dir;
11102 * Module and infrastructure proble, init and exit handling
11105 static bool force_load;
11107 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
11108 static const char * __init str_supported(int is_supported)
11110 static char text_unsupported[] __initdata = "not supported";
11112 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
11114 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
11116 static void ibm_exit(struct ibm_struct *ibm)
11118 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
11120 list_del_init(&ibm->all_drivers);
11122 if (ibm->flags.acpi_notify_installed) {
11123 dbg_printk(TPACPI_DBG_EXIT,
11124 "%s: acpi_remove_notify_handler\n", ibm->name);
11125 BUG_ON(!ibm->acpi);
11126 acpi_remove_notify_handler(*ibm->acpi->handle,
11128 dispatch_acpi_notify);
11129 ibm->flags.acpi_notify_installed = 0;
11132 if (ibm->flags.proc_created) {
11133 dbg_printk(TPACPI_DBG_EXIT,
11134 "%s: remove_proc_entry\n", ibm->name);
11135 remove_proc_entry(ibm->name, proc_dir);
11136 ibm->flags.proc_created = 0;
11139 if (ibm->flags.acpi_driver_registered) {
11140 dbg_printk(TPACPI_DBG_EXIT,
11141 "%s: acpi_bus_unregister_driver\n", ibm->name);
11142 BUG_ON(!ibm->acpi);
11143 acpi_bus_unregister_driver(ibm->acpi->driver);
11144 kfree(ibm->acpi->driver);
11145 ibm->acpi->driver = NULL;
11146 ibm->flags.acpi_driver_registered = 0;
11149 if (ibm->flags.init_called && ibm->exit) {
11151 ibm->flags.init_called = 0;
11154 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
11157 static int __init ibm_init(struct ibm_init_struct *iibm)
11160 struct ibm_struct *ibm = iibm->data;
11161 struct proc_dir_entry *entry;
11163 BUG_ON(ibm == NULL);
11165 INIT_LIST_HEAD(&ibm->all_drivers);
11167 if (ibm->flags.experimental && !experimental)
11170 dbg_printk(TPACPI_DBG_INIT,
11171 "probing for %s\n", ibm->name);
11174 ret = iibm->init(iibm);
11175 if (ret > 0 || ret == -ENODEV)
11176 return 0; /* subdriver functionality not available */
11180 ibm->flags.init_called = 1;
11184 if (ibm->acpi->hid) {
11185 ret = register_tpacpi_subdriver(ibm);
11190 if (ibm->acpi->notify) {
11191 ret = setup_acpi_notify(ibm);
11192 if (ret == -ENODEV) {
11193 pr_notice("disabling subdriver %s\n",
11203 dbg_printk(TPACPI_DBG_INIT,
11204 "%s installed\n", ibm->name);
11207 umode_t mode = iibm->base_procfs_mode;
11213 entry = proc_create_data(ibm->name, mode, proc_dir,
11214 &dispatch_proc_ops, ibm);
11216 pr_err("unable to create proc entry %s\n", ibm->name);
11220 ibm->flags.proc_created = 1;
11223 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
11228 dbg_printk(TPACPI_DBG_INIT,
11229 "%s: at error exit path with result %d\n",
11233 return (ret < 0) ? ret : 0;
11238 static char __init tpacpi_parse_fw_id(const char * const s,
11239 u32 *model, u16 *release)
11243 if (!s || strlen(s) < 8)
11246 for (i = 0; i < 8; i++)
11247 if (!((s[i] >= '0' && s[i] <= '9') ||
11248 (s[i] >= 'A' && s[i] <= 'Z')))
11252 * Most models: xxyTkkWW (#.##c)
11253 * Ancient 570/600 and -SL lacks (#.##c)
11255 if (s[3] == 'T' || s[3] == 'N') {
11256 *model = TPID(s[0], s[1]);
11257 *release = TPVER(s[4], s[5]);
11260 /* New models: xxxyTkkW (#.##c); T550 and some others */
11261 } else if (s[4] == 'T' || s[4] == 'N') {
11262 *model = TPID3(s[0], s[1], s[2]);
11263 *release = TPVER(s[5], s[6]);
11271 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
11273 char *ec_fw_string = (char *) private;
11274 const char *dmi_data = (const char *)dm;
11276 * ThinkPad Embedded Controller Program Table on newer models
11278 * Offset | Name | Width | Description
11279 * ----------------------------------------------------
11280 * 0x00 | Type | BYTE | 0x8C
11281 * 0x01 | Length | BYTE |
11282 * 0x02 | Handle | WORD | Varies
11283 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11284 * 0x0A | OEM struct offset | BYTE | 0x0B
11285 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11286 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11287 * 0x0D | ECP version ID | STR ID |
11288 * 0x0E | ECP release date | STR ID |
11291 /* Return if data structure not match */
11292 if (dm->type != 140 || dm->length < 0x0F ||
11293 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11294 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11295 dmi_data[0x0C] != 0x01)
11298 /* fwstr is the first 8byte string */
11299 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
11302 /* returns 0 - probe ok, or < 0 - probe error.
11303 * Probe ok doesn't mean thinkpad found.
11304 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11305 static int __must_check __init get_thinkpad_model_data(
11306 struct thinkpad_id_data *tp)
11308 const struct dmi_device *dev = NULL;
11309 char ec_fw_string[18] = {0};
11316 memset(tp, 0, sizeof(*tp));
11318 if (dmi_name_in_vendors("IBM"))
11319 tp->vendor = PCI_VENDOR_ID_IBM;
11320 else if (dmi_name_in_vendors("LENOVO"))
11321 tp->vendor = PCI_VENDOR_ID_LENOVO;
11325 s = dmi_get_system_info(DMI_BIOS_VERSION);
11326 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11327 if (s && !tp->bios_version_str)
11330 /* Really ancient ThinkPad 240X will fail this, which is fine */
11331 t = tpacpi_parse_fw_id(tp->bios_version_str,
11332 &tp->bios_model, &tp->bios_release);
11333 if (t != 'E' && t != 'C')
11337 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11338 * X32 or newer, all Z series; Some models must have an
11339 * up-to-date BIOS or they will not be detected.
11341 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11343 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11344 if (sscanf(dev->name,
11345 "IBM ThinkPad Embedded Controller -[%17c",
11346 ec_fw_string) == 1) {
11347 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11348 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11353 /* Newer ThinkPads have different EC program info table */
11354 if (!ec_fw_string[0])
11355 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11357 if (ec_fw_string[0]) {
11358 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11359 if (!tp->ec_version_str)
11362 t = tpacpi_parse_fw_id(ec_fw_string,
11363 &tp->ec_model, &tp->ec_release);
11365 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11367 pr_notice("please report this to %s\n", TPACPI_MAIL);
11371 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11372 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11373 tp->model_str = kstrdup(s, GFP_KERNEL);
11374 if (!tp->model_str)
11377 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11378 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11379 tp->model_str = kstrdup(s, GFP_KERNEL);
11380 if (!tp->model_str)
11385 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11386 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11387 if (s && !tp->nummodel_str)
11393 static int __init probe_for_thinkpad(void)
11400 /* It would be dangerous to run the driver in this case */
11401 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11405 * Non-ancient models have better DMI tagging, but very old models
11406 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11408 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11409 (thinkpad_id.ec_model != 0) ||
11410 tpacpi_is_fw_known();
11412 /* The EC handler is required */
11413 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11416 pr_err("Not yet supported ThinkPad detected!\n");
11420 if (!is_thinkpad && !force_load)
11426 static void __init thinkpad_acpi_init_banner(void)
11428 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11429 pr_info("%s\n", TPACPI_URL);
11431 pr_info("ThinkPad BIOS %s, EC %s\n",
11432 (thinkpad_id.bios_version_str) ?
11433 thinkpad_id.bios_version_str : "unknown",
11434 (thinkpad_id.ec_version_str) ?
11435 thinkpad_id.ec_version_str : "unknown");
11437 BUG_ON(!thinkpad_id.vendor);
11439 if (thinkpad_id.model_str)
11440 pr_info("%s %s, model %s\n",
11441 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11442 "IBM" : ((thinkpad_id.vendor ==
11443 PCI_VENDOR_ID_LENOVO) ?
11444 "Lenovo" : "Unknown vendor"),
11445 thinkpad_id.model_str,
11446 (thinkpad_id.nummodel_str) ?
11447 thinkpad_id.nummodel_str : "unknown");
11450 /* Module init, exit, parameters */
11452 static struct ibm_init_struct ibms_init[] __initdata = {
11454 .data = &thinkpad_acpi_driver_data,
11457 .init = hotkey_init,
11458 .data = &hotkey_driver_data,
11461 .init = bluetooth_init,
11462 .data = &bluetooth_driver_data,
11466 .data = &wan_driver_data,
11470 .data = &uwb_driver_data,
11472 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11474 .init = video_init,
11475 .base_procfs_mode = S_IRUSR,
11476 .data = &video_driver_data,
11480 .init = kbdlight_init,
11481 .data = &kbdlight_driver_data,
11484 .init = light_init,
11485 .data = &light_driver_data,
11489 .data = &cmos_driver_data,
11493 .data = &led_driver_data,
11497 .data = &beep_driver_data,
11500 .init = thermal_init,
11501 .data = &thermal_driver_data,
11504 .init = brightness_init,
11505 .data = &brightness_driver_data,
11508 .init = volume_init,
11509 .data = &volume_driver_data,
11513 .data = &fan_driver_data,
11516 .init = mute_led_init,
11517 .data = &mute_led_driver_data,
11520 .init = tpacpi_battery_init,
11521 .data = &battery_driver_data,
11524 .init = tpacpi_lcdshadow_init,
11525 .data = &lcdshadow_driver_data,
11528 .init = tpacpi_proxsensor_init,
11529 .data = &proxsensor_driver_data,
11532 .init = tpacpi_dytc_profile_init,
11533 .data = &dytc_profile_driver_data,
11536 .init = tpacpi_kbdlang_init,
11537 .data = &kbdlang_driver_data,
11540 .init = tpacpi_dprc_init,
11541 .data = &dprc_driver_data,
11545 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11548 struct ibm_struct *ibm;
11550 if (!kp || !kp->name || !val)
11553 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11554 ibm = ibms_init[i].data;
11555 if (!ibm || !ibm->name)
11558 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11559 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11561 strcpy(ibms_init[i].param, val);
11569 module_param(experimental, int, 0444);
11570 MODULE_PARM_DESC(experimental,
11571 "Enables experimental features when non-zero");
11573 module_param_named(debug, dbg_level, uint, 0);
11574 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11576 module_param(force_load, bool, 0444);
11577 MODULE_PARM_DESC(force_load,
11578 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11580 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11581 MODULE_PARM_DESC(fan_control,
11582 "Enables setting fan parameters features when true");
11584 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11585 MODULE_PARM_DESC(brightness_mode,
11586 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11588 module_param(brightness_enable, uint, 0444);
11589 MODULE_PARM_DESC(brightness_enable,
11590 "Enables backlight control when 1, disables when 0");
11592 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11593 module_param_named(volume_mode, volume_mode, uint, 0444);
11594 MODULE_PARM_DESC(volume_mode,
11595 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11597 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11598 MODULE_PARM_DESC(volume_capabilities,
11599 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11601 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11602 MODULE_PARM_DESC(volume_control,
11603 "Enables software override for the console audio control when true");
11605 module_param_named(software_mute, software_mute_requested, bool, 0444);
11606 MODULE_PARM_DESC(software_mute,
11607 "Request full software mute control");
11609 /* ALSA module API parameters */
11610 module_param_named(index, alsa_index, int, 0444);
11611 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11612 module_param_named(id, alsa_id, charp, 0444);
11613 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11614 module_param_named(enable, alsa_enable, bool, 0444);
11615 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11616 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11618 /* The module parameter can't be read back, that's why 0 is used here */
11619 #define TPACPI_PARAM(feature) \
11620 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11621 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11623 TPACPI_PARAM(hotkey);
11624 TPACPI_PARAM(bluetooth);
11625 TPACPI_PARAM(video);
11626 TPACPI_PARAM(light);
11627 TPACPI_PARAM(cmos);
11629 TPACPI_PARAM(beep);
11630 TPACPI_PARAM(brightness);
11631 TPACPI_PARAM(volume);
11634 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11635 module_param(dbg_wlswemul, uint, 0444);
11636 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11637 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11638 MODULE_PARM_DESC(wlsw_state,
11639 "Initial state of the emulated WLSW switch");
11641 module_param(dbg_bluetoothemul, uint, 0444);
11642 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11643 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11644 MODULE_PARM_DESC(bluetooth_state,
11645 "Initial state of the emulated bluetooth switch");
11647 module_param(dbg_wwanemul, uint, 0444);
11648 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11649 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11650 MODULE_PARM_DESC(wwan_state,
11651 "Initial state of the emulated WWAN switch");
11653 module_param(dbg_uwbemul, uint, 0444);
11654 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11655 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11656 MODULE_PARM_DESC(uwb_state,
11657 "Initial state of the emulated UWB switch");
11660 module_param(profile_force, int, 0444);
11661 MODULE_PARM_DESC(profile_force, "Force profile mode. -1=off, 1=MMC, 2=PSC");
11663 static void thinkpad_acpi_module_exit(void)
11665 struct ibm_struct *ibm, *itmp;
11667 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11669 #ifdef CONFIG_SUSPEND
11670 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio)
11671 acpi_unregister_lps0_dev(&thinkpad_acpi_s2idle_dev_ops);
11674 hwmon_device_unregister(tpacpi_hwmon);
11675 if (tp_features.sensors_pdrv_registered)
11676 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11677 if (tp_features.platform_drv_registered)
11678 platform_driver_unregister(&tpacpi_pdriver);
11680 list_for_each_entry_safe_reverse(ibm, itmp,
11681 &tpacpi_all_drivers,
11686 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11688 if (tpacpi_inputdev) {
11689 if (tp_features.input_device_registered)
11690 input_unregister_device(tpacpi_inputdev);
11692 input_free_device(tpacpi_inputdev);
11693 kfree(hotkey_keycode_map);
11696 if (tpacpi_sensors_pdev)
11697 platform_device_unregister(tpacpi_sensors_pdev);
11699 platform_device_unregister(tpacpi_pdev);
11701 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11703 destroy_workqueue(tpacpi_wq);
11705 kfree(thinkpad_id.bios_version_str);
11706 kfree(thinkpad_id.ec_version_str);
11707 kfree(thinkpad_id.model_str);
11708 kfree(thinkpad_id.nummodel_str);
11712 static int __init thinkpad_acpi_module_init(void)
11714 const struct dmi_system_id *dmi_id;
11716 acpi_object_type obj_type;
11718 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11720 /* Driver-level probe */
11722 ret = get_thinkpad_model_data(&thinkpad_id);
11724 pr_err("unable to get DMI data: %d\n", ret);
11725 thinkpad_acpi_module_exit();
11728 ret = probe_for_thinkpad();
11730 thinkpad_acpi_module_exit();
11734 /* Driver initialization */
11736 thinkpad_acpi_init_banner();
11737 tpacpi_check_outdated_fw();
11739 TPACPI_ACPIHANDLE_INIT(ecrd);
11740 TPACPI_ACPIHANDLE_INIT(ecwr);
11743 * Quirk: in some models (e.g. X380 Yoga), an object named ECRD
11744 * exists, but it is a register, not a method.
11747 acpi_get_type(ecrd_handle, &obj_type);
11748 if (obj_type != ACPI_TYPE_METHOD)
11749 ecrd_handle = NULL;
11752 acpi_get_type(ecwr_handle, &obj_type);
11753 if (obj_type != ACPI_TYPE_METHOD)
11754 ecwr_handle = NULL;
11757 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11759 thinkpad_acpi_module_exit();
11763 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11765 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11766 thinkpad_acpi_module_exit();
11770 dmi_id = dmi_first_match(fwbug_list);
11772 tp_features.quirks = dmi_id->driver_data;
11774 /* Device initialization */
11775 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
11777 if (IS_ERR(tpacpi_pdev)) {
11778 ret = PTR_ERR(tpacpi_pdev);
11779 tpacpi_pdev = NULL;
11780 pr_err("unable to register platform device\n");
11781 thinkpad_acpi_module_exit();
11784 tpacpi_sensors_pdev = platform_device_register_simple(
11785 TPACPI_HWMON_DRVR_NAME,
11786 PLATFORM_DEVID_NONE, NULL, 0);
11787 if (IS_ERR(tpacpi_sensors_pdev)) {
11788 ret = PTR_ERR(tpacpi_sensors_pdev);
11789 tpacpi_sensors_pdev = NULL;
11790 pr_err("unable to register hwmon platform device\n");
11791 thinkpad_acpi_module_exit();
11795 mutex_init(&tpacpi_inputdev_send_mutex);
11796 tpacpi_inputdev = input_allocate_device();
11797 if (!tpacpi_inputdev) {
11798 thinkpad_acpi_module_exit();
11801 /* Prepare input device, but don't register */
11802 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11803 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11804 tpacpi_inputdev->id.bustype = BUS_HOST;
11805 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11806 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11807 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11808 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11811 /* Init subdriver dependencies */
11812 tpacpi_detect_brightness_capabilities();
11814 /* Init subdrivers */
11815 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11816 ret = ibm_init(&ibms_init[i]);
11817 if (ret >= 0 && *ibms_init[i].param)
11818 ret = ibms_init[i].data->write(ibms_init[i].param);
11820 thinkpad_acpi_module_exit();
11825 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11827 ret = platform_driver_register(&tpacpi_pdriver);
11829 pr_err("unable to register main platform driver\n");
11830 thinkpad_acpi_module_exit();
11833 tp_features.platform_drv_registered = 1;
11835 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11837 pr_err("unable to register hwmon platform driver\n");
11838 thinkpad_acpi_module_exit();
11841 tp_features.sensors_pdrv_registered = 1;
11843 tpacpi_hwmon = hwmon_device_register_with_groups(
11844 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11845 if (IS_ERR(tpacpi_hwmon)) {
11846 ret = PTR_ERR(tpacpi_hwmon);
11847 tpacpi_hwmon = NULL;
11848 pr_err("unable to register hwmon device\n");
11849 thinkpad_acpi_module_exit();
11853 ret = input_register_device(tpacpi_inputdev);
11855 pr_err("unable to register input device\n");
11856 thinkpad_acpi_module_exit();
11859 tp_features.input_device_registered = 1;
11862 #ifdef CONFIG_SUSPEND
11863 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio) {
11864 if (!acpi_register_lps0_dev(&thinkpad_acpi_s2idle_dev_ops))
11865 pr_info("Using s2idle quirk to avoid %s platform firmware bug\n",
11866 (dmi_id && dmi_id->ident) ? dmi_id->ident : "");
11872 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11875 * This will autoload the driver in almost every ThinkPad
11876 * in widespread use.
11878 * Only _VERY_ old models, like the 240, 240x and 570 lack
11879 * the HKEY event interface.
11881 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11884 * DMI matching for module autoloading
11886 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11887 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
11889 * Only models listed in thinkwiki will be supported, so add yours
11890 * if it is not there yet.
11892 #define IBM_BIOS_MODULE_ALIAS(__type) \
11893 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
11895 /* Ancient thinkpad BIOSes have to be identified by
11896 * BIOS type or model number, and there are far less
11897 * BIOS types than model numbers... */
11898 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
11902 MODULE_DESCRIPTION(TPACPI_DESC);
11903 MODULE_VERSION(TPACPI_VERSION);
11904 MODULE_LICENSE("GPL");
11906 module_init(thinkpad_acpi_module_init);
11907 module_exit(thinkpad_acpi_module_exit);