qcom-spmi-iadc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mutex.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. /* IADC register and bit definition */
  28. #define IADC_REVISION2 0x1
  29. #define IADC_REVISION2_SUPPORTED_IADC 1
  30. #define IADC_PERPH_TYPE 0x4
  31. #define IADC_PERPH_TYPE_ADC 8
  32. #define IADC_PERPH_SUBTYPE 0x5
  33. #define IADC_PERPH_SUBTYPE_IADC 3
  34. #define IADC_STATUS1 0x8
  35. #define IADC_STATUS1_OP_MODE 4
  36. #define IADC_STATUS1_REQ_STS BIT(1)
  37. #define IADC_STATUS1_EOC BIT(0)
  38. #define IADC_STATUS1_REQ_STS_EOC_MASK 0x3
  39. #define IADC_MODE_CTL 0x40
  40. #define IADC_OP_MODE_SHIFT 3
  41. #define IADC_OP_MODE_NORMAL 0
  42. #define IADC_TRIM_EN BIT(0)
  43. #define IADC_EN_CTL1 0x46
  44. #define IADC_EN_CTL1_SET BIT(7)
  45. #define IADC_CH_SEL_CTL 0x48
  46. #define IADC_DIG_PARAM 0x50
  47. #define IADC_DIG_DEC_RATIO_SEL_SHIFT 2
  48. #define IADC_HW_SETTLE_DELAY 0x51
  49. #define IADC_CONV_REQ 0x52
  50. #define IADC_CONV_REQ_SET BIT(7)
  51. #define IADC_FAST_AVG_CTL 0x5a
  52. #define IADC_FAST_AVG_EN 0x5b
  53. #define IADC_FAST_AVG_EN_SET BIT(7)
  54. #define IADC_PERH_RESET_CTL3 0xda
  55. #define IADC_FOLLOW_WARM_RB BIT(2)
  56. #define IADC_DATA 0x60 /* 16 bits */
  57. #define IADC_SEC_ACCESS 0xd0
  58. #define IADC_SEC_ACCESS_DATA 0xa5
  59. #define IADC_NOMINAL_RSENSE 0xf4
  60. #define IADC_NOMINAL_RSENSE_SIGN_MASK BIT(7)
  61. #define IADC_REF_GAIN_MICRO_VOLTS 17857
  62. #define IADC_INT_RSENSE_DEVIATION 15625 /* nano Ohms per bit */
  63. #define IADC_INT_RSENSE_IDEAL_VALUE 10000 /* micro Ohms */
  64. #define IADC_INT_RSENSE_DEFAULT_VALUE 7800 /* micro Ohms */
  65. #define IADC_INT_RSENSE_DEFAULT_GF 9000 /* micro Ohms */
  66. #define IADC_INT_RSENSE_DEFAULT_SMIC 9700 /* micro Ohms */
  67. #define IADC_CONV_TIME_MIN_US 2000
  68. #define IADC_CONV_TIME_MAX_US 2100
  69. #define IADC_DEF_PRESCALING 0 /* 1:1 */
  70. #define IADC_DEF_DECIMATION 0 /* 512 */
  71. #define IADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
  72. #define IADC_DEF_AVG_SAMPLES 0 /* 1 sample */
  73. /* IADC channel list */
  74. #define IADC_INT_RSENSE 0
  75. #define IADC_EXT_RSENSE 1
  76. #define IADC_GAIN_17P857MV 3
  77. #define IADC_EXT_OFFSET_CSP_CSN 5
  78. #define IADC_INT_OFFSET_CSP2_CSN2 6
  79. /**
  80. * struct iadc_chip - IADC Current ADC device structure.
  81. * @regmap: regmap for register read/write.
  82. * @dev: This device pointer.
  83. * @base: base offset for the ADC peripheral.
  84. * @rsense: Values of the internal and external sense resister in micro Ohms.
  85. * @poll_eoc: Poll for end of conversion instead of waiting for IRQ.
  86. * @offset: Raw offset values for the internal and external channels.
  87. * @gain: Raw gain of the channels.
  88. * @lock: ADC lock for access to the peripheral.
  89. * @complete: ADC notification after end of conversion interrupt is received.
  90. */
  91. struct iadc_chip {
  92. struct regmap *regmap;
  93. struct device *dev;
  94. u16 base;
  95. bool poll_eoc;
  96. u32 rsense[2];
  97. u16 offset[2];
  98. u16 gain;
  99. struct mutex lock;
  100. struct completion complete;
  101. };
  102. static int iadc_read(struct iadc_chip *iadc, u16 offset, u8 *data)
  103. {
  104. unsigned int val;
  105. int ret;
  106. ret = regmap_read(iadc->regmap, iadc->base + offset, &val);
  107. if (ret < 0)
  108. return ret;
  109. *data = val;
  110. return 0;
  111. }
  112. static int iadc_write(struct iadc_chip *iadc, u16 offset, u8 data)
  113. {
  114. return regmap_write(iadc->regmap, iadc->base + offset, data);
  115. }
  116. static int iadc_reset(struct iadc_chip *iadc)
  117. {
  118. u8 data;
  119. int ret;
  120. ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
  121. if (ret < 0)
  122. return ret;
  123. ret = iadc_read(iadc, IADC_PERH_RESET_CTL3, &data);
  124. if (ret < 0)
  125. return ret;
  126. ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
  127. if (ret < 0)
  128. return ret;
  129. data |= IADC_FOLLOW_WARM_RB;
  130. return iadc_write(iadc, IADC_PERH_RESET_CTL3, data);
  131. }
  132. static int iadc_set_state(struct iadc_chip *iadc, bool state)
  133. {
  134. return iadc_write(iadc, IADC_EN_CTL1, state ? IADC_EN_CTL1_SET : 0);
  135. }
  136. static void iadc_status_show(struct iadc_chip *iadc)
  137. {
  138. u8 mode, sta1, chan, dig, en, req;
  139. int ret;
  140. ret = iadc_read(iadc, IADC_MODE_CTL, &mode);
  141. if (ret < 0)
  142. return;
  143. ret = iadc_read(iadc, IADC_DIG_PARAM, &dig);
  144. if (ret < 0)
  145. return;
  146. ret = iadc_read(iadc, IADC_CH_SEL_CTL, &chan);
  147. if (ret < 0)
  148. return;
  149. ret = iadc_read(iadc, IADC_CONV_REQ, &req);
  150. if (ret < 0)
  151. return;
  152. ret = iadc_read(iadc, IADC_STATUS1, &sta1);
  153. if (ret < 0)
  154. return;
  155. ret = iadc_read(iadc, IADC_EN_CTL1, &en);
  156. if (ret < 0)
  157. return;
  158. dev_err(iadc->dev,
  159. "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
  160. mode, en, chan, dig, req, sta1);
  161. }
  162. static int iadc_configure(struct iadc_chip *iadc, int channel)
  163. {
  164. u8 decim, mode;
  165. int ret;
  166. /* Mode selection */
  167. mode = (IADC_OP_MODE_NORMAL << IADC_OP_MODE_SHIFT) | IADC_TRIM_EN;
  168. ret = iadc_write(iadc, IADC_MODE_CTL, mode);
  169. if (ret < 0)
  170. return ret;
  171. /* Channel selection */
  172. ret = iadc_write(iadc, IADC_CH_SEL_CTL, channel);
  173. if (ret < 0)
  174. return ret;
  175. /* Digital parameter setup */
  176. decim = IADC_DEF_DECIMATION << IADC_DIG_DEC_RATIO_SEL_SHIFT;
  177. ret = iadc_write(iadc, IADC_DIG_PARAM, decim);
  178. if (ret < 0)
  179. return ret;
  180. /* HW settle time delay */
  181. ret = iadc_write(iadc, IADC_HW_SETTLE_DELAY, IADC_DEF_HW_SETTLE_TIME);
  182. if (ret < 0)
  183. return ret;
  184. ret = iadc_write(iadc, IADC_FAST_AVG_CTL, IADC_DEF_AVG_SAMPLES);
  185. if (ret < 0)
  186. return ret;
  187. if (IADC_DEF_AVG_SAMPLES)
  188. ret = iadc_write(iadc, IADC_FAST_AVG_EN, IADC_FAST_AVG_EN_SET);
  189. else
  190. ret = iadc_write(iadc, IADC_FAST_AVG_EN, 0);
  191. if (ret < 0)
  192. return ret;
  193. if (!iadc->poll_eoc)
  194. reinit_completion(&iadc->complete);
  195. ret = iadc_set_state(iadc, true);
  196. if (ret < 0)
  197. return ret;
  198. /* Request conversion */
  199. return iadc_write(iadc, IADC_CONV_REQ, IADC_CONV_REQ_SET);
  200. }
  201. static int iadc_poll_wait_eoc(struct iadc_chip *iadc, unsigned int interval_us)
  202. {
  203. unsigned int count, retry;
  204. int ret;
  205. u8 sta1;
  206. retry = interval_us / IADC_CONV_TIME_MIN_US;
  207. for (count = 0; count < retry; count++) {
  208. ret = iadc_read(iadc, IADC_STATUS1, &sta1);
  209. if (ret < 0)
  210. return ret;
  211. sta1 &= IADC_STATUS1_REQ_STS_EOC_MASK;
  212. if (sta1 == IADC_STATUS1_EOC)
  213. return 0;
  214. usleep_range(IADC_CONV_TIME_MIN_US, IADC_CONV_TIME_MAX_US);
  215. }
  216. iadc_status_show(iadc);
  217. return -ETIMEDOUT;
  218. }
  219. static int iadc_read_result(struct iadc_chip *iadc, u16 *data)
  220. {
  221. return regmap_bulk_read(iadc->regmap, iadc->base + IADC_DATA, data, 2);
  222. }
  223. static int iadc_do_conversion(struct iadc_chip *iadc, int chan, u16 *data)
  224. {
  225. unsigned int wait;
  226. int ret;
  227. ret = iadc_configure(iadc, chan);
  228. if (ret < 0)
  229. goto exit;
  230. wait = BIT(IADC_DEF_AVG_SAMPLES) * IADC_CONV_TIME_MIN_US * 2;
  231. if (iadc->poll_eoc) {
  232. ret = iadc_poll_wait_eoc(iadc, wait);
  233. } else {
  234. ret = wait_for_completion_timeout(&iadc->complete,
  235. usecs_to_jiffies(wait));
  236. if (!ret)
  237. ret = -ETIMEDOUT;
  238. else
  239. /* double check conversion status */
  240. ret = iadc_poll_wait_eoc(iadc, IADC_CONV_TIME_MIN_US);
  241. }
  242. if (!ret)
  243. ret = iadc_read_result(iadc, data);
  244. exit:
  245. iadc_set_state(iadc, false);
  246. if (ret < 0)
  247. dev_err(iadc->dev, "conversion failed\n");
  248. return ret;
  249. }
  250. static int iadc_read_raw(struct iio_dev *indio_dev,
  251. struct iio_chan_spec const *chan,
  252. int *val, int *val2, long mask)
  253. {
  254. struct iadc_chip *iadc = iio_priv(indio_dev);
  255. s32 isense_ua, vsense_uv;
  256. u16 adc_raw, vsense_raw;
  257. int ret;
  258. switch (mask) {
  259. case IIO_CHAN_INFO_RAW:
  260. mutex_lock(&iadc->lock);
  261. ret = iadc_do_conversion(iadc, chan->channel, &adc_raw);
  262. mutex_unlock(&iadc->lock);
  263. if (ret < 0)
  264. return ret;
  265. vsense_raw = adc_raw - iadc->offset[chan->channel];
  266. vsense_uv = vsense_raw * IADC_REF_GAIN_MICRO_VOLTS;
  267. vsense_uv /= (s32)iadc->gain - iadc->offset[chan->channel];
  268. isense_ua = vsense_uv / iadc->rsense[chan->channel];
  269. dev_dbg(iadc->dev, "off %d gain %d adc %d %duV I %duA\n",
  270. iadc->offset[chan->channel], iadc->gain,
  271. adc_raw, vsense_uv, isense_ua);
  272. *val = isense_ua;
  273. return IIO_VAL_INT;
  274. case IIO_CHAN_INFO_SCALE:
  275. *val = 0;
  276. *val2 = 1000;
  277. return IIO_VAL_INT_PLUS_MICRO;
  278. }
  279. return -EINVAL;
  280. }
  281. static const struct iio_info iadc_info = {
  282. .read_raw = iadc_read_raw,
  283. .driver_module = THIS_MODULE,
  284. };
  285. static irqreturn_t iadc_isr(int irq, void *dev_id)
  286. {
  287. struct iadc_chip *iadc = dev_id;
  288. complete(&iadc->complete);
  289. return IRQ_HANDLED;
  290. }
  291. static int iadc_update_offset(struct iadc_chip *iadc)
  292. {
  293. int ret;
  294. ret = iadc_do_conversion(iadc, IADC_GAIN_17P857MV, &iadc->gain);
  295. if (ret < 0)
  296. return ret;
  297. ret = iadc_do_conversion(iadc, IADC_INT_OFFSET_CSP2_CSN2,
  298. &iadc->offset[IADC_INT_RSENSE]);
  299. if (ret < 0)
  300. return ret;
  301. if (iadc->gain == iadc->offset[IADC_INT_RSENSE]) {
  302. dev_err(iadc->dev, "error: internal offset == gain %d\n",
  303. iadc->gain);
  304. return -EINVAL;
  305. }
  306. ret = iadc_do_conversion(iadc, IADC_EXT_OFFSET_CSP_CSN,
  307. &iadc->offset[IADC_EXT_RSENSE]);
  308. if (ret < 0)
  309. return ret;
  310. if (iadc->gain == iadc->offset[IADC_EXT_RSENSE]) {
  311. dev_err(iadc->dev, "error: external offset == gain %d\n",
  312. iadc->gain);
  313. return -EINVAL;
  314. }
  315. return 0;
  316. }
  317. static int iadc_version_check(struct iadc_chip *iadc)
  318. {
  319. u8 val;
  320. int ret;
  321. ret = iadc_read(iadc, IADC_PERPH_TYPE, &val);
  322. if (ret < 0)
  323. return ret;
  324. if (val < IADC_PERPH_TYPE_ADC) {
  325. dev_err(iadc->dev, "%d is not ADC\n", val);
  326. return -EINVAL;
  327. }
  328. ret = iadc_read(iadc, IADC_PERPH_SUBTYPE, &val);
  329. if (ret < 0)
  330. return ret;
  331. if (val < IADC_PERPH_SUBTYPE_IADC) {
  332. dev_err(iadc->dev, "%d is not IADC\n", val);
  333. return -EINVAL;
  334. }
  335. ret = iadc_read(iadc, IADC_REVISION2, &val);
  336. if (ret < 0)
  337. return ret;
  338. if (val < IADC_REVISION2_SUPPORTED_IADC) {
  339. dev_err(iadc->dev, "revision %d not supported\n", val);
  340. return -EINVAL;
  341. }
  342. return 0;
  343. }
  344. static int iadc_rsense_read(struct iadc_chip *iadc, struct device_node *node)
  345. {
  346. int ret, sign, int_sense;
  347. u8 deviation;
  348. ret = of_property_read_u32(node, "qcom,external-resistor-micro-ohms",
  349. &iadc->rsense[IADC_EXT_RSENSE]);
  350. if (ret < 0)
  351. iadc->rsense[IADC_EXT_RSENSE] = IADC_INT_RSENSE_IDEAL_VALUE;
  352. if (!iadc->rsense[IADC_EXT_RSENSE]) {
  353. dev_err(iadc->dev, "external resistor can't be zero Ohms");
  354. return -EINVAL;
  355. }
  356. ret = iadc_read(iadc, IADC_NOMINAL_RSENSE, &deviation);
  357. if (ret < 0)
  358. return ret;
  359. /*
  360. * Deviation value stored is an offset from 10 mili Ohms, bit 7 is
  361. * the sign, the remaining bits have an LSB of 15625 nano Ohms.
  362. */
  363. sign = (deviation & IADC_NOMINAL_RSENSE_SIGN_MASK) ? -1 : 1;
  364. deviation &= ~IADC_NOMINAL_RSENSE_SIGN_MASK;
  365. /* Scale it to nono Ohms */
  366. int_sense = IADC_INT_RSENSE_IDEAL_VALUE * 1000;
  367. int_sense += sign * deviation * IADC_INT_RSENSE_DEVIATION;
  368. int_sense /= 1000; /* micro Ohms */
  369. iadc->rsense[IADC_INT_RSENSE] = int_sense;
  370. return 0;
  371. }
  372. static const struct iio_chan_spec iadc_channels[] = {
  373. {
  374. .type = IIO_CURRENT,
  375. .datasheet_name = "INTERNAL_RSENSE",
  376. .channel = 0,
  377. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  378. BIT(IIO_CHAN_INFO_SCALE),
  379. .indexed = 1,
  380. },
  381. {
  382. .type = IIO_CURRENT,
  383. .datasheet_name = "EXTERNAL_RSENSE",
  384. .channel = 1,
  385. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  386. BIT(IIO_CHAN_INFO_SCALE),
  387. .indexed = 1,
  388. },
  389. };
  390. static int iadc_probe(struct platform_device *pdev)
  391. {
  392. struct device_node *node = pdev->dev.of_node;
  393. struct device *dev = &pdev->dev;
  394. struct iio_dev *indio_dev;
  395. struct iadc_chip *iadc;
  396. int ret, irq_eoc;
  397. u32 res;
  398. indio_dev = devm_iio_device_alloc(dev, sizeof(*iadc));
  399. if (!indio_dev)
  400. return -ENOMEM;
  401. iadc = iio_priv(indio_dev);
  402. iadc->dev = dev;
  403. iadc->regmap = dev_get_regmap(dev->parent, NULL);
  404. if (!iadc->regmap)
  405. return -ENODEV;
  406. init_completion(&iadc->complete);
  407. mutex_init(&iadc->lock);
  408. ret = of_property_read_u32(node, "reg", &res);
  409. if (ret < 0)
  410. return -ENODEV;
  411. iadc->base = res;
  412. ret = iadc_version_check(iadc);
  413. if (ret < 0)
  414. return -ENODEV;
  415. ret = iadc_rsense_read(iadc, node);
  416. if (ret < 0)
  417. return -ENODEV;
  418. dev_dbg(iadc->dev, "sense resistors %d and %d micro Ohm\n",
  419. iadc->rsense[IADC_INT_RSENSE],
  420. iadc->rsense[IADC_EXT_RSENSE]);
  421. irq_eoc = platform_get_irq(pdev, 0);
  422. if (irq_eoc == -EPROBE_DEFER)
  423. return irq_eoc;
  424. if (irq_eoc < 0)
  425. iadc->poll_eoc = true;
  426. ret = iadc_reset(iadc);
  427. if (ret < 0) {
  428. dev_err(dev, "reset failed\n");
  429. return ret;
  430. }
  431. if (!iadc->poll_eoc) {
  432. ret = devm_request_irq(dev, irq_eoc, iadc_isr, 0,
  433. "spmi-iadc", iadc);
  434. if (!ret)
  435. enable_irq_wake(irq_eoc);
  436. else
  437. return ret;
  438. } else {
  439. device_init_wakeup(iadc->dev, 1);
  440. }
  441. ret = iadc_update_offset(iadc);
  442. if (ret < 0) {
  443. dev_err(dev, "failed offset calibration\n");
  444. return ret;
  445. }
  446. indio_dev->dev.parent = dev;
  447. indio_dev->dev.of_node = node;
  448. indio_dev->name = pdev->name;
  449. indio_dev->modes = INDIO_DIRECT_MODE;
  450. indio_dev->info = &iadc_info;
  451. indio_dev->channels = iadc_channels;
  452. indio_dev->num_channels = ARRAY_SIZE(iadc_channels);
  453. return devm_iio_device_register(dev, indio_dev);
  454. }
  455. static const struct of_device_id iadc_match_table[] = {
  456. { .compatible = "qcom,spmi-iadc" },
  457. { }
  458. };
  459. MODULE_DEVICE_TABLE(of, iadc_match_table);
  460. static struct platform_driver iadc_driver = {
  461. .driver = {
  462. .name = "qcom-spmi-iadc",
  463. .of_match_table = iadc_match_table,
  464. },
  465. .probe = iadc_probe,
  466. };
  467. module_platform_driver(iadc_driver);
  468. MODULE_ALIAS("platform:qcom-spmi-iadc");
  469. MODULE_DESCRIPTION("Qualcomm SPMI PMIC current ADC driver");
  470. MODULE_LICENSE("GPL v2");
  471. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");