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/devm-helpers.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18 #include <linux/irq.h>
19 #include <linux/spi/spi.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #define AS3935_AFE_GAIN 0x00
28 #define AS3935_AFE_MASK 0x3F
29 #define AS3935_AFE_GAIN_MAX 0x1F
30 #define AS3935_AFE_PWR_BIT BIT(0)
32 #define AS3935_NFLWDTH 0x01
33 #define AS3935_NFLWDTH_MASK 0x7f
35 #define AS3935_INT 0x03
36 #define AS3935_INT_MASK 0x0f
37 #define AS3935_DISTURB_INT BIT(2)
38 #define AS3935_EVENT_INT BIT(3)
39 #define AS3935_NOISE_INT BIT(0)
41 #define AS3935_DATA 0x07
42 #define AS3935_DATA_MASK 0x3F
44 #define AS3935_TUNE_CAP 0x08
45 #define AS3935_DEFAULTS 0x3C
46 #define AS3935_CALIBRATE 0x3D
48 #define AS3935_READ_DATA BIT(14)
49 #define AS3935_ADDRESS(x) ((x) << 8)
51 #define MAX_PF_CAP 120
52 #define TUNE_CAP_DIV 8
55 struct spi_device
*spi
;
56 struct iio_trigger
*trig
;
58 struct delayed_work work
;
60 unsigned long noise_tripped
;
63 /* Ensure timestamp is naturally aligned */
66 s64 timestamp
__aligned(8);
68 u8 buf
[2] __aligned(IIO_DMA_MINALIGN
);
71 static const struct iio_chan_spec as3935_channels
[] = {
73 .type
= IIO_PROXIMITY
,
75 BIT(IIO_CHAN_INFO_RAW
) |
76 BIT(IIO_CHAN_INFO_PROCESSED
) |
77 BIT(IIO_CHAN_INFO_SCALE
),
85 IIO_CHAN_SOFT_TIMESTAMP(1),
88 static int as3935_read(struct as3935_state
*st
, unsigned int reg
, int *val
)
93 cmd
= (AS3935_READ_DATA
| AS3935_ADDRESS(reg
)) >> 8;
94 ret
= spi_w8r8(st
->spi
, cmd
);
102 static int as3935_write(struct as3935_state
*st
,
108 buf
[0] = AS3935_ADDRESS(reg
) >> 8;
111 return spi_write(st
->spi
, buf
, 2);
114 static ssize_t
as3935_sensor_sensitivity_show(struct device
*dev
,
115 struct device_attribute
*attr
,
118 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
121 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
124 val
= (val
& AS3935_AFE_MASK
) >> 1;
126 return sysfs_emit(buf
, "%d\n", val
);
129 static ssize_t
as3935_sensor_sensitivity_store(struct device
*dev
,
130 struct device_attribute
*attr
,
131 const char *buf
, size_t len
)
133 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
137 ret
= kstrtoul(buf
, 10, &val
);
141 if (val
> AS3935_AFE_GAIN_MAX
)
144 as3935_write(st
, AS3935_AFE_GAIN
, val
<< 1);
149 static ssize_t
as3935_noise_level_tripped_show(struct device
*dev
,
150 struct device_attribute
*attr
,
153 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
156 mutex_lock(&st
->lock
);
157 ret
= sysfs_emit(buf
, "%d\n", !time_after(jiffies
, st
->noise_tripped
+ HZ
));
158 mutex_unlock(&st
->lock
);
163 static IIO_DEVICE_ATTR(sensor_sensitivity
, S_IRUGO
| S_IWUSR
,
164 as3935_sensor_sensitivity_show
, as3935_sensor_sensitivity_store
, 0);
166 static IIO_DEVICE_ATTR(noise_level_tripped
, S_IRUGO
,
167 as3935_noise_level_tripped_show
, NULL
, 0);
169 static struct attribute
*as3935_attributes
[] = {
170 &iio_dev_attr_sensor_sensitivity
.dev_attr
.attr
,
171 &iio_dev_attr_noise_level_tripped
.dev_attr
.attr
,
175 static const struct attribute_group as3935_attribute_group
= {
176 .attrs
= as3935_attributes
,
179 static int as3935_read_raw(struct iio_dev
*indio_dev
,
180 struct iio_chan_spec
const *chan
,
185 struct as3935_state
*st
= iio_priv(indio_dev
);
190 case IIO_CHAN_INFO_PROCESSED
:
191 case IIO_CHAN_INFO_RAW
:
193 ret
= as3935_read(st
, AS3935_DATA
, val
);
197 /* storm out of range */
198 if (*val
== AS3935_DATA_MASK
)
201 if (m
== IIO_CHAN_INFO_RAW
)
204 if (m
== IIO_CHAN_INFO_PROCESSED
)
207 case IIO_CHAN_INFO_SCALE
:
217 static const struct iio_info as3935_info
= {
218 .attrs
= &as3935_attribute_group
,
219 .read_raw
= &as3935_read_raw
,
222 static irqreturn_t
as3935_trigger_handler(int irq
, void *private)
224 struct iio_poll_func
*pf
= private;
225 struct iio_dev
*indio_dev
= pf
->indio_dev
;
226 struct as3935_state
*st
= iio_priv(indio_dev
);
229 ret
= as3935_read(st
, AS3935_DATA
, &val
);
233 st
->scan
.chan
= val
& AS3935_DATA_MASK
;
234 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->scan
,
235 iio_get_time_ns(indio_dev
));
237 iio_trigger_notify_done(indio_dev
->trig
);
242 static void as3935_event_work(struct work_struct
*work
)
244 struct as3935_state
*st
;
248 st
= container_of(work
, struct as3935_state
, work
.work
);
250 ret
= as3935_read(st
, AS3935_INT
, &val
);
252 dev_warn(&st
->spi
->dev
, "read error\n");
256 val
&= AS3935_INT_MASK
;
259 case AS3935_EVENT_INT
:
260 iio_trigger_poll_nested(st
->trig
);
262 case AS3935_DISTURB_INT
:
263 case AS3935_NOISE_INT
:
264 mutex_lock(&st
->lock
);
265 st
->noise_tripped
= jiffies
;
266 mutex_unlock(&st
->lock
);
267 dev_warn(&st
->spi
->dev
, "noise level is too high\n");
272 static irqreturn_t
as3935_interrupt_handler(int irq
, void *private)
274 struct iio_dev
*indio_dev
= private;
275 struct as3935_state
*st
= iio_priv(indio_dev
);
278 * Delay work for >2 milliseconds after an interrupt to allow
279 * estimated distance to recalculated.
282 schedule_delayed_work(&st
->work
, msecs_to_jiffies(3));
287 static void calibrate_as3935(struct as3935_state
*st
)
289 as3935_write(st
, AS3935_DEFAULTS
, 0x96);
290 as3935_write(st
, AS3935_CALIBRATE
, 0x96);
291 as3935_write(st
, AS3935_TUNE_CAP
,
292 BIT(5) | (st
->tune_cap
/ TUNE_CAP_DIV
));
295 as3935_write(st
, AS3935_TUNE_CAP
, (st
->tune_cap
/ TUNE_CAP_DIV
));
296 as3935_write(st
, AS3935_NFLWDTH
, st
->nflwdth_reg
);
299 static int as3935_suspend(struct device
*dev
)
301 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
302 struct as3935_state
*st
= iio_priv(indio_dev
);
305 mutex_lock(&st
->lock
);
306 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
309 val
|= AS3935_AFE_PWR_BIT
;
311 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
314 mutex_unlock(&st
->lock
);
319 static int as3935_resume(struct device
*dev
)
321 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
322 struct as3935_state
*st
= iio_priv(indio_dev
);
325 mutex_lock(&st
->lock
);
326 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
329 val
&= ~AS3935_AFE_PWR_BIT
;
330 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
332 calibrate_as3935(st
);
335 mutex_unlock(&st
->lock
);
340 static DEFINE_SIMPLE_DEV_PM_OPS(as3935_pm_ops
, as3935_suspend
, as3935_resume
);
342 static int as3935_probe(struct spi_device
*spi
)
344 struct device
*dev
= &spi
->dev
;
345 struct iio_dev
*indio_dev
;
346 struct iio_trigger
*trig
;
347 struct as3935_state
*st
;
350 /* Be sure lightning event interrupt is specified */
352 dev_err(dev
, "unable to get event interrupt\n");
356 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
360 st
= iio_priv(indio_dev
);
363 spi_set_drvdata(spi
, indio_dev
);
364 mutex_init(&st
->lock
);
366 ret
= device_property_read_u32(dev
,
367 "ams,tuning-capacitor-pf", &st
->tune_cap
);
370 dev_warn(dev
, "no tuning-capacitor-pf set, defaulting to %d",
374 if (st
->tune_cap
> MAX_PF_CAP
) {
375 dev_err(dev
, "wrong tuning-capacitor-pf setting of %d\n",
380 ret
= device_property_read_u32(dev
,
381 "ams,nflwdth", &st
->nflwdth_reg
);
382 if (!ret
&& st
->nflwdth_reg
> AS3935_NFLWDTH_MASK
) {
383 dev_err(dev
, "invalid nflwdth setting of %d\n",
388 indio_dev
->name
= spi_get_device_id(spi
)->name
;
389 indio_dev
->channels
= as3935_channels
;
390 indio_dev
->num_channels
= ARRAY_SIZE(as3935_channels
);
391 indio_dev
->modes
= INDIO_DIRECT_MODE
;
392 indio_dev
->info
= &as3935_info
;
394 trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
396 iio_device_id(indio_dev
));
402 st
->noise_tripped
= jiffies
- HZ
;
403 iio_trigger_set_drvdata(trig
, indio_dev
);
405 ret
= devm_iio_trigger_register(dev
, trig
);
407 dev_err(dev
, "failed to register trigger\n");
411 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
412 iio_pollfunc_store_time
,
413 as3935_trigger_handler
, NULL
);
416 dev_err(dev
, "cannot setup iio trigger\n");
420 calibrate_as3935(st
);
422 ret
= devm_delayed_work_autocancel(dev
, &st
->work
, as3935_event_work
);
426 ret
= devm_request_irq(dev
, spi
->irq
,
427 &as3935_interrupt_handler
,
433 dev_err(dev
, "unable to request irq\n");
437 ret
= devm_iio_device_register(dev
, indio_dev
);
439 dev_err(dev
, "unable to register device\n");
445 static const struct of_device_id as3935_of_match
[] = {
446 { .compatible
= "ams,as3935", },
449 MODULE_DEVICE_TABLE(of
, as3935_of_match
);
451 static const struct spi_device_id as3935_id
[] = {
455 MODULE_DEVICE_TABLE(spi
, as3935_id
);
457 static struct spi_driver as3935_driver
= {
460 .of_match_table
= as3935_of_match
,
461 .pm
= pm_sleep_ptr(&as3935_pm_ops
),
463 .probe
= as3935_probe
,
464 .id_table
= as3935_id
,
466 module_spi_driver(as3935_driver
);
468 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
469 MODULE_DESCRIPTION("AS3935 lightning sensor");
470 MODULE_LICENSE("GPL");