18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// Copyright 2018 NXP 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci#include <linux/clk.h> 58c2ecf20Sopenharmony_ci#include <linux/device.h> 68c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 78c2ecf20Sopenharmony_ci#include <linux/kobject.h> 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/of.h> 118c2ecf20Sopenharmony_ci#include <linux/of_address.h> 128c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 138c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 148c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 158c2ecf20Sopenharmony_ci#include <linux/regmap.h> 168c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 178c2ecf20Sopenharmony_ci#include <linux/types.h> 188c2ecf20Sopenharmony_ci#include <sound/dmaengine_pcm.h> 198c2ecf20Sopenharmony_ci#include <sound/pcm.h> 208c2ecf20Sopenharmony_ci#include <sound/soc.h> 218c2ecf20Sopenharmony_ci#include <sound/tlv.h> 228c2ecf20Sopenharmony_ci#include <sound/core.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include "fsl_micfil.h" 258c2ecf20Sopenharmony_ci#include "imx-pcm.h" 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define FSL_MICFIL_RATES SNDRV_PCM_RATE_8000_48000 288c2ecf20Sopenharmony_ci#define FSL_MICFIL_FORMATS (SNDRV_PCM_FMTBIT_S16_LE) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct fsl_micfil { 318c2ecf20Sopenharmony_ci struct platform_device *pdev; 328c2ecf20Sopenharmony_ci struct regmap *regmap; 338c2ecf20Sopenharmony_ci const struct fsl_micfil_soc_data *soc; 348c2ecf20Sopenharmony_ci struct clk *mclk; 358c2ecf20Sopenharmony_ci struct snd_dmaengine_dai_dma_data dma_params_rx; 368c2ecf20Sopenharmony_ci unsigned int dataline; 378c2ecf20Sopenharmony_ci char name[32]; 388c2ecf20Sopenharmony_ci int irq[MICFIL_IRQ_LINES]; 398c2ecf20Sopenharmony_ci unsigned int mclk_streams; 408c2ecf20Sopenharmony_ci int quality; /*QUALITY 2-0 bits */ 418c2ecf20Sopenharmony_ci bool slave_mode; 428c2ecf20Sopenharmony_ci int channel_gain[8]; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistruct fsl_micfil_soc_data { 468c2ecf20Sopenharmony_ci unsigned int fifos; 478c2ecf20Sopenharmony_ci unsigned int fifo_depth; 488c2ecf20Sopenharmony_ci unsigned int dataline; 498c2ecf20Sopenharmony_ci bool imx; 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic struct fsl_micfil_soc_data fsl_micfil_imx8mm = { 538c2ecf20Sopenharmony_ci .imx = true, 548c2ecf20Sopenharmony_ci .fifos = 8, 558c2ecf20Sopenharmony_ci .fifo_depth = 8, 568c2ecf20Sopenharmony_ci .dataline = 0xf, 578c2ecf20Sopenharmony_ci}; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic const struct of_device_id fsl_micfil_dt_ids[] = { 608c2ecf20Sopenharmony_ci { .compatible = "fsl,imx8mm-micfil", .data = &fsl_micfil_imx8mm }, 618c2ecf20Sopenharmony_ci {} 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, fsl_micfil_dt_ids); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* Table 5. Quality Modes 668c2ecf20Sopenharmony_ci * Medium 0 0 0 678c2ecf20Sopenharmony_ci * High 0 0 1 688c2ecf20Sopenharmony_ci * Very Low 2 1 0 0 698c2ecf20Sopenharmony_ci * Very Low 1 1 0 1 708c2ecf20Sopenharmony_ci * Very Low 0 1 1 0 718c2ecf20Sopenharmony_ci * Low 1 1 1 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_cistatic const char * const micfil_quality_select_texts[] = { 748c2ecf20Sopenharmony_ci "Medium", "High", 758c2ecf20Sopenharmony_ci "N/A", "N/A", 768c2ecf20Sopenharmony_ci "VLow2", "VLow1", 778c2ecf20Sopenharmony_ci "VLow0", "Low", 788c2ecf20Sopenharmony_ci}; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic const struct soc_enum fsl_micfil_quality_enum = 818c2ecf20Sopenharmony_ci SOC_ENUM_SINGLE(REG_MICFIL_CTRL2, 828c2ecf20Sopenharmony_ci MICFIL_CTRL2_QSEL_SHIFT, 838c2ecf20Sopenharmony_ci ARRAY_SIZE(micfil_quality_select_texts), 848c2ecf20Sopenharmony_ci micfil_quality_select_texts); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(gain_tlv, 0, 100, 0); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new fsl_micfil_snd_controls[] = { 898c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL, 908c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(0), 0x8, 0xF, gain_tlv), 918c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL, 928c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(1), 0x8, 0xF, gain_tlv), 938c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL, 948c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(2), 0x8, 0xF, gain_tlv), 958c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL, 968c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(3), 0x8, 0xF, gain_tlv), 978c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL, 988c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(4), 0x8, 0xF, gain_tlv), 998c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL, 1008c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(5), 0x8, 0xF, gain_tlv), 1018c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL, 1028c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(6), 0x8, 0xF, gain_tlv), 1038c2ecf20Sopenharmony_ci SOC_SINGLE_SX_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL, 1048c2ecf20Sopenharmony_ci MICFIL_OUTGAIN_CHX_SHIFT(7), 0x8, 0xF, gain_tlv), 1058c2ecf20Sopenharmony_ci SOC_ENUM_EXT("MICFIL Quality Select", 1068c2ecf20Sopenharmony_ci fsl_micfil_quality_enum, 1078c2ecf20Sopenharmony_ci snd_soc_get_enum_double, snd_soc_put_enum_double), 1088c2ecf20Sopenharmony_ci}; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic inline int get_pdm_clk(struct fsl_micfil *micfil, 1118c2ecf20Sopenharmony_ci unsigned int rate) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci u32 ctrl2_reg; 1148c2ecf20Sopenharmony_ci int qsel, osr; 1158c2ecf20Sopenharmony_ci int bclk; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg); 1188c2ecf20Sopenharmony_ci osr = 16 - ((ctrl2_reg & MICFIL_CTRL2_CICOSR_MASK) 1198c2ecf20Sopenharmony_ci >> MICFIL_CTRL2_CICOSR_SHIFT); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg); 1228c2ecf20Sopenharmony_ci qsel = ctrl2_reg & MICFIL_CTRL2_QSEL_MASK; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci switch (qsel) { 1258c2ecf20Sopenharmony_ci case MICFIL_HIGH_QUALITY: 1268c2ecf20Sopenharmony_ci bclk = rate * 8 * osr / 2; /* kfactor = 0.5 */ 1278c2ecf20Sopenharmony_ci break; 1288c2ecf20Sopenharmony_ci case MICFIL_MEDIUM_QUALITY: 1298c2ecf20Sopenharmony_ci case MICFIL_VLOW0_QUALITY: 1308c2ecf20Sopenharmony_ci bclk = rate * 4 * osr * 1; /* kfactor = 1 */ 1318c2ecf20Sopenharmony_ci break; 1328c2ecf20Sopenharmony_ci case MICFIL_LOW_QUALITY: 1338c2ecf20Sopenharmony_ci case MICFIL_VLOW1_QUALITY: 1348c2ecf20Sopenharmony_ci bclk = rate * 2 * osr * 2; /* kfactor = 2 */ 1358c2ecf20Sopenharmony_ci break; 1368c2ecf20Sopenharmony_ci case MICFIL_VLOW2_QUALITY: 1378c2ecf20Sopenharmony_ci bclk = rate * osr * 4; /* kfactor = 4 */ 1388c2ecf20Sopenharmony_ci break; 1398c2ecf20Sopenharmony_ci default: 1408c2ecf20Sopenharmony_ci dev_err(&micfil->pdev->dev, 1418c2ecf20Sopenharmony_ci "Please make sure you select a valid quality.\n"); 1428c2ecf20Sopenharmony_ci bclk = -1; 1438c2ecf20Sopenharmony_ci break; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return bclk; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic inline int get_clk_div(struct fsl_micfil *micfil, 1508c2ecf20Sopenharmony_ci unsigned int rate) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci u32 ctrl2_reg; 1538c2ecf20Sopenharmony_ci long mclk_rate; 1548c2ecf20Sopenharmony_ci int clk_div; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci mclk_rate = clk_get_rate(micfil->mclk); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci clk_div = mclk_rate / (get_pdm_clk(micfil, rate) * 2); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return clk_div; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* The SRES is a self-negated bit which provides the CPU with the 1668c2ecf20Sopenharmony_ci * capability to initialize the PDM Interface module through the 1678c2ecf20Sopenharmony_ci * slave-bus interface. This bit always reads as zero, and this 1688c2ecf20Sopenharmony_ci * bit is only effective when MDIS is cleared 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_cistatic int fsl_micfil_reset(struct device *dev) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = dev_get_drvdata(dev); 1738c2ecf20Sopenharmony_ci int ret; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, 1768c2ecf20Sopenharmony_ci REG_MICFIL_CTRL1, 1778c2ecf20Sopenharmony_ci MICFIL_CTRL1_MDIS_MASK, 1788c2ecf20Sopenharmony_ci 0); 1798c2ecf20Sopenharmony_ci if (ret) { 1808c2ecf20Sopenharmony_ci dev_err(dev, "failed to clear MDIS bit %d\n", ret); 1818c2ecf20Sopenharmony_ci return ret; 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, 1858c2ecf20Sopenharmony_ci REG_MICFIL_CTRL1, 1868c2ecf20Sopenharmony_ci MICFIL_CTRL1_SRES_MASK, 1878c2ecf20Sopenharmony_ci MICFIL_CTRL1_SRES); 1888c2ecf20Sopenharmony_ci if (ret) { 1898c2ecf20Sopenharmony_ci dev_err(dev, "failed to reset MICFIL: %d\n", ret); 1908c2ecf20Sopenharmony_ci return ret; 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci /* 1948c2ecf20Sopenharmony_ci * SRES is self-cleared bit, but REG_MICFIL_CTRL1 is defined 1958c2ecf20Sopenharmony_ci * as non-volatile register, so SRES still remain in regmap 1968c2ecf20Sopenharmony_ci * cache after set, that every update of REG_MICFIL_CTRL1, 1978c2ecf20Sopenharmony_ci * software reset happens. so clear it explicitly. 1988c2ecf20Sopenharmony_ci */ 1998c2ecf20Sopenharmony_ci ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1, 2008c2ecf20Sopenharmony_ci MICFIL_CTRL1_SRES); 2018c2ecf20Sopenharmony_ci if (ret) 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* 2058c2ecf20Sopenharmony_ci * Set SRES should clear CHnF flags, But even add delay here 2068c2ecf20Sopenharmony_ci * the CHnF may not be cleared sometimes, so clear CHnF explicitly. 2078c2ecf20Sopenharmony_ci */ 2088c2ecf20Sopenharmony_ci ret = regmap_write_bits(micfil->regmap, REG_MICFIL_STAT, 0xFF, 0xFF); 2098c2ecf20Sopenharmony_ci if (ret) 2108c2ecf20Sopenharmony_ci return ret; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci return 0; 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic int fsl_micfil_set_mclk_rate(struct fsl_micfil *micfil, 2168c2ecf20Sopenharmony_ci unsigned int freq) 2178c2ecf20Sopenharmony_ci{ 2188c2ecf20Sopenharmony_ci struct device *dev = &micfil->pdev->dev; 2198c2ecf20Sopenharmony_ci int ret; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci clk_disable_unprepare(micfil->mclk); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci ret = clk_set_rate(micfil->mclk, freq * 1024); 2248c2ecf20Sopenharmony_ci if (ret) 2258c2ecf20Sopenharmony_ci dev_warn(dev, "failed to set rate (%u): %d\n", 2268c2ecf20Sopenharmony_ci freq * 1024, ret); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci clk_prepare_enable(micfil->mclk); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci return ret; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic int fsl_micfil_startup(struct snd_pcm_substream *substream, 2348c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (!micfil) { 2398c2ecf20Sopenharmony_ci dev_err(dai->dev, "micfil dai priv_data not set\n"); 2408c2ecf20Sopenharmony_ci return -EINVAL; 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci return 0; 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd, 2478c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai); 2508c2ecf20Sopenharmony_ci struct device *dev = &micfil->pdev->dev; 2518c2ecf20Sopenharmony_ci int ret; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci switch (cmd) { 2548c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 2558c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_RESUME: 2568c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2578c2ecf20Sopenharmony_ci ret = fsl_micfil_reset(dev); 2588c2ecf20Sopenharmony_ci if (ret) { 2598c2ecf20Sopenharmony_ci dev_err(dev, "failed to soft reset\n"); 2608c2ecf20Sopenharmony_ci return ret; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* DMA Interrupt Selection - DISEL bits 2648c2ecf20Sopenharmony_ci * 00 - DMA and IRQ disabled 2658c2ecf20Sopenharmony_ci * 01 - DMA req enabled 2668c2ecf20Sopenharmony_ci * 10 - IRQ enabled 2678c2ecf20Sopenharmony_ci * 11 - reserved 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 2708c2ecf20Sopenharmony_ci MICFIL_CTRL1_DISEL_MASK, 2718c2ecf20Sopenharmony_ci (1 << MICFIL_CTRL1_DISEL_SHIFT)); 2728c2ecf20Sopenharmony_ci if (ret) { 2738c2ecf20Sopenharmony_ci dev_err(dev, "failed to update DISEL bits\n"); 2748c2ecf20Sopenharmony_ci return ret; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci /* Enable the module */ 2788c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 2798c2ecf20Sopenharmony_ci MICFIL_CTRL1_PDMIEN_MASK, 2808c2ecf20Sopenharmony_ci MICFIL_CTRL1_PDMIEN); 2818c2ecf20Sopenharmony_ci if (ret) { 2828c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable the module\n"); 2838c2ecf20Sopenharmony_ci return ret; 2848c2ecf20Sopenharmony_ci } 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci break; 2878c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 2888c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_SUSPEND: 2898c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2908c2ecf20Sopenharmony_ci /* Disable the module */ 2918c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 2928c2ecf20Sopenharmony_ci MICFIL_CTRL1_PDMIEN_MASK, 2938c2ecf20Sopenharmony_ci 0); 2948c2ecf20Sopenharmony_ci if (ret) { 2958c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable the module\n"); 2968c2ecf20Sopenharmony_ci return ret; 2978c2ecf20Sopenharmony_ci } 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 3008c2ecf20Sopenharmony_ci MICFIL_CTRL1_DISEL_MASK, 3018c2ecf20Sopenharmony_ci (0 << MICFIL_CTRL1_DISEL_SHIFT)); 3028c2ecf20Sopenharmony_ci if (ret) { 3038c2ecf20Sopenharmony_ci dev_err(dev, "failed to update DISEL bits\n"); 3048c2ecf20Sopenharmony_ci return ret; 3058c2ecf20Sopenharmony_ci } 3068c2ecf20Sopenharmony_ci break; 3078c2ecf20Sopenharmony_ci default: 3088c2ecf20Sopenharmony_ci return -EINVAL; 3098c2ecf20Sopenharmony_ci } 3108c2ecf20Sopenharmony_ci return 0; 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cistatic int fsl_set_clock_params(struct device *dev, unsigned int rate) 3148c2ecf20Sopenharmony_ci{ 3158c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = dev_get_drvdata(dev); 3168c2ecf20Sopenharmony_ci int clk_div; 3178c2ecf20Sopenharmony_ci int ret; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci ret = fsl_micfil_set_mclk_rate(micfil, rate); 3208c2ecf20Sopenharmony_ci if (ret < 0) 3218c2ecf20Sopenharmony_ci dev_err(dev, "failed to set mclk[%lu] to rate %u\n", 3228c2ecf20Sopenharmony_ci clk_get_rate(micfil->mclk), rate); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci /* set CICOSR */ 3258c2ecf20Sopenharmony_ci ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2, 3268c2ecf20Sopenharmony_ci MICFIL_CTRL2_CICOSR_MASK, 3278c2ecf20Sopenharmony_ci MICFIL_CTRL2_OSR_DEFAULT); 3288c2ecf20Sopenharmony_ci if (ret) 3298c2ecf20Sopenharmony_ci dev_err(dev, "failed to set CICOSR in reg 0x%X\n", 3308c2ecf20Sopenharmony_ci REG_MICFIL_CTRL2); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci /* set CLK_DIV */ 3338c2ecf20Sopenharmony_ci clk_div = get_clk_div(micfil, rate); 3348c2ecf20Sopenharmony_ci if (clk_div < 0) 3358c2ecf20Sopenharmony_ci ret = -EINVAL; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2, 3388c2ecf20Sopenharmony_ci MICFIL_CTRL2_CLKDIV_MASK, clk_div); 3398c2ecf20Sopenharmony_ci if (ret) 3408c2ecf20Sopenharmony_ci dev_err(dev, "failed to set CLKDIV in reg 0x%X\n", 3418c2ecf20Sopenharmony_ci REG_MICFIL_CTRL2); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci return ret; 3448c2ecf20Sopenharmony_ci} 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_cistatic int fsl_micfil_hw_params(struct snd_pcm_substream *substream, 3478c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *params, 3488c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai); 3518c2ecf20Sopenharmony_ci unsigned int channels = params_channels(params); 3528c2ecf20Sopenharmony_ci unsigned int rate = params_rate(params); 3538c2ecf20Sopenharmony_ci struct device *dev = &micfil->pdev->dev; 3548c2ecf20Sopenharmony_ci int ret; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci /* 1. Disable the module */ 3578c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 3588c2ecf20Sopenharmony_ci MICFIL_CTRL1_PDMIEN_MASK, 0); 3598c2ecf20Sopenharmony_ci if (ret) { 3608c2ecf20Sopenharmony_ci dev_err(dev, "failed to disable the module\n"); 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci /* enable channels */ 3658c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1, 3668c2ecf20Sopenharmony_ci 0xFF, ((1 << channels) - 1)); 3678c2ecf20Sopenharmony_ci if (ret) { 3688c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable channels %d, reg 0x%X\n", ret, 3698c2ecf20Sopenharmony_ci REG_MICFIL_CTRL1); 3708c2ecf20Sopenharmony_ci return ret; 3718c2ecf20Sopenharmony_ci } 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci ret = fsl_set_clock_params(dev, rate); 3748c2ecf20Sopenharmony_ci if (ret < 0) { 3758c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set clock parameters [%d]\n", ret); 3768c2ecf20Sopenharmony_ci return ret; 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci return 0; 3828c2ecf20Sopenharmony_ci} 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_cistatic int fsl_micfil_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 3858c2ecf20Sopenharmony_ci unsigned int freq, int dir) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai); 3888c2ecf20Sopenharmony_ci struct device *dev = &micfil->pdev->dev; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci int ret; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci if (!freq) 3938c2ecf20Sopenharmony_ci return 0; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci ret = fsl_micfil_set_mclk_rate(micfil, freq); 3968c2ecf20Sopenharmony_ci if (ret < 0) 3978c2ecf20Sopenharmony_ci dev_err(dev, "failed to set mclk[%lu] to rate %u\n", 3988c2ecf20Sopenharmony_ci clk_get_rate(micfil->mclk), freq); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci return ret; 4018c2ecf20Sopenharmony_ci} 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_cistatic struct snd_soc_dai_ops fsl_micfil_dai_ops = { 4048c2ecf20Sopenharmony_ci .startup = fsl_micfil_startup, 4058c2ecf20Sopenharmony_ci .trigger = fsl_micfil_trigger, 4068c2ecf20Sopenharmony_ci .hw_params = fsl_micfil_hw_params, 4078c2ecf20Sopenharmony_ci .set_sysclk = fsl_micfil_set_dai_sysclk, 4088c2ecf20Sopenharmony_ci}; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev); 4138c2ecf20Sopenharmony_ci struct device *dev = cpu_dai->dev; 4148c2ecf20Sopenharmony_ci unsigned int val; 4158c2ecf20Sopenharmony_ci int ret; 4168c2ecf20Sopenharmony_ci int i; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci /* set qsel to medium */ 4198c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2, 4208c2ecf20Sopenharmony_ci MICFIL_CTRL2_QSEL_MASK, MICFIL_MEDIUM_QUALITY); 4218c2ecf20Sopenharmony_ci if (ret) { 4228c2ecf20Sopenharmony_ci dev_err(dev, "failed to set quality mode bits, reg 0x%X\n", 4238c2ecf20Sopenharmony_ci REG_MICFIL_CTRL2); 4248c2ecf20Sopenharmony_ci return ret; 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci /* set default gain to max_gain */ 4288c2ecf20Sopenharmony_ci regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777); 4298c2ecf20Sopenharmony_ci for (i = 0; i < 8; i++) 4308c2ecf20Sopenharmony_ci micfil->channel_gain[i] = 0xF; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci snd_soc_dai_init_dma_data(cpu_dai, NULL, 4338c2ecf20Sopenharmony_ci &micfil->dma_params_rx); 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci /* FIFO Watermark Control - FIFOWMK*/ 4368c2ecf20Sopenharmony_ci val = MICFIL_FIFO_CTRL_FIFOWMK(micfil->soc->fifo_depth) - 1; 4378c2ecf20Sopenharmony_ci ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL, 4388c2ecf20Sopenharmony_ci MICFIL_FIFO_CTRL_FIFOWMK_MASK, 4398c2ecf20Sopenharmony_ci val); 4408c2ecf20Sopenharmony_ci if (ret) { 4418c2ecf20Sopenharmony_ci dev_err(dev, "failed to set FIFOWMK\n"); 4428c2ecf20Sopenharmony_ci return ret; 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci snd_soc_dai_set_drvdata(cpu_dai, micfil); 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci return 0; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver fsl_micfil_dai = { 4518c2ecf20Sopenharmony_ci .probe = fsl_micfil_dai_probe, 4528c2ecf20Sopenharmony_ci .capture = { 4538c2ecf20Sopenharmony_ci .stream_name = "CPU-Capture", 4548c2ecf20Sopenharmony_ci .channels_min = 1, 4558c2ecf20Sopenharmony_ci .channels_max = 8, 4568c2ecf20Sopenharmony_ci .rates = FSL_MICFIL_RATES, 4578c2ecf20Sopenharmony_ci .formats = FSL_MICFIL_FORMATS, 4588c2ecf20Sopenharmony_ci }, 4598c2ecf20Sopenharmony_ci .ops = &fsl_micfil_dai_ops, 4608c2ecf20Sopenharmony_ci}; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver fsl_micfil_component = { 4638c2ecf20Sopenharmony_ci .name = "fsl-micfil-dai", 4648c2ecf20Sopenharmony_ci .controls = fsl_micfil_snd_controls, 4658c2ecf20Sopenharmony_ci .num_controls = ARRAY_SIZE(fsl_micfil_snd_controls), 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci}; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci/* REGMAP */ 4708c2ecf20Sopenharmony_cistatic const struct reg_default fsl_micfil_reg_defaults[] = { 4718c2ecf20Sopenharmony_ci {REG_MICFIL_CTRL1, 0x00000000}, 4728c2ecf20Sopenharmony_ci {REG_MICFIL_CTRL2, 0x00000000}, 4738c2ecf20Sopenharmony_ci {REG_MICFIL_STAT, 0x00000000}, 4748c2ecf20Sopenharmony_ci {REG_MICFIL_FIFO_CTRL, 0x00000007}, 4758c2ecf20Sopenharmony_ci {REG_MICFIL_FIFO_STAT, 0x00000000}, 4768c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH0, 0x00000000}, 4778c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH1, 0x00000000}, 4788c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH2, 0x00000000}, 4798c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH3, 0x00000000}, 4808c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH4, 0x00000000}, 4818c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH5, 0x00000000}, 4828c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH6, 0x00000000}, 4838c2ecf20Sopenharmony_ci {REG_MICFIL_DATACH7, 0x00000000}, 4848c2ecf20Sopenharmony_ci {REG_MICFIL_DC_CTRL, 0x00000000}, 4858c2ecf20Sopenharmony_ci {REG_MICFIL_OUT_CTRL, 0x00000000}, 4868c2ecf20Sopenharmony_ci {REG_MICFIL_OUT_STAT, 0x00000000}, 4878c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_CTRL1, 0x00000000}, 4888c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_CTRL2, 0x000A0000}, 4898c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_STAT, 0x00000000}, 4908c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_SCONFIG, 0x00000000}, 4918c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_NCONFIG, 0x80000000}, 4928c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_NDATA, 0x00000000}, 4938c2ecf20Sopenharmony_ci {REG_MICFIL_VAD0_ZCD, 0x00000004}, 4948c2ecf20Sopenharmony_ci}; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_cistatic bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg) 4978c2ecf20Sopenharmony_ci{ 4988c2ecf20Sopenharmony_ci switch (reg) { 4998c2ecf20Sopenharmony_ci case REG_MICFIL_CTRL1: 5008c2ecf20Sopenharmony_ci case REG_MICFIL_CTRL2: 5018c2ecf20Sopenharmony_ci case REG_MICFIL_STAT: 5028c2ecf20Sopenharmony_ci case REG_MICFIL_FIFO_CTRL: 5038c2ecf20Sopenharmony_ci case REG_MICFIL_FIFO_STAT: 5048c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH0: 5058c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH1: 5068c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH2: 5078c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH3: 5088c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH4: 5098c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH5: 5108c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH6: 5118c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH7: 5128c2ecf20Sopenharmony_ci case REG_MICFIL_DC_CTRL: 5138c2ecf20Sopenharmony_ci case REG_MICFIL_OUT_CTRL: 5148c2ecf20Sopenharmony_ci case REG_MICFIL_OUT_STAT: 5158c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_CTRL1: 5168c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_CTRL2: 5178c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_STAT: 5188c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_SCONFIG: 5198c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_NCONFIG: 5208c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_NDATA: 5218c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_ZCD: 5228c2ecf20Sopenharmony_ci return true; 5238c2ecf20Sopenharmony_ci default: 5248c2ecf20Sopenharmony_ci return false; 5258c2ecf20Sopenharmony_ci } 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_cistatic bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg) 5298c2ecf20Sopenharmony_ci{ 5308c2ecf20Sopenharmony_ci switch (reg) { 5318c2ecf20Sopenharmony_ci case REG_MICFIL_CTRL1: 5328c2ecf20Sopenharmony_ci case REG_MICFIL_CTRL2: 5338c2ecf20Sopenharmony_ci case REG_MICFIL_STAT: /* Write 1 to Clear */ 5348c2ecf20Sopenharmony_ci case REG_MICFIL_FIFO_CTRL: 5358c2ecf20Sopenharmony_ci case REG_MICFIL_FIFO_STAT: /* Write 1 to Clear */ 5368c2ecf20Sopenharmony_ci case REG_MICFIL_DC_CTRL: 5378c2ecf20Sopenharmony_ci case REG_MICFIL_OUT_CTRL: 5388c2ecf20Sopenharmony_ci case REG_MICFIL_OUT_STAT: /* Write 1 to Clear */ 5398c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_CTRL1: 5408c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_CTRL2: 5418c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_STAT: /* Write 1 to Clear */ 5428c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_SCONFIG: 5438c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_NCONFIG: 5448c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_ZCD: 5458c2ecf20Sopenharmony_ci return true; 5468c2ecf20Sopenharmony_ci default: 5478c2ecf20Sopenharmony_ci return false; 5488c2ecf20Sopenharmony_ci } 5498c2ecf20Sopenharmony_ci} 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_cistatic bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg) 5528c2ecf20Sopenharmony_ci{ 5538c2ecf20Sopenharmony_ci switch (reg) { 5548c2ecf20Sopenharmony_ci case REG_MICFIL_STAT: 5558c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH0: 5568c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH1: 5578c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH2: 5588c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH3: 5598c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH4: 5608c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH5: 5618c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH6: 5628c2ecf20Sopenharmony_ci case REG_MICFIL_DATACH7: 5638c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_STAT: 5648c2ecf20Sopenharmony_ci case REG_MICFIL_VAD0_NDATA: 5658c2ecf20Sopenharmony_ci return true; 5668c2ecf20Sopenharmony_ci default: 5678c2ecf20Sopenharmony_ci return false; 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci} 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_cistatic const struct regmap_config fsl_micfil_regmap_config = { 5728c2ecf20Sopenharmony_ci .reg_bits = 32, 5738c2ecf20Sopenharmony_ci .reg_stride = 4, 5748c2ecf20Sopenharmony_ci .val_bits = 32, 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci .max_register = REG_MICFIL_VAD0_ZCD, 5778c2ecf20Sopenharmony_ci .reg_defaults = fsl_micfil_reg_defaults, 5788c2ecf20Sopenharmony_ci .num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults), 5798c2ecf20Sopenharmony_ci .readable_reg = fsl_micfil_readable_reg, 5808c2ecf20Sopenharmony_ci .volatile_reg = fsl_micfil_volatile_reg, 5818c2ecf20Sopenharmony_ci .writeable_reg = fsl_micfil_writeable_reg, 5828c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 5838c2ecf20Sopenharmony_ci}; 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci/* END OF REGMAP */ 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_cistatic irqreturn_t micfil_isr(int irq, void *devid) 5888c2ecf20Sopenharmony_ci{ 5898c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = (struct fsl_micfil *)devid; 5908c2ecf20Sopenharmony_ci struct platform_device *pdev = micfil->pdev; 5918c2ecf20Sopenharmony_ci u32 stat_reg; 5928c2ecf20Sopenharmony_ci u32 fifo_stat_reg; 5938c2ecf20Sopenharmony_ci u32 ctrl1_reg; 5948c2ecf20Sopenharmony_ci bool dma_enabled; 5958c2ecf20Sopenharmony_ci int i; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg); 5988c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg); 5998c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg); 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci dma_enabled = MICFIL_DMA_ENABLED(ctrl1_reg); 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci /* Channel 0-7 Output Data Flags */ 6048c2ecf20Sopenharmony_ci for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) { 6058c2ecf20Sopenharmony_ci if (stat_reg & MICFIL_STAT_CHXF_MASK(i)) 6068c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, 6078c2ecf20Sopenharmony_ci "Data available in Data Channel %d\n", i); 6088c2ecf20Sopenharmony_ci /* if DMA is not enabled, field must be written with 1 6098c2ecf20Sopenharmony_ci * to clear 6108c2ecf20Sopenharmony_ci */ 6118c2ecf20Sopenharmony_ci if (!dma_enabled) 6128c2ecf20Sopenharmony_ci regmap_write_bits(micfil->regmap, 6138c2ecf20Sopenharmony_ci REG_MICFIL_STAT, 6148c2ecf20Sopenharmony_ci MICFIL_STAT_CHXF_MASK(i), 6158c2ecf20Sopenharmony_ci 1); 6168c2ecf20Sopenharmony_ci } 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci for (i = 0; i < MICFIL_FIFO_NUM; i++) { 6198c2ecf20Sopenharmony_ci if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER_MASK(i)) 6208c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, 6218c2ecf20Sopenharmony_ci "FIFO Overflow Exception flag for channel %d\n", 6228c2ecf20Sopenharmony_ci i); 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER_MASK(i)) 6258c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, 6268c2ecf20Sopenharmony_ci "FIFO Underflow Exception flag for channel %d\n", 6278c2ecf20Sopenharmony_ci i); 6288c2ecf20Sopenharmony_ci } 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci return IRQ_HANDLED; 6318c2ecf20Sopenharmony_ci} 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_cistatic irqreturn_t micfil_err_isr(int irq, void *devid) 6348c2ecf20Sopenharmony_ci{ 6358c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = (struct fsl_micfil *)devid; 6368c2ecf20Sopenharmony_ci struct platform_device *pdev = micfil->pdev; 6378c2ecf20Sopenharmony_ci u32 stat_reg; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci if (stat_reg & MICFIL_STAT_BSY_FIL_MASK) 6428c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n"); 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci if (stat_reg & MICFIL_STAT_FIR_RDY_MASK) 6458c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n"); 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci if (stat_reg & MICFIL_STAT_LOWFREQF_MASK) { 6488c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n"); 6498c2ecf20Sopenharmony_ci regmap_write_bits(micfil->regmap, REG_MICFIL_STAT, 6508c2ecf20Sopenharmony_ci MICFIL_STAT_LOWFREQF_MASK, 1); 6518c2ecf20Sopenharmony_ci } 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci return IRQ_HANDLED; 6548c2ecf20Sopenharmony_ci} 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_cistatic int fsl_micfil_probe(struct platform_device *pdev) 6578c2ecf20Sopenharmony_ci{ 6588c2ecf20Sopenharmony_ci struct device_node *np = pdev->dev.of_node; 6598c2ecf20Sopenharmony_ci const struct of_device_id *of_id; 6608c2ecf20Sopenharmony_ci struct fsl_micfil *micfil; 6618c2ecf20Sopenharmony_ci struct resource *res; 6628c2ecf20Sopenharmony_ci void __iomem *regs; 6638c2ecf20Sopenharmony_ci int ret, i; 6648c2ecf20Sopenharmony_ci unsigned long irqflag = 0; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL); 6678c2ecf20Sopenharmony_ci if (!micfil) 6688c2ecf20Sopenharmony_ci return -ENOMEM; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci micfil->pdev = pdev; 6718c2ecf20Sopenharmony_ci strncpy(micfil->name, np->name, sizeof(micfil->name) - 1); 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci of_id = of_match_device(fsl_micfil_dt_ids, &pdev->dev); 6748c2ecf20Sopenharmony_ci if (!of_id || !of_id->data) 6758c2ecf20Sopenharmony_ci return -EINVAL; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci micfil->soc = of_id->data; 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci /* ipg_clk is used to control the registers 6808c2ecf20Sopenharmony_ci * ipg_clk_app is used to operate the filter 6818c2ecf20Sopenharmony_ci */ 6828c2ecf20Sopenharmony_ci micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app"); 6838c2ecf20Sopenharmony_ci if (IS_ERR(micfil->mclk)) { 6848c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to get core clock: %ld\n", 6858c2ecf20Sopenharmony_ci PTR_ERR(micfil->mclk)); 6868c2ecf20Sopenharmony_ci return PTR_ERR(micfil->mclk); 6878c2ecf20Sopenharmony_ci } 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci /* init regmap */ 6908c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6918c2ecf20Sopenharmony_ci regs = devm_ioremap_resource(&pdev->dev, res); 6928c2ecf20Sopenharmony_ci if (IS_ERR(regs)) 6938c2ecf20Sopenharmony_ci return PTR_ERR(regs); 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci micfil->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 6968c2ecf20Sopenharmony_ci "ipg_clk", 6978c2ecf20Sopenharmony_ci regs, 6988c2ecf20Sopenharmony_ci &fsl_micfil_regmap_config); 6998c2ecf20Sopenharmony_ci if (IS_ERR(micfil->regmap)) { 7008c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n", 7018c2ecf20Sopenharmony_ci PTR_ERR(micfil->regmap)); 7028c2ecf20Sopenharmony_ci return PTR_ERR(micfil->regmap); 7038c2ecf20Sopenharmony_ci } 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci /* dataline mask for RX */ 7068c2ecf20Sopenharmony_ci ret = of_property_read_u32_index(np, 7078c2ecf20Sopenharmony_ci "fsl,dataline", 7088c2ecf20Sopenharmony_ci 0, 7098c2ecf20Sopenharmony_ci &micfil->dataline); 7108c2ecf20Sopenharmony_ci if (ret) 7118c2ecf20Sopenharmony_ci micfil->dataline = 1; 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci if (micfil->dataline & ~micfil->soc->dataline) { 7148c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n", 7158c2ecf20Sopenharmony_ci micfil->soc->dataline); 7168c2ecf20Sopenharmony_ci return -EINVAL; 7178c2ecf20Sopenharmony_ci } 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci /* get IRQs */ 7208c2ecf20Sopenharmony_ci for (i = 0; i < MICFIL_IRQ_LINES; i++) { 7218c2ecf20Sopenharmony_ci micfil->irq[i] = platform_get_irq(pdev, i); 7228c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]); 7238c2ecf20Sopenharmony_ci if (micfil->irq[i] < 0) 7248c2ecf20Sopenharmony_ci return micfil->irq[i]; 7258c2ecf20Sopenharmony_ci } 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci if (of_property_read_bool(np, "fsl,shared-interrupt")) 7288c2ecf20Sopenharmony_ci irqflag = IRQF_SHARED; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci /* Digital Microphone interface interrupt */ 7318c2ecf20Sopenharmony_ci ret = devm_request_irq(&pdev->dev, micfil->irq[0], 7328c2ecf20Sopenharmony_ci micfil_isr, irqflag, 7338c2ecf20Sopenharmony_ci micfil->name, micfil); 7348c2ecf20Sopenharmony_ci if (ret) { 7358c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to claim mic interface irq %u\n", 7368c2ecf20Sopenharmony_ci micfil->irq[0]); 7378c2ecf20Sopenharmony_ci return ret; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci /* Digital Microphone interface error interrupt */ 7418c2ecf20Sopenharmony_ci ret = devm_request_irq(&pdev->dev, micfil->irq[1], 7428c2ecf20Sopenharmony_ci micfil_err_isr, irqflag, 7438c2ecf20Sopenharmony_ci micfil->name, micfil); 7448c2ecf20Sopenharmony_ci if (ret) { 7458c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n", 7468c2ecf20Sopenharmony_ci micfil->irq[1]); 7478c2ecf20Sopenharmony_ci return ret; 7488c2ecf20Sopenharmony_ci } 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci micfil->dma_params_rx.chan_name = "rx"; 7518c2ecf20Sopenharmony_ci micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0; 7528c2ecf20Sopenharmony_ci micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX; 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, micfil); 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci pm_runtime_enable(&pdev->dev); 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component, 7608c2ecf20Sopenharmony_ci &fsl_micfil_dai, 1); 7618c2ecf20Sopenharmony_ci if (ret) { 7628c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register component %s\n", 7638c2ecf20Sopenharmony_ci fsl_micfil_component.name); 7648c2ecf20Sopenharmony_ci return ret; 7658c2ecf20Sopenharmony_ci } 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); 7688c2ecf20Sopenharmony_ci if (ret) 7698c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to pcm register\n"); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci return ret; 7728c2ecf20Sopenharmony_ci} 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_cistatic int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev) 7758c2ecf20Sopenharmony_ci{ 7768c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = dev_get_drvdata(dev); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci regcache_cache_only(micfil->regmap, true); 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_ci clk_disable_unprepare(micfil->mclk); 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci return 0; 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_cistatic int __maybe_unused fsl_micfil_runtime_resume(struct device *dev) 7868c2ecf20Sopenharmony_ci{ 7878c2ecf20Sopenharmony_ci struct fsl_micfil *micfil = dev_get_drvdata(dev); 7888c2ecf20Sopenharmony_ci int ret; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci ret = clk_prepare_enable(micfil->mclk); 7918c2ecf20Sopenharmony_ci if (ret < 0) 7928c2ecf20Sopenharmony_ci return ret; 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci regcache_cache_only(micfil->regmap, false); 7958c2ecf20Sopenharmony_ci regcache_mark_dirty(micfil->regmap); 7968c2ecf20Sopenharmony_ci regcache_sync(micfil->regmap); 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci return 0; 7998c2ecf20Sopenharmony_ci} 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_cistatic int __maybe_unused fsl_micfil_suspend(struct device *dev) 8028c2ecf20Sopenharmony_ci{ 8038c2ecf20Sopenharmony_ci pm_runtime_force_suspend(dev); 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci return 0; 8068c2ecf20Sopenharmony_ci} 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_cistatic int __maybe_unused fsl_micfil_resume(struct device *dev) 8098c2ecf20Sopenharmony_ci{ 8108c2ecf20Sopenharmony_ci pm_runtime_force_resume(dev); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci return 0; 8138c2ecf20Sopenharmony_ci} 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_cistatic const struct dev_pm_ops fsl_micfil_pm_ops = { 8168c2ecf20Sopenharmony_ci SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend, 8178c2ecf20Sopenharmony_ci fsl_micfil_runtime_resume, 8188c2ecf20Sopenharmony_ci NULL) 8198c2ecf20Sopenharmony_ci SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend, 8208c2ecf20Sopenharmony_ci fsl_micfil_resume) 8218c2ecf20Sopenharmony_ci}; 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_cistatic struct platform_driver fsl_micfil_driver = { 8248c2ecf20Sopenharmony_ci .probe = fsl_micfil_probe, 8258c2ecf20Sopenharmony_ci .driver = { 8268c2ecf20Sopenharmony_ci .name = "fsl-micfil-dai", 8278c2ecf20Sopenharmony_ci .pm = &fsl_micfil_pm_ops, 8288c2ecf20Sopenharmony_ci .of_match_table = fsl_micfil_dt_ids, 8298c2ecf20Sopenharmony_ci }, 8308c2ecf20Sopenharmony_ci}; 8318c2ecf20Sopenharmony_cimodule_platform_driver(fsl_micfil_driver); 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ciMODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>"); 8348c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver"); 8358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 836