palmas_gpadc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. .driver_module = THIS_MODULE,
  372. };
  373. #define PALMAS_ADC_CHAN_IIO(chan, _type, chan_info) \
  374. { \
  375. .datasheet_name = PALMAS_DATASHEET_NAME(chan), \
  376. .type = _type, \
  377. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  378. BIT(chan_info), \
  379. .indexed = 1, \
  380. .channel = PALMAS_ADC_CH_##chan, \
  381. }
  382. static const struct iio_chan_spec palmas_gpadc_iio_channel[] = {
  383. PALMAS_ADC_CHAN_IIO(IN0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  384. PALMAS_ADC_CHAN_IIO(IN1, IIO_TEMP, IIO_CHAN_INFO_RAW),
  385. PALMAS_ADC_CHAN_IIO(IN2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  386. PALMAS_ADC_CHAN_IIO(IN3, IIO_TEMP, IIO_CHAN_INFO_RAW),
  387. PALMAS_ADC_CHAN_IIO(IN4, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  388. PALMAS_ADC_CHAN_IIO(IN5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  389. PALMAS_ADC_CHAN_IIO(IN6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  390. PALMAS_ADC_CHAN_IIO(IN7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  391. PALMAS_ADC_CHAN_IIO(IN8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  392. PALMAS_ADC_CHAN_IIO(IN9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  393. PALMAS_ADC_CHAN_IIO(IN10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  394. PALMAS_ADC_CHAN_IIO(IN11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  395. PALMAS_ADC_CHAN_IIO(IN12, IIO_TEMP, IIO_CHAN_INFO_RAW),
  396. PALMAS_ADC_CHAN_IIO(IN13, IIO_TEMP, IIO_CHAN_INFO_RAW),
  397. PALMAS_ADC_CHAN_IIO(IN14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  398. PALMAS_ADC_CHAN_IIO(IN15, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  399. };
  400. static int palmas_gpadc_get_adc_dt_data(struct platform_device *pdev,
  401. struct palmas_gpadc_platform_data **gpadc_pdata)
  402. {
  403. struct device_node *np = pdev->dev.of_node;
  404. struct palmas_gpadc_platform_data *gp_data;
  405. int ret;
  406. u32 pval;
  407. gp_data = devm_kzalloc(&pdev->dev, sizeof(*gp_data), GFP_KERNEL);
  408. if (!gp_data)
  409. return -ENOMEM;
  410. ret = of_property_read_u32(np, "ti,channel0-current-microamp", &pval);
  411. if (!ret)
  412. gp_data->ch0_current = pval;
  413. ret = of_property_read_u32(np, "ti,channel3-current-microamp", &pval);
  414. if (!ret)
  415. gp_data->ch3_current = pval;
  416. gp_data->extended_delay = of_property_read_bool(np,
  417. "ti,enable-extended-delay");
  418. *gpadc_pdata = gp_data;
  419. return 0;
  420. }
  421. static int palmas_gpadc_probe(struct platform_device *pdev)
  422. {
  423. struct palmas_gpadc *adc;
  424. struct palmas_platform_data *pdata;
  425. struct palmas_gpadc_platform_data *gpadc_pdata = NULL;
  426. struct iio_dev *indio_dev;
  427. int ret, i;
  428. pdata = dev_get_platdata(pdev->dev.parent);
  429. if (pdata && pdata->gpadc_pdata)
  430. gpadc_pdata = pdata->gpadc_pdata;
  431. if (!gpadc_pdata && pdev->dev.of_node) {
  432. ret = palmas_gpadc_get_adc_dt_data(pdev, &gpadc_pdata);
  433. if (ret < 0)
  434. return ret;
  435. }
  436. if (!gpadc_pdata)
  437. return -EINVAL;
  438. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  439. if (!indio_dev) {
  440. dev_err(&pdev->dev, "iio_device_alloc failed\n");
  441. return -ENOMEM;
  442. }
  443. adc = iio_priv(indio_dev);
  444. adc->dev = &pdev->dev;
  445. adc->palmas = dev_get_drvdata(pdev->dev.parent);
  446. adc->adc_info = palmas_gpadc_info;
  447. init_completion(&adc->conv_completion);
  448. dev_set_drvdata(&pdev->dev, indio_dev);
  449. adc->auto_conversion_period = gpadc_pdata->auto_conversion_period_ms;
  450. adc->irq = palmas_irq_get_virq(adc->palmas, PALMAS_GPADC_EOC_SW_IRQ);
  451. if (adc->irq < 0) {
  452. dev_err(adc->dev,
  453. "get virq failed: %d\n", adc->irq);
  454. ret = adc->irq;
  455. goto out;
  456. }
  457. ret = request_threaded_irq(adc->irq, NULL,
  458. palmas_gpadc_irq,
  459. IRQF_ONESHOT, dev_name(adc->dev),
  460. adc);
  461. if (ret < 0) {
  462. dev_err(adc->dev,
  463. "request irq %d failed: %d\n", adc->irq, ret);
  464. goto out;
  465. }
  466. if (gpadc_pdata->adc_wakeup1_data) {
  467. memcpy(&adc->wakeup1_data, gpadc_pdata->adc_wakeup1_data,
  468. sizeof(adc->wakeup1_data));
  469. adc->wakeup1_enable = true;
  470. adc->irq_auto_0 = platform_get_irq(pdev, 1);
  471. ret = request_threaded_irq(adc->irq_auto_0, NULL,
  472. palmas_gpadc_irq_auto,
  473. IRQF_ONESHOT,
  474. "palmas-adc-auto-0", adc);
  475. if (ret < 0) {
  476. dev_err(adc->dev, "request auto0 irq %d failed: %d\n",
  477. adc->irq_auto_0, ret);
  478. goto out_irq_free;
  479. }
  480. }
  481. if (gpadc_pdata->adc_wakeup2_data) {
  482. memcpy(&adc->wakeup2_data, gpadc_pdata->adc_wakeup2_data,
  483. sizeof(adc->wakeup2_data));
  484. adc->wakeup2_enable = true;
  485. adc->irq_auto_1 = platform_get_irq(pdev, 2);
  486. ret = request_threaded_irq(adc->irq_auto_1, NULL,
  487. palmas_gpadc_irq_auto,
  488. IRQF_ONESHOT,
  489. "palmas-adc-auto-1", adc);
  490. if (ret < 0) {
  491. dev_err(adc->dev, "request auto1 irq %d failed: %d\n",
  492. adc->irq_auto_1, ret);
  493. goto out_irq_auto0_free;
  494. }
  495. }
  496. /* set the current source 0 (value 0/5/15/20 uA => 0..3) */
  497. if (gpadc_pdata->ch0_current <= 1)
  498. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_0;
  499. else if (gpadc_pdata->ch0_current <= 5)
  500. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_5;
  501. else if (gpadc_pdata->ch0_current <= 15)
  502. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_15;
  503. else
  504. adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_20;
  505. /* set the current source 3 (value 0/10/400/800 uA => 0..3) */
  506. if (gpadc_pdata->ch3_current <= 1)
  507. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_0;
  508. else if (gpadc_pdata->ch3_current <= 10)
  509. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_10;
  510. else if (gpadc_pdata->ch3_current <= 400)
  511. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_400;
  512. else
  513. adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_800;
  514. adc->extended_delay = gpadc_pdata->extended_delay;
  515. indio_dev->name = MOD_NAME;
  516. indio_dev->dev.parent = &pdev->dev;
  517. indio_dev->info = &palmas_gpadc_iio_info;
  518. indio_dev->modes = INDIO_DIRECT_MODE;
  519. indio_dev->channels = palmas_gpadc_iio_channel;
  520. indio_dev->num_channels = ARRAY_SIZE(palmas_gpadc_iio_channel);
  521. ret = iio_device_register(indio_dev);
  522. if (ret < 0) {
  523. dev_err(adc->dev, "iio_device_register() failed: %d\n", ret);
  524. goto out_irq_auto1_free;
  525. }
  526. device_set_wakeup_capable(&pdev->dev, 1);
  527. for (i = 0; i < PALMAS_ADC_CH_MAX; i++) {
  528. if (!(adc->adc_info[i].is_uncalibrated))
  529. palmas_gpadc_calibrate(adc, i);
  530. }
  531. if (adc->wakeup1_enable || adc->wakeup2_enable)
  532. device_wakeup_enable(&pdev->dev);
  533. return 0;
  534. out_irq_auto1_free:
  535. if (gpadc_pdata->adc_wakeup2_data)
  536. free_irq(adc->irq_auto_1, adc);
  537. out_irq_auto0_free:
  538. if (gpadc_pdata->adc_wakeup1_data)
  539. free_irq(adc->irq_auto_0, adc);
  540. out_irq_free:
  541. free_irq(adc->irq, adc);
  542. out:
  543. return ret;
  544. }
  545. static int palmas_gpadc_remove(struct platform_device *pdev)
  546. {
  547. struct iio_dev *indio_dev = dev_to_iio_dev(&pdev->dev);
  548. struct palmas_gpadc *adc = iio_priv(indio_dev);
  549. if (adc->wakeup1_enable || adc->wakeup2_enable)
  550. device_wakeup_disable(&pdev->dev);
  551. iio_device_unregister(indio_dev);
  552. free_irq(adc->irq, adc);
  553. if (adc->wakeup1_enable)
  554. free_irq(adc->irq_auto_0, adc);
  555. if (adc->wakeup2_enable)
  556. free_irq(adc->irq_auto_1, adc);
  557. return 0;
  558. }
  559. #ifdef CONFIG_PM_SLEEP
  560. static int palmas_adc_wakeup_configure(struct palmas_gpadc *adc)
  561. {
  562. int adc_period, conv;
  563. int i;
  564. int ch0 = 0, ch1 = 0;
  565. int thres;
  566. int ret;
  567. adc_period = adc->auto_conversion_period;
  568. for (i = 0; i < 16; ++i) {
  569. if (((1000 * (1 << i)) / 32) < adc_period)
  570. continue;
  571. }
  572. if (i > 0)
  573. i--;
  574. adc_period = i;
  575. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  576. PALMAS_GPADC_AUTO_CTRL,
  577. PALMAS_GPADC_AUTO_CTRL_COUNTER_CONV_MASK,
  578. adc_period);
  579. if (ret < 0) {
  580. dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret);
  581. return ret;
  582. }
  583. conv = 0;
  584. if (adc->wakeup1_enable) {
  585. int polarity;
  586. ch0 = adc->wakeup1_data.adc_channel_number;
  587. conv |= PALMAS_GPADC_AUTO_CTRL_AUTO_CONV0_EN;
  588. if (adc->wakeup1_data.adc_high_threshold > 0) {
  589. thres = adc->wakeup1_data.adc_high_threshold;
  590. polarity = 0;
  591. } else {
  592. thres = adc->wakeup1_data.adc_low_threshold;
  593. polarity = PALMAS_GPADC_THRES_CONV0_MSB_THRES_CONV0_POL;
  594. }
  595. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  596. PALMAS_GPADC_THRES_CONV0_LSB, thres & 0xFF);
  597. if (ret < 0) {
  598. dev_err(adc->dev,
  599. "THRES_CONV0_LSB write failed: %d\n", ret);
  600. return ret;
  601. }
  602. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  603. PALMAS_GPADC_THRES_CONV0_MSB,
  604. ((thres >> 8) & 0xF) | polarity);
  605. if (ret < 0) {
  606. dev_err(adc->dev,
  607. "THRES_CONV0_MSB write failed: %d\n", ret);
  608. return ret;
  609. }
  610. }
  611. if (adc->wakeup2_enable) {
  612. int polarity;
  613. ch1 = adc->wakeup2_data.adc_channel_number;
  614. conv |= PALMAS_GPADC_AUTO_CTRL_AUTO_CONV1_EN;
  615. if (adc->wakeup2_data.adc_high_threshold > 0) {
  616. thres = adc->wakeup2_data.adc_high_threshold;
  617. polarity = 0;
  618. } else {
  619. thres = adc->wakeup2_data.adc_low_threshold;
  620. polarity = PALMAS_GPADC_THRES_CONV1_MSB_THRES_CONV1_POL;
  621. }
  622. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  623. PALMAS_GPADC_THRES_CONV1_LSB, thres & 0xFF);
  624. if (ret < 0) {
  625. dev_err(adc->dev,
  626. "THRES_CONV1_LSB write failed: %d\n", ret);
  627. return ret;
  628. }
  629. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  630. PALMAS_GPADC_THRES_CONV1_MSB,
  631. ((thres >> 8) & 0xF) | polarity);
  632. if (ret < 0) {
  633. dev_err(adc->dev,
  634. "THRES_CONV1_MSB write failed: %d\n", ret);
  635. return ret;
  636. }
  637. }
  638. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  639. PALMAS_GPADC_AUTO_SELECT, (ch1 << 4) | ch0);
  640. if (ret < 0) {
  641. dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret);
  642. return ret;
  643. }
  644. ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE,
  645. PALMAS_GPADC_AUTO_CTRL,
  646. PALMAS_GPADC_AUTO_CTRL_AUTO_CONV1_EN |
  647. PALMAS_GPADC_AUTO_CTRL_AUTO_CONV0_EN, conv);
  648. if (ret < 0)
  649. dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret);
  650. return ret;
  651. }
  652. static int palmas_adc_wakeup_reset(struct palmas_gpadc *adc)
  653. {
  654. int ret;
  655. ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE,
  656. PALMAS_GPADC_AUTO_SELECT, 0);
  657. if (ret < 0) {
  658. dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret);
  659. return ret;
  660. }
  661. ret = palmas_disable_auto_conversion(adc);
  662. if (ret < 0)
  663. dev_err(adc->dev, "Disable auto conversion failed: %d\n", ret);
  664. return ret;
  665. }
  666. static int palmas_gpadc_suspend(struct device *dev)
  667. {
  668. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  669. struct palmas_gpadc *adc = iio_priv(indio_dev);
  670. int wakeup = adc->wakeup1_enable || adc->wakeup2_enable;
  671. int ret;
  672. if (!device_may_wakeup(dev) || !wakeup)
  673. return 0;
  674. ret = palmas_adc_wakeup_configure(adc);
  675. if (ret < 0)
  676. return ret;
  677. if (adc->wakeup1_enable)
  678. enable_irq_wake(adc->irq_auto_0);
  679. if (adc->wakeup2_enable)
  680. enable_irq_wake(adc->irq_auto_1);
  681. return 0;
  682. }
  683. static int palmas_gpadc_resume(struct device *dev)
  684. {
  685. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  686. struct palmas_gpadc *adc = iio_priv(indio_dev);
  687. int wakeup = adc->wakeup1_enable || adc->wakeup2_enable;
  688. int ret;
  689. if (!device_may_wakeup(dev) || !wakeup)
  690. return 0;
  691. ret = palmas_adc_wakeup_reset(adc);
  692. if (ret < 0)
  693. return ret;
  694. if (adc->wakeup1_enable)
  695. disable_irq_wake(adc->irq_auto_0);
  696. if (adc->wakeup2_enable)
  697. disable_irq_wake(adc->irq_auto_1);
  698. return 0;
  699. };
  700. #endif
  701. static const struct dev_pm_ops palmas_pm_ops = {
  702. SET_SYSTEM_SLEEP_PM_OPS(palmas_gpadc_suspend,
  703. palmas_gpadc_resume)
  704. };
  705. static const struct of_device_id of_palmas_gpadc_match_tbl[] = {
  706. { .compatible = "ti,palmas-gpadc", },
  707. { /* end */ }
  708. };
  709. MODULE_DEVICE_TABLE(of, of_palmas_gpadc_match_tbl);
  710. static struct platform_driver palmas_gpadc_driver = {
  711. .probe = palmas_gpadc_probe,
  712. .remove = palmas_gpadc_remove,
  713. .driver = {
  714. .name = MOD_NAME,
  715. .pm = &palmas_pm_ops,
  716. .of_match_table = of_palmas_gpadc_match_tbl,
  717. },
  718. };
  719. static int __init palmas_gpadc_init(void)
  720. {
  721. return platform_driver_register(&palmas_gpadc_driver);
  722. }
  723. module_init(palmas_gpadc_init);
  724. static void __exit palmas_gpadc_exit(void)
  725. {
  726. platform_driver_unregister(&palmas_gpadc_driver);
  727. }
  728. module_exit(palmas_gpadc_exit);
  729. MODULE_DESCRIPTION("palmas GPADC driver");
  730. MODULE_AUTHOR("Pradeep Goudagunta<pgoudagunta@nvidia.com>");
  731. MODULE_ALIAS("platform:palmas-gpadc");
  732. MODULE_LICENSE("GPL v2");