Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * rotary_encoder.c |
| 3 | * |
| 4 | * (c) 2009 Daniel Mack <daniel@caiaq.de> |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 5 | * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com> |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 6 | * |
| 7 | * state machine code inspired by code from Tim Ruetz |
| 8 | * |
| 9 | * A generic driver for rotary encoders connected to GPIO lines. |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 10 | * See file:Documentation/input/rotary-encoder.txt for more information |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/platform_device.h> |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Sachin Kamat | 2e45e53 | 2013-10-06 00:56:24 -0700 | [diff] [blame] | 25 | #include <linux/of.h> |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 26 | #include <linux/pm.h> |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 27 | #include <linux/property.h> |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 28 | |
| 29 | #define DRV_NAME "rotary-encoder" |
| 30 | |
| 31 | struct rotary_encoder { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 32 | struct input_dev *input; |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 33 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 34 | struct mutex access_mutex; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 35 | |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 36 | u32 steps; |
| 37 | u32 axis; |
| 38 | bool relative_axis; |
| 39 | bool rollover; |
| 40 | |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 41 | unsigned int pos; |
| 42 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 43 | struct gpio_descs *gpios; |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 44 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 45 | unsigned int *irq; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 46 | |
| 47 | bool armed; |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 48 | signed char dir; /* 1 - clockwise, -1 - CCW */ |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 49 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 50 | unsigned last_stable; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 53 | static unsigned rotary_encoder_get_state(struct rotary_encoder *encoder) |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 54 | { |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 55 | int i; |
| 56 | unsigned ret = 0; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 57 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 58 | for (i = 0; i < encoder->gpios->ndescs; ++i) { |
| 59 | int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]); |
| 60 | /* convert from gray encoding to normal */ |
| 61 | if (ret & 1) |
| 62 | val = !val; |
| 63 | |
| 64 | ret = ret << 1 | val; |
| 65 | } |
| 66 | |
| 67 | return ret & 3; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void rotary_encoder_report_event(struct rotary_encoder *encoder) |
| 71 | { |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 72 | if (encoder->relative_axis) { |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 73 | input_report_rel(encoder->input, |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 74 | encoder->axis, encoder->dir); |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 75 | } else { |
| 76 | unsigned int pos = encoder->pos; |
| 77 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 78 | if (encoder->dir < 0) { |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 79 | /* turning counter-clockwise */ |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 80 | if (encoder->rollover) |
| 81 | pos += encoder->steps; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 82 | if (pos) |
| 83 | pos--; |
| 84 | } else { |
| 85 | /* turning clockwise */ |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 86 | if (encoder->rollover || pos < encoder->steps) |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 87 | pos++; |
| 88 | } |
| 89 | |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 90 | if (encoder->rollover) |
| 91 | pos %= encoder->steps; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 92 | |
| 93 | encoder->pos = pos; |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 94 | input_report_abs(encoder->input, encoder->axis, encoder->pos); |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | input_sync(encoder->input); |
| 98 | } |
| 99 | |
| 100 | static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) |
| 101 | { |
| 102 | struct rotary_encoder *encoder = dev_id; |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 103 | unsigned state; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 104 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 105 | mutex_lock(&encoder->access_mutex); |
| 106 | |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 107 | state = rotary_encoder_get_state(encoder); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 108 | |
| 109 | switch (state) { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 110 | case 0x0: |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 111 | if (encoder->armed) { |
| 112 | rotary_encoder_report_event(encoder); |
| 113 | encoder->armed = false; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 114 | } |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 115 | break; |
| 116 | |
| 117 | case 0x1: |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 118 | case 0x3: |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 119 | if (encoder->armed) |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 120 | encoder->dir = 2 - state; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 121 | break; |
| 122 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 123 | case 0x2: |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 124 | encoder->armed = true; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 128 | mutex_unlock(&encoder->access_mutex); |
| 129 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 130 | return IRQ_HANDLED; |
| 131 | } |
| 132 | |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 133 | static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id) |
| 134 | { |
| 135 | struct rotary_encoder *encoder = dev_id; |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 136 | unsigned int state; |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 137 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 138 | mutex_lock(&encoder->access_mutex); |
| 139 | |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 140 | state = rotary_encoder_get_state(encoder); |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 141 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 142 | if (state & 1) { |
| 143 | encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1; |
| 144 | } else { |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 145 | if (state != encoder->last_stable) { |
| 146 | rotary_encoder_report_event(encoder); |
| 147 | encoder->last_stable = state; |
| 148 | } |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 151 | mutex_unlock(&encoder->access_mutex); |
| 152 | |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 153 | return IRQ_HANDLED; |
| 154 | } |
| 155 | |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 156 | static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id) |
| 157 | { |
| 158 | struct rotary_encoder *encoder = dev_id; |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 159 | unsigned int state; |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 160 | |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 161 | mutex_lock(&encoder->access_mutex); |
| 162 | |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 163 | state = rotary_encoder_get_state(encoder); |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 164 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 165 | if ((encoder->last_stable + 1) % 4 == state) |
| 166 | encoder->dir = 1; |
| 167 | else if (encoder->last_stable == (state + 1) % 4) |
| 168 | encoder->dir = -1; |
| 169 | else |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 170 | goto out; |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 171 | |
| 172 | rotary_encoder_report_event(encoder); |
| 173 | |
| 174 | out: |
| 175 | encoder->last_stable = state; |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 176 | mutex_unlock(&encoder->access_mutex); |
| 177 | |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 178 | return IRQ_HANDLED; |
| 179 | } |
| 180 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 181 | static int rotary_encoder_probe(struct platform_device *pdev) |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 182 | { |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 183 | struct device *dev = &pdev->dev; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 184 | struct rotary_encoder *encoder; |
| 185 | struct input_dev *input; |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 186 | irq_handler_t handler; |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 187 | u32 steps_per_period; |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 188 | unsigned int i; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 189 | int err; |
| 190 | |
Timo Teräs | d9202af | 2016-01-16 20:22:07 -0800 | [diff] [blame] | 191 | encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL); |
| 192 | if (!encoder) |
| 193 | return -ENOMEM; |
| 194 | |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 195 | mutex_init(&encoder->access_mutex); |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 196 | |
| 197 | device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps); |
| 198 | |
| 199 | err = device_property_read_u32(dev, "rotary-encoder,steps-per-period", |
| 200 | &steps_per_period); |
| 201 | if (err) { |
| 202 | /* |
| 203 | * The 'half-period' property has been deprecated, you must |
| 204 | * use 'steps-per-period' and set an appropriate value, but |
| 205 | * we still need to parse it to maintain compatibility. If |
| 206 | * neither property is present we fall back to the one step |
| 207 | * per period behavior. |
| 208 | */ |
| 209 | steps_per_period = device_property_read_bool(dev, |
| 210 | "rotary-encoder,half-period") ? 2 : 1; |
| 211 | } |
| 212 | |
| 213 | encoder->rollover = |
| 214 | device_property_read_bool(dev, "rotary-encoder,rollover"); |
| 215 | |
| 216 | device_property_read_u32(dev, "linux,axis", &encoder->axis); |
| 217 | encoder->relative_axis = |
| 218 | device_property_read_bool(dev, "rotary-encoder,relative-axis"); |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 219 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 220 | encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN); |
| 221 | if (IS_ERR(encoder->gpios)) { |
| 222 | dev_err(dev, "unable to get gpios\n"); |
| 223 | return PTR_ERR(encoder->gpios); |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 224 | } |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 225 | if (encoder->gpios->ndescs < 2) { |
| 226 | dev_err(dev, "not enough gpios found\n"); |
| 227 | return -EINVAL; |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Timo Teräs | d9202af | 2016-01-16 20:22:07 -0800 | [diff] [blame] | 230 | input = devm_input_allocate_device(dev); |
| 231 | if (!input) |
| 232 | return -ENOMEM; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 233 | |
| 234 | encoder->input = input; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 235 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 236 | input->name = pdev->name; |
| 237 | input->id.bustype = BUS_HOST; |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 238 | input->dev.parent = dev; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 239 | |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 240 | if (encoder->relative_axis) |
| 241 | input_set_capability(input, EV_REL, encoder->axis); |
Dmitry Torokhov | 8631580 | 2016-01-17 10:52:54 -0800 | [diff] [blame] | 242 | else |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 243 | input_set_abs_params(input, |
| 244 | encoder->axis, 0, encoder->steps, 0, 1); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 245 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 246 | switch (steps_per_period >> (encoder->gpios->ndescs - 2)) { |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 247 | case 4: |
| 248 | handler = &rotary_encoder_quarter_period_irq; |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 249 | encoder->last_stable = rotary_encoder_get_state(encoder); |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 250 | break; |
| 251 | case 2: |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 252 | handler = &rotary_encoder_half_period_irq; |
Dmitry Torokhov | 77a8f0a | 2016-01-17 10:18:12 -0800 | [diff] [blame] | 253 | encoder->last_stable = rotary_encoder_get_state(encoder); |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 254 | break; |
| 255 | case 1: |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 256 | handler = &rotary_encoder_irq; |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 257 | break; |
| 258 | default: |
| 259 | dev_err(dev, "'%d' is not a valid steps-per-period value\n", |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 260 | steps_per_period); |
Timo Teräs | d9202af | 2016-01-16 20:22:07 -0800 | [diff] [blame] | 261 | return -EINVAL; |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 264 | encoder->irq = |
| 265 | devm_kzalloc(dev, |
| 266 | sizeof(*encoder->irq) * encoder->gpios->ndescs, |
| 267 | GFP_KERNEL); |
| 268 | if (!encoder->irq) |
| 269 | return -ENOMEM; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 270 | |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 271 | for (i = 0; i < encoder->gpios->ndescs; ++i) { |
| 272 | encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]); |
| 273 | |
| 274 | err = devm_request_threaded_irq(dev, encoder->irq[i], |
| 275 | NULL, handler, |
Timo Teräs | dee520e | 2016-01-16 20:46:24 -0800 | [diff] [blame] | 276 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | |
| 277 | IRQF_ONESHOT, |
| 278 | DRV_NAME, encoder); |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 279 | if (err) { |
| 280 | dev_err(dev, "unable to request IRQ %d (gpio#%d)\n", |
| 281 | encoder->irq[i], i); |
| 282 | return err; |
| 283 | } |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 286 | err = input_register_device(input); |
| 287 | if (err) { |
| 288 | dev_err(dev, "failed to register input device\n"); |
Timo Teräs | d9202af | 2016-01-16 20:22:07 -0800 | [diff] [blame] | 289 | return err; |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 292 | device_init_wakeup(dev, |
| 293 | device_property_read_bool(dev, "wakeup-source")); |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 294 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 295 | platform_set_drvdata(pdev, encoder); |
| 296 | |
| 297 | return 0; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Dmitry Torokhov | 6a6f70b | 2016-01-16 20:50:06 -0800 | [diff] [blame] | 300 | static int __maybe_unused rotary_encoder_suspend(struct device *dev) |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 301 | { |
| 302 | struct rotary_encoder *encoder = dev_get_drvdata(dev); |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 303 | unsigned int i; |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 304 | |
| 305 | if (device_may_wakeup(dev)) { |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 306 | for (i = 0; i < encoder->gpios->ndescs; ++i) |
| 307 | enable_irq_wake(encoder->irq[i]); |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Dmitry Torokhov | 6a6f70b | 2016-01-16 20:50:06 -0800 | [diff] [blame] | 313 | static int __maybe_unused rotary_encoder_resume(struct device *dev) |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct rotary_encoder *encoder = dev_get_drvdata(dev); |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 316 | unsigned int i; |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 317 | |
| 318 | if (device_may_wakeup(dev)) { |
Uwe Kleine-König | 7dde4e7 | 2016-03-01 18:25:26 -0800 | [diff] [blame] | 319 | for (i = 0; i < encoder->gpios->ndescs; ++i) |
| 320 | disable_irq_wake(encoder->irq[i]); |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 325 | |
| 326 | static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops, |
Dmitry Torokhov | 6a6f70b | 2016-01-16 20:50:06 -0800 | [diff] [blame] | 327 | rotary_encoder_suspend, rotary_encoder_resume); |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 328 | |
Dmitry Torokhov | a9e340d | 2016-01-17 10:57:09 -0800 | [diff] [blame] | 329 | #ifdef CONFIG_OF |
| 330 | static const struct of_device_id rotary_encoder_of_match[] = { |
| 331 | { .compatible = "rotary-encoder", }, |
| 332 | { }, |
| 333 | }; |
| 334 | MODULE_DEVICE_TABLE(of, rotary_encoder_of_match); |
| 335 | #endif |
| 336 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 337 | static struct platform_driver rotary_encoder_driver = { |
| 338 | .probe = rotary_encoder_probe, |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 339 | .driver = { |
| 340 | .name = DRV_NAME, |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 341 | .pm = &rotary_encoder_pm_ops, |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 342 | .of_match_table = of_match_ptr(rotary_encoder_of_match), |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 343 | } |
| 344 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 345 | module_platform_driver(rotary_encoder_driver); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 346 | |
| 347 | MODULE_ALIAS("platform:" DRV_NAME); |
| 348 | MODULE_DESCRIPTION("GPIO rotary encoder driver"); |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 349 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold"); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 350 | MODULE_LICENSE("GPL v2"); |