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 ""
49 * - proper handling error flags for gpio_to_irq ...
50 * - using pp->dev-port should be equal to pp->port ...
51 * - Sharing parport with other devices controled by module load
52 * paramter with using mutex mode ... but check with releasing parport IRQ handler
53 * - Verifing Parport modes
54 * - Add sysfs Debug functionality
55 * - Support PM functions like resume, suspend
58 * Port Direction Register Name Signal
59 * ----------- --------- -------- ---------------------- ------
60 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE nGPO08
61 * D0 2 --> D0 (1 << 0) GPO00
62 * D1 3 --> D1 (1 << 1) GPO01
63 * D2 4 --> D2 (1 << 2) GPO02
64 * D3 5 --> D3 (1 << 3) GPO03
65 * D4 6 --> D4 (1 << 4) GPO04
66 * D5 7 --> D5 (1 << 5) GPO05
67 * D6 8 --> D6 (1 << 6) GPO06
68 * D7 9 --> D7 (1 << 7) GPO07
69 * nAckn 10 <-- S6 PARPORT_STATUS_ACK GPI15*
70 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY nGPI16
71 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT GPI14
72 * Sel 13 <-- S4 PARPORT_STATUS_SELECT GPI13
73 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD nGPO09
74 * nError 15 <-- S3 PARPORT_STATUS_ERROR GPI12
75 * nInit 16 --> C2 PARPORT_CONTROL_INIT GPO10
76 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT nGPO11
80 * - GPOx : general purpose output only
81 * - GPIx : general purpose input only
82 * - GPIx* : interrupt in
83 * - nGPXx : signal is inverted
86 //*****************************************************************************
88 //*****************************************************************************
89 #define DRVNAME "gpio_parport" /* name of the driver */
90 #define DRIVER_VERSION "0.2" /* helps identifing different versions of that driver */
92 #define GPIO_PARPORT_CTRL_PORT_BITS 8 /* starting with bit 8 use the parport control port */
93 #define GPIO_PARPORT_STAT_PORT_BITS 12 /* starting with bit 12 use the parport status port */
94 #define GPIO_PARPORT_STAT_PORT_OFF 3 /* parport status port needs internally 3 bit offset */
95 #define GPIO_PARPORT_INTERRUPT 15 /* gpio offset for parport pin Acknolge, which support interrupt */
96 #define GPIO_PARPORT_NR_GPIOS 17 /* parport support up to 17 general purpose (dedicated) Inputs and Outputs */
99 static int modparam_gpiobase
= -1 /* dynamic assingment */;
102 struct pardevice
*dev
; /* parport device entry */
103 struct parport
*port
; /* parport port entry */
104 u8 dcache
; /* parport data port mirror bits */
105 u8 ccache
; /* parport control port mirror bits */
106 struct gpio_chip gpio
; /* parport GPIO seen as a chip */
107 int irq
; /* parport interrupt */
108 #ifdef CONFIG_GPIO_PARPORT_MUTEX
109 struct mutex lock
; /* prevent double accessing bits */
111 spinlock_t lock
; /* prevent double accessing bits */
113 struct list_head list
; /* gpio_parport host controller list */
117 /*----------------------------------------------------------------------*/
119 ** use the lock and unlock macros to get rid of the bad #ifdef
120 ** stuff in the rest of the code.
122 #ifdef CONFIG_GPIO_PARPORT_MUTEX
123 #define gpio_parport_lock(lock, flags) \
128 #define gpio_parport_lock(lock, flags) \
130 spin_lock_irqsave(lock, flags); \
134 #ifdef CONFIG_GPIO_PARPORT_MUTEX
135 #define gpio_parport_unlock(lock, flags) \
137 mutex_unlock(lock); \
140 #define gpio_parport_unlock(lock, flags) \
142 spin_unlock_irqrestore(lock, flags); \
147 /*----------------------------------------------------------------------*/
149 ** parport data bit input function
151 static inline int gpio_parport_data_get(gpio_pp_struct
*pp
, unsigned mask
)
153 // good idea to update also the cache
154 pp
->dcache
= parport_read_data(pp
->port
);
155 return ((pp
->dcache
& mask
) != 0);
159 ** parport control bit input function
161 static inline int gpio_parport_ctrl_get(gpio_pp_struct
*pp
, unsigned mask
)
163 // good idea to update also the cache
164 pp
->ccache
= parport_read_control(pp
->port
);
165 return ((pp
->ccache
& mask
) != 0);
169 ** parport status bit input function
171 static inline int gpio_parport_status_get(gpio_pp_struct
*pp
, unsigned mask
)
173 unsigned int cache
= parport_read_status(pp
->port
);
174 return ((cache
& mask
) != 0);
179 ** general parport read bit function
181 static int gpio_parport_get(struct gpio_chip
*gpio
, unsigned offset
)
183 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
184 int status
= 0; /* inititalization ensures returm zero for unhandled bits */
187 if (offset
< GPIO_PARPORT_CTRL_PORT_BITS
) {
188 u8 mask
= 1 << offset
;
189 gpio_parport_lock(&pp
->lock
, flags
);
190 status
= gpio_parport_data_get(pp
, mask
);
191 gpio_parport_unlock(&pp
->lock
, flags
);
192 } else if (offset
< GPIO_PARPORT_STAT_PORT_BITS
) {
193 u8 mask
= 1 << (offset
- GPIO_PARPORT_CTRL_PORT_BITS
);
194 gpio_parport_lock(&pp
->lock
, flags
);
195 status
= gpio_parport_ctrl_get(pp
, mask
);
196 gpio_parport_unlock(&pp
->lock
, flags
);
197 } else if (offset
< GPIO_PARPORT_NR_GPIOS
) {
198 u8 mask
= 1 << (offset
- GPIO_PARPORT_STAT_PORT_BITS
+ GPIO_PARPORT_STAT_PORT_OFF
);
199 gpio_parport_lock(&pp
->lock
, flags
);
200 status
= gpio_parport_status_get(pp
, mask
);
201 gpio_parport_unlock(&pp
->lock
, flags
);
207 /*----------------------------------------------------------------------*/
209 ** parport data bit output function
211 static inline void gpio_parport_data_set(gpio_pp_struct
*pp
, unsigned mask
, int value
)
213 u8 byte
= pp
->dcache
; // use old value from cache
218 pp
->dcache
= byte
; // restore cache
219 return parport_write_data(pp
->port
, byte
);
223 ** parport control bit output function
224 ** most PARPORT_CONTROL_* bits are negated, but they are currently _not_ corrected
225 ** to keep it as simple as possible
227 static inline void gpio_parport_ctrl_set(gpio_pp_struct
*pp
, unsigned mask
, int value
)
229 u8 byte
= pp
->ccache
; // use old value from cache
234 pp
->ccache
= byte
; // restore cache
235 return parport_write_control(pp
->port
, byte
);
239 ** general parport set bit output function
241 static void gpio_parport_set(struct gpio_chip
*gpio
, unsigned offset
, int value
)
243 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
246 if (offset
< GPIO_PARPORT_CTRL_PORT_BITS
) {
247 u8 mask
= 1 << offset
;
248 gpio_parport_lock(&pp
->lock
, flags
);
249 gpio_parport_data_set(pp
, mask
, value
);
250 gpio_parport_unlock(&pp
->lock
, flags
);
251 } else if (offset
< GPIO_PARPORT_STAT_PORT_BITS
) {
252 u8 mask
= 1 << (offset
- GPIO_PARPORT_CTRL_PORT_BITS
);
253 gpio_parport_lock(&pp
->lock
, flags
);
254 gpio_parport_ctrl_set(pp
, mask
, value
);
255 gpio_parport_unlock(&pp
->lock
, flags
);
259 /*----------------------------------------------------------------------*/
261 ** general parport direction input funtion
262 ** we support only the parport control bits as dedicated input bits
264 static int gpio_parport_direction_input(struct gpio_chip
* gpio
, unsigned offset
)
266 if (offset
< GPIO_PARPORT_STAT_PORT_BITS
) {
268 } else if (offset
< GPIO_PARPORT_NR_GPIOS
) {
269 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
271 gpio_parport_lock(&pp
->lock
, flags
);
272 if ((GPIO_PARPORT_INTERRUPT
== offset
) && (pp
->irq
>= 0)) {
273 parport_enable_irq(pp
->port
);
275 gpio_parport_unlock(&pp
->lock
, flags
);
282 ** general parport direction output function
283 ** we support only data bits and control bits as dedicated output bits
285 static int gpio_parport_direction_output(struct gpio_chip
* gpio
, unsigned offset
, int value
)
287 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
290 if (offset
< GPIO_PARPORT_CTRL_PORT_BITS
) {
291 u8 mask
= 1 << offset
;
292 gpio_parport_lock(&pp
->lock
, flags
);
293 gpio_parport_data_set(pp
, mask
, value
);
294 gpio_parport_unlock(&pp
->lock
, flags
);
296 } else if (offset
< GPIO_PARPORT_STAT_PORT_BITS
) {
297 u8 mask
= 1 << (offset
- GPIO_PARPORT_CTRL_PORT_BITS
);
298 gpio_parport_lock(&pp
->lock
, flags
);
299 gpio_parport_ctrl_set(pp
, mask
, value
);
300 gpio_parport_unlock(&pp
->lock
, flags
);
302 } else if (offset
< GPIO_PARPORT_NR_GPIOS
) {
308 /*----------------------------------------------------------------------*/
309 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
311 ** parport interrupt support only for acknoledge on GPI15 interrupt
313 static int gpio_parport_gpio_to_irq(struct gpio_chip
* gpio
, unsigned offset
)
315 gpio_pp_struct
*pp
= container_of(gpio
, gpio_pp_struct
, gpio
);
317 if (GPIO_PARPORT_INTERRUPT
== offset
) {
325 /*----------------------------------------------------------------------*/
327 ** gpio_parport setup function
328 ** add all entries into the GPIO framework
330 static void gpio_parport_gpio_setup(gpio_pp_struct
*pp
)
332 struct gpio_chip
*c
= &pp
->gpio
;
333 c
->label
= (char *)pp
->port
->name
;
334 c
->owner
= THIS_MODULE
;
335 c
->direction_input
= gpio_parport_direction_input
;
336 c
->get
= gpio_parport_get
;
337 c
->direction_output
= gpio_parport_direction_output
;
338 c
->set
= gpio_parport_set
;
340 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
343 c
->to_irq
= gpio_parport_gpio_to_irq
;
345 c
->base
= modparam_gpiobase
;
346 c
->ngpio
= GPIO_PARPORT_NR_GPIOS
;
347 #ifdef CONFIG_GPIO_PARPORT_MUTEX
355 /***************************************************************************
356 * Parallel port attaching and detaching routines *
357 ***************************************************************************/
359 static LIST_HEAD(gpio_parport_hosts
);
362 ** attach driver to parport is currently done with exclusive access. Unfortunately
363 ** the driver is still loaded, although the attach might not be done successful.
364 ** If you have already registered e.g. the lp driver, please unload this drivers
365 ** first as long as no shared supported is here implemented.
367 static int __gpio_parport_attach(struct parport
*pb
)
372 /* we need a zero initialized structure here */
373 pp
= kzalloc(sizeof(*pp
), GFP_KERNEL
);
377 #ifdef CONFIG_GPIO_PARPORT_MUTEX
378 mutex_init(&pp
->lock
);
380 spin_lock_init(&pp
->lock
);
383 /* ----------------- ParPort Hooks ------------------- */
384 /* REVISIT: no probe, just think it is there */
387 //ToDo: share parport with other device, but may need a mutex lock!
388 //ToDo: check supported modes are ok
389 //ToDo: PM support, e.g. Suspend and Resume
390 /* interrupt handler will not be here registered.
391 But an interrupt handler can be later registered to IRQ pin */
392 pp
->dev
= parport_register_device(pb
, /* port */
393 DRVNAME
, /* driver name */
394 NULL
, /* preemption fkt */
395 NULL
, /* wake-up fkt */
396 NULL
, /* interrupt handler */
397 PARPORT_FLAG_EXCL
, /* flags */
399 if (pp
->dev
== NULL
) {
400 pr_err("%s: unable to register with parport\n", DRVNAME
);
402 goto err_release_mem
;
405 err
= parport_claim(pp
->dev
);
407 pr_err("%s: unable to claim parport\n", DRVNAME
);
409 goto err_unregister_parport
;
412 /* ------------- GPIO PARPORT init --------------- */
413 /* cache initialisation just preventing changing unused bits */
414 pp
->dcache
= parport_read_data(pp
->port
);
415 pp
->ccache
= parport_read_control(pp
->port
);
417 /* interrrut initialidation */
418 /* works for x86 arch, because we have nothing special to handle here */
419 if(pp
->dev
->port
->irq
!= PARPORT_IRQ_NONE
) {
420 pp
->irq
= pp
->dev
->port
->irq
;
421 /* we free the standard parport handler, because we want to use our own later */
422 free_irq(pp
->irq
, pp
->dev
->port
);
423 pr_info("%s: IRQ %d is available\n", DRVNAME
, pp
->irq
);
428 /* ----------------- GPIO init ------------------- */
429 gpio_parport_gpio_setup(pp
);
430 err
= gpiochip_add(&pp
->gpio
);
432 pr_err("%s: Failed to register GPIOs\n", DRVNAME
);
433 goto err_release_parport
;
436 pr_info("%s: Abusing Parport for GPIO %d to %d, labled as %s\n",
437 DRVNAME
, pp
->gpio
.base
, pp
->gpio
.base
+ GPIO_PARPORT_NR_GPIOS
- 1, pp
->gpio
.label
);
439 list_add_tail(&pp
->list
, &gpio_parport_hosts
);
443 parport_release(pp
->dev
);
444 err_unregister_parport
:
445 parport_unregister_device(pp
->dev
);
453 static void gpio_parport_attach(struct parport
*pb
)
455 (void)__gpio_parport_attach(pb
);
459 static int gpio_parport_remove(gpio_pp_struct
*pp
)
463 parport_disable_irq(pp
->port
);
465 err
= gpiochip_remove(&pp
->gpio
);
467 pr_err("%s: gpio_remove error: %d\n", DRVNAME
, err
);
468 parport_release(pp
->dev
);
469 parport_unregister_device(pp
->dev
);
470 //ToDo: Check the unregister return value
474 static void gpio_parport_detach(struct parport
*pb
)
478 list_for_each_entry(pp
, &gpio_parport_hosts
, list
) {
479 if (pp
->dev
->port
== pb
) {
480 (void)gpio_parport_remove(pp
);
481 list_del_init(&pp
->list
);
488 static struct parport_driver gpio_parport_driver
= {
490 .attach
= gpio_parport_attach
,
491 .detach
= gpio_parport_detach
,
495 static int __init
gpio_parport_init(void)
497 return parport_register_driver(&gpio_parport_driver
);
499 device_initcall(gpio_parport_init
);
501 static void __exit
gpio_parport_exit(void)
503 parport_unregister_driver(&gpio_parport_driver
);
505 module_exit(gpio_parport_exit
);
507 MODULE_AUTHOR("Option Wireless");
508 MODULE_DESCRIPTION("GPIO master controller driver for Parport Adapter");
509 MODULE_LICENSE("GPL");
511 MODULE_INFO(Flags
, sCONFIG_GPIO_PARPORT_MUTEX
);
512 MODULE_INFO(Version
, DRIVER_VERSION
);