diff options
author | Ike Panhc <ike.pan@canonical.com> | 2010-10-01 03:40:09 -0400 |
---|---|---|
committer | Matthew Garrett <mjg@redhat.com> | 2010-10-21 09:36:51 -0400 |
commit | 57ac3b051cc09693f2e0f4725c87091ab11c7318 (patch) | |
tree | c2bd612211ebf861808ac40ef18f1ce8ecdde271 /drivers/platform/x86/ideapad-laptop.c | |
parent | fa08359ee29bd0dc52a4281d0e482fff08664b96 (diff) |
ideapad: Change the driver name to ideapad-laptop
Since the platform drivers doing more for laptops than just using specific
ACPI device. It will be good to change the name from *_acpi to *-laptop.
Reference: http://lkml.org/lkml/2010/8/14/154
Signed-off-by: Ike Panhc <ike.pan@canonical.com>
Acked-by: Len Brown <len.brown@intel.com>
Signed-off-by: Matthew Garrett <mjg@redhat.com>
Diffstat (limited to 'drivers/platform/x86/ideapad-laptop.c')
-rw-r--r-- | drivers/platform/x86/ideapad-laptop.c | 382 |
1 files changed, 382 insertions, 0 deletions
diff --git a/drivers/platform/x86/ideapad-laptop.c b/drivers/platform/x86/ideapad-laptop.c new file mode 100644 index 000000000000..e9f7395efc3a --- /dev/null +++ b/drivers/platform/x86/ideapad-laptop.c | |||
@@ -0,0 +1,382 @@ | |||
1 | /* | ||
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 | |||
37 | struct ideapad_private { | ||
38 | acpi_handle handle; | ||
39 | struct rfkill *rfk[5]; | ||
40 | } *ideapad_priv; | ||
41 | |||
42 | static struct { | ||
43 | char *name; | ||
44 | int cfgbit; | ||
45 | int opcode; | ||
46 | int type; | ||
47 | } ideapad_rfk_data[] = { | ||
48 | { "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 } | ||
53 | }; | ||
54 | |||
55 | /* | ||
56 | * ACPI Helpers | ||
57 | */ | ||
58 | #define IDEAPAD_EC_TIMEOUT (100) /* in ms */ | ||
59 | |||
60 | static 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 | |||
75 | static 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", ¶ms, &result); | ||
88 | |||
89 | if (ACPI_FAILURE(status)) { | ||
90 | *ret = -1; | ||
91 | return -1; | ||
92 | } else { | ||
93 | *ret = result; | ||
94 | return 0; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | static 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", ¶ms, NULL); | ||
112 | if (status != AE_OK) | ||
113 | return -1; | ||
114 | return 0; | ||
115 | } | ||
116 | |||
117 | static 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 | |||
141 | static 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 | |||
164 | static ssize_t show_ideapad_cam(struct device *dev, | ||
165 | struct device_attribute *attr, | ||
166 | char *buf) | ||
167 | { | ||
168 | struct ideapad_private *priv = dev_get_drvdata(dev); | ||
169 | acpi_handle handle = priv->handle; | ||
170 | unsigned long result; | ||
171 | |||
172 | if (read_ec_data(handle, 0x1D, &result)) | ||
173 | return sprintf(buf, "-1\n"); | ||
174 | return sprintf(buf, "%lu\n", result); | ||
175 | } | ||
176 | |||
177 | static ssize_t store_ideapad_cam(struct device *dev, | ||
178 | struct device_attribute *attr, | ||
179 | const char *buf, size_t count) | ||
180 | { | ||
181 | struct ideapad_private *priv = dev_get_drvdata(dev); | ||
182 | acpi_handle handle = priv->handle; | ||
183 | int ret, state; | ||
184 | |||
185 | if (!count) | ||
186 | return 0; | ||
187 | if (sscanf(buf, "%i", &state) != 1) | ||
188 | return -EINVAL; | ||
189 | ret = write_ec_cmd(handle, 0x1E, state); | ||
190 | if (ret < 0) | ||
191 | return ret; | ||
192 | return count; | ||
193 | } | ||
194 | |||
195 | static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam); | ||
196 | |||
197 | static 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; | ||
203 | |||
204 | return write_ec_cmd(ideapad_priv->handle, | ||
205 | ideapad_rfk_data[device].opcode, | ||
206 | !blocked); | ||
207 | } | ||
208 | |||
209 | static struct rfkill_ops ideapad_rfk_ops = { | ||
210 | .set_block = ideapad_rfk_set, | ||
211 | }; | ||
212 | |||
213 | static void ideapad_sync_rfk_state(struct acpi_device *adevice) | ||
214 | { | ||
215 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); | ||
216 | acpi_handle handle = priv->handle; | ||
217 | unsigned long hw_blocked; | ||
218 | int i; | ||
219 | |||
220 | 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++) | ||
225 | if (priv->rfk[i]) | ||
226 | rfkill_set_hw_state(priv->rfk[i], hw_blocked); | ||
227 | } | ||
228 | |||
229 | static int ideapad_register_rfkill(struct acpi_device *adevice, int dev) | ||
230 | { | ||
231 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); | ||
232 | int ret; | ||
233 | unsigned long sw_blocked; | ||
234 | |||
235 | priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev, | ||
236 | ideapad_rfk_data[dev].type, &ideapad_rfk_ops, | ||
237 | (void *)(long)dev); | ||
238 | if (!priv->rfk[dev]) | ||
239 | return -ENOMEM; | ||
240 | |||
241 | 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 | |||
249 | ret = rfkill_register(priv->rfk[dev]); | ||
250 | if (ret) { | ||
251 | rfkill_destroy(priv->rfk[dev]); | ||
252 | return ret; | ||
253 | } | ||
254 | return 0; | ||
255 | } | ||
256 | |||
257 | static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev) | ||
258 | { | ||
259 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); | ||
260 | |||
261 | if (!priv->rfk[dev]) | ||
262 | return; | ||
263 | |||
264 | rfkill_unregister(priv->rfk[dev]); | ||
265 | rfkill_destroy(priv->rfk[dev]); | ||
266 | } | ||
267 | |||
268 | static const struct acpi_device_id ideapad_device_ids[] = { | ||
269 | { "VPC2004", 0}, | ||
270 | { "", 0}, | ||
271 | }; | ||
272 | MODULE_DEVICE_TABLE(acpi, ideapad_device_ids); | ||
273 | |||
274 | static int ideapad_acpi_add(struct acpi_device *adevice) | ||
275 | { | ||
276 | int i, cfg; | ||
277 | int devs_present[5]; | ||
278 | struct ideapad_private *priv; | ||
279 | |||
280 | if (read_method_int(adevice->handle, "_CFG", &cfg)) | ||
281 | return -ENODEV; | ||
282 | |||
283 | for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) { | ||
284 | if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg)) | ||
285 | devs_present[i] = 1; | ||
286 | else | ||
287 | devs_present[i] = 0; | ||
288 | } | ||
289 | |||
290 | /* The hardware switch is always present */ | ||
291 | devs_present[IDEAPAD_DEV_KILLSW] = 1; | ||
292 | |||
293 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | ||
294 | if (!priv) | ||
295 | return -ENOMEM; | ||
296 | |||
297 | if (devs_present[IDEAPAD_DEV_CAMERA]) { | ||
298 | int ret = device_create_file(&adevice->dev, &dev_attr_camera_power); | ||
299 | if (ret) { | ||
300 | kfree(priv); | ||
301 | return ret; | ||
302 | } | ||
303 | } | ||
304 | |||
305 | priv->handle = adevice->handle; | ||
306 | dev_set_drvdata(&adevice->dev, priv); | ||
307 | ideapad_priv = priv; | ||
308 | for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) { | ||
309 | if (!devs_present[i]) | ||
310 | continue; | ||
311 | |||
312 | ideapad_register_rfkill(adevice, i); | ||
313 | } | ||
314 | ideapad_sync_rfk_state(adevice); | ||
315 | return 0; | ||
316 | } | ||
317 | |||
318 | static int ideapad_acpi_remove(struct acpi_device *adevice, int type) | ||
319 | { | ||
320 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); | ||
321 | int i; | ||
322 | |||
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); | ||
330 | return 0; | ||
331 | } | ||
332 | |||
333 | static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event) | ||
334 | { | ||
335 | 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 | } | ||
350 | } | ||
351 | |||
352 | static 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 | |||
363 | static int __init ideapad_acpi_module_init(void) | ||
364 | { | ||
365 | acpi_bus_register_driver(&ideapad_acpi_driver); | ||
366 | |||
367 | return 0; | ||
368 | } | ||
369 | |||
370 | |||
371 | static void __exit ideapad_acpi_module_exit(void) | ||
372 | { | ||
373 | acpi_bus_unregister_driver(&ideapad_acpi_driver); | ||
374 | |||
375 | } | ||
376 | |||
377 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | ||
378 | MODULE_DESCRIPTION("IdeaPad ACPI Extras"); | ||
379 | MODULE_LICENSE("GPL"); | ||
380 | |||
381 | module_init(ideapad_acpi_module_init); | ||
382 | module_exit(ideapad_acpi_module_exit); | ||