2 * ak4535.c -- AK4535 ALSA Soc Audio driver
4 * Copyright 2005 Openedhand Ltd.
6 * Author: Richard Purdie <richard@openedhand.com>
8 * Based on wm8753.c by Liam Girdwood
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.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.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>
30 #define AK4535_VERSION "0.3"
32 /* codec private data */
35 enum snd_soc_control_type control_type
;
39 * ak4535 register cache
41 static const u8 ak4535_reg
[AK4535_CACHEREGNUM
] = {
42 0x00, 0x80, 0x00, 0x03,
43 0x02, 0x00, 0x11, 0x01,
44 0x00, 0x40, 0x36, 0x10,
45 0x00, 0x00, 0x57, 0x00,
48 static const char *ak4535_mono_gain
[] = {"+6dB", "-17dB"};
49 static const char *ak4535_mono_out
[] = {"(L + R)/2", "Hi-Z"};
50 static const char *ak4535_hp_out
[] = {"Stereo", "Mono"};
51 static const char *ak4535_deemp
[] = {"44.1kHz", "Off", "48kHz", "32kHz"};
52 static const char *ak4535_mic_select
[] = {"Internal", "External"};
54 static const struct soc_enum ak4535_enum
[] = {
55 SOC_ENUM_SINGLE(AK4535_SIG1
, 7, 2, ak4535_mono_gain
),
56 SOC_ENUM_SINGLE(AK4535_SIG1
, 6, 2, ak4535_mono_out
),
57 SOC_ENUM_SINGLE(AK4535_MODE2
, 2, 2, ak4535_hp_out
),
58 SOC_ENUM_SINGLE(AK4535_DAC
, 0, 4, ak4535_deemp
),
59 SOC_ENUM_SINGLE(AK4535_MIC
, 1, 2, ak4535_mic_select
),
62 static const struct snd_kcontrol_new ak4535_snd_controls
[] = {
63 SOC_SINGLE("ALC2 Switch", AK4535_SIG1
, 1, 1, 0),
64 SOC_ENUM("Mono 1 Output", ak4535_enum
[1]),
65 SOC_ENUM("Mono 1 Gain", ak4535_enum
[0]),
66 SOC_ENUM("Headphone Output", ak4535_enum
[2]),
67 SOC_ENUM("Playback Deemphasis", ak4535_enum
[3]),
68 SOC_SINGLE("Bass Volume", AK4535_DAC
, 2, 3, 0),
69 SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC
, 0, 1, 0),
70 SOC_ENUM("Mic Select", ak4535_enum
[4]),
71 SOC_SINGLE("ALC Operation Time", AK4535_TIMER
, 0, 3, 0),
72 SOC_SINGLE("ALC Recovery Time", AK4535_TIMER
, 2, 3, 0),
73 SOC_SINGLE("ALC ZC Time", AK4535_TIMER
, 4, 3, 0),
74 SOC_SINGLE("ALC 1 Switch", AK4535_ALC1
, 5, 1, 0),
75 SOC_SINGLE("ALC 2 Switch", AK4535_ALC1
, 6, 1, 0),
76 SOC_SINGLE("ALC Volume", AK4535_ALC2
, 0, 127, 0),
77 SOC_SINGLE("Capture Volume", AK4535_PGA
, 0, 127, 0),
78 SOC_SINGLE("Left Playback Volume", AK4535_LATT
, 0, 127, 1),
79 SOC_SINGLE("Right Playback Volume", AK4535_RATT
, 0, 127, 1),
80 SOC_SINGLE("AUX Bypass Volume", AK4535_VOL
, 0, 15, 0),
81 SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL
, 4, 7, 0),
85 static const struct snd_kcontrol_new ak4535_mono1_mixer_controls
[] = {
86 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1
, 4, 1, 0),
87 SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1
, 5, 1, 0),
91 static const struct snd_kcontrol_new ak4535_stereo_mixer_controls
[] = {
92 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2
, 4, 1, 0),
93 SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2
, 7, 1, 0),
94 SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2
, 5, 1, 0),
98 static const struct snd_kcontrol_new ak4535_input_mixer_controls
[] = {
99 SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC
, 2, 1, 0),
100 SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC
, 5, 1, 0),
104 static const struct snd_kcontrol_new ak4535_input_mux_control
=
105 SOC_DAPM_ENUM("Input Select", ak4535_enum
[4]);
108 static const struct snd_kcontrol_new ak4535_hpl_control
=
109 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 1, 1, 1);
112 static const struct snd_kcontrol_new ak4535_hpr_control
=
113 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 0, 1, 1);
116 static const struct snd_kcontrol_new ak4535_mono2_control
=
117 SOC_DAPM_SINGLE("Switch", AK4535_SIG1
, 0, 1, 0);
119 /* Line out switch */
120 static const struct snd_kcontrol_new ak4535_line_control
=
121 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 6, 1, 0);
123 /* ak4535 dapm widgets */
124 static const struct snd_soc_dapm_widget ak4535_dapm_widgets
[] = {
125 SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM
, 0, 0,
126 &ak4535_stereo_mixer_controls
[0],
127 ARRAY_SIZE(ak4535_stereo_mixer_controls
)),
128 SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM
, 0, 0,
129 &ak4535_mono1_mixer_controls
[0],
130 ARRAY_SIZE(ak4535_mono1_mixer_controls
)),
131 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM
, 0, 0,
132 &ak4535_input_mixer_controls
[0],
133 ARRAY_SIZE(ak4535_input_mixer_controls
)),
134 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM
, 0, 0,
135 &ak4535_input_mux_control
),
136 SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2
, 0, 0),
137 SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM
, 0, 0,
138 &ak4535_mono2_control
),
139 /* speaker powersave bit */
140 SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2
, 0, 0, NULL
, 0),
141 SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM
, 0, 0,
142 &ak4535_line_control
),
143 SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM
, 0, 0,
144 &ak4535_hpl_control
),
145 SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM
, 0, 0,
146 &ak4535_hpr_control
),
147 SND_SOC_DAPM_OUTPUT("LOUT"),
148 SND_SOC_DAPM_OUTPUT("HPL"),
149 SND_SOC_DAPM_OUTPUT("ROUT"),
150 SND_SOC_DAPM_OUTPUT("HPR"),
151 SND_SOC_DAPM_OUTPUT("SPP"),
152 SND_SOC_DAPM_OUTPUT("SPN"),
153 SND_SOC_DAPM_OUTPUT("MOUT1"),
154 SND_SOC_DAPM_OUTPUT("MOUT2"),
155 SND_SOC_DAPM_OUTPUT("MICOUT"),
156 SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1
, 0, 0),
157 SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2
, 3, 0, NULL
, 0),
158 SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2
, 1, 0, NULL
, 0),
159 SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2
, 2, 0, NULL
, 0),
160 SND_SOC_DAPM_PGA("Mic", AK4535_PM1
, 1, 0, NULL
, 0),
161 SND_SOC_DAPM_PGA("Line Out", AK4535_PM1
, 4, 0, NULL
, 0),
162 SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1
, 3, 0, NULL
, 0),
163 SND_SOC_DAPM_PGA("AUX In", AK4535_PM1
, 2, 0, NULL
, 0),
165 SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC
, 3, 0),
166 SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC
, 4, 0),
167 SND_SOC_DAPM_INPUT("MICIN"),
168 SND_SOC_DAPM_INPUT("MICEXT"),
169 SND_SOC_DAPM_INPUT("AUX"),
170 SND_SOC_DAPM_INPUT("MIN"),
171 SND_SOC_DAPM_INPUT("AIN"),
174 static const struct snd_soc_dapm_route ak4535_audio_map
[] = {
176 {"Stereo Mixer", "Playback Switch", "DAC"},
177 {"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
178 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
181 {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
182 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
185 {"Mic", NULL
, "AIN"},
186 {"Input Mux", "Internal", "Mic Int Bias"},
187 {"Input Mux", "External", "Mic Ext Bias"},
188 {"Mic Int Bias", NULL
, "MICIN"},
189 {"Mic Ext Bias", NULL
, "MICEXT"},
190 {"MICOUT", NULL
, "Input Mux"},
193 {"LOUT", NULL
, "Line Out Enable"},
194 {"ROUT", NULL
, "Line Out Enable"},
195 {"Line Out Enable", "Switch", "Line Out"},
196 {"Line Out", NULL
, "Stereo Mixer"},
199 {"MOUT1", NULL
, "Mono Out"},
200 {"Mono Out", NULL
, "Mono1 Mixer"},
203 {"HPL", NULL
, "Left HP Enable"},
204 {"Left HP Enable", "Switch", "HP L Amp"},
205 {"HP L Amp", NULL
, "Stereo Mixer"},
208 {"HPR", NULL
, "Right HP Enable"},
209 {"Right HP Enable", "Switch", "HP R Amp"},
210 {"HP R Amp", NULL
, "Stereo Mixer"},
213 {"SPP", NULL
, "Speaker Enable"},
214 {"SPN", NULL
, "Speaker Enable"},
215 {"Speaker Enable", "Switch", "Spk Amp"},
216 {"Spk Amp", NULL
, "MIN"},
219 {"MOUT2", NULL
, "Mono 2 Enable"},
220 {"Mono 2 Enable", "Switch", "Stereo Mixer"},
223 {"Aux In", NULL
, "AUX"},
226 {"ADC", NULL
, "Input Mixer"},
227 {"Input Mixer", "Mic Capture Switch", "Mic"},
228 {"Input Mixer", "Aux Capture Switch", "Aux In"},
231 static int ak4535_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
232 int clk_id
, unsigned int freq
, int dir
)
234 struct snd_soc_codec
*codec
= codec_dai
->codec
;
235 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
237 ak4535
->sysclk
= freq
;
241 static int ak4535_hw_params(struct snd_pcm_substream
*substream
,
242 struct snd_pcm_hw_params
*params
,
243 struct snd_soc_dai
*dai
)
245 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
246 struct snd_soc_codec
*codec
= rtd
->codec
;
247 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
248 u8 mode2
= snd_soc_read(codec
, AK4535_MODE2
) & ~(0x3 << 5);
249 int rate
= params_rate(params
), fs
= 256;
252 fs
= ak4535
->sysclk
/ rate
;
267 snd_soc_write(codec
, AK4535_MODE2
, mode2
);
271 static int ak4535_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
274 struct snd_soc_codec
*codec
= codec_dai
->codec
;
277 /* interface format */
278 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
279 case SND_SOC_DAIFMT_I2S
:
282 case SND_SOC_DAIFMT_LEFT_J
:
289 /* use 32 fs for BCLK to save power */
292 snd_soc_write(codec
, AK4535_MODE1
, mode1
);
296 static int ak4535_mute(struct snd_soc_dai
*dai
, int mute
)
298 struct snd_soc_codec
*codec
= dai
->codec
;
299 u16 mute_reg
= snd_soc_read(codec
, AK4535_DAC
);
301 snd_soc_write(codec
, AK4535_DAC
, mute_reg
& ~0x20);
303 snd_soc_write(codec
, AK4535_DAC
, mute_reg
| 0x20);
307 static int ak4535_set_bias_level(struct snd_soc_codec
*codec
,
308 enum snd_soc_bias_level level
)
311 case SND_SOC_BIAS_ON
:
312 snd_soc_update_bits(codec
, AK4535_DAC
, 0x20, 0);
314 case SND_SOC_BIAS_PREPARE
:
315 snd_soc_update_bits(codec
, AK4535_DAC
, 0x20, 0x20);
317 case SND_SOC_BIAS_STANDBY
:
318 snd_soc_update_bits(codec
, AK4535_PM1
, 0x80, 0x80);
319 snd_soc_update_bits(codec
, AK4535_PM2
, 0x80, 0);
321 case SND_SOC_BIAS_OFF
:
322 snd_soc_update_bits(codec
, AK4535_PM1
, 0x80, 0);
325 codec
->dapm
.bias_level
= level
;
329 #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
330 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
331 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
333 static const struct snd_soc_dai_ops ak4535_dai_ops
= {
334 .hw_params
= ak4535_hw_params
,
335 .set_fmt
= ak4535_set_dai_fmt
,
336 .digital_mute
= ak4535_mute
,
337 .set_sysclk
= ak4535_set_dai_sysclk
,
340 static struct snd_soc_dai_driver ak4535_dai
= {
341 .name
= "ak4535-hifi",
343 .stream_name
= "Playback",
346 .rates
= AK4535_RATES
,
347 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
349 .stream_name
= "Capture",
352 .rates
= AK4535_RATES
,
353 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
354 .ops
= &ak4535_dai_ops
,
357 static int ak4535_suspend(struct snd_soc_codec
*codec
)
359 ak4535_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
363 static int ak4535_resume(struct snd_soc_codec
*codec
)
365 snd_soc_cache_sync(codec
);
366 ak4535_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
370 static int ak4535_probe(struct snd_soc_codec
*codec
)
372 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
375 printk(KERN_INFO
"AK4535 Audio Codec %s", AK4535_VERSION
);
377 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, ak4535
->control_type
);
379 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
382 /* power on device */
383 ak4535_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
385 snd_soc_add_controls(codec
, ak4535_snd_controls
,
386 ARRAY_SIZE(ak4535_snd_controls
));
390 /* power down chip */
391 static int ak4535_remove(struct snd_soc_codec
*codec
)
393 ak4535_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
397 static struct snd_soc_codec_driver soc_codec_dev_ak4535
= {
398 .probe
= ak4535_probe
,
399 .remove
= ak4535_remove
,
400 .suspend
= ak4535_suspend
,
401 .resume
= ak4535_resume
,
402 .set_bias_level
= ak4535_set_bias_level
,
403 .reg_cache_size
= ARRAY_SIZE(ak4535_reg
),
404 .reg_word_size
= sizeof(u8
),
405 .reg_cache_default
= ak4535_reg
,
406 .dapm_widgets
= ak4535_dapm_widgets
,
407 .num_dapm_widgets
= ARRAY_SIZE(ak4535_dapm_widgets
),
408 .dapm_routes
= ak4535_audio_map
,
409 .num_dapm_routes
= ARRAY_SIZE(ak4535_audio_map
),
412 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
413 static __devinit
int ak4535_i2c_probe(struct i2c_client
*i2c
,
414 const struct i2c_device_id
*id
)
416 struct ak4535_priv
*ak4535
;
419 ak4535
= devm_kzalloc(&i2c
->dev
, sizeof(struct ak4535_priv
),
424 i2c_set_clientdata(i2c
, ak4535
);
425 ak4535
->control_type
= SND_SOC_I2C
;
427 ret
= snd_soc_register_codec(&i2c
->dev
,
428 &soc_codec_dev_ak4535
, &ak4535_dai
, 1);
432 static __devexit
int ak4535_i2c_remove(struct i2c_client
*client
)
434 snd_soc_unregister_codec(&client
->dev
);
438 static const struct i2c_device_id ak4535_i2c_id
[] = {
442 MODULE_DEVICE_TABLE(i2c
, ak4535_i2c_id
);
444 static struct i2c_driver ak4535_i2c_driver
= {
446 .name
= "ak4535-codec",
447 .owner
= THIS_MODULE
,
449 .probe
= ak4535_i2c_probe
,
450 .remove
= __devexit_p(ak4535_i2c_remove
),
451 .id_table
= ak4535_i2c_id
,
455 static int __init
ak4535_modinit(void)
458 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
459 ret
= i2c_add_driver(&ak4535_i2c_driver
);
461 printk(KERN_ERR
"Failed to register AK4535 I2C driver: %d\n",
467 module_init(ak4535_modinit
);
469 static void __exit
ak4535_exit(void)
471 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
472 i2c_del_driver(&ak4535_i2c_driver
);
475 module_exit(ak4535_exit
);
477 MODULE_DESCRIPTION("Soc AK4535 driver");
478 MODULE_AUTHOR("Richard Purdie");
479 MODULE_LICENSE("GPL");