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