Skip to content

Commit 77a8f0a

Browse files
committed
Input: rotary_encoder - convert to use gpiod API
Instead of using old GPIO API, let's switch to GPIOD API, which automatically handles polarity. Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 6a6f70b commit 77a8f0a

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

arch/arm/mach-pxa/raumfeld.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/platform_device.h>
2222
#include <linux/interrupt.h>
2323
#include <linux/gpio.h>
24+
#include <linux/gpio/machine.h>
2425
#include <linux/smsc911x.h>
2526
#include <linux/input.h>
2627
#include <linux/rotary_encoder.h>
@@ -366,14 +367,21 @@ static struct pxaohci_platform_data raumfeld_ohci_info = {
366367
* Rotary encoder input device
367368
*/
368369

370+
static struct gpiod_lookup_table raumfeld_rotary_gpios_table = {
371+
.dev_id = "rotary-encoder.0",
372+
.table = {
373+
GPIO_LOOKUP_IDX("gpio-0",
374+
GPIO_VOLENC_A, NULL, 0, GPIO_ACTIVE_LOW),
375+
GPIO_LOOKUP_IDX("gpio-0",
376+
GPIO_VOLENC_B, NULL, 1, GPIO_ACTIVE_HIGH),
377+
{ },
378+
},
379+
};
380+
369381
static struct rotary_encoder_platform_data raumfeld_rotary_encoder_info = {
370382
.steps = 24,
371383
.axis = REL_X,
372384
.relative_axis = 1,
373-
.gpio_a = GPIO_VOLENC_A,
374-
.gpio_b = GPIO_VOLENC_B,
375-
.inverted_a = 1,
376-
.inverted_b = 0,
377385
};
378386

379387
static struct platform_device rotary_encoder_device = {
@@ -1051,7 +1059,10 @@ static void __init raumfeld_controller_init(void)
10511059
int ret;
10521060

10531061
pxa3xx_mfp_config(ARRAY_AND_SIZE(raumfeld_controller_pin_config));
1062+
1063+
gpiod_add_lookup_table(&raumfeld_rotary_gpios_table);
10541064
platform_device_register(&rotary_encoder_device);
1065+
10551066
spi_register_board_info(ARRAY_AND_SIZE(controller_spi_devices));
10561067
i2c_register_board_info(0, &raumfeld_controller_i2c_board_info, 1);
10571068

@@ -1086,6 +1097,8 @@ static void __init raumfeld_speaker_init(void)
10861097
i2c_register_board_info(0, &raumfeld_connector_i2c_board_info, 1);
10871098

10881099
platform_device_register(&smc91x_device);
1100+
1101+
gpiod_add_lookup_table(&raumfeld_rotary_gpios_table);
10891102
platform_device_register(&rotary_encoder_device);
10901103

10911104
raumfeld_audio_init();

drivers/input/misc/rotary_encoder.c

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
#include <linux/input.h>
2121
#include <linux/device.h>
2222
#include <linux/platform_device.h>
23-
#include <linux/gpio.h>
23+
#include <linux/gpio/consumer.h>
2424
#include <linux/rotary_encoder.h>
2525
#include <linux/slab.h>
2626
#include <linux/of.h>
2727
#include <linux/of_platform.h>
28-
#include <linux/of_gpio.h>
2928
#include <linux/pm.h>
3029

3130
#define DRV_NAME "rotary-encoder"
@@ -38,6 +37,9 @@ struct rotary_encoder {
3837
unsigned int axis;
3938
unsigned int pos;
4039

40+
struct gpio_desc *gpio_a;
41+
struct gpio_desc *gpio_b;
42+
4143
unsigned int irq_a;
4244
unsigned int irq_b;
4345

@@ -47,13 +49,10 @@ struct rotary_encoder {
4749
char last_stable;
4850
};
4951

50-
static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata)
52+
static int rotary_encoder_get_state(struct rotary_encoder *encoder)
5153
{
52-
int a = !!gpio_get_value_cansleep(pdata->gpio_a);
53-
int b = !!gpio_get_value_cansleep(pdata->gpio_b);
54-
55-
a ^= pdata->inverted_a;
56-
b ^= pdata->inverted_b;
54+
int a = !!gpiod_get_value_cansleep(encoder->gpio_a);
55+
int b = !!gpiod_get_value_cansleep(encoder->gpio_b);
5756

5857
return ((a << 1) | b);
5958
}
@@ -97,7 +96,7 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
9796

9897
mutex_lock(&encoder->access_mutex);
9998

100-
state = rotary_encoder_get_state(encoder->pdata);
99+
state = rotary_encoder_get_state(encoder);
101100

102101
switch (state) {
103102
case 0x0:
@@ -130,7 +129,7 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
130129

131130
mutex_lock(&encoder->access_mutex);
132131

133-
state = rotary_encoder_get_state(encoder->pdata);
132+
state = rotary_encoder_get_state(encoder);
134133

135134
switch (state) {
136135
case 0x00:
@@ -160,7 +159,7 @@ static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
160159

161160
mutex_lock(&encoder->access_mutex);
162161

163-
state = rotary_encoder_get_state(encoder->pdata);
162+
state = rotary_encoder_get_state(encoder);
164163

165164
/*
166165
* We encode the previous and the current state using a byte.
@@ -218,7 +217,6 @@ static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct devic
218217
of_match_device(rotary_encoder_of_match, dev);
219218
struct device_node *np = dev->of_node;
220219
struct rotary_encoder_platform_data *pdata;
221-
enum of_gpio_flags flags;
222220
int error;
223221

224222
if (!of_id || !np)
@@ -232,12 +230,6 @@ static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct devic
232230
of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
233231
of_property_read_u32(np, "linux,axis", &pdata->axis);
234232

235-
pdata->gpio_a = of_get_gpio_flags(np, 0, &flags);
236-
pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW;
237-
238-
pdata->gpio_b = of_get_gpio_flags(np, 1, &flags);
239-
pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
240-
241233
pdata->relative_axis =
242234
of_property_read_bool(np, "rotary-encoder,relative-axis");
243235
pdata->rollover = of_property_read_bool(np, "rotary-encoder,rollover");
@@ -294,14 +286,32 @@ static int rotary_encoder_probe(struct platform_device *pdev)
294286
if (!encoder)
295287
return -ENOMEM;
296288

289+
mutex_init(&encoder->access_mutex);
290+
encoder->pdata = pdata;
291+
292+
encoder->gpio_a = devm_gpiod_get_index(dev, NULL, 0, GPIOD_IN);
293+
if (IS_ERR(encoder->gpio_a)) {
294+
err = PTR_ERR(encoder->gpio_a);
295+
dev_err(dev, "unable to get GPIO at index 0: %d\n", err);
296+
return err;
297+
}
298+
299+
encoder->irq_a = gpiod_to_irq(encoder->gpio_a);
300+
301+
encoder->gpio_b = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
302+
if (IS_ERR(encoder->gpio_b)) {
303+
err = PTR_ERR(encoder->gpio_b);
304+
dev_err(dev, "unable to get GPIO at index 1: %d\n", err);
305+
return err;
306+
}
307+
308+
encoder->irq_b = gpiod_to_irq(encoder->gpio_b);
309+
297310
input = devm_input_allocate_device(dev);
298311
if (!input)
299312
return -ENOMEM;
300313

301-
mutex_init(&encoder->access_mutex);
302-
303314
encoder->input = input;
304-
encoder->pdata = pdata;
305315

306316
input->name = pdev->name;
307317
input->id.bustype = BUS_HOST;
@@ -316,32 +326,14 @@ static int rotary_encoder_probe(struct platform_device *pdev)
316326
pdata->axis, 0, pdata->steps, 0, 1);
317327
}
318328

319-
/* request the GPIOs */
320-
err = devm_gpio_request_one(dev, pdata->gpio_a, GPIOF_IN,
321-
dev_name(dev));
322-
if (err) {
323-
dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
324-
return err;
325-
}
326-
327-
err = devm_gpio_request_one(dev, pdata->gpio_b, GPIOF_IN,
328-
dev_name(dev));
329-
if (err) {
330-
dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
331-
return err;
332-
}
333-
334-
encoder->irq_a = gpio_to_irq(pdata->gpio_a);
335-
encoder->irq_b = gpio_to_irq(pdata->gpio_b);
336-
337329
switch (pdata->steps_per_period) {
338330
case 4:
339331
handler = &rotary_encoder_quarter_period_irq;
340-
encoder->last_stable = rotary_encoder_get_state(pdata);
332+
encoder->last_stable = rotary_encoder_get_state(encoder);
341333
break;
342334
case 2:
343335
handler = &rotary_encoder_half_period_irq;
344-
encoder->last_stable = rotary_encoder_get_state(pdata);
336+
encoder->last_stable = rotary_encoder_get_state(encoder);
345337
break;
346338
case 1:
347339
handler = &rotary_encoder_irq;

include/linux/rotary_encoder.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
struct rotary_encoder_platform_data {
55
unsigned int steps;
66
unsigned int axis;
7-
unsigned int gpio_a;
8-
unsigned int gpio_b;
9-
unsigned int inverted_a;
10-
unsigned int inverted_b;
117
unsigned int steps_per_period;
128
bool relative_axis;
139
bool rollover;

0 commit comments

Comments
 (0)