Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * AD193X Audio Codec driver supporting AD1936/7/8/9 |
| 3 | * |
| 4 | * Copyright 2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/device.h> |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 12 | #include <linux/regmap.h> |
Stephen Rothwell | 1b132ea | 2010-03-29 15:32:18 +1100 | [diff] [blame] | 13 | #include <linux/slab.h> |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 14 | #include <sound/core.h> |
| 15 | #include <sound/pcm.h> |
| 16 | #include <sound/pcm_params.h> |
| 17 | #include <sound/initval.h> |
| 18 | #include <sound/soc.h> |
| 19 | #include <sound/tlv.h> |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 20 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 21 | #include "ad193x.h" |
| 22 | |
| 23 | /* codec private data */ |
| 24 | struct ad193x_priv { |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 25 | struct regmap *regmap; |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 26 | enum ad193x_type type; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 27 | int sysclk; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 30 | /* |
| 31 | * AD193X volume/mute/de-emphasis etc. controls |
| 32 | */ |
Lars-Peter Clausen | c4e7a4a | 2011-11-28 17:28:08 +0100 | [diff] [blame] | 33 | static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 34 | |
Takashi Iwai | b6592d8 | 2014-02-18 09:55:27 +0100 | [diff] [blame] | 35 | static SOC_ENUM_SINGLE_DECL(ad193x_deemp_enum, AD193X_DAC_CTRL2, 1, |
| 36 | ad193x_deemp); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 37 | |
Lars-Peter Clausen | 591c034 | 2011-11-28 17:28:07 +0100 | [diff] [blame] | 38 | static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0); |
| 39 | |
Codrin Ciubotariu | 75c2ecb | 2019-02-18 16:10:30 +0000 | [diff] [blame] | 40 | static const unsigned int ad193x_sb[] = {32}; |
| 41 | |
| 42 | static struct snd_pcm_hw_constraint_list constr = { |
| 43 | .list = ad193x_sb, |
| 44 | .count = ARRAY_SIZE(ad193x_sb), |
| 45 | }; |
| 46 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 47 | static const struct snd_kcontrol_new ad193x_snd_controls[] = { |
| 48 | /* DAC volume control */ |
Lars-Peter Clausen | 591c034 | 2011-11-28 17:28:07 +0100 | [diff] [blame] | 49 | SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL, |
| 50 | AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv), |
| 51 | SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL, |
| 52 | AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv), |
| 53 | SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL, |
| 54 | AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv), |
| 55 | SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL, |
| 56 | AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 57 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 58 | /* DAC switch control */ |
| 59 | SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE, |
| 60 | AD193X_DACR1_MUTE, 1, 1), |
| 61 | SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE, |
| 62 | AD193X_DACR2_MUTE, 1, 1), |
| 63 | SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE, |
| 64 | AD193X_DACR3_MUTE, 1, 1), |
| 65 | SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE, |
| 66 | AD193X_DACR4_MUTE, 1, 1), |
| 67 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 68 | /* DAC de-emphasis */ |
| 69 | SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum), |
| 70 | }; |
| 71 | |
| 72 | static const struct snd_kcontrol_new ad193x_adc_snd_controls[] = { |
| 73 | /* ADC switch control */ |
| 74 | SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE, |
| 75 | AD193X_ADCR1_MUTE, 1, 1), |
| 76 | SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE, |
| 77 | AD193X_ADCR2_MUTE, 1, 1), |
| 78 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 79 | /* ADC high-pass filter */ |
| 80 | SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0, |
| 81 | AD193X_ADC_HIGHPASS_FILTER, 1, 0), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = { |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 85 | SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0), |
| 86 | SND_SOC_DAPM_PGA("DAC Output", AD193X_DAC_CTRL0, 0, 1, NULL, 0), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 87 | SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0), |
Lars-Peter Clausen | 0718fd2 | 2011-11-28 17:28:10 +0100 | [diff] [blame] | 88 | SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0), |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 89 | SND_SOC_DAPM_VMID("VMID"), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 90 | SND_SOC_DAPM_OUTPUT("DAC1OUT"), |
| 91 | SND_SOC_DAPM_OUTPUT("DAC2OUT"), |
| 92 | SND_SOC_DAPM_OUTPUT("DAC3OUT"), |
| 93 | SND_SOC_DAPM_OUTPUT("DAC4OUT"), |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static const struct snd_soc_dapm_widget ad193x_adc_widgets[] = { |
| 97 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
| 98 | SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 99 | SND_SOC_DAPM_INPUT("ADC1IN"), |
| 100 | SND_SOC_DAPM_INPUT("ADC2IN"), |
| 101 | }; |
| 102 | |
Codrin Ciubotariu | 5952947 | 2019-02-18 16:10:36 +0000 | [diff] [blame] | 103 | static int ad193x_check_pll(struct snd_soc_dapm_widget *source, |
| 104 | struct snd_soc_dapm_widget *sink) |
| 105 | { |
| 106 | struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm); |
| 107 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
| 108 | |
| 109 | return !!ad193x->sysclk; |
| 110 | } |
| 111 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 112 | static const struct snd_soc_dapm_route audio_paths[] = { |
Lars-Peter Clausen | 0718fd2 | 2011-11-28 17:28:10 +0100 | [diff] [blame] | 113 | { "DAC", NULL, "SYSCLK" }, |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 114 | { "DAC Output", NULL, "DAC" }, |
| 115 | { "DAC Output", NULL, "VMID" }, |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 116 | { "DAC1OUT", NULL, "DAC Output" }, |
| 117 | { "DAC2OUT", NULL, "DAC Output" }, |
| 118 | { "DAC3OUT", NULL, "DAC Output" }, |
| 119 | { "DAC4OUT", NULL, "DAC Output" }, |
Codrin Ciubotariu | 5952947 | 2019-02-18 16:10:36 +0000 | [diff] [blame] | 120 | { "SYSCLK", NULL, "PLL_PWR", &ad193x_check_pll }, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 121 | }; |
| 122 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 123 | static const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = { |
| 124 | { "ADC", NULL, "SYSCLK" }, |
| 125 | { "ADC", NULL, "ADC_PWR" }, |
| 126 | { "ADC", NULL, "ADC1IN" }, |
| 127 | { "ADC", NULL, "ADC2IN" }, |
| 128 | }; |
| 129 | |
| 130 | static inline bool ad193x_has_adc(const struct ad193x_priv *ad193x) |
| 131 | { |
| 132 | switch (ad193x->type) { |
| 133 | case AD1933: |
| 134 | case AD1934: |
| 135 | return false; |
| 136 | default: |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 143 | /* |
| 144 | * DAI ops entries |
| 145 | */ |
| 146 | |
| 147 | static int ad193x_mute(struct snd_soc_dai *dai, int mute) |
| 148 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 149 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 150 | |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 151 | if (mute) |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 152 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 153 | AD193X_DAC_MASTER_MUTE, |
| 154 | AD193X_DAC_MASTER_MUTE); |
| 155 | else |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 156 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 157 | AD193X_DAC_MASTER_MUTE, 0); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, |
| 163 | unsigned int rx_mask, int slots, int width) |
| 164 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 165 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 166 | unsigned int channels; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 167 | |
| 168 | switch (slots) { |
| 169 | case 2: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 170 | channels = AD193X_2_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 171 | break; |
| 172 | case 4: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 173 | channels = AD193X_4_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 174 | break; |
| 175 | case 8: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 176 | channels = AD193X_8_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 177 | break; |
| 178 | case 16: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 179 | channels = AD193X_16_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 180 | break; |
| 181 | default: |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 185 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, |
| 186 | AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 187 | if (ad193x_has_adc(ad193x)) |
| 188 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2, |
| 189 | AD193X_ADC_CHAN_MASK, |
| 190 | channels << AD193X_ADC_CHAN_SHFT); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 196 | unsigned int fmt) |
| 197 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 198 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component); |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 199 | unsigned int adc_serfmt = 0; |
Codrin Ciubotariu | bccf9c7 | 2019-02-18 16:10:34 +0000 | [diff] [blame] | 200 | unsigned int dac_serfmt = 0; |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 201 | unsigned int adc_fmt = 0; |
| 202 | unsigned int dac_fmt = 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 203 | |
| 204 | /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S |
Codrin Ciubotariu | bccf9c7 | 2019-02-18 16:10:34 +0000 | [diff] [blame] | 205 | * with TDM), ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) and DAC I2S mode |
| 206 | * (SND_SOC_DAIFMT_I2S) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 207 | */ |
| 208 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 209 | case SND_SOC_DAIFMT_I2S: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 210 | adc_serfmt |= AD193X_ADC_SERFMT_TDM; |
Codrin Ciubotariu | bccf9c7 | 2019-02-18 16:10:34 +0000 | [diff] [blame] | 211 | dac_serfmt |= AD193X_DAC_SERFMT_STEREO; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 212 | break; |
| 213 | case SND_SOC_DAIFMT_DSP_A: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 214 | adc_serfmt |= AD193X_ADC_SERFMT_AUX; |
Codrin Ciubotariu | bccf9c7 | 2019-02-18 16:10:34 +0000 | [diff] [blame] | 215 | dac_serfmt |= AD193X_DAC_SERFMT_TDM; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 216 | break; |
| 217 | default: |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 218 | if (ad193x_has_adc(ad193x)) |
| 219 | return -EINVAL; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 223 | case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 224 | break; |
| 225 | case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 226 | adc_fmt |= AD193X_ADC_LEFT_HIGH; |
| 227 | dac_fmt |= AD193X_DAC_LEFT_HIGH; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 228 | break; |
| 229 | case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 230 | adc_fmt |= AD193X_ADC_BCLK_INV; |
| 231 | dac_fmt |= AD193X_DAC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 232 | break; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 233 | case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 234 | adc_fmt |= AD193X_ADC_LEFT_HIGH; |
| 235 | adc_fmt |= AD193X_ADC_BCLK_INV; |
| 236 | dac_fmt |= AD193X_DAC_LEFT_HIGH; |
| 237 | dac_fmt |= AD193X_DAC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
Codrin Ciubotariu | 90f6e68 | 2019-02-18 16:10:32 +0000 | [diff] [blame] | 243 | /* For DSP_*, LRCLK's polarity must be inverted */ |
Codrin Ciubotariu | da7260c | 2019-06-26 13:49:46 +0300 | [diff] [blame^] | 244 | if (fmt & SND_SOC_DAIFMT_DSP_A) |
| 245 | dac_fmt ^= AD193X_DAC_LEFT_HIGH; |
Codrin Ciubotariu | 90f6e68 | 2019-02-18 16:10:32 +0000 | [diff] [blame] | 246 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 247 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 248 | case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 249 | adc_fmt |= AD193X_ADC_LCR_MASTER; |
| 250 | adc_fmt |= AD193X_ADC_BCLK_MASTER; |
| 251 | dac_fmt |= AD193X_DAC_LCR_MASTER; |
| 252 | dac_fmt |= AD193X_DAC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 253 | break; |
| 254 | case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 255 | adc_fmt |= AD193X_ADC_LCR_MASTER; |
| 256 | dac_fmt |= AD193X_DAC_LCR_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 257 | break; |
| 258 | case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 259 | adc_fmt |= AD193X_ADC_BCLK_MASTER; |
| 260 | dac_fmt |= AD193X_DAC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 261 | break; |
| 262 | case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 263 | break; |
| 264 | default: |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 268 | if (ad193x_has_adc(ad193x)) { |
| 269 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, |
| 270 | AD193X_ADC_SERFMT_MASK, adc_serfmt); |
| 271 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2, |
| 272 | AD193X_ADC_FMT_MASK, adc_fmt); |
| 273 | } |
Codrin Ciubotariu | bccf9c7 | 2019-02-18 16:10:34 +0000 | [diff] [blame] | 274 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL0, |
| 275 | AD193X_DAC_SERFMT_MASK, dac_serfmt); |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 276 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, |
| 277 | AD193X_DAC_FMT_MASK, dac_fmt); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 282 | static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 283 | int clk_id, unsigned int freq, int dir) |
| 284 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 285 | struct snd_soc_component *component = codec_dai->component; |
Codrin Ciubotariu | 5952947 | 2019-02-18 16:10:36 +0000 | [diff] [blame] | 286 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 287 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
Codrin Ciubotariu | 5952947 | 2019-02-18 16:10:36 +0000 | [diff] [blame] | 288 | |
| 289 | if (clk_id == AD193X_SYSCLK_MCLK) { |
| 290 | /* MCLK must be 512 x fs */ |
| 291 | if (dir == SND_SOC_CLOCK_OUT || freq != 24576000) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL1, |
| 295 | AD193X_PLL_SRC_MASK, |
| 296 | AD193X_PLL_DAC_SRC_MCLK | |
| 297 | AD193X_PLL_CLK_SRC_MCLK); |
| 298 | |
| 299 | snd_soc_dapm_sync(dapm); |
| 300 | return 0; |
| 301 | } |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 302 | switch (freq) { |
| 303 | case 12288000: |
| 304 | case 18432000: |
| 305 | case 24576000: |
| 306 | case 36864000: |
| 307 | ad193x->sysclk = freq; |
| 308 | return 0; |
| 309 | } |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 313 | static int ad193x_hw_params(struct snd_pcm_substream *substream, |
| 314 | struct snd_pcm_hw_params *params, |
| 315 | struct snd_soc_dai *dai) |
| 316 | { |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 317 | int word_len = 0, master_rate = 0; |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 318 | struct snd_soc_component *component = dai->component; |
| 319 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 320 | |
| 321 | /* bit size */ |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 322 | switch (params_width(params)) { |
| 323 | case 16: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 324 | word_len = 3; |
| 325 | break; |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 326 | case 20: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 327 | word_len = 1; |
| 328 | break; |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 329 | case 24: |
| 330 | case 32: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 331 | word_len = 0; |
| 332 | break; |
| 333 | } |
| 334 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 335 | switch (ad193x->sysclk) { |
| 336 | case 12288000: |
| 337 | master_rate = AD193X_PLL_INPUT_256; |
| 338 | break; |
| 339 | case 18432000: |
| 340 | master_rate = AD193X_PLL_INPUT_384; |
| 341 | break; |
| 342 | case 24576000: |
| 343 | master_rate = AD193X_PLL_INPUT_512; |
| 344 | break; |
| 345 | case 36864000: |
| 346 | master_rate = AD193X_PLL_INPUT_768; |
| 347 | break; |
| 348 | } |
| 349 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 350 | regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 351 | AD193X_PLL_INPUT_MASK, master_rate); |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 352 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 353 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 354 | AD193X_DAC_WORD_LEN_MASK, |
| 355 | word_len << AD193X_DAC_WORD_LEN_SHFT); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 356 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 357 | if (ad193x_has_adc(ad193x)) |
| 358 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, |
| 359 | AD193X_ADC_WORD_LEN_MASK, word_len); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Codrin Ciubotariu | 75c2ecb | 2019-02-18 16:10:30 +0000 | [diff] [blame] | 364 | static int ad193x_startup(struct snd_pcm_substream *substream, |
| 365 | struct snd_soc_dai *dai) |
| 366 | { |
| 367 | return snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 368 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 369 | &constr); |
| 370 | } |
| 371 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 372 | static const struct snd_soc_dai_ops ad193x_dai_ops = { |
Codrin Ciubotariu | 75c2ecb | 2019-02-18 16:10:30 +0000 | [diff] [blame] | 373 | .startup = ad193x_startup, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 374 | .hw_params = ad193x_hw_params, |
| 375 | .digital_mute = ad193x_mute, |
| 376 | .set_tdm_slot = ad193x_set_tdm_slot, |
| 377 | .set_sysclk = ad193x_set_dai_sysclk, |
| 378 | .set_fmt = ad193x_set_dai_fmt, |
| 379 | }; |
| 380 | |
| 381 | /* codec DAI instance */ |
| 382 | static struct snd_soc_dai_driver ad193x_dai = { |
| 383 | .name = "ad193x-hifi", |
| 384 | .playback = { |
| 385 | .stream_name = "Playback", |
| 386 | .channels_min = 2, |
| 387 | .channels_max = 8, |
| 388 | .rates = SNDRV_PCM_RATE_48000, |
| 389 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 390 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 391 | }, |
| 392 | .capture = { |
| 393 | .stream_name = "Capture", |
| 394 | .channels_min = 2, |
| 395 | .channels_max = 4, |
| 396 | .rates = SNDRV_PCM_RATE_48000, |
| 397 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 398 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 399 | }, |
| 400 | .ops = &ad193x_dai_ops, |
| 401 | }; |
| 402 | |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 403 | /* codec DAI instance for DAC only */ |
| 404 | static struct snd_soc_dai_driver ad193x_no_adc_dai = { |
| 405 | .name = "ad193x-hifi", |
| 406 | .playback = { |
| 407 | .stream_name = "Playback", |
| 408 | .channels_min = 2, |
| 409 | .channels_max = 8, |
| 410 | .rates = SNDRV_PCM_RATE_48000, |
| 411 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 412 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 413 | }, |
| 414 | .ops = &ad193x_dai_ops, |
| 415 | }; |
| 416 | |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 417 | static int ad193x_component_probe(struct snd_soc_component *component) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 418 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 419 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
| 420 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 421 | int num, ret; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 422 | |
| 423 | /* default setting for ad193x */ |
| 424 | |
| 425 | /* unmute dac channels */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 426 | regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 427 | /* de-emphasis: 48kHz, powedown dac */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 428 | regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A); |
Lars-Peter Clausen | bdb2c74 | 2015-01-12 13:54:13 +0100 | [diff] [blame] | 429 | /* dac in tdm mode */ |
| 430 | regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x40); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 431 | |
| 432 | /* adc only */ |
| 433 | if (ad193x_has_adc(ad193x)) { |
| 434 | /* high-pass filter enable */ |
| 435 | regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3); |
| 436 | /* sata delay=1, adc aux mode */ |
| 437 | regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43); |
| 438 | } |
| 439 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 440 | /* pll input: mclki/xi */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 441 | regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */ |
| 442 | regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 443 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 444 | /* adc only */ |
| 445 | if (ad193x_has_adc(ad193x)) { |
| 446 | /* add adc controls */ |
| 447 | num = ARRAY_SIZE(ad193x_adc_snd_controls); |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 448 | ret = snd_soc_add_component_controls(component, |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 449 | ad193x_adc_snd_controls, |
| 450 | num); |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | |
| 454 | /* add adc widgets */ |
| 455 | num = ARRAY_SIZE(ad193x_adc_widgets); |
| 456 | ret = snd_soc_dapm_new_controls(dapm, |
| 457 | ad193x_adc_widgets, |
| 458 | num); |
| 459 | if (ret) |
| 460 | return ret; |
| 461 | |
| 462 | /* add adc routes */ |
| 463 | num = ARRAY_SIZE(ad193x_adc_audio_paths); |
| 464 | ret = snd_soc_dapm_add_routes(dapm, |
| 465 | ad193x_adc_audio_paths, |
| 466 | num); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | } |
| 470 | |
Xiubo Li | 5d6be5a | 2014-03-11 12:43:20 +0800 | [diff] [blame] | 471 | return 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 472 | } |
| 473 | |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 474 | static const struct snd_soc_component_driver soc_component_dev_ad193x = { |
| 475 | .probe = ad193x_component_probe, |
| 476 | .controls = ad193x_snd_controls, |
| 477 | .num_controls = ARRAY_SIZE(ad193x_snd_controls), |
| 478 | .dapm_widgets = ad193x_dapm_widgets, |
| 479 | .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets), |
| 480 | .dapm_routes = audio_paths, |
| 481 | .num_dapm_routes = ARRAY_SIZE(audio_paths), |
| 482 | .idle_bias_on = 1, |
| 483 | .use_pmdown_time = 1, |
| 484 | .endianness = 1, |
| 485 | .non_legacy_dai_naming = 1, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 486 | }; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 487 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 488 | const struct regmap_config ad193x_regmap_config = { |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 489 | .max_register = AD193X_NUM_REGS - 1, |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 490 | }; |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 491 | EXPORT_SYMBOL_GPL(ad193x_regmap_config); |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 492 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 493 | int ad193x_probe(struct device *dev, struct regmap *regmap, |
| 494 | enum ad193x_type type) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 495 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 496 | struct ad193x_priv *ad193x; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 497 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 498 | if (IS_ERR(regmap)) |
| 499 | return PTR_ERR(regmap); |
| 500 | |
| 501 | ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 502 | if (ad193x == NULL) |
| 503 | return -ENOMEM; |
| 504 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 505 | ad193x->regmap = regmap; |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 506 | ad193x->type = type; |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 507 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 508 | dev_set_drvdata(dev, ad193x); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 509 | |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 510 | if (ad193x_has_adc(ad193x)) |
| 511 | return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, |
| 512 | &ad193x_dai, 1); |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 513 | return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 514 | &ad193x_no_adc_dai, 1); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 515 | } |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 516 | EXPORT_SYMBOL_GPL(ad193x_probe); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 517 | |
| 518 | MODULE_DESCRIPTION("ASoC ad193x driver"); |
| 519 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 520 | MODULE_LICENSE("GPL"); |