0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/of.h>
0010 #include <linux/clk.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regmap.h>
0014 #include <sound/core.h>
0015 #include <sound/dmaengine_pcm.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/tlv.h>
0018 #include "atmel-pdmic.h"
0019
0020 struct atmel_pdmic_pdata {
0021 u32 mic_min_freq;
0022 u32 mic_max_freq;
0023 s32 mic_offset;
0024 const char *card_name;
0025 };
0026
0027 struct atmel_pdmic {
0028 dma_addr_t phy_base;
0029 struct regmap *regmap;
0030 struct clk *pclk;
0031 struct clk *gclk;
0032 struct device *dev;
0033 int irq;
0034 struct snd_pcm_substream *substream;
0035 const struct atmel_pdmic_pdata *pdata;
0036 };
0037
0038 static const struct of_device_id atmel_pdmic_of_match[] = {
0039 {
0040 .compatible = "atmel,sama5d2-pdmic",
0041 }, {
0042
0043 }
0044 };
0045 MODULE_DEVICE_TABLE(of, atmel_pdmic_of_match);
0046
0047 #define PDMIC_OFFSET_MAX_VAL S16_MAX
0048 #define PDMIC_OFFSET_MIN_VAL S16_MIN
0049
0050 static struct atmel_pdmic_pdata *atmel_pdmic_dt_init(struct device *dev)
0051 {
0052 struct device_node *np = dev->of_node;
0053 struct atmel_pdmic_pdata *pdata;
0054
0055 if (!np) {
0056 dev_err(dev, "device node not found\n");
0057 return ERR_PTR(-EINVAL);
0058 }
0059
0060 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0061 if (!pdata)
0062 return ERR_PTR(-ENOMEM);
0063
0064 if (of_property_read_string(np, "atmel,model", &pdata->card_name))
0065 pdata->card_name = "PDMIC";
0066
0067 if (of_property_read_u32(np, "atmel,mic-min-freq",
0068 &pdata->mic_min_freq)) {
0069 dev_err(dev, "failed to get mic-min-freq\n");
0070 return ERR_PTR(-EINVAL);
0071 }
0072
0073 if (of_property_read_u32(np, "atmel,mic-max-freq",
0074 &pdata->mic_max_freq)) {
0075 dev_err(dev, "failed to get mic-max-freq\n");
0076 return ERR_PTR(-EINVAL);
0077 }
0078
0079 if (pdata->mic_max_freq < pdata->mic_min_freq) {
0080 dev_err(dev,
0081 "mic-max-freq should not be less than mic-min-freq\n");
0082 return ERR_PTR(-EINVAL);
0083 }
0084
0085 if (of_property_read_s32(np, "atmel,mic-offset", &pdata->mic_offset))
0086 pdata->mic_offset = 0;
0087
0088 if (pdata->mic_offset > PDMIC_OFFSET_MAX_VAL) {
0089 dev_warn(dev,
0090 "mic-offset value %d is larger than the max value %d, the max value is specified\n",
0091 pdata->mic_offset, PDMIC_OFFSET_MAX_VAL);
0092 pdata->mic_offset = PDMIC_OFFSET_MAX_VAL;
0093 } else if (pdata->mic_offset < PDMIC_OFFSET_MIN_VAL) {
0094 dev_warn(dev,
0095 "mic-offset value %d is less than the min value %d, the min value is specified\n",
0096 pdata->mic_offset, PDMIC_OFFSET_MIN_VAL);
0097 pdata->mic_offset = PDMIC_OFFSET_MIN_VAL;
0098 }
0099
0100 return pdata;
0101 }
0102
0103
0104 static int atmel_pdmic_cpu_dai_startup(struct snd_pcm_substream *substream,
0105 struct snd_soc_dai *cpu_dai)
0106 {
0107 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0108 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
0109 int ret;
0110
0111 ret = clk_prepare_enable(dd->gclk);
0112 if (ret)
0113 return ret;
0114
0115 ret = clk_prepare_enable(dd->pclk);
0116 if (ret) {
0117 clk_disable_unprepare(dd->gclk);
0118 return ret;
0119 }
0120
0121
0122 regmap_write(dd->regmap, PDMIC_CR, 0);
0123
0124 dd->substream = substream;
0125
0126
0127 regmap_write(dd->regmap, PDMIC_IER, PDMIC_IER_OVRE);
0128
0129 return 0;
0130 }
0131
0132 static void atmel_pdmic_cpu_dai_shutdown(struct snd_pcm_substream *substream,
0133 struct snd_soc_dai *cpu_dai)
0134 {
0135 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0136 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
0137
0138
0139 regmap_write(dd->regmap, PDMIC_IDR, PDMIC_IDR_OVRE);
0140
0141 clk_disable_unprepare(dd->gclk);
0142 clk_disable_unprepare(dd->pclk);
0143 }
0144
0145 static int atmel_pdmic_cpu_dai_prepare(struct snd_pcm_substream *substream,
0146 struct snd_soc_dai *cpu_dai)
0147 {
0148 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0149 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
0150 struct snd_soc_component *component = cpu_dai->component;
0151 u32 val;
0152 int ret;
0153
0154
0155 ret = regmap_read(dd->regmap, PDMIC_CDR, &val);
0156 if (ret < 0)
0157 return 0;
0158
0159 ret = snd_soc_component_update_bits(component, PDMIC_CR,
0160 PDMIC_CR_ENPDM_MASK,
0161 PDMIC_CR_ENPDM_DIS <<
0162 PDMIC_CR_ENPDM_SHIFT);
0163 if (ret < 0)
0164 return ret;
0165
0166 return 0;
0167 }
0168
0169 #define ATMEL_PDMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
0170
0171
0172 #define ATMEL_PDMIC_MAX_BUF_SIZE (64 * 1024)
0173 #define ATMEL_PDMIC_PREALLOC_BUF_SIZE ATMEL_PDMIC_MAX_BUF_SIZE
0174
0175 static const struct snd_pcm_hardware atmel_pdmic_hw = {
0176 .info = SNDRV_PCM_INFO_MMAP
0177 | SNDRV_PCM_INFO_MMAP_VALID
0178 | SNDRV_PCM_INFO_INTERLEAVED
0179 | SNDRV_PCM_INFO_RESUME
0180 | SNDRV_PCM_INFO_PAUSE,
0181 .formats = ATMEL_PDMIC_FORMATS,
0182 .buffer_bytes_max = ATMEL_PDMIC_MAX_BUF_SIZE,
0183 .period_bytes_min = 256,
0184 .period_bytes_max = 32 * 1024,
0185 .periods_min = 2,
0186 .periods_max = 256,
0187 };
0188
0189 static int
0190 atmel_pdmic_platform_configure_dma(struct snd_pcm_substream *substream,
0191 struct snd_pcm_hw_params *params,
0192 struct dma_slave_config *slave_config)
0193 {
0194 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0195 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
0196 int ret;
0197
0198 ret = snd_hwparams_to_dma_slave_config(substream, params,
0199 slave_config);
0200 if (ret) {
0201 dev_err(dd->dev,
0202 "hw params to dma slave configure failed\n");
0203 return ret;
0204 }
0205
0206 slave_config->src_addr = dd->phy_base + PDMIC_CDR;
0207 slave_config->src_maxburst = 1;
0208 slave_config->dst_maxburst = 1;
0209
0210 return 0;
0211 }
0212
0213 static const struct snd_dmaengine_pcm_config
0214 atmel_pdmic_dmaengine_pcm_config = {
0215 .prepare_slave_config = atmel_pdmic_platform_configure_dma,
0216 .pcm_hardware = &atmel_pdmic_hw,
0217 .prealloc_buffer_size = ATMEL_PDMIC_PREALLOC_BUF_SIZE,
0218 };
0219
0220
0221
0222 struct mic_gain {
0223 unsigned int dgain;
0224 unsigned int scale;
0225 };
0226
0227
0228 static const struct mic_gain mic_gain_table[] = {
0229 { 1, 15}, { 1, 14},
0230 { 3, 15}, { 1, 13}, { 3, 14}, { 1, 12},
0231 { 5, 14}, { 13, 15},
0232 { 9, 14}, { 21, 15}, { 23, 15}, { 13, 14},
0233 { 29, 15}, { 33, 15}, { 37, 15}, { 41, 15},
0234 { 23, 14}, { 13, 13}, { 58, 15}, { 65, 15},
0235 { 73, 15}, { 41, 14}, { 23, 13}, { 13, 12},
0236 { 29, 13}, { 65, 14}, { 73, 14}, { 41, 13},
0237 { 23, 12}, { 207, 15}, { 29, 12}, { 65, 13},
0238 { 73, 13}, { 41, 12}, { 23, 11}, { 413, 15},
0239 { 463, 15}, { 519, 15}, { 583, 15}, { 327, 14},
0240 { 367, 14}, { 823, 15}, { 231, 13}, { 1036, 15},
0241 { 1163, 15}, { 1305, 15}, { 183, 12}, { 1642, 15},
0242 { 1843, 15}, { 2068, 15}, { 145, 11}, { 2603, 15},
0243 { 365, 12}, { 3277, 15}, { 3677, 15}, { 4125, 15},
0244 { 4629, 15}, { 5193, 15}, { 5827, 15}, { 3269, 14},
0245 { 917, 12}, { 8231, 15}, { 9235, 15}, { 5181, 14},
0246 {11627, 15}, {13045, 15}, {14637, 15}, {16423, 15},
0247 {18427, 15}, {20675, 15}, { 5799, 13}, {26029, 15},
0248 { 7301, 13}, { 1, 0}, {18383, 14}, {10313, 13},
0249 {23143, 14}, {25967, 14}, {29135, 14}, {16345, 13},
0250 { 4585, 11}, {20577, 13}, { 1443, 9}, {25905, 13},
0251 {14533, 12}, { 8153, 11}, { 2287, 9}, {20529, 12},
0252 {11517, 11}, { 6461, 10}, {28997, 12}, { 4067, 9},
0253 {18253, 11}, { 10, 0}, {22979, 11}, {25783, 11},
0254 {28929, 11}, {32459, 11}, { 9105, 9}, {20431, 10},
0255 {22925, 10}, {12861, 9}, { 7215, 8}, {16191, 9},
0256 { 9083, 8}, {20383, 9}, {11435, 8}, { 6145, 7},
0257 { 3599, 6}, {32305, 9}, {18123, 8}, {20335, 8},
0258 { 713, 3}, { 100, 0}, { 7181, 6}, { 8057, 6},
0259 { 565, 2}, {20287, 7}, {11381, 6}, {25539, 7},
0260 { 1791, 3}, { 4019, 4}, { 9019, 5}, {20239, 6},
0261 { 5677, 4}, {25479, 6}, { 7147, 4}, { 8019, 4},
0262 {17995, 5}, {20191, 5}, {11327, 4}, {12709, 4},
0263 { 3565, 2}, { 1000, 0}, { 1122, 0}, { 1259, 0},
0264 { 2825, 1}, {12679, 3}, { 7113, 2}, { 7981, 2},
0265 { 8955, 2}, {20095, 3}, {22547, 3}, {12649, 2},
0266 {28385, 3}, { 3981, 0}, {17867, 2}, {20047, 2},
0267 {11247, 1}, {12619, 1}, {14159, 1}, {31773, 2},
0268 {17825, 1}, {10000, 0}, {11220, 0}, {12589, 0},
0269 {28251, 1}, {15849, 0}, {17783, 0}, {19953, 0},
0270 {22387, 0}, {25119, 0}, {28184, 0}, {31623, 0},
0271 };
0272
0273 static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
0274 0, 1, TLV_DB_SCALE_ITEM(-9000, 600, 0),
0275 2, 5, TLV_DB_SCALE_ITEM(-8100, 300, 0),
0276 6, 7, TLV_DB_SCALE_ITEM(-7000, 200, 0),
0277 8, ARRAY_SIZE(mic_gain_table)-1, TLV_DB_SCALE_ITEM(-6500, 100, 0),
0278 );
0279
0280 static int pdmic_get_mic_volsw(struct snd_kcontrol *kcontrol,
0281 struct snd_ctl_elem_value *ucontrol)
0282 {
0283 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0284 unsigned int dgain_val, scale_val;
0285 int i;
0286
0287 dgain_val = (snd_soc_component_read(component, PDMIC_DSPR1) & PDMIC_DSPR1_DGAIN_MASK)
0288 >> PDMIC_DSPR1_DGAIN_SHIFT;
0289
0290 scale_val = (snd_soc_component_read(component, PDMIC_DSPR0) & PDMIC_DSPR0_SCALE_MASK)
0291 >> PDMIC_DSPR0_SCALE_SHIFT;
0292
0293 for (i = 0; i < ARRAY_SIZE(mic_gain_table); i++) {
0294 if ((mic_gain_table[i].dgain == dgain_val) &&
0295 (mic_gain_table[i].scale == scale_val))
0296 ucontrol->value.integer.value[0] = i;
0297 }
0298
0299 return 0;
0300 }
0301
0302 static int pdmic_put_mic_volsw(struct snd_kcontrol *kcontrol,
0303 struct snd_ctl_elem_value *ucontrol)
0304 {
0305 struct soc_mixer_control *mc =
0306 (struct soc_mixer_control *)kcontrol->private_value;
0307 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0308 int max = mc->max;
0309 unsigned int val;
0310 int ret;
0311
0312 val = ucontrol->value.integer.value[0];
0313
0314 if (val > max)
0315 return -EINVAL;
0316
0317 ret = snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_MASK,
0318 mic_gain_table[val].dgain << PDMIC_DSPR1_DGAIN_SHIFT);
0319 if (ret < 0)
0320 return ret;
0321
0322 ret = snd_soc_component_update_bits(component, PDMIC_DSPR0, PDMIC_DSPR0_SCALE_MASK,
0323 mic_gain_table[val].scale << PDMIC_DSPR0_SCALE_SHIFT);
0324 if (ret < 0)
0325 return ret;
0326
0327 return 0;
0328 }
0329
0330 static const struct snd_kcontrol_new atmel_pdmic_snd_controls[] = {
0331 SOC_SINGLE_EXT_TLV("Mic Capture Volume", PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_SHIFT,
0332 ARRAY_SIZE(mic_gain_table)-1, 0,
0333 pdmic_get_mic_volsw, pdmic_put_mic_volsw, mic_gain_tlv),
0334
0335 SOC_SINGLE("High Pass Filter Switch", PDMIC_DSPR0,
0336 PDMIC_DSPR0_HPFBYP_SHIFT, 1, 1),
0337
0338 SOC_SINGLE("SINCC Filter Switch", PDMIC_DSPR0, PDMIC_DSPR0_SINBYP_SHIFT, 1, 1),
0339 };
0340
0341 static int atmel_pdmic_component_probe(struct snd_soc_component *component)
0342 {
0343 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
0344 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
0345
0346 snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_OFFSET_MASK,
0347 (u32)(dd->pdata->mic_offset << PDMIC_DSPR1_OFFSET_SHIFT));
0348
0349 return 0;
0350 }
0351
0352 #define PDMIC_MR_PRESCAL_MAX_VAL 127
0353
0354 static int
0355 atmel_pdmic_cpu_dai_hw_params(struct snd_pcm_substream *substream,
0356 struct snd_pcm_hw_params *params,
0357 struct snd_soc_dai *cpu_dai)
0358 {
0359 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0360 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
0361 struct snd_soc_component *component = cpu_dai->component;
0362 unsigned int rate_min = substream->runtime->hw.rate_min;
0363 unsigned int rate_max = substream->runtime->hw.rate_max;
0364 int fs = params_rate(params);
0365 int bits = params_width(params);
0366 unsigned long pclk_rate, gclk_rate;
0367 unsigned int f_pdmic;
0368 u32 mr_val, dspr0_val, pclk_prescal, gclk_prescal;
0369
0370 if (params_channels(params) != 1) {
0371 dev_err(component->dev,
0372 "only supports one channel\n");
0373 return -EINVAL;
0374 }
0375
0376 if ((fs < rate_min) || (fs > rate_max)) {
0377 dev_err(component->dev,
0378 "sample rate is %dHz, min rate is %dHz, max rate is %dHz\n",
0379 fs, rate_min, rate_max);
0380
0381 return -EINVAL;
0382 }
0383
0384 switch (bits) {
0385 case 16:
0386 dspr0_val = (PDMIC_DSPR0_SIZE_16_BITS
0387 << PDMIC_DSPR0_SIZE_SHIFT);
0388 break;
0389 case 32:
0390 dspr0_val = (PDMIC_DSPR0_SIZE_32_BITS
0391 << PDMIC_DSPR0_SIZE_SHIFT);
0392 break;
0393 default:
0394 return -EINVAL;
0395 }
0396
0397 if ((fs << 7) > (rate_max << 6)) {
0398 f_pdmic = fs << 6;
0399 dspr0_val |= PDMIC_DSPR0_OSR_64 << PDMIC_DSPR0_OSR_SHIFT;
0400 } else {
0401 f_pdmic = fs << 7;
0402 dspr0_val |= PDMIC_DSPR0_OSR_128 << PDMIC_DSPR0_OSR_SHIFT;
0403 }
0404
0405 pclk_rate = clk_get_rate(dd->pclk);
0406 gclk_rate = clk_get_rate(dd->gclk);
0407
0408
0409 pclk_prescal = (u32)(pclk_rate/(f_pdmic << 1)) - 1;
0410 gclk_prescal = (u32)(gclk_rate/(f_pdmic << 1)) - 1;
0411
0412 if ((pclk_prescal > PDMIC_MR_PRESCAL_MAX_VAL) ||
0413 (gclk_rate/((gclk_prescal + 1) << 1) <
0414 pclk_rate/((pclk_prescal + 1) << 1))) {
0415 mr_val = gclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
0416 mr_val |= PDMIC_MR_CLKS_GCK << PDMIC_MR_CLKS_SHIFT;
0417 } else {
0418 mr_val = pclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
0419 mr_val |= PDMIC_MR_CLKS_PCK << PDMIC_MR_CLKS_SHIFT;
0420 }
0421
0422 snd_soc_component_update_bits(component, PDMIC_MR,
0423 PDMIC_MR_PRESCAL_MASK | PDMIC_MR_CLKS_MASK, mr_val);
0424
0425 snd_soc_component_update_bits(component, PDMIC_DSPR0,
0426 PDMIC_DSPR0_OSR_MASK | PDMIC_DSPR0_SIZE_MASK, dspr0_val);
0427
0428 return 0;
0429 }
0430
0431 static int atmel_pdmic_cpu_dai_trigger(struct snd_pcm_substream *substream,
0432 int cmd, struct snd_soc_dai *cpu_dai)
0433 {
0434 struct snd_soc_component *component = cpu_dai->component;
0435 u32 val;
0436
0437 switch (cmd) {
0438 case SNDRV_PCM_TRIGGER_START:
0439 case SNDRV_PCM_TRIGGER_RESUME:
0440 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0441 val = PDMIC_CR_ENPDM_EN << PDMIC_CR_ENPDM_SHIFT;
0442 break;
0443 case SNDRV_PCM_TRIGGER_STOP:
0444 case SNDRV_PCM_TRIGGER_SUSPEND:
0445 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0446 val = PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT;
0447 break;
0448 default:
0449 return -EINVAL;
0450 }
0451
0452 snd_soc_component_update_bits(component, PDMIC_CR, PDMIC_CR_ENPDM_MASK, val);
0453
0454 return 0;
0455 }
0456
0457 static const struct snd_soc_dai_ops atmel_pdmic_cpu_dai_ops = {
0458 .startup = atmel_pdmic_cpu_dai_startup,
0459 .shutdown = atmel_pdmic_cpu_dai_shutdown,
0460 .prepare = atmel_pdmic_cpu_dai_prepare,
0461 .hw_params = atmel_pdmic_cpu_dai_hw_params,
0462 .trigger = atmel_pdmic_cpu_dai_trigger,
0463 };
0464
0465
0466 static struct snd_soc_dai_driver atmel_pdmic_cpu_dai = {
0467 .capture = {
0468 .stream_name = "Capture",
0469 .channels_min = 1,
0470 .channels_max = 1,
0471 .rates = SNDRV_PCM_RATE_KNOT,
0472 .formats = ATMEL_PDMIC_FORMATS,
0473 },
0474 .ops = &atmel_pdmic_cpu_dai_ops,
0475 };
0476
0477 static const struct snd_soc_component_driver atmel_pdmic_cpu_dai_component = {
0478 .name = "atmel-pdmic",
0479 .probe = atmel_pdmic_component_probe,
0480 .controls = atmel_pdmic_snd_controls,
0481 .num_controls = ARRAY_SIZE(atmel_pdmic_snd_controls),
0482 .idle_bias_on = 1,
0483 .use_pmdown_time = 1,
0484 .legacy_dai_naming = 1,
0485 };
0486
0487
0488 static int atmel_pdmic_asoc_card_init(struct device *dev,
0489 struct snd_soc_card *card)
0490 {
0491 struct snd_soc_dai_link *dai_link;
0492 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
0493 struct snd_soc_dai_link_component *comp;
0494
0495 dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
0496 if (!dai_link)
0497 return -ENOMEM;
0498
0499 comp = devm_kzalloc(dev, 3 * sizeof(*comp), GFP_KERNEL);
0500 if (!comp)
0501 return -ENOMEM;
0502
0503 dai_link->cpus = &comp[0];
0504 dai_link->codecs = &comp[1];
0505 dai_link->platforms = &comp[2];
0506
0507 dai_link->num_cpus = 1;
0508 dai_link->num_codecs = 1;
0509 dai_link->num_platforms = 1;
0510
0511 dai_link->name = "PDMIC";
0512 dai_link->stream_name = "PDMIC PCM";
0513 dai_link->codecs->dai_name = "snd-soc-dummy-dai";
0514 dai_link->cpus->dai_name = dev_name(dev);
0515 dai_link->codecs->name = "snd-soc-dummy";
0516 dai_link->platforms->name = dev_name(dev);
0517
0518 card->dai_link = dai_link;
0519 card->num_links = 1;
0520 card->name = dd->pdata->card_name;
0521 card->dev = dev;
0522
0523 return 0;
0524 }
0525
0526 static void atmel_pdmic_get_sample_rate(struct atmel_pdmic *dd,
0527 unsigned int *rate_min, unsigned int *rate_max)
0528 {
0529 u32 mic_min_freq = dd->pdata->mic_min_freq;
0530 u32 mic_max_freq = dd->pdata->mic_max_freq;
0531 u32 clk_max_rate = (u32)(clk_get_rate(dd->pclk) >> 1);
0532 u32 clk_min_rate = (u32)(clk_get_rate(dd->gclk) >> 8);
0533
0534 if (mic_max_freq > clk_max_rate)
0535 mic_max_freq = clk_max_rate;
0536
0537 if (mic_min_freq < clk_min_rate)
0538 mic_min_freq = clk_min_rate;
0539
0540 *rate_min = DIV_ROUND_CLOSEST(mic_min_freq, 128);
0541 *rate_max = mic_max_freq >> 6;
0542 }
0543
0544
0545 static irqreturn_t atmel_pdmic_interrupt(int irq, void *dev_id)
0546 {
0547 struct atmel_pdmic *dd = (struct atmel_pdmic *)dev_id;
0548 u32 pdmic_isr;
0549 irqreturn_t ret = IRQ_NONE;
0550
0551 regmap_read(dd->regmap, PDMIC_ISR, &pdmic_isr);
0552
0553 if (pdmic_isr & PDMIC_ISR_OVRE) {
0554 regmap_update_bits(dd->regmap, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
0555 PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
0556
0557 snd_pcm_stop_xrun(dd->substream);
0558
0559 ret = IRQ_HANDLED;
0560 }
0561
0562 return ret;
0563 }
0564
0565
0566 #define ATMEL_PDMIC_REG_MAX 0x124
0567 static const struct regmap_config atmel_pdmic_regmap_config = {
0568 .reg_bits = 32,
0569 .reg_stride = 4,
0570 .val_bits = 32,
0571 .max_register = ATMEL_PDMIC_REG_MAX,
0572 };
0573
0574 static int atmel_pdmic_probe(struct platform_device *pdev)
0575 {
0576 struct device *dev = &pdev->dev;
0577 struct atmel_pdmic *dd;
0578 struct resource *res;
0579 void __iomem *io_base;
0580 const struct atmel_pdmic_pdata *pdata;
0581 struct snd_soc_card *card;
0582 unsigned int rate_min, rate_max;
0583 int ret;
0584
0585 pdata = atmel_pdmic_dt_init(dev);
0586 if (IS_ERR(pdata))
0587 return PTR_ERR(pdata);
0588
0589 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
0590 if (!dd)
0591 return -ENOMEM;
0592
0593 dd->pdata = pdata;
0594 dd->dev = dev;
0595
0596 dd->irq = platform_get_irq(pdev, 0);
0597 if (dd->irq < 0)
0598 return dd->irq;
0599
0600 dd->pclk = devm_clk_get(dev, "pclk");
0601 if (IS_ERR(dd->pclk)) {
0602 ret = PTR_ERR(dd->pclk);
0603 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
0604 return ret;
0605 }
0606
0607 dd->gclk = devm_clk_get(dev, "gclk");
0608 if (IS_ERR(dd->gclk)) {
0609 ret = PTR_ERR(dd->gclk);
0610 dev_err(dev, "failed to get GCK: %d\n", ret);
0611 return ret;
0612 }
0613
0614
0615
0616
0617 ret = clk_set_rate(dd->gclk, clk_get_rate(dd->pclk)/3);
0618 if (ret) {
0619 dev_err(dev, "failed to set GCK clock rate: %d\n", ret);
0620 return ret;
0621 }
0622
0623 io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
0624 if (IS_ERR(io_base))
0625 return PTR_ERR(io_base);
0626
0627 dd->phy_base = res->start;
0628
0629 dd->regmap = devm_regmap_init_mmio(dev, io_base,
0630 &atmel_pdmic_regmap_config);
0631 if (IS_ERR(dd->regmap)) {
0632 ret = PTR_ERR(dd->regmap);
0633 dev_err(dev, "failed to init register map: %d\n", ret);
0634 return ret;
0635 }
0636
0637 ret = devm_request_irq(dev, dd->irq, atmel_pdmic_interrupt, 0,
0638 "PDMIC", (void *)dd);
0639 if (ret < 0) {
0640 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
0641 dd->irq, ret);
0642 return ret;
0643 }
0644
0645
0646 atmel_pdmic_get_sample_rate(dd, &rate_min, &rate_max);
0647
0648
0649 atmel_pdmic_cpu_dai.capture.rate_min = rate_min;
0650 atmel_pdmic_cpu_dai.capture.rate_max = rate_max;
0651 ret = devm_snd_soc_register_component(dev,
0652 &atmel_pdmic_cpu_dai_component,
0653 &atmel_pdmic_cpu_dai, 1);
0654 if (ret) {
0655 dev_err(dev, "could not register CPU DAI: %d\n", ret);
0656 return ret;
0657 }
0658
0659
0660 ret = devm_snd_dmaengine_pcm_register(dev,
0661 &atmel_pdmic_dmaengine_pcm_config,
0662 0);
0663 if (ret) {
0664 dev_err(dev, "could not register platform: %d\n", ret);
0665 return ret;
0666 }
0667
0668
0669 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
0670 if (!card) {
0671 ret = -ENOMEM;
0672 goto unregister_codec;
0673 }
0674
0675 snd_soc_card_set_drvdata(card, dd);
0676
0677 ret = atmel_pdmic_asoc_card_init(dev, card);
0678 if (ret) {
0679 dev_err(dev, "failed to init sound card: %d\n", ret);
0680 goto unregister_codec;
0681 }
0682
0683 ret = devm_snd_soc_register_card(dev, card);
0684 if (ret) {
0685 dev_err(dev, "failed to register sound card: %d\n", ret);
0686 goto unregister_codec;
0687 }
0688
0689 return 0;
0690
0691 unregister_codec:
0692 return ret;
0693 }
0694
0695 static int atmel_pdmic_remove(struct platform_device *pdev)
0696 {
0697 return 0;
0698 }
0699
0700 static struct platform_driver atmel_pdmic_driver = {
0701 .driver = {
0702 .name = "atmel-pdmic",
0703 .of_match_table = of_match_ptr(atmel_pdmic_of_match),
0704 .pm = &snd_soc_pm_ops,
0705 },
0706 .probe = atmel_pdmic_probe,
0707 .remove = atmel_pdmic_remove,
0708 };
0709 module_platform_driver(atmel_pdmic_driver);
0710
0711 MODULE_DESCRIPTION("Atmel PDMIC driver under ALSA SoC architecture");
0712 MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
0713 MODULE_LICENSE("GPL v2");