blob: 4549efd792da682f9557d22855c1cf65656392f5 [file] [log] [blame]
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Driver for LM70EVAL-LLP board for the LM70 sensor
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -07003 *
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.
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070015 */
16
Sudip Mukherjee22de54a2015-12-07 16:17:35 +053017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070019#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/delay.h>
23#include <linux/device.h>
24#include <linux/parport.h>
25#include <linux/sysfs.h>
26#include <linux/workqueue.h>
27
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070028#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
30
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070031/*
32 * The LM70 communicates with a host processor using a 3-wire variant of
33 * the SPI/Microwire bus interface. This driver specifically supports an
34 * NS LM70 LLP Evaluation Board, interfacing to a PC using its parallel
35 * port to bitbang an SPI-parport bridge. Accordingly, this is an SPI
36 * master controller driver. The hwmon/lm70 driver is a "SPI protocol
37 * driver", layered on top of this one and usable without the lm70llp.
38 *
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +010039 * Datasheet and Schematic:
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070040 * The LM70 is a temperature sensor chip from National Semiconductor; its
41 * datasheet is available at http://www.national.com/pf/LM/LM70.html
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +010042 * The schematic for this particular board (the LM70EVAL-LLP) is
43 * available (on page 4) here:
44 * http://www.national.com/appinfo/tempsensors/files/LM70LLPEVALmanual.pdf
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070045 *
46 * Also see Documentation/spi/spi-lm70llp. The SPI<->parport code here is
47 * (heavily) based on spi-butterfly by David Brownell.
48 *
49 * The LM70 LLP connects to the PC parallel port in the following manner:
50 *
51 * Parallel LM70 LLP
52 * Port Direction JP2 Header
53 * ----------- --------- ------------
54 * D0 2 - -
55 * D1 3 --> V+ 5
56 * D2 4 --> V+ 5
57 * D3 5 --> V+ 5
58 * D4 6 --> V+ 5
59 * D5 7 --> nCS 8
60 * D6 8 --> SCLK 3
61 * D7 9 --> SI/O 5
62 * GND 25 - GND 7
63 * Select 13 <-- SI/O 1
64 *
65 * Note that parport pin 13 actually gets inverted by the transistor
66 * arrangement which lets either the parport or the LM70 drive the
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +010067 * SI/SO signal (see the schematic for details).
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070068 */
69
70#define DRVNAME "spi-lm70llp"
71
72#define lm70_INIT 0xBE
73#define SIO 0x10
74#define nCS 0x20
75#define SCLK 0x40
76
77/*-------------------------------------------------------------------------*/
78
79struct spi_lm70llp {
80 struct spi_bitbang bitbang;
81 struct parport *port;
82 struct pardevice *pd;
83 struct spi_device *spidev_lm70;
84 struct spi_board_info info;
Tony Jones49dce682007-10-16 01:27:48 -070085 //struct device *dev;
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070086};
87
88/* REVISIT : ugly global ; provides "exclusive open" facility */
89static struct spi_lm70llp *lm70llp;
90
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -070091/*-------------------------------------------------------------------*/
92
93static inline struct spi_lm70llp *spidev_to_pp(struct spi_device *spi)
94{
95 return spi->controller_data;
96}
97
98/*---------------------- LM70 LLP eval board-specific inlines follow */
99
100/* NOTE: we don't actually need to reread the output values, since they'll
101 * still be what we wrote before. Plus, going through parport builds in
102 * a ~1ms/operation delay; these SPI transfers could easily be faster.
103 */
104
105static inline void deassertCS(struct spi_lm70llp *pp)
106{
107 u8 data = parport_read_data(pp->port);
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +0100108
109 data &= ~0x80; /* pull D7/SI-out low while de-asserted */
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700110 parport_write_data(pp->port, data | nCS);
111}
112
113static inline void assertCS(struct spi_lm70llp *pp)
114{
115 u8 data = parport_read_data(pp->port);
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +0100116
117 data |= 0x80; /* pull D7/SI-out high so lm70 drives SO-in */
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700118 parport_write_data(pp->port, data & ~nCS);
119}
120
121static inline void clkHigh(struct spi_lm70llp *pp)
122{
123 u8 data = parport_read_data(pp->port);
Sudip Mukherjee832329d2015-12-03 18:29:54 +0530124
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700125 parport_write_data(pp->port, data | SCLK);
126}
127
128static inline void clkLow(struct spi_lm70llp *pp)
129{
130 u8 data = parport_read_data(pp->port);
Sudip Mukherjee832329d2015-12-03 18:29:54 +0530131
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700132 parport_write_data(pp->port, data & ~SCLK);
133}
134
135/*------------------------- SPI-LM70-specific inlines ----------------------*/
136
137static inline void spidelay(unsigned d)
138{
139 udelay(d);
140}
141
142static inline void setsck(struct spi_device *s, int is_on)
143{
144 struct spi_lm70llp *pp = spidev_to_pp(s);
145
146 if (is_on)
147 clkHigh(pp);
148 else
149 clkLow(pp);
150}
151
152static inline void setmosi(struct spi_device *s, int is_on)
153{
154 /* FIXME update D7 ... this way we can put the chip
155 * into shutdown mode and read the manufacturer ID,
156 * but we can't put it back into operational mode.
157 */
158}
159
160/*
161 * getmiso:
162 * Why do we return 0 when the SIO line is high and vice-versa?
163 * The fact is, the lm70 eval board from NS (which this driver drives),
164 * is wired in just such a way : when the lm70's SIO goes high, a transistor
165 * switches it to low reflecting this on the parport (pin 13), and vice-versa.
166 */
167static inline int getmiso(struct spi_device *s)
168{
169 struct spi_lm70llp *pp = spidev_to_pp(s);
Sudip Mukherjee832329d2015-12-03 18:29:54 +0530170
Sudip Mukherjee74bdced2015-12-03 18:29:57 +0530171 return ((SIO == (parport_read_status(pp->port) & SIO)) ? 0 : 1);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700172}
Sudip Mukherjee832329d2015-12-03 18:29:54 +0530173
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700174/*--------------------------------------------------------------------*/
175
Grant Likelyca632f52011-06-06 01:16:30 -0600176#include "spi-bitbang-txrx.h"
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700177
178static void lm70_chipselect(struct spi_device *spi, int value)
179{
180 struct spi_lm70llp *pp = spidev_to_pp(spi);
181
182 if (value)
183 assertCS(pp);
184 else
185 deassertCS(pp);
186}
187
188/*
189 * Our actual bitbanger routine.
190 */
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200191static u32 lm70_txrx(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits,
192 unsigned flags)
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700193{
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200194 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700195}
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;
Sudip Mukherjee2baed302015-12-03 18:29:59 +0530203 struct pardev_cb lm70llp_cb;
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700204
205 if (lm70llp) {
Sudip Mukherjee22de54a2015-12-07 16:17:35 +0530206 pr_warn("spi_lm70llp instance already loaded. Aborting.\n");
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700207 return;
208 }
209
210 /* TODO: this just _assumes_ a lm70 is there ... no probe;
211 * the lm70 driver could verify it, reading the manf ID.
212 */
213
214 master = spi_alloc_master(p->physport->dev, sizeof *pp);
215 if (!master) {
216 status = -ENOMEM;
217 goto out_fail;
218 }
219 pp = spi_master_get_devdata(master);
220
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700221 /*
222 * SPI and bitbang hookup.
223 */
Axel Lin94c69f72013-09-10 15:43:41 +0800224 pp->bitbang.master = master;
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700225 pp->bitbang.chipselect = lm70_chipselect;
226 pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
227 pp->bitbang.flags = SPI_3WIRE;
228
229 /*
230 * Parport hookup
231 */
232 pp->port = p;
Sudip Mukherjee2baed302015-12-03 18:29:59 +0530233 memset(&lm70llp_cb, 0, sizeof(lm70llp_cb));
234 lm70llp_cb.private = pp;
235 lm70llp_cb.flags = PARPORT_FLAG_EXCL;
236 pd = parport_register_dev_model(p, DRVNAME, &lm70llp_cb, 0);
237
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700238 if (!pd) {
239 status = -ENOMEM;
240 goto out_free_master;
241 }
242 pp->pd = pd;
243
244 status = parport_claim(pd);
245 if (status < 0)
246 goto out_parport_unreg;
247
248 /*
249 * Start SPI ...
250 */
251 status = spi_bitbang_start(&pp->bitbang);
252 if (status < 0) {
Sudip Mukherjee46c52c22015-12-07 16:17:34 +0530253 dev_warn(&pd->dev, "spi_bitbang_start failed with status %d\n",
254 status);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700255 goto out_off_and_release;
256 }
257
258 /*
259 * The modalias name MUST match the device_driver name
260 * for the bus glue code to match and subsequently bind them.
261 * We are binding to the generic drivers/hwmon/lm70.c device
262 * driver.
263 */
264 strcpy(pp->info.modalias, "lm70");
265 pp->info.max_speed_hz = 6 * 1000 * 1000;
266 pp->info.chip_select = 0;
267 pp->info.mode = SPI_3WIRE | SPI_MODE_0;
268
269 /* power up the chip, and let the LM70 control SI/SO */
270 parport_write_data(pp->port, lm70_INIT);
271
272 /* Enable access to our primary data structure via
273 * the board info's (void *)controller_data.
274 */
275 pp->info.controller_data = pp;
276 pp->spidev_lm70 = spi_new_device(pp->bitbang.master, &pp->info);
277 if (pp->spidev_lm70)
278 dev_dbg(&pp->spidev_lm70->dev, "spidev_lm70 at %s\n",
Sudip Mukherjee44dc0fd2015-12-03 18:29:56 +0530279 dev_name(&pp->spidev_lm70->dev));
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700280 else {
Sudip Mukherjee46c52c22015-12-07 16:17:34 +0530281 dev_warn(&pd->dev, "spi_new_device failed\n");
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700282 status = -ENODEV;
283 goto out_bitbang_stop;
284 }
Kaiwan N Billimoria2b730052009-01-07 16:37:34 +0100285 pp->spidev_lm70->bits_per_word = 8;
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700286
287 lm70llp = pp;
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700288 return;
289
290out_bitbang_stop:
291 spi_bitbang_stop(&pp->bitbang);
292out_off_and_release:
293 /* power down */
294 parport_write_data(pp->port, 0);
295 mdelay(10);
296 parport_release(pp->pd);
297out_parport_unreg:
298 parport_unregister_device(pd);
299out_free_master:
Sudip Mukherjee25fb1a42015-12-03 18:29:55 +0530300 spi_master_put(master);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700301out_fail:
Sudip Mukherjee22de54a2015-12-07 16:17:35 +0530302 pr_info("spi_lm70llp probe fail, status %d\n", status);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700303}
304
305static void spi_lm70llp_detach(struct parport *p)
306{
307 struct spi_lm70llp *pp;
308
309 if (!lm70llp || lm70llp->port != p)
310 return;
311
312 pp = lm70llp;
313 spi_bitbang_stop(&pp->bitbang);
314
315 /* power down */
316 parport_write_data(pp->port, 0);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700317
318 parport_release(pp->pd);
319 parport_unregister_device(pp->pd);
320
Sudip Mukherjee25fb1a42015-12-03 18:29:55 +0530321 spi_master_put(pp->bitbang.master);
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700322
323 lm70llp = NULL;
324}
325
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700326static struct parport_driver spi_lm70llp_drv = {
327 .name = DRVNAME,
Sudip Mukherjee2baed302015-12-03 18:29:59 +0530328 .match_port = spi_lm70llp_attach,
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700329 .detach = spi_lm70llp_detach,
Sudip Mukherjee2baed302015-12-03 18:29:59 +0530330 .devmodel = true,
Kaiwan N Billimoria78961a52007-07-17 04:04:05 -0700331};
332
333static int __init init_spi_lm70llp(void)
334{
335 return parport_register_driver(&spi_lm70llp_drv);
336}
337module_init(init_spi_lm70llp);
338
339static void __exit cleanup_spi_lm70llp(void)
340{
341 parport_unregister_driver(&spi_lm70llp_drv);
342}
343module_exit(cleanup_spi_lm70llp);
344
345MODULE_AUTHOR("Kaiwan N Billimoria <kaiwan@designergraphix.com>");
346MODULE_DESCRIPTION(
347 "Parport adapter for the National Semiconductor LM70 LLP eval board");
348MODULE_LICENSE("GPL");