exynos_adc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * exynos_adc.c - Support for ADC in EXYNOS SoCs
  4. *
  5. * 8 ~ 10 channel, 10/12-bit ADC
  6. *
  7. * Copyright (C) 2013 Naveen Krishna Chatradhi <ch.naveen@samsung.com>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/io.h>
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/of.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/err.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/machine.h>
  26. #include <linux/iio/driver.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/regmap.h>
  29. /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
  30. #define ADC_V1_CON(x) ((x) + 0x00)
  31. #define ADC_V1_DLY(x) ((x) + 0x08)
  32. #define ADC_V1_DATX(x) ((x) + 0x0C)
  33. #define ADC_V1_DATY(x) ((x) + 0x10)
  34. #define ADC_V1_UPDN(x) ((x) + 0x14)
  35. #define ADC_V1_INTCLR(x) ((x) + 0x18)
  36. #define ADC_V1_MUX(x) ((x) + 0x1c)
  37. /* Future ADC_V2 registers definitions */
  38. #define ADC_V2_CON1(x) ((x) + 0x00)
  39. #define ADC_V2_CON2(x) ((x) + 0x04)
  40. #define ADC_V2_STAT(x) ((x) + 0x08)
  41. #define ADC_V2_INT_EN(x) ((x) + 0x10)
  42. #define ADC_V2_INT_ST(x) ((x) + 0x14)
  43. #define ADC_V2_VER(x) ((x) + 0x20)
  44. /* Bit definitions for ADC_V1 */
  45. #define ADC_V1_CON_RES (1u << 16)
  46. #define ADC_V1_CON_PRSCEN (1u << 14)
  47. #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
  48. #define ADC_V1_CON_STANDBY (1u << 2)
  49. /* Bit definitions for S3C2410 / S3C6410 ADC */
  50. #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
  51. /* ADCTSC Register Bits */
  52. #define ADC_S3C2443_TSC_UD_SEN (1u << 8)
  53. #define ADC_S3C2410_TSC_YM_SEN (1u << 7)
  54. #define ADC_S3C2410_TSC_YP_SEN (1u << 6)
  55. #define ADC_S3C2410_TSC_XM_SEN (1u << 5)
  56. #define ADC_S3C2410_TSC_XP_SEN (1u << 4)
  57. #define ADC_S3C2410_TSC_XY_PST(x) (((x) & 0x3) << 0)
  58. #define ADC_TSC_WAIT4INT (ADC_S3C2410_TSC_YM_SEN | \
  59. ADC_S3C2410_TSC_YP_SEN | \
  60. ADC_S3C2410_TSC_XP_SEN | \
  61. ADC_S3C2410_TSC_XY_PST(3))
  62. /* Bit definitions for ADC_V2 */
  63. #define ADC_V2_CON1_SOFT_RESET (1u << 2)
  64. #define ADC_V2_CON2_OSEL (1u << 10)
  65. #define ADC_V2_CON2_ESEL (1u << 9)
  66. #define ADC_V2_CON2_HIGHF (1u << 8)
  67. #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
  68. #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
  69. #define ADC_V2_CON2_ACH_MASK 0xF
  70. #define MAX_ADC_V2_CHANNELS 10
  71. #define MAX_ADC_V1_CHANNELS 8
  72. #define MAX_EXYNOS3250_ADC_CHANNELS 2
  73. #define MAX_EXYNOS4212_ADC_CHANNELS 4
  74. #define MAX_S5PV210_ADC_CHANNELS 10
  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_PRESSED (1u << 15)
  79. #define ADC_DATX_MASK 0xFFF
  80. #define ADC_DATY_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. * Lock to protect from potential concurrent access to the
  98. * completion callback during a manual conversion. For this driver
  99. * a wait-callback is used to wait for the conversion result,
  100. * so in the meantime no other read request (or conversion start)
  101. * must be performed, otherwise it would interfere with the
  102. * current conversion result.
  103. */
  104. struct mutex lock;
  105. };
  106. struct exynos_adc_data {
  107. int num_channels;
  108. bool needs_sclk;
  109. bool needs_adc_phy;
  110. int phy_offset;
  111. u32 mask;
  112. void (*init_hw)(struct exynos_adc *info);
  113. void (*exit_hw)(struct exynos_adc *info);
  114. void (*clear_irq)(struct exynos_adc *info);
  115. void (*start_conv)(struct exynos_adc *info, unsigned long addr);
  116. };
  117. static void exynos_adc_unprepare_clk(struct exynos_adc *info)
  118. {
  119. if (info->data->needs_sclk)
  120. clk_unprepare(info->sclk);
  121. clk_unprepare(info->clk);
  122. }
  123. static int exynos_adc_prepare_clk(struct exynos_adc *info)
  124. {
  125. int ret;
  126. ret = clk_prepare(info->clk);
  127. if (ret) {
  128. dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
  129. return ret;
  130. }
  131. if (info->data->needs_sclk) {
  132. ret = clk_prepare(info->sclk);
  133. if (ret) {
  134. clk_unprepare(info->clk);
  135. dev_err(info->dev,
  136. "failed preparing sclk_adc clock: %d\n", ret);
  137. return ret;
  138. }
  139. }
  140. return 0;
  141. }
  142. static void exynos_adc_disable_clk(struct exynos_adc *info)
  143. {
  144. if (info->data->needs_sclk)
  145. clk_disable(info->sclk);
  146. clk_disable(info->clk);
  147. }
  148. static int exynos_adc_enable_clk(struct exynos_adc *info)
  149. {
  150. int ret;
  151. ret = clk_enable(info->clk);
  152. if (ret) {
  153. dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
  154. return ret;
  155. }
  156. if (info->data->needs_sclk) {
  157. ret = clk_enable(info->sclk);
  158. if (ret) {
  159. clk_disable(info->clk);
  160. dev_err(info->dev,
  161. "failed enabling sclk_adc clock: %d\n", ret);
  162. return ret;
  163. }
  164. }
  165. return 0;
  166. }
  167. static void exynos_adc_v1_init_hw(struct exynos_adc *info)
  168. {
  169. u32 con1;
  170. if (info->data->needs_adc_phy)
  171. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  172. /* set default prescaler values and Enable prescaler */
  173. con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
  174. /* Enable 12-bit ADC resolution */
  175. con1 |= ADC_V1_CON_RES;
  176. writel(con1, ADC_V1_CON(info->regs));
  177. /* set touchscreen delay */
  178. writel(10000, ADC_V1_DLY(info->regs));
  179. }
  180. static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
  181. {
  182. u32 con;
  183. if (info->data->needs_adc_phy)
  184. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  185. con = readl(ADC_V1_CON(info->regs));
  186. con |= ADC_V1_CON_STANDBY;
  187. writel(con, ADC_V1_CON(info->regs));
  188. }
  189. static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
  190. {
  191. writel(1, ADC_V1_INTCLR(info->regs));
  192. }
  193. static void exynos_adc_v1_start_conv(struct exynos_adc *info,
  194. unsigned long addr)
  195. {
  196. u32 con1;
  197. writel(addr, ADC_V1_MUX(info->regs));
  198. con1 = readl(ADC_V1_CON(info->regs));
  199. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  200. }
  201. /* Exynos4212 and 4412 is like ADCv1 but with four channels only */
  202. static const struct exynos_adc_data exynos4212_adc_data = {
  203. .num_channels = MAX_EXYNOS4212_ADC_CHANNELS,
  204. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  205. .needs_adc_phy = true,
  206. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  207. .init_hw = exynos_adc_v1_init_hw,
  208. .exit_hw = exynos_adc_v1_exit_hw,
  209. .clear_irq = exynos_adc_v1_clear_irq,
  210. .start_conv = exynos_adc_v1_start_conv,
  211. };
  212. static const struct exynos_adc_data exynos_adc_v1_data = {
  213. .num_channels = MAX_ADC_V1_CHANNELS,
  214. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  215. .needs_adc_phy = true,
  216. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  217. .init_hw = exynos_adc_v1_init_hw,
  218. .exit_hw = exynos_adc_v1_exit_hw,
  219. .clear_irq = exynos_adc_v1_clear_irq,
  220. .start_conv = exynos_adc_v1_start_conv,
  221. };
  222. static const struct exynos_adc_data exynos_adc_s5pv210_data = {
  223. .num_channels = MAX_S5PV210_ADC_CHANNELS,
  224. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  225. .init_hw = exynos_adc_v1_init_hw,
  226. .exit_hw = exynos_adc_v1_exit_hw,
  227. .clear_irq = exynos_adc_v1_clear_irq,
  228. .start_conv = exynos_adc_v1_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_s3c64xx_data = {
  240. .num_channels = MAX_ADC_V1_CHANNELS,
  241. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  242. .init_hw = exynos_adc_v1_init_hw,
  243. .exit_hw = exynos_adc_v1_exit_hw,
  244. .clear_irq = exynos_adc_v1_clear_irq,
  245. .start_conv = exynos_adc_s3c64xx_start_conv,
  246. };
  247. static void exynos_adc_v2_init_hw(struct exynos_adc *info)
  248. {
  249. u32 con1, con2;
  250. if (info->data->needs_adc_phy)
  251. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  252. con1 = ADC_V2_CON1_SOFT_RESET;
  253. writel(con1, ADC_V2_CON1(info->regs));
  254. con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
  255. ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
  256. writel(con2, ADC_V2_CON2(info->regs));
  257. /* Enable interrupts */
  258. writel(1, ADC_V2_INT_EN(info->regs));
  259. }
  260. static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
  261. {
  262. u32 con;
  263. if (info->data->needs_adc_phy)
  264. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  265. con = readl(ADC_V2_CON1(info->regs));
  266. con &= ~ADC_CON_EN_START;
  267. writel(con, ADC_V2_CON1(info->regs));
  268. }
  269. static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
  270. {
  271. writel(1, ADC_V2_INT_ST(info->regs));
  272. }
  273. static void exynos_adc_v2_start_conv(struct exynos_adc *info,
  274. unsigned long addr)
  275. {
  276. u32 con1, con2;
  277. con2 = readl(ADC_V2_CON2(info->regs));
  278. con2 &= ~ADC_V2_CON2_ACH_MASK;
  279. con2 |= ADC_V2_CON2_ACH_SEL(addr);
  280. writel(con2, ADC_V2_CON2(info->regs));
  281. con1 = readl(ADC_V2_CON1(info->regs));
  282. writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
  283. }
  284. static const struct exynos_adc_data exynos_adc_v2_data = {
  285. .num_channels = MAX_ADC_V2_CHANNELS,
  286. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  287. .needs_adc_phy = true,
  288. .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
  289. .init_hw = exynos_adc_v2_init_hw,
  290. .exit_hw = exynos_adc_v2_exit_hw,
  291. .clear_irq = exynos_adc_v2_clear_irq,
  292. .start_conv = exynos_adc_v2_start_conv,
  293. };
  294. static const struct exynos_adc_data exynos3250_adc_data = {
  295. .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
  296. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  297. .needs_sclk = true,
  298. .needs_adc_phy = true,
  299. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  300. .init_hw = exynos_adc_v2_init_hw,
  301. .exit_hw = exynos_adc_v2_exit_hw,
  302. .clear_irq = exynos_adc_v2_clear_irq,
  303. .start_conv = exynos_adc_v2_start_conv,
  304. };
  305. static void exynos_adc_exynos7_init_hw(struct exynos_adc *info)
  306. {
  307. u32 con1, con2;
  308. con1 = ADC_V2_CON1_SOFT_RESET;
  309. writel(con1, ADC_V2_CON1(info->regs));
  310. con2 = readl(ADC_V2_CON2(info->regs));
  311. con2 &= ~ADC_V2_CON2_C_TIME(7);
  312. con2 |= ADC_V2_CON2_C_TIME(0);
  313. writel(con2, ADC_V2_CON2(info->regs));
  314. /* Enable interrupts */
  315. writel(1, ADC_V2_INT_EN(info->regs));
  316. }
  317. static const struct exynos_adc_data exynos7_adc_data = {
  318. .num_channels = MAX_ADC_V1_CHANNELS,
  319. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  320. .init_hw = exynos_adc_exynos7_init_hw,
  321. .exit_hw = exynos_adc_v2_exit_hw,
  322. .clear_irq = exynos_adc_v2_clear_irq,
  323. .start_conv = exynos_adc_v2_start_conv,
  324. };
  325. static const struct of_device_id exynos_adc_match[] = {
  326. {
  327. .compatible = "samsung,s3c6410-adc",
  328. .data = &exynos_adc_s3c64xx_data,
  329. }, {
  330. .compatible = "samsung,s5pv210-adc",
  331. .data = &exynos_adc_s5pv210_data,
  332. }, {
  333. .compatible = "samsung,exynos4212-adc",
  334. .data = &exynos4212_adc_data,
  335. }, {
  336. .compatible = "samsung,exynos-adc-v1",
  337. .data = &exynos_adc_v1_data,
  338. }, {
  339. .compatible = "samsung,exynos-adc-v2",
  340. .data = &exynos_adc_v2_data,
  341. }, {
  342. .compatible = "samsung,exynos3250-adc",
  343. .data = &exynos3250_adc_data,
  344. }, {
  345. .compatible = "samsung,exynos7-adc",
  346. .data = &exynos7_adc_data,
  347. },
  348. { }
  349. };
  350. MODULE_DEVICE_TABLE(of, exynos_adc_match);
  351. static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
  352. {
  353. const struct of_device_id *match;
  354. match = of_match_node(exynos_adc_match, pdev->dev.of_node);
  355. return (struct exynos_adc_data *)match->data;
  356. }
  357. static int exynos_read_raw(struct iio_dev *indio_dev,
  358. struct iio_chan_spec const *chan,
  359. int *val,
  360. int *val2,
  361. long mask)
  362. {
  363. struct exynos_adc *info = iio_priv(indio_dev);
  364. unsigned long time_left;
  365. int ret;
  366. if (mask == IIO_CHAN_INFO_SCALE) {
  367. ret = regulator_get_voltage(info->vdd);
  368. if (ret < 0)
  369. return ret;
  370. /* Regulator voltage is in uV, but need mV */
  371. *val = ret / 1000;
  372. *val2 = info->data->mask;
  373. return IIO_VAL_FRACTIONAL;
  374. } else if (mask != IIO_CHAN_INFO_RAW) {
  375. return -EINVAL;
  376. }
  377. mutex_lock(&info->lock);
  378. reinit_completion(&info->completion);
  379. /* Select the channel to be used and Trigger conversion */
  380. if (info->data->start_conv)
  381. info->data->start_conv(info, chan->address);
  382. time_left = wait_for_completion_timeout(&info->completion,
  383. EXYNOS_ADC_TIMEOUT);
  384. if (time_left == 0) {
  385. dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
  386. if (info->data->init_hw)
  387. info->data->init_hw(info);
  388. ret = -ETIMEDOUT;
  389. } else {
  390. *val = info->value;
  391. *val2 = 0;
  392. ret = IIO_VAL_INT;
  393. }
  394. mutex_unlock(&info->lock);
  395. return ret;
  396. }
  397. static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
  398. {
  399. struct exynos_adc *info = dev_id;
  400. u32 mask = info->data->mask;
  401. /* Read value */
  402. info->value = readl(ADC_V1_DATX(info->regs)) & mask;
  403. /* clear irq */
  404. if (info->data->clear_irq)
  405. info->data->clear_irq(info);
  406. complete(&info->completion);
  407. return IRQ_HANDLED;
  408. }
  409. static int exynos_adc_reg_access(struct iio_dev *indio_dev,
  410. unsigned reg, unsigned writeval,
  411. unsigned *readval)
  412. {
  413. struct exynos_adc *info = iio_priv(indio_dev);
  414. if (readval == NULL)
  415. return -EINVAL;
  416. *readval = readl(info->regs + reg);
  417. return 0;
  418. }
  419. static const struct iio_info exynos_adc_iio_info = {
  420. .read_raw = &exynos_read_raw,
  421. .debugfs_reg_access = &exynos_adc_reg_access,
  422. };
  423. #define ADC_CHANNEL(_index, _id) { \
  424. .type = IIO_VOLTAGE, \
  425. .indexed = 1, \
  426. .channel = _index, \
  427. .address = _index, \
  428. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  429. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \
  430. .datasheet_name = _id, \
  431. }
  432. static const struct iio_chan_spec exynos_adc_iio_channels[] = {
  433. ADC_CHANNEL(0, "adc0"),
  434. ADC_CHANNEL(1, "adc1"),
  435. ADC_CHANNEL(2, "adc2"),
  436. ADC_CHANNEL(3, "adc3"),
  437. ADC_CHANNEL(4, "adc4"),
  438. ADC_CHANNEL(5, "adc5"),
  439. ADC_CHANNEL(6, "adc6"),
  440. ADC_CHANNEL(7, "adc7"),
  441. ADC_CHANNEL(8, "adc8"),
  442. ADC_CHANNEL(9, "adc9"),
  443. };
  444. static int exynos_adc_probe(struct platform_device *pdev)
  445. {
  446. struct exynos_adc *info = NULL;
  447. struct device *dev = &pdev->dev;
  448. struct device_node *np = pdev->dev.of_node;
  449. struct iio_dev *indio_dev = NULL;
  450. int ret;
  451. int irq;
  452. indio_dev = devm_iio_device_alloc(dev, sizeof(struct exynos_adc));
  453. if (!indio_dev)
  454. return -ENOMEM;
  455. info = iio_priv(indio_dev);
  456. info->data = exynos_adc_get_data(pdev);
  457. if (!info->data)
  458. return dev_err_probe(dev, -EINVAL, "failed getting exynos_adc_data\n");
  459. info->regs = devm_platform_ioremap_resource(pdev, 0);
  460. if (IS_ERR(info->regs))
  461. return PTR_ERR(info->regs);
  462. if (info->data->needs_adc_phy) {
  463. info->pmu_map = syscon_regmap_lookup_by_phandle(np, "samsung,syscon-phandle");
  464. if (IS_ERR(info->pmu_map))
  465. return dev_err_probe(dev, PTR_ERR(info->pmu_map),
  466. "syscon regmap lookup failed.\n");
  467. }
  468. irq = platform_get_irq(pdev, 0);
  469. if (irq < 0)
  470. return irq;
  471. info->irq = irq;
  472. info->dev = dev;
  473. init_completion(&info->completion);
  474. info->clk = devm_clk_get(dev, "adc");
  475. if (IS_ERR(info->clk))
  476. return dev_err_probe(dev, PTR_ERR(info->clk), "failed getting clock\n");
  477. if (info->data->needs_sclk) {
  478. info->sclk = devm_clk_get(dev, "sclk");
  479. if (IS_ERR(info->sclk))
  480. return dev_err_probe(dev, PTR_ERR(info->sclk),
  481. "failed getting sclk clock\n");
  482. }
  483. info->vdd = devm_regulator_get(dev, "vdd");
  484. if (IS_ERR(info->vdd))
  485. return dev_err_probe(dev, PTR_ERR(info->vdd), "failed getting regulator");
  486. ret = regulator_enable(info->vdd);
  487. if (ret)
  488. return ret;
  489. ret = exynos_adc_prepare_clk(info);
  490. if (ret)
  491. goto err_disable_reg;
  492. ret = exynos_adc_enable_clk(info);
  493. if (ret)
  494. goto err_unprepare_clk;
  495. platform_set_drvdata(pdev, indio_dev);
  496. indio_dev->name = dev_name(dev);
  497. indio_dev->info = &exynos_adc_iio_info;
  498. indio_dev->modes = INDIO_DIRECT_MODE;
  499. indio_dev->channels = exynos_adc_iio_channels;
  500. indio_dev->num_channels = info->data->num_channels;
  501. mutex_init(&info->lock);
  502. ret = request_irq(info->irq, exynos_adc_isr, 0, dev_name(dev), info);
  503. if (ret < 0) {
  504. dev_err(dev, "failed requesting irq, irq = %d\n", info->irq);
  505. goto err_disable_clk;
  506. }
  507. ret = iio_device_register(indio_dev);
  508. if (ret)
  509. goto err_irq;
  510. if (info->data->init_hw)
  511. info->data->init_hw(info);
  512. ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
  513. if (ret < 0) {
  514. dev_err(dev, "failed adding child nodes\n");
  515. goto err_of_populate;
  516. }
  517. return 0;
  518. err_of_populate:
  519. of_platform_depopulate(&indio_dev->dev);
  520. iio_device_unregister(indio_dev);
  521. err_irq:
  522. free_irq(info->irq, info);
  523. err_disable_clk:
  524. if (info->data->exit_hw)
  525. info->data->exit_hw(info);
  526. exynos_adc_disable_clk(info);
  527. err_unprepare_clk:
  528. exynos_adc_unprepare_clk(info);
  529. err_disable_reg:
  530. regulator_disable(info->vdd);
  531. return ret;
  532. }
  533. static void exynos_adc_remove(struct platform_device *pdev)
  534. {
  535. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  536. struct exynos_adc *info = iio_priv(indio_dev);
  537. of_platform_depopulate(&indio_dev->dev);
  538. iio_device_unregister(indio_dev);
  539. free_irq(info->irq, info);
  540. if (info->data->exit_hw)
  541. info->data->exit_hw(info);
  542. exynos_adc_disable_clk(info);
  543. exynos_adc_unprepare_clk(info);
  544. regulator_disable(info->vdd);
  545. }
  546. static int exynos_adc_suspend(struct device *dev)
  547. {
  548. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  549. struct exynos_adc *info = iio_priv(indio_dev);
  550. if (info->data->exit_hw)
  551. info->data->exit_hw(info);
  552. exynos_adc_disable_clk(info);
  553. regulator_disable(info->vdd);
  554. return 0;
  555. }
  556. static int exynos_adc_resume(struct device *dev)
  557. {
  558. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  559. struct exynos_adc *info = iio_priv(indio_dev);
  560. int ret;
  561. ret = regulator_enable(info->vdd);
  562. if (ret)
  563. return ret;
  564. ret = exynos_adc_enable_clk(info);
  565. if (ret)
  566. return ret;
  567. if (info->data->init_hw)
  568. info->data->init_hw(info);
  569. return 0;
  570. }
  571. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops, exynos_adc_suspend,
  572. exynos_adc_resume);
  573. static struct platform_driver exynos_adc_driver = {
  574. .probe = exynos_adc_probe,
  575. .remove = exynos_adc_remove,
  576. .driver = {
  577. .name = "exynos-adc",
  578. .of_match_table = exynos_adc_match,
  579. .pm = pm_sleep_ptr(&exynos_adc_pm_ops),
  580. },
  581. };
  582. module_platform_driver(exynos_adc_driver);
  583. MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
  584. MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
  585. MODULE_LICENSE("GPL v2");