Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ak4535.c -- AK4535 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright 2005 Openedhand Ltd. |
| 5 | * |
| 6 | * Author: Richard Purdie <richard@openedhand.com> |
| 7 | * |
| 8 | * Based on wm8753.c by Liam Girdwood |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/pm.h> |
| 20 | #include <linux/i2c.h> |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 23 | #include <sound/core.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
| 26 | #include <sound/soc.h> |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 27 | #include <sound/initval.h> |
| 28 | |
| 29 | #include "ak4535.h" |
| 30 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 31 | /* codec private data */ |
| 32 | struct ak4535_priv { |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 33 | struct regmap *regmap; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 34 | unsigned int sysclk; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * ak4535 register cache |
| 39 | */ |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 40 | static const struct reg_default ak4535_reg_defaults[] = { |
| 41 | { 0, 0x00 }, |
| 42 | { 1, 0x80 }, |
| 43 | { 2, 0x00 }, |
| 44 | { 3, 0x03 }, |
| 45 | { 4, 0x02 }, |
| 46 | { 5, 0x00 }, |
| 47 | { 6, 0x11 }, |
| 48 | { 7, 0x01 }, |
| 49 | { 8, 0x00 }, |
| 50 | { 9, 0x40 }, |
| 51 | { 10, 0x36 }, |
| 52 | { 11, 0x10 }, |
| 53 | { 12, 0x00 }, |
| 54 | { 13, 0x00 }, |
| 55 | { 14, 0x57 }, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 58 | static bool ak4535_volatile(struct device *dev, unsigned int reg) |
| 59 | { |
| 60 | switch (reg) { |
| 61 | case AK4535_STATUS: |
| 62 | return true; |
| 63 | default: |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 68 | static const char *ak4535_mono_gain[] = {"+6dB", "-17dB"}; |
| 69 | static const char *ak4535_mono_out[] = {"(L + R)/2", "Hi-Z"}; |
| 70 | static const char *ak4535_hp_out[] = {"Stereo", "Mono"}; |
| 71 | static const char *ak4535_deemp[] = {"44.1kHz", "Off", "48kHz", "32kHz"}; |
| 72 | static const char *ak4535_mic_select[] = {"Internal", "External"}; |
| 73 | |
| 74 | static const struct soc_enum ak4535_enum[] = { |
| 75 | SOC_ENUM_SINGLE(AK4535_SIG1, 7, 2, ak4535_mono_gain), |
| 76 | SOC_ENUM_SINGLE(AK4535_SIG1, 6, 2, ak4535_mono_out), |
| 77 | SOC_ENUM_SINGLE(AK4535_MODE2, 2, 2, ak4535_hp_out), |
| 78 | SOC_ENUM_SINGLE(AK4535_DAC, 0, 4, ak4535_deemp), |
| 79 | SOC_ENUM_SINGLE(AK4535_MIC, 1, 2, ak4535_mic_select), |
| 80 | }; |
| 81 | |
| 82 | static const struct snd_kcontrol_new ak4535_snd_controls[] = { |
| 83 | SOC_SINGLE("ALC2 Switch", AK4535_SIG1, 1, 1, 0), |
| 84 | SOC_ENUM("Mono 1 Output", ak4535_enum[1]), |
| 85 | SOC_ENUM("Mono 1 Gain", ak4535_enum[0]), |
| 86 | SOC_ENUM("Headphone Output", ak4535_enum[2]), |
| 87 | SOC_ENUM("Playback Deemphasis", ak4535_enum[3]), |
| 88 | SOC_SINGLE("Bass Volume", AK4535_DAC, 2, 3, 0), |
| 89 | SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC, 0, 1, 0), |
| 90 | SOC_ENUM("Mic Select", ak4535_enum[4]), |
| 91 | SOC_SINGLE("ALC Operation Time", AK4535_TIMER, 0, 3, 0), |
| 92 | SOC_SINGLE("ALC Recovery Time", AK4535_TIMER, 2, 3, 0), |
| 93 | SOC_SINGLE("ALC ZC Time", AK4535_TIMER, 4, 3, 0), |
| 94 | SOC_SINGLE("ALC 1 Switch", AK4535_ALC1, 5, 1, 0), |
| 95 | SOC_SINGLE("ALC 2 Switch", AK4535_ALC1, 6, 1, 0), |
| 96 | SOC_SINGLE("ALC Volume", AK4535_ALC2, 0, 127, 0), |
| 97 | SOC_SINGLE("Capture Volume", AK4535_PGA, 0, 127, 0), |
| 98 | SOC_SINGLE("Left Playback Volume", AK4535_LATT, 0, 127, 1), |
| 99 | SOC_SINGLE("Right Playback Volume", AK4535_RATT, 0, 127, 1), |
| 100 | SOC_SINGLE("AUX Bypass Volume", AK4535_VOL, 0, 15, 0), |
| 101 | SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL, 4, 7, 0), |
| 102 | }; |
| 103 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 104 | /* Mono 1 Mixer */ |
| 105 | static const struct snd_kcontrol_new ak4535_mono1_mixer_controls[] = { |
| 106 | SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1, 4, 1, 0), |
| 107 | SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1, 5, 1, 0), |
| 108 | }; |
| 109 | |
| 110 | /* Stereo Mixer */ |
| 111 | static const struct snd_kcontrol_new ak4535_stereo_mixer_controls[] = { |
| 112 | SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2, 4, 1, 0), |
| 113 | SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2, 7, 1, 0), |
| 114 | SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2, 5, 1, 0), |
| 115 | }; |
| 116 | |
| 117 | /* Input Mixer */ |
| 118 | static const struct snd_kcontrol_new ak4535_input_mixer_controls[] = { |
| 119 | SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC, 2, 1, 0), |
| 120 | SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC, 5, 1, 0), |
| 121 | }; |
| 122 | |
| 123 | /* Input mux */ |
| 124 | static const struct snd_kcontrol_new ak4535_input_mux_control = |
| 125 | SOC_DAPM_ENUM("Input Select", ak4535_enum[4]); |
| 126 | |
| 127 | /* HP L switch */ |
| 128 | static const struct snd_kcontrol_new ak4535_hpl_control = |
| 129 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1); |
| 130 | |
| 131 | /* HP R switch */ |
| 132 | static const struct snd_kcontrol_new ak4535_hpr_control = |
| 133 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1); |
| 134 | |
| 135 | /* mono 2 switch */ |
| 136 | static const struct snd_kcontrol_new ak4535_mono2_control = |
| 137 | SOC_DAPM_SINGLE("Switch", AK4535_SIG1, 0, 1, 0); |
| 138 | |
| 139 | /* Line out switch */ |
| 140 | static const struct snd_kcontrol_new ak4535_line_control = |
| 141 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 6, 1, 0); |
| 142 | |
| 143 | /* ak4535 dapm widgets */ |
| 144 | static const struct snd_soc_dapm_widget ak4535_dapm_widgets[] = { |
| 145 | SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0, |
| 146 | &ak4535_stereo_mixer_controls[0], |
| 147 | ARRAY_SIZE(ak4535_stereo_mixer_controls)), |
| 148 | SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0, |
| 149 | &ak4535_mono1_mixer_controls[0], |
| 150 | ARRAY_SIZE(ak4535_mono1_mixer_controls)), |
| 151 | SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, |
| 152 | &ak4535_input_mixer_controls[0], |
| 153 | ARRAY_SIZE(ak4535_input_mixer_controls)), |
| 154 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, |
| 155 | &ak4535_input_mux_control), |
| 156 | SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2, 0, 0), |
| 157 | SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0, |
| 158 | &ak4535_mono2_control), |
| 159 | /* speaker powersave bit */ |
| 160 | SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2, 0, 0, NULL, 0), |
| 161 | SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM, 0, 0, |
| 162 | &ak4535_line_control), |
| 163 | SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM, 0, 0, |
| 164 | &ak4535_hpl_control), |
| 165 | SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM, 0, 0, |
| 166 | &ak4535_hpr_control), |
| 167 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 168 | SND_SOC_DAPM_OUTPUT("HPL"), |
| 169 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 170 | SND_SOC_DAPM_OUTPUT("HPR"), |
| 171 | SND_SOC_DAPM_OUTPUT("SPP"), |
| 172 | SND_SOC_DAPM_OUTPUT("SPN"), |
| 173 | SND_SOC_DAPM_OUTPUT("MOUT1"), |
| 174 | SND_SOC_DAPM_OUTPUT("MOUT2"), |
| 175 | SND_SOC_DAPM_OUTPUT("MICOUT"), |
| 176 | SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1, 0, 0), |
| 177 | SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2, 3, 0, NULL, 0), |
| 178 | SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2, 1, 0, NULL, 0), |
| 179 | SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2, 2, 0, NULL, 0), |
| 180 | SND_SOC_DAPM_PGA("Mic", AK4535_PM1, 1, 0, NULL, 0), |
| 181 | SND_SOC_DAPM_PGA("Line Out", AK4535_PM1, 4, 0, NULL, 0), |
| 182 | SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1, 3, 0, NULL, 0), |
| 183 | SND_SOC_DAPM_PGA("AUX In", AK4535_PM1, 2, 0, NULL, 0), |
| 184 | |
| 185 | SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC, 3, 0), |
| 186 | SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC, 4, 0), |
| 187 | SND_SOC_DAPM_INPUT("MICIN"), |
| 188 | SND_SOC_DAPM_INPUT("MICEXT"), |
| 189 | SND_SOC_DAPM_INPUT("AUX"), |
| 190 | SND_SOC_DAPM_INPUT("MIN"), |
| 191 | SND_SOC_DAPM_INPUT("AIN"), |
| 192 | }; |
| 193 | |
Lu Guanqun | b63c963 | 2011-03-30 21:53:07 +0800 | [diff] [blame] | 194 | static const struct snd_soc_dapm_route ak4535_audio_map[] = { |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 195 | /*stereo mixer */ |
| 196 | {"Stereo Mixer", "Playback Switch", "DAC"}, |
| 197 | {"Stereo Mixer", "Mic Sidetone Switch", "Mic"}, |
| 198 | {"Stereo Mixer", "Aux Bypass Switch", "AUX In"}, |
| 199 | |
| 200 | /* mono1 mixer */ |
| 201 | {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"}, |
| 202 | {"Mono1 Mixer", "Mono Playback Switch", "DAC"}, |
| 203 | |
| 204 | /* Mic */ |
| 205 | {"Mic", NULL, "AIN"}, |
| 206 | {"Input Mux", "Internal", "Mic Int Bias"}, |
| 207 | {"Input Mux", "External", "Mic Ext Bias"}, |
| 208 | {"Mic Int Bias", NULL, "MICIN"}, |
| 209 | {"Mic Ext Bias", NULL, "MICEXT"}, |
| 210 | {"MICOUT", NULL, "Input Mux"}, |
| 211 | |
| 212 | /* line out */ |
| 213 | {"LOUT", NULL, "Line Out Enable"}, |
| 214 | {"ROUT", NULL, "Line Out Enable"}, |
| 215 | {"Line Out Enable", "Switch", "Line Out"}, |
| 216 | {"Line Out", NULL, "Stereo Mixer"}, |
| 217 | |
| 218 | /* mono1 out */ |
| 219 | {"MOUT1", NULL, "Mono Out"}, |
| 220 | {"Mono Out", NULL, "Mono1 Mixer"}, |
| 221 | |
| 222 | /* left HP */ |
| 223 | {"HPL", NULL, "Left HP Enable"}, |
| 224 | {"Left HP Enable", "Switch", "HP L Amp"}, |
| 225 | {"HP L Amp", NULL, "Stereo Mixer"}, |
| 226 | |
| 227 | /* right HP */ |
| 228 | {"HPR", NULL, "Right HP Enable"}, |
| 229 | {"Right HP Enable", "Switch", "HP R Amp"}, |
| 230 | {"HP R Amp", NULL, "Stereo Mixer"}, |
| 231 | |
| 232 | /* speaker */ |
| 233 | {"SPP", NULL, "Speaker Enable"}, |
| 234 | {"SPN", NULL, "Speaker Enable"}, |
| 235 | {"Speaker Enable", "Switch", "Spk Amp"}, |
| 236 | {"Spk Amp", NULL, "MIN"}, |
| 237 | |
| 238 | /* mono 2 */ |
| 239 | {"MOUT2", NULL, "Mono 2 Enable"}, |
| 240 | {"Mono 2 Enable", "Switch", "Stereo Mixer"}, |
| 241 | |
| 242 | /* Aux In */ |
| 243 | {"Aux In", NULL, "AUX"}, |
| 244 | |
| 245 | /* ADC */ |
| 246 | {"ADC", NULL, "Input Mixer"}, |
| 247 | {"Input Mixer", "Mic Capture Switch", "Mic"}, |
| 248 | {"Input Mixer", "Aux Capture Switch", "Aux In"}, |
| 249 | }; |
| 250 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 251 | static int ak4535_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 252 | int clk_id, unsigned int freq, int dir) |
| 253 | { |
| 254 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 255 | struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 256 | |
| 257 | ak4535->sysclk = freq; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int ak4535_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 262 | struct snd_pcm_hw_params *params, |
| 263 | struct snd_soc_dai *dai) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 264 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 265 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 266 | struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 267 | u8 mode2 = snd_soc_read(codec, AK4535_MODE2) & ~(0x3 << 5); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 268 | int rate = params_rate(params), fs = 256; |
| 269 | |
| 270 | if (rate) |
| 271 | fs = ak4535->sysclk / rate; |
| 272 | |
| 273 | /* set fs */ |
| 274 | switch (fs) { |
| 275 | case 1024: |
| 276 | mode2 |= (0x2 << 5); |
| 277 | break; |
| 278 | case 512: |
| 279 | mode2 |= (0x1 << 5); |
| 280 | break; |
| 281 | case 256: |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | /* set rate */ |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 286 | snd_soc_write(codec, AK4535_MODE2, mode2); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 290 | static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 291 | unsigned int fmt) |
| 292 | { |
| 293 | struct snd_soc_codec *codec = codec_dai->codec; |
| 294 | u8 mode1 = 0; |
| 295 | |
| 296 | /* interface format */ |
| 297 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 298 | case SND_SOC_DAIFMT_I2S: |
| 299 | mode1 = 0x0002; |
| 300 | break; |
| 301 | case SND_SOC_DAIFMT_LEFT_J: |
| 302 | mode1 = 0x0001; |
| 303 | break; |
| 304 | default: |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | |
| 308 | /* use 32 fs for BCLK to save power */ |
| 309 | mode1 |= 0x4; |
| 310 | |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 311 | snd_soc_write(codec, AK4535_MODE1, mode1); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 315 | static int ak4535_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 316 | { |
| 317 | struct snd_soc_codec *codec = dai->codec; |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 318 | u16 mute_reg = snd_soc_read(codec, AK4535_DAC); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 319 | if (!mute) |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 320 | snd_soc_write(codec, AK4535_DAC, mute_reg & ~0x20); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 321 | else |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 322 | snd_soc_write(codec, AK4535_DAC, mute_reg | 0x20); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int ak4535_set_bias_level(struct snd_soc_codec *codec, |
| 327 | enum snd_soc_bias_level level) |
| 328 | { |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 329 | switch (level) { |
| 330 | case SND_SOC_BIAS_ON: |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 331 | snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 332 | break; |
| 333 | case SND_SOC_BIAS_PREPARE: |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 334 | snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0x20); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 335 | break; |
| 336 | case SND_SOC_BIAS_STANDBY: |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 337 | snd_soc_update_bits(codec, AK4535_PM1, 0x80, 0x80); |
| 338 | snd_soc_update_bits(codec, AK4535_PM2, 0x80, 0); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 339 | break; |
| 340 | case SND_SOC_BIAS_OFF: |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 341 | snd_soc_update_bits(codec, AK4535_PM1, 0x80, 0); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 342 | break; |
| 343 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 344 | codec->dapm.bias_level = level; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 349 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 350 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) |
| 351 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 352 | static const struct snd_soc_dai_ops ak4535_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 353 | .hw_params = ak4535_hw_params, |
| 354 | .set_fmt = ak4535_set_dai_fmt, |
| 355 | .digital_mute = ak4535_mute, |
| 356 | .set_sysclk = ak4535_set_dai_sysclk, |
| 357 | }; |
| 358 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 359 | static struct snd_soc_dai_driver ak4535_dai = { |
| 360 | .name = "ak4535-hifi", |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 361 | .playback = { |
| 362 | .stream_name = "Playback", |
| 363 | .channels_min = 1, |
| 364 | .channels_max = 2, |
| 365 | .rates = AK4535_RATES, |
| 366 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 367 | .capture = { |
| 368 | .stream_name = "Capture", |
| 369 | .channels_min = 1, |
| 370 | .channels_max = 2, |
| 371 | .rates = AK4535_RATES, |
| 372 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 373 | .ops = &ak4535_dai_ops, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 374 | }; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 375 | |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 376 | static int ak4535_suspend(struct snd_soc_codec *codec) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 377 | { |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 378 | ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 379 | return 0; |
| 380 | } |
| 381 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 382 | static int ak4535_resume(struct snd_soc_codec *codec) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 383 | { |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 384 | snd_soc_cache_sync(codec); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 385 | ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 389 | static int ak4535_probe(struct snd_soc_codec *codec) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 390 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 391 | struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 392 | int ret; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 393 | |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 394 | codec->control_data = ak4535->regmap; |
| 395 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP); |
Axel Lin | 3b18450 | 2011-10-06 10:11:51 +0800 | [diff] [blame] | 396 | if (ret < 0) { |
| 397 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 398 | return ret; |
| 399 | } |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 400 | /* power on device */ |
| 401 | ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 402 | |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 403 | snd_soc_add_codec_controls(codec, ak4535_snd_controls, |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 404 | ARRAY_SIZE(ak4535_snd_controls)); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 405 | return 0; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 408 | /* power down chip */ |
| 409 | static int ak4535_remove(struct snd_soc_codec *codec) |
| 410 | { |
| 411 | ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 412 | return 0; |
| 413 | } |
| 414 | |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 415 | static const struct regmap_config ak4535_regmap = { |
| 416 | .reg_bits = 8, |
| 417 | .val_bits = 8, |
| 418 | |
| 419 | .max_register = AK4535_STATUS, |
| 420 | .volatile_reg = ak4535_volatile, |
| 421 | |
| 422 | .cache_type = REGCACHE_RBTREE, |
| 423 | .reg_defaults = ak4535_reg_defaults, |
| 424 | .num_reg_defaults = ARRAY_SIZE(ak4535_reg_defaults), |
| 425 | }; |
| 426 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 427 | static struct snd_soc_codec_driver soc_codec_dev_ak4535 = { |
| 428 | .probe = ak4535_probe, |
| 429 | .remove = ak4535_remove, |
| 430 | .suspend = ak4535_suspend, |
| 431 | .resume = ak4535_resume, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 432 | .set_bias_level = ak4535_set_bias_level, |
Lu Guanqun | b63c963 | 2011-03-30 21:53:07 +0800 | [diff] [blame] | 433 | .dapm_widgets = ak4535_dapm_widgets, |
| 434 | .num_dapm_widgets = ARRAY_SIZE(ak4535_dapm_widgets), |
| 435 | .dapm_routes = ak4535_audio_map, |
| 436 | .num_dapm_routes = ARRAY_SIZE(ak4535_audio_map), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 437 | }; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 438 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 439 | static __devinit int ak4535_i2c_probe(struct i2c_client *i2c, |
| 440 | const struct i2c_device_id *id) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 441 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 442 | struct ak4535_priv *ak4535; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 443 | int ret; |
| 444 | |
Axel Lin | 7246492 | 2011-12-20 14:38:09 +0800 | [diff] [blame] | 445 | ak4535 = devm_kzalloc(&i2c->dev, sizeof(struct ak4535_priv), |
| 446 | GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 447 | if (ak4535 == NULL) |
| 448 | return -ENOMEM; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 449 | |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 450 | ak4535->regmap = regmap_init_i2c(i2c, &ak4535_regmap); |
| 451 | if (IS_ERR(ak4535->regmap)) { |
| 452 | ret = PTR_ERR(ak4535->regmap); |
| 453 | dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret); |
| 454 | return ret; |
| 455 | } |
| 456 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 457 | i2c_set_clientdata(i2c, ak4535); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 458 | |
| 459 | ret = snd_soc_register_codec(&i2c->dev, |
| 460 | &soc_codec_dev_ak4535, &ak4535_dai, 1); |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 461 | if (ret != 0) |
| 462 | regmap_exit(ak4535->regmap); |
| 463 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 464 | return ret; |
| 465 | } |
| 466 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 467 | static __devexit int ak4535_i2c_remove(struct i2c_client *client) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 468 | { |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 469 | struct ak4535_priv *ak4535 = i2c_get_clientdata(client); |
| 470 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 471 | snd_soc_unregister_codec(&client->dev); |
Mark Brown | a1fa921 | 2012-02-16 23:03:44 -0800 | [diff] [blame] | 472 | regmap_exit(ak4535->regmap); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 476 | static const struct i2c_device_id ak4535_i2c_id[] = { |
| 477 | { "ak4535", 0 }, |
| 478 | { } |
| 479 | }; |
| 480 | MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 481 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 482 | static struct i2c_driver ak4535_i2c_driver = { |
| 483 | .driver = { |
Mark Brown | d69280b | 2012-02-16 22:52:19 -0800 | [diff] [blame] | 484 | .name = "ak4535", |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 485 | .owner = THIS_MODULE, |
| 486 | }, |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 487 | .probe = ak4535_i2c_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 488 | .remove = __devexit_p(ak4535_i2c_remove), |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 489 | .id_table = ak4535_i2c_id, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 490 | }; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 491 | |
Mark Brown | b08c576 | 2012-02-16 22:52:44 -0800 | [diff] [blame] | 492 | module_i2c_driver(ak4535_i2c_driver); |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 493 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 494 | MODULE_DESCRIPTION("Soc AK4535 driver"); |
| 495 | MODULE_AUTHOR("Richard Purdie"); |
| 496 | MODULE_LICENSE("GPL"); |