1 // SPDX-License-Identifier: GPL-2.0+
3 * as3935.c - Support for AS3935 Franklin lightning sensor
5 * Copyright (C) 2014, 2017-2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/workqueue.h>
14 #include <linux/mutex.h>
15 #include <linux/err.h>
16 #include <linux/irq.h>
17 #include <linux/gpio.h>
18 #include <linux/spi/spi.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/of_gpio.h>
28 #define AS3935_AFE_GAIN 0x00
29 #define AS3935_AFE_MASK 0x3F
30 #define AS3935_AFE_GAIN_MAX 0x1F
31 #define AS3935_AFE_PWR_BIT BIT(0)
33 #define AS3935_NFLWDTH 0x01
34 #define AS3935_NFLWDTH_MASK 0x7f
36 #define AS3935_INT 0x03
37 #define AS3935_INT_MASK 0x0f
38 #define AS3935_DISTURB_INT BIT(2)
39 #define AS3935_EVENT_INT BIT(3)
40 #define AS3935_NOISE_INT BIT(0)
42 #define AS3935_DATA 0x07
43 #define AS3935_DATA_MASK 0x3F
45 #define AS3935_TUNE_CAP 0x08
46 #define AS3935_DEFAULTS 0x3C
47 #define AS3935_CALIBRATE 0x3D
49 #define AS3935_READ_DATA BIT(14)
50 #define AS3935_ADDRESS(x) ((x) << 8)
52 #define MAX_PF_CAP 120
53 #define TUNE_CAP_DIV 8
56 struct spi_device *spi;
57 struct iio_trigger *trig;
59 struct delayed_work work;
61 unsigned long noise_tripped;
64 /* Ensure timestamp is naturally aligned */
67 s64 timestamp __aligned(8);
69 u8 buf[2] ____cacheline_aligned;
72 static const struct iio_chan_spec as3935_channels[] = {
74 .type = IIO_PROXIMITY,
76 BIT(IIO_CHAN_INFO_RAW) |
77 BIT(IIO_CHAN_INFO_PROCESSED) |
78 BIT(IIO_CHAN_INFO_SCALE),
86 IIO_CHAN_SOFT_TIMESTAMP(1),
89 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
94 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
95 ret = spi_w8r8(st->spi, cmd);
103 static int as3935_write(struct as3935_state *st,
109 buf[0] = AS3935_ADDRESS(reg) >> 8;
112 return spi_write(st->spi, buf, 2);
115 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
116 struct device_attribute *attr,
119 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
122 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
125 val = (val & AS3935_AFE_MASK) >> 1;
127 return sprintf(buf, "%d\n", val);
130 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t len)
134 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
138 ret = kstrtoul((const char *) buf, 10, &val);
142 if (val > AS3935_AFE_GAIN_MAX)
145 as3935_write(st, AS3935_AFE_GAIN, val << 1);
150 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
151 struct device_attribute *attr,
154 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
157 mutex_lock(&st->lock);
158 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
159 mutex_unlock(&st->lock);
164 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
165 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
167 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
168 as3935_noise_level_tripped_show, NULL, 0);
170 static struct attribute *as3935_attributes[] = {
171 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
172 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
176 static const struct attribute_group as3935_attribute_group = {
177 .attrs = as3935_attributes,
180 static int as3935_read_raw(struct iio_dev *indio_dev,
181 struct iio_chan_spec const *chan,
186 struct as3935_state *st = iio_priv(indio_dev);
191 case IIO_CHAN_INFO_PROCESSED:
192 case IIO_CHAN_INFO_RAW:
194 ret = as3935_read(st, AS3935_DATA, val);
198 /* storm out of range */
199 if (*val == AS3935_DATA_MASK)
202 if (m == IIO_CHAN_INFO_RAW)
205 if (m == IIO_CHAN_INFO_PROCESSED)
208 case IIO_CHAN_INFO_SCALE:
218 static const struct iio_info as3935_info = {
219 .attrs = &as3935_attribute_group,
220 .read_raw = &as3935_read_raw,
223 static irqreturn_t as3935_trigger_handler(int irq, void *private)
225 struct iio_poll_func *pf = private;
226 struct iio_dev *indio_dev = pf->indio_dev;
227 struct as3935_state *st = iio_priv(indio_dev);
230 ret = as3935_read(st, AS3935_DATA, &val);
234 st->scan.chan = val & AS3935_DATA_MASK;
235 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan,
236 iio_get_time_ns(indio_dev));
238 iio_trigger_notify_done(indio_dev->trig);
243 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
246 static void as3935_event_work(struct work_struct *work)
248 struct as3935_state *st;
252 st = container_of(work, struct as3935_state, work.work);
254 ret = as3935_read(st, AS3935_INT, &val);
256 dev_warn(&st->spi->dev, "read error\n");
260 val &= AS3935_INT_MASK;
263 case AS3935_EVENT_INT:
264 iio_trigger_poll_chained(st->trig);
266 case AS3935_DISTURB_INT:
267 case AS3935_NOISE_INT:
268 mutex_lock(&st->lock);
269 st->noise_tripped = jiffies;
270 mutex_unlock(&st->lock);
271 dev_warn(&st->spi->dev, "noise level is too high\n");
276 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
278 struct iio_dev *indio_dev = private;
279 struct as3935_state *st = iio_priv(indio_dev);
282 * Delay work for >2 milliseconds after an interrupt to allow
283 * estimated distance to recalculated.
286 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
291 static void calibrate_as3935(struct as3935_state *st)
293 as3935_write(st, AS3935_DEFAULTS, 0x96);
294 as3935_write(st, AS3935_CALIBRATE, 0x96);
295 as3935_write(st, AS3935_TUNE_CAP,
296 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
299 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
300 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
303 #ifdef CONFIG_PM_SLEEP
304 static int as3935_suspend(struct device *dev)
306 struct iio_dev *indio_dev = dev_get_drvdata(dev);
307 struct as3935_state *st = iio_priv(indio_dev);
310 mutex_lock(&st->lock);
311 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
314 val |= AS3935_AFE_PWR_BIT;
316 ret = as3935_write(st, AS3935_AFE_GAIN, val);
319 mutex_unlock(&st->lock);
324 static int as3935_resume(struct device *dev)
326 struct iio_dev *indio_dev = dev_get_drvdata(dev);
327 struct as3935_state *st = iio_priv(indio_dev);
330 mutex_lock(&st->lock);
331 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
334 val &= ~AS3935_AFE_PWR_BIT;
335 ret = as3935_write(st, AS3935_AFE_GAIN, val);
337 calibrate_as3935(st);
340 mutex_unlock(&st->lock);
345 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
346 #define AS3935_PM_OPS (&as3935_pm_ops)
349 #define AS3935_PM_OPS NULL
352 static int as3935_probe(struct spi_device *spi)
354 struct iio_dev *indio_dev;
355 struct iio_trigger *trig;
356 struct as3935_state *st;
357 struct device_node *np = spi->dev.of_node;
360 /* Be sure lightning event interrupt is specified */
362 dev_err(&spi->dev, "unable to get event interrupt\n");
366 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
370 st = iio_priv(indio_dev);
373 spi_set_drvdata(spi, indio_dev);
374 mutex_init(&st->lock);
375 INIT_DELAYED_WORK(&st->work, as3935_event_work);
377 ret = of_property_read_u32(np,
378 "ams,tuning-capacitor-pf", &st->tune_cap);
382 "no tuning-capacitor-pf set, defaulting to %d",
386 if (st->tune_cap > MAX_PF_CAP) {
388 "wrong tuning-capacitor-pf setting of %d\n",
393 ret = of_property_read_u32(np,
394 "ams,nflwdth", &st->nflwdth_reg);
395 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
397 "invalid nflwdth setting of %d\n",
402 indio_dev->dev.parent = &spi->dev;
403 indio_dev->name = spi_get_device_id(spi)->name;
404 indio_dev->channels = as3935_channels;
405 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
406 indio_dev->modes = INDIO_DIRECT_MODE;
407 indio_dev->info = &as3935_info;
409 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
410 indio_dev->name, indio_dev->id);
416 st->noise_tripped = jiffies - HZ;
417 trig->dev.parent = indio_dev->dev.parent;
418 iio_trigger_set_drvdata(trig, indio_dev);
419 trig->ops = &iio_interrupt_trigger_ops;
421 ret = iio_trigger_register(trig);
423 dev_err(&spi->dev, "failed to register trigger\n");
427 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
428 &as3935_trigger_handler, NULL);
431 dev_err(&spi->dev, "cannot setup iio trigger\n");
432 goto unregister_trigger;
435 calibrate_as3935(st);
437 ret = devm_request_irq(&spi->dev, spi->irq,
438 &as3935_interrupt_handler,
444 dev_err(&spi->dev, "unable to request irq\n");
445 goto unregister_buffer;
448 ret = iio_device_register(indio_dev);
450 dev_err(&spi->dev, "unable to register device\n");
451 goto unregister_buffer;
456 iio_triggered_buffer_cleanup(indio_dev);
459 iio_trigger_unregister(st->trig);
464 static int as3935_remove(struct spi_device *spi)
466 struct iio_dev *indio_dev = spi_get_drvdata(spi);
467 struct as3935_state *st = iio_priv(indio_dev);
469 iio_device_unregister(indio_dev);
470 iio_triggered_buffer_cleanup(indio_dev);
471 iio_trigger_unregister(st->trig);
476 static const struct of_device_id as3935_of_match[] = {
477 { .compatible = "ams,as3935", },
480 MODULE_DEVICE_TABLE(of, as3935_of_match);
482 static const struct spi_device_id as3935_id[] = {
486 MODULE_DEVICE_TABLE(spi, as3935_id);
488 static struct spi_driver as3935_driver = {
491 .of_match_table = of_match_ptr(as3935_of_match),
494 .probe = as3935_probe,
495 .remove = as3935_remove,
496 .id_table = as3935_id,
498 module_spi_driver(as3935_driver);
500 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
501 MODULE_DESCRIPTION("AS3935 lightning sensor");
502 MODULE_LICENSE("GPL");