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 void as3935_stop_work(void *data) 349 { 350 struct iio_dev *indio_dev = data; 351 struct as3935_state *st = iio_priv(indio_dev); 352 353 cancel_delayed_work_sync(&st->work); 354 } 355 356 static int as3935_probe(struct spi_device *spi) 357 { 358 struct iio_dev *indio_dev; 359 struct iio_trigger *trig; 360 struct as3935_state *st; 361 struct device_node *np = spi->dev.of_node; 362 int ret; 363 364 /* Be sure lightning event interrupt is specified */ 365 if (!spi->irq) { 366 dev_err(&spi->dev, "unable to get event interrupt\n"); 367 return -EINVAL; 368 } 369 370 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 371 if (!indio_dev) 372 return -ENOMEM; 373 374 st = iio_priv(indio_dev); 375 st->spi = spi; 376 377 spi_set_drvdata(spi, indio_dev); 378 mutex_init(&st->lock); 379 380 ret = of_property_read_u32(np, 381 "ams,tuning-capacitor-pf", &st->tune_cap); 382 if (ret) { 383 st->tune_cap = 0; 384 dev_warn(&spi->dev, 385 "no tuning-capacitor-pf set, defaulting to %d", 386 st->tune_cap); 387 } 388 389 if (st->tune_cap > MAX_PF_CAP) { 390 dev_err(&spi->dev, 391 "wrong tuning-capacitor-pf setting of %d\n", 392 st->tune_cap); 393 return -EINVAL; 394 } 395 396 ret = of_property_read_u32(np, 397 "ams,nflwdth", &st->nflwdth_reg); 398 if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) { 399 dev_err(&spi->dev, 400 "invalid nflwdth setting of %d\n", 401 st->nflwdth_reg); 402 return -EINVAL; 403 } 404 405 indio_dev->dev.parent = &spi->dev; 406 indio_dev->name = spi_get_device_id(spi)->name; 407 indio_dev->channels = as3935_channels; 408 indio_dev->num_channels = ARRAY_SIZE(as3935_channels); 409 indio_dev->modes = INDIO_DIRECT_MODE; 410 indio_dev->info = &as3935_info; 411 412 trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d", 413 indio_dev->name, indio_dev->id); 414 415 if (!trig) 416 return -ENOMEM; 417 418 st->trig = trig; 419 st->noise_tripped = jiffies - HZ; 420 trig->dev.parent = indio_dev->dev.parent; 421 iio_trigger_set_drvdata(trig, indio_dev); 422 trig->ops = &iio_interrupt_trigger_ops; 423 424 ret = devm_iio_trigger_register(&spi->dev, trig); 425 if (ret) { 426 dev_err(&spi->dev, "failed to register trigger\n"); 427 return ret; 428 } 429 430 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, 431 iio_pollfunc_store_time, 432 as3935_trigger_handler, NULL); 433 434 if (ret) { 435 dev_err(&spi->dev, "cannot setup iio trigger\n"); 436 return ret; 437 } 438 439 calibrate_as3935(st); 440 441 INIT_DELAYED_WORK(&st->work, as3935_event_work); 442 ret = devm_add_action(&spi->dev, as3935_stop_work, indio_dev); 443 if (ret) 444 return ret; 445 446 ret = devm_request_irq(&spi->dev, spi->irq, 447 &as3935_interrupt_handler, 448 IRQF_TRIGGER_RISING, 449 dev_name(&spi->dev), 450 indio_dev); 451 452 if (ret) { 453 dev_err(&spi->dev, "unable to request irq\n"); 454 return ret; 455 } 456 457 ret = devm_iio_device_register(&spi->dev, indio_dev); 458 if (ret < 0) { 459 dev_err(&spi->dev, "unable to register device\n"); 460 return ret; 461 } 462 return 0; 463 } 464 465 static const struct of_device_id as3935_of_match[] = { 466 { .compatible = "ams,as3935", }, 467 { /* sentinel */ }, 468 }; 469 MODULE_DEVICE_TABLE(of, as3935_of_match); 470 471 static const struct spi_device_id as3935_id[] = { 472 {"as3935", 0}, 473 {}, 474 }; 475 MODULE_DEVICE_TABLE(spi, as3935_id); 476 477 static struct spi_driver as3935_driver = { 478 .driver = { 479 .name = "as3935", 480 .of_match_table = of_match_ptr(as3935_of_match), 481 .pm = AS3935_PM_OPS, 482 }, 483 .probe = as3935_probe, 484 .id_table = as3935_id, 485 }; 486 module_spi_driver(as3935_driver); 487 488 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 489 MODULE_DESCRIPTION("AS3935 lightning sensor"); 490 MODULE_LICENSE("GPL"); 491