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/platform_device.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
31 #define AK4535_VERSION "0.3"
33 /* codec private data */
36 enum snd_soc_control_type control_type
;
40 * ak4535 register cache
42 static const u8 ak4535_reg
[AK4535_CACHEREGNUM
] = {
43 0x00, 0x80, 0x00, 0x03,
44 0x02, 0x00, 0x11, 0x01,
45 0x00, 0x40, 0x36, 0x10,
46 0x00, 0x00, 0x57, 0x00,
49 static const char *ak4535_mono_gain
[] = {"+6dB", "-17dB"};
50 static const char *ak4535_mono_out
[] = {"(L + R)/2", "Hi-Z"};
51 static const char *ak4535_hp_out
[] = {"Stereo", "Mono"};
52 static const char *ak4535_deemp
[] = {"44.1kHz", "Off", "48kHz", "32kHz"};
53 static const char *ak4535_mic_select
[] = {"Internal", "External"};
55 static const struct soc_enum ak4535_enum
[] = {
56 SOC_ENUM_SINGLE(AK4535_SIG1
, 7, 2, ak4535_mono_gain
),
57 SOC_ENUM_SINGLE(AK4535_SIG1
, 6, 2, ak4535_mono_out
),
58 SOC_ENUM_SINGLE(AK4535_MODE2
, 2, 2, ak4535_hp_out
),
59 SOC_ENUM_SINGLE(AK4535_DAC
, 0, 4, ak4535_deemp
),
60 SOC_ENUM_SINGLE(AK4535_MIC
, 1, 2, ak4535_mic_select
),
63 static const struct snd_kcontrol_new ak4535_snd_controls
[] = {
64 SOC_SINGLE("ALC2 Switch", AK4535_SIG1
, 1, 1, 0),
65 SOC_ENUM("Mono 1 Output", ak4535_enum
[1]),
66 SOC_ENUM("Mono 1 Gain", ak4535_enum
[0]),
67 SOC_ENUM("Headphone Output", ak4535_enum
[2]),
68 SOC_ENUM("Playback Deemphasis", ak4535_enum
[3]),
69 SOC_SINGLE("Bass Volume", AK4535_DAC
, 2, 3, 0),
70 SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC
, 0, 1, 0),
71 SOC_ENUM("Mic Select", ak4535_enum
[4]),
72 SOC_SINGLE("ALC Operation Time", AK4535_TIMER
, 0, 3, 0),
73 SOC_SINGLE("ALC Recovery Time", AK4535_TIMER
, 2, 3, 0),
74 SOC_SINGLE("ALC ZC Time", AK4535_TIMER
, 4, 3, 0),
75 SOC_SINGLE("ALC 1 Switch", AK4535_ALC1
, 5, 1, 0),
76 SOC_SINGLE("ALC 2 Switch", AK4535_ALC1
, 6, 1, 0),
77 SOC_SINGLE("ALC Volume", AK4535_ALC2
, 0, 127, 0),
78 SOC_SINGLE("Capture Volume", AK4535_PGA
, 0, 127, 0),
79 SOC_SINGLE("Left Playback Volume", AK4535_LATT
, 0, 127, 1),
80 SOC_SINGLE("Right Playback Volume", AK4535_RATT
, 0, 127, 1),
81 SOC_SINGLE("AUX Bypass Volume", AK4535_VOL
, 0, 15, 0),
82 SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL
, 4, 7, 0),
86 static const struct snd_kcontrol_new ak4535_mono1_mixer_controls
[] = {
87 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1
, 4, 1, 0),
88 SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1
, 5, 1, 0),
92 static const struct snd_kcontrol_new ak4535_stereo_mixer_controls
[] = {
93 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2
, 4, 1, 0),
94 SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2
, 7, 1, 0),
95 SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2
, 5, 1, 0),
99 static const struct snd_kcontrol_new ak4535_input_mixer_controls
[] = {
100 SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC
, 2, 1, 0),
101 SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC
, 5, 1, 0),
105 static const struct snd_kcontrol_new ak4535_input_mux_control
=
106 SOC_DAPM_ENUM("Input Select", ak4535_enum
[4]);
109 static const struct snd_kcontrol_new ak4535_hpl_control
=
110 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 1, 1, 1);
113 static const struct snd_kcontrol_new ak4535_hpr_control
=
114 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 0, 1, 1);
117 static const struct snd_kcontrol_new ak4535_mono2_control
=
118 SOC_DAPM_SINGLE("Switch", AK4535_SIG1
, 0, 1, 0);
120 /* Line out switch */
121 static const struct snd_kcontrol_new ak4535_line_control
=
122 SOC_DAPM_SINGLE("Switch", AK4535_SIG2
, 6, 1, 0);
124 /* ak4535 dapm widgets */
125 static const struct snd_soc_dapm_widget ak4535_dapm_widgets
[] = {
126 SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM
, 0, 0,
127 &ak4535_stereo_mixer_controls
[0],
128 ARRAY_SIZE(ak4535_stereo_mixer_controls
)),
129 SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM
, 0, 0,
130 &ak4535_mono1_mixer_controls
[0],
131 ARRAY_SIZE(ak4535_mono1_mixer_controls
)),
132 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM
, 0, 0,
133 &ak4535_input_mixer_controls
[0],
134 ARRAY_SIZE(ak4535_input_mixer_controls
)),
135 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM
, 0, 0,
136 &ak4535_input_mux_control
),
137 SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2
, 0, 0),
138 SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM
, 0, 0,
139 &ak4535_mono2_control
),
140 /* speaker powersave bit */
141 SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2
, 0, 0, NULL
, 0),
142 SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM
, 0, 0,
143 &ak4535_line_control
),
144 SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM
, 0, 0,
145 &ak4535_hpl_control
),
146 SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM
, 0, 0,
147 &ak4535_hpr_control
),
148 SND_SOC_DAPM_OUTPUT("LOUT"),
149 SND_SOC_DAPM_OUTPUT("HPL"),
150 SND_SOC_DAPM_OUTPUT("ROUT"),
151 SND_SOC_DAPM_OUTPUT("HPR"),
152 SND_SOC_DAPM_OUTPUT("SPP"),
153 SND_SOC_DAPM_OUTPUT("SPN"),
154 SND_SOC_DAPM_OUTPUT("MOUT1"),
155 SND_SOC_DAPM_OUTPUT("MOUT2"),
156 SND_SOC_DAPM_OUTPUT("MICOUT"),
157 SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1
, 0, 0),
158 SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2
, 3, 0, NULL
, 0),
159 SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2
, 1, 0, NULL
, 0),
160 SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2
, 2, 0, NULL
, 0),
161 SND_SOC_DAPM_PGA("Mic", AK4535_PM1
, 1, 0, NULL
, 0),
162 SND_SOC_DAPM_PGA("Line Out", AK4535_PM1
, 4, 0, NULL
, 0),
163 SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1
, 3, 0, NULL
, 0),
164 SND_SOC_DAPM_PGA("AUX In", AK4535_PM1
, 2, 0, NULL
, 0),
166 SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC
, 3, 0),
167 SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC
, 4, 0),
168 SND_SOC_DAPM_INPUT("MICIN"),
169 SND_SOC_DAPM_INPUT("MICEXT"),
170 SND_SOC_DAPM_INPUT("AUX"),
171 SND_SOC_DAPM_INPUT("MIN"),
172 SND_SOC_DAPM_INPUT("AIN"),
175 static const struct snd_soc_dapm_route ak4535_audio_map
[] = {
177 {"Stereo Mixer", "Playback Switch", "DAC"},
178 {"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
179 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
182 {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
183 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
186 {"Mic", NULL
, "AIN"},
187 {"Input Mux", "Internal", "Mic Int Bias"},
188 {"Input Mux", "External", "Mic Ext Bias"},
189 {"Mic Int Bias", NULL
, "MICIN"},
190 {"Mic Ext Bias", NULL
, "MICEXT"},
191 {"MICOUT", NULL
, "Input Mux"},
194 {"LOUT", NULL
, "Line Out Enable"},
195 {"ROUT", NULL
, "Line Out Enable"},
196 {"Line Out Enable", "Switch", "Line Out"},
197 {"Line Out", NULL
, "Stereo Mixer"},
200 {"MOUT1", NULL
, "Mono Out"},
201 {"Mono Out", NULL
, "Mono1 Mixer"},
204 {"HPL", NULL
, "Left HP Enable"},
205 {"Left HP Enable", "Switch", "HP L Amp"},
206 {"HP L Amp", NULL
, "Stereo Mixer"},
209 {"HPR", NULL
, "Right HP Enable"},
210 {"Right HP Enable", "Switch", "HP R Amp"},
211 {"HP R Amp", NULL
, "Stereo Mixer"},
214 {"SPP", NULL
, "Speaker Enable"},
215 {"SPN", NULL
, "Speaker Enable"},
216 {"Speaker Enable", "Switch", "Spk Amp"},
217 {"Spk Amp", NULL
, "MIN"},
220 {"MOUT2", NULL
, "Mono 2 Enable"},
221 {"Mono 2 Enable", "Switch", "Stereo Mixer"},
224 {"Aux In", NULL
, "AUX"},
227 {"ADC", NULL
, "Input Mixer"},
228 {"Input Mixer", "Mic Capture Switch", "Mic"},
229 {"Input Mixer", "Aux Capture Switch", "Aux In"},
232 static int ak4535_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
233 int clk_id
, unsigned int freq
, int dir
)
235 struct snd_soc_codec
*codec
= codec_dai
->codec
;
236 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
238 ak4535
->sysclk
= freq
;
242 static int ak4535_hw_params(struct snd_pcm_substream
*substream
,
243 struct snd_pcm_hw_params
*params
,
244 struct snd_soc_dai
*dai
)
246 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
247 struct snd_soc_codec
*codec
= rtd
->codec
;
248 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
249 u8 mode2
= snd_soc_read(codec
, AK4535_MODE2
) & ~(0x3 << 5);
250 int rate
= params_rate(params
), fs
= 256;
253 fs
= ak4535
->sysclk
/ rate
;
268 snd_soc_write(codec
, AK4535_MODE2
, mode2
);
272 static int ak4535_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
275 struct snd_soc_codec
*codec
= codec_dai
->codec
;
278 /* interface format */
279 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
280 case SND_SOC_DAIFMT_I2S
:
283 case SND_SOC_DAIFMT_LEFT_J
:
290 /* use 32 fs for BCLK to save power */
293 snd_soc_write(codec
, AK4535_MODE1
, mode1
);
297 static int ak4535_mute(struct snd_soc_dai
*dai
, int mute
)
299 struct snd_soc_codec
*codec
= dai
->codec
;
300 u16 mute_reg
= snd_soc_read(codec
, AK4535_DAC
);
302 snd_soc_write(codec
, AK4535_DAC
, mute_reg
& ~0x20);
304 snd_soc_write(codec
, AK4535_DAC
, mute_reg
| 0x20);
308 static int ak4535_set_bias_level(struct snd_soc_codec
*codec
,
309 enum snd_soc_bias_level level
)
312 case SND_SOC_BIAS_ON
:
313 snd_soc_update_bits(codec
, AK4535_DAC
, 0x20, 0);
315 case SND_SOC_BIAS_PREPARE
:
316 snd_soc_update_bits(codec
, AK4535_DAC
, 0x20, 0x20);
318 case SND_SOC_BIAS_STANDBY
:
319 snd_soc_update_bits(codec
, AK4535_PM1
, 0x80, 0x80);
320 snd_soc_update_bits(codec
, AK4535_PM2
, 0x80, 0);
322 case SND_SOC_BIAS_OFF
:
323 snd_soc_update_bits(codec
, AK4535_PM1
, 0x80, 0);
326 codec
->dapm
.bias_level
= level
;
330 #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
331 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
332 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
334 static struct snd_soc_dai_ops ak4535_dai_ops
= {
335 .hw_params
= ak4535_hw_params
,
336 .set_fmt
= ak4535_set_dai_fmt
,
337 .digital_mute
= ak4535_mute
,
338 .set_sysclk
= ak4535_set_dai_sysclk
,
341 static struct snd_soc_dai_driver ak4535_dai
= {
342 .name
= "ak4535-hifi",
344 .stream_name
= "Playback",
347 .rates
= AK4535_RATES
,
348 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
350 .stream_name
= "Capture",
353 .rates
= AK4535_RATES
,
354 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
355 .ops
= &ak4535_dai_ops
,
358 static int ak4535_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
360 ak4535_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
364 static int ak4535_resume(struct snd_soc_codec
*codec
)
366 snd_soc_cache_sync(codec
);
367 ak4535_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
371 static int ak4535_probe(struct snd_soc_codec
*codec
)
373 struct ak4535_priv
*ak4535
= snd_soc_codec_get_drvdata(codec
);
376 printk(KERN_INFO
"AK4535 Audio Codec %s", AK4535_VERSION
);
378 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, ak4535
->control_type
);
380 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
383 /* power on device */
384 ak4535_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
386 snd_soc_add_controls(codec
, ak4535_snd_controls
,
387 ARRAY_SIZE(ak4535_snd_controls
));
391 /* power down chip */
392 static int ak4535_remove(struct snd_soc_codec
*codec
)
394 ak4535_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
398 static struct snd_soc_codec_driver soc_codec_dev_ak4535
= {
399 .probe
= ak4535_probe
,
400 .remove
= ak4535_remove
,
401 .suspend
= ak4535_suspend
,
402 .resume
= ak4535_resume
,
403 .set_bias_level
= ak4535_set_bias_level
,
404 .reg_cache_size
= ARRAY_SIZE(ak4535_reg
),
405 .reg_word_size
= sizeof(u8
),
406 .reg_cache_default
= ak4535_reg
,
407 .dapm_widgets
= ak4535_dapm_widgets
,
408 .num_dapm_widgets
= ARRAY_SIZE(ak4535_dapm_widgets
),
409 .dapm_routes
= ak4535_audio_map
,
410 .num_dapm_routes
= ARRAY_SIZE(ak4535_audio_map
),
413 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
414 static __devinit
int ak4535_i2c_probe(struct i2c_client
*i2c
,
415 const struct i2c_device_id
*id
)
417 struct ak4535_priv
*ak4535
;
420 ak4535
= kzalloc(sizeof(struct ak4535_priv
), GFP_KERNEL
);
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);
434 static __devexit
int ak4535_i2c_remove(struct i2c_client
*client
)
436 snd_soc_unregister_codec(&client
->dev
);
437 kfree(i2c_get_clientdata(client
));
441 static const struct i2c_device_id ak4535_i2c_id
[] = {
445 MODULE_DEVICE_TABLE(i2c
, ak4535_i2c_id
);
447 static struct i2c_driver ak4535_i2c_driver
= {
449 .name
= "ak4535-codec",
450 .owner
= THIS_MODULE
,
452 .probe
= ak4535_i2c_probe
,
453 .remove
= __devexit_p(ak4535_i2c_remove
),
454 .id_table
= ak4535_i2c_id
,
458 static int __init
ak4535_modinit(void)
461 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
462 ret
= i2c_add_driver(&ak4535_i2c_driver
);
464 printk(KERN_ERR
"Failed to register AK4535 I2C driver: %d\n",
470 module_init(ak4535_modinit
);
472 static void __exit
ak4535_exit(void)
474 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
475 i2c_del_driver(&ak4535_i2c_driver
);
478 module_exit(ak4535_exit
);
480 MODULE_DESCRIPTION("Soc AK4535 driver");
481 MODULE_AUTHOR("Richard Purdie");
482 MODULE_LICENSE("GPL");