spi-lm70llp.c 9.0 KB

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