bcm_iproc_adc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright 2016 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation (the "GPL").
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License version 2 (GPLv2) for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * version 2 (GPLv2) along with this source code.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/iio/iio.h>
  26. /* Below Register's are common to IPROC ADC and Touchscreen IP */
  27. #define IPROC_REGCTL1 0x00
  28. #define IPROC_REGCTL2 0x04
  29. #define IPROC_INTERRUPT_THRES 0x08
  30. #define IPROC_INTERRUPT_MASK 0x0c
  31. #define IPROC_INTERRUPT_STATUS 0x10
  32. #define IPROC_ANALOG_CONTROL 0x1c
  33. #define IPROC_CONTROLLER_STATUS 0x14
  34. #define IPROC_AUX_DATA 0x20
  35. #define IPROC_SOFT_BYPASS_CONTROL 0x38
  36. #define IPROC_SOFT_BYPASS_DATA 0x3C
  37. /* IPROC ADC Channel register offsets */
  38. #define IPROC_ADC_CHANNEL_REGCTL1 0x800
  39. #define IPROC_ADC_CHANNEL_REGCTL2 0x804
  40. #define IPROC_ADC_CHANNEL_STATUS 0x808
  41. #define IPROC_ADC_CHANNEL_INTERRUPT_STATUS 0x80c
  42. #define IPROC_ADC_CHANNEL_INTERRUPT_MASK 0x810
  43. #define IPROC_ADC_CHANNEL_DATA 0x814
  44. #define IPROC_ADC_CHANNEL_OFFSET 0x20
  45. /* Bit definitions for IPROC_REGCTL2 */
  46. #define IPROC_ADC_AUXIN_SCAN_ENA BIT(0)
  47. #define IPROC_ADC_PWR_LDO BIT(5)
  48. #define IPROC_ADC_PWR_ADC BIT(4)
  49. #define IPROC_ADC_PWR_BG BIT(3)
  50. #define IPROC_ADC_CONTROLLER_EN BIT(17)
  51. /* Bit definitions for IPROC_INTERRUPT_MASK and IPROC_INTERRUPT_STATUS */
  52. #define IPROC_ADC_AUXDATA_RDY_INTR BIT(3)
  53. #define IPROC_ADC_INTR 9
  54. #define IPROC_ADC_INTR_MASK (0xFF << IPROC_ADC_INTR)
  55. /* Bit definitions for IPROC_ANALOG_CONTROL */
  56. #define IPROC_ADC_CHANNEL_SEL 11
  57. #define IPROC_ADC_CHANNEL_SEL_MASK (0x7 << IPROC_ADC_CHANNEL_SEL)
  58. /* Bit definitions for IPROC_ADC_CHANNEL_REGCTL1 */
  59. #define IPROC_ADC_CHANNEL_ROUNDS 0x2
  60. #define IPROC_ADC_CHANNEL_ROUNDS_MASK (0x3F << IPROC_ADC_CHANNEL_ROUNDS)
  61. #define IPROC_ADC_CHANNEL_MODE 0x1
  62. #define IPROC_ADC_CHANNEL_MODE_MASK (0x1 << IPROC_ADC_CHANNEL_MODE)
  63. #define IPROC_ADC_CHANNEL_MODE_TDM 0x1
  64. #define IPROC_ADC_CHANNEL_MODE_SNAPSHOT 0x0
  65. #define IPROC_ADC_CHANNEL_ENABLE 0x0
  66. #define IPROC_ADC_CHANNEL_ENABLE_MASK 0x1
  67. /* Bit definitions for IPROC_ADC_CHANNEL_REGCTL2 */
  68. #define IPROC_ADC_CHANNEL_WATERMARK 0x0
  69. #define IPROC_ADC_CHANNEL_WATERMARK_MASK \
  70. (0x3F << IPROC_ADC_CHANNEL_WATERMARK)
  71. #define IPROC_ADC_WATER_MARK_LEVEL 0x1
  72. /* Bit definitions for IPROC_ADC_CHANNEL_STATUS */
  73. #define IPROC_ADC_CHANNEL_DATA_LOST 0x0
  74. #define IPROC_ADC_CHANNEL_DATA_LOST_MASK \
  75. (0x0 << IPROC_ADC_CHANNEL_DATA_LOST)
  76. #define IPROC_ADC_CHANNEL_VALID_ENTERIES 0x1
  77. #define IPROC_ADC_CHANNEL_VALID_ENTERIES_MASK \
  78. (0xFF << IPROC_ADC_CHANNEL_VALID_ENTERIES)
  79. #define IPROC_ADC_CHANNEL_TOTAL_ENTERIES 0x9
  80. #define IPROC_ADC_CHANNEL_TOTAL_ENTERIES_MASK \
  81. (0xFF << IPROC_ADC_CHANNEL_TOTAL_ENTERIES)
  82. /* Bit definitions for IPROC_ADC_CHANNEL_INTERRUPT_MASK */
  83. #define IPROC_ADC_CHANNEL_WTRMRK_INTR 0x0
  84. #define IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK \
  85. (0x1 << IPROC_ADC_CHANNEL_WTRMRK_INTR)
  86. #define IPROC_ADC_CHANNEL_FULL_INTR 0x1
  87. #define IPROC_ADC_CHANNEL_FULL_INTR_MASK \
  88. (0x1 << IPROC_ADC_IPROC_ADC_CHANNEL_FULL_INTR)
  89. #define IPROC_ADC_CHANNEL_EMPTY_INTR 0x2
  90. #define IPROC_ADC_CHANNEL_EMPTY_INTR_MASK \
  91. (0x1 << IPROC_ADC_CHANNEL_EMPTY_INTR)
  92. #define IPROC_ADC_WATER_MARK_INTR_ENABLE 0x1
  93. /* Number of time to retry a set of the interrupt mask reg */
  94. #define IPROC_ADC_INTMASK_RETRY_ATTEMPTS 10
  95. #define IPROC_ADC_READ_TIMEOUT (HZ*2)
  96. #define iproc_adc_dbg_reg(dev, priv, reg) \
  97. do { \
  98. u32 val; \
  99. regmap_read(priv->regmap, reg, &val); \
  100. dev_dbg(dev, "%20s= 0x%08x\n", #reg, val); \
  101. } while (0)
  102. struct iproc_adc_priv {
  103. struct regmap *regmap;
  104. struct clk *adc_clk;
  105. struct mutex mutex;
  106. int irqno;
  107. int chan_val;
  108. int chan_id;
  109. struct completion completion;
  110. };
  111. static void iproc_adc_reg_dump(struct iio_dev *indio_dev)
  112. {
  113. struct device *dev = &indio_dev->dev;
  114. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  115. iproc_adc_dbg_reg(dev, adc_priv, IPROC_REGCTL1);
  116. iproc_adc_dbg_reg(dev, adc_priv, IPROC_REGCTL2);
  117. iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_THRES);
  118. iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_MASK);
  119. iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_STATUS);
  120. iproc_adc_dbg_reg(dev, adc_priv, IPROC_CONTROLLER_STATUS);
  121. iproc_adc_dbg_reg(dev, adc_priv, IPROC_ANALOG_CONTROL);
  122. iproc_adc_dbg_reg(dev, adc_priv, IPROC_AUX_DATA);
  123. iproc_adc_dbg_reg(dev, adc_priv, IPROC_SOFT_BYPASS_CONTROL);
  124. iproc_adc_dbg_reg(dev, adc_priv, IPROC_SOFT_BYPASS_DATA);
  125. }
  126. static irqreturn_t iproc_adc_interrupt_thread(int irq, void *data)
  127. {
  128. u32 channel_intr_status;
  129. u32 intr_status;
  130. u32 intr_mask;
  131. struct iio_dev *indio_dev = data;
  132. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  133. /*
  134. * This interrupt is shared with the touchscreen driver.
  135. * Make sure this interrupt is intended for us.
  136. * Handle only ADC channel specific interrupts.
  137. */
  138. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_STATUS, &intr_status);
  139. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &intr_mask);
  140. intr_status = intr_status & intr_mask;
  141. channel_intr_status = (intr_status & IPROC_ADC_INTR_MASK) >>
  142. IPROC_ADC_INTR;
  143. if (channel_intr_status)
  144. return IRQ_WAKE_THREAD;
  145. return IRQ_NONE;
  146. }
  147. static irqreturn_t iproc_adc_interrupt_handler(int irq, void *data)
  148. {
  149. irqreturn_t retval = IRQ_NONE;
  150. struct iproc_adc_priv *adc_priv;
  151. struct iio_dev *indio_dev = data;
  152. unsigned int valid_entries;
  153. u32 intr_status;
  154. u32 intr_channels;
  155. u32 channel_status;
  156. u32 ch_intr_status;
  157. adc_priv = iio_priv(indio_dev);
  158. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_STATUS, &intr_status);
  159. dev_dbg(&indio_dev->dev, "iproc_adc_interrupt_handler(),INTRPT_STS:%x\n",
  160. intr_status);
  161. intr_channels = (intr_status & IPROC_ADC_INTR_MASK) >> IPROC_ADC_INTR;
  162. if (intr_channels) {
  163. regmap_read(adc_priv->regmap,
  164. IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
  165. IPROC_ADC_CHANNEL_OFFSET * adc_priv->chan_id,
  166. &ch_intr_status);
  167. if (ch_intr_status & IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK) {
  168. regmap_read(adc_priv->regmap,
  169. IPROC_ADC_CHANNEL_STATUS +
  170. IPROC_ADC_CHANNEL_OFFSET *
  171. adc_priv->chan_id,
  172. &channel_status);
  173. valid_entries = ((channel_status &
  174. IPROC_ADC_CHANNEL_VALID_ENTERIES_MASK) >>
  175. IPROC_ADC_CHANNEL_VALID_ENTERIES);
  176. if (valid_entries >= 1) {
  177. regmap_read(adc_priv->regmap,
  178. IPROC_ADC_CHANNEL_DATA +
  179. IPROC_ADC_CHANNEL_OFFSET *
  180. adc_priv->chan_id,
  181. &adc_priv->chan_val);
  182. complete(&adc_priv->completion);
  183. } else {
  184. dev_err(&indio_dev->dev,
  185. "No data rcvd on channel %d\n",
  186. adc_priv->chan_id);
  187. }
  188. regmap_write(adc_priv->regmap,
  189. IPROC_ADC_CHANNEL_INTERRUPT_MASK +
  190. IPROC_ADC_CHANNEL_OFFSET *
  191. adc_priv->chan_id,
  192. (ch_intr_status &
  193. ~(IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK)));
  194. }
  195. regmap_write(adc_priv->regmap,
  196. IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
  197. IPROC_ADC_CHANNEL_OFFSET * adc_priv->chan_id,
  198. ch_intr_status);
  199. regmap_write(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
  200. intr_channels);
  201. retval = IRQ_HANDLED;
  202. }
  203. return retval;
  204. }
  205. static int iproc_adc_do_read(struct iio_dev *indio_dev,
  206. int channel,
  207. u16 *p_adc_data)
  208. {
  209. int read_len = 0;
  210. u32 val;
  211. u32 mask;
  212. u32 val_check;
  213. int failed_cnt = 0;
  214. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  215. mutex_lock(&adc_priv->mutex);
  216. /*
  217. * After a read is complete the ADC interrupts will be disabled so
  218. * we can assume this section of code is safe from interrupts.
  219. */
  220. adc_priv->chan_val = -1;
  221. adc_priv->chan_id = channel;
  222. reinit_completion(&adc_priv->completion);
  223. /* Clear any pending interrupt */
  224. regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
  225. IPROC_ADC_INTR_MASK | IPROC_ADC_AUXDATA_RDY_INTR,
  226. ((0x0 << channel) << IPROC_ADC_INTR) |
  227. IPROC_ADC_AUXDATA_RDY_INTR);
  228. /* Configure channel for snapshot mode and enable */
  229. val = (BIT(IPROC_ADC_CHANNEL_ROUNDS) |
  230. (IPROC_ADC_CHANNEL_MODE_SNAPSHOT << IPROC_ADC_CHANNEL_MODE) |
  231. (0x1 << IPROC_ADC_CHANNEL_ENABLE));
  232. mask = IPROC_ADC_CHANNEL_ROUNDS_MASK | IPROC_ADC_CHANNEL_MODE_MASK |
  233. IPROC_ADC_CHANNEL_ENABLE_MASK;
  234. regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_REGCTL1 +
  235. IPROC_ADC_CHANNEL_OFFSET * channel),
  236. mask, val);
  237. /* Set the Watermark for a channel */
  238. regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_REGCTL2 +
  239. IPROC_ADC_CHANNEL_OFFSET * channel),
  240. IPROC_ADC_CHANNEL_WATERMARK_MASK,
  241. 0x1);
  242. /* Enable water mark interrupt */
  243. regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_INTERRUPT_MASK +
  244. IPROC_ADC_CHANNEL_OFFSET *
  245. channel),
  246. IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK,
  247. IPROC_ADC_WATER_MARK_INTR_ENABLE);
  248. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val);
  249. /* Enable ADC interrupt for a channel */
  250. val |= (BIT(channel) << IPROC_ADC_INTR);
  251. regmap_write(adc_priv->regmap, IPROC_INTERRUPT_MASK, val);
  252. /*
  253. * There seems to be a very rare issue where writing to this register
  254. * does not take effect. To work around the issue we will try multiple
  255. * writes. In total we will spend about 10*10 = 100 us attempting this.
  256. * Testing has shown that this may loop a few time, but we have never
  257. * hit the full count.
  258. */
  259. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
  260. while (val_check != val) {
  261. failed_cnt++;
  262. if (failed_cnt > IPROC_ADC_INTMASK_RETRY_ATTEMPTS)
  263. break;
  264. udelay(10);
  265. regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_MASK,
  266. IPROC_ADC_INTR_MASK,
  267. ((0x1 << channel) <<
  268. IPROC_ADC_INTR));
  269. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
  270. }
  271. if (failed_cnt) {
  272. dev_dbg(&indio_dev->dev,
  273. "IntMask failed (%d times)", failed_cnt);
  274. if (failed_cnt > IPROC_ADC_INTMASK_RETRY_ATTEMPTS) {
  275. dev_err(&indio_dev->dev,
  276. "IntMask set failed. Read will likely fail.");
  277. read_len = -EIO;
  278. goto adc_err;
  279. };
  280. }
  281. regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
  282. if (wait_for_completion_timeout(&adc_priv->completion,
  283. IPROC_ADC_READ_TIMEOUT) > 0) {
  284. /* Only the lower 16 bits are relevant */
  285. *p_adc_data = adc_priv->chan_val & 0xFFFF;
  286. read_len = sizeof(*p_adc_data);
  287. } else {
  288. /*
  289. * We never got the interrupt, something went wrong.
  290. * Perhaps the interrupt may still be coming, we do not want
  291. * that now. Lets disable the ADC interrupt, and clear the
  292. * status to put it back in to normal state.
  293. */
  294. read_len = -ETIMEDOUT;
  295. goto adc_err;
  296. }
  297. mutex_unlock(&adc_priv->mutex);
  298. return read_len;
  299. adc_err:
  300. regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_MASK,
  301. IPROC_ADC_INTR_MASK,
  302. ((0x0 << channel) << IPROC_ADC_INTR));
  303. regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
  304. IPROC_ADC_INTR_MASK,
  305. ((0x0 << channel) << IPROC_ADC_INTR));
  306. dev_err(&indio_dev->dev, "Timed out waiting for ADC data!\n");
  307. iproc_adc_reg_dump(indio_dev);
  308. mutex_unlock(&adc_priv->mutex);
  309. return read_len;
  310. }
  311. static int iproc_adc_enable(struct iio_dev *indio_dev)
  312. {
  313. u32 val;
  314. u32 channel_id;
  315. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  316. int ret;
  317. /* Set i_amux = 3b'000, select channel 0 */
  318. ret = regmap_update_bits(adc_priv->regmap, IPROC_ANALOG_CONTROL,
  319. IPROC_ADC_CHANNEL_SEL_MASK, 0);
  320. if (ret) {
  321. dev_err(&indio_dev->dev,
  322. "failed to write IPROC_ANALOG_CONTROL %d\n", ret);
  323. return ret;
  324. }
  325. adc_priv->chan_val = -1;
  326. /*
  327. * PWR up LDO, ADC, and Band Gap (0 to enable)
  328. * Also enable ADC controller (set high)
  329. */
  330. ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
  331. if (ret) {
  332. dev_err(&indio_dev->dev,
  333. "failed to read IPROC_REGCTL2 %d\n", ret);
  334. return ret;
  335. }
  336. val &= ~(IPROC_ADC_PWR_LDO | IPROC_ADC_PWR_ADC | IPROC_ADC_PWR_BG);
  337. ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
  338. if (ret) {
  339. dev_err(&indio_dev->dev,
  340. "failed to write IPROC_REGCTL2 %d\n", ret);
  341. return ret;
  342. }
  343. ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
  344. if (ret) {
  345. dev_err(&indio_dev->dev,
  346. "failed to read IPROC_REGCTL2 %d\n", ret);
  347. return ret;
  348. }
  349. val |= IPROC_ADC_CONTROLLER_EN;
  350. ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
  351. if (ret) {
  352. dev_err(&indio_dev->dev,
  353. "failed to write IPROC_REGCTL2 %d\n", ret);
  354. return ret;
  355. }
  356. for (channel_id = 0; channel_id < indio_dev->num_channels;
  357. channel_id++) {
  358. ret = regmap_write(adc_priv->regmap,
  359. IPROC_ADC_CHANNEL_INTERRUPT_MASK +
  360. IPROC_ADC_CHANNEL_OFFSET * channel_id, 0);
  361. if (ret) {
  362. dev_err(&indio_dev->dev,
  363. "failed to write ADC_CHANNEL_INTERRUPT_MASK %d\n",
  364. ret);
  365. return ret;
  366. }
  367. ret = regmap_write(adc_priv->regmap,
  368. IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
  369. IPROC_ADC_CHANNEL_OFFSET * channel_id, 0);
  370. if (ret) {
  371. dev_err(&indio_dev->dev,
  372. "failed to write ADC_CHANNEL_INTERRUPT_STATUS %d\n",
  373. ret);
  374. return ret;
  375. }
  376. }
  377. return 0;
  378. }
  379. static void iproc_adc_disable(struct iio_dev *indio_dev)
  380. {
  381. u32 val;
  382. int ret;
  383. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  384. ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
  385. if (ret) {
  386. dev_err(&indio_dev->dev,
  387. "failed to read IPROC_REGCTL2 %d\n", ret);
  388. return;
  389. }
  390. val &= ~IPROC_ADC_CONTROLLER_EN;
  391. ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
  392. if (ret) {
  393. dev_err(&indio_dev->dev,
  394. "failed to write IPROC_REGCTL2 %d\n", ret);
  395. return;
  396. }
  397. }
  398. static int iproc_adc_read_raw(struct iio_dev *indio_dev,
  399. struct iio_chan_spec const *chan,
  400. int *val,
  401. int *val2,
  402. long mask)
  403. {
  404. u16 adc_data;
  405. int err;
  406. switch (mask) {
  407. case IIO_CHAN_INFO_RAW:
  408. err = iproc_adc_do_read(indio_dev, chan->channel, &adc_data);
  409. if (err < 0)
  410. return err;
  411. *val = adc_data;
  412. return IIO_VAL_INT;
  413. case IIO_CHAN_INFO_SCALE:
  414. switch (chan->type) {
  415. case IIO_VOLTAGE:
  416. *val = 1800;
  417. *val2 = 10;
  418. return IIO_VAL_FRACTIONAL_LOG2;
  419. default:
  420. return -EINVAL;
  421. }
  422. default:
  423. return -EINVAL;
  424. }
  425. }
  426. static const struct iio_info iproc_adc_iio_info = {
  427. .read_raw = &iproc_adc_read_raw,
  428. .driver_module = THIS_MODULE,
  429. };
  430. #define IPROC_ADC_CHANNEL(_index, _id) { \
  431. .type = IIO_VOLTAGE, \
  432. .indexed = 1, \
  433. .channel = _index, \
  434. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  435. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  436. .datasheet_name = _id, \
  437. }
  438. static const struct iio_chan_spec iproc_adc_iio_channels[] = {
  439. IPROC_ADC_CHANNEL(0, "adc0"),
  440. IPROC_ADC_CHANNEL(1, "adc1"),
  441. IPROC_ADC_CHANNEL(2, "adc2"),
  442. IPROC_ADC_CHANNEL(3, "adc3"),
  443. IPROC_ADC_CHANNEL(4, "adc4"),
  444. IPROC_ADC_CHANNEL(5, "adc5"),
  445. IPROC_ADC_CHANNEL(6, "adc6"),
  446. IPROC_ADC_CHANNEL(7, "adc7"),
  447. };
  448. static int iproc_adc_probe(struct platform_device *pdev)
  449. {
  450. struct iproc_adc_priv *adc_priv;
  451. struct iio_dev *indio_dev = NULL;
  452. int ret;
  453. indio_dev = devm_iio_device_alloc(&pdev->dev,
  454. sizeof(*adc_priv));
  455. if (!indio_dev) {
  456. dev_err(&pdev->dev, "failed to allocate iio device\n");
  457. return -ENOMEM;
  458. }
  459. adc_priv = iio_priv(indio_dev);
  460. platform_set_drvdata(pdev, indio_dev);
  461. mutex_init(&adc_priv->mutex);
  462. init_completion(&adc_priv->completion);
  463. adc_priv->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  464. "adc-syscon");
  465. if (IS_ERR(adc_priv->regmap)) {
  466. dev_err(&pdev->dev, "failed to get handle for tsc syscon\n");
  467. ret = PTR_ERR(adc_priv->regmap);
  468. return ret;
  469. }
  470. adc_priv->adc_clk = devm_clk_get(&pdev->dev, "tsc_clk");
  471. if (IS_ERR(adc_priv->adc_clk)) {
  472. dev_err(&pdev->dev,
  473. "failed getting clock tsc_clk\n");
  474. ret = PTR_ERR(adc_priv->adc_clk);
  475. return ret;
  476. }
  477. adc_priv->irqno = platform_get_irq(pdev, 0);
  478. if (adc_priv->irqno <= 0) {
  479. dev_err(&pdev->dev, "platform_get_irq failed\n");
  480. ret = -ENODEV;
  481. return ret;
  482. }
  483. ret = regmap_update_bits(adc_priv->regmap, IPROC_REGCTL2,
  484. IPROC_ADC_AUXIN_SCAN_ENA, 0);
  485. if (ret) {
  486. dev_err(&pdev->dev, "failed to write IPROC_REGCTL2 %d\n", ret);
  487. return ret;
  488. }
  489. ret = devm_request_threaded_irq(&pdev->dev, adc_priv->irqno,
  490. iproc_adc_interrupt_handler,
  491. iproc_adc_interrupt_thread,
  492. IRQF_SHARED, "iproc-adc", indio_dev);
  493. if (ret) {
  494. dev_err(&pdev->dev, "request_irq error %d\n", ret);
  495. return ret;
  496. }
  497. ret = clk_prepare_enable(adc_priv->adc_clk);
  498. if (ret) {
  499. dev_err(&pdev->dev,
  500. "clk_prepare_enable failed %d\n", ret);
  501. return ret;
  502. }
  503. ret = iproc_adc_enable(indio_dev);
  504. if (ret) {
  505. dev_err(&pdev->dev, "failed to enable adc %d\n", ret);
  506. goto err_adc_enable;
  507. }
  508. indio_dev->name = "iproc-static-adc";
  509. indio_dev->dev.parent = &pdev->dev;
  510. indio_dev->dev.of_node = pdev->dev.of_node;
  511. indio_dev->info = &iproc_adc_iio_info;
  512. indio_dev->modes = INDIO_DIRECT_MODE;
  513. indio_dev->channels = iproc_adc_iio_channels;
  514. indio_dev->num_channels = ARRAY_SIZE(iproc_adc_iio_channels);
  515. ret = iio_device_register(indio_dev);
  516. if (ret) {
  517. dev_err(&pdev->dev, "iio_device_register failed:err %d\n", ret);
  518. goto err_clk;
  519. }
  520. return 0;
  521. err_clk:
  522. iproc_adc_disable(indio_dev);
  523. err_adc_enable:
  524. clk_disable_unprepare(adc_priv->adc_clk);
  525. return ret;
  526. }
  527. static int iproc_adc_remove(struct platform_device *pdev)
  528. {
  529. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  530. struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
  531. iio_device_unregister(indio_dev);
  532. iproc_adc_disable(indio_dev);
  533. clk_disable_unprepare(adc_priv->adc_clk);
  534. return 0;
  535. }
  536. static const struct of_device_id iproc_adc_of_match[] = {
  537. {.compatible = "brcm,iproc-static-adc", },
  538. { },
  539. };
  540. MODULE_DEVICE_TABLE(of, iproc_adc_of_match);
  541. static struct platform_driver iproc_adc_driver = {
  542. .probe = iproc_adc_probe,
  543. .remove = iproc_adc_remove,
  544. .driver = {
  545. .name = "iproc-static-adc",
  546. .of_match_table = of_match_ptr(iproc_adc_of_match),
  547. },
  548. };
  549. module_platform_driver(iproc_adc_driver);
  550. MODULE_DESCRIPTION("Broadcom iProc ADC controller driver");
  551. MODULE_AUTHOR("Raveendra Padasalagi <raveendra.padasalagi@broadcom.com>");
  552. MODULE_LICENSE("GPL v2");