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> |
| 23 | #include <linux/gpio.h> |
| 24 | #include <linux/rotary_encoder.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Sachin Kamat | 2e45e53 | 2013-10-06 00:56:24 -0700 | [diff] [blame] | 26 | #include <linux/of.h> |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/of_gpio.h> |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 29 | #include <linux/pm.h> |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 30 | |
| 31 | #define DRV_NAME "rotary-encoder" |
| 32 | |
| 33 | struct rotary_encoder { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 34 | struct input_dev *input; |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 35 | const struct rotary_encoder_platform_data *pdata; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 36 | |
| 37 | unsigned int axis; |
| 38 | unsigned int pos; |
| 39 | |
| 40 | unsigned int irq_a; |
| 41 | unsigned int irq_b; |
| 42 | |
| 43 | bool armed; |
| 44 | unsigned char dir; /* 0 - clockwise, 1 - CCW */ |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 45 | |
| 46 | char last_stable; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 49 | static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata) |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 50 | { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 51 | int a = !!gpio_get_value(pdata->gpio_a); |
| 52 | int b = !!gpio_get_value(pdata->gpio_b); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 53 | |
| 54 | a ^= pdata->inverted_a; |
| 55 | b ^= pdata->inverted_b; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 56 | |
| 57 | return ((a << 1) | b); |
| 58 | } |
| 59 | |
| 60 | static void rotary_encoder_report_event(struct rotary_encoder *encoder) |
| 61 | { |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 62 | const struct rotary_encoder_platform_data *pdata = encoder->pdata; |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 63 | |
| 64 | if (pdata->relative_axis) { |
| 65 | input_report_rel(encoder->input, |
| 66 | pdata->axis, encoder->dir ? -1 : 1); |
| 67 | } else { |
| 68 | unsigned int pos = encoder->pos; |
| 69 | |
| 70 | if (encoder->dir) { |
| 71 | /* turning counter-clockwise */ |
| 72 | if (pdata->rollover) |
| 73 | pos += pdata->steps; |
| 74 | if (pos) |
| 75 | pos--; |
| 76 | } else { |
| 77 | /* turning clockwise */ |
| 78 | if (pdata->rollover || pos < pdata->steps) |
| 79 | pos++; |
| 80 | } |
| 81 | |
| 82 | if (pdata->rollover) |
| 83 | pos %= pdata->steps; |
| 84 | |
| 85 | encoder->pos = pos; |
| 86 | input_report_abs(encoder->input, pdata->axis, encoder->pos); |
| 87 | } |
| 88 | |
| 89 | input_sync(encoder->input); |
| 90 | } |
| 91 | |
| 92 | static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) |
| 93 | { |
| 94 | struct rotary_encoder *encoder = dev_id; |
| 95 | int state; |
| 96 | |
| 97 | state = rotary_encoder_get_state(encoder->pdata); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 98 | |
| 99 | switch (state) { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 100 | case 0x0: |
Johan Hovold | 521a8f5 | 2011-05-11 16:33:50 -0700 | [diff] [blame] | 101 | if (encoder->armed) { |
| 102 | rotary_encoder_report_event(encoder); |
| 103 | encoder->armed = false; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 104 | } |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 105 | break; |
| 106 | |
| 107 | case 0x1: |
| 108 | case 0x2: |
| 109 | if (encoder->armed) |
| 110 | encoder->dir = state - 1; |
| 111 | break; |
| 112 | |
| 113 | case 0x3: |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 114 | encoder->armed = true; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | |
| 118 | return IRQ_HANDLED; |
| 119 | } |
| 120 | |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 121 | static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id) |
| 122 | { |
| 123 | struct rotary_encoder *encoder = dev_id; |
| 124 | int state; |
| 125 | |
| 126 | state = rotary_encoder_get_state(encoder->pdata); |
| 127 | |
| 128 | switch (state) { |
| 129 | case 0x00: |
| 130 | case 0x03: |
| 131 | if (state != encoder->last_stable) { |
| 132 | rotary_encoder_report_event(encoder); |
| 133 | encoder->last_stable = state; |
| 134 | } |
| 135 | break; |
| 136 | |
| 137 | case 0x01: |
| 138 | case 0x02: |
| 139 | encoder->dir = (encoder->last_stable + state) & 0x01; |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | return IRQ_HANDLED; |
| 144 | } |
| 145 | |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 146 | static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id) |
| 147 | { |
| 148 | struct rotary_encoder *encoder = dev_id; |
| 149 | unsigned char sum; |
| 150 | int state; |
| 151 | |
| 152 | state = rotary_encoder_get_state(encoder->pdata); |
| 153 | |
| 154 | /* |
| 155 | * We encode the previous and the current state using a byte. |
| 156 | * The previous state in the MSB nibble, the current state in the LSB |
| 157 | * nibble. Then use a table to decide the direction of the turn. |
| 158 | */ |
| 159 | sum = (encoder->last_stable << 4) + state; |
| 160 | switch (sum) { |
| 161 | case 0x31: |
| 162 | case 0x10: |
| 163 | case 0x02: |
| 164 | case 0x23: |
| 165 | encoder->dir = 0; /* clockwise */ |
| 166 | break; |
| 167 | |
| 168 | case 0x13: |
| 169 | case 0x01: |
| 170 | case 0x20: |
| 171 | case 0x32: |
| 172 | encoder->dir = 1; /* counter-clockwise */ |
| 173 | break; |
| 174 | |
| 175 | default: |
| 176 | /* |
| 177 | * Ignore all other values. This covers the case when the |
| 178 | * state didn't change (a spurious interrupt) and the |
| 179 | * cases where the state changed by two steps, making it |
| 180 | * impossible to tell the direction. |
| 181 | * |
| 182 | * In either case, don't report any event and save the |
| 183 | * state for later. |
| 184 | */ |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | rotary_encoder_report_event(encoder); |
| 189 | |
| 190 | out: |
| 191 | encoder->last_stable = state; |
| 192 | return IRQ_HANDLED; |
| 193 | } |
| 194 | |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 195 | #ifdef CONFIG_OF |
Jingoo Han | 5f155ee | 2014-05-07 13:03:00 -0700 | [diff] [blame] | 196 | static const struct of_device_id rotary_encoder_of_match[] = { |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 197 | { .compatible = "rotary-encoder", }, |
| 198 | { }, |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(of, rotary_encoder_of_match); |
| 201 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 202 | static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct device *dev) |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 203 | { |
| 204 | const struct of_device_id *of_id = |
| 205 | of_match_device(rotary_encoder_of_match, dev); |
| 206 | struct device_node *np = dev->of_node; |
| 207 | struct rotary_encoder_platform_data *pdata; |
| 208 | enum of_gpio_flags flags; |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 209 | int error; |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 210 | |
| 211 | if (!of_id || !np) |
| 212 | return NULL; |
| 213 | |
| 214 | pdata = kzalloc(sizeof(struct rotary_encoder_platform_data), |
| 215 | GFP_KERNEL); |
| 216 | if (!pdata) |
| 217 | return ERR_PTR(-ENOMEM); |
| 218 | |
| 219 | of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps); |
| 220 | of_property_read_u32(np, "linux,axis", &pdata->axis); |
| 221 | |
| 222 | pdata->gpio_a = of_get_gpio_flags(np, 0, &flags); |
| 223 | pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW; |
| 224 | |
| 225 | pdata->gpio_b = of_get_gpio_flags(np, 1, &flags); |
| 226 | pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW; |
| 227 | |
Ben Gamari | 648b15c | 2015-10-13 23:37:28 -0700 | [diff] [blame] | 228 | pdata->relative_axis = |
| 229 | of_property_read_bool(np, "rotary-encoder,relative-axis"); |
| 230 | pdata->rollover = of_property_read_bool(np, "rotary-encoder,rollover"); |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 231 | |
| 232 | error = of_property_read_u32(np, "rotary-encoder,steps-per-period", |
| 233 | &pdata->steps_per_period); |
| 234 | if (error) { |
| 235 | /* |
| 236 | * The 'half-period' property has been deprecated, you must use |
| 237 | * 'steps-per-period' and set an appropriate value, but we still |
| 238 | * need to parse it to maintain compatibility. |
| 239 | */ |
| 240 | if (of_property_read_bool(np, "rotary-encoder,half-period")) { |
| 241 | pdata->steps_per_period = 2; |
| 242 | } else { |
| 243 | /* Fallback to one step per period behavior */ |
| 244 | pdata->steps_per_period = 1; |
| 245 | } |
| 246 | } |
| 247 | |
Ben Gamari | 648b15c | 2015-10-13 23:37:28 -0700 | [diff] [blame] | 248 | pdata->wakeup_source = of_property_read_bool(np, "wakeup-source"); |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 249 | |
| 250 | return pdata; |
| 251 | } |
| 252 | #else |
| 253 | static inline struct rotary_encoder_platform_data * |
| 254 | rotary_encoder_parse_dt(struct device *dev) |
| 255 | { |
| 256 | return NULL; |
| 257 | } |
| 258 | #endif |
| 259 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 260 | static int rotary_encoder_probe(struct platform_device *pdev) |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 261 | { |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 262 | struct device *dev = &pdev->dev; |
| 263 | const struct rotary_encoder_platform_data *pdata = dev_get_platdata(dev); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 264 | struct rotary_encoder *encoder; |
| 265 | struct input_dev *input; |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 266 | irq_handler_t handler; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 267 | int err; |
| 268 | |
H Hartley Sweeten | 06ee3d3 | 2009-10-05 21:43:42 -0700 | [diff] [blame] | 269 | if (!pdata) { |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 270 | pdata = rotary_encoder_parse_dt(dev); |
| 271 | if (IS_ERR(pdata)) |
| 272 | return PTR_ERR(pdata); |
| 273 | |
| 274 | if (!pdata) { |
| 275 | dev_err(dev, "missing platform data\n"); |
| 276 | return -EINVAL; |
| 277 | } |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL); |
| 281 | input = input_allocate_device(); |
| 282 | if (!encoder || !input) { |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 283 | err = -ENOMEM; |
| 284 | goto exit_free_mem; |
| 285 | } |
| 286 | |
| 287 | encoder->input = input; |
| 288 | encoder->pdata = pdata; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 289 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 290 | input->name = pdev->name; |
| 291 | input->id.bustype = BUS_HOST; |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 292 | input->dev.parent = dev; |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 293 | |
| 294 | if (pdata->relative_axis) { |
| 295 | input->evbit[0] = BIT_MASK(EV_REL); |
| 296 | input->relbit[0] = BIT_MASK(pdata->axis); |
| 297 | } else { |
| 298 | input->evbit[0] = BIT_MASK(EV_ABS); |
| 299 | input_set_abs_params(encoder->input, |
| 300 | pdata->axis, 0, pdata->steps, 0, 1); |
| 301 | } |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 302 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 303 | /* request the GPIOs */ |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 304 | err = gpio_request_one(pdata->gpio_a, GPIOF_IN, dev_name(dev)); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 305 | if (err) { |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 306 | dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a); |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 307 | goto exit_free_mem; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 310 | err = gpio_request_one(pdata->gpio_b, GPIOF_IN, dev_name(dev)); |
Andrew Clayton | 5deeac9 | 2010-02-10 23:18:05 -0800 | [diff] [blame] | 311 | if (err) { |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 312 | dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b); |
Andrew Clayton | 5deeac9 | 2010-02-10 23:18:05 -0800 | [diff] [blame] | 313 | goto exit_free_gpio_a; |
| 314 | } |
| 315 | |
Daniel Mack | a78769b | 2012-07-31 22:08:47 -0700 | [diff] [blame] | 316 | encoder->irq_a = gpio_to_irq(pdata->gpio_a); |
| 317 | encoder->irq_b = gpio_to_irq(pdata->gpio_b); |
| 318 | |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 319 | switch (pdata->steps_per_period) { |
| 320 | case 4: |
| 321 | handler = &rotary_encoder_quarter_period_irq; |
| 322 | encoder->last_stable = rotary_encoder_get_state(pdata); |
| 323 | break; |
| 324 | case 2: |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 325 | handler = &rotary_encoder_half_period_irq; |
| 326 | encoder->last_stable = rotary_encoder_get_state(pdata); |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 327 | break; |
| 328 | case 1: |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 329 | handler = &rotary_encoder_irq; |
Ezequiel Garcia | 3a341a4 | 2015-10-13 23:39:50 -0700 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | dev_err(dev, "'%d' is not a valid steps-per-period value\n", |
| 333 | pdata->steps_per_period); |
| 334 | err = -EINVAL; |
| 335 | goto exit_free_gpio_b; |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | err = request_irq(encoder->irq_a, handler, |
Alexander Stein | e0d5f4c | 2011-02-02 22:59:54 -0800 | [diff] [blame] | 339 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 340 | DRV_NAME, encoder); |
| 341 | if (err) { |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 342 | dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 343 | goto exit_free_gpio_b; |
| 344 | } |
| 345 | |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 346 | err = request_irq(encoder->irq_b, handler, |
Alexander Stein | e0d5f4c | 2011-02-02 22:59:54 -0800 | [diff] [blame] | 347 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 348 | DRV_NAME, encoder); |
| 349 | if (err) { |
Daniel Mack | 429a34d | 2012-07-31 22:08:48 -0700 | [diff] [blame] | 350 | dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 351 | goto exit_free_irq_a; |
| 352 | } |
| 353 | |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 354 | err = input_register_device(input); |
| 355 | if (err) { |
| 356 | dev_err(dev, "failed to register input device\n"); |
| 357 | goto exit_free_irq_b; |
| 358 | } |
| 359 | |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 360 | device_init_wakeup(&pdev->dev, pdata->wakeup_source); |
| 361 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 362 | platform_set_drvdata(pdev, encoder); |
| 363 | |
| 364 | return 0; |
| 365 | |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 366 | exit_free_irq_b: |
| 367 | free_irq(encoder->irq_b, encoder); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 368 | exit_free_irq_a: |
| 369 | free_irq(encoder->irq_a, encoder); |
| 370 | exit_free_gpio_b: |
| 371 | gpio_free(pdata->gpio_b); |
| 372 | exit_free_gpio_a: |
| 373 | gpio_free(pdata->gpio_a); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 374 | exit_free_mem: |
| 375 | input_free_device(input); |
| 376 | kfree(encoder); |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 377 | if (!dev_get_platdata(&pdev->dev)) |
| 378 | kfree(pdata); |
| 379 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 380 | return err; |
| 381 | } |
| 382 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 383 | static int rotary_encoder_remove(struct platform_device *pdev) |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 384 | { |
| 385 | struct rotary_encoder *encoder = platform_get_drvdata(pdev); |
Dmitry Torokhov | ce91953 | 2012-07-31 22:08:49 -0700 | [diff] [blame] | 386 | const struct rotary_encoder_platform_data *pdata = encoder->pdata; |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 387 | |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 388 | device_init_wakeup(&pdev->dev, false); |
| 389 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 390 | free_irq(encoder->irq_a, encoder); |
| 391 | free_irq(encoder->irq_b, encoder); |
| 392 | gpio_free(pdata->gpio_a); |
| 393 | gpio_free(pdata->gpio_b); |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 394 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 395 | input_unregister_device(encoder->input); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 396 | kfree(encoder); |
| 397 | |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 398 | if (!dev_get_platdata(&pdev->dev)) |
| 399 | kfree(pdata); |
| 400 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 404 | #ifdef CONFIG_PM_SLEEP |
| 405 | static int rotary_encoder_suspend(struct device *dev) |
| 406 | { |
| 407 | struct rotary_encoder *encoder = dev_get_drvdata(dev); |
| 408 | |
| 409 | if (device_may_wakeup(dev)) { |
| 410 | enable_irq_wake(encoder->irq_a); |
| 411 | enable_irq_wake(encoder->irq_b); |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int rotary_encoder_resume(struct device *dev) |
| 418 | { |
| 419 | struct rotary_encoder *encoder = dev_get_drvdata(dev); |
| 420 | |
| 421 | if (device_may_wakeup(dev)) { |
| 422 | disable_irq_wake(encoder->irq_a); |
| 423 | disable_irq_wake(encoder->irq_b); |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | #endif |
| 429 | |
| 430 | static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops, |
| 431 | rotary_encoder_suspend, rotary_encoder_resume); |
| 432 | |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 433 | static struct platform_driver rotary_encoder_driver = { |
| 434 | .probe = rotary_encoder_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 435 | .remove = rotary_encoder_remove, |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 436 | .driver = { |
| 437 | .name = DRV_NAME, |
Sylvain Rochet | 47ec6e5 | 2015-10-13 23:24:36 -0700 | [diff] [blame] | 438 | .pm = &rotary_encoder_pm_ops, |
Daniel Mack | 80c99bc | 2012-07-31 22:08:50 -0700 | [diff] [blame] | 439 | .of_match_table = of_match_ptr(rotary_encoder_of_match), |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 440 | } |
| 441 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 442 | module_platform_driver(rotary_encoder_driver); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 443 | |
| 444 | MODULE_ALIAS("platform:" DRV_NAME); |
| 445 | MODULE_DESCRIPTION("GPIO rotary encoder driver"); |
Johan Hovold | e70bdd4 | 2011-05-11 16:35:30 -0700 | [diff] [blame] | 446 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold"); |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 447 | MODULE_LICENSE("GPL v2"); |