xref: /linux/drivers/iio/proximity/as3935.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
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/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>
26 
27 
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)
32 
33 #define AS3935_NFLWDTH		0x01
34 #define AS3935_NFLWDTH_MASK	0x7f
35 
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)
41 
42 #define AS3935_DATA		0x07
43 #define AS3935_DATA_MASK	0x3F
44 
45 #define AS3935_TUNE_CAP		0x08
46 #define AS3935_DEFAULTS		0x3C
47 #define AS3935_CALIBRATE	0x3D
48 
49 #define AS3935_READ_DATA	BIT(14)
50 #define AS3935_ADDRESS(x)	((x) << 8)
51 
52 #define MAX_PF_CAP		120
53 #define TUNE_CAP_DIV		8
54 
55 struct as3935_state {
56 	struct spi_device *spi;
57 	struct iio_trigger *trig;
58 	struct mutex lock;
59 	struct delayed_work work;
60 
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;
66 };
67 
68 static const struct iio_chan_spec as3935_channels[] = {
69 	{
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,
80 		},
81 	},
82 	IIO_CHAN_SOFT_TIMESTAMP(1),
83 };
84 
85 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
86 {
87 	u8 cmd;
88 	int ret;
89 
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;
95 
96 	return 0;
97 }
98 
99 static int as3935_write(struct as3935_state *st,
100 				unsigned int reg,
101 				unsigned int val)
102 {
103 	u8 *buf = st->buf;
104 
105 	buf[0] = AS3935_ADDRESS(reg) >> 8;
106 	buf[1] = val;
107 
108 	return spi_write(st->spi, buf, 2);
109 }
110 
111 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
112 					struct device_attribute *attr,
113 					char *buf)
114 {
115 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
116 	int val, ret;
117 
118 	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
119 	if (ret)
120 		return ret;
121 	val = (val & AS3935_AFE_MASK) >> 1;
122 
123 	return sprintf(buf, "%d\n", val);
124 }
125 
126 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
127 					struct device_attribute *attr,
128 					const char *buf, size_t len)
129 {
130 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
131 	unsigned long val;
132 	int ret;
133 
134 	ret = kstrtoul((const char *) buf, 10, &val);
135 	if (ret)
136 		return -EINVAL;
137 
138 	if (val > AS3935_AFE_GAIN_MAX)
139 		return -EINVAL;
140 
141 	as3935_write(st, AS3935_AFE_GAIN, val << 1);
142 
143 	return len;
144 }
145 
146 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
147 					struct device_attribute *attr,
148 					char *buf)
149 {
150 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
151 	int ret;
152 
153 	mutex_lock(&st->lock);
154 	ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
155 	mutex_unlock(&st->lock);
156 
157 	return ret;
158 }
159 
160 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
161 	as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
162 
163 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
164 	as3935_noise_level_tripped_show, NULL, 0);
165 
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,
170 };
171 
172 static const struct attribute_group as3935_attribute_group = {
173 	.attrs = as3935_attributes,
174 };
175 
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)
181 {
182 	struct as3935_state *st = iio_priv(indio_dev);
183 	int ret;
184 
185 
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;
193 
194 		/* storm out of range */
195 		if (*val == AS3935_DATA_MASK)
196 			return -EINVAL;
197 
198 		if (m == IIO_CHAN_INFO_RAW)
199 			return IIO_VAL_INT;
200 
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;
209 	}
210 
211 	return IIO_VAL_INT;
212 }
213 
214 static const struct iio_info as3935_info = {
215 	.attrs = &as3935_attribute_group,
216 	.read_raw = &as3935_read_raw,
217 };
218 
219 static irqreturn_t as3935_trigger_handler(int irq, void *private)
220 {
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;
225 
226 	ret = as3935_read(st, AS3935_DATA, &val);
227 	if (ret)
228 		goto err_read;
229 
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);
235 
236 	return IRQ_HANDLED;
237 }
238 
239 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
240 };
241 
242 static void as3935_event_work(struct work_struct *work)
243 {
244 	struct as3935_state *st;
245 	int val;
246 	int ret;
247 
248 	st = container_of(work, struct as3935_state, work.work);
249 
250 	ret = as3935_read(st, AS3935_INT, &val);
251 	if (ret) {
252 		dev_warn(&st->spi->dev, "read error\n");
253 		return;
254 	}
255 
256 	val &= AS3935_INT_MASK;
257 
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;
269 	}
270 }
271 
272 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
273 {
274 	struct iio_dev *indio_dev = private;
275 	struct as3935_state *st = iio_priv(indio_dev);
276 
277 	/*
278 	 * Delay work for >2 milliseconds after an interrupt to allow
279 	 * estimated distance to recalculated.
280 	 */
281 
282 	schedule_delayed_work(&st->work, msecs_to_jiffies(3));
283 
284 	return IRQ_HANDLED;
285 }
286 
287 static void calibrate_as3935(struct as3935_state *st)
288 {
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));
293 
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);
297 }
298 
299 #ifdef CONFIG_PM_SLEEP
300 static int as3935_suspend(struct device *dev)
301 {
302 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
303 	struct as3935_state *st = iio_priv(indio_dev);
304 	int val, ret;
305 
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;
311 
312 	ret = as3935_write(st, AS3935_AFE_GAIN, val);
313 
314 err_suspend:
315 	mutex_unlock(&st->lock);
316 
317 	return ret;
318 }
319 
320 static int as3935_resume(struct device *dev)
321 {
322 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
323 	struct as3935_state *st = iio_priv(indio_dev);
324 	int val, ret;
325 
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);
332 
333 	calibrate_as3935(st);
334 
335 err_resume:
336 	mutex_unlock(&st->lock);
337 
338 	return ret;
339 }
340 
341 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
342 #define AS3935_PM_OPS (&as3935_pm_ops)
343 
344 #else
345 #define AS3935_PM_OPS NULL
346 #endif
347 
348 static int as3935_probe(struct spi_device *spi)
349 {
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;
355 
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;
360 	}
361 
362 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
363 	if (!indio_dev)
364 		return -ENOMEM;
365 
366 	st = iio_priv(indio_dev);
367 	st->spi = spi;
368 
369 	spi_set_drvdata(spi, indio_dev);
370 	mutex_init(&st->lock);
371 	INIT_DELAYED_WORK(&st->work, as3935_event_work);
372 
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);
380 	}
381 
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;
387 	}
388 
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;
396 	}
397 
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;
404 
405 	trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
406 				      indio_dev->name, indio_dev->id);
407 
408 	if (!trig)
409 		return -ENOMEM;
410 
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;
416 
417 	ret = iio_trigger_register(trig);
418 	if (ret) {
419 		dev_err(&spi->dev, "failed to register trigger\n");
420 		return ret;
421 	}
422 
423 	ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
424 		&as3935_trigger_handler, NULL);
425 
426 	if (ret) {
427 		dev_err(&spi->dev, "cannot setup iio trigger\n");
428 		goto unregister_trigger;
429 	}
430 
431 	calibrate_as3935(st);
432 
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);
438 
439 	if (ret) {
440 		dev_err(&spi->dev, "unable to request irq\n");
441 		goto unregister_buffer;
442 	}
443 
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;
448 	}
449 	return 0;
450 
451 unregister_buffer:
452 	iio_triggered_buffer_cleanup(indio_dev);
453 
454 unregister_trigger:
455 	iio_trigger_unregister(st->trig);
456 
457 	return ret;
458 }
459 
460 static int as3935_remove(struct spi_device *spi)
461 {
462 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
463 	struct as3935_state *st = iio_priv(indio_dev);
464 
465 	iio_device_unregister(indio_dev);
466 	iio_triggered_buffer_cleanup(indio_dev);
467 	iio_trigger_unregister(st->trig);
468 
469 	return 0;
470 }
471 
472 static const struct of_device_id as3935_of_match[] = {
473 	{ .compatible = "ams,as3935", },
474 	{ /* sentinel */ },
475 };
476 MODULE_DEVICE_TABLE(of, as3935_of_match);
477 
478 static const struct spi_device_id as3935_id[] = {
479 	{"as3935", 0},
480 	{},
481 };
482 MODULE_DEVICE_TABLE(spi, as3935_id);
483 
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,
489 	},
490 	.probe		= as3935_probe,
491 	.remove		= as3935_remove,
492 	.id_table	= as3935_id,
493 };
494 module_spi_driver(as3935_driver);
495 
496 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
497 MODULE_DESCRIPTION("AS3935 lightning sensor");
498 MODULE_LICENSE("GPL");
499