adc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
  215. unsigned long flags;
  216. unsigned data0, data1;
  217. if (!client) {
  218. dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
  219. goto exit;
  220. }
  221. data0 = readl(adc->regs + S3C2410_ADCDAT0);
  222. data1 = readl(adc->regs + S3C2410_ADCDAT1);
  223. adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1);
  224. client->nr_samples--;
  225. if (cpu == TYPE_S3C64XX) {
  226. /* S3C64XX ADC resolution is 12-bit */
  227. data0 &= 0xfff;
  228. data1 &= 0xfff;
  229. } else {
  230. data0 &= 0x3ff;
  231. data1 &= 0x3ff;
  232. }
  233. if (client->convert_cb)
  234. (client->convert_cb)(client, data0, data1, &client->nr_samples);
  235. if (client->nr_samples > 0) {
  236. /* fire another conversion for this */
  237. client->select_cb(client, 1);
  238. s3c_adc_convert(adc);
  239. } else {
  240. local_irq_save(flags);
  241. (client->select_cb)(client, 0);
  242. adc->cur = NULL;
  243. s3c_adc_try(adc);
  244. local_irq_restore(flags);
  245. }
  246. exit:
  247. if (cpu == TYPE_S3C64XX) {
  248. /* Clear ADC interrupt */
  249. writel(0, adc->regs + S3C64XX_ADCCLRINT);
  250. }
  251. return IRQ_HANDLED;
  252. }
  253. static int s3c_adc_probe(struct platform_device *pdev)
  254. {
  255. struct device *dev = &pdev->dev;
  256. struct adc_device *adc;
  257. struct resource *regs;
  258. int ret;
  259. unsigned tmp;
  260. adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
  261. if (adc == NULL) {
  262. dev_err(dev, "failed to allocate adc_device\n");
  263. return -ENOMEM;
  264. }
  265. adc->pdev = pdev;
  266. adc->prescale = S3C2410_ADCCON_PRSCVL(49);
  267. adc->irq = platform_get_irq(pdev, 1);
  268. if (adc->irq <= 0) {
  269. dev_err(dev, "failed to get adc irq\n");
  270. ret = -ENOENT;
  271. goto err_alloc;
  272. }
  273. ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc);
  274. if (ret < 0) {
  275. dev_err(dev, "failed to attach adc irq\n");
  276. goto err_alloc;
  277. }
  278. adc->clk = clk_get(dev, "adc");
  279. if (IS_ERR(adc->clk)) {
  280. dev_err(dev, "failed to get adc clock\n");
  281. ret = PTR_ERR(adc->clk);
  282. goto err_irq;
  283. }
  284. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. if (!regs) {
  286. dev_err(dev, "failed to find registers\n");
  287. ret = -ENXIO;
  288. goto err_clk;
  289. }
  290. adc->regs = ioremap(regs->start, resource_size(regs));
  291. if (!adc->regs) {
  292. dev_err(dev, "failed to map registers\n");
  293. ret = -ENXIO;
  294. goto err_clk;
  295. }
  296. clk_enable(adc->clk);
  297. tmp = adc->prescale | S3C2410_ADCCON_PRSCEN;
  298. if (platform_get_device_id(pdev)->driver_data == TYPE_S3C64XX) {
  299. /* Enable 12-bit ADC resolution */
  300. tmp |= S3C64XX_ADCCON_RESSEL;
  301. }
  302. writel(tmp, adc->regs + S3C2410_ADCCON);
  303. dev_info(dev, "attached adc driver\n");
  304. platform_set_drvdata(pdev, adc);
  305. adc_dev = adc;
  306. return 0;
  307. err_clk:
  308. clk_put(adc->clk);
  309. err_irq:
  310. free_irq(adc->irq, adc);
  311. err_alloc:
  312. kfree(adc);
  313. return ret;
  314. }
  315. static int __devexit s3c_adc_remove(struct platform_device *pdev)
  316. {
  317. struct adc_device *adc = platform_get_drvdata(pdev);
  318. iounmap(adc->regs);
  319. free_irq(adc->irq, adc);
  320. clk_disable(adc->clk);
  321. clk_put(adc->clk);
  322. kfree(adc);
  323. return 0;
  324. }
  325. #ifdef CONFIG_PM
  326. static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state)
  327. {
  328. struct adc_device *adc = platform_get_drvdata(pdev);
  329. u32 con;
  330. con = readl(adc->regs + S3C2410_ADCCON);
  331. con |= S3C2410_ADCCON_STDBM;
  332. writel(con, adc->regs + S3C2410_ADCCON);
  333. clk_disable(adc->clk);
  334. return 0;
  335. }
  336. static int s3c_adc_resume(struct platform_device *pdev)
  337. {
  338. struct adc_device *adc = platform_get_drvdata(pdev);
  339. clk_enable(adc->clk);
  340. writel(adc->prescale | S3C2410_ADCCON_PRSCEN,
  341. adc->regs + S3C2410_ADCCON);
  342. return 0;
  343. }
  344. #else
  345. #define s3c_adc_suspend NULL
  346. #define s3c_adc_resume NULL
  347. #endif
  348. static struct platform_device_id s3c_adc_driver_ids[] = {
  349. {
  350. .name = "s3c24xx-adc",
  351. .driver_data = TYPE_S3C24XX,
  352. }, {
  353. .name = "s3c64xx-adc",
  354. .driver_data = TYPE_S3C64XX,
  355. },
  356. { }
  357. };
  358. MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids);
  359. static struct platform_driver s3c_adc_driver = {
  360. .id_table = s3c_adc_driver_ids,
  361. .driver = {
  362. .name = "s3c-adc",
  363. .owner = THIS_MODULE,
  364. },
  365. .probe = s3c_adc_probe,
  366. .remove = __devexit_p(s3c_adc_remove),
  367. .suspend = s3c_adc_suspend,
  368. .resume = s3c_adc_resume,
  369. };
  370. static int __init adc_init(void)
  371. {
  372. int ret;
  373. ret = platform_driver_register(&s3c_adc_driver);
  374. if (ret)
  375. printk(KERN_ERR "%s: failed to add adc driver\n", __func__);
  376. return ret;
  377. }
  378. arch_initcall(adc_init);