2 * ADAU1977/ADAU1978/ADAU1979 driver
4 * Copyright 2014 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2.
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/platform_data/adau1977.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
30 #define ADAU1977_REG_POWER 0x00
31 #define ADAU1977_REG_PLL 0x01
32 #define ADAU1977_REG_BOOST 0x02
33 #define ADAU1977_REG_MICBIAS 0x03
34 #define ADAU1977_REG_BLOCK_POWER_SAI 0x04
35 #define ADAU1977_REG_SAI_CTRL0 0x05
36 #define ADAU1977_REG_SAI_CTRL1 0x06
37 #define ADAU1977_REG_CMAP12 0x07
38 #define ADAU1977_REG_CMAP34 0x08
39 #define ADAU1977_REG_SAI_OVERTEMP 0x09
40 #define ADAU1977_REG_POST_ADC_GAIN(x) (0x0a + (x))
41 #define ADAU1977_REG_MISC_CONTROL 0x0e
42 #define ADAU1977_REG_DIAG_CONTROL 0x10
43 #define ADAU1977_REG_STATUS(x) (0x11 + (x))
44 #define ADAU1977_REG_DIAG_IRQ1 0x15
45 #define ADAU1977_REG_DIAG_IRQ2 0x16
46 #define ADAU1977_REG_ADJUST1 0x17
47 #define ADAU1977_REG_ADJUST2 0x18
48 #define ADAU1977_REG_ADC_CLIP 0x19
49 #define ADAU1977_REG_DC_HPF_CAL 0x1a
51 #define ADAU1977_POWER_RESET BIT(7)
52 #define ADAU1977_POWER_PWUP BIT(0)
54 #define ADAU1977_PLL_CLK_S BIT(4)
55 #define ADAU1977_PLL_MCS_MASK 0x7
57 #define ADAU1977_MICBIAS_MB_VOLTS_MASK 0xf0
58 #define ADAU1977_MICBIAS_MB_VOLTS_OFFSET 4
60 #define ADAU1977_BLOCK_POWER_SAI_LR_POL BIT(7)
61 #define ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE BIT(6)
62 #define ADAU1977_BLOCK_POWER_SAI_LDO_EN BIT(5)
64 #define ADAU1977_SAI_CTRL0_FMT_MASK (0x3 << 6)
65 #define ADAU1977_SAI_CTRL0_FMT_I2S (0x0 << 6)
66 #define ADAU1977_SAI_CTRL0_FMT_LJ (0x1 << 6)
67 #define ADAU1977_SAI_CTRL0_FMT_RJ_24BIT (0x2 << 6)
68 #define ADAU1977_SAI_CTRL0_FMT_RJ_16BIT (0x3 << 6)
70 #define ADAU1977_SAI_CTRL0_SAI_MASK (0x7 << 3)
71 #define ADAU1977_SAI_CTRL0_SAI_I2S (0x0 << 3)
72 #define ADAU1977_SAI_CTRL0_SAI_TDM_2 (0x1 << 3)
73 #define ADAU1977_SAI_CTRL0_SAI_TDM_4 (0x2 << 3)
74 #define ADAU1977_SAI_CTRL0_SAI_TDM_8 (0x3 << 3)
75 #define ADAU1977_SAI_CTRL0_SAI_TDM_16 (0x4 << 3)
77 #define ADAU1977_SAI_CTRL0_FS_MASK (0x7)
78 #define ADAU1977_SAI_CTRL0_FS_8000_12000 (0x0)
79 #define ADAU1977_SAI_CTRL0_FS_16000_24000 (0x1)
80 #define ADAU1977_SAI_CTRL0_FS_32000_48000 (0x2)
81 #define ADAU1977_SAI_CTRL0_FS_64000_96000 (0x3)
82 #define ADAU1977_SAI_CTRL0_FS_128000_192000 (0x4)
84 #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_MASK (0x3 << 5)
85 #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_32 (0x0 << 5)
86 #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_24 (0x1 << 5)
87 #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_16 (0x2 << 5)
88 #define ADAU1977_SAI_CTRL1_DATA_WIDTH_MASK (0x1 << 4)
89 #define ADAU1977_SAI_CTRL1_DATA_WIDTH_16BIT (0x1 << 4)
90 #define ADAU1977_SAI_CTRL1_DATA_WIDTH_24BIT (0x0 << 4)
91 #define ADAU1977_SAI_CTRL1_LRCLK_PULSE BIT(3)
92 #define ADAU1977_SAI_CTRL1_MSB BIT(2)
93 #define ADAU1977_SAI_CTRL1_BCLKRATE_16 (0x1 << 1)
94 #define ADAU1977_SAI_CTRL1_BCLKRATE_32 (0x0 << 1)
95 #define ADAU1977_SAI_CTRL1_BCLKRATE_MASK (0x1 << 1)
96 #define ADAU1977_SAI_CTRL1_MASTER BIT(0)
98 #define ADAU1977_SAI_OVERTEMP_DRV_C(x) BIT(4 + (x))
99 #define ADAU1977_SAI_OVERTEMP_DRV_HIZ BIT(3)
101 #define ADAU1977_MISC_CONTROL_SUM_MODE_MASK (0x3 << 6)
102 #define ADAU1977_MISC_CONTROL_SUM_MODE_1CH (0x2 << 6)
103 #define ADAU1977_MISC_CONTROL_SUM_MODE_2CH (0x1 << 6)
104 #define ADAU1977_MISC_CONTROL_SUM_MODE_4CH (0x0 << 6)
105 #define ADAU1977_MISC_CONTROL_MMUTE BIT(4)
106 #define ADAU1977_MISC_CONTROL_DC_CAL BIT(0)
108 #define ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET 4
109 #define ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET 0
112 struct regmap *regmap;
115 enum adau1977_sysclk_src sysclk_src;
116 struct gpio_desc *reset_gpio;
117 enum adau1977_type type;
119 struct regulator *avdd_reg;
120 struct regulator *dvdd_reg;
122 struct snd_pcm_hw_constraint_list constraints;
125 void (*switch_mode)(struct device *dev);
127 unsigned int max_master_fs;
128 unsigned int slot_width;
133 static const struct reg_default adau1977_reg_defaults[] = {
157 static const DECLARE_TLV_DB_MINMAX_MUTE(adau1977_adc_gain, -3562, 6000);
159 static const struct snd_soc_dapm_widget adau1977_micbias_dapm_widgets[] = {
160 SND_SOC_DAPM_SUPPLY("MICBIAS", ADAU1977_REG_MICBIAS,
164 static const struct snd_soc_dapm_widget adau1977_dapm_widgets[] = {
165 SND_SOC_DAPM_SUPPLY("Vref", ADAU1977_REG_BLOCK_POWER_SAI,
168 SND_SOC_DAPM_ADC("ADC1", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 0, 0),
169 SND_SOC_DAPM_ADC("ADC2", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 1, 0),
170 SND_SOC_DAPM_ADC("ADC3", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 2, 0),
171 SND_SOC_DAPM_ADC("ADC4", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 3, 0),
173 SND_SOC_DAPM_INPUT("AIN1"),
174 SND_SOC_DAPM_INPUT("AIN2"),
175 SND_SOC_DAPM_INPUT("AIN3"),
176 SND_SOC_DAPM_INPUT("AIN4"),
178 SND_SOC_DAPM_OUTPUT("VREF"),
181 static const struct snd_soc_dapm_route adau1977_dapm_routes[] = {
182 { "ADC1", NULL, "AIN1" },
183 { "ADC2", NULL, "AIN2" },
184 { "ADC3", NULL, "AIN3" },
185 { "ADC4", NULL, "AIN4" },
187 { "ADC1", NULL, "Vref" },
188 { "ADC2", NULL, "Vref" },
189 { "ADC3", NULL, "Vref" },
190 { "ADC4", NULL, "Vref" },
192 { "VREF", NULL, "Vref" },
195 #define ADAU1977_VOLUME(x) \
196 SOC_SINGLE_TLV("ADC" #x " Capture Volume", \
197 ADAU1977_REG_POST_ADC_GAIN((x) - 1), \
198 0, 255, 1, adau1977_adc_gain)
200 #define ADAU1977_HPF_SWITCH(x) \
201 SOC_SINGLE("ADC" #x " Highpass-Filter Capture Switch", \
202 ADAU1977_REG_DC_HPF_CAL, (x) - 1, 1, 0)
204 #define ADAU1977_DC_SUB_SWITCH(x) \
205 SOC_SINGLE("ADC" #x " DC Subtraction Capture Switch", \
206 ADAU1977_REG_DC_HPF_CAL, (x) + 3, 1, 0)
208 static const struct snd_kcontrol_new adau1977_snd_controls[] = {
214 ADAU1977_HPF_SWITCH(1),
215 ADAU1977_HPF_SWITCH(2),
216 ADAU1977_HPF_SWITCH(3),
217 ADAU1977_HPF_SWITCH(4),
219 ADAU1977_DC_SUB_SWITCH(1),
220 ADAU1977_DC_SUB_SWITCH(2),
221 ADAU1977_DC_SUB_SWITCH(3),
222 ADAU1977_DC_SUB_SWITCH(4),
225 static int adau1977_reset(struct adau1977 *adau1977)
230 * The reset bit is obviously volatile, but we need to be able to cache
231 * the other bits in the register, so we can't just mark the whole
232 * register as volatile. Since this is the only place where we'll ever
233 * touch the reset bit just bypass the cache for this operation.
235 regcache_cache_bypass(adau1977->regmap, true);
236 ret = regmap_write(adau1977->regmap, ADAU1977_REG_POWER,
237 ADAU1977_POWER_RESET);
238 regcache_cache_bypass(adau1977->regmap, false);
246 * Returns the appropriate setting for ths FS field in the CTRL0 register
247 * depending on the rate.
249 static int adau1977_lookup_fs(unsigned int rate)
251 if (rate >= 8000 && rate <= 12000)
252 return ADAU1977_SAI_CTRL0_FS_8000_12000;
253 else if (rate >= 16000 && rate <= 24000)
254 return ADAU1977_SAI_CTRL0_FS_16000_24000;
255 else if (rate >= 32000 && rate <= 48000)
256 return ADAU1977_SAI_CTRL0_FS_32000_48000;
257 else if (rate >= 64000 && rate <= 96000)
258 return ADAU1977_SAI_CTRL0_FS_64000_96000;
259 else if (rate >= 128000 && rate <= 192000)
260 return ADAU1977_SAI_CTRL0_FS_128000_192000;
265 static int adau1977_lookup_mcs(struct adau1977 *adau1977, unsigned int rate,
271 * rate = sysclk / (512 * mcs_lut[mcs]) * 2**fs
272 * => mcs_lut[mcs] = sysclk / (512 * rate) * 2**fs
273 * => mcs_lut[mcs] = sysclk / ((512 / 2**fs) * rate)
278 if (adau1977->sysclk % rate != 0)
281 mcs = adau1977->sysclk / rate;
283 /* The factors configured by MCS are 1, 2, 3, 4, 6 */
284 if (mcs < 1 || mcs > 6 || mcs == 5)
294 static int adau1977_hw_params(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
297 struct snd_soc_codec *codec = dai->codec;
298 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
299 unsigned int rate = params_rate(params);
300 unsigned int slot_width;
301 unsigned int ctrl0, ctrl0_mask;
306 fs = adau1977_lookup_fs(rate);
310 if (adau1977->sysclk_src == ADAU1977_SYSCLK_SRC_MCLK) {
311 mcs = adau1977_lookup_mcs(adau1977, rate, fs);
318 ctrl0_mask = ADAU1977_SAI_CTRL0_FS_MASK;
321 if (adau1977->right_j) {
322 switch (params_width(params)) {
324 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_16BIT;
327 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_24BIT;
332 ctrl0_mask |= ADAU1977_SAI_CTRL0_FMT_MASK;
335 if (adau1977->master) {
336 switch (params_width(params)) {
338 ctrl1 = ADAU1977_SAI_CTRL1_DATA_WIDTH_16BIT;
343 ctrl1 = ADAU1977_SAI_CTRL1_DATA_WIDTH_24BIT;
350 /* In TDM mode there is a fixed slot width */
351 if (adau1977->slot_width)
352 slot_width = adau1977->slot_width;
354 if (slot_width == 16)
355 ctrl1 |= ADAU1977_SAI_CTRL1_BCLKRATE_16;
357 ctrl1 |= ADAU1977_SAI_CTRL1_BCLKRATE_32;
359 ret = regmap_update_bits(adau1977->regmap,
360 ADAU1977_REG_SAI_CTRL1,
361 ADAU1977_SAI_CTRL1_DATA_WIDTH_MASK |
362 ADAU1977_SAI_CTRL1_BCLKRATE_MASK,
368 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
373 return regmap_update_bits(adau1977->regmap, ADAU1977_REG_PLL,
374 ADAU1977_PLL_MCS_MASK, mcs);
377 static int adau1977_power_disable(struct adau1977 *adau1977)
381 if (!adau1977->enabled)
384 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_POWER,
385 ADAU1977_POWER_PWUP, 0);
389 regcache_mark_dirty(adau1977->regmap);
391 if (adau1977->reset_gpio)
392 gpiod_set_value_cansleep(adau1977->reset_gpio, 0);
394 regcache_cache_only(adau1977->regmap, true);
396 regulator_disable(adau1977->avdd_reg);
397 if (adau1977->dvdd_reg)
398 regulator_disable(adau1977->dvdd_reg);
400 adau1977->enabled = false;
405 static int adau1977_power_enable(struct adau1977 *adau1977)
410 if (adau1977->enabled)
413 ret = regulator_enable(adau1977->avdd_reg);
417 if (adau1977->dvdd_reg) {
418 ret = regulator_enable(adau1977->dvdd_reg);
420 goto err_disable_avdd;
423 if (adau1977->reset_gpio)
424 gpiod_set_value_cansleep(adau1977->reset_gpio, 1);
426 regcache_cache_only(adau1977->regmap, false);
428 if (adau1977->switch_mode)
429 adau1977->switch_mode(adau1977->dev);
431 ret = adau1977_reset(adau1977);
433 goto err_disable_dvdd;
435 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_POWER,
436 ADAU1977_POWER_PWUP, ADAU1977_POWER_PWUP);
438 goto err_disable_dvdd;
440 ret = regcache_sync(adau1977->regmap);
442 goto err_disable_dvdd;
445 * The PLL register is not affected by the software reset. It is
446 * possible that the value of the register was changed to the
447 * default value while we were in cache only mode. In this case
448 * regcache_sync will skip over it and we have to manually sync
451 ret = regmap_read(adau1977->regmap, ADAU1977_REG_PLL, &val);
453 goto err_disable_dvdd;
456 regcache_cache_bypass(adau1977->regmap, true);
457 ret = regmap_write(adau1977->regmap, ADAU1977_REG_PLL,
460 goto err_disable_dvdd;
461 regcache_cache_bypass(adau1977->regmap, false);
464 adau1977->enabled = true;
469 if (adau1977->dvdd_reg)
470 regulator_disable(adau1977->dvdd_reg);
472 regulator_disable(adau1977->avdd_reg);
476 static int adau1977_set_bias_level(struct snd_soc_codec *codec,
477 enum snd_soc_bias_level level)
479 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
483 case SND_SOC_BIAS_ON:
485 case SND_SOC_BIAS_PREPARE:
487 case SND_SOC_BIAS_STANDBY:
488 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
489 ret = adau1977_power_enable(adau1977);
491 case SND_SOC_BIAS_OFF:
492 ret = adau1977_power_disable(adau1977);
499 static int adau1977_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
500 unsigned int rx_mask, int slots, int width)
502 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
503 unsigned int ctrl0, ctrl1, drv;
504 unsigned int slot[4];
509 /* 0 = No fixed slot width */
510 adau1977->slot_width = 0;
511 adau1977->max_master_fs = 192000;
512 return regmap_update_bits(adau1977->regmap,
513 ADAU1977_REG_SAI_CTRL0, ADAU1977_SAI_CTRL0_SAI_MASK,
514 ADAU1977_SAI_CTRL0_SAI_I2S);
517 if (rx_mask == 0 || tx_mask != 0)
521 for (i = 0; i < 4; i++) {
522 slot[i] = __ffs(rx_mask);
523 drv |= ADAU1977_SAI_OVERTEMP_DRV_C(i);
524 rx_mask &= ~(1 << slot[i]);
525 if (slot[i] >= slots)
536 ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_16;
539 /* We can only generate 16 bit or 32 bit wide slots */
540 if (adau1977->master)
542 ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_24;
545 ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_32;
553 ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_2;
556 ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_4;
559 ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_8;
562 ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_16;
568 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
569 ADAU1977_SAI_OVERTEMP_DRV_C(0) |
570 ADAU1977_SAI_OVERTEMP_DRV_C(1) |
571 ADAU1977_SAI_OVERTEMP_DRV_C(2) |
572 ADAU1977_SAI_OVERTEMP_DRV_C(3), drv);
576 ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP12,
577 (slot[1] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
578 (slot[0] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
582 ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP34,
583 (slot[3] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
584 (slot[2] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
588 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
589 ADAU1977_SAI_CTRL0_SAI_MASK, ctrl0);
593 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
594 ADAU1977_SAI_CTRL1_SLOT_WIDTH_MASK, ctrl1);
598 adau1977->slot_width = width;
600 /* In master mode the maximum bitclock is 24.576 MHz */
601 adau1977->max_master_fs = min(192000, 24576000 / width / slots);
606 static int adau1977_mute(struct snd_soc_dai *dai, int mute, int stream)
608 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
612 val = ADAU1977_MISC_CONTROL_MMUTE;
616 return regmap_update_bits(adau1977->regmap, ADAU1977_REG_MISC_CONTROL,
617 ADAU1977_MISC_CONTROL_MMUTE, val);
620 static int adau1977_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
622 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
623 unsigned int ctrl0 = 0, ctrl1 = 0, block_power = 0;
627 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
628 case SND_SOC_DAIFMT_CBS_CFS:
629 adau1977->master = false;
631 case SND_SOC_DAIFMT_CBM_CFM:
632 ctrl1 |= ADAU1977_SAI_CTRL1_MASTER;
633 adau1977->master = true;
639 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
640 case SND_SOC_DAIFMT_NB_NF:
641 invert_lrclk = false;
643 case SND_SOC_DAIFMT_IB_NF:
644 block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
645 invert_lrclk = false;
647 case SND_SOC_DAIFMT_NB_IF:
650 case SND_SOC_DAIFMT_IB_IF:
651 block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
658 adau1977->right_j = false;
659 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
660 case SND_SOC_DAIFMT_I2S:
661 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
663 case SND_SOC_DAIFMT_LEFT_J:
664 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
665 invert_lrclk = !invert_lrclk;
667 case SND_SOC_DAIFMT_RIGHT_J:
668 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_24BIT;
669 adau1977->right_j = true;
670 invert_lrclk = !invert_lrclk;
672 case SND_SOC_DAIFMT_DSP_A:
673 ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
674 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
675 invert_lrclk = false;
677 case SND_SOC_DAIFMT_DSP_B:
678 ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
679 ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
680 invert_lrclk = false;
687 block_power |= ADAU1977_BLOCK_POWER_SAI_LR_POL;
689 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
690 ADAU1977_BLOCK_POWER_SAI_LR_POL |
691 ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE, block_power);
695 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
696 ADAU1977_SAI_CTRL0_FMT_MASK,
701 return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
702 ADAU1977_SAI_CTRL1_MASTER | ADAU1977_SAI_CTRL1_LRCLK_PULSE,
706 static int adau1977_startup(struct snd_pcm_substream *substream,
707 struct snd_soc_dai *dai)
709 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
712 if (adau1977->slot_width == 16)
713 formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE;
714 else if (adau1977->right_j || adau1977->slot_width == 24)
715 formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
716 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE;
718 snd_pcm_hw_constraint_list(substream->runtime, 0,
719 SNDRV_PCM_HW_PARAM_RATE, &adau1977->constraints);
721 if (adau1977->master)
722 snd_pcm_hw_constraint_minmax(substream->runtime,
723 SNDRV_PCM_HW_PARAM_RATE, 8000, adau1977->max_master_fs);
726 snd_pcm_hw_constraint_mask64(substream->runtime,
727 SNDRV_PCM_HW_PARAM_FORMAT, formats);
732 static int adau1977_set_tristate(struct snd_soc_dai *dai, int tristate)
734 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
738 val = ADAU1977_SAI_OVERTEMP_DRV_HIZ;
742 return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
743 ADAU1977_SAI_OVERTEMP_DRV_HIZ, val);
746 static const struct snd_soc_dai_ops adau1977_dai_ops = {
747 .startup = adau1977_startup,
748 .hw_params = adau1977_hw_params,
749 .mute_stream = adau1977_mute,
750 .set_fmt = adau1977_set_dai_fmt,
751 .set_tdm_slot = adau1977_set_tdm_slot,
752 .set_tristate = adau1977_set_tristate,
755 static struct snd_soc_dai_driver adau1977_dai = {
756 .name = "adau1977-hifi",
758 .stream_name = "Capture",
761 .rates = SNDRV_PCM_RATE_KNOT,
762 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
763 SNDRV_PCM_FMTBIT_S32_LE,
766 .ops = &adau1977_dai_ops,
769 static const unsigned int adau1977_rates[] = {
770 8000, 16000, 32000, 64000, 128000,
771 11025, 22050, 44100, 88200, 172400,
772 12000, 24000, 48000, 96000, 192000,
775 #define ADAU1977_RATE_CONSTRAINT_MASK_32000 0x001f
776 #define ADAU1977_RATE_CONSTRAINT_MASK_44100 0x03e0
777 #define ADAU1977_RATE_CONSTRAINT_MASK_48000 0x7c00
778 /* All rates >= 32000 */
779 #define ADAU1977_RATE_CONSTRAINT_MASK_LRCLK 0x739c
781 static bool adau1977_check_sysclk(unsigned int mclk, unsigned int base_freq)
785 if (mclk % (base_freq * 128) != 0)
788 mcs = mclk / (128 * base_freq);
789 if (mcs < 1 || mcs > 6 || mcs == 5)
795 static int adau1977_set_sysclk(struct snd_soc_codec *codec,
796 int clk_id, int source, unsigned int freq, int dir)
798 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
799 unsigned int mask = 0;
800 unsigned int clk_src;
803 if (dir != SND_SOC_CLOCK_IN)
806 if (clk_id != ADAU1977_SYSCLK)
810 case ADAU1977_SYSCLK_SRC_MCLK:
813 case ADAU1977_SYSCLK_SRC_LRCLK:
814 clk_src = ADAU1977_PLL_CLK_S;
820 if (freq != 0 && source == ADAU1977_SYSCLK_SRC_MCLK) {
821 if (freq < 4000000 || freq > 36864000)
824 if (adau1977_check_sysclk(freq, 32000))
825 mask |= ADAU1977_RATE_CONSTRAINT_MASK_32000;
826 if (adau1977_check_sysclk(freq, 44100))
827 mask |= ADAU1977_RATE_CONSTRAINT_MASK_44100;
828 if (adau1977_check_sysclk(freq, 48000))
829 mask |= ADAU1977_RATE_CONSTRAINT_MASK_48000;
833 } else if (source == ADAU1977_SYSCLK_SRC_LRCLK) {
834 mask = ADAU1977_RATE_CONSTRAINT_MASK_LRCLK;
837 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_PLL,
838 ADAU1977_PLL_CLK_S, clk_src);
842 adau1977->constraints.mask = mask;
843 adau1977->sysclk_src = source;
844 adau1977->sysclk = freq;
849 static int adau1977_codec_probe(struct snd_soc_codec *codec)
851 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
852 struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
855 switch (adau1977->type) {
857 ret = snd_soc_dapm_new_controls(dapm,
858 adau1977_micbias_dapm_widgets,
859 ARRAY_SIZE(adau1977_micbias_dapm_widgets));
870 static struct snd_soc_codec_driver adau1977_codec_driver = {
871 .probe = adau1977_codec_probe,
872 .set_bias_level = adau1977_set_bias_level,
873 .set_sysclk = adau1977_set_sysclk,
874 .idle_bias_off = true,
876 .component_driver = {
877 .controls = adau1977_snd_controls,
878 .num_controls = ARRAY_SIZE(adau1977_snd_controls),
879 .dapm_widgets = adau1977_dapm_widgets,
880 .num_dapm_widgets = ARRAY_SIZE(adau1977_dapm_widgets),
881 .dapm_routes = adau1977_dapm_routes,
882 .num_dapm_routes = ARRAY_SIZE(adau1977_dapm_routes),
886 static int adau1977_setup_micbias(struct adau1977 *adau1977)
888 struct adau1977_platform_data *pdata = adau1977->dev->platform_data;
889 unsigned int micbias;
892 micbias = pdata->micbias;
893 if (micbias > ADAU1977_MICBIAS_9V0)
897 micbias = ADAU1977_MICBIAS_8V5;
900 return regmap_update_bits(adau1977->regmap, ADAU1977_REG_MICBIAS,
901 ADAU1977_MICBIAS_MB_VOLTS_MASK,
902 micbias << ADAU1977_MICBIAS_MB_VOLTS_OFFSET);
905 int adau1977_probe(struct device *dev, struct regmap *regmap,
906 enum adau1977_type type, void (*switch_mode)(struct device *dev))
908 unsigned int power_off_mask;
909 struct adau1977 *adau1977;
913 return PTR_ERR(regmap);
915 adau1977 = devm_kzalloc(dev, sizeof(*adau1977), GFP_KERNEL);
916 if (adau1977 == NULL)
920 adau1977->type = type;
921 adau1977->regmap = regmap;
922 adau1977->switch_mode = switch_mode;
923 adau1977->max_master_fs = 192000;
925 adau1977->constraints.list = adau1977_rates;
926 adau1977->constraints.count = ARRAY_SIZE(adau1977_rates);
928 adau1977->avdd_reg = devm_regulator_get(dev, "AVDD");
929 if (IS_ERR(adau1977->avdd_reg))
930 return PTR_ERR(adau1977->avdd_reg);
932 adau1977->dvdd_reg = devm_regulator_get_optional(dev, "DVDD");
933 if (IS_ERR(adau1977->dvdd_reg)) {
934 if (PTR_ERR(adau1977->dvdd_reg) != -ENODEV)
935 return PTR_ERR(adau1977->dvdd_reg);
936 adau1977->dvdd_reg = NULL;
939 adau1977->reset_gpio = devm_gpiod_get_optional(dev, "reset",
941 if (IS_ERR(adau1977->reset_gpio))
942 return PTR_ERR(adau1977->reset_gpio);
944 dev_set_drvdata(dev, adau1977);
946 if (adau1977->reset_gpio)
949 ret = adau1977_power_enable(adau1977);
953 if (type == ADAU1977) {
954 ret = adau1977_setup_micbias(adau1977);
959 if (adau1977->dvdd_reg)
962 power_off_mask = (unsigned int)~ADAU1977_BLOCK_POWER_SAI_LDO_EN;
964 ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
965 power_off_mask, 0x00);
969 ret = adau1977_power_disable(adau1977);
973 return snd_soc_register_codec(dev, &adau1977_codec_driver,
977 adau1977_power_disable(adau1977);
981 EXPORT_SYMBOL_GPL(adau1977_probe);
983 static bool adau1977_register_volatile(struct device *dev, unsigned int reg)
986 case ADAU1977_REG_STATUS(0):
987 case ADAU1977_REG_STATUS(1):
988 case ADAU1977_REG_STATUS(2):
989 case ADAU1977_REG_STATUS(3):
990 case ADAU1977_REG_ADC_CLIP:
997 const struct regmap_config adau1977_regmap_config = {
998 .max_register = ADAU1977_REG_DC_HPF_CAL,
999 .volatile_reg = adau1977_register_volatile,
1001 .cache_type = REGCACHE_RBTREE,
1002 .reg_defaults = adau1977_reg_defaults,
1003 .num_reg_defaults = ARRAY_SIZE(adau1977_reg_defaults),
1005 EXPORT_SYMBOL_GPL(adau1977_regmap_config);
1007 MODULE_DESCRIPTION("ASoC ADAU1977/ADAU1978/ADAU1979 driver");
1008 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1009 MODULE_LICENSE("GPL");