Commit | Line | Data |
---|---|---|
b7138212 CC |
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> | |
b39e2855 | 35 | #include <linux/spi/spi.h> |
b7138212 | 36 | #include <linux/platform_device.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
b7138212 CC |
38 | #include <sound/core.h> |
39 | #include <sound/pcm.h> | |
40 | #include <sound/pcm_params.h> | |
41 | #include <sound/soc.h> | |
b7138212 CC |
42 | #include <sound/initval.h> |
43 | ||
44 | #include "ssm2602.h" | |
45 | ||
b7138212 CC |
46 | #define SSM2602_VERSION "0.1" |
47 | ||
b7138212 CC |
48 | /* codec private data */ |
49 | struct ssm2602_priv { | |
50 | unsigned int sysclk; | |
f0fba2ad | 51 | enum snd_soc_control_type control_type; |
b7138212 CC |
52 | struct snd_pcm_substream *master_substream; |
53 | struct snd_pcm_substream *slave_substream; | |
54 | }; | |
55 | ||
56 | /* | |
57 | * ssm2602 register cache | |
58 | * We can't read the ssm2602 register space when we are | |
59 | * using 2 wire for device control, so we cache them instead. | |
60 | * There is no point in caching the reset register | |
61 | */ | |
62 | static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = { | |
63 | 0x0017, 0x0017, 0x0079, 0x0079, | |
64 | 0x0000, 0x0000, 0x0000, 0x000a, | |
65 | 0x0000, 0x0000 | |
66 | }; | |
67 | ||
93547e89 | 68 | #define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0) |
b7138212 CC |
69 | |
70 | /*Appending several "None"s just for OSS mixer use*/ | |
71 | static const char *ssm2602_input_select[] = { | |
72 | "Line", "Mic", "None", "None", "None", | |
73 | "None", "None", "None", | |
74 | }; | |
75 | ||
76 | static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
77 | ||
78 | static const struct soc_enum ssm2602_enum[] = { | |
79 | SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select), | |
80 | SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph), | |
81 | }; | |
82 | ||
83 | static const struct snd_kcontrol_new ssm2602_snd_controls[] = { | |
84 | ||
85 | SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V, | |
86 | 0, 127, 0), | |
87 | SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V, | |
88 | 7, 1, 0), | |
89 | ||
90 | SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0), | |
91 | SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1), | |
92 | ||
93 | SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0), | |
4b527e29 | 94 | SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 7, 1, 0), |
b7138212 CC |
95 | SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1), |
96 | ||
97 | SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1), | |
98 | ||
99 | SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1), | |
100 | SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0), | |
101 | ||
102 | SOC_ENUM("Capture Source", ssm2602_enum[0]), | |
103 | ||
104 | SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]), | |
105 | }; | |
106 | ||
b7138212 CC |
107 | /* Output Mixer */ |
108 | static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = { | |
109 | SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0), | |
110 | SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0), | |
111 | SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0), | |
112 | }; | |
113 | ||
114 | /* Input mux */ | |
115 | static const struct snd_kcontrol_new ssm2602_input_mux_controls = | |
116 | SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]); | |
117 | ||
118 | static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = { | |
119 | SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1, | |
120 | &ssm2602_output_mixer_controls[0], | |
121 | ARRAY_SIZE(ssm2602_output_mixer_controls)), | |
122 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1), | |
123 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
124 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
125 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
126 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
127 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1), | |
128 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls), | |
129 | SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0), | |
130 | SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1), | |
131 | SND_SOC_DAPM_INPUT("MICIN"), | |
132 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
133 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
134 | }; | |
135 | ||
136 | static const struct snd_soc_dapm_route audio_conn[] = { | |
137 | /* output mixer */ | |
138 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
139 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, | |
140 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, | |
141 | ||
142 | /* outputs */ | |
143 | {"RHPOUT", NULL, "Output Mixer"}, | |
144 | {"ROUT", NULL, "Output Mixer"}, | |
145 | {"LHPOUT", NULL, "Output Mixer"}, | |
146 | {"LOUT", NULL, "Output Mixer"}, | |
147 | ||
148 | /* input mux */ | |
149 | {"Input Mux", "Line", "Line Input"}, | |
150 | {"Input Mux", "Mic", "Mic Bias"}, | |
151 | {"ADC", NULL, "Input Mux"}, | |
152 | ||
153 | /* inputs */ | |
154 | {"Line Input", NULL, "LLINEIN"}, | |
155 | {"Line Input", NULL, "RLINEIN"}, | |
156 | {"Mic Bias", NULL, "MICIN"}, | |
157 | }; | |
158 | ||
159 | static int ssm2602_add_widgets(struct snd_soc_codec *codec) | |
160 | { | |
ce6120cc | 161 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
b7138212 | 162 | |
ce6120cc LG |
163 | snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets, |
164 | ARRAY_SIZE(ssm2602_dapm_widgets)); | |
165 | snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn)); | |
b7138212 | 166 | |
b7138212 CC |
167 | return 0; |
168 | } | |
169 | ||
170 | struct _coeff_div { | |
171 | u32 mclk; | |
172 | u32 rate; | |
173 | u16 fs; | |
174 | u8 sr:4; | |
175 | u8 bosr:1; | |
176 | u8 usb:1; | |
177 | }; | |
178 | ||
179 | /* codec mclk clock divider coefficients */ | |
180 | static const struct _coeff_div coeff_div[] = { | |
181 | /* 48k */ | |
182 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, | |
183 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, | |
184 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, | |
185 | ||
186 | /* 32k */ | |
187 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, | |
188 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, | |
189 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, | |
190 | ||
191 | /* 8k */ | |
192 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, | |
193 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, | |
194 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, | |
195 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, | |
196 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, | |
197 | ||
198 | /* 96k */ | |
199 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, | |
200 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, | |
201 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, | |
202 | ||
203 | /* 44.1k */ | |
204 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, | |
205 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, | |
206 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, | |
207 | ||
208 | /* 88.2k */ | |
209 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, | |
210 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, | |
211 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, | |
212 | }; | |
213 | ||
214 | static inline int get_coeff(int mclk, int rate) | |
215 | { | |
216 | int i; | |
217 | ||
218 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { | |
219 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) | |
220 | return i; | |
221 | } | |
222 | return i; | |
223 | } | |
224 | ||
225 | static int ssm2602_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
226 | struct snd_pcm_hw_params *params, |
227 | struct snd_soc_dai *dai) | |
b7138212 CC |
228 | { |
229 | u16 srate; | |
230 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 231 | struct snd_soc_codec *codec = rtd->codec; |
b2c812e2 | 232 | struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec); |
93547e89 | 233 | u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3; |
b7138212 CC |
234 | int i = get_coeff(ssm2602->sysclk, params_rate(params)); |
235 | ||
faab5a32 | 236 | if (substream == ssm2602->slave_substream) { |
93547e89 | 237 | dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n"); |
faab5a32 KB |
238 | return 0; |
239 | } | |
240 | ||
b7138212 CC |
241 | /*no match is found*/ |
242 | if (i == ARRAY_SIZE(coeff_div)) | |
243 | return -EINVAL; | |
244 | ||
245 | srate = (coeff_div[i].sr << 2) | | |
246 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; | |
247 | ||
93547e89 CC |
248 | snd_soc_write(codec, SSM2602_ACTIVE, 0); |
249 | snd_soc_write(codec, SSM2602_SRATE, srate); | |
b7138212 CC |
250 | |
251 | /* bit size */ | |
252 | switch (params_format(params)) { | |
253 | case SNDRV_PCM_FORMAT_S16_LE: | |
254 | break; | |
255 | case SNDRV_PCM_FORMAT_S20_3LE: | |
256 | iface |= 0x0004; | |
257 | break; | |
258 | case SNDRV_PCM_FORMAT_S24_LE: | |
259 | iface |= 0x0008; | |
260 | break; | |
261 | case SNDRV_PCM_FORMAT_S32_LE: | |
262 | iface |= 0x000c; | |
263 | break; | |
264 | } | |
93547e89 CC |
265 | snd_soc_write(codec, SSM2602_IFACE, iface); |
266 | snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC); | |
b7138212 CC |
267 | return 0; |
268 | } | |
269 | ||
dee89c4d MB |
270 | static int ssm2602_startup(struct snd_pcm_substream *substream, |
271 | struct snd_soc_dai *dai) | |
b7138212 CC |
272 | { |
273 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 274 | struct snd_soc_codec *codec = rtd->codec; |
b2c812e2 | 275 | struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec); |
faab5a32 | 276 | struct i2c_client *i2c = codec->control_data; |
b7138212 CC |
277 | struct snd_pcm_runtime *master_runtime; |
278 | ||
279 | /* The DAI has shared clocks so if we already have a playback or | |
280 | * capture going then constrain this substream to match it. | |
faab5a32 | 281 | * TODO: the ssm2602 allows pairs of non-matching PB/REC rates |
b7138212 CC |
282 | */ |
283 | if (ssm2602->master_substream) { | |
284 | master_runtime = ssm2602->master_substream->runtime; | |
faab5a32 KB |
285 | dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n", |
286 | master_runtime->sample_bits, | |
287 | master_runtime->rate); | |
288 | ||
f692fce0 CC |
289 | if (master_runtime->rate != 0) |
290 | snd_pcm_hw_constraint_minmax(substream->runtime, | |
291 | SNDRV_PCM_HW_PARAM_RATE, | |
292 | master_runtime->rate, | |
293 | master_runtime->rate); | |
294 | ||
295 | if (master_runtime->sample_bits != 0) | |
296 | snd_pcm_hw_constraint_minmax(substream->runtime, | |
297 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
298 | master_runtime->sample_bits, | |
299 | master_runtime->sample_bits); | |
b7138212 CC |
300 | |
301 | ssm2602->slave_substream = substream; | |
302 | } else | |
303 | ssm2602->master_substream = substream; | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
dee89c4d MB |
308 | static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream, |
309 | struct snd_soc_dai *dai) | |
b7138212 CC |
310 | { |
311 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 312 | struct snd_soc_codec *codec = rtd->codec; |
b7138212 | 313 | /* set active */ |
93547e89 | 314 | snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC); |
b7138212 CC |
315 | |
316 | return 0; | |
317 | } | |
318 | ||
dee89c4d MB |
319 | static void ssm2602_shutdown(struct snd_pcm_substream *substream, |
320 | struct snd_soc_dai *dai) | |
b7138212 CC |
321 | { |
322 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 323 | struct snd_soc_codec *codec = rtd->codec; |
b2c812e2 | 324 | struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec); |
f692fce0 | 325 | |
b7138212 CC |
326 | /* deactivate */ |
327 | if (!codec->active) | |
93547e89 | 328 | snd_soc_write(codec, SSM2602_ACTIVE, 0); |
faab5a32 KB |
329 | |
330 | if (ssm2602->master_substream == substream) | |
331 | ssm2602->master_substream = ssm2602->slave_substream; | |
332 | ||
333 | ssm2602->slave_substream = NULL; | |
b7138212 CC |
334 | } |
335 | ||
336 | static int ssm2602_mute(struct snd_soc_dai *dai, int mute) | |
337 | { | |
338 | struct snd_soc_codec *codec = dai->codec; | |
93547e89 | 339 | u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE; |
b7138212 | 340 | if (mute) |
93547e89 | 341 | snd_soc_write(codec, SSM2602_APDIGI, |
b7138212 CC |
342 | mute_reg | APDIGI_ENABLE_DAC_MUTE); |
343 | else | |
93547e89 | 344 | snd_soc_write(codec, SSM2602_APDIGI, mute_reg); |
b7138212 CC |
345 | return 0; |
346 | } | |
347 | ||
348 | static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai, | |
349 | int clk_id, unsigned int freq, int dir) | |
350 | { | |
351 | struct snd_soc_codec *codec = codec_dai->codec; | |
b2c812e2 | 352 | struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec); |
b7138212 CC |
353 | switch (freq) { |
354 | case 11289600: | |
355 | case 12000000: | |
356 | case 12288000: | |
357 | case 16934400: | |
358 | case 18432000: | |
359 | ssm2602->sysclk = freq; | |
360 | return 0; | |
361 | } | |
362 | return -EINVAL; | |
363 | } | |
364 | ||
365 | static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
366 | unsigned int fmt) | |
367 | { | |
368 | struct snd_soc_codec *codec = codec_dai->codec; | |
369 | u16 iface = 0; | |
370 | ||
371 | /* set master/slave audio interface */ | |
372 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
373 | case SND_SOC_DAIFMT_CBM_CFM: | |
374 | iface |= 0x0040; | |
375 | break; | |
376 | case SND_SOC_DAIFMT_CBS_CFS: | |
377 | break; | |
378 | default: | |
379 | return -EINVAL; | |
380 | } | |
381 | ||
382 | /* interface format */ | |
383 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
384 | case SND_SOC_DAIFMT_I2S: | |
385 | iface |= 0x0002; | |
386 | break; | |
387 | case SND_SOC_DAIFMT_RIGHT_J: | |
388 | break; | |
389 | case SND_SOC_DAIFMT_LEFT_J: | |
390 | iface |= 0x0001; | |
391 | break; | |
392 | case SND_SOC_DAIFMT_DSP_A: | |
c6913485 | 393 | iface |= 0x0013; |
b7138212 CC |
394 | break; |
395 | case SND_SOC_DAIFMT_DSP_B: | |
c6913485 | 396 | iface |= 0x0003; |
b7138212 CC |
397 | break; |
398 | default: | |
399 | return -EINVAL; | |
400 | } | |
401 | ||
402 | /* clock inversion */ | |
403 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
404 | case SND_SOC_DAIFMT_NB_NF: | |
405 | break; | |
406 | case SND_SOC_DAIFMT_IB_IF: | |
407 | iface |= 0x0090; | |
408 | break; | |
409 | case SND_SOC_DAIFMT_IB_NF: | |
410 | iface |= 0x0080; | |
411 | break; | |
412 | case SND_SOC_DAIFMT_NB_IF: | |
413 | iface |= 0x0010; | |
414 | break; | |
415 | default: | |
416 | return -EINVAL; | |
417 | } | |
418 | ||
419 | /* set iface */ | |
93547e89 | 420 | snd_soc_write(codec, SSM2602_IFACE, iface); |
b7138212 CC |
421 | return 0; |
422 | } | |
423 | ||
424 | static int ssm2602_set_bias_level(struct snd_soc_codec *codec, | |
425 | enum snd_soc_bias_level level) | |
426 | { | |
93547e89 | 427 | u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f; |
b7138212 CC |
428 | |
429 | switch (level) { | |
430 | case SND_SOC_BIAS_ON: | |
431 | /* vref/mid, osc on, dac unmute */ | |
93547e89 | 432 | snd_soc_write(codec, SSM2602_PWR, reg); |
b7138212 CC |
433 | break; |
434 | case SND_SOC_BIAS_PREPARE: | |
435 | break; | |
436 | case SND_SOC_BIAS_STANDBY: | |
437 | /* everything off except vref/vmid, */ | |
93547e89 | 438 | snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN); |
b7138212 CC |
439 | break; |
440 | case SND_SOC_BIAS_OFF: | |
441 | /* everything off, dac mute, inactive */ | |
93547e89 CC |
442 | snd_soc_write(codec, SSM2602_ACTIVE, 0); |
443 | snd_soc_write(codec, SSM2602_PWR, 0xffff); | |
b7138212 CC |
444 | break; |
445 | ||
446 | } | |
ce6120cc | 447 | codec->dapm.bias_level = level; |
b7138212 CC |
448 | return 0; |
449 | } | |
450 | ||
2552a710 CC |
451 | #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\ |
452 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ | |
453 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) | |
b7138212 | 454 | |
5de27b6c KB |
455 | #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
456 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) | |
457 | ||
6335d055 EM |
458 | static struct snd_soc_dai_ops ssm2602_dai_ops = { |
459 | .startup = ssm2602_startup, | |
460 | .prepare = ssm2602_pcm_prepare, | |
461 | .hw_params = ssm2602_hw_params, | |
462 | .shutdown = ssm2602_shutdown, | |
463 | .digital_mute = ssm2602_mute, | |
464 | .set_sysclk = ssm2602_set_dai_sysclk, | |
465 | .set_fmt = ssm2602_set_dai_fmt, | |
466 | }; | |
467 | ||
f0fba2ad LG |
468 | static struct snd_soc_dai_driver ssm2602_dai = { |
469 | .name = "ssm2602-hifi", | |
b7138212 CC |
470 | .playback = { |
471 | .stream_name = "Playback", | |
472 | .channels_min = 2, | |
473 | .channels_max = 2, | |
474 | .rates = SSM2602_RATES, | |
5de27b6c | 475 | .formats = SSM2602_FORMATS,}, |
b7138212 CC |
476 | .capture = { |
477 | .stream_name = "Capture", | |
478 | .channels_min = 2, | |
479 | .channels_max = 2, | |
480 | .rates = SSM2602_RATES, | |
5de27b6c | 481 | .formats = SSM2602_FORMATS,}, |
6335d055 | 482 | .ops = &ssm2602_dai_ops, |
b7138212 | 483 | }; |
b7138212 | 484 | |
f0fba2ad | 485 | static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state) |
b7138212 | 486 | { |
b7138212 CC |
487 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF); |
488 | return 0; | |
489 | } | |
490 | ||
f0fba2ad | 491 | static int ssm2602_resume(struct snd_soc_codec *codec) |
b7138212 | 492 | { |
93547e89 CC |
493 | snd_soc_cache_sync(codec); |
494 | ||
b7138212 | 495 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
93547e89 | 496 | |
b7138212 CC |
497 | return 0; |
498 | } | |
499 | ||
f0fba2ad | 500 | static int ssm2602_probe(struct snd_soc_codec *codec) |
b7138212 | 501 | { |
f0fba2ad LG |
502 | struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec); |
503 | int ret = 0, reg; | |
504 | ||
505 | pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION); | |
506 | ||
93547e89 CC |
507 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type); |
508 | if (ret < 0) { | |
509 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); | |
510 | return ret; | |
511 | } | |
b7138212 | 512 | |
93547e89 CC |
513 | ret = ssm2602_reset(codec); |
514 | if (ret < 0) { | |
515 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); | |
516 | return ret; | |
517 | } | |
b7138212 | 518 | |
b7138212 | 519 | /*power on device*/ |
93547e89 | 520 | snd_soc_write(codec, SSM2602_ACTIVE, 0); |
b7138212 | 521 | /* set the update bits */ |
93547e89 CC |
522 | reg = snd_soc_read(codec, SSM2602_LINVOL); |
523 | snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH); | |
524 | reg = snd_soc_read(codec, SSM2602_RINVOL); | |
525 | snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH); | |
526 | reg = snd_soc_read(codec, SSM2602_LOUT1V); | |
527 | snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH); | |
528 | reg = snd_soc_read(codec, SSM2602_ROUT1V); | |
529 | snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH); | |
b7138212 | 530 | /*select Line in as default input*/ |
93547e89 | 531 | snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC | |
b7138212 | 532 | APANA_ENABLE_MIC_BOOST); |
93547e89 | 533 | snd_soc_write(codec, SSM2602_PWR, 0); |
b7138212 | 534 | |
3e8e1952 IM |
535 | snd_soc_add_controls(codec, ssm2602_snd_controls, |
536 | ARRAY_SIZE(ssm2602_snd_controls)); | |
b7138212 | 537 | ssm2602_add_widgets(codec); |
b7138212 | 538 | |
93547e89 | 539 | return 0; |
f0fba2ad | 540 | } |
b7138212 | 541 | |
f0fba2ad LG |
542 | /* remove everything here */ |
543 | static int ssm2602_remove(struct snd_soc_codec *codec) | |
544 | { | |
545 | ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF); | |
546 | return 0; | |
b7138212 CC |
547 | } |
548 | ||
f0fba2ad LG |
549 | static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = { |
550 | .probe = ssm2602_probe, | |
551 | .remove = ssm2602_remove, | |
552 | .suspend = ssm2602_suspend, | |
553 | .resume = ssm2602_resume, | |
f0fba2ad LG |
554 | .set_bias_level = ssm2602_set_bias_level, |
555 | .reg_cache_size = sizeof(ssm2602_reg), | |
556 | .reg_word_size = sizeof(u16), | |
557 | .reg_cache_default = ssm2602_reg, | |
558 | }; | |
b7138212 | 559 | |
b39e2855 MF |
560 | #if defined(CONFIG_SPI_MASTER) |
561 | static int __devinit ssm2602_spi_probe(struct spi_device *spi) | |
562 | { | |
563 | struct ssm2602_priv *ssm2602; | |
564 | int ret; | |
565 | ||
566 | ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL); | |
567 | if (ssm2602 == NULL) | |
568 | return -ENOMEM; | |
569 | ||
570 | spi_set_drvdata(spi, ssm2602); | |
571 | ssm2602->control_type = SND_SOC_SPI; | |
572 | ||
573 | ret = snd_soc_register_codec(&spi->dev, | |
574 | &soc_codec_dev_ssm2602, &ssm2602_dai, 1); | |
575 | if (ret < 0) | |
576 | kfree(ssm2602); | |
577 | return ret; | |
578 | } | |
579 | ||
580 | static int __devexit ssm2602_spi_remove(struct spi_device *spi) | |
581 | { | |
582 | snd_soc_unregister_codec(&spi->dev); | |
583 | kfree(spi_get_drvdata(spi)); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | static struct spi_driver ssm2602_spi_driver = { | |
588 | .driver = { | |
589 | .name = "ssm2602", | |
590 | .owner = THIS_MODULE, | |
591 | }, | |
592 | .probe = ssm2602_spi_probe, | |
593 | .remove = __devexit_p(ssm2602_spi_remove), | |
594 | }; | |
595 | #endif | |
596 | ||
b7138212 CC |
597 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
598 | /* | |
599 | * ssm2602 2 wire address is determined by GPIO5 | |
600 | * state during powerup. | |
601 | * low = 0x1a | |
602 | * high = 0x1b | |
603 | */ | |
604 | static int ssm2602_i2c_probe(struct i2c_client *i2c, | |
605 | const struct i2c_device_id *id) | |
606 | { | |
f0fba2ad | 607 | struct ssm2602_priv *ssm2602; |
b7138212 CC |
608 | int ret; |
609 | ||
f0fba2ad LG |
610 | ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL); |
611 | if (ssm2602 == NULL) | |
612 | return -ENOMEM; | |
b7138212 | 613 | |
f0fba2ad | 614 | i2c_set_clientdata(i2c, ssm2602); |
f0fba2ad | 615 | ssm2602->control_type = SND_SOC_I2C; |
b7138212 | 616 | |
f0fba2ad LG |
617 | ret = snd_soc_register_codec(&i2c->dev, |
618 | &soc_codec_dev_ssm2602, &ssm2602_dai, 1); | |
619 | if (ret < 0) | |
620 | kfree(ssm2602); | |
b7138212 CC |
621 | return ret; |
622 | } | |
623 | ||
624 | static int ssm2602_i2c_remove(struct i2c_client *client) | |
625 | { | |
f0fba2ad LG |
626 | snd_soc_unregister_codec(&client->dev); |
627 | kfree(i2c_get_clientdata(client)); | |
b7138212 CC |
628 | return 0; |
629 | } | |
630 | ||
631 | static const struct i2c_device_id ssm2602_i2c_id[] = { | |
632 | { "ssm2602", 0 }, | |
633 | { } | |
634 | }; | |
635 | MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id); | |
f0fba2ad | 636 | |
b7138212 CC |
637 | /* corgi i2c codec control layer */ |
638 | static struct i2c_driver ssm2602_i2c_driver = { | |
639 | .driver = { | |
2a161018 | 640 | .name = "ssm2602", |
b7138212 CC |
641 | .owner = THIS_MODULE, |
642 | }, | |
643 | .probe = ssm2602_i2c_probe, | |
644 | .remove = ssm2602_i2c_remove, | |
645 | .id_table = ssm2602_i2c_id, | |
646 | }; | |
b7138212 CC |
647 | #endif |
648 | ||
f0fba2ad LG |
649 | |
650 | static int __init ssm2602_modinit(void) | |
b7138212 | 651 | { |
b7138212 | 652 | int ret = 0; |
b39e2855 MF |
653 | |
654 | #if defined(CONFIG_SPI_MASTER) | |
655 | ret = spi_register_driver(&ssm2602_spi_driver); | |
656 | if (ret) | |
657 | return ret; | |
658 | #endif | |
659 | ||
b7138212 | 660 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
f0fba2ad | 661 | ret = i2c_add_driver(&ssm2602_i2c_driver); |
b39e2855 MF |
662 | if (ret) |
663 | return ret; | |
b7138212 | 664 | #endif |
b39e2855 | 665 | |
b7138212 CC |
666 | return ret; |
667 | } | |
f0fba2ad | 668 | module_init(ssm2602_modinit); |
b7138212 | 669 | |
f0fba2ad | 670 | static void __exit ssm2602_exit(void) |
b7138212 | 671 | { |
b39e2855 MF |
672 | #if defined(CONFIG_SPI_MASTER) |
673 | spi_unregister_driver(&ssm2602_spi_driver); | |
674 | #endif | |
675 | ||
b7138212 | 676 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
b7138212 CC |
677 | i2c_del_driver(&ssm2602_i2c_driver); |
678 | #endif | |
64089b84 MB |
679 | } |
680 | module_exit(ssm2602_exit); | |
681 | ||
b7138212 CC |
682 | MODULE_DESCRIPTION("ASoC ssm2602 driver"); |
683 | MODULE_AUTHOR("Cliff Cai"); | |
684 | MODULE_LICENSE("GPL"); |