Skip to content

Commit 0f78ba9

Browse files
committed
Input: gpio_keys_polled - keep button data constant
Commit 633a21d ("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>
1 parent 8dd5e0b commit 0f78ba9

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

drivers/input/keyboard/gpio_keys.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ gpio_keys_get_devtree_pdata(struct device *dev)
624624
struct gpio_keys_button *button;
625625
int error;
626626
int nbuttons;
627-
int i;
628627

629628
node = dev->of_node;
630629
if (!node)
@@ -640,19 +639,18 @@ gpio_keys_get_devtree_pdata(struct device *dev)
640639
if (!pdata)
641640
return ERR_PTR(-ENOMEM);
642641

643-
pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
642+
button = (struct gpio_keys_button *)(pdata + 1);
643+
644+
pdata->buttons = button;
644645
pdata->nbuttons = nbuttons;
645646

646647
pdata->rep = !!of_get_property(node, "autorepeat", NULL);
647648

648649
of_property_read_string(node, "label", &pdata->name);
649650

650-
i = 0;
651651
for_each_available_child_of_node(node, pp) {
652652
enum of_gpio_flags flags;
653653

654-
button = &pdata->buttons[i++];
655-
656654
button->gpio = of_get_gpio_flags(pp, 0, &flags);
657655
if (button->gpio < 0) {
658656
error = button->gpio;
@@ -694,6 +692,8 @@ gpio_keys_get_devtree_pdata(struct device *dev)
694692
if (of_property_read_u32(pp, "debounce-interval",
695693
&button->debounce_interval))
696694
button->debounce_interval = 5;
695+
696+
button++;
697697
}
698698

699699
if (pdata->nbuttons == 0)

drivers/input/keyboard/gpio_keys_polled.c

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define DRV_NAME "gpio-keys-polled"
3131

3232
struct gpio_keys_button_data {
33+
struct gpio_desc *gpiod;
3334
int last_state;
3435
int count;
3536
int threshold;
@@ -46,7 +47,7 @@ struct gpio_keys_polled_dev {
4647
};
4748

4849
static void gpio_keys_button_event(struct input_polled_dev *dev,
49-
struct gpio_keys_button *button,
50+
const struct gpio_keys_button *button,
5051
int state)
5152
{
5253
struct gpio_keys_polled_dev *bdev = dev->private;
@@ -70,15 +71,15 @@ static void gpio_keys_button_event(struct input_polled_dev *dev,
7071
}
7172

7273
static void gpio_keys_polled_check_state(struct input_polled_dev *dev,
73-
struct gpio_keys_button *button,
74+
const struct gpio_keys_button *button,
7475
struct gpio_keys_button_data *bdata)
7576
{
7677
int state;
7778

7879
if (bdata->can_sleep)
79-
state = !!gpiod_get_value_cansleep(button->gpiod);
80+
state = !!gpiod_get_value_cansleep(bdata->gpiod);
8081
else
81-
state = !!gpiod_get_value(button->gpiod);
82+
state = !!gpiod_get_value(bdata->gpiod);
8283

8384
gpio_keys_button_event(dev, button, state);
8485

@@ -142,48 +143,35 @@ static void gpio_keys_polled_close(struct input_polled_dev *dev)
142143
pdata->disable(bdev->dev);
143144
}
144145

145-
static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev)
146+
static struct gpio_keys_platform_data *
147+
gpio_keys_polled_get_devtree_pdata(struct device *dev)
146148
{
147149
struct gpio_keys_platform_data *pdata;
148150
struct gpio_keys_button *button;
149151
struct fwnode_handle *child;
150-
int error;
151152
int nbuttons;
152153

153154
nbuttons = device_get_child_node_count(dev);
154155
if (nbuttons == 0)
155-
return NULL;
156+
return ERR_PTR(-EINVAL);
156157

157158
pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
158159
GFP_KERNEL);
159160
if (!pdata)
160161
return ERR_PTR(-ENOMEM);
161162

162-
pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
163+
button = (struct gpio_keys_button *)(pdata + 1);
164+
165+
pdata->buttons = button;
166+
pdata->nbuttons = nbuttons;
163167

164168
pdata->rep = device_property_present(dev, "autorepeat");
165169
device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
166170

167171
device_for_each_child_node(dev, child) {
168-
struct gpio_desc *desc;
169-
170-
desc = devm_get_gpiod_from_child(dev, NULL, child);
171-
if (IS_ERR(desc)) {
172-
error = PTR_ERR(desc);
173-
if (error != -EPROBE_DEFER)
174-
dev_err(dev,
175-
"Failed to get gpio flags, error: %d\n",
176-
error);
177-
fwnode_handle_put(child);
178-
return ERR_PTR(error);
179-
}
180-
181-
button = &pdata->buttons[pdata->nbuttons++];
182-
button->gpiod = desc;
183-
184-
if (fwnode_property_read_u32(child, "linux,code", &button->code)) {
185-
dev_err(dev, "Button without keycode: %d\n",
186-
pdata->nbuttons - 1);
172+
if (fwnode_property_read_u32(child, "linux,code",
173+
&button->code)) {
174+
dev_err(dev, "button without keycode\n");
187175
fwnode_handle_put(child);
188176
return ERR_PTR(-EINVAL);
189177
}
@@ -206,10 +194,9 @@ static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct
206194
if (fwnode_property_read_u32(child, "debounce-interval",
207195
&button->debounce_interval))
208196
button->debounce_interval = 5;
209-
}
210197

211-
if (pdata->nbuttons == 0)
212-
return ERR_PTR(-EINVAL);
198+
button++;
199+
}
213200

214201
return pdata;
215202
}
@@ -220,7 +207,7 @@ static void gpio_keys_polled_set_abs_params(struct input_dev *input,
220207
int i, min = 0, max = 0;
221208

222209
for (i = 0; i < pdata->nbuttons; i++) {
223-
struct gpio_keys_button *button = &pdata->buttons[i];
210+
const struct gpio_keys_button *button = &pdata->buttons[i];
224211

225212
if (button->type != EV_ABS || button->code != code)
226213
continue;
@@ -230,6 +217,7 @@ static void gpio_keys_polled_set_abs_params(struct input_dev *input,
230217
if (button->value > max)
231218
max = button->value;
232219
}
220+
233221
input_set_abs_params(input, code, min, max, 0, 0);
234222
}
235223

@@ -242,6 +230,7 @@ MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
242230
static int gpio_keys_polled_probe(struct platform_device *pdev)
243231
{
244232
struct device *dev = &pdev->dev;
233+
struct fwnode_handle *child = NULL;
245234
const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
246235
struct gpio_keys_polled_dev *bdev;
247236
struct input_polled_dev *poll_dev;
@@ -254,10 +243,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
254243
pdata = gpio_keys_polled_get_devtree_pdata(dev);
255244
if (IS_ERR(pdata))
256245
return PTR_ERR(pdata);
257-
if (!pdata) {
258-
dev_err(dev, "missing platform data\n");
259-
return -EINVAL;
260-
}
261246
}
262247

263248
if (!pdata->poll_interval) {
@@ -300,20 +285,40 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
300285
__set_bit(EV_REP, input->evbit);
301286

302287
for (i = 0; i < pdata->nbuttons; i++) {
303-
struct gpio_keys_button *button = &pdata->buttons[i];
288+
const struct gpio_keys_button *button = &pdata->buttons[i];
304289
struct gpio_keys_button_data *bdata = &bdev->data[i];
305290
unsigned int type = button->type ?: EV_KEY;
306291

307292
if (button->wakeup) {
308293
dev_err(dev, DRV_NAME " does not support wakeup\n");
294+
fwnode_handle_put(child);
309295
return -EINVAL;
310296
}
311297

312-
/*
313-
* Legacy GPIO number so request the GPIO here and
314-
* convert it to descriptor.
315-
*/
316-
if (!button->gpiod && gpio_is_valid(button->gpio)) {
298+
if (!dev_get_platdata(dev)) {
299+
/* No legacy static platform data */
300+
child = device_get_next_child_node(dev, child);
301+
if (!child) {
302+
dev_err(dev, "missing child device node\n");
303+
return -EINVAL;
304+
}
305+
306+
bdata->gpiod = devm_get_gpiod_from_child(dev, NULL,
307+
child);
308+
if (IS_ERR(bdata->gpiod)) {
309+
error = PTR_ERR(bdata->gpiod);
310+
if (error != -EPROBE_DEFER)
311+
dev_err(dev,
312+
"failed to get gpio: %d\n",
313+
error);
314+
fwnode_handle_put(child);
315+
return error;
316+
}
317+
} else if (gpio_is_valid(button->gpio)) {
318+
/*
319+
* Legacy GPIO number so request the GPIO here and
320+
* convert it to descriptor.
321+
*/
317322
unsigned flags = GPIOF_IN;
318323

319324
if (button->active_low)
@@ -322,18 +327,22 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
322327
error = devm_gpio_request_one(&pdev->dev, button->gpio,
323328
flags, button->desc ? : DRV_NAME);
324329
if (error) {
325-
dev_err(dev, "unable to claim gpio %u, err=%d\n",
330+
dev_err(dev,
331+
"unable to claim gpio %u, err=%d\n",
326332
button->gpio, error);
327333
return error;
328334
}
329335

330-
button->gpiod = gpio_to_desc(button->gpio);
336+
bdata->gpiod = gpio_to_desc(button->gpio);
337+
if (!bdata->gpiod) {
338+
dev_err(dev,
339+
"unable to convert gpio %u to descriptor\n",
340+
button->gpio);
341+
return -EINVAL;
342+
}
331343
}
332344

333-
if (IS_ERR(button->gpiod))
334-
return PTR_ERR(button->gpiod);
335-
336-
bdata->can_sleep = gpiod_cansleep(button->gpiod);
345+
bdata->can_sleep = gpiod_cansleep(bdata->gpiod);
337346
bdata->last_state = -1;
338347
bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
339348
pdata->poll_interval);
@@ -344,6 +353,8 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
344353
button->code);
345354
}
346355

356+
fwnode_handle_put(child);
357+
347358
bdev->poll_dev = poll_dev;
348359
bdev->dev = dev;
349360
bdev->pdata = pdata;

include/linux/gpio_keys.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define _GPIO_KEYS_H
33

44
struct device;
5-
struct gpio_desc;
65

76
/**
87
* struct gpio_keys_button - configuration parameters
@@ -18,7 +17,6 @@ struct gpio_desc;
1817
* disable button via sysfs
1918
* @value: axis value for %EV_ABS
2019
* @irq: Irq number in case of interrupt keys
21-
* @gpiod: GPIO descriptor
2220
*/
2321
struct gpio_keys_button {
2422
unsigned int code;
@@ -31,7 +29,6 @@ struct gpio_keys_button {
3129
bool can_disable;
3230
int value;
3331
unsigned int irq;
34-
struct gpio_desc *gpiod;
3532
};
3633

3734
/**
@@ -46,7 +43,7 @@ struct gpio_keys_button {
4643
* @name: input device name
4744
*/
4845
struct gpio_keys_platform_data {
49-
struct gpio_keys_button *buttons;
46+
const struct gpio_keys_button *buttons;
5047
int nbuttons;
5148
unsigned int poll_interval;
5249
unsigned int rep:1;

0 commit comments

Comments
 (0)