Commit 219edc71 authored by Alexandre Pereira da Silva's avatar Alexandre Pereira da Silva Committed by Dmitry Torokhov

Input: gpio_keys - clean up device tree parser

- fix sizeof in memset;
- clean up dt properties extraction;
- use for_each_chil_of_node macro;
- use of_get_child_count();
- use of_match_ptr macro.
Signed-off-by: default avatarAlexandre Pereira da Silva <aletes.xgr@gmail.com>
Acked-by: default avatarRob Herring <rob.herring@calxeda.com>
[Fabio Estevam <fabio.estevam@freescale.com>: fix fix NULL pointer
dereference for dt case - pdata->buttons wasn't initialized]
Signed-off-by: default avatarDmitry Torokhov <dtor@mail.ru>
parent c0394506
...@@ -43,11 +43,9 @@ struct gpio_button_data { ...@@ -43,11 +43,9 @@ struct gpio_button_data {
}; };
struct gpio_keys_drvdata { struct gpio_keys_drvdata {
const struct gpio_keys_platform_data *pdata;
struct input_dev *input; struct input_dev *input;
struct mutex disable_lock; struct mutex disable_lock;
unsigned int n_buttons;
int (*enable)(struct device *dev);
void (*disable)(struct device *dev);
struct gpio_button_data data[0]; struct gpio_button_data data[0];
}; };
...@@ -171,7 +169,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata, ...@@ -171,7 +169,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
if (!bits) if (!bits)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < ddata->n_buttons; i++) { for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i]; struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->type != type) if (bdata->button->type != type)
...@@ -219,7 +217,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, ...@@ -219,7 +217,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
goto out; goto out;
/* First validate */ /* First validate */
for (i = 0; i < ddata->n_buttons; i++) { for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i]; struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->type != type) if (bdata->button->type != type)
...@@ -234,7 +232,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, ...@@ -234,7 +232,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
mutex_lock(&ddata->disable_lock); mutex_lock(&ddata->disable_lock);
for (i = 0; i < ddata->n_buttons; i++) { for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i]; struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->type != type) if (bdata->button->type != type)
...@@ -523,56 +521,64 @@ static int __devinit gpio_keys_setup_key(struct platform_device *pdev, ...@@ -523,56 +521,64 @@ static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
static int gpio_keys_open(struct input_dev *input) static int gpio_keys_open(struct input_dev *input)
{ {
struct gpio_keys_drvdata *ddata = input_get_drvdata(input); struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
const struct gpio_keys_platform_data *pdata = ddata->pdata;
return ddata->enable ? ddata->enable(input->dev.parent) : 0; return pdata->enable ? pdata->enable(input->dev.parent) : 0;
} }
static void gpio_keys_close(struct input_dev *input) static void gpio_keys_close(struct input_dev *input)
{ {
struct gpio_keys_drvdata *ddata = input_get_drvdata(input); struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
const struct gpio_keys_platform_data *pdata = ddata->pdata;
if (ddata->disable) if (pdata->disable)
ddata->disable(input->dev.parent); pdata->disable(input->dev.parent);
} }
/* /*
* Handlers for alternative sources of platform_data * Handlers for alternative sources of platform_data
*/ */
#ifdef CONFIG_OF #ifdef CONFIG_OF
/* /*
* Translate OpenFirmware node properties into platform_data * Translate OpenFirmware node properties into platform_data
*/ */
static int gpio_keys_get_devtree_pdata(struct device *dev, static struct gpio_keys_platform_data * __devinit
struct gpio_keys_platform_data *pdata) gpio_keys_get_devtree_pdata(struct device *dev)
{ {
struct device_node *node, *pp; struct device_node *node, *pp;
struct gpio_keys_platform_data *pdata;
struct gpio_keys_button *button;
int error;
int nbuttons;
int i; int i;
struct gpio_keys_button *buttons;
u32 reg;
node = dev->of_node; node = dev->of_node;
if (node == NULL) if (!node) {
return -ENODEV; error = -ENODEV;
goto err_out;
memset(pdata, 0, sizeof *pdata); }
pdata->rep = !!of_get_property(node, "autorepeat", NULL); nbuttons = of_get_child_count(node);
if (nbuttons == 0) {
error = -ENODEV;
goto err_out;
}
/* First count the subnodes */ pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
pp = NULL; GFP_KERNEL);
while ((pp = of_get_next_child(node, pp))) if (!pdata) {
pdata->nbuttons++; error = -ENOMEM;
goto err_out;
}
if (pdata->nbuttons == 0) pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
return -ENODEV; pdata->nbuttons = nbuttons;
buttons = kzalloc(pdata->nbuttons * (sizeof *buttons), GFP_KERNEL); pdata->rep = !!of_get_property(node, "autorepeat", NULL);
if (!buttons)
return -ENOMEM;
pp = NULL;
i = 0; i = 0;
while ((pp = of_get_next_child(node, pp))) { for_each_child_of_node(node, pp) {
enum of_gpio_flags flags; enum of_gpio_flags flags;
if (!of_find_property(pp, "gpios", NULL)) { if (!of_find_property(pp, "gpios", NULL)) {
...@@ -580,39 +586,42 @@ static int gpio_keys_get_devtree_pdata(struct device *dev, ...@@ -580,39 +586,42 @@ static int gpio_keys_get_devtree_pdata(struct device *dev,
dev_warn(dev, "Found button without gpios\n"); dev_warn(dev, "Found button without gpios\n");
continue; continue;
} }
buttons[i].gpio = of_get_gpio_flags(pp, 0, &flags);
buttons[i].active_low = flags & OF_GPIO_ACTIVE_LOW;
if (of_property_read_u32(pp, "linux,code", &reg)) { button = &pdata->buttons[i++];
dev_err(dev, "Button without keycode: 0x%x\n", buttons[i].gpio);
goto out_fail;
}
buttons[i].code = reg;
buttons[i].desc = of_get_property(pp, "label", NULL); button->gpio = of_get_gpio_flags(pp, 0, &flags);
button->active_low = flags & OF_GPIO_ACTIVE_LOW;
if (of_property_read_u32(pp, "linux,input-type", &reg) == 0) if (of_property_read_u32(pp, "linux,code", &button->code)) {
buttons[i].type = reg; dev_err(dev, "Button without keycode: 0x%x\n",
else button->gpio);
buttons[i].type = EV_KEY; error = -EINVAL;
goto err_free_pdata;
}
buttons[i].wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL); button->desc = of_get_property(pp, "label", NULL);
if (of_property_read_u32(pp, "debounce-interval", &reg) == 0) if (of_property_read_u32(pp, "linux,input-type", &button->type))
buttons[i].debounce_interval = reg; button->type = EV_KEY;
else
buttons[i].debounce_interval = 5; button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
i++; if (of_property_read_u32(pp, "debounce-interval",
&button->debounce_interval))
button->debounce_interval = 5;
} }
pdata->buttons = buttons; if (pdata->nbuttons == 0) {
error = -EINVAL;
goto err_free_pdata;
}
return 0; return pdata;
out_fail: err_free_pdata:
kfree(buttons); kfree(pdata);
return -ENODEV; err_out:
return ERR_PTR(error);
} }
static struct of_device_id gpio_keys_of_match[] = { static struct of_device_id gpio_keys_of_match[] = {
...@@ -623,14 +632,12 @@ MODULE_DEVICE_TABLE(of, gpio_keys_of_match); ...@@ -623,14 +632,12 @@ MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
#else #else
static int gpio_keys_get_devtree_pdata(struct device *dev, static inline struct gpio_keys_platform_data *
struct gpio_keys_platform_data *altp) gpio_keys_get_devtree_pdata(struct device *dev)
{ {
return -ENODEV; return ERR_PTR(-ENODEV);
} }
#define gpio_keys_of_match NULL
#endif #endif
static void gpio_remove_key(struct gpio_button_data *bdata) static void gpio_remove_key(struct gpio_button_data *bdata)
...@@ -645,19 +652,17 @@ static void gpio_remove_key(struct gpio_button_data *bdata) ...@@ -645,19 +652,17 @@ static void gpio_remove_key(struct gpio_button_data *bdata)
static int __devinit gpio_keys_probe(struct platform_device *pdev) static int __devinit gpio_keys_probe(struct platform_device *pdev)
{ {
const struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
struct gpio_keys_drvdata *ddata;
struct device *dev = &pdev->dev; struct device *dev = &pdev->dev;
struct gpio_keys_platform_data alt_pdata; const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
struct gpio_keys_drvdata *ddata;
struct input_dev *input; struct input_dev *input;
int i, error; int i, error;
int wakeup = 0; int wakeup = 0;
if (!pdata) { if (!pdata) {
error = gpio_keys_get_devtree_pdata(dev, &alt_pdata); pdata = gpio_keys_get_devtree_pdata(dev);
if (error) if (IS_ERR(pdata))
return error; return PTR_ERR(pdata);
pdata = &alt_pdata;
} }
ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
...@@ -670,10 +675,8 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev) ...@@ -670,10 +675,8 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
goto fail1; goto fail1;
} }
ddata->pdata = pdata;
ddata->input = input; ddata->input = input;
ddata->n_buttons = pdata->nbuttons;
ddata->enable = pdata->enable;
ddata->disable = pdata->disable;
mutex_init(&ddata->disable_lock); mutex_init(&ddata->disable_lock);
platform_set_drvdata(pdev, ddata); platform_set_drvdata(pdev, ddata);
...@@ -742,9 +745,9 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev) ...@@ -742,9 +745,9 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
fail1: fail1:
input_free_device(input); input_free_device(input);
kfree(ddata); kfree(ddata);
/* If we have no platform_data, we allocated buttons dynamically. */ /* If we have no platform data, we allocated pdata dynamically. */
if (!pdev->dev.platform_data) if (!dev_get_platdata(&pdev->dev))
kfree(pdata->buttons); kfree(pdata);
return error; return error;
} }
...@@ -759,18 +762,14 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev) ...@@ -759,18 +762,14 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev)
device_init_wakeup(&pdev->dev, 0); device_init_wakeup(&pdev->dev, 0);
for (i = 0; i < ddata->n_buttons; i++) for (i = 0; i < ddata->pdata->nbuttons; i++)
gpio_remove_key(&ddata->data[i]); gpio_remove_key(&ddata->data[i]);
input_unregister_device(input); input_unregister_device(input);
/* /* If we have no platform data, we allocated pdata dynamically. */
* If we had no platform_data, we allocated buttons dynamically, and if (!dev_get_platdata(&pdev->dev))
* must free them here. ddata->data[0].button is the pointer to the kfree(ddata->pdata);
* beginning of the allocated array.
*/
if (!pdev->dev.platform_data)
kfree(ddata->data[0].button);
kfree(ddata); kfree(ddata);
...@@ -784,7 +783,7 @@ static int gpio_keys_suspend(struct device *dev) ...@@ -784,7 +783,7 @@ static int gpio_keys_suspend(struct device *dev)
int i; int i;
if (device_may_wakeup(dev)) { if (device_may_wakeup(dev)) {
for (i = 0; i < ddata->n_buttons; i++) { for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i]; struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->wakeup) if (bdata->button->wakeup)
enable_irq_wake(bdata->irq); enable_irq_wake(bdata->irq);
...@@ -799,7 +798,7 @@ static int gpio_keys_resume(struct device *dev) ...@@ -799,7 +798,7 @@ static int gpio_keys_resume(struct device *dev)
struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev); struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
int i; int i;
for (i = 0; i < ddata->n_buttons; i++) { for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i]; struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->wakeup && device_may_wakeup(dev)) if (bdata->button->wakeup && device_may_wakeup(dev))
disable_irq_wake(bdata->irq); disable_irq_wake(bdata->irq);
...@@ -822,7 +821,7 @@ static struct platform_driver gpio_keys_device_driver = { ...@@ -822,7 +821,7 @@ static struct platform_driver gpio_keys_device_driver = {
.name = "gpio-keys", .name = "gpio-keys",
.owner = THIS_MODULE, .owner = THIS_MODULE,
.pm = &gpio_keys_pm_ops, .pm = &gpio_keys_pm_ops,
.of_match_table = gpio_keys_of_match, .of_match_table = of_match_ptr(gpio_keys_of_match),
} }
}; };
......
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