blob: e59376220f00400f6a1dc9a20ebfe9b1ba0fe7eb [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
Darren Harte0769fe2015-02-11 20:50:08 -0800358/*
359 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600360 */
361
362static int sci_open(struct toshiba_acpi_dev *dev)
363{
Azael Avalos258c5902014-09-29 20:40:07 -0600364 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
365 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600366 acpi_status status;
367
Azael Avalos258c5902014-09-29 20:40:07 -0600368 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600369 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600370 pr_err("ACPI call to open SCI failed\n");
371 return 0;
372 }
373
Azael Avalos1864bbc2014-09-29 20:40:08 -0600374 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600375 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600376 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600377 pr_info("Toshiba SCI already opened\n");
378 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700379 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800380 /*
381 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700382 * implemented and return 0x8000 (Not Supported), failing to
383 * register some supported features.
384 *
385 * Simply return 1 if we hit those affected laptops to make the
386 * supported features work.
387 *
388 * In the case that some laptops really do not support the SCI,
389 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
390 * and thus, not registering support for the queried feature.
391 */
392 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600393 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600394 pr_info("Toshiba SCI is not present\n");
395 }
396
397 return 0;
398}
399
400static void sci_close(struct toshiba_acpi_dev *dev)
401{
Azael Avalos258c5902014-09-29 20:40:07 -0600402 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
403 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600404 acpi_status status;
405
Azael Avalos258c5902014-09-29 20:40:07 -0600406 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600407 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600408 pr_err("ACPI call to close SCI failed\n");
409 return;
410 }
411
Azael Avalos1864bbc2014-09-29 20:40:08 -0600412 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600413 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600414 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600415 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600416 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600417 pr_info("Toshiba SCI is not present\n");
418}
419
Azael Avalos893f3f62014-09-29 20:40:09 -0600420static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600421{
Azael Avalos258c5902014-09-29 20:40:07 -0600422 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
423 u32 out[TCI_WORDS];
424 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700425
Azael Avalos893f3f62014-09-29 20:40:09 -0600426 if (ACPI_FAILURE(status))
427 return TOS_FAILURE;
428
Azael Avalos84a62732014-03-25 20:38:29 -0600429 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600430
431 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600432}
433
Azael Avalos893f3f62014-09-29 20:40:09 -0600434static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600435{
Azael Avalos258c5902014-09-29 20:40:07 -0600436 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
437 u32 out[TCI_WORDS];
438 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600439
440 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600441}
442
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200443/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500444static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200445{
Azael Avalos258c5902014-09-29 20:40:07 -0600446 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
447 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200448 acpi_status status;
449
Azael Avalosfdb79082014-03-25 20:38:30 -0600450 if (!sci_open(dev))
451 return 0;
452
Azael Avalos258c5902014-09-29 20:40:07 -0600453 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600454 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600455 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600456 pr_err("ACPI call to query Illumination support failed\n");
457 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600458 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700459 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200460 return 0;
461 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600462
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200463 return 1;
464}
465
466static void toshiba_illumination_set(struct led_classdev *cdev,
467 enum led_brightness brightness)
468{
Seth Forshee135740d2011-09-20 16:55:49 -0500469 struct toshiba_acpi_dev *dev = container_of(cdev,
470 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600471 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200472
473 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600474 if (!sci_open(dev))
475 return;
476
477 /* Switch the illumination on/off */
478 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600479 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600480 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600481 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600482 pr_err("ACPI call for illumination failed\n");
483 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600484 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600485 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200486 return;
487 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200488}
489
490static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
491{
Seth Forshee135740d2011-09-20 16:55:49 -0500492 struct toshiba_acpi_dev *dev = container_of(cdev,
493 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600494 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200495
496 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600497 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200498 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200499
500 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600501 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600502 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600503 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600504 pr_err("ACPI call for illumination failed\n");
505 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600506 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200508 return LED_OFF;
509 }
510
Azael Avalosfdb79082014-03-25 20:38:30 -0600511 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200512}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400513
Azael Avalos360f0f32014-03-25 20:38:31 -0600514/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600515static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
516{
Azael Avalos258c5902014-09-29 20:40:07 -0600517 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
518 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600519 acpi_status status;
520
521 if (!sci_open(dev))
522 return 0;
523
Azael Avalos258c5902014-09-29 20:40:07 -0600524 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600525 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600526 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600527 pr_err("ACPI call to query kbd illumination support failed\n");
528 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600529 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600530 pr_info("Keyboard illumination not available\n");
531 return 0;
532 }
533
Darren Harte0769fe2015-02-11 20:50:08 -0800534 /*
535 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600536 * previous kbd backlight implementation set this to
537 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800538 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600539 */
540 if (out[3] == SCI_KBD_TIME_MAX)
541 dev->kbd_type = 2;
542 else
543 dev->kbd_type = 1;
544 /* Get the current keyboard backlight mode */
545 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
546 /* Get the current time (1-60 seconds) */
547 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
548
549 return 1;
550}
551
Azael Avalos360f0f32014-03-25 20:38:31 -0600552static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
553{
554 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600555
556 if (!sci_open(dev))
557 return -EIO;
558
Azael Avalos893f3f62014-09-29 20:40:09 -0600559 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600560 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600561 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600562 pr_err("ACPI call to set KBD backlight status failed\n");
563 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600564 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600565 pr_info("Keyboard backlight status not supported\n");
566 return -ENODEV;
567 }
568
569 return 0;
570}
571
572static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
573{
574 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600575
576 if (!sci_open(dev))
577 return -EIO;
578
Azael Avalos893f3f62014-09-29 20:40:09 -0600579 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600580 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600581 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600582 pr_err("ACPI call to get KBD backlight status failed\n");
583 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600584 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600585 pr_info("Keyboard backlight status not supported\n");
586 return -ENODEV;
587 }
588
589 return 0;
590}
591
592static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
593{
594 struct toshiba_acpi_dev *dev = container_of(cdev,
595 struct toshiba_acpi_dev, kbd_led);
596 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600597
598 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600599 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
600 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600601 pr_err("ACPI call to get the keyboard backlight failed\n");
602 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600603 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600604 pr_info("Keyboard backlight not supported\n");
605 return LED_OFF;
606 }
607
608 return state ? LED_FULL : LED_OFF;
609}
610
611static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
612 enum led_brightness brightness)
613{
614 struct toshiba_acpi_dev *dev = container_of(cdev,
615 struct toshiba_acpi_dev, kbd_led);
616 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600617
618 /* Set the keyboard backlight state */
619 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600620 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
621 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600622 pr_err("ACPI call to set KBD Illumination mode failed\n");
623 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600624 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600625 pr_info("Keyboard backlight not supported\n");
626 return;
627 }
628}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400629
Azael Avalos9d8658a2014-03-25 20:38:32 -0600630/* TouchPad support */
631static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
632{
633 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600634
635 if (!sci_open(dev))
636 return -EIO;
637
Azael Avalos893f3f62014-09-29 20:40:09 -0600638 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600639 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600640 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600641 pr_err("ACPI call to set the touchpad failed\n");
642 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600643 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600644 return -ENODEV;
645 }
646
647 return 0;
648}
649
650static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
651{
652 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600653
654 if (!sci_open(dev))
655 return -EIO;
656
Azael Avalos893f3f62014-09-29 20:40:09 -0600657 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600658 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600659 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600660 pr_err("ACPI call to query the touchpad failed\n");
661 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600662 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600663 return -ENODEV;
664 }
665
666 return 0;
667}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200668
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600669/* Eco Mode support */
670static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
671{
672 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700673 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600674 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600675
Azael Avalos258c5902014-09-29 20:40:07 -0600676 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700677 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
678 pr_err("ACPI call to get ECO led failed\n");
679 } else if (out[0] == TOS_NOT_INSTALLED) {
680 pr_info("ECO led not installed");
681 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800682 /*
683 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700684 * LED device is present, but that we just screwed the input
685 * parameters.
686 *
687 * Let's query the status of the LED to see if we really have a
688 * success response, indicating the actual presense of the LED,
689 * bail out otherwise.
690 */
691 in[3] = 1;
692 status = tci_raw(dev, in, out);
693 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
694 pr_err("ACPI call to get ECO led failed\n");
695 else if (out[0] == TOS_SUCCESS)
696 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600697 }
698
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700699 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600700}
701
Azael Avalosb5163992015-02-10 23:43:56 -0700702static enum led_brightness
703toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600704{
705 struct toshiba_acpi_dev *dev = container_of(cdev,
706 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600707 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
708 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600709 acpi_status status;
710
Azael Avalos258c5902014-09-29 20:40:07 -0600711 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600712 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600713 pr_err("ACPI call to get ECO led failed\n");
714 return LED_OFF;
715 }
716
717 return out[2] ? LED_FULL : LED_OFF;
718}
719
720static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
721 enum led_brightness brightness)
722{
723 struct toshiba_acpi_dev *dev = container_of(cdev,
724 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600725 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
726 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600727 acpi_status status;
728
729 /* Switch the Eco Mode led on/off */
730 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600731 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600732 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600733 pr_err("ACPI call to set ECO led failed\n");
734 return;
735 }
736}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400737
Azael Avalos5a2813e2014-03-25 20:38:34 -0600738/* Accelerometer support */
739static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
740{
Azael Avalos258c5902014-09-29 20:40:07 -0600741 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
742 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600743 acpi_status status;
744
Darren Harte0769fe2015-02-11 20:50:08 -0800745 /*
746 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600747 * this call also serves as initialization
748 */
Azael Avalos258c5902014-09-29 20:40:07 -0600749 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600750 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600751 pr_err("ACPI call to query the accelerometer failed\n");
752 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600753 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
754 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600755 pr_err("Accelerometer not initialized\n");
756 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600757 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600758 pr_info("Accelerometer not supported\n");
759 return -ENODEV;
760 }
761
762 return 0;
763}
764
765static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
766 u32 *xy, u32 *z)
767{
Azael Avalos258c5902014-09-29 20:40:07 -0600768 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
769 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600770 acpi_status status;
771
772 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600773 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600774 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600775 pr_err("ACPI call to query the accelerometer failed\n");
776 return -EIO;
777 }
778
779 *xy = out[2];
780 *z = out[4];
781
782 return 0;
783}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600784
Azael Avalose26ffe52015-01-18 18:30:22 -0700785/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600786static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
787{
788 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
789 u32 out[TCI_WORDS];
790 acpi_status status;
791
792 /* Set the feature to "not supported" in case of error */
793 dev->usb_sleep_charge_supported = 0;
794
795 if (!sci_open(dev))
796 return;
797
798 status = tci_raw(dev, in, out);
799 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
800 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
801 sci_close(dev);
802 return;
803 } else if (out[0] == TOS_NOT_SUPPORTED) {
804 pr_info("USB Sleep and Charge not supported\n");
805 sci_close(dev);
806 return;
807 } else if (out[0] == TOS_SUCCESS) {
808 dev->usbsc_mode_base = out[4];
809 }
810
811 in[5] = SCI_USB_CHARGE_BAT_LVL;
812 status = tci_raw(dev, in, out);
813 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
814 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
815 sci_close(dev);
816 return;
817 } else if (out[0] == TOS_NOT_SUPPORTED) {
818 pr_info("USB Sleep and Charge not supported\n");
819 sci_close(dev);
820 return;
821 } else if (out[0] == TOS_SUCCESS) {
822 dev->usbsc_bat_level = out[2];
823 /*
824 * If we reach this point, it means that the laptop has support
825 * for this feature and all values are initialized.
826 * Set it as supported.
827 */
828 dev->usb_sleep_charge_supported = 1;
829 }
830
831 sci_close(dev);
832}
833
Azael Avalose26ffe52015-01-18 18:30:22 -0700834static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
835 u32 *mode)
836{
837 u32 result;
838
839 if (!sci_open(dev))
840 return -EIO;
841
842 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
843 sci_close(dev);
844 if (result == TOS_FAILURE) {
845 pr_err("ACPI call to set USB S&C mode failed\n");
846 return -EIO;
847 } else if (result == TOS_NOT_SUPPORTED) {
848 pr_info("USB Sleep and Charge not supported\n");
849 return -ENODEV;
850 } else if (result == TOS_INPUT_DATA_ERROR) {
851 return -EIO;
852 }
853
854 return 0;
855}
856
857static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
858 u32 mode)
859{
860 u32 result;
861
862 if (!sci_open(dev))
863 return -EIO;
864
865 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
866 sci_close(dev);
867 if (result == TOS_FAILURE) {
868 pr_err("ACPI call to set USB S&C mode failed\n");
869 return -EIO;
870 } else if (result == TOS_NOT_SUPPORTED) {
871 pr_info("USB Sleep and Charge not supported\n");
872 return -ENODEV;
873 } else if (result == TOS_INPUT_DATA_ERROR) {
874 return -EIO;
875 }
876
877 return 0;
878}
879
Azael Avalos182bcaa2015-01-18 18:30:23 -0700880static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
881 u32 *mode)
882{
883 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
884 u32 out[TCI_WORDS];
885 acpi_status status;
886
887 if (!sci_open(dev))
888 return -EIO;
889
890 in[5] = SCI_USB_CHARGE_BAT_LVL;
891 status = tci_raw(dev, in, out);
892 sci_close(dev);
893 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
894 pr_err("ACPI call to get USB S&C battery level failed\n");
895 return -EIO;
896 } else if (out[0] == TOS_NOT_SUPPORTED) {
897 pr_info("USB Sleep and Charge not supported\n");
898 return -ENODEV;
899 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
900 return -EIO;
901 }
902
903 *mode = out[2];
904
905 return 0;
906}
907
908static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
909 u32 mode)
910{
911 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
912 u32 out[TCI_WORDS];
913 acpi_status status;
914
915 if (!sci_open(dev))
916 return -EIO;
917
918 in[2] = mode;
919 in[5] = SCI_USB_CHARGE_BAT_LVL;
920 status = tci_raw(dev, in, out);
921 sci_close(dev);
922 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
923 pr_err("ACPI call to set USB S&C battery level failed\n");
924 return -EIO;
925 } else if (out[0] == TOS_NOT_SUPPORTED) {
926 pr_info("USB Sleep and Charge not supported\n");
927 return -ENODEV;
928 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
929 return -EIO;
930 }
931
932 return 0;
933}
934
Azael Avalosbb3fe012015-01-18 18:30:24 -0700935static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
936 u32 *state)
937{
938 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
939 u32 out[TCI_WORDS];
940 acpi_status status;
941
942 if (!sci_open(dev))
943 return -EIO;
944
945 in[5] = SCI_USB_CHARGE_RAPID_DSP;
946 status = tci_raw(dev, in, out);
947 sci_close(dev);
948 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600949 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700950 return -EIO;
951 } else if (out[0] == TOS_NOT_SUPPORTED ||
952 out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600953 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700954 return -ENODEV;
955 }
956
957 *state = out[2];
958
959 return 0;
960}
961
962static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
963 u32 state)
964{
965 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
966 u32 out[TCI_WORDS];
967 acpi_status status;
968
969 if (!sci_open(dev))
970 return -EIO;
971
972 in[2] = state;
973 in[5] = SCI_USB_CHARGE_RAPID_DSP;
974 status = tci_raw(dev, in, out);
975 sci_close(dev);
976 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600977 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700978 return -EIO;
979 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600980 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700981 return -ENODEV;
982 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
983 return -EIO;
984 }
985
986 return 0;
987}
988
Azael Avalos172ce0a2015-01-18 18:30:25 -0700989static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
990{
991 u32 result;
992
993 if (!sci_open(dev))
994 return -EIO;
995
996 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
997 sci_close(dev);
998 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600999 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001000 return -EIO;
1001 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001002 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001003 return -ENODEV;
1004 } else if (result == TOS_INPUT_DATA_ERROR) {
1005 return -EIO;
1006 }
1007
1008 return 0;
1009}
1010
1011static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1012{
1013 u32 result;
1014
1015 if (!sci_open(dev))
1016 return -EIO;
1017
1018 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1019 sci_close(dev);
1020 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001021 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001022 return -EIO;
1023 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001024 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001025 return -ENODEV;
1026 } else if (result == TOS_INPUT_DATA_ERROR) {
1027 return -EIO;
1028 }
1029
1030 return 0;
1031}
1032
Azael Avalosbae84192015-02-10 21:09:18 -07001033/* Keyboard function keys */
1034static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1035{
1036 u32 result;
1037
1038 if (!sci_open(dev))
1039 return -EIO;
1040
1041 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1042 sci_close(dev);
1043 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1044 pr_err("ACPI call to get KBD function keys failed\n");
1045 return -EIO;
1046 } else if (result == TOS_NOT_SUPPORTED) {
1047 pr_info("KBD function keys not supported\n");
1048 return -ENODEV;
1049 }
1050
1051 return 0;
1052}
1053
1054static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1055{
1056 u32 result;
1057
1058 if (!sci_open(dev))
1059 return -EIO;
1060
1061 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1062 sci_close(dev);
1063 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1064 pr_err("ACPI call to set KBD function keys failed\n");
1065 return -EIO;
1066 } else if (result == TOS_NOT_SUPPORTED) {
1067 pr_info("KBD function keys not supported\n");
1068 return -ENODEV;
1069 }
1070
1071 return 0;
1072}
1073
Azael Avalos35d53ce2015-02-10 21:09:19 -07001074/* Panel Power ON */
1075static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1076{
1077 u32 result;
1078
1079 if (!sci_open(dev))
1080 return -EIO;
1081
1082 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1083 sci_close(dev);
1084 if (result == TOS_FAILURE) {
1085 pr_err("ACPI call to get Panel Power ON failed\n");
1086 return -EIO;
1087 } else if (result == TOS_NOT_SUPPORTED) {
1088 pr_info("Panel Power on not supported\n");
1089 return -ENODEV;
1090 } else if (result == TOS_INPUT_DATA_ERROR) {
1091 return -EIO;
1092 }
1093
1094 return 0;
1095}
1096
1097static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1098{
1099 u32 result;
1100
1101 if (!sci_open(dev))
1102 return -EIO;
1103
1104 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1105 sci_close(dev);
1106 if (result == TOS_FAILURE) {
1107 pr_err("ACPI call to set Panel Power ON failed\n");
1108 return -EIO;
1109 } else if (result == TOS_NOT_SUPPORTED) {
1110 pr_info("Panel Power ON not supported\n");
1111 return -ENODEV;
1112 } else if (result == TOS_INPUT_DATA_ERROR) {
1113 return -EIO;
1114 }
1115
1116 return 0;
1117}
1118
Azael Avalos17fe4b32015-02-10 21:09:20 -07001119/* USB Three */
1120static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1121{
1122 u32 result;
1123
1124 if (!sci_open(dev))
1125 return -EIO;
1126
1127 result = sci_read(dev, SCI_USB_THREE, state);
1128 sci_close(dev);
1129 if (result == TOS_FAILURE) {
1130 pr_err("ACPI call to get USB 3 failed\n");
1131 return -EIO;
1132 } else if (result == TOS_NOT_SUPPORTED) {
1133 pr_info("USB 3 not supported\n");
1134 return -ENODEV;
1135 } else if (result == TOS_INPUT_DATA_ERROR) {
1136 return -EIO;
1137 }
1138
1139 return 0;
1140}
1141
1142static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1143{
1144 u32 result;
1145
1146 if (!sci_open(dev))
1147 return -EIO;
1148
1149 result = sci_write(dev, SCI_USB_THREE, state);
1150 sci_close(dev);
1151 if (result == TOS_FAILURE) {
1152 pr_err("ACPI call to set USB 3 failed\n");
1153 return -EIO;
1154 } else if (result == TOS_NOT_SUPPORTED) {
1155 pr_info("USB 3 not supported\n");
1156 return -ENODEV;
1157 } else if (result == TOS_INPUT_DATA_ERROR) {
1158 return -EIO;
1159 }
1160
1161 return 0;
1162}
1163
Azael Avalos56e6b352015-03-20 16:55:16 -06001164/* Hotkey Event type */
1165static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1166 u32 *type)
1167{
Azael Avalos3b876002015-05-06 09:35:09 -06001168 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1169 u32 out[TCI_WORDS];
1170 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001171
Azael Avalos3b876002015-05-06 09:35:09 -06001172 status = tci_raw(dev, in, out);
1173 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001174 pr_err("ACPI call to get System type failed\n");
1175 return -EIO;
Azael Avalos3b876002015-05-06 09:35:09 -06001176 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001177 pr_info("System type not supported\n");
1178 return -ENODEV;
1179 }
1180
Azael Avalos3b876002015-05-06 09:35:09 -06001181 *type = out[3];
Azael Avalos56e6b352015-03-20 16:55:16 -06001182
1183 return 0;
1184}
1185
Akio Idehara121b7b02012-04-06 01:46:43 +09001186static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1187{
1188 u32 hci_result;
1189 u32 status;
1190
Azael Avalos893f3f62014-09-29 20:40:09 -06001191 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001192 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001193 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001194}
1195
1196static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1197{
1198 u32 hci_result;
1199 u32 value = !enable;
1200
Azael Avalos893f3f62014-09-29 20:40:09 -06001201 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001202 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001203}
1204
Azael Avalosb5163992015-02-10 23:43:56 -07001205static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Seth Forshee62cce752012-04-19 11:23:50 -05001207static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 u32 hci_result;
1210 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001211 int brightness = 0;
1212
1213 if (dev->tr_backlight_supported) {
1214 bool enabled;
1215 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001216
Akio Idehara121b7b02012-04-06 01:46:43 +09001217 if (ret)
1218 return ret;
1219 if (enabled)
1220 return 0;
1221 brightness++;
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Azael Avalos893f3f62014-09-29 20:40:09 -06001224 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001225 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001226 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001227
1228 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001229}
1230
Seth Forshee62cce752012-04-19 11:23:50 -05001231static int get_lcd_brightness(struct backlight_device *bd)
1232{
1233 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001234
Seth Forshee62cce752012-04-19 11:23:50 -05001235 return __get_lcd_brightness(dev);
1236}
1237
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001238static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001239{
Seth Forshee135740d2011-09-20 16:55:49 -05001240 struct toshiba_acpi_dev *dev = m->private;
1241 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001242 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001243
Seth Forshee135740d2011-09-20 16:55:49 -05001244 if (!dev->backlight_dev)
1245 return -ENODEV;
1246
Akio Idehara121b7b02012-04-06 01:46:43 +09001247 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001248 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001249 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001250 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001251 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001255 pr_err("Error reading LCD brightness\n");
1256 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001257}
1258
1259static int lcd_proc_open(struct inode *inode, struct file *file)
1260{
Al Virod9dda782013-03-31 18:16:14 -04001261 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Seth Forshee62cce752012-04-19 11:23:50 -05001264static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001265{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001266 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001267
Akio Idehara121b7b02012-04-06 01:46:43 +09001268 if (dev->tr_backlight_supported) {
1269 bool enable = !value;
1270 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001271
Akio Idehara121b7b02012-04-06 01:46:43 +09001272 if (ret)
1273 return ret;
1274 if (value)
1275 value--;
1276 }
1277
Azael Avalosa39f46d2014-11-24 19:29:36 -07001278 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1279 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1280 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001281}
1282
1283static int set_lcd_status(struct backlight_device *bd)
1284{
Seth Forshee135740d2011-09-20 16:55:49 -05001285 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001286
Seth Forshee62cce752012-04-19 11:23:50 -05001287 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001288}
1289
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001290static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1291 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Al Virod9dda782013-03-31 18:16:14 -04001293 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001294 char cmd[42];
1295 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001297 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001298 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001300 len = min(count, sizeof(cmd) - 1);
1301 if (copy_from_user(cmd, buf, len))
1302 return -EFAULT;
1303 cmd[len] = '\0';
1304
1305 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001306 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001307 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001308 if (ret == 0)
1309 ret = count;
1310 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001311 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001312 }
Holger Machtc9263552006-10-20 14:30:29 -07001313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001316static const struct file_operations lcd_proc_fops = {
1317 .owner = THIS_MODULE,
1318 .open = lcd_proc_open,
1319 .read = seq_read,
1320 .llseek = seq_lseek,
1321 .release = single_release,
1322 .write = lcd_proc_write,
1323};
1324
Seth Forshee36d03f92011-09-20 16:55:53 -05001325static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1326{
1327 u32 hci_result;
1328
Azael Avalos893f3f62014-09-29 20:40:09 -06001329 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001330 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001331}
1332
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001333static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Seth Forshee135740d2011-09-20 16:55:49 -05001335 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001337 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Seth Forshee36d03f92011-09-20 16:55:53 -05001339 ret = get_video_status(dev, &value);
1340 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1342 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001343 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001344
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001345 seq_printf(m, "lcd_out: %d\n", is_lcd);
1346 seq_printf(m, "crt_out: %d\n", is_crt);
1347 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Seth Forshee36d03f92011-09-20 16:55:53 -05001350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001353static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Al Virod9dda782013-03-31 18:16:14 -04001355 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001356}
1357
1358static ssize_t video_proc_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *pos)
1360{
Al Virod9dda782013-03-31 18:16:14 -04001361 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001362 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int value;
1365 int remain = count;
1366 int lcd_out = -1;
1367 int crt_out = -1;
1368 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001369 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001371 cmd = kmalloc(count + 1, GFP_KERNEL);
1372 if (!cmd)
1373 return -ENOMEM;
1374 if (copy_from_user(cmd, buf, count)) {
1375 kfree(cmd);
1376 return -EFAULT;
1377 }
1378 cmd[count] = '\0';
1379
1380 buffer = cmd;
1381
Darren Harte0769fe2015-02-11 20:50:08 -08001382 /*
1383 * Scan expression. Multiple expressions may be delimited with ;
1384 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386 while (remain) {
1387 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1388 lcd_out = value & 1;
1389 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1390 crt_out = value & 1;
1391 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1392 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001393 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 do {
1395 ++buffer;
1396 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001397 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001400 kfree(cmd);
1401
Seth Forshee36d03f92011-09-20 16:55:53 -05001402 ret = get_video_status(dev, &video_out);
1403 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001404 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (lcd_out != -1)
1407 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1408 if (crt_out != -1)
1409 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1410 if (tv_out != -1)
1411 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001412 /*
1413 * To avoid unnecessary video disruption, only write the new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * video setting if something changed. */
1415 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001416 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001419 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001422static const struct file_operations video_proc_fops = {
1423 .owner = THIS_MODULE,
1424 .open = video_proc_open,
1425 .read = seq_read,
1426 .llseek = seq_lseek,
1427 .release = single_release,
1428 .write = video_proc_write,
1429};
1430
Seth Forshee36d03f92011-09-20 16:55:53 -05001431static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1432{
1433 u32 hci_result;
1434
Azael Avalos893f3f62014-09-29 20:40:09 -06001435 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001436 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001437}
1438
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001439static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Seth Forshee135740d2011-09-20 16:55:49 -05001441 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001442 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 u32 value;
1444
Seth Forshee36d03f92011-09-20 16:55:53 -05001445 ret = get_fan_status(dev, &value);
1446 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001447 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001448 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Seth Forshee36d03f92011-09-20 16:55:53 -05001451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001454static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Al Virod9dda782013-03-31 18:16:14 -04001456 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001457}
1458
1459static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1460 size_t count, loff_t *pos)
1461{
Al Virod9dda782013-03-31 18:16:14 -04001462 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001463 char cmd[42];
1464 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int value;
1466 u32 hci_result;
1467
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001468 len = min(count, sizeof(cmd) - 1);
1469 if (copy_from_user(cmd, buf, len))
1470 return -EFAULT;
1471 cmd[len] = '\0';
1472
1473 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001474 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001475 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001476 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001477 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001478 else
1479 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 } else {
1481 return -EINVAL;
1482 }
1483
1484 return count;
1485}
1486
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001487static const struct file_operations fan_proc_fops = {
1488 .owner = THIS_MODULE,
1489 .open = fan_proc_open,
1490 .read = seq_read,
1491 .llseek = seq_lseek,
1492 .release = single_release,
1493 .write = fan_proc_write,
1494};
1495
1496static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Seth Forshee135740d2011-09-20 16:55:49 -05001498 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 u32 hci_result;
1500 u32 value;
1501
Seth Forshee11948b92011-11-16 17:37:45 -06001502 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001503 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001504 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001505 dev->key_event_valid = 1;
1506 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001507 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001508 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001509 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001510 /*
1511 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001513 * become disabled.
1514 */
Azael Avalos893f3f62014-09-29 20:40:09 -06001515 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001516 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001518 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001519 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 }
1522
Seth Forshee135740d2011-09-20 16:55:49 -05001523 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1524 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001528static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Al Virod9dda782013-03-31 18:16:14 -04001530 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001531}
1532
1533static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1534 size_t count, loff_t *pos)
1535{
Al Virod9dda782013-03-31 18:16:14 -04001536 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001537 char cmd[42];
1538 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int value;
1540
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001541 len = min(count, sizeof(cmd) - 1);
1542 if (copy_from_user(cmd, buf, len))
1543 return -EFAULT;
1544 cmd[len] = '\0';
1545
Azael Avalosb5163992015-02-10 23:43:56 -07001546 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001547 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001548 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 return count;
1552}
1553
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001554static const struct file_operations keys_proc_fops = {
1555 .owner = THIS_MODULE,
1556 .open = keys_proc_open,
1557 .read = seq_read,
1558 .llseek = seq_lseek,
1559 .release = single_release,
1560 .write = keys_proc_write,
1561};
1562
1563static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001565 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1566 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001570static int version_proc_open(struct inode *inode, struct file *file)
1571{
Al Virod9dda782013-03-31 18:16:14 -04001572 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001573}
1574
1575static const struct file_operations version_proc_fops = {
1576 .owner = THIS_MODULE,
1577 .open = version_proc_open,
1578 .read = seq_read,
1579 .llseek = seq_lseek,
1580 .release = single_release,
1581};
1582
Darren Harte0769fe2015-02-11 20:50:08 -08001583/*
1584 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
1586
1587#define PROC_TOSHIBA "toshiba"
1588
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001589static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Seth Forshee36d03f92011-09-20 16:55:53 -05001591 if (dev->backlight_dev)
1592 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1593 &lcd_proc_fops, dev);
1594 if (dev->video_supported)
1595 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1596 &video_proc_fops, dev);
1597 if (dev->fan_supported)
1598 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1599 &fan_proc_fops, dev);
1600 if (dev->hotkey_dev)
1601 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1602 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001603 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1604 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Seth Forshee36d03f92011-09-20 16:55:53 -05001607static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Seth Forshee36d03f92011-09-20 16:55:53 -05001609 if (dev->backlight_dev)
1610 remove_proc_entry("lcd", toshiba_proc_dir);
1611 if (dev->video_supported)
1612 remove_proc_entry("video", toshiba_proc_dir);
1613 if (dev->fan_supported)
1614 remove_proc_entry("fan", toshiba_proc_dir);
1615 if (dev->hotkey_dev)
1616 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001617 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Lionel Debrouxacc24722010-11-16 14:14:02 +01001620static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001621 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001622 .get_brightness = get_lcd_brightness,
1623 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001624};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001625
Azael Avalos360f0f32014-03-25 20:38:31 -06001626/*
1627 * Sysfs files
1628 */
Azael Avalos9d309842015-02-10 23:43:59 -07001629static ssize_t version_show(struct device *dev,
1630 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001631{
1632 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1633}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001634static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001635
Azael Avalos9d309842015-02-10 23:43:59 -07001636static ssize_t fan_store(struct device *dev,
1637 struct device_attribute *attr,
1638 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001639{
1640 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1641 u32 result;
1642 int state;
1643 int ret;
1644
1645 ret = kstrtoint(buf, 0, &state);
1646 if (ret)
1647 return ret;
1648
1649 if (state != 0 && state != 1)
1650 return -EINVAL;
1651
1652 result = hci_write1(toshiba, HCI_FAN, state);
1653 if (result == TOS_FAILURE)
1654 return -EIO;
1655 else if (result == TOS_NOT_SUPPORTED)
1656 return -ENODEV;
1657
1658 return count;
1659}
1660
Azael Avalos9d309842015-02-10 23:43:59 -07001661static ssize_t fan_show(struct device *dev,
1662 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001663{
1664 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1665 u32 value;
1666 int ret;
1667
1668 ret = get_fan_status(toshiba, &value);
1669 if (ret)
1670 return ret;
1671
1672 return sprintf(buf, "%d\n", value);
1673}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001674static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001675
Azael Avalos9d309842015-02-10 23:43:59 -07001676static ssize_t kbd_backlight_mode_store(struct device *dev,
1677 struct device_attribute *attr,
1678 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001679{
1680 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001681 int mode;
1682 int time;
1683 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001684
Dan Carpenteraeaac092014-09-03 14:44:37 +03001685
1686 ret = kstrtoint(buf, 0, &mode);
1687 if (ret)
1688 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001689
1690 /* Check for supported modes depending on keyboard backlight type */
1691 if (toshiba->kbd_type == 1) {
1692 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1693 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1694 return -EINVAL;
1695 } else if (toshiba->kbd_type == 2) {
1696 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1697 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1698 mode != SCI_KBD_MODE_OFF)
1699 return -EINVAL;
1700 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001701
Darren Harte0769fe2015-02-11 20:50:08 -08001702 /*
1703 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001704 * Auto - KBD backlight turns off automatically in given time
1705 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001706 * ON - KBD backlight is always on
1707 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001708 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001709
1710 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001711 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001712 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001713 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001714
1715 /* OR the "base time" to the actual method format */
1716 if (toshiba->kbd_type == 1) {
1717 /* Type 1 requires the current mode */
1718 time |= toshiba->kbd_mode;
1719 } else if (toshiba->kbd_type == 2) {
1720 /* Type 2 requires the desired mode */
1721 time |= mode;
1722 }
1723
Dan Carpenteraeaac092014-09-03 14:44:37 +03001724 ret = toshiba_kbd_illum_status_set(toshiba, time);
1725 if (ret)
1726 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001727
Azael Avalos360f0f32014-03-25 20:38:31 -06001728 toshiba->kbd_mode = mode;
1729 }
1730
1731 return count;
1732}
1733
Azael Avalos9d309842015-02-10 23:43:59 -07001734static ssize_t kbd_backlight_mode_show(struct device *dev,
1735 struct device_attribute *attr,
1736 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001737{
1738 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1739 u32 time;
1740
1741 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1742 return -EIO;
1743
Azael Avalos93f8c162014-09-12 18:50:36 -06001744 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1745}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001746static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001747
Azael Avalos9d309842015-02-10 23:43:59 -07001748static ssize_t kbd_type_show(struct device *dev,
1749 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001750{
1751 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1752
1753 return sprintf(buf, "%d\n", toshiba->kbd_type);
1754}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001755static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001756
Azael Avalos9d309842015-02-10 23:43:59 -07001757static ssize_t available_kbd_modes_show(struct device *dev,
1758 struct device_attribute *attr,
1759 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001760{
1761 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1762
1763 if (toshiba->kbd_type == 1)
1764 return sprintf(buf, "%x %x\n",
1765 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1766
1767 return sprintf(buf, "%x %x %x\n",
1768 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001769}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001770static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001771
Azael Avalos9d309842015-02-10 23:43:59 -07001772static ssize_t kbd_backlight_timeout_store(struct device *dev,
1773 struct device_attribute *attr,
1774 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001775{
1776 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001777 int time;
1778 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001779
Azael Avaloseabde0f2014-10-04 12:02:21 -06001780 ret = kstrtoint(buf, 0, &time);
1781 if (ret)
1782 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001783
Azael Avaloseabde0f2014-10-04 12:02:21 -06001784 /* Check for supported values depending on kbd_type */
1785 if (toshiba->kbd_type == 1) {
1786 if (time < 0 || time > 60)
1787 return -EINVAL;
1788 } else if (toshiba->kbd_type == 2) {
1789 if (time < 1 || time > 60)
1790 return -EINVAL;
1791 }
1792
1793 /* Set the Keyboard Backlight Timeout */
1794
1795 /* Only make a change if the actual timeout has changed */
1796 if (toshiba->kbd_time != time) {
1797 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001798 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001799 /* OR the "base time" to the actual method format */
1800 if (toshiba->kbd_type == 1)
1801 time |= SCI_KBD_MODE_FNZ;
1802 else if (toshiba->kbd_type == 2)
1803 time |= SCI_KBD_MODE_AUTO;
1804
1805 ret = toshiba_kbd_illum_status_set(toshiba, time);
1806 if (ret)
1807 return ret;
1808
Azael Avalos360f0f32014-03-25 20:38:31 -06001809 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1810 }
1811
1812 return count;
1813}
1814
Azael Avalos9d309842015-02-10 23:43:59 -07001815static ssize_t kbd_backlight_timeout_show(struct device *dev,
1816 struct device_attribute *attr,
1817 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001818{
1819 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1820 u32 time;
1821
1822 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1823 return -EIO;
1824
1825 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1826}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001827static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001828
Azael Avalos9d309842015-02-10 23:43:59 -07001829static ssize_t touchpad_store(struct device *dev,
1830 struct device_attribute *attr,
1831 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001832{
1833 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1834 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001835 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001836
1837 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001838 ret = kstrtoint(buf, 0, &state);
1839 if (ret)
1840 return ret;
1841 if (state != 0 && state != 1)
1842 return -EINVAL;
1843
1844 ret = toshiba_touchpad_set(toshiba, state);
1845 if (ret)
1846 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001847
1848 return count;
1849}
1850
Azael Avalos9d309842015-02-10 23:43:59 -07001851static ssize_t touchpad_show(struct device *dev,
1852 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001853{
1854 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1855 u32 state;
1856 int ret;
1857
1858 ret = toshiba_touchpad_get(toshiba, &state);
1859 if (ret < 0)
1860 return ret;
1861
1862 return sprintf(buf, "%i\n", state);
1863}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001864static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001865
Azael Avalos9d309842015-02-10 23:43:59 -07001866static ssize_t position_show(struct device *dev,
1867 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001868{
1869 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1870 u32 xyval, zval, tmp;
1871 u16 x, y, z;
1872 int ret;
1873
1874 xyval = zval = 0;
1875 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1876 if (ret < 0)
1877 return ret;
1878
1879 x = xyval & HCI_ACCEL_MASK;
1880 tmp = xyval >> HCI_MISC_SHIFT;
1881 y = tmp & HCI_ACCEL_MASK;
1882 z = zval & HCI_ACCEL_MASK;
1883
1884 return sprintf(buf, "%d %d %d\n", x, y, z);
1885}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001886static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001887
Azael Avalos9d309842015-02-10 23:43:59 -07001888static ssize_t usb_sleep_charge_show(struct device *dev,
1889 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001890{
1891 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1892 u32 mode;
1893 int ret;
1894
1895 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1896 if (ret < 0)
1897 return ret;
1898
1899 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1900}
1901
Azael Avalos9d309842015-02-10 23:43:59 -07001902static ssize_t usb_sleep_charge_store(struct device *dev,
1903 struct device_attribute *attr,
1904 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001905{
1906 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1907 u32 mode;
1908 int state;
1909 int ret;
1910
1911 ret = kstrtoint(buf, 0, &state);
1912 if (ret)
1913 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001914 /*
1915 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001916 * 0 - Disabled
1917 * 1 - Alternate (Non USB conformant devices that require more power)
1918 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001919 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001920 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001921 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001922 return -EINVAL;
1923
1924 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001925 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001926 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001927 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001928 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001929 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001930 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001931 mode |= SCI_USB_CHARGE_AUTO;
1932 else if (state == 3)
1933 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001934
1935 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1936 if (ret)
1937 return ret;
1938
1939 return count;
1940}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001941static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001942
Azael Avalos182bcaa2015-01-18 18:30:23 -07001943static ssize_t sleep_functions_on_battery_show(struct device *dev,
1944 struct device_attribute *attr,
1945 char *buf)
1946{
1947 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1948 u32 state;
1949 int bat_lvl;
1950 int status;
1951 int ret;
1952 int tmp;
1953
1954 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1955 if (ret < 0)
1956 return ret;
1957
1958 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1959 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1960 status = (tmp == 0x4) ? 1 : 0;
1961 /* Determine the battery level set */
1962 bat_lvl = state >> HCI_MISC_SHIFT;
1963
1964 return sprintf(buf, "%d %d\n", status, bat_lvl);
1965}
1966
1967static ssize_t sleep_functions_on_battery_store(struct device *dev,
1968 struct device_attribute *attr,
1969 const char *buf, size_t count)
1970{
1971 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1972 u32 status;
1973 int value;
1974 int ret;
1975 int tmp;
1976
1977 ret = kstrtoint(buf, 0, &value);
1978 if (ret)
1979 return ret;
1980
Darren Harte0769fe2015-02-11 20:50:08 -08001981 /*
1982 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07001983 * 0 - Disabled
1984 * 1-100 - Enabled
1985 */
1986 if (value < 0 || value > 100)
1987 return -EINVAL;
1988
1989 if (value == 0) {
1990 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1991 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1992 } else {
1993 tmp = value << HCI_MISC_SHIFT;
1994 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1995 }
1996 ret = toshiba_sleep_functions_status_set(toshiba, status);
1997 if (ret < 0)
1998 return ret;
1999
2000 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2001
2002 return count;
2003}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002004static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002005
Azael Avalos9d309842015-02-10 23:43:59 -07002006static ssize_t usb_rapid_charge_show(struct device *dev,
2007 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002008{
2009 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2010 u32 state;
2011 int ret;
2012
2013 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2014 if (ret < 0)
2015 return ret;
2016
2017 return sprintf(buf, "%d\n", state);
2018}
2019
Azael Avalos9d309842015-02-10 23:43:59 -07002020static ssize_t usb_rapid_charge_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002023{
2024 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2025 int state;
2026 int ret;
2027
2028 ret = kstrtoint(buf, 0, &state);
2029 if (ret)
2030 return ret;
2031 if (state != 0 && state != 1)
2032 return -EINVAL;
2033
2034 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2035 if (ret)
2036 return ret;
2037
2038 return count;
2039}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002040static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002041
Azael Avalos9d309842015-02-10 23:43:59 -07002042static ssize_t usb_sleep_music_show(struct device *dev,
2043 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002044{
2045 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2046 u32 state;
2047 int ret;
2048
2049 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2050 if (ret < 0)
2051 return ret;
2052
2053 return sprintf(buf, "%d\n", state);
2054}
2055
Azael Avalos9d309842015-02-10 23:43:59 -07002056static ssize_t usb_sleep_music_store(struct device *dev,
2057 struct device_attribute *attr,
2058 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002059{
2060 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2061 int state;
2062 int ret;
2063
2064 ret = kstrtoint(buf, 0, &state);
2065 if (ret)
2066 return ret;
2067 if (state != 0 && state != 1)
2068 return -EINVAL;
2069
2070 ret = toshiba_usb_sleep_music_set(toshiba, state);
2071 if (ret)
2072 return ret;
2073
2074 return count;
2075}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002076static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002077
Azael Avalos9d309842015-02-10 23:43:59 -07002078static ssize_t kbd_function_keys_show(struct device *dev,
2079 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002080{
2081 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2082 int mode;
2083 int ret;
2084
2085 ret = toshiba_function_keys_get(toshiba, &mode);
2086 if (ret < 0)
2087 return ret;
2088
2089 return sprintf(buf, "%d\n", mode);
2090}
2091
Azael Avalos9d309842015-02-10 23:43:59 -07002092static ssize_t kbd_function_keys_store(struct device *dev,
2093 struct device_attribute *attr,
2094 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002095{
2096 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2097 int mode;
2098 int ret;
2099
2100 ret = kstrtoint(buf, 0, &mode);
2101 if (ret)
2102 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002103 /*
2104 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002105 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2106 * 1 - Special functions (Opposite of the above setting)
2107 */
2108 if (mode != 0 && mode != 1)
2109 return -EINVAL;
2110
2111 ret = toshiba_function_keys_set(toshiba, mode);
2112 if (ret)
2113 return ret;
2114
2115 pr_info("Reboot for changes to KBD Function Keys to take effect");
2116
2117 return count;
2118}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002119static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002120
Azael Avalos9d309842015-02-10 23:43:59 -07002121static ssize_t panel_power_on_show(struct device *dev,
2122 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002123{
2124 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2125 u32 state;
2126 int ret;
2127
2128 ret = toshiba_panel_power_on_get(toshiba, &state);
2129 if (ret < 0)
2130 return ret;
2131
2132 return sprintf(buf, "%d\n", state);
2133}
2134
Azael Avalos9d309842015-02-10 23:43:59 -07002135static ssize_t panel_power_on_store(struct device *dev,
2136 struct device_attribute *attr,
2137 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002138{
2139 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2140 int state;
2141 int ret;
2142
2143 ret = kstrtoint(buf, 0, &state);
2144 if (ret)
2145 return ret;
2146 if (state != 0 && state != 1)
2147 return -EINVAL;
2148
2149 ret = toshiba_panel_power_on_set(toshiba, state);
2150 if (ret)
2151 return ret;
2152
2153 pr_info("Reboot for changes to Panel Power ON to take effect");
2154
2155 return count;
2156}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002157static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002158
Azael Avalos9d309842015-02-10 23:43:59 -07002159static ssize_t usb_three_show(struct device *dev,
2160 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002161{
2162 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2163 u32 state;
2164 int ret;
2165
2166 ret = toshiba_usb_three_get(toshiba, &state);
2167 if (ret < 0)
2168 return ret;
2169
2170 return sprintf(buf, "%d\n", state);
2171}
2172
Azael Avalos9d309842015-02-10 23:43:59 -07002173static ssize_t usb_three_store(struct device *dev,
2174 struct device_attribute *attr,
2175 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002176{
2177 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2178 int state;
2179 int ret;
2180
2181 ret = kstrtoint(buf, 0, &state);
2182 if (ret)
2183 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002184 /*
2185 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002186 * 0 - Disabled (Acts like a USB 2 port, saving power)
2187 * 1 - Enabled
2188 */
2189 if (state != 0 && state != 1)
2190 return -EINVAL;
2191
2192 ret = toshiba_usb_three_set(toshiba, state);
2193 if (ret)
2194 return ret;
2195
2196 pr_info("Reboot for changes to USB 3 to take effect");
2197
2198 return count;
2199}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002200static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002201
2202static struct attribute *toshiba_attributes[] = {
2203 &dev_attr_version.attr,
2204 &dev_attr_fan.attr,
2205 &dev_attr_kbd_backlight_mode.attr,
2206 &dev_attr_kbd_type.attr,
2207 &dev_attr_available_kbd_modes.attr,
2208 &dev_attr_kbd_backlight_timeout.attr,
2209 &dev_attr_touchpad.attr,
2210 &dev_attr_position.attr,
2211 &dev_attr_usb_sleep_charge.attr,
2212 &dev_attr_sleep_functions_on_battery.attr,
2213 &dev_attr_usb_rapid_charge.attr,
2214 &dev_attr_usb_sleep_music.attr,
2215 &dev_attr_kbd_function_keys.attr,
2216 &dev_attr_panel_power_on.attr,
2217 &dev_attr_usb_three.attr,
2218 NULL,
2219};
2220
Azael Avalos360f0f32014-03-25 20:38:31 -06002221static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2222 struct attribute *attr, int idx)
2223{
2224 struct device *dev = container_of(kobj, struct device, kobj);
2225 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2226 bool exists = true;
2227
Azael Avalos94477d42015-02-10 21:09:17 -07002228 if (attr == &dev_attr_fan.attr)
2229 exists = (drv->fan_supported) ? true : false;
2230 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002231 exists = (drv->kbd_illum_supported) ? true : false;
2232 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2233 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002234 else if (attr == &dev_attr_touchpad.attr)
2235 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002236 else if (attr == &dev_attr_position.attr)
2237 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002238 else if (attr == &dev_attr_usb_sleep_charge.attr)
2239 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002240 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2241 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002242 else if (attr == &dev_attr_usb_rapid_charge.attr)
2243 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002244 else if (attr == &dev_attr_usb_sleep_music.attr)
2245 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002246 else if (attr == &dev_attr_kbd_function_keys.attr)
2247 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002248 else if (attr == &dev_attr_panel_power_on.attr)
2249 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002250 else if (attr == &dev_attr_usb_three.attr)
2251 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002252
2253 return exists ? attr->mode : 0;
2254}
2255
Azael Avalos9bd12132015-02-10 23:43:58 -07002256static struct attribute_group toshiba_attr_group = {
2257 .is_visible = toshiba_sysfs_is_visible,
2258 .attrs = toshiba_attributes,
2259};
2260
Azael Avalos1f28f292014-12-04 20:22:45 -07002261/*
2262 * Hotkeys
2263 */
2264static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2265{
2266 acpi_status status;
2267 u32 result;
2268
2269 status = acpi_evaluate_object(dev->acpi_dev->handle,
2270 "ENAB", NULL, NULL);
2271 if (ACPI_FAILURE(status))
2272 return -ENODEV;
2273
2274 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2275 if (result == TOS_FAILURE)
2276 return -EIO;
2277 else if (result == TOS_NOT_SUPPORTED)
2278 return -ENODEV;
2279
2280 return 0;
2281}
2282
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002283static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2284{
2285 u32 result;
2286
2287 /*
2288 * Re-activate the hotkeys, but this time, we are using the
2289 * "Special Functions" mode.
2290 */
2291 result = hci_write1(dev, HCI_HOTKEY_EVENT,
2292 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2293 if (result != TOS_SUCCESS)
2294 pr_err("Could not enable the Special Function mode\n");
2295}
2296
Seth Forshee29cd2932012-01-18 13:44:09 -06002297static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2298 struct serio *port)
2299{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002300 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002301 return false;
2302
2303 if (unlikely(data == 0xe0))
2304 return false;
2305
2306 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2307 schedule_work(&toshiba_acpi->hotkey_work);
2308 return true;
2309 }
2310
2311 return false;
2312}
2313
2314static void toshiba_acpi_hotkey_work(struct work_struct *work)
2315{
2316 acpi_handle ec_handle = ec_get_handle();
2317 acpi_status status;
2318
2319 if (!ec_handle)
2320 return;
2321
2322 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2323 if (ACPI_FAILURE(status))
2324 pr_err("ACPI NTFY method execution failed\n");
2325}
2326
2327/*
2328 * Returns hotkey scancode, or < 0 on failure.
2329 */
2330static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2331{
Zhang Rui74facaf2013-09-03 08:32:15 +08002332 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002333 acpi_status status;
2334
Zhang Rui74facaf2013-09-03 08:32:15 +08002335 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2336 NULL, &value);
2337 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002338 pr_err("ACPI INFO method execution failed\n");
2339 return -EIO;
2340 }
2341
Zhang Rui74facaf2013-09-03 08:32:15 +08002342 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002343}
2344
2345static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2346 int scancode)
2347{
2348 if (scancode == 0x100)
2349 return;
2350
Darren Harte0769fe2015-02-11 20:50:08 -08002351 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002352 if (scancode & 0x80)
2353 return;
2354
2355 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2356 pr_info("Unknown key %x\n", scancode);
2357}
2358
Azael Avalos71454d72014-12-04 20:22:46 -07002359static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2360{
2361 u32 hci_result, value;
2362 int retries = 3;
2363 int scancode;
2364
2365 if (dev->info_supported) {
2366 scancode = toshiba_acpi_query_hotkey(dev);
2367 if (scancode < 0)
2368 pr_err("Failed to query hotkey event\n");
2369 else if (scancode != 0)
2370 toshiba_acpi_report_hotkey(dev, scancode);
2371 } else if (dev->system_event_supported) {
2372 do {
2373 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2374 switch (hci_result) {
2375 case TOS_SUCCESS:
2376 toshiba_acpi_report_hotkey(dev, (int)value);
2377 break;
2378 case TOS_NOT_SUPPORTED:
2379 /*
2380 * This is a workaround for an unresolved
2381 * issue on some machines where system events
2382 * sporadically become disabled.
2383 */
2384 hci_result =
2385 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2386 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002387 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002388 default:
2389 retries--;
2390 break;
2391 }
2392 } while (retries && hci_result != TOS_FIFO_EMPTY);
2393 }
2394}
2395
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002396static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002397{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002398 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002399 acpi_handle ec_handle;
2400 u32 events_type;
2401 u32 hci_result;
2402 int error;
2403
2404 error = toshiba_acpi_enable_hotkeys(dev);
2405 if (error)
2406 return error;
2407
2408 error = toshiba_hotkey_event_type_get(dev, &events_type);
2409 if (error) {
2410 pr_err("Unable to query Hotkey Event Type\n");
2411 return error;
2412 }
2413 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002414
Seth Forshee135740d2011-09-20 16:55:49 -05002415 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002416 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002417 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002418
2419 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002420 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002421 dev->hotkey_dev->id.bustype = BUS_HOST;
2422
Azael Avalosa2b34712015-03-20 16:55:17 -06002423 if (events_type == HCI_SYSTEM_TYPE1 ||
2424 !dev->kbd_function_keys_supported)
2425 keymap = toshiba_acpi_keymap;
2426 else if (events_type == HCI_SYSTEM_TYPE2 ||
2427 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002428 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002429 else
2430 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002431 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002432 if (error)
2433 goto err_free_dev;
2434
Seth Forshee29cd2932012-01-18 13:44:09 -06002435 /*
2436 * For some machines the SCI responsible for providing hotkey
2437 * notification doesn't fire. We can trigger the notification
2438 * whenever the Fn key is pressed using the NTFY method, if
2439 * supported, so if it's present set up an i8042 key filter
2440 * for this purpose.
2441 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002442 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002443 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002444 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2445
2446 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2447 if (error) {
2448 pr_err("Error installing key filter\n");
2449 goto err_free_keymap;
2450 }
2451
2452 dev->ntfy_supported = 1;
2453 }
2454
2455 /*
2456 * Determine hotkey query interface. Prefer using the INFO
2457 * method when it is available.
2458 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002459 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002460 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002461 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002462 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002463 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002464 dev->system_event_supported = 1;
2465 }
2466
2467 if (!dev->info_supported && !dev->system_event_supported) {
2468 pr_warn("No hotkey query interface found\n");
2469 goto err_remove_filter;
2470 }
2471
Seth Forshee135740d2011-09-20 16:55:49 -05002472 error = input_register_device(dev->hotkey_dev);
2473 if (error) {
2474 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002475 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002476 }
2477
2478 return 0;
2479
Seth Forshee29cd2932012-01-18 13:44:09 -06002480 err_remove_filter:
2481 if (dev->ntfy_supported)
2482 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002483 err_free_keymap:
2484 sparse_keymap_free(dev->hotkey_dev);
2485 err_free_dev:
2486 input_free_device(dev->hotkey_dev);
2487 dev->hotkey_dev = NULL;
2488 return error;
2489}
2490
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002491static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002492{
2493 struct backlight_properties props;
2494 int brightness;
2495 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002496 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002497
2498 /*
2499 * Some machines don't support the backlight methods at all, and
2500 * others support it read-only. Either of these is pretty useless,
2501 * so only register the backlight device if the backlight method
2502 * supports both reads and writes.
2503 */
2504 brightness = __get_lcd_brightness(dev);
2505 if (brightness < 0)
2506 return 0;
2507 ret = set_lcd_brightness(dev, brightness);
2508 if (ret) {
2509 pr_debug("Backlight method is read-only, disabling backlight support\n");
2510 return 0;
2511 }
2512
Akio Idehara121b7b02012-04-06 01:46:43 +09002513 /* Determine whether or not BIOS supports transflective backlight */
2514 ret = get_tr_backlight_status(dev, &enabled);
2515 dev->tr_backlight_supported = !ret;
2516
Hans de Goede358d6a22015-04-21 12:01:32 +02002517 /*
2518 * Tell acpi-video-detect code to prefer vendor backlight on all
2519 * systems with transflective backlight and on dmi matched systems.
2520 */
2521 if (dev->tr_backlight_supported ||
2522 dmi_check_system(toshiba_vendor_backlight_dmi))
2523 acpi_video_dmi_promote_vendor();
2524
2525 if (acpi_video_backlight_support())
2526 return 0;
2527
2528 /* acpi-video may have loaded before we called dmi_promote_vendor() */
2529 acpi_video_unregister_backlight();
2530
Matthew Garrett53039f22012-06-01 11:02:36 -04002531 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002532 props.type = BACKLIGHT_PLATFORM;
2533 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002534
Darren Harte0769fe2015-02-11 20:50:08 -08002535 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002536 if (dev->tr_backlight_supported)
2537 props.max_brightness++;
2538
Seth Forshee62cce752012-04-19 11:23:50 -05002539 dev->backlight_dev = backlight_device_register("toshiba",
2540 &dev->acpi_dev->dev,
2541 dev,
2542 &toshiba_backlight_data,
2543 &props);
2544 if (IS_ERR(dev->backlight_dev)) {
2545 ret = PTR_ERR(dev->backlight_dev);
2546 pr_err("Could not register toshiba backlight device\n");
2547 dev->backlight_dev = NULL;
2548 return ret;
2549 }
2550
2551 dev->backlight_dev->props.brightness = brightness;
2552 return 0;
2553}
2554
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002555static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002556{
2557 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2558
Seth Forshee36d03f92011-09-20 16:55:53 -05002559 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002560
Azael Avalos360f0f32014-03-25 20:38:31 -06002561 if (dev->sysfs_created)
2562 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2563 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002564
Seth Forshee29cd2932012-01-18 13:44:09 -06002565 if (dev->ntfy_supported) {
2566 i8042_remove_filter(toshiba_acpi_i8042_filter);
2567 cancel_work_sync(&dev->hotkey_work);
2568 }
2569
Seth Forshee135740d2011-09-20 16:55:49 -05002570 if (dev->hotkey_dev) {
2571 input_unregister_device(dev->hotkey_dev);
2572 sparse_keymap_free(dev->hotkey_dev);
2573 }
2574
Markus Elfring00981812014-11-24 20:30:29 +01002575 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002576
Seth Forshee36d03f92011-09-20 16:55:53 -05002577 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002578 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002579
Azael Avalos360f0f32014-03-25 20:38:31 -06002580 if (dev->kbd_led_registered)
2581 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002582
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002583 if (dev->eco_supported)
2584 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002585
Seth Forshee29cd2932012-01-18 13:44:09 -06002586 if (toshiba_acpi)
2587 toshiba_acpi = NULL;
2588
Seth Forshee135740d2011-09-20 16:55:49 -05002589 kfree(dev);
2590
2591 return 0;
2592}
2593
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002594static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002595{
Zhang Ruie2e19602013-09-03 08:32:06 +08002596 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002597 return "GHCI";
2598
Zhang Ruie2e19602013-09-03 08:32:06 +08002599 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002600 return "SPFC";
2601
2602 return NULL;
2603}
2604
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002605static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002606{
2607 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002608 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002609 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002610 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002611 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002612
Seth Forshee29cd2932012-01-18 13:44:09 -06002613 if (toshiba_acpi)
2614 return -EBUSY;
2615
Seth Forshee135740d2011-09-20 16:55:49 -05002616 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2617 TOSHIBA_ACPI_VERSION);
2618
Seth Forsheea540d6b2011-09-20 16:55:52 -05002619 hci_method = find_hci_method(acpi_dev->handle);
2620 if (!hci_method) {
2621 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002622 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002623 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002624
Seth Forshee135740d2011-09-20 16:55:49 -05002625 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2626 if (!dev)
2627 return -ENOMEM;
2628 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002629 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002630 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002631 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002632
Azael Avalosa2b34712015-03-20 16:55:17 -06002633 /* Query the BIOS for supported features */
2634
2635 /*
2636 * The "Special Functions" are always supported by the laptops
2637 * with the new keyboard layout, query for its presence to help
2638 * determine the keymap layout to use.
2639 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002640 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002641 dev->kbd_function_keys_supported = !ret;
2642
Seth Forshee6e02cc72011-09-20 16:55:51 -05002643 if (toshiba_acpi_setup_keyboard(dev))
2644 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002645
Seth Forshee62cce752012-04-19 11:23:50 -05002646 ret = toshiba_acpi_setup_backlight(dev);
2647 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002648 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002649
Seth Forshee135740d2011-09-20 16:55:49 -05002650 if (toshiba_illumination_available(dev)) {
2651 dev->led_dev.name = "toshiba::illumination";
2652 dev->led_dev.max_brightness = 1;
2653 dev->led_dev.brightness_set = toshiba_illumination_set;
2654 dev->led_dev.brightness_get = toshiba_illumination_get;
2655 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002656 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002657 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002658
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002659 if (toshiba_eco_mode_available(dev)) {
2660 dev->eco_led.name = "toshiba::eco_mode";
2661 dev->eco_led.max_brightness = 1;
2662 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2663 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2664 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2665 dev->eco_supported = 1;
2666 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002667
Azael Avalos93f8c162014-09-12 18:50:36 -06002668 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002669 /*
2670 * Only register the LED if KBD illumination is supported
2671 * and the keyboard backlight operation mode is set to FN-Z
2672 */
2673 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2674 dev->kbd_led.name = "toshiba::kbd_backlight";
2675 dev->kbd_led.max_brightness = 1;
2676 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2677 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2678 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2679 dev->kbd_led_registered = 1;
2680 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002681
Azael Avalos9d8658a2014-03-25 20:38:32 -06002682 ret = toshiba_touchpad_get(dev, &dummy);
2683 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002684
Azael Avalos5a2813e2014-03-25 20:38:34 -06002685 ret = toshiba_accelerometer_supported(dev);
2686 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002687
Azael Avalosc8c91842015-04-02 19:26:20 -06002688 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002689
Azael Avalosbb3fe012015-01-18 18:30:24 -07002690 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2691 dev->usb_rapid_charge_supported = !ret;
2692
Azael Avalos172ce0a2015-01-18 18:30:25 -07002693 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2694 dev->usb_sleep_music_supported = !ret;
2695
Azael Avalos35d53ce2015-02-10 21:09:19 -07002696 ret = toshiba_panel_power_on_get(dev, &dummy);
2697 dev->panel_power_on_supported = !ret;
2698
Azael Avalos17fe4b32015-02-10 21:09:20 -07002699 ret = toshiba_usb_three_get(dev, &dummy);
2700 dev->usb_three_supported = !ret;
2701
Seth Forshee36d03f92011-09-20 16:55:53 -05002702 ret = get_video_status(dev, &dummy);
2703 dev->video_supported = !ret;
2704
2705 ret = get_fan_status(dev, &dummy);
2706 dev->fan_supported = !ret;
2707
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002708 /*
2709 * Enable the "Special Functions" mode only if they are
2710 * supported and if they are activated.
2711 */
2712 if (dev->kbd_function_keys_supported && special_functions)
2713 toshiba_acpi_enable_special_functions(dev);
2714
Azael Avalos360f0f32014-03-25 20:38:31 -06002715 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2716 &toshiba_attr_group);
2717 if (ret) {
2718 dev->sysfs_created = 0;
2719 goto error;
2720 }
2721 dev->sysfs_created = !ret;
2722
Seth Forshee36d03f92011-09-20 16:55:53 -05002723 create_toshiba_proc_entries(dev);
2724
Seth Forshee29cd2932012-01-18 13:44:09 -06002725 toshiba_acpi = dev;
2726
Seth Forshee135740d2011-09-20 16:55:49 -05002727 return 0;
2728
2729error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002730 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002731 return ret;
2732}
2733
2734static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2735{
2736 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002737 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002738
Azael Avalos71454d72014-12-04 20:22:46 -07002739 switch (event) {
2740 case 0x80: /* Hotkeys and some system events */
2741 toshiba_acpi_process_hotkeys(dev);
2742 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002743 case 0x81: /* Dock events */
2744 case 0x82:
2745 case 0x83:
2746 pr_info("Dock event received %x\n", event);
2747 break;
2748 case 0x88: /* Thermal events */
2749 pr_info("Thermal event received\n");
2750 break;
2751 case 0x8f: /* LID closed */
2752 case 0x90: /* LID is closed and Dock has been ejected */
2753 break;
2754 case 0x8c: /* SATA power events */
2755 case 0x8b:
2756 pr_info("SATA power event received %x\n", event);
2757 break;
Azael Avalos80546902014-12-04 20:22:47 -07002758 case 0x92: /* Keyboard backlight mode changed */
2759 /* Update sysfs entries */
2760 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2761 &toshiba_attr_group);
2762 if (ret)
2763 pr_err("Unable to update sysfs entries\n");
2764 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002765 case 0x85: /* Unknown */
2766 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002767 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002768 case 0x94: /* Unknown */
2769 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002770 default:
2771 pr_info("Unknown event received %x\n", event);
2772 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002773 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002774
2775 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2776 dev_name(&acpi_dev->dev),
2777 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002778}
2779
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002780#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002781static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002782{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002783 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002784 u32 result;
2785
2786 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002787 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002788
2789 return 0;
2790}
2791
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002792static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002793{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002794 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002795 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002796
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002797 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002798 error = toshiba_acpi_enable_hotkeys(dev);
2799 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002800 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002801 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002802
2803 return 0;
2804}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002805#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002806
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002807static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2808 toshiba_acpi_suspend, toshiba_acpi_resume);
2809
Seth Forshee135740d2011-09-20 16:55:49 -05002810static struct acpi_driver toshiba_acpi_driver = {
2811 .name = "Toshiba ACPI driver",
2812 .owner = THIS_MODULE,
2813 .ids = toshiba_device_ids,
2814 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2815 .ops = {
2816 .add = toshiba_acpi_add,
2817 .remove = toshiba_acpi_remove,
2818 .notify = toshiba_acpi_notify,
2819 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002820 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002821};
Holger Machtc9263552006-10-20 14:30:29 -07002822
Len Brown4be44fc2005-08-05 00:44:28 -04002823static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Seth Forshee135740d2011-09-20 16:55:49 -05002825 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Seth Forsheef11f9992012-01-18 13:44:11 -06002827 /*
2828 * Machines with this WMI guid aren't supported due to bugs in
2829 * their AML. This check relies on wmi initializing before
2830 * toshiba_acpi to guarantee guids have been identified.
2831 */
2832 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2833 return -ENODEV;
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2836 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002837 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002838 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840
Seth Forshee135740d2011-09-20 16:55:49 -05002841 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2842 if (ret) {
2843 pr_err("Failed to register ACPI driver: %d\n", ret);
2844 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002845 }
2846
Seth Forshee135740d2011-09-20 16:55:49 -05002847 return ret;
2848}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002849
Seth Forshee135740d2011-09-20 16:55:49 -05002850static void __exit toshiba_acpi_exit(void)
2851{
2852 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2853 if (toshiba_proc_dir)
2854 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857module_init(toshiba_acpi_init);
2858module_exit(toshiba_acpi_exit);