qcom-spmi-vadc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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/math64.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #include <linux/log2.h>
  27. #include <dt-bindings/iio/qcom,spmi-vadc.h>
  28. /* VADC register and bit definitions */
  29. #define VADC_REVISION2 0x1
  30. #define VADC_REVISION2_SUPPORTED_VADC 1
  31. #define VADC_PERPH_TYPE 0x4
  32. #define VADC_PERPH_TYPE_ADC 8
  33. #define VADC_PERPH_SUBTYPE 0x5
  34. #define VADC_PERPH_SUBTYPE_VADC 1
  35. #define VADC_STATUS1 0x8
  36. #define VADC_STATUS1_OP_MODE 4
  37. #define VADC_STATUS1_REQ_STS BIT(1)
  38. #define VADC_STATUS1_EOC BIT(0)
  39. #define VADC_STATUS1_REQ_STS_EOC_MASK 0x3
  40. #define VADC_MODE_CTL 0x40
  41. #define VADC_OP_MODE_SHIFT 3
  42. #define VADC_OP_MODE_NORMAL 0
  43. #define VADC_AMUX_TRIM_EN BIT(1)
  44. #define VADC_ADC_TRIM_EN BIT(0)
  45. #define VADC_EN_CTL1 0x46
  46. #define VADC_EN_CTL1_SET BIT(7)
  47. #define VADC_ADC_CH_SEL_CTL 0x48
  48. #define VADC_ADC_DIG_PARAM 0x50
  49. #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
  50. #define VADC_HW_SETTLE_DELAY 0x51
  51. #define VADC_CONV_REQ 0x52
  52. #define VADC_CONV_REQ_SET BIT(7)
  53. #define VADC_FAST_AVG_CTL 0x5a
  54. #define VADC_FAST_AVG_EN 0x5b
  55. #define VADC_FAST_AVG_EN_SET BIT(7)
  56. #define VADC_ACCESS 0xd0
  57. #define VADC_ACCESS_DATA 0xa5
  58. #define VADC_PERH_RESET_CTL3 0xda
  59. #define VADC_FOLLOW_WARM_RB BIT(2)
  60. #define VADC_DATA 0x60 /* 16 bits */
  61. #define VADC_CONV_TIME_MIN_US 2000
  62. #define VADC_CONV_TIME_MAX_US 2100
  63. /* Min ADC code represents 0V */
  64. #define VADC_MIN_ADC_CODE 0x6000
  65. /* Max ADC code represents full-scale range of 1.8V */
  66. #define VADC_MAX_ADC_CODE 0xa800
  67. #define VADC_ABSOLUTE_RANGE_UV 625000
  68. #define VADC_RATIOMETRIC_RANGE_UV 1800000
  69. #define VADC_DEF_PRESCALING 0 /* 1:1 */
  70. #define VADC_DEF_DECIMATION 0 /* 512 */
  71. #define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
  72. #define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */
  73. #define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE
  74. #define VADC_DECIMATION_MIN 512
  75. #define VADC_DECIMATION_MAX 4096
  76. #define VADC_HW_SETTLE_DELAY_MAX 10000
  77. #define VADC_AVG_SAMPLES_MAX 512
  78. #define KELVINMIL_CELSIUSMIL 273150
  79. #define VADC_CHAN_MIN VADC_USBIN
  80. #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
  81. /*
  82. * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
  83. * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
  84. * calibration.
  85. */
  86. enum vadc_calibration {
  87. VADC_CALIB_ABSOLUTE = 0,
  88. VADC_CALIB_RATIOMETRIC
  89. };
  90. /**
  91. * struct vadc_linear_graph - Represent ADC characteristics.
  92. * @dy: numerator slope to calculate the gain.
  93. * @dx: denominator slope to calculate the gain.
  94. * @gnd: A/D word of the ground reference used for the channel.
  95. *
  96. * Each ADC device has different offset and gain parameters which are
  97. * computed to calibrate the device.
  98. */
  99. struct vadc_linear_graph {
  100. s32 dy;
  101. s32 dx;
  102. s32 gnd;
  103. };
  104. /**
  105. * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
  106. * @num: the inverse numerator of the gain applied to the input channel.
  107. * @den: the inverse denominator of the gain applied to the input channel.
  108. */
  109. struct vadc_prescale_ratio {
  110. u32 num;
  111. u32 den;
  112. };
  113. /**
  114. * struct vadc_channel_prop - VADC channel property.
  115. * @channel: channel number, refer to the channel list.
  116. * @calibration: calibration type.
  117. * @decimation: sampling rate supported for the channel.
  118. * @prescale: channel scaling performed on the input signal.
  119. * @hw_settle_time: the time between AMUX being configured and the
  120. * start of conversion.
  121. * @avg_samples: ability to provide single result from the ADC
  122. * that is an average of multiple measurements.
  123. */
  124. struct vadc_channel_prop {
  125. unsigned int channel;
  126. enum vadc_calibration calibration;
  127. unsigned int decimation;
  128. unsigned int prescale;
  129. unsigned int hw_settle_time;
  130. unsigned int avg_samples;
  131. };
  132. /**
  133. * struct vadc_priv - VADC private structure.
  134. * @regmap: pointer to struct regmap.
  135. * @dev: pointer to struct device.
  136. * @base: base address for the ADC peripheral.
  137. * @nchannels: number of VADC channels.
  138. * @chan_props: array of VADC channel properties.
  139. * @iio_chans: array of IIO channels specification.
  140. * @are_ref_measured: are reference points measured.
  141. * @poll_eoc: use polling instead of interrupt.
  142. * @complete: VADC result notification after interrupt is received.
  143. * @graph: store parameters for calibration.
  144. * @lock: ADC lock for access to the peripheral.
  145. */
  146. struct vadc_priv {
  147. struct regmap *regmap;
  148. struct device *dev;
  149. u16 base;
  150. unsigned int nchannels;
  151. struct vadc_channel_prop *chan_props;
  152. struct iio_chan_spec *iio_chans;
  153. bool are_ref_measured;
  154. bool poll_eoc;
  155. struct completion complete;
  156. struct vadc_linear_graph graph[2];
  157. struct mutex lock;
  158. };
  159. static const struct vadc_prescale_ratio vadc_prescale_ratios[] = {
  160. {.num = 1, .den = 1},
  161. {.num = 1, .den = 3},
  162. {.num = 1, .den = 4},
  163. {.num = 1, .den = 6},
  164. {.num = 1, .den = 20},
  165. {.num = 1, .den = 8},
  166. {.num = 10, .den = 81},
  167. {.num = 1, .den = 10}
  168. };
  169. static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
  170. {
  171. return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
  172. }
  173. static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
  174. {
  175. return regmap_write(vadc->regmap, vadc->base + offset, data);
  176. }
  177. static int vadc_reset(struct vadc_priv *vadc)
  178. {
  179. u8 data;
  180. int ret;
  181. ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
  182. if (ret)
  183. return ret;
  184. ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
  185. if (ret)
  186. return ret;
  187. ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
  188. if (ret)
  189. return ret;
  190. data |= VADC_FOLLOW_WARM_RB;
  191. return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
  192. }
  193. static int vadc_set_state(struct vadc_priv *vadc, bool state)
  194. {
  195. return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
  196. }
  197. static void vadc_show_status(struct vadc_priv *vadc)
  198. {
  199. u8 mode, sta1, chan, dig, en, req;
  200. int ret;
  201. ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
  202. if (ret)
  203. return;
  204. ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
  205. if (ret)
  206. return;
  207. ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
  208. if (ret)
  209. return;
  210. ret = vadc_read(vadc, VADC_CONV_REQ, &req);
  211. if (ret)
  212. return;
  213. ret = vadc_read(vadc, VADC_STATUS1, &sta1);
  214. if (ret)
  215. return;
  216. ret = vadc_read(vadc, VADC_EN_CTL1, &en);
  217. if (ret)
  218. return;
  219. dev_err(vadc->dev,
  220. "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
  221. mode, en, chan, dig, req, sta1);
  222. }
  223. static int vadc_configure(struct vadc_priv *vadc,
  224. struct vadc_channel_prop *prop)
  225. {
  226. u8 decimation, mode_ctrl;
  227. int ret;
  228. /* Mode selection */
  229. mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) |
  230. VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN;
  231. ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl);
  232. if (ret)
  233. return ret;
  234. /* Channel selection */
  235. ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
  236. if (ret)
  237. return ret;
  238. /* Digital parameter setup */
  239. decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT;
  240. ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation);
  241. if (ret)
  242. return ret;
  243. /* HW settle time delay */
  244. ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
  245. if (ret)
  246. return ret;
  247. ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
  248. if (ret)
  249. return ret;
  250. if (prop->avg_samples)
  251. ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET);
  252. else
  253. ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0);
  254. return ret;
  255. }
  256. static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
  257. {
  258. unsigned int count, retry;
  259. u8 sta1;
  260. int ret;
  261. retry = interval_us / VADC_CONV_TIME_MIN_US;
  262. for (count = 0; count < retry; count++) {
  263. ret = vadc_read(vadc, VADC_STATUS1, &sta1);
  264. if (ret)
  265. return ret;
  266. sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
  267. if (sta1 == VADC_STATUS1_EOC)
  268. return 0;
  269. usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
  270. }
  271. vadc_show_status(vadc);
  272. return -ETIMEDOUT;
  273. }
  274. static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
  275. {
  276. int ret;
  277. ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
  278. if (ret)
  279. return ret;
  280. *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
  281. return 0;
  282. }
  283. static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
  284. unsigned int num)
  285. {
  286. unsigned int i;
  287. for (i = 0; i < vadc->nchannels; i++)
  288. if (vadc->chan_props[i].channel == num)
  289. return &vadc->chan_props[i];
  290. dev_dbg(vadc->dev, "no such channel %02x\n", num);
  291. return NULL;
  292. }
  293. static int vadc_do_conversion(struct vadc_priv *vadc,
  294. struct vadc_channel_prop *prop, u16 *data)
  295. {
  296. unsigned int timeout;
  297. int ret;
  298. mutex_lock(&vadc->lock);
  299. ret = vadc_configure(vadc, prop);
  300. if (ret)
  301. goto unlock;
  302. if (!vadc->poll_eoc)
  303. reinit_completion(&vadc->complete);
  304. ret = vadc_set_state(vadc, true);
  305. if (ret)
  306. goto unlock;
  307. ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
  308. if (ret)
  309. goto err_disable;
  310. timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
  311. if (vadc->poll_eoc) {
  312. ret = vadc_poll_wait_eoc(vadc, timeout);
  313. } else {
  314. ret = wait_for_completion_timeout(&vadc->complete, timeout);
  315. if (!ret) {
  316. ret = -ETIMEDOUT;
  317. goto err_disable;
  318. }
  319. /* Double check conversion status */
  320. ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
  321. if (ret)
  322. goto err_disable;
  323. }
  324. ret = vadc_read_result(vadc, data);
  325. err_disable:
  326. vadc_set_state(vadc, false);
  327. if (ret)
  328. dev_err(vadc->dev, "conversion failed\n");
  329. unlock:
  330. mutex_unlock(&vadc->lock);
  331. return ret;
  332. }
  333. static int vadc_measure_ref_points(struct vadc_priv *vadc)
  334. {
  335. struct vadc_channel_prop *prop;
  336. u16 read_1, read_2;
  337. int ret;
  338. vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE_UV;
  339. vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
  340. prop = vadc_get_channel(vadc, VADC_REF_1250MV);
  341. ret = vadc_do_conversion(vadc, prop, &read_1);
  342. if (ret)
  343. goto err;
  344. /* Try with buffered 625mV channel first */
  345. prop = vadc_get_channel(vadc, VADC_SPARE1);
  346. if (!prop)
  347. prop = vadc_get_channel(vadc, VADC_REF_625MV);
  348. ret = vadc_do_conversion(vadc, prop, &read_2);
  349. if (ret)
  350. goto err;
  351. if (read_1 == read_2) {
  352. ret = -EINVAL;
  353. goto err;
  354. }
  355. vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
  356. vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
  357. /* Ratiometric calibration */
  358. prop = vadc_get_channel(vadc, VADC_VDD_VADC);
  359. ret = vadc_do_conversion(vadc, prop, &read_1);
  360. if (ret)
  361. goto err;
  362. prop = vadc_get_channel(vadc, VADC_GND_REF);
  363. ret = vadc_do_conversion(vadc, prop, &read_2);
  364. if (ret)
  365. goto err;
  366. if (read_1 == read_2) {
  367. ret = -EINVAL;
  368. goto err;
  369. }
  370. vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2;
  371. vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2;
  372. err:
  373. if (ret)
  374. dev_err(vadc->dev, "measure reference points failed\n");
  375. return ret;
  376. }
  377. static s32 vadc_calibrate(struct vadc_priv *vadc,
  378. const struct vadc_channel_prop *prop, u16 adc_code)
  379. {
  380. const struct vadc_prescale_ratio *prescale;
  381. s64 voltage;
  382. voltage = adc_code - vadc->graph[prop->calibration].gnd;
  383. voltage *= vadc->graph[prop->calibration].dx;
  384. voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy);
  385. if (prop->calibration == VADC_CALIB_ABSOLUTE)
  386. voltage += vadc->graph[prop->calibration].dx;
  387. if (voltage < 0)
  388. voltage = 0;
  389. prescale = &vadc_prescale_ratios[prop->prescale];
  390. voltage = voltage * prescale->den;
  391. return div64_s64(voltage, prescale->num);
  392. }
  393. static int vadc_decimation_from_dt(u32 value)
  394. {
  395. if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN ||
  396. value > VADC_DECIMATION_MAX)
  397. return -EINVAL;
  398. return __ffs64(value / VADC_DECIMATION_MIN);
  399. }
  400. static int vadc_prescaling_from_dt(u32 num, u32 den)
  401. {
  402. unsigned int pre;
  403. for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
  404. if (vadc_prescale_ratios[pre].num == num &&
  405. vadc_prescale_ratios[pre].den == den)
  406. break;
  407. if (pre == ARRAY_SIZE(vadc_prescale_ratios))
  408. return -EINVAL;
  409. return pre;
  410. }
  411. static int vadc_hw_settle_time_from_dt(u32 value)
  412. {
  413. if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
  414. return -EINVAL;
  415. if (value <= 1000)
  416. value /= 100;
  417. else
  418. value = value / 2000 + 10;
  419. return value;
  420. }
  421. static int vadc_avg_samples_from_dt(u32 value)
  422. {
  423. if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
  424. return -EINVAL;
  425. return __ffs64(value);
  426. }
  427. static int vadc_read_raw(struct iio_dev *indio_dev,
  428. struct iio_chan_spec const *chan, int *val, int *val2,
  429. long mask)
  430. {
  431. struct vadc_priv *vadc = iio_priv(indio_dev);
  432. struct vadc_channel_prop *prop;
  433. u16 adc_code;
  434. int ret;
  435. switch (mask) {
  436. case IIO_CHAN_INFO_PROCESSED:
  437. prop = &vadc->chan_props[chan->address];
  438. ret = vadc_do_conversion(vadc, prop, &adc_code);
  439. if (ret)
  440. break;
  441. *val = vadc_calibrate(vadc, prop, adc_code);
  442. /* 2mV/K, return milli Celsius */
  443. *val /= 2;
  444. *val -= KELVINMIL_CELSIUSMIL;
  445. return IIO_VAL_INT;
  446. case IIO_CHAN_INFO_RAW:
  447. prop = &vadc->chan_props[chan->address];
  448. ret = vadc_do_conversion(vadc, prop, &adc_code);
  449. if (ret)
  450. break;
  451. *val = vadc_calibrate(vadc, prop, adc_code);
  452. return IIO_VAL_INT;
  453. case IIO_CHAN_INFO_SCALE:
  454. *val = 0;
  455. *val2 = 1000;
  456. return IIO_VAL_INT_PLUS_MICRO;
  457. default:
  458. ret = -EINVAL;
  459. break;
  460. }
  461. return ret;
  462. }
  463. static int vadc_of_xlate(struct iio_dev *indio_dev,
  464. const struct of_phandle_args *iiospec)
  465. {
  466. struct vadc_priv *vadc = iio_priv(indio_dev);
  467. unsigned int i;
  468. for (i = 0; i < vadc->nchannels; i++)
  469. if (vadc->iio_chans[i].channel == iiospec->args[0])
  470. return i;
  471. return -EINVAL;
  472. }
  473. static const struct iio_info vadc_info = {
  474. .read_raw = vadc_read_raw,
  475. .of_xlate = vadc_of_xlate,
  476. .driver_module = THIS_MODULE,
  477. };
  478. struct vadc_channels {
  479. const char *datasheet_name;
  480. unsigned int prescale_index;
  481. enum iio_chan_type type;
  482. long info_mask;
  483. };
  484. #define VADC_CHAN(_dname, _type, _mask, _pre) \
  485. [VADC_##_dname] = { \
  486. .datasheet_name = __stringify(_dname), \
  487. .prescale_index = _pre, \
  488. .type = _type, \
  489. .info_mask = _mask \
  490. }, \
  491. #define VADC_CHAN_TEMP(_dname, _pre) \
  492. VADC_CHAN(_dname, IIO_TEMP, BIT(IIO_CHAN_INFO_PROCESSED), _pre) \
  493. #define VADC_CHAN_VOLT(_dname, _pre) \
  494. VADC_CHAN(_dname, IIO_VOLTAGE, \
  495. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
  496. _pre) \
  497. /*
  498. * The array represents all possible ADC channels found in the supported PMICs.
  499. * Every index in the array is equal to the channel number per datasheet. The
  500. * gaps in the array should be treated as reserved channels.
  501. */
  502. static const struct vadc_channels vadc_chans[] = {
  503. VADC_CHAN_VOLT(USBIN, 4)
  504. VADC_CHAN_VOLT(DCIN, 4)
  505. VADC_CHAN_VOLT(VCHG_SNS, 3)
  506. VADC_CHAN_VOLT(SPARE1_03, 1)
  507. VADC_CHAN_VOLT(USB_ID_MV, 1)
  508. VADC_CHAN_VOLT(VCOIN, 1)
  509. VADC_CHAN_VOLT(VBAT_SNS, 1)
  510. VADC_CHAN_VOLT(VSYS, 1)
  511. VADC_CHAN_TEMP(DIE_TEMP, 0)
  512. VADC_CHAN_VOLT(REF_625MV, 0)
  513. VADC_CHAN_VOLT(REF_1250MV, 0)
  514. VADC_CHAN_VOLT(CHG_TEMP, 0)
  515. VADC_CHAN_VOLT(SPARE1, 0)
  516. VADC_CHAN_VOLT(SPARE2, 0)
  517. VADC_CHAN_VOLT(GND_REF, 0)
  518. VADC_CHAN_VOLT(VDD_VADC, 0)
  519. VADC_CHAN_VOLT(P_MUX1_1_1, 0)
  520. VADC_CHAN_VOLT(P_MUX2_1_1, 0)
  521. VADC_CHAN_VOLT(P_MUX3_1_1, 0)
  522. VADC_CHAN_VOLT(P_MUX4_1_1, 0)
  523. VADC_CHAN_VOLT(P_MUX5_1_1, 0)
  524. VADC_CHAN_VOLT(P_MUX6_1_1, 0)
  525. VADC_CHAN_VOLT(P_MUX7_1_1, 0)
  526. VADC_CHAN_VOLT(P_MUX8_1_1, 0)
  527. VADC_CHAN_VOLT(P_MUX9_1_1, 0)
  528. VADC_CHAN_VOLT(P_MUX10_1_1, 0)
  529. VADC_CHAN_VOLT(P_MUX11_1_1, 0)
  530. VADC_CHAN_VOLT(P_MUX12_1_1, 0)
  531. VADC_CHAN_VOLT(P_MUX13_1_1, 0)
  532. VADC_CHAN_VOLT(P_MUX14_1_1, 0)
  533. VADC_CHAN_VOLT(P_MUX15_1_1, 0)
  534. VADC_CHAN_VOLT(P_MUX16_1_1, 0)
  535. VADC_CHAN_VOLT(P_MUX1_1_3, 1)
  536. VADC_CHAN_VOLT(P_MUX2_1_3, 1)
  537. VADC_CHAN_VOLT(P_MUX3_1_3, 1)
  538. VADC_CHAN_VOLT(P_MUX4_1_3, 1)
  539. VADC_CHAN_VOLT(P_MUX5_1_3, 1)
  540. VADC_CHAN_VOLT(P_MUX6_1_3, 1)
  541. VADC_CHAN_VOLT(P_MUX7_1_3, 1)
  542. VADC_CHAN_VOLT(P_MUX8_1_3, 1)
  543. VADC_CHAN_VOLT(P_MUX9_1_3, 1)
  544. VADC_CHAN_VOLT(P_MUX10_1_3, 1)
  545. VADC_CHAN_VOLT(P_MUX11_1_3, 1)
  546. VADC_CHAN_VOLT(P_MUX12_1_3, 1)
  547. VADC_CHAN_VOLT(P_MUX13_1_3, 1)
  548. VADC_CHAN_VOLT(P_MUX14_1_3, 1)
  549. VADC_CHAN_VOLT(P_MUX15_1_3, 1)
  550. VADC_CHAN_VOLT(P_MUX16_1_3, 1)
  551. VADC_CHAN_VOLT(LR_MUX1_BAT_THERM, 0)
  552. VADC_CHAN_VOLT(LR_MUX2_BAT_ID, 0)
  553. VADC_CHAN_VOLT(LR_MUX3_XO_THERM, 0)
  554. VADC_CHAN_VOLT(LR_MUX4_AMUX_THM1, 0)
  555. VADC_CHAN_VOLT(LR_MUX5_AMUX_THM2, 0)
  556. VADC_CHAN_VOLT(LR_MUX6_AMUX_THM3, 0)
  557. VADC_CHAN_VOLT(LR_MUX7_HW_ID, 0)
  558. VADC_CHAN_VOLT(LR_MUX8_AMUX_THM4, 0)
  559. VADC_CHAN_VOLT(LR_MUX9_AMUX_THM5, 0)
  560. VADC_CHAN_VOLT(LR_MUX10_USB_ID, 0)
  561. VADC_CHAN_VOLT(AMUX_PU1, 0)
  562. VADC_CHAN_VOLT(AMUX_PU2, 0)
  563. VADC_CHAN_VOLT(LR_MUX3_BUF_XO_THERM, 0)
  564. VADC_CHAN_VOLT(LR_MUX1_PU1_BAT_THERM, 0)
  565. VADC_CHAN_VOLT(LR_MUX2_PU1_BAT_ID, 0)
  566. VADC_CHAN_VOLT(LR_MUX3_PU1_XO_THERM, 0)
  567. VADC_CHAN_VOLT(LR_MUX4_PU1_AMUX_THM1, 0)
  568. VADC_CHAN_VOLT(LR_MUX5_PU1_AMUX_THM2, 0)
  569. VADC_CHAN_VOLT(LR_MUX6_PU1_AMUX_THM3, 0)
  570. VADC_CHAN_VOLT(LR_MUX7_PU1_AMUX_HW_ID, 0)
  571. VADC_CHAN_VOLT(LR_MUX8_PU1_AMUX_THM4, 0)
  572. VADC_CHAN_VOLT(LR_MUX9_PU1_AMUX_THM5, 0)
  573. VADC_CHAN_VOLT(LR_MUX10_PU1_AMUX_USB_ID, 0)
  574. VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_XO_THERM, 0)
  575. VADC_CHAN_VOLT(LR_MUX1_PU2_BAT_THERM, 0)
  576. VADC_CHAN_VOLT(LR_MUX2_PU2_BAT_ID, 0)
  577. VADC_CHAN_VOLT(LR_MUX3_PU2_XO_THERM, 0)
  578. VADC_CHAN_VOLT(LR_MUX4_PU2_AMUX_THM1, 0)
  579. VADC_CHAN_VOLT(LR_MUX5_PU2_AMUX_THM2, 0)
  580. VADC_CHAN_VOLT(LR_MUX6_PU2_AMUX_THM3, 0)
  581. VADC_CHAN_VOLT(LR_MUX7_PU2_AMUX_HW_ID, 0)
  582. VADC_CHAN_VOLT(LR_MUX8_PU2_AMUX_THM4, 0)
  583. VADC_CHAN_VOLT(LR_MUX9_PU2_AMUX_THM5, 0)
  584. VADC_CHAN_VOLT(LR_MUX10_PU2_AMUX_USB_ID, 0)
  585. VADC_CHAN_VOLT(LR_MUX3_BUF_PU2_XO_THERM, 0)
  586. VADC_CHAN_VOLT(LR_MUX1_PU1_PU2_BAT_THERM, 0)
  587. VADC_CHAN_VOLT(LR_MUX2_PU1_PU2_BAT_ID, 0)
  588. VADC_CHAN_VOLT(LR_MUX3_PU1_PU2_XO_THERM, 0)
  589. VADC_CHAN_VOLT(LR_MUX4_PU1_PU2_AMUX_THM1, 0)
  590. VADC_CHAN_VOLT(LR_MUX5_PU1_PU2_AMUX_THM2, 0)
  591. VADC_CHAN_VOLT(LR_MUX6_PU1_PU2_AMUX_THM3, 0)
  592. VADC_CHAN_VOLT(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0)
  593. VADC_CHAN_VOLT(LR_MUX8_PU1_PU2_AMUX_THM4, 0)
  594. VADC_CHAN_VOLT(LR_MUX9_PU1_PU2_AMUX_THM5, 0)
  595. VADC_CHAN_VOLT(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0)
  596. VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0)
  597. };
  598. static int vadc_get_dt_channel_data(struct device *dev,
  599. struct vadc_channel_prop *prop,
  600. struct device_node *node)
  601. {
  602. const char *name = node->name;
  603. u32 chan, value, varr[2];
  604. int ret;
  605. ret = of_property_read_u32(node, "reg", &chan);
  606. if (ret) {
  607. dev_err(dev, "invalid channel number %s\n", name);
  608. return ret;
  609. }
  610. if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) {
  611. dev_err(dev, "%s invalid channel number %d\n", name, chan);
  612. return -EINVAL;
  613. }
  614. /* the channel has DT description */
  615. prop->channel = chan;
  616. ret = of_property_read_u32(node, "qcom,decimation", &value);
  617. if (!ret) {
  618. ret = vadc_decimation_from_dt(value);
  619. if (ret < 0) {
  620. dev_err(dev, "%02x invalid decimation %d\n",
  621. chan, value);
  622. return ret;
  623. }
  624. prop->decimation = ret;
  625. } else {
  626. prop->decimation = VADC_DEF_DECIMATION;
  627. }
  628. ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
  629. if (!ret) {
  630. ret = vadc_prescaling_from_dt(varr[0], varr[1]);
  631. if (ret < 0) {
  632. dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
  633. chan, varr[0], varr[1]);
  634. return ret;
  635. }
  636. prop->prescale = ret;
  637. } else {
  638. prop->prescale = vadc_chans[prop->channel].prescale_index;
  639. }
  640. ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
  641. if (!ret) {
  642. ret = vadc_hw_settle_time_from_dt(value);
  643. if (ret < 0) {
  644. dev_err(dev, "%02x invalid hw-settle-time %d us\n",
  645. chan, value);
  646. return ret;
  647. }
  648. prop->hw_settle_time = ret;
  649. } else {
  650. prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
  651. }
  652. ret = of_property_read_u32(node, "qcom,avg-samples", &value);
  653. if (!ret) {
  654. ret = vadc_avg_samples_from_dt(value);
  655. if (ret < 0) {
  656. dev_err(dev, "%02x invalid avg-samples %d\n",
  657. chan, value);
  658. return ret;
  659. }
  660. prop->avg_samples = ret;
  661. } else {
  662. prop->avg_samples = VADC_DEF_AVG_SAMPLES;
  663. }
  664. if (of_property_read_bool(node, "qcom,ratiometric"))
  665. prop->calibration = VADC_CALIB_RATIOMETRIC;
  666. else
  667. prop->calibration = VADC_CALIB_ABSOLUTE;
  668. dev_dbg(dev, "%02x name %s\n", chan, name);
  669. return 0;
  670. }
  671. static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node)
  672. {
  673. const struct vadc_channels *vadc_chan;
  674. struct iio_chan_spec *iio_chan;
  675. struct vadc_channel_prop prop;
  676. struct device_node *child;
  677. unsigned int index = 0;
  678. int ret;
  679. vadc->nchannels = of_get_available_child_count(node);
  680. if (!vadc->nchannels)
  681. return -EINVAL;
  682. vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels,
  683. sizeof(*vadc->iio_chans), GFP_KERNEL);
  684. if (!vadc->iio_chans)
  685. return -ENOMEM;
  686. vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels,
  687. sizeof(*vadc->chan_props), GFP_KERNEL);
  688. if (!vadc->chan_props)
  689. return -ENOMEM;
  690. iio_chan = vadc->iio_chans;
  691. for_each_available_child_of_node(node, child) {
  692. ret = vadc_get_dt_channel_data(vadc->dev, &prop, child);
  693. if (ret) {
  694. of_node_put(child);
  695. return ret;
  696. }
  697. vadc->chan_props[index] = prop;
  698. vadc_chan = &vadc_chans[prop.channel];
  699. iio_chan->channel = prop.channel;
  700. iio_chan->datasheet_name = vadc_chan->datasheet_name;
  701. iio_chan->info_mask_separate = vadc_chan->info_mask;
  702. iio_chan->type = vadc_chan->type;
  703. iio_chan->indexed = 1;
  704. iio_chan->address = index++;
  705. iio_chan++;
  706. }
  707. /* These channels are mandatory, they are used as reference points */
  708. if (!vadc_get_channel(vadc, VADC_REF_1250MV)) {
  709. dev_err(vadc->dev, "Please define 1.25V channel\n");
  710. return -ENODEV;
  711. }
  712. if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
  713. dev_err(vadc->dev, "Please define 0.625V channel\n");
  714. return -ENODEV;
  715. }
  716. if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
  717. dev_err(vadc->dev, "Please define VDD channel\n");
  718. return -ENODEV;
  719. }
  720. if (!vadc_get_channel(vadc, VADC_GND_REF)) {
  721. dev_err(vadc->dev, "Please define GND channel\n");
  722. return -ENODEV;
  723. }
  724. return 0;
  725. }
  726. static irqreturn_t vadc_isr(int irq, void *dev_id)
  727. {
  728. struct vadc_priv *vadc = dev_id;
  729. complete(&vadc->complete);
  730. return IRQ_HANDLED;
  731. }
  732. static int vadc_check_revision(struct vadc_priv *vadc)
  733. {
  734. u8 val;
  735. int ret;
  736. ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
  737. if (ret)
  738. return ret;
  739. if (val < VADC_PERPH_TYPE_ADC) {
  740. dev_err(vadc->dev, "%d is not ADC\n", val);
  741. return -ENODEV;
  742. }
  743. ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
  744. if (ret)
  745. return ret;
  746. if (val < VADC_PERPH_SUBTYPE_VADC) {
  747. dev_err(vadc->dev, "%d is not VADC\n", val);
  748. return -ENODEV;
  749. }
  750. ret = vadc_read(vadc, VADC_REVISION2, &val);
  751. if (ret)
  752. return ret;
  753. if (val < VADC_REVISION2_SUPPORTED_VADC) {
  754. dev_err(vadc->dev, "revision %d not supported\n", val);
  755. return -ENODEV;
  756. }
  757. return 0;
  758. }
  759. static int vadc_probe(struct platform_device *pdev)
  760. {
  761. struct device_node *node = pdev->dev.of_node;
  762. struct device *dev = &pdev->dev;
  763. struct iio_dev *indio_dev;
  764. struct vadc_priv *vadc;
  765. struct regmap *regmap;
  766. int ret, irq_eoc;
  767. u32 reg;
  768. regmap = dev_get_regmap(dev->parent, NULL);
  769. if (!regmap)
  770. return -ENODEV;
  771. ret = of_property_read_u32(node, "reg", &reg);
  772. if (ret < 0)
  773. return ret;
  774. indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
  775. if (!indio_dev)
  776. return -ENOMEM;
  777. vadc = iio_priv(indio_dev);
  778. vadc->regmap = regmap;
  779. vadc->dev = dev;
  780. vadc->base = reg;
  781. vadc->are_ref_measured = false;
  782. init_completion(&vadc->complete);
  783. mutex_init(&vadc->lock);
  784. ret = vadc_check_revision(vadc);
  785. if (ret)
  786. return ret;
  787. ret = vadc_get_dt_data(vadc, node);
  788. if (ret)
  789. return ret;
  790. irq_eoc = platform_get_irq(pdev, 0);
  791. if (irq_eoc < 0) {
  792. if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
  793. return irq_eoc;
  794. vadc->poll_eoc = true;
  795. } else {
  796. ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0,
  797. "spmi-vadc", vadc);
  798. if (ret)
  799. return ret;
  800. }
  801. ret = vadc_reset(vadc);
  802. if (ret) {
  803. dev_err(dev, "reset failed\n");
  804. return ret;
  805. }
  806. ret = vadc_measure_ref_points(vadc);
  807. if (ret)
  808. return ret;
  809. indio_dev->dev.parent = dev;
  810. indio_dev->dev.of_node = node;
  811. indio_dev->name = pdev->name;
  812. indio_dev->modes = INDIO_DIRECT_MODE;
  813. indio_dev->info = &vadc_info;
  814. indio_dev->channels = vadc->iio_chans;
  815. indio_dev->num_channels = vadc->nchannels;
  816. return devm_iio_device_register(dev, indio_dev);
  817. }
  818. static const struct of_device_id vadc_match_table[] = {
  819. { .compatible = "qcom,spmi-vadc" },
  820. { }
  821. };
  822. MODULE_DEVICE_TABLE(of, vadc_match_table);
  823. static struct platform_driver vadc_driver = {
  824. .driver = {
  825. .name = "qcom-spmi-vadc",
  826. .of_match_table = vadc_match_table,
  827. },
  828. .probe = vadc_probe,
  829. };
  830. module_platform_driver(vadc_driver);
  831. MODULE_ALIAS("platform:qcom-spmi-vadc");
  832. MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
  833. MODULE_LICENSE("GPL v2");
  834. MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
  835. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");