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