adc.c 9.3 KB

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