2 * Supports for the button array on SoC tablets originally running
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
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>
23 * Definition of buttons on the tablet. The ACPI index of each button
24 * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
27 #define MAX_NBUTTONS 5
29 struct soc_button_info
{
32 unsigned int event_type
;
33 unsigned int event_code
;
39 * Some of the buttons like volume up/down are auto repeat, while others
40 * are not. To support both, we register two platform devices, and put
41 * buttons into them based on whether the key should be auto repeat.
43 #define BUTTON_TYPES 2
45 struct soc_button_data
{
46 struct platform_device
*children
[BUTTON_TYPES
];
50 * Get the Nth GPIO number from the ACPI object.
52 static int soc_button_lookup_gpio(struct device
*dev
, int acpi_index
)
54 struct gpio_desc
*desc
;
57 desc
= gpiod_get_index(dev
, KBUILD_MODNAME
, acpi_index
, GPIOD_ASIS
);
61 gpio
= desc_to_gpio(desc
);
68 static struct platform_device
*
69 soc_button_device_create(struct platform_device
*pdev
,
70 const struct soc_button_info
*button_info
,
73 const struct soc_button_info
*info
;
74 struct platform_device
*pd
;
75 struct gpio_keys_button
*gpio_keys
;
76 struct gpio_keys_platform_data
*gpio_keys_pdata
;
81 gpio_keys_pdata
= devm_kzalloc(&pdev
->dev
,
82 sizeof(*gpio_keys_pdata
) +
83 sizeof(*gpio_keys
) * MAX_NBUTTONS
,
86 return ERR_PTR(-ENOMEM
);
88 gpio_keys
= (void *)(gpio_keys_pdata
+ 1);
90 for (info
= button_info
; info
->name
; info
++) {
91 if (info
->autorepeat
!= autorepeat
)
94 gpio
= soc_button_lookup_gpio(&pdev
->dev
, info
->acpi_index
);
98 gpio_keys
[n_buttons
].type
= info
->event_type
;
99 gpio_keys
[n_buttons
].code
= info
->event_code
;
100 gpio_keys
[n_buttons
].gpio
= gpio
;
101 gpio_keys
[n_buttons
].active_low
= 1;
102 gpio_keys
[n_buttons
].desc
= info
->name
;
103 gpio_keys
[n_buttons
].wakeup
= info
->wakeup
;
107 if (n_buttons
== 0) {
112 gpio_keys_pdata
->buttons
= gpio_keys
;
113 gpio_keys_pdata
->nbuttons
= n_buttons
;
114 gpio_keys_pdata
->rep
= autorepeat
;
116 pd
= platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO
);
122 error
= platform_device_add_data(pd
, gpio_keys_pdata
,
123 sizeof(*gpio_keys_pdata
));
127 error
= platform_device_add(pd
);
134 platform_device_put(pd
);
136 devm_kfree(&pdev
->dev
, gpio_keys_pdata
);
137 return ERR_PTR(error
);
140 static int soc_button_remove(struct platform_device
*pdev
)
142 struct soc_button_data
*priv
= platform_get_drvdata(pdev
);
146 for (i
= 0; i
< BUTTON_TYPES
; i
++)
147 if (priv
->children
[i
])
148 platform_device_unregister(priv
->children
[i
]);
153 static int soc_button_probe(struct platform_device
*pdev
)
155 struct device
*dev
= &pdev
->dev
;
156 const struct acpi_device_id
*id
;
157 struct soc_button_info
*button_info
;
158 struct soc_button_data
*priv
;
159 struct platform_device
*pd
;
163 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
167 button_info
= (struct soc_button_info
*)id
->driver_data
;
169 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
173 platform_set_drvdata(pdev
, priv
);
175 for (i
= 0; i
< BUTTON_TYPES
; i
++) {
176 pd
= soc_button_device_create(pdev
, button_info
, i
== 0);
179 if (error
!= -ENODEV
) {
180 soc_button_remove(pdev
);
186 priv
->children
[i
] = pd
;
189 if (!priv
->children
[0] && !priv
->children
[1])
195 static struct soc_button_info soc_button_PNP0C40
[] = {
196 { "power", 0, EV_KEY
, KEY_POWER
, false, true },
197 { "home", 1, EV_KEY
, KEY_LEFTMETA
, false, true },
198 { "volume_up", 2, EV_KEY
, KEY_VOLUMEUP
, true, false },
199 { "volume_down", 3, EV_KEY
, KEY_VOLUMEDOWN
, true, false },
200 { "rotation_lock", 4, EV_SW
, SW_ROTATE_LOCK
, false, false },
204 static const struct acpi_device_id soc_button_acpi_match
[] = {
205 { "PNP0C40", (unsigned long)soc_button_PNP0C40
},
209 MODULE_DEVICE_TABLE(acpi
, soc_button_acpi_match
);
211 static struct platform_driver soc_button_driver
= {
212 .probe
= soc_button_probe
,
213 .remove
= soc_button_remove
,
215 .name
= KBUILD_MODNAME
,
216 .acpi_match_table
= ACPI_PTR(soc_button_acpi_match
),
219 module_platform_driver(soc_button_driver
);
221 MODULE_LICENSE("GPL");