ASoC: ak4641: Fix wrong value references for boolean kctl
[pandora-kernel.git] / sound / soc / codecs / ak4641.c
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>
20 #include <linux/platform_device.h>
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
30 #include "ak4641.h"
31
32 /* codec private data */
33 struct ak4641_priv {
34         unsigned int sysclk;
35         int deemph;
36         int playback_fs;
37 };
38
39 /*
40  * ak4641 register cache
41  */
42 static const u8 ak4641_reg[AK4641_CACHEREGNUM] = {
43         0x00, 0x80, 0x00, 0x80,
44         0x02, 0x00, 0x11, 0x05,
45         0x00, 0x00, 0x36, 0x10,
46         0x00, 0x00, 0x57, 0x00,
47         0x88, 0x88, 0x08, 0x08
48 };
49
50 static const int deemph_settings[] = {44100, 0, 48000, 32000};
51
52 static int ak4641_set_deemph(struct snd_soc_codec *codec)
53 {
54         struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
55         int i, best = 0;
56
57         for (i = 0 ; i < ARRAY_SIZE(deemph_settings); i++) {
58                 /* if deemphasis is on, select the nearest available rate */
59                 if (ak4641->deemph && deemph_settings[i] != 0 &&
60                     abs(deemph_settings[i] - ak4641->playback_fs) <
61                     abs(deemph_settings[best] - ak4641->playback_fs))
62                         best = i;
63
64                 if (!ak4641->deemph && deemph_settings[i] == 0)
65                         best = i;
66         }
67
68         dev_dbg(codec->dev, "Set deemphasis %d\n", best);
69
70         return snd_soc_update_bits(codec, AK4641_DAC, 0x3, best);
71 }
72
73 static int ak4641_put_deemph(struct snd_kcontrol *kcontrol,
74                                 struct snd_ctl_elem_value *ucontrol)
75 {
76         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
77         struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
78         int deemph = ucontrol->value.integer.value[0];
79
80         if (deemph > 1)
81                 return -EINVAL;
82
83         ak4641->deemph = deemph;
84
85         return ak4641_set_deemph(codec);
86 }
87
88 static int ak4641_get_deemph(struct snd_kcontrol *kcontrol,
89                                 struct snd_ctl_elem_value *ucontrol)
90 {
91         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
92         struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
93
94         ucontrol->value.integer.value[0] = ak4641->deemph;
95         return 0;
96 };
97
98 static const char *ak4641_mono_out[] = {"(L + R)/2", "Hi-Z"};
99 static const char *ak4641_hp_out[] = {"Stereo", "Mono"};
100 static const char *ak4641_mic_select[] = {"Internal", "External"};
101 static const char *ak4641_mic_or_dac[] = {"Microphone", "Voice DAC"};
102
103
104 static const DECLARE_TLV_DB_SCALE(mono_gain_tlv, -1700, 2300, 0);
105 static const DECLARE_TLV_DB_SCALE(mic_boost_tlv, 0, 2000, 0);
106 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1050, 150, 0);
107 static const DECLARE_TLV_DB_SCALE(master_tlv, -12750, 50, 0);
108 static const DECLARE_TLV_DB_SCALE(mic_stereo_sidetone_tlv, -2700, 300, 0);
109 static const DECLARE_TLV_DB_SCALE(mic_mono_sidetone_tlv, -400, 400, 0);
110 static const DECLARE_TLV_DB_SCALE(capture_tlv, -800, 50, 0);
111 static const DECLARE_TLV_DB_SCALE(alc_tlv, -800, 50, 0);
112 static const DECLARE_TLV_DB_SCALE(aux_in_tlv, -2100, 300, 0);
113
114
115 static const struct soc_enum ak4641_mono_out_enum =
116         SOC_ENUM_SINGLE(AK4641_SIG1, 6, 2, ak4641_mono_out);
117 static const struct soc_enum ak4641_hp_out_enum =
118         SOC_ENUM_SINGLE(AK4641_MODE2, 2, 2, ak4641_hp_out);
119 static const struct soc_enum ak4641_mic_select_enum =
120         SOC_ENUM_SINGLE(AK4641_MIC, 1, 2, ak4641_mic_select);
121 static const struct soc_enum ak4641_mic_or_dac_enum =
122         SOC_ENUM_SINGLE(AK4641_BTIF, 4, 2, ak4641_mic_or_dac);
123
124 static const struct snd_kcontrol_new ak4641_snd_controls[] = {
125         SOC_ENUM("Mono 1 Output", ak4641_mono_out_enum),
126         SOC_SINGLE_TLV("Mono 1 Gain Volume", AK4641_SIG1, 7, 1, 1,
127                                                         mono_gain_tlv),
128         SOC_ENUM("Headphone Output", ak4641_hp_out_enum),
129         SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
130                                         ak4641_get_deemph, ak4641_put_deemph),
131
132         SOC_SINGLE_TLV("Mic Boost Volume", AK4641_MIC, 0, 1, 0, mic_boost_tlv),
133
134         SOC_SINGLE("ALC Operation Time", AK4641_TIMER, 0, 3, 0),
135         SOC_SINGLE("ALC Recovery Time", AK4641_TIMER, 2, 3, 0),
136         SOC_SINGLE("ALC ZC Time", AK4641_TIMER, 4, 3, 0),
137
138         SOC_SINGLE("ALC 1 Switch", AK4641_ALC1, 5, 1, 0),
139
140         SOC_SINGLE_TLV("ALC Volume", AK4641_ALC2, 0, 71, 0, alc_tlv),
141         SOC_SINGLE("Left Out Enable Switch", AK4641_SIG2, 1, 1, 0),
142         SOC_SINGLE("Right Out Enable Switch", AK4641_SIG2, 0, 1, 0),
143
144         SOC_SINGLE_TLV("Capture Volume", AK4641_PGA, 0, 71, 0, capture_tlv),
145
146         SOC_DOUBLE_R_TLV("Master Playback Volume", AK4641_LATT,
147                                 AK4641_RATT, 0, 255, 1, master_tlv),
148
149         SOC_SINGLE_TLV("AUX In Volume", AK4641_VOL, 0, 15, 0, aux_in_tlv),
150
151         SOC_SINGLE("Equalizer Switch", AK4641_DAC, 2, 1, 0),
152         SOC_SINGLE_TLV("EQ1 100 Hz Volume", AK4641_EQLO, 0, 15, 1, eq_tlv),
153         SOC_SINGLE_TLV("EQ2 250 Hz Volume", AK4641_EQLO, 4, 15, 1, eq_tlv),
154         SOC_SINGLE_TLV("EQ3 1 kHz Volume", AK4641_EQMID, 0, 15, 1, eq_tlv),
155         SOC_SINGLE_TLV("EQ4 3.5 kHz Volume", AK4641_EQMID, 4, 15, 1, eq_tlv),
156         SOC_SINGLE_TLV("EQ5 10 kHz Volume", AK4641_EQHI, 0, 15, 1, eq_tlv),
157 };
158
159 /* Mono 1 Mixer */
160 static const struct snd_kcontrol_new ak4641_mono1_mixer_controls[] = {
161         SOC_DAPM_SINGLE_TLV("Mic Mono Sidetone Volume", AK4641_VOL, 7, 1, 0,
162                                                 mic_mono_sidetone_tlv),
163         SOC_DAPM_SINGLE("Mic Mono Sidetone Switch", AK4641_SIG1, 4, 1, 0),
164         SOC_DAPM_SINGLE("Mono Playback Switch", AK4641_SIG1, 5, 1, 0),
165 };
166
167 /* Stereo Mixer */
168 static const struct snd_kcontrol_new ak4641_stereo_mixer_controls[] = {
169         SOC_DAPM_SINGLE_TLV("Mic Sidetone Volume", AK4641_VOL, 4, 7, 0,
170                                                 mic_stereo_sidetone_tlv),
171         SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4641_SIG2, 4, 1, 0),
172         SOC_DAPM_SINGLE("Playback Switch", AK4641_SIG2, 7, 1, 0),
173         SOC_DAPM_SINGLE("Aux Bypass Switch", AK4641_SIG2, 5, 1, 0),
174 };
175
176 /* Input Mixer */
177 static const struct snd_kcontrol_new ak4641_input_mixer_controls[] = {
178         SOC_DAPM_SINGLE("Mic Capture Switch", AK4641_MIC, 2, 1, 0),
179         SOC_DAPM_SINGLE("Aux Capture Switch", AK4641_MIC, 5, 1, 0),
180 };
181
182 /* Mic mux */
183 static const struct snd_kcontrol_new ak4641_mic_mux_control =
184         SOC_DAPM_ENUM("Mic Select", ak4641_mic_select_enum);
185
186 /* Input mux */
187 static const struct snd_kcontrol_new ak4641_input_mux_control =
188         SOC_DAPM_ENUM("Input Select", ak4641_mic_or_dac_enum);
189
190 /* mono 2 switch */
191 static const struct snd_kcontrol_new ak4641_mono2_control =
192         SOC_DAPM_SINGLE("Switch", AK4641_SIG1, 0, 1, 0);
193
194 /* ak4641 dapm widgets */
195 static const struct snd_soc_dapm_widget ak4641_dapm_widgets[] = {
196         SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
197                 &ak4641_stereo_mixer_controls[0],
198                 ARRAY_SIZE(ak4641_stereo_mixer_controls)),
199         SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
200                 &ak4641_mono1_mixer_controls[0],
201                 ARRAY_SIZE(ak4641_mono1_mixer_controls)),
202         SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
203                 &ak4641_input_mixer_controls[0],
204                 ARRAY_SIZE(ak4641_input_mixer_controls)),
205         SND_SOC_DAPM_MUX("Mic Mux", SND_SOC_NOPM, 0, 0,
206                 &ak4641_mic_mux_control),
207         SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
208                 &ak4641_input_mux_control),
209         SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
210                 &ak4641_mono2_control),
211
212         SND_SOC_DAPM_OUTPUT("LOUT"),
213         SND_SOC_DAPM_OUTPUT("ROUT"),
214         SND_SOC_DAPM_OUTPUT("MOUT1"),
215         SND_SOC_DAPM_OUTPUT("MOUT2"),
216         SND_SOC_DAPM_OUTPUT("MICOUT"),
217
218         SND_SOC_DAPM_ADC("ADC", "HiFi Capture", AK4641_PM1, 0, 0),
219         SND_SOC_DAPM_PGA("Mic", AK4641_PM1, 1, 0, NULL, 0),
220         SND_SOC_DAPM_PGA("AUX In", AK4641_PM1, 2, 0, NULL, 0),
221         SND_SOC_DAPM_PGA("Mono Out", AK4641_PM1, 3, 0, NULL, 0),
222         SND_SOC_DAPM_PGA("Line Out", AK4641_PM1, 4, 0, NULL, 0),
223
224         SND_SOC_DAPM_DAC("DAC", "HiFi Playback", AK4641_PM2, 0, 0),
225         SND_SOC_DAPM_PGA("Mono Out 2", AK4641_PM2, 3, 0, NULL, 0),
226
227         SND_SOC_DAPM_ADC("Voice ADC", "Voice Capture", AK4641_BTIF, 0, 0),
228         SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AK4641_BTIF, 1, 0),
229
230         SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4641_MIC, 3, 0),
231         SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4641_MIC, 4, 0),
232
233         SND_SOC_DAPM_INPUT("MICIN"),
234         SND_SOC_DAPM_INPUT("MICEXT"),
235         SND_SOC_DAPM_INPUT("AUX"),
236         SND_SOC_DAPM_INPUT("AIN"),
237 };
238
239 static const struct snd_soc_dapm_route ak4641_audio_map[] = {
240         /* Stereo Mixer */
241         {"Stereo Mixer", "Playback Switch", "DAC"},
242         {"Stereo Mixer", "Mic Sidetone Switch", "Input Mux"},
243         {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
244
245         /* Mono 1 Mixer */
246         {"Mono1 Mixer", "Mic Mono Sidetone Switch", "Input Mux"},
247         {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
248
249         /* Mic */
250         {"Mic", NULL, "AIN"},
251         {"Mic Mux", "Internal", "Mic Int Bias"},
252         {"Mic Mux", "External", "Mic Ext Bias"},
253         {"Mic Int Bias", NULL, "MICIN"},
254         {"Mic Ext Bias", NULL, "MICEXT"},
255         {"MICOUT", NULL, "Mic Mux"},
256
257         /* Input Mux */
258         {"Input Mux", "Microphone", "Mic"},
259         {"Input Mux", "Voice DAC", "Voice DAC"},
260
261         /* Line Out */
262         {"LOUT", NULL, "Line Out"},
263         {"ROUT", NULL, "Line Out"},
264         {"Line Out", NULL, "Stereo Mixer"},
265
266         /* Mono 1 Out */
267         {"MOUT1", NULL, "Mono Out"},
268         {"Mono Out", NULL, "Mono1 Mixer"},
269
270         /* Mono 2 Out */
271         {"MOUT2", NULL, "Mono 2 Enable"},
272         {"Mono 2 Enable", "Switch", "Mono Out 2"},
273         {"Mono Out 2", NULL, "Stereo Mixer"},
274
275         {"Voice ADC", NULL, "Mono 2 Enable"},
276
277         /* Aux In */
278         {"AUX In", NULL, "AUX"},
279
280         /* ADC */
281         {"ADC", NULL, "Input Mixer"},
282         {"Input Mixer", "Mic Capture Switch", "Mic"},
283         {"Input Mixer", "Aux Capture Switch", "AUX In"},
284 };
285
286 static int ak4641_set_dai_sysclk(struct snd_soc_dai *codec_dai,
287         int clk_id, unsigned int freq, int dir)
288 {
289         struct snd_soc_codec *codec = codec_dai->codec;
290         struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
291
292         ak4641->sysclk = freq;
293         return 0;
294 }
295
296 static int ak4641_i2s_hw_params(struct snd_pcm_substream *substream,
297                                  struct snd_pcm_hw_params *params,
298                                  struct snd_soc_dai *dai)
299 {
300         struct snd_soc_pcm_runtime *rtd = substream->private_data;
301         struct snd_soc_codec *codec = rtd->codec;
302         struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
303         int rate = params_rate(params), fs = 256;
304         u8 mode2;
305
306         if (rate)
307                 fs = ak4641->sysclk / rate;
308         else
309                 return -EINVAL;
310
311         /* set fs */
312         switch (fs) {
313         case 1024:
314                 mode2 = (0x2 << 5);
315                 break;
316         case 512:
317                 mode2 = (0x1 << 5);
318                 break;
319         case 256:
320                 mode2 = (0x0 << 5);
321                 break;
322         default:
323                 dev_err(codec->dev, "Error: unsupported fs=%d\n", fs);
324                 return -EINVAL;
325         }
326
327         snd_soc_update_bits(codec, AK4641_MODE2, (0x3 << 5), mode2);
328
329         /* Update de-emphasis filter for the new rate */
330         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331                 ak4641->playback_fs = rate;
332                 ak4641_set_deemph(codec);
333         };
334
335         return 0;
336 }
337
338 static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
339                                   unsigned int fmt)
340 {
341         struct snd_soc_codec *codec = codec_dai->codec;
342         u8 btif;
343
344         /* interface format */
345         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
346         case SND_SOC_DAIFMT_I2S:
347                 btif = (0x3 << 5);
348                 break;
349         case SND_SOC_DAIFMT_LEFT_J:
350                 btif = (0x2 << 5);
351                 break;
352         case SND_SOC_DAIFMT_DSP_A:      /* MSB after FRM */
353                 btif = (0x0 << 5);
354                 break;
355         case SND_SOC_DAIFMT_DSP_B:      /* MSB during FRM */
356                 btif = (0x1 << 5);
357                 break;
358         default:
359                 return -EINVAL;
360         }
361
362         return snd_soc_update_bits(codec, AK4641_BTIF, (0x3 << 5), btif);
363 }
364
365 static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
366                 unsigned int fmt)
367 {
368         struct snd_soc_codec *codec = codec_dai->codec;
369         u8 mode1 = 0;
370
371         /* interface format */
372         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
373         case SND_SOC_DAIFMT_I2S:
374                 mode1 = 0x02;
375                 break;
376         case SND_SOC_DAIFMT_LEFT_J:
377                 mode1 = 0x01;
378                 break;
379         default:
380                 return -EINVAL;
381         }
382
383         return snd_soc_write(codec, AK4641_MODE1, mode1);
384 }
385
386 static int ak4641_mute(struct snd_soc_dai *dai, int mute)
387 {
388         struct snd_soc_codec *codec = dai->codec;
389
390         return snd_soc_update_bits(codec, AK4641_DAC, 0x20, mute ? 0x20 : 0);
391 }
392
393 static int ak4641_set_bias_level(struct snd_soc_codec *codec,
394         enum snd_soc_bias_level level)
395 {
396         struct ak4641_platform_data *pdata = codec->dev->platform_data;
397         int ret;
398
399         switch (level) {
400         case SND_SOC_BIAS_ON:
401                 /* unmute */
402                 snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0);
403                 break;
404         case SND_SOC_BIAS_PREPARE:
405                 /* mute */
406                 snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0x20);
407                 break;
408         case SND_SOC_BIAS_STANDBY:
409                 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
410                         if (pdata && gpio_is_valid(pdata->gpio_power))
411                                 gpio_set_value(pdata->gpio_power, 1);
412                         mdelay(1);
413                         if (pdata && gpio_is_valid(pdata->gpio_npdn))
414                                 gpio_set_value(pdata->gpio_npdn, 1);
415                         mdelay(1);
416
417                         ret = snd_soc_cache_sync(codec);
418                         if (ret) {
419                                 dev_err(codec->dev,
420                                         "Failed to sync cache: %d\n", ret);
421                                 return ret;
422                         }
423                 }
424                 snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0x80);
425                 snd_soc_update_bits(codec, AK4641_PM2, 0x80, 0);
426                 break;
427         case SND_SOC_BIAS_OFF:
428                 snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0);
429                 if (pdata && gpio_is_valid(pdata->gpio_npdn))
430                         gpio_set_value(pdata->gpio_npdn, 0);
431                 if (pdata && gpio_is_valid(pdata->gpio_power))
432                         gpio_set_value(pdata->gpio_power, 0);
433                 codec->cache_sync = 1;
434                 break;
435         }
436         codec->dapm.bias_level = level;
437         return 0;
438 }
439
440 #define AK4641_RATES    (SNDRV_PCM_RATE_8000_48000)
441 #define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
442                          SNDRV_PCM_RATE_16000)
443 #define AK4641_FORMATS  (SNDRV_PCM_FMTBIT_S16_LE)
444
445 static struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
446         .hw_params    = ak4641_i2s_hw_params,
447         .set_fmt      = ak4641_i2s_set_dai_fmt,
448         .digital_mute = ak4641_mute,
449         .set_sysclk   = ak4641_set_dai_sysclk,
450 };
451
452 static struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
453         .hw_params    = NULL, /* rates are controlled by BT chip */
454         .set_fmt      = ak4641_pcm_set_dai_fmt,
455         .digital_mute = ak4641_mute,
456         .set_sysclk   = ak4641_set_dai_sysclk,
457 };
458
459 static struct snd_soc_dai_driver ak4641_dai[] = {
460 {
461         .name = "ak4641-hifi",
462         .id = 1,
463         .playback = {
464                 .stream_name = "HiFi Playback",
465                 .channels_min = 1,
466                 .channels_max = 2,
467                 .rates = AK4641_RATES,
468                 .formats = AK4641_FORMATS,
469         },
470         .capture = {
471                 .stream_name = "HiFi Capture",
472                 .channels_min = 1,
473                 .channels_max = 2,
474                 .rates = AK4641_RATES,
475                 .formats = AK4641_FORMATS,
476         },
477         .ops = &ak4641_i2s_dai_ops,
478         .symmetric_rates = 1,
479 },
480 {
481         .name = "ak4641-voice",
482         .id = 1,
483         .playback = {
484                 .stream_name = "Voice Playback",
485                 .channels_min = 1,
486                 .channels_max = 1,
487                 .rates = AK4641_RATES_BT,
488                 .formats = AK4641_FORMATS,
489         },
490         .capture = {
491                 .stream_name = "Voice Capture",
492                 .channels_min = 1,
493                 .channels_max = 1,
494                 .rates = AK4641_RATES_BT,
495                 .formats = AK4641_FORMATS,
496         },
497         .ops = &ak4641_pcm_dai_ops,
498         .symmetric_rates = 1,
499 },
500 };
501
502 static int ak4641_suspend(struct snd_soc_codec *codec, pm_message_t state)
503 {
504         ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
505         return 0;
506 }
507
508 static int ak4641_resume(struct snd_soc_codec *codec)
509 {
510         ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
511         return 0;
512 }
513
514 static int ak4641_probe(struct snd_soc_codec *codec)
515 {
516         struct ak4641_platform_data *pdata = codec->dev->platform_data;
517         int ret;
518
519
520         if (pdata) {
521                 if (gpio_is_valid(pdata->gpio_power)) {
522                         ret = gpio_request_one(pdata->gpio_power,
523                                         GPIOF_OUT_INIT_LOW, "ak4641 power");
524                         if (ret)
525                                 goto err_out;
526                 }
527                 if (gpio_is_valid(pdata->gpio_npdn)) {
528                         ret = gpio_request_one(pdata->gpio_npdn,
529                                         GPIOF_OUT_INIT_LOW, "ak4641 npdn");
530                         if (ret)
531                                 goto err_gpio;
532
533                         udelay(1); /* > 150 ns */
534                         gpio_set_value(pdata->gpio_npdn, 1);
535                 }
536         }
537
538         ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
539         if (ret != 0) {
540                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
541                 goto err_register;
542         }
543
544         /* power on device */
545         ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
546
547         return 0;
548
549 err_register:
550         if (pdata) {
551                 if (gpio_is_valid(pdata->gpio_power))
552                         gpio_set_value(pdata->gpio_power, 0);
553                 if (gpio_is_valid(pdata->gpio_npdn))
554                         gpio_free(pdata->gpio_npdn);
555         }
556 err_gpio:
557         if (pdata && gpio_is_valid(pdata->gpio_power))
558                 gpio_free(pdata->gpio_power);
559 err_out:
560         return ret;
561 }
562
563 static int ak4641_remove(struct snd_soc_codec *codec)
564 {
565         struct ak4641_platform_data *pdata = codec->dev->platform_data;
566
567         ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
568
569         if (pdata) {
570                 if (gpio_is_valid(pdata->gpio_power)) {
571                         gpio_set_value(pdata->gpio_power, 0);
572                         gpio_free(pdata->gpio_power);
573                 }
574                 if (gpio_is_valid(pdata->gpio_npdn))
575                         gpio_free(pdata->gpio_npdn);
576         }
577         return 0;
578 }
579
580
581 static struct snd_soc_codec_driver soc_codec_dev_ak4641 = {
582         .probe                  = ak4641_probe,
583         .remove                 = ak4641_remove,
584         .suspend                = ak4641_suspend,
585         .resume                 = ak4641_resume,
586         .controls               = ak4641_snd_controls,
587         .num_controls           = ARRAY_SIZE(ak4641_snd_controls),
588         .dapm_widgets           = ak4641_dapm_widgets,
589         .num_dapm_widgets       = ARRAY_SIZE(ak4641_dapm_widgets),
590         .dapm_routes            = ak4641_audio_map,
591         .num_dapm_routes        = ARRAY_SIZE(ak4641_audio_map),
592         .set_bias_level         = ak4641_set_bias_level,
593         .reg_cache_size         = ARRAY_SIZE(ak4641_reg),
594         .reg_word_size          = sizeof(u8),
595         .reg_cache_default      = ak4641_reg,
596         .reg_cache_step         = 1,
597 };
598
599
600 static int __devinit ak4641_i2c_probe(struct i2c_client *i2c,
601                                       const struct i2c_device_id *id)
602 {
603         struct ak4641_priv *ak4641;
604         int ret;
605
606         ak4641 = kzalloc(sizeof(struct ak4641_priv), GFP_KERNEL);
607         if (!ak4641)
608                 return -ENOMEM;
609
610         i2c_set_clientdata(i2c, ak4641);
611
612         ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_ak4641,
613                                 ak4641_dai, ARRAY_SIZE(ak4641_dai));
614         if (ret < 0)
615                 kfree(ak4641);
616
617         return ret;
618 }
619
620 static int __devexit ak4641_i2c_remove(struct i2c_client *i2c)
621 {
622         snd_soc_unregister_codec(&i2c->dev);
623         kfree(i2c_get_clientdata(i2c));
624         return 0;
625 }
626
627 static const struct i2c_device_id ak4641_i2c_id[] = {
628         { "ak4641", 0 },
629         { }
630 };
631 MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id);
632
633 static struct i2c_driver ak4641_i2c_driver = {
634         .driver = {
635                 .name = "ak4641",
636                 .owner = THIS_MODULE,
637         },
638         .probe =    ak4641_i2c_probe,
639         .remove =   __devexit_p(ak4641_i2c_remove),
640         .id_table = ak4641_i2c_id,
641 };
642
643 static int __init ak4641_modinit(void)
644 {
645         int ret;
646
647         ret = i2c_add_driver(&ak4641_i2c_driver);
648         if (ret != 0)
649                 pr_err("Failed to register AK4641 I2C driver: %d\n", ret);
650
651         return ret;
652 }
653 module_init(ak4641_modinit);
654
655 static void __exit ak4641_exit(void)
656 {
657         i2c_del_driver(&ak4641_i2c_driver);
658 }
659 module_exit(ak4641_exit);
660
661 MODULE_DESCRIPTION("SoC AK4641 driver");
662 MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>");
663 MODULE_LICENSE("GPL");