adc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* arch/arm/plat-s3c24xx/adc.c
  2. *
  3. * Copyright (c) 2008 Simtec Electronics
  4. * http://armlinux.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
  6. *
  7. * S3C24XX ADC device core
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/list.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <plat/regs-adc.h>
  22. #include <plat/adc.h>
  23. /* This driver is designed to control the usage of the ADC block between
  24. * the touchscreen and any other drivers that may need to use it, such as
  25. * the hwmon driver.
  26. *
  27. * Priority will be given to the touchscreen driver, but as this itself is
  28. * rate limited it should not starve other requests which are processed in
  29. * order that they are received.
  30. *
  31. * Each user registers to get a client block which uniquely identifies it
  32. * and stores information such as the necessary functions to callback when
  33. * action is required.
  34. */
  35. struct s3c_adc_client {
  36. struct platform_device *pdev;
  37. struct list_head pend;
  38. unsigned int nr_samples;
  39. unsigned char is_ts;
  40. unsigned char channel;
  41. void (*select_cb)(unsigned selected);
  42. void (*convert_cb)(unsigned val1, unsigned val2);
  43. };
  44. struct adc_device {
  45. struct platform_device *pdev;
  46. struct platform_device *owner;
  47. struct clk *clk;
  48. struct s3c_adc_client *cur;
  49. struct s3c_adc_client *ts_pend;
  50. void __iomem *regs;
  51. unsigned int prescale;
  52. int irq;
  53. };
  54. static struct adc_device *adc_dev;
  55. static LIST_HEAD(adc_pending);
  56. #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
  57. static inline void s3c_adc_convert(struct adc_device *adc)
  58. {
  59. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  60. con |= S3C2410_ADCCON_ENABLE_START;
  61. writel(con, adc->regs + S3C2410_ADCCON);
  62. }
  63. static inline void s3c_adc_select(struct adc_device *adc,
  64. struct s3c_adc_client *client)
  65. {
  66. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  67. client->select_cb(1);
  68. con &= ~S3C2410_ADCCON_MUXMASK;
  69. con &= ~S3C2410_ADCCON_STDBM;
  70. con &= ~S3C2410_ADCCON_STARTMASK;
  71. if (!client->is_ts)
  72. con |= S3C2410_ADCCON_SELMUX(client->channel);
  73. writel(con, adc->regs + S3C2410_ADCCON);
  74. }
  75. static void s3c_adc_dbgshow(struct adc_device *adc)
  76. {
  77. adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
  78. readl(adc->regs + S3C2410_ADCCON),
  79. readl(adc->regs + S3C2410_ADCTSC),
  80. readl(adc->regs + S3C2410_ADCDLY));
  81. }
  82. void s3c_adc_try(struct adc_device *adc)
  83. {
  84. struct s3c_adc_client *next = adc->ts_pend;
  85. if (!next && !list_empty(&adc_pending)) {
  86. next = list_first_entry(&adc_pending,
  87. struct s3c_adc_client, pend);
  88. list_del(&next->pend);
  89. } else
  90. adc->ts_pend = NULL;
  91. if (next) {
  92. adc_dbg(adc, "new client is %p\n", next);
  93. adc->cur = next;
  94. s3c_adc_select(adc, next);
  95. s3c_adc_convert(adc);
  96. s3c_adc_dbgshow(adc);
  97. }
  98. }
  99. int s3c_adc_start(struct s3c_adc_client *client,
  100. unsigned int channel, unsigned int nr_samples)
  101. {
  102. struct adc_device *adc = adc_dev;
  103. unsigned long flags;
  104. if (!adc) {
  105. printk(KERN_ERR "%s: failed to find adc\n", __func__);
  106. return -EINVAL;
  107. }
  108. if (client->is_ts && adc->ts_pend)
  109. return -EAGAIN;
  110. local_irq_save(flags);
  111. client->channel = channel;
  112. client->nr_samples = nr_samples;
  113. if (client->is_ts)
  114. adc->ts_pend = client;
  115. else
  116. list_add_tail(&client->pend, &adc_pending);
  117. if (!adc->cur)
  118. s3c_adc_try(adc);
  119. local_irq_restore(flags);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(s3c_adc_start);
  123. static void s3c_adc_default_select(unsigned select)
  124. {
  125. }
  126. struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
  127. void (*select)(unsigned int selected),
  128. void (*conv)(unsigned d0, unsigned d1),
  129. unsigned int is_ts)
  130. {
  131. struct s3c_adc_client *client;
  132. WARN_ON(!pdev);
  133. WARN_ON(!conv);
  134. if (!select)
  135. select = s3c_adc_default_select;
  136. if (!conv || !pdev)
  137. return ERR_PTR(-EINVAL);
  138. client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
  139. if (!client) {
  140. dev_err(&pdev->dev, "no memory for adc client\n");
  141. return ERR_PTR(-ENOMEM);
  142. }
  143. client->pdev = pdev;
  144. client->is_ts = is_ts;
  145. client->select_cb = select;
  146. client->convert_cb = conv;
  147. return client;
  148. }
  149. EXPORT_SYMBOL_GPL(s3c_adc_register);
  150. void s3c_adc_release(struct s3c_adc_client *client)
  151. {
  152. /* We should really check that nothing is in progress. */
  153. kfree(client);
  154. }
  155. EXPORT_SYMBOL_GPL(s3c_adc_release);
  156. static irqreturn_t s3c_adc_irq(int irq, void *pw)
  157. {
  158. struct adc_device *adc = pw;
  159. struct s3c_adc_client *client = adc->cur;
  160. unsigned long flags;
  161. unsigned data0, data1;
  162. if (!client) {
  163. dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
  164. return IRQ_HANDLED;
  165. }
  166. data0 = readl(adc->regs + S3C2410_ADCDAT0);
  167. data1 = readl(adc->regs + S3C2410_ADCDAT1);
  168. adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
  169. (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff);
  170. if (--client->nr_samples > 0) {
  171. /* fire another conversion for this */
  172. client->select_cb(1);
  173. s3c_adc_convert(adc);
  174. } else {
  175. local_irq_save(flags);
  176. (client->select_cb)(0);
  177. adc->cur = NULL;
  178. s3c_adc_try(adc);
  179. local_irq_restore(flags);
  180. }
  181. return IRQ_HANDLED;
  182. }
  183. static int s3c_adc_probe(struct platform_device *pdev)
  184. {
  185. struct device *dev = &pdev->dev;
  186. struct adc_device *adc;
  187. struct resource *regs;
  188. int ret;
  189. adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
  190. if (adc == NULL) {
  191. dev_err(dev, "failed to allocate adc_device\n");
  192. return -ENOMEM;
  193. }
  194. adc->pdev = pdev;
  195. adc->prescale = S3C2410_ADCCON_PRSCVL(49);
  196. adc->irq = platform_get_irq(pdev, 1);
  197. if (adc->irq <= 0) {
  198. dev_err(dev, "failed to get adc irq\n");
  199. ret = -ENOENT;
  200. goto err_alloc;
  201. }
  202. ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
  203. if (ret < 0) {
  204. dev_err(dev, "failed to attach adc irq\n");
  205. goto err_alloc;
  206. }
  207. adc->clk = clk_get(dev, "adc");
  208. if (IS_ERR(adc->clk)) {
  209. dev_err(dev, "failed to get adc clock\n");
  210. ret = PTR_ERR(adc->clk);
  211. goto err_irq;
  212. }
  213. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. if (!regs) {
  215. dev_err(dev, "failed to find registers\n");
  216. ret = -ENXIO;
  217. goto err_clk;
  218. }
  219. adc->regs = ioremap(regs->start, resource_size(regs));
  220. if (!adc->regs) {
  221. dev_err(dev, "failed to map registers\n");
  222. ret = -ENXIO;
  223. goto err_clk;
  224. }
  225. clk_enable(adc->clk);
  226. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  227. adc->regs + S3C2410_ADCCON);
  228. dev_info(dev, "attached adc driver\n");
  229. platform_set_drvdata(pdev, adc);
  230. adc_dev = adc;
  231. return 0;
  232. err_clk:
  233. clk_put(adc->clk);
  234. err_irq:
  235. free_irq(adc->irq, adc);
  236. err_alloc:
  237. kfree(adc);
  238. return ret;
  239. }
  240. static int s3c_adc_remove(struct platform_device *pdev)
  241. {
  242. struct adc_device *adc = platform_get_drvdata(pdev);
  243. iounmap(adc->regs);
  244. free_irq(adc->irq, adc);
  245. clk_disable(adc->clk);
  246. clk_put(adc->clk);
  247. kfree(adc);
  248. return 0;
  249. }
  250. #ifdef CONFIG_PM
  251. static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
  252. {
  253. struct adc_device *adc = platform_get_drvdata(pdev);
  254. u32 con;
  255. con = readl(adc->regs + S3C2410_ADCCON);
  256. con |= S3C2410_ADCCON_STDBM;
  257. writel(con, adc->regs + S3C2410_ADCCON);
  258. clk_disable(adc->clk);
  259. return 0;
  260. }
  261. static int s3c_adc_resume(struct platform_device *pdev)
  262. {
  263. struct adc_device *adc = platform_get_drvdata(pdev);
  264. clk_enable(adc->clk);
  265. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  266. adc->regs + S3C2410_ADCCON);
  267. return 0;
  268. }
  269. #else
  270. #define s3c_adc_suspend NULL
  271. #define s3c_adc_resume NULL
  272. #endif
  273. static struct platform_driver s3c_adc_driver = {
  274. .driver = {
  275. .name = "s3c24xx-adc",
  276. .owner = THIS_MODULE,
  277. },
  278. .probe = s3c_adc_probe,
  279. .remove = __devexit_p(s3c_adc_remove),
  280. .suspend = s3c_adc_suspend,
  281. .resume = s3c_adc_resume,
  282. };
  283. static int __init adc_init(void)
  284. {
  285. int ret;
  286. ret = platform_driver_register(&s3c_adc_driver);
  287. if (ret)
  288. printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
  289. return ret;
  290. }
  291. arch_initcall(adc_init);