blob: 8ee7c24fb7eb1677d8df4dac9a46ad97e388a172 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04005 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02006 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07007 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Darren Hartc57c0fa2015-02-11 21:25:22 -080019 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches7e334602011-03-29 15:21:52 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Azael Avalos7216d702015-02-10 21:09:21 -070034#define TOSHIBA_ACPI_VERSION "0.21"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define PROC_INTERFACE_VERSION 1
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080042#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070043#include <linux/backlight.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050044#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070045#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020046#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060048#include <linux/workqueue.h>
49#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080050#include <linux/acpi.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020051#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070052#include <linux/uaccess.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020053#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_AUTHOR("John Belmonte");
56MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
57MODULE_LICENSE("GPL");
58
Seth Forsheef11f9992012-01-18 13:44:11 -060059#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
60
Seth Forshee29cd2932012-01-18 13:44:09 -060061/* Scan code for Fn key on TOS1900 models */
62#define TOS1900_FN_SCAN 0x6e
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
66
Darren Harte0769fe2015-02-11 20:50:08 -080067/*
68 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060069 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
72 * be uniform across all their models. Ideally we would just call
73 * dedicated ACPI methods instead of using this primitive interface.
74 * However the ACPI methods seem to be incomplete in some areas (for
75 * example they allow setting, but not reading, the LCD brightness value),
76 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040077 *
Azael Avalos84a62732014-03-25 20:38:29 -060078 * SCI stands for "System Configuration Interface" which aim is to
79 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
Azael Avalos258c5902014-09-29 20:40:07 -060082#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* operations */
85#define HCI_SET 0xff00
86#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060087#define SCI_OPEN 0xf100
88#define SCI_CLOSE 0xf200
89#define SCI_GET 0xf300
90#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060093#define TOS_SUCCESS 0x0000
94#define TOS_OPEN_CLOSE_OK 0x0044
95#define TOS_FAILURE 0x1000
96#define TOS_NOT_SUPPORTED 0x8000
97#define TOS_ALREADY_OPEN 0x8100
98#define TOS_NOT_OPENED 0x8200
99#define TOS_INPUT_DATA_ERROR 0x8300
100#define TOS_WRITE_PROTECTED 0x8400
101#define TOS_NOT_PRESENT 0x8600
102#define TOS_FIFO_EMPTY 0x8c00
103#define TOS_DATA_NOT_AVAILABLE 0x8d20
104#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700105#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* registers */
108#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900109#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define HCI_SYSTEM_EVENT 0x0016
111#define HCI_VIDEO_OUT 0x001c
112#define HCI_HOTKEY_EVENT 0x001e
113#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400114#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600115#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600116#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600117#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600118#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600119#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700120#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600121#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700122#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600123#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700124#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700125#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600126#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700127#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600130#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600131#define HCI_HOTKEY_DISABLE 0x0b
132#define HCI_HOTKEY_ENABLE 0x09
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600133#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define HCI_LCD_BRIGHTNESS_BITS 3
135#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
136#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600137#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600138#define HCI_SYSTEM_TYPE1 0x10
139#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define HCI_VIDEO_OUT_LCD 0x1
141#define HCI_VIDEO_OUT_CRT 0x2
142#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400143#define HCI_WIRELESS_KILL_SWITCH 0x01
144#define HCI_WIRELESS_BT_PRESENT 0x0f
145#define HCI_WIRELESS_BT_ATTACH 0x40
146#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600147#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600148#define SCI_KBD_MODE_FNZ 0x1
149#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600150#define SCI_KBD_MODE_ON 0x8
151#define SCI_KBD_MODE_OFF 0x10
152#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700153#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600154#define SCI_USB_CHARGE_DISABLED 0x00
155#define SCI_USB_CHARGE_ALTERNATE 0x09
156#define SCI_USB_CHARGE_TYPICAL 0x11
157#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700158#define SCI_USB_CHARGE_BAT_MASK 0x7
159#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
160#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
161#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700162#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Seth Forshee135740d2011-09-20 16:55:49 -0500164struct toshiba_acpi_dev {
165 struct acpi_device *acpi_dev;
166 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500167 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600168 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500169 struct backlight_device *backlight_dev;
170 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600171 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600172 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500173
Seth Forshee135740d2011-09-20 16:55:49 -0500174 int force_fan;
175 int last_key_event;
176 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600177 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600178 int kbd_mode;
179 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700180 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600181 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600182 int hotkey_event_type;
Seth Forshee135740d2011-09-20 16:55:49 -0500183
Dan Carpenter592b7462012-01-15 14:28:20 +0300184 unsigned int illumination_supported:1;
185 unsigned int video_supported:1;
186 unsigned int fan_supported:1;
187 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600188 unsigned int ntfy_supported:1;
189 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900190 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600191 unsigned int kbd_illum_supported:1;
192 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600193 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600194 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600195 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700196 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700197 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700198 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700199 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700200 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700201 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600202 unsigned int sysfs_created:1;
Seth Forshee135740d2011-09-20 16:55:49 -0500203};
204
Seth Forshee29cd2932012-01-18 13:44:09 -0600205static struct toshiba_acpi_dev *toshiba_acpi;
206
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800207static const struct acpi_device_id toshiba_device_ids[] = {
208 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100209 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400210 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800211 {"TOS1900", 0},
212 {"", 0},
213};
214MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
215
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800216static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100217 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700218 { KE_KEY, 0x101, { KEY_MUTE } },
219 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
220 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600221 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600222 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
223 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700224 { KE_KEY, 0x13b, { KEY_COFFEE } },
225 { KE_KEY, 0x13c, { KEY_BATTERY } },
226 { KE_KEY, 0x13d, { KEY_SLEEP } },
227 { KE_KEY, 0x13e, { KEY_SUSPEND } },
228 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
229 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
230 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
231 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600232 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100233 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700234 { KE_KEY, 0xb05, { KEY_PROG2 } },
235 { KE_KEY, 0xb06, { KEY_WWW } },
236 { KE_KEY, 0xb07, { KEY_MAIL } },
237 { KE_KEY, 0xb30, { KEY_STOP } },
238 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
239 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
240 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
241 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600242 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
243 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
244 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
245 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
246 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700247 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500248};
249
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200250static const struct key_entry toshiba_acpi_alt_keymap[] = {
251 { KE_KEY, 0x157, { KEY_MUTE } },
252 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
253 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600254 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200255 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
256 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
257 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
258 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
259 { KE_KEY, 0x158, { KEY_WLAN } },
260 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
261 { KE_END, 0 },
262};
263
Darren Harte0769fe2015-02-11 20:50:08 -0800264/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200265 * List of models which have a broken acpi-video backlight interface and thus
266 * need to use the toshiba (vendor) interface instead.
267 */
268static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
269 {}
270};
271
272/*
Darren Harte0769fe2015-02-11 20:50:08 -0800273 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275
Azael Avalosb5163992015-02-10 23:43:56 -0700276static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 *word = (*word & ~mask) | (mask * value);
279}
280
Darren Harte0769fe2015-02-11 20:50:08 -0800281/*
282 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 acpi_status status;
288
Zhang Rui619400d2013-09-03 08:31:56 +0800289 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500290 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Darren Harte0769fe2015-02-11 20:50:08 -0800293/*
294 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600295 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Azael Avalos258c5902014-09-29 20:40:07 -0600297static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
298 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600301 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600303 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 acpi_status status;
305 int i;
306
Azael Avalos258c5902014-09-29 20:40:07 -0600307 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600309 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 in_objs[i].type = ACPI_TYPE_INTEGER;
311 in_objs[i].integer.value = in[i];
312 }
313
314 results.length = sizeof(out_objs);
315 results.pointer = out_objs;
316
Seth Forshee6e02cc72011-09-20 16:55:51 -0500317 status = acpi_evaluate_object(dev->acpi_dev->handle,
318 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400319 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600320 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700321 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
325 return status;
326}
327
Darren Harte0769fe2015-02-11 20:50:08 -0800328/*
329 * Common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * In addition to the ACPI status, the HCI system returns a result which
332 * may be useful (such as "not supported").
333 */
334
Azael Avalos893f3f62014-09-29 20:40:09 -0600335static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Azael Avalos258c5902014-09-29 20:40:07 -0600337 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
338 u32 out[TCI_WORDS];
339 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600340
341 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Azael Avalos893f3f62014-09-29 20:40:09 -0600344static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Azael Avalos258c5902014-09-29 20:40:07 -0600346 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
347 u32 out[TCI_WORDS];
348 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700349
Azael Avalos893f3f62014-09-29 20:40:09 -0600350 if (ACPI_FAILURE(status))
351 return TOS_FAILURE;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600354
355 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Azael Avalos893f3f62014-09-29 20:40:09 -0600358static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400359{
Azael Avalos258c5902014-09-29 20:40:07 -0600360 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
361 u32 out[TCI_WORDS];
362 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600363
364 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400365}
366
Azael Avalosb5163992015-02-10 23:43:56 -0700367static u32 hci_read2(struct toshiba_acpi_dev *dev,
368 u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400369{
Azael Avalos258c5902014-09-29 20:40:07 -0600370 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
371 u32 out[TCI_WORDS];
372 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700373
Azael Avalos893f3f62014-09-29 20:40:09 -0600374 if (ACPI_FAILURE(status))
375 return TOS_FAILURE;
376
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400377 *out1 = out[2];
378 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600379
380 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400381}
382
Darren Harte0769fe2015-02-11 20:50:08 -0800383/*
384 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600385 */
386
387static int sci_open(struct toshiba_acpi_dev *dev)
388{
Azael Avalos258c5902014-09-29 20:40:07 -0600389 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
390 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600391 acpi_status status;
392
Azael Avalos258c5902014-09-29 20:40:07 -0600393 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600394 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600395 pr_err("ACPI call to open SCI failed\n");
396 return 0;
397 }
398
Azael Avalos1864bbc2014-09-29 20:40:08 -0600399 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600400 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600401 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600402 pr_info("Toshiba SCI already opened\n");
403 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700404 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800405 /*
406 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700407 * implemented and return 0x8000 (Not Supported), failing to
408 * register some supported features.
409 *
410 * Simply return 1 if we hit those affected laptops to make the
411 * supported features work.
412 *
413 * In the case that some laptops really do not support the SCI,
414 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
415 * and thus, not registering support for the queried feature.
416 */
417 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600418 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600419 pr_info("Toshiba SCI is not present\n");
420 }
421
422 return 0;
423}
424
425static void sci_close(struct toshiba_acpi_dev *dev)
426{
Azael Avalos258c5902014-09-29 20:40:07 -0600427 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
428 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600429 acpi_status status;
430
Azael Avalos258c5902014-09-29 20:40:07 -0600431 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600432 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600433 pr_err("ACPI call to close SCI failed\n");
434 return;
435 }
436
Azael Avalos1864bbc2014-09-29 20:40:08 -0600437 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600438 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600439 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600440 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600441 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600442 pr_info("Toshiba SCI is not present\n");
443}
444
Azael Avalos893f3f62014-09-29 20:40:09 -0600445static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600446{
Azael Avalos258c5902014-09-29 20:40:07 -0600447 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
448 u32 out[TCI_WORDS];
449 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700450
Azael Avalos893f3f62014-09-29 20:40:09 -0600451 if (ACPI_FAILURE(status))
452 return TOS_FAILURE;
453
Azael Avalos84a62732014-03-25 20:38:29 -0600454 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600455
456 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600457}
458
Azael Avalos893f3f62014-09-29 20:40:09 -0600459static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600460{
Azael Avalos258c5902014-09-29 20:40:07 -0600461 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
462 u32 out[TCI_WORDS];
463 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600464
465 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600466}
467
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200468/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500469static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200470{
Azael Avalos258c5902014-09-29 20:40:07 -0600471 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
472 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200473 acpi_status status;
474
Azael Avalosfdb79082014-03-25 20:38:30 -0600475 if (!sci_open(dev))
476 return 0;
477
Azael Avalos258c5902014-09-29 20:40:07 -0600478 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600479 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600480 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600481 pr_err("ACPI call to query Illumination support failed\n");
482 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600483 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700484 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200485 return 0;
486 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600487
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200488 return 1;
489}
490
491static void toshiba_illumination_set(struct led_classdev *cdev,
492 enum led_brightness brightness)
493{
Seth Forshee135740d2011-09-20 16:55:49 -0500494 struct toshiba_acpi_dev *dev = container_of(cdev,
495 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600496 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200497
498 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600499 if (!sci_open(dev))
500 return;
501
502 /* Switch the illumination on/off */
503 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600504 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600505 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600506 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 pr_err("ACPI call for illumination failed\n");
508 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600509 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600510 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200511 return;
512 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200513}
514
515static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
516{
Seth Forshee135740d2011-09-20 16:55:49 -0500517 struct toshiba_acpi_dev *dev = container_of(cdev,
518 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600519 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200520
521 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600522 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200523 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200524
525 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600526 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600527 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600528 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600529 pr_err("ACPI call for illumination failed\n");
530 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600531 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600532 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200533 return LED_OFF;
534 }
535
Azael Avalosfdb79082014-03-25 20:38:30 -0600536 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200537}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400538
Azael Avalos360f0f32014-03-25 20:38:31 -0600539/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600540static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
541{
Azael Avalos258c5902014-09-29 20:40:07 -0600542 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
543 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600544 acpi_status status;
545
546 if (!sci_open(dev))
547 return 0;
548
Azael Avalos258c5902014-09-29 20:40:07 -0600549 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600550 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600551 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600552 pr_err("ACPI call to query kbd illumination support failed\n");
553 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600554 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600555 pr_info("Keyboard illumination not available\n");
556 return 0;
557 }
558
Darren Harte0769fe2015-02-11 20:50:08 -0800559 /*
560 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600561 * previous kbd backlight implementation set this to
562 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800563 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600564 */
565 if (out[3] == SCI_KBD_TIME_MAX)
566 dev->kbd_type = 2;
567 else
568 dev->kbd_type = 1;
569 /* Get the current keyboard backlight mode */
570 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
571 /* Get the current time (1-60 seconds) */
572 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
573
574 return 1;
575}
576
Azael Avalos360f0f32014-03-25 20:38:31 -0600577static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
578{
579 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600580
581 if (!sci_open(dev))
582 return -EIO;
583
Azael Avalos893f3f62014-09-29 20:40:09 -0600584 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600585 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600586 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600587 pr_err("ACPI call to set KBD backlight status failed\n");
588 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600589 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600590 pr_info("Keyboard backlight status not supported\n");
591 return -ENODEV;
592 }
593
594 return 0;
595}
596
597static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
598{
599 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600600
601 if (!sci_open(dev))
602 return -EIO;
603
Azael Avalos893f3f62014-09-29 20:40:09 -0600604 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600605 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600606 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600607 pr_err("ACPI call to get KBD backlight status failed\n");
608 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600609 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600610 pr_info("Keyboard backlight status not supported\n");
611 return -ENODEV;
612 }
613
614 return 0;
615}
616
617static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
618{
619 struct toshiba_acpi_dev *dev = container_of(cdev,
620 struct toshiba_acpi_dev, kbd_led);
621 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600622
623 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600624 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
625 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600626 pr_err("ACPI call to get the keyboard backlight failed\n");
627 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600628 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600629 pr_info("Keyboard backlight not supported\n");
630 return LED_OFF;
631 }
632
633 return state ? LED_FULL : LED_OFF;
634}
635
636static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
637 enum led_brightness brightness)
638{
639 struct toshiba_acpi_dev *dev = container_of(cdev,
640 struct toshiba_acpi_dev, kbd_led);
641 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600642
643 /* Set the keyboard backlight state */
644 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600645 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
646 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600647 pr_err("ACPI call to set KBD Illumination mode failed\n");
648 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600649 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600650 pr_info("Keyboard backlight not supported\n");
651 return;
652 }
653}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400654
Azael Avalos9d8658a2014-03-25 20:38:32 -0600655/* TouchPad support */
656static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
657{
658 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600659
660 if (!sci_open(dev))
661 return -EIO;
662
Azael Avalos893f3f62014-09-29 20:40:09 -0600663 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600664 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600665 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600666 pr_err("ACPI call to set the touchpad failed\n");
667 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600668 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600669 return -ENODEV;
670 }
671
672 return 0;
673}
674
675static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
676{
677 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600678
679 if (!sci_open(dev))
680 return -EIO;
681
Azael Avalos893f3f62014-09-29 20:40:09 -0600682 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600683 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600684 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600685 pr_err("ACPI call to query the touchpad failed\n");
686 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600687 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600688 return -ENODEV;
689 }
690
691 return 0;
692}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200693
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600694/* Eco Mode support */
695static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
696{
697 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700698 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600699 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600700
Azael Avalos258c5902014-09-29 20:40:07 -0600701 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700702 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
703 pr_err("ACPI call to get ECO led failed\n");
704 } else if (out[0] == TOS_NOT_INSTALLED) {
705 pr_info("ECO led not installed");
706 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800707 /*
708 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700709 * LED device is present, but that we just screwed the input
710 * parameters.
711 *
712 * Let's query the status of the LED to see if we really have a
713 * success response, indicating the actual presense of the LED,
714 * bail out otherwise.
715 */
716 in[3] = 1;
717 status = tci_raw(dev, in, out);
718 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
719 pr_err("ACPI call to get ECO led failed\n");
720 else if (out[0] == TOS_SUCCESS)
721 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600722 }
723
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700724 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600725}
726
Azael Avalosb5163992015-02-10 23:43:56 -0700727static enum led_brightness
728toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600729{
730 struct toshiba_acpi_dev *dev = container_of(cdev,
731 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600732 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
733 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600734 acpi_status status;
735
Azael Avalos258c5902014-09-29 20:40:07 -0600736 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600737 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600738 pr_err("ACPI call to get ECO led failed\n");
739 return LED_OFF;
740 }
741
742 return out[2] ? LED_FULL : LED_OFF;
743}
744
745static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
746 enum led_brightness brightness)
747{
748 struct toshiba_acpi_dev *dev = container_of(cdev,
749 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600750 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
751 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600752 acpi_status status;
753
754 /* Switch the Eco Mode led on/off */
755 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600756 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600757 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600758 pr_err("ACPI call to set ECO led failed\n");
759 return;
760 }
761}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400762
Azael Avalos5a2813e2014-03-25 20:38:34 -0600763/* Accelerometer support */
764static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
765{
Azael Avalos258c5902014-09-29 20:40:07 -0600766 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
767 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600768 acpi_status status;
769
Darren Harte0769fe2015-02-11 20:50:08 -0800770 /*
771 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600772 * this call also serves as initialization
773 */
Azael Avalos258c5902014-09-29 20:40:07 -0600774 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600775 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600776 pr_err("ACPI call to query the accelerometer failed\n");
777 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600778 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
779 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600780 pr_err("Accelerometer not initialized\n");
781 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600782 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600783 pr_info("Accelerometer not supported\n");
784 return -ENODEV;
785 }
786
787 return 0;
788}
789
790static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
791 u32 *xy, u32 *z)
792{
Azael Avalos258c5902014-09-29 20:40:07 -0600793 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
794 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600795 acpi_status status;
796
797 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600798 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600799 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600800 pr_err("ACPI call to query the accelerometer failed\n");
801 return -EIO;
802 }
803
804 *xy = out[2];
805 *z = out[4];
806
807 return 0;
808}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600809
Azael Avalose26ffe52015-01-18 18:30:22 -0700810/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600811static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
812{
813 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
814 u32 out[TCI_WORDS];
815 acpi_status status;
816
817 /* Set the feature to "not supported" in case of error */
818 dev->usb_sleep_charge_supported = 0;
819
820 if (!sci_open(dev))
821 return;
822
823 status = tci_raw(dev, in, out);
824 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
825 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
826 sci_close(dev);
827 return;
828 } else if (out[0] == TOS_NOT_SUPPORTED) {
829 pr_info("USB Sleep and Charge not supported\n");
830 sci_close(dev);
831 return;
832 } else if (out[0] == TOS_SUCCESS) {
833 dev->usbsc_mode_base = out[4];
834 }
835
836 in[5] = SCI_USB_CHARGE_BAT_LVL;
837 status = tci_raw(dev, in, out);
838 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
839 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
840 sci_close(dev);
841 return;
842 } else if (out[0] == TOS_NOT_SUPPORTED) {
843 pr_info("USB Sleep and Charge not supported\n");
844 sci_close(dev);
845 return;
846 } else if (out[0] == TOS_SUCCESS) {
847 dev->usbsc_bat_level = out[2];
848 /*
849 * If we reach this point, it means that the laptop has support
850 * for this feature and all values are initialized.
851 * Set it as supported.
852 */
853 dev->usb_sleep_charge_supported = 1;
854 }
855
856 sci_close(dev);
857}
858
Azael Avalose26ffe52015-01-18 18:30:22 -0700859static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
860 u32 *mode)
861{
862 u32 result;
863
864 if (!sci_open(dev))
865 return -EIO;
866
867 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
868 sci_close(dev);
869 if (result == TOS_FAILURE) {
870 pr_err("ACPI call to set USB S&C mode failed\n");
871 return -EIO;
872 } else if (result == TOS_NOT_SUPPORTED) {
873 pr_info("USB Sleep and Charge not supported\n");
874 return -ENODEV;
875 } else if (result == TOS_INPUT_DATA_ERROR) {
876 return -EIO;
877 }
878
879 return 0;
880}
881
882static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
883 u32 mode)
884{
885 u32 result;
886
887 if (!sci_open(dev))
888 return -EIO;
889
890 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
891 sci_close(dev);
892 if (result == TOS_FAILURE) {
893 pr_err("ACPI call to set USB S&C mode failed\n");
894 return -EIO;
895 } else if (result == TOS_NOT_SUPPORTED) {
896 pr_info("USB Sleep and Charge not supported\n");
897 return -ENODEV;
898 } else if (result == TOS_INPUT_DATA_ERROR) {
899 return -EIO;
900 }
901
902 return 0;
903}
904
Azael Avalos182bcaa2015-01-18 18:30:23 -0700905static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
906 u32 *mode)
907{
908 u32 in[TCI_WORDS] = { SCI_GET, 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[5] = SCI_USB_CHARGE_BAT_LVL;
916 status = tci_raw(dev, in, out);
917 sci_close(dev);
918 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
919 pr_err("ACPI call to get USB S&C battery level failed\n");
920 return -EIO;
921 } else if (out[0] == TOS_NOT_SUPPORTED) {
922 pr_info("USB Sleep and Charge not supported\n");
923 return -ENODEV;
924 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
925 return -EIO;
926 }
927
928 *mode = out[2];
929
930 return 0;
931}
932
933static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
934 u32 mode)
935{
936 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
937 u32 out[TCI_WORDS];
938 acpi_status status;
939
940 if (!sci_open(dev))
941 return -EIO;
942
943 in[2] = mode;
944 in[5] = SCI_USB_CHARGE_BAT_LVL;
945 status = tci_raw(dev, in, out);
946 sci_close(dev);
947 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
948 pr_err("ACPI call to set USB S&C battery level failed\n");
949 return -EIO;
950 } else if (out[0] == TOS_NOT_SUPPORTED) {
951 pr_info("USB Sleep and Charge not supported\n");
952 return -ENODEV;
953 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
954 return -EIO;
955 }
956
957 return 0;
958}
959
Azael Avalosbb3fe012015-01-18 18:30:24 -0700960static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
961 u32 *state)
962{
963 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
964 u32 out[TCI_WORDS];
965 acpi_status status;
966
967 if (!sci_open(dev))
968 return -EIO;
969
970 in[5] = SCI_USB_CHARGE_RAPID_DSP;
971 status = tci_raw(dev, in, out);
972 sci_close(dev);
973 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600974 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700975 return -EIO;
976 } else if (out[0] == TOS_NOT_SUPPORTED ||
977 out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600978 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700979 return -ENODEV;
980 }
981
982 *state = out[2];
983
984 return 0;
985}
986
987static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
988 u32 state)
989{
990 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
991 u32 out[TCI_WORDS];
992 acpi_status status;
993
994 if (!sci_open(dev))
995 return -EIO;
996
997 in[2] = state;
998 in[5] = SCI_USB_CHARGE_RAPID_DSP;
999 status = tci_raw(dev, in, out);
1000 sci_close(dev);
1001 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001002 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -07001003 return -EIO;
1004 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001005 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -07001006 return -ENODEV;
1007 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
1008 return -EIO;
1009 }
1010
1011 return 0;
1012}
1013
Azael Avalos172ce0a2015-01-18 18:30:25 -07001014static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
1015{
1016 u32 result;
1017
1018 if (!sci_open(dev))
1019 return -EIO;
1020
1021 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1022 sci_close(dev);
1023 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001024 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001025 return -EIO;
1026 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001027 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001028 return -ENODEV;
1029 } else if (result == TOS_INPUT_DATA_ERROR) {
1030 return -EIO;
1031 }
1032
1033 return 0;
1034}
1035
1036static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1037{
1038 u32 result;
1039
1040 if (!sci_open(dev))
1041 return -EIO;
1042
1043 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1044 sci_close(dev);
1045 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001046 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001047 return -EIO;
1048 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001049 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001050 return -ENODEV;
1051 } else if (result == TOS_INPUT_DATA_ERROR) {
1052 return -EIO;
1053 }
1054
1055 return 0;
1056}
1057
Azael Avalosbae84192015-02-10 21:09:18 -07001058/* Keyboard function keys */
1059static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1060{
1061 u32 result;
1062
1063 if (!sci_open(dev))
1064 return -EIO;
1065
1066 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1067 sci_close(dev);
1068 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1069 pr_err("ACPI call to get KBD function keys failed\n");
1070 return -EIO;
1071 } else if (result == TOS_NOT_SUPPORTED) {
1072 pr_info("KBD function keys not supported\n");
1073 return -ENODEV;
1074 }
1075
1076 return 0;
1077}
1078
1079static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1080{
1081 u32 result;
1082
1083 if (!sci_open(dev))
1084 return -EIO;
1085
1086 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1087 sci_close(dev);
1088 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1089 pr_err("ACPI call to set KBD function keys failed\n");
1090 return -EIO;
1091 } else if (result == TOS_NOT_SUPPORTED) {
1092 pr_info("KBD function keys not supported\n");
1093 return -ENODEV;
1094 }
1095
1096 return 0;
1097}
1098
Azael Avalos35d53ce2015-02-10 21:09:19 -07001099/* Panel Power ON */
1100static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1101{
1102 u32 result;
1103
1104 if (!sci_open(dev))
1105 return -EIO;
1106
1107 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1108 sci_close(dev);
1109 if (result == TOS_FAILURE) {
1110 pr_err("ACPI call to get Panel Power ON failed\n");
1111 return -EIO;
1112 } else if (result == TOS_NOT_SUPPORTED) {
1113 pr_info("Panel Power on not supported\n");
1114 return -ENODEV;
1115 } else if (result == TOS_INPUT_DATA_ERROR) {
1116 return -EIO;
1117 }
1118
1119 return 0;
1120}
1121
1122static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1123{
1124 u32 result;
1125
1126 if (!sci_open(dev))
1127 return -EIO;
1128
1129 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1130 sci_close(dev);
1131 if (result == TOS_FAILURE) {
1132 pr_err("ACPI call to set Panel Power ON failed\n");
1133 return -EIO;
1134 } else if (result == TOS_NOT_SUPPORTED) {
1135 pr_info("Panel Power ON not supported\n");
1136 return -ENODEV;
1137 } else if (result == TOS_INPUT_DATA_ERROR) {
1138 return -EIO;
1139 }
1140
1141 return 0;
1142}
1143
Azael Avalos17fe4b32015-02-10 21:09:20 -07001144/* USB Three */
1145static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1146{
1147 u32 result;
1148
1149 if (!sci_open(dev))
1150 return -EIO;
1151
1152 result = sci_read(dev, SCI_USB_THREE, state);
1153 sci_close(dev);
1154 if (result == TOS_FAILURE) {
1155 pr_err("ACPI call to get USB 3 failed\n");
1156 return -EIO;
1157 } else if (result == TOS_NOT_SUPPORTED) {
1158 pr_info("USB 3 not supported\n");
1159 return -ENODEV;
1160 } else if (result == TOS_INPUT_DATA_ERROR) {
1161 return -EIO;
1162 }
1163
1164 return 0;
1165}
1166
1167static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1168{
1169 u32 result;
1170
1171 if (!sci_open(dev))
1172 return -EIO;
1173
1174 result = sci_write(dev, SCI_USB_THREE, state);
1175 sci_close(dev);
1176 if (result == TOS_FAILURE) {
1177 pr_err("ACPI call to set USB 3 failed\n");
1178 return -EIO;
1179 } else if (result == TOS_NOT_SUPPORTED) {
1180 pr_info("USB 3 not supported\n");
1181 return -ENODEV;
1182 } else if (result == TOS_INPUT_DATA_ERROR) {
1183 return -EIO;
1184 }
1185
1186 return 0;
1187}
1188
Azael Avalos56e6b352015-03-20 16:55:16 -06001189/* Hotkey Event type */
1190static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1191 u32 *type)
1192{
1193 u32 val1 = 0x03;
1194 u32 val2 = 0;
1195 u32 result;
1196
1197 result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
1198 if (result == TOS_FAILURE) {
1199 pr_err("ACPI call to get System type failed\n");
1200 return -EIO;
1201 } else if (result == TOS_NOT_SUPPORTED) {
1202 pr_info("System type not supported\n");
1203 return -ENODEV;
1204 }
1205
1206 *type = val2;
1207
1208 return 0;
1209}
1210
Akio Idehara121b7b02012-04-06 01:46:43 +09001211static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1212{
1213 u32 hci_result;
1214 u32 status;
1215
Azael Avalos893f3f62014-09-29 20:40:09 -06001216 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001217 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001218 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001219}
1220
1221static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1222{
1223 u32 hci_result;
1224 u32 value = !enable;
1225
Azael Avalos893f3f62014-09-29 20:40:09 -06001226 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001227 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001228}
1229
Azael Avalosb5163992015-02-10 23:43:56 -07001230static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Seth Forshee62cce752012-04-19 11:23:50 -05001232static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 u32 hci_result;
1235 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001236 int brightness = 0;
1237
1238 if (dev->tr_backlight_supported) {
1239 bool enabled;
1240 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001241
Akio Idehara121b7b02012-04-06 01:46:43 +09001242 if (ret)
1243 return ret;
1244 if (enabled)
1245 return 0;
1246 brightness++;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Azael Avalos893f3f62014-09-29 20:40:09 -06001249 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001250 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001251 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001252
1253 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001254}
1255
Seth Forshee62cce752012-04-19 11:23:50 -05001256static int get_lcd_brightness(struct backlight_device *bd)
1257{
1258 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001259
Seth Forshee62cce752012-04-19 11:23:50 -05001260 return __get_lcd_brightness(dev);
1261}
1262
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001263static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001264{
Seth Forshee135740d2011-09-20 16:55:49 -05001265 struct toshiba_acpi_dev *dev = m->private;
1266 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001267 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001268
Seth Forshee135740d2011-09-20 16:55:49 -05001269 if (!dev->backlight_dev)
1270 return -ENODEV;
1271
Akio Idehara121b7b02012-04-06 01:46:43 +09001272 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001273 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001274 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001275 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001276 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001280 pr_err("Error reading LCD brightness\n");
1281 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001282}
1283
1284static int lcd_proc_open(struct inode *inode, struct file *file)
1285{
Al Virod9dda782013-03-31 18:16:14 -04001286 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Seth Forshee62cce752012-04-19 11:23:50 -05001289static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001290{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001291 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001292
Akio Idehara121b7b02012-04-06 01:46:43 +09001293 if (dev->tr_backlight_supported) {
1294 bool enable = !value;
1295 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001296
Akio Idehara121b7b02012-04-06 01:46:43 +09001297 if (ret)
1298 return ret;
1299 if (value)
1300 value--;
1301 }
1302
Azael Avalosa39f46d2014-11-24 19:29:36 -07001303 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1304 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1305 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001306}
1307
1308static int set_lcd_status(struct backlight_device *bd)
1309{
Seth Forshee135740d2011-09-20 16:55:49 -05001310 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001311
Seth Forshee62cce752012-04-19 11:23:50 -05001312 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001313}
1314
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001315static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1316 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Al Virod9dda782013-03-31 18:16:14 -04001318 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001319 char cmd[42];
1320 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001322 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001323 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001325 len = min(count, sizeof(cmd) - 1);
1326 if (copy_from_user(cmd, buf, len))
1327 return -EFAULT;
1328 cmd[len] = '\0';
1329
1330 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001331 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001332 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001333 if (ret == 0)
1334 ret = count;
1335 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001336 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001337 }
Holger Machtc9263552006-10-20 14:30:29 -07001338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001341static const struct file_operations lcd_proc_fops = {
1342 .owner = THIS_MODULE,
1343 .open = lcd_proc_open,
1344 .read = seq_read,
1345 .llseek = seq_lseek,
1346 .release = single_release,
1347 .write = lcd_proc_write,
1348};
1349
Seth Forshee36d03f92011-09-20 16:55:53 -05001350static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1351{
1352 u32 hci_result;
1353
Azael Avalos893f3f62014-09-29 20:40:09 -06001354 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001355 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001356}
1357
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001358static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Seth Forshee135740d2011-09-20 16:55:49 -05001360 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Seth Forshee36d03f92011-09-20 16:55:53 -05001364 ret = get_video_status(dev, &value);
1365 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1367 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001368 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001369
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001370 seq_printf(m, "lcd_out: %d\n", is_lcd);
1371 seq_printf(m, "crt_out: %d\n", is_crt);
1372 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Seth Forshee36d03f92011-09-20 16:55:53 -05001375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001378static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Al Virod9dda782013-03-31 18:16:14 -04001380 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001381}
1382
1383static ssize_t video_proc_write(struct file *file, const char __user *buf,
1384 size_t count, loff_t *pos)
1385{
Al Virod9dda782013-03-31 18:16:14 -04001386 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001387 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int value;
1390 int remain = count;
1391 int lcd_out = -1;
1392 int crt_out = -1;
1393 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001394 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001396 cmd = kmalloc(count + 1, GFP_KERNEL);
1397 if (!cmd)
1398 return -ENOMEM;
1399 if (copy_from_user(cmd, buf, count)) {
1400 kfree(cmd);
1401 return -EFAULT;
1402 }
1403 cmd[count] = '\0';
1404
1405 buffer = cmd;
1406
Darren Harte0769fe2015-02-11 20:50:08 -08001407 /*
1408 * Scan expression. Multiple expressions may be delimited with ;
1409 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411 while (remain) {
1412 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1413 lcd_out = value & 1;
1414 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1415 crt_out = value & 1;
1416 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1417 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001418 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 do {
1420 ++buffer;
1421 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001422 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001425 kfree(cmd);
1426
Seth Forshee36d03f92011-09-20 16:55:53 -05001427 ret = get_video_status(dev, &video_out);
1428 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001429 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (lcd_out != -1)
1432 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1433 if (crt_out != -1)
1434 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1435 if (tv_out != -1)
1436 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001437 /*
1438 * To avoid unnecessary video disruption, only write the new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * video setting if something changed. */
1440 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001441 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001444 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001447static const struct file_operations video_proc_fops = {
1448 .owner = THIS_MODULE,
1449 .open = video_proc_open,
1450 .read = seq_read,
1451 .llseek = seq_lseek,
1452 .release = single_release,
1453 .write = video_proc_write,
1454};
1455
Seth Forshee36d03f92011-09-20 16:55:53 -05001456static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1457{
1458 u32 hci_result;
1459
Azael Avalos893f3f62014-09-29 20:40:09 -06001460 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001461 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001462}
1463
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001464static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Seth Forshee135740d2011-09-20 16:55:49 -05001466 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001467 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 u32 value;
1469
Seth Forshee36d03f92011-09-20 16:55:53 -05001470 ret = get_fan_status(dev, &value);
1471 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001472 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001473 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Seth Forshee36d03f92011-09-20 16:55:53 -05001476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001479static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Al Virod9dda782013-03-31 18:16:14 -04001481 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001482}
1483
1484static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1485 size_t count, loff_t *pos)
1486{
Al Virod9dda782013-03-31 18:16:14 -04001487 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001488 char cmd[42];
1489 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int value;
1491 u32 hci_result;
1492
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001493 len = min(count, sizeof(cmd) - 1);
1494 if (copy_from_user(cmd, buf, len))
1495 return -EFAULT;
1496 cmd[len] = '\0';
1497
1498 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001499 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001500 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001501 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001502 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001503 else
1504 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 } else {
1506 return -EINVAL;
1507 }
1508
1509 return count;
1510}
1511
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001512static const struct file_operations fan_proc_fops = {
1513 .owner = THIS_MODULE,
1514 .open = fan_proc_open,
1515 .read = seq_read,
1516 .llseek = seq_lseek,
1517 .release = single_release,
1518 .write = fan_proc_write,
1519};
1520
1521static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Seth Forshee135740d2011-09-20 16:55:49 -05001523 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 u32 hci_result;
1525 u32 value;
1526
Seth Forshee11948b92011-11-16 17:37:45 -06001527 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001528 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001529 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001530 dev->key_event_valid = 1;
1531 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001532 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001533 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001534 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001535 /*
1536 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001538 * become disabled.
1539 */
Azael Avalos893f3f62014-09-29 20:40:09 -06001540 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001541 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001543 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001544 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 }
1547
Seth Forshee135740d2011-09-20 16:55:49 -05001548 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1549 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001553static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Al Virod9dda782013-03-31 18:16:14 -04001555 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001556}
1557
1558static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1559 size_t count, loff_t *pos)
1560{
Al Virod9dda782013-03-31 18:16:14 -04001561 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001562 char cmd[42];
1563 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int value;
1565
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001566 len = min(count, sizeof(cmd) - 1);
1567 if (copy_from_user(cmd, buf, len))
1568 return -EFAULT;
1569 cmd[len] = '\0';
1570
Azael Avalosb5163992015-02-10 23:43:56 -07001571 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001572 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001573 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 return count;
1577}
1578
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001579static const struct file_operations keys_proc_fops = {
1580 .owner = THIS_MODULE,
1581 .open = keys_proc_open,
1582 .read = seq_read,
1583 .llseek = seq_lseek,
1584 .release = single_release,
1585 .write = keys_proc_write,
1586};
1587
1588static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001590 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1591 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001595static int version_proc_open(struct inode *inode, struct file *file)
1596{
Al Virod9dda782013-03-31 18:16:14 -04001597 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001598}
1599
1600static const struct file_operations version_proc_fops = {
1601 .owner = THIS_MODULE,
1602 .open = version_proc_open,
1603 .read = seq_read,
1604 .llseek = seq_lseek,
1605 .release = single_release,
1606};
1607
Darren Harte0769fe2015-02-11 20:50:08 -08001608/*
1609 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
1611
1612#define PROC_TOSHIBA "toshiba"
1613
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001614static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Seth Forshee36d03f92011-09-20 16:55:53 -05001616 if (dev->backlight_dev)
1617 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1618 &lcd_proc_fops, dev);
1619 if (dev->video_supported)
1620 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1621 &video_proc_fops, dev);
1622 if (dev->fan_supported)
1623 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1624 &fan_proc_fops, dev);
1625 if (dev->hotkey_dev)
1626 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1627 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001628 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1629 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Seth Forshee36d03f92011-09-20 16:55:53 -05001632static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Seth Forshee36d03f92011-09-20 16:55:53 -05001634 if (dev->backlight_dev)
1635 remove_proc_entry("lcd", toshiba_proc_dir);
1636 if (dev->video_supported)
1637 remove_proc_entry("video", toshiba_proc_dir);
1638 if (dev->fan_supported)
1639 remove_proc_entry("fan", toshiba_proc_dir);
1640 if (dev->hotkey_dev)
1641 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001642 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Lionel Debrouxacc24722010-11-16 14:14:02 +01001645static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001646 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001647 .get_brightness = get_lcd_brightness,
1648 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001649};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001650
Azael Avalos360f0f32014-03-25 20:38:31 -06001651/*
1652 * Sysfs files
1653 */
Azael Avalos9d309842015-02-10 23:43:59 -07001654static ssize_t version_show(struct device *dev,
1655 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001656{
1657 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1658}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001659static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001660
Azael Avalos9d309842015-02-10 23:43:59 -07001661static ssize_t fan_store(struct device *dev,
1662 struct device_attribute *attr,
1663 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001664{
1665 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1666 u32 result;
1667 int state;
1668 int ret;
1669
1670 ret = kstrtoint(buf, 0, &state);
1671 if (ret)
1672 return ret;
1673
1674 if (state != 0 && state != 1)
1675 return -EINVAL;
1676
1677 result = hci_write1(toshiba, HCI_FAN, state);
1678 if (result == TOS_FAILURE)
1679 return -EIO;
1680 else if (result == TOS_NOT_SUPPORTED)
1681 return -ENODEV;
1682
1683 return count;
1684}
1685
Azael Avalos9d309842015-02-10 23:43:59 -07001686static ssize_t fan_show(struct device *dev,
1687 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001688{
1689 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1690 u32 value;
1691 int ret;
1692
1693 ret = get_fan_status(toshiba, &value);
1694 if (ret)
1695 return ret;
1696
1697 return sprintf(buf, "%d\n", value);
1698}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001699static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001700
Azael Avalos9d309842015-02-10 23:43:59 -07001701static ssize_t kbd_backlight_mode_store(struct device *dev,
1702 struct device_attribute *attr,
1703 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001704{
1705 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001706 int mode;
1707 int time;
1708 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001709
Dan Carpenteraeaac092014-09-03 14:44:37 +03001710
1711 ret = kstrtoint(buf, 0, &mode);
1712 if (ret)
1713 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001714
1715 /* Check for supported modes depending on keyboard backlight type */
1716 if (toshiba->kbd_type == 1) {
1717 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1718 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1719 return -EINVAL;
1720 } else if (toshiba->kbd_type == 2) {
1721 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1722 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1723 mode != SCI_KBD_MODE_OFF)
1724 return -EINVAL;
1725 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001726
Darren Harte0769fe2015-02-11 20:50:08 -08001727 /*
1728 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001729 * Auto - KBD backlight turns off automatically in given time
1730 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001731 * ON - KBD backlight is always on
1732 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001733 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001734
1735 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001736 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001737 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001738 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001739
1740 /* OR the "base time" to the actual method format */
1741 if (toshiba->kbd_type == 1) {
1742 /* Type 1 requires the current mode */
1743 time |= toshiba->kbd_mode;
1744 } else if (toshiba->kbd_type == 2) {
1745 /* Type 2 requires the desired mode */
1746 time |= mode;
1747 }
1748
Dan Carpenteraeaac092014-09-03 14:44:37 +03001749 ret = toshiba_kbd_illum_status_set(toshiba, time);
1750 if (ret)
1751 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001752
Azael Avalos360f0f32014-03-25 20:38:31 -06001753 toshiba->kbd_mode = mode;
1754 }
1755
1756 return count;
1757}
1758
Azael Avalos9d309842015-02-10 23:43:59 -07001759static ssize_t kbd_backlight_mode_show(struct device *dev,
1760 struct device_attribute *attr,
1761 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001762{
1763 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1764 u32 time;
1765
1766 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1767 return -EIO;
1768
Azael Avalos93f8c162014-09-12 18:50:36 -06001769 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1770}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001771static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001772
Azael Avalos9d309842015-02-10 23:43:59 -07001773static ssize_t kbd_type_show(struct device *dev,
1774 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001775{
1776 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1777
1778 return sprintf(buf, "%d\n", toshiba->kbd_type);
1779}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001780static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001781
Azael Avalos9d309842015-02-10 23:43:59 -07001782static ssize_t available_kbd_modes_show(struct device *dev,
1783 struct device_attribute *attr,
1784 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001785{
1786 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1787
1788 if (toshiba->kbd_type == 1)
1789 return sprintf(buf, "%x %x\n",
1790 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1791
1792 return sprintf(buf, "%x %x %x\n",
1793 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001794}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001795static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001796
Azael Avalos9d309842015-02-10 23:43:59 -07001797static ssize_t kbd_backlight_timeout_store(struct device *dev,
1798 struct device_attribute *attr,
1799 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001800{
1801 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001802 int time;
1803 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001804
Azael Avaloseabde0f2014-10-04 12:02:21 -06001805 ret = kstrtoint(buf, 0, &time);
1806 if (ret)
1807 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001808
Azael Avaloseabde0f2014-10-04 12:02:21 -06001809 /* Check for supported values depending on kbd_type */
1810 if (toshiba->kbd_type == 1) {
1811 if (time < 0 || time > 60)
1812 return -EINVAL;
1813 } else if (toshiba->kbd_type == 2) {
1814 if (time < 1 || time > 60)
1815 return -EINVAL;
1816 }
1817
1818 /* Set the Keyboard Backlight Timeout */
1819
1820 /* Only make a change if the actual timeout has changed */
1821 if (toshiba->kbd_time != time) {
1822 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001823 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001824 /* OR the "base time" to the actual method format */
1825 if (toshiba->kbd_type == 1)
1826 time |= SCI_KBD_MODE_FNZ;
1827 else if (toshiba->kbd_type == 2)
1828 time |= SCI_KBD_MODE_AUTO;
1829
1830 ret = toshiba_kbd_illum_status_set(toshiba, time);
1831 if (ret)
1832 return ret;
1833
Azael Avalos360f0f32014-03-25 20:38:31 -06001834 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1835 }
1836
1837 return count;
1838}
1839
Azael Avalos9d309842015-02-10 23:43:59 -07001840static ssize_t kbd_backlight_timeout_show(struct device *dev,
1841 struct device_attribute *attr,
1842 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001843{
1844 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1845 u32 time;
1846
1847 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1848 return -EIO;
1849
1850 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1851}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001852static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001853
Azael Avalos9d309842015-02-10 23:43:59 -07001854static ssize_t touchpad_store(struct device *dev,
1855 struct device_attribute *attr,
1856 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001857{
1858 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1859 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001860 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001861
1862 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001863 ret = kstrtoint(buf, 0, &state);
1864 if (ret)
1865 return ret;
1866 if (state != 0 && state != 1)
1867 return -EINVAL;
1868
1869 ret = toshiba_touchpad_set(toshiba, state);
1870 if (ret)
1871 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001872
1873 return count;
1874}
1875
Azael Avalos9d309842015-02-10 23:43:59 -07001876static ssize_t touchpad_show(struct device *dev,
1877 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001878{
1879 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1880 u32 state;
1881 int ret;
1882
1883 ret = toshiba_touchpad_get(toshiba, &state);
1884 if (ret < 0)
1885 return ret;
1886
1887 return sprintf(buf, "%i\n", state);
1888}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001889static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001890
Azael Avalos9d309842015-02-10 23:43:59 -07001891static ssize_t position_show(struct device *dev,
1892 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001893{
1894 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1895 u32 xyval, zval, tmp;
1896 u16 x, y, z;
1897 int ret;
1898
1899 xyval = zval = 0;
1900 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1901 if (ret < 0)
1902 return ret;
1903
1904 x = xyval & HCI_ACCEL_MASK;
1905 tmp = xyval >> HCI_MISC_SHIFT;
1906 y = tmp & HCI_ACCEL_MASK;
1907 z = zval & HCI_ACCEL_MASK;
1908
1909 return sprintf(buf, "%d %d %d\n", x, y, z);
1910}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001911static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001912
Azael Avalos9d309842015-02-10 23:43:59 -07001913static ssize_t usb_sleep_charge_show(struct device *dev,
1914 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001915{
1916 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1917 u32 mode;
1918 int ret;
1919
1920 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1921 if (ret < 0)
1922 return ret;
1923
1924 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1925}
1926
Azael Avalos9d309842015-02-10 23:43:59 -07001927static ssize_t usb_sleep_charge_store(struct device *dev,
1928 struct device_attribute *attr,
1929 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001930{
1931 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1932 u32 mode;
1933 int state;
1934 int ret;
1935
1936 ret = kstrtoint(buf, 0, &state);
1937 if (ret)
1938 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001939 /*
1940 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001941 * 0 - Disabled
1942 * 1 - Alternate (Non USB conformant devices that require more power)
1943 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001944 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001945 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001946 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001947 return -EINVAL;
1948
1949 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001950 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001951 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001952 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001953 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001954 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001955 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001956 mode |= SCI_USB_CHARGE_AUTO;
1957 else if (state == 3)
1958 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001959
1960 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1961 if (ret)
1962 return ret;
1963
1964 return count;
1965}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001966static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001967
Azael Avalos182bcaa2015-01-18 18:30:23 -07001968static ssize_t sleep_functions_on_battery_show(struct device *dev,
1969 struct device_attribute *attr,
1970 char *buf)
1971{
1972 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1973 u32 state;
1974 int bat_lvl;
1975 int status;
1976 int ret;
1977 int tmp;
1978
1979 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1980 if (ret < 0)
1981 return ret;
1982
1983 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1984 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1985 status = (tmp == 0x4) ? 1 : 0;
1986 /* Determine the battery level set */
1987 bat_lvl = state >> HCI_MISC_SHIFT;
1988
1989 return sprintf(buf, "%d %d\n", status, bat_lvl);
1990}
1991
1992static ssize_t sleep_functions_on_battery_store(struct device *dev,
1993 struct device_attribute *attr,
1994 const char *buf, size_t count)
1995{
1996 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1997 u32 status;
1998 int value;
1999 int ret;
2000 int tmp;
2001
2002 ret = kstrtoint(buf, 0, &value);
2003 if (ret)
2004 return ret;
2005
Darren Harte0769fe2015-02-11 20:50:08 -08002006 /*
2007 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002008 * 0 - Disabled
2009 * 1-100 - Enabled
2010 */
2011 if (value < 0 || value > 100)
2012 return -EINVAL;
2013
2014 if (value == 0) {
2015 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2016 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2017 } else {
2018 tmp = value << HCI_MISC_SHIFT;
2019 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2020 }
2021 ret = toshiba_sleep_functions_status_set(toshiba, status);
2022 if (ret < 0)
2023 return ret;
2024
2025 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2026
2027 return count;
2028}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002029static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002030
Azael Avalos9d309842015-02-10 23:43:59 -07002031static ssize_t usb_rapid_charge_show(struct device *dev,
2032 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002033{
2034 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2035 u32 state;
2036 int ret;
2037
2038 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2039 if (ret < 0)
2040 return ret;
2041
2042 return sprintf(buf, "%d\n", state);
2043}
2044
Azael Avalos9d309842015-02-10 23:43:59 -07002045static ssize_t usb_rapid_charge_store(struct device *dev,
2046 struct device_attribute *attr,
2047 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002048{
2049 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2050 int state;
2051 int ret;
2052
2053 ret = kstrtoint(buf, 0, &state);
2054 if (ret)
2055 return ret;
2056 if (state != 0 && state != 1)
2057 return -EINVAL;
2058
2059 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2060 if (ret)
2061 return ret;
2062
2063 return count;
2064}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002065static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002066
Azael Avalos9d309842015-02-10 23:43:59 -07002067static ssize_t usb_sleep_music_show(struct device *dev,
2068 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002069{
2070 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2071 u32 state;
2072 int ret;
2073
2074 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2075 if (ret < 0)
2076 return ret;
2077
2078 return sprintf(buf, "%d\n", state);
2079}
2080
Azael Avalos9d309842015-02-10 23:43:59 -07002081static ssize_t usb_sleep_music_store(struct device *dev,
2082 struct device_attribute *attr,
2083 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002084{
2085 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2086 int state;
2087 int ret;
2088
2089 ret = kstrtoint(buf, 0, &state);
2090 if (ret)
2091 return ret;
2092 if (state != 0 && state != 1)
2093 return -EINVAL;
2094
2095 ret = toshiba_usb_sleep_music_set(toshiba, state);
2096 if (ret)
2097 return ret;
2098
2099 return count;
2100}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002101static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002102
Azael Avalos9d309842015-02-10 23:43:59 -07002103static ssize_t kbd_function_keys_show(struct device *dev,
2104 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002105{
2106 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2107 int mode;
2108 int ret;
2109
2110 ret = toshiba_function_keys_get(toshiba, &mode);
2111 if (ret < 0)
2112 return ret;
2113
2114 return sprintf(buf, "%d\n", mode);
2115}
2116
Azael Avalos9d309842015-02-10 23:43:59 -07002117static ssize_t kbd_function_keys_store(struct device *dev,
2118 struct device_attribute *attr,
2119 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002120{
2121 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2122 int mode;
2123 int ret;
2124
2125 ret = kstrtoint(buf, 0, &mode);
2126 if (ret)
2127 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002128 /*
2129 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002130 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2131 * 1 - Special functions (Opposite of the above setting)
2132 */
2133 if (mode != 0 && mode != 1)
2134 return -EINVAL;
2135
2136 ret = toshiba_function_keys_set(toshiba, mode);
2137 if (ret)
2138 return ret;
2139
2140 pr_info("Reboot for changes to KBD Function Keys to take effect");
2141
2142 return count;
2143}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002144static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002145
Azael Avalos9d309842015-02-10 23:43:59 -07002146static ssize_t panel_power_on_show(struct device *dev,
2147 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002148{
2149 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2150 u32 state;
2151 int ret;
2152
2153 ret = toshiba_panel_power_on_get(toshiba, &state);
2154 if (ret < 0)
2155 return ret;
2156
2157 return sprintf(buf, "%d\n", state);
2158}
2159
Azael Avalos9d309842015-02-10 23:43:59 -07002160static ssize_t panel_power_on_store(struct device *dev,
2161 struct device_attribute *attr,
2162 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002163{
2164 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2165 int state;
2166 int ret;
2167
2168 ret = kstrtoint(buf, 0, &state);
2169 if (ret)
2170 return ret;
2171 if (state != 0 && state != 1)
2172 return -EINVAL;
2173
2174 ret = toshiba_panel_power_on_set(toshiba, state);
2175 if (ret)
2176 return ret;
2177
2178 pr_info("Reboot for changes to Panel Power ON to take effect");
2179
2180 return count;
2181}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002182static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002183
Azael Avalos9d309842015-02-10 23:43:59 -07002184static ssize_t usb_three_show(struct device *dev,
2185 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002186{
2187 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2188 u32 state;
2189 int ret;
2190
2191 ret = toshiba_usb_three_get(toshiba, &state);
2192 if (ret < 0)
2193 return ret;
2194
2195 return sprintf(buf, "%d\n", state);
2196}
2197
Azael Avalos9d309842015-02-10 23:43:59 -07002198static ssize_t usb_three_store(struct device *dev,
2199 struct device_attribute *attr,
2200 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002201{
2202 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2203 int state;
2204 int ret;
2205
2206 ret = kstrtoint(buf, 0, &state);
2207 if (ret)
2208 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002209 /*
2210 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002211 * 0 - Disabled (Acts like a USB 2 port, saving power)
2212 * 1 - Enabled
2213 */
2214 if (state != 0 && state != 1)
2215 return -EINVAL;
2216
2217 ret = toshiba_usb_three_set(toshiba, state);
2218 if (ret)
2219 return ret;
2220
2221 pr_info("Reboot for changes to USB 3 to take effect");
2222
2223 return count;
2224}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002225static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002226
2227static struct attribute *toshiba_attributes[] = {
2228 &dev_attr_version.attr,
2229 &dev_attr_fan.attr,
2230 &dev_attr_kbd_backlight_mode.attr,
2231 &dev_attr_kbd_type.attr,
2232 &dev_attr_available_kbd_modes.attr,
2233 &dev_attr_kbd_backlight_timeout.attr,
2234 &dev_attr_touchpad.attr,
2235 &dev_attr_position.attr,
2236 &dev_attr_usb_sleep_charge.attr,
2237 &dev_attr_sleep_functions_on_battery.attr,
2238 &dev_attr_usb_rapid_charge.attr,
2239 &dev_attr_usb_sleep_music.attr,
2240 &dev_attr_kbd_function_keys.attr,
2241 &dev_attr_panel_power_on.attr,
2242 &dev_attr_usb_three.attr,
2243 NULL,
2244};
2245
Azael Avalos360f0f32014-03-25 20:38:31 -06002246static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2247 struct attribute *attr, int idx)
2248{
2249 struct device *dev = container_of(kobj, struct device, kobj);
2250 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2251 bool exists = true;
2252
Azael Avalos94477d42015-02-10 21:09:17 -07002253 if (attr == &dev_attr_fan.attr)
2254 exists = (drv->fan_supported) ? true : false;
2255 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002256 exists = (drv->kbd_illum_supported) ? true : false;
2257 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2258 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002259 else if (attr == &dev_attr_touchpad.attr)
2260 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002261 else if (attr == &dev_attr_position.attr)
2262 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002263 else if (attr == &dev_attr_usb_sleep_charge.attr)
2264 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002265 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2266 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002267 else if (attr == &dev_attr_usb_rapid_charge.attr)
2268 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002269 else if (attr == &dev_attr_usb_sleep_music.attr)
2270 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002271 else if (attr == &dev_attr_kbd_function_keys.attr)
2272 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002273 else if (attr == &dev_attr_panel_power_on.attr)
2274 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002275 else if (attr == &dev_attr_usb_three.attr)
2276 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002277
2278 return exists ? attr->mode : 0;
2279}
2280
Azael Avalos9bd12132015-02-10 23:43:58 -07002281static struct attribute_group toshiba_attr_group = {
2282 .is_visible = toshiba_sysfs_is_visible,
2283 .attrs = toshiba_attributes,
2284};
2285
Azael Avalos1f28f292014-12-04 20:22:45 -07002286/*
2287 * Hotkeys
2288 */
2289static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2290{
2291 acpi_status status;
2292 u32 result;
2293
2294 status = acpi_evaluate_object(dev->acpi_dev->handle,
2295 "ENAB", NULL, NULL);
2296 if (ACPI_FAILURE(status))
2297 return -ENODEV;
2298
2299 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2300 if (result == TOS_FAILURE)
2301 return -EIO;
2302 else if (result == TOS_NOT_SUPPORTED)
2303 return -ENODEV;
2304
2305 return 0;
2306}
2307
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002308static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2309{
2310 u32 result;
2311
2312 /*
2313 * Re-activate the hotkeys, but this time, we are using the
2314 * "Special Functions" mode.
2315 */
2316 result = hci_write1(dev, HCI_HOTKEY_EVENT,
2317 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2318 if (result != TOS_SUCCESS)
2319 pr_err("Could not enable the Special Function mode\n");
2320}
2321
Seth Forshee29cd2932012-01-18 13:44:09 -06002322static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2323 struct serio *port)
2324{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002325 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002326 return false;
2327
2328 if (unlikely(data == 0xe0))
2329 return false;
2330
2331 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2332 schedule_work(&toshiba_acpi->hotkey_work);
2333 return true;
2334 }
2335
2336 return false;
2337}
2338
2339static void toshiba_acpi_hotkey_work(struct work_struct *work)
2340{
2341 acpi_handle ec_handle = ec_get_handle();
2342 acpi_status status;
2343
2344 if (!ec_handle)
2345 return;
2346
2347 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2348 if (ACPI_FAILURE(status))
2349 pr_err("ACPI NTFY method execution failed\n");
2350}
2351
2352/*
2353 * Returns hotkey scancode, or < 0 on failure.
2354 */
2355static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2356{
Zhang Rui74facaf2013-09-03 08:32:15 +08002357 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002358 acpi_status status;
2359
Zhang Rui74facaf2013-09-03 08:32:15 +08002360 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2361 NULL, &value);
2362 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002363 pr_err("ACPI INFO method execution failed\n");
2364 return -EIO;
2365 }
2366
Zhang Rui74facaf2013-09-03 08:32:15 +08002367 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002368}
2369
2370static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2371 int scancode)
2372{
2373 if (scancode == 0x100)
2374 return;
2375
Darren Harte0769fe2015-02-11 20:50:08 -08002376 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002377 if (scancode & 0x80)
2378 return;
2379
2380 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2381 pr_info("Unknown key %x\n", scancode);
2382}
2383
Azael Avalos71454d72014-12-04 20:22:46 -07002384static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2385{
2386 u32 hci_result, value;
2387 int retries = 3;
2388 int scancode;
2389
2390 if (dev->info_supported) {
2391 scancode = toshiba_acpi_query_hotkey(dev);
2392 if (scancode < 0)
2393 pr_err("Failed to query hotkey event\n");
2394 else if (scancode != 0)
2395 toshiba_acpi_report_hotkey(dev, scancode);
2396 } else if (dev->system_event_supported) {
2397 do {
2398 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2399 switch (hci_result) {
2400 case TOS_SUCCESS:
2401 toshiba_acpi_report_hotkey(dev, (int)value);
2402 break;
2403 case TOS_NOT_SUPPORTED:
2404 /*
2405 * This is a workaround for an unresolved
2406 * issue on some machines where system events
2407 * sporadically become disabled.
2408 */
2409 hci_result =
2410 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2411 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002412 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002413 default:
2414 retries--;
2415 break;
2416 }
2417 } while (retries && hci_result != TOS_FIFO_EMPTY);
2418 }
2419}
2420
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002421static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002422{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002423 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002424 acpi_handle ec_handle;
2425 u32 events_type;
2426 u32 hci_result;
2427 int error;
2428
2429 error = toshiba_acpi_enable_hotkeys(dev);
2430 if (error)
2431 return error;
2432
2433 error = toshiba_hotkey_event_type_get(dev, &events_type);
2434 if (error) {
2435 pr_err("Unable to query Hotkey Event Type\n");
2436 return error;
2437 }
2438 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002439
Seth Forshee135740d2011-09-20 16:55:49 -05002440 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002441 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002442 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002443
2444 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002445 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002446 dev->hotkey_dev->id.bustype = BUS_HOST;
2447
Azael Avalosa2b34712015-03-20 16:55:17 -06002448 if (events_type == HCI_SYSTEM_TYPE1 ||
2449 !dev->kbd_function_keys_supported)
2450 keymap = toshiba_acpi_keymap;
2451 else if (events_type == HCI_SYSTEM_TYPE2 ||
2452 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002453 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002454 else
2455 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002456 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002457 if (error)
2458 goto err_free_dev;
2459
Seth Forshee29cd2932012-01-18 13:44:09 -06002460 /*
2461 * For some machines the SCI responsible for providing hotkey
2462 * notification doesn't fire. We can trigger the notification
2463 * whenever the Fn key is pressed using the NTFY method, if
2464 * supported, so if it's present set up an i8042 key filter
2465 * for this purpose.
2466 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002467 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002468 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002469 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2470
2471 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2472 if (error) {
2473 pr_err("Error installing key filter\n");
2474 goto err_free_keymap;
2475 }
2476
2477 dev->ntfy_supported = 1;
2478 }
2479
2480 /*
2481 * Determine hotkey query interface. Prefer using the INFO
2482 * method when it is available.
2483 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002484 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002485 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002486 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002487 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002488 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002489 dev->system_event_supported = 1;
2490 }
2491
2492 if (!dev->info_supported && !dev->system_event_supported) {
2493 pr_warn("No hotkey query interface found\n");
2494 goto err_remove_filter;
2495 }
2496
Seth Forshee135740d2011-09-20 16:55:49 -05002497 error = input_register_device(dev->hotkey_dev);
2498 if (error) {
2499 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002500 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002501 }
2502
2503 return 0;
2504
Seth Forshee29cd2932012-01-18 13:44:09 -06002505 err_remove_filter:
2506 if (dev->ntfy_supported)
2507 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002508 err_free_keymap:
2509 sparse_keymap_free(dev->hotkey_dev);
2510 err_free_dev:
2511 input_free_device(dev->hotkey_dev);
2512 dev->hotkey_dev = NULL;
2513 return error;
2514}
2515
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002516static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002517{
2518 struct backlight_properties props;
2519 int brightness;
2520 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002521 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002522
2523 /*
2524 * Some machines don't support the backlight methods at all, and
2525 * others support it read-only. Either of these is pretty useless,
2526 * so only register the backlight device if the backlight method
2527 * supports both reads and writes.
2528 */
2529 brightness = __get_lcd_brightness(dev);
2530 if (brightness < 0)
2531 return 0;
2532 ret = set_lcd_brightness(dev, brightness);
2533 if (ret) {
2534 pr_debug("Backlight method is read-only, disabling backlight support\n");
2535 return 0;
2536 }
2537
Akio Idehara121b7b02012-04-06 01:46:43 +09002538 /* Determine whether or not BIOS supports transflective backlight */
2539 ret = get_tr_backlight_status(dev, &enabled);
2540 dev->tr_backlight_supported = !ret;
2541
Hans de Goede358d6a22015-04-21 12:01:32 +02002542 /*
2543 * Tell acpi-video-detect code to prefer vendor backlight on all
2544 * systems with transflective backlight and on dmi matched systems.
2545 */
2546 if (dev->tr_backlight_supported ||
2547 dmi_check_system(toshiba_vendor_backlight_dmi))
2548 acpi_video_dmi_promote_vendor();
2549
2550 if (acpi_video_backlight_support())
2551 return 0;
2552
2553 /* acpi-video may have loaded before we called dmi_promote_vendor() */
2554 acpi_video_unregister_backlight();
2555
Matthew Garrett53039f22012-06-01 11:02:36 -04002556 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002557 props.type = BACKLIGHT_PLATFORM;
2558 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002559
Darren Harte0769fe2015-02-11 20:50:08 -08002560 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002561 if (dev->tr_backlight_supported)
2562 props.max_brightness++;
2563
Seth Forshee62cce752012-04-19 11:23:50 -05002564 dev->backlight_dev = backlight_device_register("toshiba",
2565 &dev->acpi_dev->dev,
2566 dev,
2567 &toshiba_backlight_data,
2568 &props);
2569 if (IS_ERR(dev->backlight_dev)) {
2570 ret = PTR_ERR(dev->backlight_dev);
2571 pr_err("Could not register toshiba backlight device\n");
2572 dev->backlight_dev = NULL;
2573 return ret;
2574 }
2575
2576 dev->backlight_dev->props.brightness = brightness;
2577 return 0;
2578}
2579
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002580static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002581{
2582 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2583
Seth Forshee36d03f92011-09-20 16:55:53 -05002584 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002585
Azael Avalos360f0f32014-03-25 20:38:31 -06002586 if (dev->sysfs_created)
2587 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2588 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002589
Seth Forshee29cd2932012-01-18 13:44:09 -06002590 if (dev->ntfy_supported) {
2591 i8042_remove_filter(toshiba_acpi_i8042_filter);
2592 cancel_work_sync(&dev->hotkey_work);
2593 }
2594
Seth Forshee135740d2011-09-20 16:55:49 -05002595 if (dev->hotkey_dev) {
2596 input_unregister_device(dev->hotkey_dev);
2597 sparse_keymap_free(dev->hotkey_dev);
2598 }
2599
Markus Elfring00981812014-11-24 20:30:29 +01002600 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002601
Seth Forshee36d03f92011-09-20 16:55:53 -05002602 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002603 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002604
Azael Avalos360f0f32014-03-25 20:38:31 -06002605 if (dev->kbd_led_registered)
2606 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002607
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002608 if (dev->eco_supported)
2609 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002610
Seth Forshee29cd2932012-01-18 13:44:09 -06002611 if (toshiba_acpi)
2612 toshiba_acpi = NULL;
2613
Seth Forshee135740d2011-09-20 16:55:49 -05002614 kfree(dev);
2615
2616 return 0;
2617}
2618
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002619static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002620{
Zhang Ruie2e19602013-09-03 08:32:06 +08002621 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002622 return "GHCI";
2623
Zhang Ruie2e19602013-09-03 08:32:06 +08002624 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002625 return "SPFC";
2626
2627 return NULL;
2628}
2629
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002630static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002631{
2632 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002633 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002634 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002635 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002636 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002637
Seth Forshee29cd2932012-01-18 13:44:09 -06002638 if (toshiba_acpi)
2639 return -EBUSY;
2640
Seth Forshee135740d2011-09-20 16:55:49 -05002641 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2642 TOSHIBA_ACPI_VERSION);
2643
Seth Forsheea540d6b2011-09-20 16:55:52 -05002644 hci_method = find_hci_method(acpi_dev->handle);
2645 if (!hci_method) {
2646 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002647 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002648 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002649
Seth Forshee135740d2011-09-20 16:55:49 -05002650 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2651 if (!dev)
2652 return -ENOMEM;
2653 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002654 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002655 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002656 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002657
Azael Avalosa2b34712015-03-20 16:55:17 -06002658 /* Query the BIOS for supported features */
2659
2660 /*
2661 * The "Special Functions" are always supported by the laptops
2662 * with the new keyboard layout, query for its presence to help
2663 * determine the keymap layout to use.
2664 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002665 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002666 dev->kbd_function_keys_supported = !ret;
2667
Seth Forshee6e02cc72011-09-20 16:55:51 -05002668 if (toshiba_acpi_setup_keyboard(dev))
2669 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002670
Seth Forshee62cce752012-04-19 11:23:50 -05002671 ret = toshiba_acpi_setup_backlight(dev);
2672 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002673 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002674
Seth Forshee135740d2011-09-20 16:55:49 -05002675 if (toshiba_illumination_available(dev)) {
2676 dev->led_dev.name = "toshiba::illumination";
2677 dev->led_dev.max_brightness = 1;
2678 dev->led_dev.brightness_set = toshiba_illumination_set;
2679 dev->led_dev.brightness_get = toshiba_illumination_get;
2680 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002681 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002682 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002683
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002684 if (toshiba_eco_mode_available(dev)) {
2685 dev->eco_led.name = "toshiba::eco_mode";
2686 dev->eco_led.max_brightness = 1;
2687 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2688 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2689 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2690 dev->eco_supported = 1;
2691 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002692
Azael Avalos93f8c162014-09-12 18:50:36 -06002693 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002694 /*
2695 * Only register the LED if KBD illumination is supported
2696 * and the keyboard backlight operation mode is set to FN-Z
2697 */
2698 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2699 dev->kbd_led.name = "toshiba::kbd_backlight";
2700 dev->kbd_led.max_brightness = 1;
2701 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2702 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2703 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2704 dev->kbd_led_registered = 1;
2705 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002706
Azael Avalos9d8658a2014-03-25 20:38:32 -06002707 ret = toshiba_touchpad_get(dev, &dummy);
2708 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002709
Azael Avalos5a2813e2014-03-25 20:38:34 -06002710 ret = toshiba_accelerometer_supported(dev);
2711 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002712
Azael Avalosc8c91842015-04-02 19:26:20 -06002713 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002714
Azael Avalosbb3fe012015-01-18 18:30:24 -07002715 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2716 dev->usb_rapid_charge_supported = !ret;
2717
Azael Avalos172ce0a2015-01-18 18:30:25 -07002718 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2719 dev->usb_sleep_music_supported = !ret;
2720
Azael Avalos35d53ce2015-02-10 21:09:19 -07002721 ret = toshiba_panel_power_on_get(dev, &dummy);
2722 dev->panel_power_on_supported = !ret;
2723
Azael Avalos17fe4b32015-02-10 21:09:20 -07002724 ret = toshiba_usb_three_get(dev, &dummy);
2725 dev->usb_three_supported = !ret;
2726
Seth Forshee36d03f92011-09-20 16:55:53 -05002727 ret = get_video_status(dev, &dummy);
2728 dev->video_supported = !ret;
2729
2730 ret = get_fan_status(dev, &dummy);
2731 dev->fan_supported = !ret;
2732
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002733 /*
2734 * Enable the "Special Functions" mode only if they are
2735 * supported and if they are activated.
2736 */
2737 if (dev->kbd_function_keys_supported && special_functions)
2738 toshiba_acpi_enable_special_functions(dev);
2739
Azael Avalos360f0f32014-03-25 20:38:31 -06002740 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2741 &toshiba_attr_group);
2742 if (ret) {
2743 dev->sysfs_created = 0;
2744 goto error;
2745 }
2746 dev->sysfs_created = !ret;
2747
Seth Forshee36d03f92011-09-20 16:55:53 -05002748 create_toshiba_proc_entries(dev);
2749
Seth Forshee29cd2932012-01-18 13:44:09 -06002750 toshiba_acpi = dev;
2751
Seth Forshee135740d2011-09-20 16:55:49 -05002752 return 0;
2753
2754error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002755 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002756 return ret;
2757}
2758
2759static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2760{
2761 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002762 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002763
Azael Avalos71454d72014-12-04 20:22:46 -07002764 switch (event) {
2765 case 0x80: /* Hotkeys and some system events */
2766 toshiba_acpi_process_hotkeys(dev);
2767 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002768 case 0x81: /* Dock events */
2769 case 0x82:
2770 case 0x83:
2771 pr_info("Dock event received %x\n", event);
2772 break;
2773 case 0x88: /* Thermal events */
2774 pr_info("Thermal event received\n");
2775 break;
2776 case 0x8f: /* LID closed */
2777 case 0x90: /* LID is closed and Dock has been ejected */
2778 break;
2779 case 0x8c: /* SATA power events */
2780 case 0x8b:
2781 pr_info("SATA power event received %x\n", event);
2782 break;
Azael Avalos80546902014-12-04 20:22:47 -07002783 case 0x92: /* Keyboard backlight mode changed */
2784 /* Update sysfs entries */
2785 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2786 &toshiba_attr_group);
2787 if (ret)
2788 pr_err("Unable to update sysfs entries\n");
2789 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002790 case 0x85: /* Unknown */
2791 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002792 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002793 case 0x94: /* Unknown */
2794 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002795 default:
2796 pr_info("Unknown event received %x\n", event);
2797 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002798 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002799
2800 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2801 dev_name(&acpi_dev->dev),
2802 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002803}
2804
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002805#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002806static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002807{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002808 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002809 u32 result;
2810
2811 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002812 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002813
2814 return 0;
2815}
2816
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002817static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002818{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002819 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002820 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002821
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002822 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002823 error = toshiba_acpi_enable_hotkeys(dev);
2824 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002825 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002826 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002827
2828 return 0;
2829}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002830#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002831
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002832static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2833 toshiba_acpi_suspend, toshiba_acpi_resume);
2834
Seth Forshee135740d2011-09-20 16:55:49 -05002835static struct acpi_driver toshiba_acpi_driver = {
2836 .name = "Toshiba ACPI driver",
2837 .owner = THIS_MODULE,
2838 .ids = toshiba_device_ids,
2839 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2840 .ops = {
2841 .add = toshiba_acpi_add,
2842 .remove = toshiba_acpi_remove,
2843 .notify = toshiba_acpi_notify,
2844 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002845 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002846};
Holger Machtc9263552006-10-20 14:30:29 -07002847
Len Brown4be44fc2005-08-05 00:44:28 -04002848static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Seth Forshee135740d2011-09-20 16:55:49 -05002850 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Seth Forsheef11f9992012-01-18 13:44:11 -06002852 /*
2853 * Machines with this WMI guid aren't supported due to bugs in
2854 * their AML. This check relies on wmi initializing before
2855 * toshiba_acpi to guarantee guids have been identified.
2856 */
2857 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2858 return -ENODEV;
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2861 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002862 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002863 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
Seth Forshee135740d2011-09-20 16:55:49 -05002866 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2867 if (ret) {
2868 pr_err("Failed to register ACPI driver: %d\n", ret);
2869 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002870 }
2871
Seth Forshee135740d2011-09-20 16:55:49 -05002872 return ret;
2873}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002874
Seth Forshee135740d2011-09-20 16:55:49 -05002875static void __exit toshiba_acpi_exit(void)
2876{
2877 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2878 if (toshiba_proc_dir)
2879 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882module_init(toshiba_acpi_init);
2883module_exit(toshiba_acpi_exit);