spi_lm70llp.c 9.2 KB

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