spi-lm70llp.c 8.9 KB

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