blob: 6f79cb0fbc63ea386c0b679349aa212d96eb0f67 [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
Philipp Zabel37768e32019-02-06 16:29:47 +010029#include <linux/delay.h>
Cliff Caib7138212008-09-05 18:09:57 +080030#include <linux/module.h>
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020031#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +010033
Cliff Caib7138212008-09-05 18:09:57 +080034#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/soc.h>
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070037#include <sound/tlv.h>
Cliff Caib7138212008-09-05 18:09:57 +080038
39#include "ssm2602.h"
40
Cliff Caib7138212008-09-05 18:09:57 +080041/* codec private data */
42struct ssm2602_priv {
43 unsigned int sysclk;
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +010044 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070045
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020046 struct regmap *regmap;
47
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070048 enum ssm2602_type type;
Lars-Peter Clausen02890532011-09-27 11:08:48 +020049 unsigned int clk_out_pwr;
Cliff Caib7138212008-09-05 18:09:57 +080050};
51
52/*
53 * ssm2602 register cache
54 * We can't read the ssm2602 register space when we are
55 * using 2 wire for device control, so we cache them instead.
56 * There is no point in caching the reset register
57 */
James Kellya01df752018-03-19 21:29:50 +110058static const struct reg_default ssm2602_reg[SSM2602_CACHEREGNUM] = {
59 { .reg = 0x00, .def = 0x0097 },
60 { .reg = 0x01, .def = 0x0097 },
61 { .reg = 0x02, .def = 0x0079 },
62 { .reg = 0x03, .def = 0x0079 },
63 { .reg = 0x04, .def = 0x000a },
64 { .reg = 0x05, .def = 0x0008 },
65 { .reg = 0x06, .def = 0x009f },
66 { .reg = 0x07, .def = 0x000a },
67 { .reg = 0x08, .def = 0x0000 },
68 { .reg = 0x09, .def = 0x0000 }
Cliff Caib7138212008-09-05 18:09:57 +080069};
70
Cliff Caib7138212008-09-05 18:09:57 +080071
72/*Appending several "None"s just for OSS mixer use*/
73static const char *ssm2602_input_select[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010074 "Line", "Mic",
Cliff Caib7138212008-09-05 18:09:57 +080075};
76
77static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
78
79static const struct soc_enum ssm2602_enum[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010080 SOC_ENUM_SINGLE(SSM2602_APANA, 2, ARRAY_SIZE(ssm2602_input_select),
81 ssm2602_input_select),
82 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, ARRAY_SIZE(ssm2602_deemph),
83 ssm2602_deemph),
Cliff Caib7138212008-09-05 18:09:57 +080084};
85
Lars-Peter Clausenf9d548022015-08-02 17:19:54 +020086static const DECLARE_TLV_DB_RANGE(ssm260x_outmix_tlv,
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070087 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
Lars-Peter Clausenf9d548022015-08-02 17:19:54 +020088 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0)
89);
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070090
91static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
92static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
93
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070094static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070095SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
96 ssm260x_inpga_tlv),
Cliff Caib7138212008-09-05 18:09:57 +080097SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
98
Cliff Caib7138212008-09-05 18:09:57 +080099SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
100SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
101
Cliff Caib7138212008-09-05 18:09:57 +0800102SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
103};
104
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700105static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700106SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
107 0, 127, 0, ssm260x_outmix_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700108SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
109 7, 1, 0),
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -0700110SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
111 ssm260x_sidetone_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700112
113SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
114SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700115};
116
Cliff Caib7138212008-09-05 18:09:57 +0800117/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700118static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800119SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800120SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700121SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800122};
123
Philipp Zabel37768e32019-02-06 16:29:47 +0100124static const struct snd_kcontrol_new mic_ctl =
125 SOC_DAPM_SINGLE("Switch", SSM2602_APANA, 1, 1, 1);
126
Cliff Caib7138212008-09-05 18:09:57 +0800127/* Input mux */
128static const struct snd_kcontrol_new ssm2602_input_mux_controls =
129SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
130
Philipp Zabel37768e32019-02-06 16:29:47 +0100131static int ssm2602_mic_switch_event(struct snd_soc_dapm_widget *w,
132 struct snd_kcontrol *kcontrol, int event)
133{
134 /*
135 * According to the ssm2603 data sheet (control register sequencing),
136 * the digital core should be activated only after all necessary bits
137 * in the power register are enabled, and a delay determined by the
138 * decoupling capacitor on the VMID pin has passed. If the digital core
139 * is activated too early, or even before the ADC is powered up, audible
140 * artifacts appear at the beginning and end of the recorded signal.
141 *
142 * In practice, audible artifacts disappear well over 500 ms.
143 */
144 msleep(500);
145
146 return 0;
147}
148
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700149static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800150SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800151SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800152SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700153
Mark Brown3afb1b3e2011-05-11 00:01:58 +0200154SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700155
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700156SND_SOC_DAPM_OUTPUT("LOUT"),
157SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800158SND_SOC_DAPM_INPUT("RLINEIN"),
159SND_SOC_DAPM_INPUT("LLINEIN"),
160};
161
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700162static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
163SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
164 ssm260x_output_mixer_controls,
165 ARRAY_SIZE(ssm260x_output_mixer_controls)),
166
167SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
168SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
169
Philipp Zabel37768e32019-02-06 16:29:47 +0100170SND_SOC_DAPM_SWITCH_E("Mic Switch", SSM2602_APANA, 1, 1, &mic_ctl,
171 ssm2602_mic_switch_event, SND_SOC_DAPM_PRE_PMU),
172
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700173SND_SOC_DAPM_OUTPUT("LHPOUT"),
174SND_SOC_DAPM_OUTPUT("RHPOUT"),
175SND_SOC_DAPM_INPUT("MICIN"),
176};
177
178static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
179SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
180 ssm260x_output_mixer_controls,
181 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
182};
183
184static const struct snd_soc_dapm_route ssm260x_routes[] = {
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700185 {"DAC", NULL, "Digital Core Power"},
186 {"ADC", NULL, "Digital Core Power"},
187
Cliff Caib7138212008-09-05 18:09:57 +0800188 {"Output Mixer", "Line Bypass Switch", "Line Input"},
189 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800190
Cliff Caib7138212008-09-05 18:09:57 +0800191 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800192 {"LOUT", NULL, "Output Mixer"},
193
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700194 {"Line Input", NULL, "LLINEIN"},
195 {"Line Input", NULL, "RLINEIN"},
196};
197
198static const struct snd_soc_dapm_route ssm2602_routes[] = {
199 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
200
201 {"RHPOUT", NULL, "Output Mixer"},
202 {"LHPOUT", NULL, "Output Mixer"},
203
Cliff Caib7138212008-09-05 18:09:57 +0800204 {"Input Mux", "Line", "Line Input"},
Philipp Zabel37768e32019-02-06 16:29:47 +0100205 {"Input Mux", "Mic", "Mic Switch"},
Cliff Caib7138212008-09-05 18:09:57 +0800206 {"ADC", NULL, "Input Mux"},
207
Philipp Zabel37768e32019-02-06 16:29:47 +0100208 {"Mic Switch", NULL, "Mic Bias"},
209
Cliff Caib7138212008-09-05 18:09:57 +0800210 {"Mic Bias", NULL, "MICIN"},
211};
212
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700213static const struct snd_soc_dapm_route ssm2604_routes[] = {
214 {"ADC", NULL, "Line Input"},
215};
Cliff Caib7138212008-09-05 18:09:57 +0800216
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200217static const unsigned int ssm2602_rates_12288000[] = {
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100218 8000, 16000, 32000, 48000, 96000,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200219};
220
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100221static const struct snd_pcm_hw_constraint_list ssm2602_constraints_12288000 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200222 .list = ssm2602_rates_12288000,
223 .count = ARRAY_SIZE(ssm2602_rates_12288000),
224};
225
226static const unsigned int ssm2602_rates_11289600[] = {
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300227 8000, 11025, 22050, 44100, 88200,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200228};
229
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100230static const struct snd_pcm_hw_constraint_list ssm2602_constraints_11289600 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200231 .list = ssm2602_rates_11289600,
232 .count = ARRAY_SIZE(ssm2602_rates_11289600),
233};
234
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200235struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800236 u32 mclk;
237 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200238 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800239};
240
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200241#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
242
243/* codec mclk clock coefficients */
244static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800245 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200246 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
247 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
248 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800249
250 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200251 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
252 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
253 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800254
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100255 /* 16k */
256 {12288000, 16000, SSM2602_COEFF_SRATE(0x5, 0x0, 0x0)},
257 {18432000, 16000, SSM2602_COEFF_SRATE(0x5, 0x1, 0x0)},
258 {12000000, 16000, SSM2602_COEFF_SRATE(0xa, 0x0, 0x1)},
259
Cliff Caib7138212008-09-05 18:09:57 +0800260 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200261 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
262 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
263 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
264 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
265 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800266
267 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200268 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
269 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
270 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800271
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300272 /* 11.025k */
273 {11289600, 11025, SSM2602_COEFF_SRATE(0xc, 0x0, 0x0)},
274 {16934400, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x0)},
275 {12000000, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x1)},
276
277 /* 22.05k */
278 {11289600, 22050, SSM2602_COEFF_SRATE(0xd, 0x0, 0x0)},
279 {16934400, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x0)},
280 {12000000, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x1)},
281
Cliff Caib7138212008-09-05 18:09:57 +0800282 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200283 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
284 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
285 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800286
287 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200288 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
289 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
290 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800291};
292
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200293static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800294{
295 int i;
296
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200297 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
298 if (ssm2602_coeff_table[i].rate == rate &&
299 ssm2602_coeff_table[i].mclk == mclk)
300 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800301 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200302 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800303}
304
305static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000306 struct snd_pcm_hw_params *params,
307 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800308{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000309 struct snd_soc_component *component = dai->component;
310 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200311 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200312 unsigned int iface;
Cliff Caib7138212008-09-05 18:09:57 +0800313
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200314 if (srate < 0)
315 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800316
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200317 regmap_write(ssm2602->regmap, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800318
319 /* bit size */
Mark Brown00a37032014-07-31 12:47:13 +0100320 switch (params_width(params)) {
321 case 16:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200322 iface = 0x0;
Cliff Caib7138212008-09-05 18:09:57 +0800323 break;
Mark Brown00a37032014-07-31 12:47:13 +0100324 case 20:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200325 iface = 0x4;
Cliff Caib7138212008-09-05 18:09:57 +0800326 break;
Mark Brown00a37032014-07-31 12:47:13 +0100327 case 24:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200328 iface = 0x8;
Cliff Caib7138212008-09-05 18:09:57 +0800329 break;
Mark Brown00a37032014-07-31 12:47:13 +0100330 case 32:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200331 iface = 0xc;
Cliff Caib7138212008-09-05 18:09:57 +0800332 break;
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200333 default:
334 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800335 }
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200336 regmap_update_bits(ssm2602->regmap, SSM2602_IFACE,
337 IFACE_AUDIO_DATA_LEN, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800338 return 0;
339}
340
Mark Browndee89c42008-11-18 22:11:38 +0000341static int ssm2602_startup(struct snd_pcm_substream *substream,
342 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800343{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000344 struct snd_soc_component *component = dai->component;
345 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800346
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200347 if (ssm2602->sysclk_constraints) {
348 snd_pcm_hw_constraint_list(substream->runtime, 0,
349 SNDRV_PCM_HW_PARAM_RATE,
350 ssm2602->sysclk_constraints);
351 }
352
Cliff Caib7138212008-09-05 18:09:57 +0800353 return 0;
354}
355
Cliff Caib7138212008-09-05 18:09:57 +0800356static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
357{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000358 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(dai->component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800359
Cliff Caib7138212008-09-05 18:09:57 +0800360 if (mute)
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200361 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800362 APDIGI_ENABLE_DAC_MUTE,
363 APDIGI_ENABLE_DAC_MUTE);
Cliff Caib7138212008-09-05 18:09:57 +0800364 else
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200365 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800366 APDIGI_ENABLE_DAC_MUTE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800367 return 0;
368}
369
370static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
371 int clk_id, unsigned int freq, int dir)
372{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000373 struct snd_soc_component *component = codec_dai->component;
374 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200375
376 if (dir == SND_SOC_CLOCK_IN) {
377 if (clk_id != SSM2602_SYSCLK)
378 return -EINVAL;
379
380 switch (freq) {
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200381 case 12288000:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200382 case 18432000:
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200383 ssm2602->sysclk_constraints = &ssm2602_constraints_12288000;
384 break;
385 case 11289600:
386 case 16934400:
387 ssm2602->sysclk_constraints = &ssm2602_constraints_11289600;
388 break;
389 case 12000000:
390 ssm2602->sysclk_constraints = NULL;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200391 break;
392 default:
393 return -EINVAL;
394 }
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200395 ssm2602->sysclk = freq;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200396 } else {
397 unsigned int mask;
398
399 switch (clk_id) {
400 case SSM2602_CLK_CLKOUT:
401 mask = PWR_CLK_OUT_PDN;
402 break;
403 case SSM2602_CLK_XTO:
404 mask = PWR_OSC_PDN;
405 break;
406 default:
407 return -EINVAL;
408 }
409
410 if (freq == 0)
411 ssm2602->clk_out_pwr |= mask;
412 else
413 ssm2602->clk_out_pwr &= ~mask;
414
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200415 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200416 PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800417 }
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200418
419 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800420}
421
422static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
423 unsigned int fmt)
424{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000425 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(codec_dai->component);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200426 unsigned int iface = 0;
Cliff Caib7138212008-09-05 18:09:57 +0800427
428 /* set master/slave audio interface */
429 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
430 case SND_SOC_DAIFMT_CBM_CFM:
431 iface |= 0x0040;
432 break;
433 case SND_SOC_DAIFMT_CBS_CFS:
434 break;
435 default:
436 return -EINVAL;
437 }
438
439 /* interface format */
440 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
441 case SND_SOC_DAIFMT_I2S:
442 iface |= 0x0002;
443 break;
444 case SND_SOC_DAIFMT_RIGHT_J:
445 break;
446 case SND_SOC_DAIFMT_LEFT_J:
447 iface |= 0x0001;
448 break;
449 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200450 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800451 break;
452 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200453 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800454 break;
455 default:
456 return -EINVAL;
457 }
458
459 /* clock inversion */
460 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
461 case SND_SOC_DAIFMT_NB_NF:
462 break;
463 case SND_SOC_DAIFMT_IB_IF:
464 iface |= 0x0090;
465 break;
466 case SND_SOC_DAIFMT_IB_NF:
467 iface |= 0x0080;
468 break;
469 case SND_SOC_DAIFMT_NB_IF:
470 iface |= 0x0010;
471 break;
472 default:
473 return -EINVAL;
474 }
475
476 /* set iface */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200477 regmap_write(ssm2602->regmap, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800478 return 0;
479}
480
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000481static int ssm2602_set_bias_level(struct snd_soc_component *component,
Cliff Caib7138212008-09-05 18:09:57 +0800482 enum snd_soc_bias_level level)
483{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000484 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800485
486 switch (level) {
487 case SND_SOC_BIAS_ON:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200488 /* vref/mid on, osc and clkout on if enabled */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200489 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200490 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
491 ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800492 break;
493 case SND_SOC_BIAS_PREPARE:
494 break;
495 case SND_SOC_BIAS_STANDBY:
496 /* everything off except vref/vmid, */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200497 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200498 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
499 PWR_CLK_OUT_PDN | PWR_OSC_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800500 break;
501 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200502 /* everything off */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200503 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200504 PWR_POWER_OFF, PWR_POWER_OFF);
Cliff Caib7138212008-09-05 18:09:57 +0800505 break;
506
507 }
Cliff Caib7138212008-09-05 18:09:57 +0800508 return 0;
509}
510
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300511#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
512 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100513 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
514 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
515 SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800516
Karl Beldan5de27b62008-11-20 15:39:31 +0100517#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
518 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
519
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100520static const struct snd_soc_dai_ops ssm2602_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800521 .startup = ssm2602_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800522 .hw_params = ssm2602_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +0800523 .digital_mute = ssm2602_mute,
524 .set_sysclk = ssm2602_set_dai_sysclk,
525 .set_fmt = ssm2602_set_dai_fmt,
526};
527
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528static struct snd_soc_dai_driver ssm2602_dai = {
529 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800530 .playback = {
531 .stream_name = "Playback",
532 .channels_min = 2,
533 .channels_max = 2,
534 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100535 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800536 .capture = {
537 .stream_name = "Capture",
538 .channels_min = 2,
539 .channels_max = 2,
540 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100541 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800542 .ops = &ssm2602_dai_ops,
Lars-Peter Clausena010ff62013-12-02 21:26:22 +0100543 .symmetric_rates = 1,
544 .symmetric_samplebits = 1,
Cliff Caib7138212008-09-05 18:09:57 +0800545};
Cliff Caib7138212008-09-05 18:09:57 +0800546
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000547static int ssm2602_resume(struct snd_soc_component *component)
Cliff Caib7138212008-09-05 18:09:57 +0800548{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000549 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800550
Lars-Peter Clausen9d863b882013-08-31 18:15:23 +0200551 regcache_sync(ssm2602->regmap);
Cliff Cai93547e82011-03-27 17:22:57 -0400552
Cliff Caib7138212008-09-05 18:09:57 +0800553 return 0;
554}
555
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000556static int ssm2602_component_probe(struct snd_soc_component *component)
Cliff Caib7138212008-09-05 18:09:57 +0800557{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000558 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
559 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800560 int ret;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700561
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200562 regmap_update_bits(ssm2602->regmap, SSM2602_LOUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800563 LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200564 regmap_update_bits(ssm2602->regmap, SSM2602_ROUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800565 ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700566
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000567 ret = snd_soc_add_component_controls(component, ssm2602_snd_controls,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700568 ARRAY_SIZE(ssm2602_snd_controls));
569 if (ret)
570 return ret;
571
572 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
573 ARRAY_SIZE(ssm2602_dapm_widgets));
574 if (ret)
575 return ret;
576
577 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
578 ARRAY_SIZE(ssm2602_routes));
579}
580
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000581static int ssm2604_component_probe(struct snd_soc_component *component)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700582{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000583 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700584 int ret;
585
586 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
587 ARRAY_SIZE(ssm2604_dapm_widgets));
588 if (ret)
589 return ret;
590
591 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
592 ARRAY_SIZE(ssm2604_routes));
593}
594
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000595static int ssm260x_component_probe(struct snd_soc_component *component)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700596{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000597 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800598 int ret;
Cliff Caib7138212008-09-05 18:09:57 +0800599
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200600 ret = regmap_write(ssm2602->regmap, SSM2602_RESET, 0);
Cliff Cai93547e82011-03-27 17:22:57 -0400601 if (ret < 0) {
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000602 dev_err(component->dev, "Failed to issue reset: %d\n", ret);
Cliff Cai93547e82011-03-27 17:22:57 -0400603 return ret;
604 }
Cliff Caib7138212008-09-05 18:09:57 +0800605
Cliff Caib7138212008-09-05 18:09:57 +0800606 /* set the update bits */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200607 regmap_update_bits(ssm2602->regmap, SSM2602_LINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800608 LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200609 regmap_update_bits(ssm2602->regmap, SSM2602_RINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800610 RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800611 /*select Line in as default input*/
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200612 regmap_write(ssm2602->regmap, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800613 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800614
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700615 switch (ssm2602->type) {
616 case SSM2602:
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000617 ret = ssm2602_component_probe(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700618 break;
619 case SSM2604:
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000620 ret = ssm2604_component_probe(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700621 break;
622 }
Cliff Caib7138212008-09-05 18:09:57 +0800623
Lars-Peter Clausen85362ef2014-09-04 19:44:12 +0200624 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625}
626
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000627static const struct snd_soc_component_driver soc_component_dev_ssm2602 = {
628 .probe = ssm260x_component_probe,
629 .resume = ssm2602_resume,
630 .set_bias_level = ssm2602_set_bias_level,
631 .controls = ssm260x_snd_controls,
632 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
633 .dapm_widgets = ssm260x_dapm_widgets,
634 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
635 .dapm_routes = ssm260x_routes,
636 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
637 .suspend_bias_off = 1,
638 .idle_bias_on = 1,
639 .use_pmdown_time = 1,
640 .endianness = 1,
641 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642};
Cliff Caib7138212008-09-05 18:09:57 +0800643
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200644static bool ssm2602_register_volatile(struct device *dev, unsigned int reg)
645{
646 return reg == SSM2602_RESET;
647}
648
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100649const struct regmap_config ssm2602_regmap_config = {
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200650 .val_bits = 9,
651 .reg_bits = 7,
652
653 .max_register = SSM2602_RESET,
654 .volatile_reg = ssm2602_register_volatile,
655
656 .cache_type = REGCACHE_RBTREE,
James Kellya01df752018-03-19 21:29:50 +1100657 .reg_defaults = ssm2602_reg,
658 .num_reg_defaults = ARRAY_SIZE(ssm2602_reg),
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200659};
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100660EXPORT_SYMBOL_GPL(ssm2602_regmap_config);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200661
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100662int ssm2602_probe(struct device *dev, enum ssm2602_type type,
663 struct regmap *regmap)
Mike Frysingerb39e2852011-04-07 02:05:11 -0400664{
665 struct ssm2602_priv *ssm2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400666
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100667 if (IS_ERR(regmap))
668 return PTR_ERR(regmap);
669
670 ssm2602 = devm_kzalloc(dev, sizeof(*ssm2602), GFP_KERNEL);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400671 if (ssm2602 == NULL)
672 return -ENOMEM;
673
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100674 dev_set_drvdata(dev, ssm2602);
Stefan Kristianssonfe2a08b2014-09-29 22:39:13 +0300675 ssm2602->type = type;
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100676 ssm2602->regmap = regmap;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400677
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000678 return devm_snd_soc_register_component(dev, &soc_component_dev_ssm2602,
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100679 &ssm2602_dai, 1);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400680}
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100681EXPORT_SYMBOL_GPL(ssm2602_probe);
Mark Brown64089b82008-12-08 19:17:58 +0000682
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700683MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800684MODULE_AUTHOR("Cliff Cai");
685MODULE_LICENSE("GPL");