mm: drop migrate type checks from has_unmovable_pages
[linux/fpc-iii.git] / sound / soc / atmel / atmel-pdmic.c
blob8e3d34be9e690878be8c084ed9dfb1ff79a7b087
1 /* Atmel PDMIC driver
3 * Copyright (C) 2015 Atmel
5 * Author: Songjun Wu <songjun.wu@atmel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or later
9 * as published by the Free Software Foundation.
12 #include <linux/of.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <sound/core.h>
18 #include <sound/dmaengine_pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/tlv.h>
21 #include "atmel-pdmic.h"
23 struct atmel_pdmic_pdata {
24 u32 mic_min_freq;
25 u32 mic_max_freq;
26 s32 mic_offset;
27 const char *card_name;
30 struct atmel_pdmic {
31 dma_addr_t phy_base;
32 struct regmap *regmap;
33 struct clk *pclk;
34 struct clk *gclk;
35 int irq;
36 struct snd_pcm_substream *substream;
37 const struct atmel_pdmic_pdata *pdata;
40 static const struct of_device_id atmel_pdmic_of_match[] = {
42 .compatible = "atmel,sama5d2-pdmic",
43 }, {
44 /* sentinel */
47 MODULE_DEVICE_TABLE(of, atmel_pdmic_of_match);
49 #define PDMIC_OFFSET_MAX_VAL S16_MAX
50 #define PDMIC_OFFSET_MIN_VAL S16_MIN
52 static struct atmel_pdmic_pdata *atmel_pdmic_dt_init(struct device *dev)
54 struct device_node *np = dev->of_node;
55 struct atmel_pdmic_pdata *pdata;
57 if (!np) {
58 dev_err(dev, "device node not found\n");
59 return ERR_PTR(-EINVAL);
62 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
63 if (!pdata)
64 return ERR_PTR(-ENOMEM);
66 if (of_property_read_string(np, "atmel,model", &pdata->card_name))
67 pdata->card_name = "PDMIC";
69 if (of_property_read_u32(np, "atmel,mic-min-freq",
70 &pdata->mic_min_freq)) {
71 dev_err(dev, "failed to get mic-min-freq\n");
72 return ERR_PTR(-EINVAL);
75 if (of_property_read_u32(np, "atmel,mic-max-freq",
76 &pdata->mic_max_freq)) {
77 dev_err(dev, "failed to get mic-max-freq\n");
78 return ERR_PTR(-EINVAL);
81 if (pdata->mic_max_freq < pdata->mic_min_freq) {
82 dev_err(dev,
83 "mic-max-freq should not be less than mic-min-freq\n");
84 return ERR_PTR(-EINVAL);
87 if (of_property_read_s32(np, "atmel,mic-offset", &pdata->mic_offset))
88 pdata->mic_offset = 0;
90 if (pdata->mic_offset > PDMIC_OFFSET_MAX_VAL) {
91 dev_warn(dev,
92 "mic-offset value %d is larger than the max value %d, the max value is specified\n",
93 pdata->mic_offset, PDMIC_OFFSET_MAX_VAL);
94 pdata->mic_offset = PDMIC_OFFSET_MAX_VAL;
95 } else if (pdata->mic_offset < PDMIC_OFFSET_MIN_VAL) {
96 dev_warn(dev,
97 "mic-offset value %d is less than the min value %d, the min value is specified\n",
98 pdata->mic_offset, PDMIC_OFFSET_MIN_VAL);
99 pdata->mic_offset = PDMIC_OFFSET_MIN_VAL;
102 return pdata;
105 /* cpu dai component */
106 static int atmel_pdmic_cpu_dai_startup(struct snd_pcm_substream *substream,
107 struct snd_soc_dai *cpu_dai)
109 struct snd_soc_pcm_runtime *rtd = substream->private_data;
110 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
111 int ret;
113 ret = clk_prepare_enable(dd->gclk);
114 if (ret)
115 return ret;
117 ret = clk_prepare_enable(dd->pclk);
118 if (ret) {
119 clk_disable_unprepare(dd->gclk);
120 return ret;
123 /* Clear all bits in the Control Register(PDMIC_CR) */
124 regmap_write(dd->regmap, PDMIC_CR, 0);
126 dd->substream = substream;
128 /* Enable the overrun error interrupt */
129 regmap_write(dd->regmap, PDMIC_IER, PDMIC_IER_OVRE);
131 return 0;
134 static void atmel_pdmic_cpu_dai_shutdown(struct snd_pcm_substream *substream,
135 struct snd_soc_dai *cpu_dai)
137 struct snd_soc_pcm_runtime *rtd = substream->private_data;
138 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
140 /* Disable the overrun error interrupt */
141 regmap_write(dd->regmap, PDMIC_IDR, PDMIC_IDR_OVRE);
143 clk_disable_unprepare(dd->gclk);
144 clk_disable_unprepare(dd->pclk);
147 static int atmel_pdmic_cpu_dai_prepare(struct snd_pcm_substream *substream,
148 struct snd_soc_dai *cpu_dai)
150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
151 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
152 u32 val;
154 /* Clean the PDMIC Converted Data Register */
155 return regmap_read(dd->regmap, PDMIC_CDR, &val);
158 static const struct snd_soc_dai_ops atmel_pdmic_cpu_dai_ops = {
159 .startup = atmel_pdmic_cpu_dai_startup,
160 .shutdown = atmel_pdmic_cpu_dai_shutdown,
161 .prepare = atmel_pdmic_cpu_dai_prepare,
164 #define ATMEL_PDMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
166 static struct snd_soc_dai_driver atmel_pdmic_cpu_dai = {
167 .capture = {
168 .channels_min = 1,
169 .channels_max = 1,
170 .rates = SNDRV_PCM_RATE_KNOT,
171 .formats = ATMEL_PDMIC_FORMATS,},
172 .ops = &atmel_pdmic_cpu_dai_ops,
175 static const struct snd_soc_component_driver atmel_pdmic_cpu_dai_component = {
176 .name = "atmel-pdmic",
179 /* platform */
180 #define ATMEL_PDMIC_MAX_BUF_SIZE (64 * 1024)
181 #define ATMEL_PDMIC_PREALLOC_BUF_SIZE ATMEL_PDMIC_MAX_BUF_SIZE
183 static const struct snd_pcm_hardware atmel_pdmic_hw = {
184 .info = SNDRV_PCM_INFO_MMAP
185 | SNDRV_PCM_INFO_MMAP_VALID
186 | SNDRV_PCM_INFO_INTERLEAVED
187 | SNDRV_PCM_INFO_RESUME
188 | SNDRV_PCM_INFO_PAUSE,
189 .formats = ATMEL_PDMIC_FORMATS,
190 .buffer_bytes_max = ATMEL_PDMIC_MAX_BUF_SIZE,
191 .period_bytes_min = 256,
192 .period_bytes_max = 32 * 1024,
193 .periods_min = 2,
194 .periods_max = 256,
197 static int
198 atmel_pdmic_platform_configure_dma(struct snd_pcm_substream *substream,
199 struct snd_pcm_hw_params *params,
200 struct dma_slave_config *slave_config)
202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
203 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
204 int ret;
206 ret = snd_hwparams_to_dma_slave_config(substream, params,
207 slave_config);
208 if (ret) {
209 dev_err(rtd->platform->dev,
210 "hw params to dma slave configure failed\n");
211 return ret;
214 slave_config->src_addr = dd->phy_base + PDMIC_CDR;
215 slave_config->src_maxburst = 1;
216 slave_config->dst_maxburst = 1;
218 return 0;
221 static const struct snd_dmaengine_pcm_config
222 atmel_pdmic_dmaengine_pcm_config = {
223 .prepare_slave_config = atmel_pdmic_platform_configure_dma,
224 .pcm_hardware = &atmel_pdmic_hw,
225 .prealloc_buffer_size = ATMEL_PDMIC_PREALLOC_BUF_SIZE,
228 /* codec */
229 /* Mic Gain = dgain * 2^(-scale) */
230 struct mic_gain {
231 unsigned int dgain;
232 unsigned int scale;
235 /* range from -90 dB to 90 dB */
236 static const struct mic_gain mic_gain_table[] = {
237 { 1, 15}, { 1, 14}, /* -90, -84 dB */
238 { 3, 15}, { 1, 13}, { 3, 14}, { 1, 12}, /* -81, -78, -75, -72 dB */
239 { 5, 14}, { 13, 15}, /* -70, -68 dB */
240 { 9, 14}, { 21, 15}, { 23, 15}, { 13, 14}, /* -65 ~ -62 dB */
241 { 29, 15}, { 33, 15}, { 37, 15}, { 41, 15}, /* -61 ~ -58 dB */
242 { 23, 14}, { 13, 13}, { 58, 15}, { 65, 15}, /* -57 ~ -54 dB */
243 { 73, 15}, { 41, 14}, { 23, 13}, { 13, 12}, /* -53 ~ -50 dB */
244 { 29, 13}, { 65, 14}, { 73, 14}, { 41, 13}, /* -49 ~ -46 dB */
245 { 23, 12}, { 207, 15}, { 29, 12}, { 65, 13}, /* -45 ~ -42 dB */
246 { 73, 13}, { 41, 12}, { 23, 11}, { 413, 15}, /* -41 ~ -38 dB */
247 { 463, 15}, { 519, 15}, { 583, 15}, { 327, 14}, /* -37 ~ -34 dB */
248 { 367, 14}, { 823, 15}, { 231, 13}, { 1036, 15}, /* -33 ~ -30 dB */
249 { 1163, 15}, { 1305, 15}, { 183, 12}, { 1642, 15}, /* -29 ~ -26 dB */
250 { 1843, 15}, { 2068, 15}, { 145, 11}, { 2603, 15}, /* -25 ~ -22 dB */
251 { 365, 12}, { 3277, 15}, { 3677, 15}, { 4125, 15}, /* -21 ~ -18 dB */
252 { 4629, 15}, { 5193, 15}, { 5827, 15}, { 3269, 14}, /* -17 ~ -14 dB */
253 { 917, 12}, { 8231, 15}, { 9235, 15}, { 5181, 14}, /* -13 ~ -10 dB */
254 {11627, 15}, {13045, 15}, {14637, 15}, {16423, 15}, /* -9 ~ -6 dB */
255 {18427, 15}, {20675, 15}, { 5799, 13}, {26029, 15}, /* -5 ~ -2 dB */
256 { 7301, 13}, { 1, 0}, {18383, 14}, {10313, 13}, /* -1 ~ 2 dB */
257 {23143, 14}, {25967, 14}, {29135, 14}, {16345, 13}, /* 3 ~ 6 dB */
258 { 4585, 11}, {20577, 13}, { 1443, 9}, {25905, 13}, /* 7 ~ 10 dB */
259 {14533, 12}, { 8153, 11}, { 2287, 9}, {20529, 12}, /* 11 ~ 14 dB */
260 {11517, 11}, { 6461, 10}, {28997, 12}, { 4067, 9}, /* 15 ~ 18 dB */
261 {18253, 11}, { 10, 0}, {22979, 11}, {25783, 11}, /* 19 ~ 22 dB */
262 {28929, 11}, {32459, 11}, { 9105, 9}, {20431, 10}, /* 23 ~ 26 dB */
263 {22925, 10}, {12861, 9}, { 7215, 8}, {16191, 9}, /* 27 ~ 30 dB */
264 { 9083, 8}, {20383, 9}, {11435, 8}, { 6145, 7}, /* 31 ~ 34 dB */
265 { 3599, 6}, {32305, 9}, {18123, 8}, {20335, 8}, /* 35 ~ 38 dB */
266 { 713, 3}, { 100, 0}, { 7181, 6}, { 8057, 6}, /* 39 ~ 42 dB */
267 { 565, 2}, {20287, 7}, {11381, 6}, {25539, 7}, /* 43 ~ 46 dB */
268 { 1791, 3}, { 4019, 4}, { 9019, 5}, {20239, 6}, /* 47 ~ 50 dB */
269 { 5677, 4}, {25479, 6}, { 7147, 4}, { 8019, 4}, /* 51 ~ 54 dB */
270 {17995, 5}, {20191, 5}, {11327, 4}, {12709, 4}, /* 55 ~ 58 dB */
271 { 3565, 2}, { 1000, 0}, { 1122, 0}, { 1259, 0}, /* 59 ~ 62 dB */
272 { 2825, 1}, {12679, 3}, { 7113, 2}, { 7981, 2}, /* 63 ~ 66 dB */
273 { 8955, 2}, {20095, 3}, {22547, 3}, {12649, 2}, /* 67 ~ 70 dB */
274 {28385, 3}, { 3981, 0}, {17867, 2}, {20047, 2}, /* 71 ~ 74 dB */
275 {11247, 1}, {12619, 1}, {14159, 1}, {31773, 2}, /* 75 ~ 78 dB */
276 {17825, 1}, {10000, 0}, {11220, 0}, {12589, 0}, /* 79 ~ 82 dB */
277 {28251, 1}, {15849, 0}, {17783, 0}, {19953, 0}, /* 83 ~ 86 dB */
278 {22387, 0}, {25119, 0}, {28184, 0}, {31623, 0}, /* 87 ~ 90 dB */
281 static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
282 0, 1, TLV_DB_SCALE_ITEM(-9000, 600, 0),
283 2, 5, TLV_DB_SCALE_ITEM(-8100, 300, 0),
284 6, 7, TLV_DB_SCALE_ITEM(-7000, 200, 0),
285 8, ARRAY_SIZE(mic_gain_table)-1, TLV_DB_SCALE_ITEM(-6500, 100, 0),
288 static int pdmic_get_mic_volsw(struct snd_kcontrol *kcontrol,
289 struct snd_ctl_elem_value *ucontrol)
291 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
292 unsigned int dgain_val, scale_val;
293 int i;
295 dgain_val = (snd_soc_read(codec, PDMIC_DSPR1) & PDMIC_DSPR1_DGAIN_MASK)
296 >> PDMIC_DSPR1_DGAIN_SHIFT;
298 scale_val = (snd_soc_read(codec, PDMIC_DSPR0) & PDMIC_DSPR0_SCALE_MASK)
299 >> PDMIC_DSPR0_SCALE_SHIFT;
301 for (i = 0; i < ARRAY_SIZE(mic_gain_table); i++) {
302 if ((mic_gain_table[i].dgain == dgain_val) &&
303 (mic_gain_table[i].scale == scale_val))
304 ucontrol->value.integer.value[0] = i;
307 return 0;
310 static int pdmic_put_mic_volsw(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
313 struct soc_mixer_control *mc =
314 (struct soc_mixer_control *)kcontrol->private_value;
315 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
316 int max = mc->max;
317 unsigned int val;
318 int ret;
320 val = ucontrol->value.integer.value[0];
322 if (val > max)
323 return -EINVAL;
325 ret = snd_soc_update_bits(codec, PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_MASK,
326 mic_gain_table[val].dgain << PDMIC_DSPR1_DGAIN_SHIFT);
327 if (ret < 0)
328 return ret;
330 ret = snd_soc_update_bits(codec, PDMIC_DSPR0, PDMIC_DSPR0_SCALE_MASK,
331 mic_gain_table[val].scale << PDMIC_DSPR0_SCALE_SHIFT);
332 if (ret < 0)
333 return ret;
335 return 0;
338 static const struct snd_kcontrol_new atmel_pdmic_snd_controls[] = {
339 SOC_SINGLE_EXT_TLV("Mic Capture Volume", PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_SHIFT,
340 ARRAY_SIZE(mic_gain_table)-1, 0,
341 pdmic_get_mic_volsw, pdmic_put_mic_volsw, mic_gain_tlv),
343 SOC_SINGLE("High Pass Filter Switch", PDMIC_DSPR0,
344 PDMIC_DSPR0_HPFBYP_SHIFT, 1, 1),
346 SOC_SINGLE("SINCC Filter Switch", PDMIC_DSPR0, PDMIC_DSPR0_SINBYP_SHIFT, 1, 1),
349 static int atmel_pdmic_codec_probe(struct snd_soc_codec *codec)
351 struct snd_soc_card *card = snd_soc_codec_get_drvdata(codec);
352 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
354 snd_soc_update_bits(codec, PDMIC_DSPR1, PDMIC_DSPR1_OFFSET_MASK,
355 (u32)(dd->pdata->mic_offset << PDMIC_DSPR1_OFFSET_SHIFT));
357 return 0;
360 static struct snd_soc_codec_driver soc_codec_dev_pdmic = {
361 .probe = atmel_pdmic_codec_probe,
362 .component_driver = {
363 .controls = atmel_pdmic_snd_controls,
364 .num_controls = ARRAY_SIZE(atmel_pdmic_snd_controls),
368 /* codec dai component */
369 #define PDMIC_MR_PRESCAL_MAX_VAL 127
371 static int
372 atmel_pdmic_codec_dai_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params,
374 struct snd_soc_dai *codec_dai)
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
378 struct snd_soc_codec *codec = codec_dai->codec;
379 unsigned int rate_min = substream->runtime->hw.rate_min;
380 unsigned int rate_max = substream->runtime->hw.rate_max;
381 int fs = params_rate(params);
382 int bits = params_width(params);
383 unsigned long pclk_rate, gclk_rate;
384 unsigned int f_pdmic;
385 u32 mr_val, dspr0_val, pclk_prescal, gclk_prescal;
387 if (params_channels(params) != 1) {
388 dev_err(codec->dev,
389 "only supports one channel\n");
390 return -EINVAL;
393 if ((fs < rate_min) || (fs > rate_max)) {
394 dev_err(codec->dev,
395 "sample rate is %dHz, min rate is %dHz, max rate is %dHz\n",
396 fs, rate_min, rate_max);
398 return -EINVAL;
401 switch (bits) {
402 case 16:
403 dspr0_val = (PDMIC_DSPR0_SIZE_16_BITS
404 << PDMIC_DSPR0_SIZE_SHIFT);
405 break;
406 case 32:
407 dspr0_val = (PDMIC_DSPR0_SIZE_32_BITS
408 << PDMIC_DSPR0_SIZE_SHIFT);
409 break;
410 default:
411 return -EINVAL;
414 if ((fs << 7) > (rate_max << 6)) {
415 f_pdmic = fs << 6;
416 dspr0_val |= PDMIC_DSPR0_OSR_64 << PDMIC_DSPR0_OSR_SHIFT;
417 } else {
418 f_pdmic = fs << 7;
419 dspr0_val |= PDMIC_DSPR0_OSR_128 << PDMIC_DSPR0_OSR_SHIFT;
422 pclk_rate = clk_get_rate(dd->pclk);
423 gclk_rate = clk_get_rate(dd->gclk);
425 /* PRESCAL = SELCK/(2*f_pdmic) - 1*/
426 pclk_prescal = (u32)(pclk_rate/(f_pdmic << 1)) - 1;
427 gclk_prescal = (u32)(gclk_rate/(f_pdmic << 1)) - 1;
429 if ((pclk_prescal > PDMIC_MR_PRESCAL_MAX_VAL) ||
430 (gclk_rate/((gclk_prescal + 1) << 1) <
431 pclk_rate/((pclk_prescal + 1) << 1))) {
432 mr_val = gclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
433 mr_val |= PDMIC_MR_CLKS_GCK << PDMIC_MR_CLKS_SHIFT;
434 } else {
435 mr_val = pclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
436 mr_val |= PDMIC_MR_CLKS_PCK << PDMIC_MR_CLKS_SHIFT;
439 snd_soc_update_bits(codec, PDMIC_MR,
440 PDMIC_MR_PRESCAL_MASK | PDMIC_MR_CLKS_MASK, mr_val);
442 snd_soc_update_bits(codec, PDMIC_DSPR0,
443 PDMIC_DSPR0_OSR_MASK | PDMIC_DSPR0_SIZE_MASK, dspr0_val);
445 return 0;
448 static int atmel_pdmic_codec_dai_prepare(struct snd_pcm_substream *substream,
449 struct snd_soc_dai *codec_dai)
451 struct snd_soc_codec *codec = codec_dai->codec;
453 snd_soc_update_bits(codec, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
454 PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
456 return 0;
459 static int atmel_pdmic_codec_dai_trigger(struct snd_pcm_substream *substream,
460 int cmd, struct snd_soc_dai *codec_dai)
462 struct snd_soc_codec *codec = codec_dai->codec;
463 u32 val;
465 switch (cmd) {
466 case SNDRV_PCM_TRIGGER_START:
467 case SNDRV_PCM_TRIGGER_RESUME:
468 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
469 val = PDMIC_CR_ENPDM_EN << PDMIC_CR_ENPDM_SHIFT;
470 break;
471 case SNDRV_PCM_TRIGGER_STOP:
472 case SNDRV_PCM_TRIGGER_SUSPEND:
473 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
474 val = PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT;
475 break;
476 default:
477 return -EINVAL;
480 snd_soc_update_bits(codec, PDMIC_CR, PDMIC_CR_ENPDM_MASK, val);
482 return 0;
485 static const struct snd_soc_dai_ops atmel_pdmic_codec_dai_ops = {
486 .hw_params = atmel_pdmic_codec_dai_hw_params,
487 .prepare = atmel_pdmic_codec_dai_prepare,
488 .trigger = atmel_pdmic_codec_dai_trigger,
491 #define ATMEL_PDMIC_CODEC_DAI_NAME "atmel-pdmic-hifi"
493 static struct snd_soc_dai_driver atmel_pdmic_codec_dai = {
494 .name = ATMEL_PDMIC_CODEC_DAI_NAME,
495 .capture = {
496 .stream_name = "Capture",
497 .channels_min = 1,
498 .channels_max = 1,
499 .rates = SNDRV_PCM_RATE_KNOT,
500 .formats = ATMEL_PDMIC_FORMATS,
502 .ops = &atmel_pdmic_codec_dai_ops,
505 /* ASoC sound card */
506 static int atmel_pdmic_asoc_card_init(struct device *dev,
507 struct snd_soc_card *card)
509 struct snd_soc_dai_link *dai_link;
510 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
512 dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
513 if (!dai_link)
514 return -ENOMEM;
516 dai_link->name = "PDMIC";
517 dai_link->stream_name = "PDMIC PCM";
518 dai_link->codec_dai_name = ATMEL_PDMIC_CODEC_DAI_NAME;
519 dai_link->cpu_dai_name = dev_name(dev);
520 dai_link->codec_name = dev_name(dev);
521 dai_link->platform_name = dev_name(dev);
523 card->dai_link = dai_link;
524 card->num_links = 1;
525 card->name = dd->pdata->card_name;
526 card->dev = dev;
528 return 0;
531 static void atmel_pdmic_get_sample_rate(struct atmel_pdmic *dd,
532 unsigned int *rate_min, unsigned int *rate_max)
534 u32 mic_min_freq = dd->pdata->mic_min_freq;
535 u32 mic_max_freq = dd->pdata->mic_max_freq;
536 u32 clk_max_rate = (u32)(clk_get_rate(dd->pclk) >> 1);
537 u32 clk_min_rate = (u32)(clk_get_rate(dd->gclk) >> 8);
539 if (mic_max_freq > clk_max_rate)
540 mic_max_freq = clk_max_rate;
542 if (mic_min_freq < clk_min_rate)
543 mic_min_freq = clk_min_rate;
545 *rate_min = DIV_ROUND_CLOSEST(mic_min_freq, 128);
546 *rate_max = mic_max_freq >> 6;
549 /* PDMIC interrupt handler */
550 static irqreturn_t atmel_pdmic_interrupt(int irq, void *dev_id)
552 struct atmel_pdmic *dd = (struct atmel_pdmic *)dev_id;
553 u32 pdmic_isr;
554 irqreturn_t ret = IRQ_NONE;
556 regmap_read(dd->regmap, PDMIC_ISR, &pdmic_isr);
558 if (pdmic_isr & PDMIC_ISR_OVRE) {
559 regmap_update_bits(dd->regmap, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
560 PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
562 snd_pcm_stop_xrun(dd->substream);
564 ret = IRQ_HANDLED;
567 return ret;
570 /* regmap configuration */
571 #define ATMEL_PDMIC_REG_MAX 0x124
572 static const struct regmap_config atmel_pdmic_regmap_config = {
573 .reg_bits = 32,
574 .reg_stride = 4,
575 .val_bits = 32,
576 .max_register = ATMEL_PDMIC_REG_MAX,
579 static int atmel_pdmic_probe(struct platform_device *pdev)
581 struct device *dev = &pdev->dev;
582 struct atmel_pdmic *dd;
583 struct resource *res;
584 void __iomem *io_base;
585 const struct atmel_pdmic_pdata *pdata;
586 struct snd_soc_card *card;
587 unsigned int rate_min, rate_max;
588 int ret;
590 pdata = atmel_pdmic_dt_init(dev);
591 if (IS_ERR(pdata))
592 return PTR_ERR(pdata);
594 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
595 if (!dd)
596 return -ENOMEM;
598 dd->pdata = pdata;
600 dd->irq = platform_get_irq(pdev, 0);
601 if (dd->irq < 0) {
602 ret = dd->irq;
603 dev_err(dev, "failed to get irq: %d\n", ret);
604 return ret;
607 dd->pclk = devm_clk_get(dev, "pclk");
608 if (IS_ERR(dd->pclk)) {
609 ret = PTR_ERR(dd->pclk);
610 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
611 return ret;
614 dd->gclk = devm_clk_get(dev, "gclk");
615 if (IS_ERR(dd->gclk)) {
616 ret = PTR_ERR(dd->gclk);
617 dev_err(dev, "failed to get GCK: %d\n", ret);
618 return ret;
621 /* The gclk clock frequency must always be three times
622 * lower than the pclk clock frequency
624 ret = clk_set_rate(dd->gclk, clk_get_rate(dd->pclk)/3);
625 if (ret) {
626 dev_err(dev, "failed to set GCK clock rate: %d\n", ret);
627 return ret;
630 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
631 io_base = devm_ioremap_resource(dev, res);
632 if (IS_ERR(io_base)) {
633 ret = PTR_ERR(io_base);
634 dev_err(dev, "failed to remap register memory: %d\n", ret);
635 return ret;
638 dd->phy_base = res->start;
640 dd->regmap = devm_regmap_init_mmio(dev, io_base,
641 &atmel_pdmic_regmap_config);
642 if (IS_ERR(dd->regmap)) {
643 ret = PTR_ERR(dd->regmap);
644 dev_err(dev, "failed to init register map: %d\n", ret);
645 return ret;
648 ret = devm_request_irq(dev, dd->irq, atmel_pdmic_interrupt, 0,
649 "PDMIC", (void *)dd);
650 if (ret < 0) {
651 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
652 dd->irq, ret);
653 return ret;
656 /* Get the minimal and maximal sample rate that the microphone supports */
657 atmel_pdmic_get_sample_rate(dd, &rate_min, &rate_max);
659 /* register cpu dai */
660 atmel_pdmic_cpu_dai.capture.rate_min = rate_min;
661 atmel_pdmic_cpu_dai.capture.rate_max = rate_max;
662 ret = devm_snd_soc_register_component(dev,
663 &atmel_pdmic_cpu_dai_component,
664 &atmel_pdmic_cpu_dai, 1);
665 if (ret) {
666 dev_err(dev, "could not register CPU DAI: %d\n", ret);
667 return ret;
670 /* register platform */
671 ret = devm_snd_dmaengine_pcm_register(dev,
672 &atmel_pdmic_dmaengine_pcm_config,
674 if (ret) {
675 dev_err(dev, "could not register platform: %d\n", ret);
676 return ret;
679 /* register codec and codec dai */
680 atmel_pdmic_codec_dai.capture.rate_min = rate_min;
681 atmel_pdmic_codec_dai.capture.rate_max = rate_max;
682 ret = snd_soc_register_codec(dev, &soc_codec_dev_pdmic,
683 &atmel_pdmic_codec_dai, 1);
684 if (ret) {
685 dev_err(dev, "could not register codec: %d\n", ret);
686 return ret;
689 /* register sound card */
690 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
691 if (!card) {
692 ret = -ENOMEM;
693 goto unregister_codec;
696 snd_soc_card_set_drvdata(card, dd);
698 ret = atmel_pdmic_asoc_card_init(dev, card);
699 if (ret) {
700 dev_err(dev, "failed to init sound card: %d\n", ret);
701 goto unregister_codec;
704 ret = devm_snd_soc_register_card(dev, card);
705 if (ret) {
706 dev_err(dev, "failed to register sound card: %d\n", ret);
707 goto unregister_codec;
710 return 0;
712 unregister_codec:
713 snd_soc_unregister_codec(dev);
714 return ret;
717 static int atmel_pdmic_remove(struct platform_device *pdev)
719 snd_soc_unregister_codec(&pdev->dev);
720 return 0;
723 static struct platform_driver atmel_pdmic_driver = {
724 .driver = {
725 .name = "atmel-pdmic",
726 .of_match_table = of_match_ptr(atmel_pdmic_of_match),
727 .pm = &snd_soc_pm_ops,
729 .probe = atmel_pdmic_probe,
730 .remove = atmel_pdmic_remove,
732 module_platform_driver(atmel_pdmic_driver);
734 MODULE_DESCRIPTION("Atmel PDMIC driver under ALSA SoC architecture");
735 MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
736 MODULE_LICENSE("GPL v2");