1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#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
28#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
85
86};
87
88
89static struct spi_lm70llp *lm70llp;
90
91
92
93static inline struct spi_lm70llp *spidev_to_pp(struct spi_device *spi)
94{
95 return spi->controller_data;
96}
97
98
99
100
101
102
103
104
105static inline void deassertCS(struct spi_lm70llp *pp)
106{
107 u8 data = parport_read_data(pp->port);
108
109 data &= ~0x80;
110 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);
116
117 data |= 0x80;
118 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);
124
125 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);
131
132 parport_write_data(pp->port, data & ~SCLK);
133}
134
135
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
155
156
157
158}
159
160
161
162
163
164
165
166
167static inline int getmiso(struct spi_device *s)
168{
169 struct spi_lm70llp *pp = spidev_to_pp(s);
170
171 return ((SIO == (parport_read_status(pp->port) & SIO)) ? 0 : 1);
172}
173
174
175
176#include "spi-bitbang-txrx.h"
177
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
190
191static u32 lm70_txrx(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits,
192 unsigned flags)
193{
194 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, 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 struct pardev_cb lm70llp_cb;
204
205 if (lm70llp) {
206 pr_warn("spi_lm70llp instance already loaded. Aborting.\n");
207 return;
208 }
209
210
211
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
221
222
223
224 pp->bitbang.master = master;
225 pp->bitbang.chipselect = lm70_chipselect;
226 pp->bitbang.txrx_word[SPI_MODE_0] = lm70_txrx;
227 pp->bitbang.flags = SPI_3WIRE;
228
229
230
231
232 pp->port = p;
233 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
238 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
250
251 status = spi_bitbang_start(&pp->bitbang);
252 if (status < 0) {
253 dev_warn(&pd->dev, "spi_bitbang_start failed with status %d\n",
254 status);
255 goto out_off_and_release;
256 }
257
258
259
260
261
262
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
270 parport_write_data(pp->port, lm70_INIT);
271
272
273
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",
279 dev_name(&pp->spidev_lm70->dev));
280 else {
281 dev_warn(&pd->dev, "spi_new_device failed\n");
282 status = -ENODEV;
283 goto out_bitbang_stop;
284 }
285 pp->spidev_lm70->bits_per_word = 8;
286
287 lm70llp = pp;
288 return;
289
290out_bitbang_stop:
291 spi_bitbang_stop(&pp->bitbang);
292out_off_and_release:
293
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:
300 spi_master_put(master);
301out_fail:
302 pr_info("spi_lm70llp probe fail, status %d\n", status);
303}
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
316 parport_write_data(pp->port, 0);
317
318 parport_release(pp->pd);
319 parport_unregister_device(pp->pd);
320
321 spi_master_put(pp->bitbang.master);
322
323 lm70llp = NULL;
324}
325
326static struct parport_driver spi_lm70llp_drv = {
327 .name = DRVNAME,
328 .match_port = spi_lm70llp_attach,
329 .detach = spi_lm70llp_detach,
330 .devmodel = true,
331};
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");
349