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/platform_device.h>
21#include <linux/acpi.h>
22
23/*
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"
27 */
28#define MAX_NBUTTONS	5
29
30struct 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;
37};
38
39/*
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.
43 */
44#define BUTTON_TYPES	2
45
46struct soc_button_data {
47	struct platform_device *children[BUTTON_TYPES];
48};
49
50/*
51 * Get the Nth GPIO number from the ACPI object.
52 */
53static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
54{
55	struct gpio_desc *desc;
56	int gpio;
57
58	desc = gpiod_get_index(dev, KBUILD_MODNAME, acpi_index, GPIOD_ASIS);
59	if (IS_ERR(desc))
60		return PTR_ERR(desc);
61
62	gpio = desc_to_gpio(desc);
63
64	gpiod_put(desc);
65
66	return gpio;
67}
68
69static struct platform_device *
70soc_button_device_create(struct platform_device *pdev,
71			 const struct soc_button_info *button_info,
72			 bool autorepeat)
73{
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;
81
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);
88
89	gpio_keys = (void *)(gpio_keys_pdata + 1);
90
91	for (info = button_info; info->name; info++) {
92		if (info->autorepeat != autorepeat)
93			continue;
94
95		gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
96		if (gpio < 0)
97			continue;
98
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		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
134err_free_pdev:
135	platform_device_put(pd);
136err_free_mem:
137	devm_kfree(&pdev->dev, gpio_keys_pdata);
138	return ERR_PTR(error);
139}
140
141static int soc_button_remove(struct platform_device *pdev)
142{
143	struct soc_button_data *priv = platform_get_drvdata(pdev);
144
145	int i;
146
147	for (i = 0; i < BUTTON_TYPES; i++)
148		if (priv->children[i])
149			platform_device_unregister(priv->children[i]);
150
151	return 0;
152}
153
154static int soc_button_probe(struct platform_device *pdev)
155{
156	struct device *dev = &pdev->dev;
157	const struct acpi_device_id *id;
158	struct soc_button_info *button_info;
159	struct soc_button_data *priv;
160	struct platform_device *pd;
161	int i;
162	int error;
163
164	id = acpi_match_device(dev->driver->acpi_match_table, dev);
165	if (!id)
166		return -ENODEV;
167
168	button_info = (struct soc_button_info *)id->driver_data;
169
170	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
171	if (!priv)
172		return -ENOMEM;
173
174	platform_set_drvdata(pdev, priv);
175
176	for (i = 0; i < BUTTON_TYPES; i++) {
177		pd = soc_button_device_create(pdev, button_info, i == 0);
178		if (IS_ERR(pd)) {
179			error = PTR_ERR(pd);
180			if (error != -ENODEV) {
181				soc_button_remove(pdev);
182				return error;
183			}
184			continue;
185		}
186
187		priv->children[i] = pd;
188	}
189
190	if (!priv->children[0] && !priv->children[1])
191		return -ENODEV;
192
193	return 0;
194}
195
196static struct soc_button_info soc_button_PNP0C40[] = {
197	{ "power", 0, EV_KEY, KEY_POWER, false, true },
198	{ "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
199	{ "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
200	{ "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
201	{ "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
202	{ }
203};
204
205static const struct acpi_device_id soc_button_acpi_match[] = {
206	{ "PNP0C40", (unsigned long)soc_button_PNP0C40 },
207	{ }
208};
209
210MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
211
212static struct platform_driver soc_button_driver = {
213	.probe          = soc_button_probe,
214	.remove		= soc_button_remove,
215	.driver		= {
216		.name = KBUILD_MODNAME,
217		.acpi_match_table = ACPI_PTR(soc_button_acpi_match),
218	},
219};
220module_platform_driver(soc_button_driver);
221
222MODULE_LICENSE("GPL");
223