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> |
| 21 | #include <linux/platform_device.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> |
| 27 | #include <sound/soc-dapm.h> |
| 28 | #include <sound/initval.h> |
| 29 | |
| 30 | #include "ak4535.h" |
| 31 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 32 | #define AK4535_VERSION "0.3" |
| 33 | |
| 34 | struct snd_soc_codec_device soc_codec_dev_ak4535; |
| 35 | |
| 36 | /* codec private data */ |
| 37 | struct ak4535_priv { |
| 38 | unsigned int sysclk; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * ak4535 register cache |
| 43 | */ |
| 44 | static const u16 ak4535_reg[AK4535_CACHEREGNUM] = { |
| 45 | 0x0000, 0x0080, 0x0000, 0x0003, |
| 46 | 0x0002, 0x0000, 0x0011, 0x0001, |
| 47 | 0x0000, 0x0040, 0x0036, 0x0010, |
| 48 | 0x0000, 0x0000, 0x0057, 0x0000, |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * read ak4535 register cache |
| 53 | */ |
| 54 | static inline unsigned int ak4535_read_reg_cache(struct snd_soc_codec *codec, |
| 55 | unsigned int reg) |
| 56 | { |
| 57 | u16 *cache = codec->reg_cache; |
| 58 | if (reg >= AK4535_CACHEREGNUM) |
| 59 | return -1; |
| 60 | return cache[reg]; |
| 61 | } |
| 62 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 63 | /* |
| 64 | * write ak4535 register cache |
| 65 | */ |
| 66 | static inline void ak4535_write_reg_cache(struct snd_soc_codec *codec, |
| 67 | u16 reg, unsigned int value) |
| 68 | { |
| 69 | u16 *cache = codec->reg_cache; |
| 70 | if (reg >= AK4535_CACHEREGNUM) |
| 71 | return; |
| 72 | cache[reg] = value; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * write to the AK4535 register space |
| 77 | */ |
| 78 | static int ak4535_write(struct snd_soc_codec *codec, unsigned int reg, |
| 79 | unsigned int value) |
| 80 | { |
| 81 | u8 data[2]; |
| 82 | |
| 83 | /* data is |
| 84 | * D15..D8 AK4535 register offset |
| 85 | * D7...D0 register data |
| 86 | */ |
| 87 | data[0] = reg & 0xff; |
| 88 | data[1] = value & 0xff; |
| 89 | |
| 90 | ak4535_write_reg_cache(codec, reg, value); |
| 91 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 92 | return 0; |
| 93 | else |
| 94 | return -EIO; |
| 95 | } |
| 96 | |
| 97 | static int ak4535_sync(struct snd_soc_codec *codec) |
| 98 | { |
| 99 | u16 *cache = codec->reg_cache; |
| 100 | int i, r = 0; |
| 101 | |
| 102 | for (i = 0; i < AK4535_CACHEREGNUM; i++) |
| 103 | r |= ak4535_write(codec, i, cache[i]); |
| 104 | |
| 105 | return r; |
| 106 | }; |
| 107 | |
| 108 | static const char *ak4535_mono_gain[] = {"+6dB", "-17dB"}; |
| 109 | static const char *ak4535_mono_out[] = {"(L + R)/2", "Hi-Z"}; |
| 110 | static const char *ak4535_hp_out[] = {"Stereo", "Mono"}; |
| 111 | static const char *ak4535_deemp[] = {"44.1kHz", "Off", "48kHz", "32kHz"}; |
| 112 | static const char *ak4535_mic_select[] = {"Internal", "External"}; |
| 113 | |
| 114 | static const struct soc_enum ak4535_enum[] = { |
| 115 | SOC_ENUM_SINGLE(AK4535_SIG1, 7, 2, ak4535_mono_gain), |
| 116 | SOC_ENUM_SINGLE(AK4535_SIG1, 6, 2, ak4535_mono_out), |
| 117 | SOC_ENUM_SINGLE(AK4535_MODE2, 2, 2, ak4535_hp_out), |
| 118 | SOC_ENUM_SINGLE(AK4535_DAC, 0, 4, ak4535_deemp), |
| 119 | SOC_ENUM_SINGLE(AK4535_MIC, 1, 2, ak4535_mic_select), |
| 120 | }; |
| 121 | |
| 122 | static const struct snd_kcontrol_new ak4535_snd_controls[] = { |
| 123 | SOC_SINGLE("ALC2 Switch", AK4535_SIG1, 1, 1, 0), |
| 124 | SOC_ENUM("Mono 1 Output", ak4535_enum[1]), |
| 125 | SOC_ENUM("Mono 1 Gain", ak4535_enum[0]), |
| 126 | SOC_ENUM("Headphone Output", ak4535_enum[2]), |
| 127 | SOC_ENUM("Playback Deemphasis", ak4535_enum[3]), |
| 128 | SOC_SINGLE("Bass Volume", AK4535_DAC, 2, 3, 0), |
| 129 | SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC, 0, 1, 0), |
| 130 | SOC_ENUM("Mic Select", ak4535_enum[4]), |
| 131 | SOC_SINGLE("ALC Operation Time", AK4535_TIMER, 0, 3, 0), |
| 132 | SOC_SINGLE("ALC Recovery Time", AK4535_TIMER, 2, 3, 0), |
| 133 | SOC_SINGLE("ALC ZC Time", AK4535_TIMER, 4, 3, 0), |
| 134 | SOC_SINGLE("ALC 1 Switch", AK4535_ALC1, 5, 1, 0), |
| 135 | SOC_SINGLE("ALC 2 Switch", AK4535_ALC1, 6, 1, 0), |
| 136 | SOC_SINGLE("ALC Volume", AK4535_ALC2, 0, 127, 0), |
| 137 | SOC_SINGLE("Capture Volume", AK4535_PGA, 0, 127, 0), |
| 138 | SOC_SINGLE("Left Playback Volume", AK4535_LATT, 0, 127, 1), |
| 139 | SOC_SINGLE("Right Playback Volume", AK4535_RATT, 0, 127, 1), |
| 140 | SOC_SINGLE("AUX Bypass Volume", AK4535_VOL, 0, 15, 0), |
| 141 | SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL, 4, 7, 0), |
| 142 | }; |
| 143 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 144 | /* Mono 1 Mixer */ |
| 145 | static const struct snd_kcontrol_new ak4535_mono1_mixer_controls[] = { |
| 146 | SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1, 4, 1, 0), |
| 147 | SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1, 5, 1, 0), |
| 148 | }; |
| 149 | |
| 150 | /* Stereo Mixer */ |
| 151 | static const struct snd_kcontrol_new ak4535_stereo_mixer_controls[] = { |
| 152 | SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2, 4, 1, 0), |
| 153 | SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2, 7, 1, 0), |
| 154 | SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2, 5, 1, 0), |
| 155 | }; |
| 156 | |
| 157 | /* Input Mixer */ |
| 158 | static const struct snd_kcontrol_new ak4535_input_mixer_controls[] = { |
| 159 | SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC, 2, 1, 0), |
| 160 | SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC, 5, 1, 0), |
| 161 | }; |
| 162 | |
| 163 | /* Input mux */ |
| 164 | static const struct snd_kcontrol_new ak4535_input_mux_control = |
| 165 | SOC_DAPM_ENUM("Input Select", ak4535_enum[4]); |
| 166 | |
| 167 | /* HP L switch */ |
| 168 | static const struct snd_kcontrol_new ak4535_hpl_control = |
| 169 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1); |
| 170 | |
| 171 | /* HP R switch */ |
| 172 | static const struct snd_kcontrol_new ak4535_hpr_control = |
| 173 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1); |
| 174 | |
| 175 | /* mono 2 switch */ |
| 176 | static const struct snd_kcontrol_new ak4535_mono2_control = |
| 177 | SOC_DAPM_SINGLE("Switch", AK4535_SIG1, 0, 1, 0); |
| 178 | |
| 179 | /* Line out switch */ |
| 180 | static const struct snd_kcontrol_new ak4535_line_control = |
| 181 | SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 6, 1, 0); |
| 182 | |
| 183 | /* ak4535 dapm widgets */ |
| 184 | static const struct snd_soc_dapm_widget ak4535_dapm_widgets[] = { |
| 185 | SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0, |
| 186 | &ak4535_stereo_mixer_controls[0], |
| 187 | ARRAY_SIZE(ak4535_stereo_mixer_controls)), |
| 188 | SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0, |
| 189 | &ak4535_mono1_mixer_controls[0], |
| 190 | ARRAY_SIZE(ak4535_mono1_mixer_controls)), |
| 191 | SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, |
| 192 | &ak4535_input_mixer_controls[0], |
| 193 | ARRAY_SIZE(ak4535_input_mixer_controls)), |
| 194 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, |
| 195 | &ak4535_input_mux_control), |
| 196 | SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2, 0, 0), |
| 197 | SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0, |
| 198 | &ak4535_mono2_control), |
| 199 | /* speaker powersave bit */ |
| 200 | SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2, 0, 0, NULL, 0), |
| 201 | SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM, 0, 0, |
| 202 | &ak4535_line_control), |
| 203 | SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM, 0, 0, |
| 204 | &ak4535_hpl_control), |
| 205 | SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM, 0, 0, |
| 206 | &ak4535_hpr_control), |
| 207 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 208 | SND_SOC_DAPM_OUTPUT("HPL"), |
| 209 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 210 | SND_SOC_DAPM_OUTPUT("HPR"), |
| 211 | SND_SOC_DAPM_OUTPUT("SPP"), |
| 212 | SND_SOC_DAPM_OUTPUT("SPN"), |
| 213 | SND_SOC_DAPM_OUTPUT("MOUT1"), |
| 214 | SND_SOC_DAPM_OUTPUT("MOUT2"), |
| 215 | SND_SOC_DAPM_OUTPUT("MICOUT"), |
| 216 | SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1, 0, 0), |
| 217 | SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2, 3, 0, NULL, 0), |
| 218 | SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2, 1, 0, NULL, 0), |
| 219 | SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2, 2, 0, NULL, 0), |
| 220 | SND_SOC_DAPM_PGA("Mic", AK4535_PM1, 1, 0, NULL, 0), |
| 221 | SND_SOC_DAPM_PGA("Line Out", AK4535_PM1, 4, 0, NULL, 0), |
| 222 | SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1, 3, 0, NULL, 0), |
| 223 | SND_SOC_DAPM_PGA("AUX In", AK4535_PM1, 2, 0, NULL, 0), |
| 224 | |
| 225 | SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC, 3, 0), |
| 226 | SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC, 4, 0), |
| 227 | SND_SOC_DAPM_INPUT("MICIN"), |
| 228 | SND_SOC_DAPM_INPUT("MICEXT"), |
| 229 | SND_SOC_DAPM_INPUT("AUX"), |
| 230 | SND_SOC_DAPM_INPUT("MIN"), |
| 231 | SND_SOC_DAPM_INPUT("AIN"), |
| 232 | }; |
| 233 | |
| 234 | static const struct snd_soc_dapm_route audio_map[] = { |
| 235 | /*stereo mixer */ |
| 236 | {"Stereo Mixer", "Playback Switch", "DAC"}, |
| 237 | {"Stereo Mixer", "Mic Sidetone Switch", "Mic"}, |
| 238 | {"Stereo Mixer", "Aux Bypass Switch", "AUX In"}, |
| 239 | |
| 240 | /* mono1 mixer */ |
| 241 | {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"}, |
| 242 | {"Mono1 Mixer", "Mono Playback Switch", "DAC"}, |
| 243 | |
| 244 | /* Mic */ |
| 245 | {"Mic", NULL, "AIN"}, |
| 246 | {"Input Mux", "Internal", "Mic Int Bias"}, |
| 247 | {"Input Mux", "External", "Mic Ext Bias"}, |
| 248 | {"Mic Int Bias", NULL, "MICIN"}, |
| 249 | {"Mic Ext Bias", NULL, "MICEXT"}, |
| 250 | {"MICOUT", NULL, "Input Mux"}, |
| 251 | |
| 252 | /* line out */ |
| 253 | {"LOUT", NULL, "Line Out Enable"}, |
| 254 | {"ROUT", NULL, "Line Out Enable"}, |
| 255 | {"Line Out Enable", "Switch", "Line Out"}, |
| 256 | {"Line Out", NULL, "Stereo Mixer"}, |
| 257 | |
| 258 | /* mono1 out */ |
| 259 | {"MOUT1", NULL, "Mono Out"}, |
| 260 | {"Mono Out", NULL, "Mono1 Mixer"}, |
| 261 | |
| 262 | /* left HP */ |
| 263 | {"HPL", NULL, "Left HP Enable"}, |
| 264 | {"Left HP Enable", "Switch", "HP L Amp"}, |
| 265 | {"HP L Amp", NULL, "Stereo Mixer"}, |
| 266 | |
| 267 | /* right HP */ |
| 268 | {"HPR", NULL, "Right HP Enable"}, |
| 269 | {"Right HP Enable", "Switch", "HP R Amp"}, |
| 270 | {"HP R Amp", NULL, "Stereo Mixer"}, |
| 271 | |
| 272 | /* speaker */ |
| 273 | {"SPP", NULL, "Speaker Enable"}, |
| 274 | {"SPN", NULL, "Speaker Enable"}, |
| 275 | {"Speaker Enable", "Switch", "Spk Amp"}, |
| 276 | {"Spk Amp", NULL, "MIN"}, |
| 277 | |
| 278 | /* mono 2 */ |
| 279 | {"MOUT2", NULL, "Mono 2 Enable"}, |
| 280 | {"Mono 2 Enable", "Switch", "Stereo Mixer"}, |
| 281 | |
| 282 | /* Aux In */ |
| 283 | {"Aux In", NULL, "AUX"}, |
| 284 | |
| 285 | /* ADC */ |
| 286 | {"ADC", NULL, "Input Mixer"}, |
| 287 | {"Input Mixer", "Mic Capture Switch", "Mic"}, |
| 288 | {"Input Mixer", "Aux Capture Switch", "Aux In"}, |
| 289 | }; |
| 290 | |
| 291 | static int ak4535_add_widgets(struct snd_soc_codec *codec) |
| 292 | { |
| 293 | snd_soc_dapm_new_controls(codec, ak4535_dapm_widgets, |
| 294 | ARRAY_SIZE(ak4535_dapm_widgets)); |
| 295 | |
| 296 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 297 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 301 | static int ak4535_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 302 | int clk_id, unsigned int freq, int dir) |
| 303 | { |
| 304 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 305 | struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 306 | |
| 307 | ak4535->sysclk = freq; |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int ak4535_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 312 | struct snd_pcm_hw_params *params, |
| 313 | struct snd_soc_dai *dai) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 314 | { |
| 315 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 316 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 317 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 318 | struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 319 | u8 mode2 = ak4535_read_reg_cache(codec, AK4535_MODE2) & ~(0x3 << 5); |
| 320 | int rate = params_rate(params), fs = 256; |
| 321 | |
| 322 | if (rate) |
| 323 | fs = ak4535->sysclk / rate; |
| 324 | |
| 325 | /* set fs */ |
| 326 | switch (fs) { |
| 327 | case 1024: |
| 328 | mode2 |= (0x2 << 5); |
| 329 | break; |
| 330 | case 512: |
| 331 | mode2 |= (0x1 << 5); |
| 332 | break; |
| 333 | case 256: |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | /* set rate */ |
| 338 | ak4535_write(codec, AK4535_MODE2, mode2); |
| 339 | return 0; |
| 340 | } |
| 341 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 342 | static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 343 | unsigned int fmt) |
| 344 | { |
| 345 | struct snd_soc_codec *codec = codec_dai->codec; |
| 346 | u8 mode1 = 0; |
| 347 | |
| 348 | /* interface format */ |
| 349 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 350 | case SND_SOC_DAIFMT_I2S: |
| 351 | mode1 = 0x0002; |
| 352 | break; |
| 353 | case SND_SOC_DAIFMT_LEFT_J: |
| 354 | mode1 = 0x0001; |
| 355 | break; |
| 356 | default: |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | /* use 32 fs for BCLK to save power */ |
| 361 | mode1 |= 0x4; |
| 362 | |
| 363 | ak4535_write(codec, AK4535_MODE1, mode1); |
| 364 | return 0; |
| 365 | } |
| 366 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 367 | static int ak4535_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 368 | { |
| 369 | struct snd_soc_codec *codec = dai->codec; |
| 370 | u16 mute_reg = ak4535_read_reg_cache(codec, AK4535_DAC) & 0xffdf; |
| 371 | if (!mute) |
| 372 | ak4535_write(codec, AK4535_DAC, mute_reg); |
| 373 | else |
| 374 | ak4535_write(codec, AK4535_DAC, mute_reg | 0x20); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int ak4535_set_bias_level(struct snd_soc_codec *codec, |
| 379 | enum snd_soc_bias_level level) |
| 380 | { |
| 381 | u16 i; |
| 382 | |
| 383 | switch (level) { |
| 384 | case SND_SOC_BIAS_ON: |
| 385 | ak4535_mute(codec->dai, 0); |
| 386 | break; |
| 387 | case SND_SOC_BIAS_PREPARE: |
| 388 | ak4535_mute(codec->dai, 1); |
| 389 | break; |
| 390 | case SND_SOC_BIAS_STANDBY: |
| 391 | i = ak4535_read_reg_cache(codec, AK4535_PM1); |
| 392 | ak4535_write(codec, AK4535_PM1, i | 0x80); |
| 393 | i = ak4535_read_reg_cache(codec, AK4535_PM2); |
| 394 | ak4535_write(codec, AK4535_PM2, i & (~0x80)); |
| 395 | break; |
| 396 | case SND_SOC_BIAS_OFF: |
| 397 | i = ak4535_read_reg_cache(codec, AK4535_PM1); |
| 398 | ak4535_write(codec, AK4535_PM1, i & (~0x80)); |
| 399 | break; |
| 400 | } |
| 401 | codec->bias_level = level; |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 406 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 407 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) |
| 408 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 409 | static struct snd_soc_dai_ops ak4535_dai_ops = { |
| 410 | .hw_params = ak4535_hw_params, |
| 411 | .set_fmt = ak4535_set_dai_fmt, |
| 412 | .digital_mute = ak4535_mute, |
| 413 | .set_sysclk = ak4535_set_dai_sysclk, |
| 414 | }; |
| 415 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 416 | struct snd_soc_dai ak4535_dai = { |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 417 | .name = "AK4535", |
| 418 | .playback = { |
| 419 | .stream_name = "Playback", |
| 420 | .channels_min = 1, |
| 421 | .channels_max = 2, |
| 422 | .rates = AK4535_RATES, |
| 423 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 424 | .capture = { |
| 425 | .stream_name = "Capture", |
| 426 | .channels_min = 1, |
| 427 | .channels_max = 2, |
| 428 | .rates = AK4535_RATES, |
| 429 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 430 | .ops = &ak4535_dai_ops, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 431 | }; |
| 432 | EXPORT_SYMBOL_GPL(ak4535_dai); |
| 433 | |
| 434 | static int ak4535_suspend(struct platform_device *pdev, pm_message_t state) |
| 435 | { |
| 436 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 437 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 438 | |
| 439 | ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int ak4535_resume(struct platform_device *pdev) |
| 444 | { |
| 445 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 446 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 447 | ak4535_sync(codec); |
| 448 | ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * initialise the AK4535 driver |
| 454 | * register the mixer and dsp interfaces with the kernel |
| 455 | */ |
| 456 | static int ak4535_init(struct snd_soc_device *socdev) |
| 457 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 458 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 459 | int ret = 0; |
| 460 | |
| 461 | codec->name = "AK4535"; |
| 462 | codec->owner = THIS_MODULE; |
| 463 | codec->read = ak4535_read_reg_cache; |
| 464 | codec->write = ak4535_write; |
| 465 | codec->set_bias_level = ak4535_set_bias_level; |
| 466 | codec->dai = &ak4535_dai; |
| 467 | codec->num_dai = 1; |
| 468 | codec->reg_cache_size = ARRAY_SIZE(ak4535_reg); |
| 469 | codec->reg_cache = kmemdup(ak4535_reg, sizeof(ak4535_reg), GFP_KERNEL); |
| 470 | |
| 471 | if (codec->reg_cache == NULL) |
| 472 | return -ENOMEM; |
| 473 | |
| 474 | /* register pcms */ |
| 475 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 476 | if (ret < 0) { |
| 477 | printk(KERN_ERR "ak4535: failed to create pcms\n"); |
| 478 | goto pcm_err; |
| 479 | } |
| 480 | |
| 481 | /* power on device */ |
| 482 | ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 483 | |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 484 | snd_soc_add_controls(codec, ak4535_snd_controls, |
| 485 | ARRAY_SIZE(ak4535_snd_controls)); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 486 | ak4535_add_widgets(codec); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 487 | |
| 488 | return ret; |
| 489 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 490 | pcm_err: |
| 491 | kfree(codec->reg_cache); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | static struct snd_soc_device *ak4535_socdev; |
| 497 | |
| 498 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 499 | |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 500 | static int ak4535_i2c_probe(struct i2c_client *i2c, |
| 501 | const struct i2c_device_id *id) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 502 | { |
| 503 | struct snd_soc_device *socdev = ak4535_socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 504 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 505 | int ret; |
| 506 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 507 | i2c_set_clientdata(i2c, codec); |
| 508 | codec->control_data = i2c; |
| 509 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 510 | ret = ak4535_init(socdev); |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 511 | if (ret < 0) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 512 | printk(KERN_ERR "failed to initialise AK4535\n"); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 513 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 517 | static int ak4535_i2c_remove(struct i2c_client *client) |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 518 | { |
| 519 | struct snd_soc_codec *codec = i2c_get_clientdata(client); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 520 | kfree(codec->reg_cache); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 524 | static const struct i2c_device_id ak4535_i2c_id[] = { |
| 525 | { "ak4535", 0 }, |
| 526 | { } |
| 527 | }; |
| 528 | MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 529 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 530 | static struct i2c_driver ak4535_i2c_driver = { |
| 531 | .driver = { |
| 532 | .name = "AK4535 I2C Codec", |
| 533 | .owner = THIS_MODULE, |
| 534 | }, |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 535 | .probe = ak4535_i2c_probe, |
| 536 | .remove = ak4535_i2c_remove, |
| 537 | .id_table = ak4535_i2c_id, |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 538 | }; |
| 539 | |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 540 | static int ak4535_add_i2c_device(struct platform_device *pdev, |
| 541 | const struct ak4535_setup_data *setup) |
| 542 | { |
| 543 | struct i2c_board_info info; |
| 544 | struct i2c_adapter *adapter; |
| 545 | struct i2c_client *client; |
| 546 | int ret; |
| 547 | |
| 548 | ret = i2c_add_driver(&ak4535_i2c_driver); |
| 549 | if (ret != 0) { |
| 550 | dev_err(&pdev->dev, "can't add i2c driver\n"); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 555 | info.addr = setup->i2c_address; |
| 556 | strlcpy(info.type, "ak4535", I2C_NAME_SIZE); |
| 557 | |
| 558 | adapter = i2c_get_adapter(setup->i2c_bus); |
| 559 | if (!adapter) { |
| 560 | dev_err(&pdev->dev, "can't get i2c adapter %d\n", |
| 561 | setup->i2c_bus); |
| 562 | goto err_driver; |
| 563 | } |
| 564 | |
| 565 | client = i2c_new_device(adapter, &info); |
| 566 | i2c_put_adapter(adapter); |
| 567 | if (!client) { |
| 568 | dev_err(&pdev->dev, "can't add i2c device at 0x%x\n", |
| 569 | (unsigned int)info.addr); |
| 570 | goto err_driver; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | |
| 575 | err_driver: |
| 576 | i2c_del_driver(&ak4535_i2c_driver); |
| 577 | return -ENODEV; |
| 578 | } |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 579 | #endif |
| 580 | |
| 581 | static int ak4535_probe(struct platform_device *pdev) |
| 582 | { |
| 583 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 584 | struct ak4535_setup_data *setup; |
| 585 | struct snd_soc_codec *codec; |
| 586 | struct ak4535_priv *ak4535; |
Mark Brown | b7c9d85 | 2008-09-01 18:47:04 +0100 | [diff] [blame] | 587 | int ret; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 588 | |
| 589 | printk(KERN_INFO "AK4535 Audio Codec %s", AK4535_VERSION); |
| 590 | |
| 591 | setup = socdev->codec_data; |
| 592 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 593 | if (codec == NULL) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | ak4535 = kzalloc(sizeof(struct ak4535_priv), GFP_KERNEL); |
| 597 | if (ak4535 == NULL) { |
| 598 | kfree(codec); |
| 599 | return -ENOMEM; |
| 600 | } |
| 601 | |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 602 | snd_soc_codec_set_drvdata(codec, ak4535); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 603 | socdev->card->codec = codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 604 | mutex_init(&codec->mutex); |
| 605 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 606 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 607 | |
| 608 | ak4535_socdev = socdev; |
Mark Brown | b7c9d85 | 2008-09-01 18:47:04 +0100 | [diff] [blame] | 609 | ret = -ENODEV; |
| 610 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 611 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 612 | if (setup->i2c_address) { |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 613 | codec->hw_write = (hw_write_t)i2c_master_send; |
Jean Delvare | d28d271 | 2008-09-01 18:46:59 +0100 | [diff] [blame] | 614 | ret = ak4535_add_i2c_device(pdev, setup); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 615 | } |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 616 | #endif |
Jean Delvare | 3051e41 | 2008-08-25 11:49:20 +0100 | [diff] [blame] | 617 | |
| 618 | if (ret != 0) { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 619 | kfree(snd_soc_codec_get_drvdata(codec)); |
Jean Delvare | 3051e41 | 2008-08-25 11:49:20 +0100 | [diff] [blame] | 620 | kfree(codec); |
| 621 | } |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | /* power down chip */ |
| 626 | static int ak4535_remove(struct platform_device *pdev) |
| 627 | { |
| 628 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 629 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 630 | |
| 631 | if (codec->control_data) |
| 632 | ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 633 | |
| 634 | snd_soc_free_pcms(socdev); |
| 635 | snd_soc_dapm_free(socdev); |
| 636 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Dan Carpenter | 09318c4 | 2009-04-06 03:50:46 +0200 | [diff] [blame] | 637 | if (codec->control_data) |
| 638 | i2c_unregister_device(codec->control_data); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 639 | i2c_del_driver(&ak4535_i2c_driver); |
| 640 | #endif |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 641 | kfree(snd_soc_codec_get_drvdata(codec)); |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 642 | kfree(codec); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | struct snd_soc_codec_device soc_codec_dev_ak4535 = { |
| 648 | .probe = ak4535_probe, |
| 649 | .remove = ak4535_remove, |
| 650 | .suspend = ak4535_suspend, |
| 651 | .resume = ak4535_resume, |
| 652 | }; |
| 653 | EXPORT_SYMBOL_GPL(soc_codec_dev_ak4535); |
| 654 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 655 | static int __init ak4535_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 656 | { |
| 657 | return snd_soc_register_dai(&ak4535_dai); |
| 658 | } |
| 659 | module_init(ak4535_modinit); |
| 660 | |
| 661 | static void __exit ak4535_exit(void) |
| 662 | { |
| 663 | snd_soc_unregister_dai(&ak4535_dai); |
| 664 | } |
| 665 | module_exit(ak4535_exit); |
| 666 | |
Richard Purdie | 796d2ca | 2008-06-23 14:51:28 +0100 | [diff] [blame] | 667 | MODULE_DESCRIPTION("Soc AK4535 driver"); |
| 668 | MODULE_AUTHOR("Richard Purdie"); |
| 669 | MODULE_LICENSE("GPL"); |