2 * spi_parport.c - SPI master controller driver based on the parallel port adapter
4 * Copyright (C) 2008 Peter Henn <Peter.Henn@web.de>
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 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
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.
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.
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>
47 #include <linux/sched.h>
48 #include <linux/spi/spi.h>
49 #include <linux/spi/spi_bitbang.h>
51 #ifdef CONFIG_SPI_PARPORT_EXCLUSIVE
52 #define sCONFIG_SPI_PARPORT_EXCLUSIVE " CONFIG_SPI_PARPORT_EXCLUSIVE"
54 #define sCONFIG_SPI_PARPORT_EXCLUSIVE ""
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. first setup all CS, because spi_new_device or spi_add_device will call
63 * probe fkt of the SPI protocoll driver. This might initiate a SPI transfer
64 * and triggers the SCLK, which is worse for other chips on the SPI-BUS using
65 * the wrong CS polarity
66 * 2. use spi_add_device instead of spi_new_device. That's the only chance
67 * to setup the also the SPI bits per word value, just before any transaction
68 * might be initiated with the SPI probe fkt of the SPI protocol driver.
69 * Setting this spi->bits_per_word value afterwards might be here too late.
70 * Note that bits_per_word is normally has to be set normally by the protocoll
71 * driver (e.g. a bad style of the lm70.c driver, which guess that the value is
73 * Therefore we MUST support a mode, where NOTHING will be changed!
74 * 3. support better irq via array load parameter and values like AUTO, NONE. Additionally
75 * don't free any interrupt, if no interrupt might be requested
76 * 4. output warning, if open mode, bits values after finish init
79 * Note: - SPI_LSB_FIRST is not yet supported and would need own txrx functions
80 * - SPI_LOOP is supported, but requires tx before rx in own transfer functions
81 * - SPI_3WIRE depends on signal routing, but is in principle possible
82 * and therefore supported.
83 * - SPI_CS_HIGH is supported
84 * - Slow CPU frequencies are currently not supported. The
85 * maximum Bit rate may be around 200 kBit/sec
87 * ToDo: - support also bits of the Control port
88 * - support more than one SPI controller
91 * Port Direction Register Name Signal
92 * ----------- --------- -------- ---------------------- ------
93 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE
94 * D0 2 --> D0 (1 << 0) SCLK
95 * D1 3 --> D1 (1 << 1) MOSI
96 * D2 4 --> D2 (1 << 2) nCS0
97 * D3 5 --> D3 (1 << 3) nCS1
98 * D4 6 --> D4 (1 << 4) --
99 * D5 7 --> D5 (1 << 5) --
100 * D6 8 --> D6 (1 << 6) --
101 * D7 9 --> D7 (1 << 7) --
102 * nAckn 10 <-- S6 PARPORT_STATUS_ACK
103 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY MISO
104 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT --
105 * Sel 13 <-- S4 PARPORT_STATUS_SELECT --
106 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD --
107 * nError 15 <-- S3 PARPORT_STATUS_ERROR --
108 * nInit 16 --> C2 PARPORT_CONTROL_INIT --
109 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT --
113 * - SCLK : SPI Master Clock
114 * - MOSI : SPI Master Data Output, Slave Data Input
115 * - MISO : SPI Master Date Input, Slave Data Output
116 * - nCSx : SPI Chip Select x, default low active
117 * Note: nCSx must be in consecutive bit order!
120 //*****************************************************************************
122 //*****************************************************************************
123 #define DRVNAME "spi_parport" /* name of the driver */
124 #define DRIVER_VERSION "0.2" /* helps identifing different versions of that driver */
126 #define SPI_PARPORT_SCLK (1 << 0) /* SPI Clock on parport D0 or pin 2 */
127 #define SPI_PARPORT_MOSI (1 << 1) /* SPI MOSI on parport D1 or pin 3 */
128 #define SPI_PARPORT_CS0 (1 << 2) /* first SPI chipselet on parport D2 or pin 4 */
129 #define SPI_PARPORT_MISO PARPORT_STATUS_BUSY /* SPI MISO on parport Status Busy or pin 11 */
131 #define SPI_PARPORT_STATUS_INV 0x80 /* invert internal bit 7 of status parport bit (Busy) */
132 #define SPI_PARPORT_CONTROL_INV 0x0b /* invert internal bit 0, 1 and 3 of control parport bits
133 (Strobe, Autofd, Select) */
135 #define SPI_PARPORT_MAX_SPEED 1*1000*1000 /* maximum allowed speed for any SPI device, currently not used */
136 #define SPI_DEV_MAX 4 /* maximum number of supported SPI devices and CS */
137 #define SPI_MODALIAS_LEN 32 /* spi protocol module alias name length, not defined in spi.h */
139 /* Module load parameter */
140 static int irq
=-1; /* force irq for parport pin 10 */
141 static char spi_modalias
[SPI_DEV_MAX
*(SPI_MODALIAS_LEN
+1)]="";
142 static unsigned int spi_mode
[SPI_DEV_MAX
] = {SPI_MODE_0
, SPI_MODE_0
, SPI_MODE_0
, SPI_MODE_0
};
143 static int spi_mode_cnt
= SPI_DEV_MAX
;
144 static unsigned int spi_bits
[SPI_DEV_MAX
] = {8, 8, 8, 8};
145 static int spi_bits_cnt
= SPI_DEV_MAX
;
148 struct spi_bitbang bitbang
; /* SPI bitbang controller data, must be the first item */
149 struct pardevice
*dev
; /* parport device entry */
150 struct parport
*port
; /* parport port entry */
151 u8 dcache
; /* parport data port mirror bits */
152 int irq
; /* parport interrupt */
153 struct list_head list
; /* spi_parport host controller list */
156 /* helps accessing the spi controller stucture */
157 static inline struct spi_parport
*spidev_to_pp(struct spi_device
*spi
)
159 return spi
->controller_data
;
161 /*----------------------------------------------------------------------*/
162 /* spi_parport to spi_bitbang interface functions */
165 ** parport data bit output function
167 static inline void spi_parport_data_set(struct spi_parport
*pp
, unsigned mask
, int value
)
169 u8 byte
= pp
->dcache
; // use old value from cache
174 pp
->dcache
= byte
; // restore cache
175 return parport_write_data(pp
->port
, byte
);
181 static inline void setsck(struct spi_device
*spi
, int value
)
183 struct spi_parport
*pp
= spidev_to_pp(spi
);
184 return spi_parport_data_set(pp
, SPI_PARPORT_SCLK
, value
);
190 static inline void setmosi(struct spi_device
*spi
, int value
)
192 struct spi_parport
*pp
= spidev_to_pp(spi
);
193 return spi_parport_data_set(pp
, SPI_PARPORT_MOSI
, value
);
196 /*----------------------------------------------------------------------*/
198 ** parport status bit input function
200 static inline int spi_parport_status_get(struct spi_parport
*pp
, unsigned mask
)
202 u8 byte
= SPI_PARPORT_STATUS_INV
^ parport_read_status(pp
->port
);
203 return ((byte
& mask
) != 0);
207 ** parport data bit input function, only need for loop back mode
208 ** Note: Neiter the dcache is here updated nor the value is read from that dcache
209 ** to ensure similar system timing bahaviour as in standard MISO read mode.
211 static inline int spi_parport_data_get(struct spi_parport
*pp
, unsigned mask
)
213 u8 byte
= parport_read_data(pp
->port
);
214 return ((byte
& mask
) != 0);
219 ** supports also loop back mode in master controller
221 static inline int getmiso(struct spi_device
*spi
)
224 struct spi_parport
*pp
= spidev_to_pp(spi
);
225 if (spi
->mode
& SPI_LOOP
) {
226 /* Loop back mode, read direct from output port is possible, */
227 /* because setmosi will be done before getmiso in txrx */
228 value
= spi_parport_data_get(pp
, SPI_PARPORT_MOSI
);
230 /* default MISO read */
231 value
= spi_parport_status_get(pp
, SPI_PARPORT_MISO
);
238 ** sets corresponding CSx and CLK, depending on its polarity and mode
240 static void spi_parport_chipselect(struct spi_device
*spi
, int value
)
242 struct spi_parport
*pp
= spidev_to_pp(spi
);
243 /* set default clock polarity */
244 if (value
== BITBANG_CS_INACTIVE
) {
245 /* Deselect the slaves on the SPI bus by programming CS to inactive state */
246 spi_parport_data_set(pp
, SPI_PARPORT_CS0
<< spi
->chip_select
, (spi
->mode
& SPI_CS_HIGH
)==0);
247 } else if (value
== BITBANG_CS_ACTIVE
) {
248 /* Set the SPI clock phase and polarity */
249 setsck(spi
, (spi
->mode
& SPI_CPOL
)!=0);
250 /* Activate the chip select by programming CS to active state */
251 if ((spi
->chip_select
>= 0) && (spi
->chip_select
< SPI_DEV_MAX
)) {
252 spi_parport_data_set(pp
, SPI_PARPORT_CS0
<< spi
->chip_select
, (spi
->mode
& SPI_CS_HIGH
)!=0);
254 pr_err("%s: Use chip %sCS%d select out of range CS0 - CS%d\n",
255 DRVNAME
, ((spi
->mode
& SPI_CS_HIGH
)!=0)?
"":"/", spi
->chip_select
, SPI_DEV_MAX
);
260 /* use default TXRX bitbang interface functions from header file */
261 #define EXPAND_BITBANG_TXRX
263 /* currently we omit any additional delay, because parport is even slow.
264 Naturally for X > 10000, a delay would become more important */
265 #define spidelay(X) do{}while(0)
267 #include <linux/spi/spi_bitbang.h>
269 static u32
spi_parport_txrx_word_mode0(struct spi_device
*spi
,
273 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
276 static u32
spi_parport_txrx_word_mode1(struct spi_device
*spi
,
280 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
283 static u32
spi_parport_txrx_word_mode2(struct spi_device
*spi
,
287 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
290 static u32
spi_parport_txrx_word_mode3(struct spi_device
*spi
,
294 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
298 /*----------------------------------------------------------------------*/
300 ** copies src to dest string up to count characters or until a character is found
301 ** which matches the delim string set. As the return value will be returned the next
302 ** address of src, which can be used for the next stnarr of NULL if the found string
303 ** was longer than count.
305 const char *strnarr(char *dst
, const char *src
, const char *dlim
, size_t count
)
308 for(i
=0; i
<count
; i
++) {
313 if (strchr(dlim
, *src
)!=NULL
) {
319 *--dst
='\0'; /* add missing line end */
325 void parse_irqs(int nports
, const char *irqstr
, int irqval
[])
328 for (i
=0; i
<nports
&& irgstr
; i
++){
329 if (!strncmp(irqstr
, "auto", 4))
331 else if (!strncmp(irqstr
, "none", 4))
335 unsigned long r
= simple_strtoul(irqstr
, &ep
, 0);
339 printk("... bad irq specifier '%s'\n", irqstr
);
343 irqstr
= strchr(irqstr
, ',');
344 if (irqstr
) irqstr
++;
351 ** spi parport registers SPI devices, defined by the given module load parameters.
353 static int spi_parport_register_spi_devices(struct spi_parport
*pp
)
355 const char *modalias_param
=spi_modalias
;
358 /* loop over all SPI device informations found in module parameter settings */
359 while ((modalias_param
[0]!='\0') && (i
<SPI_DEV_MAX
)) {
360 struct spi_board_info chip
; /* parport SPI board description placeholder*/
361 struct spi_device
*proxy
; /* just need to check spi device result and add additional settings */
362 modalias_param
=strnarr(chip
.modalias
, modalias_param
, " ,", sizeof(chip
.modalias
));
363 if (modalias_param
==NULL
) {
364 pr_err("%s: %d given modalias name is too long! See only that '%s'!\n", DRVNAME
, i
, chip
.modalias
);
369 * The modalias name MUST match the device_driver name
370 * for the bus glue code to match and subsequently bind them.
372 chip
.max_speed_hz
= SPI_PARPORT_MAX_SPEED
;
373 chip
.chip_select
= i
;
374 chip
.mode
= spi_mode
[i
];
376 chip
.controller_data
= pp
; /* enable access to primary controller data */
377 chip
.platform_data
= NULL
; /* data area of SPI protocol driver */
378 /* chip.bus_num = master->bus_num; // not needed for spi_new_device */
379 i
++; /* found new SPI protocol driver */
381 if (spi_mode_cnt
< i
) {
382 pr_warning("%s: No spi mode parameter given for device %d with driver '%s'. "
383 "Will use default setting: %d\n",
384 DRVNAME
, i
, chip
.modalias
, chip
.mode
);
386 pr_info("%s: Will load protocol driver: '%s' on CS%d with mode 0x%02x!\n",
387 DRVNAME
, chip
.modalias
, chip
.chip_select
, chip
.mode
);
390 * Register spi device from board info settings.
391 * Note that this will also call the probe inside the SPI protocall driver
393 proxy
= spi_new_device(pp
->bitbang
.master
, &chip
);
395 pr_err("%s: spi_new_device failed!\n", DRVNAME
);
398 if (spi_bits_cnt
< i
) {
399 pr_warning("%s: No spi bits per word parameter given for device %d with driver '%s'. "
400 "Will use default setting: %d\n",
401 DRVNAME
, i
, chip
.modalias
, spi_bits
[i
-1]);
403 proxy
->bits_per_word
=spi_bits
[i
-1];
404 pr_info("%s: SPI device successfully registered at bus %s as device %s with %d bits/word\n", DRVNAME
,
405 proxy
->dev
.bus_id
, chip
.modalias
, proxy
->bits_per_word
);
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
);
414 /***************************************************************************
415 * Parallel port attaching and detaching routines *
416 ***************************************************************************/
418 static LIST_HEAD(spi_parport_hosts
);
421 ** If attaching the driver to parport is done with exclusive access, the driver
422 ** is still loaded, although the attach might not be done successful.
423 ** If you have already registered e.g. the lp driver, please unload any drivers
424 ** first as long as you use exclusive access.
426 static int __spi_parport_attach(struct parport
*pb
)
428 struct spi_master
*master
; /* is only temporary need */
429 struct device
*dev
= pb
->physport
->dev
;
430 struct spi_parport
*pp
;
437 /* spi_alloc_master gives zero initialized structure */
438 master
= spi_alloc_master(dev
, sizeof *pp
);
439 if (master
== NULL
) {
442 master
->bus_num
= -1; /* dynamic allocation of a bus number */
443 master
->num_chipselect
= SPI_DEV_MAX
; /* setup maximum capability */
445 /* call the container_of fkt */
446 pp
= spi_master_get_devdata(master
);
450 goto err_release_mem
;
453 /* ----------------- SPI BitBang Hook ---------------- */
454 /* use default setup(), cleanup(), and transfer() methods */
455 pp
->bitbang
.master
= spi_master_get(master
); /* now pp->bitbang.master = master */
456 pp
->bitbang
.chipselect
= spi_parport_chipselect
;
457 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = spi_parport_txrx_word_mode0
;
458 pp
->bitbang
.txrx_word
[SPI_MODE_1
] = spi_parport_txrx_word_mode1
;
459 pp
->bitbang
.txrx_word
[SPI_MODE_2
] = spi_parport_txrx_word_mode2
;
460 pp
->bitbang
.txrx_word
[SPI_MODE_3
] = spi_parport_txrx_word_mode3
;
461 pp
->bitbang
.flags
= SPI_CS_HIGH
| SPI_3WIRE
| SPI_LOOP
; /* setup supported mode flags */
463 /* ----------------- ParPort Hook -------------------- */
464 /* REVISIT: no probe, just think it is there */
467 //ToDo: share parport with other device, but may need a mutex lock!
468 //ToDo: check supported modes are ok
469 //ToDo: PM support, e.g. Suspend and Resume
470 pp
->dev
= parport_register_device(pb
, /* port */
471 DRVNAME
, /* driver name */
472 NULL
, /* preemption fkt */
473 NULL
, /* wake-up fkt */
474 NULL
, /* no interrupt handler used */
475 #ifdef CONFIG_SPI_PARPORT_EXCLUSIVE
476 PARPORT_FLAG_EXCL
, /* flags */
478 0, /* flags, 0 means used port shared */
481 if (pp
->dev
== NULL
) {
482 pr_err("%s: unable to register with parport\n", DRVNAME
);
484 goto err_release_mem
;
487 err
= parport_claim(pp
->dev
);
489 pr_err("%s: unable to claim parport\n", DRVNAME
);
491 goto err_unregister_parport
;
494 /* ----------------- SPI ParPort init ---------------- */
495 /* cache initialisation just preventing changing unused bits */
496 pp
->dcache
= parport_read_data(pp
->port
);
500 pr_info("%s: force usage IRQ %d\n", DRVNAME
, pp
->irq
);
501 if(pp
->port
->irq
== pp
->irq
) {
502 /* we free the standard parport handler, because we want to use our own later */
503 free_irq(pp
->irq
, pp
->port
);
504 pr_info("%s: Note parport driver will claim later irq as already freed during its module unload\n",
507 } else if(pp
->port
->irq
!= PARPORT_IRQ_NONE
) {
508 pp
->irq
= pp
->port
->irq
;
509 /* we free the standard parport handler, because we want to use our own later */
510 free_irq(pp
->irq
, pp
->port
);
511 pr_info("%s: will use IRQ %d exclusive\n", DRVNAME
, pp
->irq
);
512 pr_info("%s: Note parport driver will claim later irq as already freed during its module unload\n", DRVNAME
);
517 err
= spi_bitbang_start(&pp
->bitbang
);
519 pr_warning("%s: spi_bitbang_start failed with status %d\n",
521 goto err_release_parport
;
524 /* ----------------- SPI BoardInfo init -------------- */
525 err
= spi_parport_register_spi_devices(pp
);
527 pr_err("%s: at least one protocol driver name is needed, non found!\n", DRVNAME
);
529 goto err_spi_controller_stop
;
530 } else if (err
< 0) {
531 goto err_spi_controller_stop
;
533 list_add_tail(&pp
->list
, &spi_parport_hosts
);
536 err_spi_controller_stop
:
537 /* this will unregister child SPI devices too, if there any */
538 spi_bitbang_stop(&pp
->bitbang
);
540 parport_release(pp
->dev
);
541 err_unregister_parport
:
542 parport_unregister_device(pp
->dev
);
544 spi_master_put(master
); /* will release memory */
549 static void spi_parport_attach(struct parport
*pb
)
551 (void)__spi_parport_attach(pb
);
555 static int spi_parport_remove(struct spi_parport
*pp
)
559 /* stop and unregisters child devices too */
560 err
= spi_bitbang_stop(&pp
->bitbang
);
562 pr_err("%s: bitbang stop error: %d\n", DRVNAME
, err
);
564 parport_release(pp
->dev
);
565 parport_unregister_device(pp
->dev
);
566 /* this frees also the pp struct */
567 spi_master_put(pp
->bitbang
.master
);
571 static void spi_parport_detach(struct parport
*pb
)
573 struct spi_parport
*pp
;
575 list_for_each_entry(pp
, &spi_parport_hosts
, list
) {
576 if (pp
->port
== pb
) {
577 list_del_init(&pp
->list
);
578 (void)spi_parport_remove(pp
);
584 static struct parport_driver spi_parport_driver
= {
586 .attach
= spi_parport_attach
,
587 .detach
= spi_parport_detach
,
591 static int __init
spi_parport_init(void)
593 return parport_register_driver(&spi_parport_driver
);
595 device_initcall(spi_parport_init
);
597 static void __exit
spi_parport_exit(void)
599 parport_unregister_driver(&spi_parport_driver
);
601 module_exit(spi_parport_exit
);
603 MODULE_AUTHOR("Option Wireless");
604 MODULE_DESCRIPTION("SPI master controller driver for Parport Adapter");
605 MODULE_LICENSE("GPL");
607 module_param(irq
, uint
, S_IRUGO
);
608 MODULE_PARM_DESC(irq
, "Force IRQ for parport pin 10");
610 module_param_string(pdrv
, spi_modalias
, sizeof(spi_modalias
), S_IRUGO
);
611 MODULE_PARM_DESC(pdrv
, "array of spi protocol driver names");
613 module_param_array_named(mode
, spi_mode
, uint
, &spi_mode_cnt
, S_IRUGO
);
614 MODULE_PARM_DESC(mode
, "array of spi modes for each driver");
616 module_param_array_named(bits
, spi_bits
, uint
, &spi_bits_cnt
, S_IRUGO
);
617 MODULE_PARM_DESC(bits
, "array of spi bits per word for each driver");
619 MODULE_INFO(Flags
, sCONFIG_SPI_PARPORT_EXCLUSIVE
);
620 MODULE_INFO(Version
, DRIVER_VERSION
);