spear_adc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ST SPEAr ADC driver
  4. *
  5. * Copyright 2012 Stefan Roese <sr@denx.de>
  6. */
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/property.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/completion.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. /* SPEAR registers definitions */
  23. #define SPEAR600_ADC_SCAN_RATE_LO(x) ((x) & 0xFFFF)
  24. #define SPEAR600_ADC_SCAN_RATE_HI(x) (((x) >> 0x10) & 0xFFFF)
  25. #define SPEAR_ADC_CLK_LOW(x) (((x) & 0xf) << 0)
  26. #define SPEAR_ADC_CLK_HIGH(x) (((x) & 0xf) << 4)
  27. /* Bit definitions for SPEAR_ADC_STATUS */
  28. #define SPEAR_ADC_STATUS_START_CONVERSION BIT(0)
  29. #define SPEAR_ADC_STATUS_CHANNEL_NUM_MASK GENMASK(3, 1)
  30. #define SPEAR_ADC_STATUS_ADC_ENABLE BIT(4)
  31. #define SPEAR_ADC_STATUS_AVG_SAMPLE_MASK GENMASK(8, 5)
  32. #define SPEAR_ADC_STATUS_VREF_INTERNAL BIT(9)
  33. #define SPEAR_ADC_DATA_MASK 0x03ff
  34. #define SPEAR_ADC_DATA_BITS 10
  35. #define SPEAR_ADC_MOD_NAME "spear-adc"
  36. #define SPEAR_ADC_CHANNEL_NUM 8
  37. #define SPEAR_ADC_CLK_MIN 2500000
  38. #define SPEAR_ADC_CLK_MAX 20000000
  39. struct adc_regs_spear3xx {
  40. u32 status;
  41. u32 average;
  42. u32 scan_rate;
  43. u32 clk; /* Not avail for 1340 & 1310 */
  44. u32 ch_ctrl[SPEAR_ADC_CHANNEL_NUM];
  45. u32 ch_data[SPEAR_ADC_CHANNEL_NUM];
  46. };
  47. struct chan_data {
  48. u32 lsb;
  49. u32 msb;
  50. };
  51. struct adc_regs_spear6xx {
  52. u32 status;
  53. u32 pad[2];
  54. u32 clk;
  55. u32 ch_ctrl[SPEAR_ADC_CHANNEL_NUM];
  56. struct chan_data ch_data[SPEAR_ADC_CHANNEL_NUM];
  57. u32 scan_rate_lo;
  58. u32 scan_rate_hi;
  59. struct chan_data average;
  60. };
  61. struct spear_adc_state {
  62. struct device *dev;
  63. struct adc_regs_spear3xx __iomem *adc_base_spear3xx;
  64. struct adc_regs_spear6xx __iomem *adc_base_spear6xx;
  65. struct clk *clk;
  66. struct completion completion;
  67. /*
  68. * Lock to protect the device state during a potential concurrent
  69. * read access from userspace. Reading a raw value requires a sequence
  70. * of register writes, then a wait for a completion callback,
  71. * and finally a register read, during which userspace could issue
  72. * another read request. This lock protects a read access from
  73. * ocurring before another one has finished.
  74. */
  75. struct mutex lock;
  76. u32 current_clk;
  77. u32 sampling_freq;
  78. u32 avg_samples;
  79. u32 vref_external;
  80. u32 value;
  81. };
  82. /*
  83. * Functions to access some SPEAr ADC register. Abstracted into
  84. * static inline functions, because of different register offsets
  85. * on different SoC variants (SPEAr300 vs SPEAr600 etc).
  86. */
  87. static void spear_adc_set_status(struct spear_adc_state *st, u32 val)
  88. {
  89. __raw_writel(val, &st->adc_base_spear6xx->status);
  90. }
  91. static void spear_adc_set_clk(struct spear_adc_state *st, u32 val)
  92. {
  93. u32 clk_high, clk_low, count;
  94. u32 apb_clk = clk_get_rate(st->clk);
  95. count = DIV_ROUND_UP(apb_clk, val);
  96. clk_low = count / 2;
  97. clk_high = count - clk_low;
  98. st->current_clk = apb_clk / count;
  99. __raw_writel(SPEAR_ADC_CLK_LOW(clk_low) | SPEAR_ADC_CLK_HIGH(clk_high),
  100. &st->adc_base_spear6xx->clk);
  101. }
  102. static void spear_adc_set_ctrl(struct spear_adc_state *st, int n,
  103. u32 val)
  104. {
  105. __raw_writel(val, &st->adc_base_spear6xx->ch_ctrl[n]);
  106. }
  107. static u32 spear_adc_get_average(struct spear_adc_state *st)
  108. {
  109. if (device_is_compatible(st->dev, "st,spear600-adc")) {
  110. return __raw_readl(&st->adc_base_spear6xx->average.msb) &
  111. SPEAR_ADC_DATA_MASK;
  112. } else {
  113. return __raw_readl(&st->adc_base_spear3xx->average) &
  114. SPEAR_ADC_DATA_MASK;
  115. }
  116. }
  117. static void spear_adc_set_scanrate(struct spear_adc_state *st, u32 rate)
  118. {
  119. if (device_is_compatible(st->dev, "st,spear600-adc")) {
  120. __raw_writel(SPEAR600_ADC_SCAN_RATE_LO(rate),
  121. &st->adc_base_spear6xx->scan_rate_lo);
  122. __raw_writel(SPEAR600_ADC_SCAN_RATE_HI(rate),
  123. &st->adc_base_spear6xx->scan_rate_hi);
  124. } else {
  125. __raw_writel(rate, &st->adc_base_spear3xx->scan_rate);
  126. }
  127. }
  128. static int spear_adc_read_raw(struct iio_dev *indio_dev,
  129. struct iio_chan_spec const *chan,
  130. int *val,
  131. int *val2,
  132. long mask)
  133. {
  134. struct spear_adc_state *st = iio_priv(indio_dev);
  135. u32 status;
  136. switch (mask) {
  137. case IIO_CHAN_INFO_RAW:
  138. mutex_lock(&st->lock);
  139. status = FIELD_PREP(SPEAR_ADC_STATUS_CHANNEL_NUM_MASK, chan->channel) |
  140. FIELD_PREP(SPEAR_ADC_STATUS_AVG_SAMPLE_MASK, st->avg_samples) |
  141. SPEAR_ADC_STATUS_START_CONVERSION |
  142. SPEAR_ADC_STATUS_ADC_ENABLE;
  143. if (st->vref_external == 0)
  144. status |= SPEAR_ADC_STATUS_VREF_INTERNAL;
  145. spear_adc_set_status(st, status);
  146. wait_for_completion(&st->completion); /* set by ISR */
  147. *val = st->value;
  148. mutex_unlock(&st->lock);
  149. return IIO_VAL_INT;
  150. case IIO_CHAN_INFO_SCALE:
  151. *val = st->vref_external;
  152. *val2 = SPEAR_ADC_DATA_BITS;
  153. return IIO_VAL_FRACTIONAL_LOG2;
  154. case IIO_CHAN_INFO_SAMP_FREQ:
  155. *val = st->current_clk;
  156. return IIO_VAL_INT;
  157. }
  158. return -EINVAL;
  159. }
  160. static int spear_adc_write_raw(struct iio_dev *indio_dev,
  161. struct iio_chan_spec const *chan,
  162. int val,
  163. int val2,
  164. long mask)
  165. {
  166. struct spear_adc_state *st = iio_priv(indio_dev);
  167. int ret = 0;
  168. if (mask != IIO_CHAN_INFO_SAMP_FREQ)
  169. return -EINVAL;
  170. mutex_lock(&st->lock);
  171. if ((val < SPEAR_ADC_CLK_MIN) ||
  172. (val > SPEAR_ADC_CLK_MAX) ||
  173. (val2 != 0)) {
  174. ret = -EINVAL;
  175. goto out;
  176. }
  177. spear_adc_set_clk(st, val);
  178. out:
  179. mutex_unlock(&st->lock);
  180. return ret;
  181. }
  182. #define SPEAR_ADC_CHAN(idx) { \
  183. .type = IIO_VOLTAGE, \
  184. .indexed = 1, \
  185. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  186. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  187. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  188. .channel = idx, \
  189. }
  190. static const struct iio_chan_spec spear_adc_iio_channels[] = {
  191. SPEAR_ADC_CHAN(0),
  192. SPEAR_ADC_CHAN(1),
  193. SPEAR_ADC_CHAN(2),
  194. SPEAR_ADC_CHAN(3),
  195. SPEAR_ADC_CHAN(4),
  196. SPEAR_ADC_CHAN(5),
  197. SPEAR_ADC_CHAN(6),
  198. SPEAR_ADC_CHAN(7),
  199. };
  200. static irqreturn_t spear_adc_isr(int irq, void *dev_id)
  201. {
  202. struct spear_adc_state *st = dev_id;
  203. /* Read value to clear IRQ */
  204. st->value = spear_adc_get_average(st);
  205. complete(&st->completion);
  206. return IRQ_HANDLED;
  207. }
  208. static int spear_adc_configure(struct spear_adc_state *st)
  209. {
  210. int i;
  211. /* Reset ADC core */
  212. spear_adc_set_status(st, 0);
  213. __raw_writel(0, &st->adc_base_spear6xx->clk);
  214. for (i = 0; i < 8; i++)
  215. spear_adc_set_ctrl(st, i, 0);
  216. spear_adc_set_scanrate(st, 0);
  217. spear_adc_set_clk(st, st->sampling_freq);
  218. return 0;
  219. }
  220. static const struct iio_info spear_adc_info = {
  221. .read_raw = &spear_adc_read_raw,
  222. .write_raw = &spear_adc_write_raw,
  223. };
  224. static int spear_adc_probe(struct platform_device *pdev)
  225. {
  226. struct device *dev = &pdev->dev;
  227. struct spear_adc_state *st;
  228. struct iio_dev *indio_dev = NULL;
  229. int ret = -ENODEV;
  230. int irq;
  231. indio_dev = devm_iio_device_alloc(dev, sizeof(struct spear_adc_state));
  232. if (!indio_dev)
  233. return -ENOMEM;
  234. st = iio_priv(indio_dev);
  235. st->dev = dev;
  236. mutex_init(&st->lock);
  237. /*
  238. * SPEAr600 has a different register layout than other SPEAr SoC's
  239. * (e.g. SPEAr3xx). Let's provide two register base addresses
  240. * to support multi-arch kernels.
  241. */
  242. st->adc_base_spear6xx = devm_platform_ioremap_resource(pdev, 0);
  243. if (IS_ERR(st->adc_base_spear6xx))
  244. return PTR_ERR(st->adc_base_spear6xx);
  245. st->adc_base_spear3xx =
  246. (struct adc_regs_spear3xx __iomem *)st->adc_base_spear6xx;
  247. st->clk = devm_clk_get_enabled(dev, NULL);
  248. if (IS_ERR(st->clk))
  249. return dev_err_probe(dev, PTR_ERR(st->clk),
  250. "failed enabling clock\n");
  251. irq = platform_get_irq(pdev, 0);
  252. if (irq < 0)
  253. return irq;
  254. ret = devm_request_irq(dev, irq, spear_adc_isr, 0, SPEAR_ADC_MOD_NAME,
  255. st);
  256. if (ret < 0)
  257. return dev_err_probe(dev, ret, "failed requesting interrupt\n");
  258. if (device_property_read_u32(dev, "sampling-frequency", &st->sampling_freq))
  259. return dev_err_probe(dev, -EINVAL,
  260. "sampling-frequency missing in DT\n");
  261. /*
  262. * Optional avg_samples defaults to 0, resulting in single data
  263. * conversion
  264. */
  265. device_property_read_u32(dev, "average-samples", &st->avg_samples);
  266. /*
  267. * Optional vref_external defaults to 0, resulting in internal vref
  268. * selection
  269. */
  270. device_property_read_u32(dev, "vref-external", &st->vref_external);
  271. spear_adc_configure(st);
  272. init_completion(&st->completion);
  273. indio_dev->name = SPEAR_ADC_MOD_NAME;
  274. indio_dev->info = &spear_adc_info;
  275. indio_dev->modes = INDIO_DIRECT_MODE;
  276. indio_dev->channels = spear_adc_iio_channels;
  277. indio_dev->num_channels = ARRAY_SIZE(spear_adc_iio_channels);
  278. ret = devm_iio_device_register(dev, indio_dev);
  279. if (ret)
  280. return ret;
  281. dev_info(dev, "SPEAR ADC driver loaded, IRQ %d\n", irq);
  282. return 0;
  283. }
  284. static const struct of_device_id spear_adc_dt_ids[] = {
  285. { .compatible = "st,spear600-adc", },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(of, spear_adc_dt_ids);
  289. static struct platform_driver spear_adc_driver = {
  290. .probe = spear_adc_probe,
  291. .driver = {
  292. .name = SPEAR_ADC_MOD_NAME,
  293. .of_match_table = spear_adc_dt_ids,
  294. },
  295. };
  296. module_platform_driver(spear_adc_driver);
  297. MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
  298. MODULE_DESCRIPTION("SPEAr ADC driver");
  299. MODULE_LICENSE("GPL");