Linux Audio

Check our new training course

Linux BSP development engineering services

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