1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * as3935.c - Support for AS3935 Franklin lightning sensor
4 *
5 * Copyright (C) 2014, 2017-2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7 */
8
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>
26
27
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)
32
33 #define AS3935_NFLWDTH 0x01
34 #define AS3935_NFLWDTH_MASK 0x7f
35
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)
41
42 #define AS3935_DATA 0x07
43 #define AS3935_DATA_MASK 0x3F
44
45 #define AS3935_TUNE_CAP 0x08
46 #define AS3935_DEFAULTS 0x3C
47 #define AS3935_CALIBRATE 0x3D
48
49 #define AS3935_READ_DATA BIT(14)
50 #define AS3935_ADDRESS(x) ((x) << 8)
51
52 #define MAX_PF_CAP 120
53 #define TUNE_CAP_DIV 8
54
55 struct as3935_state {
56 struct spi_device *spi;
57 struct iio_trigger *trig;
58 struct mutex lock;
59 struct delayed_work work;
60
61 unsigned long noise_tripped;
62 u32 tune_cap;
63 u32 nflwdth_reg;
64 /* Ensure timestamp is naturally aligned */
65 struct {
66 u8 chan;
67 s64 timestamp __aligned(8);
68 } scan;
69 u8 buf[2] ____cacheline_aligned;
70 };
71
72 static const struct iio_chan_spec as3935_channels[] = {
73 {
74 .type = IIO_PROXIMITY,
75 .info_mask_separate =
76 BIT(IIO_CHAN_INFO_RAW) |
77 BIT(IIO_CHAN_INFO_PROCESSED) |
78 BIT(IIO_CHAN_INFO_SCALE),
79 .scan_index = 0,
80 .scan_type = {
81 .sign = 'u',
82 .realbits = 6,
83 .storagebits = 8,
84 },
85 },
86 IIO_CHAN_SOFT_TIMESTAMP(1),
87 };
88
as3935_read(struct as3935_state * st,unsigned int reg,int * val)89 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
90 {
91 u8 cmd;
92 int ret;
93
94 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
95 ret = spi_w8r8(st->spi, cmd);
96 if (ret < 0)
97 return ret;
98 *val = ret;
99
100 return 0;
101 }
102
as3935_write(struct as3935_state * st,unsigned int reg,unsigned int val)103 static int as3935_write(struct as3935_state *st,
104 unsigned int reg,
105 unsigned int val)
106 {
107 u8 *buf = st->buf;
108
109 buf[0] = AS3935_ADDRESS(reg) >> 8;
110 buf[1] = val;
111
112 return spi_write(st->spi, buf, 2);
113 }
114
as3935_sensor_sensitivity_show(struct device * dev,struct device_attribute * attr,char * buf)115 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
116 struct device_attribute *attr,
117 char *buf)
118 {
119 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
120 int val, ret;
121
122 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
123 if (ret)
124 return ret;
125 val = (val & AS3935_AFE_MASK) >> 1;
126
127 return sprintf(buf, "%d\n", val);
128 }
129
as3935_sensor_sensitivity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)130 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t len)
133 {
134 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
135 unsigned long val;
136 int ret;
137
138 ret = kstrtoul((const char *) buf, 10, &val);
139 if (ret)
140 return -EINVAL;
141
142 if (val > AS3935_AFE_GAIN_MAX)
143 return -EINVAL;
144
145 as3935_write(st, AS3935_AFE_GAIN, val << 1);
146
147 return len;
148 }
149
as3935_noise_level_tripped_show(struct device * dev,struct device_attribute * attr,char * buf)150 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
153 {
154 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
155 int ret;
156
157 mutex_lock(&st->lock);
158 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
159 mutex_unlock(&st->lock);
160
161 return ret;
162 }
163
164 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
165 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
166
167 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
168 as3935_noise_level_tripped_show, NULL, 0);
169
170 static struct attribute *as3935_attributes[] = {
171 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
172 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
173 NULL,
174 };
175
176 static const struct attribute_group as3935_attribute_group = {
177 .attrs = as3935_attributes,
178 };
179
as3935_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)180 static int as3935_read_raw(struct iio_dev *indio_dev,
181 struct iio_chan_spec const *chan,
182 int *val,
183 int *val2,
184 long m)
185 {
186 struct as3935_state *st = iio_priv(indio_dev);
187 int ret;
188
189
190 switch (m) {
191 case IIO_CHAN_INFO_PROCESSED:
192 case IIO_CHAN_INFO_RAW:
193 *val2 = 0;
194 ret = as3935_read(st, AS3935_DATA, val);
195 if (ret)
196 return ret;
197
198 /* storm out of range */
199 if (*val == AS3935_DATA_MASK)
200 return -EINVAL;
201
202 if (m == IIO_CHAN_INFO_RAW)
203 return IIO_VAL_INT;
204
205 if (m == IIO_CHAN_INFO_PROCESSED)
206 *val *= 1000;
207 break;
208 case IIO_CHAN_INFO_SCALE:
209 *val = 1000;
210 break;
211 default:
212 return -EINVAL;
213 }
214
215 return IIO_VAL_INT;
216 }
217
218 static const struct iio_info as3935_info = {
219 .attrs = &as3935_attribute_group,
220 .read_raw = &as3935_read_raw,
221 };
222
as3935_trigger_handler(int irq,void * private)223 static irqreturn_t as3935_trigger_handler(int irq, void *private)
224 {
225 struct iio_poll_func *pf = private;
226 struct iio_dev *indio_dev = pf->indio_dev;
227 struct as3935_state *st = iio_priv(indio_dev);
228 int val, ret;
229
230 ret = as3935_read(st, AS3935_DATA, &val);
231 if (ret)
232 goto err_read;
233
234 st->scan.chan = val & AS3935_DATA_MASK;
235 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan,
236 iio_get_time_ns(indio_dev));
237 err_read:
238 iio_trigger_notify_done(indio_dev->trig);
239
240 return IRQ_HANDLED;
241 }
242
243 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
244 };
245
as3935_event_work(struct work_struct * work)246 static void as3935_event_work(struct work_struct *work)
247 {
248 struct as3935_state *st;
249 int val;
250 int ret;
251
252 st = container_of(work, struct as3935_state, work.work);
253
254 ret = as3935_read(st, AS3935_INT, &val);
255 if (ret) {
256 dev_warn(&st->spi->dev, "read error\n");
257 return;
258 }
259
260 val &= AS3935_INT_MASK;
261
262 switch (val) {
263 case AS3935_EVENT_INT:
264 iio_trigger_poll_chained(st->trig);
265 break;
266 case AS3935_DISTURB_INT:
267 case AS3935_NOISE_INT:
268 mutex_lock(&st->lock);
269 st->noise_tripped = jiffies;
270 mutex_unlock(&st->lock);
271 dev_warn(&st->spi->dev, "noise level is too high\n");
272 break;
273 }
274 }
275
as3935_interrupt_handler(int irq,void * private)276 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
277 {
278 struct iio_dev *indio_dev = private;
279 struct as3935_state *st = iio_priv(indio_dev);
280
281 /*
282 * Delay work for >2 milliseconds after an interrupt to allow
283 * estimated distance to recalculated.
284 */
285
286 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
287
288 return IRQ_HANDLED;
289 }
290
calibrate_as3935(struct as3935_state * st)291 static void calibrate_as3935(struct as3935_state *st)
292 {
293 as3935_write(st, AS3935_DEFAULTS, 0x96);
294 as3935_write(st, AS3935_CALIBRATE, 0x96);
295 as3935_write(st, AS3935_TUNE_CAP,
296 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
297
298 mdelay(2);
299 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
300 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
301 }
302
303 #ifdef CONFIG_PM_SLEEP
as3935_suspend(struct device * dev)304 static int as3935_suspend(struct device *dev)
305 {
306 struct iio_dev *indio_dev = dev_get_drvdata(dev);
307 struct as3935_state *st = iio_priv(indio_dev);
308 int val, ret;
309
310 mutex_lock(&st->lock);
311 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
312 if (ret)
313 goto err_suspend;
314 val |= AS3935_AFE_PWR_BIT;
315
316 ret = as3935_write(st, AS3935_AFE_GAIN, val);
317
318 err_suspend:
319 mutex_unlock(&st->lock);
320
321 return ret;
322 }
323
as3935_resume(struct device * dev)324 static int as3935_resume(struct device *dev)
325 {
326 struct iio_dev *indio_dev = dev_get_drvdata(dev);
327 struct as3935_state *st = iio_priv(indio_dev);
328 int val, ret;
329
330 mutex_lock(&st->lock);
331 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
332 if (ret)
333 goto err_resume;
334 val &= ~AS3935_AFE_PWR_BIT;
335 ret = as3935_write(st, AS3935_AFE_GAIN, val);
336
337 calibrate_as3935(st);
338
339 err_resume:
340 mutex_unlock(&st->lock);
341
342 return ret;
343 }
344
345 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
346 #define AS3935_PM_OPS (&as3935_pm_ops)
347
348 #else
349 #define AS3935_PM_OPS NULL
350 #endif
351
as3935_stop_work(void * data)352 static void as3935_stop_work(void *data)
353 {
354 struct iio_dev *indio_dev = data;
355 struct as3935_state *st = iio_priv(indio_dev);
356
357 cancel_delayed_work_sync(&st->work);
358 }
359
as3935_probe(struct spi_device * spi)360 static int as3935_probe(struct spi_device *spi)
361 {
362 struct iio_dev *indio_dev;
363 struct iio_trigger *trig;
364 struct as3935_state *st;
365 struct device_node *np = spi->dev.of_node;
366 int ret;
367
368 /* Be sure lightning event interrupt is specified */
369 if (!spi->irq) {
370 dev_err(&spi->dev, "unable to get event interrupt\n");
371 return -EINVAL;
372 }
373
374 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
375 if (!indio_dev)
376 return -ENOMEM;
377
378 st = iio_priv(indio_dev);
379 st->spi = spi;
380
381 spi_set_drvdata(spi, indio_dev);
382 mutex_init(&st->lock);
383
384 ret = of_property_read_u32(np,
385 "ams,tuning-capacitor-pf", &st->tune_cap);
386 if (ret) {
387 st->tune_cap = 0;
388 dev_warn(&spi->dev,
389 "no tuning-capacitor-pf set, defaulting to %d",
390 st->tune_cap);
391 }
392
393 if (st->tune_cap > MAX_PF_CAP) {
394 dev_err(&spi->dev,
395 "wrong tuning-capacitor-pf setting of %d\n",
396 st->tune_cap);
397 return -EINVAL;
398 }
399
400 ret = of_property_read_u32(np,
401 "ams,nflwdth", &st->nflwdth_reg);
402 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
403 dev_err(&spi->dev,
404 "invalid nflwdth setting of %d\n",
405 st->nflwdth_reg);
406 return -EINVAL;
407 }
408
409 indio_dev->dev.parent = &spi->dev;
410 indio_dev->name = spi_get_device_id(spi)->name;
411 indio_dev->channels = as3935_channels;
412 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
413 indio_dev->modes = INDIO_DIRECT_MODE;
414 indio_dev->info = &as3935_info;
415
416 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
417 indio_dev->name, indio_dev->id);
418
419 if (!trig)
420 return -ENOMEM;
421
422 st->trig = trig;
423 st->noise_tripped = jiffies - HZ;
424 trig->dev.parent = indio_dev->dev.parent;
425 iio_trigger_set_drvdata(trig, indio_dev);
426 trig->ops = &iio_interrupt_trigger_ops;
427
428 ret = devm_iio_trigger_register(&spi->dev, trig);
429 if (ret) {
430 dev_err(&spi->dev, "failed to register trigger\n");
431 return ret;
432 }
433
434 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
435 iio_pollfunc_store_time,
436 as3935_trigger_handler, NULL);
437
438 if (ret) {
439 dev_err(&spi->dev, "cannot setup iio trigger\n");
440 return ret;
441 }
442
443 calibrate_as3935(st);
444
445 INIT_DELAYED_WORK(&st->work, as3935_event_work);
446 ret = devm_add_action(&spi->dev, as3935_stop_work, indio_dev);
447 if (ret)
448 return ret;
449
450 ret = devm_request_irq(&spi->dev, spi->irq,
451 &as3935_interrupt_handler,
452 IRQF_TRIGGER_RISING,
453 dev_name(&spi->dev),
454 indio_dev);
455
456 if (ret) {
457 dev_err(&spi->dev, "unable to request irq\n");
458 return ret;
459 }
460
461 ret = devm_iio_device_register(&spi->dev, indio_dev);
462 if (ret < 0) {
463 dev_err(&spi->dev, "unable to register device\n");
464 return ret;
465 }
466 return 0;
467 }
468
469 static const struct of_device_id as3935_of_match[] = {
470 { .compatible = "ams,as3935", },
471 { /* sentinel */ },
472 };
473 MODULE_DEVICE_TABLE(of, as3935_of_match);
474
475 static const struct spi_device_id as3935_id[] = {
476 {"as3935", 0},
477 {},
478 };
479 MODULE_DEVICE_TABLE(spi, as3935_id);
480
481 static struct spi_driver as3935_driver = {
482 .driver = {
483 .name = "as3935",
484 .of_match_table = of_match_ptr(as3935_of_match),
485 .pm = AS3935_PM_OPS,
486 },
487 .probe = as3935_probe,
488 .id_table = as3935_id,
489 };
490 module_spi_driver(as3935_driver);
491
492 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
493 MODULE_DESCRIPTION("AS3935 lightning sensor");
494 MODULE_LICENSE("GPL");
495