exynos_adc.c 11 KB

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