xref: /linux/drivers/spi/spi-lm70llp.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Driver for LM70EVAL-LLP board for the LM70 sensor
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
6ca632f55SGrant Likely  */
7ca632f55SGrant Likely 
822de54a9SSudip Mukherjee #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
922de54a9SSudip Mukherjee 
10ca632f55SGrant Likely #include <linux/init.h>
11ca632f55SGrant Likely #include <linux/module.h>
12ca632f55SGrant Likely #include <linux/kernel.h>
13ca632f55SGrant Likely #include <linux/delay.h>
14ca632f55SGrant Likely #include <linux/device.h>
15ca632f55SGrant Likely #include <linux/parport.h>
16ca632f55SGrant Likely #include <linux/sysfs.h>
17ca632f55SGrant Likely #include <linux/workqueue.h>
18ca632f55SGrant Likely 
19ca632f55SGrant Likely #include <linux/spi/spi.h>
20ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
21ca632f55SGrant Likely 
22ca632f55SGrant Likely /*
23ca632f55SGrant Likely  * The LM70 communicates with a host processor using a 3-wire variant of
24ca632f55SGrant Likely  * the SPI/Microwire bus interface. This driver specifically supports an
25ca632f55SGrant Likely  * NS LM70 LLP Evaluation Board, interfacing to a PC using its parallel
26ca632f55SGrant Likely  * port to bitbang an SPI-parport bridge.  Accordingly, this is an SPI
277c5d1d97SYang Yingliang  * host controller driver.  The hwmon/lm70 driver is a "SPI protocol
28ca632f55SGrant Likely  * driver", layered on top of this one and usable without the lm70llp.
29ca632f55SGrant Likely  *
30ca632f55SGrant Likely  * Datasheet and Schematic:
31ca632f55SGrant Likely  * The LM70 is a temperature sensor chip from National Semiconductor; its
32*7397175cSKousik Sanagavarapu  * datasheet is available at https://www.ti.com/lit/gpn/lm70
33ca632f55SGrant Likely  * The schematic for this particular board (the LM70EVAL-LLP) is
34ca632f55SGrant Likely  * available (on page 4) here:
35*7397175cSKousik Sanagavarapu  *  https://download.datasheets.com/pdfs/documentation/nat/kit&board/lm70llpevalmanual.pdf
36ca632f55SGrant Likely  *
379cdd273eSMauro Carvalho Chehab  * Also see Documentation/spi/spi-lm70llp.rst.  The SPI<->parport code here is
38ca632f55SGrant Likely  * (heavily) based on spi-butterfly by David Brownell.
39ca632f55SGrant Likely  *
40ca632f55SGrant Likely  * The LM70 LLP connects to the PC parallel port in the following manner:
41ca632f55SGrant Likely  *
42ca632f55SGrant Likely  *   Parallel                 LM70 LLP
43ca632f55SGrant Likely  *     Port      Direction   JP2 Header
44ca632f55SGrant Likely  *  -----------  ---------  ------------
45ca632f55SGrant Likely  *      D0    2      -         -
46ca632f55SGrant Likely  *      D1    3     -->      V+   5
47ca632f55SGrant Likely  *      D2    4     -->      V+   5
48ca632f55SGrant Likely  *      D3    5     -->      V+   5
49ca632f55SGrant Likely  *      D4    6     -->      V+   5
50ca632f55SGrant Likely  *      D5    7     -->      nCS  8
51ca632f55SGrant Likely  *      D6    8     -->      SCLK 3
52ca632f55SGrant Likely  *      D7    9     -->      SI/O 5
53ca632f55SGrant Likely  *     GND   25      -       GND  7
54ca632f55SGrant Likely  *    Select 13     <--      SI/O 1
55ca632f55SGrant Likely  *
56ca632f55SGrant Likely  * Note that parport pin 13 actually gets inverted by the transistor
57ca632f55SGrant Likely  * arrangement which lets either the parport or the LM70 drive the
58ca632f55SGrant Likely  * SI/SO signal (see the schematic for details).
59ca632f55SGrant Likely  */
60ca632f55SGrant Likely 
61ca632f55SGrant Likely #define DRVNAME		"spi-lm70llp"
62ca632f55SGrant Likely 
63ca632f55SGrant Likely #define lm70_INIT	0xBE
64ca632f55SGrant Likely #define SIO		0x10
65ca632f55SGrant Likely #define nCS		0x20
66ca632f55SGrant Likely #define SCLK		0x40
67ca632f55SGrant Likely 
68ca632f55SGrant Likely /*-------------------------------------------------------------------------*/
69ca632f55SGrant Likely 
70ca632f55SGrant Likely struct spi_lm70llp {
71ca632f55SGrant Likely 	struct spi_bitbang	bitbang;
72ca632f55SGrant Likely 	struct parport		*port;
73ca632f55SGrant Likely 	struct pardevice	*pd;
74ca632f55SGrant Likely 	struct spi_device	*spidev_lm70;
75ca632f55SGrant Likely 	struct spi_board_info	info;
76ca632f55SGrant Likely 	//struct device		*dev;
77ca632f55SGrant Likely };
78ca632f55SGrant Likely 
79ca632f55SGrant Likely /* REVISIT : ugly global ; provides "exclusive open" facility */
80ca632f55SGrant Likely static struct spi_lm70llp *lm70llp;
81ca632f55SGrant Likely 
82ca632f55SGrant Likely /*-------------------------------------------------------------------*/
83ca632f55SGrant Likely 
spidev_to_pp(struct spi_device * spi)84ca632f55SGrant Likely static inline struct spi_lm70llp *spidev_to_pp(struct spi_device *spi)
85ca632f55SGrant Likely {
86ca632f55SGrant Likely 	return spi->controller_data;
87ca632f55SGrant Likely }
88ca632f55SGrant Likely 
89ca632f55SGrant Likely /*---------------------- LM70 LLP eval board-specific inlines follow */
90ca632f55SGrant Likely 
91ca632f55SGrant Likely /* NOTE:  we don't actually need to reread the output values, since they'll
92ca632f55SGrant Likely  * still be what we wrote before.  Plus, going through parport builds in
93ca632f55SGrant Likely  * a ~1ms/operation delay; these SPI transfers could easily be faster.
94ca632f55SGrant Likely  */
95ca632f55SGrant Likely 
deassertCS(struct spi_lm70llp * pp)96ca632f55SGrant Likely static inline void deassertCS(struct spi_lm70llp *pp)
97ca632f55SGrant Likely {
98ca632f55SGrant Likely 	u8 data = parport_read_data(pp->port);
99ca632f55SGrant Likely 
100ca632f55SGrant Likely 	data &= ~0x80;		/* pull D7/SI-out low while de-asserted */
101ca632f55SGrant Likely 	parport_write_data(pp->port, data | nCS);
102ca632f55SGrant Likely }
103ca632f55SGrant Likely 
assertCS(struct spi_lm70llp * pp)104ca632f55SGrant Likely static inline void assertCS(struct spi_lm70llp *pp)
105ca632f55SGrant Likely {
106ca632f55SGrant Likely 	u8 data = parport_read_data(pp->port);
107ca632f55SGrant Likely 
108ca632f55SGrant Likely 	data |= 0x80;		/* pull D7/SI-out high so lm70 drives SO-in */
109ca632f55SGrant Likely 	parport_write_data(pp->port, data & ~nCS);
110ca632f55SGrant Likely }
111ca632f55SGrant Likely 
clkHigh(struct spi_lm70llp * pp)112ca632f55SGrant Likely static inline void clkHigh(struct spi_lm70llp *pp)
113ca632f55SGrant Likely {
114ca632f55SGrant Likely 	u8 data = parport_read_data(pp->port);
115832329d7SSudip Mukherjee 
116ca632f55SGrant Likely 	parport_write_data(pp->port, data | SCLK);
117ca632f55SGrant Likely }
118ca632f55SGrant Likely 
clkLow(struct spi_lm70llp * pp)119ca632f55SGrant Likely static inline void clkLow(struct spi_lm70llp *pp)
120ca632f55SGrant Likely {
121ca632f55SGrant Likely 	u8 data = parport_read_data(pp->port);
122832329d7SSudip Mukherjee 
123ca632f55SGrant Likely 	parport_write_data(pp->port, data & ~SCLK);
124ca632f55SGrant Likely }
125ca632f55SGrant Likely 
126ca632f55SGrant Likely /*------------------------- SPI-LM70-specific inlines ----------------------*/
127ca632f55SGrant Likely 
spidelay(unsigned d)128ca632f55SGrant Likely static inline void spidelay(unsigned d)
129ca632f55SGrant Likely {
130ca632f55SGrant Likely 	udelay(d);
131ca632f55SGrant Likely }
132ca632f55SGrant Likely 
setsck(struct spi_device * s,int is_on)133ca632f55SGrant Likely static inline void setsck(struct spi_device *s, int is_on)
134ca632f55SGrant Likely {
135ca632f55SGrant Likely 	struct spi_lm70llp *pp = spidev_to_pp(s);
136ca632f55SGrant Likely 
137ca632f55SGrant Likely 	if (is_on)
138ca632f55SGrant Likely 		clkHigh(pp);
139ca632f55SGrant Likely 	else
140ca632f55SGrant Likely 		clkLow(pp);
141ca632f55SGrant Likely }
142ca632f55SGrant Likely 
setmosi(struct spi_device * s,int is_on)143ca632f55SGrant Likely static inline void setmosi(struct spi_device *s, int is_on)
144ca632f55SGrant Likely {
145ca632f55SGrant Likely 	/* FIXME update D7 ... this way we can put the chip
146ca632f55SGrant Likely 	 * into shutdown mode and read the manufacturer ID,
147ca632f55SGrant Likely 	 * but we can't put it back into operational mode.
148ca632f55SGrant Likely 	 */
149ca632f55SGrant Likely }
150ca632f55SGrant Likely 
151ca632f55SGrant Likely /*
152ca632f55SGrant Likely  * getmiso:
153ca632f55SGrant Likely  * Why do we return 0 when the SIO line is high and vice-versa?
154ca632f55SGrant Likely  * The fact is, the lm70 eval board from NS (which this driver drives),
155ca632f55SGrant Likely  * is wired in just such a way : when the lm70's SIO goes high, a transistor
156ca632f55SGrant Likely  * switches it to low reflecting this on the parport (pin 13), and vice-versa.
157ca632f55SGrant Likely  */
getmiso(struct spi_device * s)158ca632f55SGrant Likely static inline int getmiso(struct spi_device *s)
159ca632f55SGrant Likely {
160ca632f55SGrant Likely 	struct spi_lm70llp *pp = spidev_to_pp(s);
161832329d7SSudip Mukherjee 
162ca632f55SGrant Likely 	return ((SIO == (parport_read_status(pp->port) & SIO)) ? 0 : 1);
163ca632f55SGrant Likely }
164832329d7SSudip Mukherjee 
165ca632f55SGrant Likely /*--------------------------------------------------------------------*/
166ca632f55SGrant Likely 
167ca632f55SGrant Likely #include "spi-bitbang-txrx.h"
168ca632f55SGrant Likely 
lm70_chipselect(struct spi_device * spi,int value)169ca632f55SGrant Likely static void lm70_chipselect(struct spi_device *spi, int value)
170ca632f55SGrant Likely {
171ca632f55SGrant Likely 	struct spi_lm70llp *pp = spidev_to_pp(spi);
172ca632f55SGrant Likely 
173ca632f55SGrant Likely 	if (value)
174ca632f55SGrant Likely 		assertCS(pp);
175ca632f55SGrant Likely 	else
176ca632f55SGrant Likely 		deassertCS(pp);
177ca632f55SGrant Likely }
178ca632f55SGrant Likely 
179ca632f55SGrant Likely /*
180ca632f55SGrant Likely  * Our actual bitbanger routine.
181ca632f55SGrant Likely  */
lm70_txrx(struct spi_device * spi,unsigned nsecs,u32 word,u8 bits,unsigned flags)182304d3436SLorenzo Bianconi static u32 lm70_txrx(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits,
183304d3436SLorenzo Bianconi 		     unsigned flags)
184ca632f55SGrant Likely {
185304d3436SLorenzo Bianconi 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
186ca632f55SGrant Likely }
187ca632f55SGrant Likely 
spi_lm70llp_attach(struct parport * p)188ca632f55SGrant Likely static void spi_lm70llp_attach(struct parport *p)
189ca632f55SGrant Likely {
190ca632f55SGrant Likely 	struct pardevice	*pd;
191ca632f55SGrant Likely 	struct spi_lm70llp	*pp;
1927c5d1d97SYang Yingliang 	struct spi_controller	*host;
193ca632f55SGrant Likely 	int			status;
1942baed30cSSudip Mukherjee 	struct pardev_cb	lm70llp_cb;
195ca632f55SGrant Likely 
196ca632f55SGrant Likely 	if (lm70llp) {
19722de54a9SSudip Mukherjee 		pr_warn("spi_lm70llp instance already loaded. Aborting.\n");
198ca632f55SGrant Likely 		return;
199ca632f55SGrant Likely 	}
200ca632f55SGrant Likely 
201ca632f55SGrant Likely 	/* TODO:  this just _assumes_ a lm70 is there ... no probe;
202ca632f55SGrant Likely 	 * the lm70 driver could verify it, reading the manf ID.
203ca632f55SGrant Likely 	 */
204ca632f55SGrant Likely 
2057c5d1d97SYang Yingliang 	host = spi_alloc_host(p->physport->dev, sizeof(*pp));
2067c5d1d97SYang Yingliang 	if (!host) {
207ca632f55SGrant Likely 		status = -ENOMEM;
208ca632f55SGrant Likely 		goto out_fail;
209ca632f55SGrant Likely 	}
2107c5d1d97SYang Yingliang 	pp = spi_controller_get_devdata(host);
211ca632f55SGrant Likely 
212ca632f55SGrant Likely 	/*
213ca632f55SGrant Likely 	 * SPI and bitbang hookup.
214ca632f55SGrant Likely 	 */
21522592331SUwe Kleine-König 	pp->bitbang.ctlr = host;
216ca632f55SGrant Likely 	pp->bitbang.chipselect = lm70_chipselect;
217ca632f55SGrant Likely 	pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
218ca632f55SGrant Likely 	pp->bitbang.flags = SPI_3WIRE;
219ca632f55SGrant Likely 
220ca632f55SGrant Likely 	/*
221ca632f55SGrant Likely 	 * Parport hookup
222ca632f55SGrant Likely 	 */
223ca632f55SGrant Likely 	pp->port = p;
2242baed30cSSudip Mukherjee 	memset(&lm70llp_cb, 0, sizeof(lm70llp_cb));
2252baed30cSSudip Mukherjee 	lm70llp_cb.private = pp;
2262baed30cSSudip Mukherjee 	lm70llp_cb.flags = PARPORT_FLAG_EXCL;
2272baed30cSSudip Mukherjee 	pd = parport_register_dev_model(p, DRVNAME, &lm70llp_cb, 0);
2282baed30cSSudip Mukherjee 
229ca632f55SGrant Likely 	if (!pd) {
230ca632f55SGrant Likely 		status = -ENOMEM;
2317c5d1d97SYang Yingliang 		goto out_free_host;
232ca632f55SGrant Likely 	}
233ca632f55SGrant Likely 	pp->pd = pd;
234ca632f55SGrant Likely 
235ca632f55SGrant Likely 	status = parport_claim(pd);
236ca632f55SGrant Likely 	if (status < 0)
237ca632f55SGrant Likely 		goto out_parport_unreg;
238ca632f55SGrant Likely 
239ca632f55SGrant Likely 	/*
240ca632f55SGrant Likely 	 * Start SPI ...
241ca632f55SGrant Likely 	 */
242ca632f55SGrant Likely 	status = spi_bitbang_start(&pp->bitbang);
243ca632f55SGrant Likely 	if (status < 0) {
24446c52c28SSudip Mukherjee 		dev_warn(&pd->dev, "spi_bitbang_start failed with status %d\n",
24546c52c28SSudip Mukherjee 			 status);
246ca632f55SGrant Likely 		goto out_off_and_release;
247ca632f55SGrant Likely 	}
248ca632f55SGrant Likely 
249ca632f55SGrant Likely 	/*
250ca632f55SGrant Likely 	 * The modalias name MUST match the device_driver name
251ca632f55SGrant Likely 	 * for the bus glue code to match and subsequently bind them.
252ca632f55SGrant Likely 	 * We are binding to the generic drivers/hwmon/lm70.c device
253ca632f55SGrant Likely 	 * driver.
254ca632f55SGrant Likely 	 */
255ca632f55SGrant Likely 	strcpy(pp->info.modalias, "lm70");
256ca632f55SGrant Likely 	pp->info.max_speed_hz = 6 * 1000 * 1000;
257ca632f55SGrant Likely 	pp->info.chip_select = 0;
258ca632f55SGrant Likely 	pp->info.mode = SPI_3WIRE | SPI_MODE_0;
259ca632f55SGrant Likely 
260ca632f55SGrant Likely 	/* power up the chip, and let the LM70 control SI/SO */
261ca632f55SGrant Likely 	parport_write_data(pp->port, lm70_INIT);
262ca632f55SGrant Likely 
263ca632f55SGrant Likely 	/* Enable access to our primary data structure via
264ca632f55SGrant Likely 	 * the board info's (void *)controller_data.
265ca632f55SGrant Likely 	 */
266ca632f55SGrant Likely 	pp->info.controller_data = pp;
26722592331SUwe Kleine-König 	pp->spidev_lm70 = spi_new_device(pp->bitbang.ctlr, &pp->info);
268ca632f55SGrant Likely 	if (pp->spidev_lm70)
269ca632f55SGrant Likely 		dev_dbg(&pp->spidev_lm70->dev, "spidev_lm70 at %s\n",
270ca632f55SGrant Likely 			dev_name(&pp->spidev_lm70->dev));
271ca632f55SGrant Likely 	else {
27246c52c28SSudip Mukherjee 		dev_warn(&pd->dev, "spi_new_device failed\n");
273ca632f55SGrant Likely 		status = -ENODEV;
274ca632f55SGrant Likely 		goto out_bitbang_stop;
275ca632f55SGrant Likely 	}
276ca632f55SGrant Likely 	pp->spidev_lm70->bits_per_word = 8;
277ca632f55SGrant Likely 
278ca632f55SGrant Likely 	lm70llp = pp;
279ca632f55SGrant Likely 	return;
280ca632f55SGrant Likely 
281ca632f55SGrant Likely out_bitbang_stop:
282ca632f55SGrant Likely 	spi_bitbang_stop(&pp->bitbang);
283ca632f55SGrant Likely out_off_and_release:
284ca632f55SGrant Likely 	/* power down */
285ca632f55SGrant Likely 	parport_write_data(pp->port, 0);
286ca632f55SGrant Likely 	mdelay(10);
287ca632f55SGrant Likely 	parport_release(pp->pd);
288ca632f55SGrant Likely out_parport_unreg:
289ca632f55SGrant Likely 	parport_unregister_device(pd);
2907c5d1d97SYang Yingliang out_free_host:
2917c5d1d97SYang Yingliang 	spi_controller_put(host);
292ca632f55SGrant Likely out_fail:
29322de54a9SSudip Mukherjee 	pr_info("spi_lm70llp probe fail, status %d\n", status);
294ca632f55SGrant Likely }
295ca632f55SGrant Likely 
spi_lm70llp_detach(struct parport * p)296ca632f55SGrant Likely static void spi_lm70llp_detach(struct parport *p)
297ca632f55SGrant Likely {
298ca632f55SGrant Likely 	struct spi_lm70llp		*pp;
299ca632f55SGrant Likely 
300ca632f55SGrant Likely 	if (!lm70llp || lm70llp->port != p)
301ca632f55SGrant Likely 		return;
302ca632f55SGrant Likely 
303ca632f55SGrant Likely 	pp = lm70llp;
304ca632f55SGrant Likely 	spi_bitbang_stop(&pp->bitbang);
305ca632f55SGrant Likely 
306ca632f55SGrant Likely 	/* power down */
307ca632f55SGrant Likely 	parport_write_data(pp->port, 0);
308ca632f55SGrant Likely 
309ca632f55SGrant Likely 	parport_release(pp->pd);
310ca632f55SGrant Likely 	parport_unregister_device(pp->pd);
311ca632f55SGrant Likely 
31222592331SUwe Kleine-König 	spi_controller_put(pp->bitbang.ctlr);
313ca632f55SGrant Likely 
314ca632f55SGrant Likely 	lm70llp = NULL;
315ca632f55SGrant Likely }
316ca632f55SGrant Likely 
317ca632f55SGrant Likely static struct parport_driver spi_lm70llp_drv = {
318ca632f55SGrant Likely 	.name =		DRVNAME,
3192baed30cSSudip Mukherjee 	.match_port =	spi_lm70llp_attach,
320ca632f55SGrant Likely 	.detach =	spi_lm70llp_detach,
321ca632f55SGrant Likely };
322529bee9eSAndy Shevchenko module_parport_driver(spi_lm70llp_drv);
323ca632f55SGrant Likely 
324ca632f55SGrant Likely MODULE_AUTHOR("Kaiwan N Billimoria <kaiwan@designergraphix.com>");
325ca632f55SGrant Likely MODULE_DESCRIPTION(
326ca632f55SGrant Likely 	"Parport adapter for the National Semiconductor LM70 LLP eval board");
327ca632f55SGrant Likely MODULE_LICENSE("GPL");
328