spear_adc.c 9.5 KB

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