Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: gpio_keys_polled - keep button data constant

Commit 633a21d80b4a ("input: gpio_keys_polled: Add support for GPIO
descriptors") placed gpio descriptor into gpio_keys_button structure, which
is supposed to be part of platform data and not modifiable by the driver.
To keep the data constant, let's move the descriptor to
gpio_keys_button_data structure instead.

Tested-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+64 -56
+5 -5
drivers/input/keyboard/gpio_keys.c
··· 624 624 struct gpio_keys_button *button; 625 625 int error; 626 626 int nbuttons; 627 - int i; 628 627 629 628 node = dev->of_node; 630 629 if (!node) ··· 639 640 if (!pdata) 640 641 return ERR_PTR(-ENOMEM); 641 642 642 - pdata->buttons = (struct gpio_keys_button *)(pdata + 1); 643 + button = (struct gpio_keys_button *)(pdata + 1); 644 + 645 + pdata->buttons = button; 643 646 pdata->nbuttons = nbuttons; 644 647 645 648 pdata->rep = !!of_get_property(node, "autorepeat", NULL); 646 649 647 650 of_property_read_string(node, "label", &pdata->name); 648 651 649 - i = 0; 650 652 for_each_available_child_of_node(node, pp) { 651 653 enum of_gpio_flags flags; 652 - 653 - button = &pdata->buttons[i++]; 654 654 655 655 button->gpio = of_get_gpio_flags(pp, 0, &flags); 656 656 if (button->gpio < 0) { ··· 692 694 if (of_property_read_u32(pp, "debounce-interval", 693 695 &button->debounce_interval)) 694 696 button->debounce_interval = 5; 697 + 698 + button++; 695 699 } 696 700 697 701 if (pdata->nbuttons == 0)
+58 -47
drivers/input/keyboard/gpio_keys_polled.c
··· 30 30 #define DRV_NAME "gpio-keys-polled" 31 31 32 32 struct gpio_keys_button_data { 33 + struct gpio_desc *gpiod; 33 34 int last_state; 34 35 int count; 35 36 int threshold; ··· 47 46 }; 48 47 49 48 static void gpio_keys_button_event(struct input_polled_dev *dev, 50 - struct gpio_keys_button *button, 49 + const struct gpio_keys_button *button, 51 50 int state) 52 51 { 53 52 struct gpio_keys_polled_dev *bdev = dev->private; ··· 71 70 } 72 71 73 72 static void gpio_keys_polled_check_state(struct input_polled_dev *dev, 74 - struct gpio_keys_button *button, 73 + const struct gpio_keys_button *button, 75 74 struct gpio_keys_button_data *bdata) 76 75 { 77 76 int state; 78 77 79 78 if (bdata->can_sleep) 80 - state = !!gpiod_get_value_cansleep(button->gpiod); 79 + state = !!gpiod_get_value_cansleep(bdata->gpiod); 81 80 else 82 - state = !!gpiod_get_value(button->gpiod); 81 + state = !!gpiod_get_value(bdata->gpiod); 83 82 84 83 gpio_keys_button_event(dev, button, state); 85 84 ··· 143 142 pdata->disable(bdev->dev); 144 143 } 145 144 146 - static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev) 145 + static struct gpio_keys_platform_data * 146 + gpio_keys_polled_get_devtree_pdata(struct device *dev) 147 147 { 148 148 struct gpio_keys_platform_data *pdata; 149 149 struct gpio_keys_button *button; 150 150 struct fwnode_handle *child; 151 - int error; 152 151 int nbuttons; 153 152 154 153 nbuttons = device_get_child_node_count(dev); 155 154 if (nbuttons == 0) 156 - return NULL; 155 + return ERR_PTR(-EINVAL); 157 156 158 157 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button), 159 158 GFP_KERNEL); 160 159 if (!pdata) 161 160 return ERR_PTR(-ENOMEM); 162 161 163 - pdata->buttons = (struct gpio_keys_button *)(pdata + 1); 162 + button = (struct gpio_keys_button *)(pdata + 1); 163 + 164 + pdata->buttons = button; 165 + pdata->nbuttons = nbuttons; 164 166 165 167 pdata->rep = device_property_present(dev, "autorepeat"); 166 168 device_property_read_u32(dev, "poll-interval", &pdata->poll_interval); 167 169 168 170 device_for_each_child_node(dev, child) { 169 - struct gpio_desc *desc; 170 - 171 - desc = devm_get_gpiod_from_child(dev, NULL, child); 172 - if (IS_ERR(desc)) { 173 - error = PTR_ERR(desc); 174 - if (error != -EPROBE_DEFER) 175 - dev_err(dev, 176 - "Failed to get gpio flags, error: %d\n", 177 - error); 178 - fwnode_handle_put(child); 179 - return ERR_PTR(error); 180 - } 181 - 182 - button = &pdata->buttons[pdata->nbuttons++]; 183 - button->gpiod = desc; 184 - 185 - if (fwnode_property_read_u32(child, "linux,code", &button->code)) { 186 - dev_err(dev, "Button without keycode: %d\n", 187 - pdata->nbuttons - 1); 171 + if (fwnode_property_read_u32(child, "linux,code", 172 + &button->code)) { 173 + dev_err(dev, "button without keycode\n"); 188 174 fwnode_handle_put(child); 189 175 return ERR_PTR(-EINVAL); 190 176 } ··· 194 206 if (fwnode_property_read_u32(child, "debounce-interval", 195 207 &button->debounce_interval)) 196 208 button->debounce_interval = 5; 197 - } 198 209 199 - if (pdata->nbuttons == 0) 200 - return ERR_PTR(-EINVAL); 210 + button++; 211 + } 201 212 202 213 return pdata; 203 214 } ··· 207 220 int i, min = 0, max = 0; 208 221 209 222 for (i = 0; i < pdata->nbuttons; i++) { 210 - struct gpio_keys_button *button = &pdata->buttons[i]; 223 + const struct gpio_keys_button *button = &pdata->buttons[i]; 211 224 212 225 if (button->type != EV_ABS || button->code != code) 213 226 continue; ··· 217 230 if (button->value > max) 218 231 max = button->value; 219 232 } 233 + 220 234 input_set_abs_params(input, code, min, max, 0, 0); 221 235 } 222 236 ··· 230 242 static int gpio_keys_polled_probe(struct platform_device *pdev) 231 243 { 232 244 struct device *dev = &pdev->dev; 245 + struct fwnode_handle *child = NULL; 233 246 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev); 234 247 struct gpio_keys_polled_dev *bdev; 235 248 struct input_polled_dev *poll_dev; ··· 243 254 pdata = gpio_keys_polled_get_devtree_pdata(dev); 244 255 if (IS_ERR(pdata)) 245 256 return PTR_ERR(pdata); 246 - if (!pdata) { 247 - dev_err(dev, "missing platform data\n"); 248 - return -EINVAL; 249 - } 250 257 } 251 258 252 259 if (!pdata->poll_interval) { ··· 285 300 __set_bit(EV_REP, input->evbit); 286 301 287 302 for (i = 0; i < pdata->nbuttons; i++) { 288 - struct gpio_keys_button *button = &pdata->buttons[i]; 303 + const struct gpio_keys_button *button = &pdata->buttons[i]; 289 304 struct gpio_keys_button_data *bdata = &bdev->data[i]; 290 305 unsigned int type = button->type ?: EV_KEY; 291 306 292 307 if (button->wakeup) { 293 308 dev_err(dev, DRV_NAME " does not support wakeup\n"); 309 + fwnode_handle_put(child); 294 310 return -EINVAL; 295 311 } 296 312 297 - /* 298 - * Legacy GPIO number so request the GPIO here and 299 - * convert it to descriptor. 300 - */ 301 - if (!button->gpiod && gpio_is_valid(button->gpio)) { 313 + if (!dev_get_platdata(dev)) { 314 + /* No legacy static platform data */ 315 + child = device_get_next_child_node(dev, child); 316 + if (!child) { 317 + dev_err(dev, "missing child device node\n"); 318 + return -EINVAL; 319 + } 320 + 321 + bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, 322 + child); 323 + if (IS_ERR(bdata->gpiod)) { 324 + error = PTR_ERR(bdata->gpiod); 325 + if (error != -EPROBE_DEFER) 326 + dev_err(dev, 327 + "failed to get gpio: %d\n", 328 + error); 329 + fwnode_handle_put(child); 330 + return error; 331 + } 332 + } else if (gpio_is_valid(button->gpio)) { 333 + /* 334 + * Legacy GPIO number so request the GPIO here and 335 + * convert it to descriptor. 336 + */ 302 337 unsigned flags = GPIOF_IN; 303 338 304 339 if (button->active_low) ··· 327 322 error = devm_gpio_request_one(&pdev->dev, button->gpio, 328 323 flags, button->desc ? : DRV_NAME); 329 324 if (error) { 330 - dev_err(dev, "unable to claim gpio %u, err=%d\n", 325 + dev_err(dev, 326 + "unable to claim gpio %u, err=%d\n", 331 327 button->gpio, error); 332 328 return error; 333 329 } 334 330 335 - button->gpiod = gpio_to_desc(button->gpio); 331 + bdata->gpiod = gpio_to_desc(button->gpio); 332 + if (!bdata->gpiod) { 333 + dev_err(dev, 334 + "unable to convert gpio %u to descriptor\n", 335 + button->gpio); 336 + return -EINVAL; 337 + } 336 338 } 337 339 338 - if (IS_ERR(button->gpiod)) 339 - return PTR_ERR(button->gpiod); 340 - 341 - bdata->can_sleep = gpiod_cansleep(button->gpiod); 340 + bdata->can_sleep = gpiod_cansleep(bdata->gpiod); 342 341 bdata->last_state = -1; 343 342 bdata->threshold = DIV_ROUND_UP(button->debounce_interval, 344 343 pdata->poll_interval); ··· 352 343 gpio_keys_polled_set_abs_params(input, pdata, 353 344 button->code); 354 345 } 346 + 347 + fwnode_handle_put(child); 355 348 356 349 bdev->poll_dev = poll_dev; 357 350 bdev->dev = dev;
+1 -4
include/linux/gpio_keys.h
··· 2 2 #define _GPIO_KEYS_H 3 3 4 4 struct device; 5 - struct gpio_desc; 6 5 7 6 /** 8 7 * struct gpio_keys_button - configuration parameters ··· 17 18 * disable button via sysfs 18 19 * @value: axis value for %EV_ABS 19 20 * @irq: Irq number in case of interrupt keys 20 - * @gpiod: GPIO descriptor 21 21 */ 22 22 struct gpio_keys_button { 23 23 unsigned int code; ··· 29 31 bool can_disable; 30 32 int value; 31 33 unsigned int irq; 32 - struct gpio_desc *gpiod; 33 34 }; 34 35 35 36 /** ··· 43 46 * @name: input device name 44 47 */ 45 48 struct gpio_keys_platform_data { 46 - struct gpio_keys_button *buttons; 49 + const struct gpio_keys_button *buttons; 47 50 int nbuttons; 48 51 unsigned int poll_interval; 49 52 unsigned int rep:1;