I2C: Add helper macro for i2c_driver boilerplate
[zen-stable.git] / sound / soc / codecs / ak4535.c
blob95d782d86e7d5fc906e122c23dd7ae48ff39fd4a
1 /*
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>
19 #include <linux/pm.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>
29 #include "ak4535.h"
31 #define AK4535_VERSION "0.3"
33 /* codec private data */
34 struct ak4535_priv {
35 unsigned int sysclk;
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),
85 /* Mono 1 Mixer */
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),
91 /* Stereo Mixer */
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),
98 /* Input Mixer */
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),
104 /* Input mux */
105 static const struct snd_kcontrol_new ak4535_input_mux_control =
106 SOC_DAPM_ENUM("Input Select", ak4535_enum[4]);
108 /* HP L switch */
109 static const struct snd_kcontrol_new ak4535_hpl_control =
110 SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1);
112 /* HP R switch */
113 static const struct snd_kcontrol_new ak4535_hpr_control =
114 SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1);
116 /* mono 2 switch */
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[] = {
176 /*stereo mixer */
177 {"Stereo Mixer", "Playback Switch", "DAC"},
178 {"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
179 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
181 /* mono1 mixer */
182 {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
183 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
185 /* Mic */
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"},
193 /* line out */
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"},
199 /* mono1 out */
200 {"MOUT1", NULL, "Mono Out"},
201 {"Mono Out", NULL, "Mono1 Mixer"},
203 /* left HP */
204 {"HPL", NULL, "Left HP Enable"},
205 {"Left HP Enable", "Switch", "HP L Amp"},
206 {"HP L Amp", NULL, "Stereo Mixer"},
208 /* right HP */
209 {"HPR", NULL, "Right HP Enable"},
210 {"Right HP Enable", "Switch", "HP R Amp"},
211 {"HP R Amp", NULL, "Stereo Mixer"},
213 /* speaker */
214 {"SPP", NULL, "Speaker Enable"},
215 {"SPN", NULL, "Speaker Enable"},
216 {"Speaker Enable", "Switch", "Spk Amp"},
217 {"Spk Amp", NULL, "MIN"},
219 /* mono 2 */
220 {"MOUT2", NULL, "Mono 2 Enable"},
221 {"Mono 2 Enable", "Switch", "Stereo Mixer"},
223 /* Aux In */
224 {"Aux In", NULL, "AUX"},
226 /* ADC */
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;
239 return 0;
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;
252 if (rate)
253 fs = ak4535->sysclk / rate;
255 /* set fs */
256 switch (fs) {
257 case 1024:
258 mode2 |= (0x2 << 5);
259 break;
260 case 512:
261 mode2 |= (0x1 << 5);
262 break;
263 case 256:
264 break;
267 /* set rate */
268 snd_soc_write(codec, AK4535_MODE2, mode2);
269 return 0;
272 static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai,
273 unsigned int fmt)
275 struct snd_soc_codec *codec = codec_dai->codec;
276 u8 mode1 = 0;
278 /* interface format */
279 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
280 case SND_SOC_DAIFMT_I2S:
281 mode1 = 0x0002;
282 break;
283 case SND_SOC_DAIFMT_LEFT_J:
284 mode1 = 0x0001;
285 break;
286 default:
287 return -EINVAL;
290 /* use 32 fs for BCLK to save power */
291 mode1 |= 0x4;
293 snd_soc_write(codec, AK4535_MODE1, mode1);
294 return 0;
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);
301 if (!mute)
302 snd_soc_write(codec, AK4535_DAC, mute_reg & ~0x20);
303 else
304 snd_soc_write(codec, AK4535_DAC, mute_reg | 0x20);
305 return 0;
308 static int ak4535_set_bias_level(struct snd_soc_codec *codec,
309 enum snd_soc_bias_level level)
311 switch (level) {
312 case SND_SOC_BIAS_ON:
313 snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0);
314 break;
315 case SND_SOC_BIAS_PREPARE:
316 snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0x20);
317 break;
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);
321 break;
322 case SND_SOC_BIAS_OFF:
323 snd_soc_update_bits(codec, AK4535_PM1, 0x80, 0);
324 break;
326 codec->dapm.bias_level = level;
327 return 0;
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",
343 .playback = {
344 .stream_name = "Playback",
345 .channels_min = 1,
346 .channels_max = 2,
347 .rates = AK4535_RATES,
348 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
349 .capture = {
350 .stream_name = "Capture",
351 .channels_min = 1,
352 .channels_max = 2,
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);
361 return 0;
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);
368 return 0;
371 static int ak4535_probe(struct snd_soc_codec *codec)
373 struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec);
374 int ret;
376 printk(KERN_INFO "AK4535 Audio Codec %s", AK4535_VERSION);
378 ret = snd_soc_codec_set_cache_io(codec, 8, 8, ak4535->control_type);
379 if (ret < 0) {
380 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
381 return 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));
388 return 0;
391 /* power down chip */
392 static int ak4535_remove(struct snd_soc_codec *codec)
394 ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
395 return 0;
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;
418 int ret;
420 ak4535 = kzalloc(sizeof(struct ak4535_priv), GFP_KERNEL);
421 if (ak4535 == NULL)
422 return -ENOMEM;
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);
429 if (ret < 0)
430 kfree(ak4535);
431 return ret;
434 static __devexit int ak4535_i2c_remove(struct i2c_client *client)
436 snd_soc_unregister_codec(&client->dev);
437 kfree(i2c_get_clientdata(client));
438 return 0;
441 static const struct i2c_device_id ak4535_i2c_id[] = {
442 { "ak4535", 0 },
445 MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id);
447 static struct i2c_driver ak4535_i2c_driver = {
448 .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,
456 #endif
458 static int __init ak4535_modinit(void)
460 int ret = 0;
461 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
462 ret = i2c_add_driver(&ak4535_i2c_driver);
463 if (ret != 0) {
464 printk(KERN_ERR "Failed to register AK4535 I2C driver: %d\n",
465 ret);
467 #endif
468 return ret;
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);
476 #endif
478 module_exit(ak4535_exit);
480 MODULE_DESCRIPTION("Soc AK4535 driver");
481 MODULE_AUTHOR("Richard Purdie");
482 MODULE_LICENSE("GPL");