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>
44 * - make driver possible to share the parport with a gpio_parport.c
47 /* DATA output bits (pins 2..9 == D0..D7) */
48 #define MODALIASLEN 10
49 #define DRVNAME "spi_parport"
50 #define DRIVER_VERSION "0.2"
52 #define spi_sck_bit (1 << 0) /* pin 2 */
53 #define spi_mosi_bit (1 << 7) /* pin 9 */
55 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
57 /* STATUS input bits */
58 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
60 /* CONTROL output bits */
61 #define spi_cs_bit (1 << 1) /* pin 3 */
65 * Port Direction Register Name Signal
66 * ----------- --------- -------- ---------------------- ------
67 * nStrobe 1 --> nC0 PARPORT_CONTROL_STROBE
68 * D0 2 --> D0 (1 << 0) SCLK
69 * D1 3 --> D1 (1 << 1) nCS
70 * D2 4 --> D2 (1 << 2) --
71 * D3 5 --> D3 (1 << 3) --
72 * D4 6 --> D4 (1 << 4) --
73 * D5 7 --> D5 (1 << 5) --
74 * D6 8 --> D6 (1 << 6) --
75 * D7 9 --> D7 (1 << 7) MOSI
76 * nAckn 10 <-- S6 PARPORT_STATUS_ACK
77 * Busy 11 <-- nS7 PARPORT_STATUS_BUSY MISO
78 * Paper 12 <-- S5 PARPORT_STATUS_PAPEROUT --
79 * Sel 13 <-- S4 PARPORT_STATUS_SELECT --
80 * nFeed 14 --> nC1 PARPORT_CONTROL_AUTOFD --
81 * nError 15 <-- S3 PARPORT_STATUS_ERROR --
82 * nInit 16 --> C2 PARPORT_CONTROL_INIT --
83 * nSelIn 17 --> nC3 PARPORT_CONTROL_SELECT --
87 //*****************************************************************************
89 //*****************************************************************************
90 /* module parameters */
91 static char modalias
[MODALIASLEN
] = "gtm501l_spi";
92 static unsigned int spi_mode
= SPI_3WIRE
| SPI_MODE_0
;
93 static unsigned int spi_bits
= 16;
95 static inline struct spi_parport
*spidev_to_pp(struct spi_device
*spi
)
97 return spi
->controller_data
;
102 struct pardevice
*dev
; /* Parport device entry */
103 struct parport
*port
; /* */
104 struct spi_bitbang bitbang
;
105 u8 lastbyte
; /* hold copy of parport to be faster */
106 struct spi_message
*msg
;
107 struct spi_device
*spi_parport
;
108 struct spi_board_info info
;
109 struct list_head list
;
110 } spi_parport_struct
;
114 /* REVISIT ... for now, this must be first */
115 struct spi_bitbang bitbang
;
117 struct parport
*port
;
118 struct pardevice
*pdev
;
120 u8 lastbyte
; /* hold copy of parport to be faster */
122 struct spi_message
*msg
;
123 struct spi_device
*spi_parport
;
124 struct spi_board_info info
;
125 struct list_head list
;
128 /*----------------------------------------------------------------------*/
132 setsck(struct spi_device
*spi
, int is_on
)
134 struct spi_parport
*pp
= spidev_to_pp(spi
);
135 u8 bit
, byte
= pp
->lastbyte
;
143 parport_write_data(pp
->port
, byte
);
144 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
148 setmosi(struct spi_device
*spi
, int is_on
)
150 struct spi_parport
*pp
= spidev_to_pp(spi
);
151 u8 bit
, byte
= pp
->lastbyte
;
159 parport_write_data(pp
->port
, byte
);
160 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
163 static inline int getmiso(struct spi_device
*spi
)
165 struct spi_parport
*pp
= spidev_to_pp(spi
);
171 /* only STATUS_BUSY is NOT negated */
172 value
= !(parport_read_status(pp
->port
) & bit
);
173 return (bit
== PARPORT_STATUS_BUSY
) ? value
: !value
;
176 static void spi_parport_chipselect(struct spi_device
*spi
, int value
)
178 struct spi_parport
*pp
= spidev_to_pp(spi
);
181 /* set default clock polarity */
182 if (value
!= BITBANG_CS_INACTIVE
)
183 setsck(spi
, spi
->mode
& SPI_CPOL
);
185 /* here, value == "activate or not";
186 * most PARPORT_CONTROL_* bits are negated, so we must
187 * morph it to value == "bit value to write in control register"
189 if (spi_cs_bit
== PARPORT_CONTROL_INIT
)
192 parport_frob_control(pp
->port
, spi_cs_bit
, value ? spi_cs_bit
: 0);
194 u8 bit
, byte
= pp
->lastbyte
;
202 parport_write_data(pp
->port
, byte
);
203 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
207 // we do not wait, because parport is even slow, but if X > 10000, this would become important
208 //#define spidelay ndelay
209 #define spidelay(X) do{}while(0)
211 #define EXPAND_BITBANG_TXRX
212 #include <linux/spi/spi_bitbang.h>
215 spi_parport_txrx_word_mode0(struct spi_device
*spi
,
219 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
223 spi_parport_txrx_word_mode1(struct spi_device
*spi
,
227 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
231 spi_parport_txrx_word_mode2(struct spi_device
*spi
,
235 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
239 spi_parport_txrx_word_mode3(struct spi_device
*spi
,
243 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
248 /***************************************************************************
249 * Parallel port probing routines *
250 ***************************************************************************/
252 static LIST_HEAD(spi_parport_hosts
);
255 static void spi_parport_attach(struct parport
*p
)
258 struct spi_parport
*pp
;
259 struct spi_master
*master
;
263 *dev
= p
->physport
->dev
;
264 # spi_parport no longer existing, we use now a standard list structure
265 # therefore getting from p->physport->dev makes no sense any longer
266 # end testing as well
267 if (spi_parport
|| !dev
)
270 /* REVISIT: this just _assumes_ a spi_parport is there ... no probe,
271 * and no way to be selective about what it binds to.
274 master
= spi_alloc_master(dev
, sizeof *pp
);
279 pp
= spi_master_get_devdata(master
);
281 master
->bus_num
= -1; /* dynamic alloc of a bus number */
282 master
->num_chipselect
= 1;
285 * SPI and bitbang hookup
287 * use default setup(), cleanup(), and transfer() methods; and
288 * only bother implementing mode 0. Start it later.
290 pp
->bitbang
.master
= spi_master_get(master
);
291 pp
->bitbang
.chipselect
= spi_parport_chipselect
;
292 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = spi_parport_txrx_word_mode0
;
293 pp
->bitbang
.txrx_word
[SPI_MODE_1
] = spi_parport_txrx_word_mode1
;
294 pp
->bitbang
.txrx_word
[SPI_MODE_2
] = spi_parport_txrx_word_mode2
;
295 pp
->bitbang
.txrx_word
[SPI_MODE_3
] = spi_parport_txrx_word_mode3
;
296 pp
->bitbang
.flags
= spi_mode
; //############### get info from board info would be even better
302 //##### because we want to share with gpio_partport !!!!!
303 pp
->pdev
= parport_register_device(p
, DRVNAME
,
305 0 /* FLAGS */, pp
); //PARPORT_FLAG_EXCL, pp);
307 pr_err("%s: unable to register with parport\n", DRVNAME
);
312 //ToDo: Check and share really parport device as it is done e.g. in ppa.c
313 status
= parport_claim(pp
->pdev
);
318 parport_write_data(pp
->port
, pp
->lastbyte
);
321 * Spi_Parport powerup, run firmware
323 pr_debug("%s: powerup Spi_Parport\n", DRVNAME
);
325 //##### nCS setting depends on spi_mode ....
326 /* nCS for dataflash (this bit is inverted on output) */
327 parport_frob_control(pp
->port
, spi_cs_bit
, 0);
329 /* stabilize power with spi_sck_bit clear (CPOL=0)
331 pp
->lastbyte
|= vcc_bits
;
332 parport_write_data(pp
->port
, pp
->lastbyte
);
336 * Start SPI ... for now, hide that we're two physical busses.
338 status
= spi_bitbang_start(&pp
->bitbang
);
340 pr_warning("%s: spi_bitbang_start failed with status %d\n",
346 * The modalias name MUST match the device_driver name
347 * for the bus glue code to match and subsequently bind them.
348 * We are binding to the generic drivers/hwmon/lm70.c device
352 pr_info("%s: Will load protocol driver: '%s'!\n", DRVNAME
, modalias
);
355 /* need to make this parameter loadable */
356 strcpy(pp
->info
.modalias
, modalias
);
357 pp
->info
.max_speed_hz
= 15 * 1000 * 1000;
358 pp
->info
.chip_select
= 0; // 0: .. 1:
359 pp
->info
.mode
= spi_mode
;
361 /* Enable access to our primary data structure via
362 * the board info's (void *)controller_data.
364 pp
->info
.platform_data
= NULL
; // here we should add data structures for subsystem driver ???
365 pp
->info
.controller_data
= pp
; /* save my structure for later use */
366 pp
->spi_parport
= spi_new_device(pp
->bitbang
.master
, &pp
->info
);
368 pr_info("%s: SPI tty at %s\n", DRVNAME
,
369 pp
->spi_parport
->dev
.bus_id
);
371 pr_warning("%s: spi_new_device failed\n", DRVNAME
);
373 goto out_bitbang_stop
;
375 pp
->spi_parport
->bits_per_word
=spi_bits
;
377 /* get spi_bitbang_transfer either via global Symbol, or better
378 * ask it from the driver structure
380 pp
->msg
= spi_message_alloc(1, GFP_KERNEL
);
381 if (master
) { /* the same address is also saved in pp->bitbang.master or pp->spi_parport->master */
382 struct spi_message
*spi_msg
;
385 // spi_message_init(spi_msg);
386 pr_info("Alloc SPI message buffer\n");
387 spi_msg
->spi
= pp
->spi_parport
;
388 spi_msg
->actual_length
= 1;
390 spi_msg
->list_head
... Addresse der ersten SPI transfer
struct
395 spi_msg
->cs_change
= 1;
396 spi_msg
->delay_usecs
= 0;
398 /* fill up message */
399 master
->transfer(pp
->spi_parport
, spi_msg
);
403 list_add_tail(&pp
->list
, &spi_parport_hosts
);
407 spi_bitbang_stop(&pp
->bitbang
);
410 parport_write_data(pp
->port
, 0);
412 parport_release(pp
->pdev
);
414 parport_unregister_device(pp
->pdev
);
416 (void) spi_master_put(pp
->bitbang
.master
);
418 pr_debug("%s: spi_parport probe, fail %d\n", DRVNAME
, status
);
421 static void spi_parport_detach(struct parport
*p
)
423 struct spi_parport
*pp
;
426 list_for_each_entry(pp
, &spi_parport_hosts
, list
) {
427 if (pp
->pdev
->port
== p
) {
428 list_del_init(&pp
->list
);
430 spi_message_free(pp
->msg
);
431 pr_info("Dealloc SPI message buffer\n");
433 /* stop() unregisters child devices too */
434 status
= spi_bitbang_stop(&pp
->bitbang
);
437 parport_write_data(pp
->port
, 0);
439 parport_release(pp
->pdev
);
440 parport_unregister_device(pp
->pdev
);
441 (void) spi_master_put(pp
->bitbang
.master
);
449 static void spi_parport_detach(struct parport
*pb
)
451 spi_parport_struct
*dev
;
452 list_for_each_entry(dev
, &spi_parport_hosts
, list
) {
453 if (dev
->dev
->port
== pb
) {
454 list_del_init(&dev
->list
);
456 spi_remove_host(dev
->host
);
457 spi_host_put(dev
->host
);
459 parport_release(dev
->pdev
);
460 parport_unregister_device(dev
->pdev
);
468 static struct parport_driver spi_parport_driver
= {
470 .attach
= spi_parport_attach
,
471 .detach
= spi_parport_detach
,
475 static int __init
spi_parport_init(void)
477 return parport_register_driver(&spi_parport_driver
);
479 device_initcall(spi_parport_init
);
481 static void __exit
spi_parport_exit(void)
483 parport_unregister_driver(&spi_parport_driver
);
485 module_exit(spi_parport_exit
);
487 MODULE_DESCRIPTION("SPI master controller driver for Parport Adapter");
488 MODULE_LICENSE("GPL");
490 module_param_string(spi_pdrv
, modalias
, sizeof(modalias
), S_IRUGO
);
491 MODULE_PARM_DESC(spi_pdrv
, "spi protocol driver name");
493 module_param(spi_mode
, uint
, S_IRUGO
);
494 MODULE_PARM_DESC(spi_mode
, "spi mode");
496 module_param(spi_bits
, uint
, S_IRUGO
);
497 MODULE_PARM_DESC(spi_bits
, "spi bits per word");
499 MODULE_INFO(Version
, DRIVER_VERSION
);