2 * spi_parport.c - SPI master controller driver based on the parallel port adapter
4 * Copyright (C) 2005 David Brownell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * The spi_parport is generated from the spi_butterfly driver, but with the
22 * idea to isolate here only the SPI master controller part. Note that we
23 * have not implement the full SPI functionality
25 * Some important settings can be given to the driver by module load parameter.
26 * Also the refernced SPI-protocoll driver can be changed by a module load parameter.
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/parport.h>
34 #include <linux/sched.h>
35 #include <linux/spi/spi.h>
36 #include <linux/spi/spi_bitbang.h>
37 //#include <linux/gpio.h>
42 * - implement SPI mode 1,2,3 is yet missing
43 * - prevent ugly handling of global spi_parport structure
44 //drivers/i2c/busses/i2c-parport.c
45 // uses one static pointer of a list:
46 //static struct i2c_par *adapter_list;
48 // uses one static pointer of a list:
49 // static LIST_HEAD(ppa_hosts);
50 //the spi_parport structure has to be pushed into the heap and the pointer has to be saved into the SPI core structre ...
51 * - make driver possible to share the parport with a gpio_parport.c
54 /* DATA output bits (pins 2..9 == D0..D7) */
55 #define MODALIASLEN 10
56 #define DRVNAME "spi_parport"
57 #define DRIVER_VERSION "0.2"
59 #define spi_sck_bit (1 << 0) /* pin 2 */
60 #define spi_mosi_bit (1 << 7) /* pin 9 */
62 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
64 /* STATUS input bits */
65 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
67 /* CONTROL output bits */
68 #define spi_cs_bit (1 << 1) /* pin 3 */
72 * Port Direction Register Name Signal
73 * ----------- --------- -------- ---------------------- ------
74 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE
75 * D0 2 --> D0 (1 << 0) SCLK
76 * D1 3 --> D1 (1 << 1) nCS
77 * D2 4 --> D2 (1 << 2) --
78 * D3 5 --> D3 (1 << 3) --
79 * D4 6 --> D4 (1 << 4) --
80 * D5 7 --> D5 (1 << 5) --
81 * D6 8 --> D6 (1 << 6) --
82 * D7 9 --> D7 (1 << 7) MOSI
83 * nAckn 10 <-- S6 PARPORT_STATUS_ACK
84 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY MISO
85 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT --
86 * Sel 13 <-- S4 PARPORT_STATUS_SELECT --
87 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD --
88 * nError 15 <-- S3 PARPORT_STATUS_ERROR --
89 * nInit 16 --> C2 PARPORT_CONTROL_INIT --
90 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT --
94 //*****************************************************************************
96 //*****************************************************************************
97 /* module parameters */
98 static char modalias
[MODALIASLEN
] = "gtm501l_spi";
99 static unsigned int spi_mode
= SPI_3WIRE
| SPI_MODE_0
;
100 static unsigned int spi_bits
= 16;
102 static inline struct spi_parport
*spidev_to_pp(struct spi_device
*spi
)
104 return spi
->controller_data
;
109 struct pardevice
*dev
; /* Parport device entry */
110 struct parport
*port
; /* */
111 struct spi_bitbang bitbang
;
112 u8 lastbyte
; /* hold copy of parport to be faster */
113 struct spi_message
*msg
;
114 struct spi_device
*spi_parport
;
115 struct spi_board_info info
;
116 struct list_head list
;
117 } spi_parport_struct
;
121 /* REVISIT ... for now, this must be first */
122 struct spi_bitbang bitbang
;
124 struct parport
*port
;
125 struct pardevice
*pdev
;
127 u8 lastbyte
; /* hold copy of parport to be faster */
129 struct spi_message
*msg
;
130 struct spi_device
*spi_parport
;
131 struct spi_board_info info
;
135 static LIST_HEAD(spi_parport
);
137 /* REVISIT remove this ugly global and its "only one" limitation */
138 static struct spi_parport
*spi_parport
;
140 /*----------------------------------------------------------------------*/
144 setsck(struct spi_device
*spi
, int is_on
)
146 struct spi_parport
*pp
= spidev_to_pp(spi
);
147 u8 bit
, byte
= pp
->lastbyte
;
155 parport_write_data(pp
->port
, byte
);
156 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
160 setmosi(struct spi_device
*spi
, int is_on
)
162 struct spi_parport
*pp
= spidev_to_pp(spi
);
163 u8 bit
, byte
= pp
->lastbyte
;
171 parport_write_data(pp
->port
, byte
);
172 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
175 static inline int getmiso(struct spi_device
*spi
)
177 struct spi_parport
*pp
= spidev_to_pp(spi
);
183 /* only STATUS_BUSY is NOT negated */
184 value
= !(parport_read_status(pp
->port
) & bit
);
185 return (bit
== PARPORT_STATUS_BUSY
) ? value
: !value
;
188 static void spi_parport_chipselect(struct spi_device
*spi
, int value
)
190 struct spi_parport
*pp
= spidev_to_pp(spi
);
193 /* set default clock polarity */
194 if (value
!= BITBANG_CS_INACTIVE
)
195 setsck(spi
, spi
->mode
& SPI_CPOL
);
197 /* here, value == "activate or not";
198 * most PARPORT_CONTROL_* bits are negated, so we must
199 * morph it to value == "bit value to write in control register"
201 if (spi_cs_bit
== PARPORT_CONTROL_INIT
)
204 parport_frob_control(pp
->port
, spi_cs_bit
, value ? spi_cs_bit
: 0);
206 u8 bit
, byte
= pp
->lastbyte
;
214 parport_write_data(pp
->port
, byte
);
215 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
219 // we do not wait, because parport is even slow, but if X > 10000, this would become important
220 //#define spidelay ndelay
221 #define spidelay(X) do{}while(0)
223 #define EXPAND_BITBANG_TXRX
224 #include <linux/spi/spi_bitbang.h>
227 spi_parport_txrx_word_mode0(struct spi_device
*spi
,
231 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
235 spi_parport_txrx_word_mode1(struct spi_device
*spi
,
239 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
243 spi_parport_txrx_word_mode2(struct spi_device
*spi
,
247 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
251 spi_parport_txrx_word_mode3(struct spi_device
*spi
,
255 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
260 /*----------------------------------------------------------------------*/
262 static void spi_parport_attach(struct parport
*p
)
265 struct spi_parport
*pp
;
266 struct spi_master
*master
;
267 struct device
*dev
= p
->physport
->dev
;
269 if (spi_parport
|| !dev
)
272 /* REVISIT: this just _assumes_ a spi_parport is there ... no probe,
273 * and no way to be selective about what it binds to.
276 master
= spi_alloc_master(dev
, sizeof *pp
);
281 pp
= spi_master_get_devdata(master
);
283 master
->bus_num
= -1; /* dynamic alloc of a bus number */
284 master
->num_chipselect
= 1;
287 * SPI and bitbang hookup
289 * use default setup(), cleanup(), and transfer() methods; and
290 * only bother implementing mode 0. Start it later.
292 pp
->bitbang
.master
= spi_master_get(master
);
293 pp
->bitbang
.chipselect
= spi_parport_chipselect
;
294 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = spi_parport_txrx_word_mode0
;
295 pp
->bitbang
.txrx_word
[SPI_MODE_1
] = spi_parport_txrx_word_mode1
;
296 pp
->bitbang
.txrx_word
[SPI_MODE_2
] = spi_parport_txrx_word_mode2
;
297 pp
->bitbang
.txrx_word
[SPI_MODE_3
] = spi_parport_txrx_word_mode3
;
298 pp
->bitbang
.flags
= spi_mode
; //############### get info from board info would be even better
304 //##### because we want to share with gpio_partport !!!!!
305 pp
->pdev
= parport_register_device(p
, DRVNAME
,
307 0 /* FLAGS */, pp
); //PARPORT_FLAG_EXCL, pp);
309 pr_err("%s: unable to register with parport\n", DRVNAME
);
314 status
= parport_claim(pp
->pdev
);
319 parport_write_data(pp
->port
, pp
->lastbyte
);
322 * Spi_Parport powerup, run firmware
324 pr_debug("%s: powerup Spi_Parport\n", DRVNAME
);
326 //##### nCS setting depends on spi_mode ....
327 /* nCS for dataflash (this bit is inverted on output) */
328 parport_frob_control(pp
->port
, spi_cs_bit
, 0);
330 /* stabilize power with spi_sck_bit clear (CPOL=0)
332 pp
->lastbyte
|= vcc_bits
;
333 parport_write_data(pp
->port
, pp
->lastbyte
);
337 * Start SPI ... for now, hide that we're two physical busses.
339 status
= spi_bitbang_start(&pp
->bitbang
);
341 pr_warning("%s: spi_bitbang_start failed with status %d\n",
347 * The modalias name MUST match the device_driver name
348 * for the bus glue code to match and subsequently bind them.
349 * We are binding to the generic drivers/hwmon/lm70.c device
353 pr_info("%s: Will load protocol driver: '%s'!\n", DRVNAME
, modalias
);
356 /* need to make this parameter loadable */
357 strcpy(pp
->info
.modalias
, modalias
);
358 pp
->info
.max_speed_hz
= 15 * 1000 * 1000;
359 pp
->info
.chip_select
= 0; // 0: .. 1:
360 pp
->info
.mode
= spi_mode
;
362 /* Enable access to our primary data structure via
363 * the board info's (void *)controller_data.
365 pp
->info
.platform_data
= NULL
; // here we should add data structures for subsystem driver ???
366 pp
->info
.controller_data
= pp
; /* save my structure for later use */
367 pp
->spi_parport
= spi_new_device(pp
->bitbang
.master
, &pp
->info
);
369 pr_info("%s: SPI tty at %s\n", DRVNAME
,
370 pp
->spi_parport
->dev
.bus_id
);
372 pr_warning("%s: spi_new_device failed\n", DRVNAME
);
374 goto out_bitbang_stop
;
376 pp
->spi_parport
->bits_per_word
=spi_bits
;
378 /* get spi_bitbang_transfer either via global Symbol, or better
379 * ask it from the driver structure
381 pp
->msg
= spi_message_alloc(1, GFP_KERNEL
);
382 if (master
) { /* the same address is also saved in pp->bitbang.master or pp->spi_parport->master */
383 struct spi_message
*spi_msg
;
386 // spi_message_init(spi_msg);
387 pr_info("Alloc SPI message buffer\n");
388 spi_msg
->spi
= pp
->spi_parport
;
389 spi_msg
->actual_length
= 1;
391 spi_msg
->list_head
... Addresse der ersten SPI transfer
struct
396 spi_msg
->cs_change
= 1;
397 spi_msg
->delay_usecs
= 0;
399 /* fill up message */
400 master
->transfer(pp
->spi_parport
, spi_msg
);
408 spi_bitbang_stop(&pp
->bitbang
);
411 parport_write_data(pp
->port
, 0);
413 parport_release(pp
->pdev
);
415 parport_unregister_device(pp
->pdev
);
417 (void) spi_master_put(pp
->bitbang
.master
);
419 pr_debug("%s: spi_parport probe, fail %d\n", DRVNAME
, status
);
422 static void spi_parport_detach(struct parport
*p
)
424 struct spi_parport
*pp
;
427 /* FIXME this global is ugly ... but, how to quickly get from
428 * the parport to the "struct spi_parport" associated with it?
429 * "old school" driver-internal device lists?
431 if (!spi_parport
|| spi_parport
->port
!= p
)
435 spi_message_free(pp
->msg
);
436 pr_info("Dealloc SPI message buffer\n");
439 /* stop() unregisters child devices too */
440 status
= spi_bitbang_stop(&pp
->bitbang
);
443 parport_write_data(pp
->port
, 0);
446 parport_release(pp
->pdev
);
447 parport_unregister_device(pp
->pdev
);
449 (void) spi_master_put(pp
->bitbang
.master
);
454 static void spi_parport_detach(struct parport
*pb
)
457 list_for_each_entry(dev
, &ppa_hosts
, list
) {
458 if (dev
->dev
->port
== pb
) {
459 list_del_init(&dev
->list
);
460 scsi_remove_host(dev
->host
);
461 scsi_host_put(dev
->host
);
462 parport_unregister_device(dev
->dev
);
470 static struct parport_driver spi_parport_driver
= {
472 .attach
= spi_parport_attach
,
473 .detach
= spi_parport_detach
,
477 static int __init
spi_parport_init(void)
479 return parport_register_driver(&spi_parport_driver
);
481 device_initcall(spi_parport_init
);
483 static void __exit
spi_parport_exit(void)
485 parport_unregister_driver(&spi_parport_driver
);
487 module_exit(spi_parport_exit
);
489 MODULE_DESCRIPTION("SPI master controller driver for Parport Adapter");
490 MODULE_LICENSE("GPL");
492 module_param_string(spi_pdrv
, modalias
, sizeof(modalias
), S_IRUGO
);
493 MODULE_PARM_DESC(spi_pdrv
, "spi protocol driver name");
495 module_param(spi_mode
, uint
, S_IRUGO
);
496 MODULE_PARM_DESC(spi_mode
, "spi mode");
498 module_param(spi_bits
, uint
, S_IRUGO
);
499 MODULE_PARM_DESC(spi_mode
, "spi bits per word");
501 MODULE_INFO(Version
, DRIVER_VERSION
);