0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/input.h>
0019 #include <linux/ioport.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/gpio.h>
0022 #include <linux/gpio/consumer.h>
0023 #include <linux/gpio_keys.h>
0024 #include <linux/property.h>
0025
0026 #define DRV_NAME "gpio-keys-polled"
0027
0028 struct gpio_keys_button_data {
0029 struct gpio_desc *gpiod;
0030 int last_state;
0031 int count;
0032 int threshold;
0033 };
0034
0035 struct gpio_keys_polled_dev {
0036 struct input_dev *input;
0037 struct device *dev;
0038 const struct gpio_keys_platform_data *pdata;
0039 unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)];
0040 unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)];
0041 struct gpio_keys_button_data data[];
0042 };
0043
0044 static void gpio_keys_button_event(struct input_dev *input,
0045 const struct gpio_keys_button *button,
0046 int state)
0047 {
0048 struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
0049 unsigned int type = button->type ?: EV_KEY;
0050
0051 if (type == EV_REL) {
0052 if (state) {
0053 input_event(input, type, button->code, button->value);
0054 __set_bit(button->code, bdev->rel_axis_seen);
0055 }
0056 } else if (type == EV_ABS) {
0057 if (state) {
0058 input_event(input, type, button->code, button->value);
0059 __set_bit(button->code, bdev->abs_axis_seen);
0060 }
0061 } else {
0062 input_event(input, type, button->code, state);
0063 input_sync(input);
0064 }
0065 }
0066
0067 static void gpio_keys_polled_check_state(struct input_dev *input,
0068 const struct gpio_keys_button *button,
0069 struct gpio_keys_button_data *bdata)
0070 {
0071 int state;
0072
0073 state = gpiod_get_value_cansleep(bdata->gpiod);
0074 if (state < 0) {
0075 dev_err(input->dev.parent,
0076 "failed to get gpio state: %d\n", state);
0077 } else {
0078 gpio_keys_button_event(input, button, state);
0079
0080 if (state != bdata->last_state) {
0081 bdata->count = 0;
0082 bdata->last_state = state;
0083 }
0084 }
0085 }
0086
0087 static void gpio_keys_polled_poll(struct input_dev *input)
0088 {
0089 struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
0090 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0091 int i;
0092
0093 memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen));
0094 memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen));
0095
0096 for (i = 0; i < pdata->nbuttons; i++) {
0097 struct gpio_keys_button_data *bdata = &bdev->data[i];
0098
0099 if (bdata->count < bdata->threshold) {
0100 bdata->count++;
0101 gpio_keys_button_event(input, &pdata->buttons[i],
0102 bdata->last_state);
0103 } else {
0104 gpio_keys_polled_check_state(input, &pdata->buttons[i],
0105 bdata);
0106 }
0107 }
0108
0109 for_each_set_bit(i, input->relbit, REL_CNT) {
0110 if (!test_bit(i, bdev->rel_axis_seen))
0111 input_event(input, EV_REL, i, 0);
0112 }
0113
0114 for_each_set_bit(i, input->absbit, ABS_CNT) {
0115 if (!test_bit(i, bdev->abs_axis_seen))
0116 input_event(input, EV_ABS, i, 0);
0117 }
0118
0119 input_sync(input);
0120 }
0121
0122 static int gpio_keys_polled_open(struct input_dev *input)
0123 {
0124 struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
0125 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0126
0127 if (pdata->enable)
0128 pdata->enable(bdev->dev);
0129
0130 return 0;
0131 }
0132
0133 static void gpio_keys_polled_close(struct input_dev *input)
0134 {
0135 struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
0136 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0137
0138 if (pdata->disable)
0139 pdata->disable(bdev->dev);
0140 }
0141
0142 static struct gpio_keys_platform_data *
0143 gpio_keys_polled_get_devtree_pdata(struct device *dev)
0144 {
0145 struct gpio_keys_platform_data *pdata;
0146 struct gpio_keys_button *button;
0147 struct fwnode_handle *child;
0148 int nbuttons;
0149
0150 nbuttons = device_get_child_node_count(dev);
0151 if (nbuttons == 0)
0152 return ERR_PTR(-EINVAL);
0153
0154 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
0155 GFP_KERNEL);
0156 if (!pdata)
0157 return ERR_PTR(-ENOMEM);
0158
0159 button = (struct gpio_keys_button *)(pdata + 1);
0160
0161 pdata->buttons = button;
0162 pdata->nbuttons = nbuttons;
0163
0164 pdata->rep = device_property_present(dev, "autorepeat");
0165 device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
0166
0167 device_property_read_string(dev, "label", &pdata->name);
0168
0169 device_for_each_child_node(dev, child) {
0170 if (fwnode_property_read_u32(child, "linux,code",
0171 &button->code)) {
0172 dev_err(dev, "button without keycode\n");
0173 fwnode_handle_put(child);
0174 return ERR_PTR(-EINVAL);
0175 }
0176
0177 fwnode_property_read_string(child, "label", &button->desc);
0178
0179 if (fwnode_property_read_u32(child, "linux,input-type",
0180 &button->type))
0181 button->type = EV_KEY;
0182
0183 if (fwnode_property_read_u32(child, "linux,input-value",
0184 (u32 *)&button->value))
0185 button->value = 1;
0186
0187 button->wakeup =
0188 fwnode_property_read_bool(child, "wakeup-source") ||
0189
0190 fwnode_property_read_bool(child, "gpio-key,wakeup");
0191
0192 if (fwnode_property_read_u32(child, "debounce-interval",
0193 &button->debounce_interval))
0194 button->debounce_interval = 5;
0195
0196 button++;
0197 }
0198
0199 return pdata;
0200 }
0201
0202 static void gpio_keys_polled_set_abs_params(struct input_dev *input,
0203 const struct gpio_keys_platform_data *pdata, unsigned int code)
0204 {
0205 int i, min = 0, max = 0;
0206
0207 for (i = 0; i < pdata->nbuttons; i++) {
0208 const struct gpio_keys_button *button = &pdata->buttons[i];
0209
0210 if (button->type != EV_ABS || button->code != code)
0211 continue;
0212
0213 if (button->value < min)
0214 min = button->value;
0215 if (button->value > max)
0216 max = button->value;
0217 }
0218
0219 input_set_abs_params(input, code, min, max, 0, 0);
0220 }
0221
0222 static const struct of_device_id gpio_keys_polled_of_match[] = {
0223 { .compatible = "gpio-keys-polled", },
0224 { },
0225 };
0226 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
0227
0228 static int gpio_keys_polled_probe(struct platform_device *pdev)
0229 {
0230 struct device *dev = &pdev->dev;
0231 struct fwnode_handle *child = NULL;
0232 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
0233 struct gpio_keys_polled_dev *bdev;
0234 struct input_dev *input;
0235 int error;
0236 int i;
0237
0238 if (!pdata) {
0239 pdata = gpio_keys_polled_get_devtree_pdata(dev);
0240 if (IS_ERR(pdata))
0241 return PTR_ERR(pdata);
0242 }
0243
0244 if (!pdata->poll_interval) {
0245 dev_err(dev, "missing poll_interval value\n");
0246 return -EINVAL;
0247 }
0248
0249 bdev = devm_kzalloc(dev, struct_size(bdev, data, pdata->nbuttons),
0250 GFP_KERNEL);
0251 if (!bdev) {
0252 dev_err(dev, "no memory for private data\n");
0253 return -ENOMEM;
0254 }
0255
0256 input = devm_input_allocate_device(dev);
0257 if (!input) {
0258 dev_err(dev, "no memory for input device\n");
0259 return -ENOMEM;
0260 }
0261
0262 input_set_drvdata(input, bdev);
0263
0264 input->name = pdata->name ?: pdev->name;
0265 input->phys = DRV_NAME"/input0";
0266
0267 input->id.bustype = BUS_HOST;
0268 input->id.vendor = 0x0001;
0269 input->id.product = 0x0001;
0270 input->id.version = 0x0100;
0271
0272 input->open = gpio_keys_polled_open;
0273 input->close = gpio_keys_polled_close;
0274
0275 __set_bit(EV_KEY, input->evbit);
0276 if (pdata->rep)
0277 __set_bit(EV_REP, input->evbit);
0278
0279 for (i = 0; i < pdata->nbuttons; i++) {
0280 const struct gpio_keys_button *button = &pdata->buttons[i];
0281 struct gpio_keys_button_data *bdata = &bdev->data[i];
0282 unsigned int type = button->type ?: EV_KEY;
0283
0284 if (button->wakeup) {
0285 dev_err(dev, DRV_NAME " does not support wakeup\n");
0286 fwnode_handle_put(child);
0287 return -EINVAL;
0288 }
0289
0290 if (!dev_get_platdata(dev)) {
0291
0292 child = device_get_next_child_node(dev, child);
0293 if (!child) {
0294 dev_err(dev, "missing child device node\n");
0295 return -EINVAL;
0296 }
0297
0298 bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
0299 NULL, GPIOD_IN,
0300 button->desc);
0301 if (IS_ERR(bdata->gpiod)) {
0302 error = PTR_ERR(bdata->gpiod);
0303 if (error != -EPROBE_DEFER)
0304 dev_err(dev,
0305 "failed to get gpio: %d\n",
0306 error);
0307 fwnode_handle_put(child);
0308 return error;
0309 }
0310 } else if (gpio_is_valid(button->gpio)) {
0311
0312
0313
0314
0315 unsigned flags = GPIOF_IN;
0316
0317 if (button->active_low)
0318 flags |= GPIOF_ACTIVE_LOW;
0319
0320 error = devm_gpio_request_one(dev, button->gpio,
0321 flags, button->desc ? : DRV_NAME);
0322 if (error) {
0323 dev_err(dev,
0324 "unable to claim gpio %u, err=%d\n",
0325 button->gpio, error);
0326 return error;
0327 }
0328
0329 bdata->gpiod = gpio_to_desc(button->gpio);
0330 if (!bdata->gpiod) {
0331 dev_err(dev,
0332 "unable to convert gpio %u to descriptor\n",
0333 button->gpio);
0334 return -EINVAL;
0335 }
0336 }
0337
0338 bdata->last_state = -1;
0339 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
0340 pdata->poll_interval);
0341
0342 input_set_capability(input, type, button->code);
0343 if (type == EV_ABS)
0344 gpio_keys_polled_set_abs_params(input, pdata,
0345 button->code);
0346 }
0347
0348 fwnode_handle_put(child);
0349
0350 bdev->input = input;
0351 bdev->dev = dev;
0352 bdev->pdata = pdata;
0353
0354 error = input_setup_polling(input, gpio_keys_polled_poll);
0355 if (error) {
0356 dev_err(dev, "unable to set up polling, err=%d\n", error);
0357 return error;
0358 }
0359
0360 input_set_poll_interval(input, pdata->poll_interval);
0361
0362 error = input_register_device(input);
0363 if (error) {
0364 dev_err(dev, "unable to register polled device, err=%d\n",
0365 error);
0366 return error;
0367 }
0368
0369
0370 for (i = 0; i < pdata->nbuttons; i++)
0371 gpio_keys_polled_check_state(input, &pdata->buttons[i],
0372 &bdev->data[i]);
0373
0374 input_sync(input);
0375
0376 return 0;
0377 }
0378
0379 static struct platform_driver gpio_keys_polled_driver = {
0380 .probe = gpio_keys_polled_probe,
0381 .driver = {
0382 .name = DRV_NAME,
0383 .of_match_table = gpio_keys_polled_of_match,
0384 },
0385 };
0386 module_platform_driver(gpio_keys_polled_driver);
0387
0388 MODULE_LICENSE("GPL v2");
0389 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
0390 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
0391 MODULE_ALIAS("platform:" DRV_NAME);