hi8435.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Holt Integrated Circuits HI-8435 threshold detector driver
  3. *
  4. * Copyright (C) 2015 Zodiac Inflight Innovations
  5. * Copyright (C) 2015 Cogent Embedded, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/iio/events.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. #include <linux/iio/trigger.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_event.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/gpio/consumer.h>
  26. #define DRV_NAME "hi8435"
  27. /* Register offsets for HI-8435 */
  28. #define HI8435_CTRL_REG 0x02
  29. #define HI8435_PSEN_REG 0x04
  30. #define HI8435_TMDATA_REG 0x1E
  31. #define HI8435_GOCENHYS_REG 0x3A
  32. #define HI8435_SOCENHYS_REG 0x3C
  33. #define HI8435_SO7_0_REG 0x10
  34. #define HI8435_SO15_8_REG 0x12
  35. #define HI8435_SO23_16_REG 0x14
  36. #define HI8435_SO31_24_REG 0x16
  37. #define HI8435_SO31_0_REG 0x78
  38. #define HI8435_WRITE_OPCODE 0x00
  39. #define HI8435_READ_OPCODE 0x80
  40. /* CTRL register bits */
  41. #define HI8435_CTRL_TEST 0x01
  42. #define HI8435_CTRL_SRST 0x02
  43. struct hi8435_priv {
  44. struct spi_device *spi;
  45. struct mutex lock;
  46. unsigned long event_scan_mask; /* soft mask/unmask channels events */
  47. unsigned int event_prev_val;
  48. unsigned threshold_lo[2]; /* GND-Open and Supply-Open thresholds */
  49. unsigned threshold_hi[2]; /* GND-Open and Supply-Open thresholds */
  50. u8 reg_buffer[3] ____cacheline_aligned;
  51. };
  52. static int hi8435_readb(struct hi8435_priv *priv, u8 reg, u8 *val)
  53. {
  54. reg |= HI8435_READ_OPCODE;
  55. return spi_write_then_read(priv->spi, &reg, 1, val, 1);
  56. }
  57. static int hi8435_readw(struct hi8435_priv *priv, u8 reg, u16 *val)
  58. {
  59. int ret;
  60. __be16 be_val;
  61. reg |= HI8435_READ_OPCODE;
  62. ret = spi_write_then_read(priv->spi, &reg, 1, &be_val, 2);
  63. *val = be16_to_cpu(be_val);
  64. return ret;
  65. }
  66. static int hi8435_readl(struct hi8435_priv *priv, u8 reg, u32 *val)
  67. {
  68. int ret;
  69. __be32 be_val;
  70. reg |= HI8435_READ_OPCODE;
  71. ret = spi_write_then_read(priv->spi, &reg, 1, &be_val, 4);
  72. *val = be32_to_cpu(be_val);
  73. return ret;
  74. }
  75. static int hi8435_writeb(struct hi8435_priv *priv, u8 reg, u8 val)
  76. {
  77. priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE;
  78. priv->reg_buffer[1] = val;
  79. return spi_write(priv->spi, priv->reg_buffer, 2);
  80. }
  81. static int hi8435_writew(struct hi8435_priv *priv, u8 reg, u16 val)
  82. {
  83. priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE;
  84. priv->reg_buffer[1] = (val >> 8) & 0xff;
  85. priv->reg_buffer[2] = val & 0xff;
  86. return spi_write(priv->spi, priv->reg_buffer, 3);
  87. }
  88. static int hi8435_read_event_config(struct iio_dev *idev,
  89. const struct iio_chan_spec *chan,
  90. enum iio_event_type type,
  91. enum iio_event_direction dir)
  92. {
  93. struct hi8435_priv *priv = iio_priv(idev);
  94. return !!(priv->event_scan_mask & BIT(chan->channel));
  95. }
  96. static int hi8435_write_event_config(struct iio_dev *idev,
  97. const struct iio_chan_spec *chan,
  98. enum iio_event_type type,
  99. enum iio_event_direction dir, int state)
  100. {
  101. struct hi8435_priv *priv = iio_priv(idev);
  102. int ret;
  103. u32 tmp;
  104. if (state) {
  105. ret = hi8435_readl(priv, HI8435_SO31_0_REG, &tmp);
  106. if (ret < 0)
  107. return ret;
  108. if (tmp & BIT(chan->channel))
  109. priv->event_prev_val |= BIT(chan->channel);
  110. else
  111. priv->event_prev_val &= ~BIT(chan->channel);
  112. priv->event_scan_mask |= BIT(chan->channel);
  113. } else
  114. priv->event_scan_mask &= ~BIT(chan->channel);
  115. return 0;
  116. }
  117. static int hi8435_read_event_value(struct iio_dev *idev,
  118. const struct iio_chan_spec *chan,
  119. enum iio_event_type type,
  120. enum iio_event_direction dir,
  121. enum iio_event_info info,
  122. int *val, int *val2)
  123. {
  124. struct hi8435_priv *priv = iio_priv(idev);
  125. int ret;
  126. u8 mode, psen;
  127. u16 reg;
  128. ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
  129. if (ret < 0)
  130. return ret;
  131. /* Supply-Open or GND-Open sensing mode */
  132. mode = !!(psen & BIT(chan->channel / 8));
  133. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  134. HI8435_GOCENHYS_REG, &reg);
  135. if (ret < 0)
  136. return ret;
  137. if (dir == IIO_EV_DIR_FALLING)
  138. *val = ((reg & 0xff) - (reg >> 8)) / 2;
  139. else if (dir == IIO_EV_DIR_RISING)
  140. *val = ((reg & 0xff) + (reg >> 8)) / 2;
  141. return IIO_VAL_INT;
  142. }
  143. static int hi8435_write_event_value(struct iio_dev *idev,
  144. const struct iio_chan_spec *chan,
  145. enum iio_event_type type,
  146. enum iio_event_direction dir,
  147. enum iio_event_info info,
  148. int val, int val2)
  149. {
  150. struct hi8435_priv *priv = iio_priv(idev);
  151. int ret;
  152. u8 mode, psen;
  153. u16 reg;
  154. ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
  155. if (ret < 0)
  156. return ret;
  157. /* Supply-Open or GND-Open sensing mode */
  158. mode = !!(psen & BIT(chan->channel / 8));
  159. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  160. HI8435_GOCENHYS_REG, &reg);
  161. if (ret < 0)
  162. return ret;
  163. if (dir == IIO_EV_DIR_FALLING) {
  164. /* falling threshold range 2..21V, hysteresis minimum 2V */
  165. if (val < 2 || val > 21 || (val + 2) > priv->threshold_hi[mode])
  166. return -EINVAL;
  167. if (val == priv->threshold_lo[mode])
  168. return 0;
  169. priv->threshold_lo[mode] = val;
  170. /* hysteresis must not be odd */
  171. if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
  172. priv->threshold_hi[mode]--;
  173. } else if (dir == IIO_EV_DIR_RISING) {
  174. /* rising threshold range 3..22V, hysteresis minimum 2V */
  175. if (val < 3 || val > 22 || val < (priv->threshold_lo[mode] + 2))
  176. return -EINVAL;
  177. if (val == priv->threshold_hi[mode])
  178. return 0;
  179. priv->threshold_hi[mode] = val;
  180. /* hysteresis must not be odd */
  181. if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
  182. priv->threshold_lo[mode]++;
  183. }
  184. /* program thresholds */
  185. mutex_lock(&priv->lock);
  186. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  187. HI8435_GOCENHYS_REG, &reg);
  188. if (ret < 0) {
  189. mutex_unlock(&priv->lock);
  190. return ret;
  191. }
  192. /* hysteresis */
  193. reg = priv->threshold_hi[mode] - priv->threshold_lo[mode];
  194. reg <<= 8;
  195. /* threshold center */
  196. reg |= (priv->threshold_hi[mode] + priv->threshold_lo[mode]);
  197. ret = hi8435_writew(priv, mode ? HI8435_SOCENHYS_REG :
  198. HI8435_GOCENHYS_REG, reg);
  199. mutex_unlock(&priv->lock);
  200. return ret;
  201. }
  202. static int hi8435_debugfs_reg_access(struct iio_dev *idev,
  203. unsigned reg, unsigned writeval,
  204. unsigned *readval)
  205. {
  206. struct hi8435_priv *priv = iio_priv(idev);
  207. int ret;
  208. u8 val;
  209. if (readval != NULL) {
  210. ret = hi8435_readb(priv, reg, &val);
  211. *readval = val;
  212. } else {
  213. val = (u8)writeval;
  214. ret = hi8435_writeb(priv, reg, val);
  215. }
  216. return ret;
  217. }
  218. static const struct iio_event_spec hi8435_events[] = {
  219. {
  220. .type = IIO_EV_TYPE_THRESH,
  221. .dir = IIO_EV_DIR_RISING,
  222. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  223. }, {
  224. .type = IIO_EV_TYPE_THRESH,
  225. .dir = IIO_EV_DIR_FALLING,
  226. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  227. }, {
  228. .type = IIO_EV_TYPE_THRESH,
  229. .dir = IIO_EV_DIR_EITHER,
  230. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  231. },
  232. };
  233. static int hi8435_get_sensing_mode(struct iio_dev *idev,
  234. const struct iio_chan_spec *chan)
  235. {
  236. struct hi8435_priv *priv = iio_priv(idev);
  237. int ret;
  238. u8 reg;
  239. ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg);
  240. if (ret < 0)
  241. return ret;
  242. return !!(reg & BIT(chan->channel / 8));
  243. }
  244. static int hi8435_set_sensing_mode(struct iio_dev *idev,
  245. const struct iio_chan_spec *chan,
  246. unsigned int mode)
  247. {
  248. struct hi8435_priv *priv = iio_priv(idev);
  249. int ret;
  250. u8 reg;
  251. mutex_lock(&priv->lock);
  252. ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg);
  253. if (ret < 0) {
  254. mutex_unlock(&priv->lock);
  255. return ret;
  256. }
  257. reg &= ~BIT(chan->channel / 8);
  258. if (mode)
  259. reg |= BIT(chan->channel / 8);
  260. ret = hi8435_writeb(priv, HI8435_PSEN_REG, reg);
  261. mutex_unlock(&priv->lock);
  262. return ret;
  263. }
  264. static const char * const hi8435_sensing_modes[] = { "GND-Open",
  265. "Supply-Open" };
  266. static const struct iio_enum hi8435_sensing_mode = {
  267. .items = hi8435_sensing_modes,
  268. .num_items = ARRAY_SIZE(hi8435_sensing_modes),
  269. .get = hi8435_get_sensing_mode,
  270. .set = hi8435_set_sensing_mode,
  271. };
  272. static const struct iio_chan_spec_ext_info hi8435_ext_info[] = {
  273. IIO_ENUM("sensing_mode", IIO_SEPARATE, &hi8435_sensing_mode),
  274. {},
  275. };
  276. #define HI8435_VOLTAGE_CHANNEL(num) \
  277. { \
  278. .type = IIO_VOLTAGE, \
  279. .indexed = 1, \
  280. .channel = num, \
  281. .event_spec = hi8435_events, \
  282. .num_event_specs = ARRAY_SIZE(hi8435_events), \
  283. .ext_info = hi8435_ext_info, \
  284. }
  285. static const struct iio_chan_spec hi8435_channels[] = {
  286. HI8435_VOLTAGE_CHANNEL(0),
  287. HI8435_VOLTAGE_CHANNEL(1),
  288. HI8435_VOLTAGE_CHANNEL(2),
  289. HI8435_VOLTAGE_CHANNEL(3),
  290. HI8435_VOLTAGE_CHANNEL(4),
  291. HI8435_VOLTAGE_CHANNEL(5),
  292. HI8435_VOLTAGE_CHANNEL(6),
  293. HI8435_VOLTAGE_CHANNEL(7),
  294. HI8435_VOLTAGE_CHANNEL(8),
  295. HI8435_VOLTAGE_CHANNEL(9),
  296. HI8435_VOLTAGE_CHANNEL(10),
  297. HI8435_VOLTAGE_CHANNEL(11),
  298. HI8435_VOLTAGE_CHANNEL(12),
  299. HI8435_VOLTAGE_CHANNEL(13),
  300. HI8435_VOLTAGE_CHANNEL(14),
  301. HI8435_VOLTAGE_CHANNEL(15),
  302. HI8435_VOLTAGE_CHANNEL(16),
  303. HI8435_VOLTAGE_CHANNEL(17),
  304. HI8435_VOLTAGE_CHANNEL(18),
  305. HI8435_VOLTAGE_CHANNEL(19),
  306. HI8435_VOLTAGE_CHANNEL(20),
  307. HI8435_VOLTAGE_CHANNEL(21),
  308. HI8435_VOLTAGE_CHANNEL(22),
  309. HI8435_VOLTAGE_CHANNEL(23),
  310. HI8435_VOLTAGE_CHANNEL(24),
  311. HI8435_VOLTAGE_CHANNEL(25),
  312. HI8435_VOLTAGE_CHANNEL(26),
  313. HI8435_VOLTAGE_CHANNEL(27),
  314. HI8435_VOLTAGE_CHANNEL(28),
  315. HI8435_VOLTAGE_CHANNEL(29),
  316. HI8435_VOLTAGE_CHANNEL(30),
  317. HI8435_VOLTAGE_CHANNEL(31),
  318. IIO_CHAN_SOFT_TIMESTAMP(32),
  319. };
  320. static const struct iio_info hi8435_info = {
  321. .driver_module = THIS_MODULE,
  322. .read_event_config = &hi8435_read_event_config,
  323. .write_event_config = hi8435_write_event_config,
  324. .read_event_value = &hi8435_read_event_value,
  325. .write_event_value = &hi8435_write_event_value,
  326. .debugfs_reg_access = &hi8435_debugfs_reg_access,
  327. };
  328. static void hi8435_iio_push_event(struct iio_dev *idev, unsigned int val)
  329. {
  330. struct hi8435_priv *priv = iio_priv(idev);
  331. enum iio_event_direction dir;
  332. unsigned int i;
  333. unsigned int status = priv->event_prev_val ^ val;
  334. if (!status)
  335. return;
  336. for_each_set_bit(i, &priv->event_scan_mask, 32) {
  337. if (status & BIT(i)) {
  338. dir = val & BIT(i) ? IIO_EV_DIR_RISING :
  339. IIO_EV_DIR_FALLING;
  340. iio_push_event(idev,
  341. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
  342. IIO_EV_TYPE_THRESH, dir),
  343. iio_get_time_ns(idev));
  344. }
  345. }
  346. priv->event_prev_val = val;
  347. }
  348. static irqreturn_t hi8435_trigger_handler(int irq, void *private)
  349. {
  350. struct iio_poll_func *pf = private;
  351. struct iio_dev *idev = pf->indio_dev;
  352. struct hi8435_priv *priv = iio_priv(idev);
  353. u32 val;
  354. int ret;
  355. ret = hi8435_readl(priv, HI8435_SO31_0_REG, &val);
  356. if (ret < 0)
  357. goto err_read;
  358. hi8435_iio_push_event(idev, val);
  359. err_read:
  360. iio_trigger_notify_done(idev->trig);
  361. return IRQ_HANDLED;
  362. }
  363. static int hi8435_probe(struct spi_device *spi)
  364. {
  365. struct iio_dev *idev;
  366. struct hi8435_priv *priv;
  367. struct gpio_desc *reset_gpio;
  368. int ret;
  369. idev = devm_iio_device_alloc(&spi->dev, sizeof(*priv));
  370. if (!idev)
  371. return -ENOMEM;
  372. priv = iio_priv(idev);
  373. priv->spi = spi;
  374. reset_gpio = devm_gpiod_get(&spi->dev, NULL, GPIOD_OUT_LOW);
  375. if (!IS_ERR(reset_gpio)) {
  376. /* need >=100ns low pulse to reset chip */
  377. gpiod_set_raw_value_cansleep(reset_gpio, 0);
  378. udelay(1);
  379. gpiod_set_raw_value_cansleep(reset_gpio, 1);
  380. } else {
  381. /* s/w reset chip if h/w reset is not available */
  382. hi8435_writeb(priv, HI8435_CTRL_REG, HI8435_CTRL_SRST);
  383. hi8435_writeb(priv, HI8435_CTRL_REG, 0);
  384. }
  385. spi_set_drvdata(spi, idev);
  386. mutex_init(&priv->lock);
  387. idev->dev.parent = &spi->dev;
  388. idev->dev.of_node = spi->dev.of_node;
  389. idev->name = spi_get_device_id(spi)->name;
  390. idev->modes = INDIO_DIRECT_MODE;
  391. idev->info = &hi8435_info;
  392. idev->channels = hi8435_channels;
  393. idev->num_channels = ARRAY_SIZE(hi8435_channels);
  394. /* unmask all events */
  395. priv->event_scan_mask = ~(0);
  396. /*
  397. * There is a restriction in the chip - the hysteresis can not be odd.
  398. * If the hysteresis is set to odd value then chip gets into lock state
  399. * and not functional anymore.
  400. * After chip reset the thresholds are in undefined state, so we need to
  401. * initialize thresholds to some initial values and then prevent
  402. * userspace setting odd hysteresis.
  403. *
  404. * Set threshold low voltage to 2V, threshold high voltage to 4V
  405. * for both GND-Open and Supply-Open sensing modes.
  406. */
  407. priv->threshold_lo[0] = priv->threshold_lo[1] = 2;
  408. priv->threshold_hi[0] = priv->threshold_hi[1] = 4;
  409. hi8435_writew(priv, HI8435_GOCENHYS_REG, 0x206);
  410. hi8435_writew(priv, HI8435_SOCENHYS_REG, 0x206);
  411. ret = iio_triggered_event_setup(idev, NULL, hi8435_trigger_handler);
  412. if (ret)
  413. return ret;
  414. ret = iio_device_register(idev);
  415. if (ret < 0) {
  416. dev_err(&spi->dev, "unable to register device\n");
  417. goto unregister_triggered_event;
  418. }
  419. return 0;
  420. unregister_triggered_event:
  421. iio_triggered_event_cleanup(idev);
  422. return ret;
  423. }
  424. static int hi8435_remove(struct spi_device *spi)
  425. {
  426. struct iio_dev *idev = spi_get_drvdata(spi);
  427. iio_device_unregister(idev);
  428. iio_triggered_event_cleanup(idev);
  429. return 0;
  430. }
  431. static const struct of_device_id hi8435_dt_ids[] = {
  432. { .compatible = "holt,hi8435" },
  433. {},
  434. };
  435. MODULE_DEVICE_TABLE(of, hi8435_dt_ids);
  436. static const struct spi_device_id hi8435_id[] = {
  437. { "hi8435", 0},
  438. { }
  439. };
  440. MODULE_DEVICE_TABLE(spi, hi8435_id);
  441. static struct spi_driver hi8435_driver = {
  442. .driver = {
  443. .name = DRV_NAME,
  444. .of_match_table = of_match_ptr(hi8435_dt_ids),
  445. },
  446. .probe = hi8435_probe,
  447. .remove = hi8435_remove,
  448. .id_table = hi8435_id,
  449. };
  450. module_spi_driver(hi8435_driver);
  451. MODULE_LICENSE("GPL");
  452. MODULE_AUTHOR("Vladimir Barinov");
  453. MODULE_DESCRIPTION("HI-8435 threshold detector");