blob: 5ace1e0fd0bd1e2b3c7bd1afccc95b0c3534bf6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04005 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02006 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07007 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Darren Hartc57c0fa2015-02-11 21:25:22 -080019 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches7e334602011-03-29 15:21:52 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Azael Avalos495078f2015-07-31 21:58:16 -060034#define TOSHIBA_ACPI_VERSION "0.23"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define PROC_INTERFACE_VERSION 1
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080042#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070043#include <linux/backlight.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050044#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070045#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020046#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060048#include <linux/workqueue.h>
49#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080050#include <linux/acpi.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020051#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070052#include <linux/uaccess.h>
Azael Avalosfc5462f2015-07-22 18:09:11 -060053#include <linux/miscdevice.h>
Azael Avalos2fdde832015-11-23 10:49:11 -070054#include <linux/rfkill.h>
Azael Avalosfc5462f2015-07-22 18:09:11 -060055#include <linux/toshiba.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020056#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_AUTHOR("John Belmonte");
59MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
60MODULE_LICENSE("GPL");
61
Seth Forsheef11f9992012-01-18 13:44:11 -060062#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
63
Seth Forshee29cd2932012-01-18 13:44:09 -060064/* Scan code for Fn key on TOS1900 models */
65#define TOS1900_FN_SCAN 0x6e
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
69
Darren Harte0769fe2015-02-11 20:50:08 -080070/*
71 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060072 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
75 * be uniform across all their models. Ideally we would just call
76 * dedicated ACPI methods instead of using this primitive interface.
77 * However the ACPI methods seem to be incomplete in some areas (for
78 * example they allow setting, but not reading, the LCD brightness value),
79 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040080 *
Azael Avalos84a62732014-03-25 20:38:29 -060081 * SCI stands for "System Configuration Interface" which aim is to
82 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Azael Avalos258c5902014-09-29 20:40:07 -060085#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Azael Avalos3f75bbe2015-05-06 09:35:11 -060087/* Operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define HCI_SET 0xff00
89#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060090#define SCI_OPEN 0xf100
91#define SCI_CLOSE 0xf200
92#define SCI_GET 0xf300
93#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Azael Avalos3f75bbe2015-05-06 09:35:11 -060095/* Return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060096#define TOS_SUCCESS 0x0000
Azael Avalose1a949c2015-07-31 21:58:14 -060097#define TOS_SUCCESS2 0x0001
Azael Avalos1864bbc2014-09-29 20:40:08 -060098#define TOS_OPEN_CLOSE_OK 0x0044
99#define TOS_FAILURE 0x1000
100#define TOS_NOT_SUPPORTED 0x8000
101#define TOS_ALREADY_OPEN 0x8100
102#define TOS_NOT_OPENED 0x8200
103#define TOS_INPUT_DATA_ERROR 0x8300
104#define TOS_WRITE_PROTECTED 0x8400
105#define TOS_NOT_PRESENT 0x8600
106#define TOS_FIFO_EMPTY 0x8c00
107#define TOS_DATA_NOT_AVAILABLE 0x8d20
108#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700109#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600111/* Registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900113#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define HCI_SYSTEM_EVENT 0x0016
115#define HCI_VIDEO_OUT 0x001c
116#define HCI_HOTKEY_EVENT 0x001e
117#define HCI_LCD_BRIGHTNESS 0x002a
Azael Avalos6873f462015-11-23 10:49:10 -0700118#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600119#define HCI_ACCELEROMETER 0x006d
Azael Avalos763ff322016-01-25 11:29:10 -0700120#define HCI_COOLING_METHOD 0x007f
Azael Avalos360f0f32014-03-25 20:38:31 -0600121#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600122#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600123#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600124#define HCI_SYSTEM_INFO 0xc000
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 Avalos17fe4b32015-02-10 21:09:20 -0700130#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600131#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700132#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600134/* Field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600135#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600136#define HCI_HOTKEY_DISABLE 0x0b
Azael Avalos52cbae02015-09-09 11:28:20 -0600137#define HCI_HOTKEY_ENABLE 0x01
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600138#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define HCI_LCD_BRIGHTNESS_BITS 3
140#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
141#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600142#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600143#define HCI_SYSTEM_TYPE1 0x10
144#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define HCI_VIDEO_OUT_LCD 0x1
146#define HCI_VIDEO_OUT_CRT 0x2
147#define HCI_VIDEO_OUT_TV 0x4
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 Avalos6873f462015-11-23 10:49:10 -0700154#define HCI_WIRELESS_STATUS 0x1
155#define HCI_WIRELESS_WWAN 0x3
156#define HCI_WIRELESS_WWAN_STATUS 0x2000
157#define HCI_WIRELESS_WWAN_POWER 0x4000
Azael Avalose26ffe52015-01-18 18:30:22 -0700158#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600159#define SCI_USB_CHARGE_DISABLED 0x00
160#define SCI_USB_CHARGE_ALTERNATE 0x09
161#define SCI_USB_CHARGE_TYPICAL 0x11
162#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700163#define SCI_USB_CHARGE_BAT_MASK 0x7
164#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
165#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
166#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700167#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Seth Forshee135740d2011-09-20 16:55:49 -0500169struct toshiba_acpi_dev {
170 struct acpi_device *acpi_dev;
171 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500172 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600173 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500174 struct backlight_device *backlight_dev;
175 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600176 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600177 struct led_classdev eco_led;
Azael Avalosfc5462f2015-07-22 18:09:11 -0600178 struct miscdevice miscdev;
Azael Avalos2fdde832015-11-23 10:49:11 -0700179 struct rfkill *wwan_rfk;
Seth Forshee36d03f92011-09-20 16:55:53 -0500180
Seth Forshee135740d2011-09-20 16:55:49 -0500181 int force_fan;
182 int last_key_event;
183 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600184 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600185 int kbd_mode;
186 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700187 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600188 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600189 int hotkey_event_type;
Azael Avalos763ff322016-01-25 11:29:10 -0700190 int max_cooling_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500191
Dan Carpenter592b7462012-01-15 14:28:20 +0300192 unsigned int illumination_supported:1;
193 unsigned int video_supported:1;
194 unsigned int fan_supported:1;
195 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600196 unsigned int ntfy_supported:1;
197 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900198 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600199 unsigned int kbd_illum_supported:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600200 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600201 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600202 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700203 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700204 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700205 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700206 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700207 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700208 unsigned int usb_three_supported:1;
Azael Avalos6873f462015-11-23 10:49:10 -0700209 unsigned int wwan_supported:1;
Azael Avalos763ff322016-01-25 11:29:10 -0700210 unsigned int cooling_method_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600211 unsigned int sysfs_created:1;
Azael Avalosb116fd002015-09-09 11:28:19 -0600212 unsigned int special_functions;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600213
Azael Avalos65e3cf92015-11-23 10:51:30 -0700214 bool kbd_event_generated;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600215 bool kbd_led_registered;
216 bool illumination_led_registered;
217 bool eco_led_registered;
Azael Avalos6873f462015-11-23 10:49:10 -0700218 bool killswitch;
Seth Forshee135740d2011-09-20 16:55:49 -0500219};
220
Seth Forshee29cd2932012-01-18 13:44:09 -0600221static struct toshiba_acpi_dev *toshiba_acpi;
222
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800223static const struct acpi_device_id toshiba_device_ids[] = {
224 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100225 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400226 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800227 {"TOS1900", 0},
228 {"", 0},
229};
230MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
231
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800232static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100233 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700234 { KE_KEY, 0x101, { KEY_MUTE } },
235 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
236 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600237 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600238 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
239 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700240 { KE_KEY, 0x13b, { KEY_COFFEE } },
241 { KE_KEY, 0x13c, { KEY_BATTERY } },
242 { KE_KEY, 0x13d, { KEY_SLEEP } },
243 { KE_KEY, 0x13e, { KEY_SUSPEND } },
244 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
245 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
246 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
247 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600248 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100249 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700250 { KE_KEY, 0xb05, { KEY_PROG2 } },
251 { KE_KEY, 0xb06, { KEY_WWW } },
252 { KE_KEY, 0xb07, { KEY_MAIL } },
253 { KE_KEY, 0xb30, { KEY_STOP } },
254 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
255 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
256 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
257 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600258 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
259 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
260 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
261 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
262 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700263 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500264};
265
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200266static const struct key_entry toshiba_acpi_alt_keymap[] = {
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200267 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
268 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600269 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200270 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200271 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
272 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600273 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200274 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600275 { KE_KEY, 0x157, { KEY_MUTE } },
276 { KE_KEY, 0x158, { KEY_WLAN } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200277 { KE_END, 0 },
278};
279
Darren Harte0769fe2015-02-11 20:50:08 -0800280/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200281 * List of models which have a broken acpi-video backlight interface and thus
282 * need to use the toshiba (vendor) interface instead.
283 */
284static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
285 {}
286};
287
288/*
Darren Harte0769fe2015-02-11 20:50:08 -0800289 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291
Azael Avalosb5163992015-02-10 23:43:56 -0700292static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 *word = (*word & ~mask) | (mask * value);
295}
296
Darren Harte0769fe2015-02-11 20:50:08 -0800297/*
298 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 acpi_status status;
304
Zhang Rui619400d2013-09-03 08:31:56 +0800305 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500306 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Darren Harte0769fe2015-02-11 20:50:08 -0800309/*
310 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600311 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Azael Avalos258c5902014-09-29 20:40:07 -0600313static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
314 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600317 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600319 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 acpi_status status;
321 int i;
322
Azael Avalos258c5902014-09-29 20:40:07 -0600323 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600325 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 in_objs[i].type = ACPI_TYPE_INTEGER;
327 in_objs[i].integer.value = in[i];
328 }
329
330 results.length = sizeof(out_objs);
331 results.pointer = out_objs;
332
Seth Forshee6e02cc72011-09-20 16:55:51 -0500333 status = acpi_evaluate_object(dev->acpi_dev->handle,
334 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400335 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600336 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700337 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 return status;
342}
343
Darren Harte0769fe2015-02-11 20:50:08 -0800344/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600345 * Common hci tasks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
347 * In addition to the ACPI status, the HCI system returns a result which
348 * may be useful (such as "not supported").
349 */
350
Azael Avalosd37782b2015-05-06 09:35:10 -0600351static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Azael Avalos258c5902014-09-29 20:40:07 -0600353 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
354 u32 out[TCI_WORDS];
355 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600356
357 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Azael Avalosd37782b2015-05-06 09:35:10 -0600360static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Azael Avalos258c5902014-09-29 20:40:07 -0600362 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
363 u32 out[TCI_WORDS];
364 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700365
Azael Avalos893f3f62014-09-29 20:40:09 -0600366 if (ACPI_FAILURE(status))
367 return TOS_FAILURE;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600370
371 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Darren Harte0769fe2015-02-11 20:50:08 -0800374/*
375 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600376 */
377
378static int sci_open(struct toshiba_acpi_dev *dev)
379{
Azael Avalos258c5902014-09-29 20:40:07 -0600380 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
381 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600382 acpi_status status;
383
Azael Avalos258c5902014-09-29 20:40:07 -0600384 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600385 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600386 pr_err("ACPI call to open SCI failed\n");
387 return 0;
388 }
389
Azael Avalos1864bbc2014-09-29 20:40:08 -0600390 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600391 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600392 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600393 pr_info("Toshiba SCI already opened\n");
394 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700395 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800396 /*
397 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700398 * implemented and return 0x8000 (Not Supported), failing to
399 * register some supported features.
400 *
401 * Simply return 1 if we hit those affected laptops to make the
402 * supported features work.
403 *
404 * In the case that some laptops really do not support the SCI,
405 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
406 * and thus, not registering support for the queried feature.
407 */
408 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600409 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600410 pr_info("Toshiba SCI is not present\n");
411 }
412
413 return 0;
414}
415
416static void sci_close(struct toshiba_acpi_dev *dev)
417{
Azael Avalos258c5902014-09-29 20:40:07 -0600418 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
419 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600420 acpi_status status;
421
Azael Avalos258c5902014-09-29 20:40:07 -0600422 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600423 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600424 pr_err("ACPI call to close SCI failed\n");
425 return;
426 }
427
Azael Avalos1864bbc2014-09-29 20:40:08 -0600428 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600429 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600430 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600431 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600432 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600433 pr_info("Toshiba SCI is not present\n");
434}
435
Azael Avalos893f3f62014-09-29 20:40:09 -0600436static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600437{
Azael Avalos258c5902014-09-29 20:40:07 -0600438 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
439 u32 out[TCI_WORDS];
440 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700441
Azael Avalos893f3f62014-09-29 20:40:09 -0600442 if (ACPI_FAILURE(status))
443 return TOS_FAILURE;
444
Azael Avalos84a62732014-03-25 20:38:29 -0600445 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600446
447 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600448}
449
Azael Avalos893f3f62014-09-29 20:40:09 -0600450static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600451{
Azael Avalos258c5902014-09-29 20:40:07 -0600452 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
453 u32 out[TCI_WORDS];
454 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600455
456 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600457}
458
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200459/* Illumination support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600460static void toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200461{
Azael Avalos258c5902014-09-29 20:40:07 -0600462 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
463 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200464 acpi_status status;
465
Azael Avalosea215a3f2015-07-31 21:58:12 -0600466 dev->illumination_supported = 0;
467 dev->illumination_led_registered = false;
468
Azael Avalosfdb79082014-03-25 20:38:30 -0600469 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600470 return;
Azael Avalosfdb79082014-03-25 20:38:30 -0600471
Azael Avalos258c5902014-09-29 20:40:07 -0600472 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600473 sci_close(dev);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600474 if (ACPI_FAILURE(status))
Azael Avalosfdb79082014-03-25 20:38:30 -0600475 pr_err("ACPI call to query Illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600476 else if (out[0] == TOS_SUCCESS)
477 dev->illumination_supported = 1;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200478}
479
480static void toshiba_illumination_set(struct led_classdev *cdev,
481 enum led_brightness brightness)
482{
Seth Forshee135740d2011-09-20 16:55:49 -0500483 struct toshiba_acpi_dev *dev = container_of(cdev,
484 struct toshiba_acpi_dev, led_dev);
Azael Avalose1a949c2015-07-31 21:58:14 -0600485 u32 result;
486 u32 state;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200487
488 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600489 if (!sci_open(dev))
490 return;
491
492 /* Switch the illumination on/off */
493 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600494 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600495 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600496 if (result == TOS_FAILURE)
Azael Avalosfdb79082014-03-25 20:38:30 -0600497 pr_err("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200498}
499
500static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
501{
Seth Forshee135740d2011-09-20 16:55:49 -0500502 struct toshiba_acpi_dev *dev = container_of(cdev,
503 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600504 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200505
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600506 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200508 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200509
510 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600511 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600512 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600513 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600514 pr_err("ACPI call for illumination failed\n");
515 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600516 } else if (result != TOS_SUCCESS) {
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200517 return LED_OFF;
518 }
519
Azael Avalosfdb79082014-03-25 20:38:30 -0600520 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200521}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400522
Azael Avalos360f0f32014-03-25 20:38:31 -0600523/* KBD Illumination */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600524static void toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
Azael Avalos93f8c162014-09-12 18:50:36 -0600525{
Azael Avalos258c5902014-09-29 20:40:07 -0600526 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
527 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600528 acpi_status status;
529
Azael Avalosea215a3f2015-07-31 21:58:12 -0600530 dev->kbd_illum_supported = 0;
531 dev->kbd_led_registered = false;
Azael Avalos65e3cf92015-11-23 10:51:30 -0700532 dev->kbd_event_generated = false;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600533
Azael Avalos93f8c162014-09-12 18:50:36 -0600534 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600535 return;
Azael Avalos93f8c162014-09-12 18:50:36 -0600536
Azael Avalos258c5902014-09-29 20:40:07 -0600537 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600538 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600539 if (ACPI_FAILURE(status)) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600540 pr_err("ACPI call to query kbd illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600541 } else if (out[0] == TOS_SUCCESS) {
542 /*
543 * Check for keyboard backlight timeout max value,
544 * previous kbd backlight implementation set this to
545 * 0x3c0003, and now the new implementation set this
546 * to 0x3c001a, use this to distinguish between them.
547 */
548 if (out[3] == SCI_KBD_TIME_MAX)
549 dev->kbd_type = 2;
550 else
551 dev->kbd_type = 1;
552 /* Get the current keyboard backlight mode */
553 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
554 /* Get the current time (1-60 seconds) */
555 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
556 /* Flag as supported */
557 dev->kbd_illum_supported = 1;
Azael Avalos93f8c162014-09-12 18:50:36 -0600558 }
Azael Avalos93f8c162014-09-12 18:50:36 -0600559}
560
Azael Avalos360f0f32014-03-25 20:38:31 -0600561static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
562{
563 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600564
565 if (!sci_open(dev))
566 return -EIO;
567
Azael Avalos893f3f62014-09-29 20:40:09 -0600568 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600569 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600570 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600571 pr_err("ACPI call to set KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600572 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600573 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600574
Azael Avalose1a949c2015-07-31 21:58:14 -0600575 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600576}
577
578static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
579{
580 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600581
582 if (!sci_open(dev))
583 return -EIO;
584
Azael Avalos893f3f62014-09-29 20:40:09 -0600585 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600586 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600587 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600588 pr_err("ACPI call to get KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600589 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600590 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600591
Azael Avalose1a949c2015-07-31 21:58:14 -0600592 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600593}
594
595static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
596{
597 struct toshiba_acpi_dev *dev = container_of(cdev,
598 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600599 u32 result;
600 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600601
602 /* Check the keyboard backlight state */
Azael Avalosd37782b2015-05-06 09:35:10 -0600603 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600604 if (result == TOS_FAILURE) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600605 pr_err("ACPI call to get the keyboard backlight failed\n");
606 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600607 } else if (result != TOS_SUCCESS) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600608 return LED_OFF;
609 }
610
611 return state ? LED_FULL : LED_OFF;
612}
613
614static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
615 enum led_brightness brightness)
616{
617 struct toshiba_acpi_dev *dev = container_of(cdev,
618 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600619 u32 result;
620 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600621
622 /* Set the keyboard backlight state */
623 state = brightness ? 1 : 0;
Azael Avalosd37782b2015-05-06 09:35:10 -0600624 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600625 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600626 pr_err("ACPI call to set KBD Illumination mode failed\n");
Azael Avalos360f0f32014-03-25 20:38:31 -0600627}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400628
Azael Avalos9d8658a2014-03-25 20:38:32 -0600629/* TouchPad support */
630static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
631{
632 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600633
634 if (!sci_open(dev))
635 return -EIO;
636
Azael Avalos893f3f62014-09-29 20:40:09 -0600637 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600638 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600639 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600640 pr_err("ACPI call to set the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600641 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600642 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600643
Azael Avalose1a949c2015-07-31 21:58:14 -0600644 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600645}
646
647static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
648{
649 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600650
651 if (!sci_open(dev))
652 return -EIO;
653
Azael Avalos893f3f62014-09-29 20:40:09 -0600654 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600655 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600656 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600657 pr_err("ACPI call to query the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600658 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600659 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600660
Azael Avalose1a949c2015-07-31 21:58:14 -0600661 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600662}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200663
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600664/* Eco Mode support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600665static void toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600666{
667 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700668 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600669 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600670
Azael Avalosea215a3f2015-07-31 21:58:12 -0600671 dev->eco_supported = 0;
672 dev->eco_led_registered = false;
673
Azael Avalos258c5902014-09-29 20:40:07 -0600674 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600675 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700676 pr_err("ACPI call to get ECO led failed\n");
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700677 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800678 /*
679 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700680 * LED device is present, but that we just screwed the input
681 * parameters.
682 *
683 * Let's query the status of the LED to see if we really have a
684 * success response, indicating the actual presense of the LED,
685 * bail out otherwise.
686 */
687 in[3] = 1;
688 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600689 if (ACPI_FAILURE(status))
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700690 pr_err("ACPI call to get ECO led failed\n");
691 else if (out[0] == TOS_SUCCESS)
Azael Avalosea215a3f2015-07-31 21:58:12 -0600692 dev->eco_supported = 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600693 }
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600694}
695
Azael Avalosb5163992015-02-10 23:43:56 -0700696static enum led_brightness
697toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600698{
699 struct toshiba_acpi_dev *dev = container_of(cdev,
700 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600701 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
702 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600703 acpi_status status;
704
Azael Avalos258c5902014-09-29 20:40:07 -0600705 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600706 if (ACPI_FAILURE(status)) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600707 pr_err("ACPI call to get ECO led failed\n");
708 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600709 } else if (out[0] != TOS_SUCCESS) {
710 return LED_OFF;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600711 }
712
713 return out[2] ? LED_FULL : LED_OFF;
714}
715
716static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
717 enum led_brightness brightness)
718{
719 struct toshiba_acpi_dev *dev = container_of(cdev,
720 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600721 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
722 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600723 acpi_status status;
724
725 /* Switch the Eco Mode led on/off */
726 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600727 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600728 if (ACPI_FAILURE(status))
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600729 pr_err("ACPI call to set ECO led failed\n");
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600730}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400731
Azael Avalos5a2813e2014-03-25 20:38:34 -0600732/* Accelerometer support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600733static void toshiba_accelerometer_available(struct toshiba_acpi_dev *dev)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600734{
Azael Avalos258c5902014-09-29 20:40:07 -0600735 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
736 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600737 acpi_status status;
738
Azael Avalosea215a3f2015-07-31 21:58:12 -0600739 dev->accelerometer_supported = 0;
740
Darren Harte0769fe2015-02-11 20:50:08 -0800741 /*
742 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600743 * this call also serves as initialization
744 */
Azael Avalos258c5902014-09-29 20:40:07 -0600745 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600746 if (ACPI_FAILURE(status))
Azael Avalos5a2813e2014-03-25 20:38:34 -0600747 pr_err("ACPI call to query the accelerometer failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600748 else if (out[0] == TOS_SUCCESS)
749 dev->accelerometer_supported = 1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600750}
751
752static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
Azael Avalosa6b53542015-07-31 21:58:15 -0600753 u32 *xy, u32 *z)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600754{
Azael Avalos258c5902014-09-29 20:40:07 -0600755 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
756 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600757 acpi_status status;
758
759 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600760 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600761 if (ACPI_FAILURE(status)) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600762 pr_err("ACPI call to query the accelerometer failed\n");
763 return -EIO;
Azael Avalose1a949c2015-07-31 21:58:14 -0600764 } else if (out[0] == TOS_NOT_SUPPORTED) {
765 return -ENODEV;
766 } else if (out[0] == TOS_SUCCESS) {
767 *xy = out[2];
768 *z = out[4];
769 return 0;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600770 }
771
Azael Avalose1a949c2015-07-31 21:58:14 -0600772 return -EIO;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600773}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600774
Azael Avalose26ffe52015-01-18 18:30:22 -0700775/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600776static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
777{
778 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
779 u32 out[TCI_WORDS];
780 acpi_status status;
781
Azael Avalosc8c91842015-04-02 19:26:20 -0600782 dev->usb_sleep_charge_supported = 0;
783
784 if (!sci_open(dev))
785 return;
786
787 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600788 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600789 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
790 sci_close(dev);
791 return;
792 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600793 sci_close(dev);
794 return;
795 } else if (out[0] == TOS_SUCCESS) {
796 dev->usbsc_mode_base = out[4];
797 }
798
799 in[5] = SCI_USB_CHARGE_BAT_LVL;
800 status = tci_raw(dev, in, out);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600801 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600802 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600803 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
Azael Avalosc8c91842015-04-02 19:26:20 -0600804 } else if (out[0] == TOS_SUCCESS) {
805 dev->usbsc_bat_level = out[2];
Azael Avalosea215a3f2015-07-31 21:58:12 -0600806 /* Flag as supported */
Azael Avalosc8c91842015-04-02 19:26:20 -0600807 dev->usb_sleep_charge_supported = 1;
808 }
809
Azael Avalosc8c91842015-04-02 19:26:20 -0600810}
811
Azael Avalose26ffe52015-01-18 18:30:22 -0700812static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
813 u32 *mode)
814{
815 u32 result;
816
817 if (!sci_open(dev))
818 return -EIO;
819
820 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
821 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600822 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700823 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600824 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700825 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700826
Azael Avalose1a949c2015-07-31 21:58:14 -0600827 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700828}
829
830static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
831 u32 mode)
832{
833 u32 result;
834
835 if (!sci_open(dev))
836 return -EIO;
837
838 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
839 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600840 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700841 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600842 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700843 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700844
Azael Avalose1a949c2015-07-31 21:58:14 -0600845 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700846}
847
Azael Avalos182bcaa2015-01-18 18:30:23 -0700848static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
849 u32 *mode)
850{
851 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
852 u32 out[TCI_WORDS];
853 acpi_status status;
854
855 if (!sci_open(dev))
856 return -EIO;
857
858 in[5] = SCI_USB_CHARGE_BAT_LVL;
859 status = tci_raw(dev, in, out);
860 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600861 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700862 pr_err("ACPI call to get USB S&C battery level failed\n");
Azael Avalos182bcaa2015-01-18 18:30:23 -0700863 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700864 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600865 } else if (out[0] == TOS_SUCCESS) {
866 *mode = out[2];
867 return 0;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700868 }
869
Azael Avalose1a949c2015-07-31 21:58:14 -0600870 return -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700871}
872
873static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
874 u32 mode)
875{
876 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
877 u32 out[TCI_WORDS];
878 acpi_status status;
879
880 if (!sci_open(dev))
881 return -EIO;
882
883 in[2] = mode;
884 in[5] = SCI_USB_CHARGE_BAT_LVL;
885 status = tci_raw(dev, in, out);
886 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600887 if (ACPI_FAILURE(status))
Azael Avalos182bcaa2015-01-18 18:30:23 -0700888 pr_err("ACPI call to set USB S&C battery level failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600889 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalos182bcaa2015-01-18 18:30:23 -0700890 return -ENODEV;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700891
Azael Avalose1a949c2015-07-31 21:58:14 -0600892 return out[0] == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700893}
894
Azael Avalosbb3fe012015-01-18 18:30:24 -0700895static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
896 u32 *state)
897{
898 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
899 u32 out[TCI_WORDS];
900 acpi_status status;
901
902 if (!sci_open(dev))
903 return -EIO;
904
905 in[5] = SCI_USB_CHARGE_RAPID_DSP;
906 status = tci_raw(dev, in, out);
907 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600908 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600909 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600910 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb3fe012015-01-18 18:30:24 -0700911 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600912 } else if (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) {
913 *state = out[2];
914 return 0;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700915 }
916
Azael Avalose1a949c2015-07-31 21:58:14 -0600917 return -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700918}
919
920static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
921 u32 state)
922{
923 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
924 u32 out[TCI_WORDS];
925 acpi_status status;
926
927 if (!sci_open(dev))
928 return -EIO;
929
930 in[2] = state;
931 in[5] = SCI_USB_CHARGE_RAPID_DSP;
932 status = tci_raw(dev, in, out);
933 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600934 if (ACPI_FAILURE(status))
Azael Avalosbb26f182015-04-02 19:26:21 -0600935 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600936 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalosbb3fe012015-01-18 18:30:24 -0700937 return -ENODEV;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700938
Azael Avalose1a949c2015-07-31 21:58:14 -0600939 return (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700940}
941
Azael Avalos172ce0a2015-01-18 18:30:25 -0700942static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
943{
944 u32 result;
945
946 if (!sci_open(dev))
947 return -EIO;
948
949 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
950 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600951 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600952 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600953 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700954 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700955
Azael Avaloscf680ea2015-09-09 11:25:44 -0600956 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700957}
958
959static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
960{
961 u32 result;
962
963 if (!sci_open(dev))
964 return -EIO;
965
966 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
967 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600968 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600969 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600970 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700971 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700972
Azael Avalose1a949c2015-07-31 21:58:14 -0600973 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700974}
975
Azael Avalosbae84192015-02-10 21:09:18 -0700976/* Keyboard function keys */
977static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
978{
979 u32 result;
980
981 if (!sci_open(dev))
982 return -EIO;
983
984 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
985 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600986 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -0700987 pr_err("ACPI call to get KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600988 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -0700989 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -0700990
Azael Avalose1a949c2015-07-31 21:58:14 -0600991 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -0700992}
993
994static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
995{
996 u32 result;
997
998 if (!sci_open(dev))
999 return -EIO;
1000
1001 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1002 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001003 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -07001004 pr_err("ACPI call to set KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001005 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -07001006 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -07001007
Azael Avalose1a949c2015-07-31 21:58:14 -06001008 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -07001009}
1010
Azael Avalos35d53ce2015-02-10 21:09:19 -07001011/* Panel Power ON */
1012static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1013{
1014 u32 result;
1015
1016 if (!sci_open(dev))
1017 return -EIO;
1018
1019 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1020 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001021 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001022 pr_err("ACPI call to get Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001023 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001024 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001025
Azael Avalose1a949c2015-07-31 21:58:14 -06001026 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001027}
1028
1029static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1030{
1031 u32 result;
1032
1033 if (!sci_open(dev))
1034 return -EIO;
1035
1036 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1037 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001038 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001039 pr_err("ACPI call to set Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001040 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001041 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001042
Azael Avalose1a949c2015-07-31 21:58:14 -06001043 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001044}
1045
Azael Avalos17fe4b32015-02-10 21:09:20 -07001046/* USB Three */
1047static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1048{
1049 u32 result;
1050
1051 if (!sci_open(dev))
1052 return -EIO;
1053
1054 result = sci_read(dev, SCI_USB_THREE, state);
1055 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001056 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001057 pr_err("ACPI call to get USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001058 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001059 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001060
Azael Avalose1a949c2015-07-31 21:58:14 -06001061 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001062}
1063
1064static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1065{
1066 u32 result;
1067
1068 if (!sci_open(dev))
1069 return -EIO;
1070
1071 result = sci_write(dev, SCI_USB_THREE, state);
1072 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001073 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001074 pr_err("ACPI call to set USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001075 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001076 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001077
Azael Avalose1a949c2015-07-31 21:58:14 -06001078 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001079}
1080
Azael Avalos56e6b352015-03-20 16:55:16 -06001081/* Hotkey Event type */
1082static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1083 u32 *type)
1084{
Azael Avalos3b876002015-05-06 09:35:09 -06001085 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1086 u32 out[TCI_WORDS];
1087 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001088
Azael Avalos3b876002015-05-06 09:35:09 -06001089 status = tci_raw(dev, in, out);
1090 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001091 pr_err("ACPI call to get System type failed\n");
Azael Avalos3b876002015-05-06 09:35:09 -06001092 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001093 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -06001094 } else if (out[0] == TOS_SUCCESS) {
1095 *type = out[3];
1096 return 0;
Azael Avalos56e6b352015-03-20 16:55:16 -06001097 }
1098
Azael Avalose1a949c2015-07-31 21:58:14 -06001099 return -EIO;
Azael Avalos56e6b352015-03-20 16:55:16 -06001100}
1101
Azael Avalos6873f462015-11-23 10:49:10 -07001102/* Wireless status (RFKill, WLAN, BT, WWAN) */
1103static int toshiba_wireless_status(struct toshiba_acpi_dev *dev)
1104{
1105 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1106 u32 out[TCI_WORDS];
1107 acpi_status status;
1108
1109 in[3] = HCI_WIRELESS_STATUS;
1110 status = tci_raw(dev, in, out);
1111
1112 if (ACPI_FAILURE(status)) {
1113 pr_err("ACPI call to get Wireless status failed\n");
1114 return -EIO;
1115 }
1116
1117 if (out[0] == TOS_NOT_SUPPORTED)
1118 return -ENODEV;
1119
1120 if (out[0] != TOS_SUCCESS)
1121 return -EIO;
1122
1123 dev->killswitch = !!(out[2] & HCI_WIRELESS_STATUS);
1124
1125 return 0;
1126}
1127
1128/* WWAN */
1129static void toshiba_wwan_available(struct toshiba_acpi_dev *dev)
1130{
1131 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1132 u32 out[TCI_WORDS];
1133 acpi_status status;
1134
1135 dev->wwan_supported = 0;
1136
1137 /*
1138 * WWAN support can be queried by setting the in[3] value to
1139 * HCI_WIRELESS_WWAN (0x03).
1140 *
1141 * If supported, out[0] contains TOS_SUCCESS and out[2] contains
1142 * HCI_WIRELESS_WWAN_STATUS (0x2000).
1143 *
1144 * If not supported, out[0] contains TOS_INPUT_DATA_ERROR (0x8300)
1145 * or TOS_NOT_SUPPORTED (0x8000).
1146 */
1147 in[3] = HCI_WIRELESS_WWAN;
1148 status = tci_raw(dev, in, out);
1149
1150 if (ACPI_FAILURE(status)) {
1151 pr_err("ACPI call to get WWAN status failed\n");
1152 return;
1153 }
1154
1155 if (out[0] != TOS_SUCCESS)
1156 return;
1157
1158 dev->wwan_supported = (out[2] == HCI_WIRELESS_WWAN_STATUS);
1159}
1160
1161static int toshiba_wwan_set(struct toshiba_acpi_dev *dev, u32 state)
1162{
1163 u32 in[TCI_WORDS] = { HCI_SET, HCI_WIRELESS, state, 0, 0, 0 };
1164 u32 out[TCI_WORDS];
1165 acpi_status status;
1166
1167 in[3] = HCI_WIRELESS_WWAN_STATUS;
1168 status = tci_raw(dev, in, out);
1169
1170 if (ACPI_FAILURE(status)) {
1171 pr_err("ACPI call to set WWAN status failed\n");
1172 return -EIO;
1173 }
1174
1175 if (out[0] == TOS_NOT_SUPPORTED)
1176 return -ENODEV;
1177
1178 if (out[0] != TOS_SUCCESS)
1179 return -EIO;
1180
1181 /*
1182 * Some devices only need to call HCI_WIRELESS_WWAN_STATUS to
1183 * (de)activate the device, but some others need the
1184 * HCI_WIRELESS_WWAN_POWER call as well.
1185 */
1186 in[3] = HCI_WIRELESS_WWAN_POWER;
1187 status = tci_raw(dev, in, out);
1188
1189 if (ACPI_FAILURE(status)) {
1190 pr_err("ACPI call to set WWAN power failed\n");
1191 return -EIO;
1192 }
1193
1194 if (out[0] == TOS_NOT_SUPPORTED)
1195 return -ENODEV;
1196
1197 return out[0] == TOS_SUCCESS ? 0 : -EIO;
1198}
1199
Azael Avalos763ff322016-01-25 11:29:10 -07001200/* Cooling Method */
1201static void toshiba_cooling_method_available(struct toshiba_acpi_dev *dev)
1202{
1203 u32 in[TCI_WORDS] = { HCI_GET, HCI_COOLING_METHOD, 0, 0, 0, 0 };
1204 u32 out[TCI_WORDS];
1205 acpi_status status;
1206
1207 dev->cooling_method_supported = 0;
1208 dev->max_cooling_method = 0;
1209
1210 status = tci_raw(dev, in, out);
1211 if (ACPI_FAILURE(status))
1212 pr_err("ACPI call to get Cooling Method failed\n");
1213
1214 if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
1215 return;
1216
1217 dev->cooling_method_supported = 1;
1218 dev->max_cooling_method = out[3];
1219}
1220
1221static int toshiba_cooling_method_get(struct toshiba_acpi_dev *dev, u32 *state)
1222{
1223 u32 result = hci_read(dev, HCI_COOLING_METHOD, state);
1224
1225 if (result == TOS_FAILURE)
1226 pr_err("ACPI call to get Cooling Method failed\n");
1227
1228 if (result == TOS_NOT_SUPPORTED)
1229 return -ENODEV;
1230
1231 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1232}
1233
1234static int toshiba_cooling_method_set(struct toshiba_acpi_dev *dev, u32 state)
1235{
1236 u32 result = hci_write(dev, HCI_COOLING_METHOD, state);
1237
1238 if (result == TOS_FAILURE)
1239 pr_err("ACPI call to get Cooling Method failed\n");
1240
1241 if (result == TOS_NOT_SUPPORTED)
1242 return -ENODEV;
1243
1244 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1245}
1246
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001247/* Transflective Backlight */
Azael Avalos695f6062015-07-22 18:09:13 -06001248static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001249{
Azael Avalose1a949c2015-07-31 21:58:14 -06001250 u32 result = hci_read(dev, HCI_TR_BACKLIGHT, status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001251
Azael Avalose1a949c2015-07-31 21:58:14 -06001252 if (result == TOS_FAILURE)
1253 pr_err("ACPI call to get Transflective Backlight failed\n");
1254 else if (result == TOS_NOT_SUPPORTED)
1255 return -ENODEV;
1256
1257 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001258}
1259
Azael Avalos695f6062015-07-22 18:09:13 -06001260static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001261{
Azael Avalose1a949c2015-07-31 21:58:14 -06001262 u32 result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001263
Azael Avalose1a949c2015-07-31 21:58:14 -06001264 if (result == TOS_FAILURE)
1265 pr_err("ACPI call to set Transflective Backlight failed\n");
1266 else if (result == TOS_NOT_SUPPORTED)
1267 return -ENODEV;
1268
1269 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001270}
1271
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001272static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001274/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001275static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Azael Avalose1a949c2015-07-31 21:58:14 -06001277 u32 result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001279 int brightness = 0;
1280
1281 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001282 int ret = get_tr_backlight_status(dev, &value);
Azael Avalosb5163992015-02-10 23:43:56 -07001283
Akio Idehara121b7b02012-04-06 01:46:43 +09001284 if (ret)
1285 return ret;
Azael Avalos695f6062015-07-22 18:09:13 -06001286 if (value)
Akio Idehara121b7b02012-04-06 01:46:43 +09001287 return 0;
1288 brightness++;
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Azael Avalose1a949c2015-07-31 21:58:14 -06001291 result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1292 if (result == TOS_FAILURE)
1293 pr_err("ACPI call to get LCD Brightness failed\n");
1294 else if (result == TOS_NOT_SUPPORTED)
1295 return -ENODEV;
1296 if (result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001297 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001298
1299 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001300}
1301
Seth Forshee62cce752012-04-19 11:23:50 -05001302static int get_lcd_brightness(struct backlight_device *bd)
1303{
1304 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001305
Seth Forshee62cce752012-04-19 11:23:50 -05001306 return __get_lcd_brightness(dev);
1307}
1308
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001309static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001310{
Seth Forshee135740d2011-09-20 16:55:49 -05001311 struct toshiba_acpi_dev *dev = m->private;
Akio Idehara121b7b02012-04-06 01:46:43 +09001312 int levels;
Azael Avalose1a949c2015-07-31 21:58:14 -06001313 int value;
Holger Machtc9263552006-10-20 14:30:29 -07001314
Seth Forshee135740d2011-09-20 16:55:49 -05001315 if (!dev->backlight_dev)
1316 return -ENODEV;
1317
Akio Idehara121b7b02012-04-06 01:46:43 +09001318 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001319 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001320 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001321 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001322 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001326 pr_err("Error reading LCD brightness\n");
Azael Avalose1a949c2015-07-31 21:58:14 -06001327
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001328 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001329}
1330
1331static int lcd_proc_open(struct inode *inode, struct file *file)
1332{
Al Virod9dda782013-03-31 18:16:14 -04001333 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Seth Forshee62cce752012-04-19 11:23:50 -05001336static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001337{
Azael Avalose1a949c2015-07-31 21:58:14 -06001338 u32 result;
Holger Machtc9263552006-10-20 14:30:29 -07001339
Akio Idehara121b7b02012-04-06 01:46:43 +09001340 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001341 int ret = set_tr_backlight_status(dev, !value);
Azael Avalosb5163992015-02-10 23:43:56 -07001342
Akio Idehara121b7b02012-04-06 01:46:43 +09001343 if (ret)
1344 return ret;
1345 if (value)
1346 value--;
1347 }
1348
Azael Avalosa39f46d2014-11-24 19:29:36 -07001349 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalose1a949c2015-07-31 21:58:14 -06001350 result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1351 if (result == TOS_FAILURE)
1352 pr_err("ACPI call to set LCD Brightness failed\n");
1353 else if (result == TOS_NOT_SUPPORTED)
1354 return -ENODEV;
1355
1356 return result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001357}
1358
1359static int set_lcd_status(struct backlight_device *bd)
1360{
Seth Forshee135740d2011-09-20 16:55:49 -05001361 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001362
Seth Forshee62cce752012-04-19 11:23:50 -05001363 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001364}
1365
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001366static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1367 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Al Virod9dda782013-03-31 18:16:14 -04001369 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001370 char cmd[42];
1371 size_t len;
Akio Idehara121b7b02012-04-06 01:46:43 +09001372 int levels = dev->backlight_dev->props.max_brightness + 1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001373 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001375 len = min(count, sizeof(cmd) - 1);
1376 if (copy_from_user(cmd, buf, len))
1377 return -EFAULT;
1378 cmd[len] = '\0';
1379
Azael Avalose1a949c2015-07-31 21:58:14 -06001380 if (sscanf(cmd, " brightness : %i", &value) != 1 &&
1381 value < 0 && value > levels)
1382 return -EINVAL;
1383
1384 if (set_lcd_brightness(dev, value))
1385 return -EIO;
1386
1387 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001390static const struct file_operations lcd_proc_fops = {
1391 .owner = THIS_MODULE,
1392 .open = lcd_proc_open,
1393 .read = seq_read,
1394 .llseek = seq_lseek,
1395 .release = single_release,
1396 .write = lcd_proc_write,
1397};
1398
Azael Avalose1a949c2015-07-31 21:58:14 -06001399/* Video-Out */
Seth Forshee36d03f92011-09-20 16:55:53 -05001400static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1401{
Azael Avalose1a949c2015-07-31 21:58:14 -06001402 u32 result = hci_read(dev, HCI_VIDEO_OUT, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001403
Azael Avalose1a949c2015-07-31 21:58:14 -06001404 if (result == TOS_FAILURE)
1405 pr_err("ACPI call to get Video-Out failed\n");
1406 else if (result == TOS_NOT_SUPPORTED)
1407 return -ENODEV;
1408
1409 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001410}
1411
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001412static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Seth Forshee135740d2011-09-20 16:55:49 -05001414 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 u32 value;
1416
Azael Avalose1a949c2015-07-31 21:58:14 -06001417 if (!get_video_status(dev, &value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1419 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001420 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001421
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001422 seq_printf(m, "lcd_out: %d\n", is_lcd);
1423 seq_printf(m, "crt_out: %d\n", is_crt);
1424 seq_printf(m, "tv_out: %d\n", is_tv);
Azael Avalose1a949c2015-07-31 21:58:14 -06001425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Azael Avalose1a949c2015-07-31 21:58:14 -06001428 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001431static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Al Virod9dda782013-03-31 18:16:14 -04001433 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001434}
1435
1436static ssize_t video_proc_write(struct file *file, const char __user *buf,
1437 size_t count, loff_t *pos)
1438{
Al Virod9dda782013-03-31 18:16:14 -04001439 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Azael Avalose1a949c2015-07-31 21:58:14 -06001440 char *buffer;
1441 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int remain = count;
1443 int lcd_out = -1;
1444 int crt_out = -1;
1445 int tv_out = -1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001446 int value;
1447 int ret;
Al Virob4482a42007-10-14 19:35:40 +01001448 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001450 cmd = kmalloc(count + 1, GFP_KERNEL);
1451 if (!cmd)
1452 return -ENOMEM;
1453 if (copy_from_user(cmd, buf, count)) {
1454 kfree(cmd);
1455 return -EFAULT;
1456 }
1457 cmd[count] = '\0';
1458
1459 buffer = cmd;
1460
Darren Harte0769fe2015-02-11 20:50:08 -08001461 /*
1462 * Scan expression. Multiple expressions may be delimited with ;
1463 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465 while (remain) {
1466 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1467 lcd_out = value & 1;
1468 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1469 crt_out = value & 1;
1470 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1471 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001472 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 do {
1474 ++buffer;
1475 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001476 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001479 kfree(cmd);
1480
Seth Forshee36d03f92011-09-20 16:55:53 -05001481 ret = get_video_status(dev, &video_out);
1482 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001483 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (lcd_out != -1)
1486 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1487 if (crt_out != -1)
1488 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1489 if (tv_out != -1)
1490 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001491 /*
1492 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001493 * video setting if something changed.
1494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001496 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
Azael Avalose1a949c2015-07-31 21:58:14 -06001499 return ret ? -EIO : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001502static const struct file_operations video_proc_fops = {
1503 .owner = THIS_MODULE,
1504 .open = video_proc_open,
1505 .read = seq_read,
1506 .llseek = seq_lseek,
1507 .release = single_release,
1508 .write = video_proc_write,
1509};
1510
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001511/* Fan status */
Seth Forshee36d03f92011-09-20 16:55:53 -05001512static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1513{
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001514 u32 result = hci_read(dev, HCI_FAN, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001515
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001516 if (result == TOS_FAILURE)
1517 pr_err("ACPI call to get Fan status failed\n");
1518 else if (result == TOS_NOT_SUPPORTED)
1519 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001520
Azael Avalose1a949c2015-07-31 21:58:14 -06001521 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001522}
1523
1524static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
1525{
1526 u32 result = hci_write(dev, HCI_FAN, status);
1527
1528 if (result == TOS_FAILURE)
1529 pr_err("ACPI call to set Fan status failed\n");
1530 else if (result == TOS_NOT_SUPPORTED)
1531 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001532
Azael Avalose1a949c2015-07-31 21:58:14 -06001533 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001534}
1535
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001536static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Seth Forshee135740d2011-09-20 16:55:49 -05001538 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 u32 value;
1540
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001541 if (get_fan_status(dev, &value))
1542 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001544 seq_printf(m, "running: %d\n", (value > 0));
1545 seq_printf(m, "force_on: %d\n", dev->force_fan);
1546
1547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001550static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Al Virod9dda782013-03-31 18:16:14 -04001552 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001553}
1554
1555static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1556 size_t count, loff_t *pos)
1557{
Al Virod9dda782013-03-31 18:16:14 -04001558 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001559 char cmd[42];
1560 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001563 len = min(count, sizeof(cmd) - 1);
1564 if (copy_from_user(cmd, buf, len))
1565 return -EFAULT;
1566 cmd[len] = '\0';
1567
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001568 if (sscanf(cmd, " force_on : %i", &value) != 1 &&
1569 value != 0 && value != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EINVAL;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001571
1572 if (set_fan_status(dev, value))
1573 return -EIO;
1574
1575 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 return count;
1578}
1579
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001580static const struct file_operations fan_proc_fops = {
1581 .owner = THIS_MODULE,
1582 .open = fan_proc_open,
1583 .read = seq_read,
1584 .llseek = seq_lseek,
1585 .release = single_release,
1586 .write = fan_proc_write,
1587};
1588
1589static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Seth Forshee135740d2011-09-20 16:55:49 -05001591 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Seth Forshee135740d2011-09-20 16:55:49 -05001593 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1594 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Azael Avalos7deef552015-07-22 18:09:10 -06001595
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001599static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Al Virod9dda782013-03-31 18:16:14 -04001601 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001602}
1603
1604static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1605 size_t count, loff_t *pos)
1606{
Al Virod9dda782013-03-31 18:16:14 -04001607 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001608 char cmd[42];
1609 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int value;
1611
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001612 len = min(count, sizeof(cmd) - 1);
1613 if (copy_from_user(cmd, buf, len))
1614 return -EFAULT;
1615 cmd[len] = '\0';
1616
Azael Avalosb5163992015-02-10 23:43:56 -07001617 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001618 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 return count;
1623}
1624
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001625static const struct file_operations keys_proc_fops = {
1626 .owner = THIS_MODULE,
1627 .open = keys_proc_open,
1628 .read = seq_read,
1629 .llseek = seq_lseek,
1630 .release = single_release,
1631 .write = keys_proc_write,
1632};
1633
1634static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001636 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1637 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001641static int version_proc_open(struct inode *inode, struct file *file)
1642{
Al Virod9dda782013-03-31 18:16:14 -04001643 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001644}
1645
1646static const struct file_operations version_proc_fops = {
1647 .owner = THIS_MODULE,
1648 .open = version_proc_open,
1649 .read = seq_read,
1650 .llseek = seq_lseek,
1651 .release = single_release,
1652};
1653
Darren Harte0769fe2015-02-11 20:50:08 -08001654/*
1655 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657
1658#define PROC_TOSHIBA "toshiba"
1659
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001660static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Seth Forshee36d03f92011-09-20 16:55:53 -05001662 if (dev->backlight_dev)
1663 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1664 &lcd_proc_fops, dev);
1665 if (dev->video_supported)
1666 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1667 &video_proc_fops, dev);
1668 if (dev->fan_supported)
1669 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1670 &fan_proc_fops, dev);
1671 if (dev->hotkey_dev)
1672 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1673 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001674 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1675 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Seth Forshee36d03f92011-09-20 16:55:53 -05001678static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Seth Forshee36d03f92011-09-20 16:55:53 -05001680 if (dev->backlight_dev)
1681 remove_proc_entry("lcd", toshiba_proc_dir);
1682 if (dev->video_supported)
1683 remove_proc_entry("video", toshiba_proc_dir);
1684 if (dev->fan_supported)
1685 remove_proc_entry("fan", toshiba_proc_dir);
1686 if (dev->hotkey_dev)
1687 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001688 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Lionel Debrouxacc24722010-11-16 14:14:02 +01001691static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001692 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001693 .get_brightness = get_lcd_brightness,
1694 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001695};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001696
Azael Avalos65e3cf92015-11-23 10:51:30 -07001697/* Keyboard backlight work */
1698static void toshiba_acpi_kbd_bl_work(struct work_struct *work);
1699
1700static DECLARE_WORK(kbd_bl_work, toshiba_acpi_kbd_bl_work);
1701
Azael Avalos360f0f32014-03-25 20:38:31 -06001702/*
1703 * Sysfs files
1704 */
Azael Avalos9d309842015-02-10 23:43:59 -07001705static ssize_t version_show(struct device *dev,
1706 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001707{
1708 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1709}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001710static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001711
Azael Avalos9d309842015-02-10 23:43:59 -07001712static ssize_t fan_store(struct device *dev,
1713 struct device_attribute *attr,
1714 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001715{
1716 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avalos94477d42015-02-10 21:09:17 -07001717 int state;
1718 int ret;
1719
1720 ret = kstrtoint(buf, 0, &state);
1721 if (ret)
1722 return ret;
1723
1724 if (state != 0 && state != 1)
1725 return -EINVAL;
1726
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001727 ret = set_fan_status(toshiba, state);
1728 if (ret)
1729 return ret;
Azael Avalos94477d42015-02-10 21:09:17 -07001730
1731 return count;
1732}
1733
Azael Avalos9d309842015-02-10 23:43:59 -07001734static ssize_t fan_show(struct device *dev,
1735 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001736{
1737 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1738 u32 value;
1739 int ret;
1740
1741 ret = get_fan_status(toshiba, &value);
1742 if (ret)
1743 return ret;
1744
1745 return sprintf(buf, "%d\n", value);
1746}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001747static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001748
Azael Avalos9d309842015-02-10 23:43:59 -07001749static ssize_t kbd_backlight_mode_store(struct device *dev,
1750 struct device_attribute *attr,
1751 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001752{
1753 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001754 int mode;
Dan Carpenteraeaac092014-09-03 14:44:37 +03001755 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001756
Dan Carpenteraeaac092014-09-03 14:44:37 +03001757
1758 ret = kstrtoint(buf, 0, &mode);
1759 if (ret)
1760 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001761
1762 /* Check for supported modes depending on keyboard backlight type */
1763 if (toshiba->kbd_type == 1) {
1764 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1765 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1766 return -EINVAL;
1767 } else if (toshiba->kbd_type == 2) {
1768 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1769 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1770 mode != SCI_KBD_MODE_OFF)
1771 return -EINVAL;
1772 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001773
Darren Harte0769fe2015-02-11 20:50:08 -08001774 /*
1775 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001776 * Auto - KBD backlight turns off automatically in given time
1777 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001778 * ON - KBD backlight is always on
1779 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001780 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001781
1782 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001783 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001784 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos1e574db2015-07-22 19:37:49 -06001785 int time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001786
1787 /* OR the "base time" to the actual method format */
1788 if (toshiba->kbd_type == 1) {
1789 /* Type 1 requires the current mode */
1790 time |= toshiba->kbd_mode;
1791 } else if (toshiba->kbd_type == 2) {
1792 /* Type 2 requires the desired mode */
1793 time |= mode;
1794 }
1795
Dan Carpenteraeaac092014-09-03 14:44:37 +03001796 ret = toshiba_kbd_illum_status_set(toshiba, time);
1797 if (ret)
1798 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001799
Azael Avalos360f0f32014-03-25 20:38:31 -06001800 toshiba->kbd_mode = mode;
Azael Avalos65e3cf92015-11-23 10:51:30 -07001801
1802 /*
1803 * Some laptop models with the second generation backlit
1804 * keyboard (type 2) do not generate the keyboard backlight
1805 * changed event (0x92), and thus, the driver will never update
1806 * the sysfs entries.
1807 *
1808 * The event is generated right when changing the keyboard
1809 * backlight mode and the *notify function will set the
1810 * kbd_event_generated to true.
1811 *
1812 * In case the event is not generated, schedule the keyboard
1813 * backlight work to update the sysfs entries and emulate the
1814 * event via genetlink.
1815 */
1816 if (toshiba->kbd_type == 2 &&
1817 !toshiba_acpi->kbd_event_generated)
1818 schedule_work(&kbd_bl_work);
Azael Avalos360f0f32014-03-25 20:38:31 -06001819 }
1820
1821 return count;
1822}
1823
Azael Avalos9d309842015-02-10 23:43:59 -07001824static ssize_t kbd_backlight_mode_show(struct device *dev,
1825 struct device_attribute *attr,
1826 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001827{
1828 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1829 u32 time;
1830
1831 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1832 return -EIO;
1833
Azael Avalos93f8c162014-09-12 18:50:36 -06001834 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1835}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001836static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001837
Azael Avalos9d309842015-02-10 23:43:59 -07001838static ssize_t kbd_type_show(struct device *dev,
1839 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001840{
1841 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1842
1843 return sprintf(buf, "%d\n", toshiba->kbd_type);
1844}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001845static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001846
Azael Avalos9d309842015-02-10 23:43:59 -07001847static ssize_t available_kbd_modes_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001850{
1851 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1852
1853 if (toshiba->kbd_type == 1)
Azael Avalos0b498202015-09-09 11:30:09 -06001854 return sprintf(buf, "0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001855 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1856
Azael Avalos0b498202015-09-09 11:30:09 -06001857 return sprintf(buf, "0x%x 0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001858 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001859}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001860static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001861
Azael Avalos9d309842015-02-10 23:43:59 -07001862static ssize_t kbd_backlight_timeout_store(struct device *dev,
1863 struct device_attribute *attr,
1864 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001865{
1866 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001867 int time;
1868 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001869
Azael Avaloseabde0f2014-10-04 12:02:21 -06001870 ret = kstrtoint(buf, 0, &time);
1871 if (ret)
1872 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001873
Azael Avaloseabde0f2014-10-04 12:02:21 -06001874 /* Check for supported values depending on kbd_type */
1875 if (toshiba->kbd_type == 1) {
1876 if (time < 0 || time > 60)
1877 return -EINVAL;
1878 } else if (toshiba->kbd_type == 2) {
1879 if (time < 1 || time > 60)
1880 return -EINVAL;
1881 }
1882
1883 /* Set the Keyboard Backlight Timeout */
1884
1885 /* Only make a change if the actual timeout has changed */
1886 if (toshiba->kbd_time != time) {
1887 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001888 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001889 /* OR the "base time" to the actual method format */
1890 if (toshiba->kbd_type == 1)
1891 time |= SCI_KBD_MODE_FNZ;
1892 else if (toshiba->kbd_type == 2)
1893 time |= SCI_KBD_MODE_AUTO;
1894
1895 ret = toshiba_kbd_illum_status_set(toshiba, time);
1896 if (ret)
1897 return ret;
1898
Azael Avalos360f0f32014-03-25 20:38:31 -06001899 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1900 }
1901
1902 return count;
1903}
1904
Azael Avalos9d309842015-02-10 23:43:59 -07001905static ssize_t kbd_backlight_timeout_show(struct device *dev,
1906 struct device_attribute *attr,
1907 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001908{
1909 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1910 u32 time;
1911
1912 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1913 return -EIO;
1914
1915 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1916}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001917static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001918
Azael Avalos9d309842015-02-10 23:43:59 -07001919static ssize_t touchpad_store(struct device *dev,
1920 struct device_attribute *attr,
1921 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001922{
1923 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1924 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001925 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001926
1927 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001928 ret = kstrtoint(buf, 0, &state);
1929 if (ret)
1930 return ret;
1931 if (state != 0 && state != 1)
1932 return -EINVAL;
1933
1934 ret = toshiba_touchpad_set(toshiba, state);
1935 if (ret)
1936 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001937
1938 return count;
1939}
1940
Azael Avalos9d309842015-02-10 23:43:59 -07001941static ssize_t touchpad_show(struct device *dev,
1942 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001943{
1944 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1945 u32 state;
1946 int ret;
1947
1948 ret = toshiba_touchpad_get(toshiba, &state);
1949 if (ret < 0)
1950 return ret;
1951
1952 return sprintf(buf, "%i\n", state);
1953}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001954static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001955
Azael Avalos9d309842015-02-10 23:43:59 -07001956static ssize_t position_show(struct device *dev,
1957 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001958{
1959 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1960 u32 xyval, zval, tmp;
1961 u16 x, y, z;
1962 int ret;
1963
1964 xyval = zval = 0;
1965 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1966 if (ret < 0)
1967 return ret;
1968
1969 x = xyval & HCI_ACCEL_MASK;
1970 tmp = xyval >> HCI_MISC_SHIFT;
1971 y = tmp & HCI_ACCEL_MASK;
1972 z = zval & HCI_ACCEL_MASK;
1973
1974 return sprintf(buf, "%d %d %d\n", x, y, z);
1975}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001976static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001977
Azael Avalos9d309842015-02-10 23:43:59 -07001978static ssize_t usb_sleep_charge_show(struct device *dev,
1979 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001980{
1981 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1982 u32 mode;
1983 int ret;
1984
1985 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1986 if (ret < 0)
1987 return ret;
1988
1989 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1990}
1991
Azael Avalos9d309842015-02-10 23:43:59 -07001992static ssize_t usb_sleep_charge_store(struct device *dev,
1993 struct device_attribute *attr,
1994 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001995{
1996 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1997 u32 mode;
1998 int state;
1999 int ret;
2000
2001 ret = kstrtoint(buf, 0, &state);
2002 if (ret)
2003 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002004 /*
2005 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07002006 * 0 - Disabled
2007 * 1 - Alternate (Non USB conformant devices that require more power)
2008 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06002009 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07002010 */
Azael Avalosc8c91842015-04-02 19:26:20 -06002011 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07002012 return -EINVAL;
2013
2014 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06002015 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07002016 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06002017 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07002018 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06002019 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07002020 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06002021 mode |= SCI_USB_CHARGE_AUTO;
2022 else if (state == 3)
2023 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07002024
2025 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2026 if (ret)
2027 return ret;
2028
2029 return count;
2030}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002031static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07002032
Azael Avalos182bcaa2015-01-18 18:30:23 -07002033static ssize_t sleep_functions_on_battery_show(struct device *dev,
2034 struct device_attribute *attr,
2035 char *buf)
2036{
2037 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2038 u32 state;
2039 int bat_lvl;
2040 int status;
2041 int ret;
2042 int tmp;
2043
2044 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2045 if (ret < 0)
2046 return ret;
2047
2048 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2049 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2050 status = (tmp == 0x4) ? 1 : 0;
2051 /* Determine the battery level set */
2052 bat_lvl = state >> HCI_MISC_SHIFT;
2053
2054 return sprintf(buf, "%d %d\n", status, bat_lvl);
2055}
2056
2057static ssize_t sleep_functions_on_battery_store(struct device *dev,
2058 struct device_attribute *attr,
2059 const char *buf, size_t count)
2060{
2061 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2062 u32 status;
2063 int value;
2064 int ret;
2065 int tmp;
2066
2067 ret = kstrtoint(buf, 0, &value);
2068 if (ret)
2069 return ret;
2070
Darren Harte0769fe2015-02-11 20:50:08 -08002071 /*
2072 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002073 * 0 - Disabled
2074 * 1-100 - Enabled
2075 */
2076 if (value < 0 || value > 100)
2077 return -EINVAL;
2078
2079 if (value == 0) {
2080 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2081 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2082 } else {
2083 tmp = value << HCI_MISC_SHIFT;
2084 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2085 }
2086 ret = toshiba_sleep_functions_status_set(toshiba, status);
2087 if (ret < 0)
2088 return ret;
2089
2090 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2091
2092 return count;
2093}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002094static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002095
Azael Avalos9d309842015-02-10 23:43:59 -07002096static ssize_t usb_rapid_charge_show(struct device *dev,
2097 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002098{
2099 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2100 u32 state;
2101 int ret;
2102
2103 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2104 if (ret < 0)
2105 return ret;
2106
2107 return sprintf(buf, "%d\n", state);
2108}
2109
Azael Avalos9d309842015-02-10 23:43:59 -07002110static ssize_t usb_rapid_charge_store(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002113{
2114 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2115 int state;
2116 int ret;
2117
2118 ret = kstrtoint(buf, 0, &state);
2119 if (ret)
2120 return ret;
2121 if (state != 0 && state != 1)
2122 return -EINVAL;
2123
2124 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2125 if (ret)
2126 return ret;
2127
2128 return count;
2129}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002130static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002131
Azael Avalos9d309842015-02-10 23:43:59 -07002132static ssize_t usb_sleep_music_show(struct device *dev,
2133 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002134{
2135 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2136 u32 state;
2137 int ret;
2138
2139 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2140 if (ret < 0)
2141 return ret;
2142
2143 return sprintf(buf, "%d\n", state);
2144}
2145
Azael Avalos9d309842015-02-10 23:43:59 -07002146static ssize_t usb_sleep_music_store(struct device *dev,
2147 struct device_attribute *attr,
2148 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002149{
2150 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2151 int state;
2152 int ret;
2153
2154 ret = kstrtoint(buf, 0, &state);
2155 if (ret)
2156 return ret;
2157 if (state != 0 && state != 1)
2158 return -EINVAL;
2159
2160 ret = toshiba_usb_sleep_music_set(toshiba, state);
2161 if (ret)
2162 return ret;
2163
2164 return count;
2165}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002166static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002167
Azael Avalos9d309842015-02-10 23:43:59 -07002168static ssize_t kbd_function_keys_show(struct device *dev,
2169 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002170{
2171 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2172 int mode;
2173 int ret;
2174
2175 ret = toshiba_function_keys_get(toshiba, &mode);
2176 if (ret < 0)
2177 return ret;
2178
2179 return sprintf(buf, "%d\n", mode);
2180}
2181
Azael Avalos9d309842015-02-10 23:43:59 -07002182static ssize_t kbd_function_keys_store(struct device *dev,
2183 struct device_attribute *attr,
2184 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002185{
2186 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2187 int mode;
2188 int ret;
2189
2190 ret = kstrtoint(buf, 0, &mode);
2191 if (ret)
2192 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002193 /*
2194 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002195 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2196 * 1 - Special functions (Opposite of the above setting)
2197 */
2198 if (mode != 0 && mode != 1)
2199 return -EINVAL;
2200
2201 ret = toshiba_function_keys_set(toshiba, mode);
2202 if (ret)
2203 return ret;
2204
2205 pr_info("Reboot for changes to KBD Function Keys to take effect");
2206
2207 return count;
2208}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002209static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002210
Azael Avalos9d309842015-02-10 23:43:59 -07002211static ssize_t panel_power_on_show(struct device *dev,
2212 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002213{
2214 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2215 u32 state;
2216 int ret;
2217
2218 ret = toshiba_panel_power_on_get(toshiba, &state);
2219 if (ret < 0)
2220 return ret;
2221
2222 return sprintf(buf, "%d\n", state);
2223}
2224
Azael Avalos9d309842015-02-10 23:43:59 -07002225static ssize_t panel_power_on_store(struct device *dev,
2226 struct device_attribute *attr,
2227 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002228{
2229 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2230 int state;
2231 int ret;
2232
2233 ret = kstrtoint(buf, 0, &state);
2234 if (ret)
2235 return ret;
2236 if (state != 0 && state != 1)
2237 return -EINVAL;
2238
2239 ret = toshiba_panel_power_on_set(toshiba, state);
2240 if (ret)
2241 return ret;
2242
2243 pr_info("Reboot for changes to Panel Power ON to take effect");
2244
2245 return count;
2246}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002247static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002248
Azael Avalos9d309842015-02-10 23:43:59 -07002249static ssize_t usb_three_show(struct device *dev,
2250 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002251{
2252 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2253 u32 state;
2254 int ret;
2255
2256 ret = toshiba_usb_three_get(toshiba, &state);
2257 if (ret < 0)
2258 return ret;
2259
2260 return sprintf(buf, "%d\n", state);
2261}
2262
Azael Avalos9d309842015-02-10 23:43:59 -07002263static ssize_t usb_three_store(struct device *dev,
2264 struct device_attribute *attr,
2265 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002266{
2267 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2268 int state;
2269 int ret;
2270
2271 ret = kstrtoint(buf, 0, &state);
2272 if (ret)
2273 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002274 /*
2275 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002276 * 0 - Disabled (Acts like a USB 2 port, saving power)
2277 * 1 - Enabled
2278 */
2279 if (state != 0 && state != 1)
2280 return -EINVAL;
2281
2282 ret = toshiba_usb_three_set(toshiba, state);
2283 if (ret)
2284 return ret;
2285
2286 pr_info("Reboot for changes to USB 3 to take effect");
2287
2288 return count;
2289}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002290static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002291
Azael Avalosb1009b92016-01-25 11:29:11 -07002292static ssize_t cooling_method_show(struct device *dev,
2293 struct device_attribute *attr, char *buf)
2294{
2295 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2296 int state;
2297 int ret;
2298
2299 ret = toshiba_cooling_method_get(toshiba, &state);
2300 if (ret < 0)
2301 return ret;
2302
2303 return sprintf(buf, "%d %d\n", state, toshiba->max_cooling_method);
2304}
2305
2306static ssize_t cooling_method_store(struct device *dev,
2307 struct device_attribute *attr,
2308 const char *buf, size_t count)
2309{
2310 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2311 int state;
2312 int ret;
2313
2314 ret = kstrtoint(buf, 0, &state);
2315 if (ret)
2316 return ret;
2317
2318 /*
2319 * Check for supported values
2320 * Depending on the laptop model, some only support these two:
2321 * 0 - Maximum Performance
2322 * 1 - Battery Optimized
2323 *
2324 * While some others support all three methods:
2325 * 0 - Maximum Performance
2326 * 1 - Performance
2327 * 2 - Battery Optimized
2328 */
2329 if (state < 0 || state > toshiba->max_cooling_method)
2330 return -EINVAL;
2331
2332 ret = toshiba_cooling_method_set(toshiba, state);
2333 if (ret)
2334 return ret;
2335
2336 return count;
2337}
2338static DEVICE_ATTR_RW(cooling_method);
2339
Azael Avalos9bd12132015-02-10 23:43:58 -07002340static struct attribute *toshiba_attributes[] = {
2341 &dev_attr_version.attr,
2342 &dev_attr_fan.attr,
2343 &dev_attr_kbd_backlight_mode.attr,
2344 &dev_attr_kbd_type.attr,
2345 &dev_attr_available_kbd_modes.attr,
2346 &dev_attr_kbd_backlight_timeout.attr,
2347 &dev_attr_touchpad.attr,
2348 &dev_attr_position.attr,
2349 &dev_attr_usb_sleep_charge.attr,
2350 &dev_attr_sleep_functions_on_battery.attr,
2351 &dev_attr_usb_rapid_charge.attr,
2352 &dev_attr_usb_sleep_music.attr,
2353 &dev_attr_kbd_function_keys.attr,
2354 &dev_attr_panel_power_on.attr,
2355 &dev_attr_usb_three.attr,
Azael Avalosb1009b92016-01-25 11:29:11 -07002356 &dev_attr_cooling_method.attr,
Azael Avalos9bd12132015-02-10 23:43:58 -07002357 NULL,
2358};
2359
Azael Avalos360f0f32014-03-25 20:38:31 -06002360static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2361 struct attribute *attr, int idx)
2362{
2363 struct device *dev = container_of(kobj, struct device, kobj);
2364 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2365 bool exists = true;
2366
Azael Avalos94477d42015-02-10 21:09:17 -07002367 if (attr == &dev_attr_fan.attr)
2368 exists = (drv->fan_supported) ? true : false;
2369 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002370 exists = (drv->kbd_illum_supported) ? true : false;
2371 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2372 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002373 else if (attr == &dev_attr_touchpad.attr)
2374 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002375 else if (attr == &dev_attr_position.attr)
2376 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002377 else if (attr == &dev_attr_usb_sleep_charge.attr)
2378 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002379 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2380 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002381 else if (attr == &dev_attr_usb_rapid_charge.attr)
2382 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002383 else if (attr == &dev_attr_usb_sleep_music.attr)
2384 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002385 else if (attr == &dev_attr_kbd_function_keys.attr)
2386 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002387 else if (attr == &dev_attr_panel_power_on.attr)
2388 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002389 else if (attr == &dev_attr_usb_three.attr)
2390 exists = (drv->usb_three_supported) ? true : false;
Azael Avalosb1009b92016-01-25 11:29:11 -07002391 else if (attr == &dev_attr_cooling_method.attr)
2392 exists = (drv->cooling_method_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002393
2394 return exists ? attr->mode : 0;
2395}
2396
Azael Avalos9bd12132015-02-10 23:43:58 -07002397static struct attribute_group toshiba_attr_group = {
2398 .is_visible = toshiba_sysfs_is_visible,
2399 .attrs = toshiba_attributes,
2400};
2401
Azael Avalos65e3cf92015-11-23 10:51:30 -07002402static void toshiba_acpi_kbd_bl_work(struct work_struct *work)
2403{
2404 struct acpi_device *acpi_dev = toshiba_acpi->acpi_dev;
2405
2406 /* Update the sysfs entries */
2407 if (sysfs_update_group(&acpi_dev->dev.kobj,
2408 &toshiba_attr_group))
2409 pr_err("Unable to update sysfs entries\n");
2410
2411 /* Emulate the keyboard backlight event */
2412 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2413 dev_name(&acpi_dev->dev),
2414 0x92, 0);
2415}
2416
Azael Avalos1f28f292014-12-04 20:22:45 -07002417/*
Azael Avalosfc5462f2015-07-22 18:09:11 -06002418 * Misc device
2419 */
2420static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2421{
2422 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2423 regs->edx, regs->esi, regs->edi };
2424 u32 out[TCI_WORDS];
2425 acpi_status status;
2426
2427 status = tci_raw(toshiba_acpi, in, out);
2428 if (ACPI_FAILURE(status)) {
2429 pr_err("ACPI call to query SMM registers failed\n");
2430 return -EIO;
2431 }
2432
2433 /* Fillout the SMM struct with the TCI call results */
2434 regs->eax = out[0];
2435 regs->ebx = out[1];
2436 regs->ecx = out[2];
2437 regs->edx = out[3];
2438 regs->esi = out[4];
2439 regs->edi = out[5];
2440
2441 return 0;
2442}
2443
2444static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2445 unsigned long arg)
2446{
2447 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2448 SMMRegisters regs;
2449 int ret;
2450
2451 if (!argp)
2452 return -EINVAL;
2453
2454 switch (cmd) {
2455 case TOSH_SMM:
2456 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2457 return -EFAULT;
2458 ret = toshiba_acpi_smm_bridge(&regs);
2459 if (ret)
2460 return ret;
2461 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2462 return -EFAULT;
2463 break;
2464 case TOSHIBA_ACPI_SCI:
2465 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2466 return -EFAULT;
2467 /* Ensure we are being called with a SCI_{GET, SET} register */
2468 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2469 return -EINVAL;
2470 if (!sci_open(toshiba_acpi))
2471 return -EIO;
2472 ret = toshiba_acpi_smm_bridge(&regs);
2473 sci_close(toshiba_acpi);
2474 if (ret)
2475 return ret;
2476 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2477 return -EFAULT;
2478 break;
2479 default:
2480 return -EINVAL;
2481 }
2482
2483 return 0;
2484}
2485
2486static const struct file_operations toshiba_acpi_fops = {
2487 .owner = THIS_MODULE,
2488 .unlocked_ioctl = toshiba_acpi_ioctl,
2489 .llseek = noop_llseek,
2490};
2491
2492/*
Azael Avalos2fdde832015-11-23 10:49:11 -07002493 * WWAN RFKill handlers
2494 */
2495static int toshiba_acpi_wwan_set_block(void *data, bool blocked)
2496{
2497 struct toshiba_acpi_dev *dev = data;
2498 int ret;
2499
2500 ret = toshiba_wireless_status(dev);
2501 if (ret)
2502 return ret;
2503
2504 if (!dev->killswitch)
2505 return 0;
2506
2507 return toshiba_wwan_set(dev, !blocked);
2508}
2509
2510static void toshiba_acpi_wwan_poll(struct rfkill *rfkill, void *data)
2511{
2512 struct toshiba_acpi_dev *dev = data;
2513
2514 if (toshiba_wireless_status(dev))
2515 return;
2516
2517 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2518}
2519
2520static const struct rfkill_ops wwan_rfk_ops = {
2521 .set_block = toshiba_acpi_wwan_set_block,
2522 .poll = toshiba_acpi_wwan_poll,
2523};
2524
2525static int toshiba_acpi_setup_wwan_rfkill(struct toshiba_acpi_dev *dev)
2526{
2527 int ret = toshiba_wireless_status(dev);
2528
2529 if (ret)
2530 return ret;
2531
2532 dev->wwan_rfk = rfkill_alloc("Toshiba WWAN",
2533 &dev->acpi_dev->dev,
2534 RFKILL_TYPE_WWAN,
2535 &wwan_rfk_ops,
2536 dev);
2537 if (!dev->wwan_rfk) {
2538 pr_err("Unable to allocate WWAN rfkill device\n");
2539 return -ENOMEM;
2540 }
2541
2542 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2543
2544 ret = rfkill_register(dev->wwan_rfk);
2545 if (ret) {
2546 pr_err("Unable to register WWAN rfkill device\n");
2547 rfkill_destroy(dev->wwan_rfk);
2548 }
2549
2550 return ret;
2551}
2552
2553/*
Azael Avalos1f28f292014-12-04 20:22:45 -07002554 * Hotkeys
2555 */
2556static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2557{
2558 acpi_status status;
2559 u32 result;
2560
2561 status = acpi_evaluate_object(dev->acpi_dev->handle,
2562 "ENAB", NULL, NULL);
2563 if (ACPI_FAILURE(status))
2564 return -ENODEV;
2565
Azael Avalosb116fd002015-09-09 11:28:19 -06002566 /*
2567 * Enable the "Special Functions" mode only if they are
2568 * supported and if they are activated.
2569 */
2570 if (dev->kbd_function_keys_supported && dev->special_functions)
2571 result = hci_write(dev, HCI_HOTKEY_EVENT,
2572 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2573 else
2574 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2575
Azael Avalos1f28f292014-12-04 20:22:45 -07002576 if (result == TOS_FAILURE)
2577 return -EIO;
2578 else if (result == TOS_NOT_SUPPORTED)
2579 return -ENODEV;
2580
2581 return 0;
2582}
2583
Seth Forshee29cd2932012-01-18 13:44:09 -06002584static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2585 struct serio *port)
2586{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002587 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002588 return false;
2589
2590 if (unlikely(data == 0xe0))
2591 return false;
2592
2593 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2594 schedule_work(&toshiba_acpi->hotkey_work);
2595 return true;
2596 }
2597
2598 return false;
2599}
2600
2601static void toshiba_acpi_hotkey_work(struct work_struct *work)
2602{
2603 acpi_handle ec_handle = ec_get_handle();
2604 acpi_status status;
2605
2606 if (!ec_handle)
2607 return;
2608
2609 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2610 if (ACPI_FAILURE(status))
2611 pr_err("ACPI NTFY method execution failed\n");
2612}
2613
2614/*
2615 * Returns hotkey scancode, or < 0 on failure.
2616 */
2617static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2618{
Zhang Rui74facaf2013-09-03 08:32:15 +08002619 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002620 acpi_status status;
2621
Zhang Rui74facaf2013-09-03 08:32:15 +08002622 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2623 NULL, &value);
2624 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002625 pr_err("ACPI INFO method execution failed\n");
2626 return -EIO;
2627 }
2628
Zhang Rui74facaf2013-09-03 08:32:15 +08002629 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002630}
2631
2632static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2633 int scancode)
2634{
2635 if (scancode == 0x100)
2636 return;
2637
Darren Harte0769fe2015-02-11 20:50:08 -08002638 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002639 if (scancode & 0x80)
2640 return;
2641
2642 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2643 pr_info("Unknown key %x\n", scancode);
2644}
2645
Azael Avalos71454d72014-12-04 20:22:46 -07002646static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2647{
Azael Avalos71454d72014-12-04 20:22:46 -07002648 if (dev->info_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002649 int scancode = toshiba_acpi_query_hotkey(dev);
2650
2651 if (scancode < 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002652 pr_err("Failed to query hotkey event\n");
Azael Avalos7deef552015-07-22 18:09:10 -06002653 } else if (scancode != 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002654 toshiba_acpi_report_hotkey(dev, scancode);
Azael Avalos7deef552015-07-22 18:09:10 -06002655 dev->key_event_valid = 1;
2656 dev->last_key_event = scancode;
2657 }
Azael Avalos71454d72014-12-04 20:22:46 -07002658 } else if (dev->system_event_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002659 u32 result;
2660 u32 value;
2661 int retries = 3;
2662
Azael Avalos71454d72014-12-04 20:22:46 -07002663 do {
Azael Avalos7deef552015-07-22 18:09:10 -06002664 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2665 switch (result) {
Azael Avalos71454d72014-12-04 20:22:46 -07002666 case TOS_SUCCESS:
2667 toshiba_acpi_report_hotkey(dev, (int)value);
Azael Avalos7deef552015-07-22 18:09:10 -06002668 dev->key_event_valid = 1;
2669 dev->last_key_event = value;
Azael Avalos71454d72014-12-04 20:22:46 -07002670 break;
2671 case TOS_NOT_SUPPORTED:
2672 /*
2673 * This is a workaround for an unresolved
2674 * issue on some machines where system events
2675 * sporadically become disabled.
2676 */
Azael Avalos7deef552015-07-22 18:09:10 -06002677 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2678 if (result == TOS_SUCCESS)
2679 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002680 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002681 default:
2682 retries--;
2683 break;
2684 }
Azael Avalos7deef552015-07-22 18:09:10 -06002685 } while (retries && result != TOS_FIFO_EMPTY);
Azael Avalos71454d72014-12-04 20:22:46 -07002686 }
2687}
2688
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002689static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002690{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002691 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002692 acpi_handle ec_handle;
Azael Avalosa2b34712015-03-20 16:55:17 -06002693 int error;
2694
Azael Avalosa88bc062015-07-22 18:09:12 -06002695 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2696 pr_info("WMI event detected, hotkeys will not be monitored\n");
2697 return 0;
2698 }
2699
Azael Avalosa2b34712015-03-20 16:55:17 -06002700 error = toshiba_acpi_enable_hotkeys(dev);
2701 if (error)
2702 return error;
2703
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002704 if (toshiba_hotkey_event_type_get(dev, &dev->hotkey_event_type))
Azael Avalos53147b62015-09-09 11:25:45 -06002705 pr_notice("Unable to query Hotkey Event Type\n");
2706
Seth Forshee135740d2011-09-20 16:55:49 -05002707 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002708 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002709 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002710
2711 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002712 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002713 dev->hotkey_dev->id.bustype = BUS_HOST;
2714
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002715 if (dev->hotkey_event_type == HCI_SYSTEM_TYPE1 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002716 !dev->kbd_function_keys_supported)
2717 keymap = toshiba_acpi_keymap;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002718 else if (dev->hotkey_event_type == HCI_SYSTEM_TYPE2 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002719 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002720 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002721 else
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002722 pr_info("Unknown event type received %x\n",
2723 dev->hotkey_event_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002724 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002725 if (error)
2726 goto err_free_dev;
2727
Seth Forshee29cd2932012-01-18 13:44:09 -06002728 /*
2729 * For some machines the SCI responsible for providing hotkey
2730 * notification doesn't fire. We can trigger the notification
2731 * whenever the Fn key is pressed using the NTFY method, if
2732 * supported, so if it's present set up an i8042 key filter
2733 * for this purpose.
2734 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002735 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002736 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002737 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2738
2739 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2740 if (error) {
2741 pr_err("Error installing key filter\n");
2742 goto err_free_keymap;
2743 }
2744
2745 dev->ntfy_supported = 1;
2746 }
2747
2748 /*
2749 * Determine hotkey query interface. Prefer using the INFO
2750 * method when it is available.
2751 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002752 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002753 dev->info_supported = 1;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002754 else if (hci_write(dev, HCI_SYSTEM_EVENT, 1) == TOS_SUCCESS)
2755 dev->system_event_supported = 1;
Seth Forshee29cd2932012-01-18 13:44:09 -06002756
2757 if (!dev->info_supported && !dev->system_event_supported) {
2758 pr_warn("No hotkey query interface found\n");
2759 goto err_remove_filter;
2760 }
2761
Seth Forshee135740d2011-09-20 16:55:49 -05002762 error = input_register_device(dev->hotkey_dev);
2763 if (error) {
2764 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002765 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002766 }
2767
2768 return 0;
2769
Seth Forshee29cd2932012-01-18 13:44:09 -06002770 err_remove_filter:
2771 if (dev->ntfy_supported)
2772 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002773 err_free_keymap:
2774 sparse_keymap_free(dev->hotkey_dev);
2775 err_free_dev:
2776 input_free_device(dev->hotkey_dev);
2777 dev->hotkey_dev = NULL;
2778 return error;
2779}
2780
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002781static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002782{
2783 struct backlight_properties props;
2784 int brightness;
2785 int ret;
2786
2787 /*
2788 * Some machines don't support the backlight methods at all, and
2789 * others support it read-only. Either of these is pretty useless,
2790 * so only register the backlight device if the backlight method
2791 * supports both reads and writes.
2792 */
2793 brightness = __get_lcd_brightness(dev);
2794 if (brightness < 0)
2795 return 0;
Azael Avalosbae53362015-11-15 20:32:47 -07002796 /*
2797 * If transflective backlight is supported and the brightness is zero
2798 * (lowest brightness level), the set_lcd_brightness function will
2799 * activate the transflective backlight, making the LCD appear to be
2800 * turned off, simply increment the brightness level to avoid that.
2801 */
2802 if (dev->tr_backlight_supported && brightness == 0)
2803 brightness++;
Seth Forshee62cce752012-04-19 11:23:50 -05002804 ret = set_lcd_brightness(dev, brightness);
2805 if (ret) {
2806 pr_debug("Backlight method is read-only, disabling backlight support\n");
2807 return 0;
2808 }
2809
Hans de Goede358d6a22015-04-21 12:01:32 +02002810 /*
2811 * Tell acpi-video-detect code to prefer vendor backlight on all
2812 * systems with transflective backlight and on dmi matched systems.
2813 */
2814 if (dev->tr_backlight_supported ||
2815 dmi_check_system(toshiba_vendor_backlight_dmi))
Hans de Goede234b7cf2015-06-16 16:28:11 +02002816 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
Hans de Goede358d6a22015-04-21 12:01:32 +02002817
Hans de Goede234b7cf2015-06-16 16:28:11 +02002818 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
Hans de Goede358d6a22015-04-21 12:01:32 +02002819 return 0;
2820
Matthew Garrett53039f22012-06-01 11:02:36 -04002821 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002822 props.type = BACKLIGHT_PLATFORM;
2823 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002824
Darren Harte0769fe2015-02-11 20:50:08 -08002825 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002826 if (dev->tr_backlight_supported)
2827 props.max_brightness++;
2828
Seth Forshee62cce752012-04-19 11:23:50 -05002829 dev->backlight_dev = backlight_device_register("toshiba",
2830 &dev->acpi_dev->dev,
2831 dev,
2832 &toshiba_backlight_data,
2833 &props);
2834 if (IS_ERR(dev->backlight_dev)) {
2835 ret = PTR_ERR(dev->backlight_dev);
2836 pr_err("Could not register toshiba backlight device\n");
2837 dev->backlight_dev = NULL;
2838 return ret;
2839 }
2840
2841 dev->backlight_dev->props.brightness = brightness;
2842 return 0;
2843}
2844
Azael Avalos0409cbc2015-07-31 21:58:13 -06002845static void print_supported_features(struct toshiba_acpi_dev *dev)
2846{
2847 pr_info("Supported laptop features:");
2848
2849 if (dev->hotkey_dev)
2850 pr_cont(" hotkeys");
2851 if (dev->backlight_dev)
2852 pr_cont(" backlight");
2853 if (dev->video_supported)
2854 pr_cont(" video-out");
2855 if (dev->fan_supported)
2856 pr_cont(" fan");
2857 if (dev->tr_backlight_supported)
2858 pr_cont(" transflective-backlight");
2859 if (dev->illumination_supported)
2860 pr_cont(" illumination");
2861 if (dev->kbd_illum_supported)
2862 pr_cont(" keyboard-backlight");
2863 if (dev->touchpad_supported)
2864 pr_cont(" touchpad");
2865 if (dev->eco_supported)
2866 pr_cont(" eco-led");
2867 if (dev->accelerometer_supported)
2868 pr_cont(" accelerometer-axes");
2869 if (dev->usb_sleep_charge_supported)
2870 pr_cont(" usb-sleep-charge");
2871 if (dev->usb_rapid_charge_supported)
2872 pr_cont(" usb-rapid-charge");
2873 if (dev->usb_sleep_music_supported)
2874 pr_cont(" usb-sleep-music");
2875 if (dev->kbd_function_keys_supported)
2876 pr_cont(" special-function-keys");
2877 if (dev->panel_power_on_supported)
2878 pr_cont(" panel-power-on");
2879 if (dev->usb_three_supported)
2880 pr_cont(" usb3");
Azael Avalos6873f462015-11-23 10:49:10 -07002881 if (dev->wwan_supported)
2882 pr_cont(" wwan");
Azael Avalos763ff322016-01-25 11:29:10 -07002883 if (dev->cooling_method_supported)
2884 pr_cont(" cooling-method");
Azael Avalos0409cbc2015-07-31 21:58:13 -06002885
2886 pr_cont("\n");
2887}
2888
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002889static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002890{
2891 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2892
Azael Avalosfc5462f2015-07-22 18:09:11 -06002893 misc_deregister(&dev->miscdev);
2894
Seth Forshee36d03f92011-09-20 16:55:53 -05002895 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002896
Azael Avalos360f0f32014-03-25 20:38:31 -06002897 if (dev->sysfs_created)
2898 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2899 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002900
Seth Forshee29cd2932012-01-18 13:44:09 -06002901 if (dev->ntfy_supported) {
2902 i8042_remove_filter(toshiba_acpi_i8042_filter);
2903 cancel_work_sync(&dev->hotkey_work);
2904 }
2905
Seth Forshee135740d2011-09-20 16:55:49 -05002906 if (dev->hotkey_dev) {
2907 input_unregister_device(dev->hotkey_dev);
2908 sparse_keymap_free(dev->hotkey_dev);
2909 }
2910
Markus Elfring00981812014-11-24 20:30:29 +01002911 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002912
Azael Avalosea215a3f2015-07-31 21:58:12 -06002913 if (dev->illumination_led_registered)
Seth Forshee135740d2011-09-20 16:55:49 -05002914 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002915
Azael Avalos360f0f32014-03-25 20:38:31 -06002916 if (dev->kbd_led_registered)
2917 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002918
Azael Avalosea215a3f2015-07-31 21:58:12 -06002919 if (dev->eco_led_registered)
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002920 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002921
Azael Avalos2fdde832015-11-23 10:49:11 -07002922 if (dev->wwan_rfk) {
2923 rfkill_unregister(dev->wwan_rfk);
2924 rfkill_destroy(dev->wwan_rfk);
2925 }
2926
Seth Forshee29cd2932012-01-18 13:44:09 -06002927 if (toshiba_acpi)
2928 toshiba_acpi = NULL;
2929
Seth Forshee135740d2011-09-20 16:55:49 -05002930 kfree(dev);
2931
2932 return 0;
2933}
2934
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002935static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002936{
Zhang Ruie2e19602013-09-03 08:32:06 +08002937 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002938 return "GHCI";
2939
Zhang Ruie2e19602013-09-03 08:32:06 +08002940 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002941 return "SPFC";
2942
2943 return NULL;
2944}
2945
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002946static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002947{
2948 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002949 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002950 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002951 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002952
Seth Forshee29cd2932012-01-18 13:44:09 -06002953 if (toshiba_acpi)
2954 return -EBUSY;
2955
Seth Forshee135740d2011-09-20 16:55:49 -05002956 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2957 TOSHIBA_ACPI_VERSION);
2958
Seth Forsheea540d6b2011-09-20 16:55:52 -05002959 hci_method = find_hci_method(acpi_dev->handle);
2960 if (!hci_method) {
2961 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002962 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002963 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002964
Seth Forshee135740d2011-09-20 16:55:49 -05002965 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2966 if (!dev)
2967 return -ENOMEM;
2968 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002969 dev->method_hci = hci_method;
Azael Avalosfc5462f2015-07-22 18:09:11 -06002970 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2971 dev->miscdev.name = "toshiba_acpi";
2972 dev->miscdev.fops = &toshiba_acpi_fops;
2973
2974 ret = misc_register(&dev->miscdev);
2975 if (ret) {
2976 pr_err("Failed to register miscdevice\n");
2977 kfree(dev);
2978 return ret;
2979 }
2980
Seth Forshee135740d2011-09-20 16:55:49 -05002981 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002982 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002983
Azael Avalosa2b34712015-03-20 16:55:17 -06002984 /* Query the BIOS for supported features */
2985
2986 /*
2987 * The "Special Functions" are always supported by the laptops
2988 * with the new keyboard layout, query for its presence to help
2989 * determine the keymap layout to use.
2990 */
Azael Avalosb116fd002015-09-09 11:28:19 -06002991 ret = toshiba_function_keys_get(dev, &dev->special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002992 dev->kbd_function_keys_supported = !ret;
2993
Azael Avalosd2f20612015-11-04 09:28:26 -07002994 dev->hotkey_event_type = 0;
Seth Forshee6e02cc72011-09-20 16:55:51 -05002995 if (toshiba_acpi_setup_keyboard(dev))
2996 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002997
Azael Avalos695f6062015-07-22 18:09:13 -06002998 /* Determine whether or not BIOS supports transflective backlight */
2999 ret = get_tr_backlight_status(dev, &dummy);
3000 dev->tr_backlight_supported = !ret;
3001
Seth Forshee62cce752012-04-19 11:23:50 -05003002 ret = toshiba_acpi_setup_backlight(dev);
3003 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05003004 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05003005
Azael Avalosea215a3f2015-07-31 21:58:12 -06003006 toshiba_illumination_available(dev);
3007 if (dev->illumination_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05003008 dev->led_dev.name = "toshiba::illumination";
3009 dev->led_dev.max_brightness = 1;
3010 dev->led_dev.brightness_set = toshiba_illumination_set;
3011 dev->led_dev.brightness_get = toshiba_illumination_get;
3012 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -06003013 dev->illumination_led_registered = true;
Seth Forshee135740d2011-09-20 16:55:49 -05003014 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04003015
Azael Avalosea215a3f2015-07-31 21:58:12 -06003016 toshiba_eco_mode_available(dev);
3017 if (dev->eco_supported) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -06003018 dev->eco_led.name = "toshiba::eco_mode";
3019 dev->eco_led.max_brightness = 1;
3020 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
3021 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
3022 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06003023 dev->eco_led_registered = true;
Azael Avalosdef6c4e2014-03-25 20:38:33 -06003024 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04003025
Azael Avalosea215a3f2015-07-31 21:58:12 -06003026 toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06003027 /*
3028 * Only register the LED if KBD illumination is supported
3029 * and the keyboard backlight operation mode is set to FN-Z
3030 */
3031 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
3032 dev->kbd_led.name = "toshiba::kbd_backlight";
3033 dev->kbd_led.max_brightness = 1;
3034 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
3035 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
3036 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06003037 dev->kbd_led_registered = true;
Azael Avalos360f0f32014-03-25 20:38:31 -06003038 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04003039
Azael Avalos9d8658a2014-03-25 20:38:32 -06003040 ret = toshiba_touchpad_get(dev, &dummy);
3041 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04003042
Azael Avalosea215a3f2015-07-31 21:58:12 -06003043 toshiba_accelerometer_available(dev);
Seth Forshee135740d2011-09-20 16:55:49 -05003044
Azael Avalosc8c91842015-04-02 19:26:20 -06003045 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07003046
Azael Avalosbb3fe012015-01-18 18:30:24 -07003047 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
3048 dev->usb_rapid_charge_supported = !ret;
3049
Azael Avalos172ce0a2015-01-18 18:30:25 -07003050 ret = toshiba_usb_sleep_music_get(dev, &dummy);
3051 dev->usb_sleep_music_supported = !ret;
3052
Azael Avalos35d53ce2015-02-10 21:09:19 -07003053 ret = toshiba_panel_power_on_get(dev, &dummy);
3054 dev->panel_power_on_supported = !ret;
3055
Azael Avalos17fe4b32015-02-10 21:09:20 -07003056 ret = toshiba_usb_three_get(dev, &dummy);
3057 dev->usb_three_supported = !ret;
3058
Seth Forshee36d03f92011-09-20 16:55:53 -05003059 ret = get_video_status(dev, &dummy);
3060 dev->video_supported = !ret;
3061
3062 ret = get_fan_status(dev, &dummy);
3063 dev->fan_supported = !ret;
3064
Azael Avalos6873f462015-11-23 10:49:10 -07003065 toshiba_wwan_available(dev);
Azael Avalos2fdde832015-11-23 10:49:11 -07003066 if (dev->wwan_supported)
3067 toshiba_acpi_setup_wwan_rfkill(dev);
Azael Avalos6873f462015-11-23 10:49:10 -07003068
Azael Avalos763ff322016-01-25 11:29:10 -07003069 toshiba_cooling_method_available(dev);
3070
Azael Avalos0409cbc2015-07-31 21:58:13 -06003071 print_supported_features(dev);
3072
Azael Avalos360f0f32014-03-25 20:38:31 -06003073 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
3074 &toshiba_attr_group);
3075 if (ret) {
3076 dev->sysfs_created = 0;
3077 goto error;
3078 }
3079 dev->sysfs_created = !ret;
3080
Seth Forshee36d03f92011-09-20 16:55:53 -05003081 create_toshiba_proc_entries(dev);
3082
Seth Forshee29cd2932012-01-18 13:44:09 -06003083 toshiba_acpi = dev;
3084
Seth Forshee135740d2011-09-20 16:55:49 -05003085 return 0;
3086
3087error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01003088 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05003089 return ret;
3090}
3091
3092static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
3093{
3094 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003095
Azael Avalos71454d72014-12-04 20:22:46 -07003096 switch (event) {
3097 case 0x80: /* Hotkeys and some system events */
Azael Avalosa88bc062015-07-22 18:09:12 -06003098 /*
3099 * Machines with this WMI GUID aren't supported due to bugs in
3100 * their AML.
3101 *
3102 * Return silently to avoid triggering a netlink event.
3103 */
3104 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
3105 return;
Azael Avalos71454d72014-12-04 20:22:46 -07003106 toshiba_acpi_process_hotkeys(dev);
3107 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003108 case 0x81: /* Dock events */
3109 case 0x82:
3110 case 0x83:
3111 pr_info("Dock event received %x\n", event);
3112 break;
3113 case 0x88: /* Thermal events */
3114 pr_info("Thermal event received\n");
3115 break;
3116 case 0x8f: /* LID closed */
3117 case 0x90: /* LID is closed and Dock has been ejected */
3118 break;
3119 case 0x8c: /* SATA power events */
3120 case 0x8b:
3121 pr_info("SATA power event received %x\n", event);
3122 break;
Azael Avalos80546902014-12-04 20:22:47 -07003123 case 0x92: /* Keyboard backlight mode changed */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003124 toshiba_acpi->kbd_event_generated = true;
Azael Avalos80546902014-12-04 20:22:47 -07003125 /* Update sysfs entries */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003126 if (sysfs_update_group(&acpi_dev->dev.kobj,
3127 &toshiba_attr_group))
Azael Avalos80546902014-12-04 20:22:47 -07003128 pr_err("Unable to update sysfs entries\n");
3129 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003130 case 0x85: /* Unknown */
3131 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003132 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07003133 case 0x94: /* Unknown */
3134 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003135 default:
3136 pr_info("Unknown event received %x\n", event);
3137 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06003138 }
Azael Avalosbab09e22015-03-06 18:14:41 -07003139
3140 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
3141 dev_name(&acpi_dev->dev),
Azael Avalos13ae84f2015-11-15 20:33:46 -07003142 event, (event == 0x80) ?
3143 dev->last_key_event : 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003144}
3145
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003146#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003147static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003148{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003149 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003150
Azael Avalos1e574db2015-07-22 19:37:49 -06003151 if (dev->hotkey_dev) {
3152 u32 result;
3153
Azael Avalosd37782b2015-05-06 09:35:10 -06003154 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Azael Avalos1e574db2015-07-22 19:37:49 -06003155 if (result != TOS_SUCCESS)
3156 pr_info("Unable to disable hotkeys\n");
3157 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003158
3159 return 0;
3160}
3161
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003162static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003163{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003164 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003165
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003166 if (dev->hotkey_dev) {
Azael Avalos2fdde832015-11-23 10:49:11 -07003167 if (toshiba_acpi_enable_hotkeys(dev))
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003168 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003169 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003170
Azael Avalos2fdde832015-11-23 10:49:11 -07003171 if (dev->wwan_rfk) {
3172 if (!toshiba_wireless_status(dev))
3173 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
3174 }
3175
Seth Forshee29cd2932012-01-18 13:44:09 -06003176 return 0;
3177}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003178#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003179
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003180static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
3181 toshiba_acpi_suspend, toshiba_acpi_resume);
3182
Seth Forshee135740d2011-09-20 16:55:49 -05003183static struct acpi_driver toshiba_acpi_driver = {
3184 .name = "Toshiba ACPI driver",
3185 .owner = THIS_MODULE,
3186 .ids = toshiba_device_ids,
3187 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
3188 .ops = {
3189 .add = toshiba_acpi_add,
3190 .remove = toshiba_acpi_remove,
3191 .notify = toshiba_acpi_notify,
3192 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003193 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05003194};
Holger Machtc9263552006-10-20 14:30:29 -07003195
Len Brown4be44fc2005-08-05 00:44:28 -04003196static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Seth Forshee135740d2011-09-20 16:55:49 -05003198 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
3201 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05003202 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003203 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205
Seth Forshee135740d2011-09-20 16:55:49 -05003206 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
3207 if (ret) {
3208 pr_err("Failed to register ACPI driver: %d\n", ret);
3209 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07003210 }
3211
Seth Forshee135740d2011-09-20 16:55:49 -05003212 return ret;
3213}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003214
Seth Forshee135740d2011-09-20 16:55:49 -05003215static void __exit toshiba_acpi_exit(void)
3216{
3217 acpi_bus_unregister_driver(&toshiba_acpi_driver);
3218 if (toshiba_proc_dir)
3219 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222module_init(toshiba_acpi_init);
3223module_exit(toshiba_acpi_exit);