exynos_adc.c 11 KB

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