blob: e9f7395efc3a2172cf2c488b93915e1e88b9f27e [file] [log] [blame]
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001/*
2 * ideapad_acpi.c - Lenovo IdeaPad ACPI Extras
3 *
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <acpi/acpi_bus.h>
28#include <acpi/acpi_drivers.h>
29#include <linux/rfkill.h>
30
31#define IDEAPAD_DEV_CAMERA 0
32#define IDEAPAD_DEV_WLAN 1
33#define IDEAPAD_DEV_BLUETOOTH 2
34#define IDEAPAD_DEV_3G 3
35#define IDEAPAD_DEV_KILLSW 4
36
David Woodhousece326322010-08-11 17:59:35 +010037struct ideapad_private {
Ike Panhc26c81d52010-10-01 15:39:40 +080038 acpi_handle handle;
David Woodhousece326322010-08-11 17:59:35 +010039 struct rfkill *rfk[5];
Ike Panhcfa083592010-10-01 15:39:59 +080040} *ideapad_priv;
David Woodhousece326322010-08-11 17:59:35 +010041
42static struct {
43 char *name;
Ike Panhcdfa7f6f2010-10-01 15:39:29 +080044 int cfgbit;
Ike Panhcfa083592010-10-01 15:39:59 +080045 int opcode;
David Woodhousece326322010-08-11 17:59:35 +010046 int type;
47} ideapad_rfk_data[] = {
Ike Panhcfa083592010-10-01 15:39:59 +080048 { "ideapad_camera", 19, 0x1E, NUM_RFKILL_TYPES },
49 { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN },
50 { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH },
51 { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN },
52 { "ideapad_killsw", 0, 0, RFKILL_TYPE_WLAN }
David Woodhouse58ac7aa2010-08-10 23:44:05 +010053};
54
Ike Panhc6a09f212010-10-01 15:38:46 +080055/*
56 * ACPI Helpers
57 */
58#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
59
60static int read_method_int(acpi_handle handle, const char *method, int *val)
61{
62 acpi_status status;
63 unsigned long long result;
64
65 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
66 if (ACPI_FAILURE(status)) {
67 *val = -1;
68 return -1;
69 } else {
70 *val = result;
71 return 0;
72 }
73}
74
75static int method_vpcr(acpi_handle handle, int cmd, int *ret)
76{
77 acpi_status status;
78 unsigned long long result;
79 struct acpi_object_list params;
80 union acpi_object in_obj;
81
82 params.count = 1;
83 params.pointer = &in_obj;
84 in_obj.type = ACPI_TYPE_INTEGER;
85 in_obj.integer.value = cmd;
86
87 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
88
89 if (ACPI_FAILURE(status)) {
90 *ret = -1;
91 return -1;
92 } else {
93 *ret = result;
94 return 0;
95 }
96}
97
98static int method_vpcw(acpi_handle handle, int cmd, int data)
99{
100 struct acpi_object_list params;
101 union acpi_object in_obj[2];
102 acpi_status status;
103
104 params.count = 2;
105 params.pointer = in_obj;
106 in_obj[0].type = ACPI_TYPE_INTEGER;
107 in_obj[0].integer.value = cmd;
108 in_obj[1].type = ACPI_TYPE_INTEGER;
109 in_obj[1].integer.value = data;
110
111 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
112 if (status != AE_OK)
113 return -1;
114 return 0;
115}
116
117static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
118{
119 int val;
120 unsigned long int end_jiffies;
121
122 if (method_vpcw(handle, 1, cmd))
123 return -1;
124
125 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
126 time_before(jiffies, end_jiffies);) {
127 schedule();
128 if (method_vpcr(handle, 1, &val))
129 return -1;
130 if (val == 0) {
131 if (method_vpcr(handle, 0, &val))
132 return -1;
133 *data = val;
134 return 0;
135 }
136 }
137 pr_err("timeout in read_ec_cmd\n");
138 return -1;
139}
140
141static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
142{
143 int val;
144 unsigned long int end_jiffies;
145
146 if (method_vpcw(handle, 0, data))
147 return -1;
148 if (method_vpcw(handle, 1, cmd))
149 return -1;
150
151 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
152 time_before(jiffies, end_jiffies);) {
153 schedule();
154 if (method_vpcr(handle, 1, &val))
155 return -1;
156 if (val == 0)
157 return 0;
158 }
159 pr_err("timeout in write_ec_cmd\n");
160 return -1;
161}
162/* the above is ACPI helpers */
163
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100164static ssize_t show_ideapad_cam(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
Ike Panhc26c81d52010-10-01 15:39:40 +0800168 struct ideapad_private *priv = dev_get_drvdata(dev);
169 acpi_handle handle = priv->handle;
170 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100171
Ike Panhc26c81d52010-10-01 15:39:40 +0800172 if (read_ec_data(handle, 0x1D, &result))
173 return sprintf(buf, "-1\n");
174 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100175}
176
177static ssize_t store_ideapad_cam(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
180{
Ike Panhc26c81d52010-10-01 15:39:40 +0800181 struct ideapad_private *priv = dev_get_drvdata(dev);
182 acpi_handle handle = priv->handle;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100183 int ret, state;
184
185 if (!count)
186 return 0;
187 if (sscanf(buf, "%i", &state) != 1)
188 return -EINVAL;
Ike Panhc26c81d52010-10-01 15:39:40 +0800189 ret = write_ec_cmd(handle, 0x1E, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100190 if (ret < 0)
191 return ret;
192 return count;
193}
194
195static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
196
197static int ideapad_rfk_set(void *data, bool blocked)
198{
199 int device = (unsigned long)data;
200
201 if (device == IDEAPAD_DEV_KILLSW)
202 return -EINVAL;
Ike Panhcfa083592010-10-01 15:39:59 +0800203
204 return write_ec_cmd(ideapad_priv->handle,
205 ideapad_rfk_data[device].opcode,
206 !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100207}
208
209static struct rfkill_ops ideapad_rfk_ops = {
210 .set_block = ideapad_rfk_set,
211};
212
David Woodhousece326322010-08-11 17:59:35 +0100213static void ideapad_sync_rfk_state(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100214{
David Woodhousece326322010-08-11 17:59:35 +0100215 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc2b7266b2010-10-01 15:39:49 +0800216 acpi_handle handle = priv->handle;
217 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100218 int i;
219
Ike Panhc2b7266b2010-10-01 15:39:49 +0800220 if (read_ec_data(handle, 0x23, &hw_blocked))
221 return;
222 hw_blocked = !hw_blocked;
223
224 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
David Woodhousece326322010-08-11 17:59:35 +0100225 if (priv->rfk[i])
226 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100227}
228
David Woodhousece326322010-08-11 17:59:35 +0100229static int ideapad_register_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100230{
David Woodhousece326322010-08-11 17:59:35 +0100231 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100232 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800233 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100234
Ike Panhc2b7266b2010-10-01 15:39:49 +0800235 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
236 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100237 (void *)(long)dev);
238 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100239 return -ENOMEM;
240
Ike Panhc2b7266b2010-10-01 15:39:49 +0800241 if (read_ec_data(ideapad_priv->handle, ideapad_rfk_data[dev].opcode-1,
242 &sw_blocked)) {
243 rfkill_init_sw_state(priv->rfk[dev], 0);
244 } else {
245 sw_blocked = !sw_blocked;
246 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
247 }
248
David Woodhousece326322010-08-11 17:59:35 +0100249 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100250 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100251 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100252 return ret;
253 }
254 return 0;
255}
256
David Woodhousece326322010-08-11 17:59:35 +0100257static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100258{
David Woodhousece326322010-08-11 17:59:35 +0100259 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
260
261 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100262 return;
263
David Woodhousece326322010-08-11 17:59:35 +0100264 rfkill_unregister(priv->rfk[dev]);
265 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100266}
267
268static const struct acpi_device_id ideapad_device_ids[] = {
269 { "VPC2004", 0},
270 { "", 0},
271};
272MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
273
David Woodhousece326322010-08-11 17:59:35 +0100274static int ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100275{
Ike Panhc6f8371c2010-10-01 15:39:14 +0800276 int i, cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100277 int devs_present[5];
David Woodhousece326322010-08-11 17:59:35 +0100278 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100279
Ike Panhc6f8371c2010-10-01 15:39:14 +0800280 if (read_method_int(adevice->handle, "_CFG", &cfg))
281 return -ENODEV;
282
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100283 for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) {
Ike Panhcdfa7f6f2010-10-01 15:39:29 +0800284 if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
285 devs_present[i] = 1;
286 else
287 devs_present[i] = 0;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100288 }
289
290 /* The hardware switch is always present */
291 devs_present[IDEAPAD_DEV_KILLSW] = 1;
292
David Woodhousece326322010-08-11 17:59:35 +0100293 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
294 if (!priv)
295 return -ENOMEM;
296
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100297 if (devs_present[IDEAPAD_DEV_CAMERA]) {
David Woodhousece326322010-08-11 17:59:35 +0100298 int ret = device_create_file(&adevice->dev, &dev_attr_camera_power);
299 if (ret) {
300 kfree(priv);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100301 return ret;
David Woodhousece326322010-08-11 17:59:35 +0100302 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100303 }
304
Ike Panhc26c81d52010-10-01 15:39:40 +0800305 priv->handle = adevice->handle;
David Woodhousece326322010-08-11 17:59:35 +0100306 dev_set_drvdata(&adevice->dev, priv);
Ike Panhcfa083592010-10-01 15:39:59 +0800307 ideapad_priv = priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100308 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) {
309 if (!devs_present[i])
310 continue;
311
David Woodhousece326322010-08-11 17:59:35 +0100312 ideapad_register_rfkill(adevice, i);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100313 }
David Woodhousece326322010-08-11 17:59:35 +0100314 ideapad_sync_rfk_state(adevice);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100315 return 0;
316}
317
David Woodhousece326322010-08-11 17:59:35 +0100318static int ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100319{
David Woodhousece326322010-08-11 17:59:35 +0100320 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100321 int i;
David Woodhousece326322010-08-11 17:59:35 +0100322
323 device_remove_file(&adevice->dev, &dev_attr_camera_power);
324
325 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
326 ideapad_unregister_rfkill(adevice, i);
327
328 dev_set_drvdata(&adevice->dev, NULL);
329 kfree(priv);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100330 return 0;
331}
332
David Woodhousece326322010-08-11 17:59:35 +0100333static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100334{
Ike Panhc8e7d3542010-10-01 15:39:05 +0800335 acpi_handle handle = adevice->handle;
336 unsigned long vpc1, vpc2, vpc_bit;
337
338 if (read_ec_data(handle, 0x10, &vpc1))
339 return;
340 if (read_ec_data(handle, 0x1A, &vpc2))
341 return;
342
343 vpc1 = (vpc2 << 8) | vpc1;
344 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
345 if (test_bit(vpc_bit, &vpc1)) {
346 if (vpc_bit == 9)
347 ideapad_sync_rfk_state(adevice);
348 }
349 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100350}
351
352static struct acpi_driver ideapad_acpi_driver = {
353 .name = "ideapad_acpi",
354 .class = "IdeaPad",
355 .ids = ideapad_device_ids,
356 .ops.add = ideapad_acpi_add,
357 .ops.remove = ideapad_acpi_remove,
358 .ops.notify = ideapad_acpi_notify,
359 .owner = THIS_MODULE,
360};
361
362
363static int __init ideapad_acpi_module_init(void)
364{
365 acpi_bus_register_driver(&ideapad_acpi_driver);
366
367 return 0;
368}
369
370
371static void __exit ideapad_acpi_module_exit(void)
372{
373 acpi_bus_unregister_driver(&ideapad_acpi_driver);
374
375}
376
377MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
378MODULE_DESCRIPTION("IdeaPad ACPI Extras");
379MODULE_LICENSE("GPL");
380
381module_init(ideapad_acpi_module_init);
382module_exit(ideapad_acpi_module_exit);