1 /* 2 * rotary_encoder.c 3 * 4 * (c) 2009 Daniel Mack <daniel@caiaq.de> 5 * 6 * state machine code inspired by code from Tim Ruetz 7 * 8 * A generic driver for rotary encoders connected to GPIO lines. 9 * See file:Documentation/input/rotary_encoder.txt for more information 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/init.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.h> 24 #include <linux/rotary_encoder.h> 25 #include <linux/slab.h> 26 27 #define DRV_NAME "rotary-encoder" 28 29 struct rotary_encoder { 30 struct input_dev *input; 31 struct rotary_encoder_platform_data *pdata; 32 33 unsigned int axis; 34 unsigned int pos; 35 36 unsigned int irq_a; 37 unsigned int irq_b; 38 39 bool armed; 40 unsigned char dir; /* 0 - clockwise, 1 - CCW */ 41 }; 42 43 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) 44 { 45 struct rotary_encoder *encoder = dev_id; 46 struct rotary_encoder_platform_data *pdata = encoder->pdata; 47 int a = !!gpio_get_value(pdata->gpio_a); 48 int b = !!gpio_get_value(pdata->gpio_b); 49 int state; 50 51 a ^= pdata->inverted_a; 52 b ^= pdata->inverted_b; 53 state = (a << 1) | b; 54 55 switch (state) { 56 57 case 0x0: 58 if (!encoder->armed) 59 break; 60 61 if (pdata->relative_axis) { 62 input_report_rel(encoder->input, pdata->axis, 63 encoder->dir ? -1 : 1); 64 } else { 65 unsigned int pos = encoder->pos; 66 67 if (encoder->dir) { 68 /* turning counter-clockwise */ 69 if (pdata->rollover) 70 pos += pdata->steps; 71 if (pos) 72 pos--; 73 } else { 74 /* turning clockwise */ 75 if (pdata->rollover || pos < pdata->steps) 76 pos++; 77 } 78 if (pdata->rollover) 79 pos %= pdata->steps; 80 encoder->pos = pos; 81 input_report_abs(encoder->input, pdata->axis, 82 encoder->pos); 83 } 84 input_sync(encoder->input); 85 86 encoder->armed = false; 87 break; 88 89 case 0x1: 90 case 0x2: 91 if (encoder->armed) 92 encoder->dir = state - 1; 93 break; 94 95 case 0x3: 96 encoder->armed = true; 97 break; 98 } 99 100 return IRQ_HANDLED; 101 } 102 103 static int __devinit rotary_encoder_probe(struct platform_device *pdev) 104 { 105 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; 106 struct rotary_encoder *encoder; 107 struct input_dev *input; 108 int err; 109 110 if (!pdata) { 111 dev_err(&pdev->dev, "missing platform data\n"); 112 return -ENOENT; 113 } 114 115 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL); 116 input = input_allocate_device(); 117 if (!encoder || !input) { 118 dev_err(&pdev->dev, "failed to allocate memory for device\n"); 119 err = -ENOMEM; 120 goto exit_free_mem; 121 } 122 123 encoder->input = input; 124 encoder->pdata = pdata; 125 encoder->irq_a = gpio_to_irq(pdata->gpio_a); 126 encoder->irq_b = gpio_to_irq(pdata->gpio_b); 127 128 /* create and register the input driver */ 129 input->name = pdev->name; 130 input->id.bustype = BUS_HOST; 131 input->dev.parent = &pdev->dev; 132 133 if (pdata->relative_axis) { 134 input->evbit[0] = BIT_MASK(EV_REL); 135 input->relbit[0] = BIT_MASK(pdata->axis); 136 } else { 137 input->evbit[0] = BIT_MASK(EV_ABS); 138 input_set_abs_params(encoder->input, 139 pdata->axis, 0, pdata->steps, 0, 1); 140 } 141 142 err = input_register_device(input); 143 if (err) { 144 dev_err(&pdev->dev, "failed to register input device\n"); 145 goto exit_free_mem; 146 } 147 148 /* request the GPIOs */ 149 err = gpio_request(pdata->gpio_a, DRV_NAME); 150 if (err) { 151 dev_err(&pdev->dev, "unable to request GPIO %d\n", 152 pdata->gpio_a); 153 goto exit_unregister_input; 154 } 155 156 err = gpio_direction_input(pdata->gpio_a); 157 if (err) { 158 dev_err(&pdev->dev, "unable to set GPIO %d for input\n", 159 pdata->gpio_a); 160 goto exit_unregister_input; 161 } 162 163 err = gpio_request(pdata->gpio_b, DRV_NAME); 164 if (err) { 165 dev_err(&pdev->dev, "unable to request GPIO %d\n", 166 pdata->gpio_b); 167 goto exit_free_gpio_a; 168 } 169 170 err = gpio_direction_input(pdata->gpio_b); 171 if (err) { 172 dev_err(&pdev->dev, "unable to set GPIO %d for input\n", 173 pdata->gpio_b); 174 goto exit_free_gpio_a; 175 } 176 177 /* request the IRQs */ 178 err = request_irq(encoder->irq_a, &rotary_encoder_irq, 179 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 180 DRV_NAME, encoder); 181 if (err) { 182 dev_err(&pdev->dev, "unable to request IRQ %d\n", 183 encoder->irq_a); 184 goto exit_free_gpio_b; 185 } 186 187 err = request_irq(encoder->irq_b, &rotary_encoder_irq, 188 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 189 DRV_NAME, encoder); 190 if (err) { 191 dev_err(&pdev->dev, "unable to request IRQ %d\n", 192 encoder->irq_b); 193 goto exit_free_irq_a; 194 } 195 196 platform_set_drvdata(pdev, encoder); 197 198 return 0; 199 200 exit_free_irq_a: 201 free_irq(encoder->irq_a, encoder); 202 exit_free_gpio_b: 203 gpio_free(pdata->gpio_b); 204 exit_free_gpio_a: 205 gpio_free(pdata->gpio_a); 206 exit_unregister_input: 207 input_unregister_device(input); 208 input = NULL; /* so we don't try to free it */ 209 exit_free_mem: 210 input_free_device(input); 211 kfree(encoder); 212 return err; 213 } 214 215 static int __devexit rotary_encoder_remove(struct platform_device *pdev) 216 { 217 struct rotary_encoder *encoder = platform_get_drvdata(pdev); 218 struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data; 219 220 free_irq(encoder->irq_a, encoder); 221 free_irq(encoder->irq_b, encoder); 222 gpio_free(pdata->gpio_a); 223 gpio_free(pdata->gpio_b); 224 input_unregister_device(encoder->input); 225 platform_set_drvdata(pdev, NULL); 226 kfree(encoder); 227 228 return 0; 229 } 230 231 static struct platform_driver rotary_encoder_driver = { 232 .probe = rotary_encoder_probe, 233 .remove = __devexit_p(rotary_encoder_remove), 234 .driver = { 235 .name = DRV_NAME, 236 .owner = THIS_MODULE, 237 } 238 }; 239 240 static int __init rotary_encoder_init(void) 241 { 242 return platform_driver_register(&rotary_encoder_driver); 243 } 244 245 static void __exit rotary_encoder_exit(void) 246 { 247 platform_driver_unregister(&rotary_encoder_driver); 248 } 249 250 module_init(rotary_encoder_init); 251 module_exit(rotary_encoder_exit); 252 253 MODULE_ALIAS("platform:" DRV_NAME); 254 MODULE_DESCRIPTION("GPIO rotary encoder driver"); 255 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 256 MODULE_LICENSE("GPL v2"); 257 258