Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / iio / proximity / as3935.c
blobb6249af48014a4dfdbf7b9c876272c73935b3c20
1 /*
2 * as3935.c - Support for AS3935 Franklin lightning sensor
4 * Copyright (C) 2014 Matt Ranostay <mranostay@gmail.com>
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.
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.
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>
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)
42 #define AS3935_NFLWDTH 0x01
43 #define AS3935_NFLWDTH_MASK 0x7f
45 #define AS3935_INT 0x03
46 #define AS3935_INT_MASK 0x0f
47 #define AS3935_DISTURB_INT BIT(2)
48 #define AS3935_EVENT_INT BIT(3)
49 #define AS3935_NOISE_INT BIT(0)
51 #define AS3935_DATA 0x07
52 #define AS3935_DATA_MASK 0x3F
54 #define AS3935_TUNE_CAP 0x08
55 #define AS3935_DEFAULTS 0x3C
56 #define AS3935_CALIBRATE 0x3D
58 #define AS3935_READ_DATA BIT(14)
59 #define AS3935_ADDRESS(x) ((x) << 8)
61 #define MAX_PF_CAP 120
62 #define TUNE_CAP_DIV 8
64 struct as3935_state {
65 struct spi_device *spi;
66 struct iio_trigger *trig;
67 struct mutex lock;
68 struct delayed_work work;
70 unsigned long noise_tripped;
71 u32 tune_cap;
72 u32 nflwdth_reg;
73 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
74 u8 buf[2] ____cacheline_aligned;
77 static const struct iio_chan_spec as3935_channels[] = {
79 .type = IIO_PROXIMITY,
80 .info_mask_separate =
81 BIT(IIO_CHAN_INFO_RAW) |
82 BIT(IIO_CHAN_INFO_PROCESSED) |
83 BIT(IIO_CHAN_INFO_SCALE),
84 .scan_index = 0,
85 .scan_type = {
86 .sign = 'u',
87 .realbits = 6,
88 .storagebits = 8,
91 IIO_CHAN_SOFT_TIMESTAMP(1),
94 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
96 u8 cmd;
97 int ret;
99 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
100 ret = spi_w8r8(st->spi, cmd);
101 if (ret < 0)
102 return ret;
103 *val = ret;
105 return 0;
108 static int as3935_write(struct as3935_state *st,
109 unsigned int reg,
110 unsigned int val)
112 u8 *buf = st->buf;
114 buf[0] = AS3935_ADDRESS(reg) >> 8;
115 buf[1] = val;
117 return spi_write(st->spi, buf, 2);
120 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
121 struct device_attribute *attr,
122 char *buf)
124 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
125 int val, ret;
127 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
128 if (ret)
129 return ret;
130 val = (val & AS3935_AFE_MASK) >> 1;
132 return sprintf(buf, "%d\n", val);
135 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
136 struct device_attribute *attr,
137 const char *buf, size_t len)
139 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
140 unsigned long val;
141 int ret;
143 ret = kstrtoul((const char *) buf, 10, &val);
144 if (ret)
145 return -EINVAL;
147 if (val > AS3935_AFE_GAIN_MAX)
148 return -EINVAL;
150 as3935_write(st, AS3935_AFE_GAIN, val << 1);
152 return len;
155 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
159 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
160 int ret;
162 mutex_lock(&st->lock);
163 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
164 mutex_unlock(&st->lock);
166 return ret;
169 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
170 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
172 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
173 as3935_noise_level_tripped_show, NULL, 0);
175 static struct attribute *as3935_attributes[] = {
176 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
177 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
178 NULL,
181 static const struct attribute_group as3935_attribute_group = {
182 .attrs = as3935_attributes,
185 static int as3935_read_raw(struct iio_dev *indio_dev,
186 struct iio_chan_spec const *chan,
187 int *val,
188 int *val2,
189 long m)
191 struct as3935_state *st = iio_priv(indio_dev);
192 int ret;
195 switch (m) {
196 case IIO_CHAN_INFO_PROCESSED:
197 case IIO_CHAN_INFO_RAW:
198 *val2 = 0;
199 ret = as3935_read(st, AS3935_DATA, val);
200 if (ret)
201 return ret;
203 /* storm out of range */
204 if (*val == AS3935_DATA_MASK)
205 return -EINVAL;
207 if (m == IIO_CHAN_INFO_RAW)
208 return IIO_VAL_INT;
210 if (m == IIO_CHAN_INFO_PROCESSED)
211 *val *= 1000;
212 break;
213 case IIO_CHAN_INFO_SCALE:
214 *val = 1000;
215 break;
216 default:
217 return -EINVAL;
220 return IIO_VAL_INT;
223 static const struct iio_info as3935_info = {
224 .attrs = &as3935_attribute_group,
225 .read_raw = &as3935_read_raw,
228 static irqreturn_t as3935_trigger_handler(int irq, void *private)
230 struct iio_poll_func *pf = private;
231 struct iio_dev *indio_dev = pf->indio_dev;
232 struct as3935_state *st = iio_priv(indio_dev);
233 int val, ret;
235 ret = as3935_read(st, AS3935_DATA, &val);
236 if (ret)
237 goto err_read;
239 st->buffer[0] = val & AS3935_DATA_MASK;
240 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
241 iio_get_time_ns(indio_dev));
242 err_read:
243 iio_trigger_notify_done(indio_dev->trig);
245 return IRQ_HANDLED;
248 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
251 static void as3935_event_work(struct work_struct *work)
253 struct as3935_state *st;
254 int val;
255 int ret;
257 st = container_of(work, struct as3935_state, work.work);
259 ret = as3935_read(st, AS3935_INT, &val);
260 if (ret) {
261 dev_warn(&st->spi->dev, "read error\n");
262 return;
265 val &= AS3935_INT_MASK;
267 switch (val) {
268 case AS3935_EVENT_INT:
269 iio_trigger_poll_chained(st->trig);
270 break;
271 case AS3935_DISTURB_INT:
272 case AS3935_NOISE_INT:
273 mutex_lock(&st->lock);
274 st->noise_tripped = jiffies;
275 mutex_unlock(&st->lock);
276 dev_warn(&st->spi->dev, "noise level is too high\n");
277 break;
281 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
283 struct iio_dev *indio_dev = private;
284 struct as3935_state *st = iio_priv(indio_dev);
287 * Delay work for >2 milliseconds after an interrupt to allow
288 * estimated distance to recalculated.
291 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
293 return IRQ_HANDLED;
296 static void calibrate_as3935(struct as3935_state *st)
298 as3935_write(st, AS3935_DEFAULTS, 0x96);
299 as3935_write(st, AS3935_CALIBRATE, 0x96);
300 as3935_write(st, AS3935_TUNE_CAP,
301 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
303 mdelay(2);
304 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
305 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
308 #ifdef CONFIG_PM_SLEEP
309 static int as3935_suspend(struct device *dev)
311 struct iio_dev *indio_dev = dev_get_drvdata(dev);
312 struct as3935_state *st = iio_priv(indio_dev);
313 int val, ret;
315 mutex_lock(&st->lock);
316 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
317 if (ret)
318 goto err_suspend;
319 val |= AS3935_AFE_PWR_BIT;
321 ret = as3935_write(st, AS3935_AFE_GAIN, val);
323 err_suspend:
324 mutex_unlock(&st->lock);
326 return ret;
329 static int as3935_resume(struct device *dev)
331 struct iio_dev *indio_dev = dev_get_drvdata(dev);
332 struct as3935_state *st = iio_priv(indio_dev);
333 int val, ret;
335 mutex_lock(&st->lock);
336 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
337 if (ret)
338 goto err_resume;
339 val &= ~AS3935_AFE_PWR_BIT;
340 ret = as3935_write(st, AS3935_AFE_GAIN, val);
342 calibrate_as3935(st);
344 err_resume:
345 mutex_unlock(&st->lock);
347 return ret;
350 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
351 #define AS3935_PM_OPS (&as3935_pm_ops)
353 #else
354 #define AS3935_PM_OPS NULL
355 #endif
357 static int as3935_probe(struct spi_device *spi)
359 struct iio_dev *indio_dev;
360 struct iio_trigger *trig;
361 struct as3935_state *st;
362 struct device_node *np = spi->dev.of_node;
363 int ret;
365 /* Be sure lightning event interrupt is specified */
366 if (!spi->irq) {
367 dev_err(&spi->dev, "unable to get event interrupt\n");
368 return -EINVAL;
371 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
372 if (!indio_dev)
373 return -ENOMEM;
375 st = iio_priv(indio_dev);
376 st->spi = spi;
378 spi_set_drvdata(spi, indio_dev);
379 mutex_init(&st->lock);
380 INIT_DELAYED_WORK(&st->work, as3935_event_work);
382 ret = of_property_read_u32(np,
383 "ams,tuning-capacitor-pf", &st->tune_cap);
384 if (ret) {
385 st->tune_cap = 0;
386 dev_warn(&spi->dev,
387 "no tuning-capacitor-pf set, defaulting to %d",
388 st->tune_cap);
391 if (st->tune_cap > MAX_PF_CAP) {
392 dev_err(&spi->dev,
393 "wrong tuning-capacitor-pf setting of %d\n",
394 st->tune_cap);
395 return -EINVAL;
398 ret = of_property_read_u32(np,
399 "ams,nflwdth", &st->nflwdth_reg);
400 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
401 dev_err(&spi->dev,
402 "invalid nflwdth setting of %d\n",
403 st->nflwdth_reg);
404 return -EINVAL;
407 indio_dev->dev.parent = &spi->dev;
408 indio_dev->name = spi_get_device_id(spi)->name;
409 indio_dev->channels = as3935_channels;
410 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
411 indio_dev->modes = INDIO_DIRECT_MODE;
412 indio_dev->info = &as3935_info;
414 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
415 indio_dev->name, indio_dev->id);
417 if (!trig)
418 return -ENOMEM;
420 st->trig = trig;
421 st->noise_tripped = jiffies - HZ;
422 trig->dev.parent = indio_dev->dev.parent;
423 iio_trigger_set_drvdata(trig, indio_dev);
424 trig->ops = &iio_interrupt_trigger_ops;
426 ret = iio_trigger_register(trig);
427 if (ret) {
428 dev_err(&spi->dev, "failed to register trigger\n");
429 return ret;
432 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
433 &as3935_trigger_handler, NULL);
435 if (ret) {
436 dev_err(&spi->dev, "cannot setup iio trigger\n");
437 goto unregister_trigger;
440 calibrate_as3935(st);
442 ret = devm_request_irq(&spi->dev, spi->irq,
443 &as3935_interrupt_handler,
444 IRQF_TRIGGER_RISING,
445 dev_name(&spi->dev),
446 indio_dev);
448 if (ret) {
449 dev_err(&spi->dev, "unable to request irq\n");
450 goto unregister_buffer;
453 ret = iio_device_register(indio_dev);
454 if (ret < 0) {
455 dev_err(&spi->dev, "unable to register device\n");
456 goto unregister_buffer;
458 return 0;
460 unregister_buffer:
461 iio_triggered_buffer_cleanup(indio_dev);
463 unregister_trigger:
464 iio_trigger_unregister(st->trig);
466 return ret;
469 static int as3935_remove(struct spi_device *spi)
471 struct iio_dev *indio_dev = spi_get_drvdata(spi);
472 struct as3935_state *st = iio_priv(indio_dev);
474 iio_device_unregister(indio_dev);
475 iio_triggered_buffer_cleanup(indio_dev);
476 iio_trigger_unregister(st->trig);
478 return 0;
481 static const struct of_device_id as3935_of_match[] = {
482 { .compatible = "ams,as3935", },
483 { /* sentinel */ },
485 MODULE_DEVICE_TABLE(of, as3935_of_match);
487 static const struct spi_device_id as3935_id[] = {
488 {"as3935", 0},
491 MODULE_DEVICE_TABLE(spi, as3935_id);
493 static struct spi_driver as3935_driver = {
494 .driver = {
495 .name = "as3935",
496 .of_match_table = of_match_ptr(as3935_of_match),
497 .pm = AS3935_PM_OPS,
499 .probe = as3935_probe,
500 .remove = as3935_remove,
501 .id_table = as3935_id,
503 module_spi_driver(as3935_driver);
505 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
506 MODULE_DESCRIPTION("AS3935 lightning sensor");
507 MODULE_LICENSE("GPL");