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