blob: a09d66c0e70e7f84f9f8efab664b437424913d90 [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
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;
Cliff Caib7138212008-09-05 18:09:57 +080052 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 */
62static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
Lars-Peter Clausen7164bdb2011-05-08 09:24:41 -070063 0x0097, 0x0097, 0x0079, 0x0079,
64 0x000a, 0x0008, 0x009f, 0x000a,
Cliff Caib7138212008-09-05 18:09:57 +080065 0x0000, 0x0000
66};
67
Cliff Cai93547e82011-03-27 17:22:57 -040068#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080069
70/*Appending several "None"s just for OSS mixer use*/
71static const char *ssm2602_input_select[] = {
72 "Line", "Mic", "None", "None", "None",
73 "None", "None", "None",
74};
75
76static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
77
78static 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
83static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
84
85SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
86 0, 127, 0),
87SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
88 7, 1, 0),
89
90SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
91SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
92
93SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
Lars-Peter Clausen36c90ab2011-05-05 16:59:16 +020094SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +080095SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
96
97SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
98
99SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
100SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
101
Cliff Caib7138212008-09-05 18:09:57 +0800102SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
103};
104
Cliff Caib7138212008-09-05 18:09:57 +0800105/* Output Mixer */
106static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
107SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
108SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
109SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
110};
111
112/* Input mux */
113static const struct snd_kcontrol_new ssm2602_input_mux_controls =
114SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
115
116static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
117SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
118 &ssm2602_output_mixer_controls[0],
119 ARRAY_SIZE(ssm2602_output_mixer_controls)),
120SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
121SND_SOC_DAPM_OUTPUT("LOUT"),
122SND_SOC_DAPM_OUTPUT("LHPOUT"),
123SND_SOC_DAPM_OUTPUT("ROUT"),
124SND_SOC_DAPM_OUTPUT("RHPOUT"),
125SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
126SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
127SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
128SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
129SND_SOC_DAPM_INPUT("MICIN"),
130SND_SOC_DAPM_INPUT("RLINEIN"),
131SND_SOC_DAPM_INPUT("LLINEIN"),
132};
133
134static const struct snd_soc_dapm_route audio_conn[] = {
135 /* output mixer */
136 {"Output Mixer", "Line Bypass Switch", "Line Input"},
137 {"Output Mixer", "HiFi Playback Switch", "DAC"},
138 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
139
140 /* outputs */
141 {"RHPOUT", NULL, "Output Mixer"},
142 {"ROUT", NULL, "Output Mixer"},
143 {"LHPOUT", NULL, "Output Mixer"},
144 {"LOUT", NULL, "Output Mixer"},
145
146 /* input mux */
147 {"Input Mux", "Line", "Line Input"},
148 {"Input Mux", "Mic", "Mic Bias"},
149 {"ADC", NULL, "Input Mux"},
150
151 /* inputs */
152 {"Line Input", NULL, "LLINEIN"},
153 {"Line Input", NULL, "RLINEIN"},
154 {"Mic Bias", NULL, "MICIN"},
155};
156
157static int ssm2602_add_widgets(struct snd_soc_codec *codec)
158{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200159 struct snd_soc_dapm_context *dapm = &codec->dapm;
Cliff Caib7138212008-09-05 18:09:57 +0800160
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
162 ARRAY_SIZE(ssm2602_dapm_widgets));
163 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
Cliff Caib7138212008-09-05 18:09:57 +0800164
Cliff Caib7138212008-09-05 18:09:57 +0800165 return 0;
166}
167
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200168struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800169 u32 mclk;
170 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200171 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800172};
173
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200174#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
175
176/* codec mclk clock coefficients */
177static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800178 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200179 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
180 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
181 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800182
183 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200184 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
185 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
186 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800187
188 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200189 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
190 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
191 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
192 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
193 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800194
195 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200196 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
197 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
198 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800199
200 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200201 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
202 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
203 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800204
205 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200206 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
207 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
208 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800209};
210
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200211static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800212{
213 int i;
214
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200215 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
216 if (ssm2602_coeff_table[i].rate == rate &&
217 ssm2602_coeff_table[i].mclk == mclk)
218 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800219 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200220 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800221}
222
223static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000224 struct snd_pcm_hw_params *params,
225 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800226{
Cliff Caib7138212008-09-05 18:09:57 +0800227 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000228 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900229 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400230 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200231 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Cliff Caib7138212008-09-05 18:09:57 +0800232
Karl Beldanfaab5a32008-11-20 15:39:27 +0100233 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400234 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100235 return 0;
236 }
237
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200238 if (srate < 0)
239 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800240
Cliff Cai93547e82011-03-27 17:22:57 -0400241 snd_soc_write(codec, SSM2602_ACTIVE, 0);
242 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800243
244 /* bit size */
245 switch (params_format(params)) {
246 case SNDRV_PCM_FORMAT_S16_LE:
247 break;
248 case SNDRV_PCM_FORMAT_S20_3LE:
249 iface |= 0x0004;
250 break;
251 case SNDRV_PCM_FORMAT_S24_LE:
252 iface |= 0x0008;
253 break;
254 case SNDRV_PCM_FORMAT_S32_LE:
255 iface |= 0x000c;
256 break;
257 }
Cliff Cai93547e82011-03-27 17:22:57 -0400258 snd_soc_write(codec, SSM2602_IFACE, iface);
259 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800260 return 0;
261}
262
Mark Browndee89c42008-11-18 22:11:38 +0000263static int ssm2602_startup(struct snd_pcm_substream *substream,
264 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800265{
266 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000267 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900268 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100269 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800270 struct snd_pcm_runtime *master_runtime;
271
272 /* The DAI has shared clocks so if we already have a playback or
273 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100274 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800275 */
276 if (ssm2602->master_substream) {
277 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100278 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
279 master_runtime->sample_bits,
280 master_runtime->rate);
281
Cliff Caif692fce2009-06-02 00:18:54 -0400282 if (master_runtime->rate != 0)
283 snd_pcm_hw_constraint_minmax(substream->runtime,
284 SNDRV_PCM_HW_PARAM_RATE,
285 master_runtime->rate,
286 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800287
Cliff Caif692fce2009-06-02 00:18:54 -0400288 if (master_runtime->sample_bits != 0)
289 snd_pcm_hw_constraint_minmax(substream->runtime,
290 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
291 master_runtime->sample_bits,
292 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800293
294 ssm2602->slave_substream = substream;
295 } else
296 ssm2602->master_substream = substream;
297
298 return 0;
299}
300
Mark Browndee89c42008-11-18 22:11:38 +0000301static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
302 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800303{
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000305 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800306 /* set active */
Cliff Cai93547e82011-03-27 17:22:57 -0400307 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800308
309 return 0;
310}
311
Mark Browndee89c42008-11-18 22:11:38 +0000312static void ssm2602_shutdown(struct snd_pcm_substream *substream,
313 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800314{
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000316 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900317 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400318
Cliff Caib7138212008-09-05 18:09:57 +0800319 /* deactivate */
320 if (!codec->active)
Cliff Cai93547e82011-03-27 17:22:57 -0400321 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100322
323 if (ssm2602->master_substream == substream)
324 ssm2602->master_substream = ssm2602->slave_substream;
325
326 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800327}
328
329static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
330{
331 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400332 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800333 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400334 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800335 mute_reg | APDIGI_ENABLE_DAC_MUTE);
336 else
Cliff Cai93547e82011-03-27 17:22:57 -0400337 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800338 return 0;
339}
340
341static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
342 int clk_id, unsigned int freq, int dir)
343{
344 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900345 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800346 switch (freq) {
347 case 11289600:
348 case 12000000:
349 case 12288000:
350 case 16934400:
351 case 18432000:
352 ssm2602->sysclk = freq;
353 return 0;
354 }
355 return -EINVAL;
356}
357
358static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
359 unsigned int fmt)
360{
361 struct snd_soc_codec *codec = codec_dai->codec;
362 u16 iface = 0;
363
364 /* set master/slave audio interface */
365 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
366 case SND_SOC_DAIFMT_CBM_CFM:
367 iface |= 0x0040;
368 break;
369 case SND_SOC_DAIFMT_CBS_CFS:
370 break;
371 default:
372 return -EINVAL;
373 }
374
375 /* interface format */
376 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
377 case SND_SOC_DAIFMT_I2S:
378 iface |= 0x0002;
379 break;
380 case SND_SOC_DAIFMT_RIGHT_J:
381 break;
382 case SND_SOC_DAIFMT_LEFT_J:
383 iface |= 0x0001;
384 break;
385 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200386 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800387 break;
388 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200389 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800390 break;
391 default:
392 return -EINVAL;
393 }
394
395 /* clock inversion */
396 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
397 case SND_SOC_DAIFMT_NB_NF:
398 break;
399 case SND_SOC_DAIFMT_IB_IF:
400 iface |= 0x0090;
401 break;
402 case SND_SOC_DAIFMT_IB_NF:
403 iface |= 0x0080;
404 break;
405 case SND_SOC_DAIFMT_NB_IF:
406 iface |= 0x0010;
407 break;
408 default:
409 return -EINVAL;
410 }
411
412 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400413 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800414 return 0;
415}
416
417static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
418 enum snd_soc_bias_level level)
419{
Cliff Cai93547e82011-03-27 17:22:57 -0400420 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800421
422 switch (level) {
423 case SND_SOC_BIAS_ON:
424 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400425 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800426 break;
427 case SND_SOC_BIAS_PREPARE:
428 break;
429 case SND_SOC_BIAS_STANDBY:
430 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400431 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800432 break;
433 case SND_SOC_BIAS_OFF:
434 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400435 snd_soc_write(codec, SSM2602_ACTIVE, 0);
436 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800437 break;
438
439 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200440 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800441 return 0;
442}
443
Cliff Cai2552a712009-06-02 00:18:53 -0400444#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
445 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
446 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800447
Karl Beldan5de27b62008-11-20 15:39:31 +0100448#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
449 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
450
Eric Miao6335d052009-03-03 09:41:00 +0800451static struct snd_soc_dai_ops ssm2602_dai_ops = {
452 .startup = ssm2602_startup,
453 .prepare = ssm2602_pcm_prepare,
454 .hw_params = ssm2602_hw_params,
455 .shutdown = ssm2602_shutdown,
456 .digital_mute = ssm2602_mute,
457 .set_sysclk = ssm2602_set_dai_sysclk,
458 .set_fmt = ssm2602_set_dai_fmt,
459};
460
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461static struct snd_soc_dai_driver ssm2602_dai = {
462 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800463 .playback = {
464 .stream_name = "Playback",
465 .channels_min = 2,
466 .channels_max = 2,
467 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100468 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800469 .capture = {
470 .stream_name = "Capture",
471 .channels_min = 2,
472 .channels_max = 2,
473 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100474 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800475 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800476};
Cliff Caib7138212008-09-05 18:09:57 +0800477
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800479{
Cliff Caib7138212008-09-05 18:09:57 +0800480 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
481 return 0;
482}
483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800485{
Cliff Cai93547e82011-03-27 17:22:57 -0400486 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800487
Cliff Caib7138212008-09-05 18:09:57 +0800488 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400489
Cliff Caib7138212008-09-05 18:09:57 +0800490 return 0;
491}
492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800494{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
496 int ret = 0, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800497
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
499
Cliff Cai93547e82011-03-27 17:22:57 -0400500 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
501 if (ret < 0) {
502 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
503 return ret;
504 }
Cliff Caib7138212008-09-05 18:09:57 +0800505
Cliff Cai93547e82011-03-27 17:22:57 -0400506 ret = ssm2602_reset(codec);
507 if (ret < 0) {
508 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
509 return ret;
510 }
Cliff Caib7138212008-09-05 18:09:57 +0800511
Cliff Caib7138212008-09-05 18:09:57 +0800512 /*power on device*/
Cliff Cai93547e82011-03-27 17:22:57 -0400513 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800514 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400515 reg = snd_soc_read(codec, SSM2602_LINVOL);
516 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
517 reg = snd_soc_read(codec, SSM2602_RINVOL);
518 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
519 reg = snd_soc_read(codec, SSM2602_LOUT1V);
520 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
521 reg = snd_soc_read(codec, SSM2602_ROUT1V);
522 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800523 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400524 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800525 APANA_ENABLE_MIC_BOOST);
Cliff Cai93547e82011-03-27 17:22:57 -0400526 snd_soc_write(codec, SSM2602_PWR, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800527
Ian Molton3e8e1952009-01-09 00:23:21 +0000528 snd_soc_add_controls(codec, ssm2602_snd_controls,
529 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800530 ssm2602_add_widgets(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800531
Cliff Cai93547e82011-03-27 17:22:57 -0400532 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800533}
534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535/* remove everything here */
536static int ssm2602_remove(struct snd_soc_codec *codec)
537{
538 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
539 return 0;
540}
541
542static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
543 .probe = ssm2602_probe,
544 .remove = ssm2602_remove,
545 .suspend = ssm2602_suspend,
546 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen8fc63fe2011-05-05 16:59:14 +0200548 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 .reg_word_size = sizeof(u16),
550 .reg_cache_default = ssm2602_reg,
551};
Cliff Caib7138212008-09-05 18:09:57 +0800552
Mike Frysingerb39e2852011-04-07 02:05:11 -0400553#if defined(CONFIG_SPI_MASTER)
554static int __devinit ssm2602_spi_probe(struct spi_device *spi)
555{
556 struct ssm2602_priv *ssm2602;
557 int ret;
558
559 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
560 if (ssm2602 == NULL)
561 return -ENOMEM;
562
563 spi_set_drvdata(spi, ssm2602);
564 ssm2602->control_type = SND_SOC_SPI;
565
566 ret = snd_soc_register_codec(&spi->dev,
567 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
568 if (ret < 0)
569 kfree(ssm2602);
570 return ret;
571}
572
573static int __devexit ssm2602_spi_remove(struct spi_device *spi)
574{
575 snd_soc_unregister_codec(&spi->dev);
576 kfree(spi_get_drvdata(spi));
577 return 0;
578}
579
580static struct spi_driver ssm2602_spi_driver = {
581 .driver = {
582 .name = "ssm2602",
583 .owner = THIS_MODULE,
584 },
585 .probe = ssm2602_spi_probe,
586 .remove = __devexit_p(ssm2602_spi_remove),
587};
588#endif
589
Cliff Caib7138212008-09-05 18:09:57 +0800590#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
591/*
592 * ssm2602 2 wire address is determined by GPIO5
593 * state during powerup.
594 * low = 0x1a
595 * high = 0x1b
596 */
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200597static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
Cliff Caib7138212008-09-05 18:09:57 +0800598 const struct i2c_device_id *id)
599{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800601 int ret;
602
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
604 if (ssm2602 == NULL)
605 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800606
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 ssm2602->control_type = SND_SOC_I2C;
609
610 ret = snd_soc_register_codec(&i2c->dev,
611 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800612 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800614 return ret;
615}
616
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200617static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
Cliff Caib7138212008-09-05 18:09:57 +0800618{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 snd_soc_unregister_codec(&client->dev);
620 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800621 return 0;
622}
623
624static const struct i2c_device_id ssm2602_i2c_id[] = {
625 { "ssm2602", 0 },
626 { }
627};
628MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629
Cliff Caib7138212008-09-05 18:09:57 +0800630/* corgi i2c codec control layer */
631static struct i2c_driver ssm2602_i2c_driver = {
632 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400633 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800634 .owner = THIS_MODULE,
635 },
636 .probe = ssm2602_i2c_probe,
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200637 .remove = __devexit_p(ssm2602_i2c_remove),
Cliff Caib7138212008-09-05 18:09:57 +0800638 .id_table = ssm2602_i2c_id,
639};
Cliff Caib7138212008-09-05 18:09:57 +0800640#endif
641
Cliff Caib7138212008-09-05 18:09:57 +0800642
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100643static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000644{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400646
647#if defined(CONFIG_SPI_MASTER)
648 ret = spi_register_driver(&ssm2602_spi_driver);
649 if (ret)
650 return ret;
651#endif
652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
654 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400655 if (ret)
656 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000660}
661module_init(ssm2602_modinit);
662
663static void __exit ssm2602_exit(void)
664{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400665#if defined(CONFIG_SPI_MASTER)
666 spi_unregister_driver(&ssm2602_spi_driver);
667#endif
668
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
670 i2c_del_driver(&ssm2602_i2c_driver);
671#endif
Mark Brown64089b82008-12-08 19:17:58 +0000672}
673module_exit(ssm2602_exit);
674
Cliff Caib7138212008-09-05 18:09:57 +0800675MODULE_DESCRIPTION("ASoC ssm2602 driver");
676MODULE_AUTHOR("Cliff Cai");
677MODULE_LICENSE("GPL");