fsl-imx25-gcq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (C) 2014-2015 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License version 2 as published by the
  6. * Free Software Foundation.
  7. *
  8. * This is the driver for the imx25 GCQ (Generic Conversion Queue)
  9. * connected to the imx25 ADC.
  10. */
  11. #include <dt-bindings/iio/adc/fsl-imx25-gcq.h>
  12. #include <linux/clk.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/mfd/imx25-tsadc.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/consumer.h>
  21. #define MX25_GCQ_TIMEOUT (msecs_to_jiffies(2000))
  22. static const char * const driver_name = "mx25-gcq";
  23. enum mx25_gcq_cfgs {
  24. MX25_CFG_XP = 0,
  25. MX25_CFG_YP,
  26. MX25_CFG_XN,
  27. MX25_CFG_YN,
  28. MX25_CFG_WIPER,
  29. MX25_CFG_INAUX0,
  30. MX25_CFG_INAUX1,
  31. MX25_CFG_INAUX2,
  32. MX25_NUM_CFGS,
  33. };
  34. struct mx25_gcq_priv {
  35. struct regmap *regs;
  36. struct completion completed;
  37. struct clk *clk;
  38. int irq;
  39. struct regulator *vref[4];
  40. u32 channel_vref_mv[MX25_NUM_CFGS];
  41. };
  42. #define MX25_CQG_CHAN(chan, id) {\
  43. .type = IIO_VOLTAGE,\
  44. .indexed = 1,\
  45. .channel = chan,\
  46. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  47. BIT(IIO_CHAN_INFO_SCALE),\
  48. .datasheet_name = id,\
  49. }
  50. static const struct iio_chan_spec mx25_gcq_channels[MX25_NUM_CFGS] = {
  51. MX25_CQG_CHAN(MX25_CFG_XP, "xp"),
  52. MX25_CQG_CHAN(MX25_CFG_YP, "yp"),
  53. MX25_CQG_CHAN(MX25_CFG_XN, "xn"),
  54. MX25_CQG_CHAN(MX25_CFG_YN, "yn"),
  55. MX25_CQG_CHAN(MX25_CFG_WIPER, "wiper"),
  56. MX25_CQG_CHAN(MX25_CFG_INAUX0, "inaux0"),
  57. MX25_CQG_CHAN(MX25_CFG_INAUX1, "inaux1"),
  58. MX25_CQG_CHAN(MX25_CFG_INAUX2, "inaux2"),
  59. };
  60. static const char * const mx25_gcq_refp_names[] = {
  61. [MX25_ADC_REFP_YP] = "yp",
  62. [MX25_ADC_REFP_XP] = "xp",
  63. [MX25_ADC_REFP_INT] = "int",
  64. [MX25_ADC_REFP_EXT] = "ext",
  65. };
  66. static irqreturn_t mx25_gcq_irq(int irq, void *data)
  67. {
  68. struct mx25_gcq_priv *priv = data;
  69. u32 stats;
  70. regmap_read(priv->regs, MX25_ADCQ_SR, &stats);
  71. if (stats & MX25_ADCQ_SR_EOQ) {
  72. regmap_update_bits(priv->regs, MX25_ADCQ_MR,
  73. MX25_ADCQ_MR_EOQ_IRQ, MX25_ADCQ_MR_EOQ_IRQ);
  74. complete(&priv->completed);
  75. }
  76. /* Disable conversion queue run */
  77. regmap_update_bits(priv->regs, MX25_ADCQ_CR, MX25_ADCQ_CR_FQS, 0);
  78. /* Acknowledge all possible irqs */
  79. regmap_write(priv->regs, MX25_ADCQ_SR, MX25_ADCQ_SR_FRR |
  80. MX25_ADCQ_SR_FUR | MX25_ADCQ_SR_FOR |
  81. MX25_ADCQ_SR_EOQ | MX25_ADCQ_SR_PD);
  82. return IRQ_HANDLED;
  83. }
  84. static int mx25_gcq_get_raw_value(struct device *dev,
  85. struct iio_chan_spec const *chan,
  86. struct mx25_gcq_priv *priv,
  87. int *val)
  88. {
  89. long timeout;
  90. u32 data;
  91. /* Setup the configuration we want to use */
  92. regmap_write(priv->regs, MX25_ADCQ_ITEM_7_0,
  93. MX25_ADCQ_ITEM(0, chan->channel));
  94. regmap_update_bits(priv->regs, MX25_ADCQ_MR, MX25_ADCQ_MR_EOQ_IRQ, 0);
  95. /* Trigger queue for one run */
  96. regmap_update_bits(priv->regs, MX25_ADCQ_CR, MX25_ADCQ_CR_FQS,
  97. MX25_ADCQ_CR_FQS);
  98. timeout = wait_for_completion_interruptible_timeout(
  99. &priv->completed, MX25_GCQ_TIMEOUT);
  100. if (timeout < 0) {
  101. dev_err(dev, "ADC wait for measurement failed\n");
  102. return timeout;
  103. } else if (timeout == 0) {
  104. dev_err(dev, "ADC timed out\n");
  105. return -ETIMEDOUT;
  106. }
  107. regmap_read(priv->regs, MX25_ADCQ_FIFO, &data);
  108. *val = MX25_ADCQ_FIFO_DATA(data);
  109. return IIO_VAL_INT;
  110. }
  111. static int mx25_gcq_read_raw(struct iio_dev *indio_dev,
  112. struct iio_chan_spec const *chan, int *val,
  113. int *val2, long mask)
  114. {
  115. struct mx25_gcq_priv *priv = iio_priv(indio_dev);
  116. int ret;
  117. switch (mask) {
  118. case IIO_CHAN_INFO_RAW:
  119. mutex_lock(&indio_dev->mlock);
  120. ret = mx25_gcq_get_raw_value(&indio_dev->dev, chan, priv, val);
  121. mutex_unlock(&indio_dev->mlock);
  122. return ret;
  123. case IIO_CHAN_INFO_SCALE:
  124. *val = priv->channel_vref_mv[chan->channel];
  125. *val2 = 12;
  126. return IIO_VAL_FRACTIONAL_LOG2;
  127. default:
  128. return -EINVAL;
  129. }
  130. }
  131. static const struct iio_info mx25_gcq_iio_info = {
  132. .read_raw = mx25_gcq_read_raw,
  133. };
  134. static const struct regmap_config mx25_gcq_regconfig = {
  135. .max_register = 0x5c,
  136. .reg_bits = 32,
  137. .val_bits = 32,
  138. .reg_stride = 4,
  139. };
  140. static int mx25_gcq_setup_cfgs(struct platform_device *pdev,
  141. struct mx25_gcq_priv *priv)
  142. {
  143. struct device_node *np = pdev->dev.of_node;
  144. struct device_node *child;
  145. struct device *dev = &pdev->dev;
  146. unsigned int refp_used[4] = {};
  147. int ret, i;
  148. /*
  149. * Setup all configurations registers with a default conversion
  150. * configuration for each input
  151. */
  152. for (i = 0; i < MX25_NUM_CFGS; ++i)
  153. regmap_write(priv->regs, MX25_ADCQ_CFG(i),
  154. MX25_ADCQ_CFG_YPLL_OFF |
  155. MX25_ADCQ_CFG_XNUR_OFF |
  156. MX25_ADCQ_CFG_XPUL_OFF |
  157. MX25_ADCQ_CFG_REFP_INT |
  158. MX25_ADCQ_CFG_IN(i) |
  159. MX25_ADCQ_CFG_REFN_NGND2);
  160. /*
  161. * First get all regulators to store them in channel_vref_mv if
  162. * necessary. Later we use that information for proper IIO scale
  163. * information.
  164. */
  165. priv->vref[MX25_ADC_REFP_INT] = NULL;
  166. priv->vref[MX25_ADC_REFP_EXT] =
  167. devm_regulator_get_optional(&pdev->dev, "vref-ext");
  168. priv->vref[MX25_ADC_REFP_XP] =
  169. devm_regulator_get_optional(&pdev->dev, "vref-xp");
  170. priv->vref[MX25_ADC_REFP_YP] =
  171. devm_regulator_get_optional(&pdev->dev, "vref-yp");
  172. for_each_child_of_node(np, child) {
  173. u32 reg;
  174. u32 refp = MX25_ADCQ_CFG_REFP_INT;
  175. u32 refn = MX25_ADCQ_CFG_REFN_NGND2;
  176. ret = of_property_read_u32(child, "reg", &reg);
  177. if (ret) {
  178. dev_err(dev, "Failed to get reg property\n");
  179. return ret;
  180. }
  181. if (reg >= MX25_NUM_CFGS) {
  182. dev_err(dev,
  183. "reg value is greater than the number of available configuration registers\n");
  184. return -EINVAL;
  185. }
  186. of_property_read_u32(child, "fsl,adc-refp", &refp);
  187. of_property_read_u32(child, "fsl,adc-refn", &refn);
  188. switch (refp) {
  189. case MX25_ADC_REFP_EXT:
  190. case MX25_ADC_REFP_XP:
  191. case MX25_ADC_REFP_YP:
  192. if (IS_ERR(priv->vref[refp])) {
  193. dev_err(dev, "Error, trying to use external voltage reference without a vref-%s regulator.",
  194. mx25_gcq_refp_names[refp]);
  195. return PTR_ERR(priv->vref[refp]);
  196. }
  197. priv->channel_vref_mv[reg] =
  198. regulator_get_voltage(priv->vref[refp]);
  199. /* Conversion from uV to mV */
  200. priv->channel_vref_mv[reg] /= 1000;
  201. break;
  202. case MX25_ADC_REFP_INT:
  203. priv->channel_vref_mv[reg] = 2500;
  204. break;
  205. default:
  206. dev_err(dev, "Invalid positive reference %d\n", refp);
  207. return -EINVAL;
  208. }
  209. ++refp_used[refp];
  210. /*
  211. * Shift the read values to the correct positions within the
  212. * register.
  213. */
  214. refp = MX25_ADCQ_CFG_REFP(refp);
  215. refn = MX25_ADCQ_CFG_REFN(refn);
  216. if ((refp & MX25_ADCQ_CFG_REFP_MASK) != refp) {
  217. dev_err(dev, "Invalid fsl,adc-refp property value\n");
  218. return -EINVAL;
  219. }
  220. if ((refn & MX25_ADCQ_CFG_REFN_MASK) != refn) {
  221. dev_err(dev, "Invalid fsl,adc-refn property value\n");
  222. return -EINVAL;
  223. }
  224. regmap_update_bits(priv->regs, MX25_ADCQ_CFG(reg),
  225. MX25_ADCQ_CFG_REFP_MASK |
  226. MX25_ADCQ_CFG_REFN_MASK,
  227. refp | refn);
  228. }
  229. regmap_update_bits(priv->regs, MX25_ADCQ_CR,
  230. MX25_ADCQ_CR_FRST | MX25_ADCQ_CR_QRST,
  231. MX25_ADCQ_CR_FRST | MX25_ADCQ_CR_QRST);
  232. regmap_write(priv->regs, MX25_ADCQ_CR,
  233. MX25_ADCQ_CR_PDMSK | MX25_ADCQ_CR_QSM_FQS);
  234. /* Remove unused regulators */
  235. for (i = 0; i != 4; ++i) {
  236. if (!refp_used[i]) {
  237. if (!IS_ERR_OR_NULL(priv->vref[i]))
  238. devm_regulator_put(priv->vref[i]);
  239. priv->vref[i] = NULL;
  240. }
  241. }
  242. return 0;
  243. }
  244. static int mx25_gcq_probe(struct platform_device *pdev)
  245. {
  246. struct iio_dev *indio_dev;
  247. struct mx25_gcq_priv *priv;
  248. struct mx25_tsadc *tsadc = dev_get_drvdata(pdev->dev.parent);
  249. struct device *dev = &pdev->dev;
  250. struct resource *res;
  251. void __iomem *mem;
  252. int ret;
  253. int i;
  254. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
  255. if (!indio_dev)
  256. return -ENOMEM;
  257. priv = iio_priv(indio_dev);
  258. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  259. mem = devm_ioremap_resource(dev, res);
  260. if (IS_ERR(mem))
  261. return PTR_ERR(mem);
  262. priv->regs = devm_regmap_init_mmio(dev, mem, &mx25_gcq_regconfig);
  263. if (IS_ERR(priv->regs)) {
  264. dev_err(dev, "Failed to initialize regmap\n");
  265. return PTR_ERR(priv->regs);
  266. }
  267. init_completion(&priv->completed);
  268. ret = mx25_gcq_setup_cfgs(pdev, priv);
  269. if (ret)
  270. return ret;
  271. for (i = 0; i != 4; ++i) {
  272. if (!priv->vref[i])
  273. continue;
  274. ret = regulator_enable(priv->vref[i]);
  275. if (ret)
  276. goto err_regulator_disable;
  277. }
  278. priv->clk = tsadc->clk;
  279. ret = clk_prepare_enable(priv->clk);
  280. if (ret) {
  281. dev_err(dev, "Failed to enable clock\n");
  282. goto err_vref_disable;
  283. }
  284. priv->irq = platform_get_irq(pdev, 0);
  285. if (priv->irq <= 0) {
  286. dev_err(dev, "Failed to get IRQ\n");
  287. ret = priv->irq;
  288. if (!ret)
  289. ret = -ENXIO;
  290. goto err_clk_unprepare;
  291. }
  292. ret = request_irq(priv->irq, mx25_gcq_irq, 0, pdev->name, priv);
  293. if (ret) {
  294. dev_err(dev, "Failed requesting IRQ\n");
  295. goto err_clk_unprepare;
  296. }
  297. indio_dev->dev.parent = &pdev->dev;
  298. indio_dev->channels = mx25_gcq_channels;
  299. indio_dev->num_channels = ARRAY_SIZE(mx25_gcq_channels);
  300. indio_dev->info = &mx25_gcq_iio_info;
  301. indio_dev->name = driver_name;
  302. ret = iio_device_register(indio_dev);
  303. if (ret) {
  304. dev_err(dev, "Failed to register iio device\n");
  305. goto err_irq_free;
  306. }
  307. platform_set_drvdata(pdev, indio_dev);
  308. return 0;
  309. err_irq_free:
  310. free_irq(priv->irq, priv);
  311. err_clk_unprepare:
  312. clk_disable_unprepare(priv->clk);
  313. err_vref_disable:
  314. i = 4;
  315. err_regulator_disable:
  316. for (; i-- > 0;) {
  317. if (priv->vref[i])
  318. regulator_disable(priv->vref[i]);
  319. }
  320. return ret;
  321. }
  322. static int mx25_gcq_remove(struct platform_device *pdev)
  323. {
  324. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  325. struct mx25_gcq_priv *priv = iio_priv(indio_dev);
  326. int i;
  327. iio_device_unregister(indio_dev);
  328. free_irq(priv->irq, priv);
  329. clk_disable_unprepare(priv->clk);
  330. for (i = 4; i-- > 0;) {
  331. if (priv->vref[i])
  332. regulator_disable(priv->vref[i]);
  333. }
  334. return 0;
  335. }
  336. static const struct of_device_id mx25_gcq_ids[] = {
  337. { .compatible = "fsl,imx25-gcq", },
  338. { /* Sentinel */ }
  339. };
  340. MODULE_DEVICE_TABLE(of, mx25_gcq_ids);
  341. static struct platform_driver mx25_gcq_driver = {
  342. .driver = {
  343. .name = "mx25-gcq",
  344. .of_match_table = mx25_gcq_ids,
  345. },
  346. .probe = mx25_gcq_probe,
  347. .remove = mx25_gcq_remove,
  348. };
  349. module_platform_driver(mx25_gcq_driver);
  350. MODULE_DESCRIPTION("ADC driver for Freescale mx25");
  351. MODULE_AUTHOR("Markus Pargmann <mpa@pengutronix.de>");
  352. MODULE_LICENSE("GPL v2");