PageRenderTime 72ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/iio/adc/hi8435.c

https://gitlab.com/openbar/rpi-linux
C | 534 lines | 405 code | 94 blank | 35 comment | 42 complexity | 620f61218b2df88a0bd687512fb9aaea MD5 | raw file
  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. priv->event_scan_mask &= ~BIT(chan->channel);
  103. if (state)
  104. priv->event_scan_mask |= BIT(chan->channel);
  105. return 0;
  106. }
  107. static int hi8435_read_event_value(struct iio_dev *idev,
  108. const struct iio_chan_spec *chan,
  109. enum iio_event_type type,
  110. enum iio_event_direction dir,
  111. enum iio_event_info info,
  112. int *val, int *val2)
  113. {
  114. struct hi8435_priv *priv = iio_priv(idev);
  115. int ret;
  116. u8 mode, psen;
  117. u16 reg;
  118. ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
  119. if (ret < 0)
  120. return ret;
  121. /* Supply-Open or GND-Open sensing mode */
  122. mode = !!(psen & BIT(chan->channel / 8));
  123. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  124. HI8435_GOCENHYS_REG, &reg);
  125. if (ret < 0)
  126. return ret;
  127. if (dir == IIO_EV_DIR_FALLING)
  128. *val = ((reg & 0xff) - (reg >> 8)) / 2;
  129. else if (dir == IIO_EV_DIR_RISING)
  130. *val = ((reg & 0xff) + (reg >> 8)) / 2;
  131. return IIO_VAL_INT;
  132. }
  133. static int hi8435_write_event_value(struct iio_dev *idev,
  134. const struct iio_chan_spec *chan,
  135. enum iio_event_type type,
  136. enum iio_event_direction dir,
  137. enum iio_event_info info,
  138. int val, int val2)
  139. {
  140. struct hi8435_priv *priv = iio_priv(idev);
  141. int ret;
  142. u8 mode, psen;
  143. u16 reg;
  144. ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
  145. if (ret < 0)
  146. return ret;
  147. /* Supply-Open or GND-Open sensing mode */
  148. mode = !!(psen & BIT(chan->channel / 8));
  149. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  150. HI8435_GOCENHYS_REG, &reg);
  151. if (ret < 0)
  152. return ret;
  153. if (dir == IIO_EV_DIR_FALLING) {
  154. /* falling threshold range 2..21V, hysteresis minimum 2V */
  155. if (val < 2 || val > 21 || (val + 2) > priv->threshold_hi[mode])
  156. return -EINVAL;
  157. if (val == priv->threshold_lo[mode])
  158. return 0;
  159. priv->threshold_lo[mode] = val;
  160. /* hysteresis must not be odd */
  161. if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
  162. priv->threshold_hi[mode]--;
  163. } else if (dir == IIO_EV_DIR_RISING) {
  164. /* rising threshold range 3..22V, hysteresis minimum 2V */
  165. if (val < 3 || val > 22 || val < (priv->threshold_lo[mode] + 2))
  166. return -EINVAL;
  167. if (val == priv->threshold_hi[mode])
  168. return 0;
  169. priv->threshold_hi[mode] = val;
  170. /* hysteresis must not be odd */
  171. if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
  172. priv->threshold_lo[mode]++;
  173. }
  174. /* program thresholds */
  175. mutex_lock(&priv->lock);
  176. ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
  177. HI8435_GOCENHYS_REG, &reg);
  178. if (ret < 0) {
  179. mutex_unlock(&priv->lock);
  180. return ret;
  181. }
  182. /* hysteresis */
  183. reg = priv->threshold_hi[mode] - priv->threshold_lo[mode];
  184. reg <<= 8;
  185. /* threshold center */
  186. reg |= (priv->threshold_hi[mode] + priv->threshold_lo[mode]);
  187. ret = hi8435_writew(priv, mode ? HI8435_SOCENHYS_REG :
  188. HI8435_GOCENHYS_REG, reg);
  189. mutex_unlock(&priv->lock);
  190. return ret;
  191. }
  192. static int hi8435_debugfs_reg_access(struct iio_dev *idev,
  193. unsigned reg, unsigned writeval,
  194. unsigned *readval)
  195. {
  196. struct hi8435_priv *priv = iio_priv(idev);
  197. int ret;
  198. u8 val;
  199. if (readval != NULL) {
  200. ret = hi8435_readb(priv, reg, &val);
  201. *readval = val;
  202. } else {
  203. val = (u8)writeval;
  204. ret = hi8435_writeb(priv, reg, val);
  205. }
  206. return ret;
  207. }
  208. static const struct iio_event_spec hi8435_events[] = {
  209. {
  210. .type = IIO_EV_TYPE_THRESH,
  211. .dir = IIO_EV_DIR_RISING,
  212. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  213. }, {
  214. .type = IIO_EV_TYPE_THRESH,
  215. .dir = IIO_EV_DIR_FALLING,
  216. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  217. }, {
  218. .type = IIO_EV_TYPE_THRESH,
  219. .dir = IIO_EV_DIR_EITHER,
  220. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  221. },
  222. };
  223. static int hi8435_get_sensing_mode(struct iio_dev *idev,
  224. const struct iio_chan_spec *chan)
  225. {
  226. struct hi8435_priv *priv = iio_priv(idev);
  227. int ret;
  228. u8 reg;
  229. ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg);
  230. if (ret < 0)
  231. return ret;
  232. return !!(reg & BIT(chan->channel / 8));
  233. }
  234. static int hi8435_set_sensing_mode(struct iio_dev *idev,
  235. const struct iio_chan_spec *chan,
  236. unsigned int mode)
  237. {
  238. struct hi8435_priv *priv = iio_priv(idev);
  239. int ret;
  240. u8 reg;
  241. mutex_lock(&priv->lock);
  242. ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg);
  243. if (ret < 0) {
  244. mutex_unlock(&priv->lock);
  245. return ret;
  246. }
  247. reg &= ~BIT(chan->channel / 8);
  248. if (mode)
  249. reg |= BIT(chan->channel / 8);
  250. ret = hi8435_writeb(priv, HI8435_PSEN_REG, reg);
  251. mutex_unlock(&priv->lock);
  252. return ret;
  253. }
  254. static const char * const hi8435_sensing_modes[] = { "GND-Open",
  255. "Supply-Open" };
  256. static const struct iio_enum hi8435_sensing_mode = {
  257. .items = hi8435_sensing_modes,
  258. .num_items = ARRAY_SIZE(hi8435_sensing_modes),
  259. .get = hi8435_get_sensing_mode,
  260. .set = hi8435_set_sensing_mode,
  261. };
  262. static const struct iio_chan_spec_ext_info hi8435_ext_info[] = {
  263. IIO_ENUM("sensing_mode", IIO_SEPARATE, &hi8435_sensing_mode),
  264. {},
  265. };
  266. #define HI8435_VOLTAGE_CHANNEL(num) \
  267. { \
  268. .type = IIO_VOLTAGE, \
  269. .indexed = 1, \
  270. .channel = num, \
  271. .event_spec = hi8435_events, \
  272. .num_event_specs = ARRAY_SIZE(hi8435_events), \
  273. .ext_info = hi8435_ext_info, \
  274. }
  275. static const struct iio_chan_spec hi8435_channels[] = {
  276. HI8435_VOLTAGE_CHANNEL(0),
  277. HI8435_VOLTAGE_CHANNEL(1),
  278. HI8435_VOLTAGE_CHANNEL(2),
  279. HI8435_VOLTAGE_CHANNEL(3),
  280. HI8435_VOLTAGE_CHANNEL(4),
  281. HI8435_VOLTAGE_CHANNEL(5),
  282. HI8435_VOLTAGE_CHANNEL(6),
  283. HI8435_VOLTAGE_CHANNEL(7),
  284. HI8435_VOLTAGE_CHANNEL(8),
  285. HI8435_VOLTAGE_CHANNEL(9),
  286. HI8435_VOLTAGE_CHANNEL(10),
  287. HI8435_VOLTAGE_CHANNEL(11),
  288. HI8435_VOLTAGE_CHANNEL(12),
  289. HI8435_VOLTAGE_CHANNEL(13),
  290. HI8435_VOLTAGE_CHANNEL(14),
  291. HI8435_VOLTAGE_CHANNEL(15),
  292. HI8435_VOLTAGE_CHANNEL(16),
  293. HI8435_VOLTAGE_CHANNEL(17),
  294. HI8435_VOLTAGE_CHANNEL(18),
  295. HI8435_VOLTAGE_CHANNEL(19),
  296. HI8435_VOLTAGE_CHANNEL(20),
  297. HI8435_VOLTAGE_CHANNEL(21),
  298. HI8435_VOLTAGE_CHANNEL(22),
  299. HI8435_VOLTAGE_CHANNEL(23),
  300. HI8435_VOLTAGE_CHANNEL(24),
  301. HI8435_VOLTAGE_CHANNEL(25),
  302. HI8435_VOLTAGE_CHANNEL(26),
  303. HI8435_VOLTAGE_CHANNEL(27),
  304. HI8435_VOLTAGE_CHANNEL(28),
  305. HI8435_VOLTAGE_CHANNEL(29),
  306. HI8435_VOLTAGE_CHANNEL(30),
  307. HI8435_VOLTAGE_CHANNEL(31),
  308. IIO_CHAN_SOFT_TIMESTAMP(32),
  309. };
  310. static const struct iio_info hi8435_info = {
  311. .driver_module = THIS_MODULE,
  312. .read_event_config = &hi8435_read_event_config,
  313. .write_event_config = hi8435_write_event_config,
  314. .read_event_value = &hi8435_read_event_value,
  315. .write_event_value = &hi8435_write_event_value,
  316. .debugfs_reg_access = &hi8435_debugfs_reg_access,
  317. };
  318. static void hi8435_iio_push_event(struct iio_dev *idev, unsigned int val)
  319. {
  320. struct hi8435_priv *priv = iio_priv(idev);
  321. enum iio_event_direction dir;
  322. unsigned int i;
  323. unsigned int status = priv->event_prev_val ^ val;
  324. if (!status)
  325. return;
  326. for_each_set_bit(i, &priv->event_scan_mask, 32) {
  327. if (status & BIT(i)) {
  328. dir = val & BIT(i) ? IIO_EV_DIR_RISING :
  329. IIO_EV_DIR_FALLING;
  330. iio_push_event(idev,
  331. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
  332. IIO_EV_TYPE_THRESH, dir),
  333. iio_get_time_ns());
  334. }
  335. }
  336. priv->event_prev_val = val;
  337. }
  338. static irqreturn_t hi8435_trigger_handler(int irq, void *private)
  339. {
  340. struct iio_poll_func *pf = private;
  341. struct iio_dev *idev = pf->indio_dev;
  342. struct hi8435_priv *priv = iio_priv(idev);
  343. u32 val;
  344. int ret;
  345. ret = hi8435_readl(priv, HI8435_SO31_0_REG, &val);
  346. if (ret < 0)
  347. goto err_read;
  348. hi8435_iio_push_event(idev, val);
  349. err_read:
  350. iio_trigger_notify_done(idev->trig);
  351. return IRQ_HANDLED;
  352. }
  353. static int hi8435_probe(struct spi_device *spi)
  354. {
  355. struct iio_dev *idev;
  356. struct hi8435_priv *priv;
  357. struct gpio_desc *reset_gpio;
  358. int ret;
  359. idev = devm_iio_device_alloc(&spi->dev, sizeof(*priv));
  360. if (!idev)
  361. return -ENOMEM;
  362. priv = iio_priv(idev);
  363. priv->spi = spi;
  364. reset_gpio = devm_gpiod_get(&spi->dev, NULL, GPIOD_OUT_LOW);
  365. if (IS_ERR(reset_gpio)) {
  366. /* chip s/w reset if h/w reset failed */
  367. hi8435_writeb(priv, HI8435_CTRL_REG, HI8435_CTRL_SRST);
  368. hi8435_writeb(priv, HI8435_CTRL_REG, 0);
  369. } else {
  370. udelay(5);
  371. gpiod_set_value(reset_gpio, 1);
  372. }
  373. spi_set_drvdata(spi, idev);
  374. mutex_init(&priv->lock);
  375. idev->dev.parent = &spi->dev;
  376. idev->name = spi_get_device_id(spi)->name;
  377. idev->modes = INDIO_DIRECT_MODE;
  378. idev->info = &hi8435_info;
  379. idev->channels = hi8435_channels;
  380. idev->num_channels = ARRAY_SIZE(hi8435_channels);
  381. /* unmask all events */
  382. priv->event_scan_mask = ~(0);
  383. /*
  384. * There is a restriction in the chip - the hysteresis can not be odd.
  385. * If the hysteresis is set to odd value then chip gets into lock state
  386. * and not functional anymore.
  387. * After chip reset the thresholds are in undefined state, so we need to
  388. * initialize thresholds to some initial values and then prevent
  389. * userspace setting odd hysteresis.
  390. *
  391. * Set threshold low voltage to 2V, threshold high voltage to 4V
  392. * for both GND-Open and Supply-Open sensing modes.
  393. */
  394. priv->threshold_lo[0] = priv->threshold_lo[1] = 2;
  395. priv->threshold_hi[0] = priv->threshold_hi[1] = 4;
  396. hi8435_writew(priv, HI8435_GOCENHYS_REG, 0x206);
  397. hi8435_writew(priv, HI8435_SOCENHYS_REG, 0x206);
  398. ret = iio_triggered_event_setup(idev, NULL, hi8435_trigger_handler);
  399. if (ret)
  400. return ret;
  401. ret = iio_device_register(idev);
  402. if (ret < 0) {
  403. dev_err(&spi->dev, "unable to register device\n");
  404. goto unregister_triggered_event;
  405. }
  406. return 0;
  407. unregister_triggered_event:
  408. iio_triggered_event_cleanup(idev);
  409. return ret;
  410. }
  411. static int hi8435_remove(struct spi_device *spi)
  412. {
  413. struct iio_dev *idev = spi_get_drvdata(spi);
  414. iio_device_unregister(idev);
  415. iio_triggered_event_cleanup(idev);
  416. return 0;
  417. }
  418. static const struct of_device_id hi8435_dt_ids[] = {
  419. { .compatible = "holt,hi8435" },
  420. {},
  421. };
  422. MODULE_DEVICE_TABLE(of, hi8435_dt_ids);
  423. static const struct spi_device_id hi8435_id[] = {
  424. { "hi8435", 0},
  425. { }
  426. };
  427. MODULE_DEVICE_TABLE(spi, hi8435_id);
  428. static struct spi_driver hi8435_driver = {
  429. .driver = {
  430. .name = DRV_NAME,
  431. .of_match_table = of_match_ptr(hi8435_dt_ids),
  432. },
  433. .probe = hi8435_probe,
  434. .remove = hi8435_remove,
  435. .id_table = hi8435_id,
  436. };
  437. module_spi_driver(hi8435_driver);
  438. MODULE_LICENSE("GPL");
  439. MODULE_AUTHOR("Vladimir Barinov");
  440. MODULE_DESCRIPTION("HI-8435 threshold detector");