qcom-spmi-adc5.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/completion.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/iio/adc/qcom-vadc-common.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/log2.h>
  14. #include <linux/math64.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/property.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include <dt-bindings/iio/qcom,spmi-vadc.h>
  22. #define ADC5_USR_REVISION1 0x0
  23. #define ADC5_USR_STATUS1 0x8
  24. #define ADC5_USR_STATUS1_CONV_FAULT BIT(7)
  25. #define ADC5_USR_STATUS1_REQ_STS BIT(1)
  26. #define ADC5_USR_STATUS1_EOC BIT(0)
  27. #define ADC5_USR_STATUS1_REQ_STS_EOC_MASK 0x3
  28. #define ADC5_USR_STATUS2 0x9
  29. #define ADC5_USR_STATUS2_CONV_SEQ_MASK 0x70
  30. #define ADC5_USR_STATUS2_CONV_SEQ_MASK_SHIFT 0x5
  31. #define ADC5_USR_IBAT_MEAS 0xf
  32. #define ADC5_USR_IBAT_MEAS_SUPPORTED BIT(0)
  33. #define ADC5_USR_DIG_PARAM 0x42
  34. #define ADC5_USR_DIG_PARAM_CAL_VAL BIT(6)
  35. #define ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT 6
  36. #define ADC5_USR_DIG_PARAM_CAL_SEL 0x30
  37. #define ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT 4
  38. #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL 0xc
  39. #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT 2
  40. #define ADC5_USR_FAST_AVG_CTL 0x43
  41. #define ADC5_USR_FAST_AVG_CTL_EN BIT(7)
  42. #define ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK 0x7
  43. #define ADC5_USR_CH_SEL_CTL 0x44
  44. #define ADC5_USR_DELAY_CTL 0x45
  45. #define ADC5_USR_HW_SETTLE_DELAY_MASK 0xf
  46. #define ADC5_USR_EN_CTL1 0x46
  47. #define ADC5_USR_EN_CTL1_ADC_EN BIT(7)
  48. #define ADC5_USR_CONV_REQ 0x47
  49. #define ADC5_USR_CONV_REQ_REQ BIT(7)
  50. #define ADC5_USR_DATA0 0x50
  51. #define ADC5_USR_DATA1 0x51
  52. #define ADC5_USR_IBAT_DATA0 0x52
  53. #define ADC5_USR_IBAT_DATA1 0x53
  54. #define ADC_CHANNEL_OFFSET 0x8
  55. #define ADC_CHANNEL_MASK GENMASK(7, 0)
  56. /*
  57. * Conversion time varies based on the decimation, clock rate, fast average
  58. * samples and measurements queued across different VADC peripherals.
  59. * Set the timeout to a max of 100ms.
  60. */
  61. #define ADC5_CONV_TIME_MIN_US 263
  62. #define ADC5_CONV_TIME_MAX_US 264
  63. #define ADC5_CONV_TIME_RETRY 400
  64. #define ADC5_CONV_TIMEOUT msecs_to_jiffies(100)
  65. /* Digital version >= 5.3 supports hw_settle_2 */
  66. #define ADC5_HW_SETTLE_DIFF_MINOR 3
  67. #define ADC5_HW_SETTLE_DIFF_MAJOR 5
  68. /* For PMIC7 */
  69. #define ADC_APP_SID 0x40
  70. #define ADC_APP_SID_MASK GENMASK(3, 0)
  71. #define ADC7_CONV_TIMEOUT msecs_to_jiffies(10)
  72. enum adc5_cal_method {
  73. ADC5_NO_CAL = 0,
  74. ADC5_RATIOMETRIC_CAL,
  75. ADC5_ABSOLUTE_CAL
  76. };
  77. enum adc5_cal_val {
  78. ADC5_TIMER_CAL = 0,
  79. ADC5_NEW_CAL
  80. };
  81. /**
  82. * struct adc5_channel_prop - ADC channel property.
  83. * @channel: channel number, refer to the channel list.
  84. * @cal_method: calibration method.
  85. * @cal_val: calibration value
  86. * @decimation: sampling rate supported for the channel.
  87. * @sid: slave id of PMIC owning the channel, for PMIC7.
  88. * @prescale: channel scaling performed on the input signal.
  89. * @hw_settle_time: the time between AMUX being configured and the
  90. * start of conversion.
  91. * @avg_samples: ability to provide single result from the ADC
  92. * that is an average of multiple measurements.
  93. * @scale_fn_type: Represents the scaling function to convert voltage
  94. * physical units desired by the client for the channel.
  95. * @channel_name: Channel name used in device tree.
  96. */
  97. struct adc5_channel_prop {
  98. unsigned int channel;
  99. enum adc5_cal_method cal_method;
  100. enum adc5_cal_val cal_val;
  101. unsigned int decimation;
  102. unsigned int sid;
  103. unsigned int prescale;
  104. unsigned int hw_settle_time;
  105. unsigned int avg_samples;
  106. enum vadc_scale_fn_type scale_fn_type;
  107. const char *channel_name;
  108. };
  109. /**
  110. * struct adc5_chip - ADC private structure.
  111. * @regmap: SPMI ADC5 peripheral register map field.
  112. * @dev: SPMI ADC5 device.
  113. * @base: base address for the ADC peripheral.
  114. * @nchannels: number of ADC channels.
  115. * @chan_props: array of ADC channel properties.
  116. * @iio_chans: array of IIO channels specification.
  117. * @poll_eoc: use polling instead of interrupt.
  118. * @complete: ADC result notification after interrupt is received.
  119. * @lock: ADC lock for access to the peripheral.
  120. * @data: software configuration data.
  121. */
  122. struct adc5_chip {
  123. struct regmap *regmap;
  124. struct device *dev;
  125. u16 base;
  126. unsigned int nchannels;
  127. struct adc5_channel_prop *chan_props;
  128. struct iio_chan_spec *iio_chans;
  129. bool poll_eoc;
  130. struct completion complete;
  131. struct mutex lock;
  132. const struct adc5_data *data;
  133. };
  134. static int adc5_read(struct adc5_chip *adc, u16 offset, u8 *data, int len)
  135. {
  136. return regmap_bulk_read(adc->regmap, adc->base + offset, data, len);
  137. }
  138. static int adc5_write(struct adc5_chip *adc, u16 offset, u8 *data, int len)
  139. {
  140. return regmap_bulk_write(adc->regmap, adc->base + offset, data, len);
  141. }
  142. static int adc5_masked_write(struct adc5_chip *adc, u16 offset, u8 mask, u8 val)
  143. {
  144. return regmap_update_bits(adc->regmap, adc->base + offset, mask, val);
  145. }
  146. static int adc5_read_voltage_data(struct adc5_chip *adc, u16 *data)
  147. {
  148. int ret;
  149. u8 rslt_lsb, rslt_msb;
  150. ret = adc5_read(adc, ADC5_USR_DATA0, &rslt_lsb, sizeof(rslt_lsb));
  151. if (ret)
  152. return ret;
  153. ret = adc5_read(adc, ADC5_USR_DATA1, &rslt_msb, sizeof(rslt_lsb));
  154. if (ret)
  155. return ret;
  156. *data = (rslt_msb << 8) | rslt_lsb;
  157. if (*data == ADC5_USR_DATA_CHECK) {
  158. dev_err(adc->dev, "Invalid data:0x%x\n", *data);
  159. return -EINVAL;
  160. }
  161. dev_dbg(adc->dev, "voltage raw code:0x%x\n", *data);
  162. return 0;
  163. }
  164. static int adc5_poll_wait_eoc(struct adc5_chip *adc)
  165. {
  166. unsigned int count, retry = ADC5_CONV_TIME_RETRY;
  167. u8 status1;
  168. int ret;
  169. for (count = 0; count < retry; count++) {
  170. ret = adc5_read(adc, ADC5_USR_STATUS1, &status1,
  171. sizeof(status1));
  172. if (ret)
  173. return ret;
  174. status1 &= ADC5_USR_STATUS1_REQ_STS_EOC_MASK;
  175. if (status1 == ADC5_USR_STATUS1_EOC)
  176. return 0;
  177. usleep_range(ADC5_CONV_TIME_MIN_US, ADC5_CONV_TIME_MAX_US);
  178. }
  179. return -ETIMEDOUT;
  180. }
  181. static void adc5_update_dig_param(struct adc5_chip *adc,
  182. struct adc5_channel_prop *prop, u8 *data)
  183. {
  184. /* Update calibration value */
  185. *data &= ~ADC5_USR_DIG_PARAM_CAL_VAL;
  186. *data |= (prop->cal_val << ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT);
  187. /* Update calibration select */
  188. *data &= ~ADC5_USR_DIG_PARAM_CAL_SEL;
  189. *data |= (prop->cal_method << ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT);
  190. /* Update decimation ratio select */
  191. *data &= ~ADC5_USR_DIG_PARAM_DEC_RATIO_SEL;
  192. *data |= (prop->decimation << ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT);
  193. }
  194. static int adc5_configure(struct adc5_chip *adc,
  195. struct adc5_channel_prop *prop)
  196. {
  197. int ret;
  198. u8 buf[6];
  199. /* Read registers 0x42 through 0x46 */
  200. ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
  201. if (ret)
  202. return ret;
  203. /* Digital param selection */
  204. adc5_update_dig_param(adc, prop, &buf[0]);
  205. /* Update fast average sample value */
  206. buf[1] &= (u8) ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK;
  207. buf[1] |= prop->avg_samples;
  208. /* Select ADC channel */
  209. buf[2] = prop->channel;
  210. /* Select HW settle delay for channel */
  211. buf[3] &= (u8) ~ADC5_USR_HW_SETTLE_DELAY_MASK;
  212. buf[3] |= prop->hw_settle_time;
  213. /* Select ADC enable */
  214. buf[4] |= ADC5_USR_EN_CTL1_ADC_EN;
  215. /* Select CONV request */
  216. buf[5] |= ADC5_USR_CONV_REQ_REQ;
  217. if (!adc->poll_eoc)
  218. reinit_completion(&adc->complete);
  219. return adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
  220. }
  221. static int adc7_configure(struct adc5_chip *adc,
  222. struct adc5_channel_prop *prop)
  223. {
  224. int ret;
  225. u8 conv_req = 0, buf[4];
  226. ret = adc5_masked_write(adc, ADC_APP_SID, ADC_APP_SID_MASK, prop->sid);
  227. if (ret)
  228. return ret;
  229. ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
  230. if (ret)
  231. return ret;
  232. /* Digital param selection */
  233. adc5_update_dig_param(adc, prop, &buf[0]);
  234. /* Update fast average sample value */
  235. buf[1] &= ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK;
  236. buf[1] |= prop->avg_samples;
  237. /* Select ADC channel */
  238. buf[2] = prop->channel;
  239. /* Select HW settle delay for channel */
  240. buf[3] &= ~ADC5_USR_HW_SETTLE_DELAY_MASK;
  241. buf[3] |= prop->hw_settle_time;
  242. /* Select CONV request */
  243. conv_req = ADC5_USR_CONV_REQ_REQ;
  244. if (!adc->poll_eoc)
  245. reinit_completion(&adc->complete);
  246. ret = adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
  247. if (ret)
  248. return ret;
  249. return adc5_write(adc, ADC5_USR_CONV_REQ, &conv_req, 1);
  250. }
  251. static int adc5_do_conversion(struct adc5_chip *adc,
  252. struct adc5_channel_prop *prop,
  253. struct iio_chan_spec const *chan,
  254. u16 *data_volt, u16 *data_cur)
  255. {
  256. int ret;
  257. mutex_lock(&adc->lock);
  258. ret = adc5_configure(adc, prop);
  259. if (ret) {
  260. dev_err(adc->dev, "ADC configure failed with %d\n", ret);
  261. goto unlock;
  262. }
  263. if (adc->poll_eoc) {
  264. ret = adc5_poll_wait_eoc(adc);
  265. if (ret) {
  266. dev_err(adc->dev, "EOC bit not set\n");
  267. goto unlock;
  268. }
  269. } else {
  270. ret = wait_for_completion_timeout(&adc->complete,
  271. ADC5_CONV_TIMEOUT);
  272. if (!ret) {
  273. dev_dbg(adc->dev, "Did not get completion timeout.\n");
  274. ret = adc5_poll_wait_eoc(adc);
  275. if (ret) {
  276. dev_err(adc->dev, "EOC bit not set\n");
  277. goto unlock;
  278. }
  279. }
  280. }
  281. ret = adc5_read_voltage_data(adc, data_volt);
  282. unlock:
  283. mutex_unlock(&adc->lock);
  284. return ret;
  285. }
  286. static int adc7_do_conversion(struct adc5_chip *adc,
  287. struct adc5_channel_prop *prop,
  288. struct iio_chan_spec const *chan,
  289. u16 *data_volt, u16 *data_cur)
  290. {
  291. int ret;
  292. u8 status;
  293. mutex_lock(&adc->lock);
  294. ret = adc7_configure(adc, prop);
  295. if (ret) {
  296. dev_err(adc->dev, "ADC configure failed with %d\n", ret);
  297. goto unlock;
  298. }
  299. /* No support for polling mode at present */
  300. wait_for_completion_timeout(&adc->complete, ADC7_CONV_TIMEOUT);
  301. ret = adc5_read(adc, ADC5_USR_STATUS1, &status, 1);
  302. if (ret)
  303. goto unlock;
  304. if (status & ADC5_USR_STATUS1_CONV_FAULT) {
  305. dev_err(adc->dev, "Unexpected conversion fault\n");
  306. ret = -EIO;
  307. goto unlock;
  308. }
  309. ret = adc5_read_voltage_data(adc, data_volt);
  310. unlock:
  311. mutex_unlock(&adc->lock);
  312. return ret;
  313. }
  314. typedef int (*adc_do_conversion)(struct adc5_chip *adc,
  315. struct adc5_channel_prop *prop,
  316. struct iio_chan_spec const *chan,
  317. u16 *data_volt, u16 *data_cur);
  318. static irqreturn_t adc5_isr(int irq, void *dev_id)
  319. {
  320. struct adc5_chip *adc = dev_id;
  321. complete(&adc->complete);
  322. return IRQ_HANDLED;
  323. }
  324. static int adc5_fwnode_xlate(struct iio_dev *indio_dev,
  325. const struct fwnode_reference_args *iiospec)
  326. {
  327. struct adc5_chip *adc = iio_priv(indio_dev);
  328. int i;
  329. for (i = 0; i < adc->nchannels; i++)
  330. if (adc->chan_props[i].channel == iiospec->args[0])
  331. return i;
  332. return -EINVAL;
  333. }
  334. static int adc7_fwnode_xlate(struct iio_dev *indio_dev,
  335. const struct fwnode_reference_args *iiospec)
  336. {
  337. struct adc5_chip *adc = iio_priv(indio_dev);
  338. int i, v_channel;
  339. for (i = 0; i < adc->nchannels; i++) {
  340. v_channel = (adc->chan_props[i].sid << ADC_CHANNEL_OFFSET) |
  341. adc->chan_props[i].channel;
  342. if (v_channel == iiospec->args[0])
  343. return i;
  344. }
  345. return -EINVAL;
  346. }
  347. static int adc_read_raw_common(struct iio_dev *indio_dev,
  348. struct iio_chan_spec const *chan, int *val, int *val2,
  349. long mask, adc_do_conversion do_conv)
  350. {
  351. struct adc5_chip *adc = iio_priv(indio_dev);
  352. struct adc5_channel_prop *prop;
  353. u16 adc_code_volt, adc_code_cur;
  354. int ret;
  355. prop = &adc->chan_props[chan->address];
  356. switch (mask) {
  357. case IIO_CHAN_INFO_PROCESSED:
  358. ret = do_conv(adc, prop, chan,
  359. &adc_code_volt, &adc_code_cur);
  360. if (ret)
  361. return ret;
  362. ret = qcom_adc5_hw_scale(prop->scale_fn_type,
  363. prop->prescale,
  364. adc->data,
  365. adc_code_volt, val);
  366. if (ret)
  367. return ret;
  368. return IIO_VAL_INT;
  369. default:
  370. return -EINVAL;
  371. }
  372. }
  373. static int adc5_read_raw(struct iio_dev *indio_dev,
  374. struct iio_chan_spec const *chan, int *val, int *val2,
  375. long mask)
  376. {
  377. return adc_read_raw_common(indio_dev, chan, val, val2,
  378. mask, adc5_do_conversion);
  379. }
  380. static int adc7_read_raw(struct iio_dev *indio_dev,
  381. struct iio_chan_spec const *chan, int *val, int *val2,
  382. long mask)
  383. {
  384. return adc_read_raw_common(indio_dev, chan, val, val2,
  385. mask, adc7_do_conversion);
  386. }
  387. static const struct iio_info adc5_info = {
  388. .read_raw = adc5_read_raw,
  389. .fwnode_xlate = adc5_fwnode_xlate,
  390. };
  391. static const struct iio_info adc7_info = {
  392. .read_raw = adc7_read_raw,
  393. .fwnode_xlate = adc7_fwnode_xlate,
  394. };
  395. struct adc5_channels {
  396. const char *datasheet_name;
  397. unsigned int prescale_index;
  398. enum iio_chan_type type;
  399. long info_mask;
  400. enum vadc_scale_fn_type scale_fn_type;
  401. };
  402. /* In these definitions, _pre refers to an index into adc5_prescale_ratios. */
  403. #define ADC5_CHAN(_dname, _type, _mask, _pre, _scale) \
  404. { \
  405. .datasheet_name = _dname, \
  406. .prescale_index = _pre, \
  407. .type = _type, \
  408. .info_mask = _mask, \
  409. .scale_fn_type = _scale, \
  410. }, \
  411. #define ADC5_CHAN_TEMP(_dname, _pre, _scale) \
  412. ADC5_CHAN(_dname, IIO_TEMP, \
  413. BIT(IIO_CHAN_INFO_PROCESSED), \
  414. _pre, _scale) \
  415. #define ADC5_CHAN_VOLT(_dname, _pre, _scale) \
  416. ADC5_CHAN(_dname, IIO_VOLTAGE, \
  417. BIT(IIO_CHAN_INFO_PROCESSED), \
  418. _pre, _scale) \
  419. static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
  420. [ADC5_REF_GND] = ADC5_CHAN_VOLT("ref_gnd", 0,
  421. SCALE_HW_CALIB_DEFAULT)
  422. [ADC5_1P25VREF] = ADC5_CHAN_VOLT("vref_1p25", 0,
  423. SCALE_HW_CALIB_DEFAULT)
  424. [ADC5_VPH_PWR] = ADC5_CHAN_VOLT("vph_pwr", 1,
  425. SCALE_HW_CALIB_DEFAULT)
  426. [ADC5_VBAT_SNS] = ADC5_CHAN_VOLT("vbat_sns", 1,
  427. SCALE_HW_CALIB_DEFAULT)
  428. [ADC5_VCOIN] = ADC5_CHAN_VOLT("vcoin", 1,
  429. SCALE_HW_CALIB_DEFAULT)
  430. [ADC5_DIE_TEMP] = ADC5_CHAN_TEMP("die_temp", 0,
  431. SCALE_HW_CALIB_PMIC_THERM)
  432. [ADC5_USB_IN_I] = ADC5_CHAN_VOLT("usb_in_i_uv", 0,
  433. SCALE_HW_CALIB_DEFAULT)
  434. [ADC5_USB_IN_V_16] = ADC5_CHAN_VOLT("usb_in_v_div_16", 8,
  435. SCALE_HW_CALIB_DEFAULT)
  436. [ADC5_CHG_TEMP] = ADC5_CHAN_TEMP("chg_temp", 0,
  437. SCALE_HW_CALIB_PM5_CHG_TEMP)
  438. /* Charger prescales SBUx and MID_CHG to fit within 1.8V upper unit */
  439. [ADC5_SBUx] = ADC5_CHAN_VOLT("chg_sbux", 1,
  440. SCALE_HW_CALIB_DEFAULT)
  441. [ADC5_MID_CHG_DIV6] = ADC5_CHAN_VOLT("chg_mid_chg", 3,
  442. SCALE_HW_CALIB_DEFAULT)
  443. [ADC5_XO_THERM_100K_PU] = ADC5_CHAN_TEMP("xo_therm", 0,
  444. SCALE_HW_CALIB_XOTHERM)
  445. [ADC5_BAT_ID_100K_PU] = ADC5_CHAN_TEMP("bat_id", 0,
  446. SCALE_HW_CALIB_DEFAULT)
  447. [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
  448. SCALE_HW_CALIB_THERM_100K_PULLUP)
  449. [ADC5_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
  450. SCALE_HW_CALIB_THERM_100K_PULLUP)
  451. [ADC5_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
  452. SCALE_HW_CALIB_THERM_100K_PULLUP)
  453. [ADC5_AMUX_THM2] = ADC5_CHAN_TEMP("amux_thm2", 0,
  454. SCALE_HW_CALIB_PM5_SMB_TEMP)
  455. [ADC5_GPIO1_100K_PU] = ADC5_CHAN_TEMP("gpio1_100k_pu", 0,
  456. SCALE_HW_CALIB_THERM_100K_PULLUP)
  457. [ADC5_GPIO2_100K_PU] = ADC5_CHAN_TEMP("gpio2_100k_pu", 0,
  458. SCALE_HW_CALIB_THERM_100K_PULLUP)
  459. [ADC5_GPIO3_100K_PU] = ADC5_CHAN_TEMP("gpio3_100k_pu", 0,
  460. SCALE_HW_CALIB_THERM_100K_PULLUP)
  461. [ADC5_GPIO4_100K_PU] = ADC5_CHAN_TEMP("gpio4_100k_pu", 0,
  462. SCALE_HW_CALIB_THERM_100K_PULLUP)
  463. };
  464. static const struct adc5_channels adc7_chans_pmic[ADC5_MAX_CHANNEL] = {
  465. [ADC7_REF_GND] = ADC5_CHAN_VOLT("ref_gnd", 0,
  466. SCALE_HW_CALIB_DEFAULT)
  467. [ADC7_1P25VREF] = ADC5_CHAN_VOLT("vref_1p25", 0,
  468. SCALE_HW_CALIB_DEFAULT)
  469. [ADC7_VPH_PWR] = ADC5_CHAN_VOLT("vph_pwr", 1,
  470. SCALE_HW_CALIB_DEFAULT)
  471. [ADC7_VBAT_SNS] = ADC5_CHAN_VOLT("vbat_sns", 3,
  472. SCALE_HW_CALIB_DEFAULT)
  473. [ADC7_DIE_TEMP] = ADC5_CHAN_TEMP("die_temp", 0,
  474. SCALE_HW_CALIB_PMIC_THERM_PM7)
  475. [ADC7_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_pu2", 0,
  476. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  477. [ADC7_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_pu2", 0,
  478. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  479. [ADC7_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_pu2", 0,
  480. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  481. [ADC7_AMUX_THM4_100K_PU] = ADC5_CHAN_TEMP("amux_thm4_pu2", 0,
  482. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  483. [ADC7_AMUX_THM5_100K_PU] = ADC5_CHAN_TEMP("amux_thm5_pu2", 0,
  484. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  485. [ADC7_AMUX_THM6_100K_PU] = ADC5_CHAN_TEMP("amux_thm6_pu2", 0,
  486. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  487. [ADC7_GPIO1_100K_PU] = ADC5_CHAN_TEMP("gpio1_pu2", 0,
  488. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  489. [ADC7_GPIO2_100K_PU] = ADC5_CHAN_TEMP("gpio2_pu2", 0,
  490. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  491. [ADC7_GPIO3_100K_PU] = ADC5_CHAN_TEMP("gpio3_pu2", 0,
  492. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  493. [ADC7_GPIO4_100K_PU] = ADC5_CHAN_TEMP("gpio4_pu2", 0,
  494. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  495. };
  496. static const struct adc5_channels adc5_chans_rev2[ADC5_MAX_CHANNEL] = {
  497. [ADC5_REF_GND] = ADC5_CHAN_VOLT("ref_gnd", 0,
  498. SCALE_HW_CALIB_DEFAULT)
  499. [ADC5_1P25VREF] = ADC5_CHAN_VOLT("vref_1p25", 0,
  500. SCALE_HW_CALIB_DEFAULT)
  501. [ADC5_VREF_VADC] = ADC5_CHAN_VOLT("vref_vadc", 0,
  502. SCALE_HW_CALIB_DEFAULT)
  503. [ADC5_VPH_PWR] = ADC5_CHAN_VOLT("vph_pwr", 1,
  504. SCALE_HW_CALIB_DEFAULT)
  505. [ADC5_VBAT_SNS] = ADC5_CHAN_VOLT("vbat_sns", 1,
  506. SCALE_HW_CALIB_DEFAULT)
  507. [ADC5_VCOIN] = ADC5_CHAN_VOLT("vcoin", 1,
  508. SCALE_HW_CALIB_DEFAULT)
  509. [ADC5_DIE_TEMP] = ADC5_CHAN_TEMP("die_temp", 0,
  510. SCALE_HW_CALIB_PMIC_THERM)
  511. [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
  512. SCALE_HW_CALIB_THERM_100K_PULLUP)
  513. [ADC5_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
  514. SCALE_HW_CALIB_THERM_100K_PULLUP)
  515. [ADC5_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
  516. SCALE_HW_CALIB_THERM_100K_PULLUP)
  517. [ADC5_AMUX_THM4_100K_PU] = ADC5_CHAN_TEMP("amux_thm4_100k_pu", 0,
  518. SCALE_HW_CALIB_THERM_100K_PULLUP)
  519. [ADC5_AMUX_THM5_100K_PU] = ADC5_CHAN_TEMP("amux_thm5_100k_pu", 0,
  520. SCALE_HW_CALIB_THERM_100K_PULLUP)
  521. [ADC5_XO_THERM_100K_PU] = ADC5_CHAN_TEMP("xo_therm_100k_pu", 0,
  522. SCALE_HW_CALIB_THERM_100K_PULLUP)
  523. };
  524. static int adc5_get_fw_channel_data(struct adc5_chip *adc,
  525. struct adc5_channel_prop *prop,
  526. struct fwnode_handle *fwnode,
  527. const struct adc5_data *data)
  528. {
  529. const char *channel_name;
  530. char *name;
  531. u32 chan, value, varr[2];
  532. u32 sid = 0;
  533. int ret;
  534. struct device *dev = adc->dev;
  535. name = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", fwnode);
  536. if (!name)
  537. return -ENOMEM;
  538. /* Cut the address part */
  539. name[strchrnul(name, '@') - name] = '\0';
  540. ret = fwnode_property_read_u32(fwnode, "reg", &chan);
  541. if (ret) {
  542. dev_err(dev, "invalid channel number %s\n", name);
  543. return ret;
  544. }
  545. /* Value read from "reg" is virtual channel number */
  546. /* virtual channel number = sid << 8 | channel number */
  547. if (adc->data->info == &adc7_info) {
  548. sid = chan >> ADC_CHANNEL_OFFSET;
  549. chan = chan & ADC_CHANNEL_MASK;
  550. }
  551. if (chan > ADC5_PARALLEL_ISENSE_VBAT_IDATA) {
  552. dev_err(dev, "%s invalid channel number %d\n", name, chan);
  553. return -EINVAL;
  554. }
  555. /* the channel has DT description */
  556. prop->channel = chan;
  557. prop->sid = sid;
  558. ret = fwnode_property_read_string(fwnode, "label", &channel_name);
  559. if (ret)
  560. channel_name = data->adc_chans[chan].datasheet_name;
  561. prop->channel_name = channel_name;
  562. ret = fwnode_property_read_u32(fwnode, "qcom,decimation", &value);
  563. if (!ret) {
  564. ret = qcom_adc5_decimation_from_dt(value, data->decimation);
  565. if (ret < 0) {
  566. dev_err(dev, "%02x invalid decimation %d\n",
  567. chan, value);
  568. return ret;
  569. }
  570. prop->decimation = ret;
  571. } else {
  572. prop->decimation = ADC5_DECIMATION_DEFAULT;
  573. }
  574. ret = fwnode_property_read_u32_array(fwnode, "qcom,pre-scaling", varr, 2);
  575. if (!ret) {
  576. ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
  577. if (ret < 0) {
  578. dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
  579. chan, varr[0], varr[1]);
  580. return ret;
  581. }
  582. prop->prescale = ret;
  583. } else {
  584. prop->prescale =
  585. adc->data->adc_chans[prop->channel].prescale_index;
  586. }
  587. ret = fwnode_property_read_u32(fwnode, "qcom,hw-settle-time", &value);
  588. if (!ret) {
  589. u8 dig_version[2];
  590. ret = adc5_read(adc, ADC5_USR_REVISION1, dig_version,
  591. sizeof(dig_version));
  592. if (ret) {
  593. dev_err(dev, "Invalid dig version read %d\n", ret);
  594. return ret;
  595. }
  596. dev_dbg(dev, "dig_ver:minor:%d, major:%d\n", dig_version[0],
  597. dig_version[1]);
  598. /* Digital controller >= 5.3 have hw_settle_2 option */
  599. if ((dig_version[0] >= ADC5_HW_SETTLE_DIFF_MINOR &&
  600. dig_version[1] >= ADC5_HW_SETTLE_DIFF_MAJOR) ||
  601. adc->data->info == &adc7_info)
  602. ret = qcom_adc5_hw_settle_time_from_dt(value, data->hw_settle_2);
  603. else
  604. ret = qcom_adc5_hw_settle_time_from_dt(value, data->hw_settle_1);
  605. if (ret < 0) {
  606. dev_err(dev, "%02x invalid hw-settle-time %d us\n",
  607. chan, value);
  608. return ret;
  609. }
  610. prop->hw_settle_time = ret;
  611. } else {
  612. prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
  613. }
  614. ret = fwnode_property_read_u32(fwnode, "qcom,avg-samples", &value);
  615. if (!ret) {
  616. ret = qcom_adc5_avg_samples_from_dt(value);
  617. if (ret < 0) {
  618. dev_err(dev, "%02x invalid avg-samples %d\n",
  619. chan, value);
  620. return ret;
  621. }
  622. prop->avg_samples = ret;
  623. } else {
  624. prop->avg_samples = VADC_DEF_AVG_SAMPLES;
  625. }
  626. if (fwnode_property_read_bool(fwnode, "qcom,ratiometric"))
  627. prop->cal_method = ADC5_RATIOMETRIC_CAL;
  628. else
  629. prop->cal_method = ADC5_ABSOLUTE_CAL;
  630. /*
  631. * Default to using timer calibration. Using a fresh calibration value
  632. * for every conversion will increase the overall time for a request.
  633. */
  634. prop->cal_val = ADC5_TIMER_CAL;
  635. dev_dbg(dev, "%02x name %s\n", chan, name);
  636. return 0;
  637. }
  638. static const struct adc5_data adc5_data_pmic = {
  639. .full_scale_code_volt = 0x70e4,
  640. .full_scale_code_cur = 0x2710,
  641. .adc_chans = adc5_chans_pmic,
  642. .info = &adc5_info,
  643. .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
  644. {250, 420, 840},
  645. .hw_settle_1 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  646. {15, 100, 200, 300, 400, 500, 600, 700,
  647. 800, 900, 1, 2, 4, 6, 8, 10},
  648. .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  649. {15, 100, 200, 300, 400, 500, 600, 700,
  650. 1, 2, 4, 8, 16, 32, 64, 128},
  651. };
  652. static const struct adc5_data adc7_data_pmic = {
  653. .full_scale_code_volt = 0x70e4,
  654. .adc_chans = adc7_chans_pmic,
  655. .info = &adc7_info,
  656. .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
  657. {85, 340, 1360},
  658. .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  659. {15, 100, 200, 300, 400, 500, 600, 700,
  660. 1000, 2000, 4000, 8000, 16000, 32000,
  661. 64000, 128000},
  662. };
  663. static const struct adc5_data adc5_data_pmic_rev2 = {
  664. .full_scale_code_volt = 0x4000,
  665. .full_scale_code_cur = 0x1800,
  666. .adc_chans = adc5_chans_rev2,
  667. .info = &adc5_info,
  668. .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
  669. {256, 512, 1024},
  670. .hw_settle_1 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  671. {0, 100, 200, 300, 400, 500, 600, 700,
  672. 800, 900, 1, 2, 4, 6, 8, 10},
  673. .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  674. {15, 100, 200, 300, 400, 500, 600, 700,
  675. 1, 2, 4, 8, 16, 32, 64, 128},
  676. };
  677. static const struct of_device_id adc5_match_table[] = {
  678. {
  679. .compatible = "qcom,spmi-adc5",
  680. .data = &adc5_data_pmic,
  681. },
  682. {
  683. .compatible = "qcom,spmi-adc7",
  684. .data = &adc7_data_pmic,
  685. },
  686. {
  687. .compatible = "qcom,spmi-adc-rev2",
  688. .data = &adc5_data_pmic_rev2,
  689. },
  690. { }
  691. };
  692. MODULE_DEVICE_TABLE(of, adc5_match_table);
  693. static int adc5_get_fw_data(struct adc5_chip *adc)
  694. {
  695. const struct adc5_channels *adc_chan;
  696. struct iio_chan_spec *iio_chan;
  697. struct adc5_channel_prop prop, *chan_props;
  698. unsigned int index = 0;
  699. int ret;
  700. adc->nchannels = device_get_child_node_count(adc->dev);
  701. if (!adc->nchannels)
  702. return -EINVAL;
  703. adc->iio_chans = devm_kcalloc(adc->dev, adc->nchannels,
  704. sizeof(*adc->iio_chans), GFP_KERNEL);
  705. if (!adc->iio_chans)
  706. return -ENOMEM;
  707. adc->chan_props = devm_kcalloc(adc->dev, adc->nchannels,
  708. sizeof(*adc->chan_props), GFP_KERNEL);
  709. if (!adc->chan_props)
  710. return -ENOMEM;
  711. chan_props = adc->chan_props;
  712. iio_chan = adc->iio_chans;
  713. adc->data = device_get_match_data(adc->dev);
  714. if (!adc->data)
  715. adc->data = &adc5_data_pmic;
  716. device_for_each_child_node_scoped(adc->dev, child) {
  717. ret = adc5_get_fw_channel_data(adc, &prop, child, adc->data);
  718. if (ret)
  719. return ret;
  720. prop.scale_fn_type =
  721. adc->data->adc_chans[prop.channel].scale_fn_type;
  722. *chan_props = prop;
  723. adc_chan = &adc->data->adc_chans[prop.channel];
  724. iio_chan->channel = prop.channel;
  725. iio_chan->datasheet_name = adc_chan->datasheet_name;
  726. iio_chan->extend_name = prop.channel_name;
  727. iio_chan->info_mask_separate = adc_chan->info_mask;
  728. iio_chan->type = adc_chan->type;
  729. iio_chan->address = index;
  730. iio_chan++;
  731. chan_props++;
  732. index++;
  733. }
  734. return 0;
  735. }
  736. static int adc5_probe(struct platform_device *pdev)
  737. {
  738. struct device *dev = &pdev->dev;
  739. struct iio_dev *indio_dev;
  740. struct adc5_chip *adc;
  741. struct regmap *regmap;
  742. int ret, irq_eoc;
  743. u32 reg;
  744. regmap = dev_get_regmap(dev->parent, NULL);
  745. if (!regmap)
  746. return -ENODEV;
  747. ret = device_property_read_u32(dev, "reg", &reg);
  748. if (ret < 0)
  749. return ret;
  750. indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
  751. if (!indio_dev)
  752. return -ENOMEM;
  753. adc = iio_priv(indio_dev);
  754. adc->regmap = regmap;
  755. adc->dev = dev;
  756. adc->base = reg;
  757. init_completion(&adc->complete);
  758. mutex_init(&adc->lock);
  759. ret = adc5_get_fw_data(adc);
  760. if (ret)
  761. return dev_err_probe(dev, ret, "adc get dt data failed\n");
  762. irq_eoc = platform_get_irq(pdev, 0);
  763. if (irq_eoc < 0) {
  764. if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
  765. return irq_eoc;
  766. adc->poll_eoc = true;
  767. } else {
  768. ret = devm_request_irq(dev, irq_eoc, adc5_isr, 0,
  769. "pm-adc5", adc);
  770. if (ret)
  771. return ret;
  772. }
  773. indio_dev->name = pdev->name;
  774. indio_dev->modes = INDIO_DIRECT_MODE;
  775. indio_dev->info = adc->data->info;
  776. indio_dev->channels = adc->iio_chans;
  777. indio_dev->num_channels = adc->nchannels;
  778. return devm_iio_device_register(dev, indio_dev);
  779. }
  780. static struct platform_driver adc5_driver = {
  781. .driver = {
  782. .name = "qcom-spmi-adc5",
  783. .of_match_table = adc5_match_table,
  784. },
  785. .probe = adc5_probe,
  786. };
  787. module_platform_driver(adc5_driver);
  788. MODULE_ALIAS("platform:qcom-spmi-adc5");
  789. MODULE_DESCRIPTION("Qualcomm Technologies Inc. PMIC5 ADC driver");
  790. MODULE_LICENSE("GPL v2");