X-Git-Url: http://git.linex4red.de/pub/spi-gpio-pp.git/blobdiff_plain/ed56ae3ac69596bea18437ef0b0752d64b7e6d49..b64fc7ae9036d15175c5dacaf8df3b9109eaf2f5:/spi_parport.c?ds=sidebyside diff --git a/spi_parport.c b/spi_parport.c new file mode 100644 index 0000000..02e26e1 --- /dev/null +++ b/spi_parport.c @@ -0,0 +1,453 @@ +/* + * spi_butterfly.c - parport-to-butterfly adapter + * + * Copyright (C) 2005 David Brownell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * idea: + * separate the butterfly driver into a hotpluggable SPI master controller + * use a board description file, which loads the: + * - the SPI master controller + * - the SPI protocoll driver + * call spi_new_device with + - spi_master = butterfly parport driver (or parameter configurable) + - spi_board_info = infos about Mauro-Lite + + */ +#include +#include +#include +#include +#include + +#include +#include +#include + + + +/* + * This uses SPI to talk with an "AVR Butterfly", which is a $US20 card + * with a battery powered AVR microcontroller and lots of goodies. You + * can use GCC to develop firmware for this. + * + * See Documentation/spi/butterfly for information about how to build + * and use this custom parallel port cable. + */ + +/* + * spi_device + * todo: + * - implement SPI mode 1,2,3 is yet missing + * - prevent ugly handling of global butterfly structure + * - use serial subsystem + * - make stuff module configurable during runtime + */ + +/* DATA output bits (pins 2..9 == D0..D7) */ +#define MODALIASLEN 10 +#define DRVNAME "spi_butterfly" +#define DRIVER_VERSION "0.2" + +#define butterfly_nreset (1 << 1) /* pin 3 */ + +#define spi_sck_bit (1 << 0) /* pin 2 */ +#define spi_mosi_bit (1 << 7) /* pin 9 */ + +#define vcc_bits ((1 << 6) | (1 << 5)) /* pins 7, 8 */ + +/* STATUS input bits */ +#define spi_miso_bit PARPORT_STATUS_BUSY /* pin 11 */ + +/* CONTROL output bits */ +#define spi_cs_bit PARPORT_CONTROL_SELECT /* pin 17 */ + +/* + * Parallel + * Port Direction Signal + * ----------- --------- ------------ + * D0 2 --> SCLK + * D1 3 --> nRESET + * D2 4 --> -- + * D3 5 --> -- + * D4 6 --> -- + * D5 7 --> -- + * D6 8 --> -- + * D7 9 --> MOSI + * GND 25 - GND + * Busy 11 <-- MISO + * Paper 12 <-- -- + * Select 13 <-- -- + * Feed 14 --> -- + * Error 15 <-- -- + * Init 16 --> -- + * Select 17 --> nCS + */ + +//***************************************************************************** +// Globals +//***************************************************************************** +/* module parameters */ +static char modalias[MODALIASLEN] = "maurol"; + +static inline struct butterfly *spidev_to_pp(struct spi_device *spi) +{ + return spi->controller_data; +} + + +struct butterfly { + /* REVISIT ... for now, this must be first */ + struct spi_bitbang bitbang; + + struct parport *port; + struct pardevice *pd; + + u8 lastbyte; /* hold copy of partport to be faster */ + + struct spi_message *msg; + struct spi_device *butterfly; + struct spi_board_info info; + +}; + +/*----------------------------------------------------------------------*/ + + +static inline void +setsck(struct spi_device *spi, int is_on) +{ + struct butterfly *pp = spidev_to_pp(spi); + u8 bit, byte = pp->lastbyte; + + bit = spi_sck_bit; + + if (is_on) + byte |= bit; + else + byte &= ~bit; + parport_write_data(pp->port, byte); + pp->lastbyte = byte; /* use parport mirror to be faster */ +} + +static inline void +setmosi(struct spi_device *spi, int is_on) +{ + struct butterfly *pp = spidev_to_pp(spi); + u8 bit, byte = pp->lastbyte; + + bit = spi_mosi_bit; + + if (is_on) + byte |= bit; + else + byte &= ~bit; + parport_write_data(pp->port, byte); + pp->lastbyte = byte; /* use parport mirror to be faster */ +} + +static inline int getmiso(struct spi_device *spi) +{ + struct butterfly *pp = spidev_to_pp(spi); + int value; + u8 bit; + + bit = spi_miso_bit; + + /* only STATUS_BUSY is NOT negated */ + value = !(parport_read_status(pp->port) & bit); + return (bit == PARPORT_STATUS_BUSY) ? value : !value; +} + +static void butterfly_chipselect(struct spi_device *spi, int value) +{ + struct butterfly *pp = spidev_to_pp(spi); + +#if 0 + /* set default clock polarity */ + if (value != BITBANG_CS_INACTIVE) + setsck(spi, spi->mode & SPI_CPOL); + + /* here, value == "activate or not"; + * most PARPORT_CONTROL_* bits are negated, so we must + * morph it to value == "bit value to write in control register" + */ + if (spi_cs_bit == PARPORT_CONTROL_INIT) + value = !value; + + parport_frob_control(pp->port, spi_cs_bit, value ? spi_cs_bit : 0); +#else + u8 bit, byte = pp->lastbyte; + + bit = butterfly_nreset; + + if (value) + byte &= ~bit; + else + byte |= bit; + parport_write_data(pp->port, byte); + pp->lastbyte = byte; /* use parport mirror to be faster */ +#endif +} + + +/* we only needed to implement one mode here, and choose SPI_MODE_0 */ + +#define spidelay(X) do{}while(0) +//#define spidelay ndelay + +#define EXPAND_BITBANG_TXRX +#include + +static u32 +butterfly_txrx_word_mode0(struct spi_device *spi, + unsigned nsecs, + u32 word, u8 bits) +{ + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits); +} + +/*----------------------------------------------------------------------*/ + +/* REVISIT remove this ugly global and its "only one" limitation */ +static struct butterfly *butterfly; + +static void butterfly_attach(struct parport *p) +{ + struct pardevice *pd; + int status; + struct butterfly *pp; + struct spi_master *master; + struct device *dev = p->physport->dev; + + if (butterfly || !dev) + return; + + /* REVISIT: this just _assumes_ a butterfly is there ... no probe, + * and no way to be selective about what it binds to. + */ + + master = spi_alloc_master(dev, sizeof *pp); + if (!master) { + status = -ENOMEM; + goto done; + } + pp = spi_master_get_devdata(master); + + master->bus_num = -1; /* dynamic alloc of a bus number */ + master->num_chipselect = 1; + + /* + * SPI and bitbang hookup + * + * use default setup(), cleanup(), and transfer() methods; and + * only bother implementing mode 0. Start it later. + */ + pp->bitbang.master = spi_master_get(master); + pp->bitbang.chipselect = butterfly_chipselect; + pp->bitbang.txrx_word[SPI_MODE_0] = butterfly_txrx_word_mode0; + pp->bitbang.flags = SPI_3WIRE; // #################################### + + /* + * parport hookup + */ + pp->port = p; + pd = parport_register_device(p, DRVNAME, + NULL, NULL, NULL, + 0 /* FLAGS */, pp); //PARPORT_FLAG_EXCL, pp); + if (!pd) { + status = -ENOMEM; + goto clean0; + } + pp->pd = pd; + + status = parport_claim(pd); + if (status < 0) + goto clean1; + + pp->lastbyte = 0; + parport_write_data(pp->port, pp->lastbyte); + + /* + * Butterfly reset, powerup, run firmware + */ + pr_debug("%s: powerup/reset Butterfly\n", DRVNAME); + + /* nCS for dataflash (this bit is inverted on output) */ + parport_frob_control(pp->port, spi_cs_bit, 0); + + /* stabilize power with chip in reset (nRESET), and + * spi_sck_bit clear (CPOL=0) + */ + pp->lastbyte |= vcc_bits; + parport_write_data(pp->port, pp->lastbyte); + msleep(5); + +#if 0 + /* take it out of reset; assume long reset delay */ + pp->lastbyte |= butterfly_nreset; + parport_write_data(pp->port, pp->lastbyte); + msleep(10); + + /* take reset as trigger signal ############# */ + pp->lastbyte &= ~butterfly_nreset; + parport_write_data(pp->port, pp->lastbyte); +#endif + + /* + * Start SPI ... for now, hide that we're two physical busses. + */ + status = spi_bitbang_start(&pp->bitbang); + if (status < 0) { + pr_warning("%s: spi_bitbang_start failed with status %d\n", + DRVNAME, status); + goto clean2; + } + + /* + * The modalias name MUST match the device_driver name + * for the bus glue code to match and subsequently bind them. + * We are binding to the generic drivers/hwmon/lm70.c device + * driver. + */ + if (modalias[0]) { + pr_info("%s: Will load protocol driver: '%s'!\n", DRVNAME, modalias); + } else goto clean2; + + /* need to make this parameter loadable */ + strcpy(pp->info.modalias, modalias); + pp->info.max_speed_hz = 15 * 1000 * 1000; + pp->info.chip_select = 0; // 0: .. 1: + pp->info.mode = SPI_3WIRE | SPI_MODE_0; // ################ + + /* Enable access to our primary data structure via + * the board info's (void *)controller_data. + */ + pp->info.platform_data = NULL; // here we should add data structures for subsystem driver ??? + pp->info.controller_data = pp; /* save my structure for later use */ + pp->butterfly = spi_new_device(pp->bitbang.master, &pp->info); + if (pp->butterfly) + pr_info("%s: SPI tty at %s\n", DRVNAME, + pp->butterfly->dev.bus_id); + else { + pr_warning("%s: spi_new_device failed\n", DRVNAME); + status = -ENODEV; + goto out_bitbang_stop; + } + // pp->butterfly->bits_per_word=16; // ############### + + /* get spi_bitbang_transfer either via global Symbol, or better + * ask it from the driver structure + */ + pp->msg = spi_message_alloc(1, GFP_KERNEL); + if (master) { /* the same address is also saved in pp->bitbang.master or pp->butterfly->master */ + struct spi_message *spi_msg; + spi_msg = pp->msg; + if (spi_msg) { + // spi_message_init(spi_msg); + pr_info("Alloc SPI message buffer\n"); + spi_msg->spi = pp->butterfly; + spi_msg->actual_length = 1; +#if 0 + spi_msg->list_head ... Addresse der ersten SPI transfer struct + + + spi_msg->tx_buf = ; + spi_msg->len = 1; + spi_msg->cs_change = 1; + spi_msg->delay_usecs = 0; + + /* fill up message */ + master->transfer(pp->butterfly, spi_msg); +#endif + } + } + butterfly = pp; + return; + +out_bitbang_stop: + spi_bitbang_stop(&pp->bitbang); +clean2: + /* turn off VCC */ + parport_write_data(pp->port, 0); + + parport_release(pp->pd); +clean1: + parport_unregister_device(pd); +clean0: + (void) spi_master_put(pp->bitbang.master); +done: + pr_debug("%s: butterfly probe, fail %d\n", DRVNAME, status); +} + +static void butterfly_detach(struct parport *p) +{ + struct butterfly *pp; + int status; + + /* FIXME this global is ugly ... but, how to quickly get from + * the parport to the "struct butterfly" associated with it? + * "old school" driver-internal device lists? + */ + if (!butterfly || butterfly->port != p) + return; + pp = butterfly; + if (pp->msg) { + spi_message_free(pp->msg); + pr_info("Dealloc SPI message buffer\n"); + } + + /* stop() unregisters child devices too */ + status = spi_bitbang_stop(&pp->bitbang); + + /* turn off VCC */ + parport_write_data(pp->port, 0); + msleep(10); + + parport_release(pp->pd); + parport_unregister_device(pp->pd); + + (void) spi_master_put(pp->bitbang.master); + + butterfly = NULL; +} + +static struct parport_driver butterfly_driver = { + .name = DRVNAME, + .attach = butterfly_attach, + .detach = butterfly_detach, +}; + + +static int __init butterfly_init(void) +{ + return parport_register_driver(&butterfly_driver); +} +device_initcall(butterfly_init); + +static void __exit butterfly_exit(void) +{ + parport_unregister_driver(&butterfly_driver); +} +module_exit(butterfly_exit); + +MODULE_DESCRIPTION("Parport Adapter driver for SPI tty Butterfly"); +MODULE_LICENSE("GPL"); + +module_param_string(spi_pdrv, modalias, sizeof(modalias), S_IRUGO); +MODULE_PARM_DESC(spi_pdrv, "spi protocol driver name"); + +MODULE_INFO(Version, DRIVER_VERSION);