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 | |
| 103 | static const struct snd_soc_dapm_route audio_paths[] = { |
Lars-Peter Clausen | 0718fd2 | 2011-11-28 17:28:10 +0100 | [diff] [blame] | 104 | { "DAC", NULL, "SYSCLK" }, |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 105 | { "DAC Output", NULL, "DAC" }, |
| 106 | { "DAC Output", NULL, "VMID" }, |
Lars-Peter Clausen | 1b86a3f | 2014-11-05 17:19:53 +0100 | [diff] [blame] | 107 | { "DAC1OUT", NULL, "DAC Output" }, |
| 108 | { "DAC2OUT", NULL, "DAC Output" }, |
| 109 | { "DAC3OUT", NULL, "DAC Output" }, |
| 110 | { "DAC4OUT", NULL, "DAC Output" }, |
Lars-Peter Clausen | 0718fd2 | 2011-11-28 17:28:10 +0100 | [diff] [blame] | 111 | { "SYSCLK", NULL, "PLL_PWR" }, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 112 | }; |
| 113 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 114 | static const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = { |
| 115 | { "ADC", NULL, "SYSCLK" }, |
| 116 | { "ADC", NULL, "ADC_PWR" }, |
| 117 | { "ADC", NULL, "ADC1IN" }, |
| 118 | { "ADC", NULL, "ADC2IN" }, |
| 119 | }; |
| 120 | |
| 121 | static inline bool ad193x_has_adc(const struct ad193x_priv *ad193x) |
| 122 | { |
| 123 | switch (ad193x->type) { |
| 124 | case AD1933: |
| 125 | case AD1934: |
| 126 | return false; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 134 | /* |
| 135 | * DAI ops entries |
| 136 | */ |
| 137 | |
| 138 | static int ad193x_mute(struct snd_soc_dai *dai, int mute) |
| 139 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 140 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 141 | |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 142 | if (mute) |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 143 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 144 | AD193X_DAC_MASTER_MUTE, |
| 145 | AD193X_DAC_MASTER_MUTE); |
| 146 | else |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 147 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 148 | AD193X_DAC_MASTER_MUTE, 0); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, |
| 154 | unsigned int rx_mask, int slots, int width) |
| 155 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 156 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 157 | unsigned int channels; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 158 | |
| 159 | switch (slots) { |
| 160 | case 2: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 161 | channels = AD193X_2_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 162 | break; |
| 163 | case 4: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 164 | channels = AD193X_4_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 165 | break; |
| 166 | case 8: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 167 | channels = AD193X_8_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 168 | break; |
| 169 | case 16: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 170 | channels = AD193X_16_CHANNELS; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 171 | break; |
| 172 | default: |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 176 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, |
| 177 | AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 178 | if (ad193x_has_adc(ad193x)) |
| 179 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2, |
| 180 | AD193X_ADC_CHAN_MASK, |
| 181 | channels << AD193X_ADC_CHAN_SHFT); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 187 | unsigned int fmt) |
| 188 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 189 | 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] | 190 | unsigned int adc_serfmt = 0; |
| 191 | unsigned int adc_fmt = 0; |
| 192 | unsigned int dac_fmt = 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 193 | |
| 194 | /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S |
| 195 | * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) |
| 196 | */ |
| 197 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 198 | case SND_SOC_DAIFMT_I2S: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 199 | adc_serfmt |= AD193X_ADC_SERFMT_TDM; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 200 | break; |
| 201 | case SND_SOC_DAIFMT_DSP_A: |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 202 | adc_serfmt |= AD193X_ADC_SERFMT_AUX; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 203 | break; |
| 204 | default: |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 205 | if (ad193x_has_adc(ad193x)) |
| 206 | return -EINVAL; |
| 207 | break; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 211 | case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 212 | break; |
| 213 | case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 214 | adc_fmt |= AD193X_ADC_LEFT_HIGH; |
| 215 | dac_fmt |= AD193X_DAC_LEFT_HIGH; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 216 | break; |
| 217 | case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 218 | adc_fmt |= AD193X_ADC_BCLK_INV; |
| 219 | dac_fmt |= AD193X_DAC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 220 | break; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 221 | case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 222 | adc_fmt |= AD193X_ADC_LEFT_HIGH; |
| 223 | adc_fmt |= AD193X_ADC_BCLK_INV; |
| 224 | dac_fmt |= AD193X_DAC_LEFT_HIGH; |
| 225 | dac_fmt |= AD193X_DAC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 226 | break; |
| 227 | default: |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 232 | case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 233 | adc_fmt |= AD193X_ADC_LCR_MASTER; |
| 234 | adc_fmt |= AD193X_ADC_BCLK_MASTER; |
| 235 | dac_fmt |= AD193X_DAC_LCR_MASTER; |
| 236 | dac_fmt |= AD193X_DAC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 237 | break; |
| 238 | case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 239 | adc_fmt |= AD193X_ADC_LCR_MASTER; |
| 240 | dac_fmt |= AD193X_DAC_LCR_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 241 | break; |
| 242 | case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ |
Lars-Peter Clausen | b82ca57 | 2011-11-28 17:28:11 +0100 | [diff] [blame] | 243 | adc_fmt |= AD193X_ADC_BCLK_MASTER; |
| 244 | dac_fmt |= AD193X_DAC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 245 | break; |
| 246 | case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 247 | break; |
| 248 | default: |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 252 | if (ad193x_has_adc(ad193x)) { |
| 253 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, |
| 254 | AD193X_ADC_SERFMT_MASK, adc_serfmt); |
| 255 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2, |
| 256 | AD193X_ADC_FMT_MASK, adc_fmt); |
| 257 | } |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 258 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, |
| 259 | AD193X_DAC_FMT_MASK, dac_fmt); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 264 | static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 265 | int clk_id, unsigned int freq, int dir) |
| 266 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 267 | struct snd_soc_component *component = codec_dai->component; |
| 268 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 269 | switch (freq) { |
| 270 | case 12288000: |
| 271 | case 18432000: |
| 272 | case 24576000: |
| 273 | case 36864000: |
| 274 | ad193x->sysclk = freq; |
| 275 | return 0; |
| 276 | } |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 280 | static int ad193x_hw_params(struct snd_pcm_substream *substream, |
| 281 | struct snd_pcm_hw_params *params, |
| 282 | struct snd_soc_dai *dai) |
| 283 | { |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 284 | int word_len = 0, master_rate = 0; |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 285 | struct snd_soc_component *component = dai->component; |
| 286 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 287 | |
| 288 | /* bit size */ |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 289 | switch (params_width(params)) { |
| 290 | case 16: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 291 | word_len = 3; |
| 292 | break; |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 293 | case 20: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 294 | word_len = 1; |
| 295 | break; |
Mark Brown | d4dd1fd | 2014-01-08 18:38:20 +0000 | [diff] [blame] | 296 | case 24: |
| 297 | case 32: |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 298 | word_len = 0; |
| 299 | break; |
| 300 | } |
| 301 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 302 | switch (ad193x->sysclk) { |
| 303 | case 12288000: |
| 304 | master_rate = AD193X_PLL_INPUT_256; |
| 305 | break; |
| 306 | case 18432000: |
| 307 | master_rate = AD193X_PLL_INPUT_384; |
| 308 | break; |
| 309 | case 24576000: |
| 310 | master_rate = AD193X_PLL_INPUT_512; |
| 311 | break; |
| 312 | case 36864000: |
| 313 | master_rate = AD193X_PLL_INPUT_768; |
| 314 | break; |
| 315 | } |
| 316 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 317 | regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 318 | AD193X_PLL_INPUT_MASK, master_rate); |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 319 | |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 320 | regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, |
Axel Lin | 54c96cf | 2011-10-18 06:25:08 +0800 | [diff] [blame] | 321 | AD193X_DAC_WORD_LEN_MASK, |
| 322 | word_len << AD193X_DAC_WORD_LEN_SHFT); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 323 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 324 | if (ad193x_has_adc(ad193x)) |
| 325 | regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, |
| 326 | AD193X_ADC_WORD_LEN_MASK, word_len); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Codrin Ciubotariu | 75c2ecb | 2019-02-18 16:10:30 +0000 | [diff] [blame^] | 331 | static int ad193x_startup(struct snd_pcm_substream *substream, |
| 332 | struct snd_soc_dai *dai) |
| 333 | { |
| 334 | return snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 335 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 336 | &constr); |
| 337 | } |
| 338 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 339 | static const struct snd_soc_dai_ops ad193x_dai_ops = { |
Codrin Ciubotariu | 75c2ecb | 2019-02-18 16:10:30 +0000 | [diff] [blame^] | 340 | .startup = ad193x_startup, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 341 | .hw_params = ad193x_hw_params, |
| 342 | .digital_mute = ad193x_mute, |
| 343 | .set_tdm_slot = ad193x_set_tdm_slot, |
| 344 | .set_sysclk = ad193x_set_dai_sysclk, |
| 345 | .set_fmt = ad193x_set_dai_fmt, |
| 346 | }; |
| 347 | |
| 348 | /* codec DAI instance */ |
| 349 | static struct snd_soc_dai_driver ad193x_dai = { |
| 350 | .name = "ad193x-hifi", |
| 351 | .playback = { |
| 352 | .stream_name = "Playback", |
| 353 | .channels_min = 2, |
| 354 | .channels_max = 8, |
| 355 | .rates = SNDRV_PCM_RATE_48000, |
| 356 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 357 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 358 | }, |
| 359 | .capture = { |
| 360 | .stream_name = "Capture", |
| 361 | .channels_min = 2, |
| 362 | .channels_max = 4, |
| 363 | .rates = SNDRV_PCM_RATE_48000, |
| 364 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 365 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 366 | }, |
| 367 | .ops = &ad193x_dai_ops, |
| 368 | }; |
| 369 | |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 370 | /* codec DAI instance for DAC only */ |
| 371 | static struct snd_soc_dai_driver ad193x_no_adc_dai = { |
| 372 | .name = "ad193x-hifi", |
| 373 | .playback = { |
| 374 | .stream_name = "Playback", |
| 375 | .channels_min = 2, |
| 376 | .channels_max = 8, |
| 377 | .rates = SNDRV_PCM_RATE_48000, |
| 378 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 379 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 380 | }, |
| 381 | .ops = &ad193x_dai_ops, |
| 382 | }; |
| 383 | |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 384 | static int ad193x_component_probe(struct snd_soc_component *component) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 385 | { |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 386 | struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); |
| 387 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 388 | int num, ret; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 389 | |
| 390 | /* default setting for ad193x */ |
| 391 | |
| 392 | /* unmute dac channels */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 393 | regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 394 | /* de-emphasis: 48kHz, powedown dac */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 395 | regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A); |
Lars-Peter Clausen | bdb2c74 | 2015-01-12 13:54:13 +0100 | [diff] [blame] | 396 | /* dac in tdm mode */ |
| 397 | regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x40); |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 398 | |
| 399 | /* adc only */ |
| 400 | if (ad193x_has_adc(ad193x)) { |
| 401 | /* high-pass filter enable */ |
| 402 | regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3); |
| 403 | /* sata delay=1, adc aux mode */ |
| 404 | regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43); |
| 405 | } |
| 406 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 407 | /* pll input: mclki/xi */ |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 408 | regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */ |
| 409 | regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 410 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 411 | /* adc only */ |
| 412 | if (ad193x_has_adc(ad193x)) { |
| 413 | /* add adc controls */ |
| 414 | num = ARRAY_SIZE(ad193x_adc_snd_controls); |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 415 | ret = snd_soc_add_component_controls(component, |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 416 | ad193x_adc_snd_controls, |
| 417 | num); |
| 418 | if (ret) |
| 419 | return ret; |
| 420 | |
| 421 | /* add adc widgets */ |
| 422 | num = ARRAY_SIZE(ad193x_adc_widgets); |
| 423 | ret = snd_soc_dapm_new_controls(dapm, |
| 424 | ad193x_adc_widgets, |
| 425 | num); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | |
| 429 | /* add adc routes */ |
| 430 | num = ARRAY_SIZE(ad193x_adc_audio_paths); |
| 431 | ret = snd_soc_dapm_add_routes(dapm, |
| 432 | ad193x_adc_audio_paths, |
| 433 | num); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | } |
| 437 | |
Xiubo Li | 5d6be5a | 2014-03-11 12:43:20 +0800 | [diff] [blame] | 438 | return 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 439 | } |
| 440 | |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 441 | static const struct snd_soc_component_driver soc_component_dev_ad193x = { |
| 442 | .probe = ad193x_component_probe, |
| 443 | .controls = ad193x_snd_controls, |
| 444 | .num_controls = ARRAY_SIZE(ad193x_snd_controls), |
| 445 | .dapm_widgets = ad193x_dapm_widgets, |
| 446 | .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets), |
| 447 | .dapm_routes = audio_paths, |
| 448 | .num_dapm_routes = ARRAY_SIZE(audio_paths), |
| 449 | .idle_bias_on = 1, |
| 450 | .use_pmdown_time = 1, |
| 451 | .endianness = 1, |
| 452 | .non_legacy_dai_naming = 1, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 453 | }; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 454 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 455 | const struct regmap_config ad193x_regmap_config = { |
Lars-Peter Clausen | 34cbe16 | 2011-11-28 17:28:12 +0100 | [diff] [blame] | 456 | .max_register = AD193X_NUM_REGS - 1, |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 457 | }; |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 458 | EXPORT_SYMBOL_GPL(ad193x_regmap_config); |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 459 | |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 460 | int ad193x_probe(struct device *dev, struct regmap *regmap, |
| 461 | enum ad193x_type type) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 462 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 463 | struct ad193x_priv *ad193x; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 464 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 465 | if (IS_ERR(regmap)) |
| 466 | return PTR_ERR(regmap); |
| 467 | |
| 468 | ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 469 | if (ad193x == NULL) |
| 470 | return -ENOMEM; |
| 471 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 472 | ad193x->regmap = regmap; |
Cyrille Pitchen | e5224f5 | 2015-09-29 16:41:43 +0200 | [diff] [blame] | 473 | ad193x->type = type; |
Lars-Peter Clausen | 30ab1e7 | 2011-09-05 20:46:33 +0200 | [diff] [blame] | 474 | |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 475 | dev_set_drvdata(dev, ad193x); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 476 | |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 477 | if (ad193x_has_adc(ad193x)) |
| 478 | return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, |
| 479 | &ad193x_dai, 1); |
Kuninori Morimoto | 89cea5c | 2018-01-29 04:25:29 +0000 | [diff] [blame] | 480 | return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, |
Codrin Ciubotariu | 7aac8d13 | 2019-02-18 16:10:28 +0000 | [diff] [blame] | 481 | &ad193x_no_adc_dai, 1); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 482 | } |
Lars-Peter Clausen | 6c3d713 | 2014-02-17 13:16:54 +0100 | [diff] [blame] | 483 | EXPORT_SYMBOL_GPL(ad193x_probe); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 484 | |
| 485 | MODULE_DESCRIPTION("ASoC ad193x driver"); |
| 486 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 487 | MODULE_LICENSE("GPL"); |