berlin2-adc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Marvell Berlin2 ADC driver
  3. *
  4. * Copyright (C) 2015 Marvell Technology Group Ltd.
  5. *
  6. * Antoine Tenart <antoine.tenart@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/driver.h>
  14. #include <linux/iio/machine.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #define BERLIN2_SM_CTRL 0x14
  25. #define BERLIN2_SM_CTRL_SM_SOC_INT BIT(1)
  26. #define BERLIN2_SM_CTRL_SOC_SM_INT BIT(2)
  27. #define BERLIN2_SM_CTRL_ADC_SEL(x) ((x) << 5) /* 0-15 */
  28. #define BERLIN2_SM_CTRL_ADC_SEL_MASK GENMASK(8, 5)
  29. #define BERLIN2_SM_CTRL_ADC_POWER BIT(9)
  30. #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV2 (0x0 << 10)
  31. #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV3 (0x1 << 10)
  32. #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV4 (0x2 << 10)
  33. #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV8 (0x3 << 10)
  34. #define BERLIN2_SM_CTRL_ADC_CLKSEL_MASK GENMASK(11, 10)
  35. #define BERLIN2_SM_CTRL_ADC_START BIT(12)
  36. #define BERLIN2_SM_CTRL_ADC_RESET BIT(13)
  37. #define BERLIN2_SM_CTRL_ADC_BANDGAP_RDY BIT(14)
  38. #define BERLIN2_SM_CTRL_ADC_CONT_SINGLE (0x0 << 15)
  39. #define BERLIN2_SM_CTRL_ADC_CONT_CONTINUOUS (0x1 << 15)
  40. #define BERLIN2_SM_CTRL_ADC_BUFFER_EN BIT(16)
  41. #define BERLIN2_SM_CTRL_ADC_VREF_EXT (0x0 << 17)
  42. #define BERLIN2_SM_CTRL_ADC_VREF_INT (0x1 << 17)
  43. #define BERLIN2_SM_CTRL_ADC_ROTATE BIT(19)
  44. #define BERLIN2_SM_CTRL_TSEN_EN BIT(20)
  45. #define BERLIN2_SM_CTRL_TSEN_CLK_SEL_125 (0x0 << 21) /* 1.25 MHz */
  46. #define BERLIN2_SM_CTRL_TSEN_CLK_SEL_250 (0x1 << 21) /* 2.5 MHz */
  47. #define BERLIN2_SM_CTRL_TSEN_MODE_0_125 (0x0 << 22) /* 0-125 C */
  48. #define BERLIN2_SM_CTRL_TSEN_MODE_10_50 (0x1 << 22) /* 10-50 C */
  49. #define BERLIN2_SM_CTRL_TSEN_RESET BIT(29)
  50. #define BERLIN2_SM_ADC_DATA 0x20
  51. #define BERLIN2_SM_ADC_MASK GENMASK(9, 0)
  52. #define BERLIN2_SM_ADC_STATUS 0x1c
  53. #define BERLIN2_SM_ADC_STATUS_DATA_RDY(x) BIT(x) /* 0-15 */
  54. #define BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK GENMASK(15, 0)
  55. #define BERLIN2_SM_ADC_STATUS_INT_EN(x) (BIT(x) << 16) /* 0-15 */
  56. #define BERLIN2_SM_ADC_STATUS_INT_EN_MASK GENMASK(31, 16)
  57. #define BERLIN2_SM_TSEN_STATUS 0x24
  58. #define BERLIN2_SM_TSEN_STATUS_DATA_RDY BIT(0)
  59. #define BERLIN2_SM_TSEN_STATUS_INT_EN BIT(1)
  60. #define BERLIN2_SM_TSEN_DATA 0x28
  61. #define BERLIN2_SM_TSEN_MASK GENMASK(9, 0)
  62. #define BERLIN2_SM_TSEN_CTRL 0x74
  63. #define BERLIN2_SM_TSEN_CTRL_START BIT(8)
  64. #define BERLIN2_SM_TSEN_CTRL_SETTLING_4 (0x0 << 21) /* 4 us */
  65. #define BERLIN2_SM_TSEN_CTRL_SETTLING_12 (0x1 << 21) /* 12 us */
  66. #define BERLIN2_SM_TSEN_CTRL_SETTLING_MASK BIT(21)
  67. #define BERLIN2_SM_TSEN_CTRL_TRIM(x) ((x) << 22)
  68. #define BERLIN2_SM_TSEN_CTRL_TRIM_MASK GENMASK(25, 22)
  69. struct berlin2_adc_priv {
  70. struct regmap *regmap;
  71. struct mutex lock;
  72. wait_queue_head_t wq;
  73. bool data_available;
  74. int data;
  75. };
  76. #define BERLIN2_ADC_CHANNEL(n, t) \
  77. { \
  78. .channel = n, \
  79. .datasheet_name = "channel"#n, \
  80. .type = t, \
  81. .indexed = 1, \
  82. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  83. }
  84. static const struct iio_chan_spec berlin2_adc_channels[] = {
  85. BERLIN2_ADC_CHANNEL(0, IIO_VOLTAGE), /* external input */
  86. BERLIN2_ADC_CHANNEL(1, IIO_VOLTAGE), /* external input */
  87. BERLIN2_ADC_CHANNEL(2, IIO_VOLTAGE), /* external input */
  88. BERLIN2_ADC_CHANNEL(3, IIO_VOLTAGE), /* external input */
  89. BERLIN2_ADC_CHANNEL(4, IIO_VOLTAGE), /* reserved */
  90. BERLIN2_ADC_CHANNEL(5, IIO_VOLTAGE), /* reserved */
  91. { /* temperature sensor */
  92. .channel = 6,
  93. .datasheet_name = "channel6",
  94. .type = IIO_TEMP,
  95. .indexed = 0,
  96. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  97. },
  98. BERLIN2_ADC_CHANNEL(7, IIO_VOLTAGE), /* reserved */
  99. IIO_CHAN_SOFT_TIMESTAMP(8), /* timestamp */
  100. };
  101. static int berlin2_adc_read(struct iio_dev *indio_dev, int channel)
  102. {
  103. struct berlin2_adc_priv *priv = iio_priv(indio_dev);
  104. int data, ret;
  105. mutex_lock(&priv->lock);
  106. /* Enable the interrupts */
  107. regmap_write(priv->regmap, BERLIN2_SM_ADC_STATUS,
  108. BERLIN2_SM_ADC_STATUS_INT_EN(channel));
  109. /* Configure the ADC */
  110. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  111. BERLIN2_SM_CTRL_ADC_RESET |
  112. BERLIN2_SM_CTRL_ADC_SEL_MASK |
  113. BERLIN2_SM_CTRL_ADC_START,
  114. BERLIN2_SM_CTRL_ADC_SEL(channel) |
  115. BERLIN2_SM_CTRL_ADC_START);
  116. ret = wait_event_interruptible_timeout(priv->wq, priv->data_available,
  117. msecs_to_jiffies(1000));
  118. /* Disable the interrupts */
  119. regmap_update_bits(priv->regmap, BERLIN2_SM_ADC_STATUS,
  120. BERLIN2_SM_ADC_STATUS_INT_EN(channel), 0);
  121. if (ret == 0)
  122. ret = -ETIMEDOUT;
  123. if (ret < 0) {
  124. mutex_unlock(&priv->lock);
  125. return ret;
  126. }
  127. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  128. BERLIN2_SM_CTRL_ADC_START, 0);
  129. data = priv->data;
  130. priv->data_available = false;
  131. mutex_unlock(&priv->lock);
  132. return data;
  133. }
  134. static int berlin2_adc_tsen_read(struct iio_dev *indio_dev)
  135. {
  136. struct berlin2_adc_priv *priv = iio_priv(indio_dev);
  137. int data, ret;
  138. mutex_lock(&priv->lock);
  139. /* Enable interrupts */
  140. regmap_write(priv->regmap, BERLIN2_SM_TSEN_STATUS,
  141. BERLIN2_SM_TSEN_STATUS_INT_EN);
  142. /* Configure the ADC */
  143. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  144. BERLIN2_SM_CTRL_TSEN_RESET |
  145. BERLIN2_SM_CTRL_ADC_ROTATE,
  146. BERLIN2_SM_CTRL_ADC_ROTATE);
  147. /* Configure the temperature sensor */
  148. regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_CTRL,
  149. BERLIN2_SM_TSEN_CTRL_TRIM_MASK |
  150. BERLIN2_SM_TSEN_CTRL_SETTLING_MASK |
  151. BERLIN2_SM_TSEN_CTRL_START,
  152. BERLIN2_SM_TSEN_CTRL_TRIM(3) |
  153. BERLIN2_SM_TSEN_CTRL_SETTLING_12 |
  154. BERLIN2_SM_TSEN_CTRL_START);
  155. ret = wait_event_interruptible_timeout(priv->wq, priv->data_available,
  156. msecs_to_jiffies(1000));
  157. /* Disable interrupts */
  158. regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_STATUS,
  159. BERLIN2_SM_TSEN_STATUS_INT_EN, 0);
  160. if (ret == 0)
  161. ret = -ETIMEDOUT;
  162. if (ret < 0) {
  163. mutex_unlock(&priv->lock);
  164. return ret;
  165. }
  166. regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_CTRL,
  167. BERLIN2_SM_TSEN_CTRL_START, 0);
  168. data = priv->data;
  169. priv->data_available = false;
  170. mutex_unlock(&priv->lock);
  171. return data;
  172. }
  173. static int berlin2_adc_read_raw(struct iio_dev *indio_dev,
  174. struct iio_chan_spec const *chan, int *val,
  175. int *val2, long mask)
  176. {
  177. int temp;
  178. switch (mask) {
  179. case IIO_CHAN_INFO_RAW:
  180. if (chan->type != IIO_VOLTAGE)
  181. return -EINVAL;
  182. *val = berlin2_adc_read(indio_dev, chan->channel);
  183. if (*val < 0)
  184. return *val;
  185. return IIO_VAL_INT;
  186. case IIO_CHAN_INFO_PROCESSED:
  187. if (chan->type != IIO_TEMP)
  188. return -EINVAL;
  189. temp = berlin2_adc_tsen_read(indio_dev);
  190. if (temp < 0)
  191. return temp;
  192. if (temp > 2047)
  193. temp -= 4096;
  194. /* Convert to milli Celsius */
  195. *val = ((temp * 100000) / 264 - 270000);
  196. return IIO_VAL_INT;
  197. default:
  198. break;
  199. }
  200. return -EINVAL;
  201. }
  202. static irqreturn_t berlin2_adc_irq(int irq, void *private)
  203. {
  204. struct berlin2_adc_priv *priv = iio_priv(private);
  205. unsigned val;
  206. regmap_read(priv->regmap, BERLIN2_SM_ADC_STATUS, &val);
  207. if (val & BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK) {
  208. regmap_read(priv->regmap, BERLIN2_SM_ADC_DATA, &priv->data);
  209. priv->data &= BERLIN2_SM_ADC_MASK;
  210. val &= ~BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK;
  211. regmap_write(priv->regmap, BERLIN2_SM_ADC_STATUS, val);
  212. priv->data_available = true;
  213. wake_up_interruptible(&priv->wq);
  214. }
  215. return IRQ_HANDLED;
  216. }
  217. static irqreturn_t berlin2_adc_tsen_irq(int irq, void *private)
  218. {
  219. struct berlin2_adc_priv *priv = iio_priv(private);
  220. unsigned val;
  221. regmap_read(priv->regmap, BERLIN2_SM_TSEN_STATUS, &val);
  222. if (val & BERLIN2_SM_TSEN_STATUS_DATA_RDY) {
  223. regmap_read(priv->regmap, BERLIN2_SM_TSEN_DATA, &priv->data);
  224. priv->data &= BERLIN2_SM_TSEN_MASK;
  225. val &= ~BERLIN2_SM_TSEN_STATUS_DATA_RDY;
  226. regmap_write(priv->regmap, BERLIN2_SM_TSEN_STATUS, val);
  227. priv->data_available = true;
  228. wake_up_interruptible(&priv->wq);
  229. }
  230. return IRQ_HANDLED;
  231. }
  232. static const struct iio_info berlin2_adc_info = {
  233. .driver_module = THIS_MODULE,
  234. .read_raw = berlin2_adc_read_raw,
  235. };
  236. static int berlin2_adc_probe(struct platform_device *pdev)
  237. {
  238. struct iio_dev *indio_dev;
  239. struct berlin2_adc_priv *priv;
  240. struct device_node *parent_np = of_get_parent(pdev->dev.of_node);
  241. int irq, tsen_irq;
  242. int ret;
  243. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
  244. if (!indio_dev)
  245. return -ENOMEM;
  246. priv = iio_priv(indio_dev);
  247. platform_set_drvdata(pdev, indio_dev);
  248. priv->regmap = syscon_node_to_regmap(parent_np);
  249. of_node_put(parent_np);
  250. if (IS_ERR(priv->regmap))
  251. return PTR_ERR(priv->regmap);
  252. irq = platform_get_irq_byname(pdev, "adc");
  253. if (irq < 0)
  254. return irq;
  255. tsen_irq = platform_get_irq_byname(pdev, "tsen");
  256. if (tsen_irq < 0)
  257. return tsen_irq;
  258. ret = devm_request_irq(&pdev->dev, irq, berlin2_adc_irq, 0,
  259. pdev->dev.driver->name, indio_dev);
  260. if (ret)
  261. return ret;
  262. ret = devm_request_irq(&pdev->dev, tsen_irq, berlin2_adc_tsen_irq,
  263. 0, pdev->dev.driver->name, indio_dev);
  264. if (ret)
  265. return ret;
  266. init_waitqueue_head(&priv->wq);
  267. mutex_init(&priv->lock);
  268. indio_dev->dev.parent = &pdev->dev;
  269. indio_dev->name = dev_name(&pdev->dev);
  270. indio_dev->modes = INDIO_DIRECT_MODE;
  271. indio_dev->info = &berlin2_adc_info;
  272. indio_dev->channels = berlin2_adc_channels;
  273. indio_dev->num_channels = ARRAY_SIZE(berlin2_adc_channels);
  274. /* Power up the ADC */
  275. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  276. BERLIN2_SM_CTRL_ADC_POWER,
  277. BERLIN2_SM_CTRL_ADC_POWER);
  278. ret = iio_device_register(indio_dev);
  279. if (ret) {
  280. /* Power down the ADC */
  281. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  282. BERLIN2_SM_CTRL_ADC_POWER, 0);
  283. return ret;
  284. }
  285. return 0;
  286. }
  287. static int berlin2_adc_remove(struct platform_device *pdev)
  288. {
  289. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  290. struct berlin2_adc_priv *priv = iio_priv(indio_dev);
  291. iio_device_unregister(indio_dev);
  292. /* Power down the ADC */
  293. regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL,
  294. BERLIN2_SM_CTRL_ADC_POWER, 0);
  295. return 0;
  296. }
  297. static const struct of_device_id berlin2_adc_match[] = {
  298. { .compatible = "marvell,berlin2-adc", },
  299. { },
  300. };
  301. MODULE_DEVICE_TABLE(of, berlin2_adc_match);
  302. static struct platform_driver berlin2_adc_driver = {
  303. .driver = {
  304. .name = "berlin2-adc",
  305. .of_match_table = berlin2_adc_match,
  306. },
  307. .probe = berlin2_adc_probe,
  308. .remove = berlin2_adc_remove,
  309. };
  310. module_platform_driver(berlin2_adc_driver);
  311. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  312. MODULE_DESCRIPTION("Marvell Berlin2 ADC driver");
  313. MODULE_LICENSE("GPL v2");