adc.c 10 KB

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