x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / iio / proximity / as3935.c
blob24fb54398a3bc0e265118e9d2ea6be36665cf8bf
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_INT 0x03
43 #define AS3935_INT_MASK 0x0f
44 #define AS3935_EVENT_INT BIT(3)
45 #define AS3935_NOISE_INT BIT(0)
47 #define AS3935_DATA 0x07
48 #define AS3935_DATA_MASK 0x3F
50 #define AS3935_TUNE_CAP 0x08
51 #define AS3935_CALIBRATE 0x3D
53 #define AS3935_READ_DATA BIT(14)
54 #define AS3935_ADDRESS(x) ((x) << 8)
56 #define MAX_PF_CAP 120
57 #define TUNE_CAP_DIV 8
59 struct as3935_state {
60 struct spi_device *spi;
61 struct iio_trigger *trig;
62 struct mutex lock;
63 struct delayed_work work;
65 u32 tune_cap;
66 u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
67 u8 buf[2] ____cacheline_aligned;
70 static const struct iio_chan_spec as3935_channels[] = {
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,
84 IIO_CHAN_SOFT_TIMESTAMP(1),
87 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
89 u8 cmd;
90 int ret;
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;
98 return 0;
101 static int as3935_write(struct as3935_state *st,
102 unsigned int reg,
103 unsigned int val)
105 u8 *buf = st->buf;
107 buf[0] = AS3935_ADDRESS(reg) >> 8;
108 buf[1] = val;
110 return spi_write(st->spi, buf, 2);
113 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
114 struct device_attribute *attr,
115 char *buf)
117 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
118 int val, ret;
120 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
121 if (ret)
122 return ret;
123 val = (val & AS3935_AFE_MASK) >> 1;
125 return sprintf(buf, "%d\n", val);
128 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t len)
132 struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
133 unsigned long val;
134 int ret;
136 ret = kstrtoul((const char *) buf, 10, &val);
137 if (ret)
138 return -EINVAL;
140 if (val > AS3935_AFE_GAIN_MAX)
141 return -EINVAL;
143 as3935_write(st, AS3935_AFE_GAIN, val << 1);
145 return len;
148 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
149 as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
152 static struct attribute *as3935_attributes[] = {
153 &iio_dev_attr_sensor_sensitivity.dev_attr.attr,
154 NULL,
157 static struct attribute_group as3935_attribute_group = {
158 .attrs = as3935_attributes,
161 static int as3935_read_raw(struct iio_dev *indio_dev,
162 struct iio_chan_spec const *chan,
163 int *val,
164 int *val2,
165 long m)
167 struct as3935_state *st = iio_priv(indio_dev);
168 int ret;
171 switch (m) {
172 case IIO_CHAN_INFO_PROCESSED:
173 case IIO_CHAN_INFO_RAW:
174 *val2 = 0;
175 ret = as3935_read(st, AS3935_DATA, val);
176 if (ret)
177 return ret;
179 if (m == IIO_CHAN_INFO_RAW)
180 return IIO_VAL_INT;
182 /* storm out of range */
183 if (*val == AS3935_DATA_MASK)
184 return -EINVAL;
186 if (m == IIO_CHAN_INFO_PROCESSED)
187 *val *= 1000;
188 break;
189 case IIO_CHAN_INFO_SCALE:
190 *val = 1000;
191 break;
192 default:
193 return -EINVAL;
196 return IIO_VAL_INT;
199 static const struct iio_info as3935_info = {
200 .driver_module = THIS_MODULE,
201 .attrs = &as3935_attribute_group,
202 .read_raw = &as3935_read_raw,
205 static irqreturn_t as3935_trigger_handler(int irq, void *private)
207 struct iio_poll_func *pf = private;
208 struct iio_dev *indio_dev = pf->indio_dev;
209 struct as3935_state *st = iio_priv(indio_dev);
210 int val, ret;
212 ret = as3935_read(st, AS3935_DATA, &val);
213 if (ret)
214 goto err_read;
216 st->buffer[0] = val & AS3935_DATA_MASK;
217 iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
218 iio_get_time_ns(indio_dev));
219 err_read:
220 iio_trigger_notify_done(indio_dev->trig);
222 return IRQ_HANDLED;
225 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
226 .owner = THIS_MODULE,
229 static void as3935_event_work(struct work_struct *work)
231 struct as3935_state *st;
232 int val;
233 int ret;
235 st = container_of(work, struct as3935_state, work.work);
237 ret = as3935_read(st, AS3935_INT, &val);
238 if (ret) {
239 dev_warn(&st->spi->dev, "read error\n");
240 return;
243 val &= AS3935_INT_MASK;
245 switch (val) {
246 case AS3935_EVENT_INT:
247 iio_trigger_poll_chained(st->trig);
248 break;
249 case AS3935_NOISE_INT:
250 dev_warn(&st->spi->dev, "noise level is too high\n");
251 break;
255 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
257 struct iio_dev *indio_dev = private;
258 struct as3935_state *st = iio_priv(indio_dev);
261 * Delay work for >2 milliseconds after an interrupt to allow
262 * estimated distance to recalculated.
265 schedule_delayed_work(&st->work, msecs_to_jiffies(3));
267 return IRQ_HANDLED;
270 static void calibrate_as3935(struct as3935_state *st)
272 /* mask disturber interrupt bit */
273 as3935_write(st, AS3935_INT, BIT(5));
275 as3935_write(st, AS3935_CALIBRATE, 0x96);
276 as3935_write(st, AS3935_TUNE_CAP,
277 BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
279 mdelay(2);
280 as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
283 #ifdef CONFIG_PM_SLEEP
284 static int as3935_suspend(struct device *dev)
286 struct iio_dev *indio_dev = dev_get_drvdata(dev);
287 struct as3935_state *st = iio_priv(indio_dev);
288 int val, ret;
290 mutex_lock(&st->lock);
291 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
292 if (ret)
293 goto err_suspend;
294 val |= AS3935_AFE_PWR_BIT;
296 ret = as3935_write(st, AS3935_AFE_GAIN, val);
298 err_suspend:
299 mutex_unlock(&st->lock);
301 return ret;
304 static int as3935_resume(struct device *dev)
306 struct iio_dev *indio_dev = dev_get_drvdata(dev);
307 struct as3935_state *st = iio_priv(indio_dev);
308 int val, ret;
310 mutex_lock(&st->lock);
311 ret = as3935_read(st, AS3935_AFE_GAIN, &val);
312 if (ret)
313 goto err_resume;
314 val &= ~AS3935_AFE_PWR_BIT;
315 ret = as3935_write(st, AS3935_AFE_GAIN, val);
317 calibrate_as3935(st);
319 err_resume:
320 mutex_unlock(&st->lock);
322 return ret;
325 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
326 #define AS3935_PM_OPS (&as3935_pm_ops)
328 #else
329 #define AS3935_PM_OPS NULL
330 #endif
332 static int as3935_probe(struct spi_device *spi)
334 struct iio_dev *indio_dev;
335 struct iio_trigger *trig;
336 struct as3935_state *st;
337 struct device_node *np = spi->dev.of_node;
338 int ret;
340 /* Be sure lightning event interrupt is specified */
341 if (!spi->irq) {
342 dev_err(&spi->dev, "unable to get event interrupt\n");
343 return -EINVAL;
346 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
347 if (!indio_dev)
348 return -ENOMEM;
350 st = iio_priv(indio_dev);
351 st->spi = spi;
353 spi_set_drvdata(spi, indio_dev);
354 mutex_init(&st->lock);
355 INIT_DELAYED_WORK(&st->work, as3935_event_work);
357 ret = of_property_read_u32(np,
358 "ams,tuning-capacitor-pf", &st->tune_cap);
359 if (ret) {
360 st->tune_cap = 0;
361 dev_warn(&spi->dev,
362 "no tuning-capacitor-pf set, defaulting to %d",
363 st->tune_cap);
366 if (st->tune_cap > MAX_PF_CAP) {
367 dev_err(&spi->dev,
368 "wrong tuning-capacitor-pf setting of %d\n",
369 st->tune_cap);
370 return -EINVAL;
373 indio_dev->dev.parent = &spi->dev;
374 indio_dev->name = spi_get_device_id(spi)->name;
375 indio_dev->channels = as3935_channels;
376 indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
377 indio_dev->modes = INDIO_DIRECT_MODE;
378 indio_dev->info = &as3935_info;
380 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
381 indio_dev->name, indio_dev->id);
383 if (!trig)
384 return -ENOMEM;
386 st->trig = trig;
387 trig->dev.parent = indio_dev->dev.parent;
388 iio_trigger_set_drvdata(trig, indio_dev);
389 trig->ops = &iio_interrupt_trigger_ops;
391 ret = iio_trigger_register(trig);
392 if (ret) {
393 dev_err(&spi->dev, "failed to register trigger\n");
394 return ret;
397 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
398 &as3935_trigger_handler, NULL);
400 if (ret) {
401 dev_err(&spi->dev, "cannot setup iio trigger\n");
402 goto unregister_trigger;
405 calibrate_as3935(st);
407 ret = devm_request_irq(&spi->dev, spi->irq,
408 &as3935_interrupt_handler,
409 IRQF_TRIGGER_RISING,
410 dev_name(&spi->dev),
411 indio_dev);
413 if (ret) {
414 dev_err(&spi->dev, "unable to request irq\n");
415 goto unregister_buffer;
418 ret = iio_device_register(indio_dev);
419 if (ret < 0) {
420 dev_err(&spi->dev, "unable to register device\n");
421 goto unregister_buffer;
423 return 0;
425 unregister_buffer:
426 iio_triggered_buffer_cleanup(indio_dev);
428 unregister_trigger:
429 iio_trigger_unregister(st->trig);
431 return ret;
434 static int as3935_remove(struct spi_device *spi)
436 struct iio_dev *indio_dev = spi_get_drvdata(spi);
437 struct as3935_state *st = iio_priv(indio_dev);
439 iio_device_unregister(indio_dev);
440 iio_triggered_buffer_cleanup(indio_dev);
441 iio_trigger_unregister(st->trig);
443 return 0;
446 static const struct of_device_id as3935_of_match[] = {
447 { .compatible = "ams,as3935", },
448 { /* sentinel */ },
450 MODULE_DEVICE_TABLE(of, as3935_of_match);
452 static const struct spi_device_id as3935_id[] = {
453 {"as3935", 0},
456 MODULE_DEVICE_TABLE(spi, as3935_id);
458 static struct spi_driver as3935_driver = {
459 .driver = {
460 .name = "as3935",
461 .of_match_table = of_match_ptr(as3935_of_match),
462 .pm = AS3935_PM_OPS,
464 .probe = as3935_probe,
465 .remove = as3935_remove,
466 .id_table = as3935_id,
468 module_spi_driver(as3935_driver);
470 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
471 MODULE_DESCRIPTION("AS3935 lightning sensor");
472 MODULE_LICENSE("GPL");