spi_lm70llp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * spi_lm70llp.c - driver for lm70llp eval 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. * The LM70 is a temperature sensor chip from National Semiconductor; its
  39. * datasheet is available at http://www.national.com/pf/LM/LM70.html
  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.
  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. parport_write_data(pp->port, data | nCS);
  94. }
  95. static inline void assertCS(struct spi_lm70llp *pp)
  96. {
  97. u8 data = parport_read_data(pp->port);
  98. parport_write_data(pp->port, data & ~nCS);
  99. }
  100. static inline void clkHigh(struct spi_lm70llp *pp)
  101. {
  102. u8 data = parport_read_data(pp->port);
  103. parport_write_data(pp->port, data | SCLK);
  104. }
  105. static inline void clkLow(struct spi_lm70llp *pp)
  106. {
  107. u8 data = parport_read_data(pp->port);
  108. parport_write_data(pp->port, data & ~SCLK);
  109. }
  110. /*------------------------- SPI-LM70-specific inlines ----------------------*/
  111. static inline void spidelay(unsigned d)
  112. {
  113. udelay(d);
  114. }
  115. static inline void setsck(struct spi_device *s, int is_on)
  116. {
  117. struct spi_lm70llp *pp = spidev_to_pp(s);
  118. if (is_on)
  119. clkHigh(pp);
  120. else
  121. clkLow(pp);
  122. }
  123. static inline void setmosi(struct spi_device *s, int is_on)
  124. {
  125. /* FIXME update D7 ... this way we can put the chip
  126. * into shutdown mode and read the manufacturer ID,
  127. * but we can't put it back into operational mode.
  128. */
  129. }
  130. /*
  131. * getmiso:
  132. * Why do we return 0 when the SIO line is high and vice-versa?
  133. * The fact is, the lm70 eval board from NS (which this driver drives),
  134. * is wired in just such a way : when the lm70's SIO goes high, a transistor
  135. * switches it to low reflecting this on the parport (pin 13), and vice-versa.
  136. */
  137. static inline int getmiso(struct spi_device *s)
  138. {
  139. struct spi_lm70llp *pp = spidev_to_pp(s);
  140. return ((SIO == (parport_read_status(pp->port) & SIO)) ? 0 : 1 );
  141. }
  142. /*--------------------------------------------------------------------*/
  143. #define EXPAND_BITBANG_TXRX 1
  144. #include <linux/spi/spi_bitbang.h>
  145. static void lm70_chipselect(struct spi_device *spi, int value)
  146. {
  147. struct spi_lm70llp *pp = spidev_to_pp(spi);
  148. if (value)
  149. assertCS(pp);
  150. else
  151. deassertCS(pp);
  152. }
  153. /*
  154. * Our actual bitbanger routine.
  155. */
  156. static u32 lm70_txrx(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits)
  157. {
  158. static u32 sio=0;
  159. static int first_time=1;
  160. /* First time: perform SPI bitbang and return the LSB of
  161. * the result of the SPI call.
  162. */
  163. if (first_time) {
  164. sio = bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  165. first_time=0;
  166. return (sio & 0x00ff);
  167. }
  168. /* Return the MSB of the result of the SPI call */
  169. else {
  170. first_time=1;
  171. return (sio >> 8);
  172. }
  173. }
  174. static void spi_lm70llp_attach(struct parport *p)
  175. {
  176. struct pardevice *pd;
  177. struct spi_lm70llp *pp;
  178. struct spi_master *master;
  179. int status;
  180. if (lm70llp) {
  181. printk(KERN_WARNING
  182. "%s: spi_lm70llp instance already loaded. Aborting.\n",
  183. DRVNAME);
  184. return;
  185. }
  186. /* TODO: this just _assumes_ a lm70 is there ... no probe;
  187. * the lm70 driver could verify it, reading the manf ID.
  188. */
  189. master = spi_alloc_master(p->physport->dev, sizeof *pp);
  190. if (!master) {
  191. status = -ENOMEM;
  192. goto out_fail;
  193. }
  194. pp = spi_master_get_devdata(master);
  195. master->bus_num = -1; /* dynamic alloc of a bus number */
  196. master->num_chipselect = 1;
  197. /*
  198. * SPI and bitbang hookup.
  199. */
  200. pp->bitbang.master = spi_master_get(master);
  201. pp->bitbang.chipselect = lm70_chipselect;
  202. pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
  203. pp->bitbang.flags = SPI_3WIRE;
  204. /*
  205. * Parport hookup
  206. */
  207. pp->port = p;
  208. pd = parport_register_device(p, DRVNAME,
  209. NULL, NULL, NULL,
  210. PARPORT_FLAG_EXCL, pp);
  211. if (!pd) {
  212. status = -ENOMEM;
  213. goto out_free_master;
  214. }
  215. pp->pd = pd;
  216. status = parport_claim(pd);
  217. if (status < 0)
  218. goto out_parport_unreg;
  219. /*
  220. * Start SPI ...
  221. */
  222. status = spi_bitbang_start(&pp->bitbang);
  223. if (status < 0) {
  224. printk(KERN_WARNING
  225. "%s: spi_bitbang_start failed with status %d\n",
  226. DRVNAME, status);
  227. goto out_off_and_release;
  228. }
  229. /*
  230. * The modalias name MUST match the device_driver name
  231. * for the bus glue code to match and subsequently bind them.
  232. * We are binding to the generic drivers/hwmon/lm70.c device
  233. * driver.
  234. */
  235. strcpy(pp->info.modalias, "lm70");
  236. pp->info.max_speed_hz = 6 * 1000 * 1000;
  237. pp->info.chip_select = 0;
  238. pp->info.mode = SPI_3WIRE | SPI_MODE_0;
  239. /* power up the chip, and let the LM70 control SI/SO */
  240. parport_write_data(pp->port, lm70_INIT);
  241. /* Enable access to our primary data structure via
  242. * the board info's (void *)controller_data.
  243. */
  244. pp->info.controller_data = pp;
  245. pp->spidev_lm70 = spi_new_device(pp->bitbang.master, &pp->info);
  246. if (pp->spidev_lm70)
  247. dev_dbg(&pp->spidev_lm70->dev, "spidev_lm70 at %s\n",
  248. pp->spidev_lm70->dev.bus_id);
  249. else {
  250. printk(KERN_WARNING "%s: spi_new_device failed\n", DRVNAME);
  251. status = -ENODEV;
  252. goto out_bitbang_stop;
  253. }
  254. pp->spidev_lm70->bits_per_word = 16;
  255. lm70llp = pp;
  256. return;
  257. out_bitbang_stop:
  258. spi_bitbang_stop(&pp->bitbang);
  259. out_off_and_release:
  260. /* power down */
  261. parport_write_data(pp->port, 0);
  262. mdelay(10);
  263. parport_release(pp->pd);
  264. out_parport_unreg:
  265. parport_unregister_device(pd);
  266. out_free_master:
  267. (void) spi_master_put(master);
  268. out_fail:
  269. pr_info("%s: spi_lm70llp probe fail, status %d\n", DRVNAME, status);
  270. }
  271. static void spi_lm70llp_detach(struct parport *p)
  272. {
  273. struct spi_lm70llp *pp;
  274. if (!lm70llp || lm70llp->port != p)
  275. return;
  276. pp = lm70llp;
  277. spi_bitbang_stop(&pp->bitbang);
  278. /* power down */
  279. parport_write_data(pp->port, 0);
  280. msleep(10);
  281. parport_release(pp->pd);
  282. parport_unregister_device(pp->pd);
  283. (void) spi_master_put(pp->bitbang.master);
  284. lm70llp = NULL;
  285. }
  286. static struct parport_driver spi_lm70llp_drv = {
  287. .name = DRVNAME,
  288. .attach = spi_lm70llp_attach,
  289. .detach = spi_lm70llp_detach,
  290. };
  291. static int __init init_spi_lm70llp(void)
  292. {
  293. return parport_register_driver(&spi_lm70llp_drv);
  294. }
  295. module_init(init_spi_lm70llp);
  296. static void __exit cleanup_spi_lm70llp(void)
  297. {
  298. parport_unregister_driver(&spi_lm70llp_drv);
  299. }
  300. module_exit(cleanup_spi_lm70llp);
  301. MODULE_AUTHOR("Kaiwan N Billimoria <kaiwan@designergraphix.com>");
  302. MODULE_DESCRIPTION(
  303. "Parport adapter for the National Semiconductor LM70 LLP eval board");
  304. MODULE_LICENSE("GPL");