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/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/triggered_buffer.h>
25 #define AS3935_AFE_GAIN 0x00
26 #define AS3935_AFE_MASK 0x3F
27 #define AS3935_AFE_GAIN_MAX 0x1F
28 #define AS3935_AFE_PWR_BIT BIT(0)
30 #define AS3935_NFLWDTH 0x01
31 #define AS3935_NFLWDTH_MASK 0x7f
33 #define AS3935_INT 0x03
34 #define AS3935_INT_MASK 0x0f
35 #define AS3935_DISTURB_INT BIT(2)
36 #define AS3935_EVENT_INT BIT(3)
37 #define AS3935_NOISE_INT BIT(0)
39 #define AS3935_DATA 0x07
40 #define AS3935_DATA_MASK 0x3F
42 #define AS3935_TUNE_CAP 0x08
43 #define AS3935_DEFAULTS 0x3C
44 #define AS3935_CALIBRATE 0x3D
46 #define AS3935_READ_DATA BIT(14)
47 #define AS3935_ADDRESS(x) ((x) << 8)
49 #define MAX_PF_CAP 120
50 #define TUNE_CAP_DIV 8
53 struct spi_device
*spi
;
54 struct iio_trigger
*trig
;
56 struct delayed_work work
;
58 unsigned long noise_tripped
;
61 u8 buffer
[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
62 u8 buf
[2] ____cacheline_aligned
;
65 static const struct iio_chan_spec as3935_channels
[] = {
67 .type
= IIO_PROXIMITY
,
69 BIT(IIO_CHAN_INFO_RAW
) |
70 BIT(IIO_CHAN_INFO_PROCESSED
) |
71 BIT(IIO_CHAN_INFO_SCALE
),
79 IIO_CHAN_SOFT_TIMESTAMP(1),
82 static int as3935_read(struct as3935_state
*st
, unsigned int reg
, int *val
)
87 cmd
= (AS3935_READ_DATA
| AS3935_ADDRESS(reg
)) >> 8;
88 ret
= spi_w8r8(st
->spi
, cmd
);
96 static int as3935_write(struct as3935_state
*st
,
102 buf
[0] = AS3935_ADDRESS(reg
) >> 8;
105 return spi_write(st
->spi
, buf
, 2);
108 static ssize_t
as3935_sensor_sensitivity_show(struct device
*dev
,
109 struct device_attribute
*attr
,
112 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
115 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
118 val
= (val
& AS3935_AFE_MASK
) >> 1;
120 return sprintf(buf
, "%d\n", val
);
123 static ssize_t
as3935_sensor_sensitivity_store(struct device
*dev
,
124 struct device_attribute
*attr
,
125 const char *buf
, size_t len
)
127 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
131 ret
= kstrtoul((const char *) buf
, 10, &val
);
135 if (val
> AS3935_AFE_GAIN_MAX
)
138 as3935_write(st
, AS3935_AFE_GAIN
, val
<< 1);
143 static ssize_t
as3935_noise_level_tripped_show(struct device
*dev
,
144 struct device_attribute
*attr
,
147 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
150 mutex_lock(&st
->lock
);
151 ret
= sprintf(buf
, "%d\n", !time_after(jiffies
, st
->noise_tripped
+ HZ
));
152 mutex_unlock(&st
->lock
);
157 static IIO_DEVICE_ATTR(sensor_sensitivity
, S_IRUGO
| S_IWUSR
,
158 as3935_sensor_sensitivity_show
, as3935_sensor_sensitivity_store
, 0);
160 static IIO_DEVICE_ATTR(noise_level_tripped
, S_IRUGO
,
161 as3935_noise_level_tripped_show
, NULL
, 0);
163 static struct attribute
*as3935_attributes
[] = {
164 &iio_dev_attr_sensor_sensitivity
.dev_attr
.attr
,
165 &iio_dev_attr_noise_level_tripped
.dev_attr
.attr
,
169 static const struct attribute_group as3935_attribute_group
= {
170 .attrs
= as3935_attributes
,
173 static int as3935_read_raw(struct iio_dev
*indio_dev
,
174 struct iio_chan_spec
const *chan
,
179 struct as3935_state
*st
= iio_priv(indio_dev
);
184 case IIO_CHAN_INFO_PROCESSED
:
185 case IIO_CHAN_INFO_RAW
:
187 ret
= as3935_read(st
, AS3935_DATA
, val
);
191 /* storm out of range */
192 if (*val
== AS3935_DATA_MASK
)
195 if (m
== IIO_CHAN_INFO_RAW
)
198 if (m
== IIO_CHAN_INFO_PROCESSED
)
201 case IIO_CHAN_INFO_SCALE
:
211 static const struct iio_info as3935_info
= {
212 .attrs
= &as3935_attribute_group
,
213 .read_raw
= &as3935_read_raw
,
216 static irqreturn_t
as3935_trigger_handler(int irq
, void *private)
218 struct iio_poll_func
*pf
= private;
219 struct iio_dev
*indio_dev
= pf
->indio_dev
;
220 struct as3935_state
*st
= iio_priv(indio_dev
);
223 ret
= as3935_read(st
, AS3935_DATA
, &val
);
227 st
->buffer
[0] = val
& AS3935_DATA_MASK
;
228 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->buffer
,
229 iio_get_time_ns(indio_dev
));
231 iio_trigger_notify_done(indio_dev
->trig
);
236 static const struct iio_trigger_ops iio_interrupt_trigger_ops
= {
239 static void as3935_event_work(struct work_struct
*work
)
241 struct as3935_state
*st
;
245 st
= container_of(work
, struct as3935_state
, work
.work
);
247 ret
= as3935_read(st
, AS3935_INT
, &val
);
249 dev_warn(&st
->spi
->dev
, "read error\n");
253 val
&= AS3935_INT_MASK
;
256 case AS3935_EVENT_INT
:
257 iio_trigger_poll_chained(st
->trig
);
259 case AS3935_DISTURB_INT
:
260 case AS3935_NOISE_INT
:
261 mutex_lock(&st
->lock
);
262 st
->noise_tripped
= jiffies
;
263 mutex_unlock(&st
->lock
);
264 dev_warn(&st
->spi
->dev
, "noise level is too high\n");
269 static irqreturn_t
as3935_interrupt_handler(int irq
, void *private)
271 struct iio_dev
*indio_dev
= private;
272 struct as3935_state
*st
= iio_priv(indio_dev
);
275 * Delay work for >2 milliseconds after an interrupt to allow
276 * estimated distance to recalculated.
279 schedule_delayed_work(&st
->work
, msecs_to_jiffies(3));
284 static void calibrate_as3935(struct as3935_state
*st
)
286 as3935_write(st
, AS3935_DEFAULTS
, 0x96);
287 as3935_write(st
, AS3935_CALIBRATE
, 0x96);
288 as3935_write(st
, AS3935_TUNE_CAP
,
289 BIT(5) | (st
->tune_cap
/ TUNE_CAP_DIV
));
292 as3935_write(st
, AS3935_TUNE_CAP
, (st
->tune_cap
/ TUNE_CAP_DIV
));
293 as3935_write(st
, AS3935_NFLWDTH
, st
->nflwdth_reg
);
296 #ifdef CONFIG_PM_SLEEP
297 static int as3935_suspend(struct device
*dev
)
299 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
300 struct as3935_state
*st
= iio_priv(indio_dev
);
303 mutex_lock(&st
->lock
);
304 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
307 val
|= AS3935_AFE_PWR_BIT
;
309 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
312 mutex_unlock(&st
->lock
);
317 static int as3935_resume(struct device
*dev
)
319 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
320 struct as3935_state
*st
= iio_priv(indio_dev
);
323 mutex_lock(&st
->lock
);
324 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
327 val
&= ~AS3935_AFE_PWR_BIT
;
328 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
330 calibrate_as3935(st
);
333 mutex_unlock(&st
->lock
);
338 static SIMPLE_DEV_PM_OPS(as3935_pm_ops
, as3935_suspend
, as3935_resume
);
339 #define AS3935_PM_OPS (&as3935_pm_ops)
342 #define AS3935_PM_OPS NULL
345 static void as3935_stop_work(void *data
)
347 struct iio_dev
*indio_dev
= data
;
348 struct as3935_state
*st
= iio_priv(indio_dev
);
350 cancel_delayed_work_sync(&st
->work
);
353 static int as3935_probe(struct spi_device
*spi
)
355 struct iio_dev
*indio_dev
;
356 struct iio_trigger
*trig
;
357 struct as3935_state
*st
;
358 struct device_node
*np
= spi
->dev
.of_node
;
361 /* Be sure lightning event interrupt is specified */
363 dev_err(&spi
->dev
, "unable to get event interrupt\n");
367 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
371 st
= iio_priv(indio_dev
);
374 spi_set_drvdata(spi
, indio_dev
);
375 mutex_init(&st
->lock
);
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
= devm_iio_trigger_register(&spi
->dev
, trig
);
423 dev_err(&spi
->dev
, "failed to register trigger\n");
427 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
,
428 iio_pollfunc_store_time
,
429 as3935_trigger_handler
, NULL
);
432 dev_err(&spi
->dev
, "cannot setup iio trigger\n");
436 calibrate_as3935(st
);
438 INIT_DELAYED_WORK(&st
->work
, as3935_event_work
);
439 ret
= devm_add_action(&spi
->dev
, as3935_stop_work
, indio_dev
);
443 ret
= devm_request_irq(&spi
->dev
, spi
->irq
,
444 &as3935_interrupt_handler
,
450 dev_err(&spi
->dev
, "unable to request irq\n");
454 ret
= devm_iio_device_register(&spi
->dev
, indio_dev
);
456 dev_err(&spi
->dev
, "unable to register device\n");
462 static const struct of_device_id as3935_of_match
[] = {
463 { .compatible
= "ams,as3935", },
466 MODULE_DEVICE_TABLE(of
, as3935_of_match
);
468 static const struct spi_device_id as3935_id
[] = {
472 MODULE_DEVICE_TABLE(spi
, as3935_id
);
474 static struct spi_driver as3935_driver
= {
477 .of_match_table
= of_match_ptr(as3935_of_match
),
480 .probe
= as3935_probe
,
481 .id_table
= as3935_id
,
483 module_spi_driver(as3935_driver
);
485 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
486 MODULE_DESCRIPTION("AS3935 lightning sensor");
487 MODULE_LICENSE("GPL");