stm32-adc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * This file is part of STM32 ADC driver
  3. *
  4. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  5. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
  6. *
  7. * License type: GPLv2
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/of.h>
  29. #include "stm32-adc-core.h"
  30. /* STM32F4 - Registers for each ADC instance */
  31. #define STM32F4_ADC_SR 0x00
  32. #define STM32F4_ADC_CR1 0x04
  33. #define STM32F4_ADC_CR2 0x08
  34. #define STM32F4_ADC_SMPR1 0x0C
  35. #define STM32F4_ADC_SMPR2 0x10
  36. #define STM32F4_ADC_HTR 0x24
  37. #define STM32F4_ADC_LTR 0x28
  38. #define STM32F4_ADC_SQR1 0x2C
  39. #define STM32F4_ADC_SQR2 0x30
  40. #define STM32F4_ADC_SQR3 0x34
  41. #define STM32F4_ADC_JSQR 0x38
  42. #define STM32F4_ADC_JDR1 0x3C
  43. #define STM32F4_ADC_JDR2 0x40
  44. #define STM32F4_ADC_JDR3 0x44
  45. #define STM32F4_ADC_JDR4 0x48
  46. #define STM32F4_ADC_DR 0x4C
  47. /* STM32F4_ADC_SR - bit fields */
  48. #define STM32F4_STRT BIT(4)
  49. #define STM32F4_EOC BIT(1)
  50. /* STM32F4_ADC_CR1 - bit fields */
  51. #define STM32F4_SCAN BIT(8)
  52. #define STM32F4_EOCIE BIT(5)
  53. /* STM32F4_ADC_CR2 - bit fields */
  54. #define STM32F4_SWSTART BIT(30)
  55. #define STM32F4_EXTEN_MASK GENMASK(29, 28)
  56. #define STM32F4_EOCS BIT(10)
  57. #define STM32F4_ADON BIT(0)
  58. /* STM32F4_ADC_SQR1 - bit fields */
  59. #define STM32F4_L_SHIFT 20
  60. #define STM32F4_L_MASK GENMASK(23, 20)
  61. /* STM32F4_ADC_SQR3 - bit fields */
  62. #define STM32F4_SQ1_SHIFT 0
  63. #define STM32F4_SQ1_MASK GENMASK(4, 0)
  64. #define STM32_ADC_TIMEOUT_US 100000
  65. #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000))
  66. /**
  67. * struct stm32_adc - private data of each ADC IIO instance
  68. * @common: reference to ADC block common data
  69. * @offset: ADC instance register offset in ADC block
  70. * @completion: end of single conversion completion
  71. * @buffer: data buffer
  72. * @clk: clock for this adc instance
  73. * @irq: interrupt for this adc instance
  74. * @lock: spinlock
  75. */
  76. struct stm32_adc {
  77. struct stm32_adc_common *common;
  78. u32 offset;
  79. struct completion completion;
  80. u16 *buffer;
  81. struct clk *clk;
  82. int irq;
  83. spinlock_t lock; /* interrupt lock */
  84. };
  85. /**
  86. * struct stm32_adc_chan_spec - specification of stm32 adc channel
  87. * @type: IIO channel type
  88. * @channel: channel number (single ended)
  89. * @name: channel name (single ended)
  90. */
  91. struct stm32_adc_chan_spec {
  92. enum iio_chan_type type;
  93. int channel;
  94. const char *name;
  95. };
  96. /* Input definitions common for all STM32F4 instances */
  97. static const struct stm32_adc_chan_spec stm32f4_adc123_channels[] = {
  98. { IIO_VOLTAGE, 0, "in0" },
  99. { IIO_VOLTAGE, 1, "in1" },
  100. { IIO_VOLTAGE, 2, "in2" },
  101. { IIO_VOLTAGE, 3, "in3" },
  102. { IIO_VOLTAGE, 4, "in4" },
  103. { IIO_VOLTAGE, 5, "in5" },
  104. { IIO_VOLTAGE, 6, "in6" },
  105. { IIO_VOLTAGE, 7, "in7" },
  106. { IIO_VOLTAGE, 8, "in8" },
  107. { IIO_VOLTAGE, 9, "in9" },
  108. { IIO_VOLTAGE, 10, "in10" },
  109. { IIO_VOLTAGE, 11, "in11" },
  110. { IIO_VOLTAGE, 12, "in12" },
  111. { IIO_VOLTAGE, 13, "in13" },
  112. { IIO_VOLTAGE, 14, "in14" },
  113. { IIO_VOLTAGE, 15, "in15" },
  114. };
  115. /**
  116. * STM32 ADC registers access routines
  117. * @adc: stm32 adc instance
  118. * @reg: reg offset in adc instance
  119. *
  120. * Note: All instances share same base, with 0x0, 0x100 or 0x200 offset resp.
  121. * for adc1, adc2 and adc3.
  122. */
  123. static u32 stm32_adc_readl(struct stm32_adc *adc, u32 reg)
  124. {
  125. return readl_relaxed(adc->common->base + adc->offset + reg);
  126. }
  127. static u16 stm32_adc_readw(struct stm32_adc *adc, u32 reg)
  128. {
  129. return readw_relaxed(adc->common->base + adc->offset + reg);
  130. }
  131. static void stm32_adc_writel(struct stm32_adc *adc, u32 reg, u32 val)
  132. {
  133. writel_relaxed(val, adc->common->base + adc->offset + reg);
  134. }
  135. static void stm32_adc_set_bits(struct stm32_adc *adc, u32 reg, u32 bits)
  136. {
  137. unsigned long flags;
  138. spin_lock_irqsave(&adc->lock, flags);
  139. stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) | bits);
  140. spin_unlock_irqrestore(&adc->lock, flags);
  141. }
  142. static void stm32_adc_clr_bits(struct stm32_adc *adc, u32 reg, u32 bits)
  143. {
  144. unsigned long flags;
  145. spin_lock_irqsave(&adc->lock, flags);
  146. stm32_adc_writel(adc, reg, stm32_adc_readl(adc, reg) & ~bits);
  147. spin_unlock_irqrestore(&adc->lock, flags);
  148. }
  149. /**
  150. * stm32_adc_conv_irq_enable() - Enable end of conversion interrupt
  151. * @adc: stm32 adc instance
  152. */
  153. static void stm32_adc_conv_irq_enable(struct stm32_adc *adc)
  154. {
  155. stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE);
  156. };
  157. /**
  158. * stm32_adc_conv_irq_disable() - Disable end of conversion interrupt
  159. * @adc: stm32 adc instance
  160. */
  161. static void stm32_adc_conv_irq_disable(struct stm32_adc *adc)
  162. {
  163. stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_EOCIE);
  164. }
  165. /**
  166. * stm32_adc_start_conv() - Start conversions for regular channels.
  167. * @adc: stm32 adc instance
  168. */
  169. static void stm32_adc_start_conv(struct stm32_adc *adc)
  170. {
  171. stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
  172. stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_EOCS | STM32F4_ADON);
  173. /* Wait for Power-up time (tSTAB from datasheet) */
  174. usleep_range(2, 3);
  175. /* Software start ? (e.g. trigger detection disabled ?) */
  176. if (!(stm32_adc_readl(adc, STM32F4_ADC_CR2) & STM32F4_EXTEN_MASK))
  177. stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART);
  178. }
  179. static void stm32_adc_stop_conv(struct stm32_adc *adc)
  180. {
  181. stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
  182. stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT);
  183. stm32_adc_clr_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN);
  184. stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_ADON);
  185. }
  186. /**
  187. * stm32_adc_single_conv() - Performs a single conversion
  188. * @indio_dev: IIO device
  189. * @chan: IIO channel
  190. * @res: conversion result
  191. *
  192. * The function performs a single conversion on a given channel:
  193. * - Program sequencer with one channel (e.g. in SQ1 with len = 1)
  194. * - Use SW trigger
  195. * - Start conversion, then wait for interrupt completion.
  196. */
  197. static int stm32_adc_single_conv(struct iio_dev *indio_dev,
  198. const struct iio_chan_spec *chan,
  199. int *res)
  200. {
  201. struct stm32_adc *adc = iio_priv(indio_dev);
  202. long timeout;
  203. u32 val;
  204. u16 result;
  205. int ret;
  206. reinit_completion(&adc->completion);
  207. adc->buffer = &result;
  208. /* Program chan number in regular sequence */
  209. val = stm32_adc_readl(adc, STM32F4_ADC_SQR3);
  210. val &= ~STM32F4_SQ1_MASK;
  211. val |= chan->channel << STM32F4_SQ1_SHIFT;
  212. stm32_adc_writel(adc, STM32F4_ADC_SQR3, val);
  213. /* Set regular sequence len (0 for 1 conversion) */
  214. stm32_adc_clr_bits(adc, STM32F4_ADC_SQR1, STM32F4_L_MASK);
  215. /* Trigger detection disabled (conversion can be launched in SW) */
  216. stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK);
  217. stm32_adc_conv_irq_enable(adc);
  218. stm32_adc_start_conv(adc);
  219. timeout = wait_for_completion_interruptible_timeout(
  220. &adc->completion, STM32_ADC_TIMEOUT);
  221. if (timeout == 0) {
  222. ret = -ETIMEDOUT;
  223. } else if (timeout < 0) {
  224. ret = timeout;
  225. } else {
  226. *res = result;
  227. ret = IIO_VAL_INT;
  228. }
  229. stm32_adc_stop_conv(adc);
  230. stm32_adc_conv_irq_disable(adc);
  231. return ret;
  232. }
  233. static int stm32_adc_read_raw(struct iio_dev *indio_dev,
  234. struct iio_chan_spec const *chan,
  235. int *val, int *val2, long mask)
  236. {
  237. struct stm32_adc *adc = iio_priv(indio_dev);
  238. int ret;
  239. switch (mask) {
  240. case IIO_CHAN_INFO_RAW:
  241. ret = iio_device_claim_direct_mode(indio_dev);
  242. if (ret)
  243. return ret;
  244. if (chan->type == IIO_VOLTAGE)
  245. ret = stm32_adc_single_conv(indio_dev, chan, val);
  246. else
  247. ret = -EINVAL;
  248. iio_device_release_direct_mode(indio_dev);
  249. return ret;
  250. case IIO_CHAN_INFO_SCALE:
  251. *val = adc->common->vref_mv;
  252. *val2 = chan->scan_type.realbits;
  253. return IIO_VAL_FRACTIONAL_LOG2;
  254. default:
  255. return -EINVAL;
  256. }
  257. }
  258. static irqreturn_t stm32_adc_isr(int irq, void *data)
  259. {
  260. struct stm32_adc *adc = data;
  261. u32 status = stm32_adc_readl(adc, STM32F4_ADC_SR);
  262. if (status & STM32F4_EOC) {
  263. *adc->buffer = stm32_adc_readw(adc, STM32F4_ADC_DR);
  264. complete(&adc->completion);
  265. return IRQ_HANDLED;
  266. }
  267. return IRQ_NONE;
  268. }
  269. static int stm32_adc_of_xlate(struct iio_dev *indio_dev,
  270. const struct of_phandle_args *iiospec)
  271. {
  272. int i;
  273. for (i = 0; i < indio_dev->num_channels; i++)
  274. if (indio_dev->channels[i].channel == iiospec->args[0])
  275. return i;
  276. return -EINVAL;
  277. }
  278. /**
  279. * stm32_adc_debugfs_reg_access - read or write register value
  280. *
  281. * To read a value from an ADC register:
  282. * echo [ADC reg offset] > direct_reg_access
  283. * cat direct_reg_access
  284. *
  285. * To write a value in a ADC register:
  286. * echo [ADC_reg_offset] [value] > direct_reg_access
  287. */
  288. static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
  289. unsigned reg, unsigned writeval,
  290. unsigned *readval)
  291. {
  292. struct stm32_adc *adc = iio_priv(indio_dev);
  293. if (!readval)
  294. stm32_adc_writel(adc, reg, writeval);
  295. else
  296. *readval = stm32_adc_readl(adc, reg);
  297. return 0;
  298. }
  299. static const struct iio_info stm32_adc_iio_info = {
  300. .read_raw = stm32_adc_read_raw,
  301. .debugfs_reg_access = stm32_adc_debugfs_reg_access,
  302. .of_xlate = stm32_adc_of_xlate,
  303. .driver_module = THIS_MODULE,
  304. };
  305. static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
  306. struct iio_chan_spec *chan,
  307. const struct stm32_adc_chan_spec *channel,
  308. int scan_index)
  309. {
  310. chan->type = channel->type;
  311. chan->channel = channel->channel;
  312. chan->datasheet_name = channel->name;
  313. chan->scan_index = scan_index;
  314. chan->indexed = 1;
  315. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  316. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  317. chan->scan_type.sign = 'u';
  318. chan->scan_type.realbits = 12;
  319. chan->scan_type.storagebits = 16;
  320. }
  321. static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
  322. {
  323. struct device_node *node = indio_dev->dev.of_node;
  324. struct property *prop;
  325. const __be32 *cur;
  326. struct iio_chan_spec *channels;
  327. int scan_index = 0, num_channels;
  328. u32 val;
  329. num_channels = of_property_count_u32_elems(node, "st,adc-channels");
  330. if (num_channels < 0 ||
  331. num_channels >= ARRAY_SIZE(stm32f4_adc123_channels)) {
  332. dev_err(&indio_dev->dev, "Bad st,adc-channels?\n");
  333. return num_channels < 0 ? num_channels : -EINVAL;
  334. }
  335. channels = devm_kcalloc(&indio_dev->dev, num_channels,
  336. sizeof(struct iio_chan_spec), GFP_KERNEL);
  337. if (!channels)
  338. return -ENOMEM;
  339. of_property_for_each_u32(node, "st,adc-channels", prop, cur, val) {
  340. if (val >= ARRAY_SIZE(stm32f4_adc123_channels)) {
  341. dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
  342. return -EINVAL;
  343. }
  344. stm32_adc_chan_init_one(indio_dev, &channels[scan_index],
  345. &stm32f4_adc123_channels[val],
  346. scan_index);
  347. scan_index++;
  348. }
  349. indio_dev->num_channels = scan_index;
  350. indio_dev->channels = channels;
  351. return 0;
  352. }
  353. static int stm32_adc_probe(struct platform_device *pdev)
  354. {
  355. struct iio_dev *indio_dev;
  356. struct stm32_adc *adc;
  357. int ret;
  358. if (!pdev->dev.of_node)
  359. return -ENODEV;
  360. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  361. if (!indio_dev)
  362. return -ENOMEM;
  363. adc = iio_priv(indio_dev);
  364. adc->common = dev_get_drvdata(pdev->dev.parent);
  365. spin_lock_init(&adc->lock);
  366. init_completion(&adc->completion);
  367. indio_dev->name = dev_name(&pdev->dev);
  368. indio_dev->dev.parent = &pdev->dev;
  369. indio_dev->dev.of_node = pdev->dev.of_node;
  370. indio_dev->info = &stm32_adc_iio_info;
  371. indio_dev->modes = INDIO_DIRECT_MODE;
  372. platform_set_drvdata(pdev, adc);
  373. ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset);
  374. if (ret != 0) {
  375. dev_err(&pdev->dev, "missing reg property\n");
  376. return -EINVAL;
  377. }
  378. adc->irq = platform_get_irq(pdev, 0);
  379. if (adc->irq < 0) {
  380. dev_err(&pdev->dev, "failed to get irq\n");
  381. return adc->irq;
  382. }
  383. ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr,
  384. 0, pdev->name, adc);
  385. if (ret) {
  386. dev_err(&pdev->dev, "failed to request IRQ\n");
  387. return ret;
  388. }
  389. adc->clk = devm_clk_get(&pdev->dev, NULL);
  390. if (IS_ERR(adc->clk)) {
  391. dev_err(&pdev->dev, "Can't get clock\n");
  392. return PTR_ERR(adc->clk);
  393. }
  394. ret = clk_prepare_enable(adc->clk);
  395. if (ret < 0) {
  396. dev_err(&pdev->dev, "clk enable failed\n");
  397. return ret;
  398. }
  399. ret = stm32_adc_chan_of_init(indio_dev);
  400. if (ret < 0)
  401. goto err_clk_disable;
  402. ret = iio_device_register(indio_dev);
  403. if (ret) {
  404. dev_err(&pdev->dev, "iio dev register failed\n");
  405. goto err_clk_disable;
  406. }
  407. return 0;
  408. err_clk_disable:
  409. clk_disable_unprepare(adc->clk);
  410. return ret;
  411. }
  412. static int stm32_adc_remove(struct platform_device *pdev)
  413. {
  414. struct stm32_adc *adc = platform_get_drvdata(pdev);
  415. struct iio_dev *indio_dev = iio_priv_to_dev(adc);
  416. iio_device_unregister(indio_dev);
  417. clk_disable_unprepare(adc->clk);
  418. return 0;
  419. }
  420. static const struct of_device_id stm32_adc_of_match[] = {
  421. { .compatible = "st,stm32f4-adc" },
  422. {},
  423. };
  424. MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
  425. static struct platform_driver stm32_adc_driver = {
  426. .probe = stm32_adc_probe,
  427. .remove = stm32_adc_remove,
  428. .driver = {
  429. .name = "stm32-adc",
  430. .of_match_table = stm32_adc_of_match,
  431. },
  432. };
  433. module_platform_driver(stm32_adc_driver);
  434. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  435. MODULE_DESCRIPTION("STMicroelectronics STM32 ADC IIO driver");
  436. MODULE_LICENSE("GPL v2");
  437. MODULE_ALIAS("platform:stm32-adc");