stm32-dfsdm-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is part the core part STM32 DFSDM driver
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Author(s): Arnaud Pouliquen <arnaud.pouliquen@st.com> for STMicroelectronics.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/sysfs.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include "stm32-dfsdm.h"
  22. /**
  23. * struct stm32_dfsdm_dev_data - DFSDM compatible configuration data
  24. * @ipid: DFSDM identification number. Used only if hardware provides identification registers
  25. * @num_filters: DFSDM number of filters. Unused if identification registers are available
  26. * @num_channels: DFSDM number of channels. Unused if identification registers are available
  27. * @regmap_cfg: SAI register map configuration pointer
  28. */
  29. struct stm32_dfsdm_dev_data {
  30. u32 ipid;
  31. unsigned int num_filters;
  32. unsigned int num_channels;
  33. const struct regmap_config *regmap_cfg;
  34. };
  35. #define STM32H7_DFSDM_NUM_FILTERS 4
  36. #define STM32H7_DFSDM_NUM_CHANNELS 8
  37. static bool stm32_dfsdm_volatile_reg(struct device *dev, unsigned int reg)
  38. {
  39. if (reg < DFSDM_FILTER_BASE_ADR)
  40. return false;
  41. /*
  42. * Mask is done on register to avoid to list registers of all
  43. * filter instances.
  44. */
  45. switch (reg & DFSDM_FILTER_REG_MASK) {
  46. case DFSDM_CR1(0) & DFSDM_FILTER_REG_MASK:
  47. case DFSDM_ISR(0) & DFSDM_FILTER_REG_MASK:
  48. case DFSDM_JDATAR(0) & DFSDM_FILTER_REG_MASK:
  49. case DFSDM_RDATAR(0) & DFSDM_FILTER_REG_MASK:
  50. return true;
  51. }
  52. return false;
  53. }
  54. static const struct regmap_config stm32h7_dfsdm_regmap_cfg = {
  55. .reg_bits = 32,
  56. .val_bits = 32,
  57. .reg_stride = sizeof(u32),
  58. .max_register = 0x2B8,
  59. .volatile_reg = stm32_dfsdm_volatile_reg,
  60. .fast_io = true,
  61. };
  62. static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_data = {
  63. .num_filters = STM32H7_DFSDM_NUM_FILTERS,
  64. .num_channels = STM32H7_DFSDM_NUM_CHANNELS,
  65. .regmap_cfg = &stm32h7_dfsdm_regmap_cfg,
  66. };
  67. static const struct regmap_config stm32mp1_dfsdm_regmap_cfg = {
  68. .reg_bits = 32,
  69. .val_bits = 32,
  70. .reg_stride = sizeof(u32),
  71. .max_register = 0x7fc,
  72. .volatile_reg = stm32_dfsdm_volatile_reg,
  73. .fast_io = true,
  74. };
  75. static const struct stm32_dfsdm_dev_data stm32mp1_dfsdm_data = {
  76. .ipid = STM32MP15_IPIDR_NUMBER,
  77. .regmap_cfg = &stm32mp1_dfsdm_regmap_cfg,
  78. };
  79. struct dfsdm_priv {
  80. struct platform_device *pdev; /* platform device */
  81. struct stm32_dfsdm dfsdm; /* common data exported for all instances */
  82. unsigned int spi_clk_out_div; /* SPI clkout divider value */
  83. atomic_t n_active_ch; /* number of current active channels */
  84. struct clk *clk; /* DFSDM clock */
  85. struct clk *aclk; /* audio clock */
  86. };
  87. static inline struct dfsdm_priv *to_stm32_dfsdm_priv(struct stm32_dfsdm *dfsdm)
  88. {
  89. return container_of(dfsdm, struct dfsdm_priv, dfsdm);
  90. }
  91. static int stm32_dfsdm_clk_prepare_enable(struct stm32_dfsdm *dfsdm)
  92. {
  93. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  94. int ret;
  95. ret = clk_prepare_enable(priv->clk);
  96. if (ret || !priv->aclk)
  97. return ret;
  98. ret = clk_prepare_enable(priv->aclk);
  99. if (ret)
  100. clk_disable_unprepare(priv->clk);
  101. return ret;
  102. }
  103. static void stm32_dfsdm_clk_disable_unprepare(struct stm32_dfsdm *dfsdm)
  104. {
  105. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  106. clk_disable_unprepare(priv->aclk);
  107. clk_disable_unprepare(priv->clk);
  108. }
  109. /**
  110. * stm32_dfsdm_start_dfsdm - start global dfsdm interface.
  111. *
  112. * Enable interface if n_active_ch is not null.
  113. * @dfsdm: Handle used to retrieve dfsdm context.
  114. */
  115. int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
  116. {
  117. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  118. struct device *dev = &priv->pdev->dev;
  119. unsigned int clk_div = priv->spi_clk_out_div, clk_src;
  120. int ret;
  121. if (atomic_inc_return(&priv->n_active_ch) == 1) {
  122. ret = pm_runtime_resume_and_get(dev);
  123. if (ret < 0)
  124. goto error_ret;
  125. /* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
  126. clk_src = priv->aclk ? 1 : 0;
  127. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
  128. DFSDM_CHCFGR1_CKOUTSRC_MASK,
  129. DFSDM_CHCFGR1_CKOUTSRC(clk_src));
  130. if (ret < 0)
  131. goto pm_put;
  132. /* Output the SPI CLKOUT (if clk_div == 0 clock if OFF) */
  133. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
  134. DFSDM_CHCFGR1_CKOUTDIV_MASK,
  135. DFSDM_CHCFGR1_CKOUTDIV(clk_div));
  136. if (ret < 0)
  137. goto pm_put;
  138. /* Global enable of DFSDM interface */
  139. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
  140. DFSDM_CHCFGR1_DFSDMEN_MASK,
  141. DFSDM_CHCFGR1_DFSDMEN(1));
  142. if (ret < 0)
  143. goto pm_put;
  144. }
  145. dev_dbg(dev, "%s: n_active_ch %d\n", __func__,
  146. atomic_read(&priv->n_active_ch));
  147. return 0;
  148. pm_put:
  149. pm_runtime_put_sync(dev);
  150. error_ret:
  151. atomic_dec(&priv->n_active_ch);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(stm32_dfsdm_start_dfsdm);
  155. /**
  156. * stm32_dfsdm_stop_dfsdm - stop global DFSDM interface.
  157. *
  158. * Disable interface if n_active_ch is null
  159. * @dfsdm: Handle used to retrieve dfsdm context.
  160. */
  161. int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm)
  162. {
  163. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  164. int ret;
  165. if (atomic_dec_and_test(&priv->n_active_ch)) {
  166. /* Global disable of DFSDM interface */
  167. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
  168. DFSDM_CHCFGR1_DFSDMEN_MASK,
  169. DFSDM_CHCFGR1_DFSDMEN(0));
  170. if (ret < 0)
  171. return ret;
  172. /* Stop SPI CLKOUT */
  173. ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0),
  174. DFSDM_CHCFGR1_CKOUTDIV_MASK,
  175. DFSDM_CHCFGR1_CKOUTDIV(0));
  176. if (ret < 0)
  177. return ret;
  178. pm_runtime_put_sync(&priv->pdev->dev);
  179. }
  180. dev_dbg(&priv->pdev->dev, "%s: n_active_ch %d\n", __func__,
  181. atomic_read(&priv->n_active_ch));
  182. return 0;
  183. }
  184. EXPORT_SYMBOL_GPL(stm32_dfsdm_stop_dfsdm);
  185. static int stm32_dfsdm_parse_of(struct platform_device *pdev,
  186. struct dfsdm_priv *priv)
  187. {
  188. struct device_node *node = pdev->dev.of_node;
  189. struct resource *res;
  190. unsigned long clk_freq, divider;
  191. unsigned int spi_freq, rem;
  192. int ret;
  193. if (!node)
  194. return -EINVAL;
  195. priv->dfsdm.base = devm_platform_get_and_ioremap_resource(pdev, 0,
  196. &res);
  197. if (IS_ERR(priv->dfsdm.base))
  198. return PTR_ERR(priv->dfsdm.base);
  199. priv->dfsdm.phys_base = res->start;
  200. /*
  201. * "dfsdm" clock is mandatory for DFSDM peripheral clocking.
  202. * "dfsdm" or "audio" clocks can be used as source clock for
  203. * the SPI clock out signal and internal processing, depending
  204. * on use case.
  205. */
  206. priv->clk = devm_clk_get(&pdev->dev, "dfsdm");
  207. if (IS_ERR(priv->clk))
  208. return dev_err_probe(&pdev->dev, PTR_ERR(priv->clk),
  209. "Failed to get clock\n");
  210. priv->aclk = devm_clk_get(&pdev->dev, "audio");
  211. if (IS_ERR(priv->aclk))
  212. priv->aclk = NULL;
  213. if (priv->aclk)
  214. clk_freq = clk_get_rate(priv->aclk);
  215. else
  216. clk_freq = clk_get_rate(priv->clk);
  217. /* SPI clock out frequency */
  218. ret = of_property_read_u32(pdev->dev.of_node, "spi-max-frequency",
  219. &spi_freq);
  220. if (ret < 0) {
  221. /* No SPI master mode */
  222. return 0;
  223. }
  224. divider = div_u64_rem(clk_freq, spi_freq, &rem);
  225. /* Round up divider when ckout isn't precise, not to exceed spi_freq */
  226. if (rem)
  227. divider++;
  228. /* programmable divider is in range of [2:256] */
  229. if (divider < 2 || divider > 256) {
  230. dev_err(&pdev->dev, "spi-max-frequency not achievable\n");
  231. return -EINVAL;
  232. }
  233. /* SPI clock output divider is: divider = CKOUTDIV + 1 */
  234. priv->spi_clk_out_div = divider - 1;
  235. priv->dfsdm.spi_master_freq = clk_freq / (priv->spi_clk_out_div + 1);
  236. if (rem) {
  237. dev_warn(&pdev->dev, "SPI clock not accurate\n");
  238. dev_warn(&pdev->dev, "%ld = %d * %d + %d\n",
  239. clk_freq, spi_freq, priv->spi_clk_out_div + 1, rem);
  240. }
  241. return 0;
  242. };
  243. static const struct of_device_id stm32_dfsdm_of_match[] = {
  244. {
  245. .compatible = "st,stm32h7-dfsdm",
  246. .data = &stm32h7_dfsdm_data,
  247. },
  248. {
  249. .compatible = "st,stm32mp1-dfsdm",
  250. .data = &stm32mp1_dfsdm_data,
  251. },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(of, stm32_dfsdm_of_match);
  255. static int stm32_dfsdm_probe_identification(struct platform_device *pdev,
  256. struct dfsdm_priv *priv,
  257. const struct stm32_dfsdm_dev_data *dev_data)
  258. {
  259. struct device_node *np = pdev->dev.of_node;
  260. struct device_node *child;
  261. struct stm32_dfsdm *dfsdm = &priv->dfsdm;
  262. const char *compat;
  263. int ret, count = 0;
  264. u32 id, val;
  265. if (!dev_data->ipid) {
  266. dfsdm->num_fls = dev_data->num_filters;
  267. dfsdm->num_chs = dev_data->num_channels;
  268. return 0;
  269. }
  270. ret = regmap_read(dfsdm->regmap, DFSDM_IPIDR, &id);
  271. if (ret)
  272. return ret;
  273. if (id != dev_data->ipid) {
  274. dev_err(&pdev->dev, "Unexpected IP version: 0x%x", id);
  275. return -EINVAL;
  276. }
  277. for_each_child_of_node(np, child) {
  278. ret = of_property_read_string(child, "compatible", &compat);
  279. if (ret)
  280. continue;
  281. /* Count only child nodes with dfsdm compatible */
  282. if (strstr(compat, "dfsdm"))
  283. count++;
  284. }
  285. ret = regmap_read(dfsdm->regmap, DFSDM_HWCFGR, &val);
  286. if (ret)
  287. return ret;
  288. dfsdm->num_fls = FIELD_GET(DFSDM_HWCFGR_NBF_MASK, val);
  289. dfsdm->num_chs = FIELD_GET(DFSDM_HWCFGR_NBT_MASK, val);
  290. if (count > dfsdm->num_fls) {
  291. dev_err(&pdev->dev, "Unexpected child number: %d", count);
  292. return -EINVAL;
  293. }
  294. ret = regmap_read(dfsdm->regmap, DFSDM_VERR, &val);
  295. if (ret)
  296. return ret;
  297. dev_dbg(&pdev->dev, "DFSDM version: %lu.%lu. %d channels/%d filters\n",
  298. FIELD_GET(DFSDM_VERR_MAJREV_MASK, val),
  299. FIELD_GET(DFSDM_VERR_MINREV_MASK, val),
  300. dfsdm->num_chs, dfsdm->num_fls);
  301. return 0;
  302. }
  303. static int stm32_dfsdm_probe(struct platform_device *pdev)
  304. {
  305. struct dfsdm_priv *priv;
  306. const struct stm32_dfsdm_dev_data *dev_data;
  307. struct stm32_dfsdm *dfsdm;
  308. int ret;
  309. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  310. if (!priv)
  311. return -ENOMEM;
  312. priv->pdev = pdev;
  313. dev_data = of_device_get_match_data(&pdev->dev);
  314. dfsdm = &priv->dfsdm;
  315. ret = stm32_dfsdm_parse_of(pdev, priv);
  316. if (ret < 0)
  317. return ret;
  318. dfsdm->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "dfsdm",
  319. dfsdm->base,
  320. dev_data->regmap_cfg);
  321. if (IS_ERR(dfsdm->regmap)) {
  322. ret = PTR_ERR(dfsdm->regmap);
  323. dev_err(&pdev->dev, "%s: Failed to allocate regmap: %d\n",
  324. __func__, ret);
  325. return ret;
  326. }
  327. ret = stm32_dfsdm_probe_identification(pdev, priv, dev_data);
  328. if (ret < 0)
  329. return ret;
  330. dfsdm->fl_list = devm_kcalloc(&pdev->dev, dfsdm->num_fls,
  331. sizeof(*dfsdm->fl_list), GFP_KERNEL);
  332. if (!dfsdm->fl_list)
  333. return -ENOMEM;
  334. dfsdm->ch_list = devm_kcalloc(&pdev->dev, dfsdm->num_chs,
  335. sizeof(*dfsdm->ch_list), GFP_KERNEL);
  336. if (!dfsdm->ch_list)
  337. return -ENOMEM;
  338. platform_set_drvdata(pdev, dfsdm);
  339. ret = stm32_dfsdm_clk_prepare_enable(dfsdm);
  340. if (ret) {
  341. dev_err(&pdev->dev, "Failed to start clock\n");
  342. return ret;
  343. }
  344. pm_runtime_get_noresume(&pdev->dev);
  345. pm_runtime_set_active(&pdev->dev);
  346. pm_runtime_enable(&pdev->dev);
  347. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  348. if (ret)
  349. goto pm_put;
  350. pm_runtime_put(&pdev->dev);
  351. return 0;
  352. pm_put:
  353. pm_runtime_disable(&pdev->dev);
  354. pm_runtime_set_suspended(&pdev->dev);
  355. pm_runtime_put_noidle(&pdev->dev);
  356. stm32_dfsdm_clk_disable_unprepare(dfsdm);
  357. return ret;
  358. }
  359. static void stm32_dfsdm_core_remove(struct platform_device *pdev)
  360. {
  361. struct stm32_dfsdm *dfsdm = platform_get_drvdata(pdev);
  362. pm_runtime_get_sync(&pdev->dev);
  363. of_platform_depopulate(&pdev->dev);
  364. pm_runtime_disable(&pdev->dev);
  365. pm_runtime_set_suspended(&pdev->dev);
  366. pm_runtime_put_noidle(&pdev->dev);
  367. stm32_dfsdm_clk_disable_unprepare(dfsdm);
  368. }
  369. static int stm32_dfsdm_core_suspend(struct device *dev)
  370. {
  371. struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
  372. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  373. int ret;
  374. ret = pm_runtime_force_suspend(dev);
  375. if (ret)
  376. return ret;
  377. /* Balance devm_regmap_init_mmio_clk() clk_prepare() */
  378. clk_unprepare(priv->clk);
  379. return pinctrl_pm_select_sleep_state(dev);
  380. }
  381. static int stm32_dfsdm_core_resume(struct device *dev)
  382. {
  383. struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
  384. struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm);
  385. int ret;
  386. ret = pinctrl_pm_select_default_state(dev);
  387. if (ret)
  388. return ret;
  389. ret = clk_prepare(priv->clk);
  390. if (ret)
  391. return ret;
  392. return pm_runtime_force_resume(dev);
  393. }
  394. static int stm32_dfsdm_core_runtime_suspend(struct device *dev)
  395. {
  396. struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
  397. stm32_dfsdm_clk_disable_unprepare(dfsdm);
  398. return 0;
  399. }
  400. static int stm32_dfsdm_core_runtime_resume(struct device *dev)
  401. {
  402. struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev);
  403. return stm32_dfsdm_clk_prepare_enable(dfsdm);
  404. }
  405. static const struct dev_pm_ops stm32_dfsdm_core_pm_ops = {
  406. SYSTEM_SLEEP_PM_OPS(stm32_dfsdm_core_suspend, stm32_dfsdm_core_resume)
  407. RUNTIME_PM_OPS(stm32_dfsdm_core_runtime_suspend,
  408. stm32_dfsdm_core_runtime_resume,
  409. NULL)
  410. };
  411. static struct platform_driver stm32_dfsdm_driver = {
  412. .probe = stm32_dfsdm_probe,
  413. .remove_new = stm32_dfsdm_core_remove,
  414. .driver = {
  415. .name = "stm32-dfsdm",
  416. .of_match_table = stm32_dfsdm_of_match,
  417. .pm = pm_ptr(&stm32_dfsdm_core_pm_ops),
  418. },
  419. };
  420. module_platform_driver(stm32_dfsdm_driver);
  421. MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
  422. MODULE_DESCRIPTION("STMicroelectronics STM32 dfsdm driver");
  423. MODULE_LICENSE("GPL v2");