diff options
Diffstat (limited to 'sound/soc/codecs/ssm2602.c')
-rw-r--r-- | sound/soc/codecs/ssm2602.c | 776 |
1 files changed, 776 insertions, 0 deletions
diff --git a/sound/soc/codecs/ssm2602.c b/sound/soc/codecs/ssm2602.c new file mode 100644 index 00000000000..940ce1c3522 --- /dev/null +++ b/sound/soc/codecs/ssm2602.c | |||
@@ -0,0 +1,776 @@ | |||
1 | /* | ||
2 | * File: sound/soc/codecs/ssm2602.c | ||
3 | * Author: Cliff Cai <Cliff.Cai@analog.com> | ||
4 | * | ||
5 | * Created: Tue June 06 2008 | ||
6 | * Description: Driver for ssm2602 sound chip | ||
7 | * | ||
8 | * Modified: | ||
9 | * Copyright 2008 Analog Devices Inc. | ||
10 | * | ||
11 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or modify | ||
14 | * it under the terms of the GNU General Public License as published by | ||
15 | * the Free Software Foundation; either version 2 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | * GNU General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program; if not, see the file COPYING, or write | ||
25 | * to the Free Software Foundation, Inc., | ||
26 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
27 | */ | ||
28 | |||
29 | #include <linux/module.h> | ||
30 | #include <linux/moduleparam.h> | ||
31 | #include <linux/init.h> | ||
32 | #include <linux/delay.h> | ||
33 | #include <linux/pm.h> | ||
34 | #include <linux/i2c.h> | ||
35 | #include <linux/platform_device.h> | ||
36 | #include <sound/core.h> | ||
37 | #include <sound/pcm.h> | ||
38 | #include <sound/pcm_params.h> | ||
39 | #include <sound/soc.h> | ||
40 | #include <sound/soc-dapm.h> | ||
41 | #include <sound/initval.h> | ||
42 | |||
43 | #include "ssm2602.h" | ||
44 | |||
45 | #define AUDIO_NAME "ssm2602" | ||
46 | #define SSM2602_VERSION "0.1" | ||
47 | |||
48 | struct snd_soc_codec_device soc_codec_dev_ssm2602; | ||
49 | |||
50 | /* codec private data */ | ||
51 | struct ssm2602_priv { | ||
52 | unsigned int sysclk; | ||
53 | struct snd_pcm_substream *master_substream; | ||
54 | struct snd_pcm_substream *slave_substream; | ||
55 | }; | ||
56 | |||
57 | /* | ||
58 | * ssm2602 register cache | ||
59 | * We can't read the ssm2602 register space when we are | ||
60 | * using 2 wire for device control, so we cache them instead. | ||
61 | * There is no point in caching the reset register | ||
62 | */ | ||
63 | static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = { | ||
64 | 0x0017, 0x0017, 0x0079, 0x0079, | ||
65 | 0x0000, 0x0000, 0x0000, 0x000a, | ||
66 | 0x0000, 0x0000 | ||
67 | }; | ||
68 | |||
69 | /* | ||
70 | * read ssm2602 register cache | ||
71 | */ | ||
72 | static inline unsigned int ssm2602_read_reg_cache(struct snd_soc_codec *codec, | ||
73 | unsigned int reg) | ||
74 | { | ||
75 | u16 *cache = codec->reg_cache; | ||
76 | if (reg == SSM2602_RESET) | ||
77 | return 0; | ||
78 | if (reg >= SSM2602_CACHEREGNUM) | ||
79 | return -1; | ||
80 | return cache[reg]; | ||
81 | } | ||
82 | |||
83 | /* | ||
84 | * write ssm2602 register cache | ||
85 | */ | ||
86 | static inline void ssm2602_write_reg_cache(struct snd_soc_codec *codec, | ||
87 | u16 reg, unsigned int value) | ||
88 | { | ||
89 | u16 *cache = codec->reg_cache; | ||
90 | if (reg >= SSM2602_CACHEREGNUM) | ||
91 | return; | ||
92 | cache[reg] = value; | ||
93 | } | ||
94 | |||
95 | /* | ||
96 | * write to the ssm2602 register space | ||
97 | */ | ||
98 | static int ssm2602_write(struct snd_soc_codec *codec, unsigned int reg, | ||
99 | unsigned int value) | ||
100 | { | ||
101 | u8 data[2]; | ||
102 | |||
103 | /* data is | ||
104 | * D15..D9 ssm2602 register offset | ||
105 | * D8...D0 register data | ||
106 | */ | ||
107 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); | ||
108 | data[1] = value & 0x00ff; | ||
109 | |||
110 | ssm2602_write_reg_cache(codec, reg, value); | ||
111 | if (codec->hw_write(codec->control_data, data, 2) == 2) | ||
112 | return 0; | ||
113 | else | ||
114 | return -EIO; | ||
115 | } | ||
116 | |||
117 | #define ssm2602_reset(c) ssm2602_write(c, SSM2602_RESET, 0) | ||
118 | |||
119 | /*Appending several "None"s just for OSS mixer use*/ | ||
120 | static const char *ssm2602_input_select[] = { | ||
121 | "Line", "Mic", "None", "None", "None", | ||
122 | "None", "None", "None", | ||
123 | }; | ||
124 | |||
125 | static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | ||
126 | |||
127 | static const struct soc_enum ssm2602_enum[] = { | ||
128 | SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select), | ||
129 | SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph), | ||
130 | }; | ||
131 | |||
132 | static const struct snd_kcontrol_new ssm2602_snd_controls[] = { | ||
133 | |||
134 | SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V, | ||
135 | 0, 127, 0), | ||
136 | SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V, | ||
137 | 7, 1, 0), | ||
138 | |||
139 | SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0), | ||
140 | SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1), | ||
141 | |||
142 | SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0), | ||
143 | SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1), | ||
144 | |||
145 | SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1), | ||
146 | |||
147 | SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1), | ||
148 | SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0), | ||
149 | |||
150 | SOC_ENUM("Capture Source", ssm2602_enum[0]), | ||
151 | |||
152 | SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]), | ||
153 | }; | ||
154 | |||
155 | /* add non dapm controls */ | ||
156 | static int ssm2602_add_controls(struct snd_soc_codec *codec) | ||
157 | { | ||
158 | int err, i; | ||
159 | |||
160 | for (i = 0; i < ARRAY_SIZE(ssm2602_snd_controls); i++) { | ||
161 | err = snd_ctl_add(codec->card, | ||
162 | snd_soc_cnew(&ssm2602_snd_controls[i], codec, NULL)); | ||
163 | if (err < 0) | ||
164 | return err; | ||
165 | } | ||
166 | |||
167 | return 0; | ||
168 | } | ||
169 | |||
170 | /* Output Mixer */ | ||
171 | static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = { | ||
172 | SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0), | ||
173 | SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0), | ||
174 | SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0), | ||
175 | }; | ||
176 | |||
177 | /* Input mux */ | ||
178 | static const struct snd_kcontrol_new ssm2602_input_mux_controls = | ||
179 | SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]); | ||
180 | |||
181 | static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = { | ||
182 | SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1, | ||
183 | &ssm2602_output_mixer_controls[0], | ||
184 | ARRAY_SIZE(ssm2602_output_mixer_controls)), | ||
185 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1), | ||
186 | SND_SOC_DAPM_OUTPUT("LOUT"), | ||
187 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | ||
188 | SND_SOC_DAPM_OUTPUT("ROUT"), | ||
189 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | ||
190 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1), | ||
191 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls), | ||
192 | SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0), | ||
193 | SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1), | ||
194 | SND_SOC_DAPM_INPUT("MICIN"), | ||
195 | SND_SOC_DAPM_INPUT("RLINEIN"), | ||
196 | SND_SOC_DAPM_INPUT("LLINEIN"), | ||
197 | }; | ||
198 | |||
199 | static const struct snd_soc_dapm_route audio_conn[] = { | ||
200 | /* output mixer */ | ||
201 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | ||
202 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, | ||
203 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, | ||
204 | |||
205 | /* outputs */ | ||
206 | {"RHPOUT", NULL, "Output Mixer"}, | ||
207 | {"ROUT", NULL, "Output Mixer"}, | ||
208 | {"LHPOUT", NULL, "Output Mixer"}, | ||
209 | {"LOUT", NULL, "Output Mixer"}, | ||
210 | |||
211 | /* input mux */ | ||
212 | {"Input Mux", "Line", "Line Input"}, | ||
213 | {"Input Mux", "Mic", "Mic Bias"}, | ||
214 | {"ADC", NULL, "Input Mux"}, | ||
215 | |||
216 | /* inputs */ | ||
217 | {"Line Input", NULL, "LLINEIN"}, | ||
218 | {"Line Input", NULL, "RLINEIN"}, | ||
219 | {"Mic Bias", NULL, "MICIN"}, | ||
220 | }; | ||
221 | |||
222 | static int ssm2602_add_widgets(struct snd_soc_codec *codec) | ||
223 | { | ||
224 | snd_soc_dapm_new_controls(codec, ssm2602_dapm_widgets, | ||
225 | ARRAY_SIZE(ssm2602_dapm_widgets)); | ||
226 | |||
227 | snd_soc_dapm_add_routes(codec, audio_conn, ARRAY_SIZE(audio_conn)); | ||
228 | |||
229 | snd_soc_dapm_new_widgets(codec); | ||
230 | return 0; | ||
231 | } | ||
232 | |||
233 | struct _coeff_div { | ||
234 | u32 mclk; | ||
235 | u32 rate; | ||
236 | u16 fs; | ||
237 | u8 sr:4; | ||
238 | u8 bosr:1; | ||
239 | u8 usb:1; | ||
240 | }; | ||
241 | |||
242 | /* codec mclk clock divider coefficients */ | ||
243 | static const struct _coeff_div coeff_div[] = { | ||
244 | /* 48k */ | ||
245 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, | ||
246 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, | ||
247 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, | ||
248 | |||
249 | /* 32k */ | ||
250 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, | ||
251 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, | ||
252 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, | ||
253 | |||
254 | /* 8k */ | ||
255 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, | ||
256 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, | ||
257 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, | ||
258 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, | ||
259 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, | ||
260 | |||
261 | /* 96k */ | ||
262 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, | ||
263 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, | ||
264 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, | ||
265 | |||
266 | /* 44.1k */ | ||
267 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, | ||
268 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, | ||
269 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, | ||
270 | |||
271 | /* 88.2k */ | ||
272 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, | ||
273 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, | ||
274 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, | ||
275 | }; | ||
276 | |||
277 | static inline int get_coeff(int mclk, int rate) | ||
278 | { | ||
279 | int i; | ||
280 | |||
281 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { | ||
282 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) | ||
283 | return i; | ||
284 | } | ||
285 | return i; | ||
286 | } | ||
287 | |||
288 | static int ssm2602_hw_params(struct snd_pcm_substream *substream, | ||
289 | struct snd_pcm_hw_params *params) | ||
290 | { | ||
291 | u16 srate; | ||
292 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | ||
293 | struct snd_soc_device *socdev = rtd->socdev; | ||
294 | struct snd_soc_codec *codec = socdev->codec; | ||
295 | struct ssm2602_priv *ssm2602 = codec->private_data; | ||
296 | u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3; | ||
297 | int i = get_coeff(ssm2602->sysclk, params_rate(params)); | ||
298 | |||
299 | /*no match is found*/ | ||
300 | if (i == ARRAY_SIZE(coeff_div)) | ||
301 | return -EINVAL; | ||
302 | |||
303 | srate = (coeff_div[i].sr << 2) | | ||
304 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; | ||
305 | |||
306 | ssm2602_write(codec, SSM2602_ACTIVE, 0); | ||
307 | ssm2602_write(codec, SSM2602_SRATE, srate); | ||
308 | |||
309 | /* bit size */ | ||
310 | switch (params_format(params)) { | ||
311 | case SNDRV_PCM_FORMAT_S16_LE: | ||
312 | break; | ||
313 | case SNDRV_PCM_FORMAT_S20_3LE: | ||
314 | iface |= 0x0004; | ||
315 | break; | ||
316 | case SNDRV_PCM_FORMAT_S24_LE: | ||
317 | iface |= 0x0008; | ||
318 | break; | ||
319 | case SNDRV_PCM_FORMAT_S32_LE: | ||
320 | iface |= 0x000c; | ||
321 | break; | ||
322 | } | ||
323 | ssm2602_write(codec, SSM2602_IFACE, iface); | ||
324 | ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC); | ||
325 | return 0; | ||
326 | } | ||
327 | |||
328 | static int ssm2602_startup(struct snd_pcm_substream *substream) | ||
329 | { | ||
330 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | ||
331 | struct snd_soc_device *socdev = rtd->socdev; | ||
332 | struct snd_soc_codec *codec = socdev->codec; | ||
333 | struct ssm2602_priv *ssm2602 = codec->private_data; | ||
334 | struct snd_pcm_runtime *master_runtime; | ||
335 | |||
336 | /* The DAI has shared clocks so if we already have a playback or | ||
337 | * capture going then constrain this substream to match it. | ||
338 | */ | ||
339 | if (ssm2602->master_substream) { | ||
340 | master_runtime = ssm2602->master_substream->runtime; | ||
341 | snd_pcm_hw_constraint_minmax(substream->runtime, | ||
342 | SNDRV_PCM_HW_PARAM_RATE, | ||
343 | master_runtime->rate, | ||
344 | master_runtime->rate); | ||
345 | |||
346 | snd_pcm_hw_constraint_minmax(substream->runtime, | ||
347 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | ||
348 | master_runtime->sample_bits, | ||
349 | master_runtime->sample_bits); | ||
350 | |||
351 | ssm2602->slave_substream = substream; | ||
352 | } else | ||
353 | ssm2602->master_substream = substream; | ||
354 | |||
355 | return 0; | ||
356 | } | ||
357 | |||
358 | static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream) | ||
359 | { | ||
360 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | ||
361 | struct snd_soc_device *socdev = rtd->socdev; | ||
362 | struct snd_soc_codec *codec = socdev->codec; | ||
363 | /* set active */ | ||
364 | ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC); | ||
365 | |||
366 | return 0; | ||
367 | } | ||
368 | |||
369 | static void ssm2602_shutdown(struct snd_pcm_substream *substream) | ||
370 | { | ||
371 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | ||
372 | struct snd_soc_device *socdev = rtd->socdev; | ||
373 | struct snd_soc_codec *codec = socdev->codec; | ||
374 | /* deactivate */ | ||
375 | if (!codec->active) | ||
376 | ssm2602_write(codec, SSM2602_ACTIVE, 0); | ||
377 | } | ||
378 | |||
379 | static int ssm2602_mute(struct snd_soc_dai *dai, int mute) | ||
380 | { | ||
381 | struct snd_soc_codec *codec = dai->codec; | ||
382 | u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE; | ||
383 | if (mute) | ||
384 | ssm2602_write(codec, SSM2602_APDIGI, | ||
385 | mute_reg | APDIGI_ENABLE_DAC_MUTE); | ||
386 | else | ||
387 | ssm2602_write(codec, SSM2602_APDIGI, mute_reg); | ||
388 | return 0; | ||
389 | } | ||
390 | |||
391 | static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai, | ||
392 | int clk_id, unsigned int freq, int dir) | ||
393 | { | ||
394 | struct snd_soc_codec *codec = codec_dai->codec; | ||
395 | struct ssm2602_priv *ssm2602 = codec->private_data; | ||
396 | switch (freq) { | ||
397 | case 11289600: | ||
398 | case 12000000: | ||
399 | case 12288000: | ||
400 | case 16934400: | ||
401 | case 18432000: | ||
402 | ssm2602->sysclk = freq; | ||
403 | return 0; | ||
404 | } | ||
405 | return -EINVAL; | ||
406 | } | ||
407 | |||
408 | static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai, | ||
409 | unsigned int fmt) | ||
410 | { | ||
411 | struct snd_soc_codec *codec = codec_dai->codec; | ||
412 | u16 iface = 0; | ||
413 | |||
414 | /* set master/slave audio interface */ | ||
415 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | ||
416 | case SND_SOC_DAIFMT_CBM_CFM: | ||
417 | iface |= 0x0040; | ||
418 | break; | ||
419 | case SND_SOC_DAIFMT_CBS_CFS: | ||
420 | break; | ||
421 | default: | ||
422 | return -EINVAL; | ||
423 | } | ||
424 | |||
425 | /* interface format */ | ||
426 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | ||
427 | case SND_SOC_DAIFMT_I2S: | ||
428 | iface |= 0x0002; | ||
429 | break; | ||
430 | case SND_SOC_DAIFMT_RIGHT_J: | ||
431 | break; | ||
432 | case SND_SOC_DAIFMT_LEFT_J: | ||
433 | iface |= 0x0001; | ||
434 | break; | ||
435 | case SND_SOC_DAIFMT_DSP_A: | ||
436 | iface |= 0x0003; | ||
437 | break; | ||
438 | case SND_SOC_DAIFMT_DSP_B: | ||
439 | iface |= 0x0013; | ||
440 | break; | ||
441 | default: | ||
442 | return -EINVAL; | ||
443 | } | ||
444 | |||
445 | /* clock inversion */ | ||
446 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | ||
447 | case SND_SOC_DAIFMT_NB_NF: | ||
448 | break; | ||
449 | case SND_SOC_DAIFMT_IB_IF: | ||
450 | iface |= 0x0090; | ||
451 | break; | ||
452 | case SND_SOC_DAIFMT_IB_NF: | ||
453 | iface |= 0x0080; | ||
454 | break; | ||
455 | case SND_SOC_DAIFMT_NB_IF: | ||
456 | iface |= 0x0010; | ||
457 | break; | ||
458 | default: | ||
459 | return -EINVAL; | ||
460 | } | ||
461 | |||
462 | /* set iface */ | ||
463 | ssm2602_write(codec, SSM2602_IFACE, iface); | ||
464 | return 0; | ||
465 | } | ||
466 | |||
467 | static int ssm2602_set_bias_level(struct snd_soc_codec *codec, | ||
468 | enum snd_soc_bias_level level) | ||
469 | { | ||
470 | u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f; | ||
471 | |||
472 | switch (level) { | ||
473 | case SND_SOC_BIAS_ON: | ||
474 | /* vref/mid, osc on, dac unmute */ | ||
475 | ssm2602_write(codec, SSM2602_PWR, reg); | ||
476 | break; | ||
477 | case SND_SOC_BIAS_PREPARE: | ||
478 | break; | ||
479 | case SND_SOC_BIAS_STANDBY: | ||
480 | /* everything off except vref/vmid, */ | ||
481 | ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN); | ||
482 | break; | ||
483 | case SND_SOC_BIAS_OFF: | ||
484 | /* everything off, dac mute, inactive */ | ||
485 | ssm2602_write(codec, SSM2602_ACTIVE, 0); | ||
486 | ssm2602_write(codec, SSM2602_PWR, 0xffff); | ||
487 | break; | ||
488 | |||
489 | } | ||
490 | codec->bias_level = level; | ||
491 | return 0; | ||
492 | } | ||
493 | |||
494 | #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ | ||
495 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ | ||
496 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ | ||
497 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ | ||
498 | SNDRV_PCM_RATE_96000) | ||
499 | |||
500 | struct snd_soc_dai ssm2602_dai = { | ||
501 | .name = "SSM2602", | ||
502 | .playback = { | ||
503 | .stream_name = "Playback", | ||
504 | .channels_min = 2, | ||
505 | .channels_max = 2, | ||
506 | .rates = SSM2602_RATES, | ||
507 | .formats = SNDRV_PCM_FMTBIT_S32_LE,}, | ||
508 | .capture = { | ||
509 | .stream_name = "Capture", | ||
510 | .channels_min = 2, | ||
511 | .channels_max = 2, | ||
512 | .rates = SSM2602_RATES, | ||
513 | .formats = SNDRV_PCM_FMTBIT_S32_LE,}, | ||
514 | .ops = { | ||
515 | .startup = ssm2602_startup, | ||
516 | .prepare = ssm2602_pcm_prepare, | ||
517 | .hw_params = ssm2602_hw_params, | ||
518 | .shutdown = ssm2602_shutdown, | ||
519 | }, | ||
520 | .dai_ops = { | ||
521 | .digital_mute = ssm2602_mute, | ||
522 | .set_sysclk = ssm2602_set_dai_sysclk, | ||
523 | .set_fmt = ssm2602_set_dai_fmt, | ||
524 | } | ||
525 | }; | ||
526 | EXPORT_SYMBOL_GPL(ssm2602_dai); | ||
527 | |||
528 | static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state) | ||
529 | { | ||
530 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
531 | struct snd_soc_codec *codec = socdev->codec; | ||
532 | |||
533 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF); | ||
534 | return 0; | ||
535 | } | ||
536 | |||
537 | static int ssm2602_resume(struct platform_device *pdev) | ||
538 | { | ||
539 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
540 | struct snd_soc_codec *codec = socdev->codec; | ||
541 | int i; | ||
542 | u8 data[2]; | ||
543 | u16 *cache = codec->reg_cache; | ||
544 | |||
545 | /* Sync reg_cache with the hardware */ | ||
546 | for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) { | ||
547 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); | ||
548 | data[1] = cache[i] & 0x00ff; | ||
549 | codec->hw_write(codec->control_data, data, 2); | ||
550 | } | ||
551 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | ||
552 | ssm2602_set_bias_level(codec, codec->suspend_bias_level); | ||
553 | return 0; | ||
554 | } | ||
555 | |||
556 | /* | ||
557 | * initialise the ssm2602 driver | ||
558 | * register the mixer and dsp interfaces with the kernel | ||
559 | */ | ||
560 | static int ssm2602_init(struct snd_soc_device *socdev) | ||
561 | { | ||
562 | struct snd_soc_codec *codec = socdev->codec; | ||
563 | int reg, ret = 0; | ||
564 | |||
565 | codec->name = "SSM2602"; | ||
566 | codec->owner = THIS_MODULE; | ||
567 | codec->read = ssm2602_read_reg_cache; | ||
568 | codec->write = ssm2602_write; | ||
569 | codec->set_bias_level = ssm2602_set_bias_level; | ||
570 | codec->dai = &ssm2602_dai; | ||
571 | codec->num_dai = 1; | ||
572 | codec->reg_cache_size = sizeof(ssm2602_reg); | ||
573 | codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg), | ||
574 | GFP_KERNEL); | ||
575 | if (codec->reg_cache == NULL) | ||
576 | return -ENOMEM; | ||
577 | |||
578 | ssm2602_reset(codec); | ||
579 | |||
580 | /* register pcms */ | ||
581 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | ||
582 | if (ret < 0) { | ||
583 | pr_err("ssm2602: failed to create pcms\n"); | ||
584 | goto pcm_err; | ||
585 | } | ||
586 | /*power on device*/ | ||
587 | ssm2602_write(codec, SSM2602_ACTIVE, 0); | ||
588 | /* set the update bits */ | ||
589 | reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL); | ||
590 | ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH); | ||
591 | reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL); | ||
592 | ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH); | ||
593 | reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V); | ||
594 | ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH); | ||
595 | reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V); | ||
596 | ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH); | ||
597 | /*select Line in as default input*/ | ||
598 | ssm2602_write(codec, SSM2602_APANA, | ||
599 | APANA_ENABLE_MIC_BOOST2 | APANA_SELECT_DAC | | ||
600 | APANA_ENABLE_MIC_BOOST); | ||
601 | ssm2602_write(codec, SSM2602_PWR, 0); | ||
602 | |||
603 | ssm2602_add_controls(codec); | ||
604 | ssm2602_add_widgets(codec); | ||
605 | ret = snd_soc_register_card(socdev); | ||
606 | if (ret < 0) { | ||
607 | pr_err("ssm2602: failed to register card\n"); | ||
608 | goto card_err; | ||
609 | } | ||
610 | |||
611 | return ret; | ||
612 | |||
613 | card_err: | ||
614 | snd_soc_free_pcms(socdev); | ||
615 | snd_soc_dapm_free(socdev); | ||
616 | pcm_err: | ||
617 | kfree(codec->reg_cache); | ||
618 | return ret; | ||
619 | } | ||
620 | |||
621 | static struct snd_soc_device *ssm2602_socdev; | ||
622 | |||
623 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | ||
624 | /* | ||
625 | * ssm2602 2 wire address is determined by GPIO5 | ||
626 | * state during powerup. | ||
627 | * low = 0x1a | ||
628 | * high = 0x1b | ||
629 | */ | ||
630 | static int ssm2602_i2c_probe(struct i2c_client *i2c, | ||
631 | const struct i2c_device_id *id) | ||
632 | { | ||
633 | struct snd_soc_device *socdev = ssm2602_socdev; | ||
634 | struct snd_soc_codec *codec = socdev->codec; | ||
635 | int ret; | ||
636 | |||
637 | i2c_set_clientdata(i2c, codec); | ||
638 | codec->control_data = i2c; | ||
639 | |||
640 | ret = ssm2602_init(socdev); | ||
641 | if (ret < 0) | ||
642 | pr_err("failed to initialise SSM2602\n"); | ||
643 | |||
644 | return ret; | ||
645 | } | ||
646 | |||
647 | static int ssm2602_i2c_remove(struct i2c_client *client) | ||
648 | { | ||
649 | struct snd_soc_codec *codec = i2c_get_clientdata(client); | ||
650 | kfree(codec->reg_cache); | ||
651 | return 0; | ||
652 | } | ||
653 | |||
654 | static const struct i2c_device_id ssm2602_i2c_id[] = { | ||
655 | { "ssm2602", 0 }, | ||
656 | { } | ||
657 | }; | ||
658 | MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id); | ||
659 | /* corgi i2c codec control layer */ | ||
660 | static struct i2c_driver ssm2602_i2c_driver = { | ||
661 | .driver = { | ||
662 | .name = "SSM2602 I2C Codec", | ||
663 | .owner = THIS_MODULE, | ||
664 | }, | ||
665 | .probe = ssm2602_i2c_probe, | ||
666 | .remove = ssm2602_i2c_remove, | ||
667 | .id_table = ssm2602_i2c_id, | ||
668 | }; | ||
669 | |||
670 | static int ssm2602_add_i2c_device(struct platform_device *pdev, | ||
671 | const struct ssm2602_setup_data *setup) | ||
672 | { | ||
673 | struct i2c_board_info info; | ||
674 | struct i2c_adapter *adapter; | ||
675 | struct i2c_client *client; | ||
676 | int ret; | ||
677 | |||
678 | ret = i2c_add_driver(&ssm2602_i2c_driver); | ||
679 | if (ret != 0) { | ||
680 | dev_err(&pdev->dev, "can't add i2c driver\n"); | ||
681 | return ret; | ||
682 | } | ||
683 | memset(&info, 0, sizeof(struct i2c_board_info)); | ||
684 | info.addr = setup->i2c_address; | ||
685 | strlcpy(info.type, "ssm2602", I2C_NAME_SIZE); | ||
686 | adapter = i2c_get_adapter(setup->i2c_bus); | ||
687 | if (!adapter) { | ||
688 | dev_err(&pdev->dev, "can't get i2c adapter %d\n", | ||
689 | setup->i2c_bus); | ||
690 | goto err_driver; | ||
691 | } | ||
692 | client = i2c_new_device(adapter, &info); | ||
693 | i2c_put_adapter(adapter); | ||
694 | if (!client) { | ||
695 | dev_err(&pdev->dev, "can't add i2c device at 0x%x\n", | ||
696 | (unsigned int)info.addr); | ||
697 | goto err_driver; | ||
698 | } | ||
699 | return 0; | ||
700 | err_driver: | ||
701 | i2c_del_driver(&ssm2602_i2c_driver); | ||
702 | return -ENODEV; | ||
703 | } | ||
704 | #endif | ||
705 | |||
706 | static int ssm2602_probe(struct platform_device *pdev) | ||
707 | { | ||
708 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
709 | struct ssm2602_setup_data *setup; | ||
710 | struct snd_soc_codec *codec; | ||
711 | struct ssm2602_priv *ssm2602; | ||
712 | int ret = 0; | ||
713 | |||
714 | pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION); | ||
715 | |||
716 | setup = socdev->codec_data; | ||
717 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); | ||
718 | if (codec == NULL) | ||
719 | return -ENOMEM; | ||
720 | |||
721 | ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL); | ||
722 | if (ssm2602 == NULL) { | ||
723 | kfree(codec); | ||
724 | return -ENOMEM; | ||
725 | } | ||
726 | |||
727 | codec->private_data = ssm2602; | ||
728 | socdev->codec = codec; | ||
729 | mutex_init(&codec->mutex); | ||
730 | INIT_LIST_HEAD(&codec->dapm_widgets); | ||
731 | INIT_LIST_HEAD(&codec->dapm_paths); | ||
732 | |||
733 | ssm2602_socdev = socdev; | ||
734 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | ||
735 | if (setup->i2c_address) { | ||
736 | codec->hw_write = (hw_write_t)i2c_master_send; | ||
737 | ret = ssm2602_add_i2c_device(pdev, setup); | ||
738 | } | ||
739 | #else | ||
740 | /* other interfaces */ | ||
741 | #endif | ||
742 | return ret; | ||
743 | } | ||
744 | |||
745 | /* remove everything here */ | ||
746 | static int ssm2602_remove(struct platform_device *pdev) | ||
747 | { | ||
748 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
749 | struct snd_soc_codec *codec = socdev->codec; | ||
750 | |||
751 | if (codec->control_data) | ||
752 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF); | ||
753 | |||
754 | snd_soc_free_pcms(socdev); | ||
755 | snd_soc_dapm_free(socdev); | ||
756 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | ||
757 | i2c_unregister_device(codec->control_data); | ||
758 | i2c_del_driver(&ssm2602_i2c_driver); | ||
759 | #endif | ||
760 | kfree(codec->private_data); | ||
761 | kfree(codec); | ||
762 | |||
763 | return 0; | ||
764 | } | ||
765 | |||
766 | struct snd_soc_codec_device soc_codec_dev_ssm2602 = { | ||
767 | .probe = ssm2602_probe, | ||
768 | .remove = ssm2602_remove, | ||
769 | .suspend = ssm2602_suspend, | ||
770 | .resume = ssm2602_resume, | ||
771 | }; | ||
772 | EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602); | ||
773 | |||
774 | MODULE_DESCRIPTION("ASoC ssm2602 driver"); | ||
775 | MODULE_AUTHOR("Cliff Cai"); | ||
776 | MODULE_LICENSE("GPL"); | ||