spi_parport with proper irq support
[pub/spi-gpio-pp.git] / spi_parport.c
1 /*
2 * spi_parport.c - SPI master controller driver based on the parallel port adapter
3 *
4 * Copyright (C) 2008 Peter Henn <Peter.Henn@web.de>
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 spi_parport find its beginning in the spi_butterfly driver, but with the
21 * idea to isolate the SPI master controller part and move all additional SPI
22 * protocoll stuff into separate drivers.
23 * The most important settings can be given to the driver by module load parameter.
24 * Also the SPI-protocol driver can be configured by module load parameter. Make
25 * sure that all related SPI-protocol drivers are load before you load this SPI
26 * controller module!
27 * The SPI parport driver supports more than one SPI device. Therefore all module
28 * parameter can be given as an ARRAY with ',' as a delimiter. Starting from the
29 * first entry up to SPI_DEV_MAX devices will be handled. Therefore a successive
30 * chip selects will be reserved in the parport settings.
31 *
32 * Naturally this driver should normally use its gpio_parport base driver. But just
33 * to prevent additional performace issues I decice to finish this spi_parport.
34 * Both drivers may gives you also the possibility to use them hardware shared on
35 * one parport device, as long as you never use GPIO outputs in the gpio_parport from
36 * the parport data port and move the SPI control signals in the spi_parport driver
37 * to the parport control port - and visa verse, although its not a real software
38 * shared functionality.
39 */
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/device.h>
44 #include <linux/parport.h>
45 #include <linux/spinlock.h>
46
47 #include <linux/sched.h>
48 #include <linux/spi/spi.h>
49 #include <linux/spi/spi_bitbang.h>
50
51 #ifdef CONFIG_SPI_PARPORT_EXCLUSIVE
52 #define sCONFIG_SPI_PARPORT_EXCLUSIVE " CONFIG_SPI_PARPORT_EXCLUSIVE"
53 #else
54 #define sCONFIG_SPI_PARPORT_EXCLUSIVE ""
55 #endif
56
57 /* ToDo
58 * 1. Setup of the parport mode must be done. Therefore we read the
59 * supported modes: dev->dev->port->modes; Depending on the mode flags we change
60 * the mode value in the dev->dev->port->base_hi+0x02; Register, which is the ECR
61 * (extended control register). So first set to SPP (standard parport mode)
62 *
63 * Note: - SPI_LSB_FIRST is not yet supported and would need own txrx functions
64 * - SPI_LOOP is supported, but requires tx before rx in own transfer functions
65 * - SPI_3WIRE depends on signal routing, but is in principle possible
66 * and therefore supported.
67 * - SPI_CS_HIGH is supported
68 * - Slow CPU frequencies are currently not supported. The
69 * maximum Bit rate may be around 200 kBit/sec
70 *
71 * ToDo: - support also bits of the Control port
72 * - support more than one SPI controller
73 *
74 * Parallel
75 * Port Direction Register Name Signal
76 * ----------- --------- -------- ---------------------- ------
77 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE
78 * D0 2 --> D0 (1 << 0) SCLK
79 * D1 3 --> D1 (1 << 1) MOSI
80 * D2 4 --> D2 (1 << 2) nCS0
81 * D3 5 --> D3 (1 << 3) nCS1
82 * D4 6 --> D4 (1 << 4) nCS2
83 * D5 7 --> D5 (1 << 5) nCS3
84 * D6 8 --> D6 (1 << 6) --
85 * D7 9 --> D7 (1 << 7) --
86 * nAckn 10 <-- S6 PARPORT_STATUS_ACK
87 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY MISO
88 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT --
89 * Sel 13 <-- S4 PARPORT_STATUS_SELECT --
90 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD --
91 * nError 15 <-- S3 PARPORT_STATUS_ERROR --
92 * nInit 16 --> C2 PARPORT_CONTROL_INIT --
93 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT --
94 * GND 25 -- -- GND
95 *
96 * Signal:
97 * - SCLK : SPI Master Clock
98 * - MOSI : SPI Master Data Output, Slave Data Input
99 * - MISO : SPI Master Date Input, Slave Data Output
100 * - nCSx : SPI Chip Select x, default low active
101 * Note: nCSx must be in consecutive bit order!
102 */
103
104 //*****************************************************************************
105 // MODULE PARAMETERS
106 //*****************************************************************************
107 #define DRVNAME "spi_parport" /* name of the driver */
108 #define DRIVER_VERSION "0.3" /* helps identifying different versions of that driver */
109
110 #define SPI_PARPORT_SCLK (1 << 0) /* SPI Clock on parport D0 or pin 2 */
111 #define SPI_PARPORT_MOSI (1 << 1) /* SPI MOSI on parport D1 or pin 3 */
112 #define SPI_PARPORT_CS0 (1 << 2) /* first SPI chipselet on parport D2 or pin 4 */
113 #define SPI_PARPORT_MISO PARPORT_STATUS_BUSY /* SPI MISO on parport Status Busy or pin 11 */
114
115 #define SPI_PARPORT_STATUS_INV 0x80 /* invert internal bit 7 of status parport bit (Busy) */
116 #define SPI_PARPORT_CONTROL_INV 0x0b /* invert internal bit 0, 1 and 3 of control parport bits
117 (Strobe, Autofd, Select) */
118
119 #define SPI_PARPORT_MAX_SPEED 1*1000*1000 /* maximum allowed speed for any SPI device, currently not used */
120 #define SPI_DEV_MAX 4 /* maximum number of supported SPI devices and CS */
121 #define SPI_MODALIAS_LEN 32 /* spi protocol module alias name length, not defined in spi.h */
122
123
124 /* Module load parameter */
125 static char spi_modalias[SPI_DEV_MAX*(SPI_MODALIAS_LEN+1)]="";
126 static unsigned int spi_mode[SPI_DEV_MAX] = {SPI_MODE_0, SPI_MODE_0, SPI_MODE_0, SPI_MODE_0};
127 static int spi_mode_cnt = SPI_DEV_MAX;
128 static unsigned int spi_bits[SPI_DEV_MAX] = {8, 8, 8, 8};
129 static int spi_bits_cnt = SPI_DEV_MAX;
130 static int spi_irq[SPI_DEV_MAX] = {-1, -1, -1, -1};
131 static int spi_irq_cnt = SPI_DEV_MAX;
132
133 struct spi_parport {
134 struct spi_bitbang bitbang; /* SPI bitbang controller data, must be the first item */
135 struct pardevice *dev; /* parport device entry */
136 struct parport *port; /* parport port entry */
137 u8 dcache; /* parport data port mirror bits */
138 struct list_head list; /* spi_parport host controller list */
139 };
140
141 /* helps accessing the spi controller stucture */
142 static inline struct spi_parport *spidev_to_pp(struct spi_device *spi)
143 {
144 return spi->controller_data;
145 }
146 /*----------------------------------------------------------------------*/
147 /* spi_parport to spi_bitbang interface functions */
148
149 /**
150 * parport data bit output function
151 */
152 static inline void spi_parport_data_set(struct spi_parport *pp, unsigned mask, int value)
153 {
154 u8 byte = pp->dcache; // use old value from cache
155 if (value)
156 byte |= mask;
157 else
158 byte &= ~mask;
159 pp->dcache = byte; // restore cache
160 return parport_write_data(pp->port, byte);
161 }
162
163 /**
164 * set clock signal
165 */
166 static inline void setsck(struct spi_device *spi, int value)
167 {
168 struct spi_parport *pp = spidev_to_pp(spi);
169 return spi_parport_data_set(pp, SPI_PARPORT_SCLK, value);
170 }
171
172 /**
173 * set MOSI signal
174 */
175 static inline void setmosi(struct spi_device *spi, int value)
176 {
177 struct spi_parport *pp = spidev_to_pp(spi);
178 return spi_parport_data_set(pp, SPI_PARPORT_MOSI, value);
179 }
180
181 /*----------------------------------------------------------------------*/
182 /**
183 * parport status bit input function
184 */
185 static inline int spi_parport_status_get(struct spi_parport *pp, unsigned mask)
186 {
187 u8 byte = SPI_PARPORT_STATUS_INV ^ parport_read_status(pp->port);
188 return ((byte & mask) != 0);
189 }
190
191 /**
192 * parport data bit input function, only need for loop back mode
193 * Note: Neither the dcache is here updated nor the value is read from that dcache
194 * to ensure similar system timing behavior as in standard MISO read mode.
195 */
196 static inline int spi_parport_data_get(struct spi_parport *pp, unsigned mask)
197 {
198 u8 byte = parport_read_data(pp->port);
199 return ((byte & mask) != 0);
200 }
201
202 /**
203 * get MISO signal
204 * supports also loop back mode in master controller
205 */
206 static inline int getmiso(struct spi_device *spi)
207 {
208 int value;
209 struct spi_parport *pp = spidev_to_pp(spi);
210 if (spi->mode & SPI_LOOP) {
211 /* Loop back mode, read direct from output port is possible, */
212 /* because setmosi will be done before getmiso in txrx */
213 value = spi_parport_data_get(pp, SPI_PARPORT_MOSI);
214 } else {
215 /* default MISO read */
216 value = spi_parport_status_get(pp, SPI_PARPORT_MISO);
217 }
218 return value;
219 }
220
221 /**
222 * set chipselects
223 * sets corresponding CSx and CLK, depending on its polarity and mode
224 */
225 static void spi_parport_chipselect(struct spi_device *spi, int value)
226 {
227 struct spi_parport *pp = spidev_to_pp(spi);
228 /* set default clock polarity */
229 if (value == BITBANG_CS_INACTIVE) {
230 /* Deselect the slaves on the SPI bus by programming CS to inactive state */
231 spi_parport_data_set(pp, SPI_PARPORT_CS0 << spi->chip_select, (spi->mode & SPI_CS_HIGH)==0);
232 } else if (value == BITBANG_CS_ACTIVE) {
233 /* Set the SPI clock phase and polarity */
234 setsck(spi, (spi->mode & SPI_CPOL)!=0);
235 /* Activate the chip select by programming CS to active state */
236 if ((spi->chip_select >= 0) && (spi->chip_select < SPI_DEV_MAX)) {
237 spi_parport_data_set(pp, SPI_PARPORT_CS0 << spi->chip_select, (spi->mode & SPI_CS_HIGH)!=0);
238 } else {
239 pr_err("%s: Use chip %sCS%d select out of range CS0 - CS%d\n",
240 DRVNAME, ((spi->mode & SPI_CS_HIGH)!=0)?"":"/", spi->chip_select, SPI_DEV_MAX);
241 }
242 }
243 }
244
245 /* use default TXRX bitbang interface functions from header file */
246 #define EXPAND_BITBANG_TXRX
247
248 /* currently we omit any additional delay, because parport is even slow.
249 Naturally for X > 10000, a delay would become more important */
250 #define spidelay(X) do{}while(0)
251
252 #include <linux/spi/spi_bitbang.h>
253
254 static u32 spi_parport_txrx_word_mode0(struct spi_device *spi,
255 unsigned nsecs,
256 u32 word, u8 bits)
257 {
258 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
259 }
260
261 static u32 spi_parport_txrx_word_mode1(struct spi_device *spi,
262 unsigned nsecs,
263 u32 word, u8 bits)
264 {
265 return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
266 }
267
268 static u32 spi_parport_txrx_word_mode2(struct spi_device *spi,
269 unsigned nsecs,
270 u32 word, u8 bits)
271 {
272 return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
273 }
274
275 static u32 spi_parport_txrx_word_mode3(struct spi_device *spi,
276 unsigned nsecs,
277 u32 word, u8 bits)
278 {
279 return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
280 }
281
282
283 /*----------------------------------------------------------------------*/
284 #define param_check_irq(name, p) /* define own irq type */
285 #define PARAM_IRQ_VAL_NONE -1 /* value, if 'none' irq is used */
286 #define PARAM_IRQ_VAL_AUTO -2 /* value, if 'auto' irq is used */
287 #define PARAM_IRQ_NAME_NONE "none" /* string, if no irq is used */
288 #define PARAM_IRQ_NAME_AUTO "auto" /* string, if irq is automatically determined by the parport irq line */
289
290 /**
291 * special irq parameter type
292 * The irq type allows setting of irq numbers in the range of all positive integer values
293 * and additionally the values "none" and "auto". These extra values will be converted into
294 * corresponding negative values -1 and -2. As exception is also the value -1 allowed, which
295 * will be seen as "none".
296 */
297 int param_set_irq(const char *pbuf, struct kernel_param *kp)
298 {
299 int number;
300
301 if (0 == strncmp(pbuf, PARAM_IRQ_NAME_NONE, sizeof(PARAM_IRQ_NAME_NONE))) {
302 number = PARAM_IRQ_VAL_NONE;
303 } else if (0 == strncmp(pbuf, PARAM_IRQ_NAME_AUTO, sizeof(PARAM_IRQ_NAME_AUTO))) {
304 number = PARAM_IRQ_VAL_AUTO;
305 } else {
306 char *ep;
307 number = simple_strtol(pbuf, &ep, 0);
308 if (ep != pbuf) {
309 if ((0 > number) && (PARAM_IRQ_VAL_NONE != number))
310 return -EINVAL;
311 } else
312 return -EINVAL;
313 }
314 *(int *)kp->arg = number;
315 return 0;
316 }
317
318 int param_get_irq(char *pbuf, struct kernel_param *kp)
319 {
320 int value = *(int *)kp->arg;
321 if (PARAM_IRQ_VAL_NONE == value)
322 return sprintf((char *)pbuf, "%s", PARAM_IRQ_NAME_NONE);
323 else if (PARAM_IRQ_VAL_AUTO == value)
324 return sprintf((char *)pbuf, "%s", PARAM_IRQ_NAME_AUTO);
325 else if (0 <= value)
326 return sprintf((char *)pbuf, "%d", value);
327 else
328 return -EINVAL;
329 }
330
331 /**
332 * copies src to dest string up to count characters or until a character is found
333 * which matches the delim string set. As the return value will be returned the next
334 * address of src, which can be used for the next stnarr of NULL if the found string
335 * was longer than count.
336 */
337 const char *strnarr(char *dst, const char *src, const char *dlim, size_t count)
338 {
339 int i;
340 for(i=0; i<count; i++) {
341 if (*src == '\0') {
342 *dst='\0';
343 return src;
344 }
345 if (strchr(dlim, *src)!=NULL) {
346 *dst='\0';
347 return ++src;
348 }
349 *dst++=*src++;
350 }
351 *--dst='\0'; /* add missing line end */
352 return NULL;
353 }
354
355
356 /**
357 * spi parport registers the SPI devices, defined by the given module load parameters.
358 * Although the bit_per_word value is strongly recommended to set by the protocol driver
359 * itself, spi_parport_register_spi_devices supports that for buggy drivers like the lm70.c
360 * SPI temperature driver. But that requires using spi_add_device instead of spi_new_device
361 * to ensure setting that bit_per_word value just before the protocol driver may change and
362 * use that value for probing the connected SPI device hardware.
363 *
364 * Additionally all SPI data structures will be first setup and all used spi chipselects will
365 * be initialized to its inactive state before the spi_add_device is called. This ensures that
366 * no early spi device usage by a spi protocol drivers probe sequence may toggle the spi clock,
367 * although there might be another spi device at the bus, which has not yet properly setup its
368 * chip select state as inactive.
369 */
370 static int spi_parport_register_spi_devices(struct spi_parport *pp)
371 {
372 const char *modalias_param=spi_modalias;
373 struct spi_device *spi[SPI_DEV_MAX]; /* fill up here all settings instead using a board_info struct */
374 int i; /* loop counter */
375 int max; /* maximum loop value */
376
377 /* loop over all SPI device informations found in module parameter settings */
378 for (i=0; (modalias_param[0]!='\0') && (i<SPI_DEV_MAX); i++) {
379 spi[i] = spi_alloc_device(pp->bitbang.master); /* kzalloc memory and add device to master controller */
380 if (NULL == spi[i]) {
381 while (i > 0) {
382 spi_dev_put(spi[--i]); /* free all spi devices from former spi_alloc_device */
383 }
384 return -ENOMEM;
385 }
386
387 /*
388 * first copy the modalias parameter
389 */
390 modalias_param=strnarr(spi[i]->modalias, modalias_param, " ,", sizeof(spi[i]->modalias));
391 if (NULL == spi[i]->modalias) {
392 pr_err("%s: %d given modalias name is too long! See only that '%s'!\n",
393 DRVNAME, i, spi[i]->modalias);
394 while (i >= 0) {
395 spi_dev_put(spi[i--]); /* free all spi devices */
396 }
397 return -EINVAL;
398 }
399
400 /*
401 * now setup the rest as it would be done by
402 * the spi_new_device from a given board_info
403 * structure.
404 */
405 spi[i]->max_speed_hz = SPI_PARPORT_MAX_SPEED;
406 spi[i]->chip_select = i;
407 spi[i]->master = pp->bitbang.master;
408 spi[i]->controller_data = pp; /* enable access to primary controller data */
409 spi[i]->controller_state = NULL; /* init state like in new_spi_device */
410 spi[i]->mode = spi_mode[i];
411 spi[i]->irq = spi_irq[i];
412 spi[i]->bits_per_word = spi_bits[i]; /* must normally be done by protocol driver */
413 spi[i]->dev.platform_data = (void *)NULL; /* data area of SPI protocol driver,
414 hot plug drivers must do that itself */
415 /*
416 * first only initialize the chipselect
417 * for that spi device to set it inactive
418 */
419 spi_parport_chipselect(spi[i], BITBANG_CS_INACTIVE);
420 if (spi_mode_cnt < (i+1)) {
421 pr_warning("%s: No spi mode parameter given for device %d with driver '%s'. "
422 "Will use default setting: %d\n",
423 DRVNAME, i, spi[i]->modalias, spi[i]->mode);
424 }
425 }
426 /* additional parameter checks and possibly logged warnings */
427 if (modalias_param[0]!='\0'){
428 pr_warning("%s: Found more modalias parameters of SPI devices than allowed (max: %d)!\n",
429 DRVNAME, SPI_DEV_MAX);
430 }
431 if (spi_mode_cnt > i) {
432 pr_warning("%s: Found further %d mode parameters than SPI devices used or no SPI mode defined!\n",
433 DRVNAME, spi_mode_cnt-i);
434 }
435 /*
436 * Now all spi device structures are setup and all used
437 * chipselects are initialized. We will now add the
438 * spi devices for probing and usage. We use the same
439 * ordering!
440 */
441 max = i; /* remember the last value */
442 for (i=0; i<max; i++) {
443 int err;
444
445 err = spi_add_device(spi[i]);
446 if (err < 0) {
447 /*
448 * free all further spi devices too.
449 * all already added spi devices will be freed
450 * later with the final call to spi_master_put
451 */
452 while (i<max) {
453 spi_dev_put(spi[i++]);
454 }
455 pr_err("%s: spi_new_device failed!\n", DRVNAME);
456 return -ENODEV;
457 }
458 if (0 <= spi[i]->irq) {
459 pr_info("%s: SPI device successfully registered at bus %s as device %s with mode 0x%x,"
460 " %d bits/word, irq %d\n", DRVNAME, spi[i]->dev.bus_id, spi[i]->modalias,
461 spi[i]->mode, spi[i]->bits_per_word, spi[i]->irq);
462 } else {
463 pr_info("%s: SPI device successfully registered at bus %s as device %s with mode 0x%x,"
464 " %d bits/word, no irq\n", DRVNAME, spi[i]->dev.bus_id, spi[i]->modalias,
465 spi[i]->mode, spi[i]->bits_per_word);
466 }
467 }
468 return i;
469 }
470
471 /***************************************************************************
472 * Parallel port attaching and detaching routines *
473 ***************************************************************************/
474
475 static LIST_HEAD(spi_parport_hosts);
476
477 /**
478 * If attaching the driver to parport is done with exclusive access, the driver
479 * is still loaded, although the attach might not be done successful.
480 * If you have already registered e.g. the lp driver, please unload any drivers
481 * first as long as you use exclusive access.
482 */
483 static int __spi_parport_attach(struct parport *pb)
484 {
485 struct spi_master *master; /* is only temporary need */
486 struct device *dev = pb->physport->dev;
487 struct spi_parport *pp;
488 int err;
489 int i, use_irq;
490
491 if (dev == NULL) {
492 return -ENODEV;
493 }
494
495 /* spi_alloc_master gives zero initialized structure */
496 master = spi_alloc_master(dev, sizeof *pp);
497 if (master == NULL) {
498 return -ENOMEM;
499 }
500 master->bus_num = -1; /* dynamic allocation of a bus number */
501 master->num_chipselect = SPI_DEV_MAX; /* setup maximum capability */
502
503 /* call the container_of fkt */
504 pp = spi_master_get_devdata(master);
505
506 if (pp == NULL) {
507 err = -ENOMEM;
508 goto err_release_mem;
509 }
510
511 /* ----------------- SPI BitBang Hook ---------------- */
512 /* use default setup(), cleanup(), and transfer() methods */
513 pp->bitbang.master = spi_master_get(master); /* now pp->bitbang.master = master */
514 pp->bitbang.chipselect = spi_parport_chipselect;
515 pp->bitbang.txrx_word[SPI_MODE_0] = spi_parport_txrx_word_mode0;
516 pp->bitbang.txrx_word[SPI_MODE_1] = spi_parport_txrx_word_mode1;
517 pp->bitbang.txrx_word[SPI_MODE_2] = spi_parport_txrx_word_mode2;
518 pp->bitbang.txrx_word[SPI_MODE_3] = spi_parport_txrx_word_mode3;
519 pp->bitbang.flags = SPI_CS_HIGH | SPI_3WIRE | SPI_LOOP; /* setup supported mode flags */
520
521 /* ----------------- ParPort Hook -------------------- */
522 /* REVISIT: no probe, just think it is there */
523 pp->port = pb;
524
525 //ToDo: share parport with other device, but may need a mutex lock!
526 //ToDo: check supported modes are ok
527 //ToDo: PM support, e.g. Suspend and Resume
528 pp->dev = parport_register_device(pb, /* port */
529 DRVNAME, /* driver name */
530 NULL, /* preemption fkt */
531 NULL, /* wake-up fkt */
532 NULL, /* no interrupt handler used */
533 #ifdef CONFIG_SPI_PARPORT_EXCLUSIVE
534 PARPORT_FLAG_EXCL, /* flags */
535 #else
536 0, /* flags, 0 means used port shared */
537 #endif
538 pp /* handle */ );
539 if (pp->dev == NULL) {
540 pr_err("%s: unable to register with parport\n", DRVNAME);
541 err = -ENOMEM;
542 goto err_release_mem;
543 }
544
545 err = parport_claim(pp->dev);
546 if (err < 0) {
547 pr_err("%s: unable to claim parport\n", DRVNAME);
548 err = -ENODEV;
549 goto err_unregister_parport;
550 }
551
552 /* ----------------- SPI ParPort init ---------------- */
553 /* cache initialization just preventing changing unused bits */
554 pp->dcache = parport_read_data(pp->port);
555
556 /*
557 * loop over all irq module parameter, which must be exchanged by
558 * the parport irq, if value requires PARAM_IRQ_VAL_AUTO
559 */
560 for (use_irq = 0, i=0; i < spi_irq_cnt; i++) {
561 if (PARAM_IRQ_VAL_AUTO == spi_irq[i]) {
562 if (PARPORT_IRQ_NONE != pp->port->irq) {
563 spi_irq[i] = pp->port->irq; /* use parport irq */
564 use_irq = 1;
565 } else {
566 pr_warning("%s: Can not auto resolve IRQ, because parport has no valid IRQ\n",
567 DRVNAME);
568 break; /* further replacement makes no sense */
569 }
570 } else if (PARAM_IRQ_VAL_NONE != spi_irq[i]) {
571 if ((PARPORT_IRQ_NONE != pp->port->irq) && (spi_irq[i] == pp->port->irq))
572 use_irq = 1;
573 }
574 }
575 if (use_irq) {
576 /* we free the standard parport handler, because we want to use our own later */
577 free_irq(pp->port->irq, pp->port);
578 pr_info("%s: will use IRQ %d exclusive\n", DRVNAME, pp->port->irq);
579 pr_info("%s: Note parport driver will claim later irq as already freed during its module unload\n",
580 DRVNAME);
581 }
582
583 err = spi_bitbang_start(&pp->bitbang);
584 if (err < 0) {
585 pr_warning("%s: spi_bitbang_start failed with status %d\n",
586 DRVNAME, err);
587 goto err_release_parport;
588 }
589
590 /* ----------------- SPI BoardInfo init -------------- */
591 err = spi_parport_register_spi_devices(pp);
592 if (err == 0) {
593 pr_err("%s: at least one protocol driver name is needed, non found!\n", DRVNAME);
594 err = -ENODEV;
595 goto err_spi_controller_stop;
596 } else if (err < 0) {
597 goto err_spi_controller_stop;
598 }
599 list_add_tail(&pp->list, &spi_parport_hosts);
600 return 0;
601
602 err_spi_controller_stop:
603 /* this will unregister child SPI devices too, if there any */
604 spi_bitbang_stop(&pp->bitbang);
605 err_release_parport:
606 parport_release(pp->dev);
607 err_unregister_parport:
608 parport_unregister_device(pp->dev);
609 err_release_mem:
610 spi_master_put(master); /* will release memory */
611 return err;
612 }
613
614
615 static void spi_parport_attach(struct parport *pb)
616 {
617 (void)__spi_parport_attach(pb);
618 }
619
620
621 static int spi_parport_remove(struct spi_parport *pp)
622 {
623 int err;
624
625 /* stop and unregisters child devices too */
626 err = spi_bitbang_stop(&pp->bitbang);
627 if (err < 0) {
628 pr_err("%s: bitbang stop error: %d\n", DRVNAME, err);
629 }
630 parport_release(pp->dev);
631 parport_unregister_device(pp->dev);
632 /* this frees also the pp struct */
633 spi_master_put(pp->bitbang.master);
634 return err;
635 }
636
637 static void spi_parport_detach(struct parport *pb)
638 {
639 struct spi_parport *pp;
640
641 list_for_each_entry(pp, &spi_parport_hosts, list) {
642 if (pp->port == pb) {
643 list_del_init(&pp->list);
644 (void)spi_parport_remove(pp);
645 break;
646 }
647 }
648 }
649
650 static struct parport_driver spi_parport_driver = {
651 .name = DRVNAME,
652 .attach = spi_parport_attach,
653 .detach = spi_parport_detach,
654 };
655
656
657 static int __init spi_parport_init(void)
658 {
659 return parport_register_driver(&spi_parport_driver);
660 }
661 device_initcall(spi_parport_init);
662
663 static void __exit spi_parport_exit(void)
664 {
665 parport_unregister_driver(&spi_parport_driver);
666 }
667 module_exit(spi_parport_exit);
668
669 MODULE_AUTHOR("Option Wireless");
670 MODULE_DESCRIPTION("SPI master controller driver for Parport Adapter");
671 MODULE_LICENSE("GPL");
672
673 module_param_array_named(irq, spi_irq, irq, &spi_irq_cnt, S_IRUGO);
674 MODULE_PARM_DESC(irq, "array of irq numbers for each driver");
675
676 module_param_string(pdrv, spi_modalias, sizeof(spi_modalias), S_IRUGO);
677 MODULE_PARM_DESC(pdrv, "array of spi protocol driver names");
678
679 module_param_array_named(mode, spi_mode, uint, &spi_mode_cnt, S_IRUGO);
680 MODULE_PARM_DESC(mode, "array of spi modes for each driver");
681
682 module_param_array_named(bits, spi_bits, uint, &spi_bits_cnt, S_IRUGO);
683 MODULE_PARM_DESC(bits, "array of spi bits per word for each driver");
684
685 MODULE_INFO(Flags, sCONFIG_SPI_PARPORT_EXCLUSIVE);
686 MODULE_INFO(Version, DRIVER_VERSION);