exynos_adc.c 11 KB

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