1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Audio driver for AK5558 ADC 4 // 5 // Copyright (C) 2015 Asahi Kasei Microdevices Corporation 6 // Copyright 2018 NXP 7 8 #include <linux/delay.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/i2c.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/regmap.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/slab.h> 17 18 #include <sound/initval.h> 19 #include <sound/pcm.h> 20 #include <sound/pcm_params.h> 21 #include <sound/soc.h> 22 #include <sound/soc-dapm.h> 23 #include <sound/tlv.h> 24 25 #include "ak5558.h" 26 27 enum ak555x_type { 28 AK5558, 29 AK5552, 30 }; 31 32 #define AK5558_NUM_SUPPLIES 2 33 static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = { 34 "DVDD", 35 "AVDD", 36 }; 37 38 /* AK5558 Codec Private Data */ 39 struct ak5558_priv { 40 struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES]; 41 struct regmap *regmap; 42 struct i2c_client *i2c; 43 struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */ 44 int slots; 45 int slot_width; 46 }; 47 48 /* ak5558 register cache & default register settings */ 49 static const struct reg_default ak5558_reg[] = { 50 { 0x0, 0xFF }, /* 0x00 AK5558_00_POWER_MANAGEMENT1 */ 51 { 0x1, 0x01 }, /* 0x01 AK5558_01_POWER_MANAGEMENT2 */ 52 { 0x2, 0x01 }, /* 0x02 AK5558_02_CONTROL1 */ 53 { 0x3, 0x00 }, /* 0x03 AK5558_03_CONTROL2 */ 54 { 0x4, 0x00 }, /* 0x04 AK5558_04_CONTROL3 */ 55 { 0x5, 0x00 } /* 0x05 AK5558_05_DSD */ 56 }; 57 58 static const char * const mono_texts[] = { 59 "8 Slot", "2 Slot", "4 Slot", "1 Slot", 60 }; 61 62 static const struct soc_enum ak5558_mono_enum[] = { 63 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1, 64 ARRAY_SIZE(mono_texts), mono_texts), 65 }; 66 67 static const char * const mono_5552_texts[] = { 68 "2 Slot", "1 Slot (Fixed)", "2 Slot", "1 Slot (Optimal)", 69 }; 70 71 static const struct soc_enum ak5552_mono_enum[] = { 72 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1, 73 ARRAY_SIZE(mono_5552_texts), mono_5552_texts), 74 }; 75 76 static const char * const digfil_texts[] = { 77 "Sharp Roll-Off", "Slow Roll-Off", 78 "Short Delay Sharp Roll-Off", "Short Delay Slow Roll-Off", 79 }; 80 81 static const struct soc_enum ak5558_adcset_enum[] = { 82 SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0, 83 ARRAY_SIZE(digfil_texts), digfil_texts), 84 }; 85 86 static const struct snd_kcontrol_new ak5558_snd_controls[] = { 87 SOC_ENUM("Monaural Mode", ak5558_mono_enum[0]), 88 SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]), 89 }; 90 91 static const struct snd_kcontrol_new ak5552_snd_controls[] = { 92 SOC_ENUM("Monaural Mode", ak5552_mono_enum[0]), 93 SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]), 94 }; 95 96 static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = { 97 /* Analog Input */ 98 SND_SOC_DAPM_INPUT("AIN1"), 99 SND_SOC_DAPM_INPUT("AIN2"), 100 SND_SOC_DAPM_INPUT("AIN3"), 101 SND_SOC_DAPM_INPUT("AIN4"), 102 SND_SOC_DAPM_INPUT("AIN5"), 103 SND_SOC_DAPM_INPUT("AIN6"), 104 SND_SOC_DAPM_INPUT("AIN7"), 105 SND_SOC_DAPM_INPUT("AIN8"), 106 107 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0), 108 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0), 109 SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0), 110 SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0), 111 SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0), 112 SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0), 113 SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0), 114 SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0), 115 116 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0), 117 }; 118 119 static const struct snd_soc_dapm_widget ak5552_dapm_widgets[] = { 120 /* Analog Input */ 121 SND_SOC_DAPM_INPUT("AIN1"), 122 SND_SOC_DAPM_INPUT("AIN2"), 123 124 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0), 125 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0), 126 127 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0), 128 }; 129 130 static const struct snd_soc_dapm_route ak5558_intercon[] = { 131 {"ADC Ch1", NULL, "AIN1"}, 132 {"SDTO", NULL, "ADC Ch1"}, 133 134 {"ADC Ch2", NULL, "AIN2"}, 135 {"SDTO", NULL, "ADC Ch2"}, 136 137 {"ADC Ch3", NULL, "AIN3"}, 138 {"SDTO", NULL, "ADC Ch3"}, 139 140 {"ADC Ch4", NULL, "AIN4"}, 141 {"SDTO", NULL, "ADC Ch4"}, 142 143 {"ADC Ch5", NULL, "AIN5"}, 144 {"SDTO", NULL, "ADC Ch5"}, 145 146 {"ADC Ch6", NULL, "AIN6"}, 147 {"SDTO", NULL, "ADC Ch6"}, 148 149 {"ADC Ch7", NULL, "AIN7"}, 150 {"SDTO", NULL, "ADC Ch7"}, 151 152 {"ADC Ch8", NULL, "AIN8"}, 153 {"SDTO", NULL, "ADC Ch8"}, 154 }; 155 156 static const struct snd_soc_dapm_route ak5552_intercon[] = { 157 {"ADC Ch1", NULL, "AIN1"}, 158 {"SDTO", NULL, "ADC Ch1"}, 159 160 {"ADC Ch2", NULL, "AIN2"}, 161 {"SDTO", NULL, "ADC Ch2"}, 162 }; 163 164 static int ak5558_set_mcki(struct snd_soc_component *component) 165 { 166 return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS, 167 AK5558_CKS_AUTO); 168 } 169 170 static int ak5558_hw_params(struct snd_pcm_substream *substream, 171 struct snd_pcm_hw_params *params, 172 struct snd_soc_dai *dai) 173 { 174 struct snd_soc_component *component = dai->component; 175 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component); 176 u8 bits; 177 int pcm_width = max(params_physical_width(params), ak5558->slot_width); 178 179 switch (pcm_width) { 180 case 16: 181 bits = AK5558_DIF_24BIT_MODE; 182 break; 183 case 32: 184 bits = AK5558_DIF_32BIT_MODE; 185 break; 186 default: 187 return -EINVAL; 188 } 189 190 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits); 191 192 return 0; 193 } 194 195 static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 196 { 197 struct snd_soc_component *component = dai->component; 198 u8 format; 199 200 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 201 case SND_SOC_DAIFMT_CBC_CFC: 202 break; 203 case SND_SOC_DAIFMT_CBP_CFP: 204 break; 205 case SND_SOC_DAIFMT_CBC_CFP: 206 case SND_SOC_DAIFMT_CBP_CFC: 207 default: 208 dev_err(dai->dev, "Clock mode unsupported"); 209 return -EINVAL; 210 } 211 212 /* set master/slave audio interface */ 213 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 214 case SND_SOC_DAIFMT_I2S: 215 format = AK5558_DIF_I2S_MODE; 216 break; 217 case SND_SOC_DAIFMT_LEFT_J: 218 format = AK5558_DIF_MSB_MODE; 219 break; 220 case SND_SOC_DAIFMT_DSP_B: 221 format = AK5558_DIF_MSB_MODE; 222 break; 223 default: 224 return -EINVAL; 225 } 226 227 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format); 228 229 return 0; 230 } 231 232 static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 233 unsigned int rx_mask, int slots, 234 int slot_width) 235 { 236 struct snd_soc_component *component = dai->component; 237 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component); 238 int tdm_mode; 239 240 ak5558->slots = slots; 241 ak5558->slot_width = slot_width; 242 243 switch (slots * slot_width) { 244 case 128: 245 tdm_mode = AK5558_MODE_TDM128; 246 break; 247 case 256: 248 tdm_mode = AK5558_MODE_TDM256; 249 break; 250 case 512: 251 tdm_mode = AK5558_MODE_TDM512; 252 break; 253 default: 254 tdm_mode = AK5558_MODE_NORMAL; 255 break; 256 } 257 258 snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS, 259 tdm_mode); 260 return 0; 261 } 262 263 #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\ 264 SNDRV_PCM_FMTBIT_S24_LE |\ 265 SNDRV_PCM_FMTBIT_S32_LE) 266 267 static const unsigned int ak5558_rates[] = { 268 8000, 11025, 16000, 22050, 269 32000, 44100, 48000, 88200, 270 96000, 176400, 192000, 352800, 271 384000, 705600, 768000, 1411200, 272 2822400, 273 }; 274 275 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = { 276 .count = ARRAY_SIZE(ak5558_rates), 277 .list = ak5558_rates, 278 }; 279 280 static int ak5558_startup(struct snd_pcm_substream *substream, 281 struct snd_soc_dai *dai) 282 { 283 return snd_pcm_hw_constraint_list(substream->runtime, 0, 284 SNDRV_PCM_HW_PARAM_RATE, 285 &ak5558_rate_constraints); 286 } 287 288 static const struct snd_soc_dai_ops ak5558_dai_ops = { 289 .startup = ak5558_startup, 290 .hw_params = ak5558_hw_params, 291 292 .set_fmt = ak5558_set_dai_fmt, 293 .set_tdm_slot = ak5558_set_tdm_slot, 294 }; 295 296 static struct snd_soc_dai_driver ak5558_dai = { 297 .name = "ak5558-aif", 298 .capture = { 299 .stream_name = "Capture", 300 .channels_min = 1, 301 .channels_max = 8, 302 .rates = SNDRV_PCM_RATE_KNOT, 303 .formats = AK5558_FORMATS, 304 }, 305 .ops = &ak5558_dai_ops, 306 }; 307 308 static struct snd_soc_dai_driver ak5552_dai = { 309 .name = "ak5552-aif", 310 .capture = { 311 .stream_name = "Capture", 312 .channels_min = 1, 313 .channels_max = 2, 314 .rates = SNDRV_PCM_RATE_KNOT, 315 .formats = AK5558_FORMATS, 316 }, 317 .ops = &ak5558_dai_ops, 318 }; 319 320 static void ak5558_reset(struct ak5558_priv *ak5558, bool active) 321 { 322 if (!ak5558->reset_gpiod) 323 return; 324 325 gpiod_set_value_cansleep(ak5558->reset_gpiod, active); 326 usleep_range(1000, 2000); 327 } 328 329 static int ak5558_probe(struct snd_soc_component *component) 330 { 331 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component); 332 333 ak5558_reset(ak5558, false); 334 return ak5558_set_mcki(component); 335 } 336 337 static void ak5558_remove(struct snd_soc_component *component) 338 { 339 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component); 340 341 ak5558_reset(ak5558, true); 342 } 343 344 static int ak5558_runtime_suspend(struct device *dev) 345 { 346 struct ak5558_priv *ak5558 = dev_get_drvdata(dev); 347 348 regcache_cache_only(ak5558->regmap, true); 349 ak5558_reset(ak5558, true); 350 351 regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies), 352 ak5558->supplies); 353 return 0; 354 } 355 356 static int ak5558_runtime_resume(struct device *dev) 357 { 358 struct ak5558_priv *ak5558 = dev_get_drvdata(dev); 359 int ret; 360 361 ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies), 362 ak5558->supplies); 363 if (ret != 0) { 364 dev_err(dev, "Failed to enable supplies: %d\n", ret); 365 return ret; 366 } 367 368 ak5558_reset(ak5558, true); 369 ak5558_reset(ak5558, false); 370 371 regcache_cache_only(ak5558->regmap, false); 372 regcache_mark_dirty(ak5558->regmap); 373 374 ret = regcache_sync(ak5558->regmap); 375 if (ret) 376 goto err; 377 378 return 0; 379 err: 380 regcache_cache_only(ak5558->regmap, true); 381 regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies), ak5558->supplies); 382 return ret; 383 } 384 385 static const struct dev_pm_ops ak5558_pm = { 386 RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL) 387 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) 388 }; 389 390 static const struct snd_soc_component_driver soc_codec_dev_ak5558 = { 391 .probe = ak5558_probe, 392 .remove = ak5558_remove, 393 .controls = ak5558_snd_controls, 394 .num_controls = ARRAY_SIZE(ak5558_snd_controls), 395 .dapm_widgets = ak5558_dapm_widgets, 396 .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets), 397 .dapm_routes = ak5558_intercon, 398 .num_dapm_routes = ARRAY_SIZE(ak5558_intercon), 399 .idle_bias_on = 1, 400 .use_pmdown_time = 1, 401 .endianness = 1, 402 }; 403 404 static const struct snd_soc_component_driver soc_codec_dev_ak5552 = { 405 .probe = ak5558_probe, 406 .remove = ak5558_remove, 407 .controls = ak5552_snd_controls, 408 .num_controls = ARRAY_SIZE(ak5552_snd_controls), 409 .dapm_widgets = ak5552_dapm_widgets, 410 .num_dapm_widgets = ARRAY_SIZE(ak5552_dapm_widgets), 411 .dapm_routes = ak5552_intercon, 412 .num_dapm_routes = ARRAY_SIZE(ak5552_intercon), 413 .idle_bias_on = 1, 414 .use_pmdown_time = 1, 415 .endianness = 1, 416 }; 417 418 static const struct regmap_config ak5558_regmap = { 419 .reg_bits = 8, 420 .val_bits = 8, 421 422 .max_register = AK5558_05_DSD, 423 .reg_defaults = ak5558_reg, 424 .num_reg_defaults = ARRAY_SIZE(ak5558_reg), 425 .cache_type = REGCACHE_RBTREE, 426 }; 427 428 static int ak5558_i2c_probe(struct i2c_client *i2c) 429 { 430 struct ak5558_priv *ak5558; 431 int ret = 0; 432 int dev_id; 433 int i; 434 435 ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL); 436 if (!ak5558) 437 return -ENOMEM; 438 439 ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap); 440 if (IS_ERR(ak5558->regmap)) 441 return PTR_ERR(ak5558->regmap); 442 443 i2c_set_clientdata(i2c, ak5558); 444 ak5558->i2c = i2c; 445 446 ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset", 447 GPIOD_OUT_LOW); 448 if (IS_ERR(ak5558->reset_gpiod)) 449 return PTR_ERR(ak5558->reset_gpiod); 450 451 for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++) 452 ak5558->supplies[i].supply = ak5558_supply_names[i]; 453 454 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies), 455 ak5558->supplies); 456 if (ret != 0) { 457 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); 458 return ret; 459 } 460 461 dev_id = (uintptr_t)of_device_get_match_data(&i2c->dev); 462 switch (dev_id) { 463 case AK5552: 464 ret = devm_snd_soc_register_component(&i2c->dev, 465 &soc_codec_dev_ak5552, 466 &ak5552_dai, 1); 467 break; 468 case AK5558: 469 ret = devm_snd_soc_register_component(&i2c->dev, 470 &soc_codec_dev_ak5558, 471 &ak5558_dai, 1); 472 break; 473 default: 474 dev_err(&i2c->dev, "unexpected device type\n"); 475 return -EINVAL; 476 } 477 if (ret < 0) { 478 dev_err(&i2c->dev, "failed to register component: %d\n", ret); 479 return ret; 480 } 481 482 pm_runtime_enable(&i2c->dev); 483 regcache_cache_only(ak5558->regmap, true); 484 485 return 0; 486 } 487 488 static void ak5558_i2c_remove(struct i2c_client *i2c) 489 { 490 pm_runtime_disable(&i2c->dev); 491 } 492 493 static const struct of_device_id ak5558_i2c_dt_ids[] __maybe_unused = { 494 { .compatible = "asahi-kasei,ak5558", .data = (void *) AK5558 }, 495 { .compatible = "asahi-kasei,ak5552", .data = (void *) AK5552 }, 496 { } 497 }; 498 MODULE_DEVICE_TABLE(of, ak5558_i2c_dt_ids); 499 500 static struct i2c_driver ak5558_i2c_driver = { 501 .driver = { 502 .name = "ak5558", 503 .of_match_table = of_match_ptr(ak5558_i2c_dt_ids), 504 .pm = pm_ptr(&ak5558_pm), 505 }, 506 .probe = ak5558_i2c_probe, 507 .remove = ak5558_i2c_remove, 508 }; 509 510 module_i2c_driver(ak5558_i2c_driver); 511 512 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>"); 513 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>"); 514 MODULE_DESCRIPTION("ASoC AK5558 ADC driver"); 515 MODULE_LICENSE("GPL v2"); 516