xref: /OK3568_Linux_fs/kernel/drivers/platform/x86/thinkpad_acpi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  thinkpad_acpi.c - ThinkPad ACPI Extras
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6*4882a593Smuzhiyun  *  Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define TPACPI_VERSION "0.26"
12*4882a593Smuzhiyun #define TPACPI_SYSFS_VERSION 0x030000
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  *  Changelog:
16*4882a593Smuzhiyun  *  2007-10-20		changelog trimmed down
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *  2007-03-27  0.14	renamed to thinkpad_acpi and moved to
19*4882a593Smuzhiyun  *  			drivers/misc.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *  2006-11-22	0.13	new maintainer
22*4882a593Smuzhiyun  *  			changelog now lives in git commit history, and will
23*4882a593Smuzhiyun  *  			not be updated further in-file.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  *  2005-03-17	0.11	support for 600e, 770x
26*4882a593Smuzhiyun  *			    thanks to Jamie Lentin <lentinj@dial.pipex.com>
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *  2005-01-16	0.9	use MODULE_VERSION
29*4882a593Smuzhiyun  *			    thanks to Henrik Brix Andersen <brix@gentoo.org>
30*4882a593Smuzhiyun  *			fix parameter passing on module loading
31*4882a593Smuzhiyun  *			    thanks to Rusty Russell <rusty@rustcorp.com.au>
32*4882a593Smuzhiyun  *			    thanks to Jim Radford <radford@blackbean.org>
33*4882a593Smuzhiyun  *  2004-11-08	0.8	fix init error case, don't return from a macro
34*4882a593Smuzhiyun  *			    thanks to Chris Wright <chrisw@osdl.org>
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <linux/kernel.h>
38*4882a593Smuzhiyun #include <linux/module.h>
39*4882a593Smuzhiyun #include <linux/init.h>
40*4882a593Smuzhiyun #include <linux/types.h>
41*4882a593Smuzhiyun #include <linux/string.h>
42*4882a593Smuzhiyun #include <linux/list.h>
43*4882a593Smuzhiyun #include <linux/mutex.h>
44*4882a593Smuzhiyun #include <linux/sched.h>
45*4882a593Smuzhiyun #include <linux/sched/signal.h>
46*4882a593Smuzhiyun #include <linux/kthread.h>
47*4882a593Smuzhiyun #include <linux/freezer.h>
48*4882a593Smuzhiyun #include <linux/delay.h>
49*4882a593Smuzhiyun #include <linux/slab.h>
50*4882a593Smuzhiyun #include <linux/nvram.h>
51*4882a593Smuzhiyun #include <linux/proc_fs.h>
52*4882a593Smuzhiyun #include <linux/seq_file.h>
53*4882a593Smuzhiyun #include <linux/sysfs.h>
54*4882a593Smuzhiyun #include <linux/backlight.h>
55*4882a593Smuzhiyun #include <linux/bitops.h>
56*4882a593Smuzhiyun #include <linux/fb.h>
57*4882a593Smuzhiyun #include <linux/platform_device.h>
58*4882a593Smuzhiyun #include <linux/hwmon.h>
59*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
60*4882a593Smuzhiyun #include <linux/input.h>
61*4882a593Smuzhiyun #include <linux/leds.h>
62*4882a593Smuzhiyun #include <linux/rfkill.h>
63*4882a593Smuzhiyun #include <linux/dmi.h>
64*4882a593Smuzhiyun #include <linux/jiffies.h>
65*4882a593Smuzhiyun #include <linux/workqueue.h>
66*4882a593Smuzhiyun #include <linux/acpi.h>
67*4882a593Smuzhiyun #include <linux/pci.h>
68*4882a593Smuzhiyun #include <linux/power_supply.h>
69*4882a593Smuzhiyun #include <sound/core.h>
70*4882a593Smuzhiyun #include <sound/control.h>
71*4882a593Smuzhiyun #include <sound/initval.h>
72*4882a593Smuzhiyun #include <linux/uaccess.h>
73*4882a593Smuzhiyun #include <acpi/battery.h>
74*4882a593Smuzhiyun #include <acpi/video.h>
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /* ThinkPad CMOS commands */
77*4882a593Smuzhiyun #define TP_CMOS_VOLUME_DOWN	0
78*4882a593Smuzhiyun #define TP_CMOS_VOLUME_UP	1
79*4882a593Smuzhiyun #define TP_CMOS_VOLUME_MUTE	2
80*4882a593Smuzhiyun #define TP_CMOS_BRIGHTNESS_UP	4
81*4882a593Smuzhiyun #define TP_CMOS_BRIGHTNESS_DOWN	5
82*4882a593Smuzhiyun #define TP_CMOS_THINKLIGHT_ON	12
83*4882a593Smuzhiyun #define TP_CMOS_THINKLIGHT_OFF	13
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* NVRAM Addresses */
86*4882a593Smuzhiyun enum tp_nvram_addr {
87*4882a593Smuzhiyun 	TP_NVRAM_ADDR_HK2		= 0x57,
88*4882a593Smuzhiyun 	TP_NVRAM_ADDR_THINKLIGHT	= 0x58,
89*4882a593Smuzhiyun 	TP_NVRAM_ADDR_VIDEO		= 0x59,
90*4882a593Smuzhiyun 	TP_NVRAM_ADDR_BRIGHTNESS	= 0x5e,
91*4882a593Smuzhiyun 	TP_NVRAM_ADDR_MIXER		= 0x60,
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* NVRAM bit masks */
95*4882a593Smuzhiyun enum {
96*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_THINKPAD	= 0x08,
97*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_ZOOM		= 0x20,
98*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_DISPLAY	= 0x40,
99*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_HIBERNATE	= 0x80,
100*4882a593Smuzhiyun 	TP_NVRAM_MASK_THINKLIGHT	= 0x10,
101*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_DISPEXPND	= 0x30,
102*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_BRIGHTNESS	= 0x20,
103*4882a593Smuzhiyun 	TP_NVRAM_MASK_LEVEL_BRIGHTNESS	= 0x0f,
104*4882a593Smuzhiyun 	TP_NVRAM_POS_LEVEL_BRIGHTNESS	= 0,
105*4882a593Smuzhiyun 	TP_NVRAM_MASK_MUTE		= 0x40,
106*4882a593Smuzhiyun 	TP_NVRAM_MASK_HKT_VOLUME	= 0x80,
107*4882a593Smuzhiyun 	TP_NVRAM_MASK_LEVEL_VOLUME	= 0x0f,
108*4882a593Smuzhiyun 	TP_NVRAM_POS_LEVEL_VOLUME	= 0,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* Misc NVRAM-related */
112*4882a593Smuzhiyun enum {
113*4882a593Smuzhiyun 	TP_NVRAM_LEVEL_VOLUME_MAX = 14,
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /* ACPI HIDs */
117*4882a593Smuzhiyun #define TPACPI_ACPI_IBM_HKEY_HID	"IBM0068"
118*4882a593Smuzhiyun #define TPACPI_ACPI_LENOVO_HKEY_HID	"LEN0068"
119*4882a593Smuzhiyun #define TPACPI_ACPI_LENOVO_HKEY_V2_HID	"LEN0268"
120*4882a593Smuzhiyun #define TPACPI_ACPI_EC_HID		"PNP0C09"
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /* Input IDs */
123*4882a593Smuzhiyun #define TPACPI_HKEY_INPUT_PRODUCT	0x5054 /* "TP" */
124*4882a593Smuzhiyun #define TPACPI_HKEY_INPUT_VERSION	0x4101
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /* ACPI \WGSV commands */
127*4882a593Smuzhiyun enum {
128*4882a593Smuzhiyun 	TP_ACPI_WGSV_GET_STATE		= 0x01, /* Get state information */
129*4882a593Smuzhiyun 	TP_ACPI_WGSV_PWR_ON_ON_RESUME	= 0x02, /* Resume WWAN powered on */
130*4882a593Smuzhiyun 	TP_ACPI_WGSV_PWR_OFF_ON_RESUME	= 0x03,	/* Resume WWAN powered off */
131*4882a593Smuzhiyun 	TP_ACPI_WGSV_SAVE_STATE		= 0x04, /* Save state for S4/S5 */
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /* TP_ACPI_WGSV_GET_STATE bits */
135*4882a593Smuzhiyun enum {
136*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_WWANEXIST	= 0x0001, /* WWAN hw available */
137*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_WWANPWR	= 0x0002, /* WWAN radio enabled */
138*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_WWANPWRRES	= 0x0004, /* WWAN state at resume */
139*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_WWANBIOSOFF	= 0x0008, /* WWAN disabled in BIOS */
140*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_BLTHEXIST	= 0x0001, /* BLTH hw available */
141*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_BLTHPWR	= 0x0002, /* BLTH radio enabled */
142*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_BLTHPWRRES	= 0x0004, /* BLTH state at resume */
143*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_BLTHBIOSOFF	= 0x0008, /* BLTH disabled in BIOS */
144*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_UWBEXIST	= 0x0010, /* UWB hw available */
145*4882a593Smuzhiyun 	TP_ACPI_WGSV_STATE_UWBPWR	= 0x0020, /* UWB radio enabled */
146*4882a593Smuzhiyun };
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /* HKEY events */
149*4882a593Smuzhiyun enum tpacpi_hkey_event_t {
150*4882a593Smuzhiyun 	/* Hotkey-related */
151*4882a593Smuzhiyun 	TP_HKEY_EV_HOTKEY_BASE		= 0x1001, /* first hotkey (FN+F1) */
152*4882a593Smuzhiyun 	TP_HKEY_EV_BRGHT_UP		= 0x1010, /* Brightness up */
153*4882a593Smuzhiyun 	TP_HKEY_EV_BRGHT_DOWN		= 0x1011, /* Brightness down */
154*4882a593Smuzhiyun 	TP_HKEY_EV_KBD_LIGHT		= 0x1012, /* Thinklight/kbd backlight */
155*4882a593Smuzhiyun 	TP_HKEY_EV_VOL_UP		= 0x1015, /* Volume up or unmute */
156*4882a593Smuzhiyun 	TP_HKEY_EV_VOL_DOWN		= 0x1016, /* Volume down or unmute */
157*4882a593Smuzhiyun 	TP_HKEY_EV_VOL_MUTE		= 0x1017, /* Mixer output mute */
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Reasons for waking up from S3/S4 */
160*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S3_UNDOCK	= 0x2304, /* undock requested, S3 */
161*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S4_UNDOCK	= 0x2404, /* undock requested, S4 */
162*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S3_BAYEJ	= 0x2305, /* bay ejection req, S3 */
163*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S4_BAYEJ	= 0x2405, /* bay ejection req, S4 */
164*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S3_BATLOW	= 0x2313, /* battery empty, S3 */
165*4882a593Smuzhiyun 	TP_HKEY_EV_WKUP_S4_BATLOW	= 0x2413, /* battery empty, S4 */
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* Auto-sleep after eject request */
168*4882a593Smuzhiyun 	TP_HKEY_EV_BAYEJ_ACK		= 0x3003, /* bay ejection complete */
169*4882a593Smuzhiyun 	TP_HKEY_EV_UNDOCK_ACK		= 0x4003, /* undock complete */
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* Misc bay events */
172*4882a593Smuzhiyun 	TP_HKEY_EV_OPTDRV_EJ		= 0x3006, /* opt. drive tray ejected */
173*4882a593Smuzhiyun 	TP_HKEY_EV_HOTPLUG_DOCK		= 0x4010, /* docked into hotplug dock
174*4882a593Smuzhiyun 						     or port replicator */
175*4882a593Smuzhiyun 	TP_HKEY_EV_HOTPLUG_UNDOCK	= 0x4011, /* undocked from hotplug
176*4882a593Smuzhiyun 						     dock or port replicator */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* User-interface events */
179*4882a593Smuzhiyun 	TP_HKEY_EV_LID_CLOSE		= 0x5001, /* laptop lid closed */
180*4882a593Smuzhiyun 	TP_HKEY_EV_LID_OPEN		= 0x5002, /* laptop lid opened */
181*4882a593Smuzhiyun 	TP_HKEY_EV_TABLET_TABLET	= 0x5009, /* tablet swivel up */
182*4882a593Smuzhiyun 	TP_HKEY_EV_TABLET_NOTEBOOK	= 0x500a, /* tablet swivel down */
183*4882a593Smuzhiyun 	TP_HKEY_EV_TABLET_CHANGED	= 0x60c0, /* X1 Yoga (2016):
184*4882a593Smuzhiyun 						   * enter/leave tablet mode
185*4882a593Smuzhiyun 						   */
186*4882a593Smuzhiyun 	TP_HKEY_EV_PEN_INSERTED		= 0x500b, /* tablet pen inserted */
187*4882a593Smuzhiyun 	TP_HKEY_EV_PEN_REMOVED		= 0x500c, /* tablet pen removed */
188*4882a593Smuzhiyun 	TP_HKEY_EV_BRGHT_CHANGED	= 0x5010, /* backlight control event */
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	/* Key-related user-interface events */
191*4882a593Smuzhiyun 	TP_HKEY_EV_KEY_NUMLOCK		= 0x6000, /* NumLock key pressed */
192*4882a593Smuzhiyun 	TP_HKEY_EV_KEY_FN		= 0x6005, /* Fn key pressed? E420 */
193*4882a593Smuzhiyun 	TP_HKEY_EV_KEY_FN_ESC           = 0x6060, /* Fn+Esc key pressed X240 */
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	/* Thermal events */
196*4882a593Smuzhiyun 	TP_HKEY_EV_ALARM_BAT_HOT	= 0x6011, /* battery too hot */
197*4882a593Smuzhiyun 	TP_HKEY_EV_ALARM_BAT_XHOT	= 0x6012, /* battery critically hot */
198*4882a593Smuzhiyun 	TP_HKEY_EV_ALARM_SENSOR_HOT	= 0x6021, /* sensor too hot */
199*4882a593Smuzhiyun 	TP_HKEY_EV_ALARM_SENSOR_XHOT	= 0x6022, /* sensor critically hot */
200*4882a593Smuzhiyun 	TP_HKEY_EV_THM_TABLE_CHANGED	= 0x6030, /* windows; thermal table changed */
201*4882a593Smuzhiyun 	TP_HKEY_EV_THM_CSM_COMPLETED    = 0x6032, /* windows; thermal control set
202*4882a593Smuzhiyun 						   * command completed. Related to
203*4882a593Smuzhiyun 						   * AML DYTC */
204*4882a593Smuzhiyun 	TP_HKEY_EV_THM_TRANSFM_CHANGED  = 0x60F0, /* windows; thermal transformation
205*4882a593Smuzhiyun 						   * changed. Related to AML GMTS */
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* AC-related events */
208*4882a593Smuzhiyun 	TP_HKEY_EV_AC_CHANGED		= 0x6040, /* AC status changed */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* Further user-interface events */
211*4882a593Smuzhiyun 	TP_HKEY_EV_PALM_DETECTED	= 0x60b0, /* palm hoveres keyboard */
212*4882a593Smuzhiyun 	TP_HKEY_EV_PALM_UNDETECTED	= 0x60b1, /* palm removed */
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* Misc */
215*4882a593Smuzhiyun 	TP_HKEY_EV_RFKILL_CHANGED	= 0x7000, /* rfkill switch changed */
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /****************************************************************************
219*4882a593Smuzhiyun  * Main driver
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun #define TPACPI_NAME "thinkpad"
223*4882a593Smuzhiyun #define TPACPI_DESC "ThinkPad ACPI Extras"
224*4882a593Smuzhiyun #define TPACPI_FILE TPACPI_NAME "_acpi"
225*4882a593Smuzhiyun #define TPACPI_URL "http://ibm-acpi.sf.net/"
226*4882a593Smuzhiyun #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun #define TPACPI_PROC_DIR "ibm"
229*4882a593Smuzhiyun #define TPACPI_ACPI_EVENT_PREFIX "ibm"
230*4882a593Smuzhiyun #define TPACPI_DRVR_NAME TPACPI_FILE
231*4882a593Smuzhiyun #define TPACPI_DRVR_SHORTNAME "tpacpi"
232*4882a593Smuzhiyun #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
235*4882a593Smuzhiyun #define TPACPI_WORKQUEUE_NAME "ktpacpid"
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun #define TPACPI_MAX_ACPI_ARGS 3
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /* Debugging printk groups */
240*4882a593Smuzhiyun #define TPACPI_DBG_ALL		0xffff
241*4882a593Smuzhiyun #define TPACPI_DBG_DISCLOSETASK	0x8000
242*4882a593Smuzhiyun #define TPACPI_DBG_INIT		0x0001
243*4882a593Smuzhiyun #define TPACPI_DBG_EXIT		0x0002
244*4882a593Smuzhiyun #define TPACPI_DBG_RFKILL	0x0004
245*4882a593Smuzhiyun #define TPACPI_DBG_HKEY		0x0008
246*4882a593Smuzhiyun #define TPACPI_DBG_FAN		0x0010
247*4882a593Smuzhiyun #define TPACPI_DBG_BRGHT	0x0020
248*4882a593Smuzhiyun #define TPACPI_DBG_MIXER	0x0040
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
251*4882a593Smuzhiyun #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
252*4882a593Smuzhiyun #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /****************************************************************************
256*4882a593Smuzhiyun  * Driver-wide structs and misc. variables
257*4882a593Smuzhiyun  */
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun struct ibm_struct;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun struct tp_acpi_drv_struct {
262*4882a593Smuzhiyun 	const struct acpi_device_id *hid;
263*4882a593Smuzhiyun 	struct acpi_driver *driver;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	void (*notify) (struct ibm_struct *, u32);
266*4882a593Smuzhiyun 	acpi_handle *handle;
267*4882a593Smuzhiyun 	u32 type;
268*4882a593Smuzhiyun 	struct acpi_device *device;
269*4882a593Smuzhiyun };
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun struct ibm_struct {
272*4882a593Smuzhiyun 	char *name;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	int (*read) (struct seq_file *);
275*4882a593Smuzhiyun 	int (*write) (char *);
276*4882a593Smuzhiyun 	void (*exit) (void);
277*4882a593Smuzhiyun 	void (*resume) (void);
278*4882a593Smuzhiyun 	void (*suspend) (void);
279*4882a593Smuzhiyun 	void (*shutdown) (void);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	struct list_head all_drivers;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	struct tp_acpi_drv_struct *acpi;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	struct {
286*4882a593Smuzhiyun 		u8 acpi_driver_registered:1;
287*4882a593Smuzhiyun 		u8 acpi_notify_installed:1;
288*4882a593Smuzhiyun 		u8 proc_created:1;
289*4882a593Smuzhiyun 		u8 init_called:1;
290*4882a593Smuzhiyun 		u8 experimental:1;
291*4882a593Smuzhiyun 	} flags;
292*4882a593Smuzhiyun };
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun struct ibm_init_struct {
295*4882a593Smuzhiyun 	char param[32];
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	int (*init) (struct ibm_init_struct *);
298*4882a593Smuzhiyun 	umode_t base_procfs_mode;
299*4882a593Smuzhiyun 	struct ibm_struct *data;
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun static struct {
303*4882a593Smuzhiyun 	u32 bluetooth:1;
304*4882a593Smuzhiyun 	u32 hotkey:1;
305*4882a593Smuzhiyun 	u32 hotkey_mask:1;
306*4882a593Smuzhiyun 	u32 hotkey_wlsw:1;
307*4882a593Smuzhiyun 	enum {
308*4882a593Smuzhiyun 		TP_HOTKEY_TABLET_NONE = 0,
309*4882a593Smuzhiyun 		TP_HOTKEY_TABLET_USES_MHKG,
310*4882a593Smuzhiyun 		TP_HOTKEY_TABLET_USES_GMMS,
311*4882a593Smuzhiyun 	} hotkey_tablet;
312*4882a593Smuzhiyun 	u32 kbdlight:1;
313*4882a593Smuzhiyun 	u32 light:1;
314*4882a593Smuzhiyun 	u32 light_status:1;
315*4882a593Smuzhiyun 	u32 bright_acpimode:1;
316*4882a593Smuzhiyun 	u32 bright_unkfw:1;
317*4882a593Smuzhiyun 	u32 wan:1;
318*4882a593Smuzhiyun 	u32 uwb:1;
319*4882a593Smuzhiyun 	u32 fan_ctrl_status_undef:1;
320*4882a593Smuzhiyun 	u32 second_fan:1;
321*4882a593Smuzhiyun 	u32 second_fan_ctl:1;
322*4882a593Smuzhiyun 	u32 beep_needs_two_args:1;
323*4882a593Smuzhiyun 	u32 mixer_no_level_control:1;
324*4882a593Smuzhiyun 	u32 battery_force_primary:1;
325*4882a593Smuzhiyun 	u32 input_device_registered:1;
326*4882a593Smuzhiyun 	u32 platform_drv_registered:1;
327*4882a593Smuzhiyun 	u32 platform_drv_attrs_registered:1;
328*4882a593Smuzhiyun 	u32 sensors_pdrv_registered:1;
329*4882a593Smuzhiyun 	u32 sensors_pdrv_attrs_registered:1;
330*4882a593Smuzhiyun 	u32 sensors_pdev_attrs_registered:1;
331*4882a593Smuzhiyun 	u32 hotkey_poll_active:1;
332*4882a593Smuzhiyun 	u32 has_adaptive_kbd:1;
333*4882a593Smuzhiyun } tp_features;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun static struct {
336*4882a593Smuzhiyun 	u16 hotkey_mask_ff:1;
337*4882a593Smuzhiyun 	u16 volume_ctrl_forbidden:1;
338*4882a593Smuzhiyun } tp_warned;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun struct thinkpad_id_data {
341*4882a593Smuzhiyun 	unsigned int vendor;	/* ThinkPad vendor:
342*4882a593Smuzhiyun 				 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	char *bios_version_str;	/* Something like 1ZET51WW (1.03z) */
345*4882a593Smuzhiyun 	char *ec_version_str;	/* Something like 1ZHT51WW-1.04a */
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	u32 bios_model;		/* 1Y = 0x3159, 0 = unknown */
348*4882a593Smuzhiyun 	u32 ec_model;
349*4882a593Smuzhiyun 	u16 bios_release;	/* 1ZETK1WW = 0x4b31, 0 = unknown */
350*4882a593Smuzhiyun 	u16 ec_release;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	char *model_str;	/* ThinkPad T43 */
353*4882a593Smuzhiyun 	char *nummodel_str;	/* 9384A9C for a 9384-A9C model */
354*4882a593Smuzhiyun };
355*4882a593Smuzhiyun static struct thinkpad_id_data thinkpad_id;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun static enum {
358*4882a593Smuzhiyun 	TPACPI_LIFE_INIT = 0,
359*4882a593Smuzhiyun 	TPACPI_LIFE_RUNNING,
360*4882a593Smuzhiyun 	TPACPI_LIFE_EXITING,
361*4882a593Smuzhiyun } tpacpi_lifecycle;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun static int experimental;
364*4882a593Smuzhiyun static u32 dbg_level;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun static struct workqueue_struct *tpacpi_wq;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun enum led_status_t {
369*4882a593Smuzhiyun 	TPACPI_LED_OFF = 0,
370*4882a593Smuzhiyun 	TPACPI_LED_ON,
371*4882a593Smuzhiyun 	TPACPI_LED_BLINK,
372*4882a593Smuzhiyun };
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun /* tpacpi LED class */
375*4882a593Smuzhiyun struct tpacpi_led_classdev {
376*4882a593Smuzhiyun 	struct led_classdev led_classdev;
377*4882a593Smuzhiyun 	int led;
378*4882a593Smuzhiyun };
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun /* brightness level capabilities */
381*4882a593Smuzhiyun static unsigned int bright_maxlvl;	/* 0 = unknown */
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
384*4882a593Smuzhiyun static int dbg_wlswemul;
385*4882a593Smuzhiyun static bool tpacpi_wlsw_emulstate;
386*4882a593Smuzhiyun static int dbg_bluetoothemul;
387*4882a593Smuzhiyun static bool tpacpi_bluetooth_emulstate;
388*4882a593Smuzhiyun static int dbg_wwanemul;
389*4882a593Smuzhiyun static bool tpacpi_wwan_emulstate;
390*4882a593Smuzhiyun static int dbg_uwbemul;
391*4882a593Smuzhiyun static bool tpacpi_uwb_emulstate;
392*4882a593Smuzhiyun #endif
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /*************************************************************************
396*4882a593Smuzhiyun  *  Debugging helpers
397*4882a593Smuzhiyun  */
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun #define dbg_printk(a_dbg_level, format, arg...)				\
400*4882a593Smuzhiyun do {									\
401*4882a593Smuzhiyun 	if (dbg_level & (a_dbg_level))					\
402*4882a593Smuzhiyun 		printk(KERN_DEBUG pr_fmt("%s: " format),		\
403*4882a593Smuzhiyun 		       __func__, ##arg);				\
404*4882a593Smuzhiyun } while (0)
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUG
407*4882a593Smuzhiyun #define vdbg_printk dbg_printk
408*4882a593Smuzhiyun static const char *str_supported(int is_supported);
409*4882a593Smuzhiyun #else
str_supported(int is_supported)410*4882a593Smuzhiyun static inline const char *str_supported(int is_supported) { return ""; }
411*4882a593Smuzhiyun #define vdbg_printk(a_dbg_level, format, arg...)	\
412*4882a593Smuzhiyun 	do { if (0) no_printk(format, ##arg); } while (0)
413*4882a593Smuzhiyun #endif
414*4882a593Smuzhiyun 
tpacpi_log_usertask(const char * const what)415*4882a593Smuzhiyun static void tpacpi_log_usertask(const char * const what)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
418*4882a593Smuzhiyun 	       what, task_tgid_vnr(current));
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun #define tpacpi_disclose_usertask(what, format, arg...)			\
422*4882a593Smuzhiyun do {									\
423*4882a593Smuzhiyun 	if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) &&		\
424*4882a593Smuzhiyun 		     (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) {	\
425*4882a593Smuzhiyun 		printk(KERN_DEBUG pr_fmt("%s: PID %d: " format),	\
426*4882a593Smuzhiyun 		       what, task_tgid_vnr(current), ## arg);		\
427*4882a593Smuzhiyun 	}								\
428*4882a593Smuzhiyun } while (0)
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /*
431*4882a593Smuzhiyun  * Quirk handling helpers
432*4882a593Smuzhiyun  *
433*4882a593Smuzhiyun  * ThinkPad IDs and versions seen in the field so far are
434*4882a593Smuzhiyun  * two or three characters from the set [0-9A-Z], i.e. base 36.
435*4882a593Smuzhiyun  *
436*4882a593Smuzhiyun  * We use values well outside that range as specials.
437*4882a593Smuzhiyun  */
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun #define TPACPI_MATCH_ANY		0xffffffffU
440*4882a593Smuzhiyun #define TPACPI_MATCH_ANY_VERSION	0xffffU
441*4882a593Smuzhiyun #define TPACPI_MATCH_UNKNOWN		0U
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun /* TPID('1', 'Y') == 0x3159 */
444*4882a593Smuzhiyun #define TPID(__c1, __c2)	(((__c1) << 8) | (__c2))
445*4882a593Smuzhiyun #define TPID3(__c1, __c2, __c3)	(((__c1) << 16) | ((__c2) << 8) | (__c3))
446*4882a593Smuzhiyun #define TPVER TPID
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun #define TPACPI_Q_IBM(__id1, __id2, __quirk)	\
449*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_IBM,		\
450*4882a593Smuzhiyun 	  .bios = TPID(__id1, __id2),		\
451*4882a593Smuzhiyun 	  .ec = TPACPI_MATCH_ANY,		\
452*4882a593Smuzhiyun 	  .quirks = (__quirk) }
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun #define TPACPI_Q_LNV(__id1, __id2, __quirk)	\
455*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_LENOVO,	\
456*4882a593Smuzhiyun 	  .bios = TPID(__id1, __id2),		\
457*4882a593Smuzhiyun 	  .ec = TPACPI_MATCH_ANY,		\
458*4882a593Smuzhiyun 	  .quirks = (__quirk) }
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
461*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_LENOVO,	\
462*4882a593Smuzhiyun 	  .bios = TPID3(__id1, __id2, __id3),	\
463*4882a593Smuzhiyun 	  .ec = TPACPI_MATCH_ANY,		\
464*4882a593Smuzhiyun 	  .quirks = (__quirk) }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun #define TPACPI_QEC_IBM(__id1, __id2, __quirk)	\
467*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_IBM,		\
468*4882a593Smuzhiyun 	  .bios = TPACPI_MATCH_ANY,		\
469*4882a593Smuzhiyun 	  .ec = TPID(__id1, __id2),		\
470*4882a593Smuzhiyun 	  .quirks = (__quirk) }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun #define TPACPI_QEC_LNV(__id1, __id2, __quirk)	\
473*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_LENOVO,	\
474*4882a593Smuzhiyun 	  .bios = TPACPI_MATCH_ANY,		\
475*4882a593Smuzhiyun 	  .ec = TPID(__id1, __id2),		\
476*4882a593Smuzhiyun 	  .quirks = (__quirk) }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun struct tpacpi_quirk {
479*4882a593Smuzhiyun 	unsigned int vendor;
480*4882a593Smuzhiyun 	u32 bios;
481*4882a593Smuzhiyun 	u32 ec;
482*4882a593Smuzhiyun 	unsigned long quirks;
483*4882a593Smuzhiyun };
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun  * tpacpi_check_quirks() - search BIOS/EC version on a list
487*4882a593Smuzhiyun  * @qlist:		array of &struct tpacpi_quirk
488*4882a593Smuzhiyun  * @qlist_size:		number of elements in @qlist
489*4882a593Smuzhiyun  *
490*4882a593Smuzhiyun  * Iterates over a quirks list until one is found that matches the
491*4882a593Smuzhiyun  * ThinkPad's vendor, BIOS and EC model.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * Returns 0 if nothing matches, otherwise returns the quirks field of
494*4882a593Smuzhiyun  * the matching &struct tpacpi_quirk entry.
495*4882a593Smuzhiyun  *
496*4882a593Smuzhiyun  * The match criteria is: vendor, ec and bios much match.
497*4882a593Smuzhiyun  */
tpacpi_check_quirks(const struct tpacpi_quirk * qlist,unsigned int qlist_size)498*4882a593Smuzhiyun static unsigned long __init tpacpi_check_quirks(
499*4882a593Smuzhiyun 			const struct tpacpi_quirk *qlist,
500*4882a593Smuzhiyun 			unsigned int qlist_size)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun 	while (qlist_size) {
503*4882a593Smuzhiyun 		if ((qlist->vendor == thinkpad_id.vendor ||
504*4882a593Smuzhiyun 				qlist->vendor == TPACPI_MATCH_ANY) &&
505*4882a593Smuzhiyun 		    (qlist->bios == thinkpad_id.bios_model ||
506*4882a593Smuzhiyun 				qlist->bios == TPACPI_MATCH_ANY) &&
507*4882a593Smuzhiyun 		    (qlist->ec == thinkpad_id.ec_model ||
508*4882a593Smuzhiyun 				qlist->ec == TPACPI_MATCH_ANY))
509*4882a593Smuzhiyun 			return qlist->quirks;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		qlist_size--;
512*4882a593Smuzhiyun 		qlist++;
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 	return 0;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
tpacpi_is_lenovo(void)517*4882a593Smuzhiyun static inline bool __pure __init tpacpi_is_lenovo(void)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
tpacpi_is_ibm(void)522*4882a593Smuzhiyun static inline bool __pure __init tpacpi_is_ibm(void)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun 	return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun /****************************************************************************
528*4882a593Smuzhiyun  ****************************************************************************
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * ACPI Helpers and device model
531*4882a593Smuzhiyun  *
532*4882a593Smuzhiyun  ****************************************************************************
533*4882a593Smuzhiyun  ****************************************************************************/
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun /*************************************************************************
536*4882a593Smuzhiyun  * ACPI basic handles
537*4882a593Smuzhiyun  */
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun static acpi_handle root_handle;
540*4882a593Smuzhiyun static acpi_handle ec_handle;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun #define TPACPI_HANDLE(object, parent, paths...)			\
543*4882a593Smuzhiyun 	static acpi_handle  object##_handle;			\
544*4882a593Smuzhiyun 	static const acpi_handle * const object##_parent __initconst =	\
545*4882a593Smuzhiyun 						&parent##_handle; \
546*4882a593Smuzhiyun 	static char *object##_paths[] __initdata = { paths }
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun TPACPI_HANDLE(ecrd, ec, "ECRD");	/* 570 */
549*4882a593Smuzhiyun TPACPI_HANDLE(ecwr, ec, "ECWR");	/* 570 */
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun TPACPI_HANDLE(cmos, root, "\\UCMS",	/* R50, R50e, R50p, R51, */
552*4882a593Smuzhiyun 					/* T4x, X31, X40 */
553*4882a593Smuzhiyun 	   "\\CMOS",		/* A3x, G4x, R32, T23, T30, X22-24, X30 */
554*4882a593Smuzhiyun 	   "\\CMS",		/* R40, R40e */
555*4882a593Smuzhiyun 	   );			/* all others */
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY",	/* 600e/x, 770e, 770x */
558*4882a593Smuzhiyun 	   "^HKEY",		/* R30, R31 */
559*4882a593Smuzhiyun 	   "HKEY",		/* all others */
560*4882a593Smuzhiyun 	   );			/* 570 */
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun /*************************************************************************
563*4882a593Smuzhiyun  * ACPI helpers
564*4882a593Smuzhiyun  */
565*4882a593Smuzhiyun 
acpi_evalf(acpi_handle handle,int * res,char * method,char * fmt,...)566*4882a593Smuzhiyun static int acpi_evalf(acpi_handle handle,
567*4882a593Smuzhiyun 		      int *res, char *method, char *fmt, ...)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun 	char *fmt0 = fmt;
570*4882a593Smuzhiyun 	struct acpi_object_list params;
571*4882a593Smuzhiyun 	union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
572*4882a593Smuzhiyun 	struct acpi_buffer result, *resultp;
573*4882a593Smuzhiyun 	union acpi_object out_obj;
574*4882a593Smuzhiyun 	acpi_status status;
575*4882a593Smuzhiyun 	va_list ap;
576*4882a593Smuzhiyun 	char res_type;
577*4882a593Smuzhiyun 	int success;
578*4882a593Smuzhiyun 	int quiet;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	if (!*fmt) {
581*4882a593Smuzhiyun 		pr_err("acpi_evalf() called with empty format\n");
582*4882a593Smuzhiyun 		return 0;
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	if (*fmt == 'q') {
586*4882a593Smuzhiyun 		quiet = 1;
587*4882a593Smuzhiyun 		fmt++;
588*4882a593Smuzhiyun 	} else
589*4882a593Smuzhiyun 		quiet = 0;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	res_type = *(fmt++);
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	params.count = 0;
594*4882a593Smuzhiyun 	params.pointer = &in_objs[0];
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	va_start(ap, fmt);
597*4882a593Smuzhiyun 	while (*fmt) {
598*4882a593Smuzhiyun 		char c = *(fmt++);
599*4882a593Smuzhiyun 		switch (c) {
600*4882a593Smuzhiyun 		case 'd':	/* int */
601*4882a593Smuzhiyun 			in_objs[params.count].integer.value = va_arg(ap, int);
602*4882a593Smuzhiyun 			in_objs[params.count++].type = ACPI_TYPE_INTEGER;
603*4882a593Smuzhiyun 			break;
604*4882a593Smuzhiyun 			/* add more types as needed */
605*4882a593Smuzhiyun 		default:
606*4882a593Smuzhiyun 			pr_err("acpi_evalf() called with invalid format character '%c'\n",
607*4882a593Smuzhiyun 			       c);
608*4882a593Smuzhiyun 			va_end(ap);
609*4882a593Smuzhiyun 			return 0;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 	}
612*4882a593Smuzhiyun 	va_end(ap);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if (res_type != 'v') {
615*4882a593Smuzhiyun 		result.length = sizeof(out_obj);
616*4882a593Smuzhiyun 		result.pointer = &out_obj;
617*4882a593Smuzhiyun 		resultp = &result;
618*4882a593Smuzhiyun 	} else
619*4882a593Smuzhiyun 		resultp = NULL;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	status = acpi_evaluate_object(handle, method, &params, resultp);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	switch (res_type) {
624*4882a593Smuzhiyun 	case 'd':		/* int */
625*4882a593Smuzhiyun 		success = (status == AE_OK &&
626*4882a593Smuzhiyun 			   out_obj.type == ACPI_TYPE_INTEGER);
627*4882a593Smuzhiyun 		if (success && res)
628*4882a593Smuzhiyun 			*res = out_obj.integer.value;
629*4882a593Smuzhiyun 		break;
630*4882a593Smuzhiyun 	case 'v':		/* void */
631*4882a593Smuzhiyun 		success = status == AE_OK;
632*4882a593Smuzhiyun 		break;
633*4882a593Smuzhiyun 		/* add more types as needed */
634*4882a593Smuzhiyun 	default:
635*4882a593Smuzhiyun 		pr_err("acpi_evalf() called with invalid format character '%c'\n",
636*4882a593Smuzhiyun 		       res_type);
637*4882a593Smuzhiyun 		return 0;
638*4882a593Smuzhiyun 	}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	if (!success && !quiet)
641*4882a593Smuzhiyun 		pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
642*4882a593Smuzhiyun 		       method, fmt0, acpi_format_exception(status));
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	return success;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun 
acpi_ec_read(int i,u8 * p)647*4882a593Smuzhiyun static int acpi_ec_read(int i, u8 *p)
648*4882a593Smuzhiyun {
649*4882a593Smuzhiyun 	int v;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	if (ecrd_handle) {
652*4882a593Smuzhiyun 		if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
653*4882a593Smuzhiyun 			return 0;
654*4882a593Smuzhiyun 		*p = v;
655*4882a593Smuzhiyun 	} else {
656*4882a593Smuzhiyun 		if (ec_read(i, p) < 0)
657*4882a593Smuzhiyun 			return 0;
658*4882a593Smuzhiyun 	}
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	return 1;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun 
acpi_ec_write(int i,u8 v)663*4882a593Smuzhiyun static int acpi_ec_write(int i, u8 v)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	if (ecwr_handle) {
666*4882a593Smuzhiyun 		if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
667*4882a593Smuzhiyun 			return 0;
668*4882a593Smuzhiyun 	} else {
669*4882a593Smuzhiyun 		if (ec_write(i, v) < 0)
670*4882a593Smuzhiyun 			return 0;
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	return 1;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun 
issue_thinkpad_cmos_command(int cmos_cmd)676*4882a593Smuzhiyun static int issue_thinkpad_cmos_command(int cmos_cmd)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	if (!cmos_handle)
679*4882a593Smuzhiyun 		return -ENXIO;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
682*4882a593Smuzhiyun 		return -EIO;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	return 0;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /*************************************************************************
688*4882a593Smuzhiyun  * ACPI device model
689*4882a593Smuzhiyun  */
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun #define TPACPI_ACPIHANDLE_INIT(object) \
692*4882a593Smuzhiyun 	drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
693*4882a593Smuzhiyun 		object##_paths, ARRAY_SIZE(object##_paths))
694*4882a593Smuzhiyun 
drv_acpi_handle_init(const char * name,acpi_handle * handle,const acpi_handle parent,char ** paths,const int num_paths)695*4882a593Smuzhiyun static void __init drv_acpi_handle_init(const char *name,
696*4882a593Smuzhiyun 			   acpi_handle *handle, const acpi_handle parent,
697*4882a593Smuzhiyun 			   char **paths, const int num_paths)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun 	int i;
700*4882a593Smuzhiyun 	acpi_status status;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
703*4882a593Smuzhiyun 		name);
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	for (i = 0; i < num_paths; i++) {
706*4882a593Smuzhiyun 		status = acpi_get_handle(parent, paths[i], handle);
707*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status)) {
708*4882a593Smuzhiyun 			dbg_printk(TPACPI_DBG_INIT,
709*4882a593Smuzhiyun 				   "Found ACPI handle %s for %s\n",
710*4882a593Smuzhiyun 				   paths[i], name);
711*4882a593Smuzhiyun 			return;
712*4882a593Smuzhiyun 		}
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
716*4882a593Smuzhiyun 		    name);
717*4882a593Smuzhiyun 	*handle = NULL;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
tpacpi_acpi_handle_locate_callback(acpi_handle handle,u32 level,void * context,void ** return_value)720*4882a593Smuzhiyun static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
721*4882a593Smuzhiyun 			u32 level, void *context, void **return_value)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun 	struct acpi_device *dev;
724*4882a593Smuzhiyun 	if (!strcmp(context, "video")) {
725*4882a593Smuzhiyun 		if (acpi_bus_get_device(handle, &dev))
726*4882a593Smuzhiyun 			return AE_OK;
727*4882a593Smuzhiyun 		if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
728*4882a593Smuzhiyun 			return AE_OK;
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	*(acpi_handle *)return_value = handle;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	return AE_CTRL_TERMINATE;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
tpacpi_acpi_handle_locate(const char * name,const char * hid,acpi_handle * handle)736*4882a593Smuzhiyun static void __init tpacpi_acpi_handle_locate(const char *name,
737*4882a593Smuzhiyun 		const char *hid,
738*4882a593Smuzhiyun 		acpi_handle *handle)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	acpi_status status;
741*4882a593Smuzhiyun 	acpi_handle device_found;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	BUG_ON(!name || !handle);
744*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT,
745*4882a593Smuzhiyun 			"trying to locate ACPI handle for %s, using HID %s\n",
746*4882a593Smuzhiyun 			name, hid ? hid : "NULL");
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	memset(&device_found, 0, sizeof(device_found));
749*4882a593Smuzhiyun 	status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
750*4882a593Smuzhiyun 				  (void *)name, &device_found);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	*handle = NULL;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status)) {
755*4882a593Smuzhiyun 		*handle = device_found;
756*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT,
757*4882a593Smuzhiyun 			   "Found ACPI handle for %s\n", name);
758*4882a593Smuzhiyun 	} else {
759*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT,
760*4882a593Smuzhiyun 			    "Could not locate an ACPI handle for %s: %s\n",
761*4882a593Smuzhiyun 			    name, acpi_format_exception(status));
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun 
dispatch_acpi_notify(acpi_handle handle,u32 event,void * data)765*4882a593Smuzhiyun static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	struct ibm_struct *ibm = data;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
770*4882a593Smuzhiyun 		return;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	if (!ibm || !ibm->acpi || !ibm->acpi->notify)
773*4882a593Smuzhiyun 		return;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	ibm->acpi->notify(ibm, event);
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun 
setup_acpi_notify(struct ibm_struct * ibm)778*4882a593Smuzhiyun static int __init setup_acpi_notify(struct ibm_struct *ibm)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun 	acpi_status status;
781*4882a593Smuzhiyun 	int rc;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	BUG_ON(!ibm->acpi);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	if (!*ibm->acpi->handle)
786*4882a593Smuzhiyun 		return 0;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT,
789*4882a593Smuzhiyun 		"setting up ACPI notify for %s\n", ibm->name);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
792*4882a593Smuzhiyun 	if (rc < 0) {
793*4882a593Smuzhiyun 		pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
794*4882a593Smuzhiyun 		return -ENODEV;
795*4882a593Smuzhiyun 	}
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	ibm->acpi->device->driver_data = ibm;
798*4882a593Smuzhiyun 	sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
799*4882a593Smuzhiyun 		TPACPI_ACPI_EVENT_PREFIX,
800*4882a593Smuzhiyun 		ibm->name);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	status = acpi_install_notify_handler(*ibm->acpi->handle,
803*4882a593Smuzhiyun 			ibm->acpi->type, dispatch_acpi_notify, ibm);
804*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
805*4882a593Smuzhiyun 		if (status == AE_ALREADY_EXISTS) {
806*4882a593Smuzhiyun 			pr_notice("another device driver is already handling %s events\n",
807*4882a593Smuzhiyun 				  ibm->name);
808*4882a593Smuzhiyun 		} else {
809*4882a593Smuzhiyun 			pr_err("acpi_install_notify_handler(%s) failed: %s\n",
810*4882a593Smuzhiyun 			       ibm->name, acpi_format_exception(status));
811*4882a593Smuzhiyun 		}
812*4882a593Smuzhiyun 		return -ENODEV;
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 	ibm->flags.acpi_notify_installed = 1;
815*4882a593Smuzhiyun 	return 0;
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun 
tpacpi_device_add(struct acpi_device * device)818*4882a593Smuzhiyun static int __init tpacpi_device_add(struct acpi_device *device)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	return 0;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
register_tpacpi_subdriver(struct ibm_struct * ibm)823*4882a593Smuzhiyun static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	int rc;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT,
828*4882a593Smuzhiyun 		"registering %s as an ACPI driver\n", ibm->name);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	BUG_ON(!ibm->acpi);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
833*4882a593Smuzhiyun 	if (!ibm->acpi->driver) {
834*4882a593Smuzhiyun 		pr_err("failed to allocate memory for ibm->acpi->driver\n");
835*4882a593Smuzhiyun 		return -ENOMEM;
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
839*4882a593Smuzhiyun 	ibm->acpi->driver->ids = ibm->acpi->hid;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	ibm->acpi->driver->ops.add = &tpacpi_device_add;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	rc = acpi_bus_register_driver(ibm->acpi->driver);
844*4882a593Smuzhiyun 	if (rc < 0) {
845*4882a593Smuzhiyun 		pr_err("acpi_bus_register_driver(%s) failed: %d\n",
846*4882a593Smuzhiyun 		       ibm->name, rc);
847*4882a593Smuzhiyun 		kfree(ibm->acpi->driver);
848*4882a593Smuzhiyun 		ibm->acpi->driver = NULL;
849*4882a593Smuzhiyun 	} else if (!rc)
850*4882a593Smuzhiyun 		ibm->flags.acpi_driver_registered = 1;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	return rc;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun /****************************************************************************
857*4882a593Smuzhiyun  ****************************************************************************
858*4882a593Smuzhiyun  *
859*4882a593Smuzhiyun  * Procfs Helpers
860*4882a593Smuzhiyun  *
861*4882a593Smuzhiyun  ****************************************************************************
862*4882a593Smuzhiyun  ****************************************************************************/
863*4882a593Smuzhiyun 
dispatch_proc_show(struct seq_file * m,void * v)864*4882a593Smuzhiyun static int dispatch_proc_show(struct seq_file *m, void *v)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	struct ibm_struct *ibm = m->private;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (!ibm || !ibm->read)
869*4882a593Smuzhiyun 		return -EINVAL;
870*4882a593Smuzhiyun 	return ibm->read(m);
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
dispatch_proc_open(struct inode * inode,struct file * file)873*4882a593Smuzhiyun static int dispatch_proc_open(struct inode *inode, struct file *file)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun 	return single_open(file, dispatch_proc_show, PDE_DATA(inode));
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun 
dispatch_proc_write(struct file * file,const char __user * userbuf,size_t count,loff_t * pos)878*4882a593Smuzhiyun static ssize_t dispatch_proc_write(struct file *file,
879*4882a593Smuzhiyun 			const char __user *userbuf,
880*4882a593Smuzhiyun 			size_t count, loff_t *pos)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	struct ibm_struct *ibm = PDE_DATA(file_inode(file));
883*4882a593Smuzhiyun 	char *kernbuf;
884*4882a593Smuzhiyun 	int ret;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	if (!ibm || !ibm->write)
887*4882a593Smuzhiyun 		return -EINVAL;
888*4882a593Smuzhiyun 	if (count > PAGE_SIZE - 1)
889*4882a593Smuzhiyun 		return -EINVAL;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	kernbuf = kmalloc(count + 1, GFP_KERNEL);
892*4882a593Smuzhiyun 	if (!kernbuf)
893*4882a593Smuzhiyun 		return -ENOMEM;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	if (copy_from_user(kernbuf, userbuf, count)) {
896*4882a593Smuzhiyun 		kfree(kernbuf);
897*4882a593Smuzhiyun 		return -EFAULT;
898*4882a593Smuzhiyun 	}
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	kernbuf[count] = 0;
901*4882a593Smuzhiyun 	ret = ibm->write(kernbuf);
902*4882a593Smuzhiyun 	if (ret == 0)
903*4882a593Smuzhiyun 		ret = count;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	kfree(kernbuf);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	return ret;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun static const struct proc_ops dispatch_proc_ops = {
911*4882a593Smuzhiyun 	.proc_open	= dispatch_proc_open,
912*4882a593Smuzhiyun 	.proc_read	= seq_read,
913*4882a593Smuzhiyun 	.proc_lseek	= seq_lseek,
914*4882a593Smuzhiyun 	.proc_release	= single_release,
915*4882a593Smuzhiyun 	.proc_write	= dispatch_proc_write,
916*4882a593Smuzhiyun };
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun /****************************************************************************
919*4882a593Smuzhiyun  ****************************************************************************
920*4882a593Smuzhiyun  *
921*4882a593Smuzhiyun  * Device model: input, hwmon and platform
922*4882a593Smuzhiyun  *
923*4882a593Smuzhiyun  ****************************************************************************
924*4882a593Smuzhiyun  ****************************************************************************/
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun static struct platform_device *tpacpi_pdev;
927*4882a593Smuzhiyun static struct platform_device *tpacpi_sensors_pdev;
928*4882a593Smuzhiyun static struct device *tpacpi_hwmon;
929*4882a593Smuzhiyun static struct input_dev *tpacpi_inputdev;
930*4882a593Smuzhiyun static struct mutex tpacpi_inputdev_send_mutex;
931*4882a593Smuzhiyun static LIST_HEAD(tpacpi_all_drivers);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
tpacpi_suspend_handler(struct device * dev)934*4882a593Smuzhiyun static int tpacpi_suspend_handler(struct device *dev)
935*4882a593Smuzhiyun {
936*4882a593Smuzhiyun 	struct ibm_struct *ibm, *itmp;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	list_for_each_entry_safe(ibm, itmp,
939*4882a593Smuzhiyun 				 &tpacpi_all_drivers,
940*4882a593Smuzhiyun 				 all_drivers) {
941*4882a593Smuzhiyun 		if (ibm->suspend)
942*4882a593Smuzhiyun 			(ibm->suspend)();
943*4882a593Smuzhiyun 	}
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	return 0;
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun 
tpacpi_resume_handler(struct device * dev)948*4882a593Smuzhiyun static int tpacpi_resume_handler(struct device *dev)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun 	struct ibm_struct *ibm, *itmp;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	list_for_each_entry_safe(ibm, itmp,
953*4882a593Smuzhiyun 				 &tpacpi_all_drivers,
954*4882a593Smuzhiyun 				 all_drivers) {
955*4882a593Smuzhiyun 		if (ibm->resume)
956*4882a593Smuzhiyun 			(ibm->resume)();
957*4882a593Smuzhiyun 	}
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	return 0;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun #endif
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(tpacpi_pm,
964*4882a593Smuzhiyun 			 tpacpi_suspend_handler, tpacpi_resume_handler);
965*4882a593Smuzhiyun 
tpacpi_shutdown_handler(struct platform_device * pdev)966*4882a593Smuzhiyun static void tpacpi_shutdown_handler(struct platform_device *pdev)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	struct ibm_struct *ibm, *itmp;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	list_for_each_entry_safe(ibm, itmp,
971*4882a593Smuzhiyun 				 &tpacpi_all_drivers,
972*4882a593Smuzhiyun 				 all_drivers) {
973*4882a593Smuzhiyun 		if (ibm->shutdown)
974*4882a593Smuzhiyun 			(ibm->shutdown)();
975*4882a593Smuzhiyun 	}
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun static struct platform_driver tpacpi_pdriver = {
979*4882a593Smuzhiyun 	.driver = {
980*4882a593Smuzhiyun 		.name = TPACPI_DRVR_NAME,
981*4882a593Smuzhiyun 		.pm = &tpacpi_pm,
982*4882a593Smuzhiyun 	},
983*4882a593Smuzhiyun 	.shutdown = tpacpi_shutdown_handler,
984*4882a593Smuzhiyun };
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun static struct platform_driver tpacpi_hwmon_pdriver = {
987*4882a593Smuzhiyun 	.driver = {
988*4882a593Smuzhiyun 		.name = TPACPI_HWMON_DRVR_NAME,
989*4882a593Smuzhiyun 	},
990*4882a593Smuzhiyun };
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun /*************************************************************************
993*4882a593Smuzhiyun  * sysfs support helpers
994*4882a593Smuzhiyun  */
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun struct attribute_set {
997*4882a593Smuzhiyun 	unsigned int members, max_members;
998*4882a593Smuzhiyun 	struct attribute_group group;
999*4882a593Smuzhiyun };
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun struct attribute_set_obj {
1002*4882a593Smuzhiyun 	struct attribute_set s;
1003*4882a593Smuzhiyun 	struct attribute *a;
1004*4882a593Smuzhiyun } __attribute__((packed));
1005*4882a593Smuzhiyun 
create_attr_set(unsigned int max_members,const char * name)1006*4882a593Smuzhiyun static struct attribute_set *create_attr_set(unsigned int max_members,
1007*4882a593Smuzhiyun 						const char *name)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun 	struct attribute_set_obj *sobj;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	if (max_members == 0)
1012*4882a593Smuzhiyun 		return NULL;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	/* Allocates space for implicit NULL at the end too */
1015*4882a593Smuzhiyun 	sobj = kzalloc(sizeof(struct attribute_set_obj) +
1016*4882a593Smuzhiyun 		    max_members * sizeof(struct attribute *),
1017*4882a593Smuzhiyun 		    GFP_KERNEL);
1018*4882a593Smuzhiyun 	if (!sobj)
1019*4882a593Smuzhiyun 		return NULL;
1020*4882a593Smuzhiyun 	sobj->s.max_members = max_members;
1021*4882a593Smuzhiyun 	sobj->s.group.attrs = &sobj->a;
1022*4882a593Smuzhiyun 	sobj->s.group.name = name;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	return &sobj->s;
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun #define destroy_attr_set(_set) \
1028*4882a593Smuzhiyun 	kfree(_set);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun /* not multi-threaded safe, use it in a single thread per set */
add_to_attr_set(struct attribute_set * s,struct attribute * attr)1031*4882a593Smuzhiyun static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun 	if (!s || !attr)
1034*4882a593Smuzhiyun 		return -EINVAL;
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	if (s->members >= s->max_members)
1037*4882a593Smuzhiyun 		return -ENOMEM;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	s->group.attrs[s->members] = attr;
1040*4882a593Smuzhiyun 	s->members++;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	return 0;
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun 
add_many_to_attr_set(struct attribute_set * s,struct attribute ** attr,unsigned int count)1045*4882a593Smuzhiyun static int add_many_to_attr_set(struct attribute_set *s,
1046*4882a593Smuzhiyun 			struct attribute **attr,
1047*4882a593Smuzhiyun 			unsigned int count)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	int i, res;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
1052*4882a593Smuzhiyun 		res = add_to_attr_set(s, attr[i]);
1053*4882a593Smuzhiyun 		if (res)
1054*4882a593Smuzhiyun 			return res;
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	return 0;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
delete_attr_set(struct attribute_set * s,struct kobject * kobj)1060*4882a593Smuzhiyun static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun 	sysfs_remove_group(kobj, &s->group);
1063*4882a593Smuzhiyun 	destroy_attr_set(s);
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1067*4882a593Smuzhiyun 	sysfs_create_group(_kobj, &_attr_set->group)
1068*4882a593Smuzhiyun 
parse_strtoul(const char * buf,unsigned long max,unsigned long * value)1069*4882a593Smuzhiyun static int parse_strtoul(const char *buf,
1070*4882a593Smuzhiyun 		unsigned long max, unsigned long *value)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun 	char *endp;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	*value = simple_strtoul(skip_spaces(buf), &endp, 0);
1075*4882a593Smuzhiyun 	endp = skip_spaces(endp);
1076*4882a593Smuzhiyun 	if (*endp || *value > max)
1077*4882a593Smuzhiyun 		return -EINVAL;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	return 0;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun 
tpacpi_disable_brightness_delay(void)1082*4882a593Smuzhiyun static void tpacpi_disable_brightness_delay(void)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1085*4882a593Smuzhiyun 		pr_notice("ACPI backlight control delay disabled\n");
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun 
printk_deprecated_attribute(const char * const what,const char * const details)1088*4882a593Smuzhiyun static void printk_deprecated_attribute(const char * const what,
1089*4882a593Smuzhiyun 					const char * const details)
1090*4882a593Smuzhiyun {
1091*4882a593Smuzhiyun 	tpacpi_log_usertask("deprecated sysfs attribute");
1092*4882a593Smuzhiyun 	pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1093*4882a593Smuzhiyun 		what, details);
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun /*************************************************************************
1097*4882a593Smuzhiyun  * rfkill and radio control support helpers
1098*4882a593Smuzhiyun  */
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun /*
1101*4882a593Smuzhiyun  * ThinkPad-ACPI firmware handling model:
1102*4882a593Smuzhiyun  *
1103*4882a593Smuzhiyun  * WLSW (master wireless switch) is event-driven, and is common to all
1104*4882a593Smuzhiyun  * firmware-controlled radios.  It cannot be controlled, just monitored,
1105*4882a593Smuzhiyun  * as expected.  It overrides all radio state in firmware
1106*4882a593Smuzhiyun  *
1107*4882a593Smuzhiyun  * The kernel, a masked-off hotkey, and WLSW can change the radio state
1108*4882a593Smuzhiyun  * (TODO: verify how WLSW interacts with the returned radio state).
1109*4882a593Smuzhiyun  *
1110*4882a593Smuzhiyun  * The only time there are shadow radio state changes, is when
1111*4882a593Smuzhiyun  * masked-off hotkeys are used.
1112*4882a593Smuzhiyun  */
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /*
1115*4882a593Smuzhiyun  * Internal driver API for radio state:
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1118*4882a593Smuzhiyun  * bool: true means radio blocked (off)
1119*4882a593Smuzhiyun  */
1120*4882a593Smuzhiyun enum tpacpi_rfkill_state {
1121*4882a593Smuzhiyun 	TPACPI_RFK_RADIO_OFF = 0,
1122*4882a593Smuzhiyun 	TPACPI_RFK_RADIO_ON
1123*4882a593Smuzhiyun };
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun /* rfkill switches */
1126*4882a593Smuzhiyun enum tpacpi_rfk_id {
1127*4882a593Smuzhiyun 	TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1128*4882a593Smuzhiyun 	TPACPI_RFK_WWAN_SW_ID,
1129*4882a593Smuzhiyun 	TPACPI_RFK_UWB_SW_ID,
1130*4882a593Smuzhiyun 	TPACPI_RFK_SW_MAX
1131*4882a593Smuzhiyun };
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun static const char *tpacpi_rfkill_names[] = {
1134*4882a593Smuzhiyun 	[TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1135*4882a593Smuzhiyun 	[TPACPI_RFK_WWAN_SW_ID] = "wwan",
1136*4882a593Smuzhiyun 	[TPACPI_RFK_UWB_SW_ID] = "uwb",
1137*4882a593Smuzhiyun 	[TPACPI_RFK_SW_MAX] = NULL
1138*4882a593Smuzhiyun };
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun /* ThinkPad-ACPI rfkill subdriver */
1141*4882a593Smuzhiyun struct tpacpi_rfk {
1142*4882a593Smuzhiyun 	struct rfkill *rfkill;
1143*4882a593Smuzhiyun 	enum tpacpi_rfk_id id;
1144*4882a593Smuzhiyun 	const struct tpacpi_rfk_ops *ops;
1145*4882a593Smuzhiyun };
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun struct tpacpi_rfk_ops {
1148*4882a593Smuzhiyun 	/* firmware interface */
1149*4882a593Smuzhiyun 	int (*get_status)(void);
1150*4882a593Smuzhiyun 	int (*set_status)(const enum tpacpi_rfkill_state);
1151*4882a593Smuzhiyun };
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun /* Query FW and update rfkill sw state for a given rfkill switch */
tpacpi_rfk_update_swstate(const struct tpacpi_rfk * tp_rfk)1156*4882a593Smuzhiyun static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun 	int status;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	if (!tp_rfk)
1161*4882a593Smuzhiyun 		return -ENODEV;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	status = (tp_rfk->ops->get_status)();
1164*4882a593Smuzhiyun 	if (status < 0)
1165*4882a593Smuzhiyun 		return status;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	rfkill_set_sw_state(tp_rfk->rfkill,
1168*4882a593Smuzhiyun 			    (status == TPACPI_RFK_RADIO_OFF));
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	return status;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun /*
1174*4882a593Smuzhiyun  * Sync the HW-blocking state of all rfkill switches,
1175*4882a593Smuzhiyun  * do notice it causes the rfkill core to schedule uevents
1176*4882a593Smuzhiyun  */
tpacpi_rfk_update_hwblock_state(bool blocked)1177*4882a593Smuzhiyun static void tpacpi_rfk_update_hwblock_state(bool blocked)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun 	unsigned int i;
1180*4882a593Smuzhiyun 	struct tpacpi_rfk *tp_rfk;
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1183*4882a593Smuzhiyun 		tp_rfk = tpacpi_rfkill_switches[i];
1184*4882a593Smuzhiyun 		if (tp_rfk) {
1185*4882a593Smuzhiyun 			if (rfkill_set_hw_state(tp_rfk->rfkill,
1186*4882a593Smuzhiyun 						blocked)) {
1187*4882a593Smuzhiyun 				/* ignore -- we track sw block */
1188*4882a593Smuzhiyun 			}
1189*4882a593Smuzhiyun 		}
1190*4882a593Smuzhiyun 	}
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun /* Call to get the WLSW state from the firmware */
1194*4882a593Smuzhiyun static int hotkey_get_wlsw(void);
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun /* Call to query WLSW state and update all rfkill switches */
tpacpi_rfk_check_hwblock_state(void)1197*4882a593Smuzhiyun static bool tpacpi_rfk_check_hwblock_state(void)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun 	int res = hotkey_get_wlsw();
1200*4882a593Smuzhiyun 	int hw_blocked;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	/* When unknown or unsupported, we have to assume it is unblocked */
1203*4882a593Smuzhiyun 	if (res < 0)
1204*4882a593Smuzhiyun 		return false;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1207*4882a593Smuzhiyun 	tpacpi_rfk_update_hwblock_state(hw_blocked);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	return hw_blocked;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun 
tpacpi_rfk_hook_set_block(void * data,bool blocked)1212*4882a593Smuzhiyun static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun 	struct tpacpi_rfk *tp_rfk = data;
1215*4882a593Smuzhiyun 	int res;
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_RFKILL,
1218*4882a593Smuzhiyun 		   "request to change radio state to %s\n",
1219*4882a593Smuzhiyun 		   blocked ? "blocked" : "unblocked");
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	/* try to set radio state */
1222*4882a593Smuzhiyun 	res = (tp_rfk->ops->set_status)(blocked ?
1223*4882a593Smuzhiyun 				TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	/* and update the rfkill core with whatever the FW really did */
1226*4882a593Smuzhiyun 	tpacpi_rfk_update_swstate(tp_rfk);
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	return (res < 0) ? res : 0;
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1232*4882a593Smuzhiyun 	.set_block = tpacpi_rfk_hook_set_block,
1233*4882a593Smuzhiyun };
1234*4882a593Smuzhiyun 
tpacpi_new_rfkill(const enum tpacpi_rfk_id id,const struct tpacpi_rfk_ops * tp_rfkops,const enum rfkill_type rfktype,const char * name,const bool set_default)1235*4882a593Smuzhiyun static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1236*4882a593Smuzhiyun 			const struct tpacpi_rfk_ops *tp_rfkops,
1237*4882a593Smuzhiyun 			const enum rfkill_type rfktype,
1238*4882a593Smuzhiyun 			const char *name,
1239*4882a593Smuzhiyun 			const bool set_default)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	struct tpacpi_rfk *atp_rfk;
1242*4882a593Smuzhiyun 	int res;
1243*4882a593Smuzhiyun 	bool sw_state = false;
1244*4882a593Smuzhiyun 	bool hw_state;
1245*4882a593Smuzhiyun 	int sw_status;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1250*4882a593Smuzhiyun 	if (atp_rfk)
1251*4882a593Smuzhiyun 		atp_rfk->rfkill = rfkill_alloc(name,
1252*4882a593Smuzhiyun 						&tpacpi_pdev->dev,
1253*4882a593Smuzhiyun 						rfktype,
1254*4882a593Smuzhiyun 						&tpacpi_rfk_rfkill_ops,
1255*4882a593Smuzhiyun 						atp_rfk);
1256*4882a593Smuzhiyun 	if (!atp_rfk || !atp_rfk->rfkill) {
1257*4882a593Smuzhiyun 		pr_err("failed to allocate memory for rfkill class\n");
1258*4882a593Smuzhiyun 		kfree(atp_rfk);
1259*4882a593Smuzhiyun 		return -ENOMEM;
1260*4882a593Smuzhiyun 	}
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	atp_rfk->id = id;
1263*4882a593Smuzhiyun 	atp_rfk->ops = tp_rfkops;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	sw_status = (tp_rfkops->get_status)();
1266*4882a593Smuzhiyun 	if (sw_status < 0) {
1267*4882a593Smuzhiyun 		pr_err("failed to read initial state for %s, error %d\n",
1268*4882a593Smuzhiyun 		       name, sw_status);
1269*4882a593Smuzhiyun 	} else {
1270*4882a593Smuzhiyun 		sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1271*4882a593Smuzhiyun 		if (set_default) {
1272*4882a593Smuzhiyun 			/* try to keep the initial state, since we ask the
1273*4882a593Smuzhiyun 			 * firmware to preserve it across S5 in NVRAM */
1274*4882a593Smuzhiyun 			rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1275*4882a593Smuzhiyun 		}
1276*4882a593Smuzhiyun 	}
1277*4882a593Smuzhiyun 	hw_state = tpacpi_rfk_check_hwblock_state();
1278*4882a593Smuzhiyun 	rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	res = rfkill_register(atp_rfk->rfkill);
1281*4882a593Smuzhiyun 	if (res < 0) {
1282*4882a593Smuzhiyun 		pr_err("failed to register %s rfkill switch: %d\n", name, res);
1283*4882a593Smuzhiyun 		rfkill_destroy(atp_rfk->rfkill);
1284*4882a593Smuzhiyun 		kfree(atp_rfk);
1285*4882a593Smuzhiyun 		return res;
1286*4882a593Smuzhiyun 	}
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	tpacpi_rfkill_switches[id] = atp_rfk;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	pr_info("rfkill switch %s: radio is %sblocked\n",
1291*4882a593Smuzhiyun 		name, (sw_state || hw_state) ? "" : "un");
1292*4882a593Smuzhiyun 	return 0;
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun 
tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)1295*4882a593Smuzhiyun static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1296*4882a593Smuzhiyun {
1297*4882a593Smuzhiyun 	struct tpacpi_rfk *tp_rfk;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	BUG_ON(id >= TPACPI_RFK_SW_MAX);
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	tp_rfk = tpacpi_rfkill_switches[id];
1302*4882a593Smuzhiyun 	if (tp_rfk) {
1303*4882a593Smuzhiyun 		rfkill_unregister(tp_rfk->rfkill);
1304*4882a593Smuzhiyun 		rfkill_destroy(tp_rfk->rfkill);
1305*4882a593Smuzhiyun 		tpacpi_rfkill_switches[id] = NULL;
1306*4882a593Smuzhiyun 		kfree(tp_rfk);
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
printk_deprecated_rfkill_attribute(const char * const what)1310*4882a593Smuzhiyun static void printk_deprecated_rfkill_attribute(const char * const what)
1311*4882a593Smuzhiyun {
1312*4882a593Smuzhiyun 	printk_deprecated_attribute(what,
1313*4882a593Smuzhiyun 			"Please switch to generic rfkill before year 2010");
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun /* sysfs <radio> enable ------------------------------------------------ */
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,struct device_attribute * attr,char * buf)1317*4882a593Smuzhiyun static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1318*4882a593Smuzhiyun 					    struct device_attribute *attr,
1319*4882a593Smuzhiyun 					    char *buf)
1320*4882a593Smuzhiyun {
1321*4882a593Smuzhiyun 	int status;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	printk_deprecated_rfkill_attribute(attr->attr.name);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	/* This is in the ABI... */
1326*4882a593Smuzhiyun 	if (tpacpi_rfk_check_hwblock_state()) {
1327*4882a593Smuzhiyun 		status = TPACPI_RFK_RADIO_OFF;
1328*4882a593Smuzhiyun 	} else {
1329*4882a593Smuzhiyun 		status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1330*4882a593Smuzhiyun 		if (status < 0)
1331*4882a593Smuzhiyun 			return status;
1332*4882a593Smuzhiyun 	}
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n",
1335*4882a593Smuzhiyun 			(status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,struct device_attribute * attr,const char * buf,size_t count)1338*4882a593Smuzhiyun static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1339*4882a593Smuzhiyun 			    struct device_attribute *attr,
1340*4882a593Smuzhiyun 			    const char *buf, size_t count)
1341*4882a593Smuzhiyun {
1342*4882a593Smuzhiyun 	unsigned long t;
1343*4882a593Smuzhiyun 	int res;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	printk_deprecated_rfkill_attribute(attr->attr.name);
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
1348*4882a593Smuzhiyun 		return -EINVAL;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	/* This is in the ABI... */
1353*4882a593Smuzhiyun 	if (tpacpi_rfk_check_hwblock_state() && !!t)
1354*4882a593Smuzhiyun 		return -EPERM;
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1357*4882a593Smuzhiyun 				TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1358*4882a593Smuzhiyun 	tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 	return (res < 0) ? res : count;
1361*4882a593Smuzhiyun }
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun /* procfs -------------------------------------------------------------- */
tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,struct seq_file * m)1364*4882a593Smuzhiyun static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun 	if (id >= TPACPI_RFK_SW_MAX)
1367*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
1368*4882a593Smuzhiyun 	else {
1369*4882a593Smuzhiyun 		int status;
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 		/* This is in the ABI... */
1372*4882a593Smuzhiyun 		if (tpacpi_rfk_check_hwblock_state()) {
1373*4882a593Smuzhiyun 			status = TPACPI_RFK_RADIO_OFF;
1374*4882a593Smuzhiyun 		} else {
1375*4882a593Smuzhiyun 			status = tpacpi_rfk_update_swstate(
1376*4882a593Smuzhiyun 						tpacpi_rfkill_switches[id]);
1377*4882a593Smuzhiyun 			if (status < 0)
1378*4882a593Smuzhiyun 				return status;
1379*4882a593Smuzhiyun 		}
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 		seq_printf(m, "status:\t\t%s\n",
1382*4882a593Smuzhiyun 				(status == TPACPI_RFK_RADIO_ON) ?
1383*4882a593Smuzhiyun 					"enabled" : "disabled");
1384*4882a593Smuzhiyun 		seq_printf(m, "commands:\tenable, disable\n");
1385*4882a593Smuzhiyun 	}
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	return 0;
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun 
tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id,char * buf)1390*4882a593Smuzhiyun static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1391*4882a593Smuzhiyun {
1392*4882a593Smuzhiyun 	char *cmd;
1393*4882a593Smuzhiyun 	int status = -1;
1394*4882a593Smuzhiyun 	int res = 0;
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	if (id >= TPACPI_RFK_SW_MAX)
1397*4882a593Smuzhiyun 		return -ENODEV;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
1400*4882a593Smuzhiyun 		if (strlencmp(cmd, "enable") == 0)
1401*4882a593Smuzhiyun 			status = TPACPI_RFK_RADIO_ON;
1402*4882a593Smuzhiyun 		else if (strlencmp(cmd, "disable") == 0)
1403*4882a593Smuzhiyun 			status = TPACPI_RFK_RADIO_OFF;
1404*4882a593Smuzhiyun 		else
1405*4882a593Smuzhiyun 			return -EINVAL;
1406*4882a593Smuzhiyun 	}
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	if (status != -1) {
1409*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1410*4882a593Smuzhiyun 				(status == TPACPI_RFK_RADIO_ON) ?
1411*4882a593Smuzhiyun 						"enable" : "disable",
1412*4882a593Smuzhiyun 				tpacpi_rfkill_names[id]);
1413*4882a593Smuzhiyun 		res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1414*4882a593Smuzhiyun 		tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1415*4882a593Smuzhiyun 	}
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	return res;
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun /*************************************************************************
1421*4882a593Smuzhiyun  * thinkpad-acpi driver attributes
1422*4882a593Smuzhiyun  */
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun /* interface_version --------------------------------------------------- */
interface_version_show(struct device_driver * drv,char * buf)1425*4882a593Smuzhiyun static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun static DRIVER_ATTR_RO(interface_version);
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun /* debug_level --------------------------------------------------------- */
debug_level_show(struct device_driver * drv,char * buf)1432*4882a593Smuzhiyun static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1433*4882a593Smuzhiyun {
1434*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
debug_level_store(struct device_driver * drv,const char * buf,size_t count)1437*4882a593Smuzhiyun static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1438*4882a593Smuzhiyun 				 size_t count)
1439*4882a593Smuzhiyun {
1440*4882a593Smuzhiyun 	unsigned long t;
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	if (parse_strtoul(buf, 0xffff, &t))
1443*4882a593Smuzhiyun 		return -EINVAL;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	dbg_level = t;
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	return count;
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun static DRIVER_ATTR_RW(debug_level);
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun /* version ------------------------------------------------------------- */
version_show(struct device_driver * drv,char * buf)1452*4882a593Smuzhiyun static ssize_t version_show(struct device_driver *drv, char *buf)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1455*4882a593Smuzhiyun 			TPACPI_DESC, TPACPI_VERSION);
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun static DRIVER_ATTR_RO(version);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun /* wlsw_emulstate ------------------------------------------------------ */
wlsw_emulstate_show(struct device_driver * drv,char * buf)1464*4882a593Smuzhiyun static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1465*4882a593Smuzhiyun {
1466*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
wlsw_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1469*4882a593Smuzhiyun static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1470*4882a593Smuzhiyun 				    size_t count)
1471*4882a593Smuzhiyun {
1472*4882a593Smuzhiyun 	unsigned long t;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
1475*4882a593Smuzhiyun 		return -EINVAL;
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	if (tpacpi_wlsw_emulstate != !!t) {
1478*4882a593Smuzhiyun 		tpacpi_wlsw_emulstate = !!t;
1479*4882a593Smuzhiyun 		tpacpi_rfk_update_hwblock_state(!t);	/* negative logic */
1480*4882a593Smuzhiyun 	}
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	return count;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun static DRIVER_ATTR_RW(wlsw_emulstate);
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun /* bluetooth_emulstate ------------------------------------------------- */
bluetooth_emulstate_show(struct device_driver * drv,char * buf)1487*4882a593Smuzhiyun static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1488*4882a593Smuzhiyun {
1489*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun 
bluetooth_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1492*4882a593Smuzhiyun static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1493*4882a593Smuzhiyun 					 const char *buf, size_t count)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun 	unsigned long t;
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
1498*4882a593Smuzhiyun 		return -EINVAL;
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	tpacpi_bluetooth_emulstate = !!t;
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	return count;
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun static DRIVER_ATTR_RW(bluetooth_emulstate);
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun /* wwan_emulstate ------------------------------------------------- */
wwan_emulstate_show(struct device_driver * drv,char * buf)1507*4882a593Smuzhiyun static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1508*4882a593Smuzhiyun {
1509*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun 
wwan_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1512*4882a593Smuzhiyun static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1513*4882a593Smuzhiyun 				    size_t count)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	unsigned long t;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
1518*4882a593Smuzhiyun 		return -EINVAL;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	tpacpi_wwan_emulstate = !!t;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	return count;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun static DRIVER_ATTR_RW(wwan_emulstate);
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun /* uwb_emulstate ------------------------------------------------- */
uwb_emulstate_show(struct device_driver * drv,char * buf)1527*4882a593Smuzhiyun static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1528*4882a593Smuzhiyun {
1529*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1530*4882a593Smuzhiyun }
1531*4882a593Smuzhiyun 
uwb_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1532*4882a593Smuzhiyun static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1533*4882a593Smuzhiyun 				   size_t count)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	unsigned long t;
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
1538*4882a593Smuzhiyun 		return -EINVAL;
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 	tpacpi_uwb_emulstate = !!t;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	return count;
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun static DRIVER_ATTR_RW(uwb_emulstate);
1545*4882a593Smuzhiyun #endif
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun static struct driver_attribute *tpacpi_driver_attributes[] = {
1550*4882a593Smuzhiyun 	&driver_attr_debug_level, &driver_attr_version,
1551*4882a593Smuzhiyun 	&driver_attr_interface_version,
1552*4882a593Smuzhiyun };
1553*4882a593Smuzhiyun 
tpacpi_create_driver_attributes(struct device_driver * drv)1554*4882a593Smuzhiyun static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1555*4882a593Smuzhiyun {
1556*4882a593Smuzhiyun 	int i, res;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	i = 0;
1559*4882a593Smuzhiyun 	res = 0;
1560*4882a593Smuzhiyun 	while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1561*4882a593Smuzhiyun 		res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1562*4882a593Smuzhiyun 		i++;
1563*4882a593Smuzhiyun 	}
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1566*4882a593Smuzhiyun 	if (!res && dbg_wlswemul)
1567*4882a593Smuzhiyun 		res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1568*4882a593Smuzhiyun 	if (!res && dbg_bluetoothemul)
1569*4882a593Smuzhiyun 		res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1570*4882a593Smuzhiyun 	if (!res && dbg_wwanemul)
1571*4882a593Smuzhiyun 		res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1572*4882a593Smuzhiyun 	if (!res && dbg_uwbemul)
1573*4882a593Smuzhiyun 		res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1574*4882a593Smuzhiyun #endif
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun 	return res;
1577*4882a593Smuzhiyun }
1578*4882a593Smuzhiyun 
tpacpi_remove_driver_attributes(struct device_driver * drv)1579*4882a593Smuzhiyun static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	int i;
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1584*4882a593Smuzhiyun 		driver_remove_file(drv, tpacpi_driver_attributes[i]);
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1587*4882a593Smuzhiyun 	driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1588*4882a593Smuzhiyun 	driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1589*4882a593Smuzhiyun 	driver_remove_file(drv, &driver_attr_wwan_emulstate);
1590*4882a593Smuzhiyun 	driver_remove_file(drv, &driver_attr_uwb_emulstate);
1591*4882a593Smuzhiyun #endif
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun /*************************************************************************
1595*4882a593Smuzhiyun  * Firmware Data
1596*4882a593Smuzhiyun  */
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun /*
1599*4882a593Smuzhiyun  * Table of recommended minimum BIOS versions
1600*4882a593Smuzhiyun  *
1601*4882a593Smuzhiyun  * Reasons for listing:
1602*4882a593Smuzhiyun  *    1. Stable BIOS, listed because the unknown amount of
1603*4882a593Smuzhiyun  *       bugs and bad ACPI behaviour on older versions
1604*4882a593Smuzhiyun  *
1605*4882a593Smuzhiyun  *    2. BIOS or EC fw with known bugs that trigger on Linux
1606*4882a593Smuzhiyun  *
1607*4882a593Smuzhiyun  *    3. BIOS with known reduced functionality in older versions
1608*4882a593Smuzhiyun  *
1609*4882a593Smuzhiyun  *  We recommend the latest BIOS and EC version.
1610*4882a593Smuzhiyun  *  We only support the latest BIOS and EC fw version as a rule.
1611*4882a593Smuzhiyun  *
1612*4882a593Smuzhiyun  *  Sources: IBM ThinkPad Public Web Documents (update changelogs),
1613*4882a593Smuzhiyun  *  Information from users in ThinkWiki
1614*4882a593Smuzhiyun  *
1615*4882a593Smuzhiyun  *  WARNING: we use this table also to detect that the machine is
1616*4882a593Smuzhiyun  *  a ThinkPad in some cases, so don't remove entries lightly.
1617*4882a593Smuzhiyun  */
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun #define TPV_Q(__v, __id1, __id2, __bv1, __bv2)		\
1620*4882a593Smuzhiyun 	{ .vendor	= (__v),			\
1621*4882a593Smuzhiyun 	  .bios		= TPID(__id1, __id2),		\
1622*4882a593Smuzhiyun 	  .ec		= TPACPI_MATCH_ANY,		\
1623*4882a593Smuzhiyun 	  .quirks	= TPACPI_MATCH_ANY_VERSION << 16 \
1624*4882a593Smuzhiyun 			  | TPVER(__bv1, __bv2) }
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2,	\
1627*4882a593Smuzhiyun 		__eid, __ev1, __ev2)			\
1628*4882a593Smuzhiyun 	{ .vendor	= (__v),			\
1629*4882a593Smuzhiyun 	  .bios		= TPID(__bid1, __bid2),		\
1630*4882a593Smuzhiyun 	  .ec		= __eid,			\
1631*4882a593Smuzhiyun 	  .quirks	= TPVER(__ev1, __ev2) << 16	\
1632*4882a593Smuzhiyun 			  | TPVER(__bv1, __bv2) }
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1635*4882a593Smuzhiyun 	TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun /* Outdated IBM BIOSes often lack the EC id string */
1638*4882a593Smuzhiyun #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1639*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, 	\
1640*4882a593Smuzhiyun 		__bv1, __bv2, TPID(__id1, __id2),	\
1641*4882a593Smuzhiyun 		__ev1, __ev2),				\
1642*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, 	\
1643*4882a593Smuzhiyun 		__bv1, __bv2, TPACPI_MATCH_UNKNOWN,	\
1644*4882a593Smuzhiyun 		__ev1, __ev2)
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun /* Outdated IBM BIOSes often lack the EC id string */
1647*4882a593Smuzhiyun #define TPV_QI2(__bid1, __bid2, __bv1, __bv2,		\
1648*4882a593Smuzhiyun 		__eid1, __eid2, __ev1, __ev2) 		\
1649*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, 	\
1650*4882a593Smuzhiyun 		__bv1, __bv2, TPID(__eid1, __eid2),	\
1651*4882a593Smuzhiyun 		__ev1, __ev2),				\
1652*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, 	\
1653*4882a593Smuzhiyun 		__bv1, __bv2, TPACPI_MATCH_UNKNOWN,	\
1654*4882a593Smuzhiyun 		__ev1, __ev2)
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1657*4882a593Smuzhiyun 	TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1660*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, 	\
1661*4882a593Smuzhiyun 		__bv1, __bv2, TPID(__id1, __id2),	\
1662*4882a593Smuzhiyun 		__ev1, __ev2)
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun #define TPV_QL2(__bid1, __bid2, __bv1, __bv2,		\
1665*4882a593Smuzhiyun 		__eid1, __eid2, __ev1, __ev2) 		\
1666*4882a593Smuzhiyun 	TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, 	\
1667*4882a593Smuzhiyun 		__bv1, __bv2, TPID(__eid1, __eid2),	\
1668*4882a593Smuzhiyun 		__ev1, __ev2)
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1671*4882a593Smuzhiyun 	/*  Numeric models ------------------ */
1672*4882a593Smuzhiyun 	/*      FW MODEL   BIOS VERS	      */
1673*4882a593Smuzhiyun 	TPV_QI0('I', 'M',  '6', '5'),		 /* 570 */
1674*4882a593Smuzhiyun 	TPV_QI0('I', 'U',  '2', '6'),		 /* 570E */
1675*4882a593Smuzhiyun 	TPV_QI0('I', 'B',  '5', '4'),		 /* 600 */
1676*4882a593Smuzhiyun 	TPV_QI0('I', 'H',  '4', '7'),		 /* 600E */
1677*4882a593Smuzhiyun 	TPV_QI0('I', 'N',  '3', '6'),		 /* 600E */
1678*4882a593Smuzhiyun 	TPV_QI0('I', 'T',  '5', '5'),		 /* 600X */
1679*4882a593Smuzhiyun 	TPV_QI0('I', 'D',  '4', '8'),		 /* 770, 770E, 770ED */
1680*4882a593Smuzhiyun 	TPV_QI0('I', 'I',  '4', '2'),		 /* 770X */
1681*4882a593Smuzhiyun 	TPV_QI0('I', 'O',  '2', '3'),		 /* 770Z */
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	/* A-series ------------------------- */
1684*4882a593Smuzhiyun 	/*      FW MODEL   BIOS VERS  EC VERS */
1685*4882a593Smuzhiyun 	TPV_QI0('I', 'W',  '5', '9'),		 /* A20m */
1686*4882a593Smuzhiyun 	TPV_QI0('I', 'V',  '6', '9'),		 /* A20p */
1687*4882a593Smuzhiyun 	TPV_QI0('1', '0',  '2', '6'),		 /* A21e, A22e */
1688*4882a593Smuzhiyun 	TPV_QI0('K', 'U',  '3', '6'),		 /* A21e */
1689*4882a593Smuzhiyun 	TPV_QI0('K', 'X',  '3', '6'),		 /* A21m, A22m */
1690*4882a593Smuzhiyun 	TPV_QI0('K', 'Y',  '3', '8'),		 /* A21p, A22p */
1691*4882a593Smuzhiyun 	TPV_QI0('1', 'B',  '1', '7'),		 /* A22e */
1692*4882a593Smuzhiyun 	TPV_QI0('1', '3',  '2', '0'),		 /* A22m */
1693*4882a593Smuzhiyun 	TPV_QI0('1', 'E',  '7', '3'),		 /* A30/p (0) */
1694*4882a593Smuzhiyun 	TPV_QI1('1', 'G',  '4', '1',  '1', '7'), /* A31/p (0) */
1695*4882a593Smuzhiyun 	TPV_QI1('1', 'N',  '1', '6',  '0', '7'), /* A31/p (0) */
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	/* G-series ------------------------- */
1698*4882a593Smuzhiyun 	/*      FW MODEL   BIOS VERS	      */
1699*4882a593Smuzhiyun 	TPV_QI0('1', 'T',  'A', '6'),		 /* G40 */
1700*4882a593Smuzhiyun 	TPV_QI0('1', 'X',  '5', '7'),		 /* G41 */
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	/* R-series, T-series --------------- */
1703*4882a593Smuzhiyun 	/*      FW MODEL   BIOS VERS  EC VERS */
1704*4882a593Smuzhiyun 	TPV_QI0('1', 'C',  'F', '0'),		 /* R30 */
1705*4882a593Smuzhiyun 	TPV_QI0('1', 'F',  'F', '1'),		 /* R31 */
1706*4882a593Smuzhiyun 	TPV_QI0('1', 'M',  '9', '7'),		 /* R32 */
1707*4882a593Smuzhiyun 	TPV_QI0('1', 'O',  '6', '1'),		 /* R40 */
1708*4882a593Smuzhiyun 	TPV_QI0('1', 'P',  '6', '5'),		 /* R40 */
1709*4882a593Smuzhiyun 	TPV_QI0('1', 'S',  '7', '0'),		 /* R40e */
1710*4882a593Smuzhiyun 	TPV_QI1('1', 'R',  'D', 'R',  '7', '1'), /* R50/p, R51,
1711*4882a593Smuzhiyun 						    T40/p, T41/p, T42/p (1) */
1712*4882a593Smuzhiyun 	TPV_QI1('1', 'V',  '7', '1',  '2', '8'), /* R50e, R51 (1) */
1713*4882a593Smuzhiyun 	TPV_QI1('7', '8',  '7', '1',  '0', '6'), /* R51e (1) */
1714*4882a593Smuzhiyun 	TPV_QI1('7', '6',  '6', '9',  '1', '6'), /* R52 (1) */
1715*4882a593Smuzhiyun 	TPV_QI1('7', '0',  '6', '9',  '2', '8'), /* R52, T43 (1) */
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	TPV_QI0('I', 'Y',  '6', '1'),		 /* T20 */
1718*4882a593Smuzhiyun 	TPV_QI0('K', 'Z',  '3', '4'),		 /* T21 */
1719*4882a593Smuzhiyun 	TPV_QI0('1', '6',  '3', '2'),		 /* T22 */
1720*4882a593Smuzhiyun 	TPV_QI1('1', 'A',  '6', '4',  '2', '3'), /* T23 (0) */
1721*4882a593Smuzhiyun 	TPV_QI1('1', 'I',  '7', '1',  '2', '0'), /* T30 (0) */
1722*4882a593Smuzhiyun 	TPV_QI1('1', 'Y',  '6', '5',  '2', '9'), /* T43/p (1) */
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	TPV_QL1('7', '9',  'E', '3',  '5', '0'), /* T60/p */
1725*4882a593Smuzhiyun 	TPV_QL1('7', 'C',  'D', '2',  '2', '2'), /* R60, R60i */
1726*4882a593Smuzhiyun 	TPV_QL1('7', 'E',  'D', '0',  '1', '5'), /* R60e, R60i */
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	/*      BIOS FW    BIOS VERS  EC FW     EC VERS */
1729*4882a593Smuzhiyun 	TPV_QI2('1', 'W',  '9', '0',  '1', 'V', '2', '8'), /* R50e (1) */
1730*4882a593Smuzhiyun 	TPV_QL2('7', 'I',  '3', '4',  '7', '9', '5', '0'), /* T60/p wide */
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 	/* X-series ------------------------- */
1733*4882a593Smuzhiyun 	/*      FW MODEL   BIOS VERS  EC VERS */
1734*4882a593Smuzhiyun 	TPV_QI0('I', 'Z',  '9', 'D'),		 /* X20, X21 */
1735*4882a593Smuzhiyun 	TPV_QI0('1', 'D',  '7', '0'),		 /* X22, X23, X24 */
1736*4882a593Smuzhiyun 	TPV_QI1('1', 'K',  '4', '8',  '1', '8'), /* X30 (0) */
1737*4882a593Smuzhiyun 	TPV_QI1('1', 'Q',  '9', '7',  '2', '3'), /* X31, X32 (0) */
1738*4882a593Smuzhiyun 	TPV_QI1('1', 'U',  'D', '3',  'B', '2'), /* X40 (0) */
1739*4882a593Smuzhiyun 	TPV_QI1('7', '4',  '6', '4',  '2', '7'), /* X41 (0) */
1740*4882a593Smuzhiyun 	TPV_QI1('7', '5',  '6', '0',  '2', '0'), /* X41t (0) */
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	TPV_QL1('7', 'B',  'D', '7',  '4', '0'), /* X60/s */
1743*4882a593Smuzhiyun 	TPV_QL1('7', 'J',  '3', '0',  '1', '3'), /* X60t */
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	/* (0) - older versions lack DMI EC fw string and functionality */
1746*4882a593Smuzhiyun 	/* (1) - older versions known to lack functionality */
1747*4882a593Smuzhiyun };
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun #undef TPV_QL1
1750*4882a593Smuzhiyun #undef TPV_QL0
1751*4882a593Smuzhiyun #undef TPV_QI2
1752*4882a593Smuzhiyun #undef TPV_QI1
1753*4882a593Smuzhiyun #undef TPV_QI0
1754*4882a593Smuzhiyun #undef TPV_Q_X
1755*4882a593Smuzhiyun #undef TPV_Q
1756*4882a593Smuzhiyun 
tpacpi_check_outdated_fw(void)1757*4882a593Smuzhiyun static void __init tpacpi_check_outdated_fw(void)
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun 	unsigned long fwvers;
1760*4882a593Smuzhiyun 	u16 ec_version, bios_version;
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1763*4882a593Smuzhiyun 				ARRAY_SIZE(tpacpi_bios_version_qtable));
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	if (!fwvers)
1766*4882a593Smuzhiyun 		return;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	bios_version = fwvers & 0xffffU;
1769*4882a593Smuzhiyun 	ec_version = (fwvers >> 16) & 0xffffU;
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 	/* note that unknown versions are set to 0x0000 and we use that */
1772*4882a593Smuzhiyun 	if ((bios_version > thinkpad_id.bios_release) ||
1773*4882a593Smuzhiyun 	    (ec_version > thinkpad_id.ec_release &&
1774*4882a593Smuzhiyun 				ec_version != TPACPI_MATCH_ANY_VERSION)) {
1775*4882a593Smuzhiyun 		/*
1776*4882a593Smuzhiyun 		 * The changelogs would let us track down the exact
1777*4882a593Smuzhiyun 		 * reason, but it is just too much of a pain to track
1778*4882a593Smuzhiyun 		 * it.  We only list BIOSes that are either really
1779*4882a593Smuzhiyun 		 * broken, or really stable to begin with, so it is
1780*4882a593Smuzhiyun 		 * best if the user upgrades the firmware anyway.
1781*4882a593Smuzhiyun 		 */
1782*4882a593Smuzhiyun 		pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1783*4882a593Smuzhiyun 		pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1784*4882a593Smuzhiyun 	}
1785*4882a593Smuzhiyun }
1786*4882a593Smuzhiyun 
tpacpi_is_fw_known(void)1787*4882a593Smuzhiyun static bool __init tpacpi_is_fw_known(void)
1788*4882a593Smuzhiyun {
1789*4882a593Smuzhiyun 	return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1790*4882a593Smuzhiyun 			ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun /****************************************************************************
1794*4882a593Smuzhiyun  ****************************************************************************
1795*4882a593Smuzhiyun  *
1796*4882a593Smuzhiyun  * Subdrivers
1797*4882a593Smuzhiyun  *
1798*4882a593Smuzhiyun  ****************************************************************************
1799*4882a593Smuzhiyun  ****************************************************************************/
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun /*************************************************************************
1802*4882a593Smuzhiyun  * thinkpad-acpi metadata subdriver
1803*4882a593Smuzhiyun  */
1804*4882a593Smuzhiyun 
thinkpad_acpi_driver_read(struct seq_file * m)1805*4882a593Smuzhiyun static int thinkpad_acpi_driver_read(struct seq_file *m)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun 	seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1808*4882a593Smuzhiyun 	seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1809*4882a593Smuzhiyun 	return 0;
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun static struct ibm_struct thinkpad_acpi_driver_data = {
1813*4882a593Smuzhiyun 	.name = "driver",
1814*4882a593Smuzhiyun 	.read = thinkpad_acpi_driver_read,
1815*4882a593Smuzhiyun };
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun /*************************************************************************
1818*4882a593Smuzhiyun  * Hotkey subdriver
1819*4882a593Smuzhiyun  */
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun /*
1822*4882a593Smuzhiyun  * ThinkPad firmware event model
1823*4882a593Smuzhiyun  *
1824*4882a593Smuzhiyun  * The ThinkPad firmware has two main event interfaces: normal ACPI
1825*4882a593Smuzhiyun  * notifications (which follow the ACPI standard), and a private event
1826*4882a593Smuzhiyun  * interface.
1827*4882a593Smuzhiyun  *
1828*4882a593Smuzhiyun  * The private event interface also issues events for the hotkeys.  As
1829*4882a593Smuzhiyun  * the driver gained features, the event handling code ended up being
1830*4882a593Smuzhiyun  * built around the hotkey subdriver.  This will need to be refactored
1831*4882a593Smuzhiyun  * to a more formal event API eventually.
1832*4882a593Smuzhiyun  *
1833*4882a593Smuzhiyun  * Some "hotkeys" are actually supposed to be used as event reports,
1834*4882a593Smuzhiyun  * such as "brightness has changed", "volume has changed", depending on
1835*4882a593Smuzhiyun  * the ThinkPad model and how the firmware is operating.
1836*4882a593Smuzhiyun  *
1837*4882a593Smuzhiyun  * Unlike other classes, hotkey-class events have mask/unmask control on
1838*4882a593Smuzhiyun  * non-ancient firmware.  However, how it behaves changes a lot with the
1839*4882a593Smuzhiyun  * firmware model and version.
1840*4882a593Smuzhiyun  */
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun enum {	/* hot key scan codes (derived from ACPI DSDT) */
1843*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF1		= 0,
1844*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF2,
1845*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF3,
1846*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF4,
1847*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF5,
1848*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF6,
1849*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF7,
1850*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF8,
1851*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF9,
1852*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF10,
1853*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF11,
1854*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNF12,
1855*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1856*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNINSERT,
1857*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNDELETE,
1858*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNHOME,
1859*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNEND,
1860*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1861*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1862*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FNSPACE,
1863*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1864*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1865*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_MUTE,
1866*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_THINKPAD,
1867*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK1,
1868*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK2,
1869*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK3,
1870*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK4,
1871*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK5,
1872*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK6,
1873*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK7,
1874*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK8,
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	/* Adaptive keyboard keycodes */
1877*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1878*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_MUTE2        = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1879*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1880*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1881*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CLOUD,
1882*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK9,
1883*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_VOICE,
1884*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK10,
1885*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_GESTURES,
1886*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK11,
1887*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK12,
1888*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_UNK13,
1889*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CONFIG,
1890*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_NEW_TAB,
1891*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_RELOAD,
1892*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_BACK,
1893*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1894*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_MIC_UP,
1895*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1896*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1897*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	/* Lenovo extended keymap, starting at 0x1300 */
1900*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1901*4882a593Smuzhiyun 	/* first new observed key (star, favorites) is 0x1311 */
1902*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_STAR = 69,
1903*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1904*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_CALCULATOR,
1905*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1906*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_KEYBOARD,
1907*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1908*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1909*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1910*4882a593Smuzhiyun 	TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 	/* Hotkey keymap size */
1913*4882a593Smuzhiyun 	TPACPI_HOTKEY_MAP_LEN
1914*4882a593Smuzhiyun };
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun enum {	/* Keys/events available through NVRAM polling */
1917*4882a593Smuzhiyun 	TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1918*4882a593Smuzhiyun 	TPACPI_HKEY_NVRAM_GOOD_MASK  = 0x00fb8000U,
1919*4882a593Smuzhiyun };
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun enum {	/* Positions of some of the keys in hotkey masks */
1922*4882a593Smuzhiyun 	TP_ACPI_HKEY_DISPSWTCH_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1923*4882a593Smuzhiyun 	TP_ACPI_HKEY_DISPXPAND_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1924*4882a593Smuzhiyun 	TP_ACPI_HKEY_HIBERNATE_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1925*4882a593Smuzhiyun 	TP_ACPI_HKEY_BRGHTUP_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1926*4882a593Smuzhiyun 	TP_ACPI_HKEY_BRGHTDWN_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1927*4882a593Smuzhiyun 	TP_ACPI_HKEY_KBD_LIGHT_MASK	= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1928*4882a593Smuzhiyun 	TP_ACPI_HKEY_ZOOM_MASK		= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1929*4882a593Smuzhiyun 	TP_ACPI_HKEY_VOLUP_MASK		= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1930*4882a593Smuzhiyun 	TP_ACPI_HKEY_VOLDWN_MASK	= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1931*4882a593Smuzhiyun 	TP_ACPI_HKEY_MUTE_MASK		= 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1932*4882a593Smuzhiyun 	TP_ACPI_HKEY_THINKPAD_MASK	= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1933*4882a593Smuzhiyun };
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun enum {	/* NVRAM to ACPI HKEY group map */
1936*4882a593Smuzhiyun 	TP_NVRAM_HKEY_GROUP_HK2		= TP_ACPI_HKEY_THINKPAD_MASK |
1937*4882a593Smuzhiyun 					  TP_ACPI_HKEY_ZOOM_MASK |
1938*4882a593Smuzhiyun 					  TP_ACPI_HKEY_DISPSWTCH_MASK |
1939*4882a593Smuzhiyun 					  TP_ACPI_HKEY_HIBERNATE_MASK,
1940*4882a593Smuzhiyun 	TP_NVRAM_HKEY_GROUP_BRIGHTNESS	= TP_ACPI_HKEY_BRGHTUP_MASK |
1941*4882a593Smuzhiyun 					  TP_ACPI_HKEY_BRGHTDWN_MASK,
1942*4882a593Smuzhiyun 	TP_NVRAM_HKEY_GROUP_VOLUME	= TP_ACPI_HKEY_VOLUP_MASK |
1943*4882a593Smuzhiyun 					  TP_ACPI_HKEY_VOLDWN_MASK |
1944*4882a593Smuzhiyun 					  TP_ACPI_HKEY_MUTE_MASK,
1945*4882a593Smuzhiyun };
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1948*4882a593Smuzhiyun struct tp_nvram_state {
1949*4882a593Smuzhiyun        u16 thinkpad_toggle:1;
1950*4882a593Smuzhiyun        u16 zoom_toggle:1;
1951*4882a593Smuzhiyun        u16 display_toggle:1;
1952*4882a593Smuzhiyun        u16 thinklight_toggle:1;
1953*4882a593Smuzhiyun        u16 hibernate_toggle:1;
1954*4882a593Smuzhiyun        u16 displayexp_toggle:1;
1955*4882a593Smuzhiyun        u16 display_state:1;
1956*4882a593Smuzhiyun        u16 brightness_toggle:1;
1957*4882a593Smuzhiyun        u16 volume_toggle:1;
1958*4882a593Smuzhiyun        u16 mute:1;
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun        u8 brightness_level;
1961*4882a593Smuzhiyun        u8 volume_level;
1962*4882a593Smuzhiyun };
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun /* kthread for the hotkey poller */
1965*4882a593Smuzhiyun static struct task_struct *tpacpi_hotkey_task;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun /*
1968*4882a593Smuzhiyun  * Acquire mutex to write poller control variables as an
1969*4882a593Smuzhiyun  * atomic block.
1970*4882a593Smuzhiyun  *
1971*4882a593Smuzhiyun  * Increment hotkey_config_change when changing them if you
1972*4882a593Smuzhiyun  * want the kthread to forget old state.
1973*4882a593Smuzhiyun  *
1974*4882a593Smuzhiyun  * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1975*4882a593Smuzhiyun  */
1976*4882a593Smuzhiyun static struct mutex hotkey_thread_data_mutex;
1977*4882a593Smuzhiyun static unsigned int hotkey_config_change;
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun /*
1980*4882a593Smuzhiyun  * hotkey poller control variables
1981*4882a593Smuzhiyun  *
1982*4882a593Smuzhiyun  * Must be atomic or readers will also need to acquire mutex
1983*4882a593Smuzhiyun  *
1984*4882a593Smuzhiyun  * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1985*4882a593Smuzhiyun  * should be used only when the changes need to be taken as
1986*4882a593Smuzhiyun  * a block, OR when one needs to force the kthread to forget
1987*4882a593Smuzhiyun  * old state.
1988*4882a593Smuzhiyun  */
1989*4882a593Smuzhiyun static u32 hotkey_source_mask;		/* bit mask 0=ACPI,1=NVRAM */
1990*4882a593Smuzhiyun static unsigned int hotkey_poll_freq = 10; /* Hz */
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun #define HOTKEY_CONFIG_CRITICAL_START \
1993*4882a593Smuzhiyun 	do { \
1994*4882a593Smuzhiyun 		mutex_lock(&hotkey_thread_data_mutex); \
1995*4882a593Smuzhiyun 		hotkey_config_change++; \
1996*4882a593Smuzhiyun 	} while (0);
1997*4882a593Smuzhiyun #define HOTKEY_CONFIG_CRITICAL_END \
1998*4882a593Smuzhiyun 	mutex_unlock(&hotkey_thread_data_mutex);
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun #define hotkey_source_mask 0U
2003*4882a593Smuzhiyun #define HOTKEY_CONFIG_CRITICAL_START
2004*4882a593Smuzhiyun #define HOTKEY_CONFIG_CRITICAL_END
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun static struct mutex hotkey_mutex;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun static enum {	/* Reasons for waking up */
2011*4882a593Smuzhiyun 	TP_ACPI_WAKEUP_NONE = 0,	/* None or unknown */
2012*4882a593Smuzhiyun 	TP_ACPI_WAKEUP_BAYEJ,		/* Bay ejection request */
2013*4882a593Smuzhiyun 	TP_ACPI_WAKEUP_UNDOCK,		/* Undock request */
2014*4882a593Smuzhiyun } hotkey_wakeup_reason;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun static int hotkey_autosleep_ack;
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun static u32 hotkey_orig_mask;		/* events the BIOS had enabled */
2019*4882a593Smuzhiyun static u32 hotkey_all_mask;		/* all events supported in fw */
2020*4882a593Smuzhiyun static u32 hotkey_adaptive_all_mask;	/* all adaptive events supported in fw */
2021*4882a593Smuzhiyun static u32 hotkey_reserved_mask;	/* events better left disabled */
2022*4882a593Smuzhiyun static u32 hotkey_driver_mask;		/* events needed by the driver */
2023*4882a593Smuzhiyun static u32 hotkey_user_mask;		/* events visible to userspace */
2024*4882a593Smuzhiyun static u32 hotkey_acpi_mask;		/* events enabled in firmware */
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun static u16 *hotkey_keycode_map;
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun static struct attribute_set *hotkey_dev_attributes;
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun static void tpacpi_driver_event(const unsigned int hkey_event);
2031*4882a593Smuzhiyun static void hotkey_driver_event(const unsigned int scancode);
2032*4882a593Smuzhiyun static void hotkey_poll_setup(const bool may_warn);
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun /* HKEY.MHKG() return bits */
2035*4882a593Smuzhiyun #define TP_HOTKEY_TABLET_MASK (1 << 3)
2036*4882a593Smuzhiyun enum {
2037*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_INVALID	= 0,
2038*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_UNKNOWN	= 1 << 0,
2039*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_LAPTOP	= 1 << 1,
2040*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_TABLET	= 1 << 2,
2041*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_FLAT		= 1 << 3,
2042*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_STAND	= 1 << 4,
2043*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_TENT		= 1 << 5,
2044*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_STAND_TENT	= 1 << 6,
2045*4882a593Smuzhiyun };
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun enum {
2048*4882a593Smuzhiyun 	/* The following modes are considered tablet mode for the purpose of
2049*4882a593Smuzhiyun 	 * reporting the status to userspace. i.e. in all these modes it makes
2050*4882a593Smuzhiyun 	 * sense to disable the laptop input devices such as touchpad and
2051*4882a593Smuzhiyun 	 * keyboard.
2052*4882a593Smuzhiyun 	 */
2053*4882a593Smuzhiyun 	TP_ACPI_MULTI_MODE_TABLET_LIKE	= TP_ACPI_MULTI_MODE_TABLET |
2054*4882a593Smuzhiyun 					  TP_ACPI_MULTI_MODE_STAND |
2055*4882a593Smuzhiyun 					  TP_ACPI_MULTI_MODE_TENT |
2056*4882a593Smuzhiyun 					  TP_ACPI_MULTI_MODE_STAND_TENT,
2057*4882a593Smuzhiyun };
2058*4882a593Smuzhiyun 
hotkey_get_wlsw(void)2059*4882a593Smuzhiyun static int hotkey_get_wlsw(void)
2060*4882a593Smuzhiyun {
2061*4882a593Smuzhiyun 	int status;
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	if (!tp_features.hotkey_wlsw)
2064*4882a593Smuzhiyun 		return -ENODEV;
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2067*4882a593Smuzhiyun 	if (dbg_wlswemul)
2068*4882a593Smuzhiyun 		return (tpacpi_wlsw_emulstate) ?
2069*4882a593Smuzhiyun 				TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2070*4882a593Smuzhiyun #endif
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2073*4882a593Smuzhiyun 		return -EIO;
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun 	return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2076*4882a593Smuzhiyun }
2077*4882a593Smuzhiyun 
hotkey_gmms_get_tablet_mode(int s,int * has_tablet_mode)2078*4882a593Smuzhiyun static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2079*4882a593Smuzhiyun {
2080*4882a593Smuzhiyun 	int type = (s >> 16) & 0xffff;
2081*4882a593Smuzhiyun 	int value = s & 0xffff;
2082*4882a593Smuzhiyun 	int mode = TP_ACPI_MULTI_MODE_INVALID;
2083*4882a593Smuzhiyun 	int valid_modes = 0;
2084*4882a593Smuzhiyun 
2085*4882a593Smuzhiyun 	if (has_tablet_mode)
2086*4882a593Smuzhiyun 		*has_tablet_mode = 0;
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	switch (type) {
2089*4882a593Smuzhiyun 	case 1:
2090*4882a593Smuzhiyun 		valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2091*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_TABLET |
2092*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_STAND_TENT;
2093*4882a593Smuzhiyun 		break;
2094*4882a593Smuzhiyun 	case 2:
2095*4882a593Smuzhiyun 		valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2096*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_FLAT |
2097*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_TABLET |
2098*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_STAND |
2099*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_TENT;
2100*4882a593Smuzhiyun 		break;
2101*4882a593Smuzhiyun 	case 3:
2102*4882a593Smuzhiyun 		valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2103*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_FLAT;
2104*4882a593Smuzhiyun 		break;
2105*4882a593Smuzhiyun 	case 4:
2106*4882a593Smuzhiyun 	case 5:
2107*4882a593Smuzhiyun 		/* In mode 4, FLAT is not specified as a valid mode. However,
2108*4882a593Smuzhiyun 		 * it can be seen at least on the X1 Yoga 2nd Generation.
2109*4882a593Smuzhiyun 		 */
2110*4882a593Smuzhiyun 		valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2111*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_FLAT |
2112*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_TABLET |
2113*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_STAND |
2114*4882a593Smuzhiyun 			      TP_ACPI_MULTI_MODE_TENT;
2115*4882a593Smuzhiyun 		break;
2116*4882a593Smuzhiyun 	default:
2117*4882a593Smuzhiyun 		pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2118*4882a593Smuzhiyun 		       type, value, TPACPI_MAIL);
2119*4882a593Smuzhiyun 		return 0;
2120*4882a593Smuzhiyun 	}
2121*4882a593Smuzhiyun 
2122*4882a593Smuzhiyun 	if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2123*4882a593Smuzhiyun 		*has_tablet_mode = 1;
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	switch (value) {
2126*4882a593Smuzhiyun 	case 1:
2127*4882a593Smuzhiyun 		mode = TP_ACPI_MULTI_MODE_LAPTOP;
2128*4882a593Smuzhiyun 		break;
2129*4882a593Smuzhiyun 	case 2:
2130*4882a593Smuzhiyun 		mode = TP_ACPI_MULTI_MODE_FLAT;
2131*4882a593Smuzhiyun 		break;
2132*4882a593Smuzhiyun 	case 3:
2133*4882a593Smuzhiyun 		mode = TP_ACPI_MULTI_MODE_TABLET;
2134*4882a593Smuzhiyun 		break;
2135*4882a593Smuzhiyun 	case 4:
2136*4882a593Smuzhiyun 		if (type == 1)
2137*4882a593Smuzhiyun 			mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2138*4882a593Smuzhiyun 		else
2139*4882a593Smuzhiyun 			mode = TP_ACPI_MULTI_MODE_STAND;
2140*4882a593Smuzhiyun 		break;
2141*4882a593Smuzhiyun 	case 5:
2142*4882a593Smuzhiyun 		mode = TP_ACPI_MULTI_MODE_TENT;
2143*4882a593Smuzhiyun 		break;
2144*4882a593Smuzhiyun 	default:
2145*4882a593Smuzhiyun 		if (type == 5 && value == 0xffff) {
2146*4882a593Smuzhiyun 			pr_warn("Multi mode status is undetected, assuming laptop\n");
2147*4882a593Smuzhiyun 			return 0;
2148*4882a593Smuzhiyun 		}
2149*4882a593Smuzhiyun 	}
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun 	if (!(mode & valid_modes)) {
2152*4882a593Smuzhiyun 		pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2153*4882a593Smuzhiyun 		       value, type, TPACPI_MAIL);
2154*4882a593Smuzhiyun 		return 0;
2155*4882a593Smuzhiyun 	}
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2158*4882a593Smuzhiyun }
2159*4882a593Smuzhiyun 
hotkey_get_tablet_mode(int * status)2160*4882a593Smuzhiyun static int hotkey_get_tablet_mode(int *status)
2161*4882a593Smuzhiyun {
2162*4882a593Smuzhiyun 	int s;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	switch (tp_features.hotkey_tablet) {
2165*4882a593Smuzhiyun 	case TP_HOTKEY_TABLET_USES_MHKG:
2166*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2167*4882a593Smuzhiyun 			return -EIO;
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 		*status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2170*4882a593Smuzhiyun 		break;
2171*4882a593Smuzhiyun 	case TP_HOTKEY_TABLET_USES_GMMS:
2172*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2173*4882a593Smuzhiyun 			return -EIO;
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun 		*status = hotkey_gmms_get_tablet_mode(s, NULL);
2176*4882a593Smuzhiyun 		break;
2177*4882a593Smuzhiyun 	default:
2178*4882a593Smuzhiyun 		break;
2179*4882a593Smuzhiyun 	}
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun 	return 0;
2182*4882a593Smuzhiyun }
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun /*
2185*4882a593Smuzhiyun  * Reads current event mask from firmware, and updates
2186*4882a593Smuzhiyun  * hotkey_acpi_mask accordingly.  Also resets any bits
2187*4882a593Smuzhiyun  * from hotkey_user_mask that are unavailable to be
2188*4882a593Smuzhiyun  * delivered (shadow requirement of the userspace ABI).
2189*4882a593Smuzhiyun  *
2190*4882a593Smuzhiyun  * Call with hotkey_mutex held
2191*4882a593Smuzhiyun  */
hotkey_mask_get(void)2192*4882a593Smuzhiyun static int hotkey_mask_get(void)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun 	if (tp_features.hotkey_mask) {
2195*4882a593Smuzhiyun 		u32 m = 0;
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2198*4882a593Smuzhiyun 			return -EIO;
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun 		hotkey_acpi_mask = m;
2201*4882a593Smuzhiyun 	} else {
2202*4882a593Smuzhiyun 		/* no mask support doesn't mean no event support... */
2203*4882a593Smuzhiyun 		hotkey_acpi_mask = hotkey_all_mask;
2204*4882a593Smuzhiyun 	}
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 	/* sync userspace-visible mask */
2207*4882a593Smuzhiyun 	hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	return 0;
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun 
hotkey_mask_warn_incomplete_mask(void)2212*4882a593Smuzhiyun static void hotkey_mask_warn_incomplete_mask(void)
2213*4882a593Smuzhiyun {
2214*4882a593Smuzhiyun 	/* log only what the user can fix... */
2215*4882a593Smuzhiyun 	const u32 wantedmask = hotkey_driver_mask &
2216*4882a593Smuzhiyun 		~(hotkey_acpi_mask | hotkey_source_mask) &
2217*4882a593Smuzhiyun 		(hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	if (wantedmask)
2220*4882a593Smuzhiyun 		pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2221*4882a593Smuzhiyun }
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun /*
2224*4882a593Smuzhiyun  * Set the firmware mask when supported
2225*4882a593Smuzhiyun  *
2226*4882a593Smuzhiyun  * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2227*4882a593Smuzhiyun  *
2228*4882a593Smuzhiyun  * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2229*4882a593Smuzhiyun  *
2230*4882a593Smuzhiyun  * Call with hotkey_mutex held
2231*4882a593Smuzhiyun  */
hotkey_mask_set(u32 mask)2232*4882a593Smuzhiyun static int hotkey_mask_set(u32 mask)
2233*4882a593Smuzhiyun {
2234*4882a593Smuzhiyun 	int i;
2235*4882a593Smuzhiyun 	int rc = 0;
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 	const u32 fwmask = mask & ~hotkey_source_mask;
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 	if (tp_features.hotkey_mask) {
2240*4882a593Smuzhiyun 		for (i = 0; i < 32; i++) {
2241*4882a593Smuzhiyun 			if (!acpi_evalf(hkey_handle,
2242*4882a593Smuzhiyun 					NULL, "MHKM", "vdd", i + 1,
2243*4882a593Smuzhiyun 					!!(mask & (1 << i)))) {
2244*4882a593Smuzhiyun 				rc = -EIO;
2245*4882a593Smuzhiyun 				break;
2246*4882a593Smuzhiyun 			}
2247*4882a593Smuzhiyun 		}
2248*4882a593Smuzhiyun 	}
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 	/*
2251*4882a593Smuzhiyun 	 * We *must* make an inconditional call to hotkey_mask_get to
2252*4882a593Smuzhiyun 	 * refresh hotkey_acpi_mask and update hotkey_user_mask
2253*4882a593Smuzhiyun 	 *
2254*4882a593Smuzhiyun 	 * Take the opportunity to also log when we cannot _enable_
2255*4882a593Smuzhiyun 	 * a given event.
2256*4882a593Smuzhiyun 	 */
2257*4882a593Smuzhiyun 	if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2258*4882a593Smuzhiyun 		pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2259*4882a593Smuzhiyun 			  fwmask, hotkey_acpi_mask);
2260*4882a593Smuzhiyun 	}
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2263*4882a593Smuzhiyun 		hotkey_mask_warn_incomplete_mask();
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	return rc;
2266*4882a593Smuzhiyun }
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun /*
2269*4882a593Smuzhiyun  * Sets hotkey_user_mask and tries to set the firmware mask
2270*4882a593Smuzhiyun  *
2271*4882a593Smuzhiyun  * Call with hotkey_mutex held
2272*4882a593Smuzhiyun  */
hotkey_user_mask_set(const u32 mask)2273*4882a593Smuzhiyun static int hotkey_user_mask_set(const u32 mask)
2274*4882a593Smuzhiyun {
2275*4882a593Smuzhiyun 	int rc;
2276*4882a593Smuzhiyun 
2277*4882a593Smuzhiyun 	/* Give people a chance to notice they are doing something that
2278*4882a593Smuzhiyun 	 * is bound to go boom on their users sooner or later */
2279*4882a593Smuzhiyun 	if (!tp_warned.hotkey_mask_ff &&
2280*4882a593Smuzhiyun 	    (mask == 0xffff || mask == 0xffffff ||
2281*4882a593Smuzhiyun 	     mask == 0xffffffff)) {
2282*4882a593Smuzhiyun 		tp_warned.hotkey_mask_ff = 1;
2283*4882a593Smuzhiyun 		pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2284*4882a593Smuzhiyun 			  mask);
2285*4882a593Smuzhiyun 		pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2286*4882a593Smuzhiyun 	}
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 	/* Try to enable what the user asked for, plus whatever we need.
2289*4882a593Smuzhiyun 	 * this syncs everything but won't enable bits in hotkey_user_mask */
2290*4882a593Smuzhiyun 	rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	/* Enable the available bits in hotkey_user_mask */
2293*4882a593Smuzhiyun 	hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	return rc;
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun /*
2299*4882a593Smuzhiyun  * Sets the driver hotkey mask.
2300*4882a593Smuzhiyun  *
2301*4882a593Smuzhiyun  * Can be called even if the hotkey subdriver is inactive
2302*4882a593Smuzhiyun  */
tpacpi_hotkey_driver_mask_set(const u32 mask)2303*4882a593Smuzhiyun static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2304*4882a593Smuzhiyun {
2305*4882a593Smuzhiyun 	int rc;
2306*4882a593Smuzhiyun 
2307*4882a593Smuzhiyun 	/* Do the right thing if hotkey_init has not been called yet */
2308*4882a593Smuzhiyun 	if (!tp_features.hotkey) {
2309*4882a593Smuzhiyun 		hotkey_driver_mask = mask;
2310*4882a593Smuzhiyun 		return 0;
2311*4882a593Smuzhiyun 	}
2312*4882a593Smuzhiyun 
2313*4882a593Smuzhiyun 	mutex_lock(&hotkey_mutex);
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun 	HOTKEY_CONFIG_CRITICAL_START
2316*4882a593Smuzhiyun 	hotkey_driver_mask = mask;
2317*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2318*4882a593Smuzhiyun 	hotkey_source_mask |= (mask & ~hotkey_all_mask);
2319*4882a593Smuzhiyun #endif
2320*4882a593Smuzhiyun 	HOTKEY_CONFIG_CRITICAL_END
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2323*4882a593Smuzhiyun 							~hotkey_source_mask);
2324*4882a593Smuzhiyun 	hotkey_poll_setup(true);
2325*4882a593Smuzhiyun 
2326*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
2327*4882a593Smuzhiyun 
2328*4882a593Smuzhiyun 	return rc;
2329*4882a593Smuzhiyun }
2330*4882a593Smuzhiyun 
hotkey_status_get(int * status)2331*4882a593Smuzhiyun static int hotkey_status_get(int *status)
2332*4882a593Smuzhiyun {
2333*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2334*4882a593Smuzhiyun 		return -EIO;
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun 	return 0;
2337*4882a593Smuzhiyun }
2338*4882a593Smuzhiyun 
hotkey_status_set(bool enable)2339*4882a593Smuzhiyun static int hotkey_status_set(bool enable)
2340*4882a593Smuzhiyun {
2341*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2342*4882a593Smuzhiyun 		return -EIO;
2343*4882a593Smuzhiyun 
2344*4882a593Smuzhiyun 	return 0;
2345*4882a593Smuzhiyun }
2346*4882a593Smuzhiyun 
tpacpi_input_send_tabletsw(void)2347*4882a593Smuzhiyun static void tpacpi_input_send_tabletsw(void)
2348*4882a593Smuzhiyun {
2349*4882a593Smuzhiyun 	int state;
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	if (tp_features.hotkey_tablet &&
2352*4882a593Smuzhiyun 	    !hotkey_get_tablet_mode(&state)) {
2353*4882a593Smuzhiyun 		mutex_lock(&tpacpi_inputdev_send_mutex);
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 		input_report_switch(tpacpi_inputdev,
2356*4882a593Smuzhiyun 				    SW_TABLET_MODE, !!state);
2357*4882a593Smuzhiyun 		input_sync(tpacpi_inputdev);
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 		mutex_unlock(&tpacpi_inputdev_send_mutex);
2360*4882a593Smuzhiyun 	}
2361*4882a593Smuzhiyun }
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun /* Do NOT call without validating scancode first */
tpacpi_input_send_key(const unsigned int scancode)2364*4882a593Smuzhiyun static void tpacpi_input_send_key(const unsigned int scancode)
2365*4882a593Smuzhiyun {
2366*4882a593Smuzhiyun 	const unsigned int keycode = hotkey_keycode_map[scancode];
2367*4882a593Smuzhiyun 
2368*4882a593Smuzhiyun 	if (keycode != KEY_RESERVED) {
2369*4882a593Smuzhiyun 		mutex_lock(&tpacpi_inputdev_send_mutex);
2370*4882a593Smuzhiyun 
2371*4882a593Smuzhiyun 		input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2372*4882a593Smuzhiyun 		input_report_key(tpacpi_inputdev, keycode, 1);
2373*4882a593Smuzhiyun 		input_sync(tpacpi_inputdev);
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun 		input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2376*4882a593Smuzhiyun 		input_report_key(tpacpi_inputdev, keycode, 0);
2377*4882a593Smuzhiyun 		input_sync(tpacpi_inputdev);
2378*4882a593Smuzhiyun 
2379*4882a593Smuzhiyun 		mutex_unlock(&tpacpi_inputdev_send_mutex);
2380*4882a593Smuzhiyun 	}
2381*4882a593Smuzhiyun }
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun /* Do NOT call without validating scancode first */
tpacpi_input_send_key_masked(const unsigned int scancode)2384*4882a593Smuzhiyun static void tpacpi_input_send_key_masked(const unsigned int scancode)
2385*4882a593Smuzhiyun {
2386*4882a593Smuzhiyun 	hotkey_driver_event(scancode);
2387*4882a593Smuzhiyun 	if (hotkey_user_mask & (1 << scancode))
2388*4882a593Smuzhiyun 		tpacpi_input_send_key(scancode);
2389*4882a593Smuzhiyun }
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2392*4882a593Smuzhiyun static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun /* Do NOT call without validating scancode first */
tpacpi_hotkey_send_key(unsigned int scancode)2395*4882a593Smuzhiyun static void tpacpi_hotkey_send_key(unsigned int scancode)
2396*4882a593Smuzhiyun {
2397*4882a593Smuzhiyun 	tpacpi_input_send_key_masked(scancode);
2398*4882a593Smuzhiyun }
2399*4882a593Smuzhiyun 
hotkey_read_nvram(struct tp_nvram_state * n,const u32 m)2400*4882a593Smuzhiyun static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2401*4882a593Smuzhiyun {
2402*4882a593Smuzhiyun 	u8 d;
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2405*4882a593Smuzhiyun 		d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2406*4882a593Smuzhiyun 		n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2407*4882a593Smuzhiyun 		n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2408*4882a593Smuzhiyun 		n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2409*4882a593Smuzhiyun 		n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2410*4882a593Smuzhiyun 	}
2411*4882a593Smuzhiyun 	if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2412*4882a593Smuzhiyun 		d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2413*4882a593Smuzhiyun 		n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2414*4882a593Smuzhiyun 	}
2415*4882a593Smuzhiyun 	if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2416*4882a593Smuzhiyun 		d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2417*4882a593Smuzhiyun 		n->displayexp_toggle =
2418*4882a593Smuzhiyun 				!!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2419*4882a593Smuzhiyun 	}
2420*4882a593Smuzhiyun 	if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2421*4882a593Smuzhiyun 		d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2422*4882a593Smuzhiyun 		n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2423*4882a593Smuzhiyun 				>> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2424*4882a593Smuzhiyun 		n->brightness_toggle =
2425*4882a593Smuzhiyun 				!!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2426*4882a593Smuzhiyun 	}
2427*4882a593Smuzhiyun 	if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2428*4882a593Smuzhiyun 		d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2429*4882a593Smuzhiyun 		n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2430*4882a593Smuzhiyun 				>> TP_NVRAM_POS_LEVEL_VOLUME;
2431*4882a593Smuzhiyun 		n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2432*4882a593Smuzhiyun 		n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2433*4882a593Smuzhiyun 	}
2434*4882a593Smuzhiyun }
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun #define TPACPI_COMPARE_KEY(__scancode, __member) \
2437*4882a593Smuzhiyun do { \
2438*4882a593Smuzhiyun 	if ((event_mask & (1 << __scancode)) && \
2439*4882a593Smuzhiyun 	    oldn->__member != newn->__member) \
2440*4882a593Smuzhiyun 		tpacpi_hotkey_send_key(__scancode); \
2441*4882a593Smuzhiyun } while (0)
2442*4882a593Smuzhiyun 
2443*4882a593Smuzhiyun #define TPACPI_MAY_SEND_KEY(__scancode) \
2444*4882a593Smuzhiyun do { \
2445*4882a593Smuzhiyun 	if (event_mask & (1 << __scancode)) \
2446*4882a593Smuzhiyun 		tpacpi_hotkey_send_key(__scancode); \
2447*4882a593Smuzhiyun } while (0)
2448*4882a593Smuzhiyun 
issue_volchange(const unsigned int oldvol,const unsigned int newvol,const u32 event_mask)2449*4882a593Smuzhiyun static void issue_volchange(const unsigned int oldvol,
2450*4882a593Smuzhiyun 			    const unsigned int newvol,
2451*4882a593Smuzhiyun 			    const u32 event_mask)
2452*4882a593Smuzhiyun {
2453*4882a593Smuzhiyun 	unsigned int i = oldvol;
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun 	while (i > newvol) {
2456*4882a593Smuzhiyun 		TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2457*4882a593Smuzhiyun 		i--;
2458*4882a593Smuzhiyun 	}
2459*4882a593Smuzhiyun 	while (i < newvol) {
2460*4882a593Smuzhiyun 		TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2461*4882a593Smuzhiyun 		i++;
2462*4882a593Smuzhiyun 	}
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun 
issue_brightnesschange(const unsigned int oldbrt,const unsigned int newbrt,const u32 event_mask)2465*4882a593Smuzhiyun static void issue_brightnesschange(const unsigned int oldbrt,
2466*4882a593Smuzhiyun 				   const unsigned int newbrt,
2467*4882a593Smuzhiyun 				   const u32 event_mask)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun 	unsigned int i = oldbrt;
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 	while (i > newbrt) {
2472*4882a593Smuzhiyun 		TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2473*4882a593Smuzhiyun 		i--;
2474*4882a593Smuzhiyun 	}
2475*4882a593Smuzhiyun 	while (i < newbrt) {
2476*4882a593Smuzhiyun 		TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2477*4882a593Smuzhiyun 		i++;
2478*4882a593Smuzhiyun 	}
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun 
hotkey_compare_and_issue_event(struct tp_nvram_state * oldn,struct tp_nvram_state * newn,const u32 event_mask)2481*4882a593Smuzhiyun static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2482*4882a593Smuzhiyun 					   struct tp_nvram_state *newn,
2483*4882a593Smuzhiyun 					   const u32 event_mask)
2484*4882a593Smuzhiyun {
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2487*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2488*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2489*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2490*4882a593Smuzhiyun 
2491*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun 	TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2494*4882a593Smuzhiyun 
2495*4882a593Smuzhiyun 	/*
2496*4882a593Smuzhiyun 	 * Handle volume
2497*4882a593Smuzhiyun 	 *
2498*4882a593Smuzhiyun 	 * This code is supposed to duplicate the IBM firmware behaviour:
2499*4882a593Smuzhiyun 	 * - Pressing MUTE issues mute hotkey message, even when already mute
2500*4882a593Smuzhiyun 	 * - Pressing Volume up/down issues volume up/down hotkey messages,
2501*4882a593Smuzhiyun 	 *   even when already at maximum or minimum volume
2502*4882a593Smuzhiyun 	 * - The act of unmuting issues volume up/down notification,
2503*4882a593Smuzhiyun 	 *   depending which key was used to unmute
2504*4882a593Smuzhiyun 	 *
2505*4882a593Smuzhiyun 	 * We are constrained to what the NVRAM can tell us, which is not much
2506*4882a593Smuzhiyun 	 * and certainly not enough if more than one volume hotkey was pressed
2507*4882a593Smuzhiyun 	 * since the last poll cycle.
2508*4882a593Smuzhiyun 	 *
2509*4882a593Smuzhiyun 	 * Just to make our life interesting, some newer Lenovo ThinkPads have
2510*4882a593Smuzhiyun 	 * bugs in the BIOS and may fail to update volume_toggle properly.
2511*4882a593Smuzhiyun 	 */
2512*4882a593Smuzhiyun 	if (newn->mute) {
2513*4882a593Smuzhiyun 		/* muted */
2514*4882a593Smuzhiyun 		if (!oldn->mute ||
2515*4882a593Smuzhiyun 		    oldn->volume_toggle != newn->volume_toggle ||
2516*4882a593Smuzhiyun 		    oldn->volume_level != newn->volume_level) {
2517*4882a593Smuzhiyun 			/* recently muted, or repeated mute keypress, or
2518*4882a593Smuzhiyun 			 * multiple presses ending in mute */
2519*4882a593Smuzhiyun 			issue_volchange(oldn->volume_level, newn->volume_level,
2520*4882a593Smuzhiyun 				event_mask);
2521*4882a593Smuzhiyun 			TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2522*4882a593Smuzhiyun 		}
2523*4882a593Smuzhiyun 	} else {
2524*4882a593Smuzhiyun 		/* unmute */
2525*4882a593Smuzhiyun 		if (oldn->mute) {
2526*4882a593Smuzhiyun 			/* recently unmuted, issue 'unmute' keypress */
2527*4882a593Smuzhiyun 			TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2528*4882a593Smuzhiyun 		}
2529*4882a593Smuzhiyun 		if (oldn->volume_level != newn->volume_level) {
2530*4882a593Smuzhiyun 			issue_volchange(oldn->volume_level, newn->volume_level,
2531*4882a593Smuzhiyun 				event_mask);
2532*4882a593Smuzhiyun 		} else if (oldn->volume_toggle != newn->volume_toggle) {
2533*4882a593Smuzhiyun 			/* repeated vol up/down keypress at end of scale ? */
2534*4882a593Smuzhiyun 			if (newn->volume_level == 0)
2535*4882a593Smuzhiyun 				TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2536*4882a593Smuzhiyun 			else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2537*4882a593Smuzhiyun 				TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2538*4882a593Smuzhiyun 		}
2539*4882a593Smuzhiyun 	}
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 	/* handle brightness */
2542*4882a593Smuzhiyun 	if (oldn->brightness_level != newn->brightness_level) {
2543*4882a593Smuzhiyun 		issue_brightnesschange(oldn->brightness_level,
2544*4882a593Smuzhiyun 				       newn->brightness_level, event_mask);
2545*4882a593Smuzhiyun 	} else if (oldn->brightness_toggle != newn->brightness_toggle) {
2546*4882a593Smuzhiyun 		/* repeated key presses that didn't change state */
2547*4882a593Smuzhiyun 		if (newn->brightness_level == 0)
2548*4882a593Smuzhiyun 			TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2549*4882a593Smuzhiyun 		else if (newn->brightness_level >= bright_maxlvl
2550*4882a593Smuzhiyun 				&& !tp_features.bright_unkfw)
2551*4882a593Smuzhiyun 			TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2552*4882a593Smuzhiyun 	}
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun #undef TPACPI_COMPARE_KEY
2555*4882a593Smuzhiyun #undef TPACPI_MAY_SEND_KEY
2556*4882a593Smuzhiyun }
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun /*
2559*4882a593Smuzhiyun  * Polling driver
2560*4882a593Smuzhiyun  *
2561*4882a593Smuzhiyun  * We track all events in hotkey_source_mask all the time, since
2562*4882a593Smuzhiyun  * most of them are edge-based.  We only issue those requested by
2563*4882a593Smuzhiyun  * hotkey_user_mask or hotkey_driver_mask, though.
2564*4882a593Smuzhiyun  */
hotkey_kthread(void * data)2565*4882a593Smuzhiyun static int hotkey_kthread(void *data)
2566*4882a593Smuzhiyun {
2567*4882a593Smuzhiyun 	struct tp_nvram_state s[2] = { 0 };
2568*4882a593Smuzhiyun 	u32 poll_mask, event_mask;
2569*4882a593Smuzhiyun 	unsigned int si, so;
2570*4882a593Smuzhiyun 	unsigned long t;
2571*4882a593Smuzhiyun 	unsigned int change_detector;
2572*4882a593Smuzhiyun 	unsigned int poll_freq;
2573*4882a593Smuzhiyun 	bool was_frozen;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2576*4882a593Smuzhiyun 		goto exit;
2577*4882a593Smuzhiyun 
2578*4882a593Smuzhiyun 	set_freezable();
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun 	so = 0;
2581*4882a593Smuzhiyun 	si = 1;
2582*4882a593Smuzhiyun 	t = 0;
2583*4882a593Smuzhiyun 
2584*4882a593Smuzhiyun 	/* Initial state for compares */
2585*4882a593Smuzhiyun 	mutex_lock(&hotkey_thread_data_mutex);
2586*4882a593Smuzhiyun 	change_detector = hotkey_config_change;
2587*4882a593Smuzhiyun 	poll_mask = hotkey_source_mask;
2588*4882a593Smuzhiyun 	event_mask = hotkey_source_mask &
2589*4882a593Smuzhiyun 			(hotkey_driver_mask | hotkey_user_mask);
2590*4882a593Smuzhiyun 	poll_freq = hotkey_poll_freq;
2591*4882a593Smuzhiyun 	mutex_unlock(&hotkey_thread_data_mutex);
2592*4882a593Smuzhiyun 	hotkey_read_nvram(&s[so], poll_mask);
2593*4882a593Smuzhiyun 
2594*4882a593Smuzhiyun 	while (!kthread_should_stop()) {
2595*4882a593Smuzhiyun 		if (t == 0) {
2596*4882a593Smuzhiyun 			if (likely(poll_freq))
2597*4882a593Smuzhiyun 				t = 1000/poll_freq;
2598*4882a593Smuzhiyun 			else
2599*4882a593Smuzhiyun 				t = 100;	/* should never happen... */
2600*4882a593Smuzhiyun 		}
2601*4882a593Smuzhiyun 		t = msleep_interruptible(t);
2602*4882a593Smuzhiyun 		if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2603*4882a593Smuzhiyun 			break;
2604*4882a593Smuzhiyun 
2605*4882a593Smuzhiyun 		if (t > 0 && !was_frozen)
2606*4882a593Smuzhiyun 			continue;
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 		mutex_lock(&hotkey_thread_data_mutex);
2609*4882a593Smuzhiyun 		if (was_frozen || hotkey_config_change != change_detector) {
2610*4882a593Smuzhiyun 			/* forget old state on thaw or config change */
2611*4882a593Smuzhiyun 			si = so;
2612*4882a593Smuzhiyun 			t = 0;
2613*4882a593Smuzhiyun 			change_detector = hotkey_config_change;
2614*4882a593Smuzhiyun 		}
2615*4882a593Smuzhiyun 		poll_mask = hotkey_source_mask;
2616*4882a593Smuzhiyun 		event_mask = hotkey_source_mask &
2617*4882a593Smuzhiyun 				(hotkey_driver_mask | hotkey_user_mask);
2618*4882a593Smuzhiyun 		poll_freq = hotkey_poll_freq;
2619*4882a593Smuzhiyun 		mutex_unlock(&hotkey_thread_data_mutex);
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun 		if (likely(poll_mask)) {
2622*4882a593Smuzhiyun 			hotkey_read_nvram(&s[si], poll_mask);
2623*4882a593Smuzhiyun 			if (likely(si != so)) {
2624*4882a593Smuzhiyun 				hotkey_compare_and_issue_event(&s[so], &s[si],
2625*4882a593Smuzhiyun 								event_mask);
2626*4882a593Smuzhiyun 			}
2627*4882a593Smuzhiyun 		}
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 		so = si;
2630*4882a593Smuzhiyun 		si ^= 1;
2631*4882a593Smuzhiyun 	}
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun exit:
2634*4882a593Smuzhiyun 	return 0;
2635*4882a593Smuzhiyun }
2636*4882a593Smuzhiyun 
2637*4882a593Smuzhiyun /* call with hotkey_mutex held */
hotkey_poll_stop_sync(void)2638*4882a593Smuzhiyun static void hotkey_poll_stop_sync(void)
2639*4882a593Smuzhiyun {
2640*4882a593Smuzhiyun 	if (tpacpi_hotkey_task) {
2641*4882a593Smuzhiyun 		kthread_stop(tpacpi_hotkey_task);
2642*4882a593Smuzhiyun 		tpacpi_hotkey_task = NULL;
2643*4882a593Smuzhiyun 	}
2644*4882a593Smuzhiyun }
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun /* call with hotkey_mutex held */
hotkey_poll_setup(const bool may_warn)2647*4882a593Smuzhiyun static void hotkey_poll_setup(const bool may_warn)
2648*4882a593Smuzhiyun {
2649*4882a593Smuzhiyun 	const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2650*4882a593Smuzhiyun 	const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun 	if (hotkey_poll_freq > 0 &&
2653*4882a593Smuzhiyun 	    (poll_driver_mask ||
2654*4882a593Smuzhiyun 	     (poll_user_mask && tpacpi_inputdev->users > 0))) {
2655*4882a593Smuzhiyun 		if (!tpacpi_hotkey_task) {
2656*4882a593Smuzhiyun 			tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2657*4882a593Smuzhiyun 					NULL, TPACPI_NVRAM_KTHREAD_NAME);
2658*4882a593Smuzhiyun 			if (IS_ERR(tpacpi_hotkey_task)) {
2659*4882a593Smuzhiyun 				tpacpi_hotkey_task = NULL;
2660*4882a593Smuzhiyun 				pr_err("could not create kernel thread for hotkey polling\n");
2661*4882a593Smuzhiyun 			}
2662*4882a593Smuzhiyun 		}
2663*4882a593Smuzhiyun 	} else {
2664*4882a593Smuzhiyun 		hotkey_poll_stop_sync();
2665*4882a593Smuzhiyun 		if (may_warn && (poll_driver_mask || poll_user_mask) &&
2666*4882a593Smuzhiyun 		    hotkey_poll_freq == 0) {
2667*4882a593Smuzhiyun 			pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2668*4882a593Smuzhiyun 				  poll_user_mask, poll_driver_mask);
2669*4882a593Smuzhiyun 		}
2670*4882a593Smuzhiyun 	}
2671*4882a593Smuzhiyun }
2672*4882a593Smuzhiyun 
hotkey_poll_setup_safe(const bool may_warn)2673*4882a593Smuzhiyun static void hotkey_poll_setup_safe(const bool may_warn)
2674*4882a593Smuzhiyun {
2675*4882a593Smuzhiyun 	mutex_lock(&hotkey_mutex);
2676*4882a593Smuzhiyun 	hotkey_poll_setup(may_warn);
2677*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
2678*4882a593Smuzhiyun }
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun /* call with hotkey_mutex held */
hotkey_poll_set_freq(unsigned int freq)2681*4882a593Smuzhiyun static void hotkey_poll_set_freq(unsigned int freq)
2682*4882a593Smuzhiyun {
2683*4882a593Smuzhiyun 	if (!freq)
2684*4882a593Smuzhiyun 		hotkey_poll_stop_sync();
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 	hotkey_poll_freq = freq;
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2690*4882a593Smuzhiyun 
hotkey_poll_setup(const bool __unused)2691*4882a593Smuzhiyun static void hotkey_poll_setup(const bool __unused)
2692*4882a593Smuzhiyun {
2693*4882a593Smuzhiyun }
2694*4882a593Smuzhiyun 
hotkey_poll_setup_safe(const bool __unused)2695*4882a593Smuzhiyun static void hotkey_poll_setup_safe(const bool __unused)
2696*4882a593Smuzhiyun {
2697*4882a593Smuzhiyun }
2698*4882a593Smuzhiyun 
2699*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2700*4882a593Smuzhiyun 
hotkey_inputdev_open(struct input_dev * dev)2701*4882a593Smuzhiyun static int hotkey_inputdev_open(struct input_dev *dev)
2702*4882a593Smuzhiyun {
2703*4882a593Smuzhiyun 	switch (tpacpi_lifecycle) {
2704*4882a593Smuzhiyun 	case TPACPI_LIFE_INIT:
2705*4882a593Smuzhiyun 	case TPACPI_LIFE_RUNNING:
2706*4882a593Smuzhiyun 		hotkey_poll_setup_safe(false);
2707*4882a593Smuzhiyun 		return 0;
2708*4882a593Smuzhiyun 	case TPACPI_LIFE_EXITING:
2709*4882a593Smuzhiyun 		return -EBUSY;
2710*4882a593Smuzhiyun 	}
2711*4882a593Smuzhiyun 
2712*4882a593Smuzhiyun 	/* Should only happen if tpacpi_lifecycle is corrupt */
2713*4882a593Smuzhiyun 	BUG();
2714*4882a593Smuzhiyun 	return -EBUSY;
2715*4882a593Smuzhiyun }
2716*4882a593Smuzhiyun 
hotkey_inputdev_close(struct input_dev * dev)2717*4882a593Smuzhiyun static void hotkey_inputdev_close(struct input_dev *dev)
2718*4882a593Smuzhiyun {
2719*4882a593Smuzhiyun 	/* disable hotkey polling when possible */
2720*4882a593Smuzhiyun 	if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2721*4882a593Smuzhiyun 	    !(hotkey_source_mask & hotkey_driver_mask))
2722*4882a593Smuzhiyun 		hotkey_poll_setup_safe(false);
2723*4882a593Smuzhiyun }
2724*4882a593Smuzhiyun 
2725*4882a593Smuzhiyun /* sysfs hotkey enable ------------------------------------------------- */
hotkey_enable_show(struct device * dev,struct device_attribute * attr,char * buf)2726*4882a593Smuzhiyun static ssize_t hotkey_enable_show(struct device *dev,
2727*4882a593Smuzhiyun 			   struct device_attribute *attr,
2728*4882a593Smuzhiyun 			   char *buf)
2729*4882a593Smuzhiyun {
2730*4882a593Smuzhiyun 	int res, status;
2731*4882a593Smuzhiyun 
2732*4882a593Smuzhiyun 	printk_deprecated_attribute("hotkey_enable",
2733*4882a593Smuzhiyun 			"Hotkey reporting is always enabled");
2734*4882a593Smuzhiyun 
2735*4882a593Smuzhiyun 	res = hotkey_status_get(&status);
2736*4882a593Smuzhiyun 	if (res)
2737*4882a593Smuzhiyun 		return res;
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", status);
2740*4882a593Smuzhiyun }
2741*4882a593Smuzhiyun 
hotkey_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2742*4882a593Smuzhiyun static ssize_t hotkey_enable_store(struct device *dev,
2743*4882a593Smuzhiyun 			    struct device_attribute *attr,
2744*4882a593Smuzhiyun 			    const char *buf, size_t count)
2745*4882a593Smuzhiyun {
2746*4882a593Smuzhiyun 	unsigned long t;
2747*4882a593Smuzhiyun 
2748*4882a593Smuzhiyun 	printk_deprecated_attribute("hotkey_enable",
2749*4882a593Smuzhiyun 			"Hotkeys can be disabled through hotkey_mask");
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun 	if (parse_strtoul(buf, 1, &t))
2752*4882a593Smuzhiyun 		return -EINVAL;
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun 	if (t == 0)
2755*4882a593Smuzhiyun 		return -EPERM;
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	return count;
2758*4882a593Smuzhiyun }
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun static DEVICE_ATTR_RW(hotkey_enable);
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun /* sysfs hotkey mask --------------------------------------------------- */
hotkey_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2763*4882a593Smuzhiyun static ssize_t hotkey_mask_show(struct device *dev,
2764*4882a593Smuzhiyun 			   struct device_attribute *attr,
2765*4882a593Smuzhiyun 			   char *buf)
2766*4882a593Smuzhiyun {
2767*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2768*4882a593Smuzhiyun }
2769*4882a593Smuzhiyun 
hotkey_mask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2770*4882a593Smuzhiyun static ssize_t hotkey_mask_store(struct device *dev,
2771*4882a593Smuzhiyun 			    struct device_attribute *attr,
2772*4882a593Smuzhiyun 			    const char *buf, size_t count)
2773*4882a593Smuzhiyun {
2774*4882a593Smuzhiyun 	unsigned long t;
2775*4882a593Smuzhiyun 	int res;
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 	if (parse_strtoul(buf, 0xffffffffUL, &t))
2778*4882a593Smuzhiyun 		return -EINVAL;
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun 	if (mutex_lock_killable(&hotkey_mutex))
2781*4882a593Smuzhiyun 		return -ERESTARTSYS;
2782*4882a593Smuzhiyun 
2783*4882a593Smuzhiyun 	res = hotkey_user_mask_set(t);
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2786*4882a593Smuzhiyun 	hotkey_poll_setup(true);
2787*4882a593Smuzhiyun #endif
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun 	tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2792*4882a593Smuzhiyun 
2793*4882a593Smuzhiyun 	return (res) ? res : count;
2794*4882a593Smuzhiyun }
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun static DEVICE_ATTR_RW(hotkey_mask);
2797*4882a593Smuzhiyun 
2798*4882a593Smuzhiyun /* sysfs hotkey bios_enabled ------------------------------------------- */
hotkey_bios_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)2799*4882a593Smuzhiyun static ssize_t hotkey_bios_enabled_show(struct device *dev,
2800*4882a593Smuzhiyun 			   struct device_attribute *attr,
2801*4882a593Smuzhiyun 			   char *buf)
2802*4882a593Smuzhiyun {
2803*4882a593Smuzhiyun 	return sprintf(buf, "0\n");
2804*4882a593Smuzhiyun }
2805*4882a593Smuzhiyun 
2806*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_bios_enabled);
2807*4882a593Smuzhiyun 
2808*4882a593Smuzhiyun /* sysfs hotkey bios_mask ---------------------------------------------- */
hotkey_bios_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2809*4882a593Smuzhiyun static ssize_t hotkey_bios_mask_show(struct device *dev,
2810*4882a593Smuzhiyun 			   struct device_attribute *attr,
2811*4882a593Smuzhiyun 			   char *buf)
2812*4882a593Smuzhiyun {
2813*4882a593Smuzhiyun 	printk_deprecated_attribute("hotkey_bios_mask",
2814*4882a593Smuzhiyun 			"This attribute is useless.");
2815*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_bios_mask);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun /* sysfs hotkey all_mask ----------------------------------------------- */
hotkey_all_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2821*4882a593Smuzhiyun static ssize_t hotkey_all_mask_show(struct device *dev,
2822*4882a593Smuzhiyun 			   struct device_attribute *attr,
2823*4882a593Smuzhiyun 			   char *buf)
2824*4882a593Smuzhiyun {
2825*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2826*4882a593Smuzhiyun 				hotkey_all_mask | hotkey_source_mask);
2827*4882a593Smuzhiyun }
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_all_mask);
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun /* sysfs hotkey all_mask ----------------------------------------------- */
hotkey_adaptive_all_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2832*4882a593Smuzhiyun static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2833*4882a593Smuzhiyun 			   struct device_attribute *attr,
2834*4882a593Smuzhiyun 			   char *buf)
2835*4882a593Smuzhiyun {
2836*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2837*4882a593Smuzhiyun 			hotkey_adaptive_all_mask | hotkey_source_mask);
2838*4882a593Smuzhiyun }
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun /* sysfs hotkey recommended_mask --------------------------------------- */
hotkey_recommended_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2843*4882a593Smuzhiyun static ssize_t hotkey_recommended_mask_show(struct device *dev,
2844*4882a593Smuzhiyun 					    struct device_attribute *attr,
2845*4882a593Smuzhiyun 					    char *buf)
2846*4882a593Smuzhiyun {
2847*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2848*4882a593Smuzhiyun 			(hotkey_all_mask | hotkey_source_mask)
2849*4882a593Smuzhiyun 			& ~hotkey_reserved_mask);
2850*4882a593Smuzhiyun }
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_recommended_mask);
2853*4882a593Smuzhiyun 
2854*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun /* sysfs hotkey hotkey_source_mask ------------------------------------- */
hotkey_source_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2857*4882a593Smuzhiyun static ssize_t hotkey_source_mask_show(struct device *dev,
2858*4882a593Smuzhiyun 			   struct device_attribute *attr,
2859*4882a593Smuzhiyun 			   char *buf)
2860*4882a593Smuzhiyun {
2861*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2862*4882a593Smuzhiyun }
2863*4882a593Smuzhiyun 
hotkey_source_mask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2864*4882a593Smuzhiyun static ssize_t hotkey_source_mask_store(struct device *dev,
2865*4882a593Smuzhiyun 			    struct device_attribute *attr,
2866*4882a593Smuzhiyun 			    const char *buf, size_t count)
2867*4882a593Smuzhiyun {
2868*4882a593Smuzhiyun 	unsigned long t;
2869*4882a593Smuzhiyun 	u32 r_ev;
2870*4882a593Smuzhiyun 	int rc;
2871*4882a593Smuzhiyun 
2872*4882a593Smuzhiyun 	if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2873*4882a593Smuzhiyun 		((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2874*4882a593Smuzhiyun 		return -EINVAL;
2875*4882a593Smuzhiyun 
2876*4882a593Smuzhiyun 	if (mutex_lock_killable(&hotkey_mutex))
2877*4882a593Smuzhiyun 		return -ERESTARTSYS;
2878*4882a593Smuzhiyun 
2879*4882a593Smuzhiyun 	HOTKEY_CONFIG_CRITICAL_START
2880*4882a593Smuzhiyun 	hotkey_source_mask = t;
2881*4882a593Smuzhiyun 	HOTKEY_CONFIG_CRITICAL_END
2882*4882a593Smuzhiyun 
2883*4882a593Smuzhiyun 	rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2884*4882a593Smuzhiyun 			~hotkey_source_mask);
2885*4882a593Smuzhiyun 	hotkey_poll_setup(true);
2886*4882a593Smuzhiyun 
2887*4882a593Smuzhiyun 	/* check if events needed by the driver got disabled */
2888*4882a593Smuzhiyun 	r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2889*4882a593Smuzhiyun 		& ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
2892*4882a593Smuzhiyun 
2893*4882a593Smuzhiyun 	if (rc < 0)
2894*4882a593Smuzhiyun 		pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	if (r_ev)
2897*4882a593Smuzhiyun 		pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2898*4882a593Smuzhiyun 			  r_ev);
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 	tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 	return (rc < 0) ? rc : count;
2903*4882a593Smuzhiyun }
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun static DEVICE_ATTR_RW(hotkey_source_mask);
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
hotkey_poll_freq_show(struct device * dev,struct device_attribute * attr,char * buf)2908*4882a593Smuzhiyun static ssize_t hotkey_poll_freq_show(struct device *dev,
2909*4882a593Smuzhiyun 			   struct device_attribute *attr,
2910*4882a593Smuzhiyun 			   char *buf)
2911*4882a593Smuzhiyun {
2912*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2913*4882a593Smuzhiyun }
2914*4882a593Smuzhiyun 
hotkey_poll_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2915*4882a593Smuzhiyun static ssize_t hotkey_poll_freq_store(struct device *dev,
2916*4882a593Smuzhiyun 			    struct device_attribute *attr,
2917*4882a593Smuzhiyun 			    const char *buf, size_t count)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun 	unsigned long t;
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	if (parse_strtoul(buf, 25, &t))
2922*4882a593Smuzhiyun 		return -EINVAL;
2923*4882a593Smuzhiyun 
2924*4882a593Smuzhiyun 	if (mutex_lock_killable(&hotkey_mutex))
2925*4882a593Smuzhiyun 		return -ERESTARTSYS;
2926*4882a593Smuzhiyun 
2927*4882a593Smuzhiyun 	hotkey_poll_set_freq(t);
2928*4882a593Smuzhiyun 	hotkey_poll_setup(true);
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
2931*4882a593Smuzhiyun 
2932*4882a593Smuzhiyun 	tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 	return count;
2935*4882a593Smuzhiyun }
2936*4882a593Smuzhiyun 
2937*4882a593Smuzhiyun static DEVICE_ATTR_RW(hotkey_poll_freq);
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2940*4882a593Smuzhiyun 
2941*4882a593Smuzhiyun /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
hotkey_radio_sw_show(struct device * dev,struct device_attribute * attr,char * buf)2942*4882a593Smuzhiyun static ssize_t hotkey_radio_sw_show(struct device *dev,
2943*4882a593Smuzhiyun 			   struct device_attribute *attr,
2944*4882a593Smuzhiyun 			   char *buf)
2945*4882a593Smuzhiyun {
2946*4882a593Smuzhiyun 	int res;
2947*4882a593Smuzhiyun 	res = hotkey_get_wlsw();
2948*4882a593Smuzhiyun 	if (res < 0)
2949*4882a593Smuzhiyun 		return res;
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun 	/* Opportunistic update */
2952*4882a593Smuzhiyun 	tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n",
2955*4882a593Smuzhiyun 			(res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2956*4882a593Smuzhiyun }
2957*4882a593Smuzhiyun 
2958*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_radio_sw);
2959*4882a593Smuzhiyun 
hotkey_radio_sw_notify_change(void)2960*4882a593Smuzhiyun static void hotkey_radio_sw_notify_change(void)
2961*4882a593Smuzhiyun {
2962*4882a593Smuzhiyun 	if (tp_features.hotkey_wlsw)
2963*4882a593Smuzhiyun 		sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2964*4882a593Smuzhiyun 			     "hotkey_radio_sw");
2965*4882a593Smuzhiyun }
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun /* sysfs hotkey tablet mode (pollable) --------------------------------- */
hotkey_tablet_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2968*4882a593Smuzhiyun static ssize_t hotkey_tablet_mode_show(struct device *dev,
2969*4882a593Smuzhiyun 			   struct device_attribute *attr,
2970*4882a593Smuzhiyun 			   char *buf)
2971*4882a593Smuzhiyun {
2972*4882a593Smuzhiyun 	int res, s;
2973*4882a593Smuzhiyun 	res = hotkey_get_tablet_mode(&s);
2974*4882a593Smuzhiyun 	if (res < 0)
2975*4882a593Smuzhiyun 		return res;
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2978*4882a593Smuzhiyun }
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun static DEVICE_ATTR_RO(hotkey_tablet_mode);
2981*4882a593Smuzhiyun 
hotkey_tablet_mode_notify_change(void)2982*4882a593Smuzhiyun static void hotkey_tablet_mode_notify_change(void)
2983*4882a593Smuzhiyun {
2984*4882a593Smuzhiyun 	if (tp_features.hotkey_tablet)
2985*4882a593Smuzhiyun 		sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2986*4882a593Smuzhiyun 			     "hotkey_tablet_mode");
2987*4882a593Smuzhiyun }
2988*4882a593Smuzhiyun 
2989*4882a593Smuzhiyun /* sysfs wakeup reason (pollable) -------------------------------------- */
hotkey_wakeup_reason_show(struct device * dev,struct device_attribute * attr,char * buf)2990*4882a593Smuzhiyun static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2991*4882a593Smuzhiyun 			   struct device_attribute *attr,
2992*4882a593Smuzhiyun 			   char *buf)
2993*4882a593Smuzhiyun {
2994*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2995*4882a593Smuzhiyun }
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2998*4882a593Smuzhiyun 
hotkey_wakeup_reason_notify_change(void)2999*4882a593Smuzhiyun static void hotkey_wakeup_reason_notify_change(void)
3000*4882a593Smuzhiyun {
3001*4882a593Smuzhiyun 	sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3002*4882a593Smuzhiyun 		     "wakeup_reason");
3003*4882a593Smuzhiyun }
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
hotkey_wakeup_hotunplug_complete_show(struct device * dev,struct device_attribute * attr,char * buf)3006*4882a593Smuzhiyun static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3007*4882a593Smuzhiyun 			   struct device_attribute *attr,
3008*4882a593Smuzhiyun 			   char *buf)
3009*4882a593Smuzhiyun {
3010*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3011*4882a593Smuzhiyun }
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3014*4882a593Smuzhiyun 		   hotkey_wakeup_hotunplug_complete_show, NULL);
3015*4882a593Smuzhiyun 
hotkey_wakeup_hotunplug_complete_notify_change(void)3016*4882a593Smuzhiyun static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3017*4882a593Smuzhiyun {
3018*4882a593Smuzhiyun 	sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3019*4882a593Smuzhiyun 		     "wakeup_hotunplug_complete");
3020*4882a593Smuzhiyun }
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun /* sysfs adaptive kbd mode --------------------------------------------- */
3023*4882a593Smuzhiyun 
3024*4882a593Smuzhiyun static int adaptive_keyboard_get_mode(void);
3025*4882a593Smuzhiyun static int adaptive_keyboard_set_mode(int new_mode);
3026*4882a593Smuzhiyun 
3027*4882a593Smuzhiyun enum ADAPTIVE_KEY_MODE {
3028*4882a593Smuzhiyun 	HOME_MODE,
3029*4882a593Smuzhiyun 	WEB_BROWSER_MODE,
3030*4882a593Smuzhiyun 	WEB_CONFERENCE_MODE,
3031*4882a593Smuzhiyun 	FUNCTION_MODE,
3032*4882a593Smuzhiyun 	LAYFLAT_MODE
3033*4882a593Smuzhiyun };
3034*4882a593Smuzhiyun 
adaptive_kbd_mode_show(struct device * dev,struct device_attribute * attr,char * buf)3035*4882a593Smuzhiyun static ssize_t adaptive_kbd_mode_show(struct device *dev,
3036*4882a593Smuzhiyun 			   struct device_attribute *attr,
3037*4882a593Smuzhiyun 			   char *buf)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	int current_mode;
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	current_mode = adaptive_keyboard_get_mode();
3042*4882a593Smuzhiyun 	if (current_mode < 0)
3043*4882a593Smuzhiyun 		return current_mode;
3044*4882a593Smuzhiyun 
3045*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3046*4882a593Smuzhiyun }
3047*4882a593Smuzhiyun 
adaptive_kbd_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3048*4882a593Smuzhiyun static ssize_t adaptive_kbd_mode_store(struct device *dev,
3049*4882a593Smuzhiyun 			    struct device_attribute *attr,
3050*4882a593Smuzhiyun 			    const char *buf, size_t count)
3051*4882a593Smuzhiyun {
3052*4882a593Smuzhiyun 	unsigned long t;
3053*4882a593Smuzhiyun 	int res;
3054*4882a593Smuzhiyun 
3055*4882a593Smuzhiyun 	if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3056*4882a593Smuzhiyun 		return -EINVAL;
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun 	res = adaptive_keyboard_set_mode(t);
3059*4882a593Smuzhiyun 	return (res < 0) ? res : count;
3060*4882a593Smuzhiyun }
3061*4882a593Smuzhiyun 
3062*4882a593Smuzhiyun static DEVICE_ATTR_RW(adaptive_kbd_mode);
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun static struct attribute *adaptive_kbd_attributes[] = {
3065*4882a593Smuzhiyun 	&dev_attr_adaptive_kbd_mode.attr,
3066*4882a593Smuzhiyun 	NULL
3067*4882a593Smuzhiyun };
3068*4882a593Smuzhiyun 
3069*4882a593Smuzhiyun static const struct attribute_group adaptive_kbd_attr_group = {
3070*4882a593Smuzhiyun 	.attrs = adaptive_kbd_attributes,
3071*4882a593Smuzhiyun };
3072*4882a593Smuzhiyun 
3073*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
3074*4882a593Smuzhiyun 
3075*4882a593Smuzhiyun static struct attribute *hotkey_attributes[] __initdata = {
3076*4882a593Smuzhiyun 	&dev_attr_hotkey_enable.attr,
3077*4882a593Smuzhiyun 	&dev_attr_hotkey_bios_enabled.attr,
3078*4882a593Smuzhiyun 	&dev_attr_hotkey_bios_mask.attr,
3079*4882a593Smuzhiyun 	&dev_attr_wakeup_reason.attr,
3080*4882a593Smuzhiyun 	&dev_attr_wakeup_hotunplug_complete.attr,
3081*4882a593Smuzhiyun 	&dev_attr_hotkey_mask.attr,
3082*4882a593Smuzhiyun 	&dev_attr_hotkey_all_mask.attr,
3083*4882a593Smuzhiyun 	&dev_attr_hotkey_adaptive_all_mask.attr,
3084*4882a593Smuzhiyun 	&dev_attr_hotkey_recommended_mask.attr,
3085*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3086*4882a593Smuzhiyun 	&dev_attr_hotkey_source_mask.attr,
3087*4882a593Smuzhiyun 	&dev_attr_hotkey_poll_freq.attr,
3088*4882a593Smuzhiyun #endif
3089*4882a593Smuzhiyun };
3090*4882a593Smuzhiyun 
3091*4882a593Smuzhiyun /*
3092*4882a593Smuzhiyun  * Sync both the hw and sw blocking state of all switches
3093*4882a593Smuzhiyun  */
tpacpi_send_radiosw_update(void)3094*4882a593Smuzhiyun static void tpacpi_send_radiosw_update(void)
3095*4882a593Smuzhiyun {
3096*4882a593Smuzhiyun 	int wlsw;
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 	/*
3099*4882a593Smuzhiyun 	 * We must sync all rfkill controllers *before* issuing any
3100*4882a593Smuzhiyun 	 * rfkill input events, or we will race the rfkill core input
3101*4882a593Smuzhiyun 	 * handler.
3102*4882a593Smuzhiyun 	 *
3103*4882a593Smuzhiyun 	 * tpacpi_inputdev_send_mutex works as a synchronization point
3104*4882a593Smuzhiyun 	 * for the above.
3105*4882a593Smuzhiyun 	 *
3106*4882a593Smuzhiyun 	 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3107*4882a593Smuzhiyun 	 */
3108*4882a593Smuzhiyun 
3109*4882a593Smuzhiyun 	wlsw = hotkey_get_wlsw();
3110*4882a593Smuzhiyun 
3111*4882a593Smuzhiyun 	/* Sync hw blocking state first if it is hw-blocked */
3112*4882a593Smuzhiyun 	if (wlsw == TPACPI_RFK_RADIO_OFF)
3113*4882a593Smuzhiyun 		tpacpi_rfk_update_hwblock_state(true);
3114*4882a593Smuzhiyun 
3115*4882a593Smuzhiyun 	/* Sync hw blocking state last if it is hw-unblocked */
3116*4882a593Smuzhiyun 	if (wlsw == TPACPI_RFK_RADIO_ON)
3117*4882a593Smuzhiyun 		tpacpi_rfk_update_hwblock_state(false);
3118*4882a593Smuzhiyun 
3119*4882a593Smuzhiyun 	/* Issue rfkill input event for WLSW switch */
3120*4882a593Smuzhiyun 	if (!(wlsw < 0)) {
3121*4882a593Smuzhiyun 		mutex_lock(&tpacpi_inputdev_send_mutex);
3122*4882a593Smuzhiyun 
3123*4882a593Smuzhiyun 		input_report_switch(tpacpi_inputdev,
3124*4882a593Smuzhiyun 				    SW_RFKILL_ALL, (wlsw > 0));
3125*4882a593Smuzhiyun 		input_sync(tpacpi_inputdev);
3126*4882a593Smuzhiyun 
3127*4882a593Smuzhiyun 		mutex_unlock(&tpacpi_inputdev_send_mutex);
3128*4882a593Smuzhiyun 	}
3129*4882a593Smuzhiyun 
3130*4882a593Smuzhiyun 	/*
3131*4882a593Smuzhiyun 	 * this can be unconditional, as we will poll state again
3132*4882a593Smuzhiyun 	 * if userspace uses the notify to read data
3133*4882a593Smuzhiyun 	 */
3134*4882a593Smuzhiyun 	hotkey_radio_sw_notify_change();
3135*4882a593Smuzhiyun }
3136*4882a593Smuzhiyun 
hotkey_exit(void)3137*4882a593Smuzhiyun static void hotkey_exit(void)
3138*4882a593Smuzhiyun {
3139*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3140*4882a593Smuzhiyun 	mutex_lock(&hotkey_mutex);
3141*4882a593Smuzhiyun 	hotkey_poll_stop_sync();
3142*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
3143*4882a593Smuzhiyun #endif
3144*4882a593Smuzhiyun 
3145*4882a593Smuzhiyun 	if (hotkey_dev_attributes)
3146*4882a593Smuzhiyun 		delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3147*4882a593Smuzhiyun 
3148*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3149*4882a593Smuzhiyun 		   "restoring original HKEY status and mask\n");
3150*4882a593Smuzhiyun 	/* yes, there is a bitwise or below, we want the
3151*4882a593Smuzhiyun 	 * functions to be called even if one of them fail */
3152*4882a593Smuzhiyun 	if (((tp_features.hotkey_mask &&
3153*4882a593Smuzhiyun 	      hotkey_mask_set(hotkey_orig_mask)) |
3154*4882a593Smuzhiyun 	     hotkey_status_set(false)) != 0)
3155*4882a593Smuzhiyun 		pr_err("failed to restore hot key mask to BIOS defaults\n");
3156*4882a593Smuzhiyun }
3157*4882a593Smuzhiyun 
hotkey_unmap(const unsigned int scancode)3158*4882a593Smuzhiyun static void __init hotkey_unmap(const unsigned int scancode)
3159*4882a593Smuzhiyun {
3160*4882a593Smuzhiyun 	if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3161*4882a593Smuzhiyun 		clear_bit(hotkey_keycode_map[scancode],
3162*4882a593Smuzhiyun 			  tpacpi_inputdev->keybit);
3163*4882a593Smuzhiyun 		hotkey_keycode_map[scancode] = KEY_RESERVED;
3164*4882a593Smuzhiyun 	}
3165*4882a593Smuzhiyun }
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun /*
3168*4882a593Smuzhiyun  * HKEY quirks:
3169*4882a593Smuzhiyun  *   TPACPI_HK_Q_INIMASK:	Supports FN+F3,FN+F4,FN+F12
3170*4882a593Smuzhiyun  */
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun #define	TPACPI_HK_Q_INIMASK	0x0001
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3175*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3176*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3177*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3178*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3179*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3180*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3181*4882a593Smuzhiyun 	TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3182*4882a593Smuzhiyun 	TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3183*4882a593Smuzhiyun 	TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3184*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3185*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3186*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3187*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3188*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3189*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3190*4882a593Smuzhiyun 	TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3191*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3192*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3193*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3194*4882a593Smuzhiyun };
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun typedef u16 tpacpi_keymap_entry_t;
3197*4882a593Smuzhiyun typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3198*4882a593Smuzhiyun 
hotkey_init_tablet_mode(void)3199*4882a593Smuzhiyun static int hotkey_init_tablet_mode(void)
3200*4882a593Smuzhiyun {
3201*4882a593Smuzhiyun 	int in_tablet_mode = 0, res;
3202*4882a593Smuzhiyun 	char *type = NULL;
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun 	if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3205*4882a593Smuzhiyun 		int has_tablet_mode;
3206*4882a593Smuzhiyun 
3207*4882a593Smuzhiyun 		in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3208*4882a593Smuzhiyun 							     &has_tablet_mode);
3209*4882a593Smuzhiyun 		/*
3210*4882a593Smuzhiyun 		 * The Yoga 11e series has 2 accelerometers described by a
3211*4882a593Smuzhiyun 		 * BOSC0200 ACPI node. This setup relies on a Windows service
3212*4882a593Smuzhiyun 		 * which calls special ACPI methods on this node to report
3213*4882a593Smuzhiyun 		 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3214*4882a593Smuzhiyun 		 * does not support this, so skip the hotkey on these models.
3215*4882a593Smuzhiyun 		 */
3216*4882a593Smuzhiyun 		if (has_tablet_mode && !acpi_dev_present("BOSC0200", "1", -1))
3217*4882a593Smuzhiyun 			tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3218*4882a593Smuzhiyun 		type = "GMMS";
3219*4882a593Smuzhiyun 	} else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3220*4882a593Smuzhiyun 		/* For X41t, X60t, X61t Tablets... */
3221*4882a593Smuzhiyun 		tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3222*4882a593Smuzhiyun 		in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3223*4882a593Smuzhiyun 		type = "MHKG";
3224*4882a593Smuzhiyun 	}
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun 	if (!tp_features.hotkey_tablet)
3227*4882a593Smuzhiyun 		return 0;
3228*4882a593Smuzhiyun 
3229*4882a593Smuzhiyun 	pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3230*4882a593Smuzhiyun 		type, in_tablet_mode ? "tablet" : "laptop");
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 	res = add_to_attr_set(hotkey_dev_attributes,
3233*4882a593Smuzhiyun 			      &dev_attr_hotkey_tablet_mode.attr);
3234*4882a593Smuzhiyun 	if (res)
3235*4882a593Smuzhiyun 		return -1;
3236*4882a593Smuzhiyun 
3237*4882a593Smuzhiyun 	return in_tablet_mode;
3238*4882a593Smuzhiyun }
3239*4882a593Smuzhiyun 
hotkey_init(struct ibm_init_struct * iibm)3240*4882a593Smuzhiyun static int __init hotkey_init(struct ibm_init_struct *iibm)
3241*4882a593Smuzhiyun {
3242*4882a593Smuzhiyun 	/* Requirements for changing the default keymaps:
3243*4882a593Smuzhiyun 	 *
3244*4882a593Smuzhiyun 	 * 1. Many of the keys are mapped to KEY_RESERVED for very
3245*4882a593Smuzhiyun 	 *    good reasons.  Do not change them unless you have deep
3246*4882a593Smuzhiyun 	 *    knowledge on the IBM and Lenovo ThinkPad firmware for
3247*4882a593Smuzhiyun 	 *    the various ThinkPad models.  The driver behaves
3248*4882a593Smuzhiyun 	 *    differently for KEY_RESERVED: such keys have their
3249*4882a593Smuzhiyun 	 *    hot key mask *unset* in mask_recommended, and also
3250*4882a593Smuzhiyun 	 *    in the initial hot key mask programmed into the
3251*4882a593Smuzhiyun 	 *    firmware at driver load time, which means the firm-
3252*4882a593Smuzhiyun 	 *    ware may react very differently if you change them to
3253*4882a593Smuzhiyun 	 *    something else;
3254*4882a593Smuzhiyun 	 *
3255*4882a593Smuzhiyun 	 * 2. You must be subscribed to the linux-thinkpad and
3256*4882a593Smuzhiyun 	 *    ibm-acpi-devel mailing lists, and you should read the
3257*4882a593Smuzhiyun 	 *    list archives since 2007 if you want to change the
3258*4882a593Smuzhiyun 	 *    keymaps.  This requirement exists so that you will
3259*4882a593Smuzhiyun 	 *    know the past history of problems with the thinkpad-
3260*4882a593Smuzhiyun 	 *    acpi driver keymaps, and also that you will be
3261*4882a593Smuzhiyun 	 *    listening to any bug reports;
3262*4882a593Smuzhiyun 	 *
3263*4882a593Smuzhiyun 	 * 3. Do not send thinkpad-acpi specific patches directly to
3264*4882a593Smuzhiyun 	 *    for merging, *ever*.  Send them to the linux-acpi
3265*4882a593Smuzhiyun 	 *    mailinglist for comments.  Merging is to be done only
3266*4882a593Smuzhiyun 	 *    through acpi-test and the ACPI maintainer.
3267*4882a593Smuzhiyun 	 *
3268*4882a593Smuzhiyun 	 * If the above is too much to ask, don't change the keymap.
3269*4882a593Smuzhiyun 	 * Ask the thinkpad-acpi maintainer to do it, instead.
3270*4882a593Smuzhiyun 	 */
3271*4882a593Smuzhiyun 
3272*4882a593Smuzhiyun 	enum keymap_index {
3273*4882a593Smuzhiyun 		TPACPI_KEYMAP_IBM_GENERIC = 0,
3274*4882a593Smuzhiyun 		TPACPI_KEYMAP_LENOVO_GENERIC,
3275*4882a593Smuzhiyun 	};
3276*4882a593Smuzhiyun 
3277*4882a593Smuzhiyun 	static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3278*4882a593Smuzhiyun 	/* Generic keymap for IBM ThinkPads */
3279*4882a593Smuzhiyun 	[TPACPI_KEYMAP_IBM_GENERIC] = {
3280*4882a593Smuzhiyun 		/* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3281*4882a593Smuzhiyun 		KEY_FN_F1,	KEY_BATTERY,	KEY_COFFEE,	KEY_SLEEP,
3282*4882a593Smuzhiyun 		KEY_WLAN,	KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3283*4882a593Smuzhiyun 		KEY_FN_F9,	KEY_FN_F10,	KEY_FN_F11,	KEY_SUSPEND,
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 		/* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3286*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0C: FN+BACKSPACE */
3287*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0D: FN+INSERT */
3288*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0E: FN+DELETE */
3289*4882a593Smuzhiyun 
3290*4882a593Smuzhiyun 		/* brightness: firmware always reacts to them */
3291*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x0F: FN+HOME (brightness up) */
3292*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x10: FN+END (brightness down) */
3293*4882a593Smuzhiyun 
3294*4882a593Smuzhiyun 		/* Thinklight: firmware always react to it */
3295*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x11: FN+PGUP (thinklight toggle) */
3296*4882a593Smuzhiyun 
3297*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x12: FN+PGDOWN */
3298*4882a593Smuzhiyun 		KEY_ZOOM,	/* 0x13: FN+SPACE (zoom) */
3299*4882a593Smuzhiyun 
3300*4882a593Smuzhiyun 		/* Volume: firmware always react to it and reprograms
3301*4882a593Smuzhiyun 		 * the built-in *extra* mixer.  Never map it to control
3302*4882a593Smuzhiyun 		 * another mixer by default. */
3303*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x14: VOLUME UP */
3304*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x15: VOLUME DOWN */
3305*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x16: MUTE */
3306*4882a593Smuzhiyun 
3307*4882a593Smuzhiyun 		KEY_VENDOR,	/* 0x17: Thinkpad/AccessIBM/Lenovo */
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 		/* (assignments unknown, please report if found) */
3310*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3311*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3312*4882a593Smuzhiyun 
3313*4882a593Smuzhiyun 		/* No assignments, only used for Adaptive keyboards. */
3314*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3315*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3316*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3317*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3318*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun 		/* No assignment, used for newer Lenovo models */
3321*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3322*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3323*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3324*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3325*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3326*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN
3327*4882a593Smuzhiyun 
3328*4882a593Smuzhiyun 		},
3329*4882a593Smuzhiyun 
3330*4882a593Smuzhiyun 	/* Generic keymap for Lenovo ThinkPads */
3331*4882a593Smuzhiyun 	[TPACPI_KEYMAP_LENOVO_GENERIC] = {
3332*4882a593Smuzhiyun 		/* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3333*4882a593Smuzhiyun 		KEY_FN_F1,	KEY_COFFEE,	KEY_BATTERY,	KEY_SLEEP,
3334*4882a593Smuzhiyun 		KEY_WLAN,	KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3335*4882a593Smuzhiyun 		KEY_FN_F9,	KEY_FN_F10,	KEY_FN_F11,	KEY_SUSPEND,
3336*4882a593Smuzhiyun 
3337*4882a593Smuzhiyun 		/* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3338*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0C: FN+BACKSPACE */
3339*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0D: FN+INSERT */
3340*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x0E: FN+DELETE */
3341*4882a593Smuzhiyun 
3342*4882a593Smuzhiyun 		/* These should be enabled --only-- when ACPI video
3343*4882a593Smuzhiyun 		 * is disabled (i.e. in "vendor" mode), and are handled
3344*4882a593Smuzhiyun 		 * in a special way by the init code */
3345*4882a593Smuzhiyun 		KEY_BRIGHTNESSUP,	/* 0x0F: FN+HOME (brightness up) */
3346*4882a593Smuzhiyun 		KEY_BRIGHTNESSDOWN,	/* 0x10: FN+END (brightness down) */
3347*4882a593Smuzhiyun 
3348*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x11: FN+PGUP (thinklight toggle) */
3349*4882a593Smuzhiyun 
3350*4882a593Smuzhiyun 		KEY_UNKNOWN,	/* 0x12: FN+PGDOWN */
3351*4882a593Smuzhiyun 		KEY_ZOOM,	/* 0x13: FN+SPACE (zoom) */
3352*4882a593Smuzhiyun 
3353*4882a593Smuzhiyun 		/* Volume: z60/z61, T60 (BIOS version?): firmware always
3354*4882a593Smuzhiyun 		 * react to it and reprograms the built-in *extra* mixer.
3355*4882a593Smuzhiyun 		 * Never map it to control another mixer by default.
3356*4882a593Smuzhiyun 		 *
3357*4882a593Smuzhiyun 		 * T60?, T61, R60?, R61: firmware and EC tries to send
3358*4882a593Smuzhiyun 		 * these over the regular keyboard, so these are no-ops,
3359*4882a593Smuzhiyun 		 * but there are still weird bugs re. MUTE, so do not
3360*4882a593Smuzhiyun 		 * change unless you get test reports from all Lenovo
3361*4882a593Smuzhiyun 		 * models.  May cause the BIOS to interfere with the
3362*4882a593Smuzhiyun 		 * HDA mixer.
3363*4882a593Smuzhiyun 		 */
3364*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x14: VOLUME UP */
3365*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x15: VOLUME DOWN */
3366*4882a593Smuzhiyun 		KEY_RESERVED,	/* 0x16: MUTE */
3367*4882a593Smuzhiyun 
3368*4882a593Smuzhiyun 		KEY_VENDOR,	/* 0x17: Thinkpad/AccessIBM/Lenovo */
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun 		/* (assignments unknown, please report if found) */
3371*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN,
3372*4882a593Smuzhiyun 
3373*4882a593Smuzhiyun 		/*
3374*4882a593Smuzhiyun 		 * The mic mute button only sends 0x1a.  It does not
3375*4882a593Smuzhiyun 		 * automatically mute the mic or change the mute light.
3376*4882a593Smuzhiyun 		 */
3377*4882a593Smuzhiyun 		KEY_MICMUTE,	/* 0x1a: Mic mute (since ?400 or so) */
3378*4882a593Smuzhiyun 
3379*4882a593Smuzhiyun 		/* (assignments unknown, please report if found) */
3380*4882a593Smuzhiyun 		KEY_UNKNOWN,
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun 		/* Extra keys in use since the X240 / T440 / T540 */
3383*4882a593Smuzhiyun 		KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3384*4882a593Smuzhiyun 
3385*4882a593Smuzhiyun 		/*
3386*4882a593Smuzhiyun 		 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3387*4882a593Smuzhiyun 		 * The first item in this list is the Mute button which is
3388*4882a593Smuzhiyun 		 * emitted with 0x103 through
3389*4882a593Smuzhiyun 		 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3390*4882a593Smuzhiyun 		 * symbol is held.
3391*4882a593Smuzhiyun 		 * We'll need to offset those by 0x20.
3392*4882a593Smuzhiyun 		 */
3393*4882a593Smuzhiyun 		KEY_RESERVED,        /* Mute held, 0x103 */
3394*4882a593Smuzhiyun 		KEY_BRIGHTNESS_MIN,  /* Backlight off */
3395*4882a593Smuzhiyun 		KEY_RESERVED,        /* Clipping tool */
3396*4882a593Smuzhiyun 		KEY_RESERVED,        /* Cloud */
3397*4882a593Smuzhiyun 		KEY_RESERVED,
3398*4882a593Smuzhiyun 		KEY_VOICECOMMAND,    /* Voice */
3399*4882a593Smuzhiyun 		KEY_RESERVED,
3400*4882a593Smuzhiyun 		KEY_RESERVED,        /* Gestures */
3401*4882a593Smuzhiyun 		KEY_RESERVED,
3402*4882a593Smuzhiyun 		KEY_RESERVED,
3403*4882a593Smuzhiyun 		KEY_RESERVED,
3404*4882a593Smuzhiyun 		KEY_CONFIG,          /* Settings */
3405*4882a593Smuzhiyun 		KEY_RESERVED,        /* New tab */
3406*4882a593Smuzhiyun 		KEY_REFRESH,         /* Reload */
3407*4882a593Smuzhiyun 		KEY_BACK,            /* Back */
3408*4882a593Smuzhiyun 		KEY_RESERVED,        /* Microphone down */
3409*4882a593Smuzhiyun 		KEY_RESERVED,        /* Microphone up */
3410*4882a593Smuzhiyun 		KEY_RESERVED,        /* Microphone cancellation */
3411*4882a593Smuzhiyun 		KEY_RESERVED,        /* Camera mode */
3412*4882a593Smuzhiyun 		KEY_RESERVED,        /* Rotate display, 0x116 */
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 		/*
3415*4882a593Smuzhiyun 		 * These are found in 2017 models (e.g. T470s, X270).
3416*4882a593Smuzhiyun 		 * The lowest known value is 0x311, which according to
3417*4882a593Smuzhiyun 		 * the manual should launch a user defined favorite
3418*4882a593Smuzhiyun 		 * application.
3419*4882a593Smuzhiyun 		 *
3420*4882a593Smuzhiyun 		 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3421*4882a593Smuzhiyun 		 * corresponding to 0x34.
3422*4882a593Smuzhiyun 		 */
3423*4882a593Smuzhiyun 
3424*4882a593Smuzhiyun 		/* (assignments unknown, please report if found) */
3425*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3426*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3427*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3428*4882a593Smuzhiyun 		KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3429*4882a593Smuzhiyun 		KEY_UNKNOWN,
3430*4882a593Smuzhiyun 
3431*4882a593Smuzhiyun 		KEY_BOOKMARKS,			/* Favorite app, 0x311 */
3432*4882a593Smuzhiyun 		KEY_SELECTIVE_SCREENSHOT,	/* Clipping tool */
3433*4882a593Smuzhiyun 		KEY_CALC,			/* Calculator (above numpad, P52) */
3434*4882a593Smuzhiyun 		KEY_BLUETOOTH,			/* Bluetooth */
3435*4882a593Smuzhiyun 		KEY_KEYBOARD,			/* Keyboard, 0x315 */
3436*4882a593Smuzhiyun 		KEY_FN_RIGHT_SHIFT,		/* Fn + right Shift */
3437*4882a593Smuzhiyun 		KEY_NOTIFICATION_CENTER,	/* Notification Center */
3438*4882a593Smuzhiyun 		KEY_PICKUP_PHONE,		/* Answer incoming call */
3439*4882a593Smuzhiyun 		KEY_HANGUP_PHONE,		/* Decline incoming call */
3440*4882a593Smuzhiyun 		},
3441*4882a593Smuzhiyun 	};
3442*4882a593Smuzhiyun 
3443*4882a593Smuzhiyun 	static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3444*4882a593Smuzhiyun 		/* Generic maps (fallback) */
3445*4882a593Smuzhiyun 		{
3446*4882a593Smuzhiyun 		  .vendor = PCI_VENDOR_ID_IBM,
3447*4882a593Smuzhiyun 		  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3448*4882a593Smuzhiyun 		  .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3449*4882a593Smuzhiyun 		},
3450*4882a593Smuzhiyun 		{
3451*4882a593Smuzhiyun 		  .vendor = PCI_VENDOR_ID_LENOVO,
3452*4882a593Smuzhiyun 		  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3453*4882a593Smuzhiyun 		  .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3454*4882a593Smuzhiyun 		},
3455*4882a593Smuzhiyun 	};
3456*4882a593Smuzhiyun 
3457*4882a593Smuzhiyun #define TPACPI_HOTKEY_MAP_SIZE		sizeof(tpacpi_keymap_t)
3458*4882a593Smuzhiyun #define TPACPI_HOTKEY_MAP_TYPESIZE	sizeof(tpacpi_keymap_entry_t)
3459*4882a593Smuzhiyun 
3460*4882a593Smuzhiyun 	int res, i;
3461*4882a593Smuzhiyun 	int status;
3462*4882a593Smuzhiyun 	int hkeyv;
3463*4882a593Smuzhiyun 	bool radiosw_state  = false;
3464*4882a593Smuzhiyun 	bool tabletsw_state = false;
3465*4882a593Smuzhiyun 
3466*4882a593Smuzhiyun 	unsigned long quirks;
3467*4882a593Smuzhiyun 	unsigned long keymap_id;
3468*4882a593Smuzhiyun 
3469*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3470*4882a593Smuzhiyun 			"initializing hotkey subdriver\n");
3471*4882a593Smuzhiyun 
3472*4882a593Smuzhiyun 	BUG_ON(!tpacpi_inputdev);
3473*4882a593Smuzhiyun 	BUG_ON(tpacpi_inputdev->open != NULL ||
3474*4882a593Smuzhiyun 	       tpacpi_inputdev->close != NULL);
3475*4882a593Smuzhiyun 
3476*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(hkey);
3477*4882a593Smuzhiyun 	mutex_init(&hotkey_mutex);
3478*4882a593Smuzhiyun 
3479*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3480*4882a593Smuzhiyun 	mutex_init(&hotkey_thread_data_mutex);
3481*4882a593Smuzhiyun #endif
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 	/* hotkey not supported on 570 */
3484*4882a593Smuzhiyun 	tp_features.hotkey = hkey_handle != NULL;
3485*4882a593Smuzhiyun 
3486*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3487*4882a593Smuzhiyun 		"hotkeys are %s\n",
3488*4882a593Smuzhiyun 		str_supported(tp_features.hotkey));
3489*4882a593Smuzhiyun 
3490*4882a593Smuzhiyun 	if (!tp_features.hotkey)
3491*4882a593Smuzhiyun 		return 1;
3492*4882a593Smuzhiyun 
3493*4882a593Smuzhiyun 	quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3494*4882a593Smuzhiyun 				     ARRAY_SIZE(tpacpi_hotkey_qtable));
3495*4882a593Smuzhiyun 
3496*4882a593Smuzhiyun 	tpacpi_disable_brightness_delay();
3497*4882a593Smuzhiyun 
3498*4882a593Smuzhiyun 	/* MUST have enough space for all attributes to be added to
3499*4882a593Smuzhiyun 	 * hotkey_dev_attributes */
3500*4882a593Smuzhiyun 	hotkey_dev_attributes = create_attr_set(
3501*4882a593Smuzhiyun 					ARRAY_SIZE(hotkey_attributes) + 2,
3502*4882a593Smuzhiyun 					NULL);
3503*4882a593Smuzhiyun 	if (!hotkey_dev_attributes)
3504*4882a593Smuzhiyun 		return -ENOMEM;
3505*4882a593Smuzhiyun 	res = add_many_to_attr_set(hotkey_dev_attributes,
3506*4882a593Smuzhiyun 			hotkey_attributes,
3507*4882a593Smuzhiyun 			ARRAY_SIZE(hotkey_attributes));
3508*4882a593Smuzhiyun 	if (res)
3509*4882a593Smuzhiyun 		goto err_exit;
3510*4882a593Smuzhiyun 
3511*4882a593Smuzhiyun 	/* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3512*4882a593Smuzhiyun 	   A30, R30, R31, T20-22, X20-21, X22-24.  Detected by checking
3513*4882a593Smuzhiyun 	   for HKEY interface version 0x100 */
3514*4882a593Smuzhiyun 	if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3515*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3516*4882a593Smuzhiyun 			    "firmware HKEY interface version: 0x%x\n",
3517*4882a593Smuzhiyun 			    hkeyv);
3518*4882a593Smuzhiyun 
3519*4882a593Smuzhiyun 		switch (hkeyv >> 8) {
3520*4882a593Smuzhiyun 		case 1:
3521*4882a593Smuzhiyun 			/*
3522*4882a593Smuzhiyun 			 * MHKV 0x100 in A31, R40, R40e,
3523*4882a593Smuzhiyun 			 * T4x, X31, and later
3524*4882a593Smuzhiyun 			 */
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 			/* Paranoia check AND init hotkey_all_mask */
3527*4882a593Smuzhiyun 			if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3528*4882a593Smuzhiyun 					"MHKA", "qd")) {
3529*4882a593Smuzhiyun 				pr_err("missing MHKA handler, please report this to %s\n",
3530*4882a593Smuzhiyun 				       TPACPI_MAIL);
3531*4882a593Smuzhiyun 				/* Fallback: pre-init for FN+F3,F4,F12 */
3532*4882a593Smuzhiyun 				hotkey_all_mask = 0x080cU;
3533*4882a593Smuzhiyun 			} else {
3534*4882a593Smuzhiyun 				tp_features.hotkey_mask = 1;
3535*4882a593Smuzhiyun 			}
3536*4882a593Smuzhiyun 			break;
3537*4882a593Smuzhiyun 
3538*4882a593Smuzhiyun 		case 2:
3539*4882a593Smuzhiyun 			/*
3540*4882a593Smuzhiyun 			 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3541*4882a593Smuzhiyun 			 */
3542*4882a593Smuzhiyun 
3543*4882a593Smuzhiyun 			/* Paranoia check AND init hotkey_all_mask */
3544*4882a593Smuzhiyun 			if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3545*4882a593Smuzhiyun 					"MHKA", "dd", 1)) {
3546*4882a593Smuzhiyun 				pr_err("missing MHKA handler, please report this to %s\n",
3547*4882a593Smuzhiyun 				       TPACPI_MAIL);
3548*4882a593Smuzhiyun 				/* Fallback: pre-init for FN+F3,F4,F12 */
3549*4882a593Smuzhiyun 				hotkey_all_mask = 0x080cU;
3550*4882a593Smuzhiyun 			} else {
3551*4882a593Smuzhiyun 				tp_features.hotkey_mask = 1;
3552*4882a593Smuzhiyun 			}
3553*4882a593Smuzhiyun 
3554*4882a593Smuzhiyun 			/*
3555*4882a593Smuzhiyun 			 * Check if we have an adaptive keyboard, like on the
3556*4882a593Smuzhiyun 			 * Lenovo Carbon X1 2014 (2nd Gen).
3557*4882a593Smuzhiyun 			 */
3558*4882a593Smuzhiyun 			if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3559*4882a593Smuzhiyun 				       "MHKA", "dd", 2)) {
3560*4882a593Smuzhiyun 				if (hotkey_adaptive_all_mask != 0) {
3561*4882a593Smuzhiyun 					tp_features.has_adaptive_kbd = true;
3562*4882a593Smuzhiyun 					res = sysfs_create_group(
3563*4882a593Smuzhiyun 						&tpacpi_pdev->dev.kobj,
3564*4882a593Smuzhiyun 						&adaptive_kbd_attr_group);
3565*4882a593Smuzhiyun 					if (res)
3566*4882a593Smuzhiyun 						goto err_exit;
3567*4882a593Smuzhiyun 				}
3568*4882a593Smuzhiyun 			} else {
3569*4882a593Smuzhiyun 				tp_features.has_adaptive_kbd = false;
3570*4882a593Smuzhiyun 				hotkey_adaptive_all_mask = 0x0U;
3571*4882a593Smuzhiyun 			}
3572*4882a593Smuzhiyun 			break;
3573*4882a593Smuzhiyun 
3574*4882a593Smuzhiyun 		default:
3575*4882a593Smuzhiyun 			pr_err("unknown version of the HKEY interface: 0x%x\n",
3576*4882a593Smuzhiyun 			       hkeyv);
3577*4882a593Smuzhiyun 			pr_err("please report this to %s\n", TPACPI_MAIL);
3578*4882a593Smuzhiyun 			break;
3579*4882a593Smuzhiyun 		}
3580*4882a593Smuzhiyun 	}
3581*4882a593Smuzhiyun 
3582*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3583*4882a593Smuzhiyun 		"hotkey masks are %s\n",
3584*4882a593Smuzhiyun 		str_supported(tp_features.hotkey_mask));
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 	/* Init hotkey_all_mask if not initialized yet */
3587*4882a593Smuzhiyun 	if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3588*4882a593Smuzhiyun 	    (quirks & TPACPI_HK_Q_INIMASK))
3589*4882a593Smuzhiyun 		hotkey_all_mask = 0x080cU;  /* FN+F12, FN+F4, FN+F3 */
3590*4882a593Smuzhiyun 
3591*4882a593Smuzhiyun 	/* Init hotkey_acpi_mask and hotkey_orig_mask */
3592*4882a593Smuzhiyun 	if (tp_features.hotkey_mask) {
3593*4882a593Smuzhiyun 		/* hotkey_source_mask *must* be zero for
3594*4882a593Smuzhiyun 		 * the first hotkey_mask_get to return hotkey_orig_mask */
3595*4882a593Smuzhiyun 		res = hotkey_mask_get();
3596*4882a593Smuzhiyun 		if (res)
3597*4882a593Smuzhiyun 			goto err_exit;
3598*4882a593Smuzhiyun 
3599*4882a593Smuzhiyun 		hotkey_orig_mask = hotkey_acpi_mask;
3600*4882a593Smuzhiyun 	} else {
3601*4882a593Smuzhiyun 		hotkey_orig_mask = hotkey_all_mask;
3602*4882a593Smuzhiyun 		hotkey_acpi_mask = hotkey_all_mask;
3603*4882a593Smuzhiyun 	}
3604*4882a593Smuzhiyun 
3605*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3606*4882a593Smuzhiyun 	if (dbg_wlswemul) {
3607*4882a593Smuzhiyun 		tp_features.hotkey_wlsw = 1;
3608*4882a593Smuzhiyun 		radiosw_state = !!tpacpi_wlsw_emulstate;
3609*4882a593Smuzhiyun 		pr_info("radio switch emulation enabled\n");
3610*4882a593Smuzhiyun 	} else
3611*4882a593Smuzhiyun #endif
3612*4882a593Smuzhiyun 	/* Not all thinkpads have a hardware radio switch */
3613*4882a593Smuzhiyun 	if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3614*4882a593Smuzhiyun 		tp_features.hotkey_wlsw = 1;
3615*4882a593Smuzhiyun 		radiosw_state = !!status;
3616*4882a593Smuzhiyun 		pr_info("radio switch found; radios are %s\n",
3617*4882a593Smuzhiyun 			enabled(status, 0));
3618*4882a593Smuzhiyun 	}
3619*4882a593Smuzhiyun 	if (tp_features.hotkey_wlsw)
3620*4882a593Smuzhiyun 		res = add_to_attr_set(hotkey_dev_attributes,
3621*4882a593Smuzhiyun 				&dev_attr_hotkey_radio_sw.attr);
3622*4882a593Smuzhiyun 
3623*4882a593Smuzhiyun 	res = hotkey_init_tablet_mode();
3624*4882a593Smuzhiyun 	if (res < 0)
3625*4882a593Smuzhiyun 		goto err_exit;
3626*4882a593Smuzhiyun 
3627*4882a593Smuzhiyun 	tabletsw_state = res;
3628*4882a593Smuzhiyun 
3629*4882a593Smuzhiyun 	res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3630*4882a593Smuzhiyun 					   &tpacpi_pdev->dev.kobj);
3631*4882a593Smuzhiyun 	if (res)
3632*4882a593Smuzhiyun 		goto err_exit;
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun 	/* Set up key map */
3635*4882a593Smuzhiyun 	keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3636*4882a593Smuzhiyun 					ARRAY_SIZE(tpacpi_keymap_qtable));
3637*4882a593Smuzhiyun 	BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3638*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3639*4882a593Smuzhiyun 		   "using keymap number %lu\n", keymap_id);
3640*4882a593Smuzhiyun 
3641*4882a593Smuzhiyun 	hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3642*4882a593Smuzhiyun 			TPACPI_HOTKEY_MAP_SIZE,	GFP_KERNEL);
3643*4882a593Smuzhiyun 	if (!hotkey_keycode_map) {
3644*4882a593Smuzhiyun 		pr_err("failed to allocate memory for key map\n");
3645*4882a593Smuzhiyun 		res = -ENOMEM;
3646*4882a593Smuzhiyun 		goto err_exit;
3647*4882a593Smuzhiyun 	}
3648*4882a593Smuzhiyun 
3649*4882a593Smuzhiyun 	input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3650*4882a593Smuzhiyun 	tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3651*4882a593Smuzhiyun 	tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3652*4882a593Smuzhiyun 	tpacpi_inputdev->keycode = hotkey_keycode_map;
3653*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3654*4882a593Smuzhiyun 		if (hotkey_keycode_map[i] != KEY_RESERVED) {
3655*4882a593Smuzhiyun 			input_set_capability(tpacpi_inputdev, EV_KEY,
3656*4882a593Smuzhiyun 						hotkey_keycode_map[i]);
3657*4882a593Smuzhiyun 		} else {
3658*4882a593Smuzhiyun 			if (i < sizeof(hotkey_reserved_mask)*8)
3659*4882a593Smuzhiyun 				hotkey_reserved_mask |= 1 << i;
3660*4882a593Smuzhiyun 		}
3661*4882a593Smuzhiyun 	}
3662*4882a593Smuzhiyun 
3663*4882a593Smuzhiyun 	if (tp_features.hotkey_wlsw) {
3664*4882a593Smuzhiyun 		input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3665*4882a593Smuzhiyun 		input_report_switch(tpacpi_inputdev,
3666*4882a593Smuzhiyun 				    SW_RFKILL_ALL, radiosw_state);
3667*4882a593Smuzhiyun 	}
3668*4882a593Smuzhiyun 	if (tp_features.hotkey_tablet) {
3669*4882a593Smuzhiyun 		input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3670*4882a593Smuzhiyun 		input_report_switch(tpacpi_inputdev,
3671*4882a593Smuzhiyun 				    SW_TABLET_MODE, tabletsw_state);
3672*4882a593Smuzhiyun 	}
3673*4882a593Smuzhiyun 
3674*4882a593Smuzhiyun 	/* Do not issue duplicate brightness change events to
3675*4882a593Smuzhiyun 	 * userspace. tpacpi_detect_brightness_capabilities() must have
3676*4882a593Smuzhiyun 	 * been called before this point  */
3677*4882a593Smuzhiyun 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3678*4882a593Smuzhiyun 		pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3679*4882a593Smuzhiyun 		pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 		/* Disable brightness up/down on Lenovo thinkpads when
3682*4882a593Smuzhiyun 		 * ACPI is handling them, otherwise it is plain impossible
3683*4882a593Smuzhiyun 		 * for userspace to do something even remotely sane */
3684*4882a593Smuzhiyun 		hotkey_reserved_mask |=
3685*4882a593Smuzhiyun 			(1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3686*4882a593Smuzhiyun 			| (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3687*4882a593Smuzhiyun 		hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3688*4882a593Smuzhiyun 		hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3689*4882a593Smuzhiyun 	}
3690*4882a593Smuzhiyun 
3691*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3692*4882a593Smuzhiyun 	hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3693*4882a593Smuzhiyun 				& ~hotkey_all_mask
3694*4882a593Smuzhiyun 				& ~hotkey_reserved_mask;
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3697*4882a593Smuzhiyun 		    "hotkey source mask 0x%08x, polling freq %u\n",
3698*4882a593Smuzhiyun 		    hotkey_source_mask, hotkey_poll_freq);
3699*4882a593Smuzhiyun #endif
3700*4882a593Smuzhiyun 
3701*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3702*4882a593Smuzhiyun 			"enabling firmware HKEY event interface...\n");
3703*4882a593Smuzhiyun 	res = hotkey_status_set(true);
3704*4882a593Smuzhiyun 	if (res) {
3705*4882a593Smuzhiyun 		hotkey_exit();
3706*4882a593Smuzhiyun 		return res;
3707*4882a593Smuzhiyun 	}
3708*4882a593Smuzhiyun 	res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3709*4882a593Smuzhiyun 			       | hotkey_driver_mask)
3710*4882a593Smuzhiyun 			      & ~hotkey_source_mask);
3711*4882a593Smuzhiyun 	if (res < 0 && res != -ENXIO) {
3712*4882a593Smuzhiyun 		hotkey_exit();
3713*4882a593Smuzhiyun 		return res;
3714*4882a593Smuzhiyun 	}
3715*4882a593Smuzhiyun 	hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3716*4882a593Smuzhiyun 				& ~hotkey_reserved_mask;
3717*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3718*4882a593Smuzhiyun 		"initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3719*4882a593Smuzhiyun 		hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3720*4882a593Smuzhiyun 
3721*4882a593Smuzhiyun 	tpacpi_inputdev->open = &hotkey_inputdev_open;
3722*4882a593Smuzhiyun 	tpacpi_inputdev->close = &hotkey_inputdev_close;
3723*4882a593Smuzhiyun 
3724*4882a593Smuzhiyun 	hotkey_poll_setup_safe(true);
3725*4882a593Smuzhiyun 
3726*4882a593Smuzhiyun 	return 0;
3727*4882a593Smuzhiyun 
3728*4882a593Smuzhiyun err_exit:
3729*4882a593Smuzhiyun 	delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3730*4882a593Smuzhiyun 	sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3731*4882a593Smuzhiyun 			&adaptive_kbd_attr_group);
3732*4882a593Smuzhiyun 
3733*4882a593Smuzhiyun 	hotkey_dev_attributes = NULL;
3734*4882a593Smuzhiyun 
3735*4882a593Smuzhiyun 	return (res < 0) ? res : 1;
3736*4882a593Smuzhiyun }
3737*4882a593Smuzhiyun 
3738*4882a593Smuzhiyun /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3739*4882a593Smuzhiyun  * mode, Web conference mode, Function mode and Lay-flat mode.
3740*4882a593Smuzhiyun  * We support Home mode and Function mode currently.
3741*4882a593Smuzhiyun  *
3742*4882a593Smuzhiyun  * Will consider support rest of modes in future.
3743*4882a593Smuzhiyun  *
3744*4882a593Smuzhiyun  */
3745*4882a593Smuzhiyun static const int adaptive_keyboard_modes[] = {
3746*4882a593Smuzhiyun 	HOME_MODE,
3747*4882a593Smuzhiyun /*	WEB_BROWSER_MODE = 2,
3748*4882a593Smuzhiyun 	WEB_CONFERENCE_MODE = 3, */
3749*4882a593Smuzhiyun 	FUNCTION_MODE
3750*4882a593Smuzhiyun };
3751*4882a593Smuzhiyun 
3752*4882a593Smuzhiyun #define DFR_CHANGE_ROW			0x101
3753*4882a593Smuzhiyun #define DFR_SHOW_QUICKVIEW_ROW		0x102
3754*4882a593Smuzhiyun #define FIRST_ADAPTIVE_KEY		0x103
3755*4882a593Smuzhiyun 
3756*4882a593Smuzhiyun /* press Fn key a while second, it will switch to Function Mode. Then
3757*4882a593Smuzhiyun  * release Fn key, previous mode be restored.
3758*4882a593Smuzhiyun  */
3759*4882a593Smuzhiyun static bool adaptive_keyboard_mode_is_saved;
3760*4882a593Smuzhiyun static int adaptive_keyboard_prev_mode;
3761*4882a593Smuzhiyun 
adaptive_keyboard_get_mode(void)3762*4882a593Smuzhiyun static int adaptive_keyboard_get_mode(void)
3763*4882a593Smuzhiyun {
3764*4882a593Smuzhiyun 	int mode = 0;
3765*4882a593Smuzhiyun 
3766*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3767*4882a593Smuzhiyun 		pr_err("Cannot read adaptive keyboard mode\n");
3768*4882a593Smuzhiyun 		return -EIO;
3769*4882a593Smuzhiyun 	}
3770*4882a593Smuzhiyun 
3771*4882a593Smuzhiyun 	return mode;
3772*4882a593Smuzhiyun }
3773*4882a593Smuzhiyun 
adaptive_keyboard_set_mode(int new_mode)3774*4882a593Smuzhiyun static int adaptive_keyboard_set_mode(int new_mode)
3775*4882a593Smuzhiyun {
3776*4882a593Smuzhiyun 	if (new_mode < 0 ||
3777*4882a593Smuzhiyun 		new_mode > LAYFLAT_MODE)
3778*4882a593Smuzhiyun 		return -EINVAL;
3779*4882a593Smuzhiyun 
3780*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3781*4882a593Smuzhiyun 		pr_err("Cannot set adaptive keyboard mode\n");
3782*4882a593Smuzhiyun 		return -EIO;
3783*4882a593Smuzhiyun 	}
3784*4882a593Smuzhiyun 
3785*4882a593Smuzhiyun 	return 0;
3786*4882a593Smuzhiyun }
3787*4882a593Smuzhiyun 
adaptive_keyboard_get_next_mode(int mode)3788*4882a593Smuzhiyun static int adaptive_keyboard_get_next_mode(int mode)
3789*4882a593Smuzhiyun {
3790*4882a593Smuzhiyun 	size_t i;
3791*4882a593Smuzhiyun 	size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3792*4882a593Smuzhiyun 
3793*4882a593Smuzhiyun 	for (i = 0; i <= max_mode; i++) {
3794*4882a593Smuzhiyun 		if (adaptive_keyboard_modes[i] == mode)
3795*4882a593Smuzhiyun 			break;
3796*4882a593Smuzhiyun 	}
3797*4882a593Smuzhiyun 
3798*4882a593Smuzhiyun 	if (i >= max_mode)
3799*4882a593Smuzhiyun 		i = 0;
3800*4882a593Smuzhiyun 	else
3801*4882a593Smuzhiyun 		i++;
3802*4882a593Smuzhiyun 
3803*4882a593Smuzhiyun 	return adaptive_keyboard_modes[i];
3804*4882a593Smuzhiyun }
3805*4882a593Smuzhiyun 
adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)3806*4882a593Smuzhiyun static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3807*4882a593Smuzhiyun {
3808*4882a593Smuzhiyun 	int current_mode = 0;
3809*4882a593Smuzhiyun 	int new_mode = 0;
3810*4882a593Smuzhiyun 	int keycode;
3811*4882a593Smuzhiyun 
3812*4882a593Smuzhiyun 	switch (scancode) {
3813*4882a593Smuzhiyun 	case DFR_CHANGE_ROW:
3814*4882a593Smuzhiyun 		if (adaptive_keyboard_mode_is_saved) {
3815*4882a593Smuzhiyun 			new_mode = adaptive_keyboard_prev_mode;
3816*4882a593Smuzhiyun 			adaptive_keyboard_mode_is_saved = false;
3817*4882a593Smuzhiyun 		} else {
3818*4882a593Smuzhiyun 			current_mode = adaptive_keyboard_get_mode();
3819*4882a593Smuzhiyun 			if (current_mode < 0)
3820*4882a593Smuzhiyun 				return false;
3821*4882a593Smuzhiyun 			new_mode = adaptive_keyboard_get_next_mode(
3822*4882a593Smuzhiyun 					current_mode);
3823*4882a593Smuzhiyun 		}
3824*4882a593Smuzhiyun 
3825*4882a593Smuzhiyun 		if (adaptive_keyboard_set_mode(new_mode) < 0)
3826*4882a593Smuzhiyun 			return false;
3827*4882a593Smuzhiyun 
3828*4882a593Smuzhiyun 		return true;
3829*4882a593Smuzhiyun 
3830*4882a593Smuzhiyun 	case DFR_SHOW_QUICKVIEW_ROW:
3831*4882a593Smuzhiyun 		current_mode = adaptive_keyboard_get_mode();
3832*4882a593Smuzhiyun 		if (current_mode < 0)
3833*4882a593Smuzhiyun 			return false;
3834*4882a593Smuzhiyun 
3835*4882a593Smuzhiyun 		adaptive_keyboard_prev_mode = current_mode;
3836*4882a593Smuzhiyun 		adaptive_keyboard_mode_is_saved = true;
3837*4882a593Smuzhiyun 
3838*4882a593Smuzhiyun 		if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3839*4882a593Smuzhiyun 			return false;
3840*4882a593Smuzhiyun 		return true;
3841*4882a593Smuzhiyun 
3842*4882a593Smuzhiyun 	default:
3843*4882a593Smuzhiyun 		if (scancode < FIRST_ADAPTIVE_KEY ||
3844*4882a593Smuzhiyun 		    scancode >= FIRST_ADAPTIVE_KEY +
3845*4882a593Smuzhiyun 		    TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3846*4882a593Smuzhiyun 		    TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3847*4882a593Smuzhiyun 			pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3848*4882a593Smuzhiyun 				scancode);
3849*4882a593Smuzhiyun 			return false;
3850*4882a593Smuzhiyun 		}
3851*4882a593Smuzhiyun 		keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3852*4882a593Smuzhiyun 					     TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3853*4882a593Smuzhiyun 		if (keycode != KEY_RESERVED) {
3854*4882a593Smuzhiyun 			mutex_lock(&tpacpi_inputdev_send_mutex);
3855*4882a593Smuzhiyun 
3856*4882a593Smuzhiyun 			input_report_key(tpacpi_inputdev, keycode, 1);
3857*4882a593Smuzhiyun 			input_sync(tpacpi_inputdev);
3858*4882a593Smuzhiyun 
3859*4882a593Smuzhiyun 			input_report_key(tpacpi_inputdev, keycode, 0);
3860*4882a593Smuzhiyun 			input_sync(tpacpi_inputdev);
3861*4882a593Smuzhiyun 
3862*4882a593Smuzhiyun 			mutex_unlock(&tpacpi_inputdev_send_mutex);
3863*4882a593Smuzhiyun 		}
3864*4882a593Smuzhiyun 		return true;
3865*4882a593Smuzhiyun 	}
3866*4882a593Smuzhiyun }
3867*4882a593Smuzhiyun 
hotkey_notify_hotkey(const u32 hkey,bool * send_acpi_ev,bool * ignore_acpi_ev)3868*4882a593Smuzhiyun static bool hotkey_notify_hotkey(const u32 hkey,
3869*4882a593Smuzhiyun 				 bool *send_acpi_ev,
3870*4882a593Smuzhiyun 				 bool *ignore_acpi_ev)
3871*4882a593Smuzhiyun {
3872*4882a593Smuzhiyun 	/* 0x1000-0x1FFF: key presses */
3873*4882a593Smuzhiyun 	unsigned int scancode = hkey & 0xfff;
3874*4882a593Smuzhiyun 	*send_acpi_ev = true;
3875*4882a593Smuzhiyun 	*ignore_acpi_ev = false;
3876*4882a593Smuzhiyun 
3877*4882a593Smuzhiyun 	/*
3878*4882a593Smuzhiyun 	 * Original events are in the 0x10XX range, the adaptive keyboard
3879*4882a593Smuzhiyun 	 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3880*4882a593Smuzhiyun 	 * models, additional keys are emitted through 0x13XX.
3881*4882a593Smuzhiyun 	 */
3882*4882a593Smuzhiyun 	switch ((hkey >> 8) & 0xf) {
3883*4882a593Smuzhiyun 	case 0:
3884*4882a593Smuzhiyun 		if (scancode > 0 &&
3885*4882a593Smuzhiyun 		    scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3886*4882a593Smuzhiyun 			/* HKEY event 0x1001 is scancode 0x00 */
3887*4882a593Smuzhiyun 			scancode--;
3888*4882a593Smuzhiyun 			if (!(hotkey_source_mask & (1 << scancode))) {
3889*4882a593Smuzhiyun 				tpacpi_input_send_key_masked(scancode);
3890*4882a593Smuzhiyun 				*send_acpi_ev = false;
3891*4882a593Smuzhiyun 			} else {
3892*4882a593Smuzhiyun 				*ignore_acpi_ev = true;
3893*4882a593Smuzhiyun 			}
3894*4882a593Smuzhiyun 			return true;
3895*4882a593Smuzhiyun 		}
3896*4882a593Smuzhiyun 		break;
3897*4882a593Smuzhiyun 
3898*4882a593Smuzhiyun 	case 1:
3899*4882a593Smuzhiyun 		return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3900*4882a593Smuzhiyun 
3901*4882a593Smuzhiyun 	case 3:
3902*4882a593Smuzhiyun 		/* Extended keycodes start at 0x300 and our offset into the map
3903*4882a593Smuzhiyun 		 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3904*4882a593Smuzhiyun 		 * will be positive, but might not be in the correct range.
3905*4882a593Smuzhiyun 		 */
3906*4882a593Smuzhiyun 		scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3907*4882a593Smuzhiyun 		if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3908*4882a593Smuzhiyun 		    scancode < TPACPI_HOTKEY_MAP_LEN) {
3909*4882a593Smuzhiyun 			tpacpi_input_send_key(scancode);
3910*4882a593Smuzhiyun 			return true;
3911*4882a593Smuzhiyun 		}
3912*4882a593Smuzhiyun 		break;
3913*4882a593Smuzhiyun 	}
3914*4882a593Smuzhiyun 
3915*4882a593Smuzhiyun 	return false;
3916*4882a593Smuzhiyun }
3917*4882a593Smuzhiyun 
hotkey_notify_wakeup(const u32 hkey,bool * send_acpi_ev,bool * ignore_acpi_ev)3918*4882a593Smuzhiyun static bool hotkey_notify_wakeup(const u32 hkey,
3919*4882a593Smuzhiyun 				 bool *send_acpi_ev,
3920*4882a593Smuzhiyun 				 bool *ignore_acpi_ev)
3921*4882a593Smuzhiyun {
3922*4882a593Smuzhiyun 	/* 0x2000-0x2FFF: Wakeup reason */
3923*4882a593Smuzhiyun 	*send_acpi_ev = true;
3924*4882a593Smuzhiyun 	*ignore_acpi_ev = false;
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun 	switch (hkey) {
3927*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3928*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3929*4882a593Smuzhiyun 		hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3930*4882a593Smuzhiyun 		*ignore_acpi_ev = true;
3931*4882a593Smuzhiyun 		break;
3932*4882a593Smuzhiyun 
3933*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3934*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3935*4882a593Smuzhiyun 		hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3936*4882a593Smuzhiyun 		*ignore_acpi_ev = true;
3937*4882a593Smuzhiyun 		break;
3938*4882a593Smuzhiyun 
3939*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3940*4882a593Smuzhiyun 	case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3941*4882a593Smuzhiyun 		pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3942*4882a593Smuzhiyun 		/* how to auto-heal: */
3943*4882a593Smuzhiyun 		/* 2313: woke up from S3, go to S4/S5 */
3944*4882a593Smuzhiyun 		/* 2413: woke up from S4, go to S5 */
3945*4882a593Smuzhiyun 		break;
3946*4882a593Smuzhiyun 
3947*4882a593Smuzhiyun 	default:
3948*4882a593Smuzhiyun 		return false;
3949*4882a593Smuzhiyun 	}
3950*4882a593Smuzhiyun 
3951*4882a593Smuzhiyun 	if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3952*4882a593Smuzhiyun 		pr_info("woke up due to a hot-unplug request...\n");
3953*4882a593Smuzhiyun 		hotkey_wakeup_reason_notify_change();
3954*4882a593Smuzhiyun 	}
3955*4882a593Smuzhiyun 	return true;
3956*4882a593Smuzhiyun }
3957*4882a593Smuzhiyun 
hotkey_notify_dockevent(const u32 hkey,bool * send_acpi_ev,bool * ignore_acpi_ev)3958*4882a593Smuzhiyun static bool hotkey_notify_dockevent(const u32 hkey,
3959*4882a593Smuzhiyun 				 bool *send_acpi_ev,
3960*4882a593Smuzhiyun 				 bool *ignore_acpi_ev)
3961*4882a593Smuzhiyun {
3962*4882a593Smuzhiyun 	/* 0x4000-0x4FFF: dock-related events */
3963*4882a593Smuzhiyun 	*send_acpi_ev = true;
3964*4882a593Smuzhiyun 	*ignore_acpi_ev = false;
3965*4882a593Smuzhiyun 
3966*4882a593Smuzhiyun 	switch (hkey) {
3967*4882a593Smuzhiyun 	case TP_HKEY_EV_UNDOCK_ACK:
3968*4882a593Smuzhiyun 		/* ACPI undock operation completed after wakeup */
3969*4882a593Smuzhiyun 		hotkey_autosleep_ack = 1;
3970*4882a593Smuzhiyun 		pr_info("undocked\n");
3971*4882a593Smuzhiyun 		hotkey_wakeup_hotunplug_complete_notify_change();
3972*4882a593Smuzhiyun 		return true;
3973*4882a593Smuzhiyun 
3974*4882a593Smuzhiyun 	case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3975*4882a593Smuzhiyun 		pr_info("docked into hotplug port replicator\n");
3976*4882a593Smuzhiyun 		return true;
3977*4882a593Smuzhiyun 	case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3978*4882a593Smuzhiyun 		pr_info("undocked from hotplug port replicator\n");
3979*4882a593Smuzhiyun 		return true;
3980*4882a593Smuzhiyun 
3981*4882a593Smuzhiyun 	default:
3982*4882a593Smuzhiyun 		return false;
3983*4882a593Smuzhiyun 	}
3984*4882a593Smuzhiyun }
3985*4882a593Smuzhiyun 
hotkey_notify_usrevent(const u32 hkey,bool * send_acpi_ev,bool * ignore_acpi_ev)3986*4882a593Smuzhiyun static bool hotkey_notify_usrevent(const u32 hkey,
3987*4882a593Smuzhiyun 				 bool *send_acpi_ev,
3988*4882a593Smuzhiyun 				 bool *ignore_acpi_ev)
3989*4882a593Smuzhiyun {
3990*4882a593Smuzhiyun 	/* 0x5000-0x5FFF: human interface helpers */
3991*4882a593Smuzhiyun 	*send_acpi_ev = true;
3992*4882a593Smuzhiyun 	*ignore_acpi_ev = false;
3993*4882a593Smuzhiyun 
3994*4882a593Smuzhiyun 	switch (hkey) {
3995*4882a593Smuzhiyun 	case TP_HKEY_EV_PEN_INSERTED:  /* X61t: tablet pen inserted into bay */
3996*4882a593Smuzhiyun 	case TP_HKEY_EV_PEN_REMOVED:   /* X61t: tablet pen removed from bay */
3997*4882a593Smuzhiyun 		return true;
3998*4882a593Smuzhiyun 
3999*4882a593Smuzhiyun 	case TP_HKEY_EV_TABLET_TABLET:   /* X41t-X61t: tablet mode */
4000*4882a593Smuzhiyun 	case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4001*4882a593Smuzhiyun 		tpacpi_input_send_tabletsw();
4002*4882a593Smuzhiyun 		hotkey_tablet_mode_notify_change();
4003*4882a593Smuzhiyun 		*send_acpi_ev = false;
4004*4882a593Smuzhiyun 		return true;
4005*4882a593Smuzhiyun 
4006*4882a593Smuzhiyun 	case TP_HKEY_EV_LID_CLOSE:	/* Lid closed */
4007*4882a593Smuzhiyun 	case TP_HKEY_EV_LID_OPEN:	/* Lid opened */
4008*4882a593Smuzhiyun 	case TP_HKEY_EV_BRGHT_CHANGED:	/* brightness changed */
4009*4882a593Smuzhiyun 		/* do not propagate these events */
4010*4882a593Smuzhiyun 		*ignore_acpi_ev = true;
4011*4882a593Smuzhiyun 		return true;
4012*4882a593Smuzhiyun 
4013*4882a593Smuzhiyun 	default:
4014*4882a593Smuzhiyun 		return false;
4015*4882a593Smuzhiyun 	}
4016*4882a593Smuzhiyun }
4017*4882a593Smuzhiyun 
4018*4882a593Smuzhiyun static void thermal_dump_all_sensors(void);
4019*4882a593Smuzhiyun 
hotkey_notify_6xxx(const u32 hkey,bool * send_acpi_ev,bool * ignore_acpi_ev)4020*4882a593Smuzhiyun static bool hotkey_notify_6xxx(const u32 hkey,
4021*4882a593Smuzhiyun 				 bool *send_acpi_ev,
4022*4882a593Smuzhiyun 				 bool *ignore_acpi_ev)
4023*4882a593Smuzhiyun {
4024*4882a593Smuzhiyun 	/* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4025*4882a593Smuzhiyun 	*send_acpi_ev = true;
4026*4882a593Smuzhiyun 	*ignore_acpi_ev = false;
4027*4882a593Smuzhiyun 
4028*4882a593Smuzhiyun 	switch (hkey) {
4029*4882a593Smuzhiyun 	case TP_HKEY_EV_THM_TABLE_CHANGED:
4030*4882a593Smuzhiyun 		pr_debug("EC reports: Thermal Table has changed\n");
4031*4882a593Smuzhiyun 		/* recommended action: do nothing, we don't have
4032*4882a593Smuzhiyun 		 * Lenovo ATM information */
4033*4882a593Smuzhiyun 		return true;
4034*4882a593Smuzhiyun 	case TP_HKEY_EV_THM_CSM_COMPLETED:
4035*4882a593Smuzhiyun 		pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4036*4882a593Smuzhiyun 		/* Thermal event - pass on to event handler */
4037*4882a593Smuzhiyun 		tpacpi_driver_event(hkey);
4038*4882a593Smuzhiyun 		return true;
4039*4882a593Smuzhiyun 	case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4040*4882a593Smuzhiyun 		pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
4041*4882a593Smuzhiyun 		/* recommended action: do nothing, we don't have
4042*4882a593Smuzhiyun 		 * Lenovo ATM information */
4043*4882a593Smuzhiyun 		return true;
4044*4882a593Smuzhiyun 	case TP_HKEY_EV_ALARM_BAT_HOT:
4045*4882a593Smuzhiyun 		pr_crit("THERMAL ALARM: battery is too hot!\n");
4046*4882a593Smuzhiyun 		/* recommended action: warn user through gui */
4047*4882a593Smuzhiyun 		break;
4048*4882a593Smuzhiyun 	case TP_HKEY_EV_ALARM_BAT_XHOT:
4049*4882a593Smuzhiyun 		pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4050*4882a593Smuzhiyun 		/* recommended action: immediate sleep/hibernate */
4051*4882a593Smuzhiyun 		break;
4052*4882a593Smuzhiyun 	case TP_HKEY_EV_ALARM_SENSOR_HOT:
4053*4882a593Smuzhiyun 		pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4054*4882a593Smuzhiyun 		/* recommended action: warn user through gui, that */
4055*4882a593Smuzhiyun 		/* some internal component is too hot */
4056*4882a593Smuzhiyun 		break;
4057*4882a593Smuzhiyun 	case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4058*4882a593Smuzhiyun 		pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4059*4882a593Smuzhiyun 		/* recommended action: immediate sleep/hibernate */
4060*4882a593Smuzhiyun 		break;
4061*4882a593Smuzhiyun 	case TP_HKEY_EV_AC_CHANGED:
4062*4882a593Smuzhiyun 		/* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4063*4882a593Smuzhiyun 		 * AC status changed; can be triggered by plugging or
4064*4882a593Smuzhiyun 		 * unplugging AC adapter, docking or undocking. */
4065*4882a593Smuzhiyun 
4066*4882a593Smuzhiyun 		fallthrough;
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	case TP_HKEY_EV_KEY_NUMLOCK:
4069*4882a593Smuzhiyun 	case TP_HKEY_EV_KEY_FN:
4070*4882a593Smuzhiyun 		/* key press events, we just ignore them as long as the EC
4071*4882a593Smuzhiyun 		 * is still reporting them in the normal keyboard stream */
4072*4882a593Smuzhiyun 		*send_acpi_ev = false;
4073*4882a593Smuzhiyun 		*ignore_acpi_ev = true;
4074*4882a593Smuzhiyun 		return true;
4075*4882a593Smuzhiyun 
4076*4882a593Smuzhiyun 	case TP_HKEY_EV_KEY_FN_ESC:
4077*4882a593Smuzhiyun 		/* Get the media key status to foce the status LED to update */
4078*4882a593Smuzhiyun 		acpi_evalf(hkey_handle, NULL, "GMKS", "v");
4079*4882a593Smuzhiyun 		*send_acpi_ev = false;
4080*4882a593Smuzhiyun 		*ignore_acpi_ev = true;
4081*4882a593Smuzhiyun 		return true;
4082*4882a593Smuzhiyun 
4083*4882a593Smuzhiyun 	case TP_HKEY_EV_TABLET_CHANGED:
4084*4882a593Smuzhiyun 		tpacpi_input_send_tabletsw();
4085*4882a593Smuzhiyun 		hotkey_tablet_mode_notify_change();
4086*4882a593Smuzhiyun 		*send_acpi_ev = false;
4087*4882a593Smuzhiyun 		return true;
4088*4882a593Smuzhiyun 
4089*4882a593Smuzhiyun 	case TP_HKEY_EV_PALM_DETECTED:
4090*4882a593Smuzhiyun 	case TP_HKEY_EV_PALM_UNDETECTED:
4091*4882a593Smuzhiyun 		/* palm detected hovering the keyboard, forward to user-space
4092*4882a593Smuzhiyun 		 * via netlink for consumption */
4093*4882a593Smuzhiyun 		return true;
4094*4882a593Smuzhiyun 
4095*4882a593Smuzhiyun 	default:
4096*4882a593Smuzhiyun 		/* report simply as unknown, no sensor dump */
4097*4882a593Smuzhiyun 		return false;
4098*4882a593Smuzhiyun 	}
4099*4882a593Smuzhiyun 
4100*4882a593Smuzhiyun 	thermal_dump_all_sensors();
4101*4882a593Smuzhiyun 	return true;
4102*4882a593Smuzhiyun }
4103*4882a593Smuzhiyun 
hotkey_notify(struct ibm_struct * ibm,u32 event)4104*4882a593Smuzhiyun static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4105*4882a593Smuzhiyun {
4106*4882a593Smuzhiyun 	u32 hkey;
4107*4882a593Smuzhiyun 	bool send_acpi_ev;
4108*4882a593Smuzhiyun 	bool ignore_acpi_ev;
4109*4882a593Smuzhiyun 	bool known_ev;
4110*4882a593Smuzhiyun 
4111*4882a593Smuzhiyun 	if (event != 0x80) {
4112*4882a593Smuzhiyun 		pr_err("unknown HKEY notification event %d\n", event);
4113*4882a593Smuzhiyun 		/* forward it to userspace, maybe it knows how to handle it */
4114*4882a593Smuzhiyun 		acpi_bus_generate_netlink_event(
4115*4882a593Smuzhiyun 					ibm->acpi->device->pnp.device_class,
4116*4882a593Smuzhiyun 					dev_name(&ibm->acpi->device->dev),
4117*4882a593Smuzhiyun 					event, 0);
4118*4882a593Smuzhiyun 		return;
4119*4882a593Smuzhiyun 	}
4120*4882a593Smuzhiyun 
4121*4882a593Smuzhiyun 	while (1) {
4122*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4123*4882a593Smuzhiyun 			pr_err("failed to retrieve HKEY event\n");
4124*4882a593Smuzhiyun 			return;
4125*4882a593Smuzhiyun 		}
4126*4882a593Smuzhiyun 
4127*4882a593Smuzhiyun 		if (hkey == 0) {
4128*4882a593Smuzhiyun 			/* queue empty */
4129*4882a593Smuzhiyun 			return;
4130*4882a593Smuzhiyun 		}
4131*4882a593Smuzhiyun 
4132*4882a593Smuzhiyun 		send_acpi_ev = true;
4133*4882a593Smuzhiyun 		ignore_acpi_ev = false;
4134*4882a593Smuzhiyun 
4135*4882a593Smuzhiyun 		switch (hkey >> 12) {
4136*4882a593Smuzhiyun 		case 1:
4137*4882a593Smuzhiyun 			/* 0x1000-0x1FFF: key presses */
4138*4882a593Smuzhiyun 			known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4139*4882a593Smuzhiyun 						 &ignore_acpi_ev);
4140*4882a593Smuzhiyun 			break;
4141*4882a593Smuzhiyun 		case 2:
4142*4882a593Smuzhiyun 			/* 0x2000-0x2FFF: Wakeup reason */
4143*4882a593Smuzhiyun 			known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4144*4882a593Smuzhiyun 						 &ignore_acpi_ev);
4145*4882a593Smuzhiyun 			break;
4146*4882a593Smuzhiyun 		case 3:
4147*4882a593Smuzhiyun 			/* 0x3000-0x3FFF: bay-related wakeups */
4148*4882a593Smuzhiyun 			switch (hkey) {
4149*4882a593Smuzhiyun 			case TP_HKEY_EV_BAYEJ_ACK:
4150*4882a593Smuzhiyun 				hotkey_autosleep_ack = 1;
4151*4882a593Smuzhiyun 				pr_info("bay ejected\n");
4152*4882a593Smuzhiyun 				hotkey_wakeup_hotunplug_complete_notify_change();
4153*4882a593Smuzhiyun 				known_ev = true;
4154*4882a593Smuzhiyun 				break;
4155*4882a593Smuzhiyun 			case TP_HKEY_EV_OPTDRV_EJ:
4156*4882a593Smuzhiyun 				/* FIXME: kick libata if SATA link offline */
4157*4882a593Smuzhiyun 				known_ev = true;
4158*4882a593Smuzhiyun 				break;
4159*4882a593Smuzhiyun 			default:
4160*4882a593Smuzhiyun 				known_ev = false;
4161*4882a593Smuzhiyun 			}
4162*4882a593Smuzhiyun 			break;
4163*4882a593Smuzhiyun 		case 4:
4164*4882a593Smuzhiyun 			/* 0x4000-0x4FFF: dock-related events */
4165*4882a593Smuzhiyun 			known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4166*4882a593Smuzhiyun 						&ignore_acpi_ev);
4167*4882a593Smuzhiyun 			break;
4168*4882a593Smuzhiyun 		case 5:
4169*4882a593Smuzhiyun 			/* 0x5000-0x5FFF: human interface helpers */
4170*4882a593Smuzhiyun 			known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4171*4882a593Smuzhiyun 						 &ignore_acpi_ev);
4172*4882a593Smuzhiyun 			break;
4173*4882a593Smuzhiyun 		case 6:
4174*4882a593Smuzhiyun 			/* 0x6000-0x6FFF: thermal alarms/notices and
4175*4882a593Smuzhiyun 			 *                keyboard events */
4176*4882a593Smuzhiyun 			known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4177*4882a593Smuzhiyun 						 &ignore_acpi_ev);
4178*4882a593Smuzhiyun 			break;
4179*4882a593Smuzhiyun 		case 7:
4180*4882a593Smuzhiyun 			/* 0x7000-0x7FFF: misc */
4181*4882a593Smuzhiyun 			if (tp_features.hotkey_wlsw &&
4182*4882a593Smuzhiyun 					hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4183*4882a593Smuzhiyun 				tpacpi_send_radiosw_update();
4184*4882a593Smuzhiyun 				send_acpi_ev = 0;
4185*4882a593Smuzhiyun 				known_ev = true;
4186*4882a593Smuzhiyun 				break;
4187*4882a593Smuzhiyun 			}
4188*4882a593Smuzhiyun 			fallthrough;	/* to default */
4189*4882a593Smuzhiyun 		default:
4190*4882a593Smuzhiyun 			known_ev = false;
4191*4882a593Smuzhiyun 		}
4192*4882a593Smuzhiyun 		if (!known_ev) {
4193*4882a593Smuzhiyun 			pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4194*4882a593Smuzhiyun 			pr_notice("please report the conditions when this event happened to %s\n",
4195*4882a593Smuzhiyun 				  TPACPI_MAIL);
4196*4882a593Smuzhiyun 		}
4197*4882a593Smuzhiyun 
4198*4882a593Smuzhiyun 		/* netlink events */
4199*4882a593Smuzhiyun 		if (!ignore_acpi_ev && send_acpi_ev) {
4200*4882a593Smuzhiyun 			acpi_bus_generate_netlink_event(
4201*4882a593Smuzhiyun 					ibm->acpi->device->pnp.device_class,
4202*4882a593Smuzhiyun 					dev_name(&ibm->acpi->device->dev),
4203*4882a593Smuzhiyun 					event, hkey);
4204*4882a593Smuzhiyun 		}
4205*4882a593Smuzhiyun 	}
4206*4882a593Smuzhiyun }
4207*4882a593Smuzhiyun 
hotkey_suspend(void)4208*4882a593Smuzhiyun static void hotkey_suspend(void)
4209*4882a593Smuzhiyun {
4210*4882a593Smuzhiyun 	/* Do these on suspend, we get the events on early resume! */
4211*4882a593Smuzhiyun 	hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4212*4882a593Smuzhiyun 	hotkey_autosleep_ack = 0;
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun 	/* save previous mode of adaptive keyboard of X1 Carbon */
4215*4882a593Smuzhiyun 	if (tp_features.has_adaptive_kbd) {
4216*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4217*4882a593Smuzhiyun 					"GTRW", "dd", 0)) {
4218*4882a593Smuzhiyun 			pr_err("Cannot read adaptive keyboard mode.\n");
4219*4882a593Smuzhiyun 		}
4220*4882a593Smuzhiyun 	}
4221*4882a593Smuzhiyun }
4222*4882a593Smuzhiyun 
hotkey_resume(void)4223*4882a593Smuzhiyun static void hotkey_resume(void)
4224*4882a593Smuzhiyun {
4225*4882a593Smuzhiyun 	tpacpi_disable_brightness_delay();
4226*4882a593Smuzhiyun 
4227*4882a593Smuzhiyun 	if (hotkey_status_set(true) < 0 ||
4228*4882a593Smuzhiyun 	    hotkey_mask_set(hotkey_acpi_mask) < 0)
4229*4882a593Smuzhiyun 		pr_err("error while attempting to reset the event firmware interface\n");
4230*4882a593Smuzhiyun 
4231*4882a593Smuzhiyun 	tpacpi_send_radiosw_update();
4232*4882a593Smuzhiyun 	tpacpi_input_send_tabletsw();
4233*4882a593Smuzhiyun 	hotkey_tablet_mode_notify_change();
4234*4882a593Smuzhiyun 	hotkey_wakeup_reason_notify_change();
4235*4882a593Smuzhiyun 	hotkey_wakeup_hotunplug_complete_notify_change();
4236*4882a593Smuzhiyun 	hotkey_poll_setup_safe(false);
4237*4882a593Smuzhiyun 
4238*4882a593Smuzhiyun 	/* restore previous mode of adapive keyboard of X1 Carbon */
4239*4882a593Smuzhiyun 	if (tp_features.has_adaptive_kbd) {
4240*4882a593Smuzhiyun 		if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4241*4882a593Smuzhiyun 					adaptive_keyboard_prev_mode)) {
4242*4882a593Smuzhiyun 			pr_err("Cannot set adaptive keyboard mode.\n");
4243*4882a593Smuzhiyun 		}
4244*4882a593Smuzhiyun 	}
4245*4882a593Smuzhiyun }
4246*4882a593Smuzhiyun 
4247*4882a593Smuzhiyun /* procfs -------------------------------------------------------------- */
hotkey_read(struct seq_file * m)4248*4882a593Smuzhiyun static int hotkey_read(struct seq_file *m)
4249*4882a593Smuzhiyun {
4250*4882a593Smuzhiyun 	int res, status;
4251*4882a593Smuzhiyun 
4252*4882a593Smuzhiyun 	if (!tp_features.hotkey) {
4253*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
4254*4882a593Smuzhiyun 		return 0;
4255*4882a593Smuzhiyun 	}
4256*4882a593Smuzhiyun 
4257*4882a593Smuzhiyun 	if (mutex_lock_killable(&hotkey_mutex))
4258*4882a593Smuzhiyun 		return -ERESTARTSYS;
4259*4882a593Smuzhiyun 	res = hotkey_status_get(&status);
4260*4882a593Smuzhiyun 	if (!res)
4261*4882a593Smuzhiyun 		res = hotkey_mask_get();
4262*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
4263*4882a593Smuzhiyun 	if (res)
4264*4882a593Smuzhiyun 		return res;
4265*4882a593Smuzhiyun 
4266*4882a593Smuzhiyun 	seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4267*4882a593Smuzhiyun 	if (hotkey_all_mask) {
4268*4882a593Smuzhiyun 		seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4269*4882a593Smuzhiyun 		seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4270*4882a593Smuzhiyun 	} else {
4271*4882a593Smuzhiyun 		seq_printf(m, "mask:\t\tnot supported\n");
4272*4882a593Smuzhiyun 		seq_printf(m, "commands:\tenable, disable, reset\n");
4273*4882a593Smuzhiyun 	}
4274*4882a593Smuzhiyun 
4275*4882a593Smuzhiyun 	return 0;
4276*4882a593Smuzhiyun }
4277*4882a593Smuzhiyun 
hotkey_enabledisable_warn(bool enable)4278*4882a593Smuzhiyun static void hotkey_enabledisable_warn(bool enable)
4279*4882a593Smuzhiyun {
4280*4882a593Smuzhiyun 	tpacpi_log_usertask("procfs hotkey enable/disable");
4281*4882a593Smuzhiyun 	if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4282*4882a593Smuzhiyun 		  pr_fmt("hotkey enable/disable functionality has been removed from the driver.  Hotkeys are always enabled.\n")))
4283*4882a593Smuzhiyun 		pr_err("Please remove the hotkey=enable module parameter, it is deprecated.  Hotkeys are always enabled.\n");
4284*4882a593Smuzhiyun }
4285*4882a593Smuzhiyun 
hotkey_write(char * buf)4286*4882a593Smuzhiyun static int hotkey_write(char *buf)
4287*4882a593Smuzhiyun {
4288*4882a593Smuzhiyun 	int res;
4289*4882a593Smuzhiyun 	u32 mask;
4290*4882a593Smuzhiyun 	char *cmd;
4291*4882a593Smuzhiyun 
4292*4882a593Smuzhiyun 	if (!tp_features.hotkey)
4293*4882a593Smuzhiyun 		return -ENODEV;
4294*4882a593Smuzhiyun 
4295*4882a593Smuzhiyun 	if (mutex_lock_killable(&hotkey_mutex))
4296*4882a593Smuzhiyun 		return -ERESTARTSYS;
4297*4882a593Smuzhiyun 
4298*4882a593Smuzhiyun 	mask = hotkey_user_mask;
4299*4882a593Smuzhiyun 
4300*4882a593Smuzhiyun 	res = 0;
4301*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
4302*4882a593Smuzhiyun 		if (strlencmp(cmd, "enable") == 0) {
4303*4882a593Smuzhiyun 			hotkey_enabledisable_warn(1);
4304*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "disable") == 0) {
4305*4882a593Smuzhiyun 			hotkey_enabledisable_warn(0);
4306*4882a593Smuzhiyun 			res = -EPERM;
4307*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "reset") == 0) {
4308*4882a593Smuzhiyun 			mask = (hotkey_all_mask | hotkey_source_mask)
4309*4882a593Smuzhiyun 				& ~hotkey_reserved_mask;
4310*4882a593Smuzhiyun 		} else if (sscanf(cmd, "0x%x", &mask) == 1) {
4311*4882a593Smuzhiyun 			/* mask set */
4312*4882a593Smuzhiyun 		} else if (sscanf(cmd, "%x", &mask) == 1) {
4313*4882a593Smuzhiyun 			/* mask set */
4314*4882a593Smuzhiyun 		} else {
4315*4882a593Smuzhiyun 			res = -EINVAL;
4316*4882a593Smuzhiyun 			goto errexit;
4317*4882a593Smuzhiyun 		}
4318*4882a593Smuzhiyun 	}
4319*4882a593Smuzhiyun 
4320*4882a593Smuzhiyun 	if (!res) {
4321*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs hotkey",
4322*4882a593Smuzhiyun 			"set mask to 0x%08x\n", mask);
4323*4882a593Smuzhiyun 		res = hotkey_user_mask_set(mask);
4324*4882a593Smuzhiyun 	}
4325*4882a593Smuzhiyun 
4326*4882a593Smuzhiyun errexit:
4327*4882a593Smuzhiyun 	mutex_unlock(&hotkey_mutex);
4328*4882a593Smuzhiyun 	return res;
4329*4882a593Smuzhiyun }
4330*4882a593Smuzhiyun 
4331*4882a593Smuzhiyun static const struct acpi_device_id ibm_htk_device_ids[] = {
4332*4882a593Smuzhiyun 	{TPACPI_ACPI_IBM_HKEY_HID, 0},
4333*4882a593Smuzhiyun 	{TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4334*4882a593Smuzhiyun 	{TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4335*4882a593Smuzhiyun 	{"", 0},
4336*4882a593Smuzhiyun };
4337*4882a593Smuzhiyun 
4338*4882a593Smuzhiyun static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4339*4882a593Smuzhiyun 	.hid = ibm_htk_device_ids,
4340*4882a593Smuzhiyun 	.notify = hotkey_notify,
4341*4882a593Smuzhiyun 	.handle = &hkey_handle,
4342*4882a593Smuzhiyun 	.type = ACPI_DEVICE_NOTIFY,
4343*4882a593Smuzhiyun };
4344*4882a593Smuzhiyun 
4345*4882a593Smuzhiyun static struct ibm_struct hotkey_driver_data = {
4346*4882a593Smuzhiyun 	.name = "hotkey",
4347*4882a593Smuzhiyun 	.read = hotkey_read,
4348*4882a593Smuzhiyun 	.write = hotkey_write,
4349*4882a593Smuzhiyun 	.exit = hotkey_exit,
4350*4882a593Smuzhiyun 	.resume = hotkey_resume,
4351*4882a593Smuzhiyun 	.suspend = hotkey_suspend,
4352*4882a593Smuzhiyun 	.acpi = &ibm_hotkey_acpidriver,
4353*4882a593Smuzhiyun };
4354*4882a593Smuzhiyun 
4355*4882a593Smuzhiyun /*************************************************************************
4356*4882a593Smuzhiyun  * Bluetooth subdriver
4357*4882a593Smuzhiyun  */
4358*4882a593Smuzhiyun 
4359*4882a593Smuzhiyun enum {
4360*4882a593Smuzhiyun 	/* ACPI GBDC/SBDC bits */
4361*4882a593Smuzhiyun 	TP_ACPI_BLUETOOTH_HWPRESENT	= 0x01,	/* Bluetooth hw available */
4362*4882a593Smuzhiyun 	TP_ACPI_BLUETOOTH_RADIOSSW	= 0x02,	/* Bluetooth radio enabled */
4363*4882a593Smuzhiyun 	TP_ACPI_BLUETOOTH_RESUMECTRL	= 0x04,	/* Bluetooth state at resume:
4364*4882a593Smuzhiyun 						   0 = disable, 1 = enable */
4365*4882a593Smuzhiyun };
4366*4882a593Smuzhiyun 
4367*4882a593Smuzhiyun enum {
4368*4882a593Smuzhiyun 	/* ACPI \BLTH commands */
4369*4882a593Smuzhiyun 	TP_ACPI_BLTH_GET_ULTRAPORT_ID	= 0x00, /* Get Ultraport BT ID */
4370*4882a593Smuzhiyun 	TP_ACPI_BLTH_GET_PWR_ON_RESUME	= 0x01, /* Get power-on-resume state */
4371*4882a593Smuzhiyun 	TP_ACPI_BLTH_PWR_ON_ON_RESUME	= 0x02, /* Resume powered on */
4372*4882a593Smuzhiyun 	TP_ACPI_BLTH_PWR_OFF_ON_RESUME	= 0x03,	/* Resume powered off */
4373*4882a593Smuzhiyun 	TP_ACPI_BLTH_SAVE_STATE		= 0x05, /* Save state for S4/S5 */
4374*4882a593Smuzhiyun };
4375*4882a593Smuzhiyun 
4376*4882a593Smuzhiyun #define TPACPI_RFK_BLUETOOTH_SW_NAME	"tpacpi_bluetooth_sw"
4377*4882a593Smuzhiyun 
bluetooth_get_status(void)4378*4882a593Smuzhiyun static int bluetooth_get_status(void)
4379*4882a593Smuzhiyun {
4380*4882a593Smuzhiyun 	int status;
4381*4882a593Smuzhiyun 
4382*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4383*4882a593Smuzhiyun 	if (dbg_bluetoothemul)
4384*4882a593Smuzhiyun 		return (tpacpi_bluetooth_emulstate) ?
4385*4882a593Smuzhiyun 		       TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4386*4882a593Smuzhiyun #endif
4387*4882a593Smuzhiyun 
4388*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4389*4882a593Smuzhiyun 		return -EIO;
4390*4882a593Smuzhiyun 
4391*4882a593Smuzhiyun 	return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4392*4882a593Smuzhiyun 			TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4393*4882a593Smuzhiyun }
4394*4882a593Smuzhiyun 
bluetooth_set_status(enum tpacpi_rfkill_state state)4395*4882a593Smuzhiyun static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4396*4882a593Smuzhiyun {
4397*4882a593Smuzhiyun 	int status;
4398*4882a593Smuzhiyun 
4399*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_RFKILL,
4400*4882a593Smuzhiyun 		"will attempt to %s bluetooth\n",
4401*4882a593Smuzhiyun 		(state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4402*4882a593Smuzhiyun 
4403*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4404*4882a593Smuzhiyun 	if (dbg_bluetoothemul) {
4405*4882a593Smuzhiyun 		tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4406*4882a593Smuzhiyun 		return 0;
4407*4882a593Smuzhiyun 	}
4408*4882a593Smuzhiyun #endif
4409*4882a593Smuzhiyun 
4410*4882a593Smuzhiyun 	if (state == TPACPI_RFK_RADIO_ON)
4411*4882a593Smuzhiyun 		status = TP_ACPI_BLUETOOTH_RADIOSSW
4412*4882a593Smuzhiyun 			  | TP_ACPI_BLUETOOTH_RESUMECTRL;
4413*4882a593Smuzhiyun 	else
4414*4882a593Smuzhiyun 		status = 0;
4415*4882a593Smuzhiyun 
4416*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4417*4882a593Smuzhiyun 		return -EIO;
4418*4882a593Smuzhiyun 
4419*4882a593Smuzhiyun 	return 0;
4420*4882a593Smuzhiyun }
4421*4882a593Smuzhiyun 
4422*4882a593Smuzhiyun /* sysfs bluetooth enable ---------------------------------------------- */
bluetooth_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4423*4882a593Smuzhiyun static ssize_t bluetooth_enable_show(struct device *dev,
4424*4882a593Smuzhiyun 			   struct device_attribute *attr,
4425*4882a593Smuzhiyun 			   char *buf)
4426*4882a593Smuzhiyun {
4427*4882a593Smuzhiyun 	return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4428*4882a593Smuzhiyun 			attr, buf);
4429*4882a593Smuzhiyun }
4430*4882a593Smuzhiyun 
bluetooth_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4431*4882a593Smuzhiyun static ssize_t bluetooth_enable_store(struct device *dev,
4432*4882a593Smuzhiyun 			    struct device_attribute *attr,
4433*4882a593Smuzhiyun 			    const char *buf, size_t count)
4434*4882a593Smuzhiyun {
4435*4882a593Smuzhiyun 	return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4436*4882a593Smuzhiyun 				attr, buf, count);
4437*4882a593Smuzhiyun }
4438*4882a593Smuzhiyun 
4439*4882a593Smuzhiyun static DEVICE_ATTR_RW(bluetooth_enable);
4440*4882a593Smuzhiyun 
4441*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
4442*4882a593Smuzhiyun 
4443*4882a593Smuzhiyun static struct attribute *bluetooth_attributes[] = {
4444*4882a593Smuzhiyun 	&dev_attr_bluetooth_enable.attr,
4445*4882a593Smuzhiyun 	NULL
4446*4882a593Smuzhiyun };
4447*4882a593Smuzhiyun 
4448*4882a593Smuzhiyun static const struct attribute_group bluetooth_attr_group = {
4449*4882a593Smuzhiyun 	.attrs = bluetooth_attributes,
4450*4882a593Smuzhiyun };
4451*4882a593Smuzhiyun 
4452*4882a593Smuzhiyun static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4453*4882a593Smuzhiyun 	.get_status = bluetooth_get_status,
4454*4882a593Smuzhiyun 	.set_status = bluetooth_set_status,
4455*4882a593Smuzhiyun };
4456*4882a593Smuzhiyun 
bluetooth_shutdown(void)4457*4882a593Smuzhiyun static void bluetooth_shutdown(void)
4458*4882a593Smuzhiyun {
4459*4882a593Smuzhiyun 	/* Order firmware to save current state to NVRAM */
4460*4882a593Smuzhiyun 	if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4461*4882a593Smuzhiyun 			TP_ACPI_BLTH_SAVE_STATE))
4462*4882a593Smuzhiyun 		pr_notice("failed to save bluetooth state to NVRAM\n");
4463*4882a593Smuzhiyun 	else
4464*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_RFKILL,
4465*4882a593Smuzhiyun 			"bluetooth state saved to NVRAM\n");
4466*4882a593Smuzhiyun }
4467*4882a593Smuzhiyun 
bluetooth_exit(void)4468*4882a593Smuzhiyun static void bluetooth_exit(void)
4469*4882a593Smuzhiyun {
4470*4882a593Smuzhiyun 	sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4471*4882a593Smuzhiyun 			&bluetooth_attr_group);
4472*4882a593Smuzhiyun 
4473*4882a593Smuzhiyun 	tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4474*4882a593Smuzhiyun 
4475*4882a593Smuzhiyun 	bluetooth_shutdown();
4476*4882a593Smuzhiyun }
4477*4882a593Smuzhiyun 
4478*4882a593Smuzhiyun static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4479*4882a593Smuzhiyun 	{
4480*4882a593Smuzhiyun 		.ident = "ThinkPad E485",
4481*4882a593Smuzhiyun 		.matches = {
4482*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4483*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4484*4882a593Smuzhiyun 		},
4485*4882a593Smuzhiyun 	},
4486*4882a593Smuzhiyun 	{
4487*4882a593Smuzhiyun 		.ident = "ThinkPad E585",
4488*4882a593Smuzhiyun 		.matches = {
4489*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4490*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4491*4882a593Smuzhiyun 		},
4492*4882a593Smuzhiyun 	},
4493*4882a593Smuzhiyun 	{
4494*4882a593Smuzhiyun 		.ident = "ThinkPad A285 - 20MW",
4495*4882a593Smuzhiyun 		.matches = {
4496*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4497*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4498*4882a593Smuzhiyun 		},
4499*4882a593Smuzhiyun 	},
4500*4882a593Smuzhiyun 	{
4501*4882a593Smuzhiyun 		.ident = "ThinkPad A285 - 20MX",
4502*4882a593Smuzhiyun 		.matches = {
4503*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4504*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4505*4882a593Smuzhiyun 		},
4506*4882a593Smuzhiyun 	},
4507*4882a593Smuzhiyun 	{
4508*4882a593Smuzhiyun 		.ident = "ThinkPad A485 - 20MU",
4509*4882a593Smuzhiyun 		.matches = {
4510*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4511*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4512*4882a593Smuzhiyun 		},
4513*4882a593Smuzhiyun 	},
4514*4882a593Smuzhiyun 	{
4515*4882a593Smuzhiyun 		.ident = "ThinkPad A485 - 20MV",
4516*4882a593Smuzhiyun 		.matches = {
4517*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4518*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4519*4882a593Smuzhiyun 		},
4520*4882a593Smuzhiyun 	},
4521*4882a593Smuzhiyun 	{}
4522*4882a593Smuzhiyun };
4523*4882a593Smuzhiyun 
4524*4882a593Smuzhiyun static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4525*4882a593Smuzhiyun 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4526*4882a593Smuzhiyun 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4527*4882a593Smuzhiyun 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4528*4882a593Smuzhiyun 	{}
4529*4882a593Smuzhiyun };
4530*4882a593Smuzhiyun 
4531*4882a593Smuzhiyun 
have_bt_fwbug(void)4532*4882a593Smuzhiyun static int __init have_bt_fwbug(void)
4533*4882a593Smuzhiyun {
4534*4882a593Smuzhiyun 	/*
4535*4882a593Smuzhiyun 	 * Some AMD based ThinkPads have a firmware bug that calling
4536*4882a593Smuzhiyun 	 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4537*4882a593Smuzhiyun 	 */
4538*4882a593Smuzhiyun 	if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4539*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4540*4882a593Smuzhiyun 			FW_BUG "disable bluetooth subdriver for Intel cards\n");
4541*4882a593Smuzhiyun 		return 1;
4542*4882a593Smuzhiyun 	} else
4543*4882a593Smuzhiyun 		return 0;
4544*4882a593Smuzhiyun }
4545*4882a593Smuzhiyun 
bluetooth_init(struct ibm_init_struct * iibm)4546*4882a593Smuzhiyun static int __init bluetooth_init(struct ibm_init_struct *iibm)
4547*4882a593Smuzhiyun {
4548*4882a593Smuzhiyun 	int res;
4549*4882a593Smuzhiyun 	int status = 0;
4550*4882a593Smuzhiyun 
4551*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4552*4882a593Smuzhiyun 			"initializing bluetooth subdriver\n");
4553*4882a593Smuzhiyun 
4554*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(hkey);
4555*4882a593Smuzhiyun 
4556*4882a593Smuzhiyun 	/* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4557*4882a593Smuzhiyun 	   G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4558*4882a593Smuzhiyun 	tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4559*4882a593Smuzhiyun 	    acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4560*4882a593Smuzhiyun 
4561*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4562*4882a593Smuzhiyun 		"bluetooth is %s, status 0x%02x\n",
4563*4882a593Smuzhiyun 		str_supported(tp_features.bluetooth),
4564*4882a593Smuzhiyun 		status);
4565*4882a593Smuzhiyun 
4566*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4567*4882a593Smuzhiyun 	if (dbg_bluetoothemul) {
4568*4882a593Smuzhiyun 		tp_features.bluetooth = 1;
4569*4882a593Smuzhiyun 		pr_info("bluetooth switch emulation enabled\n");
4570*4882a593Smuzhiyun 	} else
4571*4882a593Smuzhiyun #endif
4572*4882a593Smuzhiyun 	if (tp_features.bluetooth &&
4573*4882a593Smuzhiyun 	    !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4574*4882a593Smuzhiyun 		/* no bluetooth hardware present in system */
4575*4882a593Smuzhiyun 		tp_features.bluetooth = 0;
4576*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4577*4882a593Smuzhiyun 			   "bluetooth hardware not installed\n");
4578*4882a593Smuzhiyun 	}
4579*4882a593Smuzhiyun 
4580*4882a593Smuzhiyun 	if (!tp_features.bluetooth)
4581*4882a593Smuzhiyun 		return 1;
4582*4882a593Smuzhiyun 
4583*4882a593Smuzhiyun 	res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4584*4882a593Smuzhiyun 				&bluetooth_tprfk_ops,
4585*4882a593Smuzhiyun 				RFKILL_TYPE_BLUETOOTH,
4586*4882a593Smuzhiyun 				TPACPI_RFK_BLUETOOTH_SW_NAME,
4587*4882a593Smuzhiyun 				true);
4588*4882a593Smuzhiyun 	if (res)
4589*4882a593Smuzhiyun 		return res;
4590*4882a593Smuzhiyun 
4591*4882a593Smuzhiyun 	res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4592*4882a593Smuzhiyun 				&bluetooth_attr_group);
4593*4882a593Smuzhiyun 	if (res) {
4594*4882a593Smuzhiyun 		tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4595*4882a593Smuzhiyun 		return res;
4596*4882a593Smuzhiyun 	}
4597*4882a593Smuzhiyun 
4598*4882a593Smuzhiyun 	return 0;
4599*4882a593Smuzhiyun }
4600*4882a593Smuzhiyun 
4601*4882a593Smuzhiyun /* procfs -------------------------------------------------------------- */
bluetooth_read(struct seq_file * m)4602*4882a593Smuzhiyun static int bluetooth_read(struct seq_file *m)
4603*4882a593Smuzhiyun {
4604*4882a593Smuzhiyun 	return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4605*4882a593Smuzhiyun }
4606*4882a593Smuzhiyun 
bluetooth_write(char * buf)4607*4882a593Smuzhiyun static int bluetooth_write(char *buf)
4608*4882a593Smuzhiyun {
4609*4882a593Smuzhiyun 	return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4610*4882a593Smuzhiyun }
4611*4882a593Smuzhiyun 
4612*4882a593Smuzhiyun static struct ibm_struct bluetooth_driver_data = {
4613*4882a593Smuzhiyun 	.name = "bluetooth",
4614*4882a593Smuzhiyun 	.read = bluetooth_read,
4615*4882a593Smuzhiyun 	.write = bluetooth_write,
4616*4882a593Smuzhiyun 	.exit = bluetooth_exit,
4617*4882a593Smuzhiyun 	.shutdown = bluetooth_shutdown,
4618*4882a593Smuzhiyun };
4619*4882a593Smuzhiyun 
4620*4882a593Smuzhiyun /*************************************************************************
4621*4882a593Smuzhiyun  * Wan subdriver
4622*4882a593Smuzhiyun  */
4623*4882a593Smuzhiyun 
4624*4882a593Smuzhiyun enum {
4625*4882a593Smuzhiyun 	/* ACPI GWAN/SWAN bits */
4626*4882a593Smuzhiyun 	TP_ACPI_WANCARD_HWPRESENT	= 0x01,	/* Wan hw available */
4627*4882a593Smuzhiyun 	TP_ACPI_WANCARD_RADIOSSW	= 0x02,	/* Wan radio enabled */
4628*4882a593Smuzhiyun 	TP_ACPI_WANCARD_RESUMECTRL	= 0x04,	/* Wan state at resume:
4629*4882a593Smuzhiyun 						   0 = disable, 1 = enable */
4630*4882a593Smuzhiyun };
4631*4882a593Smuzhiyun 
4632*4882a593Smuzhiyun #define TPACPI_RFK_WWAN_SW_NAME		"tpacpi_wwan_sw"
4633*4882a593Smuzhiyun 
wan_get_status(void)4634*4882a593Smuzhiyun static int wan_get_status(void)
4635*4882a593Smuzhiyun {
4636*4882a593Smuzhiyun 	int status;
4637*4882a593Smuzhiyun 
4638*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4639*4882a593Smuzhiyun 	if (dbg_wwanemul)
4640*4882a593Smuzhiyun 		return (tpacpi_wwan_emulstate) ?
4641*4882a593Smuzhiyun 		       TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4642*4882a593Smuzhiyun #endif
4643*4882a593Smuzhiyun 
4644*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4645*4882a593Smuzhiyun 		return -EIO;
4646*4882a593Smuzhiyun 
4647*4882a593Smuzhiyun 	return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4648*4882a593Smuzhiyun 			TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4649*4882a593Smuzhiyun }
4650*4882a593Smuzhiyun 
wan_set_status(enum tpacpi_rfkill_state state)4651*4882a593Smuzhiyun static int wan_set_status(enum tpacpi_rfkill_state state)
4652*4882a593Smuzhiyun {
4653*4882a593Smuzhiyun 	int status;
4654*4882a593Smuzhiyun 
4655*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_RFKILL,
4656*4882a593Smuzhiyun 		"will attempt to %s wwan\n",
4657*4882a593Smuzhiyun 		(state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4658*4882a593Smuzhiyun 
4659*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4660*4882a593Smuzhiyun 	if (dbg_wwanemul) {
4661*4882a593Smuzhiyun 		tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4662*4882a593Smuzhiyun 		return 0;
4663*4882a593Smuzhiyun 	}
4664*4882a593Smuzhiyun #endif
4665*4882a593Smuzhiyun 
4666*4882a593Smuzhiyun 	if (state == TPACPI_RFK_RADIO_ON)
4667*4882a593Smuzhiyun 		status = TP_ACPI_WANCARD_RADIOSSW
4668*4882a593Smuzhiyun 			 | TP_ACPI_WANCARD_RESUMECTRL;
4669*4882a593Smuzhiyun 	else
4670*4882a593Smuzhiyun 		status = 0;
4671*4882a593Smuzhiyun 
4672*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4673*4882a593Smuzhiyun 		return -EIO;
4674*4882a593Smuzhiyun 
4675*4882a593Smuzhiyun 	return 0;
4676*4882a593Smuzhiyun }
4677*4882a593Smuzhiyun 
4678*4882a593Smuzhiyun /* sysfs wan enable ---------------------------------------------------- */
wan_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4679*4882a593Smuzhiyun static ssize_t wan_enable_show(struct device *dev,
4680*4882a593Smuzhiyun 			   struct device_attribute *attr,
4681*4882a593Smuzhiyun 			   char *buf)
4682*4882a593Smuzhiyun {
4683*4882a593Smuzhiyun 	return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4684*4882a593Smuzhiyun 			attr, buf);
4685*4882a593Smuzhiyun }
4686*4882a593Smuzhiyun 
wan_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4687*4882a593Smuzhiyun static ssize_t wan_enable_store(struct device *dev,
4688*4882a593Smuzhiyun 			    struct device_attribute *attr,
4689*4882a593Smuzhiyun 			    const char *buf, size_t count)
4690*4882a593Smuzhiyun {
4691*4882a593Smuzhiyun 	return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4692*4882a593Smuzhiyun 			attr, buf, count);
4693*4882a593Smuzhiyun }
4694*4882a593Smuzhiyun 
4695*4882a593Smuzhiyun static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4696*4882a593Smuzhiyun 		   wan_enable_show, wan_enable_store);
4697*4882a593Smuzhiyun 
4698*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
4699*4882a593Smuzhiyun 
4700*4882a593Smuzhiyun static struct attribute *wan_attributes[] = {
4701*4882a593Smuzhiyun 	&dev_attr_wwan_enable.attr,
4702*4882a593Smuzhiyun 	NULL
4703*4882a593Smuzhiyun };
4704*4882a593Smuzhiyun 
4705*4882a593Smuzhiyun static const struct attribute_group wan_attr_group = {
4706*4882a593Smuzhiyun 	.attrs = wan_attributes,
4707*4882a593Smuzhiyun };
4708*4882a593Smuzhiyun 
4709*4882a593Smuzhiyun static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4710*4882a593Smuzhiyun 	.get_status = wan_get_status,
4711*4882a593Smuzhiyun 	.set_status = wan_set_status,
4712*4882a593Smuzhiyun };
4713*4882a593Smuzhiyun 
wan_shutdown(void)4714*4882a593Smuzhiyun static void wan_shutdown(void)
4715*4882a593Smuzhiyun {
4716*4882a593Smuzhiyun 	/* Order firmware to save current state to NVRAM */
4717*4882a593Smuzhiyun 	if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4718*4882a593Smuzhiyun 			TP_ACPI_WGSV_SAVE_STATE))
4719*4882a593Smuzhiyun 		pr_notice("failed to save WWAN state to NVRAM\n");
4720*4882a593Smuzhiyun 	else
4721*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_RFKILL,
4722*4882a593Smuzhiyun 			"WWAN state saved to NVRAM\n");
4723*4882a593Smuzhiyun }
4724*4882a593Smuzhiyun 
wan_exit(void)4725*4882a593Smuzhiyun static void wan_exit(void)
4726*4882a593Smuzhiyun {
4727*4882a593Smuzhiyun 	sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4728*4882a593Smuzhiyun 		&wan_attr_group);
4729*4882a593Smuzhiyun 
4730*4882a593Smuzhiyun 	tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4731*4882a593Smuzhiyun 
4732*4882a593Smuzhiyun 	wan_shutdown();
4733*4882a593Smuzhiyun }
4734*4882a593Smuzhiyun 
wan_init(struct ibm_init_struct * iibm)4735*4882a593Smuzhiyun static int __init wan_init(struct ibm_init_struct *iibm)
4736*4882a593Smuzhiyun {
4737*4882a593Smuzhiyun 	int res;
4738*4882a593Smuzhiyun 	int status = 0;
4739*4882a593Smuzhiyun 
4740*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4741*4882a593Smuzhiyun 			"initializing wan subdriver\n");
4742*4882a593Smuzhiyun 
4743*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(hkey);
4744*4882a593Smuzhiyun 
4745*4882a593Smuzhiyun 	tp_features.wan = hkey_handle &&
4746*4882a593Smuzhiyun 	    acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4747*4882a593Smuzhiyun 
4748*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4749*4882a593Smuzhiyun 		"wan is %s, status 0x%02x\n",
4750*4882a593Smuzhiyun 		str_supported(tp_features.wan),
4751*4882a593Smuzhiyun 		status);
4752*4882a593Smuzhiyun 
4753*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4754*4882a593Smuzhiyun 	if (dbg_wwanemul) {
4755*4882a593Smuzhiyun 		tp_features.wan = 1;
4756*4882a593Smuzhiyun 		pr_info("wwan switch emulation enabled\n");
4757*4882a593Smuzhiyun 	} else
4758*4882a593Smuzhiyun #endif
4759*4882a593Smuzhiyun 	if (tp_features.wan &&
4760*4882a593Smuzhiyun 	    !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4761*4882a593Smuzhiyun 		/* no wan hardware present in system */
4762*4882a593Smuzhiyun 		tp_features.wan = 0;
4763*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4764*4882a593Smuzhiyun 			   "wan hardware not installed\n");
4765*4882a593Smuzhiyun 	}
4766*4882a593Smuzhiyun 
4767*4882a593Smuzhiyun 	if (!tp_features.wan)
4768*4882a593Smuzhiyun 		return 1;
4769*4882a593Smuzhiyun 
4770*4882a593Smuzhiyun 	res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4771*4882a593Smuzhiyun 				&wan_tprfk_ops,
4772*4882a593Smuzhiyun 				RFKILL_TYPE_WWAN,
4773*4882a593Smuzhiyun 				TPACPI_RFK_WWAN_SW_NAME,
4774*4882a593Smuzhiyun 				true);
4775*4882a593Smuzhiyun 	if (res)
4776*4882a593Smuzhiyun 		return res;
4777*4882a593Smuzhiyun 
4778*4882a593Smuzhiyun 	res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4779*4882a593Smuzhiyun 				&wan_attr_group);
4780*4882a593Smuzhiyun 
4781*4882a593Smuzhiyun 	if (res) {
4782*4882a593Smuzhiyun 		tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4783*4882a593Smuzhiyun 		return res;
4784*4882a593Smuzhiyun 	}
4785*4882a593Smuzhiyun 
4786*4882a593Smuzhiyun 	return 0;
4787*4882a593Smuzhiyun }
4788*4882a593Smuzhiyun 
4789*4882a593Smuzhiyun /* procfs -------------------------------------------------------------- */
wan_read(struct seq_file * m)4790*4882a593Smuzhiyun static int wan_read(struct seq_file *m)
4791*4882a593Smuzhiyun {
4792*4882a593Smuzhiyun 	return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4793*4882a593Smuzhiyun }
4794*4882a593Smuzhiyun 
wan_write(char * buf)4795*4882a593Smuzhiyun static int wan_write(char *buf)
4796*4882a593Smuzhiyun {
4797*4882a593Smuzhiyun 	return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4798*4882a593Smuzhiyun }
4799*4882a593Smuzhiyun 
4800*4882a593Smuzhiyun static struct ibm_struct wan_driver_data = {
4801*4882a593Smuzhiyun 	.name = "wan",
4802*4882a593Smuzhiyun 	.read = wan_read,
4803*4882a593Smuzhiyun 	.write = wan_write,
4804*4882a593Smuzhiyun 	.exit = wan_exit,
4805*4882a593Smuzhiyun 	.shutdown = wan_shutdown,
4806*4882a593Smuzhiyun };
4807*4882a593Smuzhiyun 
4808*4882a593Smuzhiyun /*************************************************************************
4809*4882a593Smuzhiyun  * UWB subdriver
4810*4882a593Smuzhiyun  */
4811*4882a593Smuzhiyun 
4812*4882a593Smuzhiyun enum {
4813*4882a593Smuzhiyun 	/* ACPI GUWB/SUWB bits */
4814*4882a593Smuzhiyun 	TP_ACPI_UWB_HWPRESENT	= 0x01,	/* UWB hw available */
4815*4882a593Smuzhiyun 	TP_ACPI_UWB_RADIOSSW	= 0x02,	/* UWB radio enabled */
4816*4882a593Smuzhiyun };
4817*4882a593Smuzhiyun 
4818*4882a593Smuzhiyun #define TPACPI_RFK_UWB_SW_NAME	"tpacpi_uwb_sw"
4819*4882a593Smuzhiyun 
uwb_get_status(void)4820*4882a593Smuzhiyun static int uwb_get_status(void)
4821*4882a593Smuzhiyun {
4822*4882a593Smuzhiyun 	int status;
4823*4882a593Smuzhiyun 
4824*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4825*4882a593Smuzhiyun 	if (dbg_uwbemul)
4826*4882a593Smuzhiyun 		return (tpacpi_uwb_emulstate) ?
4827*4882a593Smuzhiyun 		       TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4828*4882a593Smuzhiyun #endif
4829*4882a593Smuzhiyun 
4830*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4831*4882a593Smuzhiyun 		return -EIO;
4832*4882a593Smuzhiyun 
4833*4882a593Smuzhiyun 	return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4834*4882a593Smuzhiyun 			TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4835*4882a593Smuzhiyun }
4836*4882a593Smuzhiyun 
uwb_set_status(enum tpacpi_rfkill_state state)4837*4882a593Smuzhiyun static int uwb_set_status(enum tpacpi_rfkill_state state)
4838*4882a593Smuzhiyun {
4839*4882a593Smuzhiyun 	int status;
4840*4882a593Smuzhiyun 
4841*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_RFKILL,
4842*4882a593Smuzhiyun 		"will attempt to %s UWB\n",
4843*4882a593Smuzhiyun 		(state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4844*4882a593Smuzhiyun 
4845*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4846*4882a593Smuzhiyun 	if (dbg_uwbemul) {
4847*4882a593Smuzhiyun 		tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4848*4882a593Smuzhiyun 		return 0;
4849*4882a593Smuzhiyun 	}
4850*4882a593Smuzhiyun #endif
4851*4882a593Smuzhiyun 
4852*4882a593Smuzhiyun 	if (state == TPACPI_RFK_RADIO_ON)
4853*4882a593Smuzhiyun 		status = TP_ACPI_UWB_RADIOSSW;
4854*4882a593Smuzhiyun 	else
4855*4882a593Smuzhiyun 		status = 0;
4856*4882a593Smuzhiyun 
4857*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4858*4882a593Smuzhiyun 		return -EIO;
4859*4882a593Smuzhiyun 
4860*4882a593Smuzhiyun 	return 0;
4861*4882a593Smuzhiyun }
4862*4882a593Smuzhiyun 
4863*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
4864*4882a593Smuzhiyun 
4865*4882a593Smuzhiyun static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4866*4882a593Smuzhiyun 	.get_status = uwb_get_status,
4867*4882a593Smuzhiyun 	.set_status = uwb_set_status,
4868*4882a593Smuzhiyun };
4869*4882a593Smuzhiyun 
uwb_exit(void)4870*4882a593Smuzhiyun static void uwb_exit(void)
4871*4882a593Smuzhiyun {
4872*4882a593Smuzhiyun 	tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4873*4882a593Smuzhiyun }
4874*4882a593Smuzhiyun 
uwb_init(struct ibm_init_struct * iibm)4875*4882a593Smuzhiyun static int __init uwb_init(struct ibm_init_struct *iibm)
4876*4882a593Smuzhiyun {
4877*4882a593Smuzhiyun 	int res;
4878*4882a593Smuzhiyun 	int status = 0;
4879*4882a593Smuzhiyun 
4880*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4881*4882a593Smuzhiyun 			"initializing uwb subdriver\n");
4882*4882a593Smuzhiyun 
4883*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(hkey);
4884*4882a593Smuzhiyun 
4885*4882a593Smuzhiyun 	tp_features.uwb = hkey_handle &&
4886*4882a593Smuzhiyun 	    acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4887*4882a593Smuzhiyun 
4888*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4889*4882a593Smuzhiyun 		"uwb is %s, status 0x%02x\n",
4890*4882a593Smuzhiyun 		str_supported(tp_features.uwb),
4891*4882a593Smuzhiyun 		status);
4892*4882a593Smuzhiyun 
4893*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4894*4882a593Smuzhiyun 	if (dbg_uwbemul) {
4895*4882a593Smuzhiyun 		tp_features.uwb = 1;
4896*4882a593Smuzhiyun 		pr_info("uwb switch emulation enabled\n");
4897*4882a593Smuzhiyun 	} else
4898*4882a593Smuzhiyun #endif
4899*4882a593Smuzhiyun 	if (tp_features.uwb &&
4900*4882a593Smuzhiyun 	    !(status & TP_ACPI_UWB_HWPRESENT)) {
4901*4882a593Smuzhiyun 		/* no uwb hardware present in system */
4902*4882a593Smuzhiyun 		tp_features.uwb = 0;
4903*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT,
4904*4882a593Smuzhiyun 			   "uwb hardware not installed\n");
4905*4882a593Smuzhiyun 	}
4906*4882a593Smuzhiyun 
4907*4882a593Smuzhiyun 	if (!tp_features.uwb)
4908*4882a593Smuzhiyun 		return 1;
4909*4882a593Smuzhiyun 
4910*4882a593Smuzhiyun 	res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4911*4882a593Smuzhiyun 				&uwb_tprfk_ops,
4912*4882a593Smuzhiyun 				RFKILL_TYPE_UWB,
4913*4882a593Smuzhiyun 				TPACPI_RFK_UWB_SW_NAME,
4914*4882a593Smuzhiyun 				false);
4915*4882a593Smuzhiyun 	return res;
4916*4882a593Smuzhiyun }
4917*4882a593Smuzhiyun 
4918*4882a593Smuzhiyun static struct ibm_struct uwb_driver_data = {
4919*4882a593Smuzhiyun 	.name = "uwb",
4920*4882a593Smuzhiyun 	.exit = uwb_exit,
4921*4882a593Smuzhiyun 	.flags.experimental = 1,
4922*4882a593Smuzhiyun };
4923*4882a593Smuzhiyun 
4924*4882a593Smuzhiyun /*************************************************************************
4925*4882a593Smuzhiyun  * Video subdriver
4926*4882a593Smuzhiyun  */
4927*4882a593Smuzhiyun 
4928*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4929*4882a593Smuzhiyun 
4930*4882a593Smuzhiyun enum video_access_mode {
4931*4882a593Smuzhiyun 	TPACPI_VIDEO_NONE = 0,
4932*4882a593Smuzhiyun 	TPACPI_VIDEO_570,	/* 570 */
4933*4882a593Smuzhiyun 	TPACPI_VIDEO_770,	/* 600e/x, 770e, 770x */
4934*4882a593Smuzhiyun 	TPACPI_VIDEO_NEW,	/* all others */
4935*4882a593Smuzhiyun };
4936*4882a593Smuzhiyun 
4937*4882a593Smuzhiyun enum {	/* video status flags, based on VIDEO_570 */
4938*4882a593Smuzhiyun 	TP_ACPI_VIDEO_S_LCD = 0x01,	/* LCD output enabled */
4939*4882a593Smuzhiyun 	TP_ACPI_VIDEO_S_CRT = 0x02,	/* CRT output enabled */
4940*4882a593Smuzhiyun 	TP_ACPI_VIDEO_S_DVI = 0x08,	/* DVI output enabled */
4941*4882a593Smuzhiyun };
4942*4882a593Smuzhiyun 
4943*4882a593Smuzhiyun enum {  /* TPACPI_VIDEO_570 constants */
4944*4882a593Smuzhiyun 	TP_ACPI_VIDEO_570_PHSCMD = 0x87,	/* unknown magic constant :( */
4945*4882a593Smuzhiyun 	TP_ACPI_VIDEO_570_PHSMASK = 0x03,	/* PHS bits that map to
4946*4882a593Smuzhiyun 						 * video_status_flags */
4947*4882a593Smuzhiyun 	TP_ACPI_VIDEO_570_PHS2CMD = 0x8b,	/* unknown magic constant :( */
4948*4882a593Smuzhiyun 	TP_ACPI_VIDEO_570_PHS2SET = 0x80,	/* unknown magic constant :( */
4949*4882a593Smuzhiyun };
4950*4882a593Smuzhiyun 
4951*4882a593Smuzhiyun static enum video_access_mode video_supported;
4952*4882a593Smuzhiyun static int video_orig_autosw;
4953*4882a593Smuzhiyun 
4954*4882a593Smuzhiyun static int video_autosw_get(void);
4955*4882a593Smuzhiyun static int video_autosw_set(int enable);
4956*4882a593Smuzhiyun 
4957*4882a593Smuzhiyun TPACPI_HANDLE(vid, root,
4958*4882a593Smuzhiyun 	      "\\_SB.PCI.AGP.VGA",	/* 570 */
4959*4882a593Smuzhiyun 	      "\\_SB.PCI0.AGP0.VID0",	/* 600e/x, 770x */
4960*4882a593Smuzhiyun 	      "\\_SB.PCI0.VID0",	/* 770e */
4961*4882a593Smuzhiyun 	      "\\_SB.PCI0.VID",		/* A21e, G4x, R50e, X30, X40 */
4962*4882a593Smuzhiyun 	      "\\_SB.PCI0.AGP.VGA",	/* X100e and a few others */
4963*4882a593Smuzhiyun 	      "\\_SB.PCI0.AGP.VID",	/* all others */
4964*4882a593Smuzhiyun 	);				/* R30, R31 */
4965*4882a593Smuzhiyun 
4966*4882a593Smuzhiyun TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");	/* G41 */
4967*4882a593Smuzhiyun 
video_init(struct ibm_init_struct * iibm)4968*4882a593Smuzhiyun static int __init video_init(struct ibm_init_struct *iibm)
4969*4882a593Smuzhiyun {
4970*4882a593Smuzhiyun 	int ivga;
4971*4882a593Smuzhiyun 
4972*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4973*4882a593Smuzhiyun 
4974*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(vid);
4975*4882a593Smuzhiyun 	if (tpacpi_is_ibm())
4976*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(vid2);
4977*4882a593Smuzhiyun 
4978*4882a593Smuzhiyun 	if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4979*4882a593Smuzhiyun 		/* G41, assume IVGA doesn't change */
4980*4882a593Smuzhiyun 		vid_handle = vid2_handle;
4981*4882a593Smuzhiyun 
4982*4882a593Smuzhiyun 	if (!vid_handle)
4983*4882a593Smuzhiyun 		/* video switching not supported on R30, R31 */
4984*4882a593Smuzhiyun 		video_supported = TPACPI_VIDEO_NONE;
4985*4882a593Smuzhiyun 	else if (tpacpi_is_ibm() &&
4986*4882a593Smuzhiyun 		 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4987*4882a593Smuzhiyun 		/* 570 */
4988*4882a593Smuzhiyun 		video_supported = TPACPI_VIDEO_570;
4989*4882a593Smuzhiyun 	else if (tpacpi_is_ibm() &&
4990*4882a593Smuzhiyun 		 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4991*4882a593Smuzhiyun 		/* 600e/x, 770e, 770x */
4992*4882a593Smuzhiyun 		video_supported = TPACPI_VIDEO_770;
4993*4882a593Smuzhiyun 	else
4994*4882a593Smuzhiyun 		/* all others */
4995*4882a593Smuzhiyun 		video_supported = TPACPI_VIDEO_NEW;
4996*4882a593Smuzhiyun 
4997*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4998*4882a593Smuzhiyun 		str_supported(video_supported != TPACPI_VIDEO_NONE),
4999*4882a593Smuzhiyun 		video_supported);
5000*4882a593Smuzhiyun 
5001*4882a593Smuzhiyun 	return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
5002*4882a593Smuzhiyun }
5003*4882a593Smuzhiyun 
video_exit(void)5004*4882a593Smuzhiyun static void video_exit(void)
5005*4882a593Smuzhiyun {
5006*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_EXIT,
5007*4882a593Smuzhiyun 		   "restoring original video autoswitch mode\n");
5008*4882a593Smuzhiyun 	if (video_autosw_set(video_orig_autosw))
5009*4882a593Smuzhiyun 		pr_err("error while trying to restore original video autoswitch mode\n");
5010*4882a593Smuzhiyun }
5011*4882a593Smuzhiyun 
video_outputsw_get(void)5012*4882a593Smuzhiyun static int video_outputsw_get(void)
5013*4882a593Smuzhiyun {
5014*4882a593Smuzhiyun 	int status = 0;
5015*4882a593Smuzhiyun 	int i;
5016*4882a593Smuzhiyun 
5017*4882a593Smuzhiyun 	switch (video_supported) {
5018*4882a593Smuzhiyun 	case TPACPI_VIDEO_570:
5019*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5020*4882a593Smuzhiyun 				 TP_ACPI_VIDEO_570_PHSCMD))
5021*4882a593Smuzhiyun 			return -EIO;
5022*4882a593Smuzhiyun 		status = i & TP_ACPI_VIDEO_570_PHSMASK;
5023*4882a593Smuzhiyun 		break;
5024*4882a593Smuzhiyun 	case TPACPI_VIDEO_770:
5025*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5026*4882a593Smuzhiyun 			return -EIO;
5027*4882a593Smuzhiyun 		if (i)
5028*4882a593Smuzhiyun 			status |= TP_ACPI_VIDEO_S_LCD;
5029*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5030*4882a593Smuzhiyun 			return -EIO;
5031*4882a593Smuzhiyun 		if (i)
5032*4882a593Smuzhiyun 			status |= TP_ACPI_VIDEO_S_CRT;
5033*4882a593Smuzhiyun 		break;
5034*4882a593Smuzhiyun 	case TPACPI_VIDEO_NEW:
5035*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5036*4882a593Smuzhiyun 		    !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5037*4882a593Smuzhiyun 			return -EIO;
5038*4882a593Smuzhiyun 		if (i)
5039*4882a593Smuzhiyun 			status |= TP_ACPI_VIDEO_S_CRT;
5040*4882a593Smuzhiyun 
5041*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5042*4882a593Smuzhiyun 		    !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5043*4882a593Smuzhiyun 			return -EIO;
5044*4882a593Smuzhiyun 		if (i)
5045*4882a593Smuzhiyun 			status |= TP_ACPI_VIDEO_S_LCD;
5046*4882a593Smuzhiyun 		if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5047*4882a593Smuzhiyun 			return -EIO;
5048*4882a593Smuzhiyun 		if (i)
5049*4882a593Smuzhiyun 			status |= TP_ACPI_VIDEO_S_DVI;
5050*4882a593Smuzhiyun 		break;
5051*4882a593Smuzhiyun 	default:
5052*4882a593Smuzhiyun 		return -ENOSYS;
5053*4882a593Smuzhiyun 	}
5054*4882a593Smuzhiyun 
5055*4882a593Smuzhiyun 	return status;
5056*4882a593Smuzhiyun }
5057*4882a593Smuzhiyun 
video_outputsw_set(int status)5058*4882a593Smuzhiyun static int video_outputsw_set(int status)
5059*4882a593Smuzhiyun {
5060*4882a593Smuzhiyun 	int autosw;
5061*4882a593Smuzhiyun 	int res = 0;
5062*4882a593Smuzhiyun 
5063*4882a593Smuzhiyun 	switch (video_supported) {
5064*4882a593Smuzhiyun 	case TPACPI_VIDEO_570:
5065*4882a593Smuzhiyun 		res = acpi_evalf(NULL, NULL,
5066*4882a593Smuzhiyun 				 "\\_SB.PHS2", "vdd",
5067*4882a593Smuzhiyun 				 TP_ACPI_VIDEO_570_PHS2CMD,
5068*4882a593Smuzhiyun 				 status | TP_ACPI_VIDEO_570_PHS2SET);
5069*4882a593Smuzhiyun 		break;
5070*4882a593Smuzhiyun 	case TPACPI_VIDEO_770:
5071*4882a593Smuzhiyun 		autosw = video_autosw_get();
5072*4882a593Smuzhiyun 		if (autosw < 0)
5073*4882a593Smuzhiyun 			return autosw;
5074*4882a593Smuzhiyun 
5075*4882a593Smuzhiyun 		res = video_autosw_set(1);
5076*4882a593Smuzhiyun 		if (res)
5077*4882a593Smuzhiyun 			return res;
5078*4882a593Smuzhiyun 		res = acpi_evalf(vid_handle, NULL,
5079*4882a593Smuzhiyun 				 "ASWT", "vdd", status * 0x100, 0);
5080*4882a593Smuzhiyun 		if (!autosw && video_autosw_set(autosw)) {
5081*4882a593Smuzhiyun 			pr_err("video auto-switch left enabled due to error\n");
5082*4882a593Smuzhiyun 			return -EIO;
5083*4882a593Smuzhiyun 		}
5084*4882a593Smuzhiyun 		break;
5085*4882a593Smuzhiyun 	case TPACPI_VIDEO_NEW:
5086*4882a593Smuzhiyun 		res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5087*4882a593Smuzhiyun 		      acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5088*4882a593Smuzhiyun 		break;
5089*4882a593Smuzhiyun 	default:
5090*4882a593Smuzhiyun 		return -ENOSYS;
5091*4882a593Smuzhiyun 	}
5092*4882a593Smuzhiyun 
5093*4882a593Smuzhiyun 	return (res) ? 0 : -EIO;
5094*4882a593Smuzhiyun }
5095*4882a593Smuzhiyun 
video_autosw_get(void)5096*4882a593Smuzhiyun static int video_autosw_get(void)
5097*4882a593Smuzhiyun {
5098*4882a593Smuzhiyun 	int autosw = 0;
5099*4882a593Smuzhiyun 
5100*4882a593Smuzhiyun 	switch (video_supported) {
5101*4882a593Smuzhiyun 	case TPACPI_VIDEO_570:
5102*4882a593Smuzhiyun 		if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5103*4882a593Smuzhiyun 			return -EIO;
5104*4882a593Smuzhiyun 		break;
5105*4882a593Smuzhiyun 	case TPACPI_VIDEO_770:
5106*4882a593Smuzhiyun 	case TPACPI_VIDEO_NEW:
5107*4882a593Smuzhiyun 		if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5108*4882a593Smuzhiyun 			return -EIO;
5109*4882a593Smuzhiyun 		break;
5110*4882a593Smuzhiyun 	default:
5111*4882a593Smuzhiyun 		return -ENOSYS;
5112*4882a593Smuzhiyun 	}
5113*4882a593Smuzhiyun 
5114*4882a593Smuzhiyun 	return autosw & 1;
5115*4882a593Smuzhiyun }
5116*4882a593Smuzhiyun 
video_autosw_set(int enable)5117*4882a593Smuzhiyun static int video_autosw_set(int enable)
5118*4882a593Smuzhiyun {
5119*4882a593Smuzhiyun 	if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5120*4882a593Smuzhiyun 		return -EIO;
5121*4882a593Smuzhiyun 	return 0;
5122*4882a593Smuzhiyun }
5123*4882a593Smuzhiyun 
video_outputsw_cycle(void)5124*4882a593Smuzhiyun static int video_outputsw_cycle(void)
5125*4882a593Smuzhiyun {
5126*4882a593Smuzhiyun 	int autosw = video_autosw_get();
5127*4882a593Smuzhiyun 	int res;
5128*4882a593Smuzhiyun 
5129*4882a593Smuzhiyun 	if (autosw < 0)
5130*4882a593Smuzhiyun 		return autosw;
5131*4882a593Smuzhiyun 
5132*4882a593Smuzhiyun 	switch (video_supported) {
5133*4882a593Smuzhiyun 	case TPACPI_VIDEO_570:
5134*4882a593Smuzhiyun 		res = video_autosw_set(1);
5135*4882a593Smuzhiyun 		if (res)
5136*4882a593Smuzhiyun 			return res;
5137*4882a593Smuzhiyun 		res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5138*4882a593Smuzhiyun 		break;
5139*4882a593Smuzhiyun 	case TPACPI_VIDEO_770:
5140*4882a593Smuzhiyun 	case TPACPI_VIDEO_NEW:
5141*4882a593Smuzhiyun 		res = video_autosw_set(1);
5142*4882a593Smuzhiyun 		if (res)
5143*4882a593Smuzhiyun 			return res;
5144*4882a593Smuzhiyun 		res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5145*4882a593Smuzhiyun 		break;
5146*4882a593Smuzhiyun 	default:
5147*4882a593Smuzhiyun 		return -ENOSYS;
5148*4882a593Smuzhiyun 	}
5149*4882a593Smuzhiyun 	if (!autosw && video_autosw_set(autosw)) {
5150*4882a593Smuzhiyun 		pr_err("video auto-switch left enabled due to error\n");
5151*4882a593Smuzhiyun 		return -EIO;
5152*4882a593Smuzhiyun 	}
5153*4882a593Smuzhiyun 
5154*4882a593Smuzhiyun 	return (res) ? 0 : -EIO;
5155*4882a593Smuzhiyun }
5156*4882a593Smuzhiyun 
video_expand_toggle(void)5157*4882a593Smuzhiyun static int video_expand_toggle(void)
5158*4882a593Smuzhiyun {
5159*4882a593Smuzhiyun 	switch (video_supported) {
5160*4882a593Smuzhiyun 	case TPACPI_VIDEO_570:
5161*4882a593Smuzhiyun 		return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5162*4882a593Smuzhiyun 			0 : -EIO;
5163*4882a593Smuzhiyun 	case TPACPI_VIDEO_770:
5164*4882a593Smuzhiyun 		return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5165*4882a593Smuzhiyun 			0 : -EIO;
5166*4882a593Smuzhiyun 	case TPACPI_VIDEO_NEW:
5167*4882a593Smuzhiyun 		return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5168*4882a593Smuzhiyun 			0 : -EIO;
5169*4882a593Smuzhiyun 	default:
5170*4882a593Smuzhiyun 		return -ENOSYS;
5171*4882a593Smuzhiyun 	}
5172*4882a593Smuzhiyun 	/* not reached */
5173*4882a593Smuzhiyun }
5174*4882a593Smuzhiyun 
video_read(struct seq_file * m)5175*4882a593Smuzhiyun static int video_read(struct seq_file *m)
5176*4882a593Smuzhiyun {
5177*4882a593Smuzhiyun 	int status, autosw;
5178*4882a593Smuzhiyun 
5179*4882a593Smuzhiyun 	if (video_supported == TPACPI_VIDEO_NONE) {
5180*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
5181*4882a593Smuzhiyun 		return 0;
5182*4882a593Smuzhiyun 	}
5183*4882a593Smuzhiyun 
5184*4882a593Smuzhiyun 	/* Even reads can crash X.org, so... */
5185*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
5186*4882a593Smuzhiyun 		return -EPERM;
5187*4882a593Smuzhiyun 
5188*4882a593Smuzhiyun 	status = video_outputsw_get();
5189*4882a593Smuzhiyun 	if (status < 0)
5190*4882a593Smuzhiyun 		return status;
5191*4882a593Smuzhiyun 
5192*4882a593Smuzhiyun 	autosw = video_autosw_get();
5193*4882a593Smuzhiyun 	if (autosw < 0)
5194*4882a593Smuzhiyun 		return autosw;
5195*4882a593Smuzhiyun 
5196*4882a593Smuzhiyun 	seq_printf(m, "status:\t\tsupported\n");
5197*4882a593Smuzhiyun 	seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5198*4882a593Smuzhiyun 	seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5199*4882a593Smuzhiyun 	if (video_supported == TPACPI_VIDEO_NEW)
5200*4882a593Smuzhiyun 		seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5201*4882a593Smuzhiyun 	seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5202*4882a593Smuzhiyun 	seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5203*4882a593Smuzhiyun 	seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5204*4882a593Smuzhiyun 	if (video_supported == TPACPI_VIDEO_NEW)
5205*4882a593Smuzhiyun 		seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5206*4882a593Smuzhiyun 	seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5207*4882a593Smuzhiyun 	seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5208*4882a593Smuzhiyun 
5209*4882a593Smuzhiyun 	return 0;
5210*4882a593Smuzhiyun }
5211*4882a593Smuzhiyun 
video_write(char * buf)5212*4882a593Smuzhiyun static int video_write(char *buf)
5213*4882a593Smuzhiyun {
5214*4882a593Smuzhiyun 	char *cmd;
5215*4882a593Smuzhiyun 	int enable, disable, status;
5216*4882a593Smuzhiyun 	int res;
5217*4882a593Smuzhiyun 
5218*4882a593Smuzhiyun 	if (video_supported == TPACPI_VIDEO_NONE)
5219*4882a593Smuzhiyun 		return -ENODEV;
5220*4882a593Smuzhiyun 
5221*4882a593Smuzhiyun 	/* Even reads can crash X.org, let alone writes... */
5222*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
5223*4882a593Smuzhiyun 		return -EPERM;
5224*4882a593Smuzhiyun 
5225*4882a593Smuzhiyun 	enable = 0;
5226*4882a593Smuzhiyun 	disable = 0;
5227*4882a593Smuzhiyun 
5228*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
5229*4882a593Smuzhiyun 		if (strlencmp(cmd, "lcd_enable") == 0) {
5230*4882a593Smuzhiyun 			enable |= TP_ACPI_VIDEO_S_LCD;
5231*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "lcd_disable") == 0) {
5232*4882a593Smuzhiyun 			disable |= TP_ACPI_VIDEO_S_LCD;
5233*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "crt_enable") == 0) {
5234*4882a593Smuzhiyun 			enable |= TP_ACPI_VIDEO_S_CRT;
5235*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "crt_disable") == 0) {
5236*4882a593Smuzhiyun 			disable |= TP_ACPI_VIDEO_S_CRT;
5237*4882a593Smuzhiyun 		} else if (video_supported == TPACPI_VIDEO_NEW &&
5238*4882a593Smuzhiyun 			   strlencmp(cmd, "dvi_enable") == 0) {
5239*4882a593Smuzhiyun 			enable |= TP_ACPI_VIDEO_S_DVI;
5240*4882a593Smuzhiyun 		} else if (video_supported == TPACPI_VIDEO_NEW &&
5241*4882a593Smuzhiyun 			   strlencmp(cmd, "dvi_disable") == 0) {
5242*4882a593Smuzhiyun 			disable |= TP_ACPI_VIDEO_S_DVI;
5243*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "auto_enable") == 0) {
5244*4882a593Smuzhiyun 			res = video_autosw_set(1);
5245*4882a593Smuzhiyun 			if (res)
5246*4882a593Smuzhiyun 				return res;
5247*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "auto_disable") == 0) {
5248*4882a593Smuzhiyun 			res = video_autosw_set(0);
5249*4882a593Smuzhiyun 			if (res)
5250*4882a593Smuzhiyun 				return res;
5251*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "video_switch") == 0) {
5252*4882a593Smuzhiyun 			res = video_outputsw_cycle();
5253*4882a593Smuzhiyun 			if (res)
5254*4882a593Smuzhiyun 				return res;
5255*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "expand_toggle") == 0) {
5256*4882a593Smuzhiyun 			res = video_expand_toggle();
5257*4882a593Smuzhiyun 			if (res)
5258*4882a593Smuzhiyun 				return res;
5259*4882a593Smuzhiyun 		} else
5260*4882a593Smuzhiyun 			return -EINVAL;
5261*4882a593Smuzhiyun 	}
5262*4882a593Smuzhiyun 
5263*4882a593Smuzhiyun 	if (enable || disable) {
5264*4882a593Smuzhiyun 		status = video_outputsw_get();
5265*4882a593Smuzhiyun 		if (status < 0)
5266*4882a593Smuzhiyun 			return status;
5267*4882a593Smuzhiyun 		res = video_outputsw_set((status & ~disable) | enable);
5268*4882a593Smuzhiyun 		if (res)
5269*4882a593Smuzhiyun 			return res;
5270*4882a593Smuzhiyun 	}
5271*4882a593Smuzhiyun 
5272*4882a593Smuzhiyun 	return 0;
5273*4882a593Smuzhiyun }
5274*4882a593Smuzhiyun 
5275*4882a593Smuzhiyun static struct ibm_struct video_driver_data = {
5276*4882a593Smuzhiyun 	.name = "video",
5277*4882a593Smuzhiyun 	.read = video_read,
5278*4882a593Smuzhiyun 	.write = video_write,
5279*4882a593Smuzhiyun 	.exit = video_exit,
5280*4882a593Smuzhiyun };
5281*4882a593Smuzhiyun 
5282*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5283*4882a593Smuzhiyun 
5284*4882a593Smuzhiyun /*************************************************************************
5285*4882a593Smuzhiyun  * Keyboard backlight subdriver
5286*4882a593Smuzhiyun  */
5287*4882a593Smuzhiyun 
5288*4882a593Smuzhiyun static enum led_brightness kbdlight_brightness;
5289*4882a593Smuzhiyun static DEFINE_MUTEX(kbdlight_mutex);
5290*4882a593Smuzhiyun 
kbdlight_set_level(int level)5291*4882a593Smuzhiyun static int kbdlight_set_level(int level)
5292*4882a593Smuzhiyun {
5293*4882a593Smuzhiyun 	int ret = 0;
5294*4882a593Smuzhiyun 
5295*4882a593Smuzhiyun 	if (!hkey_handle)
5296*4882a593Smuzhiyun 		return -ENXIO;
5297*4882a593Smuzhiyun 
5298*4882a593Smuzhiyun 	mutex_lock(&kbdlight_mutex);
5299*4882a593Smuzhiyun 
5300*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5301*4882a593Smuzhiyun 		ret = -EIO;
5302*4882a593Smuzhiyun 	else
5303*4882a593Smuzhiyun 		kbdlight_brightness = level;
5304*4882a593Smuzhiyun 
5305*4882a593Smuzhiyun 	mutex_unlock(&kbdlight_mutex);
5306*4882a593Smuzhiyun 
5307*4882a593Smuzhiyun 	return ret;
5308*4882a593Smuzhiyun }
5309*4882a593Smuzhiyun 
kbdlight_get_level(void)5310*4882a593Smuzhiyun static int kbdlight_get_level(void)
5311*4882a593Smuzhiyun {
5312*4882a593Smuzhiyun 	int status = 0;
5313*4882a593Smuzhiyun 
5314*4882a593Smuzhiyun 	if (!hkey_handle)
5315*4882a593Smuzhiyun 		return -ENXIO;
5316*4882a593Smuzhiyun 
5317*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5318*4882a593Smuzhiyun 		return -EIO;
5319*4882a593Smuzhiyun 
5320*4882a593Smuzhiyun 	if (status < 0)
5321*4882a593Smuzhiyun 		return status;
5322*4882a593Smuzhiyun 
5323*4882a593Smuzhiyun 	return status & 0x3;
5324*4882a593Smuzhiyun }
5325*4882a593Smuzhiyun 
kbdlight_is_supported(void)5326*4882a593Smuzhiyun static bool kbdlight_is_supported(void)
5327*4882a593Smuzhiyun {
5328*4882a593Smuzhiyun 	int status = 0;
5329*4882a593Smuzhiyun 
5330*4882a593Smuzhiyun 	if (!hkey_handle)
5331*4882a593Smuzhiyun 		return false;
5332*4882a593Smuzhiyun 
5333*4882a593Smuzhiyun 	if (!acpi_has_method(hkey_handle, "MLCG")) {
5334*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5335*4882a593Smuzhiyun 		return false;
5336*4882a593Smuzhiyun 	}
5337*4882a593Smuzhiyun 
5338*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5339*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5340*4882a593Smuzhiyun 		return false;
5341*4882a593Smuzhiyun 	}
5342*4882a593Smuzhiyun 
5343*4882a593Smuzhiyun 	if (status < 0) {
5344*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5345*4882a593Smuzhiyun 		return false;
5346*4882a593Smuzhiyun 	}
5347*4882a593Smuzhiyun 
5348*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5349*4882a593Smuzhiyun 	/*
5350*4882a593Smuzhiyun 	 * Guessed test for keyboard backlight:
5351*4882a593Smuzhiyun 	 *
5352*4882a593Smuzhiyun 	 * Machines with backlight keyboard return:
5353*4882a593Smuzhiyun 	 *   b010100000010000000XX - ThinkPad X1 Carbon 3rd
5354*4882a593Smuzhiyun 	 *   b110100010010000000XX - ThinkPad x230
5355*4882a593Smuzhiyun 	 *   b010100000010000000XX - ThinkPad x240
5356*4882a593Smuzhiyun 	 *   b010100000010000000XX - ThinkPad W541
5357*4882a593Smuzhiyun 	 * (XX is current backlight level)
5358*4882a593Smuzhiyun 	 *
5359*4882a593Smuzhiyun 	 * Machines without backlight keyboard return:
5360*4882a593Smuzhiyun 	 *   b10100001000000000000 - ThinkPad x230
5361*4882a593Smuzhiyun 	 *   b10110001000000000000 - ThinkPad E430
5362*4882a593Smuzhiyun 	 *   b00000000000000000000 - ThinkPad E450
5363*4882a593Smuzhiyun 	 *
5364*4882a593Smuzhiyun 	 * Candidate BITs for detection test (XOR):
5365*4882a593Smuzhiyun 	 *   b01000000001000000000
5366*4882a593Smuzhiyun 	 *              ^
5367*4882a593Smuzhiyun 	 */
5368*4882a593Smuzhiyun 	return status & BIT(9);
5369*4882a593Smuzhiyun }
5370*4882a593Smuzhiyun 
kbdlight_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5371*4882a593Smuzhiyun static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5372*4882a593Smuzhiyun 			enum led_brightness brightness)
5373*4882a593Smuzhiyun {
5374*4882a593Smuzhiyun 	return kbdlight_set_level(brightness);
5375*4882a593Smuzhiyun }
5376*4882a593Smuzhiyun 
kbdlight_sysfs_get(struct led_classdev * led_cdev)5377*4882a593Smuzhiyun static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5378*4882a593Smuzhiyun {
5379*4882a593Smuzhiyun 	int level;
5380*4882a593Smuzhiyun 
5381*4882a593Smuzhiyun 	level = kbdlight_get_level();
5382*4882a593Smuzhiyun 	if (level < 0)
5383*4882a593Smuzhiyun 		return 0;
5384*4882a593Smuzhiyun 
5385*4882a593Smuzhiyun 	return level;
5386*4882a593Smuzhiyun }
5387*4882a593Smuzhiyun 
5388*4882a593Smuzhiyun static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5389*4882a593Smuzhiyun 	.led_classdev = {
5390*4882a593Smuzhiyun 		.name		= "tpacpi::kbd_backlight",
5391*4882a593Smuzhiyun 		.max_brightness	= 2,
5392*4882a593Smuzhiyun 		.flags		= LED_BRIGHT_HW_CHANGED,
5393*4882a593Smuzhiyun 		.brightness_set_blocking = &kbdlight_sysfs_set,
5394*4882a593Smuzhiyun 		.brightness_get	= &kbdlight_sysfs_get,
5395*4882a593Smuzhiyun 	}
5396*4882a593Smuzhiyun };
5397*4882a593Smuzhiyun 
kbdlight_init(struct ibm_init_struct * iibm)5398*4882a593Smuzhiyun static int __init kbdlight_init(struct ibm_init_struct *iibm)
5399*4882a593Smuzhiyun {
5400*4882a593Smuzhiyun 	int rc;
5401*4882a593Smuzhiyun 
5402*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5403*4882a593Smuzhiyun 
5404*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(hkey);
5405*4882a593Smuzhiyun 
5406*4882a593Smuzhiyun 	if (!kbdlight_is_supported()) {
5407*4882a593Smuzhiyun 		tp_features.kbdlight = 0;
5408*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5409*4882a593Smuzhiyun 		return 1;
5410*4882a593Smuzhiyun 	}
5411*4882a593Smuzhiyun 
5412*4882a593Smuzhiyun 	kbdlight_brightness = kbdlight_sysfs_get(NULL);
5413*4882a593Smuzhiyun 	tp_features.kbdlight = 1;
5414*4882a593Smuzhiyun 
5415*4882a593Smuzhiyun 	rc = led_classdev_register(&tpacpi_pdev->dev,
5416*4882a593Smuzhiyun 				   &tpacpi_led_kbdlight.led_classdev);
5417*4882a593Smuzhiyun 	if (rc < 0) {
5418*4882a593Smuzhiyun 		tp_features.kbdlight = 0;
5419*4882a593Smuzhiyun 		return rc;
5420*4882a593Smuzhiyun 	}
5421*4882a593Smuzhiyun 
5422*4882a593Smuzhiyun 	tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5423*4882a593Smuzhiyun 				      TP_ACPI_HKEY_KBD_LIGHT_MASK);
5424*4882a593Smuzhiyun 	return 0;
5425*4882a593Smuzhiyun }
5426*4882a593Smuzhiyun 
kbdlight_exit(void)5427*4882a593Smuzhiyun static void kbdlight_exit(void)
5428*4882a593Smuzhiyun {
5429*4882a593Smuzhiyun 	led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5430*4882a593Smuzhiyun }
5431*4882a593Smuzhiyun 
kbdlight_set_level_and_update(int level)5432*4882a593Smuzhiyun static int kbdlight_set_level_and_update(int level)
5433*4882a593Smuzhiyun {
5434*4882a593Smuzhiyun 	int ret;
5435*4882a593Smuzhiyun 	struct led_classdev *led_cdev;
5436*4882a593Smuzhiyun 
5437*4882a593Smuzhiyun 	ret = kbdlight_set_level(level);
5438*4882a593Smuzhiyun 	led_cdev = &tpacpi_led_kbdlight.led_classdev;
5439*4882a593Smuzhiyun 
5440*4882a593Smuzhiyun 	if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5441*4882a593Smuzhiyun 		led_cdev->brightness = level;
5442*4882a593Smuzhiyun 
5443*4882a593Smuzhiyun 	return ret;
5444*4882a593Smuzhiyun }
5445*4882a593Smuzhiyun 
kbdlight_read(struct seq_file * m)5446*4882a593Smuzhiyun static int kbdlight_read(struct seq_file *m)
5447*4882a593Smuzhiyun {
5448*4882a593Smuzhiyun 	int level;
5449*4882a593Smuzhiyun 
5450*4882a593Smuzhiyun 	if (!tp_features.kbdlight) {
5451*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
5452*4882a593Smuzhiyun 	} else {
5453*4882a593Smuzhiyun 		level = kbdlight_get_level();
5454*4882a593Smuzhiyun 		if (level < 0)
5455*4882a593Smuzhiyun 			seq_printf(m, "status:\t\terror %d\n", level);
5456*4882a593Smuzhiyun 		else
5457*4882a593Smuzhiyun 			seq_printf(m, "status:\t\t%d\n", level);
5458*4882a593Smuzhiyun 		seq_printf(m, "commands:\t0, 1, 2\n");
5459*4882a593Smuzhiyun 	}
5460*4882a593Smuzhiyun 
5461*4882a593Smuzhiyun 	return 0;
5462*4882a593Smuzhiyun }
5463*4882a593Smuzhiyun 
kbdlight_write(char * buf)5464*4882a593Smuzhiyun static int kbdlight_write(char *buf)
5465*4882a593Smuzhiyun {
5466*4882a593Smuzhiyun 	char *cmd;
5467*4882a593Smuzhiyun 	int res, level = -EINVAL;
5468*4882a593Smuzhiyun 
5469*4882a593Smuzhiyun 	if (!tp_features.kbdlight)
5470*4882a593Smuzhiyun 		return -ENODEV;
5471*4882a593Smuzhiyun 
5472*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
5473*4882a593Smuzhiyun 		res = kstrtoint(cmd, 10, &level);
5474*4882a593Smuzhiyun 		if (res < 0)
5475*4882a593Smuzhiyun 			return res;
5476*4882a593Smuzhiyun 	}
5477*4882a593Smuzhiyun 
5478*4882a593Smuzhiyun 	if (level >= 3 || level < 0)
5479*4882a593Smuzhiyun 		return -EINVAL;
5480*4882a593Smuzhiyun 
5481*4882a593Smuzhiyun 	return kbdlight_set_level_and_update(level);
5482*4882a593Smuzhiyun }
5483*4882a593Smuzhiyun 
kbdlight_suspend(void)5484*4882a593Smuzhiyun static void kbdlight_suspend(void)
5485*4882a593Smuzhiyun {
5486*4882a593Smuzhiyun 	struct led_classdev *led_cdev;
5487*4882a593Smuzhiyun 
5488*4882a593Smuzhiyun 	if (!tp_features.kbdlight)
5489*4882a593Smuzhiyun 		return;
5490*4882a593Smuzhiyun 
5491*4882a593Smuzhiyun 	led_cdev = &tpacpi_led_kbdlight.led_classdev;
5492*4882a593Smuzhiyun 	led_update_brightness(led_cdev);
5493*4882a593Smuzhiyun 	led_classdev_suspend(led_cdev);
5494*4882a593Smuzhiyun }
5495*4882a593Smuzhiyun 
kbdlight_resume(void)5496*4882a593Smuzhiyun static void kbdlight_resume(void)
5497*4882a593Smuzhiyun {
5498*4882a593Smuzhiyun 	if (!tp_features.kbdlight)
5499*4882a593Smuzhiyun 		return;
5500*4882a593Smuzhiyun 
5501*4882a593Smuzhiyun 	led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5502*4882a593Smuzhiyun }
5503*4882a593Smuzhiyun 
5504*4882a593Smuzhiyun static struct ibm_struct kbdlight_driver_data = {
5505*4882a593Smuzhiyun 	.name = "kbdlight",
5506*4882a593Smuzhiyun 	.read = kbdlight_read,
5507*4882a593Smuzhiyun 	.write = kbdlight_write,
5508*4882a593Smuzhiyun 	.suspend = kbdlight_suspend,
5509*4882a593Smuzhiyun 	.resume = kbdlight_resume,
5510*4882a593Smuzhiyun 	.exit = kbdlight_exit,
5511*4882a593Smuzhiyun };
5512*4882a593Smuzhiyun 
5513*4882a593Smuzhiyun /*************************************************************************
5514*4882a593Smuzhiyun  * Light (thinklight) subdriver
5515*4882a593Smuzhiyun  */
5516*4882a593Smuzhiyun 
5517*4882a593Smuzhiyun TPACPI_HANDLE(lght, root, "\\LGHT");	/* A21e, A2xm/p, T20-22, X20-21 */
5518*4882a593Smuzhiyun TPACPI_HANDLE(ledb, ec, "LEDB");		/* G4x */
5519*4882a593Smuzhiyun 
light_get_status(void)5520*4882a593Smuzhiyun static int light_get_status(void)
5521*4882a593Smuzhiyun {
5522*4882a593Smuzhiyun 	int status = 0;
5523*4882a593Smuzhiyun 
5524*4882a593Smuzhiyun 	if (tp_features.light_status) {
5525*4882a593Smuzhiyun 		if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5526*4882a593Smuzhiyun 			return -EIO;
5527*4882a593Smuzhiyun 		return (!!status);
5528*4882a593Smuzhiyun 	}
5529*4882a593Smuzhiyun 
5530*4882a593Smuzhiyun 	return -ENXIO;
5531*4882a593Smuzhiyun }
5532*4882a593Smuzhiyun 
light_set_status(int status)5533*4882a593Smuzhiyun static int light_set_status(int status)
5534*4882a593Smuzhiyun {
5535*4882a593Smuzhiyun 	int rc;
5536*4882a593Smuzhiyun 
5537*4882a593Smuzhiyun 	if (tp_features.light) {
5538*4882a593Smuzhiyun 		if (cmos_handle) {
5539*4882a593Smuzhiyun 			rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5540*4882a593Smuzhiyun 					(status) ?
5541*4882a593Smuzhiyun 						TP_CMOS_THINKLIGHT_ON :
5542*4882a593Smuzhiyun 						TP_CMOS_THINKLIGHT_OFF);
5543*4882a593Smuzhiyun 		} else {
5544*4882a593Smuzhiyun 			rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5545*4882a593Smuzhiyun 					(status) ? 1 : 0);
5546*4882a593Smuzhiyun 		}
5547*4882a593Smuzhiyun 		return (rc) ? 0 : -EIO;
5548*4882a593Smuzhiyun 	}
5549*4882a593Smuzhiyun 
5550*4882a593Smuzhiyun 	return -ENXIO;
5551*4882a593Smuzhiyun }
5552*4882a593Smuzhiyun 
light_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5553*4882a593Smuzhiyun static int light_sysfs_set(struct led_classdev *led_cdev,
5554*4882a593Smuzhiyun 			enum led_brightness brightness)
5555*4882a593Smuzhiyun {
5556*4882a593Smuzhiyun 	return light_set_status((brightness != LED_OFF) ?
5557*4882a593Smuzhiyun 				TPACPI_LED_ON : TPACPI_LED_OFF);
5558*4882a593Smuzhiyun }
5559*4882a593Smuzhiyun 
light_sysfs_get(struct led_classdev * led_cdev)5560*4882a593Smuzhiyun static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5561*4882a593Smuzhiyun {
5562*4882a593Smuzhiyun 	return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5563*4882a593Smuzhiyun }
5564*4882a593Smuzhiyun 
5565*4882a593Smuzhiyun static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5566*4882a593Smuzhiyun 	.led_classdev = {
5567*4882a593Smuzhiyun 		.name		= "tpacpi::thinklight",
5568*4882a593Smuzhiyun 		.brightness_set_blocking = &light_sysfs_set,
5569*4882a593Smuzhiyun 		.brightness_get	= &light_sysfs_get,
5570*4882a593Smuzhiyun 	}
5571*4882a593Smuzhiyun };
5572*4882a593Smuzhiyun 
light_init(struct ibm_init_struct * iibm)5573*4882a593Smuzhiyun static int __init light_init(struct ibm_init_struct *iibm)
5574*4882a593Smuzhiyun {
5575*4882a593Smuzhiyun 	int rc;
5576*4882a593Smuzhiyun 
5577*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5578*4882a593Smuzhiyun 
5579*4882a593Smuzhiyun 	if (tpacpi_is_ibm()) {
5580*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(ledb);
5581*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(lght);
5582*4882a593Smuzhiyun 	}
5583*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(cmos);
5584*4882a593Smuzhiyun 
5585*4882a593Smuzhiyun 	/* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5586*4882a593Smuzhiyun 	tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5587*4882a593Smuzhiyun 
5588*4882a593Smuzhiyun 	if (tp_features.light)
5589*4882a593Smuzhiyun 		/* light status not supported on
5590*4882a593Smuzhiyun 		   570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5591*4882a593Smuzhiyun 		tp_features.light_status =
5592*4882a593Smuzhiyun 			acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5593*4882a593Smuzhiyun 
5594*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5595*4882a593Smuzhiyun 		str_supported(tp_features.light),
5596*4882a593Smuzhiyun 		str_supported(tp_features.light_status));
5597*4882a593Smuzhiyun 
5598*4882a593Smuzhiyun 	if (!tp_features.light)
5599*4882a593Smuzhiyun 		return 1;
5600*4882a593Smuzhiyun 
5601*4882a593Smuzhiyun 	rc = led_classdev_register(&tpacpi_pdev->dev,
5602*4882a593Smuzhiyun 				   &tpacpi_led_thinklight.led_classdev);
5603*4882a593Smuzhiyun 
5604*4882a593Smuzhiyun 	if (rc < 0) {
5605*4882a593Smuzhiyun 		tp_features.light = 0;
5606*4882a593Smuzhiyun 		tp_features.light_status = 0;
5607*4882a593Smuzhiyun 	} else  {
5608*4882a593Smuzhiyun 		rc = 0;
5609*4882a593Smuzhiyun 	}
5610*4882a593Smuzhiyun 
5611*4882a593Smuzhiyun 	return rc;
5612*4882a593Smuzhiyun }
5613*4882a593Smuzhiyun 
light_exit(void)5614*4882a593Smuzhiyun static void light_exit(void)
5615*4882a593Smuzhiyun {
5616*4882a593Smuzhiyun 	led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5617*4882a593Smuzhiyun }
5618*4882a593Smuzhiyun 
light_read(struct seq_file * m)5619*4882a593Smuzhiyun static int light_read(struct seq_file *m)
5620*4882a593Smuzhiyun {
5621*4882a593Smuzhiyun 	int status;
5622*4882a593Smuzhiyun 
5623*4882a593Smuzhiyun 	if (!tp_features.light) {
5624*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
5625*4882a593Smuzhiyun 	} else if (!tp_features.light_status) {
5626*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tunknown\n");
5627*4882a593Smuzhiyun 		seq_printf(m, "commands:\ton, off\n");
5628*4882a593Smuzhiyun 	} else {
5629*4882a593Smuzhiyun 		status = light_get_status();
5630*4882a593Smuzhiyun 		if (status < 0)
5631*4882a593Smuzhiyun 			return status;
5632*4882a593Smuzhiyun 		seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5633*4882a593Smuzhiyun 		seq_printf(m, "commands:\ton, off\n");
5634*4882a593Smuzhiyun 	}
5635*4882a593Smuzhiyun 
5636*4882a593Smuzhiyun 	return 0;
5637*4882a593Smuzhiyun }
5638*4882a593Smuzhiyun 
light_write(char * buf)5639*4882a593Smuzhiyun static int light_write(char *buf)
5640*4882a593Smuzhiyun {
5641*4882a593Smuzhiyun 	char *cmd;
5642*4882a593Smuzhiyun 	int newstatus = 0;
5643*4882a593Smuzhiyun 
5644*4882a593Smuzhiyun 	if (!tp_features.light)
5645*4882a593Smuzhiyun 		return -ENODEV;
5646*4882a593Smuzhiyun 
5647*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
5648*4882a593Smuzhiyun 		if (strlencmp(cmd, "on") == 0) {
5649*4882a593Smuzhiyun 			newstatus = 1;
5650*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "off") == 0) {
5651*4882a593Smuzhiyun 			newstatus = 0;
5652*4882a593Smuzhiyun 		} else
5653*4882a593Smuzhiyun 			return -EINVAL;
5654*4882a593Smuzhiyun 	}
5655*4882a593Smuzhiyun 
5656*4882a593Smuzhiyun 	return light_set_status(newstatus);
5657*4882a593Smuzhiyun }
5658*4882a593Smuzhiyun 
5659*4882a593Smuzhiyun static struct ibm_struct light_driver_data = {
5660*4882a593Smuzhiyun 	.name = "light",
5661*4882a593Smuzhiyun 	.read = light_read,
5662*4882a593Smuzhiyun 	.write = light_write,
5663*4882a593Smuzhiyun 	.exit = light_exit,
5664*4882a593Smuzhiyun };
5665*4882a593Smuzhiyun 
5666*4882a593Smuzhiyun /*************************************************************************
5667*4882a593Smuzhiyun  * CMOS subdriver
5668*4882a593Smuzhiyun  */
5669*4882a593Smuzhiyun 
5670*4882a593Smuzhiyun /* sysfs cmos_command -------------------------------------------------- */
cmos_command_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5671*4882a593Smuzhiyun static ssize_t cmos_command_store(struct device *dev,
5672*4882a593Smuzhiyun 			    struct device_attribute *attr,
5673*4882a593Smuzhiyun 			    const char *buf, size_t count)
5674*4882a593Smuzhiyun {
5675*4882a593Smuzhiyun 	unsigned long cmos_cmd;
5676*4882a593Smuzhiyun 	int res;
5677*4882a593Smuzhiyun 
5678*4882a593Smuzhiyun 	if (parse_strtoul(buf, 21, &cmos_cmd))
5679*4882a593Smuzhiyun 		return -EINVAL;
5680*4882a593Smuzhiyun 
5681*4882a593Smuzhiyun 	res = issue_thinkpad_cmos_command(cmos_cmd);
5682*4882a593Smuzhiyun 	return (res) ? res : count;
5683*4882a593Smuzhiyun }
5684*4882a593Smuzhiyun 
5685*4882a593Smuzhiyun static DEVICE_ATTR_WO(cmos_command);
5686*4882a593Smuzhiyun 
5687*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
5688*4882a593Smuzhiyun 
cmos_init(struct ibm_init_struct * iibm)5689*4882a593Smuzhiyun static int __init cmos_init(struct ibm_init_struct *iibm)
5690*4882a593Smuzhiyun {
5691*4882a593Smuzhiyun 	int res;
5692*4882a593Smuzhiyun 
5693*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT,
5694*4882a593Smuzhiyun 		"initializing cmos commands subdriver\n");
5695*4882a593Smuzhiyun 
5696*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(cmos);
5697*4882a593Smuzhiyun 
5698*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5699*4882a593Smuzhiyun 		str_supported(cmos_handle != NULL));
5700*4882a593Smuzhiyun 
5701*4882a593Smuzhiyun 	res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5702*4882a593Smuzhiyun 	if (res)
5703*4882a593Smuzhiyun 		return res;
5704*4882a593Smuzhiyun 
5705*4882a593Smuzhiyun 	return (cmos_handle) ? 0 : 1;
5706*4882a593Smuzhiyun }
5707*4882a593Smuzhiyun 
cmos_exit(void)5708*4882a593Smuzhiyun static void cmos_exit(void)
5709*4882a593Smuzhiyun {
5710*4882a593Smuzhiyun 	device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5711*4882a593Smuzhiyun }
5712*4882a593Smuzhiyun 
cmos_read(struct seq_file * m)5713*4882a593Smuzhiyun static int cmos_read(struct seq_file *m)
5714*4882a593Smuzhiyun {
5715*4882a593Smuzhiyun 	/* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5716*4882a593Smuzhiyun 	   R30, R31, T20-22, X20-21 */
5717*4882a593Smuzhiyun 	if (!cmos_handle)
5718*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
5719*4882a593Smuzhiyun 	else {
5720*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tsupported\n");
5721*4882a593Smuzhiyun 		seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5722*4882a593Smuzhiyun 	}
5723*4882a593Smuzhiyun 
5724*4882a593Smuzhiyun 	return 0;
5725*4882a593Smuzhiyun }
5726*4882a593Smuzhiyun 
cmos_write(char * buf)5727*4882a593Smuzhiyun static int cmos_write(char *buf)
5728*4882a593Smuzhiyun {
5729*4882a593Smuzhiyun 	char *cmd;
5730*4882a593Smuzhiyun 	int cmos_cmd, res;
5731*4882a593Smuzhiyun 
5732*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
5733*4882a593Smuzhiyun 		if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5734*4882a593Smuzhiyun 		    cmos_cmd >= 0 && cmos_cmd <= 21) {
5735*4882a593Smuzhiyun 			/* cmos_cmd set */
5736*4882a593Smuzhiyun 		} else
5737*4882a593Smuzhiyun 			return -EINVAL;
5738*4882a593Smuzhiyun 
5739*4882a593Smuzhiyun 		res = issue_thinkpad_cmos_command(cmos_cmd);
5740*4882a593Smuzhiyun 		if (res)
5741*4882a593Smuzhiyun 			return res;
5742*4882a593Smuzhiyun 	}
5743*4882a593Smuzhiyun 
5744*4882a593Smuzhiyun 	return 0;
5745*4882a593Smuzhiyun }
5746*4882a593Smuzhiyun 
5747*4882a593Smuzhiyun static struct ibm_struct cmos_driver_data = {
5748*4882a593Smuzhiyun 	.name = "cmos",
5749*4882a593Smuzhiyun 	.read = cmos_read,
5750*4882a593Smuzhiyun 	.write = cmos_write,
5751*4882a593Smuzhiyun 	.exit = cmos_exit,
5752*4882a593Smuzhiyun };
5753*4882a593Smuzhiyun 
5754*4882a593Smuzhiyun /*************************************************************************
5755*4882a593Smuzhiyun  * LED subdriver
5756*4882a593Smuzhiyun  */
5757*4882a593Smuzhiyun 
5758*4882a593Smuzhiyun enum led_access_mode {
5759*4882a593Smuzhiyun 	TPACPI_LED_NONE = 0,
5760*4882a593Smuzhiyun 	TPACPI_LED_570,	/* 570 */
5761*4882a593Smuzhiyun 	TPACPI_LED_OLD,	/* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5762*4882a593Smuzhiyun 	TPACPI_LED_NEW,	/* all others */
5763*4882a593Smuzhiyun };
5764*4882a593Smuzhiyun 
5765*4882a593Smuzhiyun enum {	/* For TPACPI_LED_OLD */
5766*4882a593Smuzhiyun 	TPACPI_LED_EC_HLCL = 0x0c,	/* EC reg to get led to power on */
5767*4882a593Smuzhiyun 	TPACPI_LED_EC_HLBL = 0x0d,	/* EC reg to blink a lit led */
5768*4882a593Smuzhiyun 	TPACPI_LED_EC_HLMS = 0x0e,	/* EC reg to select led to command */
5769*4882a593Smuzhiyun };
5770*4882a593Smuzhiyun 
5771*4882a593Smuzhiyun static enum led_access_mode led_supported;
5772*4882a593Smuzhiyun 
5773*4882a593Smuzhiyun static acpi_handle led_handle;
5774*4882a593Smuzhiyun 
5775*4882a593Smuzhiyun #define TPACPI_LED_NUMLEDS 16
5776*4882a593Smuzhiyun static struct tpacpi_led_classdev *tpacpi_leds;
5777*4882a593Smuzhiyun static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5778*4882a593Smuzhiyun static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5779*4882a593Smuzhiyun 	/* there's a limit of 19 chars + NULL before 2.6.26 */
5780*4882a593Smuzhiyun 	"tpacpi::power",
5781*4882a593Smuzhiyun 	"tpacpi:orange:batt",
5782*4882a593Smuzhiyun 	"tpacpi:green:batt",
5783*4882a593Smuzhiyun 	"tpacpi::dock_active",
5784*4882a593Smuzhiyun 	"tpacpi::bay_active",
5785*4882a593Smuzhiyun 	"tpacpi::dock_batt",
5786*4882a593Smuzhiyun 	"tpacpi::unknown_led",
5787*4882a593Smuzhiyun 	"tpacpi::standby",
5788*4882a593Smuzhiyun 	"tpacpi::dock_status1",
5789*4882a593Smuzhiyun 	"tpacpi::dock_status2",
5790*4882a593Smuzhiyun 	"tpacpi::unknown_led2",
5791*4882a593Smuzhiyun 	"tpacpi::unknown_led3",
5792*4882a593Smuzhiyun 	"tpacpi::thinkvantage",
5793*4882a593Smuzhiyun };
5794*4882a593Smuzhiyun #define TPACPI_SAFE_LEDS	0x1081U
5795*4882a593Smuzhiyun 
tpacpi_is_led_restricted(const unsigned int led)5796*4882a593Smuzhiyun static inline bool tpacpi_is_led_restricted(const unsigned int led)
5797*4882a593Smuzhiyun {
5798*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5799*4882a593Smuzhiyun 	return false;
5800*4882a593Smuzhiyun #else
5801*4882a593Smuzhiyun 	return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5802*4882a593Smuzhiyun #endif
5803*4882a593Smuzhiyun }
5804*4882a593Smuzhiyun 
led_get_status(const unsigned int led)5805*4882a593Smuzhiyun static int led_get_status(const unsigned int led)
5806*4882a593Smuzhiyun {
5807*4882a593Smuzhiyun 	int status;
5808*4882a593Smuzhiyun 	enum led_status_t led_s;
5809*4882a593Smuzhiyun 
5810*4882a593Smuzhiyun 	switch (led_supported) {
5811*4882a593Smuzhiyun 	case TPACPI_LED_570:
5812*4882a593Smuzhiyun 		if (!acpi_evalf(ec_handle,
5813*4882a593Smuzhiyun 				&status, "GLED", "dd", 1 << led))
5814*4882a593Smuzhiyun 			return -EIO;
5815*4882a593Smuzhiyun 		led_s = (status == 0) ?
5816*4882a593Smuzhiyun 				TPACPI_LED_OFF :
5817*4882a593Smuzhiyun 				((status == 1) ?
5818*4882a593Smuzhiyun 					TPACPI_LED_ON :
5819*4882a593Smuzhiyun 					TPACPI_LED_BLINK);
5820*4882a593Smuzhiyun 		tpacpi_led_state_cache[led] = led_s;
5821*4882a593Smuzhiyun 		return led_s;
5822*4882a593Smuzhiyun 	default:
5823*4882a593Smuzhiyun 		return -ENXIO;
5824*4882a593Smuzhiyun 	}
5825*4882a593Smuzhiyun 
5826*4882a593Smuzhiyun 	/* not reached */
5827*4882a593Smuzhiyun }
5828*4882a593Smuzhiyun 
led_set_status(const unsigned int led,const enum led_status_t ledstatus)5829*4882a593Smuzhiyun static int led_set_status(const unsigned int led,
5830*4882a593Smuzhiyun 			  const enum led_status_t ledstatus)
5831*4882a593Smuzhiyun {
5832*4882a593Smuzhiyun 	/* off, on, blink. Index is led_status_t */
5833*4882a593Smuzhiyun 	static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5834*4882a593Smuzhiyun 	static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5835*4882a593Smuzhiyun 
5836*4882a593Smuzhiyun 	int rc = 0;
5837*4882a593Smuzhiyun 
5838*4882a593Smuzhiyun 	switch (led_supported) {
5839*4882a593Smuzhiyun 	case TPACPI_LED_570:
5840*4882a593Smuzhiyun 		/* 570 */
5841*4882a593Smuzhiyun 		if (unlikely(led > 7))
5842*4882a593Smuzhiyun 			return -EINVAL;
5843*4882a593Smuzhiyun 		if (unlikely(tpacpi_is_led_restricted(led)))
5844*4882a593Smuzhiyun 			return -EPERM;
5845*4882a593Smuzhiyun 		if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5846*4882a593Smuzhiyun 				(1 << led), led_sled_arg1[ledstatus]))
5847*4882a593Smuzhiyun 			return -EIO;
5848*4882a593Smuzhiyun 		break;
5849*4882a593Smuzhiyun 	case TPACPI_LED_OLD:
5850*4882a593Smuzhiyun 		/* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5851*4882a593Smuzhiyun 		if (unlikely(led > 7))
5852*4882a593Smuzhiyun 			return -EINVAL;
5853*4882a593Smuzhiyun 		if (unlikely(tpacpi_is_led_restricted(led)))
5854*4882a593Smuzhiyun 			return -EPERM;
5855*4882a593Smuzhiyun 		rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5856*4882a593Smuzhiyun 		if (rc >= 0)
5857*4882a593Smuzhiyun 			rc = ec_write(TPACPI_LED_EC_HLBL,
5858*4882a593Smuzhiyun 				      (ledstatus == TPACPI_LED_BLINK) << led);
5859*4882a593Smuzhiyun 		if (rc >= 0)
5860*4882a593Smuzhiyun 			rc = ec_write(TPACPI_LED_EC_HLCL,
5861*4882a593Smuzhiyun 				      (ledstatus != TPACPI_LED_OFF) << led);
5862*4882a593Smuzhiyun 		break;
5863*4882a593Smuzhiyun 	case TPACPI_LED_NEW:
5864*4882a593Smuzhiyun 		/* all others */
5865*4882a593Smuzhiyun 		if (unlikely(led >= TPACPI_LED_NUMLEDS))
5866*4882a593Smuzhiyun 			return -EINVAL;
5867*4882a593Smuzhiyun 		if (unlikely(tpacpi_is_led_restricted(led)))
5868*4882a593Smuzhiyun 			return -EPERM;
5869*4882a593Smuzhiyun 		if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5870*4882a593Smuzhiyun 				led, led_led_arg1[ledstatus]))
5871*4882a593Smuzhiyun 			return -EIO;
5872*4882a593Smuzhiyun 		break;
5873*4882a593Smuzhiyun 	default:
5874*4882a593Smuzhiyun 		return -ENXIO;
5875*4882a593Smuzhiyun 	}
5876*4882a593Smuzhiyun 
5877*4882a593Smuzhiyun 	if (!rc)
5878*4882a593Smuzhiyun 		tpacpi_led_state_cache[led] = ledstatus;
5879*4882a593Smuzhiyun 
5880*4882a593Smuzhiyun 	return rc;
5881*4882a593Smuzhiyun }
5882*4882a593Smuzhiyun 
led_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5883*4882a593Smuzhiyun static int led_sysfs_set(struct led_classdev *led_cdev,
5884*4882a593Smuzhiyun 			enum led_brightness brightness)
5885*4882a593Smuzhiyun {
5886*4882a593Smuzhiyun 	struct tpacpi_led_classdev *data = container_of(led_cdev,
5887*4882a593Smuzhiyun 			     struct tpacpi_led_classdev, led_classdev);
5888*4882a593Smuzhiyun 	enum led_status_t new_state;
5889*4882a593Smuzhiyun 
5890*4882a593Smuzhiyun 	if (brightness == LED_OFF)
5891*4882a593Smuzhiyun 		new_state = TPACPI_LED_OFF;
5892*4882a593Smuzhiyun 	else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5893*4882a593Smuzhiyun 		new_state = TPACPI_LED_ON;
5894*4882a593Smuzhiyun 	else
5895*4882a593Smuzhiyun 		new_state = TPACPI_LED_BLINK;
5896*4882a593Smuzhiyun 
5897*4882a593Smuzhiyun 	return led_set_status(data->led, new_state);
5898*4882a593Smuzhiyun }
5899*4882a593Smuzhiyun 
led_sysfs_blink_set(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)5900*4882a593Smuzhiyun static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5901*4882a593Smuzhiyun 			unsigned long *delay_on, unsigned long *delay_off)
5902*4882a593Smuzhiyun {
5903*4882a593Smuzhiyun 	struct tpacpi_led_classdev *data = container_of(led_cdev,
5904*4882a593Smuzhiyun 			     struct tpacpi_led_classdev, led_classdev);
5905*4882a593Smuzhiyun 
5906*4882a593Smuzhiyun 	/* Can we choose the flash rate? */
5907*4882a593Smuzhiyun 	if (*delay_on == 0 && *delay_off == 0) {
5908*4882a593Smuzhiyun 		/* yes. set them to the hardware blink rate (1 Hz) */
5909*4882a593Smuzhiyun 		*delay_on = 500; /* ms */
5910*4882a593Smuzhiyun 		*delay_off = 500; /* ms */
5911*4882a593Smuzhiyun 	} else if ((*delay_on != 500) || (*delay_off != 500))
5912*4882a593Smuzhiyun 		return -EINVAL;
5913*4882a593Smuzhiyun 
5914*4882a593Smuzhiyun 	return led_set_status(data->led, TPACPI_LED_BLINK);
5915*4882a593Smuzhiyun }
5916*4882a593Smuzhiyun 
led_sysfs_get(struct led_classdev * led_cdev)5917*4882a593Smuzhiyun static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5918*4882a593Smuzhiyun {
5919*4882a593Smuzhiyun 	int rc;
5920*4882a593Smuzhiyun 
5921*4882a593Smuzhiyun 	struct tpacpi_led_classdev *data = container_of(led_cdev,
5922*4882a593Smuzhiyun 			     struct tpacpi_led_classdev, led_classdev);
5923*4882a593Smuzhiyun 
5924*4882a593Smuzhiyun 	rc = led_get_status(data->led);
5925*4882a593Smuzhiyun 
5926*4882a593Smuzhiyun 	if (rc == TPACPI_LED_OFF || rc < 0)
5927*4882a593Smuzhiyun 		rc = LED_OFF;	/* no error handling in led class :( */
5928*4882a593Smuzhiyun 	else
5929*4882a593Smuzhiyun 		rc = LED_FULL;
5930*4882a593Smuzhiyun 
5931*4882a593Smuzhiyun 	return rc;
5932*4882a593Smuzhiyun }
5933*4882a593Smuzhiyun 
led_exit(void)5934*4882a593Smuzhiyun static void led_exit(void)
5935*4882a593Smuzhiyun {
5936*4882a593Smuzhiyun 	unsigned int i;
5937*4882a593Smuzhiyun 
5938*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5939*4882a593Smuzhiyun 		led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5940*4882a593Smuzhiyun 
5941*4882a593Smuzhiyun 	kfree(tpacpi_leds);
5942*4882a593Smuzhiyun }
5943*4882a593Smuzhiyun 
tpacpi_init_led(unsigned int led)5944*4882a593Smuzhiyun static int __init tpacpi_init_led(unsigned int led)
5945*4882a593Smuzhiyun {
5946*4882a593Smuzhiyun 	/* LEDs with no name don't get registered */
5947*4882a593Smuzhiyun 	if (!tpacpi_led_names[led])
5948*4882a593Smuzhiyun 		return 0;
5949*4882a593Smuzhiyun 
5950*4882a593Smuzhiyun 	tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5951*4882a593Smuzhiyun 	tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5952*4882a593Smuzhiyun 	if (led_supported == TPACPI_LED_570)
5953*4882a593Smuzhiyun 		tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5954*4882a593Smuzhiyun 
5955*4882a593Smuzhiyun 	tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5956*4882a593Smuzhiyun 	tpacpi_leds[led].led = led;
5957*4882a593Smuzhiyun 
5958*4882a593Smuzhiyun 	return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5959*4882a593Smuzhiyun }
5960*4882a593Smuzhiyun 
5961*4882a593Smuzhiyun static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5962*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5963*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5964*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5965*4882a593Smuzhiyun 
5966*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5967*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5968*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5969*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5970*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5971*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5972*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5973*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5974*4882a593Smuzhiyun 
5975*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5976*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5977*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5978*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5979*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5980*4882a593Smuzhiyun 
5981*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5982*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5983*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5984*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5985*4882a593Smuzhiyun 
5986*4882a593Smuzhiyun 	/* (1) - may have excess leds enabled on MSB */
5987*4882a593Smuzhiyun 
5988*4882a593Smuzhiyun 	/* Defaults (order matters, keep last, don't reorder!) */
5989*4882a593Smuzhiyun 	{ /* Lenovo */
5990*4882a593Smuzhiyun 	  .vendor = PCI_VENDOR_ID_LENOVO,
5991*4882a593Smuzhiyun 	  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5992*4882a593Smuzhiyun 	  .quirks = 0x1fffU,
5993*4882a593Smuzhiyun 	},
5994*4882a593Smuzhiyun 	{ /* IBM ThinkPads with no EC version string */
5995*4882a593Smuzhiyun 	  .vendor = PCI_VENDOR_ID_IBM,
5996*4882a593Smuzhiyun 	  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5997*4882a593Smuzhiyun 	  .quirks = 0x00ffU,
5998*4882a593Smuzhiyun 	},
5999*4882a593Smuzhiyun 	{ /* IBM ThinkPads with EC version string */
6000*4882a593Smuzhiyun 	  .vendor = PCI_VENDOR_ID_IBM,
6001*4882a593Smuzhiyun 	  .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6002*4882a593Smuzhiyun 	  .quirks = 0x00bfU,
6003*4882a593Smuzhiyun 	},
6004*4882a593Smuzhiyun };
6005*4882a593Smuzhiyun 
led_init_detect_mode(void)6006*4882a593Smuzhiyun static enum led_access_mode __init led_init_detect_mode(void)
6007*4882a593Smuzhiyun {
6008*4882a593Smuzhiyun 	acpi_status status;
6009*4882a593Smuzhiyun 
6010*4882a593Smuzhiyun 	if (tpacpi_is_ibm()) {
6011*4882a593Smuzhiyun 		/* 570 */
6012*4882a593Smuzhiyun 		status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6013*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status))
6014*4882a593Smuzhiyun 			return TPACPI_LED_570;
6015*4882a593Smuzhiyun 
6016*4882a593Smuzhiyun 		/* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6017*4882a593Smuzhiyun 		status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6018*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status))
6019*4882a593Smuzhiyun 			return TPACPI_LED_OLD;
6020*4882a593Smuzhiyun 	}
6021*4882a593Smuzhiyun 
6022*4882a593Smuzhiyun 	/* most others */
6023*4882a593Smuzhiyun 	status = acpi_get_handle(ec_handle, "LED", &led_handle);
6024*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status))
6025*4882a593Smuzhiyun 		return TPACPI_LED_NEW;
6026*4882a593Smuzhiyun 
6027*4882a593Smuzhiyun 	/* R30, R31, and unknown firmwares */
6028*4882a593Smuzhiyun 	led_handle = NULL;
6029*4882a593Smuzhiyun 	return TPACPI_LED_NONE;
6030*4882a593Smuzhiyun }
6031*4882a593Smuzhiyun 
led_init(struct ibm_init_struct * iibm)6032*4882a593Smuzhiyun static int __init led_init(struct ibm_init_struct *iibm)
6033*4882a593Smuzhiyun {
6034*4882a593Smuzhiyun 	unsigned int i;
6035*4882a593Smuzhiyun 	int rc;
6036*4882a593Smuzhiyun 	unsigned long useful_leds;
6037*4882a593Smuzhiyun 
6038*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6039*4882a593Smuzhiyun 
6040*4882a593Smuzhiyun 	led_supported = led_init_detect_mode();
6041*4882a593Smuzhiyun 
6042*4882a593Smuzhiyun 	if (led_supported != TPACPI_LED_NONE) {
6043*4882a593Smuzhiyun 		useful_leds = tpacpi_check_quirks(led_useful_qtable,
6044*4882a593Smuzhiyun 				ARRAY_SIZE(led_useful_qtable));
6045*4882a593Smuzhiyun 
6046*4882a593Smuzhiyun 		if (!useful_leds) {
6047*4882a593Smuzhiyun 			led_handle = NULL;
6048*4882a593Smuzhiyun 			led_supported = TPACPI_LED_NONE;
6049*4882a593Smuzhiyun 		}
6050*4882a593Smuzhiyun 	}
6051*4882a593Smuzhiyun 
6052*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6053*4882a593Smuzhiyun 		str_supported(led_supported), led_supported);
6054*4882a593Smuzhiyun 
6055*4882a593Smuzhiyun 	if (led_supported == TPACPI_LED_NONE)
6056*4882a593Smuzhiyun 		return 1;
6057*4882a593Smuzhiyun 
6058*4882a593Smuzhiyun 	tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6059*4882a593Smuzhiyun 			      GFP_KERNEL);
6060*4882a593Smuzhiyun 	if (!tpacpi_leds) {
6061*4882a593Smuzhiyun 		pr_err("Out of memory for LED data\n");
6062*4882a593Smuzhiyun 		return -ENOMEM;
6063*4882a593Smuzhiyun 	}
6064*4882a593Smuzhiyun 
6065*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6066*4882a593Smuzhiyun 		tpacpi_leds[i].led = -1;
6067*4882a593Smuzhiyun 
6068*4882a593Smuzhiyun 		if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
6069*4882a593Smuzhiyun 			rc = tpacpi_init_led(i);
6070*4882a593Smuzhiyun 			if (rc < 0) {
6071*4882a593Smuzhiyun 				led_exit();
6072*4882a593Smuzhiyun 				return rc;
6073*4882a593Smuzhiyun 			}
6074*4882a593Smuzhiyun 		}
6075*4882a593Smuzhiyun 	}
6076*4882a593Smuzhiyun 
6077*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6078*4882a593Smuzhiyun 	pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6079*4882a593Smuzhiyun #endif
6080*4882a593Smuzhiyun 	return 0;
6081*4882a593Smuzhiyun }
6082*4882a593Smuzhiyun 
6083*4882a593Smuzhiyun #define str_led_status(s) \
6084*4882a593Smuzhiyun 	((s) == TPACPI_LED_OFF ? "off" : \
6085*4882a593Smuzhiyun 		((s) == TPACPI_LED_ON ? "on" : "blinking"))
6086*4882a593Smuzhiyun 
led_read(struct seq_file * m)6087*4882a593Smuzhiyun static int led_read(struct seq_file *m)
6088*4882a593Smuzhiyun {
6089*4882a593Smuzhiyun 	if (!led_supported) {
6090*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
6091*4882a593Smuzhiyun 		return 0;
6092*4882a593Smuzhiyun 	}
6093*4882a593Smuzhiyun 	seq_printf(m, "status:\t\tsupported\n");
6094*4882a593Smuzhiyun 
6095*4882a593Smuzhiyun 	if (led_supported == TPACPI_LED_570) {
6096*4882a593Smuzhiyun 		/* 570 */
6097*4882a593Smuzhiyun 		int i, status;
6098*4882a593Smuzhiyun 		for (i = 0; i < 8; i++) {
6099*4882a593Smuzhiyun 			status = led_get_status(i);
6100*4882a593Smuzhiyun 			if (status < 0)
6101*4882a593Smuzhiyun 				return -EIO;
6102*4882a593Smuzhiyun 			seq_printf(m, "%d:\t\t%s\n",
6103*4882a593Smuzhiyun 				       i, str_led_status(status));
6104*4882a593Smuzhiyun 		}
6105*4882a593Smuzhiyun 	}
6106*4882a593Smuzhiyun 
6107*4882a593Smuzhiyun 	seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6108*4882a593Smuzhiyun 
6109*4882a593Smuzhiyun 	return 0;
6110*4882a593Smuzhiyun }
6111*4882a593Smuzhiyun 
led_write(char * buf)6112*4882a593Smuzhiyun static int led_write(char *buf)
6113*4882a593Smuzhiyun {
6114*4882a593Smuzhiyun 	char *cmd;
6115*4882a593Smuzhiyun 	int led, rc;
6116*4882a593Smuzhiyun 	enum led_status_t s;
6117*4882a593Smuzhiyun 
6118*4882a593Smuzhiyun 	if (!led_supported)
6119*4882a593Smuzhiyun 		return -ENODEV;
6120*4882a593Smuzhiyun 
6121*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
6122*4882a593Smuzhiyun 		if (sscanf(cmd, "%d", &led) != 1)
6123*4882a593Smuzhiyun 			return -EINVAL;
6124*4882a593Smuzhiyun 
6125*4882a593Smuzhiyun 		if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6126*4882a593Smuzhiyun 			return -ENODEV;
6127*4882a593Smuzhiyun 
6128*4882a593Smuzhiyun 		if (tpacpi_leds[led].led < 0)
6129*4882a593Smuzhiyun 			return -ENODEV;
6130*4882a593Smuzhiyun 
6131*4882a593Smuzhiyun 		if (strstr(cmd, "off")) {
6132*4882a593Smuzhiyun 			s = TPACPI_LED_OFF;
6133*4882a593Smuzhiyun 		} else if (strstr(cmd, "on")) {
6134*4882a593Smuzhiyun 			s = TPACPI_LED_ON;
6135*4882a593Smuzhiyun 		} else if (strstr(cmd, "blink")) {
6136*4882a593Smuzhiyun 			s = TPACPI_LED_BLINK;
6137*4882a593Smuzhiyun 		} else {
6138*4882a593Smuzhiyun 			return -EINVAL;
6139*4882a593Smuzhiyun 		}
6140*4882a593Smuzhiyun 
6141*4882a593Smuzhiyun 		rc = led_set_status(led, s);
6142*4882a593Smuzhiyun 		if (rc < 0)
6143*4882a593Smuzhiyun 			return rc;
6144*4882a593Smuzhiyun 	}
6145*4882a593Smuzhiyun 
6146*4882a593Smuzhiyun 	return 0;
6147*4882a593Smuzhiyun }
6148*4882a593Smuzhiyun 
6149*4882a593Smuzhiyun static struct ibm_struct led_driver_data = {
6150*4882a593Smuzhiyun 	.name = "led",
6151*4882a593Smuzhiyun 	.read = led_read,
6152*4882a593Smuzhiyun 	.write = led_write,
6153*4882a593Smuzhiyun 	.exit = led_exit,
6154*4882a593Smuzhiyun };
6155*4882a593Smuzhiyun 
6156*4882a593Smuzhiyun /*************************************************************************
6157*4882a593Smuzhiyun  * Beep subdriver
6158*4882a593Smuzhiyun  */
6159*4882a593Smuzhiyun 
6160*4882a593Smuzhiyun TPACPI_HANDLE(beep, ec, "BEEP");	/* all except R30, R31 */
6161*4882a593Smuzhiyun 
6162*4882a593Smuzhiyun #define TPACPI_BEEP_Q1 0x0001
6163*4882a593Smuzhiyun 
6164*4882a593Smuzhiyun static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6165*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6166*4882a593Smuzhiyun 	TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6167*4882a593Smuzhiyun };
6168*4882a593Smuzhiyun 
beep_init(struct ibm_init_struct * iibm)6169*4882a593Smuzhiyun static int __init beep_init(struct ibm_init_struct *iibm)
6170*4882a593Smuzhiyun {
6171*4882a593Smuzhiyun 	unsigned long quirks;
6172*4882a593Smuzhiyun 
6173*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6174*4882a593Smuzhiyun 
6175*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(beep);
6176*4882a593Smuzhiyun 
6177*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6178*4882a593Smuzhiyun 		str_supported(beep_handle != NULL));
6179*4882a593Smuzhiyun 
6180*4882a593Smuzhiyun 	quirks = tpacpi_check_quirks(beep_quirk_table,
6181*4882a593Smuzhiyun 				     ARRAY_SIZE(beep_quirk_table));
6182*4882a593Smuzhiyun 
6183*4882a593Smuzhiyun 	tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6184*4882a593Smuzhiyun 
6185*4882a593Smuzhiyun 	return (beep_handle) ? 0 : 1;
6186*4882a593Smuzhiyun }
6187*4882a593Smuzhiyun 
beep_read(struct seq_file * m)6188*4882a593Smuzhiyun static int beep_read(struct seq_file *m)
6189*4882a593Smuzhiyun {
6190*4882a593Smuzhiyun 	if (!beep_handle)
6191*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
6192*4882a593Smuzhiyun 	else {
6193*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tsupported\n");
6194*4882a593Smuzhiyun 		seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6195*4882a593Smuzhiyun 	}
6196*4882a593Smuzhiyun 
6197*4882a593Smuzhiyun 	return 0;
6198*4882a593Smuzhiyun }
6199*4882a593Smuzhiyun 
beep_write(char * buf)6200*4882a593Smuzhiyun static int beep_write(char *buf)
6201*4882a593Smuzhiyun {
6202*4882a593Smuzhiyun 	char *cmd;
6203*4882a593Smuzhiyun 	int beep_cmd;
6204*4882a593Smuzhiyun 
6205*4882a593Smuzhiyun 	if (!beep_handle)
6206*4882a593Smuzhiyun 		return -ENODEV;
6207*4882a593Smuzhiyun 
6208*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
6209*4882a593Smuzhiyun 		if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6210*4882a593Smuzhiyun 		    beep_cmd >= 0 && beep_cmd <= 17) {
6211*4882a593Smuzhiyun 			/* beep_cmd set */
6212*4882a593Smuzhiyun 		} else
6213*4882a593Smuzhiyun 			return -EINVAL;
6214*4882a593Smuzhiyun 		if (tp_features.beep_needs_two_args) {
6215*4882a593Smuzhiyun 			if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6216*4882a593Smuzhiyun 					beep_cmd, 0))
6217*4882a593Smuzhiyun 				return -EIO;
6218*4882a593Smuzhiyun 		} else {
6219*4882a593Smuzhiyun 			if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6220*4882a593Smuzhiyun 					beep_cmd))
6221*4882a593Smuzhiyun 				return -EIO;
6222*4882a593Smuzhiyun 		}
6223*4882a593Smuzhiyun 	}
6224*4882a593Smuzhiyun 
6225*4882a593Smuzhiyun 	return 0;
6226*4882a593Smuzhiyun }
6227*4882a593Smuzhiyun 
6228*4882a593Smuzhiyun static struct ibm_struct beep_driver_data = {
6229*4882a593Smuzhiyun 	.name = "beep",
6230*4882a593Smuzhiyun 	.read = beep_read,
6231*4882a593Smuzhiyun 	.write = beep_write,
6232*4882a593Smuzhiyun };
6233*4882a593Smuzhiyun 
6234*4882a593Smuzhiyun /*************************************************************************
6235*4882a593Smuzhiyun  * Thermal subdriver
6236*4882a593Smuzhiyun  */
6237*4882a593Smuzhiyun 
6238*4882a593Smuzhiyun enum thermal_access_mode {
6239*4882a593Smuzhiyun 	TPACPI_THERMAL_NONE = 0,	/* No thermal support */
6240*4882a593Smuzhiyun 	TPACPI_THERMAL_ACPI_TMP07,	/* Use ACPI TMP0-7 */
6241*4882a593Smuzhiyun 	TPACPI_THERMAL_ACPI_UPDT,	/* Use ACPI TMP0-7 with UPDT */
6242*4882a593Smuzhiyun 	TPACPI_THERMAL_TPEC_8,		/* Use ACPI EC regs, 8 sensors */
6243*4882a593Smuzhiyun 	TPACPI_THERMAL_TPEC_16,		/* Use ACPI EC regs, 16 sensors */
6244*4882a593Smuzhiyun };
6245*4882a593Smuzhiyun 
6246*4882a593Smuzhiyun enum { /* TPACPI_THERMAL_TPEC_* */
6247*4882a593Smuzhiyun 	TP_EC_THERMAL_TMP0 = 0x78,	/* ACPI EC regs TMP 0..7 */
6248*4882a593Smuzhiyun 	TP_EC_THERMAL_TMP8 = 0xC0,	/* ACPI EC regs TMP 8..15 */
6249*4882a593Smuzhiyun 	TP_EC_FUNCREV      = 0xEF,      /* ACPI EC Functional revision */
6250*4882a593Smuzhiyun 	TP_EC_THERMAL_TMP_NA = -128,	/* ACPI EC sensor not available */
6251*4882a593Smuzhiyun 
6252*4882a593Smuzhiyun 	TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6253*4882a593Smuzhiyun };
6254*4882a593Smuzhiyun 
6255*4882a593Smuzhiyun 
6256*4882a593Smuzhiyun #define TPACPI_MAX_THERMAL_SENSORS 16	/* Max thermal sensors supported */
6257*4882a593Smuzhiyun struct ibm_thermal_sensors_struct {
6258*4882a593Smuzhiyun 	s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6259*4882a593Smuzhiyun };
6260*4882a593Smuzhiyun 
6261*4882a593Smuzhiyun static enum thermal_access_mode thermal_read_mode;
6262*4882a593Smuzhiyun 
6263*4882a593Smuzhiyun /* idx is zero-based */
thermal_get_sensor(int idx,s32 * value)6264*4882a593Smuzhiyun static int thermal_get_sensor(int idx, s32 *value)
6265*4882a593Smuzhiyun {
6266*4882a593Smuzhiyun 	int t;
6267*4882a593Smuzhiyun 	s8 tmp;
6268*4882a593Smuzhiyun 	char tmpi[5];
6269*4882a593Smuzhiyun 
6270*4882a593Smuzhiyun 	t = TP_EC_THERMAL_TMP0;
6271*4882a593Smuzhiyun 
6272*4882a593Smuzhiyun 	switch (thermal_read_mode) {
6273*4882a593Smuzhiyun #if TPACPI_MAX_THERMAL_SENSORS >= 16
6274*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_16:
6275*4882a593Smuzhiyun 		if (idx >= 8 && idx <= 15) {
6276*4882a593Smuzhiyun 			t = TP_EC_THERMAL_TMP8;
6277*4882a593Smuzhiyun 			idx -= 8;
6278*4882a593Smuzhiyun 		}
6279*4882a593Smuzhiyun #endif
6280*4882a593Smuzhiyun 		fallthrough;
6281*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_8:
6282*4882a593Smuzhiyun 		if (idx <= 7) {
6283*4882a593Smuzhiyun 			if (!acpi_ec_read(t + idx, &tmp))
6284*4882a593Smuzhiyun 				return -EIO;
6285*4882a593Smuzhiyun 			*value = tmp * 1000;
6286*4882a593Smuzhiyun 			return 0;
6287*4882a593Smuzhiyun 		}
6288*4882a593Smuzhiyun 		break;
6289*4882a593Smuzhiyun 
6290*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_UPDT:
6291*4882a593Smuzhiyun 		if (idx <= 7) {
6292*4882a593Smuzhiyun 			snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6293*4882a593Smuzhiyun 			if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6294*4882a593Smuzhiyun 				return -EIO;
6295*4882a593Smuzhiyun 			if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6296*4882a593Smuzhiyun 				return -EIO;
6297*4882a593Smuzhiyun 			*value = (t - 2732) * 100;
6298*4882a593Smuzhiyun 			return 0;
6299*4882a593Smuzhiyun 		}
6300*4882a593Smuzhiyun 		break;
6301*4882a593Smuzhiyun 
6302*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_TMP07:
6303*4882a593Smuzhiyun 		if (idx <= 7) {
6304*4882a593Smuzhiyun 			snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6305*4882a593Smuzhiyun 			if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6306*4882a593Smuzhiyun 				return -EIO;
6307*4882a593Smuzhiyun 			if (t > 127 || t < -127)
6308*4882a593Smuzhiyun 				t = TP_EC_THERMAL_TMP_NA;
6309*4882a593Smuzhiyun 			*value = t * 1000;
6310*4882a593Smuzhiyun 			return 0;
6311*4882a593Smuzhiyun 		}
6312*4882a593Smuzhiyun 		break;
6313*4882a593Smuzhiyun 
6314*4882a593Smuzhiyun 	case TPACPI_THERMAL_NONE:
6315*4882a593Smuzhiyun 	default:
6316*4882a593Smuzhiyun 		return -ENOSYS;
6317*4882a593Smuzhiyun 	}
6318*4882a593Smuzhiyun 
6319*4882a593Smuzhiyun 	return -EINVAL;
6320*4882a593Smuzhiyun }
6321*4882a593Smuzhiyun 
thermal_get_sensors(struct ibm_thermal_sensors_struct * s)6322*4882a593Smuzhiyun static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6323*4882a593Smuzhiyun {
6324*4882a593Smuzhiyun 	int res, i;
6325*4882a593Smuzhiyun 	int n;
6326*4882a593Smuzhiyun 
6327*4882a593Smuzhiyun 	n = 8;
6328*4882a593Smuzhiyun 	i = 0;
6329*4882a593Smuzhiyun 
6330*4882a593Smuzhiyun 	if (!s)
6331*4882a593Smuzhiyun 		return -EINVAL;
6332*4882a593Smuzhiyun 
6333*4882a593Smuzhiyun 	if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6334*4882a593Smuzhiyun 		n = 16;
6335*4882a593Smuzhiyun 
6336*4882a593Smuzhiyun 	for (i = 0 ; i < n; i++) {
6337*4882a593Smuzhiyun 		res = thermal_get_sensor(i, &s->temp[i]);
6338*4882a593Smuzhiyun 		if (res)
6339*4882a593Smuzhiyun 			return res;
6340*4882a593Smuzhiyun 	}
6341*4882a593Smuzhiyun 
6342*4882a593Smuzhiyun 	return n;
6343*4882a593Smuzhiyun }
6344*4882a593Smuzhiyun 
thermal_dump_all_sensors(void)6345*4882a593Smuzhiyun static void thermal_dump_all_sensors(void)
6346*4882a593Smuzhiyun {
6347*4882a593Smuzhiyun 	int n, i;
6348*4882a593Smuzhiyun 	struct ibm_thermal_sensors_struct t;
6349*4882a593Smuzhiyun 
6350*4882a593Smuzhiyun 	n = thermal_get_sensors(&t);
6351*4882a593Smuzhiyun 	if (n <= 0)
6352*4882a593Smuzhiyun 		return;
6353*4882a593Smuzhiyun 
6354*4882a593Smuzhiyun 	pr_notice("temperatures (Celsius):");
6355*4882a593Smuzhiyun 
6356*4882a593Smuzhiyun 	for (i = 0; i < n; i++) {
6357*4882a593Smuzhiyun 		if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6358*4882a593Smuzhiyun 			pr_cont(" %d", (int)(t.temp[i] / 1000));
6359*4882a593Smuzhiyun 		else
6360*4882a593Smuzhiyun 			pr_cont(" N/A");
6361*4882a593Smuzhiyun 	}
6362*4882a593Smuzhiyun 
6363*4882a593Smuzhiyun 	pr_cont("\n");
6364*4882a593Smuzhiyun }
6365*4882a593Smuzhiyun 
6366*4882a593Smuzhiyun /* sysfs temp##_input -------------------------------------------------- */
6367*4882a593Smuzhiyun 
thermal_temp_input_show(struct device * dev,struct device_attribute * attr,char * buf)6368*4882a593Smuzhiyun static ssize_t thermal_temp_input_show(struct device *dev,
6369*4882a593Smuzhiyun 			   struct device_attribute *attr,
6370*4882a593Smuzhiyun 			   char *buf)
6371*4882a593Smuzhiyun {
6372*4882a593Smuzhiyun 	struct sensor_device_attribute *sensor_attr =
6373*4882a593Smuzhiyun 					to_sensor_dev_attr(attr);
6374*4882a593Smuzhiyun 	int idx = sensor_attr->index;
6375*4882a593Smuzhiyun 	s32 value;
6376*4882a593Smuzhiyun 	int res;
6377*4882a593Smuzhiyun 
6378*4882a593Smuzhiyun 	res = thermal_get_sensor(idx, &value);
6379*4882a593Smuzhiyun 	if (res)
6380*4882a593Smuzhiyun 		return res;
6381*4882a593Smuzhiyun 	if (value == TPACPI_THERMAL_SENSOR_NA)
6382*4882a593Smuzhiyun 		return -ENXIO;
6383*4882a593Smuzhiyun 
6384*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
6385*4882a593Smuzhiyun }
6386*4882a593Smuzhiyun 
6387*4882a593Smuzhiyun #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6388*4882a593Smuzhiyun 	 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6389*4882a593Smuzhiyun 		     thermal_temp_input_show, NULL, _idxB)
6390*4882a593Smuzhiyun 
6391*4882a593Smuzhiyun static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6392*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(1, 0),
6393*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(2, 1),
6394*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(3, 2),
6395*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(4, 3),
6396*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(5, 4),
6397*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(6, 5),
6398*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(7, 6),
6399*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(8, 7),
6400*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(9, 8),
6401*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(10, 9),
6402*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(11, 10),
6403*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(12, 11),
6404*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(13, 12),
6405*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(14, 13),
6406*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(15, 14),
6407*4882a593Smuzhiyun 	THERMAL_SENSOR_ATTR_TEMP(16, 15),
6408*4882a593Smuzhiyun };
6409*4882a593Smuzhiyun 
6410*4882a593Smuzhiyun #define THERMAL_ATTRS(X) \
6411*4882a593Smuzhiyun 	&sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6412*4882a593Smuzhiyun 
6413*4882a593Smuzhiyun static struct attribute *thermal_temp_input_attr[] = {
6414*4882a593Smuzhiyun 	THERMAL_ATTRS(8),
6415*4882a593Smuzhiyun 	THERMAL_ATTRS(9),
6416*4882a593Smuzhiyun 	THERMAL_ATTRS(10),
6417*4882a593Smuzhiyun 	THERMAL_ATTRS(11),
6418*4882a593Smuzhiyun 	THERMAL_ATTRS(12),
6419*4882a593Smuzhiyun 	THERMAL_ATTRS(13),
6420*4882a593Smuzhiyun 	THERMAL_ATTRS(14),
6421*4882a593Smuzhiyun 	THERMAL_ATTRS(15),
6422*4882a593Smuzhiyun 	THERMAL_ATTRS(0),
6423*4882a593Smuzhiyun 	THERMAL_ATTRS(1),
6424*4882a593Smuzhiyun 	THERMAL_ATTRS(2),
6425*4882a593Smuzhiyun 	THERMAL_ATTRS(3),
6426*4882a593Smuzhiyun 	THERMAL_ATTRS(4),
6427*4882a593Smuzhiyun 	THERMAL_ATTRS(5),
6428*4882a593Smuzhiyun 	THERMAL_ATTRS(6),
6429*4882a593Smuzhiyun 	THERMAL_ATTRS(7),
6430*4882a593Smuzhiyun 	NULL
6431*4882a593Smuzhiyun };
6432*4882a593Smuzhiyun 
6433*4882a593Smuzhiyun static const struct attribute_group thermal_temp_input16_group = {
6434*4882a593Smuzhiyun 	.attrs = thermal_temp_input_attr
6435*4882a593Smuzhiyun };
6436*4882a593Smuzhiyun 
6437*4882a593Smuzhiyun static const struct attribute_group thermal_temp_input8_group = {
6438*4882a593Smuzhiyun 	.attrs = &thermal_temp_input_attr[8]
6439*4882a593Smuzhiyun };
6440*4882a593Smuzhiyun 
6441*4882a593Smuzhiyun #undef THERMAL_SENSOR_ATTR_TEMP
6442*4882a593Smuzhiyun #undef THERMAL_ATTRS
6443*4882a593Smuzhiyun 
6444*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
6445*4882a593Smuzhiyun 
thermal_init(struct ibm_init_struct * iibm)6446*4882a593Smuzhiyun static int __init thermal_init(struct ibm_init_struct *iibm)
6447*4882a593Smuzhiyun {
6448*4882a593Smuzhiyun 	u8 t, ta1, ta2, ver = 0;
6449*4882a593Smuzhiyun 	int i;
6450*4882a593Smuzhiyun 	int acpi_tmp7;
6451*4882a593Smuzhiyun 	int res;
6452*4882a593Smuzhiyun 
6453*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6454*4882a593Smuzhiyun 
6455*4882a593Smuzhiyun 	acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6456*4882a593Smuzhiyun 
6457*4882a593Smuzhiyun 	if (thinkpad_id.ec_model) {
6458*4882a593Smuzhiyun 		/*
6459*4882a593Smuzhiyun 		 * Direct EC access mode: sensors at registers
6460*4882a593Smuzhiyun 		 * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
6461*4882a593Smuzhiyun 		 * non-implemented, thermal sensors return 0x80 when
6462*4882a593Smuzhiyun 		 * not available
6463*4882a593Smuzhiyun 		 * The above rule is unfortunately flawed. This has been seen with
6464*4882a593Smuzhiyun 		 * 0xC2 (power supply ID) causing thermal control problems.
6465*4882a593Smuzhiyun 		 * The EC version can be determined by offset 0xEF and at least for
6466*4882a593Smuzhiyun 		 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6467*4882a593Smuzhiyun 		 * are not thermal registers.
6468*4882a593Smuzhiyun 		 */
6469*4882a593Smuzhiyun 		if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6470*4882a593Smuzhiyun 			pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun 		ta1 = ta2 = 0;
6473*4882a593Smuzhiyun 		for (i = 0; i < 8; i++) {
6474*4882a593Smuzhiyun 			if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6475*4882a593Smuzhiyun 				ta1 |= t;
6476*4882a593Smuzhiyun 			} else {
6477*4882a593Smuzhiyun 				ta1 = 0;
6478*4882a593Smuzhiyun 				break;
6479*4882a593Smuzhiyun 			}
6480*4882a593Smuzhiyun 			if (ver < 3) {
6481*4882a593Smuzhiyun 				if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6482*4882a593Smuzhiyun 					ta2 |= t;
6483*4882a593Smuzhiyun 				} else {
6484*4882a593Smuzhiyun 					ta1 = 0;
6485*4882a593Smuzhiyun 					break;
6486*4882a593Smuzhiyun 				}
6487*4882a593Smuzhiyun 			}
6488*4882a593Smuzhiyun 		}
6489*4882a593Smuzhiyun 		if (ta1 == 0) {
6490*4882a593Smuzhiyun 			/* This is sheer paranoia, but we handle it anyway */
6491*4882a593Smuzhiyun 			if (acpi_tmp7) {
6492*4882a593Smuzhiyun 				pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6493*4882a593Smuzhiyun 				thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6494*4882a593Smuzhiyun 			} else {
6495*4882a593Smuzhiyun 				pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6496*4882a593Smuzhiyun 				thermal_read_mode = TPACPI_THERMAL_NONE;
6497*4882a593Smuzhiyun 			}
6498*4882a593Smuzhiyun 		} else {
6499*4882a593Smuzhiyun 			if (ver >= 3)
6500*4882a593Smuzhiyun 				thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6501*4882a593Smuzhiyun 			else
6502*4882a593Smuzhiyun 				thermal_read_mode =
6503*4882a593Smuzhiyun 					(ta2 != 0) ?
6504*4882a593Smuzhiyun 					TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6505*4882a593Smuzhiyun 		}
6506*4882a593Smuzhiyun 	} else if (acpi_tmp7) {
6507*4882a593Smuzhiyun 		if (tpacpi_is_ibm() &&
6508*4882a593Smuzhiyun 		    acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6509*4882a593Smuzhiyun 			/* 600e/x, 770e, 770x */
6510*4882a593Smuzhiyun 			thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6511*4882a593Smuzhiyun 		} else {
6512*4882a593Smuzhiyun 			/* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6513*4882a593Smuzhiyun 			thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6514*4882a593Smuzhiyun 		}
6515*4882a593Smuzhiyun 	} else {
6516*4882a593Smuzhiyun 		/* temperatures not supported on 570, G4x, R30, R31, R32 */
6517*4882a593Smuzhiyun 		thermal_read_mode = TPACPI_THERMAL_NONE;
6518*4882a593Smuzhiyun 	}
6519*4882a593Smuzhiyun 
6520*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6521*4882a593Smuzhiyun 		str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6522*4882a593Smuzhiyun 		thermal_read_mode);
6523*4882a593Smuzhiyun 
6524*4882a593Smuzhiyun 	switch (thermal_read_mode) {
6525*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_16:
6526*4882a593Smuzhiyun 		res = sysfs_create_group(&tpacpi_hwmon->kobj,
6527*4882a593Smuzhiyun 				&thermal_temp_input16_group);
6528*4882a593Smuzhiyun 		if (res)
6529*4882a593Smuzhiyun 			return res;
6530*4882a593Smuzhiyun 		break;
6531*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_8:
6532*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_TMP07:
6533*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_UPDT:
6534*4882a593Smuzhiyun 		res = sysfs_create_group(&tpacpi_hwmon->kobj,
6535*4882a593Smuzhiyun 				&thermal_temp_input8_group);
6536*4882a593Smuzhiyun 		if (res)
6537*4882a593Smuzhiyun 			return res;
6538*4882a593Smuzhiyun 		break;
6539*4882a593Smuzhiyun 	case TPACPI_THERMAL_NONE:
6540*4882a593Smuzhiyun 	default:
6541*4882a593Smuzhiyun 		return 1;
6542*4882a593Smuzhiyun 	}
6543*4882a593Smuzhiyun 
6544*4882a593Smuzhiyun 	return 0;
6545*4882a593Smuzhiyun }
6546*4882a593Smuzhiyun 
thermal_exit(void)6547*4882a593Smuzhiyun static void thermal_exit(void)
6548*4882a593Smuzhiyun {
6549*4882a593Smuzhiyun 	switch (thermal_read_mode) {
6550*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_16:
6551*4882a593Smuzhiyun 		sysfs_remove_group(&tpacpi_hwmon->kobj,
6552*4882a593Smuzhiyun 				   &thermal_temp_input16_group);
6553*4882a593Smuzhiyun 		break;
6554*4882a593Smuzhiyun 	case TPACPI_THERMAL_TPEC_8:
6555*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_TMP07:
6556*4882a593Smuzhiyun 	case TPACPI_THERMAL_ACPI_UPDT:
6557*4882a593Smuzhiyun 		sysfs_remove_group(&tpacpi_hwmon->kobj,
6558*4882a593Smuzhiyun 				   &thermal_temp_input8_group);
6559*4882a593Smuzhiyun 		break;
6560*4882a593Smuzhiyun 	case TPACPI_THERMAL_NONE:
6561*4882a593Smuzhiyun 	default:
6562*4882a593Smuzhiyun 		break;
6563*4882a593Smuzhiyun 	}
6564*4882a593Smuzhiyun }
6565*4882a593Smuzhiyun 
thermal_read(struct seq_file * m)6566*4882a593Smuzhiyun static int thermal_read(struct seq_file *m)
6567*4882a593Smuzhiyun {
6568*4882a593Smuzhiyun 	int n, i;
6569*4882a593Smuzhiyun 	struct ibm_thermal_sensors_struct t;
6570*4882a593Smuzhiyun 
6571*4882a593Smuzhiyun 	n = thermal_get_sensors(&t);
6572*4882a593Smuzhiyun 	if (unlikely(n < 0))
6573*4882a593Smuzhiyun 		return n;
6574*4882a593Smuzhiyun 
6575*4882a593Smuzhiyun 	seq_printf(m, "temperatures:\t");
6576*4882a593Smuzhiyun 
6577*4882a593Smuzhiyun 	if (n > 0) {
6578*4882a593Smuzhiyun 		for (i = 0; i < (n - 1); i++)
6579*4882a593Smuzhiyun 			seq_printf(m, "%d ", t.temp[i] / 1000);
6580*4882a593Smuzhiyun 		seq_printf(m, "%d\n", t.temp[i] / 1000);
6581*4882a593Smuzhiyun 	} else
6582*4882a593Smuzhiyun 		seq_printf(m, "not supported\n");
6583*4882a593Smuzhiyun 
6584*4882a593Smuzhiyun 	return 0;
6585*4882a593Smuzhiyun }
6586*4882a593Smuzhiyun 
6587*4882a593Smuzhiyun static struct ibm_struct thermal_driver_data = {
6588*4882a593Smuzhiyun 	.name = "thermal",
6589*4882a593Smuzhiyun 	.read = thermal_read,
6590*4882a593Smuzhiyun 	.exit = thermal_exit,
6591*4882a593Smuzhiyun };
6592*4882a593Smuzhiyun 
6593*4882a593Smuzhiyun /*************************************************************************
6594*4882a593Smuzhiyun  * Backlight/brightness subdriver
6595*4882a593Smuzhiyun  */
6596*4882a593Smuzhiyun 
6597*4882a593Smuzhiyun #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6598*4882a593Smuzhiyun 
6599*4882a593Smuzhiyun /*
6600*4882a593Smuzhiyun  * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6601*4882a593Smuzhiyun  * CMOS NVRAM byte 0x5E, bits 0-3.
6602*4882a593Smuzhiyun  *
6603*4882a593Smuzhiyun  * EC HBRV (0x31) has the following layout
6604*4882a593Smuzhiyun  *   Bit 7: unknown function
6605*4882a593Smuzhiyun  *   Bit 6: unknown function
6606*4882a593Smuzhiyun  *   Bit 5: Z: honour scale changes, NZ: ignore scale changes
6607*4882a593Smuzhiyun  *   Bit 4: must be set to zero to avoid problems
6608*4882a593Smuzhiyun  *   Bit 3-0: backlight brightness level
6609*4882a593Smuzhiyun  *
6610*4882a593Smuzhiyun  * brightness_get_raw returns status data in the HBRV layout
6611*4882a593Smuzhiyun  *
6612*4882a593Smuzhiyun  * WARNING: The X61 has been verified to use HBRV for something else, so
6613*4882a593Smuzhiyun  * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6614*4882a593Smuzhiyun  * testing on the very early *60 Lenovo models...
6615*4882a593Smuzhiyun  */
6616*4882a593Smuzhiyun 
6617*4882a593Smuzhiyun enum {
6618*4882a593Smuzhiyun 	TP_EC_BACKLIGHT = 0x31,
6619*4882a593Smuzhiyun 
6620*4882a593Smuzhiyun 	/* TP_EC_BACKLIGHT bitmasks */
6621*4882a593Smuzhiyun 	TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6622*4882a593Smuzhiyun 	TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6623*4882a593Smuzhiyun 	TP_EC_BACKLIGHT_MAPSW = 0x20,
6624*4882a593Smuzhiyun };
6625*4882a593Smuzhiyun 
6626*4882a593Smuzhiyun enum tpacpi_brightness_access_mode {
6627*4882a593Smuzhiyun 	TPACPI_BRGHT_MODE_AUTO = 0,	/* Not implemented yet */
6628*4882a593Smuzhiyun 	TPACPI_BRGHT_MODE_EC,		/* EC control */
6629*4882a593Smuzhiyun 	TPACPI_BRGHT_MODE_UCMS_STEP,	/* UCMS step-based control */
6630*4882a593Smuzhiyun 	TPACPI_BRGHT_MODE_ECNVRAM,	/* EC control w/ NVRAM store */
6631*4882a593Smuzhiyun 	TPACPI_BRGHT_MODE_MAX
6632*4882a593Smuzhiyun };
6633*4882a593Smuzhiyun 
6634*4882a593Smuzhiyun static struct backlight_device *ibm_backlight_device;
6635*4882a593Smuzhiyun 
6636*4882a593Smuzhiyun static enum tpacpi_brightness_access_mode brightness_mode =
6637*4882a593Smuzhiyun 		TPACPI_BRGHT_MODE_MAX;
6638*4882a593Smuzhiyun 
6639*4882a593Smuzhiyun static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6640*4882a593Smuzhiyun 
6641*4882a593Smuzhiyun static struct mutex brightness_mutex;
6642*4882a593Smuzhiyun 
6643*4882a593Smuzhiyun /* NVRAM brightness access,
6644*4882a593Smuzhiyun  * call with brightness_mutex held! */
tpacpi_brightness_nvram_get(void)6645*4882a593Smuzhiyun static unsigned int tpacpi_brightness_nvram_get(void)
6646*4882a593Smuzhiyun {
6647*4882a593Smuzhiyun 	u8 lnvram;
6648*4882a593Smuzhiyun 
6649*4882a593Smuzhiyun 	lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6650*4882a593Smuzhiyun 		  & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6651*4882a593Smuzhiyun 		  >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6652*4882a593Smuzhiyun 	lnvram &= bright_maxlvl;
6653*4882a593Smuzhiyun 
6654*4882a593Smuzhiyun 	return lnvram;
6655*4882a593Smuzhiyun }
6656*4882a593Smuzhiyun 
tpacpi_brightness_checkpoint_nvram(void)6657*4882a593Smuzhiyun static void tpacpi_brightness_checkpoint_nvram(void)
6658*4882a593Smuzhiyun {
6659*4882a593Smuzhiyun 	u8 lec = 0;
6660*4882a593Smuzhiyun 	u8 b_nvram;
6661*4882a593Smuzhiyun 
6662*4882a593Smuzhiyun 	if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6663*4882a593Smuzhiyun 		return;
6664*4882a593Smuzhiyun 
6665*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_BRGHT,
6666*4882a593Smuzhiyun 		"trying to checkpoint backlight level to NVRAM...\n");
6667*4882a593Smuzhiyun 
6668*4882a593Smuzhiyun 	if (mutex_lock_killable(&brightness_mutex) < 0)
6669*4882a593Smuzhiyun 		return;
6670*4882a593Smuzhiyun 
6671*4882a593Smuzhiyun 	if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6672*4882a593Smuzhiyun 		goto unlock;
6673*4882a593Smuzhiyun 	lec &= TP_EC_BACKLIGHT_LVLMSK;
6674*4882a593Smuzhiyun 	b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6675*4882a593Smuzhiyun 
6676*4882a593Smuzhiyun 	if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6677*4882a593Smuzhiyun 			     >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6678*4882a593Smuzhiyun 		/* NVRAM needs update */
6679*4882a593Smuzhiyun 		b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6680*4882a593Smuzhiyun 				TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6681*4882a593Smuzhiyun 		b_nvram |= lec;
6682*4882a593Smuzhiyun 		nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6683*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_BRGHT,
6684*4882a593Smuzhiyun 			   "updated NVRAM backlight level to %u (0x%02x)\n",
6685*4882a593Smuzhiyun 			   (unsigned int) lec, (unsigned int) b_nvram);
6686*4882a593Smuzhiyun 	} else
6687*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_BRGHT,
6688*4882a593Smuzhiyun 			   "NVRAM backlight level already is %u (0x%02x)\n",
6689*4882a593Smuzhiyun 			   (unsigned int) lec, (unsigned int) b_nvram);
6690*4882a593Smuzhiyun 
6691*4882a593Smuzhiyun unlock:
6692*4882a593Smuzhiyun 	mutex_unlock(&brightness_mutex);
6693*4882a593Smuzhiyun }
6694*4882a593Smuzhiyun 
6695*4882a593Smuzhiyun 
6696*4882a593Smuzhiyun /* call with brightness_mutex held! */
tpacpi_brightness_get_raw(int * status)6697*4882a593Smuzhiyun static int tpacpi_brightness_get_raw(int *status)
6698*4882a593Smuzhiyun {
6699*4882a593Smuzhiyun 	u8 lec = 0;
6700*4882a593Smuzhiyun 
6701*4882a593Smuzhiyun 	switch (brightness_mode) {
6702*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_UCMS_STEP:
6703*4882a593Smuzhiyun 		*status = tpacpi_brightness_nvram_get();
6704*4882a593Smuzhiyun 		return 0;
6705*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_EC:
6706*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_ECNVRAM:
6707*4882a593Smuzhiyun 		if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6708*4882a593Smuzhiyun 			return -EIO;
6709*4882a593Smuzhiyun 		*status = lec;
6710*4882a593Smuzhiyun 		return 0;
6711*4882a593Smuzhiyun 	default:
6712*4882a593Smuzhiyun 		return -ENXIO;
6713*4882a593Smuzhiyun 	}
6714*4882a593Smuzhiyun }
6715*4882a593Smuzhiyun 
6716*4882a593Smuzhiyun /* call with brightness_mutex held! */
6717*4882a593Smuzhiyun /* do NOT call with illegal backlight level value */
tpacpi_brightness_set_ec(unsigned int value)6718*4882a593Smuzhiyun static int tpacpi_brightness_set_ec(unsigned int value)
6719*4882a593Smuzhiyun {
6720*4882a593Smuzhiyun 	u8 lec = 0;
6721*4882a593Smuzhiyun 
6722*4882a593Smuzhiyun 	if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6723*4882a593Smuzhiyun 		return -EIO;
6724*4882a593Smuzhiyun 
6725*4882a593Smuzhiyun 	if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6726*4882a593Smuzhiyun 				(lec & TP_EC_BACKLIGHT_CMDMSK) |
6727*4882a593Smuzhiyun 				(value & TP_EC_BACKLIGHT_LVLMSK))))
6728*4882a593Smuzhiyun 		return -EIO;
6729*4882a593Smuzhiyun 
6730*4882a593Smuzhiyun 	return 0;
6731*4882a593Smuzhiyun }
6732*4882a593Smuzhiyun 
6733*4882a593Smuzhiyun /* call with brightness_mutex held! */
tpacpi_brightness_set_ucmsstep(unsigned int value)6734*4882a593Smuzhiyun static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6735*4882a593Smuzhiyun {
6736*4882a593Smuzhiyun 	int cmos_cmd, inc;
6737*4882a593Smuzhiyun 	unsigned int current_value, i;
6738*4882a593Smuzhiyun 
6739*4882a593Smuzhiyun 	current_value = tpacpi_brightness_nvram_get();
6740*4882a593Smuzhiyun 
6741*4882a593Smuzhiyun 	if (value == current_value)
6742*4882a593Smuzhiyun 		return 0;
6743*4882a593Smuzhiyun 
6744*4882a593Smuzhiyun 	cmos_cmd = (value > current_value) ?
6745*4882a593Smuzhiyun 			TP_CMOS_BRIGHTNESS_UP :
6746*4882a593Smuzhiyun 			TP_CMOS_BRIGHTNESS_DOWN;
6747*4882a593Smuzhiyun 	inc = (value > current_value) ? 1 : -1;
6748*4882a593Smuzhiyun 
6749*4882a593Smuzhiyun 	for (i = current_value; i != value; i += inc)
6750*4882a593Smuzhiyun 		if (issue_thinkpad_cmos_command(cmos_cmd))
6751*4882a593Smuzhiyun 			return -EIO;
6752*4882a593Smuzhiyun 
6753*4882a593Smuzhiyun 	return 0;
6754*4882a593Smuzhiyun }
6755*4882a593Smuzhiyun 
6756*4882a593Smuzhiyun /* May return EINTR which can always be mapped to ERESTARTSYS */
brightness_set(unsigned int value)6757*4882a593Smuzhiyun static int brightness_set(unsigned int value)
6758*4882a593Smuzhiyun {
6759*4882a593Smuzhiyun 	int res;
6760*4882a593Smuzhiyun 
6761*4882a593Smuzhiyun 	if (value > bright_maxlvl)
6762*4882a593Smuzhiyun 		return -EINVAL;
6763*4882a593Smuzhiyun 
6764*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_BRGHT,
6765*4882a593Smuzhiyun 			"set backlight level to %d\n", value);
6766*4882a593Smuzhiyun 
6767*4882a593Smuzhiyun 	res = mutex_lock_killable(&brightness_mutex);
6768*4882a593Smuzhiyun 	if (res < 0)
6769*4882a593Smuzhiyun 		return res;
6770*4882a593Smuzhiyun 
6771*4882a593Smuzhiyun 	switch (brightness_mode) {
6772*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_EC:
6773*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_ECNVRAM:
6774*4882a593Smuzhiyun 		res = tpacpi_brightness_set_ec(value);
6775*4882a593Smuzhiyun 		break;
6776*4882a593Smuzhiyun 	case TPACPI_BRGHT_MODE_UCMS_STEP:
6777*4882a593Smuzhiyun 		res = tpacpi_brightness_set_ucmsstep(value);
6778*4882a593Smuzhiyun 		break;
6779*4882a593Smuzhiyun 	default:
6780*4882a593Smuzhiyun 		res = -ENXIO;
6781*4882a593Smuzhiyun 	}
6782*4882a593Smuzhiyun 
6783*4882a593Smuzhiyun 	mutex_unlock(&brightness_mutex);
6784*4882a593Smuzhiyun 	return res;
6785*4882a593Smuzhiyun }
6786*4882a593Smuzhiyun 
6787*4882a593Smuzhiyun /* sysfs backlight class ----------------------------------------------- */
6788*4882a593Smuzhiyun 
brightness_update_status(struct backlight_device * bd)6789*4882a593Smuzhiyun static int brightness_update_status(struct backlight_device *bd)
6790*4882a593Smuzhiyun {
6791*4882a593Smuzhiyun 	unsigned int level =
6792*4882a593Smuzhiyun 		(bd->props.fb_blank == FB_BLANK_UNBLANK &&
6793*4882a593Smuzhiyun 		 bd->props.power == FB_BLANK_UNBLANK) ?
6794*4882a593Smuzhiyun 				bd->props.brightness : 0;
6795*4882a593Smuzhiyun 
6796*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_BRGHT,
6797*4882a593Smuzhiyun 			"backlight: attempt to set level to %d\n",
6798*4882a593Smuzhiyun 			level);
6799*4882a593Smuzhiyun 
6800*4882a593Smuzhiyun 	/* it is the backlight class's job (caller) to handle
6801*4882a593Smuzhiyun 	 * EINTR and other errors properly */
6802*4882a593Smuzhiyun 	return brightness_set(level);
6803*4882a593Smuzhiyun }
6804*4882a593Smuzhiyun 
brightness_get(struct backlight_device * bd)6805*4882a593Smuzhiyun static int brightness_get(struct backlight_device *bd)
6806*4882a593Smuzhiyun {
6807*4882a593Smuzhiyun 	int status, res;
6808*4882a593Smuzhiyun 
6809*4882a593Smuzhiyun 	res = mutex_lock_killable(&brightness_mutex);
6810*4882a593Smuzhiyun 	if (res < 0)
6811*4882a593Smuzhiyun 		return 0;
6812*4882a593Smuzhiyun 
6813*4882a593Smuzhiyun 	res = tpacpi_brightness_get_raw(&status);
6814*4882a593Smuzhiyun 
6815*4882a593Smuzhiyun 	mutex_unlock(&brightness_mutex);
6816*4882a593Smuzhiyun 
6817*4882a593Smuzhiyun 	if (res < 0)
6818*4882a593Smuzhiyun 		return 0;
6819*4882a593Smuzhiyun 
6820*4882a593Smuzhiyun 	return status & TP_EC_BACKLIGHT_LVLMSK;
6821*4882a593Smuzhiyun }
6822*4882a593Smuzhiyun 
tpacpi_brightness_notify_change(void)6823*4882a593Smuzhiyun static void tpacpi_brightness_notify_change(void)
6824*4882a593Smuzhiyun {
6825*4882a593Smuzhiyun 	backlight_force_update(ibm_backlight_device,
6826*4882a593Smuzhiyun 			       BACKLIGHT_UPDATE_HOTKEY);
6827*4882a593Smuzhiyun }
6828*4882a593Smuzhiyun 
6829*4882a593Smuzhiyun static const struct backlight_ops ibm_backlight_data = {
6830*4882a593Smuzhiyun 	.get_brightness = brightness_get,
6831*4882a593Smuzhiyun 	.update_status  = brightness_update_status,
6832*4882a593Smuzhiyun };
6833*4882a593Smuzhiyun 
6834*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
6835*4882a593Smuzhiyun 
6836*4882a593Smuzhiyun /*
6837*4882a593Smuzhiyun  * Call _BCL method of video device.  On some ThinkPads this will
6838*4882a593Smuzhiyun  * switch the firmware to the ACPI brightness control mode.
6839*4882a593Smuzhiyun  */
6840*4882a593Smuzhiyun 
tpacpi_query_bcl_levels(acpi_handle handle)6841*4882a593Smuzhiyun static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6842*4882a593Smuzhiyun {
6843*4882a593Smuzhiyun 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6844*4882a593Smuzhiyun 	union acpi_object *obj;
6845*4882a593Smuzhiyun 	struct acpi_device *device, *child;
6846*4882a593Smuzhiyun 	int rc;
6847*4882a593Smuzhiyun 
6848*4882a593Smuzhiyun 	if (acpi_bus_get_device(handle, &device))
6849*4882a593Smuzhiyun 		return 0;
6850*4882a593Smuzhiyun 
6851*4882a593Smuzhiyun 	rc = 0;
6852*4882a593Smuzhiyun 	list_for_each_entry(child, &device->children, node) {
6853*4882a593Smuzhiyun 		acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6854*4882a593Smuzhiyun 							  NULL, &buffer);
6855*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
6856*4882a593Smuzhiyun 			buffer.length = ACPI_ALLOCATE_BUFFER;
6857*4882a593Smuzhiyun 			continue;
6858*4882a593Smuzhiyun 		}
6859*4882a593Smuzhiyun 
6860*4882a593Smuzhiyun 		obj = (union acpi_object *)buffer.pointer;
6861*4882a593Smuzhiyun 		if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6862*4882a593Smuzhiyun 			pr_err("Unknown _BCL data, please report this to %s\n",
6863*4882a593Smuzhiyun 				TPACPI_MAIL);
6864*4882a593Smuzhiyun 			rc = 0;
6865*4882a593Smuzhiyun 		} else {
6866*4882a593Smuzhiyun 			rc = obj->package.count;
6867*4882a593Smuzhiyun 		}
6868*4882a593Smuzhiyun 		break;
6869*4882a593Smuzhiyun 	}
6870*4882a593Smuzhiyun 
6871*4882a593Smuzhiyun 	kfree(buffer.pointer);
6872*4882a593Smuzhiyun 	return rc;
6873*4882a593Smuzhiyun }
6874*4882a593Smuzhiyun 
6875*4882a593Smuzhiyun 
6876*4882a593Smuzhiyun /*
6877*4882a593Smuzhiyun  * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6878*4882a593Smuzhiyun  */
tpacpi_check_std_acpi_brightness_support(void)6879*4882a593Smuzhiyun static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6880*4882a593Smuzhiyun {
6881*4882a593Smuzhiyun 	acpi_handle video_device;
6882*4882a593Smuzhiyun 	int bcl_levels = 0;
6883*4882a593Smuzhiyun 
6884*4882a593Smuzhiyun 	tpacpi_acpi_handle_locate("video", NULL, &video_device);
6885*4882a593Smuzhiyun 	if (video_device)
6886*4882a593Smuzhiyun 		bcl_levels = tpacpi_query_bcl_levels(video_device);
6887*4882a593Smuzhiyun 
6888*4882a593Smuzhiyun 	tp_features.bright_acpimode = (bcl_levels > 0);
6889*4882a593Smuzhiyun 
6890*4882a593Smuzhiyun 	return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6891*4882a593Smuzhiyun }
6892*4882a593Smuzhiyun 
6893*4882a593Smuzhiyun /*
6894*4882a593Smuzhiyun  * These are only useful for models that have only one possibility
6895*4882a593Smuzhiyun  * of GPU.  If the BIOS model handles both ATI and Intel, don't use
6896*4882a593Smuzhiyun  * these quirks.
6897*4882a593Smuzhiyun  */
6898*4882a593Smuzhiyun #define TPACPI_BRGHT_Q_NOEC	0x0001	/* Must NOT use EC HBRV */
6899*4882a593Smuzhiyun #define TPACPI_BRGHT_Q_EC	0x0002  /* Should or must use EC HBRV */
6900*4882a593Smuzhiyun #define TPACPI_BRGHT_Q_ASK	0x8000	/* Ask for user report */
6901*4882a593Smuzhiyun 
6902*4882a593Smuzhiyun static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6903*4882a593Smuzhiyun 	/* Models with ATI GPUs known to require ECNVRAM mode */
6904*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC),	/* T43/p ATI */
6905*4882a593Smuzhiyun 
6906*4882a593Smuzhiyun 	/* Models with ATI GPUs that can use ECNVRAM */
6907*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),	/* R50,51 T40-42 */
6908*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6909*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC),	/* R52 */
6910*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6911*4882a593Smuzhiyun 
6912*4882a593Smuzhiyun 	/* Models with Intel Extreme Graphics 2 */
6913*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),	/* X40 */
6914*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6915*4882a593Smuzhiyun 	TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6916*4882a593Smuzhiyun 
6917*4882a593Smuzhiyun 	/* Models with Intel GMA900 */
6918*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC),	/* T43, R52 */
6919*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC),	/* X41 */
6920*4882a593Smuzhiyun 	TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC),	/* X41 Tablet */
6921*4882a593Smuzhiyun };
6922*4882a593Smuzhiyun 
6923*4882a593Smuzhiyun /*
6924*4882a593Smuzhiyun  * Returns < 0 for error, otherwise sets tp_features.bright_*
6925*4882a593Smuzhiyun  * and bright_maxlvl.
6926*4882a593Smuzhiyun  */
tpacpi_detect_brightness_capabilities(void)6927*4882a593Smuzhiyun static void __init tpacpi_detect_brightness_capabilities(void)
6928*4882a593Smuzhiyun {
6929*4882a593Smuzhiyun 	unsigned int b;
6930*4882a593Smuzhiyun 
6931*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT,
6932*4882a593Smuzhiyun 		    "detecting firmware brightness interface capabilities\n");
6933*4882a593Smuzhiyun 
6934*4882a593Smuzhiyun 	/* we could run a quirks check here (same table used by
6935*4882a593Smuzhiyun 	 * brightness_init) if needed */
6936*4882a593Smuzhiyun 
6937*4882a593Smuzhiyun 	/*
6938*4882a593Smuzhiyun 	 * We always attempt to detect acpi support, so as to switch
6939*4882a593Smuzhiyun 	 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6940*4882a593Smuzhiyun 	 * going to publish a backlight interface
6941*4882a593Smuzhiyun 	 */
6942*4882a593Smuzhiyun 	b = tpacpi_check_std_acpi_brightness_support();
6943*4882a593Smuzhiyun 	switch (b) {
6944*4882a593Smuzhiyun 	case 16:
6945*4882a593Smuzhiyun 		bright_maxlvl = 15;
6946*4882a593Smuzhiyun 		break;
6947*4882a593Smuzhiyun 	case 8:
6948*4882a593Smuzhiyun 	case 0:
6949*4882a593Smuzhiyun 		bright_maxlvl = 7;
6950*4882a593Smuzhiyun 		break;
6951*4882a593Smuzhiyun 	default:
6952*4882a593Smuzhiyun 		tp_features.bright_unkfw = 1;
6953*4882a593Smuzhiyun 		bright_maxlvl = b - 1;
6954*4882a593Smuzhiyun 	}
6955*4882a593Smuzhiyun 	pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6956*4882a593Smuzhiyun }
6957*4882a593Smuzhiyun 
brightness_init(struct ibm_init_struct * iibm)6958*4882a593Smuzhiyun static int __init brightness_init(struct ibm_init_struct *iibm)
6959*4882a593Smuzhiyun {
6960*4882a593Smuzhiyun 	struct backlight_properties props;
6961*4882a593Smuzhiyun 	int b;
6962*4882a593Smuzhiyun 	unsigned long quirks;
6963*4882a593Smuzhiyun 
6964*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6965*4882a593Smuzhiyun 
6966*4882a593Smuzhiyun 	mutex_init(&brightness_mutex);
6967*4882a593Smuzhiyun 
6968*4882a593Smuzhiyun 	quirks = tpacpi_check_quirks(brightness_quirk_table,
6969*4882a593Smuzhiyun 				ARRAY_SIZE(brightness_quirk_table));
6970*4882a593Smuzhiyun 
6971*4882a593Smuzhiyun 	/* tpacpi_detect_brightness_capabilities() must have run already */
6972*4882a593Smuzhiyun 
6973*4882a593Smuzhiyun 	/* if it is unknown, we don't handle it: it wouldn't be safe */
6974*4882a593Smuzhiyun 	if (tp_features.bright_unkfw)
6975*4882a593Smuzhiyun 		return 1;
6976*4882a593Smuzhiyun 
6977*4882a593Smuzhiyun 	if (!brightness_enable) {
6978*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6979*4882a593Smuzhiyun 			   "brightness support disabled by module parameter\n");
6980*4882a593Smuzhiyun 		return 1;
6981*4882a593Smuzhiyun 	}
6982*4882a593Smuzhiyun 
6983*4882a593Smuzhiyun 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6984*4882a593Smuzhiyun 		if (brightness_enable > 1) {
6985*4882a593Smuzhiyun 			pr_info("Standard ACPI backlight interface available, not loading native one\n");
6986*4882a593Smuzhiyun 			return 1;
6987*4882a593Smuzhiyun 		} else if (brightness_enable == 1) {
6988*4882a593Smuzhiyun 			pr_warn("Cannot enable backlight brightness support, ACPI is already handling it.  Refer to the acpi_backlight kernel parameter.\n");
6989*4882a593Smuzhiyun 			return 1;
6990*4882a593Smuzhiyun 		}
6991*4882a593Smuzhiyun 	} else if (!tp_features.bright_acpimode) {
6992*4882a593Smuzhiyun 		pr_notice("ACPI backlight interface not available\n");
6993*4882a593Smuzhiyun 		return 1;
6994*4882a593Smuzhiyun 	}
6995*4882a593Smuzhiyun 
6996*4882a593Smuzhiyun 	pr_notice("ACPI native brightness control enabled\n");
6997*4882a593Smuzhiyun 
6998*4882a593Smuzhiyun 	/*
6999*4882a593Smuzhiyun 	 * Check for module parameter bogosity, note that we
7000*4882a593Smuzhiyun 	 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7001*4882a593Smuzhiyun 	 * able to detect "unspecified"
7002*4882a593Smuzhiyun 	 */
7003*4882a593Smuzhiyun 	if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7004*4882a593Smuzhiyun 		return -EINVAL;
7005*4882a593Smuzhiyun 
7006*4882a593Smuzhiyun 	/* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7007*4882a593Smuzhiyun 	if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7008*4882a593Smuzhiyun 	    brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7009*4882a593Smuzhiyun 		if (quirks & TPACPI_BRGHT_Q_EC)
7010*4882a593Smuzhiyun 			brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7011*4882a593Smuzhiyun 		else
7012*4882a593Smuzhiyun 			brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7013*4882a593Smuzhiyun 
7014*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_BRGHT,
7015*4882a593Smuzhiyun 			   "driver auto-selected brightness_mode=%d\n",
7016*4882a593Smuzhiyun 			   brightness_mode);
7017*4882a593Smuzhiyun 	}
7018*4882a593Smuzhiyun 
7019*4882a593Smuzhiyun 	/* Safety */
7020*4882a593Smuzhiyun 	if (!tpacpi_is_ibm() &&
7021*4882a593Smuzhiyun 	    (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7022*4882a593Smuzhiyun 	     brightness_mode == TPACPI_BRGHT_MODE_EC))
7023*4882a593Smuzhiyun 		return -EINVAL;
7024*4882a593Smuzhiyun 
7025*4882a593Smuzhiyun 	if (tpacpi_brightness_get_raw(&b) < 0)
7026*4882a593Smuzhiyun 		return 1;
7027*4882a593Smuzhiyun 
7028*4882a593Smuzhiyun 	memset(&props, 0, sizeof(struct backlight_properties));
7029*4882a593Smuzhiyun 	props.type = BACKLIGHT_PLATFORM;
7030*4882a593Smuzhiyun 	props.max_brightness = bright_maxlvl;
7031*4882a593Smuzhiyun 	props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7032*4882a593Smuzhiyun 	ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7033*4882a593Smuzhiyun 							 NULL, NULL,
7034*4882a593Smuzhiyun 							 &ibm_backlight_data,
7035*4882a593Smuzhiyun 							 &props);
7036*4882a593Smuzhiyun 	if (IS_ERR(ibm_backlight_device)) {
7037*4882a593Smuzhiyun 		int rc = PTR_ERR(ibm_backlight_device);
7038*4882a593Smuzhiyun 		ibm_backlight_device = NULL;
7039*4882a593Smuzhiyun 		pr_err("Could not register backlight device\n");
7040*4882a593Smuzhiyun 		return rc;
7041*4882a593Smuzhiyun 	}
7042*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7043*4882a593Smuzhiyun 			"brightness is supported\n");
7044*4882a593Smuzhiyun 
7045*4882a593Smuzhiyun 	if (quirks & TPACPI_BRGHT_Q_ASK) {
7046*4882a593Smuzhiyun 		pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7047*4882a593Smuzhiyun 			  brightness_mode);
7048*4882a593Smuzhiyun 		pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7049*4882a593Smuzhiyun 			  TPACPI_MAIL);
7050*4882a593Smuzhiyun 	}
7051*4882a593Smuzhiyun 
7052*4882a593Smuzhiyun 	/* Added by mistake in early 2007.  Probably useless, but it could
7053*4882a593Smuzhiyun 	 * be working around some unknown firmware problem where the value
7054*4882a593Smuzhiyun 	 * read at startup doesn't match the real hardware state... so leave
7055*4882a593Smuzhiyun 	 * it in place just in case */
7056*4882a593Smuzhiyun 	backlight_update_status(ibm_backlight_device);
7057*4882a593Smuzhiyun 
7058*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7059*4882a593Smuzhiyun 		    "brightness: registering brightness hotkeys as change notification\n");
7060*4882a593Smuzhiyun 	tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7061*4882a593Smuzhiyun 				| TP_ACPI_HKEY_BRGHTUP_MASK
7062*4882a593Smuzhiyun 				| TP_ACPI_HKEY_BRGHTDWN_MASK);
7063*4882a593Smuzhiyun 	return 0;
7064*4882a593Smuzhiyun }
7065*4882a593Smuzhiyun 
brightness_suspend(void)7066*4882a593Smuzhiyun static void brightness_suspend(void)
7067*4882a593Smuzhiyun {
7068*4882a593Smuzhiyun 	tpacpi_brightness_checkpoint_nvram();
7069*4882a593Smuzhiyun }
7070*4882a593Smuzhiyun 
brightness_shutdown(void)7071*4882a593Smuzhiyun static void brightness_shutdown(void)
7072*4882a593Smuzhiyun {
7073*4882a593Smuzhiyun 	tpacpi_brightness_checkpoint_nvram();
7074*4882a593Smuzhiyun }
7075*4882a593Smuzhiyun 
brightness_exit(void)7076*4882a593Smuzhiyun static void brightness_exit(void)
7077*4882a593Smuzhiyun {
7078*4882a593Smuzhiyun 	if (ibm_backlight_device) {
7079*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7080*4882a593Smuzhiyun 			    "calling backlight_device_unregister()\n");
7081*4882a593Smuzhiyun 		backlight_device_unregister(ibm_backlight_device);
7082*4882a593Smuzhiyun 	}
7083*4882a593Smuzhiyun 
7084*4882a593Smuzhiyun 	tpacpi_brightness_checkpoint_nvram();
7085*4882a593Smuzhiyun }
7086*4882a593Smuzhiyun 
brightness_read(struct seq_file * m)7087*4882a593Smuzhiyun static int brightness_read(struct seq_file *m)
7088*4882a593Smuzhiyun {
7089*4882a593Smuzhiyun 	int level;
7090*4882a593Smuzhiyun 
7091*4882a593Smuzhiyun 	level = brightness_get(NULL);
7092*4882a593Smuzhiyun 	if (level < 0) {
7093*4882a593Smuzhiyun 		seq_printf(m, "level:\t\tunreadable\n");
7094*4882a593Smuzhiyun 	} else {
7095*4882a593Smuzhiyun 		seq_printf(m, "level:\t\t%d\n", level);
7096*4882a593Smuzhiyun 		seq_printf(m, "commands:\tup, down\n");
7097*4882a593Smuzhiyun 		seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7098*4882a593Smuzhiyun 			       bright_maxlvl);
7099*4882a593Smuzhiyun 	}
7100*4882a593Smuzhiyun 
7101*4882a593Smuzhiyun 	return 0;
7102*4882a593Smuzhiyun }
7103*4882a593Smuzhiyun 
brightness_write(char * buf)7104*4882a593Smuzhiyun static int brightness_write(char *buf)
7105*4882a593Smuzhiyun {
7106*4882a593Smuzhiyun 	int level;
7107*4882a593Smuzhiyun 	int rc;
7108*4882a593Smuzhiyun 	char *cmd;
7109*4882a593Smuzhiyun 
7110*4882a593Smuzhiyun 	level = brightness_get(NULL);
7111*4882a593Smuzhiyun 	if (level < 0)
7112*4882a593Smuzhiyun 		return level;
7113*4882a593Smuzhiyun 
7114*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
7115*4882a593Smuzhiyun 		if (strlencmp(cmd, "up") == 0) {
7116*4882a593Smuzhiyun 			if (level < bright_maxlvl)
7117*4882a593Smuzhiyun 				level++;
7118*4882a593Smuzhiyun 		} else if (strlencmp(cmd, "down") == 0) {
7119*4882a593Smuzhiyun 			if (level > 0)
7120*4882a593Smuzhiyun 				level--;
7121*4882a593Smuzhiyun 		} else if (sscanf(cmd, "level %d", &level) == 1 &&
7122*4882a593Smuzhiyun 			   level >= 0 && level <= bright_maxlvl) {
7123*4882a593Smuzhiyun 			/* new level set */
7124*4882a593Smuzhiyun 		} else
7125*4882a593Smuzhiyun 			return -EINVAL;
7126*4882a593Smuzhiyun 	}
7127*4882a593Smuzhiyun 
7128*4882a593Smuzhiyun 	tpacpi_disclose_usertask("procfs brightness",
7129*4882a593Smuzhiyun 			"set level to %d\n", level);
7130*4882a593Smuzhiyun 
7131*4882a593Smuzhiyun 	/*
7132*4882a593Smuzhiyun 	 * Now we know what the final level should be, so we try to set it.
7133*4882a593Smuzhiyun 	 * Doing it this way makes the syscall restartable in case of EINTR
7134*4882a593Smuzhiyun 	 */
7135*4882a593Smuzhiyun 	rc = brightness_set(level);
7136*4882a593Smuzhiyun 	if (!rc && ibm_backlight_device)
7137*4882a593Smuzhiyun 		backlight_force_update(ibm_backlight_device,
7138*4882a593Smuzhiyun 					BACKLIGHT_UPDATE_SYSFS);
7139*4882a593Smuzhiyun 	return (rc == -EINTR) ? -ERESTARTSYS : rc;
7140*4882a593Smuzhiyun }
7141*4882a593Smuzhiyun 
7142*4882a593Smuzhiyun static struct ibm_struct brightness_driver_data = {
7143*4882a593Smuzhiyun 	.name = "brightness",
7144*4882a593Smuzhiyun 	.read = brightness_read,
7145*4882a593Smuzhiyun 	.write = brightness_write,
7146*4882a593Smuzhiyun 	.exit = brightness_exit,
7147*4882a593Smuzhiyun 	.suspend = brightness_suspend,
7148*4882a593Smuzhiyun 	.shutdown = brightness_shutdown,
7149*4882a593Smuzhiyun };
7150*4882a593Smuzhiyun 
7151*4882a593Smuzhiyun /*************************************************************************
7152*4882a593Smuzhiyun  * Volume subdriver
7153*4882a593Smuzhiyun  */
7154*4882a593Smuzhiyun 
7155*4882a593Smuzhiyun /*
7156*4882a593Smuzhiyun  * IBM ThinkPads have a simple volume controller with MUTE gating.
7157*4882a593Smuzhiyun  * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7158*4882a593Smuzhiyun  *
7159*4882a593Smuzhiyun  * Since the *61 series (and probably also the later *60 series), Lenovo
7160*4882a593Smuzhiyun  * ThinkPads only implement the MUTE gate.
7161*4882a593Smuzhiyun  *
7162*4882a593Smuzhiyun  * EC register 0x30
7163*4882a593Smuzhiyun  *   Bit 6: MUTE (1 mutes sound)
7164*4882a593Smuzhiyun  *   Bit 3-0: Volume
7165*4882a593Smuzhiyun  *   Other bits should be zero as far as we know.
7166*4882a593Smuzhiyun  *
7167*4882a593Smuzhiyun  * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7168*4882a593Smuzhiyun  * bits 3-0 (volume).  Other bits in NVRAM may have other functions,
7169*4882a593Smuzhiyun  * such as bit 7 which is used to detect repeated presses of MUTE,
7170*4882a593Smuzhiyun  * and we leave them unchanged.
7171*4882a593Smuzhiyun  *
7172*4882a593Smuzhiyun  * On newer Lenovo ThinkPads, the EC can automatically change the volume
7173*4882a593Smuzhiyun  * in response to user input.  Unfortunately, this rarely works well.
7174*4882a593Smuzhiyun  * The laptop changes the state of its internal MUTE gate and, on some
7175*4882a593Smuzhiyun  * models, sends KEY_MUTE, causing any user code that responds to the
7176*4882a593Smuzhiyun  * mute button to get confused.  The hardware MUTE gate is also
7177*4882a593Smuzhiyun  * unnecessary, since user code can handle the mute button without
7178*4882a593Smuzhiyun  * kernel or EC help.
7179*4882a593Smuzhiyun  *
7180*4882a593Smuzhiyun  * To avoid confusing userspace, we simply disable all EC-based mute
7181*4882a593Smuzhiyun  * and volume controls when possible.
7182*4882a593Smuzhiyun  */
7183*4882a593Smuzhiyun 
7184*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7185*4882a593Smuzhiyun 
7186*4882a593Smuzhiyun #define TPACPI_ALSA_DRVNAME  "ThinkPad EC"
7187*4882a593Smuzhiyun #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7188*4882a593Smuzhiyun #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7189*4882a593Smuzhiyun 
7190*4882a593Smuzhiyun #if SNDRV_CARDS <= 32
7191*4882a593Smuzhiyun #define DEFAULT_ALSA_IDX		~((1 << (SNDRV_CARDS - 3)) - 1)
7192*4882a593Smuzhiyun #else
7193*4882a593Smuzhiyun #define DEFAULT_ALSA_IDX		~((1 << (32 - 3)) - 1)
7194*4882a593Smuzhiyun #endif
7195*4882a593Smuzhiyun static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7196*4882a593Smuzhiyun static char *alsa_id = "ThinkPadEC";
7197*4882a593Smuzhiyun static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7198*4882a593Smuzhiyun 
7199*4882a593Smuzhiyun struct tpacpi_alsa_data {
7200*4882a593Smuzhiyun 	struct snd_card *card;
7201*4882a593Smuzhiyun 	struct snd_ctl_elem_id *ctl_mute_id;
7202*4882a593Smuzhiyun 	struct snd_ctl_elem_id *ctl_vol_id;
7203*4882a593Smuzhiyun };
7204*4882a593Smuzhiyun 
7205*4882a593Smuzhiyun static struct snd_card *alsa_card;
7206*4882a593Smuzhiyun 
7207*4882a593Smuzhiyun enum {
7208*4882a593Smuzhiyun 	TP_EC_AUDIO = 0x30,
7209*4882a593Smuzhiyun 
7210*4882a593Smuzhiyun 	/* TP_EC_AUDIO bits */
7211*4882a593Smuzhiyun 	TP_EC_AUDIO_MUTESW = 6,
7212*4882a593Smuzhiyun 
7213*4882a593Smuzhiyun 	/* TP_EC_AUDIO bitmasks */
7214*4882a593Smuzhiyun 	TP_EC_AUDIO_LVL_MSK = 0x0F,
7215*4882a593Smuzhiyun 	TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7216*4882a593Smuzhiyun 
7217*4882a593Smuzhiyun 	/* Maximum volume */
7218*4882a593Smuzhiyun 	TP_EC_VOLUME_MAX = 14,
7219*4882a593Smuzhiyun };
7220*4882a593Smuzhiyun 
7221*4882a593Smuzhiyun enum tpacpi_volume_access_mode {
7222*4882a593Smuzhiyun 	TPACPI_VOL_MODE_AUTO = 0,	/* Not implemented yet */
7223*4882a593Smuzhiyun 	TPACPI_VOL_MODE_EC,		/* Pure EC control */
7224*4882a593Smuzhiyun 	TPACPI_VOL_MODE_UCMS_STEP,	/* UCMS step-based control: N/A */
7225*4882a593Smuzhiyun 	TPACPI_VOL_MODE_ECNVRAM,	/* EC control w/ NVRAM store */
7226*4882a593Smuzhiyun 	TPACPI_VOL_MODE_MAX
7227*4882a593Smuzhiyun };
7228*4882a593Smuzhiyun 
7229*4882a593Smuzhiyun enum tpacpi_volume_capabilities {
7230*4882a593Smuzhiyun 	TPACPI_VOL_CAP_AUTO = 0,	/* Use white/blacklist */
7231*4882a593Smuzhiyun 	TPACPI_VOL_CAP_VOLMUTE,		/* Output vol and mute */
7232*4882a593Smuzhiyun 	TPACPI_VOL_CAP_MUTEONLY,	/* Output mute only */
7233*4882a593Smuzhiyun 	TPACPI_VOL_CAP_MAX
7234*4882a593Smuzhiyun };
7235*4882a593Smuzhiyun 
7236*4882a593Smuzhiyun enum tpacpi_mute_btn_mode {
7237*4882a593Smuzhiyun 	TP_EC_MUTE_BTN_LATCH  = 0,	/* Mute mutes; up/down unmutes */
7238*4882a593Smuzhiyun 	/* We don't know what mode 1 is. */
7239*4882a593Smuzhiyun 	TP_EC_MUTE_BTN_NONE   = 2,	/* Mute and up/down are just keys */
7240*4882a593Smuzhiyun 	TP_EC_MUTE_BTN_TOGGLE = 3,	/* Mute toggles; up/down unmutes */
7241*4882a593Smuzhiyun };
7242*4882a593Smuzhiyun 
7243*4882a593Smuzhiyun static enum tpacpi_volume_access_mode volume_mode =
7244*4882a593Smuzhiyun 	TPACPI_VOL_MODE_MAX;
7245*4882a593Smuzhiyun 
7246*4882a593Smuzhiyun static enum tpacpi_volume_capabilities volume_capabilities;
7247*4882a593Smuzhiyun static bool volume_control_allowed;
7248*4882a593Smuzhiyun static bool software_mute_requested = true;
7249*4882a593Smuzhiyun static bool software_mute_active;
7250*4882a593Smuzhiyun static int software_mute_orig_mode;
7251*4882a593Smuzhiyun 
7252*4882a593Smuzhiyun /*
7253*4882a593Smuzhiyun  * Used to syncronize writers to TP_EC_AUDIO and
7254*4882a593Smuzhiyun  * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7255*4882a593Smuzhiyun  */
7256*4882a593Smuzhiyun static struct mutex volume_mutex;
7257*4882a593Smuzhiyun 
tpacpi_volume_checkpoint_nvram(void)7258*4882a593Smuzhiyun static void tpacpi_volume_checkpoint_nvram(void)
7259*4882a593Smuzhiyun {
7260*4882a593Smuzhiyun 	u8 lec = 0;
7261*4882a593Smuzhiyun 	u8 b_nvram;
7262*4882a593Smuzhiyun 	u8 ec_mask;
7263*4882a593Smuzhiyun 
7264*4882a593Smuzhiyun 	if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7265*4882a593Smuzhiyun 		return;
7266*4882a593Smuzhiyun 	if (!volume_control_allowed)
7267*4882a593Smuzhiyun 		return;
7268*4882a593Smuzhiyun 	if (software_mute_active)
7269*4882a593Smuzhiyun 		return;
7270*4882a593Smuzhiyun 
7271*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_MIXER,
7272*4882a593Smuzhiyun 		"trying to checkpoint mixer state to NVRAM...\n");
7273*4882a593Smuzhiyun 
7274*4882a593Smuzhiyun 	if (tp_features.mixer_no_level_control)
7275*4882a593Smuzhiyun 		ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7276*4882a593Smuzhiyun 	else
7277*4882a593Smuzhiyun 		ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7278*4882a593Smuzhiyun 
7279*4882a593Smuzhiyun 	if (mutex_lock_killable(&volume_mutex) < 0)
7280*4882a593Smuzhiyun 		return;
7281*4882a593Smuzhiyun 
7282*4882a593Smuzhiyun 	if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7283*4882a593Smuzhiyun 		goto unlock;
7284*4882a593Smuzhiyun 	lec &= ec_mask;
7285*4882a593Smuzhiyun 	b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7286*4882a593Smuzhiyun 
7287*4882a593Smuzhiyun 	if (lec != (b_nvram & ec_mask)) {
7288*4882a593Smuzhiyun 		/* NVRAM needs update */
7289*4882a593Smuzhiyun 		b_nvram &= ~ec_mask;
7290*4882a593Smuzhiyun 		b_nvram |= lec;
7291*4882a593Smuzhiyun 		nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7292*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_MIXER,
7293*4882a593Smuzhiyun 			   "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7294*4882a593Smuzhiyun 			   (unsigned int) lec, (unsigned int) b_nvram);
7295*4882a593Smuzhiyun 	} else {
7296*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_MIXER,
7297*4882a593Smuzhiyun 			   "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7298*4882a593Smuzhiyun 			   (unsigned int) lec, (unsigned int) b_nvram);
7299*4882a593Smuzhiyun 	}
7300*4882a593Smuzhiyun 
7301*4882a593Smuzhiyun unlock:
7302*4882a593Smuzhiyun 	mutex_unlock(&volume_mutex);
7303*4882a593Smuzhiyun }
7304*4882a593Smuzhiyun 
volume_get_status_ec(u8 * status)7305*4882a593Smuzhiyun static int volume_get_status_ec(u8 *status)
7306*4882a593Smuzhiyun {
7307*4882a593Smuzhiyun 	u8 s;
7308*4882a593Smuzhiyun 
7309*4882a593Smuzhiyun 	if (!acpi_ec_read(TP_EC_AUDIO, &s))
7310*4882a593Smuzhiyun 		return -EIO;
7311*4882a593Smuzhiyun 
7312*4882a593Smuzhiyun 	*status = s;
7313*4882a593Smuzhiyun 
7314*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7315*4882a593Smuzhiyun 
7316*4882a593Smuzhiyun 	return 0;
7317*4882a593Smuzhiyun }
7318*4882a593Smuzhiyun 
volume_get_status(u8 * status)7319*4882a593Smuzhiyun static int volume_get_status(u8 *status)
7320*4882a593Smuzhiyun {
7321*4882a593Smuzhiyun 	return volume_get_status_ec(status);
7322*4882a593Smuzhiyun }
7323*4882a593Smuzhiyun 
volume_set_status_ec(const u8 status)7324*4882a593Smuzhiyun static int volume_set_status_ec(const u8 status)
7325*4882a593Smuzhiyun {
7326*4882a593Smuzhiyun 	if (!acpi_ec_write(TP_EC_AUDIO, status))
7327*4882a593Smuzhiyun 		return -EIO;
7328*4882a593Smuzhiyun 
7329*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7330*4882a593Smuzhiyun 
7331*4882a593Smuzhiyun 	/*
7332*4882a593Smuzhiyun 	 * On X200s, and possibly on others, it can take a while for
7333*4882a593Smuzhiyun 	 * reads to become correct.
7334*4882a593Smuzhiyun 	 */
7335*4882a593Smuzhiyun 	msleep(1);
7336*4882a593Smuzhiyun 
7337*4882a593Smuzhiyun 	return 0;
7338*4882a593Smuzhiyun }
7339*4882a593Smuzhiyun 
volume_set_status(const u8 status)7340*4882a593Smuzhiyun static int volume_set_status(const u8 status)
7341*4882a593Smuzhiyun {
7342*4882a593Smuzhiyun 	return volume_set_status_ec(status);
7343*4882a593Smuzhiyun }
7344*4882a593Smuzhiyun 
7345*4882a593Smuzhiyun /* returns < 0 on error, 0 on no change, 1 on change */
__volume_set_mute_ec(const bool mute)7346*4882a593Smuzhiyun static int __volume_set_mute_ec(const bool mute)
7347*4882a593Smuzhiyun {
7348*4882a593Smuzhiyun 	int rc;
7349*4882a593Smuzhiyun 	u8 s, n;
7350*4882a593Smuzhiyun 
7351*4882a593Smuzhiyun 	if (mutex_lock_killable(&volume_mutex) < 0)
7352*4882a593Smuzhiyun 		return -EINTR;
7353*4882a593Smuzhiyun 
7354*4882a593Smuzhiyun 	rc = volume_get_status_ec(&s);
7355*4882a593Smuzhiyun 	if (rc)
7356*4882a593Smuzhiyun 		goto unlock;
7357*4882a593Smuzhiyun 
7358*4882a593Smuzhiyun 	n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7359*4882a593Smuzhiyun 		     s & ~TP_EC_AUDIO_MUTESW_MSK;
7360*4882a593Smuzhiyun 
7361*4882a593Smuzhiyun 	if (n != s) {
7362*4882a593Smuzhiyun 		rc = volume_set_status_ec(n);
7363*4882a593Smuzhiyun 		if (!rc)
7364*4882a593Smuzhiyun 			rc = 1;
7365*4882a593Smuzhiyun 	}
7366*4882a593Smuzhiyun 
7367*4882a593Smuzhiyun unlock:
7368*4882a593Smuzhiyun 	mutex_unlock(&volume_mutex);
7369*4882a593Smuzhiyun 	return rc;
7370*4882a593Smuzhiyun }
7371*4882a593Smuzhiyun 
volume_alsa_set_mute(const bool mute)7372*4882a593Smuzhiyun static int volume_alsa_set_mute(const bool mute)
7373*4882a593Smuzhiyun {
7374*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7375*4882a593Smuzhiyun 		   (mute) ? "" : "un");
7376*4882a593Smuzhiyun 	return __volume_set_mute_ec(mute);
7377*4882a593Smuzhiyun }
7378*4882a593Smuzhiyun 
volume_set_mute(const bool mute)7379*4882a593Smuzhiyun static int volume_set_mute(const bool mute)
7380*4882a593Smuzhiyun {
7381*4882a593Smuzhiyun 	int rc;
7382*4882a593Smuzhiyun 
7383*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7384*4882a593Smuzhiyun 		   (mute) ? "" : "un");
7385*4882a593Smuzhiyun 
7386*4882a593Smuzhiyun 	rc = __volume_set_mute_ec(mute);
7387*4882a593Smuzhiyun 	return (rc < 0) ? rc : 0;
7388*4882a593Smuzhiyun }
7389*4882a593Smuzhiyun 
7390*4882a593Smuzhiyun /* returns < 0 on error, 0 on no change, 1 on change */
__volume_set_volume_ec(const u8 vol)7391*4882a593Smuzhiyun static int __volume_set_volume_ec(const u8 vol)
7392*4882a593Smuzhiyun {
7393*4882a593Smuzhiyun 	int rc;
7394*4882a593Smuzhiyun 	u8 s, n;
7395*4882a593Smuzhiyun 
7396*4882a593Smuzhiyun 	if (vol > TP_EC_VOLUME_MAX)
7397*4882a593Smuzhiyun 		return -EINVAL;
7398*4882a593Smuzhiyun 
7399*4882a593Smuzhiyun 	if (mutex_lock_killable(&volume_mutex) < 0)
7400*4882a593Smuzhiyun 		return -EINTR;
7401*4882a593Smuzhiyun 
7402*4882a593Smuzhiyun 	rc = volume_get_status_ec(&s);
7403*4882a593Smuzhiyun 	if (rc)
7404*4882a593Smuzhiyun 		goto unlock;
7405*4882a593Smuzhiyun 
7406*4882a593Smuzhiyun 	n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7407*4882a593Smuzhiyun 
7408*4882a593Smuzhiyun 	if (n != s) {
7409*4882a593Smuzhiyun 		rc = volume_set_status_ec(n);
7410*4882a593Smuzhiyun 		if (!rc)
7411*4882a593Smuzhiyun 			rc = 1;
7412*4882a593Smuzhiyun 	}
7413*4882a593Smuzhiyun 
7414*4882a593Smuzhiyun unlock:
7415*4882a593Smuzhiyun 	mutex_unlock(&volume_mutex);
7416*4882a593Smuzhiyun 	return rc;
7417*4882a593Smuzhiyun }
7418*4882a593Smuzhiyun 
volume_set_software_mute(bool startup)7419*4882a593Smuzhiyun static int volume_set_software_mute(bool startup)
7420*4882a593Smuzhiyun {
7421*4882a593Smuzhiyun 	int result;
7422*4882a593Smuzhiyun 
7423*4882a593Smuzhiyun 	if (!tpacpi_is_lenovo())
7424*4882a593Smuzhiyun 		return -ENODEV;
7425*4882a593Smuzhiyun 
7426*4882a593Smuzhiyun 	if (startup) {
7427*4882a593Smuzhiyun 		if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7428*4882a593Smuzhiyun 				"HAUM", "qd"))
7429*4882a593Smuzhiyun 			return -EIO;
7430*4882a593Smuzhiyun 
7431*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7432*4882a593Smuzhiyun 			    "Initial HAUM setting was %d\n",
7433*4882a593Smuzhiyun 			    software_mute_orig_mode);
7434*4882a593Smuzhiyun 	}
7435*4882a593Smuzhiyun 
7436*4882a593Smuzhiyun 	if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7437*4882a593Smuzhiyun 			(int)TP_EC_MUTE_BTN_NONE))
7438*4882a593Smuzhiyun 		return -EIO;
7439*4882a593Smuzhiyun 
7440*4882a593Smuzhiyun 	if (result != TP_EC_MUTE_BTN_NONE)
7441*4882a593Smuzhiyun 		pr_warn("Unexpected SAUM result %d\n",
7442*4882a593Smuzhiyun 			result);
7443*4882a593Smuzhiyun 
7444*4882a593Smuzhiyun 	/*
7445*4882a593Smuzhiyun 	 * In software mute mode, the standard codec controls take
7446*4882a593Smuzhiyun 	 * precendence, so we unmute the ThinkPad HW switch at
7447*4882a593Smuzhiyun 	 * startup.  Just on case there are SAUM-capable ThinkPads
7448*4882a593Smuzhiyun 	 * with level controls, set max HW volume as well.
7449*4882a593Smuzhiyun 	 */
7450*4882a593Smuzhiyun 	if (tp_features.mixer_no_level_control)
7451*4882a593Smuzhiyun 		result = volume_set_mute(false);
7452*4882a593Smuzhiyun 	else
7453*4882a593Smuzhiyun 		result = volume_set_status(TP_EC_VOLUME_MAX);
7454*4882a593Smuzhiyun 
7455*4882a593Smuzhiyun 	if (result != 0)
7456*4882a593Smuzhiyun 		pr_warn("Failed to unmute the HW mute switch\n");
7457*4882a593Smuzhiyun 
7458*4882a593Smuzhiyun 	return 0;
7459*4882a593Smuzhiyun }
7460*4882a593Smuzhiyun 
volume_exit_software_mute(void)7461*4882a593Smuzhiyun static void volume_exit_software_mute(void)
7462*4882a593Smuzhiyun {
7463*4882a593Smuzhiyun 	int r;
7464*4882a593Smuzhiyun 
7465*4882a593Smuzhiyun 	if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7466*4882a593Smuzhiyun 	    || r != software_mute_orig_mode)
7467*4882a593Smuzhiyun 		pr_warn("Failed to restore mute mode\n");
7468*4882a593Smuzhiyun }
7469*4882a593Smuzhiyun 
volume_alsa_set_volume(const u8 vol)7470*4882a593Smuzhiyun static int volume_alsa_set_volume(const u8 vol)
7471*4882a593Smuzhiyun {
7472*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_MIXER,
7473*4882a593Smuzhiyun 		   "ALSA: trying to set volume level to %hu\n", vol);
7474*4882a593Smuzhiyun 	return __volume_set_volume_ec(vol);
7475*4882a593Smuzhiyun }
7476*4882a593Smuzhiyun 
volume_alsa_notify_change(void)7477*4882a593Smuzhiyun static void volume_alsa_notify_change(void)
7478*4882a593Smuzhiyun {
7479*4882a593Smuzhiyun 	struct tpacpi_alsa_data *d;
7480*4882a593Smuzhiyun 
7481*4882a593Smuzhiyun 	if (alsa_card && alsa_card->private_data) {
7482*4882a593Smuzhiyun 		d = alsa_card->private_data;
7483*4882a593Smuzhiyun 		if (d->ctl_mute_id)
7484*4882a593Smuzhiyun 			snd_ctl_notify(alsa_card,
7485*4882a593Smuzhiyun 					SNDRV_CTL_EVENT_MASK_VALUE,
7486*4882a593Smuzhiyun 					d->ctl_mute_id);
7487*4882a593Smuzhiyun 		if (d->ctl_vol_id)
7488*4882a593Smuzhiyun 			snd_ctl_notify(alsa_card,
7489*4882a593Smuzhiyun 					SNDRV_CTL_EVENT_MASK_VALUE,
7490*4882a593Smuzhiyun 					d->ctl_vol_id);
7491*4882a593Smuzhiyun 	}
7492*4882a593Smuzhiyun }
7493*4882a593Smuzhiyun 
volume_alsa_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)7494*4882a593Smuzhiyun static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7495*4882a593Smuzhiyun 				struct snd_ctl_elem_info *uinfo)
7496*4882a593Smuzhiyun {
7497*4882a593Smuzhiyun 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7498*4882a593Smuzhiyun 	uinfo->count = 1;
7499*4882a593Smuzhiyun 	uinfo->value.integer.min = 0;
7500*4882a593Smuzhiyun 	uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7501*4882a593Smuzhiyun 	return 0;
7502*4882a593Smuzhiyun }
7503*4882a593Smuzhiyun 
volume_alsa_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7504*4882a593Smuzhiyun static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7505*4882a593Smuzhiyun 				struct snd_ctl_elem_value *ucontrol)
7506*4882a593Smuzhiyun {
7507*4882a593Smuzhiyun 	u8 s;
7508*4882a593Smuzhiyun 	int rc;
7509*4882a593Smuzhiyun 
7510*4882a593Smuzhiyun 	rc = volume_get_status(&s);
7511*4882a593Smuzhiyun 	if (rc < 0)
7512*4882a593Smuzhiyun 		return rc;
7513*4882a593Smuzhiyun 
7514*4882a593Smuzhiyun 	ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7515*4882a593Smuzhiyun 	return 0;
7516*4882a593Smuzhiyun }
7517*4882a593Smuzhiyun 
volume_alsa_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7518*4882a593Smuzhiyun static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7519*4882a593Smuzhiyun 				struct snd_ctl_elem_value *ucontrol)
7520*4882a593Smuzhiyun {
7521*4882a593Smuzhiyun 	tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7522*4882a593Smuzhiyun 				 ucontrol->value.integer.value[0]);
7523*4882a593Smuzhiyun 	return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7524*4882a593Smuzhiyun }
7525*4882a593Smuzhiyun 
7526*4882a593Smuzhiyun #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7527*4882a593Smuzhiyun 
volume_alsa_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7528*4882a593Smuzhiyun static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7529*4882a593Smuzhiyun 				struct snd_ctl_elem_value *ucontrol)
7530*4882a593Smuzhiyun {
7531*4882a593Smuzhiyun 	u8 s;
7532*4882a593Smuzhiyun 	int rc;
7533*4882a593Smuzhiyun 
7534*4882a593Smuzhiyun 	rc = volume_get_status(&s);
7535*4882a593Smuzhiyun 	if (rc < 0)
7536*4882a593Smuzhiyun 		return rc;
7537*4882a593Smuzhiyun 
7538*4882a593Smuzhiyun 	ucontrol->value.integer.value[0] =
7539*4882a593Smuzhiyun 				(s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7540*4882a593Smuzhiyun 	return 0;
7541*4882a593Smuzhiyun }
7542*4882a593Smuzhiyun 
volume_alsa_mute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7543*4882a593Smuzhiyun static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7544*4882a593Smuzhiyun 				struct snd_ctl_elem_value *ucontrol)
7545*4882a593Smuzhiyun {
7546*4882a593Smuzhiyun 	tpacpi_disclose_usertask("ALSA", "%smute\n",
7547*4882a593Smuzhiyun 				 ucontrol->value.integer.value[0] ?
7548*4882a593Smuzhiyun 					"un" : "");
7549*4882a593Smuzhiyun 	return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7550*4882a593Smuzhiyun }
7551*4882a593Smuzhiyun 
7552*4882a593Smuzhiyun static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7553*4882a593Smuzhiyun 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7554*4882a593Smuzhiyun 	.name = "Console Playback Volume",
7555*4882a593Smuzhiyun 	.index = 0,
7556*4882a593Smuzhiyun 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
7557*4882a593Smuzhiyun 	.info = volume_alsa_vol_info,
7558*4882a593Smuzhiyun 	.get = volume_alsa_vol_get,
7559*4882a593Smuzhiyun };
7560*4882a593Smuzhiyun 
7561*4882a593Smuzhiyun static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7562*4882a593Smuzhiyun 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7563*4882a593Smuzhiyun 	.name = "Console Playback Switch",
7564*4882a593Smuzhiyun 	.index = 0,
7565*4882a593Smuzhiyun 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
7566*4882a593Smuzhiyun 	.info = volume_alsa_mute_info,
7567*4882a593Smuzhiyun 	.get = volume_alsa_mute_get,
7568*4882a593Smuzhiyun };
7569*4882a593Smuzhiyun 
volume_suspend(void)7570*4882a593Smuzhiyun static void volume_suspend(void)
7571*4882a593Smuzhiyun {
7572*4882a593Smuzhiyun 	tpacpi_volume_checkpoint_nvram();
7573*4882a593Smuzhiyun }
7574*4882a593Smuzhiyun 
volume_resume(void)7575*4882a593Smuzhiyun static void volume_resume(void)
7576*4882a593Smuzhiyun {
7577*4882a593Smuzhiyun 	if (software_mute_active) {
7578*4882a593Smuzhiyun 		if (volume_set_software_mute(false) < 0)
7579*4882a593Smuzhiyun 			pr_warn("Failed to restore software mute\n");
7580*4882a593Smuzhiyun 	} else {
7581*4882a593Smuzhiyun 		volume_alsa_notify_change();
7582*4882a593Smuzhiyun 	}
7583*4882a593Smuzhiyun }
7584*4882a593Smuzhiyun 
volume_shutdown(void)7585*4882a593Smuzhiyun static void volume_shutdown(void)
7586*4882a593Smuzhiyun {
7587*4882a593Smuzhiyun 	tpacpi_volume_checkpoint_nvram();
7588*4882a593Smuzhiyun }
7589*4882a593Smuzhiyun 
volume_exit(void)7590*4882a593Smuzhiyun static void volume_exit(void)
7591*4882a593Smuzhiyun {
7592*4882a593Smuzhiyun 	if (alsa_card) {
7593*4882a593Smuzhiyun 		snd_card_free(alsa_card);
7594*4882a593Smuzhiyun 		alsa_card = NULL;
7595*4882a593Smuzhiyun 	}
7596*4882a593Smuzhiyun 
7597*4882a593Smuzhiyun 	tpacpi_volume_checkpoint_nvram();
7598*4882a593Smuzhiyun 
7599*4882a593Smuzhiyun 	if (software_mute_active)
7600*4882a593Smuzhiyun 		volume_exit_software_mute();
7601*4882a593Smuzhiyun }
7602*4882a593Smuzhiyun 
volume_create_alsa_mixer(void)7603*4882a593Smuzhiyun static int __init volume_create_alsa_mixer(void)
7604*4882a593Smuzhiyun {
7605*4882a593Smuzhiyun 	struct snd_card *card;
7606*4882a593Smuzhiyun 	struct tpacpi_alsa_data *data;
7607*4882a593Smuzhiyun 	struct snd_kcontrol *ctl_vol;
7608*4882a593Smuzhiyun 	struct snd_kcontrol *ctl_mute;
7609*4882a593Smuzhiyun 	int rc;
7610*4882a593Smuzhiyun 
7611*4882a593Smuzhiyun 	rc = snd_card_new(&tpacpi_pdev->dev,
7612*4882a593Smuzhiyun 			  alsa_index, alsa_id, THIS_MODULE,
7613*4882a593Smuzhiyun 			  sizeof(struct tpacpi_alsa_data), &card);
7614*4882a593Smuzhiyun 	if (rc < 0 || !card) {
7615*4882a593Smuzhiyun 		pr_err("Failed to create ALSA card structures: %d\n", rc);
7616*4882a593Smuzhiyun 		return 1;
7617*4882a593Smuzhiyun 	}
7618*4882a593Smuzhiyun 
7619*4882a593Smuzhiyun 	BUG_ON(!card->private_data);
7620*4882a593Smuzhiyun 	data = card->private_data;
7621*4882a593Smuzhiyun 	data->card = card;
7622*4882a593Smuzhiyun 
7623*4882a593Smuzhiyun 	strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7624*4882a593Smuzhiyun 		sizeof(card->driver));
7625*4882a593Smuzhiyun 	strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7626*4882a593Smuzhiyun 		sizeof(card->shortname));
7627*4882a593Smuzhiyun 	snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7628*4882a593Smuzhiyun 		 (thinkpad_id.ec_version_str) ?
7629*4882a593Smuzhiyun 			thinkpad_id.ec_version_str : "(unknown)");
7630*4882a593Smuzhiyun 	snprintf(card->longname, sizeof(card->longname),
7631*4882a593Smuzhiyun 		 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7632*4882a593Smuzhiyun 		 (thinkpad_id.ec_version_str) ?
7633*4882a593Smuzhiyun 			thinkpad_id.ec_version_str : "unknown");
7634*4882a593Smuzhiyun 
7635*4882a593Smuzhiyun 	if (volume_control_allowed) {
7636*4882a593Smuzhiyun 		volume_alsa_control_vol.put = volume_alsa_vol_put;
7637*4882a593Smuzhiyun 		volume_alsa_control_vol.access =
7638*4882a593Smuzhiyun 				SNDRV_CTL_ELEM_ACCESS_READWRITE;
7639*4882a593Smuzhiyun 
7640*4882a593Smuzhiyun 		volume_alsa_control_mute.put = volume_alsa_mute_put;
7641*4882a593Smuzhiyun 		volume_alsa_control_mute.access =
7642*4882a593Smuzhiyun 				SNDRV_CTL_ELEM_ACCESS_READWRITE;
7643*4882a593Smuzhiyun 	}
7644*4882a593Smuzhiyun 
7645*4882a593Smuzhiyun 	if (!tp_features.mixer_no_level_control) {
7646*4882a593Smuzhiyun 		ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7647*4882a593Smuzhiyun 		rc = snd_ctl_add(card, ctl_vol);
7648*4882a593Smuzhiyun 		if (rc < 0) {
7649*4882a593Smuzhiyun 			pr_err("Failed to create ALSA volume control: %d\n",
7650*4882a593Smuzhiyun 			       rc);
7651*4882a593Smuzhiyun 			goto err_exit;
7652*4882a593Smuzhiyun 		}
7653*4882a593Smuzhiyun 		data->ctl_vol_id = &ctl_vol->id;
7654*4882a593Smuzhiyun 	}
7655*4882a593Smuzhiyun 
7656*4882a593Smuzhiyun 	ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7657*4882a593Smuzhiyun 	rc = snd_ctl_add(card, ctl_mute);
7658*4882a593Smuzhiyun 	if (rc < 0) {
7659*4882a593Smuzhiyun 		pr_err("Failed to create ALSA mute control: %d\n", rc);
7660*4882a593Smuzhiyun 		goto err_exit;
7661*4882a593Smuzhiyun 	}
7662*4882a593Smuzhiyun 	data->ctl_mute_id = &ctl_mute->id;
7663*4882a593Smuzhiyun 
7664*4882a593Smuzhiyun 	rc = snd_card_register(card);
7665*4882a593Smuzhiyun 	if (rc < 0) {
7666*4882a593Smuzhiyun 		pr_err("Failed to register ALSA card: %d\n", rc);
7667*4882a593Smuzhiyun 		goto err_exit;
7668*4882a593Smuzhiyun 	}
7669*4882a593Smuzhiyun 
7670*4882a593Smuzhiyun 	alsa_card = card;
7671*4882a593Smuzhiyun 	return 0;
7672*4882a593Smuzhiyun 
7673*4882a593Smuzhiyun err_exit:
7674*4882a593Smuzhiyun 	snd_card_free(card);
7675*4882a593Smuzhiyun 	return 1;
7676*4882a593Smuzhiyun }
7677*4882a593Smuzhiyun 
7678*4882a593Smuzhiyun #define TPACPI_VOL_Q_MUTEONLY	0x0001	/* Mute-only control available */
7679*4882a593Smuzhiyun #define TPACPI_VOL_Q_LEVEL	0x0002  /* Volume control available */
7680*4882a593Smuzhiyun 
7681*4882a593Smuzhiyun static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7682*4882a593Smuzhiyun 	/* Whitelist volume level on all IBM by default */
7683*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_IBM,
7684*4882a593Smuzhiyun 	  .bios   = TPACPI_MATCH_ANY,
7685*4882a593Smuzhiyun 	  .ec     = TPACPI_MATCH_ANY,
7686*4882a593Smuzhiyun 	  .quirks = TPACPI_VOL_Q_LEVEL },
7687*4882a593Smuzhiyun 
7688*4882a593Smuzhiyun 	/* Lenovo models with volume control (needs confirmation) */
7689*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7690*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7691*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7692*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7693*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7694*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7695*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7696*4882a593Smuzhiyun 
7697*4882a593Smuzhiyun 	/* Whitelist mute-only on all Lenovo by default */
7698*4882a593Smuzhiyun 	{ .vendor = PCI_VENDOR_ID_LENOVO,
7699*4882a593Smuzhiyun 	  .bios   = TPACPI_MATCH_ANY,
7700*4882a593Smuzhiyun 	  .ec	  = TPACPI_MATCH_ANY,
7701*4882a593Smuzhiyun 	  .quirks = TPACPI_VOL_Q_MUTEONLY }
7702*4882a593Smuzhiyun };
7703*4882a593Smuzhiyun 
volume_init(struct ibm_init_struct * iibm)7704*4882a593Smuzhiyun static int __init volume_init(struct ibm_init_struct *iibm)
7705*4882a593Smuzhiyun {
7706*4882a593Smuzhiyun 	unsigned long quirks;
7707*4882a593Smuzhiyun 	int rc;
7708*4882a593Smuzhiyun 
7709*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7710*4882a593Smuzhiyun 
7711*4882a593Smuzhiyun 	mutex_init(&volume_mutex);
7712*4882a593Smuzhiyun 
7713*4882a593Smuzhiyun 	/*
7714*4882a593Smuzhiyun 	 * Check for module parameter bogosity, note that we
7715*4882a593Smuzhiyun 	 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7716*4882a593Smuzhiyun 	 * able to detect "unspecified"
7717*4882a593Smuzhiyun 	 */
7718*4882a593Smuzhiyun 	if (volume_mode > TPACPI_VOL_MODE_MAX)
7719*4882a593Smuzhiyun 		return -EINVAL;
7720*4882a593Smuzhiyun 
7721*4882a593Smuzhiyun 	if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7722*4882a593Smuzhiyun 		pr_err("UCMS step volume mode not implemented, please contact %s\n",
7723*4882a593Smuzhiyun 		       TPACPI_MAIL);
7724*4882a593Smuzhiyun 		return 1;
7725*4882a593Smuzhiyun 	}
7726*4882a593Smuzhiyun 
7727*4882a593Smuzhiyun 	if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7728*4882a593Smuzhiyun 		return -EINVAL;
7729*4882a593Smuzhiyun 
7730*4882a593Smuzhiyun 	/*
7731*4882a593Smuzhiyun 	 * The ALSA mixer is our primary interface.
7732*4882a593Smuzhiyun 	 * When disabled, don't install the subdriver at all
7733*4882a593Smuzhiyun 	 */
7734*4882a593Smuzhiyun 	if (!alsa_enable) {
7735*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7736*4882a593Smuzhiyun 			    "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7737*4882a593Smuzhiyun 		return 1;
7738*4882a593Smuzhiyun 	}
7739*4882a593Smuzhiyun 
7740*4882a593Smuzhiyun 	quirks = tpacpi_check_quirks(volume_quirk_table,
7741*4882a593Smuzhiyun 				     ARRAY_SIZE(volume_quirk_table));
7742*4882a593Smuzhiyun 
7743*4882a593Smuzhiyun 	switch (volume_capabilities) {
7744*4882a593Smuzhiyun 	case TPACPI_VOL_CAP_AUTO:
7745*4882a593Smuzhiyun 		if (quirks & TPACPI_VOL_Q_MUTEONLY)
7746*4882a593Smuzhiyun 			tp_features.mixer_no_level_control = 1;
7747*4882a593Smuzhiyun 		else if (quirks & TPACPI_VOL_Q_LEVEL)
7748*4882a593Smuzhiyun 			tp_features.mixer_no_level_control = 0;
7749*4882a593Smuzhiyun 		else
7750*4882a593Smuzhiyun 			return 1; /* no mixer */
7751*4882a593Smuzhiyun 		break;
7752*4882a593Smuzhiyun 	case TPACPI_VOL_CAP_VOLMUTE:
7753*4882a593Smuzhiyun 		tp_features.mixer_no_level_control = 0;
7754*4882a593Smuzhiyun 		break;
7755*4882a593Smuzhiyun 	case TPACPI_VOL_CAP_MUTEONLY:
7756*4882a593Smuzhiyun 		tp_features.mixer_no_level_control = 1;
7757*4882a593Smuzhiyun 		break;
7758*4882a593Smuzhiyun 	default:
7759*4882a593Smuzhiyun 		return 1;
7760*4882a593Smuzhiyun 	}
7761*4882a593Smuzhiyun 
7762*4882a593Smuzhiyun 	if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7763*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7764*4882a593Smuzhiyun 				"using user-supplied volume_capabilities=%d\n",
7765*4882a593Smuzhiyun 				volume_capabilities);
7766*4882a593Smuzhiyun 
7767*4882a593Smuzhiyun 	if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7768*4882a593Smuzhiyun 	    volume_mode == TPACPI_VOL_MODE_MAX) {
7769*4882a593Smuzhiyun 		volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7770*4882a593Smuzhiyun 
7771*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7772*4882a593Smuzhiyun 				"driver auto-selected volume_mode=%d\n",
7773*4882a593Smuzhiyun 				volume_mode);
7774*4882a593Smuzhiyun 	} else {
7775*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7776*4882a593Smuzhiyun 				"using user-supplied volume_mode=%d\n",
7777*4882a593Smuzhiyun 				volume_mode);
7778*4882a593Smuzhiyun 	}
7779*4882a593Smuzhiyun 
7780*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7781*4882a593Smuzhiyun 			"mute is supported, volume control is %s\n",
7782*4882a593Smuzhiyun 			str_supported(!tp_features.mixer_no_level_control));
7783*4882a593Smuzhiyun 
7784*4882a593Smuzhiyun 	if (software_mute_requested && volume_set_software_mute(true) == 0) {
7785*4882a593Smuzhiyun 		software_mute_active = true;
7786*4882a593Smuzhiyun 	} else {
7787*4882a593Smuzhiyun 		rc = volume_create_alsa_mixer();
7788*4882a593Smuzhiyun 		if (rc) {
7789*4882a593Smuzhiyun 			pr_err("Could not create the ALSA mixer interface\n");
7790*4882a593Smuzhiyun 			return rc;
7791*4882a593Smuzhiyun 		}
7792*4882a593Smuzhiyun 
7793*4882a593Smuzhiyun 		pr_info("Console audio control enabled, mode: %s\n",
7794*4882a593Smuzhiyun 			(volume_control_allowed) ?
7795*4882a593Smuzhiyun 				"override (read/write)" :
7796*4882a593Smuzhiyun 				"monitor (read only)");
7797*4882a593Smuzhiyun 	}
7798*4882a593Smuzhiyun 
7799*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7800*4882a593Smuzhiyun 		"registering volume hotkeys as change notification\n");
7801*4882a593Smuzhiyun 	tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7802*4882a593Smuzhiyun 			| TP_ACPI_HKEY_VOLUP_MASK
7803*4882a593Smuzhiyun 			| TP_ACPI_HKEY_VOLDWN_MASK
7804*4882a593Smuzhiyun 			| TP_ACPI_HKEY_MUTE_MASK);
7805*4882a593Smuzhiyun 
7806*4882a593Smuzhiyun 	return 0;
7807*4882a593Smuzhiyun }
7808*4882a593Smuzhiyun 
volume_read(struct seq_file * m)7809*4882a593Smuzhiyun static int volume_read(struct seq_file *m)
7810*4882a593Smuzhiyun {
7811*4882a593Smuzhiyun 	u8 status;
7812*4882a593Smuzhiyun 
7813*4882a593Smuzhiyun 	if (volume_get_status(&status) < 0) {
7814*4882a593Smuzhiyun 		seq_printf(m, "level:\t\tunreadable\n");
7815*4882a593Smuzhiyun 	} else {
7816*4882a593Smuzhiyun 		if (tp_features.mixer_no_level_control)
7817*4882a593Smuzhiyun 			seq_printf(m, "level:\t\tunsupported\n");
7818*4882a593Smuzhiyun 		else
7819*4882a593Smuzhiyun 			seq_printf(m, "level:\t\t%d\n",
7820*4882a593Smuzhiyun 					status & TP_EC_AUDIO_LVL_MSK);
7821*4882a593Smuzhiyun 
7822*4882a593Smuzhiyun 		seq_printf(m, "mute:\t\t%s\n",
7823*4882a593Smuzhiyun 				onoff(status, TP_EC_AUDIO_MUTESW));
7824*4882a593Smuzhiyun 
7825*4882a593Smuzhiyun 		if (volume_control_allowed) {
7826*4882a593Smuzhiyun 			seq_printf(m, "commands:\tunmute, mute\n");
7827*4882a593Smuzhiyun 			if (!tp_features.mixer_no_level_control) {
7828*4882a593Smuzhiyun 				seq_printf(m, "commands:\tup, down\n");
7829*4882a593Smuzhiyun 				seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7830*4882a593Smuzhiyun 					      TP_EC_VOLUME_MAX);
7831*4882a593Smuzhiyun 			}
7832*4882a593Smuzhiyun 		}
7833*4882a593Smuzhiyun 	}
7834*4882a593Smuzhiyun 
7835*4882a593Smuzhiyun 	return 0;
7836*4882a593Smuzhiyun }
7837*4882a593Smuzhiyun 
volume_write(char * buf)7838*4882a593Smuzhiyun static int volume_write(char *buf)
7839*4882a593Smuzhiyun {
7840*4882a593Smuzhiyun 	u8 s;
7841*4882a593Smuzhiyun 	u8 new_level, new_mute;
7842*4882a593Smuzhiyun 	int l;
7843*4882a593Smuzhiyun 	char *cmd;
7844*4882a593Smuzhiyun 	int rc;
7845*4882a593Smuzhiyun 
7846*4882a593Smuzhiyun 	/*
7847*4882a593Smuzhiyun 	 * We do allow volume control at driver startup, so that the
7848*4882a593Smuzhiyun 	 * user can set initial state through the volume=... parameter hack.
7849*4882a593Smuzhiyun 	 */
7850*4882a593Smuzhiyun 	if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7851*4882a593Smuzhiyun 		if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7852*4882a593Smuzhiyun 			tp_warned.volume_ctrl_forbidden = 1;
7853*4882a593Smuzhiyun 			pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7854*4882a593Smuzhiyun 			pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7855*4882a593Smuzhiyun 		}
7856*4882a593Smuzhiyun 		return -EPERM;
7857*4882a593Smuzhiyun 	}
7858*4882a593Smuzhiyun 
7859*4882a593Smuzhiyun 	rc = volume_get_status(&s);
7860*4882a593Smuzhiyun 	if (rc < 0)
7861*4882a593Smuzhiyun 		return rc;
7862*4882a593Smuzhiyun 
7863*4882a593Smuzhiyun 	new_level = s & TP_EC_AUDIO_LVL_MSK;
7864*4882a593Smuzhiyun 	new_mute  = s & TP_EC_AUDIO_MUTESW_MSK;
7865*4882a593Smuzhiyun 
7866*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
7867*4882a593Smuzhiyun 		if (!tp_features.mixer_no_level_control) {
7868*4882a593Smuzhiyun 			if (strlencmp(cmd, "up") == 0) {
7869*4882a593Smuzhiyun 				if (new_mute)
7870*4882a593Smuzhiyun 					new_mute = 0;
7871*4882a593Smuzhiyun 				else if (new_level < TP_EC_VOLUME_MAX)
7872*4882a593Smuzhiyun 					new_level++;
7873*4882a593Smuzhiyun 				continue;
7874*4882a593Smuzhiyun 			} else if (strlencmp(cmd, "down") == 0) {
7875*4882a593Smuzhiyun 				if (new_mute)
7876*4882a593Smuzhiyun 					new_mute = 0;
7877*4882a593Smuzhiyun 				else if (new_level > 0)
7878*4882a593Smuzhiyun 					new_level--;
7879*4882a593Smuzhiyun 				continue;
7880*4882a593Smuzhiyun 			} else if (sscanf(cmd, "level %u", &l) == 1 &&
7881*4882a593Smuzhiyun 				   l >= 0 && l <= TP_EC_VOLUME_MAX) {
7882*4882a593Smuzhiyun 					new_level = l;
7883*4882a593Smuzhiyun 				continue;
7884*4882a593Smuzhiyun 			}
7885*4882a593Smuzhiyun 		}
7886*4882a593Smuzhiyun 		if (strlencmp(cmd, "mute") == 0)
7887*4882a593Smuzhiyun 			new_mute = TP_EC_AUDIO_MUTESW_MSK;
7888*4882a593Smuzhiyun 		else if (strlencmp(cmd, "unmute") == 0)
7889*4882a593Smuzhiyun 			new_mute = 0;
7890*4882a593Smuzhiyun 		else
7891*4882a593Smuzhiyun 			return -EINVAL;
7892*4882a593Smuzhiyun 	}
7893*4882a593Smuzhiyun 
7894*4882a593Smuzhiyun 	if (tp_features.mixer_no_level_control) {
7895*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs volume", "%smute\n",
7896*4882a593Smuzhiyun 					new_mute ? "" : "un");
7897*4882a593Smuzhiyun 		rc = volume_set_mute(!!new_mute);
7898*4882a593Smuzhiyun 	} else {
7899*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs volume",
7900*4882a593Smuzhiyun 					"%smute and set level to %d\n",
7901*4882a593Smuzhiyun 					new_mute ? "" : "un", new_level);
7902*4882a593Smuzhiyun 		rc = volume_set_status(new_mute | new_level);
7903*4882a593Smuzhiyun 	}
7904*4882a593Smuzhiyun 	volume_alsa_notify_change();
7905*4882a593Smuzhiyun 
7906*4882a593Smuzhiyun 	return (rc == -EINTR) ? -ERESTARTSYS : rc;
7907*4882a593Smuzhiyun }
7908*4882a593Smuzhiyun 
7909*4882a593Smuzhiyun static struct ibm_struct volume_driver_data = {
7910*4882a593Smuzhiyun 	.name = "volume",
7911*4882a593Smuzhiyun 	.read = volume_read,
7912*4882a593Smuzhiyun 	.write = volume_write,
7913*4882a593Smuzhiyun 	.exit = volume_exit,
7914*4882a593Smuzhiyun 	.suspend = volume_suspend,
7915*4882a593Smuzhiyun 	.resume = volume_resume,
7916*4882a593Smuzhiyun 	.shutdown = volume_shutdown,
7917*4882a593Smuzhiyun };
7918*4882a593Smuzhiyun 
7919*4882a593Smuzhiyun #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7920*4882a593Smuzhiyun 
7921*4882a593Smuzhiyun #define alsa_card NULL
7922*4882a593Smuzhiyun 
volume_alsa_notify_change(void)7923*4882a593Smuzhiyun static inline void volume_alsa_notify_change(void)
7924*4882a593Smuzhiyun {
7925*4882a593Smuzhiyun }
7926*4882a593Smuzhiyun 
volume_init(struct ibm_init_struct * iibm)7927*4882a593Smuzhiyun static int __init volume_init(struct ibm_init_struct *iibm)
7928*4882a593Smuzhiyun {
7929*4882a593Smuzhiyun 	pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7930*4882a593Smuzhiyun 
7931*4882a593Smuzhiyun 	return 1;
7932*4882a593Smuzhiyun }
7933*4882a593Smuzhiyun 
7934*4882a593Smuzhiyun static struct ibm_struct volume_driver_data = {
7935*4882a593Smuzhiyun 	.name = "volume",
7936*4882a593Smuzhiyun };
7937*4882a593Smuzhiyun 
7938*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7939*4882a593Smuzhiyun 
7940*4882a593Smuzhiyun /*************************************************************************
7941*4882a593Smuzhiyun  * Fan subdriver
7942*4882a593Smuzhiyun  */
7943*4882a593Smuzhiyun 
7944*4882a593Smuzhiyun /*
7945*4882a593Smuzhiyun  * FAN ACCESS MODES
7946*4882a593Smuzhiyun  *
7947*4882a593Smuzhiyun  * TPACPI_FAN_RD_ACPI_GFAN:
7948*4882a593Smuzhiyun  * 	ACPI GFAN method: returns fan level
7949*4882a593Smuzhiyun  *
7950*4882a593Smuzhiyun  * 	see TPACPI_FAN_WR_ACPI_SFAN
7951*4882a593Smuzhiyun  * 	EC 0x2f (HFSP) not available if GFAN exists
7952*4882a593Smuzhiyun  *
7953*4882a593Smuzhiyun  * TPACPI_FAN_WR_ACPI_SFAN:
7954*4882a593Smuzhiyun  * 	ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7955*4882a593Smuzhiyun  *
7956*4882a593Smuzhiyun  * 	EC 0x2f (HFSP) might be available *for reading*, but do not use
7957*4882a593Smuzhiyun  * 	it for writing.
7958*4882a593Smuzhiyun  *
7959*4882a593Smuzhiyun  * TPACPI_FAN_WR_TPEC:
7960*4882a593Smuzhiyun  * 	ThinkPad EC register 0x2f (HFSP): fan control loop mode
7961*4882a593Smuzhiyun  * 	Supported on almost all ThinkPads
7962*4882a593Smuzhiyun  *
7963*4882a593Smuzhiyun  * 	Fan speed changes of any sort (including those caused by the
7964*4882a593Smuzhiyun  * 	disengaged mode) are usually done slowly by the firmware as the
7965*4882a593Smuzhiyun  * 	maximum amount of fan duty cycle change per second seems to be
7966*4882a593Smuzhiyun  * 	limited.
7967*4882a593Smuzhiyun  *
7968*4882a593Smuzhiyun  * 	Reading is not available if GFAN exists.
7969*4882a593Smuzhiyun  * 	Writing is not available if SFAN exists.
7970*4882a593Smuzhiyun  *
7971*4882a593Smuzhiyun  * 	Bits
7972*4882a593Smuzhiyun  *	 7	automatic mode engaged;
7973*4882a593Smuzhiyun  *  		(default operation mode of the ThinkPad)
7974*4882a593Smuzhiyun  * 		fan level is ignored in this mode.
7975*4882a593Smuzhiyun  *	 6	full speed mode (takes precedence over bit 7);
7976*4882a593Smuzhiyun  *		not available on all thinkpads.  May disable
7977*4882a593Smuzhiyun  *		the tachometer while the fan controller ramps up
7978*4882a593Smuzhiyun  *		the speed (which can take up to a few *minutes*).
7979*4882a593Smuzhiyun  *		Speeds up fan to 100% duty-cycle, which is far above
7980*4882a593Smuzhiyun  *		the standard RPM levels.  It is not impossible that
7981*4882a593Smuzhiyun  *		it could cause hardware damage.
7982*4882a593Smuzhiyun  *	5-3	unused in some models.  Extra bits for fan level
7983*4882a593Smuzhiyun  *		in others, but still useless as all values above
7984*4882a593Smuzhiyun  *		7 map to the same speed as level 7 in these models.
7985*4882a593Smuzhiyun  *	2-0	fan level (0..7 usually)
7986*4882a593Smuzhiyun  *			0x00 = stop
7987*4882a593Smuzhiyun  * 			0x07 = max (set when temperatures critical)
7988*4882a593Smuzhiyun  * 		Some ThinkPads may have other levels, see
7989*4882a593Smuzhiyun  * 		TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7990*4882a593Smuzhiyun  *
7991*4882a593Smuzhiyun  *	FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7992*4882a593Smuzhiyun  *	boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7993*4882a593Smuzhiyun  *	does so, its initial value is meaningless (0x07).
7994*4882a593Smuzhiyun  *
7995*4882a593Smuzhiyun  *	For firmware bugs, refer to:
7996*4882a593Smuzhiyun  *	https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7997*4882a593Smuzhiyun  *
7998*4882a593Smuzhiyun  * 	----
7999*4882a593Smuzhiyun  *
8000*4882a593Smuzhiyun  *	ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8001*4882a593Smuzhiyun  *	Main fan tachometer reading (in RPM)
8002*4882a593Smuzhiyun  *
8003*4882a593Smuzhiyun  *	This register is present on all ThinkPads with a new-style EC, and
8004*4882a593Smuzhiyun  *	it is known not to be present on the A21m/e, and T22, as there is
8005*4882a593Smuzhiyun  *	something else in offset 0x84 according to the ACPI DSDT.  Other
8006*4882a593Smuzhiyun  *	ThinkPads from this same time period (and earlier) probably lack the
8007*4882a593Smuzhiyun  *	tachometer as well.
8008*4882a593Smuzhiyun  *
8009*4882a593Smuzhiyun  *	Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8010*4882a593Smuzhiyun  *	was never fixed by IBM to report the EC firmware version string
8011*4882a593Smuzhiyun  *	probably support the tachometer (like the early X models), so
8012*4882a593Smuzhiyun  *	detecting it is quite hard.  We need more data to know for sure.
8013*4882a593Smuzhiyun  *
8014*4882a593Smuzhiyun  *	FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8015*4882a593Smuzhiyun  *	might result.
8016*4882a593Smuzhiyun  *
8017*4882a593Smuzhiyun  *	FIRMWARE BUG: may go stale while the EC is switching to full speed
8018*4882a593Smuzhiyun  *	mode.
8019*4882a593Smuzhiyun  *
8020*4882a593Smuzhiyun  *	For firmware bugs, refer to:
8021*4882a593Smuzhiyun  *	https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8022*4882a593Smuzhiyun  *
8023*4882a593Smuzhiyun  *	----
8024*4882a593Smuzhiyun  *
8025*4882a593Smuzhiyun  *	ThinkPad EC register 0x31 bit 0 (only on select models)
8026*4882a593Smuzhiyun  *
8027*4882a593Smuzhiyun  *	When bit 0 of EC register 0x31 is zero, the tachometer registers
8028*4882a593Smuzhiyun  *	show the speed of the main fan.  When bit 0 of EC register 0x31
8029*4882a593Smuzhiyun  *	is one, the tachometer registers show the speed of the auxiliary
8030*4882a593Smuzhiyun  *	fan.
8031*4882a593Smuzhiyun  *
8032*4882a593Smuzhiyun  *	Fan control seems to affect both fans, regardless of the state
8033*4882a593Smuzhiyun  *	of this bit.
8034*4882a593Smuzhiyun  *
8035*4882a593Smuzhiyun  *	So far, only the firmware for the X60/X61 non-tablet versions
8036*4882a593Smuzhiyun  *	seem to support this (firmware TP-7M).
8037*4882a593Smuzhiyun  *
8038*4882a593Smuzhiyun  * TPACPI_FAN_WR_ACPI_FANS:
8039*4882a593Smuzhiyun  *	ThinkPad X31, X40, X41.  Not available in the X60.
8040*4882a593Smuzhiyun  *
8041*4882a593Smuzhiyun  *	FANS ACPI handle: takes three arguments: low speed, medium speed,
8042*4882a593Smuzhiyun  *	high speed.  ACPI DSDT seems to map these three speeds to levels
8043*4882a593Smuzhiyun  *	as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8044*4882a593Smuzhiyun  *	(this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8045*4882a593Smuzhiyun  *
8046*4882a593Smuzhiyun  * 	The speeds are stored on handles
8047*4882a593Smuzhiyun  * 	(FANA:FAN9), (FANC:FANB), (FANE:FAND).
8048*4882a593Smuzhiyun  *
8049*4882a593Smuzhiyun  * 	There are three default speed sets, accessible as handles:
8050*4882a593Smuzhiyun  * 	FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8051*4882a593Smuzhiyun  *
8052*4882a593Smuzhiyun  * 	ACPI DSDT switches which set is in use depending on various
8053*4882a593Smuzhiyun  * 	factors.
8054*4882a593Smuzhiyun  *
8055*4882a593Smuzhiyun  * 	TPACPI_FAN_WR_TPEC is also available and should be used to
8056*4882a593Smuzhiyun  * 	command the fan.  The X31/X40/X41 seems to have 8 fan levels,
8057*4882a593Smuzhiyun  * 	but the ACPI tables just mention level 7.
8058*4882a593Smuzhiyun  */
8059*4882a593Smuzhiyun 
8060*4882a593Smuzhiyun enum {					/* Fan control constants */
8061*4882a593Smuzhiyun 	fan_status_offset = 0x2f,	/* EC register 0x2f */
8062*4882a593Smuzhiyun 	fan_rpm_offset = 0x84,		/* EC register 0x84: LSB, 0x85 MSB (RPM)
8063*4882a593Smuzhiyun 					 * 0x84 must be read before 0x85 */
8064*4882a593Smuzhiyun 	fan_select_offset = 0x31,	/* EC register 0x31 (Firmware 7M)
8065*4882a593Smuzhiyun 					   bit 0 selects which fan is active */
8066*4882a593Smuzhiyun 
8067*4882a593Smuzhiyun 	TP_EC_FAN_FULLSPEED = 0x40,	/* EC fan mode: full speed */
8068*4882a593Smuzhiyun 	TP_EC_FAN_AUTO	    = 0x80,	/* EC fan mode: auto fan control */
8069*4882a593Smuzhiyun 
8070*4882a593Smuzhiyun 	TPACPI_FAN_LAST_LEVEL = 0x100,	/* Use cached last-seen fan level */
8071*4882a593Smuzhiyun };
8072*4882a593Smuzhiyun 
8073*4882a593Smuzhiyun enum fan_status_access_mode {
8074*4882a593Smuzhiyun 	TPACPI_FAN_NONE = 0,		/* No fan status or control */
8075*4882a593Smuzhiyun 	TPACPI_FAN_RD_ACPI_GFAN,	/* Use ACPI GFAN */
8076*4882a593Smuzhiyun 	TPACPI_FAN_RD_TPEC,		/* Use ACPI EC regs 0x2f, 0x84-0x85 */
8077*4882a593Smuzhiyun };
8078*4882a593Smuzhiyun 
8079*4882a593Smuzhiyun enum fan_control_access_mode {
8080*4882a593Smuzhiyun 	TPACPI_FAN_WR_NONE = 0,		/* No fan control */
8081*4882a593Smuzhiyun 	TPACPI_FAN_WR_ACPI_SFAN,	/* Use ACPI SFAN */
8082*4882a593Smuzhiyun 	TPACPI_FAN_WR_TPEC,		/* Use ACPI EC reg 0x2f */
8083*4882a593Smuzhiyun 	TPACPI_FAN_WR_ACPI_FANS,	/* Use ACPI FANS and EC reg 0x2f */
8084*4882a593Smuzhiyun };
8085*4882a593Smuzhiyun 
8086*4882a593Smuzhiyun enum fan_control_commands {
8087*4882a593Smuzhiyun 	TPACPI_FAN_CMD_SPEED 	= 0x0001,	/* speed command */
8088*4882a593Smuzhiyun 	TPACPI_FAN_CMD_LEVEL 	= 0x0002,	/* level command  */
8089*4882a593Smuzhiyun 	TPACPI_FAN_CMD_ENABLE	= 0x0004,	/* enable/disable cmd,
8090*4882a593Smuzhiyun 						 * and also watchdog cmd */
8091*4882a593Smuzhiyun };
8092*4882a593Smuzhiyun 
8093*4882a593Smuzhiyun static bool fan_control_allowed;
8094*4882a593Smuzhiyun 
8095*4882a593Smuzhiyun static enum fan_status_access_mode fan_status_access_mode;
8096*4882a593Smuzhiyun static enum fan_control_access_mode fan_control_access_mode;
8097*4882a593Smuzhiyun static enum fan_control_commands fan_control_commands;
8098*4882a593Smuzhiyun 
8099*4882a593Smuzhiyun static u8 fan_control_initial_status;
8100*4882a593Smuzhiyun static u8 fan_control_desired_level;
8101*4882a593Smuzhiyun static u8 fan_control_resume_level;
8102*4882a593Smuzhiyun static int fan_watchdog_maxinterval;
8103*4882a593Smuzhiyun 
8104*4882a593Smuzhiyun static struct mutex fan_mutex;
8105*4882a593Smuzhiyun 
8106*4882a593Smuzhiyun static void fan_watchdog_fire(struct work_struct *ignored);
8107*4882a593Smuzhiyun static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8108*4882a593Smuzhiyun 
8109*4882a593Smuzhiyun TPACPI_HANDLE(fans, ec, "FANS");	/* X31, X40, X41 */
8110*4882a593Smuzhiyun TPACPI_HANDLE(gfan, ec, "GFAN",	/* 570 */
8111*4882a593Smuzhiyun 	   "\\FSPD",		/* 600e/x, 770e, 770x */
8112*4882a593Smuzhiyun 	   );			/* all others */
8113*4882a593Smuzhiyun TPACPI_HANDLE(sfan, ec, "SFAN",	/* 570 */
8114*4882a593Smuzhiyun 	   "JFNS",		/* 770x-JL */
8115*4882a593Smuzhiyun 	   );			/* all others */
8116*4882a593Smuzhiyun 
8117*4882a593Smuzhiyun /*
8118*4882a593Smuzhiyun  * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8119*4882a593Smuzhiyun  * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8120*4882a593Smuzhiyun  * be in auto mode (0x80).
8121*4882a593Smuzhiyun  *
8122*4882a593Smuzhiyun  * This is corrected by any write to HFSP either by the driver, or
8123*4882a593Smuzhiyun  * by the firmware.
8124*4882a593Smuzhiyun  *
8125*4882a593Smuzhiyun  * We assume 0x07 really means auto mode while this quirk is active,
8126*4882a593Smuzhiyun  * as this is far more likely than the ThinkPad being in level 7,
8127*4882a593Smuzhiyun  * which is only used by the firmware during thermal emergencies.
8128*4882a593Smuzhiyun  *
8129*4882a593Smuzhiyun  * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8130*4882a593Smuzhiyun  * TP-70 (T43, R52), which are known to be buggy.
8131*4882a593Smuzhiyun  */
8132*4882a593Smuzhiyun 
fan_quirk1_setup(void)8133*4882a593Smuzhiyun static void fan_quirk1_setup(void)
8134*4882a593Smuzhiyun {
8135*4882a593Smuzhiyun 	if (fan_control_initial_status == 0x07) {
8136*4882a593Smuzhiyun 		pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8137*4882a593Smuzhiyun 		tp_features.fan_ctrl_status_undef = 1;
8138*4882a593Smuzhiyun 	}
8139*4882a593Smuzhiyun }
8140*4882a593Smuzhiyun 
fan_quirk1_handle(u8 * fan_status)8141*4882a593Smuzhiyun static void fan_quirk1_handle(u8 *fan_status)
8142*4882a593Smuzhiyun {
8143*4882a593Smuzhiyun 	if (unlikely(tp_features.fan_ctrl_status_undef)) {
8144*4882a593Smuzhiyun 		if (*fan_status != fan_control_initial_status) {
8145*4882a593Smuzhiyun 			/* something changed the HFSP regisnter since
8146*4882a593Smuzhiyun 			 * driver init time, so it is not undefined
8147*4882a593Smuzhiyun 			 * anymore */
8148*4882a593Smuzhiyun 			tp_features.fan_ctrl_status_undef = 0;
8149*4882a593Smuzhiyun 		} else {
8150*4882a593Smuzhiyun 			/* Return most likely status. In fact, it
8151*4882a593Smuzhiyun 			 * might be the only possible status */
8152*4882a593Smuzhiyun 			*fan_status = TP_EC_FAN_AUTO;
8153*4882a593Smuzhiyun 		}
8154*4882a593Smuzhiyun 	}
8155*4882a593Smuzhiyun }
8156*4882a593Smuzhiyun 
8157*4882a593Smuzhiyun /* Select main fan on X60/X61, NOOP on others */
fan_select_fan1(void)8158*4882a593Smuzhiyun static bool fan_select_fan1(void)
8159*4882a593Smuzhiyun {
8160*4882a593Smuzhiyun 	if (tp_features.second_fan) {
8161*4882a593Smuzhiyun 		u8 val;
8162*4882a593Smuzhiyun 
8163*4882a593Smuzhiyun 		if (ec_read(fan_select_offset, &val) < 0)
8164*4882a593Smuzhiyun 			return false;
8165*4882a593Smuzhiyun 		val &= 0xFEU;
8166*4882a593Smuzhiyun 		if (ec_write(fan_select_offset, val) < 0)
8167*4882a593Smuzhiyun 			return false;
8168*4882a593Smuzhiyun 	}
8169*4882a593Smuzhiyun 	return true;
8170*4882a593Smuzhiyun }
8171*4882a593Smuzhiyun 
8172*4882a593Smuzhiyun /* Select secondary fan on X60/X61 */
fan_select_fan2(void)8173*4882a593Smuzhiyun static bool fan_select_fan2(void)
8174*4882a593Smuzhiyun {
8175*4882a593Smuzhiyun 	u8 val;
8176*4882a593Smuzhiyun 
8177*4882a593Smuzhiyun 	if (!tp_features.second_fan)
8178*4882a593Smuzhiyun 		return false;
8179*4882a593Smuzhiyun 
8180*4882a593Smuzhiyun 	if (ec_read(fan_select_offset, &val) < 0)
8181*4882a593Smuzhiyun 		return false;
8182*4882a593Smuzhiyun 	val |= 0x01U;
8183*4882a593Smuzhiyun 	if (ec_write(fan_select_offset, val) < 0)
8184*4882a593Smuzhiyun 		return false;
8185*4882a593Smuzhiyun 
8186*4882a593Smuzhiyun 	return true;
8187*4882a593Smuzhiyun }
8188*4882a593Smuzhiyun 
8189*4882a593Smuzhiyun /*
8190*4882a593Smuzhiyun  * Call with fan_mutex held
8191*4882a593Smuzhiyun  */
fan_update_desired_level(u8 status)8192*4882a593Smuzhiyun static void fan_update_desired_level(u8 status)
8193*4882a593Smuzhiyun {
8194*4882a593Smuzhiyun 	if ((status &
8195*4882a593Smuzhiyun 	     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8196*4882a593Smuzhiyun 		if (status > 7)
8197*4882a593Smuzhiyun 			fan_control_desired_level = 7;
8198*4882a593Smuzhiyun 		else
8199*4882a593Smuzhiyun 			fan_control_desired_level = status;
8200*4882a593Smuzhiyun 	}
8201*4882a593Smuzhiyun }
8202*4882a593Smuzhiyun 
fan_get_status(u8 * status)8203*4882a593Smuzhiyun static int fan_get_status(u8 *status)
8204*4882a593Smuzhiyun {
8205*4882a593Smuzhiyun 	u8 s;
8206*4882a593Smuzhiyun 
8207*4882a593Smuzhiyun 	/* TODO:
8208*4882a593Smuzhiyun 	 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8209*4882a593Smuzhiyun 
8210*4882a593Smuzhiyun 	switch (fan_status_access_mode) {
8211*4882a593Smuzhiyun 	case TPACPI_FAN_RD_ACPI_GFAN: {
8212*4882a593Smuzhiyun 		/* 570, 600e/x, 770e, 770x */
8213*4882a593Smuzhiyun 		int res;
8214*4882a593Smuzhiyun 
8215*4882a593Smuzhiyun 		if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8216*4882a593Smuzhiyun 			return -EIO;
8217*4882a593Smuzhiyun 
8218*4882a593Smuzhiyun 		if (likely(status))
8219*4882a593Smuzhiyun 			*status = res & 0x07;
8220*4882a593Smuzhiyun 
8221*4882a593Smuzhiyun 		break;
8222*4882a593Smuzhiyun 	}
8223*4882a593Smuzhiyun 	case TPACPI_FAN_RD_TPEC:
8224*4882a593Smuzhiyun 		/* all except 570, 600e/x, 770e, 770x */
8225*4882a593Smuzhiyun 		if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8226*4882a593Smuzhiyun 			return -EIO;
8227*4882a593Smuzhiyun 
8228*4882a593Smuzhiyun 		if (likely(status)) {
8229*4882a593Smuzhiyun 			*status = s;
8230*4882a593Smuzhiyun 			fan_quirk1_handle(status);
8231*4882a593Smuzhiyun 		}
8232*4882a593Smuzhiyun 
8233*4882a593Smuzhiyun 		break;
8234*4882a593Smuzhiyun 
8235*4882a593Smuzhiyun 	default:
8236*4882a593Smuzhiyun 		return -ENXIO;
8237*4882a593Smuzhiyun 	}
8238*4882a593Smuzhiyun 
8239*4882a593Smuzhiyun 	return 0;
8240*4882a593Smuzhiyun }
8241*4882a593Smuzhiyun 
fan_get_status_safe(u8 * status)8242*4882a593Smuzhiyun static int fan_get_status_safe(u8 *status)
8243*4882a593Smuzhiyun {
8244*4882a593Smuzhiyun 	int rc;
8245*4882a593Smuzhiyun 	u8 s;
8246*4882a593Smuzhiyun 
8247*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8248*4882a593Smuzhiyun 		return -ERESTARTSYS;
8249*4882a593Smuzhiyun 	rc = fan_get_status(&s);
8250*4882a593Smuzhiyun 	if (!rc)
8251*4882a593Smuzhiyun 		fan_update_desired_level(s);
8252*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8253*4882a593Smuzhiyun 
8254*4882a593Smuzhiyun 	if (rc)
8255*4882a593Smuzhiyun 		return rc;
8256*4882a593Smuzhiyun 	if (status)
8257*4882a593Smuzhiyun 		*status = s;
8258*4882a593Smuzhiyun 
8259*4882a593Smuzhiyun 	return 0;
8260*4882a593Smuzhiyun }
8261*4882a593Smuzhiyun 
fan_get_speed(unsigned int * speed)8262*4882a593Smuzhiyun static int fan_get_speed(unsigned int *speed)
8263*4882a593Smuzhiyun {
8264*4882a593Smuzhiyun 	u8 hi, lo;
8265*4882a593Smuzhiyun 
8266*4882a593Smuzhiyun 	switch (fan_status_access_mode) {
8267*4882a593Smuzhiyun 	case TPACPI_FAN_RD_TPEC:
8268*4882a593Smuzhiyun 		/* all except 570, 600e/x, 770e, 770x */
8269*4882a593Smuzhiyun 		if (unlikely(!fan_select_fan1()))
8270*4882a593Smuzhiyun 			return -EIO;
8271*4882a593Smuzhiyun 		if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8272*4882a593Smuzhiyun 			     !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8273*4882a593Smuzhiyun 			return -EIO;
8274*4882a593Smuzhiyun 
8275*4882a593Smuzhiyun 		if (likely(speed))
8276*4882a593Smuzhiyun 			*speed = (hi << 8) | lo;
8277*4882a593Smuzhiyun 
8278*4882a593Smuzhiyun 		break;
8279*4882a593Smuzhiyun 
8280*4882a593Smuzhiyun 	default:
8281*4882a593Smuzhiyun 		return -ENXIO;
8282*4882a593Smuzhiyun 	}
8283*4882a593Smuzhiyun 
8284*4882a593Smuzhiyun 	return 0;
8285*4882a593Smuzhiyun }
8286*4882a593Smuzhiyun 
fan2_get_speed(unsigned int * speed)8287*4882a593Smuzhiyun static int fan2_get_speed(unsigned int *speed)
8288*4882a593Smuzhiyun {
8289*4882a593Smuzhiyun 	u8 hi, lo;
8290*4882a593Smuzhiyun 	bool rc;
8291*4882a593Smuzhiyun 
8292*4882a593Smuzhiyun 	switch (fan_status_access_mode) {
8293*4882a593Smuzhiyun 	case TPACPI_FAN_RD_TPEC:
8294*4882a593Smuzhiyun 		/* all except 570, 600e/x, 770e, 770x */
8295*4882a593Smuzhiyun 		if (unlikely(!fan_select_fan2()))
8296*4882a593Smuzhiyun 			return -EIO;
8297*4882a593Smuzhiyun 		rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8298*4882a593Smuzhiyun 			     !acpi_ec_read(fan_rpm_offset + 1, &hi);
8299*4882a593Smuzhiyun 		fan_select_fan1(); /* play it safe */
8300*4882a593Smuzhiyun 		if (rc)
8301*4882a593Smuzhiyun 			return -EIO;
8302*4882a593Smuzhiyun 
8303*4882a593Smuzhiyun 		if (likely(speed))
8304*4882a593Smuzhiyun 			*speed = (hi << 8) | lo;
8305*4882a593Smuzhiyun 
8306*4882a593Smuzhiyun 		break;
8307*4882a593Smuzhiyun 
8308*4882a593Smuzhiyun 	default:
8309*4882a593Smuzhiyun 		return -ENXIO;
8310*4882a593Smuzhiyun 	}
8311*4882a593Smuzhiyun 
8312*4882a593Smuzhiyun 	return 0;
8313*4882a593Smuzhiyun }
8314*4882a593Smuzhiyun 
fan_set_level(int level)8315*4882a593Smuzhiyun static int fan_set_level(int level)
8316*4882a593Smuzhiyun {
8317*4882a593Smuzhiyun 	if (!fan_control_allowed)
8318*4882a593Smuzhiyun 		return -EPERM;
8319*4882a593Smuzhiyun 
8320*4882a593Smuzhiyun 	switch (fan_control_access_mode) {
8321*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_SFAN:
8322*4882a593Smuzhiyun 		if ((level < 0) || (level > 7))
8323*4882a593Smuzhiyun 			return -EINVAL;
8324*4882a593Smuzhiyun 
8325*4882a593Smuzhiyun 		if (tp_features.second_fan_ctl) {
8326*4882a593Smuzhiyun 			if (!fan_select_fan2() ||
8327*4882a593Smuzhiyun 			    !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8328*4882a593Smuzhiyun 				pr_warn("Couldn't set 2nd fan level, disabling support\n");
8329*4882a593Smuzhiyun 				tp_features.second_fan_ctl = 0;
8330*4882a593Smuzhiyun 			}
8331*4882a593Smuzhiyun 			fan_select_fan1();
8332*4882a593Smuzhiyun 		}
8333*4882a593Smuzhiyun 		if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8334*4882a593Smuzhiyun 			return -EIO;
8335*4882a593Smuzhiyun 		break;
8336*4882a593Smuzhiyun 
8337*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_FANS:
8338*4882a593Smuzhiyun 	case TPACPI_FAN_WR_TPEC:
8339*4882a593Smuzhiyun 		if (!(level & TP_EC_FAN_AUTO) &&
8340*4882a593Smuzhiyun 		    !(level & TP_EC_FAN_FULLSPEED) &&
8341*4882a593Smuzhiyun 		    ((level < 0) || (level > 7)))
8342*4882a593Smuzhiyun 			return -EINVAL;
8343*4882a593Smuzhiyun 
8344*4882a593Smuzhiyun 		/* safety net should the EC not support AUTO
8345*4882a593Smuzhiyun 		 * or FULLSPEED mode bits and just ignore them */
8346*4882a593Smuzhiyun 		if (level & TP_EC_FAN_FULLSPEED)
8347*4882a593Smuzhiyun 			level |= 7;	/* safety min speed 7 */
8348*4882a593Smuzhiyun 		else if (level & TP_EC_FAN_AUTO)
8349*4882a593Smuzhiyun 			level |= 4;	/* safety min speed 4 */
8350*4882a593Smuzhiyun 
8351*4882a593Smuzhiyun 		if (tp_features.second_fan_ctl) {
8352*4882a593Smuzhiyun 			if (!fan_select_fan2() ||
8353*4882a593Smuzhiyun 			    !acpi_ec_write(fan_status_offset, level)) {
8354*4882a593Smuzhiyun 				pr_warn("Couldn't set 2nd fan level, disabling support\n");
8355*4882a593Smuzhiyun 				tp_features.second_fan_ctl = 0;
8356*4882a593Smuzhiyun 			}
8357*4882a593Smuzhiyun 			fan_select_fan1();
8358*4882a593Smuzhiyun 
8359*4882a593Smuzhiyun 		}
8360*4882a593Smuzhiyun 		if (!acpi_ec_write(fan_status_offset, level))
8361*4882a593Smuzhiyun 			return -EIO;
8362*4882a593Smuzhiyun 		else
8363*4882a593Smuzhiyun 			tp_features.fan_ctrl_status_undef = 0;
8364*4882a593Smuzhiyun 		break;
8365*4882a593Smuzhiyun 
8366*4882a593Smuzhiyun 	default:
8367*4882a593Smuzhiyun 		return -ENXIO;
8368*4882a593Smuzhiyun 	}
8369*4882a593Smuzhiyun 
8370*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_FAN,
8371*4882a593Smuzhiyun 		"fan control: set fan control register to 0x%02x\n", level);
8372*4882a593Smuzhiyun 	return 0;
8373*4882a593Smuzhiyun }
8374*4882a593Smuzhiyun 
fan_set_level_safe(int level)8375*4882a593Smuzhiyun static int fan_set_level_safe(int level)
8376*4882a593Smuzhiyun {
8377*4882a593Smuzhiyun 	int rc;
8378*4882a593Smuzhiyun 
8379*4882a593Smuzhiyun 	if (!fan_control_allowed)
8380*4882a593Smuzhiyun 		return -EPERM;
8381*4882a593Smuzhiyun 
8382*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8383*4882a593Smuzhiyun 		return -ERESTARTSYS;
8384*4882a593Smuzhiyun 
8385*4882a593Smuzhiyun 	if (level == TPACPI_FAN_LAST_LEVEL)
8386*4882a593Smuzhiyun 		level = fan_control_desired_level;
8387*4882a593Smuzhiyun 
8388*4882a593Smuzhiyun 	rc = fan_set_level(level);
8389*4882a593Smuzhiyun 	if (!rc)
8390*4882a593Smuzhiyun 		fan_update_desired_level(level);
8391*4882a593Smuzhiyun 
8392*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8393*4882a593Smuzhiyun 	return rc;
8394*4882a593Smuzhiyun }
8395*4882a593Smuzhiyun 
fan_set_enable(void)8396*4882a593Smuzhiyun static int fan_set_enable(void)
8397*4882a593Smuzhiyun {
8398*4882a593Smuzhiyun 	u8 s;
8399*4882a593Smuzhiyun 	int rc;
8400*4882a593Smuzhiyun 
8401*4882a593Smuzhiyun 	if (!fan_control_allowed)
8402*4882a593Smuzhiyun 		return -EPERM;
8403*4882a593Smuzhiyun 
8404*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8405*4882a593Smuzhiyun 		return -ERESTARTSYS;
8406*4882a593Smuzhiyun 
8407*4882a593Smuzhiyun 	switch (fan_control_access_mode) {
8408*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_FANS:
8409*4882a593Smuzhiyun 	case TPACPI_FAN_WR_TPEC:
8410*4882a593Smuzhiyun 		rc = fan_get_status(&s);
8411*4882a593Smuzhiyun 		if (rc < 0)
8412*4882a593Smuzhiyun 			break;
8413*4882a593Smuzhiyun 
8414*4882a593Smuzhiyun 		/* Don't go out of emergency fan mode */
8415*4882a593Smuzhiyun 		if (s != 7) {
8416*4882a593Smuzhiyun 			s &= 0x07;
8417*4882a593Smuzhiyun 			s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8418*4882a593Smuzhiyun 		}
8419*4882a593Smuzhiyun 
8420*4882a593Smuzhiyun 		if (!acpi_ec_write(fan_status_offset, s))
8421*4882a593Smuzhiyun 			rc = -EIO;
8422*4882a593Smuzhiyun 		else {
8423*4882a593Smuzhiyun 			tp_features.fan_ctrl_status_undef = 0;
8424*4882a593Smuzhiyun 			rc = 0;
8425*4882a593Smuzhiyun 		}
8426*4882a593Smuzhiyun 		break;
8427*4882a593Smuzhiyun 
8428*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_SFAN:
8429*4882a593Smuzhiyun 		rc = fan_get_status(&s);
8430*4882a593Smuzhiyun 		if (rc < 0)
8431*4882a593Smuzhiyun 			break;
8432*4882a593Smuzhiyun 
8433*4882a593Smuzhiyun 		s &= 0x07;
8434*4882a593Smuzhiyun 
8435*4882a593Smuzhiyun 		/* Set fan to at least level 4 */
8436*4882a593Smuzhiyun 		s |= 4;
8437*4882a593Smuzhiyun 
8438*4882a593Smuzhiyun 		if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8439*4882a593Smuzhiyun 			rc = -EIO;
8440*4882a593Smuzhiyun 		else
8441*4882a593Smuzhiyun 			rc = 0;
8442*4882a593Smuzhiyun 		break;
8443*4882a593Smuzhiyun 
8444*4882a593Smuzhiyun 	default:
8445*4882a593Smuzhiyun 		rc = -ENXIO;
8446*4882a593Smuzhiyun 	}
8447*4882a593Smuzhiyun 
8448*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8449*4882a593Smuzhiyun 
8450*4882a593Smuzhiyun 	if (!rc)
8451*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_FAN,
8452*4882a593Smuzhiyun 			"fan control: set fan control register to 0x%02x\n",
8453*4882a593Smuzhiyun 			s);
8454*4882a593Smuzhiyun 	return rc;
8455*4882a593Smuzhiyun }
8456*4882a593Smuzhiyun 
fan_set_disable(void)8457*4882a593Smuzhiyun static int fan_set_disable(void)
8458*4882a593Smuzhiyun {
8459*4882a593Smuzhiyun 	int rc;
8460*4882a593Smuzhiyun 
8461*4882a593Smuzhiyun 	if (!fan_control_allowed)
8462*4882a593Smuzhiyun 		return -EPERM;
8463*4882a593Smuzhiyun 
8464*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8465*4882a593Smuzhiyun 		return -ERESTARTSYS;
8466*4882a593Smuzhiyun 
8467*4882a593Smuzhiyun 	rc = 0;
8468*4882a593Smuzhiyun 	switch (fan_control_access_mode) {
8469*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_FANS:
8470*4882a593Smuzhiyun 	case TPACPI_FAN_WR_TPEC:
8471*4882a593Smuzhiyun 		if (!acpi_ec_write(fan_status_offset, 0x00))
8472*4882a593Smuzhiyun 			rc = -EIO;
8473*4882a593Smuzhiyun 		else {
8474*4882a593Smuzhiyun 			fan_control_desired_level = 0;
8475*4882a593Smuzhiyun 			tp_features.fan_ctrl_status_undef = 0;
8476*4882a593Smuzhiyun 		}
8477*4882a593Smuzhiyun 		break;
8478*4882a593Smuzhiyun 
8479*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_SFAN:
8480*4882a593Smuzhiyun 		if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8481*4882a593Smuzhiyun 			rc = -EIO;
8482*4882a593Smuzhiyun 		else
8483*4882a593Smuzhiyun 			fan_control_desired_level = 0;
8484*4882a593Smuzhiyun 		break;
8485*4882a593Smuzhiyun 
8486*4882a593Smuzhiyun 	default:
8487*4882a593Smuzhiyun 		rc = -ENXIO;
8488*4882a593Smuzhiyun 	}
8489*4882a593Smuzhiyun 
8490*4882a593Smuzhiyun 	if (!rc)
8491*4882a593Smuzhiyun 		vdbg_printk(TPACPI_DBG_FAN,
8492*4882a593Smuzhiyun 			"fan control: set fan control register to 0\n");
8493*4882a593Smuzhiyun 
8494*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8495*4882a593Smuzhiyun 	return rc;
8496*4882a593Smuzhiyun }
8497*4882a593Smuzhiyun 
fan_set_speed(int speed)8498*4882a593Smuzhiyun static int fan_set_speed(int speed)
8499*4882a593Smuzhiyun {
8500*4882a593Smuzhiyun 	int rc;
8501*4882a593Smuzhiyun 
8502*4882a593Smuzhiyun 	if (!fan_control_allowed)
8503*4882a593Smuzhiyun 		return -EPERM;
8504*4882a593Smuzhiyun 
8505*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8506*4882a593Smuzhiyun 		return -ERESTARTSYS;
8507*4882a593Smuzhiyun 
8508*4882a593Smuzhiyun 	rc = 0;
8509*4882a593Smuzhiyun 	switch (fan_control_access_mode) {
8510*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_FANS:
8511*4882a593Smuzhiyun 		if (speed >= 0 && speed <= 65535) {
8512*4882a593Smuzhiyun 			if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8513*4882a593Smuzhiyun 					speed, speed, speed))
8514*4882a593Smuzhiyun 				rc = -EIO;
8515*4882a593Smuzhiyun 		} else
8516*4882a593Smuzhiyun 			rc = -EINVAL;
8517*4882a593Smuzhiyun 		break;
8518*4882a593Smuzhiyun 
8519*4882a593Smuzhiyun 	default:
8520*4882a593Smuzhiyun 		rc = -ENXIO;
8521*4882a593Smuzhiyun 	}
8522*4882a593Smuzhiyun 
8523*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8524*4882a593Smuzhiyun 	return rc;
8525*4882a593Smuzhiyun }
8526*4882a593Smuzhiyun 
fan_watchdog_reset(void)8527*4882a593Smuzhiyun static void fan_watchdog_reset(void)
8528*4882a593Smuzhiyun {
8529*4882a593Smuzhiyun 	if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8530*4882a593Smuzhiyun 		return;
8531*4882a593Smuzhiyun 
8532*4882a593Smuzhiyun 	if (fan_watchdog_maxinterval > 0 &&
8533*4882a593Smuzhiyun 	    tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8534*4882a593Smuzhiyun 		mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8535*4882a593Smuzhiyun 			msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8536*4882a593Smuzhiyun 	else
8537*4882a593Smuzhiyun 		cancel_delayed_work(&fan_watchdog_task);
8538*4882a593Smuzhiyun }
8539*4882a593Smuzhiyun 
fan_watchdog_fire(struct work_struct * ignored)8540*4882a593Smuzhiyun static void fan_watchdog_fire(struct work_struct *ignored)
8541*4882a593Smuzhiyun {
8542*4882a593Smuzhiyun 	int rc;
8543*4882a593Smuzhiyun 
8544*4882a593Smuzhiyun 	if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8545*4882a593Smuzhiyun 		return;
8546*4882a593Smuzhiyun 
8547*4882a593Smuzhiyun 	pr_notice("fan watchdog: enabling fan\n");
8548*4882a593Smuzhiyun 	rc = fan_set_enable();
8549*4882a593Smuzhiyun 	if (rc < 0) {
8550*4882a593Smuzhiyun 		pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8551*4882a593Smuzhiyun 		       rc);
8552*4882a593Smuzhiyun 		/* reschedule for later */
8553*4882a593Smuzhiyun 		fan_watchdog_reset();
8554*4882a593Smuzhiyun 	}
8555*4882a593Smuzhiyun }
8556*4882a593Smuzhiyun 
8557*4882a593Smuzhiyun /*
8558*4882a593Smuzhiyun  * SYSFS fan layout: hwmon compatible (device)
8559*4882a593Smuzhiyun  *
8560*4882a593Smuzhiyun  * pwm*_enable:
8561*4882a593Smuzhiyun  * 	0: "disengaged" mode
8562*4882a593Smuzhiyun  * 	1: manual mode
8563*4882a593Smuzhiyun  * 	2: native EC "auto" mode (recommended, hardware default)
8564*4882a593Smuzhiyun  *
8565*4882a593Smuzhiyun  * pwm*: set speed in manual mode, ignored otherwise.
8566*4882a593Smuzhiyun  * 	0 is level 0; 255 is level 7. Intermediate points done with linear
8567*4882a593Smuzhiyun  * 	interpolation.
8568*4882a593Smuzhiyun  *
8569*4882a593Smuzhiyun  * fan*_input: tachometer reading, RPM
8570*4882a593Smuzhiyun  *
8571*4882a593Smuzhiyun  *
8572*4882a593Smuzhiyun  * SYSFS fan layout: extensions
8573*4882a593Smuzhiyun  *
8574*4882a593Smuzhiyun  * fan_watchdog (driver):
8575*4882a593Smuzhiyun  * 	fan watchdog interval in seconds, 0 disables (default), max 120
8576*4882a593Smuzhiyun  */
8577*4882a593Smuzhiyun 
8578*4882a593Smuzhiyun /* sysfs fan pwm1_enable ----------------------------------------------- */
fan_pwm1_enable_show(struct device * dev,struct device_attribute * attr,char * buf)8579*4882a593Smuzhiyun static ssize_t fan_pwm1_enable_show(struct device *dev,
8580*4882a593Smuzhiyun 				    struct device_attribute *attr,
8581*4882a593Smuzhiyun 				    char *buf)
8582*4882a593Smuzhiyun {
8583*4882a593Smuzhiyun 	int res, mode;
8584*4882a593Smuzhiyun 	u8 status;
8585*4882a593Smuzhiyun 
8586*4882a593Smuzhiyun 	res = fan_get_status_safe(&status);
8587*4882a593Smuzhiyun 	if (res)
8588*4882a593Smuzhiyun 		return res;
8589*4882a593Smuzhiyun 
8590*4882a593Smuzhiyun 	if (status & TP_EC_FAN_FULLSPEED) {
8591*4882a593Smuzhiyun 		mode = 0;
8592*4882a593Smuzhiyun 	} else if (status & TP_EC_FAN_AUTO) {
8593*4882a593Smuzhiyun 		mode = 2;
8594*4882a593Smuzhiyun 	} else
8595*4882a593Smuzhiyun 		mode = 1;
8596*4882a593Smuzhiyun 
8597*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8598*4882a593Smuzhiyun }
8599*4882a593Smuzhiyun 
fan_pwm1_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)8600*4882a593Smuzhiyun static ssize_t fan_pwm1_enable_store(struct device *dev,
8601*4882a593Smuzhiyun 				     struct device_attribute *attr,
8602*4882a593Smuzhiyun 				     const char *buf, size_t count)
8603*4882a593Smuzhiyun {
8604*4882a593Smuzhiyun 	unsigned long t;
8605*4882a593Smuzhiyun 	int res, level;
8606*4882a593Smuzhiyun 
8607*4882a593Smuzhiyun 	if (parse_strtoul(buf, 2, &t))
8608*4882a593Smuzhiyun 		return -EINVAL;
8609*4882a593Smuzhiyun 
8610*4882a593Smuzhiyun 	tpacpi_disclose_usertask("hwmon pwm1_enable",
8611*4882a593Smuzhiyun 			"set fan mode to %lu\n", t);
8612*4882a593Smuzhiyun 
8613*4882a593Smuzhiyun 	switch (t) {
8614*4882a593Smuzhiyun 	case 0:
8615*4882a593Smuzhiyun 		level = TP_EC_FAN_FULLSPEED;
8616*4882a593Smuzhiyun 		break;
8617*4882a593Smuzhiyun 	case 1:
8618*4882a593Smuzhiyun 		level = TPACPI_FAN_LAST_LEVEL;
8619*4882a593Smuzhiyun 		break;
8620*4882a593Smuzhiyun 	case 2:
8621*4882a593Smuzhiyun 		level = TP_EC_FAN_AUTO;
8622*4882a593Smuzhiyun 		break;
8623*4882a593Smuzhiyun 	case 3:
8624*4882a593Smuzhiyun 		/* reserved for software-controlled auto mode */
8625*4882a593Smuzhiyun 		return -ENOSYS;
8626*4882a593Smuzhiyun 	default:
8627*4882a593Smuzhiyun 		return -EINVAL;
8628*4882a593Smuzhiyun 	}
8629*4882a593Smuzhiyun 
8630*4882a593Smuzhiyun 	res = fan_set_level_safe(level);
8631*4882a593Smuzhiyun 	if (res == -ENXIO)
8632*4882a593Smuzhiyun 		return -EINVAL;
8633*4882a593Smuzhiyun 	else if (res < 0)
8634*4882a593Smuzhiyun 		return res;
8635*4882a593Smuzhiyun 
8636*4882a593Smuzhiyun 	fan_watchdog_reset();
8637*4882a593Smuzhiyun 
8638*4882a593Smuzhiyun 	return count;
8639*4882a593Smuzhiyun }
8640*4882a593Smuzhiyun 
8641*4882a593Smuzhiyun static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8642*4882a593Smuzhiyun 		   fan_pwm1_enable_show, fan_pwm1_enable_store);
8643*4882a593Smuzhiyun 
8644*4882a593Smuzhiyun /* sysfs fan pwm1 ------------------------------------------------------ */
fan_pwm1_show(struct device * dev,struct device_attribute * attr,char * buf)8645*4882a593Smuzhiyun static ssize_t fan_pwm1_show(struct device *dev,
8646*4882a593Smuzhiyun 			     struct device_attribute *attr,
8647*4882a593Smuzhiyun 			     char *buf)
8648*4882a593Smuzhiyun {
8649*4882a593Smuzhiyun 	int res;
8650*4882a593Smuzhiyun 	u8 status;
8651*4882a593Smuzhiyun 
8652*4882a593Smuzhiyun 	res = fan_get_status_safe(&status);
8653*4882a593Smuzhiyun 	if (res)
8654*4882a593Smuzhiyun 		return res;
8655*4882a593Smuzhiyun 
8656*4882a593Smuzhiyun 	if ((status &
8657*4882a593Smuzhiyun 	     (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8658*4882a593Smuzhiyun 		status = fan_control_desired_level;
8659*4882a593Smuzhiyun 
8660*4882a593Smuzhiyun 	if (status > 7)
8661*4882a593Smuzhiyun 		status = 7;
8662*4882a593Smuzhiyun 
8663*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8664*4882a593Smuzhiyun }
8665*4882a593Smuzhiyun 
fan_pwm1_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)8666*4882a593Smuzhiyun static ssize_t fan_pwm1_store(struct device *dev,
8667*4882a593Smuzhiyun 			      struct device_attribute *attr,
8668*4882a593Smuzhiyun 			      const char *buf, size_t count)
8669*4882a593Smuzhiyun {
8670*4882a593Smuzhiyun 	unsigned long s;
8671*4882a593Smuzhiyun 	int rc;
8672*4882a593Smuzhiyun 	u8 status, newlevel;
8673*4882a593Smuzhiyun 
8674*4882a593Smuzhiyun 	if (parse_strtoul(buf, 255, &s))
8675*4882a593Smuzhiyun 		return -EINVAL;
8676*4882a593Smuzhiyun 
8677*4882a593Smuzhiyun 	tpacpi_disclose_usertask("hwmon pwm1",
8678*4882a593Smuzhiyun 			"set fan speed to %lu\n", s);
8679*4882a593Smuzhiyun 
8680*4882a593Smuzhiyun 	/* scale down from 0-255 to 0-7 */
8681*4882a593Smuzhiyun 	newlevel = (s >> 5) & 0x07;
8682*4882a593Smuzhiyun 
8683*4882a593Smuzhiyun 	if (mutex_lock_killable(&fan_mutex))
8684*4882a593Smuzhiyun 		return -ERESTARTSYS;
8685*4882a593Smuzhiyun 
8686*4882a593Smuzhiyun 	rc = fan_get_status(&status);
8687*4882a593Smuzhiyun 	if (!rc && (status &
8688*4882a593Smuzhiyun 		    (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8689*4882a593Smuzhiyun 		rc = fan_set_level(newlevel);
8690*4882a593Smuzhiyun 		if (rc == -ENXIO)
8691*4882a593Smuzhiyun 			rc = -EINVAL;
8692*4882a593Smuzhiyun 		else if (!rc) {
8693*4882a593Smuzhiyun 			fan_update_desired_level(newlevel);
8694*4882a593Smuzhiyun 			fan_watchdog_reset();
8695*4882a593Smuzhiyun 		}
8696*4882a593Smuzhiyun 	}
8697*4882a593Smuzhiyun 
8698*4882a593Smuzhiyun 	mutex_unlock(&fan_mutex);
8699*4882a593Smuzhiyun 	return (rc) ? rc : count;
8700*4882a593Smuzhiyun }
8701*4882a593Smuzhiyun 
8702*4882a593Smuzhiyun static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8703*4882a593Smuzhiyun 
8704*4882a593Smuzhiyun /* sysfs fan fan1_input ------------------------------------------------ */
fan_fan1_input_show(struct device * dev,struct device_attribute * attr,char * buf)8705*4882a593Smuzhiyun static ssize_t fan_fan1_input_show(struct device *dev,
8706*4882a593Smuzhiyun 			   struct device_attribute *attr,
8707*4882a593Smuzhiyun 			   char *buf)
8708*4882a593Smuzhiyun {
8709*4882a593Smuzhiyun 	int res;
8710*4882a593Smuzhiyun 	unsigned int speed;
8711*4882a593Smuzhiyun 
8712*4882a593Smuzhiyun 	res = fan_get_speed(&speed);
8713*4882a593Smuzhiyun 	if (res < 0)
8714*4882a593Smuzhiyun 		return res;
8715*4882a593Smuzhiyun 
8716*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8717*4882a593Smuzhiyun }
8718*4882a593Smuzhiyun 
8719*4882a593Smuzhiyun static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8720*4882a593Smuzhiyun 
8721*4882a593Smuzhiyun /* sysfs fan fan2_input ------------------------------------------------ */
fan_fan2_input_show(struct device * dev,struct device_attribute * attr,char * buf)8722*4882a593Smuzhiyun static ssize_t fan_fan2_input_show(struct device *dev,
8723*4882a593Smuzhiyun 			   struct device_attribute *attr,
8724*4882a593Smuzhiyun 			   char *buf)
8725*4882a593Smuzhiyun {
8726*4882a593Smuzhiyun 	int res;
8727*4882a593Smuzhiyun 	unsigned int speed;
8728*4882a593Smuzhiyun 
8729*4882a593Smuzhiyun 	res = fan2_get_speed(&speed);
8730*4882a593Smuzhiyun 	if (res < 0)
8731*4882a593Smuzhiyun 		return res;
8732*4882a593Smuzhiyun 
8733*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8734*4882a593Smuzhiyun }
8735*4882a593Smuzhiyun 
8736*4882a593Smuzhiyun static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8737*4882a593Smuzhiyun 
8738*4882a593Smuzhiyun /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
fan_watchdog_show(struct device_driver * drv,char * buf)8739*4882a593Smuzhiyun static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8740*4882a593Smuzhiyun {
8741*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8742*4882a593Smuzhiyun }
8743*4882a593Smuzhiyun 
fan_watchdog_store(struct device_driver * drv,const char * buf,size_t count)8744*4882a593Smuzhiyun static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8745*4882a593Smuzhiyun 				  size_t count)
8746*4882a593Smuzhiyun {
8747*4882a593Smuzhiyun 	unsigned long t;
8748*4882a593Smuzhiyun 
8749*4882a593Smuzhiyun 	if (parse_strtoul(buf, 120, &t))
8750*4882a593Smuzhiyun 		return -EINVAL;
8751*4882a593Smuzhiyun 
8752*4882a593Smuzhiyun 	if (!fan_control_allowed)
8753*4882a593Smuzhiyun 		return -EPERM;
8754*4882a593Smuzhiyun 
8755*4882a593Smuzhiyun 	fan_watchdog_maxinterval = t;
8756*4882a593Smuzhiyun 	fan_watchdog_reset();
8757*4882a593Smuzhiyun 
8758*4882a593Smuzhiyun 	tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8759*4882a593Smuzhiyun 
8760*4882a593Smuzhiyun 	return count;
8761*4882a593Smuzhiyun }
8762*4882a593Smuzhiyun static DRIVER_ATTR_RW(fan_watchdog);
8763*4882a593Smuzhiyun 
8764*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
8765*4882a593Smuzhiyun static struct attribute *fan_attributes[] = {
8766*4882a593Smuzhiyun 	&dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8767*4882a593Smuzhiyun 	&dev_attr_fan1_input.attr,
8768*4882a593Smuzhiyun 	NULL, /* for fan2_input */
8769*4882a593Smuzhiyun 	NULL
8770*4882a593Smuzhiyun };
8771*4882a593Smuzhiyun 
8772*4882a593Smuzhiyun static const struct attribute_group fan_attr_group = {
8773*4882a593Smuzhiyun 	.attrs = fan_attributes,
8774*4882a593Smuzhiyun };
8775*4882a593Smuzhiyun 
8776*4882a593Smuzhiyun #define TPACPI_FAN_Q1	0x0001		/* Unitialized HFSP */
8777*4882a593Smuzhiyun #define TPACPI_FAN_2FAN	0x0002		/* EC 0x31 bit 0 selects fan2 */
8778*4882a593Smuzhiyun #define TPACPI_FAN_2CTL	0x0004		/* selects fan2 control */
8779*4882a593Smuzhiyun 
8780*4882a593Smuzhiyun static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8781*4882a593Smuzhiyun 	TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8782*4882a593Smuzhiyun 	TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8783*4882a593Smuzhiyun 	TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8784*4882a593Smuzhiyun 	TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8785*4882a593Smuzhiyun 	TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8786*4882a593Smuzhiyun 	TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8787*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL),	/* P70 */
8788*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL),	/* P50 */
8789*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL),	/* P71 */
8790*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL),	/* P51 */
8791*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL),	/* P52 / P72 */
8792*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL),	/* P53 / P73 */
8793*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL),	/* P1 / X1 Extreme (1st gen) */
8794*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL),	/* P1 / X1 Extreme (2nd gen) */
8795*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL),	/* P1 / X1 Extreme (3nd gen) */
8796*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '4', '0', TPACPI_FAN_2CTL),	/* P1 / X1 Extreme (4nd gen) */
8797*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL),	/* P15 (1st gen) / P15v (1st gen) */
8798*4882a593Smuzhiyun 	TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL),	/* X1 Carbon (9th gen) */
8799*4882a593Smuzhiyun };
8800*4882a593Smuzhiyun 
fan_init(struct ibm_init_struct * iibm)8801*4882a593Smuzhiyun static int __init fan_init(struct ibm_init_struct *iibm)
8802*4882a593Smuzhiyun {
8803*4882a593Smuzhiyun 	int rc;
8804*4882a593Smuzhiyun 	unsigned long quirks;
8805*4882a593Smuzhiyun 
8806*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8807*4882a593Smuzhiyun 			"initializing fan subdriver\n");
8808*4882a593Smuzhiyun 
8809*4882a593Smuzhiyun 	mutex_init(&fan_mutex);
8810*4882a593Smuzhiyun 	fan_status_access_mode = TPACPI_FAN_NONE;
8811*4882a593Smuzhiyun 	fan_control_access_mode = TPACPI_FAN_WR_NONE;
8812*4882a593Smuzhiyun 	fan_control_commands = 0;
8813*4882a593Smuzhiyun 	fan_watchdog_maxinterval = 0;
8814*4882a593Smuzhiyun 	tp_features.fan_ctrl_status_undef = 0;
8815*4882a593Smuzhiyun 	tp_features.second_fan = 0;
8816*4882a593Smuzhiyun 	tp_features.second_fan_ctl = 0;
8817*4882a593Smuzhiyun 	fan_control_desired_level = 7;
8818*4882a593Smuzhiyun 
8819*4882a593Smuzhiyun 	if (tpacpi_is_ibm()) {
8820*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(fans);
8821*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(gfan);
8822*4882a593Smuzhiyun 		TPACPI_ACPIHANDLE_INIT(sfan);
8823*4882a593Smuzhiyun 	}
8824*4882a593Smuzhiyun 
8825*4882a593Smuzhiyun 	quirks = tpacpi_check_quirks(fan_quirk_table,
8826*4882a593Smuzhiyun 				     ARRAY_SIZE(fan_quirk_table));
8827*4882a593Smuzhiyun 
8828*4882a593Smuzhiyun 	if (gfan_handle) {
8829*4882a593Smuzhiyun 		/* 570, 600e/x, 770e, 770x */
8830*4882a593Smuzhiyun 		fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8831*4882a593Smuzhiyun 	} else {
8832*4882a593Smuzhiyun 		/* all other ThinkPads: note that even old-style
8833*4882a593Smuzhiyun 		 * ThinkPad ECs supports the fan control register */
8834*4882a593Smuzhiyun 		if (likely(acpi_ec_read(fan_status_offset,
8835*4882a593Smuzhiyun 					&fan_control_initial_status))) {
8836*4882a593Smuzhiyun 			fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8837*4882a593Smuzhiyun 			if (quirks & TPACPI_FAN_Q1)
8838*4882a593Smuzhiyun 				fan_quirk1_setup();
8839*4882a593Smuzhiyun 			if (quirks & TPACPI_FAN_2FAN) {
8840*4882a593Smuzhiyun 				tp_features.second_fan = 1;
8841*4882a593Smuzhiyun 				pr_info("secondary fan support enabled\n");
8842*4882a593Smuzhiyun 			}
8843*4882a593Smuzhiyun 			if (quirks & TPACPI_FAN_2CTL) {
8844*4882a593Smuzhiyun 				tp_features.second_fan = 1;
8845*4882a593Smuzhiyun 				tp_features.second_fan_ctl = 1;
8846*4882a593Smuzhiyun 				pr_info("secondary fan control enabled\n");
8847*4882a593Smuzhiyun 			}
8848*4882a593Smuzhiyun 		} else {
8849*4882a593Smuzhiyun 			pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8850*4882a593Smuzhiyun 			return 1;
8851*4882a593Smuzhiyun 		}
8852*4882a593Smuzhiyun 	}
8853*4882a593Smuzhiyun 
8854*4882a593Smuzhiyun 	if (sfan_handle) {
8855*4882a593Smuzhiyun 		/* 570, 770x-JL */
8856*4882a593Smuzhiyun 		fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8857*4882a593Smuzhiyun 		fan_control_commands |=
8858*4882a593Smuzhiyun 		    TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8859*4882a593Smuzhiyun 	} else {
8860*4882a593Smuzhiyun 		if (!gfan_handle) {
8861*4882a593Smuzhiyun 			/* gfan without sfan means no fan control */
8862*4882a593Smuzhiyun 			/* all other models implement TP EC 0x2f control */
8863*4882a593Smuzhiyun 
8864*4882a593Smuzhiyun 			if (fans_handle) {
8865*4882a593Smuzhiyun 				/* X31, X40, X41 */
8866*4882a593Smuzhiyun 				fan_control_access_mode =
8867*4882a593Smuzhiyun 				    TPACPI_FAN_WR_ACPI_FANS;
8868*4882a593Smuzhiyun 				fan_control_commands |=
8869*4882a593Smuzhiyun 				    TPACPI_FAN_CMD_SPEED |
8870*4882a593Smuzhiyun 				    TPACPI_FAN_CMD_LEVEL |
8871*4882a593Smuzhiyun 				    TPACPI_FAN_CMD_ENABLE;
8872*4882a593Smuzhiyun 			} else {
8873*4882a593Smuzhiyun 				fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8874*4882a593Smuzhiyun 				fan_control_commands |=
8875*4882a593Smuzhiyun 				    TPACPI_FAN_CMD_LEVEL |
8876*4882a593Smuzhiyun 				    TPACPI_FAN_CMD_ENABLE;
8877*4882a593Smuzhiyun 			}
8878*4882a593Smuzhiyun 		}
8879*4882a593Smuzhiyun 	}
8880*4882a593Smuzhiyun 
8881*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8882*4882a593Smuzhiyun 		"fan is %s, modes %d, %d\n",
8883*4882a593Smuzhiyun 		str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8884*4882a593Smuzhiyun 		  fan_control_access_mode != TPACPI_FAN_WR_NONE),
8885*4882a593Smuzhiyun 		fan_status_access_mode, fan_control_access_mode);
8886*4882a593Smuzhiyun 
8887*4882a593Smuzhiyun 	/* fan control master switch */
8888*4882a593Smuzhiyun 	if (!fan_control_allowed) {
8889*4882a593Smuzhiyun 		fan_control_access_mode = TPACPI_FAN_WR_NONE;
8890*4882a593Smuzhiyun 		fan_control_commands = 0;
8891*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8892*4882a593Smuzhiyun 			   "fan control features disabled by parameter\n");
8893*4882a593Smuzhiyun 	}
8894*4882a593Smuzhiyun 
8895*4882a593Smuzhiyun 	/* update fan_control_desired_level */
8896*4882a593Smuzhiyun 	if (fan_status_access_mode != TPACPI_FAN_NONE)
8897*4882a593Smuzhiyun 		fan_get_status_safe(NULL);
8898*4882a593Smuzhiyun 
8899*4882a593Smuzhiyun 	if (fan_status_access_mode != TPACPI_FAN_NONE ||
8900*4882a593Smuzhiyun 	    fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8901*4882a593Smuzhiyun 		if (tp_features.second_fan) {
8902*4882a593Smuzhiyun 			/* attach second fan tachometer */
8903*4882a593Smuzhiyun 			fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8904*4882a593Smuzhiyun 					&dev_attr_fan2_input.attr;
8905*4882a593Smuzhiyun 		}
8906*4882a593Smuzhiyun 		rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8907*4882a593Smuzhiyun 					 &fan_attr_group);
8908*4882a593Smuzhiyun 		if (rc < 0)
8909*4882a593Smuzhiyun 			return rc;
8910*4882a593Smuzhiyun 
8911*4882a593Smuzhiyun 		rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8912*4882a593Smuzhiyun 					&driver_attr_fan_watchdog);
8913*4882a593Smuzhiyun 		if (rc < 0) {
8914*4882a593Smuzhiyun 			sysfs_remove_group(&tpacpi_hwmon->kobj,
8915*4882a593Smuzhiyun 					&fan_attr_group);
8916*4882a593Smuzhiyun 			return rc;
8917*4882a593Smuzhiyun 		}
8918*4882a593Smuzhiyun 		return 0;
8919*4882a593Smuzhiyun 	} else
8920*4882a593Smuzhiyun 		return 1;
8921*4882a593Smuzhiyun }
8922*4882a593Smuzhiyun 
fan_exit(void)8923*4882a593Smuzhiyun static void fan_exit(void)
8924*4882a593Smuzhiyun {
8925*4882a593Smuzhiyun 	vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8926*4882a593Smuzhiyun 		    "cancelling any pending fan watchdog tasks\n");
8927*4882a593Smuzhiyun 
8928*4882a593Smuzhiyun 	/* FIXME: can we really do this unconditionally? */
8929*4882a593Smuzhiyun 	sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8930*4882a593Smuzhiyun 	driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8931*4882a593Smuzhiyun 			   &driver_attr_fan_watchdog);
8932*4882a593Smuzhiyun 
8933*4882a593Smuzhiyun 	cancel_delayed_work(&fan_watchdog_task);
8934*4882a593Smuzhiyun 	flush_workqueue(tpacpi_wq);
8935*4882a593Smuzhiyun }
8936*4882a593Smuzhiyun 
fan_suspend(void)8937*4882a593Smuzhiyun static void fan_suspend(void)
8938*4882a593Smuzhiyun {
8939*4882a593Smuzhiyun 	int rc;
8940*4882a593Smuzhiyun 
8941*4882a593Smuzhiyun 	if (!fan_control_allowed)
8942*4882a593Smuzhiyun 		return;
8943*4882a593Smuzhiyun 
8944*4882a593Smuzhiyun 	/* Store fan status in cache */
8945*4882a593Smuzhiyun 	fan_control_resume_level = 0;
8946*4882a593Smuzhiyun 	rc = fan_get_status_safe(&fan_control_resume_level);
8947*4882a593Smuzhiyun 	if (rc < 0)
8948*4882a593Smuzhiyun 		pr_notice("failed to read fan level for later restore during resume: %d\n",
8949*4882a593Smuzhiyun 			  rc);
8950*4882a593Smuzhiyun 
8951*4882a593Smuzhiyun 	/* if it is undefined, don't attempt to restore it.
8952*4882a593Smuzhiyun 	 * KEEP THIS LAST */
8953*4882a593Smuzhiyun 	if (tp_features.fan_ctrl_status_undef)
8954*4882a593Smuzhiyun 		fan_control_resume_level = 0;
8955*4882a593Smuzhiyun }
8956*4882a593Smuzhiyun 
fan_resume(void)8957*4882a593Smuzhiyun static void fan_resume(void)
8958*4882a593Smuzhiyun {
8959*4882a593Smuzhiyun 	u8 current_level = 7;
8960*4882a593Smuzhiyun 	bool do_set = false;
8961*4882a593Smuzhiyun 	int rc;
8962*4882a593Smuzhiyun 
8963*4882a593Smuzhiyun 	/* DSDT *always* updates status on resume */
8964*4882a593Smuzhiyun 	tp_features.fan_ctrl_status_undef = 0;
8965*4882a593Smuzhiyun 
8966*4882a593Smuzhiyun 	if (!fan_control_allowed ||
8967*4882a593Smuzhiyun 	    !fan_control_resume_level ||
8968*4882a593Smuzhiyun 	    (fan_get_status_safe(&current_level) < 0))
8969*4882a593Smuzhiyun 		return;
8970*4882a593Smuzhiyun 
8971*4882a593Smuzhiyun 	switch (fan_control_access_mode) {
8972*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_SFAN:
8973*4882a593Smuzhiyun 		/* never decrease fan level */
8974*4882a593Smuzhiyun 		do_set = (fan_control_resume_level > current_level);
8975*4882a593Smuzhiyun 		break;
8976*4882a593Smuzhiyun 	case TPACPI_FAN_WR_ACPI_FANS:
8977*4882a593Smuzhiyun 	case TPACPI_FAN_WR_TPEC:
8978*4882a593Smuzhiyun 		/* never decrease fan level, scale is:
8979*4882a593Smuzhiyun 		 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8980*4882a593Smuzhiyun 		 *
8981*4882a593Smuzhiyun 		 * We expect the firmware to set either 7 or AUTO, but we
8982*4882a593Smuzhiyun 		 * handle FULLSPEED out of paranoia.
8983*4882a593Smuzhiyun 		 *
8984*4882a593Smuzhiyun 		 * So, we can safely only restore FULLSPEED or 7, anything
8985*4882a593Smuzhiyun 		 * else could slow the fan.  Restoring AUTO is useless, at
8986*4882a593Smuzhiyun 		 * best that's exactly what the DSDT already set (it is the
8987*4882a593Smuzhiyun 		 * slower it uses).
8988*4882a593Smuzhiyun 		 *
8989*4882a593Smuzhiyun 		 * Always keep in mind that the DSDT *will* have set the
8990*4882a593Smuzhiyun 		 * fans to what the vendor supposes is the best level.  We
8991*4882a593Smuzhiyun 		 * muck with it only to speed the fan up.
8992*4882a593Smuzhiyun 		 */
8993*4882a593Smuzhiyun 		if (fan_control_resume_level != 7 &&
8994*4882a593Smuzhiyun 		    !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8995*4882a593Smuzhiyun 			return;
8996*4882a593Smuzhiyun 		else
8997*4882a593Smuzhiyun 			do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8998*4882a593Smuzhiyun 				 (current_level != fan_control_resume_level);
8999*4882a593Smuzhiyun 		break;
9000*4882a593Smuzhiyun 	default:
9001*4882a593Smuzhiyun 		return;
9002*4882a593Smuzhiyun 	}
9003*4882a593Smuzhiyun 	if (do_set) {
9004*4882a593Smuzhiyun 		pr_notice("restoring fan level to 0x%02x\n",
9005*4882a593Smuzhiyun 			  fan_control_resume_level);
9006*4882a593Smuzhiyun 		rc = fan_set_level_safe(fan_control_resume_level);
9007*4882a593Smuzhiyun 		if (rc < 0)
9008*4882a593Smuzhiyun 			pr_notice("failed to restore fan level: %d\n", rc);
9009*4882a593Smuzhiyun 	}
9010*4882a593Smuzhiyun }
9011*4882a593Smuzhiyun 
fan_read(struct seq_file * m)9012*4882a593Smuzhiyun static int fan_read(struct seq_file *m)
9013*4882a593Smuzhiyun {
9014*4882a593Smuzhiyun 	int rc;
9015*4882a593Smuzhiyun 	u8 status;
9016*4882a593Smuzhiyun 	unsigned int speed = 0;
9017*4882a593Smuzhiyun 
9018*4882a593Smuzhiyun 	switch (fan_status_access_mode) {
9019*4882a593Smuzhiyun 	case TPACPI_FAN_RD_ACPI_GFAN:
9020*4882a593Smuzhiyun 		/* 570, 600e/x, 770e, 770x */
9021*4882a593Smuzhiyun 		rc = fan_get_status_safe(&status);
9022*4882a593Smuzhiyun 		if (rc < 0)
9023*4882a593Smuzhiyun 			return rc;
9024*4882a593Smuzhiyun 
9025*4882a593Smuzhiyun 		seq_printf(m, "status:\t\t%s\n"
9026*4882a593Smuzhiyun 			       "level:\t\t%d\n",
9027*4882a593Smuzhiyun 			       (status != 0) ? "enabled" : "disabled", status);
9028*4882a593Smuzhiyun 		break;
9029*4882a593Smuzhiyun 
9030*4882a593Smuzhiyun 	case TPACPI_FAN_RD_TPEC:
9031*4882a593Smuzhiyun 		/* all except 570, 600e/x, 770e, 770x */
9032*4882a593Smuzhiyun 		rc = fan_get_status_safe(&status);
9033*4882a593Smuzhiyun 		if (rc < 0)
9034*4882a593Smuzhiyun 			return rc;
9035*4882a593Smuzhiyun 
9036*4882a593Smuzhiyun 		seq_printf(m, "status:\t\t%s\n",
9037*4882a593Smuzhiyun 			       (status != 0) ? "enabled" : "disabled");
9038*4882a593Smuzhiyun 
9039*4882a593Smuzhiyun 		rc = fan_get_speed(&speed);
9040*4882a593Smuzhiyun 		if (rc < 0)
9041*4882a593Smuzhiyun 			return rc;
9042*4882a593Smuzhiyun 
9043*4882a593Smuzhiyun 		seq_printf(m, "speed:\t\t%d\n", speed);
9044*4882a593Smuzhiyun 
9045*4882a593Smuzhiyun 		if (status & TP_EC_FAN_FULLSPEED)
9046*4882a593Smuzhiyun 			/* Disengaged mode takes precedence */
9047*4882a593Smuzhiyun 			seq_printf(m, "level:\t\tdisengaged\n");
9048*4882a593Smuzhiyun 		else if (status & TP_EC_FAN_AUTO)
9049*4882a593Smuzhiyun 			seq_printf(m, "level:\t\tauto\n");
9050*4882a593Smuzhiyun 		else
9051*4882a593Smuzhiyun 			seq_printf(m, "level:\t\t%d\n", status);
9052*4882a593Smuzhiyun 		break;
9053*4882a593Smuzhiyun 
9054*4882a593Smuzhiyun 	case TPACPI_FAN_NONE:
9055*4882a593Smuzhiyun 	default:
9056*4882a593Smuzhiyun 		seq_printf(m, "status:\t\tnot supported\n");
9057*4882a593Smuzhiyun 	}
9058*4882a593Smuzhiyun 
9059*4882a593Smuzhiyun 	if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9060*4882a593Smuzhiyun 		seq_printf(m, "commands:\tlevel <level>");
9061*4882a593Smuzhiyun 
9062*4882a593Smuzhiyun 		switch (fan_control_access_mode) {
9063*4882a593Smuzhiyun 		case TPACPI_FAN_WR_ACPI_SFAN:
9064*4882a593Smuzhiyun 			seq_printf(m, " (<level> is 0-7)\n");
9065*4882a593Smuzhiyun 			break;
9066*4882a593Smuzhiyun 
9067*4882a593Smuzhiyun 		default:
9068*4882a593Smuzhiyun 			seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9069*4882a593Smuzhiyun 			break;
9070*4882a593Smuzhiyun 		}
9071*4882a593Smuzhiyun 	}
9072*4882a593Smuzhiyun 
9073*4882a593Smuzhiyun 	if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9074*4882a593Smuzhiyun 		seq_printf(m, "commands:\tenable, disable\n"
9075*4882a593Smuzhiyun 			       "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9076*4882a593Smuzhiyun 
9077*4882a593Smuzhiyun 	if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9078*4882a593Smuzhiyun 		seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9079*4882a593Smuzhiyun 
9080*4882a593Smuzhiyun 	return 0;
9081*4882a593Smuzhiyun }
9082*4882a593Smuzhiyun 
fan_write_cmd_level(const char * cmd,int * rc)9083*4882a593Smuzhiyun static int fan_write_cmd_level(const char *cmd, int *rc)
9084*4882a593Smuzhiyun {
9085*4882a593Smuzhiyun 	int level;
9086*4882a593Smuzhiyun 
9087*4882a593Smuzhiyun 	if (strlencmp(cmd, "level auto") == 0)
9088*4882a593Smuzhiyun 		level = TP_EC_FAN_AUTO;
9089*4882a593Smuzhiyun 	else if ((strlencmp(cmd, "level disengaged") == 0) ||
9090*4882a593Smuzhiyun 			(strlencmp(cmd, "level full-speed") == 0))
9091*4882a593Smuzhiyun 		level = TP_EC_FAN_FULLSPEED;
9092*4882a593Smuzhiyun 	else if (sscanf(cmd, "level %d", &level) != 1)
9093*4882a593Smuzhiyun 		return 0;
9094*4882a593Smuzhiyun 
9095*4882a593Smuzhiyun 	*rc = fan_set_level_safe(level);
9096*4882a593Smuzhiyun 	if (*rc == -ENXIO)
9097*4882a593Smuzhiyun 		pr_err("level command accepted for unsupported access mode %d\n",
9098*4882a593Smuzhiyun 		       fan_control_access_mode);
9099*4882a593Smuzhiyun 	else if (!*rc)
9100*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs fan",
9101*4882a593Smuzhiyun 			"set level to %d\n", level);
9102*4882a593Smuzhiyun 
9103*4882a593Smuzhiyun 	return 1;
9104*4882a593Smuzhiyun }
9105*4882a593Smuzhiyun 
fan_write_cmd_enable(const char * cmd,int * rc)9106*4882a593Smuzhiyun static int fan_write_cmd_enable(const char *cmd, int *rc)
9107*4882a593Smuzhiyun {
9108*4882a593Smuzhiyun 	if (strlencmp(cmd, "enable") != 0)
9109*4882a593Smuzhiyun 		return 0;
9110*4882a593Smuzhiyun 
9111*4882a593Smuzhiyun 	*rc = fan_set_enable();
9112*4882a593Smuzhiyun 	if (*rc == -ENXIO)
9113*4882a593Smuzhiyun 		pr_err("enable command accepted for unsupported access mode %d\n",
9114*4882a593Smuzhiyun 		       fan_control_access_mode);
9115*4882a593Smuzhiyun 	else if (!*rc)
9116*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs fan", "enable\n");
9117*4882a593Smuzhiyun 
9118*4882a593Smuzhiyun 	return 1;
9119*4882a593Smuzhiyun }
9120*4882a593Smuzhiyun 
fan_write_cmd_disable(const char * cmd,int * rc)9121*4882a593Smuzhiyun static int fan_write_cmd_disable(const char *cmd, int *rc)
9122*4882a593Smuzhiyun {
9123*4882a593Smuzhiyun 	if (strlencmp(cmd, "disable") != 0)
9124*4882a593Smuzhiyun 		return 0;
9125*4882a593Smuzhiyun 
9126*4882a593Smuzhiyun 	*rc = fan_set_disable();
9127*4882a593Smuzhiyun 	if (*rc == -ENXIO)
9128*4882a593Smuzhiyun 		pr_err("disable command accepted for unsupported access mode %d\n",
9129*4882a593Smuzhiyun 		       fan_control_access_mode);
9130*4882a593Smuzhiyun 	else if (!*rc)
9131*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs fan", "disable\n");
9132*4882a593Smuzhiyun 
9133*4882a593Smuzhiyun 	return 1;
9134*4882a593Smuzhiyun }
9135*4882a593Smuzhiyun 
fan_write_cmd_speed(const char * cmd,int * rc)9136*4882a593Smuzhiyun static int fan_write_cmd_speed(const char *cmd, int *rc)
9137*4882a593Smuzhiyun {
9138*4882a593Smuzhiyun 	int speed;
9139*4882a593Smuzhiyun 
9140*4882a593Smuzhiyun 	/* TODO:
9141*4882a593Smuzhiyun 	 * Support speed <low> <medium> <high> ? */
9142*4882a593Smuzhiyun 
9143*4882a593Smuzhiyun 	if (sscanf(cmd, "speed %d", &speed) != 1)
9144*4882a593Smuzhiyun 		return 0;
9145*4882a593Smuzhiyun 
9146*4882a593Smuzhiyun 	*rc = fan_set_speed(speed);
9147*4882a593Smuzhiyun 	if (*rc == -ENXIO)
9148*4882a593Smuzhiyun 		pr_err("speed command accepted for unsupported access mode %d\n",
9149*4882a593Smuzhiyun 		       fan_control_access_mode);
9150*4882a593Smuzhiyun 	else if (!*rc)
9151*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs fan",
9152*4882a593Smuzhiyun 			"set speed to %d\n", speed);
9153*4882a593Smuzhiyun 
9154*4882a593Smuzhiyun 	return 1;
9155*4882a593Smuzhiyun }
9156*4882a593Smuzhiyun 
fan_write_cmd_watchdog(const char * cmd,int * rc)9157*4882a593Smuzhiyun static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9158*4882a593Smuzhiyun {
9159*4882a593Smuzhiyun 	int interval;
9160*4882a593Smuzhiyun 
9161*4882a593Smuzhiyun 	if (sscanf(cmd, "watchdog %d", &interval) != 1)
9162*4882a593Smuzhiyun 		return 0;
9163*4882a593Smuzhiyun 
9164*4882a593Smuzhiyun 	if (interval < 0 || interval > 120)
9165*4882a593Smuzhiyun 		*rc = -EINVAL;
9166*4882a593Smuzhiyun 	else {
9167*4882a593Smuzhiyun 		fan_watchdog_maxinterval = interval;
9168*4882a593Smuzhiyun 		tpacpi_disclose_usertask("procfs fan",
9169*4882a593Smuzhiyun 			"set watchdog timer to %d\n",
9170*4882a593Smuzhiyun 			interval);
9171*4882a593Smuzhiyun 	}
9172*4882a593Smuzhiyun 
9173*4882a593Smuzhiyun 	return 1;
9174*4882a593Smuzhiyun }
9175*4882a593Smuzhiyun 
fan_write(char * buf)9176*4882a593Smuzhiyun static int fan_write(char *buf)
9177*4882a593Smuzhiyun {
9178*4882a593Smuzhiyun 	char *cmd;
9179*4882a593Smuzhiyun 	int rc = 0;
9180*4882a593Smuzhiyun 
9181*4882a593Smuzhiyun 	while (!rc && (cmd = strsep(&buf, ","))) {
9182*4882a593Smuzhiyun 		if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9183*4882a593Smuzhiyun 		      fan_write_cmd_level(cmd, &rc)) &&
9184*4882a593Smuzhiyun 		    !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9185*4882a593Smuzhiyun 		      (fan_write_cmd_enable(cmd, &rc) ||
9186*4882a593Smuzhiyun 		       fan_write_cmd_disable(cmd, &rc) ||
9187*4882a593Smuzhiyun 		       fan_write_cmd_watchdog(cmd, &rc))) &&
9188*4882a593Smuzhiyun 		    !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9189*4882a593Smuzhiyun 		      fan_write_cmd_speed(cmd, &rc))
9190*4882a593Smuzhiyun 		    )
9191*4882a593Smuzhiyun 			rc = -EINVAL;
9192*4882a593Smuzhiyun 		else if (!rc)
9193*4882a593Smuzhiyun 			fan_watchdog_reset();
9194*4882a593Smuzhiyun 	}
9195*4882a593Smuzhiyun 
9196*4882a593Smuzhiyun 	return rc;
9197*4882a593Smuzhiyun }
9198*4882a593Smuzhiyun 
9199*4882a593Smuzhiyun static struct ibm_struct fan_driver_data = {
9200*4882a593Smuzhiyun 	.name = "fan",
9201*4882a593Smuzhiyun 	.read = fan_read,
9202*4882a593Smuzhiyun 	.write = fan_write,
9203*4882a593Smuzhiyun 	.exit = fan_exit,
9204*4882a593Smuzhiyun 	.suspend = fan_suspend,
9205*4882a593Smuzhiyun 	.resume = fan_resume,
9206*4882a593Smuzhiyun };
9207*4882a593Smuzhiyun 
9208*4882a593Smuzhiyun /*************************************************************************
9209*4882a593Smuzhiyun  * Mute LED subdriver
9210*4882a593Smuzhiyun  */
9211*4882a593Smuzhiyun 
9212*4882a593Smuzhiyun #define TPACPI_LED_MAX		2
9213*4882a593Smuzhiyun 
9214*4882a593Smuzhiyun struct tp_led_table {
9215*4882a593Smuzhiyun 	acpi_string name;
9216*4882a593Smuzhiyun 	int on_value;
9217*4882a593Smuzhiyun 	int off_value;
9218*4882a593Smuzhiyun 	int state;
9219*4882a593Smuzhiyun };
9220*4882a593Smuzhiyun 
9221*4882a593Smuzhiyun static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9222*4882a593Smuzhiyun 	[LED_AUDIO_MUTE] = {
9223*4882a593Smuzhiyun 		.name = "SSMS",
9224*4882a593Smuzhiyun 		.on_value = 1,
9225*4882a593Smuzhiyun 		.off_value = 0,
9226*4882a593Smuzhiyun 	},
9227*4882a593Smuzhiyun 	[LED_AUDIO_MICMUTE] = {
9228*4882a593Smuzhiyun 		.name = "MMTS",
9229*4882a593Smuzhiyun 		.on_value = 2,
9230*4882a593Smuzhiyun 		.off_value = 0,
9231*4882a593Smuzhiyun 	},
9232*4882a593Smuzhiyun };
9233*4882a593Smuzhiyun 
mute_led_on_off(struct tp_led_table * t,bool state)9234*4882a593Smuzhiyun static int mute_led_on_off(struct tp_led_table *t, bool state)
9235*4882a593Smuzhiyun {
9236*4882a593Smuzhiyun 	acpi_handle temp;
9237*4882a593Smuzhiyun 	int output;
9238*4882a593Smuzhiyun 
9239*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9240*4882a593Smuzhiyun 		pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9241*4882a593Smuzhiyun 		return -EIO;
9242*4882a593Smuzhiyun 	}
9243*4882a593Smuzhiyun 
9244*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9245*4882a593Smuzhiyun 			state ? t->on_value : t->off_value))
9246*4882a593Smuzhiyun 		return -EIO;
9247*4882a593Smuzhiyun 
9248*4882a593Smuzhiyun 	t->state = state;
9249*4882a593Smuzhiyun 	return state;
9250*4882a593Smuzhiyun }
9251*4882a593Smuzhiyun 
tpacpi_led_set(int whichled,bool on)9252*4882a593Smuzhiyun static int tpacpi_led_set(int whichled, bool on)
9253*4882a593Smuzhiyun {
9254*4882a593Smuzhiyun 	struct tp_led_table *t;
9255*4882a593Smuzhiyun 
9256*4882a593Smuzhiyun 	t = &led_tables[whichled];
9257*4882a593Smuzhiyun 	if (t->state < 0 || t->state == on)
9258*4882a593Smuzhiyun 		return t->state;
9259*4882a593Smuzhiyun 	return mute_led_on_off(t, on);
9260*4882a593Smuzhiyun }
9261*4882a593Smuzhiyun 
tpacpi_led_mute_set(struct led_classdev * led_cdev,enum led_brightness brightness)9262*4882a593Smuzhiyun static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9263*4882a593Smuzhiyun 			       enum led_brightness brightness)
9264*4882a593Smuzhiyun {
9265*4882a593Smuzhiyun 	return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9266*4882a593Smuzhiyun }
9267*4882a593Smuzhiyun 
tpacpi_led_micmute_set(struct led_classdev * led_cdev,enum led_brightness brightness)9268*4882a593Smuzhiyun static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9269*4882a593Smuzhiyun 				  enum led_brightness brightness)
9270*4882a593Smuzhiyun {
9271*4882a593Smuzhiyun 	return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9272*4882a593Smuzhiyun }
9273*4882a593Smuzhiyun 
9274*4882a593Smuzhiyun static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9275*4882a593Smuzhiyun 	[LED_AUDIO_MUTE] = {
9276*4882a593Smuzhiyun 		.name		= "platform::mute",
9277*4882a593Smuzhiyun 		.max_brightness = 1,
9278*4882a593Smuzhiyun 		.brightness_set_blocking = tpacpi_led_mute_set,
9279*4882a593Smuzhiyun 		.default_trigger = "audio-mute",
9280*4882a593Smuzhiyun 	},
9281*4882a593Smuzhiyun 	[LED_AUDIO_MICMUTE] = {
9282*4882a593Smuzhiyun 		.name		= "platform::micmute",
9283*4882a593Smuzhiyun 		.max_brightness = 1,
9284*4882a593Smuzhiyun 		.brightness_set_blocking = tpacpi_led_micmute_set,
9285*4882a593Smuzhiyun 		.default_trigger = "audio-micmute",
9286*4882a593Smuzhiyun 	},
9287*4882a593Smuzhiyun };
9288*4882a593Smuzhiyun 
mute_led_init(struct ibm_init_struct * iibm)9289*4882a593Smuzhiyun static int mute_led_init(struct ibm_init_struct *iibm)
9290*4882a593Smuzhiyun {
9291*4882a593Smuzhiyun 	acpi_handle temp;
9292*4882a593Smuzhiyun 	int i, err;
9293*4882a593Smuzhiyun 
9294*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_LED_MAX; i++) {
9295*4882a593Smuzhiyun 		struct tp_led_table *t = &led_tables[i];
9296*4882a593Smuzhiyun 		if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9297*4882a593Smuzhiyun 			t->state = -ENODEV;
9298*4882a593Smuzhiyun 			continue;
9299*4882a593Smuzhiyun 		}
9300*4882a593Smuzhiyun 
9301*4882a593Smuzhiyun 		mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9302*4882a593Smuzhiyun 		err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9303*4882a593Smuzhiyun 		if (err < 0) {
9304*4882a593Smuzhiyun 			while (i--)
9305*4882a593Smuzhiyun 				led_classdev_unregister(&mute_led_cdev[i]);
9306*4882a593Smuzhiyun 			return err;
9307*4882a593Smuzhiyun 		}
9308*4882a593Smuzhiyun 	}
9309*4882a593Smuzhiyun 	return 0;
9310*4882a593Smuzhiyun }
9311*4882a593Smuzhiyun 
mute_led_exit(void)9312*4882a593Smuzhiyun static void mute_led_exit(void)
9313*4882a593Smuzhiyun {
9314*4882a593Smuzhiyun 	int i;
9315*4882a593Smuzhiyun 
9316*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_LED_MAX; i++) {
9317*4882a593Smuzhiyun 		led_classdev_unregister(&mute_led_cdev[i]);
9318*4882a593Smuzhiyun 		tpacpi_led_set(i, false);
9319*4882a593Smuzhiyun 	}
9320*4882a593Smuzhiyun }
9321*4882a593Smuzhiyun 
mute_led_resume(void)9322*4882a593Smuzhiyun static void mute_led_resume(void)
9323*4882a593Smuzhiyun {
9324*4882a593Smuzhiyun 	int i;
9325*4882a593Smuzhiyun 
9326*4882a593Smuzhiyun 	for (i = 0; i < TPACPI_LED_MAX; i++) {
9327*4882a593Smuzhiyun 		struct tp_led_table *t = &led_tables[i];
9328*4882a593Smuzhiyun 		if (t->state >= 0)
9329*4882a593Smuzhiyun 			mute_led_on_off(t, t->state);
9330*4882a593Smuzhiyun 	}
9331*4882a593Smuzhiyun }
9332*4882a593Smuzhiyun 
9333*4882a593Smuzhiyun static struct ibm_struct mute_led_driver_data = {
9334*4882a593Smuzhiyun 	.name = "mute_led",
9335*4882a593Smuzhiyun 	.exit = mute_led_exit,
9336*4882a593Smuzhiyun 	.resume = mute_led_resume,
9337*4882a593Smuzhiyun };
9338*4882a593Smuzhiyun 
9339*4882a593Smuzhiyun /*
9340*4882a593Smuzhiyun  * Battery Wear Control Driver
9341*4882a593Smuzhiyun  * Contact: Ognjen Galic <smclt30p@gmail.com>
9342*4882a593Smuzhiyun  */
9343*4882a593Smuzhiyun 
9344*4882a593Smuzhiyun /* Metadata */
9345*4882a593Smuzhiyun 
9346*4882a593Smuzhiyun #define GET_START	"BCTG"
9347*4882a593Smuzhiyun #define SET_START	"BCCS"
9348*4882a593Smuzhiyun #define GET_STOP	"BCSG"
9349*4882a593Smuzhiyun #define SET_STOP	"BCSS"
9350*4882a593Smuzhiyun 
9351*4882a593Smuzhiyun enum {
9352*4882a593Smuzhiyun 	BAT_ANY = 0,
9353*4882a593Smuzhiyun 	BAT_PRIMARY = 1,
9354*4882a593Smuzhiyun 	BAT_SECONDARY = 2
9355*4882a593Smuzhiyun };
9356*4882a593Smuzhiyun 
9357*4882a593Smuzhiyun enum {
9358*4882a593Smuzhiyun 	/* Error condition bit */
9359*4882a593Smuzhiyun 	METHOD_ERR = BIT(31),
9360*4882a593Smuzhiyun };
9361*4882a593Smuzhiyun 
9362*4882a593Smuzhiyun enum {
9363*4882a593Smuzhiyun 	/* This is used in the get/set helpers */
9364*4882a593Smuzhiyun 	THRESHOLD_START,
9365*4882a593Smuzhiyun 	THRESHOLD_STOP,
9366*4882a593Smuzhiyun };
9367*4882a593Smuzhiyun 
9368*4882a593Smuzhiyun struct tpacpi_battery_data {
9369*4882a593Smuzhiyun 	int charge_start;
9370*4882a593Smuzhiyun 	int start_support;
9371*4882a593Smuzhiyun 	int charge_stop;
9372*4882a593Smuzhiyun 	int stop_support;
9373*4882a593Smuzhiyun };
9374*4882a593Smuzhiyun 
9375*4882a593Smuzhiyun struct tpacpi_battery_driver_data {
9376*4882a593Smuzhiyun 	struct tpacpi_battery_data batteries[3];
9377*4882a593Smuzhiyun 	int individual_addressing;
9378*4882a593Smuzhiyun };
9379*4882a593Smuzhiyun 
9380*4882a593Smuzhiyun static struct tpacpi_battery_driver_data battery_info;
9381*4882a593Smuzhiyun 
9382*4882a593Smuzhiyun /* ACPI helpers/functions/probes */
9383*4882a593Smuzhiyun 
9384*4882a593Smuzhiyun /**
9385*4882a593Smuzhiyun  * This evaluates a ACPI method call specific to the battery
9386*4882a593Smuzhiyun  * ACPI extension. The specifics are that an error is marked
9387*4882a593Smuzhiyun  * in the 32rd bit of the response, so we just check that here.
9388*4882a593Smuzhiyun  */
tpacpi_battery_acpi_eval(char * method,int * ret,int param)9389*4882a593Smuzhiyun static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9390*4882a593Smuzhiyun {
9391*4882a593Smuzhiyun 	int response;
9392*4882a593Smuzhiyun 
9393*4882a593Smuzhiyun 	if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9394*4882a593Smuzhiyun 		acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9395*4882a593Smuzhiyun 		return AE_ERROR;
9396*4882a593Smuzhiyun 	}
9397*4882a593Smuzhiyun 	if (response & METHOD_ERR) {
9398*4882a593Smuzhiyun 		acpi_handle_err(hkey_handle,
9399*4882a593Smuzhiyun 				"%s evaluated but flagged as error", method);
9400*4882a593Smuzhiyun 		return AE_ERROR;
9401*4882a593Smuzhiyun 	}
9402*4882a593Smuzhiyun 	*ret = response;
9403*4882a593Smuzhiyun 	return AE_OK;
9404*4882a593Smuzhiyun }
9405*4882a593Smuzhiyun 
tpacpi_battery_get(int what,int battery,int * ret)9406*4882a593Smuzhiyun static int tpacpi_battery_get(int what, int battery, int *ret)
9407*4882a593Smuzhiyun {
9408*4882a593Smuzhiyun 	switch (what) {
9409*4882a593Smuzhiyun 	case THRESHOLD_START:
9410*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9411*4882a593Smuzhiyun 			return -ENODEV;
9412*4882a593Smuzhiyun 
9413*4882a593Smuzhiyun 		/* The value is in the low 8 bits of the response */
9414*4882a593Smuzhiyun 		*ret = *ret & 0xFF;
9415*4882a593Smuzhiyun 		return 0;
9416*4882a593Smuzhiyun 	case THRESHOLD_STOP:
9417*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9418*4882a593Smuzhiyun 			return -ENODEV;
9419*4882a593Smuzhiyun 		/* Value is in lower 8 bits */
9420*4882a593Smuzhiyun 		*ret = *ret & 0xFF;
9421*4882a593Smuzhiyun 		/*
9422*4882a593Smuzhiyun 		 * On the stop value, if we return 0 that
9423*4882a593Smuzhiyun 		 * does not make any sense. 0 means Default, which
9424*4882a593Smuzhiyun 		 * means that charging stops at 100%, so we return
9425*4882a593Smuzhiyun 		 * that.
9426*4882a593Smuzhiyun 		 */
9427*4882a593Smuzhiyun 		if (*ret == 0)
9428*4882a593Smuzhiyun 			*ret = 100;
9429*4882a593Smuzhiyun 		return 0;
9430*4882a593Smuzhiyun 	default:
9431*4882a593Smuzhiyun 		pr_crit("wrong parameter: %d", what);
9432*4882a593Smuzhiyun 		return -EINVAL;
9433*4882a593Smuzhiyun 	}
9434*4882a593Smuzhiyun }
9435*4882a593Smuzhiyun 
tpacpi_battery_set(int what,int battery,int value)9436*4882a593Smuzhiyun static int tpacpi_battery_set(int what, int battery, int value)
9437*4882a593Smuzhiyun {
9438*4882a593Smuzhiyun 	int param, ret;
9439*4882a593Smuzhiyun 	/* The first 8 bits are the value of the threshold */
9440*4882a593Smuzhiyun 	param = value;
9441*4882a593Smuzhiyun 	/* The battery ID is in bits 8-9, 2 bits */
9442*4882a593Smuzhiyun 	param |= battery << 8;
9443*4882a593Smuzhiyun 
9444*4882a593Smuzhiyun 	switch (what) {
9445*4882a593Smuzhiyun 	case THRESHOLD_START:
9446*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9447*4882a593Smuzhiyun 			pr_err("failed to set charge threshold on battery %d",
9448*4882a593Smuzhiyun 					battery);
9449*4882a593Smuzhiyun 			return -ENODEV;
9450*4882a593Smuzhiyun 		}
9451*4882a593Smuzhiyun 		return 0;
9452*4882a593Smuzhiyun 	case THRESHOLD_STOP:
9453*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9454*4882a593Smuzhiyun 			pr_err("failed to set stop threshold: %d", battery);
9455*4882a593Smuzhiyun 			return -ENODEV;
9456*4882a593Smuzhiyun 		}
9457*4882a593Smuzhiyun 		return 0;
9458*4882a593Smuzhiyun 	default:
9459*4882a593Smuzhiyun 		pr_crit("wrong parameter: %d", what);
9460*4882a593Smuzhiyun 		return -EINVAL;
9461*4882a593Smuzhiyun 	}
9462*4882a593Smuzhiyun }
9463*4882a593Smuzhiyun 
tpacpi_battery_probe(int battery)9464*4882a593Smuzhiyun static int tpacpi_battery_probe(int battery)
9465*4882a593Smuzhiyun {
9466*4882a593Smuzhiyun 	int ret = 0;
9467*4882a593Smuzhiyun 
9468*4882a593Smuzhiyun 	memset(&battery_info.batteries[battery], 0,
9469*4882a593Smuzhiyun 		sizeof(battery_info.batteries[battery]));
9470*4882a593Smuzhiyun 
9471*4882a593Smuzhiyun 	/*
9472*4882a593Smuzhiyun 	 * 1) Get the current start threshold
9473*4882a593Smuzhiyun 	 * 2) Check for support
9474*4882a593Smuzhiyun 	 * 3) Get the current stop threshold
9475*4882a593Smuzhiyun 	 * 4) Check for support
9476*4882a593Smuzhiyun 	 */
9477*4882a593Smuzhiyun 	if (acpi_has_method(hkey_handle, GET_START)) {
9478*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9479*4882a593Smuzhiyun 			pr_err("Error probing battery %d\n", battery);
9480*4882a593Smuzhiyun 			return -ENODEV;
9481*4882a593Smuzhiyun 		}
9482*4882a593Smuzhiyun 		/* Individual addressing is in bit 9 */
9483*4882a593Smuzhiyun 		if (ret & BIT(9))
9484*4882a593Smuzhiyun 			battery_info.individual_addressing = true;
9485*4882a593Smuzhiyun 		/* Support is marked in bit 8 */
9486*4882a593Smuzhiyun 		if (ret & BIT(8))
9487*4882a593Smuzhiyun 			battery_info.batteries[battery].start_support = 1;
9488*4882a593Smuzhiyun 		else
9489*4882a593Smuzhiyun 			return -ENODEV;
9490*4882a593Smuzhiyun 		if (tpacpi_battery_get(THRESHOLD_START, battery,
9491*4882a593Smuzhiyun 			&battery_info.batteries[battery].charge_start)) {
9492*4882a593Smuzhiyun 			pr_err("Error probing battery %d\n", battery);
9493*4882a593Smuzhiyun 			return -ENODEV;
9494*4882a593Smuzhiyun 		}
9495*4882a593Smuzhiyun 	}
9496*4882a593Smuzhiyun 	if (acpi_has_method(hkey_handle, GET_STOP)) {
9497*4882a593Smuzhiyun 		if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9498*4882a593Smuzhiyun 			pr_err("Error probing battery stop; %d\n", battery);
9499*4882a593Smuzhiyun 			return -ENODEV;
9500*4882a593Smuzhiyun 		}
9501*4882a593Smuzhiyun 		/* Support is marked in bit 8 */
9502*4882a593Smuzhiyun 		if (ret & BIT(8))
9503*4882a593Smuzhiyun 			battery_info.batteries[battery].stop_support = 1;
9504*4882a593Smuzhiyun 		else
9505*4882a593Smuzhiyun 			return -ENODEV;
9506*4882a593Smuzhiyun 		if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9507*4882a593Smuzhiyun 			&battery_info.batteries[battery].charge_stop)) {
9508*4882a593Smuzhiyun 			pr_err("Error probing battery stop: %d\n", battery);
9509*4882a593Smuzhiyun 			return -ENODEV;
9510*4882a593Smuzhiyun 		}
9511*4882a593Smuzhiyun 	}
9512*4882a593Smuzhiyun 	pr_info("battery %d registered (start %d, stop %d)",
9513*4882a593Smuzhiyun 			battery,
9514*4882a593Smuzhiyun 			battery_info.batteries[battery].charge_start,
9515*4882a593Smuzhiyun 			battery_info.batteries[battery].charge_stop);
9516*4882a593Smuzhiyun 
9517*4882a593Smuzhiyun 	return 0;
9518*4882a593Smuzhiyun }
9519*4882a593Smuzhiyun 
9520*4882a593Smuzhiyun /* General helper functions */
9521*4882a593Smuzhiyun 
tpacpi_battery_get_id(const char * battery_name)9522*4882a593Smuzhiyun static int tpacpi_battery_get_id(const char *battery_name)
9523*4882a593Smuzhiyun {
9524*4882a593Smuzhiyun 
9525*4882a593Smuzhiyun 	if (strcmp(battery_name, "BAT0") == 0 ||
9526*4882a593Smuzhiyun 	    tp_features.battery_force_primary)
9527*4882a593Smuzhiyun 		return BAT_PRIMARY;
9528*4882a593Smuzhiyun 	if (strcmp(battery_name, "BAT1") == 0)
9529*4882a593Smuzhiyun 		return BAT_SECONDARY;
9530*4882a593Smuzhiyun 	/*
9531*4882a593Smuzhiyun 	 * If for some reason the battery is not BAT0 nor is it
9532*4882a593Smuzhiyun 	 * BAT1, we will assume it's the default, first battery,
9533*4882a593Smuzhiyun 	 * AKA primary.
9534*4882a593Smuzhiyun 	 */
9535*4882a593Smuzhiyun 	pr_warn("unknown battery %s, assuming primary", battery_name);
9536*4882a593Smuzhiyun 	return BAT_PRIMARY;
9537*4882a593Smuzhiyun }
9538*4882a593Smuzhiyun 
9539*4882a593Smuzhiyun /* sysfs interface */
9540*4882a593Smuzhiyun 
tpacpi_battery_store(int what,struct device * dev,const char * buf,size_t count)9541*4882a593Smuzhiyun static ssize_t tpacpi_battery_store(int what,
9542*4882a593Smuzhiyun 				    struct device *dev,
9543*4882a593Smuzhiyun 				    const char *buf, size_t count)
9544*4882a593Smuzhiyun {
9545*4882a593Smuzhiyun 	struct power_supply *supply = to_power_supply(dev);
9546*4882a593Smuzhiyun 	unsigned long value;
9547*4882a593Smuzhiyun 	int battery, rval;
9548*4882a593Smuzhiyun 	/*
9549*4882a593Smuzhiyun 	 * Some systems have support for more than
9550*4882a593Smuzhiyun 	 * one battery. If that is the case,
9551*4882a593Smuzhiyun 	 * tpacpi_battery_probe marked that addressing
9552*4882a593Smuzhiyun 	 * them individually is supported, so we do that
9553*4882a593Smuzhiyun 	 * based on the device struct.
9554*4882a593Smuzhiyun 	 *
9555*4882a593Smuzhiyun 	 * On systems that are not supported, we assume
9556*4882a593Smuzhiyun 	 * the primary as most of the ACPI calls fail
9557*4882a593Smuzhiyun 	 * with "Any Battery" as the parameter.
9558*4882a593Smuzhiyun 	 */
9559*4882a593Smuzhiyun 	if (battery_info.individual_addressing)
9560*4882a593Smuzhiyun 		/* BAT_PRIMARY or BAT_SECONDARY */
9561*4882a593Smuzhiyun 		battery = tpacpi_battery_get_id(supply->desc->name);
9562*4882a593Smuzhiyun 	else
9563*4882a593Smuzhiyun 		battery = BAT_PRIMARY;
9564*4882a593Smuzhiyun 
9565*4882a593Smuzhiyun 	rval = kstrtoul(buf, 10, &value);
9566*4882a593Smuzhiyun 	if (rval)
9567*4882a593Smuzhiyun 		return rval;
9568*4882a593Smuzhiyun 
9569*4882a593Smuzhiyun 	switch (what) {
9570*4882a593Smuzhiyun 	case THRESHOLD_START:
9571*4882a593Smuzhiyun 		if (!battery_info.batteries[battery].start_support)
9572*4882a593Smuzhiyun 			return -ENODEV;
9573*4882a593Smuzhiyun 		/* valid values are [0, 99] */
9574*4882a593Smuzhiyun 		if (value > 99)
9575*4882a593Smuzhiyun 			return -EINVAL;
9576*4882a593Smuzhiyun 		if (value > battery_info.batteries[battery].charge_stop)
9577*4882a593Smuzhiyun 			return -EINVAL;
9578*4882a593Smuzhiyun 		if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9579*4882a593Smuzhiyun 			return -ENODEV;
9580*4882a593Smuzhiyun 		battery_info.batteries[battery].charge_start = value;
9581*4882a593Smuzhiyun 		return count;
9582*4882a593Smuzhiyun 
9583*4882a593Smuzhiyun 	case THRESHOLD_STOP:
9584*4882a593Smuzhiyun 		if (!battery_info.batteries[battery].stop_support)
9585*4882a593Smuzhiyun 			return -ENODEV;
9586*4882a593Smuzhiyun 		/* valid values are [1, 100] */
9587*4882a593Smuzhiyun 		if (value < 1 || value > 100)
9588*4882a593Smuzhiyun 			return -EINVAL;
9589*4882a593Smuzhiyun 		if (value < battery_info.batteries[battery].charge_start)
9590*4882a593Smuzhiyun 			return -EINVAL;
9591*4882a593Smuzhiyun 		battery_info.batteries[battery].charge_stop = value;
9592*4882a593Smuzhiyun 		/*
9593*4882a593Smuzhiyun 		 * When 100 is passed to stop, we need to flip
9594*4882a593Smuzhiyun 		 * it to 0 as that the EC understands that as
9595*4882a593Smuzhiyun 		 * "Default", which will charge to 100%
9596*4882a593Smuzhiyun 		 */
9597*4882a593Smuzhiyun 		if (value == 100)
9598*4882a593Smuzhiyun 			value = 0;
9599*4882a593Smuzhiyun 		if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9600*4882a593Smuzhiyun 			return -EINVAL;
9601*4882a593Smuzhiyun 		return count;
9602*4882a593Smuzhiyun 	default:
9603*4882a593Smuzhiyun 		pr_crit("Wrong parameter: %d", what);
9604*4882a593Smuzhiyun 		return -EINVAL;
9605*4882a593Smuzhiyun 	}
9606*4882a593Smuzhiyun 	return count;
9607*4882a593Smuzhiyun }
9608*4882a593Smuzhiyun 
tpacpi_battery_show(int what,struct device * dev,char * buf)9609*4882a593Smuzhiyun static ssize_t tpacpi_battery_show(int what,
9610*4882a593Smuzhiyun 				   struct device *dev,
9611*4882a593Smuzhiyun 				   char *buf)
9612*4882a593Smuzhiyun {
9613*4882a593Smuzhiyun 	struct power_supply *supply = to_power_supply(dev);
9614*4882a593Smuzhiyun 	int ret, battery;
9615*4882a593Smuzhiyun 	/*
9616*4882a593Smuzhiyun 	 * Some systems have support for more than
9617*4882a593Smuzhiyun 	 * one battery. If that is the case,
9618*4882a593Smuzhiyun 	 * tpacpi_battery_probe marked that addressing
9619*4882a593Smuzhiyun 	 * them individually is supported, so we;
9620*4882a593Smuzhiyun 	 * based on the device struct.
9621*4882a593Smuzhiyun 	 *
9622*4882a593Smuzhiyun 	 * On systems that are not supported, we assume
9623*4882a593Smuzhiyun 	 * the primary as most of the ACPI calls fail
9624*4882a593Smuzhiyun 	 * with "Any Battery" as the parameter.
9625*4882a593Smuzhiyun 	 */
9626*4882a593Smuzhiyun 	if (battery_info.individual_addressing)
9627*4882a593Smuzhiyun 		/* BAT_PRIMARY or BAT_SECONDARY */
9628*4882a593Smuzhiyun 		battery = tpacpi_battery_get_id(supply->desc->name);
9629*4882a593Smuzhiyun 	else
9630*4882a593Smuzhiyun 		battery = BAT_PRIMARY;
9631*4882a593Smuzhiyun 	if (tpacpi_battery_get(what, battery, &ret))
9632*4882a593Smuzhiyun 		return -ENODEV;
9633*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", ret);
9634*4882a593Smuzhiyun }
9635*4882a593Smuzhiyun 
charge_control_start_threshold_show(struct device * device,struct device_attribute * attr,char * buf)9636*4882a593Smuzhiyun static ssize_t charge_control_start_threshold_show(struct device *device,
9637*4882a593Smuzhiyun 				struct device_attribute *attr,
9638*4882a593Smuzhiyun 				char *buf)
9639*4882a593Smuzhiyun {
9640*4882a593Smuzhiyun 	return tpacpi_battery_show(THRESHOLD_START, device, buf);
9641*4882a593Smuzhiyun }
9642*4882a593Smuzhiyun 
charge_control_end_threshold_show(struct device * device,struct device_attribute * attr,char * buf)9643*4882a593Smuzhiyun static ssize_t charge_control_end_threshold_show(struct device *device,
9644*4882a593Smuzhiyun 				struct device_attribute *attr,
9645*4882a593Smuzhiyun 				char *buf)
9646*4882a593Smuzhiyun {
9647*4882a593Smuzhiyun 	return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9648*4882a593Smuzhiyun }
9649*4882a593Smuzhiyun 
charge_control_start_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9650*4882a593Smuzhiyun static ssize_t charge_control_start_threshold_store(struct device *dev,
9651*4882a593Smuzhiyun 				struct device_attribute *attr,
9652*4882a593Smuzhiyun 				const char *buf, size_t count)
9653*4882a593Smuzhiyun {
9654*4882a593Smuzhiyun 	return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9655*4882a593Smuzhiyun }
9656*4882a593Smuzhiyun 
charge_control_end_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9657*4882a593Smuzhiyun static ssize_t charge_control_end_threshold_store(struct device *dev,
9658*4882a593Smuzhiyun 				struct device_attribute *attr,
9659*4882a593Smuzhiyun 				const char *buf, size_t count)
9660*4882a593Smuzhiyun {
9661*4882a593Smuzhiyun 	return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9662*4882a593Smuzhiyun }
9663*4882a593Smuzhiyun 
9664*4882a593Smuzhiyun static DEVICE_ATTR_RW(charge_control_start_threshold);
9665*4882a593Smuzhiyun static DEVICE_ATTR_RW(charge_control_end_threshold);
9666*4882a593Smuzhiyun static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9667*4882a593Smuzhiyun 	charge_start_threshold,
9668*4882a593Smuzhiyun 	0644,
9669*4882a593Smuzhiyun 	charge_control_start_threshold_show,
9670*4882a593Smuzhiyun 	charge_control_start_threshold_store
9671*4882a593Smuzhiyun );
9672*4882a593Smuzhiyun static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9673*4882a593Smuzhiyun 	charge_stop_threshold,
9674*4882a593Smuzhiyun 	0644,
9675*4882a593Smuzhiyun 	charge_control_end_threshold_show,
9676*4882a593Smuzhiyun 	charge_control_end_threshold_store
9677*4882a593Smuzhiyun );
9678*4882a593Smuzhiyun 
9679*4882a593Smuzhiyun static struct attribute *tpacpi_battery_attrs[] = {
9680*4882a593Smuzhiyun 	&dev_attr_charge_control_start_threshold.attr,
9681*4882a593Smuzhiyun 	&dev_attr_charge_control_end_threshold.attr,
9682*4882a593Smuzhiyun 	&dev_attr_charge_start_threshold.attr,
9683*4882a593Smuzhiyun 	&dev_attr_charge_stop_threshold.attr,
9684*4882a593Smuzhiyun 	NULL,
9685*4882a593Smuzhiyun };
9686*4882a593Smuzhiyun 
9687*4882a593Smuzhiyun ATTRIBUTE_GROUPS(tpacpi_battery);
9688*4882a593Smuzhiyun 
9689*4882a593Smuzhiyun /* ACPI battery hooking */
9690*4882a593Smuzhiyun 
tpacpi_battery_add(struct power_supply * battery)9691*4882a593Smuzhiyun static int tpacpi_battery_add(struct power_supply *battery)
9692*4882a593Smuzhiyun {
9693*4882a593Smuzhiyun 	int batteryid = tpacpi_battery_get_id(battery->desc->name);
9694*4882a593Smuzhiyun 
9695*4882a593Smuzhiyun 	if (tpacpi_battery_probe(batteryid))
9696*4882a593Smuzhiyun 		return -ENODEV;
9697*4882a593Smuzhiyun 	if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9698*4882a593Smuzhiyun 		return -ENODEV;
9699*4882a593Smuzhiyun 	return 0;
9700*4882a593Smuzhiyun }
9701*4882a593Smuzhiyun 
tpacpi_battery_remove(struct power_supply * battery)9702*4882a593Smuzhiyun static int tpacpi_battery_remove(struct power_supply *battery)
9703*4882a593Smuzhiyun {
9704*4882a593Smuzhiyun 	device_remove_groups(&battery->dev, tpacpi_battery_groups);
9705*4882a593Smuzhiyun 	return 0;
9706*4882a593Smuzhiyun }
9707*4882a593Smuzhiyun 
9708*4882a593Smuzhiyun static struct acpi_battery_hook battery_hook = {
9709*4882a593Smuzhiyun 	.add_battery = tpacpi_battery_add,
9710*4882a593Smuzhiyun 	.remove_battery = tpacpi_battery_remove,
9711*4882a593Smuzhiyun 	.name = "ThinkPad Battery Extension",
9712*4882a593Smuzhiyun };
9713*4882a593Smuzhiyun 
9714*4882a593Smuzhiyun /* Subdriver init/exit */
9715*4882a593Smuzhiyun 
9716*4882a593Smuzhiyun static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9717*4882a593Smuzhiyun 	/*
9718*4882a593Smuzhiyun 	 * Individual addressing is broken on models that expose the
9719*4882a593Smuzhiyun 	 * primary battery as BAT1.
9720*4882a593Smuzhiyun 	 */
9721*4882a593Smuzhiyun 	TPACPI_Q_LNV('J', '7', true),       /* B5400 */
9722*4882a593Smuzhiyun 	TPACPI_Q_LNV('J', 'I', true),       /* Thinkpad 11e */
9723*4882a593Smuzhiyun 	TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9724*4882a593Smuzhiyun 	TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9725*4882a593Smuzhiyun 	TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9726*4882a593Smuzhiyun 	TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9727*4882a593Smuzhiyun };
9728*4882a593Smuzhiyun 
tpacpi_battery_init(struct ibm_init_struct * ibm)9729*4882a593Smuzhiyun static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9730*4882a593Smuzhiyun {
9731*4882a593Smuzhiyun 	memset(&battery_info, 0, sizeof(battery_info));
9732*4882a593Smuzhiyun 
9733*4882a593Smuzhiyun 	tp_features.battery_force_primary = tpacpi_check_quirks(
9734*4882a593Smuzhiyun 					battery_quirk_table,
9735*4882a593Smuzhiyun 					ARRAY_SIZE(battery_quirk_table));
9736*4882a593Smuzhiyun 
9737*4882a593Smuzhiyun 	battery_hook_register(&battery_hook);
9738*4882a593Smuzhiyun 	return 0;
9739*4882a593Smuzhiyun }
9740*4882a593Smuzhiyun 
tpacpi_battery_exit(void)9741*4882a593Smuzhiyun static void tpacpi_battery_exit(void)
9742*4882a593Smuzhiyun {
9743*4882a593Smuzhiyun 	battery_hook_unregister(&battery_hook);
9744*4882a593Smuzhiyun }
9745*4882a593Smuzhiyun 
9746*4882a593Smuzhiyun static struct ibm_struct battery_driver_data = {
9747*4882a593Smuzhiyun 	.name = "battery",
9748*4882a593Smuzhiyun 	.exit = tpacpi_battery_exit,
9749*4882a593Smuzhiyun };
9750*4882a593Smuzhiyun 
9751*4882a593Smuzhiyun /*************************************************************************
9752*4882a593Smuzhiyun  * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9753*4882a593Smuzhiyun  */
9754*4882a593Smuzhiyun 
9755*4882a593Smuzhiyun static int lcdshadow_state;
9756*4882a593Smuzhiyun 
lcdshadow_on_off(bool state)9757*4882a593Smuzhiyun static int lcdshadow_on_off(bool state)
9758*4882a593Smuzhiyun {
9759*4882a593Smuzhiyun 	acpi_handle set_shadow_handle;
9760*4882a593Smuzhiyun 	int output;
9761*4882a593Smuzhiyun 
9762*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSSS", &set_shadow_handle))) {
9763*4882a593Smuzhiyun 		pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9764*4882a593Smuzhiyun 		return -EIO;
9765*4882a593Smuzhiyun 	}
9766*4882a593Smuzhiyun 
9767*4882a593Smuzhiyun 	if (!acpi_evalf(set_shadow_handle, &output, NULL, "dd", (int)state))
9768*4882a593Smuzhiyun 		return -EIO;
9769*4882a593Smuzhiyun 
9770*4882a593Smuzhiyun 	lcdshadow_state = state;
9771*4882a593Smuzhiyun 	return 0;
9772*4882a593Smuzhiyun }
9773*4882a593Smuzhiyun 
lcdshadow_set(bool on)9774*4882a593Smuzhiyun static int lcdshadow_set(bool on)
9775*4882a593Smuzhiyun {
9776*4882a593Smuzhiyun 	if (lcdshadow_state < 0)
9777*4882a593Smuzhiyun 		return lcdshadow_state;
9778*4882a593Smuzhiyun 	if (lcdshadow_state == on)
9779*4882a593Smuzhiyun 		return 0;
9780*4882a593Smuzhiyun 	return lcdshadow_on_off(on);
9781*4882a593Smuzhiyun }
9782*4882a593Smuzhiyun 
tpacpi_lcdshadow_init(struct ibm_init_struct * iibm)9783*4882a593Smuzhiyun static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9784*4882a593Smuzhiyun {
9785*4882a593Smuzhiyun 	acpi_handle get_shadow_handle;
9786*4882a593Smuzhiyun 	int output;
9787*4882a593Smuzhiyun 
9788*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSSS", &get_shadow_handle))) {
9789*4882a593Smuzhiyun 		lcdshadow_state = -ENODEV;
9790*4882a593Smuzhiyun 		return 0;
9791*4882a593Smuzhiyun 	}
9792*4882a593Smuzhiyun 
9793*4882a593Smuzhiyun 	if (!acpi_evalf(get_shadow_handle, &output, NULL, "dd", 0)) {
9794*4882a593Smuzhiyun 		lcdshadow_state = -EIO;
9795*4882a593Smuzhiyun 		return -EIO;
9796*4882a593Smuzhiyun 	}
9797*4882a593Smuzhiyun 	if (!(output & 0x10000)) {
9798*4882a593Smuzhiyun 		lcdshadow_state = -ENODEV;
9799*4882a593Smuzhiyun 		return 0;
9800*4882a593Smuzhiyun 	}
9801*4882a593Smuzhiyun 	lcdshadow_state = output & 0x1;
9802*4882a593Smuzhiyun 
9803*4882a593Smuzhiyun 	return 0;
9804*4882a593Smuzhiyun }
9805*4882a593Smuzhiyun 
lcdshadow_resume(void)9806*4882a593Smuzhiyun static void lcdshadow_resume(void)
9807*4882a593Smuzhiyun {
9808*4882a593Smuzhiyun 	if (lcdshadow_state >= 0)
9809*4882a593Smuzhiyun 		lcdshadow_on_off(lcdshadow_state);
9810*4882a593Smuzhiyun }
9811*4882a593Smuzhiyun 
lcdshadow_read(struct seq_file * m)9812*4882a593Smuzhiyun static int lcdshadow_read(struct seq_file *m)
9813*4882a593Smuzhiyun {
9814*4882a593Smuzhiyun 	if (lcdshadow_state < 0) {
9815*4882a593Smuzhiyun 		seq_puts(m, "status:\t\tnot supported\n");
9816*4882a593Smuzhiyun 	} else {
9817*4882a593Smuzhiyun 		seq_printf(m, "status:\t\t%d\n", lcdshadow_state);
9818*4882a593Smuzhiyun 		seq_puts(m, "commands:\t0, 1\n");
9819*4882a593Smuzhiyun 	}
9820*4882a593Smuzhiyun 
9821*4882a593Smuzhiyun 	return 0;
9822*4882a593Smuzhiyun }
9823*4882a593Smuzhiyun 
lcdshadow_write(char * buf)9824*4882a593Smuzhiyun static int lcdshadow_write(char *buf)
9825*4882a593Smuzhiyun {
9826*4882a593Smuzhiyun 	char *cmd;
9827*4882a593Smuzhiyun 	int res, state = -EINVAL;
9828*4882a593Smuzhiyun 
9829*4882a593Smuzhiyun 	if (lcdshadow_state < 0)
9830*4882a593Smuzhiyun 		return -ENODEV;
9831*4882a593Smuzhiyun 
9832*4882a593Smuzhiyun 	while ((cmd = strsep(&buf, ","))) {
9833*4882a593Smuzhiyun 		res = kstrtoint(cmd, 10, &state);
9834*4882a593Smuzhiyun 		if (res < 0)
9835*4882a593Smuzhiyun 			return res;
9836*4882a593Smuzhiyun 	}
9837*4882a593Smuzhiyun 
9838*4882a593Smuzhiyun 	if (state >= 2 || state < 0)
9839*4882a593Smuzhiyun 		return -EINVAL;
9840*4882a593Smuzhiyun 
9841*4882a593Smuzhiyun 	return lcdshadow_set(state);
9842*4882a593Smuzhiyun }
9843*4882a593Smuzhiyun 
9844*4882a593Smuzhiyun static struct ibm_struct lcdshadow_driver_data = {
9845*4882a593Smuzhiyun 	.name = "lcdshadow",
9846*4882a593Smuzhiyun 	.resume = lcdshadow_resume,
9847*4882a593Smuzhiyun 	.read = lcdshadow_read,
9848*4882a593Smuzhiyun 	.write = lcdshadow_write,
9849*4882a593Smuzhiyun };
9850*4882a593Smuzhiyun 
9851*4882a593Smuzhiyun /*************************************************************************
9852*4882a593Smuzhiyun  * DYTC subdriver, for the Lenovo lapmode feature
9853*4882a593Smuzhiyun  */
9854*4882a593Smuzhiyun 
9855*4882a593Smuzhiyun #define DYTC_CMD_GET          2 /* To get current IC function and mode */
9856*4882a593Smuzhiyun #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9857*4882a593Smuzhiyun 
9858*4882a593Smuzhiyun static bool dytc_lapmode;
9859*4882a593Smuzhiyun 
dytc_lapmode_notify_change(void)9860*4882a593Smuzhiyun static void dytc_lapmode_notify_change(void)
9861*4882a593Smuzhiyun {
9862*4882a593Smuzhiyun 	sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
9863*4882a593Smuzhiyun }
9864*4882a593Smuzhiyun 
dytc_command(int command,int * output)9865*4882a593Smuzhiyun static int dytc_command(int command, int *output)
9866*4882a593Smuzhiyun {
9867*4882a593Smuzhiyun 	acpi_handle dytc_handle;
9868*4882a593Smuzhiyun 
9869*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9870*4882a593Smuzhiyun 		/* Platform doesn't support DYTC */
9871*4882a593Smuzhiyun 		return -ENODEV;
9872*4882a593Smuzhiyun 	}
9873*4882a593Smuzhiyun 	if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9874*4882a593Smuzhiyun 		return -EIO;
9875*4882a593Smuzhiyun 	return 0;
9876*4882a593Smuzhiyun }
9877*4882a593Smuzhiyun 
dytc_lapmode_get(bool * state)9878*4882a593Smuzhiyun static int dytc_lapmode_get(bool *state)
9879*4882a593Smuzhiyun {
9880*4882a593Smuzhiyun 	int output, err;
9881*4882a593Smuzhiyun 
9882*4882a593Smuzhiyun 	err = dytc_command(DYTC_CMD_GET, &output);
9883*4882a593Smuzhiyun 	if (err)
9884*4882a593Smuzhiyun 		return err;
9885*4882a593Smuzhiyun 	*state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
9886*4882a593Smuzhiyun 	return 0;
9887*4882a593Smuzhiyun }
9888*4882a593Smuzhiyun 
dytc_lapmode_refresh(void)9889*4882a593Smuzhiyun static void dytc_lapmode_refresh(void)
9890*4882a593Smuzhiyun {
9891*4882a593Smuzhiyun 	bool new_state;
9892*4882a593Smuzhiyun 	int err;
9893*4882a593Smuzhiyun 
9894*4882a593Smuzhiyun 	err = dytc_lapmode_get(&new_state);
9895*4882a593Smuzhiyun 	if (err || (new_state == dytc_lapmode))
9896*4882a593Smuzhiyun 		return;
9897*4882a593Smuzhiyun 
9898*4882a593Smuzhiyun 	dytc_lapmode = new_state;
9899*4882a593Smuzhiyun 	dytc_lapmode_notify_change();
9900*4882a593Smuzhiyun }
9901*4882a593Smuzhiyun 
9902*4882a593Smuzhiyun /* sysfs lapmode entry */
dytc_lapmode_show(struct device * dev,struct device_attribute * attr,char * buf)9903*4882a593Smuzhiyun static ssize_t dytc_lapmode_show(struct device *dev,
9904*4882a593Smuzhiyun 					struct device_attribute *attr,
9905*4882a593Smuzhiyun 					char *buf)
9906*4882a593Smuzhiyun {
9907*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", dytc_lapmode);
9908*4882a593Smuzhiyun }
9909*4882a593Smuzhiyun 
9910*4882a593Smuzhiyun static DEVICE_ATTR_RO(dytc_lapmode);
9911*4882a593Smuzhiyun 
9912*4882a593Smuzhiyun static struct attribute *dytc_attributes[] = {
9913*4882a593Smuzhiyun 	&dev_attr_dytc_lapmode.attr,
9914*4882a593Smuzhiyun 	NULL,
9915*4882a593Smuzhiyun };
9916*4882a593Smuzhiyun 
9917*4882a593Smuzhiyun static const struct attribute_group dytc_attr_group = {
9918*4882a593Smuzhiyun 	.attrs = dytc_attributes,
9919*4882a593Smuzhiyun };
9920*4882a593Smuzhiyun 
tpacpi_dytc_init(struct ibm_init_struct * iibm)9921*4882a593Smuzhiyun static int tpacpi_dytc_init(struct ibm_init_struct *iibm)
9922*4882a593Smuzhiyun {
9923*4882a593Smuzhiyun 	int err;
9924*4882a593Smuzhiyun 
9925*4882a593Smuzhiyun 	err = dytc_lapmode_get(&dytc_lapmode);
9926*4882a593Smuzhiyun 	/* If support isn't available (ENODEV) then don't return an error
9927*4882a593Smuzhiyun 	 * but just don't create the sysfs group
9928*4882a593Smuzhiyun 	 */
9929*4882a593Smuzhiyun 	if (err == -ENODEV)
9930*4882a593Smuzhiyun 		return 0;
9931*4882a593Smuzhiyun 	/* For all other errors we can flag the failure */
9932*4882a593Smuzhiyun 	if (err)
9933*4882a593Smuzhiyun 		return err;
9934*4882a593Smuzhiyun 
9935*4882a593Smuzhiyun 	/* Platform supports this feature - create the group */
9936*4882a593Smuzhiyun 	err = sysfs_create_group(&tpacpi_pdev->dev.kobj, &dytc_attr_group);
9937*4882a593Smuzhiyun 	return err;
9938*4882a593Smuzhiyun }
9939*4882a593Smuzhiyun 
dytc_exit(void)9940*4882a593Smuzhiyun static void dytc_exit(void)
9941*4882a593Smuzhiyun {
9942*4882a593Smuzhiyun 	sysfs_remove_group(&tpacpi_pdev->dev.kobj, &dytc_attr_group);
9943*4882a593Smuzhiyun }
9944*4882a593Smuzhiyun 
9945*4882a593Smuzhiyun static struct ibm_struct dytc_driver_data = {
9946*4882a593Smuzhiyun 	.name = "dytc",
9947*4882a593Smuzhiyun 	.exit = dytc_exit,
9948*4882a593Smuzhiyun };
9949*4882a593Smuzhiyun 
9950*4882a593Smuzhiyun /****************************************************************************
9951*4882a593Smuzhiyun  ****************************************************************************
9952*4882a593Smuzhiyun  *
9953*4882a593Smuzhiyun  * Infrastructure
9954*4882a593Smuzhiyun  *
9955*4882a593Smuzhiyun  ****************************************************************************
9956*4882a593Smuzhiyun  ****************************************************************************/
9957*4882a593Smuzhiyun 
9958*4882a593Smuzhiyun /*
9959*4882a593Smuzhiyun  * HKEY event callout for other subdrivers go here
9960*4882a593Smuzhiyun  * (yes, it is ugly, but it is quick, safe, and gets the job done
9961*4882a593Smuzhiyun  */
tpacpi_driver_event(const unsigned int hkey_event)9962*4882a593Smuzhiyun static void tpacpi_driver_event(const unsigned int hkey_event)
9963*4882a593Smuzhiyun {
9964*4882a593Smuzhiyun 	if (ibm_backlight_device) {
9965*4882a593Smuzhiyun 		switch (hkey_event) {
9966*4882a593Smuzhiyun 		case TP_HKEY_EV_BRGHT_UP:
9967*4882a593Smuzhiyun 		case TP_HKEY_EV_BRGHT_DOWN:
9968*4882a593Smuzhiyun 			tpacpi_brightness_notify_change();
9969*4882a593Smuzhiyun 		}
9970*4882a593Smuzhiyun 	}
9971*4882a593Smuzhiyun 	if (alsa_card) {
9972*4882a593Smuzhiyun 		switch (hkey_event) {
9973*4882a593Smuzhiyun 		case TP_HKEY_EV_VOL_UP:
9974*4882a593Smuzhiyun 		case TP_HKEY_EV_VOL_DOWN:
9975*4882a593Smuzhiyun 		case TP_HKEY_EV_VOL_MUTE:
9976*4882a593Smuzhiyun 			volume_alsa_notify_change();
9977*4882a593Smuzhiyun 		}
9978*4882a593Smuzhiyun 	}
9979*4882a593Smuzhiyun 	if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9980*4882a593Smuzhiyun 		enum led_brightness brightness;
9981*4882a593Smuzhiyun 
9982*4882a593Smuzhiyun 		mutex_lock(&kbdlight_mutex);
9983*4882a593Smuzhiyun 
9984*4882a593Smuzhiyun 		/*
9985*4882a593Smuzhiyun 		 * Check the brightness actually changed, setting the brightness
9986*4882a593Smuzhiyun 		 * through kbdlight_set_level() also triggers this event.
9987*4882a593Smuzhiyun 		 */
9988*4882a593Smuzhiyun 		brightness = kbdlight_sysfs_get(NULL);
9989*4882a593Smuzhiyun 		if (kbdlight_brightness != brightness) {
9990*4882a593Smuzhiyun 			kbdlight_brightness = brightness;
9991*4882a593Smuzhiyun 			led_classdev_notify_brightness_hw_changed(
9992*4882a593Smuzhiyun 				&tpacpi_led_kbdlight.led_classdev, brightness);
9993*4882a593Smuzhiyun 		}
9994*4882a593Smuzhiyun 
9995*4882a593Smuzhiyun 		mutex_unlock(&kbdlight_mutex);
9996*4882a593Smuzhiyun 	}
9997*4882a593Smuzhiyun 
9998*4882a593Smuzhiyun 	if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED)
9999*4882a593Smuzhiyun 		dytc_lapmode_refresh();
10000*4882a593Smuzhiyun 
10001*4882a593Smuzhiyun }
10002*4882a593Smuzhiyun 
hotkey_driver_event(const unsigned int scancode)10003*4882a593Smuzhiyun static void hotkey_driver_event(const unsigned int scancode)
10004*4882a593Smuzhiyun {
10005*4882a593Smuzhiyun 	tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
10006*4882a593Smuzhiyun }
10007*4882a593Smuzhiyun 
10008*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
10009*4882a593Smuzhiyun 
10010*4882a593Smuzhiyun /* /proc support */
10011*4882a593Smuzhiyun static struct proc_dir_entry *proc_dir;
10012*4882a593Smuzhiyun 
10013*4882a593Smuzhiyun /*
10014*4882a593Smuzhiyun  * Module and infrastructure proble, init and exit handling
10015*4882a593Smuzhiyun  */
10016*4882a593Smuzhiyun 
10017*4882a593Smuzhiyun static bool force_load;
10018*4882a593Smuzhiyun 
10019*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUG
str_supported(int is_supported)10020*4882a593Smuzhiyun static const char * __init str_supported(int is_supported)
10021*4882a593Smuzhiyun {
10022*4882a593Smuzhiyun 	static char text_unsupported[] __initdata = "not supported";
10023*4882a593Smuzhiyun 
10024*4882a593Smuzhiyun 	return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
10025*4882a593Smuzhiyun }
10026*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10027*4882a593Smuzhiyun 
ibm_exit(struct ibm_struct * ibm)10028*4882a593Smuzhiyun static void ibm_exit(struct ibm_struct *ibm)
10029*4882a593Smuzhiyun {
10030*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10031*4882a593Smuzhiyun 
10032*4882a593Smuzhiyun 	list_del_init(&ibm->all_drivers);
10033*4882a593Smuzhiyun 
10034*4882a593Smuzhiyun 	if (ibm->flags.acpi_notify_installed) {
10035*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_EXIT,
10036*4882a593Smuzhiyun 			"%s: acpi_remove_notify_handler\n", ibm->name);
10037*4882a593Smuzhiyun 		BUG_ON(!ibm->acpi);
10038*4882a593Smuzhiyun 		acpi_remove_notify_handler(*ibm->acpi->handle,
10039*4882a593Smuzhiyun 					   ibm->acpi->type,
10040*4882a593Smuzhiyun 					   dispatch_acpi_notify);
10041*4882a593Smuzhiyun 		ibm->flags.acpi_notify_installed = 0;
10042*4882a593Smuzhiyun 	}
10043*4882a593Smuzhiyun 
10044*4882a593Smuzhiyun 	if (ibm->flags.proc_created) {
10045*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_EXIT,
10046*4882a593Smuzhiyun 			"%s: remove_proc_entry\n", ibm->name);
10047*4882a593Smuzhiyun 		remove_proc_entry(ibm->name, proc_dir);
10048*4882a593Smuzhiyun 		ibm->flags.proc_created = 0;
10049*4882a593Smuzhiyun 	}
10050*4882a593Smuzhiyun 
10051*4882a593Smuzhiyun 	if (ibm->flags.acpi_driver_registered) {
10052*4882a593Smuzhiyun 		dbg_printk(TPACPI_DBG_EXIT,
10053*4882a593Smuzhiyun 			"%s: acpi_bus_unregister_driver\n", ibm->name);
10054*4882a593Smuzhiyun 		BUG_ON(!ibm->acpi);
10055*4882a593Smuzhiyun 		acpi_bus_unregister_driver(ibm->acpi->driver);
10056*4882a593Smuzhiyun 		kfree(ibm->acpi->driver);
10057*4882a593Smuzhiyun 		ibm->acpi->driver = NULL;
10058*4882a593Smuzhiyun 		ibm->flags.acpi_driver_registered = 0;
10059*4882a593Smuzhiyun 	}
10060*4882a593Smuzhiyun 
10061*4882a593Smuzhiyun 	if (ibm->flags.init_called && ibm->exit) {
10062*4882a593Smuzhiyun 		ibm->exit();
10063*4882a593Smuzhiyun 		ibm->flags.init_called = 0;
10064*4882a593Smuzhiyun 	}
10065*4882a593Smuzhiyun 
10066*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10067*4882a593Smuzhiyun }
10068*4882a593Smuzhiyun 
ibm_init(struct ibm_init_struct * iibm)10069*4882a593Smuzhiyun static int __init ibm_init(struct ibm_init_struct *iibm)
10070*4882a593Smuzhiyun {
10071*4882a593Smuzhiyun 	int ret;
10072*4882a593Smuzhiyun 	struct ibm_struct *ibm = iibm->data;
10073*4882a593Smuzhiyun 	struct proc_dir_entry *entry;
10074*4882a593Smuzhiyun 
10075*4882a593Smuzhiyun 	BUG_ON(ibm == NULL);
10076*4882a593Smuzhiyun 
10077*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ibm->all_drivers);
10078*4882a593Smuzhiyun 
10079*4882a593Smuzhiyun 	if (ibm->flags.experimental && !experimental)
10080*4882a593Smuzhiyun 		return 0;
10081*4882a593Smuzhiyun 
10082*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT,
10083*4882a593Smuzhiyun 		"probing for %s\n", ibm->name);
10084*4882a593Smuzhiyun 
10085*4882a593Smuzhiyun 	if (iibm->init) {
10086*4882a593Smuzhiyun 		ret = iibm->init(iibm);
10087*4882a593Smuzhiyun 		if (ret > 0)
10088*4882a593Smuzhiyun 			return 0;	/* probe failed */
10089*4882a593Smuzhiyun 		if (ret)
10090*4882a593Smuzhiyun 			return ret;
10091*4882a593Smuzhiyun 
10092*4882a593Smuzhiyun 		ibm->flags.init_called = 1;
10093*4882a593Smuzhiyun 	}
10094*4882a593Smuzhiyun 
10095*4882a593Smuzhiyun 	if (ibm->acpi) {
10096*4882a593Smuzhiyun 		if (ibm->acpi->hid) {
10097*4882a593Smuzhiyun 			ret = register_tpacpi_subdriver(ibm);
10098*4882a593Smuzhiyun 			if (ret)
10099*4882a593Smuzhiyun 				goto err_out;
10100*4882a593Smuzhiyun 		}
10101*4882a593Smuzhiyun 
10102*4882a593Smuzhiyun 		if (ibm->acpi->notify) {
10103*4882a593Smuzhiyun 			ret = setup_acpi_notify(ibm);
10104*4882a593Smuzhiyun 			if (ret == -ENODEV) {
10105*4882a593Smuzhiyun 				pr_notice("disabling subdriver %s\n",
10106*4882a593Smuzhiyun 					  ibm->name);
10107*4882a593Smuzhiyun 				ret = 0;
10108*4882a593Smuzhiyun 				goto err_out;
10109*4882a593Smuzhiyun 			}
10110*4882a593Smuzhiyun 			if (ret < 0)
10111*4882a593Smuzhiyun 				goto err_out;
10112*4882a593Smuzhiyun 		}
10113*4882a593Smuzhiyun 	}
10114*4882a593Smuzhiyun 
10115*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT,
10116*4882a593Smuzhiyun 		"%s installed\n", ibm->name);
10117*4882a593Smuzhiyun 
10118*4882a593Smuzhiyun 	if (ibm->read) {
10119*4882a593Smuzhiyun 		umode_t mode = iibm->base_procfs_mode;
10120*4882a593Smuzhiyun 
10121*4882a593Smuzhiyun 		if (!mode)
10122*4882a593Smuzhiyun 			mode = S_IRUGO;
10123*4882a593Smuzhiyun 		if (ibm->write)
10124*4882a593Smuzhiyun 			mode |= S_IWUSR;
10125*4882a593Smuzhiyun 		entry = proc_create_data(ibm->name, mode, proc_dir,
10126*4882a593Smuzhiyun 					 &dispatch_proc_ops, ibm);
10127*4882a593Smuzhiyun 		if (!entry) {
10128*4882a593Smuzhiyun 			pr_err("unable to create proc entry %s\n", ibm->name);
10129*4882a593Smuzhiyun 			ret = -ENODEV;
10130*4882a593Smuzhiyun 			goto err_out;
10131*4882a593Smuzhiyun 		}
10132*4882a593Smuzhiyun 		ibm->flags.proc_created = 1;
10133*4882a593Smuzhiyun 	}
10134*4882a593Smuzhiyun 
10135*4882a593Smuzhiyun 	list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10136*4882a593Smuzhiyun 
10137*4882a593Smuzhiyun 	return 0;
10138*4882a593Smuzhiyun 
10139*4882a593Smuzhiyun err_out:
10140*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT,
10141*4882a593Smuzhiyun 		"%s: at error exit path with result %d\n",
10142*4882a593Smuzhiyun 		ibm->name, ret);
10143*4882a593Smuzhiyun 
10144*4882a593Smuzhiyun 	ibm_exit(ibm);
10145*4882a593Smuzhiyun 	return (ret < 0) ? ret : 0;
10146*4882a593Smuzhiyun }
10147*4882a593Smuzhiyun 
10148*4882a593Smuzhiyun /* Probing */
10149*4882a593Smuzhiyun 
tpacpi_parse_fw_id(const char * const s,u32 * model,u16 * release)10150*4882a593Smuzhiyun static char __init tpacpi_parse_fw_id(const char * const s,
10151*4882a593Smuzhiyun 				      u32 *model, u16 *release)
10152*4882a593Smuzhiyun {
10153*4882a593Smuzhiyun 	int i;
10154*4882a593Smuzhiyun 
10155*4882a593Smuzhiyun 	if (!s || strlen(s) < 8)
10156*4882a593Smuzhiyun 		goto invalid;
10157*4882a593Smuzhiyun 
10158*4882a593Smuzhiyun 	for (i = 0; i < 8; i++)
10159*4882a593Smuzhiyun 		if (!((s[i] >= '0' && s[i] <= '9') ||
10160*4882a593Smuzhiyun 		      (s[i] >= 'A' && s[i] <= 'Z')))
10161*4882a593Smuzhiyun 			goto invalid;
10162*4882a593Smuzhiyun 
10163*4882a593Smuzhiyun 	/*
10164*4882a593Smuzhiyun 	 * Most models: xxyTkkWW (#.##c)
10165*4882a593Smuzhiyun 	 * Ancient 570/600 and -SL lacks (#.##c)
10166*4882a593Smuzhiyun 	 */
10167*4882a593Smuzhiyun 	if (s[3] == 'T' || s[3] == 'N') {
10168*4882a593Smuzhiyun 		*model = TPID(s[0], s[1]);
10169*4882a593Smuzhiyun 		*release = TPVER(s[4], s[5]);
10170*4882a593Smuzhiyun 		return s[2];
10171*4882a593Smuzhiyun 
10172*4882a593Smuzhiyun 	/* New models: xxxyTkkW (#.##c); T550 and some others */
10173*4882a593Smuzhiyun 	} else if (s[4] == 'T' || s[4] == 'N') {
10174*4882a593Smuzhiyun 		*model = TPID3(s[0], s[1], s[2]);
10175*4882a593Smuzhiyun 		*release = TPVER(s[5], s[6]);
10176*4882a593Smuzhiyun 		return s[3];
10177*4882a593Smuzhiyun 	}
10178*4882a593Smuzhiyun 
10179*4882a593Smuzhiyun invalid:
10180*4882a593Smuzhiyun 	return '\0';
10181*4882a593Smuzhiyun }
10182*4882a593Smuzhiyun 
find_new_ec_fwstr(const struct dmi_header * dm,void * private)10183*4882a593Smuzhiyun static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10184*4882a593Smuzhiyun {
10185*4882a593Smuzhiyun 	char *ec_fw_string = (char *) private;
10186*4882a593Smuzhiyun 	const char *dmi_data = (const char *)dm;
10187*4882a593Smuzhiyun 	/*
10188*4882a593Smuzhiyun 	 * ThinkPad Embedded Controller Program Table on newer models
10189*4882a593Smuzhiyun 	 *
10190*4882a593Smuzhiyun 	 * Offset |  Name                | Width  | Description
10191*4882a593Smuzhiyun 	 * ----------------------------------------------------
10192*4882a593Smuzhiyun 	 *  0x00  | Type                 | BYTE   | 0x8C
10193*4882a593Smuzhiyun 	 *  0x01  | Length               | BYTE   |
10194*4882a593Smuzhiyun 	 *  0x02  | Handle               | WORD   | Varies
10195*4882a593Smuzhiyun 	 *  0x04  | Signature            | BYTEx6 | ASCII for "LENOVO"
10196*4882a593Smuzhiyun 	 *  0x0A  | OEM struct offset    | BYTE   | 0x0B
10197*4882a593Smuzhiyun 	 *  0x0B  | OEM struct number    | BYTE   | 0x07, for this structure
10198*4882a593Smuzhiyun 	 *  0x0C  | OEM struct revision  | BYTE   | 0x01, for this format
10199*4882a593Smuzhiyun 	 *  0x0D  | ECP version ID       | STR ID |
10200*4882a593Smuzhiyun 	 *  0x0E  | ECP release date     | STR ID |
10201*4882a593Smuzhiyun 	 */
10202*4882a593Smuzhiyun 
10203*4882a593Smuzhiyun 	/* Return if data structure not match */
10204*4882a593Smuzhiyun 	if (dm->type != 140 || dm->length < 0x0F ||
10205*4882a593Smuzhiyun 	memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10206*4882a593Smuzhiyun 	dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10207*4882a593Smuzhiyun 	dmi_data[0x0C] != 0x01)
10208*4882a593Smuzhiyun 		return;
10209*4882a593Smuzhiyun 
10210*4882a593Smuzhiyun 	/* fwstr is the first 8byte string  */
10211*4882a593Smuzhiyun 	strncpy(ec_fw_string, dmi_data + 0x0F, 8);
10212*4882a593Smuzhiyun }
10213*4882a593Smuzhiyun 
10214*4882a593Smuzhiyun /* returns 0 - probe ok, or < 0 - probe error.
10215*4882a593Smuzhiyun  * Probe ok doesn't mean thinkpad found.
10216*4882a593Smuzhiyun  * On error, kfree() cleanup on tp->* is not performed, caller must do it */
get_thinkpad_model_data(struct thinkpad_id_data * tp)10217*4882a593Smuzhiyun static int __must_check __init get_thinkpad_model_data(
10218*4882a593Smuzhiyun 						struct thinkpad_id_data *tp)
10219*4882a593Smuzhiyun {
10220*4882a593Smuzhiyun 	const struct dmi_device *dev = NULL;
10221*4882a593Smuzhiyun 	char ec_fw_string[18] = {0};
10222*4882a593Smuzhiyun 	char const *s;
10223*4882a593Smuzhiyun 	char t;
10224*4882a593Smuzhiyun 
10225*4882a593Smuzhiyun 	if (!tp)
10226*4882a593Smuzhiyun 		return -EINVAL;
10227*4882a593Smuzhiyun 
10228*4882a593Smuzhiyun 	memset(tp, 0, sizeof(*tp));
10229*4882a593Smuzhiyun 
10230*4882a593Smuzhiyun 	if (dmi_name_in_vendors("IBM"))
10231*4882a593Smuzhiyun 		tp->vendor = PCI_VENDOR_ID_IBM;
10232*4882a593Smuzhiyun 	else if (dmi_name_in_vendors("LENOVO"))
10233*4882a593Smuzhiyun 		tp->vendor = PCI_VENDOR_ID_LENOVO;
10234*4882a593Smuzhiyun 	else
10235*4882a593Smuzhiyun 		return 0;
10236*4882a593Smuzhiyun 
10237*4882a593Smuzhiyun 	s = dmi_get_system_info(DMI_BIOS_VERSION);
10238*4882a593Smuzhiyun 	tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10239*4882a593Smuzhiyun 	if (s && !tp->bios_version_str)
10240*4882a593Smuzhiyun 		return -ENOMEM;
10241*4882a593Smuzhiyun 
10242*4882a593Smuzhiyun 	/* Really ancient ThinkPad 240X will fail this, which is fine */
10243*4882a593Smuzhiyun 	t = tpacpi_parse_fw_id(tp->bios_version_str,
10244*4882a593Smuzhiyun 			       &tp->bios_model, &tp->bios_release);
10245*4882a593Smuzhiyun 	if (t != 'E' && t != 'C')
10246*4882a593Smuzhiyun 		return 0;
10247*4882a593Smuzhiyun 
10248*4882a593Smuzhiyun 	/*
10249*4882a593Smuzhiyun 	 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10250*4882a593Smuzhiyun 	 * X32 or newer, all Z series;  Some models must have an
10251*4882a593Smuzhiyun 	 * up-to-date BIOS or they will not be detected.
10252*4882a593Smuzhiyun 	 *
10253*4882a593Smuzhiyun 	 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
10254*4882a593Smuzhiyun 	 */
10255*4882a593Smuzhiyun 	while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
10256*4882a593Smuzhiyun 		if (sscanf(dev->name,
10257*4882a593Smuzhiyun 			   "IBM ThinkPad Embedded Controller -[%17c",
10258*4882a593Smuzhiyun 			   ec_fw_string) == 1) {
10259*4882a593Smuzhiyun 			ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10260*4882a593Smuzhiyun 			ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
10261*4882a593Smuzhiyun 			break;
10262*4882a593Smuzhiyun 		}
10263*4882a593Smuzhiyun 	}
10264*4882a593Smuzhiyun 
10265*4882a593Smuzhiyun 	/* Newer ThinkPads have different EC program info table */
10266*4882a593Smuzhiyun 	if (!ec_fw_string[0])
10267*4882a593Smuzhiyun 		dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10268*4882a593Smuzhiyun 
10269*4882a593Smuzhiyun 	if (ec_fw_string[0]) {
10270*4882a593Smuzhiyun 		tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10271*4882a593Smuzhiyun 		if (!tp->ec_version_str)
10272*4882a593Smuzhiyun 			return -ENOMEM;
10273*4882a593Smuzhiyun 
10274*4882a593Smuzhiyun 		t = tpacpi_parse_fw_id(ec_fw_string,
10275*4882a593Smuzhiyun 			 &tp->ec_model, &tp->ec_release);
10276*4882a593Smuzhiyun 		if (t != 'H') {
10277*4882a593Smuzhiyun 			pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10278*4882a593Smuzhiyun 				  ec_fw_string);
10279*4882a593Smuzhiyun 			pr_notice("please report this to %s\n", TPACPI_MAIL);
10280*4882a593Smuzhiyun 		}
10281*4882a593Smuzhiyun 	}
10282*4882a593Smuzhiyun 
10283*4882a593Smuzhiyun 	s = dmi_get_system_info(DMI_PRODUCT_VERSION);
10284*4882a593Smuzhiyun 	if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
10285*4882a593Smuzhiyun 		tp->model_str = kstrdup(s, GFP_KERNEL);
10286*4882a593Smuzhiyun 		if (!tp->model_str)
10287*4882a593Smuzhiyun 			return -ENOMEM;
10288*4882a593Smuzhiyun 	} else {
10289*4882a593Smuzhiyun 		s = dmi_get_system_info(DMI_BIOS_VENDOR);
10290*4882a593Smuzhiyun 		if (s && !(strncasecmp(s, "Lenovo", 6))) {
10291*4882a593Smuzhiyun 			tp->model_str = kstrdup(s, GFP_KERNEL);
10292*4882a593Smuzhiyun 			if (!tp->model_str)
10293*4882a593Smuzhiyun 				return -ENOMEM;
10294*4882a593Smuzhiyun 		}
10295*4882a593Smuzhiyun 	}
10296*4882a593Smuzhiyun 
10297*4882a593Smuzhiyun 	s = dmi_get_system_info(DMI_PRODUCT_NAME);
10298*4882a593Smuzhiyun 	tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10299*4882a593Smuzhiyun 	if (s && !tp->nummodel_str)
10300*4882a593Smuzhiyun 		return -ENOMEM;
10301*4882a593Smuzhiyun 
10302*4882a593Smuzhiyun 	return 0;
10303*4882a593Smuzhiyun }
10304*4882a593Smuzhiyun 
probe_for_thinkpad(void)10305*4882a593Smuzhiyun static int __init probe_for_thinkpad(void)
10306*4882a593Smuzhiyun {
10307*4882a593Smuzhiyun 	int is_thinkpad;
10308*4882a593Smuzhiyun 
10309*4882a593Smuzhiyun 	if (acpi_disabled)
10310*4882a593Smuzhiyun 		return -ENODEV;
10311*4882a593Smuzhiyun 
10312*4882a593Smuzhiyun 	/* It would be dangerous to run the driver in this case */
10313*4882a593Smuzhiyun 	if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10314*4882a593Smuzhiyun 		return -ENODEV;
10315*4882a593Smuzhiyun 
10316*4882a593Smuzhiyun 	/*
10317*4882a593Smuzhiyun 	 * Non-ancient models have better DMI tagging, but very old models
10318*4882a593Smuzhiyun 	 * don't.  tpacpi_is_fw_known() is a cheat to help in that case.
10319*4882a593Smuzhiyun 	 */
10320*4882a593Smuzhiyun 	is_thinkpad = (thinkpad_id.model_str != NULL) ||
10321*4882a593Smuzhiyun 		      (thinkpad_id.ec_model != 0) ||
10322*4882a593Smuzhiyun 		      tpacpi_is_fw_known();
10323*4882a593Smuzhiyun 
10324*4882a593Smuzhiyun 	/* The EC handler is required */
10325*4882a593Smuzhiyun 	tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
10326*4882a593Smuzhiyun 	if (!ec_handle) {
10327*4882a593Smuzhiyun 		if (is_thinkpad)
10328*4882a593Smuzhiyun 			pr_err("Not yet supported ThinkPad detected!\n");
10329*4882a593Smuzhiyun 		return -ENODEV;
10330*4882a593Smuzhiyun 	}
10331*4882a593Smuzhiyun 
10332*4882a593Smuzhiyun 	if (!is_thinkpad && !force_load)
10333*4882a593Smuzhiyun 		return -ENODEV;
10334*4882a593Smuzhiyun 
10335*4882a593Smuzhiyun 	return 0;
10336*4882a593Smuzhiyun }
10337*4882a593Smuzhiyun 
thinkpad_acpi_init_banner(void)10338*4882a593Smuzhiyun static void __init thinkpad_acpi_init_banner(void)
10339*4882a593Smuzhiyun {
10340*4882a593Smuzhiyun 	pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10341*4882a593Smuzhiyun 	pr_info("%s\n", TPACPI_URL);
10342*4882a593Smuzhiyun 
10343*4882a593Smuzhiyun 	pr_info("ThinkPad BIOS %s, EC %s\n",
10344*4882a593Smuzhiyun 		(thinkpad_id.bios_version_str) ?
10345*4882a593Smuzhiyun 			thinkpad_id.bios_version_str : "unknown",
10346*4882a593Smuzhiyun 		(thinkpad_id.ec_version_str) ?
10347*4882a593Smuzhiyun 			thinkpad_id.ec_version_str : "unknown");
10348*4882a593Smuzhiyun 
10349*4882a593Smuzhiyun 	BUG_ON(!thinkpad_id.vendor);
10350*4882a593Smuzhiyun 
10351*4882a593Smuzhiyun 	if (thinkpad_id.model_str)
10352*4882a593Smuzhiyun 		pr_info("%s %s, model %s\n",
10353*4882a593Smuzhiyun 			(thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10354*4882a593Smuzhiyun 				"IBM" : ((thinkpad_id.vendor ==
10355*4882a593Smuzhiyun 						PCI_VENDOR_ID_LENOVO) ?
10356*4882a593Smuzhiyun 					"Lenovo" : "Unknown vendor"),
10357*4882a593Smuzhiyun 			thinkpad_id.model_str,
10358*4882a593Smuzhiyun 			(thinkpad_id.nummodel_str) ?
10359*4882a593Smuzhiyun 				thinkpad_id.nummodel_str : "unknown");
10360*4882a593Smuzhiyun }
10361*4882a593Smuzhiyun 
10362*4882a593Smuzhiyun /* Module init, exit, parameters */
10363*4882a593Smuzhiyun 
10364*4882a593Smuzhiyun static struct ibm_init_struct ibms_init[] __initdata = {
10365*4882a593Smuzhiyun 	{
10366*4882a593Smuzhiyun 		.data = &thinkpad_acpi_driver_data,
10367*4882a593Smuzhiyun 	},
10368*4882a593Smuzhiyun 	{
10369*4882a593Smuzhiyun 		.init = hotkey_init,
10370*4882a593Smuzhiyun 		.data = &hotkey_driver_data,
10371*4882a593Smuzhiyun 	},
10372*4882a593Smuzhiyun 	{
10373*4882a593Smuzhiyun 		.init = bluetooth_init,
10374*4882a593Smuzhiyun 		.data = &bluetooth_driver_data,
10375*4882a593Smuzhiyun 	},
10376*4882a593Smuzhiyun 	{
10377*4882a593Smuzhiyun 		.init = wan_init,
10378*4882a593Smuzhiyun 		.data = &wan_driver_data,
10379*4882a593Smuzhiyun 	},
10380*4882a593Smuzhiyun 	{
10381*4882a593Smuzhiyun 		.init = uwb_init,
10382*4882a593Smuzhiyun 		.data = &uwb_driver_data,
10383*4882a593Smuzhiyun 	},
10384*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_VIDEO
10385*4882a593Smuzhiyun 	{
10386*4882a593Smuzhiyun 		.init = video_init,
10387*4882a593Smuzhiyun 		.base_procfs_mode = S_IRUSR,
10388*4882a593Smuzhiyun 		.data = &video_driver_data,
10389*4882a593Smuzhiyun 	},
10390*4882a593Smuzhiyun #endif
10391*4882a593Smuzhiyun 	{
10392*4882a593Smuzhiyun 		.init = kbdlight_init,
10393*4882a593Smuzhiyun 		.data = &kbdlight_driver_data,
10394*4882a593Smuzhiyun 	},
10395*4882a593Smuzhiyun 	{
10396*4882a593Smuzhiyun 		.init = light_init,
10397*4882a593Smuzhiyun 		.data = &light_driver_data,
10398*4882a593Smuzhiyun 	},
10399*4882a593Smuzhiyun 	{
10400*4882a593Smuzhiyun 		.init = cmos_init,
10401*4882a593Smuzhiyun 		.data = &cmos_driver_data,
10402*4882a593Smuzhiyun 	},
10403*4882a593Smuzhiyun 	{
10404*4882a593Smuzhiyun 		.init = led_init,
10405*4882a593Smuzhiyun 		.data = &led_driver_data,
10406*4882a593Smuzhiyun 	},
10407*4882a593Smuzhiyun 	{
10408*4882a593Smuzhiyun 		.init = beep_init,
10409*4882a593Smuzhiyun 		.data = &beep_driver_data,
10410*4882a593Smuzhiyun 	},
10411*4882a593Smuzhiyun 	{
10412*4882a593Smuzhiyun 		.init = thermal_init,
10413*4882a593Smuzhiyun 		.data = &thermal_driver_data,
10414*4882a593Smuzhiyun 	},
10415*4882a593Smuzhiyun 	{
10416*4882a593Smuzhiyun 		.init = brightness_init,
10417*4882a593Smuzhiyun 		.data = &brightness_driver_data,
10418*4882a593Smuzhiyun 	},
10419*4882a593Smuzhiyun 	{
10420*4882a593Smuzhiyun 		.init = volume_init,
10421*4882a593Smuzhiyun 		.data = &volume_driver_data,
10422*4882a593Smuzhiyun 	},
10423*4882a593Smuzhiyun 	{
10424*4882a593Smuzhiyun 		.init = fan_init,
10425*4882a593Smuzhiyun 		.data = &fan_driver_data,
10426*4882a593Smuzhiyun 	},
10427*4882a593Smuzhiyun 	{
10428*4882a593Smuzhiyun 		.init = mute_led_init,
10429*4882a593Smuzhiyun 		.data = &mute_led_driver_data,
10430*4882a593Smuzhiyun 	},
10431*4882a593Smuzhiyun 	{
10432*4882a593Smuzhiyun 		.init = tpacpi_battery_init,
10433*4882a593Smuzhiyun 		.data = &battery_driver_data,
10434*4882a593Smuzhiyun 	},
10435*4882a593Smuzhiyun 	{
10436*4882a593Smuzhiyun 		.init = tpacpi_lcdshadow_init,
10437*4882a593Smuzhiyun 		.data = &lcdshadow_driver_data,
10438*4882a593Smuzhiyun 	},
10439*4882a593Smuzhiyun 	{
10440*4882a593Smuzhiyun 		.init = tpacpi_dytc_init,
10441*4882a593Smuzhiyun 		.data = &dytc_driver_data,
10442*4882a593Smuzhiyun 	},
10443*4882a593Smuzhiyun };
10444*4882a593Smuzhiyun 
set_ibm_param(const char * val,const struct kernel_param * kp)10445*4882a593Smuzhiyun static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
10446*4882a593Smuzhiyun {
10447*4882a593Smuzhiyun 	unsigned int i;
10448*4882a593Smuzhiyun 	struct ibm_struct *ibm;
10449*4882a593Smuzhiyun 
10450*4882a593Smuzhiyun 	if (!kp || !kp->name || !val)
10451*4882a593Smuzhiyun 		return -EINVAL;
10452*4882a593Smuzhiyun 
10453*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10454*4882a593Smuzhiyun 		ibm = ibms_init[i].data;
10455*4882a593Smuzhiyun 		WARN_ON(ibm == NULL);
10456*4882a593Smuzhiyun 
10457*4882a593Smuzhiyun 		if (!ibm || !ibm->name)
10458*4882a593Smuzhiyun 			continue;
10459*4882a593Smuzhiyun 
10460*4882a593Smuzhiyun 		if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10461*4882a593Smuzhiyun 			if (strlen(val) > sizeof(ibms_init[i].param) - 1)
10462*4882a593Smuzhiyun 				return -ENOSPC;
10463*4882a593Smuzhiyun 			strcpy(ibms_init[i].param, val);
10464*4882a593Smuzhiyun 			return 0;
10465*4882a593Smuzhiyun 		}
10466*4882a593Smuzhiyun 	}
10467*4882a593Smuzhiyun 
10468*4882a593Smuzhiyun 	return -EINVAL;
10469*4882a593Smuzhiyun }
10470*4882a593Smuzhiyun 
10471*4882a593Smuzhiyun module_param(experimental, int, 0444);
10472*4882a593Smuzhiyun MODULE_PARM_DESC(experimental,
10473*4882a593Smuzhiyun 		 "Enables experimental features when non-zero");
10474*4882a593Smuzhiyun 
10475*4882a593Smuzhiyun module_param_named(debug, dbg_level, uint, 0);
10476*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
10477*4882a593Smuzhiyun 
10478*4882a593Smuzhiyun module_param(force_load, bool, 0444);
10479*4882a593Smuzhiyun MODULE_PARM_DESC(force_load,
10480*4882a593Smuzhiyun 		 "Attempts to load the driver even on a mis-identified ThinkPad when true");
10481*4882a593Smuzhiyun 
10482*4882a593Smuzhiyun module_param_named(fan_control, fan_control_allowed, bool, 0444);
10483*4882a593Smuzhiyun MODULE_PARM_DESC(fan_control,
10484*4882a593Smuzhiyun 		 "Enables setting fan parameters features when true");
10485*4882a593Smuzhiyun 
10486*4882a593Smuzhiyun module_param_named(brightness_mode, brightness_mode, uint, 0444);
10487*4882a593Smuzhiyun MODULE_PARM_DESC(brightness_mode,
10488*4882a593Smuzhiyun 		 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
10489*4882a593Smuzhiyun 
10490*4882a593Smuzhiyun module_param(brightness_enable, uint, 0444);
10491*4882a593Smuzhiyun MODULE_PARM_DESC(brightness_enable,
10492*4882a593Smuzhiyun 		 "Enables backlight control when 1, disables when 0");
10493*4882a593Smuzhiyun 
10494*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
10495*4882a593Smuzhiyun module_param_named(volume_mode, volume_mode, uint, 0444);
10496*4882a593Smuzhiyun MODULE_PARM_DESC(volume_mode,
10497*4882a593Smuzhiyun 		 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
10498*4882a593Smuzhiyun 
10499*4882a593Smuzhiyun module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10500*4882a593Smuzhiyun MODULE_PARM_DESC(volume_capabilities,
10501*4882a593Smuzhiyun 		 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
10502*4882a593Smuzhiyun 
10503*4882a593Smuzhiyun module_param_named(volume_control, volume_control_allowed, bool, 0444);
10504*4882a593Smuzhiyun MODULE_PARM_DESC(volume_control,
10505*4882a593Smuzhiyun 		 "Enables software override for the console audio control when true");
10506*4882a593Smuzhiyun 
10507*4882a593Smuzhiyun module_param_named(software_mute, software_mute_requested, bool, 0444);
10508*4882a593Smuzhiyun MODULE_PARM_DESC(software_mute,
10509*4882a593Smuzhiyun 		 "Request full software mute control");
10510*4882a593Smuzhiyun 
10511*4882a593Smuzhiyun /* ALSA module API parameters */
10512*4882a593Smuzhiyun module_param_named(index, alsa_index, int, 0444);
10513*4882a593Smuzhiyun MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10514*4882a593Smuzhiyun module_param_named(id, alsa_id, charp, 0444);
10515*4882a593Smuzhiyun MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10516*4882a593Smuzhiyun module_param_named(enable, alsa_enable, bool, 0444);
10517*4882a593Smuzhiyun MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
10518*4882a593Smuzhiyun #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
10519*4882a593Smuzhiyun 
10520*4882a593Smuzhiyun /* The module parameter can't be read back, that's why 0 is used here */
10521*4882a593Smuzhiyun #define TPACPI_PARAM(feature) \
10522*4882a593Smuzhiyun 	module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
10523*4882a593Smuzhiyun 	MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
10524*4882a593Smuzhiyun 
10525*4882a593Smuzhiyun TPACPI_PARAM(hotkey);
10526*4882a593Smuzhiyun TPACPI_PARAM(bluetooth);
10527*4882a593Smuzhiyun TPACPI_PARAM(video);
10528*4882a593Smuzhiyun TPACPI_PARAM(light);
10529*4882a593Smuzhiyun TPACPI_PARAM(cmos);
10530*4882a593Smuzhiyun TPACPI_PARAM(led);
10531*4882a593Smuzhiyun TPACPI_PARAM(beep);
10532*4882a593Smuzhiyun TPACPI_PARAM(brightness);
10533*4882a593Smuzhiyun TPACPI_PARAM(volume);
10534*4882a593Smuzhiyun TPACPI_PARAM(fan);
10535*4882a593Smuzhiyun 
10536*4882a593Smuzhiyun #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10537*4882a593Smuzhiyun module_param(dbg_wlswemul, uint, 0444);
10538*4882a593Smuzhiyun MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10539*4882a593Smuzhiyun module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10540*4882a593Smuzhiyun MODULE_PARM_DESC(wlsw_state,
10541*4882a593Smuzhiyun 		 "Initial state of the emulated WLSW switch");
10542*4882a593Smuzhiyun 
10543*4882a593Smuzhiyun module_param(dbg_bluetoothemul, uint, 0444);
10544*4882a593Smuzhiyun MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10545*4882a593Smuzhiyun module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10546*4882a593Smuzhiyun MODULE_PARM_DESC(bluetooth_state,
10547*4882a593Smuzhiyun 		 "Initial state of the emulated bluetooth switch");
10548*4882a593Smuzhiyun 
10549*4882a593Smuzhiyun module_param(dbg_wwanemul, uint, 0444);
10550*4882a593Smuzhiyun MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10551*4882a593Smuzhiyun module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10552*4882a593Smuzhiyun MODULE_PARM_DESC(wwan_state,
10553*4882a593Smuzhiyun 		 "Initial state of the emulated WWAN switch");
10554*4882a593Smuzhiyun 
10555*4882a593Smuzhiyun module_param(dbg_uwbemul, uint, 0444);
10556*4882a593Smuzhiyun MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10557*4882a593Smuzhiyun module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10558*4882a593Smuzhiyun MODULE_PARM_DESC(uwb_state,
10559*4882a593Smuzhiyun 		 "Initial state of the emulated UWB switch");
10560*4882a593Smuzhiyun #endif
10561*4882a593Smuzhiyun 
thinkpad_acpi_module_exit(void)10562*4882a593Smuzhiyun static void thinkpad_acpi_module_exit(void)
10563*4882a593Smuzhiyun {
10564*4882a593Smuzhiyun 	struct ibm_struct *ibm, *itmp;
10565*4882a593Smuzhiyun 
10566*4882a593Smuzhiyun 	tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10567*4882a593Smuzhiyun 
10568*4882a593Smuzhiyun 	list_for_each_entry_safe_reverse(ibm, itmp,
10569*4882a593Smuzhiyun 					 &tpacpi_all_drivers,
10570*4882a593Smuzhiyun 					 all_drivers) {
10571*4882a593Smuzhiyun 		ibm_exit(ibm);
10572*4882a593Smuzhiyun 	}
10573*4882a593Smuzhiyun 
10574*4882a593Smuzhiyun 	dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10575*4882a593Smuzhiyun 
10576*4882a593Smuzhiyun 	if (tpacpi_inputdev) {
10577*4882a593Smuzhiyun 		if (tp_features.input_device_registered)
10578*4882a593Smuzhiyun 			input_unregister_device(tpacpi_inputdev);
10579*4882a593Smuzhiyun 		else
10580*4882a593Smuzhiyun 			input_free_device(tpacpi_inputdev);
10581*4882a593Smuzhiyun 		kfree(hotkey_keycode_map);
10582*4882a593Smuzhiyun 	}
10583*4882a593Smuzhiyun 
10584*4882a593Smuzhiyun 	if (tpacpi_hwmon)
10585*4882a593Smuzhiyun 		hwmon_device_unregister(tpacpi_hwmon);
10586*4882a593Smuzhiyun 
10587*4882a593Smuzhiyun 	if (tpacpi_sensors_pdev)
10588*4882a593Smuzhiyun 		platform_device_unregister(tpacpi_sensors_pdev);
10589*4882a593Smuzhiyun 	if (tpacpi_pdev)
10590*4882a593Smuzhiyun 		platform_device_unregister(tpacpi_pdev);
10591*4882a593Smuzhiyun 
10592*4882a593Smuzhiyun 	if (tp_features.sensors_pdrv_attrs_registered)
10593*4882a593Smuzhiyun 		tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10594*4882a593Smuzhiyun 	if (tp_features.platform_drv_attrs_registered)
10595*4882a593Smuzhiyun 		tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10596*4882a593Smuzhiyun 
10597*4882a593Smuzhiyun 	if (tp_features.sensors_pdrv_registered)
10598*4882a593Smuzhiyun 		platform_driver_unregister(&tpacpi_hwmon_pdriver);
10599*4882a593Smuzhiyun 
10600*4882a593Smuzhiyun 	if (tp_features.platform_drv_registered)
10601*4882a593Smuzhiyun 		platform_driver_unregister(&tpacpi_pdriver);
10602*4882a593Smuzhiyun 
10603*4882a593Smuzhiyun 	if (proc_dir)
10604*4882a593Smuzhiyun 		remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
10605*4882a593Smuzhiyun 
10606*4882a593Smuzhiyun 	if (tpacpi_wq)
10607*4882a593Smuzhiyun 		destroy_workqueue(tpacpi_wq);
10608*4882a593Smuzhiyun 
10609*4882a593Smuzhiyun 	kfree(thinkpad_id.bios_version_str);
10610*4882a593Smuzhiyun 	kfree(thinkpad_id.ec_version_str);
10611*4882a593Smuzhiyun 	kfree(thinkpad_id.model_str);
10612*4882a593Smuzhiyun 	kfree(thinkpad_id.nummodel_str);
10613*4882a593Smuzhiyun }
10614*4882a593Smuzhiyun 
10615*4882a593Smuzhiyun 
thinkpad_acpi_module_init(void)10616*4882a593Smuzhiyun static int __init thinkpad_acpi_module_init(void)
10617*4882a593Smuzhiyun {
10618*4882a593Smuzhiyun 	int ret, i;
10619*4882a593Smuzhiyun 
10620*4882a593Smuzhiyun 	tpacpi_lifecycle = TPACPI_LIFE_INIT;
10621*4882a593Smuzhiyun 
10622*4882a593Smuzhiyun 	/* Driver-level probe */
10623*4882a593Smuzhiyun 
10624*4882a593Smuzhiyun 	ret = get_thinkpad_model_data(&thinkpad_id);
10625*4882a593Smuzhiyun 	if (ret) {
10626*4882a593Smuzhiyun 		pr_err("unable to get DMI data: %d\n", ret);
10627*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10628*4882a593Smuzhiyun 		return ret;
10629*4882a593Smuzhiyun 	}
10630*4882a593Smuzhiyun 	ret = probe_for_thinkpad();
10631*4882a593Smuzhiyun 	if (ret) {
10632*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10633*4882a593Smuzhiyun 		return ret;
10634*4882a593Smuzhiyun 	}
10635*4882a593Smuzhiyun 
10636*4882a593Smuzhiyun 	/* Driver initialization */
10637*4882a593Smuzhiyun 
10638*4882a593Smuzhiyun 	thinkpad_acpi_init_banner();
10639*4882a593Smuzhiyun 	tpacpi_check_outdated_fw();
10640*4882a593Smuzhiyun 
10641*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(ecrd);
10642*4882a593Smuzhiyun 	TPACPI_ACPIHANDLE_INIT(ecwr);
10643*4882a593Smuzhiyun 
10644*4882a593Smuzhiyun 	tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10645*4882a593Smuzhiyun 	if (!tpacpi_wq) {
10646*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10647*4882a593Smuzhiyun 		return -ENOMEM;
10648*4882a593Smuzhiyun 	}
10649*4882a593Smuzhiyun 
10650*4882a593Smuzhiyun 	proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
10651*4882a593Smuzhiyun 	if (!proc_dir) {
10652*4882a593Smuzhiyun 		pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
10653*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10654*4882a593Smuzhiyun 		return -ENODEV;
10655*4882a593Smuzhiyun 	}
10656*4882a593Smuzhiyun 
10657*4882a593Smuzhiyun 	ret = platform_driver_register(&tpacpi_pdriver);
10658*4882a593Smuzhiyun 	if (ret) {
10659*4882a593Smuzhiyun 		pr_err("unable to register main platform driver\n");
10660*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10661*4882a593Smuzhiyun 		return ret;
10662*4882a593Smuzhiyun 	}
10663*4882a593Smuzhiyun 	tp_features.platform_drv_registered = 1;
10664*4882a593Smuzhiyun 
10665*4882a593Smuzhiyun 	ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10666*4882a593Smuzhiyun 	if (ret) {
10667*4882a593Smuzhiyun 		pr_err("unable to register hwmon platform driver\n");
10668*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10669*4882a593Smuzhiyun 		return ret;
10670*4882a593Smuzhiyun 	}
10671*4882a593Smuzhiyun 	tp_features.sensors_pdrv_registered = 1;
10672*4882a593Smuzhiyun 
10673*4882a593Smuzhiyun 	ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
10674*4882a593Smuzhiyun 	if (!ret) {
10675*4882a593Smuzhiyun 		tp_features.platform_drv_attrs_registered = 1;
10676*4882a593Smuzhiyun 		ret = tpacpi_create_driver_attributes(
10677*4882a593Smuzhiyun 					&tpacpi_hwmon_pdriver.driver);
10678*4882a593Smuzhiyun 	}
10679*4882a593Smuzhiyun 	if (ret) {
10680*4882a593Smuzhiyun 		pr_err("unable to create sysfs driver attributes\n");
10681*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10682*4882a593Smuzhiyun 		return ret;
10683*4882a593Smuzhiyun 	}
10684*4882a593Smuzhiyun 	tp_features.sensors_pdrv_attrs_registered = 1;
10685*4882a593Smuzhiyun 
10686*4882a593Smuzhiyun 
10687*4882a593Smuzhiyun 	/* Device initialization */
10688*4882a593Smuzhiyun 	tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
10689*4882a593Smuzhiyun 							NULL, 0);
10690*4882a593Smuzhiyun 	if (IS_ERR(tpacpi_pdev)) {
10691*4882a593Smuzhiyun 		ret = PTR_ERR(tpacpi_pdev);
10692*4882a593Smuzhiyun 		tpacpi_pdev = NULL;
10693*4882a593Smuzhiyun 		pr_err("unable to register platform device\n");
10694*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10695*4882a593Smuzhiyun 		return ret;
10696*4882a593Smuzhiyun 	}
10697*4882a593Smuzhiyun 	tpacpi_sensors_pdev = platform_device_register_simple(
10698*4882a593Smuzhiyun 						TPACPI_HWMON_DRVR_NAME,
10699*4882a593Smuzhiyun 						-1, NULL, 0);
10700*4882a593Smuzhiyun 	if (IS_ERR(tpacpi_sensors_pdev)) {
10701*4882a593Smuzhiyun 		ret = PTR_ERR(tpacpi_sensors_pdev);
10702*4882a593Smuzhiyun 		tpacpi_sensors_pdev = NULL;
10703*4882a593Smuzhiyun 		pr_err("unable to register hwmon platform device\n");
10704*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10705*4882a593Smuzhiyun 		return ret;
10706*4882a593Smuzhiyun 	}
10707*4882a593Smuzhiyun 	tp_features.sensors_pdev_attrs_registered = 1;
10708*4882a593Smuzhiyun 	tpacpi_hwmon = hwmon_device_register_with_groups(
10709*4882a593Smuzhiyun 		&tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10710*4882a593Smuzhiyun 
10711*4882a593Smuzhiyun 	if (IS_ERR(tpacpi_hwmon)) {
10712*4882a593Smuzhiyun 		ret = PTR_ERR(tpacpi_hwmon);
10713*4882a593Smuzhiyun 		tpacpi_hwmon = NULL;
10714*4882a593Smuzhiyun 		pr_err("unable to register hwmon device\n");
10715*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10716*4882a593Smuzhiyun 		return ret;
10717*4882a593Smuzhiyun 	}
10718*4882a593Smuzhiyun 	mutex_init(&tpacpi_inputdev_send_mutex);
10719*4882a593Smuzhiyun 	tpacpi_inputdev = input_allocate_device();
10720*4882a593Smuzhiyun 	if (!tpacpi_inputdev) {
10721*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10722*4882a593Smuzhiyun 		return -ENOMEM;
10723*4882a593Smuzhiyun 	} else {
10724*4882a593Smuzhiyun 		/* Prepare input device, but don't register */
10725*4882a593Smuzhiyun 		tpacpi_inputdev->name = "ThinkPad Extra Buttons";
10726*4882a593Smuzhiyun 		tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
10727*4882a593Smuzhiyun 		tpacpi_inputdev->id.bustype = BUS_HOST;
10728*4882a593Smuzhiyun 		tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
10729*4882a593Smuzhiyun 		tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10730*4882a593Smuzhiyun 		tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
10731*4882a593Smuzhiyun 		tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
10732*4882a593Smuzhiyun 	}
10733*4882a593Smuzhiyun 
10734*4882a593Smuzhiyun 	/* Init subdriver dependencies */
10735*4882a593Smuzhiyun 	tpacpi_detect_brightness_capabilities();
10736*4882a593Smuzhiyun 
10737*4882a593Smuzhiyun 	/* Init subdrivers */
10738*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10739*4882a593Smuzhiyun 		ret = ibm_init(&ibms_init[i]);
10740*4882a593Smuzhiyun 		if (ret >= 0 && *ibms_init[i].param)
10741*4882a593Smuzhiyun 			ret = ibms_init[i].data->write(ibms_init[i].param);
10742*4882a593Smuzhiyun 		if (ret < 0) {
10743*4882a593Smuzhiyun 			thinkpad_acpi_module_exit();
10744*4882a593Smuzhiyun 			return ret;
10745*4882a593Smuzhiyun 		}
10746*4882a593Smuzhiyun 	}
10747*4882a593Smuzhiyun 
10748*4882a593Smuzhiyun 	tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10749*4882a593Smuzhiyun 
10750*4882a593Smuzhiyun 	ret = input_register_device(tpacpi_inputdev);
10751*4882a593Smuzhiyun 	if (ret < 0) {
10752*4882a593Smuzhiyun 		pr_err("unable to register input device\n");
10753*4882a593Smuzhiyun 		thinkpad_acpi_module_exit();
10754*4882a593Smuzhiyun 		return ret;
10755*4882a593Smuzhiyun 	} else {
10756*4882a593Smuzhiyun 		tp_features.input_device_registered = 1;
10757*4882a593Smuzhiyun 	}
10758*4882a593Smuzhiyun 
10759*4882a593Smuzhiyun 	return 0;
10760*4882a593Smuzhiyun }
10761*4882a593Smuzhiyun 
10762*4882a593Smuzhiyun MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10763*4882a593Smuzhiyun 
10764*4882a593Smuzhiyun /*
10765*4882a593Smuzhiyun  * This will autoload the driver in almost every ThinkPad
10766*4882a593Smuzhiyun  * in widespread use.
10767*4882a593Smuzhiyun  *
10768*4882a593Smuzhiyun  * Only _VERY_ old models, like the 240, 240x and 570 lack
10769*4882a593Smuzhiyun  * the HKEY event interface.
10770*4882a593Smuzhiyun  */
10771*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10772*4882a593Smuzhiyun 
10773*4882a593Smuzhiyun /*
10774*4882a593Smuzhiyun  * DMI matching for module autoloading
10775*4882a593Smuzhiyun  *
10776*4882a593Smuzhiyun  * See https://thinkwiki.org/wiki/List_of_DMI_IDs
10777*4882a593Smuzhiyun  * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10778*4882a593Smuzhiyun  *
10779*4882a593Smuzhiyun  * Only models listed in thinkwiki will be supported, so add yours
10780*4882a593Smuzhiyun  * if it is not there yet.
10781*4882a593Smuzhiyun  */
10782*4882a593Smuzhiyun #define IBM_BIOS_MODULE_ALIAS(__type) \
10783*4882a593Smuzhiyun 	MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10784*4882a593Smuzhiyun 
10785*4882a593Smuzhiyun /* Ancient thinkpad BIOSes have to be identified by
10786*4882a593Smuzhiyun  * BIOS type or model number, and there are far less
10787*4882a593Smuzhiyun  * BIOS types than model numbers... */
10788*4882a593Smuzhiyun IBM_BIOS_MODULE_ALIAS("I[MU]");		/* 570, 570e */
10789*4882a593Smuzhiyun 
10790*4882a593Smuzhiyun MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10791*4882a593Smuzhiyun MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10792*4882a593Smuzhiyun MODULE_DESCRIPTION(TPACPI_DESC);
10793*4882a593Smuzhiyun MODULE_VERSION(TPACPI_VERSION);
10794*4882a593Smuzhiyun MODULE_LICENSE("GPL");
10795*4882a593Smuzhiyun 
10796*4882a593Smuzhiyun module_init(thinkpad_acpi_module_init);
10797*4882a593Smuzhiyun module_exit(thinkpad_acpi_module_exit);
10798