blob: 5009d8539c50267c99af244e07eb32e864e3485b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04005 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02006 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07007 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Darren Hartc57c0fa2015-02-11 21:25:22 -080019 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches7e334602011-03-29 15:21:52 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Azael Avalos7216d702015-02-10 21:09:21 -070034#define TOSHIBA_ACPI_VERSION "0.21"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define PROC_INTERFACE_VERSION 1
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080042#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070043#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040044#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050045#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070046#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020047#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060049#include <linux/workqueue.h>
50#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080051#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020052#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_AUTHOR("John Belmonte");
56MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
57MODULE_LICENSE("GPL");
58
Seth Forsheef11f9992012-01-18 13:44:11 -060059#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
60
Seth Forshee29cd2932012-01-18 13:44:09 -060061/* Scan code for Fn key on TOS1900 models */
62#define TOS1900_FN_SCAN 0x6e
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
66
Darren Harte0769fe2015-02-11 20:50:08 -080067/*
68 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060069 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
72 * be uniform across all their models. Ideally we would just call
73 * dedicated ACPI methods instead of using this primitive interface.
74 * However the ACPI methods seem to be incomplete in some areas (for
75 * example they allow setting, but not reading, the LCD brightness value),
76 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040077 *
Azael Avalos84a62732014-03-25 20:38:29 -060078 * SCI stands for "System Configuration Interface" which aim is to
79 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
Azael Avalos258c5902014-09-29 20:40:07 -060082#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* operations */
85#define HCI_SET 0xff00
86#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060087#define SCI_OPEN 0xf100
88#define SCI_CLOSE 0xf200
89#define SCI_GET 0xf300
90#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060093#define TOS_SUCCESS 0x0000
94#define TOS_OPEN_CLOSE_OK 0x0044
95#define TOS_FAILURE 0x1000
96#define TOS_NOT_SUPPORTED 0x8000
97#define TOS_ALREADY_OPEN 0x8100
98#define TOS_NOT_OPENED 0x8200
99#define TOS_INPUT_DATA_ERROR 0x8300
100#define TOS_WRITE_PROTECTED 0x8400
101#define TOS_NOT_PRESENT 0x8600
102#define TOS_FIFO_EMPTY 0x8c00
103#define TOS_DATA_NOT_AVAILABLE 0x8d20
104#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700105#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* registers */
108#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900109#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define HCI_SYSTEM_EVENT 0x0016
111#define HCI_VIDEO_OUT 0x001c
112#define HCI_HOTKEY_EVENT 0x001e
113#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400114#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600115#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600116#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600117#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600118#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos35d53ce2015-02-10 21:09:19 -0700119#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600120#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700121#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600122#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700123#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700124#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600125#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700126#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600129#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600130#define HCI_HOTKEY_DISABLE 0x0b
131#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define HCI_LCD_BRIGHTNESS_BITS 3
133#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
134#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600135#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define HCI_VIDEO_OUT_LCD 0x1
137#define HCI_VIDEO_OUT_CRT 0x2
138#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400139#define HCI_WIRELESS_KILL_SWITCH 0x01
140#define HCI_WIRELESS_BT_PRESENT 0x0f
141#define HCI_WIRELESS_BT_ATTACH 0x40
142#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600143#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600144#define SCI_KBD_MODE_FNZ 0x1
145#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600146#define SCI_KBD_MODE_ON 0x8
147#define SCI_KBD_MODE_OFF 0x10
148#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700149#define SCI_USB_CHARGE_MODE_MASK 0xff
150#define SCI_USB_CHARGE_DISABLED 0x30000
151#define SCI_USB_CHARGE_ALTERNATE 0x30009
152#define SCI_USB_CHARGE_AUTO 0x30021
Azael Avalos182bcaa2015-01-18 18:30:23 -0700153#define SCI_USB_CHARGE_BAT_MASK 0x7
154#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
155#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
156#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700157#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Seth Forshee135740d2011-09-20 16:55:49 -0500159struct toshiba_acpi_dev {
160 struct acpi_device *acpi_dev;
161 const char *method_hci;
162 struct rfkill *bt_rfk;
163 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600164 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500165 struct backlight_device *backlight_dev;
166 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600167 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600168 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500169
Seth Forshee135740d2011-09-20 16:55:49 -0500170 int force_fan;
171 int last_key_event;
172 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600173 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600174 int kbd_mode;
175 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700176 int usbsc_bat_level;
Seth Forshee135740d2011-09-20 16:55:49 -0500177
Dan Carpenter592b7462012-01-15 14:28:20 +0300178 unsigned int illumination_supported:1;
179 unsigned int video_supported:1;
180 unsigned int fan_supported:1;
181 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600182 unsigned int ntfy_supported:1;
183 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900184 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600185 unsigned int kbd_illum_supported:1;
186 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600187 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600188 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600189 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700190 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700191 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700192 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700193 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700194 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700195 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600196 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500197
Seth Forshee135740d2011-09-20 16:55:49 -0500198 struct mutex mutex;
199};
200
Seth Forshee29cd2932012-01-18 13:44:09 -0600201static struct toshiba_acpi_dev *toshiba_acpi;
202
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800203static const struct acpi_device_id toshiba_device_ids[] = {
204 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100205 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400206 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800207 {"TOS1900", 0},
208 {"", 0},
209};
210MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
211
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800212static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100213 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700214 { KE_KEY, 0x101, { KEY_MUTE } },
215 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
216 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600217 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600218 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
219 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700220 { KE_KEY, 0x13b, { KEY_COFFEE } },
221 { KE_KEY, 0x13c, { KEY_BATTERY } },
222 { KE_KEY, 0x13d, { KEY_SLEEP } },
223 { KE_KEY, 0x13e, { KEY_SUSPEND } },
224 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
225 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
226 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
227 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600228 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100229 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700230 { KE_KEY, 0xb05, { KEY_PROG2 } },
231 { KE_KEY, 0xb06, { KEY_WWW } },
232 { KE_KEY, 0xb07, { KEY_MAIL } },
233 { KE_KEY, 0xb30, { KEY_STOP } },
234 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
235 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
236 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
237 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600238 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
239 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
240 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
241 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
242 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700243 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500244};
245
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200246/* alternative keymap */
247static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
248 {
249 .matches = {
250 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
251 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
252 },
253 },
Azael Avalose6efad72014-08-04 09:21:02 -0600254 {
255 .matches = {
256 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
257 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
258 },
259 },
Aaron Lub1bde682014-10-23 16:18:02 +0800260 {
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
263 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
264 },
265 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200266 {}
267};
268
269static const struct key_entry toshiba_acpi_alt_keymap[] = {
270 { KE_KEY, 0x157, { KEY_MUTE } },
271 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
272 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600273 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200274 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
275 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
276 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
277 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
278 { KE_KEY, 0x158, { KEY_WLAN } },
279 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
280 { KE_END, 0 },
281};
282
Darren Harte0769fe2015-02-11 20:50:08 -0800283/*
284 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286
Azael Avalosb5163992015-02-10 23:43:56 -0700287static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 *word = (*word & ~mask) | (mask * value);
290}
291
Darren Harte0769fe2015-02-11 20:50:08 -0800292/*
293 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295
Len Brown4be44fc2005-08-05 00:44:28 -0400296static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 acpi_status status;
299
Zhang Rui619400d2013-09-03 08:31:56 +0800300 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500301 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Darren Harte0769fe2015-02-11 20:50:08 -0800304/*
305 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600306 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Azael Avalos258c5902014-09-29 20:40:07 -0600308static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
309 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600312 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600314 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 acpi_status status;
316 int i;
317
Azael Avalos258c5902014-09-29 20:40:07 -0600318 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600320 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 in_objs[i].type = ACPI_TYPE_INTEGER;
322 in_objs[i].integer.value = in[i];
323 }
324
325 results.length = sizeof(out_objs);
326 results.pointer = out_objs;
327
Seth Forshee6e02cc72011-09-20 16:55:51 -0500328 status = acpi_evaluate_object(dev->acpi_dev->handle,
329 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400330 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600331 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700332 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 return status;
337}
338
Darren Harte0769fe2015-02-11 20:50:08 -0800339/*
340 * Common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
342 * In addition to the ACPI status, the HCI system returns a result which
343 * may be useful (such as "not supported").
344 */
345
Azael Avalos893f3f62014-09-29 20:40:09 -0600346static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Azael Avalos258c5902014-09-29 20:40:07 -0600348 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
349 u32 out[TCI_WORDS];
350 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600351
352 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Azael Avalos893f3f62014-09-29 20:40:09 -0600355static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Azael Avalos258c5902014-09-29 20:40:07 -0600357 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
358 u32 out[TCI_WORDS];
359 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700360
Azael Avalos893f3f62014-09-29 20:40:09 -0600361 if (ACPI_FAILURE(status))
362 return TOS_FAILURE;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600365
366 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Azael Avalos893f3f62014-09-29 20:40:09 -0600369static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400370{
Azael Avalos258c5902014-09-29 20:40:07 -0600371 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
372 u32 out[TCI_WORDS];
373 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600374
375 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400376}
377
Azael Avalosb5163992015-02-10 23:43:56 -0700378static u32 hci_read2(struct toshiba_acpi_dev *dev,
379 u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400380{
Azael Avalos258c5902014-09-29 20:40:07 -0600381 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
382 u32 out[TCI_WORDS];
383 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700384
Azael Avalos893f3f62014-09-29 20:40:09 -0600385 if (ACPI_FAILURE(status))
386 return TOS_FAILURE;
387
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400388 *out1 = out[2];
389 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600390
391 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400392}
393
Darren Harte0769fe2015-02-11 20:50:08 -0800394/*
395 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600396 */
397
398static int sci_open(struct toshiba_acpi_dev *dev)
399{
Azael Avalos258c5902014-09-29 20:40:07 -0600400 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
401 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600402 acpi_status status;
403
Azael Avalos258c5902014-09-29 20:40:07 -0600404 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600405 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600406 pr_err("ACPI call to open SCI failed\n");
407 return 0;
408 }
409
Azael Avalos1864bbc2014-09-29 20:40:08 -0600410 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600411 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600412 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600413 pr_info("Toshiba SCI already opened\n");
414 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700415 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800416 /*
417 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700418 * 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
Darren Harte0769fe2015-02-11 20:50:08 -0800570 /*
571 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600572 * previous kbd backlight implementation set this to
573 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800574 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600575 */
576 if (out[3] == SCI_KBD_TIME_MAX)
577 dev->kbd_type = 2;
578 else
579 dev->kbd_type = 1;
580 /* Get the current keyboard backlight mode */
581 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
582 /* Get the current time (1-60 seconds) */
583 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
584
585 return 1;
586}
587
Azael Avalos360f0f32014-03-25 20:38:31 -0600588static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
589{
590 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600591
592 if (!sci_open(dev))
593 return -EIO;
594
Azael Avalos893f3f62014-09-29 20:40:09 -0600595 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600596 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600597 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600598 pr_err("ACPI call to set KBD backlight status failed\n");
599 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600600 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600601 pr_info("Keyboard backlight status not supported\n");
602 return -ENODEV;
603 }
604
605 return 0;
606}
607
608static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
609{
610 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600611
612 if (!sci_open(dev))
613 return -EIO;
614
Azael Avalos893f3f62014-09-29 20:40:09 -0600615 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600616 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600617 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600618 pr_err("ACPI call to get KBD backlight status failed\n");
619 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600620 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600621 pr_info("Keyboard backlight status not supported\n");
622 return -ENODEV;
623 }
624
625 return 0;
626}
627
628static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
629{
630 struct toshiba_acpi_dev *dev = container_of(cdev,
631 struct toshiba_acpi_dev, kbd_led);
632 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600633
634 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600635 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
636 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600637 pr_err("ACPI call to get the keyboard backlight failed\n");
638 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600639 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600640 pr_info("Keyboard backlight not supported\n");
641 return LED_OFF;
642 }
643
644 return state ? LED_FULL : LED_OFF;
645}
646
647static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
648 enum led_brightness brightness)
649{
650 struct toshiba_acpi_dev *dev = container_of(cdev,
651 struct toshiba_acpi_dev, kbd_led);
652 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600653
654 /* Set the keyboard backlight state */
655 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600656 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
657 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600658 pr_err("ACPI call to set KBD Illumination mode failed\n");
659 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600660 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600661 pr_info("Keyboard backlight not supported\n");
662 return;
663 }
664}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400665
Azael Avalos9d8658a2014-03-25 20:38:32 -0600666/* TouchPad support */
667static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
668{
669 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600670
671 if (!sci_open(dev))
672 return -EIO;
673
Azael Avalos893f3f62014-09-29 20:40:09 -0600674 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600675 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600676 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600677 pr_err("ACPI call to set the touchpad failed\n");
678 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600679 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600680 return -ENODEV;
681 }
682
683 return 0;
684}
685
686static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
687{
688 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600689
690 if (!sci_open(dev))
691 return -EIO;
692
Azael Avalos893f3f62014-09-29 20:40:09 -0600693 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600694 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600695 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600696 pr_err("ACPI call to query the touchpad failed\n");
697 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600698 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600699 return -ENODEV;
700 }
701
702 return 0;
703}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200704
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600705/* Eco Mode support */
706static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
707{
708 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700709 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600710 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600711
Azael Avalos258c5902014-09-29 20:40:07 -0600712 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700713 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
714 pr_err("ACPI call to get ECO led failed\n");
715 } else if (out[0] == TOS_NOT_INSTALLED) {
716 pr_info("ECO led not installed");
717 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800718 /*
719 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700720 * LED device is present, but that we just screwed the input
721 * parameters.
722 *
723 * Let's query the status of the LED to see if we really have a
724 * success response, indicating the actual presense of the LED,
725 * bail out otherwise.
726 */
727 in[3] = 1;
728 status = tci_raw(dev, in, out);
729 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
730 pr_err("ACPI call to get ECO led failed\n");
731 else if (out[0] == TOS_SUCCESS)
732 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600733 }
734
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700735 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600736}
737
Azael Avalosb5163992015-02-10 23:43:56 -0700738static enum led_brightness
739toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600740{
741 struct toshiba_acpi_dev *dev = container_of(cdev,
742 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600743 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
744 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600745 acpi_status status;
746
Azael Avalos258c5902014-09-29 20:40:07 -0600747 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600748 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600749 pr_err("ACPI call to get ECO led failed\n");
750 return LED_OFF;
751 }
752
753 return out[2] ? LED_FULL : LED_OFF;
754}
755
756static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
757 enum led_brightness brightness)
758{
759 struct toshiba_acpi_dev *dev = container_of(cdev,
760 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600761 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
762 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600763 acpi_status status;
764
765 /* Switch the Eco Mode led on/off */
766 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600767 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600768 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600769 pr_err("ACPI call to set ECO led failed\n");
770 return;
771 }
772}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400773
Azael Avalos5a2813e2014-03-25 20:38:34 -0600774/* Accelerometer support */
775static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
776{
Azael Avalos258c5902014-09-29 20:40:07 -0600777 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
778 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600779 acpi_status status;
780
Darren Harte0769fe2015-02-11 20:50:08 -0800781 /*
782 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600783 * this call also serves as initialization
784 */
Azael Avalos258c5902014-09-29 20:40:07 -0600785 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600786 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600787 pr_err("ACPI call to query the accelerometer failed\n");
788 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600789 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
790 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600791 pr_err("Accelerometer not initialized\n");
792 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600793 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600794 pr_info("Accelerometer not supported\n");
795 return -ENODEV;
796 }
797
798 return 0;
799}
800
801static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
802 u32 *xy, u32 *z)
803{
Azael Avalos258c5902014-09-29 20:40:07 -0600804 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
805 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600806 acpi_status status;
807
808 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600809 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600810 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600811 pr_err("ACPI call to query the accelerometer failed\n");
812 return -EIO;
813 }
814
815 *xy = out[2];
816 *z = out[4];
817
818 return 0;
819}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600820
Azael Avalose26ffe52015-01-18 18:30:22 -0700821/* Sleep (Charge and Music) utilities support */
822static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
823 u32 *mode)
824{
825 u32 result;
826
827 if (!sci_open(dev))
828 return -EIO;
829
830 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
831 sci_close(dev);
832 if (result == TOS_FAILURE) {
833 pr_err("ACPI call to set USB S&C mode failed\n");
834 return -EIO;
835 } else if (result == TOS_NOT_SUPPORTED) {
836 pr_info("USB Sleep and Charge not supported\n");
837 return -ENODEV;
838 } else if (result == TOS_INPUT_DATA_ERROR) {
839 return -EIO;
840 }
841
842 return 0;
843}
844
845static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
846 u32 mode)
847{
848 u32 result;
849
850 if (!sci_open(dev))
851 return -EIO;
852
853 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
854 sci_close(dev);
855 if (result == TOS_FAILURE) {
856 pr_err("ACPI call to set USB S&C mode failed\n");
857 return -EIO;
858 } else if (result == TOS_NOT_SUPPORTED) {
859 pr_info("USB Sleep and Charge not supported\n");
860 return -ENODEV;
861 } else if (result == TOS_INPUT_DATA_ERROR) {
862 return -EIO;
863 }
864
865 return 0;
866}
867
Azael Avalos182bcaa2015-01-18 18:30:23 -0700868static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
869 u32 *mode)
870{
871 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
872 u32 out[TCI_WORDS];
873 acpi_status status;
874
875 if (!sci_open(dev))
876 return -EIO;
877
878 in[5] = SCI_USB_CHARGE_BAT_LVL;
879 status = tci_raw(dev, in, out);
880 sci_close(dev);
881 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
882 pr_err("ACPI call to get USB S&C battery level failed\n");
883 return -EIO;
884 } else if (out[0] == TOS_NOT_SUPPORTED) {
885 pr_info("USB Sleep and Charge not supported\n");
886 return -ENODEV;
887 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
888 return -EIO;
889 }
890
891 *mode = out[2];
892
893 return 0;
894}
895
896static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
897 u32 mode)
898{
899 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
900 u32 out[TCI_WORDS];
901 acpi_status status;
902
903 if (!sci_open(dev))
904 return -EIO;
905
906 in[2] = mode;
907 in[5] = SCI_USB_CHARGE_BAT_LVL;
908 status = tci_raw(dev, in, out);
909 sci_close(dev);
910 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
911 pr_err("ACPI call to set USB S&C battery level failed\n");
912 return -EIO;
913 } else if (out[0] == TOS_NOT_SUPPORTED) {
914 pr_info("USB Sleep and Charge not supported\n");
915 return -ENODEV;
916 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
917 return -EIO;
918 }
919
920 return 0;
921}
922
Azael Avalosbb3fe012015-01-18 18:30:24 -0700923static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
924 u32 *state)
925{
926 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
927 u32 out[TCI_WORDS];
928 acpi_status status;
929
930 if (!sci_open(dev))
931 return -EIO;
932
933 in[5] = SCI_USB_CHARGE_RAPID_DSP;
934 status = tci_raw(dev, in, out);
935 sci_close(dev);
936 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
937 pr_err("ACPI call to get USB S&C battery level failed\n");
938 return -EIO;
939 } else if (out[0] == TOS_NOT_SUPPORTED ||
940 out[0] == TOS_INPUT_DATA_ERROR) {
941 pr_info("USB Sleep and Charge not supported\n");
942 return -ENODEV;
943 }
944
945 *state = out[2];
946
947 return 0;
948}
949
950static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
951 u32 state)
952{
953 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
954 u32 out[TCI_WORDS];
955 acpi_status status;
956
957 if (!sci_open(dev))
958 return -EIO;
959
960 in[2] = state;
961 in[5] = SCI_USB_CHARGE_RAPID_DSP;
962 status = tci_raw(dev, in, out);
963 sci_close(dev);
964 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
965 pr_err("ACPI call to set USB S&C battery level failed\n");
966 return -EIO;
967 } else if (out[0] == TOS_NOT_SUPPORTED) {
968 pr_info("USB Sleep and Charge not supported\n");
969 return -ENODEV;
970 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
971 return -EIO;
972 }
973
974 return 0;
975}
976
Azael Avalos172ce0a2015-01-18 18:30:25 -0700977static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
978{
979 u32 result;
980
981 if (!sci_open(dev))
982 return -EIO;
983
984 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
985 sci_close(dev);
986 if (result == TOS_FAILURE) {
987 pr_err("ACPI call to set USB S&C mode failed\n");
988 return -EIO;
989 } else if (result == TOS_NOT_SUPPORTED) {
990 pr_info("USB Sleep and Charge not supported\n");
991 return -ENODEV;
992 } else if (result == TOS_INPUT_DATA_ERROR) {
993 return -EIO;
994 }
995
996 return 0;
997}
998
999static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1000{
1001 u32 result;
1002
1003 if (!sci_open(dev))
1004 return -EIO;
1005
1006 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1007 sci_close(dev);
1008 if (result == TOS_FAILURE) {
1009 pr_err("ACPI call to set USB S&C mode failed\n");
1010 return -EIO;
1011 } else if (result == TOS_NOT_SUPPORTED) {
1012 pr_info("USB Sleep and Charge not supported\n");
1013 return -ENODEV;
1014 } else if (result == TOS_INPUT_DATA_ERROR) {
1015 return -EIO;
1016 }
1017
1018 return 0;
1019}
1020
Azael Avalosbae84192015-02-10 21:09:18 -07001021/* Keyboard function keys */
1022static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1023{
1024 u32 result;
1025
1026 if (!sci_open(dev))
1027 return -EIO;
1028
1029 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1030 sci_close(dev);
1031 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1032 pr_err("ACPI call to get KBD function keys failed\n");
1033 return -EIO;
1034 } else if (result == TOS_NOT_SUPPORTED) {
1035 pr_info("KBD function keys not supported\n");
1036 return -ENODEV;
1037 }
1038
1039 return 0;
1040}
1041
1042static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1043{
1044 u32 result;
1045
1046 if (!sci_open(dev))
1047 return -EIO;
1048
1049 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1050 sci_close(dev);
1051 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1052 pr_err("ACPI call to set KBD function keys failed\n");
1053 return -EIO;
1054 } else if (result == TOS_NOT_SUPPORTED) {
1055 pr_info("KBD function keys not supported\n");
1056 return -ENODEV;
1057 }
1058
1059 return 0;
1060}
1061
Azael Avalos35d53ce2015-02-10 21:09:19 -07001062/* Panel Power ON */
1063static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1064{
1065 u32 result;
1066
1067 if (!sci_open(dev))
1068 return -EIO;
1069
1070 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1071 sci_close(dev);
1072 if (result == TOS_FAILURE) {
1073 pr_err("ACPI call to get Panel Power ON failed\n");
1074 return -EIO;
1075 } else if (result == TOS_NOT_SUPPORTED) {
1076 pr_info("Panel Power on not supported\n");
1077 return -ENODEV;
1078 } else if (result == TOS_INPUT_DATA_ERROR) {
1079 return -EIO;
1080 }
1081
1082 return 0;
1083}
1084
1085static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1086{
1087 u32 result;
1088
1089 if (!sci_open(dev))
1090 return -EIO;
1091
1092 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1093 sci_close(dev);
1094 if (result == TOS_FAILURE) {
1095 pr_err("ACPI call to set Panel Power ON failed\n");
1096 return -EIO;
1097 } else if (result == TOS_NOT_SUPPORTED) {
1098 pr_info("Panel Power ON not supported\n");
1099 return -ENODEV;
1100 } else if (result == TOS_INPUT_DATA_ERROR) {
1101 return -EIO;
1102 }
1103
1104 return 0;
1105}
1106
Azael Avalos17fe4b32015-02-10 21:09:20 -07001107/* USB Three */
1108static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1109{
1110 u32 result;
1111
1112 if (!sci_open(dev))
1113 return -EIO;
1114
1115 result = sci_read(dev, SCI_USB_THREE, state);
1116 sci_close(dev);
1117 if (result == TOS_FAILURE) {
1118 pr_err("ACPI call to get USB 3 failed\n");
1119 return -EIO;
1120 } else if (result == TOS_NOT_SUPPORTED) {
1121 pr_info("USB 3 not supported\n");
1122 return -ENODEV;
1123 } else if (result == TOS_INPUT_DATA_ERROR) {
1124 return -EIO;
1125 }
1126
1127 return 0;
1128}
1129
1130static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1131{
1132 u32 result;
1133
1134 if (!sci_open(dev))
1135 return -EIO;
1136
1137 result = sci_write(dev, SCI_USB_THREE, state);
1138 sci_close(dev);
1139 if (result == TOS_FAILURE) {
1140 pr_err("ACPI call to set USB 3 failed\n");
1141 return -EIO;
1142 } else if (result == TOS_NOT_SUPPORTED) {
1143 pr_info("USB 3 not supported\n");
1144 return -ENODEV;
1145 } else if (result == TOS_INPUT_DATA_ERROR) {
1146 return -EIO;
1147 }
1148
1149 return 0;
1150}
1151
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001152/* Bluetooth rfkill handlers */
1153
Seth Forshee135740d2011-09-20 16:55:49 -05001154static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001155{
1156 u32 hci_result;
1157 u32 value, value2;
1158
1159 value = 0;
1160 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -06001161 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001162 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001163 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1164
1165 return hci_result;
1166}
1167
Seth Forshee135740d2011-09-20 16:55:49 -05001168static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001169{
1170 u32 hci_result;
1171 u32 value, value2;
1172
1173 value = 0;
1174 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -06001175 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001176
1177 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1178 return hci_result;
1179}
1180
Johannes Berg19d337d2009-06-02 13:01:37 +02001181static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001182{
Johannes Berg19d337d2009-06-02 13:01:37 +02001183 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001184 u32 result1, result2;
1185 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +02001186 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001187 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001188
Johannes Berg19d337d2009-06-02 13:01:37 +02001189 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001190
1191 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001192 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001193 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001194 goto out;
1195 }
1196
1197 if (!radio_state) {
1198 err = 0;
1199 goto out;
1200 }
1201
Azael Avalos893f3f62014-09-29 20:40:09 -06001202 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1203 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001204
Azael Avalos1864bbc2014-09-29 20:40:08 -06001205 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001206 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001207 else
1208 err = 0;
1209 out:
1210 mutex_unlock(&dev->mutex);
1211 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001212}
1213
Johannes Berg19d337d2009-06-02 13:01:37 +02001214static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001215{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001216 bool new_rfk_state;
1217 bool value;
1218 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +02001219 struct toshiba_acpi_dev *dev = data;
1220
1221 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001222
Seth Forshee135740d2011-09-20 16:55:49 -05001223 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001224 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001225 /* Can't do anything useful */
1226 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001227 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001228 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001229
1230 new_rfk_state = value;
1231
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001232 mutex_unlock(&dev->mutex);
1233
Johannes Berg19d337d2009-06-02 13:01:37 +02001234 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1235 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001236}
1237
Johannes Berg19d337d2009-06-02 13:01:37 +02001238static const struct rfkill_ops toshiba_rfk_ops = {
1239 .set_block = bt_rfkill_set_block,
1240 .poll = bt_rfkill_poll,
1241};
1242
Akio Idehara121b7b02012-04-06 01:46:43 +09001243static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1244{
1245 u32 hci_result;
1246 u32 status;
1247
Azael Avalos893f3f62014-09-29 20:40:09 -06001248 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001249 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001250 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001251}
1252
1253static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1254{
1255 u32 hci_result;
1256 u32 value = !enable;
1257
Azael Avalos893f3f62014-09-29 20:40:09 -06001258 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001259 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001260}
1261
Azael Avalosb5163992015-02-10 23:43:56 -07001262static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Seth Forshee62cce752012-04-19 11:23:50 -05001264static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 u32 hci_result;
1267 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001268 int brightness = 0;
1269
1270 if (dev->tr_backlight_supported) {
1271 bool enabled;
1272 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001273
Akio Idehara121b7b02012-04-06 01:46:43 +09001274 if (ret)
1275 return ret;
1276 if (enabled)
1277 return 0;
1278 brightness++;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Azael Avalos893f3f62014-09-29 20:40:09 -06001281 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001282 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001283 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001284
1285 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001286}
1287
Seth Forshee62cce752012-04-19 11:23:50 -05001288static int get_lcd_brightness(struct backlight_device *bd)
1289{
1290 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001291
Seth Forshee62cce752012-04-19 11:23:50 -05001292 return __get_lcd_brightness(dev);
1293}
1294
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001295static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001296{
Seth Forshee135740d2011-09-20 16:55:49 -05001297 struct toshiba_acpi_dev *dev = m->private;
1298 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001299 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001300
Seth Forshee135740d2011-09-20 16:55:49 -05001301 if (!dev->backlight_dev)
1302 return -ENODEV;
1303
Akio Idehara121b7b02012-04-06 01:46:43 +09001304 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001305 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001306 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001307 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001308 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001312 pr_err("Error reading LCD brightness\n");
1313 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001314}
1315
1316static int lcd_proc_open(struct inode *inode, struct file *file)
1317{
Al Virod9dda782013-03-31 18:16:14 -04001318 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Seth Forshee62cce752012-04-19 11:23:50 -05001321static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001322{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001323 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001324
Akio Idehara121b7b02012-04-06 01:46:43 +09001325 if (dev->tr_backlight_supported) {
1326 bool enable = !value;
1327 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001328
Akio Idehara121b7b02012-04-06 01:46:43 +09001329 if (ret)
1330 return ret;
1331 if (value)
1332 value--;
1333 }
1334
Azael Avalosa39f46d2014-11-24 19:29:36 -07001335 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1336 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1337 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001338}
1339
1340static int set_lcd_status(struct backlight_device *bd)
1341{
Seth Forshee135740d2011-09-20 16:55:49 -05001342 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001343
Seth Forshee62cce752012-04-19 11:23:50 -05001344 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001345}
1346
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001347static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1348 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Al Virod9dda782013-03-31 18:16:14 -04001350 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001351 char cmd[42];
1352 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001354 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001355 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001357 len = min(count, sizeof(cmd) - 1);
1358 if (copy_from_user(cmd, buf, len))
1359 return -EFAULT;
1360 cmd[len] = '\0';
1361
1362 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001363 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001364 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001365 if (ret == 0)
1366 ret = count;
1367 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001368 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001369 }
Holger Machtc9263552006-10-20 14:30:29 -07001370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001373static const struct file_operations lcd_proc_fops = {
1374 .owner = THIS_MODULE,
1375 .open = lcd_proc_open,
1376 .read = seq_read,
1377 .llseek = seq_lseek,
1378 .release = single_release,
1379 .write = lcd_proc_write,
1380};
1381
Seth Forshee36d03f92011-09-20 16:55:53 -05001382static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1383{
1384 u32 hci_result;
1385
Azael Avalos893f3f62014-09-29 20:40:09 -06001386 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001387 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001388}
1389
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001390static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Seth Forshee135740d2011-09-20 16:55:49 -05001392 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001394 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Seth Forshee36d03f92011-09-20 16:55:53 -05001396 ret = get_video_status(dev, &value);
1397 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1399 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001400 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001401
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001402 seq_printf(m, "lcd_out: %d\n", is_lcd);
1403 seq_printf(m, "crt_out: %d\n", is_crt);
1404 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Seth Forshee36d03f92011-09-20 16:55:53 -05001407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001410static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Al Virod9dda782013-03-31 18:16:14 -04001412 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001413}
1414
1415static ssize_t video_proc_write(struct file *file, const char __user *buf,
1416 size_t count, loff_t *pos)
1417{
Al Virod9dda782013-03-31 18:16:14 -04001418 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001419 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001420 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int value;
1422 int remain = count;
1423 int lcd_out = -1;
1424 int crt_out = -1;
1425 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001426 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001428 cmd = kmalloc(count + 1, GFP_KERNEL);
1429 if (!cmd)
1430 return -ENOMEM;
1431 if (copy_from_user(cmd, buf, count)) {
1432 kfree(cmd);
1433 return -EFAULT;
1434 }
1435 cmd[count] = '\0';
1436
1437 buffer = cmd;
1438
Darren Harte0769fe2015-02-11 20:50:08 -08001439 /*
1440 * Scan expression. Multiple expressions may be delimited with ;
1441 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443 while (remain) {
1444 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1445 lcd_out = value & 1;
1446 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1447 crt_out = value & 1;
1448 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1449 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001450 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 do {
1452 ++buffer;
1453 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001454 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001457 kfree(cmd);
1458
Seth Forshee36d03f92011-09-20 16:55:53 -05001459 ret = get_video_status(dev, &video_out);
1460 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001461 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (lcd_out != -1)
1464 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1465 if (crt_out != -1)
1466 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1467 if (tv_out != -1)
1468 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001469 /*
1470 * To avoid unnecessary video disruption, only write the new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * video setting if something changed. */
1472 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001473 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001476 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001479static const struct file_operations video_proc_fops = {
1480 .owner = THIS_MODULE,
1481 .open = video_proc_open,
1482 .read = seq_read,
1483 .llseek = seq_lseek,
1484 .release = single_release,
1485 .write = video_proc_write,
1486};
1487
Seth Forshee36d03f92011-09-20 16:55:53 -05001488static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1489{
1490 u32 hci_result;
1491
Azael Avalos893f3f62014-09-29 20:40:09 -06001492 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001493 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001494}
1495
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001496static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Seth Forshee135740d2011-09-20 16:55:49 -05001498 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 u32 value;
1501
Seth Forshee36d03f92011-09-20 16:55:53 -05001502 ret = get_fan_status(dev, &value);
1503 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001504 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001505 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
Seth Forshee36d03f92011-09-20 16:55:53 -05001508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001511static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Al Virod9dda782013-03-31 18:16:14 -04001513 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001514}
1515
1516static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1517 size_t count, loff_t *pos)
1518{
Al Virod9dda782013-03-31 18:16:14 -04001519 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001520 char cmd[42];
1521 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int value;
1523 u32 hci_result;
1524
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001525 len = min(count, sizeof(cmd) - 1);
1526 if (copy_from_user(cmd, buf, len))
1527 return -EFAULT;
1528 cmd[len] = '\0';
1529
1530 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001531 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001532 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001533 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001534 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001535 else
1536 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 } else {
1538 return -EINVAL;
1539 }
1540
1541 return count;
1542}
1543
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001544static const struct file_operations fan_proc_fops = {
1545 .owner = THIS_MODULE,
1546 .open = fan_proc_open,
1547 .read = seq_read,
1548 .llseek = seq_lseek,
1549 .release = single_release,
1550 .write = fan_proc_write,
1551};
1552
1553static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Seth Forshee135740d2011-09-20 16:55:49 -05001555 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 u32 hci_result;
1557 u32 value;
1558
Seth Forshee11948b92011-11-16 17:37:45 -06001559 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001560 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001561 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001562 dev->key_event_valid = 1;
1563 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001564 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001565 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001566 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001567 /*
1568 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001570 * become disabled.
1571 */
Azael Avalos893f3f62014-09-29 20:40:09 -06001572 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001573 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001575 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001576 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578 }
1579
Seth Forshee135740d2011-09-20 16:55:49 -05001580 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1581 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001585static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Al Virod9dda782013-03-31 18:16:14 -04001587 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001588}
1589
1590static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1591 size_t count, loff_t *pos)
1592{
Al Virod9dda782013-03-31 18:16:14 -04001593 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001594 char cmd[42];
1595 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int value;
1597
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001598 len = min(count, sizeof(cmd) - 1);
1599 if (copy_from_user(cmd, buf, len))
1600 return -EFAULT;
1601 cmd[len] = '\0';
1602
Azael Avalosb5163992015-02-10 23:43:56 -07001603 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001604 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001605 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 return count;
1609}
1610
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001611static const struct file_operations keys_proc_fops = {
1612 .owner = THIS_MODULE,
1613 .open = keys_proc_open,
1614 .read = seq_read,
1615 .llseek = seq_lseek,
1616 .release = single_release,
1617 .write = keys_proc_write,
1618};
1619
1620static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001622 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1623 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001627static int version_proc_open(struct inode *inode, struct file *file)
1628{
Al Virod9dda782013-03-31 18:16:14 -04001629 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001630}
1631
1632static const struct file_operations version_proc_fops = {
1633 .owner = THIS_MODULE,
1634 .open = version_proc_open,
1635 .read = seq_read,
1636 .llseek = seq_lseek,
1637 .release = single_release,
1638};
1639
Darren Harte0769fe2015-02-11 20:50:08 -08001640/*
1641 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
1643
1644#define PROC_TOSHIBA "toshiba"
1645
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001646static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Seth Forshee36d03f92011-09-20 16:55:53 -05001648 if (dev->backlight_dev)
1649 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1650 &lcd_proc_fops, dev);
1651 if (dev->video_supported)
1652 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1653 &video_proc_fops, dev);
1654 if (dev->fan_supported)
1655 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1656 &fan_proc_fops, dev);
1657 if (dev->hotkey_dev)
1658 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1659 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001660 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1661 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Seth Forshee36d03f92011-09-20 16:55:53 -05001664static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Seth Forshee36d03f92011-09-20 16:55:53 -05001666 if (dev->backlight_dev)
1667 remove_proc_entry("lcd", toshiba_proc_dir);
1668 if (dev->video_supported)
1669 remove_proc_entry("video", toshiba_proc_dir);
1670 if (dev->fan_supported)
1671 remove_proc_entry("fan", toshiba_proc_dir);
1672 if (dev->hotkey_dev)
1673 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001674 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Lionel Debrouxacc24722010-11-16 14:14:02 +01001677static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001678 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001679 .get_brightness = get_lcd_brightness,
1680 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001681};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001682
Azael Avalos360f0f32014-03-25 20:38:31 -06001683/*
1684 * Sysfs files
1685 */
Azael Avalos9d309842015-02-10 23:43:59 -07001686static ssize_t version_show(struct device *dev,
1687 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001688{
1689 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1690}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001691static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001692
Azael Avalos9d309842015-02-10 23:43:59 -07001693static ssize_t fan_store(struct device *dev,
1694 struct device_attribute *attr,
1695 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001696{
1697 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1698 u32 result;
1699 int state;
1700 int ret;
1701
1702 ret = kstrtoint(buf, 0, &state);
1703 if (ret)
1704 return ret;
1705
1706 if (state != 0 && state != 1)
1707 return -EINVAL;
1708
1709 result = hci_write1(toshiba, HCI_FAN, state);
1710 if (result == TOS_FAILURE)
1711 return -EIO;
1712 else if (result == TOS_NOT_SUPPORTED)
1713 return -ENODEV;
1714
1715 return count;
1716}
1717
Azael Avalos9d309842015-02-10 23:43:59 -07001718static ssize_t fan_show(struct device *dev,
1719 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001720{
1721 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1722 u32 value;
1723 int ret;
1724
1725 ret = get_fan_status(toshiba, &value);
1726 if (ret)
1727 return ret;
1728
1729 return sprintf(buf, "%d\n", value);
1730}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001731static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001732
Azael Avalos9d309842015-02-10 23:43:59 -07001733static ssize_t kbd_backlight_mode_store(struct device *dev,
1734 struct device_attribute *attr,
1735 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001736{
1737 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001738 int mode;
1739 int time;
1740 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001741
Dan Carpenteraeaac092014-09-03 14:44:37 +03001742
1743 ret = kstrtoint(buf, 0, &mode);
1744 if (ret)
1745 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001746
1747 /* Check for supported modes depending on keyboard backlight type */
1748 if (toshiba->kbd_type == 1) {
1749 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1750 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1751 return -EINVAL;
1752 } else if (toshiba->kbd_type == 2) {
1753 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1754 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1755 mode != SCI_KBD_MODE_OFF)
1756 return -EINVAL;
1757 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001758
Darren Harte0769fe2015-02-11 20:50:08 -08001759 /*
1760 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001761 * Auto - KBD backlight turns off automatically in given time
1762 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001763 * ON - KBD backlight is always on
1764 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001765 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001766
1767 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001768 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001769 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001770 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001771
1772 /* OR the "base time" to the actual method format */
1773 if (toshiba->kbd_type == 1) {
1774 /* Type 1 requires the current mode */
1775 time |= toshiba->kbd_mode;
1776 } else if (toshiba->kbd_type == 2) {
1777 /* Type 2 requires the desired mode */
1778 time |= mode;
1779 }
1780
Dan Carpenteraeaac092014-09-03 14:44:37 +03001781 ret = toshiba_kbd_illum_status_set(toshiba, time);
1782 if (ret)
1783 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001784
Azael Avalos360f0f32014-03-25 20:38:31 -06001785 toshiba->kbd_mode = mode;
1786 }
1787
1788 return count;
1789}
1790
Azael Avalos9d309842015-02-10 23:43:59 -07001791static ssize_t kbd_backlight_mode_show(struct device *dev,
1792 struct device_attribute *attr,
1793 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001794{
1795 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1796 u32 time;
1797
1798 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1799 return -EIO;
1800
Azael Avalos93f8c162014-09-12 18:50:36 -06001801 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1802}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001803static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001804
Azael Avalos9d309842015-02-10 23:43:59 -07001805static ssize_t kbd_type_show(struct device *dev,
1806 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001807{
1808 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1809
1810 return sprintf(buf, "%d\n", toshiba->kbd_type);
1811}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001812static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001813
Azael Avalos9d309842015-02-10 23:43:59 -07001814static ssize_t available_kbd_modes_show(struct device *dev,
1815 struct device_attribute *attr,
1816 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001817{
1818 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1819
1820 if (toshiba->kbd_type == 1)
1821 return sprintf(buf, "%x %x\n",
1822 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1823
1824 return sprintf(buf, "%x %x %x\n",
1825 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001826}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001827static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001828
Azael Avalos9d309842015-02-10 23:43:59 -07001829static ssize_t kbd_backlight_timeout_store(struct device *dev,
1830 struct device_attribute *attr,
1831 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001832{
1833 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001834 int time;
1835 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001836
Azael Avaloseabde0f2014-10-04 12:02:21 -06001837 ret = kstrtoint(buf, 0, &time);
1838 if (ret)
1839 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001840
Azael Avaloseabde0f2014-10-04 12:02:21 -06001841 /* Check for supported values depending on kbd_type */
1842 if (toshiba->kbd_type == 1) {
1843 if (time < 0 || time > 60)
1844 return -EINVAL;
1845 } else if (toshiba->kbd_type == 2) {
1846 if (time < 1 || time > 60)
1847 return -EINVAL;
1848 }
1849
1850 /* Set the Keyboard Backlight Timeout */
1851
1852 /* Only make a change if the actual timeout has changed */
1853 if (toshiba->kbd_time != time) {
1854 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001855 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001856 /* OR the "base time" to the actual method format */
1857 if (toshiba->kbd_type == 1)
1858 time |= SCI_KBD_MODE_FNZ;
1859 else if (toshiba->kbd_type == 2)
1860 time |= SCI_KBD_MODE_AUTO;
1861
1862 ret = toshiba_kbd_illum_status_set(toshiba, time);
1863 if (ret)
1864 return ret;
1865
Azael Avalos360f0f32014-03-25 20:38:31 -06001866 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1867 }
1868
1869 return count;
1870}
1871
Azael Avalos9d309842015-02-10 23:43:59 -07001872static ssize_t kbd_backlight_timeout_show(struct device *dev,
1873 struct device_attribute *attr,
1874 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001875{
1876 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1877 u32 time;
1878
1879 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1880 return -EIO;
1881
1882 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1883}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001884static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001885
Azael Avalos9d309842015-02-10 23:43:59 -07001886static ssize_t touchpad_store(struct device *dev,
1887 struct device_attribute *attr,
1888 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001889{
1890 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1891 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001892 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001893
1894 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001895 ret = kstrtoint(buf, 0, &state);
1896 if (ret)
1897 return ret;
1898 if (state != 0 && state != 1)
1899 return -EINVAL;
1900
1901 ret = toshiba_touchpad_set(toshiba, state);
1902 if (ret)
1903 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001904
1905 return count;
1906}
1907
Azael Avalos9d309842015-02-10 23:43:59 -07001908static ssize_t touchpad_show(struct device *dev,
1909 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001910{
1911 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1912 u32 state;
1913 int ret;
1914
1915 ret = toshiba_touchpad_get(toshiba, &state);
1916 if (ret < 0)
1917 return ret;
1918
1919 return sprintf(buf, "%i\n", state);
1920}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001921static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001922
Azael Avalos9d309842015-02-10 23:43:59 -07001923static ssize_t position_show(struct device *dev,
1924 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001925{
1926 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1927 u32 xyval, zval, tmp;
1928 u16 x, y, z;
1929 int ret;
1930
1931 xyval = zval = 0;
1932 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1933 if (ret < 0)
1934 return ret;
1935
1936 x = xyval & HCI_ACCEL_MASK;
1937 tmp = xyval >> HCI_MISC_SHIFT;
1938 y = tmp & HCI_ACCEL_MASK;
1939 z = zval & HCI_ACCEL_MASK;
1940
1941 return sprintf(buf, "%d %d %d\n", x, y, z);
1942}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001943static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001944
Azael Avalos9d309842015-02-10 23:43:59 -07001945static ssize_t usb_sleep_charge_show(struct device *dev,
1946 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001947{
1948 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1949 u32 mode;
1950 int ret;
1951
1952 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1953 if (ret < 0)
1954 return ret;
1955
1956 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1957}
1958
Azael Avalos9d309842015-02-10 23:43:59 -07001959static ssize_t usb_sleep_charge_store(struct device *dev,
1960 struct device_attribute *attr,
1961 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001962{
1963 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1964 u32 mode;
1965 int state;
1966 int ret;
1967
1968 ret = kstrtoint(buf, 0, &state);
1969 if (ret)
1970 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001971 /*
1972 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001973 * 0 - Disabled
1974 * 1 - Alternate (Non USB conformant devices that require more power)
1975 * 2 - Auto (USB conformant devices)
1976 */
1977 if (state != 0 && state != 1 && state != 2)
1978 return -EINVAL;
1979
1980 /* Set the USB charging mode to internal value */
1981 if (state == 0)
1982 mode = SCI_USB_CHARGE_DISABLED;
1983 else if (state == 1)
1984 mode = SCI_USB_CHARGE_ALTERNATE;
1985 else if (state == 2)
1986 mode = SCI_USB_CHARGE_AUTO;
1987
1988 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1989 if (ret)
1990 return ret;
1991
1992 return count;
1993}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001994static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001995
Azael Avalos182bcaa2015-01-18 18:30:23 -07001996static ssize_t sleep_functions_on_battery_show(struct device *dev,
1997 struct device_attribute *attr,
1998 char *buf)
1999{
2000 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2001 u32 state;
2002 int bat_lvl;
2003 int status;
2004 int ret;
2005 int tmp;
2006
2007 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2008 if (ret < 0)
2009 return ret;
2010
2011 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2012 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2013 status = (tmp == 0x4) ? 1 : 0;
2014 /* Determine the battery level set */
2015 bat_lvl = state >> HCI_MISC_SHIFT;
2016
2017 return sprintf(buf, "%d %d\n", status, bat_lvl);
2018}
2019
2020static ssize_t sleep_functions_on_battery_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
2023{
2024 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2025 u32 status;
2026 int value;
2027 int ret;
2028 int tmp;
2029
2030 ret = kstrtoint(buf, 0, &value);
2031 if (ret)
2032 return ret;
2033
Darren Harte0769fe2015-02-11 20:50:08 -08002034 /*
2035 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002036 * 0 - Disabled
2037 * 1-100 - Enabled
2038 */
2039 if (value < 0 || value > 100)
2040 return -EINVAL;
2041
2042 if (value == 0) {
2043 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2044 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2045 } else {
2046 tmp = value << HCI_MISC_SHIFT;
2047 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2048 }
2049 ret = toshiba_sleep_functions_status_set(toshiba, status);
2050 if (ret < 0)
2051 return ret;
2052
2053 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2054
2055 return count;
2056}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002057static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002058
Azael Avalos9d309842015-02-10 23:43:59 -07002059static ssize_t usb_rapid_charge_show(struct device *dev,
2060 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002061{
2062 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2063 u32 state;
2064 int ret;
2065
2066 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2067 if (ret < 0)
2068 return ret;
2069
2070 return sprintf(buf, "%d\n", state);
2071}
2072
Azael Avalos9d309842015-02-10 23:43:59 -07002073static ssize_t usb_rapid_charge_store(struct device *dev,
2074 struct device_attribute *attr,
2075 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002076{
2077 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2078 int state;
2079 int ret;
2080
2081 ret = kstrtoint(buf, 0, &state);
2082 if (ret)
2083 return ret;
2084 if (state != 0 && state != 1)
2085 return -EINVAL;
2086
2087 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2088 if (ret)
2089 return ret;
2090
2091 return count;
2092}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002093static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002094
Azael Avalos9d309842015-02-10 23:43:59 -07002095static ssize_t usb_sleep_music_show(struct device *dev,
2096 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002097{
2098 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2099 u32 state;
2100 int ret;
2101
2102 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2103 if (ret < 0)
2104 return ret;
2105
2106 return sprintf(buf, "%d\n", state);
2107}
2108
Azael Avalos9d309842015-02-10 23:43:59 -07002109static ssize_t usb_sleep_music_store(struct device *dev,
2110 struct device_attribute *attr,
2111 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002112{
2113 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2114 int state;
2115 int ret;
2116
2117 ret = kstrtoint(buf, 0, &state);
2118 if (ret)
2119 return ret;
2120 if (state != 0 && state != 1)
2121 return -EINVAL;
2122
2123 ret = toshiba_usb_sleep_music_set(toshiba, state);
2124 if (ret)
2125 return ret;
2126
2127 return count;
2128}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002129static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002130
Azael Avalos9d309842015-02-10 23:43:59 -07002131static ssize_t kbd_function_keys_show(struct device *dev,
2132 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002133{
2134 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135 int mode;
2136 int ret;
2137
2138 ret = toshiba_function_keys_get(toshiba, &mode);
2139 if (ret < 0)
2140 return ret;
2141
2142 return sprintf(buf, "%d\n", mode);
2143}
2144
Azael Avalos9d309842015-02-10 23:43:59 -07002145static ssize_t kbd_function_keys_store(struct device *dev,
2146 struct device_attribute *attr,
2147 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002148{
2149 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2150 int mode;
2151 int ret;
2152
2153 ret = kstrtoint(buf, 0, &mode);
2154 if (ret)
2155 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002156 /*
2157 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002158 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2159 * 1 - Special functions (Opposite of the above setting)
2160 */
2161 if (mode != 0 && mode != 1)
2162 return -EINVAL;
2163
2164 ret = toshiba_function_keys_set(toshiba, mode);
2165 if (ret)
2166 return ret;
2167
2168 pr_info("Reboot for changes to KBD Function Keys to take effect");
2169
2170 return count;
2171}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002172static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002173
Azael Avalos9d309842015-02-10 23:43:59 -07002174static ssize_t panel_power_on_show(struct device *dev,
2175 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002176{
2177 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2178 u32 state;
2179 int ret;
2180
2181 ret = toshiba_panel_power_on_get(toshiba, &state);
2182 if (ret < 0)
2183 return ret;
2184
2185 return sprintf(buf, "%d\n", state);
2186}
2187
Azael Avalos9d309842015-02-10 23:43:59 -07002188static ssize_t panel_power_on_store(struct device *dev,
2189 struct device_attribute *attr,
2190 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002191{
2192 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2193 int state;
2194 int ret;
2195
2196 ret = kstrtoint(buf, 0, &state);
2197 if (ret)
2198 return ret;
2199 if (state != 0 && state != 1)
2200 return -EINVAL;
2201
2202 ret = toshiba_panel_power_on_set(toshiba, state);
2203 if (ret)
2204 return ret;
2205
2206 pr_info("Reboot for changes to Panel Power ON to take effect");
2207
2208 return count;
2209}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002210static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002211
Azael Avalos9d309842015-02-10 23:43:59 -07002212static ssize_t usb_three_show(struct device *dev,
2213 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002214{
2215 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2216 u32 state;
2217 int ret;
2218
2219 ret = toshiba_usb_three_get(toshiba, &state);
2220 if (ret < 0)
2221 return ret;
2222
2223 return sprintf(buf, "%d\n", state);
2224}
2225
Azael Avalos9d309842015-02-10 23:43:59 -07002226static ssize_t usb_three_store(struct device *dev,
2227 struct device_attribute *attr,
2228 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002229{
2230 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2231 int state;
2232 int ret;
2233
2234 ret = kstrtoint(buf, 0, &state);
2235 if (ret)
2236 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002237 /*
2238 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002239 * 0 - Disabled (Acts like a USB 2 port, saving power)
2240 * 1 - Enabled
2241 */
2242 if (state != 0 && state != 1)
2243 return -EINVAL;
2244
2245 ret = toshiba_usb_three_set(toshiba, state);
2246 if (ret)
2247 return ret;
2248
2249 pr_info("Reboot for changes to USB 3 to take effect");
2250
2251 return count;
2252}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002253static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002254
2255static struct attribute *toshiba_attributes[] = {
2256 &dev_attr_version.attr,
2257 &dev_attr_fan.attr,
2258 &dev_attr_kbd_backlight_mode.attr,
2259 &dev_attr_kbd_type.attr,
2260 &dev_attr_available_kbd_modes.attr,
2261 &dev_attr_kbd_backlight_timeout.attr,
2262 &dev_attr_touchpad.attr,
2263 &dev_attr_position.attr,
2264 &dev_attr_usb_sleep_charge.attr,
2265 &dev_attr_sleep_functions_on_battery.attr,
2266 &dev_attr_usb_rapid_charge.attr,
2267 &dev_attr_usb_sleep_music.attr,
2268 &dev_attr_kbd_function_keys.attr,
2269 &dev_attr_panel_power_on.attr,
2270 &dev_attr_usb_three.attr,
2271 NULL,
2272};
2273
Azael Avalos360f0f32014-03-25 20:38:31 -06002274static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2275 struct attribute *attr, int idx)
2276{
2277 struct device *dev = container_of(kobj, struct device, kobj);
2278 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2279 bool exists = true;
2280
Azael Avalos94477d42015-02-10 21:09:17 -07002281 if (attr == &dev_attr_fan.attr)
2282 exists = (drv->fan_supported) ? true : false;
2283 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002284 exists = (drv->kbd_illum_supported) ? true : false;
2285 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2286 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002287 else if (attr == &dev_attr_touchpad.attr)
2288 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002289 else if (attr == &dev_attr_position.attr)
2290 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002291 else if (attr == &dev_attr_usb_sleep_charge.attr)
2292 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002293 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2294 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002295 else if (attr == &dev_attr_usb_rapid_charge.attr)
2296 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002297 else if (attr == &dev_attr_usb_sleep_music.attr)
2298 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002299 else if (attr == &dev_attr_kbd_function_keys.attr)
2300 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002301 else if (attr == &dev_attr_panel_power_on.attr)
2302 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002303 else if (attr == &dev_attr_usb_three.attr)
2304 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002305
2306 return exists ? attr->mode : 0;
2307}
2308
Azael Avalos9bd12132015-02-10 23:43:58 -07002309static struct attribute_group toshiba_attr_group = {
2310 .is_visible = toshiba_sysfs_is_visible,
2311 .attrs = toshiba_attributes,
2312};
2313
Azael Avalos1f28f292014-12-04 20:22:45 -07002314/*
2315 * Hotkeys
2316 */
2317static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2318{
2319 acpi_status status;
2320 u32 result;
2321
2322 status = acpi_evaluate_object(dev->acpi_dev->handle,
2323 "ENAB", NULL, NULL);
2324 if (ACPI_FAILURE(status))
2325 return -ENODEV;
2326
2327 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2328 if (result == TOS_FAILURE)
2329 return -EIO;
2330 else if (result == TOS_NOT_SUPPORTED)
2331 return -ENODEV;
2332
2333 return 0;
2334}
2335
Seth Forshee29cd2932012-01-18 13:44:09 -06002336static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2337 struct serio *port)
2338{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002339 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002340 return false;
2341
2342 if (unlikely(data == 0xe0))
2343 return false;
2344
2345 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2346 schedule_work(&toshiba_acpi->hotkey_work);
2347 return true;
2348 }
2349
2350 return false;
2351}
2352
2353static void toshiba_acpi_hotkey_work(struct work_struct *work)
2354{
2355 acpi_handle ec_handle = ec_get_handle();
2356 acpi_status status;
2357
2358 if (!ec_handle)
2359 return;
2360
2361 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2362 if (ACPI_FAILURE(status))
2363 pr_err("ACPI NTFY method execution failed\n");
2364}
2365
2366/*
2367 * Returns hotkey scancode, or < 0 on failure.
2368 */
2369static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2370{
Zhang Rui74facaf2013-09-03 08:32:15 +08002371 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002372 acpi_status status;
2373
Zhang Rui74facaf2013-09-03 08:32:15 +08002374 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2375 NULL, &value);
2376 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002377 pr_err("ACPI INFO method execution failed\n");
2378 return -EIO;
2379 }
2380
Zhang Rui74facaf2013-09-03 08:32:15 +08002381 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002382}
2383
2384static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2385 int scancode)
2386{
2387 if (scancode == 0x100)
2388 return;
2389
Darren Harte0769fe2015-02-11 20:50:08 -08002390 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002391 if (scancode & 0x80)
2392 return;
2393
2394 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2395 pr_info("Unknown key %x\n", scancode);
2396}
2397
Azael Avalos71454d72014-12-04 20:22:46 -07002398static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2399{
2400 u32 hci_result, value;
2401 int retries = 3;
2402 int scancode;
2403
2404 if (dev->info_supported) {
2405 scancode = toshiba_acpi_query_hotkey(dev);
2406 if (scancode < 0)
2407 pr_err("Failed to query hotkey event\n");
2408 else if (scancode != 0)
2409 toshiba_acpi_report_hotkey(dev, scancode);
2410 } else if (dev->system_event_supported) {
2411 do {
2412 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2413 switch (hci_result) {
2414 case TOS_SUCCESS:
2415 toshiba_acpi_report_hotkey(dev, (int)value);
2416 break;
2417 case TOS_NOT_SUPPORTED:
2418 /*
2419 * This is a workaround for an unresolved
2420 * issue on some machines where system events
2421 * sporadically become disabled.
2422 */
2423 hci_result =
2424 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2425 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002426 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002427 default:
2428 retries--;
2429 break;
2430 }
2431 } while (retries && hci_result != TOS_FIFO_EMPTY);
2432 }
2433}
2434
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002435static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002436{
Zhang Ruie2e19602013-09-03 08:32:06 +08002437 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05002438 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002439 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002440 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05002441
Seth Forshee135740d2011-09-20 16:55:49 -05002442 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002443 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002444 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002445
2446 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002447 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002448 dev->hotkey_dev->id.bustype = BUS_HOST;
2449
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002450 if (dmi_check_system(toshiba_alt_keymap_dmi))
2451 keymap = toshiba_acpi_alt_keymap;
2452 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002453 if (error)
2454 goto err_free_dev;
2455
Seth Forshee29cd2932012-01-18 13:44:09 -06002456 /*
2457 * For some machines the SCI responsible for providing hotkey
2458 * notification doesn't fire. We can trigger the notification
2459 * whenever the Fn key is pressed using the NTFY method, if
2460 * supported, so if it's present set up an i8042 key filter
2461 * for this purpose.
2462 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002463 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002464 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002465 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2466
2467 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2468 if (error) {
2469 pr_err("Error installing key filter\n");
2470 goto err_free_keymap;
2471 }
2472
2473 dev->ntfy_supported = 1;
2474 }
2475
2476 /*
2477 * Determine hotkey query interface. Prefer using the INFO
2478 * method when it is available.
2479 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002480 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002481 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002482 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002483 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002484 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002485 dev->system_event_supported = 1;
2486 }
2487
2488 if (!dev->info_supported && !dev->system_event_supported) {
2489 pr_warn("No hotkey query interface found\n");
2490 goto err_remove_filter;
2491 }
2492
Azael Avalos1f28f292014-12-04 20:22:45 -07002493 error = toshiba_acpi_enable_hotkeys(dev);
2494 if (error) {
Seth Forshee135740d2011-09-20 16:55:49 -05002495 pr_info("Unable to enable hotkeys\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002496 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002497 }
2498
2499 error = input_register_device(dev->hotkey_dev);
2500 if (error) {
2501 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002502 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002503 }
2504
2505 return 0;
2506
Seth Forshee29cd2932012-01-18 13:44:09 -06002507 err_remove_filter:
2508 if (dev->ntfy_supported)
2509 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002510 err_free_keymap:
2511 sparse_keymap_free(dev->hotkey_dev);
2512 err_free_dev:
2513 input_free_device(dev->hotkey_dev);
2514 dev->hotkey_dev = NULL;
2515 return error;
2516}
2517
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002518static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002519{
2520 struct backlight_properties props;
2521 int brightness;
2522 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002523 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002524
2525 /*
2526 * Some machines don't support the backlight methods at all, and
2527 * others support it read-only. Either of these is pretty useless,
2528 * so only register the backlight device if the backlight method
2529 * supports both reads and writes.
2530 */
2531 brightness = __get_lcd_brightness(dev);
2532 if (brightness < 0)
2533 return 0;
2534 ret = set_lcd_brightness(dev, brightness);
2535 if (ret) {
2536 pr_debug("Backlight method is read-only, disabling backlight support\n");
2537 return 0;
2538 }
2539
Akio Idehara121b7b02012-04-06 01:46:43 +09002540 /* Determine whether or not BIOS supports transflective backlight */
2541 ret = get_tr_backlight_status(dev, &enabled);
2542 dev->tr_backlight_supported = !ret;
2543
Matthew Garrett53039f22012-06-01 11:02:36 -04002544 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002545 props.type = BACKLIGHT_PLATFORM;
2546 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002547
Darren Harte0769fe2015-02-11 20:50:08 -08002548 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002549 if (dev->tr_backlight_supported)
2550 props.max_brightness++;
2551
Seth Forshee62cce752012-04-19 11:23:50 -05002552 dev->backlight_dev = backlight_device_register("toshiba",
2553 &dev->acpi_dev->dev,
2554 dev,
2555 &toshiba_backlight_data,
2556 &props);
2557 if (IS_ERR(dev->backlight_dev)) {
2558 ret = PTR_ERR(dev->backlight_dev);
2559 pr_err("Could not register toshiba backlight device\n");
2560 dev->backlight_dev = NULL;
2561 return ret;
2562 }
2563
2564 dev->backlight_dev->props.brightness = brightness;
2565 return 0;
2566}
2567
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002568static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002569{
2570 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2571
Seth Forshee36d03f92011-09-20 16:55:53 -05002572 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002573
Azael Avalos360f0f32014-03-25 20:38:31 -06002574 if (dev->sysfs_created)
2575 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2576 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002577
Seth Forshee29cd2932012-01-18 13:44:09 -06002578 if (dev->ntfy_supported) {
2579 i8042_remove_filter(toshiba_acpi_i8042_filter);
2580 cancel_work_sync(&dev->hotkey_work);
2581 }
2582
Seth Forshee135740d2011-09-20 16:55:49 -05002583 if (dev->hotkey_dev) {
2584 input_unregister_device(dev->hotkey_dev);
2585 sparse_keymap_free(dev->hotkey_dev);
2586 }
2587
2588 if (dev->bt_rfk) {
2589 rfkill_unregister(dev->bt_rfk);
2590 rfkill_destroy(dev->bt_rfk);
2591 }
2592
Markus Elfring00981812014-11-24 20:30:29 +01002593 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002594
Seth Forshee36d03f92011-09-20 16:55:53 -05002595 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002596 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002597
Azael Avalos360f0f32014-03-25 20:38:31 -06002598 if (dev->kbd_led_registered)
2599 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002600
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002601 if (dev->eco_supported)
2602 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002603
Seth Forshee29cd2932012-01-18 13:44:09 -06002604 if (toshiba_acpi)
2605 toshiba_acpi = NULL;
2606
Seth Forshee135740d2011-09-20 16:55:49 -05002607 kfree(dev);
2608
2609 return 0;
2610}
2611
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002612static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002613{
Zhang Ruie2e19602013-09-03 08:32:06 +08002614 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002615 return "GHCI";
2616
Zhang Ruie2e19602013-09-03 08:32:06 +08002617 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002618 return "SPFC";
2619
2620 return NULL;
2621}
2622
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002623static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002624{
2625 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002626 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002627 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002628 bool bt_present;
2629 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002630
Seth Forshee29cd2932012-01-18 13:44:09 -06002631 if (toshiba_acpi)
2632 return -EBUSY;
2633
Seth Forshee135740d2011-09-20 16:55:49 -05002634 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2635 TOSHIBA_ACPI_VERSION);
2636
Seth Forsheea540d6b2011-09-20 16:55:52 -05002637 hci_method = find_hci_method(acpi_dev->handle);
2638 if (!hci_method) {
2639 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002640 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002641 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002642
Seth Forshee135740d2011-09-20 16:55:49 -05002643 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2644 if (!dev)
2645 return -ENOMEM;
2646 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002647 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002648 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002649 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002650
Seth Forshee6e02cc72011-09-20 16:55:51 -05002651 if (toshiba_acpi_setup_keyboard(dev))
2652 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002653
2654 mutex_init(&dev->mutex);
2655
Seth Forshee62cce752012-04-19 11:23:50 -05002656 ret = toshiba_acpi_setup_backlight(dev);
2657 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002658 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002659
2660 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002661 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002662 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2663 &acpi_dev->dev,
2664 RFKILL_TYPE_BLUETOOTH,
2665 &toshiba_rfk_ops,
2666 dev);
2667 if (!dev->bt_rfk) {
2668 pr_err("unable to allocate rfkill device\n");
2669 ret = -ENOMEM;
2670 goto error;
2671 }
2672
2673 ret = rfkill_register(dev->bt_rfk);
2674 if (ret) {
2675 pr_err("unable to register rfkill device\n");
2676 rfkill_destroy(dev->bt_rfk);
2677 goto error;
2678 }
2679 }
2680
2681 if (toshiba_illumination_available(dev)) {
2682 dev->led_dev.name = "toshiba::illumination";
2683 dev->led_dev.max_brightness = 1;
2684 dev->led_dev.brightness_set = toshiba_illumination_set;
2685 dev->led_dev.brightness_get = toshiba_illumination_get;
2686 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002687 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002688 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002689
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002690 if (toshiba_eco_mode_available(dev)) {
2691 dev->eco_led.name = "toshiba::eco_mode";
2692 dev->eco_led.max_brightness = 1;
2693 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2694 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2695 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2696 dev->eco_supported = 1;
2697 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002698
Azael Avalos93f8c162014-09-12 18:50:36 -06002699 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002700 /*
2701 * Only register the LED if KBD illumination is supported
2702 * and the keyboard backlight operation mode is set to FN-Z
2703 */
2704 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2705 dev->kbd_led.name = "toshiba::kbd_backlight";
2706 dev->kbd_led.max_brightness = 1;
2707 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2708 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2709 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2710 dev->kbd_led_registered = 1;
2711 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002712
Azael Avalos9d8658a2014-03-25 20:38:32 -06002713 ret = toshiba_touchpad_get(dev, &dummy);
2714 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002715
Azael Avalos5a2813e2014-03-25 20:38:34 -06002716 ret = toshiba_accelerometer_supported(dev);
2717 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002718
Azael Avalose26ffe52015-01-18 18:30:22 -07002719 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2720 dev->usb_sleep_charge_supported = !ret;
2721
Azael Avalosbb3fe012015-01-18 18:30:24 -07002722 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2723 dev->usb_rapid_charge_supported = !ret;
2724
Azael Avalos172ce0a2015-01-18 18:30:25 -07002725 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2726 dev->usb_sleep_music_supported = !ret;
2727
Azael Avalosbae84192015-02-10 21:09:18 -07002728 ret = toshiba_function_keys_get(dev, &dummy);
2729 dev->kbd_function_keys_supported = !ret;
2730
Azael Avalos35d53ce2015-02-10 21:09:19 -07002731 ret = toshiba_panel_power_on_get(dev, &dummy);
2732 dev->panel_power_on_supported = !ret;
2733
Azael Avalos17fe4b32015-02-10 21:09:20 -07002734 ret = toshiba_usb_three_get(dev, &dummy);
2735 dev->usb_three_supported = !ret;
2736
Seth Forshee36d03f92011-09-20 16:55:53 -05002737 /* Determine whether or not BIOS supports fan and video interfaces */
2738
2739 ret = get_video_status(dev, &dummy);
2740 dev->video_supported = !ret;
2741
2742 ret = get_fan_status(dev, &dummy);
2743 dev->fan_supported = !ret;
2744
Azael Avalos360f0f32014-03-25 20:38:31 -06002745 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2746 &toshiba_attr_group);
2747 if (ret) {
2748 dev->sysfs_created = 0;
2749 goto error;
2750 }
2751 dev->sysfs_created = !ret;
2752
Seth Forshee36d03f92011-09-20 16:55:53 -05002753 create_toshiba_proc_entries(dev);
2754
Seth Forshee29cd2932012-01-18 13:44:09 -06002755 toshiba_acpi = dev;
2756
Seth Forshee135740d2011-09-20 16:55:49 -05002757 return 0;
2758
2759error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002760 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002761 return ret;
2762}
2763
2764static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2765{
2766 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002767 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002768
Azael Avalos71454d72014-12-04 20:22:46 -07002769 switch (event) {
2770 case 0x80: /* Hotkeys and some system events */
2771 toshiba_acpi_process_hotkeys(dev);
2772 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002773 case 0x81: /* Dock events */
2774 case 0x82:
2775 case 0x83:
2776 pr_info("Dock event received %x\n", event);
2777 break;
2778 case 0x88: /* Thermal events */
2779 pr_info("Thermal event received\n");
2780 break;
2781 case 0x8f: /* LID closed */
2782 case 0x90: /* LID is closed and Dock has been ejected */
2783 break;
2784 case 0x8c: /* SATA power events */
2785 case 0x8b:
2786 pr_info("SATA power event received %x\n", event);
2787 break;
Azael Avalos80546902014-12-04 20:22:47 -07002788 case 0x92: /* Keyboard backlight mode changed */
2789 /* Update sysfs entries */
2790 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2791 &toshiba_attr_group);
2792 if (ret)
2793 pr_err("Unable to update sysfs entries\n");
2794 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002795 case 0x85: /* Unknown */
2796 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002797 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002798 case 0x94: /* Unknown */
2799 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002800 default:
2801 pr_info("Unknown event received %x\n", event);
2802 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002803 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002804
2805 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2806 dev_name(&acpi_dev->dev),
2807 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002808}
2809
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002810#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002811static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002812{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002813 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002814 u32 result;
2815
2816 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002817 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002818
2819 return 0;
2820}
2821
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002822static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002823{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002824 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002825 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002826
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002827 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002828 error = toshiba_acpi_enable_hotkeys(dev);
2829 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002830 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002831 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002832
2833 return 0;
2834}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002835#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002836
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002837static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2838 toshiba_acpi_suspend, toshiba_acpi_resume);
2839
Seth Forshee135740d2011-09-20 16:55:49 -05002840static struct acpi_driver toshiba_acpi_driver = {
2841 .name = "Toshiba ACPI driver",
2842 .owner = THIS_MODULE,
2843 .ids = toshiba_device_ids,
2844 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2845 .ops = {
2846 .add = toshiba_acpi_add,
2847 .remove = toshiba_acpi_remove,
2848 .notify = toshiba_acpi_notify,
2849 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002850 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002851};
Holger Machtc9263552006-10-20 14:30:29 -07002852
Len Brown4be44fc2005-08-05 00:44:28 -04002853static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Seth Forshee135740d2011-09-20 16:55:49 -05002855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Seth Forsheef11f9992012-01-18 13:44:11 -06002857 /*
2858 * Machines with this WMI guid aren't supported due to bugs in
2859 * their AML. This check relies on wmi initializing before
2860 * toshiba_acpi to guarantee guids have been identified.
2861 */
2862 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2863 return -ENODEV;
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2866 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002867 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002868 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870
Seth Forshee135740d2011-09-20 16:55:49 -05002871 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2872 if (ret) {
2873 pr_err("Failed to register ACPI driver: %d\n", ret);
2874 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002875 }
2876
Seth Forshee135740d2011-09-20 16:55:49 -05002877 return ret;
2878}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002879
Seth Forshee135740d2011-09-20 16:55:49 -05002880static void __exit toshiba_acpi_exit(void)
2881{
2882 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2883 if (toshiba_proc_dir)
2884 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887module_init(toshiba_acpi_init);
2888module_exit(toshiba_acpi_exit);