drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / platform / x86 / dell-rbtn.c
blob56535d7222ddc1b94e4d201aead0bca933133828
1 /*
2 Dell Airplane Mode Switch driver
3 Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/rfkill.h>
19 #include <linux/input.h>
21 #include "dell-rbtn.h"
23 enum rbtn_type {
24 RBTN_UNKNOWN,
25 RBTN_TOGGLE,
26 RBTN_SLIDER,
29 struct rbtn_data {
30 enum rbtn_type type;
31 struct rfkill *rfkill;
32 struct input_dev *input_dev;
33 bool suspended;
38 * acpi functions
41 static enum rbtn_type rbtn_check(struct acpi_device *device)
43 unsigned long long output;
44 acpi_status status;
46 status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
47 if (ACPI_FAILURE(status))
48 return RBTN_UNKNOWN;
50 switch (output) {
51 case 0:
52 case 1:
53 return RBTN_TOGGLE;
54 case 2:
55 case 3:
56 return RBTN_SLIDER;
57 default:
58 return RBTN_UNKNOWN;
62 static int rbtn_get(struct acpi_device *device)
64 unsigned long long output;
65 acpi_status status;
67 status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
68 if (ACPI_FAILURE(status))
69 return -EINVAL;
71 return !output;
74 static int rbtn_acquire(struct acpi_device *device, bool enable)
76 struct acpi_object_list input;
77 union acpi_object param;
78 acpi_status status;
80 param.type = ACPI_TYPE_INTEGER;
81 param.integer.value = enable;
82 input.count = 1;
83 input.pointer = &param;
85 status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
86 if (ACPI_FAILURE(status))
87 return -EINVAL;
89 return 0;
94 * rfkill device
97 static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
99 struct acpi_device *device = data;
100 int state;
102 state = rbtn_get(device);
103 if (state < 0)
104 return;
106 rfkill_set_states(rfkill, state, state);
109 static int rbtn_rfkill_set_block(void *data, bool blocked)
111 /* NOTE: setting soft rfkill state is not supported */
112 return -EINVAL;
115 static const struct rfkill_ops rbtn_ops = {
116 .query = rbtn_rfkill_query,
117 .set_block = rbtn_rfkill_set_block,
120 static int rbtn_rfkill_init(struct acpi_device *device)
122 struct rbtn_data *rbtn_data = device->driver_data;
123 int ret;
125 if (rbtn_data->rfkill)
126 return 0;
129 * NOTE: rbtn controls all radio devices, not only WLAN
130 * but rfkill interface does not support "ANY" type
131 * so "WLAN" type is used
133 rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
134 RFKILL_TYPE_WLAN, &rbtn_ops, device);
135 if (!rbtn_data->rfkill)
136 return -ENOMEM;
138 ret = rfkill_register(rbtn_data->rfkill);
139 if (ret) {
140 rfkill_destroy(rbtn_data->rfkill);
141 rbtn_data->rfkill = NULL;
142 return ret;
145 return 0;
148 static void rbtn_rfkill_exit(struct acpi_device *device)
150 struct rbtn_data *rbtn_data = device->driver_data;
152 if (!rbtn_data->rfkill)
153 return;
155 rfkill_unregister(rbtn_data->rfkill);
156 rfkill_destroy(rbtn_data->rfkill);
157 rbtn_data->rfkill = NULL;
160 static void rbtn_rfkill_event(struct acpi_device *device)
162 struct rbtn_data *rbtn_data = device->driver_data;
164 if (rbtn_data->rfkill)
165 rbtn_rfkill_query(rbtn_data->rfkill, device);
170 * input device
173 static int rbtn_input_init(struct rbtn_data *rbtn_data)
175 int ret;
177 rbtn_data->input_dev = input_allocate_device();
178 if (!rbtn_data->input_dev)
179 return -ENOMEM;
181 rbtn_data->input_dev->name = "DELL Wireless hotkeys";
182 rbtn_data->input_dev->phys = "dellabce/input0";
183 rbtn_data->input_dev->id.bustype = BUS_HOST;
184 rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
185 set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
187 ret = input_register_device(rbtn_data->input_dev);
188 if (ret) {
189 input_free_device(rbtn_data->input_dev);
190 rbtn_data->input_dev = NULL;
191 return ret;
194 return 0;
197 static void rbtn_input_exit(struct rbtn_data *rbtn_data)
199 input_unregister_device(rbtn_data->input_dev);
200 rbtn_data->input_dev = NULL;
203 static void rbtn_input_event(struct rbtn_data *rbtn_data)
205 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
206 input_sync(rbtn_data->input_dev);
207 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
208 input_sync(rbtn_data->input_dev);
213 * acpi driver
216 static int rbtn_add(struct acpi_device *device);
217 static int rbtn_remove(struct acpi_device *device);
218 static void rbtn_notify(struct acpi_device *device, u32 event);
220 static const struct acpi_device_id rbtn_ids[] = {
221 { "DELRBTN", 0 },
222 { "DELLABCE", 0 },
225 * This driver can also handle the "DELLABC6" device that
226 * appears on the XPS 13 9350, but that device is disabled by
227 * the DSDT unless booted with acpi_osi="!Windows 2012"
228 * acpi_osi="!Windows 2013".
230 * According to Mario at Dell:
232 * DELLABC6 is a custom interface that was created solely to
233 * have airplane mode support for Windows 7. For Windows 10
234 * the proper interface is to use that which is handled by
235 * intel-hid. A OEM airplane mode driver is not used.
237 * Since the kernel doesn't identify as Windows 7 it would be
238 * incorrect to do attempt to use that interface.
240 * Even if we override _OSI and bind to DELLABC6, we end up with
241 * inconsistent behavior in which userspace can get out of sync
242 * with the rfkill state as it conflicts with events from
243 * intel-hid.
245 * The upshot is that it is better to just ignore DELLABC6
246 * devices.
249 { "", 0 },
252 #ifdef CONFIG_PM_SLEEP
253 static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
255 struct rbtn_data *rbtn_data = context;
257 rbtn_data->suspended = false;
260 static int rbtn_suspend(struct device *dev)
262 struct acpi_device *device = to_acpi_device(dev);
263 struct rbtn_data *rbtn_data = acpi_driver_data(device);
265 rbtn_data->suspended = true;
267 return 0;
270 static int rbtn_resume(struct device *dev)
272 struct acpi_device *device = to_acpi_device(dev);
273 struct rbtn_data *rbtn_data = acpi_driver_data(device);
274 acpi_status status;
277 * Upon resume, some BIOSes send an ACPI notification thet triggers
278 * an unwanted input event. In order to ignore it, we use a flag
279 * that we set at suspend and clear once we have received the extra
280 * ACPI notification. Since ACPI notifications are delivered
281 * asynchronously to drivers, we clear the flag from the workqueue
282 * used to deliver the notifications. This should be enough
283 * to have the flag cleared only after we received the extra
284 * notification, if any.
286 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
287 rbtn_clear_suspended_flag, rbtn_data);
288 if (ACPI_FAILURE(status))
289 rbtn_clear_suspended_flag(rbtn_data);
291 return 0;
293 #endif
295 static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
297 static struct acpi_driver rbtn_driver = {
298 .name = "dell-rbtn",
299 .ids = rbtn_ids,
300 .drv.pm = &rbtn_pm_ops,
301 .ops = {
302 .add = rbtn_add,
303 .remove = rbtn_remove,
304 .notify = rbtn_notify,
306 .owner = THIS_MODULE,
311 * notifier export functions
314 static bool auto_remove_rfkill = true;
316 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
318 static int rbtn_inc_count(struct device *dev, void *data)
320 struct acpi_device *device = to_acpi_device(dev);
321 struct rbtn_data *rbtn_data = device->driver_data;
322 int *count = data;
324 if (rbtn_data->type == RBTN_SLIDER)
325 (*count)++;
327 return 0;
330 static int rbtn_switch_dev(struct device *dev, void *data)
332 struct acpi_device *device = to_acpi_device(dev);
333 struct rbtn_data *rbtn_data = device->driver_data;
334 bool enable = data;
336 if (rbtn_data->type != RBTN_SLIDER)
337 return 0;
339 if (enable)
340 rbtn_rfkill_init(device);
341 else
342 rbtn_rfkill_exit(device);
344 return 0;
347 int dell_rbtn_notifier_register(struct notifier_block *nb)
349 bool first;
350 int count;
351 int ret;
353 count = 0;
354 ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
355 rbtn_inc_count);
356 if (ret || count == 0)
357 return -ENODEV;
359 first = !rbtn_chain_head.head;
361 ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
362 if (ret != 0)
363 return ret;
365 if (auto_remove_rfkill && first)
366 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
367 (void *)false, rbtn_switch_dev);
369 return ret;
371 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
373 int dell_rbtn_notifier_unregister(struct notifier_block *nb)
375 int ret;
377 ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
378 if (ret != 0)
379 return ret;
381 if (auto_remove_rfkill && !rbtn_chain_head.head)
382 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
383 (void *)true, rbtn_switch_dev);
385 return ret;
387 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
391 * acpi driver functions
394 static int rbtn_add(struct acpi_device *device)
396 struct rbtn_data *rbtn_data;
397 enum rbtn_type type;
398 int ret = 0;
400 type = rbtn_check(device);
401 if (type == RBTN_UNKNOWN) {
402 dev_info(&device->dev, "Unknown device type\n");
403 return -EINVAL;
406 ret = rbtn_acquire(device, true);
407 if (ret < 0) {
408 dev_err(&device->dev, "Cannot enable device\n");
409 return ret;
412 rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
413 if (!rbtn_data)
414 return -ENOMEM;
416 rbtn_data->type = type;
417 device->driver_data = rbtn_data;
419 switch (rbtn_data->type) {
420 case RBTN_TOGGLE:
421 ret = rbtn_input_init(rbtn_data);
422 break;
423 case RBTN_SLIDER:
424 if (auto_remove_rfkill && rbtn_chain_head.head)
425 ret = 0;
426 else
427 ret = rbtn_rfkill_init(device);
428 break;
429 default:
430 ret = -EINVAL;
433 return ret;
437 static int rbtn_remove(struct acpi_device *device)
439 struct rbtn_data *rbtn_data = device->driver_data;
441 switch (rbtn_data->type) {
442 case RBTN_TOGGLE:
443 rbtn_input_exit(rbtn_data);
444 break;
445 case RBTN_SLIDER:
446 rbtn_rfkill_exit(device);
447 break;
448 default:
449 break;
452 rbtn_acquire(device, false);
453 device->driver_data = NULL;
455 return 0;
458 static void rbtn_notify(struct acpi_device *device, u32 event)
460 struct rbtn_data *rbtn_data = device->driver_data;
463 * Some BIOSes send a notification at resume.
464 * Ignore it to prevent unwanted input events.
466 if (rbtn_data->suspended) {
467 dev_dbg(&device->dev, "ACPI notification ignored\n");
468 return;
471 if (event != 0x80) {
472 dev_info(&device->dev, "Received unknown event (0x%x)\n",
473 event);
474 return;
477 switch (rbtn_data->type) {
478 case RBTN_TOGGLE:
479 rbtn_input_event(rbtn_data);
480 break;
481 case RBTN_SLIDER:
482 rbtn_rfkill_event(device);
483 atomic_notifier_call_chain(&rbtn_chain_head, event, device);
484 break;
485 default:
486 break;
492 * module functions
495 module_acpi_driver(rbtn_driver);
497 module_param(auto_remove_rfkill, bool, 0444);
499 MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
500 "other modules start receiving events "
501 "from this module and re-add them when "
502 "the last module stops receiving events "
503 "(default true)");
504 MODULE_DEVICE_TABLE(acpi, rbtn_ids);
505 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
506 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
507 MODULE_LICENSE("GPL");