2 * gpio_parport.c - GPIO master controller driver based on the parallel port adapter
4 * Copyright (C) 2008 Peter Henn
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.
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.
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.
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.
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.
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>
39 #ifdef CONFIG_GPIO_PARPORT_MUTEX
40 #include <linux/mutex.h>
41 #define sCONFIG_GPIO_PARPORT_MUTEX "CONFIG_GPIO_PARPORT_MUTEX"
43 #include <linux/spinlock.h>
44 #define sCONFIG_GPIO_PARPORT_MUTEX ""
46 #ifdef CONFIG_GPIO_PARPORT_EXCLUSIVE
47 #define sCONFIG_GPIO_PARPORT_EXCLUSIVE " CONFIG_GPIO_PARPORT_EXCLUSIVE"
48 #undef CONFIG_GPIO_PARPORT_NOCLAIME
50 #define sCONFIG_GPIO_PARPORT_EXCLUSIVE ""
52 #ifdef CONFIG_GPIO_PARPORT_NOCLAIME
53 #define sCONFIG_GPIO_PARPORT_NOCLAIME " CONFIG_GPIO_PARPORT_NOCLAIME"
55 #define sCONFIG_GPIO_PARPORT_NOCLAIME ""
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
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
88 * - GPOx : general purpose output only
89 * - GPIx : general purpose input only
90 * - GPIx* : interrupt in
93 //*****************************************************************************
95 //*****************************************************************************
96 #define DRVNAME "gpio_parport" /* name of the driver */
97 #define DRIVER_VERSION "0.2" /* helps identifing different versions of that driver */
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) */
107 /* Module load parameter */
108 static int irq
=-1; /* force irq for parport pin 10 */
109 static int gpiobase
= -1 /* default dynamic assignment */;
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 */
121 spinlock_t lock
; /* prevent double accessing bits */
123 struct list_head list
; /* gpio_parport host controller list */
127 /*----------------------------------------------------------------------*/
129 ** use the lock and unlock macros to get rid of the bad #ifdef
130 ** stuff in the rest of the code.
132 #ifdef CONFIG_GPIO_PARPORT_MUTEX
133 #define gpio_parport_lock(lock, flags) \
138 #define gpio_parport_lock(lock, flags) \
140 spin_lock_irqsave(lock, flags); \
144 #ifdef CONFIG_GPIO_PARPORT_MUTEX
145 #define gpio_parport_unlock(lock, flags) \
147 mutex_unlock(lock); \
150 #define gpio_parport_unlock(lock, flags) \
152 spin_unlock_irqrestore(lock, flags); \
157 /*----------------------------------------------------------------------*/
159 ** parport data bit input function
161 static inline int gpio_parport_data_get(gpio_pp_struct
*pp
, unsigned mask
)
163 // good idea to update also the cache
164 pp
->dcache
= parport_read_data(pp
->port
);
165 return ((pp
->dcache
& mask
) != 0);
169 ** parport control bit input function
171 static inline int gpio_parport_ctrl_get(gpio_pp_struct
*pp
, unsigned mask
)
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);
179 ** parport status bit input function
181 static inline int gpio_parport_status_get(gpio_pp_struct
*pp
, unsigned mask
)
183 unsigned int cache
= GPIO_PARPORT_STATUS_INV
^ parport_read_status(pp
->port
);
184 return ((cache
& mask
) != 0);
189 ** general parport read bit function
191 static int gpio_parport_get(struct gpio_chip
*gpio
, unsigned offset
)
193 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
194 int status
= 0; /* inititalization ensures return zero for unhandled bits */
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
);
217 /*----------------------------------------------------------------------*/
219 ** parport data bit output function
221 static inline void gpio_parport_data_set(gpio_pp_struct
*pp
, unsigned mask
, int value
)
223 u8 byte
= pp
->dcache
; // use old value from cache
228 pp
->dcache
= byte
; // restore cache
229 return parport_write_data(pp
->port
, byte
);
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
237 static inline void gpio_parport_ctrl_set(gpio_pp_struct
*pp
, unsigned mask
, int value
)
239 u8 byte
= pp
->ccache
; // use old value from cache
244 pp
->ccache
= byte
; // restore cache
245 return parport_write_control(pp
->port
, (GPIO_PARPORT_CONTROL_INV
^ byte
));
249 ** general parport set bit output function
251 static void gpio_parport_set(struct gpio_chip
*gpio
, unsigned offset
, int value
)
253 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
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
);
269 /*----------------------------------------------------------------------*/
271 ** general parport direction input funtion
272 ** we support only the parport control bits as dedicated input bits
274 static int gpio_parport_direction_input(struct gpio_chip
* gpio
, unsigned offset
)
276 if (offset
< GPIO_PARPORT_STAT_PORT_BITS
) {
278 } else if (offset
< GPIO_PARPORT_NR_GPIOS
) {
279 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
281 gpio_parport_lock(&pp
->lock
, flags
);
282 if ((GPIO_PARPORT_INTERRUPT
== offset
) && (pp
->irq
>= 0)) {
283 parport_enable_irq(pp
->port
);
285 gpio_parport_unlock(&pp
->lock
, flags
);
292 ** general parport direction output function
293 ** we support only data bits and control bits as dedicated output bits
295 static int gpio_parport_direction_output(struct gpio_chip
* gpio
, unsigned offset
, int value
)
297 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
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
);
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
);
312 } else if (offset
< GPIO_PARPORT_NR_GPIOS
) {
318 /*----------------------------------------------------------------------*/
319 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
321 ** parport interrupt support only for acknoledge on GPI15 interrupt
323 static int gpio_parport_gpio_to_irq(struct gpio_chip
* gpio
, unsigned offset
)
325 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
327 if (GPIO_PARPORT_INTERRUPT
== offset
) {
335 /*----------------------------------------------------------------------*/
337 ** gpio_parport setup function
338 ** add all entries into the GPIO framework
340 static void gpio_parport_gpio_setup(gpio_pp_struct
*pp
)
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
;
350 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
353 c
->to_irq
= gpio_parport_gpio_to_irq
;
356 c
->ngpio
= GPIO_PARPORT_NR_GPIOS
;
357 #ifdef CONFIG_GPIO_PARPORT_MUTEX
365 /***************************************************************************
366 * Parallel port attaching and detaching routines *
367 ***************************************************************************/
369 static LIST_HEAD(gpio_parport_hosts
);
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.
377 static int __gpio_parport_attach(struct parport
*pb
)
382 /* we need a zero initialized structure here */
383 pp
= kzalloc(sizeof(*pp
), GFP_KERNEL
);
387 #ifdef CONFIG_GPIO_PARPORT_MUTEX
388 mutex_init(&pp
->lock
);
390 spin_lock_init(&pp
->lock
);
393 /* ----------------- ParPort Hooks ------------------- */
394 /* REVISIT: no probe, just think it is there */
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 */
410 0, /* flags, 0 means used port shared */
413 if (pp
->dev
== NULL
) {
414 pr_err("%s: unable to register with parport\n", DRVNAME
);
416 goto err_release_mem
;
419 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
420 err
= parport_claim(pp
->dev
);
422 pr_err("%s: unable to claim parport\n", DRVNAME
);
424 goto err_unregister_parport
;
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
);
433 /* interrrut initialization */
434 /* tested on x86 arch, because irq has nothing special to handle here */
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",
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
);
454 /* ----------------- GPIO init ------------------- */
455 gpio_parport_gpio_setup(pp
);
456 err
= gpiochip_add(&pp
->gpio
);
458 pr_err("%s: Failed to register GPIOs\n", DRVNAME
);
459 goto err_release_parport
;
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
);
465 list_add_tail(&pp
->list
, &gpio_parport_hosts
);
469 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
470 parport_release(pp
->dev
);
471 err_unregister_parport
:
473 parport_unregister_device(pp
->dev
);
481 static void gpio_parport_attach(struct parport
*pb
)
483 (void)__gpio_parport_attach(pb
);
487 static int gpio_parport_remove(gpio_pp_struct
*pp
)
491 parport_disable_irq(pp
->port
);
493 err
= gpiochip_remove(&pp
->gpio
);
495 pr_err("%s: gpio_remove error: %d\n", DRVNAME
, err
);
496 #ifndef CONFIG_GPIO_PARPORT_NOCLAIME
497 parport_release(pp
->dev
);
499 parport_unregister_device(pp
->dev
);
503 static void gpio_parport_detach(struct parport
*pb
)
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
);
517 static struct parport_driver gpio_parport_driver
= {
519 .attach
= gpio_parport_attach
,
520 .detach
= gpio_parport_detach
,
524 static int __init
gpio_parport_init(void)
526 return parport_register_driver(&gpio_parport_driver
);
528 device_initcall(gpio_parport_init
);
530 static void __exit
gpio_parport_exit(void)
532 parport_unregister_driver(&gpio_parport_driver
);
534 module_exit(gpio_parport_exit
);
536 MODULE_AUTHOR("Option Wireless");
537 MODULE_DESCRIPTION("GPIO master controller driver for Parport Adapter");
538 MODULE_LICENSE("GPL");
540 module_param(irq
, uint
, S_IRUGO
);
541 MODULE_PARM_DESC(irq
, "force set IRQ for parport pin 10");
543 module_param_named(base
, gpiobase
, uint
, S_IRUGO
);
544 MODULE_PARM_DESC(base
, "force set GPIO base address");
546 MODULE_INFO(Flags
, sCONFIG_GPIO_PARPORT_MUTEX sCONFIG_GPIO_PARPORT_EXCLUSIVE sCONFIG_GPIO_PARPORT_NOCLAIME
);
547 MODULE_INFO(Version
, DRIVER_VERSION
);