dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / proximity / as3935.c
blobbac9a433dd19a78002185c3fb70981d75c256253
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/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/triggered_buffer.h>
25 #define AS3935_AFE_GAIN 0x00
26 #define AS3935_AFE_MASK 0x3F
27 #define AS3935_AFE_GAIN_MAX 0x1F
28 #define AS3935_AFE_PWR_BIT BIT(0)
30 #define AS3935_NFLWDTH 0x01
31 #define AS3935_NFLWDTH_MASK 0x7f
33 #define AS3935_INT 0x03
34 #define AS3935_INT_MASK 0x0f
35 #define AS3935_DISTURB_INT BIT(2)
36 #define AS3935_EVENT_INT BIT(3)
37 #define AS3935_NOISE_INT BIT(0)
39 #define AS3935_DATA 0x07
40 #define AS3935_DATA_MASK 0x3F
42 #define AS3935_TUNE_CAP 0x08
43 #define AS3935_DEFAULTS 0x3C
44 #define AS3935_CALIBRATE 0x3D
46 #define AS3935_READ_DATA BIT(14)
47 #define AS3935_ADDRESS(x) ((x) << 8)
49 #define MAX_PF_CAP 120
50 #define TUNE_CAP_DIV 8
52 struct as3935_state {
53 struct spi_device *spi;
54 struct iio_trigger *trig;
55 struct mutex lock;
56 struct delayed_work work;
58 unsigned long noise_tripped;
59 u32 tune_cap;
60 u32 nflwdth_reg;
61 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
62 u8 buf[2] ____cacheline_aligned;
65 static const struct iio_chan_spec as3935_channels[] = {
67 .type = IIO_PROXIMITY,
68 .info_mask_separate =
69 BIT(IIO_CHAN_INFO_RAW) |
70 BIT(IIO_CHAN_INFO_PROCESSED) |
71 BIT(IIO_CHAN_INFO_SCALE),
72 .scan_index = 0,
73 .scan_type = {
74 .sign = 'u',
75 .realbits = 6,
76 .storagebits = 8,
79 IIO_CHAN_SOFT_TIMESTAMP(1),
82 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
84 u8 cmd;
85 int ret;
87 cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
88 ret = spi_w8r8(st->spi, cmd);
89 if (ret < 0)
90 return ret;
91 *val = ret;
93 return 0;
96 static int as3935_write(struct as3935_state *st,
97 unsigned int reg,
98 unsigned int val)
100 u8 *buf = st->buf;
102 buf[0] = AS3935_ADDRESS(reg) >> 8;
103 buf[1] = val;
105 return spi_write(st->spi, buf, 2);
108 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
109 struct device_attribute *attr,
110 char *buf)
112 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
113 int val, ret;
115 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
116 if (ret)
117 return ret;
118 val = (val & AS3935_AFE_MASK) >> 1;
120 return sprintf(buf, "%d\n", val);
123 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
124 struct device_attribute *attr,
125 const char *buf, size_t len)
127 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
128 unsigned long val;
129 int ret;
131 ret = kstrtoul((const char *) buf, 10, &val);
132 if (ret)
133 return -EINVAL;
135 if (val > AS3935_AFE_GAIN_MAX)
136 return -EINVAL;
138 as3935_write(st, AS3935_AFE_GAIN, val << 1);
140 return len;
143 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
144 struct device_attribute *attr,
145 char *buf)
147 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
148 int ret;
150 mutex_lock(&st->lock);
151 ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
152 mutex_unlock(&st->lock);
154 return ret;
157 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
158 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
160 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
161 as3935_noise_level_tripped_show, NULL, 0);
163 static struct attribute *as3935_attributes[] = {
164 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
165 &iio_dev_attr_noise_level_tripped.dev_attr.attr,
166 NULL,
169 static const struct attribute_group as3935_attribute_group = {
170 .attrs = as3935_attributes,
173 static int as3935_read_raw(struct iio_dev *indio_dev,
174 struct iio_chan_spec const *chan,
175 int *val,
176 int *val2,
177 long m)
179 struct as3935_state *st = iio_priv(indio_dev);
180 int ret;
183 switch (m) {
184 case IIO_CHAN_INFO_PROCESSED:
185 case IIO_CHAN_INFO_RAW:
186 *val2 = 0;
187 ret = as3935_read(st, AS3935_DATA, val);
188 if (ret)
189 return ret;
191 /* storm out of range */
192 if (*val == AS3935_DATA_MASK)
193 return -EINVAL;
195 if (m == IIO_CHAN_INFO_RAW)
196 return IIO_VAL_INT;
198 if (m == IIO_CHAN_INFO_PROCESSED)
199 *val *= 1000;
200 break;
201 case IIO_CHAN_INFO_SCALE:
202 *val = 1000;
203 break;
204 default:
205 return -EINVAL;
208 return IIO_VAL_INT;
211 static const struct iio_info as3935_info = {
212 .attrs = &as3935_attribute_group,
213 .read_raw = &as3935_read_raw,
216 static irqreturn_t as3935_trigger_handler(int irq, void *private)
218 struct iio_poll_func *pf = private;
219 struct iio_dev *indio_dev = pf->indio_dev;
220 struct as3935_state *st = iio_priv(indio_dev);
221 int val, ret;
223 ret = as3935_read(st, AS3935_DATA, &val);
224 if (ret)
225 goto err_read;
227 st->buffer[0] = val & AS3935_DATA_MASK;
228 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
229 iio_get_time_ns(indio_dev));
230 err_read:
231 iio_trigger_notify_done(indio_dev->trig);
233 return IRQ_HANDLED;
236 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
239 static void as3935_event_work(struct work_struct *work)
241 struct as3935_state *st;
242 int val;
243 int ret;
245 st = container_of(work, struct as3935_state, work.work);
247 ret = as3935_read(st, AS3935_INT, &val);
248 if (ret) {
249 dev_warn(&st->spi->dev, "read error\n");
250 return;
253 val &= AS3935_INT_MASK;
255 switch (val) {
256 case AS3935_EVENT_INT:
257 iio_trigger_poll_chained(st->trig);
258 break;
259 case AS3935_DISTURB_INT:
260 case AS3935_NOISE_INT:
261 mutex_lock(&st->lock);
262 st->noise_tripped = jiffies;
263 mutex_unlock(&st->lock);
264 dev_warn(&st->spi->dev, "noise level is too high\n");
265 break;
269 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
271 struct iio_dev *indio_dev = private;
272 struct as3935_state *st = iio_priv(indio_dev);
275 * Delay work for >2 milliseconds after an interrupt to allow
276 * estimated distance to recalculated.
279 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
281 return IRQ_HANDLED;
284 static void calibrate_as3935(struct as3935_state *st)
286 as3935_write(st, AS3935_DEFAULTS, 0x96);
287 as3935_write(st, AS3935_CALIBRATE, 0x96);
288 as3935_write(st, AS3935_TUNE_CAP,
289 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
291 mdelay(2);
292 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
293 as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
296 #ifdef CONFIG_PM_SLEEP
297 static int as3935_suspend(struct device *dev)
299 struct iio_dev *indio_dev = dev_get_drvdata(dev);
300 struct as3935_state *st = iio_priv(indio_dev);
301 int val, ret;
303 mutex_lock(&st->lock);
304 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
305 if (ret)
306 goto err_suspend;
307 val |= AS3935_AFE_PWR_BIT;
309 ret = as3935_write(st, AS3935_AFE_GAIN, val);
311 err_suspend:
312 mutex_unlock(&st->lock);
314 return ret;
317 static int as3935_resume(struct device *dev)
319 struct iio_dev *indio_dev = dev_get_drvdata(dev);
320 struct as3935_state *st = iio_priv(indio_dev);
321 int val, ret;
323 mutex_lock(&st->lock);
324 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
325 if (ret)
326 goto err_resume;
327 val &= ~AS3935_AFE_PWR_BIT;
328 ret = as3935_write(st, AS3935_AFE_GAIN, val);
330 calibrate_as3935(st);
332 err_resume:
333 mutex_unlock(&st->lock);
335 return ret;
338 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
339 #define AS3935_PM_OPS (&as3935_pm_ops)
341 #else
342 #define AS3935_PM_OPS NULL
343 #endif
345 static void as3935_stop_work(void *data)
347 struct iio_dev *indio_dev = data;
348 struct as3935_state *st = iio_priv(indio_dev);
350 cancel_delayed_work_sync(&st->work);
353 static int as3935_probe(struct spi_device *spi)
355 struct iio_dev *indio_dev;
356 struct iio_trigger *trig;
357 struct as3935_state *st;
358 struct device_node *np = spi->dev.of_node;
359 int ret;
361 /* Be sure lightning event interrupt is specified */
362 if (!spi->irq) {
363 dev_err(&spi->dev, "unable to get event interrupt\n");
364 return -EINVAL;
367 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
368 if (!indio_dev)
369 return -ENOMEM;
371 st = iio_priv(indio_dev);
372 st->spi = spi;
374 spi_set_drvdata(spi, indio_dev);
375 mutex_init(&st->lock);
377 ret = of_property_read_u32(np,
378 "ams,tuning-capacitor-pf", &st->tune_cap);
379 if (ret) {
380 st->tune_cap = 0;
381 dev_warn(&spi->dev,
382 "no tuning-capacitor-pf set, defaulting to %d",
383 st->tune_cap);
386 if (st->tune_cap > MAX_PF_CAP) {
387 dev_err(&spi->dev,
388 "wrong tuning-capacitor-pf setting of %d\n",
389 st->tune_cap);
390 return -EINVAL;
393 ret = of_property_read_u32(np,
394 "ams,nflwdth", &st->nflwdth_reg);
395 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
396 dev_err(&spi->dev,
397 "invalid nflwdth setting of %d\n",
398 st->nflwdth_reg);
399 return -EINVAL;
402 indio_dev->dev.parent = &spi->dev;
403 indio_dev->name = spi_get_device_id(spi)->name;
404 indio_dev->channels = as3935_channels;
405 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
406 indio_dev->modes = INDIO_DIRECT_MODE;
407 indio_dev->info = &as3935_info;
409 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
410 indio_dev->name, indio_dev->id);
412 if (!trig)
413 return -ENOMEM;
415 st->trig = trig;
416 st->noise_tripped = jiffies - HZ;
417 trig->dev.parent = indio_dev->dev.parent;
418 iio_trigger_set_drvdata(trig, indio_dev);
419 trig->ops = &iio_interrupt_trigger_ops;
421 ret = devm_iio_trigger_register(&spi->dev, trig);
422 if (ret) {
423 dev_err(&spi->dev, "failed to register trigger\n");
424 return ret;
427 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
428 iio_pollfunc_store_time,
429 as3935_trigger_handler, NULL);
431 if (ret) {
432 dev_err(&spi->dev, "cannot setup iio trigger\n");
433 return ret;
436 calibrate_as3935(st);
438 INIT_DELAYED_WORK(&st->work, as3935_event_work);
439 ret = devm_add_action(&spi->dev, as3935_stop_work, indio_dev);
440 if (ret)
441 return ret;
443 ret = devm_request_irq(&spi->dev, spi->irq,
444 &as3935_interrupt_handler,
445 IRQF_TRIGGER_RISING,
446 dev_name(&spi->dev),
447 indio_dev);
449 if (ret) {
450 dev_err(&spi->dev, "unable to request irq\n");
451 return ret;
454 ret = devm_iio_device_register(&spi->dev, indio_dev);
455 if (ret < 0) {
456 dev_err(&spi->dev, "unable to register device\n");
457 return ret;
459 return 0;
462 static const struct of_device_id as3935_of_match[] = {
463 { .compatible = "ams,as3935", },
464 { /* sentinel */ },
466 MODULE_DEVICE_TABLE(of, as3935_of_match);
468 static const struct spi_device_id as3935_id[] = {
469 {"as3935", 0},
472 MODULE_DEVICE_TABLE(spi, as3935_id);
474 static struct spi_driver as3935_driver = {
475 .driver = {
476 .name = "as3935",
477 .of_match_table = of_match_ptr(as3935_of_match),
478 .pm = AS3935_PM_OPS,
480 .probe = as3935_probe,
481 .id_table = as3935_id,
483 module_spi_driver(as3935_driver);
485 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
486 MODULE_DESCRIPTION("AS3935 lightning sensor");
487 MODULE_LICENSE("GPL");