adc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. unsigned *samples_left);
  44. };
  45. struct adc_device {
  46. struct platform_device *pdev;
  47. struct platform_device *owner;
  48. struct clk *clk;
  49. struct s3c_adc_client *cur;
  50. struct s3c_adc_client *ts_pend;
  51. void __iomem *regs;
  52. unsigned int prescale;
  53. int irq;
  54. };
  55. static struct adc_device *adc_dev;
  56. static LIST_HEAD(adc_pending);
  57. #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
  58. static inline void s3c_adc_convert(struct adc_device *adc)
  59. {
  60. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  61. con |= S3C2410_ADCCON_ENABLE_START;
  62. writel(con, adc->regs + S3C2410_ADCCON);
  63. }
  64. static inline void s3c_adc_select(struct adc_device *adc,
  65. struct s3c_adc_client *client)
  66. {
  67. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  68. client->select_cb(1);
  69. con &= ~S3C2410_ADCCON_MUXMASK;
  70. con &= ~S3C2410_ADCCON_STDBM;
  71. con &= ~S3C2410_ADCCON_STARTMASK;
  72. if (!client->is_ts)
  73. con |= S3C2410_ADCCON_SELMUX(client->channel);
  74. writel(con, adc->regs + S3C2410_ADCCON);
  75. }
  76. static void s3c_adc_dbgshow(struct adc_device *adc)
  77. {
  78. adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
  79. readl(adc->regs + S3C2410_ADCCON),
  80. readl(adc->regs + S3C2410_ADCTSC),
  81. readl(adc->regs + S3C2410_ADCDLY));
  82. }
  83. static void s3c_adc_try(struct adc_device *adc)
  84. {
  85. struct s3c_adc_client *next = adc->ts_pend;
  86. if (!next && !list_empty(&adc_pending)) {
  87. next = list_first_entry(&adc_pending,
  88. struct s3c_adc_client, pend);
  89. list_del(&next->pend);
  90. } else
  91. adc->ts_pend = NULL;
  92. if (next) {
  93. adc_dbg(adc, "new client is %p\n", next);
  94. adc->cur = next;
  95. s3c_adc_select(adc, next);
  96. s3c_adc_convert(adc);
  97. s3c_adc_dbgshow(adc);
  98. }
  99. }
  100. int s3c_adc_start(struct s3c_adc_client *client,
  101. unsigned int channel, unsigned int nr_samples)
  102. {
  103. struct adc_device *adc = adc_dev;
  104. unsigned long flags;
  105. if (!adc) {
  106. printk(KERN_ERR "%s: failed to find adc\n", __func__);
  107. return -EINVAL;
  108. }
  109. if (client->is_ts && adc->ts_pend)
  110. return -EAGAIN;
  111. local_irq_save(flags);
  112. client->channel = channel;
  113. client->nr_samples = nr_samples;
  114. if (client->is_ts)
  115. adc->ts_pend = client;
  116. else
  117. list_add_tail(&client->pend, &adc_pending);
  118. if (!adc->cur)
  119. s3c_adc_try(adc);
  120. local_irq_restore(flags);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL_GPL(s3c_adc_start);
  124. static void s3c_adc_default_select(unsigned select)
  125. {
  126. }
  127. struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
  128. void (*select)(unsigned int selected),
  129. void (*conv)(unsigned d0, unsigned d1,
  130. unsigned *samples_left),
  131. unsigned int is_ts)
  132. {
  133. struct s3c_adc_client *client;
  134. WARN_ON(!pdev);
  135. WARN_ON(!conv);
  136. if (!select)
  137. select = s3c_adc_default_select;
  138. if (!conv || !pdev)
  139. return ERR_PTR(-EINVAL);
  140. client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
  141. if (!client) {
  142. dev_err(&pdev->dev, "no memory for adc client\n");
  143. return ERR_PTR(-ENOMEM);
  144. }
  145. client->pdev = pdev;
  146. client->is_ts = is_ts;
  147. client->select_cb = select;
  148. client->convert_cb = conv;
  149. return client;
  150. }
  151. EXPORT_SYMBOL_GPL(s3c_adc_register);
  152. void s3c_adc_release(struct s3c_adc_client *client)
  153. {
  154. /* We should really check that nothing is in progress. */
  155. if (adc_dev->cur == client)
  156. adc_dev->cur = NULL;
  157. if (adc_dev->ts_pend == client)
  158. adc_dev->ts_pend = NULL;
  159. else {
  160. struct list_head *p, *n;
  161. struct s3c_adc_client *tmp;
  162. list_for_each_safe(p, n, &adc_pending) {
  163. tmp = list_entry(p, struct s3c_adc_client, pend);
  164. if (tmp == client)
  165. list_del(&tmp->pend);
  166. }
  167. }
  168. if (adc_dev->cur == NULL)
  169. s3c_adc_try(adc_dev);
  170. kfree(client);
  171. }
  172. EXPORT_SYMBOL_GPL(s3c_adc_release);
  173. static irqreturn_t s3c_adc_irq(int irq, void *pw)
  174. {
  175. struct adc_device *adc = pw;
  176. struct s3c_adc_client *client = adc->cur;
  177. unsigned long flags;
  178. unsigned data0, data1;
  179. if (!client) {
  180. dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
  181. return IRQ_HANDLED;
  182. }
  183. data0 = readl(adc->regs + S3C2410_ADCDAT0);
  184. data1 = readl(adc->regs + S3C2410_ADCDAT1);
  185. adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
  186. client->nr_samples--;
  187. (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff, &client->nr_samples);
  188. if (client->nr_samples > 0) {
  189. /* fire another conversion for this */
  190. client->select_cb(1);
  191. s3c_adc_convert(adc);
  192. } else {
  193. local_irq_save(flags);
  194. (client->select_cb)(0);
  195. adc->cur = NULL;
  196. s3c_adc_try(adc);
  197. local_irq_restore(flags);
  198. }
  199. return IRQ_HANDLED;
  200. }
  201. static int s3c_adc_probe(struct platform_device *pdev)
  202. {
  203. struct device *dev = &pdev->dev;
  204. struct adc_device *adc;
  205. struct resource *regs;
  206. int ret;
  207. adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
  208. if (adc == NULL) {
  209. dev_err(dev, "failed to allocate adc_device\n");
  210. return -ENOMEM;
  211. }
  212. adc->pdev = pdev;
  213. adc->prescale = S3C2410_ADCCON_PRSCVL(49);
  214. adc->irq = platform_get_irq(pdev, 1);
  215. if (adc->irq <= 0) {
  216. dev_err(dev, "failed to get adc irq\n");
  217. ret = -ENOENT;
  218. goto err_alloc;
  219. }
  220. ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
  221. if (ret < 0) {
  222. dev_err(dev, "failed to attach adc irq\n");
  223. goto err_alloc;
  224. }
  225. adc->clk = clk_get(dev, "adc");
  226. if (IS_ERR(adc->clk)) {
  227. dev_err(dev, "failed to get adc clock\n");
  228. ret = PTR_ERR(adc->clk);
  229. goto err_irq;
  230. }
  231. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. if (!regs) {
  233. dev_err(dev, "failed to find registers\n");
  234. ret = -ENXIO;
  235. goto err_clk;
  236. }
  237. adc->regs = ioremap(regs->start, resource_size(regs));
  238. if (!adc->regs) {
  239. dev_err(dev, "failed to map registers\n");
  240. ret = -ENXIO;
  241. goto err_clk;
  242. }
  243. clk_enable(adc->clk);
  244. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  245. adc->regs + S3C2410_ADCCON);
  246. dev_info(dev, "attached adc driver\n");
  247. platform_set_drvdata(pdev, adc);
  248. adc_dev = adc;
  249. return 0;
  250. err_clk:
  251. clk_put(adc->clk);
  252. err_irq:
  253. free_irq(adc->irq, adc);
  254. err_alloc:
  255. kfree(adc);
  256. return ret;
  257. }
  258. static int s3c_adc_remove(struct platform_device *pdev)
  259. {
  260. struct adc_device *adc = platform_get_drvdata(pdev);
  261. iounmap(adc->regs);
  262. free_irq(adc->irq, adc);
  263. clk_disable(adc->clk);
  264. clk_put(adc->clk);
  265. kfree(adc);
  266. return 0;
  267. }
  268. #ifdef CONFIG_PM
  269. static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
  270. {
  271. struct adc_device *adc = platform_get_drvdata(pdev);
  272. u32 con;
  273. con = readl(adc->regs + S3C2410_ADCCON);
  274. con |= S3C2410_ADCCON_STDBM;
  275. writel(con, adc->regs + S3C2410_ADCCON);
  276. clk_disable(adc->clk);
  277. return 0;
  278. }
  279. static int s3c_adc_resume(struct platform_device *pdev)
  280. {
  281. struct adc_device *adc = platform_get_drvdata(pdev);
  282. clk_enable(adc->clk);
  283. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  284. adc->regs + S3C2410_ADCCON);
  285. return 0;
  286. }
  287. #else
  288. #define s3c_adc_suspend NULL
  289. #define s3c_adc_resume NULL
  290. #endif
  291. static struct platform_driver s3c_adc_driver = {
  292. .driver = {
  293. .name = "s3c24xx-adc",
  294. .owner = THIS_MODULE,
  295. },
  296. .probe = s3c_adc_probe,
  297. .remove = __devexit_p(s3c_adc_remove),
  298. .suspend = s3c_adc_suspend,
  299. .resume = s3c_adc_resume,
  300. };
  301. static int __init adc_init(void)
  302. {
  303. int ret;
  304. ret = platform_driver_register(&s3c_adc_driver);
  305. if (ret)
  306. printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
  307. return ret;
  308. }
  309. arch_initcall(adc_init);