Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[sfrench/cifs-2.6.git] / drivers / input / misc / soc_button_array.c
1 /*
2  * Supports for the button array on SoC tablets originally running
3  * Windows 8.
4  *
5  * (C) Copyright 2014 Intel Corporation
6  *
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.
11  */
12
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>
22
23 struct soc_button_info {
24         const char *name;
25         int acpi_index;
26         unsigned int event_type;
27         unsigned int event_code;
28         bool autorepeat;
29         bool wakeup;
30 };
31
32 /*
33  * Some of the buttons like volume up/down are auto repeat, while others
34  * are not. To support both, we register two platform devices, and put
35  * buttons into them based on whether the key should be auto repeat.
36  */
37 #define BUTTON_TYPES    2
38
39 struct soc_button_data {
40         struct platform_device *children[BUTTON_TYPES];
41 };
42
43 /*
44  * Get the Nth GPIO number from the ACPI object.
45  */
46 static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
47 {
48         struct gpio_desc *desc;
49         int gpio;
50
51         desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
52         if (IS_ERR(desc))
53                 return PTR_ERR(desc);
54
55         gpio = desc_to_gpio(desc);
56
57         gpiod_put(desc);
58
59         return gpio;
60 }
61
62 static struct platform_device *
63 soc_button_device_create(struct platform_device *pdev,
64                          const struct soc_button_info *button_info,
65                          bool autorepeat)
66 {
67         const struct soc_button_info *info;
68         struct platform_device *pd;
69         struct gpio_keys_button *gpio_keys;
70         struct gpio_keys_platform_data *gpio_keys_pdata;
71         int n_buttons = 0;
72         int gpio;
73         int error;
74
75         for (info = button_info; info->name; info++)
76                 if (info->autorepeat == autorepeat)
77                         n_buttons++;
78
79         gpio_keys_pdata = devm_kzalloc(&pdev->dev,
80                                        sizeof(*gpio_keys_pdata) +
81                                         sizeof(*gpio_keys) * n_buttons,
82                                        GFP_KERNEL);
83         if (!gpio_keys_pdata)
84                 return ERR_PTR(-ENOMEM);
85
86         gpio_keys = (void *)(gpio_keys_pdata + 1);
87         n_buttons = 0;
88
89         for (info = button_info; info->name; info++) {
90                 if (info->autorepeat != autorepeat)
91                         continue;
92
93                 gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
94                 if (!gpio_is_valid(gpio))
95                         continue;
96
97                 gpio_keys[n_buttons].type = info->event_type;
98                 gpio_keys[n_buttons].code = info->event_code;
99                 gpio_keys[n_buttons].gpio = gpio;
100                 gpio_keys[n_buttons].active_low = 1;
101                 gpio_keys[n_buttons].desc = info->name;
102                 gpio_keys[n_buttons].wakeup = info->wakeup;
103                 /* These devices often use cheap buttons, use 50 ms debounce */
104                 gpio_keys[n_buttons].debounce_interval = 50;
105                 n_buttons++;
106         }
107
108         if (n_buttons == 0) {
109                 error = -ENODEV;
110                 goto err_free_mem;
111         }
112
113         gpio_keys_pdata->buttons = gpio_keys;
114         gpio_keys_pdata->nbuttons = n_buttons;
115         gpio_keys_pdata->rep = autorepeat;
116
117         pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO);
118         if (!pd) {
119                 error = -ENOMEM;
120                 goto err_free_mem;
121         }
122
123         error = platform_device_add_data(pd, gpio_keys_pdata,
124                                          sizeof(*gpio_keys_pdata));
125         if (error)
126                 goto err_free_pdev;
127
128         error = platform_device_add(pd);
129         if (error)
130                 goto err_free_pdev;
131
132         return pd;
133
134 err_free_pdev:
135         platform_device_put(pd);
136 err_free_mem:
137         devm_kfree(&pdev->dev, gpio_keys_pdata);
138         return ERR_PTR(error);
139 }
140
141 static int soc_button_get_acpi_object_int(const union acpi_object *obj)
142 {
143         if (obj->type != ACPI_TYPE_INTEGER)
144                 return -1;
145
146         return obj->integer.value;
147 }
148
149 /* Parse a single ACPI0011 _DSD button descriptor */
150 static int soc_button_parse_btn_desc(struct device *dev,
151                                      const union acpi_object *desc,
152                                      int collection_uid,
153                                      struct soc_button_info *info)
154 {
155         int upage, usage;
156
157         if (desc->type != ACPI_TYPE_PACKAGE ||
158             desc->package.count != 5 ||
159             /* First byte should be 1 (control) */
160             soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
161             /* Third byte should be collection uid */
162             soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
163                                                             collection_uid) {
164                 dev_err(dev, "Invalid ACPI Button Descriptor\n");
165                 return -ENODEV;
166         }
167
168         info->event_type = EV_KEY;
169         info->acpi_index =
170                 soc_button_get_acpi_object_int(&desc->package.elements[1]);
171         upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
172         usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
173
174         /*
175          * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
176          * usage page and usage codes, but otherwise the device is not HID
177          * compliant: it uses one irq per button instead of generating HID
178          * input reports and some buttons should generate wakeups where as
179          * others should not, so we cannot use the HID subsystem.
180          *
181          * Luckily all devices only use a few usage page + usage combinations,
182          * so we can simply check for the known combinations here.
183          */
184         if (upage == 0x01 && usage == 0x81) {
185                 info->name = "power";
186                 info->event_code = KEY_POWER;
187                 info->wakeup = true;
188         } else if (upage == 0x07 && usage == 0xe3) {
189                 info->name = "home";
190                 info->event_code = KEY_LEFTMETA;
191                 info->wakeup = true;
192         } else if (upage == 0x0c && usage == 0xe9) {
193                 info->name = "volume_up";
194                 info->event_code = KEY_VOLUMEUP;
195                 info->autorepeat = true;
196         } else if (upage == 0x0c && usage == 0xea) {
197                 info->name = "volume_down";
198                 info->event_code = KEY_VOLUMEDOWN;
199                 info->autorepeat = true;
200         } else {
201                 dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
202                          info->acpi_index, upage, usage);
203                 info->name = "unknown";
204                 info->event_code = KEY_RESERVED;
205         }
206
207         return 0;
208 }
209
210 /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
211 static const u8 btns_desc_uuid[16] = {
212         0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
213         0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
214 };
215
216 /* Parse ACPI0011 _DSD button descriptors */
217 static struct soc_button_info *soc_button_get_button_info(struct device *dev)
218 {
219         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
220         const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
221         struct soc_button_info *button_info;
222         acpi_status status;
223         int i, btn, collection_uid = -1;
224
225         status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
226                                             &buf, ACPI_TYPE_PACKAGE);
227         if (ACPI_FAILURE(status)) {
228                 dev_err(dev, "ACPI _DSD object not found\n");
229                 return ERR_PTR(-ENODEV);
230         }
231
232         /* Look for the Button Descriptors UUID */
233         desc = buf.pointer;
234         for (i = 0; (i + 1) < desc->package.count; i += 2) {
235                 uuid = &desc->package.elements[i];
236
237                 if (uuid->type != ACPI_TYPE_BUFFER ||
238                     uuid->buffer.length != 16 ||
239                     desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
240                         break;
241                 }
242
243                 if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
244                         btns_desc = &desc->package.elements[i + 1];
245                         break;
246                 }
247         }
248
249         if (!btns_desc) {
250                 dev_err(dev, "ACPI Button Descriptors not found\n");
251                 return ERR_PTR(-ENODEV);
252         }
253
254         /* The first package describes the collection */
255         el0 = &btns_desc->package.elements[0];
256         if (el0->type == ACPI_TYPE_PACKAGE &&
257             el0->package.count == 5 &&
258             /* First byte should be 0 (collection) */
259             soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
260             /* Third byte should be 0 (top level collection) */
261             soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
262                 collection_uid = soc_button_get_acpi_object_int(
263                                                 &el0->package.elements[1]);
264         }
265         if (collection_uid == -1) {
266                 dev_err(dev, "Invalid Button Collection Descriptor\n");
267                 return ERR_PTR(-ENODEV);
268         }
269
270         /* There are package.count - 1 buttons + 1 terminating empty entry */
271         button_info = devm_kcalloc(dev, btns_desc->package.count,
272                                    sizeof(*button_info), GFP_KERNEL);
273         if (!button_info)
274                 return ERR_PTR(-ENOMEM);
275
276         /* Parse the button descriptors */
277         for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
278                 if (soc_button_parse_btn_desc(dev,
279                                               &btns_desc->package.elements[i],
280                                               collection_uid,
281                                               &button_info[btn]))
282                         return ERR_PTR(-ENODEV);
283         }
284
285         return button_info;
286 }
287
288 static int soc_button_remove(struct platform_device *pdev)
289 {
290         struct soc_button_data *priv = platform_get_drvdata(pdev);
291
292         int i;
293
294         for (i = 0; i < BUTTON_TYPES; i++)
295                 if (priv->children[i])
296                         platform_device_unregister(priv->children[i]);
297
298         return 0;
299 }
300
301 static int soc_button_probe(struct platform_device *pdev)
302 {
303         struct device *dev = &pdev->dev;
304         const struct acpi_device_id *id;
305         struct soc_button_info *button_info;
306         struct soc_button_data *priv;
307         struct platform_device *pd;
308         int i;
309         int error;
310
311         id = acpi_match_device(dev->driver->acpi_match_table, dev);
312         if (!id)
313                 return -ENODEV;
314
315         if (!id->driver_data) {
316                 button_info = soc_button_get_button_info(dev);
317                 if (IS_ERR(button_info))
318                         return PTR_ERR(button_info);
319         } else {
320                 button_info = (struct soc_button_info *)id->driver_data;
321         }
322
323         error = gpiod_count(dev, NULL);
324         if (error < 0) {
325                 dev_dbg(dev, "no GPIO attached, ignoring...\n");
326                 return error;
327         }
328
329         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
330         if (!priv)
331                 return -ENOMEM;
332
333         platform_set_drvdata(pdev, priv);
334
335         for (i = 0; i < BUTTON_TYPES; i++) {
336                 pd = soc_button_device_create(pdev, button_info, i == 0);
337                 if (IS_ERR(pd)) {
338                         error = PTR_ERR(pd);
339                         if (error != -ENODEV) {
340                                 soc_button_remove(pdev);
341                                 return error;
342                         }
343                         continue;
344                 }
345
346                 priv->children[i] = pd;
347         }
348
349         if (!priv->children[0] && !priv->children[1])
350                 return -ENODEV;
351
352         if (!id->driver_data)
353                 devm_kfree(dev, button_info);
354
355         return 0;
356 }
357
358 /*
359  * Definition of buttons on the tablet. The ACPI index of each button
360  * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
361  * Platforms"
362  */
363 static struct soc_button_info soc_button_PNP0C40[] = {
364         { "power", 0, EV_KEY, KEY_POWER, false, true },
365         { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
366         { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
367         { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
368         { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
369         { }
370 };
371
372 static const struct acpi_device_id soc_button_acpi_match[] = {
373         { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
374         { "ACPI0011", 0 },
375         { }
376 };
377
378 MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
379
380 static struct platform_driver soc_button_driver = {
381         .probe          = soc_button_probe,
382         .remove         = soc_button_remove,
383         .driver         = {
384                 .name = KBUILD_MODNAME,
385                 .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
386         },
387 };
388 module_platform_driver(soc_button_driver);
389
390 MODULE_LICENSE("GPL");