adc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. static 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. if (adc_dev->cur == client)
  154. adc_dev->cur = NULL;
  155. if (adc_dev->ts_pend == client)
  156. adc_dev->ts_pend = NULL;
  157. else {
  158. struct list_head *p, *n;
  159. struct s3c_adc_client *tmp;
  160. list_for_each_safe(p, n, &adc_pending) {
  161. tmp = list_entry(p, struct s3c_adc_client, pend);
  162. if (tmp == client)
  163. list_del(&tmp->pend);
  164. }
  165. }
  166. if (adc_dev->cur == NULL)
  167. s3c_adc_try(adc_dev);
  168. kfree(client);
  169. }
  170. EXPORT_SYMBOL_GPL(s3c_adc_release);
  171. static irqreturn_t s3c_adc_irq(int irq, void *pw)
  172. {
  173. struct adc_device *adc = pw;
  174. struct s3c_adc_client *client = adc->cur;
  175. unsigned long flags;
  176. unsigned data0, data1;
  177. if (!client) {
  178. dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
  179. return IRQ_HANDLED;
  180. }
  181. data0 = readl(adc->regs + S3C2410_ADCDAT0);
  182. data1 = readl(adc->regs + S3C2410_ADCDAT1);
  183. adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
  184. (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff);
  185. if (--client->nr_samples > 0) {
  186. /* fire another conversion for this */
  187. client->select_cb(1);
  188. s3c_adc_convert(adc);
  189. } else {
  190. local_irq_save(flags);
  191. (client->select_cb)(0);
  192. adc->cur = NULL;
  193. s3c_adc_try(adc);
  194. local_irq_restore(flags);
  195. }
  196. return IRQ_HANDLED;
  197. }
  198. static int s3c_adc_probe(struct platform_device *pdev)
  199. {
  200. struct device *dev = &pdev->dev;
  201. struct adc_device *adc;
  202. struct resource *regs;
  203. int ret;
  204. adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
  205. if (adc == NULL) {
  206. dev_err(dev, "failed to allocate adc_device\n");
  207. return -ENOMEM;
  208. }
  209. adc->pdev = pdev;
  210. adc->prescale = S3C2410_ADCCON_PRSCVL(49);
  211. adc->irq = platform_get_irq(pdev, 1);
  212. if (adc->irq <= 0) {
  213. dev_err(dev, "failed to get adc irq\n");
  214. ret = -ENOENT;
  215. goto err_alloc;
  216. }
  217. ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
  218. if (ret < 0) {
  219. dev_err(dev, "failed to attach adc irq\n");
  220. goto err_alloc;
  221. }
  222. adc->clk = clk_get(dev, "adc");
  223. if (IS_ERR(adc->clk)) {
  224. dev_err(dev, "failed to get adc clock\n");
  225. ret = PTR_ERR(adc->clk);
  226. goto err_irq;
  227. }
  228. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  229. if (!regs) {
  230. dev_err(dev, "failed to find registers\n");
  231. ret = -ENXIO;
  232. goto err_clk;
  233. }
  234. adc->regs = ioremap(regs->start, resource_size(regs));
  235. if (!adc->regs) {
  236. dev_err(dev, "failed to map registers\n");
  237. ret = -ENXIO;
  238. goto err_clk;
  239. }
  240. clk_enable(adc->clk);
  241. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  242. adc->regs + S3C2410_ADCCON);
  243. dev_info(dev, "attached adc driver\n");
  244. platform_set_drvdata(pdev, adc);
  245. adc_dev = adc;
  246. return 0;
  247. err_clk:
  248. clk_put(adc->clk);
  249. err_irq:
  250. free_irq(adc->irq, adc);
  251. err_alloc:
  252. kfree(adc);
  253. return ret;
  254. }
  255. static int s3c_adc_remove(struct platform_device *pdev)
  256. {
  257. struct adc_device *adc = platform_get_drvdata(pdev);
  258. iounmap(adc->regs);
  259. free_irq(adc->irq, adc);
  260. clk_disable(adc->clk);
  261. clk_put(adc->clk);
  262. kfree(adc);
  263. return 0;
  264. }
  265. #ifdef CONFIG_PM
  266. static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
  267. {
  268. struct adc_device *adc = platform_get_drvdata(pdev);
  269. u32 con;
  270. con = readl(adc->regs + S3C2410_ADCCON);
  271. con |= S3C2410_ADCCON_STDBM;
  272. writel(con, adc->regs + S3C2410_ADCCON);
  273. clk_disable(adc->clk);
  274. return 0;
  275. }
  276. static int s3c_adc_resume(struct platform_device *pdev)
  277. {
  278. struct adc_device *adc = platform_get_drvdata(pdev);
  279. clk_enable(adc->clk);
  280. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  281. adc->regs + S3C2410_ADCCON);
  282. return 0;
  283. }
  284. #else
  285. #define s3c_adc_suspend NULL
  286. #define s3c_adc_resume NULL
  287. #endif
  288. static struct platform_driver s3c_adc_driver = {
  289. .driver = {
  290. .name = "s3c24xx-adc",
  291. .owner = THIS_MODULE,
  292. },
  293. .probe = s3c_adc_probe,
  294. .remove = __devexit_p(s3c_adc_remove),
  295. .suspend = s3c_adc_suspend,
  296. .resume = s3c_adc_resume,
  297. };
  298. static int __init adc_init(void)
  299. {
  300. int ret;
  301. ret = platform_driver_register(&s3c_adc_driver);
  302. if (ret)
  303. printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
  304. return ret;
  305. }
  306. arch_initcall(adc_init);