Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / proximity / as3935.c
blobf130388a16a0cbbcf72d1bb99a3bd46caa9382a8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * as3935.c - Support for AS3935 Franklin lightning sensor
5 * Copyright (C) 2014, 2017-2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7 */
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>
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)
33 #define AS3935_NFLWDTH 0x01
34 #define AS3935_NFLWDTH_MASK 0x7f
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)
42 #define AS3935_DATA 0x07
43 #define AS3935_DATA_MASK 0x3F
45 #define AS3935_TUNE_CAP 0x08
46 #define AS3935_DEFAULTS 0x3C
47 #define AS3935_CALIBRATE 0x3D
49 #define AS3935_READ_DATA BIT(14)
50 #define AS3935_ADDRESS(x) ((x) << 8)
52 #define MAX_PF_CAP 120
53 #define TUNE_CAP_DIV 8
55 struct as3935_state {
56 struct spi_device *spi;
57 struct iio_trigger *trig;
58 struct mutex lock;
59 struct delayed_work work;
61 unsigned long noise_tripped;
62 u32 tune_cap;
63 u32 nflwdth_reg;
64 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
65 u8 buf[2] ____cacheline_aligned;
68 static const struct iio_chan_spec as3935_channels[] = {
70 .type = IIO_PROXIMITY,
71 .info_mask_separate =
72 BIT(IIO_CHAN_INFO_RAW) |
73 BIT(IIO_CHAN_INFO_PROCESSED) |
74 BIT(IIO_CHAN_INFO_SCALE),
75 .scan_index = 0,
76 .scan_type = {
77 .sign = 'u',
78 .realbits = 6,
79 .storagebits = 8,
82 IIO_CHAN_SOFT_TIMESTAMP(1),
85 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
87 u8 cmd;
88 int ret;
90 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
91 ret = spi_w8r8(st->spi, cmd);
92 if (ret < 0)
93 return ret;
94 *val = ret;
96 return 0;
99 static int as3935_write(struct as3935_state *st,
100 unsigned int reg,
101 unsigned int val)
103 u8 *buf = st->buf;
105 buf[0] = AS3935_ADDRESS(reg) >> 8;
106 buf[1] = val;
108 return spi_write(st->spi, buf, 2);
111 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
112 struct device_attribute *attr,
113 char *buf)
115 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
116 int val, ret;
118 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
119 if (ret)
120 return ret;
121 val = (val & AS3935_AFE_MASK) >> 1;
123 return sprintf(buf, "%d\n", val);
126 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t len)
130 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
131 unsigned long val;
132 int ret;
134 ret = kstrtoul((const char *) buf, 10, &val);
135 if (ret)
136 return -EINVAL;
138 if (val > AS3935_AFE_GAIN_MAX)
139 return -EINVAL;
141 as3935_write(st, AS3935_AFE_GAIN, val << 1);
143 return len;
146 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
147 struct device_attribute *attr,
148 char *buf)
150 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
151 int ret;
153 mutex_lock(&st->lock);
154 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
155 mutex_unlock(&st->lock);
157 return ret;
160 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
161 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
163 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
164 as3935_noise_level_tripped_show, NULL, 0);
166 static struct attribute *as3935_attributes[] = {
167 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
168 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
169 NULL,
172 static const struct attribute_group as3935_attribute_group = {
173 .attrs = as3935_attributes,
176 static int as3935_read_raw(struct iio_dev *indio_dev,
177 struct iio_chan_spec const *chan,
178 int *val,
179 int *val2,
180 long m)
182 struct as3935_state *st = iio_priv(indio_dev);
183 int ret;
186 switch (m) {
187 case IIO_CHAN_INFO_PROCESSED:
188 case IIO_CHAN_INFO_RAW:
189 *val2 = 0;
190 ret = as3935_read(st, AS3935_DATA, val);
191 if (ret)
192 return ret;
194 /* storm out of range */
195 if (*val == AS3935_DATA_MASK)
196 return -EINVAL;
198 if (m == IIO_CHAN_INFO_RAW)
199 return IIO_VAL_INT;
201 if (m == IIO_CHAN_INFO_PROCESSED)
202 *val *= 1000;
203 break;
204 case IIO_CHAN_INFO_SCALE:
205 *val = 1000;
206 break;
207 default:
208 return -EINVAL;
211 return IIO_VAL_INT;
214 static const struct iio_info as3935_info = {
215 .attrs = &as3935_attribute_group,
216 .read_raw = &as3935_read_raw,
219 static irqreturn_t as3935_trigger_handler(int irq, void *private)
221 struct iio_poll_func *pf = private;
222 struct iio_dev *indio_dev = pf->indio_dev;
223 struct as3935_state *st = iio_priv(indio_dev);
224 int val, ret;
226 ret = as3935_read(st, AS3935_DATA, &val);
227 if (ret)
228 goto err_read;
230 st->buffer[0] = val & AS3935_DATA_MASK;
231 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
232 iio_get_time_ns(indio_dev));
233 err_read:
234 iio_trigger_notify_done(indio_dev->trig);
236 return IRQ_HANDLED;
239 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
242 static void as3935_event_work(struct work_struct *work)
244 struct as3935_state *st;
245 int val;
246 int ret;
248 st = container_of(work, struct as3935_state, work.work);
250 ret = as3935_read(st, AS3935_INT, &val);
251 if (ret) {
252 dev_warn(&st->spi->dev, "read error\n");
253 return;
256 val &= AS3935_INT_MASK;
258 switch (val) {
259 case AS3935_EVENT_INT:
260 iio_trigger_poll_chained(st->trig);
261 break;
262 case AS3935_DISTURB_INT:
263 case AS3935_NOISE_INT:
264 mutex_lock(&st->lock);
265 st->noise_tripped = jiffies;
266 mutex_unlock(&st->lock);
267 dev_warn(&st->spi->dev, "noise level is too high\n");
268 break;
272 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
274 struct iio_dev *indio_dev = private;
275 struct as3935_state *st = iio_priv(indio_dev);
278 * Delay work for >2 milliseconds after an interrupt to allow
279 * estimated distance to recalculated.
282 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
284 return IRQ_HANDLED;
287 static void calibrate_as3935(struct as3935_state *st)
289 as3935_write(st, AS3935_DEFAULTS, 0x96);
290 as3935_write(st, AS3935_CALIBRATE, 0x96);
291 as3935_write(st, AS3935_TUNE_CAP,
292 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
294 mdelay(2);
295 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
296 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
299 #ifdef CONFIG_PM_SLEEP
300 static int as3935_suspend(struct device *dev)
302 struct iio_dev *indio_dev = dev_get_drvdata(dev);
303 struct as3935_state *st = iio_priv(indio_dev);
304 int val, ret;
306 mutex_lock(&st->lock);
307 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
308 if (ret)
309 goto err_suspend;
310 val |= AS3935_AFE_PWR_BIT;
312 ret = as3935_write(st, AS3935_AFE_GAIN, val);
314 err_suspend:
315 mutex_unlock(&st->lock);
317 return ret;
320 static int as3935_resume(struct device *dev)
322 struct iio_dev *indio_dev = dev_get_drvdata(dev);
323 struct as3935_state *st = iio_priv(indio_dev);
324 int val, ret;
326 mutex_lock(&st->lock);
327 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
328 if (ret)
329 goto err_resume;
330 val &= ~AS3935_AFE_PWR_BIT;
331 ret = as3935_write(st, AS3935_AFE_GAIN, val);
333 calibrate_as3935(st);
335 err_resume:
336 mutex_unlock(&st->lock);
338 return ret;
341 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
342 #define AS3935_PM_OPS (&as3935_pm_ops)
344 #else
345 #define AS3935_PM_OPS NULL
346 #endif
348 static int as3935_probe(struct spi_device *spi)
350 struct iio_dev *indio_dev;
351 struct iio_trigger *trig;
352 struct as3935_state *st;
353 struct device_node *np = spi->dev.of_node;
354 int ret;
356 /* Be sure lightning event interrupt is specified */
357 if (!spi->irq) {
358 dev_err(&spi->dev, "unable to get event interrupt\n");
359 return -EINVAL;
362 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
363 if (!indio_dev)
364 return -ENOMEM;
366 st = iio_priv(indio_dev);
367 st->spi = spi;
369 spi_set_drvdata(spi, indio_dev);
370 mutex_init(&st->lock);
371 INIT_DELAYED_WORK(&st->work, as3935_event_work);
373 ret = of_property_read_u32(np,
374 "ams,tuning-capacitor-pf", &st->tune_cap);
375 if (ret) {
376 st->tune_cap = 0;
377 dev_warn(&spi->dev,
378 "no tuning-capacitor-pf set, defaulting to %d",
379 st->tune_cap);
382 if (st->tune_cap > MAX_PF_CAP) {
383 dev_err(&spi->dev,
384 "wrong tuning-capacitor-pf setting of %d\n",
385 st->tune_cap);
386 return -EINVAL;
389 ret = of_property_read_u32(np,
390 "ams,nflwdth", &st->nflwdth_reg);
391 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
392 dev_err(&spi->dev,
393 "invalid nflwdth setting of %d\n",
394 st->nflwdth_reg);
395 return -EINVAL;
398 indio_dev->dev.parent = &spi->dev;
399 indio_dev->name = spi_get_device_id(spi)->name;
400 indio_dev->channels = as3935_channels;
401 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
402 indio_dev->modes = INDIO_DIRECT_MODE;
403 indio_dev->info = &as3935_info;
405 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
406 indio_dev->name, indio_dev->id);
408 if (!trig)
409 return -ENOMEM;
411 st->trig = trig;
412 st->noise_tripped = jiffies - HZ;
413 trig->dev.parent = indio_dev->dev.parent;
414 iio_trigger_set_drvdata(trig, indio_dev);
415 trig->ops = &iio_interrupt_trigger_ops;
417 ret = iio_trigger_register(trig);
418 if (ret) {
419 dev_err(&spi->dev, "failed to register trigger\n");
420 return ret;
423 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
424 &as3935_trigger_handler, NULL);
426 if (ret) {
427 dev_err(&spi->dev, "cannot setup iio trigger\n");
428 goto unregister_trigger;
431 calibrate_as3935(st);
433 ret = devm_request_irq(&spi->dev, spi->irq,
434 &as3935_interrupt_handler,
435 IRQF_TRIGGER_RISING,
436 dev_name(&spi->dev),
437 indio_dev);
439 if (ret) {
440 dev_err(&spi->dev, "unable to request irq\n");
441 goto unregister_buffer;
444 ret = iio_device_register(indio_dev);
445 if (ret < 0) {
446 dev_err(&spi->dev, "unable to register device\n");
447 goto unregister_buffer;
449 return 0;
451 unregister_buffer:
452 iio_triggered_buffer_cleanup(indio_dev);
454 unregister_trigger:
455 iio_trigger_unregister(st->trig);
457 return ret;
460 static int as3935_remove(struct spi_device *spi)
462 struct iio_dev *indio_dev = spi_get_drvdata(spi);
463 struct as3935_state *st = iio_priv(indio_dev);
465 iio_device_unregister(indio_dev);
466 iio_triggered_buffer_cleanup(indio_dev);
467 iio_trigger_unregister(st->trig);
469 return 0;
472 static const struct of_device_id as3935_of_match[] = {
473 { .compatible = "ams,as3935", },
474 { /* sentinel */ },
476 MODULE_DEVICE_TABLE(of, as3935_of_match);
478 static const struct spi_device_id as3935_id[] = {
479 {"as3935", 0},
482 MODULE_DEVICE_TABLE(spi, as3935_id);
484 static struct spi_driver as3935_driver = {
485 .driver = {
486 .name = "as3935",
487 .of_match_table = of_match_ptr(as3935_of_match),
488 .pm = AS3935_PM_OPS,
490 .probe = as3935_probe,
491 .remove = as3935_remove,
492 .id_table = as3935_id,
494 module_spi_driver(as3935_driver);
496 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
497 MODULE_DESCRIPTION("AS3935 lightning sensor");
498 MODULE_LICENSE("GPL");