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