blob: e149ec61e6be9482f3ab8888025c4d7081c49305 [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;
Lars-Peter Clausen02890532011-09-27 11:08:48 +020062 unsigned int clk_out_pwr;
Cliff Caib7138212008-09-05 18:09:57 +080063};
64
65/*
66 * ssm2602 register cache
67 * We can't read the ssm2602 register space when we are
68 * using 2 wire for device control, so we cache them instead.
69 * There is no point in caching the reset register
70 */
71static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
Lars-Peter Clausen7164bdb2011-05-08 09:24:41 -070072 0x0097, 0x0097, 0x0079, 0x0079,
73 0x000a, 0x0008, 0x009f, 0x000a,
Cliff Caib7138212008-09-05 18:09:57 +080074 0x0000, 0x0000
75};
76
Cliff Cai93547e82011-03-27 17:22:57 -040077#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080078
79/*Appending several "None"s just for OSS mixer use*/
80static const char *ssm2602_input_select[] = {
81 "Line", "Mic", "None", "None", "None",
82 "None", "None", "None",
83};
84
85static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
86
87static const struct soc_enum ssm2602_enum[] = {
88 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
89 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
90};
91
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070092static const unsigned int ssm260x_outmix_tlv[] = {
93 TLV_DB_RANGE_HEAD(2),
94 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
95 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0),
96};
97
98static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
99static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
100
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700101static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700102SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
103 ssm260x_inpga_tlv),
Cliff Caib7138212008-09-05 18:09:57 +0800104SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
105
Cliff Caib7138212008-09-05 18:09:57 +0800106SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
107SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
108
Cliff Caib7138212008-09-05 18:09:57 +0800109SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
110};
111
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700112static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700113SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
114 0, 127, 0, ssm260x_outmix_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700115SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
116 7, 1, 0),
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700117SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
118 ssm260x_sidetone_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700119
120SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
121SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
122SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
123};
124
Cliff Caib7138212008-09-05 18:09:57 +0800125/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700126static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800127SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800128SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700129SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800130};
131
132/* Input mux */
133static const struct snd_kcontrol_new ssm2602_input_mux_controls =
134SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
135
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700136static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800137SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800138SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800139SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700140
Mark Brown3afb1b3e2011-05-11 00:01:58 +0200141SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700142
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700143SND_SOC_DAPM_OUTPUT("LOUT"),
144SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800145SND_SOC_DAPM_INPUT("RLINEIN"),
146SND_SOC_DAPM_INPUT("LLINEIN"),
147};
148
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700149static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
150SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
151 ssm260x_output_mixer_controls,
152 ARRAY_SIZE(ssm260x_output_mixer_controls)),
153
154SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
155SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
156
157SND_SOC_DAPM_OUTPUT("LHPOUT"),
158SND_SOC_DAPM_OUTPUT("RHPOUT"),
159SND_SOC_DAPM_INPUT("MICIN"),
160};
161
162static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
163SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
164 ssm260x_output_mixer_controls,
165 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
166};
167
168static const struct snd_soc_dapm_route ssm260x_routes[] = {
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700169 {"DAC", NULL, "Digital Core Power"},
170 {"ADC", NULL, "Digital Core Power"},
171
Cliff Caib7138212008-09-05 18:09:57 +0800172 {"Output Mixer", "Line Bypass Switch", "Line Input"},
173 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800174
Cliff Caib7138212008-09-05 18:09:57 +0800175 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800176 {"LOUT", NULL, "Output Mixer"},
177
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700178 {"Line Input", NULL, "LLINEIN"},
179 {"Line Input", NULL, "RLINEIN"},
180};
181
182static const struct snd_soc_dapm_route ssm2602_routes[] = {
183 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
184
185 {"RHPOUT", NULL, "Output Mixer"},
186 {"LHPOUT", NULL, "Output Mixer"},
187
Cliff Caib7138212008-09-05 18:09:57 +0800188 {"Input Mux", "Line", "Line Input"},
189 {"Input Mux", "Mic", "Mic Bias"},
190 {"ADC", NULL, "Input Mux"},
191
Cliff Caib7138212008-09-05 18:09:57 +0800192 {"Mic Bias", NULL, "MICIN"},
193};
194
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700195static const struct snd_soc_dapm_route ssm2604_routes[] = {
196 {"ADC", NULL, "Line Input"},
197};
Cliff Caib7138212008-09-05 18:09:57 +0800198
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200199struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800200 u32 mclk;
201 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200202 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800203};
204
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200205#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
206
207/* codec mclk clock coefficients */
208static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800209 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200210 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
211 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
212 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800213
214 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200215 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
216 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
217 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800218
219 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200220 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
221 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
222 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
223 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
224 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800225
226 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200227 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
228 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
229 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800230
231 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200232 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
233 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
234 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800235
236 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200237 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
238 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
239 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800240};
241
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200242static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800243{
244 int i;
245
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200246 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
247 if (ssm2602_coeff_table[i].rate == rate &&
248 ssm2602_coeff_table[i].mclk == mclk)
249 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800250 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200251 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800252}
253
254static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000255 struct snd_pcm_hw_params *params,
256 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800257{
Cliff Caib7138212008-09-05 18:09:57 +0800258 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000259 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900260 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400261 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200262 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Cliff Caib7138212008-09-05 18:09:57 +0800263
Karl Beldanfaab5a32008-11-20 15:39:27 +0100264 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400265 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100266 return 0;
267 }
268
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200269 if (srate < 0)
270 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800271
Cliff Cai93547e82011-03-27 17:22:57 -0400272 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800273
274 /* bit size */
275 switch (params_format(params)) {
276 case SNDRV_PCM_FORMAT_S16_LE:
277 break;
278 case SNDRV_PCM_FORMAT_S20_3LE:
279 iface |= 0x0004;
280 break;
281 case SNDRV_PCM_FORMAT_S24_LE:
282 iface |= 0x0008;
283 break;
284 case SNDRV_PCM_FORMAT_S32_LE:
285 iface |= 0x000c;
286 break;
287 }
Cliff Cai93547e82011-03-27 17:22:57 -0400288 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800289 return 0;
290}
291
Mark Browndee89c42008-11-18 22:11:38 +0000292static int ssm2602_startup(struct snd_pcm_substream *substream,
293 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800294{
295 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000296 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900297 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800298 struct snd_pcm_runtime *master_runtime;
299
300 /* The DAI has shared clocks so if we already have a playback or
301 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100302 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800303 */
304 if (ssm2602->master_substream) {
305 master_runtime = ssm2602->master_substream->runtime;
Lars-Peter Clausen26806a42011-09-20 08:19:58 +0200306 dev_dbg(codec->dev, "Constraining to %d bits at %dHz\n",
Karl Beldanfaab5a32008-11-20 15:39:27 +0100307 master_runtime->sample_bits,
308 master_runtime->rate);
309
Cliff Caif692fce2009-06-02 00:18:54 -0400310 if (master_runtime->rate != 0)
311 snd_pcm_hw_constraint_minmax(substream->runtime,
312 SNDRV_PCM_HW_PARAM_RATE,
313 master_runtime->rate,
314 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800315
Cliff Caif692fce2009-06-02 00:18:54 -0400316 if (master_runtime->sample_bits != 0)
317 snd_pcm_hw_constraint_minmax(substream->runtime,
318 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
319 master_runtime->sample_bits,
320 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800321
322 ssm2602->slave_substream = substream;
323 } else
324 ssm2602->master_substream = substream;
325
326 return 0;
327}
328
Mark Browndee89c42008-11-18 22:11:38 +0000329static void ssm2602_shutdown(struct snd_pcm_substream *substream,
330 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800331{
332 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900334 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400335
Karl Beldanfaab5a32008-11-20 15:39:27 +0100336 if (ssm2602->master_substream == substream)
337 ssm2602->master_substream = ssm2602->slave_substream;
338
339 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800340}
341
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700342
Cliff Caib7138212008-09-05 18:09:57 +0800343static 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);
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200360
361 if (dir == SND_SOC_CLOCK_IN) {
362 if (clk_id != SSM2602_SYSCLK)
363 return -EINVAL;
364
365 switch (freq) {
366 case 11289600:
367 case 12000000:
368 case 12288000:
369 case 16934400:
370 case 18432000:
371 ssm2602->sysclk = freq;
372 break;
373 default:
374 return -EINVAL;
375 }
376 } else {
377 unsigned int mask;
378
379 switch (clk_id) {
380 case SSM2602_CLK_CLKOUT:
381 mask = PWR_CLK_OUT_PDN;
382 break;
383 case SSM2602_CLK_XTO:
384 mask = PWR_OSC_PDN;
385 break;
386 default:
387 return -EINVAL;
388 }
389
390 if (freq == 0)
391 ssm2602->clk_out_pwr |= mask;
392 else
393 ssm2602->clk_out_pwr &= ~mask;
394
395 snd_soc_update_bits(codec, SSM2602_PWR,
396 PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800397 }
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200398
399 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800400}
401
402static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
403 unsigned int fmt)
404{
405 struct snd_soc_codec *codec = codec_dai->codec;
406 u16 iface = 0;
407
408 /* set master/slave audio interface */
409 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
410 case SND_SOC_DAIFMT_CBM_CFM:
411 iface |= 0x0040;
412 break;
413 case SND_SOC_DAIFMT_CBS_CFS:
414 break;
415 default:
416 return -EINVAL;
417 }
418
419 /* interface format */
420 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
421 case SND_SOC_DAIFMT_I2S:
422 iface |= 0x0002;
423 break;
424 case SND_SOC_DAIFMT_RIGHT_J:
425 break;
426 case SND_SOC_DAIFMT_LEFT_J:
427 iface |= 0x0001;
428 break;
429 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200430 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800431 break;
432 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200433 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800434 break;
435 default:
436 return -EINVAL;
437 }
438
439 /* clock inversion */
440 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
441 case SND_SOC_DAIFMT_NB_NF:
442 break;
443 case SND_SOC_DAIFMT_IB_IF:
444 iface |= 0x0090;
445 break;
446 case SND_SOC_DAIFMT_IB_NF:
447 iface |= 0x0080;
448 break;
449 case SND_SOC_DAIFMT_NB_IF:
450 iface |= 0x0010;
451 break;
452 default:
453 return -EINVAL;
454 }
455
456 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400457 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800458 return 0;
459}
460
461static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
462 enum snd_soc_bias_level level)
463{
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200464 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800465
466 switch (level) {
467 case SND_SOC_BIAS_ON:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200468 /* vref/mid on, osc and clkout on if enabled */
469 snd_soc_update_bits(codec, SSM2602_PWR,
470 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
471 ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800472 break;
473 case SND_SOC_BIAS_PREPARE:
474 break;
475 case SND_SOC_BIAS_STANDBY:
476 /* everything off except vref/vmid, */
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200477 snd_soc_update_bits(codec, SSM2602_PWR,
478 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
479 PWR_CLK_OUT_PDN | PWR_OSC_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800480 break;
481 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200482 /* everything off */
483 snd_soc_update_bits(codec, SSM2602_PWR,
484 PWR_POWER_OFF, PWR_POWER_OFF);
Cliff Caib7138212008-09-05 18:09:57 +0800485 break;
486
487 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200488 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800489 return 0;
490}
491
Cliff Cai2552a712009-06-02 00:18:53 -0400492#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
493 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
494 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800495
Karl Beldan5de27b62008-11-20 15:39:31 +0100496#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
497 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
498
Eric Miao6335d052009-03-03 09:41:00 +0800499static struct snd_soc_dai_ops ssm2602_dai_ops = {
500 .startup = ssm2602_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800501 .hw_params = ssm2602_hw_params,
502 .shutdown = ssm2602_shutdown,
503 .digital_mute = ssm2602_mute,
504 .set_sysclk = ssm2602_set_dai_sysclk,
505 .set_fmt = ssm2602_set_dai_fmt,
506};
507
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508static struct snd_soc_dai_driver ssm2602_dai = {
509 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800510 .playback = {
511 .stream_name = "Playback",
512 .channels_min = 2,
513 .channels_max = 2,
514 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100515 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800516 .capture = {
517 .stream_name = "Capture",
518 .channels_min = 2,
519 .channels_max = 2,
520 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100521 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800522 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800523};
Cliff Caib7138212008-09-05 18:09:57 +0800524
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800526{
Cliff Caib7138212008-09-05 18:09:57 +0800527 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
528 return 0;
529}
530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800532{
Cliff Cai93547e82011-03-27 17:22:57 -0400533 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800534
Cliff Caib7138212008-09-05 18:09:57 +0800535 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400536
Cliff Caib7138212008-09-05 18:09:57 +0800537 return 0;
538}
539
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800541{
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700542 struct snd_soc_dapm_context *dapm = &codec->dapm;
543 int ret, reg;
544
545 reg = snd_soc_read(codec, SSM2602_LOUT1V);
546 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
547 reg = snd_soc_read(codec, SSM2602_ROUT1V);
548 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
549
550 ret = snd_soc_add_controls(codec, ssm2602_snd_controls,
551 ARRAY_SIZE(ssm2602_snd_controls));
552 if (ret)
553 return ret;
554
555 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
556 ARRAY_SIZE(ssm2602_dapm_widgets));
557 if (ret)
558 return ret;
559
560 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
561 ARRAY_SIZE(ssm2602_routes));
562}
563
564static int ssm2604_probe(struct snd_soc_codec *codec)
565{
566 struct snd_soc_dapm_context *dapm = &codec->dapm;
567 int ret;
568
569 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
570 ARRAY_SIZE(ssm2604_dapm_widgets));
571 if (ret)
572 return ret;
573
574 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
575 ARRAY_SIZE(ssm2604_routes));
576}
577
578static int ssm260x_probe(struct snd_soc_codec *codec)
579{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700581 int ret, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
584
Cliff Cai93547e82011-03-27 17:22:57 -0400585 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
586 if (ret < 0) {
587 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
588 return ret;
589 }
Cliff Caib7138212008-09-05 18:09:57 +0800590
Cliff Cai93547e82011-03-27 17:22:57 -0400591 ret = ssm2602_reset(codec);
592 if (ret < 0) {
593 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
594 return ret;
595 }
Cliff Caib7138212008-09-05 18:09:57 +0800596
Cliff Caib7138212008-09-05 18:09:57 +0800597 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400598 reg = snd_soc_read(codec, SSM2602_LINVOL);
599 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
600 reg = snd_soc_read(codec, SSM2602_RINVOL);
601 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800602 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400603 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800604 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800605
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700606 switch (ssm2602->type) {
607 case SSM2602:
608 ret = ssm2602_probe(codec);
609 break;
610 case SSM2604:
611 ret = ssm2604_probe(codec);
612 break;
613 }
Cliff Caib7138212008-09-05 18:09:57 +0800614
Lars-Peter Clausena9d19742011-09-27 11:08:47 +0200615 if (ret)
616 return ret;
617
618 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
619
620 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800621}
622
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623/* remove everything here */
624static int ssm2602_remove(struct snd_soc_codec *codec)
625{
626 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
627 return 0;
628}
629
630static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700631 .probe = ssm260x_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 .remove = ssm2602_remove,
633 .suspend = ssm2602_suspend,
634 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen8fc63fe2011-05-05 16:59:14 +0200636 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 .reg_word_size = sizeof(u16),
638 .reg_cache_default = ssm2602_reg,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700639
640 .controls = ssm260x_snd_controls,
641 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
642 .dapm_widgets = ssm260x_dapm_widgets,
643 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
644 .dapm_routes = ssm260x_routes,
645 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646};
Cliff Caib7138212008-09-05 18:09:57 +0800647
Mike Frysingerb39e2852011-04-07 02:05:11 -0400648#if defined(CONFIG_SPI_MASTER)
649static int __devinit ssm2602_spi_probe(struct spi_device *spi)
650{
651 struct ssm2602_priv *ssm2602;
652 int ret;
653
654 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
655 if (ssm2602 == NULL)
656 return -ENOMEM;
657
658 spi_set_drvdata(spi, ssm2602);
659 ssm2602->control_type = SND_SOC_SPI;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700660 ssm2602->type = SSM2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400661
662 ret = snd_soc_register_codec(&spi->dev,
663 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
664 if (ret < 0)
665 kfree(ssm2602);
666 return ret;
667}
668
669static int __devexit ssm2602_spi_remove(struct spi_device *spi)
670{
671 snd_soc_unregister_codec(&spi->dev);
672 kfree(spi_get_drvdata(spi));
673 return 0;
674}
675
676static struct spi_driver ssm2602_spi_driver = {
677 .driver = {
678 .name = "ssm2602",
679 .owner = THIS_MODULE,
680 },
681 .probe = ssm2602_spi_probe,
682 .remove = __devexit_p(ssm2602_spi_remove),
683};
684#endif
685
Cliff Caib7138212008-09-05 18:09:57 +0800686#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
687/*
688 * ssm2602 2 wire address is determined by GPIO5
689 * state during powerup.
690 * low = 0x1a
691 * high = 0x1b
692 */
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200693static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
Cliff Caib7138212008-09-05 18:09:57 +0800694 const struct i2c_device_id *id)
695{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800697 int ret;
698
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
700 if (ssm2602 == NULL)
701 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 ssm2602->control_type = SND_SOC_I2C;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700705 ssm2602->type = id->driver_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706
707 ret = snd_soc_register_codec(&i2c->dev,
708 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800709 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800711 return ret;
712}
713
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200714static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
Cliff Caib7138212008-09-05 18:09:57 +0800715{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 snd_soc_unregister_codec(&client->dev);
717 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800718 return 0;
719}
720
721static const struct i2c_device_id ssm2602_i2c_id[] = {
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700722 { "ssm2602", SSM2602 },
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700723 { "ssm2603", SSM2602 },
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700724 { "ssm2604", SSM2604 },
Cliff Caib7138212008-09-05 18:09:57 +0800725 { }
726};
727MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728
Cliff Caib7138212008-09-05 18:09:57 +0800729/* corgi i2c codec control layer */
730static struct i2c_driver ssm2602_i2c_driver = {
731 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400732 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800733 .owner = THIS_MODULE,
734 },
735 .probe = ssm2602_i2c_probe,
Lars-Peter Clausen04b89452011-05-05 16:59:12 +0200736 .remove = __devexit_p(ssm2602_i2c_remove),
Cliff Caib7138212008-09-05 18:09:57 +0800737 .id_table = ssm2602_i2c_id,
738};
Cliff Caib7138212008-09-05 18:09:57 +0800739#endif
740
Cliff Caib7138212008-09-05 18:09:57 +0800741
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100742static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000743{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400745
746#if defined(CONFIG_SPI_MASTER)
747 ret = spi_register_driver(&ssm2602_spi_driver);
748 if (ret)
749 return ret;
750#endif
751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
753 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400754 if (ret)
755 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400757
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000759}
760module_init(ssm2602_modinit);
761
762static void __exit ssm2602_exit(void)
763{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400764#if defined(CONFIG_SPI_MASTER)
765 spi_unregister_driver(&ssm2602_spi_driver);
766#endif
767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000768#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
769 i2c_del_driver(&ssm2602_i2c_driver);
770#endif
Mark Brown64089b82008-12-08 19:17:58 +0000771}
772module_exit(ssm2602_exit);
773
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700774MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800775MODULE_AUTHOR("Cliff Cai");
776MODULE_LICENSE("GPL");