blob: 73b447eba2973fde2700a0678c300b264b02115d [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] = {
63 0x0017, 0x0017, 0x0079, 0x0079,
64 0x0000, 0x0000, 0x0000, 0x000a,
65 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
102SOC_ENUM("Capture Source", ssm2602_enum[0]),
103
104SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
105};
106
Cliff Caib7138212008-09-05 18:09:57 +0800107/* Output Mixer */
108static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
109SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
110SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
111SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
112};
113
114/* Input mux */
115static const struct snd_kcontrol_new ssm2602_input_mux_controls =
116SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
117
118static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
119SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
120 &ssm2602_output_mixer_controls[0],
121 ARRAY_SIZE(ssm2602_output_mixer_controls)),
122SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
123SND_SOC_DAPM_OUTPUT("LOUT"),
124SND_SOC_DAPM_OUTPUT("LHPOUT"),
125SND_SOC_DAPM_OUTPUT("ROUT"),
126SND_SOC_DAPM_OUTPUT("RHPOUT"),
127SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
128SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
129SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
130SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
131SND_SOC_DAPM_INPUT("MICIN"),
132SND_SOC_DAPM_INPUT("RLINEIN"),
133SND_SOC_DAPM_INPUT("LLINEIN"),
134};
135
136static const struct snd_soc_dapm_route audio_conn[] = {
137 /* output mixer */
138 {"Output Mixer", "Line Bypass Switch", "Line Input"},
139 {"Output Mixer", "HiFi Playback Switch", "DAC"},
140 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
141
142 /* outputs */
143 {"RHPOUT", NULL, "Output Mixer"},
144 {"ROUT", NULL, "Output Mixer"},
145 {"LHPOUT", NULL, "Output Mixer"},
146 {"LOUT", NULL, "Output Mixer"},
147
148 /* input mux */
149 {"Input Mux", "Line", "Line Input"},
150 {"Input Mux", "Mic", "Mic Bias"},
151 {"ADC", NULL, "Input Mux"},
152
153 /* inputs */
154 {"Line Input", NULL, "LLINEIN"},
155 {"Line Input", NULL, "RLINEIN"},
156 {"Mic Bias", NULL, "MICIN"},
157};
158
159static int ssm2602_add_widgets(struct snd_soc_codec *codec)
160{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 struct snd_soc_dapm_context *dapm = &codec->dapm;
Cliff Caib7138212008-09-05 18:09:57 +0800162
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200163 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
164 ARRAY_SIZE(ssm2602_dapm_widgets));
165 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
Cliff Caib7138212008-09-05 18:09:57 +0800166
Cliff Caib7138212008-09-05 18:09:57 +0800167 return 0;
168}
169
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200170struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800171 u32 mclk;
172 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200173 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800174};
175
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200176#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
177
178/* codec mclk clock coefficients */
179static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800180 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200181 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
182 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
183 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800184
185 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200186 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
187 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
188 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800189
190 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200191 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
192 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
193 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
194 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
195 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800196
197 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200198 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
199 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
200 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800201
202 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200203 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
204 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
205 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800206
207 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200208 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
209 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
210 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800211};
212
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200213static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800214{
215 int i;
216
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200217 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
218 if (ssm2602_coeff_table[i].rate == rate &&
219 ssm2602_coeff_table[i].mclk == mclk)
220 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800221 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200222 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800223}
224
225static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000226 struct snd_pcm_hw_params *params,
227 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800228{
Cliff Caib7138212008-09-05 18:09:57 +0800229 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;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200233 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Cliff Caib7138212008-09-05 18:09:57 +0800234
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
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200240 if (srate < 0)
241 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800242
Cliff Cai93547e82011-03-27 17:22:57 -0400243 snd_soc_write(codec, SSM2602_ACTIVE, 0);
244 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800245
246 /* bit size */
247 switch (params_format(params)) {
248 case SNDRV_PCM_FORMAT_S16_LE:
249 break;
250 case SNDRV_PCM_FORMAT_S20_3LE:
251 iface |= 0x0004;
252 break;
253 case SNDRV_PCM_FORMAT_S24_LE:
254 iface |= 0x0008;
255 break;
256 case SNDRV_PCM_FORMAT_S32_LE:
257 iface |= 0x000c;
258 break;
259 }
Cliff Cai93547e82011-03-27 17:22:57 -0400260 snd_soc_write(codec, SSM2602_IFACE, iface);
261 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800262 return 0;
263}
264
Mark Browndee89c42008-11-18 22:11:38 +0000265static int ssm2602_startup(struct snd_pcm_substream *substream,
266 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800267{
268 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000269 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900270 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100271 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800272 struct snd_pcm_runtime *master_runtime;
273
274 /* The DAI has shared clocks so if we already have a playback or
275 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100276 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800277 */
278 if (ssm2602->master_substream) {
279 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100280 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
281 master_runtime->sample_bits,
282 master_runtime->rate);
283
Cliff Caif692fce2009-06-02 00:18:54 -0400284 if (master_runtime->rate != 0)
285 snd_pcm_hw_constraint_minmax(substream->runtime,
286 SNDRV_PCM_HW_PARAM_RATE,
287 master_runtime->rate,
288 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800289
Cliff Caif692fce2009-06-02 00:18:54 -0400290 if (master_runtime->sample_bits != 0)
291 snd_pcm_hw_constraint_minmax(substream->runtime,
292 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
293 master_runtime->sample_bits,
294 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800295
296 ssm2602->slave_substream = substream;
297 } else
298 ssm2602->master_substream = substream;
299
300 return 0;
301}
302
Mark Browndee89c42008-11-18 22:11:38 +0000303static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
304 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800308 /* set active */
Cliff Cai93547e82011-03-27 17:22:57 -0400309 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800310
311 return 0;
312}
313
Mark Browndee89c42008-11-18 22:11:38 +0000314static void ssm2602_shutdown(struct snd_pcm_substream *substream,
315 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800316{
317 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000318 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900319 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400320
Cliff Caib7138212008-09-05 18:09:57 +0800321 /* deactivate */
322 if (!codec->active)
Cliff Cai93547e82011-03-27 17:22:57 -0400323 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100324
325 if (ssm2602->master_substream == substream)
326 ssm2602->master_substream = ssm2602->slave_substream;
327
328 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800329}
330
331static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
332{
333 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400334 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800335 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400336 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800337 mute_reg | APDIGI_ENABLE_DAC_MUTE);
338 else
Cliff Cai93547e82011-03-27 17:22:57 -0400339 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800340 return 0;
341}
342
343static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
344 int clk_id, unsigned int freq, int dir)
345{
346 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900347 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800348 switch (freq) {
349 case 11289600:
350 case 12000000:
351 case 12288000:
352 case 16934400:
353 case 18432000:
354 ssm2602->sysclk = freq;
355 return 0;
356 }
357 return -EINVAL;
358}
359
360static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
361 unsigned int fmt)
362{
363 struct snd_soc_codec *codec = codec_dai->codec;
364 u16 iface = 0;
365
366 /* set master/slave audio interface */
367 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
368 case SND_SOC_DAIFMT_CBM_CFM:
369 iface |= 0x0040;
370 break;
371 case SND_SOC_DAIFMT_CBS_CFS:
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 /* interface format */
378 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
379 case SND_SOC_DAIFMT_I2S:
380 iface |= 0x0002;
381 break;
382 case SND_SOC_DAIFMT_RIGHT_J:
383 break;
384 case SND_SOC_DAIFMT_LEFT_J:
385 iface |= 0x0001;
386 break;
387 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200388 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800389 break;
390 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200391 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800392 break;
393 default:
394 return -EINVAL;
395 }
396
397 /* clock inversion */
398 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
399 case SND_SOC_DAIFMT_NB_NF:
400 break;
401 case SND_SOC_DAIFMT_IB_IF:
402 iface |= 0x0090;
403 break;
404 case SND_SOC_DAIFMT_IB_NF:
405 iface |= 0x0080;
406 break;
407 case SND_SOC_DAIFMT_NB_IF:
408 iface |= 0x0010;
409 break;
410 default:
411 return -EINVAL;
412 }
413
414 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400415 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800416 return 0;
417}
418
419static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
420 enum snd_soc_bias_level level)
421{
Cliff Cai93547e82011-03-27 17:22:57 -0400422 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800423
424 switch (level) {
425 case SND_SOC_BIAS_ON:
426 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400427 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800428 break;
429 case SND_SOC_BIAS_PREPARE:
430 break;
431 case SND_SOC_BIAS_STANDBY:
432 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400433 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800434 break;
435 case SND_SOC_BIAS_OFF:
436 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400437 snd_soc_write(codec, SSM2602_ACTIVE, 0);
438 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800439 break;
440
441 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200442 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800443 return 0;
444}
445
Cliff Cai2552a712009-06-02 00:18:53 -0400446#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
447 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
448 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800449
Karl Beldan5de27b62008-11-20 15:39:31 +0100450#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
451 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
452
Eric Miao6335d052009-03-03 09:41:00 +0800453static struct snd_soc_dai_ops ssm2602_dai_ops = {
454 .startup = ssm2602_startup,
455 .prepare = ssm2602_pcm_prepare,
456 .hw_params = ssm2602_hw_params,
457 .shutdown = ssm2602_shutdown,
458 .digital_mute = ssm2602_mute,
459 .set_sysclk = ssm2602_set_dai_sysclk,
460 .set_fmt = ssm2602_set_dai_fmt,
461};
462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463static struct snd_soc_dai_driver ssm2602_dai = {
464 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800465 .playback = {
466 .stream_name = "Playback",
467 .channels_min = 2,
468 .channels_max = 2,
469 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100470 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800471 .capture = {
472 .stream_name = "Capture",
473 .channels_min = 2,
474 .channels_max = 2,
475 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100476 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800477 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800478};
Cliff Caib7138212008-09-05 18:09:57 +0800479
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800481{
Cliff Caib7138212008-09-05 18:09:57 +0800482 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
483 return 0;
484}
485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800487{
Cliff Cai93547e82011-03-27 17:22:57 -0400488 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800489
Cliff Caib7138212008-09-05 18:09:57 +0800490 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400491
Cliff Caib7138212008-09-05 18:09:57 +0800492 return 0;
493}
494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800496{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
498 int ret = 0, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
501
Cliff Cai93547e82011-03-27 17:22:57 -0400502 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
503 if (ret < 0) {
504 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
505 return ret;
506 }
Cliff Caib7138212008-09-05 18:09:57 +0800507
Cliff Cai93547e82011-03-27 17:22:57 -0400508 ret = ssm2602_reset(codec);
509 if (ret < 0) {
510 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
511 return ret;
512 }
Cliff Caib7138212008-09-05 18:09:57 +0800513
Cliff Caib7138212008-09-05 18:09:57 +0800514 /*power on device*/
Cliff Cai93547e82011-03-27 17:22:57 -0400515 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800516 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400517 reg = snd_soc_read(codec, SSM2602_LINVOL);
518 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
519 reg = snd_soc_read(codec, SSM2602_RINVOL);
520 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
521 reg = snd_soc_read(codec, SSM2602_LOUT1V);
522 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
523 reg = snd_soc_read(codec, SSM2602_ROUT1V);
524 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800525 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400526 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800527 APANA_ENABLE_MIC_BOOST);
Cliff Cai93547e82011-03-27 17:22:57 -0400528 snd_soc_write(codec, SSM2602_PWR, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800529
Ian Molton3e8e1952009-01-09 00:23:21 +0000530 snd_soc_add_controls(codec, ssm2602_snd_controls,
531 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800532 ssm2602_add_widgets(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800533
Cliff Cai93547e82011-03-27 17:22:57 -0400534 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800535}
536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537/* remove everything here */
538static int ssm2602_remove(struct snd_soc_codec *codec)
539{
540 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
541 return 0;
542}
543
544static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
545 .probe = ssm2602_probe,
546 .remove = ssm2602_remove,
547 .suspend = ssm2602_suspend,
548 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen8fc63fe2011-05-05 16:59:14 +0200550 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 .reg_word_size = sizeof(u16),
552 .reg_cache_default = ssm2602_reg,
553};
Cliff Caib7138212008-09-05 18:09:57 +0800554
Mike Frysingerb39e2852011-04-07 02:05:11 -0400555#if defined(CONFIG_SPI_MASTER)
556static int __devinit ssm2602_spi_probe(struct spi_device *spi)
557{
558 struct ssm2602_priv *ssm2602;
559 int ret;
560
561 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
562 if (ssm2602 == NULL)
563 return -ENOMEM;
564
565 spi_set_drvdata(spi, ssm2602);
566 ssm2602->control_type = SND_SOC_SPI;
567
568 ret = snd_soc_register_codec(&spi->dev,
569 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
570 if (ret < 0)
571 kfree(ssm2602);
572 return ret;
573}
574
575static int __devexit ssm2602_spi_remove(struct spi_device *spi)
576{
577 snd_soc_unregister_codec(&spi->dev);
578 kfree(spi_get_drvdata(spi));
579 return 0;
580}
581
582static struct spi_driver ssm2602_spi_driver = {
583 .driver = {
584 .name = "ssm2602",
585 .owner = THIS_MODULE,
586 },
587 .probe = ssm2602_spi_probe,
588 .remove = __devexit_p(ssm2602_spi_remove),
589};
590#endif
591
Cliff Caib7138212008-09-05 18:09:57 +0800592#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
593/*
594 * ssm2602 2 wire address is determined by GPIO5
595 * state during powerup.
596 * low = 0x1a
597 * high = 0x1b
598 */
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200599static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
Cliff Caib7138212008-09-05 18:09:57 +0800600 const struct i2c_device_id *id)
601{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800603 int ret;
604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
606 if (ssm2602 == NULL)
607 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 ssm2602->control_type = SND_SOC_I2C;
611
612 ret = snd_soc_register_codec(&i2c->dev,
613 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800614 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800616 return ret;
617}
618
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200619static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
Cliff Caib7138212008-09-05 18:09:57 +0800620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 snd_soc_unregister_codec(&client->dev);
622 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800623 return 0;
624}
625
626static const struct i2c_device_id ssm2602_i2c_id[] = {
627 { "ssm2602", 0 },
628 { }
629};
630MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631
Cliff Caib7138212008-09-05 18:09:57 +0800632/* corgi i2c codec control layer */
633static struct i2c_driver ssm2602_i2c_driver = {
634 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400635 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800636 .owner = THIS_MODULE,
637 },
638 .probe = ssm2602_i2c_probe,
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200639 .remove = __devexit_p(ssm2602_i2c_remove),
Cliff Caib7138212008-09-05 18:09:57 +0800640 .id_table = ssm2602_i2c_id,
641};
Cliff Caib7138212008-09-05 18:09:57 +0800642#endif
643
Cliff Caib7138212008-09-05 18:09:57 +0800644
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100645static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000646{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400648
649#if defined(CONFIG_SPI_MASTER)
650 ret = spi_register_driver(&ssm2602_spi_driver);
651 if (ret)
652 return ret;
653#endif
654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
656 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400657 if (ret)
658 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000662}
663module_init(ssm2602_modinit);
664
665static void __exit ssm2602_exit(void)
666{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400667#if defined(CONFIG_SPI_MASTER)
668 spi_unregister_driver(&ssm2602_spi_driver);
669#endif
670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
672 i2c_del_driver(&ssm2602_i2c_driver);
673#endif
Mark Brown64089b82008-12-08 19:17:58 +0000674}
675module_exit(ssm2602_exit);
676
Cliff Caib7138212008-09-05 18:09:57 +0800677MODULE_DESCRIPTION("ASoC ssm2602 driver");
678MODULE_AUTHOR("Cliff Cai");
679MODULE_LICENSE("GPL");