spi_parport
[pub/spi-gpio-pp.git] / spi_parport.c
1 /*
2 * spi_parport.c - SPI master controller driver based on the parallel port adapter
3 *
4 * Copyright (C) 2005 David Brownell
5 * 2008 Peter Henn
6 *
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.
11 *
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.
16 *
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.
20 *
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
24 *
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.
27 */
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>
33
34 #include <linux/sched.h>
35 #include <linux/spi/spi.h>
36 #include <linux/spi/spi_bitbang.h>
37 //#include <linux/gpio.h>
38
39 /*
40 * spi_device
41 * todo:
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;
47 //drivers/scsi/ppa.c
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
52 */
53
54 /* DATA output bits (pins 2..9 == D0..D7) */
55 #define MODALIASLEN 10
56 #define DRVNAME "spi_parport"
57 #define DRIVER_VERSION "0.2"
58
59 #define spi_sck_bit (1 << 0) /* pin 2 */
60 #define spi_mosi_bit (1 << 7) /* pin 9 */
61
62 #define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */
63
64 /* STATUS input bits */
65 #define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */
66
67 /* CONTROL output bits */
68 #define spi_cs_bit (1 << 1) /* pin 3 */
69
70 /*
71 * Parallel
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 --
91 * GND 25 -- -- GND
92 */
93
94 //*****************************************************************************
95 // Globals
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;
101
102 static inline struct spi_parport *spidev_to_pp(struct spi_device *spi)
103 {
104 return spi->controller_data;
105 }
106
107
108 typedef struct {
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;
118
119
120 struct spi_parport {
121 /* REVISIT ... for now, this must be first */
122 struct spi_bitbang bitbang;
123
124 struct parport *port;
125 struct pardevice *pdev;
126
127 u8 lastbyte; /* hold copy of parport to be faster */
128
129 struct spi_message *msg;
130 struct spi_device *spi_parport;
131 struct spi_board_info info;
132
133 };
134
135 static LIST_HEAD(spi_parport);
136
137 /* REVISIT remove this ugly global and its "only one" limitation */
138 static struct spi_parport *spi_parport;
139
140 /*----------------------------------------------------------------------*/
141
142
143 static inline void
144 setsck(struct spi_device *spi, int is_on)
145 {
146 struct spi_parport *pp = spidev_to_pp(spi);
147 u8 bit, byte = pp->lastbyte;
148
149 bit = spi_sck_bit;
150
151 if (is_on)
152 byte |= bit;
153 else
154 byte &= ~bit;
155 parport_write_data(pp->port, byte);
156 pp->lastbyte = byte; /* use parport mirror to be faster */
157 }
158
159 static inline void
160 setmosi(struct spi_device *spi, int is_on)
161 {
162 struct spi_parport *pp = spidev_to_pp(spi);
163 u8 bit, byte = pp->lastbyte;
164
165 bit = spi_mosi_bit;
166
167 if (is_on)
168 byte |= bit;
169 else
170 byte &= ~bit;
171 parport_write_data(pp->port, byte);
172 pp->lastbyte = byte; /* use parport mirror to be faster */
173 }
174
175 static inline int getmiso(struct spi_device *spi)
176 {
177 struct spi_parport *pp = spidev_to_pp(spi);
178 int value;
179 u8 bit;
180
181 bit = spi_miso_bit;
182
183 /* only STATUS_BUSY is NOT negated */
184 value = !(parport_read_status(pp->port) & bit);
185 return (bit == PARPORT_STATUS_BUSY) ? value : !value;
186 }
187
188 static void spi_parport_chipselect(struct spi_device *spi, int value)
189 {
190 struct spi_parport *pp = spidev_to_pp(spi);
191
192 #if 0
193 /* set default clock polarity */
194 if (value != BITBANG_CS_INACTIVE)
195 setsck(spi, spi->mode & SPI_CPOL);
196
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"
200 */
201 if (spi_cs_bit == PARPORT_CONTROL_INIT)
202 value = !value;
203
204 parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0);
205 #else
206 u8 bit, byte = pp->lastbyte;
207
208 bit = spi_cs_bit;
209
210 if (value)
211 byte &= ~bit;
212 else
213 byte |= bit;
214 parport_write_data(pp->port, byte);
215 pp->lastbyte = byte; /* use parport mirror to be faster */
216 #endif
217 }
218
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)
222
223 #define EXPAND_BITBANG_TXRX
224 #include <linux/spi/spi_bitbang.h>
225
226 static u32
227 spi_parport_txrx_word_mode0(struct spi_device *spi,
228 unsigned nsecs,
229 u32 word, u8 bits)
230 {
231 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
232 }
233
234 static u32
235 spi_parport_txrx_word_mode1(struct spi_device *spi,
236 unsigned nsecs,
237 u32 word, u8 bits)
238 {
239 return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
240 }
241
242 static u32
243 spi_parport_txrx_word_mode2(struct spi_device *spi,
244 unsigned nsecs,
245 u32 word, u8 bits)
246 {
247 return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
248 }
249
250 static u32
251 spi_parport_txrx_word_mode3(struct spi_device *spi,
252 unsigned nsecs,
253 u32 word, u8 bits)
254 {
255 return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
256 }
257
258
259
260 /*----------------------------------------------------------------------*/
261
262 static void spi_parport_attach(struct parport *p)
263 {
264 int status;
265 struct spi_parport *pp;
266 struct spi_master *master;
267 struct device *dev = p->physport->dev;
268
269 if (spi_parport || !dev)
270 return;
271
272 /* REVISIT: this just _assumes_ a spi_parport is there ... no probe,
273 * and no way to be selective about what it binds to.
274 */
275
276 master = spi_alloc_master(dev, sizeof *pp);
277 if (!master) {
278 status = -ENOMEM;
279 goto done;
280 }
281 pp = spi_master_get_devdata(master);
282
283 master->bus_num = -1; /* dynamic alloc of a bus number */
284 master->num_chipselect = 1;
285
286 /*
287 * SPI and bitbang hookup
288 *
289 * use default setup(), cleanup(), and transfer() methods; and
290 * only bother implementing mode 0. Start it later.
291 */
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
299
300 /*
301 * parport hookup
302 */
303 pp->port = p;
304 //##### because we want to share with gpio_partport !!!!!
305 pp->pdev = parport_register_device(p, DRVNAME,
306 NULL, NULL, NULL,
307 0 /* FLAGS */, pp); //PARPORT_FLAG_EXCL, pp);
308 if (!pp->pdev) {
309 pr_err("%s: unable to register with parport\n", DRVNAME);
310 status = -ENOMEM;
311 goto clean0;
312 }
313
314 status = parport_claim(pp->pdev);
315 if (status < 0)
316 goto clean1;
317
318 pp->lastbyte = 0;
319 parport_write_data(pp->port, pp->lastbyte);
320
321 /*
322 * Spi_Parport powerup, run firmware
323 */
324 pr_debug("%s: powerup Spi_Parport\n", DRVNAME);
325
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);
329
330 /* stabilize power with spi_sck_bit clear (CPOL=0)
331 */
332 pp->lastbyte |= vcc_bits;
333 parport_write_data(pp->port, pp->lastbyte);
334 msleep(5);
335
336 /*
337 * Start SPI ... for now, hide that we're two physical busses.
338 */
339 status = spi_bitbang_start(&pp->bitbang);
340 if (status < 0) {
341 pr_warning("%s: spi_bitbang_start failed with status %d\n",
342 DRVNAME, status);
343 goto clean2;
344 }
345
346 /*
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
350 * driver.
351 */
352 if (modalias[0]) {
353 pr_info("%s: Will load protocol driver: '%s'!\n", DRVNAME, modalias);
354 } else goto clean2;
355
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;
361
362 /* Enable access to our primary data structure via
363 * the board info's (void *)controller_data.
364 */
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);
368 if (pp->spi_parport)
369 pr_info("%s: SPI tty at %s\n", DRVNAME,
370 pp->spi_parport->dev.bus_id);
371 else {
372 pr_warning("%s: spi_new_device failed\n", DRVNAME);
373 status = -ENODEV;
374 goto out_bitbang_stop;
375 }
376 pp->spi_parport->bits_per_word=spi_bits;
377
378 /* get spi_bitbang_transfer either via global Symbol, or better
379 * ask it from the driver structure
380 */
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;
384 spi_msg = pp->msg;
385 if (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;
390 #if 0
391 spi_msg->list_head ... Addresse der ersten SPI transfer struct
392
393
394 spi_msg->tx_buf = ;
395 spi_msg->len = 1;
396 spi_msg->cs_change = 1;
397 spi_msg->delay_usecs = 0;
398
399 /* fill up message */
400 master->transfer(pp->spi_parport, spi_msg);
401 #endif
402 }
403 }
404 spi_parport = pp;
405 return;
406
407 out_bitbang_stop:
408 spi_bitbang_stop(&pp->bitbang);
409 clean2:
410 /* turn off VCC */
411 parport_write_data(pp->port, 0);
412
413 parport_release(pp->pdev);
414 clean1:
415 parport_unregister_device(pp->pdev);
416 clean0:
417 (void) spi_master_put(pp->bitbang.master);
418 done:
419 pr_debug("%s: spi_parport probe, fail %d\n", DRVNAME, status);
420 }
421
422 static void spi_parport_detach(struct parport *p)
423 {
424 struct spi_parport *pp;
425 int status;
426
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?
430 */
431 if (!spi_parport || spi_parport->port != p)
432 return;
433 pp = spi_parport;
434 if (pp->msg) {
435 spi_message_free(pp->msg);
436 pr_info("Dealloc SPI message buffer\n");
437 }
438
439 /* stop() unregisters child devices too */
440 status = spi_bitbang_stop(&pp->bitbang);
441
442 /* turn off VCC */
443 parport_write_data(pp->port, 0);
444 msleep(10);
445
446 parport_release(pp->pdev);
447 parport_unregister_device(pp->pdev);
448
449 (void) spi_master_put(pp->bitbang.master);
450
451 spi_parport = NULL;
452 }
453
454 static void spi_parport_detach(struct parport *pb)
455 {
456 ppa_struct *dev;
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);
463 kfree(dev);
464 break;
465 }
466 }
467 }
468
469
470 static struct parport_driver spi_parport_driver = {
471 .name = DRVNAME,
472 .attach = spi_parport_attach,
473 .detach = spi_parport_detach,
474 };
475
476
477 static int __init spi_parport_init(void)
478 {
479 return parport_register_driver(&spi_parport_driver);
480 }
481 device_initcall(spi_parport_init);
482
483 static void __exit spi_parport_exit(void)
484 {
485 parport_unregister_driver(&spi_parport_driver);
486 }
487 module_exit(spi_parport_exit);
488
489 MODULE_DESCRIPTION("SPI master controller driver for Parport Adapter");
490 MODULE_LICENSE("GPL");
491
492 module_param_string(spi_pdrv, modalias, sizeof(modalias), S_IRUGO);
493 MODULE_PARM_DESC(spi_pdrv, "spi protocol driver name");
494
495 module_param(spi_mode, uint, S_IRUGO);
496 MODULE_PARM_DESC(spi_mode, "spi mode");
497
498 module_param(spi_bits, uint, S_IRUGO);
499 MODULE_PARM_DESC(spi_mode, "spi bits per word");
500
501 MODULE_INFO(Version, DRIVER_VERSION);