blob: 8a2b52fa4a7ea0ab4bbbc61030dc0ffd2a250335 [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>
Cliff Caib7138212008-09-05 18:09:57 +080041#include <sound/initval.h>
42
43#include "ssm2602.h"
44
Cliff Caib7138212008-09-05 18:09:57 +080045#define SSM2602_VERSION "0.1"
46
Cliff Caib7138212008-09-05 18:09:57 +080047/* codec private data */
48struct ssm2602_priv {
49 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000050 enum snd_soc_control_type control_type;
Cliff Caib7138212008-09-05 18:09:57 +080051 struct snd_pcm_substream *master_substream;
52 struct snd_pcm_substream *slave_substream;
53};
54
55/*
56 * ssm2602 register cache
57 * We can't read the ssm2602 register space when we are
58 * using 2 wire for device control, so we cache them instead.
59 * There is no point in caching the reset register
60 */
61static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
62 0x0017, 0x0017, 0x0079, 0x0079,
63 0x0000, 0x0000, 0x0000, 0x000a,
64 0x0000, 0x0000
65};
66
Cliff Cai93547e82011-03-27 17:22:57 -040067#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080068
69/*Appending several "None"s just for OSS mixer use*/
70static const char *ssm2602_input_select[] = {
71 "Line", "Mic", "None", "None", "None",
72 "None", "None", "None",
73};
74
75static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
76
77static const struct soc_enum ssm2602_enum[] = {
78 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
79 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
80};
81
82static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
83
84SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
85 0, 127, 0),
86SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
87 7, 1, 0),
88
89SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
90SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
91
92SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
Cliff Cai4b527e22010-03-09 12:58:09 -050093SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 7, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +080094SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
95
96SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
97
98SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
99SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
100
101SOC_ENUM("Capture Source", ssm2602_enum[0]),
102
103SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
104};
105
Cliff Caib7138212008-09-05 18:09:57 +0800106/* Output Mixer */
107static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
108SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
109SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
110SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
111};
112
113/* Input mux */
114static const struct snd_kcontrol_new ssm2602_input_mux_controls =
115SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
116
117static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
118SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
119 &ssm2602_output_mixer_controls[0],
120 ARRAY_SIZE(ssm2602_output_mixer_controls)),
121SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
122SND_SOC_DAPM_OUTPUT("LOUT"),
123SND_SOC_DAPM_OUTPUT("LHPOUT"),
124SND_SOC_DAPM_OUTPUT("ROUT"),
125SND_SOC_DAPM_OUTPUT("RHPOUT"),
126SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
127SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
128SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
129SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
130SND_SOC_DAPM_INPUT("MICIN"),
131SND_SOC_DAPM_INPUT("RLINEIN"),
132SND_SOC_DAPM_INPUT("LLINEIN"),
133};
134
135static const struct snd_soc_dapm_route audio_conn[] = {
136 /* output mixer */
137 {"Output Mixer", "Line Bypass Switch", "Line Input"},
138 {"Output Mixer", "HiFi Playback Switch", "DAC"},
139 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
140
141 /* outputs */
142 {"RHPOUT", NULL, "Output Mixer"},
143 {"ROUT", NULL, "Output Mixer"},
144 {"LHPOUT", NULL, "Output Mixer"},
145 {"LOUT", NULL, "Output Mixer"},
146
147 /* input mux */
148 {"Input Mux", "Line", "Line Input"},
149 {"Input Mux", "Mic", "Mic Bias"},
150 {"ADC", NULL, "Input Mux"},
151
152 /* inputs */
153 {"Line Input", NULL, "LLINEIN"},
154 {"Line Input", NULL, "RLINEIN"},
155 {"Mic Bias", NULL, "MICIN"},
156};
157
158static int ssm2602_add_widgets(struct snd_soc_codec *codec)
159{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200160 struct snd_soc_dapm_context *dapm = &codec->dapm;
Cliff Caib7138212008-09-05 18:09:57 +0800161
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200162 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
163 ARRAY_SIZE(ssm2602_dapm_widgets));
164 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
Cliff Caib7138212008-09-05 18:09:57 +0800165
Cliff Caib7138212008-09-05 18:09:57 +0800166 return 0;
167}
168
169struct _coeff_div {
170 u32 mclk;
171 u32 rate;
172 u16 fs;
173 u8 sr:4;
174 u8 bosr:1;
175 u8 usb:1;
176};
177
178/* codec mclk clock divider coefficients */
179static const struct _coeff_div coeff_div[] = {
180 /* 48k */
181 {12288000, 48000, 256, 0x0, 0x0, 0x0},
182 {18432000, 48000, 384, 0x0, 0x1, 0x0},
183 {12000000, 48000, 250, 0x0, 0x0, 0x1},
184
185 /* 32k */
186 {12288000, 32000, 384, 0x6, 0x0, 0x0},
187 {18432000, 32000, 576, 0x6, 0x1, 0x0},
188 {12000000, 32000, 375, 0x6, 0x0, 0x1},
189
190 /* 8k */
191 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
192 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
193 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
194 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
195 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
196
197 /* 96k */
198 {12288000, 96000, 128, 0x7, 0x0, 0x0},
199 {18432000, 96000, 192, 0x7, 0x1, 0x0},
200 {12000000, 96000, 125, 0x7, 0x0, 0x1},
201
202 /* 44.1k */
203 {11289600, 44100, 256, 0x8, 0x0, 0x0},
204 {16934400, 44100, 384, 0x8, 0x1, 0x0},
205 {12000000, 44100, 272, 0x8, 0x1, 0x1},
206
207 /* 88.2k */
208 {11289600, 88200, 128, 0xf, 0x0, 0x0},
209 {16934400, 88200, 192, 0xf, 0x1, 0x0},
210 {12000000, 88200, 136, 0xf, 0x1, 0x1},
211};
212
213static inline int get_coeff(int mclk, int rate)
214{
215 int i;
216
217 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
218 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
219 return i;
220 }
221 return i;
222}
223
224static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000225 struct snd_pcm_hw_params *params,
226 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800227{
228 u16 srate;
229 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000230 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900231 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400232 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Cliff Caib7138212008-09-05 18:09:57 +0800233 int i = get_coeff(ssm2602->sysclk, params_rate(params));
234
Karl Beldanfaab5a32008-11-20 15:39:27 +0100235 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400236 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100237 return 0;
238 }
239
Cliff Caib7138212008-09-05 18:09:57 +0800240 /*no match is found*/
241 if (i == ARRAY_SIZE(coeff_div))
242 return -EINVAL;
243
244 srate = (coeff_div[i].sr << 2) |
245 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
246
Cliff Cai93547e82011-03-27 17:22:57 -0400247 snd_soc_write(codec, SSM2602_ACTIVE, 0);
248 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800249
250 /* bit size */
251 switch (params_format(params)) {
252 case SNDRV_PCM_FORMAT_S16_LE:
253 break;
254 case SNDRV_PCM_FORMAT_S20_3LE:
255 iface |= 0x0004;
256 break;
257 case SNDRV_PCM_FORMAT_S24_LE:
258 iface |= 0x0008;
259 break;
260 case SNDRV_PCM_FORMAT_S32_LE:
261 iface |= 0x000c;
262 break;
263 }
Cliff Cai93547e82011-03-27 17:22:57 -0400264 snd_soc_write(codec, SSM2602_IFACE, iface);
265 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800266 return 0;
267}
268
Mark Browndee89c42008-11-18 22:11:38 +0000269static int ssm2602_startup(struct snd_pcm_substream *substream,
270 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800271{
272 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000273 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900274 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100275 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800276 struct snd_pcm_runtime *master_runtime;
277
278 /* The DAI has shared clocks so if we already have a playback or
279 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100280 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800281 */
282 if (ssm2602->master_substream) {
283 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100284 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
285 master_runtime->sample_bits,
286 master_runtime->rate);
287
Cliff Caif692fce2009-06-02 00:18:54 -0400288 if (master_runtime->rate != 0)
289 snd_pcm_hw_constraint_minmax(substream->runtime,
290 SNDRV_PCM_HW_PARAM_RATE,
291 master_runtime->rate,
292 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800293
Cliff Caif692fce2009-06-02 00:18:54 -0400294 if (master_runtime->sample_bits != 0)
295 snd_pcm_hw_constraint_minmax(substream->runtime,
296 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
297 master_runtime->sample_bits,
298 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800299
300 ssm2602->slave_substream = substream;
301 } else
302 ssm2602->master_substream = substream;
303
304 return 0;
305}
306
Mark Browndee89c42008-11-18 22:11:38 +0000307static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
308 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800309{
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800312 /* set active */
Cliff Cai93547e82011-03-27 17:22:57 -0400313 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800314
315 return 0;
316}
317
Mark Browndee89c42008-11-18 22:11:38 +0000318static void ssm2602_shutdown(struct snd_pcm_substream *substream,
319 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800320{
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000322 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900323 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400324
Cliff Caib7138212008-09-05 18:09:57 +0800325 /* deactivate */
326 if (!codec->active)
Cliff Cai93547e82011-03-27 17:22:57 -0400327 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100328
329 if (ssm2602->master_substream == substream)
330 ssm2602->master_substream = ssm2602->slave_substream;
331
332 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800333}
334
335static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
336{
337 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400338 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800339 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400340 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800341 mute_reg | APDIGI_ENABLE_DAC_MUTE);
342 else
Cliff Cai93547e82011-03-27 17:22:57 -0400343 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800344 return 0;
345}
346
347static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
348 int clk_id, unsigned int freq, int dir)
349{
350 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900351 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800352 switch (freq) {
353 case 11289600:
354 case 12000000:
355 case 12288000:
356 case 16934400:
357 case 18432000:
358 ssm2602->sysclk = freq;
359 return 0;
360 }
361 return -EINVAL;
362}
363
364static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
365 unsigned int fmt)
366{
367 struct snd_soc_codec *codec = codec_dai->codec;
368 u16 iface = 0;
369
370 /* set master/slave audio interface */
371 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
372 case SND_SOC_DAIFMT_CBM_CFM:
373 iface |= 0x0040;
374 break;
375 case SND_SOC_DAIFMT_CBS_CFS:
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 /* interface format */
382 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
383 case SND_SOC_DAIFMT_I2S:
384 iface |= 0x0002;
385 break;
386 case SND_SOC_DAIFMT_RIGHT_J:
387 break;
388 case SND_SOC_DAIFMT_LEFT_J:
389 iface |= 0x0001;
390 break;
391 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200392 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800393 break;
394 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200395 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800396 break;
397 default:
398 return -EINVAL;
399 }
400
401 /* clock inversion */
402 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
403 case SND_SOC_DAIFMT_NB_NF:
404 break;
405 case SND_SOC_DAIFMT_IB_IF:
406 iface |= 0x0090;
407 break;
408 case SND_SOC_DAIFMT_IB_NF:
409 iface |= 0x0080;
410 break;
411 case SND_SOC_DAIFMT_NB_IF:
412 iface |= 0x0010;
413 break;
414 default:
415 return -EINVAL;
416 }
417
418 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400419 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800420 return 0;
421}
422
423static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
424 enum snd_soc_bias_level level)
425{
Cliff Cai93547e82011-03-27 17:22:57 -0400426 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800427
428 switch (level) {
429 case SND_SOC_BIAS_ON:
430 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400431 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800432 break;
433 case SND_SOC_BIAS_PREPARE:
434 break;
435 case SND_SOC_BIAS_STANDBY:
436 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400437 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800438 break;
439 case SND_SOC_BIAS_OFF:
440 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400441 snd_soc_write(codec, SSM2602_ACTIVE, 0);
442 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800443 break;
444
445 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200446 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800447 return 0;
448}
449
Cliff Cai2552a712009-06-02 00:18:53 -0400450#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
451 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
452 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800453
Karl Beldan5de27b62008-11-20 15:39:31 +0100454#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
455 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
456
Eric Miao6335d052009-03-03 09:41:00 +0800457static struct snd_soc_dai_ops ssm2602_dai_ops = {
458 .startup = ssm2602_startup,
459 .prepare = ssm2602_pcm_prepare,
460 .hw_params = ssm2602_hw_params,
461 .shutdown = ssm2602_shutdown,
462 .digital_mute = ssm2602_mute,
463 .set_sysclk = ssm2602_set_dai_sysclk,
464 .set_fmt = ssm2602_set_dai_fmt,
465};
466
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000467static struct snd_soc_dai_driver ssm2602_dai = {
468 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800469 .playback = {
470 .stream_name = "Playback",
471 .channels_min = 2,
472 .channels_max = 2,
473 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100474 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800475 .capture = {
476 .stream_name = "Capture",
477 .channels_min = 2,
478 .channels_max = 2,
479 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100480 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800481 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800482};
Cliff Caib7138212008-09-05 18:09:57 +0800483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800485{
Cliff Caib7138212008-09-05 18:09:57 +0800486 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
487 return 0;
488}
489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800491{
Cliff Cai93547e82011-03-27 17:22:57 -0400492 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800493
Cliff Caib7138212008-09-05 18:09:57 +0800494 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400495
Cliff Caib7138212008-09-05 18:09:57 +0800496 return 0;
497}
498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800500{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
502 int ret = 0, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800503
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
505
Cliff Cai93547e82011-03-27 17:22:57 -0400506 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
507 if (ret < 0) {
508 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
509 return ret;
510 }
Cliff Caib7138212008-09-05 18:09:57 +0800511
Cliff Cai93547e82011-03-27 17:22:57 -0400512 ret = ssm2602_reset(codec);
513 if (ret < 0) {
514 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
515 return ret;
516 }
Cliff Caib7138212008-09-05 18:09:57 +0800517
Cliff Caib7138212008-09-05 18:09:57 +0800518 /*power on device*/
Cliff Cai93547e82011-03-27 17:22:57 -0400519 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800520 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400521 reg = snd_soc_read(codec, SSM2602_LINVOL);
522 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
523 reg = snd_soc_read(codec, SSM2602_RINVOL);
524 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
525 reg = snd_soc_read(codec, SSM2602_LOUT1V);
526 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
527 reg = snd_soc_read(codec, SSM2602_ROUT1V);
528 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800529 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400530 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800531 APANA_ENABLE_MIC_BOOST);
Cliff Cai93547e82011-03-27 17:22:57 -0400532 snd_soc_write(codec, SSM2602_PWR, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800533
Ian Molton3e8e1952009-01-09 00:23:21 +0000534 snd_soc_add_controls(codec, ssm2602_snd_controls,
535 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800536 ssm2602_add_widgets(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800537
Cliff Cai93547e82011-03-27 17:22:57 -0400538 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800539}
540
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541/* remove everything here */
542static int ssm2602_remove(struct snd_soc_codec *codec)
543{
544 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
545 return 0;
546}
547
548static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
549 .probe = ssm2602_probe,
550 .remove = ssm2602_remove,
551 .suspend = ssm2602_suspend,
552 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 .set_bias_level = ssm2602_set_bias_level,
554 .reg_cache_size = sizeof(ssm2602_reg),
555 .reg_word_size = sizeof(u16),
556 .reg_cache_default = ssm2602_reg,
557};
Cliff Caib7138212008-09-05 18:09:57 +0800558
559#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
560/*
561 * ssm2602 2 wire address is determined by GPIO5
562 * state during powerup.
563 * low = 0x1a
564 * high = 0x1b
565 */
566static int ssm2602_i2c_probe(struct i2c_client *i2c,
567 const struct i2c_device_id *id)
568{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800570 int ret;
571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
573 if (ssm2602 == NULL)
574 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 ssm2602->control_type = SND_SOC_I2C;
578
579 ret = snd_soc_register_codec(&i2c->dev,
580 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800581 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800583 return ret;
584}
585
586static int ssm2602_i2c_remove(struct i2c_client *client)
587{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 snd_soc_unregister_codec(&client->dev);
589 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800590 return 0;
591}
592
593static const struct i2c_device_id ssm2602_i2c_id[] = {
594 { "ssm2602", 0 },
595 { }
596};
597MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598
Cliff Caib7138212008-09-05 18:09:57 +0800599/* corgi i2c codec control layer */
600static struct i2c_driver ssm2602_i2c_driver = {
601 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400602 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800603 .owner = THIS_MODULE,
604 },
605 .probe = ssm2602_i2c_probe,
606 .remove = ssm2602_i2c_remove,
607 .id_table = ssm2602_i2c_id,
608};
Cliff Caib7138212008-09-05 18:09:57 +0800609#endif
610
Cliff Caib7138212008-09-05 18:09:57 +0800611
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100612static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000613{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 int ret = 0;
615#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
616 ret = i2c_add_driver(&ssm2602_i2c_driver);
617 if (ret != 0) {
618 printk(KERN_ERR "Failed to register SSM2602 I2C driver: %d\n",
619 ret);
620 }
621#endif
622 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000623}
624module_init(ssm2602_modinit);
625
626static void __exit ssm2602_exit(void)
627{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
629 i2c_del_driver(&ssm2602_i2c_driver);
630#endif
Mark Brown64089b82008-12-08 19:17:58 +0000631}
632module_exit(ssm2602_exit);
633
Cliff Caib7138212008-09-05 18:09:57 +0800634MODULE_DESCRIPTION("ASoC ssm2602 driver");
635MODULE_AUTHOR("Cliff Cai");
636MODULE_LICENSE("GPL");