1 /* 2 * rotary_encoder.c 3 * 4 * (c) 2009 Daniel Mack <daniel@caiaq.de> 5 * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com> 6 * 7 * state machine code inspired by code from Tim Ruetz 8 * 9 * A generic driver for rotary encoders connected to GPIO lines. 10 * See file:Documentation/input/rotary-encoder.txt for more information 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> 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/consumer.h> 24 #include <linux/slab.h> 25 #include <linux/of.h> 26 #include <linux/pm.h> 27 #include <linux/property.h> 28 29 #define DRV_NAME "rotary-encoder" 30 31 struct rotary_encoder { 32 struct input_dev *input; 33 34 struct mutex access_mutex; 35 36 u32 steps; 37 u32 axis; 38 bool relative_axis; 39 bool rollover; 40 41 unsigned int pos; 42 43 struct gpio_descs *gpios; 44 45 unsigned int *irq; 46 47 bool armed; 48 signed char dir; /* 1 - clockwise, -1 - CCW */ 49 50 unsigned int last_stable; 51 }; 52 53 static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder) 54 { 55 int i; 56 unsigned int ret = 0; 57 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; 68 } 69 70 static void rotary_encoder_report_event(struct rotary_encoder *encoder) 71 { 72 if (encoder->relative_axis) { 73 input_report_rel(encoder->input, 74 encoder->axis, encoder->dir); 75 } else { 76 unsigned int pos = encoder->pos; 77 78 if (encoder->dir < 0) { 79 /* turning counter-clockwise */ 80 if (encoder->rollover) 81 pos += encoder->steps; 82 if (pos) 83 pos--; 84 } else { 85 /* turning clockwise */ 86 if (encoder->rollover || pos < encoder->steps) 87 pos++; 88 } 89 90 if (encoder->rollover) 91 pos %= encoder->steps; 92 93 encoder->pos = pos; 94 input_report_abs(encoder->input, encoder->axis, encoder->pos); 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; 103 unsigned int state; 104 105 mutex_lock(&encoder->access_mutex); 106 107 state = rotary_encoder_get_state(encoder); 108 109 switch (state) { 110 case 0x0: 111 if (encoder->armed) { 112 rotary_encoder_report_event(encoder); 113 encoder->armed = false; 114 } 115 break; 116 117 case 0x1: 118 case 0x3: 119 if (encoder->armed) 120 encoder->dir = 2 - state; 121 break; 122 123 case 0x2: 124 encoder->armed = true; 125 break; 126 } 127 128 mutex_unlock(&encoder->access_mutex); 129 130 return IRQ_HANDLED; 131 } 132 133 static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id) 134 { 135 struct rotary_encoder *encoder = dev_id; 136 unsigned int state; 137 138 mutex_lock(&encoder->access_mutex); 139 140 state = rotary_encoder_get_state(encoder); 141 142 if (state & 1) { 143 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1; 144 } else { 145 if (state != encoder->last_stable) { 146 rotary_encoder_report_event(encoder); 147 encoder->last_stable = state; 148 } 149 } 150 151 mutex_unlock(&encoder->access_mutex); 152 153 return IRQ_HANDLED; 154 } 155 156 static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id) 157 { 158 struct rotary_encoder *encoder = dev_id; 159 unsigned int state; 160 161 mutex_lock(&encoder->access_mutex); 162 163 state = rotary_encoder_get_state(encoder); 164 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 170 goto out; 171 172 rotary_encoder_report_event(encoder); 173 174 out: 175 encoder->last_stable = state; 176 mutex_unlock(&encoder->access_mutex); 177 178 return IRQ_HANDLED; 179 } 180 181 static int rotary_encoder_probe(struct platform_device *pdev) 182 { 183 struct device *dev = &pdev->dev; 184 struct rotary_encoder *encoder; 185 struct input_dev *input; 186 irq_handler_t handler; 187 u32 steps_per_period; 188 unsigned int i; 189 int err; 190 191 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL); 192 if (!encoder) 193 return -ENOMEM; 194 195 mutex_init(&encoder->access_mutex); 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"); 219 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); 224 } 225 if (encoder->gpios->ndescs < 2) { 226 dev_err(dev, "not enough gpios found\n"); 227 return -EINVAL; 228 } 229 230 input = devm_input_allocate_device(dev); 231 if (!input) 232 return -ENOMEM; 233 234 encoder->input = input; 235 236 input->name = pdev->name; 237 input->id.bustype = BUS_HOST; 238 input->dev.parent = dev; 239 240 if (encoder->relative_axis) 241 input_set_capability(input, EV_REL, encoder->axis); 242 else 243 input_set_abs_params(input, 244 encoder->axis, 0, encoder->steps, 0, 1); 245 246 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) { 247 case 4: 248 handler = &rotary_encoder_quarter_period_irq; 249 encoder->last_stable = rotary_encoder_get_state(encoder); 250 break; 251 case 2: 252 handler = &rotary_encoder_half_period_irq; 253 encoder->last_stable = rotary_encoder_get_state(encoder); 254 break; 255 case 1: 256 handler = &rotary_encoder_irq; 257 break; 258 default: 259 dev_err(dev, "'%d' is not a valid steps-per-period value\n", 260 steps_per_period); 261 return -EINVAL; 262 } 263 264 encoder->irq = 265 devm_kzalloc(dev, 266 sizeof(*encoder->irq) * encoder->gpios->ndescs, 267 GFP_KERNEL); 268 if (!encoder->irq) 269 return -ENOMEM; 270 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, 276 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | 277 IRQF_ONESHOT, 278 DRV_NAME, encoder); 279 if (err) { 280 dev_err(dev, "unable to request IRQ %d (gpio#%d)\n", 281 encoder->irq[i], i); 282 return err; 283 } 284 } 285 286 err = input_register_device(input); 287 if (err) { 288 dev_err(dev, "failed to register input device\n"); 289 return err; 290 } 291 292 device_init_wakeup(dev, 293 device_property_read_bool(dev, "wakeup-source")); 294 295 platform_set_drvdata(pdev, encoder); 296 297 return 0; 298 } 299 300 static int __maybe_unused rotary_encoder_suspend(struct device *dev) 301 { 302 struct rotary_encoder *encoder = dev_get_drvdata(dev); 303 unsigned int i; 304 305 if (device_may_wakeup(dev)) { 306 for (i = 0; i < encoder->gpios->ndescs; ++i) 307 enable_irq_wake(encoder->irq[i]); 308 } 309 310 return 0; 311 } 312 313 static int __maybe_unused rotary_encoder_resume(struct device *dev) 314 { 315 struct rotary_encoder *encoder = dev_get_drvdata(dev); 316 unsigned int i; 317 318 if (device_may_wakeup(dev)) { 319 for (i = 0; i < encoder->gpios->ndescs; ++i) 320 disable_irq_wake(encoder->irq[i]); 321 } 322 323 return 0; 324 } 325 326 static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops, 327 rotary_encoder_suspend, rotary_encoder_resume); 328 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 337 static struct platform_driver rotary_encoder_driver = { 338 .probe = rotary_encoder_probe, 339 .driver = { 340 .name = DRV_NAME, 341 .pm = &rotary_encoder_pm_ops, 342 .of_match_table = of_match_ptr(rotary_encoder_of_match), 343 } 344 }; 345 module_platform_driver(rotary_encoder_driver); 346 347 MODULE_ALIAS("platform:" DRV_NAME); 348 MODULE_DESCRIPTION("GPIO rotary encoder driver"); 349 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold"); 350 MODULE_LICENSE("GPL v2"); 351