imx7d_adc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Freescale i.MX7D ADC driver
  3. *
  4. * Copyright (C) 2015 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/completion.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/driver.h>
  22. #include <linux/iio/sysfs.h>
  23. /* ADC register */
  24. #define IMX7D_REG_ADC_CH_A_CFG1 0x00
  25. #define IMX7D_REG_ADC_CH_A_CFG2 0x10
  26. #define IMX7D_REG_ADC_CH_B_CFG1 0x20
  27. #define IMX7D_REG_ADC_CH_B_CFG2 0x30
  28. #define IMX7D_REG_ADC_CH_C_CFG1 0x40
  29. #define IMX7D_REG_ADC_CH_C_CFG2 0x50
  30. #define IMX7D_REG_ADC_CH_D_CFG1 0x60
  31. #define IMX7D_REG_ADC_CH_D_CFG2 0x70
  32. #define IMX7D_REG_ADC_CH_SW_CFG 0x80
  33. #define IMX7D_REG_ADC_TIMER_UNIT 0x90
  34. #define IMX7D_REG_ADC_DMA_FIFO 0xa0
  35. #define IMX7D_REG_ADC_FIFO_STATUS 0xb0
  36. #define IMX7D_REG_ADC_INT_SIG_EN 0xc0
  37. #define IMX7D_REG_ADC_INT_EN 0xd0
  38. #define IMX7D_REG_ADC_INT_STATUS 0xe0
  39. #define IMX7D_REG_ADC_CHA_B_CNV_RSLT 0xf0
  40. #define IMX7D_REG_ADC_CHC_D_CNV_RSLT 0x100
  41. #define IMX7D_REG_ADC_CH_SW_CNV_RSLT 0x110
  42. #define IMX7D_REG_ADC_DMA_FIFO_DAT 0x120
  43. #define IMX7D_REG_ADC_ADC_CFG 0x130
  44. #define IMX7D_REG_ADC_CHANNEL_CFG2_BASE 0x10
  45. #define IMX7D_EACH_CHANNEL_REG_OFFSET 0x20
  46. #define IMX7D_REG_ADC_CH_CFG1_CHANNEL_EN (0x1 << 31)
  47. #define IMX7D_REG_ADC_CH_CFG1_CHANNEL_SINGLE BIT(30)
  48. #define IMX7D_REG_ADC_CH_CFG1_CHANNEL_AVG_EN BIT(29)
  49. #define IMX7D_REG_ADC_CH_CFG1_CHANNEL_SEL(x) ((x) << 24)
  50. #define IMX7D_REG_ADC_CH_CFG2_AVG_NUM_4 (0x0 << 12)
  51. #define IMX7D_REG_ADC_CH_CFG2_AVG_NUM_8 (0x1 << 12)
  52. #define IMX7D_REG_ADC_CH_CFG2_AVG_NUM_16 (0x2 << 12)
  53. #define IMX7D_REG_ADC_CH_CFG2_AVG_NUM_32 (0x3 << 12)
  54. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_4 (0x0 << 29)
  55. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_8 (0x1 << 29)
  56. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_16 (0x2 << 29)
  57. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_32 (0x3 << 29)
  58. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_64 (0x4 << 29)
  59. #define IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_128 (0x5 << 29)
  60. #define IMX7D_REG_ADC_ADC_CFG_ADC_CLK_DOWN BIT(31)
  61. #define IMX7D_REG_ADC_ADC_CFG_ADC_POWER_DOWN BIT(1)
  62. #define IMX7D_REG_ADC_ADC_CFG_ADC_EN BIT(0)
  63. #define IMX7D_REG_ADC_INT_CHA_COV_INT_EN BIT(8)
  64. #define IMX7D_REG_ADC_INT_CHB_COV_INT_EN BIT(9)
  65. #define IMX7D_REG_ADC_INT_CHC_COV_INT_EN BIT(10)
  66. #define IMX7D_REG_ADC_INT_CHD_COV_INT_EN BIT(11)
  67. #define IMX7D_REG_ADC_INT_CHANNEL_INT_EN \
  68. (IMX7D_REG_ADC_INT_CHA_COV_INT_EN | \
  69. IMX7D_REG_ADC_INT_CHB_COV_INT_EN | \
  70. IMX7D_REG_ADC_INT_CHC_COV_INT_EN | \
  71. IMX7D_REG_ADC_INT_CHD_COV_INT_EN)
  72. #define IMX7D_REG_ADC_INT_STATUS_CHANNEL_INT_STATUS 0xf00
  73. #define IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT 0xf0000
  74. #define IMX7D_ADC_TIMEOUT msecs_to_jiffies(100)
  75. enum imx7d_adc_clk_pre_div {
  76. IMX7D_ADC_ANALOG_CLK_PRE_DIV_4,
  77. IMX7D_ADC_ANALOG_CLK_PRE_DIV_8,
  78. IMX7D_ADC_ANALOG_CLK_PRE_DIV_16,
  79. IMX7D_ADC_ANALOG_CLK_PRE_DIV_32,
  80. IMX7D_ADC_ANALOG_CLK_PRE_DIV_64,
  81. IMX7D_ADC_ANALOG_CLK_PRE_DIV_128,
  82. };
  83. enum imx7d_adc_average_num {
  84. IMX7D_ADC_AVERAGE_NUM_4,
  85. IMX7D_ADC_AVERAGE_NUM_8,
  86. IMX7D_ADC_AVERAGE_NUM_16,
  87. IMX7D_ADC_AVERAGE_NUM_32,
  88. };
  89. struct imx7d_adc_feature {
  90. enum imx7d_adc_clk_pre_div clk_pre_div;
  91. enum imx7d_adc_average_num avg_num;
  92. u32 core_time_unit; /* impact the sample rate */
  93. bool average_en;
  94. };
  95. struct imx7d_adc {
  96. struct device *dev;
  97. void __iomem *regs;
  98. struct clk *clk;
  99. u32 vref_uv;
  100. u32 value;
  101. u32 channel;
  102. u32 pre_div_num;
  103. struct regulator *vref;
  104. struct imx7d_adc_feature adc_feature;
  105. struct completion completion;
  106. };
  107. struct imx7d_adc_analogue_core_clk {
  108. u32 pre_div;
  109. u32 reg_config;
  110. };
  111. #define IMX7D_ADC_ANALOGUE_CLK_CONFIG(_pre_div, _reg_conf) { \
  112. .pre_div = (_pre_div), \
  113. .reg_config = (_reg_conf), \
  114. }
  115. static const struct imx7d_adc_analogue_core_clk imx7d_adc_analogue_clk[] = {
  116. IMX7D_ADC_ANALOGUE_CLK_CONFIG(4, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_4),
  117. IMX7D_ADC_ANALOGUE_CLK_CONFIG(8, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_8),
  118. IMX7D_ADC_ANALOGUE_CLK_CONFIG(16, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_16),
  119. IMX7D_ADC_ANALOGUE_CLK_CONFIG(32, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_32),
  120. IMX7D_ADC_ANALOGUE_CLK_CONFIG(64, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_64),
  121. IMX7D_ADC_ANALOGUE_CLK_CONFIG(128, IMX7D_REG_ADC_TIMER_UNIT_PRE_DIV_128),
  122. };
  123. #define IMX7D_ADC_CHAN(_idx) { \
  124. .type = IIO_VOLTAGE, \
  125. .indexed = 1, \
  126. .channel = (_idx), \
  127. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  128. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  129. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  130. }
  131. static const struct iio_chan_spec imx7d_adc_iio_channels[] = {
  132. IMX7D_ADC_CHAN(0),
  133. IMX7D_ADC_CHAN(1),
  134. IMX7D_ADC_CHAN(2),
  135. IMX7D_ADC_CHAN(3),
  136. IMX7D_ADC_CHAN(4),
  137. IMX7D_ADC_CHAN(5),
  138. IMX7D_ADC_CHAN(6),
  139. IMX7D_ADC_CHAN(7),
  140. IMX7D_ADC_CHAN(8),
  141. IMX7D_ADC_CHAN(9),
  142. IMX7D_ADC_CHAN(10),
  143. IMX7D_ADC_CHAN(11),
  144. IMX7D_ADC_CHAN(12),
  145. IMX7D_ADC_CHAN(13),
  146. IMX7D_ADC_CHAN(14),
  147. IMX7D_ADC_CHAN(15),
  148. };
  149. static const u32 imx7d_adc_average_num[] = {
  150. IMX7D_REG_ADC_CH_CFG2_AVG_NUM_4,
  151. IMX7D_REG_ADC_CH_CFG2_AVG_NUM_8,
  152. IMX7D_REG_ADC_CH_CFG2_AVG_NUM_16,
  153. IMX7D_REG_ADC_CH_CFG2_AVG_NUM_32,
  154. };
  155. static void imx7d_adc_feature_config(struct imx7d_adc *info)
  156. {
  157. info->adc_feature.clk_pre_div = IMX7D_ADC_ANALOG_CLK_PRE_DIV_4;
  158. info->adc_feature.avg_num = IMX7D_ADC_AVERAGE_NUM_32;
  159. info->adc_feature.core_time_unit = 1;
  160. info->adc_feature.average_en = true;
  161. }
  162. static void imx7d_adc_sample_rate_set(struct imx7d_adc *info)
  163. {
  164. struct imx7d_adc_feature *adc_feature = &info->adc_feature;
  165. struct imx7d_adc_analogue_core_clk adc_analogure_clk;
  166. u32 i;
  167. u32 tmp_cfg1;
  168. u32 sample_rate = 0;
  169. /*
  170. * Before sample set, disable channel A,B,C,D. Here we
  171. * clear the bit 31 of register REG_ADC_CH_A\B\C\D_CFG1.
  172. */
  173. for (i = 0; i < 4; i++) {
  174. tmp_cfg1 =
  175. readl(info->regs + i * IMX7D_EACH_CHANNEL_REG_OFFSET);
  176. tmp_cfg1 &= ~IMX7D_REG_ADC_CH_CFG1_CHANNEL_EN;
  177. writel(tmp_cfg1,
  178. info->regs + i * IMX7D_EACH_CHANNEL_REG_OFFSET);
  179. }
  180. adc_analogure_clk = imx7d_adc_analogue_clk[adc_feature->clk_pre_div];
  181. sample_rate |= adc_analogure_clk.reg_config;
  182. info->pre_div_num = adc_analogure_clk.pre_div;
  183. sample_rate |= adc_feature->core_time_unit;
  184. writel(sample_rate, info->regs + IMX7D_REG_ADC_TIMER_UNIT);
  185. }
  186. static void imx7d_adc_hw_init(struct imx7d_adc *info)
  187. {
  188. u32 cfg;
  189. /* power up and enable adc analogue core */
  190. cfg = readl(info->regs + IMX7D_REG_ADC_ADC_CFG);
  191. cfg &= ~(IMX7D_REG_ADC_ADC_CFG_ADC_CLK_DOWN |
  192. IMX7D_REG_ADC_ADC_CFG_ADC_POWER_DOWN);
  193. cfg |= IMX7D_REG_ADC_ADC_CFG_ADC_EN;
  194. writel(cfg, info->regs + IMX7D_REG_ADC_ADC_CFG);
  195. /* enable channel A,B,C,D interrupt */
  196. writel(IMX7D_REG_ADC_INT_CHANNEL_INT_EN,
  197. info->regs + IMX7D_REG_ADC_INT_SIG_EN);
  198. writel(IMX7D_REG_ADC_INT_CHANNEL_INT_EN,
  199. info->regs + IMX7D_REG_ADC_INT_EN);
  200. imx7d_adc_sample_rate_set(info);
  201. }
  202. static void imx7d_adc_channel_set(struct imx7d_adc *info)
  203. {
  204. u32 cfg1 = 0;
  205. u32 cfg2;
  206. u32 channel;
  207. channel = info->channel;
  208. /* the channel choose single conversion, and enable average mode */
  209. cfg1 |= (IMX7D_REG_ADC_CH_CFG1_CHANNEL_EN |
  210. IMX7D_REG_ADC_CH_CFG1_CHANNEL_SINGLE);
  211. if (info->adc_feature.average_en)
  212. cfg1 |= IMX7D_REG_ADC_CH_CFG1_CHANNEL_AVG_EN;
  213. /*
  214. * physical channel 0 chose logical channel A
  215. * physical channel 1 chose logical channel B
  216. * physical channel 2 chose logical channel C
  217. * physical channel 3 chose logical channel D
  218. */
  219. cfg1 |= IMX7D_REG_ADC_CH_CFG1_CHANNEL_SEL(channel);
  220. /*
  221. * read register REG_ADC_CH_A\B\C\D_CFG2, according to the
  222. * channel chosen
  223. */
  224. cfg2 = readl(info->regs + IMX7D_EACH_CHANNEL_REG_OFFSET * channel +
  225. IMX7D_REG_ADC_CHANNEL_CFG2_BASE);
  226. cfg2 |= imx7d_adc_average_num[info->adc_feature.avg_num];
  227. /*
  228. * write the register REG_ADC_CH_A\B\C\D_CFG2, according to
  229. * the channel chosen
  230. */
  231. writel(cfg2, info->regs + IMX7D_EACH_CHANNEL_REG_OFFSET * channel +
  232. IMX7D_REG_ADC_CHANNEL_CFG2_BASE);
  233. writel(cfg1, info->regs + IMX7D_EACH_CHANNEL_REG_OFFSET * channel);
  234. }
  235. static u32 imx7d_adc_get_sample_rate(struct imx7d_adc *info)
  236. {
  237. /* input clock is always 24MHz */
  238. u32 input_clk = 24000000;
  239. u32 analogue_core_clk;
  240. u32 core_time_unit = info->adc_feature.core_time_unit;
  241. u32 tmp;
  242. analogue_core_clk = input_clk / info->pre_div_num;
  243. tmp = (core_time_unit + 1) * 6;
  244. return analogue_core_clk / tmp;
  245. }
  246. static int imx7d_adc_read_raw(struct iio_dev *indio_dev,
  247. struct iio_chan_spec const *chan,
  248. int *val,
  249. int *val2,
  250. long mask)
  251. {
  252. struct imx7d_adc *info = iio_priv(indio_dev);
  253. u32 channel;
  254. long ret;
  255. switch (mask) {
  256. case IIO_CHAN_INFO_RAW:
  257. mutex_lock(&indio_dev->mlock);
  258. reinit_completion(&info->completion);
  259. channel = chan->channel & 0x03;
  260. info->channel = channel;
  261. imx7d_adc_channel_set(info);
  262. ret = wait_for_completion_interruptible_timeout
  263. (&info->completion, IMX7D_ADC_TIMEOUT);
  264. if (ret == 0) {
  265. mutex_unlock(&indio_dev->mlock);
  266. return -ETIMEDOUT;
  267. }
  268. if (ret < 0) {
  269. mutex_unlock(&indio_dev->mlock);
  270. return ret;
  271. }
  272. *val = info->value;
  273. mutex_unlock(&indio_dev->mlock);
  274. return IIO_VAL_INT;
  275. case IIO_CHAN_INFO_SCALE:
  276. info->vref_uv = regulator_get_voltage(info->vref);
  277. *val = info->vref_uv / 1000;
  278. *val2 = 12;
  279. return IIO_VAL_FRACTIONAL_LOG2;
  280. case IIO_CHAN_INFO_SAMP_FREQ:
  281. *val = imx7d_adc_get_sample_rate(info);
  282. return IIO_VAL_INT;
  283. default:
  284. return -EINVAL;
  285. }
  286. }
  287. static int imx7d_adc_read_data(struct imx7d_adc *info)
  288. {
  289. u32 channel;
  290. u32 value;
  291. channel = info->channel & 0x03;
  292. /*
  293. * channel A and B conversion result share one register,
  294. * bit[27~16] is the channel B conversion result,
  295. * bit[11~0] is the channel A conversion result.
  296. * channel C and D is the same.
  297. */
  298. if (channel < 2)
  299. value = readl(info->regs + IMX7D_REG_ADC_CHA_B_CNV_RSLT);
  300. else
  301. value = readl(info->regs + IMX7D_REG_ADC_CHC_D_CNV_RSLT);
  302. if (channel & 0x1) /* channel B or D */
  303. value = (value >> 16) & 0xFFF;
  304. else /* channel A or C */
  305. value &= 0xFFF;
  306. return value;
  307. }
  308. static irqreturn_t imx7d_adc_isr(int irq, void *dev_id)
  309. {
  310. struct imx7d_adc *info = (struct imx7d_adc *)dev_id;
  311. int status;
  312. status = readl(info->regs + IMX7D_REG_ADC_INT_STATUS);
  313. if (status & IMX7D_REG_ADC_INT_STATUS_CHANNEL_INT_STATUS) {
  314. info->value = imx7d_adc_read_data(info);
  315. complete(&info->completion);
  316. /*
  317. * The register IMX7D_REG_ADC_INT_STATUS can't clear
  318. * itself after read operation, need software to write
  319. * 0 to the related bit. Here we clear the channel A/B/C/D
  320. * conversion finished flag.
  321. */
  322. status &= ~IMX7D_REG_ADC_INT_STATUS_CHANNEL_INT_STATUS;
  323. writel(status, info->regs + IMX7D_REG_ADC_INT_STATUS);
  324. }
  325. /*
  326. * If the channel A/B/C/D conversion timeout, report it and clear these
  327. * timeout flags.
  328. */
  329. if (status & IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT) {
  330. pr_err("%s: ADC got conversion time out interrupt: 0x%08x\n",
  331. dev_name(info->dev), status);
  332. status &= ~IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT;
  333. writel(status, info->regs + IMX7D_REG_ADC_INT_STATUS);
  334. }
  335. return IRQ_HANDLED;
  336. }
  337. static int imx7d_adc_reg_access(struct iio_dev *indio_dev,
  338. unsigned reg, unsigned writeval,
  339. unsigned *readval)
  340. {
  341. struct imx7d_adc *info = iio_priv(indio_dev);
  342. if (!readval || reg % 4 || reg > IMX7D_REG_ADC_ADC_CFG)
  343. return -EINVAL;
  344. *readval = readl(info->regs + reg);
  345. return 0;
  346. }
  347. static const struct iio_info imx7d_adc_iio_info = {
  348. .driver_module = THIS_MODULE,
  349. .read_raw = &imx7d_adc_read_raw,
  350. .debugfs_reg_access = &imx7d_adc_reg_access,
  351. };
  352. static const struct of_device_id imx7d_adc_match[] = {
  353. { .compatible = "fsl,imx7d-adc", },
  354. { /* sentinel */ }
  355. };
  356. MODULE_DEVICE_TABLE(of, imx7d_adc_match);
  357. static void imx7d_adc_power_down(struct imx7d_adc *info)
  358. {
  359. u32 adc_cfg;
  360. adc_cfg = readl(info->regs + IMX7D_REG_ADC_ADC_CFG);
  361. adc_cfg |= IMX7D_REG_ADC_ADC_CFG_ADC_CLK_DOWN |
  362. IMX7D_REG_ADC_ADC_CFG_ADC_POWER_DOWN;
  363. adc_cfg &= ~IMX7D_REG_ADC_ADC_CFG_ADC_EN;
  364. writel(adc_cfg, info->regs + IMX7D_REG_ADC_ADC_CFG);
  365. }
  366. static int imx7d_adc_probe(struct platform_device *pdev)
  367. {
  368. struct imx7d_adc *info;
  369. struct iio_dev *indio_dev;
  370. struct resource *mem;
  371. int irq;
  372. int ret;
  373. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  374. if (!indio_dev) {
  375. dev_err(&pdev->dev, "Failed allocating iio device\n");
  376. return -ENOMEM;
  377. }
  378. info = iio_priv(indio_dev);
  379. info->dev = &pdev->dev;
  380. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  381. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  382. if (IS_ERR(info->regs)) {
  383. ret = PTR_ERR(info->regs);
  384. dev_err(&pdev->dev,
  385. "Failed to remap adc memory, err = %d\n", ret);
  386. return ret;
  387. }
  388. irq = platform_get_irq(pdev, 0);
  389. if (irq < 0) {
  390. dev_err(&pdev->dev, "No irq resource?\n");
  391. return irq;
  392. }
  393. info->clk = devm_clk_get(&pdev->dev, "adc");
  394. if (IS_ERR(info->clk)) {
  395. ret = PTR_ERR(info->clk);
  396. dev_err(&pdev->dev, "Failed getting clock, err = %d\n", ret);
  397. return ret;
  398. }
  399. info->vref = devm_regulator_get(&pdev->dev, "vref");
  400. if (IS_ERR(info->vref)) {
  401. ret = PTR_ERR(info->vref);
  402. dev_err(&pdev->dev,
  403. "Failed getting reference voltage, err = %d\n", ret);
  404. return ret;
  405. }
  406. ret = regulator_enable(info->vref);
  407. if (ret) {
  408. dev_err(&pdev->dev,
  409. "Can't enable adc reference top voltage, err = %d\n",
  410. ret);
  411. return ret;
  412. }
  413. platform_set_drvdata(pdev, indio_dev);
  414. init_completion(&info->completion);
  415. indio_dev->name = dev_name(&pdev->dev);
  416. indio_dev->dev.parent = &pdev->dev;
  417. indio_dev->info = &imx7d_adc_iio_info;
  418. indio_dev->modes = INDIO_DIRECT_MODE;
  419. indio_dev->channels = imx7d_adc_iio_channels;
  420. indio_dev->num_channels = ARRAY_SIZE(imx7d_adc_iio_channels);
  421. ret = clk_prepare_enable(info->clk);
  422. if (ret) {
  423. dev_err(&pdev->dev,
  424. "Could not prepare or enable the clock.\n");
  425. goto error_adc_clk_enable;
  426. }
  427. ret = devm_request_irq(info->dev, irq,
  428. imx7d_adc_isr, 0,
  429. dev_name(&pdev->dev), info);
  430. if (ret < 0) {
  431. dev_err(&pdev->dev, "Failed requesting irq, irq = %d\n", irq);
  432. goto error_iio_device_register;
  433. }
  434. imx7d_adc_feature_config(info);
  435. imx7d_adc_hw_init(info);
  436. ret = iio_device_register(indio_dev);
  437. if (ret) {
  438. imx7d_adc_power_down(info);
  439. dev_err(&pdev->dev, "Couldn't register the device.\n");
  440. goto error_iio_device_register;
  441. }
  442. return 0;
  443. error_iio_device_register:
  444. clk_disable_unprepare(info->clk);
  445. error_adc_clk_enable:
  446. regulator_disable(info->vref);
  447. return ret;
  448. }
  449. static int imx7d_adc_remove(struct platform_device *pdev)
  450. {
  451. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  452. struct imx7d_adc *info = iio_priv(indio_dev);
  453. iio_device_unregister(indio_dev);
  454. imx7d_adc_power_down(info);
  455. clk_disable_unprepare(info->clk);
  456. regulator_disable(info->vref);
  457. return 0;
  458. }
  459. static int __maybe_unused imx7d_adc_suspend(struct device *dev)
  460. {
  461. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  462. struct imx7d_adc *info = iio_priv(indio_dev);
  463. imx7d_adc_power_down(info);
  464. clk_disable_unprepare(info->clk);
  465. regulator_disable(info->vref);
  466. return 0;
  467. }
  468. static int __maybe_unused imx7d_adc_resume(struct device *dev)
  469. {
  470. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  471. struct imx7d_adc *info = iio_priv(indio_dev);
  472. int ret;
  473. ret = regulator_enable(info->vref);
  474. if (ret) {
  475. dev_err(info->dev,
  476. "Can't enable adc reference top voltage, err = %d\n",
  477. ret);
  478. return ret;
  479. }
  480. ret = clk_prepare_enable(info->clk);
  481. if (ret) {
  482. dev_err(info->dev,
  483. "Could not prepare or enable clock.\n");
  484. regulator_disable(info->vref);
  485. return ret;
  486. }
  487. imx7d_adc_hw_init(info);
  488. return 0;
  489. }
  490. static SIMPLE_DEV_PM_OPS(imx7d_adc_pm_ops, imx7d_adc_suspend, imx7d_adc_resume);
  491. static struct platform_driver imx7d_adc_driver = {
  492. .probe = imx7d_adc_probe,
  493. .remove = imx7d_adc_remove,
  494. .driver = {
  495. .name = "imx7d_adc",
  496. .of_match_table = imx7d_adc_match,
  497. .pm = &imx7d_adc_pm_ops,
  498. },
  499. };
  500. module_platform_driver(imx7d_adc_driver);
  501. MODULE_AUTHOR("Haibo Chen <haibo.chen@freescale.com>");
  502. MODULE_DESCRIPTION("Freeacale IMX7D ADC driver");
  503. MODULE_LICENSE("GPL v2");