blob: 802577f43a23379fb4ff167132173cf6a27fa12e [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 Avalos63ba3e22015-05-06 09:35:13 -060034#define TOSHIBA_ACPI_VERSION "0.22"
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>
54#include <linux/toshiba.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020055#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_AUTHOR("John Belmonte");
58MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
59MODULE_LICENSE("GPL");
60
Seth Forsheef11f9992012-01-18 13:44:11 -060061#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
62
Seth Forshee29cd2932012-01-18 13:44:09 -060063/* Scan code for Fn key on TOS1900 models */
64#define TOS1900_FN_SCAN 0x6e
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
68
Darren Harte0769fe2015-02-11 20:50:08 -080069/*
70 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060071 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
74 * be uniform across all their models. Ideally we would just call
75 * dedicated ACPI methods instead of using this primitive interface.
76 * However the ACPI methods seem to be incomplete in some areas (for
77 * example they allow setting, but not reading, the LCD brightness value),
78 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040079 *
Azael Avalos84a62732014-03-25 20:38:29 -060080 * SCI stands for "System Configuration Interface" which aim is to
81 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
Azael Avalos258c5902014-09-29 20:40:07 -060084#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Azael Avalos3f75bbe2015-05-06 09:35:11 -060086/* Operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define HCI_SET 0xff00
88#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060089#define SCI_OPEN 0xf100
90#define SCI_CLOSE 0xf200
91#define SCI_GET 0xf300
92#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Azael Avalos3f75bbe2015-05-06 09:35:11 -060094/* Return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060095#define TOS_SUCCESS 0x0000
96#define TOS_OPEN_CLOSE_OK 0x0044
97#define TOS_FAILURE 0x1000
98#define TOS_NOT_SUPPORTED 0x8000
99#define TOS_ALREADY_OPEN 0x8100
100#define TOS_NOT_OPENED 0x8200
101#define TOS_INPUT_DATA_ERROR 0x8300
102#define TOS_WRITE_PROTECTED 0x8400
103#define TOS_NOT_PRESENT 0x8600
104#define TOS_FIFO_EMPTY 0x8c00
105#define TOS_DATA_NOT_AVAILABLE 0x8d20
106#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700107#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600109/* Registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900111#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define HCI_SYSTEM_EVENT 0x0016
113#define HCI_VIDEO_OUT 0x001c
114#define HCI_HOTKEY_EVENT 0x001e
115#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400116#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600117#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600118#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600119#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600120#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600121#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700122#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600123#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700124#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600125#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700126#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700127#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600128#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700129#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600131/* Field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600132#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600133#define HCI_HOTKEY_DISABLE 0x0b
134#define HCI_HOTKEY_ENABLE 0x09
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600135#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define HCI_LCD_BRIGHTNESS_BITS 3
137#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
138#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600139#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600140#define HCI_SYSTEM_TYPE1 0x10
141#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define HCI_VIDEO_OUT_LCD 0x1
143#define HCI_VIDEO_OUT_CRT 0x2
144#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400145#define HCI_WIRELESS_KILL_SWITCH 0x01
146#define HCI_WIRELESS_BT_PRESENT 0x0f
147#define HCI_WIRELESS_BT_ATTACH 0x40
148#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600149#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600150#define SCI_KBD_MODE_FNZ 0x1
151#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600152#define SCI_KBD_MODE_ON 0x8
153#define SCI_KBD_MODE_OFF 0x10
154#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700155#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600156#define SCI_USB_CHARGE_DISABLED 0x00
157#define SCI_USB_CHARGE_ALTERNATE 0x09
158#define SCI_USB_CHARGE_TYPICAL 0x11
159#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700160#define SCI_USB_CHARGE_BAT_MASK 0x7
161#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
162#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
163#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700164#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Seth Forshee135740d2011-09-20 16:55:49 -0500166struct toshiba_acpi_dev {
167 struct acpi_device *acpi_dev;
168 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500169 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600170 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500171 struct backlight_device *backlight_dev;
172 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600173 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600174 struct led_classdev eco_led;
Azael Avalosfc5462f2015-07-22 18:09:11 -0600175 struct miscdevice miscdev;
Seth Forshee36d03f92011-09-20 16:55:53 -0500176
Seth Forshee135740d2011-09-20 16:55:49 -0500177 int force_fan;
178 int last_key_event;
179 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600180 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600181 int kbd_mode;
182 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700183 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600184 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600185 int hotkey_event_type;
Seth Forshee135740d2011-09-20 16:55:49 -0500186
Dan Carpenter592b7462012-01-15 14:28:20 +0300187 unsigned int illumination_supported:1;
188 unsigned int video_supported:1;
189 unsigned int fan_supported:1;
190 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600191 unsigned int ntfy_supported:1;
192 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900193 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600194 unsigned int kbd_illum_supported:1;
195 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600196 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600197 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600198 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700199 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700200 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700201 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700202 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700203 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700204 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600205 unsigned int sysfs_created:1;
Seth Forshee135740d2011-09-20 16:55:49 -0500206};
207
Seth Forshee29cd2932012-01-18 13:44:09 -0600208static struct toshiba_acpi_dev *toshiba_acpi;
209
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800210static const struct acpi_device_id toshiba_device_ids[] = {
211 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100212 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400213 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800214 {"TOS1900", 0},
215 {"", 0},
216};
217MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
218
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800219static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100220 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700221 { KE_KEY, 0x101, { KEY_MUTE } },
222 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
223 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600224 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600225 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
226 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700227 { KE_KEY, 0x13b, { KEY_COFFEE } },
228 { KE_KEY, 0x13c, { KEY_BATTERY } },
229 { KE_KEY, 0x13d, { KEY_SLEEP } },
230 { KE_KEY, 0x13e, { KEY_SUSPEND } },
231 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
232 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
233 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
234 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600235 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100236 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700237 { KE_KEY, 0xb05, { KEY_PROG2 } },
238 { KE_KEY, 0xb06, { KEY_WWW } },
239 { KE_KEY, 0xb07, { KEY_MAIL } },
240 { KE_KEY, 0xb30, { KEY_STOP } },
241 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
242 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
243 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
244 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600245 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
246 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
247 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
248 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
249 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700250 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500251};
252
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200253static const struct key_entry toshiba_acpi_alt_keymap[] = {
254 { KE_KEY, 0x157, { KEY_MUTE } },
255 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
256 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600257 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200258 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
259 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
260 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
261 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
262 { KE_KEY, 0x158, { KEY_WLAN } },
263 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
264 { KE_END, 0 },
265};
266
Darren Harte0769fe2015-02-11 20:50:08 -0800267/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200268 * List of models which have a broken acpi-video backlight interface and thus
269 * need to use the toshiba (vendor) interface instead.
270 */
271static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
272 {}
273};
274
275/*
Darren Harte0769fe2015-02-11 20:50:08 -0800276 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
278
Azael Avalosb5163992015-02-10 23:43:56 -0700279static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 *word = (*word & ~mask) | (mask * value);
282}
283
Darren Harte0769fe2015-02-11 20:50:08 -0800284/*
285 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 acpi_status status;
291
Zhang Rui619400d2013-09-03 08:31:56 +0800292 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500293 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Darren Harte0769fe2015-02-11 20:50:08 -0800296/*
297 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600298 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Azael Avalos258c5902014-09-29 20:40:07 -0600300static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
301 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600304 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600306 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 acpi_status status;
308 int i;
309
Azael Avalos258c5902014-09-29 20:40:07 -0600310 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600312 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 in_objs[i].type = ACPI_TYPE_INTEGER;
314 in_objs[i].integer.value = in[i];
315 }
316
317 results.length = sizeof(out_objs);
318 results.pointer = out_objs;
319
Seth Forshee6e02cc72011-09-20 16:55:51 -0500320 status = acpi_evaluate_object(dev->acpi_dev->handle,
321 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400322 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600323 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700324 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 return status;
329}
330
Darren Harte0769fe2015-02-11 20:50:08 -0800331/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600332 * Common hci tasks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * In addition to the ACPI status, the HCI system returns a result which
335 * may be useful (such as "not supported").
336 */
337
Azael Avalosd37782b2015-05-06 09:35:10 -0600338static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Azael Avalos258c5902014-09-29 20:40:07 -0600340 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
341 u32 out[TCI_WORDS];
342 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600343
344 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Azael Avalosd37782b2015-05-06 09:35:10 -0600347static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Azael Avalos258c5902014-09-29 20:40:07 -0600349 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
350 u32 out[TCI_WORDS];
351 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700352
Azael Avalos893f3f62014-09-29 20:40:09 -0600353 if (ACPI_FAILURE(status))
354 return TOS_FAILURE;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600357
358 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Darren Harte0769fe2015-02-11 20:50:08 -0800361/*
362 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600363 */
364
365static int sci_open(struct toshiba_acpi_dev *dev)
366{
Azael Avalos258c5902014-09-29 20:40:07 -0600367 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
368 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600369 acpi_status status;
370
Azael Avalos258c5902014-09-29 20:40:07 -0600371 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600372 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600373 pr_err("ACPI call to open SCI failed\n");
374 return 0;
375 }
376
Azael Avalos1864bbc2014-09-29 20:40:08 -0600377 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600378 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600379 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600380 pr_info("Toshiba SCI already opened\n");
381 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700382 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800383 /*
384 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700385 * implemented and return 0x8000 (Not Supported), failing to
386 * register some supported features.
387 *
388 * Simply return 1 if we hit those affected laptops to make the
389 * supported features work.
390 *
391 * In the case that some laptops really do not support the SCI,
392 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
393 * and thus, not registering support for the queried feature.
394 */
395 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600396 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600397 pr_info("Toshiba SCI is not present\n");
398 }
399
400 return 0;
401}
402
403static void sci_close(struct toshiba_acpi_dev *dev)
404{
Azael Avalos258c5902014-09-29 20:40:07 -0600405 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
406 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600407 acpi_status status;
408
Azael Avalos258c5902014-09-29 20:40:07 -0600409 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600410 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600411 pr_err("ACPI call to close SCI failed\n");
412 return;
413 }
414
Azael Avalos1864bbc2014-09-29 20:40:08 -0600415 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600416 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600417 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600418 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600419 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600420 pr_info("Toshiba SCI is not present\n");
421}
422
Azael Avalos893f3f62014-09-29 20:40:09 -0600423static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600424{
Azael Avalos258c5902014-09-29 20:40:07 -0600425 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
426 u32 out[TCI_WORDS];
427 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700428
Azael Avalos893f3f62014-09-29 20:40:09 -0600429 if (ACPI_FAILURE(status))
430 return TOS_FAILURE;
431
Azael Avalos84a62732014-03-25 20:38:29 -0600432 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600433
434 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600435}
436
Azael Avalos893f3f62014-09-29 20:40:09 -0600437static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600438{
Azael Avalos258c5902014-09-29 20:40:07 -0600439 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
440 u32 out[TCI_WORDS];
441 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600442
443 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600444}
445
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200446/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500447static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200448{
Azael Avalos258c5902014-09-29 20:40:07 -0600449 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
450 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200451 acpi_status status;
452
Azael Avalosfdb79082014-03-25 20:38:30 -0600453 if (!sci_open(dev))
454 return 0;
455
Azael Avalos258c5902014-09-29 20:40:07 -0600456 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600457 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600458 if (ACPI_FAILURE(status)) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600459 pr_err("ACPI call to query Illumination support failed\n");
460 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600461 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700462 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200463 return 0;
464 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600465
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200466 return 1;
467}
468
469static void toshiba_illumination_set(struct led_classdev *cdev,
470 enum led_brightness brightness)
471{
Seth Forshee135740d2011-09-20 16:55:49 -0500472 struct toshiba_acpi_dev *dev = container_of(cdev,
473 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600474 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200475
476 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600477 if (!sci_open(dev))
478 return;
479
480 /* Switch the illumination on/off */
481 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600482 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600483 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600484 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600485 pr_err("ACPI call for illumination failed\n");
486 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600487 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600488 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200489 return;
490 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200491}
492
493static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
494{
Seth Forshee135740d2011-09-20 16:55:49 -0500495 struct toshiba_acpi_dev *dev = container_of(cdev,
496 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600497 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200498
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600499 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600500 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200501 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200502
503 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600504 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600505 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600506 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 pr_err("ACPI call for illumination failed\n");
508 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600509 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600510 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200511 return LED_OFF;
512 }
513
Azael Avalosfdb79082014-03-25 20:38:30 -0600514 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200515}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400516
Azael Avalos360f0f32014-03-25 20:38:31 -0600517/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600518static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
519{
Azael Avalos258c5902014-09-29 20:40:07 -0600520 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
521 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600522 acpi_status status;
523
524 if (!sci_open(dev))
525 return 0;
526
Azael Avalos258c5902014-09-29 20:40:07 -0600527 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600528 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600529 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600530 pr_err("ACPI call to query kbd illumination support failed\n");
531 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600532 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600533 pr_info("Keyboard illumination not available\n");
534 return 0;
535 }
536
Darren Harte0769fe2015-02-11 20:50:08 -0800537 /*
538 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600539 * previous kbd backlight implementation set this to
540 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800541 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600542 */
543 if (out[3] == SCI_KBD_TIME_MAX)
544 dev->kbd_type = 2;
545 else
546 dev->kbd_type = 1;
547 /* Get the current keyboard backlight mode */
548 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
549 /* Get the current time (1-60 seconds) */
550 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
551
552 return 1;
553}
554
Azael Avalos360f0f32014-03-25 20:38:31 -0600555static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
556{
557 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600558
559 if (!sci_open(dev))
560 return -EIO;
561
Azael Avalos893f3f62014-09-29 20:40:09 -0600562 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600563 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600564 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600565 pr_err("ACPI call to set KBD backlight status failed\n");
566 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600567 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600568 pr_info("Keyboard backlight status not supported\n");
569 return -ENODEV;
570 }
571
572 return 0;
573}
574
575static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
576{
577 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600578
579 if (!sci_open(dev))
580 return -EIO;
581
Azael Avalos893f3f62014-09-29 20:40:09 -0600582 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600583 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600584 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600585 pr_err("ACPI call to get KBD backlight status failed\n");
586 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600587 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600588 pr_info("Keyboard backlight status not supported\n");
589 return -ENODEV;
590 }
591
592 return 0;
593}
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);
599 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600600
601 /* Check the keyboard backlight state */
Azael Avalosd37782b2015-05-06 09:35:10 -0600602 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600603 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600604 pr_err("ACPI call to get the keyboard backlight failed\n");
605 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600606 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600607 pr_info("Keyboard backlight not supported\n");
608 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);
619 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600620
621 /* Set the keyboard backlight state */
622 state = brightness ? 1 : 0;
Azael Avalosd37782b2015-05-06 09:35:10 -0600623 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600624 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600625 pr_err("ACPI call to set KBD Illumination mode failed\n");
626 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600627 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600628 pr_info("Keyboard backlight not supported\n");
629 return;
630 }
631}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400632
Azael Avalos9d8658a2014-03-25 20:38:32 -0600633/* TouchPad support */
634static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
635{
636 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600637
638 if (!sci_open(dev))
639 return -EIO;
640
Azael Avalos893f3f62014-09-29 20:40:09 -0600641 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600642 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600643 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600644 pr_err("ACPI call to set the touchpad failed\n");
645 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600646 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600647 return -ENODEV;
648 }
649
650 return 0;
651}
652
653static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
654{
655 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600656
657 if (!sci_open(dev))
658 return -EIO;
659
Azael Avalos893f3f62014-09-29 20:40:09 -0600660 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600661 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600662 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600663 pr_err("ACPI call to query the touchpad failed\n");
664 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600665 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600666 return -ENODEV;
667 }
668
669 return 0;
670}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200671
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600672/* Eco Mode support */
673static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
674{
675 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700676 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600677 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600678
Azael Avalos258c5902014-09-29 20:40:07 -0600679 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600680 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700681 pr_err("ACPI call to get ECO led failed\n");
682 } else if (out[0] == TOS_NOT_INSTALLED) {
683 pr_info("ECO led not installed");
684 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800685 /*
686 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700687 * LED device is present, but that we just screwed the input
688 * parameters.
689 *
690 * Let's query the status of the LED to see if we really have a
691 * success response, indicating the actual presense of the LED,
692 * bail out otherwise.
693 */
694 in[3] = 1;
695 status = tci_raw(dev, in, out);
696 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
697 pr_err("ACPI call to get ECO led failed\n");
698 else if (out[0] == TOS_SUCCESS)
699 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600700 }
701
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700702 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600703}
704
Azael Avalosb5163992015-02-10 23:43:56 -0700705static enum led_brightness
706toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600707{
708 struct toshiba_acpi_dev *dev = container_of(cdev,
709 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600710 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
711 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600712 acpi_status status;
713
Azael Avalos258c5902014-09-29 20:40:07 -0600714 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600715 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600716 pr_err("ACPI call to get ECO led failed\n");
717 return LED_OFF;
718 }
719
720 return out[2] ? LED_FULL : LED_OFF;
721}
722
723static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
724 enum led_brightness brightness)
725{
726 struct toshiba_acpi_dev *dev = container_of(cdev,
727 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600728 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
729 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600730 acpi_status status;
731
732 /* Switch the Eco Mode led on/off */
733 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600734 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600735 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600736 pr_err("ACPI call to set ECO led failed\n");
737 return;
738 }
739}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400740
Azael Avalos5a2813e2014-03-25 20:38:34 -0600741/* Accelerometer support */
742static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
743{
Azael Avalos258c5902014-09-29 20:40:07 -0600744 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
745 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600746 acpi_status status;
747
Darren Harte0769fe2015-02-11 20:50:08 -0800748 /*
749 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600750 * this call also serves as initialization
751 */
Azael Avalos258c5902014-09-29 20:40:07 -0600752 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600753 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600754 pr_err("ACPI call to query the accelerometer failed\n");
755 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600756 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
757 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600758 pr_err("Accelerometer not initialized\n");
759 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600760 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600761 pr_info("Accelerometer not supported\n");
762 return -ENODEV;
763 }
764
765 return 0;
766}
767
768static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
769 u32 *xy, u32 *z)
770{
Azael Avalos258c5902014-09-29 20:40:07 -0600771 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
772 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600773 acpi_status status;
774
775 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600776 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600777 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600778 pr_err("ACPI call to query the accelerometer failed\n");
779 return -EIO;
780 }
781
782 *xy = out[2];
783 *z = out[4];
784
785 return 0;
786}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600787
Azael Avalose26ffe52015-01-18 18:30:22 -0700788/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600789static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
790{
791 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
792 u32 out[TCI_WORDS];
793 acpi_status status;
794
795 /* Set the feature to "not supported" in case of error */
796 dev->usb_sleep_charge_supported = 0;
797
798 if (!sci_open(dev))
799 return;
800
801 status = tci_raw(dev, in, out);
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");
804 sci_close(dev);
805 return;
806 } else if (out[0] == TOS_NOT_SUPPORTED) {
807 pr_info("USB Sleep and Charge not supported\n");
808 sci_close(dev);
809 return;
810 } else if (out[0] == TOS_SUCCESS) {
811 dev->usbsc_mode_base = out[4];
812 }
813
814 in[5] = SCI_USB_CHARGE_BAT_LVL;
815 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600816 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600817 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
818 sci_close(dev);
819 return;
820 } else if (out[0] == TOS_NOT_SUPPORTED) {
821 pr_info("USB Sleep and Charge not supported\n");
822 sci_close(dev);
823 return;
824 } else if (out[0] == TOS_SUCCESS) {
825 dev->usbsc_bat_level = out[2];
826 /*
827 * If we reach this point, it means that the laptop has support
828 * for this feature and all values are initialized.
829 * Set it as supported.
830 */
831 dev->usb_sleep_charge_supported = 1;
832 }
833
834 sci_close(dev);
835}
836
Azael Avalose26ffe52015-01-18 18:30:22 -0700837static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
838 u32 *mode)
839{
840 u32 result;
841
842 if (!sci_open(dev))
843 return -EIO;
844
845 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
846 sci_close(dev);
847 if (result == TOS_FAILURE) {
848 pr_err("ACPI call to set USB S&C mode failed\n");
849 return -EIO;
850 } else if (result == TOS_NOT_SUPPORTED) {
851 pr_info("USB Sleep and Charge not supported\n");
852 return -ENODEV;
853 } else if (result == TOS_INPUT_DATA_ERROR) {
854 return -EIO;
855 }
856
857 return 0;
858}
859
860static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
861 u32 mode)
862{
863 u32 result;
864
865 if (!sci_open(dev))
866 return -EIO;
867
868 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
869 sci_close(dev);
870 if (result == TOS_FAILURE) {
871 pr_err("ACPI call to set USB S&C mode failed\n");
872 return -EIO;
873 } else if (result == TOS_NOT_SUPPORTED) {
874 pr_info("USB Sleep and Charge not supported\n");
875 return -ENODEV;
876 } else if (result == TOS_INPUT_DATA_ERROR) {
877 return -EIO;
878 }
879
880 return 0;
881}
882
Azael Avalos182bcaa2015-01-18 18:30:23 -0700883static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
884 u32 *mode)
885{
886 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
887 u32 out[TCI_WORDS];
888 acpi_status status;
889
890 if (!sci_open(dev))
891 return -EIO;
892
893 in[5] = SCI_USB_CHARGE_BAT_LVL;
894 status = tci_raw(dev, in, out);
895 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600896 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700897 pr_err("ACPI call to get USB S&C battery level failed\n");
898 return -EIO;
899 } else if (out[0] == TOS_NOT_SUPPORTED) {
900 pr_info("USB Sleep and Charge not supported\n");
901 return -ENODEV;
902 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
903 return -EIO;
904 }
905
906 *mode = out[2];
907
908 return 0;
909}
910
911static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
912 u32 mode)
913{
914 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
915 u32 out[TCI_WORDS];
916 acpi_status status;
917
918 if (!sci_open(dev))
919 return -EIO;
920
921 in[2] = mode;
922 in[5] = SCI_USB_CHARGE_BAT_LVL;
923 status = tci_raw(dev, in, out);
924 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600925 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700926 pr_err("ACPI call to set USB S&C battery level failed\n");
927 return -EIO;
928 } else if (out[0] == TOS_NOT_SUPPORTED) {
929 pr_info("USB Sleep and Charge not supported\n");
930 return -ENODEV;
931 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
932 return -EIO;
933 }
934
935 return 0;
936}
937
Azael Avalosbb3fe012015-01-18 18:30:24 -0700938static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
939 u32 *state)
940{
941 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
942 u32 out[TCI_WORDS];
943 acpi_status status;
944
945 if (!sci_open(dev))
946 return -EIO;
947
948 in[5] = SCI_USB_CHARGE_RAPID_DSP;
949 status = tci_raw(dev, in, out);
950 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600951 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600952 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700953 return -EIO;
954 } else if (out[0] == TOS_NOT_SUPPORTED ||
955 out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600956 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700957 return -ENODEV;
958 }
959
960 *state = out[2];
961
962 return 0;
963}
964
965static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
966 u32 state)
967{
968 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
969 u32 out[TCI_WORDS];
970 acpi_status status;
971
972 if (!sci_open(dev))
973 return -EIO;
974
975 in[2] = state;
976 in[5] = SCI_USB_CHARGE_RAPID_DSP;
977 status = tci_raw(dev, in, out);
978 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600979 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600980 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700981 return -EIO;
982 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600983 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700984 return -ENODEV;
985 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
986 return -EIO;
987 }
988
989 return 0;
990}
991
Azael Avalos172ce0a2015-01-18 18:30:25 -0700992static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
993{
994 u32 result;
995
996 if (!sci_open(dev))
997 return -EIO;
998
999 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1000 sci_close(dev);
1001 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001002 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001003 return -EIO;
1004 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001005 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001006 return -ENODEV;
1007 } else if (result == TOS_INPUT_DATA_ERROR) {
1008 return -EIO;
1009 }
1010
1011 return 0;
1012}
1013
1014static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1015{
1016 u32 result;
1017
1018 if (!sci_open(dev))
1019 return -EIO;
1020
1021 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1022 sci_close(dev);
1023 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001024 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001025 return -EIO;
1026 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001027 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001028 return -ENODEV;
1029 } else if (result == TOS_INPUT_DATA_ERROR) {
1030 return -EIO;
1031 }
1032
1033 return 0;
1034}
1035
Azael Avalosbae84192015-02-10 21:09:18 -07001036/* Keyboard function keys */
1037static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1038{
1039 u32 result;
1040
1041 if (!sci_open(dev))
1042 return -EIO;
1043
1044 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1045 sci_close(dev);
1046 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1047 pr_err("ACPI call to get KBD function keys failed\n");
1048 return -EIO;
1049 } else if (result == TOS_NOT_SUPPORTED) {
1050 pr_info("KBD function keys not supported\n");
1051 return -ENODEV;
1052 }
1053
1054 return 0;
1055}
1056
1057static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1058{
1059 u32 result;
1060
1061 if (!sci_open(dev))
1062 return -EIO;
1063
1064 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1065 sci_close(dev);
1066 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1067 pr_err("ACPI call to set KBD function keys failed\n");
1068 return -EIO;
1069 } else if (result == TOS_NOT_SUPPORTED) {
1070 pr_info("KBD function keys not supported\n");
1071 return -ENODEV;
1072 }
1073
1074 return 0;
1075}
1076
Azael Avalos35d53ce2015-02-10 21:09:19 -07001077/* Panel Power ON */
1078static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1079{
1080 u32 result;
1081
1082 if (!sci_open(dev))
1083 return -EIO;
1084
1085 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1086 sci_close(dev);
1087 if (result == TOS_FAILURE) {
1088 pr_err("ACPI call to get Panel Power ON failed\n");
1089 return -EIO;
1090 } else if (result == TOS_NOT_SUPPORTED) {
1091 pr_info("Panel Power on not supported\n");
1092 return -ENODEV;
1093 } else if (result == TOS_INPUT_DATA_ERROR) {
1094 return -EIO;
1095 }
1096
1097 return 0;
1098}
1099
1100static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1101{
1102 u32 result;
1103
1104 if (!sci_open(dev))
1105 return -EIO;
1106
1107 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1108 sci_close(dev);
1109 if (result == TOS_FAILURE) {
1110 pr_err("ACPI call to set Panel Power ON failed\n");
1111 return -EIO;
1112 } else if (result == TOS_NOT_SUPPORTED) {
1113 pr_info("Panel Power ON not supported\n");
1114 return -ENODEV;
1115 } else if (result == TOS_INPUT_DATA_ERROR) {
1116 return -EIO;
1117 }
1118
1119 return 0;
1120}
1121
Azael Avalos17fe4b32015-02-10 21:09:20 -07001122/* USB Three */
1123static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1124{
1125 u32 result;
1126
1127 if (!sci_open(dev))
1128 return -EIO;
1129
1130 result = sci_read(dev, SCI_USB_THREE, state);
1131 sci_close(dev);
1132 if (result == TOS_FAILURE) {
1133 pr_err("ACPI call to get USB 3 failed\n");
1134 return -EIO;
1135 } else if (result == TOS_NOT_SUPPORTED) {
1136 pr_info("USB 3 not supported\n");
1137 return -ENODEV;
1138 } else if (result == TOS_INPUT_DATA_ERROR) {
1139 return -EIO;
1140 }
1141
1142 return 0;
1143}
1144
1145static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1146{
1147 u32 result;
1148
1149 if (!sci_open(dev))
1150 return -EIO;
1151
1152 result = sci_write(dev, SCI_USB_THREE, state);
1153 sci_close(dev);
1154 if (result == TOS_FAILURE) {
1155 pr_err("ACPI call to set USB 3 failed\n");
1156 return -EIO;
1157 } else if (result == TOS_NOT_SUPPORTED) {
1158 pr_info("USB 3 not supported\n");
1159 return -ENODEV;
1160 } else if (result == TOS_INPUT_DATA_ERROR) {
1161 return -EIO;
1162 }
1163
1164 return 0;
1165}
1166
Azael Avalos56e6b352015-03-20 16:55:16 -06001167/* Hotkey Event type */
1168static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1169 u32 *type)
1170{
Azael Avalos3b876002015-05-06 09:35:09 -06001171 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1172 u32 out[TCI_WORDS];
1173 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001174
Azael Avalos3b876002015-05-06 09:35:09 -06001175 status = tci_raw(dev, in, out);
1176 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001177 pr_err("ACPI call to get System type failed\n");
1178 return -EIO;
Azael Avalos3b876002015-05-06 09:35:09 -06001179 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001180 pr_info("System type not supported\n");
1181 return -ENODEV;
1182 }
1183
Azael Avalos3b876002015-05-06 09:35:09 -06001184 *type = out[3];
Azael Avalos56e6b352015-03-20 16:55:16 -06001185
1186 return 0;
1187}
1188
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001189/* Transflective Backlight */
Akio Idehara121b7b02012-04-06 01:46:43 +09001190static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1191{
1192 u32 hci_result;
1193 u32 status;
1194
Azael Avalosd37782b2015-05-06 09:35:10 -06001195 hci_result = hci_read(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001196 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001197 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001198}
1199
1200static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1201{
1202 u32 hci_result;
1203 u32 value = !enable;
1204
Azael Avalosd37782b2015-05-06 09:35:10 -06001205 hci_result = hci_write(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001206 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001207}
1208
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001209static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001211/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001212static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 u32 hci_result;
1215 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001216 int brightness = 0;
1217
1218 if (dev->tr_backlight_supported) {
1219 bool enabled;
1220 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001221
Akio Idehara121b7b02012-04-06 01:46:43 +09001222 if (ret)
1223 return ret;
1224 if (enabled)
1225 return 0;
1226 brightness++;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Azael Avalosd37782b2015-05-06 09:35:10 -06001229 hci_result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001230 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001231 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001232
1233 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001234}
1235
Seth Forshee62cce752012-04-19 11:23:50 -05001236static int get_lcd_brightness(struct backlight_device *bd)
1237{
1238 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001239
Seth Forshee62cce752012-04-19 11:23:50 -05001240 return __get_lcd_brightness(dev);
1241}
1242
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001243static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001244{
Seth Forshee135740d2011-09-20 16:55:49 -05001245 struct toshiba_acpi_dev *dev = m->private;
1246 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001247 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001248
Seth Forshee135740d2011-09-20 16:55:49 -05001249 if (!dev->backlight_dev)
1250 return -ENODEV;
1251
Akio Idehara121b7b02012-04-06 01:46:43 +09001252 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001253 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001254 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001255 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001256 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001260 pr_err("Error reading LCD brightness\n");
1261 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001262}
1263
1264static int lcd_proc_open(struct inode *inode, struct file *file)
1265{
Al Virod9dda782013-03-31 18:16:14 -04001266 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Seth Forshee62cce752012-04-19 11:23:50 -05001269static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001270{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001271 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001272
Akio Idehara121b7b02012-04-06 01:46:43 +09001273 if (dev->tr_backlight_supported) {
1274 bool enable = !value;
1275 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001276
Akio Idehara121b7b02012-04-06 01:46:43 +09001277 if (ret)
1278 return ret;
1279 if (value)
1280 value--;
1281 }
1282
Azael Avalosa39f46d2014-11-24 19:29:36 -07001283 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalosd37782b2015-05-06 09:35:10 -06001284 hci_result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
Azael Avalosa39f46d2014-11-24 19:29:36 -07001285 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001286}
1287
1288static int set_lcd_status(struct backlight_device *bd)
1289{
Seth Forshee135740d2011-09-20 16:55:49 -05001290 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001291
Seth Forshee62cce752012-04-19 11:23:50 -05001292 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001293}
1294
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001295static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1296 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Al Virod9dda782013-03-31 18:16:14 -04001298 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001299 char cmd[42];
1300 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001302 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001303 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001305 len = min(count, sizeof(cmd) - 1);
1306 if (copy_from_user(cmd, buf, len))
1307 return -EFAULT;
1308 cmd[len] = '\0';
1309
1310 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001311 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001312 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001313 if (ret == 0)
1314 ret = count;
1315 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001316 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001317 }
Holger Machtc9263552006-10-20 14:30:29 -07001318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001321static const struct file_operations lcd_proc_fops = {
1322 .owner = THIS_MODULE,
1323 .open = lcd_proc_open,
1324 .read = seq_read,
1325 .llseek = seq_lseek,
1326 .release = single_release,
1327 .write = lcd_proc_write,
1328};
1329
Seth Forshee36d03f92011-09-20 16:55:53 -05001330static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1331{
1332 u32 hci_result;
1333
Azael Avalosd37782b2015-05-06 09:35:10 -06001334 hci_result = hci_read(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001335 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001336}
1337
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001338static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Seth Forshee135740d2011-09-20 16:55:49 -05001340 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001342 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Seth Forshee36d03f92011-09-20 16:55:53 -05001344 ret = get_video_status(dev, &value);
1345 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1347 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001348 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001349
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001350 seq_printf(m, "lcd_out: %d\n", is_lcd);
1351 seq_printf(m, "crt_out: %d\n", is_crt);
1352 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Seth Forshee36d03f92011-09-20 16:55:53 -05001355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001358static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Al Virod9dda782013-03-31 18:16:14 -04001360 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001361}
1362
1363static ssize_t video_proc_write(struct file *file, const char __user *buf,
1364 size_t count, loff_t *pos)
1365{
Al Virod9dda782013-03-31 18:16:14 -04001366 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001367 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001368 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int value;
1370 int remain = count;
1371 int lcd_out = -1;
1372 int crt_out = -1;
1373 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001374 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001376 cmd = kmalloc(count + 1, GFP_KERNEL);
1377 if (!cmd)
1378 return -ENOMEM;
1379 if (copy_from_user(cmd, buf, count)) {
1380 kfree(cmd);
1381 return -EFAULT;
1382 }
1383 cmd[count] = '\0';
1384
1385 buffer = cmd;
1386
Darren Harte0769fe2015-02-11 20:50:08 -08001387 /*
1388 * Scan expression. Multiple expressions may be delimited with ;
1389 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391 while (remain) {
1392 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1393 lcd_out = value & 1;
1394 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1395 crt_out = value & 1;
1396 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1397 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001398 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 do {
1400 ++buffer;
1401 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001402 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001405 kfree(cmd);
1406
Seth Forshee36d03f92011-09-20 16:55:53 -05001407 ret = get_video_status(dev, &video_out);
1408 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001409 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (lcd_out != -1)
1412 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1413 if (crt_out != -1)
1414 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1415 if (tv_out != -1)
1416 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001417 /*
1418 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001419 * video setting if something changed.
1420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001422 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001425 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001428static const struct file_operations video_proc_fops = {
1429 .owner = THIS_MODULE,
1430 .open = video_proc_open,
1431 .read = seq_read,
1432 .llseek = seq_lseek,
1433 .release = single_release,
1434 .write = video_proc_write,
1435};
1436
Seth Forshee36d03f92011-09-20 16:55:53 -05001437static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1438{
1439 u32 hci_result;
1440
Azael Avalosd37782b2015-05-06 09:35:10 -06001441 hci_result = hci_read(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001442 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001443}
1444
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001445static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Seth Forshee135740d2011-09-20 16:55:49 -05001447 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001448 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 u32 value;
1450
Seth Forshee36d03f92011-09-20 16:55:53 -05001451 ret = get_fan_status(dev, &value);
1452 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001453 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001454 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Seth Forshee36d03f92011-09-20 16:55:53 -05001457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001460static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Al Virod9dda782013-03-31 18:16:14 -04001462 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001463}
1464
1465static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1466 size_t count, loff_t *pos)
1467{
Al Virod9dda782013-03-31 18:16:14 -04001468 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001469 char cmd[42];
1470 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int value;
1472 u32 hci_result;
1473
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001474 len = min(count, sizeof(cmd) - 1);
1475 if (copy_from_user(cmd, buf, len))
1476 return -EFAULT;
1477 cmd[len] = '\0';
1478
1479 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001480 value >= 0 && value <= 1) {
Azael Avalosd37782b2015-05-06 09:35:10 -06001481 hci_result = hci_write(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001482 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001483 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001484 else
1485 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 } else {
1487 return -EINVAL;
1488 }
1489
1490 return count;
1491}
1492
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001493static const struct file_operations fan_proc_fops = {
1494 .owner = THIS_MODULE,
1495 .open = fan_proc_open,
1496 .read = seq_read,
1497 .llseek = seq_lseek,
1498 .release = single_release,
1499 .write = fan_proc_write,
1500};
1501
1502static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Seth Forshee135740d2011-09-20 16:55:49 -05001504 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Seth Forshee135740d2011-09-20 16:55:49 -05001506 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1507 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Azael Avalos7deef552015-07-22 18:09:10 -06001508
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001512static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Al Virod9dda782013-03-31 18:16:14 -04001514 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001515}
1516
1517static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1518 size_t count, loff_t *pos)
1519{
Al Virod9dda782013-03-31 18:16:14 -04001520 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001521 char cmd[42];
1522 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int value;
1524
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001525 len = min(count, sizeof(cmd) - 1);
1526 if (copy_from_user(cmd, buf, len))
1527 return -EFAULT;
1528 cmd[len] = '\0';
1529
Azael Avalosb5163992015-02-10 23:43:56 -07001530 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001531 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001532 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 return count;
1536}
1537
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001538static const struct file_operations keys_proc_fops = {
1539 .owner = THIS_MODULE,
1540 .open = keys_proc_open,
1541 .read = seq_read,
1542 .llseek = seq_lseek,
1543 .release = single_release,
1544 .write = keys_proc_write,
1545};
1546
1547static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001549 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1550 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001554static int version_proc_open(struct inode *inode, struct file *file)
1555{
Al Virod9dda782013-03-31 18:16:14 -04001556 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001557}
1558
1559static const struct file_operations version_proc_fops = {
1560 .owner = THIS_MODULE,
1561 .open = version_proc_open,
1562 .read = seq_read,
1563 .llseek = seq_lseek,
1564 .release = single_release,
1565};
1566
Darren Harte0769fe2015-02-11 20:50:08 -08001567/*
1568 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
1570
1571#define PROC_TOSHIBA "toshiba"
1572
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001573static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Seth Forshee36d03f92011-09-20 16:55:53 -05001575 if (dev->backlight_dev)
1576 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1577 &lcd_proc_fops, dev);
1578 if (dev->video_supported)
1579 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1580 &video_proc_fops, dev);
1581 if (dev->fan_supported)
1582 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1583 &fan_proc_fops, dev);
1584 if (dev->hotkey_dev)
1585 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1586 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001587 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1588 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Seth Forshee36d03f92011-09-20 16:55:53 -05001591static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Seth Forshee36d03f92011-09-20 16:55:53 -05001593 if (dev->backlight_dev)
1594 remove_proc_entry("lcd", toshiba_proc_dir);
1595 if (dev->video_supported)
1596 remove_proc_entry("video", toshiba_proc_dir);
1597 if (dev->fan_supported)
1598 remove_proc_entry("fan", toshiba_proc_dir);
1599 if (dev->hotkey_dev)
1600 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001601 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Lionel Debrouxacc24722010-11-16 14:14:02 +01001604static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001605 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001606 .get_brightness = get_lcd_brightness,
1607 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001608};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001609
Azael Avalos360f0f32014-03-25 20:38:31 -06001610/*
1611 * Sysfs files
1612 */
Azael Avalos9d309842015-02-10 23:43:59 -07001613static ssize_t version_show(struct device *dev,
1614 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001615{
1616 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1617}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001618static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001619
Azael Avalos9d309842015-02-10 23:43:59 -07001620static ssize_t fan_store(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001623{
1624 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1625 u32 result;
1626 int state;
1627 int ret;
1628
1629 ret = kstrtoint(buf, 0, &state);
1630 if (ret)
1631 return ret;
1632
1633 if (state != 0 && state != 1)
1634 return -EINVAL;
1635
Azael Avalosd37782b2015-05-06 09:35:10 -06001636 result = hci_write(toshiba, HCI_FAN, state);
Azael Avalos94477d42015-02-10 21:09:17 -07001637 if (result == TOS_FAILURE)
1638 return -EIO;
1639 else if (result == TOS_NOT_SUPPORTED)
1640 return -ENODEV;
1641
1642 return count;
1643}
1644
Azael Avalos9d309842015-02-10 23:43:59 -07001645static ssize_t fan_show(struct device *dev,
1646 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001647{
1648 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1649 u32 value;
1650 int ret;
1651
1652 ret = get_fan_status(toshiba, &value);
1653 if (ret)
1654 return ret;
1655
1656 return sprintf(buf, "%d\n", value);
1657}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001658static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001659
Azael Avalos9d309842015-02-10 23:43:59 -07001660static ssize_t kbd_backlight_mode_store(struct device *dev,
1661 struct device_attribute *attr,
1662 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001663{
1664 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001665 int mode;
1666 int time;
1667 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001668
Dan Carpenteraeaac092014-09-03 14:44:37 +03001669
1670 ret = kstrtoint(buf, 0, &mode);
1671 if (ret)
1672 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001673
1674 /* Check for supported modes depending on keyboard backlight type */
1675 if (toshiba->kbd_type == 1) {
1676 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1677 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1678 return -EINVAL;
1679 } else if (toshiba->kbd_type == 2) {
1680 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1681 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1682 mode != SCI_KBD_MODE_OFF)
1683 return -EINVAL;
1684 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001685
Darren Harte0769fe2015-02-11 20:50:08 -08001686 /*
1687 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001688 * Auto - KBD backlight turns off automatically in given time
1689 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001690 * ON - KBD backlight is always on
1691 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001692 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001693
1694 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001695 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001696 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001697 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001698
1699 /* OR the "base time" to the actual method format */
1700 if (toshiba->kbd_type == 1) {
1701 /* Type 1 requires the current mode */
1702 time |= toshiba->kbd_mode;
1703 } else if (toshiba->kbd_type == 2) {
1704 /* Type 2 requires the desired mode */
1705 time |= mode;
1706 }
1707
Dan Carpenteraeaac092014-09-03 14:44:37 +03001708 ret = toshiba_kbd_illum_status_set(toshiba, time);
1709 if (ret)
1710 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001711
Azael Avalos360f0f32014-03-25 20:38:31 -06001712 toshiba->kbd_mode = mode;
1713 }
1714
1715 return count;
1716}
1717
Azael Avalos9d309842015-02-10 23:43:59 -07001718static ssize_t kbd_backlight_mode_show(struct device *dev,
1719 struct device_attribute *attr,
1720 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001721{
1722 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1723 u32 time;
1724
1725 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1726 return -EIO;
1727
Azael Avalos93f8c162014-09-12 18:50:36 -06001728 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1729}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001730static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001731
Azael Avalos9d309842015-02-10 23:43:59 -07001732static ssize_t kbd_type_show(struct device *dev,
1733 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001734{
1735 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1736
1737 return sprintf(buf, "%d\n", toshiba->kbd_type);
1738}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001739static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001740
Azael Avalos9d309842015-02-10 23:43:59 -07001741static ssize_t available_kbd_modes_show(struct device *dev,
1742 struct device_attribute *attr,
1743 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001744{
1745 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1746
1747 if (toshiba->kbd_type == 1)
1748 return sprintf(buf, "%x %x\n",
1749 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1750
1751 return sprintf(buf, "%x %x %x\n",
1752 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001753}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001754static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001755
Azael Avalos9d309842015-02-10 23:43:59 -07001756static ssize_t kbd_backlight_timeout_store(struct device *dev,
1757 struct device_attribute *attr,
1758 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001759{
1760 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001761 int time;
1762 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001763
Azael Avaloseabde0f2014-10-04 12:02:21 -06001764 ret = kstrtoint(buf, 0, &time);
1765 if (ret)
1766 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001767
Azael Avaloseabde0f2014-10-04 12:02:21 -06001768 /* Check for supported values depending on kbd_type */
1769 if (toshiba->kbd_type == 1) {
1770 if (time < 0 || time > 60)
1771 return -EINVAL;
1772 } else if (toshiba->kbd_type == 2) {
1773 if (time < 1 || time > 60)
1774 return -EINVAL;
1775 }
1776
1777 /* Set the Keyboard Backlight Timeout */
1778
1779 /* Only make a change if the actual timeout has changed */
1780 if (toshiba->kbd_time != time) {
1781 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001782 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001783 /* OR the "base time" to the actual method format */
1784 if (toshiba->kbd_type == 1)
1785 time |= SCI_KBD_MODE_FNZ;
1786 else if (toshiba->kbd_type == 2)
1787 time |= SCI_KBD_MODE_AUTO;
1788
1789 ret = toshiba_kbd_illum_status_set(toshiba, time);
1790 if (ret)
1791 return ret;
1792
Azael Avalos360f0f32014-03-25 20:38:31 -06001793 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1794 }
1795
1796 return count;
1797}
1798
Azael Avalos9d309842015-02-10 23:43:59 -07001799static ssize_t kbd_backlight_timeout_show(struct device *dev,
1800 struct device_attribute *attr,
1801 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001802{
1803 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1804 u32 time;
1805
1806 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1807 return -EIO;
1808
1809 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1810}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001811static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001812
Azael Avalos9d309842015-02-10 23:43:59 -07001813static ssize_t touchpad_store(struct device *dev,
1814 struct device_attribute *attr,
1815 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001816{
1817 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1818 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001819 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001820
1821 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001822 ret = kstrtoint(buf, 0, &state);
1823 if (ret)
1824 return ret;
1825 if (state != 0 && state != 1)
1826 return -EINVAL;
1827
1828 ret = toshiba_touchpad_set(toshiba, state);
1829 if (ret)
1830 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001831
1832 return count;
1833}
1834
Azael Avalos9d309842015-02-10 23:43:59 -07001835static ssize_t touchpad_show(struct device *dev,
1836 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001837{
1838 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1839 u32 state;
1840 int ret;
1841
1842 ret = toshiba_touchpad_get(toshiba, &state);
1843 if (ret < 0)
1844 return ret;
1845
1846 return sprintf(buf, "%i\n", state);
1847}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001848static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001849
Azael Avalos9d309842015-02-10 23:43:59 -07001850static ssize_t position_show(struct device *dev,
1851 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001852{
1853 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1854 u32 xyval, zval, tmp;
1855 u16 x, y, z;
1856 int ret;
1857
1858 xyval = zval = 0;
1859 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1860 if (ret < 0)
1861 return ret;
1862
1863 x = xyval & HCI_ACCEL_MASK;
1864 tmp = xyval >> HCI_MISC_SHIFT;
1865 y = tmp & HCI_ACCEL_MASK;
1866 z = zval & HCI_ACCEL_MASK;
1867
1868 return sprintf(buf, "%d %d %d\n", x, y, z);
1869}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001870static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001871
Azael Avalos9d309842015-02-10 23:43:59 -07001872static ssize_t usb_sleep_charge_show(struct device *dev,
1873 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001874{
1875 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1876 u32 mode;
1877 int ret;
1878
1879 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1880 if (ret < 0)
1881 return ret;
1882
1883 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1884}
1885
Azael Avalos9d309842015-02-10 23:43:59 -07001886static ssize_t usb_sleep_charge_store(struct device *dev,
1887 struct device_attribute *attr,
1888 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001889{
1890 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1891 u32 mode;
1892 int state;
1893 int ret;
1894
1895 ret = kstrtoint(buf, 0, &state);
1896 if (ret)
1897 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001898 /*
1899 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001900 * 0 - Disabled
1901 * 1 - Alternate (Non USB conformant devices that require more power)
1902 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001903 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001904 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001905 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001906 return -EINVAL;
1907
1908 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001909 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001910 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001911 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001912 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001913 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001914 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001915 mode |= SCI_USB_CHARGE_AUTO;
1916 else if (state == 3)
1917 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001918
1919 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1920 if (ret)
1921 return ret;
1922
1923 return count;
1924}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001925static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001926
Azael Avalos182bcaa2015-01-18 18:30:23 -07001927static ssize_t sleep_functions_on_battery_show(struct device *dev,
1928 struct device_attribute *attr,
1929 char *buf)
1930{
1931 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1932 u32 state;
1933 int bat_lvl;
1934 int status;
1935 int ret;
1936 int tmp;
1937
1938 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1939 if (ret < 0)
1940 return ret;
1941
1942 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1943 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1944 status = (tmp == 0x4) ? 1 : 0;
1945 /* Determine the battery level set */
1946 bat_lvl = state >> HCI_MISC_SHIFT;
1947
1948 return sprintf(buf, "%d %d\n", status, bat_lvl);
1949}
1950
1951static ssize_t sleep_functions_on_battery_store(struct device *dev,
1952 struct device_attribute *attr,
1953 const char *buf, size_t count)
1954{
1955 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1956 u32 status;
1957 int value;
1958 int ret;
1959 int tmp;
1960
1961 ret = kstrtoint(buf, 0, &value);
1962 if (ret)
1963 return ret;
1964
Darren Harte0769fe2015-02-11 20:50:08 -08001965 /*
1966 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07001967 * 0 - Disabled
1968 * 1-100 - Enabled
1969 */
1970 if (value < 0 || value > 100)
1971 return -EINVAL;
1972
1973 if (value == 0) {
1974 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1975 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1976 } else {
1977 tmp = value << HCI_MISC_SHIFT;
1978 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1979 }
1980 ret = toshiba_sleep_functions_status_set(toshiba, status);
1981 if (ret < 0)
1982 return ret;
1983
1984 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
1985
1986 return count;
1987}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001988static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001989
Azael Avalos9d309842015-02-10 23:43:59 -07001990static ssize_t usb_rapid_charge_show(struct device *dev,
1991 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07001992{
1993 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1994 u32 state;
1995 int ret;
1996
1997 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
1998 if (ret < 0)
1999 return ret;
2000
2001 return sprintf(buf, "%d\n", state);
2002}
2003
Azael Avalos9d309842015-02-10 23:43:59 -07002004static ssize_t usb_rapid_charge_store(struct device *dev,
2005 struct device_attribute *attr,
2006 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002007{
2008 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2009 int state;
2010 int ret;
2011
2012 ret = kstrtoint(buf, 0, &state);
2013 if (ret)
2014 return ret;
2015 if (state != 0 && state != 1)
2016 return -EINVAL;
2017
2018 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2019 if (ret)
2020 return ret;
2021
2022 return count;
2023}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002024static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002025
Azael Avalos9d309842015-02-10 23:43:59 -07002026static ssize_t usb_sleep_music_show(struct device *dev,
2027 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002028{
2029 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2030 u32 state;
2031 int ret;
2032
2033 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2034 if (ret < 0)
2035 return ret;
2036
2037 return sprintf(buf, "%d\n", state);
2038}
2039
Azael Avalos9d309842015-02-10 23:43:59 -07002040static ssize_t usb_sleep_music_store(struct device *dev,
2041 struct device_attribute *attr,
2042 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002043{
2044 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2045 int state;
2046 int ret;
2047
2048 ret = kstrtoint(buf, 0, &state);
2049 if (ret)
2050 return ret;
2051 if (state != 0 && state != 1)
2052 return -EINVAL;
2053
2054 ret = toshiba_usb_sleep_music_set(toshiba, state);
2055 if (ret)
2056 return ret;
2057
2058 return count;
2059}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002060static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002061
Azael Avalos9d309842015-02-10 23:43:59 -07002062static ssize_t kbd_function_keys_show(struct device *dev,
2063 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002064{
2065 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2066 int mode;
2067 int ret;
2068
2069 ret = toshiba_function_keys_get(toshiba, &mode);
2070 if (ret < 0)
2071 return ret;
2072
2073 return sprintf(buf, "%d\n", mode);
2074}
2075
Azael Avalos9d309842015-02-10 23:43:59 -07002076static ssize_t kbd_function_keys_store(struct device *dev,
2077 struct device_attribute *attr,
2078 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002079{
2080 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2081 int mode;
2082 int ret;
2083
2084 ret = kstrtoint(buf, 0, &mode);
2085 if (ret)
2086 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002087 /*
2088 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002089 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2090 * 1 - Special functions (Opposite of the above setting)
2091 */
2092 if (mode != 0 && mode != 1)
2093 return -EINVAL;
2094
2095 ret = toshiba_function_keys_set(toshiba, mode);
2096 if (ret)
2097 return ret;
2098
2099 pr_info("Reboot for changes to KBD Function Keys to take effect");
2100
2101 return count;
2102}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002103static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002104
Azael Avalos9d309842015-02-10 23:43:59 -07002105static ssize_t panel_power_on_show(struct device *dev,
2106 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002107{
2108 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2109 u32 state;
2110 int ret;
2111
2112 ret = toshiba_panel_power_on_get(toshiba, &state);
2113 if (ret < 0)
2114 return ret;
2115
2116 return sprintf(buf, "%d\n", state);
2117}
2118
Azael Avalos9d309842015-02-10 23:43:59 -07002119static ssize_t panel_power_on_store(struct device *dev,
2120 struct device_attribute *attr,
2121 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002122{
2123 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2124 int state;
2125 int ret;
2126
2127 ret = kstrtoint(buf, 0, &state);
2128 if (ret)
2129 return ret;
2130 if (state != 0 && state != 1)
2131 return -EINVAL;
2132
2133 ret = toshiba_panel_power_on_set(toshiba, state);
2134 if (ret)
2135 return ret;
2136
2137 pr_info("Reboot for changes to Panel Power ON to take effect");
2138
2139 return count;
2140}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002141static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002142
Azael Avalos9d309842015-02-10 23:43:59 -07002143static ssize_t usb_three_show(struct device *dev,
2144 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002145{
2146 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2147 u32 state;
2148 int ret;
2149
2150 ret = toshiba_usb_three_get(toshiba, &state);
2151 if (ret < 0)
2152 return ret;
2153
2154 return sprintf(buf, "%d\n", state);
2155}
2156
Azael Avalos9d309842015-02-10 23:43:59 -07002157static ssize_t usb_three_store(struct device *dev,
2158 struct device_attribute *attr,
2159 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002160{
2161 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2162 int state;
2163 int ret;
2164
2165 ret = kstrtoint(buf, 0, &state);
2166 if (ret)
2167 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002168 /*
2169 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002170 * 0 - Disabled (Acts like a USB 2 port, saving power)
2171 * 1 - Enabled
2172 */
2173 if (state != 0 && state != 1)
2174 return -EINVAL;
2175
2176 ret = toshiba_usb_three_set(toshiba, state);
2177 if (ret)
2178 return ret;
2179
2180 pr_info("Reboot for changes to USB 3 to take effect");
2181
2182 return count;
2183}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002184static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002185
2186static struct attribute *toshiba_attributes[] = {
2187 &dev_attr_version.attr,
2188 &dev_attr_fan.attr,
2189 &dev_attr_kbd_backlight_mode.attr,
2190 &dev_attr_kbd_type.attr,
2191 &dev_attr_available_kbd_modes.attr,
2192 &dev_attr_kbd_backlight_timeout.attr,
2193 &dev_attr_touchpad.attr,
2194 &dev_attr_position.attr,
2195 &dev_attr_usb_sleep_charge.attr,
2196 &dev_attr_sleep_functions_on_battery.attr,
2197 &dev_attr_usb_rapid_charge.attr,
2198 &dev_attr_usb_sleep_music.attr,
2199 &dev_attr_kbd_function_keys.attr,
2200 &dev_attr_panel_power_on.attr,
2201 &dev_attr_usb_three.attr,
2202 NULL,
2203};
2204
Azael Avalos360f0f32014-03-25 20:38:31 -06002205static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2206 struct attribute *attr, int idx)
2207{
2208 struct device *dev = container_of(kobj, struct device, kobj);
2209 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2210 bool exists = true;
2211
Azael Avalos94477d42015-02-10 21:09:17 -07002212 if (attr == &dev_attr_fan.attr)
2213 exists = (drv->fan_supported) ? true : false;
2214 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002215 exists = (drv->kbd_illum_supported) ? true : false;
2216 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2217 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002218 else if (attr == &dev_attr_touchpad.attr)
2219 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002220 else if (attr == &dev_attr_position.attr)
2221 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002222 else if (attr == &dev_attr_usb_sleep_charge.attr)
2223 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002224 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2225 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002226 else if (attr == &dev_attr_usb_rapid_charge.attr)
2227 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002228 else if (attr == &dev_attr_usb_sleep_music.attr)
2229 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002230 else if (attr == &dev_attr_kbd_function_keys.attr)
2231 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002232 else if (attr == &dev_attr_panel_power_on.attr)
2233 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002234 else if (attr == &dev_attr_usb_three.attr)
2235 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002236
2237 return exists ? attr->mode : 0;
2238}
2239
Azael Avalos9bd12132015-02-10 23:43:58 -07002240static struct attribute_group toshiba_attr_group = {
2241 .is_visible = toshiba_sysfs_is_visible,
2242 .attrs = toshiba_attributes,
2243};
2244
Azael Avalos1f28f292014-12-04 20:22:45 -07002245/*
Azael Avalosfc5462f2015-07-22 18:09:11 -06002246 * Misc device
2247 */
2248static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2249{
2250 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2251 regs->edx, regs->esi, regs->edi };
2252 u32 out[TCI_WORDS];
2253 acpi_status status;
2254
2255 status = tci_raw(toshiba_acpi, in, out);
2256 if (ACPI_FAILURE(status)) {
2257 pr_err("ACPI call to query SMM registers failed\n");
2258 return -EIO;
2259 }
2260
2261 /* Fillout the SMM struct with the TCI call results */
2262 regs->eax = out[0];
2263 regs->ebx = out[1];
2264 regs->ecx = out[2];
2265 regs->edx = out[3];
2266 regs->esi = out[4];
2267 regs->edi = out[5];
2268
2269 return 0;
2270}
2271
2272static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2273 unsigned long arg)
2274{
2275 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2276 SMMRegisters regs;
2277 int ret;
2278
2279 if (!argp)
2280 return -EINVAL;
2281
2282 switch (cmd) {
2283 case TOSH_SMM:
2284 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2285 return -EFAULT;
2286 ret = toshiba_acpi_smm_bridge(&regs);
2287 if (ret)
2288 return ret;
2289 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2290 return -EFAULT;
2291 break;
2292 case TOSHIBA_ACPI_SCI:
2293 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2294 return -EFAULT;
2295 /* Ensure we are being called with a SCI_{GET, SET} register */
2296 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2297 return -EINVAL;
2298 if (!sci_open(toshiba_acpi))
2299 return -EIO;
2300 ret = toshiba_acpi_smm_bridge(&regs);
2301 sci_close(toshiba_acpi);
2302 if (ret)
2303 return ret;
2304 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2305 return -EFAULT;
2306 break;
2307 default:
2308 return -EINVAL;
2309 }
2310
2311 return 0;
2312}
2313
2314static const struct file_operations toshiba_acpi_fops = {
2315 .owner = THIS_MODULE,
2316 .unlocked_ioctl = toshiba_acpi_ioctl,
2317 .llseek = noop_llseek,
2318};
2319
2320/*
Azael Avalos1f28f292014-12-04 20:22:45 -07002321 * Hotkeys
2322 */
2323static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2324{
2325 acpi_status status;
2326 u32 result;
2327
2328 status = acpi_evaluate_object(dev->acpi_dev->handle,
2329 "ENAB", NULL, NULL);
2330 if (ACPI_FAILURE(status))
2331 return -ENODEV;
2332
Azael Avalosd37782b2015-05-06 09:35:10 -06002333 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
Azael Avalos1f28f292014-12-04 20:22:45 -07002334 if (result == TOS_FAILURE)
2335 return -EIO;
2336 else if (result == TOS_NOT_SUPPORTED)
2337 return -ENODEV;
2338
2339 return 0;
2340}
2341
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002342static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2343{
2344 u32 result;
2345
2346 /*
2347 * Re-activate the hotkeys, but this time, we are using the
2348 * "Special Functions" mode.
2349 */
Azael Avalosd37782b2015-05-06 09:35:10 -06002350 result = hci_write(dev, HCI_HOTKEY_EVENT,
2351 HCI_HOTKEY_SPECIAL_FUNCTIONS);
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002352 if (result != TOS_SUCCESS)
2353 pr_err("Could not enable the Special Function mode\n");
2354}
2355
Seth Forshee29cd2932012-01-18 13:44:09 -06002356static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2357 struct serio *port)
2358{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002359 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002360 return false;
2361
2362 if (unlikely(data == 0xe0))
2363 return false;
2364
2365 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2366 schedule_work(&toshiba_acpi->hotkey_work);
2367 return true;
2368 }
2369
2370 return false;
2371}
2372
2373static void toshiba_acpi_hotkey_work(struct work_struct *work)
2374{
2375 acpi_handle ec_handle = ec_get_handle();
2376 acpi_status status;
2377
2378 if (!ec_handle)
2379 return;
2380
2381 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2382 if (ACPI_FAILURE(status))
2383 pr_err("ACPI NTFY method execution failed\n");
2384}
2385
2386/*
2387 * Returns hotkey scancode, or < 0 on failure.
2388 */
2389static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2390{
Zhang Rui74facaf2013-09-03 08:32:15 +08002391 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002392 acpi_status status;
2393
Zhang Rui74facaf2013-09-03 08:32:15 +08002394 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2395 NULL, &value);
2396 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002397 pr_err("ACPI INFO method execution failed\n");
2398 return -EIO;
2399 }
2400
Zhang Rui74facaf2013-09-03 08:32:15 +08002401 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002402}
2403
2404static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2405 int scancode)
2406{
2407 if (scancode == 0x100)
2408 return;
2409
Darren Harte0769fe2015-02-11 20:50:08 -08002410 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002411 if (scancode & 0x80)
2412 return;
2413
2414 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2415 pr_info("Unknown key %x\n", scancode);
2416}
2417
Azael Avalos71454d72014-12-04 20:22:46 -07002418static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2419{
Azael Avalos71454d72014-12-04 20:22:46 -07002420 if (dev->info_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002421 int scancode = toshiba_acpi_query_hotkey(dev);
2422
2423 if (scancode < 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002424 pr_err("Failed to query hotkey event\n");
Azael Avalos7deef552015-07-22 18:09:10 -06002425 } else if (scancode != 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002426 toshiba_acpi_report_hotkey(dev, scancode);
Azael Avalos7deef552015-07-22 18:09:10 -06002427 dev->key_event_valid = 1;
2428 dev->last_key_event = scancode;
2429 }
Azael Avalos71454d72014-12-04 20:22:46 -07002430 } else if (dev->system_event_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002431 u32 result;
2432 u32 value;
2433 int retries = 3;
2434
Azael Avalos71454d72014-12-04 20:22:46 -07002435 do {
Azael Avalos7deef552015-07-22 18:09:10 -06002436 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2437 switch (result) {
Azael Avalos71454d72014-12-04 20:22:46 -07002438 case TOS_SUCCESS:
2439 toshiba_acpi_report_hotkey(dev, (int)value);
Azael Avalos7deef552015-07-22 18:09:10 -06002440 dev->key_event_valid = 1;
2441 dev->last_key_event = value;
Azael Avalos71454d72014-12-04 20:22:46 -07002442 break;
2443 case TOS_NOT_SUPPORTED:
2444 /*
2445 * This is a workaround for an unresolved
2446 * issue on some machines where system events
2447 * sporadically become disabled.
2448 */
Azael Avalos7deef552015-07-22 18:09:10 -06002449 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2450 if (result == TOS_SUCCESS)
2451 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002452 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002453 default:
2454 retries--;
2455 break;
2456 }
Azael Avalos7deef552015-07-22 18:09:10 -06002457 } while (retries && result != TOS_FIFO_EMPTY);
Azael Avalos71454d72014-12-04 20:22:46 -07002458 }
2459}
2460
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002461static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002462{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002463 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002464 acpi_handle ec_handle;
2465 u32 events_type;
2466 u32 hci_result;
2467 int error;
2468
2469 error = toshiba_acpi_enable_hotkeys(dev);
2470 if (error)
2471 return error;
2472
2473 error = toshiba_hotkey_event_type_get(dev, &events_type);
2474 if (error) {
2475 pr_err("Unable to query Hotkey Event Type\n");
2476 return error;
2477 }
2478 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002479
Seth Forshee135740d2011-09-20 16:55:49 -05002480 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002481 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002482 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002483
2484 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002485 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002486 dev->hotkey_dev->id.bustype = BUS_HOST;
2487
Azael Avalosa2b34712015-03-20 16:55:17 -06002488 if (events_type == HCI_SYSTEM_TYPE1 ||
2489 !dev->kbd_function_keys_supported)
2490 keymap = toshiba_acpi_keymap;
2491 else if (events_type == HCI_SYSTEM_TYPE2 ||
2492 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002493 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002494 else
2495 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002496 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002497 if (error)
2498 goto err_free_dev;
2499
Seth Forshee29cd2932012-01-18 13:44:09 -06002500 /*
2501 * For some machines the SCI responsible for providing hotkey
2502 * notification doesn't fire. We can trigger the notification
2503 * whenever the Fn key is pressed using the NTFY method, if
2504 * supported, so if it's present set up an i8042 key filter
2505 * for this purpose.
2506 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002507 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002508 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002509 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2510
2511 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2512 if (error) {
2513 pr_err("Error installing key filter\n");
2514 goto err_free_keymap;
2515 }
2516
2517 dev->ntfy_supported = 1;
2518 }
2519
2520 /*
2521 * Determine hotkey query interface. Prefer using the INFO
2522 * method when it is available.
2523 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002524 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002525 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002526 else {
Azael Avalosd37782b2015-05-06 09:35:10 -06002527 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002528 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002529 dev->system_event_supported = 1;
2530 }
2531
2532 if (!dev->info_supported && !dev->system_event_supported) {
2533 pr_warn("No hotkey query interface found\n");
2534 goto err_remove_filter;
2535 }
2536
Seth Forshee135740d2011-09-20 16:55:49 -05002537 error = input_register_device(dev->hotkey_dev);
2538 if (error) {
2539 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002540 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002541 }
2542
2543 return 0;
2544
Seth Forshee29cd2932012-01-18 13:44:09 -06002545 err_remove_filter:
2546 if (dev->ntfy_supported)
2547 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002548 err_free_keymap:
2549 sparse_keymap_free(dev->hotkey_dev);
2550 err_free_dev:
2551 input_free_device(dev->hotkey_dev);
2552 dev->hotkey_dev = NULL;
2553 return error;
2554}
2555
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002556static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002557{
2558 struct backlight_properties props;
2559 int brightness;
2560 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002561 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002562
2563 /*
2564 * Some machines don't support the backlight methods at all, and
2565 * others support it read-only. Either of these is pretty useless,
2566 * so only register the backlight device if the backlight method
2567 * supports both reads and writes.
2568 */
2569 brightness = __get_lcd_brightness(dev);
2570 if (brightness < 0)
2571 return 0;
2572 ret = set_lcd_brightness(dev, brightness);
2573 if (ret) {
2574 pr_debug("Backlight method is read-only, disabling backlight support\n");
2575 return 0;
2576 }
2577
Akio Idehara121b7b02012-04-06 01:46:43 +09002578 /* Determine whether or not BIOS supports transflective backlight */
2579 ret = get_tr_backlight_status(dev, &enabled);
2580 dev->tr_backlight_supported = !ret;
2581
Hans de Goede358d6a22015-04-21 12:01:32 +02002582 /*
2583 * Tell acpi-video-detect code to prefer vendor backlight on all
2584 * systems with transflective backlight and on dmi matched systems.
2585 */
2586 if (dev->tr_backlight_supported ||
2587 dmi_check_system(toshiba_vendor_backlight_dmi))
Hans de Goede234b7cf2015-06-16 16:28:11 +02002588 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
Hans de Goede358d6a22015-04-21 12:01:32 +02002589
Hans de Goede234b7cf2015-06-16 16:28:11 +02002590 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
Hans de Goede358d6a22015-04-21 12:01:32 +02002591 return 0;
2592
Matthew Garrett53039f22012-06-01 11:02:36 -04002593 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002594 props.type = BACKLIGHT_PLATFORM;
2595 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002596
Darren Harte0769fe2015-02-11 20:50:08 -08002597 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002598 if (dev->tr_backlight_supported)
2599 props.max_brightness++;
2600
Seth Forshee62cce752012-04-19 11:23:50 -05002601 dev->backlight_dev = backlight_device_register("toshiba",
2602 &dev->acpi_dev->dev,
2603 dev,
2604 &toshiba_backlight_data,
2605 &props);
2606 if (IS_ERR(dev->backlight_dev)) {
2607 ret = PTR_ERR(dev->backlight_dev);
2608 pr_err("Could not register toshiba backlight device\n");
2609 dev->backlight_dev = NULL;
2610 return ret;
2611 }
2612
2613 dev->backlight_dev->props.brightness = brightness;
2614 return 0;
2615}
2616
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002617static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002618{
2619 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2620
Azael Avalosfc5462f2015-07-22 18:09:11 -06002621 misc_deregister(&dev->miscdev);
2622
Seth Forshee36d03f92011-09-20 16:55:53 -05002623 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002624
Azael Avalos360f0f32014-03-25 20:38:31 -06002625 if (dev->sysfs_created)
2626 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2627 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002628
Seth Forshee29cd2932012-01-18 13:44:09 -06002629 if (dev->ntfy_supported) {
2630 i8042_remove_filter(toshiba_acpi_i8042_filter);
2631 cancel_work_sync(&dev->hotkey_work);
2632 }
2633
Seth Forshee135740d2011-09-20 16:55:49 -05002634 if (dev->hotkey_dev) {
2635 input_unregister_device(dev->hotkey_dev);
2636 sparse_keymap_free(dev->hotkey_dev);
2637 }
2638
Markus Elfring00981812014-11-24 20:30:29 +01002639 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002640
Seth Forshee36d03f92011-09-20 16:55:53 -05002641 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002642 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002643
Azael Avalos360f0f32014-03-25 20:38:31 -06002644 if (dev->kbd_led_registered)
2645 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002646
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002647 if (dev->eco_supported)
2648 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002649
Seth Forshee29cd2932012-01-18 13:44:09 -06002650 if (toshiba_acpi)
2651 toshiba_acpi = NULL;
2652
Seth Forshee135740d2011-09-20 16:55:49 -05002653 kfree(dev);
2654
2655 return 0;
2656}
2657
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002658static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002659{
Zhang Ruie2e19602013-09-03 08:32:06 +08002660 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002661 return "GHCI";
2662
Zhang Ruie2e19602013-09-03 08:32:06 +08002663 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002664 return "SPFC";
2665
2666 return NULL;
2667}
2668
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002669static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002670{
2671 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002672 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002673 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002674 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002675 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002676
Seth Forshee29cd2932012-01-18 13:44:09 -06002677 if (toshiba_acpi)
2678 return -EBUSY;
2679
Seth Forshee135740d2011-09-20 16:55:49 -05002680 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2681 TOSHIBA_ACPI_VERSION);
2682
Seth Forsheea540d6b2011-09-20 16:55:52 -05002683 hci_method = find_hci_method(acpi_dev->handle);
2684 if (!hci_method) {
2685 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002686 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002687 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002688
Seth Forshee135740d2011-09-20 16:55:49 -05002689 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2690 if (!dev)
2691 return -ENOMEM;
2692 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002693 dev->method_hci = hci_method;
Azael Avalosfc5462f2015-07-22 18:09:11 -06002694 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2695 dev->miscdev.name = "toshiba_acpi";
2696 dev->miscdev.fops = &toshiba_acpi_fops;
2697
2698 ret = misc_register(&dev->miscdev);
2699 if (ret) {
2700 pr_err("Failed to register miscdevice\n");
2701 kfree(dev);
2702 return ret;
2703 }
2704
Seth Forshee135740d2011-09-20 16:55:49 -05002705 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002706 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002707
Azael Avalosa2b34712015-03-20 16:55:17 -06002708 /* Query the BIOS for supported features */
2709
2710 /*
2711 * The "Special Functions" are always supported by the laptops
2712 * with the new keyboard layout, query for its presence to help
2713 * determine the keymap layout to use.
2714 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002715 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002716 dev->kbd_function_keys_supported = !ret;
2717
Seth Forshee6e02cc72011-09-20 16:55:51 -05002718 if (toshiba_acpi_setup_keyboard(dev))
2719 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002720
Seth Forshee62cce752012-04-19 11:23:50 -05002721 ret = toshiba_acpi_setup_backlight(dev);
2722 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002723 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002724
Seth Forshee135740d2011-09-20 16:55:49 -05002725 if (toshiba_illumination_available(dev)) {
2726 dev->led_dev.name = "toshiba::illumination";
2727 dev->led_dev.max_brightness = 1;
2728 dev->led_dev.brightness_set = toshiba_illumination_set;
2729 dev->led_dev.brightness_get = toshiba_illumination_get;
2730 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002731 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002732 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002733
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002734 if (toshiba_eco_mode_available(dev)) {
2735 dev->eco_led.name = "toshiba::eco_mode";
2736 dev->eco_led.max_brightness = 1;
2737 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2738 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2739 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2740 dev->eco_supported = 1;
2741 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002742
Azael Avalos93f8c162014-09-12 18:50:36 -06002743 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002744 /*
2745 * Only register the LED if KBD illumination is supported
2746 * and the keyboard backlight operation mode is set to FN-Z
2747 */
2748 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2749 dev->kbd_led.name = "toshiba::kbd_backlight";
2750 dev->kbd_led.max_brightness = 1;
2751 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2752 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2753 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2754 dev->kbd_led_registered = 1;
2755 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002756
Azael Avalos9d8658a2014-03-25 20:38:32 -06002757 ret = toshiba_touchpad_get(dev, &dummy);
2758 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002759
Azael Avalos5a2813e2014-03-25 20:38:34 -06002760 ret = toshiba_accelerometer_supported(dev);
2761 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002762
Azael Avalosc8c91842015-04-02 19:26:20 -06002763 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002764
Azael Avalosbb3fe012015-01-18 18:30:24 -07002765 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2766 dev->usb_rapid_charge_supported = !ret;
2767
Azael Avalos172ce0a2015-01-18 18:30:25 -07002768 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2769 dev->usb_sleep_music_supported = !ret;
2770
Azael Avalos35d53ce2015-02-10 21:09:19 -07002771 ret = toshiba_panel_power_on_get(dev, &dummy);
2772 dev->panel_power_on_supported = !ret;
2773
Azael Avalos17fe4b32015-02-10 21:09:20 -07002774 ret = toshiba_usb_three_get(dev, &dummy);
2775 dev->usb_three_supported = !ret;
2776
Seth Forshee36d03f92011-09-20 16:55:53 -05002777 ret = get_video_status(dev, &dummy);
2778 dev->video_supported = !ret;
2779
2780 ret = get_fan_status(dev, &dummy);
2781 dev->fan_supported = !ret;
2782
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002783 /*
2784 * Enable the "Special Functions" mode only if they are
2785 * supported and if they are activated.
2786 */
2787 if (dev->kbd_function_keys_supported && special_functions)
2788 toshiba_acpi_enable_special_functions(dev);
2789
Azael Avalos360f0f32014-03-25 20:38:31 -06002790 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2791 &toshiba_attr_group);
2792 if (ret) {
2793 dev->sysfs_created = 0;
2794 goto error;
2795 }
2796 dev->sysfs_created = !ret;
2797
Seth Forshee36d03f92011-09-20 16:55:53 -05002798 create_toshiba_proc_entries(dev);
2799
Seth Forshee29cd2932012-01-18 13:44:09 -06002800 toshiba_acpi = dev;
2801
Seth Forshee135740d2011-09-20 16:55:49 -05002802 return 0;
2803
2804error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002805 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002806 return ret;
2807}
2808
2809static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2810{
2811 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002812 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002813
Azael Avalos71454d72014-12-04 20:22:46 -07002814 switch (event) {
2815 case 0x80: /* Hotkeys and some system events */
2816 toshiba_acpi_process_hotkeys(dev);
2817 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002818 case 0x81: /* Dock events */
2819 case 0x82:
2820 case 0x83:
2821 pr_info("Dock event received %x\n", event);
2822 break;
2823 case 0x88: /* Thermal events */
2824 pr_info("Thermal event received\n");
2825 break;
2826 case 0x8f: /* LID closed */
2827 case 0x90: /* LID is closed and Dock has been ejected */
2828 break;
2829 case 0x8c: /* SATA power events */
2830 case 0x8b:
2831 pr_info("SATA power event received %x\n", event);
2832 break;
Azael Avalos80546902014-12-04 20:22:47 -07002833 case 0x92: /* Keyboard backlight mode changed */
2834 /* Update sysfs entries */
2835 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2836 &toshiba_attr_group);
2837 if (ret)
2838 pr_err("Unable to update sysfs entries\n");
2839 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002840 case 0x85: /* Unknown */
2841 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002842 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002843 case 0x94: /* Unknown */
2844 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002845 default:
2846 pr_info("Unknown event received %x\n", event);
2847 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002848 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002849
2850 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2851 dev_name(&acpi_dev->dev),
2852 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002853}
2854
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002855#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002856static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002857{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002858 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002859 u32 result;
2860
2861 if (dev->hotkey_dev)
Azael Avalosd37782b2015-05-06 09:35:10 -06002862 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002863
2864 return 0;
2865}
2866
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002867static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002868{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002869 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002870 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002871
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002872 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002873 error = toshiba_acpi_enable_hotkeys(dev);
2874 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002875 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002876 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002877
2878 return 0;
2879}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002880#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002881
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002882static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2883 toshiba_acpi_suspend, toshiba_acpi_resume);
2884
Seth Forshee135740d2011-09-20 16:55:49 -05002885static struct acpi_driver toshiba_acpi_driver = {
2886 .name = "Toshiba ACPI driver",
2887 .owner = THIS_MODULE,
2888 .ids = toshiba_device_ids,
2889 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2890 .ops = {
2891 .add = toshiba_acpi_add,
2892 .remove = toshiba_acpi_remove,
2893 .notify = toshiba_acpi_notify,
2894 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002895 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002896};
Holger Machtc9263552006-10-20 14:30:29 -07002897
Len Brown4be44fc2005-08-05 00:44:28 -04002898static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
Seth Forshee135740d2011-09-20 16:55:49 -05002900 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Seth Forsheef11f9992012-01-18 13:44:11 -06002902 /*
2903 * Machines with this WMI guid aren't supported due to bugs in
2904 * their AML. This check relies on wmi initializing before
2905 * toshiba_acpi to guarantee guids have been identified.
2906 */
2907 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2908 return -ENODEV;
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2911 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002912 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002913 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Seth Forshee135740d2011-09-20 16:55:49 -05002916 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2917 if (ret) {
2918 pr_err("Failed to register ACPI driver: %d\n", ret);
2919 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002920 }
2921
Seth Forshee135740d2011-09-20 16:55:49 -05002922 return ret;
2923}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002924
Seth Forshee135740d2011-09-20 16:55:49 -05002925static void __exit toshiba_acpi_exit(void)
2926{
2927 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2928 if (toshiba_proc_dir)
2929 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932module_init(toshiba_acpi_init);
2933module_exit(toshiba_acpi_exit);