2 * as3935.c - Support for AS3935 Franklin lightning sensor
4 * Copyright (C) 2014 Matt Ranostay <mranostay@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/mutex.h>
24 #include <linux/err.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/spi/spi.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/buffer.h>
33 #include <linux/iio/triggered_buffer.h>
34 #include <linux/of_gpio.h>
37 #define AS3935_AFE_GAIN 0x00
38 #define AS3935_AFE_MASK 0x3F
39 #define AS3935_AFE_GAIN_MAX 0x1F
40 #define AS3935_AFE_PWR_BIT BIT(0)
42 #define AS3935_INT 0x03
43 #define AS3935_INT_MASK 0x07
44 #define AS3935_EVENT_INT BIT(3)
45 #define AS3935_NOISE_INT BIT(1)
47 #define AS3935_DATA 0x07
48 #define AS3935_DATA_MASK 0x3F
50 #define AS3935_TUNE_CAP 0x08
51 #define AS3935_CALIBRATE 0x3D
53 #define AS3935_WRITE_DATA BIT(15)
54 #define AS3935_READ_DATA BIT(14)
55 #define AS3935_ADDRESS(x) ((x) << 8)
57 #define MAX_PF_CAP 120
58 #define TUNE_CAP_DIV 8
61 struct spi_device
*spi
;
62 struct iio_trigger
*trig
;
64 struct delayed_work work
;
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
),
83 IIO_CHAN_SOFT_TIMESTAMP(1),
86 static int as3935_read(struct as3935_state
*st
, unsigned int reg
, int *val
)
91 cmd
= (AS3935_READ_DATA
| AS3935_ADDRESS(reg
)) >> 8;
92 ret
= spi_w8r8(st
->spi
, cmd
);
100 static int as3935_write(struct as3935_state
*st
,
106 buf
[0] = (AS3935_WRITE_DATA
| AS3935_ADDRESS(reg
)) >> 8;
109 return spi_write(st
->spi
, buf
, 2);
112 static ssize_t
as3935_sensor_sensitivity_show(struct device
*dev
,
113 struct device_attribute
*attr
,
116 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
119 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
122 val
= (val
& AS3935_AFE_MASK
) >> 1;
124 return sprintf(buf
, "%d\n", val
);
127 static ssize_t
as3935_sensor_sensitivity_store(struct device
*dev
,
128 struct device_attribute
*attr
,
129 const char *buf
, size_t len
)
131 struct as3935_state
*st
= iio_priv(dev_to_iio_dev(dev
));
135 ret
= kstrtoul((const char *) buf
, 10, &val
);
139 if (val
> AS3935_AFE_GAIN_MAX
)
142 as3935_write(st
, AS3935_AFE_GAIN
, val
<< 1);
147 static IIO_DEVICE_ATTR(sensor_sensitivity
, S_IRUGO
| S_IWUSR
,
148 as3935_sensor_sensitivity_show
, as3935_sensor_sensitivity_store
, 0);
151 static struct attribute
*as3935_attributes
[] = {
152 &iio_dev_attr_sensor_sensitivity
.dev_attr
.attr
,
156 static struct attribute_group as3935_attribute_group
= {
157 .attrs
= as3935_attributes
,
160 static int as3935_read_raw(struct iio_dev
*indio_dev
,
161 struct iio_chan_spec
const *chan
,
166 struct as3935_state
*st
= iio_priv(indio_dev
);
171 case IIO_CHAN_INFO_PROCESSED
:
172 case IIO_CHAN_INFO_RAW
:
174 ret
= as3935_read(st
, AS3935_DATA
, val
);
178 if (m
== IIO_CHAN_INFO_RAW
)
181 /* storm out of range */
182 if (*val
== AS3935_DATA_MASK
)
193 static const struct iio_info as3935_info
= {
194 .driver_module
= THIS_MODULE
,
195 .attrs
= &as3935_attribute_group
,
196 .read_raw
= &as3935_read_raw
,
199 static irqreturn_t
as3935_trigger_handler(int irq
, void *private)
201 struct iio_poll_func
*pf
= private;
202 struct iio_dev
*indio_dev
= pf
->indio_dev
;
203 struct as3935_state
*st
= iio_priv(indio_dev
);
206 ret
= as3935_read(st
, AS3935_DATA
, &val
);
209 val
&= AS3935_DATA_MASK
;
212 iio_push_to_buffers_with_timestamp(indio_dev
, &val
, pf
->timestamp
);
214 iio_trigger_notify_done(indio_dev
->trig
);
219 static const struct iio_trigger_ops iio_interrupt_trigger_ops
= {
220 .owner
= THIS_MODULE
,
223 static void as3935_event_work(struct work_struct
*work
)
225 struct as3935_state
*st
;
228 st
= container_of(work
, struct as3935_state
, work
.work
);
230 as3935_read(st
, AS3935_INT
, &val
);
231 val
&= AS3935_INT_MASK
;
234 case AS3935_EVENT_INT
:
235 iio_trigger_poll(st
->trig
);
237 case AS3935_NOISE_INT
:
238 dev_warn(&st
->spi
->dev
, "noise level is too high");
243 static irqreturn_t
as3935_interrupt_handler(int irq
, void *private)
245 struct iio_dev
*indio_dev
= private;
246 struct as3935_state
*st
= iio_priv(indio_dev
);
249 * Delay work for >2 milliseconds after an interrupt to allow
250 * estimated distance to recalculated.
253 schedule_delayed_work(&st
->work
, msecs_to_jiffies(3));
258 static void calibrate_as3935(struct as3935_state
*st
)
260 mutex_lock(&st
->lock
);
262 /* mask disturber interrupt bit */
263 as3935_write(st
, AS3935_INT
, BIT(5));
265 as3935_write(st
, AS3935_CALIBRATE
, 0x96);
266 as3935_write(st
, AS3935_TUNE_CAP
,
267 BIT(5) | (st
->tune_cap
/ TUNE_CAP_DIV
));
270 as3935_write(st
, AS3935_TUNE_CAP
, (st
->tune_cap
/ TUNE_CAP_DIV
));
272 mutex_unlock(&st
->lock
);
275 #ifdef CONFIG_PM_SLEEP
276 static int as3935_suspend(struct device
*dev
)
278 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
279 struct as3935_state
*st
= iio_priv(indio_dev
);
282 mutex_lock(&st
->lock
);
283 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
286 val
|= AS3935_AFE_PWR_BIT
;
288 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
291 mutex_unlock(&st
->lock
);
296 static int as3935_resume(struct device
*dev
)
298 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
299 struct as3935_state
*st
= iio_priv(indio_dev
);
302 mutex_lock(&st
->lock
);
303 ret
= as3935_read(st
, AS3935_AFE_GAIN
, &val
);
306 val
&= ~AS3935_AFE_PWR_BIT
;
307 ret
= as3935_write(st
, AS3935_AFE_GAIN
, val
);
310 mutex_unlock(&st
->lock
);
315 static SIMPLE_DEV_PM_OPS(as3935_pm_ops
, as3935_suspend
, as3935_resume
);
316 #define AS3935_PM_OPS (&as3935_pm_ops)
319 #define AS3935_PM_OPS NULL
322 static int as3935_probe(struct spi_device
*spi
)
324 struct iio_dev
*indio_dev
;
325 struct iio_trigger
*trig
;
326 struct as3935_state
*st
;
327 struct device_node
*np
= spi
->dev
.of_node
;
330 /* Be sure lightning event interrupt is specified */
332 dev_err(&spi
->dev
, "unable to get event interrupt\n");
336 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
340 st
= iio_priv(indio_dev
);
344 spi_set_drvdata(spi
, indio_dev
);
345 mutex_init(&st
->lock
);
346 INIT_DELAYED_WORK(&st
->work
, as3935_event_work
);
348 ret
= of_property_read_u32(np
,
349 "ams,tuning-capacitor-pf", &st
->tune_cap
);
353 "no tuning-capacitor-pf set, defaulting to %d",
357 if (st
->tune_cap
> MAX_PF_CAP
) {
359 "wrong tuning-capacitor-pf setting of %d\n",
364 indio_dev
->dev
.parent
= &spi
->dev
;
365 indio_dev
->name
= spi_get_device_id(spi
)->name
;
366 indio_dev
->channels
= as3935_channels
;
367 indio_dev
->num_channels
= ARRAY_SIZE(as3935_channels
);
368 indio_dev
->modes
= INDIO_DIRECT_MODE
;
369 indio_dev
->info
= &as3935_info
;
371 trig
= devm_iio_trigger_alloc(&spi
->dev
, "%s-dev%d",
372 indio_dev
->name
, indio_dev
->id
);
378 trig
->dev
.parent
= indio_dev
->dev
.parent
;
379 iio_trigger_set_drvdata(trig
, indio_dev
);
380 trig
->ops
= &iio_interrupt_trigger_ops
;
382 ret
= iio_trigger_register(trig
);
384 dev_err(&spi
->dev
, "failed to register trigger\n");
388 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
389 &as3935_trigger_handler
, NULL
);
392 dev_err(&spi
->dev
, "cannot setup iio trigger\n");
393 goto unregister_trigger
;
396 calibrate_as3935(st
);
398 ret
= devm_request_irq(&spi
->dev
, spi
->irq
,
399 &as3935_interrupt_handler
,
405 dev_err(&spi
->dev
, "unable to request irq\n");
406 goto unregister_buffer
;
409 ret
= iio_device_register(indio_dev
);
411 dev_err(&spi
->dev
, "unable to register device\n");
412 goto unregister_buffer
;
417 iio_triggered_buffer_cleanup(indio_dev
);
420 iio_trigger_unregister(st
->trig
);
425 static int as3935_remove(struct spi_device
*spi
)
427 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
428 struct as3935_state
*st
= iio_priv(indio_dev
);
430 iio_device_unregister(indio_dev
);
431 iio_triggered_buffer_cleanup(indio_dev
);
432 iio_trigger_unregister(st
->trig
);
437 static const struct spi_device_id as3935_id
[] = {
441 MODULE_DEVICE_TABLE(spi
, as3935_id
);
443 static struct spi_driver as3935_driver
= {
446 .owner
= THIS_MODULE
,
449 .probe
= as3935_probe
,
450 .remove
= as3935_remove
,
451 .id_table
= as3935_id
,
453 module_spi_driver(as3935_driver
);
455 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
456 MODULE_DESCRIPTION("AS3935 lightning sensor");
457 MODULE_LICENSE("GPL");
458 MODULE_ALIAS("spi:as3935");