exynos_adc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <linux/clk.h>
  30. #include <linux/completion.h>
  31. #include <linux/of.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/iio/iio.h>
  36. #include <linux/iio/machine.h>
  37. #include <linux/iio/driver.h>
  38. enum adc_version {
  39. ADC_V1,
  40. ADC_V2
  41. };
  42. /* 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. /* 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 ADC_V2 */
  61. #define ADC_V2_CON1_SOFT_RESET (1u << 2)
  62. #define ADC_V2_CON2_OSEL (1u << 10)
  63. #define ADC_V2_CON2_ESEL (1u << 9)
  64. #define ADC_V2_CON2_HIGHF (1u << 8)
  65. #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
  66. #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
  67. #define ADC_V2_CON2_ACH_MASK 0xF
  68. #define MAX_ADC_V2_CHANNELS 10
  69. #define MAX_ADC_V1_CHANNELS 8
  70. /* Bit definitions common for ADC_V1 and ADC_V2 */
  71. #define ADC_CON_EN_START (1u << 0)
  72. #define ADC_DATX_MASK 0xFFF
  73. #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(1000))
  74. struct exynos_adc {
  75. void __iomem *regs;
  76. struct clk *clk;
  77. unsigned int irq;
  78. struct regulator *vdd;
  79. struct completion completion;
  80. u32 value;
  81. unsigned int version;
  82. };
  83. static const struct of_device_id exynos_adc_match[] = {
  84. { .compatible = "samsung,exynos-adc-v1", .data = (void *)ADC_V1 },
  85. { .compatible = "samsung,exynos-adc-v2", .data = (void *)ADC_V2 },
  86. {},
  87. };
  88. MODULE_DEVICE_TABLE(of, exynos_adc_match);
  89. static inline unsigned int exynos_adc_get_version(struct platform_device *pdev)
  90. {
  91. const struct of_device_id *match;
  92. match = of_match_node(exynos_adc_match, pdev->dev.of_node);
  93. return (unsigned int)match->data;
  94. }
  95. static int exynos_read_raw(struct iio_dev *indio_dev,
  96. struct iio_chan_spec const *chan,
  97. int *val,
  98. int *val2,
  99. long mask)
  100. {
  101. struct exynos_adc *info = iio_priv(indio_dev);
  102. unsigned long timeout;
  103. u32 con1, con2;
  104. if (mask != IIO_CHAN_INFO_RAW)
  105. return -EINVAL;
  106. mutex_lock(&indio_dev->mlock);
  107. /* Select the channel to be used and Trigger conversion */
  108. if (info->version == ADC_V2) {
  109. con2 = readl(ADC_V2_CON2(info->regs));
  110. con2 &= ~ADC_V2_CON2_ACH_MASK;
  111. con2 |= ADC_V2_CON2_ACH_SEL(chan->address);
  112. writel(con2, ADC_V2_CON2(info->regs));
  113. con1 = readl(ADC_V2_CON1(info->regs));
  114. writel(con1 | ADC_CON_EN_START,
  115. ADC_V2_CON1(info->regs));
  116. } else {
  117. writel(chan->address, ADC_V1_MUX(info->regs));
  118. con1 = readl(ADC_V1_CON(info->regs));
  119. writel(con1 | ADC_CON_EN_START,
  120. ADC_V1_CON(info->regs));
  121. }
  122. timeout = wait_for_completion_interruptible_timeout
  123. (&info->completion, EXYNOS_ADC_TIMEOUT);
  124. *val = info->value;
  125. mutex_unlock(&indio_dev->mlock);
  126. if (timeout == 0)
  127. return -ETIMEDOUT;
  128. return IIO_VAL_INT;
  129. }
  130. static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
  131. {
  132. struct exynos_adc *info = (struct exynos_adc *)dev_id;
  133. /* Read value */
  134. info->value = readl(ADC_V1_DATX(info->regs)) &
  135. ADC_DATX_MASK;
  136. /* clear irq */
  137. if (info->version == ADC_V2)
  138. writel(1, ADC_V2_INT_ST(info->regs));
  139. else
  140. writel(1, ADC_V1_INTCLR(info->regs));
  141. complete(&info->completion);
  142. return IRQ_HANDLED;
  143. }
  144. static int exynos_adc_reg_access(struct iio_dev *indio_dev,
  145. unsigned reg, unsigned writeval,
  146. unsigned *readval)
  147. {
  148. struct exynos_adc *info = iio_priv(indio_dev);
  149. if (readval == NULL)
  150. return -EINVAL;
  151. *readval = readl(info->regs + reg);
  152. return 0;
  153. }
  154. static const struct iio_info exynos_adc_iio_info = {
  155. .read_raw = &exynos_read_raw,
  156. .debugfs_reg_access = &exynos_adc_reg_access,
  157. .driver_module = THIS_MODULE,
  158. };
  159. #define ADC_CHANNEL(_index, _id) { \
  160. .type = IIO_VOLTAGE, \
  161. .indexed = 1, \
  162. .channel = _index, \
  163. .address = _index, \
  164. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
  165. .datasheet_name = _id, \
  166. }
  167. static const struct iio_chan_spec exynos_adc_iio_channels[] = {
  168. ADC_CHANNEL(0, "adc0"),
  169. ADC_CHANNEL(1, "adc1"),
  170. ADC_CHANNEL(2, "adc2"),
  171. ADC_CHANNEL(3, "adc3"),
  172. ADC_CHANNEL(4, "adc4"),
  173. ADC_CHANNEL(5, "adc5"),
  174. ADC_CHANNEL(6, "adc6"),
  175. ADC_CHANNEL(7, "adc7"),
  176. ADC_CHANNEL(8, "adc8"),
  177. ADC_CHANNEL(9, "adc9"),
  178. };
  179. static int exynos_adc_remove_devices(struct device *dev, void *c)
  180. {
  181. struct platform_device *pdev = to_platform_device(dev);
  182. platform_device_unregister(pdev);
  183. return 0;
  184. }
  185. static void exynos_adc_hw_init(struct exynos_adc *info)
  186. {
  187. u32 con1, con2;
  188. if (info->version == ADC_V2) {
  189. con1 = ADC_V2_CON1_SOFT_RESET;
  190. writel(con1, ADC_V2_CON1(info->regs));
  191. con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
  192. ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
  193. writel(con2, ADC_V2_CON2(info->regs));
  194. /* Enable interrupts */
  195. writel(1, ADC_V2_INT_EN(info->regs));
  196. } else {
  197. /* set default prescaler values and Enable prescaler */
  198. con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
  199. /* Enable 12-bit ADC resolution */
  200. con1 |= ADC_V1_CON_RES;
  201. writel(con1, ADC_V1_CON(info->regs));
  202. }
  203. }
  204. static int exynos_adc_probe(struct platform_device *pdev)
  205. {
  206. struct exynos_adc *info = NULL;
  207. struct device_node *np = pdev->dev.of_node;
  208. struct iio_dev *indio_dev = NULL;
  209. struct resource *mem;
  210. int ret = -ENODEV;
  211. int irq;
  212. if (!np)
  213. return ret;
  214. indio_dev = iio_device_alloc(sizeof(struct exynos_adc));
  215. if (!indio_dev) {
  216. dev_err(&pdev->dev, "failed allocating iio device\n");
  217. return -ENOMEM;
  218. }
  219. info = iio_priv(indio_dev);
  220. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  221. info->regs = devm_request_and_ioremap(&pdev->dev, mem);
  222. if (!info->regs) {
  223. ret = -ENOMEM;
  224. goto err_iio;
  225. }
  226. irq = platform_get_irq(pdev, 0);
  227. if (irq < 0) {
  228. dev_err(&pdev->dev, "no irq resource?\n");
  229. ret = irq;
  230. goto err_iio;
  231. }
  232. info->irq = irq;
  233. init_completion(&info->completion);
  234. ret = request_irq(info->irq, exynos_adc_isr,
  235. 0, dev_name(&pdev->dev), info);
  236. if (ret < 0) {
  237. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
  238. info->irq);
  239. goto err_iio;
  240. }
  241. info->clk = devm_clk_get(&pdev->dev, "adc");
  242. if (IS_ERR(info->clk)) {
  243. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  244. PTR_ERR(info->clk));
  245. ret = PTR_ERR(info->clk);
  246. goto err_irq;
  247. }
  248. info->vdd = devm_regulator_get(&pdev->dev, "vdd");
  249. if (IS_ERR(info->vdd)) {
  250. dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
  251. PTR_ERR(info->vdd));
  252. ret = PTR_ERR(info->vdd);
  253. goto err_irq;
  254. }
  255. info->version = exynos_adc_get_version(pdev);
  256. platform_set_drvdata(pdev, indio_dev);
  257. indio_dev->name = dev_name(&pdev->dev);
  258. indio_dev->dev.parent = &pdev->dev;
  259. indio_dev->dev.of_node = pdev->dev.of_node;
  260. indio_dev->info = &exynos_adc_iio_info;
  261. indio_dev->modes = INDIO_DIRECT_MODE;
  262. indio_dev->channels = exynos_adc_iio_channels;
  263. if (info->version == ADC_V1)
  264. indio_dev->num_channels = MAX_ADC_V1_CHANNELS;
  265. else
  266. indio_dev->num_channels = MAX_ADC_V2_CHANNELS;
  267. ret = iio_device_register(indio_dev);
  268. if (ret)
  269. goto err_irq;
  270. ret = regulator_enable(info->vdd);
  271. if (ret)
  272. goto err_iio_dev;
  273. clk_prepare_enable(info->clk);
  274. exynos_adc_hw_init(info);
  275. ret = of_platform_populate(np, exynos_adc_match, NULL, &pdev->dev);
  276. if (ret < 0) {
  277. dev_err(&pdev->dev, "failed adding child nodes\n");
  278. goto err_of_populate;
  279. }
  280. return 0;
  281. err_of_populate:
  282. device_for_each_child(&pdev->dev, NULL,
  283. exynos_adc_remove_devices);
  284. regulator_disable(info->vdd);
  285. clk_disable_unprepare(info->clk);
  286. err_iio_dev:
  287. iio_device_unregister(indio_dev);
  288. err_irq:
  289. free_irq(info->irq, info);
  290. err_iio:
  291. iio_device_free(indio_dev);
  292. return ret;
  293. }
  294. static int exynos_adc_remove(struct platform_device *pdev)
  295. {
  296. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  297. struct exynos_adc *info = iio_priv(indio_dev);
  298. device_for_each_child(&pdev->dev, NULL,
  299. exynos_adc_remove_devices);
  300. regulator_disable(info->vdd);
  301. clk_disable_unprepare(info->clk);
  302. iio_device_unregister(indio_dev);
  303. free_irq(info->irq, info);
  304. iio_device_free(indio_dev);
  305. return 0;
  306. }
  307. #ifdef CONFIG_PM_SLEEP
  308. static int exynos_adc_suspend(struct device *dev)
  309. {
  310. struct platform_device *pdev = to_platform_device(dev);
  311. struct exynos_adc *info = platform_get_drvdata(pdev);
  312. u32 con;
  313. if (info->version == ADC_V2) {
  314. con = readl(ADC_V2_CON1(info->regs));
  315. con &= ~ADC_CON_EN_START;
  316. writel(con, ADC_V2_CON1(info->regs));
  317. } else {
  318. con = readl(ADC_V1_CON(info->regs));
  319. con |= ADC_V1_CON_STANDBY;
  320. writel(con, ADC_V1_CON(info->regs));
  321. }
  322. clk_disable_unprepare(info->clk);
  323. regulator_disable(info->vdd);
  324. return 0;
  325. }
  326. static int exynos_adc_resume(struct device *dev)
  327. {
  328. struct platform_device *pdev = to_platform_device(dev);
  329. struct exynos_adc *info = platform_get_drvdata(pdev);
  330. int ret;
  331. ret = regulator_enable(info->vdd);
  332. if (ret)
  333. return ret;
  334. clk_prepare_enable(info->clk);
  335. exynos_adc_hw_init(info);
  336. return 0;
  337. }
  338. #endif
  339. static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
  340. exynos_adc_suspend,
  341. exynos_adc_resume);
  342. static struct platform_driver exynos_adc_driver = {
  343. .probe = exynos_adc_probe,
  344. .remove = exynos_adc_remove,
  345. .driver = {
  346. .name = "exynos-adc",
  347. .owner = THIS_MODULE,
  348. .of_match_table = of_match_ptr(exynos_adc_match),
  349. .pm = &exynos_adc_pm_ops,
  350. },
  351. };
  352. module_platform_driver(exynos_adc_driver);
  353. MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
  354. MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
  355. MODULE_LICENSE("GPL v2");