| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * ak4940.c -- audio driver for AK4940 |
| 3 | * |
| 4 | * Copyright (C) 2018 Asahi Kasei Microdevices Corporation |
| 5 | * Author Date Revision |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * 18/08/10 1.0 Kernel 4_9_XX |
| 8 | * 20/09/02 1.1 Kernel 4_19_XX |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/gpio.h> |
| 24 | #include <sound/soc.h> |
| 25 | #include <sound/soc-dapm.h> |
| 26 | #include <sound/initval.h> |
| 27 | #include <sound/tlv.h> |
| 28 | #include <linux/of_gpio.h> |
| 29 | #include <linux/regmap.h> |
| 30 | #include <sound/pcm.h> |
| 31 | #include <sound/pcm_params.h> |
| 32 | |
| 33 | #include "ak4940.h" |
| 34 | |
| 35 | #define AK4940_DEBUG |
| 36 | |
| 37 | #ifdef AK4940_DEBUG |
| 38 | #define akdbgprt printk |
| 39 | #else |
| 40 | #define akdbgprt(format, arg...) do {} while (0) |
| 41 | #endif |
| 42 | |
| 43 | /* AK4940 Codec Private Data */ |
| 44 | struct ak4940_priv { |
| 45 | struct snd_soc_component *component; |
| 46 | struct i2c_client *i2c; |
| 47 | struct regmap *regmap; |
| 48 | int pdn_gpio; |
| 49 | int fs; |
| 50 | int aif_format; |
| 51 | int sxxlebit; |
| 52 | int bitfs; |
| 53 | int dfsx; |
| 54 | int ifSetMode; |
| 55 | }; |
| 56 | |
| 57 | /* ak4940 register cache & default register settings */ |
| 58 | static const struct reg_default ak4940_reg[] = { |
| 59 | { 0x00, 0x00 }, /* AK4940_00_CLOCK_MANAGEMENT */ |
| 60 | { 0x01, 0x00 }, /* AK4940_01_AUDIO_IF_SETTING */ |
| 61 | { 0x02, 0x00 }, /* AK4940_02_ADC_CONTROL */ |
| 62 | { 0x03, 0x00 }, /* AK4940_03_LINEOUT_CONTROL */ |
| 63 | { 0x04, 0x00 }, /* AK4940_04_POWER_MANAGEMENT */ |
| 64 | { 0x05, 0x08 }, /* AK4940_05_INPUT_GAIN */ |
| 65 | { 0x06, 0x30 }, /* AK4940_06_ADC_DIGITAL_VOLUME */ |
| 66 | { 0x07, 0x18 }, /* AK4940_07_DAC_DIGITAL_VOLUME */ |
| 67 | { 0x08, 0x00 }, /* AK4940_08_SOFTMUTE_CONTROL */ |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | // MIC Gain control: |
| 72 | // from -6 to 27 dB in 3 dB steps |
| 73 | static DECLARE_TLV_DB_SCALE(mgain_tlv, -600, 300, 0); |
| 74 | |
| 75 | // ADC Digital volume control: (VOLAD) |
| 76 | // from -103.5 to 24.0 dB in 0.5 dB steps mute instead of -103.5 dB) |
| 77 | static DECLARE_TLV_DB_SCALE(volad_tlv, -10350, 50, 1); |
| 78 | |
| 79 | |
| 80 | // DAC Digital volume control: (VOLDA) |
| 81 | // (This can be used I/F output volume) |
| 82 | // from -115.5 to 12.0 dB in 0.5 dB steps mute instead of -115.5 dB) |
| 83 | static DECLARE_TLV_DB_SCALE(volda_tlv, -11550, 50, 1); |
| 84 | |
| 85 | #ifdef AK4940_DEBUG |
| 86 | static const char * const test_reg_select[] = { |
| 87 | "read AK4940 Reg 00:08", |
| 88 | }; |
| 89 | |
| 90 | static const struct soc_enum ak4940_enum2[] = { |
| 91 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(test_reg_select), test_reg_select), |
| 92 | }; |
| 93 | #endif |
| 94 | |
| 95 | static const char * const ak4940_outaddiffn_select_texts[] = { |
| 96 | "Differential", "Single-end", |
| 97 | }; |
| 98 | |
| 99 | static const struct soc_enum ak4940_outaddiffn_enum[] = { |
| 100 | SOC_ENUM_SINGLE(AK4940_04_POWER_MANAGEMENT, 2, |
| 101 | ARRAY_SIZE(ak4940_outaddiffn_select_texts), ak4940_outaddiffn_select_texts), |
| 102 | }; |
| 103 | |
| 104 | static const char * const ak4940_outnsel_select_texts[] = { |
| 105 | "Differential", "Single-end", |
| 106 | }; |
| 107 | |
| 108 | static const struct soc_enum ak4940_outnsel_enum[] = { |
| 109 | SOC_ENUM_SINGLE(AK4940_03_LINEOUT_CONTROL, 7, |
| 110 | ARRAY_SIZE(ak4940_outnsel_select_texts), ak4940_outnsel_select_texts), |
| 111 | }; |
| 112 | |
| 113 | static const char * const ak4940_bckp_select_texts[] = { |
| 114 | "Falling", "Rising", |
| 115 | }; |
| 116 | |
| 117 | static const struct soc_enum ak4940_bckp_enum[] = { |
| 118 | SOC_ENUM_SINGLE(AK4940_02_ADC_CONTROL, 7, |
| 119 | ARRAY_SIZE(ak4940_bckp_select_texts), ak4940_bckp_select_texts), |
| 120 | }; |
| 121 | |
| 122 | static const char * const ak4940_bick_freq_texts[] = { |
| 123 | "64fs", "48fs", "32fs", |
| 124 | }; |
| 125 | |
| 126 | static const struct soc_enum ak4940_bickfreq_enum[] = { |
| 127 | SOC_ENUM_SINGLE(AK4940_01_AUDIO_IF_SETTING, 0, |
| 128 | ARRAY_SIZE(ak4940_bick_freq_texts), ak4940_bick_freq_texts), |
| 129 | }; |
| 130 | |
| 131 | static int get_test_reg( |
| 132 | struct snd_kcontrol *kcontrol, |
| 133 | struct snd_ctl_elem_value *ucontrol) |
| 134 | { |
| 135 | int nTestRegNo = 0; |
| 136 | |
| 137 | /* Get the current output routing */ |
| 138 | ucontrol->value.enumerated.item[0] = nTestRegNo; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int set_test_reg( |
| 144 | struct snd_kcontrol *kcontrol, |
| 145 | struct snd_ctl_elem_value *ucontrol) |
| 146 | { |
| 147 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 148 | u32 currMode = ucontrol->value.enumerated.item[0]; |
| 149 | int i, ret; |
| 150 | int regs, rege; |
| 151 | unsigned int value; |
| 152 | int nTestRegNo = 0; |
| 153 | |
| 154 | nTestRegNo = currMode; |
| 155 | regs = 0x0; |
| 156 | rege = 0x09; |
| 157 | |
| 158 | for (i = regs; i < rege; i++) { |
| 159 | ret = snd_soc_component_read(component, i, &value); |
| 160 | if (ret < 0) { |
| 161 | akdbgprt("\t[AK4940] %s(%d), ret=%d\n", __func__, __LINE__, ret); |
| 162 | return ret; |
| 163 | } |
| 164 | akdbgprt("***AK4940 Addr,Reg=(%x, %x)\n", i, value); |
| 165 | } |
| 166 | return(0); |
| 167 | } |
| 168 | |
| 169 | static const struct snd_kcontrol_new ak4940_snd_controls[] = { |
| 170 | SOC_SINGLE_TLV("Mic Gain Control", |
| 171 | AK4940_05_INPUT_GAIN, 0, 0x0B, 0, mgain_tlv), |
| 172 | SOC_SINGLE_TLV("ADC Digital Volume (VOLAD)", |
| 173 | AK4940_06_ADC_DIGITAL_VOLUME, 0, 0xFF, 1, volad_tlv), |
| 174 | SOC_SINGLE_TLV("DAC Digital Volume (VOLDA)", |
| 175 | AK4940_07_DAC_DIGITAL_VOLUME, 0, 0xFF, 1, volda_tlv), |
| 176 | |
| 177 | SOC_SINGLE("ADC Soft Mute Control", AK4940_08_SOFTMUTE_CONTROL, 5, 1, 0), |
| 178 | SOC_SINGLE("DAC Soft Mute Control", AK4940_08_SOFTMUTE_CONTROL, 1, 1, 0), |
| 179 | SOC_SINGLE("DAC Invert", AK4940_03_LINEOUT_CONTROL, 6, 1, 0), |
| 180 | |
| 181 | SOC_ENUM("ADC Input Type", ak4940_outaddiffn_enum[0]), |
| 182 | SOC_ENUM("Lineout Type", ak4940_outnsel_enum[0]), |
| 183 | SOC_ENUM("BICK Edge Direction", ak4940_bckp_enum[0]), |
| 184 | SOC_ENUM("BICK Frequency", ak4940_bickfreq_enum[0]), |
| 185 | |
| 186 | #ifdef AK4940_DEBUG |
| 187 | SOC_ENUM_EXT("Reg Read", ak4940_enum2[0], get_test_reg, set_test_reg), |
| 188 | #endif |
| 189 | }; |
| 190 | |
| 191 | static int ak4940_ClockReset(struct snd_soc_dapm_widget *w, |
| 192 | struct snd_kcontrol *kcontrol, int event) |
| 193 | { |
| 194 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 195 | |
| 196 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 197 | |
| 198 | switch (event) { |
| 199 | case SND_SOC_DAPM_PRE_PMU: |
| 200 | akdbgprt("\t[AK4940] SND_SOC_DAPM_PRE_PMU\n"); |
| 201 | /* CKRESETN bit = 1 */ |
| 202 | snd_soc_component_update_bits(component, AK4940_00_CLOCK_MANAGEMENT, 0x08, 0x08); |
| 203 | mdelay(10); |
| 204 | /* CRESETN bit = 1 */ |
| 205 | snd_soc_component_update_bits(component, AK4940_04_POWER_MANAGEMENT, 0x01, 0x01); |
| 206 | break; |
| 207 | |
| 208 | case SND_SOC_DAPM_POST_PMD: |
| 209 | akdbgprt("\t[AK4940] SND_SOC_DAPM_POST_PMD\n"); |
| 210 | /* CKRESETN bit = 0 */ |
| 211 | snd_soc_component_update_bits(component, AK4940_00_CLOCK_MANAGEMENT, 0x08, 0x00); |
| 212 | /* CRESETN bit = 0 */ |
| 213 | snd_soc_component_update_bits(component, AK4940_04_POWER_MANAGEMENT, 0x01, 0x00); |
| 214 | break; |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static const char * const ak4940_ain_select_texts[] = { |
| 220 | "AIN1", "AIN2" /* INP1/INN1, INP2/INN2 */ |
| 221 | }; |
| 222 | |
| 223 | static const struct soc_enum ak4940_ain_mux_enum = |
| 224 | SOC_ENUM_SINGLE(AK4940_04_POWER_MANAGEMENT, 3, |
| 225 | ARRAY_SIZE(ak4940_ain_select_texts), ak4940_ain_select_texts); |
| 226 | |
| 227 | static const struct snd_kcontrol_new ak4940_ain_mux_control = |
| 228 | SOC_DAPM_ENUM("AIN Select", ak4940_ain_mux_enum); |
| 229 | |
| 230 | static const char * const ak4940_dacsw1_select_texts[] = { |
| 231 | "VCOM", "DIRECT", "DAC" |
| 232 | }; |
| 233 | |
| 234 | static const struct soc_enum ak4940_dacsw1_mux_enum = |
| 235 | SOC_ENUM_SINGLE(AK4940_03_LINEOUT_CONTROL, 0, |
| 236 | ARRAY_SIZE(ak4940_dacsw1_select_texts), ak4940_dacsw1_select_texts); |
| 237 | |
| 238 | static const struct snd_kcontrol_new ak4940_dacsw1_mux_control = |
| 239 | SOC_DAPM_ENUM("OUT1 MUX", ak4940_dacsw1_mux_enum); |
| 240 | |
| 241 | static const char * const ak4940_dacsw2_select_texts[] = { |
| 242 | "VCOM", "DIRECT", "DAC" |
| 243 | }; |
| 244 | |
| 245 | static const struct soc_enum ak4940_dacsw2_mux_enum = |
| 246 | SOC_ENUM_SINGLE(AK4940_03_LINEOUT_CONTROL, 2, |
| 247 | ARRAY_SIZE(ak4940_dacsw2_select_texts), ak4940_dacsw2_select_texts); |
| 248 | |
| 249 | static const struct snd_kcontrol_new ak4940_dacsw2_mux_control = |
| 250 | SOC_DAPM_ENUM("OUT2 MUX", ak4940_dacsw2_mux_enum); |
| 251 | |
| 252 | static const char * const ak4940_dacsw3_select_texts[] = { |
| 253 | "VCOM", "DAC Lineout(+)", "DAC Lineout(-)" |
| 254 | }; |
| 255 | |
| 256 | static const struct soc_enum ak4940_dacsw3_mux_enum = |
| 257 | SOC_ENUM_SINGLE(AK4940_03_LINEOUT_CONTROL, 4, |
| 258 | ARRAY_SIZE(ak4940_dacsw3_select_texts), ak4940_dacsw3_select_texts); |
| 259 | |
| 260 | static const struct snd_kcontrol_new ak4940_dacsw3_mux_control = |
| 261 | SOC_DAPM_ENUM("OUT3 MUX", ak4940_dacsw3_mux_enum); |
| 262 | |
| 263 | static const struct snd_soc_dapm_widget ak4940_dapm_widgets[] = { |
| 264 | SND_SOC_DAPM_SUPPLY_S("Clock Power", 1, SND_SOC_NOPM, 0, 0, |
| 265 | ak4940_ClockReset, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 266 | |
| 267 | /* ADC */ |
| 268 | SND_SOC_DAPM_ADC("ADC", NULL, AK4940_04_POWER_MANAGEMENT, 5, 0), |
| 269 | |
| 270 | /* DAC */ |
| 271 | SND_SOC_DAPM_DAC("DAC", NULL, AK4940_04_POWER_MANAGEMENT, 4, 0), |
| 272 | |
| 273 | /* Analog Input */ |
| 274 | SND_SOC_DAPM_MUX("AIN MUX", SND_SOC_NOPM, 0, 0, &ak4940_ain_mux_control), |
| 275 | SND_SOC_DAPM_INPUT("AIN1"), |
| 276 | SND_SOC_DAPM_INPUT("AIN2"), |
| 277 | |
| 278 | /* Analog Output */ |
| 279 | SND_SOC_DAPM_OUTPUT("AOUT1"), |
| 280 | SND_SOC_DAPM_OUTPUT("AOUT2"), |
| 281 | SND_SOC_DAPM_OUTPUT("AOUT3"), |
| 282 | |
| 283 | SND_SOC_DAPM_MUX("OUT1 MUX", SND_SOC_NOPM, 0, 0, &ak4940_dacsw1_mux_control), |
| 284 | SND_SOC_DAPM_MUX("OUT2 MUX", SND_SOC_NOPM, 0, 0, &ak4940_dacsw2_mux_control), |
| 285 | SND_SOC_DAPM_MUX("OUT3 MUX", SND_SOC_NOPM, 0, 0, &ak4940_dacsw3_mux_control), |
| 286 | |
| 287 | SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0), |
| 288 | SND_SOC_DAPM_AIF_IN("SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0), |
| 289 | }; |
| 290 | |
| 291 | static const struct snd_soc_dapm_route ak4940_intercon[] = { |
| 292 | { "ADC", NULL, "Clock Power" }, |
| 293 | { "DAC", NULL, "Clock Power" }, |
| 294 | |
| 295 | { "AIN MUX", "AIN1", "AIN1" }, |
| 296 | { "AIN MUX", "AIN2", "AIN2" }, |
| 297 | { "ADC", NULL, "AIN MUX" }, |
| 298 | { "SDTO", NULL, "ADC" }, |
| 299 | |
| 300 | { "DAC", NULL, "SDTI" }, |
| 301 | |
| 302 | { "OUT1 MUX", "DAC", "DAC" }, |
| 303 | { "OUT1 MUX", "DIRECT", "AIN1" }, |
| 304 | { "AOUT1", NULL, "OUT1 MUX" }, |
| 305 | |
| 306 | { "OUT2 MUX", "DAC", "DAC" }, |
| 307 | { "OUT2 MUX", "DIRECT", "AIN2" }, |
| 308 | { "AOUT2", NULL, "OUT2 MUX" }, |
| 309 | |
| 310 | { "OUT3 MUX", "DAC Lineout(+)", "DAC" }, |
| 311 | { "OUT3 MUX", "DAC Lineout(-)", "DAC" }, |
| 312 | { "AOUT3", NULL, "OUT3 MUX" }, |
| 313 | }; |
| 314 | |
| 315 | static int ak4940_set_audioif(struct snd_soc_component *component, int mode) |
| 316 | { |
| 317 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 318 | int lrif_doif, bitfs; |
| 319 | int ret; |
| 320 | u8 format; |
| 321 | unsigned int value; |
| 322 | |
| 323 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 324 | |
| 325 | ak4940->ifSetMode |= mode; |
| 326 | |
| 327 | bitfs = snd_soc_component_read(component, AK4940_01_AUDIO_IF_SETTING, &value); |
| 328 | bitfs &= AK4940_MASK_BITFS; /* D1-D0 bit */ |
| 329 | |
| 330 | format = 0; |
| 331 | ret = 0; |
| 332 | akdbgprt("\t[AK4940] %s ak4940->aif_format(%d)\n", __func__, ak4940->aif_format); |
| 333 | akdbgprt("\t[AK4940] %s bitfs(%d)\n", __func__, bitfs); |
| 334 | akdbgprt("\t[AK4940] %s ak4940->sxxlebit(%d)\n", __func__, ak4940->sxxlebit); |
| 335 | |
| 336 | switch (ak4940->aif_format) { |
| 337 | case SND_SOC_DAIFMT_I2S: /* I2S I/F */ |
| 338 | if (bitfs == 2) { /* 32fs */ |
| 339 | if (ak4940->sxxlebit == 16) |
| 340 | lrif_doif = AK4940_DIF_16I2S_MODE; |
| 341 | else |
| 342 | ret = -EINVAL; |
| 343 | } else { |
| 344 | lrif_doif = AK4940_DIF_16I2S_MODE; |
| 345 | } |
| 346 | break; |
| 347 | case SND_SOC_DAIFMT_LEFT_J: /* Left Justified */ |
| 348 | if (bitfs == 0) /* 64fs */ |
| 349 | lrif_doif = AK4940_DIF_24MSB_MODE; |
| 350 | else |
| 351 | ret = -EINVAL; |
| 352 | break; |
| 353 | case SND_SOC_DAIFMT_RIGHT_J: /* Right Justified */ |
| 354 | if (ak4940->sxxlebit == 16) |
| 355 | format = AK4940_DIF_16LSB_MODE; |
| 356 | else if (bitfs == 2) |
| 357 | ret = -EINVAL; |
| 358 | else if (ak4940->sxxlebit == 20) |
| 359 | format = AK4940_DIF_20LSB_MODE; |
| 360 | else if (ak4940->sxxlebit == 24) |
| 361 | format = AK4940_DIF_24LSB_MODE; |
| 362 | break; |
| 363 | case SND_SOC_DAIFMT_DSP_A: /* PCM Short Frame */ |
| 364 | if (bitfs == 0) /* 64fs */ |
| 365 | lrif_doif = AK4940_DIF_24PCMSHORT_MODE; |
| 366 | else |
| 367 | ret = -EINVAL; |
| 368 | break; |
| 369 | case SND_SOC_DAIFMT_DSP_B: /* PCM Long Frame */ |
| 370 | if (bitfs == 0) /* 64fs */ |
| 371 | lrif_doif = AK4940_DIF_24PCMLONG_MODE; |
| 372 | else |
| 373 | ret = -EINVAL; |
| 374 | break; |
| 375 | default: |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | if (ret != 0) { |
| 379 | akdbgprt("\t[AK4940] %s bitfs(%d) ifSetMode(%d)\n", __func__, ret, ak4940->ifSetMode); |
| 380 | if (ak4940->ifSetMode == 3) { |
| 381 | ak4940->ifSetMode = 0; |
| 382 | return ret; |
| 383 | } else { |
| 384 | return 0; |
| 385 | } |
| 386 | } |
| 387 | /* D7-D2 bit */ |
| 388 | snd_soc_component_update_bits(component, AK4940_01_AUDIO_IF_SETTING, |
| 389 | AK4940_MASK_LRIF_DOIF, lrif_doif); |
| 390 | |
| 391 | if (ak4940->ifSetMode == 3) |
| 392 | ak4940->ifSetMode = 0; |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int ak4940_hw_params(struct snd_pcm_substream *substream, |
| 397 | struct snd_pcm_hw_params *params, |
| 398 | struct snd_soc_dai *dai) |
| 399 | { |
| 400 | struct snd_soc_component *component = dai->component; |
| 401 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 402 | int ret; |
| 403 | |
| 404 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 405 | |
| 406 | ak4940->fs = params_rate(params); |
| 407 | pr_info("\t[AK4940] %s fs=%d\n", __func__, ak4940->fs); |
| 408 | |
| 409 | if ((ak4940->fs == 8000) || (ak4940->fs == 7350)) { |
| 410 | ak4940->dfsx = 0x00; |
| 411 | } else if ((ak4940->fs == 12000) || (ak4940->fs == 11025)) { |
| 412 | ak4940->dfsx = 0x01; |
| 413 | } else if ((ak4940->fs == 16000) || (ak4940->fs == 14700)) { |
| 414 | ak4940->dfsx = 0x02; |
| 415 | } else if ((ak4940->fs == 24000) || (ak4940->fs == 22050)) { |
| 416 | ak4940->dfsx = 0x03; |
| 417 | } else if ((ak4940->fs == 32000) || (ak4940->fs == 29400)) { |
| 418 | ak4940->dfsx = 0x04; |
| 419 | } else if ((ak4940->fs == 48000) || (ak4940->fs == 44100)) { |
| 420 | ak4940->dfsx = 0x05; |
| 421 | } else { |
| 422 | dev_err(component->dev, "sampling frequency unsupported"); |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | /* DFS[2:0] */ |
| 426 | snd_soc_component_update_bits(component, AK4940_00_CLOCK_MANAGEMENT, 0x07, ak4940->dfsx); |
| 427 | |
| 428 | switch (params_format(params)) { |
| 429 | case SNDRV_PCM_FORMAT_S16_LE: |
| 430 | akdbgprt("\t[AK4940]S16_LE\n"); |
| 431 | ak4940->sxxlebit = 16; |
| 432 | break; |
| 433 | |
| 434 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 435 | akdbgprt("\t[AK4940]S20_3LE\n"); |
| 436 | ak4940->sxxlebit = 20; |
| 437 | break; |
| 438 | |
| 439 | case SNDRV_PCM_FORMAT_S24_LE: |
| 440 | akdbgprt("\t[AK4940]S24_LE\n"); |
| 441 | ak4940->sxxlebit = 24; |
| 442 | break; |
| 443 | |
| 444 | default: |
| 445 | pr_err("%s: invalid Audio format %u\n", __func__, params_format(params)); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | ret = ak4940_set_audioif(component, 2); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static int ak4940_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 454 | unsigned int freq, int dir) |
| 455 | { |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int ak4940_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 460 | { |
| 461 | struct snd_soc_component *component = dai->component; |
| 462 | int ret; |
| 463 | |
| 464 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 465 | |
| 466 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 467 | |
| 468 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 469 | case SND_SOC_DAIFMT_CBS_CFS: /* BICK, LRCK (slave) */ |
| 470 | break; |
| 471 | case SND_SOC_DAIFMT_CBM_CFM: /* BICK, LRCK input (master) */ |
| 472 | case SND_SOC_DAIFMT_CBS_CFM: /* N/A */ |
| 473 | case SND_SOC_DAIFMT_CBM_CFS: /* N/A */ |
| 474 | default: |
| 475 | dev_err(component->dev, "Clock mode unsupported master/slave"); |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | ak4940->aif_format = (fmt & SND_SOC_DAIFMT_FORMAT_MASK); |
| 479 | ret = ak4940_set_audioif(component, 1); |
| 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | static bool ak4940_volatile(struct device *dev, unsigned int reg) |
| 485 | { |
| 486 | bool ret; |
| 487 | |
| 488 | #ifdef AK4940_DEBUG |
| 489 | ret = true; |
| 490 | #else |
| 491 | ret = false; |
| 492 | #endif |
| 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | static bool ak4940_readable(struct device *dev, unsigned int reg) |
| 498 | { |
| 499 | if (reg <= AK4940_MAX_REGISTER) |
| 500 | return true; |
| 501 | else |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | static bool ak4940_writeable(struct device *dev, unsigned int reg) |
| 506 | { |
| 507 | if (reg <= AK4940_MAX_REGISTER) |
| 508 | return true; |
| 509 | else |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | /* * for AK4940 */ |
| 514 | static int ak4940_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai) |
| 515 | { |
| 516 | int ret = 0; |
| 517 | |
| 518 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 519 | |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int ak4940_set_bias_level(struct snd_soc_component *component, |
| 524 | enum snd_soc_bias_level level) |
| 525 | { |
| 526 | /* u8 reg; */ |
| 527 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
| 528 | |
| 529 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 530 | |
| 531 | switch (level) { |
| 532 | case SND_SOC_BIAS_ON: |
| 533 | case SND_SOC_BIAS_PREPARE: |
| 534 | case SND_SOC_BIAS_STANDBY: |
| 535 | break; |
| 536 | case SND_SOC_BIAS_OFF: |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | dapm->bias_level = level; |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int ak4940_set_dai_mute(struct snd_soc_dai *dai, int mute) |
| 546 | { |
| 547 | struct snd_soc_component *component = dai->component; |
| 548 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 549 | int ret = 0; |
| 550 | int ndt; |
| 551 | |
| 552 | akdbgprt("\t[AK4940] %s mute[%s]\n", __func__, mute ? "ON":"OFF"); |
| 553 | |
| 554 | ndt = 4080000 / ak4940->fs; |
| 555 | if (mute) { |
| 556 | /* SMUTE: 1 , MUTE */ |
| 557 | ret = snd_soc_component_update_bits(component, AK4940_08_SOFTMUTE_CONTROL, 0x22, 0x02); |
| 558 | mdelay(ndt); |
| 559 | } else{ |
| 560 | /* SMUTE: 0 ,NORMAL operation */ |
| 561 | ret = snd_soc_component_update_bits(component, AK4940_08_SOFTMUTE_CONTROL, 0x22, 0x00); |
| 562 | mdelay(ndt); |
| 563 | } |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | #define AK4940_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 568 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 569 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ |
| 570 | SNDRV_PCM_RATE_48000) |
| 571 | |
| 572 | #define AK4940_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 573 | SNDRV_PCM_FMTBIT_S24_LE) |
| 574 | |
| 575 | static struct snd_soc_dai_ops ak4940_dai_ops = { |
| 576 | .hw_params = ak4940_hw_params, |
| 577 | .set_sysclk = ak4940_set_dai_sysclk, |
| 578 | .set_fmt = ak4940_set_dai_fmt, |
| 579 | .trigger = ak4940_trigger, |
| 580 | .digital_mute = ak4940_set_dai_mute, |
| 581 | }; |
| 582 | |
| 583 | struct snd_soc_dai_driver ak4940_dai[] = { |
| 584 | { |
| 585 | .name = "ak4940-aif", |
| 586 | .playback = { |
| 587 | .stream_name = "Playback", |
| 588 | .channels_min = 1, |
| 589 | .channels_max = 1, |
| 590 | .rates = AK4940_RATES, |
| 591 | .formats = AK4940_FORMATS, |
| 592 | }, |
| 593 | .capture = { |
| 594 | .stream_name = "Capture", |
| 595 | .channels_min = 1, |
| 596 | .channels_max = 1, |
| 597 | .rates = AK4940_RATES, |
| 598 | .formats = AK4940_FORMATS, |
| 599 | }, |
| 600 | .ops = &ak4940_dai_ops, |
| 601 | }, |
| 602 | }; |
| 603 | |
| 604 | static int ak4940_init_reg(struct snd_soc_component *component) |
| 605 | { |
| 606 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 607 | |
| 608 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 609 | |
| 610 | if (ak4940->pdn_gpio != -1) { |
| 611 | gpio_set_value(ak4940->pdn_gpio, 0); |
| 612 | mdelay(1); |
| 613 | gpio_set_value(ak4940->pdn_gpio, 1); |
| 614 | mdelay(1); |
| 615 | } |
| 616 | return(0); |
| 617 | } |
| 618 | |
| 619 | static int ak4940_parse_dt(struct ak4940_priv *ak4940) |
| 620 | { |
| 621 | struct device *dev; |
| 622 | struct device_node *np; |
| 623 | |
| 624 | dev = &(ak4940->i2c->dev); |
| 625 | |
| 626 | np = dev->of_node; |
| 627 | |
| 628 | if (!np) |
| 629 | return 0; |
| 630 | |
| 631 | akdbgprt("Read PDN pin from device tree\n"); |
| 632 | |
| 633 | ak4940->pdn_gpio = of_get_named_gpio(np, "ak4940,pdn-gpio", 0); |
| 634 | if (ak4940->pdn_gpio < 0) { |
| 635 | ak4940->pdn_gpio = -1; |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | if (!gpio_is_valid(ak4940->pdn_gpio)) { |
| 640 | akdbgprt(KERN_ERR "ak4940 pdn pin(%u) is invalid\n", ak4940->pdn_gpio); |
| 641 | ak4940->pdn_gpio = -1; |
| 642 | } |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int ak4940_probe(struct snd_soc_component *component) |
| 647 | { |
| 648 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 649 | int ret = 0; |
| 650 | |
| 651 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 652 | |
| 653 | ret = ak4940_parse_dt(ak4940); |
| 654 | |
| 655 | if (ak4940->pdn_gpio != -1) { |
| 656 | ret = gpio_request(ak4940->pdn_gpio, "ak4940 pdn"); |
| 657 | akdbgprt("\t[AK4940] %s : gpio_request ret = %d\n", __func__, ret); |
| 658 | gpio_direction_output(ak4940->pdn_gpio, 0); |
| 659 | } |
| 660 | ak4940_init_reg(component); |
| 661 | |
| 662 | ak4940->fs = 48000; |
| 663 | ak4940->ifSetMode = 0; |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | static void ak4940_remove(struct snd_soc_component *component) |
| 669 | { |
| 670 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 671 | |
| 672 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 673 | |
| 674 | ak4940_set_bias_level(component, SND_SOC_BIAS_OFF); |
| 675 | if (ak4940->pdn_gpio != -1) { |
| 676 | gpio_set_value(ak4940->pdn_gpio, 0); |
| 677 | mdelay(1); |
| 678 | gpio_free(ak4940->pdn_gpio); |
| 679 | mdelay(1); |
| 680 | } |
| 681 | |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | static int ak4940_suspend(struct snd_soc_component *component) |
| 686 | { |
| 687 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 688 | |
| 689 | ak4940_set_bias_level(component, SND_SOC_BIAS_OFF); |
| 690 | |
| 691 | regcache_cache_only(ak4940->regmap, true); |
| 692 | regcache_mark_dirty(ak4940->regmap); |
| 693 | |
| 694 | if (ak4940->pdn_gpio != -1) { |
| 695 | gpio_set_value(ak4940->pdn_gpio, 0); |
| 696 | akdbgprt("\t[AK4940] %s External PDN[OFF]\n", __func__); |
| 697 | mdelay(1); |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static int ak4940_resume(struct snd_soc_component *component) |
| 704 | { |
| 705 | struct ak4940_priv *ak4940 = snd_soc_component_get_drvdata(component); |
| 706 | |
| 707 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 708 | |
| 709 | if (ak4940->pdn_gpio != -1) { |
| 710 | gpio_set_value(ak4940->pdn_gpio, 0); |
| 711 | akdbgprt("\t[AK4940] %s External PDN[OFF]\n", __func__); |
| 712 | mdelay(1); |
| 713 | gpio_set_value(ak4940->pdn_gpio, 1); |
| 714 | akdbgprt("\t[AK4940] %s External PDN[ON]\n", __func__); |
| 715 | mdelay(1); |
| 716 | } |
| 717 | |
| 718 | regcache_cache_only(ak4940->regmap, false); |
| 719 | regcache_sync(ak4940->regmap); |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static const struct snd_soc_component_driver soc_codec_dev_ak4940 = { |
| 725 | .probe = ak4940_probe, |
| 726 | .remove = ak4940_remove, |
| 727 | .suspend = ak4940_suspend, |
| 728 | .resume = ak4940_resume, |
| 729 | |
| 730 | .set_bias_level = ak4940_set_bias_level, |
| 731 | |
| 732 | .controls = ak4940_snd_controls, |
| 733 | .num_controls = ARRAY_SIZE(ak4940_snd_controls), |
| 734 | .dapm_widgets = ak4940_dapm_widgets, |
| 735 | .num_dapm_widgets = ARRAY_SIZE(ak4940_dapm_widgets), |
| 736 | .dapm_routes = ak4940_intercon, |
| 737 | .num_dapm_routes = ARRAY_SIZE(ak4940_intercon), |
| 738 | |
| 739 | .idle_bias_on = 1, |
| 740 | .endianness = 1, |
| 741 | .non_legacy_dai_naming = 1, |
| 742 | }; |
| 743 | |
| 744 | EXPORT_SYMBOL_GPL(soc_codec_dev_ak4940); |
| 745 | |
| 746 | static const struct regmap_config ak4940_regmap = { |
| 747 | .reg_bits = 8, |
| 748 | .val_bits = 8, |
| 749 | |
| 750 | .max_register = AK4940_MAX_REGISTER, |
| 751 | .volatile_reg = ak4940_volatile, |
| 752 | .writeable_reg = ak4940_writeable, |
| 753 | .readable_reg = ak4940_readable, |
| 754 | |
| 755 | .reg_defaults = ak4940_reg, |
| 756 | .num_reg_defaults = ARRAY_SIZE(ak4940_reg), |
| 757 | .cache_type = REGCACHE_RBTREE, |
| 758 | }; |
| 759 | |
| 760 | static const struct of_device_id ak4940_i2c_dt_ids[] = { |
| 761 | { .compatible = "akm,ak4940"}, |
| 762 | { } |
| 763 | }; |
| 764 | MODULE_DEVICE_TABLE(of, ak4940_i2c_dt_ids); |
| 765 | |
| 766 | static int ak4940_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id) |
| 767 | { |
| 768 | struct ak4940_priv *ak4940; |
| 769 | int ret = 0; |
| 770 | |
| 771 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 772 | |
| 773 | ak4940 = devm_kzalloc(&i2c->dev, sizeof(struct ak4940_priv), GFP_KERNEL); |
| 774 | if (ak4940 == NULL) |
| 775 | return -ENOMEM; |
| 776 | |
| 777 | ak4940->regmap = devm_regmap_init_i2c(i2c, &ak4940_regmap); |
| 778 | if (IS_ERR(ak4940->regmap)) { |
| 779 | devm_kfree(&i2c->dev, ak4940); |
| 780 | return PTR_ERR(ak4940->regmap); |
| 781 | } |
| 782 | |
| 783 | i2c_set_clientdata(i2c, ak4940); |
| 784 | ak4940->i2c = i2c; |
| 785 | |
| 786 | ret = devm_snd_soc_register_component(&i2c->dev, |
| 787 | &soc_codec_dev_ak4940, &ak4940_dai[0], ARRAY_SIZE(ak4940_dai)); |
| 788 | if (ret < 0) { |
| 789 | devm_kfree(&i2c->dev, ak4940); |
| 790 | akdbgprt("\t[AK4940 Error!] %s(%d)\n", __func__, __LINE__); |
| 791 | } |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | static int ak4940_i2c_remove(struct i2c_client *client) |
| 796 | { |
| 797 | snd_soc_unregister_component(&client->dev); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static const struct i2c_device_id ak4940_i2c_id[] = { |
| 802 | { "ak4940", 0 }, |
| 803 | { } |
| 804 | }; |
| 805 | MODULE_DEVICE_TABLE(i2c, ak4940_i2c_id); |
| 806 | |
| 807 | static struct i2c_driver ak4940_i2c_driver = { |
| 808 | .driver = { |
| 809 | .name = "ak4940", |
| 810 | .of_match_table = of_match_ptr(ak4940_i2c_dt_ids), |
| 811 | }, |
| 812 | .probe = ak4940_i2c_probe, |
| 813 | .remove = ak4940_i2c_remove, |
| 814 | .id_table = ak4940_i2c_id, |
| 815 | }; |
| 816 | |
| 817 | static int __init ak4940_modinit(void) |
| 818 | { |
| 819 | akdbgprt("\t[AK4940] %s(%d)\n", __func__, __LINE__); |
| 820 | |
| 821 | return i2c_add_driver(&ak4940_i2c_driver); |
| 822 | } |
| 823 | |
| 824 | module_init(ak4940_modinit); |
| 825 | |
| 826 | static void __exit ak4940_exit(void) |
| 827 | { |
| 828 | i2c_del_driver(&ak4940_i2c_driver); |
| 829 | } |
| 830 | module_exit(ak4940_exit); |
| 831 | |
| 832 | MODULE_DESCRIPTION("ASoC ak4940 driver"); |
| 833 | MODULE_LICENSE("GPL v2"); |