rockchip_saradc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Rockchip Successive Approximation Register (SAR) A/D Converter
  3. * Copyright (C) 2014 ROCKCHIP, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/completion.h>
  23. #include <linux/delay.h>
  24. #include <linux/reset.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/iio/iio.h>
  27. #define SARADC_DATA 0x00
  28. #define SARADC_STAS 0x04
  29. #define SARADC_STAS_BUSY BIT(0)
  30. #define SARADC_CTRL 0x08
  31. #define SARADC_CTRL_IRQ_STATUS BIT(6)
  32. #define SARADC_CTRL_IRQ_ENABLE BIT(5)
  33. #define SARADC_CTRL_POWER_CTRL BIT(3)
  34. #define SARADC_CTRL_CHN_MASK 0x7
  35. #define SARADC_DLY_PU_SOC 0x0c
  36. #define SARADC_DLY_PU_SOC_MASK 0x3f
  37. #define SARADC_TIMEOUT msecs_to_jiffies(100)
  38. struct rockchip_saradc_data {
  39. int num_bits;
  40. const struct iio_chan_spec *channels;
  41. int num_channels;
  42. unsigned long clk_rate;
  43. };
  44. struct rockchip_saradc {
  45. void __iomem *regs;
  46. struct clk *pclk;
  47. struct clk *clk;
  48. struct completion completion;
  49. struct regulator *vref;
  50. struct reset_control *reset;
  51. const struct rockchip_saradc_data *data;
  52. u16 last_val;
  53. };
  54. static int rockchip_saradc_read_raw(struct iio_dev *indio_dev,
  55. struct iio_chan_spec const *chan,
  56. int *val, int *val2, long mask)
  57. {
  58. struct rockchip_saradc *info = iio_priv(indio_dev);
  59. int ret;
  60. switch (mask) {
  61. case IIO_CHAN_INFO_RAW:
  62. mutex_lock(&indio_dev->mlock);
  63. reinit_completion(&info->completion);
  64. /* 8 clock periods as delay between power up and start cmd */
  65. writel_relaxed(8, info->regs + SARADC_DLY_PU_SOC);
  66. /* Select the channel to be used and trigger conversion */
  67. writel(SARADC_CTRL_POWER_CTRL
  68. | (chan->channel & SARADC_CTRL_CHN_MASK)
  69. | SARADC_CTRL_IRQ_ENABLE,
  70. info->regs + SARADC_CTRL);
  71. if (!wait_for_completion_timeout(&info->completion,
  72. SARADC_TIMEOUT)) {
  73. writel_relaxed(0, info->regs + SARADC_CTRL);
  74. mutex_unlock(&indio_dev->mlock);
  75. return -ETIMEDOUT;
  76. }
  77. *val = info->last_val;
  78. mutex_unlock(&indio_dev->mlock);
  79. return IIO_VAL_INT;
  80. case IIO_CHAN_INFO_SCALE:
  81. ret = regulator_get_voltage(info->vref);
  82. if (ret < 0) {
  83. dev_err(&indio_dev->dev, "failed to get voltage\n");
  84. return ret;
  85. }
  86. *val = ret / 1000;
  87. *val2 = info->data->num_bits;
  88. return IIO_VAL_FRACTIONAL_LOG2;
  89. default:
  90. return -EINVAL;
  91. }
  92. }
  93. static irqreturn_t rockchip_saradc_isr(int irq, void *dev_id)
  94. {
  95. struct rockchip_saradc *info = (struct rockchip_saradc *)dev_id;
  96. /* Read value */
  97. info->last_val = readl_relaxed(info->regs + SARADC_DATA);
  98. info->last_val &= GENMASK(info->data->num_bits - 1, 0);
  99. /* Clear irq & power down adc */
  100. writel_relaxed(0, info->regs + SARADC_CTRL);
  101. complete(&info->completion);
  102. return IRQ_HANDLED;
  103. }
  104. static const struct iio_info rockchip_saradc_iio_info = {
  105. .read_raw = rockchip_saradc_read_raw,
  106. .driver_module = THIS_MODULE,
  107. };
  108. #define ADC_CHANNEL(_index, _id) { \
  109. .type = IIO_VOLTAGE, \
  110. .indexed = 1, \
  111. .channel = _index, \
  112. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  113. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  114. .datasheet_name = _id, \
  115. }
  116. static const struct iio_chan_spec rockchip_saradc_iio_channels[] = {
  117. ADC_CHANNEL(0, "adc0"),
  118. ADC_CHANNEL(1, "adc1"),
  119. ADC_CHANNEL(2, "adc2"),
  120. };
  121. static const struct rockchip_saradc_data saradc_data = {
  122. .num_bits = 10,
  123. .channels = rockchip_saradc_iio_channels,
  124. .num_channels = ARRAY_SIZE(rockchip_saradc_iio_channels),
  125. .clk_rate = 1000000,
  126. };
  127. static const struct iio_chan_spec rockchip_rk3066_tsadc_iio_channels[] = {
  128. ADC_CHANNEL(0, "adc0"),
  129. ADC_CHANNEL(1, "adc1"),
  130. };
  131. static const struct rockchip_saradc_data rk3066_tsadc_data = {
  132. .num_bits = 12,
  133. .channels = rockchip_rk3066_tsadc_iio_channels,
  134. .num_channels = ARRAY_SIZE(rockchip_rk3066_tsadc_iio_channels),
  135. .clk_rate = 50000,
  136. };
  137. static const struct iio_chan_spec rockchip_rk3399_saradc_iio_channels[] = {
  138. ADC_CHANNEL(0, "adc0"),
  139. ADC_CHANNEL(1, "adc1"),
  140. ADC_CHANNEL(2, "adc2"),
  141. ADC_CHANNEL(3, "adc3"),
  142. ADC_CHANNEL(4, "adc4"),
  143. ADC_CHANNEL(5, "adc5"),
  144. };
  145. static const struct rockchip_saradc_data rk3399_saradc_data = {
  146. .num_bits = 10,
  147. .channels = rockchip_rk3399_saradc_iio_channels,
  148. .num_channels = ARRAY_SIZE(rockchip_rk3399_saradc_iio_channels),
  149. .clk_rate = 1000000,
  150. };
  151. static const struct of_device_id rockchip_saradc_match[] = {
  152. {
  153. .compatible = "rockchip,saradc",
  154. .data = &saradc_data,
  155. }, {
  156. .compatible = "rockchip,rk3066-tsadc",
  157. .data = &rk3066_tsadc_data,
  158. }, {
  159. .compatible = "rockchip,rk3399-saradc",
  160. .data = &rk3399_saradc_data,
  161. },
  162. {},
  163. };
  164. MODULE_DEVICE_TABLE(of, rockchip_saradc_match);
  165. /**
  166. * Reset SARADC Controller.
  167. */
  168. static void rockchip_saradc_reset_controller(struct reset_control *reset)
  169. {
  170. reset_control_assert(reset);
  171. usleep_range(10, 20);
  172. reset_control_deassert(reset);
  173. }
  174. static int rockchip_saradc_probe(struct platform_device *pdev)
  175. {
  176. struct rockchip_saradc *info = NULL;
  177. struct device_node *np = pdev->dev.of_node;
  178. struct iio_dev *indio_dev = NULL;
  179. struct resource *mem;
  180. const struct of_device_id *match;
  181. int ret;
  182. int irq;
  183. if (!np)
  184. return -ENODEV;
  185. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  186. if (!indio_dev) {
  187. dev_err(&pdev->dev, "failed allocating iio device\n");
  188. return -ENOMEM;
  189. }
  190. info = iio_priv(indio_dev);
  191. match = of_match_device(rockchip_saradc_match, &pdev->dev);
  192. info->data = match->data;
  193. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  194. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  195. if (IS_ERR(info->regs))
  196. return PTR_ERR(info->regs);
  197. /*
  198. * The reset should be an optional property, as it should work
  199. * with old devicetrees as well
  200. */
  201. info->reset = devm_reset_control_get(&pdev->dev, "saradc-apb");
  202. if (IS_ERR(info->reset)) {
  203. ret = PTR_ERR(info->reset);
  204. if (ret != -ENOENT)
  205. return ret;
  206. dev_dbg(&pdev->dev, "no reset control found\n");
  207. info->reset = NULL;
  208. }
  209. init_completion(&info->completion);
  210. irq = platform_get_irq(pdev, 0);
  211. if (irq < 0) {
  212. dev_err(&pdev->dev, "no irq resource?\n");
  213. return irq;
  214. }
  215. ret = devm_request_irq(&pdev->dev, irq, rockchip_saradc_isr,
  216. 0, dev_name(&pdev->dev), info);
  217. if (ret < 0) {
  218. dev_err(&pdev->dev, "failed requesting irq %d\n", irq);
  219. return ret;
  220. }
  221. info->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  222. if (IS_ERR(info->pclk)) {
  223. dev_err(&pdev->dev, "failed to get pclk\n");
  224. return PTR_ERR(info->pclk);
  225. }
  226. info->clk = devm_clk_get(&pdev->dev, "saradc");
  227. if (IS_ERR(info->clk)) {
  228. dev_err(&pdev->dev, "failed to get adc clock\n");
  229. return PTR_ERR(info->clk);
  230. }
  231. info->vref = devm_regulator_get(&pdev->dev, "vref");
  232. if (IS_ERR(info->vref)) {
  233. dev_err(&pdev->dev, "failed to get regulator, %ld\n",
  234. PTR_ERR(info->vref));
  235. return PTR_ERR(info->vref);
  236. }
  237. if (info->reset)
  238. rockchip_saradc_reset_controller(info->reset);
  239. /*
  240. * Use a default value for the converter clock.
  241. * This may become user-configurable in the future.
  242. */
  243. ret = clk_set_rate(info->clk, info->data->clk_rate);
  244. if (ret < 0) {
  245. dev_err(&pdev->dev, "failed to set adc clk rate, %d\n", ret);
  246. return ret;
  247. }
  248. ret = regulator_enable(info->vref);
  249. if (ret < 0) {
  250. dev_err(&pdev->dev, "failed to enable vref regulator\n");
  251. return ret;
  252. }
  253. ret = clk_prepare_enable(info->pclk);
  254. if (ret < 0) {
  255. dev_err(&pdev->dev, "failed to enable pclk\n");
  256. goto err_reg_voltage;
  257. }
  258. ret = clk_prepare_enable(info->clk);
  259. if (ret < 0) {
  260. dev_err(&pdev->dev, "failed to enable converter clock\n");
  261. goto err_pclk;
  262. }
  263. platform_set_drvdata(pdev, indio_dev);
  264. indio_dev->name = dev_name(&pdev->dev);
  265. indio_dev->dev.parent = &pdev->dev;
  266. indio_dev->dev.of_node = pdev->dev.of_node;
  267. indio_dev->info = &rockchip_saradc_iio_info;
  268. indio_dev->modes = INDIO_DIRECT_MODE;
  269. indio_dev->channels = info->data->channels;
  270. indio_dev->num_channels = info->data->num_channels;
  271. ret = iio_device_register(indio_dev);
  272. if (ret)
  273. goto err_clk;
  274. return 0;
  275. err_clk:
  276. clk_disable_unprepare(info->clk);
  277. err_pclk:
  278. clk_disable_unprepare(info->pclk);
  279. err_reg_voltage:
  280. regulator_disable(info->vref);
  281. return ret;
  282. }
  283. static int rockchip_saradc_remove(struct platform_device *pdev)
  284. {
  285. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  286. struct rockchip_saradc *info = iio_priv(indio_dev);
  287. iio_device_unregister(indio_dev);
  288. clk_disable_unprepare(info->clk);
  289. clk_disable_unprepare(info->pclk);
  290. regulator_disable(info->vref);
  291. return 0;
  292. }
  293. #ifdef CONFIG_PM_SLEEP
  294. static int rockchip_saradc_suspend(struct device *dev)
  295. {
  296. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  297. struct rockchip_saradc *info = iio_priv(indio_dev);
  298. clk_disable_unprepare(info->clk);
  299. clk_disable_unprepare(info->pclk);
  300. regulator_disable(info->vref);
  301. return 0;
  302. }
  303. static int rockchip_saradc_resume(struct device *dev)
  304. {
  305. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  306. struct rockchip_saradc *info = iio_priv(indio_dev);
  307. int ret;
  308. ret = regulator_enable(info->vref);
  309. if (ret)
  310. return ret;
  311. ret = clk_prepare_enable(info->pclk);
  312. if (ret)
  313. return ret;
  314. ret = clk_prepare_enable(info->clk);
  315. if (ret)
  316. return ret;
  317. return ret;
  318. }
  319. #endif
  320. static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops,
  321. rockchip_saradc_suspend, rockchip_saradc_resume);
  322. static struct platform_driver rockchip_saradc_driver = {
  323. .probe = rockchip_saradc_probe,
  324. .remove = rockchip_saradc_remove,
  325. .driver = {
  326. .name = "rockchip-saradc",
  327. .of_match_table = rockchip_saradc_match,
  328. .pm = &rockchip_saradc_pm_ops,
  329. },
  330. };
  331. module_platform_driver(rockchip_saradc_driver);
  332. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  333. MODULE_DESCRIPTION("Rockchip SARADC driver");
  334. MODULE_LICENSE("GPL v2");