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.
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/parport.h>
26 #include <linux/sched.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/spi_bitbang.h>
33 * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card
34 * with a battery powered AVR microcontroller and lots of goodies. You
35 * can use GCC to develop firmware for this.
37 * See Documentation/spi/butterfly for information about how to build
38 * and use this custom parallel port cable.
44 * - implement SPI mode 1,2,3 is yet missing
45 * - prevent ugly handling of global butterfly structure
46 * - use serial subsystem
47 * - make stuff module configurable during runtime
50 /* DATA output bits (pins 2..9 == D0..D7) */
51 #define butterfly_nreset (1 << 1) /* pin 3 */
53 #define spi_sck_bit (1 << 0) /* pin 2 */
54 #define spi_mosi_bit (1 << 7) /* pin 9 */
56 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
58 /* STATUS input bits */
59 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
61 /* CONTROL output bits */
62 #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
66 static inline struct butterfly
*spidev_to_pp(struct spi_device
*spi
)
68 return spi
->controller_data
;
73 /* REVISIT ... for now, this must be first */
74 struct spi_bitbang bitbang
;
81 struct spi_message
*msg
;
82 struct spi_device
*butterfly
;
83 struct spi_board_info info
[2];
87 /*----------------------------------------------------------------------*/
90 setsck(struct spi_device
*spi
, int is_on
)
92 struct butterfly
*pp
= spidev_to_pp(spi
);
93 u8 bit
, byte
= pp
->lastbyte
;
101 parport_write_data(pp
->port
, byte
);
106 setmosi(struct spi_device
*spi
, int is_on
)
108 struct butterfly
*pp
= spidev_to_pp(spi
);
109 u8 bit
, byte
= pp
->lastbyte
;
117 parport_write_data(pp
->port
, byte
);
121 static inline int getmiso(struct spi_device
*spi
)
123 struct butterfly
*pp
= spidev_to_pp(spi
);
129 /* only STATUS_BUSY is NOT negated */
130 value
= !(parport_read_status(pp
->port
) & bit
);
131 return (bit
== PARPORT_STATUS_BUSY
) ? value
: !value
;
134 static void butterfly_chipselect(struct spi_device
*spi
, int value
)
136 struct butterfly
*pp
= spidev_to_pp(spi
);
138 /* set default clock polarity */
139 if (value
!= BITBANG_CS_INACTIVE
)
140 setsck(spi
, spi
->mode
& SPI_CPOL
);
142 /* here, value == "activate or not";
143 * most PARPORT_CONTROL_* bits are negated, so we must
144 * morph it to value == "bit value to write in control register"
146 if (spi_cs_bit
== PARPORT_CONTROL_INIT
)
149 parport_frob_control(pp
->port
, spi_cs_bit
, value ? spi_cs_bit
: 0);
153 /* we only needed to implement one mode here, and choose SPI_MODE_0 */
155 #define spidelay(X) do{}while(0)
156 //#define spidelay ndelay
158 #define EXPAND_BITBANG_TXRX
159 #include <linux/spi/spi_bitbang.h>
162 butterfly_txrx_word_mode0(struct spi_device
*spi
,
166 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
169 /*----------------------------------------------------------------------*/
171 /* REVISIT remove this ugly global and its "only one" limitation */
172 static struct butterfly
*butterfly
;
174 static void butterfly_attach(struct parport
*p
)
176 struct pardevice
*pd
;
178 struct butterfly
*pp
;
179 struct spi_master
*master
;
180 struct device
*dev
= p
->physport
->dev
;
182 if (butterfly
|| !dev
)
185 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
186 * and no way to be selective about what it binds to.
189 master
= spi_alloc_master(dev
, sizeof *pp
);
194 pp
= spi_master_get_devdata(master
);
197 * SPI and bitbang hookup
199 * use default setup(), cleanup(), and transfer() methods; and
200 * only bother implementing mode 0. Start it later.
202 master
->bus_num
= 42; // hard coded?!?
203 master
->num_chipselect
= 2; // hard coded?!?
205 pp
->bitbang
.master
= spi_master_get(master
);
206 pp
->bitbang
.chipselect
= butterfly_chipselect
;
207 pp
->bitbang
.txrx_word
[SPI_MODE_0
] = butterfly_txrx_word_mode0
;
213 pd
= parport_register_device(p
, "spi_butterfly",
222 status
= parport_claim(pd
);
227 * Butterfly reset, powerup, run firmware
229 pr_debug("%s: powerup/reset Butterfly\n", p
->name
);
231 /* nCS for dataflash (this bit is inverted on output) */
232 parport_frob_control(pp
->port
, spi_cs_bit
, 0);
234 /* stabilize power with chip in reset (nRESET), and
235 * spi_sck_bit clear (CPOL=0)
237 pp
->lastbyte
|= vcc_bits
;
238 parport_write_data(pp
->port
, pp
->lastbyte
);
241 /* take it out of reset; assume long reset delay */
242 pp
->lastbyte
|= butterfly_nreset
;
243 parport_write_data(pp
->port
, pp
->lastbyte
);
248 * Start SPI ... for now, hide that we're two physical busses.
250 status
= spi_bitbang_start(&pp
->bitbang
);
254 /* Bus 1 lets us talk to at45db041b (firmware disables AVR SPI), AVR
255 * (firmware resets at45, acts as spi slave) or neither (we ignore
256 * both, AVR uses AT45). Here we expect firmware for the first option.
259 pp
->info
[0].max_speed_hz
= 15 * 1000 * 1000;
261 strcpy(pp
->info
[0].modalias
, "spi_tty"); // name length check ! < KOBJ_NAME_LEN
262 pp
->info
[0].platform_data
= NULL
; // here we should add data structures for subsystem driver ???
263 pp
->info
[0].chip_select
= 1; // 0: .. 1:
264 pp
->info
[0].controller_data
= pp
; /* save my structure for later use */
265 pp
->butterfly
= spi_new_device(pp
->bitbang
.master
, &pp
->info
[0]);
267 pr_debug("%s: dataflash at %s\n", p
->name
,
268 pp
->butterfly
->dev
.bus_id
);
270 // dev_info(_what?_, ...)
271 pr_info("%s: AVR Butterfly\n", p
->name
);
274 /* get spi_bitbang_transfer either via global Symbol, or better
275 * ask it from the driver structure
277 pp
->msg
= spi_message_alloc(1, GFP_KERNEL
);
278 if (master
) { /* the same address is also saved in pp->bitbang.master or pp->butterfly->master */
279 struct spi_message
*spi_msg
;
282 // spi_message_init(spi_msg);
283 pr_info("Alloc SPI message buffer\n");
284 spi_msg
->spi
= pp
->butterfly
;
285 spi_msg
->actual_length
= 1;
287 spi_msg
->list_head
... Addresse der ersten SPI transfer
struct
292 spi_msg
->cs_change
= 1;
293 spi_msg
->delay_usecs
= 0;
295 /* fill up message */
296 master
->transfer(pp
->butterfly
, spi_msg
);
305 parport_write_data(pp
->port
, 0);
307 parport_release(pp
->pd
);
309 parport_unregister_device(pd
);
311 (void) spi_master_put(pp
->bitbang
.master
);
313 pr_debug("%s: butterfly probe, fail %d\n", p
->name
, status
);
316 static void butterfly_detach(struct parport
*p
)
318 struct butterfly
*pp
;
321 /* FIXME this global is ugly ... but, how to quickly get from
322 * the parport to the "struct butterfly" associated with it?
323 * "old school" driver-internal device lists?
325 if (!butterfly
|| butterfly
->port
!= p
)
329 spi_message_free(pp
->msg
);
330 pr_info("Dealloc SPI message buffer\n");
334 /* stop() unregisters child devices too */
335 status
= spi_bitbang_stop(&pp
->bitbang
);
338 parport_write_data(pp
->port
, 0);
341 parport_release(pp
->pd
);
342 parport_unregister_device(pp
->pd
);
344 (void) spi_master_put(pp
->bitbang
.master
);
347 static struct parport_driver butterfly_driver
= {
348 .name
= "spi_butterfly",
349 .attach
= butterfly_attach
,
350 .detach
= butterfly_detach
,
354 static int __init
butterfly_init(void)
356 return parport_register_driver(&butterfly_driver
);
358 device_initcall(butterfly_init
);
360 static void __exit
butterfly_exit(void)
362 parport_unregister_driver(&butterfly_driver
);
364 module_exit(butterfly_exit
);
366 MODULE_DESCRIPTION("Parport Adapter driver for SPI tty Butterfly");
367 MODULE_LICENSE("GPL");