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 u8 buffer
[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
65 u8 buf
[2] ____cacheline_aligned
;
68 static const struct iio_chan_spec as3935_channels
[] = {
70 .type
= IIO_PROXIMITY
,
72 BIT(IIO_CHAN_INFO_RAW
) |
73 BIT(IIO_CHAN_INFO_PROCESSED
) |
74 BIT(IIO_CHAN_INFO_SCALE
),
82 IIO_CHAN_SOFT_TIMESTAMP(1),
85 static int as3935_read(struct as3935_state
*st
, unsigned int reg
, int *val
)
90 cmd
= (AS3935_READ_DATA
| AS3935_ADDRESS(reg
)) >> 8;
91 ret
= spi_w8r8(st
->spi
, cmd
);
99 static int as3935_write(struct as3935_state
*st
,
105 buf
[0] = AS3935_ADDRESS(reg
) >> 8;
108 return spi_write(st
->spi
, buf
, 2);
111 static ssize_t
as3935_sensor_sensitivity_show(struct device
*dev
,
112 struct device_attribute
*attr
,
115 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
118 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
121 val
= (val
& AS3935_AFE_MASK
) >> 1;
123 return sprintf(buf
, "%d\n", val
);
126 static ssize_t
as3935_sensor_sensitivity_store(struct device
*dev
,
127 struct device_attribute
*attr
,
128 const char *buf
, size_t len
)
130 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
134 ret
= kstrtoul((const char *) buf
, 10, &val
);
138 if (val
> AS3935_AFE_GAIN_MAX
)
141 as3935_write(st
, AS3935_AFE_GAIN
, val
<< 1);
146 static ssize_t
as3935_noise_level_tripped_show(struct device
*dev
,
147 struct device_attribute
*attr
,
150 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
153 mutex_lock(&st
->lock
);
154 ret
= sprintf(buf
, "%d\n", !time_after(jiffies
, st
->noise_tripped
+ HZ
));
155 mutex_unlock(&st
->lock
);
160 static IIO_DEVICE_ATTR(sensor_sensitivity
, S_IRUGO
| S_IWUSR
,
161 as3935_sensor_sensitivity_show
, as3935_sensor_sensitivity_store
, 0);
163 static IIO_DEVICE_ATTR(noise_level_tripped
, S_IRUGO
,
164 as3935_noise_level_tripped_show
, NULL
, 0);
166 static struct attribute
*as3935_attributes
[] = {
167 &iio_dev_attr_sensor_sensitivity
.dev_attr
.attr
,
168 &iio_dev_attr_noise_level_tripped
.dev_attr
.attr
,
172 static const struct attribute_group as3935_attribute_group
= {
173 .attrs
= as3935_attributes
,
176 static int as3935_read_raw(struct iio_dev
*indio_dev
,
177 struct iio_chan_spec
const *chan
,
182 struct as3935_state
*st
= iio_priv(indio_dev
);
187 case IIO_CHAN_INFO_PROCESSED
:
188 case IIO_CHAN_INFO_RAW
:
190 ret
= as3935_read(st
, AS3935_DATA
, val
);
194 /* storm out of range */
195 if (*val
== AS3935_DATA_MASK
)
198 if (m
== IIO_CHAN_INFO_RAW
)
201 if (m
== IIO_CHAN_INFO_PROCESSED
)
204 case IIO_CHAN_INFO_SCALE
:
214 static const struct iio_info as3935_info
= {
215 .attrs
= &as3935_attribute_group
,
216 .read_raw
= &as3935_read_raw
,
219 static irqreturn_t
as3935_trigger_handler(int irq
, void *private)
221 struct iio_poll_func
*pf
= private;
222 struct iio_dev
*indio_dev
= pf
->indio_dev
;
223 struct as3935_state
*st
= iio_priv(indio_dev
);
226 ret
= as3935_read(st
, AS3935_DATA
, &val
);
230 st
->buffer
[0] = val
& AS3935_DATA_MASK
;
231 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->buffer
,
232 iio_get_time_ns(indio_dev
));
234 iio_trigger_notify_done(indio_dev
->trig
);
239 static const struct iio_trigger_ops iio_interrupt_trigger_ops
= {
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_chained(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 #ifdef CONFIG_PM_SLEEP
300 static int as3935_suspend(struct device
*dev
)
302 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
303 struct as3935_state
*st
= iio_priv(indio_dev
);
306 mutex_lock(&st
->lock
);
307 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
310 val
|= AS3935_AFE_PWR_BIT
;
312 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
315 mutex_unlock(&st
->lock
);
320 static int as3935_resume(struct device
*dev
)
322 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
323 struct as3935_state
*st
= iio_priv(indio_dev
);
326 mutex_lock(&st
->lock
);
327 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
330 val
&= ~AS3935_AFE_PWR_BIT
;
331 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
333 calibrate_as3935(st
);
336 mutex_unlock(&st
->lock
);
341 static SIMPLE_DEV_PM_OPS(as3935_pm_ops
, as3935_suspend
, as3935_resume
);
342 #define AS3935_PM_OPS (&as3935_pm_ops)
345 #define AS3935_PM_OPS NULL
348 static int as3935_probe(struct spi_device
*spi
)
350 struct iio_dev
*indio_dev
;
351 struct iio_trigger
*trig
;
352 struct as3935_state
*st
;
353 struct device_node
*np
= spi
->dev
.of_node
;
356 /* Be sure lightning event interrupt is specified */
358 dev_err(&spi
->dev
, "unable to get event interrupt\n");
362 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
366 st
= iio_priv(indio_dev
);
369 spi_set_drvdata(spi
, indio_dev
);
370 mutex_init(&st
->lock
);
371 INIT_DELAYED_WORK(&st
->work
, as3935_event_work
);
373 ret
= of_property_read_u32(np
,
374 "ams,tuning-capacitor-pf", &st
->tune_cap
);
378 "no tuning-capacitor-pf set, defaulting to %d",
382 if (st
->tune_cap
> MAX_PF_CAP
) {
384 "wrong tuning-capacitor-pf setting of %d\n",
389 ret
= of_property_read_u32(np
,
390 "ams,nflwdth", &st
->nflwdth_reg
);
391 if (!ret
&& st
->nflwdth_reg
> AS3935_NFLWDTH_MASK
) {
393 "invalid nflwdth setting of %d\n",
398 indio_dev
->dev
.parent
= &spi
->dev
;
399 indio_dev
->name
= spi_get_device_id(spi
)->name
;
400 indio_dev
->channels
= as3935_channels
;
401 indio_dev
->num_channels
= ARRAY_SIZE(as3935_channels
);
402 indio_dev
->modes
= INDIO_DIRECT_MODE
;
403 indio_dev
->info
= &as3935_info
;
405 trig
= devm_iio_trigger_alloc(&spi
->dev
, "%s-dev%d",
406 indio_dev
->name
, indio_dev
->id
);
412 st
->noise_tripped
= jiffies
- HZ
;
413 trig
->dev
.parent
= indio_dev
->dev
.parent
;
414 iio_trigger_set_drvdata(trig
, indio_dev
);
415 trig
->ops
= &iio_interrupt_trigger_ops
;
417 ret
= iio_trigger_register(trig
);
419 dev_err(&spi
->dev
, "failed to register trigger\n");
423 ret
= iio_triggered_buffer_setup(indio_dev
, iio_pollfunc_store_time
,
424 &as3935_trigger_handler
, NULL
);
427 dev_err(&spi
->dev
, "cannot setup iio trigger\n");
428 goto unregister_trigger
;
431 calibrate_as3935(st
);
433 ret
= devm_request_irq(&spi
->dev
, spi
->irq
,
434 &as3935_interrupt_handler
,
440 dev_err(&spi
->dev
, "unable to request irq\n");
441 goto unregister_buffer
;
444 ret
= iio_device_register(indio_dev
);
446 dev_err(&spi
->dev
, "unable to register device\n");
447 goto unregister_buffer
;
452 iio_triggered_buffer_cleanup(indio_dev
);
455 iio_trigger_unregister(st
->trig
);
460 static int as3935_remove(struct spi_device
*spi
)
462 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
463 struct as3935_state
*st
= iio_priv(indio_dev
);
465 iio_device_unregister(indio_dev
);
466 iio_triggered_buffer_cleanup(indio_dev
);
467 iio_trigger_unregister(st
->trig
);
472 static const struct of_device_id as3935_of_match
[] = {
473 { .compatible
= "ams,as3935", },
476 MODULE_DEVICE_TABLE(of
, as3935_of_match
);
478 static const struct spi_device_id as3935_id
[] = {
482 MODULE_DEVICE_TABLE(spi
, as3935_id
);
484 static struct spi_driver as3935_driver
= {
487 .of_match_table
= of_match_ptr(as3935_of_match
),
490 .probe
= as3935_probe
,
491 .remove
= as3935_remove
,
492 .id_table
= as3935_id
,
494 module_spi_driver(as3935_driver
);
496 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
497 MODULE_DESCRIPTION("AS3935 lightning sensor");
498 MODULE_LICENSE("GPL");