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 9 #include <linux/module.h> 10 #include <linux/kernel.h> 11 #include <linux/device.h> 12 #include <linux/regmap.h> 13 #include <linux/slab.h> 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> 20 21 #include "ad193x.h" 22 23 /* codec private data */ 24 struct ad193x_priv { 25 struct regmap *regmap; 26 enum ad193x_type type; 27 int sysclk; 28 }; 29 30 /* 31 * AD193X volume/mute/de-emphasis etc. controls 32 */ 33 static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; 34 35 static SOC_ENUM_SINGLE_DECL(ad193x_deemp_enum, AD193X_DAC_CTRL2, 1, 36 ad193x_deemp); 37 38 static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0); 39 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 47 static const struct snd_kcontrol_new ad193x_snd_controls[] = { 48 /* DAC volume control */ 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), 57 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 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 79 /* ADC high-pass filter */ 80 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0, 81 AD193X_ADC_HIGHPASS_FILTER, 1, 0), 82 }; 83 84 static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = { 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), 87 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0), 88 SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0), 89 SND_SOC_DAPM_VMID("VMID"), 90 SND_SOC_DAPM_OUTPUT("DAC1OUT"), 91 SND_SOC_DAPM_OUTPUT("DAC2OUT"), 92 SND_SOC_DAPM_OUTPUT("DAC3OUT"), 93 SND_SOC_DAPM_OUTPUT("DAC4OUT"), 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), 99 SND_SOC_DAPM_INPUT("ADC1IN"), 100 SND_SOC_DAPM_INPUT("ADC2IN"), 101 }; 102 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 112 static const struct snd_soc_dapm_route audio_paths[] = { 113 { "DAC", NULL, "SYSCLK" }, 114 { "DAC Output", NULL, "DAC" }, 115 { "DAC Output", NULL, "VMID" }, 116 { "DAC1OUT", NULL, "DAC Output" }, 117 { "DAC2OUT", NULL, "DAC Output" }, 118 { "DAC3OUT", NULL, "DAC Output" }, 119 { "DAC4OUT", NULL, "DAC Output" }, 120 { "SYSCLK", NULL, "PLL_PWR", &ad193x_check_pll }, 121 }; 122 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 143 /* 144 * DAI ops entries 145 */ 146 147 static int ad193x_mute(struct snd_soc_dai *dai, int mute) 148 { 149 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); 150 151 if (mute) 152 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, 153 AD193X_DAC_MASTER_MUTE, 154 AD193X_DAC_MASTER_MUTE); 155 else 156 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, 157 AD193X_DAC_MASTER_MUTE, 0); 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 { 165 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component); 166 unsigned int channels; 167 168 switch (slots) { 169 case 2: 170 channels = AD193X_2_CHANNELS; 171 break; 172 case 4: 173 channels = AD193X_4_CHANNELS; 174 break; 175 case 8: 176 channels = AD193X_8_CHANNELS; 177 break; 178 case 16: 179 channels = AD193X_16_CHANNELS; 180 break; 181 default: 182 return -EINVAL; 183 } 184 185 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, 186 AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT); 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); 191 192 return 0; 193 } 194 195 static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai, 196 unsigned int fmt) 197 { 198 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component); 199 unsigned int adc_serfmt = 0; 200 unsigned int dac_serfmt = 0; 201 unsigned int adc_fmt = 0; 202 unsigned int dac_fmt = 0; 203 204 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S 205 * with TDM), ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) and DAC I2S mode 206 * (SND_SOC_DAIFMT_I2S) 207 */ 208 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 209 case SND_SOC_DAIFMT_I2S: 210 adc_serfmt |= AD193X_ADC_SERFMT_TDM; 211 dac_serfmt |= AD193X_DAC_SERFMT_STEREO; 212 break; 213 case SND_SOC_DAIFMT_DSP_A: 214 adc_serfmt |= AD193X_ADC_SERFMT_AUX; 215 dac_serfmt |= AD193X_DAC_SERFMT_TDM; 216 break; 217 default: 218 if (ad193x_has_adc(ad193x)) 219 return -EINVAL; 220 } 221 222 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 223 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ 224 break; 225 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ 226 adc_fmt |= AD193X_ADC_LEFT_HIGH; 227 dac_fmt |= AD193X_DAC_LEFT_HIGH; 228 break; 229 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ 230 adc_fmt |= AD193X_ADC_BCLK_INV; 231 dac_fmt |= AD193X_DAC_BCLK_INV; 232 break; 233 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ 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; 238 break; 239 default: 240 return -EINVAL; 241 } 242 243 /* For DSP_*, LRCLK's polarity must be inverted */ 244 if (fmt & SND_SOC_DAIFMT_DSP_A) { 245 change_bit(ffs(AD193X_DAC_LEFT_HIGH) - 1, 246 (unsigned long *)&dac_fmt); 247 } 248 249 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 250 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ 251 adc_fmt |= AD193X_ADC_LCR_MASTER; 252 adc_fmt |= AD193X_ADC_BCLK_MASTER; 253 dac_fmt |= AD193X_DAC_LCR_MASTER; 254 dac_fmt |= AD193X_DAC_BCLK_MASTER; 255 break; 256 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ 257 adc_fmt |= AD193X_ADC_LCR_MASTER; 258 dac_fmt |= AD193X_DAC_LCR_MASTER; 259 break; 260 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ 261 adc_fmt |= AD193X_ADC_BCLK_MASTER; 262 dac_fmt |= AD193X_DAC_BCLK_MASTER; 263 break; 264 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ 265 break; 266 default: 267 return -EINVAL; 268 } 269 270 if (ad193x_has_adc(ad193x)) { 271 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, 272 AD193X_ADC_SERFMT_MASK, adc_serfmt); 273 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2, 274 AD193X_ADC_FMT_MASK, adc_fmt); 275 } 276 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL0, 277 AD193X_DAC_SERFMT_MASK, dac_serfmt); 278 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1, 279 AD193X_DAC_FMT_MASK, dac_fmt); 280 281 return 0; 282 } 283 284 static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai, 285 int clk_id, unsigned int freq, int dir) 286 { 287 struct snd_soc_component *component = codec_dai->component; 288 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 289 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); 290 291 if (clk_id == AD193X_SYSCLK_MCLK) { 292 /* MCLK must be 512 x fs */ 293 if (dir == SND_SOC_CLOCK_OUT || freq != 24576000) 294 return -EINVAL; 295 296 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 297 AD193X_PLL_SRC_MASK, 298 AD193X_PLL_DAC_SRC_MCLK | 299 AD193X_PLL_CLK_SRC_MCLK); 300 301 snd_soc_dapm_sync(dapm); 302 return 0; 303 } 304 switch (freq) { 305 case 12288000: 306 case 18432000: 307 case 24576000: 308 case 36864000: 309 ad193x->sysclk = freq; 310 return 0; 311 } 312 return -EINVAL; 313 } 314 315 static int ad193x_hw_params(struct snd_pcm_substream *substream, 316 struct snd_pcm_hw_params *params, 317 struct snd_soc_dai *dai) 318 { 319 int word_len = 0, master_rate = 0; 320 struct snd_soc_component *component = dai->component; 321 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); 322 323 /* bit size */ 324 switch (params_width(params)) { 325 case 16: 326 word_len = 3; 327 break; 328 case 20: 329 word_len = 1; 330 break; 331 case 24: 332 case 32: 333 word_len = 0; 334 break; 335 } 336 337 switch (ad193x->sysclk) { 338 case 12288000: 339 master_rate = AD193X_PLL_INPUT_256; 340 break; 341 case 18432000: 342 master_rate = AD193X_PLL_INPUT_384; 343 break; 344 case 24576000: 345 master_rate = AD193X_PLL_INPUT_512; 346 break; 347 case 36864000: 348 master_rate = AD193X_PLL_INPUT_768; 349 break; 350 } 351 352 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 353 AD193X_PLL_INPUT_MASK, master_rate); 354 355 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2, 356 AD193X_DAC_WORD_LEN_MASK, 357 word_len << AD193X_DAC_WORD_LEN_SHFT); 358 359 if (ad193x_has_adc(ad193x)) 360 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1, 361 AD193X_ADC_WORD_LEN_MASK, word_len); 362 363 return 0; 364 } 365 366 static int ad193x_startup(struct snd_pcm_substream *substream, 367 struct snd_soc_dai *dai) 368 { 369 return snd_pcm_hw_constraint_list(substream->runtime, 0, 370 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 371 &constr); 372 } 373 374 static const struct snd_soc_dai_ops ad193x_dai_ops = { 375 .startup = ad193x_startup, 376 .hw_params = ad193x_hw_params, 377 .digital_mute = ad193x_mute, 378 .set_tdm_slot = ad193x_set_tdm_slot, 379 .set_sysclk = ad193x_set_dai_sysclk, 380 .set_fmt = ad193x_set_dai_fmt, 381 }; 382 383 /* codec DAI instance */ 384 static struct snd_soc_dai_driver ad193x_dai = { 385 .name = "ad193x-hifi", 386 .playback = { 387 .stream_name = "Playback", 388 .channels_min = 2, 389 .channels_max = 8, 390 .rates = SNDRV_PCM_RATE_48000, 391 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | 392 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, 393 }, 394 .capture = { 395 .stream_name = "Capture", 396 .channels_min = 2, 397 .channels_max = 4, 398 .rates = SNDRV_PCM_RATE_48000, 399 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | 400 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, 401 }, 402 .ops = &ad193x_dai_ops, 403 }; 404 405 /* codec DAI instance for DAC only */ 406 static struct snd_soc_dai_driver ad193x_no_adc_dai = { 407 .name = "ad193x-hifi", 408 .playback = { 409 .stream_name = "Playback", 410 .channels_min = 2, 411 .channels_max = 8, 412 .rates = SNDRV_PCM_RATE_48000, 413 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | 414 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, 415 }, 416 .ops = &ad193x_dai_ops, 417 }; 418 419 static int ad193x_component_probe(struct snd_soc_component *component) 420 { 421 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component); 422 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 423 int num, ret; 424 425 /* default setting for ad193x */ 426 427 /* unmute dac channels */ 428 regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0); 429 /* de-emphasis: 48kHz, powedown dac */ 430 regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A); 431 /* dac in tdm mode */ 432 regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x40); 433 434 /* adc only */ 435 if (ad193x_has_adc(ad193x)) { 436 /* high-pass filter enable */ 437 regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3); 438 /* sata delay=1, adc aux mode */ 439 regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43); 440 } 441 442 /* pll input: mclki/xi */ 443 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */ 444 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04); 445 446 /* adc only */ 447 if (ad193x_has_adc(ad193x)) { 448 /* add adc controls */ 449 num = ARRAY_SIZE(ad193x_adc_snd_controls); 450 ret = snd_soc_add_component_controls(component, 451 ad193x_adc_snd_controls, 452 num); 453 if (ret) 454 return ret; 455 456 /* add adc widgets */ 457 num = ARRAY_SIZE(ad193x_adc_widgets); 458 ret = snd_soc_dapm_new_controls(dapm, 459 ad193x_adc_widgets, 460 num); 461 if (ret) 462 return ret; 463 464 /* add adc routes */ 465 num = ARRAY_SIZE(ad193x_adc_audio_paths); 466 ret = snd_soc_dapm_add_routes(dapm, 467 ad193x_adc_audio_paths, 468 num); 469 if (ret) 470 return ret; 471 } 472 473 return 0; 474 } 475 476 static const struct snd_soc_component_driver soc_component_dev_ad193x = { 477 .probe = ad193x_component_probe, 478 .controls = ad193x_snd_controls, 479 .num_controls = ARRAY_SIZE(ad193x_snd_controls), 480 .dapm_widgets = ad193x_dapm_widgets, 481 .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets), 482 .dapm_routes = audio_paths, 483 .num_dapm_routes = ARRAY_SIZE(audio_paths), 484 .idle_bias_on = 1, 485 .use_pmdown_time = 1, 486 .endianness = 1, 487 .non_legacy_dai_naming = 1, 488 }; 489 490 const struct regmap_config ad193x_regmap_config = { 491 .max_register = AD193X_NUM_REGS - 1, 492 }; 493 EXPORT_SYMBOL_GPL(ad193x_regmap_config); 494 495 int ad193x_probe(struct device *dev, struct regmap *regmap, 496 enum ad193x_type type) 497 { 498 struct ad193x_priv *ad193x; 499 500 if (IS_ERR(regmap)) 501 return PTR_ERR(regmap); 502 503 ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL); 504 if (ad193x == NULL) 505 return -ENOMEM; 506 507 ad193x->regmap = regmap; 508 ad193x->type = type; 509 510 dev_set_drvdata(dev, ad193x); 511 512 if (ad193x_has_adc(ad193x)) 513 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, 514 &ad193x_dai, 1); 515 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x, 516 &ad193x_no_adc_dai, 1); 517 } 518 EXPORT_SYMBOL_GPL(ad193x_probe); 519 520 MODULE_DESCRIPTION("ASoC ad193x driver"); 521 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); 522 MODULE_LICENSE("GPL"); 523