blob: ddf9bee89f777872ec27db426a46c18dd5da2fc3 [file] [log] [blame]
Matt Ranostay24ddb0e2014-12-03 12:53:00 +00001/*
2 * as3935.c - Support for AS3935 Franklin lightning sensor
3 *
4 * Copyright (C) 2014 Matt Ranostay <mranostay@gmail.com>
5 *
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.
10 *
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.
15 *
16 */
17
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>
35
36
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)
41
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)
46
47#define AS3935_DATA 0x07
48#define AS3935_DATA_MASK 0x3F
49
50#define AS3935_TUNE_CAP 0x08
51#define AS3935_CALIBRATE 0x3D
52
Matt Ranostay24ddb0e2014-12-03 12:53:00 +000053#define AS3935_READ_DATA BIT(14)
54#define AS3935_ADDRESS(x) ((x) << 8)
55
56#define MAX_PF_CAP 120
57#define TUNE_CAP_DIV 8
58
59struct as3935_state {
60 struct spi_device *spi;
61 struct iio_trigger *trig;
62 struct mutex lock;
63 struct delayed_work work;
64
65 u32 tune_cap;
Matt Ranostay37b1ba22016-05-21 20:01:03 -070066 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
Matt Ranostay24ddb0e2014-12-03 12:53:00 +000067 u8 buf[2] ____cacheline_aligned;
68};
69
70static const struct iio_chan_spec as3935_channels[] = {
71 {
72 .type = IIO_PROXIMITY,
73 .info_mask_separate =
74 BIT(IIO_CHAN_INFO_RAW) |
Matt Ranostay51388062016-05-21 20:01:01 -070075 BIT(IIO_CHAN_INFO_PROCESSED) |
76 BIT(IIO_CHAN_INFO_SCALE),
Matt Ranostay24ddb0e2014-12-03 12:53:00 +000077 .scan_index = 0,
78 .scan_type = {
79 .sign = 'u',
80 .realbits = 6,
81 .storagebits = 8,
82 },
83 },
84 IIO_CHAN_SOFT_TIMESTAMP(1),
85};
86
87static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
88{
89 u8 cmd;
90 int ret;
91
92 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
93 ret = spi_w8r8(st->spi, cmd);
94 if (ret < 0)
95 return ret;
96 *val = ret;
97
98 return 0;
George McCollister455f0042014-10-31 10:44:23 -050099}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000100
101static int as3935_write(struct as3935_state *st,
102 unsigned int reg,
103 unsigned int val)
104{
105 u8 *buf = st->buf;
106
Matt Ranostay84ca8e32017-04-13 23:21:56 -0700107 buf[0] = AS3935_ADDRESS(reg) >> 8;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000108 buf[1] = val;
109
110 return spi_write(st->spi, buf, 2);
George McCollister455f0042014-10-31 10:44:23 -0500111}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000112
113static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
114 struct device_attribute *attr,
115 char *buf)
116{
117 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
118 int val, ret;
119
120 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
121 if (ret)
122 return ret;
123 val = (val & AS3935_AFE_MASK) >> 1;
124
125 return sprintf(buf, "%d\n", val);
George McCollister455f0042014-10-31 10:44:23 -0500126}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000127
128static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t len)
131{
132 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
133 unsigned long val;
134 int ret;
135
136 ret = kstrtoul((const char *) buf, 10, &val);
137 if (ret)
138 return -EINVAL;
139
140 if (val > AS3935_AFE_GAIN_MAX)
141 return -EINVAL;
142
143 as3935_write(st, AS3935_AFE_GAIN, val << 1);
144
145 return len;
George McCollister455f0042014-10-31 10:44:23 -0500146}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000147
148static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
149 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
150
151
152static struct attribute *as3935_attributes[] = {
153 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
154 NULL,
155};
156
simran singhal7ab89e12017-04-01 14:09:55 +0530157static const struct attribute_group as3935_attribute_group = {
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000158 .attrs = as3935_attributes,
159};
160
161static int as3935_read_raw(struct iio_dev *indio_dev,
162 struct iio_chan_spec const *chan,
163 int *val,
164 int *val2,
165 long m)
166{
167 struct as3935_state *st = iio_priv(indio_dev);
168 int ret;
169
170
171 switch (m) {
172 case IIO_CHAN_INFO_PROCESSED:
173 case IIO_CHAN_INFO_RAW:
174 *val2 = 0;
175 ret = as3935_read(st, AS3935_DATA, val);
176 if (ret)
177 return ret;
178
179 if (m == IIO_CHAN_INFO_RAW)
180 return IIO_VAL_INT;
181
182 /* storm out of range */
183 if (*val == AS3935_DATA_MASK)
184 return -EINVAL;
Matt Ranostay51388062016-05-21 20:01:01 -0700185
186 if (m == IIO_CHAN_INFO_PROCESSED)
187 *val *= 1000;
188 break;
189 case IIO_CHAN_INFO_SCALE:
190 *val = 1000;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000191 break;
192 default:
193 return -EINVAL;
194 }
195
196 return IIO_VAL_INT;
197}
198
199static const struct iio_info as3935_info = {
200 .driver_module = THIS_MODULE,
201 .attrs = &as3935_attribute_group,
202 .read_raw = &as3935_read_raw,
203};
204
205static irqreturn_t as3935_trigger_handler(int irq, void *private)
206{
207 struct iio_poll_func *pf = private;
208 struct iio_dev *indio_dev = pf->indio_dev;
209 struct as3935_state *st = iio_priv(indio_dev);
210 int val, ret;
211
212 ret = as3935_read(st, AS3935_DATA, &val);
213 if (ret)
214 goto err_read;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000215
Matt Ranostay37b1ba22016-05-21 20:01:03 -0700216 st->buffer[0] = val & AS3935_DATA_MASK;
217 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
218 pf->timestamp);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000219err_read:
220 iio_trigger_notify_done(indio_dev->trig);
221
222 return IRQ_HANDLED;
George McCollister455f0042014-10-31 10:44:23 -0500223}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000224
225static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
226 .owner = THIS_MODULE,
227};
228
229static void as3935_event_work(struct work_struct *work)
230{
231 struct as3935_state *st;
232 int val;
Arnd Bergmann22ed1a12016-05-30 16:52:04 +0200233 int ret;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000234
235 st = container_of(work, struct as3935_state, work.work);
236
Arnd Bergmann22ed1a12016-05-30 16:52:04 +0200237 ret = as3935_read(st, AS3935_INT, &val);
238 if (ret) {
239 dev_warn(&st->spi->dev, "read error\n");
240 return;
241 }
242
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000243 val &= AS3935_INT_MASK;
244
245 switch (val) {
246 case AS3935_EVENT_INT:
Peter Meerwald398fd222014-12-06 06:46:00 +0000247 iio_trigger_poll(st->trig);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000248 break;
249 case AS3935_NOISE_INT:
Arnd Bergmann22ed1a12016-05-30 16:52:04 +0200250 dev_warn(&st->spi->dev, "noise level is too high\n");
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000251 break;
252 }
George McCollister455f0042014-10-31 10:44:23 -0500253}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000254
255static irqreturn_t as3935_interrupt_handler(int irq, void *private)
256{
257 struct iio_dev *indio_dev = private;
258 struct as3935_state *st = iio_priv(indio_dev);
259
260 /*
261 * Delay work for >2 milliseconds after an interrupt to allow
262 * estimated distance to recalculated.
263 */
264
265 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
266
267 return IRQ_HANDLED;
268}
269
270static void calibrate_as3935(struct as3935_state *st)
271{
272 mutex_lock(&st->lock);
273
274 /* mask disturber interrupt bit */
275 as3935_write(st, AS3935_INT, BIT(5));
276
277 as3935_write(st, AS3935_CALIBRATE, 0x96);
278 as3935_write(st, AS3935_TUNE_CAP,
279 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
280
281 mdelay(2);
282 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
283
284 mutex_unlock(&st->lock);
285}
286
287#ifdef CONFIG_PM_SLEEP
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100288static int as3935_suspend(struct device *dev)
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000289{
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100290 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000291 struct as3935_state *st = iio_priv(indio_dev);
292 int val, ret;
293
294 mutex_lock(&st->lock);
295 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
296 if (ret)
297 goto err_suspend;
298 val |= AS3935_AFE_PWR_BIT;
299
300 ret = as3935_write(st, AS3935_AFE_GAIN, val);
301
302err_suspend:
303 mutex_unlock(&st->lock);
304
305 return ret;
306}
307
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100308static int as3935_resume(struct device *dev)
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000309{
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100310 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000311 struct as3935_state *st = iio_priv(indio_dev);
312 int val, ret;
313
314 mutex_lock(&st->lock);
315 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
316 if (ret)
317 goto err_resume;
318 val &= ~AS3935_AFE_PWR_BIT;
319 ret = as3935_write(st, AS3935_AFE_GAIN, val);
320
321err_resume:
322 mutex_unlock(&st->lock);
323
324 return ret;
325}
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100326
327static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
328#define AS3935_PM_OPS (&as3935_pm_ops)
329
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000330#else
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100331#define AS3935_PM_OPS NULL
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000332#endif
333
334static int as3935_probe(struct spi_device *spi)
335{
336 struct iio_dev *indio_dev;
337 struct iio_trigger *trig;
338 struct as3935_state *st;
339 struct device_node *np = spi->dev.of_node;
340 int ret;
341
342 /* Be sure lightning event interrupt is specified */
343 if (!spi->irq) {
344 dev_err(&spi->dev, "unable to get event interrupt\n");
345 return -EINVAL;
346 }
347
George McCollisterf73cde62014-10-31 15:44:00 +0000348 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000349 if (!indio_dev)
350 return -ENOMEM;
351
352 st = iio_priv(indio_dev);
353 st->spi = spi;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000354
355 spi_set_drvdata(spi, indio_dev);
356 mutex_init(&st->lock);
357 INIT_DELAYED_WORK(&st->work, as3935_event_work);
358
359 ret = of_property_read_u32(np,
360 "ams,tuning-capacitor-pf", &st->tune_cap);
361 if (ret) {
362 st->tune_cap = 0;
363 dev_warn(&spi->dev,
364 "no tuning-capacitor-pf set, defaulting to %d",
365 st->tune_cap);
366 }
367
368 if (st->tune_cap > MAX_PF_CAP) {
369 dev_err(&spi->dev,
370 "wrong tuning-capacitor-pf setting of %d\n",
371 st->tune_cap);
372 return -EINVAL;
373 }
374
375 indio_dev->dev.parent = &spi->dev;
376 indio_dev->name = spi_get_device_id(spi)->name;
377 indio_dev->channels = as3935_channels;
378 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
379 indio_dev->modes = INDIO_DIRECT_MODE;
380 indio_dev->info = &as3935_info;
381
382 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
383 indio_dev->name, indio_dev->id);
384
385 if (!trig)
386 return -ENOMEM;
387
388 st->trig = trig;
389 trig->dev.parent = indio_dev->dev.parent;
390 iio_trigger_set_drvdata(trig, indio_dev);
391 trig->ops = &iio_interrupt_trigger_ops;
392
393 ret = iio_trigger_register(trig);
394 if (ret) {
395 dev_err(&spi->dev, "failed to register trigger\n");
396 return ret;
397 }
398
Alison Schofieldf8adf642016-07-11 08:26:56 -0700399 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000400 &as3935_trigger_handler, NULL);
401
402 if (ret) {
403 dev_err(&spi->dev, "cannot setup iio trigger\n");
404 goto unregister_trigger;
405 }
406
407 calibrate_as3935(st);
408
409 ret = devm_request_irq(&spi->dev, spi->irq,
410 &as3935_interrupt_handler,
411 IRQF_TRIGGER_RISING,
412 dev_name(&spi->dev),
413 indio_dev);
414
415 if (ret) {
416 dev_err(&spi->dev, "unable to request irq\n");
417 goto unregister_buffer;
418 }
419
420 ret = iio_device_register(indio_dev);
421 if (ret < 0) {
422 dev_err(&spi->dev, "unable to register device\n");
423 goto unregister_buffer;
424 }
425 return 0;
426
427unregister_buffer:
428 iio_triggered_buffer_cleanup(indio_dev);
429
430unregister_trigger:
431 iio_trigger_unregister(st->trig);
432
433 return ret;
George McCollister455f0042014-10-31 10:44:23 -0500434}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000435
436static int as3935_remove(struct spi_device *spi)
437{
438 struct iio_dev *indio_dev = spi_get_drvdata(spi);
439 struct as3935_state *st = iio_priv(indio_dev);
440
441 iio_device_unregister(indio_dev);
442 iio_triggered_buffer_cleanup(indio_dev);
443 iio_trigger_unregister(st->trig);
444
445 return 0;
George McCollister455f0042014-10-31 10:44:23 -0500446}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000447
Javier Martinez Canillas8b7c8262015-08-20 09:07:27 +0200448static const struct of_device_id as3935_of_match[] = {
449 { .compatible = "ams,as3935", },
450 { /* sentinel */ },
451};
452MODULE_DEVICE_TABLE(of, as3935_of_match);
453
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000454static const struct spi_device_id as3935_id[] = {
455 {"as3935", 0},
456 {},
457};
458MODULE_DEVICE_TABLE(spi, as3935_id);
459
460static struct spi_driver as3935_driver = {
461 .driver = {
462 .name = "as3935",
Javier Martinez Canillas8b7c8262015-08-20 09:07:27 +0200463 .of_match_table = of_match_ptr(as3935_of_match),
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100464 .pm = AS3935_PM_OPS,
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000465 },
466 .probe = as3935_probe,
467 .remove = as3935_remove,
468 .id_table = as3935_id,
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000469};
470module_spi_driver(as3935_driver);
471
472MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
473MODULE_DESCRIPTION("AS3935 lightning sensor");
474MODULE_LICENSE("GPL");