2 * File: sound/soc/codecs/ssm2602.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
5 * Created: Tue June 06 2008
6 * Description: Driver for ssm2602 sound chip
9 * Copyright 2008 Analog Devices Inc.
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
29 #include <linux/module.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/tlv.h>
40 /* codec private data */
43 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
45 struct regmap *regmap;
47 enum ssm2602_type type;
48 unsigned int clk_out_pwr;
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
57 static const struct reg_default ssm2602_reg[SSM2602_CACHEREGNUM] = {
58 { .reg = 0x00, .def = 0x0097 },
59 { .reg = 0x01, .def = 0x0097 },
60 { .reg = 0x02, .def = 0x0079 },
61 { .reg = 0x03, .def = 0x0079 },
62 { .reg = 0x04, .def = 0x000a },
63 { .reg = 0x05, .def = 0x0008 },
64 { .reg = 0x06, .def = 0x009f },
65 { .reg = 0x07, .def = 0x000a },
66 { .reg = 0x08, .def = 0x0000 },
67 { .reg = 0x09, .def = 0x0000 }
71 /*Appending several "None"s just for OSS mixer use*/
72 static const char *ssm2602_input_select[] = {
76 static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
78 static const struct soc_enum ssm2602_enum[] = {
79 SOC_ENUM_SINGLE(SSM2602_APANA, 2, ARRAY_SIZE(ssm2602_input_select),
80 ssm2602_input_select),
81 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, ARRAY_SIZE(ssm2602_deemph),
85 static const DECLARE_TLV_DB_RANGE(ssm260x_outmix_tlv,
86 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
87 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0)
90 static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
91 static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
93 static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
94 SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
96 SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
98 SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
99 SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
101 SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
104 static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
105 SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
106 0, 127, 0, ssm260x_outmix_tlv),
107 SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
109 SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
110 ssm260x_sidetone_tlv),
112 SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
113 SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
114 SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
118 static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
119 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
120 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
121 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
125 static const struct snd_kcontrol_new ssm2602_input_mux_controls =
126 SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
128 static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
129 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
130 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
131 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
133 SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
135 SND_SOC_DAPM_OUTPUT("LOUT"),
136 SND_SOC_DAPM_OUTPUT("ROUT"),
137 SND_SOC_DAPM_INPUT("RLINEIN"),
138 SND_SOC_DAPM_INPUT("LLINEIN"),
141 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
142 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
143 ssm260x_output_mixer_controls,
144 ARRAY_SIZE(ssm260x_output_mixer_controls)),
146 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
147 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
149 SND_SOC_DAPM_OUTPUT("LHPOUT"),
150 SND_SOC_DAPM_OUTPUT("RHPOUT"),
151 SND_SOC_DAPM_INPUT("MICIN"),
154 static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
155 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
156 ssm260x_output_mixer_controls,
157 ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
160 static const struct snd_soc_dapm_route ssm260x_routes[] = {
161 {"DAC", NULL, "Digital Core Power"},
162 {"ADC", NULL, "Digital Core Power"},
164 {"Output Mixer", "Line Bypass Switch", "Line Input"},
165 {"Output Mixer", "HiFi Playback Switch", "DAC"},
167 {"ROUT", NULL, "Output Mixer"},
168 {"LOUT", NULL, "Output Mixer"},
170 {"Line Input", NULL, "LLINEIN"},
171 {"Line Input", NULL, "RLINEIN"},
174 static const struct snd_soc_dapm_route ssm2602_routes[] = {
175 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
177 {"RHPOUT", NULL, "Output Mixer"},
178 {"LHPOUT", NULL, "Output Mixer"},
180 {"Input Mux", "Line", "Line Input"},
181 {"Input Mux", "Mic", "Mic Bias"},
182 {"ADC", NULL, "Input Mux"},
184 {"Mic Bias", NULL, "MICIN"},
187 static const struct snd_soc_dapm_route ssm2604_routes[] = {
188 {"ADC", NULL, "Line Input"},
191 static const unsigned int ssm2602_rates_12288000[] = {
192 8000, 16000, 32000, 48000, 96000,
195 static const struct snd_pcm_hw_constraint_list ssm2602_constraints_12288000 = {
196 .list = ssm2602_rates_12288000,
197 .count = ARRAY_SIZE(ssm2602_rates_12288000),
200 static const unsigned int ssm2602_rates_11289600[] = {
201 8000, 11025, 22050, 44100, 88200,
204 static const struct snd_pcm_hw_constraint_list ssm2602_constraints_11289600 = {
205 .list = ssm2602_rates_11289600,
206 .count = ARRAY_SIZE(ssm2602_rates_11289600),
209 struct ssm2602_coeff {
215 #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
217 /* codec mclk clock coefficients */
218 static const struct ssm2602_coeff ssm2602_coeff_table[] = {
220 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
221 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
222 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
225 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
226 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
227 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
230 {12288000, 16000, SSM2602_COEFF_SRATE(0x5, 0x0, 0x0)},
231 {18432000, 16000, SSM2602_COEFF_SRATE(0x5, 0x1, 0x0)},
232 {12000000, 16000, SSM2602_COEFF_SRATE(0xa, 0x0, 0x1)},
235 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
236 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
237 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
238 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
239 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
242 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
243 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
244 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
247 {11289600, 11025, SSM2602_COEFF_SRATE(0xc, 0x0, 0x0)},
248 {16934400, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x0)},
249 {12000000, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x1)},
252 {11289600, 22050, SSM2602_COEFF_SRATE(0xd, 0x0, 0x0)},
253 {16934400, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x0)},
254 {12000000, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x1)},
257 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
258 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
259 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
262 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
263 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
264 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
267 static inline int ssm2602_get_coeff(int mclk, int rate)
271 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
272 if (ssm2602_coeff_table[i].rate == rate &&
273 ssm2602_coeff_table[i].mclk == mclk)
274 return ssm2602_coeff_table[i].srate;
279 static int ssm2602_hw_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params,
281 struct snd_soc_dai *dai)
283 struct snd_soc_component *component = dai->component;
284 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
285 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
291 regmap_write(ssm2602->regmap, SSM2602_SRATE, srate);
294 switch (params_width(params)) {
310 regmap_update_bits(ssm2602->regmap, SSM2602_IFACE,
311 IFACE_AUDIO_DATA_LEN, iface);
315 static int ssm2602_startup(struct snd_pcm_substream *substream,
316 struct snd_soc_dai *dai)
318 struct snd_soc_component *component = dai->component;
319 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
321 if (ssm2602->sysclk_constraints) {
322 snd_pcm_hw_constraint_list(substream->runtime, 0,
323 SNDRV_PCM_HW_PARAM_RATE,
324 ssm2602->sysclk_constraints);
330 static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
332 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(dai->component);
335 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
336 APDIGI_ENABLE_DAC_MUTE,
337 APDIGI_ENABLE_DAC_MUTE);
339 regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
340 APDIGI_ENABLE_DAC_MUTE, 0);
344 static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
345 int clk_id, unsigned int freq, int dir)
347 struct snd_soc_component *component = codec_dai->component;
348 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
350 if (dir == SND_SOC_CLOCK_IN) {
351 if (clk_id != SSM2602_SYSCLK)
357 ssm2602->sysclk_constraints = &ssm2602_constraints_12288000;
361 ssm2602->sysclk_constraints = &ssm2602_constraints_11289600;
364 ssm2602->sysclk_constraints = NULL;
369 ssm2602->sysclk = freq;
374 case SSM2602_CLK_CLKOUT:
375 mask = PWR_CLK_OUT_PDN;
377 case SSM2602_CLK_XTO:
385 ssm2602->clk_out_pwr |= mask;
387 ssm2602->clk_out_pwr &= ~mask;
389 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
390 PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
396 static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
399 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(codec_dai->component);
400 unsigned int iface = 0;
402 /* set master/slave audio interface */
403 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
404 case SND_SOC_DAIFMT_CBM_CFM:
407 case SND_SOC_DAIFMT_CBS_CFS:
413 /* interface format */
414 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
415 case SND_SOC_DAIFMT_I2S:
418 case SND_SOC_DAIFMT_RIGHT_J:
420 case SND_SOC_DAIFMT_LEFT_J:
423 case SND_SOC_DAIFMT_DSP_A:
426 case SND_SOC_DAIFMT_DSP_B:
433 /* clock inversion */
434 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
435 case SND_SOC_DAIFMT_NB_NF:
437 case SND_SOC_DAIFMT_IB_IF:
440 case SND_SOC_DAIFMT_IB_NF:
443 case SND_SOC_DAIFMT_NB_IF:
451 regmap_write(ssm2602->regmap, SSM2602_IFACE, iface);
455 static int ssm2602_set_bias_level(struct snd_soc_component *component,
456 enum snd_soc_bias_level level)
458 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
461 case SND_SOC_BIAS_ON:
462 /* vref/mid on, osc and clkout on if enabled */
463 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
464 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
465 ssm2602->clk_out_pwr);
467 case SND_SOC_BIAS_PREPARE:
469 case SND_SOC_BIAS_STANDBY:
470 /* everything off except vref/vmid, */
471 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
472 PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
473 PWR_CLK_OUT_PDN | PWR_OSC_PDN);
475 case SND_SOC_BIAS_OFF:
477 regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
478 PWR_POWER_OFF, PWR_POWER_OFF);
485 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
486 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
487 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
488 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
489 SNDRV_PCM_RATE_96000)
491 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
492 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
494 static const struct snd_soc_dai_ops ssm2602_dai_ops = {
495 .startup = ssm2602_startup,
496 .hw_params = ssm2602_hw_params,
497 .digital_mute = ssm2602_mute,
498 .set_sysclk = ssm2602_set_dai_sysclk,
499 .set_fmt = ssm2602_set_dai_fmt,
502 static struct snd_soc_dai_driver ssm2602_dai = {
503 .name = "ssm2602-hifi",
505 .stream_name = "Playback",
508 .rates = SSM2602_RATES,
509 .formats = SSM2602_FORMATS,},
511 .stream_name = "Capture",
514 .rates = SSM2602_RATES,
515 .formats = SSM2602_FORMATS,},
516 .ops = &ssm2602_dai_ops,
517 .symmetric_rates = 1,
518 .symmetric_samplebits = 1,
521 static int ssm2602_resume(struct snd_soc_component *component)
523 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
525 regcache_sync(ssm2602->regmap);
530 static int ssm2602_component_probe(struct snd_soc_component *component)
532 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
533 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
536 regmap_update_bits(ssm2602->regmap, SSM2602_LOUT1V,
537 LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
538 regmap_update_bits(ssm2602->regmap, SSM2602_ROUT1V,
539 ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
541 ret = snd_soc_add_component_controls(component, ssm2602_snd_controls,
542 ARRAY_SIZE(ssm2602_snd_controls));
546 ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
547 ARRAY_SIZE(ssm2602_dapm_widgets));
551 return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
552 ARRAY_SIZE(ssm2602_routes));
555 static int ssm2604_component_probe(struct snd_soc_component *component)
557 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
560 ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
561 ARRAY_SIZE(ssm2604_dapm_widgets));
565 return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
566 ARRAY_SIZE(ssm2604_routes));
569 static int ssm260x_component_probe(struct snd_soc_component *component)
571 struct ssm2602_priv *ssm2602 = snd_soc_component_get_drvdata(component);
574 ret = regmap_write(ssm2602->regmap, SSM2602_RESET, 0);
576 dev_err(component->dev, "Failed to issue reset: %d\n", ret);
580 /* set the update bits */
581 regmap_update_bits(ssm2602->regmap, SSM2602_LINVOL,
582 LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
583 regmap_update_bits(ssm2602->regmap, SSM2602_RINVOL,
584 RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
585 /*select Line in as default input*/
586 regmap_write(ssm2602->regmap, SSM2602_APANA, APANA_SELECT_DAC |
587 APANA_ENABLE_MIC_BOOST);
589 switch (ssm2602->type) {
591 ret = ssm2602_component_probe(component);
594 ret = ssm2604_component_probe(component);
601 static const struct snd_soc_component_driver soc_component_dev_ssm2602 = {
602 .probe = ssm260x_component_probe,
603 .resume = ssm2602_resume,
604 .set_bias_level = ssm2602_set_bias_level,
605 .controls = ssm260x_snd_controls,
606 .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
607 .dapm_widgets = ssm260x_dapm_widgets,
608 .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
609 .dapm_routes = ssm260x_routes,
610 .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
611 .suspend_bias_off = 1,
613 .use_pmdown_time = 1,
615 .non_legacy_dai_naming = 1,
618 static bool ssm2602_register_volatile(struct device *dev, unsigned int reg)
620 return reg == SSM2602_RESET;
623 const struct regmap_config ssm2602_regmap_config = {
627 .max_register = SSM2602_RESET,
628 .volatile_reg = ssm2602_register_volatile,
630 .cache_type = REGCACHE_RBTREE,
631 .reg_defaults = ssm2602_reg,
632 .num_reg_defaults = ARRAY_SIZE(ssm2602_reg),
634 EXPORT_SYMBOL_GPL(ssm2602_regmap_config);
636 int ssm2602_probe(struct device *dev, enum ssm2602_type type,
637 struct regmap *regmap)
639 struct ssm2602_priv *ssm2602;
642 return PTR_ERR(regmap);
644 ssm2602 = devm_kzalloc(dev, sizeof(*ssm2602), GFP_KERNEL);
648 dev_set_drvdata(dev, ssm2602);
649 ssm2602->type = type;
650 ssm2602->regmap = regmap;
652 return devm_snd_soc_register_component(dev, &soc_component_dev_ssm2602,
655 EXPORT_SYMBOL_GPL(ssm2602_probe);
657 MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
658 MODULE_AUTHOR("Cliff Cai");
659 MODULE_LICENSE("GPL");