xref: /linux/drivers/iio/proximity/as3935.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
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/mod_devicetable.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/workqueue.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/irq.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 
26 #define AS3935_AFE_GAIN		0x00
27 #define AS3935_AFE_MASK		0x3F
28 #define AS3935_AFE_GAIN_MAX	0x1F
29 #define AS3935_AFE_PWR_BIT	BIT(0)
30 
31 #define AS3935_NFLWDTH		0x01
32 #define AS3935_NFLWDTH_MASK	0x7f
33 
34 #define AS3935_INT		0x03
35 #define AS3935_INT_MASK		0x0f
36 #define AS3935_DISTURB_INT	BIT(2)
37 #define AS3935_EVENT_INT	BIT(3)
38 #define AS3935_NOISE_INT	BIT(0)
39 
40 #define AS3935_DATA		0x07
41 #define AS3935_DATA_MASK	0x3F
42 
43 #define AS3935_TUNE_CAP		0x08
44 #define AS3935_DEFAULTS		0x3C
45 #define AS3935_CALIBRATE	0x3D
46 
47 #define AS3935_READ_DATA	BIT(14)
48 #define AS3935_ADDRESS(x)	((x) << 8)
49 
50 #define MAX_PF_CAP		120
51 #define TUNE_CAP_DIV		8
52 
53 struct as3935_state {
54 	struct spi_device *spi;
55 	struct iio_trigger *trig;
56 	struct mutex lock;
57 	struct delayed_work work;
58 
59 	unsigned long noise_tripped;
60 	u32 tune_cap;
61 	u32 nflwdth_reg;
62 	u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
63 	u8 buf[2] ____cacheline_aligned;
64 };
65 
66 static const struct iio_chan_spec as3935_channels[] = {
67 	{
68 		.type           = IIO_PROXIMITY,
69 		.info_mask_separate =
70 			BIT(IIO_CHAN_INFO_RAW) |
71 			BIT(IIO_CHAN_INFO_PROCESSED) |
72 			BIT(IIO_CHAN_INFO_SCALE),
73 		.scan_index     = 0,
74 		.scan_type = {
75 			.sign           = 'u',
76 			.realbits       = 6,
77 			.storagebits    = 8,
78 		},
79 	},
80 	IIO_CHAN_SOFT_TIMESTAMP(1),
81 };
82 
83 static int as3935_read(struct as3935_state *st, unsigned int reg, int *val)
84 {
85 	u8 cmd;
86 	int ret;
87 
88 	cmd = (AS3935_READ_DATA | AS3935_ADDRESS(reg)) >> 8;
89 	ret = spi_w8r8(st->spi, cmd);
90 	if (ret < 0)
91 		return ret;
92 	*val = ret;
93 
94 	return 0;
95 }
96 
97 static int as3935_write(struct as3935_state *st,
98 				unsigned int reg,
99 				unsigned int val)
100 {
101 	u8 *buf = st->buf;
102 
103 	buf[0] = AS3935_ADDRESS(reg) >> 8;
104 	buf[1] = val;
105 
106 	return spi_write(st->spi, buf, 2);
107 }
108 
109 static ssize_t as3935_sensor_sensitivity_show(struct device *dev,
110 					struct device_attribute *attr,
111 					char *buf)
112 {
113 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
114 	int val, ret;
115 
116 	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
117 	if (ret)
118 		return ret;
119 	val = (val & AS3935_AFE_MASK) >> 1;
120 
121 	return sprintf(buf, "%d\n", val);
122 }
123 
124 static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
125 					struct device_attribute *attr,
126 					const char *buf, size_t len)
127 {
128 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
129 	unsigned long val;
130 	int ret;
131 
132 	ret = kstrtoul((const char *) buf, 10, &val);
133 	if (ret)
134 		return -EINVAL;
135 
136 	if (val > AS3935_AFE_GAIN_MAX)
137 		return -EINVAL;
138 
139 	as3935_write(st, AS3935_AFE_GAIN, val << 1);
140 
141 	return len;
142 }
143 
144 static ssize_t as3935_noise_level_tripped_show(struct device *dev,
145 					struct device_attribute *attr,
146 					char *buf)
147 {
148 	struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
149 	int ret;
150 
151 	mutex_lock(&st->lock);
152 	ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
153 	mutex_unlock(&st->lock);
154 
155 	return ret;
156 }
157 
158 static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
159 	as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
160 
161 static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
162 	as3935_noise_level_tripped_show, NULL, 0);
163 
164 static struct attribute *as3935_attributes[] = {
165 	&iio_dev_attr_sensor_sensitivity.dev_attr.attr,
166 	&iio_dev_attr_noise_level_tripped.dev_attr.attr,
167 	NULL,
168 };
169 
170 static const struct attribute_group as3935_attribute_group = {
171 	.attrs = as3935_attributes,
172 };
173 
174 static int as3935_read_raw(struct iio_dev *indio_dev,
175 			   struct iio_chan_spec const *chan,
176 			   int *val,
177 			   int *val2,
178 			   long m)
179 {
180 	struct as3935_state *st = iio_priv(indio_dev);
181 	int ret;
182 
183 
184 	switch (m) {
185 	case IIO_CHAN_INFO_PROCESSED:
186 	case IIO_CHAN_INFO_RAW:
187 		*val2 = 0;
188 		ret = as3935_read(st, AS3935_DATA, val);
189 		if (ret)
190 			return ret;
191 
192 		/* storm out of range */
193 		if (*val == AS3935_DATA_MASK)
194 			return -EINVAL;
195 
196 		if (m == IIO_CHAN_INFO_RAW)
197 			return IIO_VAL_INT;
198 
199 		if (m == IIO_CHAN_INFO_PROCESSED)
200 			*val *= 1000;
201 		break;
202 	case IIO_CHAN_INFO_SCALE:
203 		*val = 1000;
204 		break;
205 	default:
206 		return -EINVAL;
207 	}
208 
209 	return IIO_VAL_INT;
210 }
211 
212 static const struct iio_info as3935_info = {
213 	.attrs = &as3935_attribute_group,
214 	.read_raw = &as3935_read_raw,
215 };
216 
217 static irqreturn_t as3935_trigger_handler(int irq, void *private)
218 {
219 	struct iio_poll_func *pf = private;
220 	struct iio_dev *indio_dev = pf->indio_dev;
221 	struct as3935_state *st = iio_priv(indio_dev);
222 	int val, ret;
223 
224 	ret = as3935_read(st, AS3935_DATA, &val);
225 	if (ret)
226 		goto err_read;
227 
228 	st->buffer[0] = val & AS3935_DATA_MASK;
229 	iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
230 					   iio_get_time_ns(indio_dev));
231 err_read:
232 	iio_trigger_notify_done(indio_dev->trig);
233 
234 	return IRQ_HANDLED;
235 }
236 
237 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
238 };
239 
240 static void as3935_event_work(struct work_struct *work)
241 {
242 	struct as3935_state *st;
243 	int val;
244 	int ret;
245 
246 	st = container_of(work, struct as3935_state, work.work);
247 
248 	ret = as3935_read(st, AS3935_INT, &val);
249 	if (ret) {
250 		dev_warn(&st->spi->dev, "read error\n");
251 		return;
252 	}
253 
254 	val &= AS3935_INT_MASK;
255 
256 	switch (val) {
257 	case AS3935_EVENT_INT:
258 		iio_trigger_poll_chained(st->trig);
259 		break;
260 	case AS3935_DISTURB_INT:
261 	case AS3935_NOISE_INT:
262 		mutex_lock(&st->lock);
263 		st->noise_tripped = jiffies;
264 		mutex_unlock(&st->lock);
265 		dev_warn(&st->spi->dev, "noise level is too high\n");
266 		break;
267 	}
268 }
269 
270 static irqreturn_t as3935_interrupt_handler(int irq, void *private)
271 {
272 	struct iio_dev *indio_dev = private;
273 	struct as3935_state *st = iio_priv(indio_dev);
274 
275 	/*
276 	 * Delay work for >2 milliseconds after an interrupt to allow
277 	 * estimated distance to recalculated.
278 	 */
279 
280 	schedule_delayed_work(&st->work, msecs_to_jiffies(3));
281 
282 	return IRQ_HANDLED;
283 }
284 
285 static void calibrate_as3935(struct as3935_state *st)
286 {
287 	as3935_write(st, AS3935_DEFAULTS, 0x96);
288 	as3935_write(st, AS3935_CALIBRATE, 0x96);
289 	as3935_write(st, AS3935_TUNE_CAP,
290 		BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
291 
292 	mdelay(2);
293 	as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
294 	as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
295 }
296 
297 #ifdef CONFIG_PM_SLEEP
298 static int as3935_suspend(struct device *dev)
299 {
300 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
301 	struct as3935_state *st = iio_priv(indio_dev);
302 	int val, ret;
303 
304 	mutex_lock(&st->lock);
305 	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
306 	if (ret)
307 		goto err_suspend;
308 	val |= AS3935_AFE_PWR_BIT;
309 
310 	ret = as3935_write(st, AS3935_AFE_GAIN, val);
311 
312 err_suspend:
313 	mutex_unlock(&st->lock);
314 
315 	return ret;
316 }
317 
318 static int as3935_resume(struct device *dev)
319 {
320 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
321 	struct as3935_state *st = iio_priv(indio_dev);
322 	int val, ret;
323 
324 	mutex_lock(&st->lock);
325 	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
326 	if (ret)
327 		goto err_resume;
328 	val &= ~AS3935_AFE_PWR_BIT;
329 	ret = as3935_write(st, AS3935_AFE_GAIN, val);
330 
331 	calibrate_as3935(st);
332 
333 err_resume:
334 	mutex_unlock(&st->lock);
335 
336 	return ret;
337 }
338 
339 static SIMPLE_DEV_PM_OPS(as3935_pm_ops, as3935_suspend, as3935_resume);
340 #define AS3935_PM_OPS (&as3935_pm_ops)
341 
342 #else
343 #define AS3935_PM_OPS NULL
344 #endif
345 
346 static void as3935_stop_work(void *data)
347 {
348 	struct iio_dev *indio_dev = data;
349 	struct as3935_state *st = iio_priv(indio_dev);
350 
351 	cancel_delayed_work_sync(&st->work);
352 }
353 
354 static int as3935_probe(struct spi_device *spi)
355 {
356 	struct device *dev = &spi->dev;
357 	struct iio_dev *indio_dev;
358 	struct iio_trigger *trig;
359 	struct as3935_state *st;
360 	int ret;
361 
362 	/* Be sure lightning event interrupt is specified */
363 	if (!spi->irq) {
364 		dev_err(dev, "unable to get event interrupt\n");
365 		return -EINVAL;
366 	}
367 
368 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
369 	if (!indio_dev)
370 		return -ENOMEM;
371 
372 	st = iio_priv(indio_dev);
373 	st->spi = spi;
374 
375 	spi_set_drvdata(spi, indio_dev);
376 	mutex_init(&st->lock);
377 
378 	ret = device_property_read_u32(dev,
379 			"ams,tuning-capacitor-pf", &st->tune_cap);
380 	if (ret) {
381 		st->tune_cap = 0;
382 		dev_warn(dev, "no tuning-capacitor-pf set, defaulting to %d",
383 			st->tune_cap);
384 	}
385 
386 	if (st->tune_cap > MAX_PF_CAP) {
387 		dev_err(dev, "wrong tuning-capacitor-pf setting of %d\n",
388 			st->tune_cap);
389 		return -EINVAL;
390 	}
391 
392 	ret = device_property_read_u32(dev,
393 			"ams,nflwdth", &st->nflwdth_reg);
394 	if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
395 		dev_err(dev, "invalid nflwdth setting of %d\n",
396 			st->nflwdth_reg);
397 		return -EINVAL;
398 	}
399 
400 	indio_dev->name = spi_get_device_id(spi)->name;
401 	indio_dev->channels = as3935_channels;
402 	indio_dev->num_channels = ARRAY_SIZE(as3935_channels);
403 	indio_dev->modes = INDIO_DIRECT_MODE;
404 	indio_dev->info = &as3935_info;
405 
406 	trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
407 				      indio_dev->name, indio_dev->id);
408 
409 	if (!trig)
410 		return -ENOMEM;
411 
412 	st->trig = trig;
413 	st->noise_tripped = jiffies - HZ;
414 	iio_trigger_set_drvdata(trig, indio_dev);
415 	trig->ops = &iio_interrupt_trigger_ops;
416 
417 	ret = devm_iio_trigger_register(dev, trig);
418 	if (ret) {
419 		dev_err(dev, "failed to register trigger\n");
420 		return ret;
421 	}
422 
423 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
424 					      iio_pollfunc_store_time,
425 					      as3935_trigger_handler, NULL);
426 
427 	if (ret) {
428 		dev_err(dev, "cannot setup iio trigger\n");
429 		return ret;
430 	}
431 
432 	calibrate_as3935(st);
433 
434 	INIT_DELAYED_WORK(&st->work, as3935_event_work);
435 	ret = devm_add_action(dev, as3935_stop_work, indio_dev);
436 	if (ret)
437 		return ret;
438 
439 	ret = devm_request_irq(dev, spi->irq,
440 				&as3935_interrupt_handler,
441 				IRQF_TRIGGER_RISING,
442 				dev_name(dev),
443 				indio_dev);
444 
445 	if (ret) {
446 		dev_err(dev, "unable to request irq\n");
447 		return ret;
448 	}
449 
450 	ret = devm_iio_device_register(dev, indio_dev);
451 	if (ret < 0) {
452 		dev_err(dev, "unable to register device\n");
453 		return ret;
454 	}
455 	return 0;
456 }
457 
458 static const struct of_device_id as3935_of_match[] = {
459 	{ .compatible = "ams,as3935", },
460 	{ /* sentinel */ },
461 };
462 MODULE_DEVICE_TABLE(of, as3935_of_match);
463 
464 static const struct spi_device_id as3935_id[] = {
465 	{"as3935", 0},
466 	{},
467 };
468 MODULE_DEVICE_TABLE(spi, as3935_id);
469 
470 static struct spi_driver as3935_driver = {
471 	.driver = {
472 		.name	= "as3935",
473 		.of_match_table = as3935_of_match,
474 		.pm	= AS3935_PM_OPS,
475 	},
476 	.probe		= as3935_probe,
477 	.id_table	= as3935_id,
478 };
479 module_spi_driver(as3935_driver);
480 
481 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
482 MODULE_DESCRIPTION("AS3935 lightning sensor");
483 MODULE_LICENSE("GPL");
484