SPI Parport controller forked from spi_butterfly
[pub/spi-gpio-pp.git] / spi_tty.c
1 /*
2 * spi_butterfly.c - parport-to-butterfly adapter
3 *
4 * Copyright (C) 2005 David Brownell
5 *
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.
10 *
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.
15 *
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.
19 */
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>
25
26 #include <linux/sched.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/spi_bitbang.h>
29
30
31
32 /*
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.
36 *
37 * See Documentation/spi/butterfly for information about how to build
38 * and use this custom parallel port cable.
39 */
40
41 /*
42 * spi_device
43 * todo:
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
48 */
49
50 /* DATA output bits (pins 2..9 == D0..D7) */
51 #define butterfly_nreset (1 << 1) /* pin 3 */
52
53 #define spi_sck_bit (1 << 0) /* pin 2 */
54 #define spi_mosi_bit (1 << 7) /* pin 9 */
55
56 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
57
58 /* STATUS input bits */
59 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
60
61 /* CONTROL output bits */
62 #define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */
63
64
65
66 static inline struct butterfly *spidev_to_pp(struct spi_device *spi)
67 {
68 return spi->controller_data;
69 }
70
71
72 struct butterfly {
73 /* REVISIT ... for now, this must be first */
74 struct spi_bitbang bitbang;
75
76 struct parport *port;
77 struct pardevice *pd;
78
79 u8 lastbyte;
80
81 struct spi_message *msg;
82 struct spi_device *butterfly;
83 struct spi_board_info info[2];
84
85 };
86
87 /*----------------------------------------------------------------------*/
88
89 static inline void
90 setsck(struct spi_device *spi, int is_on)
91 {
92 struct butterfly *pp = spidev_to_pp(spi);
93 u8 bit, byte = pp->lastbyte;
94
95 bit = spi_sck_bit;
96
97 if (is_on)
98 byte |= bit;
99 else
100 byte &= ~bit;
101 parport_write_data(pp->port, byte);
102 pp->lastbyte = byte;
103 }
104
105 static inline void
106 setmosi(struct spi_device *spi, int is_on)
107 {
108 struct butterfly *pp = spidev_to_pp(spi);
109 u8 bit, byte = pp->lastbyte;
110
111 bit = spi_mosi_bit;
112
113 if (is_on)
114 byte |= bit;
115 else
116 byte &= ~bit;
117 parport_write_data(pp->port, byte);
118 pp->lastbyte = byte;
119 }
120
121 static inline int getmiso(struct spi_device *spi)
122 {
123 struct butterfly *pp = spidev_to_pp(spi);
124 int value;
125 u8 bit;
126
127 bit = spi_miso_bit;
128
129 /* only STATUS_BUSY is NOT negated */
130 value = !(parport_read_status(pp->port) & bit);
131 return (bit == PARPORT_STATUS_BUSY) ? value : !value;
132 }
133
134 static void butterfly_chipselect(struct spi_device *spi, int value)
135 {
136 struct butterfly *pp = spidev_to_pp(spi);
137
138 /* set default clock polarity */
139 if (value != BITBANG_CS_INACTIVE)
140 setsck(spi, spi->mode & SPI_CPOL);
141
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"
145 */
146 if (spi_cs_bit == PARPORT_CONTROL_INIT)
147 value = !value;
148
149 parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
150 }
151
152
153 /* we only needed to implement one mode here, and choose SPI_MODE_0 */
154
155 #define spidelay(X) do{}while(0)
156 //#define spidelay ndelay
157
158 #define EXPAND_BITBANG_TXRX
159 #include <linux/spi/spi_bitbang.h>
160
161 static u32
162 butterfly_txrx_word_mode0(struct spi_device *spi,
163 unsigned nsecs,
164 u32 word, u8 bits)
165 {
166 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
167 }
168
169 /*----------------------------------------------------------------------*/
170
171 /* REVISIT remove this ugly global and its "only one" limitation */
172 static struct butterfly *butterfly;
173
174 static void butterfly_attach(struct parport *p)
175 {
176 struct pardevice *pd;
177 int status;
178 struct butterfly *pp;
179 struct spi_master *master;
180 struct device *dev = p->physport->dev;
181
182 if (butterfly || !dev)
183 return;
184
185 /* REVISIT: this just _assumes_ a butterfly is there ... no probe,
186 * and no way to be selective about what it binds to.
187 */
188
189 master = spi_alloc_master(dev, sizeof *pp);
190 if (!master) {
191 status = -ENOMEM;
192 goto done;
193 }
194 pp = spi_master_get_devdata(master);
195
196 /*
197 * SPI and bitbang hookup
198 *
199 * use default setup(), cleanup(), and transfer() methods; and
200 * only bother implementing mode 0. Start it later.
201 */
202 master->bus_num = 42; // hard coded?!?
203 master->num_chipselect = 2; // hard coded?!?
204
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;
208
209 /*
210 * parport hookup
211 */
212 pp->port = p;
213 pd = parport_register_device(p, "spi_butterfly",
214 NULL, NULL, NULL,
215 0 /* FLAGS */, pp);
216 if (!pd) {
217 status = -ENOMEM;
218 goto clean0;
219 }
220 pp->pd = pd;
221
222 status = parport_claim(pd);
223 if (status < 0)
224 goto clean1;
225
226 /*
227 * Butterfly reset, powerup, run firmware
228 */
229 pr_debug("%s: powerup/reset Butterfly\n", p->name);
230
231 /* nCS for dataflash (this bit is inverted on output) */
232 parport_frob_control(pp->port, spi_cs_bit, 0);
233
234 /* stabilize power with chip in reset (nRESET), and
235 * spi_sck_bit clear (CPOL=0)
236 */
237 pp->lastbyte |= vcc_bits;
238 parport_write_data(pp->port, pp->lastbyte);
239 msleep(5);
240
241 /* take it out of reset; assume long reset delay */
242 pp->lastbyte |= butterfly_nreset;
243 parport_write_data(pp->port, pp->lastbyte);
244 msleep(100);
245
246
247 /*
248 * Start SPI ... for now, hide that we're two physical busses.
249 */
250 status = spi_bitbang_start(&pp->bitbang);
251 if (status < 0)
252 goto clean2;
253
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.
257 */
258
259 pp->info[0].max_speed_hz = 15 * 1000 * 1000;
260
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]);
266 if (pp->butterfly)
267 pr_debug("%s: dataflash at %s\n", p->name,
268 pp->butterfly->dev.bus_id);
269
270 // dev_info(_what?_, ...)
271 pr_info("%s: AVR Butterfly\n", p->name);
272
273
274 /* get spi_bitbang_transfer either via global Symbol, or better
275 * ask it from the driver structure
276 */
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;
280 spi_msg = pp->msg;
281 if (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;
286 #if 0
287 spi_msg->list_head ... Addresse der ersten SPI transfer struct
288
289
290 spi_msg->tx_buf = ;
291 spi_msg->len = 1;
292 spi_msg->cs_change = 1;
293 spi_msg->delay_usecs = 0;
294
295 /* fill up message */
296 master->transfer(pp->butterfly, spi_msg);
297 #endif
298 }
299 }
300 butterfly = pp;
301 return;
302
303 clean2:
304 /* turn off VCC */
305 parport_write_data(pp->port, 0);
306
307 parport_release(pp->pd);
308 clean1:
309 parport_unregister_device(pd);
310 clean0:
311 (void) spi_master_put(pp->bitbang.master);
312 done:
313 pr_debug("%s: butterfly probe, fail %d\n", p->name, status);
314 }
315
316 static void butterfly_detach(struct parport *p)
317 {
318 struct butterfly *pp;
319 int status;
320
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?
324 */
325 if (!butterfly || butterfly->port != p)
326 return;
327 pp = butterfly;
328 if (pp->msg) {
329 spi_message_free(pp->msg);
330 pr_info("Dealloc SPI message buffer\n");
331 }
332 butterfly = NULL;
333
334 /* stop() unregisters child devices too */
335 status = spi_bitbang_stop(&pp->bitbang);
336
337 /* turn off VCC */
338 parport_write_data(pp->port, 0);
339 msleep(10);
340
341 parport_release(pp->pd);
342 parport_unregister_device(pp->pd);
343
344 (void) spi_master_put(pp->bitbang.master);
345 }
346
347 static struct parport_driver butterfly_driver = {
348 .name = "spi_butterfly",
349 .attach = butterfly_attach,
350 .detach = butterfly_detach,
351 };
352
353
354 static int __init butterfly_init(void)
355 {
356 return parport_register_driver(&butterfly_driver);
357 }
358 device_initcall(butterfly_init);
359
360 static void __exit butterfly_exit(void)
361 {
362 parport_unregister_driver(&butterfly_driver);
363 }
364 module_exit(butterfly_exit);
365
366 MODULE_DESCRIPTION("Parport Adapter driver for SPI tty Butterfly");
367 MODULE_LICENSE("GPL");