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