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