blob: adbc3e8dafc88de20d2f851fa0626892e24e82d9 [file] [log] [blame]
Cliff Caib7138212008-09-05 18:09:57 +08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Cliff Caib7138212008-09-05 18:09:57 +080037#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/soc.h>
41#include <sound/soc-dapm.h>
42#include <sound/initval.h>
43
44#include "ssm2602.h"
45
Cliff Caib7138212008-09-05 18:09:57 +080046#define SSM2602_VERSION "0.1"
47
Cliff Caib7138212008-09-05 18:09:57 +080048/* codec private data */
49struct ssm2602_priv {
50 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051 enum snd_soc_control_type control_type;
52 void *control_data;
Cliff Caib7138212008-09-05 18:09:57 +080053 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 */
63static 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 */
72static 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 */
86static 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 */
98static 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*/
120static const char *ssm2602_input_select[] = {
121 "Line", "Mic", "None", "None", "None",
122 "None", "None", "None",
123};
124
125static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
126
127static 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
132static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
133
134SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
135 0, 127, 0),
136SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
137 7, 1, 0),
138
139SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
140SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
141
142SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
Cliff Cai4b527e22010-03-09 12:58:09 -0500143SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 7, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800144SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
145
146SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
147
148SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
149SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
150
151SOC_ENUM("Capture Source", ssm2602_enum[0]),
152
153SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
154};
155
Cliff Caib7138212008-09-05 18:09:57 +0800156/* Output Mixer */
157static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
158SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
159SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
160SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
161};
162
163/* Input mux */
164static const struct snd_kcontrol_new ssm2602_input_mux_controls =
165SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
166
167static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
168SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
169 &ssm2602_output_mixer_controls[0],
170 ARRAY_SIZE(ssm2602_output_mixer_controls)),
171SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
172SND_SOC_DAPM_OUTPUT("LOUT"),
173SND_SOC_DAPM_OUTPUT("LHPOUT"),
174SND_SOC_DAPM_OUTPUT("ROUT"),
175SND_SOC_DAPM_OUTPUT("RHPOUT"),
176SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
177SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
178SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
179SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
180SND_SOC_DAPM_INPUT("MICIN"),
181SND_SOC_DAPM_INPUT("RLINEIN"),
182SND_SOC_DAPM_INPUT("LLINEIN"),
183};
184
185static const struct snd_soc_dapm_route audio_conn[] = {
186 /* output mixer */
187 {"Output Mixer", "Line Bypass Switch", "Line Input"},
188 {"Output Mixer", "HiFi Playback Switch", "DAC"},
189 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
190
191 /* outputs */
192 {"RHPOUT", NULL, "Output Mixer"},
193 {"ROUT", NULL, "Output Mixer"},
194 {"LHPOUT", NULL, "Output Mixer"},
195 {"LOUT", NULL, "Output Mixer"},
196
197 /* input mux */
198 {"Input Mux", "Line", "Line Input"},
199 {"Input Mux", "Mic", "Mic Bias"},
200 {"ADC", NULL, "Input Mux"},
201
202 /* inputs */
203 {"Line Input", NULL, "LLINEIN"},
204 {"Line Input", NULL, "RLINEIN"},
205 {"Mic Bias", NULL, "MICIN"},
206};
207
208static int ssm2602_add_widgets(struct snd_soc_codec *codec)
209{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200210 struct snd_soc_dapm_context *dapm = &codec->dapm;
Cliff Caib7138212008-09-05 18:09:57 +0800211
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200212 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
213 ARRAY_SIZE(ssm2602_dapm_widgets));
214 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
Cliff Caib7138212008-09-05 18:09:57 +0800215
Cliff Caib7138212008-09-05 18:09:57 +0800216 return 0;
217}
218
219struct _coeff_div {
220 u32 mclk;
221 u32 rate;
222 u16 fs;
223 u8 sr:4;
224 u8 bosr:1;
225 u8 usb:1;
226};
227
228/* codec mclk clock divider coefficients */
229static const struct _coeff_div coeff_div[] = {
230 /* 48k */
231 {12288000, 48000, 256, 0x0, 0x0, 0x0},
232 {18432000, 48000, 384, 0x0, 0x1, 0x0},
233 {12000000, 48000, 250, 0x0, 0x0, 0x1},
234
235 /* 32k */
236 {12288000, 32000, 384, 0x6, 0x0, 0x0},
237 {18432000, 32000, 576, 0x6, 0x1, 0x0},
238 {12000000, 32000, 375, 0x6, 0x0, 0x1},
239
240 /* 8k */
241 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
242 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
243 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
244 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
245 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
246
247 /* 96k */
248 {12288000, 96000, 128, 0x7, 0x0, 0x0},
249 {18432000, 96000, 192, 0x7, 0x1, 0x0},
250 {12000000, 96000, 125, 0x7, 0x0, 0x1},
251
252 /* 44.1k */
253 {11289600, 44100, 256, 0x8, 0x0, 0x0},
254 {16934400, 44100, 384, 0x8, 0x1, 0x0},
255 {12000000, 44100, 272, 0x8, 0x1, 0x1},
256
257 /* 88.2k */
258 {11289600, 88200, 128, 0xf, 0x0, 0x0},
259 {16934400, 88200, 192, 0xf, 0x1, 0x0},
260 {12000000, 88200, 136, 0xf, 0x1, 0x1},
261};
262
263static inline int get_coeff(int mclk, int rate)
264{
265 int i;
266
267 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
268 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
269 return i;
270 }
271 return i;
272}
273
274static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000275 struct snd_pcm_hw_params *params,
276 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800277{
278 u16 srate;
279 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900281 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100282 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800283 u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
284 int i = get_coeff(ssm2602->sysclk, params_rate(params));
285
Karl Beldanfaab5a32008-11-20 15:39:27 +0100286 if (substream == ssm2602->slave_substream) {
287 dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
288 return 0;
289 }
290
Cliff Caib7138212008-09-05 18:09:57 +0800291 /*no match is found*/
292 if (i == ARRAY_SIZE(coeff_div))
293 return -EINVAL;
294
295 srate = (coeff_div[i].sr << 2) |
296 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
297
298 ssm2602_write(codec, SSM2602_ACTIVE, 0);
299 ssm2602_write(codec, SSM2602_SRATE, srate);
300
301 /* bit size */
302 switch (params_format(params)) {
303 case SNDRV_PCM_FORMAT_S16_LE:
304 break;
305 case SNDRV_PCM_FORMAT_S20_3LE:
306 iface |= 0x0004;
307 break;
308 case SNDRV_PCM_FORMAT_S24_LE:
309 iface |= 0x0008;
310 break;
311 case SNDRV_PCM_FORMAT_S32_LE:
312 iface |= 0x000c;
313 break;
314 }
315 ssm2602_write(codec, SSM2602_IFACE, iface);
316 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
317 return 0;
318}
319
Mark Browndee89c42008-11-18 22:11:38 +0000320static int ssm2602_startup(struct snd_pcm_substream *substream,
321 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800322{
323 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000324 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900325 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100326 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800327 struct snd_pcm_runtime *master_runtime;
328
329 /* The DAI has shared clocks so if we already have a playback or
330 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100331 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800332 */
333 if (ssm2602->master_substream) {
334 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100335 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
336 master_runtime->sample_bits,
337 master_runtime->rate);
338
Cliff Caif692fce2009-06-02 00:18:54 -0400339 if (master_runtime->rate != 0)
340 snd_pcm_hw_constraint_minmax(substream->runtime,
341 SNDRV_PCM_HW_PARAM_RATE,
342 master_runtime->rate,
343 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800344
Cliff Caif692fce2009-06-02 00:18:54 -0400345 if (master_runtime->sample_bits != 0)
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);
Cliff Caib7138212008-09-05 18:09:57 +0800350
351 ssm2602->slave_substream = substream;
352 } else
353 ssm2602->master_substream = substream;
354
355 return 0;
356}
357
Mark Browndee89c42008-11-18 22:11:38 +0000358static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
359 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800360{
361 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800363 /* set active */
364 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
365
366 return 0;
367}
368
Mark Browndee89c42008-11-18 22:11:38 +0000369static void ssm2602_shutdown(struct snd_pcm_substream *substream,
370 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000373 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900374 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400375
Cliff Caib7138212008-09-05 18:09:57 +0800376 /* deactivate */
377 if (!codec->active)
378 ssm2602_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100379
380 if (ssm2602->master_substream == substream)
381 ssm2602->master_substream = ssm2602->slave_substream;
382
383 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800384}
385
386static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
387{
388 struct snd_soc_codec *codec = dai->codec;
389 u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
390 if (mute)
391 ssm2602_write(codec, SSM2602_APDIGI,
392 mute_reg | APDIGI_ENABLE_DAC_MUTE);
393 else
394 ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
395 return 0;
396}
397
398static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
399 int clk_id, unsigned int freq, int dir)
400{
401 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900402 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800403 switch (freq) {
404 case 11289600:
405 case 12000000:
406 case 12288000:
407 case 16934400:
408 case 18432000:
409 ssm2602->sysclk = freq;
410 return 0;
411 }
412 return -EINVAL;
413}
414
415static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
416 unsigned int fmt)
417{
418 struct snd_soc_codec *codec = codec_dai->codec;
419 u16 iface = 0;
420
421 /* set master/slave audio interface */
422 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
423 case SND_SOC_DAIFMT_CBM_CFM:
424 iface |= 0x0040;
425 break;
426 case SND_SOC_DAIFMT_CBS_CFS:
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 /* interface format */
433 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
434 case SND_SOC_DAIFMT_I2S:
435 iface |= 0x0002;
436 break;
437 case SND_SOC_DAIFMT_RIGHT_J:
438 break;
439 case SND_SOC_DAIFMT_LEFT_J:
440 iface |= 0x0001;
441 break;
442 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200443 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800444 break;
445 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200446 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800447 break;
448 default:
449 return -EINVAL;
450 }
451
452 /* clock inversion */
453 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
454 case SND_SOC_DAIFMT_NB_NF:
455 break;
456 case SND_SOC_DAIFMT_IB_IF:
457 iface |= 0x0090;
458 break;
459 case SND_SOC_DAIFMT_IB_NF:
460 iface |= 0x0080;
461 break;
462 case SND_SOC_DAIFMT_NB_IF:
463 iface |= 0x0010;
464 break;
465 default:
466 return -EINVAL;
467 }
468
469 /* set iface */
470 ssm2602_write(codec, SSM2602_IFACE, iface);
471 return 0;
472}
473
474static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
475 enum snd_soc_bias_level level)
476{
477 u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
478
479 switch (level) {
480 case SND_SOC_BIAS_ON:
481 /* vref/mid, osc on, dac unmute */
482 ssm2602_write(codec, SSM2602_PWR, reg);
483 break;
484 case SND_SOC_BIAS_PREPARE:
485 break;
486 case SND_SOC_BIAS_STANDBY:
487 /* everything off except vref/vmid, */
488 ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
489 break;
490 case SND_SOC_BIAS_OFF:
491 /* everything off, dac mute, inactive */
492 ssm2602_write(codec, SSM2602_ACTIVE, 0);
493 ssm2602_write(codec, SSM2602_PWR, 0xffff);
494 break;
495
496 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200497 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800498 return 0;
499}
500
Cliff Cai2552a712009-06-02 00:18:53 -0400501#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
502 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
503 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800504
Karl Beldan5de27b62008-11-20 15:39:31 +0100505#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
506 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
507
Eric Miao6335d052009-03-03 09:41:00 +0800508static struct snd_soc_dai_ops ssm2602_dai_ops = {
509 .startup = ssm2602_startup,
510 .prepare = ssm2602_pcm_prepare,
511 .hw_params = ssm2602_hw_params,
512 .shutdown = ssm2602_shutdown,
513 .digital_mute = ssm2602_mute,
514 .set_sysclk = ssm2602_set_dai_sysclk,
515 .set_fmt = ssm2602_set_dai_fmt,
516};
517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518static struct snd_soc_dai_driver ssm2602_dai = {
519 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800520 .playback = {
521 .stream_name = "Playback",
522 .channels_min = 2,
523 .channels_max = 2,
524 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100525 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800526 .capture = {
527 .stream_name = "Capture",
528 .channels_min = 2,
529 .channels_max = 2,
530 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100531 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800532 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800533};
Cliff Caib7138212008-09-05 18:09:57 +0800534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800536{
Cliff Caib7138212008-09-05 18:09:57 +0800537 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
538 return 0;
539}
540
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800542{
Cliff Caib7138212008-09-05 18:09:57 +0800543 int i;
544 u8 data[2];
545 u16 *cache = codec->reg_cache;
546
547 /* Sync reg_cache with the hardware */
548 for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
549 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
550 data[1] = cache[i] & 0x00ff;
551 codec->hw_write(codec->control_data, data, 2);
552 }
553 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Caib7138212008-09-05 18:09:57 +0800554 return 0;
555}
556
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800558{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
560 int ret = 0, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 codec->control_data = ssm2602->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800565
566 ssm2602_reset(codec);
567
Cliff Caib7138212008-09-05 18:09:57 +0800568 /*power on device*/
569 ssm2602_write(codec, SSM2602_ACTIVE, 0);
570 /* set the update bits */
571 reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
572 ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
573 reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
574 ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
575 reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
576 ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
577 reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
578 ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
579 /*select Line in as default input*/
Cliff Cai4b527e22010-03-09 12:58:09 -0500580 ssm2602_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800581 APANA_ENABLE_MIC_BOOST);
582 ssm2602_write(codec, SSM2602_PWR, 0);
583
Ian Molton3e8e1952009-01-09 00:23:21 +0000584 snd_soc_add_controls(codec, ssm2602_snd_controls,
585 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800586 ssm2602_add_widgets(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800587
588 return ret;
Cliff Caib7138212008-09-05 18:09:57 +0800589}
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591/* remove everything here */
592static int ssm2602_remove(struct snd_soc_codec *codec)
593{
594 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
595 return 0;
596}
597
598static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
599 .probe = ssm2602_probe,
600 .remove = ssm2602_remove,
601 .suspend = ssm2602_suspend,
602 .resume = ssm2602_resume,
603 .read = ssm2602_read_reg_cache,
604 .write = ssm2602_write,
605 .set_bias_level = ssm2602_set_bias_level,
606 .reg_cache_size = sizeof(ssm2602_reg),
607 .reg_word_size = sizeof(u16),
608 .reg_cache_default = ssm2602_reg,
609};
Cliff Caib7138212008-09-05 18:09:57 +0800610
611#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
612/*
613 * ssm2602 2 wire address is determined by GPIO5
614 * state during powerup.
615 * low = 0x1a
616 * high = 0x1b
617 */
618static int ssm2602_i2c_probe(struct i2c_client *i2c,
619 const struct i2c_device_id *id)
620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800622 int ret;
623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
625 if (ssm2602 == NULL)
626 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 i2c_set_clientdata(i2c, ssm2602);
629 ssm2602->control_data = i2c;
630 ssm2602->control_type = SND_SOC_I2C;
631
632 ret = snd_soc_register_codec(&i2c->dev,
633 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800634 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800636 return ret;
637}
638
639static int ssm2602_i2c_remove(struct i2c_client *client)
640{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 snd_soc_unregister_codec(&client->dev);
642 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800643 return 0;
644}
645
646static const struct i2c_device_id ssm2602_i2c_id[] = {
647 { "ssm2602", 0 },
648 { }
649};
650MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651
Cliff Caib7138212008-09-05 18:09:57 +0800652/* corgi i2c codec control layer */
653static struct i2c_driver ssm2602_i2c_driver = {
654 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 .name = "ssm2602-codec",
Cliff Caib7138212008-09-05 18:09:57 +0800656 .owner = THIS_MODULE,
657 },
658 .probe = ssm2602_i2c_probe,
659 .remove = ssm2602_i2c_remove,
660 .id_table = ssm2602_i2c_id,
661};
Cliff Caib7138212008-09-05 18:09:57 +0800662#endif
663
Cliff Caib7138212008-09-05 18:09:57 +0800664
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100665static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000666{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 int ret = 0;
668#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
669 ret = i2c_add_driver(&ssm2602_i2c_driver);
670 if (ret != 0) {
671 printk(KERN_ERR "Failed to register SSM2602 I2C driver: %d\n",
672 ret);
673 }
674#endif
675 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000676}
677module_init(ssm2602_modinit);
678
679static void __exit ssm2602_exit(void)
680{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
682 i2c_del_driver(&ssm2602_i2c_driver);
683#endif
Mark Brown64089b82008-12-08 19:17:58 +0000684}
685module_exit(ssm2602_exit);
686
Cliff Caib7138212008-09-05 18:09:57 +0800687MODULE_DESCRIPTION("ASoC ssm2602 driver");
688MODULE_AUTHOR("Cliff Cai");
689MODULE_LICENSE("GPL");