1 /* 2 * max9867.c -- max9867 ALSA SoC Audio driver 3 * 4 * Copyright 2013-15 Maxim Integrated Products 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/i2c.h> 13 #include <linux/module.h> 14 #include <linux/regmap.h> 15 #include <sound/pcm_params.h> 16 #include <sound/soc.h> 17 #include <sound/tlv.h> 18 #include "max9867.h" 19 20 static const char *const max9867_spmode[] = { 21 "Stereo Diff", "Mono Diff", 22 "Stereo Cap", "Mono Cap", 23 "Stereo Single", "Mono Single", 24 "Stereo Single Fast", "Mono Single Fast" 25 }; 26 static const char *const max9867_sidetone_text[] = { 27 "None", "Left", "Right", "LeftRight", "LeftRightDiv2", 28 }; 29 static const char *const max9867_filter_text[] = {"IIR", "FIR"}; 30 31 static SOC_ENUM_SINGLE_DECL(max9867_filter, MAX9867_CODECFLTR, 7, 32 max9867_filter_text); 33 static SOC_ENUM_SINGLE_DECL(max9867_spkmode, MAX9867_MODECONFIG, 0, 34 max9867_spmode); 35 static SOC_ENUM_SINGLE_DECL(max9867_sidetone, MAX9867_DACGAIN, 6, 36 max9867_sidetone_text); 37 static DECLARE_TLV_DB_SCALE(max9860_capture_tlv, -600, 200, 0); 38 static DECLARE_TLV_DB_SCALE(max9860_mic_tlv, 2000, 100, 1); 39 static DECLARE_TLV_DB_SCALE(max9860_adc_left_tlv, -1200, 100, 1); 40 static DECLARE_TLV_DB_SCALE(max9860_adc_right_tlv, -1200, 100, 1); 41 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max98088_micboost_tlv, 42 0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0), 43 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0), 44 ); 45 46 static const struct snd_kcontrol_new max9867_snd_controls[] = { 47 SOC_DOUBLE_R("Master Playback Volume", MAX9867_LEFTVOL, 48 MAX9867_RIGHTVOL, 0, 63, 1), 49 SOC_DOUBLE_R_TLV("Capture Volume", MAX9867_LEFTMICGAIN, 50 MAX9867_RIGHTMICGAIN, 51 0, 15, 1, max9860_capture_tlv), 52 SOC_DOUBLE_R_TLV("Mic Volume", MAX9867_LEFTMICGAIN, 53 MAX9867_RIGHTMICGAIN, 0, 31, 1, max9860_mic_tlv), 54 SOC_DOUBLE_R_TLV("Mic Boost Volume", MAX9867_LEFTMICGAIN, 55 MAX9867_RIGHTMICGAIN, 5, 3, 0, max98088_micboost_tlv), 56 SOC_ENUM("Digital Sidetone Src", max9867_sidetone), 57 SOC_SINGLE("Sidetone Volume", MAX9867_DACGAIN, 0, 31, 1), 58 SOC_SINGLE("DAC Volume", MAX9867_DACLEVEL, 4, 3, 0), 59 SOC_SINGLE("DAC Attenuation", MAX9867_DACLEVEL, 0, 15, 1), 60 SOC_SINGLE_TLV("ADC Left Volume", MAX9867_ADCLEVEL, 61 4, 15, 1, max9860_adc_left_tlv), 62 SOC_SINGLE_TLV("ADC Right Volume", MAX9867_ADCLEVEL, 63 0, 15, 1, max9860_adc_right_tlv), 64 SOC_ENUM("Speaker Mode", max9867_spkmode), 65 SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG, 6, 1, 0), 66 SOC_SINGLE("ZCD Switch", MAX9867_MODECONFIG, 5, 1, 0), 67 SOC_ENUM("DSP Filter", max9867_filter), 68 }; 69 70 static const char *const max9867_mux[] = {"None", "Mic", "Line", "Mic_Line"}; 71 72 static SOC_ENUM_SINGLE_DECL(max9867_mux_enum, 73 MAX9867_INPUTCONFIG, MAX9867_INPUT_SHIFT, 74 max9867_mux); 75 76 static const struct snd_kcontrol_new max9867_dapm_mux_controls = 77 SOC_DAPM_ENUM("Route", max9867_mux_enum); 78 79 static const struct snd_kcontrol_new max9867_left_dapm_control = 80 SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 6, 1, 0); 81 static const struct snd_kcontrol_new max9867_right_dapm_control = 82 SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 5, 1, 0); 83 static const struct snd_kcontrol_new max9867_line_dapm_control = 84 SOC_DAPM_SINGLE("Switch", MAX9867_LEFTLINELVL, 6, 1, 1); 85 86 static const struct snd_soc_dapm_widget max9867_dapm_widgets[] = { 87 SND_SOC_DAPM_AIF_IN("DAI_OUT", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0), 88 SND_SOC_DAPM_DAC("Left DAC", NULL, MAX9867_PWRMAN, 3, 0), 89 SND_SOC_DAPM_DAC("Right DAC", NULL, MAX9867_PWRMAN, 2, 0), 90 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), 91 SND_SOC_DAPM_OUTPUT("HPOUT"), 92 93 SND_SOC_DAPM_AIF_IN("DAI_IN", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0), 94 SND_SOC_DAPM_ADC("Left ADC", "HiFi Capture", MAX9867_PWRMAN, 1, 0), 95 SND_SOC_DAPM_ADC("Right ADC", "HiFi Capture", MAX9867_PWRMAN, 0, 0), 96 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, 97 &max9867_dapm_mux_controls), 98 99 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), 100 SND_SOC_DAPM_SWITCH("Left Line", MAX9867_LEFTLINELVL, 6, 1, 101 &max9867_left_dapm_control), 102 SND_SOC_DAPM_SWITCH("Right Line", MAX9867_RIGTHLINELVL, 6, 1, 103 &max9867_right_dapm_control), 104 SND_SOC_DAPM_SWITCH("Line Mixer", SND_SOC_NOPM, 0, 0, 105 &max9867_line_dapm_control), 106 SND_SOC_DAPM_INPUT("LINE_IN"), 107 }; 108 109 static const struct snd_soc_dapm_route max9867_audio_map[] = { 110 {"Left DAC", NULL, "DAI_OUT"}, 111 {"Right DAC", NULL, "DAI_OUT"}, 112 {"Output Mixer", NULL, "Left DAC"}, 113 {"Output Mixer", NULL, "Right DAC"}, 114 {"HPOUT", NULL, "Output Mixer"}, 115 116 {"Left ADC", NULL, "DAI_IN"}, 117 {"Right ADC", NULL, "DAI_IN"}, 118 {"Input Mixer", NULL, "Left ADC"}, 119 {"Input Mixer", NULL, "Right ADC"}, 120 {"Input Mux", "Line", "Input Mixer"}, 121 {"Input Mux", "Mic", "Input Mixer"}, 122 {"Input Mux", "Mic_Line", "Input Mixer"}, 123 {"Right Line", "Switch", "Input Mux"}, 124 {"Left Line", "Switch", "Input Mux"}, 125 {"LINE_IN", NULL, "Left Line"}, 126 {"LINE_IN", NULL, "Right Line"}, 127 }; 128 129 enum rates { 130 pcm_rate_8, pcm_rate_16, pcm_rate_24, 131 pcm_rate_32, pcm_rate_44, 132 pcm_rate_48, max_pcm_rate, 133 }; 134 135 static const struct ni_div_rates { 136 u32 mclk; 137 u16 ni[max_pcm_rate]; 138 } ni_div[] = { 139 {11289600, {0x116A, 0x22D4, 0x343F, 0x45A9, 0x6000, 0x687D} }, 140 {12000000, {0x1062, 0x20C5, 0x3127, 0x4189, 0x5A51, 0x624E} }, 141 {12288000, {0x1000, 0x2000, 0x3000, 0x4000, 0x5833, 0x6000} }, 142 {13000000, {0x0F20, 0x1E3F, 0x2D5F, 0x3C7F, 0x535F, 0x5ABE} }, 143 {19200000, {0x0A3D, 0x147B, 0x1EB8, 0x28F6, 0x3873, 0x3D71} }, 144 {24000000, {0x1062, 0x20C5, 0x1893, 0x4189, 0x5A51, 0x624E} }, 145 {26000000, {0x0F20, 0x1E3F, 0x16AF, 0x3C7F, 0x535F, 0x5ABE} }, 146 {27000000, {0x0E90, 0x1D21, 0x15D8, 0x3A41, 0x5048, 0x5762} }, 147 }; 148 149 static inline int get_ni_value(int mclk, int rate) 150 { 151 int i, ret = 0; 152 153 /* find the closest rate index*/ 154 for (i = 0; i < ARRAY_SIZE(ni_div); i++) { 155 if (ni_div[i].mclk >= mclk) 156 break; 157 } 158 if (i == ARRAY_SIZE(ni_div)) 159 return -EINVAL; 160 161 switch (rate) { 162 case 8000: 163 return ni_div[i].ni[pcm_rate_8]; 164 case 16000: 165 return ni_div[i].ni[pcm_rate_16]; 166 case 32000: 167 return ni_div[i].ni[pcm_rate_32]; 168 case 44100: 169 return ni_div[i].ni[pcm_rate_44]; 170 case 48000: 171 return ni_div[i].ni[pcm_rate_48]; 172 default: 173 pr_err("%s wrong rate %d\n", __func__, rate); 174 ret = -EINVAL; 175 } 176 return ret; 177 } 178 179 static int max9867_dai_hw_params(struct snd_pcm_substream *substream, 180 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 181 { 182 struct snd_soc_component *component = dai->component; 183 struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component); 184 unsigned int ni_h, ni_l; 185 int value; 186 187 value = get_ni_value(max9867->sysclk, params_rate(params)); 188 if (value < 0) 189 return value; 190 191 ni_h = (0xFF00 & value) >> 8; 192 ni_l = 0x00FF & value; 193 /* set up the ni value */ 194 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH, 195 MAX9867_NI_HIGH_MASK, ni_h); 196 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW, 197 MAX9867_NI_LOW_MASK, ni_l); 198 if (!max9867->master) { 199 /* 200 * digital pll locks on to any externally supplied LRCLK signal 201 * and also enable rapid lock mode. 202 */ 203 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW, 204 MAX9867_RAPID_LOCK, MAX9867_RAPID_LOCK); 205 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH, 206 MAX9867_PLL, MAX9867_PLL); 207 } else { 208 unsigned long int bclk_rate, pclk_bclk_ratio; 209 int bclk_value; 210 211 bclk_rate = params_rate(params) * 2 * params_width(params); 212 pclk_bclk_ratio = max9867->pclk/bclk_rate; 213 switch (params_width(params)) { 214 case 8: 215 case 16: 216 switch (pclk_bclk_ratio) { 217 case 2: 218 bclk_value = MAX9867_IFC1B_PCLK_2; 219 break; 220 case 4: 221 bclk_value = MAX9867_IFC1B_PCLK_4; 222 break; 223 case 8: 224 bclk_value = MAX9867_IFC1B_PCLK_8; 225 break; 226 case 16: 227 bclk_value = MAX9867_IFC1B_PCLK_16; 228 break; 229 default: 230 dev_err(component->dev, 231 "unsupported sampling rate\n"); 232 return -EINVAL; 233 } 234 break; 235 case 24: 236 bclk_value = MAX9867_IFC1B_24BIT; 237 break; 238 case 32: 239 bclk_value = MAX9867_IFC1B_32BIT; 240 break; 241 default: 242 dev_err(component->dev, "unsupported sampling rate\n"); 243 return -EINVAL; 244 } 245 regmap_update_bits(max9867->regmap, MAX9867_IFC1B, 246 MAX9867_IFC1B_BCLK_MASK, bclk_value); 247 } 248 return 0; 249 } 250 251 static int max9867_prepare(struct snd_pcm_substream *substream, 252 struct snd_soc_dai *dai) 253 { 254 struct snd_soc_component *component = dai->component; 255 struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component); 256 257 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN, 258 MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK); 259 return 0; 260 } 261 262 static int max9867_mute(struct snd_soc_dai *dai, int mute) 263 { 264 struct snd_soc_component *component = dai->component; 265 struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component); 266 267 if (mute) 268 regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL, 269 MAX9867_DAC_MUTE_MASK, MAX9867_DAC_MUTE_MASK); 270 else 271 regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL, 272 MAX9867_DAC_MUTE_MASK, 0); 273 return 0; 274 } 275 276 static int max9867_set_dai_sysclk(struct snd_soc_dai *codec_dai, 277 int clk_id, unsigned int freq, int dir) 278 { 279 struct snd_soc_component *component = codec_dai->component; 280 struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component); 281 int value = 0; 282 283 /* Set the prescaler based on the master clock frequency*/ 284 if (freq >= 10000000 && freq <= 20000000) { 285 value |= MAX9867_PSCLK_10_20; 286 max9867->pclk = freq; 287 } else if (freq >= 20000000 && freq <= 40000000) { 288 value |= MAX9867_PSCLK_20_40; 289 max9867->pclk = freq/2; 290 } else if (freq >= 40000000 && freq <= 60000000) { 291 value |= MAX9867_PSCLK_40_60; 292 max9867->pclk = freq/4; 293 } else { 294 dev_err(component->dev, 295 "Invalid clock frequency %uHz (required 10-60MHz)\n", 296 freq); 297 return -EINVAL; 298 } 299 value = value << MAX9867_PSCLK_SHIFT; 300 max9867->sysclk = freq; 301 /* exact integer mode is not supported */ 302 value &= ~MAX9867_FREQ_MASK; 303 regmap_update_bits(max9867->regmap, MAX9867_SYSCLK, 304 MAX9867_PSCLK_MASK, value); 305 return 0; 306 } 307 308 static int max9867_dai_set_fmt(struct snd_soc_dai *codec_dai, 309 unsigned int fmt) 310 { 311 struct snd_soc_component *component = codec_dai->component; 312 struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component); 313 u8 iface1A = 0, iface1B = 0; 314 315 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 316 case SND_SOC_DAIFMT_CBM_CFM: 317 max9867->master = 1; 318 iface1A |= MAX9867_MASTER; 319 break; 320 case SND_SOC_DAIFMT_CBS_CFS: 321 max9867->master = 0; 322 iface1A &= ~MAX9867_MASTER; 323 break; 324 default: 325 return -EINVAL; 326 } 327 328 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 329 case SND_SOC_DAIFMT_I2S: 330 iface1A |= MAX9867_I2S_DLY; 331 break; 332 case SND_SOC_DAIFMT_DSP_A: 333 iface1A |= MAX9867_TDM_MODE | MAX9867_SDOUT_HIZ; 334 break; 335 default: 336 return -EINVAL; 337 } 338 339 /* Clock inversion bits, BCI and WCI */ 340 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 341 case SND_SOC_DAIFMT_NB_NF: 342 break; 343 case SND_SOC_DAIFMT_IB_IF: 344 iface1A |= MAX9867_WCI_MODE | MAX9867_BCI_MODE; 345 break; 346 case SND_SOC_DAIFMT_IB_NF: 347 iface1A |= MAX9867_BCI_MODE; 348 break; 349 case SND_SOC_DAIFMT_NB_IF: 350 iface1A |= MAX9867_WCI_MODE; 351 break; 352 default: 353 return -EINVAL; 354 } 355 356 regmap_write(max9867->regmap, MAX9867_IFC1A, iface1A); 357 regmap_write(max9867->regmap, MAX9867_IFC1B, iface1B); 358 return 0; 359 } 360 361 static const struct snd_soc_dai_ops max9867_dai_ops = { 362 .set_fmt = max9867_dai_set_fmt, 363 .set_sysclk = max9867_set_dai_sysclk, 364 .prepare = max9867_prepare, 365 .digital_mute = max9867_mute, 366 .hw_params = max9867_dai_hw_params, 367 }; 368 369 #define MAX9867_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\ 370 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) 371 #define MAX9867_FORMATS (SNDRV_PCM_FMTBIT_S16_LE) 372 373 static struct snd_soc_dai_driver max9867_dai[] = { 374 { 375 .name = "max9867-aif1", 376 .playback = { 377 .stream_name = "HiFi Playback", 378 .channels_min = 2, 379 .channels_max = 2, 380 .rates = MAX9867_RATES, 381 .formats = MAX9867_FORMATS, 382 }, 383 .capture = { 384 .stream_name = "HiFi Capture", 385 .channels_min = 2, 386 .channels_max = 2, 387 .rates = MAX9867_RATES, 388 .formats = MAX9867_FORMATS, 389 }, 390 .ops = &max9867_dai_ops, 391 .symmetric_rates = 1, 392 } 393 }; 394 395 #ifdef CONFIG_PM_SLEEP 396 static int max9867_suspend(struct device *dev) 397 { 398 struct max9867_priv *max9867 = dev_get_drvdata(dev); 399 400 /* Drop down to power saving mode when system is suspended */ 401 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN, 402 MAX9867_SHTDOWN_MASK, ~MAX9867_SHTDOWN_MASK); 403 return 0; 404 } 405 406 static int max9867_resume(struct device *dev) 407 { 408 struct max9867_priv *max9867 = dev_get_drvdata(dev); 409 410 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN, 411 MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK); 412 return 0; 413 } 414 #endif 415 416 static const struct snd_soc_component_driver max9867_component = { 417 .controls = max9867_snd_controls, 418 .num_controls = ARRAY_SIZE(max9867_snd_controls), 419 .dapm_routes = max9867_audio_map, 420 .num_dapm_routes = ARRAY_SIZE(max9867_audio_map), 421 .dapm_widgets = max9867_dapm_widgets, 422 .num_dapm_widgets = ARRAY_SIZE(max9867_dapm_widgets), 423 .idle_bias_on = 1, 424 .use_pmdown_time = 1, 425 .endianness = 1, 426 .non_legacy_dai_naming = 1, 427 }; 428 429 static bool max9867_volatile_register(struct device *dev, unsigned int reg) 430 { 431 switch (reg) { 432 case MAX9867_STATUS: 433 case MAX9867_JACKSTATUS: 434 case MAX9867_AUXHIGH: 435 case MAX9867_AUXLOW: 436 return true; 437 default: 438 return false; 439 } 440 } 441 442 static const struct reg_default max9867_reg[] = { 443 { 0x04, 0x00 }, 444 { 0x05, 0x00 }, 445 { 0x06, 0x00 }, 446 { 0x07, 0x00 }, 447 { 0x08, 0x00 }, 448 { 0x09, 0x00 }, 449 { 0x0A, 0x00 }, 450 { 0x0B, 0x00 }, 451 { 0x0C, 0x00 }, 452 { 0x0D, 0x00 }, 453 { 0x0E, 0x00 }, 454 { 0x0F, 0x00 }, 455 { 0x10, 0x00 }, 456 { 0x11, 0x00 }, 457 { 0x12, 0x00 }, 458 { 0x13, 0x00 }, 459 { 0x14, 0x00 }, 460 { 0x15, 0x00 }, 461 { 0x16, 0x00 }, 462 { 0x17, 0x00 }, 463 }; 464 465 static const struct regmap_config max9867_regmap = { 466 .reg_bits = 8, 467 .val_bits = 8, 468 .max_register = MAX9867_REVISION, 469 .reg_defaults = max9867_reg, 470 .num_reg_defaults = ARRAY_SIZE(max9867_reg), 471 .volatile_reg = max9867_volatile_register, 472 .cache_type = REGCACHE_RBTREE, 473 }; 474 475 static int max9867_i2c_probe(struct i2c_client *i2c, 476 const struct i2c_device_id *id) 477 { 478 struct max9867_priv *max9867; 479 int ret = 0, reg; 480 481 max9867 = devm_kzalloc(&i2c->dev, 482 sizeof(*max9867), GFP_KERNEL); 483 if (!max9867) 484 return -ENOMEM; 485 486 i2c_set_clientdata(i2c, max9867); 487 max9867->regmap = devm_regmap_init_i2c(i2c, &max9867_regmap); 488 if (IS_ERR(max9867->regmap)) { 489 ret = PTR_ERR(max9867->regmap); 490 dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret); 491 return ret; 492 } 493 ret = regmap_read(max9867->regmap, 494 MAX9867_REVISION, ®); 495 if (ret < 0) { 496 dev_err(&i2c->dev, "Failed to read: %d\n", ret); 497 return ret; 498 } 499 dev_info(&i2c->dev, "device revision: %x\n", reg); 500 ret = devm_snd_soc_register_component(&i2c->dev, &max9867_component, 501 max9867_dai, ARRAY_SIZE(max9867_dai)); 502 if (ret < 0) { 503 dev_err(&i2c->dev, "Failed to register component: %d\n", ret); 504 return ret; 505 } 506 return ret; 507 } 508 509 static const struct i2c_device_id max9867_i2c_id[] = { 510 { "max9867", 0 }, 511 { } 512 }; 513 MODULE_DEVICE_TABLE(i2c, max9867_i2c_id); 514 515 static const struct of_device_id max9867_of_match[] = { 516 { .compatible = "maxim,max9867", }, 517 { } 518 }; 519 MODULE_DEVICE_TABLE(of, max9867_of_match); 520 521 static const struct dev_pm_ops max9867_pm_ops = { 522 SET_SYSTEM_SLEEP_PM_OPS(max9867_suspend, max9867_resume) 523 }; 524 525 static struct i2c_driver max9867_i2c_driver = { 526 .driver = { 527 .name = "max9867", 528 .of_match_table = of_match_ptr(max9867_of_match), 529 .pm = &max9867_pm_ops, 530 }, 531 .probe = max9867_i2c_probe, 532 .id_table = max9867_i2c_id, 533 }; 534 535 module_i2c_driver(max9867_i2c_driver); 536 537 MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>"); 538 MODULE_DESCRIPTION("ALSA SoC MAX9867 driver"); 539 MODULE_LICENSE("GPL"); 540