blob: d8287219eeab179234a97f49efd9165b0e95ec21 [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>
Mike Frysingerb39e2852011-04-07 02:05:11 -040035#include <linux/spi/spi.h>
Cliff Caib7138212008-09-05 18:09:57 +080036#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Cliff Caib7138212008-09-05 18:09:57 +080038#include <sound/core.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Cliff Caib7138212008-09-05 18:09:57 +080042#include <sound/initval.h>
43
44#include "ssm2602.h"
45
Cliff Caib7138212008-09-05 18:09:57 +080046#define SSM2602_VERSION "0.1"
47
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070048enum ssm2602_type {
49 SSM2602,
50 SSM2604,
51};
52
Cliff Caib7138212008-09-05 18:09:57 +080053/* codec private data */
54struct ssm2602_priv {
55 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000056 enum snd_soc_control_type control_type;
Cliff Caib7138212008-09-05 18:09:57 +080057 struct snd_pcm_substream *master_substream;
58 struct snd_pcm_substream *slave_substream;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070059
60 enum ssm2602_type type;
Cliff Caib7138212008-09-05 18:09:57 +080061};
62
63/*
64 * ssm2602 register cache
65 * We can't read the ssm2602 register space when we are
66 * using 2 wire for device control, so we cache them instead.
67 * There is no point in caching the reset register
68 */
69static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
Lars-Peter Clausen7164bdb2011-05-08 09:24:41 -070070 0x0097, 0x0097, 0x0079, 0x0079,
71 0x000a, 0x0008, 0x009f, 0x000a,
Cliff Caib7138212008-09-05 18:09:57 +080072 0x0000, 0x0000
73};
74
Cliff Cai93547e82011-03-27 17:22:57 -040075#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080076
77/*Appending several "None"s just for OSS mixer use*/
78static const char *ssm2602_input_select[] = {
79 "Line", "Mic", "None", "None", "None",
80 "None", "None", "None",
81};
82
83static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
84
85static const struct soc_enum ssm2602_enum[] = {
86 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
87 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
88};
89
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070090static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +080091SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
92SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
93
Cliff Caib7138212008-09-05 18:09:57 +080094SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
95SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
96
Cliff Caib7138212008-09-05 18:09:57 +080097SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
98};
99
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700100static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
101SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
102 0, 127, 0),
103SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
104 7, 1, 0),
105
106SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
107
108SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
109SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
110SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
111};
112
Cliff Caib7138212008-09-05 18:09:57 +0800113/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700114static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800115SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800116SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700117SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800118};
119
120/* Input mux */
121static const struct snd_kcontrol_new ssm2602_input_mux_controls =
122SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
123
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700124static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800125SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800126SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800127SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700128
129SND_SOC_DAPM_OUTPUT("LOUT"),
130SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800131SND_SOC_DAPM_INPUT("RLINEIN"),
132SND_SOC_DAPM_INPUT("LLINEIN"),
133};
134
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700135static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
136SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
137 ssm260x_output_mixer_controls,
138 ARRAY_SIZE(ssm260x_output_mixer_controls)),
139
140SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
141SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
142
143SND_SOC_DAPM_OUTPUT("LHPOUT"),
144SND_SOC_DAPM_OUTPUT("RHPOUT"),
145SND_SOC_DAPM_INPUT("MICIN"),
146};
147
148static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
149SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
150 ssm260x_output_mixer_controls,
151 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
152};
153
154static const struct snd_soc_dapm_route ssm260x_routes[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800155 {"Output Mixer", "Line Bypass Switch", "Line Input"},
156 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800157
Cliff Caib7138212008-09-05 18:09:57 +0800158 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800159 {"LOUT", NULL, "Output Mixer"},
160
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700161 {"Line Input", NULL, "LLINEIN"},
162 {"Line Input", NULL, "RLINEIN"},
163};
164
165static const struct snd_soc_dapm_route ssm2602_routes[] = {
166 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
167
168 {"RHPOUT", NULL, "Output Mixer"},
169 {"LHPOUT", NULL, "Output Mixer"},
170
Cliff Caib7138212008-09-05 18:09:57 +0800171 {"Input Mux", "Line", "Line Input"},
172 {"Input Mux", "Mic", "Mic Bias"},
173 {"ADC", NULL, "Input Mux"},
174
Cliff Caib7138212008-09-05 18:09:57 +0800175 {"Mic Bias", NULL, "MICIN"},
176};
177
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700178static const struct snd_soc_dapm_route ssm2604_routes[] = {
179 {"ADC", NULL, "Line Input"},
180};
Cliff Caib7138212008-09-05 18:09:57 +0800181
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200182struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800183 u32 mclk;
184 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200185 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800186};
187
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200188#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
189
190/* codec mclk clock coefficients */
191static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800192 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200193 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
194 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
195 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800196
197 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200198 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
199 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
200 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800201
202 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200203 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
204 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
205 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
206 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
207 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800208
209 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200210 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
211 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
212 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800213
214 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200215 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
216 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
217 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800218
219 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200220 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
221 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
222 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800223};
224
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200225static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800226{
227 int i;
228
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200229 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
230 if (ssm2602_coeff_table[i].rate == rate &&
231 ssm2602_coeff_table[i].mclk == mclk)
232 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800233 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200234 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800235}
236
237static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000238 struct snd_pcm_hw_params *params,
239 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800240{
Cliff Caib7138212008-09-05 18:09:57 +0800241 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900243 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400244 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200245 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Cliff Caib7138212008-09-05 18:09:57 +0800246
Karl Beldanfaab5a32008-11-20 15:39:27 +0100247 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400248 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100249 return 0;
250 }
251
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200252 if (srate < 0)
253 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800254
Cliff Cai93547e82011-03-27 17:22:57 -0400255 snd_soc_write(codec, SSM2602_ACTIVE, 0);
256 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800257
258 /* bit size */
259 switch (params_format(params)) {
260 case SNDRV_PCM_FORMAT_S16_LE:
261 break;
262 case SNDRV_PCM_FORMAT_S20_3LE:
263 iface |= 0x0004;
264 break;
265 case SNDRV_PCM_FORMAT_S24_LE:
266 iface |= 0x0008;
267 break;
268 case SNDRV_PCM_FORMAT_S32_LE:
269 iface |= 0x000c;
270 break;
271 }
Cliff Cai93547e82011-03-27 17:22:57 -0400272 snd_soc_write(codec, SSM2602_IFACE, iface);
273 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800274 return 0;
275}
276
Mark Browndee89c42008-11-18 22:11:38 +0000277static int ssm2602_startup(struct snd_pcm_substream *substream,
278 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800279{
280 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900282 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100283 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800284 struct snd_pcm_runtime *master_runtime;
285
286 /* The DAI has shared clocks so if we already have a playback or
287 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100288 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800289 */
290 if (ssm2602->master_substream) {
291 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100292 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
293 master_runtime->sample_bits,
294 master_runtime->rate);
295
Cliff Caif692fce2009-06-02 00:18:54 -0400296 if (master_runtime->rate != 0)
297 snd_pcm_hw_constraint_minmax(substream->runtime,
298 SNDRV_PCM_HW_PARAM_RATE,
299 master_runtime->rate,
300 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800301
Cliff Caif692fce2009-06-02 00:18:54 -0400302 if (master_runtime->sample_bits != 0)
303 snd_pcm_hw_constraint_minmax(substream->runtime,
304 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
305 master_runtime->sample_bits,
306 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800307
308 ssm2602->slave_substream = substream;
309 } else
310 ssm2602->master_substream = substream;
311
312 return 0;
313}
314
Mark Browndee89c42008-11-18 22:11:38 +0000315static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
316 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800317{
318 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000319 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800320 /* set active */
Cliff Cai93547e82011-03-27 17:22:57 -0400321 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800322
323 return 0;
324}
325
Mark Browndee89c42008-11-18 22:11:38 +0000326static void ssm2602_shutdown(struct snd_pcm_substream *substream,
327 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800328{
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000330 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900331 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400332
Cliff Caib7138212008-09-05 18:09:57 +0800333 /* deactivate */
334 if (!codec->active)
Cliff Cai93547e82011-03-27 17:22:57 -0400335 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100336
337 if (ssm2602->master_substream == substream)
338 ssm2602->master_substream = ssm2602->slave_substream;
339
340 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800341}
342
343static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
344{
345 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400346 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800347 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400348 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800349 mute_reg | APDIGI_ENABLE_DAC_MUTE);
350 else
Cliff Cai93547e82011-03-27 17:22:57 -0400351 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800352 return 0;
353}
354
355static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
356 int clk_id, unsigned int freq, int dir)
357{
358 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900359 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800360 switch (freq) {
361 case 11289600:
362 case 12000000:
363 case 12288000:
364 case 16934400:
365 case 18432000:
366 ssm2602->sysclk = freq;
367 return 0;
368 }
369 return -EINVAL;
370}
371
372static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
373 unsigned int fmt)
374{
375 struct snd_soc_codec *codec = codec_dai->codec;
376 u16 iface = 0;
377
378 /* set master/slave audio interface */
379 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
380 case SND_SOC_DAIFMT_CBM_CFM:
381 iface |= 0x0040;
382 break;
383 case SND_SOC_DAIFMT_CBS_CFS:
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 /* interface format */
390 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
391 case SND_SOC_DAIFMT_I2S:
392 iface |= 0x0002;
393 break;
394 case SND_SOC_DAIFMT_RIGHT_J:
395 break;
396 case SND_SOC_DAIFMT_LEFT_J:
397 iface |= 0x0001;
398 break;
399 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200400 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800401 break;
402 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200403 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800404 break;
405 default:
406 return -EINVAL;
407 }
408
409 /* clock inversion */
410 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
411 case SND_SOC_DAIFMT_NB_NF:
412 break;
413 case SND_SOC_DAIFMT_IB_IF:
414 iface |= 0x0090;
415 break;
416 case SND_SOC_DAIFMT_IB_NF:
417 iface |= 0x0080;
418 break;
419 case SND_SOC_DAIFMT_NB_IF:
420 iface |= 0x0010;
421 break;
422 default:
423 return -EINVAL;
424 }
425
426 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400427 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800428 return 0;
429}
430
431static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
432 enum snd_soc_bias_level level)
433{
Cliff Cai93547e82011-03-27 17:22:57 -0400434 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800435
436 switch (level) {
437 case SND_SOC_BIAS_ON:
438 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400439 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800440 break;
441 case SND_SOC_BIAS_PREPARE:
442 break;
443 case SND_SOC_BIAS_STANDBY:
444 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400445 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800446 break;
447 case SND_SOC_BIAS_OFF:
448 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400449 snd_soc_write(codec, SSM2602_ACTIVE, 0);
450 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800451 break;
452
453 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200454 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800455 return 0;
456}
457
Cliff Cai2552a712009-06-02 00:18:53 -0400458#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
459 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
460 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800461
Karl Beldan5de27b62008-11-20 15:39:31 +0100462#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
463 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
464
Eric Miao6335d052009-03-03 09:41:00 +0800465static struct snd_soc_dai_ops ssm2602_dai_ops = {
466 .startup = ssm2602_startup,
467 .prepare = ssm2602_pcm_prepare,
468 .hw_params = ssm2602_hw_params,
469 .shutdown = ssm2602_shutdown,
470 .digital_mute = ssm2602_mute,
471 .set_sysclk = ssm2602_set_dai_sysclk,
472 .set_fmt = ssm2602_set_dai_fmt,
473};
474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475static struct snd_soc_dai_driver ssm2602_dai = {
476 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800477 .playback = {
478 .stream_name = "Playback",
479 .channels_min = 2,
480 .channels_max = 2,
481 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100482 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800483 .capture = {
484 .stream_name = "Capture",
485 .channels_min = 2,
486 .channels_max = 2,
487 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100488 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800489 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800490};
Cliff Caib7138212008-09-05 18:09:57 +0800491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800493{
Cliff Caib7138212008-09-05 18:09:57 +0800494 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
495 return 0;
496}
497
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800499{
Cliff Cai93547e82011-03-27 17:22:57 -0400500 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800501
Cliff Caib7138212008-09-05 18:09:57 +0800502 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400503
Cliff Caib7138212008-09-05 18:09:57 +0800504 return 0;
505}
506
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000507static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800508{
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700509 struct snd_soc_dapm_context *dapm = &codec->dapm;
510 int ret, reg;
511
512 reg = snd_soc_read(codec, SSM2602_LOUT1V);
513 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
514 reg = snd_soc_read(codec, SSM2602_ROUT1V);
515 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
516
517 ret = snd_soc_add_controls(codec, ssm2602_snd_controls,
518 ARRAY_SIZE(ssm2602_snd_controls));
519 if (ret)
520 return ret;
521
522 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
523 ARRAY_SIZE(ssm2602_dapm_widgets));
524 if (ret)
525 return ret;
526
527 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
528 ARRAY_SIZE(ssm2602_routes));
529}
530
531static int ssm2604_probe(struct snd_soc_codec *codec)
532{
533 struct snd_soc_dapm_context *dapm = &codec->dapm;
534 int ret;
535
536 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
537 ARRAY_SIZE(ssm2604_dapm_widgets));
538 if (ret)
539 return ret;
540
541 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
542 ARRAY_SIZE(ssm2604_routes));
543}
544
545static int ssm260x_probe(struct snd_soc_codec *codec)
546{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700548 int ret, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
551
Cliff Cai93547e82011-03-27 17:22:57 -0400552 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
553 if (ret < 0) {
554 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
555 return ret;
556 }
Cliff Caib7138212008-09-05 18:09:57 +0800557
Cliff Cai93547e82011-03-27 17:22:57 -0400558 ret = ssm2602_reset(codec);
559 if (ret < 0) {
560 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
561 return ret;
562 }
Cliff Caib7138212008-09-05 18:09:57 +0800563
Cliff Caib7138212008-09-05 18:09:57 +0800564 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400565 reg = snd_soc_read(codec, SSM2602_LINVOL);
566 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
567 reg = snd_soc_read(codec, SSM2602_RINVOL);
568 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800569 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400570 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800571 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800572
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700573 switch (ssm2602->type) {
574 case SSM2602:
575 ret = ssm2602_probe(codec);
576 break;
577 case SSM2604:
578 ret = ssm2604_probe(codec);
579 break;
580 }
Cliff Caib7138212008-09-05 18:09:57 +0800581
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700582 return ret;
Cliff Caib7138212008-09-05 18:09:57 +0800583}
584
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585/* remove everything here */
586static int ssm2602_remove(struct snd_soc_codec *codec)
587{
588 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
589 return 0;
590}
591
592static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700593 .probe = ssm260x_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 .remove = ssm2602_remove,
595 .suspend = ssm2602_suspend,
596 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen8fc63fe2011-05-05 16:59:14 +0200598 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 .reg_word_size = sizeof(u16),
600 .reg_cache_default = ssm2602_reg,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700601
602 .controls = ssm260x_snd_controls,
603 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
604 .dapm_widgets = ssm260x_dapm_widgets,
605 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
606 .dapm_routes = ssm260x_routes,
607 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608};
Cliff Caib7138212008-09-05 18:09:57 +0800609
Mike Frysingerb39e2852011-04-07 02:05:11 -0400610#if defined(CONFIG_SPI_MASTER)
611static int __devinit ssm2602_spi_probe(struct spi_device *spi)
612{
613 struct ssm2602_priv *ssm2602;
614 int ret;
615
616 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
617 if (ssm2602 == NULL)
618 return -ENOMEM;
619
620 spi_set_drvdata(spi, ssm2602);
621 ssm2602->control_type = SND_SOC_SPI;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700622 ssm2602->type = SSM2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400623
624 ret = snd_soc_register_codec(&spi->dev,
625 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
626 if (ret < 0)
627 kfree(ssm2602);
628 return ret;
629}
630
631static int __devexit ssm2602_spi_remove(struct spi_device *spi)
632{
633 snd_soc_unregister_codec(&spi->dev);
634 kfree(spi_get_drvdata(spi));
635 return 0;
636}
637
638static struct spi_driver ssm2602_spi_driver = {
639 .driver = {
640 .name = "ssm2602",
641 .owner = THIS_MODULE,
642 },
643 .probe = ssm2602_spi_probe,
644 .remove = __devexit_p(ssm2602_spi_remove),
645};
646#endif
647
Cliff Caib7138212008-09-05 18:09:57 +0800648#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
649/*
650 * ssm2602 2 wire address is determined by GPIO5
651 * state during powerup.
652 * low = 0x1a
653 * high = 0x1b
654 */
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200655static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
Cliff Caib7138212008-09-05 18:09:57 +0800656 const struct i2c_device_id *id)
657{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800659 int ret;
660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
662 if (ssm2602 == NULL)
663 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 ssm2602->control_type = SND_SOC_I2C;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700667 ssm2602->type = id->driver_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668
669 ret = snd_soc_register_codec(&i2c->dev,
670 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800671 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800673 return ret;
674}
675
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200676static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
Cliff Caib7138212008-09-05 18:09:57 +0800677{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 snd_soc_unregister_codec(&client->dev);
679 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800680 return 0;
681}
682
683static const struct i2c_device_id ssm2602_i2c_id[] = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700684 { "ssm2602", SSM2602 },
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700685 { "ssm2603", SSM2602 },
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700686 { "ssm2604", SSM2604 },
Cliff Caib7138212008-09-05 18:09:57 +0800687 { }
688};
689MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690
Cliff Caib7138212008-09-05 18:09:57 +0800691/* corgi i2c codec control layer */
692static struct i2c_driver ssm2602_i2c_driver = {
693 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400694 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800695 .owner = THIS_MODULE,
696 },
697 .probe = ssm2602_i2c_probe,
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200698 .remove = __devexit_p(ssm2602_i2c_remove),
Cliff Caib7138212008-09-05 18:09:57 +0800699 .id_table = ssm2602_i2c_id,
700};
Cliff Caib7138212008-09-05 18:09:57 +0800701#endif
702
Cliff Caib7138212008-09-05 18:09:57 +0800703
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100704static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000705{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400707
708#if defined(CONFIG_SPI_MASTER)
709 ret = spi_register_driver(&ssm2602_spi_driver);
710 if (ret)
711 return ret;
712#endif
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
715 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400716 if (ret)
717 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000721}
722module_init(ssm2602_modinit);
723
724static void __exit ssm2602_exit(void)
725{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400726#if defined(CONFIG_SPI_MASTER)
727 spi_unregister_driver(&ssm2602_spi_driver);
728#endif
729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
731 i2c_del_driver(&ssm2602_i2c_driver);
732#endif
Mark Brown64089b82008-12-08 19:17:58 +0000733}
734module_exit(ssm2602_exit);
735
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700736MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800737MODULE_AUTHOR("Cliff Cai");
738MODULE_LICENSE("GPL");