Commit 68252638 authored by Alexander Shiyan's avatar Alexander Shiyan Committed by Dmitry Torokhov

Input: gpio_keys_polled - convert to devm-* API

Replace existing resource handling in the driver with managed device
resources, this ensures more consistent error values and simplifies error
handling paths:

kzalloc -> devm_kzalloc
gpio_request_one -> devm_gpio_request_one
input_allocate_polled_device -> devm_input_allocate_polled_device
Signed-off-by: default avatarAlexander Shiyan <shc_work@mail.ru>
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent bf1de976
...@@ -120,12 +120,10 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct ...@@ -120,12 +120,10 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct
if (nbuttons == 0) if (nbuttons == 0)
return NULL; return NULL;
pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button), pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
GFP_KERNEL); GFP_KERNEL);
if (!pdata) { if (!pdata)
error = -ENOMEM; return ERR_PTR(-ENOMEM);
goto err_out;
}
pdata->buttons = (struct gpio_keys_button *)(pdata + 1); pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
pdata->nbuttons = nbuttons; pdata->nbuttons = nbuttons;
...@@ -151,7 +149,7 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct ...@@ -151,7 +149,7 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct
dev_err(dev, dev_err(dev,
"Failed to get gpio flags, error: %d\n", "Failed to get gpio flags, error: %d\n",
error); error);
goto err_free_pdata; return ERR_PTR(error);
} }
button = &pdata->buttons[i++]; button = &pdata->buttons[i++];
...@@ -162,8 +160,7 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct ...@@ -162,8 +160,7 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct
if (of_property_read_u32(pp, "linux,code", &button->code)) { if (of_property_read_u32(pp, "linux,code", &button->code)) {
dev_err(dev, "Button without keycode: 0x%x\n", dev_err(dev, "Button without keycode: 0x%x\n",
button->gpio); button->gpio);
error = -EINVAL; return ERR_PTR(-EINVAL);
goto err_free_pdata;
} }
button->desc = of_get_property(pp, "label", NULL); button->desc = of_get_property(pp, "label", NULL);
...@@ -178,17 +175,10 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct ...@@ -178,17 +175,10 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct
button->debounce_interval = 5; button->debounce_interval = 5;
} }
if (pdata->nbuttons == 0) { if (pdata->nbuttons == 0)
error = -EINVAL; return ERR_PTR(-EINVAL);
goto err_free_pdata;
}
return pdata; return pdata;
err_free_pdata:
kfree(pdata);
err_out:
return ERR_PTR(error);
} }
static const struct of_device_id gpio_keys_polled_of_match[] = { static const struct of_device_id gpio_keys_polled_of_match[] = {
...@@ -213,6 +203,7 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -213,6 +203,7 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
struct gpio_keys_polled_dev *bdev; struct gpio_keys_polled_dev *bdev;
struct input_polled_dev *poll_dev; struct input_polled_dev *poll_dev;
struct input_dev *input; struct input_dev *input;
size_t size;
int error; int error;
int i; int i;
...@@ -228,24 +219,21 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -228,24 +219,21 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
if (!pdata->poll_interval) { if (!pdata->poll_interval) {
dev_err(dev, "missing poll_interval value\n"); dev_err(dev, "missing poll_interval value\n");
error = -EINVAL; return -EINVAL;
goto err_free_pdata;
} }
bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) + size = sizeof(struct gpio_keys_polled_dev) +
pdata->nbuttons * sizeof(struct gpio_keys_button_data), pdata->nbuttons * sizeof(struct gpio_keys_button_data);
GFP_KERNEL); bdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
if (!bdev) { if (!bdev) {
dev_err(dev, "no memory for private data\n"); dev_err(dev, "no memory for private data\n");
error = -ENOMEM; return -ENOMEM;
goto err_free_pdata;
} }
poll_dev = input_allocate_polled_device(); poll_dev = devm_input_allocate_polled_device(&pdev->dev);
if (!poll_dev) { if (!poll_dev) {
dev_err(dev, "no memory for polled device\n"); dev_err(dev, "no memory for polled device\n");
error = -ENOMEM; return -ENOMEM;
goto err_free_bdev;
} }
poll_dev->private = bdev; poll_dev->private = bdev;
...@@ -258,7 +246,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -258,7 +246,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
input->name = pdev->name; input->name = pdev->name;
input->phys = DRV_NAME"/input0"; input->phys = DRV_NAME"/input0";
input->dev.parent = &pdev->dev;
input->id.bustype = BUS_HOST; input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001; input->id.vendor = 0x0001;
...@@ -277,16 +264,15 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -277,16 +264,15 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
if (button->wakeup) { if (button->wakeup) {
dev_err(dev, DRV_NAME " does not support wakeup\n"); dev_err(dev, DRV_NAME " does not support wakeup\n");
error = -EINVAL; return -EINVAL;
goto err_free_gpio;
} }
error = gpio_request_one(gpio, GPIOF_IN, error = devm_gpio_request_one(&pdev->dev, gpio, GPIOF_IN,
button->desc ?: DRV_NAME); button->desc ? : DRV_NAME);
if (error) { if (error) {
dev_err(dev, "unable to claim gpio %u, err=%d\n", dev_err(dev, "unable to claim gpio %u, err=%d\n",
gpio, error); gpio, error);
goto err_free_gpio; return error;
} }
bdata->can_sleep = gpio_cansleep(gpio); bdata->can_sleep = gpio_cansleep(gpio);
...@@ -306,7 +292,7 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -306,7 +292,7 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
if (error) { if (error) {
dev_err(dev, "unable to register polled device, err=%d\n", dev_err(dev, "unable to register polled device, err=%d\n",
error); error);
goto err_free_gpio; return error;
} }
/* report initial state of the buttons */ /* report initial state of the buttons */
...@@ -315,52 +301,10 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) ...@@ -315,52 +301,10 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
&bdev->data[i]); &bdev->data[i]);
return 0; return 0;
err_free_gpio:
while (--i >= 0)
gpio_free(pdata->buttons[i].gpio);
input_free_polled_device(poll_dev);
err_free_bdev:
kfree(bdev);
err_free_pdata:
/* If we have no platform_data, we allocated pdata dynamically. */
if (!dev_get_platdata(&pdev->dev))
kfree(pdata);
return error;
}
static int gpio_keys_polled_remove(struct platform_device *pdev)
{
struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
const struct gpio_keys_platform_data *pdata = bdev->pdata;
int i;
input_unregister_polled_device(bdev->poll_dev);
for (i = 0; i < pdata->nbuttons; i++)
gpio_free(pdata->buttons[i].gpio);
input_free_polled_device(bdev->poll_dev);
/*
* If we had no platform_data, we allocated pdata dynamically and
* must free it here.
*/
if (!dev_get_platdata(&pdev->dev))
kfree(pdata);
kfree(bdev);
return 0;
} }
static struct platform_driver gpio_keys_polled_driver = { static struct platform_driver gpio_keys_polled_driver = {
.probe = gpio_keys_polled_probe, .probe = gpio_keys_polled_probe,
.remove = gpio_keys_polled_remove,
.driver = { .driver = {
.name = DRV_NAME, .name = DRV_NAME,
.owner = THIS_MODULE, .owner = THIS_MODULE,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment