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/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
34 #include <linux/i2c.h>
35 #include <linux/spi/spi.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <sound/core.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/initval.h>
46 #define SSM2602_VERSION "0.1"
48 /* codec private data */
51 enum snd_soc_control_type control_type;
52 struct snd_pcm_substream *master_substream;
53 struct snd_pcm_substream *slave_substream;
57 * ssm2602 register cache
58 * We can't read the ssm2602 register space when we are
59 * using 2 wire for device control, so we cache them instead.
60 * There is no point in caching the reset register
62 static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
63 0x0097, 0x0097, 0x0079, 0x0079,
64 0x000a, 0x0008, 0x009f, 0x000a,
68 #define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
70 /*Appending several "None"s just for OSS mixer use*/
71 static const char *ssm2602_input_select[] = {
72 "Line", "Mic", "None", "None", "None",
73 "None", "None", "None",
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, 2, ssm2602_input_select),
80 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
83 static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
85 SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
87 SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
90 SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
91 SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
93 SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
94 SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
95 SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
97 SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
99 SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
100 SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
102 SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
106 static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
107 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
108 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
109 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
113 static const struct snd_kcontrol_new ssm2602_input_mux_controls =
114 SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
116 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
117 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
118 &ssm2602_output_mixer_controls[0],
119 ARRAY_SIZE(ssm2602_output_mixer_controls)),
120 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
121 SND_SOC_DAPM_OUTPUT("LOUT"),
122 SND_SOC_DAPM_OUTPUT("LHPOUT"),
123 SND_SOC_DAPM_OUTPUT("ROUT"),
124 SND_SOC_DAPM_OUTPUT("RHPOUT"),
125 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
126 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
127 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
128 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
129 SND_SOC_DAPM_INPUT("MICIN"),
130 SND_SOC_DAPM_INPUT("RLINEIN"),
131 SND_SOC_DAPM_INPUT("LLINEIN"),
134 static const struct snd_soc_dapm_route audio_conn[] = {
136 {"Output Mixer", "Line Bypass Switch", "Line Input"},
137 {"Output Mixer", "HiFi Playback Switch", "DAC"},
138 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
141 {"RHPOUT", NULL, "Output Mixer"},
142 {"ROUT", NULL, "Output Mixer"},
143 {"LHPOUT", NULL, "Output Mixer"},
144 {"LOUT", NULL, "Output Mixer"},
147 {"Input Mux", "Line", "Line Input"},
148 {"Input Mux", "Mic", "Mic Bias"},
149 {"ADC", NULL, "Input Mux"},
152 {"Line Input", NULL, "LLINEIN"},
153 {"Line Input", NULL, "RLINEIN"},
154 {"Mic Bias", NULL, "MICIN"},
157 static int ssm2602_add_widgets(struct snd_soc_codec *codec)
159 struct snd_soc_dapm_context *dapm = &codec->dapm;
161 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
162 ARRAY_SIZE(ssm2602_dapm_widgets));
163 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
168 struct ssm2602_coeff {
174 #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
176 /* codec mclk clock coefficients */
177 static const struct ssm2602_coeff ssm2602_coeff_table[] = {
179 {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
180 {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
181 {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
184 {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
185 {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
186 {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
189 {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
190 {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
191 {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
192 {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
193 {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
196 {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
197 {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
198 {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
201 {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
202 {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
203 {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
206 {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
207 {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
208 {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
211 static inline int ssm2602_get_coeff(int mclk, int rate)
215 for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
216 if (ssm2602_coeff_table[i].rate == rate &&
217 ssm2602_coeff_table[i].mclk == mclk)
218 return ssm2602_coeff_table[i].srate;
223 static int ssm2602_hw_params(struct snd_pcm_substream *substream,
224 struct snd_pcm_hw_params *params,
225 struct snd_soc_dai *dai)
227 struct snd_soc_pcm_runtime *rtd = substream->private_data;
228 struct snd_soc_codec *codec = rtd->codec;
229 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
230 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
231 int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
233 if (substream == ssm2602->slave_substream) {
234 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
241 snd_soc_write(codec, SSM2602_ACTIVE, 0);
242 snd_soc_write(codec, SSM2602_SRATE, srate);
245 switch (params_format(params)) {
246 case SNDRV_PCM_FORMAT_S16_LE:
248 case SNDRV_PCM_FORMAT_S20_3LE:
251 case SNDRV_PCM_FORMAT_S24_LE:
254 case SNDRV_PCM_FORMAT_S32_LE:
258 snd_soc_write(codec, SSM2602_IFACE, iface);
259 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
263 static int ssm2602_startup(struct snd_pcm_substream *substream,
264 struct snd_soc_dai *dai)
266 struct snd_soc_pcm_runtime *rtd = substream->private_data;
267 struct snd_soc_codec *codec = rtd->codec;
268 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
269 struct i2c_client *i2c = codec->control_data;
270 struct snd_pcm_runtime *master_runtime;
272 /* The DAI has shared clocks so if we already have a playback or
273 * capture going then constrain this substream to match it.
274 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
276 if (ssm2602->master_substream) {
277 master_runtime = ssm2602->master_substream->runtime;
278 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
279 master_runtime->sample_bits,
280 master_runtime->rate);
282 if (master_runtime->rate != 0)
283 snd_pcm_hw_constraint_minmax(substream->runtime,
284 SNDRV_PCM_HW_PARAM_RATE,
285 master_runtime->rate,
286 master_runtime->rate);
288 if (master_runtime->sample_bits != 0)
289 snd_pcm_hw_constraint_minmax(substream->runtime,
290 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
291 master_runtime->sample_bits,
292 master_runtime->sample_bits);
294 ssm2602->slave_substream = substream;
296 ssm2602->master_substream = substream;
301 static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
302 struct snd_soc_dai *dai)
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_codec *codec = rtd->codec;
307 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
312 static void ssm2602_shutdown(struct snd_pcm_substream *substream,
313 struct snd_soc_dai *dai)
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
316 struct snd_soc_codec *codec = rtd->codec;
317 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
321 snd_soc_write(codec, SSM2602_ACTIVE, 0);
323 if (ssm2602->master_substream == substream)
324 ssm2602->master_substream = ssm2602->slave_substream;
326 ssm2602->slave_substream = NULL;
329 static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
331 struct snd_soc_codec *codec = dai->codec;
332 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
334 snd_soc_write(codec, SSM2602_APDIGI,
335 mute_reg | APDIGI_ENABLE_DAC_MUTE);
337 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
341 static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
342 int clk_id, unsigned int freq, int dir)
344 struct snd_soc_codec *codec = codec_dai->codec;
345 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
352 ssm2602->sysclk = freq;
358 static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
361 struct snd_soc_codec *codec = codec_dai->codec;
364 /* set master/slave audio interface */
365 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
366 case SND_SOC_DAIFMT_CBM_CFM:
369 case SND_SOC_DAIFMT_CBS_CFS:
375 /* interface format */
376 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
377 case SND_SOC_DAIFMT_I2S:
380 case SND_SOC_DAIFMT_RIGHT_J:
382 case SND_SOC_DAIFMT_LEFT_J:
385 case SND_SOC_DAIFMT_DSP_A:
388 case SND_SOC_DAIFMT_DSP_B:
395 /* clock inversion */
396 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
397 case SND_SOC_DAIFMT_NB_NF:
399 case SND_SOC_DAIFMT_IB_IF:
402 case SND_SOC_DAIFMT_IB_NF:
405 case SND_SOC_DAIFMT_NB_IF:
413 snd_soc_write(codec, SSM2602_IFACE, iface);
417 static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
418 enum snd_soc_bias_level level)
420 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
423 case SND_SOC_BIAS_ON:
424 /* vref/mid, osc on, dac unmute */
425 snd_soc_write(codec, SSM2602_PWR, reg);
427 case SND_SOC_BIAS_PREPARE:
429 case SND_SOC_BIAS_STANDBY:
430 /* everything off except vref/vmid, */
431 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
433 case SND_SOC_BIAS_OFF:
434 /* everything off, dac mute, inactive */
435 snd_soc_write(codec, SSM2602_ACTIVE, 0);
436 snd_soc_write(codec, SSM2602_PWR, 0xffff);
440 codec->dapm.bias_level = level;
444 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
445 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
446 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
448 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
449 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
451 static struct snd_soc_dai_ops ssm2602_dai_ops = {
452 .startup = ssm2602_startup,
453 .prepare = ssm2602_pcm_prepare,
454 .hw_params = ssm2602_hw_params,
455 .shutdown = ssm2602_shutdown,
456 .digital_mute = ssm2602_mute,
457 .set_sysclk = ssm2602_set_dai_sysclk,
458 .set_fmt = ssm2602_set_dai_fmt,
461 static struct snd_soc_dai_driver ssm2602_dai = {
462 .name = "ssm2602-hifi",
464 .stream_name = "Playback",
467 .rates = SSM2602_RATES,
468 .formats = SSM2602_FORMATS,},
470 .stream_name = "Capture",
473 .rates = SSM2602_RATES,
474 .formats = SSM2602_FORMATS,},
475 .ops = &ssm2602_dai_ops,
478 static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
480 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
484 static int ssm2602_resume(struct snd_soc_codec *codec)
486 snd_soc_cache_sync(codec);
488 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
493 static int ssm2602_probe(struct snd_soc_codec *codec)
495 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
498 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
500 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
502 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
506 ret = ssm2602_reset(codec);
508 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
512 /* set the update bits */
513 reg = snd_soc_read(codec, SSM2602_LINVOL);
514 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
515 reg = snd_soc_read(codec, SSM2602_RINVOL);
516 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
517 reg = snd_soc_read(codec, SSM2602_LOUT1V);
518 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
519 reg = snd_soc_read(codec, SSM2602_ROUT1V);
520 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
521 /*select Line in as default input*/
522 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
523 APANA_ENABLE_MIC_BOOST);
525 snd_soc_add_controls(codec, ssm2602_snd_controls,
526 ARRAY_SIZE(ssm2602_snd_controls));
527 ssm2602_add_widgets(codec);
532 /* remove everything here */
533 static int ssm2602_remove(struct snd_soc_codec *codec)
535 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
539 static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
540 .probe = ssm2602_probe,
541 .remove = ssm2602_remove,
542 .suspend = ssm2602_suspend,
543 .resume = ssm2602_resume,
544 .set_bias_level = ssm2602_set_bias_level,
545 .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
546 .reg_word_size = sizeof(u16),
547 .reg_cache_default = ssm2602_reg,
550 #if defined(CONFIG_SPI_MASTER)
551 static int __devinit ssm2602_spi_probe(struct spi_device *spi)
553 struct ssm2602_priv *ssm2602;
556 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
560 spi_set_drvdata(spi, ssm2602);
561 ssm2602->control_type = SND_SOC_SPI;
563 ret = snd_soc_register_codec(&spi->dev,
564 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
570 static int __devexit ssm2602_spi_remove(struct spi_device *spi)
572 snd_soc_unregister_codec(&spi->dev);
573 kfree(spi_get_drvdata(spi));
577 static struct spi_driver ssm2602_spi_driver = {
580 .owner = THIS_MODULE,
582 .probe = ssm2602_spi_probe,
583 .remove = __devexit_p(ssm2602_spi_remove),
587 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
589 * ssm2602 2 wire address is determined by GPIO5
590 * state during powerup.
594 static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
595 const struct i2c_device_id *id)
597 struct ssm2602_priv *ssm2602;
600 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
604 i2c_set_clientdata(i2c, ssm2602);
605 ssm2602->control_type = SND_SOC_I2C;
607 ret = snd_soc_register_codec(&i2c->dev,
608 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
614 static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
616 snd_soc_unregister_codec(&client->dev);
617 kfree(i2c_get_clientdata(client));
621 static const struct i2c_device_id ssm2602_i2c_id[] = {
625 MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
627 /* corgi i2c codec control layer */
628 static struct i2c_driver ssm2602_i2c_driver = {
631 .owner = THIS_MODULE,
633 .probe = ssm2602_i2c_probe,
634 .remove = __devexit_p(ssm2602_i2c_remove),
635 .id_table = ssm2602_i2c_id,
640 static int __init ssm2602_modinit(void)
644 #if defined(CONFIG_SPI_MASTER)
645 ret = spi_register_driver(&ssm2602_spi_driver);
650 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
651 ret = i2c_add_driver(&ssm2602_i2c_driver);
658 module_init(ssm2602_modinit);
660 static void __exit ssm2602_exit(void)
662 #if defined(CONFIG_SPI_MASTER)
663 spi_unregister_driver(&ssm2602_spi_driver);
666 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
667 i2c_del_driver(&ssm2602_i2c_driver);
670 module_exit(ssm2602_exit);
672 MODULE_DESCRIPTION("ASoC ssm2602 driver");
673 MODULE_AUTHOR("Cliff Cai");
674 MODULE_LICENSE("GPL");