blob: 0f1b7a8ed8021623a2703b7822aece06a57cdddc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02007 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos548c4302014-03-25 20:38:35 -06008 * Copyright (C) 2014 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * The devolpment page for this driver is located at
26 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 *
28 * Credits:
29 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30 * engineering the Windows drivers
31 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32 * Rob Miller - TV out and hotkeys help
33 *
34 *
35 * TODO
36 *
37 */
38
Joe Perches7e334602011-03-29 15:21:52 -070039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Azael Avalos548c4302014-03-25 20:38:35 -060041#define TOSHIBA_ACPI_VERSION "0.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define PROC_INTERFACE_VERSION 1
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/types.h>
48#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080049#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070050#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040051#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050052#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070053#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020054#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060056#include <linux/workqueue.h>
57#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080058#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020059#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/uaccess.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_AUTHOR("John Belmonte");
63MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64MODULE_LICENSE("GPL");
65
Seth Forsheef11f9992012-01-18 13:44:11 -060066#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
Seth Forshee29cd2932012-01-18 13:44:09 -060068/* Scan code for Fn key on TOS1900 models */
69#define TOS1900_FN_SCAN 0x6e
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
73
Azael Avalos258c5902014-09-29 20:40:07 -060074/* The Toshiba configuration interface is composed of the HCI and the SCI,
75 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78 * be uniform across all their models. Ideally we would just call
79 * dedicated ACPI methods instead of using this primitive interface.
80 * However the ACPI methods seem to be incomplete in some areas (for
81 * example they allow setting, but not reading, the LCD brightness value),
82 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040083 *
Azael Avalos84a62732014-03-25 20:38:29 -060084 * SCI stands for "System Configuration Interface" which aim is to
85 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Azael Avalos258c5902014-09-29 20:40:07 -060088#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* operations */
91#define HCI_SET 0xff00
92#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060093#define SCI_OPEN 0xf100
94#define SCI_CLOSE 0xf200
95#define SCI_GET 0xf300
96#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060099#define TOS_SUCCESS 0x0000
100#define TOS_OPEN_CLOSE_OK 0x0044
101#define TOS_FAILURE 0x1000
102#define TOS_NOT_SUPPORTED 0x8000
103#define TOS_ALREADY_OPEN 0x8100
104#define TOS_NOT_OPENED 0x8200
105#define TOS_INPUT_DATA_ERROR 0x8300
106#define TOS_WRITE_PROTECTED 0x8400
107#define TOS_NOT_PRESENT 0x8600
108#define TOS_FIFO_EMPTY 0x8c00
109#define TOS_DATA_NOT_AVAILABLE 0x8d20
110#define TOS_NOT_INITIALIZED 0x8d50
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* registers */
113#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900114#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define HCI_SYSTEM_EVENT 0x0016
116#define HCI_VIDEO_OUT 0x001c
117#define HCI_HOTKEY_EVENT 0x001e
118#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400119#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600120#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600121#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600122#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600123#define HCI_ACCELEROMETER2 0x00a6
Azael Avalosfdb79082014-03-25 20:38:30 -0600124#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700125#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600126#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos9d8658a2014-03-25 20:38:32 -0600127#define SCI_TOUCHPAD 0x050e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600130#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600131#define HCI_HOTKEY_DISABLE 0x0b
132#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define HCI_LCD_BRIGHTNESS_BITS 3
134#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
135#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600136#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define HCI_VIDEO_OUT_LCD 0x1
138#define HCI_VIDEO_OUT_CRT 0x2
139#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400140#define HCI_WIRELESS_KILL_SWITCH 0x01
141#define HCI_WIRELESS_BT_PRESENT 0x0f
142#define HCI_WIRELESS_BT_ATTACH 0x40
143#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600144#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600145#define SCI_KBD_MODE_FNZ 0x1
146#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600147#define SCI_KBD_MODE_ON 0x8
148#define SCI_KBD_MODE_OFF 0x10
149#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700150#define SCI_USB_CHARGE_MODE_MASK 0xff
151#define SCI_USB_CHARGE_DISABLED 0x30000
152#define SCI_USB_CHARGE_ALTERNATE 0x30009
153#define SCI_USB_CHARGE_AUTO 0x30021
Azael Avalos182bcaa2015-01-18 18:30:23 -0700154#define SCI_USB_CHARGE_BAT_MASK 0x7
155#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
156#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
157#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700158#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Seth Forshee135740d2011-09-20 16:55:49 -0500160struct toshiba_acpi_dev {
161 struct acpi_device *acpi_dev;
162 const char *method_hci;
163 struct rfkill *bt_rfk;
164 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600165 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500166 struct backlight_device *backlight_dev;
167 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600168 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600169 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500170
Seth Forshee135740d2011-09-20 16:55:49 -0500171 int force_fan;
172 int last_key_event;
173 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600174 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600175 int kbd_mode;
176 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700177 int usbsc_bat_level;
Seth Forshee135740d2011-09-20 16:55:49 -0500178
Dan Carpenter592b7462012-01-15 14:28:20 +0300179 unsigned int illumination_supported:1;
180 unsigned int video_supported:1;
181 unsigned int fan_supported:1;
182 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600183 unsigned int ntfy_supported:1;
184 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900185 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600186 unsigned int kbd_illum_supported:1;
187 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600188 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600189 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600190 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700191 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700192 unsigned int usb_rapid_charge_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600193 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500194
Seth Forshee135740d2011-09-20 16:55:49 -0500195 struct mutex mutex;
196};
197
Seth Forshee29cd2932012-01-18 13:44:09 -0600198static struct toshiba_acpi_dev *toshiba_acpi;
199
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800200static const struct acpi_device_id toshiba_device_ids[] = {
201 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100202 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400203 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800204 {"TOS1900", 0},
205 {"", 0},
206};
207MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
208
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800209static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100210 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700211 { KE_KEY, 0x101, { KEY_MUTE } },
212 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
213 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600214 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600215 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
216 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700217 { KE_KEY, 0x13b, { KEY_COFFEE } },
218 { KE_KEY, 0x13c, { KEY_BATTERY } },
219 { KE_KEY, 0x13d, { KEY_SLEEP } },
220 { KE_KEY, 0x13e, { KEY_SUSPEND } },
221 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
222 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
223 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
224 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600225 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100226 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700227 { KE_KEY, 0xb05, { KEY_PROG2 } },
228 { KE_KEY, 0xb06, { KEY_WWW } },
229 { KE_KEY, 0xb07, { KEY_MAIL } },
230 { KE_KEY, 0xb30, { KEY_STOP } },
231 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
232 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
233 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
234 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600235 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
236 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
237 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
238 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
239 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700240 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500241};
242
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200243/* alternative keymap */
244static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
245 {
246 .matches = {
247 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
248 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
249 },
250 },
Azael Avalose6efad72014-08-04 09:21:02 -0600251 {
252 .matches = {
253 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
255 },
256 },
Aaron Lub1bde682014-10-23 16:18:02 +0800257 {
258 .matches = {
259 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
260 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
261 },
262 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200263 {}
264};
265
266static const struct key_entry toshiba_acpi_alt_keymap[] = {
267 { KE_KEY, 0x157, { KEY_MUTE } },
268 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
269 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600270 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200271 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
272 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
273 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
274 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
275 { KE_KEY, 0x158, { KEY_WLAN } },
276 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
277 { KE_END, 0 },
278};
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/* utility
281 */
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 *word = (*word & ~mask) | (mask * value);
286}
287
288/* acpi interface wrappers
289 */
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 acpi_status status;
294
Zhang Rui619400d2013-09-03 08:31:56 +0800295 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500296 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Azael Avalos258c5902014-09-29 20:40:07 -0600299/* Perform a raw configuration call. Here we don't care about input or output
300 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Azael Avalos258c5902014-09-29 20:40:07 -0600302static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
303 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600306 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600308 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 acpi_status status;
310 int i;
311
Azael Avalos258c5902014-09-29 20:40:07 -0600312 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600314 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 in_objs[i].type = ACPI_TYPE_INTEGER;
316 in_objs[i].integer.value = in[i];
317 }
318
319 results.length = sizeof(out_objs);
320 results.pointer = out_objs;
321
Seth Forshee6e02cc72011-09-20 16:55:51 -0500322 status = acpi_evaluate_object(dev->acpi_dev->handle,
323 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400324 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600325 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 for (i = 0; i < out_objs->package.count; ++i) {
327 out[i] = out_objs->package.elements[i].integer.value;
328 }
329 }
330
331 return status;
332}
333
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400334/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *
336 * In addition to the ACPI status, the HCI system returns a result which
337 * may be useful (such as "not supported").
338 */
339
Azael Avalos893f3f62014-09-29 20:40:09 -0600340static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Azael Avalos258c5902014-09-29 20:40:07 -0600342 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
343 u32 out[TCI_WORDS];
344 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600345
346 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Azael Avalos893f3f62014-09-29 20:40:09 -0600349static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Azael Avalos258c5902014-09-29 20:40:07 -0600351 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
352 u32 out[TCI_WORDS];
353 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600354 if (ACPI_FAILURE(status))
355 return TOS_FAILURE;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600358
359 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Azael Avalos893f3f62014-09-29 20:40:09 -0600362static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400363{
Azael Avalos258c5902014-09-29 20:40:07 -0600364 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
365 u32 out[TCI_WORDS];
366 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600367
368 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400369}
370
Azael Avalos893f3f62014-09-29 20:40:09 -0600371static u32 hci_read2(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400372{
Azael Avalos258c5902014-09-29 20:40:07 -0600373 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
374 u32 out[TCI_WORDS];
375 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600376 if (ACPI_FAILURE(status))
377 return TOS_FAILURE;
378
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400379 *out1 = out[2];
380 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600381
382 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400383}
384
Azael Avalos84a62732014-03-25 20:38:29 -0600385/* common sci tasks
386 */
387
388static int sci_open(struct toshiba_acpi_dev *dev)
389{
Azael Avalos258c5902014-09-29 20:40:07 -0600390 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
391 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600392 acpi_status status;
393
Azael Avalos258c5902014-09-29 20:40:07 -0600394 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600395 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600396 pr_err("ACPI call to open SCI failed\n");
397 return 0;
398 }
399
Azael Avalos1864bbc2014-09-29 20:40:08 -0600400 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600401 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600402 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600403 pr_info("Toshiba SCI already opened\n");
404 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600405 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600406 pr_info("Toshiba SCI is not present\n");
407 }
408
409 return 0;
410}
411
412static void sci_close(struct toshiba_acpi_dev *dev)
413{
Azael Avalos258c5902014-09-29 20:40:07 -0600414 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
415 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600416 acpi_status status;
417
Azael Avalos258c5902014-09-29 20:40:07 -0600418 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600419 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600420 pr_err("ACPI call to close SCI failed\n");
421 return;
422 }
423
Azael Avalos1864bbc2014-09-29 20:40:08 -0600424 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600425 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600426 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600427 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600428 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600429 pr_info("Toshiba SCI is not present\n");
430}
431
Azael Avalos893f3f62014-09-29 20:40:09 -0600432static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600433{
Azael Avalos258c5902014-09-29 20:40:07 -0600434 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
435 u32 out[TCI_WORDS];
436 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600437 if (ACPI_FAILURE(status))
438 return TOS_FAILURE;
439
Azael Avalos84a62732014-03-25 20:38:29 -0600440 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600441
442 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600443}
444
Azael Avalos893f3f62014-09-29 20:40:09 -0600445static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600446{
Azael Avalos258c5902014-09-29 20:40:07 -0600447 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
448 u32 out[TCI_WORDS];
449 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600450
451 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600452}
453
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200454/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500455static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200456{
Azael Avalos258c5902014-09-29 20:40:07 -0600457 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
458 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200459 acpi_status status;
460
Azael Avalosfdb79082014-03-25 20:38:30 -0600461 if (!sci_open(dev))
462 return 0;
463
Azael Avalos258c5902014-09-29 20:40:07 -0600464 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600465 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600466 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600467 pr_err("ACPI call to query Illumination support failed\n");
468 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600469 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700470 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200471 return 0;
472 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600473
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200474 return 1;
475}
476
477static void toshiba_illumination_set(struct led_classdev *cdev,
478 enum led_brightness brightness)
479{
Seth Forshee135740d2011-09-20 16:55:49 -0500480 struct toshiba_acpi_dev *dev = container_of(cdev,
481 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600482 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200483
484 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600485 if (!sci_open(dev))
486 return;
487
488 /* Switch the illumination on/off */
489 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600490 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600491 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600492 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600493 pr_err("ACPI call for illumination failed\n");
494 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600495 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600496 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200497 return;
498 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200499}
500
501static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
502{
Seth Forshee135740d2011-09-20 16:55:49 -0500503 struct toshiba_acpi_dev *dev = container_of(cdev,
504 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600505 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200506
507 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600508 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200509 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200510
511 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600512 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600513 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600514 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600515 pr_err("ACPI call for illumination failed\n");
516 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600517 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600518 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200519 return LED_OFF;
520 }
521
Azael Avalosfdb79082014-03-25 20:38:30 -0600522 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200523}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400524
Azael Avalos360f0f32014-03-25 20:38:31 -0600525/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600526static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
527{
Azael Avalos258c5902014-09-29 20:40:07 -0600528 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
529 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600530 acpi_status status;
531
532 if (!sci_open(dev))
533 return 0;
534
Azael Avalos258c5902014-09-29 20:40:07 -0600535 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600536 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600537 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600538 pr_err("ACPI call to query kbd illumination support failed\n");
539 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600540 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600541 pr_info("Keyboard illumination not available\n");
542 return 0;
543 }
544
545 /* Check for keyboard backlight timeout max value,
546 * previous kbd backlight implementation set this to
547 * 0x3c0003, and now the new implementation set this
548 * to 0x3c001a, use this to distinguish between them
549 */
550 if (out[3] == SCI_KBD_TIME_MAX)
551 dev->kbd_type = 2;
552 else
553 dev->kbd_type = 1;
554 /* Get the current keyboard backlight mode */
555 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
556 /* Get the current time (1-60 seconds) */
557 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
558
559 return 1;
560}
561
Azael Avalos360f0f32014-03-25 20:38:31 -0600562static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
563{
564 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600565
566 if (!sci_open(dev))
567 return -EIO;
568
Azael Avalos893f3f62014-09-29 20:40:09 -0600569 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600570 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600571 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600572 pr_err("ACPI call to set KBD backlight status failed\n");
573 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600574 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600575 pr_info("Keyboard backlight status not supported\n");
576 return -ENODEV;
577 }
578
579 return 0;
580}
581
582static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
583{
584 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600585
586 if (!sci_open(dev))
587 return -EIO;
588
Azael Avalos893f3f62014-09-29 20:40:09 -0600589 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600590 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600591 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600592 pr_err("ACPI call to get KBD backlight status failed\n");
593 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600594 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600595 pr_info("Keyboard backlight status not supported\n");
596 return -ENODEV;
597 }
598
599 return 0;
600}
601
602static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
603{
604 struct toshiba_acpi_dev *dev = container_of(cdev,
605 struct toshiba_acpi_dev, kbd_led);
606 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600607
608 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600609 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
610 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600611 pr_err("ACPI call to get the keyboard backlight failed\n");
612 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600613 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600614 pr_info("Keyboard backlight not supported\n");
615 return LED_OFF;
616 }
617
618 return state ? LED_FULL : LED_OFF;
619}
620
621static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
622 enum led_brightness brightness)
623{
624 struct toshiba_acpi_dev *dev = container_of(cdev,
625 struct toshiba_acpi_dev, kbd_led);
626 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600627
628 /* Set the keyboard backlight state */
629 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600630 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
631 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600632 pr_err("ACPI call to set KBD Illumination mode failed\n");
633 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600634 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600635 pr_info("Keyboard backlight not supported\n");
636 return;
637 }
638}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400639
Azael Avalos9d8658a2014-03-25 20:38:32 -0600640/* TouchPad support */
641static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
642{
643 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600644
645 if (!sci_open(dev))
646 return -EIO;
647
Azael Avalos893f3f62014-09-29 20:40:09 -0600648 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600649 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600650 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600651 pr_err("ACPI call to set the touchpad failed\n");
652 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600653 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600654 return -ENODEV;
655 }
656
657 return 0;
658}
659
660static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
661{
662 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600663
664 if (!sci_open(dev))
665 return -EIO;
666
Azael Avalos893f3f62014-09-29 20:40:09 -0600667 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600668 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600669 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600670 pr_err("ACPI call to query the touchpad failed\n");
671 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600672 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600673 return -ENODEV;
674 }
675
676 return 0;
677}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200678
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600679/* Eco Mode support */
680static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
681{
682 acpi_status status;
Azael Avalos258c5902014-09-29 20:40:07 -0600683 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
684 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600685
Azael Avalos258c5902014-09-29 20:40:07 -0600686 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600687 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600688 pr_info("ACPI call to get ECO led failed\n");
689 return 0;
690 }
691
692 return 1;
693}
694
695static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
696{
697 struct toshiba_acpi_dev *dev = container_of(cdev,
698 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600699 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
700 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600701 acpi_status status;
702
Azael Avalos258c5902014-09-29 20:40:07 -0600703 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600704 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600705 pr_err("ACPI call to get ECO led failed\n");
706 return LED_OFF;
707 }
708
709 return out[2] ? LED_FULL : LED_OFF;
710}
711
712static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
713 enum led_brightness brightness)
714{
715 struct toshiba_acpi_dev *dev = container_of(cdev,
716 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600717 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
718 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600719 acpi_status status;
720
721 /* Switch the Eco Mode led on/off */
722 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600723 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600724 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600725 pr_err("ACPI call to set ECO led failed\n");
726 return;
727 }
728}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400729
Azael Avalos5a2813e2014-03-25 20:38:34 -0600730/* Accelerometer support */
731static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
732{
Azael Avalos258c5902014-09-29 20:40:07 -0600733 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
734 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600735 acpi_status status;
736
737 /* Check if the accelerometer call exists,
738 * this call also serves as initialization
739 */
Azael Avalos258c5902014-09-29 20:40:07 -0600740 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600741 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600742 pr_err("ACPI call to query the accelerometer failed\n");
743 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600744 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
745 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600746 pr_err("Accelerometer not initialized\n");
747 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600748 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600749 pr_info("Accelerometer not supported\n");
750 return -ENODEV;
751 }
752
753 return 0;
754}
755
756static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
757 u32 *xy, u32 *z)
758{
Azael Avalos258c5902014-09-29 20:40:07 -0600759 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
760 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600761 acpi_status status;
762
763 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600764 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600765 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600766 pr_err("ACPI call to query the accelerometer failed\n");
767 return -EIO;
768 }
769
770 *xy = out[2];
771 *z = out[4];
772
773 return 0;
774}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600775
Azael Avalose26ffe52015-01-18 18:30:22 -0700776/* Sleep (Charge and Music) utilities support */
777static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
778 u32 *mode)
779{
780 u32 result;
781
782 if (!sci_open(dev))
783 return -EIO;
784
785 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
786 sci_close(dev);
787 if (result == TOS_FAILURE) {
788 pr_err("ACPI call to set USB S&C mode failed\n");
789 return -EIO;
790 } else if (result == TOS_NOT_SUPPORTED) {
791 pr_info("USB Sleep and Charge not supported\n");
792 return -ENODEV;
793 } else if (result == TOS_INPUT_DATA_ERROR) {
794 return -EIO;
795 }
796
797 return 0;
798}
799
800static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
801 u32 mode)
802{
803 u32 result;
804
805 if (!sci_open(dev))
806 return -EIO;
807
808 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
809 sci_close(dev);
810 if (result == TOS_FAILURE) {
811 pr_err("ACPI call to set USB S&C mode failed\n");
812 return -EIO;
813 } else if (result == TOS_NOT_SUPPORTED) {
814 pr_info("USB Sleep and Charge not supported\n");
815 return -ENODEV;
816 } else if (result == TOS_INPUT_DATA_ERROR) {
817 return -EIO;
818 }
819
820 return 0;
821}
822
Azael Avalos182bcaa2015-01-18 18:30:23 -0700823static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
824 u32 *mode)
825{
826 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
827 u32 out[TCI_WORDS];
828 acpi_status status;
829
830 if (!sci_open(dev))
831 return -EIO;
832
833 in[5] = SCI_USB_CHARGE_BAT_LVL;
834 status = tci_raw(dev, in, out);
835 sci_close(dev);
836 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
837 pr_err("ACPI call to get USB S&C battery level failed\n");
838 return -EIO;
839 } else if (out[0] == TOS_NOT_SUPPORTED) {
840 pr_info("USB Sleep and Charge not supported\n");
841 return -ENODEV;
842 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
843 return -EIO;
844 }
845
846 *mode = out[2];
847
848 return 0;
849}
850
851static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
852 u32 mode)
853{
854 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
855 u32 out[TCI_WORDS];
856 acpi_status status;
857
858 if (!sci_open(dev))
859 return -EIO;
860
861 in[2] = mode;
862 in[5] = SCI_USB_CHARGE_BAT_LVL;
863 status = tci_raw(dev, in, out);
864 sci_close(dev);
865 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
866 pr_err("ACPI call to set USB S&C battery level failed\n");
867 return -EIO;
868 } else if (out[0] == TOS_NOT_SUPPORTED) {
869 pr_info("USB Sleep and Charge not supported\n");
870 return -ENODEV;
871 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
872 return -EIO;
873 }
874
875 return 0;
876}
877
Azael Avalosbb3fe012015-01-18 18:30:24 -0700878static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
879 u32 *state)
880{
881 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
882 u32 out[TCI_WORDS];
883 acpi_status status;
884
885 if (!sci_open(dev))
886 return -EIO;
887
888 in[5] = SCI_USB_CHARGE_RAPID_DSP;
889 status = tci_raw(dev, in, out);
890 sci_close(dev);
891 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
892 pr_err("ACPI call to get USB S&C battery level failed\n");
893 return -EIO;
894 } else if (out[0] == TOS_NOT_SUPPORTED ||
895 out[0] == TOS_INPUT_DATA_ERROR) {
896 pr_info("USB Sleep and Charge not supported\n");
897 return -ENODEV;
898 }
899
900 *state = out[2];
901
902 return 0;
903}
904
905static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
906 u32 state)
907{
908 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
909 u32 out[TCI_WORDS];
910 acpi_status status;
911
912 if (!sci_open(dev))
913 return -EIO;
914
915 in[2] = state;
916 in[5] = SCI_USB_CHARGE_RAPID_DSP;
917 status = tci_raw(dev, in, out);
918 sci_close(dev);
919 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
920 pr_err("ACPI call to set USB S&C battery level failed\n");
921 return -EIO;
922 } else if (out[0] == TOS_NOT_SUPPORTED) {
923 pr_info("USB Sleep and Charge not supported\n");
924 return -ENODEV;
925 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
926 return -EIO;
927 }
928
929 return 0;
930}
931
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400932/* Bluetooth rfkill handlers */
933
Seth Forshee135740d2011-09-20 16:55:49 -0500934static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400935{
936 u32 hci_result;
937 u32 value, value2;
938
939 value = 0;
940 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600941 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600942 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400943 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
944
945 return hci_result;
946}
947
Seth Forshee135740d2011-09-20 16:55:49 -0500948static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400949{
950 u32 hci_result;
951 u32 value, value2;
952
953 value = 0;
954 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -0600955 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400956
957 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
958 return hci_result;
959}
960
Johannes Berg19d337d2009-06-02 13:01:37 +0200961static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400962{
Johannes Berg19d337d2009-06-02 13:01:37 +0200963 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400964 u32 result1, result2;
965 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200966 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400967 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400968
Johannes Berg19d337d2009-06-02 13:01:37 +0200969 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400970
971 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600972 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500973 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200974 goto out;
975 }
976
977 if (!radio_state) {
978 err = 0;
979 goto out;
980 }
981
Azael Avalos893f3f62014-09-29 20:40:09 -0600982 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
983 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400984
Azael Avalos1864bbc2014-09-29 20:40:08 -0600985 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500986 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200987 else
988 err = 0;
989 out:
990 mutex_unlock(&dev->mutex);
991 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400992}
993
Johannes Berg19d337d2009-06-02 13:01:37 +0200994static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400995{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400996 bool new_rfk_state;
997 bool value;
998 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200999 struct toshiba_acpi_dev *dev = data;
1000
1001 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001002
Seth Forshee135740d2011-09-20 16:55:49 -05001003 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001004 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001005 /* Can't do anything useful */
1006 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001007 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001008 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001009
1010 new_rfk_state = value;
1011
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001012 mutex_unlock(&dev->mutex);
1013
Johannes Berg19d337d2009-06-02 13:01:37 +02001014 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1015 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001016}
1017
Johannes Berg19d337d2009-06-02 13:01:37 +02001018static const struct rfkill_ops toshiba_rfk_ops = {
1019 .set_block = bt_rfkill_set_block,
1020 .poll = bt_rfkill_poll,
1021};
1022
Akio Idehara121b7b02012-04-06 01:46:43 +09001023static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1024{
1025 u32 hci_result;
1026 u32 status;
1027
Azael Avalos893f3f62014-09-29 20:40:09 -06001028 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001029 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001030 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001031}
1032
1033static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1034{
1035 u32 hci_result;
1036 u32 value = !enable;
1037
Azael Avalos893f3f62014-09-29 20:40:09 -06001038 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001039 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001040}
1041
Len Brown4be44fc2005-08-05 00:44:28 -04001042static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Seth Forshee62cce752012-04-19 11:23:50 -05001044static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 u32 hci_result;
1047 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001048 int brightness = 0;
1049
1050 if (dev->tr_backlight_supported) {
1051 bool enabled;
1052 int ret = get_tr_backlight_status(dev, &enabled);
1053 if (ret)
1054 return ret;
1055 if (enabled)
1056 return 0;
1057 brightness++;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Azael Avalos893f3f62014-09-29 20:40:09 -06001060 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001061 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001062 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001063
1064 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001065}
1066
Seth Forshee62cce752012-04-19 11:23:50 -05001067static int get_lcd_brightness(struct backlight_device *bd)
1068{
1069 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1070 return __get_lcd_brightness(dev);
1071}
1072
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001073static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001074{
Seth Forshee135740d2011-09-20 16:55:49 -05001075 struct toshiba_acpi_dev *dev = m->private;
1076 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001077 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001078
Seth Forshee135740d2011-09-20 16:55:49 -05001079 if (!dev->backlight_dev)
1080 return -ENODEV;
1081
Akio Idehara121b7b02012-04-06 01:46:43 +09001082 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001083 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001084 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001085 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001086 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001090 pr_err("Error reading LCD brightness\n");
1091 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001092}
1093
1094static int lcd_proc_open(struct inode *inode, struct file *file)
1095{
Al Virod9dda782013-03-31 18:16:14 -04001096 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Seth Forshee62cce752012-04-19 11:23:50 -05001099static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001100{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001101 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001102
Akio Idehara121b7b02012-04-06 01:46:43 +09001103 if (dev->tr_backlight_supported) {
1104 bool enable = !value;
1105 int ret = set_tr_backlight_status(dev, enable);
1106 if (ret)
1107 return ret;
1108 if (value)
1109 value--;
1110 }
1111
Azael Avalosa39f46d2014-11-24 19:29:36 -07001112 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1113 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1114 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001115}
1116
1117static int set_lcd_status(struct backlight_device *bd)
1118{
Seth Forshee135740d2011-09-20 16:55:49 -05001119 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Seth Forshee62cce752012-04-19 11:23:50 -05001120 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001121}
1122
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001123static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1124 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Al Virod9dda782013-03-31 18:16:14 -04001126 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001127 char cmd[42];
1128 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001130 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001131 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001133 len = min(count, sizeof(cmd) - 1);
1134 if (copy_from_user(cmd, buf, len))
1135 return -EFAULT;
1136 cmd[len] = '\0';
1137
1138 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001139 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001140 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001141 if (ret == 0)
1142 ret = count;
1143 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001144 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001145 }
Holger Machtc9263552006-10-20 14:30:29 -07001146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001149static const struct file_operations lcd_proc_fops = {
1150 .owner = THIS_MODULE,
1151 .open = lcd_proc_open,
1152 .read = seq_read,
1153 .llseek = seq_lseek,
1154 .release = single_release,
1155 .write = lcd_proc_write,
1156};
1157
Seth Forshee36d03f92011-09-20 16:55:53 -05001158static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1159{
1160 u32 hci_result;
1161
Azael Avalos893f3f62014-09-29 20:40:09 -06001162 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001163 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001164}
1165
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001166static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Seth Forshee135740d2011-09-20 16:55:49 -05001168 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001170 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Seth Forshee36d03f92011-09-20 16:55:53 -05001172 ret = get_video_status(dev, &value);
1173 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1175 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001176 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001177 seq_printf(m, "lcd_out: %d\n", is_lcd);
1178 seq_printf(m, "crt_out: %d\n", is_crt);
1179 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Seth Forshee36d03f92011-09-20 16:55:53 -05001182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001185static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Al Virod9dda782013-03-31 18:16:14 -04001187 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001188}
1189
1190static ssize_t video_proc_write(struct file *file, const char __user *buf,
1191 size_t count, loff_t *pos)
1192{
Al Virod9dda782013-03-31 18:16:14 -04001193 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001194 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001195 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int value;
1197 int remain = count;
1198 int lcd_out = -1;
1199 int crt_out = -1;
1200 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001201 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001203 cmd = kmalloc(count + 1, GFP_KERNEL);
1204 if (!cmd)
1205 return -ENOMEM;
1206 if (copy_from_user(cmd, buf, count)) {
1207 kfree(cmd);
1208 return -EFAULT;
1209 }
1210 cmd[count] = '\0';
1211
1212 buffer = cmd;
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* scan expression. Multiple expressions may be delimited with ;
1215 *
1216 * NOTE: to keep scanning simple, invalid fields are ignored
1217 */
1218 while (remain) {
1219 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1220 lcd_out = value & 1;
1221 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1222 crt_out = value & 1;
1223 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1224 tv_out = value & 1;
1225 /* advance to one character past the next ; */
1226 do {
1227 ++buffer;
1228 --remain;
1229 }
Len Brown4be44fc2005-08-05 00:44:28 -04001230 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001233 kfree(cmd);
1234
Seth Forshee36d03f92011-09-20 16:55:53 -05001235 ret = get_video_status(dev, &video_out);
1236 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001237 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (lcd_out != -1)
1239 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1240 if (crt_out != -1)
1241 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1242 if (tv_out != -1)
1243 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1244 /* To avoid unnecessary video disruption, only write the new
1245 * video setting if something changed. */
1246 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001247 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001250 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001253static const struct file_operations video_proc_fops = {
1254 .owner = THIS_MODULE,
1255 .open = video_proc_open,
1256 .read = seq_read,
1257 .llseek = seq_lseek,
1258 .release = single_release,
1259 .write = video_proc_write,
1260};
1261
Seth Forshee36d03f92011-09-20 16:55:53 -05001262static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1263{
1264 u32 hci_result;
1265
Azael Avalos893f3f62014-09-29 20:40:09 -06001266 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001267 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001268}
1269
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001270static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Seth Forshee135740d2011-09-20 16:55:49 -05001272 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001273 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 u32 value;
1275
Seth Forshee36d03f92011-09-20 16:55:53 -05001276 ret = get_fan_status(dev, &value);
1277 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001278 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001279 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Seth Forshee36d03f92011-09-20 16:55:53 -05001282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001285static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Al Virod9dda782013-03-31 18:16:14 -04001287 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001288}
1289
1290static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1291 size_t count, loff_t *pos)
1292{
Al Virod9dda782013-03-31 18:16:14 -04001293 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001294 char cmd[42];
1295 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int value;
1297 u32 hci_result;
1298
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001299 len = min(count, sizeof(cmd) - 1);
1300 if (copy_from_user(cmd, buf, len))
1301 return -EFAULT;
1302 cmd[len] = '\0';
1303
1304 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001305 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001306 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001307 if (hci_result != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001308 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 else
Seth Forshee135740d2011-09-20 16:55:49 -05001310 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 } else {
1312 return -EINVAL;
1313 }
1314
1315 return count;
1316}
1317
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001318static const struct file_operations fan_proc_fops = {
1319 .owner = THIS_MODULE,
1320 .open = fan_proc_open,
1321 .read = seq_read,
1322 .llseek = seq_lseek,
1323 .release = single_release,
1324 .write = fan_proc_write,
1325};
1326
1327static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Seth Forshee135740d2011-09-20 16:55:49 -05001329 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 u32 hci_result;
1331 u32 value;
1332
Seth Forshee11948b92011-11-16 17:37:45 -06001333 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001334 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001335 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001336 dev->key_event_valid = 1;
1337 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001338 } else if (hci_result == TOS_FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001340 } else if (hci_result == TOS_NOT_SUPPORTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* This is a workaround for an unresolved issue on
1342 * some machines where system events sporadically
1343 * become disabled. */
Azael Avalos893f3f62014-09-29 20:40:09 -06001344 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001345 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001347 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001348 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
1351
Seth Forshee135740d2011-09-20 16:55:49 -05001352 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1353 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001357static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Al Virod9dda782013-03-31 18:16:14 -04001359 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001360}
1361
1362static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1363 size_t count, loff_t *pos)
1364{
Al Virod9dda782013-03-31 18:16:14 -04001365 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001366 char cmd[42];
1367 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int value;
1369
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001370 len = min(count, sizeof(cmd) - 1);
1371 if (copy_from_user(cmd, buf, len))
1372 return -EFAULT;
1373 cmd[len] = '\0';
1374
1375 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -05001376 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 } else {
1378 return -EINVAL;
1379 }
1380
1381 return count;
1382}
1383
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001384static const struct file_operations keys_proc_fops = {
1385 .owner = THIS_MODULE,
1386 .open = keys_proc_open,
1387 .read = seq_read,
1388 .llseek = seq_lseek,
1389 .release = single_release,
1390 .write = keys_proc_write,
1391};
1392
1393static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001395 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1396 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001400static int version_proc_open(struct inode *inode, struct file *file)
1401{
Al Virod9dda782013-03-31 18:16:14 -04001402 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001403}
1404
1405static const struct file_operations version_proc_fops = {
1406 .owner = THIS_MODULE,
1407 .open = version_proc_open,
1408 .read = seq_read,
1409 .llseek = seq_lseek,
1410 .release = single_release,
1411};
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/* proc and module init
1414 */
1415
1416#define PROC_TOSHIBA "toshiba"
1417
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001418static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Seth Forshee36d03f92011-09-20 16:55:53 -05001420 if (dev->backlight_dev)
1421 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1422 &lcd_proc_fops, dev);
1423 if (dev->video_supported)
1424 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1425 &video_proc_fops, dev);
1426 if (dev->fan_supported)
1427 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1428 &fan_proc_fops, dev);
1429 if (dev->hotkey_dev)
1430 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1431 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001432 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1433 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Seth Forshee36d03f92011-09-20 16:55:53 -05001436static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Seth Forshee36d03f92011-09-20 16:55:53 -05001438 if (dev->backlight_dev)
1439 remove_proc_entry("lcd", toshiba_proc_dir);
1440 if (dev->video_supported)
1441 remove_proc_entry("video", toshiba_proc_dir);
1442 if (dev->fan_supported)
1443 remove_proc_entry("fan", toshiba_proc_dir);
1444 if (dev->hotkey_dev)
1445 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001446 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Lionel Debrouxacc24722010-11-16 14:14:02 +01001449static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001450 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001451 .get_brightness = get_lcd_brightness,
1452 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001453};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001454
Azael Avalos360f0f32014-03-25 20:38:31 -06001455/*
1456 * Sysfs files
1457 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001458static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1459 struct device_attribute *attr,
1460 const char *buf, size_t count);
1461static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1462 struct device_attribute *attr,
1463 char *buf);
1464static ssize_t toshiba_kbd_type_show(struct device *dev,
1465 struct device_attribute *attr,
1466 char *buf);
1467static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1468 struct device_attribute *attr,
1469 char *buf);
1470static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1471 struct device_attribute *attr,
1472 const char *buf, size_t count);
1473static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1474 struct device_attribute *attr,
1475 char *buf);
1476static ssize_t toshiba_touchpad_store(struct device *dev,
1477 struct device_attribute *attr,
1478 const char *buf, size_t count);
1479static ssize_t toshiba_touchpad_show(struct device *dev,
1480 struct device_attribute *attr,
1481 char *buf);
1482static ssize_t toshiba_position_show(struct device *dev,
1483 struct device_attribute *attr,
1484 char *buf);
Azael Avalose26ffe52015-01-18 18:30:22 -07001485static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1486 struct device_attribute *attr,
1487 char *buf);
1488static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001491static ssize_t sleep_functions_on_battery_show(struct device *dev,
1492 struct device_attribute *attr,
1493 char *buf);
1494static ssize_t sleep_functions_on_battery_store(struct device *dev,
1495 struct device_attribute *attr,
1496 const char *buf, size_t count);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001497static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
1498 struct device_attribute *attr,
1499 char *buf);
1500static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
1501 struct device_attribute *attr,
1502 const char *buf, size_t count);
Azael Avalos93f8c162014-09-12 18:50:36 -06001503
1504static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1505 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1506static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1507static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1508 toshiba_available_kbd_modes_show, NULL);
1509static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1510 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1511static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1512 toshiba_touchpad_show, toshiba_touchpad_store);
1513static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
Azael Avalose26ffe52015-01-18 18:30:22 -07001514static DEVICE_ATTR(usb_sleep_charge, S_IRUGO | S_IWUSR,
1515 toshiba_usb_sleep_charge_show,
1516 toshiba_usb_sleep_charge_store);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001517static DEVICE_ATTR(sleep_functions_on_battery, S_IRUGO | S_IWUSR,
1518 sleep_functions_on_battery_show,
1519 sleep_functions_on_battery_store);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001520static DEVICE_ATTR(usb_rapid_charge, S_IRUGO | S_IWUSR,
1521 toshiba_usb_rapid_charge_show,
1522 toshiba_usb_rapid_charge_store);
Azael Avalos93f8c162014-09-12 18:50:36 -06001523
1524static struct attribute *toshiba_attributes[] = {
1525 &dev_attr_kbd_backlight_mode.attr,
1526 &dev_attr_kbd_type.attr,
1527 &dev_attr_available_kbd_modes.attr,
1528 &dev_attr_kbd_backlight_timeout.attr,
1529 &dev_attr_touchpad.attr,
1530 &dev_attr_position.attr,
Azael Avalose26ffe52015-01-18 18:30:22 -07001531 &dev_attr_usb_sleep_charge.attr,
Azael Avalos182bcaa2015-01-18 18:30:23 -07001532 &dev_attr_sleep_functions_on_battery.attr,
Azael Avalosbb3fe012015-01-18 18:30:24 -07001533 &dev_attr_usb_rapid_charge.attr,
Azael Avalos93f8c162014-09-12 18:50:36 -06001534 NULL,
1535};
1536
1537static umode_t toshiba_sysfs_is_visible(struct kobject *,
1538 struct attribute *, int);
1539
1540static struct attribute_group toshiba_attr_group = {
1541 .is_visible = toshiba_sysfs_is_visible,
1542 .attrs = toshiba_attributes,
1543};
Azael Avalos360f0f32014-03-25 20:38:31 -06001544
1545static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1546 struct device_attribute *attr,
1547 const char *buf, size_t count)
1548{
1549 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001550 int mode;
1551 int time;
1552 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001553
Dan Carpenteraeaac092014-09-03 14:44:37 +03001554
1555 ret = kstrtoint(buf, 0, &mode);
1556 if (ret)
1557 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001558
1559 /* Check for supported modes depending on keyboard backlight type */
1560 if (toshiba->kbd_type == 1) {
1561 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1562 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1563 return -EINVAL;
1564 } else if (toshiba->kbd_type == 2) {
1565 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1566 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1567 mode != SCI_KBD_MODE_OFF)
1568 return -EINVAL;
1569 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001570
1571 /* Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001572 * Auto - KBD backlight turns off automatically in given time
1573 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001574 * ON - KBD backlight is always on
1575 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001576 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001577
1578 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001579 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001580 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001581 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001582
1583 /* OR the "base time" to the actual method format */
1584 if (toshiba->kbd_type == 1) {
1585 /* Type 1 requires the current mode */
1586 time |= toshiba->kbd_mode;
1587 } else if (toshiba->kbd_type == 2) {
1588 /* Type 2 requires the desired mode */
1589 time |= mode;
1590 }
1591
Dan Carpenteraeaac092014-09-03 14:44:37 +03001592 ret = toshiba_kbd_illum_status_set(toshiba, time);
1593 if (ret)
1594 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001595
Azael Avalos360f0f32014-03-25 20:38:31 -06001596 toshiba->kbd_mode = mode;
1597 }
1598
1599 return count;
1600}
1601
1602static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1603 struct device_attribute *attr,
1604 char *buf)
1605{
1606 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1607 u32 time;
1608
1609 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1610 return -EIO;
1611
Azael Avalos93f8c162014-09-12 18:50:36 -06001612 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1613}
1614
1615static ssize_t toshiba_kbd_type_show(struct device *dev,
1616 struct device_attribute *attr,
1617 char *buf)
1618{
1619 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1620
1621 return sprintf(buf, "%d\n", toshiba->kbd_type);
1622}
1623
1624static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1625 struct device_attribute *attr,
1626 char *buf)
1627{
1628 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1629
1630 if (toshiba->kbd_type == 1)
1631 return sprintf(buf, "%x %x\n",
1632 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1633
1634 return sprintf(buf, "%x %x %x\n",
1635 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001636}
1637
1638static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1639 struct device_attribute *attr,
1640 const char *buf, size_t count)
1641{
1642 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001643 int time;
1644 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001645
Azael Avaloseabde0f2014-10-04 12:02:21 -06001646 ret = kstrtoint(buf, 0, &time);
1647 if (ret)
1648 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001649
Azael Avaloseabde0f2014-10-04 12:02:21 -06001650 /* Check for supported values depending on kbd_type */
1651 if (toshiba->kbd_type == 1) {
1652 if (time < 0 || time > 60)
1653 return -EINVAL;
1654 } else if (toshiba->kbd_type == 2) {
1655 if (time < 1 || time > 60)
1656 return -EINVAL;
1657 }
1658
1659 /* Set the Keyboard Backlight Timeout */
1660
1661 /* Only make a change if the actual timeout has changed */
1662 if (toshiba->kbd_time != time) {
1663 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001664 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001665 /* OR the "base time" to the actual method format */
1666 if (toshiba->kbd_type == 1)
1667 time |= SCI_KBD_MODE_FNZ;
1668 else if (toshiba->kbd_type == 2)
1669 time |= SCI_KBD_MODE_AUTO;
1670
1671 ret = toshiba_kbd_illum_status_set(toshiba, time);
1672 if (ret)
1673 return ret;
1674
Azael Avalos360f0f32014-03-25 20:38:31 -06001675 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1676 }
1677
1678 return count;
1679}
1680
1681static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1684{
1685 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1686 u32 time;
1687
1688 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1689 return -EIO;
1690
1691 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1692}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001693
Azael Avalos9d8658a2014-03-25 20:38:32 -06001694static ssize_t toshiba_touchpad_store(struct device *dev,
1695 struct device_attribute *attr,
1696 const char *buf, size_t count)
1697{
1698 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1699 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001700 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001701
1702 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001703 ret = kstrtoint(buf, 0, &state);
1704 if (ret)
1705 return ret;
1706 if (state != 0 && state != 1)
1707 return -EINVAL;
1708
1709 ret = toshiba_touchpad_set(toshiba, state);
1710 if (ret)
1711 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001712
1713 return count;
1714}
1715
1716static ssize_t toshiba_touchpad_show(struct device *dev,
1717 struct device_attribute *attr, char *buf)
1718{
1719 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1720 u32 state;
1721 int ret;
1722
1723 ret = toshiba_touchpad_get(toshiba, &state);
1724 if (ret < 0)
1725 return ret;
1726
1727 return sprintf(buf, "%i\n", state);
1728}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001729
Azael Avalos5a2813e2014-03-25 20:38:34 -06001730static ssize_t toshiba_position_show(struct device *dev,
1731 struct device_attribute *attr, char *buf)
1732{
1733 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1734 u32 xyval, zval, tmp;
1735 u16 x, y, z;
1736 int ret;
1737
1738 xyval = zval = 0;
1739 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1740 if (ret < 0)
1741 return ret;
1742
1743 x = xyval & HCI_ACCEL_MASK;
1744 tmp = xyval >> HCI_MISC_SHIFT;
1745 y = tmp & HCI_ACCEL_MASK;
1746 z = zval & HCI_ACCEL_MASK;
1747
1748 return sprintf(buf, "%d %d %d\n", x, y, z);
1749}
Azael Avalos360f0f32014-03-25 20:38:31 -06001750
Azael Avalose26ffe52015-01-18 18:30:22 -07001751static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1752 struct device_attribute *attr,
1753 char *buf)
1754{
1755 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1756 u32 mode;
1757 int ret;
1758
1759 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1760 if (ret < 0)
1761 return ret;
1762
1763 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1764}
1765
1766static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1767 struct device_attribute *attr,
1768 const char *buf, size_t count)
1769{
1770 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1771 u32 mode;
1772 int state;
1773 int ret;
1774
1775 ret = kstrtoint(buf, 0, &state);
1776 if (ret)
1777 return ret;
1778 /* Check for supported values, where:
1779 * 0 - Disabled
1780 * 1 - Alternate (Non USB conformant devices that require more power)
1781 * 2 - Auto (USB conformant devices)
1782 */
1783 if (state != 0 && state != 1 && state != 2)
1784 return -EINVAL;
1785
1786 /* Set the USB charging mode to internal value */
1787 if (state == 0)
1788 mode = SCI_USB_CHARGE_DISABLED;
1789 else if (state == 1)
1790 mode = SCI_USB_CHARGE_ALTERNATE;
1791 else if (state == 2)
1792 mode = SCI_USB_CHARGE_AUTO;
1793
1794 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1795 if (ret)
1796 return ret;
1797
1798 return count;
1799}
1800
Azael Avalos182bcaa2015-01-18 18:30:23 -07001801static ssize_t sleep_functions_on_battery_show(struct device *dev,
1802 struct device_attribute *attr,
1803 char *buf)
1804{
1805 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1806 u32 state;
1807 int bat_lvl;
1808 int status;
1809 int ret;
1810 int tmp;
1811
1812 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1813 if (ret < 0)
1814 return ret;
1815
1816 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1817 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1818 status = (tmp == 0x4) ? 1 : 0;
1819 /* Determine the battery level set */
1820 bat_lvl = state >> HCI_MISC_SHIFT;
1821
1822 return sprintf(buf, "%d %d\n", status, bat_lvl);
1823}
1824
1825static ssize_t sleep_functions_on_battery_store(struct device *dev,
1826 struct device_attribute *attr,
1827 const char *buf, size_t count)
1828{
1829 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1830 u32 status;
1831 int value;
1832 int ret;
1833 int tmp;
1834
1835 ret = kstrtoint(buf, 0, &value);
1836 if (ret)
1837 return ret;
1838
1839 /* Set the status of the function:
1840 * 0 - Disabled
1841 * 1-100 - Enabled
1842 */
1843 if (value < 0 || value > 100)
1844 return -EINVAL;
1845
1846 if (value == 0) {
1847 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1848 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1849 } else {
1850 tmp = value << HCI_MISC_SHIFT;
1851 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1852 }
1853 ret = toshiba_sleep_functions_status_set(toshiba, status);
1854 if (ret < 0)
1855 return ret;
1856
1857 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
1858
1859 return count;
1860}
1861
Azael Avalosbb3fe012015-01-18 18:30:24 -07001862static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
1863 struct device_attribute *attr,
1864 char *buf)
1865{
1866 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1867 u32 state;
1868 int ret;
1869
1870 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
1871 if (ret < 0)
1872 return ret;
1873
1874 return sprintf(buf, "%d\n", state);
1875}
1876
1877static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
1878 struct device_attribute *attr,
1879 const char *buf, size_t count)
1880{
1881 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1882 int state;
1883 int ret;
1884
1885 ret = kstrtoint(buf, 0, &state);
1886 if (ret)
1887 return ret;
1888 if (state != 0 && state != 1)
1889 return -EINVAL;
1890
1891 ret = toshiba_usb_rapid_charge_set(toshiba, state);
1892 if (ret)
1893 return ret;
1894
1895 return count;
1896}
1897
Azael Avalos360f0f32014-03-25 20:38:31 -06001898static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1899 struct attribute *attr, int idx)
1900{
1901 struct device *dev = container_of(kobj, struct device, kobj);
1902 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1903 bool exists = true;
1904
1905 if (attr == &dev_attr_kbd_backlight_mode.attr)
1906 exists = (drv->kbd_illum_supported) ? true : false;
1907 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1908 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001909 else if (attr == &dev_attr_touchpad.attr)
1910 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06001911 else if (attr == &dev_attr_position.attr)
1912 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07001913 else if (attr == &dev_attr_usb_sleep_charge.attr)
1914 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07001915 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
1916 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07001917 else if (attr == &dev_attr_usb_rapid_charge.attr)
1918 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06001919
1920 return exists ? attr->mode : 0;
1921}
1922
Azael Avalos1f28f292014-12-04 20:22:45 -07001923/*
1924 * Hotkeys
1925 */
1926static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
1927{
1928 acpi_status status;
1929 u32 result;
1930
1931 status = acpi_evaluate_object(dev->acpi_dev->handle,
1932 "ENAB", NULL, NULL);
1933 if (ACPI_FAILURE(status))
1934 return -ENODEV;
1935
1936 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
1937 if (result == TOS_FAILURE)
1938 return -EIO;
1939 else if (result == TOS_NOT_SUPPORTED)
1940 return -ENODEV;
1941
1942 return 0;
1943}
1944
Seth Forshee29cd2932012-01-18 13:44:09 -06001945static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1946 struct serio *port)
1947{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03001948 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06001949 return false;
1950
1951 if (unlikely(data == 0xe0))
1952 return false;
1953
1954 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1955 schedule_work(&toshiba_acpi->hotkey_work);
1956 return true;
1957 }
1958
1959 return false;
1960}
1961
1962static void toshiba_acpi_hotkey_work(struct work_struct *work)
1963{
1964 acpi_handle ec_handle = ec_get_handle();
1965 acpi_status status;
1966
1967 if (!ec_handle)
1968 return;
1969
1970 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1971 if (ACPI_FAILURE(status))
1972 pr_err("ACPI NTFY method execution failed\n");
1973}
1974
1975/*
1976 * Returns hotkey scancode, or < 0 on failure.
1977 */
1978static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1979{
Zhang Rui74facaf2013-09-03 08:32:15 +08001980 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001981 acpi_status status;
1982
Zhang Rui74facaf2013-09-03 08:32:15 +08001983 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1984 NULL, &value);
1985 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001986 pr_err("ACPI INFO method execution failed\n");
1987 return -EIO;
1988 }
1989
Zhang Rui74facaf2013-09-03 08:32:15 +08001990 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001991}
1992
1993static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1994 int scancode)
1995{
1996 if (scancode == 0x100)
1997 return;
1998
1999 /* act on key press; ignore key release */
2000 if (scancode & 0x80)
2001 return;
2002
2003 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2004 pr_info("Unknown key %x\n", scancode);
2005}
2006
Azael Avalos71454d72014-12-04 20:22:46 -07002007static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2008{
2009 u32 hci_result, value;
2010 int retries = 3;
2011 int scancode;
2012
2013 if (dev->info_supported) {
2014 scancode = toshiba_acpi_query_hotkey(dev);
2015 if (scancode < 0)
2016 pr_err("Failed to query hotkey event\n");
2017 else if (scancode != 0)
2018 toshiba_acpi_report_hotkey(dev, scancode);
2019 } else if (dev->system_event_supported) {
2020 do {
2021 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2022 switch (hci_result) {
2023 case TOS_SUCCESS:
2024 toshiba_acpi_report_hotkey(dev, (int)value);
2025 break;
2026 case TOS_NOT_SUPPORTED:
2027 /*
2028 * This is a workaround for an unresolved
2029 * issue on some machines where system events
2030 * sporadically become disabled.
2031 */
2032 hci_result =
2033 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2034 pr_notice("Re-enabled hotkeys\n");
2035 /* fall through */
2036 default:
2037 retries--;
2038 break;
2039 }
2040 } while (retries && hci_result != TOS_FIFO_EMPTY);
2041 }
2042}
2043
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002044static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002045{
Zhang Ruie2e19602013-09-03 08:32:06 +08002046 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05002047 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002048 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002049 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05002050
Seth Forshee135740d2011-09-20 16:55:49 -05002051 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002052 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002053 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002054
2055 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002056 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002057 dev->hotkey_dev->id.bustype = BUS_HOST;
2058
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002059 if (dmi_check_system(toshiba_alt_keymap_dmi))
2060 keymap = toshiba_acpi_alt_keymap;
2061 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002062 if (error)
2063 goto err_free_dev;
2064
Seth Forshee29cd2932012-01-18 13:44:09 -06002065 /*
2066 * For some machines the SCI responsible for providing hotkey
2067 * notification doesn't fire. We can trigger the notification
2068 * whenever the Fn key is pressed using the NTFY method, if
2069 * supported, so if it's present set up an i8042 key filter
2070 * for this purpose.
2071 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002072 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002073 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002074 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2075
2076 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2077 if (error) {
2078 pr_err("Error installing key filter\n");
2079 goto err_free_keymap;
2080 }
2081
2082 dev->ntfy_supported = 1;
2083 }
2084
2085 /*
2086 * Determine hotkey query interface. Prefer using the INFO
2087 * method when it is available.
2088 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002089 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002090 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002091 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002092 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002093 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002094 dev->system_event_supported = 1;
2095 }
2096
2097 if (!dev->info_supported && !dev->system_event_supported) {
2098 pr_warn("No hotkey query interface found\n");
2099 goto err_remove_filter;
2100 }
2101
Azael Avalos1f28f292014-12-04 20:22:45 -07002102 error = toshiba_acpi_enable_hotkeys(dev);
2103 if (error) {
Seth Forshee135740d2011-09-20 16:55:49 -05002104 pr_info("Unable to enable hotkeys\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002105 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002106 }
2107
2108 error = input_register_device(dev->hotkey_dev);
2109 if (error) {
2110 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002111 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002112 }
2113
2114 return 0;
2115
Seth Forshee29cd2932012-01-18 13:44:09 -06002116 err_remove_filter:
2117 if (dev->ntfy_supported)
2118 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002119 err_free_keymap:
2120 sparse_keymap_free(dev->hotkey_dev);
2121 err_free_dev:
2122 input_free_device(dev->hotkey_dev);
2123 dev->hotkey_dev = NULL;
2124 return error;
2125}
2126
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002127static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002128{
2129 struct backlight_properties props;
2130 int brightness;
2131 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002132 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002133
2134 /*
2135 * Some machines don't support the backlight methods at all, and
2136 * others support it read-only. Either of these is pretty useless,
2137 * so only register the backlight device if the backlight method
2138 * supports both reads and writes.
2139 */
2140 brightness = __get_lcd_brightness(dev);
2141 if (brightness < 0)
2142 return 0;
2143 ret = set_lcd_brightness(dev, brightness);
2144 if (ret) {
2145 pr_debug("Backlight method is read-only, disabling backlight support\n");
2146 return 0;
2147 }
2148
Akio Idehara121b7b02012-04-06 01:46:43 +09002149 /* Determine whether or not BIOS supports transflective backlight */
2150 ret = get_tr_backlight_status(dev, &enabled);
2151 dev->tr_backlight_supported = !ret;
2152
Matthew Garrett53039f22012-06-01 11:02:36 -04002153 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002154 props.type = BACKLIGHT_PLATFORM;
2155 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002156
Akio Idehara121b7b02012-04-06 01:46:43 +09002157 /* adding an extra level and having 0 change to transflective mode */
2158 if (dev->tr_backlight_supported)
2159 props.max_brightness++;
2160
Seth Forshee62cce752012-04-19 11:23:50 -05002161 dev->backlight_dev = backlight_device_register("toshiba",
2162 &dev->acpi_dev->dev,
2163 dev,
2164 &toshiba_backlight_data,
2165 &props);
2166 if (IS_ERR(dev->backlight_dev)) {
2167 ret = PTR_ERR(dev->backlight_dev);
2168 pr_err("Could not register toshiba backlight device\n");
2169 dev->backlight_dev = NULL;
2170 return ret;
2171 }
2172
2173 dev->backlight_dev->props.brightness = brightness;
2174 return 0;
2175}
2176
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002177static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002178{
2179 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2180
Seth Forshee36d03f92011-09-20 16:55:53 -05002181 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002182
Azael Avalos360f0f32014-03-25 20:38:31 -06002183 if (dev->sysfs_created)
2184 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2185 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002186
Seth Forshee29cd2932012-01-18 13:44:09 -06002187 if (dev->ntfy_supported) {
2188 i8042_remove_filter(toshiba_acpi_i8042_filter);
2189 cancel_work_sync(&dev->hotkey_work);
2190 }
2191
Seth Forshee135740d2011-09-20 16:55:49 -05002192 if (dev->hotkey_dev) {
2193 input_unregister_device(dev->hotkey_dev);
2194 sparse_keymap_free(dev->hotkey_dev);
2195 }
2196
2197 if (dev->bt_rfk) {
2198 rfkill_unregister(dev->bt_rfk);
2199 rfkill_destroy(dev->bt_rfk);
2200 }
2201
Markus Elfring00981812014-11-24 20:30:29 +01002202 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002203
Seth Forshee36d03f92011-09-20 16:55:53 -05002204 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002205 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002206
Azael Avalos360f0f32014-03-25 20:38:31 -06002207 if (dev->kbd_led_registered)
2208 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002209
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002210 if (dev->eco_supported)
2211 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002212
Seth Forshee29cd2932012-01-18 13:44:09 -06002213 if (toshiba_acpi)
2214 toshiba_acpi = NULL;
2215
Seth Forshee135740d2011-09-20 16:55:49 -05002216 kfree(dev);
2217
2218 return 0;
2219}
2220
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002221static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002222{
Zhang Ruie2e19602013-09-03 08:32:06 +08002223 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002224 return "GHCI";
2225
Zhang Ruie2e19602013-09-03 08:32:06 +08002226 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002227 return "SPFC";
2228
2229 return NULL;
2230}
2231
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002232static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002233{
2234 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002235 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002236 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002237 bool bt_present;
2238 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002239
Seth Forshee29cd2932012-01-18 13:44:09 -06002240 if (toshiba_acpi)
2241 return -EBUSY;
2242
Seth Forshee135740d2011-09-20 16:55:49 -05002243 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2244 TOSHIBA_ACPI_VERSION);
2245
Seth Forsheea540d6b2011-09-20 16:55:52 -05002246 hci_method = find_hci_method(acpi_dev->handle);
2247 if (!hci_method) {
2248 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002249 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002250 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002251
Seth Forshee135740d2011-09-20 16:55:49 -05002252 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2253 if (!dev)
2254 return -ENOMEM;
2255 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002256 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002257 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002258 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002259
Seth Forshee6e02cc72011-09-20 16:55:51 -05002260 if (toshiba_acpi_setup_keyboard(dev))
2261 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002262
2263 mutex_init(&dev->mutex);
2264
Seth Forshee62cce752012-04-19 11:23:50 -05002265 ret = toshiba_acpi_setup_backlight(dev);
2266 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002267 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002268
2269 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002270 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002271 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2272 &acpi_dev->dev,
2273 RFKILL_TYPE_BLUETOOTH,
2274 &toshiba_rfk_ops,
2275 dev);
2276 if (!dev->bt_rfk) {
2277 pr_err("unable to allocate rfkill device\n");
2278 ret = -ENOMEM;
2279 goto error;
2280 }
2281
2282 ret = rfkill_register(dev->bt_rfk);
2283 if (ret) {
2284 pr_err("unable to register rfkill device\n");
2285 rfkill_destroy(dev->bt_rfk);
2286 goto error;
2287 }
2288 }
2289
2290 if (toshiba_illumination_available(dev)) {
2291 dev->led_dev.name = "toshiba::illumination";
2292 dev->led_dev.max_brightness = 1;
2293 dev->led_dev.brightness_set = toshiba_illumination_set;
2294 dev->led_dev.brightness_get = toshiba_illumination_get;
2295 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002296 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002297 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002298
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002299 if (toshiba_eco_mode_available(dev)) {
2300 dev->eco_led.name = "toshiba::eco_mode";
2301 dev->eco_led.max_brightness = 1;
2302 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2303 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2304 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2305 dev->eco_supported = 1;
2306 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002307
Azael Avalos93f8c162014-09-12 18:50:36 -06002308 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002309 /*
2310 * Only register the LED if KBD illumination is supported
2311 * and the keyboard backlight operation mode is set to FN-Z
2312 */
2313 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2314 dev->kbd_led.name = "toshiba::kbd_backlight";
2315 dev->kbd_led.max_brightness = 1;
2316 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2317 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2318 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2319 dev->kbd_led_registered = 1;
2320 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002321
Azael Avalos9d8658a2014-03-25 20:38:32 -06002322 ret = toshiba_touchpad_get(dev, &dummy);
2323 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002324
Azael Avalos5a2813e2014-03-25 20:38:34 -06002325 ret = toshiba_accelerometer_supported(dev);
2326 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002327
Azael Avalose26ffe52015-01-18 18:30:22 -07002328 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2329 dev->usb_sleep_charge_supported = !ret;
2330
Azael Avalosbb3fe012015-01-18 18:30:24 -07002331 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2332 dev->usb_rapid_charge_supported = !ret;
2333
Seth Forshee36d03f92011-09-20 16:55:53 -05002334 /* Determine whether or not BIOS supports fan and video interfaces */
2335
2336 ret = get_video_status(dev, &dummy);
2337 dev->video_supported = !ret;
2338
2339 ret = get_fan_status(dev, &dummy);
2340 dev->fan_supported = !ret;
2341
Azael Avalos360f0f32014-03-25 20:38:31 -06002342 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2343 &toshiba_attr_group);
2344 if (ret) {
2345 dev->sysfs_created = 0;
2346 goto error;
2347 }
2348 dev->sysfs_created = !ret;
2349
Seth Forshee36d03f92011-09-20 16:55:53 -05002350 create_toshiba_proc_entries(dev);
2351
Seth Forshee29cd2932012-01-18 13:44:09 -06002352 toshiba_acpi = dev;
2353
Seth Forshee135740d2011-09-20 16:55:49 -05002354 return 0;
2355
2356error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002357 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002358 return ret;
2359}
2360
2361static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2362{
2363 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002364 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002365
Azael Avalos71454d72014-12-04 20:22:46 -07002366 switch (event) {
2367 case 0x80: /* Hotkeys and some system events */
2368 toshiba_acpi_process_hotkeys(dev);
2369 break;
Azael Avalos80546902014-12-04 20:22:47 -07002370 case 0x92: /* Keyboard backlight mode changed */
2371 /* Update sysfs entries */
2372 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2373 &toshiba_attr_group);
2374 if (ret)
2375 pr_err("Unable to update sysfs entries\n");
2376 break;
Azael Avalos71454d72014-12-04 20:22:46 -07002377 case 0x81: /* Unknown */
2378 case 0x82: /* Unknown */
2379 case 0x83: /* Unknown */
2380 case 0x8c: /* Unknown */
2381 case 0x8e: /* Unknown */
2382 case 0x8f: /* Unknown */
2383 case 0x90: /* Unknown */
2384 default:
2385 pr_info("Unknown event received %x\n", event);
2386 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002387 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002388}
2389
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002390#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002391static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002392{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002393 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002394 u32 result;
2395
2396 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002397 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002398
2399 return 0;
2400}
2401
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002402static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002403{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002404 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002405 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002406
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002407 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002408 error = toshiba_acpi_enable_hotkeys(dev);
2409 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002410 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002411 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002412
2413 return 0;
2414}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002415#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002416
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002417static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2418 toshiba_acpi_suspend, toshiba_acpi_resume);
2419
Seth Forshee135740d2011-09-20 16:55:49 -05002420static struct acpi_driver toshiba_acpi_driver = {
2421 .name = "Toshiba ACPI driver",
2422 .owner = THIS_MODULE,
2423 .ids = toshiba_device_ids,
2424 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2425 .ops = {
2426 .add = toshiba_acpi_add,
2427 .remove = toshiba_acpi_remove,
2428 .notify = toshiba_acpi_notify,
2429 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002430 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002431};
Holger Machtc9263552006-10-20 14:30:29 -07002432
Len Brown4be44fc2005-08-05 00:44:28 -04002433static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Seth Forshee135740d2011-09-20 16:55:49 -05002435 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Seth Forsheef11f9992012-01-18 13:44:11 -06002437 /*
2438 * Machines with this WMI guid aren't supported due to bugs in
2439 * their AML. This check relies on wmi initializing before
2440 * toshiba_acpi to guarantee guids have been identified.
2441 */
2442 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2443 return -ENODEV;
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2446 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002447 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002448 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450
Seth Forshee135740d2011-09-20 16:55:49 -05002451 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2452 if (ret) {
2453 pr_err("Failed to register ACPI driver: %d\n", ret);
2454 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002455 }
2456
Seth Forshee135740d2011-09-20 16:55:49 -05002457 return ret;
2458}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002459
Seth Forshee135740d2011-09-20 16:55:49 -05002460static void __exit toshiba_acpi_exit(void)
2461{
2462 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2463 if (toshiba_proc_dir)
2464 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467module_init(toshiba_acpi_init);
2468module_exit(toshiba_acpi_exit);