Lines Matching +full:stm32mp1 +full:- +full:dfsdm

1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part the core part STM32 DFSDM driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
23 #include "stm32-dfsdm.h"
26 * struct stm32_dfsdm_dev_data - DFSDM compatible configuration data
27 * @ipid: DFSDM identification number. Used only if hardware provides identification registers
28 * @num_filters: DFSDM number of filters. Unused if identification registers are available
29 * @num_channels: DFSDM number of channels. Unused if identification registers are available
94 struct stm32_dfsdm dfsdm; /* common data exported for all instances */ member
99 struct clk *clk; /* DFSDM clock */
103 static inline struct dfsdm_priv *to_stm32_dfsdm_priv(struct stm32_dfsdm *dfsdm) in to_stm32_dfsdm_priv() argument
105 return container_of(dfsdm, struct dfsdm_priv, dfsdm); in to_stm32_dfsdm_priv()
108 static int stm32_dfsdm_clk_prepare_enable(struct stm32_dfsdm *dfsdm) in stm32_dfsdm_clk_prepare_enable() argument
110 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_clk_prepare_enable()
113 ret = clk_prepare_enable(priv->clk); in stm32_dfsdm_clk_prepare_enable()
114 if (ret || !priv->aclk) in stm32_dfsdm_clk_prepare_enable()
117 ret = clk_prepare_enable(priv->aclk); in stm32_dfsdm_clk_prepare_enable()
119 clk_disable_unprepare(priv->clk); in stm32_dfsdm_clk_prepare_enable()
124 static void stm32_dfsdm_clk_disable_unprepare(struct stm32_dfsdm *dfsdm) in stm32_dfsdm_clk_disable_unprepare() argument
126 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_clk_disable_unprepare()
128 clk_disable_unprepare(priv->aclk); in stm32_dfsdm_clk_disable_unprepare()
129 clk_disable_unprepare(priv->clk); in stm32_dfsdm_clk_disable_unprepare()
133 * stm32_dfsdm_start_dfsdm - start global dfsdm interface.
136 * @dfsdm: Handle used to retrieve dfsdm context.
138 int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm) in stm32_dfsdm_start_dfsdm() argument
140 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_start_dfsdm()
141 struct device *dev = &priv->pdev->dev; in stm32_dfsdm_start_dfsdm()
142 unsigned int clk_div = priv->spi_clk_out_div, clk_src; in stm32_dfsdm_start_dfsdm()
145 if (atomic_inc_return(&priv->n_active_ch) == 1) { in stm32_dfsdm_start_dfsdm()
150 /* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */ in stm32_dfsdm_start_dfsdm()
151 clk_src = priv->aclk ? 1 : 0; in stm32_dfsdm_start_dfsdm()
152 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), in stm32_dfsdm_start_dfsdm()
159 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), in stm32_dfsdm_start_dfsdm()
165 /* Global enable of DFSDM interface */ in stm32_dfsdm_start_dfsdm()
166 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), in stm32_dfsdm_start_dfsdm()
174 atomic_read(&priv->n_active_ch)); in stm32_dfsdm_start_dfsdm()
181 atomic_dec(&priv->n_active_ch); in stm32_dfsdm_start_dfsdm()
188 * stm32_dfsdm_stop_dfsdm - stop global DFSDM interface.
191 * @dfsdm: Handle used to retrieve dfsdm context.
193 int stm32_dfsdm_stop_dfsdm(struct stm32_dfsdm *dfsdm) in stm32_dfsdm_stop_dfsdm() argument
195 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_stop_dfsdm()
198 if (atomic_dec_and_test(&priv->n_active_ch)) { in stm32_dfsdm_stop_dfsdm()
199 /* Global disable of DFSDM interface */ in stm32_dfsdm_stop_dfsdm()
200 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), in stm32_dfsdm_stop_dfsdm()
207 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(0), in stm32_dfsdm_stop_dfsdm()
213 pm_runtime_put_sync(&priv->pdev->dev); in stm32_dfsdm_stop_dfsdm()
215 dev_dbg(&priv->pdev->dev, "%s: n_active_ch %d\n", __func__, in stm32_dfsdm_stop_dfsdm()
216 atomic_read(&priv->n_active_ch)); in stm32_dfsdm_stop_dfsdm()
225 struct device_node *node = pdev->dev.of_node; in stm32_dfsdm_parse_of()
232 return -EINVAL; in stm32_dfsdm_parse_of()
234 priv->dfsdm.base = devm_platform_get_and_ioremap_resource(pdev, 0, in stm32_dfsdm_parse_of()
236 if (IS_ERR(priv->dfsdm.base)) in stm32_dfsdm_parse_of()
237 return PTR_ERR(priv->dfsdm.base); in stm32_dfsdm_parse_of()
239 priv->dfsdm.phys_base = res->start; in stm32_dfsdm_parse_of()
242 * "dfsdm" clock is mandatory for DFSDM peripheral clocking. in stm32_dfsdm_parse_of()
243 * "dfsdm" or "audio" clocks can be used as source clock for in stm32_dfsdm_parse_of()
247 priv->clk = devm_clk_get(&pdev->dev, "dfsdm"); in stm32_dfsdm_parse_of()
248 if (IS_ERR(priv->clk)) in stm32_dfsdm_parse_of()
249 return dev_err_probe(&pdev->dev, PTR_ERR(priv->clk), in stm32_dfsdm_parse_of()
252 priv->aclk = devm_clk_get(&pdev->dev, "audio"); in stm32_dfsdm_parse_of()
253 if (IS_ERR(priv->aclk)) in stm32_dfsdm_parse_of()
254 priv->aclk = NULL; in stm32_dfsdm_parse_of()
256 if (priv->aclk) in stm32_dfsdm_parse_of()
257 clk_freq = clk_get_rate(priv->aclk); in stm32_dfsdm_parse_of()
259 clk_freq = clk_get_rate(priv->clk); in stm32_dfsdm_parse_of()
262 ret = of_property_read_u32(pdev->dev.of_node, "spi-max-frequency", in stm32_dfsdm_parse_of()
276 dev_err(&pdev->dev, "spi-max-frequency not achievable\n"); in stm32_dfsdm_parse_of()
277 return -EINVAL; in stm32_dfsdm_parse_of()
281 priv->spi_clk_out_div = divider - 1; in stm32_dfsdm_parse_of()
282 priv->dfsdm.spi_master_freq = clk_freq / (priv->spi_clk_out_div + 1); in stm32_dfsdm_parse_of()
285 dev_warn(&pdev->dev, "SPI clock not accurate\n"); in stm32_dfsdm_parse_of()
286 dev_warn(&pdev->dev, "%ld = %d * %d + %d\n", in stm32_dfsdm_parse_of()
287 clk_freq, spi_freq, priv->spi_clk_out_div + 1, rem); in stm32_dfsdm_parse_of()
295 .compatible = "st,stm32h7-dfsdm",
299 .compatible = "st,stm32mp1-dfsdm",
310 struct device_node *np = pdev->dev.of_node; in stm32_dfsdm_probe_identification()
312 struct stm32_dfsdm *dfsdm = &priv->dfsdm; in stm32_dfsdm_probe_identification() local
317 if (!dev_data->ipid) { in stm32_dfsdm_probe_identification()
318 dfsdm->num_fls = dev_data->num_filters; in stm32_dfsdm_probe_identification()
319 dfsdm->num_chs = dev_data->num_channels; in stm32_dfsdm_probe_identification()
323 ret = regmap_read(dfsdm->regmap, DFSDM_IPIDR, &id); in stm32_dfsdm_probe_identification()
327 if (id != dev_data->ipid) { in stm32_dfsdm_probe_identification()
328 dev_err(&pdev->dev, "Unexpected IP version: 0x%x", id); in stm32_dfsdm_probe_identification()
329 return -EINVAL; in stm32_dfsdm_probe_identification()
336 /* Count only child nodes with dfsdm compatible */ in stm32_dfsdm_probe_identification()
337 if (strstr(compat, "dfsdm")) in stm32_dfsdm_probe_identification()
341 ret = regmap_read(dfsdm->regmap, DFSDM_HWCFGR, &val); in stm32_dfsdm_probe_identification()
345 dfsdm->num_fls = FIELD_GET(DFSDM_HWCFGR_NBF_MASK, val); in stm32_dfsdm_probe_identification()
346 dfsdm->num_chs = FIELD_GET(DFSDM_HWCFGR_NBT_MASK, val); in stm32_dfsdm_probe_identification()
348 if (count > dfsdm->num_fls) { in stm32_dfsdm_probe_identification()
349 dev_err(&pdev->dev, "Unexpected child number: %d", count); in stm32_dfsdm_probe_identification()
350 return -EINVAL; in stm32_dfsdm_probe_identification()
353 ret = regmap_read(dfsdm->regmap, DFSDM_VERR, &val); in stm32_dfsdm_probe_identification()
357 dev_dbg(&pdev->dev, "DFSDM version: %lu.%lu. %d channels/%d filters\n", in stm32_dfsdm_probe_identification()
360 dfsdm->num_chs, dfsdm->num_fls); in stm32_dfsdm_probe_identification()
369 struct stm32_dfsdm *dfsdm; in stm32_dfsdm_probe() local
372 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); in stm32_dfsdm_probe()
374 return -ENOMEM; in stm32_dfsdm_probe()
376 priv->pdev = pdev; in stm32_dfsdm_probe()
378 dev_data = of_device_get_match_data(&pdev->dev); in stm32_dfsdm_probe()
380 dfsdm = &priv->dfsdm; in stm32_dfsdm_probe()
386 dfsdm->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "dfsdm", in stm32_dfsdm_probe()
387 dfsdm->base, in stm32_dfsdm_probe()
388 dev_data->regmap_cfg); in stm32_dfsdm_probe()
389 if (IS_ERR(dfsdm->regmap)) { in stm32_dfsdm_probe()
390 ret = PTR_ERR(dfsdm->regmap); in stm32_dfsdm_probe()
391 dev_err(&pdev->dev, "%s: Failed to allocate regmap: %d\n", in stm32_dfsdm_probe()
400 dfsdm->fl_list = devm_kcalloc(&pdev->dev, dfsdm->num_fls, in stm32_dfsdm_probe()
401 sizeof(*dfsdm->fl_list), GFP_KERNEL); in stm32_dfsdm_probe()
402 if (!dfsdm->fl_list) in stm32_dfsdm_probe()
403 return -ENOMEM; in stm32_dfsdm_probe()
405 dfsdm->ch_list = devm_kcalloc(&pdev->dev, dfsdm->num_chs, in stm32_dfsdm_probe()
406 sizeof(*dfsdm->ch_list), GFP_KERNEL); in stm32_dfsdm_probe()
407 if (!dfsdm->ch_list) in stm32_dfsdm_probe()
408 return -ENOMEM; in stm32_dfsdm_probe()
410 platform_set_drvdata(pdev, dfsdm); in stm32_dfsdm_probe()
412 ret = stm32_dfsdm_clk_prepare_enable(dfsdm); in stm32_dfsdm_probe()
414 dev_err(&pdev->dev, "Failed to start clock\n"); in stm32_dfsdm_probe()
418 pm_runtime_get_noresume(&pdev->dev); in stm32_dfsdm_probe()
419 pm_runtime_set_active(&pdev->dev); in stm32_dfsdm_probe()
420 pm_runtime_enable(&pdev->dev); in stm32_dfsdm_probe()
422 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); in stm32_dfsdm_probe()
426 pm_runtime_put(&pdev->dev); in stm32_dfsdm_probe()
431 pm_runtime_disable(&pdev->dev); in stm32_dfsdm_probe()
432 pm_runtime_set_suspended(&pdev->dev); in stm32_dfsdm_probe()
433 pm_runtime_put_noidle(&pdev->dev); in stm32_dfsdm_probe()
434 stm32_dfsdm_clk_disable_unprepare(dfsdm); in stm32_dfsdm_probe()
441 struct stm32_dfsdm *dfsdm = platform_get_drvdata(pdev); in stm32_dfsdm_core_remove() local
443 pm_runtime_get_sync(&pdev->dev); in stm32_dfsdm_core_remove()
444 of_platform_depopulate(&pdev->dev); in stm32_dfsdm_core_remove()
445 pm_runtime_disable(&pdev->dev); in stm32_dfsdm_core_remove()
446 pm_runtime_set_suspended(&pdev->dev); in stm32_dfsdm_core_remove()
447 pm_runtime_put_noidle(&pdev->dev); in stm32_dfsdm_core_remove()
448 stm32_dfsdm_clk_disable_unprepare(dfsdm); in stm32_dfsdm_core_remove()
453 struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev); in stm32_dfsdm_core_suspend() local
454 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_core_suspend()
462 clk_unprepare(priv->clk); in stm32_dfsdm_core_suspend()
469 struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev); in stm32_dfsdm_core_resume() local
470 struct dfsdm_priv *priv = to_stm32_dfsdm_priv(dfsdm); in stm32_dfsdm_core_resume()
477 ret = clk_prepare(priv->clk); in stm32_dfsdm_core_resume()
486 struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev); in stm32_dfsdm_core_runtime_suspend() local
488 stm32_dfsdm_clk_disable_unprepare(dfsdm); in stm32_dfsdm_core_runtime_suspend()
495 struct stm32_dfsdm *dfsdm = dev_get_drvdata(dev); in stm32_dfsdm_core_runtime_resume() local
497 return stm32_dfsdm_clk_prepare_enable(dfsdm); in stm32_dfsdm_core_runtime_resume()
511 .name = "stm32-dfsdm",
520 MODULE_DESCRIPTION("STMicroelectronics STM32 dfsdm driver");