as3935.c 10 KB

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