blob: 734c98f9d493a1973cc67d5cbe8c3308eb3b6935 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02007 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07008 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * The devolpment page for this driver is located at
26 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 *
28 * Credits:
29 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30 * engineering the Windows drivers
31 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32 * Rob Miller - TV out and hotkeys help
33 *
34 *
35 * TODO
36 *
37 */
38
Joe Perches7e334602011-03-29 15:21:52 -070039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Azael Avalos7216d702015-02-10 21:09:21 -070041#define TOSHIBA_ACPI_VERSION "0.21"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define PROC_INTERFACE_VERSION 1
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/types.h>
48#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080049#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070050#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040051#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050052#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070053#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020054#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060056#include <linux/workqueue.h>
57#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080058#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020059#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_AUTHOR("John Belmonte");
63MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64MODULE_LICENSE("GPL");
65
Seth Forsheef11f9992012-01-18 13:44:11 -060066#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
Seth Forshee29cd2932012-01-18 13:44:09 -060068/* Scan code for Fn key on TOS1900 models */
69#define TOS1900_FN_SCAN 0x6e
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
73
Azael Avalos258c5902014-09-29 20:40:07 -060074/* The Toshiba configuration interface is composed of the HCI and the SCI,
75 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78 * be uniform across all their models. Ideally we would just call
79 * dedicated ACPI methods instead of using this primitive interface.
80 * However the ACPI methods seem to be incomplete in some areas (for
81 * example they allow setting, but not reading, the LCD brightness value),
82 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040083 *
Azael Avalos84a62732014-03-25 20:38:29 -060084 * SCI stands for "System Configuration Interface" which aim is to
85 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Azael Avalos258c5902014-09-29 20:40:07 -060088#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* operations */
91#define HCI_SET 0xff00
92#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060093#define SCI_OPEN 0xf100
94#define SCI_CLOSE 0xf200
95#define SCI_GET 0xf300
96#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060099#define TOS_SUCCESS 0x0000
100#define TOS_OPEN_CLOSE_OK 0x0044
101#define TOS_FAILURE 0x1000
102#define TOS_NOT_SUPPORTED 0x8000
103#define TOS_ALREADY_OPEN 0x8100
104#define TOS_NOT_OPENED 0x8200
105#define TOS_INPUT_DATA_ERROR 0x8300
106#define TOS_WRITE_PROTECTED 0x8400
107#define TOS_NOT_PRESENT 0x8600
108#define TOS_FIFO_EMPTY 0x8c00
109#define TOS_DATA_NOT_AVAILABLE 0x8d20
110#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700111#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* registers */
114#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900115#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define HCI_SYSTEM_EVENT 0x0016
117#define HCI_VIDEO_OUT 0x001c
118#define HCI_HOTKEY_EVENT 0x001e
119#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400120#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600121#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600122#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600123#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600124#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos35d53ce2015-02-10 21:09:19 -0700125#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600126#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700127#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600128#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700129#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700130#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600131#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700132#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600135#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600136#define HCI_HOTKEY_DISABLE 0x0b
137#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define HCI_LCD_BRIGHTNESS_BITS 3
139#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
140#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600141#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define HCI_VIDEO_OUT_LCD 0x1
143#define HCI_VIDEO_OUT_CRT 0x2
144#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400145#define HCI_WIRELESS_KILL_SWITCH 0x01
146#define HCI_WIRELESS_BT_PRESENT 0x0f
147#define HCI_WIRELESS_BT_ATTACH 0x40
148#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600149#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600150#define SCI_KBD_MODE_FNZ 0x1
151#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600152#define SCI_KBD_MODE_ON 0x8
153#define SCI_KBD_MODE_OFF 0x10
154#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700155#define SCI_USB_CHARGE_MODE_MASK 0xff
156#define SCI_USB_CHARGE_DISABLED 0x30000
157#define SCI_USB_CHARGE_ALTERNATE 0x30009
158#define SCI_USB_CHARGE_AUTO 0x30021
Azael Avalos182bcaa2015-01-18 18:30:23 -0700159#define SCI_USB_CHARGE_BAT_MASK 0x7
160#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
161#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
162#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700163#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Seth Forshee135740d2011-09-20 16:55:49 -0500165struct toshiba_acpi_dev {
166 struct acpi_device *acpi_dev;
167 const char *method_hci;
168 struct rfkill *bt_rfk;
169 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600170 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500171 struct backlight_device *backlight_dev;
172 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600173 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600174 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500175
Seth Forshee135740d2011-09-20 16:55:49 -0500176 int force_fan;
177 int last_key_event;
178 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600179 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600180 int kbd_mode;
181 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700182 int usbsc_bat_level;
Seth Forshee135740d2011-09-20 16:55:49 -0500183
Dan Carpenter592b7462012-01-15 14:28:20 +0300184 unsigned int illumination_supported:1;
185 unsigned int video_supported:1;
186 unsigned int fan_supported:1;
187 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600188 unsigned int ntfy_supported:1;
189 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900190 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600191 unsigned int kbd_illum_supported:1;
192 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600193 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600194 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600195 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700196 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700197 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700198 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700199 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700200 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700201 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600202 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500203
Seth Forshee135740d2011-09-20 16:55:49 -0500204 struct mutex mutex;
205};
206
Seth Forshee29cd2932012-01-18 13:44:09 -0600207static struct toshiba_acpi_dev *toshiba_acpi;
208
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800209static const struct acpi_device_id toshiba_device_ids[] = {
210 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100211 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400212 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800213 {"TOS1900", 0},
214 {"", 0},
215};
216MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
217
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800218static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100219 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700220 { KE_KEY, 0x101, { KEY_MUTE } },
221 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
222 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600223 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600224 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
225 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700226 { KE_KEY, 0x13b, { KEY_COFFEE } },
227 { KE_KEY, 0x13c, { KEY_BATTERY } },
228 { KE_KEY, 0x13d, { KEY_SLEEP } },
229 { KE_KEY, 0x13e, { KEY_SUSPEND } },
230 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
231 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
232 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
233 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600234 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100235 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700236 { KE_KEY, 0xb05, { KEY_PROG2 } },
237 { KE_KEY, 0xb06, { KEY_WWW } },
238 { KE_KEY, 0xb07, { KEY_MAIL } },
239 { KE_KEY, 0xb30, { KEY_STOP } },
240 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
241 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
242 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
243 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600244 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
245 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
246 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
247 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
248 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700249 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500250};
251
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200252/* alternative keymap */
253static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
254 {
255 .matches = {
256 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
257 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
258 },
259 },
Azael Avalose6efad72014-08-04 09:21:02 -0600260 {
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
263 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
264 },
265 },
Aaron Lub1bde682014-10-23 16:18:02 +0800266 {
267 .matches = {
268 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
269 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
270 },
271 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200272 {}
273};
274
275static const struct key_entry toshiba_acpi_alt_keymap[] = {
276 { KE_KEY, 0x157, { KEY_MUTE } },
277 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
278 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600279 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200280 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
281 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
282 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
283 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
284 { KE_KEY, 0x158, { KEY_WLAN } },
285 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
286 { KE_END, 0 },
287};
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* utility
290 */
291
Azael Avalosb5163992015-02-10 23:43:56 -0700292static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 *word = (*word & ~mask) | (mask * value);
295}
296
297/* acpi interface wrappers
298 */
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 acpi_status status;
303
Zhang Rui619400d2013-09-03 08:31:56 +0800304 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500305 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Azael Avalos258c5902014-09-29 20:40:07 -0600308/* Perform a raw configuration call. Here we don't care about input or output
309 * 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
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400342/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
344 * In addition to the ACPI status, the HCI system returns a result which
345 * may be useful (such as "not supported").
346 */
347
Azael Avalos893f3f62014-09-29 20:40:09 -0600348static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Azael Avalos258c5902014-09-29 20:40:07 -0600350 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
351 u32 out[TCI_WORDS];
352 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600353
354 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Azael Avalos893f3f62014-09-29 20:40:09 -0600357static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Azael Avalos258c5902014-09-29 20:40:07 -0600359 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
360 u32 out[TCI_WORDS];
361 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700362
Azael Avalos893f3f62014-09-29 20:40:09 -0600363 if (ACPI_FAILURE(status))
364 return TOS_FAILURE;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600367
368 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Azael Avalos893f3f62014-09-29 20:40:09 -0600371static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400372{
Azael Avalos258c5902014-09-29 20:40:07 -0600373 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
374 u32 out[TCI_WORDS];
375 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600376
377 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400378}
379
Azael Avalosb5163992015-02-10 23:43:56 -0700380static u32 hci_read2(struct toshiba_acpi_dev *dev,
381 u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400382{
Azael Avalos258c5902014-09-29 20:40:07 -0600383 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
384 u32 out[TCI_WORDS];
385 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700386
Azael Avalos893f3f62014-09-29 20:40:09 -0600387 if (ACPI_FAILURE(status))
388 return TOS_FAILURE;
389
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400390 *out1 = out[2];
391 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600392
393 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400394}
395
Azael Avalos84a62732014-03-25 20:38:29 -0600396/* common sci tasks
397 */
398
399static int sci_open(struct toshiba_acpi_dev *dev)
400{
Azael Avalos258c5902014-09-29 20:40:07 -0600401 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
402 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600403 acpi_status status;
404
Azael Avalos258c5902014-09-29 20:40:07 -0600405 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600406 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600407 pr_err("ACPI call to open SCI failed\n");
408 return 0;
409 }
410
Azael Avalos1864bbc2014-09-29 20:40:08 -0600411 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600412 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600413 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600414 pr_info("Toshiba SCI already opened\n");
415 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700416 } else if (out[0] == TOS_NOT_SUPPORTED) {
417 /* Some BIOSes do not have the SCI open/close functions
418 * implemented and return 0x8000 (Not Supported), failing to
419 * register some supported features.
420 *
421 * Simply return 1 if we hit those affected laptops to make the
422 * supported features work.
423 *
424 * In the case that some laptops really do not support the SCI,
425 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
426 * and thus, not registering support for the queried feature.
427 */
428 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600429 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600430 pr_info("Toshiba SCI is not present\n");
431 }
432
433 return 0;
434}
435
436static void sci_close(struct toshiba_acpi_dev *dev)
437{
Azael Avalos258c5902014-09-29 20:40:07 -0600438 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
439 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600440 acpi_status status;
441
Azael Avalos258c5902014-09-29 20:40:07 -0600442 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600443 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600444 pr_err("ACPI call to close SCI failed\n");
445 return;
446 }
447
Azael Avalos1864bbc2014-09-29 20:40:08 -0600448 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600449 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600450 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600451 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600452 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600453 pr_info("Toshiba SCI is not present\n");
454}
455
Azael Avalos893f3f62014-09-29 20:40:09 -0600456static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600457{
Azael Avalos258c5902014-09-29 20:40:07 -0600458 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
459 u32 out[TCI_WORDS];
460 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700461
Azael Avalos893f3f62014-09-29 20:40:09 -0600462 if (ACPI_FAILURE(status))
463 return TOS_FAILURE;
464
Azael Avalos84a62732014-03-25 20:38:29 -0600465 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600466
467 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600468}
469
Azael Avalos893f3f62014-09-29 20:40:09 -0600470static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600471{
Azael Avalos258c5902014-09-29 20:40:07 -0600472 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
473 u32 out[TCI_WORDS];
474 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600475
476 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600477}
478
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200479/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500480static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200481{
Azael Avalos258c5902014-09-29 20:40:07 -0600482 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
483 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200484 acpi_status status;
485
Azael Avalosfdb79082014-03-25 20:38:30 -0600486 if (!sci_open(dev))
487 return 0;
488
Azael Avalos258c5902014-09-29 20:40:07 -0600489 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600490 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600491 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600492 pr_err("ACPI call to query Illumination support failed\n");
493 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600494 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700495 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200496 return 0;
497 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600498
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200499 return 1;
500}
501
502static void toshiba_illumination_set(struct led_classdev *cdev,
503 enum led_brightness brightness)
504{
Seth Forshee135740d2011-09-20 16:55:49 -0500505 struct toshiba_acpi_dev *dev = container_of(cdev,
506 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200508
509 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600510 if (!sci_open(dev))
511 return;
512
513 /* Switch the illumination on/off */
514 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600515 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600516 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600517 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600518 pr_err("ACPI call for illumination failed\n");
519 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600520 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600521 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200522 return;
523 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200524}
525
526static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
527{
Seth Forshee135740d2011-09-20 16:55:49 -0500528 struct toshiba_acpi_dev *dev = container_of(cdev,
529 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600530 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200531
532 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600533 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200534 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200535
536 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600537 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600538 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600539 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600540 pr_err("ACPI call for illumination failed\n");
541 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600542 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600543 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200544 return LED_OFF;
545 }
546
Azael Avalosfdb79082014-03-25 20:38:30 -0600547 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200548}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400549
Azael Avalos360f0f32014-03-25 20:38:31 -0600550/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600551static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
552{
Azael Avalos258c5902014-09-29 20:40:07 -0600553 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
554 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600555 acpi_status status;
556
557 if (!sci_open(dev))
558 return 0;
559
Azael Avalos258c5902014-09-29 20:40:07 -0600560 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600561 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600562 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600563 pr_err("ACPI call to query kbd illumination support failed\n");
564 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600565 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600566 pr_info("Keyboard illumination not available\n");
567 return 0;
568 }
569
570 /* Check for keyboard backlight timeout max value,
571 * previous kbd backlight implementation set this to
572 * 0x3c0003, and now the new implementation set this
573 * to 0x3c001a, use this to distinguish between them
574 */
575 if (out[3] == SCI_KBD_TIME_MAX)
576 dev->kbd_type = 2;
577 else
578 dev->kbd_type = 1;
579 /* Get the current keyboard backlight mode */
580 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
581 /* Get the current time (1-60 seconds) */
582 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
583
584 return 1;
585}
586
Azael Avalos360f0f32014-03-25 20:38:31 -0600587static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
588{
589 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600590
591 if (!sci_open(dev))
592 return -EIO;
593
Azael Avalos893f3f62014-09-29 20:40:09 -0600594 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600595 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600596 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600597 pr_err("ACPI call to set KBD backlight status failed\n");
598 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600599 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600600 pr_info("Keyboard backlight status not supported\n");
601 return -ENODEV;
602 }
603
604 return 0;
605}
606
607static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
608{
609 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600610
611 if (!sci_open(dev))
612 return -EIO;
613
Azael Avalos893f3f62014-09-29 20:40:09 -0600614 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600615 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600616 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600617 pr_err("ACPI call to get KBD backlight status failed\n");
618 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600619 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600620 pr_info("Keyboard backlight status not supported\n");
621 return -ENODEV;
622 }
623
624 return 0;
625}
626
627static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
628{
629 struct toshiba_acpi_dev *dev = container_of(cdev,
630 struct toshiba_acpi_dev, kbd_led);
631 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600632
633 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600634 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
635 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600636 pr_err("ACPI call to get the keyboard backlight failed\n");
637 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600638 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600639 pr_info("Keyboard backlight not supported\n");
640 return LED_OFF;
641 }
642
643 return state ? LED_FULL : LED_OFF;
644}
645
646static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
647 enum led_brightness brightness)
648{
649 struct toshiba_acpi_dev *dev = container_of(cdev,
650 struct toshiba_acpi_dev, kbd_led);
651 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600652
653 /* Set the keyboard backlight state */
654 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600655 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
656 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600657 pr_err("ACPI call to set KBD Illumination mode failed\n");
658 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600659 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600660 pr_info("Keyboard backlight not supported\n");
661 return;
662 }
663}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400664
Azael Avalos9d8658a2014-03-25 20:38:32 -0600665/* TouchPad support */
666static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
667{
668 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600669
670 if (!sci_open(dev))
671 return -EIO;
672
Azael Avalos893f3f62014-09-29 20:40:09 -0600673 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600674 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600675 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600676 pr_err("ACPI call to set the touchpad failed\n");
677 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600678 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600679 return -ENODEV;
680 }
681
682 return 0;
683}
684
685static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
686{
687 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600688
689 if (!sci_open(dev))
690 return -EIO;
691
Azael Avalos893f3f62014-09-29 20:40:09 -0600692 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600693 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600694 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600695 pr_err("ACPI call to query the touchpad failed\n");
696 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600697 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600698 return -ENODEV;
699 }
700
701 return 0;
702}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200703
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600704/* Eco Mode support */
705static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
706{
707 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700708 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600709 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600710
Azael Avalos258c5902014-09-29 20:40:07 -0600711 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700712 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
713 pr_err("ACPI call to get ECO led failed\n");
714 } else if (out[0] == TOS_NOT_INSTALLED) {
715 pr_info("ECO led not installed");
716 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
717 /* If we receive 0x8300 (Input Data Error), it means that the
718 * LED device is present, but that we just screwed the input
719 * parameters.
720 *
721 * Let's query the status of the LED to see if we really have a
722 * success response, indicating the actual presense of the LED,
723 * bail out otherwise.
724 */
725 in[3] = 1;
726 status = tci_raw(dev, in, out);
727 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
728 pr_err("ACPI call to get ECO led failed\n");
729 else if (out[0] == TOS_SUCCESS)
730 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600731 }
732
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700733 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600734}
735
Azael Avalosb5163992015-02-10 23:43:56 -0700736static enum led_brightness
737toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600738{
739 struct toshiba_acpi_dev *dev = container_of(cdev,
740 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600741 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
742 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600743 acpi_status status;
744
Azael Avalos258c5902014-09-29 20:40:07 -0600745 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600746 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600747 pr_err("ACPI call to get ECO led failed\n");
748 return LED_OFF;
749 }
750
751 return out[2] ? LED_FULL : LED_OFF;
752}
753
754static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
755 enum led_brightness brightness)
756{
757 struct toshiba_acpi_dev *dev = container_of(cdev,
758 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600759 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
760 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600761 acpi_status status;
762
763 /* Switch the Eco Mode led on/off */
764 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600765 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600766 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600767 pr_err("ACPI call to set ECO led failed\n");
768 return;
769 }
770}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400771
Azael Avalos5a2813e2014-03-25 20:38:34 -0600772/* Accelerometer support */
773static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
774{
Azael Avalos258c5902014-09-29 20:40:07 -0600775 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
776 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600777 acpi_status status;
778
779 /* Check if the accelerometer call exists,
780 * this call also serves as initialization
781 */
Azael Avalos258c5902014-09-29 20:40:07 -0600782 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600783 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600784 pr_err("ACPI call to query the accelerometer failed\n");
785 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600786 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
787 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600788 pr_err("Accelerometer not initialized\n");
789 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600790 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600791 pr_info("Accelerometer not supported\n");
792 return -ENODEV;
793 }
794
795 return 0;
796}
797
798static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
799 u32 *xy, u32 *z)
800{
Azael Avalos258c5902014-09-29 20:40:07 -0600801 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
802 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600803 acpi_status status;
804
805 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600806 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600807 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600808 pr_err("ACPI call to query the accelerometer failed\n");
809 return -EIO;
810 }
811
812 *xy = out[2];
813 *z = out[4];
814
815 return 0;
816}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600817
Azael Avalose26ffe52015-01-18 18:30:22 -0700818/* Sleep (Charge and Music) utilities support */
819static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
820 u32 *mode)
821{
822 u32 result;
823
824 if (!sci_open(dev))
825 return -EIO;
826
827 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
828 sci_close(dev);
829 if (result == TOS_FAILURE) {
830 pr_err("ACPI call to set USB S&C mode failed\n");
831 return -EIO;
832 } else if (result == TOS_NOT_SUPPORTED) {
833 pr_info("USB Sleep and Charge not supported\n");
834 return -ENODEV;
835 } else if (result == TOS_INPUT_DATA_ERROR) {
836 return -EIO;
837 }
838
839 return 0;
840}
841
842static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
843 u32 mode)
844{
845 u32 result;
846
847 if (!sci_open(dev))
848 return -EIO;
849
850 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
851 sci_close(dev);
852 if (result == TOS_FAILURE) {
853 pr_err("ACPI call to set USB S&C mode failed\n");
854 return -EIO;
855 } else if (result == TOS_NOT_SUPPORTED) {
856 pr_info("USB Sleep and Charge not supported\n");
857 return -ENODEV;
858 } else if (result == TOS_INPUT_DATA_ERROR) {
859 return -EIO;
860 }
861
862 return 0;
863}
864
Azael Avalos182bcaa2015-01-18 18:30:23 -0700865static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
866 u32 *mode)
867{
868 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
869 u32 out[TCI_WORDS];
870 acpi_status status;
871
872 if (!sci_open(dev))
873 return -EIO;
874
875 in[5] = SCI_USB_CHARGE_BAT_LVL;
876 status = tci_raw(dev, in, out);
877 sci_close(dev);
878 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
879 pr_err("ACPI call to get USB S&C battery level failed\n");
880 return -EIO;
881 } else if (out[0] == TOS_NOT_SUPPORTED) {
882 pr_info("USB Sleep and Charge not supported\n");
883 return -ENODEV;
884 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
885 return -EIO;
886 }
887
888 *mode = out[2];
889
890 return 0;
891}
892
893static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
894 u32 mode)
895{
896 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
897 u32 out[TCI_WORDS];
898 acpi_status status;
899
900 if (!sci_open(dev))
901 return -EIO;
902
903 in[2] = mode;
904 in[5] = SCI_USB_CHARGE_BAT_LVL;
905 status = tci_raw(dev, in, out);
906 sci_close(dev);
907 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
908 pr_err("ACPI call to set USB S&C battery level failed\n");
909 return -EIO;
910 } else if (out[0] == TOS_NOT_SUPPORTED) {
911 pr_info("USB Sleep and Charge not supported\n");
912 return -ENODEV;
913 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
914 return -EIO;
915 }
916
917 return 0;
918}
919
Azael Avalosbb3fe012015-01-18 18:30:24 -0700920static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
921 u32 *state)
922{
923 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
924 u32 out[TCI_WORDS];
925 acpi_status status;
926
927 if (!sci_open(dev))
928 return -EIO;
929
930 in[5] = SCI_USB_CHARGE_RAPID_DSP;
931 status = tci_raw(dev, in, out);
932 sci_close(dev);
933 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
934 pr_err("ACPI call to get USB S&C battery level failed\n");
935 return -EIO;
936 } else if (out[0] == TOS_NOT_SUPPORTED ||
937 out[0] == TOS_INPUT_DATA_ERROR) {
938 pr_info("USB Sleep and Charge not supported\n");
939 return -ENODEV;
940 }
941
942 *state = out[2];
943
944 return 0;
945}
946
947static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
948 u32 state)
949{
950 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
951 u32 out[TCI_WORDS];
952 acpi_status status;
953
954 if (!sci_open(dev))
955 return -EIO;
956
957 in[2] = state;
958 in[5] = SCI_USB_CHARGE_RAPID_DSP;
959 status = tci_raw(dev, in, out);
960 sci_close(dev);
961 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
962 pr_err("ACPI call to set USB S&C battery level failed\n");
963 return -EIO;
964 } else if (out[0] == TOS_NOT_SUPPORTED) {
965 pr_info("USB Sleep and Charge not supported\n");
966 return -ENODEV;
967 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
968 return -EIO;
969 }
970
971 return 0;
972}
973
Azael Avalos172ce0a2015-01-18 18:30:25 -0700974static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
975{
976 u32 result;
977
978 if (!sci_open(dev))
979 return -EIO;
980
981 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
982 sci_close(dev);
983 if (result == TOS_FAILURE) {
984 pr_err("ACPI call to set USB S&C mode failed\n");
985 return -EIO;
986 } else if (result == TOS_NOT_SUPPORTED) {
987 pr_info("USB Sleep and Charge not supported\n");
988 return -ENODEV;
989 } else if (result == TOS_INPUT_DATA_ERROR) {
990 return -EIO;
991 }
992
993 return 0;
994}
995
996static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
997{
998 u32 result;
999
1000 if (!sci_open(dev))
1001 return -EIO;
1002
1003 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1004 sci_close(dev);
1005 if (result == TOS_FAILURE) {
1006 pr_err("ACPI call to set USB S&C mode failed\n");
1007 return -EIO;
1008 } else if (result == TOS_NOT_SUPPORTED) {
1009 pr_info("USB Sleep and Charge not supported\n");
1010 return -ENODEV;
1011 } else if (result == TOS_INPUT_DATA_ERROR) {
1012 return -EIO;
1013 }
1014
1015 return 0;
1016}
1017
Azael Avalosbae84192015-02-10 21:09:18 -07001018/* Keyboard function keys */
1019static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1020{
1021 u32 result;
1022
1023 if (!sci_open(dev))
1024 return -EIO;
1025
1026 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1027 sci_close(dev);
1028 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1029 pr_err("ACPI call to get KBD function keys failed\n");
1030 return -EIO;
1031 } else if (result == TOS_NOT_SUPPORTED) {
1032 pr_info("KBD function keys not supported\n");
1033 return -ENODEV;
1034 }
1035
1036 return 0;
1037}
1038
1039static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1040{
1041 u32 result;
1042
1043 if (!sci_open(dev))
1044 return -EIO;
1045
1046 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1047 sci_close(dev);
1048 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1049 pr_err("ACPI call to set KBD function keys failed\n");
1050 return -EIO;
1051 } else if (result == TOS_NOT_SUPPORTED) {
1052 pr_info("KBD function keys not supported\n");
1053 return -ENODEV;
1054 }
1055
1056 return 0;
1057}
1058
Azael Avalos35d53ce2015-02-10 21:09:19 -07001059/* Panel Power ON */
1060static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1061{
1062 u32 result;
1063
1064 if (!sci_open(dev))
1065 return -EIO;
1066
1067 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1068 sci_close(dev);
1069 if (result == TOS_FAILURE) {
1070 pr_err("ACPI call to get Panel Power ON failed\n");
1071 return -EIO;
1072 } else if (result == TOS_NOT_SUPPORTED) {
1073 pr_info("Panel Power on not supported\n");
1074 return -ENODEV;
1075 } else if (result == TOS_INPUT_DATA_ERROR) {
1076 return -EIO;
1077 }
1078
1079 return 0;
1080}
1081
1082static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1083{
1084 u32 result;
1085
1086 if (!sci_open(dev))
1087 return -EIO;
1088
1089 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1090 sci_close(dev);
1091 if (result == TOS_FAILURE) {
1092 pr_err("ACPI call to set Panel Power ON failed\n");
1093 return -EIO;
1094 } else if (result == TOS_NOT_SUPPORTED) {
1095 pr_info("Panel Power ON not supported\n");
1096 return -ENODEV;
1097 } else if (result == TOS_INPUT_DATA_ERROR) {
1098 return -EIO;
1099 }
1100
1101 return 0;
1102}
1103
Azael Avalos17fe4b32015-02-10 21:09:20 -07001104/* USB Three */
1105static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1106{
1107 u32 result;
1108
1109 if (!sci_open(dev))
1110 return -EIO;
1111
1112 result = sci_read(dev, SCI_USB_THREE, state);
1113 sci_close(dev);
1114 if (result == TOS_FAILURE) {
1115 pr_err("ACPI call to get USB 3 failed\n");
1116 return -EIO;
1117 } else if (result == TOS_NOT_SUPPORTED) {
1118 pr_info("USB 3 not supported\n");
1119 return -ENODEV;
1120 } else if (result == TOS_INPUT_DATA_ERROR) {
1121 return -EIO;
1122 }
1123
1124 return 0;
1125}
1126
1127static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1128{
1129 u32 result;
1130
1131 if (!sci_open(dev))
1132 return -EIO;
1133
1134 result = sci_write(dev, SCI_USB_THREE, state);
1135 sci_close(dev);
1136 if (result == TOS_FAILURE) {
1137 pr_err("ACPI call to set USB 3 failed\n");
1138 return -EIO;
1139 } else if (result == TOS_NOT_SUPPORTED) {
1140 pr_info("USB 3 not supported\n");
1141 return -ENODEV;
1142 } else if (result == TOS_INPUT_DATA_ERROR) {
1143 return -EIO;
1144 }
1145
1146 return 0;
1147}
1148
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001149/* Bluetooth rfkill handlers */
1150
Seth Forshee135740d2011-09-20 16:55:49 -05001151static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001152{
1153 u32 hci_result;
1154 u32 value, value2;
1155
1156 value = 0;
1157 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -06001158 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001159 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001160 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1161
1162 return hci_result;
1163}
1164
Seth Forshee135740d2011-09-20 16:55:49 -05001165static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001166{
1167 u32 hci_result;
1168 u32 value, value2;
1169
1170 value = 0;
1171 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -06001172 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001173
1174 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1175 return hci_result;
1176}
1177
Johannes Berg19d337d2009-06-02 13:01:37 +02001178static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001179{
Johannes Berg19d337d2009-06-02 13:01:37 +02001180 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001181 u32 result1, result2;
1182 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +02001183 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001184 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001185
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001187
1188 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001189 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001190 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001191 goto out;
1192 }
1193
1194 if (!radio_state) {
1195 err = 0;
1196 goto out;
1197 }
1198
Azael Avalos893f3f62014-09-29 20:40:09 -06001199 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1200 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001201
Azael Avalos1864bbc2014-09-29 20:40:08 -06001202 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001203 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001204 else
1205 err = 0;
1206 out:
1207 mutex_unlock(&dev->mutex);
1208 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001209}
1210
Johannes Berg19d337d2009-06-02 13:01:37 +02001211static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001212{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001213 bool new_rfk_state;
1214 bool value;
1215 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +02001216 struct toshiba_acpi_dev *dev = data;
1217
1218 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001219
Seth Forshee135740d2011-09-20 16:55:49 -05001220 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001221 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001222 /* Can't do anything useful */
1223 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001224 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001225 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001226
1227 new_rfk_state = value;
1228
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001229 mutex_unlock(&dev->mutex);
1230
Johannes Berg19d337d2009-06-02 13:01:37 +02001231 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1232 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001233}
1234
Johannes Berg19d337d2009-06-02 13:01:37 +02001235static const struct rfkill_ops toshiba_rfk_ops = {
1236 .set_block = bt_rfkill_set_block,
1237 .poll = bt_rfkill_poll,
1238};
1239
Akio Idehara121b7b02012-04-06 01:46:43 +09001240static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1241{
1242 u32 hci_result;
1243 u32 status;
1244
Azael Avalos893f3f62014-09-29 20:40:09 -06001245 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001246 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001247 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001248}
1249
1250static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1251{
1252 u32 hci_result;
1253 u32 value = !enable;
1254
Azael Avalos893f3f62014-09-29 20:40:09 -06001255 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001256 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001257}
1258
Azael Avalosb5163992015-02-10 23:43:56 -07001259static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Seth Forshee62cce752012-04-19 11:23:50 -05001261static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 u32 hci_result;
1264 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001265 int brightness = 0;
1266
1267 if (dev->tr_backlight_supported) {
1268 bool enabled;
1269 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001270
Akio Idehara121b7b02012-04-06 01:46:43 +09001271 if (ret)
1272 return ret;
1273 if (enabled)
1274 return 0;
1275 brightness++;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Azael Avalos893f3f62014-09-29 20:40:09 -06001278 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001279 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001280 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001281
1282 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001283}
1284
Seth Forshee62cce752012-04-19 11:23:50 -05001285static int get_lcd_brightness(struct backlight_device *bd)
1286{
1287 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001288
Seth Forshee62cce752012-04-19 11:23:50 -05001289 return __get_lcd_brightness(dev);
1290}
1291
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001292static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001293{
Seth Forshee135740d2011-09-20 16:55:49 -05001294 struct toshiba_acpi_dev *dev = m->private;
1295 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001296 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001297
Seth Forshee135740d2011-09-20 16:55:49 -05001298 if (!dev->backlight_dev)
1299 return -ENODEV;
1300
Akio Idehara121b7b02012-04-06 01:46:43 +09001301 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001302 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001303 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001304 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001305 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001309 pr_err("Error reading LCD brightness\n");
1310 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001311}
1312
1313static int lcd_proc_open(struct inode *inode, struct file *file)
1314{
Al Virod9dda782013-03-31 18:16:14 -04001315 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Seth Forshee62cce752012-04-19 11:23:50 -05001318static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001319{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001320 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001321
Akio Idehara121b7b02012-04-06 01:46:43 +09001322 if (dev->tr_backlight_supported) {
1323 bool enable = !value;
1324 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001325
Akio Idehara121b7b02012-04-06 01:46:43 +09001326 if (ret)
1327 return ret;
1328 if (value)
1329 value--;
1330 }
1331
Azael Avalosa39f46d2014-11-24 19:29:36 -07001332 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1333 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1334 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001335}
1336
1337static int set_lcd_status(struct backlight_device *bd)
1338{
Seth Forshee135740d2011-09-20 16:55:49 -05001339 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001340
Seth Forshee62cce752012-04-19 11:23:50 -05001341 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001342}
1343
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001344static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1345 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Al Virod9dda782013-03-31 18:16:14 -04001347 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001348 char cmd[42];
1349 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001351 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001352 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001354 len = min(count, sizeof(cmd) - 1);
1355 if (copy_from_user(cmd, buf, len))
1356 return -EFAULT;
1357 cmd[len] = '\0';
1358
1359 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001360 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001361 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001362 if (ret == 0)
1363 ret = count;
1364 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001365 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001366 }
Holger Machtc9263552006-10-20 14:30:29 -07001367 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001370static const struct file_operations lcd_proc_fops = {
1371 .owner = THIS_MODULE,
1372 .open = lcd_proc_open,
1373 .read = seq_read,
1374 .llseek = seq_lseek,
1375 .release = single_release,
1376 .write = lcd_proc_write,
1377};
1378
Seth Forshee36d03f92011-09-20 16:55:53 -05001379static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1380{
1381 u32 hci_result;
1382
Azael Avalos893f3f62014-09-29 20:40:09 -06001383 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001384 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001385}
1386
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001387static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Seth Forshee135740d2011-09-20 16:55:49 -05001389 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001391 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Seth Forshee36d03f92011-09-20 16:55:53 -05001393 ret = get_video_status(dev, &value);
1394 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1396 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001397 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001398
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001399 seq_printf(m, "lcd_out: %d\n", is_lcd);
1400 seq_printf(m, "crt_out: %d\n", is_crt);
1401 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Seth Forshee36d03f92011-09-20 16:55:53 -05001404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001407static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Al Virod9dda782013-03-31 18:16:14 -04001409 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001410}
1411
1412static ssize_t video_proc_write(struct file *file, const char __user *buf,
1413 size_t count, loff_t *pos)
1414{
Al Virod9dda782013-03-31 18:16:14 -04001415 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001416 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001417 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int value;
1419 int remain = count;
1420 int lcd_out = -1;
1421 int crt_out = -1;
1422 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001423 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001425 cmd = kmalloc(count + 1, GFP_KERNEL);
1426 if (!cmd)
1427 return -ENOMEM;
1428 if (copy_from_user(cmd, buf, count)) {
1429 kfree(cmd);
1430 return -EFAULT;
1431 }
1432 cmd[count] = '\0';
1433
1434 buffer = cmd;
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* scan expression. Multiple expressions may be delimited with ;
1437 *
1438 * NOTE: to keep scanning simple, invalid fields are ignored
1439 */
1440 while (remain) {
1441 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1442 lcd_out = value & 1;
1443 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1444 crt_out = value & 1;
1445 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1446 tv_out = value & 1;
1447 /* advance to one character past the next ; */
1448 do {
1449 ++buffer;
1450 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001451 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001454 kfree(cmd);
1455
Seth Forshee36d03f92011-09-20 16:55:53 -05001456 ret = get_video_status(dev, &video_out);
1457 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001458 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (lcd_out != -1)
1461 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1462 if (crt_out != -1)
1463 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1464 if (tv_out != -1)
1465 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1466 /* To avoid unnecessary video disruption, only write the new
1467 * video setting if something changed. */
1468 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001469 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001472 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001475static const struct file_operations video_proc_fops = {
1476 .owner = THIS_MODULE,
1477 .open = video_proc_open,
1478 .read = seq_read,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481 .write = video_proc_write,
1482};
1483
Seth Forshee36d03f92011-09-20 16:55:53 -05001484static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1485{
1486 u32 hci_result;
1487
Azael Avalos893f3f62014-09-29 20:40:09 -06001488 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001489 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001490}
1491
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001492static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Seth Forshee135740d2011-09-20 16:55:49 -05001494 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001495 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 u32 value;
1497
Seth Forshee36d03f92011-09-20 16:55:53 -05001498 ret = get_fan_status(dev, &value);
1499 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001500 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001501 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
Seth Forshee36d03f92011-09-20 16:55:53 -05001504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001507static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Al Virod9dda782013-03-31 18:16:14 -04001509 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001510}
1511
1512static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1513 size_t count, loff_t *pos)
1514{
Al Virod9dda782013-03-31 18:16:14 -04001515 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001516 char cmd[42];
1517 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int value;
1519 u32 hci_result;
1520
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001521 len = min(count, sizeof(cmd) - 1);
1522 if (copy_from_user(cmd, buf, len))
1523 return -EFAULT;
1524 cmd[len] = '\0';
1525
1526 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001527 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001528 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001529 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001530 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001531 else
1532 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 } else {
1534 return -EINVAL;
1535 }
1536
1537 return count;
1538}
1539
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001540static const struct file_operations fan_proc_fops = {
1541 .owner = THIS_MODULE,
1542 .open = fan_proc_open,
1543 .read = seq_read,
1544 .llseek = seq_lseek,
1545 .release = single_release,
1546 .write = fan_proc_write,
1547};
1548
1549static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Seth Forshee135740d2011-09-20 16:55:49 -05001551 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 u32 hci_result;
1553 u32 value;
1554
Seth Forshee11948b92011-11-16 17:37:45 -06001555 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001556 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001557 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001558 dev->key_event_valid = 1;
1559 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001560 } else if (hci_result == TOS_FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001562 } else if (hci_result == TOS_NOT_SUPPORTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* This is a workaround for an unresolved issue on
1564 * some machines where system events sporadically
1565 * become disabled. */
Azael Avalos893f3f62014-09-29 20:40:09 -06001566 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001567 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001569 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001570 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 }
1573
Seth Forshee135740d2011-09-20 16:55:49 -05001574 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1575 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001579static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Al Virod9dda782013-03-31 18:16:14 -04001581 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001582}
1583
1584static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1585 size_t count, loff_t *pos)
1586{
Al Virod9dda782013-03-31 18:16:14 -04001587 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001588 char cmd[42];
1589 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int value;
1591
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001592 len = min(count, sizeof(cmd) - 1);
1593 if (copy_from_user(cmd, buf, len))
1594 return -EFAULT;
1595 cmd[len] = '\0';
1596
Azael Avalosb5163992015-02-10 23:43:56 -07001597 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001598 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001599 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 return count;
1603}
1604
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001605static const struct file_operations keys_proc_fops = {
1606 .owner = THIS_MODULE,
1607 .open = keys_proc_open,
1608 .read = seq_read,
1609 .llseek = seq_lseek,
1610 .release = single_release,
1611 .write = keys_proc_write,
1612};
1613
1614static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001616 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1617 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001621static int version_proc_open(struct inode *inode, struct file *file)
1622{
Al Virod9dda782013-03-31 18:16:14 -04001623 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001624}
1625
1626static const struct file_operations version_proc_fops = {
1627 .owner = THIS_MODULE,
1628 .open = version_proc_open,
1629 .read = seq_read,
1630 .llseek = seq_lseek,
1631 .release = single_release,
1632};
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/* proc and module init
1635 */
1636
1637#define PROC_TOSHIBA "toshiba"
1638
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001639static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Seth Forshee36d03f92011-09-20 16:55:53 -05001641 if (dev->backlight_dev)
1642 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1643 &lcd_proc_fops, dev);
1644 if (dev->video_supported)
1645 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1646 &video_proc_fops, dev);
1647 if (dev->fan_supported)
1648 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1649 &fan_proc_fops, dev);
1650 if (dev->hotkey_dev)
1651 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1652 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001653 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1654 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Seth Forshee36d03f92011-09-20 16:55:53 -05001657static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Seth Forshee36d03f92011-09-20 16:55:53 -05001659 if (dev->backlight_dev)
1660 remove_proc_entry("lcd", toshiba_proc_dir);
1661 if (dev->video_supported)
1662 remove_proc_entry("video", toshiba_proc_dir);
1663 if (dev->fan_supported)
1664 remove_proc_entry("fan", toshiba_proc_dir);
1665 if (dev->hotkey_dev)
1666 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001667 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Lionel Debrouxacc24722010-11-16 14:14:02 +01001670static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001671 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001672 .get_brightness = get_lcd_brightness,
1673 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001674};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001675
Azael Avalos360f0f32014-03-25 20:38:31 -06001676/*
1677 * Sysfs files
1678 */
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001679static ssize_t toshiba_version_show(struct device *dev,
1680 struct device_attribute *attr, char *buf);
Azael Avalos94477d42015-02-10 21:09:17 -07001681static ssize_t toshiba_fan_store(struct device *dev,
1682 struct device_attribute *attr,
1683 const char *buf, size_t count);
1684static ssize_t toshiba_fan_show(struct device *dev,
1685 struct device_attribute *attr, char *buf);
Azael Avalos93f8c162014-09-12 18:50:36 -06001686static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1687 struct device_attribute *attr,
1688 const char *buf, size_t count);
1689static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1690 struct device_attribute *attr,
1691 char *buf);
1692static ssize_t toshiba_kbd_type_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf);
1695static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1696 struct device_attribute *attr,
1697 char *buf);
1698static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1699 struct device_attribute *attr,
1700 const char *buf, size_t count);
1701static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1702 struct device_attribute *attr,
1703 char *buf);
1704static ssize_t toshiba_touchpad_store(struct device *dev,
1705 struct device_attribute *attr,
1706 const char *buf, size_t count);
1707static ssize_t toshiba_touchpad_show(struct device *dev,
1708 struct device_attribute *attr,
1709 char *buf);
1710static ssize_t toshiba_position_show(struct device *dev,
1711 struct device_attribute *attr,
1712 char *buf);
Azael Avalose26ffe52015-01-18 18:30:22 -07001713static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1714 struct device_attribute *attr,
1715 char *buf);
1716static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1717 struct device_attribute *attr,
1718 const char *buf, size_t count);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001719static ssize_t sleep_functions_on_battery_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf);
1722static ssize_t sleep_functions_on_battery_store(struct device *dev,
1723 struct device_attribute *attr,
1724 const char *buf, size_t count);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001725static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
1726 struct device_attribute *attr,
1727 char *buf);
1728static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
1729 struct device_attribute *attr,
1730 const char *buf, size_t count);
Azael Avalos172ce0a2015-01-18 18:30:25 -07001731static ssize_t toshiba_usb_sleep_music_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf);
1734static ssize_t toshiba_usb_sleep_music_store(struct device *dev,
1735 struct device_attribute *attr,
1736 const char *buf, size_t count);
Azael Avalosbae84192015-02-10 21:09:18 -07001737static ssize_t toshiba_kbd_function_keys_show(struct device *dev,
1738 struct device_attribute *attr,
1739 char *buf);
1740static ssize_t toshiba_kbd_function_keys_store(struct device *dev,
1741 struct device_attribute *attr,
1742 const char *buf, size_t count);
Azael Avalos35d53ce2015-02-10 21:09:19 -07001743static ssize_t toshiba_panel_power_on_show(struct device *dev,
1744 struct device_attribute *attr,
1745 char *buf);
1746static ssize_t toshiba_panel_power_on_store(struct device *dev,
1747 struct device_attribute *attr,
1748 const char *buf, size_t count);
Azael Avalos17fe4b32015-02-10 21:09:20 -07001749static ssize_t toshiba_usb_three_show(struct device *dev,
1750 struct device_attribute *attr,
1751 char *buf);
1752static ssize_t toshiba_usb_three_store(struct device *dev,
1753 struct device_attribute *attr,
1754 const char *buf, size_t count);
Azael Avalos93f8c162014-09-12 18:50:36 -06001755
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001756static DEVICE_ATTR(version, S_IRUGO, toshiba_version_show, NULL);
Azael Avalos94477d42015-02-10 21:09:17 -07001757static DEVICE_ATTR(fan, S_IRUGO | S_IWUSR,
1758 toshiba_fan_show, toshiba_fan_store);
Azael Avalos93f8c162014-09-12 18:50:36 -06001759static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1760 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1761static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1762static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1763 toshiba_available_kbd_modes_show, NULL);
1764static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1765 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1766static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1767 toshiba_touchpad_show, toshiba_touchpad_store);
1768static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
Azael Avalose26ffe52015-01-18 18:30:22 -07001769static DEVICE_ATTR(usb_sleep_charge, S_IRUGO | S_IWUSR,
1770 toshiba_usb_sleep_charge_show,
1771 toshiba_usb_sleep_charge_store);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001772static DEVICE_ATTR(sleep_functions_on_battery, S_IRUGO | S_IWUSR,
1773 sleep_functions_on_battery_show,
1774 sleep_functions_on_battery_store);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001775static DEVICE_ATTR(usb_rapid_charge, S_IRUGO | S_IWUSR,
1776 toshiba_usb_rapid_charge_show,
1777 toshiba_usb_rapid_charge_store);
Azael Avalos172ce0a2015-01-18 18:30:25 -07001778static DEVICE_ATTR(usb_sleep_music, S_IRUGO | S_IWUSR,
1779 toshiba_usb_sleep_music_show,
1780 toshiba_usb_sleep_music_store);
Azael Avalosbae84192015-02-10 21:09:18 -07001781static DEVICE_ATTR(kbd_function_keys, S_IRUGO | S_IWUSR,
1782 toshiba_kbd_function_keys_show,
1783 toshiba_kbd_function_keys_store);
Azael Avalos35d53ce2015-02-10 21:09:19 -07001784static DEVICE_ATTR(panel_power_on, S_IRUGO | S_IWUSR,
1785 toshiba_panel_power_on_show,
1786 toshiba_panel_power_on_store);
Azael Avalos17fe4b32015-02-10 21:09:20 -07001787static DEVICE_ATTR(usb_three, S_IRUGO | S_IWUSR,
1788 toshiba_usb_three_show, toshiba_usb_three_store);
Azael Avalos93f8c162014-09-12 18:50:36 -06001789
1790static struct attribute *toshiba_attributes[] = {
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001791 &dev_attr_version.attr,
Azael Avalos94477d42015-02-10 21:09:17 -07001792 &dev_attr_fan.attr,
Azael Avalos93f8c162014-09-12 18:50:36 -06001793 &dev_attr_kbd_backlight_mode.attr,
1794 &dev_attr_kbd_type.attr,
1795 &dev_attr_available_kbd_modes.attr,
1796 &dev_attr_kbd_backlight_timeout.attr,
1797 &dev_attr_touchpad.attr,
1798 &dev_attr_position.attr,
Azael Avalose26ffe52015-01-18 18:30:22 -07001799 &dev_attr_usb_sleep_charge.attr,
Azael Avalos182bcaa2015-01-18 18:30:23 -07001800 &dev_attr_sleep_functions_on_battery.attr,
Azael Avalosbb3fe012015-01-18 18:30:24 -07001801 &dev_attr_usb_rapid_charge.attr,
Azael Avalos172ce0a2015-01-18 18:30:25 -07001802 &dev_attr_usb_sleep_music.attr,
Azael Avalosbae84192015-02-10 21:09:18 -07001803 &dev_attr_kbd_function_keys.attr,
Azael Avalos35d53ce2015-02-10 21:09:19 -07001804 &dev_attr_panel_power_on.attr,
Azael Avalos17fe4b32015-02-10 21:09:20 -07001805 &dev_attr_usb_three.attr,
Azael Avalos93f8c162014-09-12 18:50:36 -06001806 NULL,
1807};
1808
1809static umode_t toshiba_sysfs_is_visible(struct kobject *,
1810 struct attribute *, int);
1811
1812static struct attribute_group toshiba_attr_group = {
1813 .is_visible = toshiba_sysfs_is_visible,
1814 .attrs = toshiba_attributes,
1815};
Azael Avalos360f0f32014-03-25 20:38:31 -06001816
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001817static ssize_t toshiba_version_show(struct device *dev,
1818 struct device_attribute *attr, char *buf)
1819{
1820 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1821}
1822
Azael Avalos94477d42015-02-10 21:09:17 -07001823static ssize_t toshiba_fan_store(struct device *dev,
1824 struct device_attribute *attr,
1825 const char *buf, size_t count)
1826{
1827 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1828 u32 result;
1829 int state;
1830 int ret;
1831
1832 ret = kstrtoint(buf, 0, &state);
1833 if (ret)
1834 return ret;
1835
1836 if (state != 0 && state != 1)
1837 return -EINVAL;
1838
1839 result = hci_write1(toshiba, HCI_FAN, state);
1840 if (result == TOS_FAILURE)
1841 return -EIO;
1842 else if (result == TOS_NOT_SUPPORTED)
1843 return -ENODEV;
1844
1845 return count;
1846}
1847
1848static ssize_t toshiba_fan_show(struct device *dev,
1849 struct device_attribute *attr, char *buf)
1850{
1851 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1852 u32 value;
1853 int ret;
1854
1855 ret = get_fan_status(toshiba, &value);
1856 if (ret)
1857 return ret;
1858
1859 return sprintf(buf, "%d\n", value);
1860}
1861
Azael Avalos360f0f32014-03-25 20:38:31 -06001862static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1863 struct device_attribute *attr,
1864 const char *buf, size_t count)
1865{
1866 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001867 int mode;
1868 int time;
1869 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001870
Dan Carpenteraeaac092014-09-03 14:44:37 +03001871
1872 ret = kstrtoint(buf, 0, &mode);
1873 if (ret)
1874 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001875
1876 /* Check for supported modes depending on keyboard backlight type */
1877 if (toshiba->kbd_type == 1) {
1878 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1879 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1880 return -EINVAL;
1881 } else if (toshiba->kbd_type == 2) {
1882 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1883 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1884 mode != SCI_KBD_MODE_OFF)
1885 return -EINVAL;
1886 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001887
1888 /* Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001889 * Auto - KBD backlight turns off automatically in given time
1890 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001891 * ON - KBD backlight is always on
1892 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001893 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001894
1895 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001896 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001897 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001898 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001899
1900 /* OR the "base time" to the actual method format */
1901 if (toshiba->kbd_type == 1) {
1902 /* Type 1 requires the current mode */
1903 time |= toshiba->kbd_mode;
1904 } else if (toshiba->kbd_type == 2) {
1905 /* Type 2 requires the desired mode */
1906 time |= mode;
1907 }
1908
Dan Carpenteraeaac092014-09-03 14:44:37 +03001909 ret = toshiba_kbd_illum_status_set(toshiba, time);
1910 if (ret)
1911 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001912
Azael Avalos360f0f32014-03-25 20:38:31 -06001913 toshiba->kbd_mode = mode;
1914 }
1915
1916 return count;
1917}
1918
1919static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1920 struct device_attribute *attr,
1921 char *buf)
1922{
1923 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1924 u32 time;
1925
1926 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1927 return -EIO;
1928
Azael Avalos93f8c162014-09-12 18:50:36 -06001929 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1930}
1931
1932static ssize_t toshiba_kbd_type_show(struct device *dev,
1933 struct device_attribute *attr,
1934 char *buf)
1935{
1936 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1937
1938 return sprintf(buf, "%d\n", toshiba->kbd_type);
1939}
1940
1941static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1942 struct device_attribute *attr,
1943 char *buf)
1944{
1945 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1946
1947 if (toshiba->kbd_type == 1)
1948 return sprintf(buf, "%x %x\n",
1949 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1950
1951 return sprintf(buf, "%x %x %x\n",
1952 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001953}
1954
1955static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1956 struct device_attribute *attr,
1957 const char *buf, size_t count)
1958{
1959 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001960 int time;
1961 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001962
Azael Avaloseabde0f2014-10-04 12:02:21 -06001963 ret = kstrtoint(buf, 0, &time);
1964 if (ret)
1965 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001966
Azael Avaloseabde0f2014-10-04 12:02:21 -06001967 /* Check for supported values depending on kbd_type */
1968 if (toshiba->kbd_type == 1) {
1969 if (time < 0 || time > 60)
1970 return -EINVAL;
1971 } else if (toshiba->kbd_type == 2) {
1972 if (time < 1 || time > 60)
1973 return -EINVAL;
1974 }
1975
1976 /* Set the Keyboard Backlight Timeout */
1977
1978 /* Only make a change if the actual timeout has changed */
1979 if (toshiba->kbd_time != time) {
1980 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001981 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001982 /* OR the "base time" to the actual method format */
1983 if (toshiba->kbd_type == 1)
1984 time |= SCI_KBD_MODE_FNZ;
1985 else if (toshiba->kbd_type == 2)
1986 time |= SCI_KBD_MODE_AUTO;
1987
1988 ret = toshiba_kbd_illum_status_set(toshiba, time);
1989 if (ret)
1990 return ret;
1991
Azael Avalos360f0f32014-03-25 20:38:31 -06001992 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1993 }
1994
1995 return count;
1996}
1997
1998static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1999 struct device_attribute *attr,
2000 char *buf)
2001{
2002 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2003 u32 time;
2004
2005 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
2006 return -EIO;
2007
2008 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
2009}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002010
Azael Avalos9d8658a2014-03-25 20:38:32 -06002011static ssize_t toshiba_touchpad_store(struct device *dev,
2012 struct device_attribute *attr,
2013 const char *buf, size_t count)
2014{
2015 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2016 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06002017 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002018
2019 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06002020 ret = kstrtoint(buf, 0, &state);
2021 if (ret)
2022 return ret;
2023 if (state != 0 && state != 1)
2024 return -EINVAL;
2025
2026 ret = toshiba_touchpad_set(toshiba, state);
2027 if (ret)
2028 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002029
2030 return count;
2031}
2032
2033static ssize_t toshiba_touchpad_show(struct device *dev,
2034 struct device_attribute *attr, char *buf)
2035{
2036 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2037 u32 state;
2038 int ret;
2039
2040 ret = toshiba_touchpad_get(toshiba, &state);
2041 if (ret < 0)
2042 return ret;
2043
2044 return sprintf(buf, "%i\n", state);
2045}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002046
Azael Avalos5a2813e2014-03-25 20:38:34 -06002047static ssize_t toshiba_position_show(struct device *dev,
2048 struct device_attribute *attr, char *buf)
2049{
2050 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2051 u32 xyval, zval, tmp;
2052 u16 x, y, z;
2053 int ret;
2054
2055 xyval = zval = 0;
2056 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
2057 if (ret < 0)
2058 return ret;
2059
2060 x = xyval & HCI_ACCEL_MASK;
2061 tmp = xyval >> HCI_MISC_SHIFT;
2062 y = tmp & HCI_ACCEL_MASK;
2063 z = zval & HCI_ACCEL_MASK;
2064
2065 return sprintf(buf, "%d %d %d\n", x, y, z);
2066}
Azael Avalos360f0f32014-03-25 20:38:31 -06002067
Azael Avalose26ffe52015-01-18 18:30:22 -07002068static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
2069 struct device_attribute *attr,
2070 char *buf)
2071{
2072 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2073 u32 mode;
2074 int ret;
2075
2076 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2077 if (ret < 0)
2078 return ret;
2079
2080 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2081}
2082
2083static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
2084 struct device_attribute *attr,
2085 const char *buf, size_t count)
2086{
2087 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2088 u32 mode;
2089 int state;
2090 int ret;
2091
2092 ret = kstrtoint(buf, 0, &state);
2093 if (ret)
2094 return ret;
2095 /* Check for supported values, where:
2096 * 0 - Disabled
2097 * 1 - Alternate (Non USB conformant devices that require more power)
2098 * 2 - Auto (USB conformant devices)
2099 */
2100 if (state != 0 && state != 1 && state != 2)
2101 return -EINVAL;
2102
2103 /* Set the USB charging mode to internal value */
2104 if (state == 0)
2105 mode = SCI_USB_CHARGE_DISABLED;
2106 else if (state == 1)
2107 mode = SCI_USB_CHARGE_ALTERNATE;
2108 else if (state == 2)
2109 mode = SCI_USB_CHARGE_AUTO;
2110
2111 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2112 if (ret)
2113 return ret;
2114
2115 return count;
2116}
2117
Azael Avalos182bcaa2015-01-18 18:30:23 -07002118static ssize_t sleep_functions_on_battery_show(struct device *dev,
2119 struct device_attribute *attr,
2120 char *buf)
2121{
2122 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2123 u32 state;
2124 int bat_lvl;
2125 int status;
2126 int ret;
2127 int tmp;
2128
2129 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2130 if (ret < 0)
2131 return ret;
2132
2133 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2134 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2135 status = (tmp == 0x4) ? 1 : 0;
2136 /* Determine the battery level set */
2137 bat_lvl = state >> HCI_MISC_SHIFT;
2138
2139 return sprintf(buf, "%d %d\n", status, bat_lvl);
2140}
2141
2142static ssize_t sleep_functions_on_battery_store(struct device *dev,
2143 struct device_attribute *attr,
2144 const char *buf, size_t count)
2145{
2146 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2147 u32 status;
2148 int value;
2149 int ret;
2150 int tmp;
2151
2152 ret = kstrtoint(buf, 0, &value);
2153 if (ret)
2154 return ret;
2155
2156 /* Set the status of the function:
2157 * 0 - Disabled
2158 * 1-100 - Enabled
2159 */
2160 if (value < 0 || value > 100)
2161 return -EINVAL;
2162
2163 if (value == 0) {
2164 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2165 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2166 } else {
2167 tmp = value << HCI_MISC_SHIFT;
2168 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2169 }
2170 ret = toshiba_sleep_functions_status_set(toshiba, status);
2171 if (ret < 0)
2172 return ret;
2173
2174 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2175
2176 return count;
2177}
2178
Azael Avalosbb3fe012015-01-18 18:30:24 -07002179static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
2180 struct device_attribute *attr,
2181 char *buf)
2182{
2183 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2184 u32 state;
2185 int ret;
2186
2187 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2188 if (ret < 0)
2189 return ret;
2190
2191 return sprintf(buf, "%d\n", state);
2192}
2193
2194static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
2195 struct device_attribute *attr,
2196 const char *buf, size_t count)
2197{
2198 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2199 int state;
2200 int ret;
2201
2202 ret = kstrtoint(buf, 0, &state);
2203 if (ret)
2204 return ret;
2205 if (state != 0 && state != 1)
2206 return -EINVAL;
2207
2208 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2209 if (ret)
2210 return ret;
2211
2212 return count;
2213}
2214
Azael Avalos172ce0a2015-01-18 18:30:25 -07002215static ssize_t toshiba_usb_sleep_music_show(struct device *dev,
2216 struct device_attribute *attr,
2217 char *buf)
2218{
2219 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2220 u32 state;
2221 int ret;
2222
2223 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2224 if (ret < 0)
2225 return ret;
2226
2227 return sprintf(buf, "%d\n", state);
2228}
2229
2230static ssize_t toshiba_usb_sleep_music_store(struct device *dev,
2231 struct device_attribute *attr,
2232 const char *buf, size_t count)
2233{
2234 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2235 int state;
2236 int ret;
2237
2238 ret = kstrtoint(buf, 0, &state);
2239 if (ret)
2240 return ret;
2241 if (state != 0 && state != 1)
2242 return -EINVAL;
2243
2244 ret = toshiba_usb_sleep_music_set(toshiba, state);
2245 if (ret)
2246 return ret;
2247
2248 return count;
2249}
2250
Azael Avalosbae84192015-02-10 21:09:18 -07002251static ssize_t toshiba_kbd_function_keys_show(struct device *dev,
2252 struct device_attribute *attr,
2253 char *buf)
2254{
2255 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2256 int mode;
2257 int ret;
2258
2259 ret = toshiba_function_keys_get(toshiba, &mode);
2260 if (ret < 0)
2261 return ret;
2262
2263 return sprintf(buf, "%d\n", mode);
2264}
2265
2266static ssize_t toshiba_kbd_function_keys_store(struct device *dev,
2267 struct device_attribute *attr,
2268 const char *buf, size_t count)
2269{
2270 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2271 int mode;
2272 int ret;
2273
2274 ret = kstrtoint(buf, 0, &mode);
2275 if (ret)
2276 return ret;
2277 /* Check for the function keys mode where:
2278 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2279 * 1 - Special functions (Opposite of the above setting)
2280 */
2281 if (mode != 0 && mode != 1)
2282 return -EINVAL;
2283
2284 ret = toshiba_function_keys_set(toshiba, mode);
2285 if (ret)
2286 return ret;
2287
2288 pr_info("Reboot for changes to KBD Function Keys to take effect");
2289
2290 return count;
2291}
2292
Azael Avalos35d53ce2015-02-10 21:09:19 -07002293static ssize_t toshiba_panel_power_on_show(struct device *dev,
2294 struct device_attribute *attr,
2295 char *buf)
2296{
2297 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2298 u32 state;
2299 int ret;
2300
2301 ret = toshiba_panel_power_on_get(toshiba, &state);
2302 if (ret < 0)
2303 return ret;
2304
2305 return sprintf(buf, "%d\n", state);
2306}
2307
2308static ssize_t toshiba_panel_power_on_store(struct device *dev,
2309 struct device_attribute *attr,
2310 const char *buf, size_t count)
2311{
2312 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2313 int state;
2314 int ret;
2315
2316 ret = kstrtoint(buf, 0, &state);
2317 if (ret)
2318 return ret;
2319 if (state != 0 && state != 1)
2320 return -EINVAL;
2321
2322 ret = toshiba_panel_power_on_set(toshiba, state);
2323 if (ret)
2324 return ret;
2325
2326 pr_info("Reboot for changes to Panel Power ON to take effect");
2327
2328 return count;
2329}
2330
Azael Avalos17fe4b32015-02-10 21:09:20 -07002331static ssize_t toshiba_usb_three_show(struct device *dev,
2332 struct device_attribute *attr,
2333 char *buf)
2334{
2335 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2336 u32 state;
2337 int ret;
2338
2339 ret = toshiba_usb_three_get(toshiba, &state);
2340 if (ret < 0)
2341 return ret;
2342
2343 return sprintf(buf, "%d\n", state);
2344}
2345
2346static ssize_t toshiba_usb_three_store(struct device *dev,
2347 struct device_attribute *attr,
2348 const char *buf, size_t count)
2349{
2350 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2351 int state;
2352 int ret;
2353
2354 ret = kstrtoint(buf, 0, &state);
2355 if (ret)
2356 return ret;
2357 /* Check for USB 3 mode where:
2358 * 0 - Disabled (Acts like a USB 2 port, saving power)
2359 * 1 - Enabled
2360 */
2361 if (state != 0 && state != 1)
2362 return -EINVAL;
2363
2364 ret = toshiba_usb_three_set(toshiba, state);
2365 if (ret)
2366 return ret;
2367
2368 pr_info("Reboot for changes to USB 3 to take effect");
2369
2370 return count;
2371}
2372
Azael Avalos360f0f32014-03-25 20:38:31 -06002373static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2374 struct attribute *attr, int idx)
2375{
2376 struct device *dev = container_of(kobj, struct device, kobj);
2377 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2378 bool exists = true;
2379
Azael Avalos94477d42015-02-10 21:09:17 -07002380 if (attr == &dev_attr_fan.attr)
2381 exists = (drv->fan_supported) ? true : false;
2382 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002383 exists = (drv->kbd_illum_supported) ? true : false;
2384 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2385 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002386 else if (attr == &dev_attr_touchpad.attr)
2387 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002388 else if (attr == &dev_attr_position.attr)
2389 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002390 else if (attr == &dev_attr_usb_sleep_charge.attr)
2391 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002392 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2393 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002394 else if (attr == &dev_attr_usb_rapid_charge.attr)
2395 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002396 else if (attr == &dev_attr_usb_sleep_music.attr)
2397 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002398 else if (attr == &dev_attr_kbd_function_keys.attr)
2399 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002400 else if (attr == &dev_attr_panel_power_on.attr)
2401 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002402 else if (attr == &dev_attr_usb_three.attr)
2403 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002404
2405 return exists ? attr->mode : 0;
2406}
2407
Azael Avalos1f28f292014-12-04 20:22:45 -07002408/*
2409 * Hotkeys
2410 */
2411static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2412{
2413 acpi_status status;
2414 u32 result;
2415
2416 status = acpi_evaluate_object(dev->acpi_dev->handle,
2417 "ENAB", NULL, NULL);
2418 if (ACPI_FAILURE(status))
2419 return -ENODEV;
2420
2421 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2422 if (result == TOS_FAILURE)
2423 return -EIO;
2424 else if (result == TOS_NOT_SUPPORTED)
2425 return -ENODEV;
2426
2427 return 0;
2428}
2429
Seth Forshee29cd2932012-01-18 13:44:09 -06002430static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2431 struct serio *port)
2432{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002433 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002434 return false;
2435
2436 if (unlikely(data == 0xe0))
2437 return false;
2438
2439 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2440 schedule_work(&toshiba_acpi->hotkey_work);
2441 return true;
2442 }
2443
2444 return false;
2445}
2446
2447static void toshiba_acpi_hotkey_work(struct work_struct *work)
2448{
2449 acpi_handle ec_handle = ec_get_handle();
2450 acpi_status status;
2451
2452 if (!ec_handle)
2453 return;
2454
2455 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2456 if (ACPI_FAILURE(status))
2457 pr_err("ACPI NTFY method execution failed\n");
2458}
2459
2460/*
2461 * Returns hotkey scancode, or < 0 on failure.
2462 */
2463static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2464{
Zhang Rui74facaf2013-09-03 08:32:15 +08002465 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002466 acpi_status status;
2467
Zhang Rui74facaf2013-09-03 08:32:15 +08002468 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2469 NULL, &value);
2470 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002471 pr_err("ACPI INFO method execution failed\n");
2472 return -EIO;
2473 }
2474
Zhang Rui74facaf2013-09-03 08:32:15 +08002475 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002476}
2477
2478static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2479 int scancode)
2480{
2481 if (scancode == 0x100)
2482 return;
2483
2484 /* act on key press; ignore key release */
2485 if (scancode & 0x80)
2486 return;
2487
2488 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2489 pr_info("Unknown key %x\n", scancode);
2490}
2491
Azael Avalos71454d72014-12-04 20:22:46 -07002492static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2493{
2494 u32 hci_result, value;
2495 int retries = 3;
2496 int scancode;
2497
2498 if (dev->info_supported) {
2499 scancode = toshiba_acpi_query_hotkey(dev);
2500 if (scancode < 0)
2501 pr_err("Failed to query hotkey event\n");
2502 else if (scancode != 0)
2503 toshiba_acpi_report_hotkey(dev, scancode);
2504 } else if (dev->system_event_supported) {
2505 do {
2506 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2507 switch (hci_result) {
2508 case TOS_SUCCESS:
2509 toshiba_acpi_report_hotkey(dev, (int)value);
2510 break;
2511 case TOS_NOT_SUPPORTED:
2512 /*
2513 * This is a workaround for an unresolved
2514 * issue on some machines where system events
2515 * sporadically become disabled.
2516 */
2517 hci_result =
2518 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2519 pr_notice("Re-enabled hotkeys\n");
2520 /* fall through */
2521 default:
2522 retries--;
2523 break;
2524 }
2525 } while (retries && hci_result != TOS_FIFO_EMPTY);
2526 }
2527}
2528
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002529static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002530{
Zhang Ruie2e19602013-09-03 08:32:06 +08002531 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05002532 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002533 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002534 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05002535
Seth Forshee135740d2011-09-20 16:55:49 -05002536 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002537 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002538 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002539
2540 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002541 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002542 dev->hotkey_dev->id.bustype = BUS_HOST;
2543
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002544 if (dmi_check_system(toshiba_alt_keymap_dmi))
2545 keymap = toshiba_acpi_alt_keymap;
2546 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002547 if (error)
2548 goto err_free_dev;
2549
Seth Forshee29cd2932012-01-18 13:44:09 -06002550 /*
2551 * For some machines the SCI responsible for providing hotkey
2552 * notification doesn't fire. We can trigger the notification
2553 * whenever the Fn key is pressed using the NTFY method, if
2554 * supported, so if it's present set up an i8042 key filter
2555 * for this purpose.
2556 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002557 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002558 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002559 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2560
2561 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2562 if (error) {
2563 pr_err("Error installing key filter\n");
2564 goto err_free_keymap;
2565 }
2566
2567 dev->ntfy_supported = 1;
2568 }
2569
2570 /*
2571 * Determine hotkey query interface. Prefer using the INFO
2572 * method when it is available.
2573 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002574 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002575 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002576 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002577 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002578 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002579 dev->system_event_supported = 1;
2580 }
2581
2582 if (!dev->info_supported && !dev->system_event_supported) {
2583 pr_warn("No hotkey query interface found\n");
2584 goto err_remove_filter;
2585 }
2586
Azael Avalos1f28f292014-12-04 20:22:45 -07002587 error = toshiba_acpi_enable_hotkeys(dev);
2588 if (error) {
Seth Forshee135740d2011-09-20 16:55:49 -05002589 pr_info("Unable to enable hotkeys\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002590 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002591 }
2592
2593 error = input_register_device(dev->hotkey_dev);
2594 if (error) {
2595 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002596 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002597 }
2598
2599 return 0;
2600
Seth Forshee29cd2932012-01-18 13:44:09 -06002601 err_remove_filter:
2602 if (dev->ntfy_supported)
2603 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002604 err_free_keymap:
2605 sparse_keymap_free(dev->hotkey_dev);
2606 err_free_dev:
2607 input_free_device(dev->hotkey_dev);
2608 dev->hotkey_dev = NULL;
2609 return error;
2610}
2611
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002612static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002613{
2614 struct backlight_properties props;
2615 int brightness;
2616 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002617 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002618
2619 /*
2620 * Some machines don't support the backlight methods at all, and
2621 * others support it read-only. Either of these is pretty useless,
2622 * so only register the backlight device if the backlight method
2623 * supports both reads and writes.
2624 */
2625 brightness = __get_lcd_brightness(dev);
2626 if (brightness < 0)
2627 return 0;
2628 ret = set_lcd_brightness(dev, brightness);
2629 if (ret) {
2630 pr_debug("Backlight method is read-only, disabling backlight support\n");
2631 return 0;
2632 }
2633
Akio Idehara121b7b02012-04-06 01:46:43 +09002634 /* Determine whether or not BIOS supports transflective backlight */
2635 ret = get_tr_backlight_status(dev, &enabled);
2636 dev->tr_backlight_supported = !ret;
2637
Matthew Garrett53039f22012-06-01 11:02:36 -04002638 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002639 props.type = BACKLIGHT_PLATFORM;
2640 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002641
Akio Idehara121b7b02012-04-06 01:46:43 +09002642 /* adding an extra level and having 0 change to transflective mode */
2643 if (dev->tr_backlight_supported)
2644 props.max_brightness++;
2645
Seth Forshee62cce752012-04-19 11:23:50 -05002646 dev->backlight_dev = backlight_device_register("toshiba",
2647 &dev->acpi_dev->dev,
2648 dev,
2649 &toshiba_backlight_data,
2650 &props);
2651 if (IS_ERR(dev->backlight_dev)) {
2652 ret = PTR_ERR(dev->backlight_dev);
2653 pr_err("Could not register toshiba backlight device\n");
2654 dev->backlight_dev = NULL;
2655 return ret;
2656 }
2657
2658 dev->backlight_dev->props.brightness = brightness;
2659 return 0;
2660}
2661
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002662static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002663{
2664 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2665
Seth Forshee36d03f92011-09-20 16:55:53 -05002666 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002667
Azael Avalos360f0f32014-03-25 20:38:31 -06002668 if (dev->sysfs_created)
2669 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2670 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002671
Seth Forshee29cd2932012-01-18 13:44:09 -06002672 if (dev->ntfy_supported) {
2673 i8042_remove_filter(toshiba_acpi_i8042_filter);
2674 cancel_work_sync(&dev->hotkey_work);
2675 }
2676
Seth Forshee135740d2011-09-20 16:55:49 -05002677 if (dev->hotkey_dev) {
2678 input_unregister_device(dev->hotkey_dev);
2679 sparse_keymap_free(dev->hotkey_dev);
2680 }
2681
2682 if (dev->bt_rfk) {
2683 rfkill_unregister(dev->bt_rfk);
2684 rfkill_destroy(dev->bt_rfk);
2685 }
2686
Markus Elfring00981812014-11-24 20:30:29 +01002687 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002688
Seth Forshee36d03f92011-09-20 16:55:53 -05002689 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002690 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002691
Azael Avalos360f0f32014-03-25 20:38:31 -06002692 if (dev->kbd_led_registered)
2693 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002694
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002695 if (dev->eco_supported)
2696 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002697
Seth Forshee29cd2932012-01-18 13:44:09 -06002698 if (toshiba_acpi)
2699 toshiba_acpi = NULL;
2700
Seth Forshee135740d2011-09-20 16:55:49 -05002701 kfree(dev);
2702
2703 return 0;
2704}
2705
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002706static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002707{
Zhang Ruie2e19602013-09-03 08:32:06 +08002708 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002709 return "GHCI";
2710
Zhang Ruie2e19602013-09-03 08:32:06 +08002711 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002712 return "SPFC";
2713
2714 return NULL;
2715}
2716
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002717static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002718{
2719 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002720 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002721 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002722 bool bt_present;
2723 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002724
Seth Forshee29cd2932012-01-18 13:44:09 -06002725 if (toshiba_acpi)
2726 return -EBUSY;
2727
Seth Forshee135740d2011-09-20 16:55:49 -05002728 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2729 TOSHIBA_ACPI_VERSION);
2730
Seth Forsheea540d6b2011-09-20 16:55:52 -05002731 hci_method = find_hci_method(acpi_dev->handle);
2732 if (!hci_method) {
2733 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002734 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002735 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002736
Seth Forshee135740d2011-09-20 16:55:49 -05002737 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2738 if (!dev)
2739 return -ENOMEM;
2740 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002741 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002742 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002743 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002744
Seth Forshee6e02cc72011-09-20 16:55:51 -05002745 if (toshiba_acpi_setup_keyboard(dev))
2746 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002747
2748 mutex_init(&dev->mutex);
2749
Seth Forshee62cce752012-04-19 11:23:50 -05002750 ret = toshiba_acpi_setup_backlight(dev);
2751 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002752 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002753
2754 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002755 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002756 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2757 &acpi_dev->dev,
2758 RFKILL_TYPE_BLUETOOTH,
2759 &toshiba_rfk_ops,
2760 dev);
2761 if (!dev->bt_rfk) {
2762 pr_err("unable to allocate rfkill device\n");
2763 ret = -ENOMEM;
2764 goto error;
2765 }
2766
2767 ret = rfkill_register(dev->bt_rfk);
2768 if (ret) {
2769 pr_err("unable to register rfkill device\n");
2770 rfkill_destroy(dev->bt_rfk);
2771 goto error;
2772 }
2773 }
2774
2775 if (toshiba_illumination_available(dev)) {
2776 dev->led_dev.name = "toshiba::illumination";
2777 dev->led_dev.max_brightness = 1;
2778 dev->led_dev.brightness_set = toshiba_illumination_set;
2779 dev->led_dev.brightness_get = toshiba_illumination_get;
2780 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002781 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002782 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002783
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002784 if (toshiba_eco_mode_available(dev)) {
2785 dev->eco_led.name = "toshiba::eco_mode";
2786 dev->eco_led.max_brightness = 1;
2787 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2788 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2789 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2790 dev->eco_supported = 1;
2791 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002792
Azael Avalos93f8c162014-09-12 18:50:36 -06002793 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002794 /*
2795 * Only register the LED if KBD illumination is supported
2796 * and the keyboard backlight operation mode is set to FN-Z
2797 */
2798 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2799 dev->kbd_led.name = "toshiba::kbd_backlight";
2800 dev->kbd_led.max_brightness = 1;
2801 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2802 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2803 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2804 dev->kbd_led_registered = 1;
2805 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002806
Azael Avalos9d8658a2014-03-25 20:38:32 -06002807 ret = toshiba_touchpad_get(dev, &dummy);
2808 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002809
Azael Avalos5a2813e2014-03-25 20:38:34 -06002810 ret = toshiba_accelerometer_supported(dev);
2811 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002812
Azael Avalose26ffe52015-01-18 18:30:22 -07002813 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2814 dev->usb_sleep_charge_supported = !ret;
2815
Azael Avalosbb3fe012015-01-18 18:30:24 -07002816 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2817 dev->usb_rapid_charge_supported = !ret;
2818
Azael Avalos172ce0a2015-01-18 18:30:25 -07002819 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2820 dev->usb_sleep_music_supported = !ret;
2821
Azael Avalosbae84192015-02-10 21:09:18 -07002822 ret = toshiba_function_keys_get(dev, &dummy);
2823 dev->kbd_function_keys_supported = !ret;
2824
Azael Avalos35d53ce2015-02-10 21:09:19 -07002825 ret = toshiba_panel_power_on_get(dev, &dummy);
2826 dev->panel_power_on_supported = !ret;
2827
Azael Avalos17fe4b32015-02-10 21:09:20 -07002828 ret = toshiba_usb_three_get(dev, &dummy);
2829 dev->usb_three_supported = !ret;
2830
Seth Forshee36d03f92011-09-20 16:55:53 -05002831 /* Determine whether or not BIOS supports fan and video interfaces */
2832
2833 ret = get_video_status(dev, &dummy);
2834 dev->video_supported = !ret;
2835
2836 ret = get_fan_status(dev, &dummy);
2837 dev->fan_supported = !ret;
2838
Azael Avalos360f0f32014-03-25 20:38:31 -06002839 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2840 &toshiba_attr_group);
2841 if (ret) {
2842 dev->sysfs_created = 0;
2843 goto error;
2844 }
2845 dev->sysfs_created = !ret;
2846
Seth Forshee36d03f92011-09-20 16:55:53 -05002847 create_toshiba_proc_entries(dev);
2848
Seth Forshee29cd2932012-01-18 13:44:09 -06002849 toshiba_acpi = dev;
2850
Seth Forshee135740d2011-09-20 16:55:49 -05002851 return 0;
2852
2853error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002854 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002855 return ret;
2856}
2857
2858static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2859{
2860 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002861 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002862
Azael Avalos71454d72014-12-04 20:22:46 -07002863 switch (event) {
2864 case 0x80: /* Hotkeys and some system events */
2865 toshiba_acpi_process_hotkeys(dev);
2866 break;
Azael Avalos80546902014-12-04 20:22:47 -07002867 case 0x92: /* Keyboard backlight mode changed */
2868 /* Update sysfs entries */
2869 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2870 &toshiba_attr_group);
2871 if (ret)
2872 pr_err("Unable to update sysfs entries\n");
2873 break;
Azael Avalos71454d72014-12-04 20:22:46 -07002874 case 0x81: /* Unknown */
2875 case 0x82: /* Unknown */
2876 case 0x83: /* Unknown */
2877 case 0x8c: /* Unknown */
2878 case 0x8e: /* Unknown */
2879 case 0x8f: /* Unknown */
2880 case 0x90: /* Unknown */
2881 default:
2882 pr_info("Unknown event received %x\n", event);
2883 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002884 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002885}
2886
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002887#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002888static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002889{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002890 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002891 u32 result;
2892
2893 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002894 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002895
2896 return 0;
2897}
2898
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002899static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002900{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002901 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002902 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002903
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002904 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002905 error = toshiba_acpi_enable_hotkeys(dev);
2906 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002907 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002908 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002909
2910 return 0;
2911}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002912#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002913
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002914static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2915 toshiba_acpi_suspend, toshiba_acpi_resume);
2916
Seth Forshee135740d2011-09-20 16:55:49 -05002917static struct acpi_driver toshiba_acpi_driver = {
2918 .name = "Toshiba ACPI driver",
2919 .owner = THIS_MODULE,
2920 .ids = toshiba_device_ids,
2921 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2922 .ops = {
2923 .add = toshiba_acpi_add,
2924 .remove = toshiba_acpi_remove,
2925 .notify = toshiba_acpi_notify,
2926 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002927 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002928};
Holger Machtc9263552006-10-20 14:30:29 -07002929
Len Brown4be44fc2005-08-05 00:44:28 -04002930static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Seth Forshee135740d2011-09-20 16:55:49 -05002932 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Seth Forsheef11f9992012-01-18 13:44:11 -06002934 /*
2935 * Machines with this WMI guid aren't supported due to bugs in
2936 * their AML. This check relies on wmi initializing before
2937 * toshiba_acpi to guarantee guids have been identified.
2938 */
2939 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2940 return -ENODEV;
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2943 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002944 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002945 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947
Seth Forshee135740d2011-09-20 16:55:49 -05002948 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2949 if (ret) {
2950 pr_err("Failed to register ACPI driver: %d\n", ret);
2951 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002952 }
2953
Seth Forshee135740d2011-09-20 16:55:49 -05002954 return ret;
2955}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002956
Seth Forshee135740d2011-09-20 16:55:49 -05002957static void __exit toshiba_acpi_exit(void)
2958{
2959 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2960 if (toshiba_proc_dir)
2961 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962}
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964module_init(toshiba_acpi_init);
2965module_exit(toshiba_acpi_exit);