blob: 6013a11caeea81f130a0c406fd7128f4d4236a4f [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
Azael Avalos5a2813e2014-03-25 20:38:34 -0600116#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600117#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600118#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600119#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600120#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700121#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600122#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700123#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600124#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700125#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700126#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600127#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700128#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600130/* Field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600131#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600132#define HCI_HOTKEY_DISABLE 0x0b
133#define HCI_HOTKEY_ENABLE 0x09
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600134#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define HCI_LCD_BRIGHTNESS_BITS 3
136#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
137#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600138#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600139#define HCI_SYSTEM_TYPE1 0x10
140#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define HCI_VIDEO_OUT_LCD 0x1
142#define HCI_VIDEO_OUT_CRT 0x2
143#define HCI_VIDEO_OUT_TV 0x4
Azael Avalos93f8c162014-09-12 18:50:36 -0600144#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600145#define SCI_KBD_MODE_FNZ 0x1
146#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600147#define SCI_KBD_MODE_ON 0x8
148#define SCI_KBD_MODE_OFF 0x10
149#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700150#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600151#define SCI_USB_CHARGE_DISABLED 0x00
152#define SCI_USB_CHARGE_ALTERNATE 0x09
153#define SCI_USB_CHARGE_TYPICAL 0x11
154#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700155#define SCI_USB_CHARGE_BAT_MASK 0x7
156#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
157#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
158#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700159#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Seth Forshee135740d2011-09-20 16:55:49 -0500161struct toshiba_acpi_dev {
162 struct acpi_device *acpi_dev;
163 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500164 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600165 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500166 struct backlight_device *backlight_dev;
167 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600168 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600169 struct led_classdev eco_led;
Azael Avalosfc5462f2015-07-22 18:09:11 -0600170 struct miscdevice miscdev;
Seth Forshee36d03f92011-09-20 16:55:53 -0500171
Seth Forshee135740d2011-09-20 16:55:49 -0500172 int force_fan;
173 int last_key_event;
174 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600175 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600176 int kbd_mode;
177 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700178 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600179 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600180 int hotkey_event_type;
Seth Forshee135740d2011-09-20 16:55:49 -0500181
Dan Carpenter592b7462012-01-15 14:28:20 +0300182 unsigned int illumination_supported:1;
183 unsigned int video_supported:1;
184 unsigned int fan_supported:1;
185 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600186 unsigned int ntfy_supported:1;
187 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900188 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600189 unsigned int kbd_illum_supported:1;
190 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600191 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600192 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600193 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700194 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700195 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700196 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700197 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700198 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700199 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600200 unsigned int sysfs_created:1;
Seth Forshee135740d2011-09-20 16:55:49 -0500201};
202
Seth Forshee29cd2932012-01-18 13:44:09 -0600203static struct toshiba_acpi_dev *toshiba_acpi;
204
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800205static const struct acpi_device_id toshiba_device_ids[] = {
206 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100207 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400208 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800209 {"TOS1900", 0},
210 {"", 0},
211};
212MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
213
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800214static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100215 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700216 { KE_KEY, 0x101, { KEY_MUTE } },
217 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
218 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600219 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600220 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
221 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700222 { KE_KEY, 0x13b, { KEY_COFFEE } },
223 { KE_KEY, 0x13c, { KEY_BATTERY } },
224 { KE_KEY, 0x13d, { KEY_SLEEP } },
225 { KE_KEY, 0x13e, { KEY_SUSPEND } },
226 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
227 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
228 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
229 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600230 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100231 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700232 { KE_KEY, 0xb05, { KEY_PROG2 } },
233 { KE_KEY, 0xb06, { KEY_WWW } },
234 { KE_KEY, 0xb07, { KEY_MAIL } },
235 { KE_KEY, 0xb30, { KEY_STOP } },
236 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
237 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
238 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
239 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600240 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
241 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
242 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
243 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
244 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700245 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500246};
247
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200248static const struct key_entry toshiba_acpi_alt_keymap[] = {
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200249 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
250 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600251 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200252 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200253 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
254 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600255 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200256 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600257 { KE_KEY, 0x157, { KEY_MUTE } },
258 { KE_KEY, 0x158, { KEY_WLAN } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200259 { KE_END, 0 },
260};
261
Darren Harte0769fe2015-02-11 20:50:08 -0800262/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200263 * List of models which have a broken acpi-video backlight interface and thus
264 * need to use the toshiba (vendor) interface instead.
265 */
266static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
267 {}
268};
269
270/*
Darren Harte0769fe2015-02-11 20:50:08 -0800271 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273
Azael Avalosb5163992015-02-10 23:43:56 -0700274static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 *word = (*word & ~mask) | (mask * value);
277}
278
Darren Harte0769fe2015-02-11 20:50:08 -0800279/*
280 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 acpi_status status;
286
Zhang Rui619400d2013-09-03 08:31:56 +0800287 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500288 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Darren Harte0769fe2015-02-11 20:50:08 -0800291/*
292 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600293 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Azael Avalos258c5902014-09-29 20:40:07 -0600295static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
296 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600299 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600301 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 acpi_status status;
303 int i;
304
Azael Avalos258c5902014-09-29 20:40:07 -0600305 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600307 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 in_objs[i].type = ACPI_TYPE_INTEGER;
309 in_objs[i].integer.value = in[i];
310 }
311
312 results.length = sizeof(out_objs);
313 results.pointer = out_objs;
314
Seth Forshee6e02cc72011-09-20 16:55:51 -0500315 status = acpi_evaluate_object(dev->acpi_dev->handle,
316 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400317 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600318 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700319 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 return status;
324}
325
Darren Harte0769fe2015-02-11 20:50:08 -0800326/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600327 * Common hci tasks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
329 * In addition to the ACPI status, the HCI system returns a result which
330 * may be useful (such as "not supported").
331 */
332
Azael Avalosd37782b2015-05-06 09:35:10 -0600333static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Azael Avalos258c5902014-09-29 20:40:07 -0600335 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
336 u32 out[TCI_WORDS];
337 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600338
339 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Azael Avalosd37782b2015-05-06 09:35:10 -0600342static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Azael Avalos258c5902014-09-29 20:40:07 -0600344 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
345 u32 out[TCI_WORDS];
346 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700347
Azael Avalos893f3f62014-09-29 20:40:09 -0600348 if (ACPI_FAILURE(status))
349 return TOS_FAILURE;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600352
353 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Darren Harte0769fe2015-02-11 20:50:08 -0800356/*
357 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600358 */
359
360static int sci_open(struct toshiba_acpi_dev *dev)
361{
Azael Avalos258c5902014-09-29 20:40:07 -0600362 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
363 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600364 acpi_status status;
365
Azael Avalos258c5902014-09-29 20:40:07 -0600366 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600367 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600368 pr_err("ACPI call to open SCI failed\n");
369 return 0;
370 }
371
Azael Avalos1864bbc2014-09-29 20:40:08 -0600372 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600373 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600374 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600375 pr_info("Toshiba SCI already opened\n");
376 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700377 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800378 /*
379 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700380 * implemented and return 0x8000 (Not Supported), failing to
381 * register some supported features.
382 *
383 * Simply return 1 if we hit those affected laptops to make the
384 * supported features work.
385 *
386 * In the case that some laptops really do not support the SCI,
387 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
388 * and thus, not registering support for the queried feature.
389 */
390 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600391 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600392 pr_info("Toshiba SCI is not present\n");
393 }
394
395 return 0;
396}
397
398static void sci_close(struct toshiba_acpi_dev *dev)
399{
Azael Avalos258c5902014-09-29 20:40:07 -0600400 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
401 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600402 acpi_status status;
403
Azael Avalos258c5902014-09-29 20:40:07 -0600404 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600405 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600406 pr_err("ACPI call to close SCI failed\n");
407 return;
408 }
409
Azael Avalos1864bbc2014-09-29 20:40:08 -0600410 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600411 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600412 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600413 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600414 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600415 pr_info("Toshiba SCI is not present\n");
416}
417
Azael Avalos893f3f62014-09-29 20:40:09 -0600418static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600419{
Azael Avalos258c5902014-09-29 20:40:07 -0600420 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
421 u32 out[TCI_WORDS];
422 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700423
Azael Avalos893f3f62014-09-29 20:40:09 -0600424 if (ACPI_FAILURE(status))
425 return TOS_FAILURE;
426
Azael Avalos84a62732014-03-25 20:38:29 -0600427 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600428
429 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600430}
431
Azael Avalos893f3f62014-09-29 20:40:09 -0600432static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600433{
Azael Avalos258c5902014-09-29 20:40:07 -0600434 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
435 u32 out[TCI_WORDS];
436 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600437
438 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600439}
440
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200441/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500442static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200443{
Azael Avalos258c5902014-09-29 20:40:07 -0600444 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
445 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200446 acpi_status status;
447
Azael Avalosfdb79082014-03-25 20:38:30 -0600448 if (!sci_open(dev))
449 return 0;
450
Azael Avalos258c5902014-09-29 20:40:07 -0600451 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600452 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600453 if (ACPI_FAILURE(status)) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600454 pr_err("ACPI call to query Illumination support failed\n");
455 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600456 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700457 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200458 return 0;
459 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600460
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200461 return 1;
462}
463
464static void toshiba_illumination_set(struct led_classdev *cdev,
465 enum led_brightness brightness)
466{
Seth Forshee135740d2011-09-20 16:55:49 -0500467 struct toshiba_acpi_dev *dev = container_of(cdev,
468 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600469 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200470
471 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600472 if (!sci_open(dev))
473 return;
474
475 /* Switch the illumination on/off */
476 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600477 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600478 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600479 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600480 pr_err("ACPI call for illumination failed\n");
481 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600482 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600483 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200484 return;
485 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200486}
487
488static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
489{
Seth Forshee135740d2011-09-20 16:55:49 -0500490 struct toshiba_acpi_dev *dev = container_of(cdev,
491 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600492 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200493
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600494 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600495 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200496 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200497
498 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600499 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600500 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600501 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600502 pr_err("ACPI call for illumination failed\n");
503 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600504 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600505 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200506 return LED_OFF;
507 }
508
Azael Avalosfdb79082014-03-25 20:38:30 -0600509 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200510}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400511
Azael Avalos360f0f32014-03-25 20:38:31 -0600512/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600513static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
514{
Azael Avalos258c5902014-09-29 20:40:07 -0600515 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
516 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600517 acpi_status status;
518
519 if (!sci_open(dev))
520 return 0;
521
Azael Avalos258c5902014-09-29 20:40:07 -0600522 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600523 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600524 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600525 pr_err("ACPI call to query kbd illumination support failed\n");
526 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600527 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600528 pr_info("Keyboard illumination not available\n");
529 return 0;
530 }
531
Darren Harte0769fe2015-02-11 20:50:08 -0800532 /*
533 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600534 * previous kbd backlight implementation set this to
535 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800536 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600537 */
538 if (out[3] == SCI_KBD_TIME_MAX)
539 dev->kbd_type = 2;
540 else
541 dev->kbd_type = 1;
542 /* Get the current keyboard backlight mode */
543 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
544 /* Get the current time (1-60 seconds) */
545 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
546
547 return 1;
548}
549
Azael Avalos360f0f32014-03-25 20:38:31 -0600550static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
551{
552 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600553
554 if (!sci_open(dev))
555 return -EIO;
556
Azael Avalos893f3f62014-09-29 20:40:09 -0600557 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600558 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600559 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600560 pr_err("ACPI call to set KBD backlight status failed\n");
561 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600562 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600563 pr_info("Keyboard backlight status not supported\n");
564 return -ENODEV;
565 }
566
567 return 0;
568}
569
570static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
571{
572 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600573
574 if (!sci_open(dev))
575 return -EIO;
576
Azael Avalos893f3f62014-09-29 20:40:09 -0600577 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600578 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600579 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600580 pr_err("ACPI call to get KBD backlight status failed\n");
581 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600582 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600583 pr_info("Keyboard backlight status not supported\n");
584 return -ENODEV;
585 }
586
587 return 0;
588}
589
590static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
591{
592 struct toshiba_acpi_dev *dev = container_of(cdev,
593 struct toshiba_acpi_dev, kbd_led);
594 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600595
596 /* Check the keyboard backlight state */
Azael Avalosd37782b2015-05-06 09:35:10 -0600597 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600598 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600599 pr_err("ACPI call to get the keyboard backlight failed\n");
600 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600601 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600602 pr_info("Keyboard backlight not supported\n");
603 return LED_OFF;
604 }
605
606 return state ? LED_FULL : LED_OFF;
607}
608
609static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
610 enum led_brightness brightness)
611{
612 struct toshiba_acpi_dev *dev = container_of(cdev,
613 struct toshiba_acpi_dev, kbd_led);
614 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600615
616 /* Set the keyboard backlight state */
617 state = brightness ? 1 : 0;
Azael Avalosd37782b2015-05-06 09:35:10 -0600618 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalos893f3f62014-09-29 20:40:09 -0600619 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600620 pr_err("ACPI call to set KBD Illumination mode failed\n");
621 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600622 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600623 pr_info("Keyboard backlight not supported\n");
624 return;
625 }
626}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400627
Azael Avalos9d8658a2014-03-25 20:38:32 -0600628/* TouchPad support */
629static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
630{
631 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600632
633 if (!sci_open(dev))
634 return -EIO;
635
Azael Avalos893f3f62014-09-29 20:40:09 -0600636 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600637 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600638 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600639 pr_err("ACPI call to set the touchpad failed\n");
640 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600641 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600642 return -ENODEV;
643 }
644
645 return 0;
646}
647
648static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
649{
650 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600651
652 if (!sci_open(dev))
653 return -EIO;
654
Azael Avalos893f3f62014-09-29 20:40:09 -0600655 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600656 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600657 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600658 pr_err("ACPI call to query the touchpad failed\n");
659 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600660 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600661 return -ENODEV;
662 }
663
664 return 0;
665}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200666
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600667/* Eco Mode support */
668static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
669{
670 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700671 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600672 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600673
Azael Avalos258c5902014-09-29 20:40:07 -0600674 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600675 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700676 pr_err("ACPI call to get ECO led failed\n");
677 } else if (out[0] == TOS_NOT_INSTALLED) {
678 pr_info("ECO led not installed");
679 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800680 /*
681 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700682 * LED device is present, but that we just screwed the input
683 * parameters.
684 *
685 * Let's query the status of the LED to see if we really have a
686 * success response, indicating the actual presense of the LED,
687 * bail out otherwise.
688 */
689 in[3] = 1;
690 status = tci_raw(dev, in, out);
691 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
692 pr_err("ACPI call to get ECO led failed\n");
693 else if (out[0] == TOS_SUCCESS)
694 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600695 }
696
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700697 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600698}
699
Azael Avalosb5163992015-02-10 23:43:56 -0700700static enum led_brightness
701toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600702{
703 struct toshiba_acpi_dev *dev = container_of(cdev,
704 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600705 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
706 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600707 acpi_status status;
708
Azael Avalos258c5902014-09-29 20:40:07 -0600709 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600710 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600711 pr_err("ACPI call to get ECO led failed\n");
712 return LED_OFF;
713 }
714
715 return out[2] ? LED_FULL : LED_OFF;
716}
717
718static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
719 enum led_brightness brightness)
720{
721 struct toshiba_acpi_dev *dev = container_of(cdev,
722 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600723 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
724 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600725 acpi_status status;
726
727 /* Switch the Eco Mode led on/off */
728 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600729 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600730 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600731 pr_err("ACPI call to set ECO led failed\n");
732 return;
733 }
734}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400735
Azael Avalos5a2813e2014-03-25 20:38:34 -0600736/* Accelerometer support */
737static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
738{
Azael Avalos258c5902014-09-29 20:40:07 -0600739 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
740 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600741 acpi_status status;
742
Darren Harte0769fe2015-02-11 20:50:08 -0800743 /*
744 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600745 * this call also serves as initialization
746 */
Azael Avalos258c5902014-09-29 20:40:07 -0600747 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600748 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600749 pr_err("ACPI call to query the accelerometer failed\n");
750 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600751 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
752 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600753 pr_err("Accelerometer not initialized\n");
754 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600755 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600756 pr_info("Accelerometer not supported\n");
757 return -ENODEV;
758 }
759
760 return 0;
761}
762
763static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
764 u32 *xy, u32 *z)
765{
Azael Avalos258c5902014-09-29 20:40:07 -0600766 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
767 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600768 acpi_status status;
769
770 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600771 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600772 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600773 pr_err("ACPI call to query the accelerometer failed\n");
774 return -EIO;
775 }
776
777 *xy = out[2];
778 *z = out[4];
779
780 return 0;
781}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600782
Azael Avalose26ffe52015-01-18 18:30:22 -0700783/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600784static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
785{
786 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
787 u32 out[TCI_WORDS];
788 acpi_status status;
789
790 /* Set the feature to "not supported" in case of error */
791 dev->usb_sleep_charge_supported = 0;
792
793 if (!sci_open(dev))
794 return;
795
796 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600797 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600798 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
799 sci_close(dev);
800 return;
801 } else if (out[0] == TOS_NOT_SUPPORTED) {
802 pr_info("USB Sleep and Charge not supported\n");
803 sci_close(dev);
804 return;
805 } else if (out[0] == TOS_SUCCESS) {
806 dev->usbsc_mode_base = out[4];
807 }
808
809 in[5] = SCI_USB_CHARGE_BAT_LVL;
810 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600811 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600812 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
813 sci_close(dev);
814 return;
815 } else if (out[0] == TOS_NOT_SUPPORTED) {
816 pr_info("USB Sleep and Charge not supported\n");
817 sci_close(dev);
818 return;
819 } else if (out[0] == TOS_SUCCESS) {
820 dev->usbsc_bat_level = out[2];
821 /*
822 * If we reach this point, it means that the laptop has support
823 * for this feature and all values are initialized.
824 * Set it as supported.
825 */
826 dev->usb_sleep_charge_supported = 1;
827 }
828
829 sci_close(dev);
830}
831
Azael Avalose26ffe52015-01-18 18:30:22 -0700832static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
833 u32 *mode)
834{
835 u32 result;
836
837 if (!sci_open(dev))
838 return -EIO;
839
840 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
841 sci_close(dev);
842 if (result == TOS_FAILURE) {
843 pr_err("ACPI call to set USB S&C mode failed\n");
844 return -EIO;
845 } else if (result == TOS_NOT_SUPPORTED) {
846 pr_info("USB Sleep and Charge not supported\n");
847 return -ENODEV;
848 } else if (result == TOS_INPUT_DATA_ERROR) {
849 return -EIO;
850 }
851
852 return 0;
853}
854
855static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
856 u32 mode)
857{
858 u32 result;
859
860 if (!sci_open(dev))
861 return -EIO;
862
863 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
864 sci_close(dev);
865 if (result == TOS_FAILURE) {
866 pr_err("ACPI call to set USB S&C mode failed\n");
867 return -EIO;
868 } else if (result == TOS_NOT_SUPPORTED) {
869 pr_info("USB Sleep and Charge not supported\n");
870 return -ENODEV;
871 } else if (result == TOS_INPUT_DATA_ERROR) {
872 return -EIO;
873 }
874
875 return 0;
876}
877
Azael Avalos182bcaa2015-01-18 18:30:23 -0700878static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
879 u32 *mode)
880{
881 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
882 u32 out[TCI_WORDS];
883 acpi_status status;
884
885 if (!sci_open(dev))
886 return -EIO;
887
888 in[5] = SCI_USB_CHARGE_BAT_LVL;
889 status = tci_raw(dev, in, out);
890 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600891 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700892 pr_err("ACPI call to get USB S&C battery level failed\n");
893 return -EIO;
894 } else if (out[0] == TOS_NOT_SUPPORTED) {
895 pr_info("USB Sleep and Charge not supported\n");
896 return -ENODEV;
897 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
898 return -EIO;
899 }
900
901 *mode = out[2];
902
903 return 0;
904}
905
906static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
907 u32 mode)
908{
909 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
910 u32 out[TCI_WORDS];
911 acpi_status status;
912
913 if (!sci_open(dev))
914 return -EIO;
915
916 in[2] = mode;
917 in[5] = SCI_USB_CHARGE_BAT_LVL;
918 status = tci_raw(dev, in, out);
919 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600920 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700921 pr_err("ACPI call to set USB S&C battery level failed\n");
922 return -EIO;
923 } else if (out[0] == TOS_NOT_SUPPORTED) {
924 pr_info("USB Sleep and Charge not supported\n");
925 return -ENODEV;
926 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
927 return -EIO;
928 }
929
930 return 0;
931}
932
Azael Avalosbb3fe012015-01-18 18:30:24 -0700933static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
934 u32 *state)
935{
936 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
937 u32 out[TCI_WORDS];
938 acpi_status status;
939
940 if (!sci_open(dev))
941 return -EIO;
942
943 in[5] = SCI_USB_CHARGE_RAPID_DSP;
944 status = tci_raw(dev, in, out);
945 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600946 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600947 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700948 return -EIO;
949 } else if (out[0] == TOS_NOT_SUPPORTED ||
950 out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600951 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700952 return -ENODEV;
953 }
954
955 *state = out[2];
956
957 return 0;
958}
959
960static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
961 u32 state)
962{
963 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
964 u32 out[TCI_WORDS];
965 acpi_status status;
966
967 if (!sci_open(dev))
968 return -EIO;
969
970 in[2] = state;
971 in[5] = SCI_USB_CHARGE_RAPID_DSP;
972 status = tci_raw(dev, in, out);
973 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600974 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600975 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700976 return -EIO;
977 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600978 pr_info("USB Rapid Charge not supported\n");
Azael Avalosbb3fe012015-01-18 18:30:24 -0700979 return -ENODEV;
980 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
981 return -EIO;
982 }
983
984 return 0;
985}
986
Azael Avalos172ce0a2015-01-18 18:30:25 -0700987static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
988{
989 u32 result;
990
991 if (!sci_open(dev))
992 return -EIO;
993
994 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
995 sci_close(dev);
996 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600997 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -0700998 return -EIO;
999 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001000 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001001 return -ENODEV;
1002 } else if (result == TOS_INPUT_DATA_ERROR) {
1003 return -EIO;
1004 }
1005
1006 return 0;
1007}
1008
1009static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1010{
1011 u32 result;
1012
1013 if (!sci_open(dev))
1014 return -EIO;
1015
1016 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1017 sci_close(dev);
1018 if (result == TOS_FAILURE) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001019 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001020 return -EIO;
1021 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosbb26f182015-04-02 19:26:21 -06001022 pr_info("Sleep and Music not supported\n");
Azael Avalos172ce0a2015-01-18 18:30:25 -07001023 return -ENODEV;
1024 } else if (result == TOS_INPUT_DATA_ERROR) {
1025 return -EIO;
1026 }
1027
1028 return 0;
1029}
1030
Azael Avalosbae84192015-02-10 21:09:18 -07001031/* Keyboard function keys */
1032static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1033{
1034 u32 result;
1035
1036 if (!sci_open(dev))
1037 return -EIO;
1038
1039 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1040 sci_close(dev);
1041 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1042 pr_err("ACPI call to get KBD function keys failed\n");
1043 return -EIO;
1044 } else if (result == TOS_NOT_SUPPORTED) {
1045 pr_info("KBD function keys not supported\n");
1046 return -ENODEV;
1047 }
1048
1049 return 0;
1050}
1051
1052static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1053{
1054 u32 result;
1055
1056 if (!sci_open(dev))
1057 return -EIO;
1058
1059 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1060 sci_close(dev);
1061 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1062 pr_err("ACPI call to set KBD function keys failed\n");
1063 return -EIO;
1064 } else if (result == TOS_NOT_SUPPORTED) {
1065 pr_info("KBD function keys not supported\n");
1066 return -ENODEV;
1067 }
1068
1069 return 0;
1070}
1071
Azael Avalos35d53ce2015-02-10 21:09:19 -07001072/* Panel Power ON */
1073static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1074{
1075 u32 result;
1076
1077 if (!sci_open(dev))
1078 return -EIO;
1079
1080 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1081 sci_close(dev);
1082 if (result == TOS_FAILURE) {
1083 pr_err("ACPI call to get Panel Power ON failed\n");
1084 return -EIO;
1085 } else if (result == TOS_NOT_SUPPORTED) {
1086 pr_info("Panel Power on not supported\n");
1087 return -ENODEV;
1088 } else if (result == TOS_INPUT_DATA_ERROR) {
1089 return -EIO;
1090 }
1091
1092 return 0;
1093}
1094
1095static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1096{
1097 u32 result;
1098
1099 if (!sci_open(dev))
1100 return -EIO;
1101
1102 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1103 sci_close(dev);
1104 if (result == TOS_FAILURE) {
1105 pr_err("ACPI call to set Panel Power ON failed\n");
1106 return -EIO;
1107 } else if (result == TOS_NOT_SUPPORTED) {
1108 pr_info("Panel Power ON not supported\n");
1109 return -ENODEV;
1110 } else if (result == TOS_INPUT_DATA_ERROR) {
1111 return -EIO;
1112 }
1113
1114 return 0;
1115}
1116
Azael Avalos17fe4b32015-02-10 21:09:20 -07001117/* USB Three */
1118static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1119{
1120 u32 result;
1121
1122 if (!sci_open(dev))
1123 return -EIO;
1124
1125 result = sci_read(dev, SCI_USB_THREE, state);
1126 sci_close(dev);
1127 if (result == TOS_FAILURE) {
1128 pr_err("ACPI call to get USB 3 failed\n");
1129 return -EIO;
1130 } else if (result == TOS_NOT_SUPPORTED) {
1131 pr_info("USB 3 not supported\n");
1132 return -ENODEV;
1133 } else if (result == TOS_INPUT_DATA_ERROR) {
1134 return -EIO;
1135 }
1136
1137 return 0;
1138}
1139
1140static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1141{
1142 u32 result;
1143
1144 if (!sci_open(dev))
1145 return -EIO;
1146
1147 result = sci_write(dev, SCI_USB_THREE, state);
1148 sci_close(dev);
1149 if (result == TOS_FAILURE) {
1150 pr_err("ACPI call to set USB 3 failed\n");
1151 return -EIO;
1152 } else if (result == TOS_NOT_SUPPORTED) {
1153 pr_info("USB 3 not supported\n");
1154 return -ENODEV;
1155 } else if (result == TOS_INPUT_DATA_ERROR) {
1156 return -EIO;
1157 }
1158
1159 return 0;
1160}
1161
Azael Avalos56e6b352015-03-20 16:55:16 -06001162/* Hotkey Event type */
1163static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1164 u32 *type)
1165{
Azael Avalos3b876002015-05-06 09:35:09 -06001166 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1167 u32 out[TCI_WORDS];
1168 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001169
Azael Avalos3b876002015-05-06 09:35:09 -06001170 status = tci_raw(dev, in, out);
1171 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001172 pr_err("ACPI call to get System type failed\n");
1173 return -EIO;
Azael Avalos3b876002015-05-06 09:35:09 -06001174 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001175 pr_info("System type not supported\n");
1176 return -ENODEV;
1177 }
1178
Azael Avalos3b876002015-05-06 09:35:09 -06001179 *type = out[3];
Azael Avalos56e6b352015-03-20 16:55:16 -06001180
1181 return 0;
1182}
1183
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001184/* Transflective Backlight */
Azael Avalos695f6062015-07-22 18:09:13 -06001185static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001186{
Azael Avalos695f6062015-07-22 18:09:13 -06001187 u32 hci_result = hci_read(dev, HCI_TR_BACKLIGHT, status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001188
Azael Avalos1864bbc2014-09-29 20:40:08 -06001189 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001190}
1191
Azael Avalos695f6062015-07-22 18:09:13 -06001192static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001193{
Azael Avalos695f6062015-07-22 18:09:13 -06001194 u32 hci_result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001195
Azael Avalos1864bbc2014-09-29 20:40:08 -06001196 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001197}
1198
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001199static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001201/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001202static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 u32 hci_result;
1205 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001206 int brightness = 0;
1207
1208 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001209 int ret = get_tr_backlight_status(dev, &value);
Azael Avalosb5163992015-02-10 23:43:56 -07001210
Akio Idehara121b7b02012-04-06 01:46:43 +09001211 if (ret)
1212 return ret;
Azael Avalos695f6062015-07-22 18:09:13 -06001213 if (value)
Akio Idehara121b7b02012-04-06 01:46:43 +09001214 return 0;
1215 brightness++;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Azael Avalosd37782b2015-05-06 09:35:10 -06001218 hci_result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001219 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001220 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001221
1222 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001223}
1224
Seth Forshee62cce752012-04-19 11:23:50 -05001225static int get_lcd_brightness(struct backlight_device *bd)
1226{
1227 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001228
Seth Forshee62cce752012-04-19 11:23:50 -05001229 return __get_lcd_brightness(dev);
1230}
1231
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001232static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001233{
Seth Forshee135740d2011-09-20 16:55:49 -05001234 struct toshiba_acpi_dev *dev = m->private;
1235 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001236 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001237
Seth Forshee135740d2011-09-20 16:55:49 -05001238 if (!dev->backlight_dev)
1239 return -ENODEV;
1240
Akio Idehara121b7b02012-04-06 01:46:43 +09001241 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001242 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001243 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001244 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001245 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001249 pr_err("Error reading LCD brightness\n");
1250 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001251}
1252
1253static int lcd_proc_open(struct inode *inode, struct file *file)
1254{
Al Virod9dda782013-03-31 18:16:14 -04001255 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Seth Forshee62cce752012-04-19 11:23:50 -05001258static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001259{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001260 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001261
Akio Idehara121b7b02012-04-06 01:46:43 +09001262 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001263 int ret = set_tr_backlight_status(dev, !value);
Azael Avalosb5163992015-02-10 23:43:56 -07001264
Akio Idehara121b7b02012-04-06 01:46:43 +09001265 if (ret)
1266 return ret;
1267 if (value)
1268 value--;
1269 }
1270
Azael Avalosa39f46d2014-11-24 19:29:36 -07001271 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalosd37782b2015-05-06 09:35:10 -06001272 hci_result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
Azael Avalosa39f46d2014-11-24 19:29:36 -07001273 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001274}
1275
1276static int set_lcd_status(struct backlight_device *bd)
1277{
Seth Forshee135740d2011-09-20 16:55:49 -05001278 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001279
Seth Forshee62cce752012-04-19 11:23:50 -05001280 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001281}
1282
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001283static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1284 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Al Virod9dda782013-03-31 18:16:14 -04001286 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001287 char cmd[42];
1288 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001290 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001291 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001293 len = min(count, sizeof(cmd) - 1);
1294 if (copy_from_user(cmd, buf, len))
1295 return -EFAULT;
1296 cmd[len] = '\0';
1297
1298 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001299 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001300 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001301 if (ret == 0)
1302 ret = count;
1303 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001304 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001305 }
Holger Machtc9263552006-10-20 14:30:29 -07001306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001309static const struct file_operations lcd_proc_fops = {
1310 .owner = THIS_MODULE,
1311 .open = lcd_proc_open,
1312 .read = seq_read,
1313 .llseek = seq_lseek,
1314 .release = single_release,
1315 .write = lcd_proc_write,
1316};
1317
Seth Forshee36d03f92011-09-20 16:55:53 -05001318static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1319{
1320 u32 hci_result;
1321
Azael Avalosd37782b2015-05-06 09:35:10 -06001322 hci_result = hci_read(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001323 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001324}
1325
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001326static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Seth Forshee135740d2011-09-20 16:55:49 -05001328 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001330 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Seth Forshee36d03f92011-09-20 16:55:53 -05001332 ret = get_video_status(dev, &value);
1333 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1335 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001336 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001337
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001338 seq_printf(m, "lcd_out: %d\n", is_lcd);
1339 seq_printf(m, "crt_out: %d\n", is_crt);
1340 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Seth Forshee36d03f92011-09-20 16:55:53 -05001343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001346static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Al Virod9dda782013-03-31 18:16:14 -04001348 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001349}
1350
1351static ssize_t video_proc_write(struct file *file, const char __user *buf,
1352 size_t count, loff_t *pos)
1353{
Al Virod9dda782013-03-31 18:16:14 -04001354 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001355 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001356 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int value;
1358 int remain = count;
1359 int lcd_out = -1;
1360 int crt_out = -1;
1361 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001362 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001364 cmd = kmalloc(count + 1, GFP_KERNEL);
1365 if (!cmd)
1366 return -ENOMEM;
1367 if (copy_from_user(cmd, buf, count)) {
1368 kfree(cmd);
1369 return -EFAULT;
1370 }
1371 cmd[count] = '\0';
1372
1373 buffer = cmd;
1374
Darren Harte0769fe2015-02-11 20:50:08 -08001375 /*
1376 * Scan expression. Multiple expressions may be delimited with ;
1377 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
1379 while (remain) {
1380 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1381 lcd_out = value & 1;
1382 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1383 crt_out = value & 1;
1384 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1385 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001386 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 do {
1388 ++buffer;
1389 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001390 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001393 kfree(cmd);
1394
Seth Forshee36d03f92011-09-20 16:55:53 -05001395 ret = get_video_status(dev, &video_out);
1396 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001397 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (lcd_out != -1)
1400 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1401 if (crt_out != -1)
1402 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1403 if (tv_out != -1)
1404 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001405 /*
1406 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001407 * video setting if something changed.
1408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001410 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001413 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001416static const struct file_operations video_proc_fops = {
1417 .owner = THIS_MODULE,
1418 .open = video_proc_open,
1419 .read = seq_read,
1420 .llseek = seq_lseek,
1421 .release = single_release,
1422 .write = video_proc_write,
1423};
1424
Seth Forshee36d03f92011-09-20 16:55:53 -05001425static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1426{
1427 u32 hci_result;
1428
Azael Avalosd37782b2015-05-06 09:35:10 -06001429 hci_result = hci_read(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001430 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001431}
1432
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001433static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Seth Forshee135740d2011-09-20 16:55:49 -05001435 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001436 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 u32 value;
1438
Seth Forshee36d03f92011-09-20 16:55:53 -05001439 ret = get_fan_status(dev, &value);
1440 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001441 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001442 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Seth Forshee36d03f92011-09-20 16:55:53 -05001445 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001448static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Al Virod9dda782013-03-31 18:16:14 -04001450 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001451}
1452
1453static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1454 size_t count, loff_t *pos)
1455{
Al Virod9dda782013-03-31 18:16:14 -04001456 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001457 char cmd[42];
1458 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int value;
1460 u32 hci_result;
1461
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001462 len = min(count, sizeof(cmd) - 1);
1463 if (copy_from_user(cmd, buf, len))
1464 return -EFAULT;
1465 cmd[len] = '\0';
1466
1467 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001468 value >= 0 && value <= 1) {
Azael Avalosd37782b2015-05-06 09:35:10 -06001469 hci_result = hci_write(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001470 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001471 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001472 else
1473 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } else {
1475 return -EINVAL;
1476 }
1477
1478 return count;
1479}
1480
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001481static const struct file_operations fan_proc_fops = {
1482 .owner = THIS_MODULE,
1483 .open = fan_proc_open,
1484 .read = seq_read,
1485 .llseek = seq_lseek,
1486 .release = single_release,
1487 .write = fan_proc_write,
1488};
1489
1490static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Seth Forshee135740d2011-09-20 16:55:49 -05001492 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Seth Forshee135740d2011-09-20 16:55:49 -05001494 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1495 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Azael Avalos7deef552015-07-22 18:09:10 -06001496
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001500static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Al Virod9dda782013-03-31 18:16:14 -04001502 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001503}
1504
1505static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1506 size_t count, loff_t *pos)
1507{
Al Virod9dda782013-03-31 18:16:14 -04001508 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001509 char cmd[42];
1510 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int value;
1512
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001513 len = min(count, sizeof(cmd) - 1);
1514 if (copy_from_user(cmd, buf, len))
1515 return -EFAULT;
1516 cmd[len] = '\0';
1517
Azael Avalosb5163992015-02-10 23:43:56 -07001518 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001519 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001520 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 return count;
1524}
1525
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001526static const struct file_operations keys_proc_fops = {
1527 .owner = THIS_MODULE,
1528 .open = keys_proc_open,
1529 .read = seq_read,
1530 .llseek = seq_lseek,
1531 .release = single_release,
1532 .write = keys_proc_write,
1533};
1534
1535static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001537 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1538 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001542static int version_proc_open(struct inode *inode, struct file *file)
1543{
Al Virod9dda782013-03-31 18:16:14 -04001544 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001545}
1546
1547static const struct file_operations version_proc_fops = {
1548 .owner = THIS_MODULE,
1549 .open = version_proc_open,
1550 .read = seq_read,
1551 .llseek = seq_lseek,
1552 .release = single_release,
1553};
1554
Darren Harte0769fe2015-02-11 20:50:08 -08001555/*
1556 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
1558
1559#define PROC_TOSHIBA "toshiba"
1560
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001561static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Seth Forshee36d03f92011-09-20 16:55:53 -05001563 if (dev->backlight_dev)
1564 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1565 &lcd_proc_fops, dev);
1566 if (dev->video_supported)
1567 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1568 &video_proc_fops, dev);
1569 if (dev->fan_supported)
1570 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1571 &fan_proc_fops, dev);
1572 if (dev->hotkey_dev)
1573 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1574 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001575 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1576 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Seth Forshee36d03f92011-09-20 16:55:53 -05001579static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Seth Forshee36d03f92011-09-20 16:55:53 -05001581 if (dev->backlight_dev)
1582 remove_proc_entry("lcd", toshiba_proc_dir);
1583 if (dev->video_supported)
1584 remove_proc_entry("video", toshiba_proc_dir);
1585 if (dev->fan_supported)
1586 remove_proc_entry("fan", toshiba_proc_dir);
1587 if (dev->hotkey_dev)
1588 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001589 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Lionel Debrouxacc24722010-11-16 14:14:02 +01001592static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001593 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001594 .get_brightness = get_lcd_brightness,
1595 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001596};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001597
Azael Avalos360f0f32014-03-25 20:38:31 -06001598/*
1599 * Sysfs files
1600 */
Azael Avalos9d309842015-02-10 23:43:59 -07001601static ssize_t version_show(struct device *dev,
1602 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001603{
1604 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1605}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001606static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001607
Azael Avalos9d309842015-02-10 23:43:59 -07001608static ssize_t fan_store(struct device *dev,
1609 struct device_attribute *attr,
1610 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001611{
1612 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1613 u32 result;
1614 int state;
1615 int ret;
1616
1617 ret = kstrtoint(buf, 0, &state);
1618 if (ret)
1619 return ret;
1620
1621 if (state != 0 && state != 1)
1622 return -EINVAL;
1623
Azael Avalosd37782b2015-05-06 09:35:10 -06001624 result = hci_write(toshiba, HCI_FAN, state);
Azael Avalos94477d42015-02-10 21:09:17 -07001625 if (result == TOS_FAILURE)
1626 return -EIO;
1627 else if (result == TOS_NOT_SUPPORTED)
1628 return -ENODEV;
1629
1630 return count;
1631}
1632
Azael Avalos9d309842015-02-10 23:43:59 -07001633static ssize_t fan_show(struct device *dev,
1634 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001635{
1636 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1637 u32 value;
1638 int ret;
1639
1640 ret = get_fan_status(toshiba, &value);
1641 if (ret)
1642 return ret;
1643
1644 return sprintf(buf, "%d\n", value);
1645}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001646static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001647
Azael Avalos9d309842015-02-10 23:43:59 -07001648static ssize_t kbd_backlight_mode_store(struct device *dev,
1649 struct device_attribute *attr,
1650 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001651{
1652 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001653 int mode;
1654 int time;
1655 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001656
Dan Carpenteraeaac092014-09-03 14:44:37 +03001657
1658 ret = kstrtoint(buf, 0, &mode);
1659 if (ret)
1660 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001661
1662 /* Check for supported modes depending on keyboard backlight type */
1663 if (toshiba->kbd_type == 1) {
1664 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1665 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1666 return -EINVAL;
1667 } else if (toshiba->kbd_type == 2) {
1668 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1669 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1670 mode != SCI_KBD_MODE_OFF)
1671 return -EINVAL;
1672 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001673
Darren Harte0769fe2015-02-11 20:50:08 -08001674 /*
1675 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001676 * Auto - KBD backlight turns off automatically in given time
1677 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001678 * ON - KBD backlight is always on
1679 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001680 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001681
1682 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001683 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001684 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001685 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001686
1687 /* OR the "base time" to the actual method format */
1688 if (toshiba->kbd_type == 1) {
1689 /* Type 1 requires the current mode */
1690 time |= toshiba->kbd_mode;
1691 } else if (toshiba->kbd_type == 2) {
1692 /* Type 2 requires the desired mode */
1693 time |= mode;
1694 }
1695
Dan Carpenteraeaac092014-09-03 14:44:37 +03001696 ret = toshiba_kbd_illum_status_set(toshiba, time);
1697 if (ret)
1698 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001699
Azael Avalos360f0f32014-03-25 20:38:31 -06001700 toshiba->kbd_mode = mode;
1701 }
1702
1703 return count;
1704}
1705
Azael Avalos9d309842015-02-10 23:43:59 -07001706static ssize_t kbd_backlight_mode_show(struct device *dev,
1707 struct device_attribute *attr,
1708 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001709{
1710 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1711 u32 time;
1712
1713 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1714 return -EIO;
1715
Azael Avalos93f8c162014-09-12 18:50:36 -06001716 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1717}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001718static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001719
Azael Avalos9d309842015-02-10 23:43:59 -07001720static ssize_t kbd_type_show(struct device *dev,
1721 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001722{
1723 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1724
1725 return sprintf(buf, "%d\n", toshiba->kbd_type);
1726}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001727static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001728
Azael Avalos9d309842015-02-10 23:43:59 -07001729static ssize_t available_kbd_modes_show(struct device *dev,
1730 struct device_attribute *attr,
1731 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001732{
1733 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1734
1735 if (toshiba->kbd_type == 1)
1736 return sprintf(buf, "%x %x\n",
1737 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1738
1739 return sprintf(buf, "%x %x %x\n",
1740 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001741}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001742static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001743
Azael Avalos9d309842015-02-10 23:43:59 -07001744static ssize_t kbd_backlight_timeout_store(struct device *dev,
1745 struct device_attribute *attr,
1746 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001747{
1748 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001749 int time;
1750 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001751
Azael Avaloseabde0f2014-10-04 12:02:21 -06001752 ret = kstrtoint(buf, 0, &time);
1753 if (ret)
1754 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001755
Azael Avaloseabde0f2014-10-04 12:02:21 -06001756 /* Check for supported values depending on kbd_type */
1757 if (toshiba->kbd_type == 1) {
1758 if (time < 0 || time > 60)
1759 return -EINVAL;
1760 } else if (toshiba->kbd_type == 2) {
1761 if (time < 1 || time > 60)
1762 return -EINVAL;
1763 }
1764
1765 /* Set the Keyboard Backlight Timeout */
1766
1767 /* Only make a change if the actual timeout has changed */
1768 if (toshiba->kbd_time != time) {
1769 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001770 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001771 /* OR the "base time" to the actual method format */
1772 if (toshiba->kbd_type == 1)
1773 time |= SCI_KBD_MODE_FNZ;
1774 else if (toshiba->kbd_type == 2)
1775 time |= SCI_KBD_MODE_AUTO;
1776
1777 ret = toshiba_kbd_illum_status_set(toshiba, time);
1778 if (ret)
1779 return ret;
1780
Azael Avalos360f0f32014-03-25 20:38:31 -06001781 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1782 }
1783
1784 return count;
1785}
1786
Azael Avalos9d309842015-02-10 23:43:59 -07001787static ssize_t kbd_backlight_timeout_show(struct device *dev,
1788 struct device_attribute *attr,
1789 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001790{
1791 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1792 u32 time;
1793
1794 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1795 return -EIO;
1796
1797 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1798}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001799static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001800
Azael Avalos9d309842015-02-10 23:43:59 -07001801static ssize_t touchpad_store(struct device *dev,
1802 struct device_attribute *attr,
1803 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001804{
1805 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1806 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001807 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001808
1809 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001810 ret = kstrtoint(buf, 0, &state);
1811 if (ret)
1812 return ret;
1813 if (state != 0 && state != 1)
1814 return -EINVAL;
1815
1816 ret = toshiba_touchpad_set(toshiba, state);
1817 if (ret)
1818 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001819
1820 return count;
1821}
1822
Azael Avalos9d309842015-02-10 23:43:59 -07001823static ssize_t touchpad_show(struct device *dev,
1824 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001825{
1826 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1827 u32 state;
1828 int ret;
1829
1830 ret = toshiba_touchpad_get(toshiba, &state);
1831 if (ret < 0)
1832 return ret;
1833
1834 return sprintf(buf, "%i\n", state);
1835}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001836static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001837
Azael Avalos9d309842015-02-10 23:43:59 -07001838static ssize_t position_show(struct device *dev,
1839 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001840{
1841 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1842 u32 xyval, zval, tmp;
1843 u16 x, y, z;
1844 int ret;
1845
1846 xyval = zval = 0;
1847 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1848 if (ret < 0)
1849 return ret;
1850
1851 x = xyval & HCI_ACCEL_MASK;
1852 tmp = xyval >> HCI_MISC_SHIFT;
1853 y = tmp & HCI_ACCEL_MASK;
1854 z = zval & HCI_ACCEL_MASK;
1855
1856 return sprintf(buf, "%d %d %d\n", x, y, z);
1857}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001858static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001859
Azael Avalos9d309842015-02-10 23:43:59 -07001860static ssize_t usb_sleep_charge_show(struct device *dev,
1861 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001862{
1863 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1864 u32 mode;
1865 int ret;
1866
1867 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1868 if (ret < 0)
1869 return ret;
1870
1871 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1872}
1873
Azael Avalos9d309842015-02-10 23:43:59 -07001874static ssize_t usb_sleep_charge_store(struct device *dev,
1875 struct device_attribute *attr,
1876 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001877{
1878 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1879 u32 mode;
1880 int state;
1881 int ret;
1882
1883 ret = kstrtoint(buf, 0, &state);
1884 if (ret)
1885 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001886 /*
1887 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001888 * 0 - Disabled
1889 * 1 - Alternate (Non USB conformant devices that require more power)
1890 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001891 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001892 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001893 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001894 return -EINVAL;
1895
1896 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001897 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001898 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001899 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001900 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001901 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001902 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001903 mode |= SCI_USB_CHARGE_AUTO;
1904 else if (state == 3)
1905 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001906
1907 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1908 if (ret)
1909 return ret;
1910
1911 return count;
1912}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001913static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001914
Azael Avalos182bcaa2015-01-18 18:30:23 -07001915static ssize_t sleep_functions_on_battery_show(struct device *dev,
1916 struct device_attribute *attr,
1917 char *buf)
1918{
1919 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1920 u32 state;
1921 int bat_lvl;
1922 int status;
1923 int ret;
1924 int tmp;
1925
1926 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1927 if (ret < 0)
1928 return ret;
1929
1930 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1931 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1932 status = (tmp == 0x4) ? 1 : 0;
1933 /* Determine the battery level set */
1934 bat_lvl = state >> HCI_MISC_SHIFT;
1935
1936 return sprintf(buf, "%d %d\n", status, bat_lvl);
1937}
1938
1939static ssize_t sleep_functions_on_battery_store(struct device *dev,
1940 struct device_attribute *attr,
1941 const char *buf, size_t count)
1942{
1943 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1944 u32 status;
1945 int value;
1946 int ret;
1947 int tmp;
1948
1949 ret = kstrtoint(buf, 0, &value);
1950 if (ret)
1951 return ret;
1952
Darren Harte0769fe2015-02-11 20:50:08 -08001953 /*
1954 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07001955 * 0 - Disabled
1956 * 1-100 - Enabled
1957 */
1958 if (value < 0 || value > 100)
1959 return -EINVAL;
1960
1961 if (value == 0) {
1962 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1963 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1964 } else {
1965 tmp = value << HCI_MISC_SHIFT;
1966 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1967 }
1968 ret = toshiba_sleep_functions_status_set(toshiba, status);
1969 if (ret < 0)
1970 return ret;
1971
1972 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
1973
1974 return count;
1975}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001976static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07001977
Azael Avalos9d309842015-02-10 23:43:59 -07001978static ssize_t usb_rapid_charge_show(struct device *dev,
1979 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07001980{
1981 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1982 u32 state;
1983 int ret;
1984
1985 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
1986 if (ret < 0)
1987 return ret;
1988
1989 return sprintf(buf, "%d\n", state);
1990}
1991
Azael Avalos9d309842015-02-10 23:43:59 -07001992static ssize_t usb_rapid_charge_store(struct device *dev,
1993 struct device_attribute *attr,
1994 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07001995{
1996 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1997 int state;
1998 int ret;
1999
2000 ret = kstrtoint(buf, 0, &state);
2001 if (ret)
2002 return ret;
2003 if (state != 0 && state != 1)
2004 return -EINVAL;
2005
2006 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2007 if (ret)
2008 return ret;
2009
2010 return count;
2011}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002012static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002013
Azael Avalos9d309842015-02-10 23:43:59 -07002014static ssize_t usb_sleep_music_show(struct device *dev,
2015 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002016{
2017 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2018 u32 state;
2019 int ret;
2020
2021 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2022 if (ret < 0)
2023 return ret;
2024
2025 return sprintf(buf, "%d\n", state);
2026}
2027
Azael Avalos9d309842015-02-10 23:43:59 -07002028static ssize_t usb_sleep_music_store(struct device *dev,
2029 struct device_attribute *attr,
2030 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002031{
2032 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2033 int state;
2034 int ret;
2035
2036 ret = kstrtoint(buf, 0, &state);
2037 if (ret)
2038 return ret;
2039 if (state != 0 && state != 1)
2040 return -EINVAL;
2041
2042 ret = toshiba_usb_sleep_music_set(toshiba, state);
2043 if (ret)
2044 return ret;
2045
2046 return count;
2047}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002048static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002049
Azael Avalos9d309842015-02-10 23:43:59 -07002050static ssize_t kbd_function_keys_show(struct device *dev,
2051 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002052{
2053 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2054 int mode;
2055 int ret;
2056
2057 ret = toshiba_function_keys_get(toshiba, &mode);
2058 if (ret < 0)
2059 return ret;
2060
2061 return sprintf(buf, "%d\n", mode);
2062}
2063
Azael Avalos9d309842015-02-10 23:43:59 -07002064static ssize_t kbd_function_keys_store(struct device *dev,
2065 struct device_attribute *attr,
2066 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002067{
2068 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2069 int mode;
2070 int ret;
2071
2072 ret = kstrtoint(buf, 0, &mode);
2073 if (ret)
2074 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002075 /*
2076 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002077 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2078 * 1 - Special functions (Opposite of the above setting)
2079 */
2080 if (mode != 0 && mode != 1)
2081 return -EINVAL;
2082
2083 ret = toshiba_function_keys_set(toshiba, mode);
2084 if (ret)
2085 return ret;
2086
2087 pr_info("Reboot for changes to KBD Function Keys to take effect");
2088
2089 return count;
2090}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002091static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002092
Azael Avalos9d309842015-02-10 23:43:59 -07002093static ssize_t panel_power_on_show(struct device *dev,
2094 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002095{
2096 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2097 u32 state;
2098 int ret;
2099
2100 ret = toshiba_panel_power_on_get(toshiba, &state);
2101 if (ret < 0)
2102 return ret;
2103
2104 return sprintf(buf, "%d\n", state);
2105}
2106
Azael Avalos9d309842015-02-10 23:43:59 -07002107static ssize_t panel_power_on_store(struct device *dev,
2108 struct device_attribute *attr,
2109 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002110{
2111 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2112 int state;
2113 int ret;
2114
2115 ret = kstrtoint(buf, 0, &state);
2116 if (ret)
2117 return ret;
2118 if (state != 0 && state != 1)
2119 return -EINVAL;
2120
2121 ret = toshiba_panel_power_on_set(toshiba, state);
2122 if (ret)
2123 return ret;
2124
2125 pr_info("Reboot for changes to Panel Power ON to take effect");
2126
2127 return count;
2128}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002129static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002130
Azael Avalos9d309842015-02-10 23:43:59 -07002131static ssize_t usb_three_show(struct device *dev,
2132 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002133{
2134 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135 u32 state;
2136 int ret;
2137
2138 ret = toshiba_usb_three_get(toshiba, &state);
2139 if (ret < 0)
2140 return ret;
2141
2142 return sprintf(buf, "%d\n", state);
2143}
2144
Azael Avalos9d309842015-02-10 23:43:59 -07002145static ssize_t usb_three_store(struct device *dev,
2146 struct device_attribute *attr,
2147 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002148{
2149 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2150 int state;
2151 int ret;
2152
2153 ret = kstrtoint(buf, 0, &state);
2154 if (ret)
2155 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002156 /*
2157 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002158 * 0 - Disabled (Acts like a USB 2 port, saving power)
2159 * 1 - Enabled
2160 */
2161 if (state != 0 && state != 1)
2162 return -EINVAL;
2163
2164 ret = toshiba_usb_three_set(toshiba, state);
2165 if (ret)
2166 return ret;
2167
2168 pr_info("Reboot for changes to USB 3 to take effect");
2169
2170 return count;
2171}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002172static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002173
2174static struct attribute *toshiba_attributes[] = {
2175 &dev_attr_version.attr,
2176 &dev_attr_fan.attr,
2177 &dev_attr_kbd_backlight_mode.attr,
2178 &dev_attr_kbd_type.attr,
2179 &dev_attr_available_kbd_modes.attr,
2180 &dev_attr_kbd_backlight_timeout.attr,
2181 &dev_attr_touchpad.attr,
2182 &dev_attr_position.attr,
2183 &dev_attr_usb_sleep_charge.attr,
2184 &dev_attr_sleep_functions_on_battery.attr,
2185 &dev_attr_usb_rapid_charge.attr,
2186 &dev_attr_usb_sleep_music.attr,
2187 &dev_attr_kbd_function_keys.attr,
2188 &dev_attr_panel_power_on.attr,
2189 &dev_attr_usb_three.attr,
2190 NULL,
2191};
2192
Azael Avalos360f0f32014-03-25 20:38:31 -06002193static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2194 struct attribute *attr, int idx)
2195{
2196 struct device *dev = container_of(kobj, struct device, kobj);
2197 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2198 bool exists = true;
2199
Azael Avalos94477d42015-02-10 21:09:17 -07002200 if (attr == &dev_attr_fan.attr)
2201 exists = (drv->fan_supported) ? true : false;
2202 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002203 exists = (drv->kbd_illum_supported) ? true : false;
2204 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2205 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002206 else if (attr == &dev_attr_touchpad.attr)
2207 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002208 else if (attr == &dev_attr_position.attr)
2209 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002210 else if (attr == &dev_attr_usb_sleep_charge.attr)
2211 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002212 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2213 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002214 else if (attr == &dev_attr_usb_rapid_charge.attr)
2215 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002216 else if (attr == &dev_attr_usb_sleep_music.attr)
2217 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002218 else if (attr == &dev_attr_kbd_function_keys.attr)
2219 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002220 else if (attr == &dev_attr_panel_power_on.attr)
2221 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002222 else if (attr == &dev_attr_usb_three.attr)
2223 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002224
2225 return exists ? attr->mode : 0;
2226}
2227
Azael Avalos9bd12132015-02-10 23:43:58 -07002228static struct attribute_group toshiba_attr_group = {
2229 .is_visible = toshiba_sysfs_is_visible,
2230 .attrs = toshiba_attributes,
2231};
2232
Azael Avalos1f28f292014-12-04 20:22:45 -07002233/*
Azael Avalosfc5462f2015-07-22 18:09:11 -06002234 * Misc device
2235 */
2236static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2237{
2238 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2239 regs->edx, regs->esi, regs->edi };
2240 u32 out[TCI_WORDS];
2241 acpi_status status;
2242
2243 status = tci_raw(toshiba_acpi, in, out);
2244 if (ACPI_FAILURE(status)) {
2245 pr_err("ACPI call to query SMM registers failed\n");
2246 return -EIO;
2247 }
2248
2249 /* Fillout the SMM struct with the TCI call results */
2250 regs->eax = out[0];
2251 regs->ebx = out[1];
2252 regs->ecx = out[2];
2253 regs->edx = out[3];
2254 regs->esi = out[4];
2255 regs->edi = out[5];
2256
2257 return 0;
2258}
2259
2260static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2261 unsigned long arg)
2262{
2263 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2264 SMMRegisters regs;
2265 int ret;
2266
2267 if (!argp)
2268 return -EINVAL;
2269
2270 switch (cmd) {
2271 case TOSH_SMM:
2272 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2273 return -EFAULT;
2274 ret = toshiba_acpi_smm_bridge(&regs);
2275 if (ret)
2276 return ret;
2277 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2278 return -EFAULT;
2279 break;
2280 case TOSHIBA_ACPI_SCI:
2281 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2282 return -EFAULT;
2283 /* Ensure we are being called with a SCI_{GET, SET} register */
2284 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2285 return -EINVAL;
2286 if (!sci_open(toshiba_acpi))
2287 return -EIO;
2288 ret = toshiba_acpi_smm_bridge(&regs);
2289 sci_close(toshiba_acpi);
2290 if (ret)
2291 return ret;
2292 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2293 return -EFAULT;
2294 break;
2295 default:
2296 return -EINVAL;
2297 }
2298
2299 return 0;
2300}
2301
2302static const struct file_operations toshiba_acpi_fops = {
2303 .owner = THIS_MODULE,
2304 .unlocked_ioctl = toshiba_acpi_ioctl,
2305 .llseek = noop_llseek,
2306};
2307
2308/*
Azael Avalos1f28f292014-12-04 20:22:45 -07002309 * Hotkeys
2310 */
2311static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2312{
2313 acpi_status status;
2314 u32 result;
2315
2316 status = acpi_evaluate_object(dev->acpi_dev->handle,
2317 "ENAB", NULL, NULL);
2318 if (ACPI_FAILURE(status))
2319 return -ENODEV;
2320
Azael Avalosd37782b2015-05-06 09:35:10 -06002321 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
Azael Avalos1f28f292014-12-04 20:22:45 -07002322 if (result == TOS_FAILURE)
2323 return -EIO;
2324 else if (result == TOS_NOT_SUPPORTED)
2325 return -ENODEV;
2326
2327 return 0;
2328}
2329
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002330static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2331{
2332 u32 result;
2333
2334 /*
2335 * Re-activate the hotkeys, but this time, we are using the
2336 * "Special Functions" mode.
2337 */
Azael Avalosd37782b2015-05-06 09:35:10 -06002338 result = hci_write(dev, HCI_HOTKEY_EVENT,
2339 HCI_HOTKEY_SPECIAL_FUNCTIONS);
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002340 if (result != TOS_SUCCESS)
2341 pr_err("Could not enable the Special Function mode\n");
2342}
2343
Seth Forshee29cd2932012-01-18 13:44:09 -06002344static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2345 struct serio *port)
2346{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002347 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002348 return false;
2349
2350 if (unlikely(data == 0xe0))
2351 return false;
2352
2353 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2354 schedule_work(&toshiba_acpi->hotkey_work);
2355 return true;
2356 }
2357
2358 return false;
2359}
2360
2361static void toshiba_acpi_hotkey_work(struct work_struct *work)
2362{
2363 acpi_handle ec_handle = ec_get_handle();
2364 acpi_status status;
2365
2366 if (!ec_handle)
2367 return;
2368
2369 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2370 if (ACPI_FAILURE(status))
2371 pr_err("ACPI NTFY method execution failed\n");
2372}
2373
2374/*
2375 * Returns hotkey scancode, or < 0 on failure.
2376 */
2377static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2378{
Zhang Rui74facaf2013-09-03 08:32:15 +08002379 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002380 acpi_status status;
2381
Zhang Rui74facaf2013-09-03 08:32:15 +08002382 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2383 NULL, &value);
2384 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002385 pr_err("ACPI INFO method execution failed\n");
2386 return -EIO;
2387 }
2388
Zhang Rui74facaf2013-09-03 08:32:15 +08002389 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002390}
2391
2392static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2393 int scancode)
2394{
2395 if (scancode == 0x100)
2396 return;
2397
Darren Harte0769fe2015-02-11 20:50:08 -08002398 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002399 if (scancode & 0x80)
2400 return;
2401
2402 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2403 pr_info("Unknown key %x\n", scancode);
2404}
2405
Azael Avalos71454d72014-12-04 20:22:46 -07002406static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2407{
Azael Avalos71454d72014-12-04 20:22:46 -07002408 if (dev->info_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002409 int scancode = toshiba_acpi_query_hotkey(dev);
2410
2411 if (scancode < 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002412 pr_err("Failed to query hotkey event\n");
Azael Avalos7deef552015-07-22 18:09:10 -06002413 } else if (scancode != 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002414 toshiba_acpi_report_hotkey(dev, scancode);
Azael Avalos7deef552015-07-22 18:09:10 -06002415 dev->key_event_valid = 1;
2416 dev->last_key_event = scancode;
2417 }
Azael Avalos71454d72014-12-04 20:22:46 -07002418 } else if (dev->system_event_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002419 u32 result;
2420 u32 value;
2421 int retries = 3;
2422
Azael Avalos71454d72014-12-04 20:22:46 -07002423 do {
Azael Avalos7deef552015-07-22 18:09:10 -06002424 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2425 switch (result) {
Azael Avalos71454d72014-12-04 20:22:46 -07002426 case TOS_SUCCESS:
2427 toshiba_acpi_report_hotkey(dev, (int)value);
Azael Avalos7deef552015-07-22 18:09:10 -06002428 dev->key_event_valid = 1;
2429 dev->last_key_event = value;
Azael Avalos71454d72014-12-04 20:22:46 -07002430 break;
2431 case TOS_NOT_SUPPORTED:
2432 /*
2433 * This is a workaround for an unresolved
2434 * issue on some machines where system events
2435 * sporadically become disabled.
2436 */
Azael Avalos7deef552015-07-22 18:09:10 -06002437 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2438 if (result == TOS_SUCCESS)
2439 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002440 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002441 default:
2442 retries--;
2443 break;
2444 }
Azael Avalos7deef552015-07-22 18:09:10 -06002445 } while (retries && result != TOS_FIFO_EMPTY);
Azael Avalos71454d72014-12-04 20:22:46 -07002446 }
2447}
2448
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002449static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002450{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002451 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002452 acpi_handle ec_handle;
2453 u32 events_type;
2454 u32 hci_result;
2455 int error;
2456
Azael Avalosa88bc062015-07-22 18:09:12 -06002457 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2458 pr_info("WMI event detected, hotkeys will not be monitored\n");
2459 return 0;
2460 }
2461
Azael Avalosa2b34712015-03-20 16:55:17 -06002462 error = toshiba_acpi_enable_hotkeys(dev);
2463 if (error)
2464 return error;
2465
2466 error = toshiba_hotkey_event_type_get(dev, &events_type);
2467 if (error) {
2468 pr_err("Unable to query Hotkey Event Type\n");
2469 return error;
2470 }
2471 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002472
Seth Forshee135740d2011-09-20 16:55:49 -05002473 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002474 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002475 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002476
2477 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002478 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002479 dev->hotkey_dev->id.bustype = BUS_HOST;
2480
Azael Avalosa2b34712015-03-20 16:55:17 -06002481 if (events_type == HCI_SYSTEM_TYPE1 ||
2482 !dev->kbd_function_keys_supported)
2483 keymap = toshiba_acpi_keymap;
2484 else if (events_type == HCI_SYSTEM_TYPE2 ||
2485 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002486 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002487 else
2488 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002489 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002490 if (error)
2491 goto err_free_dev;
2492
Seth Forshee29cd2932012-01-18 13:44:09 -06002493 /*
2494 * For some machines the SCI responsible for providing hotkey
2495 * notification doesn't fire. We can trigger the notification
2496 * whenever the Fn key is pressed using the NTFY method, if
2497 * supported, so if it's present set up an i8042 key filter
2498 * for this purpose.
2499 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002500 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002501 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002502 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2503
2504 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2505 if (error) {
2506 pr_err("Error installing key filter\n");
2507 goto err_free_keymap;
2508 }
2509
2510 dev->ntfy_supported = 1;
2511 }
2512
2513 /*
2514 * Determine hotkey query interface. Prefer using the INFO
2515 * method when it is available.
2516 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002517 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002518 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002519 else {
Azael Avalosd37782b2015-05-06 09:35:10 -06002520 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002521 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002522 dev->system_event_supported = 1;
2523 }
2524
2525 if (!dev->info_supported && !dev->system_event_supported) {
2526 pr_warn("No hotkey query interface found\n");
2527 goto err_remove_filter;
2528 }
2529
Seth Forshee135740d2011-09-20 16:55:49 -05002530 error = input_register_device(dev->hotkey_dev);
2531 if (error) {
2532 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002533 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002534 }
2535
2536 return 0;
2537
Seth Forshee29cd2932012-01-18 13:44:09 -06002538 err_remove_filter:
2539 if (dev->ntfy_supported)
2540 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002541 err_free_keymap:
2542 sparse_keymap_free(dev->hotkey_dev);
2543 err_free_dev:
2544 input_free_device(dev->hotkey_dev);
2545 dev->hotkey_dev = NULL;
2546 return error;
2547}
2548
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002549static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002550{
2551 struct backlight_properties props;
2552 int brightness;
2553 int ret;
2554
2555 /*
2556 * Some machines don't support the backlight methods at all, and
2557 * others support it read-only. Either of these is pretty useless,
2558 * so only register the backlight device if the backlight method
2559 * supports both reads and writes.
2560 */
2561 brightness = __get_lcd_brightness(dev);
2562 if (brightness < 0)
2563 return 0;
2564 ret = set_lcd_brightness(dev, brightness);
2565 if (ret) {
2566 pr_debug("Backlight method is read-only, disabling backlight support\n");
2567 return 0;
2568 }
2569
Hans de Goede358d6a22015-04-21 12:01:32 +02002570 /*
2571 * Tell acpi-video-detect code to prefer vendor backlight on all
2572 * systems with transflective backlight and on dmi matched systems.
2573 */
2574 if (dev->tr_backlight_supported ||
2575 dmi_check_system(toshiba_vendor_backlight_dmi))
Hans de Goede234b7cf2015-06-16 16:28:11 +02002576 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
Hans de Goede358d6a22015-04-21 12:01:32 +02002577
Hans de Goede234b7cf2015-06-16 16:28:11 +02002578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
Hans de Goede358d6a22015-04-21 12:01:32 +02002579 return 0;
2580
Matthew Garrett53039f22012-06-01 11:02:36 -04002581 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002582 props.type = BACKLIGHT_PLATFORM;
2583 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002584
Darren Harte0769fe2015-02-11 20:50:08 -08002585 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002586 if (dev->tr_backlight_supported)
2587 props.max_brightness++;
2588
Seth Forshee62cce752012-04-19 11:23:50 -05002589 dev->backlight_dev = backlight_device_register("toshiba",
2590 &dev->acpi_dev->dev,
2591 dev,
2592 &toshiba_backlight_data,
2593 &props);
2594 if (IS_ERR(dev->backlight_dev)) {
2595 ret = PTR_ERR(dev->backlight_dev);
2596 pr_err("Could not register toshiba backlight device\n");
2597 dev->backlight_dev = NULL;
2598 return ret;
2599 }
2600
2601 dev->backlight_dev->props.brightness = brightness;
2602 return 0;
2603}
2604
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002605static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002606{
2607 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2608
Azael Avalosfc5462f2015-07-22 18:09:11 -06002609 misc_deregister(&dev->miscdev);
2610
Seth Forshee36d03f92011-09-20 16:55:53 -05002611 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002612
Azael Avalos360f0f32014-03-25 20:38:31 -06002613 if (dev->sysfs_created)
2614 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2615 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002616
Seth Forshee29cd2932012-01-18 13:44:09 -06002617 if (dev->ntfy_supported) {
2618 i8042_remove_filter(toshiba_acpi_i8042_filter);
2619 cancel_work_sync(&dev->hotkey_work);
2620 }
2621
Seth Forshee135740d2011-09-20 16:55:49 -05002622 if (dev->hotkey_dev) {
2623 input_unregister_device(dev->hotkey_dev);
2624 sparse_keymap_free(dev->hotkey_dev);
2625 }
2626
Markus Elfring00981812014-11-24 20:30:29 +01002627 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002628
Seth Forshee36d03f92011-09-20 16:55:53 -05002629 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002630 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002631
Azael Avalos360f0f32014-03-25 20:38:31 -06002632 if (dev->kbd_led_registered)
2633 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002634
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002635 if (dev->eco_supported)
2636 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002637
Seth Forshee29cd2932012-01-18 13:44:09 -06002638 if (toshiba_acpi)
2639 toshiba_acpi = NULL;
2640
Seth Forshee135740d2011-09-20 16:55:49 -05002641 kfree(dev);
2642
2643 return 0;
2644}
2645
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002646static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002647{
Zhang Ruie2e19602013-09-03 08:32:06 +08002648 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002649 return "GHCI";
2650
Zhang Ruie2e19602013-09-03 08:32:06 +08002651 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002652 return "SPFC";
2653
2654 return NULL;
2655}
2656
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002657static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002658{
2659 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002660 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002661 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002662 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002663 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002664
Seth Forshee29cd2932012-01-18 13:44:09 -06002665 if (toshiba_acpi)
2666 return -EBUSY;
2667
Seth Forshee135740d2011-09-20 16:55:49 -05002668 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2669 TOSHIBA_ACPI_VERSION);
2670
Seth Forsheea540d6b2011-09-20 16:55:52 -05002671 hci_method = find_hci_method(acpi_dev->handle);
2672 if (!hci_method) {
2673 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002674 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002675 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002676
Seth Forshee135740d2011-09-20 16:55:49 -05002677 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2678 if (!dev)
2679 return -ENOMEM;
2680 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002681 dev->method_hci = hci_method;
Azael Avalosfc5462f2015-07-22 18:09:11 -06002682 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2683 dev->miscdev.name = "toshiba_acpi";
2684 dev->miscdev.fops = &toshiba_acpi_fops;
2685
2686 ret = misc_register(&dev->miscdev);
2687 if (ret) {
2688 pr_err("Failed to register miscdevice\n");
2689 kfree(dev);
2690 return ret;
2691 }
2692
Seth Forshee135740d2011-09-20 16:55:49 -05002693 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002694 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002695
Azael Avalosa2b34712015-03-20 16:55:17 -06002696 /* Query the BIOS for supported features */
2697
2698 /*
2699 * The "Special Functions" are always supported by the laptops
2700 * with the new keyboard layout, query for its presence to help
2701 * determine the keymap layout to use.
2702 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002703 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002704 dev->kbd_function_keys_supported = !ret;
2705
Seth Forshee6e02cc72011-09-20 16:55:51 -05002706 if (toshiba_acpi_setup_keyboard(dev))
2707 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002708
Azael Avalos695f6062015-07-22 18:09:13 -06002709 /* Determine whether or not BIOS supports transflective backlight */
2710 ret = get_tr_backlight_status(dev, &dummy);
2711 dev->tr_backlight_supported = !ret;
2712
Seth Forshee62cce752012-04-19 11:23:50 -05002713 ret = toshiba_acpi_setup_backlight(dev);
2714 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002715 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002716
Seth Forshee135740d2011-09-20 16:55:49 -05002717 if (toshiba_illumination_available(dev)) {
2718 dev->led_dev.name = "toshiba::illumination";
2719 dev->led_dev.max_brightness = 1;
2720 dev->led_dev.brightness_set = toshiba_illumination_set;
2721 dev->led_dev.brightness_get = toshiba_illumination_get;
2722 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002723 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002724 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002725
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002726 if (toshiba_eco_mode_available(dev)) {
2727 dev->eco_led.name = "toshiba::eco_mode";
2728 dev->eco_led.max_brightness = 1;
2729 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2730 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2731 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2732 dev->eco_supported = 1;
2733 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002734
Azael Avalos93f8c162014-09-12 18:50:36 -06002735 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002736 /*
2737 * Only register the LED if KBD illumination is supported
2738 * and the keyboard backlight operation mode is set to FN-Z
2739 */
2740 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2741 dev->kbd_led.name = "toshiba::kbd_backlight";
2742 dev->kbd_led.max_brightness = 1;
2743 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2744 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2745 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2746 dev->kbd_led_registered = 1;
2747 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002748
Azael Avalos9d8658a2014-03-25 20:38:32 -06002749 ret = toshiba_touchpad_get(dev, &dummy);
2750 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002751
Azael Avalos5a2813e2014-03-25 20:38:34 -06002752 ret = toshiba_accelerometer_supported(dev);
2753 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002754
Azael Avalosc8c91842015-04-02 19:26:20 -06002755 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002756
Azael Avalosbb3fe012015-01-18 18:30:24 -07002757 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2758 dev->usb_rapid_charge_supported = !ret;
2759
Azael Avalos172ce0a2015-01-18 18:30:25 -07002760 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2761 dev->usb_sleep_music_supported = !ret;
2762
Azael Avalos35d53ce2015-02-10 21:09:19 -07002763 ret = toshiba_panel_power_on_get(dev, &dummy);
2764 dev->panel_power_on_supported = !ret;
2765
Azael Avalos17fe4b32015-02-10 21:09:20 -07002766 ret = toshiba_usb_three_get(dev, &dummy);
2767 dev->usb_three_supported = !ret;
2768
Seth Forshee36d03f92011-09-20 16:55:53 -05002769 ret = get_video_status(dev, &dummy);
2770 dev->video_supported = !ret;
2771
2772 ret = get_fan_status(dev, &dummy);
2773 dev->fan_supported = !ret;
2774
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002775 /*
2776 * Enable the "Special Functions" mode only if they are
2777 * supported and if they are activated.
2778 */
2779 if (dev->kbd_function_keys_supported && special_functions)
2780 toshiba_acpi_enable_special_functions(dev);
2781
Azael Avalos360f0f32014-03-25 20:38:31 -06002782 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2783 &toshiba_attr_group);
2784 if (ret) {
2785 dev->sysfs_created = 0;
2786 goto error;
2787 }
2788 dev->sysfs_created = !ret;
2789
Seth Forshee36d03f92011-09-20 16:55:53 -05002790 create_toshiba_proc_entries(dev);
2791
Seth Forshee29cd2932012-01-18 13:44:09 -06002792 toshiba_acpi = dev;
2793
Seth Forshee135740d2011-09-20 16:55:49 -05002794 return 0;
2795
2796error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002797 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002798 return ret;
2799}
2800
2801static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2802{
2803 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002804 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002805
Azael Avalos71454d72014-12-04 20:22:46 -07002806 switch (event) {
2807 case 0x80: /* Hotkeys and some system events */
Azael Avalosa88bc062015-07-22 18:09:12 -06002808 /*
2809 * Machines with this WMI GUID aren't supported due to bugs in
2810 * their AML.
2811 *
2812 * Return silently to avoid triggering a netlink event.
2813 */
2814 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2815 return;
Azael Avalos71454d72014-12-04 20:22:46 -07002816 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
2902 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2903 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002904 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002905 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907
Seth Forshee135740d2011-09-20 16:55:49 -05002908 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2909 if (ret) {
2910 pr_err("Failed to register ACPI driver: %d\n", ret);
2911 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002912 }
2913
Seth Forshee135740d2011-09-20 16:55:49 -05002914 return ret;
2915}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002916
Seth Forshee135740d2011-09-20 16:55:49 -05002917static void __exit toshiba_acpi_exit(void)
2918{
2919 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2920 if (toshiba_proc_dir)
2921 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924module_init(toshiba_acpi_init);
2925module_exit(toshiba_acpi_exit);