1ca632f55SGrant Likely /* 2ca632f55SGrant Likely * Driver for LM70EVAL-LLP board for the LM70 sensor 3ca632f55SGrant Likely * 4ca632f55SGrant Likely * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com> 5ca632f55SGrant Likely * 6ca632f55SGrant Likely * This program is free software; you can redistribute it and/or modify 7ca632f55SGrant Likely * it under the terms of the GNU General Public License as published by 8ca632f55SGrant Likely * the Free Software Foundation; either version 2 of the License, or 9ca632f55SGrant Likely * (at your option) any later version. 10ca632f55SGrant Likely * 11ca632f55SGrant Likely * This program is distributed in the hope that it will be useful, 12ca632f55SGrant Likely * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ca632f55SGrant Likely * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14ca632f55SGrant Likely * GNU General Public License for more details. 15ca632f55SGrant Likely */ 16ca632f55SGrant Likely 17ca632f55SGrant Likely #include <linux/init.h> 18ca632f55SGrant Likely #include <linux/module.h> 19ca632f55SGrant Likely #include <linux/kernel.h> 20ca632f55SGrant Likely #include <linux/delay.h> 21ca632f55SGrant Likely #include <linux/device.h> 22ca632f55SGrant Likely #include <linux/parport.h> 23ca632f55SGrant Likely #include <linux/sysfs.h> 24ca632f55SGrant Likely #include <linux/workqueue.h> 25ca632f55SGrant Likely 26ca632f55SGrant Likely #include <linux/spi/spi.h> 27ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h> 28ca632f55SGrant Likely 29ca632f55SGrant Likely /* 30ca632f55SGrant Likely * The LM70 communicates with a host processor using a 3-wire variant of 31ca632f55SGrant Likely * the SPI/Microwire bus interface. This driver specifically supports an 32ca632f55SGrant Likely * NS LM70 LLP Evaluation Board, interfacing to a PC using its parallel 33ca632f55SGrant Likely * port to bitbang an SPI-parport bridge. Accordingly, this is an SPI 34ca632f55SGrant Likely * master controller driver. The hwmon/lm70 driver is a "SPI protocol 35ca632f55SGrant Likely * driver", layered on top of this one and usable without the lm70llp. 36ca632f55SGrant Likely * 37ca632f55SGrant Likely * Datasheet and Schematic: 38ca632f55SGrant Likely * The LM70 is a temperature sensor chip from National Semiconductor; its 39ca632f55SGrant Likely * datasheet is available at http://www.national.com/pf/LM/LM70.html 40ca632f55SGrant Likely * The schematic for this particular board (the LM70EVAL-LLP) is 41ca632f55SGrant Likely * available (on page 4) here: 42ca632f55SGrant Likely * http://www.national.com/appinfo/tempsensors/files/LM70LLPEVALmanual.pdf 43ca632f55SGrant Likely * 44ca632f55SGrant Likely * Also see Documentation/spi/spi-lm70llp. The SPI<->parport code here is 45ca632f55SGrant Likely * (heavily) based on spi-butterfly by David Brownell. 46ca632f55SGrant Likely * 47ca632f55SGrant Likely * The LM70 LLP connects to the PC parallel port in the following manner: 48ca632f55SGrant Likely * 49ca632f55SGrant Likely * Parallel LM70 LLP 50ca632f55SGrant Likely * Port Direction JP2 Header 51ca632f55SGrant Likely * ----------- --------- ------------ 52ca632f55SGrant Likely * D0 2 - - 53ca632f55SGrant Likely * D1 3 --> V+ 5 54ca632f55SGrant Likely * D2 4 --> V+ 5 55ca632f55SGrant Likely * D3 5 --> V+ 5 56ca632f55SGrant Likely * D4 6 --> V+ 5 57ca632f55SGrant Likely * D5 7 --> nCS 8 58ca632f55SGrant Likely * D6 8 --> SCLK 3 59ca632f55SGrant Likely * D7 9 --> SI/O 5 60ca632f55SGrant Likely * GND 25 - GND 7 61ca632f55SGrant Likely * Select 13 <-- SI/O 1 62ca632f55SGrant Likely * 63ca632f55SGrant Likely * Note that parport pin 13 actually gets inverted by the transistor 64ca632f55SGrant Likely * arrangement which lets either the parport or the LM70 drive the 65ca632f55SGrant Likely * SI/SO signal (see the schematic for details). 66ca632f55SGrant Likely */ 67ca632f55SGrant Likely 68ca632f55SGrant Likely #define DRVNAME "spi-lm70llp" 69ca632f55SGrant Likely 70ca632f55SGrant Likely #define lm70_INIT 0xBE 71ca632f55SGrant Likely #define SIO 0x10 72ca632f55SGrant Likely #define nCS 0x20 73ca632f55SGrant Likely #define SCLK 0x40 74ca632f55SGrant Likely 75ca632f55SGrant Likely /*-------------------------------------------------------------------------*/ 76ca632f55SGrant Likely 77ca632f55SGrant Likely struct spi_lm70llp { 78ca632f55SGrant Likely struct spi_bitbang bitbang; 79ca632f55SGrant Likely struct parport *port; 80ca632f55SGrant Likely struct pardevice *pd; 81ca632f55SGrant Likely struct spi_device *spidev_lm70; 82ca632f55SGrant Likely struct spi_board_info info; 83ca632f55SGrant Likely //struct device *dev; 84ca632f55SGrant Likely }; 85ca632f55SGrant Likely 86ca632f55SGrant Likely /* REVISIT : ugly global ; provides "exclusive open" facility */ 87ca632f55SGrant Likely static struct spi_lm70llp *lm70llp; 88ca632f55SGrant Likely 89ca632f55SGrant Likely /*-------------------------------------------------------------------*/ 90ca632f55SGrant Likely 91ca632f55SGrant Likely static inline struct spi_lm70llp *spidev_to_pp(struct spi_device *spi) 92ca632f55SGrant Likely { 93ca632f55SGrant Likely return spi->controller_data; 94ca632f55SGrant Likely } 95ca632f55SGrant Likely 96ca632f55SGrant Likely /*---------------------- LM70 LLP eval board-specific inlines follow */ 97ca632f55SGrant Likely 98ca632f55SGrant Likely /* NOTE: we don't actually need to reread the output values, since they'll 99ca632f55SGrant Likely * still be what we wrote before. Plus, going through parport builds in 100ca632f55SGrant Likely * a ~1ms/operation delay; these SPI transfers could easily be faster. 101ca632f55SGrant Likely */ 102ca632f55SGrant Likely 103ca632f55SGrant Likely static inline void deassertCS(struct spi_lm70llp *pp) 104ca632f55SGrant Likely { 105ca632f55SGrant Likely u8 data = parport_read_data(pp->port); 106ca632f55SGrant Likely 107ca632f55SGrant Likely data &= ~0x80; /* pull D7/SI-out low while de-asserted */ 108ca632f55SGrant Likely parport_write_data(pp->port, data | nCS); 109ca632f55SGrant Likely } 110ca632f55SGrant Likely 111ca632f55SGrant Likely static inline void assertCS(struct spi_lm70llp *pp) 112ca632f55SGrant Likely { 113ca632f55SGrant Likely u8 data = parport_read_data(pp->port); 114ca632f55SGrant Likely 115ca632f55SGrant Likely data |= 0x80; /* pull D7/SI-out high so lm70 drives SO-in */ 116ca632f55SGrant Likely parport_write_data(pp->port, data & ~nCS); 117ca632f55SGrant Likely } 118ca632f55SGrant Likely 119ca632f55SGrant Likely static inline void clkHigh(struct spi_lm70llp *pp) 120ca632f55SGrant Likely { 121ca632f55SGrant Likely u8 data = parport_read_data(pp->port); 122832329d7SSudip Mukherjee 123ca632f55SGrant Likely parport_write_data(pp->port, data | SCLK); 124ca632f55SGrant Likely } 125ca632f55SGrant Likely 126ca632f55SGrant Likely static inline void clkLow(struct spi_lm70llp *pp) 127ca632f55SGrant Likely { 128ca632f55SGrant Likely u8 data = parport_read_data(pp->port); 129832329d7SSudip Mukherjee 130ca632f55SGrant Likely parport_write_data(pp->port, data & ~SCLK); 131ca632f55SGrant Likely } 132ca632f55SGrant Likely 133ca632f55SGrant Likely /*------------------------- SPI-LM70-specific inlines ----------------------*/ 134ca632f55SGrant Likely 135ca632f55SGrant Likely static inline void spidelay(unsigned d) 136ca632f55SGrant Likely { 137ca632f55SGrant Likely udelay(d); 138ca632f55SGrant Likely } 139ca632f55SGrant Likely 140ca632f55SGrant Likely static inline void setsck(struct spi_device *s, int is_on) 141ca632f55SGrant Likely { 142ca632f55SGrant Likely struct spi_lm70llp *pp = spidev_to_pp(s); 143ca632f55SGrant Likely 144ca632f55SGrant Likely if (is_on) 145ca632f55SGrant Likely clkHigh(pp); 146ca632f55SGrant Likely else 147ca632f55SGrant Likely clkLow(pp); 148ca632f55SGrant Likely } 149ca632f55SGrant Likely 150ca632f55SGrant Likely static inline void setmosi(struct spi_device *s, int is_on) 151ca632f55SGrant Likely { 152ca632f55SGrant Likely /* FIXME update D7 ... this way we can put the chip 153ca632f55SGrant Likely * into shutdown mode and read the manufacturer ID, 154ca632f55SGrant Likely * but we can't put it back into operational mode. 155ca632f55SGrant Likely */ 156ca632f55SGrant Likely } 157ca632f55SGrant Likely 158ca632f55SGrant Likely /* 159ca632f55SGrant Likely * getmiso: 160ca632f55SGrant Likely * Why do we return 0 when the SIO line is high and vice-versa? 161ca632f55SGrant Likely * The fact is, the lm70 eval board from NS (which this driver drives), 162ca632f55SGrant Likely * is wired in just such a way : when the lm70's SIO goes high, a transistor 163ca632f55SGrant Likely * switches it to low reflecting this on the parport (pin 13), and vice-versa. 164ca632f55SGrant Likely */ 165ca632f55SGrant Likely static inline int getmiso(struct spi_device *s) 166ca632f55SGrant Likely { 167ca632f55SGrant Likely struct spi_lm70llp *pp = spidev_to_pp(s); 168832329d7SSudip Mukherjee 169ca632f55SGrant Likely return ((SIO == (parport_read_status(pp->port) & SIO)) ? 0 : 1); 170ca632f55SGrant Likely } 171832329d7SSudip Mukherjee 172ca632f55SGrant Likely /*--------------------------------------------------------------------*/ 173ca632f55SGrant Likely 174ca632f55SGrant Likely #include "spi-bitbang-txrx.h" 175ca632f55SGrant Likely 176ca632f55SGrant Likely static void lm70_chipselect(struct spi_device *spi, int value) 177ca632f55SGrant Likely { 178ca632f55SGrant Likely struct spi_lm70llp *pp = spidev_to_pp(spi); 179ca632f55SGrant Likely 180ca632f55SGrant Likely if (value) 181ca632f55SGrant Likely assertCS(pp); 182ca632f55SGrant Likely else 183ca632f55SGrant Likely deassertCS(pp); 184ca632f55SGrant Likely } 185ca632f55SGrant Likely 186ca632f55SGrant Likely /* 187ca632f55SGrant Likely * Our actual bitbanger routine. 188ca632f55SGrant Likely */ 189ca632f55SGrant Likely static u32 lm70_txrx(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits) 190ca632f55SGrant Likely { 191ca632f55SGrant Likely return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits); 192ca632f55SGrant Likely } 193ca632f55SGrant Likely 194ca632f55SGrant Likely static void spi_lm70llp_attach(struct parport *p) 195ca632f55SGrant Likely { 196ca632f55SGrant Likely struct pardevice *pd; 197ca632f55SGrant Likely struct spi_lm70llp *pp; 198ca632f55SGrant Likely struct spi_master *master; 199ca632f55SGrant Likely int status; 2002baed30cSSudip Mukherjee struct pardev_cb lm70llp_cb; 201ca632f55SGrant Likely 202ca632f55SGrant Likely if (lm70llp) { 203ca632f55SGrant Likely printk(KERN_WARNING 204ca632f55SGrant Likely "%s: spi_lm70llp instance already loaded. Aborting.\n", 205ca632f55SGrant Likely DRVNAME); 206ca632f55SGrant Likely return; 207ca632f55SGrant Likely } 208ca632f55SGrant Likely 209ca632f55SGrant Likely /* TODO: this just _assumes_ a lm70 is there ... no probe; 210ca632f55SGrant Likely * the lm70 driver could verify it, reading the manf ID. 211ca632f55SGrant Likely */ 212ca632f55SGrant Likely 213ca632f55SGrant Likely master = spi_alloc_master(p->physport->dev, sizeof *pp); 214ca632f55SGrant Likely if (!master) { 215ca632f55SGrant Likely status = -ENOMEM; 216ca632f55SGrant Likely goto out_fail; 217ca632f55SGrant Likely } 218ca632f55SGrant Likely pp = spi_master_get_devdata(master); 219ca632f55SGrant Likely 220ca632f55SGrant Likely /* 221ca632f55SGrant Likely * SPI and bitbang hookup. 222ca632f55SGrant Likely */ 22394c69f76SAxel Lin pp->bitbang.master = master; 224ca632f55SGrant Likely pp->bitbang.chipselect = lm70_chipselect; 225ca632f55SGrant Likely pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx; 226ca632f55SGrant Likely pp->bitbang.flags = SPI_3WIRE; 227ca632f55SGrant Likely 228ca632f55SGrant Likely /* 229ca632f55SGrant Likely * Parport hookup 230ca632f55SGrant Likely */ 231ca632f55SGrant Likely pp->port = p; 2322baed30cSSudip Mukherjee memset(&lm70llp_cb, 0, sizeof(lm70llp_cb)); 2332baed30cSSudip Mukherjee lm70llp_cb.private = pp; 2342baed30cSSudip Mukherjee lm70llp_cb.flags = PARPORT_FLAG_EXCL; 2352baed30cSSudip Mukherjee pd = parport_register_dev_model(p, DRVNAME, &lm70llp_cb, 0); 2362baed30cSSudip Mukherjee 237ca632f55SGrant Likely if (!pd) { 238ca632f55SGrant Likely status = -ENOMEM; 239ca632f55SGrant Likely goto out_free_master; 240ca632f55SGrant Likely } 241ca632f55SGrant Likely pp->pd = pd; 242ca632f55SGrant Likely 243ca632f55SGrant Likely status = parport_claim(pd); 244ca632f55SGrant Likely if (status < 0) 245ca632f55SGrant Likely goto out_parport_unreg; 246ca632f55SGrant Likely 247ca632f55SGrant Likely /* 248ca632f55SGrant Likely * Start SPI ... 249ca632f55SGrant Likely */ 250ca632f55SGrant Likely status = spi_bitbang_start(&pp->bitbang); 251ca632f55SGrant Likely if (status < 0) { 252*46c52c28SSudip Mukherjee dev_warn(&pd->dev, "spi_bitbang_start failed with status %d\n", 253*46c52c28SSudip Mukherjee status); 254ca632f55SGrant Likely goto out_off_and_release; 255ca632f55SGrant Likely } 256ca632f55SGrant Likely 257ca632f55SGrant Likely /* 258ca632f55SGrant Likely * The modalias name MUST match the device_driver name 259ca632f55SGrant Likely * for the bus glue code to match and subsequently bind them. 260ca632f55SGrant Likely * We are binding to the generic drivers/hwmon/lm70.c device 261ca632f55SGrant Likely * driver. 262ca632f55SGrant Likely */ 263ca632f55SGrant Likely strcpy(pp->info.modalias, "lm70"); 264ca632f55SGrant Likely pp->info.max_speed_hz = 6 * 1000 * 1000; 265ca632f55SGrant Likely pp->info.chip_select = 0; 266ca632f55SGrant Likely pp->info.mode = SPI_3WIRE | SPI_MODE_0; 267ca632f55SGrant Likely 268ca632f55SGrant Likely /* power up the chip, and let the LM70 control SI/SO */ 269ca632f55SGrant Likely parport_write_data(pp->port, lm70_INIT); 270ca632f55SGrant Likely 271ca632f55SGrant Likely /* Enable access to our primary data structure via 272ca632f55SGrant Likely * the board info's (void *)controller_data. 273ca632f55SGrant Likely */ 274ca632f55SGrant Likely pp->info.controller_data = pp; 275ca632f55SGrant Likely pp->spidev_lm70 = spi_new_device(pp->bitbang.master, &pp->info); 276ca632f55SGrant Likely if (pp->spidev_lm70) 277ca632f55SGrant Likely dev_dbg(&pp->spidev_lm70->dev, "spidev_lm70 at %s\n", 278ca632f55SGrant Likely dev_name(&pp->spidev_lm70->dev)); 279ca632f55SGrant Likely else { 280*46c52c28SSudip Mukherjee dev_warn(&pd->dev, "spi_new_device failed\n"); 281ca632f55SGrant Likely status = -ENODEV; 282ca632f55SGrant Likely goto out_bitbang_stop; 283ca632f55SGrant Likely } 284ca632f55SGrant Likely pp->spidev_lm70->bits_per_word = 8; 285ca632f55SGrant Likely 286ca632f55SGrant Likely lm70llp = pp; 287ca632f55SGrant Likely return; 288ca632f55SGrant Likely 289ca632f55SGrant Likely out_bitbang_stop: 290ca632f55SGrant Likely spi_bitbang_stop(&pp->bitbang); 291ca632f55SGrant Likely out_off_and_release: 292ca632f55SGrant Likely /* power down */ 293ca632f55SGrant Likely parport_write_data(pp->port, 0); 294ca632f55SGrant Likely mdelay(10); 295ca632f55SGrant Likely parport_release(pp->pd); 296ca632f55SGrant Likely out_parport_unreg: 297ca632f55SGrant Likely parport_unregister_device(pd); 298ca632f55SGrant Likely out_free_master: 29925fb1a46SSudip Mukherjee spi_master_put(master); 300ca632f55SGrant Likely out_fail: 301ca632f55SGrant Likely pr_info("%s: spi_lm70llp probe fail, status %d\n", DRVNAME, status); 302ca632f55SGrant Likely } 303ca632f55SGrant Likely 304ca632f55SGrant Likely static void spi_lm70llp_detach(struct parport *p) 305ca632f55SGrant Likely { 306ca632f55SGrant Likely struct spi_lm70llp *pp; 307ca632f55SGrant Likely 308ca632f55SGrant Likely if (!lm70llp || lm70llp->port != p) 309ca632f55SGrant Likely return; 310ca632f55SGrant Likely 311ca632f55SGrant Likely pp = lm70llp; 312ca632f55SGrant Likely spi_bitbang_stop(&pp->bitbang); 313ca632f55SGrant Likely 314ca632f55SGrant Likely /* power down */ 315ca632f55SGrant Likely parport_write_data(pp->port, 0); 316ca632f55SGrant Likely 317ca632f55SGrant Likely parport_release(pp->pd); 318ca632f55SGrant Likely parport_unregister_device(pp->pd); 319ca632f55SGrant Likely 32025fb1a46SSudip Mukherjee spi_master_put(pp->bitbang.master); 321ca632f55SGrant Likely 322ca632f55SGrant Likely lm70llp = NULL; 323ca632f55SGrant Likely } 324ca632f55SGrant Likely 325ca632f55SGrant Likely static struct parport_driver spi_lm70llp_drv = { 326ca632f55SGrant Likely .name = DRVNAME, 3272baed30cSSudip Mukherjee .match_port = spi_lm70llp_attach, 328ca632f55SGrant Likely .detach = spi_lm70llp_detach, 3292baed30cSSudip Mukherjee .devmodel = true, 330ca632f55SGrant Likely }; 331ca632f55SGrant Likely 332ca632f55SGrant Likely static int __init init_spi_lm70llp(void) 333ca632f55SGrant Likely { 334ca632f55SGrant Likely return parport_register_driver(&spi_lm70llp_drv); 335ca632f55SGrant Likely } 336ca632f55SGrant Likely module_init(init_spi_lm70llp); 337ca632f55SGrant Likely 338ca632f55SGrant Likely static void __exit cleanup_spi_lm70llp(void) 339ca632f55SGrant Likely { 340ca632f55SGrant Likely parport_unregister_driver(&spi_lm70llp_drv); 341ca632f55SGrant Likely } 342ca632f55SGrant Likely module_exit(cleanup_spi_lm70llp); 343ca632f55SGrant Likely 344ca632f55SGrant Likely MODULE_AUTHOR("Kaiwan N Billimoria <kaiwan@designergraphix.com>"); 345ca632f55SGrant Likely MODULE_DESCRIPTION( 346ca632f55SGrant Likely "Parport adapter for the National Semiconductor LM70 LLP eval board"); 347ca632f55SGrant Likely MODULE_LICENSE("GPL"); 348