palmas_gpadc.c 23 KB

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