exynos_adc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * exynos_adc.c - Support for ADC in EXYNOS SoCs
  3. *
  4. * 8 ~ 10 channel, 10/12-bit ADC
  5. *
  6. * Copyright (C) 2013 Naveen Krishna Chatradhi <ch.naveen@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/io.h>
  30. #include <linux/clk.h>
  31. #include <linux/completion.h>
  32. #include <linux/of.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/of_platform.h>
  36. #include <linux/err.h>
  37. #include <linux/iio/iio.h>
  38. #include <linux/iio/machine.h>
  39. #include <linux/iio/driver.h>
  40. #include <linux/mfd/syscon.h>
  41. #include <linux/regmap.h>
  42. /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
  43. #define ADC_V1_CON(x) ((x) + 0x00)
  44. #define ADC_V1_DLY(x) ((x) + 0x08)
  45. #define ADC_V1_DATX(x) ((x) + 0x0C)
  46. #define ADC_V1_INTCLR(x) ((x) + 0x18)
  47. #define ADC_V1_MUX(x) ((x) + 0x1c)
  48. /* S3C2410 ADC registers definitions */
  49. #define ADC_S3C2410_MUX(x) ((x) + 0x18)
  50. /* Future ADC_V2 registers definitions */
  51. #define ADC_V2_CON1(x) ((x) + 0x00)
  52. #define ADC_V2_CON2(x) ((x) + 0x04)
  53. #define ADC_V2_STAT(x) ((x) + 0x08)
  54. #define ADC_V2_INT_EN(x) ((x) + 0x10)
  55. #define ADC_V2_INT_ST(x) ((x) + 0x14)
  56. #define ADC_V2_VER(x) ((x) + 0x20)
  57. /* Bit definitions for ADC_V1 */
  58. #define ADC_V1_CON_RES (1u << 16)
  59. #define ADC_V1_CON_PRSCEN (1u << 14)
  60. #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
  61. #define ADC_V1_CON_STANDBY (1u << 2)
  62. /* Bit definitions for S3C2410 ADC */
  63. #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
  64. #define ADC_S3C2410_DATX_MASK 0x3FF
  65. #define ADC_S3C2416_CON_RES_SEL (1u << 3)
  66. /* Bit definitions for ADC_V2 */
  67. #define ADC_V2_CON1_SOFT_RESET (1u << 2)
  68. #define ADC_V2_CON2_OSEL (1u << 10)
  69. #define ADC_V2_CON2_ESEL (1u << 9)
  70. #define ADC_V2_CON2_HIGHF (1u << 8)
  71. #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
  72. #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
  73. #define ADC_V2_CON2_ACH_MASK 0xF
  74. #define MAX_ADC_V2_CHANNELS 10
  75. #define MAX_ADC_V1_CHANNELS 8
  76. #define MAX_EXYNOS3250_ADC_CHANNELS 2
  77. /* Bit definitions common for ADC_V1 and ADC_V2 */
  78. #define ADC_CON_EN_START (1u << 0)
  79. #define ADC_CON_EN_START_MASK (0x3 << 0)
  80. #define ADC_DATX_MASK 0xFFF
  81. #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
  82. #define EXYNOS_ADCV1_PHY_OFFSET 0x0718
  83. #define EXYNOS_ADCV2_PHY_OFFSET 0x0720
  84. struct exynos_adc {
  85. struct exynos_adc_data *data;
  86. struct device *dev;
  87. void __iomem *regs;
  88. struct regmap *pmu_map;
  89. struct clk *clk;
  90. struct clk *sclk;
  91. unsigned int irq;
  92. struct regulator *vdd;
  93. struct completion completion;
  94. u32 value;
  95. unsigned int version;
  96. };
  97. struct exynos_adc_data {
  98. int num_channels;
  99. bool needs_sclk;
  100. bool needs_adc_phy;
  101. int phy_offset;
  102. u32 mask;
  103. void (*init_hw)(struct exynos_adc *info);
  104. void (*exit_hw)(struct exynos_adc *info);
  105. void (*clear_irq)(struct exynos_adc *info);
  106. void (*start_conv)(struct exynos_adc *info, unsigned long addr);
  107. };
  108. static void exynos_adc_unprepare_clk(struct exynos_adc *info)
  109. {
  110. if (info->data->needs_sclk)
  111. clk_unprepare(info->sclk);
  112. clk_unprepare(info->clk);
  113. }
  114. static int exynos_adc_prepare_clk(struct exynos_adc *info)
  115. {
  116. int ret;
  117. ret = clk_prepare(info->clk);
  118. if (ret) {
  119. dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
  120. return ret;
  121. }
  122. if (info->data->needs_sclk) {
  123. ret = clk_prepare(info->sclk);
  124. if (ret) {
  125. clk_unprepare(info->clk);
  126. dev_err(info->dev,
  127. "failed preparing sclk_adc clock: %d\n", ret);
  128. return ret;
  129. }
  130. }
  131. return 0;
  132. }
  133. static void exynos_adc_disable_clk(struct exynos_adc *info)
  134. {
  135. if (info->data->needs_sclk)
  136. clk_disable(info->sclk);
  137. clk_disable(info->clk);
  138. }
  139. static int exynos_adc_enable_clk(struct exynos_adc *info)
  140. {
  141. int ret;
  142. ret = clk_enable(info->clk);
  143. if (ret) {
  144. dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
  145. return ret;
  146. }
  147. if (info->data->needs_sclk) {
  148. ret = clk_enable(info->sclk);
  149. if (ret) {
  150. clk_disable(info->clk);
  151. dev_err(info->dev,
  152. "failed enabling sclk_adc clock: %d\n", ret);
  153. return ret;
  154. }
  155. }
  156. return 0;
  157. }
  158. static void exynos_adc_v1_init_hw(struct exynos_adc *info)
  159. {
  160. u32 con1;
  161. if (info->data->needs_adc_phy)
  162. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  163. /* set default prescaler values and Enable prescaler */
  164. con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
  165. /* Enable 12-bit ADC resolution */
  166. con1 |= ADC_V1_CON_RES;
  167. writel(con1, ADC_V1_CON(info->regs));
  168. }
  169. static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
  170. {
  171. u32 con;
  172. if (info->data->needs_adc_phy)
  173. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  174. con = readl(ADC_V1_CON(info->regs));
  175. con |= ADC_V1_CON_STANDBY;
  176. writel(con, ADC_V1_CON(info->regs));
  177. }
  178. static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
  179. {
  180. writel(1, ADC_V1_INTCLR(info->regs));
  181. }
  182. static void exynos_adc_v1_start_conv(struct exynos_adc *info,
  183. unsigned long addr)
  184. {
  185. u32 con1;
  186. writel(addr, ADC_V1_MUX(info->regs));
  187. con1 = readl(ADC_V1_CON(info->regs));
  188. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  189. }
  190. static const struct exynos_adc_data exynos_adc_v1_data = {
  191. .num_channels = MAX_ADC_V1_CHANNELS,
  192. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  193. .needs_adc_phy = true,
  194. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  195. .init_hw = exynos_adc_v1_init_hw,
  196. .exit_hw = exynos_adc_v1_exit_hw,
  197. .clear_irq = exynos_adc_v1_clear_irq,
  198. .start_conv = exynos_adc_v1_start_conv,
  199. };
  200. static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
  201. unsigned long addr)
  202. {
  203. u32 con1;
  204. /* Enable 12 bit ADC resolution */
  205. con1 = readl(ADC_V1_CON(info->regs));
  206. con1 |= ADC_S3C2416_CON_RES_SEL;
  207. writel(con1, ADC_V1_CON(info->regs));
  208. /* Select channel for S3C2416 */
  209. writel(addr, ADC_S3C2410_MUX(info->regs));
  210. con1 = readl(ADC_V1_CON(info->regs));
  211. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  212. }
  213. static struct exynos_adc_data const exynos_adc_s3c2416_data = {
  214. .num_channels = MAX_ADC_V1_CHANNELS,
  215. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  216. .init_hw = exynos_adc_v1_init_hw,
  217. .exit_hw = exynos_adc_v1_exit_hw,
  218. .start_conv = exynos_adc_s3c2416_start_conv,
  219. };
  220. static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
  221. unsigned long addr)
  222. {
  223. u32 con1;
  224. /* Select channel for S3C2433 */
  225. writel(addr, ADC_S3C2410_MUX(info->regs));
  226. con1 = readl(ADC_V1_CON(info->regs));
  227. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  228. }
  229. static struct exynos_adc_data const exynos_adc_s3c2443_data = {
  230. .num_channels = MAX_ADC_V1_CHANNELS,
  231. .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
  232. .init_hw = exynos_adc_v1_init_hw,
  233. .exit_hw = exynos_adc_v1_exit_hw,
  234. .start_conv = exynos_adc_s3c2443_start_conv,
  235. };
  236. static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
  237. unsigned long addr)
  238. {
  239. u32 con1;
  240. con1 = readl(ADC_V1_CON(info->regs));
  241. con1 &= ~ADC_S3C2410_CON_SELMUX(0x7);
  242. con1 |= ADC_S3C2410_CON_SELMUX(addr);
  243. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  244. }
  245. static struct exynos_adc_data const exynos_adc_s3c24xx_data = {
  246. .num_channels = MAX_ADC_V1_CHANNELS,
  247. .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
  248. .init_hw = exynos_adc_v1_init_hw,
  249. .exit_hw = exynos_adc_v1_exit_hw,
  250. .start_conv = exynos_adc_s3c64xx_start_conv,
  251. };
  252. static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
  253. .num_channels = MAX_ADC_V1_CHANNELS,
  254. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  255. .init_hw = exynos_adc_v1_init_hw,
  256. .exit_hw = exynos_adc_v1_exit_hw,
  257. .clear_irq = exynos_adc_v1_clear_irq,
  258. .start_conv = exynos_adc_s3c64xx_start_conv,
  259. };
  260. static void exynos_adc_v2_init_hw(struct exynos_adc *info)
  261. {
  262. u32 con1, con2;
  263. if (info->data->needs_adc_phy)
  264. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  265. con1 = ADC_V2_CON1_SOFT_RESET;
  266. writel(con1, ADC_V2_CON1(info->regs));
  267. con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
  268. ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
  269. writel(con2, ADC_V2_CON2(info->regs));
  270. /* Enable interrupts */
  271. writel(1, ADC_V2_INT_EN(info->regs));
  272. }
  273. static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
  274. {
  275. u32 con;
  276. if (info->data->needs_adc_phy)
  277. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  278. con = readl(ADC_V2_CON1(info->regs));
  279. con &= ~ADC_CON_EN_START;
  280. writel(con, ADC_V2_CON1(info->regs));
  281. }
  282. static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
  283. {
  284. writel(1, ADC_V2_INT_ST(info->regs));
  285. }
  286. static void exynos_adc_v2_start_conv(struct exynos_adc *info,
  287. unsigned long addr)
  288. {
  289. u32 con1, con2;
  290. con2 = readl(ADC_V2_CON2(info->regs));
  291. con2 &= ~ADC_V2_CON2_ACH_MASK;
  292. con2 |= ADC_V2_CON2_ACH_SEL(addr);
  293. writel(con2, ADC_V2_CON2(info->regs));
  294. con1 = readl(ADC_V2_CON1(info->regs));
  295. writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
  296. }
  297. static const struct exynos_adc_data exynos_adc_v2_data = {
  298. .num_channels = MAX_ADC_V2_CHANNELS,
  299. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  300. .needs_adc_phy = true,
  301. .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
  302. .init_hw = exynos_adc_v2_init_hw,
  303. .exit_hw = exynos_adc_v2_exit_hw,
  304. .clear_irq = exynos_adc_v2_clear_irq,
  305. .start_conv = exynos_adc_v2_start_conv,
  306. };
  307. static const struct exynos_adc_data exynos3250_adc_data = {
  308. .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
  309. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  310. .needs_sclk = true,
  311. .needs_adc_phy = true,
  312. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  313. .init_hw = exynos_adc_v2_init_hw,
  314. .exit_hw = exynos_adc_v2_exit_hw,
  315. .clear_irq = exynos_adc_v2_clear_irq,
  316. .start_conv = exynos_adc_v2_start_conv,
  317. };
  318. static void exynos_adc_exynos7_init_hw(struct exynos_adc *info)
  319. {
  320. u32 con1, con2;
  321. if (info->data->needs_adc_phy)
  322. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  323. con1 = ADC_V2_CON1_SOFT_RESET;
  324. writel(con1, ADC_V2_CON1(info->regs));
  325. con2 = readl(ADC_V2_CON2(info->regs));
  326. con2 &= ~ADC_V2_CON2_C_TIME(7);
  327. con2 |= ADC_V2_CON2_C_TIME(0);
  328. writel(con2, ADC_V2_CON2(info->regs));
  329. /* Enable interrupts */
  330. writel(1, ADC_V2_INT_EN(info->regs));
  331. }
  332. static const struct exynos_adc_data exynos7_adc_data = {
  333. .num_channels = MAX_ADC_V1_CHANNELS,
  334. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  335. .init_hw = exynos_adc_exynos7_init_hw,
  336. .exit_hw = exynos_adc_v2_exit_hw,
  337. .clear_irq = exynos_adc_v2_clear_irq,
  338. .start_conv = exynos_adc_v2_start_conv,
  339. };
  340. static const struct of_device_id exynos_adc_match[] = {
  341. {
  342. .compatible = "samsung,s3c2410-adc",
  343. .data = &exynos_adc_s3c24xx_data,
  344. }, {
  345. .compatible = "samsung,s3c2416-adc",
  346. .data = &exynos_adc_s3c2416_data,
  347. }, {
  348. .compatible = "samsung,s3c2440-adc",
  349. .data = &exynos_adc_s3c24xx_data,
  350. }, {
  351. .compatible = "samsung,s3c2443-adc",
  352. .data = &exynos_adc_s3c2443_data,
  353. }, {
  354. .compatible = "samsung,s3c6410-adc",
  355. .data = &exynos_adc_s3c64xx_data,
  356. }, {
  357. .compatible = "samsung,exynos-adc-v1",
  358. .data = &exynos_adc_v1_data,
  359. }, {
  360. .compatible = "samsung,exynos-adc-v2",
  361. .data = &exynos_adc_v2_data,
  362. }, {
  363. .compatible = "samsung,exynos3250-adc",
  364. .data = &exynos3250_adc_data,
  365. }, {
  366. .compatible = "samsung,exynos7-adc",
  367. .data = &exynos7_adc_data,
  368. },
  369. {},
  370. };
  371. MODULE_DEVICE_TABLE(of, exynos_adc_match);
  372. static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
  373. {
  374. const struct of_device_id *match;
  375. match = of_match_node(exynos_adc_match, pdev->dev.of_node);
  376. return (struct exynos_adc_data *)match->data;
  377. }
  378. static int exynos_read_raw(struct iio_dev *indio_dev,
  379. struct iio_chan_spec const *chan,
  380. int *val,
  381. int *val2,
  382. long mask)
  383. {
  384. struct exynos_adc *info = iio_priv(indio_dev);
  385. unsigned long timeout;
  386. int ret;
  387. if (mask != IIO_CHAN_INFO_RAW)
  388. return -EINVAL;
  389. mutex_lock(&indio_dev->mlock);
  390. reinit_completion(&info->completion);
  391. /* Select the channel to be used and Trigger conversion */
  392. if (info->data->start_conv)
  393. info->data->start_conv(info, chan->address);
  394. timeout = wait_for_completion_timeout
  395. (&info->completion, EXYNOS_ADC_TIMEOUT);
  396. if (timeout == 0) {
  397. dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
  398. if (info->data->init_hw)
  399. info->data->init_hw(info);
  400. ret = -ETIMEDOUT;
  401. } else {
  402. *val = info->value;
  403. *val2 = 0;
  404. ret = IIO_VAL_INT;
  405. }
  406. mutex_unlock(&indio_dev->mlock);
  407. return ret;
  408. }
  409. static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
  410. {
  411. struct exynos_adc *info = (struct exynos_adc *)dev_id;
  412. u32 mask = info->data->mask;
  413. /* Read value */
  414. info->value = readl(ADC_V1_DATX(info->regs)) & mask;
  415. /* clear irq */
  416. if (info->data->clear_irq)
  417. info->data->clear_irq(info);
  418. complete(&info->completion);
  419. return IRQ_HANDLED;
  420. }
  421. static int exynos_adc_reg_access(struct iio_dev *indio_dev,
  422. unsigned reg, unsigned writeval,
  423. unsigned *readval)
  424. {
  425. struct exynos_adc *info = iio_priv(indio_dev);
  426. if (readval == NULL)
  427. return -EINVAL;
  428. *readval = readl(info->regs + reg);
  429. return 0;
  430. }
  431. static const struct iio_info exynos_adc_iio_info = {
  432. .read_raw = &exynos_read_raw,
  433. .debugfs_reg_access = &exynos_adc_reg_access,
  434. .driver_module = THIS_MODULE,
  435. };
  436. #define ADC_CHANNEL(_index, _id) { \
  437. .type = IIO_VOLTAGE, \
  438. .indexed = 1, \
  439. .channel = _index, \
  440. .address = _index, \
  441. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  442. .datasheet_name = _id, \
  443. }
  444. static const struct iio_chan_spec exynos_adc_iio_channels[] = {
  445. ADC_CHANNEL(0, "adc0"),
  446. ADC_CHANNEL(1, "adc1"),
  447. ADC_CHANNEL(2, "adc2"),
  448. ADC_CHANNEL(3, "adc3"),
  449. ADC_CHANNEL(4, "adc4"),
  450. ADC_CHANNEL(5, "adc5"),
  451. ADC_CHANNEL(6, "adc6"),
  452. ADC_CHANNEL(7, "adc7"),
  453. ADC_CHANNEL(8, "adc8"),
  454. ADC_CHANNEL(9, "adc9"),
  455. };
  456. static int exynos_adc_remove_devices(struct device *dev, void *c)
  457. {
  458. struct platform_device *pdev = to_platform_device(dev);
  459. platform_device_unregister(pdev);
  460. return 0;
  461. }
  462. static int exynos_adc_probe(struct platform_device *pdev)
  463. {
  464. struct exynos_adc *info = NULL;
  465. struct device_node *np = pdev->dev.of_node;
  466. struct iio_dev *indio_dev = NULL;
  467. struct resource *mem;
  468. int ret = -ENODEV;
  469. int irq;
  470. if (!np)
  471. return ret;
  472. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
  473. if (!indio_dev) {
  474. dev_err(&pdev->dev, "failed allocating iio device\n");
  475. return -ENOMEM;
  476. }
  477. info = iio_priv(indio_dev);
  478. info->data = exynos_adc_get_data(pdev);
  479. if (!info->data) {
  480. dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
  481. return -EINVAL;
  482. }
  483. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  485. if (IS_ERR(info->regs))
  486. return PTR_ERR(info->regs);
  487. if (info->data->needs_adc_phy) {
  488. info->pmu_map = syscon_regmap_lookup_by_phandle(
  489. pdev->dev.of_node,
  490. "samsung,syscon-phandle");
  491. if (IS_ERR(info->pmu_map)) {
  492. dev_err(&pdev->dev, "syscon regmap lookup failed.\n");
  493. return PTR_ERR(info->pmu_map);
  494. }
  495. }
  496. irq = platform_get_irq(pdev, 0);
  497. if (irq < 0) {
  498. dev_err(&pdev->dev, "no irq resource?\n");
  499. return irq;
  500. }
  501. info->irq = irq;
  502. info->dev = &pdev->dev;
  503. init_completion(&info->completion);
  504. info->clk = devm_clk_get(&pdev->dev, "adc");
  505. if (IS_ERR(info->clk)) {
  506. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  507. PTR_ERR(info->clk));
  508. return PTR_ERR(info->clk);
  509. }
  510. if (info->data->needs_sclk) {
  511. info->sclk = devm_clk_get(&pdev->dev, "sclk");
  512. if (IS_ERR(info->sclk)) {
  513. dev_err(&pdev->dev,
  514. "failed getting sclk clock, err = %ld\n",
  515. PTR_ERR(info->sclk));
  516. return PTR_ERR(info->sclk);
  517. }
  518. }
  519. info->vdd = devm_regulator_get(&pdev->dev, "vdd");
  520. if (IS_ERR(info->vdd)) {
  521. dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
  522. PTR_ERR(info->vdd));
  523. return PTR_ERR(info->vdd);
  524. }
  525. ret = regulator_enable(info->vdd);
  526. if (ret)
  527. return ret;
  528. ret = exynos_adc_prepare_clk(info);
  529. if (ret)
  530. goto err_disable_reg;
  531. ret = exynos_adc_enable_clk(info);
  532. if (ret)
  533. goto err_unprepare_clk;
  534. platform_set_drvdata(pdev, indio_dev);
  535. indio_dev->name = dev_name(&pdev->dev);
  536. indio_dev->dev.parent = &pdev->dev;
  537. indio_dev->dev.of_node = pdev->dev.of_node;
  538. indio_dev->info = &exynos_adc_iio_info;
  539. indio_dev->modes = INDIO_DIRECT_MODE;
  540. indio_dev->channels = exynos_adc_iio_channels;
  541. indio_dev->num_channels = info->data->num_channels;
  542. ret = request_irq(info->irq, exynos_adc_isr,
  543. 0, dev_name(&pdev->dev), info);
  544. if (ret < 0) {
  545. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
  546. info->irq);
  547. goto err_disable_clk;
  548. }
  549. ret = iio_device_register(indio_dev);
  550. if (ret)
  551. goto err_irq;
  552. if (info->data->init_hw)
  553. info->data->init_hw(info);
  554. ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
  555. if (ret < 0) {
  556. dev_err(&pdev->dev, "failed adding child nodes\n");
  557. goto err_of_populate;
  558. }
  559. return 0;
  560. err_of_populate:
  561. device_for_each_child(&indio_dev->dev, NULL,
  562. exynos_adc_remove_devices);
  563. iio_device_unregister(indio_dev);
  564. err_irq:
  565. free_irq(info->irq, info);
  566. err_disable_clk:
  567. if (info->data->exit_hw)
  568. info->data->exit_hw(info);
  569. exynos_adc_disable_clk(info);
  570. err_unprepare_clk:
  571. exynos_adc_unprepare_clk(info);
  572. err_disable_reg:
  573. regulator_disable(info->vdd);
  574. return ret;
  575. }
  576. static int exynos_adc_remove(struct platform_device *pdev)
  577. {
  578. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  579. struct exynos_adc *info = iio_priv(indio_dev);
  580. device_for_each_child(&indio_dev->dev, NULL,
  581. exynos_adc_remove_devices);
  582. iio_device_unregister(indio_dev);
  583. free_irq(info->irq, info);
  584. if (info->data->exit_hw)
  585. info->data->exit_hw(info);
  586. exynos_adc_disable_clk(info);
  587. exynos_adc_unprepare_clk(info);
  588. regulator_disable(info->vdd);
  589. return 0;
  590. }
  591. #ifdef CONFIG_PM_SLEEP
  592. static int exynos_adc_suspend(struct device *dev)
  593. {
  594. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  595. struct exynos_adc *info = iio_priv(indio_dev);
  596. if (info->data->exit_hw)
  597. info->data->exit_hw(info);
  598. exynos_adc_disable_clk(info);
  599. regulator_disable(info->vdd);
  600. return 0;
  601. }
  602. static int exynos_adc_resume(struct device *dev)
  603. {
  604. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  605. struct exynos_adc *info = iio_priv(indio_dev);
  606. int ret;
  607. ret = regulator_enable(info->vdd);
  608. if (ret)
  609. return ret;
  610. ret = exynos_adc_enable_clk(info);
  611. if (ret)
  612. return ret;
  613. if (info->data->init_hw)
  614. info->data->init_hw(info);
  615. return 0;
  616. }
  617. #endif
  618. static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
  619. exynos_adc_suspend,
  620. exynos_adc_resume);
  621. static struct platform_driver exynos_adc_driver = {
  622. .probe = exynos_adc_probe,
  623. .remove = exynos_adc_remove,
  624. .driver = {
  625. .name = "exynos-adc",
  626. .of_match_table = exynos_adc_match,
  627. .pm = &exynos_adc_pm_ops,
  628. },
  629. };
  630. module_platform_driver(exynos_adc_driver);
  631. MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
  632. MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
  633. MODULE_LICENSE("GPL v2");