as3935.c 9.9 KB

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