1 /* 2 * DA7210 ALSA Soc codec driver 3 * 4 * Copyright (c) 2009 Dialog Semiconductor 5 * Written by David Chen <Dajun.chen@diasemi.com> 6 * 7 * Copyright (C) 2009 Renesas Solutions Corp. 8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com> 9 * 10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the 14 * Free Software Foundation; either version 2 of the License, or (at your 15 * option) any later version. 16 */ 17 18 #include <linux/module.h> 19 #include <linux/moduleparam.h> 20 #include <linux/kernel.h> 21 #include <linux/init.h> 22 #include <linux/delay.h> 23 #include <linux/pm.h> 24 #include <linux/i2c.h> 25 #include <linux/platform_device.h> 26 #include <sound/core.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/soc.h> 30 #include <sound/soc-dapm.h> 31 #include <sound/tlv.h> 32 #include <sound/initval.h> 33 #include <asm/div64.h> 34 35 #include "da7210.h" 36 37 /* DA7210 register space */ 38 #define DA7210_STATUS 0x02 39 #define DA7210_STARTUP1 0x03 40 #define DA7210_MIC_L 0x07 41 #define DA7210_MIC_R 0x08 42 #define DA7210_INMIX_L 0x0D 43 #define DA7210_INMIX_R 0x0E 44 #define DA7210_ADC_HPF 0x0F 45 #define DA7210_ADC 0x10 46 #define DA7210_DAC_HPF 0x14 47 #define DA7210_DAC_L 0x15 48 #define DA7210_DAC_R 0x16 49 #define DA7210_DAC_SEL 0x17 50 #define DA7210_OUTMIX_L 0x1C 51 #define DA7210_OUTMIX_R 0x1D 52 #define DA7210_HP_L_VOL 0x21 53 #define DA7210_HP_R_VOL 0x22 54 #define DA7210_HP_CFG 0x23 55 #define DA7210_DAI_SRC_SEL 0x25 56 #define DA7210_DAI_CFG1 0x26 57 #define DA7210_DAI_CFG3 0x28 58 #define DA7210_PLL_DIV3 0x2B 59 #define DA7210_PLL 0x2C 60 61 /* STARTUP1 bit fields */ 62 #define DA7210_SC_MST_EN (1 << 0) 63 64 /* MIC_L bit fields */ 65 #define DA7210_MICBIAS_EN (1 << 6) 66 #define DA7210_MIC_L_EN (1 << 7) 67 68 /* MIC_R bit fields */ 69 #define DA7210_MIC_R_EN (1 << 7) 70 71 /* INMIX_L bit fields */ 72 #define DA7210_IN_L_EN (1 << 7) 73 74 /* INMIX_R bit fields */ 75 #define DA7210_IN_R_EN (1 << 7) 76 77 /* ADC_HPF bit fields */ 78 #define DA7210_ADC_VOICE_EN (1 << 7) 79 80 /* ADC bit fields */ 81 #define DA7210_ADC_L_EN (1 << 3) 82 #define DA7210_ADC_R_EN (1 << 7) 83 84 /* DAC_HPF fields */ 85 #define DA7210_DAC_VOICE_EN (1 << 7) 86 87 /* DAC_SEL bit fields */ 88 #define DA7210_DAC_L_SRC_DAI_L (4 << 0) 89 #define DA7210_DAC_L_EN (1 << 3) 90 #define DA7210_DAC_R_SRC_DAI_R (5 << 4) 91 #define DA7210_DAC_R_EN (1 << 7) 92 93 /* OUTMIX_L bit fields */ 94 #define DA7210_OUT_L_EN (1 << 7) 95 96 /* OUTMIX_R bit fields */ 97 #define DA7210_OUT_R_EN (1 << 7) 98 99 /* HP_CFG bit fields */ 100 #define DA7210_HP_2CAP_MODE (1 << 1) 101 #define DA7210_HP_SENSE_EN (1 << 2) 102 #define DA7210_HP_L_EN (1 << 3) 103 #define DA7210_HP_MODE (1 << 6) 104 #define DA7210_HP_R_EN (1 << 7) 105 106 /* DAI_SRC_SEL bit fields */ 107 #define DA7210_DAI_OUT_L_SRC (6 << 0) 108 #define DA7210_DAI_OUT_R_SRC (7 << 4) 109 110 /* DAI_CFG1 bit fields */ 111 #define DA7210_DAI_WORD_S16_LE (0 << 0) 112 #define DA7210_DAI_WORD_S24_LE (2 << 0) 113 #define DA7210_DAI_FLEN_64BIT (1 << 2) 114 #define DA7210_DAI_MODE_MASTER (1 << 7) 115 116 /* DAI_CFG3 bit fields */ 117 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0) 118 #define DA7210_DAI_OE (1 << 3) 119 #define DA7210_DAI_EN (1 << 7) 120 121 /*PLL_DIV3 bit fields */ 122 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4) 123 #define DA7210_PLL_BYP (1 << 6) 124 125 /* PLL bit fields */ 126 #define DA7210_PLL_FS_48000 (11 << 0) 127 128 #define DA7210_VERSION "0.0.1" 129 130 /* Codec private data */ 131 struct da7210_priv { 132 struct snd_soc_codec codec; 133 }; 134 135 static struct snd_soc_codec *da7210_codec; 136 137 /* 138 * Register cache 139 */ 140 static const u8 da7210_reg[] = { 141 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */ 142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */ 143 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */ 144 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */ 145 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */ 146 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */ 147 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */ 148 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */ 149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */ 150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */ 151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */ 152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */ 153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */ 154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */ 155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */ 156 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */ 157 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */ 158 0x00, /* R88 */ 159 }; 160 161 /* 162 * Read da7210 register cache 163 */ 164 static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg) 165 { 166 u8 *cache = codec->reg_cache; 167 BUG_ON(reg > ARRAY_SIZE(da7210_reg)); 168 return cache[reg]; 169 } 170 171 /* 172 * Write to the da7210 register space 173 */ 174 static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value) 175 { 176 u8 *cache = codec->reg_cache; 177 u8 data[2]; 178 179 BUG_ON(codec->volatile_register); 180 181 data[0] = reg & 0xff; 182 data[1] = value & 0xff; 183 184 if (reg >= codec->reg_cache_size) 185 return -EIO; 186 187 if (2 != codec->hw_write(codec->control_data, data, 2)) 188 return -EIO; 189 190 cache[reg] = value; 191 return 0; 192 } 193 194 /* 195 * Read from the da7210 register space. 196 */ 197 static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg) 198 { 199 if (DA7210_STATUS == reg) 200 return i2c_smbus_read_byte_data(codec->control_data, reg); 201 202 return da7210_read_reg_cache(codec, reg); 203 } 204 205 static int da7210_startup(struct snd_pcm_substream *substream, 206 struct snd_soc_dai *dai) 207 { 208 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 209 struct snd_soc_codec *codec = dai->codec; 210 211 if (is_play) { 212 /* PlayBack Volume 40 */ 213 snd_soc_update_bits(codec, DA7210_HP_L_VOL, 0x3F, 40); 214 snd_soc_update_bits(codec, DA7210_HP_R_VOL, 0x3F, 40); 215 216 /* Enable Out */ 217 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10); 218 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10); 219 220 } else { 221 /* Volume 7 */ 222 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7); 223 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7); 224 225 /* Enable Mic */ 226 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1); 227 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1); 228 } 229 230 return 0; 231 } 232 233 /* 234 * Set PCM DAI word length. 235 */ 236 static int da7210_hw_params(struct snd_pcm_substream *substream, 237 struct snd_pcm_hw_params *params, 238 struct snd_soc_dai *dai) 239 { 240 struct snd_soc_pcm_runtime *rtd = substream->private_data; 241 struct snd_soc_device *socdev = rtd->socdev; 242 struct snd_soc_codec *codec = socdev->card->codec; 243 u32 dai_cfg1; 244 u32 reg, mask; 245 246 /* set DAI source to Left and Right ADC */ 247 da7210_write(codec, DA7210_DAI_SRC_SEL, 248 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC); 249 250 /* Enable DAI */ 251 da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN); 252 253 dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1); 254 255 switch (params_format(params)) { 256 case SNDRV_PCM_FORMAT_S16_LE: 257 dai_cfg1 |= DA7210_DAI_WORD_S16_LE; 258 break; 259 case SNDRV_PCM_FORMAT_S24_LE: 260 dai_cfg1 |= DA7210_DAI_WORD_S24_LE; 261 break; 262 default: 263 return -EINVAL; 264 } 265 266 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); 267 268 /* FIXME 269 * 270 * It support 48K only now 271 */ 272 switch (params_rate(params)) { 273 case 48000: 274 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { 275 reg = DA7210_DAC_HPF; 276 mask = DA7210_DAC_VOICE_EN; 277 } else { 278 reg = DA7210_ADC_HPF; 279 mask = DA7210_ADC_VOICE_EN; 280 } 281 break; 282 default: 283 return -EINVAL; 284 } 285 286 snd_soc_update_bits(codec, reg, mask, 0); 287 288 return 0; 289 } 290 291 /* 292 * Set DAI mode and Format 293 */ 294 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt) 295 { 296 struct snd_soc_codec *codec = codec_dai->codec; 297 u32 dai_cfg1; 298 u32 dai_cfg3; 299 300 dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1); 301 dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3); 302 303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 304 case SND_SOC_DAIFMT_CBM_CFM: 305 dai_cfg1 |= DA7210_DAI_MODE_MASTER; 306 break; 307 default: 308 return -EINVAL; 309 } 310 311 /* FIXME 312 * 313 * It support I2S only now 314 */ 315 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 316 case SND_SOC_DAIFMT_I2S: 317 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE; 318 break; 319 default: 320 return -EINVAL; 321 } 322 323 /* FIXME 324 * 325 * It support 64bit data transmission only now 326 */ 327 dai_cfg1 |= DA7210_DAI_FLEN_64BIT; 328 329 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); 330 da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3); 331 332 return 0; 333 } 334 335 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) 336 337 /* DAI operations */ 338 static struct snd_soc_dai_ops da7210_dai_ops = { 339 .startup = da7210_startup, 340 .hw_params = da7210_hw_params, 341 .set_fmt = da7210_set_dai_fmt, 342 }; 343 344 struct snd_soc_dai da7210_dai = { 345 .name = "DA7210 IIS", 346 .id = 0, 347 /* playback capabilities */ 348 .playback = { 349 .stream_name = "Playback", 350 .channels_min = 1, 351 .channels_max = 2, 352 .rates = SNDRV_PCM_RATE_8000_96000, 353 .formats = DA7210_FORMATS, 354 }, 355 /* capture capabilities */ 356 .capture = { 357 .stream_name = "Capture", 358 .channels_min = 1, 359 .channels_max = 2, 360 .rates = SNDRV_PCM_RATE_8000_96000, 361 .formats = DA7210_FORMATS, 362 }, 363 .ops = &da7210_dai_ops, 364 }; 365 EXPORT_SYMBOL_GPL(da7210_dai); 366 367 /* 368 * Initialize the DA7210 driver 369 * register the mixer and dsp interfaces with the kernel 370 */ 371 static int da7210_init(struct da7210_priv *da7210) 372 { 373 struct snd_soc_codec *codec = &da7210->codec; 374 int ret = 0; 375 376 if (da7210_codec) { 377 dev_err(codec->dev, "Another da7210 is registered\n"); 378 return -EINVAL; 379 } 380 381 mutex_init(&codec->mutex); 382 INIT_LIST_HEAD(&codec->dapm_widgets); 383 INIT_LIST_HEAD(&codec->dapm_paths); 384 385 codec->private_data = da7210; 386 codec->name = "DA7210"; 387 codec->owner = THIS_MODULE; 388 codec->read = da7210_read; 389 codec->write = da7210_write; 390 codec->dai = &da7210_dai; 391 codec->num_dai = 1; 392 codec->hw_write = (hw_write_t)i2c_master_send; 393 codec->reg_cache_size = ARRAY_SIZE(da7210_reg); 394 codec->reg_cache = kmemdup(da7210_reg, 395 sizeof(da7210_reg), GFP_KERNEL); 396 397 if (!codec->reg_cache) 398 return -ENOMEM; 399 400 da7210_dai.dev = codec->dev; 401 da7210_codec = codec; 402 403 ret = snd_soc_register_codec(codec); 404 if (ret) { 405 dev_err(codec->dev, "Failed to register CODEC: %d\n", ret); 406 goto init_err; 407 } 408 409 ret = snd_soc_register_dai(&da7210_dai); 410 if (ret) { 411 dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 412 goto init_err; 413 } 414 415 /* FIXME 416 * 417 * This driver use fixed value here 418 */ 419 420 /* 421 * ADC settings 422 */ 423 424 /* Enable Left & Right MIC PGA and Mic Bias */ 425 da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN); 426 da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN); 427 428 /* Enable Left and Right input PGA */ 429 da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN); 430 da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN); 431 432 /* Enable Left and Right ADC */ 433 da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN); 434 435 /* 436 * DAC settings 437 */ 438 439 /* Enable Left and Right DAC */ 440 da7210_write(codec, DA7210_DAC_SEL, 441 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN | 442 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN); 443 444 /* Enable Left and Right out PGA */ 445 da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN); 446 da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN); 447 448 /* Enable Left and Right HeadPhone PGA */ 449 da7210_write(codec, DA7210_HP_CFG, 450 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN | 451 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN); 452 453 /* Diable PLL and bypass it */ 454 da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000); 455 456 /* Bypass PLL and set MCLK freq rang to 10-20MHz */ 457 da7210_write(codec, DA7210_PLL_DIV3, 458 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP); 459 460 /* Activate all enabled subsystem */ 461 da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN); 462 463 return ret; 464 465 init_err: 466 kfree(codec->reg_cache); 467 codec->reg_cache = NULL; 468 469 return ret; 470 471 } 472 473 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 474 static int __devinit da7210_i2c_probe(struct i2c_client *i2c, 475 const struct i2c_device_id *id) 476 { 477 struct da7210_priv *da7210; 478 struct snd_soc_codec *codec; 479 int ret; 480 481 da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL); 482 if (!da7210) 483 return -ENOMEM; 484 485 codec = &da7210->codec; 486 codec->dev = &i2c->dev; 487 488 i2c_set_clientdata(i2c, da7210); 489 codec->control_data = i2c; 490 491 ret = da7210_init(da7210); 492 if (ret < 0) 493 pr_err("Failed to initialise da7210 audio codec\n"); 494 495 return ret; 496 } 497 498 static int __devexit da7210_i2c_remove(struct i2c_client *client) 499 { 500 struct da7210_priv *da7210 = i2c_get_clientdata(client); 501 502 snd_soc_unregister_dai(&da7210_dai); 503 kfree(da7210->codec.reg_cache); 504 kfree(da7210); 505 da7210_codec = NULL; 506 507 return 0; 508 } 509 510 static const struct i2c_device_id da7210_i2c_id[] = { 511 { "da7210", 0 }, 512 { } 513 }; 514 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id); 515 516 /* I2C codec control layer */ 517 static struct i2c_driver da7210_i2c_driver = { 518 .driver = { 519 .name = "DA7210 I2C Codec", 520 .owner = THIS_MODULE, 521 }, 522 .probe = da7210_i2c_probe, 523 .remove = __devexit_p(da7210_i2c_remove), 524 .id_table = da7210_i2c_id, 525 }; 526 #endif 527 528 static int da7210_probe(struct platform_device *pdev) 529 { 530 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 531 struct snd_soc_codec *codec; 532 int ret; 533 534 if (!da7210_codec) { 535 dev_err(&pdev->dev, "Codec device not registered\n"); 536 return -ENODEV; 537 } 538 539 socdev->card->codec = da7210_codec; 540 codec = da7210_codec; 541 542 /* Register pcms */ 543 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 544 if (ret < 0) 545 goto pcm_err; 546 547 dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION); 548 549 pcm_err: 550 return ret; 551 } 552 553 static int da7210_remove(struct platform_device *pdev) 554 { 555 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 556 557 snd_soc_free_pcms(socdev); 558 snd_soc_dapm_free(socdev); 559 560 return 0; 561 } 562 563 struct snd_soc_codec_device soc_codec_dev_da7210 = { 564 .probe = da7210_probe, 565 .remove = da7210_remove, 566 }; 567 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210); 568 569 static int __init da7210_modinit(void) 570 { 571 int ret = 0; 572 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 573 ret = i2c_add_driver(&da7210_i2c_driver); 574 #endif 575 return ret; 576 } 577 module_init(da7210_modinit); 578 579 static void __exit da7210_exit(void) 580 { 581 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 582 i2c_del_driver(&da7210_i2c_driver); 583 #endif 584 } 585 module_exit(da7210_exit); 586 587 MODULE_DESCRIPTION("ASoC DA7210 driver"); 588 MODULE_AUTHOR("David Chen, Kuninori Morimoto"); 589 MODULE_LICENSE("GPL"); 590