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