xref: /linux/drivers/platform/x86/toshiba_acpi.c (revision 78289b4a58b58e9a8a76ef43ffbaf04a097e33c6)
19ab65affSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b4f9fe12SLen Brown /*
3b4f9fe12SLen Brown  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
4b4f9fe12SLen Brown  *
5b4f9fe12SLen Brown  *  Copyright (C) 2002-2004 John Belmonte
6b4f9fe12SLen Brown  *  Copyright (C) 2008 Philip Langdale
76c3f6e6cSPierre Ducroquet  *  Copyright (C) 2010 Pierre Ducroquet
895d16d81SAzael Avalos  *  Copyright (C) 2014-2016 Azael Avalos
9b4f9fe12SLen Brown  *
10b4f9fe12SLen Brown  *  The devolpment page for this driver is located at
11b4f9fe12SLen Brown  *  http://memebeam.org/toys/ToshibaAcpiDriver.
12b4f9fe12SLen Brown  *
13b4f9fe12SLen Brown  *  Credits:
14b4f9fe12SLen Brown  *	Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
15b4f9fe12SLen Brown  *		engineering the Windows drivers
16b4f9fe12SLen Brown  *	Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
17b4f9fe12SLen Brown  *	Rob Miller - TV out and hotkeys help
18b4f9fe12SLen Brown  */
19b4f9fe12SLen Brown 
207e33460dSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
217e33460dSJoe Perches 
2295d16d81SAzael Avalos #define TOSHIBA_ACPI_VERSION	"0.24"
23b4f9fe12SLen Brown #define PROC_INTERFACE_VERSION	1
24b4f9fe12SLen Brown 
25c2e2a618SRandy Dunlap #include <linux/compiler.h>
26b4f9fe12SLen Brown #include <linux/kernel.h>
27b4f9fe12SLen Brown #include <linux/module.h>
287faa6a37SAzael Avalos #include <linux/moduleparam.h>
29b4f9fe12SLen Brown #include <linux/init.h>
30b4f9fe12SLen Brown #include <linux/types.h>
31b4f9fe12SLen Brown #include <linux/proc_fs.h>
32936c8bcdSAlexey Dobriyan #include <linux/seq_file.h>
33b4f9fe12SLen Brown #include <linux/backlight.h>
346335e4d5SMatthew Garrett #include <linux/input.h>
35384a7cd9SDmitry Torokhov #include <linux/input/sparse-keymap.h>
366c3f6e6cSPierre Ducroquet #include <linux/leds.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
3829cd293fSSeth Forshee #include <linux/workqueue.h>
3929cd293fSSeth Forshee #include <linux/i8042.h>
408b48463fSLv Zheng #include <linux/acpi.h>
41358d6a2cSHans de Goede #include <linux/dmi.h>
42b5163992SAzael Avalos #include <linux/uaccess.h>
43fc5462f8SAzael Avalos #include <linux/miscdevice.h>
442fdde834SAzael Avalos #include <linux/rfkill.h>
4598010f1eSAzael Avalos #include <linux/iio/iio.h>
46fc5462f8SAzael Avalos #include <linux/toshiba.h>
47358d6a2cSHans de Goede #include <acpi/video.h>
48b4f9fe12SLen Brown 
49b4f9fe12SLen Brown MODULE_AUTHOR("John Belmonte");
50b4f9fe12SLen Brown MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
51b4f9fe12SLen Brown MODULE_LICENSE("GPL");
52b4f9fe12SLen Brown 
53f11f999eSSeth Forshee #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
54f11f999eSSeth Forshee 
5529cd293fSSeth Forshee /* Scan code for Fn key on TOS1900 models */
5629cd293fSSeth Forshee #define TOS1900_FN_SCAN		0x6e
5729cd293fSSeth Forshee 
58b4f9fe12SLen Brown /* Toshiba ACPI method paths */
59b4f9fe12SLen Brown #define METHOD_VIDEO_OUT	"\\_SB_.VALX.DSSX"
60b4f9fe12SLen Brown 
61e0769fe6SDarren Hart /*
62e0769fe6SDarren Hart  * The Toshiba configuration interface is composed of the HCI and the SCI,
63258c5903SAzael Avalos  * which are defined as follows:
64b4f9fe12SLen Brown  *
65b4f9fe12SLen Brown  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
66b4f9fe12SLen Brown  * be uniform across all their models.  Ideally we would just call
67b4f9fe12SLen Brown  * dedicated ACPI methods instead of using this primitive interface.
68b4f9fe12SLen Brown  * However the ACPI methods seem to be incomplete in some areas (for
69b4f9fe12SLen Brown  * example they allow setting, but not reading, the LCD brightness value),
70b4f9fe12SLen Brown  * so this is still useful.
7184a6273fSAzael Avalos  *
7284a6273fSAzael Avalos  * SCI stands for "System Configuration Interface" which aim is to
7384a6273fSAzael Avalos  * conceal differences in hardware between different models.
74b4f9fe12SLen Brown  */
75b4f9fe12SLen Brown 
76258c5903SAzael Avalos #define TCI_WORDS			6
77b4f9fe12SLen Brown 
783f75bbe9SAzael Avalos /* Operations */
79b4f9fe12SLen Brown #define HCI_SET				0xff00
80b4f9fe12SLen Brown #define HCI_GET				0xfe00
8184a6273fSAzael Avalos #define SCI_OPEN			0xf100
8284a6273fSAzael Avalos #define SCI_CLOSE			0xf200
8384a6273fSAzael Avalos #define SCI_GET				0xf300
8484a6273fSAzael Avalos #define SCI_SET				0xf400
85b4f9fe12SLen Brown 
863f75bbe9SAzael Avalos /* Return codes */
871864bbc2SAzael Avalos #define TOS_SUCCESS			0x0000
88e1a949c1SAzael Avalos #define TOS_SUCCESS2			0x0001
891864bbc2SAzael Avalos #define TOS_OPEN_CLOSE_OK		0x0044
901864bbc2SAzael Avalos #define TOS_FAILURE			0x1000
911864bbc2SAzael Avalos #define TOS_NOT_SUPPORTED		0x8000
921864bbc2SAzael Avalos #define TOS_ALREADY_OPEN		0x8100
931864bbc2SAzael Avalos #define TOS_NOT_OPENED			0x8200
941864bbc2SAzael Avalos #define TOS_INPUT_DATA_ERROR		0x8300
951864bbc2SAzael Avalos #define TOS_WRITE_PROTECTED		0x8400
961864bbc2SAzael Avalos #define TOS_NOT_PRESENT			0x8600
971864bbc2SAzael Avalos #define TOS_FIFO_EMPTY			0x8c00
981864bbc2SAzael Avalos #define TOS_DATA_NOT_AVAILABLE		0x8d20
991864bbc2SAzael Avalos #define TOS_NOT_INITIALIZED		0x8d50
10098fc4ec6SAzael Avalos #define TOS_NOT_INSTALLED		0x8e00
101b4f9fe12SLen Brown 
1023f75bbe9SAzael Avalos /* Registers */
103b4f9fe12SLen Brown #define HCI_FAN				0x0004
104121b7b0dSAkio Idehara #define HCI_TR_BACKLIGHT		0x0005
105b4f9fe12SLen Brown #define HCI_SYSTEM_EVENT		0x0016
106b4f9fe12SLen Brown #define HCI_VIDEO_OUT			0x001c
107b4f9fe12SLen Brown #define HCI_HOTKEY_EVENT		0x001e
108b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS		0x002a
1096873f46aSAzael Avalos #define HCI_WIRELESS			0x0056
1105a2813e9SAzael Avalos #define HCI_ACCELEROMETER		0x006d
111763ff32fSAzael Avalos #define HCI_COOLING_METHOD		0x007f
112360f0f39SAzael Avalos #define HCI_KBD_ILLUMINATION		0x0095
113def6c4e2SAzael Avalos #define HCI_ECO_MODE			0x0097
1145a2813e9SAzael Avalos #define HCI_ACCELEROMETER2		0x00a6
11556e6b353SAzael Avalos #define HCI_SYSTEM_INFO			0xc000
11635d53ceaSAzael Avalos #define SCI_PANEL_POWER_ON		0x010d
117fdb79081SAzael Avalos #define SCI_ILLUMINATION		0x014e
118e26ffe51SAzael Avalos #define SCI_USB_SLEEP_CHARGE		0x0150
119360f0f39SAzael Avalos #define SCI_KBD_ILLUM_STATUS		0x015c
120172ce0a9SAzael Avalos #define SCI_USB_SLEEP_MUSIC		0x015e
12117fe4b3dSAzael Avalos #define SCI_USB_THREE			0x0169
1229d8658acSAzael Avalos #define SCI_TOUCHPAD			0x050e
123bae84195SAzael Avalos #define SCI_KBD_FUNCTION_KEYS		0x0522
124b4f9fe12SLen Brown 
1253f75bbe9SAzael Avalos /* Field definitions */
1265a2813e9SAzael Avalos #define HCI_ACCEL_MASK			0x7fff
12798010f1eSAzael Avalos #define HCI_ACCEL_DIRECTION_MASK	0x8000
12829cd293fSSeth Forshee #define HCI_HOTKEY_DISABLE		0x0b
129a30b8f81SAzael Avalos #define HCI_HOTKEY_ENABLE		0x09
130fb42d1f4SAzael Avalos #define HCI_HOTKEY_SPECIAL_FUNCTIONS	0x10
131b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_BITS		3
132b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_SHIFT	(16-HCI_LCD_BRIGHTNESS_BITS)
133b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_LEVELS	(1 << HCI_LCD_BRIGHTNESS_BITS)
134360f0f39SAzael Avalos #define HCI_MISC_SHIFT			0x10
13556e6b353SAzael Avalos #define HCI_SYSTEM_TYPE1		0x10
13656e6b353SAzael Avalos #define HCI_SYSTEM_TYPE2		0x11
137b4f9fe12SLen Brown #define HCI_VIDEO_OUT_LCD		0x1
138b4f9fe12SLen Brown #define HCI_VIDEO_OUT_CRT		0x2
139b4f9fe12SLen Brown #define HCI_VIDEO_OUT_TV		0x4
14093f8c16dSAzael Avalos #define SCI_KBD_MODE_MASK		0x1f
141360f0f39SAzael Avalos #define SCI_KBD_MODE_FNZ		0x1
142360f0f39SAzael Avalos #define SCI_KBD_MODE_AUTO		0x2
14393f8c16dSAzael Avalos #define SCI_KBD_MODE_ON			0x8
14493f8c16dSAzael Avalos #define SCI_KBD_MODE_OFF		0x10
14593f8c16dSAzael Avalos #define SCI_KBD_TIME_MAX		0x3c001a
1466873f46aSAzael Avalos #define HCI_WIRELESS_STATUS		0x1
1476873f46aSAzael Avalos #define HCI_WIRELESS_WWAN		0x3
1486873f46aSAzael Avalos #define HCI_WIRELESS_WWAN_STATUS	0x2000
1496873f46aSAzael Avalos #define HCI_WIRELESS_WWAN_POWER		0x4000
150e26ffe51SAzael Avalos #define SCI_USB_CHARGE_MODE_MASK	0xff
151c8c91842SAzael Avalos #define SCI_USB_CHARGE_DISABLED		0x00
152c8c91842SAzael Avalos #define SCI_USB_CHARGE_ALTERNATE	0x09
153c8c91842SAzael Avalos #define SCI_USB_CHARGE_TYPICAL		0x11
154c8c91842SAzael Avalos #define SCI_USB_CHARGE_AUTO		0x21
155182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_MASK		0x7
156182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL_OFF	0x1
157182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL_ON	0x4
158182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL		0x0200
159bb3fe01fSAzael Avalos #define SCI_USB_CHARGE_RAPID_DSP	0x0300
160b4f9fe12SLen Brown 
161135740deSSeth Forshee struct toshiba_acpi_dev {
162135740deSSeth Forshee 	struct acpi_device *acpi_dev;
163135740deSSeth Forshee 	const char *method_hci;
164135740deSSeth Forshee 	struct input_dev *hotkey_dev;
16529cd293fSSeth Forshee 	struct work_struct hotkey_work;
166135740deSSeth Forshee 	struct backlight_device *backlight_dev;
167135740deSSeth Forshee 	struct led_classdev led_dev;
168360f0f39SAzael Avalos 	struct led_classdev kbd_led;
169def6c4e2SAzael Avalos 	struct led_classdev eco_led;
170fc5462f8SAzael Avalos 	struct miscdevice miscdev;
1712fdde834SAzael Avalos 	struct rfkill *wwan_rfk;
17298010f1eSAzael Avalos 	struct iio_dev *indio_dev;
17336d03f93SSeth Forshee 
174135740deSSeth Forshee 	int force_fan;
175135740deSSeth Forshee 	int last_key_event;
176135740deSSeth Forshee 	int key_event_valid;
17793f8c16dSAzael Avalos 	int kbd_type;
178360f0f39SAzael Avalos 	int kbd_mode;
179360f0f39SAzael Avalos 	int kbd_time;
180182bcaa5SAzael Avalos 	int usbsc_bat_level;
181c8c91842SAzael Avalos 	int usbsc_mode_base;
182a2b3471bSAzael Avalos 	int hotkey_event_type;
183763ff32fSAzael Avalos 	int max_cooling_method;
184135740deSSeth Forshee 
185592b746cSDan Carpenter 	unsigned int illumination_supported:1;
186592b746cSDan Carpenter 	unsigned int video_supported:1;
187592b746cSDan Carpenter 	unsigned int fan_supported:1;
188592b746cSDan Carpenter 	unsigned int system_event_supported:1;
18929cd293fSSeth Forshee 	unsigned int ntfy_supported:1;
19029cd293fSSeth Forshee 	unsigned int info_supported:1;
191121b7b0dSAkio Idehara 	unsigned int tr_backlight_supported:1;
192360f0f39SAzael Avalos 	unsigned int kbd_illum_supported:1;
1939d8658acSAzael Avalos 	unsigned int touchpad_supported:1;
194def6c4e2SAzael Avalos 	unsigned int eco_supported:1;
1955a2813e9SAzael Avalos 	unsigned int accelerometer_supported:1;
196e26ffe51SAzael Avalos 	unsigned int usb_sleep_charge_supported:1;
197bb3fe01fSAzael Avalos 	unsigned int usb_rapid_charge_supported:1;
198172ce0a9SAzael Avalos 	unsigned int usb_sleep_music_supported:1;
199bae84195SAzael Avalos 	unsigned int kbd_function_keys_supported:1;
20035d53ceaSAzael Avalos 	unsigned int panel_power_on_supported:1;
20117fe4b3dSAzael Avalos 	unsigned int usb_three_supported:1;
2026873f46aSAzael Avalos 	unsigned int wwan_supported:1;
203763ff32fSAzael Avalos 	unsigned int cooling_method_supported:1;
204360f0f39SAzael Avalos 	unsigned int sysfs_created:1;
205b116fd00SAzael Avalos 	unsigned int special_functions;
206ea215a3fSAzael Avalos 
20765e3cf9cSAzael Avalos 	bool kbd_event_generated;
2086873f46aSAzael Avalos 	bool killswitch;
209135740deSSeth Forshee };
210135740deSSeth Forshee 
21129cd293fSSeth Forshee static struct toshiba_acpi_dev *toshiba_acpi;
21229cd293fSSeth Forshee 
2137faa6a37SAzael Avalos static bool disable_hotkeys;
2147faa6a37SAzael Avalos module_param(disable_hotkeys, bool, 0444);
2157faa6a37SAzael Avalos MODULE_PARM_DESC(disable_hotkeys, "Disables the hotkeys activation");
2167faa6a37SAzael Avalos 
217b4f9fe12SLen Brown static const struct acpi_device_id toshiba_device_ids[] = {
218b4f9fe12SLen Brown 	{"TOS6200", 0},
21963a9e016SOndrej Zary 	{"TOS6207", 0},
220b4f9fe12SLen Brown 	{"TOS6208", 0},
221b4f9fe12SLen Brown 	{"TOS1900", 0},
222b4f9fe12SLen Brown 	{"", 0},
223b4f9fe12SLen Brown };
224b4f9fe12SLen Brown MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
225b4f9fe12SLen Brown 
226b859f159SGreg Kroah-Hartman static const struct key_entry toshiba_acpi_keymap[] = {
227fec278a1SUnai Uribarri 	{ KE_KEY, 0x9e, { KEY_RFKILL } },
228384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x101, { KEY_MUTE } },
229384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x102, { KEY_ZOOMOUT } },
230384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x103, { KEY_ZOOMIN } },
231408a5d13SAzael Avalos 	{ KE_KEY, 0x10f, { KEY_TAB } },
232af502837SAzael Avalos 	{ KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
233af502837SAzael Avalos 	{ KE_KEY, 0x139, { KEY_ZOOMRESET } },
234384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13b, { KEY_COFFEE } },
235384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13c, { KEY_BATTERY } },
236384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13d, { KEY_SLEEP } },
237384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13e, { KEY_SUSPEND } },
238384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
239384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
240384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
241384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x142, { KEY_WLAN } },
242af502837SAzael Avalos 	{ KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
243a49010f5SJon Dowland 	{ KE_KEY, 0x17f, { KEY_FN } },
244384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb05, { KEY_PROG2 } },
245384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb06, { KEY_WWW } },
246384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb07, { KEY_MAIL } },
247384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb30, { KEY_STOP } },
248384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
249384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb32, { KEY_NEXTSONG } },
250384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
251384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb5a, { KEY_MEDIA } },
252408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
253408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
254408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
255408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
256408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
257384a7cd9SDmitry Torokhov 	{ KE_END, 0 },
2586335e4d5SMatthew Garrett };
2596335e4d5SMatthew Garrett 
260fe808bfbSTakashi Iwai static const struct key_entry toshiba_acpi_alt_keymap[] = {
261fe808bfbSTakashi Iwai 	{ KE_KEY, 0x102, { KEY_ZOOMOUT } },
262fe808bfbSTakashi Iwai 	{ KE_KEY, 0x103, { KEY_ZOOMIN } },
263e6efad7fSAzael Avalos 	{ KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
264fe808bfbSTakashi Iwai 	{ KE_KEY, 0x139, { KEY_ZOOMRESET } },
265fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
266fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
267d50c9005SAzael Avalos 	{ KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
268fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
269d50c9005SAzael Avalos 	{ KE_KEY, 0x157, { KEY_MUTE } },
270d50c9005SAzael Avalos 	{ KE_KEY, 0x158, { KEY_WLAN } },
271fe808bfbSTakashi Iwai 	{ KE_END, 0 },
272fe808bfbSTakashi Iwai };
273fe808bfbSTakashi Iwai 
274e0769fe6SDarren Hart /*
275358d6a2cSHans de Goede  * List of models which have a broken acpi-video backlight interface and thus
276358d6a2cSHans de Goede  * need to use the toshiba (vendor) interface instead.
277358d6a2cSHans de Goede  */
278358d6a2cSHans de Goede static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
279358d6a2cSHans de Goede 	{}
280358d6a2cSHans de Goede };
281358d6a2cSHans de Goede 
282358d6a2cSHans de Goede /*
283e0769fe6SDarren Hart  * Utility
284b4f9fe12SLen Brown  */
285b4f9fe12SLen Brown 
286b5163992SAzael Avalos static inline void _set_bit(u32 *word, u32 mask, int value)
287b4f9fe12SLen Brown {
288b4f9fe12SLen Brown 	*word = (*word & ~mask) | (mask * value);
289b4f9fe12SLen Brown }
290b4f9fe12SLen Brown 
291e0769fe6SDarren Hart /*
292e0769fe6SDarren Hart  * ACPI interface wrappers
293b4f9fe12SLen Brown  */
294b4f9fe12SLen Brown 
295b4f9fe12SLen Brown static int write_acpi_int(const char *methodName, int val)
296b4f9fe12SLen Brown {
297b4f9fe12SLen Brown 	acpi_status status;
298b4f9fe12SLen Brown 
299619400daSZhang Rui 	status = acpi_execute_simple_method(NULL, (char *)methodName, val);
30032bcd5cbSSeth Forshee 	return (status == AE_OK) ? 0 : -EIO;
301b4f9fe12SLen Brown }
302b4f9fe12SLen Brown 
303e0769fe6SDarren Hart /*
304e0769fe6SDarren Hart  * Perform a raw configuration call.  Here we don't care about input or output
305258c5903SAzael Avalos  * buffer format.
306b4f9fe12SLen Brown  */
307258c5903SAzael Avalos static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
308258c5903SAzael Avalos 			   const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
309b4f9fe12SLen Brown {
31078429e55SAzael Avalos 	union acpi_object in_objs[TCI_WORDS], out_objs[TCI_WORDS + 1];
311b4f9fe12SLen Brown 	struct acpi_object_list params;
312b4f9fe12SLen Brown 	struct acpi_buffer results;
313b4f9fe12SLen Brown 	acpi_status status;
314b4f9fe12SLen Brown 	int i;
315b4f9fe12SLen Brown 
316258c5903SAzael Avalos 	params.count = TCI_WORDS;
317b4f9fe12SLen Brown 	params.pointer = in_objs;
318258c5903SAzael Avalos 	for (i = 0; i < TCI_WORDS; ++i) {
319b4f9fe12SLen Brown 		in_objs[i].type = ACPI_TYPE_INTEGER;
320b4f9fe12SLen Brown 		in_objs[i].integer.value = in[i];
321b4f9fe12SLen Brown 	}
322b4f9fe12SLen Brown 
323b4f9fe12SLen Brown 	results.length = sizeof(out_objs);
324b4f9fe12SLen Brown 	results.pointer = out_objs;
325b4f9fe12SLen Brown 
3266e02cc7eSSeth Forshee 	status = acpi_evaluate_object(dev->acpi_dev->handle,
3276e02cc7eSSeth Forshee 				      (char *)dev->method_hci, &params,
328b4f9fe12SLen Brown 				      &results);
329258c5903SAzael Avalos 	if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
330b5163992SAzael Avalos 		for (i = 0; i < out_objs->package.count; ++i)
331b4f9fe12SLen Brown 			out[i] = out_objs->package.elements[i].integer.value;
332b4f9fe12SLen Brown 	}
333b4f9fe12SLen Brown 
334b4f9fe12SLen Brown 	return status;
335b4f9fe12SLen Brown }
336b4f9fe12SLen Brown 
337e0769fe6SDarren Hart /*
338d37782bdSAzael Avalos  * Common hci tasks
339b4f9fe12SLen Brown  *
340b4f9fe12SLen Brown  * In addition to the ACPI status, the HCI system returns a result which
341b4f9fe12SLen Brown  * may be useful (such as "not supported").
342b4f9fe12SLen Brown  */
343b4f9fe12SLen Brown 
344d37782bdSAzael Avalos static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
345b4f9fe12SLen Brown {
346258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
347258c5903SAzael Avalos 	u32 out[TCI_WORDS];
348258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
349893f3f62SAzael Avalos 
350893f3f62SAzael Avalos 	return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
351b4f9fe12SLen Brown }
352b4f9fe12SLen Brown 
353d37782bdSAzael Avalos static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
354b4f9fe12SLen Brown {
355258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
356258c5903SAzael Avalos 	u32 out[TCI_WORDS];
357258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
358b5163992SAzael Avalos 
359893f3f62SAzael Avalos 	if (ACPI_FAILURE(status))
360893f3f62SAzael Avalos 		return TOS_FAILURE;
361893f3f62SAzael Avalos 
362b4f9fe12SLen Brown 	*out1 = out[2];
363893f3f62SAzael Avalos 
364893f3f62SAzael Avalos 	return out[0];
365b4f9fe12SLen Brown }
366b4f9fe12SLen Brown 
367e0769fe6SDarren Hart /*
368e0769fe6SDarren Hart  * Common sci tasks
36984a6273fSAzael Avalos  */
37084a6273fSAzael Avalos 
37184a6273fSAzael Avalos static int sci_open(struct toshiba_acpi_dev *dev)
37284a6273fSAzael Avalos {
373258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
374258c5903SAzael Avalos 	u32 out[TCI_WORDS];
37578429e55SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
37684a6273fSAzael Avalos 
3778baec45dSAzael Avalos 	if  (ACPI_FAILURE(status)) {
37884a6273fSAzael Avalos 		pr_err("ACPI call to open SCI failed\n");
37984a6273fSAzael Avalos 		return 0;
38084a6273fSAzael Avalos 	}
38184a6273fSAzael Avalos 
3821864bbc2SAzael Avalos 	if (out[0] == TOS_OPEN_CLOSE_OK) {
38384a6273fSAzael Avalos 		return 1;
3841864bbc2SAzael Avalos 	} else if (out[0] == TOS_ALREADY_OPEN) {
38584a6273fSAzael Avalos 		pr_info("Toshiba SCI already opened\n");
38684a6273fSAzael Avalos 		return 1;
387fa465739SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
388e0769fe6SDarren Hart 		/*
389e0769fe6SDarren Hart 		 * Some BIOSes do not have the SCI open/close functions
390fa465739SAzael Avalos 		 * implemented and return 0x8000 (Not Supported), failing to
391fa465739SAzael Avalos 		 * register some supported features.
392fa465739SAzael Avalos 		 *
393fa465739SAzael Avalos 		 * Simply return 1 if we hit those affected laptops to make the
394fa465739SAzael Avalos 		 * supported features work.
395fa465739SAzael Avalos 		 *
396fa465739SAzael Avalos 		 * In the case that some laptops really do not support the SCI,
397fa465739SAzael Avalos 		 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
398fa465739SAzael Avalos 		 * and thus, not registering support for the queried feature.
399fa465739SAzael Avalos 		 */
400fa465739SAzael Avalos 		return 1;
4011864bbc2SAzael Avalos 	} else if (out[0] == TOS_NOT_PRESENT) {
40284a6273fSAzael Avalos 		pr_info("Toshiba SCI is not present\n");
40384a6273fSAzael Avalos 	}
40484a6273fSAzael Avalos 
40584a6273fSAzael Avalos 	return 0;
40684a6273fSAzael Avalos }
40784a6273fSAzael Avalos 
40884a6273fSAzael Avalos static void sci_close(struct toshiba_acpi_dev *dev)
40984a6273fSAzael Avalos {
410258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
411258c5903SAzael Avalos 	u32 out[TCI_WORDS];
41278429e55SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
41384a6273fSAzael Avalos 
4148baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
41584a6273fSAzael Avalos 		pr_err("ACPI call to close SCI failed\n");
41684a6273fSAzael Avalos 		return;
41784a6273fSAzael Avalos 	}
41884a6273fSAzael Avalos 
4191864bbc2SAzael Avalos 	if (out[0] == TOS_OPEN_CLOSE_OK)
42084a6273fSAzael Avalos 		return;
4211864bbc2SAzael Avalos 	else if (out[0] == TOS_NOT_OPENED)
42284a6273fSAzael Avalos 		pr_info("Toshiba SCI not opened\n");
4231864bbc2SAzael Avalos 	else if (out[0] == TOS_NOT_PRESENT)
42484a6273fSAzael Avalos 		pr_info("Toshiba SCI is not present\n");
42584a6273fSAzael Avalos }
42684a6273fSAzael Avalos 
427893f3f62SAzael Avalos static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
42884a6273fSAzael Avalos {
429258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
430258c5903SAzael Avalos 	u32 out[TCI_WORDS];
431258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
432b5163992SAzael Avalos 
433893f3f62SAzael Avalos 	if (ACPI_FAILURE(status))
434893f3f62SAzael Avalos 		return TOS_FAILURE;
435893f3f62SAzael Avalos 
43684a6273fSAzael Avalos 	*out1 = out[2];
437893f3f62SAzael Avalos 
438893f3f62SAzael Avalos 	return out[0];
43984a6273fSAzael Avalos }
44084a6273fSAzael Avalos 
441893f3f62SAzael Avalos static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
44284a6273fSAzael Avalos {
443258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
444258c5903SAzael Avalos 	u32 out[TCI_WORDS];
445258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
446893f3f62SAzael Avalos 
447893f3f62SAzael Avalos 	return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
44884a6273fSAzael Avalos }
44984a6273fSAzael Avalos 
4506c3f6e6cSPierre Ducroquet /* Illumination support */
451ea215a3fSAzael Avalos static void toshiba_illumination_available(struct toshiba_acpi_dev *dev)
4526c3f6e6cSPierre Ducroquet {
453258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
454258c5903SAzael Avalos 	u32 out[TCI_WORDS];
4556c3f6e6cSPierre Ducroquet 	acpi_status status;
4566c3f6e6cSPierre Ducroquet 
457ea215a3fSAzael Avalos 	dev->illumination_supported = 0;
458ea215a3fSAzael Avalos 
459fdb79081SAzael Avalos 	if (!sci_open(dev))
460ea215a3fSAzael Avalos 		return;
461fdb79081SAzael Avalos 
462258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
463fdb79081SAzael Avalos 	sci_close(dev);
464513ee146SAzael Avalos 	if (ACPI_FAILURE(status)) {
465fdb79081SAzael Avalos 		pr_err("ACPI call to query Illumination support failed\n");
466513ee146SAzael Avalos 		return;
467513ee146SAzael Avalos 	}
468513ee146SAzael Avalos 
469513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
470513ee146SAzael Avalos 		return;
471513ee146SAzael Avalos 
472ea215a3fSAzael Avalos 	dev->illumination_supported = 1;
4736c3f6e6cSPierre Ducroquet }
4746c3f6e6cSPierre Ducroquet 
4756c3f6e6cSPierre Ducroquet static void toshiba_illumination_set(struct led_classdev *cdev,
4766c3f6e6cSPierre Ducroquet 				     enum led_brightness brightness)
4776c3f6e6cSPierre Ducroquet {
478135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = container_of(cdev,
479135740deSSeth Forshee 			struct toshiba_acpi_dev, led_dev);
480e1a949c1SAzael Avalos 	u32 result;
481e1a949c1SAzael Avalos 	u32 state;
4826c3f6e6cSPierre Ducroquet 
4836c3f6e6cSPierre Ducroquet 	/* First request : initialize communication. */
484fdb79081SAzael Avalos 	if (!sci_open(dev))
4856c3f6e6cSPierre Ducroquet 		return;
4866c3f6e6cSPierre Ducroquet 
487fdb79081SAzael Avalos 	/* Switch the illumination on/off */
488fdb79081SAzael Avalos 	state = brightness ? 1 : 0;
489893f3f62SAzael Avalos 	result = sci_write(dev, SCI_ILLUMINATION, state);
490fdb79081SAzael Avalos 	sci_close(dev);
491a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
492fdb79081SAzael Avalos 		pr_err("ACPI call for illumination failed\n");
4936c3f6e6cSPierre Ducroquet }
4946c3f6e6cSPierre Ducroquet 
4956c3f6e6cSPierre Ducroquet static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
4966c3f6e6cSPierre Ducroquet {
497135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = container_of(cdev,
498135740deSSeth Forshee 			struct toshiba_acpi_dev, led_dev);
49978429e55SAzael Avalos 	u32 result;
50078429e55SAzael Avalos 	u32 state;
5016c3f6e6cSPierre Ducroquet 
5023f75bbe9SAzael Avalos 	/* First request : initialize communication. */
503fdb79081SAzael Avalos 	if (!sci_open(dev))
5046c3f6e6cSPierre Ducroquet 		return LED_OFF;
5056c3f6e6cSPierre Ducroquet 
5066c3f6e6cSPierre Ducroquet 	/* Check the illumination */
507893f3f62SAzael Avalos 	result = sci_read(dev, SCI_ILLUMINATION, &state);
508fdb79081SAzael Avalos 	sci_close(dev);
509a6b5354fSAzael Avalos 	if (result == TOS_FAILURE) {
510fdb79081SAzael Avalos 		pr_err("ACPI call for illumination failed\n");
511fdb79081SAzael Avalos 		return LED_OFF;
512e1a949c1SAzael Avalos 	} else if (result != TOS_SUCCESS) {
5136c3f6e6cSPierre Ducroquet 		return LED_OFF;
5146c3f6e6cSPierre Ducroquet 	}
5156c3f6e6cSPierre Ducroquet 
516fdb79081SAzael Avalos 	return state ? LED_FULL : LED_OFF;
5176c3f6e6cSPierre Ducroquet }
5186c3f6e6cSPierre Ducroquet 
519360f0f39SAzael Avalos /* KBD Illumination */
520ea215a3fSAzael Avalos static void toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
52193f8c16dSAzael Avalos {
522258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
523258c5903SAzael Avalos 	u32 out[TCI_WORDS];
52493f8c16dSAzael Avalos 	acpi_status status;
52593f8c16dSAzael Avalos 
526ea215a3fSAzael Avalos 	dev->kbd_illum_supported = 0;
52765e3cf9cSAzael Avalos 	dev->kbd_event_generated = false;
528ea215a3fSAzael Avalos 
52993f8c16dSAzael Avalos 	if (!sci_open(dev))
530ea215a3fSAzael Avalos 		return;
53193f8c16dSAzael Avalos 
532258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
53393f8c16dSAzael Avalos 	sci_close(dev);
534a6b5354fSAzael Avalos 	if (ACPI_FAILURE(status)) {
53593f8c16dSAzael Avalos 		pr_err("ACPI call to query kbd illumination support failed\n");
536513ee146SAzael Avalos 		return;
537513ee146SAzael Avalos 	}
538513ee146SAzael Avalos 
539513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
540513ee146SAzael Avalos 		return;
541513ee146SAzael Avalos 
542e0769fe6SDarren Hart 	/*
543e0769fe6SDarren Hart 	 * Check for keyboard backlight timeout max value,
54493f8c16dSAzael Avalos 	 * previous kbd backlight implementation set this to
54593f8c16dSAzael Avalos 	 * 0x3c0003, and now the new implementation set this
546e0769fe6SDarren Hart 	 * to 0x3c001a, use this to distinguish between them.
54793f8c16dSAzael Avalos 	 */
54893f8c16dSAzael Avalos 	if (out[3] == SCI_KBD_TIME_MAX)
54993f8c16dSAzael Avalos 		dev->kbd_type = 2;
55093f8c16dSAzael Avalos 	else
55193f8c16dSAzael Avalos 		dev->kbd_type = 1;
55293f8c16dSAzael Avalos 	/* Get the current keyboard backlight mode */
55393f8c16dSAzael Avalos 	dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
55493f8c16dSAzael Avalos 	/* Get the current time (1-60 seconds) */
55593f8c16dSAzael Avalos 	dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
556ea215a3fSAzael Avalos 	/* Flag as supported */
557ea215a3fSAzael Avalos 	dev->kbd_illum_supported = 1;
558ea215a3fSAzael Avalos }
55993f8c16dSAzael Avalos 
560360f0f39SAzael Avalos static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
561360f0f39SAzael Avalos {
562360f0f39SAzael Avalos 	u32 result;
563360f0f39SAzael Avalos 
564360f0f39SAzael Avalos 	if (!sci_open(dev))
565360f0f39SAzael Avalos 		return -EIO;
566360f0f39SAzael Avalos 
567893f3f62SAzael Avalos 	result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
568360f0f39SAzael Avalos 	sci_close(dev);
569a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
570360f0f39SAzael Avalos 		pr_err("ACPI call to set KBD backlight status failed\n");
571a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
572360f0f39SAzael Avalos 		return -ENODEV;
573360f0f39SAzael Avalos 
574e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
575360f0f39SAzael Avalos }
576360f0f39SAzael Avalos 
577360f0f39SAzael Avalos static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
578360f0f39SAzael Avalos {
579360f0f39SAzael Avalos 	u32 result;
580360f0f39SAzael Avalos 
581360f0f39SAzael Avalos 	if (!sci_open(dev))
582360f0f39SAzael Avalos 		return -EIO;
583360f0f39SAzael Avalos 
584893f3f62SAzael Avalos 	result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
585360f0f39SAzael Avalos 	sci_close(dev);
586a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
587360f0f39SAzael Avalos 		pr_err("ACPI call to get KBD backlight status failed\n");
588a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
589360f0f39SAzael Avalos 		return -ENODEV;
590360f0f39SAzael Avalos 
591e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
592360f0f39SAzael Avalos }
593360f0f39SAzael Avalos 
594360f0f39SAzael Avalos static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
595360f0f39SAzael Avalos {
596360f0f39SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
597360f0f39SAzael Avalos 			struct toshiba_acpi_dev, kbd_led);
598e1a949c1SAzael Avalos 	u32 result;
599e1a949c1SAzael Avalos 	u32 state;
600360f0f39SAzael Avalos 
601360f0f39SAzael Avalos 	/* Check the keyboard backlight state */
602d37782bdSAzael Avalos 	result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
603a6b5354fSAzael Avalos 	if (result == TOS_FAILURE) {
604360f0f39SAzael Avalos 		pr_err("ACPI call to get the keyboard backlight failed\n");
605360f0f39SAzael Avalos 		return LED_OFF;
606e1a949c1SAzael Avalos 	} else if (result != TOS_SUCCESS) {
607360f0f39SAzael Avalos 		return LED_OFF;
608360f0f39SAzael Avalos 	}
609360f0f39SAzael Avalos 
610360f0f39SAzael Avalos 	return state ? LED_FULL : LED_OFF;
611360f0f39SAzael Avalos }
612360f0f39SAzael Avalos 
613360f0f39SAzael Avalos static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
614360f0f39SAzael Avalos 				     enum led_brightness brightness)
615360f0f39SAzael Avalos {
616360f0f39SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
617360f0f39SAzael Avalos 			struct toshiba_acpi_dev, kbd_led);
618e1a949c1SAzael Avalos 	u32 result;
619e1a949c1SAzael Avalos 	u32 state;
620360f0f39SAzael Avalos 
621360f0f39SAzael Avalos 	/* Set the keyboard backlight state */
622360f0f39SAzael Avalos 	state = brightness ? 1 : 0;
623d37782bdSAzael Avalos 	result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
624a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
625360f0f39SAzael Avalos 		pr_err("ACPI call to set KBD Illumination mode failed\n");
626360f0f39SAzael Avalos }
627360f0f39SAzael Avalos 
6289d8658acSAzael Avalos /* TouchPad support */
6299d8658acSAzael Avalos static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
6309d8658acSAzael Avalos {
6319d8658acSAzael Avalos 	u32 result;
6329d8658acSAzael Avalos 
6339d8658acSAzael Avalos 	if (!sci_open(dev))
6349d8658acSAzael Avalos 		return -EIO;
6359d8658acSAzael Avalos 
636893f3f62SAzael Avalos 	result = sci_write(dev, SCI_TOUCHPAD, state);
6379d8658acSAzael Avalos 	sci_close(dev);
638a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
6399d8658acSAzael Avalos 		pr_err("ACPI call to set the touchpad failed\n");
640a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
6419d8658acSAzael Avalos 		return -ENODEV;
6429d8658acSAzael Avalos 
643e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
6449d8658acSAzael Avalos }
6459d8658acSAzael Avalos 
6469d8658acSAzael Avalos static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
6479d8658acSAzael Avalos {
6489d8658acSAzael Avalos 	u32 result;
6499d8658acSAzael Avalos 
6509d8658acSAzael Avalos 	if (!sci_open(dev))
6519d8658acSAzael Avalos 		return -EIO;
6529d8658acSAzael Avalos 
653893f3f62SAzael Avalos 	result = sci_read(dev, SCI_TOUCHPAD, state);
6549d8658acSAzael Avalos 	sci_close(dev);
655a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
6569d8658acSAzael Avalos 		pr_err("ACPI call to query the touchpad failed\n");
657a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
6589d8658acSAzael Avalos 		return -ENODEV;
6599d8658acSAzael Avalos 
660e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
6619d8658acSAzael Avalos }
6629d8658acSAzael Avalos 
663def6c4e2SAzael Avalos /* Eco Mode support */
664ea215a3fSAzael Avalos static void toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
665def6c4e2SAzael Avalos {
66698fc4ec6SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
667258c5903SAzael Avalos 	u32 out[TCI_WORDS];
66878429e55SAzael Avalos 	acpi_status status;
669def6c4e2SAzael Avalos 
670ea215a3fSAzael Avalos 	dev->eco_supported = 0;
671ea215a3fSAzael Avalos 
672258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
6738baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
67498fc4ec6SAzael Avalos 		pr_err("ACPI call to get ECO led failed\n");
675513ee146SAzael Avalos 		return;
676513ee146SAzael Avalos 	}
677513ee146SAzael Avalos 
678513ee146SAzael Avalos 	if (out[0] == TOS_INPUT_DATA_ERROR) {
679e0769fe6SDarren Hart 		/*
680e0769fe6SDarren Hart 		 * If we receive 0x8300 (Input Data Error), it means that the
68198fc4ec6SAzael Avalos 		 * LED device is present, but that we just screwed the input
68298fc4ec6SAzael Avalos 		 * parameters.
68398fc4ec6SAzael Avalos 		 *
68498fc4ec6SAzael Avalos 		 * Let's query the status of the LED to see if we really have a
68598fc4ec6SAzael Avalos 		 * success response, indicating the actual presense of the LED,
68698fc4ec6SAzael Avalos 		 * bail out otherwise.
68798fc4ec6SAzael Avalos 		 */
68898fc4ec6SAzael Avalos 		in[3] = 1;
68998fc4ec6SAzael Avalos 		status = tci_raw(dev, in, out);
690513ee146SAzael Avalos 		if (ACPI_FAILURE(status)) {
69198fc4ec6SAzael Avalos 			pr_err("ACPI call to get ECO led failed\n");
692513ee146SAzael Avalos 			return;
693513ee146SAzael Avalos 		}
694513ee146SAzael Avalos 
695513ee146SAzael Avalos 		if (out[0] != TOS_SUCCESS)
696513ee146SAzael Avalos 			return;
697513ee146SAzael Avalos 
698ea215a3fSAzael Avalos 		dev->eco_supported = 1;
699def6c4e2SAzael Avalos 	}
700def6c4e2SAzael Avalos }
701def6c4e2SAzael Avalos 
702b5163992SAzael Avalos static enum led_brightness
703b5163992SAzael Avalos toshiba_eco_mode_get_status(struct led_classdev *cdev)
704def6c4e2SAzael Avalos {
705def6c4e2SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
706def6c4e2SAzael Avalos 			struct toshiba_acpi_dev, eco_led);
707258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
708258c5903SAzael Avalos 	u32 out[TCI_WORDS];
709def6c4e2SAzael Avalos 	acpi_status status;
710def6c4e2SAzael Avalos 
711258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
712a6b5354fSAzael Avalos 	if (ACPI_FAILURE(status)) {
713def6c4e2SAzael Avalos 		pr_err("ACPI call to get ECO led failed\n");
714def6c4e2SAzael Avalos 		return LED_OFF;
715def6c4e2SAzael Avalos 	}
716def6c4e2SAzael Avalos 
717513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
718513ee146SAzael Avalos 		return LED_OFF;
719513ee146SAzael Avalos 
720def6c4e2SAzael Avalos 	return out[2] ? LED_FULL : LED_OFF;
721def6c4e2SAzael Avalos }
722def6c4e2SAzael Avalos 
723def6c4e2SAzael Avalos static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
724def6c4e2SAzael Avalos 				     enum led_brightness brightness)
725def6c4e2SAzael Avalos {
726def6c4e2SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
727def6c4e2SAzael Avalos 			struct toshiba_acpi_dev, eco_led);
728258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
729258c5903SAzael Avalos 	u32 out[TCI_WORDS];
730def6c4e2SAzael Avalos 	acpi_status status;
731def6c4e2SAzael Avalos 
732def6c4e2SAzael Avalos 	/* Switch the Eco Mode led on/off */
733def6c4e2SAzael Avalos 	in[2] = (brightness) ? 1 : 0;
734258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
735a6b5354fSAzael Avalos 	if (ACPI_FAILURE(status))
736def6c4e2SAzael Avalos 		pr_err("ACPI call to set ECO led failed\n");
737def6c4e2SAzael Avalos }
738def6c4e2SAzael Avalos 
7395a2813e9SAzael Avalos /* Accelerometer support */
740ea215a3fSAzael Avalos static void toshiba_accelerometer_available(struct toshiba_acpi_dev *dev)
7415a2813e9SAzael Avalos {
742258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
743258c5903SAzael Avalos 	u32 out[TCI_WORDS];
7445a2813e9SAzael Avalos 	acpi_status status;
7455a2813e9SAzael Avalos 
746ea215a3fSAzael Avalos 	dev->accelerometer_supported = 0;
747ea215a3fSAzael Avalos 
748e0769fe6SDarren Hart 	/*
749e0769fe6SDarren Hart 	 * Check if the accelerometer call exists,
7505a2813e9SAzael Avalos 	 * this call also serves as initialization
7515a2813e9SAzael Avalos 	 */
752258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
753513ee146SAzael Avalos 	if (ACPI_FAILURE(status)) {
7545a2813e9SAzael Avalos 		pr_err("ACPI call to query the accelerometer failed\n");
755513ee146SAzael Avalos 		return;
756513ee146SAzael Avalos 	}
757513ee146SAzael Avalos 
758513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
759513ee146SAzael Avalos 		return;
760513ee146SAzael Avalos 
761ea215a3fSAzael Avalos 	dev->accelerometer_supported = 1;
7625a2813e9SAzael Avalos }
7635a2813e9SAzael Avalos 
7645a2813e9SAzael Avalos static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
7655a2813e9SAzael Avalos 				     u32 *xy, u32 *z)
7665a2813e9SAzael Avalos {
767258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
768258c5903SAzael Avalos 	u32 out[TCI_WORDS];
7695a2813e9SAzael Avalos 	acpi_status status;
7705a2813e9SAzael Avalos 
7715a2813e9SAzael Avalos 	/* Check the Accelerometer status */
772258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
773a6b5354fSAzael Avalos 	if (ACPI_FAILURE(status)) {
7745a2813e9SAzael Avalos 		pr_err("ACPI call to query the accelerometer failed\n");
7755a2813e9SAzael Avalos 		return -EIO;
7765a2813e9SAzael Avalos 	}
7775a2813e9SAzael Avalos 
778513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
779513ee146SAzael Avalos 		return -ENODEV;
780513ee146SAzael Avalos 
781513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
782e1a949c1SAzael Avalos 		return -EIO;
783513ee146SAzael Avalos 
784513ee146SAzael Avalos 	*xy = out[2];
785513ee146SAzael Avalos 	*z = out[4];
786513ee146SAzael Avalos 
787513ee146SAzael Avalos 	return 0;
788e1a949c1SAzael Avalos }
789e1a949c1SAzael Avalos 
790e26ffe51SAzael Avalos /* Sleep (Charge and Music) utilities support */
791c8c91842SAzael Avalos static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
792c8c91842SAzael Avalos {
793c8c91842SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
794c8c91842SAzael Avalos 	u32 out[TCI_WORDS];
795c8c91842SAzael Avalos 	acpi_status status;
796c8c91842SAzael Avalos 
797c8c91842SAzael Avalos 	dev->usb_sleep_charge_supported = 0;
798c8c91842SAzael Avalos 
799c8c91842SAzael Avalos 	if (!sci_open(dev))
800c8c91842SAzael Avalos 		return;
801c8c91842SAzael Avalos 
802c8c91842SAzael Avalos 	status = tci_raw(dev, in, out);
8038baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
804c8c91842SAzael Avalos 		pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
805c8c91842SAzael Avalos 		sci_close(dev);
806c8c91842SAzael Avalos 		return;
807513ee146SAzael Avalos 	}
808513ee146SAzael Avalos 
809513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS) {
810c8c91842SAzael Avalos 		sci_close(dev);
811c8c91842SAzael Avalos 		return;
812c8c91842SAzael Avalos 	}
813c8c91842SAzael Avalos 
814513ee146SAzael Avalos 	dev->usbsc_mode_base = out[4];
815513ee146SAzael Avalos 
816c8c91842SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
817c8c91842SAzael Avalos 	status = tci_raw(dev, in, out);
818ea215a3fSAzael Avalos 	sci_close(dev);
8198baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
820c8c91842SAzael Avalos 		pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
821513ee146SAzael Avalos 		return;
822513ee146SAzael Avalos 	}
823513ee146SAzael Avalos 
824513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
825513ee146SAzael Avalos 		return;
826513ee146SAzael Avalos 
827c8c91842SAzael Avalos 	dev->usbsc_bat_level = out[2];
828ea215a3fSAzael Avalos 	/* Flag as supported */
829c8c91842SAzael Avalos 	dev->usb_sleep_charge_supported = 1;
830c8c91842SAzael Avalos }
831c8c91842SAzael Avalos 
832e26ffe51SAzael Avalos static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
833e26ffe51SAzael Avalos 					u32 *mode)
834e26ffe51SAzael Avalos {
835e26ffe51SAzael Avalos 	u32 result;
836e26ffe51SAzael Avalos 
837e26ffe51SAzael Avalos 	if (!sci_open(dev))
838e26ffe51SAzael Avalos 		return -EIO;
839e26ffe51SAzael Avalos 
840e26ffe51SAzael Avalos 	result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
841e26ffe51SAzael Avalos 	sci_close(dev);
842a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
843e26ffe51SAzael Avalos 		pr_err("ACPI call to set USB S&C mode failed\n");
844a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
845e26ffe51SAzael Avalos 		return -ENODEV;
846e26ffe51SAzael Avalos 
847e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
848e26ffe51SAzael Avalos }
849e26ffe51SAzael Avalos 
850e26ffe51SAzael Avalos static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
851e26ffe51SAzael Avalos 					u32 mode)
852e26ffe51SAzael Avalos {
853e26ffe51SAzael Avalos 	u32 result;
854e26ffe51SAzael Avalos 
855e26ffe51SAzael Avalos 	if (!sci_open(dev))
856e26ffe51SAzael Avalos 		return -EIO;
857e26ffe51SAzael Avalos 
858e26ffe51SAzael Avalos 	result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
859e26ffe51SAzael Avalos 	sci_close(dev);
860a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
861e26ffe51SAzael Avalos 		pr_err("ACPI call to set USB S&C mode failed\n");
862a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
863e26ffe51SAzael Avalos 		return -ENODEV;
864e26ffe51SAzael Avalos 
865e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
866e26ffe51SAzael Avalos }
867e26ffe51SAzael Avalos 
868182bcaa5SAzael Avalos static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
869182bcaa5SAzael Avalos 					      u32 *mode)
870182bcaa5SAzael Avalos {
871182bcaa5SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
872182bcaa5SAzael Avalos 	u32 out[TCI_WORDS];
873182bcaa5SAzael Avalos 	acpi_status status;
874182bcaa5SAzael Avalos 
875182bcaa5SAzael Avalos 	if (!sci_open(dev))
876182bcaa5SAzael Avalos 		return -EIO;
877182bcaa5SAzael Avalos 
878182bcaa5SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
879182bcaa5SAzael Avalos 	status = tci_raw(dev, in, out);
880182bcaa5SAzael Avalos 	sci_close(dev);
8818baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
882182bcaa5SAzael Avalos 		pr_err("ACPI call to get USB S&C battery level failed\n");
883513ee146SAzael Avalos 		return -EIO;
884182bcaa5SAzael Avalos 	}
885182bcaa5SAzael Avalos 
886513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
887513ee146SAzael Avalos 		return -ENODEV;
888513ee146SAzael Avalos 
889513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
890e1a949c1SAzael Avalos 		return -EIO;
891513ee146SAzael Avalos 
892513ee146SAzael Avalos 	*mode = out[2];
893513ee146SAzael Avalos 
894513ee146SAzael Avalos 	return 0;
895513ee146SAzael Avalos 
896182bcaa5SAzael Avalos }
897182bcaa5SAzael Avalos 
898182bcaa5SAzael Avalos static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
899182bcaa5SAzael Avalos 					      u32 mode)
900182bcaa5SAzael Avalos {
901182bcaa5SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
902182bcaa5SAzael Avalos 	u32 out[TCI_WORDS];
903182bcaa5SAzael Avalos 	acpi_status status;
904182bcaa5SAzael Avalos 
905182bcaa5SAzael Avalos 	if (!sci_open(dev))
906182bcaa5SAzael Avalos 		return -EIO;
907182bcaa5SAzael Avalos 
908182bcaa5SAzael Avalos 	in[2] = mode;
909182bcaa5SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
910182bcaa5SAzael Avalos 	status = tci_raw(dev, in, out);
911182bcaa5SAzael Avalos 	sci_close(dev);
912513ee146SAzael Avalos 	if (ACPI_FAILURE(status)) {
913182bcaa5SAzael Avalos 		pr_err("ACPI call to set USB S&C battery level failed\n");
914513ee146SAzael Avalos 		return -EIO;
915513ee146SAzael Avalos 	}
916513ee146SAzael Avalos 
917513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
918182bcaa5SAzael Avalos 		return -ENODEV;
919182bcaa5SAzael Avalos 
920e1a949c1SAzael Avalos 	return out[0] == TOS_SUCCESS ? 0 : -EIO;
921182bcaa5SAzael Avalos }
922182bcaa5SAzael Avalos 
923bb3fe01fSAzael Avalos static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
924bb3fe01fSAzael Avalos 					u32 *state)
925bb3fe01fSAzael Avalos {
926bb3fe01fSAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
927bb3fe01fSAzael Avalos 	u32 out[TCI_WORDS];
928bb3fe01fSAzael Avalos 	acpi_status status;
929bb3fe01fSAzael Avalos 
930bb3fe01fSAzael Avalos 	if (!sci_open(dev))
931bb3fe01fSAzael Avalos 		return -EIO;
932bb3fe01fSAzael Avalos 
933bb3fe01fSAzael Avalos 	in[5] = SCI_USB_CHARGE_RAPID_DSP;
934bb3fe01fSAzael Avalos 	status = tci_raw(dev, in, out);
935bb3fe01fSAzael Avalos 	sci_close(dev);
9368baec45dSAzael Avalos 	if (ACPI_FAILURE(status)) {
937bb26f189SAzael Avalos 		pr_err("ACPI call to get USB Rapid Charge failed\n");
938513ee146SAzael Avalos 		return -EIO;
939bb3fe01fSAzael Avalos 	}
940bb3fe01fSAzael Avalos 
941513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
942513ee146SAzael Avalos 		return -ENODEV;
943513ee146SAzael Avalos 
944513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
945e1a949c1SAzael Avalos 		return -EIO;
946513ee146SAzael Avalos 
947513ee146SAzael Avalos 	*state = out[2];
948513ee146SAzael Avalos 
949513ee146SAzael Avalos 	return 0;
950bb3fe01fSAzael Avalos }
951bb3fe01fSAzael Avalos 
952bb3fe01fSAzael Avalos static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
953bb3fe01fSAzael Avalos 					u32 state)
954bb3fe01fSAzael Avalos {
955bb3fe01fSAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
956bb3fe01fSAzael Avalos 	u32 out[TCI_WORDS];
957bb3fe01fSAzael Avalos 	acpi_status status;
958bb3fe01fSAzael Avalos 
959bb3fe01fSAzael Avalos 	if (!sci_open(dev))
960bb3fe01fSAzael Avalos 		return -EIO;
961bb3fe01fSAzael Avalos 
962bb3fe01fSAzael Avalos 	in[2] = state;
963bb3fe01fSAzael Avalos 	in[5] = SCI_USB_CHARGE_RAPID_DSP;
964bb3fe01fSAzael Avalos 	status = tci_raw(dev, in, out);
965bb3fe01fSAzael Avalos 	sci_close(dev);
966513ee146SAzael Avalos 	if (ACPI_FAILURE(status)) {
967bb26f189SAzael Avalos 		pr_err("ACPI call to set USB Rapid Charge failed\n");
968513ee146SAzael Avalos 		return -EIO;
969513ee146SAzael Avalos 	}
970513ee146SAzael Avalos 
971513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
972bb3fe01fSAzael Avalos 		return -ENODEV;
973bb3fe01fSAzael Avalos 
974e1a949c1SAzael Avalos 	return (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) ? 0 : -EIO;
975bb3fe01fSAzael Avalos }
976bb3fe01fSAzael Avalos 
977172ce0a9SAzael Avalos static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
978172ce0a9SAzael Avalos {
979172ce0a9SAzael Avalos 	u32 result;
980172ce0a9SAzael Avalos 
981172ce0a9SAzael Avalos 	if (!sci_open(dev))
982172ce0a9SAzael Avalos 		return -EIO;
983172ce0a9SAzael Avalos 
984172ce0a9SAzael Avalos 	result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
985172ce0a9SAzael Avalos 	sci_close(dev);
986a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
987bb26f189SAzael Avalos 		pr_err("ACPI call to get Sleep and Music failed\n");
988a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
989172ce0a9SAzael Avalos 		return -ENODEV;
990172ce0a9SAzael Avalos 
991cf680eaeSAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
992172ce0a9SAzael Avalos }
993172ce0a9SAzael Avalos 
994172ce0a9SAzael Avalos static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
995172ce0a9SAzael Avalos {
996172ce0a9SAzael Avalos 	u32 result;
997172ce0a9SAzael Avalos 
998172ce0a9SAzael Avalos 	if (!sci_open(dev))
999172ce0a9SAzael Avalos 		return -EIO;
1000172ce0a9SAzael Avalos 
1001172ce0a9SAzael Avalos 	result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1002172ce0a9SAzael Avalos 	sci_close(dev);
1003a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
1004bb26f189SAzael Avalos 		pr_err("ACPI call to set Sleep and Music failed\n");
1005a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1006172ce0a9SAzael Avalos 		return -ENODEV;
1007172ce0a9SAzael Avalos 
1008e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
1009172ce0a9SAzael Avalos }
1010172ce0a9SAzael Avalos 
1011bae84195SAzael Avalos /* Keyboard function keys */
1012bae84195SAzael Avalos static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1013bae84195SAzael Avalos {
1014bae84195SAzael Avalos 	u32 result;
1015bae84195SAzael Avalos 
1016bae84195SAzael Avalos 	if (!sci_open(dev))
1017bae84195SAzael Avalos 		return -EIO;
1018bae84195SAzael Avalos 
1019bae84195SAzael Avalos 	result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1020bae84195SAzael Avalos 	sci_close(dev);
1021a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
1022bae84195SAzael Avalos 		pr_err("ACPI call to get KBD function keys failed\n");
1023a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1024bae84195SAzael Avalos 		return -ENODEV;
1025bae84195SAzael Avalos 
1026e1a949c1SAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1027bae84195SAzael Avalos }
1028bae84195SAzael Avalos 
1029bae84195SAzael Avalos static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1030bae84195SAzael Avalos {
1031bae84195SAzael Avalos 	u32 result;
1032bae84195SAzael Avalos 
1033bae84195SAzael Avalos 	if (!sci_open(dev))
1034bae84195SAzael Avalos 		return -EIO;
1035bae84195SAzael Avalos 
1036bae84195SAzael Avalos 	result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1037bae84195SAzael Avalos 	sci_close(dev);
1038a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
1039bae84195SAzael Avalos 		pr_err("ACPI call to set KBD function keys failed\n");
1040a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1041bae84195SAzael Avalos 		return -ENODEV;
1042bae84195SAzael Avalos 
1043e1a949c1SAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1044bae84195SAzael Avalos }
1045bae84195SAzael Avalos 
104635d53ceaSAzael Avalos /* Panel Power ON */
104735d53ceaSAzael Avalos static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
104835d53ceaSAzael Avalos {
104935d53ceaSAzael Avalos 	u32 result;
105035d53ceaSAzael Avalos 
105135d53ceaSAzael Avalos 	if (!sci_open(dev))
105235d53ceaSAzael Avalos 		return -EIO;
105335d53ceaSAzael Avalos 
105435d53ceaSAzael Avalos 	result = sci_read(dev, SCI_PANEL_POWER_ON, state);
105535d53ceaSAzael Avalos 	sci_close(dev);
1056a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
105735d53ceaSAzael Avalos 		pr_err("ACPI call to get Panel Power ON failed\n");
1058a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
105935d53ceaSAzael Avalos 		return -ENODEV;
106035d53ceaSAzael Avalos 
1061e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
106235d53ceaSAzael Avalos }
106335d53ceaSAzael Avalos 
106435d53ceaSAzael Avalos static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
106535d53ceaSAzael Avalos {
106635d53ceaSAzael Avalos 	u32 result;
106735d53ceaSAzael Avalos 
106835d53ceaSAzael Avalos 	if (!sci_open(dev))
106935d53ceaSAzael Avalos 		return -EIO;
107035d53ceaSAzael Avalos 
107135d53ceaSAzael Avalos 	result = sci_write(dev, SCI_PANEL_POWER_ON, state);
107235d53ceaSAzael Avalos 	sci_close(dev);
1073a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
107435d53ceaSAzael Avalos 		pr_err("ACPI call to set Panel Power ON failed\n");
1075a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
107635d53ceaSAzael Avalos 		return -ENODEV;
107735d53ceaSAzael Avalos 
1078e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
107935d53ceaSAzael Avalos }
108035d53ceaSAzael Avalos 
108117fe4b3dSAzael Avalos /* USB Three */
108217fe4b3dSAzael Avalos static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
108317fe4b3dSAzael Avalos {
108417fe4b3dSAzael Avalos 	u32 result;
108517fe4b3dSAzael Avalos 
108617fe4b3dSAzael Avalos 	if (!sci_open(dev))
108717fe4b3dSAzael Avalos 		return -EIO;
108817fe4b3dSAzael Avalos 
108917fe4b3dSAzael Avalos 	result = sci_read(dev, SCI_USB_THREE, state);
109017fe4b3dSAzael Avalos 	sci_close(dev);
1091a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
109217fe4b3dSAzael Avalos 		pr_err("ACPI call to get USB 3 failed\n");
1093a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
109417fe4b3dSAzael Avalos 		return -ENODEV;
109517fe4b3dSAzael Avalos 
1096e1a949c1SAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
109717fe4b3dSAzael Avalos }
109817fe4b3dSAzael Avalos 
109917fe4b3dSAzael Avalos static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
110017fe4b3dSAzael Avalos {
110117fe4b3dSAzael Avalos 	u32 result;
110217fe4b3dSAzael Avalos 
110317fe4b3dSAzael Avalos 	if (!sci_open(dev))
110417fe4b3dSAzael Avalos 		return -EIO;
110517fe4b3dSAzael Avalos 
110617fe4b3dSAzael Avalos 	result = sci_write(dev, SCI_USB_THREE, state);
110717fe4b3dSAzael Avalos 	sci_close(dev);
1108a6b5354fSAzael Avalos 	if (result == TOS_FAILURE)
110917fe4b3dSAzael Avalos 		pr_err("ACPI call to set USB 3 failed\n");
1110a6b5354fSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
111117fe4b3dSAzael Avalos 		return -ENODEV;
111217fe4b3dSAzael Avalos 
1113e1a949c1SAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
111417fe4b3dSAzael Avalos }
111517fe4b3dSAzael Avalos 
111656e6b353SAzael Avalos /* Hotkey Event type */
111756e6b353SAzael Avalos static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
111856e6b353SAzael Avalos 					 u32 *type)
111956e6b353SAzael Avalos {
11203b876000SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
11213b876000SAzael Avalos 	u32 out[TCI_WORDS];
11223b876000SAzael Avalos 	acpi_status status;
112356e6b353SAzael Avalos 
11243b876000SAzael Avalos 	status = tci_raw(dev, in, out);
11253b876000SAzael Avalos 	if (ACPI_FAILURE(status)) {
112656e6b353SAzael Avalos 		pr_err("ACPI call to get System type failed\n");
1127513ee146SAzael Avalos 		return -EIO;
112856e6b353SAzael Avalos 	}
112956e6b353SAzael Avalos 
1130513ee146SAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
1131513ee146SAzael Avalos 		return -ENODEV;
1132513ee146SAzael Avalos 
1133513ee146SAzael Avalos 	if (out[0] != TOS_SUCCESS)
1134e1a949c1SAzael Avalos 		return -EIO;
1135513ee146SAzael Avalos 
1136513ee146SAzael Avalos 	*type = out[3];
1137513ee146SAzael Avalos 
1138513ee146SAzael Avalos 	return 0;
113956e6b353SAzael Avalos }
114056e6b353SAzael Avalos 
11416873f46aSAzael Avalos /* Wireless status (RFKill, WLAN, BT, WWAN) */
11426873f46aSAzael Avalos static int toshiba_wireless_status(struct toshiba_acpi_dev *dev)
11436873f46aSAzael Avalos {
11446873f46aSAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
11456873f46aSAzael Avalos 	u32 out[TCI_WORDS];
11466873f46aSAzael Avalos 	acpi_status status;
11476873f46aSAzael Avalos 
11486873f46aSAzael Avalos 	in[3] = HCI_WIRELESS_STATUS;
11496873f46aSAzael Avalos 	status = tci_raw(dev, in, out);
11506873f46aSAzael Avalos 
11516873f46aSAzael Avalos 	if (ACPI_FAILURE(status)) {
11526873f46aSAzael Avalos 		pr_err("ACPI call to get Wireless status failed\n");
11536873f46aSAzael Avalos 		return -EIO;
11546873f46aSAzael Avalos 	}
11556873f46aSAzael Avalos 
11566873f46aSAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
11576873f46aSAzael Avalos 		return -ENODEV;
11586873f46aSAzael Avalos 
11596873f46aSAzael Avalos 	if (out[0] != TOS_SUCCESS)
11606873f46aSAzael Avalos 		return -EIO;
11616873f46aSAzael Avalos 
11626873f46aSAzael Avalos 	dev->killswitch = !!(out[2] & HCI_WIRELESS_STATUS);
11636873f46aSAzael Avalos 
11646873f46aSAzael Avalos 	return 0;
11656873f46aSAzael Avalos }
11666873f46aSAzael Avalos 
11676873f46aSAzael Avalos /* WWAN */
11686873f46aSAzael Avalos static void toshiba_wwan_available(struct toshiba_acpi_dev *dev)
11696873f46aSAzael Avalos {
11706873f46aSAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
11716873f46aSAzael Avalos 	u32 out[TCI_WORDS];
11726873f46aSAzael Avalos 	acpi_status status;
11736873f46aSAzael Avalos 
11746873f46aSAzael Avalos 	dev->wwan_supported = 0;
11756873f46aSAzael Avalos 
11766873f46aSAzael Avalos 	/*
11776873f46aSAzael Avalos 	 * WWAN support can be queried by setting the in[3] value to
11786873f46aSAzael Avalos 	 * HCI_WIRELESS_WWAN (0x03).
11796873f46aSAzael Avalos 	 *
11806873f46aSAzael Avalos 	 * If supported, out[0] contains TOS_SUCCESS and out[2] contains
11816873f46aSAzael Avalos 	 * HCI_WIRELESS_WWAN_STATUS (0x2000).
11826873f46aSAzael Avalos 	 *
11836873f46aSAzael Avalos 	 * If not supported, out[0] contains TOS_INPUT_DATA_ERROR (0x8300)
11846873f46aSAzael Avalos 	 * or TOS_NOT_SUPPORTED (0x8000).
11856873f46aSAzael Avalos 	 */
11866873f46aSAzael Avalos 	in[3] = HCI_WIRELESS_WWAN;
11876873f46aSAzael Avalos 	status = tci_raw(dev, in, out);
11886873f46aSAzael Avalos 	if (ACPI_FAILURE(status)) {
11896873f46aSAzael Avalos 		pr_err("ACPI call to get WWAN status failed\n");
11906873f46aSAzael Avalos 		return;
11916873f46aSAzael Avalos 	}
11926873f46aSAzael Avalos 
11936873f46aSAzael Avalos 	if (out[0] != TOS_SUCCESS)
11946873f46aSAzael Avalos 		return;
11956873f46aSAzael Avalos 
11966873f46aSAzael Avalos 	dev->wwan_supported = (out[2] == HCI_WIRELESS_WWAN_STATUS);
11976873f46aSAzael Avalos }
11986873f46aSAzael Avalos 
11996873f46aSAzael Avalos static int toshiba_wwan_set(struct toshiba_acpi_dev *dev, u32 state)
12006873f46aSAzael Avalos {
12016873f46aSAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, HCI_WIRELESS, state, 0, 0, 0 };
12026873f46aSAzael Avalos 	u32 out[TCI_WORDS];
12036873f46aSAzael Avalos 	acpi_status status;
12046873f46aSAzael Avalos 
12056873f46aSAzael Avalos 	in[3] = HCI_WIRELESS_WWAN_STATUS;
12066873f46aSAzael Avalos 	status = tci_raw(dev, in, out);
12076873f46aSAzael Avalos 	if (ACPI_FAILURE(status)) {
12086873f46aSAzael Avalos 		pr_err("ACPI call to set WWAN status failed\n");
12096873f46aSAzael Avalos 		return -EIO;
12106873f46aSAzael Avalos 	}
12116873f46aSAzael Avalos 
12126873f46aSAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
12136873f46aSAzael Avalos 		return -ENODEV;
12146873f46aSAzael Avalos 
12156873f46aSAzael Avalos 	if (out[0] != TOS_SUCCESS)
12166873f46aSAzael Avalos 		return -EIO;
12176873f46aSAzael Avalos 
12186873f46aSAzael Avalos 	/*
12196873f46aSAzael Avalos 	 * Some devices only need to call HCI_WIRELESS_WWAN_STATUS to
12206873f46aSAzael Avalos 	 * (de)activate the device, but some others need the
12216873f46aSAzael Avalos 	 * HCI_WIRELESS_WWAN_POWER call as well.
12226873f46aSAzael Avalos 	 */
12236873f46aSAzael Avalos 	in[3] = HCI_WIRELESS_WWAN_POWER;
12246873f46aSAzael Avalos 	status = tci_raw(dev, in, out);
12256873f46aSAzael Avalos 	if (ACPI_FAILURE(status)) {
12266873f46aSAzael Avalos 		pr_err("ACPI call to set WWAN power failed\n");
12276873f46aSAzael Avalos 		return -EIO;
12286873f46aSAzael Avalos 	}
12296873f46aSAzael Avalos 
12306873f46aSAzael Avalos 	if (out[0] == TOS_NOT_SUPPORTED)
12316873f46aSAzael Avalos 		return -ENODEV;
12326873f46aSAzael Avalos 
12336873f46aSAzael Avalos 	return out[0] == TOS_SUCCESS ? 0 : -EIO;
12346873f46aSAzael Avalos }
12356873f46aSAzael Avalos 
1236763ff32fSAzael Avalos /* Cooling Method */
1237763ff32fSAzael Avalos static void toshiba_cooling_method_available(struct toshiba_acpi_dev *dev)
1238763ff32fSAzael Avalos {
1239763ff32fSAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_COOLING_METHOD, 0, 0, 0, 0 };
1240763ff32fSAzael Avalos 	u32 out[TCI_WORDS];
1241763ff32fSAzael Avalos 	acpi_status status;
1242763ff32fSAzael Avalos 
1243763ff32fSAzael Avalos 	dev->cooling_method_supported = 0;
1244763ff32fSAzael Avalos 	dev->max_cooling_method = 0;
1245763ff32fSAzael Avalos 
1246763ff32fSAzael Avalos 	status = tci_raw(dev, in, out);
1247513ee146SAzael Avalos 	if (ACPI_FAILURE(status)) {
1248763ff32fSAzael Avalos 		pr_err("ACPI call to get Cooling Method failed\n");
1249513ee146SAzael Avalos 		return;
1250513ee146SAzael Avalos 	}
1251763ff32fSAzael Avalos 
1252763ff32fSAzael Avalos 	if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
1253763ff32fSAzael Avalos 		return;
1254763ff32fSAzael Avalos 
1255763ff32fSAzael Avalos 	dev->cooling_method_supported = 1;
1256763ff32fSAzael Avalos 	dev->max_cooling_method = out[3];
1257763ff32fSAzael Avalos }
1258763ff32fSAzael Avalos 
1259763ff32fSAzael Avalos static int toshiba_cooling_method_get(struct toshiba_acpi_dev *dev, u32 *state)
1260763ff32fSAzael Avalos {
1261763ff32fSAzael Avalos 	u32 result = hci_read(dev, HCI_COOLING_METHOD, state);
1262763ff32fSAzael Avalos 
1263763ff32fSAzael Avalos 	if (result == TOS_FAILURE)
1264763ff32fSAzael Avalos 		pr_err("ACPI call to get Cooling Method failed\n");
1265763ff32fSAzael Avalos 
1266763ff32fSAzael Avalos 	if (result == TOS_NOT_SUPPORTED)
1267763ff32fSAzael Avalos 		return -ENODEV;
1268763ff32fSAzael Avalos 
1269763ff32fSAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1270763ff32fSAzael Avalos }
1271763ff32fSAzael Avalos 
1272763ff32fSAzael Avalos static int toshiba_cooling_method_set(struct toshiba_acpi_dev *dev, u32 state)
1273763ff32fSAzael Avalos {
1274763ff32fSAzael Avalos 	u32 result = hci_write(dev, HCI_COOLING_METHOD, state);
1275763ff32fSAzael Avalos 
1276763ff32fSAzael Avalos 	if (result == TOS_FAILURE)
1277fa1bc2a0SAzael Avalos 		pr_err("ACPI call to set Cooling Method failed\n");
1278763ff32fSAzael Avalos 
1279763ff32fSAzael Avalos 	if (result == TOS_NOT_SUPPORTED)
1280763ff32fSAzael Avalos 		return -ENODEV;
1281763ff32fSAzael Avalos 
1282763ff32fSAzael Avalos 	return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1283763ff32fSAzael Avalos }
1284763ff32fSAzael Avalos 
12853f75bbe9SAzael Avalos /* Transflective Backlight */
1286695f6060SAzael Avalos static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
1287121b7b0dSAkio Idehara {
1288e1a949c1SAzael Avalos 	u32 result = hci_read(dev, HCI_TR_BACKLIGHT, status);
1289121b7b0dSAkio Idehara 
1290e1a949c1SAzael Avalos 	if (result == TOS_FAILURE)
1291e1a949c1SAzael Avalos 		pr_err("ACPI call to get Transflective Backlight failed\n");
1292e1a949c1SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1293e1a949c1SAzael Avalos 		return -ENODEV;
1294e1a949c1SAzael Avalos 
1295e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
1296121b7b0dSAkio Idehara }
1297121b7b0dSAkio Idehara 
1298695f6060SAzael Avalos static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
1299121b7b0dSAkio Idehara {
1300e1a949c1SAzael Avalos 	u32 result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
1301121b7b0dSAkio Idehara 
1302e1a949c1SAzael Avalos 	if (result == TOS_FAILURE)
1303e1a949c1SAzael Avalos 		pr_err("ACPI call to set Transflective Backlight failed\n");
1304e1a949c1SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1305e1a949c1SAzael Avalos 		return -ENODEV;
1306e1a949c1SAzael Avalos 
1307e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
1308121b7b0dSAkio Idehara }
1309121b7b0dSAkio Idehara 
13103f75bbe9SAzael Avalos static struct proc_dir_entry *toshiba_proc_dir;
1311b4f9fe12SLen Brown 
13123f75bbe9SAzael Avalos /* LCD Brightness */
131362cce752SSeth Forshee static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1314b4f9fe12SLen Brown {
131578429e55SAzael Avalos 	int brightness = 0;
1316e1a949c1SAzael Avalos 	u32 result;
1317b4f9fe12SLen Brown 	u32 value;
1318121b7b0dSAkio Idehara 
1319121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported) {
1320695f6060SAzael Avalos 		int ret = get_tr_backlight_status(dev, &value);
1321b5163992SAzael Avalos 
1322121b7b0dSAkio Idehara 		if (ret)
1323121b7b0dSAkio Idehara 			return ret;
1324695f6060SAzael Avalos 		if (value)
1325121b7b0dSAkio Idehara 			return 0;
1326121b7b0dSAkio Idehara 		brightness++;
1327121b7b0dSAkio Idehara 	}
1328b4f9fe12SLen Brown 
1329e1a949c1SAzael Avalos 	result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1330e1a949c1SAzael Avalos 	if (result == TOS_FAILURE)
1331e1a949c1SAzael Avalos 		pr_err("ACPI call to get LCD Brightness failed\n");
1332e1a949c1SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1333e1a949c1SAzael Avalos 		return -ENODEV;
133432bcd5cbSSeth Forshee 
1335513ee146SAzael Avalos 	return result == TOS_SUCCESS ?
1336513ee146SAzael Avalos 			brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT) :
1337513ee146SAzael Avalos 			-EIO;
1338b4f9fe12SLen Brown }
1339b4f9fe12SLen Brown 
134062cce752SSeth Forshee static int get_lcd_brightness(struct backlight_device *bd)
134162cce752SSeth Forshee {
134262cce752SSeth Forshee 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
1343b5163992SAzael Avalos 
134462cce752SSeth Forshee 	return __get_lcd_brightness(dev);
134562cce752SSeth Forshee }
134662cce752SSeth Forshee 
1347936c8bcdSAlexey Dobriyan static int lcd_proc_show(struct seq_file *m, void *v)
1348b4f9fe12SLen Brown {
1349135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1350121b7b0dSAkio Idehara 	int levels;
1351e1a949c1SAzael Avalos 	int value;
1352b4f9fe12SLen Brown 
1353135740deSSeth Forshee 	if (!dev->backlight_dev)
1354135740deSSeth Forshee 		return -ENODEV;
1355135740deSSeth Forshee 
1356121b7b0dSAkio Idehara 	levels = dev->backlight_dev->props.max_brightness + 1;
135762cce752SSeth Forshee 	value = get_lcd_brightness(dev->backlight_dev);
1358513ee146SAzael Avalos 	if (value < 0) {
1359513ee146SAzael Avalos 		pr_err("Error reading LCD brightness\n");
1360513ee146SAzael Avalos 		return value;
1361b4f9fe12SLen Brown 	}
1362b4f9fe12SLen Brown 
1363513ee146SAzael Avalos 	seq_printf(m, "brightness:              %d\n", value);
1364513ee146SAzael Avalos 	seq_printf(m, "brightness_levels:       %d\n", levels);
1365e1a949c1SAzael Avalos 
1366513ee146SAzael Avalos 	return 0;
1367936c8bcdSAlexey Dobriyan }
1368936c8bcdSAlexey Dobriyan 
1369936c8bcdSAlexey Dobriyan static int lcd_proc_open(struct inode *inode, struct file *file)
1370936c8bcdSAlexey Dobriyan {
1371d9dda78bSAl Viro 	return single_open(file, lcd_proc_show, PDE_DATA(inode));
1372b4f9fe12SLen Brown }
1373b4f9fe12SLen Brown 
137462cce752SSeth Forshee static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1375b4f9fe12SLen Brown {
1376e1a949c1SAzael Avalos 	u32 result;
1377b4f9fe12SLen Brown 
1378121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported) {
1379695f6060SAzael Avalos 		int ret = set_tr_backlight_status(dev, !value);
1380b5163992SAzael Avalos 
1381121b7b0dSAkio Idehara 		if (ret)
1382121b7b0dSAkio Idehara 			return ret;
1383121b7b0dSAkio Idehara 		if (value)
1384121b7b0dSAkio Idehara 			value--;
1385121b7b0dSAkio Idehara 	}
1386121b7b0dSAkio Idehara 
1387a39f46dfSAzael Avalos 	value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1388e1a949c1SAzael Avalos 	result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1389e1a949c1SAzael Avalos 	if (result == TOS_FAILURE)
1390e1a949c1SAzael Avalos 		pr_err("ACPI call to set LCD Brightness failed\n");
1391e1a949c1SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1392e1a949c1SAzael Avalos 		return -ENODEV;
1393e1a949c1SAzael Avalos 
1394e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
1395b4f9fe12SLen Brown }
1396b4f9fe12SLen Brown 
1397b4f9fe12SLen Brown static int set_lcd_status(struct backlight_device *bd)
1398b4f9fe12SLen Brown {
1399135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
1400b5163992SAzael Avalos 
140162cce752SSeth Forshee 	return set_lcd_brightness(dev, bd->props.brightness);
1402b4f9fe12SLen Brown }
1403b4f9fe12SLen Brown 
1404936c8bcdSAlexey Dobriyan static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1405936c8bcdSAlexey Dobriyan 			      size_t count, loff_t *pos)
1406b4f9fe12SLen Brown {
1407d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1408936c8bcdSAlexey Dobriyan 	char cmd[42];
1409936c8bcdSAlexey Dobriyan 	size_t len;
141078429e55SAzael Avalos 	int levels;
1411e1a949c1SAzael Avalos 	int value;
1412b4f9fe12SLen Brown 
1413936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1414936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1415936c8bcdSAlexey Dobriyan 		return -EFAULT;
1416936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1417936c8bcdSAlexey Dobriyan 
141878429e55SAzael Avalos 	levels = dev->backlight_dev->props.max_brightness + 1;
1419e1a949c1SAzael Avalos 	if (sscanf(cmd, " brightness : %i", &value) != 1 &&
1420e1a949c1SAzael Avalos 	    value < 0 && value > levels)
1421e1a949c1SAzael Avalos 		return -EINVAL;
1422e1a949c1SAzael Avalos 
1423e1a949c1SAzael Avalos 	if (set_lcd_brightness(dev, value))
1424e1a949c1SAzael Avalos 		return -EIO;
1425e1a949c1SAzael Avalos 
1426e1a949c1SAzael Avalos 	return count;
1427b4f9fe12SLen Brown }
1428b4f9fe12SLen Brown 
142997a32539SAlexey Dobriyan static const struct proc_ops lcd_proc_ops = {
143097a32539SAlexey Dobriyan 	.proc_open	= lcd_proc_open,
143197a32539SAlexey Dobriyan 	.proc_read	= seq_read,
143297a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
143397a32539SAlexey Dobriyan 	.proc_release	= single_release,
143497a32539SAlexey Dobriyan 	.proc_write	= lcd_proc_write,
1435936c8bcdSAlexey Dobriyan };
1436936c8bcdSAlexey Dobriyan 
1437e1a949c1SAzael Avalos /* Video-Out */
143836d03f93SSeth Forshee static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
143936d03f93SSeth Forshee {
1440e1a949c1SAzael Avalos 	u32 result = hci_read(dev, HCI_VIDEO_OUT, status);
144136d03f93SSeth Forshee 
1442e1a949c1SAzael Avalos 	if (result == TOS_FAILURE)
1443e1a949c1SAzael Avalos 		pr_err("ACPI call to get Video-Out failed\n");
1444e1a949c1SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
1445e1a949c1SAzael Avalos 		return -ENODEV;
1446e1a949c1SAzael Avalos 
1447e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
144836d03f93SSeth Forshee }
144936d03f93SSeth Forshee 
1450936c8bcdSAlexey Dobriyan static int video_proc_show(struct seq_file *m, void *v)
1451b4f9fe12SLen Brown {
1452135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1453513ee146SAzael Avalos 	int is_lcd, is_crt, is_tv;
1454b4f9fe12SLen Brown 	u32 value;
1455b4f9fe12SLen Brown 
1456513ee146SAzael Avalos 	if (get_video_status(dev, &value))
1457513ee146SAzael Avalos 		return -EIO;
1458513ee146SAzael Avalos 
1459513ee146SAzael Avalos 	is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1460513ee146SAzael Avalos 	is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1461513ee146SAzael Avalos 	is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1462b5163992SAzael Avalos 
1463936c8bcdSAlexey Dobriyan 	seq_printf(m, "lcd_out:                 %d\n", is_lcd);
1464936c8bcdSAlexey Dobriyan 	seq_printf(m, "crt_out:                 %d\n", is_crt);
1465936c8bcdSAlexey Dobriyan 	seq_printf(m, "tv_out:                  %d\n", is_tv);
1466b4f9fe12SLen Brown 
1467513ee146SAzael Avalos 	return 0;
1468b4f9fe12SLen Brown }
1469b4f9fe12SLen Brown 
1470936c8bcdSAlexey Dobriyan static int video_proc_open(struct inode *inode, struct file *file)
1471b4f9fe12SLen Brown {
1472d9dda78bSAl Viro 	return single_open(file, video_proc_show, PDE_DATA(inode));
1473936c8bcdSAlexey Dobriyan }
1474936c8bcdSAlexey Dobriyan 
1475936c8bcdSAlexey Dobriyan static ssize_t video_proc_write(struct file *file, const char __user *buf,
1476936c8bcdSAlexey Dobriyan 				size_t count, loff_t *pos)
1477936c8bcdSAlexey Dobriyan {
1478d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1479e1a949c1SAzael Avalos 	char *buffer;
1480e1a949c1SAzael Avalos 	char *cmd;
148178429e55SAzael Avalos 	int lcd_out, crt_out, tv_out;
1482b4f9fe12SLen Brown 	int remain = count;
1483e1a949c1SAzael Avalos 	int value;
1484e1a949c1SAzael Avalos 	int ret;
1485b4f9fe12SLen Brown 	u32 video_out;
1486b4f9fe12SLen Brown 
1487f0ee1a6dSGeliang Tang 	cmd = memdup_user_nul(buf, count);
1488f0ee1a6dSGeliang Tang 	if (IS_ERR(cmd))
1489f0ee1a6dSGeliang Tang 		return PTR_ERR(cmd);
1490936c8bcdSAlexey Dobriyan 
1491936c8bcdSAlexey Dobriyan 	buffer = cmd;
1492936c8bcdSAlexey Dobriyan 
1493e0769fe6SDarren Hart 	/*
1494e0769fe6SDarren Hart 	 * Scan expression.  Multiple expressions may be delimited with ;
1495e0769fe6SDarren Hart 	 * NOTE: To keep scanning simple, invalid fields are ignored.
1496b4f9fe12SLen Brown 	 */
1497b4f9fe12SLen Brown 	while (remain) {
1498b4f9fe12SLen Brown 		if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1499b4f9fe12SLen Brown 			lcd_out = value & 1;
1500b4f9fe12SLen Brown 		else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1501b4f9fe12SLen Brown 			crt_out = value & 1;
1502b4f9fe12SLen Brown 		else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1503b4f9fe12SLen Brown 			tv_out = value & 1;
1504e0769fe6SDarren Hart 		/* Advance to one character past the next ; */
1505b4f9fe12SLen Brown 		do {
1506b4f9fe12SLen Brown 			++buffer;
1507b4f9fe12SLen Brown 			--remain;
1508b5163992SAzael Avalos 		} while (remain && *(buffer - 1) != ';');
1509b4f9fe12SLen Brown 	}
1510b4f9fe12SLen Brown 
1511936c8bcdSAlexey Dobriyan 	kfree(cmd);
1512936c8bcdSAlexey Dobriyan 
151378429e55SAzael Avalos 	lcd_out = crt_out = tv_out = -1;
151436d03f93SSeth Forshee 	ret = get_video_status(dev, &video_out);
151536d03f93SSeth Forshee 	if (!ret) {
1516b4f9fe12SLen Brown 		unsigned int new_video_out = video_out;
1517b5163992SAzael Avalos 
1518b4f9fe12SLen Brown 		if (lcd_out != -1)
1519b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1520b4f9fe12SLen Brown 		if (crt_out != -1)
1521b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1522b4f9fe12SLen Brown 		if (tv_out != -1)
1523b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1524e0769fe6SDarren Hart 		/*
1525e0769fe6SDarren Hart 		 * To avoid unnecessary video disruption, only write the new
15263f75bbe9SAzael Avalos 		 * video setting if something changed.
15273f75bbe9SAzael Avalos 		 */
1528b4f9fe12SLen Brown 		if (new_video_out != video_out)
152932bcd5cbSSeth Forshee 			ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1530b4f9fe12SLen Brown 	}
1531b4f9fe12SLen Brown 
1532e1a949c1SAzael Avalos 	return ret ? -EIO : count;
1533b4f9fe12SLen Brown }
1534b4f9fe12SLen Brown 
153597a32539SAlexey Dobriyan static const struct proc_ops video_proc_ops = {
153697a32539SAlexey Dobriyan 	.proc_open	= video_proc_open,
153797a32539SAlexey Dobriyan 	.proc_read	= seq_read,
153897a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
153997a32539SAlexey Dobriyan 	.proc_release	= single_release,
154097a32539SAlexey Dobriyan 	.proc_write	= video_proc_write,
1541936c8bcdSAlexey Dobriyan };
1542936c8bcdSAlexey Dobriyan 
15433e07e5baSAzael Avalos /* Fan status */
154436d03f93SSeth Forshee static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
154536d03f93SSeth Forshee {
15463e07e5baSAzael Avalos 	u32 result = hci_read(dev, HCI_FAN, status);
154736d03f93SSeth Forshee 
15483e07e5baSAzael Avalos 	if (result == TOS_FAILURE)
15493e07e5baSAzael Avalos 		pr_err("ACPI call to get Fan status failed\n");
15503e07e5baSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
15513e07e5baSAzael Avalos 		return -ENODEV;
15523e07e5baSAzael Avalos 
1553e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
15543e07e5baSAzael Avalos }
15553e07e5baSAzael Avalos 
15563e07e5baSAzael Avalos static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
15573e07e5baSAzael Avalos {
15583e07e5baSAzael Avalos 	u32 result = hci_write(dev, HCI_FAN, status);
15593e07e5baSAzael Avalos 
15603e07e5baSAzael Avalos 	if (result == TOS_FAILURE)
15613e07e5baSAzael Avalos 		pr_err("ACPI call to set Fan status failed\n");
15623e07e5baSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
15633e07e5baSAzael Avalos 		return -ENODEV;
15643e07e5baSAzael Avalos 
1565e1a949c1SAzael Avalos 	return result == TOS_SUCCESS ? 0 : -EIO;
156636d03f93SSeth Forshee }
156736d03f93SSeth Forshee 
1568936c8bcdSAlexey Dobriyan static int fan_proc_show(struct seq_file *m, void *v)
1569b4f9fe12SLen Brown {
1570135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1571b4f9fe12SLen Brown 	u32 value;
1572b4f9fe12SLen Brown 
15733e07e5baSAzael Avalos 	if (get_fan_status(dev, &value))
15743e07e5baSAzael Avalos 		return -EIO;
15753e07e5baSAzael Avalos 
1576936c8bcdSAlexey Dobriyan 	seq_printf(m, "running:                 %d\n", (value > 0));
1577135740deSSeth Forshee 	seq_printf(m, "force_on:                %d\n", dev->force_fan);
1578b4f9fe12SLen Brown 
15793e07e5baSAzael Avalos 	return 0;
1580b4f9fe12SLen Brown }
1581b4f9fe12SLen Brown 
1582936c8bcdSAlexey Dobriyan static int fan_proc_open(struct inode *inode, struct file *file)
1583b4f9fe12SLen Brown {
1584d9dda78bSAl Viro 	return single_open(file, fan_proc_show, PDE_DATA(inode));
1585936c8bcdSAlexey Dobriyan }
1586936c8bcdSAlexey Dobriyan 
1587936c8bcdSAlexey Dobriyan static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1588936c8bcdSAlexey Dobriyan 			      size_t count, loff_t *pos)
1589936c8bcdSAlexey Dobriyan {
1590d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1591936c8bcdSAlexey Dobriyan 	char cmd[42];
1592936c8bcdSAlexey Dobriyan 	size_t len;
1593b4f9fe12SLen Brown 	int value;
1594b4f9fe12SLen Brown 
1595936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1596936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1597936c8bcdSAlexey Dobriyan 		return -EFAULT;
1598936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1599936c8bcdSAlexey Dobriyan 
16003e07e5baSAzael Avalos 	if (sscanf(cmd, " force_on : %i", &value) != 1 &&
16013e07e5baSAzael Avalos 	    value != 0 && value != 1)
1602b4f9fe12SLen Brown 		return -EINVAL;
16033e07e5baSAzael Avalos 
16043e07e5baSAzael Avalos 	if (set_fan_status(dev, value))
16053e07e5baSAzael Avalos 		return -EIO;
16063e07e5baSAzael Avalos 
16073e07e5baSAzael Avalos 	dev->force_fan = value;
1608b4f9fe12SLen Brown 
1609b4f9fe12SLen Brown 	return count;
1610b4f9fe12SLen Brown }
1611b4f9fe12SLen Brown 
161297a32539SAlexey Dobriyan static const struct proc_ops fan_proc_ops = {
161397a32539SAlexey Dobriyan 	.proc_open	= fan_proc_open,
161497a32539SAlexey Dobriyan 	.proc_read	= seq_read,
161597a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
161697a32539SAlexey Dobriyan 	.proc_release	= single_release,
161797a32539SAlexey Dobriyan 	.proc_write	= fan_proc_write,
1618936c8bcdSAlexey Dobriyan };
1619936c8bcdSAlexey Dobriyan 
1620936c8bcdSAlexey Dobriyan static int keys_proc_show(struct seq_file *m, void *v)
1621b4f9fe12SLen Brown {
1622135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1623b4f9fe12SLen Brown 
1624135740deSSeth Forshee 	seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1625135740deSSeth Forshee 	seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
16267deef550SAzael Avalos 
1627936c8bcdSAlexey Dobriyan 	return 0;
1628b4f9fe12SLen Brown }
1629b4f9fe12SLen Brown 
1630936c8bcdSAlexey Dobriyan static int keys_proc_open(struct inode *inode, struct file *file)
1631b4f9fe12SLen Brown {
1632d9dda78bSAl Viro 	return single_open(file, keys_proc_show, PDE_DATA(inode));
1633936c8bcdSAlexey Dobriyan }
1634936c8bcdSAlexey Dobriyan 
1635936c8bcdSAlexey Dobriyan static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1636936c8bcdSAlexey Dobriyan 			       size_t count, loff_t *pos)
1637936c8bcdSAlexey Dobriyan {
1638d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1639936c8bcdSAlexey Dobriyan 	char cmd[42];
1640936c8bcdSAlexey Dobriyan 	size_t len;
1641b4f9fe12SLen Brown 	int value;
1642b4f9fe12SLen Brown 
1643936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1644936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1645936c8bcdSAlexey Dobriyan 		return -EFAULT;
1646936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1647936c8bcdSAlexey Dobriyan 
1648b5163992SAzael Avalos 	if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1649135740deSSeth Forshee 		dev->key_event_valid = 0;
1650b5163992SAzael Avalos 	else
1651b4f9fe12SLen Brown 		return -EINVAL;
1652b4f9fe12SLen Brown 
1653b4f9fe12SLen Brown 	return count;
1654b4f9fe12SLen Brown }
1655b4f9fe12SLen Brown 
165697a32539SAlexey Dobriyan static const struct proc_ops keys_proc_ops = {
165797a32539SAlexey Dobriyan 	.proc_open	= keys_proc_open,
165897a32539SAlexey Dobriyan 	.proc_read	= seq_read,
165997a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
166097a32539SAlexey Dobriyan 	.proc_release	= single_release,
166197a32539SAlexey Dobriyan 	.proc_write	= keys_proc_write,
1662936c8bcdSAlexey Dobriyan };
1663936c8bcdSAlexey Dobriyan 
1664c2e2a618SRandy Dunlap static int __maybe_unused version_proc_show(struct seq_file *m, void *v)
1665b4f9fe12SLen Brown {
1666936c8bcdSAlexey Dobriyan 	seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1667936c8bcdSAlexey Dobriyan 	seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1668936c8bcdSAlexey Dobriyan 	return 0;
1669b4f9fe12SLen Brown }
1670b4f9fe12SLen Brown 
1671e0769fe6SDarren Hart /*
1672e0769fe6SDarren Hart  * Proc and module init
1673b4f9fe12SLen Brown  */
1674b4f9fe12SLen Brown 
1675b4f9fe12SLen Brown #define PROC_TOSHIBA		"toshiba"
1676b4f9fe12SLen Brown 
1677b859f159SGreg Kroah-Hartman static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1678b4f9fe12SLen Brown {
167936d03f93SSeth Forshee 	if (dev->backlight_dev)
1680135740deSSeth Forshee 		proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
168197a32539SAlexey Dobriyan 				 &lcd_proc_ops, dev);
168236d03f93SSeth Forshee 	if (dev->video_supported)
1683135740deSSeth Forshee 		proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
168497a32539SAlexey Dobriyan 				 &video_proc_ops, dev);
168536d03f93SSeth Forshee 	if (dev->fan_supported)
1686135740deSSeth Forshee 		proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
168797a32539SAlexey Dobriyan 				 &fan_proc_ops, dev);
168836d03f93SSeth Forshee 	if (dev->hotkey_dev)
1689135740deSSeth Forshee 		proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
169097a32539SAlexey Dobriyan 				 &keys_proc_ops, dev);
16913f3942acSChristoph Hellwig 	proc_create_single_data("version", S_IRUGO, toshiba_proc_dir,
16923f3942acSChristoph Hellwig 			version_proc_show, dev);
1693b4f9fe12SLen Brown }
1694b4f9fe12SLen Brown 
169536d03f93SSeth Forshee static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1696b4f9fe12SLen Brown {
169736d03f93SSeth Forshee 	if (dev->backlight_dev)
1698936c8bcdSAlexey Dobriyan 		remove_proc_entry("lcd", toshiba_proc_dir);
169936d03f93SSeth Forshee 	if (dev->video_supported)
1700936c8bcdSAlexey Dobriyan 		remove_proc_entry("video", toshiba_proc_dir);
170136d03f93SSeth Forshee 	if (dev->fan_supported)
1702936c8bcdSAlexey Dobriyan 		remove_proc_entry("fan", toshiba_proc_dir);
170336d03f93SSeth Forshee 	if (dev->hotkey_dev)
1704936c8bcdSAlexey Dobriyan 		remove_proc_entry("keys", toshiba_proc_dir);
1705936c8bcdSAlexey Dobriyan 	remove_proc_entry("version", toshiba_proc_dir);
1706b4f9fe12SLen Brown }
1707b4f9fe12SLen Brown 
1708acc2472eSLionel Debroux static const struct backlight_ops toshiba_backlight_data = {
1709121b7b0dSAkio Idehara 	.options = BL_CORE_SUSPENDRESUME,
171062cce752SSeth Forshee 	.get_brightness = get_lcd_brightness,
1711b4f9fe12SLen Brown 	.update_status  = set_lcd_status,
1712b4f9fe12SLen Brown };
1713b4f9fe12SLen Brown 
171465e3cf9cSAzael Avalos /* Keyboard backlight work */
171565e3cf9cSAzael Avalos static void toshiba_acpi_kbd_bl_work(struct work_struct *work);
171665e3cf9cSAzael Avalos 
171765e3cf9cSAzael Avalos static DECLARE_WORK(kbd_bl_work, toshiba_acpi_kbd_bl_work);
171865e3cf9cSAzael Avalos 
1719360f0f39SAzael Avalos /*
1720360f0f39SAzael Avalos  * Sysfs files
1721360f0f39SAzael Avalos  */
17229d309848SAzael Avalos static ssize_t version_show(struct device *dev,
1723c6c68ff8SAzael Avalos 			    struct device_attribute *attr, char *buf)
1724c6c68ff8SAzael Avalos {
1725c6c68ff8SAzael Avalos 	return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1726c6c68ff8SAzael Avalos }
17270c3c0f10SAzael Avalos static DEVICE_ATTR_RO(version);
1728c6c68ff8SAzael Avalos 
17299d309848SAzael Avalos static ssize_t fan_store(struct device *dev,
173094477d4cSAzael Avalos 			 struct device_attribute *attr,
173194477d4cSAzael Avalos 			 const char *buf, size_t count)
173294477d4cSAzael Avalos {
173394477d4cSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
173494477d4cSAzael Avalos 	int state;
173594477d4cSAzael Avalos 	int ret;
173694477d4cSAzael Avalos 
173794477d4cSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
173894477d4cSAzael Avalos 	if (ret)
173994477d4cSAzael Avalos 		return ret;
174094477d4cSAzael Avalos 
174194477d4cSAzael Avalos 	if (state != 0 && state != 1)
174294477d4cSAzael Avalos 		return -EINVAL;
174394477d4cSAzael Avalos 
17443e07e5baSAzael Avalos 	ret = set_fan_status(toshiba, state);
17453e07e5baSAzael Avalos 	if (ret)
17463e07e5baSAzael Avalos 		return ret;
174794477d4cSAzael Avalos 
174894477d4cSAzael Avalos 	return count;
174994477d4cSAzael Avalos }
175094477d4cSAzael Avalos 
17519d309848SAzael Avalos static ssize_t fan_show(struct device *dev,
175294477d4cSAzael Avalos 			struct device_attribute *attr, char *buf)
175394477d4cSAzael Avalos {
175494477d4cSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
175594477d4cSAzael Avalos 	u32 value;
175694477d4cSAzael Avalos 	int ret;
175794477d4cSAzael Avalos 
175894477d4cSAzael Avalos 	ret = get_fan_status(toshiba, &value);
175994477d4cSAzael Avalos 	if (ret)
176094477d4cSAzael Avalos 		return ret;
176194477d4cSAzael Avalos 
176294477d4cSAzael Avalos 	return sprintf(buf, "%d\n", value);
176394477d4cSAzael Avalos }
17640c3c0f10SAzael Avalos static DEVICE_ATTR_RW(fan);
176594477d4cSAzael Avalos 
17669d309848SAzael Avalos static ssize_t kbd_backlight_mode_store(struct device *dev,
1767360f0f39SAzael Avalos 					struct device_attribute *attr,
1768360f0f39SAzael Avalos 					const char *buf, size_t count)
1769360f0f39SAzael Avalos {
1770360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1771aeaac098SDan Carpenter 	int mode;
1772aeaac098SDan Carpenter 	int ret;
1773360f0f39SAzael Avalos 
1774aeaac098SDan Carpenter 
1775aeaac098SDan Carpenter 	ret = kstrtoint(buf, 0, &mode);
1776aeaac098SDan Carpenter 	if (ret)
1777aeaac098SDan Carpenter 		return ret;
177893f8c16dSAzael Avalos 
177993f8c16dSAzael Avalos 	/* Check for supported modes depending on keyboard backlight type */
178093f8c16dSAzael Avalos 	if (toshiba->kbd_type == 1) {
178193f8c16dSAzael Avalos 		/* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1782aeaac098SDan Carpenter 		if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1783360f0f39SAzael Avalos 			return -EINVAL;
178493f8c16dSAzael Avalos 	} else if (toshiba->kbd_type == 2) {
178593f8c16dSAzael Avalos 		/* Type 2 doesn't support SCI_KBD_MODE_FNZ */
178693f8c16dSAzael Avalos 		if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
178793f8c16dSAzael Avalos 		    mode != SCI_KBD_MODE_OFF)
178893f8c16dSAzael Avalos 			return -EINVAL;
178993f8c16dSAzael Avalos 	}
1790360f0f39SAzael Avalos 
1791e0769fe6SDarren Hart 	/*
1792e0769fe6SDarren Hart 	 * Set the Keyboard Backlight Mode where:
1793360f0f39SAzael Avalos 	 *	Auto - KBD backlight turns off automatically in given time
1794360f0f39SAzael Avalos 	 *	FN-Z - KBD backlight "toggles" when hotkey pressed
179593f8c16dSAzael Avalos 	 *	ON   - KBD backlight is always on
179693f8c16dSAzael Avalos 	 *	OFF  - KBD backlight is always off
1797360f0f39SAzael Avalos 	 */
179893f8c16dSAzael Avalos 
179993f8c16dSAzael Avalos 	/* Only make a change if the actual mode has changed */
1800aeaac098SDan Carpenter 	if (toshiba->kbd_mode != mode) {
180193f8c16dSAzael Avalos 		/* Shift the time to "base time" (0x3c0000 == 60 seconds) */
18021e574dbfSAzael Avalos 		int time = toshiba->kbd_time << HCI_MISC_SHIFT;
180393f8c16dSAzael Avalos 
180493f8c16dSAzael Avalos 		/* OR the "base time" to the actual method format */
180593f8c16dSAzael Avalos 		if (toshiba->kbd_type == 1) {
180693f8c16dSAzael Avalos 			/* Type 1 requires the current mode */
180793f8c16dSAzael Avalos 			time |= toshiba->kbd_mode;
180893f8c16dSAzael Avalos 		} else if (toshiba->kbd_type == 2) {
180993f8c16dSAzael Avalos 			/* Type 2 requires the desired mode */
181093f8c16dSAzael Avalos 			time |= mode;
181193f8c16dSAzael Avalos 		}
181293f8c16dSAzael Avalos 
1813aeaac098SDan Carpenter 		ret = toshiba_kbd_illum_status_set(toshiba, time);
1814aeaac098SDan Carpenter 		if (ret)
1815aeaac098SDan Carpenter 			return ret;
181693f8c16dSAzael Avalos 
1817360f0f39SAzael Avalos 		toshiba->kbd_mode = mode;
1818147288e6SAzael Avalos 		toshiba_acpi->kbd_mode = mode;
181965e3cf9cSAzael Avalos 
182065e3cf9cSAzael Avalos 		/*
182165e3cf9cSAzael Avalos 		 * Some laptop models with the second generation backlit
182265e3cf9cSAzael Avalos 		 * keyboard (type 2) do not generate the keyboard backlight
182365e3cf9cSAzael Avalos 		 * changed event (0x92), and thus, the driver will never update
182465e3cf9cSAzael Avalos 		 * the sysfs entries.
182565e3cf9cSAzael Avalos 		 *
182665e3cf9cSAzael Avalos 		 * The event is generated right when changing the keyboard
182765e3cf9cSAzael Avalos 		 * backlight mode and the *notify function will set the
182865e3cf9cSAzael Avalos 		 * kbd_event_generated to true.
182965e3cf9cSAzael Avalos 		 *
183065e3cf9cSAzael Avalos 		 * In case the event is not generated, schedule the keyboard
183165e3cf9cSAzael Avalos 		 * backlight work to update the sysfs entries and emulate the
183265e3cf9cSAzael Avalos 		 * event via genetlink.
183365e3cf9cSAzael Avalos 		 */
183465e3cf9cSAzael Avalos 		if (toshiba->kbd_type == 2 &&
1835147288e6SAzael Avalos 		    !toshiba->kbd_event_generated)
183665e3cf9cSAzael Avalos 			schedule_work(&kbd_bl_work);
1837360f0f39SAzael Avalos 	}
1838360f0f39SAzael Avalos 
1839360f0f39SAzael Avalos 	return count;
1840360f0f39SAzael Avalos }
1841360f0f39SAzael Avalos 
18429d309848SAzael Avalos static ssize_t kbd_backlight_mode_show(struct device *dev,
1843360f0f39SAzael Avalos 				       struct device_attribute *attr,
1844360f0f39SAzael Avalos 				       char *buf)
1845360f0f39SAzael Avalos {
1846360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1847360f0f39SAzael Avalos 	u32 time;
1848360f0f39SAzael Avalos 
1849360f0f39SAzael Avalos 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1850360f0f39SAzael Avalos 		return -EIO;
1851360f0f39SAzael Avalos 
185293f8c16dSAzael Avalos 	return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
185393f8c16dSAzael Avalos }
18540c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_backlight_mode);
185593f8c16dSAzael Avalos 
18569d309848SAzael Avalos static ssize_t kbd_type_show(struct device *dev,
18579d309848SAzael Avalos 			     struct device_attribute *attr, char *buf)
185893f8c16dSAzael Avalos {
185993f8c16dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
186093f8c16dSAzael Avalos 
186193f8c16dSAzael Avalos 	return sprintf(buf, "%d\n", toshiba->kbd_type);
186293f8c16dSAzael Avalos }
18630c3c0f10SAzael Avalos static DEVICE_ATTR_RO(kbd_type);
186493f8c16dSAzael Avalos 
18659d309848SAzael Avalos static ssize_t available_kbd_modes_show(struct device *dev,
186693f8c16dSAzael Avalos 					struct device_attribute *attr,
186793f8c16dSAzael Avalos 					char *buf)
186893f8c16dSAzael Avalos {
186993f8c16dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
187093f8c16dSAzael Avalos 
187193f8c16dSAzael Avalos 	if (toshiba->kbd_type == 1)
18720b498201SAzael Avalos 		return sprintf(buf, "0x%x 0x%x\n",
187393f8c16dSAzael Avalos 			       SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
187493f8c16dSAzael Avalos 
18750b498201SAzael Avalos 	return sprintf(buf, "0x%x 0x%x 0x%x\n",
187693f8c16dSAzael Avalos 		       SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1877360f0f39SAzael Avalos }
18780c3c0f10SAzael Avalos static DEVICE_ATTR_RO(available_kbd_modes);
1879360f0f39SAzael Avalos 
18809d309848SAzael Avalos static ssize_t kbd_backlight_timeout_store(struct device *dev,
1881360f0f39SAzael Avalos 					   struct device_attribute *attr,
1882360f0f39SAzael Avalos 					   const char *buf, size_t count)
1883360f0f39SAzael Avalos {
1884360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1885eabde0faSAzael Avalos 	int time;
1886eabde0faSAzael Avalos 	int ret;
1887360f0f39SAzael Avalos 
1888eabde0faSAzael Avalos 	ret = kstrtoint(buf, 0, &time);
1889eabde0faSAzael Avalos 	if (ret)
1890eabde0faSAzael Avalos 		return ret;
1891eabde0faSAzael Avalos 
1892eabde0faSAzael Avalos 	/* Check for supported values depending on kbd_type */
1893eabde0faSAzael Avalos 	if (toshiba->kbd_type == 1) {
1894eabde0faSAzael Avalos 		if (time < 0 || time > 60)
1895360f0f39SAzael Avalos 			return -EINVAL;
1896eabde0faSAzael Avalos 	} else if (toshiba->kbd_type == 2) {
1897eabde0faSAzael Avalos 		if (time < 1 || time > 60)
1898eabde0faSAzael Avalos 			return -EINVAL;
1899eabde0faSAzael Avalos 	}
1900360f0f39SAzael Avalos 
1901eabde0faSAzael Avalos 	/* Set the Keyboard Backlight Timeout */
1902eabde0faSAzael Avalos 
1903eabde0faSAzael Avalos 	/* Only make a change if the actual timeout has changed */
1904eabde0faSAzael Avalos 	if (toshiba->kbd_time != time) {
1905eabde0faSAzael Avalos 		/* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1906360f0f39SAzael Avalos 		time = time << HCI_MISC_SHIFT;
1907eabde0faSAzael Avalos 		/* OR the "base time" to the actual method format */
1908eabde0faSAzael Avalos 		if (toshiba->kbd_type == 1)
1909eabde0faSAzael Avalos 			time |= SCI_KBD_MODE_FNZ;
1910eabde0faSAzael Avalos 		else if (toshiba->kbd_type == 2)
1911eabde0faSAzael Avalos 			time |= SCI_KBD_MODE_AUTO;
1912eabde0faSAzael Avalos 
1913eabde0faSAzael Avalos 		ret = toshiba_kbd_illum_status_set(toshiba, time);
1914eabde0faSAzael Avalos 		if (ret)
1915eabde0faSAzael Avalos 			return ret;
1916eabde0faSAzael Avalos 
1917360f0f39SAzael Avalos 		toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1918360f0f39SAzael Avalos 	}
1919360f0f39SAzael Avalos 
1920360f0f39SAzael Avalos 	return count;
1921360f0f39SAzael Avalos }
1922360f0f39SAzael Avalos 
19239d309848SAzael Avalos static ssize_t kbd_backlight_timeout_show(struct device *dev,
1924360f0f39SAzael Avalos 					  struct device_attribute *attr,
1925360f0f39SAzael Avalos 					  char *buf)
1926360f0f39SAzael Avalos {
1927360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1928360f0f39SAzael Avalos 	u32 time;
1929360f0f39SAzael Avalos 
1930360f0f39SAzael Avalos 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1931360f0f39SAzael Avalos 		return -EIO;
1932360f0f39SAzael Avalos 
1933360f0f39SAzael Avalos 	return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1934360f0f39SAzael Avalos }
19350c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_backlight_timeout);
1936360f0f39SAzael Avalos 
19379d309848SAzael Avalos static ssize_t touchpad_store(struct device *dev,
19389d8658acSAzael Avalos 			      struct device_attribute *attr,
19399d8658acSAzael Avalos 			      const char *buf, size_t count)
19409d8658acSAzael Avalos {
19419d8658acSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
19429d8658acSAzael Avalos 	int state;
1943c8a41669SAzael Avalos 	int ret;
19449d8658acSAzael Avalos 
19459d8658acSAzael Avalos 	/* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1946c8a41669SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
1947c8a41669SAzael Avalos 	if (ret)
1948c8a41669SAzael Avalos 		return ret;
1949c8a41669SAzael Avalos 	if (state != 0 && state != 1)
1950c8a41669SAzael Avalos 		return -EINVAL;
1951c8a41669SAzael Avalos 
1952c8a41669SAzael Avalos 	ret = toshiba_touchpad_set(toshiba, state);
1953c8a41669SAzael Avalos 	if (ret)
1954c8a41669SAzael Avalos 		return ret;
19559d8658acSAzael Avalos 
19569d8658acSAzael Avalos 	return count;
19579d8658acSAzael Avalos }
19589d8658acSAzael Avalos 
19599d309848SAzael Avalos static ssize_t touchpad_show(struct device *dev,
19609d8658acSAzael Avalos 			     struct device_attribute *attr, char *buf)
19619d8658acSAzael Avalos {
19629d8658acSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
19639d8658acSAzael Avalos 	u32 state;
19649d8658acSAzael Avalos 	int ret;
19659d8658acSAzael Avalos 
19669d8658acSAzael Avalos 	ret = toshiba_touchpad_get(toshiba, &state);
19679d8658acSAzael Avalos 	if (ret < 0)
19689d8658acSAzael Avalos 		return ret;
19699d8658acSAzael Avalos 
19709d8658acSAzael Avalos 	return sprintf(buf, "%i\n", state);
19719d8658acSAzael Avalos }
19720c3c0f10SAzael Avalos static DEVICE_ATTR_RW(touchpad);
19739d8658acSAzael Avalos 
19749d309848SAzael Avalos static ssize_t usb_sleep_charge_show(struct device *dev,
19759d309848SAzael Avalos 				     struct device_attribute *attr, char *buf)
1976e26ffe51SAzael Avalos {
1977e26ffe51SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1978e26ffe51SAzael Avalos 	u32 mode;
1979e26ffe51SAzael Avalos 	int ret;
1980e26ffe51SAzael Avalos 
1981e26ffe51SAzael Avalos 	ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1982e26ffe51SAzael Avalos 	if (ret < 0)
1983e26ffe51SAzael Avalos 		return ret;
1984e26ffe51SAzael Avalos 
1985e26ffe51SAzael Avalos 	return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1986e26ffe51SAzael Avalos }
1987e26ffe51SAzael Avalos 
19889d309848SAzael Avalos static ssize_t usb_sleep_charge_store(struct device *dev,
1989e26ffe51SAzael Avalos 				      struct device_attribute *attr,
1990e26ffe51SAzael Avalos 				      const char *buf, size_t count)
1991e26ffe51SAzael Avalos {
1992e26ffe51SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1993e26ffe51SAzael Avalos 	int state;
199478429e55SAzael Avalos 	u32 mode;
1995e26ffe51SAzael Avalos 	int ret;
1996e26ffe51SAzael Avalos 
1997e26ffe51SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
1998e26ffe51SAzael Avalos 	if (ret)
1999e26ffe51SAzael Avalos 		return ret;
2000e0769fe6SDarren Hart 	/*
2001e0769fe6SDarren Hart 	 * Check for supported values, where:
2002e26ffe51SAzael Avalos 	 * 0 - Disabled
2003e26ffe51SAzael Avalos 	 * 1 - Alternate (Non USB conformant devices that require more power)
2004e26ffe51SAzael Avalos 	 * 2 - Auto (USB conformant devices)
2005c8c91842SAzael Avalos 	 * 3 - Typical
2006e26ffe51SAzael Avalos 	 */
2007c8c91842SAzael Avalos 	if (state != 0 && state != 1 && state != 2 && state != 3)
2008e26ffe51SAzael Avalos 		return -EINVAL;
2009e26ffe51SAzael Avalos 
2010e26ffe51SAzael Avalos 	/* Set the USB charging mode to internal value */
2011c8c91842SAzael Avalos 	mode = toshiba->usbsc_mode_base;
2012e26ffe51SAzael Avalos 	if (state == 0)
2013c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_DISABLED;
2014e26ffe51SAzael Avalos 	else if (state == 1)
2015c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_ALTERNATE;
2016e26ffe51SAzael Avalos 	else if (state == 2)
2017c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_AUTO;
2018c8c91842SAzael Avalos 	else if (state == 3)
2019c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_TYPICAL;
2020e26ffe51SAzael Avalos 
2021e26ffe51SAzael Avalos 	ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2022e26ffe51SAzael Avalos 	if (ret)
2023e26ffe51SAzael Avalos 		return ret;
2024e26ffe51SAzael Avalos 
2025e26ffe51SAzael Avalos 	return count;
2026e26ffe51SAzael Avalos }
20270c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_sleep_charge);
2028e26ffe51SAzael Avalos 
2029182bcaa5SAzael Avalos static ssize_t sleep_functions_on_battery_show(struct device *dev,
2030182bcaa5SAzael Avalos 					       struct device_attribute *attr,
2031182bcaa5SAzael Avalos 					       char *buf)
2032182bcaa5SAzael Avalos {
2033182bcaa5SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
203478429e55SAzael Avalos 	int bat_lvl, status;
2035182bcaa5SAzael Avalos 	u32 state;
2036182bcaa5SAzael Avalos 	int ret;
2037182bcaa5SAzael Avalos 	int tmp;
2038182bcaa5SAzael Avalos 
2039182bcaa5SAzael Avalos 	ret = toshiba_sleep_functions_status_get(toshiba, &state);
2040182bcaa5SAzael Avalos 	if (ret < 0)
2041182bcaa5SAzael Avalos 		return ret;
2042182bcaa5SAzael Avalos 
2043182bcaa5SAzael Avalos 	/* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2044182bcaa5SAzael Avalos 	tmp = state & SCI_USB_CHARGE_BAT_MASK;
2045182bcaa5SAzael Avalos 	status = (tmp == 0x4) ? 1 : 0;
2046182bcaa5SAzael Avalos 	/* Determine the battery level set */
2047182bcaa5SAzael Avalos 	bat_lvl = state >> HCI_MISC_SHIFT;
2048182bcaa5SAzael Avalos 
2049182bcaa5SAzael Avalos 	return sprintf(buf, "%d %d\n", status, bat_lvl);
2050182bcaa5SAzael Avalos }
2051182bcaa5SAzael Avalos 
2052182bcaa5SAzael Avalos static ssize_t sleep_functions_on_battery_store(struct device *dev,
2053182bcaa5SAzael Avalos 						struct device_attribute *attr,
2054182bcaa5SAzael Avalos 						const char *buf, size_t count)
2055182bcaa5SAzael Avalos {
2056182bcaa5SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2057182bcaa5SAzael Avalos 	u32 status;
2058182bcaa5SAzael Avalos 	int value;
2059182bcaa5SAzael Avalos 	int ret;
2060182bcaa5SAzael Avalos 	int tmp;
2061182bcaa5SAzael Avalos 
2062182bcaa5SAzael Avalos 	ret = kstrtoint(buf, 0, &value);
2063182bcaa5SAzael Avalos 	if (ret)
2064182bcaa5SAzael Avalos 		return ret;
2065182bcaa5SAzael Avalos 
2066e0769fe6SDarren Hart 	/*
2067e0769fe6SDarren Hart 	 * Set the status of the function:
2068182bcaa5SAzael Avalos 	 * 0 - Disabled
2069182bcaa5SAzael Avalos 	 * 1-100 - Enabled
2070182bcaa5SAzael Avalos 	 */
2071182bcaa5SAzael Avalos 	if (value < 0 || value > 100)
2072182bcaa5SAzael Avalos 		return -EINVAL;
2073182bcaa5SAzael Avalos 
2074182bcaa5SAzael Avalos 	if (value == 0) {
2075182bcaa5SAzael Avalos 		tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2076182bcaa5SAzael Avalos 		status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2077182bcaa5SAzael Avalos 	} else {
2078182bcaa5SAzael Avalos 		tmp = value << HCI_MISC_SHIFT;
2079182bcaa5SAzael Avalos 		status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2080182bcaa5SAzael Avalos 	}
2081182bcaa5SAzael Avalos 	ret = toshiba_sleep_functions_status_set(toshiba, status);
2082182bcaa5SAzael Avalos 	if (ret < 0)
2083182bcaa5SAzael Avalos 		return ret;
2084182bcaa5SAzael Avalos 
2085182bcaa5SAzael Avalos 	toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2086182bcaa5SAzael Avalos 
2087182bcaa5SAzael Avalos 	return count;
2088182bcaa5SAzael Avalos }
20890c3c0f10SAzael Avalos static DEVICE_ATTR_RW(sleep_functions_on_battery);
2090182bcaa5SAzael Avalos 
20919d309848SAzael Avalos static ssize_t usb_rapid_charge_show(struct device *dev,
20929d309848SAzael Avalos 				     struct device_attribute *attr, char *buf)
2093bb3fe01fSAzael Avalos {
2094bb3fe01fSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2095bb3fe01fSAzael Avalos 	u32 state;
2096bb3fe01fSAzael Avalos 	int ret;
2097bb3fe01fSAzael Avalos 
2098bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2099bb3fe01fSAzael Avalos 	if (ret < 0)
2100bb3fe01fSAzael Avalos 		return ret;
2101bb3fe01fSAzael Avalos 
2102bb3fe01fSAzael Avalos 	return sprintf(buf, "%d\n", state);
2103bb3fe01fSAzael Avalos }
2104bb3fe01fSAzael Avalos 
21059d309848SAzael Avalos static ssize_t usb_rapid_charge_store(struct device *dev,
2106bb3fe01fSAzael Avalos 				      struct device_attribute *attr,
2107bb3fe01fSAzael Avalos 				      const char *buf, size_t count)
2108bb3fe01fSAzael Avalos {
2109bb3fe01fSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2110bb3fe01fSAzael Avalos 	int state;
2111bb3fe01fSAzael Avalos 	int ret;
2112bb3fe01fSAzael Avalos 
2113bb3fe01fSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2114bb3fe01fSAzael Avalos 	if (ret)
2115bb3fe01fSAzael Avalos 		return ret;
2116bb3fe01fSAzael Avalos 	if (state != 0 && state != 1)
2117bb3fe01fSAzael Avalos 		return -EINVAL;
2118bb3fe01fSAzael Avalos 
2119bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_set(toshiba, state);
2120bb3fe01fSAzael Avalos 	if (ret)
2121bb3fe01fSAzael Avalos 		return ret;
2122bb3fe01fSAzael Avalos 
2123bb3fe01fSAzael Avalos 	return count;
2124bb3fe01fSAzael Avalos }
21250c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_rapid_charge);
2126bb3fe01fSAzael Avalos 
21279d309848SAzael Avalos static ssize_t usb_sleep_music_show(struct device *dev,
21289d309848SAzael Avalos 				    struct device_attribute *attr, char *buf)
2129172ce0a9SAzael Avalos {
2130172ce0a9SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2131172ce0a9SAzael Avalos 	u32 state;
2132172ce0a9SAzael Avalos 	int ret;
2133172ce0a9SAzael Avalos 
2134172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_get(toshiba, &state);
2135172ce0a9SAzael Avalos 	if (ret < 0)
2136172ce0a9SAzael Avalos 		return ret;
2137172ce0a9SAzael Avalos 
2138172ce0a9SAzael Avalos 	return sprintf(buf, "%d\n", state);
2139172ce0a9SAzael Avalos }
2140172ce0a9SAzael Avalos 
21419d309848SAzael Avalos static ssize_t usb_sleep_music_store(struct device *dev,
2142172ce0a9SAzael Avalos 				     struct device_attribute *attr,
2143172ce0a9SAzael Avalos 				     const char *buf, size_t count)
2144172ce0a9SAzael Avalos {
2145172ce0a9SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2146172ce0a9SAzael Avalos 	int state;
2147172ce0a9SAzael Avalos 	int ret;
2148172ce0a9SAzael Avalos 
2149172ce0a9SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2150172ce0a9SAzael Avalos 	if (ret)
2151172ce0a9SAzael Avalos 		return ret;
2152172ce0a9SAzael Avalos 	if (state != 0 && state != 1)
2153172ce0a9SAzael Avalos 		return -EINVAL;
2154172ce0a9SAzael Avalos 
2155172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_set(toshiba, state);
2156172ce0a9SAzael Avalos 	if (ret)
2157172ce0a9SAzael Avalos 		return ret;
2158172ce0a9SAzael Avalos 
2159172ce0a9SAzael Avalos 	return count;
2160172ce0a9SAzael Avalos }
21610c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_sleep_music);
2162172ce0a9SAzael Avalos 
21639d309848SAzael Avalos static ssize_t kbd_function_keys_show(struct device *dev,
21649d309848SAzael Avalos 				      struct device_attribute *attr, char *buf)
2165bae84195SAzael Avalos {
2166bae84195SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2167bae84195SAzael Avalos 	int mode;
2168bae84195SAzael Avalos 	int ret;
2169bae84195SAzael Avalos 
2170bae84195SAzael Avalos 	ret = toshiba_function_keys_get(toshiba, &mode);
2171bae84195SAzael Avalos 	if (ret < 0)
2172bae84195SAzael Avalos 		return ret;
2173bae84195SAzael Avalos 
2174bae84195SAzael Avalos 	return sprintf(buf, "%d\n", mode);
2175bae84195SAzael Avalos }
2176bae84195SAzael Avalos 
21779d309848SAzael Avalos static ssize_t kbd_function_keys_store(struct device *dev,
2178bae84195SAzael Avalos 				       struct device_attribute *attr,
2179bae84195SAzael Avalos 				       const char *buf, size_t count)
2180bae84195SAzael Avalos {
2181bae84195SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2182bae84195SAzael Avalos 	int mode;
2183bae84195SAzael Avalos 	int ret;
2184bae84195SAzael Avalos 
2185bae84195SAzael Avalos 	ret = kstrtoint(buf, 0, &mode);
2186bae84195SAzael Avalos 	if (ret)
2187bae84195SAzael Avalos 		return ret;
2188e0769fe6SDarren Hart 	/*
2189e0769fe6SDarren Hart 	 * Check for the function keys mode where:
2190bae84195SAzael Avalos 	 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2191bae84195SAzael Avalos 	 * 1 - Special functions (Opposite of the above setting)
2192bae84195SAzael Avalos 	 */
2193bae84195SAzael Avalos 	if (mode != 0 && mode != 1)
2194bae84195SAzael Avalos 		return -EINVAL;
2195bae84195SAzael Avalos 
2196bae84195SAzael Avalos 	ret = toshiba_function_keys_set(toshiba, mode);
2197bae84195SAzael Avalos 	if (ret)
2198bae84195SAzael Avalos 		return ret;
2199bae84195SAzael Avalos 
2200bae84195SAzael Avalos 	pr_info("Reboot for changes to KBD Function Keys to take effect");
2201bae84195SAzael Avalos 
2202bae84195SAzael Avalos 	return count;
2203bae84195SAzael Avalos }
22040c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_function_keys);
2205bae84195SAzael Avalos 
22069d309848SAzael Avalos static ssize_t panel_power_on_show(struct device *dev,
22079d309848SAzael Avalos 				   struct device_attribute *attr, char *buf)
220835d53ceaSAzael Avalos {
220935d53ceaSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
221035d53ceaSAzael Avalos 	u32 state;
221135d53ceaSAzael Avalos 	int ret;
221235d53ceaSAzael Avalos 
221335d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_get(toshiba, &state);
221435d53ceaSAzael Avalos 	if (ret < 0)
221535d53ceaSAzael Avalos 		return ret;
221635d53ceaSAzael Avalos 
221735d53ceaSAzael Avalos 	return sprintf(buf, "%d\n", state);
221835d53ceaSAzael Avalos }
221935d53ceaSAzael Avalos 
22209d309848SAzael Avalos static ssize_t panel_power_on_store(struct device *dev,
222135d53ceaSAzael Avalos 				    struct device_attribute *attr,
222235d53ceaSAzael Avalos 				    const char *buf, size_t count)
222335d53ceaSAzael Avalos {
222435d53ceaSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
222535d53ceaSAzael Avalos 	int state;
222635d53ceaSAzael Avalos 	int ret;
222735d53ceaSAzael Avalos 
222835d53ceaSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
222935d53ceaSAzael Avalos 	if (ret)
223035d53ceaSAzael Avalos 		return ret;
223135d53ceaSAzael Avalos 	if (state != 0 && state != 1)
223235d53ceaSAzael Avalos 		return -EINVAL;
223335d53ceaSAzael Avalos 
223435d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_set(toshiba, state);
223535d53ceaSAzael Avalos 	if (ret)
223635d53ceaSAzael Avalos 		return ret;
223735d53ceaSAzael Avalos 
223835d53ceaSAzael Avalos 	pr_info("Reboot for changes to Panel Power ON to take effect");
223935d53ceaSAzael Avalos 
224035d53ceaSAzael Avalos 	return count;
224135d53ceaSAzael Avalos }
22420c3c0f10SAzael Avalos static DEVICE_ATTR_RW(panel_power_on);
224335d53ceaSAzael Avalos 
22449d309848SAzael Avalos static ssize_t usb_three_show(struct device *dev,
22459d309848SAzael Avalos 			      struct device_attribute *attr, char *buf)
224617fe4b3dSAzael Avalos {
224717fe4b3dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
224817fe4b3dSAzael Avalos 	u32 state;
224917fe4b3dSAzael Avalos 	int ret;
225017fe4b3dSAzael Avalos 
225117fe4b3dSAzael Avalos 	ret = toshiba_usb_three_get(toshiba, &state);
225217fe4b3dSAzael Avalos 	if (ret < 0)
225317fe4b3dSAzael Avalos 		return ret;
225417fe4b3dSAzael Avalos 
225517fe4b3dSAzael Avalos 	return sprintf(buf, "%d\n", state);
225617fe4b3dSAzael Avalos }
225717fe4b3dSAzael Avalos 
22589d309848SAzael Avalos static ssize_t usb_three_store(struct device *dev,
225917fe4b3dSAzael Avalos 			       struct device_attribute *attr,
226017fe4b3dSAzael Avalos 			       const char *buf, size_t count)
226117fe4b3dSAzael Avalos {
226217fe4b3dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
226317fe4b3dSAzael Avalos 	int state;
226417fe4b3dSAzael Avalos 	int ret;
226517fe4b3dSAzael Avalos 
226617fe4b3dSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
226717fe4b3dSAzael Avalos 	if (ret)
226817fe4b3dSAzael Avalos 		return ret;
2269e0769fe6SDarren Hart 	/*
2270e0769fe6SDarren Hart 	 * Check for USB 3 mode where:
227117fe4b3dSAzael Avalos 	 * 0 - Disabled (Acts like a USB 2 port, saving power)
227217fe4b3dSAzael Avalos 	 * 1 - Enabled
227317fe4b3dSAzael Avalos 	 */
227417fe4b3dSAzael Avalos 	if (state != 0 && state != 1)
227517fe4b3dSAzael Avalos 		return -EINVAL;
227617fe4b3dSAzael Avalos 
227717fe4b3dSAzael Avalos 	ret = toshiba_usb_three_set(toshiba, state);
227817fe4b3dSAzael Avalos 	if (ret)
227917fe4b3dSAzael Avalos 		return ret;
228017fe4b3dSAzael Avalos 
228117fe4b3dSAzael Avalos 	pr_info("Reboot for changes to USB 3 to take effect");
228217fe4b3dSAzael Avalos 
228317fe4b3dSAzael Avalos 	return count;
228417fe4b3dSAzael Avalos }
22850c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_three);
22869bd1213bSAzael Avalos 
2287b1009b91SAzael Avalos static ssize_t cooling_method_show(struct device *dev,
2288b1009b91SAzael Avalos 				   struct device_attribute *attr, char *buf)
2289b1009b91SAzael Avalos {
2290b1009b91SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2291b1009b91SAzael Avalos 	int state;
2292b1009b91SAzael Avalos 	int ret;
2293b1009b91SAzael Avalos 
2294b1009b91SAzael Avalos 	ret = toshiba_cooling_method_get(toshiba, &state);
2295b1009b91SAzael Avalos 	if (ret < 0)
2296b1009b91SAzael Avalos 		return ret;
2297b1009b91SAzael Avalos 
2298b1009b91SAzael Avalos 	return sprintf(buf, "%d %d\n", state, toshiba->max_cooling_method);
2299b1009b91SAzael Avalos }
2300b1009b91SAzael Avalos 
2301b1009b91SAzael Avalos static ssize_t cooling_method_store(struct device *dev,
2302b1009b91SAzael Avalos 				    struct device_attribute *attr,
2303b1009b91SAzael Avalos 				    const char *buf, size_t count)
2304b1009b91SAzael Avalos {
2305b1009b91SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2306b1009b91SAzael Avalos 	int state;
2307b1009b91SAzael Avalos 	int ret;
2308b1009b91SAzael Avalos 
2309b1009b91SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2310b1009b91SAzael Avalos 	if (ret)
2311b1009b91SAzael Avalos 		return ret;
2312b1009b91SAzael Avalos 
2313b1009b91SAzael Avalos 	/*
2314b1009b91SAzael Avalos 	 * Check for supported values
2315b1009b91SAzael Avalos 	 * Depending on the laptop model, some only support these two:
2316b1009b91SAzael Avalos 	 * 0 - Maximum Performance
2317b1009b91SAzael Avalos 	 * 1 - Battery Optimized
2318b1009b91SAzael Avalos 	 *
2319b1009b91SAzael Avalos 	 * While some others support all three methods:
2320b1009b91SAzael Avalos 	 * 0 - Maximum Performance
2321b1009b91SAzael Avalos 	 * 1 - Performance
2322b1009b91SAzael Avalos 	 * 2 - Battery Optimized
2323b1009b91SAzael Avalos 	 */
2324b1009b91SAzael Avalos 	if (state < 0 || state > toshiba->max_cooling_method)
2325b1009b91SAzael Avalos 		return -EINVAL;
2326b1009b91SAzael Avalos 
2327b1009b91SAzael Avalos 	ret = toshiba_cooling_method_set(toshiba, state);
2328b1009b91SAzael Avalos 	if (ret)
2329b1009b91SAzael Avalos 		return ret;
2330b1009b91SAzael Avalos 
2331b1009b91SAzael Avalos 	return count;
2332b1009b91SAzael Avalos }
2333b1009b91SAzael Avalos static DEVICE_ATTR_RW(cooling_method);
2334b1009b91SAzael Avalos 
23359bd1213bSAzael Avalos static struct attribute *toshiba_attributes[] = {
23369bd1213bSAzael Avalos 	&dev_attr_version.attr,
23379bd1213bSAzael Avalos 	&dev_attr_fan.attr,
23389bd1213bSAzael Avalos 	&dev_attr_kbd_backlight_mode.attr,
23399bd1213bSAzael Avalos 	&dev_attr_kbd_type.attr,
23409bd1213bSAzael Avalos 	&dev_attr_available_kbd_modes.attr,
23419bd1213bSAzael Avalos 	&dev_attr_kbd_backlight_timeout.attr,
23429bd1213bSAzael Avalos 	&dev_attr_touchpad.attr,
23439bd1213bSAzael Avalos 	&dev_attr_usb_sleep_charge.attr,
23449bd1213bSAzael Avalos 	&dev_attr_sleep_functions_on_battery.attr,
23459bd1213bSAzael Avalos 	&dev_attr_usb_rapid_charge.attr,
23469bd1213bSAzael Avalos 	&dev_attr_usb_sleep_music.attr,
23479bd1213bSAzael Avalos 	&dev_attr_kbd_function_keys.attr,
23489bd1213bSAzael Avalos 	&dev_attr_panel_power_on.attr,
23499bd1213bSAzael Avalos 	&dev_attr_usb_three.attr,
2350b1009b91SAzael Avalos 	&dev_attr_cooling_method.attr,
23519bd1213bSAzael Avalos 	NULL,
23529bd1213bSAzael Avalos };
23539bd1213bSAzael Avalos 
2354360f0f39SAzael Avalos static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2355360f0f39SAzael Avalos 					struct attribute *attr, int idx)
2356360f0f39SAzael Avalos {
2357360f0f39SAzael Avalos 	struct device *dev = container_of(kobj, struct device, kobj);
2358360f0f39SAzael Avalos 	struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2359360f0f39SAzael Avalos 	bool exists = true;
2360360f0f39SAzael Avalos 
236194477d4cSAzael Avalos 	if (attr == &dev_attr_fan.attr)
236294477d4cSAzael Avalos 		exists = (drv->fan_supported) ? true : false;
236394477d4cSAzael Avalos 	else if (attr == &dev_attr_kbd_backlight_mode.attr)
2364360f0f39SAzael Avalos 		exists = (drv->kbd_illum_supported) ? true : false;
2365360f0f39SAzael Avalos 	else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2366360f0f39SAzael Avalos 		exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
23679d8658acSAzael Avalos 	else if (attr == &dev_attr_touchpad.attr)
23689d8658acSAzael Avalos 		exists = (drv->touchpad_supported) ? true : false;
2369e26ffe51SAzael Avalos 	else if (attr == &dev_attr_usb_sleep_charge.attr)
2370e26ffe51SAzael Avalos 		exists = (drv->usb_sleep_charge_supported) ? true : false;
2371182bcaa5SAzael Avalos 	else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2372182bcaa5SAzael Avalos 		exists = (drv->usb_sleep_charge_supported) ? true : false;
2373bb3fe01fSAzael Avalos 	else if (attr == &dev_attr_usb_rapid_charge.attr)
2374bb3fe01fSAzael Avalos 		exists = (drv->usb_rapid_charge_supported) ? true : false;
2375172ce0a9SAzael Avalos 	else if (attr == &dev_attr_usb_sleep_music.attr)
2376172ce0a9SAzael Avalos 		exists = (drv->usb_sleep_music_supported) ? true : false;
2377bae84195SAzael Avalos 	else if (attr == &dev_attr_kbd_function_keys.attr)
2378bae84195SAzael Avalos 		exists = (drv->kbd_function_keys_supported) ? true : false;
237935d53ceaSAzael Avalos 	else if (attr == &dev_attr_panel_power_on.attr)
238035d53ceaSAzael Avalos 		exists = (drv->panel_power_on_supported) ? true : false;
238117fe4b3dSAzael Avalos 	else if (attr == &dev_attr_usb_three.attr)
238217fe4b3dSAzael Avalos 		exists = (drv->usb_three_supported) ? true : false;
2383b1009b91SAzael Avalos 	else if (attr == &dev_attr_cooling_method.attr)
2384b1009b91SAzael Avalos 		exists = (drv->cooling_method_supported) ? true : false;
2385360f0f39SAzael Avalos 
2386360f0f39SAzael Avalos 	return exists ? attr->mode : 0;
2387360f0f39SAzael Avalos }
2388360f0f39SAzael Avalos 
238944bd76d0SArvind Yadav static const struct attribute_group toshiba_attr_group = {
23909bd1213bSAzael Avalos 	.is_visible = toshiba_sysfs_is_visible,
23919bd1213bSAzael Avalos 	.attrs = toshiba_attributes,
23929bd1213bSAzael Avalos };
23939bd1213bSAzael Avalos 
239465e3cf9cSAzael Avalos static void toshiba_acpi_kbd_bl_work(struct work_struct *work)
239565e3cf9cSAzael Avalos {
239665e3cf9cSAzael Avalos 	/* Update the sysfs entries */
2397147288e6SAzael Avalos 	if (sysfs_update_group(&toshiba_acpi->acpi_dev->dev.kobj,
239865e3cf9cSAzael Avalos 			       &toshiba_attr_group))
239965e3cf9cSAzael Avalos 		pr_err("Unable to update sysfs entries\n");
240065e3cf9cSAzael Avalos 
2401147288e6SAzael Avalos 	/* Notify LED subsystem about keyboard backlight change */
2402147288e6SAzael Avalos 	if (toshiba_acpi->kbd_type == 2 &&
2403147288e6SAzael Avalos 	    toshiba_acpi->kbd_mode != SCI_KBD_MODE_AUTO)
2404147288e6SAzael Avalos 		led_classdev_notify_brightness_hw_changed(&toshiba_acpi->kbd_led,
2405147288e6SAzael Avalos 				(toshiba_acpi->kbd_mode == SCI_KBD_MODE_ON) ?
2406147288e6SAzael Avalos 				LED_FULL : LED_OFF);
2407147288e6SAzael Avalos 
240865e3cf9cSAzael Avalos 	/* Emulate the keyboard backlight event */
2409147288e6SAzael Avalos 	acpi_bus_generate_netlink_event(toshiba_acpi->acpi_dev->pnp.device_class,
2410147288e6SAzael Avalos 					dev_name(&toshiba_acpi->acpi_dev->dev),
241165e3cf9cSAzael Avalos 					0x92, 0);
241265e3cf9cSAzael Avalos }
241365e3cf9cSAzael Avalos 
24141f28f290SAzael Avalos /*
241598010f1eSAzael Avalos  * IIO device
241698010f1eSAzael Avalos  */
241798010f1eSAzael Avalos 
241898010f1eSAzael Avalos enum toshiba_iio_accel_chan {
241998010f1eSAzael Avalos 	AXIS_X,
242098010f1eSAzael Avalos 	AXIS_Y,
242198010f1eSAzael Avalos 	AXIS_Z
242298010f1eSAzael Avalos };
242398010f1eSAzael Avalos 
242498010f1eSAzael Avalos static int toshiba_iio_accel_get_axis(enum toshiba_iio_accel_chan chan)
242598010f1eSAzael Avalos {
242698010f1eSAzael Avalos 	u32 xyval, zval;
242798010f1eSAzael Avalos 	int ret;
242898010f1eSAzael Avalos 
242998010f1eSAzael Avalos 	ret = toshiba_accelerometer_get(toshiba_acpi, &xyval, &zval);
243098010f1eSAzael Avalos 	if (ret < 0)
243198010f1eSAzael Avalos 		return ret;
243298010f1eSAzael Avalos 
243398010f1eSAzael Avalos 	switch (chan) {
243498010f1eSAzael Avalos 	case AXIS_X:
243598010f1eSAzael Avalos 		return xyval & HCI_ACCEL_DIRECTION_MASK ?
243698010f1eSAzael Avalos 			-(xyval & HCI_ACCEL_MASK) : xyval & HCI_ACCEL_MASK;
243798010f1eSAzael Avalos 	case AXIS_Y:
243898010f1eSAzael Avalos 		return (xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_DIRECTION_MASK ?
243998010f1eSAzael Avalos 			-((xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_MASK) :
244098010f1eSAzael Avalos 			(xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_MASK;
244198010f1eSAzael Avalos 	case AXIS_Z:
244298010f1eSAzael Avalos 		return zval & HCI_ACCEL_DIRECTION_MASK ?
244398010f1eSAzael Avalos 			-(zval & HCI_ACCEL_MASK) : zval & HCI_ACCEL_MASK;
244498010f1eSAzael Avalos 	}
244598010f1eSAzael Avalos 
244698010f1eSAzael Avalos 	return ret;
244798010f1eSAzael Avalos }
244898010f1eSAzael Avalos 
244998010f1eSAzael Avalos static int toshiba_iio_accel_read_raw(struct iio_dev *indio_dev,
245098010f1eSAzael Avalos 				      struct iio_chan_spec const *chan,
245198010f1eSAzael Avalos 				      int *val, int *val2, long mask)
245298010f1eSAzael Avalos {
245398010f1eSAzael Avalos 	int ret;
245498010f1eSAzael Avalos 
245598010f1eSAzael Avalos 	switch (mask) {
245698010f1eSAzael Avalos 	case IIO_CHAN_INFO_RAW:
245798010f1eSAzael Avalos 		ret = toshiba_iio_accel_get_axis(chan->channel);
245898010f1eSAzael Avalos 		if (ret == -EIO || ret == -ENODEV)
245998010f1eSAzael Avalos 			return ret;
246098010f1eSAzael Avalos 
246198010f1eSAzael Avalos 		*val = ret;
246298010f1eSAzael Avalos 
246398010f1eSAzael Avalos 		return IIO_VAL_INT;
246498010f1eSAzael Avalos 	}
246598010f1eSAzael Avalos 
246698010f1eSAzael Avalos 	return -EINVAL;
246798010f1eSAzael Avalos }
246898010f1eSAzael Avalos 
246998010f1eSAzael Avalos #define TOSHIBA_IIO_ACCEL_CHANNEL(axis, chan) { \
247098010f1eSAzael Avalos 	.type = IIO_ACCEL, \
247198010f1eSAzael Avalos 	.modified = 1, \
247298010f1eSAzael Avalos 	.channel = chan, \
247398010f1eSAzael Avalos 	.channel2 = IIO_MOD_##axis, \
247498010f1eSAzael Avalos 	.output = 1, \
247598010f1eSAzael Avalos 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
247698010f1eSAzael Avalos }
247798010f1eSAzael Avalos 
247898010f1eSAzael Avalos static const struct iio_chan_spec toshiba_iio_accel_channels[] = {
247998010f1eSAzael Avalos 	TOSHIBA_IIO_ACCEL_CHANNEL(X, AXIS_X),
248098010f1eSAzael Avalos 	TOSHIBA_IIO_ACCEL_CHANNEL(Y, AXIS_Y),
248198010f1eSAzael Avalos 	TOSHIBA_IIO_ACCEL_CHANNEL(Z, AXIS_Z),
248298010f1eSAzael Avalos };
248398010f1eSAzael Avalos 
248498010f1eSAzael Avalos static const struct iio_info toshiba_iio_accel_info = {
248598010f1eSAzael Avalos 	.read_raw = &toshiba_iio_accel_read_raw,
248698010f1eSAzael Avalos };
248798010f1eSAzael Avalos 
248898010f1eSAzael Avalos /*
2489fc5462f8SAzael Avalos  * Misc device
2490fc5462f8SAzael Avalos  */
2491fc5462f8SAzael Avalos static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2492fc5462f8SAzael Avalos {
2493fc5462f8SAzael Avalos 	u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2494fc5462f8SAzael Avalos 			      regs->edx, regs->esi, regs->edi };
2495fc5462f8SAzael Avalos 	u32 out[TCI_WORDS];
2496fc5462f8SAzael Avalos 	acpi_status status;
2497fc5462f8SAzael Avalos 
2498fc5462f8SAzael Avalos 	status = tci_raw(toshiba_acpi, in, out);
2499fc5462f8SAzael Avalos 	if (ACPI_FAILURE(status)) {
2500fc5462f8SAzael Avalos 		pr_err("ACPI call to query SMM registers failed\n");
2501fc5462f8SAzael Avalos 		return -EIO;
2502fc5462f8SAzael Avalos 	}
2503fc5462f8SAzael Avalos 
2504fc5462f8SAzael Avalos 	/* Fillout the SMM struct with the TCI call results */
2505fc5462f8SAzael Avalos 	regs->eax = out[0];
2506fc5462f8SAzael Avalos 	regs->ebx = out[1];
2507fc5462f8SAzael Avalos 	regs->ecx = out[2];
2508fc5462f8SAzael Avalos 	regs->edx = out[3];
2509fc5462f8SAzael Avalos 	regs->esi = out[4];
2510fc5462f8SAzael Avalos 	regs->edi = out[5];
2511fc5462f8SAzael Avalos 
2512fc5462f8SAzael Avalos 	return 0;
2513fc5462f8SAzael Avalos }
2514fc5462f8SAzael Avalos 
2515fc5462f8SAzael Avalos static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2516fc5462f8SAzael Avalos 			       unsigned long arg)
2517fc5462f8SAzael Avalos {
2518fc5462f8SAzael Avalos 	SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2519fc5462f8SAzael Avalos 	SMMRegisters regs;
2520fc5462f8SAzael Avalos 	int ret;
2521fc5462f8SAzael Avalos 
2522fc5462f8SAzael Avalos 	if (!argp)
2523fc5462f8SAzael Avalos 		return -EINVAL;
2524fc5462f8SAzael Avalos 
2525fc5462f8SAzael Avalos 	switch (cmd) {
2526fc5462f8SAzael Avalos 	case TOSH_SMM:
2527fc5462f8SAzael Avalos 		if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2528fc5462f8SAzael Avalos 			return -EFAULT;
2529fc5462f8SAzael Avalos 		ret = toshiba_acpi_smm_bridge(&regs);
2530fc5462f8SAzael Avalos 		if (ret)
2531fc5462f8SAzael Avalos 			return ret;
2532fc5462f8SAzael Avalos 		if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2533fc5462f8SAzael Avalos 			return -EFAULT;
2534fc5462f8SAzael Avalos 		break;
2535fc5462f8SAzael Avalos 	case TOSHIBA_ACPI_SCI:
2536fc5462f8SAzael Avalos 		if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2537fc5462f8SAzael Avalos 			return -EFAULT;
2538fc5462f8SAzael Avalos 		/* Ensure we are being called with a SCI_{GET, SET} register */
2539fc5462f8SAzael Avalos 		if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2540fc5462f8SAzael Avalos 			return -EINVAL;
2541fc5462f8SAzael Avalos 		if (!sci_open(toshiba_acpi))
2542fc5462f8SAzael Avalos 			return -EIO;
2543fc5462f8SAzael Avalos 		ret = toshiba_acpi_smm_bridge(&regs);
2544fc5462f8SAzael Avalos 		sci_close(toshiba_acpi);
2545fc5462f8SAzael Avalos 		if (ret)
2546fc5462f8SAzael Avalos 			return ret;
2547fc5462f8SAzael Avalos 		if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2548fc5462f8SAzael Avalos 			return -EFAULT;
2549fc5462f8SAzael Avalos 		break;
2550fc5462f8SAzael Avalos 	default:
2551fc5462f8SAzael Avalos 		return -EINVAL;
2552fc5462f8SAzael Avalos 	}
2553fc5462f8SAzael Avalos 
2554fc5462f8SAzael Avalos 	return 0;
2555fc5462f8SAzael Avalos }
2556fc5462f8SAzael Avalos 
2557fc5462f8SAzael Avalos static const struct file_operations toshiba_acpi_fops = {
2558fc5462f8SAzael Avalos 	.owner		= THIS_MODULE,
2559fc5462f8SAzael Avalos 	.unlocked_ioctl = toshiba_acpi_ioctl,
2560fc5462f8SAzael Avalos 	.llseek		= noop_llseek,
2561fc5462f8SAzael Avalos };
2562fc5462f8SAzael Avalos 
2563fc5462f8SAzael Avalos /*
25642fdde834SAzael Avalos  * WWAN RFKill handlers
25652fdde834SAzael Avalos  */
25662fdde834SAzael Avalos static int toshiba_acpi_wwan_set_block(void *data, bool blocked)
25672fdde834SAzael Avalos {
25682fdde834SAzael Avalos 	struct toshiba_acpi_dev *dev = data;
25692fdde834SAzael Avalos 	int ret;
25702fdde834SAzael Avalos 
25712fdde834SAzael Avalos 	ret = toshiba_wireless_status(dev);
25722fdde834SAzael Avalos 	if (ret)
25732fdde834SAzael Avalos 		return ret;
25742fdde834SAzael Avalos 
25752fdde834SAzael Avalos 	if (!dev->killswitch)
25762fdde834SAzael Avalos 		return 0;
25772fdde834SAzael Avalos 
25782fdde834SAzael Avalos 	return toshiba_wwan_set(dev, !blocked);
25792fdde834SAzael Avalos }
25802fdde834SAzael Avalos 
25812fdde834SAzael Avalos static void toshiba_acpi_wwan_poll(struct rfkill *rfkill, void *data)
25822fdde834SAzael Avalos {
25832fdde834SAzael Avalos 	struct toshiba_acpi_dev *dev = data;
25842fdde834SAzael Avalos 
25852fdde834SAzael Avalos 	if (toshiba_wireless_status(dev))
25862fdde834SAzael Avalos 		return;
25872fdde834SAzael Avalos 
25882fdde834SAzael Avalos 	rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
25892fdde834SAzael Avalos }
25902fdde834SAzael Avalos 
25912fdde834SAzael Avalos static const struct rfkill_ops wwan_rfk_ops = {
25922fdde834SAzael Avalos 	.set_block = toshiba_acpi_wwan_set_block,
25932fdde834SAzael Avalos 	.poll = toshiba_acpi_wwan_poll,
25942fdde834SAzael Avalos };
25952fdde834SAzael Avalos 
25962fdde834SAzael Avalos static int toshiba_acpi_setup_wwan_rfkill(struct toshiba_acpi_dev *dev)
25972fdde834SAzael Avalos {
25982fdde834SAzael Avalos 	int ret = toshiba_wireless_status(dev);
25992fdde834SAzael Avalos 
26002fdde834SAzael Avalos 	if (ret)
26012fdde834SAzael Avalos 		return ret;
26022fdde834SAzael Avalos 
26032fdde834SAzael Avalos 	dev->wwan_rfk = rfkill_alloc("Toshiba WWAN",
26042fdde834SAzael Avalos 				     &dev->acpi_dev->dev,
26052fdde834SAzael Avalos 				     RFKILL_TYPE_WWAN,
26062fdde834SAzael Avalos 				     &wwan_rfk_ops,
26072fdde834SAzael Avalos 				     dev);
26082fdde834SAzael Avalos 	if (!dev->wwan_rfk) {
26092fdde834SAzael Avalos 		pr_err("Unable to allocate WWAN rfkill device\n");
26102fdde834SAzael Avalos 		return -ENOMEM;
26112fdde834SAzael Avalos 	}
26122fdde834SAzael Avalos 
26132fdde834SAzael Avalos 	rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
26142fdde834SAzael Avalos 
26152fdde834SAzael Avalos 	ret = rfkill_register(dev->wwan_rfk);
26162fdde834SAzael Avalos 	if (ret) {
26172fdde834SAzael Avalos 		pr_err("Unable to register WWAN rfkill device\n");
26182fdde834SAzael Avalos 		rfkill_destroy(dev->wwan_rfk);
26192fdde834SAzael Avalos 	}
26202fdde834SAzael Avalos 
26212fdde834SAzael Avalos 	return ret;
26222fdde834SAzael Avalos }
26232fdde834SAzael Avalos 
26242fdde834SAzael Avalos /*
26251f28f290SAzael Avalos  * Hotkeys
26261f28f290SAzael Avalos  */
26271f28f290SAzael Avalos static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
26281f28f290SAzael Avalos {
26291f28f290SAzael Avalos 	acpi_status status;
26301f28f290SAzael Avalos 	u32 result;
26311f28f290SAzael Avalos 
26321f28f290SAzael Avalos 	status = acpi_evaluate_object(dev->acpi_dev->handle,
26331f28f290SAzael Avalos 				      "ENAB", NULL, NULL);
26341f28f290SAzael Avalos 	if (ACPI_FAILURE(status))
26351f28f290SAzael Avalos 		return -ENODEV;
26361f28f290SAzael Avalos 
2637b116fd00SAzael Avalos 	/*
2638b116fd00SAzael Avalos 	 * Enable the "Special Functions" mode only if they are
2639b116fd00SAzael Avalos 	 * supported and if they are activated.
2640b116fd00SAzael Avalos 	 */
2641b116fd00SAzael Avalos 	if (dev->kbd_function_keys_supported && dev->special_functions)
2642b116fd00SAzael Avalos 		result = hci_write(dev, HCI_HOTKEY_EVENT,
2643b116fd00SAzael Avalos 				   HCI_HOTKEY_SPECIAL_FUNCTIONS);
2644b116fd00SAzael Avalos 	else
2645d37782bdSAzael Avalos 		result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2646b116fd00SAzael Avalos 
26471f28f290SAzael Avalos 	if (result == TOS_FAILURE)
26481f28f290SAzael Avalos 		return -EIO;
26491f28f290SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
26501f28f290SAzael Avalos 		return -ENODEV;
26511f28f290SAzael Avalos 
26521f28f290SAzael Avalos 	return 0;
26531f28f290SAzael Avalos }
26541f28f290SAzael Avalos 
265529cd293fSSeth Forshee static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
265629cd293fSSeth Forshee 				      struct serio *port)
265729cd293fSSeth Forshee {
265898280374SGiedrius Statkevičius 	if (str & I8042_STR_AUXDATA)
265929cd293fSSeth Forshee 		return false;
266029cd293fSSeth Forshee 
266129cd293fSSeth Forshee 	if (unlikely(data == 0xe0))
266229cd293fSSeth Forshee 		return false;
266329cd293fSSeth Forshee 
266429cd293fSSeth Forshee 	if ((data & 0x7f) == TOS1900_FN_SCAN) {
266529cd293fSSeth Forshee 		schedule_work(&toshiba_acpi->hotkey_work);
266629cd293fSSeth Forshee 		return true;
266729cd293fSSeth Forshee 	}
266829cd293fSSeth Forshee 
266929cd293fSSeth Forshee 	return false;
267029cd293fSSeth Forshee }
267129cd293fSSeth Forshee 
267229cd293fSSeth Forshee static void toshiba_acpi_hotkey_work(struct work_struct *work)
267329cd293fSSeth Forshee {
267429cd293fSSeth Forshee 	acpi_handle ec_handle = ec_get_handle();
267529cd293fSSeth Forshee 	acpi_status status;
267629cd293fSSeth Forshee 
267729cd293fSSeth Forshee 	if (!ec_handle)
267829cd293fSSeth Forshee 		return;
267929cd293fSSeth Forshee 
268029cd293fSSeth Forshee 	status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
268129cd293fSSeth Forshee 	if (ACPI_FAILURE(status))
268229cd293fSSeth Forshee 		pr_err("ACPI NTFY method execution failed\n");
268329cd293fSSeth Forshee }
268429cd293fSSeth Forshee 
268529cd293fSSeth Forshee /*
268629cd293fSSeth Forshee  * Returns hotkey scancode, or < 0 on failure.
268729cd293fSSeth Forshee  */
268829cd293fSSeth Forshee static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
268929cd293fSSeth Forshee {
269074facaf7SZhang Rui 	unsigned long long value;
269129cd293fSSeth Forshee 	acpi_status status;
269229cd293fSSeth Forshee 
269374facaf7SZhang Rui 	status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
269474facaf7SZhang Rui 				      NULL, &value);
269574facaf7SZhang Rui 	if (ACPI_FAILURE(status)) {
269629cd293fSSeth Forshee 		pr_err("ACPI INFO method execution failed\n");
269729cd293fSSeth Forshee 		return -EIO;
269829cd293fSSeth Forshee 	}
269929cd293fSSeth Forshee 
270074facaf7SZhang Rui 	return value;
270129cd293fSSeth Forshee }
270229cd293fSSeth Forshee 
270329cd293fSSeth Forshee static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
270429cd293fSSeth Forshee 				       int scancode)
270529cd293fSSeth Forshee {
270629cd293fSSeth Forshee 	if (scancode == 0x100)
270729cd293fSSeth Forshee 		return;
270829cd293fSSeth Forshee 
2709e0769fe6SDarren Hart 	/* Act on key press; ignore key release */
271029cd293fSSeth Forshee 	if (scancode & 0x80)
271129cd293fSSeth Forshee 		return;
271229cd293fSSeth Forshee 
271329cd293fSSeth Forshee 	if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
271429cd293fSSeth Forshee 		pr_info("Unknown key %x\n", scancode);
271529cd293fSSeth Forshee }
271629cd293fSSeth Forshee 
271771454d78SAzael Avalos static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
271871454d78SAzael Avalos {
271971454d78SAzael Avalos 	if (dev->info_supported) {
27207deef550SAzael Avalos 		int scancode = toshiba_acpi_query_hotkey(dev);
27217deef550SAzael Avalos 
27227deef550SAzael Avalos 		if (scancode < 0) {
272371454d78SAzael Avalos 			pr_err("Failed to query hotkey event\n");
27247deef550SAzael Avalos 		} else if (scancode != 0) {
272571454d78SAzael Avalos 			toshiba_acpi_report_hotkey(dev, scancode);
27267deef550SAzael Avalos 			dev->key_event_valid = 1;
27277deef550SAzael Avalos 			dev->last_key_event = scancode;
27287deef550SAzael Avalos 		}
272971454d78SAzael Avalos 	} else if (dev->system_event_supported) {
27307deef550SAzael Avalos 		u32 result;
27317deef550SAzael Avalos 		u32 value;
27327deef550SAzael Avalos 		int retries = 3;
27337deef550SAzael Avalos 
273471454d78SAzael Avalos 		do {
27357deef550SAzael Avalos 			result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
27367deef550SAzael Avalos 			switch (result) {
273771454d78SAzael Avalos 			case TOS_SUCCESS:
273871454d78SAzael Avalos 				toshiba_acpi_report_hotkey(dev, (int)value);
27397deef550SAzael Avalos 				dev->key_event_valid = 1;
27407deef550SAzael Avalos 				dev->last_key_event = value;
274171454d78SAzael Avalos 				break;
274271454d78SAzael Avalos 			case TOS_NOT_SUPPORTED:
274371454d78SAzael Avalos 				/*
274471454d78SAzael Avalos 				 * This is a workaround for an unresolved
274571454d78SAzael Avalos 				 * issue on some machines where system events
274671454d78SAzael Avalos 				 * sporadically become disabled.
274771454d78SAzael Avalos 				 */
27487deef550SAzael Avalos 				result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
27497deef550SAzael Avalos 				if (result == TOS_SUCCESS)
275071454d78SAzael Avalos 					pr_notice("Re-enabled hotkeys\n");
2751e0769fe6SDarren Hart 				/* Fall through */
275271454d78SAzael Avalos 			default:
275371454d78SAzael Avalos 				retries--;
275471454d78SAzael Avalos 				break;
275571454d78SAzael Avalos 			}
27567deef550SAzael Avalos 		} while (retries && result != TOS_FIFO_EMPTY);
275771454d78SAzael Avalos 	}
275871454d78SAzael Avalos }
275971454d78SAzael Avalos 
2760b859f159SGreg Kroah-Hartman static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
27616335e4d5SMatthew Garrett {
2762fe808bfbSTakashi Iwai 	const struct key_entry *keymap = toshiba_acpi_keymap;
2763a2b3471bSAzael Avalos 	acpi_handle ec_handle;
2764a2b3471bSAzael Avalos 	int error;
2765a2b3471bSAzael Avalos 
27667faa6a37SAzael Avalos 	if (disable_hotkeys) {
27677faa6a37SAzael Avalos 		pr_info("Hotkeys disabled by module parameter\n");
27687faa6a37SAzael Avalos 		return 0;
27697faa6a37SAzael Avalos 	}
27707faa6a37SAzael Avalos 
2771a88bc06eSAzael Avalos 	if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2772a88bc06eSAzael Avalos 		pr_info("WMI event detected, hotkeys will not be monitored\n");
2773a88bc06eSAzael Avalos 		return 0;
2774a88bc06eSAzael Avalos 	}
2775a88bc06eSAzael Avalos 
2776a2b3471bSAzael Avalos 	error = toshiba_acpi_enable_hotkeys(dev);
2777a2b3471bSAzael Avalos 	if (error)
2778a2b3471bSAzael Avalos 		return error;
2779a2b3471bSAzael Avalos 
278010e6aaabSAzael Avalos 	if (toshiba_hotkey_event_type_get(dev, &dev->hotkey_event_type))
278153147b6cSAzael Avalos 		pr_notice("Unable to query Hotkey Event Type\n");
278253147b6cSAzael Avalos 
2783135740deSSeth Forshee 	dev->hotkey_dev = input_allocate_device();
2784b222cca6SJoe Perches 	if (!dev->hotkey_dev)
2785135740deSSeth Forshee 		return -ENOMEM;
2786135740deSSeth Forshee 
2787135740deSSeth Forshee 	dev->hotkey_dev->name = "Toshiba input device";
27886e02cc7eSSeth Forshee 	dev->hotkey_dev->phys = "toshiba_acpi/input0";
2789135740deSSeth Forshee 	dev->hotkey_dev->id.bustype = BUS_HOST;
2790135740deSSeth Forshee 
279110e6aaabSAzael Avalos 	if (dev->hotkey_event_type == HCI_SYSTEM_TYPE1 ||
2792a2b3471bSAzael Avalos 	    !dev->kbd_function_keys_supported)
2793a2b3471bSAzael Avalos 		keymap = toshiba_acpi_keymap;
279410e6aaabSAzael Avalos 	else if (dev->hotkey_event_type == HCI_SYSTEM_TYPE2 ||
2795a2b3471bSAzael Avalos 		 dev->kbd_function_keys_supported)
2796fe808bfbSTakashi Iwai 		keymap = toshiba_acpi_alt_keymap;
2797a2b3471bSAzael Avalos 	else
279810e6aaabSAzael Avalos 		pr_info("Unknown event type received %x\n",
279910e6aaabSAzael Avalos 			dev->hotkey_event_type);
2800fe808bfbSTakashi Iwai 	error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2801135740deSSeth Forshee 	if (error)
2802135740deSSeth Forshee 		goto err_free_dev;
2803135740deSSeth Forshee 
280429cd293fSSeth Forshee 	/*
280529cd293fSSeth Forshee 	 * For some machines the SCI responsible for providing hotkey
280629cd293fSSeth Forshee 	 * notification doesn't fire. We can trigger the notification
280729cd293fSSeth Forshee 	 * whenever the Fn key is pressed using the NTFY method, if
280829cd293fSSeth Forshee 	 * supported, so if it's present set up an i8042 key filter
280929cd293fSSeth Forshee 	 * for this purpose.
281029cd293fSSeth Forshee 	 */
281129cd293fSSeth Forshee 	ec_handle = ec_get_handle();
2812e2e19606SZhang Rui 	if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
281329cd293fSSeth Forshee 		INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
281429cd293fSSeth Forshee 
281529cd293fSSeth Forshee 		error = i8042_install_filter(toshiba_acpi_i8042_filter);
281629cd293fSSeth Forshee 		if (error) {
281729cd293fSSeth Forshee 			pr_err("Error installing key filter\n");
2818db8f95d0SMichał Kępień 			goto err_free_dev;
281929cd293fSSeth Forshee 		}
282029cd293fSSeth Forshee 
282129cd293fSSeth Forshee 		dev->ntfy_supported = 1;
282229cd293fSSeth Forshee 	}
282329cd293fSSeth Forshee 
282429cd293fSSeth Forshee 	/*
282529cd293fSSeth Forshee 	 * Determine hotkey query interface. Prefer using the INFO
282629cd293fSSeth Forshee 	 * method when it is available.
282729cd293fSSeth Forshee 	 */
2828e2e19606SZhang Rui 	if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
282929cd293fSSeth Forshee 		dev->info_supported = 1;
283010e6aaabSAzael Avalos 	else if (hci_write(dev, HCI_SYSTEM_EVENT, 1) == TOS_SUCCESS)
283129cd293fSSeth Forshee 		dev->system_event_supported = 1;
283229cd293fSSeth Forshee 
283329cd293fSSeth Forshee 	if (!dev->info_supported && !dev->system_event_supported) {
283429cd293fSSeth Forshee 		pr_warn("No hotkey query interface found\n");
283529cd293fSSeth Forshee 		goto err_remove_filter;
283629cd293fSSeth Forshee 	}
283729cd293fSSeth Forshee 
2838135740deSSeth Forshee 	error = input_register_device(dev->hotkey_dev);
2839135740deSSeth Forshee 	if (error) {
2840135740deSSeth Forshee 		pr_info("Unable to register input device\n");
284129cd293fSSeth Forshee 		goto err_remove_filter;
2842135740deSSeth Forshee 	}
2843135740deSSeth Forshee 
2844135740deSSeth Forshee 	return 0;
2845135740deSSeth Forshee 
284629cd293fSSeth Forshee  err_remove_filter:
284729cd293fSSeth Forshee 	if (dev->ntfy_supported)
284829cd293fSSeth Forshee 		i8042_remove_filter(toshiba_acpi_i8042_filter);
2849135740deSSeth Forshee  err_free_dev:
2850135740deSSeth Forshee 	input_free_device(dev->hotkey_dev);
2851135740deSSeth Forshee 	dev->hotkey_dev = NULL;
2852135740deSSeth Forshee 	return error;
2853135740deSSeth Forshee }
2854135740deSSeth Forshee 
2855b859f159SGreg Kroah-Hartman static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
285662cce752SSeth Forshee {
285762cce752SSeth Forshee 	struct backlight_properties props;
285862cce752SSeth Forshee 	int brightness;
285962cce752SSeth Forshee 	int ret;
286062cce752SSeth Forshee 
286162cce752SSeth Forshee 	/*
286262cce752SSeth Forshee 	 * Some machines don't support the backlight methods at all, and
286362cce752SSeth Forshee 	 * others support it read-only. Either of these is pretty useless,
286462cce752SSeth Forshee 	 * so only register the backlight device if the backlight method
286562cce752SSeth Forshee 	 * supports both reads and writes.
286662cce752SSeth Forshee 	 */
286762cce752SSeth Forshee 	brightness = __get_lcd_brightness(dev);
286862cce752SSeth Forshee 	if (brightness < 0)
286962cce752SSeth Forshee 		return 0;
2870bae5336fSAzael Avalos 	/*
2871bae5336fSAzael Avalos 	 * If transflective backlight is supported and the brightness is zero
2872bae5336fSAzael Avalos 	 * (lowest brightness level), the set_lcd_brightness function will
2873bae5336fSAzael Avalos 	 * activate the transflective backlight, making the LCD appear to be
2874bae5336fSAzael Avalos 	 * turned off, simply increment the brightness level to avoid that.
2875bae5336fSAzael Avalos 	 */
2876bae5336fSAzael Avalos 	if (dev->tr_backlight_supported && brightness == 0)
2877bae5336fSAzael Avalos 		brightness++;
287862cce752SSeth Forshee 	ret = set_lcd_brightness(dev, brightness);
287962cce752SSeth Forshee 	if (ret) {
288062cce752SSeth Forshee 		pr_debug("Backlight method is read-only, disabling backlight support\n");
288162cce752SSeth Forshee 		return 0;
288262cce752SSeth Forshee 	}
288362cce752SSeth Forshee 
2884358d6a2cSHans de Goede 	/*
2885358d6a2cSHans de Goede 	 * Tell acpi-video-detect code to prefer vendor backlight on all
2886358d6a2cSHans de Goede 	 * systems with transflective backlight and on dmi matched systems.
2887358d6a2cSHans de Goede 	 */
2888358d6a2cSHans de Goede 	if (dev->tr_backlight_supported ||
2889358d6a2cSHans de Goede 	    dmi_check_system(toshiba_vendor_backlight_dmi))
2890234b7cf8SHans de Goede 		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2891358d6a2cSHans de Goede 
2892234b7cf8SHans de Goede 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2893358d6a2cSHans de Goede 		return 0;
2894358d6a2cSHans de Goede 
289553039f22SMatthew Garrett 	memset(&props, 0, sizeof(props));
289662cce752SSeth Forshee 	props.type = BACKLIGHT_PLATFORM;
289762cce752SSeth Forshee 	props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
289862cce752SSeth Forshee 
2899e0769fe6SDarren Hart 	/* Adding an extra level and having 0 change to transflective mode */
2900121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported)
2901121b7b0dSAkio Idehara 		props.max_brightness++;
2902121b7b0dSAkio Idehara 
290362cce752SSeth Forshee 	dev->backlight_dev = backlight_device_register("toshiba",
290462cce752SSeth Forshee 						       &dev->acpi_dev->dev,
290562cce752SSeth Forshee 						       dev,
290662cce752SSeth Forshee 						       &toshiba_backlight_data,
290762cce752SSeth Forshee 						       &props);
290862cce752SSeth Forshee 	if (IS_ERR(dev->backlight_dev)) {
290962cce752SSeth Forshee 		ret = PTR_ERR(dev->backlight_dev);
291062cce752SSeth Forshee 		pr_err("Could not register toshiba backlight device\n");
291162cce752SSeth Forshee 		dev->backlight_dev = NULL;
291262cce752SSeth Forshee 		return ret;
291362cce752SSeth Forshee 	}
291462cce752SSeth Forshee 
291562cce752SSeth Forshee 	dev->backlight_dev->props.brightness = brightness;
291662cce752SSeth Forshee 	return 0;
291762cce752SSeth Forshee }
291862cce752SSeth Forshee 
29190409cbceSAzael Avalos static void print_supported_features(struct toshiba_acpi_dev *dev)
29200409cbceSAzael Avalos {
29210409cbceSAzael Avalos 	pr_info("Supported laptop features:");
29220409cbceSAzael Avalos 
29230409cbceSAzael Avalos 	if (dev->hotkey_dev)
29240409cbceSAzael Avalos 		pr_cont(" hotkeys");
29250409cbceSAzael Avalos 	if (dev->backlight_dev)
29260409cbceSAzael Avalos 		pr_cont(" backlight");
29270409cbceSAzael Avalos 	if (dev->video_supported)
29280409cbceSAzael Avalos 		pr_cont(" video-out");
29290409cbceSAzael Avalos 	if (dev->fan_supported)
29300409cbceSAzael Avalos 		pr_cont(" fan");
29310409cbceSAzael Avalos 	if (dev->tr_backlight_supported)
29320409cbceSAzael Avalos 		pr_cont(" transflective-backlight");
29330409cbceSAzael Avalos 	if (dev->illumination_supported)
29340409cbceSAzael Avalos 		pr_cont(" illumination");
29350409cbceSAzael Avalos 	if (dev->kbd_illum_supported)
29360409cbceSAzael Avalos 		pr_cont(" keyboard-backlight");
29370409cbceSAzael Avalos 	if (dev->touchpad_supported)
29380409cbceSAzael Avalos 		pr_cont(" touchpad");
29390409cbceSAzael Avalos 	if (dev->eco_supported)
29400409cbceSAzael Avalos 		pr_cont(" eco-led");
29410409cbceSAzael Avalos 	if (dev->accelerometer_supported)
29420409cbceSAzael Avalos 		pr_cont(" accelerometer-axes");
29430409cbceSAzael Avalos 	if (dev->usb_sleep_charge_supported)
29440409cbceSAzael Avalos 		pr_cont(" usb-sleep-charge");
29450409cbceSAzael Avalos 	if (dev->usb_rapid_charge_supported)
29460409cbceSAzael Avalos 		pr_cont(" usb-rapid-charge");
29470409cbceSAzael Avalos 	if (dev->usb_sleep_music_supported)
29480409cbceSAzael Avalos 		pr_cont(" usb-sleep-music");
29490409cbceSAzael Avalos 	if (dev->kbd_function_keys_supported)
29500409cbceSAzael Avalos 		pr_cont(" special-function-keys");
29510409cbceSAzael Avalos 	if (dev->panel_power_on_supported)
29520409cbceSAzael Avalos 		pr_cont(" panel-power-on");
29530409cbceSAzael Avalos 	if (dev->usb_three_supported)
29540409cbceSAzael Avalos 		pr_cont(" usb3");
29556873f46aSAzael Avalos 	if (dev->wwan_supported)
29566873f46aSAzael Avalos 		pr_cont(" wwan");
2957763ff32fSAzael Avalos 	if (dev->cooling_method_supported)
2958763ff32fSAzael Avalos 		pr_cont(" cooling-method");
29590409cbceSAzael Avalos 
29600409cbceSAzael Avalos 	pr_cont("\n");
29610409cbceSAzael Avalos }
29620409cbceSAzael Avalos 
296351fac838SRafael J. Wysocki static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
2964135740deSSeth Forshee {
2965135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2966135740deSSeth Forshee 
2967fc5462f8SAzael Avalos 	misc_deregister(&dev->miscdev);
2968fc5462f8SAzael Avalos 
296936d03f93SSeth Forshee 	remove_toshiba_proc_entries(dev);
2970135740deSSeth Forshee 
297198010f1eSAzael Avalos 	if (dev->accelerometer_supported && dev->indio_dev) {
297298010f1eSAzael Avalos 		iio_device_unregister(dev->indio_dev);
297398010f1eSAzael Avalos 		iio_device_free(dev->indio_dev);
297498010f1eSAzael Avalos 	}
297598010f1eSAzael Avalos 
2976360f0f39SAzael Avalos 	if (dev->sysfs_created)
2977360f0f39SAzael Avalos 		sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2978360f0f39SAzael Avalos 				   &toshiba_attr_group);
2979360f0f39SAzael Avalos 
298029cd293fSSeth Forshee 	if (dev->ntfy_supported) {
298129cd293fSSeth Forshee 		i8042_remove_filter(toshiba_acpi_i8042_filter);
298229cd293fSSeth Forshee 		cancel_work_sync(&dev->hotkey_work);
298329cd293fSSeth Forshee 	}
298429cd293fSSeth Forshee 
2985db8f95d0SMichał Kępień 	if (dev->hotkey_dev)
2986135740deSSeth Forshee 		input_unregister_device(dev->hotkey_dev);
2987135740deSSeth Forshee 
2988135740deSSeth Forshee 	backlight_device_unregister(dev->backlight_dev);
2989135740deSSeth Forshee 
2990135740deSSeth Forshee 	led_classdev_unregister(&dev->led_dev);
2991360f0f39SAzael Avalos 	led_classdev_unregister(&dev->kbd_led);
2992def6c4e2SAzael Avalos 	led_classdev_unregister(&dev->eco_led);
2993def6c4e2SAzael Avalos 
29942fdde834SAzael Avalos 	if (dev->wwan_rfk) {
29952fdde834SAzael Avalos 		rfkill_unregister(dev->wwan_rfk);
29962fdde834SAzael Avalos 		rfkill_destroy(dev->wwan_rfk);
29972fdde834SAzael Avalos 	}
29982fdde834SAzael Avalos 
299929cd293fSSeth Forshee 	if (toshiba_acpi)
300029cd293fSSeth Forshee 		toshiba_acpi = NULL;
300129cd293fSSeth Forshee 
3002135740deSSeth Forshee 	kfree(dev);
3003135740deSSeth Forshee 
3004135740deSSeth Forshee 	return 0;
3005135740deSSeth Forshee }
3006135740deSSeth Forshee 
3007b859f159SGreg Kroah-Hartman static const char *find_hci_method(acpi_handle handle)
3008a540d6b5SSeth Forshee {
3009e2e19606SZhang Rui 	if (acpi_has_method(handle, "GHCI"))
3010a540d6b5SSeth Forshee 		return "GHCI";
3011a540d6b5SSeth Forshee 
3012e2e19606SZhang Rui 	if (acpi_has_method(handle, "SPFC"))
3013a540d6b5SSeth Forshee 		return "SPFC";
3014a540d6b5SSeth Forshee 
3015a540d6b5SSeth Forshee 	return NULL;
3016a540d6b5SSeth Forshee }
3017a540d6b5SSeth Forshee 
3018b859f159SGreg Kroah-Hartman static int toshiba_acpi_add(struct acpi_device *acpi_dev)
3019135740deSSeth Forshee {
3020135740deSSeth Forshee 	struct toshiba_acpi_dev *dev;
3021a540d6b5SSeth Forshee 	const char *hci_method;
302236d03f93SSeth Forshee 	u32 dummy;
3023135740deSSeth Forshee 	int ret = 0;
3024135740deSSeth Forshee 
302529cd293fSSeth Forshee 	if (toshiba_acpi)
302629cd293fSSeth Forshee 		return -EBUSY;
302729cd293fSSeth Forshee 
3028135740deSSeth Forshee 	pr_info("Toshiba Laptop ACPI Extras version %s\n",
3029135740deSSeth Forshee 	       TOSHIBA_ACPI_VERSION);
3030135740deSSeth Forshee 
3031a540d6b5SSeth Forshee 	hci_method = find_hci_method(acpi_dev->handle);
3032a540d6b5SSeth Forshee 	if (!hci_method) {
3033a540d6b5SSeth Forshee 		pr_err("HCI interface not found\n");
30346e02cc7eSSeth Forshee 		return -ENODEV;
3035a540d6b5SSeth Forshee 	}
30366e02cc7eSSeth Forshee 
3037135740deSSeth Forshee 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3038135740deSSeth Forshee 	if (!dev)
3039135740deSSeth Forshee 		return -ENOMEM;
3040135740deSSeth Forshee 	dev->acpi_dev = acpi_dev;
3041a540d6b5SSeth Forshee 	dev->method_hci = hci_method;
3042fc5462f8SAzael Avalos 	dev->miscdev.minor = MISC_DYNAMIC_MINOR;
3043fc5462f8SAzael Avalos 	dev->miscdev.name = "toshiba_acpi";
3044fc5462f8SAzael Avalos 	dev->miscdev.fops = &toshiba_acpi_fops;
3045fc5462f8SAzael Avalos 
3046fc5462f8SAzael Avalos 	ret = misc_register(&dev->miscdev);
3047fc5462f8SAzael Avalos 	if (ret) {
3048fc5462f8SAzael Avalos 		pr_err("Failed to register miscdevice\n");
3049fc5462f8SAzael Avalos 		kfree(dev);
3050fc5462f8SAzael Avalos 		return ret;
3051fc5462f8SAzael Avalos 	}
3052fc5462f8SAzael Avalos 
3053135740deSSeth Forshee 	acpi_dev->driver_data = dev;
3054360f0f39SAzael Avalos 	dev_set_drvdata(&acpi_dev->dev, dev);
3055135740deSSeth Forshee 
3056a2b3471bSAzael Avalos 	/* Query the BIOS for supported features */
3057a2b3471bSAzael Avalos 
3058a2b3471bSAzael Avalos 	/*
3059a2b3471bSAzael Avalos 	 * The "Special Functions" are always supported by the laptops
3060a2b3471bSAzael Avalos 	 * with the new keyboard layout, query for its presence to help
3061a2b3471bSAzael Avalos 	 * determine the keymap layout to use.
3062a2b3471bSAzael Avalos 	 */
3063b116fd00SAzael Avalos 	ret = toshiba_function_keys_get(dev, &dev->special_functions);
3064a2b3471bSAzael Avalos 	dev->kbd_function_keys_supported = !ret;
3065a2b3471bSAzael Avalos 
3066d2f20619SAzael Avalos 	dev->hotkey_event_type = 0;
30676e02cc7eSSeth Forshee 	if (toshiba_acpi_setup_keyboard(dev))
3068135740deSSeth Forshee 		pr_info("Unable to activate hotkeys\n");
3069135740deSSeth Forshee 
3070695f6060SAzael Avalos 	/* Determine whether or not BIOS supports transflective backlight */
3071695f6060SAzael Avalos 	ret = get_tr_backlight_status(dev, &dummy);
3072695f6060SAzael Avalos 	dev->tr_backlight_supported = !ret;
3073695f6060SAzael Avalos 
307462cce752SSeth Forshee 	ret = toshiba_acpi_setup_backlight(dev);
307562cce752SSeth Forshee 	if (ret)
3076135740deSSeth Forshee 		goto error;
3077135740deSSeth Forshee 
3078ea215a3fSAzael Avalos 	toshiba_illumination_available(dev);
3079ea215a3fSAzael Avalos 	if (dev->illumination_supported) {
3080135740deSSeth Forshee 		dev->led_dev.name = "toshiba::illumination";
3081135740deSSeth Forshee 		dev->led_dev.max_brightness = 1;
3082135740deSSeth Forshee 		dev->led_dev.brightness_set = toshiba_illumination_set;
3083135740deSSeth Forshee 		dev->led_dev.brightness_get = toshiba_illumination_get;
3084409f3aedSAndy Shevchenko 		led_classdev_register(&acpi_dev->dev, &dev->led_dev);
3085135740deSSeth Forshee 	}
3086135740deSSeth Forshee 
3087ea215a3fSAzael Avalos 	toshiba_eco_mode_available(dev);
3088ea215a3fSAzael Avalos 	if (dev->eco_supported) {
3089def6c4e2SAzael Avalos 		dev->eco_led.name = "toshiba::eco_mode";
3090def6c4e2SAzael Avalos 		dev->eco_led.max_brightness = 1;
3091def6c4e2SAzael Avalos 		dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
3092def6c4e2SAzael Avalos 		dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
3093409f3aedSAndy Shevchenko 		led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led);
3094def6c4e2SAzael Avalos 	}
3095def6c4e2SAzael Avalos 
3096ea215a3fSAzael Avalos 	toshiba_kbd_illum_available(dev);
3097360f0f39SAzael Avalos 	/*
3098360f0f39SAzael Avalos 	 * Only register the LED if KBD illumination is supported
3099360f0f39SAzael Avalos 	 * and the keyboard backlight operation mode is set to FN-Z
3100147288e6SAzael Avalos 	 * or we detect a second gen keyboard backlight
3101360f0f39SAzael Avalos 	 */
3102147288e6SAzael Avalos 	if (dev->kbd_illum_supported &&
3103147288e6SAzael Avalos 	    (dev->kbd_mode == SCI_KBD_MODE_FNZ || dev->kbd_type == 2)) {
3104360f0f39SAzael Avalos 		dev->kbd_led.name = "toshiba::kbd_backlight";
3105147288e6SAzael Avalos 		dev->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
3106360f0f39SAzael Avalos 		dev->kbd_led.max_brightness = 1;
3107360f0f39SAzael Avalos 		dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
3108360f0f39SAzael Avalos 		dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
3109409f3aedSAndy Shevchenko 		led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led);
3110360f0f39SAzael Avalos 	}
3111360f0f39SAzael Avalos 
31129d8658acSAzael Avalos 	ret = toshiba_touchpad_get(dev, &dummy);
31139d8658acSAzael Avalos 	dev->touchpad_supported = !ret;
31149d8658acSAzael Avalos 
3115ea215a3fSAzael Avalos 	toshiba_accelerometer_available(dev);
311698010f1eSAzael Avalos 	if (dev->accelerometer_supported) {
3117*78289b4aSAlexandru Ardelean 		dev->indio_dev = iio_device_alloc(&acpi_dev->dev, sizeof(*dev));
311898010f1eSAzael Avalos 		if (!dev->indio_dev) {
311998010f1eSAzael Avalos 			pr_err("Unable to allocate iio device\n");
312098010f1eSAzael Avalos 			goto iio_error;
312198010f1eSAzael Avalos 		}
312298010f1eSAzael Avalos 
312398010f1eSAzael Avalos 		pr_info("Registering Toshiba accelerometer iio device\n");
312498010f1eSAzael Avalos 
312598010f1eSAzael Avalos 		dev->indio_dev->info = &toshiba_iio_accel_info;
312698010f1eSAzael Avalos 		dev->indio_dev->name = "Toshiba accelerometer";
312798010f1eSAzael Avalos 		dev->indio_dev->modes = INDIO_DIRECT_MODE;
312898010f1eSAzael Avalos 		dev->indio_dev->channels = toshiba_iio_accel_channels;
312998010f1eSAzael Avalos 		dev->indio_dev->num_channels =
313098010f1eSAzael Avalos 					ARRAY_SIZE(toshiba_iio_accel_channels);
313198010f1eSAzael Avalos 
313298010f1eSAzael Avalos 		ret = iio_device_register(dev->indio_dev);
313398010f1eSAzael Avalos 		if (ret < 0) {
313498010f1eSAzael Avalos 			pr_err("Unable to register iio device\n");
313598010f1eSAzael Avalos 			iio_device_free(dev->indio_dev);
313698010f1eSAzael Avalos 		}
313798010f1eSAzael Avalos 	}
313898010f1eSAzael Avalos iio_error:
31395a2813e9SAzael Avalos 
3140c8c91842SAzael Avalos 	toshiba_usb_sleep_charge_available(dev);
3141e26ffe51SAzael Avalos 
3142bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_get(dev, &dummy);
3143bb3fe01fSAzael Avalos 	dev->usb_rapid_charge_supported = !ret;
3144bb3fe01fSAzael Avalos 
3145172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_get(dev, &dummy);
3146172ce0a9SAzael Avalos 	dev->usb_sleep_music_supported = !ret;
3147172ce0a9SAzael Avalos 
314835d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_get(dev, &dummy);
314935d53ceaSAzael Avalos 	dev->panel_power_on_supported = !ret;
315035d53ceaSAzael Avalos 
315117fe4b3dSAzael Avalos 	ret = toshiba_usb_three_get(dev, &dummy);
315217fe4b3dSAzael Avalos 	dev->usb_three_supported = !ret;
315317fe4b3dSAzael Avalos 
315436d03f93SSeth Forshee 	ret = get_video_status(dev, &dummy);
315536d03f93SSeth Forshee 	dev->video_supported = !ret;
315636d03f93SSeth Forshee 
315736d03f93SSeth Forshee 	ret = get_fan_status(dev, &dummy);
315836d03f93SSeth Forshee 	dev->fan_supported = !ret;
315936d03f93SSeth Forshee 
31606873f46aSAzael Avalos 	toshiba_wwan_available(dev);
31612fdde834SAzael Avalos 	if (dev->wwan_supported)
31622fdde834SAzael Avalos 		toshiba_acpi_setup_wwan_rfkill(dev);
31636873f46aSAzael Avalos 
3164763ff32fSAzael Avalos 	toshiba_cooling_method_available(dev);
3165763ff32fSAzael Avalos 
31660409cbceSAzael Avalos 	print_supported_features(dev);
31670409cbceSAzael Avalos 
3168360f0f39SAzael Avalos 	ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
3169360f0f39SAzael Avalos 				 &toshiba_attr_group);
3170360f0f39SAzael Avalos 	if (ret) {
3171360f0f39SAzael Avalos 		dev->sysfs_created = 0;
3172360f0f39SAzael Avalos 		goto error;
3173360f0f39SAzael Avalos 	}
3174360f0f39SAzael Avalos 	dev->sysfs_created = !ret;
3175360f0f39SAzael Avalos 
317636d03f93SSeth Forshee 	create_toshiba_proc_entries(dev);
317736d03f93SSeth Forshee 
317829cd293fSSeth Forshee 	toshiba_acpi = dev;
317929cd293fSSeth Forshee 
3180135740deSSeth Forshee 	return 0;
3181135740deSSeth Forshee 
3182135740deSSeth Forshee error:
318351fac838SRafael J. Wysocki 	toshiba_acpi_remove(acpi_dev);
3184135740deSSeth Forshee 	return ret;
3185135740deSSeth Forshee }
3186135740deSSeth Forshee 
3187135740deSSeth Forshee static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
3188135740deSSeth Forshee {
3189135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
31906335e4d5SMatthew Garrett 
319171454d78SAzael Avalos 	switch (event) {
319271454d78SAzael Avalos 	case 0x80: /* Hotkeys and some system events */
3193a88bc06eSAzael Avalos 		/*
3194a88bc06eSAzael Avalos 		 * Machines with this WMI GUID aren't supported due to bugs in
3195a88bc06eSAzael Avalos 		 * their AML.
3196a88bc06eSAzael Avalos 		 *
3197a88bc06eSAzael Avalos 		 * Return silently to avoid triggering a netlink event.
3198a88bc06eSAzael Avalos 		 */
3199a88bc06eSAzael Avalos 		if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
3200a88bc06eSAzael Avalos 			return;
320171454d78SAzael Avalos 		toshiba_acpi_process_hotkeys(dev);
320211948b93SSeth Forshee 		break;
3203bab09e23SAzael Avalos 	case 0x81: /* Dock events */
3204bab09e23SAzael Avalos 	case 0x82:
3205bab09e23SAzael Avalos 	case 0x83:
3206bab09e23SAzael Avalos 		pr_info("Dock event received %x\n", event);
3207bab09e23SAzael Avalos 		break;
3208bab09e23SAzael Avalos 	case 0x88: /* Thermal events */
3209bab09e23SAzael Avalos 		pr_info("Thermal event received\n");
3210bab09e23SAzael Avalos 		break;
3211bab09e23SAzael Avalos 	case 0x8f: /* LID closed */
3212bab09e23SAzael Avalos 	case 0x90: /* LID is closed and Dock has been ejected */
3213bab09e23SAzael Avalos 		break;
3214bab09e23SAzael Avalos 	case 0x8c: /* SATA power events */
3215bab09e23SAzael Avalos 	case 0x8b:
3216bab09e23SAzael Avalos 		pr_info("SATA power event received %x\n", event);
3217bab09e23SAzael Avalos 		break;
321880546905SAzael Avalos 	case 0x92: /* Keyboard backlight mode changed */
3219147288e6SAzael Avalos 		dev->kbd_event_generated = true;
322080546905SAzael Avalos 		/* Update sysfs entries */
322165e3cf9cSAzael Avalos 		if (sysfs_update_group(&acpi_dev->dev.kobj,
322265e3cf9cSAzael Avalos 				       &toshiba_attr_group))
322380546905SAzael Avalos 			pr_err("Unable to update sysfs entries\n");
3224147288e6SAzael Avalos 		/* Notify LED subsystem about keyboard backlight change */
3225147288e6SAzael Avalos 		if (dev->kbd_type == 2 && dev->kbd_mode != SCI_KBD_MODE_AUTO)
3226147288e6SAzael Avalos 			led_classdev_notify_brightness_hw_changed(&dev->kbd_led,
3227147288e6SAzael Avalos 					(dev->kbd_mode == SCI_KBD_MODE_ON) ?
3228147288e6SAzael Avalos 					LED_FULL : LED_OFF);
322980546905SAzael Avalos 		break;
3230bab09e23SAzael Avalos 	case 0x85: /* Unknown */
3231bab09e23SAzael Avalos 	case 0x8d: /* Unknown */
323271454d78SAzael Avalos 	case 0x8e: /* Unknown */
3233bab09e23SAzael Avalos 	case 0x94: /* Unknown */
3234bab09e23SAzael Avalos 	case 0x95: /* Unknown */
323511948b93SSeth Forshee 	default:
323671454d78SAzael Avalos 		pr_info("Unknown event received %x\n", event);
323711948b93SSeth Forshee 		break;
32386335e4d5SMatthew Garrett 	}
3239bab09e23SAzael Avalos 
3240bab09e23SAzael Avalos 	acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
3241bab09e23SAzael Avalos 					dev_name(&acpi_dev->dev),
324213ae84f9SAzael Avalos 					event, (event == 0x80) ?
324313ae84f9SAzael Avalos 					dev->last_key_event : 0);
324429cd293fSSeth Forshee }
32456335e4d5SMatthew Garrett 
32463567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
324743d2fd3bSRafael J. Wysocki static int toshiba_acpi_suspend(struct device *device)
324829cd293fSSeth Forshee {
324943d2fd3bSRafael J. Wysocki 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
32501e574dbfSAzael Avalos 
32511e574dbfSAzael Avalos 	if (dev->hotkey_dev) {
325229cd293fSSeth Forshee 		u32 result;
325329cd293fSSeth Forshee 
3254d37782bdSAzael Avalos 		result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
32551e574dbfSAzael Avalos 		if (result != TOS_SUCCESS)
32561e574dbfSAzael Avalos 			pr_info("Unable to disable hotkeys\n");
32571e574dbfSAzael Avalos 	}
325829cd293fSSeth Forshee 
325929cd293fSSeth Forshee 	return 0;
326029cd293fSSeth Forshee }
326129cd293fSSeth Forshee 
326243d2fd3bSRafael J. Wysocki static int toshiba_acpi_resume(struct device *device)
326329cd293fSSeth Forshee {
326443d2fd3bSRafael J. Wysocki 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
326529cd293fSSeth Forshee 
3266e7fdb762SBenjamin Tissoires 	if (dev->hotkey_dev) {
32672fdde834SAzael Avalos 		if (toshiba_acpi_enable_hotkeys(dev))
3268e7fdb762SBenjamin Tissoires 			pr_info("Unable to re-enable hotkeys\n");
3269e7fdb762SBenjamin Tissoires 	}
327029cd293fSSeth Forshee 
32712fdde834SAzael Avalos 	if (dev->wwan_rfk) {
32722fdde834SAzael Avalos 		if (!toshiba_wireless_status(dev))
32732fdde834SAzael Avalos 			rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
32742fdde834SAzael Avalos 	}
32752fdde834SAzael Avalos 
327629cd293fSSeth Forshee 	return 0;
327729cd293fSSeth Forshee }
32783567a4e2SRafael J. Wysocki #endif
32796335e4d5SMatthew Garrett 
328043d2fd3bSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
328143d2fd3bSRafael J. Wysocki 			 toshiba_acpi_suspend, toshiba_acpi_resume);
328243d2fd3bSRafael J. Wysocki 
3283135740deSSeth Forshee static struct acpi_driver toshiba_acpi_driver = {
3284135740deSSeth Forshee 	.name	= "Toshiba ACPI driver",
3285135740deSSeth Forshee 	.owner	= THIS_MODULE,
3286135740deSSeth Forshee 	.ids	= toshiba_device_ids,
3287135740deSSeth Forshee 	.flags	= ACPI_DRIVER_ALL_NOTIFY_EVENTS,
3288135740deSSeth Forshee 	.ops	= {
3289135740deSSeth Forshee 		.add		= toshiba_acpi_add,
3290135740deSSeth Forshee 		.remove		= toshiba_acpi_remove,
3291135740deSSeth Forshee 		.notify		= toshiba_acpi_notify,
3292135740deSSeth Forshee 	},
329343d2fd3bSRafael J. Wysocki 	.drv.pm	= &toshiba_acpi_pm,
3294135740deSSeth Forshee };
3295b4f9fe12SLen Brown 
3296b4f9fe12SLen Brown static int __init toshiba_acpi_init(void)
3297b4f9fe12SLen Brown {
3298135740deSSeth Forshee 	int ret;
3299b4f9fe12SLen Brown 
3300b4f9fe12SLen Brown 	toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
3301b4f9fe12SLen Brown 	if (!toshiba_proc_dir) {
3302135740deSSeth Forshee 		pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
3303b4f9fe12SLen Brown 		return -ENODEV;
3304b4f9fe12SLen Brown 	}
3305b4f9fe12SLen Brown 
3306135740deSSeth Forshee 	ret = acpi_bus_register_driver(&toshiba_acpi_driver);
3307b4f9fe12SLen Brown 	if (ret) {
3308135740deSSeth Forshee 		pr_err("Failed to register ACPI driver: %d\n", ret);
3309135740deSSeth Forshee 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
3310135740deSSeth Forshee 	}
3311135740deSSeth Forshee 
3312b4f9fe12SLen Brown 	return ret;
3313b4f9fe12SLen Brown }
3314b4f9fe12SLen Brown 
3315135740deSSeth Forshee static void __exit toshiba_acpi_exit(void)
3316135740deSSeth Forshee {
3317135740deSSeth Forshee 	acpi_bus_unregister_driver(&toshiba_acpi_driver);
3318135740deSSeth Forshee 	if (toshiba_proc_dir)
3319135740deSSeth Forshee 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
3320b4f9fe12SLen Brown }
3321b4f9fe12SLen Brown 
3322b4f9fe12SLen Brown module_init(toshiba_acpi_init);
3323b4f9fe12SLen Brown module_exit(toshiba_acpi_exit);
3324