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