18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* Atmel PDMIC driver 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2015 Atmel 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Author: Songjun Wu <songjun.wu@atmel.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/of.h> 108c2ecf20Sopenharmony_ci#include <linux/clk.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 138c2ecf20Sopenharmony_ci#include <linux/regmap.h> 148c2ecf20Sopenharmony_ci#include <sound/core.h> 158c2ecf20Sopenharmony_ci#include <sound/dmaengine_pcm.h> 168c2ecf20Sopenharmony_ci#include <sound/pcm_params.h> 178c2ecf20Sopenharmony_ci#include <sound/tlv.h> 188c2ecf20Sopenharmony_ci#include "atmel-pdmic.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct atmel_pdmic_pdata { 218c2ecf20Sopenharmony_ci u32 mic_min_freq; 228c2ecf20Sopenharmony_ci u32 mic_max_freq; 238c2ecf20Sopenharmony_ci s32 mic_offset; 248c2ecf20Sopenharmony_ci const char *card_name; 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct atmel_pdmic { 288c2ecf20Sopenharmony_ci dma_addr_t phy_base; 298c2ecf20Sopenharmony_ci struct regmap *regmap; 308c2ecf20Sopenharmony_ci struct clk *pclk; 318c2ecf20Sopenharmony_ci struct clk *gclk; 328c2ecf20Sopenharmony_ci struct device *dev; 338c2ecf20Sopenharmony_ci int irq; 348c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream; 358c2ecf20Sopenharmony_ci const struct atmel_pdmic_pdata *pdata; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic const struct of_device_id atmel_pdmic_of_match[] = { 398c2ecf20Sopenharmony_ci { 408c2ecf20Sopenharmony_ci .compatible = "atmel,sama5d2-pdmic", 418c2ecf20Sopenharmony_ci }, { 428c2ecf20Sopenharmony_ci /* sentinel */ 438c2ecf20Sopenharmony_ci } 448c2ecf20Sopenharmony_ci}; 458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, atmel_pdmic_of_match); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define PDMIC_OFFSET_MAX_VAL S16_MAX 488c2ecf20Sopenharmony_ci#define PDMIC_OFFSET_MIN_VAL S16_MIN 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic struct atmel_pdmic_pdata *atmel_pdmic_dt_init(struct device *dev) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 538c2ecf20Sopenharmony_ci struct atmel_pdmic_pdata *pdata; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (!np) { 568c2ecf20Sopenharmony_ci dev_err(dev, "device node not found\n"); 578c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 618c2ecf20Sopenharmony_ci if (!pdata) 628c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci if (of_property_read_string(np, "atmel,model", &pdata->card_name)) 658c2ecf20Sopenharmony_ci pdata->card_name = "PDMIC"; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (of_property_read_u32(np, "atmel,mic-min-freq", 688c2ecf20Sopenharmony_ci &pdata->mic_min_freq)) { 698c2ecf20Sopenharmony_ci dev_err(dev, "failed to get mic-min-freq\n"); 708c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (of_property_read_u32(np, "atmel,mic-max-freq", 748c2ecf20Sopenharmony_ci &pdata->mic_max_freq)) { 758c2ecf20Sopenharmony_ci dev_err(dev, "failed to get mic-max-freq\n"); 768c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci if (pdata->mic_max_freq < pdata->mic_min_freq) { 808c2ecf20Sopenharmony_ci dev_err(dev, 818c2ecf20Sopenharmony_ci "mic-max-freq should not be less than mic-min-freq\n"); 828c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (of_property_read_s32(np, "atmel,mic-offset", &pdata->mic_offset)) 868c2ecf20Sopenharmony_ci pdata->mic_offset = 0; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (pdata->mic_offset > PDMIC_OFFSET_MAX_VAL) { 898c2ecf20Sopenharmony_ci dev_warn(dev, 908c2ecf20Sopenharmony_ci "mic-offset value %d is larger than the max value %d, the max value is specified\n", 918c2ecf20Sopenharmony_ci pdata->mic_offset, PDMIC_OFFSET_MAX_VAL); 928c2ecf20Sopenharmony_ci pdata->mic_offset = PDMIC_OFFSET_MAX_VAL; 938c2ecf20Sopenharmony_ci } else if (pdata->mic_offset < PDMIC_OFFSET_MIN_VAL) { 948c2ecf20Sopenharmony_ci dev_warn(dev, 958c2ecf20Sopenharmony_ci "mic-offset value %d is less than the min value %d, the min value is specified\n", 968c2ecf20Sopenharmony_ci pdata->mic_offset, PDMIC_OFFSET_MIN_VAL); 978c2ecf20Sopenharmony_ci pdata->mic_offset = PDMIC_OFFSET_MIN_VAL; 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci return pdata; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* cpu dai component */ 1048c2ecf20Sopenharmony_cistatic int atmel_pdmic_cpu_dai_startup(struct snd_pcm_substream *substream, 1058c2ecf20Sopenharmony_ci struct snd_soc_dai *cpu_dai) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 1088c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card); 1098c2ecf20Sopenharmony_ci int ret; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci ret = clk_prepare_enable(dd->gclk); 1128c2ecf20Sopenharmony_ci if (ret) 1138c2ecf20Sopenharmony_ci return ret; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci ret = clk_prepare_enable(dd->pclk); 1168c2ecf20Sopenharmony_ci if (ret) { 1178c2ecf20Sopenharmony_ci clk_disable_unprepare(dd->gclk); 1188c2ecf20Sopenharmony_ci return ret; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* Clear all bits in the Control Register(PDMIC_CR) */ 1228c2ecf20Sopenharmony_ci regmap_write(dd->regmap, PDMIC_CR, 0); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci dd->substream = substream; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Enable the overrun error interrupt */ 1278c2ecf20Sopenharmony_ci regmap_write(dd->regmap, PDMIC_IER, PDMIC_IER_OVRE); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic void atmel_pdmic_cpu_dai_shutdown(struct snd_pcm_substream *substream, 1338c2ecf20Sopenharmony_ci struct snd_soc_dai *cpu_dai) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 1368c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* Disable the overrun error interrupt */ 1398c2ecf20Sopenharmony_ci regmap_write(dd->regmap, PDMIC_IDR, PDMIC_IDR_OVRE); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci clk_disable_unprepare(dd->gclk); 1428c2ecf20Sopenharmony_ci clk_disable_unprepare(dd->pclk); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int atmel_pdmic_cpu_dai_prepare(struct snd_pcm_substream *substream, 1468c2ecf20Sopenharmony_ci struct snd_soc_dai *cpu_dai) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 1498c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card); 1508c2ecf20Sopenharmony_ci struct snd_soc_component *component = cpu_dai->component; 1518c2ecf20Sopenharmony_ci u32 val; 1528c2ecf20Sopenharmony_ci int ret; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* Clean the PDMIC Converted Data Register */ 1558c2ecf20Sopenharmony_ci ret = regmap_read(dd->regmap, PDMIC_CDR, &val); 1568c2ecf20Sopenharmony_ci if (ret < 0) 1578c2ecf20Sopenharmony_ci return 0; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci ret = snd_soc_component_update_bits(component, PDMIC_CR, 1608c2ecf20Sopenharmony_ci PDMIC_CR_ENPDM_MASK, 1618c2ecf20Sopenharmony_ci PDMIC_CR_ENPDM_DIS << 1628c2ecf20Sopenharmony_ci PDMIC_CR_ENPDM_SHIFT); 1638c2ecf20Sopenharmony_ci if (ret < 0) 1648c2ecf20Sopenharmony_ci return ret; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return 0; 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci#define ATMEL_PDMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE) 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* platform */ 1728c2ecf20Sopenharmony_ci#define ATMEL_PDMIC_MAX_BUF_SIZE (64 * 1024) 1738c2ecf20Sopenharmony_ci#define ATMEL_PDMIC_PREALLOC_BUF_SIZE ATMEL_PDMIC_MAX_BUF_SIZE 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware atmel_pdmic_hw = { 1768c2ecf20Sopenharmony_ci .info = SNDRV_PCM_INFO_MMAP 1778c2ecf20Sopenharmony_ci | SNDRV_PCM_INFO_MMAP_VALID 1788c2ecf20Sopenharmony_ci | SNDRV_PCM_INFO_INTERLEAVED 1798c2ecf20Sopenharmony_ci | SNDRV_PCM_INFO_RESUME 1808c2ecf20Sopenharmony_ci | SNDRV_PCM_INFO_PAUSE, 1818c2ecf20Sopenharmony_ci .formats = ATMEL_PDMIC_FORMATS, 1828c2ecf20Sopenharmony_ci .buffer_bytes_max = ATMEL_PDMIC_MAX_BUF_SIZE, 1838c2ecf20Sopenharmony_ci .period_bytes_min = 256, 1848c2ecf20Sopenharmony_ci .period_bytes_max = 32 * 1024, 1858c2ecf20Sopenharmony_ci .periods_min = 2, 1868c2ecf20Sopenharmony_ci .periods_max = 256, 1878c2ecf20Sopenharmony_ci}; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic int 1908c2ecf20Sopenharmony_ciatmel_pdmic_platform_configure_dma(struct snd_pcm_substream *substream, 1918c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *params, 1928c2ecf20Sopenharmony_ci struct dma_slave_config *slave_config) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 1958c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card); 1968c2ecf20Sopenharmony_ci int ret; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci ret = snd_hwparams_to_dma_slave_config(substream, params, 1998c2ecf20Sopenharmony_ci slave_config); 2008c2ecf20Sopenharmony_ci if (ret) { 2018c2ecf20Sopenharmony_ci dev_err(dd->dev, 2028c2ecf20Sopenharmony_ci "hw params to dma slave configure failed\n"); 2038c2ecf20Sopenharmony_ci return ret; 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci slave_config->src_addr = dd->phy_base + PDMIC_CDR; 2078c2ecf20Sopenharmony_ci slave_config->src_maxburst = 1; 2088c2ecf20Sopenharmony_ci slave_config->dst_maxburst = 1; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return 0; 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic const struct snd_dmaengine_pcm_config 2148c2ecf20Sopenharmony_ciatmel_pdmic_dmaengine_pcm_config = { 2158c2ecf20Sopenharmony_ci .prepare_slave_config = atmel_pdmic_platform_configure_dma, 2168c2ecf20Sopenharmony_ci .pcm_hardware = &atmel_pdmic_hw, 2178c2ecf20Sopenharmony_ci .prealloc_buffer_size = ATMEL_PDMIC_PREALLOC_BUF_SIZE, 2188c2ecf20Sopenharmony_ci}; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci/* codec */ 2218c2ecf20Sopenharmony_ci/* Mic Gain = dgain * 2^(-scale) */ 2228c2ecf20Sopenharmony_cistruct mic_gain { 2238c2ecf20Sopenharmony_ci unsigned int dgain; 2248c2ecf20Sopenharmony_ci unsigned int scale; 2258c2ecf20Sopenharmony_ci}; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci/* range from -90 dB to 90 dB */ 2288c2ecf20Sopenharmony_cistatic const struct mic_gain mic_gain_table[] = { 2298c2ecf20Sopenharmony_ci{ 1, 15}, { 1, 14}, /* -90, -84 dB */ 2308c2ecf20Sopenharmony_ci{ 3, 15}, { 1, 13}, { 3, 14}, { 1, 12}, /* -81, -78, -75, -72 dB */ 2318c2ecf20Sopenharmony_ci{ 5, 14}, { 13, 15}, /* -70, -68 dB */ 2328c2ecf20Sopenharmony_ci{ 9, 14}, { 21, 15}, { 23, 15}, { 13, 14}, /* -65 ~ -62 dB */ 2338c2ecf20Sopenharmony_ci{ 29, 15}, { 33, 15}, { 37, 15}, { 41, 15}, /* -61 ~ -58 dB */ 2348c2ecf20Sopenharmony_ci{ 23, 14}, { 13, 13}, { 58, 15}, { 65, 15}, /* -57 ~ -54 dB */ 2358c2ecf20Sopenharmony_ci{ 73, 15}, { 41, 14}, { 23, 13}, { 13, 12}, /* -53 ~ -50 dB */ 2368c2ecf20Sopenharmony_ci{ 29, 13}, { 65, 14}, { 73, 14}, { 41, 13}, /* -49 ~ -46 dB */ 2378c2ecf20Sopenharmony_ci{ 23, 12}, { 207, 15}, { 29, 12}, { 65, 13}, /* -45 ~ -42 dB */ 2388c2ecf20Sopenharmony_ci{ 73, 13}, { 41, 12}, { 23, 11}, { 413, 15}, /* -41 ~ -38 dB */ 2398c2ecf20Sopenharmony_ci{ 463, 15}, { 519, 15}, { 583, 15}, { 327, 14}, /* -37 ~ -34 dB */ 2408c2ecf20Sopenharmony_ci{ 367, 14}, { 823, 15}, { 231, 13}, { 1036, 15}, /* -33 ~ -30 dB */ 2418c2ecf20Sopenharmony_ci{ 1163, 15}, { 1305, 15}, { 183, 12}, { 1642, 15}, /* -29 ~ -26 dB */ 2428c2ecf20Sopenharmony_ci{ 1843, 15}, { 2068, 15}, { 145, 11}, { 2603, 15}, /* -25 ~ -22 dB */ 2438c2ecf20Sopenharmony_ci{ 365, 12}, { 3277, 15}, { 3677, 15}, { 4125, 15}, /* -21 ~ -18 dB */ 2448c2ecf20Sopenharmony_ci{ 4629, 15}, { 5193, 15}, { 5827, 15}, { 3269, 14}, /* -17 ~ -14 dB */ 2458c2ecf20Sopenharmony_ci{ 917, 12}, { 8231, 15}, { 9235, 15}, { 5181, 14}, /* -13 ~ -10 dB */ 2468c2ecf20Sopenharmony_ci{11627, 15}, {13045, 15}, {14637, 15}, {16423, 15}, /* -9 ~ -6 dB */ 2478c2ecf20Sopenharmony_ci{18427, 15}, {20675, 15}, { 5799, 13}, {26029, 15}, /* -5 ~ -2 dB */ 2488c2ecf20Sopenharmony_ci{ 7301, 13}, { 1, 0}, {18383, 14}, {10313, 13}, /* -1 ~ 2 dB */ 2498c2ecf20Sopenharmony_ci{23143, 14}, {25967, 14}, {29135, 14}, {16345, 13}, /* 3 ~ 6 dB */ 2508c2ecf20Sopenharmony_ci{ 4585, 11}, {20577, 13}, { 1443, 9}, {25905, 13}, /* 7 ~ 10 dB */ 2518c2ecf20Sopenharmony_ci{14533, 12}, { 8153, 11}, { 2287, 9}, {20529, 12}, /* 11 ~ 14 dB */ 2528c2ecf20Sopenharmony_ci{11517, 11}, { 6461, 10}, {28997, 12}, { 4067, 9}, /* 15 ~ 18 dB */ 2538c2ecf20Sopenharmony_ci{18253, 11}, { 10, 0}, {22979, 11}, {25783, 11}, /* 19 ~ 22 dB */ 2548c2ecf20Sopenharmony_ci{28929, 11}, {32459, 11}, { 9105, 9}, {20431, 10}, /* 23 ~ 26 dB */ 2558c2ecf20Sopenharmony_ci{22925, 10}, {12861, 9}, { 7215, 8}, {16191, 9}, /* 27 ~ 30 dB */ 2568c2ecf20Sopenharmony_ci{ 9083, 8}, {20383, 9}, {11435, 8}, { 6145, 7}, /* 31 ~ 34 dB */ 2578c2ecf20Sopenharmony_ci{ 3599, 6}, {32305, 9}, {18123, 8}, {20335, 8}, /* 35 ~ 38 dB */ 2588c2ecf20Sopenharmony_ci{ 713, 3}, { 100, 0}, { 7181, 6}, { 8057, 6}, /* 39 ~ 42 dB */ 2598c2ecf20Sopenharmony_ci{ 565, 2}, {20287, 7}, {11381, 6}, {25539, 7}, /* 43 ~ 46 dB */ 2608c2ecf20Sopenharmony_ci{ 1791, 3}, { 4019, 4}, { 9019, 5}, {20239, 6}, /* 47 ~ 50 dB */ 2618c2ecf20Sopenharmony_ci{ 5677, 4}, {25479, 6}, { 7147, 4}, { 8019, 4}, /* 51 ~ 54 dB */ 2628c2ecf20Sopenharmony_ci{17995, 5}, {20191, 5}, {11327, 4}, {12709, 4}, /* 55 ~ 58 dB */ 2638c2ecf20Sopenharmony_ci{ 3565, 2}, { 1000, 0}, { 1122, 0}, { 1259, 0}, /* 59 ~ 62 dB */ 2648c2ecf20Sopenharmony_ci{ 2825, 1}, {12679, 3}, { 7113, 2}, { 7981, 2}, /* 63 ~ 66 dB */ 2658c2ecf20Sopenharmony_ci{ 8955, 2}, {20095, 3}, {22547, 3}, {12649, 2}, /* 67 ~ 70 dB */ 2668c2ecf20Sopenharmony_ci{28385, 3}, { 3981, 0}, {17867, 2}, {20047, 2}, /* 71 ~ 74 dB */ 2678c2ecf20Sopenharmony_ci{11247, 1}, {12619, 1}, {14159, 1}, {31773, 2}, /* 75 ~ 78 dB */ 2688c2ecf20Sopenharmony_ci{17825, 1}, {10000, 0}, {11220, 0}, {12589, 0}, /* 79 ~ 82 dB */ 2698c2ecf20Sopenharmony_ci{28251, 1}, {15849, 0}, {17783, 0}, {19953, 0}, /* 83 ~ 86 dB */ 2708c2ecf20Sopenharmony_ci{22387, 0}, {25119, 0}, {28184, 0}, {31623, 0}, /* 87 ~ 90 dB */ 2718c2ecf20Sopenharmony_ci}; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(mic_gain_tlv, 2748c2ecf20Sopenharmony_ci 0, 1, TLV_DB_SCALE_ITEM(-9000, 600, 0), 2758c2ecf20Sopenharmony_ci 2, 5, TLV_DB_SCALE_ITEM(-8100, 300, 0), 2768c2ecf20Sopenharmony_ci 6, 7, TLV_DB_SCALE_ITEM(-7000, 200, 0), 2778c2ecf20Sopenharmony_ci 8, ARRAY_SIZE(mic_gain_table)-1, TLV_DB_SCALE_ITEM(-6500, 100, 0), 2788c2ecf20Sopenharmony_ci); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic int pdmic_get_mic_volsw(struct snd_kcontrol *kcontrol, 2818c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 2848c2ecf20Sopenharmony_ci unsigned int dgain_val, scale_val; 2858c2ecf20Sopenharmony_ci int i; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci dgain_val = (snd_soc_component_read(component, PDMIC_DSPR1) & PDMIC_DSPR1_DGAIN_MASK) 2888c2ecf20Sopenharmony_ci >> PDMIC_DSPR1_DGAIN_SHIFT; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci scale_val = (snd_soc_component_read(component, PDMIC_DSPR0) & PDMIC_DSPR0_SCALE_MASK) 2918c2ecf20Sopenharmony_ci >> PDMIC_DSPR0_SCALE_SHIFT; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(mic_gain_table); i++) { 2948c2ecf20Sopenharmony_ci if ((mic_gain_table[i].dgain == dgain_val) && 2958c2ecf20Sopenharmony_ci (mic_gain_table[i].scale == scale_val)) 2968c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = i; 2978c2ecf20Sopenharmony_ci } 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci return 0; 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_cistatic int pdmic_put_mic_volsw(struct snd_kcontrol *kcontrol, 3038c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci struct soc_mixer_control *mc = 3068c2ecf20Sopenharmony_ci (struct soc_mixer_control *)kcontrol->private_value; 3078c2ecf20Sopenharmony_ci struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 3088c2ecf20Sopenharmony_ci int max = mc->max; 3098c2ecf20Sopenharmony_ci unsigned int val; 3108c2ecf20Sopenharmony_ci int ret; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci val = ucontrol->value.integer.value[0]; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci if (val > max) 3158c2ecf20Sopenharmony_ci return -EINVAL; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci ret = snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_MASK, 3188c2ecf20Sopenharmony_ci mic_gain_table[val].dgain << PDMIC_DSPR1_DGAIN_SHIFT); 3198c2ecf20Sopenharmony_ci if (ret < 0) 3208c2ecf20Sopenharmony_ci return ret; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci ret = snd_soc_component_update_bits(component, PDMIC_DSPR0, PDMIC_DSPR0_SCALE_MASK, 3238c2ecf20Sopenharmony_ci mic_gain_table[val].scale << PDMIC_DSPR0_SCALE_SHIFT); 3248c2ecf20Sopenharmony_ci if (ret < 0) 3258c2ecf20Sopenharmony_ci return ret; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci return 0; 3288c2ecf20Sopenharmony_ci} 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new atmel_pdmic_snd_controls[] = { 3318c2ecf20Sopenharmony_ciSOC_SINGLE_EXT_TLV("Mic Capture Volume", PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_SHIFT, 3328c2ecf20Sopenharmony_ci ARRAY_SIZE(mic_gain_table)-1, 0, 3338c2ecf20Sopenharmony_ci pdmic_get_mic_volsw, pdmic_put_mic_volsw, mic_gain_tlv), 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ciSOC_SINGLE("High Pass Filter Switch", PDMIC_DSPR0, 3368c2ecf20Sopenharmony_ci PDMIC_DSPR0_HPFBYP_SHIFT, 1, 1), 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ciSOC_SINGLE("SINCC Filter Switch", PDMIC_DSPR0, PDMIC_DSPR0_SINBYP_SHIFT, 1, 1), 3398c2ecf20Sopenharmony_ci}; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_cistatic int atmel_pdmic_component_probe(struct snd_soc_component *component) 3428c2ecf20Sopenharmony_ci{ 3438c2ecf20Sopenharmony_ci struct snd_soc_card *card = snd_soc_component_get_drvdata(component); 3448c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_OFFSET_MASK, 3478c2ecf20Sopenharmony_ci (u32)(dd->pdata->mic_offset << PDMIC_DSPR1_OFFSET_SHIFT)); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci return 0; 3508c2ecf20Sopenharmony_ci} 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci#define PDMIC_MR_PRESCAL_MAX_VAL 127 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic int 3558c2ecf20Sopenharmony_ciatmel_pdmic_cpu_dai_hw_params(struct snd_pcm_substream *substream, 3568c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *params, 3578c2ecf20Sopenharmony_ci struct snd_soc_dai *cpu_dai) 3588c2ecf20Sopenharmony_ci{ 3598c2ecf20Sopenharmony_ci struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 3608c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card); 3618c2ecf20Sopenharmony_ci struct snd_soc_component *component = cpu_dai->component; 3628c2ecf20Sopenharmony_ci unsigned int rate_min = substream->runtime->hw.rate_min; 3638c2ecf20Sopenharmony_ci unsigned int rate_max = substream->runtime->hw.rate_max; 3648c2ecf20Sopenharmony_ci int fs = params_rate(params); 3658c2ecf20Sopenharmony_ci int bits = params_width(params); 3668c2ecf20Sopenharmony_ci unsigned long pclk_rate, gclk_rate; 3678c2ecf20Sopenharmony_ci unsigned int f_pdmic; 3688c2ecf20Sopenharmony_ci u32 mr_val, dspr0_val, pclk_prescal, gclk_prescal; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci if (params_channels(params) != 1) { 3718c2ecf20Sopenharmony_ci dev_err(component->dev, 3728c2ecf20Sopenharmony_ci "only supports one channel\n"); 3738c2ecf20Sopenharmony_ci return -EINVAL; 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci if ((fs < rate_min) || (fs > rate_max)) { 3778c2ecf20Sopenharmony_ci dev_err(component->dev, 3788c2ecf20Sopenharmony_ci "sample rate is %dHz, min rate is %dHz, max rate is %dHz\n", 3798c2ecf20Sopenharmony_ci fs, rate_min, rate_max); 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci return -EINVAL; 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci switch (bits) { 3858c2ecf20Sopenharmony_ci case 16: 3868c2ecf20Sopenharmony_ci dspr0_val = (PDMIC_DSPR0_SIZE_16_BITS 3878c2ecf20Sopenharmony_ci << PDMIC_DSPR0_SIZE_SHIFT); 3888c2ecf20Sopenharmony_ci break; 3898c2ecf20Sopenharmony_ci case 32: 3908c2ecf20Sopenharmony_ci dspr0_val = (PDMIC_DSPR0_SIZE_32_BITS 3918c2ecf20Sopenharmony_ci << PDMIC_DSPR0_SIZE_SHIFT); 3928c2ecf20Sopenharmony_ci break; 3938c2ecf20Sopenharmony_ci default: 3948c2ecf20Sopenharmony_ci return -EINVAL; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci if ((fs << 7) > (rate_max << 6)) { 3988c2ecf20Sopenharmony_ci f_pdmic = fs << 6; 3998c2ecf20Sopenharmony_ci dspr0_val |= PDMIC_DSPR0_OSR_64 << PDMIC_DSPR0_OSR_SHIFT; 4008c2ecf20Sopenharmony_ci } else { 4018c2ecf20Sopenharmony_ci f_pdmic = fs << 7; 4028c2ecf20Sopenharmony_ci dspr0_val |= PDMIC_DSPR0_OSR_128 << PDMIC_DSPR0_OSR_SHIFT; 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci pclk_rate = clk_get_rate(dd->pclk); 4068c2ecf20Sopenharmony_ci gclk_rate = clk_get_rate(dd->gclk); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* PRESCAL = SELCK/(2*f_pdmic) - 1*/ 4098c2ecf20Sopenharmony_ci pclk_prescal = (u32)(pclk_rate/(f_pdmic << 1)) - 1; 4108c2ecf20Sopenharmony_ci gclk_prescal = (u32)(gclk_rate/(f_pdmic << 1)) - 1; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci if ((pclk_prescal > PDMIC_MR_PRESCAL_MAX_VAL) || 4138c2ecf20Sopenharmony_ci (gclk_rate/((gclk_prescal + 1) << 1) < 4148c2ecf20Sopenharmony_ci pclk_rate/((pclk_prescal + 1) << 1))) { 4158c2ecf20Sopenharmony_ci mr_val = gclk_prescal << PDMIC_MR_PRESCAL_SHIFT; 4168c2ecf20Sopenharmony_ci mr_val |= PDMIC_MR_CLKS_GCK << PDMIC_MR_CLKS_SHIFT; 4178c2ecf20Sopenharmony_ci } else { 4188c2ecf20Sopenharmony_ci mr_val = pclk_prescal << PDMIC_MR_PRESCAL_SHIFT; 4198c2ecf20Sopenharmony_ci mr_val |= PDMIC_MR_CLKS_PCK << PDMIC_MR_CLKS_SHIFT; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, PDMIC_MR, 4238c2ecf20Sopenharmony_ci PDMIC_MR_PRESCAL_MASK | PDMIC_MR_CLKS_MASK, mr_val); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, PDMIC_DSPR0, 4268c2ecf20Sopenharmony_ci PDMIC_DSPR0_OSR_MASK | PDMIC_DSPR0_SIZE_MASK, dspr0_val); 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci return 0; 4298c2ecf20Sopenharmony_ci} 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_cistatic int atmel_pdmic_cpu_dai_trigger(struct snd_pcm_substream *substream, 4328c2ecf20Sopenharmony_ci int cmd, struct snd_soc_dai *cpu_dai) 4338c2ecf20Sopenharmony_ci{ 4348c2ecf20Sopenharmony_ci struct snd_soc_component *component = cpu_dai->component; 4358c2ecf20Sopenharmony_ci u32 val; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci switch (cmd) { 4388c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 4398c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_RESUME: 4408c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 4418c2ecf20Sopenharmony_ci val = PDMIC_CR_ENPDM_EN << PDMIC_CR_ENPDM_SHIFT; 4428c2ecf20Sopenharmony_ci break; 4438c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 4448c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_SUSPEND: 4458c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 4468c2ecf20Sopenharmony_ci val = PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT; 4478c2ecf20Sopenharmony_ci break; 4488c2ecf20Sopenharmony_ci default: 4498c2ecf20Sopenharmony_ci return -EINVAL; 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, PDMIC_CR, PDMIC_CR_ENPDM_MASK, val); 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci return 0; 4558c2ecf20Sopenharmony_ci} 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops atmel_pdmic_cpu_dai_ops = { 4588c2ecf20Sopenharmony_ci .startup = atmel_pdmic_cpu_dai_startup, 4598c2ecf20Sopenharmony_ci .shutdown = atmel_pdmic_cpu_dai_shutdown, 4608c2ecf20Sopenharmony_ci .prepare = atmel_pdmic_cpu_dai_prepare, 4618c2ecf20Sopenharmony_ci .hw_params = atmel_pdmic_cpu_dai_hw_params, 4628c2ecf20Sopenharmony_ci .trigger = atmel_pdmic_cpu_dai_trigger, 4638c2ecf20Sopenharmony_ci}; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver atmel_pdmic_cpu_dai = { 4678c2ecf20Sopenharmony_ci .capture = { 4688c2ecf20Sopenharmony_ci .stream_name = "Capture", 4698c2ecf20Sopenharmony_ci .channels_min = 1, 4708c2ecf20Sopenharmony_ci .channels_max = 1, 4718c2ecf20Sopenharmony_ci .rates = SNDRV_PCM_RATE_KNOT, 4728c2ecf20Sopenharmony_ci .formats = ATMEL_PDMIC_FORMATS, 4738c2ecf20Sopenharmony_ci }, 4748c2ecf20Sopenharmony_ci .ops = &atmel_pdmic_cpu_dai_ops, 4758c2ecf20Sopenharmony_ci}; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver atmel_pdmic_cpu_dai_component = { 4788c2ecf20Sopenharmony_ci .name = "atmel-pdmic", 4798c2ecf20Sopenharmony_ci .probe = atmel_pdmic_component_probe, 4808c2ecf20Sopenharmony_ci .controls = atmel_pdmic_snd_controls, 4818c2ecf20Sopenharmony_ci .num_controls = ARRAY_SIZE(atmel_pdmic_snd_controls), 4828c2ecf20Sopenharmony_ci .idle_bias_on = 1, 4838c2ecf20Sopenharmony_ci .use_pmdown_time = 1, 4848c2ecf20Sopenharmony_ci}; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci/* ASoC sound card */ 4878c2ecf20Sopenharmony_cistatic int atmel_pdmic_asoc_card_init(struct device *dev, 4888c2ecf20Sopenharmony_ci struct snd_soc_card *card) 4898c2ecf20Sopenharmony_ci{ 4908c2ecf20Sopenharmony_ci struct snd_soc_dai_link *dai_link; 4918c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card); 4928c2ecf20Sopenharmony_ci struct snd_soc_dai_link_component *comp; 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL); 4958c2ecf20Sopenharmony_ci if (!dai_link) 4968c2ecf20Sopenharmony_ci return -ENOMEM; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci comp = devm_kzalloc(dev, 3 * sizeof(*comp), GFP_KERNEL); 4998c2ecf20Sopenharmony_ci if (!comp) 5008c2ecf20Sopenharmony_ci return -ENOMEM; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci dai_link->cpus = &comp[0]; 5038c2ecf20Sopenharmony_ci dai_link->codecs = &comp[1]; 5048c2ecf20Sopenharmony_ci dai_link->platforms = &comp[2]; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci dai_link->num_cpus = 1; 5078c2ecf20Sopenharmony_ci dai_link->num_codecs = 1; 5088c2ecf20Sopenharmony_ci dai_link->num_platforms = 1; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci dai_link->name = "PDMIC"; 5118c2ecf20Sopenharmony_ci dai_link->stream_name = "PDMIC PCM"; 5128c2ecf20Sopenharmony_ci dai_link->codecs->dai_name = "snd-soc-dummy-dai"; 5138c2ecf20Sopenharmony_ci dai_link->cpus->dai_name = dev_name(dev); 5148c2ecf20Sopenharmony_ci dai_link->codecs->name = "snd-soc-dummy"; 5158c2ecf20Sopenharmony_ci dai_link->platforms->name = dev_name(dev); 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci card->dai_link = dai_link; 5188c2ecf20Sopenharmony_ci card->num_links = 1; 5198c2ecf20Sopenharmony_ci card->name = dd->pdata->card_name; 5208c2ecf20Sopenharmony_ci card->dev = dev; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci return 0; 5238c2ecf20Sopenharmony_ci} 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cistatic void atmel_pdmic_get_sample_rate(struct atmel_pdmic *dd, 5268c2ecf20Sopenharmony_ci unsigned int *rate_min, unsigned int *rate_max) 5278c2ecf20Sopenharmony_ci{ 5288c2ecf20Sopenharmony_ci u32 mic_min_freq = dd->pdata->mic_min_freq; 5298c2ecf20Sopenharmony_ci u32 mic_max_freq = dd->pdata->mic_max_freq; 5308c2ecf20Sopenharmony_ci u32 clk_max_rate = (u32)(clk_get_rate(dd->pclk) >> 1); 5318c2ecf20Sopenharmony_ci u32 clk_min_rate = (u32)(clk_get_rate(dd->gclk) >> 8); 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci if (mic_max_freq > clk_max_rate) 5348c2ecf20Sopenharmony_ci mic_max_freq = clk_max_rate; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci if (mic_min_freq < clk_min_rate) 5378c2ecf20Sopenharmony_ci mic_min_freq = clk_min_rate; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci *rate_min = DIV_ROUND_CLOSEST(mic_min_freq, 128); 5408c2ecf20Sopenharmony_ci *rate_max = mic_max_freq >> 6; 5418c2ecf20Sopenharmony_ci} 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci/* PDMIC interrupt handler */ 5448c2ecf20Sopenharmony_cistatic irqreturn_t atmel_pdmic_interrupt(int irq, void *dev_id) 5458c2ecf20Sopenharmony_ci{ 5468c2ecf20Sopenharmony_ci struct atmel_pdmic *dd = (struct atmel_pdmic *)dev_id; 5478c2ecf20Sopenharmony_ci u32 pdmic_isr; 5488c2ecf20Sopenharmony_ci irqreturn_t ret = IRQ_NONE; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci regmap_read(dd->regmap, PDMIC_ISR, &pdmic_isr); 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci if (pdmic_isr & PDMIC_ISR_OVRE) { 5538c2ecf20Sopenharmony_ci regmap_update_bits(dd->regmap, PDMIC_CR, PDMIC_CR_ENPDM_MASK, 5548c2ecf20Sopenharmony_ci PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT); 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci snd_pcm_stop_xrun(dd->substream); 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci ret = IRQ_HANDLED; 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci return ret; 5628c2ecf20Sopenharmony_ci} 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci/* regmap configuration */ 5658c2ecf20Sopenharmony_ci#define ATMEL_PDMIC_REG_MAX 0x124 5668c2ecf20Sopenharmony_cistatic const struct regmap_config atmel_pdmic_regmap_config = { 5678c2ecf20Sopenharmony_ci .reg_bits = 32, 5688c2ecf20Sopenharmony_ci .reg_stride = 4, 5698c2ecf20Sopenharmony_ci .val_bits = 32, 5708c2ecf20Sopenharmony_ci .max_register = ATMEL_PDMIC_REG_MAX, 5718c2ecf20Sopenharmony_ci}; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_cistatic int atmel_pdmic_probe(struct platform_device *pdev) 5748c2ecf20Sopenharmony_ci{ 5758c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 5768c2ecf20Sopenharmony_ci struct atmel_pdmic *dd; 5778c2ecf20Sopenharmony_ci struct resource *res; 5788c2ecf20Sopenharmony_ci void __iomem *io_base; 5798c2ecf20Sopenharmony_ci const struct atmel_pdmic_pdata *pdata; 5808c2ecf20Sopenharmony_ci struct snd_soc_card *card; 5818c2ecf20Sopenharmony_ci unsigned int rate_min, rate_max; 5828c2ecf20Sopenharmony_ci int ret; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci pdata = atmel_pdmic_dt_init(dev); 5858c2ecf20Sopenharmony_ci if (IS_ERR(pdata)) 5868c2ecf20Sopenharmony_ci return PTR_ERR(pdata); 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL); 5898c2ecf20Sopenharmony_ci if (!dd) 5908c2ecf20Sopenharmony_ci return -ENOMEM; 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci dd->pdata = pdata; 5938c2ecf20Sopenharmony_ci dd->dev = dev; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci dd->irq = platform_get_irq(pdev, 0); 5968c2ecf20Sopenharmony_ci if (dd->irq < 0) 5978c2ecf20Sopenharmony_ci return dd->irq; 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci dd->pclk = devm_clk_get(dev, "pclk"); 6008c2ecf20Sopenharmony_ci if (IS_ERR(dd->pclk)) { 6018c2ecf20Sopenharmony_ci ret = PTR_ERR(dd->pclk); 6028c2ecf20Sopenharmony_ci dev_err(dev, "failed to get peripheral clock: %d\n", ret); 6038c2ecf20Sopenharmony_ci return ret; 6048c2ecf20Sopenharmony_ci } 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci dd->gclk = devm_clk_get(dev, "gclk"); 6078c2ecf20Sopenharmony_ci if (IS_ERR(dd->gclk)) { 6088c2ecf20Sopenharmony_ci ret = PTR_ERR(dd->gclk); 6098c2ecf20Sopenharmony_ci dev_err(dev, "failed to get GCK: %d\n", ret); 6108c2ecf20Sopenharmony_ci return ret; 6118c2ecf20Sopenharmony_ci } 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci /* The gclk clock frequency must always be three times 6148c2ecf20Sopenharmony_ci * lower than the pclk clock frequency 6158c2ecf20Sopenharmony_ci */ 6168c2ecf20Sopenharmony_ci ret = clk_set_rate(dd->gclk, clk_get_rate(dd->pclk)/3); 6178c2ecf20Sopenharmony_ci if (ret) { 6188c2ecf20Sopenharmony_ci dev_err(dev, "failed to set GCK clock rate: %d\n", ret); 6198c2ecf20Sopenharmony_ci return ret; 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6238c2ecf20Sopenharmony_ci io_base = devm_ioremap_resource(dev, res); 6248c2ecf20Sopenharmony_ci if (IS_ERR(io_base)) 6258c2ecf20Sopenharmony_ci return PTR_ERR(io_base); 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci dd->phy_base = res->start; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci dd->regmap = devm_regmap_init_mmio(dev, io_base, 6308c2ecf20Sopenharmony_ci &atmel_pdmic_regmap_config); 6318c2ecf20Sopenharmony_ci if (IS_ERR(dd->regmap)) { 6328c2ecf20Sopenharmony_ci ret = PTR_ERR(dd->regmap); 6338c2ecf20Sopenharmony_ci dev_err(dev, "failed to init register map: %d\n", ret); 6348c2ecf20Sopenharmony_ci return ret; 6358c2ecf20Sopenharmony_ci } 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci ret = devm_request_irq(dev, dd->irq, atmel_pdmic_interrupt, 0, 6388c2ecf20Sopenharmony_ci "PDMIC", (void *)dd); 6398c2ecf20Sopenharmony_ci if (ret < 0) { 6408c2ecf20Sopenharmony_ci dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n", 6418c2ecf20Sopenharmony_ci dd->irq, ret); 6428c2ecf20Sopenharmony_ci return ret; 6438c2ecf20Sopenharmony_ci } 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci /* Get the minimal and maximal sample rate that the microphone supports */ 6468c2ecf20Sopenharmony_ci atmel_pdmic_get_sample_rate(dd, &rate_min, &rate_max); 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci /* register cpu dai */ 6498c2ecf20Sopenharmony_ci atmel_pdmic_cpu_dai.capture.rate_min = rate_min; 6508c2ecf20Sopenharmony_ci atmel_pdmic_cpu_dai.capture.rate_max = rate_max; 6518c2ecf20Sopenharmony_ci ret = devm_snd_soc_register_component(dev, 6528c2ecf20Sopenharmony_ci &atmel_pdmic_cpu_dai_component, 6538c2ecf20Sopenharmony_ci &atmel_pdmic_cpu_dai, 1); 6548c2ecf20Sopenharmony_ci if (ret) { 6558c2ecf20Sopenharmony_ci dev_err(dev, "could not register CPU DAI: %d\n", ret); 6568c2ecf20Sopenharmony_ci return ret; 6578c2ecf20Sopenharmony_ci } 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci /* register platform */ 6608c2ecf20Sopenharmony_ci ret = devm_snd_dmaengine_pcm_register(dev, 6618c2ecf20Sopenharmony_ci &atmel_pdmic_dmaengine_pcm_config, 6628c2ecf20Sopenharmony_ci 0); 6638c2ecf20Sopenharmony_ci if (ret) { 6648c2ecf20Sopenharmony_ci dev_err(dev, "could not register platform: %d\n", ret); 6658c2ecf20Sopenharmony_ci return ret; 6668c2ecf20Sopenharmony_ci } 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci /* register sound card */ 6698c2ecf20Sopenharmony_ci card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 6708c2ecf20Sopenharmony_ci if (!card) { 6718c2ecf20Sopenharmony_ci ret = -ENOMEM; 6728c2ecf20Sopenharmony_ci goto unregister_codec; 6738c2ecf20Sopenharmony_ci } 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci snd_soc_card_set_drvdata(card, dd); 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci ret = atmel_pdmic_asoc_card_init(dev, card); 6788c2ecf20Sopenharmony_ci if (ret) { 6798c2ecf20Sopenharmony_ci dev_err(dev, "failed to init sound card: %d\n", ret); 6808c2ecf20Sopenharmony_ci goto unregister_codec; 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci ret = devm_snd_soc_register_card(dev, card); 6848c2ecf20Sopenharmony_ci if (ret) { 6858c2ecf20Sopenharmony_ci dev_err(dev, "failed to register sound card: %d\n", ret); 6868c2ecf20Sopenharmony_ci goto unregister_codec; 6878c2ecf20Sopenharmony_ci } 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci return 0; 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ciunregister_codec: 6928c2ecf20Sopenharmony_ci return ret; 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_cistatic int atmel_pdmic_remove(struct platform_device *pdev) 6968c2ecf20Sopenharmony_ci{ 6978c2ecf20Sopenharmony_ci return 0; 6988c2ecf20Sopenharmony_ci} 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_cistatic struct platform_driver atmel_pdmic_driver = { 7018c2ecf20Sopenharmony_ci .driver = { 7028c2ecf20Sopenharmony_ci .name = "atmel-pdmic", 7038c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(atmel_pdmic_of_match), 7048c2ecf20Sopenharmony_ci .pm = &snd_soc_pm_ops, 7058c2ecf20Sopenharmony_ci }, 7068c2ecf20Sopenharmony_ci .probe = atmel_pdmic_probe, 7078c2ecf20Sopenharmony_ci .remove = atmel_pdmic_remove, 7088c2ecf20Sopenharmony_ci}; 7098c2ecf20Sopenharmony_cimodule_platform_driver(atmel_pdmic_driver); 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Atmel PDMIC driver under ALSA SoC architecture"); 7128c2ecf20Sopenharmony_ciMODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>"); 7138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 714