blob: e2f926cdcad2acdbdd8aec50f0f479f52d2a6584 [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
53#define AS3935_WRITE_DATA BIT(15)
54#define AS3935_READ_DATA BIT(14)
55#define AS3935_ADDRESS(x) ((x) << 8)
56
57#define MAX_PF_CAP 120
58#define TUNE_CAP_DIV 8
59
60struct as3935_state {
61 struct spi_device *spi;
62 struct iio_trigger *trig;
63 struct mutex lock;
64 struct delayed_work work;
65
66 u32 tune_cap;
Matt Ranostay37b1ba22016-05-21 20:01:03 -070067 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
Matt Ranostay24ddb0e2014-12-03 12:53:00 +000068 u8 buf[2] ____cacheline_aligned;
69};
70
71static const struct iio_chan_spec as3935_channels[] = {
72 {
73 .type = IIO_PROXIMITY,
74 .info_mask_separate =
75 BIT(IIO_CHAN_INFO_RAW) |
Matt Ranostay51388062016-05-21 20:01:01 -070076 BIT(IIO_CHAN_INFO_PROCESSED) |
77 BIT(IIO_CHAN_INFO_SCALE),
Matt Ranostay24ddb0e2014-12-03 12:53:00 +000078 .scan_index = 0,
79 .scan_type = {
80 .sign = 'u',
81 .realbits = 6,
82 .storagebits = 8,
83 },
84 },
85 IIO_CHAN_SOFT_TIMESTAMP(1),
86};
87
88static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
89{
90 u8 cmd;
91 int ret;
92
93 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
94 ret = spi_w8r8(st->spi, cmd);
95 if (ret < 0)
96 return ret;
97 *val = ret;
98
99 return 0;
George McCollister455f0042014-10-31 10:44:23 -0500100}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000101
102static int as3935_write(struct as3935_state *st,
103 unsigned int reg,
104 unsigned int val)
105{
106 u8 *buf = st->buf;
107
108 buf[0] = (AS3935_WRITE_DATA | AS3935_ADDRESS(reg)) >> 8;
109 buf[1] = val;
110
111 return spi_write(st->spi, buf, 2);
George McCollister455f0042014-10-31 10:44:23 -0500112}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000113
114static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
115 struct device_attribute *attr,
116 char *buf)
117{
118 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
119 int val, ret;
120
121 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
122 if (ret)
123 return ret;
124 val = (val & AS3935_AFE_MASK) >> 1;
125
126 return sprintf(buf, "%d\n", val);
George McCollister455f0042014-10-31 10:44:23 -0500127}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000128
129static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf, size_t len)
132{
133 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
134 unsigned long val;
135 int ret;
136
137 ret = kstrtoul((const char *) buf, 10, &val);
138 if (ret)
139 return -EINVAL;
140
141 if (val > AS3935_AFE_GAIN_MAX)
142 return -EINVAL;
143
144 as3935_write(st, AS3935_AFE_GAIN, val << 1);
145
146 return len;
George McCollister455f0042014-10-31 10:44:23 -0500147}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000148
149static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
150 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
151
152
153static struct attribute *as3935_attributes[] = {
154 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
155 NULL,
156};
157
158static struct attribute_group as3935_attribute_group = {
159 .attrs = as3935_attributes,
160};
161
162static int as3935_read_raw(struct iio_dev *indio_dev,
163 struct iio_chan_spec const *chan,
164 int *val,
165 int *val2,
166 long m)
167{
168 struct as3935_state *st = iio_priv(indio_dev);
169 int ret;
170
171
172 switch (m) {
173 case IIO_CHAN_INFO_PROCESSED:
174 case IIO_CHAN_INFO_RAW:
175 *val2 = 0;
176 ret = as3935_read(st, AS3935_DATA, val);
177 if (ret)
178 return ret;
179
180 if (m == IIO_CHAN_INFO_RAW)
181 return IIO_VAL_INT;
182
183 /* storm out of range */
184 if (*val == AS3935_DATA_MASK)
185 return -EINVAL;
Matt Ranostay51388062016-05-21 20:01:01 -0700186
187 if (m == IIO_CHAN_INFO_PROCESSED)
188 *val *= 1000;
189 break;
190 case IIO_CHAN_INFO_SCALE:
191 *val = 1000;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000192 break;
193 default:
194 return -EINVAL;
195 }
196
197 return IIO_VAL_INT;
198}
199
200static const struct iio_info as3935_info = {
201 .driver_module = THIS_MODULE,
202 .attrs = &as3935_attribute_group,
203 .read_raw = &as3935_read_raw,
204};
205
206static irqreturn_t as3935_trigger_handler(int irq, void *private)
207{
208 struct iio_poll_func *pf = private;
209 struct iio_dev *indio_dev = pf->indio_dev;
210 struct as3935_state *st = iio_priv(indio_dev);
211 int val, ret;
212
213 ret = as3935_read(st, AS3935_DATA, &val);
214 if (ret)
215 goto err_read;
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000216
Matt Ranostay37b1ba22016-05-21 20:01:03 -0700217 st->buffer[0] = val & AS3935_DATA_MASK;
218 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
219 pf->timestamp);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000220err_read:
221 iio_trigger_notify_done(indio_dev->trig);
222
223 return IRQ_HANDLED;
George McCollister455f0042014-10-31 10:44:23 -0500224}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000225
226static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
227 .owner = THIS_MODULE,
228};
229
230static void as3935_event_work(struct work_struct *work)
231{
232 struct as3935_state *st;
233 int val;
234
235 st = container_of(work, struct as3935_state, work.work);
236
237 as3935_read(st, AS3935_INT, &val);
238 val &= AS3935_INT_MASK;
239
240 switch (val) {
241 case AS3935_EVENT_INT:
Peter Meerwald398fd222014-12-06 06:46:00 +0000242 iio_trigger_poll(st->trig);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000243 break;
244 case AS3935_NOISE_INT:
245 dev_warn(&st->spi->dev, "noise level is too high");
246 break;
247 }
George McCollister455f0042014-10-31 10:44:23 -0500248}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000249
250static irqreturn_t as3935_interrupt_handler(int irq, void *private)
251{
252 struct iio_dev *indio_dev = private;
253 struct as3935_state *st = iio_priv(indio_dev);
254
255 /*
256 * Delay work for >2 milliseconds after an interrupt to allow
257 * estimated distance to recalculated.
258 */
259
260 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
261
262 return IRQ_HANDLED;
263}
264
265static void calibrate_as3935(struct as3935_state *st)
266{
267 mutex_lock(&st->lock);
268
269 /* mask disturber interrupt bit */
270 as3935_write(st, AS3935_INT, BIT(5));
271
272 as3935_write(st, AS3935_CALIBRATE, 0x96);
273 as3935_write(st, AS3935_TUNE_CAP,
274 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
275
276 mdelay(2);
277 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
278
279 mutex_unlock(&st->lock);
280}
281
282#ifdef CONFIG_PM_SLEEP
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100283static int as3935_suspend(struct device *dev)
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000284{
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100285 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000286 struct as3935_state *st = iio_priv(indio_dev);
287 int val, ret;
288
289 mutex_lock(&st->lock);
290 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
291 if (ret)
292 goto err_suspend;
293 val |= AS3935_AFE_PWR_BIT;
294
295 ret = as3935_write(st, AS3935_AFE_GAIN, val);
296
297err_suspend:
298 mutex_unlock(&st->lock);
299
300 return ret;
301}
302
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100303static int as3935_resume(struct device *dev)
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000304{
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100305 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000306 struct as3935_state *st = iio_priv(indio_dev);
307 int val, ret;
308
309 mutex_lock(&st->lock);
310 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
311 if (ret)
312 goto err_resume;
313 val &= ~AS3935_AFE_PWR_BIT;
314 ret = as3935_write(st, AS3935_AFE_GAIN, val);
315
316err_resume:
317 mutex_unlock(&st->lock);
318
319 return ret;
320}
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100321
322static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
323#define AS3935_PM_OPS (&as3935_pm_ops)
324
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000325#else
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100326#define AS3935_PM_OPS NULL
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000327#endif
328
329static int as3935_probe(struct spi_device *spi)
330{
331 struct iio_dev *indio_dev;
332 struct iio_trigger *trig;
333 struct as3935_state *st;
334 struct device_node *np = spi->dev.of_node;
335 int ret;
336
337 /* Be sure lightning event interrupt is specified */
338 if (!spi->irq) {
339 dev_err(&spi->dev, "unable to get event interrupt\n");
340 return -EINVAL;
341 }
342
George McCollisterf73cde62014-10-31 15:44:00 +0000343 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000344 if (!indio_dev)
345 return -ENOMEM;
346
347 st = iio_priv(indio_dev);
348 st->spi = spi;
349 st->tune_cap = 0;
350
351 spi_set_drvdata(spi, indio_dev);
352 mutex_init(&st->lock);
353 INIT_DELAYED_WORK(&st->work, as3935_event_work);
354
355 ret = of_property_read_u32(np,
356 "ams,tuning-capacitor-pf", &st->tune_cap);
357 if (ret) {
358 st->tune_cap = 0;
359 dev_warn(&spi->dev,
360 "no tuning-capacitor-pf set, defaulting to %d",
361 st->tune_cap);
362 }
363
364 if (st->tune_cap > MAX_PF_CAP) {
365 dev_err(&spi->dev,
366 "wrong tuning-capacitor-pf setting of %d\n",
367 st->tune_cap);
368 return -EINVAL;
369 }
370
371 indio_dev->dev.parent = &spi->dev;
372 indio_dev->name = spi_get_device_id(spi)->name;
373 indio_dev->channels = as3935_channels;
374 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
375 indio_dev->modes = INDIO_DIRECT_MODE;
376 indio_dev->info = &as3935_info;
377
378 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
379 indio_dev->name, indio_dev->id);
380
381 if (!trig)
382 return -ENOMEM;
383
384 st->trig = trig;
385 trig->dev.parent = indio_dev->dev.parent;
386 iio_trigger_set_drvdata(trig, indio_dev);
387 trig->ops = &iio_interrupt_trigger_ops;
388
389 ret = iio_trigger_register(trig);
390 if (ret) {
391 dev_err(&spi->dev, "failed to register trigger\n");
392 return ret;
393 }
394
395 ret = iio_triggered_buffer_setup(indio_dev, NULL,
396 &as3935_trigger_handler, NULL);
397
398 if (ret) {
399 dev_err(&spi->dev, "cannot setup iio trigger\n");
400 goto unregister_trigger;
401 }
402
403 calibrate_as3935(st);
404
405 ret = devm_request_irq(&spi->dev, spi->irq,
406 &as3935_interrupt_handler,
407 IRQF_TRIGGER_RISING,
408 dev_name(&spi->dev),
409 indio_dev);
410
411 if (ret) {
412 dev_err(&spi->dev, "unable to request irq\n");
413 goto unregister_buffer;
414 }
415
416 ret = iio_device_register(indio_dev);
417 if (ret < 0) {
418 dev_err(&spi->dev, "unable to register device\n");
419 goto unregister_buffer;
420 }
421 return 0;
422
423unregister_buffer:
424 iio_triggered_buffer_cleanup(indio_dev);
425
426unregister_trigger:
427 iio_trigger_unregister(st->trig);
428
429 return ret;
George McCollister455f0042014-10-31 10:44:23 -0500430}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000431
432static int as3935_remove(struct spi_device *spi)
433{
434 struct iio_dev *indio_dev = spi_get_drvdata(spi);
435 struct as3935_state *st = iio_priv(indio_dev);
436
437 iio_device_unregister(indio_dev);
438 iio_triggered_buffer_cleanup(indio_dev);
439 iio_trigger_unregister(st->trig);
440
441 return 0;
George McCollister455f0042014-10-31 10:44:23 -0500442}
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000443
Javier Martinez Canillas8b7c8262015-08-20 09:07:27 +0200444static const struct of_device_id as3935_of_match[] = {
445 { .compatible = "ams,as3935", },
446 { /* sentinel */ },
447};
448MODULE_DEVICE_TABLE(of, as3935_of_match);
449
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000450static const struct spi_device_id as3935_id[] = {
451 {"as3935", 0},
452 {},
453};
454MODULE_DEVICE_TABLE(spi, as3935_id);
455
456static struct spi_driver as3935_driver = {
457 .driver = {
458 .name = "as3935",
Javier Martinez Canillas8b7c8262015-08-20 09:07:27 +0200459 .of_match_table = of_match_ptr(as3935_of_match),
Lars-Peter Clausen9d9f7802015-01-11 15:37:04 +0100460 .pm = AS3935_PM_OPS,
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000461 },
462 .probe = as3935_probe,
463 .remove = as3935_remove,
464 .id_table = as3935_id,
Matt Ranostay24ddb0e2014-12-03 12:53:00 +0000465};
466module_spi_driver(as3935_driver);
467
468MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
469MODULE_DESCRIPTION("AS3935 lightning sensor");
470MODULE_LICENSE("GPL");
471MODULE_ALIAS("spi:as3935");