blob: 314eaece1b7d6ac50bfb2d8af18ef1247bd3194c [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>
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020030#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +010032
Cliff Caib7138212008-09-05 18:09:57 +080033#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070036#include <sound/tlv.h>
Cliff Caib7138212008-09-05 18:09:57 +080037
38#include "ssm2602.h"
39
Cliff Caib7138212008-09-05 18:09:57 +080040/* codec private data */
41struct ssm2602_priv {
42 unsigned int sysclk;
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +010043 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070044
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020045 struct regmap *regmap;
46
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070047 enum ssm2602_type type;
Lars-Peter Clausen02890532011-09-27 11:08:48 +020048 unsigned int clk_out_pwr;
Cliff Caib7138212008-09-05 18:09:57 +080049};
50
51/*
52 * ssm2602 register cache
53 * We can't read the ssm2602 register space when we are
54 * using 2 wire for device control, so we cache them instead.
55 * There is no point in caching the reset register
56 */
57static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
Lars-Peter Clausen7164bdb2011-05-08 09:24:41 -070058 0x0097, 0x0097, 0x0079, 0x0079,
59 0x000a, 0x0008, 0x009f, 0x000a,
Cliff Caib7138212008-09-05 18:09:57 +080060 0x0000, 0x0000
61};
62
Cliff Caib7138212008-09-05 18:09:57 +080063
64/*Appending several "None"s just for OSS mixer use*/
65static const char *ssm2602_input_select[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010066 "Line", "Mic",
Cliff Caib7138212008-09-05 18:09:57 +080067};
68
69static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
70
71static const struct soc_enum ssm2602_enum[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010072 SOC_ENUM_SINGLE(SSM2602_APANA, 2, ARRAY_SIZE(ssm2602_input_select),
73 ssm2602_input_select),
74 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, ARRAY_SIZE(ssm2602_deemph),
75 ssm2602_deemph),
Cliff Caib7138212008-09-05 18:09:57 +080076};
77
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070078static const unsigned int ssm260x_outmix_tlv[] = {
79 TLV_DB_RANGE_HEAD(2),
80 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
81 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0),
82};
83
84static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
85static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
86
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070087static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070088SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
89 ssm260x_inpga_tlv),
Cliff Caib7138212008-09-05 18:09:57 +080090SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
91
Cliff Caib7138212008-09-05 18:09:57 +080092SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
93SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
94
Cliff Caib7138212008-09-05 18:09:57 +080095SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
96};
97
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070098static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070099SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
100 0, 127, 0, ssm260x_outmix_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700101SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
102 7, 1, 0),
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700103SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
104 ssm260x_sidetone_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700105
106SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
107SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
108SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
109};
110
Cliff Caib7138212008-09-05 18:09:57 +0800111/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700112static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800113SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800114SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700115SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800116};
117
118/* Input mux */
119static const struct snd_kcontrol_new ssm2602_input_mux_controls =
120SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
121
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700122static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800123SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800124SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800125SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700126
Mark Brown3afb1b3e2011-05-11 00:01:58 +0200127SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700128
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700129SND_SOC_DAPM_OUTPUT("LOUT"),
130SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800131SND_SOC_DAPM_INPUT("RLINEIN"),
132SND_SOC_DAPM_INPUT("LLINEIN"),
133};
134
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700135static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
136SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
137 ssm260x_output_mixer_controls,
138 ARRAY_SIZE(ssm260x_output_mixer_controls)),
139
140SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
141SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
142
143SND_SOC_DAPM_OUTPUT("LHPOUT"),
144SND_SOC_DAPM_OUTPUT("RHPOUT"),
145SND_SOC_DAPM_INPUT("MICIN"),
146};
147
148static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
149SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
150 ssm260x_output_mixer_controls,
151 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
152};
153
154static const struct snd_soc_dapm_route ssm260x_routes[] = {
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700155 {"DAC", NULL, "Digital Core Power"},
156 {"ADC", NULL, "Digital Core Power"},
157
Cliff Caib7138212008-09-05 18:09:57 +0800158 {"Output Mixer", "Line Bypass Switch", "Line Input"},
159 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800160
Cliff Caib7138212008-09-05 18:09:57 +0800161 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800162 {"LOUT", NULL, "Output Mixer"},
163
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700164 {"Line Input", NULL, "LLINEIN"},
165 {"Line Input", NULL, "RLINEIN"},
166};
167
168static const struct snd_soc_dapm_route ssm2602_routes[] = {
169 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
170
171 {"RHPOUT", NULL, "Output Mixer"},
172 {"LHPOUT", NULL, "Output Mixer"},
173
Cliff Caib7138212008-09-05 18:09:57 +0800174 {"Input Mux", "Line", "Line Input"},
175 {"Input Mux", "Mic", "Mic Bias"},
176 {"ADC", NULL, "Input Mux"},
177
Cliff Caib7138212008-09-05 18:09:57 +0800178 {"Mic Bias", NULL, "MICIN"},
179};
180
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700181static const struct snd_soc_dapm_route ssm2604_routes[] = {
182 {"ADC", NULL, "Line Input"},
183};
Cliff Caib7138212008-09-05 18:09:57 +0800184
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200185static const unsigned int ssm2602_rates_12288000[] = {
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100186 8000, 16000, 32000, 48000, 96000,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200187};
188
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100189static const struct snd_pcm_hw_constraint_list ssm2602_constraints_12288000 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200190 .list = ssm2602_rates_12288000,
191 .count = ARRAY_SIZE(ssm2602_rates_12288000),
192};
193
194static const unsigned int ssm2602_rates_11289600[] = {
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300195 8000, 11025, 22050, 44100, 88200,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200196};
197
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100198static const struct snd_pcm_hw_constraint_list ssm2602_constraints_11289600 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200199 .list = ssm2602_rates_11289600,
200 .count = ARRAY_SIZE(ssm2602_rates_11289600),
201};
202
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200203struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800204 u32 mclk;
205 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200206 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800207};
208
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200209#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
210
211/* codec mclk clock coefficients */
212static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800213 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200214 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
215 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
216 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800217
218 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200219 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
220 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
221 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800222
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100223 /* 16k */
224 {12288000, 16000, SSM2602_COEFF_SRATE(0x5, 0x0, 0x0)},
225 {18432000, 16000, SSM2602_COEFF_SRATE(0x5, 0x1, 0x0)},
226 {12000000, 16000, SSM2602_COEFF_SRATE(0xa, 0x0, 0x1)},
227
Cliff Caib7138212008-09-05 18:09:57 +0800228 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200229 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
230 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
231 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
232 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
233 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800234
235 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200236 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
237 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
238 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800239
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300240 /* 11.025k */
241 {11289600, 11025, SSM2602_COEFF_SRATE(0xc, 0x0, 0x0)},
242 {16934400, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x0)},
243 {12000000, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x1)},
244
245 /* 22.05k */
246 {11289600, 22050, SSM2602_COEFF_SRATE(0xd, 0x0, 0x0)},
247 {16934400, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x0)},
248 {12000000, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x1)},
249
Cliff Caib7138212008-09-05 18:09:57 +0800250 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200251 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
252 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
253 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800254
255 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200256 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
257 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
258 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800259};
260
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200261static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800262{
263 int i;
264
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200265 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
266 if (ssm2602_coeff_table[i].rate == rate &&
267 ssm2602_coeff_table[i].mclk == mclk)
268 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800269 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200270 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800271}
272
273static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000274 struct snd_pcm_hw_params *params,
275 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800276{
Mark Browne6968a12012-04-04 15:58:16 +0100277 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900278 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200279 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200280 unsigned int iface;
Cliff Caib7138212008-09-05 18:09:57 +0800281
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200282 if (srate < 0)
283 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800284
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200285 regmap_write(ssm2602->regmap, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800286
287 /* bit size */
Mark Brown00a37032014-07-31 12:47:13 +0100288 switch (params_width(params)) {
289 case 16:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200290 iface = 0x0;
Cliff Caib7138212008-09-05 18:09:57 +0800291 break;
Mark Brown00a37032014-07-31 12:47:13 +0100292 case 20:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200293 iface = 0x4;
Cliff Caib7138212008-09-05 18:09:57 +0800294 break;
Mark Brown00a37032014-07-31 12:47:13 +0100295 case 24:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200296 iface = 0x8;
Cliff Caib7138212008-09-05 18:09:57 +0800297 break;
Mark Brown00a37032014-07-31 12:47:13 +0100298 case 32:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200299 iface = 0xc;
Cliff Caib7138212008-09-05 18:09:57 +0800300 break;
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200301 default:
302 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800303 }
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200304 regmap_update_bits(ssm2602->regmap, SSM2602_IFACE,
305 IFACE_AUDIO_DATA_LEN, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800306 return 0;
307}
308
Mark Browndee89c42008-11-18 22:11:38 +0000309static int ssm2602_startup(struct snd_pcm_substream *substream,
310 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800311{
Mark Browne6968a12012-04-04 15:58:16 +0100312 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900313 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800314
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200315 if (ssm2602->sysclk_constraints) {
316 snd_pcm_hw_constraint_list(substream->runtime, 0,
317 SNDRV_PCM_HW_PARAM_RATE,
318 ssm2602->sysclk_constraints);
319 }
320
Cliff Caib7138212008-09-05 18:09:57 +0800321 return 0;
322}
323
Cliff Caib7138212008-09-05 18:09:57 +0800324static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
325{
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200326 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(dai->codec);
Axel Lin2ee9c182011-10-19 14:07:31 +0800327
Cliff Caib7138212008-09-05 18:09:57 +0800328 if (mute)
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200329 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800330 APDIGI_ENABLE_DAC_MUTE,
331 APDIGI_ENABLE_DAC_MUTE);
Cliff Caib7138212008-09-05 18:09:57 +0800332 else
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200333 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800334 APDIGI_ENABLE_DAC_MUTE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800335 return 0;
336}
337
338static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
339 int clk_id, unsigned int freq, int dir)
340{
341 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900342 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200343
344 if (dir == SND_SOC_CLOCK_IN) {
345 if (clk_id != SSM2602_SYSCLK)
346 return -EINVAL;
347
348 switch (freq) {
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200349 case 12288000:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200350 case 18432000:
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200351 ssm2602->sysclk_constraints = &ssm2602_constraints_12288000;
352 break;
353 case 11289600:
354 case 16934400:
355 ssm2602->sysclk_constraints = &ssm2602_constraints_11289600;
356 break;
357 case 12000000:
358 ssm2602->sysclk_constraints = NULL;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200359 break;
360 default:
361 return -EINVAL;
362 }
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200363 ssm2602->sysclk = freq;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200364 } else {
365 unsigned int mask;
366
367 switch (clk_id) {
368 case SSM2602_CLK_CLKOUT:
369 mask = PWR_CLK_OUT_PDN;
370 break;
371 case SSM2602_CLK_XTO:
372 mask = PWR_OSC_PDN;
373 break;
374 default:
375 return -EINVAL;
376 }
377
378 if (freq == 0)
379 ssm2602->clk_out_pwr |= mask;
380 else
381 ssm2602->clk_out_pwr &= ~mask;
382
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200383 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200384 PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800385 }
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200386
387 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800388}
389
390static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
391 unsigned int fmt)
392{
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200393 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec_dai->codec);
394 unsigned int iface = 0;
Cliff Caib7138212008-09-05 18:09:57 +0800395
396 /* set master/slave audio interface */
397 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
398 case SND_SOC_DAIFMT_CBM_CFM:
399 iface |= 0x0040;
400 break;
401 case SND_SOC_DAIFMT_CBS_CFS:
402 break;
403 default:
404 return -EINVAL;
405 }
406
407 /* interface format */
408 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
409 case SND_SOC_DAIFMT_I2S:
410 iface |= 0x0002;
411 break;
412 case SND_SOC_DAIFMT_RIGHT_J:
413 break;
414 case SND_SOC_DAIFMT_LEFT_J:
415 iface |= 0x0001;
416 break;
417 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200418 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800419 break;
420 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200421 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800422 break;
423 default:
424 return -EINVAL;
425 }
426
427 /* clock inversion */
428 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
429 case SND_SOC_DAIFMT_NB_NF:
430 break;
431 case SND_SOC_DAIFMT_IB_IF:
432 iface |= 0x0090;
433 break;
434 case SND_SOC_DAIFMT_IB_NF:
435 iface |= 0x0080;
436 break;
437 case SND_SOC_DAIFMT_NB_IF:
438 iface |= 0x0010;
439 break;
440 default:
441 return -EINVAL;
442 }
443
444 /* set iface */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200445 regmap_write(ssm2602->regmap, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800446 return 0;
447}
448
449static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
450 enum snd_soc_bias_level level)
451{
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200452 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800453
454 switch (level) {
455 case SND_SOC_BIAS_ON:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200456 /* vref/mid on, osc and clkout on if enabled */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200457 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200458 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
459 ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800460 break;
461 case SND_SOC_BIAS_PREPARE:
462 break;
463 case SND_SOC_BIAS_STANDBY:
464 /* everything off except vref/vmid, */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200465 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200466 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
467 PWR_CLK_OUT_PDN | PWR_OSC_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800468 break;
469 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200470 /* everything off */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200471 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200472 PWR_POWER_OFF, PWR_POWER_OFF);
Cliff Caib7138212008-09-05 18:09:57 +0800473 break;
474
475 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200476 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800477 return 0;
478}
479
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300480#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
481 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100482 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
483 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
484 SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800485
Karl Beldan5de27b62008-11-20 15:39:31 +0100486#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
487 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
488
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100489static const struct snd_soc_dai_ops ssm2602_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800490 .startup = ssm2602_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800491 .hw_params = ssm2602_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +0800492 .digital_mute = ssm2602_mute,
493 .set_sysclk = ssm2602_set_dai_sysclk,
494 .set_fmt = ssm2602_set_dai_fmt,
495};
496
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497static struct snd_soc_dai_driver ssm2602_dai = {
498 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800499 .playback = {
500 .stream_name = "Playback",
501 .channels_min = 2,
502 .channels_max = 2,
503 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100504 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800505 .capture = {
506 .stream_name = "Capture",
507 .channels_min = 2,
508 .channels_max = 2,
509 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100510 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800511 .ops = &ssm2602_dai_ops,
Lars-Peter Clausena010ff62013-12-02 21:26:22 +0100512 .symmetric_rates = 1,
513 .symmetric_samplebits = 1,
Cliff Caib7138212008-09-05 18:09:57 +0800514};
Cliff Caib7138212008-09-05 18:09:57 +0800515
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800517{
Lars-Peter Clausen9d863b882013-08-31 18:15:23 +0200518 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800519
Lars-Peter Clausen9d863b882013-08-31 18:15:23 +0200520 regcache_sync(ssm2602->regmap);
Cliff Cai93547e82011-03-27 17:22:57 -0400521
Cliff Caib7138212008-09-05 18:09:57 +0800522 return 0;
523}
524
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100525static int ssm2602_codec_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800526{
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200527 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700528 struct snd_soc_dapm_context *dapm = &codec->dapm;
Axel Lin2ee9c182011-10-19 14:07:31 +0800529 int ret;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700530
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200531 regmap_update_bits(ssm2602->regmap, SSM2602_LOUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800532 LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200533 regmap_update_bits(ssm2602->regmap, SSM2602_ROUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800534 ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700535
Liam Girdwood022658b2012-02-03 17:43:09 +0000536 ret = snd_soc_add_codec_controls(codec, ssm2602_snd_controls,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700537 ARRAY_SIZE(ssm2602_snd_controls));
538 if (ret)
539 return ret;
540
541 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
542 ARRAY_SIZE(ssm2602_dapm_widgets));
543 if (ret)
544 return ret;
545
546 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
547 ARRAY_SIZE(ssm2602_routes));
548}
549
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100550static int ssm2604_codec_probe(struct snd_soc_codec *codec)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700551{
552 struct snd_soc_dapm_context *dapm = &codec->dapm;
553 int ret;
554
555 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
556 ARRAY_SIZE(ssm2604_dapm_widgets));
557 if (ret)
558 return ret;
559
560 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
561 ARRAY_SIZE(ssm2604_routes));
562}
563
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100564static int ssm260x_codec_probe(struct snd_soc_codec *codec)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700565{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Axel Lin2ee9c182011-10-19 14:07:31 +0800567 int ret;
Cliff Caib7138212008-09-05 18:09:57 +0800568
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200569 ret = regmap_write(ssm2602->regmap, SSM2602_RESET, 0);
Cliff Cai93547e82011-03-27 17:22:57 -0400570 if (ret < 0) {
571 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
572 return ret;
573 }
Cliff Caib7138212008-09-05 18:09:57 +0800574
Cliff Caib7138212008-09-05 18:09:57 +0800575 /* set the update bits */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200576 regmap_update_bits(ssm2602->regmap, SSM2602_LINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800577 LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200578 regmap_update_bits(ssm2602->regmap, SSM2602_RINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800579 RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800580 /*select Line in as default input*/
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200581 regmap_write(ssm2602->regmap, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800582 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800583
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700584 switch (ssm2602->type) {
585 case SSM2602:
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100586 ret = ssm2602_codec_probe(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700587 break;
588 case SSM2604:
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100589 ret = ssm2604_codec_probe(codec);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700590 break;
591 }
Cliff Caib7138212008-09-05 18:09:57 +0800592
Lars-Peter Clausen85362ef2014-09-04 19:44:12 +0200593 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594}
595
596static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100597 .probe = ssm260x_codec_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 .set_bias_level = ssm2602_set_bias_level,
Lars-Peter Clausen85362ef2014-09-04 19:44:12 +0200600 .suspend_bias_off = true,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700601
602 .controls = ssm260x_snd_controls,
603 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
604 .dapm_widgets = ssm260x_dapm_widgets,
605 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
606 .dapm_routes = ssm260x_routes,
607 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608};
Cliff Caib7138212008-09-05 18:09:57 +0800609
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200610static bool ssm2602_register_volatile(struct device *dev, unsigned int reg)
611{
612 return reg == SSM2602_RESET;
613}
614
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100615const struct regmap_config ssm2602_regmap_config = {
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200616 .val_bits = 9,
617 .reg_bits = 7,
618
619 .max_register = SSM2602_RESET,
620 .volatile_reg = ssm2602_register_volatile,
621
622 .cache_type = REGCACHE_RBTREE,
623 .reg_defaults_raw = ssm2602_reg,
624 .num_reg_defaults_raw = ARRAY_SIZE(ssm2602_reg),
625};
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100626EXPORT_SYMBOL_GPL(ssm2602_regmap_config);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200627
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100628int ssm2602_probe(struct device *dev, enum ssm2602_type type,
629 struct regmap *regmap)
Mike Frysingerb39e2852011-04-07 02:05:11 -0400630{
631 struct ssm2602_priv *ssm2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400632
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100633 if (IS_ERR(regmap))
634 return PTR_ERR(regmap);
635
636 ssm2602 = devm_kzalloc(dev, sizeof(*ssm2602), GFP_KERNEL);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400637 if (ssm2602 == NULL)
638 return -ENOMEM;
639
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100640 dev_set_drvdata(dev, ssm2602);
Stefan Kristianssonfe2a08b2014-09-29 22:39:13 +0300641 ssm2602->type = type;
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100642 ssm2602->regmap = regmap;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400643
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100644 return snd_soc_register_codec(dev, &soc_codec_dev_ssm2602,
645 &ssm2602_dai, 1);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400646}
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100647EXPORT_SYMBOL_GPL(ssm2602_probe);
Mark Brown64089b82008-12-08 19:17:58 +0000648
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700649MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800650MODULE_AUTHOR("Cliff Cai");
651MODULE_LICENSE("GPL");