averageDataMemoryDefinition.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <linux/regulator/consumer.h>
  24. #include <plat/regs-adc.h>
  25. #include <plat/adc.h>
  26. /* This driver is designed to control the usage of the ADC block between
  27. * the touchscreen and any other drivers that may need to use it, such as
  28. * the hwmon driver.
  29. *
  30. * Priority will be given to the touchscreen driver, but as this itself is
  31. * rate limited it should not starve other requests which are processed in
  32. * order that they are received.
  33. *
  34. * Each user registers to get a client block which uniquely identifies it
  35. * and stores information such as the necessary functions to callback when
  36. * action is required.
  37. */
  38. enum s3c_cpu_type {
  39. TYPE_ADCV1, /* S3C24XX */
  40. TYPE_ADCV11, /* S3C2443 */
  41. TYPE_ADCV12, /* S3C2416, S3C2450 */
  42. TYPE_ADCV2, /* S3C64XX, S5P64X0, S5PC100 */
  43. TYPE_ADCV3, /* S5PV210, S5PC110, EXYNOS4210 */
  44. };
  45. struct s3c_adc_client {
  46. struct platform_device *pdev;
  47. struct list_head pend;
  48. wait_queue_head_t *wait;
  49. unsigned int nr_samples;
  50. int result;
  51. unsigned char is_ts;
  52. unsigned char channel;
  53. void (*select_cb)(struct s3c_adc_client *c, unsigned selected);
  54. void (*convert_cb)(struct s3c_adc_client *c,
  55. unsigned val1, unsigned val2,
  56. unsigned *samples_left);
  57. };
  58. struct adc_device {
  59. struct platform_device *pdev;
  60. struct platform_device *owner;
  61. struct clk *clk;
  62. struct s3c_adc_client *cur;
  63. struct s3c_adc_client *ts_pend;
  64. void __iomem *regs;
  65. spinlock_t lock;
  66. unsigned int prescale;
  67. int irq;
  68. struct regulator *vdd;
  69. };
  70. static struct adc_device *adc_dev;
  71. static LIST_HEAD(adc_pending); /* protected by adc_device.lock */
  72. #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
  73. static inline void s3c_adc_convert(struct adc_device *adc)
  74. {
  75. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  76. con |= S3C2410_ADCCON_ENABLE_START;
  77. writel(con, adc->regs + S3C2410_ADCCON);
  78. }
  79. static inline void s3c_adc_select(struct adc_device *adc,
  80. struct s3c_adc_client *client)
  81. {
  82. unsigned con = readl(adc->regs + S3C2410_ADCCON);
  83. enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data;
  84. client->select_cb(client, 1);
  85. if (cpu == TYPE_ADCV1 || cpu == TYPE_ADCV2)
  86. con &= ~S3C2410_ADCCON_MUXMASK;
  87. con &= ~S3C2410_ADCCON_STDBM;
  88. con &= ~S3C2410_ADCCON_STARTMASK;
  89. if (!client->is_ts) {
  90. if (cpu == TYPE_ADCV3)
  91. writel(client->channel & 0xf, adc->regs + S5P_ADCMUX);
  92. else if (cpu == TYPE_ADCV11 || cpu == TYPE_ADCV12)
  93. writel(client->channel & 0xf,
  94. adc->regs + S3C2443_ADCMUX);
  95. else
  96. con |= S3C2410_ADCCON_SELMUX(client->channel);
  97. }
  98. writel(con, adc->regs + S3C2410_ADCCON);
  99. }
  100. static void s3c_adc_dbgshow(struct adc_device *adc)
  101. {
  102. adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n",
  103. readl(adc->regs + S3C2410_ADCCON),
  104. readl(adc->regs + S3C2410_ADCTSC),
  105. readl(adc->regs + S3C2410_ADCDLY));
  106. }
  107. static void s3c_adc_try(struct adc_device *adc)
  108. {
  109. struct s3c_adc_client *next = adc->ts_pend;
  110. if (!next && !list_empty(&adc_pending)) {
  111. next = list_first_entry(&adc_pending,
  112. struct s3c_adc_client, pend);
  113. list_del(&next->pend);
  114. } else
  115. adc->ts_pend = NULL;
  116. if (next) {
  117. adc_dbg(adc, "new client is %p\n", next);
  118. adc->cur = next;
  119. s3c_adc_select(adc, next);
  120. s3c_adc_convert(adc);
  121. s3c_adc_dbgshow(adc);
  122. }
  123. }
  124. int s3c_adc_start(struct s3c_adc_client *client,
  125. unsigned int channel, unsigned int nr_samples)
  126. {
  127. struct adc_device *adc = adc_dev;
  128. unsigned long flags;
  129. if (!adc) {
  130. printk(KERN_ERR "%s: failed to find adc\n", __func__);
  131. return -EINVAL;
  132. }
  133. spin_lock_irqsave(&adc->lock, flags);
  134. if (client->is_ts && adc->ts_pend) {
  135. spin_unlock_irqrestore(&adc->lock, flags);
  136. return -EAGAIN;
  137. }
  138. client->channel = channel;
  139. client->nr_samples = nr_samples;
  140. if (client->is_ts)
  141. adc->ts_pend = client;
  142. else
  143. list_add_tail(&client->pend, &adc_pending);
  144. if (!adc->cur)
  145. s3c_adc_try(adc);
  146. spin_unlock_irqrestore(&adc->lock, flags);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(s3c_adc_start);
  150. static void s3c_convert_done(struct s3c_adc_client *client,
  151. unsigned v, unsigned u, unsigned *left)
  152. {
  153. client->result = v;
  154. wake_up(client->wait);
  155. }
  156. int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch)
  157. {
  158. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
  159. int ret;
  160. client->convert_cb = s3c_convert_done;
  161. client->wait = &wake;
  162. client->result = -1;
  163. ret = s3c_adc_start(client, ch, 1);
  164. if (ret < 0)
  165. goto err;
  166. ret = wait_event_timeout(wake, client->result >= 0, HZ / 2);
  167. if (client->result < 0) {
  168. ret = -ETIMEDOUT;
  169. goto err;
  170. }
  171. client->convert_cb = NULL;
  172. return client->result;
  173. err:
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(s3c_adc_read);
  177. static void s3c_adc_default_select(struct s3c_adc_client *client,
  178. unsigned select)
  179. {
  180. }
  181. struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev,
  182. void (*select)(struct s3c_adc_client *client,
  183. unsigned int selected),
  184. void (*conv)(struct s3c_adc_client *client,
  185. unsigned d0, unsigned d1,
  186. unsigned *samples_left),
  187. unsigned int is_ts)
  188. {
  189. struct s3c_adc_client *client;
  190. WARN_ON(!pdev);
  191. if (!select)
  192. select = s3c_adc_default_select;
  193. if (!pdev)
  194. return ERR_PTR(-EINVAL);
  195. client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL);
  196. if (!client) {
  197. dev_err(&pdev->dev, "no memory for adc client\n");
  198. return ERR_PTR(-ENOMEM);
  199. }
  200. client->pdev = pdev;
  201. client->is_ts = is_ts;
  202. client->select_cb = select;
  203. client->convert_cb = conv;
  204. return client;