adc.c 9.3 KB

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