2 * spi_butterfly.c - parport-to-butterfly adapter
4 * Copyright (C) 2005 David Brownell
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.
21 * separate the butterfly driver into a hotpluggable SPI master controller
22 * use a board description file, which loads the:
23 * - the SPI master controller
24 * - the SPI protocoll driver
25 * call spi_new_device with
26 - spi_master = butterfly parport driver (or parameter configurable)
27 - spi_board_info = infos about Mauro-Lite
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/device.h>
34 #include <linux/parport.h>
36 #include <linux/sched.h>
37 #include <linux/spi/spi.h>
38 #include <linux/spi/spi_bitbang.h>
43 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
44 * with a battery powered AVR microcontroller and lots of goodies. You
45 * can use GCC to develop firmware for this.
47 * See Documentation/spi/butterfly for information about how to build
48 * and use this custom parallel port cable.
54 * - implement SPI mode 1,2,3 is yet missing
55 * - prevent ugly handling of global butterfly structure
56 * - use serial subsystem
57 * - make stuff module configurable during runtime
60 /* DATA output bits (pins 2..9 == D0..D7) */
61 #define MODALIASLEN 10
62 #define DRVNAME "spi_butterfly"
63 #define DRIVER_VERSION "0.2"
65 #define butterfly_nreset (1 << 1) /* pin 3 */
67 #define spi_sck_bit (1 << 0) /* pin 2 */
68 #define spi_mosi_bit (1 << 7) /* pin 9 */
70 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
72 /* STATUS input bits */
73 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
75 /* CONTROL output bits */
76 #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
80 * Port Direction Signal
81 * ----------- --------- ------------
100 //*****************************************************************************
102 //*****************************************************************************
103 /* module parameters */
104 static char modalias
[MODALIASLEN
] = "maurol";
106 static inline struct butterfly
*spidev_to_pp(struct spi_device
*spi
)
108 return spi
->controller_data
;
113 /* REVISIT ... for now, this must be first */
114 struct spi_bitbang bitbang
;
116 struct parport
*port
;
117 struct pardevice
*pd
;
119 u8 lastbyte
; /* hold copy of partport to be faster */
121 struct spi_message
*msg
;
122 struct spi_device
*butterfly
;
123 struct spi_board_info info
;
127 /*----------------------------------------------------------------------*/
131 setsck(struct spi_device
*spi
, int is_on
)
133 struct butterfly
*pp
= spidev_to_pp(spi
);
134 u8 bit
, byte
= pp
->lastbyte
;
142 parport_write_data(pp
->port
, byte
);
143 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
147 setmosi(struct spi_device
*spi
, int is_on
)
149 struct butterfly
*pp
= spidev_to_pp(spi
);
150 u8 bit
, byte
= pp
->lastbyte
;
158 parport_write_data(pp
->port
, byte
);
159 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
162 static inline int getmiso(struct spi_device
*spi
)
164 struct butterfly
*pp
= spidev_to_pp(spi
);
170 /* only STATUS_BUSY is NOT negated */
171 value
= !(parport_read_status(pp
->port
) & bit
);
172 return (bit
== PARPORT_STATUS_BUSY
) ? value
: !value
;
175 static void butterfly_chipselect(struct spi_device
*spi
, int value
)
177 struct butterfly
*pp
= spidev_to_pp(spi
);
180 /* set default clock polarity */
181 if (value
!= BITBANG_CS_INACTIVE
)
182 setsck(spi
, spi
->mode
& SPI_CPOL
);
184 /* here, value == "activate or not";
185 * most PARPORT_CONTROL_* bits are negated, so we must
186 * morph it to value == "bit value to write in control register"
188 if (spi_cs_bit
== PARPORT_CONTROL_INIT
)
191 parport_frob_control(pp
->port
, spi_cs_bit
, value ? spi_cs_bit
: 0);
193 u8 bit
, byte
= pp
->lastbyte
;
195 bit
= butterfly_nreset
;
201 parport_write_data(pp
->port
, byte
);
202 pp
->lastbyte
= byte
; /* use parport mirror to be faster */
207 /* we only needed to implement one mode here, and choose SPI_MODE_0 */
209 #define spidelay(X) do{}while(0)
210 //#define spidelay ndelay
212 #define EXPAND_BITBANG_TXRX
213 #include <linux/spi/spi_bitbang.h>
216 butterfly_txrx_word_mode0(struct spi_device
*spi
,
220 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
223 /*----------------------------------------------------------------------*/
225 /* REVISIT remove this ugly global and its "only one" limitation */
226 static struct butterfly
*butterfly
;
228 static void butterfly_attach(struct parport
*p
)
230 struct pardevice
*pd
;
232 struct butterfly
*pp
;
233 struct spi_master
*master
;
234 struct device
*dev
= p
->physport
->dev
;
236 if (butterfly
|| !dev
)
239 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
240 * and no way to be selective about what it binds to.
243 master
= spi_alloc_master(dev
, sizeof *pp
);
248 pp
= spi_master_get_devdata(master
);
250 master
->bus_num
= -1; /* dynamic alloc of a bus number */
251 master
->num_chipselect
= 1;
254 * SPI and bitbang hookup
256 * use default setup(), cleanup(), and transfer() methods; and
257 * only bother implementing mode 0. Start it later.
259 pp
->bitbang
.master
= spi_master_get(master
);
260 pp
->bitbang
.chipselect
= butterfly_chipselect
;
261 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = butterfly_txrx_word_mode0
;
262 pp
->bitbang
.flags
= SPI_3WIRE
; // ####################################
268 pd
= parport_register_device(p
, DRVNAME
,
270 0 /* FLAGS */, pp
); //PARPORT_FLAG_EXCL, pp);
277 status
= parport_claim(pd
);
282 parport_write_data(pp
->port
, pp
->lastbyte
);
285 * Butterfly reset, powerup, run firmware
287 pr_debug("%s: powerup/reset Butterfly\n", DRVNAME
);
289 /* nCS for dataflash (this bit is inverted on output) */
290 parport_frob_control(pp
->port
, spi_cs_bit
, 0);
292 /* stabilize power with chip in reset (nRESET), and
293 * spi_sck_bit clear (CPOL=0)
295 pp
->lastbyte
|= vcc_bits
;
296 parport_write_data(pp
->port
, pp
->lastbyte
);
300 /* take it out of reset; assume long reset delay */
301 pp
->lastbyte
|= butterfly_nreset
;
302 parport_write_data(pp
->port
, pp
->lastbyte
);
305 /* take reset as trigger signal ############# */
306 pp
->lastbyte
&= ~butterfly_nreset
;
307 parport_write_data(pp
->port
, pp
->lastbyte
);
311 * Start SPI ... for now, hide that we're two physical busses.
313 status
= spi_bitbang_start(&pp
->bitbang
);
315 pr_warning("%s: spi_bitbang_start failed with status %d\n",
321 * The modalias name MUST match the device_driver name
322 * for the bus glue code to match and subsequently bind them.
323 * We are binding to the generic drivers/hwmon/lm70.c device
327 pr_info("%s: Will load protocol driver: '%s'!\n", DRVNAME
, modalias
);
330 /* need to make this parameter loadable */
331 strcpy(pp
->info
.modalias
, modalias
);
332 pp
->info
.max_speed_hz
= 15 * 1000 * 1000;
333 pp
->info
.chip_select
= 0; // 0: .. 1:
334 pp
->info
.mode
= SPI_3WIRE
| SPI_MODE_0
; // ################
336 /* Enable access to our primary data structure via
337 * the board info's (void *)controller_data.
339 pp
->info
.platform_data
= NULL
; // here we should add data structures for subsystem driver ???
340 pp
->info
.controller_data
= pp
; /* save my structure for later use */
341 pp
->butterfly
= spi_new_device(pp
->bitbang
.master
, &pp
->info
);
343 pr_info("%s: SPI tty at %s\n", DRVNAME
,
344 pp
->butterfly
->dev
.bus_id
);
346 pr_warning("%s: spi_new_device failed\n", DRVNAME
);
348 goto out_bitbang_stop
;
350 // pp->butterfly->bits_per_word=16; // ###############
352 /* get spi_bitbang_transfer either via global Symbol, or better
353 * ask it from the driver structure
355 pp
->msg
= spi_message_alloc(1, GFP_KERNEL
);
356 if (master
) { /* the same address is also saved in pp->bitbang.master or pp->butterfly->master */
357 struct spi_message
*spi_msg
;
360 // spi_message_init(spi_msg);
361 pr_info("Alloc SPI message buffer\n");
362 spi_msg
->spi
= pp
->butterfly
;
363 spi_msg
->actual_length
= 1;
365 spi_msg
->list_head
... Addresse der ersten SPI transfer
struct
370 spi_msg
->cs_change
= 1;
371 spi_msg
->delay_usecs
= 0;
373 /* fill up message */
374 master
->transfer(pp
->butterfly
, spi_msg
);
382 spi_bitbang_stop(&pp
->bitbang
);
385 parport_write_data(pp
->port
, 0);
387 parport_release(pp
->pd
);
389 parport_unregister_device(pd
);
391 (void) spi_master_put(pp
->bitbang
.master
);
393 pr_debug("%s: butterfly probe, fail %d\n", DRVNAME
, status
);
396 static void butterfly_detach(struct parport
*p
)
398 struct butterfly
*pp
;
401 /* FIXME this global is ugly ... but, how to quickly get from
402 * the parport to the "struct butterfly" associated with it?
403 * "old school" driver-internal device lists?
405 if (!butterfly
|| butterfly
->port
!= p
)
409 spi_message_free(pp
->msg
);
410 pr_info("Dealloc SPI message buffer\n");
413 /* stop() unregisters child devices too */
414 status
= spi_bitbang_stop(&pp
->bitbang
);
417 parport_write_data(pp
->port
, 0);
420 parport_release(pp
->pd
);
421 parport_unregister_device(pp
->pd
);
423 (void) spi_master_put(pp
->bitbang
.master
);
428 static struct parport_driver butterfly_driver
= {
430 .attach
= butterfly_attach
,
431 .detach
= butterfly_detach
,
435 static int __init
butterfly_init(void)
437 return parport_register_driver(&butterfly_driver
);
439 device_initcall(butterfly_init
);
441 static void __exit
butterfly_exit(void)
443 parport_unregister_driver(&butterfly_driver
);
445 module_exit(butterfly_exit
);
447 MODULE_DESCRIPTION("Parport Adapter driver for SPI tty Butterfly");
448 MODULE_LICENSE("GPL");
450 module_param_string(spi_pdrv
, modalias
, sizeof(modalias
), S_IRUGO
);
451 MODULE_PARM_DESC(spi_pdrv
, "spi protocol driver name");
453 MODULE_INFO(Version
, DRIVER_VERSION
);