1
2
3
4
5
6
7
8
9
10
11
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"
22
23struct atmel_pdmic_pdata {
24 u32 mic_min_freq;
25 u32 mic_max_freq;
26 s32 mic_offset;
27 const char *card_name;
28};
29
30struct atmel_pdmic {
31 dma_addr_t phy_base;
32 struct regmap *regmap;
33 struct clk *pclk;
34 struct clk *gclk;
35 struct device *dev;
36 int irq;
37 struct snd_pcm_substream *substream;
38 const struct atmel_pdmic_pdata *pdata;
39};
40
41static const struct of_device_id atmel_pdmic_of_match[] = {
42 {
43 .compatible = "atmel,sama5d2-pdmic",
44 }, {
45
46 }
47};
48MODULE_DEVICE_TABLE(of, atmel_pdmic_of_match);
49
50#define PDMIC_OFFSET_MAX_VAL S16_MAX
51#define PDMIC_OFFSET_MIN_VAL S16_MIN
52
53static struct atmel_pdmic_pdata *atmel_pdmic_dt_init(struct device *dev)
54{
55 struct device_node *np = dev->of_node;
56 struct atmel_pdmic_pdata *pdata;
57
58 if (!np) {
59 dev_err(dev, "device node not found\n");
60 return ERR_PTR(-EINVAL);
61 }
62
63 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
64 if (!pdata)
65 return ERR_PTR(-ENOMEM);
66
67 if (of_property_read_string(np, "atmel,model", &pdata->card_name))
68 pdata->card_name = "PDMIC";
69
70 if (of_property_read_u32(np, "atmel,mic-min-freq",
71 &pdata->mic_min_freq)) {
72 dev_err(dev, "failed to get mic-min-freq\n");
73 return ERR_PTR(-EINVAL);
74 }
75
76 if (of_property_read_u32(np, "atmel,mic-max-freq",
77 &pdata->mic_max_freq)) {
78 dev_err(dev, "failed to get mic-max-freq\n");
79 return ERR_PTR(-EINVAL);
80 }
81
82 if (pdata->mic_max_freq < pdata->mic_min_freq) {
83 dev_err(dev,
84 "mic-max-freq should not be less than mic-min-freq\n");
85 return ERR_PTR(-EINVAL);
86 }
87
88 if (of_property_read_s32(np, "atmel,mic-offset", &pdata->mic_offset))
89 pdata->mic_offset = 0;
90
91 if (pdata->mic_offset > PDMIC_OFFSET_MAX_VAL) {
92 dev_warn(dev,
93 "mic-offset value %d is larger than the max value %d, the max value is specified\n",
94 pdata->mic_offset, PDMIC_OFFSET_MAX_VAL);
95 pdata->mic_offset = PDMIC_OFFSET_MAX_VAL;
96 } else if (pdata->mic_offset < PDMIC_OFFSET_MIN_VAL) {
97 dev_warn(dev,
98 "mic-offset value %d is less than the min value %d, the min value is specified\n",
99 pdata->mic_offset, PDMIC_OFFSET_MIN_VAL);
100 pdata->mic_offset = PDMIC_OFFSET_MIN_VAL;
101 }
102
103 return pdata;
104}
105
106
107static int atmel_pdmic_cpu_dai_startup(struct snd_pcm_substream *substream,
108 struct snd_soc_dai *cpu_dai)
109{
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
111 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
112 int ret;
113
114 ret = clk_prepare_enable(dd->gclk);
115 if (ret)
116 return ret;
117
118 ret = clk_prepare_enable(dd->pclk);
119 if (ret) {
120 clk_disable_unprepare(dd->gclk);
121 return ret;
122 }
123
124
125 regmap_write(dd->regmap, PDMIC_CR, 0);
126
127 dd->substream = substream;
128
129
130 regmap_write(dd->regmap, PDMIC_IER, PDMIC_IER_OVRE);
131
132 return 0;
133}
134
135static void atmel_pdmic_cpu_dai_shutdown(struct snd_pcm_substream *substream,
136 struct snd_soc_dai *cpu_dai)
137{
138 struct snd_soc_pcm_runtime *rtd = substream->private_data;
139 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
140
141
142 regmap_write(dd->regmap, PDMIC_IDR, PDMIC_IDR_OVRE);
143
144 clk_disable_unprepare(dd->gclk);
145 clk_disable_unprepare(dd->pclk);
146}
147
148static int atmel_pdmic_cpu_dai_prepare(struct snd_pcm_substream *substream,
149 struct snd_soc_dai *cpu_dai)
150{
151 struct snd_soc_pcm_runtime *rtd = substream->private_data;
152 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
153 u32 val;
154
155
156 return regmap_read(dd->regmap, PDMIC_CDR, &val);
157}
158
159static const struct snd_soc_dai_ops atmel_pdmic_cpu_dai_ops = {
160 .startup = atmel_pdmic_cpu_dai_startup,
161 .shutdown = atmel_pdmic_cpu_dai_shutdown,
162 .prepare = atmel_pdmic_cpu_dai_prepare,
163};
164
165#define ATMEL_PDMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
166
167static struct snd_soc_dai_driver atmel_pdmic_cpu_dai = {
168 .capture = {
169 .channels_min = 1,
170 .channels_max = 1,
171 .rates = SNDRV_PCM_RATE_KNOT,
172 .formats = ATMEL_PDMIC_FORMATS,},
173 .ops = &atmel_pdmic_cpu_dai_ops,
174};
175
176static const struct snd_soc_component_driver atmel_pdmic_cpu_dai_component = {
177 .name = "atmel-pdmic",
178};
179
180
181#define ATMEL_PDMIC_MAX_BUF_SIZE (64 * 1024)
182#define ATMEL_PDMIC_PREALLOC_BUF_SIZE ATMEL_PDMIC_MAX_BUF_SIZE
183
184static const struct snd_pcm_hardware atmel_pdmic_hw = {
185 .info = SNDRV_PCM_INFO_MMAP
186 | SNDRV_PCM_INFO_MMAP_VALID
187 | SNDRV_PCM_INFO_INTERLEAVED
188 | SNDRV_PCM_INFO_RESUME
189 | SNDRV_PCM_INFO_PAUSE,
190 .formats = ATMEL_PDMIC_FORMATS,
191 .buffer_bytes_max = ATMEL_PDMIC_MAX_BUF_SIZE,
192 .period_bytes_min = 256,
193 .period_bytes_max = 32 * 1024,
194 .periods_min = 2,
195 .periods_max = 256,
196};
197
198static int
199atmel_pdmic_platform_configure_dma(struct snd_pcm_substream *substream,
200 struct snd_pcm_hw_params *params,
201 struct dma_slave_config *slave_config)
202{
203 struct snd_soc_pcm_runtime *rtd = substream->private_data;
204 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
205 int ret;
206
207 ret = snd_hwparams_to_dma_slave_config(substream, params,
208 slave_config);
209 if (ret) {
210 dev_err(dd->dev,
211 "hw params to dma slave configure failed\n");
212 return ret;
213 }
214
215 slave_config->src_addr = dd->phy_base + PDMIC_CDR;
216 slave_config->src_maxburst = 1;
217 slave_config->dst_maxburst = 1;
218
219 return 0;
220}
221
222static const struct snd_dmaengine_pcm_config
223atmel_pdmic_dmaengine_pcm_config = {
224 .prepare_slave_config = atmel_pdmic_platform_configure_dma,
225 .pcm_hardware = &atmel_pdmic_hw,
226 .prealloc_buffer_size = ATMEL_PDMIC_PREALLOC_BUF_SIZE,
227};
228
229
230
231struct mic_gain {
232 unsigned int dgain;
233 unsigned int scale;
234};
235
236
237static const struct mic_gain mic_gain_table[] = {
238{ 1, 15}, { 1, 14},
239{ 3, 15}, { 1, 13}, { 3, 14}, { 1, 12},
240{ 5, 14}, { 13, 15},
241{ 9, 14}, { 21, 15}, { 23, 15}, { 13, 14},
242{ 29, 15}, { 33, 15}, { 37, 15}, { 41, 15},
243{ 23, 14}, { 13, 13}, { 58, 15}, { 65, 15},
244{ 73, 15}, { 41, 14}, { 23, 13}, { 13, 12},
245{ 29, 13}, { 65, 14}, { 73, 14}, { 41, 13},
246{ 23, 12}, { 207, 15}, { 29, 12}, { 65, 13},
247{ 73, 13}, { 41, 12}, { 23, 11}, { 413, 15},
248{ 463, 15}, { 519, 15}, { 583, 15}, { 327, 14},
249{ 367, 14}, { 823, 15}, { 231, 13}, { 1036, 15},
250{ 1163, 15}, { 1305, 15}, { 183, 12}, { 1642, 15},
251{ 1843, 15}, { 2068, 15}, { 145, 11}, { 2603, 15},
252{ 365, 12}, { 3277, 15}, { 3677, 15}, { 4125, 15},
253{ 4629, 15}, { 5193, 15}, { 5827, 15}, { 3269, 14},
254{ 917, 12}, { 8231, 15}, { 9235, 15}, { 5181, 14},
255{11627, 15}, {13045, 15}, {14637, 15}, {16423, 15},
256{18427, 15}, {20675, 15}, { 5799, 13}, {26029, 15},
257{ 7301, 13}, { 1, 0}, {18383, 14}, {10313, 13},
258{23143, 14}, {25967, 14}, {29135, 14}, {16345, 13},
259{ 4585, 11}, {20577, 13}, { 1443, 9}, {25905, 13},
260{14533, 12}, { 8153, 11}, { 2287, 9}, {20529, 12},
261{11517, 11}, { 6461, 10}, {28997, 12}, { 4067, 9},
262{18253, 11}, { 10, 0}, {22979, 11}, {25783, 11},
263{28929, 11}, {32459, 11}, { 9105, 9}, {20431, 10},
264{22925, 10}, {12861, 9}, { 7215, 8}, {16191, 9},
265{ 9083, 8}, {20383, 9}, {11435, 8}, { 6145, 7},
266{ 3599, 6}, {32305, 9}, {18123, 8}, {20335, 8},
267{ 713, 3}, { 100, 0}, { 7181, 6}, { 8057, 6},
268{ 565, 2}, {20287, 7}, {11381, 6}, {25539, 7},
269{ 1791, 3}, { 4019, 4}, { 9019, 5}, {20239, 6},
270{ 5677, 4}, {25479, 6}, { 7147, 4}, { 8019, 4},
271{17995, 5}, {20191, 5}, {11327, 4}, {12709, 4},
272{ 3565, 2}, { 1000, 0}, { 1122, 0}, { 1259, 0},
273{ 2825, 1}, {12679, 3}, { 7113, 2}, { 7981, 2},
274{ 8955, 2}, {20095, 3}, {22547, 3}, {12649, 2},
275{28385, 3}, { 3981, 0}, {17867, 2}, {20047, 2},
276{11247, 1}, {12619, 1}, {14159, 1}, {31773, 2},
277{17825, 1}, {10000, 0}, {11220, 0}, {12589, 0},
278{28251, 1}, {15849, 0}, {17783, 0}, {19953, 0},
279{22387, 0}, {25119, 0}, {28184, 0}, {31623, 0},
280};
281
282static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
283 0, 1, TLV_DB_SCALE_ITEM(-9000, 600, 0),
284 2, 5, TLV_DB_SCALE_ITEM(-8100, 300, 0),
285 6, 7, TLV_DB_SCALE_ITEM(-7000, 200, 0),
286 8, ARRAY_SIZE(mic_gain_table)-1, TLV_DB_SCALE_ITEM(-6500, 100, 0),
287);
288
289static int pdmic_get_mic_volsw(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_value *ucontrol)
291{
292 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
293 unsigned int dgain_val, scale_val;
294 int i;
295
296 dgain_val = (snd_soc_component_read32(component, PDMIC_DSPR1) & PDMIC_DSPR1_DGAIN_MASK)
297 >> PDMIC_DSPR1_DGAIN_SHIFT;
298
299 scale_val = (snd_soc_component_read32(component, PDMIC_DSPR0) & PDMIC_DSPR0_SCALE_MASK)
300 >> PDMIC_DSPR0_SCALE_SHIFT;
301
302 for (i = 0; i < ARRAY_SIZE(mic_gain_table); i++) {
303 if ((mic_gain_table[i].dgain == dgain_val) &&
304 (mic_gain_table[i].scale == scale_val))
305 ucontrol->value.integer.value[0] = i;
306 }
307
308 return 0;
309}
310
311static int pdmic_put_mic_volsw(struct snd_kcontrol *kcontrol,
312 struct snd_ctl_elem_value *ucontrol)
313{
314 struct soc_mixer_control *mc =
315 (struct soc_mixer_control *)kcontrol->private_value;
316 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
317 int max = mc->max;
318 unsigned int val;
319 int ret;
320
321 val = ucontrol->value.integer.value[0];
322
323 if (val > max)
324 return -EINVAL;
325
326 ret = snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_MASK,
327 mic_gain_table[val].dgain << PDMIC_DSPR1_DGAIN_SHIFT);
328 if (ret < 0)
329 return ret;
330
331 ret = snd_soc_component_update_bits(component, PDMIC_DSPR0, PDMIC_DSPR0_SCALE_MASK,
332 mic_gain_table[val].scale << PDMIC_DSPR0_SCALE_SHIFT);
333 if (ret < 0)
334 return ret;
335
336 return 0;
337}
338
339static const struct snd_kcontrol_new atmel_pdmic_snd_controls[] = {
340SOC_SINGLE_EXT_TLV("Mic Capture Volume", PDMIC_DSPR1, PDMIC_DSPR1_DGAIN_SHIFT,
341 ARRAY_SIZE(mic_gain_table)-1, 0,
342 pdmic_get_mic_volsw, pdmic_put_mic_volsw, mic_gain_tlv),
343
344SOC_SINGLE("High Pass Filter Switch", PDMIC_DSPR0,
345 PDMIC_DSPR0_HPFBYP_SHIFT, 1, 1),
346
347SOC_SINGLE("SINCC Filter Switch", PDMIC_DSPR0, PDMIC_DSPR0_SINBYP_SHIFT, 1, 1),
348};
349
350static int atmel_pdmic_component_probe(struct snd_soc_component *component)
351{
352 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
353 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
354
355 snd_soc_component_update_bits(component, PDMIC_DSPR1, PDMIC_DSPR1_OFFSET_MASK,
356 (u32)(dd->pdata->mic_offset << PDMIC_DSPR1_OFFSET_SHIFT));
357
358 return 0;
359}
360
361static struct snd_soc_component_driver soc_component_dev_pdmic = {
362 .probe = atmel_pdmic_component_probe,
363 .controls = atmel_pdmic_snd_controls,
364 .num_controls = ARRAY_SIZE(atmel_pdmic_snd_controls),
365 .idle_bias_on = 1,
366 .use_pmdown_time = 1,
367 .endianness = 1,
368 .non_legacy_dai_naming = 1,
369};
370
371
372#define PDMIC_MR_PRESCAL_MAX_VAL 127
373
374static int
375atmel_pdmic_codec_dai_hw_params(struct snd_pcm_substream *substream,
376 struct snd_pcm_hw_params *params,
377 struct snd_soc_dai *codec_dai)
378{
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
380 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(rtd->card);
381 struct snd_soc_component *component = codec_dai->component;
382 unsigned int rate_min = substream->runtime->hw.rate_min;
383 unsigned int rate_max = substream->runtime->hw.rate_max;
384 int fs = params_rate(params);
385 int bits = params_width(params);
386 unsigned long pclk_rate, gclk_rate;
387 unsigned int f_pdmic;
388 u32 mr_val, dspr0_val, pclk_prescal, gclk_prescal;
389
390 if (params_channels(params) != 1) {
391 dev_err(component->dev,
392 "only supports one channel\n");
393 return -EINVAL;
394 }
395
396 if ((fs < rate_min) || (fs > rate_max)) {
397 dev_err(component->dev,
398 "sample rate is %dHz, min rate is %dHz, max rate is %dHz\n",
399 fs, rate_min, rate_max);
400
401 return -EINVAL;
402 }
403
404 switch (bits) {
405 case 16:
406 dspr0_val = (PDMIC_DSPR0_SIZE_16_BITS
407 << PDMIC_DSPR0_SIZE_SHIFT);
408 break;
409 case 32:
410 dspr0_val = (PDMIC_DSPR0_SIZE_32_BITS
411 << PDMIC_DSPR0_SIZE_SHIFT);
412 break;
413 default:
414 return -EINVAL;
415 }
416
417 if ((fs << 7) > (rate_max << 6)) {
418 f_pdmic = fs << 6;
419 dspr0_val |= PDMIC_DSPR0_OSR_64 << PDMIC_DSPR0_OSR_SHIFT;
420 } else {
421 f_pdmic = fs << 7;
422 dspr0_val |= PDMIC_DSPR0_OSR_128 << PDMIC_DSPR0_OSR_SHIFT;
423 }
424
425 pclk_rate = clk_get_rate(dd->pclk);
426 gclk_rate = clk_get_rate(dd->gclk);
427
428
429 pclk_prescal = (u32)(pclk_rate/(f_pdmic << 1)) - 1;
430 gclk_prescal = (u32)(gclk_rate/(f_pdmic << 1)) - 1;
431
432 if ((pclk_prescal > PDMIC_MR_PRESCAL_MAX_VAL) ||
433 (gclk_rate/((gclk_prescal + 1) << 1) <
434 pclk_rate/((pclk_prescal + 1) << 1))) {
435 mr_val = gclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
436 mr_val |= PDMIC_MR_CLKS_GCK << PDMIC_MR_CLKS_SHIFT;
437 } else {
438 mr_val = pclk_prescal << PDMIC_MR_PRESCAL_SHIFT;
439 mr_val |= PDMIC_MR_CLKS_PCK << PDMIC_MR_CLKS_SHIFT;
440 }
441
442 snd_soc_component_update_bits(component, PDMIC_MR,
443 PDMIC_MR_PRESCAL_MASK | PDMIC_MR_CLKS_MASK, mr_val);
444
445 snd_soc_component_update_bits(component, PDMIC_DSPR0,
446 PDMIC_DSPR0_OSR_MASK | PDMIC_DSPR0_SIZE_MASK, dspr0_val);
447
448 return 0;
449}
450
451static int atmel_pdmic_codec_dai_prepare(struct snd_pcm_substream *substream,
452 struct snd_soc_dai *codec_dai)
453{
454 struct snd_soc_component *component = codec_dai->component;
455
456 snd_soc_component_update_bits(component, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
457 PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
458
459 return 0;
460}
461
462static int atmel_pdmic_codec_dai_trigger(struct snd_pcm_substream *substream,
463 int cmd, struct snd_soc_dai *codec_dai)
464{
465 struct snd_soc_component *component = codec_dai->component;
466 u32 val;
467
468 switch (cmd) {
469 case SNDRV_PCM_TRIGGER_START:
470 case SNDRV_PCM_TRIGGER_RESUME:
471 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
472 val = PDMIC_CR_ENPDM_EN << PDMIC_CR_ENPDM_SHIFT;
473 break;
474 case SNDRV_PCM_TRIGGER_STOP:
475 case SNDRV_PCM_TRIGGER_SUSPEND:
476 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
477 val = PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT;
478 break;
479 default:
480 return -EINVAL;
481 }
482
483 snd_soc_component_update_bits(component, PDMIC_CR, PDMIC_CR_ENPDM_MASK, val);
484
485 return 0;
486}
487
488static const struct snd_soc_dai_ops atmel_pdmic_codec_dai_ops = {
489 .hw_params = atmel_pdmic_codec_dai_hw_params,
490 .prepare = atmel_pdmic_codec_dai_prepare,
491 .trigger = atmel_pdmic_codec_dai_trigger,
492};
493
494#define ATMEL_PDMIC_CODEC_DAI_NAME "atmel-pdmic-hifi"
495
496static struct snd_soc_dai_driver atmel_pdmic_codec_dai = {
497 .name = ATMEL_PDMIC_CODEC_DAI_NAME,
498 .capture = {
499 .stream_name = "Capture",
500 .channels_min = 1,
501 .channels_max = 1,
502 .rates = SNDRV_PCM_RATE_KNOT,
503 .formats = ATMEL_PDMIC_FORMATS,
504 },
505 .ops = &atmel_pdmic_codec_dai_ops,
506};
507
508
509static int atmel_pdmic_asoc_card_init(struct device *dev,
510 struct snd_soc_card *card)
511{
512 struct snd_soc_dai_link *dai_link;
513 struct atmel_pdmic *dd = snd_soc_card_get_drvdata(card);
514
515 dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
516 if (!dai_link)
517 return -ENOMEM;
518
519 dai_link->name = "PDMIC";
520 dai_link->stream_name = "PDMIC PCM";
521 dai_link->codec_dai_name = ATMEL_PDMIC_CODEC_DAI_NAME;
522 dai_link->cpu_dai_name = dev_name(dev);
523 dai_link->codec_name = dev_name(dev);
524 dai_link->platform_name = dev_name(dev);
525
526 card->dai_link = dai_link;
527 card->num_links = 1;
528 card->name = dd->pdata->card_name;
529 card->dev = dev;
530
531 return 0;
532}
533
534static void atmel_pdmic_get_sample_rate(struct atmel_pdmic *dd,
535 unsigned int *rate_min, unsigned int *rate_max)
536{
537 u32 mic_min_freq = dd->pdata->mic_min_freq;
538 u32 mic_max_freq = dd->pdata->mic_max_freq;
539 u32 clk_max_rate = (u32)(clk_get_rate(dd->pclk) >> 1);
540 u32 clk_min_rate = (u32)(clk_get_rate(dd->gclk) >> 8);
541
542 if (mic_max_freq > clk_max_rate)
543 mic_max_freq = clk_max_rate;
544
545 if (mic_min_freq < clk_min_rate)
546 mic_min_freq = clk_min_rate;
547
548 *rate_min = DIV_ROUND_CLOSEST(mic_min_freq, 128);
549 *rate_max = mic_max_freq >> 6;
550}
551
552
553static irqreturn_t atmel_pdmic_interrupt(int irq, void *dev_id)
554{
555 struct atmel_pdmic *dd = (struct atmel_pdmic *)dev_id;
556 u32 pdmic_isr;
557 irqreturn_t ret = IRQ_NONE;
558
559 regmap_read(dd->regmap, PDMIC_ISR, &pdmic_isr);
560
561 if (pdmic_isr & PDMIC_ISR_OVRE) {
562 regmap_update_bits(dd->regmap, PDMIC_CR, PDMIC_CR_ENPDM_MASK,
563 PDMIC_CR_ENPDM_DIS << PDMIC_CR_ENPDM_SHIFT);
564
565 snd_pcm_stop_xrun(dd->substream);
566
567 ret = IRQ_HANDLED;
568 }
569
570 return ret;
571}
572
573
574#define ATMEL_PDMIC_REG_MAX 0x124
575static const struct regmap_config atmel_pdmic_regmap_config = {
576 .reg_bits = 32,
577 .reg_stride = 4,
578 .val_bits = 32,
579 .max_register = ATMEL_PDMIC_REG_MAX,
580};
581
582static int atmel_pdmic_probe(struct platform_device *pdev)
583{
584 struct device *dev = &pdev->dev;
585 struct atmel_pdmic *dd;
586 struct resource *res;
587 void __iomem *io_base;
588 const struct atmel_pdmic_pdata *pdata;
589 struct snd_soc_card *card;
590 unsigned int rate_min, rate_max;
591 int ret;
592
593 pdata = atmel_pdmic_dt_init(dev);
594 if (IS_ERR(pdata))
595 return PTR_ERR(pdata);
596
597 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
598 if (!dd)
599 return -ENOMEM;
600
601 dd->pdata = pdata;
602 dd->dev = dev;
603
604 dd->irq = platform_get_irq(pdev, 0);
605 if (dd->irq < 0) {
606 ret = dd->irq;
607 dev_err(dev, "failed to get irq: %d\n", ret);
608 return ret;
609 }
610
611 dd->pclk = devm_clk_get(dev, "pclk");
612 if (IS_ERR(dd->pclk)) {
613 ret = PTR_ERR(dd->pclk);
614 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
615 return ret;
616 }
617
618 dd->gclk = devm_clk_get(dev, "gclk");
619 if (IS_ERR(dd->gclk)) {
620 ret = PTR_ERR(dd->gclk);
621 dev_err(dev, "failed to get GCK: %d\n", ret);
622 return ret;
623 }
624
625
626
627
628 ret = clk_set_rate(dd->gclk, clk_get_rate(dd->pclk)/3);
629 if (ret) {
630 dev_err(dev, "failed to set GCK clock rate: %d\n", ret);
631 return ret;
632 }
633
634 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
635 io_base = devm_ioremap_resource(dev, res);
636 if (IS_ERR(io_base))
637 return PTR_ERR(io_base);
638
639 dd->phy_base = res->start;
640
641 dd->regmap = devm_regmap_init_mmio(dev, io_base,
642 &atmel_pdmic_regmap_config);
643 if (IS_ERR(dd->regmap)) {
644 ret = PTR_ERR(dd->regmap);
645 dev_err(dev, "failed to init register map: %d\n", ret);
646 return ret;
647 }
648
649 ret = devm_request_irq(dev, dd->irq, atmel_pdmic_interrupt, 0,
650 "PDMIC", (void *)dd);
651 if (ret < 0) {
652 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
653 dd->irq, ret);
654 return ret;
655 }
656
657
658 atmel_pdmic_get_sample_rate(dd, &rate_min, &rate_max);
659
660
661 atmel_pdmic_cpu_dai.capture.rate_min = rate_min;
662 atmel_pdmic_cpu_dai.capture.rate_max = rate_max;
663 ret = devm_snd_soc_register_component(dev,
664 &atmel_pdmic_cpu_dai_component,
665 &atmel_pdmic_cpu_dai, 1);
666 if (ret) {
667 dev_err(dev, "could not register CPU DAI: %d\n", ret);
668 return ret;
669 }
670
671
672 ret = devm_snd_dmaengine_pcm_register(dev,
673 &atmel_pdmic_dmaengine_pcm_config,
674 0);
675 if (ret) {
676 dev_err(dev, "could not register platform: %d\n", ret);
677 return ret;
678 }
679
680
681 atmel_pdmic_codec_dai.capture.rate_min = rate_min;
682 atmel_pdmic_codec_dai.capture.rate_max = rate_max;
683 ret = devm_snd_soc_register_component(dev, &soc_component_dev_pdmic,
684 &atmel_pdmic_codec_dai, 1);
685 if (ret) {
686 dev_err(dev, "could not register component: %d\n", ret);
687 return ret;
688 }
689
690
691 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
692 if (!card) {
693 ret = -ENOMEM;
694 goto unregister_codec;
695 }
696
697 snd_soc_card_set_drvdata(card, dd);
698
699 ret = atmel_pdmic_asoc_card_init(dev, card);
700 if (ret) {
701 dev_err(dev, "failed to init sound card: %d\n", ret);
702 goto unregister_codec;
703 }
704
705 ret = devm_snd_soc_register_card(dev, card);
706 if (ret) {
707 dev_err(dev, "failed to register sound card: %d\n", ret);
708 goto unregister_codec;
709 }
710
711 return 0;
712
713unregister_codec:
714 return ret;
715}
716
717static int atmel_pdmic_remove(struct platform_device *pdev)
718{
719 return 0;
720}
721
722static struct platform_driver atmel_pdmic_driver = {
723 .driver = {
724 .name = "atmel-pdmic",
725 .of_match_table = of_match_ptr(atmel_pdmic_of_match),
726 .pm = &snd_soc_pm_ops,
727 },
728 .probe = atmel_pdmic_probe,
729 .remove = atmel_pdmic_remove,
730};
731module_platform_driver(atmel_pdmic_driver);
732
733MODULE_DESCRIPTION("Atmel PDMIC driver under ALSA SoC architecture");
734MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
735MODULE_LICENSE("GPL v2");
736