73a16b141dfe0c8e15e39ca7fdabd0490bcfd103
[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
47
48 /* ToDo:
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
56 *
57 * Parallel
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
77 * GND 25 -- -- GND
78 *
79 * Signal:
80 * - GPOx : general purpose output only
81 * - GPIx : general purpose input only
82 * - GPIx* : interrupt in
83 * - nGPXx : signal is inverted
84 */
85
86 //*****************************************************************************
87 // MODULE PARAMETERS
88 //*****************************************************************************
89 #define DRVNAME "gpio_parport" /* name of the driver */
90 #define DRIVER_VERSION "0.2" /* helps identifing different versions of that driver */
91
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 */
97
98
99 static int modparam_gpiobase = -1 /* dynamic assingment */;
100
101 typedef struct {
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 */
110 #else
111 spinlock_t lock; /* prevent double accessing bits */
112 #endif
113 struct list_head list; /* gpio_parport host controller list */
114 } gpio_pp_struct;
115
116
117 /*----------------------------------------------------------------------*/
118 /*
119 ** use the lock and unlock macros to get rid of the bad #ifdef
120 ** stuff in the rest of the code.
121 */
122 #ifdef CONFIG_GPIO_PARPORT_MUTEX
123 #define gpio_parport_lock(lock, flags) \
124 do { \
125 mutex_lock(lock); \
126 } while (0)
127 #else
128 #define gpio_parport_lock(lock, flags) \
129 do { \
130 spin_lock_irqsave(lock, flags); \
131 } while (0)
132 #endif
133
134 #ifdef CONFIG_GPIO_PARPORT_MUTEX
135 #define gpio_parport_unlock(lock, flags) \
136 do { \
137 mutex_unlock(lock); \
138 } while (0)
139 #else
140 #define gpio_parport_unlock(lock, flags) \
141 do { \
142 spin_unlock_irqrestore(lock, flags); \
143 } while (0)
144 #endif
145
146
147 /*----------------------------------------------------------------------*/
148 /*
149 ** parport data bit input function
150 */
151 static inline int gpio_parport_data_get(gpio_pp_struct *pp, unsigned mask)
152 {
153 // good idea to update also the cache
154 pp->dcache = parport_read_data(pp->port);
155 return ((pp->dcache & mask) != 0);
156 }
157
158 /*
159 ** parport control bit input function
160 */
161 static inline int gpio_parport_ctrl_get(gpio_pp_struct *pp, unsigned mask)
162 {
163 // good idea to update also the cache
164 pp->ccache = parport_read_control(pp->port);
165 return ((pp->ccache & mask) != 0);
166 }
167
168 /*
169 ** parport status bit input function
170 */
171 static inline int gpio_parport_status_get(gpio_pp_struct *pp, unsigned mask)
172 {
173 unsigned int cache = parport_read_status(pp->port);
174 return ((cache & mask) != 0);
175 }
176
177
178 /*
179 ** general parport read bit function
180 */
181 static int gpio_parport_get(struct gpio_chip *gpio, unsigned offset)
182 {
183 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
184 int status = 0; /* inititalization ensures returm zero for unhandled bits */
185 unsigned long flags;
186
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);
202 }
203 return status;
204 }
205
206
207 /*----------------------------------------------------------------------*/
208 /*
209 ** parport data bit output function
210 */
211 static inline void gpio_parport_data_set(gpio_pp_struct *pp, unsigned mask, int value)
212 {
213 u8 byte = pp->dcache; // use old value from cache
214 if (value)
215 byte |= mask;
216 else
217 byte &= ~mask;
218 pp->dcache = byte; // restore cache
219 return parport_write_data(pp->port, byte);
220 }
221
222 /*
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
226 */
227 static inline void gpio_parport_ctrl_set(gpio_pp_struct *pp, unsigned mask, int value)
228 {
229 u8 byte = pp->ccache; // use old value from cache
230 if (value)
231 byte |= mask;
232 else
233 byte &= ~mask;
234 pp->ccache = byte; // restore cache
235 return parport_write_control(pp->port, byte);
236 }
237
238 /*
239 ** general parport set bit output function
240 */
241 static void gpio_parport_set(struct gpio_chip *gpio, unsigned offset, int value)
242 {
243 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
244 unsigned long flags;
245
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);
256 }
257 }
258
259 /*----------------------------------------------------------------------*/
260 /*
261 ** general parport direction input funtion
262 ** we support only the parport control bits as dedicated input bits
263 */
264 static int gpio_parport_direction_input(struct gpio_chip * gpio, unsigned offset)
265 {
266 if (offset < GPIO_PARPORT_STAT_PORT_BITS) {
267 return -ENOSYS;
268 } else if (offset < GPIO_PARPORT_NR_GPIOS) {
269 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
270 unsigned long flags;
271 gpio_parport_lock(&pp->lock, flags);
272 if ((GPIO_PARPORT_INTERRUPT == offset) && (pp->irq >= 0)) {
273 parport_enable_irq(pp->port);
274 }
275 gpio_parport_unlock(&pp->lock, flags);
276 return 0;
277 }
278 return -ENODEV;
279 }
280
281 /*
282 ** general parport direction output function
283 ** we support only data bits and control bits as dedicated output bits
284 */
285 static int gpio_parport_direction_output(struct gpio_chip * gpio, unsigned offset, int value)
286 {
287 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
288 unsigned long flags;
289
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);
295 return 0;
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);
301 return 0;
302 } else if (offset < GPIO_PARPORT_NR_GPIOS) {
303 return -ENOSYS;
304 }
305 return -ENODEV;
306 }
307
308 /*----------------------------------------------------------------------*/
309 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
310 /*
311 ** parport interrupt support only for acknoledge on GPI15 interrupt
312 */
313 static int gpio_parport_gpio_to_irq(struct gpio_chip * gpio, unsigned offset)
314 {
315 gpio_pp_struct *pp = container_of(gpio, gpio_pp_struct, gpio);
316
317 if (GPIO_PARPORT_INTERRUPT == offset) {
318 return pp->irq;
319 } else {
320 return -ENOSYS;
321 }
322 }
323 #endif
324
325 /*----------------------------------------------------------------------*/
326 /*
327 ** gpio_parport setup function
328 ** add all entries into the GPIO framework
329 */
330 static void gpio_parport_gpio_setup(gpio_pp_struct *pp)
331 {
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;
339 c->dbg_show = NULL;
340 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
341 c->request = NULL;
342 c->free = NULL;
343 c->to_irq = gpio_parport_gpio_to_irq;
344 #endif
345 c->base = modparam_gpiobase;
346 c->ngpio = GPIO_PARPORT_NR_GPIOS;
347 #ifdef CONFIG_GPIO_PARPORT_MUTEX
348 c->can_sleep = 1;
349 c->dev = pp->dev;
350 #else
351 c->can_sleep = 0;
352 #endif
353 }
354
355 /***************************************************************************
356 * Parallel port attaching and detaching routines *
357 ***************************************************************************/
358
359 static LIST_HEAD(gpio_parport_hosts);
360
361 /*
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.
366 */
367 static int __gpio_parport_attach(struct parport *pb)
368 {
369 gpio_pp_struct *pp;
370 int err;
371
372 /* we need a zero initialized structure here */
373 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
374 if (!pp)
375 return -ENOMEM;
376
377 #ifdef CONFIG_GPIO_PARPORT_MUTEX
378 mutex_init(&pp->lock);
379 #else
380 spin_lock_init(&pp->lock);
381 #endif
382
383 /* ----------------- ParPort Hooks ------------------- */
384 /* REVISIT: no probe, just think it is there */
385 pp->port = pb;
386
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 */
398 pp /* handle */ );
399 if (pp->dev == NULL) {
400 pr_err("%s: unable to register with parport\n", DRVNAME);
401 err = -ENOMEM;
402 goto err_release_mem;
403 }
404
405 err = parport_claim(pp->dev);
406 if (err < 0) {
407 pr_err("%s: unable to claim parport\n", DRVNAME);
408 err = -ENODEV;
409 goto err_unregister_parport;
410 }
411
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);
416
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);
424 } else {
425 pp->irq = -ENXIO;
426 }
427
428 /* ----------------- GPIO init ------------------- */
429 gpio_parport_gpio_setup(pp);
430 err = gpiochip_add(&pp->gpio);
431 if (err != 0) {
432 pr_err("%s: Failed to register GPIOs\n", DRVNAME);
433 goto err_release_parport;
434 }
435
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);
438
439 list_add_tail(&pp->list, &gpio_parport_hosts);
440 return 0;
441
442 err_release_parport:
443 parport_release(pp->dev);
444 err_unregister_parport:
445 parport_unregister_device(pp->dev);
446 err_release_mem:
447 kfree(pp);
448 return err;
449 }
450
451
452
453 static void gpio_parport_attach(struct parport *pb)
454 {
455 (void)__gpio_parport_attach(pb);
456 }
457
458
459 static int gpio_parport_remove(gpio_pp_struct *pp)
460 {
461 int err;
462 if (pp->irq >= 0) {
463 parport_disable_irq(pp->port);
464 }
465 err = gpiochip_remove(&pp->gpio);
466 if (err < 0)
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
471 return err;
472 }
473
474 static void gpio_parport_detach(struct parport *pb)
475 {
476 gpio_pp_struct *pp;
477
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);
482 kfree(pp);
483 break;
484 }
485 }
486 }
487
488 static struct parport_driver gpio_parport_driver = {
489 .name = DRVNAME,
490 .attach = gpio_parport_attach,
491 .detach = gpio_parport_detach,
492 };
493
494
495 static int __init gpio_parport_init(void)
496 {
497 return parport_register_driver(&gpio_parport_driver);
498 }
499 device_initcall(gpio_parport_init);
500
501 static void __exit gpio_parport_exit(void)
502 {
503 parport_unregister_driver(&gpio_parport_driver);
504 }
505 module_exit(gpio_parport_exit);
506
507 MODULE_AUTHOR("Option Wireless");
508 MODULE_DESCRIPTION("GPIO master controller driver for Parport Adapter");
509 MODULE_LICENSE("GPL");
510
511 MODULE_INFO(Flags, sCONFIG_GPIO_PARPORT_MUTEX);
512 MODULE_INFO(Version, DRIVER_VERSION);