adc.c 9.8 KB

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