adc.c 10 KB

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