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/pnp.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 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; 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 46 struct soc_button_data { 47 struct platform_device *children[BUTTON_TYPES]; 48 }; 49 50 /* 51 * Get the Nth GPIO number from the ACPI object. 52 */ 53 static 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); 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 69 static struct platform_device * 70 soc_button_device_create(struct pnp_dev *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 gpio_keys = (void *)(gpio_keys_pdata + 1); 87 88 for (info = button_info; info->name; info++) { 89 if (info->autorepeat != autorepeat) 90 continue; 91 92 gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index); 93 if (gpio < 0) 94 continue; 95 96 gpio_keys[n_buttons].type = info->event_type; 97 gpio_keys[n_buttons].code = info->event_code; 98 gpio_keys[n_buttons].gpio = gpio; 99 gpio_keys[n_buttons].active_low = 1; 100 gpio_keys[n_buttons].desc = info->name; 101 gpio_keys[n_buttons].wakeup = info->wakeup; 102 n_buttons++; 103 } 104 105 if (n_buttons == 0) { 106 error = -ENODEV; 107 goto err_free_mem; 108 } 109 110 gpio_keys_pdata->buttons = gpio_keys; 111 gpio_keys_pdata->nbuttons = n_buttons; 112 gpio_keys_pdata->rep = autorepeat; 113 114 pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO); 115 if (!pd) { 116 error = -ENOMEM; 117 goto err_free_mem; 118 } 119 120 error = platform_device_add_data(pd, gpio_keys_pdata, 121 sizeof(*gpio_keys_pdata)); 122 if (error) 123 goto err_free_pdev; 124 125 error = platform_device_add(pd); 126 if (error) 127 goto err_free_pdev; 128 129 return pd; 130 131 err_free_pdev: 132 platform_device_put(pd); 133 err_free_mem: 134 devm_kfree(&pdev->dev, gpio_keys_pdata); 135 return ERR_PTR(error); 136 } 137 138 static void soc_button_remove(struct pnp_dev *pdev) 139 { 140 struct soc_button_data *priv = pnp_get_drvdata(pdev); 141 int i; 142 143 for (i = 0; i < BUTTON_TYPES; i++) 144 if (priv->children[i]) 145 platform_device_unregister(priv->children[i]); 146 } 147 148 static int soc_button_pnp_probe(struct pnp_dev *pdev, 149 const struct pnp_device_id *id) 150 { 151 const struct soc_button_info *button_info = (void *)id->driver_data; 152 struct soc_button_data *priv; 153 struct platform_device *pd; 154 int i; 155 int error; 156 157 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 158 if (!priv) 159 return -ENOMEM; 160 161 pnp_set_drvdata(pdev, priv); 162 163 for (i = 0; i < BUTTON_TYPES; i++) { 164 pd = soc_button_device_create(pdev, button_info, i == 0); 165 if (IS_ERR(pd)) { 166 error = PTR_ERR(pd); 167 if (error != -ENODEV) { 168 soc_button_remove(pdev); 169 return error; 170 } 171 continue; 172 } 173 174 priv->children[i] = pd; 175 } 176 177 if (!priv->children[0] && !priv->children[1]) 178 return -ENODEV; 179 180 return 0; 181 } 182 183 static struct soc_button_info soc_button_PNP0C40[] = { 184 { "power", 0, EV_KEY, KEY_POWER, false, true }, 185 { "home", 1, EV_KEY, KEY_HOME, false, true }, 186 { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false }, 187 { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false }, 188 { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false }, 189 { } 190 }; 191 192 static const struct pnp_device_id soc_button_pnp_match[] = { 193 { .id = "PNP0C40", .driver_data = (long)soc_button_PNP0C40 }, 194 { .id = "" } 195 }; 196 MODULE_DEVICE_TABLE(pnp, soc_button_pnp_match); 197 198 static struct pnp_driver soc_button_pnp_driver = { 199 .name = KBUILD_MODNAME, 200 .id_table = soc_button_pnp_match, 201 .probe = soc_button_pnp_probe, 202 .remove = soc_button_remove, 203 }; 204 205 static int __init soc_button_init(void) 206 { 207 return pnp_register_driver(&soc_button_pnp_driver); 208 } 209 210 static void __exit soc_button_exit(void) 211 { 212 pnp_unregister_driver(&soc_button_pnp_driver); 213 } 214 215 module_init(soc_button_init); 216 module_exit(soc_button_exit); 217 218 MODULE_LICENSE("GPL"); 219