as3935.c 10 KB

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