blob: 70370d3f5dcf6e97fb0695aff8d78c417e30060f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02007 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos548c4302014-03-25 20:38:35 -06008 * Copyright (C) 2014 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * The devolpment page for this driver is located at
26 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 *
28 * Credits:
29 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30 * engineering the Windows drivers
31 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32 * Rob Miller - TV out and hotkeys help
33 *
34 *
35 * TODO
36 *
37 */
38
Joe Perches7e334602011-03-29 15:21:52 -070039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Azael Avalos548c4302014-03-25 20:38:35 -060041#define TOSHIBA_ACPI_VERSION "0.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define PROC_INTERFACE_VERSION 1
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/types.h>
48#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080049#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070050#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040051#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050052#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070053#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020054#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060056#include <linux/workqueue.h>
57#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080058#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020059#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/uaccess.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_AUTHOR("John Belmonte");
63MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64MODULE_LICENSE("GPL");
65
Seth Forsheef11f9992012-01-18 13:44:11 -060066#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
Seth Forshee29cd2932012-01-18 13:44:09 -060068/* Scan code for Fn key on TOS1900 models */
69#define TOS1900_FN_SCAN 0x6e
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
73
Azael Avalos258c5902014-09-29 20:40:07 -060074/* The Toshiba configuration interface is composed of the HCI and the SCI,
75 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78 * be uniform across all their models. Ideally we would just call
79 * dedicated ACPI methods instead of using this primitive interface.
80 * However the ACPI methods seem to be incomplete in some areas (for
81 * example they allow setting, but not reading, the LCD brightness value),
82 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040083 *
Azael Avalos84a62732014-03-25 20:38:29 -060084 * SCI stands for "System Configuration Interface" which aim is to
85 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Azael Avalos258c5902014-09-29 20:40:07 -060088#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* operations */
91#define HCI_SET 0xff00
92#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060093#define SCI_OPEN 0xf100
94#define SCI_CLOSE 0xf200
95#define SCI_GET 0xf300
96#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060099#define TOS_SUCCESS 0x0000
100#define TOS_OPEN_CLOSE_OK 0x0044
101#define TOS_FAILURE 0x1000
102#define TOS_NOT_SUPPORTED 0x8000
103#define TOS_ALREADY_OPEN 0x8100
104#define TOS_NOT_OPENED 0x8200
105#define TOS_INPUT_DATA_ERROR 0x8300
106#define TOS_WRITE_PROTECTED 0x8400
107#define TOS_NOT_PRESENT 0x8600
108#define TOS_FIFO_EMPTY 0x8c00
109#define TOS_DATA_NOT_AVAILABLE 0x8d20
110#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700111#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* registers */
114#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900115#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define HCI_SYSTEM_EVENT 0x0016
117#define HCI_VIDEO_OUT 0x001c
118#define HCI_HOTKEY_EVENT 0x001e
119#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400120#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600121#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600122#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600123#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600124#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos35d53ce2015-02-10 21:09:19 -0700125#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600126#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700127#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600128#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700129#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos9d8658a2014-03-25 20:38:32 -0600130#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700131#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600134#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600135#define HCI_HOTKEY_DISABLE 0x0b
136#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define HCI_LCD_BRIGHTNESS_BITS 3
138#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
139#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600140#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define HCI_VIDEO_OUT_LCD 0x1
142#define HCI_VIDEO_OUT_CRT 0x2
143#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400144#define HCI_WIRELESS_KILL_SWITCH 0x01
145#define HCI_WIRELESS_BT_PRESENT 0x0f
146#define HCI_WIRELESS_BT_ATTACH 0x40
147#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600148#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600149#define SCI_KBD_MODE_FNZ 0x1
150#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600151#define SCI_KBD_MODE_ON 0x8
152#define SCI_KBD_MODE_OFF 0x10
153#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700154#define SCI_USB_CHARGE_MODE_MASK 0xff
155#define SCI_USB_CHARGE_DISABLED 0x30000
156#define SCI_USB_CHARGE_ALTERNATE 0x30009
157#define SCI_USB_CHARGE_AUTO 0x30021
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;
167 struct rfkill *bt_rfk;
168 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600169 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500170 struct backlight_device *backlight_dev;
171 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600172 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600173 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500174
Seth Forshee135740d2011-09-20 16:55:49 -0500175 int force_fan;
176 int last_key_event;
177 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600178 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600179 int kbd_mode;
180 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700181 int usbsc_bat_level;
Seth Forshee135740d2011-09-20 16:55:49 -0500182
Dan Carpenter592b7462012-01-15 14:28:20 +0300183 unsigned int illumination_supported:1;
184 unsigned int video_supported:1;
185 unsigned int fan_supported:1;
186 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600187 unsigned int ntfy_supported:1;
188 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900189 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600190 unsigned int kbd_illum_supported:1;
191 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600192 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600193 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600194 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700195 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700196 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700197 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700198 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700199 unsigned int panel_power_on_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600200 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500201
Seth Forshee135740d2011-09-20 16:55:49 -0500202 struct mutex mutex;
203};
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 +0200250/* alternative keymap */
251static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
252 {
253 .matches = {
254 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
255 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
256 },
257 },
Azael Avalose6efad72014-08-04 09:21:02 -0600258 {
259 .matches = {
260 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
261 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
262 },
263 },
Aaron Lub1bde682014-10-23 16:18:02 +0800264 {
265 .matches = {
266 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
267 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
268 },
269 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200270 {}
271};
272
273static const struct key_entry toshiba_acpi_alt_keymap[] = {
274 { KE_KEY, 0x157, { KEY_MUTE } },
275 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
276 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600277 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200278 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
279 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
280 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
281 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
282 { KE_KEY, 0x158, { KEY_WLAN } },
283 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
284 { KE_END, 0 },
285};
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* utility
288 */
289
Len Brown4be44fc2005-08-05 00:44:28 -0400290static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 *word = (*word & ~mask) | (mask * value);
293}
294
295/* acpi interface wrappers
296 */
297
Len Brown4be44fc2005-08-05 00:44:28 -0400298static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 acpi_status status;
301
Zhang Rui619400d2013-09-03 08:31:56 +0800302 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500303 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Azael Avalos258c5902014-09-29 20:40:07 -0600306/* Perform a raw configuration call. Here we don't care about input or output
307 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Azael Avalos258c5902014-09-29 20:40:07 -0600309static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
310 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600313 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600315 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 acpi_status status;
317 int i;
318
Azael Avalos258c5902014-09-29 20:40:07 -0600319 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600321 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 in_objs[i].type = ACPI_TYPE_INTEGER;
323 in_objs[i].integer.value = in[i];
324 }
325
326 results.length = sizeof(out_objs);
327 results.pointer = out_objs;
328
Seth Forshee6e02cc72011-09-20 16:55:51 -0500329 status = acpi_evaluate_object(dev->acpi_dev->handle,
330 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400331 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600332 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 for (i = 0; i < out_objs->package.count; ++i) {
334 out[i] = out_objs->package.elements[i].integer.value;
335 }
336 }
337
338 return status;
339}
340
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400341/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * In addition to the ACPI status, the HCI system returns a result which
344 * may be useful (such as "not supported").
345 */
346
Azael Avalos893f3f62014-09-29 20:40:09 -0600347static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Azael Avalos258c5902014-09-29 20:40:07 -0600349 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
350 u32 out[TCI_WORDS];
351 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600352
353 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Azael Avalos893f3f62014-09-29 20:40:09 -0600356static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Azael Avalos258c5902014-09-29 20:40:07 -0600358 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
359 u32 out[TCI_WORDS];
360 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600361 if (ACPI_FAILURE(status))
362 return TOS_FAILURE;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600365
366 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Azael Avalos893f3f62014-09-29 20:40:09 -0600369static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400370{
Azael Avalos258c5902014-09-29 20:40:07 -0600371 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
372 u32 out[TCI_WORDS];
373 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600374
375 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400376}
377
Azael Avalos893f3f62014-09-29 20:40:09 -0600378static u32 hci_read2(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400379{
Azael Avalos258c5902014-09-29 20:40:07 -0600380 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
381 u32 out[TCI_WORDS];
382 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600383 if (ACPI_FAILURE(status))
384 return TOS_FAILURE;
385
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400386 *out1 = out[2];
387 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600388
389 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400390}
391
Azael Avalos84a62732014-03-25 20:38:29 -0600392/* common sci tasks
393 */
394
395static int sci_open(struct toshiba_acpi_dev *dev)
396{
Azael Avalos258c5902014-09-29 20:40:07 -0600397 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
398 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600399 acpi_status status;
400
Azael Avalos258c5902014-09-29 20:40:07 -0600401 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600402 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600403 pr_err("ACPI call to open SCI failed\n");
404 return 0;
405 }
406
Azael Avalos1864bbc2014-09-29 20:40:08 -0600407 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600408 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600409 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600410 pr_info("Toshiba SCI already opened\n");
411 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700412 } else if (out[0] == TOS_NOT_SUPPORTED) {
413 /* Some BIOSes do not have the SCI open/close functions
414 * implemented and return 0x8000 (Not Supported), failing to
415 * register some supported features.
416 *
417 * Simply return 1 if we hit those affected laptops to make the
418 * supported features work.
419 *
420 * In the case that some laptops really do not support the SCI,
421 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
422 * and thus, not registering support for the queried feature.
423 */
424 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600425 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600426 pr_info("Toshiba SCI is not present\n");
427 }
428
429 return 0;
430}
431
432static void sci_close(struct toshiba_acpi_dev *dev)
433{
Azael Avalos258c5902014-09-29 20:40:07 -0600434 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
435 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600436 acpi_status status;
437
Azael Avalos258c5902014-09-29 20:40:07 -0600438 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600439 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600440 pr_err("ACPI call to close SCI failed\n");
441 return;
442 }
443
Azael Avalos1864bbc2014-09-29 20:40:08 -0600444 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600445 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600446 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600447 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600448 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600449 pr_info("Toshiba SCI is not present\n");
450}
451
Azael Avalos893f3f62014-09-29 20:40:09 -0600452static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600453{
Azael Avalos258c5902014-09-29 20:40:07 -0600454 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
455 u32 out[TCI_WORDS];
456 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600457 if (ACPI_FAILURE(status))
458 return TOS_FAILURE;
459
Azael Avalos84a62732014-03-25 20:38:29 -0600460 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600461
462 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600463}
464
Azael Avalos893f3f62014-09-29 20:40:09 -0600465static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600466{
Azael Avalos258c5902014-09-29 20:40:07 -0600467 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
468 u32 out[TCI_WORDS];
469 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600470
471 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600472}
473
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200474/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500475static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200476{
Azael Avalos258c5902014-09-29 20:40:07 -0600477 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
478 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200479 acpi_status status;
480
Azael Avalosfdb79082014-03-25 20:38:30 -0600481 if (!sci_open(dev))
482 return 0;
483
Azael Avalos258c5902014-09-29 20:40:07 -0600484 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600485 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600486 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600487 pr_err("ACPI call to query Illumination support failed\n");
488 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600489 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700490 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200491 return 0;
492 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600493
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200494 return 1;
495}
496
497static void toshiba_illumination_set(struct led_classdev *cdev,
498 enum led_brightness brightness)
499{
Seth Forshee135740d2011-09-20 16:55:49 -0500500 struct toshiba_acpi_dev *dev = container_of(cdev,
501 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600502 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200503
504 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600505 if (!sci_open(dev))
506 return;
507
508 /* Switch the illumination on/off */
509 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600510 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600511 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600512 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600513 pr_err("ACPI call for illumination failed\n");
514 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600515 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600516 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200517 return;
518 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200519}
520
521static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
522{
Seth Forshee135740d2011-09-20 16:55:49 -0500523 struct toshiba_acpi_dev *dev = container_of(cdev,
524 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600525 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200526
527 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600528 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200529 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200530
531 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600532 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600533 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600534 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600535 pr_err("ACPI call for illumination failed\n");
536 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600537 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600538 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200539 return LED_OFF;
540 }
541
Azael Avalosfdb79082014-03-25 20:38:30 -0600542 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200543}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400544
Azael Avalos360f0f32014-03-25 20:38:31 -0600545/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600546static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
547{
Azael Avalos258c5902014-09-29 20:40:07 -0600548 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
549 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600550 acpi_status status;
551
552 if (!sci_open(dev))
553 return 0;
554
Azael Avalos258c5902014-09-29 20:40:07 -0600555 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600556 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600557 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600558 pr_err("ACPI call to query kbd illumination support failed\n");
559 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600560 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600561 pr_info("Keyboard illumination not available\n");
562 return 0;
563 }
564
565 /* Check for keyboard backlight timeout max value,
566 * previous kbd backlight implementation set this to
567 * 0x3c0003, and now the new implementation set this
568 * to 0x3c001a, use this to distinguish between them
569 */
570 if (out[3] == SCI_KBD_TIME_MAX)
571 dev->kbd_type = 2;
572 else
573 dev->kbd_type = 1;
574 /* Get the current keyboard backlight mode */
575 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
576 /* Get the current time (1-60 seconds) */
577 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
578
579 return 1;
580}
581
Azael Avalos360f0f32014-03-25 20:38:31 -0600582static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
583{
584 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600585
586 if (!sci_open(dev))
587 return -EIO;
588
Azael Avalos893f3f62014-09-29 20:40:09 -0600589 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600590 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600591 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600592 pr_err("ACPI call to set KBD backlight status failed\n");
593 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600594 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600595 pr_info("Keyboard backlight status not supported\n");
596 return -ENODEV;
597 }
598
599 return 0;
600}
601
602static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
603{
604 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600605
606 if (!sci_open(dev))
607 return -EIO;
608
Azael Avalos893f3f62014-09-29 20:40:09 -0600609 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600610 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600611 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600612 pr_err("ACPI call to get KBD backlight status failed\n");
613 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600614 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600615 pr_info("Keyboard backlight status not supported\n");
616 return -ENODEV;
617 }
618
619 return 0;
620}
621
622static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
623{
624 struct toshiba_acpi_dev *dev = container_of(cdev,
625 struct toshiba_acpi_dev, kbd_led);
626 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600627
628 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600629 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
630 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600631 pr_err("ACPI call to get the keyboard backlight failed\n");
632 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600633 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600634 pr_info("Keyboard backlight not supported\n");
635 return LED_OFF;
636 }
637
638 return state ? LED_FULL : LED_OFF;
639}
640
641static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
642 enum led_brightness brightness)
643{
644 struct toshiba_acpi_dev *dev = container_of(cdev,
645 struct toshiba_acpi_dev, kbd_led);
646 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600647
648 /* Set the keyboard backlight state */
649 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600650 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
651 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600652 pr_err("ACPI call to set KBD Illumination mode failed\n");
653 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600654 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600655 pr_info("Keyboard backlight not supported\n");
656 return;
657 }
658}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400659
Azael Avalos9d8658a2014-03-25 20:38:32 -0600660/* TouchPad support */
661static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
662{
663 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600664
665 if (!sci_open(dev))
666 return -EIO;
667
Azael Avalos893f3f62014-09-29 20:40:09 -0600668 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600669 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600670 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600671 pr_err("ACPI call to set the touchpad failed\n");
672 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600673 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600674 return -ENODEV;
675 }
676
677 return 0;
678}
679
680static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
681{
682 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600683
684 if (!sci_open(dev))
685 return -EIO;
686
Azael Avalos893f3f62014-09-29 20:40:09 -0600687 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600688 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600689 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600690 pr_err("ACPI call to query the touchpad failed\n");
691 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600692 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600693 return -ENODEV;
694 }
695
696 return 0;
697}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200698
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600699/* Eco Mode support */
700static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
701{
702 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700703 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600704 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600705
Azael Avalos258c5902014-09-29 20:40:07 -0600706 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700707 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
708 pr_err("ACPI call to get ECO led failed\n");
709 } else if (out[0] == TOS_NOT_INSTALLED) {
710 pr_info("ECO led not installed");
711 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
712 /* If we receive 0x8300 (Input Data Error), it means that the
713 * LED device is present, but that we just screwed the input
714 * parameters.
715 *
716 * Let's query the status of the LED to see if we really have a
717 * success response, indicating the actual presense of the LED,
718 * bail out otherwise.
719 */
720 in[3] = 1;
721 status = tci_raw(dev, in, out);
722 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
723 pr_err("ACPI call to get ECO led failed\n");
724 else if (out[0] == TOS_SUCCESS)
725 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600726 }
727
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700728 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600729}
730
731static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
732{
733 struct toshiba_acpi_dev *dev = container_of(cdev,
734 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600735 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
736 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600737 acpi_status status;
738
Azael Avalos258c5902014-09-29 20:40:07 -0600739 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600740 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600741 pr_err("ACPI call to get ECO led failed\n");
742 return LED_OFF;
743 }
744
745 return out[2] ? LED_FULL : LED_OFF;
746}
747
748static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
749 enum led_brightness brightness)
750{
751 struct toshiba_acpi_dev *dev = container_of(cdev,
752 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600753 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
754 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600755 acpi_status status;
756
757 /* Switch the Eco Mode led on/off */
758 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600759 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600760 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600761 pr_err("ACPI call to set ECO led failed\n");
762 return;
763 }
764}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400765
Azael Avalos5a2813e2014-03-25 20:38:34 -0600766/* Accelerometer support */
767static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
768{
Azael Avalos258c5902014-09-29 20:40:07 -0600769 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
770 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600771 acpi_status status;
772
773 /* Check if the accelerometer call exists,
774 * this call also serves as initialization
775 */
Azael Avalos258c5902014-09-29 20:40:07 -0600776 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600777 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600778 pr_err("ACPI call to query the accelerometer failed\n");
779 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600780 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
781 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600782 pr_err("Accelerometer not initialized\n");
783 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600784 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600785 pr_info("Accelerometer not supported\n");
786 return -ENODEV;
787 }
788
789 return 0;
790}
791
792static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
793 u32 *xy, u32 *z)
794{
Azael Avalos258c5902014-09-29 20:40:07 -0600795 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
796 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600797 acpi_status status;
798
799 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600800 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600801 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600802 pr_err("ACPI call to query the accelerometer failed\n");
803 return -EIO;
804 }
805
806 *xy = out[2];
807 *z = out[4];
808
809 return 0;
810}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600811
Azael Avalose26ffe52015-01-18 18:30:22 -0700812/* Sleep (Charge and Music) utilities support */
813static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
814 u32 *mode)
815{
816 u32 result;
817
818 if (!sci_open(dev))
819 return -EIO;
820
821 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
822 sci_close(dev);
823 if (result == TOS_FAILURE) {
824 pr_err("ACPI call to set USB S&C mode failed\n");
825 return -EIO;
826 } else if (result == TOS_NOT_SUPPORTED) {
827 pr_info("USB Sleep and Charge not supported\n");
828 return -ENODEV;
829 } else if (result == TOS_INPUT_DATA_ERROR) {
830 return -EIO;
831 }
832
833 return 0;
834}
835
836static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
837 u32 mode)
838{
839 u32 result;
840
841 if (!sci_open(dev))
842 return -EIO;
843
844 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
845 sci_close(dev);
846 if (result == TOS_FAILURE) {
847 pr_err("ACPI call to set USB S&C mode failed\n");
848 return -EIO;
849 } else if (result == TOS_NOT_SUPPORTED) {
850 pr_info("USB Sleep and Charge not supported\n");
851 return -ENODEV;
852 } else if (result == TOS_INPUT_DATA_ERROR) {
853 return -EIO;
854 }
855
856 return 0;
857}
858
Azael Avalos182bcaa2015-01-18 18:30:23 -0700859static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
860 u32 *mode)
861{
862 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
863 u32 out[TCI_WORDS];
864 acpi_status status;
865
866 if (!sci_open(dev))
867 return -EIO;
868
869 in[5] = SCI_USB_CHARGE_BAT_LVL;
870 status = tci_raw(dev, in, out);
871 sci_close(dev);
872 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
873 pr_err("ACPI call to get USB S&C battery level failed\n");
874 return -EIO;
875 } else if (out[0] == TOS_NOT_SUPPORTED) {
876 pr_info("USB Sleep and Charge not supported\n");
877 return -ENODEV;
878 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
879 return -EIO;
880 }
881
882 *mode = out[2];
883
884 return 0;
885}
886
887static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
888 u32 mode)
889{
890 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
891 u32 out[TCI_WORDS];
892 acpi_status status;
893
894 if (!sci_open(dev))
895 return -EIO;
896
897 in[2] = mode;
898 in[5] = SCI_USB_CHARGE_BAT_LVL;
899 status = tci_raw(dev, in, out);
900 sci_close(dev);
901 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
902 pr_err("ACPI call to set USB S&C battery level failed\n");
903 return -EIO;
904 } else if (out[0] == TOS_NOT_SUPPORTED) {
905 pr_info("USB Sleep and Charge not supported\n");
906 return -ENODEV;
907 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
908 return -EIO;
909 }
910
911 return 0;
912}
913
Azael Avalosbb3fe012015-01-18 18:30:24 -0700914static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
915 u32 *state)
916{
917 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
918 u32 out[TCI_WORDS];
919 acpi_status status;
920
921 if (!sci_open(dev))
922 return -EIO;
923
924 in[5] = SCI_USB_CHARGE_RAPID_DSP;
925 status = tci_raw(dev, in, out);
926 sci_close(dev);
927 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
928 pr_err("ACPI call to get USB S&C battery level failed\n");
929 return -EIO;
930 } else if (out[0] == TOS_NOT_SUPPORTED ||
931 out[0] == TOS_INPUT_DATA_ERROR) {
932 pr_info("USB Sleep and Charge not supported\n");
933 return -ENODEV;
934 }
935
936 *state = out[2];
937
938 return 0;
939}
940
941static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
942 u32 state)
943{
944 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
945 u32 out[TCI_WORDS];
946 acpi_status status;
947
948 if (!sci_open(dev))
949 return -EIO;
950
951 in[2] = state;
952 in[5] = SCI_USB_CHARGE_RAPID_DSP;
953 status = tci_raw(dev, in, out);
954 sci_close(dev);
955 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
956 pr_err("ACPI call to set USB S&C battery level failed\n");
957 return -EIO;
958 } else if (out[0] == TOS_NOT_SUPPORTED) {
959 pr_info("USB Sleep and Charge not supported\n");
960 return -ENODEV;
961 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
962 return -EIO;
963 }
964
965 return 0;
966}
967
Azael Avalos172ce0a2015-01-18 18:30:25 -0700968static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
969{
970 u32 result;
971
972 if (!sci_open(dev))
973 return -EIO;
974
975 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
976 sci_close(dev);
977 if (result == TOS_FAILURE) {
978 pr_err("ACPI call to set USB S&C mode failed\n");
979 return -EIO;
980 } else if (result == TOS_NOT_SUPPORTED) {
981 pr_info("USB Sleep and Charge not supported\n");
982 return -ENODEV;
983 } else if (result == TOS_INPUT_DATA_ERROR) {
984 return -EIO;
985 }
986
987 return 0;
988}
989
990static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
991{
992 u32 result;
993
994 if (!sci_open(dev))
995 return -EIO;
996
997 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
998 sci_close(dev);
999 if (result == TOS_FAILURE) {
1000 pr_err("ACPI call to set USB S&C mode failed\n");
1001 return -EIO;
1002 } else if (result == TOS_NOT_SUPPORTED) {
1003 pr_info("USB Sleep and Charge not supported\n");
1004 return -ENODEV;
1005 } else if (result == TOS_INPUT_DATA_ERROR) {
1006 return -EIO;
1007 }
1008
1009 return 0;
1010}
1011
Azael Avalosbae84192015-02-10 21:09:18 -07001012/* Keyboard function keys */
1013static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1014{
1015 u32 result;
1016
1017 if (!sci_open(dev))
1018 return -EIO;
1019
1020 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1021 sci_close(dev);
1022 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1023 pr_err("ACPI call to get KBD function keys failed\n");
1024 return -EIO;
1025 } else if (result == TOS_NOT_SUPPORTED) {
1026 pr_info("KBD function keys not supported\n");
1027 return -ENODEV;
1028 }
1029
1030 return 0;
1031}
1032
1033static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1034{
1035 u32 result;
1036
1037 if (!sci_open(dev))
1038 return -EIO;
1039
1040 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1041 sci_close(dev);
1042 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1043 pr_err("ACPI call to set KBD function keys failed\n");
1044 return -EIO;
1045 } else if (result == TOS_NOT_SUPPORTED) {
1046 pr_info("KBD function keys not supported\n");
1047 return -ENODEV;
1048 }
1049
1050 return 0;
1051}
1052
Azael Avalos35d53ce2015-02-10 21:09:19 -07001053/* Panel Power ON */
1054static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1055{
1056 u32 result;
1057
1058 if (!sci_open(dev))
1059 return -EIO;
1060
1061 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1062 sci_close(dev);
1063 if (result == TOS_FAILURE) {
1064 pr_err("ACPI call to get Panel Power ON failed\n");
1065 return -EIO;
1066 } else if (result == TOS_NOT_SUPPORTED) {
1067 pr_info("Panel Power on not supported\n");
1068 return -ENODEV;
1069 } else if (result == TOS_INPUT_DATA_ERROR) {
1070 return -EIO;
1071 }
1072
1073 return 0;
1074}
1075
1076static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1077{
1078 u32 result;
1079
1080 if (!sci_open(dev))
1081 return -EIO;
1082
1083 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1084 sci_close(dev);
1085 if (result == TOS_FAILURE) {
1086 pr_err("ACPI call to set Panel Power ON failed\n");
1087 return -EIO;
1088 } else if (result == TOS_NOT_SUPPORTED) {
1089 pr_info("Panel Power ON not supported\n");
1090 return -ENODEV;
1091 } else if (result == TOS_INPUT_DATA_ERROR) {
1092 return -EIO;
1093 }
1094
1095 return 0;
1096}
1097
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001098/* Bluetooth rfkill handlers */
1099
Seth Forshee135740d2011-09-20 16:55:49 -05001100static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001101{
1102 u32 hci_result;
1103 u32 value, value2;
1104
1105 value = 0;
1106 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -06001107 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001108 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001109 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1110
1111 return hci_result;
1112}
1113
Seth Forshee135740d2011-09-20 16:55:49 -05001114static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001115{
1116 u32 hci_result;
1117 u32 value, value2;
1118
1119 value = 0;
1120 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -06001121 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001122
1123 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1124 return hci_result;
1125}
1126
Johannes Berg19d337d2009-06-02 13:01:37 +02001127static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001128{
Johannes Berg19d337d2009-06-02 13:01:37 +02001129 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001130 u32 result1, result2;
1131 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +02001132 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001133 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001134
Johannes Berg19d337d2009-06-02 13:01:37 +02001135 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001136
1137 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001138 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001139 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001140 goto out;
1141 }
1142
1143 if (!radio_state) {
1144 err = 0;
1145 goto out;
1146 }
1147
Azael Avalos893f3f62014-09-29 20:40:09 -06001148 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1149 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001150
Azael Avalos1864bbc2014-09-29 20:40:08 -06001151 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001152 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001153 else
1154 err = 0;
1155 out:
1156 mutex_unlock(&dev->mutex);
1157 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001158}
1159
Johannes Berg19d337d2009-06-02 13:01:37 +02001160static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001161{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001162 bool new_rfk_state;
1163 bool value;
1164 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +02001165 struct toshiba_acpi_dev *dev = data;
1166
1167 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001168
Seth Forshee135740d2011-09-20 16:55:49 -05001169 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001170 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001171 /* Can't do anything useful */
1172 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001173 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001174 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001175
1176 new_rfk_state = value;
1177
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001178 mutex_unlock(&dev->mutex);
1179
Johannes Berg19d337d2009-06-02 13:01:37 +02001180 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1181 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001182}
1183
Johannes Berg19d337d2009-06-02 13:01:37 +02001184static const struct rfkill_ops toshiba_rfk_ops = {
1185 .set_block = bt_rfkill_set_block,
1186 .poll = bt_rfkill_poll,
1187};
1188
Akio Idehara121b7b02012-04-06 01:46:43 +09001189static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1190{
1191 u32 hci_result;
1192 u32 status;
1193
Azael Avalos893f3f62014-09-29 20:40:09 -06001194 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001195 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001196 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001197}
1198
1199static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1200{
1201 u32 hci_result;
1202 u32 value = !enable;
1203
Azael Avalos893f3f62014-09-29 20:40:09 -06001204 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001205 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001206}
1207
Len Brown4be44fc2005-08-05 00:44:28 -04001208static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Seth Forshee62cce752012-04-19 11:23:50 -05001210static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 u32 hci_result;
1213 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001214 int brightness = 0;
1215
1216 if (dev->tr_backlight_supported) {
1217 bool enabled;
1218 int ret = get_tr_backlight_status(dev, &enabled);
1219 if (ret)
1220 return ret;
1221 if (enabled)
1222 return 0;
1223 brightness++;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Azael Avalos893f3f62014-09-29 20:40:09 -06001226 hci_result = hci_read1(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);
1236 return __get_lcd_brightness(dev);
1237}
1238
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001239static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001240{
Seth Forshee135740d2011-09-20 16:55:49 -05001241 struct toshiba_acpi_dev *dev = m->private;
1242 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001243 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001244
Seth Forshee135740d2011-09-20 16:55:49 -05001245 if (!dev->backlight_dev)
1246 return -ENODEV;
1247
Akio Idehara121b7b02012-04-06 01:46:43 +09001248 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001249 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001250 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001251 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001252 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001256 pr_err("Error reading LCD brightness\n");
1257 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001258}
1259
1260static int lcd_proc_open(struct inode *inode, struct file *file)
1261{
Al Virod9dda782013-03-31 18:16:14 -04001262 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Seth Forshee62cce752012-04-19 11:23:50 -05001265static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001266{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001267 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001268
Akio Idehara121b7b02012-04-06 01:46:43 +09001269 if (dev->tr_backlight_supported) {
1270 bool enable = !value;
1271 int ret = set_tr_backlight_status(dev, enable);
1272 if (ret)
1273 return ret;
1274 if (value)
1275 value--;
1276 }
1277
Azael Avalosa39f46d2014-11-24 19:29:36 -07001278 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1279 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1280 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001281}
1282
1283static int set_lcd_status(struct backlight_device *bd)
1284{
Seth Forshee135740d2011-09-20 16:55:49 -05001285 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Seth Forshee62cce752012-04-19 11:23:50 -05001286 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001287}
1288
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001289static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1290 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Al Virod9dda782013-03-31 18:16:14 -04001292 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001293 char cmd[42];
1294 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001296 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001297 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001299 len = min(count, sizeof(cmd) - 1);
1300 if (copy_from_user(cmd, buf, len))
1301 return -EFAULT;
1302 cmd[len] = '\0';
1303
1304 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001305 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001306 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001307 if (ret == 0)
1308 ret = count;
1309 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001310 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001311 }
Holger Machtc9263552006-10-20 14:30:29 -07001312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001315static const struct file_operations lcd_proc_fops = {
1316 .owner = THIS_MODULE,
1317 .open = lcd_proc_open,
1318 .read = seq_read,
1319 .llseek = seq_lseek,
1320 .release = single_release,
1321 .write = lcd_proc_write,
1322};
1323
Seth Forshee36d03f92011-09-20 16:55:53 -05001324static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1325{
1326 u32 hci_result;
1327
Azael Avalos893f3f62014-09-29 20:40:09 -06001328 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001329 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001330}
1331
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001332static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Seth Forshee135740d2011-09-20 16:55:49 -05001334 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001336 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Seth Forshee36d03f92011-09-20 16:55:53 -05001338 ret = get_video_status(dev, &value);
1339 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1341 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001342 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001343 seq_printf(m, "lcd_out: %d\n", is_lcd);
1344 seq_printf(m, "crt_out: %d\n", is_crt);
1345 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
Seth Forshee36d03f92011-09-20 16:55:53 -05001348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001351static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Al Virod9dda782013-03-31 18:16:14 -04001353 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001354}
1355
1356static ssize_t video_proc_write(struct file *file, const char __user *buf,
1357 size_t count, loff_t *pos)
1358{
Al Virod9dda782013-03-31 18:16:14 -04001359 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001360 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001361 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int value;
1363 int remain = count;
1364 int lcd_out = -1;
1365 int crt_out = -1;
1366 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001367 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001369 cmd = kmalloc(count + 1, GFP_KERNEL);
1370 if (!cmd)
1371 return -ENOMEM;
1372 if (copy_from_user(cmd, buf, count)) {
1373 kfree(cmd);
1374 return -EFAULT;
1375 }
1376 cmd[count] = '\0';
1377
1378 buffer = cmd;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* scan expression. Multiple expressions may be delimited with ;
1381 *
1382 * NOTE: to keep scanning simple, invalid fields are ignored
1383 */
1384 while (remain) {
1385 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1386 lcd_out = value & 1;
1387 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1388 crt_out = value & 1;
1389 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1390 tv_out = value & 1;
1391 /* advance to one character past the next ; */
1392 do {
1393 ++buffer;
1394 --remain;
1395 }
Len Brown4be44fc2005-08-05 00:44:28 -04001396 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001399 kfree(cmd);
1400
Seth Forshee36d03f92011-09-20 16:55:53 -05001401 ret = get_video_status(dev, &video_out);
1402 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001403 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (lcd_out != -1)
1405 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1406 if (crt_out != -1)
1407 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1408 if (tv_out != -1)
1409 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1410 /* To avoid unnecessary video disruption, only write the new
1411 * video setting if something changed. */
1412 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001413 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001416 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001419static const struct file_operations video_proc_fops = {
1420 .owner = THIS_MODULE,
1421 .open = video_proc_open,
1422 .read = seq_read,
1423 .llseek = seq_lseek,
1424 .release = single_release,
1425 .write = video_proc_write,
1426};
1427
Seth Forshee36d03f92011-09-20 16:55:53 -05001428static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1429{
1430 u32 hci_result;
1431
Azael Avalos893f3f62014-09-29 20:40:09 -06001432 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001433 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001434}
1435
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001436static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Seth Forshee135740d2011-09-20 16:55:49 -05001438 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001439 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 u32 value;
1441
Seth Forshee36d03f92011-09-20 16:55:53 -05001442 ret = get_fan_status(dev, &value);
1443 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001444 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001445 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Seth Forshee36d03f92011-09-20 16:55:53 -05001448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001451static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Al Virod9dda782013-03-31 18:16:14 -04001453 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001454}
1455
1456static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1457 size_t count, loff_t *pos)
1458{
Al Virod9dda782013-03-31 18:16:14 -04001459 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001460 char cmd[42];
1461 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int value;
1463 u32 hci_result;
1464
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001465 len = min(count, sizeof(cmd) - 1);
1466 if (copy_from_user(cmd, buf, len))
1467 return -EFAULT;
1468 cmd[len] = '\0';
1469
1470 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001471 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001472 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001473 if (hci_result != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001474 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else
Seth Forshee135740d2011-09-20 16:55:49 -05001476 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 } else {
1478 return -EINVAL;
1479 }
1480
1481 return count;
1482}
1483
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001484static const struct file_operations fan_proc_fops = {
1485 .owner = THIS_MODULE,
1486 .open = fan_proc_open,
1487 .read = seq_read,
1488 .llseek = seq_lseek,
1489 .release = single_release,
1490 .write = fan_proc_write,
1491};
1492
1493static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Seth Forshee135740d2011-09-20 16:55:49 -05001495 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 u32 hci_result;
1497 u32 value;
1498
Seth Forshee11948b92011-11-16 17:37:45 -06001499 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001500 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001501 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001502 dev->key_event_valid = 1;
1503 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001504 } else if (hci_result == TOS_FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001506 } else if (hci_result == TOS_NOT_SUPPORTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* This is a workaround for an unresolved issue on
1508 * some machines where system events sporadically
1509 * become disabled. */
Azael Avalos893f3f62014-09-29 20:40:09 -06001510 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001511 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001513 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001514 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516 }
1517
Seth Forshee135740d2011-09-20 16:55:49 -05001518 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1519 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001523static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Al Virod9dda782013-03-31 18:16:14 -04001525 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001526}
1527
1528static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1529 size_t count, loff_t *pos)
1530{
Al Virod9dda782013-03-31 18:16:14 -04001531 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001532 char cmd[42];
1533 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int value;
1535
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001536 len = min(count, sizeof(cmd) - 1);
1537 if (copy_from_user(cmd, buf, len))
1538 return -EFAULT;
1539 cmd[len] = '\0';
1540
1541 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -05001542 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 } else {
1544 return -EINVAL;
1545 }
1546
1547 return count;
1548}
1549
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001550static const struct file_operations keys_proc_fops = {
1551 .owner = THIS_MODULE,
1552 .open = keys_proc_open,
1553 .read = seq_read,
1554 .llseek = seq_lseek,
1555 .release = single_release,
1556 .write = keys_proc_write,
1557};
1558
1559static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001561 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1562 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001566static int version_proc_open(struct inode *inode, struct file *file)
1567{
Al Virod9dda782013-03-31 18:16:14 -04001568 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001569}
1570
1571static const struct file_operations version_proc_fops = {
1572 .owner = THIS_MODULE,
1573 .open = version_proc_open,
1574 .read = seq_read,
1575 .llseek = seq_lseek,
1576 .release = single_release,
1577};
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579/* proc and module init
1580 */
1581
1582#define PROC_TOSHIBA "toshiba"
1583
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001584static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Seth Forshee36d03f92011-09-20 16:55:53 -05001586 if (dev->backlight_dev)
1587 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1588 &lcd_proc_fops, dev);
1589 if (dev->video_supported)
1590 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1591 &video_proc_fops, dev);
1592 if (dev->fan_supported)
1593 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1594 &fan_proc_fops, dev);
1595 if (dev->hotkey_dev)
1596 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1597 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001598 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1599 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Seth Forshee36d03f92011-09-20 16:55:53 -05001602static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Seth Forshee36d03f92011-09-20 16:55:53 -05001604 if (dev->backlight_dev)
1605 remove_proc_entry("lcd", toshiba_proc_dir);
1606 if (dev->video_supported)
1607 remove_proc_entry("video", toshiba_proc_dir);
1608 if (dev->fan_supported)
1609 remove_proc_entry("fan", toshiba_proc_dir);
1610 if (dev->hotkey_dev)
1611 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001612 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Lionel Debrouxacc24722010-11-16 14:14:02 +01001615static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001616 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001617 .get_brightness = get_lcd_brightness,
1618 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001619};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001620
Azael Avalos360f0f32014-03-25 20:38:31 -06001621/*
1622 * Sysfs files
1623 */
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001624static ssize_t toshiba_version_show(struct device *dev,
1625 struct device_attribute *attr, char *buf);
Azael Avalos94477d42015-02-10 21:09:17 -07001626static ssize_t toshiba_fan_store(struct device *dev,
1627 struct device_attribute *attr,
1628 const char *buf, size_t count);
1629static ssize_t toshiba_fan_show(struct device *dev,
1630 struct device_attribute *attr, char *buf);
Azael Avalos93f8c162014-09-12 18:50:36 -06001631static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1632 struct device_attribute *attr,
1633 const char *buf, size_t count);
1634static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1635 struct device_attribute *attr,
1636 char *buf);
1637static ssize_t toshiba_kbd_type_show(struct device *dev,
1638 struct device_attribute *attr,
1639 char *buf);
1640static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1641 struct device_attribute *attr,
1642 char *buf);
1643static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1644 struct device_attribute *attr,
1645 const char *buf, size_t count);
1646static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1647 struct device_attribute *attr,
1648 char *buf);
1649static ssize_t toshiba_touchpad_store(struct device *dev,
1650 struct device_attribute *attr,
1651 const char *buf, size_t count);
1652static ssize_t toshiba_touchpad_show(struct device *dev,
1653 struct device_attribute *attr,
1654 char *buf);
1655static ssize_t toshiba_position_show(struct device *dev,
1656 struct device_attribute *attr,
1657 char *buf);
Azael Avalose26ffe52015-01-18 18:30:22 -07001658static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1659 struct device_attribute *attr,
1660 char *buf);
1661static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1662 struct device_attribute *attr,
1663 const char *buf, size_t count);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001664static ssize_t sleep_functions_on_battery_show(struct device *dev,
1665 struct device_attribute *attr,
1666 char *buf);
1667static ssize_t sleep_functions_on_battery_store(struct device *dev,
1668 struct device_attribute *attr,
1669 const char *buf, size_t count);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001670static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
1671 struct device_attribute *attr,
1672 char *buf);
1673static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
1674 struct device_attribute *attr,
1675 const char *buf, size_t count);
Azael Avalos172ce0a2015-01-18 18:30:25 -07001676static ssize_t toshiba_usb_sleep_music_show(struct device *dev,
1677 struct device_attribute *attr,
1678 char *buf);
1679static ssize_t toshiba_usb_sleep_music_store(struct device *dev,
1680 struct device_attribute *attr,
1681 const char *buf, size_t count);
Azael Avalosbae84192015-02-10 21:09:18 -07001682static ssize_t toshiba_kbd_function_keys_show(struct device *dev,
1683 struct device_attribute *attr,
1684 char *buf);
1685static ssize_t toshiba_kbd_function_keys_store(struct device *dev,
1686 struct device_attribute *attr,
1687 const char *buf, size_t count);
Azael Avalos35d53ce2015-02-10 21:09:19 -07001688static ssize_t toshiba_panel_power_on_show(struct device *dev,
1689 struct device_attribute *attr,
1690 char *buf);
1691static ssize_t toshiba_panel_power_on_store(struct device *dev,
1692 struct device_attribute *attr,
1693 const char *buf, size_t count);
Azael Avalos93f8c162014-09-12 18:50:36 -06001694
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001695static DEVICE_ATTR(version, S_IRUGO, toshiba_version_show, NULL);
Azael Avalos94477d42015-02-10 21:09:17 -07001696static DEVICE_ATTR(fan, S_IRUGO | S_IWUSR,
1697 toshiba_fan_show, toshiba_fan_store);
Azael Avalos93f8c162014-09-12 18:50:36 -06001698static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1699 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1700static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1701static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1702 toshiba_available_kbd_modes_show, NULL);
1703static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1704 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1705static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1706 toshiba_touchpad_show, toshiba_touchpad_store);
1707static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
Azael Avalose26ffe52015-01-18 18:30:22 -07001708static DEVICE_ATTR(usb_sleep_charge, S_IRUGO | S_IWUSR,
1709 toshiba_usb_sleep_charge_show,
1710 toshiba_usb_sleep_charge_store);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001711static DEVICE_ATTR(sleep_functions_on_battery, S_IRUGO | S_IWUSR,
1712 sleep_functions_on_battery_show,
1713 sleep_functions_on_battery_store);
Azael Avalosbb3fe012015-01-18 18:30:24 -07001714static DEVICE_ATTR(usb_rapid_charge, S_IRUGO | S_IWUSR,
1715 toshiba_usb_rapid_charge_show,
1716 toshiba_usb_rapid_charge_store);
Azael Avalos172ce0a2015-01-18 18:30:25 -07001717static DEVICE_ATTR(usb_sleep_music, S_IRUGO | S_IWUSR,
1718 toshiba_usb_sleep_music_show,
1719 toshiba_usb_sleep_music_store);
Azael Avalosbae84192015-02-10 21:09:18 -07001720static DEVICE_ATTR(kbd_function_keys, S_IRUGO | S_IWUSR,
1721 toshiba_kbd_function_keys_show,
1722 toshiba_kbd_function_keys_store);
Azael Avalos35d53ce2015-02-10 21:09:19 -07001723static DEVICE_ATTR(panel_power_on, S_IRUGO | S_IWUSR,
1724 toshiba_panel_power_on_show,
1725 toshiba_panel_power_on_store);
Azael Avalos93f8c162014-09-12 18:50:36 -06001726
1727static struct attribute *toshiba_attributes[] = {
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001728 &dev_attr_version.attr,
Azael Avalos94477d42015-02-10 21:09:17 -07001729 &dev_attr_fan.attr,
Azael Avalos93f8c162014-09-12 18:50:36 -06001730 &dev_attr_kbd_backlight_mode.attr,
1731 &dev_attr_kbd_type.attr,
1732 &dev_attr_available_kbd_modes.attr,
1733 &dev_attr_kbd_backlight_timeout.attr,
1734 &dev_attr_touchpad.attr,
1735 &dev_attr_position.attr,
Azael Avalose26ffe52015-01-18 18:30:22 -07001736 &dev_attr_usb_sleep_charge.attr,
Azael Avalos182bcaa2015-01-18 18:30:23 -07001737 &dev_attr_sleep_functions_on_battery.attr,
Azael Avalosbb3fe012015-01-18 18:30:24 -07001738 &dev_attr_usb_rapid_charge.attr,
Azael Avalos172ce0a2015-01-18 18:30:25 -07001739 &dev_attr_usb_sleep_music.attr,
Azael Avalosbae84192015-02-10 21:09:18 -07001740 &dev_attr_kbd_function_keys.attr,
Azael Avalos35d53ce2015-02-10 21:09:19 -07001741 &dev_attr_panel_power_on.attr,
Azael Avalos93f8c162014-09-12 18:50:36 -06001742 NULL,
1743};
1744
1745static umode_t toshiba_sysfs_is_visible(struct kobject *,
1746 struct attribute *, int);
1747
1748static struct attribute_group toshiba_attr_group = {
1749 .is_visible = toshiba_sysfs_is_visible,
1750 .attrs = toshiba_attributes,
1751};
Azael Avalos360f0f32014-03-25 20:38:31 -06001752
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001753static ssize_t toshiba_version_show(struct device *dev,
1754 struct device_attribute *attr, char *buf)
1755{
1756 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1757}
1758
Azael Avalos94477d42015-02-10 21:09:17 -07001759static ssize_t toshiba_fan_store(struct device *dev,
1760 struct device_attribute *attr,
1761 const char *buf, size_t count)
1762{
1763 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1764 u32 result;
1765 int state;
1766 int ret;
1767
1768 ret = kstrtoint(buf, 0, &state);
1769 if (ret)
1770 return ret;
1771
1772 if (state != 0 && state != 1)
1773 return -EINVAL;
1774
1775 result = hci_write1(toshiba, HCI_FAN, state);
1776 if (result == TOS_FAILURE)
1777 return -EIO;
1778 else if (result == TOS_NOT_SUPPORTED)
1779 return -ENODEV;
1780
1781 return count;
1782}
1783
1784static ssize_t toshiba_fan_show(struct device *dev,
1785 struct device_attribute *attr, char *buf)
1786{
1787 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1788 u32 value;
1789 int ret;
1790
1791 ret = get_fan_status(toshiba, &value);
1792 if (ret)
1793 return ret;
1794
1795 return sprintf(buf, "%d\n", value);
1796}
1797
Azael Avalos360f0f32014-03-25 20:38:31 -06001798static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1799 struct device_attribute *attr,
1800 const char *buf, size_t count)
1801{
1802 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001803 int mode;
1804 int time;
1805 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001806
Dan Carpenteraeaac092014-09-03 14:44:37 +03001807
1808 ret = kstrtoint(buf, 0, &mode);
1809 if (ret)
1810 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001811
1812 /* Check for supported modes depending on keyboard backlight type */
1813 if (toshiba->kbd_type == 1) {
1814 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1815 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1816 return -EINVAL;
1817 } else if (toshiba->kbd_type == 2) {
1818 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1819 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1820 mode != SCI_KBD_MODE_OFF)
1821 return -EINVAL;
1822 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001823
1824 /* Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001825 * Auto - KBD backlight turns off automatically in given time
1826 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001827 * ON - KBD backlight is always on
1828 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001829 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001830
1831 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001832 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001833 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001834 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001835
1836 /* OR the "base time" to the actual method format */
1837 if (toshiba->kbd_type == 1) {
1838 /* Type 1 requires the current mode */
1839 time |= toshiba->kbd_mode;
1840 } else if (toshiba->kbd_type == 2) {
1841 /* Type 2 requires the desired mode */
1842 time |= mode;
1843 }
1844
Dan Carpenteraeaac092014-09-03 14:44:37 +03001845 ret = toshiba_kbd_illum_status_set(toshiba, time);
1846 if (ret)
1847 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001848
Azael Avalos360f0f32014-03-25 20:38:31 -06001849 toshiba->kbd_mode = mode;
1850 }
1851
1852 return count;
1853}
1854
1855static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1856 struct device_attribute *attr,
1857 char *buf)
1858{
1859 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1860 u32 time;
1861
1862 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1863 return -EIO;
1864
Azael Avalos93f8c162014-09-12 18:50:36 -06001865 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1866}
1867
1868static ssize_t toshiba_kbd_type_show(struct device *dev,
1869 struct device_attribute *attr,
1870 char *buf)
1871{
1872 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1873
1874 return sprintf(buf, "%d\n", toshiba->kbd_type);
1875}
1876
1877static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1878 struct device_attribute *attr,
1879 char *buf)
1880{
1881 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1882
1883 if (toshiba->kbd_type == 1)
1884 return sprintf(buf, "%x %x\n",
1885 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1886
1887 return sprintf(buf, "%x %x %x\n",
1888 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001889}
1890
1891static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1892 struct device_attribute *attr,
1893 const char *buf, size_t count)
1894{
1895 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001896 int time;
1897 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001898
Azael Avaloseabde0f2014-10-04 12:02:21 -06001899 ret = kstrtoint(buf, 0, &time);
1900 if (ret)
1901 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001902
Azael Avaloseabde0f2014-10-04 12:02:21 -06001903 /* Check for supported values depending on kbd_type */
1904 if (toshiba->kbd_type == 1) {
1905 if (time < 0 || time > 60)
1906 return -EINVAL;
1907 } else if (toshiba->kbd_type == 2) {
1908 if (time < 1 || time > 60)
1909 return -EINVAL;
1910 }
1911
1912 /* Set the Keyboard Backlight Timeout */
1913
1914 /* Only make a change if the actual timeout has changed */
1915 if (toshiba->kbd_time != time) {
1916 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001917 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001918 /* OR the "base time" to the actual method format */
1919 if (toshiba->kbd_type == 1)
1920 time |= SCI_KBD_MODE_FNZ;
1921 else if (toshiba->kbd_type == 2)
1922 time |= SCI_KBD_MODE_AUTO;
1923
1924 ret = toshiba_kbd_illum_status_set(toshiba, time);
1925 if (ret)
1926 return ret;
1927
Azael Avalos360f0f32014-03-25 20:38:31 -06001928 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1929 }
1930
1931 return count;
1932}
1933
1934static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1935 struct device_attribute *attr,
1936 char *buf)
1937{
1938 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1939 u32 time;
1940
1941 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1942 return -EIO;
1943
1944 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1945}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001946
Azael Avalos9d8658a2014-03-25 20:38:32 -06001947static ssize_t toshiba_touchpad_store(struct device *dev,
1948 struct device_attribute *attr,
1949 const char *buf, size_t count)
1950{
1951 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1952 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001953 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001954
1955 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001956 ret = kstrtoint(buf, 0, &state);
1957 if (ret)
1958 return ret;
1959 if (state != 0 && state != 1)
1960 return -EINVAL;
1961
1962 ret = toshiba_touchpad_set(toshiba, state);
1963 if (ret)
1964 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001965
1966 return count;
1967}
1968
1969static ssize_t toshiba_touchpad_show(struct device *dev,
1970 struct device_attribute *attr, char *buf)
1971{
1972 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1973 u32 state;
1974 int ret;
1975
1976 ret = toshiba_touchpad_get(toshiba, &state);
1977 if (ret < 0)
1978 return ret;
1979
1980 return sprintf(buf, "%i\n", state);
1981}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001982
Azael Avalos5a2813e2014-03-25 20:38:34 -06001983static ssize_t toshiba_position_show(struct device *dev,
1984 struct device_attribute *attr, char *buf)
1985{
1986 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1987 u32 xyval, zval, tmp;
1988 u16 x, y, z;
1989 int ret;
1990
1991 xyval = zval = 0;
1992 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1993 if (ret < 0)
1994 return ret;
1995
1996 x = xyval & HCI_ACCEL_MASK;
1997 tmp = xyval >> HCI_MISC_SHIFT;
1998 y = tmp & HCI_ACCEL_MASK;
1999 z = zval & HCI_ACCEL_MASK;
2000
2001 return sprintf(buf, "%d %d %d\n", x, y, z);
2002}
Azael Avalos360f0f32014-03-25 20:38:31 -06002003
Azael Avalose26ffe52015-01-18 18:30:22 -07002004static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
2005 struct device_attribute *attr,
2006 char *buf)
2007{
2008 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2009 u32 mode;
2010 int ret;
2011
2012 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2013 if (ret < 0)
2014 return ret;
2015
2016 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2017}
2018
2019static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
2020 struct device_attribute *attr,
2021 const char *buf, size_t count)
2022{
2023 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2024 u32 mode;
2025 int state;
2026 int ret;
2027
2028 ret = kstrtoint(buf, 0, &state);
2029 if (ret)
2030 return ret;
2031 /* Check for supported values, where:
2032 * 0 - Disabled
2033 * 1 - Alternate (Non USB conformant devices that require more power)
2034 * 2 - Auto (USB conformant devices)
2035 */
2036 if (state != 0 && state != 1 && state != 2)
2037 return -EINVAL;
2038
2039 /* Set the USB charging mode to internal value */
2040 if (state == 0)
2041 mode = SCI_USB_CHARGE_DISABLED;
2042 else if (state == 1)
2043 mode = SCI_USB_CHARGE_ALTERNATE;
2044 else if (state == 2)
2045 mode = SCI_USB_CHARGE_AUTO;
2046
2047 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2048 if (ret)
2049 return ret;
2050
2051 return count;
2052}
2053
Azael Avalos182bcaa2015-01-18 18:30:23 -07002054static ssize_t sleep_functions_on_battery_show(struct device *dev,
2055 struct device_attribute *attr,
2056 char *buf)
2057{
2058 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2059 u32 state;
2060 int bat_lvl;
2061 int status;
2062 int ret;
2063 int tmp;
2064
2065 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2066 if (ret < 0)
2067 return ret;
2068
2069 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2070 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2071 status = (tmp == 0x4) ? 1 : 0;
2072 /* Determine the battery level set */
2073 bat_lvl = state >> HCI_MISC_SHIFT;
2074
2075 return sprintf(buf, "%d %d\n", status, bat_lvl);
2076}
2077
2078static ssize_t sleep_functions_on_battery_store(struct device *dev,
2079 struct device_attribute *attr,
2080 const char *buf, size_t count)
2081{
2082 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2083 u32 status;
2084 int value;
2085 int ret;
2086 int tmp;
2087
2088 ret = kstrtoint(buf, 0, &value);
2089 if (ret)
2090 return ret;
2091
2092 /* Set the status of the function:
2093 * 0 - Disabled
2094 * 1-100 - Enabled
2095 */
2096 if (value < 0 || value > 100)
2097 return -EINVAL;
2098
2099 if (value == 0) {
2100 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2101 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2102 } else {
2103 tmp = value << HCI_MISC_SHIFT;
2104 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2105 }
2106 ret = toshiba_sleep_functions_status_set(toshiba, status);
2107 if (ret < 0)
2108 return ret;
2109
2110 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2111
2112 return count;
2113}
2114
Azael Avalosbb3fe012015-01-18 18:30:24 -07002115static ssize_t toshiba_usb_rapid_charge_show(struct device *dev,
2116 struct device_attribute *attr,
2117 char *buf)
2118{
2119 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2120 u32 state;
2121 int ret;
2122
2123 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2124 if (ret < 0)
2125 return ret;
2126
2127 return sprintf(buf, "%d\n", state);
2128}
2129
2130static ssize_t toshiba_usb_rapid_charge_store(struct device *dev,
2131 struct device_attribute *attr,
2132 const char *buf, size_t count)
2133{
2134 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135 int state;
2136 int ret;
2137
2138 ret = kstrtoint(buf, 0, &state);
2139 if (ret)
2140 return ret;
2141 if (state != 0 && state != 1)
2142 return -EINVAL;
2143
2144 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2145 if (ret)
2146 return ret;
2147
2148 return count;
2149}
2150
Azael Avalos172ce0a2015-01-18 18:30:25 -07002151static ssize_t toshiba_usb_sleep_music_show(struct device *dev,
2152 struct device_attribute *attr,
2153 char *buf)
2154{
2155 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2156 u32 state;
2157 int ret;
2158
2159 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2160 if (ret < 0)
2161 return ret;
2162
2163 return sprintf(buf, "%d\n", state);
2164}
2165
2166static ssize_t toshiba_usb_sleep_music_store(struct device *dev,
2167 struct device_attribute *attr,
2168 const char *buf, size_t count)
2169{
2170 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2171 int state;
2172 int ret;
2173
2174 ret = kstrtoint(buf, 0, &state);
2175 if (ret)
2176 return ret;
2177 if (state != 0 && state != 1)
2178 return -EINVAL;
2179
2180 ret = toshiba_usb_sleep_music_set(toshiba, state);
2181 if (ret)
2182 return ret;
2183
2184 return count;
2185}
2186
Azael Avalosbae84192015-02-10 21:09:18 -07002187static ssize_t toshiba_kbd_function_keys_show(struct device *dev,
2188 struct device_attribute *attr,
2189 char *buf)
2190{
2191 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2192 int mode;
2193 int ret;
2194
2195 ret = toshiba_function_keys_get(toshiba, &mode);
2196 if (ret < 0)
2197 return ret;
2198
2199 return sprintf(buf, "%d\n", mode);
2200}
2201
2202static ssize_t toshiba_kbd_function_keys_store(struct device *dev,
2203 struct device_attribute *attr,
2204 const char *buf, size_t count)
2205{
2206 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2207 int mode;
2208 int ret;
2209
2210 ret = kstrtoint(buf, 0, &mode);
2211 if (ret)
2212 return ret;
2213 /* Check for the function keys mode where:
2214 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2215 * 1 - Special functions (Opposite of the above setting)
2216 */
2217 if (mode != 0 && mode != 1)
2218 return -EINVAL;
2219
2220 ret = toshiba_function_keys_set(toshiba, mode);
2221 if (ret)
2222 return ret;
2223
2224 pr_info("Reboot for changes to KBD Function Keys to take effect");
2225
2226 return count;
2227}
2228
Azael Avalos35d53ce2015-02-10 21:09:19 -07002229static ssize_t toshiba_panel_power_on_show(struct device *dev,
2230 struct device_attribute *attr,
2231 char *buf)
2232{
2233 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2234 u32 state;
2235 int ret;
2236
2237 ret = toshiba_panel_power_on_get(toshiba, &state);
2238 if (ret < 0)
2239 return ret;
2240
2241 return sprintf(buf, "%d\n", state);
2242}
2243
2244static ssize_t toshiba_panel_power_on_store(struct device *dev,
2245 struct device_attribute *attr,
2246 const char *buf, size_t count)
2247{
2248 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2249 int state;
2250 int ret;
2251
2252 ret = kstrtoint(buf, 0, &state);
2253 if (ret)
2254 return ret;
2255 if (state != 0 && state != 1)
2256 return -EINVAL;
2257
2258 ret = toshiba_panel_power_on_set(toshiba, state);
2259 if (ret)
2260 return ret;
2261
2262 pr_info("Reboot for changes to Panel Power ON to take effect");
2263
2264 return count;
2265}
2266
Azael Avalos360f0f32014-03-25 20:38:31 -06002267static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2268 struct attribute *attr, int idx)
2269{
2270 struct device *dev = container_of(kobj, struct device, kobj);
2271 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2272 bool exists = true;
2273
Azael Avalos94477d42015-02-10 21:09:17 -07002274 if (attr == &dev_attr_fan.attr)
2275 exists = (drv->fan_supported) ? true : false;
2276 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002277 exists = (drv->kbd_illum_supported) ? true : false;
2278 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2279 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002280 else if (attr == &dev_attr_touchpad.attr)
2281 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002282 else if (attr == &dev_attr_position.attr)
2283 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002284 else if (attr == &dev_attr_usb_sleep_charge.attr)
2285 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002286 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2287 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002288 else if (attr == &dev_attr_usb_rapid_charge.attr)
2289 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002290 else if (attr == &dev_attr_usb_sleep_music.attr)
2291 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002292 else if (attr == &dev_attr_kbd_function_keys.attr)
2293 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002294 else if (attr == &dev_attr_panel_power_on.attr)
2295 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002296
2297 return exists ? attr->mode : 0;
2298}
2299
Azael Avalos1f28f292014-12-04 20:22:45 -07002300/*
2301 * Hotkeys
2302 */
2303static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2304{
2305 acpi_status status;
2306 u32 result;
2307
2308 status = acpi_evaluate_object(dev->acpi_dev->handle,
2309 "ENAB", NULL, NULL);
2310 if (ACPI_FAILURE(status))
2311 return -ENODEV;
2312
2313 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2314 if (result == TOS_FAILURE)
2315 return -EIO;
2316 else if (result == TOS_NOT_SUPPORTED)
2317 return -ENODEV;
2318
2319 return 0;
2320}
2321
Seth Forshee29cd2932012-01-18 13:44:09 -06002322static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2323 struct serio *port)
2324{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002325 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002326 return false;
2327
2328 if (unlikely(data == 0xe0))
2329 return false;
2330
2331 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2332 schedule_work(&toshiba_acpi->hotkey_work);
2333 return true;
2334 }
2335
2336 return false;
2337}
2338
2339static void toshiba_acpi_hotkey_work(struct work_struct *work)
2340{
2341 acpi_handle ec_handle = ec_get_handle();
2342 acpi_status status;
2343
2344 if (!ec_handle)
2345 return;
2346
2347 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2348 if (ACPI_FAILURE(status))
2349 pr_err("ACPI NTFY method execution failed\n");
2350}
2351
2352/*
2353 * Returns hotkey scancode, or < 0 on failure.
2354 */
2355static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2356{
Zhang Rui74facaf2013-09-03 08:32:15 +08002357 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002358 acpi_status status;
2359
Zhang Rui74facaf2013-09-03 08:32:15 +08002360 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2361 NULL, &value);
2362 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002363 pr_err("ACPI INFO method execution failed\n");
2364 return -EIO;
2365 }
2366
Zhang Rui74facaf2013-09-03 08:32:15 +08002367 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002368}
2369
2370static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2371 int scancode)
2372{
2373 if (scancode == 0x100)
2374 return;
2375
2376 /* act on key press; ignore key release */
2377 if (scancode & 0x80)
2378 return;
2379
2380 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2381 pr_info("Unknown key %x\n", scancode);
2382}
2383
Azael Avalos71454d72014-12-04 20:22:46 -07002384static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2385{
2386 u32 hci_result, value;
2387 int retries = 3;
2388 int scancode;
2389
2390 if (dev->info_supported) {
2391 scancode = toshiba_acpi_query_hotkey(dev);
2392 if (scancode < 0)
2393 pr_err("Failed to query hotkey event\n");
2394 else if (scancode != 0)
2395 toshiba_acpi_report_hotkey(dev, scancode);
2396 } else if (dev->system_event_supported) {
2397 do {
2398 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2399 switch (hci_result) {
2400 case TOS_SUCCESS:
2401 toshiba_acpi_report_hotkey(dev, (int)value);
2402 break;
2403 case TOS_NOT_SUPPORTED:
2404 /*
2405 * This is a workaround for an unresolved
2406 * issue on some machines where system events
2407 * sporadically become disabled.
2408 */
2409 hci_result =
2410 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2411 pr_notice("Re-enabled hotkeys\n");
2412 /* fall through */
2413 default:
2414 retries--;
2415 break;
2416 }
2417 } while (retries && hci_result != TOS_FIFO_EMPTY);
2418 }
2419}
2420
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002421static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002422{
Zhang Ruie2e19602013-09-03 08:32:06 +08002423 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05002424 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002425 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002426 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05002427
Seth Forshee135740d2011-09-20 16:55:49 -05002428 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002429 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002430 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002431
2432 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002433 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002434 dev->hotkey_dev->id.bustype = BUS_HOST;
2435
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002436 if (dmi_check_system(toshiba_alt_keymap_dmi))
2437 keymap = toshiba_acpi_alt_keymap;
2438 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002439 if (error)
2440 goto err_free_dev;
2441
Seth Forshee29cd2932012-01-18 13:44:09 -06002442 /*
2443 * For some machines the SCI responsible for providing hotkey
2444 * notification doesn't fire. We can trigger the notification
2445 * whenever the Fn key is pressed using the NTFY method, if
2446 * supported, so if it's present set up an i8042 key filter
2447 * for this purpose.
2448 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002449 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002450 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002451 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2452
2453 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2454 if (error) {
2455 pr_err("Error installing key filter\n");
2456 goto err_free_keymap;
2457 }
2458
2459 dev->ntfy_supported = 1;
2460 }
2461
2462 /*
2463 * Determine hotkey query interface. Prefer using the INFO
2464 * method when it is available.
2465 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002466 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002467 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002468 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002469 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002470 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002471 dev->system_event_supported = 1;
2472 }
2473
2474 if (!dev->info_supported && !dev->system_event_supported) {
2475 pr_warn("No hotkey query interface found\n");
2476 goto err_remove_filter;
2477 }
2478
Azael Avalos1f28f292014-12-04 20:22:45 -07002479 error = toshiba_acpi_enable_hotkeys(dev);
2480 if (error) {
Seth Forshee135740d2011-09-20 16:55:49 -05002481 pr_info("Unable to enable hotkeys\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002482 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002483 }
2484
2485 error = input_register_device(dev->hotkey_dev);
2486 if (error) {
2487 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002488 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002489 }
2490
2491 return 0;
2492
Seth Forshee29cd2932012-01-18 13:44:09 -06002493 err_remove_filter:
2494 if (dev->ntfy_supported)
2495 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002496 err_free_keymap:
2497 sparse_keymap_free(dev->hotkey_dev);
2498 err_free_dev:
2499 input_free_device(dev->hotkey_dev);
2500 dev->hotkey_dev = NULL;
2501 return error;
2502}
2503
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002504static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002505{
2506 struct backlight_properties props;
2507 int brightness;
2508 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002509 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002510
2511 /*
2512 * Some machines don't support the backlight methods at all, and
2513 * others support it read-only. Either of these is pretty useless,
2514 * so only register the backlight device if the backlight method
2515 * supports both reads and writes.
2516 */
2517 brightness = __get_lcd_brightness(dev);
2518 if (brightness < 0)
2519 return 0;
2520 ret = set_lcd_brightness(dev, brightness);
2521 if (ret) {
2522 pr_debug("Backlight method is read-only, disabling backlight support\n");
2523 return 0;
2524 }
2525
Akio Idehara121b7b02012-04-06 01:46:43 +09002526 /* Determine whether or not BIOS supports transflective backlight */
2527 ret = get_tr_backlight_status(dev, &enabled);
2528 dev->tr_backlight_supported = !ret;
2529
Matthew Garrett53039f22012-06-01 11:02:36 -04002530 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002531 props.type = BACKLIGHT_PLATFORM;
2532 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002533
Akio Idehara121b7b02012-04-06 01:46:43 +09002534 /* adding an extra level and having 0 change to transflective mode */
2535 if (dev->tr_backlight_supported)
2536 props.max_brightness++;
2537
Seth Forshee62cce752012-04-19 11:23:50 -05002538 dev->backlight_dev = backlight_device_register("toshiba",
2539 &dev->acpi_dev->dev,
2540 dev,
2541 &toshiba_backlight_data,
2542 &props);
2543 if (IS_ERR(dev->backlight_dev)) {
2544 ret = PTR_ERR(dev->backlight_dev);
2545 pr_err("Could not register toshiba backlight device\n");
2546 dev->backlight_dev = NULL;
2547 return ret;
2548 }
2549
2550 dev->backlight_dev->props.brightness = brightness;
2551 return 0;
2552}
2553
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002554static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002555{
2556 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2557
Seth Forshee36d03f92011-09-20 16:55:53 -05002558 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002559
Azael Avalos360f0f32014-03-25 20:38:31 -06002560 if (dev->sysfs_created)
2561 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2562 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002563
Seth Forshee29cd2932012-01-18 13:44:09 -06002564 if (dev->ntfy_supported) {
2565 i8042_remove_filter(toshiba_acpi_i8042_filter);
2566 cancel_work_sync(&dev->hotkey_work);
2567 }
2568
Seth Forshee135740d2011-09-20 16:55:49 -05002569 if (dev->hotkey_dev) {
2570 input_unregister_device(dev->hotkey_dev);
2571 sparse_keymap_free(dev->hotkey_dev);
2572 }
2573
2574 if (dev->bt_rfk) {
2575 rfkill_unregister(dev->bt_rfk);
2576 rfkill_destroy(dev->bt_rfk);
2577 }
2578
Markus Elfring00981812014-11-24 20:30:29 +01002579 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002580
Seth Forshee36d03f92011-09-20 16:55:53 -05002581 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002582 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002583
Azael Avalos360f0f32014-03-25 20:38:31 -06002584 if (dev->kbd_led_registered)
2585 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002586
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002587 if (dev->eco_supported)
2588 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002589
Seth Forshee29cd2932012-01-18 13:44:09 -06002590 if (toshiba_acpi)
2591 toshiba_acpi = NULL;
2592
Seth Forshee135740d2011-09-20 16:55:49 -05002593 kfree(dev);
2594
2595 return 0;
2596}
2597
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002598static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002599{
Zhang Ruie2e19602013-09-03 08:32:06 +08002600 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002601 return "GHCI";
2602
Zhang Ruie2e19602013-09-03 08:32:06 +08002603 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002604 return "SPFC";
2605
2606 return NULL;
2607}
2608
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002609static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002610{
2611 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002612 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002613 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002614 bool bt_present;
2615 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002616
Seth Forshee29cd2932012-01-18 13:44:09 -06002617 if (toshiba_acpi)
2618 return -EBUSY;
2619
Seth Forshee135740d2011-09-20 16:55:49 -05002620 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2621 TOSHIBA_ACPI_VERSION);
2622
Seth Forsheea540d6b2011-09-20 16:55:52 -05002623 hci_method = find_hci_method(acpi_dev->handle);
2624 if (!hci_method) {
2625 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002626 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002627 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002628
Seth Forshee135740d2011-09-20 16:55:49 -05002629 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2630 if (!dev)
2631 return -ENOMEM;
2632 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002633 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002634 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002635 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002636
Seth Forshee6e02cc72011-09-20 16:55:51 -05002637 if (toshiba_acpi_setup_keyboard(dev))
2638 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002639
2640 mutex_init(&dev->mutex);
2641
Seth Forshee62cce752012-04-19 11:23:50 -05002642 ret = toshiba_acpi_setup_backlight(dev);
2643 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002644 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002645
2646 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002647 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002648 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2649 &acpi_dev->dev,
2650 RFKILL_TYPE_BLUETOOTH,
2651 &toshiba_rfk_ops,
2652 dev);
2653 if (!dev->bt_rfk) {
2654 pr_err("unable to allocate rfkill device\n");
2655 ret = -ENOMEM;
2656 goto error;
2657 }
2658
2659 ret = rfkill_register(dev->bt_rfk);
2660 if (ret) {
2661 pr_err("unable to register rfkill device\n");
2662 rfkill_destroy(dev->bt_rfk);
2663 goto error;
2664 }
2665 }
2666
2667 if (toshiba_illumination_available(dev)) {
2668 dev->led_dev.name = "toshiba::illumination";
2669 dev->led_dev.max_brightness = 1;
2670 dev->led_dev.brightness_set = toshiba_illumination_set;
2671 dev->led_dev.brightness_get = toshiba_illumination_get;
2672 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002673 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002674 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002675
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002676 if (toshiba_eco_mode_available(dev)) {
2677 dev->eco_led.name = "toshiba::eco_mode";
2678 dev->eco_led.max_brightness = 1;
2679 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2680 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2681 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2682 dev->eco_supported = 1;
2683 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002684
Azael Avalos93f8c162014-09-12 18:50:36 -06002685 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002686 /*
2687 * Only register the LED if KBD illumination is supported
2688 * and the keyboard backlight operation mode is set to FN-Z
2689 */
2690 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2691 dev->kbd_led.name = "toshiba::kbd_backlight";
2692 dev->kbd_led.max_brightness = 1;
2693 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2694 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2695 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2696 dev->kbd_led_registered = 1;
2697 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002698
Azael Avalos9d8658a2014-03-25 20:38:32 -06002699 ret = toshiba_touchpad_get(dev, &dummy);
2700 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002701
Azael Avalos5a2813e2014-03-25 20:38:34 -06002702 ret = toshiba_accelerometer_supported(dev);
2703 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002704
Azael Avalose26ffe52015-01-18 18:30:22 -07002705 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2706 dev->usb_sleep_charge_supported = !ret;
2707
Azael Avalosbb3fe012015-01-18 18:30:24 -07002708 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2709 dev->usb_rapid_charge_supported = !ret;
2710
Azael Avalos172ce0a2015-01-18 18:30:25 -07002711 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2712 dev->usb_sleep_music_supported = !ret;
2713
Azael Avalosbae84192015-02-10 21:09:18 -07002714 ret = toshiba_function_keys_get(dev, &dummy);
2715 dev->kbd_function_keys_supported = !ret;
2716
Azael Avalos35d53ce2015-02-10 21:09:19 -07002717 ret = toshiba_panel_power_on_get(dev, &dummy);
2718 dev->panel_power_on_supported = !ret;
2719
Seth Forshee36d03f92011-09-20 16:55:53 -05002720 /* Determine whether or not BIOS supports fan and video interfaces */
2721
2722 ret = get_video_status(dev, &dummy);
2723 dev->video_supported = !ret;
2724
2725 ret = get_fan_status(dev, &dummy);
2726 dev->fan_supported = !ret;
2727
Azael Avalos360f0f32014-03-25 20:38:31 -06002728 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2729 &toshiba_attr_group);
2730 if (ret) {
2731 dev->sysfs_created = 0;
2732 goto error;
2733 }
2734 dev->sysfs_created = !ret;
2735
Seth Forshee36d03f92011-09-20 16:55:53 -05002736 create_toshiba_proc_entries(dev);
2737
Seth Forshee29cd2932012-01-18 13:44:09 -06002738 toshiba_acpi = dev;
2739
Seth Forshee135740d2011-09-20 16:55:49 -05002740 return 0;
2741
2742error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002743 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002744 return ret;
2745}
2746
2747static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2748{
2749 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002750 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002751
Azael Avalos71454d72014-12-04 20:22:46 -07002752 switch (event) {
2753 case 0x80: /* Hotkeys and some system events */
2754 toshiba_acpi_process_hotkeys(dev);
2755 break;
Azael Avalos80546902014-12-04 20:22:47 -07002756 case 0x92: /* Keyboard backlight mode changed */
2757 /* Update sysfs entries */
2758 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2759 &toshiba_attr_group);
2760 if (ret)
2761 pr_err("Unable to update sysfs entries\n");
2762 break;
Azael Avalos71454d72014-12-04 20:22:46 -07002763 case 0x81: /* Unknown */
2764 case 0x82: /* Unknown */
2765 case 0x83: /* Unknown */
2766 case 0x8c: /* Unknown */
2767 case 0x8e: /* Unknown */
2768 case 0x8f: /* Unknown */
2769 case 0x90: /* Unknown */
2770 default:
2771 pr_info("Unknown event received %x\n", event);
2772 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002773 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002774}
2775
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002776#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002777static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002778{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002779 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002780 u32 result;
2781
2782 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002783 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002784
2785 return 0;
2786}
2787
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002788static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002789{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002790 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002791 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002792
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002793 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002794 error = toshiba_acpi_enable_hotkeys(dev);
2795 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002796 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002797 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002798
2799 return 0;
2800}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002801#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002802
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002803static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2804 toshiba_acpi_suspend, toshiba_acpi_resume);
2805
Seth Forshee135740d2011-09-20 16:55:49 -05002806static struct acpi_driver toshiba_acpi_driver = {
2807 .name = "Toshiba ACPI driver",
2808 .owner = THIS_MODULE,
2809 .ids = toshiba_device_ids,
2810 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2811 .ops = {
2812 .add = toshiba_acpi_add,
2813 .remove = toshiba_acpi_remove,
2814 .notify = toshiba_acpi_notify,
2815 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002816 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002817};
Holger Machtc9263552006-10-20 14:30:29 -07002818
Len Brown4be44fc2005-08-05 00:44:28 -04002819static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Seth Forshee135740d2011-09-20 16:55:49 -05002821 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Seth Forsheef11f9992012-01-18 13:44:11 -06002823 /*
2824 * Machines with this WMI guid aren't supported due to bugs in
2825 * their AML. This check relies on wmi initializing before
2826 * toshiba_acpi to guarantee guids have been identified.
2827 */
2828 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2829 return -ENODEV;
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2832 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002833 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002834 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
Seth Forshee135740d2011-09-20 16:55:49 -05002837 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2838 if (ret) {
2839 pr_err("Failed to register ACPI driver: %d\n", ret);
2840 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002841 }
2842
Seth Forshee135740d2011-09-20 16:55:49 -05002843 return ret;
2844}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002845
Seth Forshee135740d2011-09-20 16:55:49 -05002846static void __exit toshiba_acpi_exit(void)
2847{
2848 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2849 if (toshiba_proc_dir)
2850 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853module_init(toshiba_acpi_init);
2854module_exit(toshiba_acpi_exit);