stm32-dac.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is part of STM32 DAC driver
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Authors: Amelie Delaunay <[email protected]>
  7. * Fabrice Gasnier <[email protected]>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/delay.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/string_helpers.h>
  19. #include "stm32-dac-core.h"
  20. #define STM32_DAC_CHANNEL_1 1
  21. #define STM32_DAC_CHANNEL_2 2
  22. #define STM32_DAC_IS_CHAN_1(ch) ((ch) & STM32_DAC_CHANNEL_1)
  23. #define STM32_DAC_AUTO_SUSPEND_DELAY_MS 2000
  24. /**
  25. * struct stm32_dac - private data of DAC driver
  26. * @common: reference to DAC common data
  27. * @lock: lock to protect against potential races when reading
  28. * and update CR, to keep it in sync with pm_runtime
  29. */
  30. struct stm32_dac {
  31. struct stm32_dac_common *common;
  32. struct mutex lock;
  33. };
  34. static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
  35. {
  36. struct stm32_dac *dac = iio_priv(indio_dev);
  37. u32 en, val;
  38. int ret;
  39. ret = regmap_read(dac->common->regmap, STM32_DAC_CR, &val);
  40. if (ret < 0)
  41. return ret;
  42. if (STM32_DAC_IS_CHAN_1(channel))
  43. en = FIELD_GET(STM32_DAC_CR_EN1, val);
  44. else
  45. en = FIELD_GET(STM32_DAC_CR_EN2, val);
  46. return !!en;
  47. }
  48. static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
  49. bool enable)
  50. {
  51. struct stm32_dac *dac = iio_priv(indio_dev);
  52. struct device *dev = indio_dev->dev.parent;
  53. u32 msk = STM32_DAC_IS_CHAN_1(ch) ? STM32_DAC_CR_EN1 : STM32_DAC_CR_EN2;
  54. u32 en = enable ? msk : 0;
  55. int ret;
  56. /* already enabled / disabled ? */
  57. mutex_lock(&dac->lock);
  58. ret = stm32_dac_is_enabled(indio_dev, ch);
  59. if (ret < 0 || enable == !!ret) {
  60. mutex_unlock(&dac->lock);
  61. return ret < 0 ? ret : 0;
  62. }
  63. if (enable) {
  64. ret = pm_runtime_resume_and_get(dev);
  65. if (ret < 0) {
  66. mutex_unlock(&dac->lock);
  67. return ret;
  68. }
  69. }
  70. ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
  71. mutex_unlock(&dac->lock);
  72. if (ret < 0) {
  73. dev_err(&indio_dev->dev, "%s failed\n", str_enable_disable(en));
  74. goto err_put_pm;
  75. }
  76. /*
  77. * When HFSEL is set, it is not allowed to write the DHRx register
  78. * during 8 clock cycles after the ENx bit is set. It is not allowed
  79. * to make software/hardware trigger during this period either.
  80. */
  81. if (en && dac->common->hfsel)
  82. udelay(1);
  83. if (!enable) {
  84. pm_runtime_mark_last_busy(dev);
  85. pm_runtime_put_autosuspend(dev);
  86. }
  87. return 0;
  88. err_put_pm:
  89. if (enable) {
  90. pm_runtime_mark_last_busy(dev);
  91. pm_runtime_put_autosuspend(dev);
  92. }
  93. return ret;
  94. }
  95. static int stm32_dac_get_value(struct stm32_dac *dac, int channel, int *val)
  96. {
  97. int ret;
  98. if (STM32_DAC_IS_CHAN_1(channel))
  99. ret = regmap_read(dac->common->regmap, STM32_DAC_DOR1, val);
  100. else
  101. ret = regmap_read(dac->common->regmap, STM32_DAC_DOR2, val);
  102. return ret ? ret : IIO_VAL_INT;
  103. }
  104. static int stm32_dac_set_value(struct stm32_dac *dac, int channel, int val)
  105. {
  106. int ret;
  107. if (STM32_DAC_IS_CHAN_1(channel))
  108. ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R1, val);
  109. else
  110. ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R2, val);
  111. return ret;
  112. }
  113. static int stm32_dac_read_raw(struct iio_dev *indio_dev,
  114. struct iio_chan_spec const *chan,
  115. int *val, int *val2, long mask)
  116. {
  117. struct stm32_dac *dac = iio_priv(indio_dev);
  118. switch (mask) {
  119. case IIO_CHAN_INFO_RAW:
  120. return stm32_dac_get_value(dac, chan->channel, val);
  121. case IIO_CHAN_INFO_SCALE:
  122. *val = dac->common->vref_mv;
  123. *val2 = chan->scan_type.realbits;
  124. return IIO_VAL_FRACTIONAL_LOG2;
  125. default:
  126. return -EINVAL;
  127. }
  128. }
  129. static int stm32_dac_write_raw(struct iio_dev *indio_dev,
  130. struct iio_chan_spec const *chan,
  131. int val, int val2, long mask)
  132. {
  133. struct stm32_dac *dac = iio_priv(indio_dev);
  134. switch (mask) {
  135. case IIO_CHAN_INFO_RAW:
  136. return stm32_dac_set_value(dac, chan->channel, val);
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. static int stm32_dac_debugfs_reg_access(struct iio_dev *indio_dev,
  142. unsigned reg, unsigned writeval,
  143. unsigned *readval)
  144. {
  145. struct stm32_dac *dac = iio_priv(indio_dev);
  146. if (!readval)
  147. return regmap_write(dac->common->regmap, reg, writeval);
  148. else
  149. return regmap_read(dac->common->regmap, reg, readval);
  150. }
  151. static const struct iio_info stm32_dac_iio_info = {
  152. .read_raw = stm32_dac_read_raw,
  153. .write_raw = stm32_dac_write_raw,
  154. .debugfs_reg_access = stm32_dac_debugfs_reg_access,
  155. };
  156. static const char * const stm32_dac_powerdown_modes[] = {
  157. "three_state",
  158. };
  159. static int stm32_dac_get_powerdown_mode(struct iio_dev *indio_dev,
  160. const struct iio_chan_spec *chan)
  161. {
  162. return 0;
  163. }
  164. static int stm32_dac_set_powerdown_mode(struct iio_dev *indio_dev,
  165. const struct iio_chan_spec *chan,
  166. unsigned int type)
  167. {
  168. return 0;
  169. }
  170. static ssize_t stm32_dac_read_powerdown(struct iio_dev *indio_dev,
  171. uintptr_t private,
  172. const struct iio_chan_spec *chan,
  173. char *buf)
  174. {
  175. int ret = stm32_dac_is_enabled(indio_dev, chan->channel);
  176. if (ret < 0)
  177. return ret;
  178. return sysfs_emit(buf, "%d\n", ret ? 0 : 1);
  179. }
  180. static ssize_t stm32_dac_write_powerdown(struct iio_dev *indio_dev,
  181. uintptr_t private,
  182. const struct iio_chan_spec *chan,
  183. const char *buf, size_t len)
  184. {
  185. bool powerdown;
  186. int ret;
  187. ret = kstrtobool(buf, &powerdown);
  188. if (ret)
  189. return ret;
  190. ret = stm32_dac_set_enable_state(indio_dev, chan->channel, !powerdown);
  191. if (ret)
  192. return ret;
  193. return len;
  194. }
  195. static const struct iio_enum stm32_dac_powerdown_mode_en = {
  196. .items = stm32_dac_powerdown_modes,
  197. .num_items = ARRAY_SIZE(stm32_dac_powerdown_modes),
  198. .get = stm32_dac_get_powerdown_mode,
  199. .set = stm32_dac_set_powerdown_mode,
  200. };
  201. static const struct iio_chan_spec_ext_info stm32_dac_ext_info[] = {
  202. {
  203. .name = "powerdown",
  204. .read = stm32_dac_read_powerdown,
  205. .write = stm32_dac_write_powerdown,
  206. .shared = IIO_SEPARATE,
  207. },
  208. IIO_ENUM("powerdown_mode", IIO_SEPARATE, &stm32_dac_powerdown_mode_en),
  209. IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &stm32_dac_powerdown_mode_en),
  210. {},
  211. };
  212. #define STM32_DAC_CHANNEL(chan, name) { \
  213. .type = IIO_VOLTAGE, \
  214. .indexed = 1, \
  215. .output = 1, \
  216. .channel = chan, \
  217. .info_mask_separate = \
  218. BIT(IIO_CHAN_INFO_RAW) | \
  219. BIT(IIO_CHAN_INFO_SCALE), \
  220. /* scan_index is always 0 as num_channels is 1 */ \
  221. .scan_type = { \
  222. .sign = 'u', \
  223. .realbits = 12, \
  224. .storagebits = 16, \
  225. }, \
  226. .datasheet_name = name, \
  227. .ext_info = stm32_dac_ext_info \
  228. }
  229. static const struct iio_chan_spec stm32_dac_channels[] = {
  230. STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_1, "out1"),
  231. STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_2, "out2"),
  232. };
  233. static int stm32_dac_chan_of_init(struct iio_dev *indio_dev)
  234. {
  235. struct device_node *np = indio_dev->dev.of_node;
  236. unsigned int i;
  237. u32 channel;
  238. int ret;
  239. ret = of_property_read_u32(np, "reg", &channel);
  240. if (ret) {
  241. dev_err(&indio_dev->dev, "Failed to read reg property\n");
  242. return ret;
  243. }
  244. for (i = 0; i < ARRAY_SIZE(stm32_dac_channels); i++) {
  245. if (stm32_dac_channels[i].channel == channel)
  246. break;
  247. }
  248. if (i >= ARRAY_SIZE(stm32_dac_channels)) {
  249. dev_err(&indio_dev->dev, "Invalid reg property\n");
  250. return -EINVAL;
  251. }
  252. indio_dev->channels = &stm32_dac_channels[i];
  253. /*
  254. * Expose only one channel here, as they can be used independently,
  255. * with separate trigger. Then separate IIO devices are instantiated
  256. * to manage this.
  257. */
  258. indio_dev->num_channels = 1;
  259. return 0;
  260. };
  261. static int stm32_dac_probe(struct platform_device *pdev)
  262. {
  263. struct device_node *np = pdev->dev.of_node;
  264. struct device *dev = &pdev->dev;
  265. struct iio_dev *indio_dev;
  266. struct stm32_dac *dac;
  267. int ret;
  268. if (!np)
  269. return -ENODEV;
  270. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
  271. if (!indio_dev)
  272. return -ENOMEM;
  273. platform_set_drvdata(pdev, indio_dev);
  274. dac = iio_priv(indio_dev);
  275. dac->common = dev_get_drvdata(pdev->dev.parent);
  276. indio_dev->name = dev_name(&pdev->dev);
  277. indio_dev->dev.of_node = pdev->dev.of_node;
  278. indio_dev->info = &stm32_dac_iio_info;
  279. indio_dev->modes = INDIO_DIRECT_MODE;
  280. mutex_init(&dac->lock);
  281. ret = stm32_dac_chan_of_init(indio_dev);
  282. if (ret < 0)
  283. return ret;
  284. /* Get stm32-dac-core PM online */
  285. pm_runtime_get_noresume(dev);
  286. pm_runtime_set_active(dev);
  287. pm_runtime_set_autosuspend_delay(dev, STM32_DAC_AUTO_SUSPEND_DELAY_MS);
  288. pm_runtime_use_autosuspend(dev);
  289. pm_runtime_enable(dev);
  290. ret = iio_device_register(indio_dev);
  291. if (ret)
  292. goto err_pm_put;
  293. pm_runtime_mark_last_busy(dev);
  294. pm_runtime_put_autosuspend(dev);
  295. return 0;
  296. err_pm_put:
  297. pm_runtime_disable(dev);
  298. pm_runtime_set_suspended(dev);
  299. pm_runtime_put_noidle(dev);
  300. return ret;
  301. }
  302. static int stm32_dac_remove(struct platform_device *pdev)
  303. {
  304. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  305. pm_runtime_get_sync(&pdev->dev);
  306. iio_device_unregister(indio_dev);
  307. pm_runtime_disable(&pdev->dev);
  308. pm_runtime_set_suspended(&pdev->dev);
  309. pm_runtime_put_noidle(&pdev->dev);
  310. return 0;
  311. }
  312. static int stm32_dac_suspend(struct device *dev)
  313. {
  314. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  315. int channel = indio_dev->channels[0].channel;
  316. int ret;
  317. /* Ensure DAC is disabled before suspend */
  318. ret = stm32_dac_is_enabled(indio_dev, channel);
  319. if (ret)
  320. return ret < 0 ? ret : -EBUSY;
  321. return pm_runtime_force_suspend(dev);
  322. }
  323. static DEFINE_SIMPLE_DEV_PM_OPS(stm32_dac_pm_ops, stm32_dac_suspend,
  324. pm_runtime_force_resume);
  325. static const struct of_device_id stm32_dac_of_match[] = {
  326. { .compatible = "st,stm32-dac", },
  327. {},
  328. };
  329. MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
  330. static struct platform_driver stm32_dac_driver = {
  331. .probe = stm32_dac_probe,
  332. .remove = stm32_dac_remove,
  333. .driver = {
  334. .name = "stm32-dac",
  335. .of_match_table = stm32_dac_of_match,
  336. .pm = pm_sleep_ptr(&stm32_dac_pm_ops),
  337. },
  338. };
  339. module_platform_driver(stm32_dac_driver);
  340. MODULE_ALIAS("platform:stm32-dac");
  341. MODULE_AUTHOR("Amelie Delaunay <[email protected]>");
  342. MODULE_DESCRIPTION("STMicroelectronics STM32 DAC driver");
  343. MODULE_LICENSE("GPL v2");