palmas_gpadc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * palmas-adc.c -- TI PALMAS GPADC.
  3. *
  4. * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
  5. *
  6. * Author: Pradeep Goudagunta <pgoudagunta@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/i2c.h>
  20. #include <linux/pm.h>
  21. #include <linux/mfd/palmas.h>
  22. #include <linux/completion.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/machine.h>
  27. #include <linux/iio/driver.h>
  28. #define MOD_NAME "palmas-gpadc"
  29. #define PALMAS_ADC_CONVERSION_TIMEOUT (msecs_to_jiffies(5000))
  30. #define PALMAS_TO_BE_CALCULATED 0
  31. #define PALMAS_GPADC_TRIMINVALID -1
  32. struct palmas_gpadc_info {
  33. /* calibration codes and regs */
  34. int x1; /* lower ideal code */
  35. int x2; /* higher ideal code */
  36. int v1; /* expected lower volt reading */
  37. int v2; /* expected higher volt reading */
  38. u8 trim1_reg; /* register number for lower trim */
  39. u8 trim2_reg; /* register number for upper trim */
  40. int gain; /* calculated from above (after reading trim regs) */
  41. int offset; /* calculated from above (after reading trim regs) */
  42. int gain_error; /* calculated from above (after reading trim regs) */
  43. bool is_uncalibrated; /* if channel has calibration data */
  44. };
  45. #define PALMAS_ADC_INFO(_chan, _x1, _x2, _v1, _v2, _t1, _t2, _is_uncalibrated) \
  46. [PALMAS_ADC_CH_##_chan] = { \
  47. .x1 = _x1, \
  48. .x2 = _x2, \
  49. .v1 = _v1, \
  50. .v2 = _v2, \
  51. .gain = PALMAS_TO_BE_CALCULATED, \
  52. .offset = PALMAS_TO_BE_CALCULATED, \
  53. .gain_error = PALMAS_TO_BE_CALCULATED, \
  54. .trim1_reg = PALMAS_GPADC_TRIM##_t1, \
  55. .trim2_reg = PALMAS_GPADC_TRIM##_t2, \
  56. .is_uncalibrated = _is_uncalibrated \
  57. }
  58. static struct palmas_gpadc_info palmas_gpadc_info[] = {
  59. PALMAS_ADC_INFO(IN0, 2064, 3112, 630, 950, 1, 2, false),
  60. PALMAS_ADC_INFO(IN1, 2064, 3112, 630, 950, 1, 2, false),
  61. PALMAS_ADC_INFO(IN2, 2064, 3112, 1260, 1900, 3, 4, false),
  62. PALMAS_ADC_INFO(IN3, 2064, 3112, 630, 950, 1, 2, false),
  63. PALMAS_ADC_INFO(IN4, 2064, 3112, 630, 950, 1, 2, false),
  64. PALMAS_ADC_INFO(IN5, 2064, 3112, 630, 950, 1, 2, false),
  65. PALMAS_ADC_INFO(IN6, 2064, 3112, 2520, 3800, 5, 6, false),
  66. PALMAS_ADC_INFO(IN7, 2064, 3112, 2520, 3800, 7, 8, false),
  67. PALMAS_ADC_INFO(IN8, 2064, 3112, 3150, 4750, 9, 10, false),
  68. PALMAS_ADC_INFO(IN9, 2064, 3112, 5670, 8550, 11, 12, false),
  69. PALMAS_ADC_INFO(IN10, 2064, 3112, 3465, 5225, 13, 14, false),
  70. PALMAS_ADC_INFO(IN11, 0, 0, 0, 0, INVALID, INVALID, true),
  71. PALMAS_ADC_INFO(IN12, 0, 0, 0, 0, INVALID, INVALID, true),
  72. PALMAS_ADC_INFO(IN13, 0, 0, 0, 0, INVALID, INVALID, true),
  73. PALMAS_ADC_INFO(IN14, 2064, 3112, 3645, 5225, 15, 16, false),
  74. PALMAS_ADC_INFO(IN15, 0, 0, 0, 0, INVALID, INVALID, true),
  75. };
  76. /**
  77. * struct palmas_gpadc - the palmas_gpadc structure
  78. * @ch0_current: channel 0 current source setting
  79. * 0: 0 uA
  80. * 1: 5 uA
  81. * 2: 15 uA
  82. * 3: 20 uA
  83. * @ch3_current: channel 0 current source setting
  84. * 0: 0 uA
  85. * 1: 10 uA
  86. * 2: 400 uA
  87. * 3: 800 uA
  88. * @extended_delay: enable the gpadc extended delay mode
  89. * @auto_conversion_period: define the auto_conversion_period
  90. *
  91. * This is the palmas_gpadc structure to store run-time information
  92. * and pointers for this driver instance.
  93. */
  94. struct palmas_gpadc {
  95. struct device *dev;
  96. struct palmas *palmas;
  97. u8 ch0_current;
  98. u8 ch3_current;
  99. bool extended_delay;
  100. int irq;
  101. int irq_auto_0;
  102. int irq_auto_1;
  103. struct palmas_gpadc_info *adc_info;
  104. struct completion conv_completion;
  105. struct palmas_adc_wakeup_property wakeup1_data;
  106. struct palmas_adc_wakeup_property wakeup2_data;
  107. bool wakeup1_enable;
  108. bool wakeup2_enable;
  109. int auto_conversion_period;
  110. };
  111. /*
  112. * GPADC lock issue in AUTO mode.
  113. * Impact: In AUTO mode, GPADC conversion can be locked after disabling AUTO
  114. * mode feature.
  115. * Details:
  116. * When the AUTO mode is the only conversion mode enabled, if the AUTO
  117. * mode feature is disabled with bit GPADC_AUTO_CTRL. AUTO_CONV1_EN = 0
  118. * or bit GPADC_AUTO_CTRL. AUTO_CONV0_EN = 0 during a conversion, the
  119. * conversion mechanism can be seen as locked meaning that all following
  120. * conversion will give 0 as a result. Bit GPADC_STATUS.GPADC_AVAILABLE
  121. * will stay at 0 meaning that GPADC is busy. An RT conversion can unlock
  122. * the GPADC.
  123. *
  124. * Workaround(s):
  125. * To avoid the lock mechanism, the workaround to follow before any stop
  126. * conversion request is:
  127. * Force the GPADC state machine to be ON by using the GPADC_CTRL1.
  128. * GPADC_FORCE bit = 1
  129. * Shutdown the GPADC AUTO conversion using
  130. * GPADC_AUTO_CTRL.SHUTDOWN_CONV[01] = 0.
  131. * After 100us, force the GPADC state machine to be OFF by using the
  132. * GPADC_CTRL1. GPADC_FORCE bit = 0
  133. */
  134. static int palmas_disable_auto_conversion(struct palmas_gpadc *adc)
  135. {
  136. int ret;
  137. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  138. PALMAS_GPADC_CTRL1,
  139. PALMAS_GPADC_CTRL1_GPADC_FORCE,
  140. PALMAS_GPADC_CTRL1_GPADC_FORCE);
  141. if (ret < 0) {
  142. dev_err(adc->dev, "GPADC_CTRL1 update failed: %d\n", ret);
  143. return ret;
  144. }
  145. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  146. PALMAS_GPADC_AUTO_CTRL,
  147. PALMAS_GPADC_AUTO_CTRL_SHUTDOWN_CONV1 |
  148. PALMAS_GPADC_AUTO_CTRL_SHUTDOWN_CONV0,
  149. 0);
  150. if (ret < 0) {
  151. dev_err(adc->dev, "AUTO_CTRL update failed: %d\n", ret);
  152. return ret;
  153. }
  154. udelay(100);
  155. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  156. PALMAS_GPADC_CTRL1,
  157. PALMAS_GPADC_CTRL1_GPADC_FORCE, 0);
  158. if (ret < 0)
  159. dev_err(adc->dev, "GPADC_CTRL1 update failed: %d\n", ret);
  160. return ret;
  161. }
  162. static irqreturn_t palmas_gpadc_irq(int irq, void *data)
  163. {
  164. struct palmas_gpadc *adc = data;
  165. complete(&adc->conv_completion);
  166. return IRQ_HANDLED;
  167. }
  168. static irqreturn_t palmas_gpadc_irq_auto(int irq, void *data)
  169. {
  170. struct palmas_gpadc *adc = data;
  171. dev_dbg(adc->dev, "Threshold interrupt %d occurs\n", irq);
  172. palmas_disable_auto_conversion(adc);
  173. return IRQ_HANDLED;
  174. }
  175. static int palmas_gpadc_start_mask_interrupt(struct palmas_gpadc *adc,
  176. bool mask)
  177. {
  178. int ret;
  179. if (!mask)
  180. ret = palmas_update_bits(adc->palmas, PALMAS_INTERRUPT_BASE,
  181. PALMAS_INT3_MASK,
  182. PALMAS_INT3_MASK_GPADC_EOC_SW, 0);
  183. else
  184. ret = palmas_update_bits(adc->palmas, PALMAS_INTERRUPT_BASE,
  185. PALMAS_INT3_MASK,
  186. PALMAS_INT3_MASK_GPADC_EOC_SW,
  187. PALMAS_INT3_MASK_GPADC_EOC_SW);
  188. if (ret < 0)
  189. dev_err(adc->dev, "GPADC INT MASK update failed: %d\n", ret);
  190. return ret;
  191. }
  192. static int palmas_gpadc_enable(struct palmas_gpadc *adc, int adc_chan,
  193. int enable)
  194. {
  195. unsigned int mask, val;
  196. int ret;
  197. if (enable) {
  198. val = (adc->extended_delay
  199. << PALMAS_GPADC_RT_CTRL_EXTEND_DELAY_SHIFT);
  200. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  201. PALMAS_GPADC_RT_CTRL,
  202. PALMAS_GPADC_RT_CTRL_EXTEND_DELAY, val);
  203. if (ret < 0) {
  204. dev_err(adc->dev, "RT_CTRL update failed: %d\n", ret);
  205. return ret;
  206. }
  207. mask = (PALMAS_GPADC_CTRL1_CURRENT_SRC_CH0_MASK |
  208. PALMAS_GPADC_CTRL1_CURRENT_SRC_CH3_MASK |
  209. PALMAS_GPADC_CTRL1_GPADC_FORCE);
  210. val = (adc->ch0_current
  211. << PALMAS_GPADC_CTRL1_CURRENT_SRC_CH0_SHIFT);
  212. val |= (adc->ch3_current
  213. << PALMAS_GPADC_CTRL1_CURRENT_SRC_CH3_SHIFT);
  214. val |= PALMAS_GPADC_CTRL1_GPADC_FORCE;
  215. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  216. PALMAS_GPADC_CTRL1, mask, val);
  217. if (ret < 0) {
  218. dev_err(adc->dev,
  219. "Failed to update current setting: %d\n", ret);
  220. return ret;
  221. }
  222. mask = (PALMAS_GPADC_SW_SELECT_SW_CONV0_SEL_MASK |
  223. PALMAS_GPADC_SW_SELECT_SW_CONV_EN);
  224. val = (adc_chan | PALMAS_GPADC_SW_SELECT_SW_CONV_EN);
  225. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  226. PALMAS_GPADC_SW_SELECT, mask, val);
  227. if (ret < 0) {
  228. dev_err(adc->dev, "SW_SELECT update failed: %d\n", ret);
  229. return ret;
  230. }
  231. } else {
  232. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  233. PALMAS_GPADC_SW_SELECT, 0);
  234. if (ret < 0)
  235. dev_err(adc->dev, "SW_SELECT write failed: %d\n", ret);
  236. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  237. PALMAS_GPADC_CTRL1,
  238. PALMAS_GPADC_CTRL1_GPADC_FORCE, 0);
  239. if (ret < 0) {
  240. dev_err(adc->dev, "CTRL1 update failed: %d\n", ret);
  241. return ret;
  242. }
  243. }
  244. return ret;
  245. }
  246. static int palmas_gpadc_read_prepare(struct palmas_gpadc *adc, int adc_chan)
  247. {
  248. int ret;
  249. ret = palmas_gpadc_enable(adc, adc_chan, true);
  250. if (ret < 0)
  251. return ret;
  252. return palmas_gpadc_start_mask_interrupt(adc, 0);
  253. }
  254. static void palmas_gpadc_read_done(struct palmas_gpadc *adc, int adc_chan)
  255. {
  256. palmas_gpadc_start_mask_interrupt(adc, 1);
  257. palmas_gpadc_enable(adc, adc_chan, false);
  258. }
  259. static int palmas_gpadc_calibrate(struct palmas_gpadc *adc, int adc_chan)
  260. {
  261. int k;
  262. int d1;
  263. int d2;
  264. int ret;
  265. int gain;
  266. int x1 = adc->adc_info[adc_chan].x1;
  267. int x2 = adc->adc_info[adc_chan].x2;
  268. int v1 = adc->adc_info[adc_chan].v1;
  269. int v2 = adc->adc_info[adc_chan].v2;
  270. ret = palmas_read(adc->palmas, PALMAS_TRIM_GPADC_BASE,
  271. adc->adc_info[adc_chan].trim1_reg, &d1);
  272. if (ret < 0) {
  273. dev_err(adc->dev, "TRIM read failed: %d\n", ret);
  274. goto scrub;
  275. }
  276. ret = palmas_read(adc->palmas, PALMAS_TRIM_GPADC_BASE,
  277. adc->adc_info[adc_chan].trim2_reg, &d2);
  278. if (ret < 0) {
  279. dev_err(adc->dev, "TRIM read failed: %d\n", ret);
  280. goto scrub;
  281. }
  282. /* gain error calculation */
  283. k = (1000 + (1000 * (d2 - d1)) / (x2 - x1));
  284. /* gain calculation */
  285. gain = ((v2 - v1) * 1000) / (x2 - x1);
  286. adc->adc_info[adc_chan].gain_error = k;
  287. adc->adc_info[adc_chan].gain = gain;
  288. /* offset Calculation */
  289. adc->adc_info[adc_chan].offset = (d1 * 1000) - ((k - 1000) * x1);
  290. scrub:
  291. return ret;
  292. }
  293. static int palmas_gpadc_start_conversion(struct palmas_gpadc *adc, int adc_chan)
  294. {
  295. unsigned int val;
  296. int ret;
  297. init_completion(&adc->conv_completion);
  298. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  299. PALMAS_GPADC_SW_SELECT,
  300. PALMAS_GPADC_SW_SELECT_SW_START_CONV0,
  301. PALMAS_GPADC_SW_SELECT_SW_START_CONV0);
  302. if (ret < 0) {
  303. dev_err(adc->dev, "SELECT_SW_START write failed: %d\n", ret);
  304. return ret;
  305. }
  306. ret = wait_for_completion_timeout(&adc->conv_completion,
  307. PALMAS_ADC_CONVERSION_TIMEOUT);
  308. if (ret == 0) {
  309. dev_err(adc->dev, "conversion not completed\n");
  310. return -ETIMEDOUT;
  311. }
  312. ret = palmas_bulk_read(adc->palmas, PALMAS_GPADC_BASE,
  313. PALMAS_GPADC_SW_CONV0_LSB, &val, 2);
  314. if (ret < 0) {
  315. dev_err(adc->dev, "SW_CONV0_LSB read failed: %d\n", ret);
  316. return ret;
  317. }
  318. ret = val & 0xFFF;
  319. return ret;
  320. }
  321. static int palmas_gpadc_get_calibrated_code(struct palmas_gpadc *adc,
  322. int adc_chan, int val)
  323. {
  324. if (!adc->adc_info[adc_chan].is_uncalibrated)
  325. val = (val*1000 - adc->adc_info[adc_chan].offset) /
  326. adc->adc_info[adc_chan].gain_error;
  327. if (val < 0) {
  328. dev_err(adc->dev, "Mismatch with calibration\n");
  329. return 0;
  330. }
  331. val = (val * adc->adc_info[adc_chan].gain) / 1000;
  332. return val;
  333. }
  334. static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
  335. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  336. {
  337. struct palmas_gpadc *adc = iio_priv(indio_dev);
  338. int adc_chan = chan->channel;
  339. int ret = 0;
  340. if (adc_chan > PALMAS_ADC_CH_MAX)
  341. return -EINVAL;
  342. mutex_lock(&indio_dev->mlock);
  343. switch (mask) {
  344. case IIO_CHAN_INFO_RAW:
  345. case IIO_CHAN_INFO_PROCESSED:
  346. ret = palmas_gpadc_read_prepare(adc, adc_chan);
  347. if (ret < 0)
  348. goto out;
  349. ret = palmas_gpadc_start_conversion(adc, adc_chan);
  350. if (ret < 0) {
  351. dev_err(adc->dev,
  352. "ADC start conversion failed\n");
  353. goto out;
  354. }
  355. if (mask == IIO_CHAN_INFO_PROCESSED)
  356. ret = palmas_gpadc_get_calibrated_code(
  357. adc, adc_chan, ret);
  358. *val = ret;
  359. ret = IIO_VAL_INT;
  360. goto out;
  361. }
  362. mutex_unlock(&indio_dev->mlock);
  363. return ret;
  364. out:
  365. palmas_gpadc_read_done(adc, adc_chan);
  366. mutex_unlock(&indio_dev->mlock);
  367. return ret;
  368. }
  369. static const struct iio_info palmas_gpadc_iio_info = {
  370. .read_raw = palmas_gpadc_read_raw,
  371. };
  372. #define PALMAS_ADC_CHAN_IIO(chan, _type, chan_info) \
  373. { \
  374. .datasheet_name = PALMAS_DATASHEET_NAME(chan), \
  375. .type = _type, \
  376. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  377. BIT(chan_info), \
  378. .indexed = 1, \
  379. .channel = PALMAS_ADC_CH_##chan, \
  380. }
  381. static const struct iio_chan_spec palmas_gpadc_iio_channel[] = {
  382. PALMAS_ADC_CHAN_IIO(IN0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  383. PALMAS_ADC_CHAN_IIO(IN1, IIO_TEMP, IIO_CHAN_INFO_RAW),
  384. PALMAS_ADC_CHAN_IIO(IN2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  385. PALMAS_ADC_CHAN_IIO(IN3, IIO_TEMP, IIO_CHAN_INFO_RAW),
  386. PALMAS_ADC_CHAN_IIO(IN4, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  387. PALMAS_ADC_CHAN_IIO(IN5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  388. PALMAS_ADC_CHAN_IIO(IN6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  389. PALMAS_ADC_CHAN_IIO(IN7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  390. PALMAS_ADC_CHAN_IIO(IN8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  391. PALMAS_ADC_CHAN_IIO(IN9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  392. PALMAS_ADC_CHAN_IIO(IN10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  393. PALMAS_ADC_CHAN_IIO(IN11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  394. PALMAS_ADC_CHAN_IIO(IN12, IIO_TEMP, IIO_CHAN_INFO_RAW),
  395. PALMAS_ADC_CHAN_IIO(IN13, IIO_TEMP, IIO_CHAN_INFO_RAW),
  396. PALMAS_ADC_CHAN_IIO(IN14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  397. PALMAS_ADC_CHAN_IIO(IN15, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  398. };
  399. static int palmas_gpadc_get_adc_dt_data(struct platform_device *pdev,
  400. struct palmas_gpadc_platform_data **gpadc_pdata)
  401. {
  402. struct device_node *np = pdev->dev.of_node;
  403. struct palmas_gpadc_platform_data *gp_data;
  404. int ret;
  405. u32 pval;
  406. gp_data = devm_kzalloc(&pdev->dev, sizeof(*gp_data), GFP_KERNEL);
  407. if (!gp_data)
  408. return -ENOMEM;
  409. ret = of_property_read_u32(np, "ti,channel0-current-microamp", &pval);
  410. if (!ret)
  411. gp_data->ch0_current = pval;
  412. ret = of_property_read_u32(np, "ti,channel3-current-microamp", &pval);
  413. if (!ret)
  414. gp_data->ch3_current = pval;
  415. gp_data->extended_delay = of_property_read_bool(np,
  416. "ti,enable-extended-delay");
  417. *gpadc_pdata = gp_data;
  418. return 0;
  419. }
  420. static int palmas_gpadc_probe(struct platform_device *pdev)
  421. {
  422. struct palmas_gpadc *adc;
  423. struct palmas_platform_data *pdata;
  424. struct palmas_gpadc_platform_data *gpadc_pdata = NULL;
  425. struct iio_dev *indio_dev;
  426. int ret, i;
  427. pdata = dev_get_platdata(pdev->dev.parent);
  428. if (pdata && pdata->gpadc_pdata)
  429. gpadc_pdata = pdata->gpadc_pdata;
  430. if (!gpadc_pdata && pdev->dev.of_node) {
  431. ret = palmas_gpadc_get_adc_dt_data(pdev, &gpadc_pdata);
  432. if (ret < 0)
  433. return ret;
  434. }
  435. if (!gpadc_pdata)
  436. return -EINVAL;
  437. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  438. if (!indio_dev) {
  439. dev_err(&pdev->dev, "iio_device_alloc failed\n");
  440. return -ENOMEM;
  441. }
  442. adc = iio_priv(indio_dev);
  443. adc->dev = &pdev->dev;
  444. adc->palmas = dev_get_drvdata(pdev->dev.parent);
  445. adc->adc_info = palmas_gpadc_info;
  446. init_completion(&adc->conv_completion);
  447. dev_set_drvdata(&pdev->dev, indio_dev);
  448. adc->auto_conversion_period = gpadc_pdata->auto_conversion_period_ms;
  449. adc->irq = palmas_irq_get_virq(adc->palmas, PALMAS_GPADC_EOC_SW_IRQ);
  450. if (adc->irq < 0) {
  451. dev_err(adc->dev,
  452. "get virq failed: %d\n", adc->irq);
  453. ret = adc->irq;
  454. goto out;
  455. }
  456. ret = request_threaded_irq(adc->irq, NULL,
  457. palmas_gpadc_irq,
  458. IRQF_ONESHOT, dev_name(adc->dev),
  459. adc);
  460. if (ret < 0) {
  461. dev_err(adc->dev,
  462. "request irq %d failed: %d\n", adc->irq, ret);
  463. goto out;
  464. }
  465. if (gpadc_pdata->adc_wakeup1_data) {
  466. memcpy(&adc->wakeup1_data, gpadc_pdata->adc_wakeup1_data,
  467. sizeof(adc->wakeup1_data));
  468. adc->wakeup1_enable = true;
  469. adc->irq_auto_0 = platform_get_irq(pdev, 1);
  470. ret = request_threaded_irq(adc->irq_auto_0, NULL,
  471. palmas_gpadc_irq_auto,
  472. IRQF_ONESHOT,
  473. "palmas-adc-auto-0", adc);
  474. if (ret < 0) {
  475. dev_err(adc->dev, "request auto0 irq %d failed: %d\n",
  476. adc->irq_auto_0, ret);
  477. goto out_irq_free;
  478. }
  479. }
  480. if (gpadc_pdata->adc_wakeup2_data) {
  481. memcpy(&adc->wakeup2_data, gpadc_pdata->adc_wakeup2_data,
  482. sizeof(adc->wakeup2_data));
  483. adc->wakeup2_enable = true;
  484. adc->irq_auto_1 = platform_get_irq(pdev, 2);
  485. ret = request_threaded_irq(adc->irq_auto_1, NULL,
  486. palmas_gpadc_irq_auto,
  487. IRQF_ONESHOT,
  488. "palmas-adc-auto-1", adc);
  489. if (ret < 0) {
  490. dev_err(adc->dev, "request auto1 irq %d failed: %d\n",
  491. adc->irq_auto_1, ret);
  492. goto out_irq_auto0_free;
  493. }
  494. }
  495. /* set the current source 0 (value 0/5/15/20 uA => 0..3) */
  496. if (gpadc_pdata->ch0_current <= 1)
  497. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_0;
  498. else if (gpadc_pdata->ch0_current <= 5)
  499. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_5;
  500. else if (gpadc_pdata->ch0_current <= 15)
  501. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_15;
  502. else
  503. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_20;
  504. /* set the current source 3 (value 0/10/400/800 uA => 0..3) */
  505. if (gpadc_pdata->ch3_current <= 1)
  506. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_0;
  507. else if (gpadc_pdata->ch3_current <= 10)
  508. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_10;
  509. else if (gpadc_pdata->ch3_current <= 400)
  510. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_400;
  511. else
  512. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_800;
  513. adc->extended_delay = gpadc_pdata->extended_delay;
  514. indio_dev->name = MOD_NAME;
  515. indio_dev->dev.parent = &pdev->dev;
  516. indio_dev->info = &palmas_gpadc_iio_info;
  517. indio_dev->modes = INDIO_DIRECT_MODE;
  518. indio_dev->channels = palmas_gpadc_iio_channel;
  519. indio_dev->num_channels = ARRAY_SIZE(palmas_gpadc_iio_channel);
  520. ret = iio_device_register(indio_dev);
  521. if (ret < 0) {
  522. dev_err(adc->dev, "iio_device_register() failed: %d\n", ret);
  523. goto out_irq_auto1_free;
  524. }
  525. device_set_wakeup_capable(&pdev->dev, 1);
  526. for (i = 0; i < PALMAS_ADC_CH_MAX; i++) {
  527. if (!(adc->adc_info[i].is_uncalibrated))
  528. palmas_gpadc_calibrate(adc, i);
  529. }
  530. if (adc->wakeup1_enable || adc->wakeup2_enable)
  531. device_wakeup_enable(&pdev->dev);
  532. return 0;
  533. out_irq_auto1_free:
  534. if (gpadc_pdata->adc_wakeup2_data)
  535. free_irq(adc->irq_auto_1, adc);
  536. out_irq_auto0_free:
  537. if (gpadc_pdata->adc_wakeup1_data)
  538. free_irq(adc->irq_auto_0, adc);
  539. out_irq_free:
  540. free_irq(adc->irq, adc);
  541. out:
  542. return ret;
  543. }
  544. static int palmas_gpadc_remove(struct platform_device *pdev)
  545. {
  546. struct iio_dev *indio_dev = dev_to_iio_dev(&pdev->dev);
  547. struct palmas_gpadc *adc = iio_priv(indio_dev);
  548. if (adc->wakeup1_enable || adc->wakeup2_enable)
  549. device_wakeup_disable(&pdev->dev);
  550. iio_device_unregister(indio_dev);
  551. free_irq(adc->irq, adc);
  552. if (adc->wakeup1_enable)
  553. free_irq(adc->irq_auto_0, adc);
  554. if (adc->wakeup2_enable)
  555. free_irq(adc->irq_auto_1, adc);
  556. return 0;
  557. }
  558. #ifdef CONFIG_PM_SLEEP
  559. static int palmas_adc_wakeup_configure(struct palmas_gpadc *adc)
  560. {
  561. int adc_period, conv;
  562. int i;
  563. int ch0 = 0, ch1 = 0;
  564. int thres;
  565. int ret;
  566. adc_period = adc->auto_conversion_period;
  567. for (i = 0; i < 16; ++i) {
  568. if (((1000 * (1 << i)) / 32) < adc_period)
  569. continue;
  570. }
  571. if (i > 0)
  572. i--;
  573. adc_period = i;
  574. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  575. PALMAS_GPADC_AUTO_CTRL,
  576. PALMAS_GPADC_AUTO_CTRL_COUNTER_CONV_MASK,
  577. adc_period);
  578. if (ret < 0) {
  579. dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret);
  580. return ret;
  581. }
  582. conv = 0;
  583. if (adc->wakeup1_enable) {
  584. int polarity;
  585. ch0 = adc->wakeup1_data.adc_channel_number;
  586. conv |= PALMAS_GPADC_AUTO_CTRL_AUTO_CONV0_EN;
  587. if (adc->wakeup1_data.adc_high_threshold > 0) {
  588. thres = adc->wakeup1_data.adc_high_threshold;
  589. polarity = 0;
  590. } else {
  591. thres = adc->wakeup1_data.adc_low_threshold;
  592. polarity = PALMAS_GPADC_THRES_CONV0_MSB_THRES_CONV0_POL;
  593. }
  594. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  595. PALMAS_GPADC_THRES_CONV0_LSB, thres & 0xFF);
  596. if (ret < 0) {
  597. dev_err(adc->dev,
  598. "THRES_CONV0_LSB write failed: %d\n", ret);
  599. return ret;
  600. }
  601. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  602. PALMAS_GPADC_THRES_CONV0_MSB,
  603. ((thres >> 8) & 0xF) | polarity);
  604. if (ret < 0) {
  605. dev_err(adc->dev,
  606. "THRES_CONV0_MSB write failed: %d\n", ret);
  607. return ret;
  608. }
  609. }
  610. if (adc->wakeup2_enable) {
  611. int polarity;
  612. ch1 = adc->wakeup2_data.adc_channel_number;
  613. conv |= PALMAS_GPADC_AUTO_CTRL_AUTO_CONV1_EN;
  614. if (adc->wakeup2_data.adc_high_threshold > 0) {
  615. thres = adc->wakeup2_data.adc_high_threshold;
  616. polarity = 0;
  617. } else {
  618. thres = adc->wakeup2_data.adc_low_threshold;
  619. polarity = PALMAS_GPADC_THRES_CONV1_MSB_THRES_CONV1_POL;
  620. }
  621. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  622. PALMAS_GPADC_THRES_CONV1_LSB, thres & 0xFF);
  623. if (ret < 0) {
  624. dev_err(adc->dev,
  625. "THRES_CONV1_LSB write failed: %d\n", ret);
  626. return ret;
  627. }
  628. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  629. PALMAS_GPADC_THRES_CONV1_MSB,
  630. ((thres >> 8) & 0xF) | polarity);
  631. if (ret < 0) {
  632. dev_err(adc->dev,
  633. "THRES_CONV1_MSB write failed: %d\n", ret);
  634. return ret;
  635. }
  636. }
  637. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  638. PALMAS_GPADC_AUTO_SELECT, (ch1 << 4) | ch0);
  639. if (ret < 0) {
  640. dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret);
  641. return ret;
  642. }
  643. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  644. PALMAS_GPADC_AUTO_CTRL,
  645. PALMAS_GPADC_AUTO_CTRL_AUTO_CONV1_EN |
  646. PALMAS_GPADC_AUTO_CTRL_AUTO_CONV0_EN, conv);
  647. if (ret < 0)
  648. dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret);
  649. return ret;
  650. }
  651. static int palmas_adc_wakeup_reset(struct palmas_gpadc *adc)
  652. {
  653. int ret;
  654. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  655. PALMAS_GPADC_AUTO_SELECT, 0);
  656. if (ret < 0) {
  657. dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret);
  658. return ret;
  659. }
  660. ret = palmas_disable_auto_conversion(adc);
  661. if (ret < 0)
  662. dev_err(adc->dev, "Disable auto conversion failed: %d\n", ret);
  663. return ret;
  664. }
  665. static int palmas_gpadc_suspend(struct device *dev)
  666. {
  667. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  668. struct palmas_gpadc *adc = iio_priv(indio_dev);
  669. int wakeup = adc->wakeup1_enable || adc->wakeup2_enable;
  670. int ret;
  671. if (!device_may_wakeup(dev) || !wakeup)
  672. return 0;
  673. ret = palmas_adc_wakeup_configure(adc);
  674. if (ret < 0)
  675. return ret;
  676. if (adc->wakeup1_enable)
  677. enable_irq_wake(adc->irq_auto_0);
  678. if (adc->wakeup2_enable)
  679. enable_irq_wake(adc->irq_auto_1);
  680. return 0;
  681. }
  682. static int palmas_gpadc_resume(struct device *dev)
  683. {
  684. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  685. struct palmas_gpadc *adc = iio_priv(indio_dev);
  686. int wakeup = adc->wakeup1_enable || adc->wakeup2_enable;
  687. int ret;
  688. if (!device_may_wakeup(dev) || !wakeup)
  689. return 0;
  690. ret = palmas_adc_wakeup_reset(adc);
  691. if (ret < 0)
  692. return ret;
  693. if (adc->wakeup1_enable)
  694. disable_irq_wake(adc->irq_auto_0);
  695. if (adc->wakeup2_enable)
  696. disable_irq_wake(adc->irq_auto_1);
  697. return 0;
  698. };
  699. #endif
  700. static const struct dev_pm_ops palmas_pm_ops = {
  701. SET_SYSTEM_SLEEP_PM_OPS(palmas_gpadc_suspend,
  702. palmas_gpadc_resume)
  703. };
  704. static const struct of_device_id of_palmas_gpadc_match_tbl[] = {
  705. { .compatible = "ti,palmas-gpadc", },
  706. { /* end */ }
  707. };
  708. MODULE_DEVICE_TABLE(of, of_palmas_gpadc_match_tbl);
  709. static struct platform_driver palmas_gpadc_driver = {
  710. .probe = palmas_gpadc_probe,
  711. .remove = palmas_gpadc_remove,
  712. .driver = {
  713. .name = MOD_NAME,
  714. .pm = &palmas_pm_ops,
  715. .of_match_table = of_palmas_gpadc_match_tbl,
  716. },
  717. };
  718. static int __init palmas_gpadc_init(void)
  719. {
  720. return platform_driver_register(&palmas_gpadc_driver);
  721. }
  722. module_init(palmas_gpadc_init);
  723. static void __exit palmas_gpadc_exit(void)
  724. {
  725. platform_driver_unregister(&palmas_gpadc_driver);
  726. }
  727. module_exit(palmas_gpadc_exit);
  728. MODULE_DESCRIPTION("palmas GPADC driver");
  729. MODULE_AUTHOR("Pradeep Goudagunta<pgoudagunta@nvidia.com>");
  730. MODULE_ALIAS("platform:palmas-gpadc");
  731. MODULE_LICENSE("GPL v2");