exynos_adc.c 18 KB

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