rcu: Maintain special bits at bottom of ->dynticks counter
[linux/fpc-iii.git] / drivers / input / misc / soc_button_array.c
blobddb2f22fca7aff42ff7c3ae13b7b3f3e18eefafa
1 /*
2 * Supports for the button array on SoC tablets originally running
3 * Windows 8.
5 * (C) Copyright 2014 Intel Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
13 #include <linux/module.h>
14 #include <linux/input.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/acpi.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/gpio_keys.h>
20 #include <linux/gpio.h>
21 #include <linux/platform_device.h>
24 * Definition of buttons on the tablet. The ACPI index of each button
25 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
26 * Platforms"
28 #define MAX_NBUTTONS 5
30 struct soc_button_info {
31 const char *name;
32 int acpi_index;
33 unsigned int event_type;
34 unsigned int event_code;
35 bool autorepeat;
36 bool wakeup;
40 * Some of the buttons like volume up/down are auto repeat, while others
41 * are not. To support both, we register two platform devices, and put
42 * buttons into them based on whether the key should be auto repeat.
44 #define BUTTON_TYPES 2
46 struct soc_button_data {
47 struct platform_device *children[BUTTON_TYPES];
51 * Get the Nth GPIO number from the ACPI object.
53 static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
55 struct gpio_desc *desc;
56 int gpio;
58 desc = gpiod_get_index(dev, KBUILD_MODNAME, acpi_index, GPIOD_ASIS);
59 if (IS_ERR(desc))
60 return PTR_ERR(desc);
62 gpio = desc_to_gpio(desc);
64 gpiod_put(desc);
66 return gpio;
69 static struct platform_device *
70 soc_button_device_create(struct platform_device *pdev,
71 const struct soc_button_info *button_info,
72 bool autorepeat)
74 const struct soc_button_info *info;
75 struct platform_device *pd;
76 struct gpio_keys_button *gpio_keys;
77 struct gpio_keys_platform_data *gpio_keys_pdata;
78 int n_buttons = 0;
79 int gpio;
80 int error;
82 gpio_keys_pdata = devm_kzalloc(&pdev->dev,
83 sizeof(*gpio_keys_pdata) +
84 sizeof(*gpio_keys) * MAX_NBUTTONS,
85 GFP_KERNEL);
86 if (!gpio_keys_pdata)
87 return ERR_PTR(-ENOMEM);
89 gpio_keys = (void *)(gpio_keys_pdata + 1);
91 for (info = button_info; info->name; info++) {
92 if (info->autorepeat != autorepeat)
93 continue;
95 gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
96 if (!gpio_is_valid(gpio))
97 continue;
99 gpio_keys[n_buttons].type = info->event_type;
100 gpio_keys[n_buttons].code = info->event_code;
101 gpio_keys[n_buttons].gpio = gpio;
102 gpio_keys[n_buttons].active_low = 1;
103 gpio_keys[n_buttons].desc = info->name;
104 gpio_keys[n_buttons].wakeup = info->wakeup;
105 /* These devices often use cheap buttons, use 50 ms debounce */
106 gpio_keys[n_buttons].debounce_interval = 50;
107 n_buttons++;
110 if (n_buttons == 0) {
111 error = -ENODEV;
112 goto err_free_mem;
115 gpio_keys_pdata->buttons = gpio_keys;
116 gpio_keys_pdata->nbuttons = n_buttons;
117 gpio_keys_pdata->rep = autorepeat;
119 pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO);
120 if (!pd) {
121 error = -ENOMEM;
122 goto err_free_mem;
125 error = platform_device_add_data(pd, gpio_keys_pdata,
126 sizeof(*gpio_keys_pdata));
127 if (error)
128 goto err_free_pdev;
130 error = platform_device_add(pd);
131 if (error)
132 goto err_free_pdev;
134 return pd;
136 err_free_pdev:
137 platform_device_put(pd);
138 err_free_mem:
139 devm_kfree(&pdev->dev, gpio_keys_pdata);
140 return ERR_PTR(error);
143 static int soc_button_remove(struct platform_device *pdev)
145 struct soc_button_data *priv = platform_get_drvdata(pdev);
147 int i;
149 for (i = 0; i < BUTTON_TYPES; i++)
150 if (priv->children[i])
151 platform_device_unregister(priv->children[i]);
153 return 0;
156 static int soc_button_probe(struct platform_device *pdev)
158 struct device *dev = &pdev->dev;
159 const struct acpi_device_id *id;
160 struct soc_button_info *button_info;
161 struct soc_button_data *priv;
162 struct platform_device *pd;
163 int i;
164 int error;
166 id = acpi_match_device(dev->driver->acpi_match_table, dev);
167 if (!id)
168 return -ENODEV;
170 button_info = (struct soc_button_info *)id->driver_data;
172 if (gpiod_count(dev, KBUILD_MODNAME) <= 0) {
173 dev_dbg(dev, "no GPIO attached, ignoring...\n");
174 return -ENODEV;
177 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
178 if (!priv)
179 return -ENOMEM;
181 platform_set_drvdata(pdev, priv);
183 for (i = 0; i < BUTTON_TYPES; i++) {
184 pd = soc_button_device_create(pdev, button_info, i == 0);
185 if (IS_ERR(pd)) {
186 error = PTR_ERR(pd);
187 if (error != -ENODEV) {
188 soc_button_remove(pdev);
189 return error;
191 continue;
194 priv->children[i] = pd;
197 if (!priv->children[0] && !priv->children[1])
198 return -ENODEV;
200 return 0;
203 static struct soc_button_info soc_button_PNP0C40[] = {
204 { "power", 0, EV_KEY, KEY_POWER, false, true },
205 { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
206 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
207 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
208 { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
212 static const struct acpi_device_id soc_button_acpi_match[] = {
213 { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
217 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
219 static struct platform_driver soc_button_driver = {
220 .probe = soc_button_probe,
221 .remove = soc_button_remove,
222 .driver = {
223 .name = KBUILD_MODNAME,
224 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
227 module_platform_driver(soc_button_driver);
229 MODULE_LICENSE("GPL");