blob: cceb0022f02c3c660c873a45e979e028ef91ddc8 [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>
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070043#include <sound/tlv.h>
Cliff Caib7138212008-09-05 18:09:57 +080044
45#include "ssm2602.h"
46
Cliff Caib7138212008-09-05 18:09:57 +080047#define SSM2602_VERSION "0.1"
48
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070049enum ssm2602_type {
50 SSM2602,
51 SSM2604,
52};
53
Cliff Caib7138212008-09-05 18:09:57 +080054/* codec private data */
55struct ssm2602_priv {
56 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000057 enum snd_soc_control_type control_type;
Cliff Caib7138212008-09-05 18:09:57 +080058 struct snd_pcm_substream *master_substream;
59 struct snd_pcm_substream *slave_substream;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070060
61 enum ssm2602_type type;
Cliff Caib7138212008-09-05 18:09:57 +080062};
63
64/*
65 * ssm2602 register cache
66 * We can't read the ssm2602 register space when we are
67 * using 2 wire for device control, so we cache them instead.
68 * There is no point in caching the reset register
69 */
70static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
Lars-Peter Clausen7164bdb2011-05-08 09:24:41 -070071 0x0097, 0x0097, 0x0079, 0x0079,
72 0x000a, 0x0008, 0x009f, 0x000a,
Cliff Caib7138212008-09-05 18:09:57 +080073 0x0000, 0x0000
74};
75
Cliff Cai93547e82011-03-27 17:22:57 -040076#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080077
78/*Appending several "None"s just for OSS mixer use*/
79static const char *ssm2602_input_select[] = {
80 "Line", "Mic", "None", "None", "None",
81 "None", "None", "None",
82};
83
84static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
85
86static const struct soc_enum ssm2602_enum[] = {
87 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
88 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
89};
90
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070091static const unsigned int ssm260x_outmix_tlv[] = {
92 TLV_DB_RANGE_HEAD(2),
93 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
94 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0),
95};
96
97static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
98static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
99
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700100static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700101SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
102 ssm260x_inpga_tlv),
Cliff Caib7138212008-09-05 18:09:57 +0800103SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
104
Cliff Caib7138212008-09-05 18:09:57 +0800105SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
106SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
107
Cliff Caib7138212008-09-05 18:09:57 +0800108SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
109};
110
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700111static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700112SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
113 0, 127, 0, ssm260x_outmix_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700114SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
115 7, 1, 0),
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700116SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
117 ssm260x_sidetone_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700118
119SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
120SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
121SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
122};
123
Cliff Caib7138212008-09-05 18:09:57 +0800124/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700125static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800126SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800127SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700128SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800129};
130
131/* Input mux */
132static const struct snd_kcontrol_new ssm2602_input_mux_controls =
133SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
134
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700135static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800136SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800137SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800138SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700139
Mark Brown3afb1b32011-05-11 00:01:58 +0200140SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700141
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700142SND_SOC_DAPM_OUTPUT("LOUT"),
143SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800144SND_SOC_DAPM_INPUT("RLINEIN"),
145SND_SOC_DAPM_INPUT("LLINEIN"),
146};
147
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700148static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
149SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
150 ssm260x_output_mixer_controls,
151 ARRAY_SIZE(ssm260x_output_mixer_controls)),
152
153SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
154SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
155
156SND_SOC_DAPM_OUTPUT("LHPOUT"),
157SND_SOC_DAPM_OUTPUT("RHPOUT"),
158SND_SOC_DAPM_INPUT("MICIN"),
159};
160
161static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
162SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
163 ssm260x_output_mixer_controls,
164 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
165};
166
167static const struct snd_soc_dapm_route ssm260x_routes[] = {
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700168 {"DAC", NULL, "Digital Core Power"},
169 {"ADC", NULL, "Digital Core Power"},
170
Cliff Caib7138212008-09-05 18:09:57 +0800171 {"Output Mixer", "Line Bypass Switch", "Line Input"},
172 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800173
Cliff Caib7138212008-09-05 18:09:57 +0800174 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800175 {"LOUT", NULL, "Output Mixer"},
176
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700177 {"Line Input", NULL, "LLINEIN"},
178 {"Line Input", NULL, "RLINEIN"},
179};
180
181static const struct snd_soc_dapm_route ssm2602_routes[] = {
182 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
183
184 {"RHPOUT", NULL, "Output Mixer"},
185 {"LHPOUT", NULL, "Output Mixer"},
186
Cliff Caib7138212008-09-05 18:09:57 +0800187 {"Input Mux", "Line", "Line Input"},
188 {"Input Mux", "Mic", "Mic Bias"},
189 {"ADC", NULL, "Input Mux"},
190
Cliff Caib7138212008-09-05 18:09:57 +0800191 {"Mic Bias", NULL, "MICIN"},
192};
193
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700194static const struct snd_soc_dapm_route ssm2604_routes[] = {
195 {"ADC", NULL, "Line Input"},
196};
Cliff Caib7138212008-09-05 18:09:57 +0800197
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200198struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800199 u32 mclk;
200 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200201 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800202};
203
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200204#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
205
206/* codec mclk clock coefficients */
207static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800208 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200209 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
210 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
211 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800212
213 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200214 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
215 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
216 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800217
218 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200219 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
220 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
221 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
222 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
223 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800224
225 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200226 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
227 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
228 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800229
230 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200231 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
232 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
233 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800234
235 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200236 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
237 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
238 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800239};
240
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200241static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800242{
243 int i;
244
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200245 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
246 if (ssm2602_coeff_table[i].rate == rate &&
247 ssm2602_coeff_table[i].mclk == mclk)
248 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800249 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200250 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800251}
252
253static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000254 struct snd_pcm_hw_params *params,
255 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800256{
Cliff Caib7138212008-09-05 18:09:57 +0800257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000258 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900259 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400260 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200261 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Cliff Caib7138212008-09-05 18:09:57 +0800262
Karl Beldanfaab5a32008-11-20 15:39:27 +0100263 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400264 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100265 return 0;
266 }
267
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200268 if (srate < 0)
269 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800270
Cliff Cai93547e82011-03-27 17:22:57 -0400271 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800272
273 /* bit size */
274 switch (params_format(params)) {
275 case SNDRV_PCM_FORMAT_S16_LE:
276 break;
277 case SNDRV_PCM_FORMAT_S20_3LE:
278 iface |= 0x0004;
279 break;
280 case SNDRV_PCM_FORMAT_S24_LE:
281 iface |= 0x0008;
282 break;
283 case SNDRV_PCM_FORMAT_S32_LE:
284 iface |= 0x000c;
285 break;
286 }
Cliff Cai93547e82011-03-27 17:22:57 -0400287 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800288 return 0;
289}
290
Mark Browndee89c42008-11-18 22:11:38 +0000291static int ssm2602_startup(struct snd_pcm_substream *substream,
292 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800293{
294 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000295 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900296 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800297 struct snd_pcm_runtime *master_runtime;
298
299 /* The DAI has shared clocks so if we already have a playback or
300 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100301 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800302 */
303 if (ssm2602->master_substream) {
304 master_runtime = ssm2602->master_substream->runtime;
Lars-Peter Clausen26806a42011-09-20 08:19:58 +0200305 dev_dbg(codec->dev, "Constraining to %d bits at %dHz\n",
Karl Beldanfaab5a32008-11-20 15:39:27 +0100306 master_runtime->sample_bits,
307 master_runtime->rate);
308
Cliff Caif692fce2009-06-02 00:18:54 -0400309 if (master_runtime->rate != 0)
310 snd_pcm_hw_constraint_minmax(substream->runtime,
311 SNDRV_PCM_HW_PARAM_RATE,
312 master_runtime->rate,
313 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800314
Cliff Caif692fce2009-06-02 00:18:54 -0400315 if (master_runtime->sample_bits != 0)
316 snd_pcm_hw_constraint_minmax(substream->runtime,
317 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
318 master_runtime->sample_bits,
319 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800320
321 ssm2602->slave_substream = substream;
322 } else
323 ssm2602->master_substream = substream;
324
325 return 0;
326}
327
Mark Browndee89c42008-11-18 22:11:38 +0000328static void ssm2602_shutdown(struct snd_pcm_substream *substream,
329 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800330{
331 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000332 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900333 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400334
Karl Beldanfaab5a32008-11-20 15:39:27 +0100335 if (ssm2602->master_substream == substream)
336 ssm2602->master_substream = ssm2602->slave_substream;
337
338 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800339}
340
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700341
Cliff Caib7138212008-09-05 18:09:57 +0800342static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
343{
344 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400345 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800346 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400347 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800348 mute_reg | APDIGI_ENABLE_DAC_MUTE);
349 else
Cliff Cai93547e82011-03-27 17:22:57 -0400350 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800351 return 0;
352}
353
354static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
355 int clk_id, unsigned int freq, int dir)
356{
357 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900358 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800359 switch (freq) {
360 case 11289600:
361 case 12000000:
362 case 12288000:
363 case 16934400:
364 case 18432000:
365 ssm2602->sysclk = freq;
366 return 0;
367 }
368 return -EINVAL;
369}
370
371static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
372 unsigned int fmt)
373{
374 struct snd_soc_codec *codec = codec_dai->codec;
375 u16 iface = 0;
376
377 /* set master/slave audio interface */
378 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
379 case SND_SOC_DAIFMT_CBM_CFM:
380 iface |= 0x0040;
381 break;
382 case SND_SOC_DAIFMT_CBS_CFS:
383 break;
384 default:
385 return -EINVAL;
386 }
387
388 /* interface format */
389 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
390 case SND_SOC_DAIFMT_I2S:
391 iface |= 0x0002;
392 break;
393 case SND_SOC_DAIFMT_RIGHT_J:
394 break;
395 case SND_SOC_DAIFMT_LEFT_J:
396 iface |= 0x0001;
397 break;
398 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200399 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800400 break;
401 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200402 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800403 break;
404 default:
405 return -EINVAL;
406 }
407
408 /* clock inversion */
409 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
410 case SND_SOC_DAIFMT_NB_NF:
411 break;
412 case SND_SOC_DAIFMT_IB_IF:
413 iface |= 0x0090;
414 break;
415 case SND_SOC_DAIFMT_IB_NF:
416 iface |= 0x0080;
417 break;
418 case SND_SOC_DAIFMT_NB_IF:
419 iface |= 0x0010;
420 break;
421 default:
422 return -EINVAL;
423 }
424
425 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400426 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800427 return 0;
428}
429
430static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
431 enum snd_soc_bias_level level)
432{
Cliff Cai93547e82011-03-27 17:22:57 -0400433 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800434
435 switch (level) {
436 case SND_SOC_BIAS_ON:
437 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400438 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800439 break;
440 case SND_SOC_BIAS_PREPARE:
441 break;
442 case SND_SOC_BIAS_STANDBY:
443 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400444 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800445 break;
446 case SND_SOC_BIAS_OFF:
447 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400448 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800449 break;
450
451 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200452 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800453 return 0;
454}
455
Cliff Cai2552a712009-06-02 00:18:53 -0400456#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
457 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
458 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800459
Karl Beldan5de27b62008-11-20 15:39:31 +0100460#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
461 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
462
Eric Miao6335d052009-03-03 09:41:00 +0800463static struct snd_soc_dai_ops ssm2602_dai_ops = {
464 .startup = ssm2602_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800465 .hw_params = ssm2602_hw_params,
466 .shutdown = ssm2602_shutdown,
467 .digital_mute = ssm2602_mute,
468 .set_sysclk = ssm2602_set_dai_sysclk,
469 .set_fmt = ssm2602_set_dai_fmt,
470};
471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472static struct snd_soc_dai_driver ssm2602_dai = {
473 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800474 .playback = {
475 .stream_name = "Playback",
476 .channels_min = 2,
477 .channels_max = 2,
478 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100479 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800480 .capture = {
481 .stream_name = "Capture",
482 .channels_min = 2,
483 .channels_max = 2,
484 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100485 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800486 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800487};
Cliff Caib7138212008-09-05 18:09:57 +0800488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800490{
Cliff Caib7138212008-09-05 18:09:57 +0800491 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
492 return 0;
493}
494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800496{
Cliff Cai93547e82011-03-27 17:22:57 -0400497 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800498
Cliff Caib7138212008-09-05 18:09:57 +0800499 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400500
Cliff Caib7138212008-09-05 18:09:57 +0800501 return 0;
502}
503
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800505{
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700506 struct snd_soc_dapm_context *dapm = &codec->dapm;
507 int ret, reg;
508
509 reg = snd_soc_read(codec, SSM2602_LOUT1V);
510 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
511 reg = snd_soc_read(codec, SSM2602_ROUT1V);
512 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
513
514 ret = snd_soc_add_controls(codec, ssm2602_snd_controls,
515 ARRAY_SIZE(ssm2602_snd_controls));
516 if (ret)
517 return ret;
518
519 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
520 ARRAY_SIZE(ssm2602_dapm_widgets));
521 if (ret)
522 return ret;
523
524 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
525 ARRAY_SIZE(ssm2602_routes));
526}
527
528static int ssm2604_probe(struct snd_soc_codec *codec)
529{
530 struct snd_soc_dapm_context *dapm = &codec->dapm;
531 int ret;
532
533 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
534 ARRAY_SIZE(ssm2604_dapm_widgets));
535 if (ret)
536 return ret;
537
538 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
539 ARRAY_SIZE(ssm2604_routes));
540}
541
542static int ssm260x_probe(struct snd_soc_codec *codec)
543{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700545 int ret, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
548
Cliff Cai93547e82011-03-27 17:22:57 -0400549 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
550 if (ret < 0) {
551 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
552 return ret;
553 }
Cliff Caib7138212008-09-05 18:09:57 +0800554
Cliff Cai93547e82011-03-27 17:22:57 -0400555 ret = ssm2602_reset(codec);
556 if (ret < 0) {
557 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
558 return ret;
559 }
Cliff Caib7138212008-09-05 18:09:57 +0800560
Cliff Caib7138212008-09-05 18:09:57 +0800561 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400562 reg = snd_soc_read(codec, SSM2602_LINVOL);
563 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
564 reg = snd_soc_read(codec, SSM2602_RINVOL);
565 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800566 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400567 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800568 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800569
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700570 switch (ssm2602->type) {
571 case SSM2602:
572 ret = ssm2602_probe(codec);
573 break;
574 case SSM2604:
575 ret = ssm2604_probe(codec);
576 break;
577 }
Cliff Caib7138212008-09-05 18:09:57 +0800578
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700579 return ret;
Cliff Caib7138212008-09-05 18:09:57 +0800580}
581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582/* remove everything here */
583static int ssm2602_remove(struct snd_soc_codec *codec)
584{
585 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
586 return 0;
587}
588
589static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700590 .probe = ssm260x_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 .remove = ssm2602_remove,
592 .suspend = ssm2602_suspend,
593 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen8fc63fe2011-05-05 16:59:14 +0200595 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 .reg_word_size = sizeof(u16),
597 .reg_cache_default = ssm2602_reg,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700598
599 .controls = ssm260x_snd_controls,
600 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
601 .dapm_widgets = ssm260x_dapm_widgets,
602 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
603 .dapm_routes = ssm260x_routes,
604 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605};
Cliff Caib7138212008-09-05 18:09:57 +0800606
Mike Frysingerb39e2852011-04-07 02:05:11 -0400607#if defined(CONFIG_SPI_MASTER)
608static int __devinit ssm2602_spi_probe(struct spi_device *spi)
609{
610 struct ssm2602_priv *ssm2602;
611 int ret;
612
613 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
614 if (ssm2602 == NULL)
615 return -ENOMEM;
616
617 spi_set_drvdata(spi, ssm2602);
618 ssm2602->control_type = SND_SOC_SPI;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700619 ssm2602->type = SSM2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400620
621 ret = snd_soc_register_codec(&spi->dev,
622 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
623 if (ret < 0)
624 kfree(ssm2602);
625 return ret;
626}
627
628static int __devexit ssm2602_spi_remove(struct spi_device *spi)
629{
630 snd_soc_unregister_codec(&spi->dev);
631 kfree(spi_get_drvdata(spi));
632 return 0;
633}
634
635static struct spi_driver ssm2602_spi_driver = {
636 .driver = {
637 .name = "ssm2602",
638 .owner = THIS_MODULE,
639 },
640 .probe = ssm2602_spi_probe,
641 .remove = __devexit_p(ssm2602_spi_remove),
642};
643#endif
644
Cliff Caib7138212008-09-05 18:09:57 +0800645#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
646/*
647 * ssm2602 2 wire address is determined by GPIO5
648 * state during powerup.
649 * low = 0x1a
650 * high = 0x1b
651 */
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200652static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
Cliff Caib7138212008-09-05 18:09:57 +0800653 const struct i2c_device_id *id)
654{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800656 int ret;
657
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
659 if (ssm2602 == NULL)
660 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 ssm2602->control_type = SND_SOC_I2C;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700664 ssm2602->type = id->driver_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665
666 ret = snd_soc_register_codec(&i2c->dev,
667 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800668 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800670 return ret;
671}
672
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200673static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
Cliff Caib7138212008-09-05 18:09:57 +0800674{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 snd_soc_unregister_codec(&client->dev);
676 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800677 return 0;
678}
679
680static const struct i2c_device_id ssm2602_i2c_id[] = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700681 { "ssm2602", SSM2602 },
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700682 { "ssm2603", SSM2602 },
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700683 { "ssm2604", SSM2604 },
Cliff Caib7138212008-09-05 18:09:57 +0800684 { }
685};
686MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687
Cliff Caib7138212008-09-05 18:09:57 +0800688/* corgi i2c codec control layer */
689static struct i2c_driver ssm2602_i2c_driver = {
690 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400691 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800692 .owner = THIS_MODULE,
693 },
694 .probe = ssm2602_i2c_probe,
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200695 .remove = __devexit_p(ssm2602_i2c_remove),
Cliff Caib7138212008-09-05 18:09:57 +0800696 .id_table = ssm2602_i2c_id,
697};
Cliff Caib7138212008-09-05 18:09:57 +0800698#endif
699
Cliff Caib7138212008-09-05 18:09:57 +0800700
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100701static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000702{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400704
705#if defined(CONFIG_SPI_MASTER)
706 ret = spi_register_driver(&ssm2602_spi_driver);
707 if (ret)
708 return ret;
709#endif
710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
712 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400713 if (ret)
714 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000718}
719module_init(ssm2602_modinit);
720
721static void __exit ssm2602_exit(void)
722{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400723#if defined(CONFIG_SPI_MASTER)
724 spi_unregister_driver(&ssm2602_spi_driver);
725#endif
726
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
728 i2c_del_driver(&ssm2602_i2c_driver);
729#endif
Mark Brown64089b82008-12-08 19:17:58 +0000730}
731module_exit(ssm2602_exit);
732
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700733MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800734MODULE_AUTHOR("Cliff Cai");
735MODULE_LICENSE("GPL");