blob: dd3380202766cf0ff8d1aec6ea9193317f458fa1 [file] [log] [blame]
Richard Purdie796d2ca2008-06-23 14:51:28 +01001/*
2 * ak4535.c -- AK4535 ALSA Soc Audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on wm8753.c by Liam Girdwood
9 *
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.
13 */
14
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 <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/soc-dapm.h>
27#include <sound/initval.h>
28
29#include "ak4535.h"
30
Richard Purdie796d2ca2008-06-23 14:51:28 +010031#define AK4535_VERSION "0.3"
32
33struct snd_soc_codec_device soc_codec_dev_ak4535;
34
35/* codec private data */
36struct ak4535_priv {
37 unsigned int sysclk;
38};
39
40/*
41 * ak4535 register cache
42 */
43static const u16 ak4535_reg[AK4535_CACHEREGNUM] = {
44 0x0000, 0x0080, 0x0000, 0x0003,
45 0x0002, 0x0000, 0x0011, 0x0001,
46 0x0000, 0x0040, 0x0036, 0x0010,
47 0x0000, 0x0000, 0x0057, 0x0000,
48};
49
50/*
51 * read ak4535 register cache
52 */
53static inline unsigned int ak4535_read_reg_cache(struct snd_soc_codec *codec,
54 unsigned int reg)
55{
56 u16 *cache = codec->reg_cache;
57 if (reg >= AK4535_CACHEREGNUM)
58 return -1;
59 return cache[reg];
60}
61
62static inline unsigned int ak4535_read(struct snd_soc_codec *codec,
63 unsigned int reg)
64{
65 u8 data;
66 data = reg;
67
68 if (codec->hw_write(codec->control_data, &data, 1) != 1)
69 return -EIO;
70
71 if (codec->hw_read(codec->control_data, &data, 1) != 1)
72 return -EIO;
73
74 return data;
75};
76
77/*
78 * write ak4535 register cache
79 */
80static inline void ak4535_write_reg_cache(struct snd_soc_codec *codec,
81 u16 reg, unsigned int value)
82{
83 u16 *cache = codec->reg_cache;
84 if (reg >= AK4535_CACHEREGNUM)
85 return;
86 cache[reg] = value;
87}
88
89/*
90 * write to the AK4535 register space
91 */
92static int ak4535_write(struct snd_soc_codec *codec, unsigned int reg,
93 unsigned int value)
94{
95 u8 data[2];
96
97 /* data is
98 * D15..D8 AK4535 register offset
99 * D7...D0 register data
100 */
101 data[0] = reg & 0xff;
102 data[1] = value & 0xff;
103
104 ak4535_write_reg_cache(codec, reg, value);
105 if (codec->hw_write(codec->control_data, data, 2) == 2)
106 return 0;
107 else
108 return -EIO;
109}
110
111static int ak4535_sync(struct snd_soc_codec *codec)
112{
113 u16 *cache = codec->reg_cache;
114 int i, r = 0;
115
116 for (i = 0; i < AK4535_CACHEREGNUM; i++)
117 r |= ak4535_write(codec, i, cache[i]);
118
119 return r;
120};
121
122static const char *ak4535_mono_gain[] = {"+6dB", "-17dB"};
123static const char *ak4535_mono_out[] = {"(L + R)/2", "Hi-Z"};
124static const char *ak4535_hp_out[] = {"Stereo", "Mono"};
125static const char *ak4535_deemp[] = {"44.1kHz", "Off", "48kHz", "32kHz"};
126static const char *ak4535_mic_select[] = {"Internal", "External"};
127
128static const struct soc_enum ak4535_enum[] = {
129 SOC_ENUM_SINGLE(AK4535_SIG1, 7, 2, ak4535_mono_gain),
130 SOC_ENUM_SINGLE(AK4535_SIG1, 6, 2, ak4535_mono_out),
131 SOC_ENUM_SINGLE(AK4535_MODE2, 2, 2, ak4535_hp_out),
132 SOC_ENUM_SINGLE(AK4535_DAC, 0, 4, ak4535_deemp),
133 SOC_ENUM_SINGLE(AK4535_MIC, 1, 2, ak4535_mic_select),
134};
135
136static const struct snd_kcontrol_new ak4535_snd_controls[] = {
137 SOC_SINGLE("ALC2 Switch", AK4535_SIG1, 1, 1, 0),
138 SOC_ENUM("Mono 1 Output", ak4535_enum[1]),
139 SOC_ENUM("Mono 1 Gain", ak4535_enum[0]),
140 SOC_ENUM("Headphone Output", ak4535_enum[2]),
141 SOC_ENUM("Playback Deemphasis", ak4535_enum[3]),
142 SOC_SINGLE("Bass Volume", AK4535_DAC, 2, 3, 0),
143 SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC, 0, 1, 0),
144 SOC_ENUM("Mic Select", ak4535_enum[4]),
145 SOC_SINGLE("ALC Operation Time", AK4535_TIMER, 0, 3, 0),
146 SOC_SINGLE("ALC Recovery Time", AK4535_TIMER, 2, 3, 0),
147 SOC_SINGLE("ALC ZC Time", AK4535_TIMER, 4, 3, 0),
148 SOC_SINGLE("ALC 1 Switch", AK4535_ALC1, 5, 1, 0),
149 SOC_SINGLE("ALC 2 Switch", AK4535_ALC1, 6, 1, 0),
150 SOC_SINGLE("ALC Volume", AK4535_ALC2, 0, 127, 0),
151 SOC_SINGLE("Capture Volume", AK4535_PGA, 0, 127, 0),
152 SOC_SINGLE("Left Playback Volume", AK4535_LATT, 0, 127, 1),
153 SOC_SINGLE("Right Playback Volume", AK4535_RATT, 0, 127, 1),
154 SOC_SINGLE("AUX Bypass Volume", AK4535_VOL, 0, 15, 0),
155 SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL, 4, 7, 0),
156};
157
Richard Purdie796d2ca2008-06-23 14:51:28 +0100158/* Mono 1 Mixer */
159static const struct snd_kcontrol_new ak4535_mono1_mixer_controls[] = {
160 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1, 4, 1, 0),
161 SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1, 5, 1, 0),
162};
163
164/* Stereo Mixer */
165static const struct snd_kcontrol_new ak4535_stereo_mixer_controls[] = {
166 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2, 4, 1, 0),
167 SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2, 7, 1, 0),
168 SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2, 5, 1, 0),
169};
170
171/* Input Mixer */
172static const struct snd_kcontrol_new ak4535_input_mixer_controls[] = {
173 SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC, 2, 1, 0),
174 SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC, 5, 1, 0),
175};
176
177/* Input mux */
178static const struct snd_kcontrol_new ak4535_input_mux_control =
179 SOC_DAPM_ENUM("Input Select", ak4535_enum[4]);
180
181/* HP L switch */
182static const struct snd_kcontrol_new ak4535_hpl_control =
183 SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1);
184
185/* HP R switch */
186static const struct snd_kcontrol_new ak4535_hpr_control =
187 SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1);
188
189/* mono 2 switch */
190static const struct snd_kcontrol_new ak4535_mono2_control =
191 SOC_DAPM_SINGLE("Switch", AK4535_SIG1, 0, 1, 0);
192
193/* Line out switch */
194static const struct snd_kcontrol_new ak4535_line_control =
195 SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 6, 1, 0);
196
197/* ak4535 dapm widgets */
198static const struct snd_soc_dapm_widget ak4535_dapm_widgets[] = {
199 SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
200 &ak4535_stereo_mixer_controls[0],
201 ARRAY_SIZE(ak4535_stereo_mixer_controls)),
202 SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
203 &ak4535_mono1_mixer_controls[0],
204 ARRAY_SIZE(ak4535_mono1_mixer_controls)),
205 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
206 &ak4535_input_mixer_controls[0],
207 ARRAY_SIZE(ak4535_input_mixer_controls)),
208 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
209 &ak4535_input_mux_control),
210 SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2, 0, 0),
211 SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
212 &ak4535_mono2_control),
213 /* speaker powersave bit */
214 SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2, 0, 0, NULL, 0),
215 SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM, 0, 0,
216 &ak4535_line_control),
217 SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM, 0, 0,
218 &ak4535_hpl_control),
219 SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM, 0, 0,
220 &ak4535_hpr_control),
221 SND_SOC_DAPM_OUTPUT("LOUT"),
222 SND_SOC_DAPM_OUTPUT("HPL"),
223 SND_SOC_DAPM_OUTPUT("ROUT"),
224 SND_SOC_DAPM_OUTPUT("HPR"),
225 SND_SOC_DAPM_OUTPUT("SPP"),
226 SND_SOC_DAPM_OUTPUT("SPN"),
227 SND_SOC_DAPM_OUTPUT("MOUT1"),
228 SND_SOC_DAPM_OUTPUT("MOUT2"),
229 SND_SOC_DAPM_OUTPUT("MICOUT"),
230 SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1, 0, 0),
231 SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2, 3, 0, NULL, 0),
232 SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2, 1, 0, NULL, 0),
233 SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2, 2, 0, NULL, 0),
234 SND_SOC_DAPM_PGA("Mic", AK4535_PM1, 1, 0, NULL, 0),
235 SND_SOC_DAPM_PGA("Line Out", AK4535_PM1, 4, 0, NULL, 0),
236 SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1, 3, 0, NULL, 0),
237 SND_SOC_DAPM_PGA("AUX In", AK4535_PM1, 2, 0, NULL, 0),
238
239 SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC, 3, 0),
240 SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC, 4, 0),
241 SND_SOC_DAPM_INPUT("MICIN"),
242 SND_SOC_DAPM_INPUT("MICEXT"),
243 SND_SOC_DAPM_INPUT("AUX"),
244 SND_SOC_DAPM_INPUT("MIN"),
245 SND_SOC_DAPM_INPUT("AIN"),
246};
247
248static const struct snd_soc_dapm_route audio_map[] = {
249 /*stereo mixer */
250 {"Stereo Mixer", "Playback Switch", "DAC"},
251 {"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
252 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
253
254 /* mono1 mixer */
255 {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
256 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
257
258 /* Mic */
259 {"Mic", NULL, "AIN"},
260 {"Input Mux", "Internal", "Mic Int Bias"},
261 {"Input Mux", "External", "Mic Ext Bias"},
262 {"Mic Int Bias", NULL, "MICIN"},
263 {"Mic Ext Bias", NULL, "MICEXT"},
264 {"MICOUT", NULL, "Input Mux"},
265
266 /* line out */
267 {"LOUT", NULL, "Line Out Enable"},
268 {"ROUT", NULL, "Line Out Enable"},
269 {"Line Out Enable", "Switch", "Line Out"},
270 {"Line Out", NULL, "Stereo Mixer"},
271
272 /* mono1 out */
273 {"MOUT1", NULL, "Mono Out"},
274 {"Mono Out", NULL, "Mono1 Mixer"},
275
276 /* left HP */
277 {"HPL", NULL, "Left HP Enable"},
278 {"Left HP Enable", "Switch", "HP L Amp"},
279 {"HP L Amp", NULL, "Stereo Mixer"},
280
281 /* right HP */
282 {"HPR", NULL, "Right HP Enable"},
283 {"Right HP Enable", "Switch", "HP R Amp"},
284 {"HP R Amp", NULL, "Stereo Mixer"},
285
286 /* speaker */
287 {"SPP", NULL, "Speaker Enable"},
288 {"SPN", NULL, "Speaker Enable"},
289 {"Speaker Enable", "Switch", "Spk Amp"},
290 {"Spk Amp", NULL, "MIN"},
291
292 /* mono 2 */
293 {"MOUT2", NULL, "Mono 2 Enable"},
294 {"Mono 2 Enable", "Switch", "Stereo Mixer"},
295
296 /* Aux In */
297 {"Aux In", NULL, "AUX"},
298
299 /* ADC */
300 {"ADC", NULL, "Input Mixer"},
301 {"Input Mixer", "Mic Capture Switch", "Mic"},
302 {"Input Mixer", "Aux Capture Switch", "Aux In"},
303};
304
305static int ak4535_add_widgets(struct snd_soc_codec *codec)
306{
307 snd_soc_dapm_new_controls(codec, ak4535_dapm_widgets,
308 ARRAY_SIZE(ak4535_dapm_widgets));
309
310 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
311
312 snd_soc_dapm_new_widgets(codec);
313 return 0;
314}
315
Liam Girdwoode550e172008-07-07 16:07:52 +0100316static int ak4535_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Richard Purdie796d2ca2008-06-23 14:51:28 +0100317 int clk_id, unsigned int freq, int dir)
318{
319 struct snd_soc_codec *codec = codec_dai->codec;
320 struct ak4535_priv *ak4535 = codec->private_data;
321
322 ak4535->sysclk = freq;
323 return 0;
324}
325
326static int ak4535_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000327 struct snd_pcm_hw_params *params,
328 struct snd_soc_dai *dai)
Richard Purdie796d2ca2008-06-23 14:51:28 +0100329{
330 struct snd_soc_pcm_runtime *rtd = substream->private_data;
331 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000332 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100333 struct ak4535_priv *ak4535 = codec->private_data;
334 u8 mode2 = ak4535_read_reg_cache(codec, AK4535_MODE2) & ~(0x3 << 5);
335 int rate = params_rate(params), fs = 256;
336
337 if (rate)
338 fs = ak4535->sysclk / rate;
339
340 /* set fs */
341 switch (fs) {
342 case 1024:
343 mode2 |= (0x2 << 5);
344 break;
345 case 512:
346 mode2 |= (0x1 << 5);
347 break;
348 case 256:
349 break;
350 }
351
352 /* set rate */
353 ak4535_write(codec, AK4535_MODE2, mode2);
354 return 0;
355}
356
Liam Girdwoode550e172008-07-07 16:07:52 +0100357static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai,
Richard Purdie796d2ca2008-06-23 14:51:28 +0100358 unsigned int fmt)
359{
360 struct snd_soc_codec *codec = codec_dai->codec;
361 u8 mode1 = 0;
362
363 /* interface format */
364 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
365 case SND_SOC_DAIFMT_I2S:
366 mode1 = 0x0002;
367 break;
368 case SND_SOC_DAIFMT_LEFT_J:
369 mode1 = 0x0001;
370 break;
371 default:
372 return -EINVAL;
373 }
374
375 /* use 32 fs for BCLK to save power */
376 mode1 |= 0x4;
377
378 ak4535_write(codec, AK4535_MODE1, mode1);
379 return 0;
380}
381
Liam Girdwoode550e172008-07-07 16:07:52 +0100382static int ak4535_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie796d2ca2008-06-23 14:51:28 +0100383{
384 struct snd_soc_codec *codec = dai->codec;
385 u16 mute_reg = ak4535_read_reg_cache(codec, AK4535_DAC) & 0xffdf;
386 if (!mute)
387 ak4535_write(codec, AK4535_DAC, mute_reg);
388 else
389 ak4535_write(codec, AK4535_DAC, mute_reg | 0x20);
390 return 0;
391}
392
393static int ak4535_set_bias_level(struct snd_soc_codec *codec,
394 enum snd_soc_bias_level level)
395{
396 u16 i;
397
398 switch (level) {
399 case SND_SOC_BIAS_ON:
400 ak4535_mute(codec->dai, 0);
401 break;
402 case SND_SOC_BIAS_PREPARE:
403 ak4535_mute(codec->dai, 1);
404 break;
405 case SND_SOC_BIAS_STANDBY:
406 i = ak4535_read_reg_cache(codec, AK4535_PM1);
407 ak4535_write(codec, AK4535_PM1, i | 0x80);
408 i = ak4535_read_reg_cache(codec, AK4535_PM2);
409 ak4535_write(codec, AK4535_PM2, i & (~0x80));
410 break;
411 case SND_SOC_BIAS_OFF:
412 i = ak4535_read_reg_cache(codec, AK4535_PM1);
413 ak4535_write(codec, AK4535_PM1, i & (~0x80));
414 break;
415 }
416 codec->bias_level = level;
417 return 0;
418}
419
420#define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
421 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
422 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
423
Eric Miao6335d052009-03-03 09:41:00 +0800424static struct snd_soc_dai_ops ak4535_dai_ops = {
425 .hw_params = ak4535_hw_params,
426 .set_fmt = ak4535_set_dai_fmt,
427 .digital_mute = ak4535_mute,
428 .set_sysclk = ak4535_set_dai_sysclk,
429};
430
Liam Girdwoode550e172008-07-07 16:07:52 +0100431struct snd_soc_dai ak4535_dai = {
Richard Purdie796d2ca2008-06-23 14:51:28 +0100432 .name = "AK4535",
433 .playback = {
434 .stream_name = "Playback",
435 .channels_min = 1,
436 .channels_max = 2,
437 .rates = AK4535_RATES,
438 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
439 .capture = {
440 .stream_name = "Capture",
441 .channels_min = 1,
442 .channels_max = 2,
443 .rates = AK4535_RATES,
444 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
Eric Miao6335d052009-03-03 09:41:00 +0800445 .ops = &ak4535_dai_ops,
Richard Purdie796d2ca2008-06-23 14:51:28 +0100446};
447EXPORT_SYMBOL_GPL(ak4535_dai);
448
449static int ak4535_suspend(struct platform_device *pdev, pm_message_t state)
450{
451 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000452 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100453
454 ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
455 return 0;
456}
457
458static int ak4535_resume(struct platform_device *pdev)
459{
460 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000461 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100462 ak4535_sync(codec);
463 ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
464 ak4535_set_bias_level(codec, codec->suspend_bias_level);
465 return 0;
466}
467
468/*
469 * initialise the AK4535 driver
470 * register the mixer and dsp interfaces with the kernel
471 */
472static int ak4535_init(struct snd_soc_device *socdev)
473{
Mark Brown6627a652009-01-23 22:55:23 +0000474 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100475 int ret = 0;
476
477 codec->name = "AK4535";
478 codec->owner = THIS_MODULE;
479 codec->read = ak4535_read_reg_cache;
480 codec->write = ak4535_write;
481 codec->set_bias_level = ak4535_set_bias_level;
482 codec->dai = &ak4535_dai;
483 codec->num_dai = 1;
484 codec->reg_cache_size = ARRAY_SIZE(ak4535_reg);
485 codec->reg_cache = kmemdup(ak4535_reg, sizeof(ak4535_reg), GFP_KERNEL);
486
487 if (codec->reg_cache == NULL)
488 return -ENOMEM;
489
490 /* register pcms */
491 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
492 if (ret < 0) {
493 printk(KERN_ERR "ak4535: failed to create pcms\n");
494 goto pcm_err;
495 }
496
497 /* power on device */
498 ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
499
Ian Molton3e8e1952009-01-09 00:23:21 +0000500 snd_soc_add_controls(codec, ak4535_snd_controls,
501 ARRAY_SIZE(ak4535_snd_controls));
Richard Purdie796d2ca2008-06-23 14:51:28 +0100502 ak4535_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000503 ret = snd_soc_init_card(socdev);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100504 if (ret < 0) {
505 printk(KERN_ERR "ak4535: failed to register card\n");
506 goto card_err;
507 }
508
509 return ret;
510
511card_err:
512 snd_soc_free_pcms(socdev);
513 snd_soc_dapm_free(socdev);
514pcm_err:
515 kfree(codec->reg_cache);
516
517 return ret;
518}
519
520static struct snd_soc_device *ak4535_socdev;
521
522#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
523
Jean Delvared28d2712008-09-01 18:46:59 +0100524static int ak4535_i2c_probe(struct i2c_client *i2c,
525 const struct i2c_device_id *id)
Richard Purdie796d2ca2008-06-23 14:51:28 +0100526{
527 struct snd_soc_device *socdev = ak4535_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000528 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100529 int ret;
530
Richard Purdie796d2ca2008-06-23 14:51:28 +0100531 i2c_set_clientdata(i2c, codec);
532 codec->control_data = i2c;
533
Richard Purdie796d2ca2008-06-23 14:51:28 +0100534 ret = ak4535_init(socdev);
Jean Delvared28d2712008-09-01 18:46:59 +0100535 if (ret < 0)
Richard Purdie796d2ca2008-06-23 14:51:28 +0100536 printk(KERN_ERR "failed to initialise AK4535\n");
Richard Purdie796d2ca2008-06-23 14:51:28 +0100537
Richard Purdie796d2ca2008-06-23 14:51:28 +0100538 return ret;
539}
540
Jean Delvared28d2712008-09-01 18:46:59 +0100541static int ak4535_i2c_remove(struct i2c_client *client)
Richard Purdie796d2ca2008-06-23 14:51:28 +0100542{
543 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100544 kfree(codec->reg_cache);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100545 return 0;
546}
547
Jean Delvared28d2712008-09-01 18:46:59 +0100548static const struct i2c_device_id ak4535_i2c_id[] = {
549 { "ak4535", 0 },
550 { }
551};
552MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100553
Richard Purdie796d2ca2008-06-23 14:51:28 +0100554static struct i2c_driver ak4535_i2c_driver = {
555 .driver = {
556 .name = "AK4535 I2C Codec",
557 .owner = THIS_MODULE,
558 },
Jean Delvared28d2712008-09-01 18:46:59 +0100559 .probe = ak4535_i2c_probe,
560 .remove = ak4535_i2c_remove,
561 .id_table = ak4535_i2c_id,
Richard Purdie796d2ca2008-06-23 14:51:28 +0100562};
563
Jean Delvared28d2712008-09-01 18:46:59 +0100564static int ak4535_add_i2c_device(struct platform_device *pdev,
565 const struct ak4535_setup_data *setup)
566{
567 struct i2c_board_info info;
568 struct i2c_adapter *adapter;
569 struct i2c_client *client;
570 int ret;
571
572 ret = i2c_add_driver(&ak4535_i2c_driver);
573 if (ret != 0) {
574 dev_err(&pdev->dev, "can't add i2c driver\n");
575 return ret;
576 }
577
578 memset(&info, 0, sizeof(struct i2c_board_info));
579 info.addr = setup->i2c_address;
580 strlcpy(info.type, "ak4535", I2C_NAME_SIZE);
581
582 adapter = i2c_get_adapter(setup->i2c_bus);
583 if (!adapter) {
584 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
585 setup->i2c_bus);
586 goto err_driver;
587 }
588
589 client = i2c_new_device(adapter, &info);
590 i2c_put_adapter(adapter);
591 if (!client) {
592 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
593 (unsigned int)info.addr);
594 goto err_driver;
595 }
596
597 return 0;
598
599err_driver:
600 i2c_del_driver(&ak4535_i2c_driver);
601 return -ENODEV;
602}
Richard Purdie796d2ca2008-06-23 14:51:28 +0100603#endif
604
605static int ak4535_probe(struct platform_device *pdev)
606{
607 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
608 struct ak4535_setup_data *setup;
609 struct snd_soc_codec *codec;
610 struct ak4535_priv *ak4535;
Mark Brownb7c9d852008-09-01 18:47:04 +0100611 int ret;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100612
613 printk(KERN_INFO "AK4535 Audio Codec %s", AK4535_VERSION);
614
615 setup = socdev->codec_data;
616 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
617 if (codec == NULL)
618 return -ENOMEM;
619
620 ak4535 = kzalloc(sizeof(struct ak4535_priv), GFP_KERNEL);
621 if (ak4535 == NULL) {
622 kfree(codec);
623 return -ENOMEM;
624 }
625
626 codec->private_data = ak4535;
Mark Brown6627a652009-01-23 22:55:23 +0000627 socdev->card->codec = codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100628 mutex_init(&codec->mutex);
629 INIT_LIST_HEAD(&codec->dapm_widgets);
630 INIT_LIST_HEAD(&codec->dapm_paths);
631
632 ak4535_socdev = socdev;
Mark Brownb7c9d852008-09-01 18:47:04 +0100633 ret = -ENODEV;
634
Richard Purdie796d2ca2008-06-23 14:51:28 +0100635#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
636 if (setup->i2c_address) {
Richard Purdie796d2ca2008-06-23 14:51:28 +0100637 codec->hw_write = (hw_write_t)i2c_master_send;
638 codec->hw_read = (hw_read_t)i2c_master_recv;
Jean Delvared28d2712008-09-01 18:46:59 +0100639 ret = ak4535_add_i2c_device(pdev, setup);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100640 }
Richard Purdie796d2ca2008-06-23 14:51:28 +0100641#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100642
643 if (ret != 0) {
644 kfree(codec->private_data);
645 kfree(codec);
646 }
Richard Purdie796d2ca2008-06-23 14:51:28 +0100647 return ret;
648}
649
650/* power down chip */
651static int ak4535_remove(struct platform_device *pdev)
652{
653 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000654 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie796d2ca2008-06-23 14:51:28 +0100655
656 if (codec->control_data)
657 ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
658
659 snd_soc_free_pcms(socdev);
660 snd_soc_dapm_free(socdev);
661#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Dan Carpenter09318c42009-04-06 03:50:46 +0200662 if (codec->control_data)
663 i2c_unregister_device(codec->control_data);
Richard Purdie796d2ca2008-06-23 14:51:28 +0100664 i2c_del_driver(&ak4535_i2c_driver);
665#endif
666 kfree(codec->private_data);
667 kfree(codec);
668
669 return 0;
670}
671
672struct snd_soc_codec_device soc_codec_dev_ak4535 = {
673 .probe = ak4535_probe,
674 .remove = ak4535_remove,
675 .suspend = ak4535_suspend,
676 .resume = ak4535_resume,
677};
678EXPORT_SYMBOL_GPL(soc_codec_dev_ak4535);
679
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100680static int __init ak4535_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000681{
682 return snd_soc_register_dai(&ak4535_dai);
683}
684module_init(ak4535_modinit);
685
686static void __exit ak4535_exit(void)
687{
688 snd_soc_unregister_dai(&ak4535_dai);
689}
690module_exit(ak4535_exit);
691
Richard Purdie796d2ca2008-06-23 14:51:28 +0100692MODULE_DESCRIPTION("Soc AK4535 driver");
693MODULE_AUTHOR("Richard Purdie");
694MODULE_LICENSE("GPL");