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