1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Audio driver for AK4458 DAC 4 // 5 // Copyright (C) 2016 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_device.h> 13 #include <linux/of_gpio.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/slab.h> 17 #include <sound/initval.h> 18 #include <sound/pcm_params.h> 19 #include <sound/soc.h> 20 #include <sound/soc-dapm.h> 21 #include <sound/tlv.h> 22 23 #include "ak4458.h" 24 25 #define AK4458_NUM_SUPPLIES 2 26 static const char *ak4458_supply_names[AK4458_NUM_SUPPLIES] = { 27 "DVDD", 28 "AVDD", 29 }; 30 31 struct ak4458_drvdata { 32 struct snd_soc_dai_driver *dai_drv; 33 const struct snd_soc_component_driver *comp_drv; 34 }; 35 36 /* AK4458 Codec Private Data */ 37 struct ak4458_priv { 38 struct regulator_bulk_data supplies[AK4458_NUM_SUPPLIES]; 39 struct device *dev; 40 struct regmap *regmap; 41 struct gpio_desc *reset_gpiod; 42 struct gpio_desc *mute_gpiod; 43 int digfil; /* SSLOW, SD, SLOW bits */ 44 int fs; /* sampling rate */ 45 int fmt; 46 int slots; 47 int slot_width; 48 }; 49 50 static const struct reg_default ak4458_reg_defaults[] = { 51 { 0x00, 0x0C }, /* 0x00 AK4458_00_CONTROL1 */ 52 { 0x01, 0x22 }, /* 0x01 AK4458_01_CONTROL2 */ 53 { 0x02, 0x00 }, /* 0x02 AK4458_02_CONTROL3 */ 54 { 0x03, 0xFF }, /* 0x03 AK4458_03_LCHATT */ 55 { 0x04, 0xFF }, /* 0x04 AK4458_04_RCHATT */ 56 { 0x05, 0x00 }, /* 0x05 AK4458_05_CONTROL4 */ 57 { 0x06, 0x00 }, /* 0x06 AK4458_06_DSD1 */ 58 { 0x07, 0x03 }, /* 0x07 AK4458_07_CONTROL5 */ 59 { 0x08, 0x00 }, /* 0x08 AK4458_08_SOUND_CONTROL */ 60 { 0x09, 0x00 }, /* 0x09 AK4458_09_DSD2 */ 61 { 0x0A, 0x0D }, /* 0x0A AK4458_0A_CONTROL6 */ 62 { 0x0B, 0x0C }, /* 0x0B AK4458_0B_CONTROL7 */ 63 { 0x0C, 0x00 }, /* 0x0C AK4458_0C_CONTROL8 */ 64 { 0x0D, 0x00 }, /* 0x0D AK4458_0D_CONTROL9 */ 65 { 0x0E, 0x50 }, /* 0x0E AK4458_0E_CONTROL10 */ 66 { 0x0F, 0xFF }, /* 0x0F AK4458_0F_L2CHATT */ 67 { 0x10, 0xFF }, /* 0x10 AK4458_10_R2CHATT */ 68 { 0x11, 0xFF }, /* 0x11 AK4458_11_L3CHATT */ 69 { 0x12, 0xFF }, /* 0x12 AK4458_12_R3CHATT */ 70 { 0x13, 0xFF }, /* 0x13 AK4458_13_L4CHATT */ 71 { 0x14, 0xFF }, /* 0x14 AK4458_14_R4CHATT */ 72 }; 73 74 /* 75 * Volume control: 76 * from -127 to 0 dB in 0.5 dB steps (mute instead of -127.5 dB) 77 */ 78 static DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); 79 80 /* 81 * DEM1 bit DEM0 bit Mode 82 * 0 0 44.1kHz 83 * 0 1 OFF (default) 84 * 1 0 48kHz 85 * 1 1 32kHz 86 */ 87 static const char * const ak4458_dem_select_texts[] = { 88 "44.1kHz", "OFF", "48kHz", "32kHz" 89 }; 90 91 /* 92 * SSLOW, SD, SLOW bits Digital Filter Setting 93 * 0, 0, 0 : Sharp Roll-Off Filter 94 * 0, 0, 1 : Slow Roll-Off Filter 95 * 0, 1, 0 : Short delay Sharp Roll-Off Filter 96 * 0, 1, 1 : Short delay Slow Roll-Off Filter 97 * 1, *, * : Super Slow Roll-Off Filter 98 */ 99 static const char * const ak4458_digfil_select_texts[] = { 100 "Sharp Roll-Off Filter", 101 "Slow Roll-Off Filter", 102 "Short delay Sharp Roll-Off Filter", 103 "Short delay Slow Roll-Off Filter", 104 "Super Slow Roll-Off Filter" 105 }; 106 107 /* 108 * DZFB: Inverting Enable of DZF 109 * 0: DZF goes H at Zero Detection 110 * 1: DZF goes L at Zero Detection 111 */ 112 static const char * const ak4458_dzfb_select_texts[] = {"H", "L"}; 113 114 /* 115 * SC1-0 bits: Sound Mode Setting 116 * 0 0 : Sound Mode 0 117 * 0 1 : Sound Mode 1 118 * 1 0 : Sound Mode 2 119 * 1 1 : Reserved 120 */ 121 static const char * const ak4458_sc_select_texts[] = { 122 "Sound Mode 0", "Sound Mode 1", "Sound Mode 2" 123 }; 124 125 /* FIR2-0 bits: FIR Filter Mode Setting */ 126 static const char * const ak4458_fir_select_texts[] = { 127 "Mode 0", "Mode 1", "Mode 2", "Mode 3", 128 "Mode 4", "Mode 5", "Mode 6", "Mode 7", 129 }; 130 131 /* ATS1-0 bits Attenuation Speed */ 132 static const char * const ak4458_ats_select_texts[] = { 133 "4080/fs", "2040/fs", "510/fs", "255/fs", 134 }; 135 136 /* DIF2 bit Audio Interface Format Setting(BICK fs) */ 137 static const char * const ak4458_dif_select_texts[] = {"32fs,48fs", "64fs",}; 138 139 static const struct soc_enum ak4458_dac1_dem_enum = 140 SOC_ENUM_SINGLE(AK4458_01_CONTROL2, 1, 141 ARRAY_SIZE(ak4458_dem_select_texts), 142 ak4458_dem_select_texts); 143 static const struct soc_enum ak4458_dac2_dem_enum = 144 SOC_ENUM_SINGLE(AK4458_0A_CONTROL6, 0, 145 ARRAY_SIZE(ak4458_dem_select_texts), 146 ak4458_dem_select_texts); 147 static const struct soc_enum ak4458_dac3_dem_enum = 148 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 4, 149 ARRAY_SIZE(ak4458_dem_select_texts), 150 ak4458_dem_select_texts); 151 static const struct soc_enum ak4458_dac4_dem_enum = 152 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 6, 153 ARRAY_SIZE(ak4458_dem_select_texts), 154 ak4458_dem_select_texts); 155 static const struct soc_enum ak4458_digfil_enum = 156 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ak4458_digfil_select_texts), 157 ak4458_digfil_select_texts); 158 static const struct soc_enum ak4458_dzfb_enum = 159 SOC_ENUM_SINGLE(AK4458_02_CONTROL3, 2, 160 ARRAY_SIZE(ak4458_dzfb_select_texts), 161 ak4458_dzfb_select_texts); 162 static const struct soc_enum ak4458_sm_enum = 163 SOC_ENUM_SINGLE(AK4458_08_SOUND_CONTROL, 0, 164 ARRAY_SIZE(ak4458_sc_select_texts), 165 ak4458_sc_select_texts); 166 static const struct soc_enum ak4458_fir_enum = 167 SOC_ENUM_SINGLE(AK4458_0C_CONTROL8, 0, 168 ARRAY_SIZE(ak4458_fir_select_texts), 169 ak4458_fir_select_texts); 170 static const struct soc_enum ak4458_ats_enum = 171 SOC_ENUM_SINGLE(AK4458_0B_CONTROL7, 6, 172 ARRAY_SIZE(ak4458_ats_select_texts), 173 ak4458_ats_select_texts); 174 static const struct soc_enum ak4458_dif_enum = 175 SOC_ENUM_SINGLE(AK4458_00_CONTROL1, 3, 176 ARRAY_SIZE(ak4458_dif_select_texts), 177 ak4458_dif_select_texts); 178 179 static int get_digfil(struct snd_kcontrol *kcontrol, 180 struct snd_ctl_elem_value *ucontrol) 181 { 182 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 183 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 184 185 ucontrol->value.enumerated.item[0] = ak4458->digfil; 186 187 return 0; 188 } 189 190 static int set_digfil(struct snd_kcontrol *kcontrol, 191 struct snd_ctl_elem_value *ucontrol) 192 { 193 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 194 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 195 int num; 196 197 num = ucontrol->value.enumerated.item[0]; 198 if (num > 4) 199 return -EINVAL; 200 201 ak4458->digfil = num; 202 203 /* write SD bit */ 204 snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 205 AK4458_SD_MASK, 206 ((ak4458->digfil & 0x02) << 4)); 207 208 /* write SLOW bit */ 209 snd_soc_component_update_bits(component, AK4458_02_CONTROL3, 210 AK4458_SLOW_MASK, 211 (ak4458->digfil & 0x01)); 212 213 /* write SSLOW bit */ 214 snd_soc_component_update_bits(component, AK4458_05_CONTROL4, 215 AK4458_SSLOW_MASK, 216 ((ak4458->digfil & 0x04) >> 2)); 217 218 return 0; 219 } 220 221 static const struct snd_kcontrol_new ak4458_snd_controls[] = { 222 SOC_DOUBLE_R_TLV("DAC1 Playback Volume", AK4458_03_LCHATT, 223 AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv), 224 SOC_DOUBLE_R_TLV("DAC2 Playback Volume", AK4458_0F_L2CHATT, 225 AK4458_10_R2CHATT, 0, 0xFF, 0, dac_tlv), 226 SOC_DOUBLE_R_TLV("DAC3 Playback Volume", AK4458_11_L3CHATT, 227 AK4458_12_R3CHATT, 0, 0xFF, 0, dac_tlv), 228 SOC_DOUBLE_R_TLV("DAC4 Playback Volume", AK4458_13_L4CHATT, 229 AK4458_14_R4CHATT, 0, 0xFF, 0, dac_tlv), 230 SOC_ENUM("AK4458 De-emphasis Response DAC1", ak4458_dac1_dem_enum), 231 SOC_ENUM("AK4458 De-emphasis Response DAC2", ak4458_dac2_dem_enum), 232 SOC_ENUM("AK4458 De-emphasis Response DAC3", ak4458_dac3_dem_enum), 233 SOC_ENUM("AK4458 De-emphasis Response DAC4", ak4458_dac4_dem_enum), 234 SOC_ENUM_EXT("AK4458 Digital Filter Setting", ak4458_digfil_enum, 235 get_digfil, set_digfil), 236 SOC_ENUM("AK4458 Inverting Enable of DZFB", ak4458_dzfb_enum), 237 SOC_ENUM("AK4458 Sound Mode", ak4458_sm_enum), 238 SOC_ENUM("AK4458 FIR Filter Mode Setting", ak4458_fir_enum), 239 SOC_ENUM("AK4458 Attenuation transition Time Setting", 240 ak4458_ats_enum), 241 SOC_ENUM("AK4458 BICK fs Setting", ak4458_dif_enum), 242 }; 243 244 /* ak4458 dapm widgets */ 245 static const struct snd_soc_dapm_widget ak4458_dapm_widgets[] = { 246 SND_SOC_DAPM_DAC("AK4458 DAC1", NULL, AK4458_0A_CONTROL6, 2, 0),/*pw*/ 247 SND_SOC_DAPM_AIF_IN("AK4458 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0), 248 SND_SOC_DAPM_OUTPUT("AK4458 AOUTA"), 249 250 SND_SOC_DAPM_DAC("AK4458 DAC2", NULL, AK4458_0A_CONTROL6, 3, 0),/*pw*/ 251 SND_SOC_DAPM_OUTPUT("AK4458 AOUTB"), 252 253 SND_SOC_DAPM_DAC("AK4458 DAC3", NULL, AK4458_0B_CONTROL7, 2, 0),/*pw*/ 254 SND_SOC_DAPM_OUTPUT("AK4458 AOUTC"), 255 256 SND_SOC_DAPM_DAC("AK4458 DAC4", NULL, AK4458_0B_CONTROL7, 3, 0),/*pw*/ 257 SND_SOC_DAPM_OUTPUT("AK4458 AOUTD"), 258 }; 259 260 static const struct snd_soc_dapm_route ak4458_intercon[] = { 261 {"AK4458 DAC1", NULL, "AK4458 SDTI"}, 262 {"AK4458 AOUTA", NULL, "AK4458 DAC1"}, 263 264 {"AK4458 DAC2", NULL, "AK4458 SDTI"}, 265 {"AK4458 AOUTB", NULL, "AK4458 DAC2"}, 266 267 {"AK4458 DAC3", NULL, "AK4458 SDTI"}, 268 {"AK4458 AOUTC", NULL, "AK4458 DAC3"}, 269 270 {"AK4458 DAC4", NULL, "AK4458 SDTI"}, 271 {"AK4458 AOUTD", NULL, "AK4458 DAC4"}, 272 }; 273 274 /* ak4497 controls */ 275 static const struct snd_kcontrol_new ak4497_snd_controls[] = { 276 SOC_DOUBLE_R_TLV("DAC Playback Volume", AK4458_03_LCHATT, 277 AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv), 278 SOC_ENUM("AK4497 De-emphasis Response DAC", ak4458_dac1_dem_enum), 279 SOC_ENUM_EXT("AK4497 Digital Filter Setting", ak4458_digfil_enum, 280 get_digfil, set_digfil), 281 SOC_ENUM("AK4497 Inverting Enable of DZFB", ak4458_dzfb_enum), 282 SOC_ENUM("AK4497 Sound Mode", ak4458_sm_enum), 283 SOC_ENUM("AK4497 Attenuation transition Time Setting", 284 ak4458_ats_enum), 285 }; 286 287 /* ak4497 dapm widgets */ 288 static const struct snd_soc_dapm_widget ak4497_dapm_widgets[] = { 289 SND_SOC_DAPM_DAC("AK4497 DAC", NULL, AK4458_0A_CONTROL6, 2, 0), 290 SND_SOC_DAPM_AIF_IN("AK4497 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0), 291 SND_SOC_DAPM_OUTPUT("AK4497 AOUT"), 292 }; 293 294 /* ak4497 dapm routes */ 295 static const struct snd_soc_dapm_route ak4497_intercon[] = { 296 {"AK4497 DAC", NULL, "AK4497 SDTI"}, 297 {"AK4497 AOUT", NULL, "AK4497 DAC"}, 298 299 }; 300 301 static int ak4458_rstn_control(struct snd_soc_component *component, int bit) 302 { 303 int ret; 304 305 if (bit) 306 ret = snd_soc_component_update_bits(component, 307 AK4458_00_CONTROL1, 308 AK4458_RSTN_MASK, 309 0x1); 310 else 311 ret = snd_soc_component_update_bits(component, 312 AK4458_00_CONTROL1, 313 AK4458_RSTN_MASK, 314 0x0); 315 if (ret < 0) 316 return ret; 317 318 return 0; 319 } 320 321 static int ak4458_hw_params(struct snd_pcm_substream *substream, 322 struct snd_pcm_hw_params *params, 323 struct snd_soc_dai *dai) 324 { 325 struct snd_soc_component *component = dai->component; 326 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 327 int pcm_width = max(params_physical_width(params), ak4458->slot_width); 328 int nfs1; 329 u8 format; 330 331 nfs1 = params_rate(params); 332 ak4458->fs = nfs1; 333 334 /* Master Clock Frequency Auto Setting Mode Enable */ 335 snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 0x80, 0x80); 336 337 switch (pcm_width) { 338 case 16: 339 if (ak4458->fmt == SND_SOC_DAIFMT_I2S) 340 format = AK4458_DIF_24BIT_I2S; 341 else 342 format = AK4458_DIF_16BIT_LSB; 343 break; 344 case 32: 345 switch (ak4458->fmt) { 346 case SND_SOC_DAIFMT_I2S: 347 format = AK4458_DIF_32BIT_I2S; 348 break; 349 case SND_SOC_DAIFMT_LEFT_J: 350 format = AK4458_DIF_32BIT_MSB; 351 break; 352 case SND_SOC_DAIFMT_RIGHT_J: 353 format = AK4458_DIF_32BIT_LSB; 354 break; 355 case SND_SOC_DAIFMT_DSP_B: 356 format = AK4458_DIF_32BIT_MSB; 357 break; 358 default: 359 return -EINVAL; 360 } 361 break; 362 default: 363 return -EINVAL; 364 } 365 366 snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 367 AK4458_DIF_MASK, format); 368 369 ak4458_rstn_control(component, 0); 370 ak4458_rstn_control(component, 1); 371 372 return 0; 373 } 374 375 static int ak4458_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 376 { 377 struct snd_soc_component *component = dai->component; 378 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 379 380 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 381 case SND_SOC_DAIFMT_CBS_CFS: /* Slave Mode */ 382 break; 383 case SND_SOC_DAIFMT_CBM_CFM: /* Master Mode is not supported */ 384 case SND_SOC_DAIFMT_CBS_CFM: 385 case SND_SOC_DAIFMT_CBM_CFS: 386 default: 387 dev_err(component->dev, "Master mode unsupported\n"); 388 return -EINVAL; 389 } 390 391 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 392 case SND_SOC_DAIFMT_I2S: 393 case SND_SOC_DAIFMT_LEFT_J: 394 case SND_SOC_DAIFMT_RIGHT_J: 395 case SND_SOC_DAIFMT_DSP_B: 396 ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; 397 break; 398 default: 399 dev_err(component->dev, "Audio format 0x%02X unsupported\n", 400 fmt & SND_SOC_DAIFMT_FORMAT_MASK); 401 return -EINVAL; 402 } 403 404 ak4458_rstn_control(component, 0); 405 ak4458_rstn_control(component, 1); 406 407 return 0; 408 } 409 410 static const int att_speed[] = { 4080, 2040, 510, 255 }; 411 412 static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute, int direction) 413 { 414 struct snd_soc_component *component = dai->component; 415 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 416 int nfs, ndt, reg; 417 int ats; 418 419 nfs = ak4458->fs; 420 421 reg = snd_soc_component_read(component, AK4458_0B_CONTROL7); 422 ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT; 423 424 ndt = att_speed[ats] / (nfs / 1000); 425 426 if (mute) { 427 snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 1); 428 mdelay(ndt); 429 if (ak4458->mute_gpiod) 430 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); 431 } else { 432 if (ak4458->mute_gpiod) 433 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0); 434 snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0); 435 mdelay(ndt); 436 } 437 438 return 0; 439 } 440 441 static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 442 unsigned int rx_mask, int slots, int slot_width) 443 { 444 struct snd_soc_component *component = dai->component; 445 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 446 int mode; 447 448 ak4458->slots = slots; 449 ak4458->slot_width = slot_width; 450 451 switch (slots * slot_width) { 452 case 128: 453 mode = AK4458_MODE_TDM128; 454 break; 455 case 256: 456 mode = AK4458_MODE_TDM256; 457 break; 458 case 512: 459 mode = AK4458_MODE_TDM512; 460 break; 461 default: 462 mode = AK4458_MODE_NORMAL; 463 break; 464 } 465 466 snd_soc_component_update_bits(component, AK4458_0A_CONTROL6, 467 AK4458_MODE_MASK, 468 mode); 469 470 return 0; 471 } 472 473 #define AK4458_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\ 474 SNDRV_PCM_FMTBIT_S24_LE |\ 475 SNDRV_PCM_FMTBIT_S32_LE) 476 477 static const unsigned int ak4458_rates[] = { 478 8000, 11025, 16000, 22050, 479 32000, 44100, 48000, 88200, 480 96000, 176400, 192000, 352800, 481 384000, 705600, 768000, 1411200, 482 2822400, 483 }; 484 485 static const struct snd_pcm_hw_constraint_list ak4458_rate_constraints = { 486 .count = ARRAY_SIZE(ak4458_rates), 487 .list = ak4458_rates, 488 }; 489 490 static int ak4458_startup(struct snd_pcm_substream *substream, 491 struct snd_soc_dai *dai) 492 { 493 int ret; 494 495 ret = snd_pcm_hw_constraint_list(substream->runtime, 0, 496 SNDRV_PCM_HW_PARAM_RATE, 497 &ak4458_rate_constraints); 498 499 return ret; 500 } 501 502 static const struct snd_soc_dai_ops ak4458_dai_ops = { 503 .startup = ak4458_startup, 504 .hw_params = ak4458_hw_params, 505 .set_fmt = ak4458_set_dai_fmt, 506 .mute_stream = ak4458_set_dai_mute, 507 .set_tdm_slot = ak4458_set_tdm_slot, 508 .no_capture_mute = 1, 509 }; 510 511 static struct snd_soc_dai_driver ak4458_dai = { 512 .name = "ak4458-aif", 513 .playback = { 514 .stream_name = "Playback", 515 .channels_min = 1, 516 .channels_max = 8, 517 .rates = SNDRV_PCM_RATE_KNOT, 518 .formats = AK4458_FORMATS, 519 }, 520 .ops = &ak4458_dai_ops, 521 }; 522 523 static struct snd_soc_dai_driver ak4497_dai = { 524 .name = "ak4497-aif", 525 .playback = { 526 .stream_name = "Playback", 527 .channels_min = 1, 528 .channels_max = 2, 529 .rates = SNDRV_PCM_RATE_KNOT, 530 .formats = AK4458_FORMATS, 531 }, 532 .ops = &ak4458_dai_ops, 533 }; 534 535 static void ak4458_power_off(struct ak4458_priv *ak4458) 536 { 537 if (ak4458->reset_gpiod) { 538 gpiod_set_value_cansleep(ak4458->reset_gpiod, 0); 539 usleep_range(1000, 2000); 540 } 541 } 542 543 static void ak4458_power_on(struct ak4458_priv *ak4458) 544 { 545 if (ak4458->reset_gpiod) { 546 gpiod_set_value_cansleep(ak4458->reset_gpiod, 1); 547 usleep_range(1000, 2000); 548 } 549 } 550 551 static int ak4458_init(struct snd_soc_component *component) 552 { 553 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 554 int ret; 555 556 /* External Mute ON */ 557 if (ak4458->mute_gpiod) 558 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); 559 560 ak4458_power_on(ak4458); 561 562 ret = snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 563 0x80, 0x80); /* ACKS bit = 1; 10000000 */ 564 if (ret < 0) 565 return ret; 566 567 return ak4458_rstn_control(component, 1); 568 } 569 570 static int ak4458_probe(struct snd_soc_component *component) 571 { 572 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 573 574 ak4458->fs = 48000; 575 576 return ak4458_init(component); 577 } 578 579 static void ak4458_remove(struct snd_soc_component *component) 580 { 581 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component); 582 583 ak4458_power_off(ak4458); 584 } 585 586 #ifdef CONFIG_PM 587 static int __maybe_unused ak4458_runtime_suspend(struct device *dev) 588 { 589 struct ak4458_priv *ak4458 = dev_get_drvdata(dev); 590 591 regcache_cache_only(ak4458->regmap, true); 592 593 ak4458_power_off(ak4458); 594 595 if (ak4458->mute_gpiod) 596 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0); 597 598 regulator_bulk_disable(ARRAY_SIZE(ak4458->supplies), 599 ak4458->supplies); 600 return 0; 601 } 602 603 static int __maybe_unused ak4458_runtime_resume(struct device *dev) 604 { 605 struct ak4458_priv *ak4458 = dev_get_drvdata(dev); 606 int ret; 607 608 ret = regulator_bulk_enable(ARRAY_SIZE(ak4458->supplies), 609 ak4458->supplies); 610 if (ret != 0) { 611 dev_err(ak4458->dev, "Failed to enable supplies: %d\n", ret); 612 return ret; 613 } 614 615 if (ak4458->mute_gpiod) 616 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); 617 618 ak4458_power_off(ak4458); 619 ak4458_power_on(ak4458); 620 621 regcache_cache_only(ak4458->regmap, false); 622 regcache_mark_dirty(ak4458->regmap); 623 624 return regcache_sync(ak4458->regmap); 625 } 626 #endif /* CONFIG_PM */ 627 628 static const struct snd_soc_component_driver soc_codec_dev_ak4458 = { 629 .probe = ak4458_probe, 630 .remove = ak4458_remove, 631 .controls = ak4458_snd_controls, 632 .num_controls = ARRAY_SIZE(ak4458_snd_controls), 633 .dapm_widgets = ak4458_dapm_widgets, 634 .num_dapm_widgets = ARRAY_SIZE(ak4458_dapm_widgets), 635 .dapm_routes = ak4458_intercon, 636 .num_dapm_routes = ARRAY_SIZE(ak4458_intercon), 637 .idle_bias_on = 1, 638 .use_pmdown_time = 1, 639 .endianness = 1, 640 .non_legacy_dai_naming = 1, 641 }; 642 643 static const struct snd_soc_component_driver soc_codec_dev_ak4497 = { 644 .probe = ak4458_probe, 645 .remove = ak4458_remove, 646 .controls = ak4497_snd_controls, 647 .num_controls = ARRAY_SIZE(ak4497_snd_controls), 648 .dapm_widgets = ak4497_dapm_widgets, 649 .num_dapm_widgets = ARRAY_SIZE(ak4497_dapm_widgets), 650 .dapm_routes = ak4497_intercon, 651 .num_dapm_routes = ARRAY_SIZE(ak4497_intercon), 652 .idle_bias_on = 1, 653 .use_pmdown_time = 1, 654 .endianness = 1, 655 .non_legacy_dai_naming = 1, 656 }; 657 658 static const struct regmap_config ak4458_regmap = { 659 .reg_bits = 8, 660 .val_bits = 8, 661 662 .max_register = AK4458_14_R4CHATT, 663 .reg_defaults = ak4458_reg_defaults, 664 .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults), 665 .cache_type = REGCACHE_RBTREE, 666 }; 667 668 static const struct ak4458_drvdata ak4458_drvdata = { 669 .dai_drv = &ak4458_dai, 670 .comp_drv = &soc_codec_dev_ak4458, 671 }; 672 673 static const struct ak4458_drvdata ak4497_drvdata = { 674 .dai_drv = &ak4497_dai, 675 .comp_drv = &soc_codec_dev_ak4497, 676 }; 677 678 static const struct dev_pm_ops ak4458_pm = { 679 SET_RUNTIME_PM_OPS(ak4458_runtime_suspend, ak4458_runtime_resume, NULL) 680 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 681 pm_runtime_force_resume) 682 }; 683 684 static int ak4458_i2c_probe(struct i2c_client *i2c) 685 { 686 struct ak4458_priv *ak4458; 687 const struct ak4458_drvdata *drvdata; 688 int ret, i; 689 690 ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL); 691 if (!ak4458) 692 return -ENOMEM; 693 694 ak4458->regmap = devm_regmap_init_i2c(i2c, &ak4458_regmap); 695 if (IS_ERR(ak4458->regmap)) 696 return PTR_ERR(ak4458->regmap); 697 698 i2c_set_clientdata(i2c, ak4458); 699 ak4458->dev = &i2c->dev; 700 701 drvdata = of_device_get_match_data(&i2c->dev); 702 703 ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset", 704 GPIOD_OUT_LOW); 705 if (IS_ERR(ak4458->reset_gpiod)) 706 return PTR_ERR(ak4458->reset_gpiod); 707 708 ak4458->mute_gpiod = devm_gpiod_get_optional(ak4458->dev, "mute", 709 GPIOD_OUT_LOW); 710 if (IS_ERR(ak4458->mute_gpiod)) 711 return PTR_ERR(ak4458->mute_gpiod); 712 713 for (i = 0; i < ARRAY_SIZE(ak4458->supplies); i++) 714 ak4458->supplies[i].supply = ak4458_supply_names[i]; 715 716 ret = devm_regulator_bulk_get(ak4458->dev, ARRAY_SIZE(ak4458->supplies), 717 ak4458->supplies); 718 if (ret != 0) { 719 dev_err(ak4458->dev, "Failed to request supplies: %d\n", ret); 720 return ret; 721 } 722 723 ret = devm_snd_soc_register_component(ak4458->dev, drvdata->comp_drv, 724 drvdata->dai_drv, 1); 725 if (ret < 0) { 726 dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret); 727 return ret; 728 } 729 730 pm_runtime_enable(&i2c->dev); 731 regcache_cache_only(ak4458->regmap, true); 732 733 return 0; 734 } 735 736 static int ak4458_i2c_remove(struct i2c_client *i2c) 737 { 738 pm_runtime_disable(&i2c->dev); 739 740 return 0; 741 } 742 743 static const struct of_device_id ak4458_of_match[] = { 744 { .compatible = "asahi-kasei,ak4458", .data = &ak4458_drvdata}, 745 { .compatible = "asahi-kasei,ak4497", .data = &ak4497_drvdata}, 746 { }, 747 }; 748 749 static struct i2c_driver ak4458_i2c_driver = { 750 .driver = { 751 .name = "ak4458", 752 .pm = &ak4458_pm, 753 .of_match_table = ak4458_of_match, 754 }, 755 .probe_new = ak4458_i2c_probe, 756 .remove = ak4458_i2c_remove, 757 }; 758 759 module_i2c_driver(ak4458_i2c_driver); 760 761 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>"); 762 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>"); 763 MODULE_DESCRIPTION("ASoC AK4458 DAC driver"); 764 MODULE_LICENSE("GPL v2"); 765