spi_parport with proper irq support
[pub/spi-gpio-pp.git] / gpio_parport.c
1 /*
2 * gpio_parport.c - GPIO master controller driver based on the parallel port adapter
3 *
4 * Copyright (C) 2008 Peter Henn
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * The gpio_parport driver uses the gpio framwork started with kernel 2.6.25.
21 * It is designed as a PC reference GPIO driver just for testing on Intel based
22 * PC architectures. Therefore the whole functionality of a parport used as
23 * GPIO was never in focus. We initial need only one dedicated output and one
24 * dedicated input, but with interrupt support.
25 *
26 * The driver uses caches, which saves the output status, because the IO output
27 * need long time on x86 architecture. The caches will be setup with the initial
28 * values read from those ports during driver startup. If you are using the parport
29 * in shared operation mode, the caches has to be updated too.
30 */
31 #include <linux/version.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/device.h>
36 #include <linux/parport.h>
37 #include <asm/gpio.h>
38
39 #ifdef CONFIG_GPIO_PARPORT_MUTEX
40 #include <linux/mutex.h>
41 #define sCONFIG_GPIO_PARPORT_MUTEX "CONFIG_GPIO_PARPORT_MUTEX"
42 #else
43 #include <linux/spinlock.h>
44 #define sCONFIG_GPIO_PARPORT_MUTEX ""
45 #endif
46 #ifdef CONFIG_GPIO_PARPORT_EXCLUSIVE
47 #define sCONFIG_GPIO_PARPORT_EXCLUSIVE " CONFIG_GPIO_PARPORT_EXCLUSIVE"
48 #undef CONFIG_GPIO_PARPORT_NOCLAIME
49 #else
50 #define sCONFIG_GPIO_PARPORT_EXCLUSIVE ""
51 #endif
52 #ifdef CONFIG_GPIO_PARPORT_NOCLAIME
53 #define sCONFIG_GPIO_PARPORT_NOCLAIME " CONFIG_GPIO_PARPORT_NOCLAIME"
54 #else
55 #define sCONFIG_GPIO_PARPORT_NOCLAIME ""
56 #endif
57
58 /* ToDo:
59 * - proper handling error flags for gpio_to_irq ...
60 * - Sharing parport with other devices controled by module load
61 * paramter with using mutex mode ... but check with releasing parport IRQ handler
62 * - Verifing Parport modes
63 * - Support PM functions like resume, suspend
64 *
65 * Parallel
66 * Port Direction Register Name Signal
67 * ----------- --------- -------- ---------------------- ------
68 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE GPO08
69 * D0 2 --> D0 (1 << 0) GPO00
70 * D1 3 --> D1 (1 << 1) GPO01
71 * D2 4 --> D2 (1 << 2) GPO02
72 * D3 5 --> D3 (1 << 3) GPO03
73 * D4 6 --> D4 (1 << 4) GPO04
74 * D5 7 --> D5 (1 << 5) GPO05
75 * D6 8 --> D6 (1 << 6) GPO06
76 * D7 9 --> D7 (1 << 7) GPO07
77 * nAckn 10 <-- S6 PARPORT_STATUS_ACK GPI15*
78 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY GPI16
79 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT GPI14
80 * Sel 13 <-- S4 PARPORT_STATUS_SELECT GPI13
81 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD GPO09
82 * nError 15 <-- S3 PARPORT_STATUS_ERROR GPI12
83 * nInit 16 --> C2 PARPORT_CONTROL_INIT GPO10
84 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT GPO11
85 * GND 25 -- -- GND
86 *
87 * Signal:
88 * - GPOx : general purpose output only
89 * - GPIx : general purpose input only
90 * - GPIx* : interrupt in
91 */
92
93 //*****************************************************************************
94 // MODULE PARAMETERS
95 //*****************************************************************************
96 #define DRVNAME "gpio_parport" /* name of the driver */
97 #define DRIVER_VERSION "0.2" /* helps identifing different versions of that driver */
98
99 #define GPIO_PARPORT_CTRL_PORT_BITS 8 /* starting with bit 8 use the parport control port */
100 #define GPIO_PARPORT_STAT_PORT_BITS 12 /* starting with bit 12 use the parport status port */
101 #define GPIO_PARPORT_STAT_PORT_OFF 3 /* parport status port needs internally 3 bit offset */
102 #define GPIO_PARPORT_INTERRUPT 15 /* gpio offset for parport pin Acknolge, which support interrupt */
103 #define GPIO_PARPORT_NR_GPIOS 17 /* parport support up to 17 general purpose (dedicated) Inputs and Outputs */
104 #define GPIO_PARPORT_STATUS_INV 0x80 /* invert internal bit 7 of status parport bit (Busy) */
105 #define GPIO_PARPORT_CONTROL_INV 0x0b /* invert internal bit 0, 1 and 3 of control parport bits (strobe, autofd, select) */
106
107 /* Module load parameter */
108 static int irq=-1; /* force irq for parport pin 10 */
109 static int gpiobase = -1 /* default dynamic assignment */;
110
111 typedef struct {
112 struct pardevice *dev; /* parport device entry */
113 struct parport *port; /* parport port entry */
114 u8 dcache; /* parport data port mirror bits */
115 u8 ccache; /* parport control port mirror bits */
116 struct gpio_chip gpio; /* parport GPIO seen as a chip */
117 int irq; /* parport interrupt */
118 #ifdef CONFIG_GPIO_PARPORT_MUTEX
119 struct mutex lock; /* prevent double accessing bits */
120 #else
121 spinlock_t lock; /* prevent double accessing bits */
122 #endif
123 struct list_head list; /* gpio_parport host controller list */
124 } gpio_pp_struct;
125
126
127 /*----------------------------------------------------------------------*/
128 /*
129 ** use the lock and unlock macros to get rid of the bad #ifdef
130 ** stuff in the rest of the code.
131 */
132 #ifdef CONFIG_GPIO_PARPORT_MUTEX
133 #define gpio_parport_lock(lock, flags) \
134 do { \
135 mutex_lock(lock); \
136 } while (0)
137 #else
138 #define gpio_parport_lock(lock, flags) \
139 do { \
140 spin_lock_irqsave(lock, flags); \
141 } while (0)
142 #endif
143
144 #ifdef CONFIG_GPIO_PARPORT_MUTEX
145 #define gpio_parport_unlock(lock, flags) \
146 do { \
147 mutex_unlock(lock); \
148 } while (0)
149 #else
150 #define gpio_parport_unlock(lock, flags) \
151 do { \
152 spin_unlock_irqrestore(lock, flags); \
153 } while (0)
154 #endif
155
156
157 /*----------------------------------------------------------------------*/
158 /*
159 ** parport data bit input function
160 */
161 static inline int gpio_parport_data_get(gpio_pp_struct *pp, unsigned mask)
162 {
163 // good idea to update also the cache
164 pp->dcache = parport_read_data(pp->port);
165 return ((pp->dcache & mask) != 0);
166 }
167
168 /*
169 ** parport control bit input function
170 */
171 static inline int gpio_parport_ctrl_get(gpio_pp_struct *pp, unsigned mask)
172 {
173 // good idea to update also the cache
174 pp->ccache = GPIO_PARPORT_CONTROL_INV ^ parport_read_control(pp->port);
175 return ((pp->ccache & mask) != 0);
176 }
177
178 /*
179 ** parport status bit input function
180 */
181 static inline int gpio_parport_status_get(gpio_pp_struct *pp, unsigned mask)
182 {
183 unsigned int cache = GPIO_PARPORT_STATUS_INV ^ parport_read_status(pp->port);
184 return ((cache & mask) != 0);
185 }
186
187
188 /*
189 ** general parport read bit function
190 */
191 static int gpio_parport_get(struct gpio_chip *gpio, unsigned offset)
192 {
193 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
194 int status = 0; /* inititalization ensures return zero for unhandled bits */
195 unsigned long flags;
196
197 if (offset < GPIO_PARPORT_CTRL_PORT_BITS) {
198 u8 mask = 1 << offset;
199 gpio_parport_lock(&pp->lock, flags);
200 status = gpio_parport_data_get(pp, mask);
201 gpio_parport_unlock(&pp->lock, flags);
202 } else if (offset < GPIO_PARPORT_STAT_PORT_BITS) {
203 u8 mask = 1 << (offset - GPIO_PARPORT_CTRL_PORT_BITS);
204 gpio_parport_lock(&pp->lock, flags);
205 status = gpio_parport_ctrl_get(pp, mask);
206 gpio_parport_unlock(&pp->lock, flags);
207 } else if (offset < GPIO_PARPORT_NR_GPIOS) {
208 u8 mask = 1 << (offset - GPIO_PARPORT_STAT_PORT_BITS + GPIO_PARPORT_STAT_PORT_OFF);
209 gpio_parport_lock(&pp->lock, flags);
210 status = gpio_parport_status_get(pp, mask);
211 gpio_parport_unlock(&pp->lock, flags);
212 }
213 return status;
214 }
215
216
217 /*----------------------------------------------------------------------*/
218 /*
219 ** parport data bit output function
220 */
221 static inline void gpio_parport_data_set(gpio_pp_struct *pp, unsigned mask, int value)
222 {
223 u8 byte = pp->dcache; // use old value from cache
224 if (value)
225 byte |= mask;
226 else
227 byte &= ~mask;
228 pp->dcache = byte; // restore cache
229 return parport_write_data(pp->port, byte);
230 }
231
232 /*
233 ** parport control bit output function
234 ** most PARPORT_CONTROL_* bits are negated, but they are currently _not_ corrected
235 ** to keep it as simple as possible
236 */
237 static inline void gpio_parport_ctrl_set(gpio_pp_struct *pp, unsigned mask, int value)
238 {
239 u8 byte = pp->ccache; // use old value from cache
240 if (value)
241 byte |= mask;
242 else
243 byte &= ~mask;
244 pp->ccache = byte; // restore cache
245 return parport_write_control(pp->port, (GPIO_PARPORT_CONTROL_INV ^ byte));
246 }
247
248 /*
249 ** general parport set bit output function
250 */
251 static void gpio_parport_set(struct gpio_chip *gpio, unsigned offset, int value)
252 {
253 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
254 unsigned long flags;
255
256 if (offset < GPIO_PARPORT_CTRL_PORT_BITS) {
257 u8 mask = 1 << offset;
258 gpio_parport_lock(&pp->lock, flags);
259 gpio_parport_data_set(pp, mask, value);
260 gpio_parport_unlock(&pp->lock, flags);
261 } else if (offset < GPIO_PARPORT_STAT_PORT_BITS) {
262 u8 mask = 1 << (offset - GPIO_PARPORT_CTRL_PORT_BITS);
263 gpio_parport_lock(&pp->lock, flags);
264 gpio_parport_ctrl_set(pp, mask, value);
265 gpio_parport_unlock(&pp->lock, flags);
266 }
267 }
268
269 /*----------------------------------------------------------------------*/
270 /*
271 ** general parport direction input funtion
272 ** we support only the parport control bits as dedicated input bits
273 */
274 static int gpio_parport_direction_input(struct gpio_chip * gpio, unsigned offset)
275 {
276 if (offset < GPIO_PARPORT_STAT_PORT_BITS) {
277 return -ENOSYS;
278 } else if (offset < GPIO_PARPORT_NR_GPIOS) {
279 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
280 unsigned long flags;
281 gpio_parport_lock(&pp->lock, flags);
282 if ((GPIO_PARPORT_INTERRUPT == offset) && (pp->irq >= 0)) {
283 parport_enable_irq(pp->port);
284 }
285 gpio_parport_unlock(&pp->lock, flags);
286 return 0;
287 }
288 return -ENODEV;
289 }
290
291 /*
292 ** general parport direction output function
293 ** we support only data bits and control bits as dedicated output bits
294 */
295 static int gpio_parport_direction_output(struct gpio_chip * gpio, unsigned offset, int value)
296 {
297 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
298 unsigned long flags;
299
300 if (offset < GPIO_PARPORT_CTRL_PORT_BITS) {
301 u8 mask = 1 << offset;
302 gpio_parport_lock(&pp->lock, flags);
303 gpio_parport_data_set(pp, mask, value);
304 gpio_parport_unlock(&pp->lock, flags);
305 return 0;
306 } else if (offset < GPIO_PARPORT_STAT_PORT_BITS) {
307 u8 mask = 1 << (offset - GPIO_PARPORT_CTRL_PORT_BITS);
308 gpio_parport_lock(&pp->lock, flags);
309 gpio_parport_ctrl_set(pp, mask, value);
310 gpio_parport_unlock(&pp->lock, flags);
311 return 0;
312 } else if (offset < GPIO_PARPORT_NR_GPIOS) {
313 return -ENOSYS;
314 }
315 return -ENODEV;
316 }
317
318 /*----------------------------------------------------------------------*/
319 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
320 /*
321 ** parport interrupt support only for acknoledge on GPI15 interrupt
322 */
323 static int gpio_parport_gpio_to_irq(struct gpio_chip * gpio, unsigned offset)
324 {
325 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
326
327 if (GPIO_PARPORT_INTERRUPT == offset) {
328 return pp->irq;
329 } else {
330 return -ENOSYS;
331 }
332 }
333 #endif
334
335 /*----------------------------------------------------------------------*/
336 /*
337 ** gpio_parport setup function
338 ** add all entries into the GPIO framework
339 */
340 static void gpio_parport_gpio_setup(gpio_pp_struct *pp)
341 {
342 struct gpio_chip *c = &pp->gpio;
343 c->label = (char *)pp->port->name;
344 c->owner = THIS_MODULE;
345 c->direction_input = gpio_parport_direction_input;
346 c->get = gpio_parport_get;
347 c->direction_output = gpio_parport_direction_output;
348 c->set = gpio_parport_set;
349 c->dbg_show = NULL;
350 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
351 c->request = NULL;
352 c->free = NULL;
353 c->to_irq = gpio_parport_gpio_to_irq;
354 #endif
355 c->base = gpiobase;
356 c->ngpio = GPIO_PARPORT_NR_GPIOS;
357 #ifdef CONFIG_GPIO_PARPORT_MUTEX
358 c->can_sleep = 1;
359 c->dev = pp->dev;
360 #else
361 c->can_sleep = 0;
362 #endif
363 }
364
365 /***************************************************************************
366 * Parallel port attaching and detaching routines *
367 ***************************************************************************/
368
369 static LIST_HEAD(gpio_parport_hosts);
370
371 /*
372 ** If attaching the driver to parport is done with exclusive access, the driver
373 ** is still loaded, although the attach might not be done successful.
374 ** If you have already registered e.g. the lp driver, please unload any drivers
375 ** first as long as you use exclusive access.
376 */
377 static int __gpio_parport_attach(struct parport *pb)
378 {
379 gpio_pp_struct *pp;
380 int err;
381
382 /* we need a zero initialized structure here */
383 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
384 if (!pp)
385 return -ENOMEM;
386
387 #ifdef CONFIG_GPIO_PARPORT_MUTEX
388 mutex_init(&pp->lock);
389 #else
390 spin_lock_init(&pp->lock);
391 #endif
392
393 /* ----------------- ParPort Hooks ------------------- */
394 /* REVISIT: no probe, just think it is there */
395 pp->port = pb;
396
397 //ToDo: share parport with other device, but may need a mutex lock!
398 //ToDo: check supported modes are ok
399 //ToDo: PM support, e.g. Suspend and Resume
400 /* interrupt handler will not be here registered.
401 But an interrupt handler can be later registered to IRQ pin */
402 pp->dev = parport_register_device(pb, /* port */
403 DRVNAME, /* driver name */
404 NULL, /* preemption fkt */
405 NULL, /* wake-up fkt */
406 NULL, /* interrupt handler */
407 #ifdef CONFIG_GPIO_PARPORT_EXCLUSIVE
408 PARPORT_FLAG_EXCL, /* flags */
409 #else
410 0, /* flags, 0 means used port shared */
411 #endif
412 pp /* handle */ );
413 if (pp->dev == NULL) {
414 pr_err("%s: unable to register with parport\n", DRVNAME);
415 err = -ENOMEM;
416 goto err_release_mem;
417 }
418
419 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
420 err = parport_claim(pp->dev);
421 if (err < 0) {
422 pr_err("%s: unable to claim parport\n", DRVNAME);
423 err = -ENODEV;
424 goto err_unregister_parport;
425 }
426 #endif
427
428 /* ------------- GPIO PARPORT init --------------- */
429 /* cache initialisation just preventing changing unused bits */
430 pp->dcache = parport_read_data(pp->port);
431 pp->ccache = GPIO_PARPORT_CONTROL_INV ^ parport_read_control(pp->port);
432
433 /* interrrut initialization */
434 /* tested on x86 arch, because irq has nothing special to handle here */
435 if(irq >= 0) {
436 pp->irq = irq;
437 pr_info("%s: force usage IRQ %d\n", DRVNAME, pp->irq);
438 if(pp->port->irq == pp->irq) {
439 /* we free the standard parport handler, because we want to use our own later */
440 free_irq(pp->irq, pp->port);
441 pr_info("%s: Note parport driver will claim later irq as already freed during its module unload\n",
442 DRVNAME);
443 }
444 } else if(pp->port->irq != PARPORT_IRQ_NONE) {
445 pp->irq = pp->port->irq;
446 /* we free the standard parport handler, because we want to use our own later */
447 free_irq(pp->irq, pp->port);
448 pr_info("%s: will use IRQ %d exclusive\n", DRVNAME, pp->irq);
449 pr_info("%s: Note parport driver will claim later irq as already freed during its module unload\n", DRVNAME);
450 } else {
451 pp->irq = -ENXIO;
452 }
453
454 /* ----------------- GPIO init ------------------- */
455 gpio_parport_gpio_setup(pp);
456 err = gpiochip_add(&pp->gpio);
457 if (err != 0) {
458 pr_err("%s: Failed to register GPIOs\n", DRVNAME);
459 goto err_release_parport;
460 }
461
462 pr_info("%s: Abusing Parport for GPIO %d to %d, labled as %s\n",
463 DRVNAME, pp->gpio.base, pp->gpio.base + GPIO_PARPORT_NR_GPIOS - 1, pp->gpio.label);
464
465 list_add_tail(&pp->list, &gpio_parport_hosts);
466 return 0;
467
468 err_release_parport:
469 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
470 parport_release(pp->dev);
471 err_unregister_parport:
472 #endif
473 parport_unregister_device(pp->dev);
474 err_release_mem:
475 kfree(pp);
476 return err;
477 }
478
479
480
481 static void gpio_parport_attach(struct parport *pb)
482 {
483 (void)__gpio_parport_attach(pb);
484 }
485
486
487 static int gpio_parport_remove(gpio_pp_struct *pp)
488 {
489 int err;
490 if (pp->irq >= 0) {
491 parport_disable_irq(pp->port);
492 }
493 err = gpiochip_remove(&pp->gpio);
494 if (err < 0)
495 pr_err("%s: gpio_remove error: %d\n", DRVNAME, err);
496 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
497 parport_release(pp->dev);
498 #endif
499 parport_unregister_device(pp->dev);
500 return err;
501 }
502
503 static void gpio_parport_detach(struct parport *pb)
504 {
505 gpio_pp_struct *pp;
506
507 list_for_each_entry(pp, &gpio_parport_hosts, list) {
508 if (pp->port == pb) {
509 (void)gpio_parport_remove(pp);
510 list_del_init(&pp->list);
511 kfree(pp);
512 break;
513 }
514 }
515 }
516
517 static struct parport_driver gpio_parport_driver = {
518 .name = DRVNAME,
519 .attach = gpio_parport_attach,
520 .detach = gpio_parport_detach,
521 };
522
523
524 static int __init gpio_parport_init(void)
525 {
526 return parport_register_driver(&gpio_parport_driver);
527 }
528 device_initcall(gpio_parport_init);
529
530 static void __exit gpio_parport_exit(void)
531 {
532 parport_unregister_driver(&gpio_parport_driver);
533 }
534 module_exit(gpio_parport_exit);
535
536 MODULE_AUTHOR("Option Wireless");
537 MODULE_DESCRIPTION("GPIO master controller driver for Parport Adapter");
538 MODULE_LICENSE("GPL");
539
540 module_param(irq, uint, S_IRUGO);
541 MODULE_PARM_DESC(irq, "force set IRQ for parport pin 10");
542
543 module_param_named(base, gpiobase, uint, S_IRUGO);
544 MODULE_PARM_DESC(base, "force set GPIO base address");
545
546 MODULE_INFO(Flags, sCONFIG_GPIO_PARPORT_MUTEX sCONFIG_GPIO_PARPORT_EXCLUSIVE sCONFIG_GPIO_PARPORT_NOCLAIME);
547 MODULE_INFO(Version, DRIVER_VERSION);