Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 1 | /* |
| 2 | * ak4641.c -- AK4641 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Harald Welte <laforge@gnufiish.org> |
| 5 | * Copyright (C) 2011 Dmitry Artamonow <mad_soft@inbox.ru> |
| 6 | * |
| 7 | * Based on ak4535.c by Richard Purdie |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/i2c.h> |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/initval.h> |
| 27 | #include <sound/tlv.h> |
| 28 | #include <sound/ak4641.h> |
| 29 | |
Lars-Peter Clausen | 5841ce1 | 2016-10-15 21:32:41 +0200 | [diff] [blame] | 30 | /* AK4641 register space */ |
| 31 | #define AK4641_PM1 0x00 |
| 32 | #define AK4641_PM2 0x01 |
| 33 | #define AK4641_SIG1 0x02 |
| 34 | #define AK4641_SIG2 0x03 |
| 35 | #define AK4641_MODE1 0x04 |
| 36 | #define AK4641_MODE2 0x05 |
| 37 | #define AK4641_DAC 0x06 |
| 38 | #define AK4641_MIC 0x07 |
| 39 | #define AK4641_TIMER 0x08 |
| 40 | #define AK4641_ALC1 0x09 |
| 41 | #define AK4641_ALC2 0x0a |
| 42 | #define AK4641_PGA 0x0b |
| 43 | #define AK4641_LATT 0x0c |
| 44 | #define AK4641_RATT 0x0d |
| 45 | #define AK4641_VOL 0x0e |
| 46 | #define AK4641_STATUS 0x0f |
| 47 | #define AK4641_EQLO 0x10 |
| 48 | #define AK4641_EQMID 0x11 |
| 49 | #define AK4641_EQHI 0x12 |
| 50 | #define AK4641_BTIF 0x13 |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 51 | |
| 52 | /* codec private data */ |
| 53 | struct ak4641_priv { |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 54 | struct regmap *regmap; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 55 | unsigned int sysclk; |
| 56 | int deemph; |
| 57 | int playback_fs; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * ak4641 register cache |
| 62 | */ |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 63 | static const struct reg_default ak4641_reg_defaults[] = { |
| 64 | { 0, 0x00 }, { 1, 0x80 }, { 2, 0x00 }, { 3, 0x80 }, |
| 65 | { 4, 0x02 }, { 5, 0x00 }, { 6, 0x11 }, { 7, 0x05 }, |
| 66 | { 8, 0x00 }, { 9, 0x00 }, { 10, 0x36 }, { 11, 0x10 }, |
| 67 | { 12, 0x00 }, { 13, 0x00 }, { 14, 0x57 }, { 15, 0x00 }, |
| 68 | { 16, 0x88 }, { 17, 0x88 }, { 18, 0x08 }, { 19, 0x08 } |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const int deemph_settings[] = {44100, 0, 48000, 32000}; |
| 72 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 73 | static int ak4641_set_deemph(struct snd_soc_component *component) |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 74 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 75 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 76 | int i, best = 0; |
| 77 | |
| 78 | for (i = 0 ; i < ARRAY_SIZE(deemph_settings); i++) { |
| 79 | /* if deemphasis is on, select the nearest available rate */ |
| 80 | if (ak4641->deemph && deemph_settings[i] != 0 && |
| 81 | abs(deemph_settings[i] - ak4641->playback_fs) < |
| 82 | abs(deemph_settings[best] - ak4641->playback_fs)) |
| 83 | best = i; |
| 84 | |
| 85 | if (!ak4641->deemph && deemph_settings[i] == 0) |
| 86 | best = i; |
| 87 | } |
| 88 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 89 | dev_dbg(component->dev, "Set deemphasis %d\n", best); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 90 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 91 | return snd_soc_component_update_bits(component, AK4641_DAC, 0x3, best); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int ak4641_put_deemph(struct snd_kcontrol *kcontrol, |
| 95 | struct snd_ctl_elem_value *ucontrol) |
| 96 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 97 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 98 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
Takashi Iwai | 08641d9 | 2015-03-10 12:39:04 +0100 | [diff] [blame] | 99 | int deemph = ucontrol->value.integer.value[0]; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 100 | |
| 101 | if (deemph > 1) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | ak4641->deemph = deemph; |
| 105 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 106 | return ak4641_set_deemph(component); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int ak4641_get_deemph(struct snd_kcontrol *kcontrol, |
| 110 | struct snd_ctl_elem_value *ucontrol) |
| 111 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 112 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 113 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 114 | |
Takashi Iwai | 08641d9 | 2015-03-10 12:39:04 +0100 | [diff] [blame] | 115 | ucontrol->value.integer.value[0] = ak4641->deemph; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 116 | return 0; |
| 117 | }; |
| 118 | |
| 119 | static const char *ak4641_mono_out[] = {"(L + R)/2", "Hi-Z"}; |
| 120 | static const char *ak4641_hp_out[] = {"Stereo", "Mono"}; |
| 121 | static const char *ak4641_mic_select[] = {"Internal", "External"}; |
| 122 | static const char *ak4641_mic_or_dac[] = {"Microphone", "Voice DAC"}; |
| 123 | |
| 124 | |
| 125 | static const DECLARE_TLV_DB_SCALE(mono_gain_tlv, -1700, 2300, 0); |
| 126 | static const DECLARE_TLV_DB_SCALE(mic_boost_tlv, 0, 2000, 0); |
| 127 | static const DECLARE_TLV_DB_SCALE(eq_tlv, -1050, 150, 0); |
| 128 | static const DECLARE_TLV_DB_SCALE(master_tlv, -12750, 50, 0); |
| 129 | static const DECLARE_TLV_DB_SCALE(mic_stereo_sidetone_tlv, -2700, 300, 0); |
| 130 | static const DECLARE_TLV_DB_SCALE(mic_mono_sidetone_tlv, -400, 400, 0); |
| 131 | static const DECLARE_TLV_DB_SCALE(capture_tlv, -800, 50, 0); |
| 132 | static const DECLARE_TLV_DB_SCALE(alc_tlv, -800, 50, 0); |
| 133 | static const DECLARE_TLV_DB_SCALE(aux_in_tlv, -2100, 300, 0); |
| 134 | |
| 135 | |
Takashi Iwai | cbf6242 | 2014-02-18 10:43:45 +0100 | [diff] [blame] | 136 | static SOC_ENUM_SINGLE_DECL(ak4641_mono_out_enum, |
| 137 | AK4641_SIG1, 6, ak4641_mono_out); |
| 138 | static SOC_ENUM_SINGLE_DECL(ak4641_hp_out_enum, |
| 139 | AK4641_MODE2, 2, ak4641_hp_out); |
| 140 | static SOC_ENUM_SINGLE_DECL(ak4641_mic_select_enum, |
| 141 | AK4641_MIC, 1, ak4641_mic_select); |
| 142 | static SOC_ENUM_SINGLE_DECL(ak4641_mic_or_dac_enum, |
| 143 | AK4641_BTIF, 4, ak4641_mic_or_dac); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 144 | |
| 145 | static const struct snd_kcontrol_new ak4641_snd_controls[] = { |
| 146 | SOC_ENUM("Mono 1 Output", ak4641_mono_out_enum), |
| 147 | SOC_SINGLE_TLV("Mono 1 Gain Volume", AK4641_SIG1, 7, 1, 1, |
| 148 | mono_gain_tlv), |
| 149 | SOC_ENUM("Headphone Output", ak4641_hp_out_enum), |
| 150 | SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0, |
| 151 | ak4641_get_deemph, ak4641_put_deemph), |
| 152 | |
| 153 | SOC_SINGLE_TLV("Mic Boost Volume", AK4641_MIC, 0, 1, 0, mic_boost_tlv), |
| 154 | |
| 155 | SOC_SINGLE("ALC Operation Time", AK4641_TIMER, 0, 3, 0), |
| 156 | SOC_SINGLE("ALC Recovery Time", AK4641_TIMER, 2, 3, 0), |
| 157 | SOC_SINGLE("ALC ZC Time", AK4641_TIMER, 4, 3, 0), |
| 158 | |
| 159 | SOC_SINGLE("ALC 1 Switch", AK4641_ALC1, 5, 1, 0), |
| 160 | |
| 161 | SOC_SINGLE_TLV("ALC Volume", AK4641_ALC2, 0, 71, 0, alc_tlv), |
| 162 | SOC_SINGLE("Left Out Enable Switch", AK4641_SIG2, 1, 1, 0), |
| 163 | SOC_SINGLE("Right Out Enable Switch", AK4641_SIG2, 0, 1, 0), |
| 164 | |
| 165 | SOC_SINGLE_TLV("Capture Volume", AK4641_PGA, 0, 71, 0, capture_tlv), |
| 166 | |
| 167 | SOC_DOUBLE_R_TLV("Master Playback Volume", AK4641_LATT, |
| 168 | AK4641_RATT, 0, 255, 1, master_tlv), |
| 169 | |
| 170 | SOC_SINGLE_TLV("AUX In Volume", AK4641_VOL, 0, 15, 0, aux_in_tlv), |
| 171 | |
| 172 | SOC_SINGLE("Equalizer Switch", AK4641_DAC, 2, 1, 0), |
| 173 | SOC_SINGLE_TLV("EQ1 100 Hz Volume", AK4641_EQLO, 0, 15, 1, eq_tlv), |
| 174 | SOC_SINGLE_TLV("EQ2 250 Hz Volume", AK4641_EQLO, 4, 15, 1, eq_tlv), |
| 175 | SOC_SINGLE_TLV("EQ3 1 kHz Volume", AK4641_EQMID, 0, 15, 1, eq_tlv), |
| 176 | SOC_SINGLE_TLV("EQ4 3.5 kHz Volume", AK4641_EQMID, 4, 15, 1, eq_tlv), |
| 177 | SOC_SINGLE_TLV("EQ5 10 kHz Volume", AK4641_EQHI, 0, 15, 1, eq_tlv), |
| 178 | }; |
| 179 | |
| 180 | /* Mono 1 Mixer */ |
| 181 | static const struct snd_kcontrol_new ak4641_mono1_mixer_controls[] = { |
| 182 | SOC_DAPM_SINGLE_TLV("Mic Mono Sidetone Volume", AK4641_VOL, 7, 1, 0, |
| 183 | mic_mono_sidetone_tlv), |
| 184 | SOC_DAPM_SINGLE("Mic Mono Sidetone Switch", AK4641_SIG1, 4, 1, 0), |
| 185 | SOC_DAPM_SINGLE("Mono Playback Switch", AK4641_SIG1, 5, 1, 0), |
| 186 | }; |
| 187 | |
| 188 | /* Stereo Mixer */ |
| 189 | static const struct snd_kcontrol_new ak4641_stereo_mixer_controls[] = { |
| 190 | SOC_DAPM_SINGLE_TLV("Mic Sidetone Volume", AK4641_VOL, 4, 7, 0, |
| 191 | mic_stereo_sidetone_tlv), |
| 192 | SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4641_SIG2, 4, 1, 0), |
| 193 | SOC_DAPM_SINGLE("Playback Switch", AK4641_SIG2, 7, 1, 0), |
| 194 | SOC_DAPM_SINGLE("Aux Bypass Switch", AK4641_SIG2, 5, 1, 0), |
| 195 | }; |
| 196 | |
| 197 | /* Input Mixer */ |
| 198 | static const struct snd_kcontrol_new ak4641_input_mixer_controls[] = { |
| 199 | SOC_DAPM_SINGLE("Mic Capture Switch", AK4641_MIC, 2, 1, 0), |
| 200 | SOC_DAPM_SINGLE("Aux Capture Switch", AK4641_MIC, 5, 1, 0), |
| 201 | }; |
| 202 | |
| 203 | /* Mic mux */ |
| 204 | static const struct snd_kcontrol_new ak4641_mic_mux_control = |
| 205 | SOC_DAPM_ENUM("Mic Select", ak4641_mic_select_enum); |
| 206 | |
| 207 | /* Input mux */ |
| 208 | static const struct snd_kcontrol_new ak4641_input_mux_control = |
| 209 | SOC_DAPM_ENUM("Input Select", ak4641_mic_or_dac_enum); |
| 210 | |
| 211 | /* mono 2 switch */ |
| 212 | static const struct snd_kcontrol_new ak4641_mono2_control = |
| 213 | SOC_DAPM_SINGLE("Switch", AK4641_SIG1, 0, 1, 0); |
| 214 | |
| 215 | /* ak4641 dapm widgets */ |
| 216 | static const struct snd_soc_dapm_widget ak4641_dapm_widgets[] = { |
| 217 | SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0, |
| 218 | &ak4641_stereo_mixer_controls[0], |
| 219 | ARRAY_SIZE(ak4641_stereo_mixer_controls)), |
| 220 | SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0, |
| 221 | &ak4641_mono1_mixer_controls[0], |
| 222 | ARRAY_SIZE(ak4641_mono1_mixer_controls)), |
| 223 | SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, |
| 224 | &ak4641_input_mixer_controls[0], |
| 225 | ARRAY_SIZE(ak4641_input_mixer_controls)), |
| 226 | SND_SOC_DAPM_MUX("Mic Mux", SND_SOC_NOPM, 0, 0, |
| 227 | &ak4641_mic_mux_control), |
| 228 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, |
| 229 | &ak4641_input_mux_control), |
| 230 | SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0, |
| 231 | &ak4641_mono2_control), |
| 232 | |
| 233 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 234 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 235 | SND_SOC_DAPM_OUTPUT("MOUT1"), |
| 236 | SND_SOC_DAPM_OUTPUT("MOUT2"), |
| 237 | SND_SOC_DAPM_OUTPUT("MICOUT"), |
| 238 | |
| 239 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", AK4641_PM1, 0, 0), |
| 240 | SND_SOC_DAPM_PGA("Mic", AK4641_PM1, 1, 0, NULL, 0), |
| 241 | SND_SOC_DAPM_PGA("AUX In", AK4641_PM1, 2, 0, NULL, 0), |
| 242 | SND_SOC_DAPM_PGA("Mono Out", AK4641_PM1, 3, 0, NULL, 0), |
| 243 | SND_SOC_DAPM_PGA("Line Out", AK4641_PM1, 4, 0, NULL, 0), |
| 244 | |
| 245 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", AK4641_PM2, 0, 0), |
| 246 | SND_SOC_DAPM_PGA("Mono Out 2", AK4641_PM2, 3, 0, NULL, 0), |
| 247 | |
| 248 | SND_SOC_DAPM_ADC("Voice ADC", "Voice Capture", AK4641_BTIF, 0, 0), |
Axel Lin | 24441e1 | 2011-10-19 23:24:54 +0800 | [diff] [blame] | 249 | SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AK4641_BTIF, 1, 0), |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 250 | |
| 251 | SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4641_MIC, 3, 0), |
| 252 | SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4641_MIC, 4, 0), |
| 253 | |
| 254 | SND_SOC_DAPM_INPUT("MICIN"), |
| 255 | SND_SOC_DAPM_INPUT("MICEXT"), |
| 256 | SND_SOC_DAPM_INPUT("AUX"), |
| 257 | SND_SOC_DAPM_INPUT("AIN"), |
| 258 | }; |
| 259 | |
| 260 | static const struct snd_soc_dapm_route ak4641_audio_map[] = { |
| 261 | /* Stereo Mixer */ |
| 262 | {"Stereo Mixer", "Playback Switch", "DAC"}, |
| 263 | {"Stereo Mixer", "Mic Sidetone Switch", "Input Mux"}, |
| 264 | {"Stereo Mixer", "Aux Bypass Switch", "AUX In"}, |
| 265 | |
| 266 | /* Mono 1 Mixer */ |
| 267 | {"Mono1 Mixer", "Mic Mono Sidetone Switch", "Input Mux"}, |
| 268 | {"Mono1 Mixer", "Mono Playback Switch", "DAC"}, |
| 269 | |
| 270 | /* Mic */ |
| 271 | {"Mic", NULL, "AIN"}, |
| 272 | {"Mic Mux", "Internal", "Mic Int Bias"}, |
| 273 | {"Mic Mux", "External", "Mic Ext Bias"}, |
| 274 | {"Mic Int Bias", NULL, "MICIN"}, |
| 275 | {"Mic Ext Bias", NULL, "MICEXT"}, |
| 276 | {"MICOUT", NULL, "Mic Mux"}, |
| 277 | |
| 278 | /* Input Mux */ |
| 279 | {"Input Mux", "Microphone", "Mic"}, |
| 280 | {"Input Mux", "Voice DAC", "Voice DAC"}, |
| 281 | |
| 282 | /* Line Out */ |
| 283 | {"LOUT", NULL, "Line Out"}, |
| 284 | {"ROUT", NULL, "Line Out"}, |
| 285 | {"Line Out", NULL, "Stereo Mixer"}, |
| 286 | |
| 287 | /* Mono 1 Out */ |
| 288 | {"MOUT1", NULL, "Mono Out"}, |
| 289 | {"Mono Out", NULL, "Mono1 Mixer"}, |
| 290 | |
| 291 | /* Mono 2 Out */ |
| 292 | {"MOUT2", NULL, "Mono 2 Enable"}, |
| 293 | {"Mono 2 Enable", "Switch", "Mono Out 2"}, |
| 294 | {"Mono Out 2", NULL, "Stereo Mixer"}, |
| 295 | |
| 296 | {"Voice ADC", NULL, "Mono 2 Enable"}, |
| 297 | |
| 298 | /* Aux In */ |
| 299 | {"AUX In", NULL, "AUX"}, |
| 300 | |
| 301 | /* ADC */ |
| 302 | {"ADC", NULL, "Input Mixer"}, |
| 303 | {"Input Mixer", "Mic Capture Switch", "Mic"}, |
| 304 | {"Input Mixer", "Aux Capture Switch", "AUX In"}, |
| 305 | }; |
| 306 | |
| 307 | static int ak4641_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 308 | int clk_id, unsigned int freq, int dir) |
| 309 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 310 | struct snd_soc_component *component = codec_dai->component; |
| 311 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 312 | |
| 313 | ak4641->sysclk = freq; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int ak4641_i2s_hw_params(struct snd_pcm_substream *substream, |
| 318 | struct snd_pcm_hw_params *params, |
| 319 | struct snd_soc_dai *dai) |
| 320 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 321 | struct snd_soc_component *component = dai->component; |
| 322 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 323 | int rate = params_rate(params), fs = 256; |
| 324 | u8 mode2; |
| 325 | |
| 326 | if (rate) |
| 327 | fs = ak4641->sysclk / rate; |
| 328 | else |
| 329 | return -EINVAL; |
| 330 | |
| 331 | /* set fs */ |
| 332 | switch (fs) { |
| 333 | case 1024: |
| 334 | mode2 = (0x2 << 5); |
| 335 | break; |
| 336 | case 512: |
| 337 | mode2 = (0x1 << 5); |
| 338 | break; |
| 339 | case 256: |
| 340 | mode2 = (0x0 << 5); |
| 341 | break; |
| 342 | default: |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 343 | dev_err(component->dev, "Error: unsupported fs=%d\n", fs); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | } |
| 346 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 347 | snd_soc_component_update_bits(component, AK4641_MODE2, (0x3 << 5), mode2); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 348 | |
| 349 | /* Update de-emphasis filter for the new rate */ |
| 350 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 351 | ak4641->playback_fs = rate; |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 352 | ak4641_set_deemph(component); |
Joe Perches | 1d198f2 | 2013-10-08 15:55:45 -0700 | [diff] [blame] | 353 | } |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 359 | unsigned int fmt) |
| 360 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 361 | struct snd_soc_component *component = codec_dai->component; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 362 | u8 btif; |
Axel Lin | 16aff76 | 2011-12-30 23:34:54 +0800 | [diff] [blame] | 363 | int ret; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 364 | |
| 365 | /* interface format */ |
| 366 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 367 | case SND_SOC_DAIFMT_I2S: |
| 368 | btif = (0x3 << 5); |
| 369 | break; |
| 370 | case SND_SOC_DAIFMT_LEFT_J: |
| 371 | btif = (0x2 << 5); |
| 372 | break; |
| 373 | case SND_SOC_DAIFMT_DSP_A: /* MSB after FRM */ |
| 374 | btif = (0x0 << 5); |
| 375 | break; |
| 376 | case SND_SOC_DAIFMT_DSP_B: /* MSB during FRM */ |
| 377 | btif = (0x1 << 5); |
| 378 | break; |
| 379 | default: |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 383 | ret = snd_soc_component_update_bits(component, AK4641_BTIF, (0x3 << 5), btif); |
Axel Lin | 16aff76 | 2011-12-30 23:34:54 +0800 | [diff] [blame] | 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
| 387 | return 0; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 391 | unsigned int fmt) |
| 392 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 393 | struct snd_soc_component *component = codec_dai->component; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 394 | u8 mode1 = 0; |
| 395 | |
| 396 | /* interface format */ |
| 397 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 398 | case SND_SOC_DAIFMT_I2S: |
| 399 | mode1 = 0x02; |
| 400 | break; |
| 401 | case SND_SOC_DAIFMT_LEFT_J: |
| 402 | mode1 = 0x01; |
| 403 | break; |
| 404 | default: |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 408 | return snd_soc_component_write(component, AK4641_MODE1, mode1); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int ak4641_mute(struct snd_soc_dai *dai, int mute) |
| 412 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 413 | struct snd_soc_component *component = dai->component; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 414 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 415 | return snd_soc_component_update_bits(component, AK4641_DAC, 0x20, mute ? 0x20 : 0); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 416 | } |
| 417 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 418 | static int ak4641_set_bias_level(struct snd_soc_component *component, |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 419 | enum snd_soc_bias_level level) |
| 420 | { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 421 | struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component); |
| 422 | struct ak4641_platform_data *pdata = component->dev->platform_data; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 423 | int ret; |
| 424 | |
| 425 | switch (level) { |
| 426 | case SND_SOC_BIAS_ON: |
| 427 | /* unmute */ |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 428 | snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 429 | break; |
| 430 | case SND_SOC_BIAS_PREPARE: |
| 431 | /* mute */ |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 432 | snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0x20); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 433 | break; |
| 434 | case SND_SOC_BIAS_STANDBY: |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 435 | if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 436 | if (pdata && gpio_is_valid(pdata->gpio_power)) |
| 437 | gpio_set_value(pdata->gpio_power, 1); |
| 438 | mdelay(1); |
| 439 | if (pdata && gpio_is_valid(pdata->gpio_npdn)) |
| 440 | gpio_set_value(pdata->gpio_npdn, 1); |
| 441 | mdelay(1); |
| 442 | |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 443 | ret = regcache_sync(ak4641->regmap); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 444 | if (ret) { |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 445 | dev_err(component->dev, |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 446 | "Failed to sync cache: %d\n", ret); |
| 447 | return ret; |
| 448 | } |
| 449 | } |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 450 | snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0x80); |
| 451 | snd_soc_component_update_bits(component, AK4641_PM2, 0x80, 0); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 452 | break; |
| 453 | case SND_SOC_BIAS_OFF: |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 454 | snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 455 | if (pdata && gpio_is_valid(pdata->gpio_npdn)) |
| 456 | gpio_set_value(pdata->gpio_npdn, 0); |
| 457 | if (pdata && gpio_is_valid(pdata->gpio_power)) |
| 458 | gpio_set_value(pdata->gpio_power, 0); |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 459 | regcache_mark_dirty(ak4641->regmap); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 460 | break; |
| 461 | } |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | #define AK4641_RATES (SNDRV_PCM_RATE_8000_48000) |
| 466 | #define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 467 | SNDRV_PCM_RATE_16000) |
| 468 | #define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE) |
| 469 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 470 | static const struct snd_soc_dai_ops ak4641_i2s_dai_ops = { |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 471 | .hw_params = ak4641_i2s_hw_params, |
| 472 | .set_fmt = ak4641_i2s_set_dai_fmt, |
| 473 | .digital_mute = ak4641_mute, |
| 474 | .set_sysclk = ak4641_set_dai_sysclk, |
| 475 | }; |
| 476 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 477 | static const struct snd_soc_dai_ops ak4641_pcm_dai_ops = { |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 478 | .hw_params = NULL, /* rates are controlled by BT chip */ |
| 479 | .set_fmt = ak4641_pcm_set_dai_fmt, |
| 480 | .digital_mute = ak4641_mute, |
| 481 | .set_sysclk = ak4641_set_dai_sysclk, |
| 482 | }; |
| 483 | |
Mark Brown | 37aa716 | 2011-06-01 10:10:50 +0100 | [diff] [blame] | 484 | static struct snd_soc_dai_driver ak4641_dai[] = { |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 485 | { |
| 486 | .name = "ak4641-hifi", |
| 487 | .id = 1, |
| 488 | .playback = { |
| 489 | .stream_name = "HiFi Playback", |
| 490 | .channels_min = 1, |
| 491 | .channels_max = 2, |
| 492 | .rates = AK4641_RATES, |
| 493 | .formats = AK4641_FORMATS, |
| 494 | }, |
| 495 | .capture = { |
| 496 | .stream_name = "HiFi Capture", |
| 497 | .channels_min = 1, |
| 498 | .channels_max = 2, |
| 499 | .rates = AK4641_RATES, |
| 500 | .formats = AK4641_FORMATS, |
| 501 | }, |
| 502 | .ops = &ak4641_i2s_dai_ops, |
| 503 | .symmetric_rates = 1, |
| 504 | }, |
| 505 | { |
| 506 | .name = "ak4641-voice", |
| 507 | .id = 1, |
| 508 | .playback = { |
| 509 | .stream_name = "Voice Playback", |
| 510 | .channels_min = 1, |
| 511 | .channels_max = 1, |
| 512 | .rates = AK4641_RATES_BT, |
| 513 | .formats = AK4641_FORMATS, |
| 514 | }, |
| 515 | .capture = { |
| 516 | .stream_name = "Voice Capture", |
| 517 | .channels_min = 1, |
| 518 | .channels_max = 1, |
| 519 | .rates = AK4641_RATES_BT, |
| 520 | .formats = AK4641_FORMATS, |
| 521 | }, |
| 522 | .ops = &ak4641_pcm_dai_ops, |
| 523 | .symmetric_rates = 1, |
| 524 | }, |
| 525 | }; |
| 526 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 527 | static const struct snd_soc_component_driver soc_component_dev_ak4641 = { |
| 528 | .controls = ak4641_snd_controls, |
| 529 | .num_controls = ARRAY_SIZE(ak4641_snd_controls), |
| 530 | .dapm_widgets = ak4641_dapm_widgets, |
| 531 | .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets), |
| 532 | .dapm_routes = ak4641_audio_map, |
| 533 | .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map), |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 534 | .set_bias_level = ak4641_set_bias_level, |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 535 | .suspend_bias_off = 1, |
| 536 | .idle_bias_on = 1, |
| 537 | .use_pmdown_time = 1, |
| 538 | .endianness = 1, |
| 539 | .non_legacy_dai_naming = 1, |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 540 | }; |
| 541 | |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 542 | static const struct regmap_config ak4641_regmap = { |
| 543 | .reg_bits = 8, |
| 544 | .val_bits = 8, |
| 545 | |
| 546 | .max_register = AK4641_BTIF, |
| 547 | .reg_defaults = ak4641_reg_defaults, |
| 548 | .num_reg_defaults = ARRAY_SIZE(ak4641_reg_defaults), |
| 549 | .cache_type = REGCACHE_RBTREE, |
| 550 | }; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 551 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 552 | static int ak4641_i2c_probe(struct i2c_client *i2c, |
| 553 | const struct i2c_device_id *id) |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 554 | { |
Mark Brown | 253322c | 2012-03-04 14:20:18 +0000 | [diff] [blame] | 555 | struct ak4641_platform_data *pdata = i2c->dev.platform_data; |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 556 | struct ak4641_priv *ak4641; |
| 557 | int ret; |
| 558 | |
Axel Lin | 4273fcf | 2011-12-20 14:39:20 +0800 | [diff] [blame] | 559 | ak4641 = devm_kzalloc(&i2c->dev, sizeof(struct ak4641_priv), |
| 560 | GFP_KERNEL); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 561 | if (!ak4641) |
| 562 | return -ENOMEM; |
| 563 | |
Mark Brown | 4f53477 | 2013-11-22 15:14:48 +0000 | [diff] [blame] | 564 | ak4641->regmap = devm_regmap_init_i2c(i2c, &ak4641_regmap); |
| 565 | if (IS_ERR(ak4641->regmap)) |
| 566 | return PTR_ERR(ak4641->regmap); |
| 567 | |
Mark Brown | 253322c | 2012-03-04 14:20:18 +0000 | [diff] [blame] | 568 | if (pdata) { |
| 569 | if (gpio_is_valid(pdata->gpio_power)) { |
| 570 | ret = gpio_request_one(pdata->gpio_power, |
| 571 | GPIOF_OUT_INIT_LOW, "ak4641 power"); |
| 572 | if (ret) |
| 573 | goto err_out; |
| 574 | } |
| 575 | if (gpio_is_valid(pdata->gpio_npdn)) { |
| 576 | ret = gpio_request_one(pdata->gpio_npdn, |
| 577 | GPIOF_OUT_INIT_LOW, "ak4641 npdn"); |
| 578 | if (ret) |
| 579 | goto err_gpio; |
| 580 | |
| 581 | udelay(1); /* > 150 ns */ |
| 582 | gpio_set_value(pdata->gpio_npdn, 1); |
| 583 | } |
| 584 | } |
| 585 | |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 586 | i2c_set_clientdata(i2c, ak4641); |
| 587 | |
Kuninori Morimoto | 2b5c3e4 | 2018-01-29 03:14:32 +0000 | [diff] [blame] | 588 | ret = devm_snd_soc_register_component(&i2c->dev, |
| 589 | &soc_component_dev_ak4641, |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 590 | ak4641_dai, ARRAY_SIZE(ak4641_dai)); |
Mark Brown | 253322c | 2012-03-04 14:20:18 +0000 | [diff] [blame] | 591 | if (ret != 0) |
| 592 | goto err_gpio2; |
| 593 | |
| 594 | return 0; |
| 595 | |
| 596 | err_gpio2: |
| 597 | if (pdata) { |
| 598 | if (gpio_is_valid(pdata->gpio_power)) |
| 599 | gpio_set_value(pdata->gpio_power, 0); |
| 600 | if (gpio_is_valid(pdata->gpio_npdn)) |
| 601 | gpio_free(pdata->gpio_npdn); |
| 602 | } |
| 603 | err_gpio: |
| 604 | if (pdata && gpio_is_valid(pdata->gpio_power)) |
| 605 | gpio_free(pdata->gpio_power); |
| 606 | err_out: |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 610 | static int ak4641_i2c_remove(struct i2c_client *i2c) |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 611 | { |
Mark Brown | 253322c | 2012-03-04 14:20:18 +0000 | [diff] [blame] | 612 | struct ak4641_platform_data *pdata = i2c->dev.platform_data; |
| 613 | |
Mark Brown | 253322c | 2012-03-04 14:20:18 +0000 | [diff] [blame] | 614 | if (pdata) { |
| 615 | if (gpio_is_valid(pdata->gpio_power)) { |
| 616 | gpio_set_value(pdata->gpio_power, 0); |
| 617 | gpio_free(pdata->gpio_power); |
| 618 | } |
| 619 | if (gpio_is_valid(pdata->gpio_npdn)) |
| 620 | gpio_free(pdata->gpio_npdn); |
| 621 | } |
| 622 | |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static const struct i2c_device_id ak4641_i2c_id[] = { |
| 627 | { "ak4641", 0 }, |
| 628 | { } |
| 629 | }; |
| 630 | MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id); |
| 631 | |
| 632 | static struct i2c_driver ak4641_i2c_driver = { |
| 633 | .driver = { |
| 634 | .name = "ak4641", |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 635 | }, |
| 636 | .probe = ak4641_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 637 | .remove = ak4641_i2c_remove, |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 638 | .id_table = ak4641_i2c_id, |
| 639 | }; |
| 640 | |
Mark Brown | a0abacd | 2012-03-04 14:06:36 +0000 | [diff] [blame] | 641 | module_i2c_driver(ak4641_i2c_driver); |
Dmitry Artamonow | 00d2701 | 2011-05-18 19:25:09 +0400 | [diff] [blame] | 642 | |
| 643 | MODULE_DESCRIPTION("SoC AK4641 driver"); |
| 644 | MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>"); |
| 645 | MODULE_LICENSE("GPL"); |