* [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
@ 2012-07-25 7:43 Daniel Mack
2012-07-25 7:43 ` [PATCH v2 2/2] input: rotary-encoder: add DT bindings Daniel Mack
2012-07-25 16:05 ` [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Dmitry Torokhov
0 siblings, 2 replies; 7+ messages in thread
From: Daniel Mack @ 2012-07-25 7:43 UTC (permalink / raw)
To: linux-input; +Cc: dmitry.torokhov, jhovold, hartleys, Daniel Mack
This simplifies the error unwind pathes.
Also, don't call gpio_to_irq() on GPIOs before dev_gpio_request_one()
succeeded on them. This avoids Ooopses with incorrect DT bindings.
Signed-off-by: Daniel Mack <zonque@gmail.com>
---
drivers/input/misc/rotary_encoder.c | 67 +++++++++++------------------------
1 file changed, 21 insertions(+), 46 deletions(-)
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index f07f784..d4e0abb 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -145,6 +145,7 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
struct rotary_encoder *encoder;
struct input_dev *input;
+ struct device *dev = &pdev->dev;
irq_handler_t handler;
int err;
@@ -163,13 +164,11 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
encoder->input = input;
encoder->pdata = pdata;
- encoder->irq_a = gpio_to_irq(pdata->gpio_a);
- encoder->irq_b = gpio_to_irq(pdata->gpio_b);
/* create and register the input driver */
input->name = pdev->name;
input->id.bustype = BUS_HOST;
- input->dev.parent = &pdev->dev;
+ input->dev.parent = dev;
if (pdata->relative_axis) {
input->evbit[0] = BIT_MASK(EV_REL);
@@ -182,38 +181,27 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
err = input_register_device(input);
if (err) {
- dev_err(&pdev->dev, "failed to register input device\n");
+ dev_err(dev, "failed to register input device\n");
goto exit_free_mem;
}
/* request the GPIOs */
- err = gpio_request(pdata->gpio_a, DRV_NAME);
+ err = devm_gpio_request_one(dev, pdata->gpio_a,
+ GPIOF_IN, dev_name(dev));
if (err) {
- dev_err(&pdev->dev, "unable to request GPIO %d\n",
- pdata->gpio_a);
+ dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
goto exit_unregister_input;
}
- err = gpio_direction_input(pdata->gpio_a);
+ err = devm_gpio_request_one(dev, pdata->gpio_b,
+ GPIOF_IN, dev_name(dev));
if (err) {
- dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
- pdata->gpio_a);
+ dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
goto exit_unregister_input;
}
- err = gpio_request(pdata->gpio_b, DRV_NAME);
- if (err) {
- dev_err(&pdev->dev, "unable to request GPIO %d\n",
- pdata->gpio_b);
- goto exit_free_gpio_a;
- }
-
- err = gpio_direction_input(pdata->gpio_b);
- if (err) {
- dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
- pdata->gpio_b);
- goto exit_free_gpio_a;
- }
+ encoder->irq_a = gpio_to_irq(pdata->gpio_a);
+ encoder->irq_b = gpio_to_irq(pdata->gpio_b);
/* request the IRQs */
if (pdata->half_period) {
@@ -223,34 +211,26 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
handler = &rotary_encoder_irq;
}
- err = request_irq(encoder->irq_a, handler,
- IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
- DRV_NAME, encoder);
+ err = devm_request_irq(dev, encoder->irq_a, handler,
+ IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
+ dev_name(dev), encoder);
if (err) {
- dev_err(&pdev->dev, "unable to request IRQ %d\n",
- encoder->irq_a);
- goto exit_free_gpio_b;
+ dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a);
+ goto exit_unregister_input;
}
- err = request_irq(encoder->irq_b, handler,
- IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
- DRV_NAME, encoder);
+ err = devm_request_irq(dev, encoder->irq_b, handler,
+ IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
+ dev_name(dev), encoder);
if (err) {
- dev_err(&pdev->dev, "unable to request IRQ %d\n",
- encoder->irq_b);
- goto exit_free_irq_a;
+ dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b);
+ goto exit_unregister_input;
}
platform_set_drvdata(pdev, encoder);
return 0;
-exit_free_irq_a:
- free_irq(encoder->irq_a, encoder);
-exit_free_gpio_b:
- gpio_free(pdata->gpio_b);
-exit_free_gpio_a:
- gpio_free(pdata->gpio_a);
exit_unregister_input:
input_unregister_device(input);
input = NULL; /* so we don't try to free it */
@@ -263,12 +243,7 @@ exit_free_mem:
static int __devexit rotary_encoder_remove(struct platform_device *pdev)
{
struct rotary_encoder *encoder = platform_get_drvdata(pdev);
- struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
- free_irq(encoder->irq_a, encoder);
- free_irq(encoder->irq_b, encoder);
- gpio_free(pdata->gpio_a);
- gpio_free(pdata->gpio_b);
input_unregister_device(encoder->input);
platform_set_drvdata(pdev, NULL);
kfree(encoder);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] input: rotary-encoder: add DT bindings
2012-07-25 7:43 [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Daniel Mack
@ 2012-07-25 7:43 ` Daniel Mack
2012-07-25 16:05 ` [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Dmitry Torokhov
1 sibling, 0 replies; 7+ messages in thread
From: Daniel Mack @ 2012-07-25 7:43 UTC (permalink / raw)
To: linux-input; +Cc: dmitry.torokhov, jhovold, hartleys, Daniel Mack
This adds devicetree bindings to the rotary encoder driver and some
documenation about how to use them. Tested on a PXA3xx platform.
Signed-off-by: Daniel Mack <zonque@gmail.com>
---
.../devicetree/bindings/input/rotary-encoder.txt | 37 ++++++++++++
drivers/input/misc/rotary_encoder.c | 63 +++++++++++++++++++-
2 files changed, 99 insertions(+), 1 deletion(-)
create mode 100644 Documentation/devicetree/bindings/input/rotary-encoder.txt
diff --git a/Documentation/devicetree/bindings/input/rotary-encoder.txt b/Documentation/devicetree/bindings/input/rotary-encoder.txt
new file mode 100644
index 0000000..dd1f634
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/rotary-encoder.txt
@@ -0,0 +1,37 @@
+Rotary encoder DT bindings
+
+Required properties:
+- gpios: a spec for two GPIOs to be used
+
+Optional properties:
+- linux,axis: the input subsystem axis to map to this rotary encoder.
+ Defaults to 0 (ABS_X / REL_X)
+- rotary-encoder,steps: Number of steps in a full turnaround of the
+ encoder. Only relevant for absolute axis. Defaults to 24 which is a
+ typical value for such devices.
+- rotary-encoder,relative-axis: register a relative axis rather than an
+ absolute one. Relative axis will only generate +1/-1 events on the input
+ device, hence no steps need to be passed.
+- rotary-encoder,rollover: Automatic rollove when the rotary value becomes
+ greater than the specified steps or smaller than 0. For absolute axis only.
+- rotary-encoder,half-period: Makes the driver work on half-period mode.
+
+See Documentation/input/rotary-encoder.txt for more information.
+
+Example:
+
+ rotary@0 {
+ compatible = "rotary-encoder";
+ gpios = <&gpio 19 1>, <&gpio 20 0>; /* GPIO19 is inverted */
+ linux,axis = <0>; /* REL_X */
+ rotary-encoder,relative-axis;
+ };
+
+ rotary@1 {
+ compatible = "rotary-encoder";
+ gpios = <&gpio 21 0>, <&gpio 22 0>;
+ linux,axis = <1>; /* ABS_Y */
+ rotary-encoder,steps = <24>;
+ rotary-encoder,rollover;
+ };
+
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index d4e0abb..6a14fef 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -24,6 +24,8 @@
#include <linux/gpio.h>
#include <linux/rotary_encoder.h>
#include <linux/slab.h>
+#include <linux/of_platform.h>
+#include <linux/of_gpio.h>
#define DRV_NAME "rotary-encoder"
@@ -140,15 +142,73 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
return IRQ_HANDLED;
}
+#ifdef CONFIG_OF
+static struct of_device_id rotary_encoder_of_match[] = {
+ { .compatible = "rotary-encoder", },
+ { },
+};
+MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
+
+static int rotary_encoder_probe_dt(struct platform_device *pdev)
+{
+ int tmp;
+ enum of_gpio_flags flags;
+ struct rotary_encoder_platform_data *pdata;
+ struct device_node *np = pdev->dev.of_node;
+ const struct of_device_id *of_id =
+ of_match_device(rotary_encoder_of_match, &pdev->dev);
+
+ if (!of_id)
+ return 0;
+
+ pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
+ if (!pdata)
+ return -ENOMEM;
+
+ if (of_property_read_u32(np, "rotary-encoder,steps", &tmp) == 0)
+ pdata->steps = tmp;
+ if (of_property_read_u32(np, "linux,axis", &tmp) == 0)
+ pdata->axis = tmp;
+
+ pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
+ pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
+
+ pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
+ pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
+
+ if (of_get_property(np, "rotary-encoder,relative-axis", NULL))
+ pdata->relative_axis = 1;
+ if (of_get_property(np, "rotary-encoder,rollover", NULL))
+ pdata->rollover = 1;
+ if (of_get_property(np, "rotary-encoder,half-period", NULL))
+ pdata->half_period = 1;
+
+ pdev->dev.platform_data = pdata;
+
+ return 0;
+}
+#else
+static inline int rotary_encoder_probe_dt(struct platform_device *)
+{
+ return 0;
+}
+#endif
+
static int __devinit rotary_encoder_probe(struct platform_device *pdev)
{
- struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
+ struct rotary_encoder_platform_data *pdata;
struct rotary_encoder *encoder;
struct input_dev *input;
struct device *dev = &pdev->dev;
irq_handler_t handler;
int err;
+ err = rotary_encoder_probe_dt(pdev);
+ if (err)
+ return err;
+
+ pdata = pdev->dev.platform_data;
+
if (!pdata) {
dev_err(&pdev->dev, "missing platform data\n");
return -ENOENT;
@@ -257,6 +317,7 @@ static struct platform_driver rotary_encoder_driver = {
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
+ .of_match_table = of_match_ptr(rotary_encoder_of_match),
}
};
module_platform_driver(rotary_encoder_driver);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
2012-07-25 7:43 [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Daniel Mack
2012-07-25 7:43 ` [PATCH v2 2/2] input: rotary-encoder: add DT bindings Daniel Mack
@ 2012-07-25 16:05 ` Dmitry Torokhov
2012-07-25 16:07 ` Daniel Mack
2012-07-25 16:07 ` H Hartley Sweeten
1 sibling, 2 replies; 7+ messages in thread
From: Dmitry Torokhov @ 2012-07-25 16:05 UTC (permalink / raw)
To: Daniel Mack; +Cc: linux-input, jhovold, hartleys
On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
> @@ -263,12 +243,7 @@ exit_free_mem:
> static int __devexit rotary_encoder_remove(struct platform_device *pdev)
> {
> struct rotary_encoder *encoder = platform_get_drvdata(pdev);
> - struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>
> - free_irq(encoder->irq_a, encoder);
> - free_irq(encoder->irq_b, encoder);
> - gpio_free(pdata->gpio_a);
> - gpio_free(pdata->gpio_b);
> input_unregister_device(encoder->input);
Another botched devm_ conversion. *sigh*
Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
Please, do not use devm_ interfaces unless... Actually, just do not use
nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
styles of releasing resources leads to trouble.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
2012-07-25 16:05 ` [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Dmitry Torokhov
@ 2012-07-25 16:07 ` Daniel Mack
2012-07-25 16:29 ` Dmitry Torokhov
2012-07-25 16:07 ` H Hartley Sweeten
1 sibling, 1 reply; 7+ messages in thread
From: Daniel Mack @ 2012-07-25 16:07 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input, jhovold, hartleys
On 25.07.2012 18:05, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>> @@ -263,12 +243,7 @@ exit_free_mem:
>> static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>> {
>> struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>> - struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>
>> - free_irq(encoder->irq_a, encoder);
>> - free_irq(encoder->irq_b, encoder);
>> - gpio_free(pdata->gpio_a);
>> - gpio_free(pdata->gpio_b);
>> input_unregister_device(encoder->input);
>
> Another botched devm_ conversion. *sigh*
>
> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
>
> Please, do not use devm_ interfaces unless... Actually, just do not use
> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> styles of releasing resources leads to trouble.
Ok, makes sense. Thanks for noticing. Are you happy with the first
version I submitted then?
Daniel
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
2012-07-25 16:05 ` [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Dmitry Torokhov
2012-07-25 16:07 ` Daniel Mack
@ 2012-07-25 16:07 ` H Hartley Sweeten
1 sibling, 0 replies; 7+ messages in thread
From: H Hartley Sweeten @ 2012-07-25 16:07 UTC (permalink / raw)
To: Dmitry Torokhov, Daniel Mack
Cc: linux-input@vger.kernel.org, jhovold@gmail.com
On Wednesday, July 25, 2012 9:05 AM, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>> @@ -263,12 +243,7 @@ exit_free_mem:
>> static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>> {
>> struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>> - struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>
>> - free_irq(encoder->irq_a, encoder);
>> - free_irq(encoder->irq_b, encoder);
>> - gpio_free(pdata->gpio_a);
>> - gpio_free(pdata->gpio_b);
>> input_unregister_device(encoder->input);
>
> Another botched devm_ conversion. *sigh*
>
> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
>
> Please, do not use devm_ interfaces unless... Actually, just do not use
> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> styles of releasing resources leads to trouble.
Oops.. Sorry about that. I didn't think about the interaction with
input_unregister_device.
Regards,
Hartley
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
2012-07-25 16:07 ` Daniel Mack
@ 2012-07-25 16:29 ` Dmitry Torokhov
2012-07-25 18:04 ` Daniel Mack
0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Torokhov @ 2012-07-25 16:29 UTC (permalink / raw)
To: Daniel Mack; +Cc: linux-input, jhovold, hartleys
On Wed, Jul 25, 2012 at 06:07:24PM +0200, Daniel Mack wrote:
> On 25.07.2012 18:05, Dmitry Torokhov wrote:
> > On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
> >> @@ -263,12 +243,7 @@ exit_free_mem:
> >> static int __devexit rotary_encoder_remove(struct platform_device *pdev)
> >> {
> >> struct rotary_encoder *encoder = platform_get_drvdata(pdev);
> >> - struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
> >>
> >> - free_irq(encoder->irq_a, encoder);
> >> - free_irq(encoder->irq_b, encoder);
> >> - gpio_free(pdata->gpio_a);
> >> - gpio_free(pdata->gpio_b);
> >> input_unregister_device(encoder->input);
> >
> > Another botched devm_ conversion. *sigh*
> >
> > Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
> >
> > Please, do not use devm_ interfaces unless... Actually, just do not use
> > nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> > styles of releasing resources leads to trouble.
>
> Ok, makes sense. Thanks for noticing. Are you happy with the first
> version I submitted then?
I have a few issues with that version as well:
1. It writes over platform_data field in the device which does nto
belong to the driver (this field belongs to the platform/board code);
2. I believe we should favor kernel supplied data over firmware
allowing users to override DT bindings, if needed;
3. It still uses devm_* to allocate memory and as I said I do not like
mixing 2 styles of managign resources in one driver.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs
2012-07-25 16:29 ` Dmitry Torokhov
@ 2012-07-25 18:04 ` Daniel Mack
0 siblings, 0 replies; 7+ messages in thread
From: Daniel Mack @ 2012-07-25 18:04 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input, jhovold, hartleys
On 25.07.2012 18:29, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 06:07:24PM +0200, Daniel Mack wrote:
>> On 25.07.2012 18:05, Dmitry Torokhov wrote:
>>> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>>>> @@ -263,12 +243,7 @@ exit_free_mem:
>>>> static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>>>> {
>>>> struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>>>> - struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>>>
>>>> - free_irq(encoder->irq_a, encoder);
>>>> - free_irq(encoder->irq_b, encoder);
>>>> - gpio_free(pdata->gpio_a);
>>>> - gpio_free(pdata->gpio_b);
>>>> input_unregister_device(encoder->input);
>>>
>>> Another botched devm_ conversion. *sigh*
>>>
>>> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
>>>
>>> Please, do not use devm_ interfaces unless... Actually, just do not use
>>> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
>>> styles of releasing resources leads to trouble.
>>
>> Ok, makes sense. Thanks for noticing. Are you happy with the first
>> version I submitted then?
>
> I have a few issues with that version as well:
>
> 1. It writes over platform_data field in the device which does nto
> belong to the driver (this field belongs to the platform/board code);
>
> 2. I believe we should favor kernel supplied data over firmware
> allowing users to override DT bindings, if needed;
Ok, this is different from what I saw in other drivers, but I'm fine
with that.
> 3. It still uses devm_* to allocate memory and as I said I do not like
> mixing 2 styles of managign resources in one driver.
Ok, sorry again. I didn't closely follow that new development, hence I
wasn't aware of these pitfalls.
New patch series coming up.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2012-07-25 18:04 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-25 7:43 [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Daniel Mack
2012-07-25 7:43 ` [PATCH v2 2/2] input: rotary-encoder: add DT bindings Daniel Mack
2012-07-25 16:05 ` [PATCH v2 1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs Dmitry Torokhov
2012-07-25 16:07 ` Daniel Mack
2012-07-25 16:29 ` Dmitry Torokhov
2012-07-25 18:04 ` Daniel Mack
2012-07-25 16:07 ` H Hartley Sweeten
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).