1 /*
2 * as3935.c - Support for AS3935 Franklin lightning sensor
3 *
4 * Copyright (C) 2014 Matt Ranostay <mranostay@gmail.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 */
17
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/mutex.h>
24 #include <linux/err.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/spi/spi.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/buffer.h>
33 #include <linux/iio/triggered_buffer.h>
34 #include <linux/of_gpio.h>
35
36
37 #define AS3935_AFE_GAIN 0x00
38 #define AS3935_AFE_MASK 0x3F
39 #define AS3935_AFE_GAIN_MAX 0x1F
40 #define AS3935_AFE_PWR_BIT BIT(0)
41
42 #define AS3935_INT 0x03
43 #define AS3935_INT_MASK 0x0f
44 #define AS3935_EVENT_INT BIT(3)
45 #define AS3935_NOISE_INT BIT(0)
46
47 #define AS3935_DATA 0x07
48 #define AS3935_DATA_MASK 0x3F
49
50 #define AS3935_TUNE_CAP 0x08
51 #define AS3935_CALIBRATE 0x3D
52
53 #define AS3935_READ_DATA BIT(14)
54 #define AS3935_ADDRESS(x) ((x) << 8)
55
56 #define MAX_PF_CAP 120
57 #define TUNE_CAP_DIV 8
58
59 struct as3935_state {
60 struct spi_device *spi;
61 struct iio_trigger *trig;
62 struct mutex lock;
63 struct delayed_work work;
64
65 u32 tune_cap;
66 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
67 u8 buf[2] ____cacheline_aligned;
68 };
69
70 static const struct iio_chan_spec as3935_channels[] = {
71 {
72 .type = IIO_PROXIMITY,
73 .info_mask_separate =
74 BIT(IIO_CHAN_INFO_RAW) |
75 BIT(IIO_CHAN_INFO_PROCESSED) |
76 BIT(IIO_CHAN_INFO_SCALE),
77 .scan_index = 0,
78 .scan_type = {
79 .sign = 'u',
80 .realbits = 6,
81 .storagebits = 8,
82 },
83 },
84 IIO_CHAN_SOFT_TIMESTAMP(1),
85 };
86
as3935_read(struct as3935_state * st,unsigned int reg,int * val)87 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
88 {
89 u8 cmd;
90 int ret;
91
92 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
93 ret = spi_w8r8(st->spi, cmd);
94 if (ret < 0)
95 return ret;
96 *val = ret;
97
98 return 0;
99 }
100
as3935_write(struct as3935_state * st,unsigned int reg,unsigned int val)101 static int as3935_write(struct as3935_state *st,
102 unsigned int reg,
103 unsigned int val)
104 {
105 u8 *buf = st->buf;
106
107 buf[0] = AS3935_ADDRESS(reg) >> 8;
108 buf[1] = val;
109
110 return spi_write(st->spi, buf, 2);
111 }
112
as3935_sensor_sensitivity_show(struct device * dev,struct device_attribute * attr,char * buf)113 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
114 struct device_attribute *attr,
115 char *buf)
116 {
117 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
118 int val, ret;
119
120 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
121 if (ret)
122 return ret;
123 val = (val & AS3935_AFE_MASK) >> 1;
124
125 return sprintf(buf, "%d\n", val);
126 }
127
as3935_sensor_sensitivity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)128 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t len)
131 {
132 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
133 unsigned long val;
134 int ret;
135
136 ret = kstrtoul((const char *) buf, 10, &val);
137 if (ret)
138 return -EINVAL;
139
140 if (val > AS3935_AFE_GAIN_MAX)
141 return -EINVAL;
142
143 as3935_write(st, AS3935_AFE_GAIN, val << 1);
144
145 return len;
146 }
147
148 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
149 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
150
151
152 static struct attribute *as3935_attributes[] = {
153 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
154 NULL,
155 };
156
157 static struct attribute_group as3935_attribute_group = {
158 .attrs = as3935_attributes,
159 };
160
as3935_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)161 static int as3935_read_raw(struct iio_dev *indio_dev,
162 struct iio_chan_spec const *chan,
163 int *val,
164 int *val2,
165 long m)
166 {
167 struct as3935_state *st = iio_priv(indio_dev);
168 int ret;
169
170
171 switch (m) {
172 case IIO_CHAN_INFO_PROCESSED:
173 case IIO_CHAN_INFO_RAW:
174 *val2 = 0;
175 ret = as3935_read(st, AS3935_DATA, val);
176 if (ret)
177 return ret;
178
179 if (m == IIO_CHAN_INFO_RAW)
180 return IIO_VAL_INT;
181
182 /* storm out of range */
183 if (*val == AS3935_DATA_MASK)
184 return -EINVAL;
185
186 if (m == IIO_CHAN_INFO_PROCESSED)
187 *val *= 1000;
188 break;
189 case IIO_CHAN_INFO_SCALE:
190 *val = 1000;
191 break;
192 default:
193 return -EINVAL;
194 }
195
196 return IIO_VAL_INT;
197 }
198
199 static const struct iio_info as3935_info = {
200 .driver_module = THIS_MODULE,
201 .attrs = &as3935_attribute_group,
202 .read_raw = &as3935_read_raw,
203 };
204
as3935_trigger_handler(int irq,void * private)205 static irqreturn_t as3935_trigger_handler(int irq, void *private)
206 {
207 struct iio_poll_func *pf = private;
208 struct iio_dev *indio_dev = pf->indio_dev;
209 struct as3935_state *st = iio_priv(indio_dev);
210 int val, ret;
211
212 ret = as3935_read(st, AS3935_DATA, &val);
213 if (ret)
214 goto err_read;
215
216 st->buffer[0] = val & AS3935_DATA_MASK;
217 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
218 pf->timestamp);
219 err_read:
220 iio_trigger_notify_done(indio_dev->trig);
221
222 return IRQ_HANDLED;
223 }
224
225 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
226 .owner = THIS_MODULE,
227 };
228
as3935_event_work(struct work_struct * work)229 static void as3935_event_work(struct work_struct *work)
230 {
231 struct as3935_state *st;
232 int val;
233
234 st = container_of(work, struct as3935_state, work.work);
235
236 as3935_read(st, AS3935_INT, &val);
237 val &= AS3935_INT_MASK;
238
239 switch (val) {
240 case AS3935_EVENT_INT:
241 iio_trigger_poll(st->trig);
242 break;
243 case AS3935_NOISE_INT:
244 dev_warn(&st->spi->dev, "noise level is too high");
245 break;
246 }
247 }
248
as3935_interrupt_handler(int irq,void * private)249 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
250 {
251 struct iio_dev *indio_dev = private;
252 struct as3935_state *st = iio_priv(indio_dev);
253
254 /*
255 * Delay work for >2 milliseconds after an interrupt to allow
256 * estimated distance to recalculated.
257 */
258
259 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
260
261 return IRQ_HANDLED;
262 }
263
calibrate_as3935(struct as3935_state * st)264 static void calibrate_as3935(struct as3935_state *st)
265 {
266 /* mask disturber interrupt bit */
267 as3935_write(st, AS3935_INT, BIT(5));
268
269 as3935_write(st, AS3935_CALIBRATE, 0x96);
270 as3935_write(st, AS3935_TUNE_CAP,
271 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
272
273 mdelay(2);
274 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
275 }
276
277 #ifdef CONFIG_PM_SLEEP
as3935_suspend(struct device * dev)278 static int as3935_suspend(struct device *dev)
279 {
280 struct iio_dev *indio_dev = dev_get_drvdata(dev);
281 struct as3935_state *st = iio_priv(indio_dev);
282 int val, ret;
283
284 mutex_lock(&st->lock);
285 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
286 if (ret)
287 goto err_suspend;
288 val |= AS3935_AFE_PWR_BIT;
289
290 ret = as3935_write(st, AS3935_AFE_GAIN, val);
291
292 err_suspend:
293 mutex_unlock(&st->lock);
294
295 return ret;
296 }
297
as3935_resume(struct device * dev)298 static int as3935_resume(struct device *dev)
299 {
300 struct iio_dev *indio_dev = dev_get_drvdata(dev);
301 struct as3935_state *st = iio_priv(indio_dev);
302 int val, ret;
303
304 mutex_lock(&st->lock);
305 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
306 if (ret)
307 goto err_resume;
308 val &= ~AS3935_AFE_PWR_BIT;
309 ret = as3935_write(st, AS3935_AFE_GAIN, val);
310
311 calibrate_as3935(st);
312
313 err_resume:
314 mutex_unlock(&st->lock);
315
316 return ret;
317 }
318
319 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
320 #define AS3935_PM_OPS (&as3935_pm_ops)
321
322 #else
323 #define AS3935_PM_OPS NULL
324 #endif
325
as3935_probe(struct spi_device * spi)326 static int as3935_probe(struct spi_device *spi)
327 {
328 struct iio_dev *indio_dev;
329 struct iio_trigger *trig;
330 struct as3935_state *st;
331 struct device_node *np = spi->dev.of_node;
332 int ret;
333
334 /* Be sure lightning event interrupt is specified */
335 if (!spi->irq) {
336 dev_err(&spi->dev, "unable to get event interrupt\n");
337 return -EINVAL;
338 }
339
340 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
341 if (!indio_dev)
342 return -ENOMEM;
343
344 st = iio_priv(indio_dev);
345 st->spi = spi;
346 st->tune_cap = 0;
347
348 spi_set_drvdata(spi, indio_dev);
349 mutex_init(&st->lock);
350 INIT_DELAYED_WORK(&st->work, as3935_event_work);
351
352 ret = of_property_read_u32(np,
353 "ams,tuning-capacitor-pf", &st->tune_cap);
354 if (ret) {
355 st->tune_cap = 0;
356 dev_warn(&spi->dev,
357 "no tuning-capacitor-pf set, defaulting to %d",
358 st->tune_cap);
359 }
360
361 if (st->tune_cap > MAX_PF_CAP) {
362 dev_err(&spi->dev,
363 "wrong tuning-capacitor-pf setting of %d\n",
364 st->tune_cap);
365 return -EINVAL;
366 }
367
368 indio_dev->dev.parent = &spi->dev;
369 indio_dev->name = spi_get_device_id(spi)->name;
370 indio_dev->channels = as3935_channels;
371 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
372 indio_dev->modes = INDIO_DIRECT_MODE;
373 indio_dev->info = &as3935_info;
374
375 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
376 indio_dev->name, indio_dev->id);
377
378 if (!trig)
379 return -ENOMEM;
380
381 st->trig = trig;
382 trig->dev.parent = indio_dev->dev.parent;
383 iio_trigger_set_drvdata(trig, indio_dev);
384 trig->ops = &iio_interrupt_trigger_ops;
385
386 ret = iio_trigger_register(trig);
387 if (ret) {
388 dev_err(&spi->dev, "failed to register trigger\n");
389 return ret;
390 }
391
392 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
393 &as3935_trigger_handler, NULL);
394
395 if (ret) {
396 dev_err(&spi->dev, "cannot setup iio trigger\n");
397 goto unregister_trigger;
398 }
399
400 calibrate_as3935(st);
401
402 ret = devm_request_irq(&spi->dev, spi->irq,
403 &as3935_interrupt_handler,
404 IRQF_TRIGGER_RISING,
405 dev_name(&spi->dev),
406 indio_dev);
407
408 if (ret) {
409 dev_err(&spi->dev, "unable to request irq\n");
410 goto unregister_buffer;
411 }
412
413 ret = iio_device_register(indio_dev);
414 if (ret < 0) {
415 dev_err(&spi->dev, "unable to register device\n");
416 goto unregister_buffer;
417 }
418 return 0;
419
420 unregister_buffer:
421 iio_triggered_buffer_cleanup(indio_dev);
422
423 unregister_trigger:
424 iio_trigger_unregister(st->trig);
425
426 return ret;
427 }
428
as3935_remove(struct spi_device * spi)429 static int as3935_remove(struct spi_device *spi)
430 {
431 struct iio_dev *indio_dev = spi_get_drvdata(spi);
432 struct as3935_state *st = iio_priv(indio_dev);
433
434 iio_device_unregister(indio_dev);
435 iio_triggered_buffer_cleanup(indio_dev);
436 iio_trigger_unregister(st->trig);
437
438 return 0;
439 }
440
441 static const struct of_device_id as3935_of_match[] = {
442 { .compatible = "ams,as3935", },
443 { /* sentinel */ },
444 };
445 MODULE_DEVICE_TABLE(of, as3935_of_match);
446
447 static const struct spi_device_id as3935_id[] = {
448 {"as3935", 0},
449 {},
450 };
451 MODULE_DEVICE_TABLE(spi, as3935_id);
452
453 static struct spi_driver as3935_driver = {
454 .driver = {
455 .name = "as3935",
456 .of_match_table = of_match_ptr(as3935_of_match),
457 .pm = AS3935_PM_OPS,
458 },
459 .probe = as3935_probe,
460 .remove = as3935_remove,
461 .id_table = as3935_id,
462 };
463 module_spi_driver(as3935_driver);
464
465 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
466 MODULE_DESCRIPTION("AS3935 lightning sensor");
467 MODULE_LICENSE("GPL");
468 MODULE_ALIAS("spi:as3935");
469