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/mod_devicetable.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/workqueue.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/irq.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>
26 #define AS3935_AFE_GAIN 0x00
27 #define AS3935_AFE_MASK 0x3F
28 #define AS3935_AFE_GAIN_MAX 0x1F
29 #define AS3935_AFE_PWR_BIT BIT(0)
31 #define AS3935_NFLWDTH 0x01
32 #define AS3935_NFLWDTH_MASK 0x7f
34 #define AS3935_INT 0x03
35 #define AS3935_INT_MASK 0x0f
36 #define AS3935_DISTURB_INT BIT(2)
37 #define AS3935_EVENT_INT BIT(3)
38 #define AS3935_NOISE_INT BIT(0)
40 #define AS3935_DATA 0x07
41 #define AS3935_DATA_MASK 0x3F
43 #define AS3935_TUNE_CAP 0x08
44 #define AS3935_DEFAULTS 0x3C
45 #define AS3935_CALIBRATE 0x3D
47 #define AS3935_READ_DATA BIT(14)
48 #define AS3935_ADDRESS(x) ((x) << 8)
50 #define MAX_PF_CAP 120
51 #define TUNE_CAP_DIV 8
54 struct spi_device *spi;
55 struct iio_trigger *trig;
57 struct delayed_work work;
59 unsigned long noise_tripped;
62 /* Ensure timestamp is naturally aligned */
65 s64 timestamp __aligned(8);
67 u8 buf[2] ____cacheline_aligned;
70 static const struct iio_chan_spec as3935_channels[] = {
72 .type = IIO_PROXIMITY,
74 BIT(IIO_CHAN_INFO_RAW) |
75 BIT(IIO_CHAN_INFO_PROCESSED) |
76 BIT(IIO_CHAN_INFO_SCALE),
84 IIO_CHAN_SOFT_TIMESTAMP(1),
87 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
92 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
93 ret = spi_w8r8(st->spi, cmd);
101 static int as3935_write(struct as3935_state *st,
107 buf[0] = AS3935_ADDRESS(reg) >> 8;
110 return spi_write(st->spi, buf, 2);
113 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
114 struct device_attribute *attr,
117 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
120 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
123 val = (val & AS3935_AFE_MASK) >> 1;
125 return sprintf(buf, "%d\n", val);
128 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t len)
132 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
136 ret = kstrtoul((const char *) buf, 10, &val);
140 if (val > AS3935_AFE_GAIN_MAX)
143 as3935_write(st, AS3935_AFE_GAIN, val << 1);
148 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
149 struct device_attribute *attr,
152 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
155 mutex_lock(&st->lock);
156 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
157 mutex_unlock(&st->lock);
162 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
163 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
165 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
166 as3935_noise_level_tripped_show, NULL, 0);
168 static struct attribute *as3935_attributes[] = {
169 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
170 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
174 static const struct attribute_group as3935_attribute_group = {
175 .attrs = as3935_attributes,
178 static int as3935_read_raw(struct iio_dev *indio_dev,
179 struct iio_chan_spec const *chan,
184 struct as3935_state *st = iio_priv(indio_dev);
189 case IIO_CHAN_INFO_PROCESSED:
190 case IIO_CHAN_INFO_RAW:
192 ret = as3935_read(st, AS3935_DATA, val);
196 /* storm out of range */
197 if (*val == AS3935_DATA_MASK)
200 if (m == IIO_CHAN_INFO_RAW)
203 if (m == IIO_CHAN_INFO_PROCESSED)
206 case IIO_CHAN_INFO_SCALE:
216 static const struct iio_info as3935_info = {
217 .attrs = &as3935_attribute_group,
218 .read_raw = &as3935_read_raw,
221 static irqreturn_t as3935_trigger_handler(int irq, void *private)
223 struct iio_poll_func *pf = private;
224 struct iio_dev *indio_dev = pf->indio_dev;
225 struct as3935_state *st = iio_priv(indio_dev);
228 ret = as3935_read(st, AS3935_DATA, &val);
232 st->scan.chan = val & AS3935_DATA_MASK;
233 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan,
234 iio_get_time_ns(indio_dev));
236 iio_trigger_notify_done(indio_dev->trig);
241 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
244 static void as3935_event_work(struct work_struct *work)
246 struct as3935_state *st;
250 st = container_of(work, struct as3935_state, work.work);
252 ret = as3935_read(st, AS3935_INT, &val);
254 dev_warn(&st->spi->dev, "read error\n");
258 val &= AS3935_INT_MASK;
261 case AS3935_EVENT_INT:
262 iio_trigger_poll_chained(st->trig);
264 case AS3935_DISTURB_INT:
265 case AS3935_NOISE_INT:
266 mutex_lock(&st->lock);
267 st->noise_tripped = jiffies;
268 mutex_unlock(&st->lock);
269 dev_warn(&st->spi->dev, "noise level is too high\n");
274 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
276 struct iio_dev *indio_dev = private;
277 struct as3935_state *st = iio_priv(indio_dev);
280 * Delay work for >2 milliseconds after an interrupt to allow
281 * estimated distance to recalculated.
284 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
289 static void calibrate_as3935(struct as3935_state *st)
291 as3935_write(st, AS3935_DEFAULTS, 0x96);
292 as3935_write(st, AS3935_CALIBRATE, 0x96);
293 as3935_write(st, AS3935_TUNE_CAP,
294 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
297 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
298 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
301 #ifdef CONFIG_PM_SLEEP
302 static int as3935_suspend(struct device *dev)
304 struct iio_dev *indio_dev = dev_get_drvdata(dev);
305 struct as3935_state *st = iio_priv(indio_dev);
308 mutex_lock(&st->lock);
309 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
312 val |= AS3935_AFE_PWR_BIT;
314 ret = as3935_write(st, AS3935_AFE_GAIN, val);
317 mutex_unlock(&st->lock);
322 static int as3935_resume(struct device *dev)
324 struct iio_dev *indio_dev = dev_get_drvdata(dev);
325 struct as3935_state *st = iio_priv(indio_dev);
328 mutex_lock(&st->lock);
329 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
332 val &= ~AS3935_AFE_PWR_BIT;
333 ret = as3935_write(st, AS3935_AFE_GAIN, val);
335 calibrate_as3935(st);
338 mutex_unlock(&st->lock);
343 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
344 #define AS3935_PM_OPS (&as3935_pm_ops)
347 #define AS3935_PM_OPS NULL
350 static void as3935_stop_work(void *data)
352 struct iio_dev *indio_dev = data;
353 struct as3935_state *st = iio_priv(indio_dev);
355 cancel_delayed_work_sync(&st->work);
358 static int as3935_probe(struct spi_device *spi)
360 struct device *dev = &spi->dev;
361 struct iio_dev *indio_dev;
362 struct iio_trigger *trig;
363 struct as3935_state *st;
366 /* Be sure lightning event interrupt is specified */
368 dev_err(dev, "unable to get event interrupt\n");
372 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
376 st = iio_priv(indio_dev);
379 spi_set_drvdata(spi, indio_dev);
380 mutex_init(&st->lock);
382 ret = device_property_read_u32(dev,
383 "ams,tuning-capacitor-pf", &st->tune_cap);
386 dev_warn(dev, "no tuning-capacitor-pf set, defaulting to %d",
390 if (st->tune_cap > MAX_PF_CAP) {
391 dev_err(dev, "wrong tuning-capacitor-pf setting of %d\n",
396 ret = device_property_read_u32(dev,
397 "ams,nflwdth", &st->nflwdth_reg);
398 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
399 dev_err(dev, "invalid nflwdth setting of %d\n",
404 indio_dev->name = spi_get_device_id(spi)->name;
405 indio_dev->channels = as3935_channels;
406 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
407 indio_dev->modes = INDIO_DIRECT_MODE;
408 indio_dev->info = &as3935_info;
410 trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
412 iio_device_id(indio_dev));
418 st->noise_tripped = jiffies - HZ;
419 iio_trigger_set_drvdata(trig, indio_dev);
420 trig->ops = &iio_interrupt_trigger_ops;
422 ret = devm_iio_trigger_register(dev, trig);
424 dev_err(dev, "failed to register trigger\n");
428 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
429 iio_pollfunc_store_time,
430 as3935_trigger_handler, NULL);
433 dev_err(dev, "cannot setup iio trigger\n");
437 calibrate_as3935(st);
439 INIT_DELAYED_WORK(&st->work, as3935_event_work);
440 ret = devm_add_action(dev, as3935_stop_work, indio_dev);
444 ret = devm_request_irq(dev, spi->irq,
445 &as3935_interrupt_handler,
451 dev_err(dev, "unable to request irq\n");
455 ret = devm_iio_device_register(dev, indio_dev);
457 dev_err(dev, "unable to register device\n");
463 static const struct of_device_id as3935_of_match[] = {
464 { .compatible = "ams,as3935", },
467 MODULE_DEVICE_TABLE(of, as3935_of_match);
469 static const struct spi_device_id as3935_id[] = {
473 MODULE_DEVICE_TABLE(spi, as3935_id);
475 static struct spi_driver as3935_driver = {
478 .of_match_table = as3935_of_match,
481 .probe = as3935_probe,
482 .id_table = as3935_id,
484 module_spi_driver(as3935_driver);
486 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
487 MODULE_DESCRIPTION("AS3935 lightning sensor");
488 MODULE_LICENSE("GPL");