1 /* 2 * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver 3 * 4 * Copyright (C) 2009 Renesas Solutions Corp. 5 * Kuninori Morimoto <morimoto.kuninori@renesas.com> 6 * 7 * Based on wm8731.c by Richard Purdie 8 * Based on ak4535.c by Richard Purdie 9 * Based on wm8753.c by Liam Girdwood 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 */ 15 16 /* ** CAUTION ** 17 * 18 * This is very simple driver. 19 * It can use headphone output / stereo input only 20 * 21 * AK4642 is not tested. 22 * AK4643 is tested. 23 */ 24 25 #include <linux/delay.h> 26 #include <linux/i2c.h> 27 #include <linux/platform_device.h> 28 #include <linux/slab.h> 29 #include <sound/soc-dapm.h> 30 #include <sound/initval.h> 31 #include <sound/tlv.h> 32 33 #include "ak4642.h" 34 35 #define AK4642_VERSION "0.0.1" 36 37 #define PW_MGMT1 0x00 38 #define PW_MGMT2 0x01 39 #define SG_SL1 0x02 40 #define SG_SL2 0x03 41 #define MD_CTL1 0x04 42 #define MD_CTL2 0x05 43 #define TIMER 0x06 44 #define ALC_CTL1 0x07 45 #define ALC_CTL2 0x08 46 #define L_IVC 0x09 47 #define L_DVC 0x0a 48 #define ALC_CTL3 0x0b 49 #define R_IVC 0x0c 50 #define R_DVC 0x0d 51 #define MD_CTL3 0x0e 52 #define MD_CTL4 0x0f 53 #define PW_MGMT3 0x10 54 #define DF_S 0x11 55 #define FIL3_0 0x12 56 #define FIL3_1 0x13 57 #define FIL3_2 0x14 58 #define FIL3_3 0x15 59 #define EQ_0 0x16 60 #define EQ_1 0x17 61 #define EQ_2 0x18 62 #define EQ_3 0x19 63 #define EQ_4 0x1a 64 #define EQ_5 0x1b 65 #define FIL1_0 0x1c 66 #define FIL1_1 0x1d 67 #define FIL1_2 0x1e 68 #define FIL1_3 0x1f 69 #define PW_MGMT4 0x20 70 #define MD_CTL5 0x21 71 #define LO_MS 0x22 72 #define HP_MS 0x23 73 #define SPK_MS 0x24 74 75 #define AK4642_CACHEREGNUM 0x25 76 77 /* PW_MGMT2 */ 78 #define HPMTN (1 << 6) 79 #define PMHPL (1 << 5) 80 #define PMHPR (1 << 4) 81 #define MS (1 << 3) /* master/slave select */ 82 #define MCKO (1 << 1) 83 #define PMPLL (1 << 0) 84 85 #define PMHP_MASK (PMHPL | PMHPR) 86 #define PMHP PMHP_MASK 87 88 /* MD_CTL1 */ 89 #define PLL3 (1 << 7) 90 #define PLL2 (1 << 6) 91 #define PLL1 (1 << 5) 92 #define PLL0 (1 << 4) 93 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0) 94 95 #define BCKO_MASK (1 << 3) 96 #define BCKO_64 BCKO_MASK 97 98 /* MD_CTL2 */ 99 #define FS0 (1 << 0) 100 #define FS1 (1 << 1) 101 #define FS2 (1 << 2) 102 #define FS3 (1 << 5) 103 #define FS_MASK (FS0 | FS1 | FS2 | FS3) 104 105 struct snd_soc_codec_device soc_codec_dev_ak4642; 106 107 /* 108 * Playback Volume (table 39) 109 * 110 * max : 0x00 : +12.0 dB 111 * ( 0.5 dB step ) 112 * min : 0xFE : -115.0 dB 113 * mute: 0xFF 114 */ 115 static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1); 116 117 static const struct snd_kcontrol_new ak4642_snd_controls[] = { 118 119 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC, 120 0, 0xFF, 1, out_tlv), 121 }; 122 123 124 /* codec private data */ 125 struct ak4642_priv { 126 struct snd_soc_codec codec; 127 }; 128 129 static struct snd_soc_codec *ak4642_codec; 130 131 /* 132 * ak4642 register cache 133 */ 134 static const u16 ak4642_reg[AK4642_CACHEREGNUM] = { 135 0x0000, 0x0000, 0x0001, 0x0000, 136 0x0002, 0x0000, 0x0000, 0x0000, 137 0x00e1, 0x00e1, 0x0018, 0x0000, 138 0x00e1, 0x0018, 0x0011, 0x0008, 139 0x0000, 0x0000, 0x0000, 0x0000, 140 0x0000, 0x0000, 0x0000, 0x0000, 141 0x0000, 0x0000, 0x0000, 0x0000, 142 0x0000, 0x0000, 0x0000, 0x0000, 143 0x0000, 0x0000, 0x0000, 0x0000, 144 0x0000, 145 }; 146 147 /* 148 * read ak4642 register cache 149 */ 150 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec, 151 unsigned int reg) 152 { 153 u16 *cache = codec->reg_cache; 154 if (reg >= AK4642_CACHEREGNUM) 155 return -1; 156 return cache[reg]; 157 } 158 159 /* 160 * write ak4642 register cache 161 */ 162 static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec, 163 u16 reg, unsigned int value) 164 { 165 u16 *cache = codec->reg_cache; 166 if (reg >= AK4642_CACHEREGNUM) 167 return; 168 169 cache[reg] = value; 170 } 171 172 /* 173 * write to the AK4642 register space 174 */ 175 static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg, 176 unsigned int value) 177 { 178 u8 data[2]; 179 180 /* data is 181 * D15..D8 AK4642 register offset 182 * D7...D0 register data 183 */ 184 data[0] = reg & 0xff; 185 data[1] = value & 0xff; 186 187 if (codec->hw_write(codec->control_data, data, 2) == 2) { 188 ak4642_write_reg_cache(codec, reg, value); 189 return 0; 190 } else 191 return -EIO; 192 } 193 194 static int ak4642_sync(struct snd_soc_codec *codec) 195 { 196 u16 *cache = codec->reg_cache; 197 int i, r = 0; 198 199 for (i = 0; i < AK4642_CACHEREGNUM; i++) 200 r |= ak4642_write(codec, i, cache[i]); 201 202 return r; 203 }; 204 205 static int ak4642_dai_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 /* 213 * start headphone output 214 * 215 * PLL, Master Mode 216 * Audio I/F Format :MSB justified (ADC & DAC) 217 * Bass Boost Level : Middle 218 * 219 * This operation came from example code of 220 * "ASAHI KASEI AK4642" (japanese) manual p97. 221 */ 222 ak4642_write(codec, 0x0f, 0x09); 223 ak4642_write(codec, 0x0e, 0x19); 224 ak4642_write(codec, 0x09, 0x91); 225 ak4642_write(codec, 0x0c, 0x91); 226 ak4642_write(codec, 0x00, 0x64); 227 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP); 228 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN); 229 } else { 230 /* 231 * start stereo input 232 * 233 * PLL Master Mode 234 * Audio I/F Format:MSB justified (ADC & DAC) 235 * Pre MIC AMP:+20dB 236 * MIC Power On 237 * ALC setting:Refer to Table 35 238 * ALC bit=“1” 239 * 240 * This operation came from example code of 241 * "ASAHI KASEI AK4642" (japanese) manual p94. 242 */ 243 ak4642_write(codec, 0x02, 0x05); 244 ak4642_write(codec, 0x06, 0x3c); 245 ak4642_write(codec, 0x08, 0xe1); 246 ak4642_write(codec, 0x0b, 0x00); 247 ak4642_write(codec, 0x07, 0x21); 248 ak4642_write(codec, 0x00, 0x41); 249 ak4642_write(codec, 0x10, 0x01); 250 } 251 252 return 0; 253 } 254 255 static void ak4642_dai_shutdown(struct snd_pcm_substream *substream, 256 struct snd_soc_dai *dai) 257 { 258 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 259 struct snd_soc_codec *codec = dai->codec; 260 261 if (is_play) { 262 /* stop headphone output */ 263 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0); 264 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0); 265 ak4642_write(codec, 0x00, 0x40); 266 ak4642_write(codec, 0x0e, 0x11); 267 ak4642_write(codec, 0x0f, 0x08); 268 } else { 269 /* stop stereo input */ 270 ak4642_write(codec, 0x00, 0x40); 271 ak4642_write(codec, 0x10, 0x00); 272 ak4642_write(codec, 0x07, 0x01); 273 } 274 } 275 276 static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai, 277 int clk_id, unsigned int freq, int dir) 278 { 279 struct snd_soc_codec *codec = codec_dai->codec; 280 u8 pll; 281 282 switch (freq) { 283 case 11289600: 284 pll = PLL2; 285 break; 286 case 12288000: 287 pll = PLL2 | PLL0; 288 break; 289 case 12000000: 290 pll = PLL2 | PLL1; 291 break; 292 case 24000000: 293 pll = PLL2 | PLL1 | PLL0; 294 break; 295 case 13500000: 296 pll = PLL3 | PLL2; 297 break; 298 case 27000000: 299 pll = PLL3 | PLL2 | PLL0; 300 break; 301 default: 302 return -EINVAL; 303 } 304 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll); 305 306 return 0; 307 } 308 309 static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 310 { 311 struct snd_soc_codec *codec = dai->codec; 312 u8 data; 313 u8 bcko; 314 315 data = MCKO | PMPLL; /* use MCKO */ 316 bcko = 0; 317 318 /* set master/slave audio interface */ 319 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 320 case SND_SOC_DAIFMT_CBM_CFM: 321 data |= MS; 322 bcko = BCKO_64; 323 break; 324 case SND_SOC_DAIFMT_CBS_CFS: 325 break; 326 default: 327 return -EINVAL; 328 } 329 snd_soc_update_bits(codec, PW_MGMT2, MS, data); 330 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko); 331 332 return 0; 333 } 334 335 static int ak4642_dai_hw_params(struct snd_pcm_substream *substream, 336 struct snd_pcm_hw_params *params, 337 struct snd_soc_dai *dai) 338 { 339 struct snd_soc_codec *codec = dai->codec; 340 u8 rate; 341 342 switch (params_rate(params)) { 343 case 7350: 344 rate = FS2; 345 break; 346 case 8000: 347 rate = 0; 348 break; 349 case 11025: 350 rate = FS2 | FS0; 351 break; 352 case 12000: 353 rate = FS0; 354 break; 355 case 14700: 356 rate = FS2 | FS1; 357 break; 358 case 16000: 359 rate = FS1; 360 break; 361 case 22050: 362 rate = FS2 | FS1 | FS0; 363 break; 364 case 24000: 365 rate = FS1 | FS0; 366 break; 367 case 29400: 368 rate = FS3 | FS2 | FS1; 369 break; 370 case 32000: 371 rate = FS3 | FS1; 372 break; 373 case 44100: 374 rate = FS3 | FS2 | FS1 | FS0; 375 break; 376 case 48000: 377 rate = FS3 | FS1 | FS0; 378 break; 379 default: 380 return -EINVAL; 381 break; 382 } 383 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate); 384 385 return 0; 386 } 387 388 static struct snd_soc_dai_ops ak4642_dai_ops = { 389 .startup = ak4642_dai_startup, 390 .shutdown = ak4642_dai_shutdown, 391 .set_sysclk = ak4642_dai_set_sysclk, 392 .set_fmt = ak4642_dai_set_fmt, 393 .hw_params = ak4642_dai_hw_params, 394 }; 395 396 struct snd_soc_dai ak4642_dai = { 397 .name = "AK4642", 398 .playback = { 399 .stream_name = "Playback", 400 .channels_min = 1, 401 .channels_max = 2, 402 .rates = SNDRV_PCM_RATE_8000_48000, 403 .formats = SNDRV_PCM_FMTBIT_S16_LE }, 404 .capture = { 405 .stream_name = "Capture", 406 .channels_min = 1, 407 .channels_max = 2, 408 .rates = SNDRV_PCM_RATE_8000_48000, 409 .formats = SNDRV_PCM_FMTBIT_S16_LE }, 410 .ops = &ak4642_dai_ops, 411 .symmetric_rates = 1, 412 }; 413 EXPORT_SYMBOL_GPL(ak4642_dai); 414 415 static int ak4642_resume(struct platform_device *pdev) 416 { 417 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 418 struct snd_soc_codec *codec = socdev->card->codec; 419 420 ak4642_sync(codec); 421 return 0; 422 } 423 424 /* 425 * initialise the AK4642 driver 426 * register the mixer and dsp interfaces with the kernel 427 */ 428 static int ak4642_init(struct ak4642_priv *ak4642) 429 { 430 struct snd_soc_codec *codec = &ak4642->codec; 431 int ret = 0; 432 433 if (ak4642_codec) { 434 dev_err(codec->dev, "Another ak4642 is registered\n"); 435 return -EINVAL; 436 } 437 438 mutex_init(&codec->mutex); 439 INIT_LIST_HEAD(&codec->dapm_widgets); 440 INIT_LIST_HEAD(&codec->dapm_paths); 441 442 snd_soc_codec_set_drvdata(codec, ak4642); 443 codec->name = "AK4642"; 444 codec->owner = THIS_MODULE; 445 codec->read = ak4642_read_reg_cache; 446 codec->write = ak4642_write; 447 codec->dai = &ak4642_dai; 448 codec->num_dai = 1; 449 codec->hw_write = (hw_write_t)i2c_master_send; 450 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg); 451 codec->reg_cache = kmemdup(ak4642_reg, 452 sizeof(ak4642_reg), GFP_KERNEL); 453 454 if (!codec->reg_cache) 455 return -ENOMEM; 456 457 ak4642_dai.dev = codec->dev; 458 ak4642_codec = codec; 459 460 ret = snd_soc_register_codec(codec); 461 if (ret) { 462 dev_err(codec->dev, "Failed to register codec: %d\n", ret); 463 goto reg_cache_err; 464 } 465 466 ret = snd_soc_register_dai(&ak4642_dai); 467 if (ret) { 468 dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 469 snd_soc_unregister_codec(codec); 470 goto reg_cache_err; 471 } 472 473 return ret; 474 475 reg_cache_err: 476 kfree(codec->reg_cache); 477 codec->reg_cache = NULL; 478 479 return ret; 480 } 481 482 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 483 static int ak4642_i2c_probe(struct i2c_client *i2c, 484 const struct i2c_device_id *id) 485 { 486 struct ak4642_priv *ak4642; 487 struct snd_soc_codec *codec; 488 int ret; 489 490 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL); 491 if (!ak4642) 492 return -ENOMEM; 493 494 codec = &ak4642->codec; 495 codec->dev = &i2c->dev; 496 497 i2c_set_clientdata(i2c, ak4642); 498 codec->control_data = i2c; 499 500 ret = ak4642_init(ak4642); 501 if (ret < 0) { 502 printk(KERN_ERR "failed to initialise AK4642\n"); 503 kfree(ak4642); 504 } 505 506 return ret; 507 } 508 509 static int ak4642_i2c_remove(struct i2c_client *client) 510 { 511 struct ak4642_priv *ak4642 = i2c_get_clientdata(client); 512 513 snd_soc_unregister_dai(&ak4642_dai); 514 snd_soc_unregister_codec(&ak4642->codec); 515 kfree(ak4642->codec.reg_cache); 516 kfree(ak4642); 517 ak4642_codec = NULL; 518 519 return 0; 520 } 521 522 static const struct i2c_device_id ak4642_i2c_id[] = { 523 { "ak4642", 0 }, 524 { "ak4643", 0 }, 525 { } 526 }; 527 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id); 528 529 static struct i2c_driver ak4642_i2c_driver = { 530 .driver = { 531 .name = "AK4642 I2C Codec", 532 .owner = THIS_MODULE, 533 }, 534 .probe = ak4642_i2c_probe, 535 .remove = ak4642_i2c_remove, 536 .id_table = ak4642_i2c_id, 537 }; 538 539 #endif 540 541 static int ak4642_probe(struct platform_device *pdev) 542 { 543 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 544 int ret; 545 546 if (!ak4642_codec) { 547 dev_err(&pdev->dev, "Codec device not registered\n"); 548 return -ENODEV; 549 } 550 551 socdev->card->codec = ak4642_codec; 552 553 /* register pcms */ 554 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 555 if (ret < 0) { 556 printk(KERN_ERR "ak4642: failed to create pcms\n"); 557 goto pcm_err; 558 } 559 560 snd_soc_add_controls(ak4642_codec, ak4642_snd_controls, 561 ARRAY_SIZE(ak4642_snd_controls)); 562 563 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION); 564 return ret; 565 566 pcm_err: 567 return ret; 568 569 } 570 571 /* power down chip */ 572 static int ak4642_remove(struct platform_device *pdev) 573 { 574 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 575 576 snd_soc_free_pcms(socdev); 577 snd_soc_dapm_free(socdev); 578 579 return 0; 580 } 581 582 struct snd_soc_codec_device soc_codec_dev_ak4642 = { 583 .probe = ak4642_probe, 584 .remove = ak4642_remove, 585 .resume = ak4642_resume, 586 }; 587 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642); 588 589 static int __init ak4642_modinit(void) 590 { 591 int ret = 0; 592 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 593 ret = i2c_add_driver(&ak4642_i2c_driver); 594 #endif 595 return ret; 596 597 } 598 module_init(ak4642_modinit); 599 600 static void __exit ak4642_exit(void) 601 { 602 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 603 i2c_del_driver(&ak4642_i2c_driver); 604 #endif 605 606 } 607 module_exit(ak4642_exit); 608 609 MODULE_DESCRIPTION("Soc AK4642 driver"); 610 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>"); 611 MODULE_LICENSE("GPL"); 612