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/init.h> 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/device.h> 13 #include <linux/i2c.h> 14 #include <linux/spi/spi.h> 15 #include <linux/slab.h> 16 #include <sound/core.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/initval.h> 20 #include <sound/soc.h> 21 #include <sound/tlv.h> 22 #include "ad193x.h" 23 24 /* codec private data */ 25 struct ad193x_priv { 26 enum snd_soc_control_type control_type; 27 int sysclk; 28 }; 29 30 /* 31 * AD193X volume/mute/de-emphasis etc. controls 32 */ 33 static const char *ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; 34 35 static const struct soc_enum ad193x_deemp_enum = 36 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp); 37 38 static const struct snd_kcontrol_new ad193x_snd_controls[] = { 39 /* DAC volume control */ 40 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL, 41 AD193X_DAC_R1_VOL, 0, 0xFF, 1), 42 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL, 43 AD193X_DAC_R2_VOL, 0, 0xFF, 1), 44 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL, 45 AD193X_DAC_R3_VOL, 0, 0xFF, 1), 46 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL, 47 AD193X_DAC_R4_VOL, 0, 0xFF, 1), 48 49 /* ADC switch control */ 50 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE, 51 AD193X_ADCR1_MUTE, 1, 1), 52 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE, 53 AD193X_ADCR2_MUTE, 1, 1), 54 55 /* DAC switch control */ 56 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE, 57 AD193X_DACR1_MUTE, 1, 1), 58 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE, 59 AD193X_DACR2_MUTE, 1, 1), 60 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE, 61 AD193X_DACR3_MUTE, 1, 1), 62 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE, 63 AD193X_DACR4_MUTE, 1, 1), 64 65 /* ADC high-pass filter */ 66 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0, 67 AD193X_ADC_HIGHPASS_FILTER, 1, 0), 68 69 /* DAC de-emphasis */ 70 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum), 71 }; 72 73 static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = { 74 SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1), 75 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), 76 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0), 77 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0), 78 SND_SOC_DAPM_OUTPUT("DAC1OUT"), 79 SND_SOC_DAPM_OUTPUT("DAC2OUT"), 80 SND_SOC_DAPM_OUTPUT("DAC3OUT"), 81 SND_SOC_DAPM_OUTPUT("DAC4OUT"), 82 SND_SOC_DAPM_INPUT("ADC1IN"), 83 SND_SOC_DAPM_INPUT("ADC2IN"), 84 }; 85 86 static const struct snd_soc_dapm_route audio_paths[] = { 87 { "DAC", NULL, "PLL_PWR" }, 88 { "ADC", NULL, "PLL_PWR" }, 89 { "DAC", NULL, "ADC_PWR" }, 90 { "ADC", NULL, "ADC_PWR" }, 91 { "DAC1OUT", "DAC1 Switch", "DAC" }, 92 { "DAC2OUT", "DAC2 Switch", "DAC" }, 93 { "DAC3OUT", "DAC3 Switch", "DAC" }, 94 { "DAC4OUT", "DAC4 Switch", "DAC" }, 95 { "ADC", "ADC1 Switch", "ADC1IN" }, 96 { "ADC", "ADC2 Switch", "ADC2IN" }, 97 }; 98 99 /* 100 * DAI ops entries 101 */ 102 103 static int ad193x_mute(struct snd_soc_dai *dai, int mute) 104 { 105 struct snd_soc_codec *codec = dai->codec; 106 int reg; 107 108 reg = snd_soc_read(codec, AD193X_DAC_CTRL2); 109 reg = (mute > 0) ? reg | AD193X_DAC_MASTER_MUTE : reg & 110 (~AD193X_DAC_MASTER_MUTE); 111 snd_soc_write(codec, AD193X_DAC_CTRL2, reg); 112 113 return 0; 114 } 115 116 static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 117 unsigned int rx_mask, int slots, int width) 118 { 119 struct snd_soc_codec *codec = dai->codec; 120 int dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1); 121 int adc_reg = snd_soc_read(codec, AD193X_ADC_CTRL2); 122 123 dac_reg &= ~AD193X_DAC_CHAN_MASK; 124 adc_reg &= ~AD193X_ADC_CHAN_MASK; 125 126 switch (slots) { 127 case 2: 128 dac_reg |= AD193X_DAC_2_CHANNELS << AD193X_DAC_CHAN_SHFT; 129 adc_reg |= AD193X_ADC_2_CHANNELS << AD193X_ADC_CHAN_SHFT; 130 break; 131 case 4: 132 dac_reg |= AD193X_DAC_4_CHANNELS << AD193X_DAC_CHAN_SHFT; 133 adc_reg |= AD193X_ADC_4_CHANNELS << AD193X_ADC_CHAN_SHFT; 134 break; 135 case 8: 136 dac_reg |= AD193X_DAC_8_CHANNELS << AD193X_DAC_CHAN_SHFT; 137 adc_reg |= AD193X_ADC_8_CHANNELS << AD193X_ADC_CHAN_SHFT; 138 break; 139 case 16: 140 dac_reg |= AD193X_DAC_16_CHANNELS << AD193X_DAC_CHAN_SHFT; 141 adc_reg |= AD193X_ADC_16_CHANNELS << AD193X_ADC_CHAN_SHFT; 142 break; 143 default: 144 return -EINVAL; 145 } 146 147 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg); 148 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg); 149 150 return 0; 151 } 152 153 static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai, 154 unsigned int fmt) 155 { 156 struct snd_soc_codec *codec = codec_dai->codec; 157 int adc_reg1, adc_reg2, dac_reg; 158 159 adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1); 160 adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2); 161 dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1); 162 163 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S 164 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) 165 */ 166 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 167 case SND_SOC_DAIFMT_I2S: 168 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK; 169 adc_reg1 |= AD193X_ADC_SERFMT_TDM; 170 break; 171 case SND_SOC_DAIFMT_DSP_A: 172 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK; 173 adc_reg1 |= AD193X_ADC_SERFMT_AUX; 174 break; 175 default: 176 return -EINVAL; 177 } 178 179 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 180 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ 181 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH; 182 adc_reg2 &= ~AD193X_ADC_BCLK_INV; 183 dac_reg &= ~AD193X_DAC_LEFT_HIGH; 184 dac_reg &= ~AD193X_DAC_BCLK_INV; 185 break; 186 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ 187 adc_reg2 |= AD193X_ADC_LEFT_HIGH; 188 adc_reg2 &= ~AD193X_ADC_BCLK_INV; 189 dac_reg |= AD193X_DAC_LEFT_HIGH; 190 dac_reg &= ~AD193X_DAC_BCLK_INV; 191 break; 192 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ 193 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH; 194 adc_reg2 |= AD193X_ADC_BCLK_INV; 195 dac_reg &= ~AD193X_DAC_LEFT_HIGH; 196 dac_reg |= AD193X_DAC_BCLK_INV; 197 break; 198 199 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ 200 adc_reg2 |= AD193X_ADC_LEFT_HIGH; 201 adc_reg2 |= AD193X_ADC_BCLK_INV; 202 dac_reg |= AD193X_DAC_LEFT_HIGH; 203 dac_reg |= AD193X_DAC_BCLK_INV; 204 break; 205 default: 206 return -EINVAL; 207 } 208 209 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 210 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ 211 adc_reg2 |= AD193X_ADC_LCR_MASTER; 212 adc_reg2 |= AD193X_ADC_BCLK_MASTER; 213 dac_reg |= AD193X_DAC_LCR_MASTER; 214 dac_reg |= AD193X_DAC_BCLK_MASTER; 215 break; 216 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ 217 adc_reg2 |= AD193X_ADC_LCR_MASTER; 218 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER; 219 dac_reg |= AD193X_DAC_LCR_MASTER; 220 dac_reg &= ~AD193X_DAC_BCLK_MASTER; 221 break; 222 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ 223 adc_reg2 &= ~AD193X_ADC_LCR_MASTER; 224 adc_reg2 |= AD193X_ADC_BCLK_MASTER; 225 dac_reg &= ~AD193X_DAC_LCR_MASTER; 226 dac_reg |= AD193X_DAC_BCLK_MASTER; 227 break; 228 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ 229 adc_reg2 &= ~AD193X_ADC_LCR_MASTER; 230 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER; 231 dac_reg &= ~AD193X_DAC_LCR_MASTER; 232 dac_reg &= ~AD193X_DAC_BCLK_MASTER; 233 break; 234 default: 235 return -EINVAL; 236 } 237 238 snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1); 239 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2); 240 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg); 241 242 return 0; 243 } 244 245 static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai, 246 int clk_id, unsigned int freq, int dir) 247 { 248 struct snd_soc_codec *codec = codec_dai->codec; 249 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); 250 switch (freq) { 251 case 12288000: 252 case 18432000: 253 case 24576000: 254 case 36864000: 255 ad193x->sysclk = freq; 256 return 0; 257 } 258 return -EINVAL; 259 } 260 261 static int ad193x_hw_params(struct snd_pcm_substream *substream, 262 struct snd_pcm_hw_params *params, 263 struct snd_soc_dai *dai) 264 { 265 int word_len = 0, reg = 0, master_rate = 0; 266 267 struct snd_soc_pcm_runtime *rtd = substream->private_data; 268 struct snd_soc_codec *codec = rtd->codec; 269 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); 270 271 /* bit size */ 272 switch (params_format(params)) { 273 case SNDRV_PCM_FORMAT_S16_LE: 274 word_len = 3; 275 break; 276 case SNDRV_PCM_FORMAT_S20_3LE: 277 word_len = 1; 278 break; 279 case SNDRV_PCM_FORMAT_S24_LE: 280 case SNDRV_PCM_FORMAT_S32_LE: 281 word_len = 0; 282 break; 283 } 284 285 switch (ad193x->sysclk) { 286 case 12288000: 287 master_rate = AD193X_PLL_INPUT_256; 288 break; 289 case 18432000: 290 master_rate = AD193X_PLL_INPUT_384; 291 break; 292 case 24576000: 293 master_rate = AD193X_PLL_INPUT_512; 294 break; 295 case 36864000: 296 master_rate = AD193X_PLL_INPUT_768; 297 break; 298 } 299 300 reg = snd_soc_read(codec, AD193X_PLL_CLK_CTRL0); 301 reg = (reg & AD193X_PLL_INPUT_MASK) | master_rate; 302 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, reg); 303 304 reg = snd_soc_read(codec, AD193X_DAC_CTRL2); 305 reg = (reg & (~AD193X_DAC_WORD_LEN_MASK)) 306 | (word_len << AD193X_DAC_WORD_LEN_SHFT); 307 snd_soc_write(codec, AD193X_DAC_CTRL2, reg); 308 309 reg = snd_soc_read(codec, AD193X_ADC_CTRL1); 310 reg = (reg & (~AD193X_ADC_WORD_LEN_MASK)) | word_len; 311 snd_soc_write(codec, AD193X_ADC_CTRL1, reg); 312 313 return 0; 314 } 315 316 static struct snd_soc_dai_ops ad193x_dai_ops = { 317 .hw_params = ad193x_hw_params, 318 .digital_mute = ad193x_mute, 319 .set_tdm_slot = ad193x_set_tdm_slot, 320 .set_sysclk = ad193x_set_dai_sysclk, 321 .set_fmt = ad193x_set_dai_fmt, 322 }; 323 324 /* codec DAI instance */ 325 static struct snd_soc_dai_driver ad193x_dai = { 326 .name = "ad193x-hifi", 327 .playback = { 328 .stream_name = "Playback", 329 .channels_min = 2, 330 .channels_max = 8, 331 .rates = SNDRV_PCM_RATE_48000, 332 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | 333 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, 334 }, 335 .capture = { 336 .stream_name = "Capture", 337 .channels_min = 2, 338 .channels_max = 4, 339 .rates = SNDRV_PCM_RATE_48000, 340 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | 341 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, 342 }, 343 .ops = &ad193x_dai_ops, 344 }; 345 346 static int ad193x_probe(struct snd_soc_codec *codec) 347 { 348 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); 349 struct snd_soc_dapm_context *dapm = &codec->dapm; 350 int ret; 351 352 if (ad193x->control_type == SND_SOC_I2C) 353 ret = snd_soc_codec_set_cache_io(codec, 8, 8, ad193x->control_type); 354 else 355 ret = snd_soc_codec_set_cache_io(codec, 16, 8, ad193x->control_type); 356 if (ret < 0) { 357 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret); 358 return ret; 359 } 360 361 /* default setting for ad193x */ 362 363 /* unmute dac channels */ 364 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0); 365 /* de-emphasis: 48kHz, powedown dac */ 366 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A); 367 /* powerdown dac, dac in tdm mode */ 368 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41); 369 /* high-pass filter enable */ 370 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3); 371 /* sata delay=1, adc aux mode */ 372 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43); 373 /* pll input: mclki/xi */ 374 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */ 375 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04); 376 377 snd_soc_add_controls(codec, ad193x_snd_controls, 378 ARRAY_SIZE(ad193x_snd_controls)); 379 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets, 380 ARRAY_SIZE(ad193x_dapm_widgets)); 381 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths)); 382 383 return ret; 384 } 385 386 static struct snd_soc_codec_driver soc_codec_dev_ad193x = { 387 .probe = ad193x_probe, 388 }; 389 390 #if defined(CONFIG_SPI_MASTER) 391 static int __devinit ad193x_spi_probe(struct spi_device *spi) 392 { 393 struct ad193x_priv *ad193x; 394 int ret; 395 396 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL); 397 if (ad193x == NULL) 398 return -ENOMEM; 399 400 spi_set_drvdata(spi, ad193x); 401 ad193x->control_type = SND_SOC_SPI; 402 403 ret = snd_soc_register_codec(&spi->dev, 404 &soc_codec_dev_ad193x, &ad193x_dai, 1); 405 if (ret < 0) 406 kfree(ad193x); 407 return ret; 408 } 409 410 static int __devexit ad193x_spi_remove(struct spi_device *spi) 411 { 412 snd_soc_unregister_codec(&spi->dev); 413 kfree(spi_get_drvdata(spi)); 414 return 0; 415 } 416 417 static struct spi_driver ad193x_spi_driver = { 418 .driver = { 419 .name = "ad193x", 420 .owner = THIS_MODULE, 421 }, 422 .probe = ad193x_spi_probe, 423 .remove = __devexit_p(ad193x_spi_remove), 424 }; 425 #endif 426 427 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 428 static const struct i2c_device_id ad193x_id[] = { 429 { "ad1936", 0 }, 430 { "ad1937", 0 }, 431 { } 432 }; 433 MODULE_DEVICE_TABLE(i2c, ad193x_id); 434 435 static int __devinit ad193x_i2c_probe(struct i2c_client *client, 436 const struct i2c_device_id *id) 437 { 438 struct ad193x_priv *ad193x; 439 int ret; 440 441 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL); 442 if (ad193x == NULL) 443 return -ENOMEM; 444 445 i2c_set_clientdata(client, ad193x); 446 ad193x->control_type = SND_SOC_I2C; 447 448 ret = snd_soc_register_codec(&client->dev, 449 &soc_codec_dev_ad193x, &ad193x_dai, 1); 450 if (ret < 0) 451 kfree(ad193x); 452 return ret; 453 } 454 455 static int __devexit ad193x_i2c_remove(struct i2c_client *client) 456 { 457 snd_soc_unregister_codec(&client->dev); 458 kfree(i2c_get_clientdata(client)); 459 return 0; 460 } 461 462 static struct i2c_driver ad193x_i2c_driver = { 463 .driver = { 464 .name = "ad193x", 465 }, 466 .probe = ad193x_i2c_probe, 467 .remove = __devexit_p(ad193x_i2c_remove), 468 .id_table = ad193x_id, 469 }; 470 #endif 471 472 static int __init ad193x_modinit(void) 473 { 474 int ret; 475 476 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 477 ret = i2c_add_driver(&ad193x_i2c_driver); 478 if (ret != 0) { 479 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n", 480 ret); 481 } 482 #endif 483 484 #if defined(CONFIG_SPI_MASTER) 485 ret = spi_register_driver(&ad193x_spi_driver); 486 if (ret != 0) { 487 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n", 488 ret); 489 } 490 #endif 491 return ret; 492 } 493 module_init(ad193x_modinit); 494 495 static void __exit ad193x_modexit(void) 496 { 497 #if defined(CONFIG_SPI_MASTER) 498 spi_unregister_driver(&ad193x_spi_driver); 499 #endif 500 501 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 502 i2c_del_driver(&ad193x_i2c_driver); 503 #endif 504 } 505 module_exit(ad193x_modexit); 506 507 MODULE_DESCRIPTION("ASoC ad193x driver"); 508 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); 509 MODULE_LICENSE("GPL"); 510