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