blob: 464a4d7873bbad173fffd64b04d1589e2d40da97 [file] [log] [blame]
Marco Felsch47306402019-02-06 18:00:03 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2//
3// File: sound/soc/codecs/ssm2602.c
4// Author: Cliff Cai <Cliff.Cai@analog.com>
5//
6// Created: Tue June 06 2008
7// Description: Driver for ssm2602 sound chip
8//
9// Modified:
10// Copyright 2008 Analog Devices Inc.
11//
12// Bugs: Enter bugs at http://blackfin.uclinux.org/
Cliff Caib7138212008-09-05 18:09:57 +080013
Philipp Zabel37768e32019-02-06 16:29:47 +010014#include <linux/delay.h>
Cliff Caib7138212008-09-05 18:09:57 +080015#include <linux/module.h>
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020016#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +010018
Cliff Caib7138212008-09-05 18:09:57 +080019#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070022#include <sound/tlv.h>
Cliff Caib7138212008-09-05 18:09:57 +080023
24#include "ssm2602.h"
25
Cliff Caib7138212008-09-05 18:09:57 +080026/* codec private data */
27struct ssm2602_priv {
28 unsigned int sysclk;
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +010029 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070030
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +020031 struct regmap *regmap;
32
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070033 enum ssm2602_type type;
Lars-Peter Clausen02890532011-09-27 11:08:48 +020034 unsigned int clk_out_pwr;
Cliff Caib7138212008-09-05 18:09:57 +080035};
36
37/*
38 * ssm2602 register cache
39 * We can't read the ssm2602 register space when we are
40 * using 2 wire for device control, so we cache them instead.
41 * There is no point in caching the reset register
42 */
James Kellya01df752018-03-19 21:29:50 +110043static const struct reg_default ssm2602_reg[SSM2602_CACHEREGNUM] = {
44 { .reg = 0x00, .def = 0x0097 },
45 { .reg = 0x01, .def = 0x0097 },
46 { .reg = 0x02, .def = 0x0079 },
47 { .reg = 0x03, .def = 0x0079 },
48 { .reg = 0x04, .def = 0x000a },
49 { .reg = 0x05, .def = 0x0008 },
50 { .reg = 0x06, .def = 0x009f },
51 { .reg = 0x07, .def = 0x000a },
52 { .reg = 0x08, .def = 0x0000 },
53 { .reg = 0x09, .def = 0x0000 }
Cliff Caib7138212008-09-05 18:09:57 +080054};
55
Cliff Caib7138212008-09-05 18:09:57 +080056
57/*Appending several "None"s just for OSS mixer use*/
58static const char *ssm2602_input_select[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010059 "Line", "Mic",
Cliff Caib7138212008-09-05 18:09:57 +080060};
61
62static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
63
64static const struct soc_enum ssm2602_enum[] = {
Takashi Iwaib38fbe32014-02-18 09:17:44 +010065 SOC_ENUM_SINGLE(SSM2602_APANA, 2, ARRAY_SIZE(ssm2602_input_select),
66 ssm2602_input_select),
67 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, ARRAY_SIZE(ssm2602_deemph),
68 ssm2602_deemph),
Cliff Caib7138212008-09-05 18:09:57 +080069};
70
Lars-Peter Clausenf9d548022015-08-02 17:19:54 +020071static const DECLARE_TLV_DB_RANGE(ssm260x_outmix_tlv,
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070072 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
Lars-Peter Clausenf9d548022015-08-02 17:19:54 +020073 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0)
74);
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070075
76static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
77static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
78
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070079static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070080SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
81 ssm260x_inpga_tlv),
Cliff Caib7138212008-09-05 18:09:57 +080082SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
83
Cliff Caib7138212008-09-05 18:09:57 +080084SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
85SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
86
Cliff Caib7138212008-09-05 18:09:57 +080087SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
88};
89
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070090static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070091SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
92 0, 127, 0, ssm260x_outmix_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070093SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
94 7, 1, 0),
Lars-Peter Clausenf3eee002011-05-08 09:24:46 -070095SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
96 ssm260x_sidetone_tlv),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -070097
98SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
99SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700100};
101
Cliff Caib7138212008-09-05 18:09:57 +0800102/* Output Mixer */
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700103static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800104SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800105SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700106SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +0800107};
108
Philipp Zabel37768e32019-02-06 16:29:47 +0100109static const struct snd_kcontrol_new mic_ctl =
110 SOC_DAPM_SINGLE("Switch", SSM2602_APANA, 1, 1, 1);
111
Cliff Caib7138212008-09-05 18:09:57 +0800112/* Input mux */
113static const struct snd_kcontrol_new ssm2602_input_mux_controls =
114SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
115
Philipp Zabel37768e32019-02-06 16:29:47 +0100116static int ssm2602_mic_switch_event(struct snd_soc_dapm_widget *w,
117 struct snd_kcontrol *kcontrol, int event)
118{
119 /*
120 * According to the ssm2603 data sheet (control register sequencing),
121 * the digital core should be activated only after all necessary bits
122 * in the power register are enabled, and a delay determined by the
123 * decoupling capacitor on the VMID pin has passed. If the digital core
124 * is activated too early, or even before the ADC is powered up, audible
125 * artifacts appear at the beginning and end of the recorded signal.
126 *
127 * In practice, audible artifacts disappear well over 500 ms.
128 */
129 msleep(500);
130
131 return 0;
132}
133
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700134static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800135SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800136SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
Cliff Caib7138212008-09-05 18:09:57 +0800137SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700138
Mark Brown3afb1b3e2011-05-11 00:01:58 +0200139SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700140
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700141SND_SOC_DAPM_OUTPUT("LOUT"),
142SND_SOC_DAPM_OUTPUT("ROUT"),
Cliff Caib7138212008-09-05 18:09:57 +0800143SND_SOC_DAPM_INPUT("RLINEIN"),
144SND_SOC_DAPM_INPUT("LLINEIN"),
145};
146
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700147static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
148SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
149 ssm260x_output_mixer_controls,
150 ARRAY_SIZE(ssm260x_output_mixer_controls)),
151
152SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
153SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
154
Philipp Zabel37768e32019-02-06 16:29:47 +0100155SND_SOC_DAPM_SWITCH_E("Mic Switch", SSM2602_APANA, 1, 1, &mic_ctl,
156 ssm2602_mic_switch_event, SND_SOC_DAPM_PRE_PMU),
157
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700158SND_SOC_DAPM_OUTPUT("LHPOUT"),
159SND_SOC_DAPM_OUTPUT("RHPOUT"),
160SND_SOC_DAPM_INPUT("MICIN"),
161};
162
163static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
164SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
165 ssm260x_output_mixer_controls,
166 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
167};
168
169static const struct snd_soc_dapm_route ssm260x_routes[] = {
Lars-Peter Clausen2a438012011-05-08 09:24:45 -0700170 {"DAC", NULL, "Digital Core Power"},
171 {"ADC", NULL, "Digital Core Power"},
172
Cliff Caib7138212008-09-05 18:09:57 +0800173 {"Output Mixer", "Line Bypass Switch", "Line Input"},
174 {"Output Mixer", "HiFi Playback Switch", "DAC"},
Cliff Caib7138212008-09-05 18:09:57 +0800175
Cliff Caib7138212008-09-05 18:09:57 +0800176 {"ROUT", NULL, "Output Mixer"},
Cliff Caib7138212008-09-05 18:09:57 +0800177 {"LOUT", NULL, "Output Mixer"},
178
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700179 {"Line Input", NULL, "LLINEIN"},
180 {"Line Input", NULL, "RLINEIN"},
181};
182
183static const struct snd_soc_dapm_route ssm2602_routes[] = {
184 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
185
186 {"RHPOUT", NULL, "Output Mixer"},
187 {"LHPOUT", NULL, "Output Mixer"},
188
Cliff Caib7138212008-09-05 18:09:57 +0800189 {"Input Mux", "Line", "Line Input"},
Philipp Zabel37768e32019-02-06 16:29:47 +0100190 {"Input Mux", "Mic", "Mic Switch"},
Cliff Caib7138212008-09-05 18:09:57 +0800191 {"ADC", NULL, "Input Mux"},
192
Philipp Zabel37768e32019-02-06 16:29:47 +0100193 {"Mic Switch", NULL, "Mic Bias"},
194
Cliff Caib7138212008-09-05 18:09:57 +0800195 {"Mic Bias", NULL, "MICIN"},
196};
197
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700198static const struct snd_soc_dapm_route ssm2604_routes[] = {
199 {"ADC", NULL, "Line Input"},
200};
Cliff Caib7138212008-09-05 18:09:57 +0800201
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200202static const unsigned int ssm2602_rates_12288000[] = {
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100203 8000, 16000, 32000, 48000, 96000,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200204};
205
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100206static const struct snd_pcm_hw_constraint_list ssm2602_constraints_12288000 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200207 .list = ssm2602_rates_12288000,
208 .count = ARRAY_SIZE(ssm2602_rates_12288000),
209};
210
211static const unsigned int ssm2602_rates_11289600[] = {
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300212 8000, 11025, 22050, 44100, 88200,
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200213};
214
Lars-Peter Clausenf75ac2d2014-02-05 21:54:33 +0100215static const struct snd_pcm_hw_constraint_list ssm2602_constraints_11289600 = {
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200216 .list = ssm2602_rates_11289600,
217 .count = ARRAY_SIZE(ssm2602_rates_11289600),
218};
219
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200220struct ssm2602_coeff {
Cliff Caib7138212008-09-05 18:09:57 +0800221 u32 mclk;
222 u32 rate;
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200223 u8 srate;
Cliff Caib7138212008-09-05 18:09:57 +0800224};
225
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200226#define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
227
228/* codec mclk clock coefficients */
229static const struct ssm2602_coeff ssm2602_coeff_table[] = {
Cliff Caib7138212008-09-05 18:09:57 +0800230 /* 48k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200231 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
232 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
233 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800234
235 /* 32k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200236 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
237 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
238 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800239
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100240 /* 16k */
241 {12288000, 16000, SSM2602_COEFF_SRATE(0x5, 0x0, 0x0)},
242 {18432000, 16000, SSM2602_COEFF_SRATE(0x5, 0x1, 0x0)},
243 {12000000, 16000, SSM2602_COEFF_SRATE(0xa, 0x0, 0x1)},
244
Cliff Caib7138212008-09-05 18:09:57 +0800245 /* 8k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200246 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
247 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
248 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
249 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
250 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800251
252 /* 96k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200253 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
254 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
255 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800256
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300257 /* 11.025k */
258 {11289600, 11025, SSM2602_COEFF_SRATE(0xc, 0x0, 0x0)},
259 {16934400, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x0)},
260 {12000000, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x1)},
261
262 /* 22.05k */
263 {11289600, 22050, SSM2602_COEFF_SRATE(0xd, 0x0, 0x0)},
264 {16934400, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x0)},
265 {12000000, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x1)},
266
Cliff Caib7138212008-09-05 18:09:57 +0800267 /* 44.1k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200268 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
269 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
270 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800271
272 /* 88.2k */
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200273 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
274 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
275 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
Cliff Caib7138212008-09-05 18:09:57 +0800276};
277
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200278static inline int ssm2602_get_coeff(int mclk, int rate)
Cliff Caib7138212008-09-05 18:09:57 +0800279{
280 int i;
281
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200282 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
283 if (ssm2602_coeff_table[i].rate == rate &&
284 ssm2602_coeff_table[i].mclk == mclk)
285 return ssm2602_coeff_table[i].srate;
Cliff Caib7138212008-09-05 18:09:57 +0800286 }
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200287 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800288}
289
290static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000291 struct snd_pcm_hw_params *params,
292 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800293{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000294 struct snd_soc_component *component = dai->component;
295 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200296 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200297 unsigned int iface;
Cliff Caib7138212008-09-05 18:09:57 +0800298
Lars-Peter Clausen0b4cd2e2011-05-05 16:59:10 +0200299 if (srate < 0)
300 return srate;
Cliff Caib7138212008-09-05 18:09:57 +0800301
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200302 regmap_write(ssm2602->regmap, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800303
304 /* bit size */
Mark Brown00a37032014-07-31 12:47:13 +0100305 switch (params_width(params)) {
306 case 16:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200307 iface = 0x0;
Cliff Caib7138212008-09-05 18:09:57 +0800308 break;
Mark Brown00a37032014-07-31 12:47:13 +0100309 case 20:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200310 iface = 0x4;
Cliff Caib7138212008-09-05 18:09:57 +0800311 break;
Mark Brown00a37032014-07-31 12:47:13 +0100312 case 24:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200313 iface = 0x8;
Cliff Caib7138212008-09-05 18:09:57 +0800314 break;
Mark Brown00a37032014-07-31 12:47:13 +0100315 case 32:
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200316 iface = 0xc;
Cliff Caib7138212008-09-05 18:09:57 +0800317 break;
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200318 default:
319 return -EINVAL;
Cliff Caib7138212008-09-05 18:09:57 +0800320 }
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200321 regmap_update_bits(ssm2602->regmap, SSM2602_IFACE,
322 IFACE_AUDIO_DATA_LEN, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800323 return 0;
324}
325
Mark Browndee89c42008-11-18 22:11:38 +0000326static int ssm2602_startup(struct snd_pcm_substream *substream,
327 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800328{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000329 struct snd_soc_component *component = dai->component;
330 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800331
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200332 if (ssm2602->sysclk_constraints) {
333 snd_pcm_hw_constraint_list(substream->runtime, 0,
334 SNDRV_PCM_HW_PARAM_RATE,
335 ssm2602->sysclk_constraints);
336 }
337
Cliff Caib7138212008-09-05 18:09:57 +0800338 return 0;
339}
340
Cliff Caib7138212008-09-05 18:09:57 +0800341static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
342{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000343 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(dai->component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800344
Cliff Caib7138212008-09-05 18:09:57 +0800345 if (mute)
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200346 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800347 APDIGI_ENABLE_DAC_MUTE,
348 APDIGI_ENABLE_DAC_MUTE);
Cliff Caib7138212008-09-05 18:09:57 +0800349 else
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200350 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
Axel Lin2ee9c182011-10-19 14:07:31 +0800351 APDIGI_ENABLE_DAC_MUTE, 0);
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{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000358 struct snd_soc_component *component = codec_dai->component;
359 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
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) {
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200366 case 12288000:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200367 case 18432000:
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200368 ssm2602->sysclk_constraints = &ssm2602_constraints_12288000;
369 break;
370 case 11289600:
371 case 16934400:
372 ssm2602->sysclk_constraints = &ssm2602_constraints_11289600;
373 break;
374 case 12000000:
375 ssm2602->sysclk_constraints = NULL;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200376 break;
377 default:
378 return -EINVAL;
379 }
Lars-Peter Clausen8b3f39d2012-04-25 11:42:46 +0200380 ssm2602->sysclk = freq;
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200381 } else {
382 unsigned int mask;
383
384 switch (clk_id) {
385 case SSM2602_CLK_CLKOUT:
386 mask = PWR_CLK_OUT_PDN;
387 break;
388 case SSM2602_CLK_XTO:
389 mask = PWR_OSC_PDN;
390 break;
391 default:
392 return -EINVAL;
393 }
394
395 if (freq == 0)
396 ssm2602->clk_out_pwr |= mask;
397 else
398 ssm2602->clk_out_pwr &= ~mask;
399
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200400 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200401 PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800402 }
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200403
404 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800405}
406
407static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
408 unsigned int fmt)
409{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000410 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(codec_dai->component);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200411 unsigned int iface = 0;
Cliff Caib7138212008-09-05 18:09:57 +0800412
413 /* set master/slave audio interface */
414 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
415 case SND_SOC_DAIFMT_CBM_CFM:
416 iface |= 0x0040;
417 break;
418 case SND_SOC_DAIFMT_CBS_CFS:
419 break;
420 default:
421 return -EINVAL;
422 }
423
424 /* interface format */
425 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
426 case SND_SOC_DAIFMT_I2S:
427 iface |= 0x0002;
428 break;
429 case SND_SOC_DAIFMT_RIGHT_J:
430 break;
431 case SND_SOC_DAIFMT_LEFT_J:
432 iface |= 0x0001;
433 break;
434 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200435 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800436 break;
437 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200438 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800439 break;
440 default:
441 return -EINVAL;
442 }
443
444 /* clock inversion */
445 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
446 case SND_SOC_DAIFMT_NB_NF:
447 break;
448 case SND_SOC_DAIFMT_IB_IF:
449 iface |= 0x0090;
450 break;
451 case SND_SOC_DAIFMT_IB_NF:
452 iface |= 0x0080;
453 break;
454 case SND_SOC_DAIFMT_NB_IF:
455 iface |= 0x0010;
456 break;
457 default:
458 return -EINVAL;
459 }
460
461 /* set iface */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200462 regmap_write(ssm2602->regmap, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800463 return 0;
464}
465
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000466static int ssm2602_set_bias_level(struct snd_soc_component *component,
Cliff Caib7138212008-09-05 18:09:57 +0800467 enum snd_soc_bias_level level)
468{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000469 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800470
471 switch (level) {
472 case SND_SOC_BIAS_ON:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200473 /* vref/mid on, osc and clkout on if enabled */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200474 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200475 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
476 ssm2602->clk_out_pwr);
Cliff Caib7138212008-09-05 18:09:57 +0800477 break;
478 case SND_SOC_BIAS_PREPARE:
479 break;
480 case SND_SOC_BIAS_STANDBY:
481 /* everything off except vref/vmid, */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200482 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200483 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
484 PWR_CLK_OUT_PDN | PWR_OSC_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800485 break;
486 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200487 /* everything off */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200488 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
Lars-Peter Clausen02890532011-09-27 11:08:48 +0200489 PWR_POWER_OFF, PWR_POWER_OFF);
Cliff Caib7138212008-09-05 18:09:57 +0800490 break;
491
492 }
Cliff Caib7138212008-09-05 18:09:57 +0800493 return 0;
494}
495
Stefan Kristiansson3b2a0012014-09-29 22:41:37 +0300496#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
497 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
Andreas Pretzsch7ae2b552014-01-07 22:34:41 +0100498 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
499 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
500 SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800501
Karl Beldan5de27b62008-11-20 15:39:31 +0100502#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
503 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
504
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100505static const struct snd_soc_dai_ops ssm2602_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800506 .startup = ssm2602_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800507 .hw_params = ssm2602_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +0800508 .digital_mute = ssm2602_mute,
509 .set_sysclk = ssm2602_set_dai_sysclk,
510 .set_fmt = ssm2602_set_dai_fmt,
511};
512
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513static struct snd_soc_dai_driver ssm2602_dai = {
514 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800515 .playback = {
516 .stream_name = "Playback",
517 .channels_min = 2,
518 .channels_max = 2,
519 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100520 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800521 .capture = {
522 .stream_name = "Capture",
523 .channels_min = 2,
524 .channels_max = 2,
525 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100526 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800527 .ops = &ssm2602_dai_ops,
Lars-Peter Clausena010ff62013-12-02 21:26:22 +0100528 .symmetric_rates = 1,
529 .symmetric_samplebits = 1,
Cliff Caib7138212008-09-05 18:09:57 +0800530};
Cliff Caib7138212008-09-05 18:09:57 +0800531
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000532static int ssm2602_resume(struct snd_soc_component *component)
Cliff Caib7138212008-09-05 18:09:57 +0800533{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000534 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Cliff Caib7138212008-09-05 18:09:57 +0800535
Lars-Peter Clausen9d863b882013-08-31 18:15:23 +0200536 regcache_sync(ssm2602->regmap);
Cliff Cai93547e82011-03-27 17:22:57 -0400537
Cliff Caib7138212008-09-05 18:09:57 +0800538 return 0;
539}
540
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000541static int ssm2602_component_probe(struct snd_soc_component *component)
Cliff Caib7138212008-09-05 18:09:57 +0800542{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000543 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
544 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800545 int ret;
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700546
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200547 regmap_update_bits(ssm2602->regmap, SSM2602_LOUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800548 LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200549 regmap_update_bits(ssm2602->regmap, SSM2602_ROUT1V,
Axel Lin2ee9c182011-10-19 14:07:31 +0800550 ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700551
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000552 ret = snd_soc_add_component_controls(component, ssm2602_snd_controls,
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700553 ARRAY_SIZE(ssm2602_snd_controls));
554 if (ret)
555 return ret;
556
557 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
558 ARRAY_SIZE(ssm2602_dapm_widgets));
559 if (ret)
560 return ret;
561
562 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
563 ARRAY_SIZE(ssm2602_routes));
564}
565
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000566static int ssm2604_component_probe(struct snd_soc_component *component)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700567{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000568 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700569 int ret;
570
571 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
572 ARRAY_SIZE(ssm2604_dapm_widgets));
573 if (ret)
574 return ret;
575
576 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
577 ARRAY_SIZE(ssm2604_routes));
578}
579
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000580static int ssm260x_component_probe(struct snd_soc_component *component)
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700581{
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000582 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
Axel Lin2ee9c182011-10-19 14:07:31 +0800583 int ret;
Cliff Caib7138212008-09-05 18:09:57 +0800584
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200585 ret = regmap_write(ssm2602->regmap, SSM2602_RESET, 0);
Cliff Cai93547e82011-03-27 17:22:57 -0400586 if (ret < 0) {
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000587 dev_err(component->dev, "Failed to issue reset: %d\n", ret);
Cliff Cai93547e82011-03-27 17:22:57 -0400588 return ret;
589 }
Cliff Caib7138212008-09-05 18:09:57 +0800590
Cliff Caib7138212008-09-05 18:09:57 +0800591 /* set the update bits */
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200592 regmap_update_bits(ssm2602->regmap, SSM2602_LINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800593 LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200594 regmap_update_bits(ssm2602->regmap, SSM2602_RINVOL,
Axel Lin2ee9c182011-10-19 14:07:31 +0800595 RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800596 /*select Line in as default input*/
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200597 regmap_write(ssm2602->regmap, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800598 APANA_ENABLE_MIC_BOOST);
Cliff Caib7138212008-09-05 18:09:57 +0800599
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700600 switch (ssm2602->type) {
601 case SSM2602:
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000602 ret = ssm2602_component_probe(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700603 break;
604 case SSM2604:
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000605 ret = ssm2604_component_probe(component);
Lars-Peter Clausenb1f7b2b2011-05-08 09:24:43 -0700606 break;
607 }
Cliff Caib7138212008-09-05 18:09:57 +0800608
Lars-Peter Clausen85362ef2014-09-04 19:44:12 +0200609 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610}
611
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000612static const struct snd_soc_component_driver soc_component_dev_ssm2602 = {
613 .probe = ssm260x_component_probe,
614 .resume = ssm2602_resume,
615 .set_bias_level = ssm2602_set_bias_level,
616 .controls = ssm260x_snd_controls,
617 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
618 .dapm_widgets = ssm260x_dapm_widgets,
619 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
620 .dapm_routes = ssm260x_routes,
621 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
622 .suspend_bias_off = 1,
623 .idle_bias_on = 1,
624 .use_pmdown_time = 1,
625 .endianness = 1,
626 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627};
Cliff Caib7138212008-09-05 18:09:57 +0800628
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200629static bool ssm2602_register_volatile(struct device *dev, unsigned int reg)
630{
631 return reg == SSM2602_RESET;
632}
633
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100634const struct regmap_config ssm2602_regmap_config = {
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200635 .val_bits = 9,
636 .reg_bits = 7,
637
638 .max_register = SSM2602_RESET,
639 .volatile_reg = ssm2602_register_volatile,
640
641 .cache_type = REGCACHE_RBTREE,
James Kellya01df752018-03-19 21:29:50 +1100642 .reg_defaults = ssm2602_reg,
643 .num_reg_defaults = ARRAY_SIZE(ssm2602_reg),
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200644};
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100645EXPORT_SYMBOL_GPL(ssm2602_regmap_config);
Lars-Peter Clausenbec3d9a2012-04-25 11:42:48 +0200646
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100647int ssm2602_probe(struct device *dev, enum ssm2602_type type,
648 struct regmap *regmap)
Mike Frysingerb39e2852011-04-07 02:05:11 -0400649{
650 struct ssm2602_priv *ssm2602;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400651
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100652 if (IS_ERR(regmap))
653 return PTR_ERR(regmap);
654
655 ssm2602 = devm_kzalloc(dev, sizeof(*ssm2602), GFP_KERNEL);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400656 if (ssm2602 == NULL)
657 return -ENOMEM;
658
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100659 dev_set_drvdata(dev, ssm2602);
Stefan Kristianssonfe2a08b2014-09-29 22:39:13 +0300660 ssm2602->type = type;
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100661 ssm2602->regmap = regmap;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400662
Kuninori Morimotoad4771e2018-01-29 04:28:41 +0000663 return devm_snd_soc_register_component(dev, &soc_component_dev_ssm2602,
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100664 &ssm2602_dai, 1);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400665}
Lars-Peter Clausenc924dc62014-02-17 13:16:53 +0100666EXPORT_SYMBOL_GPL(ssm2602_probe);
Mark Brown64089b82008-12-08 19:17:58 +0000667
Lars-Peter Clausen7dcf2762011-05-08 09:24:44 -0700668MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
Cliff Caib7138212008-09-05 18:09:57 +0800669MODULE_AUTHOR("Cliff Cai");
670MODULE_LICENSE("GPL");