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