blob: 2a63ad043c4b4b982a8822179c318a304b99c56b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04005 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02006 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07007 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Darren Hartc57c0fa2015-02-11 21:25:22 -080019 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches7e334602011-03-29 15:21:52 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Azael Avalos7216d702015-02-10 21:09:21 -070034#define TOSHIBA_ACPI_VERSION "0.21"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define PROC_INTERFACE_VERSION 1
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080042#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070043#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040044#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050045#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070046#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020047#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060049#include <linux/workqueue.h>
50#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080051#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020052#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_AUTHOR("John Belmonte");
56MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
57MODULE_LICENSE("GPL");
58
Seth Forsheef11f9992012-01-18 13:44:11 -060059#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
60
Seth Forshee29cd2932012-01-18 13:44:09 -060061/* Scan code for Fn key on TOS1900 models */
62#define TOS1900_FN_SCAN 0x6e
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
66
Darren Harte0769fe2015-02-11 20:50:08 -080067/*
68 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060069 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
72 * be uniform across all their models. Ideally we would just call
73 * dedicated ACPI methods instead of using this primitive interface.
74 * However the ACPI methods seem to be incomplete in some areas (for
75 * example they allow setting, but not reading, the LCD brightness value),
76 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040077 *
Azael Avalos84a62732014-03-25 20:38:29 -060078 * SCI stands for "System Configuration Interface" which aim is to
79 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
Azael Avalos258c5902014-09-29 20:40:07 -060082#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* operations */
85#define HCI_SET 0xff00
86#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060087#define SCI_OPEN 0xf100
88#define SCI_CLOSE 0xf200
89#define SCI_GET 0xf300
90#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060093#define TOS_SUCCESS 0x0000
94#define TOS_OPEN_CLOSE_OK 0x0044
95#define TOS_FAILURE 0x1000
96#define TOS_NOT_SUPPORTED 0x8000
97#define TOS_ALREADY_OPEN 0x8100
98#define TOS_NOT_OPENED 0x8200
99#define TOS_INPUT_DATA_ERROR 0x8300
100#define TOS_WRITE_PROTECTED 0x8400
101#define TOS_NOT_PRESENT 0x8600
102#define TOS_FIFO_EMPTY 0x8c00
103#define TOS_DATA_NOT_AVAILABLE 0x8d20
104#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700105#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* registers */
108#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900109#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define HCI_SYSTEM_EVENT 0x0016
111#define HCI_VIDEO_OUT 0x001c
112#define HCI_HOTKEY_EVENT 0x001e
113#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400114#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600115#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600116#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600117#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600118#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600119#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700120#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600121#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700122#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600123#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700124#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700125#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600126#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700127#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600130#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600131#define HCI_HOTKEY_DISABLE 0x0b
132#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define HCI_LCD_BRIGHTNESS_BITS 3
134#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
135#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600136#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600137#define HCI_SYSTEM_TYPE1 0x10
138#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define HCI_VIDEO_OUT_LCD 0x1
140#define HCI_VIDEO_OUT_CRT 0x2
141#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400142#define HCI_WIRELESS_KILL_SWITCH 0x01
143#define HCI_WIRELESS_BT_PRESENT 0x0f
144#define HCI_WIRELESS_BT_ATTACH 0x40
145#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600146#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600147#define SCI_KBD_MODE_FNZ 0x1
148#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600149#define SCI_KBD_MODE_ON 0x8
150#define SCI_KBD_MODE_OFF 0x10
151#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700152#define SCI_USB_CHARGE_MODE_MASK 0xff
153#define SCI_USB_CHARGE_DISABLED 0x30000
154#define SCI_USB_CHARGE_ALTERNATE 0x30009
155#define SCI_USB_CHARGE_AUTO 0x30021
Azael Avalos182bcaa2015-01-18 18:30:23 -0700156#define SCI_USB_CHARGE_BAT_MASK 0x7
157#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
158#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
159#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700160#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Seth Forshee135740d2011-09-20 16:55:49 -0500162struct toshiba_acpi_dev {
163 struct acpi_device *acpi_dev;
164 const char *method_hci;
165 struct rfkill *bt_rfk;
166 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600167 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500168 struct backlight_device *backlight_dev;
169 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600170 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600171 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500172
Seth Forshee135740d2011-09-20 16:55:49 -0500173 int force_fan;
174 int last_key_event;
175 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600176 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600177 int kbd_mode;
178 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700179 int usbsc_bat_level;
Seth Forshee135740d2011-09-20 16:55:49 -0500180
Dan Carpenter592b7462012-01-15 14:28:20 +0300181 unsigned int illumination_supported:1;
182 unsigned int video_supported:1;
183 unsigned int fan_supported:1;
184 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600185 unsigned int ntfy_supported:1;
186 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900187 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600188 unsigned int kbd_illum_supported:1;
189 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600190 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600191 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600192 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700193 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700194 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700195 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700196 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700197 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700198 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600199 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500200
Seth Forshee135740d2011-09-20 16:55:49 -0500201 struct mutex mutex;
202};
203
Seth Forshee29cd2932012-01-18 13:44:09 -0600204static struct toshiba_acpi_dev *toshiba_acpi;
205
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800206static const struct acpi_device_id toshiba_device_ids[] = {
207 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100208 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400209 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800210 {"TOS1900", 0},
211 {"", 0},
212};
213MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
214
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800215static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100216 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700217 { KE_KEY, 0x101, { KEY_MUTE } },
218 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
219 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600220 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600221 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
222 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700223 { KE_KEY, 0x13b, { KEY_COFFEE } },
224 { KE_KEY, 0x13c, { KEY_BATTERY } },
225 { KE_KEY, 0x13d, { KEY_SLEEP } },
226 { KE_KEY, 0x13e, { KEY_SUSPEND } },
227 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
228 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
229 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
230 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600231 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100232 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700233 { KE_KEY, 0xb05, { KEY_PROG2 } },
234 { KE_KEY, 0xb06, { KEY_WWW } },
235 { KE_KEY, 0xb07, { KEY_MAIL } },
236 { KE_KEY, 0xb30, { KEY_STOP } },
237 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
238 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
239 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
240 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600241 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
242 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
243 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
244 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
245 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700246 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500247};
248
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200249/* alternative keymap */
250static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
251 {
252 .matches = {
253 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
255 },
256 },
Azael Avalose6efad72014-08-04 09:21:02 -0600257 {
258 .matches = {
259 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
260 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
261 },
262 },
Aaron Lub1bde682014-10-23 16:18:02 +0800263 {
264 .matches = {
265 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
266 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
267 },
268 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200269 {}
270};
271
272static const struct key_entry toshiba_acpi_alt_keymap[] = {
273 { KE_KEY, 0x157, { KEY_MUTE } },
274 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
275 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600276 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200277 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
278 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
279 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
280 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
281 { KE_KEY, 0x158, { KEY_WLAN } },
282 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
283 { KE_END, 0 },
284};
285
Darren Harte0769fe2015-02-11 20:50:08 -0800286/*
287 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289
Azael Avalosb5163992015-02-10 23:43:56 -0700290static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 *word = (*word & ~mask) | (mask * value);
293}
294
Darren Harte0769fe2015-02-11 20:50:08 -0800295/*
296 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 acpi_status status;
302
Zhang Rui619400d2013-09-03 08:31:56 +0800303 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500304 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Darren Harte0769fe2015-02-11 20:50:08 -0800307/*
308 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600309 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Azael Avalos258c5902014-09-29 20:40:07 -0600311static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
312 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600315 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600317 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 acpi_status status;
319 int i;
320
Azael Avalos258c5902014-09-29 20:40:07 -0600321 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600323 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 in_objs[i].type = ACPI_TYPE_INTEGER;
325 in_objs[i].integer.value = in[i];
326 }
327
328 results.length = sizeof(out_objs);
329 results.pointer = out_objs;
330
Seth Forshee6e02cc72011-09-20 16:55:51 -0500331 status = acpi_evaluate_object(dev->acpi_dev->handle,
332 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400333 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600334 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700335 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 return status;
340}
341
Darren Harte0769fe2015-02-11 20:50:08 -0800342/*
343 * Common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
345 * In addition to the ACPI status, the HCI system returns a result which
346 * may be useful (such as "not supported").
347 */
348
Azael Avalos893f3f62014-09-29 20:40:09 -0600349static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Azael Avalos258c5902014-09-29 20:40:07 -0600351 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
352 u32 out[TCI_WORDS];
353 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600354
355 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Azael Avalos893f3f62014-09-29 20:40:09 -0600358static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Azael Avalos258c5902014-09-29 20:40:07 -0600360 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
361 u32 out[TCI_WORDS];
362 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700363
Azael Avalos893f3f62014-09-29 20:40:09 -0600364 if (ACPI_FAILURE(status))
365 return TOS_FAILURE;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600368
369 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Azael Avalos893f3f62014-09-29 20:40:09 -0600372static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400373{
Azael Avalos258c5902014-09-29 20:40:07 -0600374 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
375 u32 out[TCI_WORDS];
376 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600377
378 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400379}
380
Azael Avalosb5163992015-02-10 23:43:56 -0700381static u32 hci_read2(struct toshiba_acpi_dev *dev,
382 u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400383{
Azael Avalos258c5902014-09-29 20:40:07 -0600384 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
385 u32 out[TCI_WORDS];
386 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700387
Azael Avalos893f3f62014-09-29 20:40:09 -0600388 if (ACPI_FAILURE(status))
389 return TOS_FAILURE;
390
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400391 *out1 = out[2];
392 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600393
394 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400395}
396
Darren Harte0769fe2015-02-11 20:50:08 -0800397/*
398 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600399 */
400
401static int sci_open(struct toshiba_acpi_dev *dev)
402{
Azael Avalos258c5902014-09-29 20:40:07 -0600403 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
404 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600405 acpi_status status;
406
Azael Avalos258c5902014-09-29 20:40:07 -0600407 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600408 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600409 pr_err("ACPI call to open SCI failed\n");
410 return 0;
411 }
412
Azael Avalos1864bbc2014-09-29 20:40:08 -0600413 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600414 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600415 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600416 pr_info("Toshiba SCI already opened\n");
417 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700418 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800419 /*
420 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700421 * implemented and return 0x8000 (Not Supported), failing to
422 * register some supported features.
423 *
424 * Simply return 1 if we hit those affected laptops to make the
425 * supported features work.
426 *
427 * In the case that some laptops really do not support the SCI,
428 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
429 * and thus, not registering support for the queried feature.
430 */
431 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600432 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600433 pr_info("Toshiba SCI is not present\n");
434 }
435
436 return 0;
437}
438
439static void sci_close(struct toshiba_acpi_dev *dev)
440{
Azael Avalos258c5902014-09-29 20:40:07 -0600441 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
442 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600443 acpi_status status;
444
Azael Avalos258c5902014-09-29 20:40:07 -0600445 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600446 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600447 pr_err("ACPI call to close SCI failed\n");
448 return;
449 }
450
Azael Avalos1864bbc2014-09-29 20:40:08 -0600451 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600452 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600453 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600454 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600455 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600456 pr_info("Toshiba SCI is not present\n");
457}
458
Azael Avalos893f3f62014-09-29 20:40:09 -0600459static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600460{
Azael Avalos258c5902014-09-29 20:40:07 -0600461 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
462 u32 out[TCI_WORDS];
463 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700464
Azael Avalos893f3f62014-09-29 20:40:09 -0600465 if (ACPI_FAILURE(status))
466 return TOS_FAILURE;
467
Azael Avalos84a62732014-03-25 20:38:29 -0600468 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600469
470 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600471}
472
Azael Avalos893f3f62014-09-29 20:40:09 -0600473static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600474{
Azael Avalos258c5902014-09-29 20:40:07 -0600475 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
476 u32 out[TCI_WORDS];
477 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600478
479 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600480}
481
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200482/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500483static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200484{
Azael Avalos258c5902014-09-29 20:40:07 -0600485 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
486 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200487 acpi_status status;
488
Azael Avalosfdb79082014-03-25 20:38:30 -0600489 if (!sci_open(dev))
490 return 0;
491
Azael Avalos258c5902014-09-29 20:40:07 -0600492 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600493 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600494 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600495 pr_err("ACPI call to query Illumination support failed\n");
496 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600497 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700498 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200499 return 0;
500 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600501
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200502 return 1;
503}
504
505static void toshiba_illumination_set(struct led_classdev *cdev,
506 enum led_brightness brightness)
507{
Seth Forshee135740d2011-09-20 16:55:49 -0500508 struct toshiba_acpi_dev *dev = container_of(cdev,
509 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600510 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200511
512 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600513 if (!sci_open(dev))
514 return;
515
516 /* Switch the illumination on/off */
517 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600518 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600519 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600520 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600521 pr_err("ACPI call for illumination failed\n");
522 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600523 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600524 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200525 return;
526 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200527}
528
529static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
530{
Seth Forshee135740d2011-09-20 16:55:49 -0500531 struct toshiba_acpi_dev *dev = container_of(cdev,
532 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600533 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200534
535 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600536 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200537 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200538
539 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600540 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600541 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600542 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600543 pr_err("ACPI call for illumination failed\n");
544 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600545 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600546 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200547 return LED_OFF;
548 }
549
Azael Avalosfdb79082014-03-25 20:38:30 -0600550 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200551}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400552
Azael Avalos360f0f32014-03-25 20:38:31 -0600553/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600554static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
555{
Azael Avalos258c5902014-09-29 20:40:07 -0600556 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
557 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600558 acpi_status status;
559
560 if (!sci_open(dev))
561 return 0;
562
Azael Avalos258c5902014-09-29 20:40:07 -0600563 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600564 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600565 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600566 pr_err("ACPI call to query kbd illumination support failed\n");
567 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600568 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600569 pr_info("Keyboard illumination not available\n");
570 return 0;
571 }
572
Darren Harte0769fe2015-02-11 20:50:08 -0800573 /*
574 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600575 * previous kbd backlight implementation set this to
576 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800577 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600578 */
579 if (out[3] == SCI_KBD_TIME_MAX)
580 dev->kbd_type = 2;
581 else
582 dev->kbd_type = 1;
583 /* Get the current keyboard backlight mode */
584 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
585 /* Get the current time (1-60 seconds) */
586 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
587
588 return 1;
589}
590
Azael Avalos360f0f32014-03-25 20:38:31 -0600591static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
592{
593 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600594
595 if (!sci_open(dev))
596 return -EIO;
597
Azael Avalos893f3f62014-09-29 20:40:09 -0600598 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600599 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600600 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600601 pr_err("ACPI call to set KBD backlight status failed\n");
602 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600603 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600604 pr_info("Keyboard backlight status not supported\n");
605 return -ENODEV;
606 }
607
608 return 0;
609}
610
611static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
612{
613 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600614
615 if (!sci_open(dev))
616 return -EIO;
617
Azael Avalos893f3f62014-09-29 20:40:09 -0600618 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600619 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600620 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600621 pr_err("ACPI call to get KBD backlight status failed\n");
622 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600623 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600624 pr_info("Keyboard backlight status not supported\n");
625 return -ENODEV;
626 }
627
628 return 0;
629}
630
631static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
632{
633 struct toshiba_acpi_dev *dev = container_of(cdev,
634 struct toshiba_acpi_dev, kbd_led);
635 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600636
637 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600638 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
639 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600640 pr_err("ACPI call to get the keyboard backlight failed\n");
641 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600642 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600643 pr_info("Keyboard backlight not supported\n");
644 return LED_OFF;
645 }
646
647 return state ? LED_FULL : LED_OFF;
648}
649
650static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
651 enum led_brightness brightness)
652{
653 struct toshiba_acpi_dev *dev = container_of(cdev,
654 struct toshiba_acpi_dev, kbd_led);
655 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600656
657 /* Set the keyboard backlight state */
658 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600659 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
660 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600661 pr_err("ACPI call to set KBD Illumination mode failed\n");
662 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600663 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600664 pr_info("Keyboard backlight not supported\n");
665 return;
666 }
667}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400668
Azael Avalos9d8658a2014-03-25 20:38:32 -0600669/* TouchPad support */
670static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
671{
672 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600673
674 if (!sci_open(dev))
675 return -EIO;
676
Azael Avalos893f3f62014-09-29 20:40:09 -0600677 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600678 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600679 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600680 pr_err("ACPI call to set the touchpad failed\n");
681 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600682 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600683 return -ENODEV;
684 }
685
686 return 0;
687}
688
689static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
690{
691 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600692
693 if (!sci_open(dev))
694 return -EIO;
695
Azael Avalos893f3f62014-09-29 20:40:09 -0600696 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600697 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600698 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600699 pr_err("ACPI call to query the touchpad failed\n");
700 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600701 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600702 return -ENODEV;
703 }
704
705 return 0;
706}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200707
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600708/* Eco Mode support */
709static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
710{
711 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700712 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600713 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600714
Azael Avalos258c5902014-09-29 20:40:07 -0600715 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700716 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
717 pr_err("ACPI call to get ECO led failed\n");
718 } else if (out[0] == TOS_NOT_INSTALLED) {
719 pr_info("ECO led not installed");
720 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800721 /*
722 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700723 * LED device is present, but that we just screwed the input
724 * parameters.
725 *
726 * Let's query the status of the LED to see if we really have a
727 * success response, indicating the actual presense of the LED,
728 * bail out otherwise.
729 */
730 in[3] = 1;
731 status = tci_raw(dev, in, out);
732 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
733 pr_err("ACPI call to get ECO led failed\n");
734 else if (out[0] == TOS_SUCCESS)
735 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600736 }
737
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700738 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600739}
740
Azael Avalosb5163992015-02-10 23:43:56 -0700741static enum led_brightness
742toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600743{
744 struct toshiba_acpi_dev *dev = container_of(cdev,
745 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600746 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
747 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600748 acpi_status status;
749
Azael Avalos258c5902014-09-29 20:40:07 -0600750 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600751 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600752 pr_err("ACPI call to get ECO led failed\n");
753 return LED_OFF;
754 }
755
756 return out[2] ? LED_FULL : LED_OFF;
757}
758
759static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
760 enum led_brightness brightness)
761{
762 struct toshiba_acpi_dev *dev = container_of(cdev,
763 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600764 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
765 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600766 acpi_status status;
767
768 /* Switch the Eco Mode led on/off */
769 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600770 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600771 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600772 pr_err("ACPI call to set ECO led failed\n");
773 return;
774 }
775}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400776
Azael Avalos5a2813e2014-03-25 20:38:34 -0600777/* Accelerometer support */
778static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
779{
Azael Avalos258c5902014-09-29 20:40:07 -0600780 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
781 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600782 acpi_status status;
783
Darren Harte0769fe2015-02-11 20:50:08 -0800784 /*
785 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600786 * this call also serves as initialization
787 */
Azael Avalos258c5902014-09-29 20:40:07 -0600788 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600789 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600790 pr_err("ACPI call to query the accelerometer failed\n");
791 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600792 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
793 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600794 pr_err("Accelerometer not initialized\n");
795 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600796 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600797 pr_info("Accelerometer not supported\n");
798 return -ENODEV;
799 }
800
801 return 0;
802}
803
804static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
805 u32 *xy, u32 *z)
806{
Azael Avalos258c5902014-09-29 20:40:07 -0600807 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
808 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600809 acpi_status status;
810
811 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600812 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600813 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600814 pr_err("ACPI call to query the accelerometer failed\n");
815 return -EIO;
816 }
817
818 *xy = out[2];
819 *z = out[4];
820
821 return 0;
822}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600823
Azael Avalose26ffe52015-01-18 18:30:22 -0700824/* Sleep (Charge and Music) utilities support */
825static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
826 u32 *mode)
827{
828 u32 result;
829
830 if (!sci_open(dev))
831 return -EIO;
832
833 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
834 sci_close(dev);
835 if (result == TOS_FAILURE) {
836 pr_err("ACPI call to set USB S&C mode failed\n");
837 return -EIO;
838 } else if (result == TOS_NOT_SUPPORTED) {
839 pr_info("USB Sleep and Charge not supported\n");
840 return -ENODEV;
841 } else if (result == TOS_INPUT_DATA_ERROR) {
842 return -EIO;
843 }
844
845 return 0;
846}
847
848static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
849 u32 mode)
850{
851 u32 result;
852
853 if (!sci_open(dev))
854 return -EIO;
855
856 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
857 sci_close(dev);
858 if (result == TOS_FAILURE) {
859 pr_err("ACPI call to set USB S&C mode failed\n");
860 return -EIO;
861 } else if (result == TOS_NOT_SUPPORTED) {
862 pr_info("USB Sleep and Charge not supported\n");
863 return -ENODEV;
864 } else if (result == TOS_INPUT_DATA_ERROR) {
865 return -EIO;
866 }
867
868 return 0;
869}
870
Azael Avalos182bcaa2015-01-18 18:30:23 -0700871static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
872 u32 *mode)
873{
874 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
875 u32 out[TCI_WORDS];
876 acpi_status status;
877
878 if (!sci_open(dev))
879 return -EIO;
880
881 in[5] = SCI_USB_CHARGE_BAT_LVL;
882 status = tci_raw(dev, in, out);
883 sci_close(dev);
884 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
885 pr_err("ACPI call to get USB S&C battery level failed\n");
886 return -EIO;
887 } else if (out[0] == TOS_NOT_SUPPORTED) {
888 pr_info("USB Sleep and Charge not supported\n");
889 return -ENODEV;
890 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
891 return -EIO;
892 }
893
894 *mode = out[2];
895
896 return 0;
897}
898
899static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
900 u32 mode)
901{
902 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
903 u32 out[TCI_WORDS];
904 acpi_status status;
905
906 if (!sci_open(dev))
907 return -EIO;
908
909 in[2] = mode;
910 in[5] = SCI_USB_CHARGE_BAT_LVL;
911 status = tci_raw(dev, in, out);
912 sci_close(dev);
913 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
914 pr_err("ACPI call to set USB S&C battery level failed\n");
915 return -EIO;
916 } else if (out[0] == TOS_NOT_SUPPORTED) {
917 pr_info("USB Sleep and Charge not supported\n");
918 return -ENODEV;
919 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
920 return -EIO;
921 }
922
923 return 0;
924}
925
Azael Avalosbb3fe012015-01-18 18:30:24 -0700926static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
927 u32 *state)
928{
929 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
930 u32 out[TCI_WORDS];
931 acpi_status status;
932
933 if (!sci_open(dev))
934 return -EIO;
935
936 in[5] = SCI_USB_CHARGE_RAPID_DSP;
937 status = tci_raw(dev, in, out);
938 sci_close(dev);
939 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
940 pr_err("ACPI call to get USB S&C battery level failed\n");
941 return -EIO;
942 } else if (out[0] == TOS_NOT_SUPPORTED ||
943 out[0] == TOS_INPUT_DATA_ERROR) {
944 pr_info("USB Sleep and Charge not supported\n");
945 return -ENODEV;
946 }
947
948 *state = out[2];
949
950 return 0;
951}
952
953static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
954 u32 state)
955{
956 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
957 u32 out[TCI_WORDS];
958 acpi_status status;
959
960 if (!sci_open(dev))
961 return -EIO;
962
963 in[2] = state;
964 in[5] = SCI_USB_CHARGE_RAPID_DSP;
965 status = tci_raw(dev, in, out);
966 sci_close(dev);
967 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
968 pr_err("ACPI call to set USB S&C battery level failed\n");
969 return -EIO;
970 } else if (out[0] == TOS_NOT_SUPPORTED) {
971 pr_info("USB Sleep and Charge not supported\n");
972 return -ENODEV;
973 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
974 return -EIO;
975 }
976
977 return 0;
978}
979
Azael Avalos172ce0a2015-01-18 18:30:25 -0700980static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
981{
982 u32 result;
983
984 if (!sci_open(dev))
985 return -EIO;
986
987 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
988 sci_close(dev);
989 if (result == TOS_FAILURE) {
990 pr_err("ACPI call to set USB S&C mode failed\n");
991 return -EIO;
992 } else if (result == TOS_NOT_SUPPORTED) {
993 pr_info("USB Sleep and Charge not supported\n");
994 return -ENODEV;
995 } else if (result == TOS_INPUT_DATA_ERROR) {
996 return -EIO;
997 }
998
999 return 0;
1000}
1001
1002static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1003{
1004 u32 result;
1005
1006 if (!sci_open(dev))
1007 return -EIO;
1008
1009 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1010 sci_close(dev);
1011 if (result == TOS_FAILURE) {
1012 pr_err("ACPI call to set USB S&C mode failed\n");
1013 return -EIO;
1014 } else if (result == TOS_NOT_SUPPORTED) {
1015 pr_info("USB Sleep and Charge not supported\n");
1016 return -ENODEV;
1017 } else if (result == TOS_INPUT_DATA_ERROR) {
1018 return -EIO;
1019 }
1020
1021 return 0;
1022}
1023
Azael Avalosbae84192015-02-10 21:09:18 -07001024/* Keyboard function keys */
1025static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1026{
1027 u32 result;
1028
1029 if (!sci_open(dev))
1030 return -EIO;
1031
1032 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1033 sci_close(dev);
1034 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1035 pr_err("ACPI call to get KBD function keys failed\n");
1036 return -EIO;
1037 } else if (result == TOS_NOT_SUPPORTED) {
1038 pr_info("KBD function keys not supported\n");
1039 return -ENODEV;
1040 }
1041
1042 return 0;
1043}
1044
1045static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1046{
1047 u32 result;
1048
1049 if (!sci_open(dev))
1050 return -EIO;
1051
1052 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1053 sci_close(dev);
1054 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1055 pr_err("ACPI call to set KBD function keys failed\n");
1056 return -EIO;
1057 } else if (result == TOS_NOT_SUPPORTED) {
1058 pr_info("KBD function keys not supported\n");
1059 return -ENODEV;
1060 }
1061
1062 return 0;
1063}
1064
Azael Avalos35d53ce2015-02-10 21:09:19 -07001065/* Panel Power ON */
1066static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1067{
1068 u32 result;
1069
1070 if (!sci_open(dev))
1071 return -EIO;
1072
1073 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1074 sci_close(dev);
1075 if (result == TOS_FAILURE) {
1076 pr_err("ACPI call to get Panel Power ON failed\n");
1077 return -EIO;
1078 } else if (result == TOS_NOT_SUPPORTED) {
1079 pr_info("Panel Power on not supported\n");
1080 return -ENODEV;
1081 } else if (result == TOS_INPUT_DATA_ERROR) {
1082 return -EIO;
1083 }
1084
1085 return 0;
1086}
1087
1088static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1089{
1090 u32 result;
1091
1092 if (!sci_open(dev))
1093 return -EIO;
1094
1095 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1096 sci_close(dev);
1097 if (result == TOS_FAILURE) {
1098 pr_err("ACPI call to set Panel Power ON failed\n");
1099 return -EIO;
1100 } else if (result == TOS_NOT_SUPPORTED) {
1101 pr_info("Panel Power ON not supported\n");
1102 return -ENODEV;
1103 } else if (result == TOS_INPUT_DATA_ERROR) {
1104 return -EIO;
1105 }
1106
1107 return 0;
1108}
1109
Azael Avalos17fe4b32015-02-10 21:09:20 -07001110/* USB Three */
1111static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1112{
1113 u32 result;
1114
1115 if (!sci_open(dev))
1116 return -EIO;
1117
1118 result = sci_read(dev, SCI_USB_THREE, state);
1119 sci_close(dev);
1120 if (result == TOS_FAILURE) {
1121 pr_err("ACPI call to get USB 3 failed\n");
1122 return -EIO;
1123 } else if (result == TOS_NOT_SUPPORTED) {
1124 pr_info("USB 3 not supported\n");
1125 return -ENODEV;
1126 } else if (result == TOS_INPUT_DATA_ERROR) {
1127 return -EIO;
1128 }
1129
1130 return 0;
1131}
1132
1133static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1134{
1135 u32 result;
1136
1137 if (!sci_open(dev))
1138 return -EIO;
1139
1140 result = sci_write(dev, SCI_USB_THREE, state);
1141 sci_close(dev);
1142 if (result == TOS_FAILURE) {
1143 pr_err("ACPI call to set USB 3 failed\n");
1144 return -EIO;
1145 } else if (result == TOS_NOT_SUPPORTED) {
1146 pr_info("USB 3 not supported\n");
1147 return -ENODEV;
1148 } else if (result == TOS_INPUT_DATA_ERROR) {
1149 return -EIO;
1150 }
1151
1152 return 0;
1153}
1154
Azael Avalos56e6b352015-03-20 16:55:16 -06001155/* Hotkey Event type */
1156static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1157 u32 *type)
1158{
1159 u32 val1 = 0x03;
1160 u32 val2 = 0;
1161 u32 result;
1162
1163 result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
1164 if (result == TOS_FAILURE) {
1165 pr_err("ACPI call to get System type failed\n");
1166 return -EIO;
1167 } else if (result == TOS_NOT_SUPPORTED) {
1168 pr_info("System type not supported\n");
1169 return -ENODEV;
1170 }
1171
1172 *type = val2;
1173
1174 return 0;
1175}
1176
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001177/* Bluetooth rfkill handlers */
1178
Seth Forshee135740d2011-09-20 16:55:49 -05001179static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001180{
1181 u32 hci_result;
1182 u32 value, value2;
1183
1184 value = 0;
1185 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -06001186 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001187 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001188 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1189
1190 return hci_result;
1191}
1192
Seth Forshee135740d2011-09-20 16:55:49 -05001193static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001194{
1195 u32 hci_result;
1196 u32 value, value2;
1197
1198 value = 0;
1199 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -06001200 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001201
1202 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1203 return hci_result;
1204}
1205
Johannes Berg19d337d2009-06-02 13:01:37 +02001206static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001207{
Johannes Berg19d337d2009-06-02 13:01:37 +02001208 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001209 u32 result1, result2;
1210 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +02001211 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001212 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001213
Johannes Berg19d337d2009-06-02 13:01:37 +02001214 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001215
1216 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001217 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001218 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001219 goto out;
1220 }
1221
1222 if (!radio_state) {
1223 err = 0;
1224 goto out;
1225 }
1226
Azael Avalos893f3f62014-09-29 20:40:09 -06001227 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1228 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001229
Azael Avalos1864bbc2014-09-29 20:40:08 -06001230 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001231 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001232 else
1233 err = 0;
1234 out:
1235 mutex_unlock(&dev->mutex);
1236 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001237}
1238
Johannes Berg19d337d2009-06-02 13:01:37 +02001239static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001240{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001241 bool new_rfk_state;
1242 bool value;
1243 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +02001244 struct toshiba_acpi_dev *dev = data;
1245
1246 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001247
Seth Forshee135740d2011-09-20 16:55:49 -05001248 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001249 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001250 /* Can't do anything useful */
1251 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001252 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001253 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001254
1255 new_rfk_state = value;
1256
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001257 mutex_unlock(&dev->mutex);
1258
Johannes Berg19d337d2009-06-02 13:01:37 +02001259 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1260 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001261}
1262
Johannes Berg19d337d2009-06-02 13:01:37 +02001263static const struct rfkill_ops toshiba_rfk_ops = {
1264 .set_block = bt_rfkill_set_block,
1265 .poll = bt_rfkill_poll,
1266};
1267
Akio Idehara121b7b02012-04-06 01:46:43 +09001268static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1269{
1270 u32 hci_result;
1271 u32 status;
1272
Azael Avalos893f3f62014-09-29 20:40:09 -06001273 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001274 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001275 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001276}
1277
1278static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1279{
1280 u32 hci_result;
1281 u32 value = !enable;
1282
Azael Avalos893f3f62014-09-29 20:40:09 -06001283 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001284 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001285}
1286
Azael Avalosb5163992015-02-10 23:43:56 -07001287static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Seth Forshee62cce752012-04-19 11:23:50 -05001289static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 u32 hci_result;
1292 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001293 int brightness = 0;
1294
1295 if (dev->tr_backlight_supported) {
1296 bool enabled;
1297 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001298
Akio Idehara121b7b02012-04-06 01:46:43 +09001299 if (ret)
1300 return ret;
1301 if (enabled)
1302 return 0;
1303 brightness++;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Azael Avalos893f3f62014-09-29 20:40:09 -06001306 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001307 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001308 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001309
1310 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001311}
1312
Seth Forshee62cce752012-04-19 11:23:50 -05001313static int get_lcd_brightness(struct backlight_device *bd)
1314{
1315 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001316
Seth Forshee62cce752012-04-19 11:23:50 -05001317 return __get_lcd_brightness(dev);
1318}
1319
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001320static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001321{
Seth Forshee135740d2011-09-20 16:55:49 -05001322 struct toshiba_acpi_dev *dev = m->private;
1323 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001324 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001325
Seth Forshee135740d2011-09-20 16:55:49 -05001326 if (!dev->backlight_dev)
1327 return -ENODEV;
1328
Akio Idehara121b7b02012-04-06 01:46:43 +09001329 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001330 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001331 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001332 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001333 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001337 pr_err("Error reading LCD brightness\n");
1338 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001339}
1340
1341static int lcd_proc_open(struct inode *inode, struct file *file)
1342{
Al Virod9dda782013-03-31 18:16:14 -04001343 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Seth Forshee62cce752012-04-19 11:23:50 -05001346static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001347{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001348 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001349
Akio Idehara121b7b02012-04-06 01:46:43 +09001350 if (dev->tr_backlight_supported) {
1351 bool enable = !value;
1352 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001353
Akio Idehara121b7b02012-04-06 01:46:43 +09001354 if (ret)
1355 return ret;
1356 if (value)
1357 value--;
1358 }
1359
Azael Avalosa39f46d2014-11-24 19:29:36 -07001360 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1361 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1362 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001363}
1364
1365static int set_lcd_status(struct backlight_device *bd)
1366{
Seth Forshee135740d2011-09-20 16:55:49 -05001367 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001368
Seth Forshee62cce752012-04-19 11:23:50 -05001369 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001370}
1371
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001372static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1373 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Al Virod9dda782013-03-31 18:16:14 -04001375 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001376 char cmd[42];
1377 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001379 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001380 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001382 len = min(count, sizeof(cmd) - 1);
1383 if (copy_from_user(cmd, buf, len))
1384 return -EFAULT;
1385 cmd[len] = '\0';
1386
1387 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001388 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001389 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001390 if (ret == 0)
1391 ret = count;
1392 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001393 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001394 }
Holger Machtc9263552006-10-20 14:30:29 -07001395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001398static const struct file_operations lcd_proc_fops = {
1399 .owner = THIS_MODULE,
1400 .open = lcd_proc_open,
1401 .read = seq_read,
1402 .llseek = seq_lseek,
1403 .release = single_release,
1404 .write = lcd_proc_write,
1405};
1406
Seth Forshee36d03f92011-09-20 16:55:53 -05001407static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1408{
1409 u32 hci_result;
1410
Azael Avalos893f3f62014-09-29 20:40:09 -06001411 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001412 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001413}
1414
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001415static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Seth Forshee135740d2011-09-20 16:55:49 -05001417 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001419 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Seth Forshee36d03f92011-09-20 16:55:53 -05001421 ret = get_video_status(dev, &value);
1422 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1424 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001425 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001426
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001427 seq_printf(m, "lcd_out: %d\n", is_lcd);
1428 seq_printf(m, "crt_out: %d\n", is_crt);
1429 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Seth Forshee36d03f92011-09-20 16:55:53 -05001432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001435static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Al Virod9dda782013-03-31 18:16:14 -04001437 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001438}
1439
1440static ssize_t video_proc_write(struct file *file, const char __user *buf,
1441 size_t count, loff_t *pos)
1442{
Al Virod9dda782013-03-31 18:16:14 -04001443 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001444 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001445 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int value;
1447 int remain = count;
1448 int lcd_out = -1;
1449 int crt_out = -1;
1450 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001451 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001453 cmd = kmalloc(count + 1, GFP_KERNEL);
1454 if (!cmd)
1455 return -ENOMEM;
1456 if (copy_from_user(cmd, buf, count)) {
1457 kfree(cmd);
1458 return -EFAULT;
1459 }
1460 cmd[count] = '\0';
1461
1462 buffer = cmd;
1463
Darren Harte0769fe2015-02-11 20:50:08 -08001464 /*
1465 * Scan expression. Multiple expressions may be delimited with ;
1466 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
1468 while (remain) {
1469 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1470 lcd_out = value & 1;
1471 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1472 crt_out = value & 1;
1473 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1474 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001475 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 do {
1477 ++buffer;
1478 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001479 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001482 kfree(cmd);
1483
Seth Forshee36d03f92011-09-20 16:55:53 -05001484 ret = get_video_status(dev, &video_out);
1485 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001486 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (lcd_out != -1)
1489 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1490 if (crt_out != -1)
1491 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1492 if (tv_out != -1)
1493 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001494 /*
1495 * To avoid unnecessary video disruption, only write the new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * video setting if something changed. */
1497 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001498 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001501 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001504static const struct file_operations video_proc_fops = {
1505 .owner = THIS_MODULE,
1506 .open = video_proc_open,
1507 .read = seq_read,
1508 .llseek = seq_lseek,
1509 .release = single_release,
1510 .write = video_proc_write,
1511};
1512
Seth Forshee36d03f92011-09-20 16:55:53 -05001513static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1514{
1515 u32 hci_result;
1516
Azael Avalos893f3f62014-09-29 20:40:09 -06001517 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001518 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001519}
1520
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001521static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Seth Forshee135740d2011-09-20 16:55:49 -05001523 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001524 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 u32 value;
1526
Seth Forshee36d03f92011-09-20 16:55:53 -05001527 ret = get_fan_status(dev, &value);
1528 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001529 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001530 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Seth Forshee36d03f92011-09-20 16:55:53 -05001533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001536static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Al Virod9dda782013-03-31 18:16:14 -04001538 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001539}
1540
1541static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1542 size_t count, loff_t *pos)
1543{
Al Virod9dda782013-03-31 18:16:14 -04001544 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001545 char cmd[42];
1546 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int value;
1548 u32 hci_result;
1549
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001550 len = min(count, sizeof(cmd) - 1);
1551 if (copy_from_user(cmd, buf, len))
1552 return -EFAULT;
1553 cmd[len] = '\0';
1554
1555 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001556 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001557 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001558 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001559 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001560 else
1561 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 } else {
1563 return -EINVAL;
1564 }
1565
1566 return count;
1567}
1568
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001569static const struct file_operations fan_proc_fops = {
1570 .owner = THIS_MODULE,
1571 .open = fan_proc_open,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = single_release,
1575 .write = fan_proc_write,
1576};
1577
1578static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Seth Forshee135740d2011-09-20 16:55:49 -05001580 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 u32 hci_result;
1582 u32 value;
1583
Seth Forshee11948b92011-11-16 17:37:45 -06001584 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001585 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001586 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001587 dev->key_event_valid = 1;
1588 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001589 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001590 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001591 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001592 /*
1593 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001595 * become disabled.
1596 */
Azael Avalos893f3f62014-09-29 20:40:09 -06001597 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001598 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001600 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001601 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 }
1604
Seth Forshee135740d2011-09-20 16:55:49 -05001605 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1606 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001610static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Al Virod9dda782013-03-31 18:16:14 -04001612 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001613}
1614
1615static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1616 size_t count, loff_t *pos)
1617{
Al Virod9dda782013-03-31 18:16:14 -04001618 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001619 char cmd[42];
1620 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int value;
1622
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001623 len = min(count, sizeof(cmd) - 1);
1624 if (copy_from_user(cmd, buf, len))
1625 return -EFAULT;
1626 cmd[len] = '\0';
1627
Azael Avalosb5163992015-02-10 23:43:56 -07001628 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001629 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001630 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 return count;
1634}
1635
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001636static const struct file_operations keys_proc_fops = {
1637 .owner = THIS_MODULE,
1638 .open = keys_proc_open,
1639 .read = seq_read,
1640 .llseek = seq_lseek,
1641 .release = single_release,
1642 .write = keys_proc_write,
1643};
1644
1645static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001647 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1648 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001652static int version_proc_open(struct inode *inode, struct file *file)
1653{
Al Virod9dda782013-03-31 18:16:14 -04001654 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001655}
1656
1657static const struct file_operations version_proc_fops = {
1658 .owner = THIS_MODULE,
1659 .open = version_proc_open,
1660 .read = seq_read,
1661 .llseek = seq_lseek,
1662 .release = single_release,
1663};
1664
Darren Harte0769fe2015-02-11 20:50:08 -08001665/*
1666 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668
1669#define PROC_TOSHIBA "toshiba"
1670
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001671static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Seth Forshee36d03f92011-09-20 16:55:53 -05001673 if (dev->backlight_dev)
1674 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1675 &lcd_proc_fops, dev);
1676 if (dev->video_supported)
1677 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1678 &video_proc_fops, dev);
1679 if (dev->fan_supported)
1680 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1681 &fan_proc_fops, dev);
1682 if (dev->hotkey_dev)
1683 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1684 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001685 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1686 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Seth Forshee36d03f92011-09-20 16:55:53 -05001689static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Seth Forshee36d03f92011-09-20 16:55:53 -05001691 if (dev->backlight_dev)
1692 remove_proc_entry("lcd", toshiba_proc_dir);
1693 if (dev->video_supported)
1694 remove_proc_entry("video", toshiba_proc_dir);
1695 if (dev->fan_supported)
1696 remove_proc_entry("fan", toshiba_proc_dir);
1697 if (dev->hotkey_dev)
1698 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001699 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Lionel Debrouxacc24722010-11-16 14:14:02 +01001702static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001703 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001704 .get_brightness = get_lcd_brightness,
1705 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001706};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001707
Azael Avalos360f0f32014-03-25 20:38:31 -06001708/*
1709 * Sysfs files
1710 */
Azael Avalos9d309842015-02-10 23:43:59 -07001711static ssize_t version_show(struct device *dev,
1712 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001713{
1714 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1715}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001716static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001717
Azael Avalos9d309842015-02-10 23:43:59 -07001718static ssize_t fan_store(struct device *dev,
1719 struct device_attribute *attr,
1720 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001721{
1722 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1723 u32 result;
1724 int state;
1725 int ret;
1726
1727 ret = kstrtoint(buf, 0, &state);
1728 if (ret)
1729 return ret;
1730
1731 if (state != 0 && state != 1)
1732 return -EINVAL;
1733
1734 result = hci_write1(toshiba, HCI_FAN, state);
1735 if (result == TOS_FAILURE)
1736 return -EIO;
1737 else if (result == TOS_NOT_SUPPORTED)
1738 return -ENODEV;
1739
1740 return count;
1741}
1742
Azael Avalos9d309842015-02-10 23:43:59 -07001743static ssize_t fan_show(struct device *dev,
1744 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001745{
1746 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1747 u32 value;
1748 int ret;
1749
1750 ret = get_fan_status(toshiba, &value);
1751 if (ret)
1752 return ret;
1753
1754 return sprintf(buf, "%d\n", value);
1755}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001756static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001757
Azael Avalos9d309842015-02-10 23:43:59 -07001758static ssize_t kbd_backlight_mode_store(struct device *dev,
1759 struct device_attribute *attr,
1760 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001761{
1762 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001763 int mode;
1764 int time;
1765 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001766
Dan Carpenteraeaac092014-09-03 14:44:37 +03001767
1768 ret = kstrtoint(buf, 0, &mode);
1769 if (ret)
1770 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001771
1772 /* Check for supported modes depending on keyboard backlight type */
1773 if (toshiba->kbd_type == 1) {
1774 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1775 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1776 return -EINVAL;
1777 } else if (toshiba->kbd_type == 2) {
1778 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1779 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1780 mode != SCI_KBD_MODE_OFF)
1781 return -EINVAL;
1782 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001783
Darren Harte0769fe2015-02-11 20:50:08 -08001784 /*
1785 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001786 * Auto - KBD backlight turns off automatically in given time
1787 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001788 * ON - KBD backlight is always on
1789 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001790 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001791
1792 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001793 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001794 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001795 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001796
1797 /* OR the "base time" to the actual method format */
1798 if (toshiba->kbd_type == 1) {
1799 /* Type 1 requires the current mode */
1800 time |= toshiba->kbd_mode;
1801 } else if (toshiba->kbd_type == 2) {
1802 /* Type 2 requires the desired mode */
1803 time |= mode;
1804 }
1805
Dan Carpenteraeaac092014-09-03 14:44:37 +03001806 ret = toshiba_kbd_illum_status_set(toshiba, time);
1807 if (ret)
1808 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001809
Azael Avalos360f0f32014-03-25 20:38:31 -06001810 toshiba->kbd_mode = mode;
1811 }
1812
1813 return count;
1814}
1815
Azael Avalos9d309842015-02-10 23:43:59 -07001816static ssize_t kbd_backlight_mode_show(struct device *dev,
1817 struct device_attribute *attr,
1818 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001819{
1820 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1821 u32 time;
1822
1823 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1824 return -EIO;
1825
Azael Avalos93f8c162014-09-12 18:50:36 -06001826 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1827}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001828static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001829
Azael Avalos9d309842015-02-10 23:43:59 -07001830static ssize_t kbd_type_show(struct device *dev,
1831 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001832{
1833 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1834
1835 return sprintf(buf, "%d\n", toshiba->kbd_type);
1836}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001837static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001838
Azael Avalos9d309842015-02-10 23:43:59 -07001839static ssize_t available_kbd_modes_show(struct device *dev,
1840 struct device_attribute *attr,
1841 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001842{
1843 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1844
1845 if (toshiba->kbd_type == 1)
1846 return sprintf(buf, "%x %x\n",
1847 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1848
1849 return sprintf(buf, "%x %x %x\n",
1850 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001851}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001852static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001853
Azael Avalos9d309842015-02-10 23:43:59 -07001854static ssize_t kbd_backlight_timeout_store(struct device *dev,
1855 struct device_attribute *attr,
1856 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001857{
1858 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001859 int time;
1860 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001861
Azael Avaloseabde0f2014-10-04 12:02:21 -06001862 ret = kstrtoint(buf, 0, &time);
1863 if (ret)
1864 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001865
Azael Avaloseabde0f2014-10-04 12:02:21 -06001866 /* Check for supported values depending on kbd_type */
1867 if (toshiba->kbd_type == 1) {
1868 if (time < 0 || time > 60)
1869 return -EINVAL;
1870 } else if (toshiba->kbd_type == 2) {
1871 if (time < 1 || time > 60)
1872 return -EINVAL;
1873 }
1874
1875 /* Set the Keyboard Backlight Timeout */
1876
1877 /* Only make a change if the actual timeout has changed */
1878 if (toshiba->kbd_time != time) {
1879 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001880 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001881 /* OR the "base time" to the actual method format */
1882 if (toshiba->kbd_type == 1)
1883 time |= SCI_KBD_MODE_FNZ;
1884 else if (toshiba->kbd_type == 2)
1885 time |= SCI_KBD_MODE_AUTO;
1886
1887 ret = toshiba_kbd_illum_status_set(toshiba, time);
1888 if (ret)
1889 return ret;
1890
Azael Avalos360f0f32014-03-25 20:38:31 -06001891 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1892 }
1893
1894 return count;
1895}
1896
Azael Avalos9d309842015-02-10 23:43:59 -07001897static ssize_t kbd_backlight_timeout_show(struct device *dev,
1898 struct device_attribute *attr,
1899 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001900{
1901 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1902 u32 time;
1903
1904 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1905 return -EIO;
1906
1907 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1908}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001909static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001910
Azael Avalos9d309842015-02-10 23:43:59 -07001911static ssize_t touchpad_store(struct device *dev,
1912 struct device_attribute *attr,
1913 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001914{
1915 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1916 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001917 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001918
1919 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001920 ret = kstrtoint(buf, 0, &state);
1921 if (ret)
1922 return ret;
1923 if (state != 0 && state != 1)
1924 return -EINVAL;
1925
1926 ret = toshiba_touchpad_set(toshiba, state);
1927 if (ret)
1928 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001929
1930 return count;
1931}
1932
Azael Avalos9d309842015-02-10 23:43:59 -07001933static ssize_t touchpad_show(struct device *dev,
1934 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001935{
1936 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1937 u32 state;
1938 int ret;
1939
1940 ret = toshiba_touchpad_get(toshiba, &state);
1941 if (ret < 0)
1942 return ret;
1943
1944 return sprintf(buf, "%i\n", state);
1945}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001946static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001947
Azael Avalos9d309842015-02-10 23:43:59 -07001948static ssize_t position_show(struct device *dev,
1949 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001950{
1951 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1952 u32 xyval, zval, tmp;
1953 u16 x, y, z;
1954 int ret;
1955
1956 xyval = zval = 0;
1957 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1958 if (ret < 0)
1959 return ret;
1960
1961 x = xyval & HCI_ACCEL_MASK;
1962 tmp = xyval >> HCI_MISC_SHIFT;
1963 y = tmp & HCI_ACCEL_MASK;
1964 z = zval & HCI_ACCEL_MASK;
1965
1966 return sprintf(buf, "%d %d %d\n", x, y, z);
1967}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001968static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001969
Azael Avalos9d309842015-02-10 23:43:59 -07001970static ssize_t usb_sleep_charge_show(struct device *dev,
1971 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001972{
1973 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1974 u32 mode;
1975 int ret;
1976
1977 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1978 if (ret < 0)
1979 return ret;
1980
1981 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1982}
1983
Azael Avalos9d309842015-02-10 23:43:59 -07001984static ssize_t usb_sleep_charge_store(struct device *dev,
1985 struct device_attribute *attr,
1986 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001987{
1988 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1989 u32 mode;
1990 int state;
1991 int ret;
1992
1993 ret = kstrtoint(buf, 0, &state);
1994 if (ret)
1995 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001996 /*
1997 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001998 * 0 - Disabled
1999 * 1 - Alternate (Non USB conformant devices that require more power)
2000 * 2 - Auto (USB conformant devices)
2001 */
2002 if (state != 0 && state != 1 && state != 2)
2003 return -EINVAL;
2004
2005 /* Set the USB charging mode to internal value */
2006 if (state == 0)
2007 mode = SCI_USB_CHARGE_DISABLED;
2008 else if (state == 1)
2009 mode = SCI_USB_CHARGE_ALTERNATE;
2010 else if (state == 2)
2011 mode = SCI_USB_CHARGE_AUTO;
2012
2013 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2014 if (ret)
2015 return ret;
2016
2017 return count;
2018}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002019static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07002020
Azael Avalos182bcaa2015-01-18 18:30:23 -07002021static ssize_t sleep_functions_on_battery_show(struct device *dev,
2022 struct device_attribute *attr,
2023 char *buf)
2024{
2025 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2026 u32 state;
2027 int bat_lvl;
2028 int status;
2029 int ret;
2030 int tmp;
2031
2032 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2033 if (ret < 0)
2034 return ret;
2035
2036 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2037 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2038 status = (tmp == 0x4) ? 1 : 0;
2039 /* Determine the battery level set */
2040 bat_lvl = state >> HCI_MISC_SHIFT;
2041
2042 return sprintf(buf, "%d %d\n", status, bat_lvl);
2043}
2044
2045static ssize_t sleep_functions_on_battery_store(struct device *dev,
2046 struct device_attribute *attr,
2047 const char *buf, size_t count)
2048{
2049 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2050 u32 status;
2051 int value;
2052 int ret;
2053 int tmp;
2054
2055 ret = kstrtoint(buf, 0, &value);
2056 if (ret)
2057 return ret;
2058
Darren Harte0769fe2015-02-11 20:50:08 -08002059 /*
2060 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002061 * 0 - Disabled
2062 * 1-100 - Enabled
2063 */
2064 if (value < 0 || value > 100)
2065 return -EINVAL;
2066
2067 if (value == 0) {
2068 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2069 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2070 } else {
2071 tmp = value << HCI_MISC_SHIFT;
2072 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2073 }
2074 ret = toshiba_sleep_functions_status_set(toshiba, status);
2075 if (ret < 0)
2076 return ret;
2077
2078 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2079
2080 return count;
2081}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002082static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002083
Azael Avalos9d309842015-02-10 23:43:59 -07002084static ssize_t usb_rapid_charge_show(struct device *dev,
2085 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002086{
2087 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2088 u32 state;
2089 int ret;
2090
2091 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2092 if (ret < 0)
2093 return ret;
2094
2095 return sprintf(buf, "%d\n", state);
2096}
2097
Azael Avalos9d309842015-02-10 23:43:59 -07002098static ssize_t usb_rapid_charge_store(struct device *dev,
2099 struct device_attribute *attr,
2100 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002101{
2102 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2103 int state;
2104 int ret;
2105
2106 ret = kstrtoint(buf, 0, &state);
2107 if (ret)
2108 return ret;
2109 if (state != 0 && state != 1)
2110 return -EINVAL;
2111
2112 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2113 if (ret)
2114 return ret;
2115
2116 return count;
2117}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002118static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002119
Azael Avalos9d309842015-02-10 23:43:59 -07002120static ssize_t usb_sleep_music_show(struct device *dev,
2121 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002122{
2123 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2124 u32 state;
2125 int ret;
2126
2127 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2128 if (ret < 0)
2129 return ret;
2130
2131 return sprintf(buf, "%d\n", state);
2132}
2133
Azael Avalos9d309842015-02-10 23:43:59 -07002134static ssize_t usb_sleep_music_store(struct device *dev,
2135 struct device_attribute *attr,
2136 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002137{
2138 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2139 int state;
2140 int ret;
2141
2142 ret = kstrtoint(buf, 0, &state);
2143 if (ret)
2144 return ret;
2145 if (state != 0 && state != 1)
2146 return -EINVAL;
2147
2148 ret = toshiba_usb_sleep_music_set(toshiba, state);
2149 if (ret)
2150 return ret;
2151
2152 return count;
2153}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002154static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002155
Azael Avalos9d309842015-02-10 23:43:59 -07002156static ssize_t kbd_function_keys_show(struct device *dev,
2157 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002158{
2159 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2160 int mode;
2161 int ret;
2162
2163 ret = toshiba_function_keys_get(toshiba, &mode);
2164 if (ret < 0)
2165 return ret;
2166
2167 return sprintf(buf, "%d\n", mode);
2168}
2169
Azael Avalos9d309842015-02-10 23:43:59 -07002170static ssize_t kbd_function_keys_store(struct device *dev,
2171 struct device_attribute *attr,
2172 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002173{
2174 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2175 int mode;
2176 int ret;
2177
2178 ret = kstrtoint(buf, 0, &mode);
2179 if (ret)
2180 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002181 /*
2182 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002183 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2184 * 1 - Special functions (Opposite of the above setting)
2185 */
2186 if (mode != 0 && mode != 1)
2187 return -EINVAL;
2188
2189 ret = toshiba_function_keys_set(toshiba, mode);
2190 if (ret)
2191 return ret;
2192
2193 pr_info("Reboot for changes to KBD Function Keys to take effect");
2194
2195 return count;
2196}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002197static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002198
Azael Avalos9d309842015-02-10 23:43:59 -07002199static ssize_t panel_power_on_show(struct device *dev,
2200 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002201{
2202 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2203 u32 state;
2204 int ret;
2205
2206 ret = toshiba_panel_power_on_get(toshiba, &state);
2207 if (ret < 0)
2208 return ret;
2209
2210 return sprintf(buf, "%d\n", state);
2211}
2212
Azael Avalos9d309842015-02-10 23:43:59 -07002213static ssize_t panel_power_on_store(struct device *dev,
2214 struct device_attribute *attr,
2215 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002216{
2217 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2218 int state;
2219 int ret;
2220
2221 ret = kstrtoint(buf, 0, &state);
2222 if (ret)
2223 return ret;
2224 if (state != 0 && state != 1)
2225 return -EINVAL;
2226
2227 ret = toshiba_panel_power_on_set(toshiba, state);
2228 if (ret)
2229 return ret;
2230
2231 pr_info("Reboot for changes to Panel Power ON to take effect");
2232
2233 return count;
2234}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002235static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002236
Azael Avalos9d309842015-02-10 23:43:59 -07002237static ssize_t usb_three_show(struct device *dev,
2238 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002239{
2240 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2241 u32 state;
2242 int ret;
2243
2244 ret = toshiba_usb_three_get(toshiba, &state);
2245 if (ret < 0)
2246 return ret;
2247
2248 return sprintf(buf, "%d\n", state);
2249}
2250
Azael Avalos9d309842015-02-10 23:43:59 -07002251static ssize_t usb_three_store(struct device *dev,
2252 struct device_attribute *attr,
2253 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002254{
2255 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2256 int state;
2257 int ret;
2258
2259 ret = kstrtoint(buf, 0, &state);
2260 if (ret)
2261 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002262 /*
2263 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002264 * 0 - Disabled (Acts like a USB 2 port, saving power)
2265 * 1 - Enabled
2266 */
2267 if (state != 0 && state != 1)
2268 return -EINVAL;
2269
2270 ret = toshiba_usb_three_set(toshiba, state);
2271 if (ret)
2272 return ret;
2273
2274 pr_info("Reboot for changes to USB 3 to take effect");
2275
2276 return count;
2277}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002278static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002279
2280static struct attribute *toshiba_attributes[] = {
2281 &dev_attr_version.attr,
2282 &dev_attr_fan.attr,
2283 &dev_attr_kbd_backlight_mode.attr,
2284 &dev_attr_kbd_type.attr,
2285 &dev_attr_available_kbd_modes.attr,
2286 &dev_attr_kbd_backlight_timeout.attr,
2287 &dev_attr_touchpad.attr,
2288 &dev_attr_position.attr,
2289 &dev_attr_usb_sleep_charge.attr,
2290 &dev_attr_sleep_functions_on_battery.attr,
2291 &dev_attr_usb_rapid_charge.attr,
2292 &dev_attr_usb_sleep_music.attr,
2293 &dev_attr_kbd_function_keys.attr,
2294 &dev_attr_panel_power_on.attr,
2295 &dev_attr_usb_three.attr,
2296 NULL,
2297};
2298
Azael Avalos360f0f32014-03-25 20:38:31 -06002299static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2300 struct attribute *attr, int idx)
2301{
2302 struct device *dev = container_of(kobj, struct device, kobj);
2303 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2304 bool exists = true;
2305
Azael Avalos94477d42015-02-10 21:09:17 -07002306 if (attr == &dev_attr_fan.attr)
2307 exists = (drv->fan_supported) ? true : false;
2308 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002309 exists = (drv->kbd_illum_supported) ? true : false;
2310 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2311 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002312 else if (attr == &dev_attr_touchpad.attr)
2313 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002314 else if (attr == &dev_attr_position.attr)
2315 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002316 else if (attr == &dev_attr_usb_sleep_charge.attr)
2317 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002318 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2319 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002320 else if (attr == &dev_attr_usb_rapid_charge.attr)
2321 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002322 else if (attr == &dev_attr_usb_sleep_music.attr)
2323 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002324 else if (attr == &dev_attr_kbd_function_keys.attr)
2325 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002326 else if (attr == &dev_attr_panel_power_on.attr)
2327 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002328 else if (attr == &dev_attr_usb_three.attr)
2329 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002330
2331 return exists ? attr->mode : 0;
2332}
2333
Azael Avalos9bd12132015-02-10 23:43:58 -07002334static struct attribute_group toshiba_attr_group = {
2335 .is_visible = toshiba_sysfs_is_visible,
2336 .attrs = toshiba_attributes,
2337};
2338
Azael Avalos1f28f292014-12-04 20:22:45 -07002339/*
2340 * Hotkeys
2341 */
2342static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2343{
2344 acpi_status status;
2345 u32 result;
2346
2347 status = acpi_evaluate_object(dev->acpi_dev->handle,
2348 "ENAB", NULL, NULL);
2349 if (ACPI_FAILURE(status))
2350 return -ENODEV;
2351
2352 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2353 if (result == TOS_FAILURE)
2354 return -EIO;
2355 else if (result == TOS_NOT_SUPPORTED)
2356 return -ENODEV;
2357
2358 return 0;
2359}
2360
Seth Forshee29cd2932012-01-18 13:44:09 -06002361static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2362 struct serio *port)
2363{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002364 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002365 return false;
2366
2367 if (unlikely(data == 0xe0))
2368 return false;
2369
2370 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2371 schedule_work(&toshiba_acpi->hotkey_work);
2372 return true;
2373 }
2374
2375 return false;
2376}
2377
2378static void toshiba_acpi_hotkey_work(struct work_struct *work)
2379{
2380 acpi_handle ec_handle = ec_get_handle();
2381 acpi_status status;
2382
2383 if (!ec_handle)
2384 return;
2385
2386 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2387 if (ACPI_FAILURE(status))
2388 pr_err("ACPI NTFY method execution failed\n");
2389}
2390
2391/*
2392 * Returns hotkey scancode, or < 0 on failure.
2393 */
2394static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2395{
Zhang Rui74facaf2013-09-03 08:32:15 +08002396 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002397 acpi_status status;
2398
Zhang Rui74facaf2013-09-03 08:32:15 +08002399 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2400 NULL, &value);
2401 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002402 pr_err("ACPI INFO method execution failed\n");
2403 return -EIO;
2404 }
2405
Zhang Rui74facaf2013-09-03 08:32:15 +08002406 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002407}
2408
2409static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2410 int scancode)
2411{
2412 if (scancode == 0x100)
2413 return;
2414
Darren Harte0769fe2015-02-11 20:50:08 -08002415 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002416 if (scancode & 0x80)
2417 return;
2418
2419 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2420 pr_info("Unknown key %x\n", scancode);
2421}
2422
Azael Avalos71454d72014-12-04 20:22:46 -07002423static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2424{
2425 u32 hci_result, value;
2426 int retries = 3;
2427 int scancode;
2428
2429 if (dev->info_supported) {
2430 scancode = toshiba_acpi_query_hotkey(dev);
2431 if (scancode < 0)
2432 pr_err("Failed to query hotkey event\n");
2433 else if (scancode != 0)
2434 toshiba_acpi_report_hotkey(dev, scancode);
2435 } else if (dev->system_event_supported) {
2436 do {
2437 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2438 switch (hci_result) {
2439 case TOS_SUCCESS:
2440 toshiba_acpi_report_hotkey(dev, (int)value);
2441 break;
2442 case TOS_NOT_SUPPORTED:
2443 /*
2444 * This is a workaround for an unresolved
2445 * issue on some machines where system events
2446 * sporadically become disabled.
2447 */
2448 hci_result =
2449 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2450 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002451 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002452 default:
2453 retries--;
2454 break;
2455 }
2456 } while (retries && hci_result != TOS_FIFO_EMPTY);
2457 }
2458}
2459
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002460static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002461{
Zhang Ruie2e19602013-09-03 08:32:06 +08002462 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05002463 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002464 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002465 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05002466
Seth Forshee135740d2011-09-20 16:55:49 -05002467 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002468 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002469 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002470
2471 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002472 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002473 dev->hotkey_dev->id.bustype = BUS_HOST;
2474
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002475 if (dmi_check_system(toshiba_alt_keymap_dmi))
2476 keymap = toshiba_acpi_alt_keymap;
2477 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002478 if (error)
2479 goto err_free_dev;
2480
Seth Forshee29cd2932012-01-18 13:44:09 -06002481 /*
2482 * For some machines the SCI responsible for providing hotkey
2483 * notification doesn't fire. We can trigger the notification
2484 * whenever the Fn key is pressed using the NTFY method, if
2485 * supported, so if it's present set up an i8042 key filter
2486 * for this purpose.
2487 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002488 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002489 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002490 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2491
2492 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2493 if (error) {
2494 pr_err("Error installing key filter\n");
2495 goto err_free_keymap;
2496 }
2497
2498 dev->ntfy_supported = 1;
2499 }
2500
2501 /*
2502 * Determine hotkey query interface. Prefer using the INFO
2503 * method when it is available.
2504 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002505 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002506 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002507 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002508 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002509 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002510 dev->system_event_supported = 1;
2511 }
2512
2513 if (!dev->info_supported && !dev->system_event_supported) {
2514 pr_warn("No hotkey query interface found\n");
2515 goto err_remove_filter;
2516 }
2517
Azael Avalos1f28f292014-12-04 20:22:45 -07002518 error = toshiba_acpi_enable_hotkeys(dev);
2519 if (error) {
Seth Forshee135740d2011-09-20 16:55:49 -05002520 pr_info("Unable to enable hotkeys\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002521 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002522 }
2523
2524 error = input_register_device(dev->hotkey_dev);
2525 if (error) {
2526 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002527 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002528 }
2529
2530 return 0;
2531
Seth Forshee29cd2932012-01-18 13:44:09 -06002532 err_remove_filter:
2533 if (dev->ntfy_supported)
2534 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002535 err_free_keymap:
2536 sparse_keymap_free(dev->hotkey_dev);
2537 err_free_dev:
2538 input_free_device(dev->hotkey_dev);
2539 dev->hotkey_dev = NULL;
2540 return error;
2541}
2542
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002543static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002544{
2545 struct backlight_properties props;
2546 int brightness;
2547 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002548 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002549
2550 /*
2551 * Some machines don't support the backlight methods at all, and
2552 * others support it read-only. Either of these is pretty useless,
2553 * so only register the backlight device if the backlight method
2554 * supports both reads and writes.
2555 */
2556 brightness = __get_lcd_brightness(dev);
2557 if (brightness < 0)
2558 return 0;
2559 ret = set_lcd_brightness(dev, brightness);
2560 if (ret) {
2561 pr_debug("Backlight method is read-only, disabling backlight support\n");
2562 return 0;
2563 }
2564
Akio Idehara121b7b02012-04-06 01:46:43 +09002565 /* Determine whether or not BIOS supports transflective backlight */
2566 ret = get_tr_backlight_status(dev, &enabled);
2567 dev->tr_backlight_supported = !ret;
2568
Matthew Garrett53039f22012-06-01 11:02:36 -04002569 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002570 props.type = BACKLIGHT_PLATFORM;
2571 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002572
Darren Harte0769fe2015-02-11 20:50:08 -08002573 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002574 if (dev->tr_backlight_supported)
2575 props.max_brightness++;
2576
Seth Forshee62cce752012-04-19 11:23:50 -05002577 dev->backlight_dev = backlight_device_register("toshiba",
2578 &dev->acpi_dev->dev,
2579 dev,
2580 &toshiba_backlight_data,
2581 &props);
2582 if (IS_ERR(dev->backlight_dev)) {
2583 ret = PTR_ERR(dev->backlight_dev);
2584 pr_err("Could not register toshiba backlight device\n");
2585 dev->backlight_dev = NULL;
2586 return ret;
2587 }
2588
2589 dev->backlight_dev->props.brightness = brightness;
2590 return 0;
2591}
2592
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002593static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002594{
2595 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2596
Seth Forshee36d03f92011-09-20 16:55:53 -05002597 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002598
Azael Avalos360f0f32014-03-25 20:38:31 -06002599 if (dev->sysfs_created)
2600 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2601 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002602
Seth Forshee29cd2932012-01-18 13:44:09 -06002603 if (dev->ntfy_supported) {
2604 i8042_remove_filter(toshiba_acpi_i8042_filter);
2605 cancel_work_sync(&dev->hotkey_work);
2606 }
2607
Seth Forshee135740d2011-09-20 16:55:49 -05002608 if (dev->hotkey_dev) {
2609 input_unregister_device(dev->hotkey_dev);
2610 sparse_keymap_free(dev->hotkey_dev);
2611 }
2612
2613 if (dev->bt_rfk) {
2614 rfkill_unregister(dev->bt_rfk);
2615 rfkill_destroy(dev->bt_rfk);
2616 }
2617
Markus Elfring00981812014-11-24 20:30:29 +01002618 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002619
Seth Forshee36d03f92011-09-20 16:55:53 -05002620 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002621 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002622
Azael Avalos360f0f32014-03-25 20:38:31 -06002623 if (dev->kbd_led_registered)
2624 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002625
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002626 if (dev->eco_supported)
2627 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002628
Seth Forshee29cd2932012-01-18 13:44:09 -06002629 if (toshiba_acpi)
2630 toshiba_acpi = NULL;
2631
Seth Forshee135740d2011-09-20 16:55:49 -05002632 kfree(dev);
2633
2634 return 0;
2635}
2636
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002637static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002638{
Zhang Ruie2e19602013-09-03 08:32:06 +08002639 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002640 return "GHCI";
2641
Zhang Ruie2e19602013-09-03 08:32:06 +08002642 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002643 return "SPFC";
2644
2645 return NULL;
2646}
2647
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002648static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002649{
2650 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002651 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002652 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002653 bool bt_present;
2654 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002655
Seth Forshee29cd2932012-01-18 13:44:09 -06002656 if (toshiba_acpi)
2657 return -EBUSY;
2658
Seth Forshee135740d2011-09-20 16:55:49 -05002659 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2660 TOSHIBA_ACPI_VERSION);
2661
Seth Forsheea540d6b2011-09-20 16:55:52 -05002662 hci_method = find_hci_method(acpi_dev->handle);
2663 if (!hci_method) {
2664 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002665 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002666 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002667
Seth Forshee135740d2011-09-20 16:55:49 -05002668 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2669 if (!dev)
2670 return -ENOMEM;
2671 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002672 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002673 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002674 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002675
Seth Forshee6e02cc72011-09-20 16:55:51 -05002676 if (toshiba_acpi_setup_keyboard(dev))
2677 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002678
2679 mutex_init(&dev->mutex);
2680
Seth Forshee62cce752012-04-19 11:23:50 -05002681 ret = toshiba_acpi_setup_backlight(dev);
2682 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002683 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002684
2685 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002686 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002687 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2688 &acpi_dev->dev,
2689 RFKILL_TYPE_BLUETOOTH,
2690 &toshiba_rfk_ops,
2691 dev);
2692 if (!dev->bt_rfk) {
2693 pr_err("unable to allocate rfkill device\n");
2694 ret = -ENOMEM;
2695 goto error;
2696 }
2697
2698 ret = rfkill_register(dev->bt_rfk);
2699 if (ret) {
2700 pr_err("unable to register rfkill device\n");
2701 rfkill_destroy(dev->bt_rfk);
2702 goto error;
2703 }
2704 }
2705
2706 if (toshiba_illumination_available(dev)) {
2707 dev->led_dev.name = "toshiba::illumination";
2708 dev->led_dev.max_brightness = 1;
2709 dev->led_dev.brightness_set = toshiba_illumination_set;
2710 dev->led_dev.brightness_get = toshiba_illumination_get;
2711 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002712 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002713 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002714
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002715 if (toshiba_eco_mode_available(dev)) {
2716 dev->eco_led.name = "toshiba::eco_mode";
2717 dev->eco_led.max_brightness = 1;
2718 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2719 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2720 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2721 dev->eco_supported = 1;
2722 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002723
Azael Avalos93f8c162014-09-12 18:50:36 -06002724 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002725 /*
2726 * Only register the LED if KBD illumination is supported
2727 * and the keyboard backlight operation mode is set to FN-Z
2728 */
2729 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2730 dev->kbd_led.name = "toshiba::kbd_backlight";
2731 dev->kbd_led.max_brightness = 1;
2732 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2733 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2734 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2735 dev->kbd_led_registered = 1;
2736 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002737
Azael Avalos9d8658a2014-03-25 20:38:32 -06002738 ret = toshiba_touchpad_get(dev, &dummy);
2739 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002740
Azael Avalos5a2813e2014-03-25 20:38:34 -06002741 ret = toshiba_accelerometer_supported(dev);
2742 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002743
Azael Avalose26ffe52015-01-18 18:30:22 -07002744 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2745 dev->usb_sleep_charge_supported = !ret;
2746
Azael Avalosbb3fe012015-01-18 18:30:24 -07002747 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2748 dev->usb_rapid_charge_supported = !ret;
2749
Azael Avalos172ce0a2015-01-18 18:30:25 -07002750 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2751 dev->usb_sleep_music_supported = !ret;
2752
Azael Avalosbae84192015-02-10 21:09:18 -07002753 ret = toshiba_function_keys_get(dev, &dummy);
2754 dev->kbd_function_keys_supported = !ret;
2755
Azael Avalos35d53ce2015-02-10 21:09:19 -07002756 ret = toshiba_panel_power_on_get(dev, &dummy);
2757 dev->panel_power_on_supported = !ret;
2758
Azael Avalos17fe4b32015-02-10 21:09:20 -07002759 ret = toshiba_usb_three_get(dev, &dummy);
2760 dev->usb_three_supported = !ret;
2761
Seth Forshee36d03f92011-09-20 16:55:53 -05002762 /* Determine whether or not BIOS supports fan and video interfaces */
2763
2764 ret = get_video_status(dev, &dummy);
2765 dev->video_supported = !ret;
2766
2767 ret = get_fan_status(dev, &dummy);
2768 dev->fan_supported = !ret;
2769
Azael Avalos360f0f32014-03-25 20:38:31 -06002770 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2771 &toshiba_attr_group);
2772 if (ret) {
2773 dev->sysfs_created = 0;
2774 goto error;
2775 }
2776 dev->sysfs_created = !ret;
2777
Seth Forshee36d03f92011-09-20 16:55:53 -05002778 create_toshiba_proc_entries(dev);
2779
Seth Forshee29cd2932012-01-18 13:44:09 -06002780 toshiba_acpi = dev;
2781
Seth Forshee135740d2011-09-20 16:55:49 -05002782 return 0;
2783
2784error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002785 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002786 return ret;
2787}
2788
2789static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2790{
2791 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002792 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002793
Azael Avalos71454d72014-12-04 20:22:46 -07002794 switch (event) {
2795 case 0x80: /* Hotkeys and some system events */
2796 toshiba_acpi_process_hotkeys(dev);
2797 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002798 case 0x81: /* Dock events */
2799 case 0x82:
2800 case 0x83:
2801 pr_info("Dock event received %x\n", event);
2802 break;
2803 case 0x88: /* Thermal events */
2804 pr_info("Thermal event received\n");
2805 break;
2806 case 0x8f: /* LID closed */
2807 case 0x90: /* LID is closed and Dock has been ejected */
2808 break;
2809 case 0x8c: /* SATA power events */
2810 case 0x8b:
2811 pr_info("SATA power event received %x\n", event);
2812 break;
Azael Avalos80546902014-12-04 20:22:47 -07002813 case 0x92: /* Keyboard backlight mode changed */
2814 /* Update sysfs entries */
2815 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2816 &toshiba_attr_group);
2817 if (ret)
2818 pr_err("Unable to update sysfs entries\n");
2819 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002820 case 0x85: /* Unknown */
2821 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002822 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002823 case 0x94: /* Unknown */
2824 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002825 default:
2826 pr_info("Unknown event received %x\n", event);
2827 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002828 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002829
2830 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2831 dev_name(&acpi_dev->dev),
2832 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002833}
2834
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002835#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002836static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002837{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002838 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002839 u32 result;
2840
2841 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002842 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002843
2844 return 0;
2845}
2846
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002847static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002848{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002849 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002850 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002851
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002852 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002853 error = toshiba_acpi_enable_hotkeys(dev);
2854 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002855 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002856 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002857
2858 return 0;
2859}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002860#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002861
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002862static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2863 toshiba_acpi_suspend, toshiba_acpi_resume);
2864
Seth Forshee135740d2011-09-20 16:55:49 -05002865static struct acpi_driver toshiba_acpi_driver = {
2866 .name = "Toshiba ACPI driver",
2867 .owner = THIS_MODULE,
2868 .ids = toshiba_device_ids,
2869 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2870 .ops = {
2871 .add = toshiba_acpi_add,
2872 .remove = toshiba_acpi_remove,
2873 .notify = toshiba_acpi_notify,
2874 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002875 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002876};
Holger Machtc9263552006-10-20 14:30:29 -07002877
Len Brown4be44fc2005-08-05 00:44:28 -04002878static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Seth Forshee135740d2011-09-20 16:55:49 -05002880 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Seth Forsheef11f9992012-01-18 13:44:11 -06002882 /*
2883 * Machines with this WMI guid aren't supported due to bugs in
2884 * their AML. This check relies on wmi initializing before
2885 * toshiba_acpi to guarantee guids have been identified.
2886 */
2887 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2888 return -ENODEV;
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2891 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002892 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002893 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895
Seth Forshee135740d2011-09-20 16:55:49 -05002896 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2897 if (ret) {
2898 pr_err("Failed to register ACPI driver: %d\n", ret);
2899 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002900 }
2901
Seth Forshee135740d2011-09-20 16:55:49 -05002902 return ret;
2903}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002904
Seth Forshee135740d2011-09-20 16:55:49 -05002905static void __exit toshiba_acpi_exit(void)
2906{
2907 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2908 if (toshiba_proc_dir)
2909 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912module_init(toshiba_acpi_init);
2913module_exit(toshiba_acpi_exit);