averageDataMemoryDefinition.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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;