blob: 26671923b994f2cddd0464175f49ea7d6ea2d0b8 [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
Azael Avalos3f75bbe2015-05-06 09:35:11 -060084/* Operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#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
Azael Avalos3f75bbe2015-05-06 09:35:11 -060092/* 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
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600107/* Registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#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
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600129/* 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/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600329 * Common hci tasks
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 Avalosd37782b2015-05-06 09:35:10 -0600335static u32 hci_write(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 Avalosd37782b2015-05-06 09:35:10 -0600344static u32 hci_read(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 Avalos8baec452015-05-06 09:35:12 -0600369 if (ACPI_FAILURE(status)) {
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 Avalos8baec452015-05-06 09:35:12 -0600407 if (ACPI_FAILURE(status)) {
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 Avalos8baec452015-05-06 09:35:12 -0600455 if (ACPI_FAILURE(status)) {
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
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600496 /* 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 Avalosd37782b2015-05-06 09:35:10 -0600599 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600600 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 Avalosd37782b2015-05-06 09:35:10 -0600620 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600621 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 Avalos8baec452015-05-06 09:35:12 -0600677 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700678 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);
Azael Avalos8baec452015-05-06 09:35:12 -0600799 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600800 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);
Azael Avalos8baec452015-05-06 09:35:12 -0600813 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600814 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);
Azael Avalos8baec452015-05-06 09:35:12 -0600893 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700894 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);
Azael Avalos8baec452015-05-06 09:35:12 -0600922 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700923 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);
Azael Avalos8baec452015-05-06 09:35:12 -0600948 if (ACPI_FAILURE(status)) {
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);
Azael Avalos8baec452015-05-06 09:35:12 -0600976 if (ACPI_FAILURE(status)) {
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
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001186/* Transflective Backlight */
Akio Idehara121b7b02012-04-06 01:46:43 +09001187static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1188{
1189 u32 hci_result;
1190 u32 status;
1191
Azael Avalosd37782b2015-05-06 09:35:10 -06001192 hci_result = hci_read(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001193 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001194 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001195}
1196
1197static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1198{
1199 u32 hci_result;
1200 u32 value = !enable;
1201
Azael Avalosd37782b2015-05-06 09:35:10 -06001202 hci_result = hci_write(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001203 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001204}
1205
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001206static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001208/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001209static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 u32 hci_result;
1212 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001213 int brightness = 0;
1214
1215 if (dev->tr_backlight_supported) {
1216 bool enabled;
1217 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001218
Akio Idehara121b7b02012-04-06 01:46:43 +09001219 if (ret)
1220 return ret;
1221 if (enabled)
1222 return 0;
1223 brightness++;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Azael Avalosd37782b2015-05-06 09:35:10 -06001226 hci_result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001227 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001228 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001229
1230 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001231}
1232
Seth Forshee62cce752012-04-19 11:23:50 -05001233static int get_lcd_brightness(struct backlight_device *bd)
1234{
1235 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001236
Seth Forshee62cce752012-04-19 11:23:50 -05001237 return __get_lcd_brightness(dev);
1238}
1239
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001240static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001241{
Seth Forshee135740d2011-09-20 16:55:49 -05001242 struct toshiba_acpi_dev *dev = m->private;
1243 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001244 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001245
Seth Forshee135740d2011-09-20 16:55:49 -05001246 if (!dev->backlight_dev)
1247 return -ENODEV;
1248
Akio Idehara121b7b02012-04-06 01:46:43 +09001249 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001250 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001251 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001252 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001253 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001257 pr_err("Error reading LCD brightness\n");
1258 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001259}
1260
1261static int lcd_proc_open(struct inode *inode, struct file *file)
1262{
Al Virod9dda782013-03-31 18:16:14 -04001263 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Seth Forshee62cce752012-04-19 11:23:50 -05001266static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001267{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001268 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001269
Akio Idehara121b7b02012-04-06 01:46:43 +09001270 if (dev->tr_backlight_supported) {
1271 bool enable = !value;
1272 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001273
Akio Idehara121b7b02012-04-06 01:46:43 +09001274 if (ret)
1275 return ret;
1276 if (value)
1277 value--;
1278 }
1279
Azael Avalosa39f46d2014-11-24 19:29:36 -07001280 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalosd37782b2015-05-06 09:35:10 -06001281 hci_result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
Azael Avalosa39f46d2014-11-24 19:29:36 -07001282 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001283}
1284
1285static int set_lcd_status(struct backlight_device *bd)
1286{
Seth Forshee135740d2011-09-20 16:55:49 -05001287 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001288
Seth Forshee62cce752012-04-19 11:23:50 -05001289 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001290}
1291
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001292static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1293 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Al Virod9dda782013-03-31 18:16:14 -04001295 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001296 char cmd[42];
1297 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001299 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001300 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001302 len = min(count, sizeof(cmd) - 1);
1303 if (copy_from_user(cmd, buf, len))
1304 return -EFAULT;
1305 cmd[len] = '\0';
1306
1307 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001308 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001309 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001310 if (ret == 0)
1311 ret = count;
1312 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001313 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001314 }
Holger Machtc9263552006-10-20 14:30:29 -07001315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001318static const struct file_operations lcd_proc_fops = {
1319 .owner = THIS_MODULE,
1320 .open = lcd_proc_open,
1321 .read = seq_read,
1322 .llseek = seq_lseek,
1323 .release = single_release,
1324 .write = lcd_proc_write,
1325};
1326
Seth Forshee36d03f92011-09-20 16:55:53 -05001327static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1328{
1329 u32 hci_result;
1330
Azael Avalosd37782b2015-05-06 09:35:10 -06001331 hci_result = hci_read(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001332 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001333}
1334
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001335static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Seth Forshee135740d2011-09-20 16:55:49 -05001337 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001339 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Seth Forshee36d03f92011-09-20 16:55:53 -05001341 ret = get_video_status(dev, &value);
1342 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1344 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001345 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001346
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001347 seq_printf(m, "lcd_out: %d\n", is_lcd);
1348 seq_printf(m, "crt_out: %d\n", is_crt);
1349 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Seth Forshee36d03f92011-09-20 16:55:53 -05001352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001355static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Al Virod9dda782013-03-31 18:16:14 -04001357 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001358}
1359
1360static ssize_t video_proc_write(struct file *file, const char __user *buf,
1361 size_t count, loff_t *pos)
1362{
Al Virod9dda782013-03-31 18:16:14 -04001363 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001364 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001365 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int value;
1367 int remain = count;
1368 int lcd_out = -1;
1369 int crt_out = -1;
1370 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001371 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001373 cmd = kmalloc(count + 1, GFP_KERNEL);
1374 if (!cmd)
1375 return -ENOMEM;
1376 if (copy_from_user(cmd, buf, count)) {
1377 kfree(cmd);
1378 return -EFAULT;
1379 }
1380 cmd[count] = '\0';
1381
1382 buffer = cmd;
1383
Darren Harte0769fe2015-02-11 20:50:08 -08001384 /*
1385 * Scan expression. Multiple expressions may be delimited with ;
1386 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
1388 while (remain) {
1389 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1390 lcd_out = value & 1;
1391 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1392 crt_out = value & 1;
1393 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1394 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001395 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 do {
1397 ++buffer;
1398 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001399 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001402 kfree(cmd);
1403
Seth Forshee36d03f92011-09-20 16:55:53 -05001404 ret = get_video_status(dev, &video_out);
1405 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001406 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (lcd_out != -1)
1409 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1410 if (crt_out != -1)
1411 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1412 if (tv_out != -1)
1413 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001414 /*
1415 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001416 * video setting if something changed.
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001419 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001422 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001425static const struct file_operations video_proc_fops = {
1426 .owner = THIS_MODULE,
1427 .open = video_proc_open,
1428 .read = seq_read,
1429 .llseek = seq_lseek,
1430 .release = single_release,
1431 .write = video_proc_write,
1432};
1433
Seth Forshee36d03f92011-09-20 16:55:53 -05001434static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1435{
1436 u32 hci_result;
1437
Azael Avalosd37782b2015-05-06 09:35:10 -06001438 hci_result = hci_read(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001439 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001440}
1441
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001442static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Seth Forshee135740d2011-09-20 16:55:49 -05001444 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001445 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 u32 value;
1447
Seth Forshee36d03f92011-09-20 16:55:53 -05001448 ret = get_fan_status(dev, &value);
1449 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001450 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001451 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Seth Forshee36d03f92011-09-20 16:55:53 -05001454 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001457static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Al Virod9dda782013-03-31 18:16:14 -04001459 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001460}
1461
1462static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1463 size_t count, loff_t *pos)
1464{
Al Virod9dda782013-03-31 18:16:14 -04001465 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001466 char cmd[42];
1467 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int value;
1469 u32 hci_result;
1470
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001471 len = min(count, sizeof(cmd) - 1);
1472 if (copy_from_user(cmd, buf, len))
1473 return -EFAULT;
1474 cmd[len] = '\0';
1475
1476 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001477 value >= 0 && value <= 1) {
Azael Avalosd37782b2015-05-06 09:35:10 -06001478 hci_result = hci_write(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001479 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001480 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001481 else
1482 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
1484 return -EINVAL;
1485 }
1486
1487 return count;
1488}
1489
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001490static const struct file_operations fan_proc_fops = {
1491 .owner = THIS_MODULE,
1492 .open = fan_proc_open,
1493 .read = seq_read,
1494 .llseek = seq_lseek,
1495 .release = single_release,
1496 .write = fan_proc_write,
1497};
1498
1499static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Seth Forshee135740d2011-09-20 16:55:49 -05001501 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 u32 hci_result;
1503 u32 value;
1504
Seth Forshee11948b92011-11-16 17:37:45 -06001505 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalosd37782b2015-05-06 09:35:10 -06001506 hci_result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001507 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001508 dev->key_event_valid = 1;
1509 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001510 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001511 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001512 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001513 /*
1514 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001516 * become disabled.
1517 */
Azael Avalosd37782b2015-05-06 09:35:10 -06001518 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001519 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001521 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001522 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 }
1525
Seth Forshee135740d2011-09-20 16:55:49 -05001526 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1527 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001531static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Al Virod9dda782013-03-31 18:16:14 -04001533 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001534}
1535
1536static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1537 size_t count, loff_t *pos)
1538{
Al Virod9dda782013-03-31 18:16:14 -04001539 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001540 char cmd[42];
1541 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int value;
1543
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001544 len = min(count, sizeof(cmd) - 1);
1545 if (copy_from_user(cmd, buf, len))
1546 return -EFAULT;
1547 cmd[len] = '\0';
1548
Azael Avalosb5163992015-02-10 23:43:56 -07001549 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001550 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001551 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 return count;
1555}
1556
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001557static const struct file_operations keys_proc_fops = {
1558 .owner = THIS_MODULE,
1559 .open = keys_proc_open,
1560 .read = seq_read,
1561 .llseek = seq_lseek,
1562 .release = single_release,
1563 .write = keys_proc_write,
1564};
1565
1566static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001568 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1569 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001573static int version_proc_open(struct inode *inode, struct file *file)
1574{
Al Virod9dda782013-03-31 18:16:14 -04001575 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001576}
1577
1578static const struct file_operations version_proc_fops = {
1579 .owner = THIS_MODULE,
1580 .open = version_proc_open,
1581 .read = seq_read,
1582 .llseek = seq_lseek,
1583 .release = single_release,
1584};
1585
Darren Harte0769fe2015-02-11 20:50:08 -08001586/*
1587 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 */
1589
1590#define PROC_TOSHIBA "toshiba"
1591
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001592static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Seth Forshee36d03f92011-09-20 16:55:53 -05001594 if (dev->backlight_dev)
1595 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1596 &lcd_proc_fops, dev);
1597 if (dev->video_supported)
1598 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1599 &video_proc_fops, dev);
1600 if (dev->fan_supported)
1601 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1602 &fan_proc_fops, dev);
1603 if (dev->hotkey_dev)
1604 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1605 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001606 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1607 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Seth Forshee36d03f92011-09-20 16:55:53 -05001610static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Seth Forshee36d03f92011-09-20 16:55:53 -05001612 if (dev->backlight_dev)
1613 remove_proc_entry("lcd", toshiba_proc_dir);
1614 if (dev->video_supported)
1615 remove_proc_entry("video", toshiba_proc_dir);
1616 if (dev->fan_supported)
1617 remove_proc_entry("fan", toshiba_proc_dir);
1618 if (dev->hotkey_dev)
1619 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001620 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Lionel Debrouxacc24722010-11-16 14:14:02 +01001623static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001624 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001625 .get_brightness = get_lcd_brightness,
1626 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001627};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001628
Azael Avalos360f0f32014-03-25 20:38:31 -06001629/*
1630 * Sysfs files
1631 */
Azael Avalos9d309842015-02-10 23:43:59 -07001632static ssize_t version_show(struct device *dev,
1633 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001634{
1635 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1636}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001637static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001638
Azael Avalos9d309842015-02-10 23:43:59 -07001639static ssize_t fan_store(struct device *dev,
1640 struct device_attribute *attr,
1641 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001642{
1643 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1644 u32 result;
1645 int state;
1646 int ret;
1647
1648 ret = kstrtoint(buf, 0, &state);
1649 if (ret)
1650 return ret;
1651
1652 if (state != 0 && state != 1)
1653 return -EINVAL;
1654
Azael Avalosd37782b2015-05-06 09:35:10 -06001655 result = hci_write(toshiba, HCI_FAN, state);
Azael Avalos94477d42015-02-10 21:09:17 -07001656 if (result == TOS_FAILURE)
1657 return -EIO;
1658 else if (result == TOS_NOT_SUPPORTED)
1659 return -ENODEV;
1660
1661 return count;
1662}
1663
Azael Avalos9d309842015-02-10 23:43:59 -07001664static ssize_t fan_show(struct device *dev,
1665 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001666{
1667 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1668 u32 value;
1669 int ret;
1670
1671 ret = get_fan_status(toshiba, &value);
1672 if (ret)
1673 return ret;
1674
1675 return sprintf(buf, "%d\n", value);
1676}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001677static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001678
Azael Avalos9d309842015-02-10 23:43:59 -07001679static ssize_t kbd_backlight_mode_store(struct device *dev,
1680 struct device_attribute *attr,
1681 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001682{
1683 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001684 int mode;
1685 int time;
1686 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001687
Dan Carpenteraeaac092014-09-03 14:44:37 +03001688
1689 ret = kstrtoint(buf, 0, &mode);
1690 if (ret)
1691 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001692
1693 /* Check for supported modes depending on keyboard backlight type */
1694 if (toshiba->kbd_type == 1) {
1695 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1696 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1697 return -EINVAL;
1698 } else if (toshiba->kbd_type == 2) {
1699 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1700 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1701 mode != SCI_KBD_MODE_OFF)
1702 return -EINVAL;
1703 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001704
Darren Harte0769fe2015-02-11 20:50:08 -08001705 /*
1706 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001707 * Auto - KBD backlight turns off automatically in given time
1708 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001709 * ON - KBD backlight is always on
1710 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001711 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001712
1713 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001714 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001715 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001716 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001717
1718 /* OR the "base time" to the actual method format */
1719 if (toshiba->kbd_type == 1) {
1720 /* Type 1 requires the current mode */
1721 time |= toshiba->kbd_mode;
1722 } else if (toshiba->kbd_type == 2) {
1723 /* Type 2 requires the desired mode */
1724 time |= mode;
1725 }
1726
Dan Carpenteraeaac092014-09-03 14:44:37 +03001727 ret = toshiba_kbd_illum_status_set(toshiba, time);
1728 if (ret)
1729 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001730
Azael Avalos360f0f32014-03-25 20:38:31 -06001731 toshiba->kbd_mode = mode;
1732 }
1733
1734 return count;
1735}
1736
Azael Avalos9d309842015-02-10 23:43:59 -07001737static ssize_t kbd_backlight_mode_show(struct device *dev,
1738 struct device_attribute *attr,
1739 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001740{
1741 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1742 u32 time;
1743
1744 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1745 return -EIO;
1746
Azael Avalos93f8c162014-09-12 18:50:36 -06001747 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1748}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001749static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001750
Azael Avalos9d309842015-02-10 23:43:59 -07001751static ssize_t kbd_type_show(struct device *dev,
1752 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001753{
1754 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1755
1756 return sprintf(buf, "%d\n", toshiba->kbd_type);
1757}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001758static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001759
Azael Avalos9d309842015-02-10 23:43:59 -07001760static ssize_t available_kbd_modes_show(struct device *dev,
1761 struct device_attribute *attr,
1762 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001763{
1764 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1765
1766 if (toshiba->kbd_type == 1)
1767 return sprintf(buf, "%x %x\n",
1768 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1769
1770 return sprintf(buf, "%x %x %x\n",
1771 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001772}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001773static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001774
Azael Avalos9d309842015-02-10 23:43:59 -07001775static ssize_t kbd_backlight_timeout_store(struct device *dev,
1776 struct device_attribute *attr,
1777 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001778{
1779 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001780 int time;
1781 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001782
Azael Avaloseabde0f2014-10-04 12:02:21 -06001783 ret = kstrtoint(buf, 0, &time);
1784 if (ret)
1785 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001786
Azael Avaloseabde0f2014-10-04 12:02:21 -06001787 /* Check for supported values depending on kbd_type */
1788 if (toshiba->kbd_type == 1) {
1789 if (time < 0 || time > 60)
1790 return -EINVAL;
1791 } else if (toshiba->kbd_type == 2) {
1792 if (time < 1 || time > 60)
1793 return -EINVAL;
1794 }
1795
1796 /* Set the Keyboard Backlight Timeout */
1797
1798 /* Only make a change if the actual timeout has changed */
1799 if (toshiba->kbd_time != time) {
1800 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001801 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001802 /* OR the "base time" to the actual method format */
1803 if (toshiba->kbd_type == 1)
1804 time |= SCI_KBD_MODE_FNZ;
1805 else if (toshiba->kbd_type == 2)
1806 time |= SCI_KBD_MODE_AUTO;
1807
1808 ret = toshiba_kbd_illum_status_set(toshiba, time);
1809 if (ret)
1810 return ret;
1811
Azael Avalos360f0f32014-03-25 20:38:31 -06001812 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1813 }
1814
1815 return count;
1816}
1817
Azael Avalos9d309842015-02-10 23:43:59 -07001818static ssize_t kbd_backlight_timeout_show(struct device *dev,
1819 struct device_attribute *attr,
1820 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001821{
1822 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1823 u32 time;
1824
1825 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1826 return -EIO;
1827
1828 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1829}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001830static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001831
Azael Avalos9d309842015-02-10 23:43:59 -07001832static ssize_t touchpad_store(struct device *dev,
1833 struct device_attribute *attr,
1834 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001835{
1836 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1837 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001838 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001839
1840 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001841 ret = kstrtoint(buf, 0, &state);
1842 if (ret)
1843 return ret;
1844 if (state != 0 && state != 1)
1845 return -EINVAL;
1846
1847 ret = toshiba_touchpad_set(toshiba, state);
1848 if (ret)
1849 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001850
1851 return count;
1852}
1853
Azael Avalos9d309842015-02-10 23:43:59 -07001854static ssize_t touchpad_show(struct device *dev,
1855 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001856{
1857 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1858 u32 state;
1859 int ret;
1860
1861 ret = toshiba_touchpad_get(toshiba, &state);
1862 if (ret < 0)
1863 return ret;
1864
1865 return sprintf(buf, "%i\n", state);
1866}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001867static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001868
Azael Avalos9d309842015-02-10 23:43:59 -07001869static ssize_t position_show(struct device *dev,
1870 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001871{
1872 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1873 u32 xyval, zval, tmp;
1874 u16 x, y, z;
1875 int ret;
1876
1877 xyval = zval = 0;
1878 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1879 if (ret < 0)
1880 return ret;
1881
1882 x = xyval & HCI_ACCEL_MASK;
1883 tmp = xyval >> HCI_MISC_SHIFT;
1884 y = tmp & HCI_ACCEL_MASK;
1885 z = zval & HCI_ACCEL_MASK;
1886
1887 return sprintf(buf, "%d %d %d\n", x, y, z);
1888}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001889static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001890
Azael Avalos9d309842015-02-10 23:43:59 -07001891static ssize_t usb_sleep_charge_show(struct device *dev,
1892 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001893{
1894 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1895 u32 mode;
1896 int ret;
1897
1898 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1899 if (ret < 0)
1900 return ret;
1901
1902 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1903}
1904
Azael Avalos9d309842015-02-10 23:43:59 -07001905static ssize_t usb_sleep_charge_store(struct device *dev,
1906 struct device_attribute *attr,
1907 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001908{
1909 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1910 u32 mode;
1911 int state;
1912 int ret;
1913
1914 ret = kstrtoint(buf, 0, &state);
1915 if (ret)
1916 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001917 /*
1918 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001919 * 0 - Disabled
1920 * 1 - Alternate (Non USB conformant devices that require more power)
1921 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001922 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001923 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001924 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001925 return -EINVAL;
1926
1927 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001928 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001929 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001930 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001931 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001932 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001933 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001934 mode |= SCI_USB_CHARGE_AUTO;
1935 else if (state == 3)
1936 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001937
1938 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1939 if (ret)
1940 return ret;
1941
1942 return count;
1943}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001944static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001945
Azael Avalos182bcaa2015-01-18 18:30:23 -07001946static ssize_t sleep_functions_on_battery_show(struct device *dev,
1947 struct device_attribute *attr,
1948 char *buf)
1949{
1950 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1951 u32 state;
1952 int bat_lvl;
1953 int status;
1954 int ret;
1955 int tmp;
1956
1957 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1958 if (ret < 0)
1959 return ret;
1960
1961 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1962 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1963 status = (tmp == 0x4) ? 1 : 0;
1964 /* Determine the battery level set */
1965 bat_lvl = state >> HCI_MISC_SHIFT;
1966
1967 return sprintf(buf, "%d %d\n", status, bat_lvl);
1968}
1969
1970static ssize_t sleep_functions_on_battery_store(struct device *dev,
1971 struct device_attribute *attr,
1972 const char *buf, size_t count)
1973{
1974 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1975 u32 status;
1976 int value;
1977 int ret;
1978 int tmp;
1979
1980 ret = kstrtoint(buf, 0, &value);
1981 if (ret)
1982 return ret;
1983
Darren Harte0769fe2015-02-11 20:50:08 -08001984 /*
1985 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07001986 * 0 - Disabled
1987 * 1-100 - Enabled
1988 */
1989 if (value < 0 || value > 100)
1990 return -EINVAL;
1991
1992 if (value == 0) {
1993 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1994 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1995 } else {
1996 tmp = value << HCI_MISC_SHIFT;
1997 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1998 }
1999 ret = toshiba_sleep_functions_status_set(toshiba, status);
2000 if (ret < 0)
2001 return ret;
2002
2003 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2004
2005 return count;
2006}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002007static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002008
Azael Avalos9d309842015-02-10 23:43:59 -07002009static ssize_t usb_rapid_charge_show(struct device *dev,
2010 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002011{
2012 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2013 u32 state;
2014 int ret;
2015
2016 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2017 if (ret < 0)
2018 return ret;
2019
2020 return sprintf(buf, "%d\n", state);
2021}
2022
Azael Avalos9d309842015-02-10 23:43:59 -07002023static ssize_t usb_rapid_charge_store(struct device *dev,
2024 struct device_attribute *attr,
2025 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002026{
2027 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2028 int state;
2029 int ret;
2030
2031 ret = kstrtoint(buf, 0, &state);
2032 if (ret)
2033 return ret;
2034 if (state != 0 && state != 1)
2035 return -EINVAL;
2036
2037 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2038 if (ret)
2039 return ret;
2040
2041 return count;
2042}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002043static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002044
Azael Avalos9d309842015-02-10 23:43:59 -07002045static ssize_t usb_sleep_music_show(struct device *dev,
2046 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002047{
2048 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2049 u32 state;
2050 int ret;
2051
2052 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2053 if (ret < 0)
2054 return ret;
2055
2056 return sprintf(buf, "%d\n", state);
2057}
2058
Azael Avalos9d309842015-02-10 23:43:59 -07002059static ssize_t usb_sleep_music_store(struct device *dev,
2060 struct device_attribute *attr,
2061 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002062{
2063 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2064 int state;
2065 int ret;
2066
2067 ret = kstrtoint(buf, 0, &state);
2068 if (ret)
2069 return ret;
2070 if (state != 0 && state != 1)
2071 return -EINVAL;
2072
2073 ret = toshiba_usb_sleep_music_set(toshiba, state);
2074 if (ret)
2075 return ret;
2076
2077 return count;
2078}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002079static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002080
Azael Avalos9d309842015-02-10 23:43:59 -07002081static ssize_t kbd_function_keys_show(struct device *dev,
2082 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002083{
2084 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2085 int mode;
2086 int ret;
2087
2088 ret = toshiba_function_keys_get(toshiba, &mode);
2089 if (ret < 0)
2090 return ret;
2091
2092 return sprintf(buf, "%d\n", mode);
2093}
2094
Azael Avalos9d309842015-02-10 23:43:59 -07002095static ssize_t kbd_function_keys_store(struct device *dev,
2096 struct device_attribute *attr,
2097 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002098{
2099 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2100 int mode;
2101 int ret;
2102
2103 ret = kstrtoint(buf, 0, &mode);
2104 if (ret)
2105 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002106 /*
2107 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002108 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2109 * 1 - Special functions (Opposite of the above setting)
2110 */
2111 if (mode != 0 && mode != 1)
2112 return -EINVAL;
2113
2114 ret = toshiba_function_keys_set(toshiba, mode);
2115 if (ret)
2116 return ret;
2117
2118 pr_info("Reboot for changes to KBD Function Keys to take effect");
2119
2120 return count;
2121}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002122static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002123
Azael Avalos9d309842015-02-10 23:43:59 -07002124static ssize_t panel_power_on_show(struct device *dev,
2125 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002126{
2127 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2128 u32 state;
2129 int ret;
2130
2131 ret = toshiba_panel_power_on_get(toshiba, &state);
2132 if (ret < 0)
2133 return ret;
2134
2135 return sprintf(buf, "%d\n", state);
2136}
2137
Azael Avalos9d309842015-02-10 23:43:59 -07002138static ssize_t panel_power_on_store(struct device *dev,
2139 struct device_attribute *attr,
2140 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002141{
2142 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2143 int state;
2144 int ret;
2145
2146 ret = kstrtoint(buf, 0, &state);
2147 if (ret)
2148 return ret;
2149 if (state != 0 && state != 1)
2150 return -EINVAL;
2151
2152 ret = toshiba_panel_power_on_set(toshiba, state);
2153 if (ret)
2154 return ret;
2155
2156 pr_info("Reboot for changes to Panel Power ON to take effect");
2157
2158 return count;
2159}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002160static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002161
Azael Avalos9d309842015-02-10 23:43:59 -07002162static ssize_t usb_three_show(struct device *dev,
2163 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002164{
2165 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2166 u32 state;
2167 int ret;
2168
2169 ret = toshiba_usb_three_get(toshiba, &state);
2170 if (ret < 0)
2171 return ret;
2172
2173 return sprintf(buf, "%d\n", state);
2174}
2175
Azael Avalos9d309842015-02-10 23:43:59 -07002176static ssize_t usb_three_store(struct device *dev,
2177 struct device_attribute *attr,
2178 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002179{
2180 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2181 int state;
2182 int ret;
2183
2184 ret = kstrtoint(buf, 0, &state);
2185 if (ret)
2186 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002187 /*
2188 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002189 * 0 - Disabled (Acts like a USB 2 port, saving power)
2190 * 1 - Enabled
2191 */
2192 if (state != 0 && state != 1)
2193 return -EINVAL;
2194
2195 ret = toshiba_usb_three_set(toshiba, state);
2196 if (ret)
2197 return ret;
2198
2199 pr_info("Reboot for changes to USB 3 to take effect");
2200
2201 return count;
2202}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002203static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002204
2205static struct attribute *toshiba_attributes[] = {
2206 &dev_attr_version.attr,
2207 &dev_attr_fan.attr,
2208 &dev_attr_kbd_backlight_mode.attr,
2209 &dev_attr_kbd_type.attr,
2210 &dev_attr_available_kbd_modes.attr,
2211 &dev_attr_kbd_backlight_timeout.attr,
2212 &dev_attr_touchpad.attr,
2213 &dev_attr_position.attr,
2214 &dev_attr_usb_sleep_charge.attr,
2215 &dev_attr_sleep_functions_on_battery.attr,
2216 &dev_attr_usb_rapid_charge.attr,
2217 &dev_attr_usb_sleep_music.attr,
2218 &dev_attr_kbd_function_keys.attr,
2219 &dev_attr_panel_power_on.attr,
2220 &dev_attr_usb_three.attr,
2221 NULL,
2222};
2223
Azael Avalos360f0f32014-03-25 20:38:31 -06002224static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2225 struct attribute *attr, int idx)
2226{
2227 struct device *dev = container_of(kobj, struct device, kobj);
2228 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2229 bool exists = true;
2230
Azael Avalos94477d42015-02-10 21:09:17 -07002231 if (attr == &dev_attr_fan.attr)
2232 exists = (drv->fan_supported) ? true : false;
2233 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002234 exists = (drv->kbd_illum_supported) ? true : false;
2235 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2236 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002237 else if (attr == &dev_attr_touchpad.attr)
2238 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002239 else if (attr == &dev_attr_position.attr)
2240 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002241 else if (attr == &dev_attr_usb_sleep_charge.attr)
2242 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002243 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2244 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002245 else if (attr == &dev_attr_usb_rapid_charge.attr)
2246 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002247 else if (attr == &dev_attr_usb_sleep_music.attr)
2248 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002249 else if (attr == &dev_attr_kbd_function_keys.attr)
2250 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002251 else if (attr == &dev_attr_panel_power_on.attr)
2252 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002253 else if (attr == &dev_attr_usb_three.attr)
2254 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002255
2256 return exists ? attr->mode : 0;
2257}
2258
Azael Avalos9bd12132015-02-10 23:43:58 -07002259static struct attribute_group toshiba_attr_group = {
2260 .is_visible = toshiba_sysfs_is_visible,
2261 .attrs = toshiba_attributes,
2262};
2263
Azael Avalos1f28f292014-12-04 20:22:45 -07002264/*
2265 * Hotkeys
2266 */
2267static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2268{
2269 acpi_status status;
2270 u32 result;
2271
2272 status = acpi_evaluate_object(dev->acpi_dev->handle,
2273 "ENAB", NULL, NULL);
2274 if (ACPI_FAILURE(status))
2275 return -ENODEV;
2276
Azael Avalosd37782b2015-05-06 09:35:10 -06002277 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
Azael Avalos1f28f292014-12-04 20:22:45 -07002278 if (result == TOS_FAILURE)
2279 return -EIO;
2280 else if (result == TOS_NOT_SUPPORTED)
2281 return -ENODEV;
2282
2283 return 0;
2284}
2285
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002286static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2287{
2288 u32 result;
2289
2290 /*
2291 * Re-activate the hotkeys, but this time, we are using the
2292 * "Special Functions" mode.
2293 */
Azael Avalosd37782b2015-05-06 09:35:10 -06002294 result = hci_write(dev, HCI_HOTKEY_EVENT,
2295 HCI_HOTKEY_SPECIAL_FUNCTIONS);
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002296 if (result != TOS_SUCCESS)
2297 pr_err("Could not enable the Special Function mode\n");
2298}
2299
Seth Forshee29cd2932012-01-18 13:44:09 -06002300static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2301 struct serio *port)
2302{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002303 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002304 return false;
2305
2306 if (unlikely(data == 0xe0))
2307 return false;
2308
2309 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2310 schedule_work(&toshiba_acpi->hotkey_work);
2311 return true;
2312 }
2313
2314 return false;
2315}
2316
2317static void toshiba_acpi_hotkey_work(struct work_struct *work)
2318{
2319 acpi_handle ec_handle = ec_get_handle();
2320 acpi_status status;
2321
2322 if (!ec_handle)
2323 return;
2324
2325 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2326 if (ACPI_FAILURE(status))
2327 pr_err("ACPI NTFY method execution failed\n");
2328}
2329
2330/*
2331 * Returns hotkey scancode, or < 0 on failure.
2332 */
2333static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2334{
Zhang Rui74facaf2013-09-03 08:32:15 +08002335 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002336 acpi_status status;
2337
Zhang Rui74facaf2013-09-03 08:32:15 +08002338 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2339 NULL, &value);
2340 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002341 pr_err("ACPI INFO method execution failed\n");
2342 return -EIO;
2343 }
2344
Zhang Rui74facaf2013-09-03 08:32:15 +08002345 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002346}
2347
2348static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2349 int scancode)
2350{
2351 if (scancode == 0x100)
2352 return;
2353
Darren Harte0769fe2015-02-11 20:50:08 -08002354 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002355 if (scancode & 0x80)
2356 return;
2357
2358 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2359 pr_info("Unknown key %x\n", scancode);
2360}
2361
Azael Avalos71454d72014-12-04 20:22:46 -07002362static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2363{
2364 u32 hci_result, value;
2365 int retries = 3;
2366 int scancode;
2367
2368 if (dev->info_supported) {
2369 scancode = toshiba_acpi_query_hotkey(dev);
2370 if (scancode < 0)
2371 pr_err("Failed to query hotkey event\n");
2372 else if (scancode != 0)
2373 toshiba_acpi_report_hotkey(dev, scancode);
2374 } else if (dev->system_event_supported) {
2375 do {
Azael Avalosd37782b2015-05-06 09:35:10 -06002376 hci_result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos71454d72014-12-04 20:22:46 -07002377 switch (hci_result) {
2378 case TOS_SUCCESS:
2379 toshiba_acpi_report_hotkey(dev, (int)value);
2380 break;
2381 case TOS_NOT_SUPPORTED:
2382 /*
2383 * This is a workaround for an unresolved
2384 * issue on some machines where system events
2385 * sporadically become disabled.
2386 */
2387 hci_result =
Azael Avalosd37782b2015-05-06 09:35:10 -06002388 hci_write(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos71454d72014-12-04 20:22:46 -07002389 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002390 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002391 default:
2392 retries--;
2393 break;
2394 }
2395 } while (retries && hci_result != TOS_FIFO_EMPTY);
2396 }
2397}
2398
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002399static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002400{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002401 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002402 acpi_handle ec_handle;
2403 u32 events_type;
2404 u32 hci_result;
2405 int error;
2406
2407 error = toshiba_acpi_enable_hotkeys(dev);
2408 if (error)
2409 return error;
2410
2411 error = toshiba_hotkey_event_type_get(dev, &events_type);
2412 if (error) {
2413 pr_err("Unable to query Hotkey Event Type\n");
2414 return error;
2415 }
2416 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002417
Seth Forshee135740d2011-09-20 16:55:49 -05002418 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002419 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002420 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002421
2422 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002423 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002424 dev->hotkey_dev->id.bustype = BUS_HOST;
2425
Azael Avalosa2b34712015-03-20 16:55:17 -06002426 if (events_type == HCI_SYSTEM_TYPE1 ||
2427 !dev->kbd_function_keys_supported)
2428 keymap = toshiba_acpi_keymap;
2429 else if (events_type == HCI_SYSTEM_TYPE2 ||
2430 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002431 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002432 else
2433 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002434 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002435 if (error)
2436 goto err_free_dev;
2437
Seth Forshee29cd2932012-01-18 13:44:09 -06002438 /*
2439 * For some machines the SCI responsible for providing hotkey
2440 * notification doesn't fire. We can trigger the notification
2441 * whenever the Fn key is pressed using the NTFY method, if
2442 * supported, so if it's present set up an i8042 key filter
2443 * for this purpose.
2444 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002445 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002446 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002447 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2448
2449 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2450 if (error) {
2451 pr_err("Error installing key filter\n");
2452 goto err_free_keymap;
2453 }
2454
2455 dev->ntfy_supported = 1;
2456 }
2457
2458 /*
2459 * Determine hotkey query interface. Prefer using the INFO
2460 * method when it is available.
2461 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002462 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002463 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002464 else {
Azael Avalosd37782b2015-05-06 09:35:10 -06002465 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002466 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002467 dev->system_event_supported = 1;
2468 }
2469
2470 if (!dev->info_supported && !dev->system_event_supported) {
2471 pr_warn("No hotkey query interface found\n");
2472 goto err_remove_filter;
2473 }
2474
Seth Forshee135740d2011-09-20 16:55:49 -05002475 error = input_register_device(dev->hotkey_dev);
2476 if (error) {
2477 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002478 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002479 }
2480
2481 return 0;
2482
Seth Forshee29cd2932012-01-18 13:44:09 -06002483 err_remove_filter:
2484 if (dev->ntfy_supported)
2485 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002486 err_free_keymap:
2487 sparse_keymap_free(dev->hotkey_dev);
2488 err_free_dev:
2489 input_free_device(dev->hotkey_dev);
2490 dev->hotkey_dev = NULL;
2491 return error;
2492}
2493
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002494static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002495{
2496 struct backlight_properties props;
2497 int brightness;
2498 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002499 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002500
2501 /*
2502 * Some machines don't support the backlight methods at all, and
2503 * others support it read-only. Either of these is pretty useless,
2504 * so only register the backlight device if the backlight method
2505 * supports both reads and writes.
2506 */
2507 brightness = __get_lcd_brightness(dev);
2508 if (brightness < 0)
2509 return 0;
2510 ret = set_lcd_brightness(dev, brightness);
2511 if (ret) {
2512 pr_debug("Backlight method is read-only, disabling backlight support\n");
2513 return 0;
2514 }
2515
Akio Idehara121b7b02012-04-06 01:46:43 +09002516 /* Determine whether or not BIOS supports transflective backlight */
2517 ret = get_tr_backlight_status(dev, &enabled);
2518 dev->tr_backlight_supported = !ret;
2519
Hans de Goede358d6a22015-04-21 12:01:32 +02002520 /*
2521 * Tell acpi-video-detect code to prefer vendor backlight on all
2522 * systems with transflective backlight and on dmi matched systems.
2523 */
2524 if (dev->tr_backlight_supported ||
2525 dmi_check_system(toshiba_vendor_backlight_dmi))
2526 acpi_video_dmi_promote_vendor();
2527
2528 if (acpi_video_backlight_support())
2529 return 0;
2530
2531 /* acpi-video may have loaded before we called dmi_promote_vendor() */
2532 acpi_video_unregister_backlight();
2533
Matthew Garrett53039f22012-06-01 11:02:36 -04002534 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002535 props.type = BACKLIGHT_PLATFORM;
2536 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002537
Darren Harte0769fe2015-02-11 20:50:08 -08002538 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002539 if (dev->tr_backlight_supported)
2540 props.max_brightness++;
2541
Seth Forshee62cce752012-04-19 11:23:50 -05002542 dev->backlight_dev = backlight_device_register("toshiba",
2543 &dev->acpi_dev->dev,
2544 dev,
2545 &toshiba_backlight_data,
2546 &props);
2547 if (IS_ERR(dev->backlight_dev)) {
2548 ret = PTR_ERR(dev->backlight_dev);
2549 pr_err("Could not register toshiba backlight device\n");
2550 dev->backlight_dev = NULL;
2551 return ret;
2552 }
2553
2554 dev->backlight_dev->props.brightness = brightness;
2555 return 0;
2556}
2557
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002558static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002559{
2560 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2561
Seth Forshee36d03f92011-09-20 16:55:53 -05002562 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002563
Azael Avalos360f0f32014-03-25 20:38:31 -06002564 if (dev->sysfs_created)
2565 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2566 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002567
Seth Forshee29cd2932012-01-18 13:44:09 -06002568 if (dev->ntfy_supported) {
2569 i8042_remove_filter(toshiba_acpi_i8042_filter);
2570 cancel_work_sync(&dev->hotkey_work);
2571 }
2572
Seth Forshee135740d2011-09-20 16:55:49 -05002573 if (dev->hotkey_dev) {
2574 input_unregister_device(dev->hotkey_dev);
2575 sparse_keymap_free(dev->hotkey_dev);
2576 }
2577
Markus Elfring00981812014-11-24 20:30:29 +01002578 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002579
Seth Forshee36d03f92011-09-20 16:55:53 -05002580 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002581 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002582
Azael Avalos360f0f32014-03-25 20:38:31 -06002583 if (dev->kbd_led_registered)
2584 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002585
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002586 if (dev->eco_supported)
2587 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002588
Seth Forshee29cd2932012-01-18 13:44:09 -06002589 if (toshiba_acpi)
2590 toshiba_acpi = NULL;
2591
Seth Forshee135740d2011-09-20 16:55:49 -05002592 kfree(dev);
2593
2594 return 0;
2595}
2596
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002597static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002598{
Zhang Ruie2e19602013-09-03 08:32:06 +08002599 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002600 return "GHCI";
2601
Zhang Ruie2e19602013-09-03 08:32:06 +08002602 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002603 return "SPFC";
2604
2605 return NULL;
2606}
2607
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002608static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002609{
2610 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002611 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002612 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002613 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002614 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002615
Seth Forshee29cd2932012-01-18 13:44:09 -06002616 if (toshiba_acpi)
2617 return -EBUSY;
2618
Seth Forshee135740d2011-09-20 16:55:49 -05002619 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2620 TOSHIBA_ACPI_VERSION);
2621
Seth Forsheea540d6b2011-09-20 16:55:52 -05002622 hci_method = find_hci_method(acpi_dev->handle);
2623 if (!hci_method) {
2624 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002625 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002626 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002627
Seth Forshee135740d2011-09-20 16:55:49 -05002628 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2629 if (!dev)
2630 return -ENOMEM;
2631 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002632 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002633 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002634 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002635
Azael Avalosa2b34712015-03-20 16:55:17 -06002636 /* Query the BIOS for supported features */
2637
2638 /*
2639 * The "Special Functions" are always supported by the laptops
2640 * with the new keyboard layout, query for its presence to help
2641 * determine the keymap layout to use.
2642 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002643 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002644 dev->kbd_function_keys_supported = !ret;
2645
Seth Forshee6e02cc72011-09-20 16:55:51 -05002646 if (toshiba_acpi_setup_keyboard(dev))
2647 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002648
Seth Forshee62cce752012-04-19 11:23:50 -05002649 ret = toshiba_acpi_setup_backlight(dev);
2650 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002651 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002652
Seth Forshee135740d2011-09-20 16:55:49 -05002653 if (toshiba_illumination_available(dev)) {
2654 dev->led_dev.name = "toshiba::illumination";
2655 dev->led_dev.max_brightness = 1;
2656 dev->led_dev.brightness_set = toshiba_illumination_set;
2657 dev->led_dev.brightness_get = toshiba_illumination_get;
2658 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002659 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002660 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002661
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002662 if (toshiba_eco_mode_available(dev)) {
2663 dev->eco_led.name = "toshiba::eco_mode";
2664 dev->eco_led.max_brightness = 1;
2665 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2666 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2667 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2668 dev->eco_supported = 1;
2669 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002670
Azael Avalos93f8c162014-09-12 18:50:36 -06002671 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002672 /*
2673 * Only register the LED if KBD illumination is supported
2674 * and the keyboard backlight operation mode is set to FN-Z
2675 */
2676 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2677 dev->kbd_led.name = "toshiba::kbd_backlight";
2678 dev->kbd_led.max_brightness = 1;
2679 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2680 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2681 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2682 dev->kbd_led_registered = 1;
2683 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002684
Azael Avalos9d8658a2014-03-25 20:38:32 -06002685 ret = toshiba_touchpad_get(dev, &dummy);
2686 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002687
Azael Avalos5a2813e2014-03-25 20:38:34 -06002688 ret = toshiba_accelerometer_supported(dev);
2689 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002690
Azael Avalosc8c91842015-04-02 19:26:20 -06002691 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002692
Azael Avalosbb3fe012015-01-18 18:30:24 -07002693 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2694 dev->usb_rapid_charge_supported = !ret;
2695
Azael Avalos172ce0a2015-01-18 18:30:25 -07002696 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2697 dev->usb_sleep_music_supported = !ret;
2698
Azael Avalos35d53ce2015-02-10 21:09:19 -07002699 ret = toshiba_panel_power_on_get(dev, &dummy);
2700 dev->panel_power_on_supported = !ret;
2701
Azael Avalos17fe4b32015-02-10 21:09:20 -07002702 ret = toshiba_usb_three_get(dev, &dummy);
2703 dev->usb_three_supported = !ret;
2704
Seth Forshee36d03f92011-09-20 16:55:53 -05002705 ret = get_video_status(dev, &dummy);
2706 dev->video_supported = !ret;
2707
2708 ret = get_fan_status(dev, &dummy);
2709 dev->fan_supported = !ret;
2710
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002711 /*
2712 * Enable the "Special Functions" mode only if they are
2713 * supported and if they are activated.
2714 */
2715 if (dev->kbd_function_keys_supported && special_functions)
2716 toshiba_acpi_enable_special_functions(dev);
2717
Azael Avalos360f0f32014-03-25 20:38:31 -06002718 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2719 &toshiba_attr_group);
2720 if (ret) {
2721 dev->sysfs_created = 0;
2722 goto error;
2723 }
2724 dev->sysfs_created = !ret;
2725
Seth Forshee36d03f92011-09-20 16:55:53 -05002726 create_toshiba_proc_entries(dev);
2727
Seth Forshee29cd2932012-01-18 13:44:09 -06002728 toshiba_acpi = dev;
2729
Seth Forshee135740d2011-09-20 16:55:49 -05002730 return 0;
2731
2732error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002733 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002734 return ret;
2735}
2736
2737static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2738{
2739 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002740 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002741
Azael Avalos71454d72014-12-04 20:22:46 -07002742 switch (event) {
2743 case 0x80: /* Hotkeys and some system events */
2744 toshiba_acpi_process_hotkeys(dev);
2745 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002746 case 0x81: /* Dock events */
2747 case 0x82:
2748 case 0x83:
2749 pr_info("Dock event received %x\n", event);
2750 break;
2751 case 0x88: /* Thermal events */
2752 pr_info("Thermal event received\n");
2753 break;
2754 case 0x8f: /* LID closed */
2755 case 0x90: /* LID is closed and Dock has been ejected */
2756 break;
2757 case 0x8c: /* SATA power events */
2758 case 0x8b:
2759 pr_info("SATA power event received %x\n", event);
2760 break;
Azael Avalos80546902014-12-04 20:22:47 -07002761 case 0x92: /* Keyboard backlight mode changed */
2762 /* Update sysfs entries */
2763 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2764 &toshiba_attr_group);
2765 if (ret)
2766 pr_err("Unable to update sysfs entries\n");
2767 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002768 case 0x85: /* Unknown */
2769 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002770 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002771 case 0x94: /* Unknown */
2772 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002773 default:
2774 pr_info("Unknown event received %x\n", event);
2775 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002776 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002777
2778 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2779 dev_name(&acpi_dev->dev),
2780 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002781}
2782
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002783#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002784static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002785{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002786 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002787 u32 result;
2788
2789 if (dev->hotkey_dev)
Azael Avalosd37782b2015-05-06 09:35:10 -06002790 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002791
2792 return 0;
2793}
2794
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002795static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002796{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002797 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002798 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002799
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002800 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002801 error = toshiba_acpi_enable_hotkeys(dev);
2802 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002803 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002804 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002805
2806 return 0;
2807}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002808#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002809
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002810static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2811 toshiba_acpi_suspend, toshiba_acpi_resume);
2812
Seth Forshee135740d2011-09-20 16:55:49 -05002813static struct acpi_driver toshiba_acpi_driver = {
2814 .name = "Toshiba ACPI driver",
2815 .owner = THIS_MODULE,
2816 .ids = toshiba_device_ids,
2817 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2818 .ops = {
2819 .add = toshiba_acpi_add,
2820 .remove = toshiba_acpi_remove,
2821 .notify = toshiba_acpi_notify,
2822 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002823 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002824};
Holger Machtc9263552006-10-20 14:30:29 -07002825
Len Brown4be44fc2005-08-05 00:44:28 -04002826static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Seth Forshee135740d2011-09-20 16:55:49 -05002828 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Seth Forsheef11f9992012-01-18 13:44:11 -06002830 /*
2831 * Machines with this WMI guid aren't supported due to bugs in
2832 * their AML. This check relies on wmi initializing before
2833 * toshiba_acpi to guarantee guids have been identified.
2834 */
2835 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2836 return -ENODEV;
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2839 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002840 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002841 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
2843
Seth Forshee135740d2011-09-20 16:55:49 -05002844 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2845 if (ret) {
2846 pr_err("Failed to register ACPI driver: %d\n", ret);
2847 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002848 }
2849
Seth Forshee135740d2011-09-20 16:55:49 -05002850 return ret;
2851}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002852
Seth Forshee135740d2011-09-20 16:55:49 -05002853static void __exit toshiba_acpi_exit(void)
2854{
2855 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2856 if (toshiba_proc_dir)
2857 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860module_init(toshiba_acpi_init);
2861module_exit(toshiba_acpi_exit);