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 0x0017, 0x0017, 0x0079, 0x0079,
64 0x0000, 0x0000, 0x0000, 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
, 7, 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("Capture Source", ssm2602_enum
[0]),
104 SOC_ENUM("Playback De-emphasis", ssm2602_enum
[1]),
108 static const struct snd_kcontrol_new ssm2602_output_mixer_controls
[] = {
109 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA
, 3, 1, 0),
110 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA
, 5, 1, 0),
111 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA
, 4, 1, 0),
115 static const struct snd_kcontrol_new ssm2602_input_mux_controls
=
116 SOC_DAPM_ENUM("Input Select", ssm2602_enum
[0]);
118 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets
[] = {
119 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR
, 4, 1,
120 &ssm2602_output_mixer_controls
[0],
121 ARRAY_SIZE(ssm2602_output_mixer_controls
)),
122 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR
, 3, 1),
123 SND_SOC_DAPM_OUTPUT("LOUT"),
124 SND_SOC_DAPM_OUTPUT("LHPOUT"),
125 SND_SOC_DAPM_OUTPUT("ROUT"),
126 SND_SOC_DAPM_OUTPUT("RHPOUT"),
127 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR
, 2, 1),
128 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM
, 0, 0, &ssm2602_input_mux_controls
),
129 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR
, 0, 1, NULL
, 0),
130 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR
, 1, 1),
131 SND_SOC_DAPM_INPUT("MICIN"),
132 SND_SOC_DAPM_INPUT("RLINEIN"),
133 SND_SOC_DAPM_INPUT("LLINEIN"),
136 static const struct snd_soc_dapm_route audio_conn
[] = {
138 {"Output Mixer", "Line Bypass Switch", "Line Input"},
139 {"Output Mixer", "HiFi Playback Switch", "DAC"},
140 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
143 {"RHPOUT", NULL
, "Output Mixer"},
144 {"ROUT", NULL
, "Output Mixer"},
145 {"LHPOUT", NULL
, "Output Mixer"},
146 {"LOUT", NULL
, "Output Mixer"},
149 {"Input Mux", "Line", "Line Input"},
150 {"Input Mux", "Mic", "Mic Bias"},
151 {"ADC", NULL
, "Input Mux"},
154 {"Line Input", NULL
, "LLINEIN"},
155 {"Line Input", NULL
, "RLINEIN"},
156 {"Mic Bias", NULL
, "MICIN"},
159 static int ssm2602_add_widgets(struct snd_soc_codec
*codec
)
161 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
163 snd_soc_dapm_new_controls(dapm
, ssm2602_dapm_widgets
,
164 ARRAY_SIZE(ssm2602_dapm_widgets
));
165 snd_soc_dapm_add_routes(dapm
, audio_conn
, ARRAY_SIZE(audio_conn
));
179 /* codec mclk clock divider coefficients */
180 static const struct _coeff_div coeff_div
[] = {
182 {12288000, 48000, 256, 0x0, 0x0, 0x0},
183 {18432000, 48000, 384, 0x0, 0x1, 0x0},
184 {12000000, 48000, 250, 0x0, 0x0, 0x1},
187 {12288000, 32000, 384, 0x6, 0x0, 0x0},
188 {18432000, 32000, 576, 0x6, 0x1, 0x0},
189 {12000000, 32000, 375, 0x6, 0x0, 0x1},
192 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
193 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
194 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
195 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
196 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
199 {12288000, 96000, 128, 0x7, 0x0, 0x0},
200 {18432000, 96000, 192, 0x7, 0x1, 0x0},
201 {12000000, 96000, 125, 0x7, 0x0, 0x1},
204 {11289600, 44100, 256, 0x8, 0x0, 0x0},
205 {16934400, 44100, 384, 0x8, 0x1, 0x0},
206 {12000000, 44100, 272, 0x8, 0x1, 0x1},
209 {11289600, 88200, 128, 0xf, 0x0, 0x0},
210 {16934400, 88200, 192, 0xf, 0x1, 0x0},
211 {12000000, 88200, 136, 0xf, 0x1, 0x1},
214 static inline int get_coeff(int mclk
, int rate
)
218 for (i
= 0; i
< ARRAY_SIZE(coeff_div
); i
++) {
219 if (coeff_div
[i
].rate
== rate
&& coeff_div
[i
].mclk
== mclk
)
225 static int ssm2602_hw_params(struct snd_pcm_substream
*substream
,
226 struct snd_pcm_hw_params
*params
,
227 struct snd_soc_dai
*dai
)
230 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
231 struct snd_soc_codec
*codec
= rtd
->codec
;
232 struct ssm2602_priv
*ssm2602
= snd_soc_codec_get_drvdata(codec
);
233 u16 iface
= snd_soc_read(codec
, SSM2602_IFACE
) & 0xfff3;
234 int i
= get_coeff(ssm2602
->sysclk
, params_rate(params
));
236 if (substream
== ssm2602
->slave_substream
) {
237 dev_dbg(codec
->dev
, "Ignoring hw_params for slave substream\n");
241 /*no match is found*/
242 if (i
== ARRAY_SIZE(coeff_div
))
245 srate
= (coeff_div
[i
].sr
<< 2) |
246 (coeff_div
[i
].bosr
<< 1) | coeff_div
[i
].usb
;
248 snd_soc_write(codec
, SSM2602_ACTIVE
, 0);
249 snd_soc_write(codec
, SSM2602_SRATE
, srate
);
252 switch (params_format(params
)) {
253 case SNDRV_PCM_FORMAT_S16_LE
:
255 case SNDRV_PCM_FORMAT_S20_3LE
:
258 case SNDRV_PCM_FORMAT_S24_LE
:
261 case SNDRV_PCM_FORMAT_S32_LE
:
265 snd_soc_write(codec
, SSM2602_IFACE
, iface
);
266 snd_soc_write(codec
, SSM2602_ACTIVE
, ACTIVE_ACTIVATE_CODEC
);
270 static int ssm2602_startup(struct snd_pcm_substream
*substream
,
271 struct snd_soc_dai
*dai
)
273 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
274 struct snd_soc_codec
*codec
= rtd
->codec
;
275 struct ssm2602_priv
*ssm2602
= snd_soc_codec_get_drvdata(codec
);
276 struct i2c_client
*i2c
= codec
->control_data
;
277 struct snd_pcm_runtime
*master_runtime
;
279 /* The DAI has shared clocks so if we already have a playback or
280 * capture going then constrain this substream to match it.
281 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
283 if (ssm2602
->master_substream
) {
284 master_runtime
= ssm2602
->master_substream
->runtime
;
285 dev_dbg(&i2c
->dev
, "Constraining to %d bits at %dHz\n",
286 master_runtime
->sample_bits
,
287 master_runtime
->rate
);
289 if (master_runtime
->rate
!= 0)
290 snd_pcm_hw_constraint_minmax(substream
->runtime
,
291 SNDRV_PCM_HW_PARAM_RATE
,
292 master_runtime
->rate
,
293 master_runtime
->rate
);
295 if (master_runtime
->sample_bits
!= 0)
296 snd_pcm_hw_constraint_minmax(substream
->runtime
,
297 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
298 master_runtime
->sample_bits
,
299 master_runtime
->sample_bits
);
301 ssm2602
->slave_substream
= substream
;
303 ssm2602
->master_substream
= substream
;
308 static int ssm2602_pcm_prepare(struct snd_pcm_substream
*substream
,
309 struct snd_soc_dai
*dai
)
311 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
312 struct snd_soc_codec
*codec
= rtd
->codec
;
314 snd_soc_write(codec
, SSM2602_ACTIVE
, ACTIVE_ACTIVATE_CODEC
);
319 static void ssm2602_shutdown(struct snd_pcm_substream
*substream
,
320 struct snd_soc_dai
*dai
)
322 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
323 struct snd_soc_codec
*codec
= rtd
->codec
;
324 struct ssm2602_priv
*ssm2602
= snd_soc_codec_get_drvdata(codec
);
328 snd_soc_write(codec
, SSM2602_ACTIVE
, 0);
330 if (ssm2602
->master_substream
== substream
)
331 ssm2602
->master_substream
= ssm2602
->slave_substream
;
333 ssm2602
->slave_substream
= NULL
;
336 static int ssm2602_mute(struct snd_soc_dai
*dai
, int mute
)
338 struct snd_soc_codec
*codec
= dai
->codec
;
339 u16 mute_reg
= snd_soc_read(codec
, SSM2602_APDIGI
) & ~APDIGI_ENABLE_DAC_MUTE
;
341 snd_soc_write(codec
, SSM2602_APDIGI
,
342 mute_reg
| APDIGI_ENABLE_DAC_MUTE
);
344 snd_soc_write(codec
, SSM2602_APDIGI
, mute_reg
);
348 static int ssm2602_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
349 int clk_id
, unsigned int freq
, int dir
)
351 struct snd_soc_codec
*codec
= codec_dai
->codec
;
352 struct ssm2602_priv
*ssm2602
= snd_soc_codec_get_drvdata(codec
);
359 ssm2602
->sysclk
= freq
;
365 static int ssm2602_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
368 struct snd_soc_codec
*codec
= codec_dai
->codec
;
371 /* set master/slave audio interface */
372 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
373 case SND_SOC_DAIFMT_CBM_CFM
:
376 case SND_SOC_DAIFMT_CBS_CFS
:
382 /* interface format */
383 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
384 case SND_SOC_DAIFMT_I2S
:
387 case SND_SOC_DAIFMT_RIGHT_J
:
389 case SND_SOC_DAIFMT_LEFT_J
:
392 case SND_SOC_DAIFMT_DSP_A
:
395 case SND_SOC_DAIFMT_DSP_B
:
402 /* clock inversion */
403 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
404 case SND_SOC_DAIFMT_NB_NF
:
406 case SND_SOC_DAIFMT_IB_IF
:
409 case SND_SOC_DAIFMT_IB_NF
:
412 case SND_SOC_DAIFMT_NB_IF
:
420 snd_soc_write(codec
, SSM2602_IFACE
, iface
);
424 static int ssm2602_set_bias_level(struct snd_soc_codec
*codec
,
425 enum snd_soc_bias_level level
)
427 u16 reg
= snd_soc_read(codec
, SSM2602_PWR
) & 0xff7f;
430 case SND_SOC_BIAS_ON
:
431 /* vref/mid, osc on, dac unmute */
432 snd_soc_write(codec
, SSM2602_PWR
, reg
);
434 case SND_SOC_BIAS_PREPARE
:
436 case SND_SOC_BIAS_STANDBY
:
437 /* everything off except vref/vmid, */
438 snd_soc_write(codec
, SSM2602_PWR
, reg
| PWR_CLK_OUT_PDN
);
440 case SND_SOC_BIAS_OFF
:
441 /* everything off, dac mute, inactive */
442 snd_soc_write(codec
, SSM2602_ACTIVE
, 0);
443 snd_soc_write(codec
, SSM2602_PWR
, 0xffff);
447 codec
->dapm
.bias_level
= level
;
451 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
452 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
453 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
455 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
456 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
458 static struct snd_soc_dai_ops ssm2602_dai_ops
= {
459 .startup
= ssm2602_startup
,
460 .prepare
= ssm2602_pcm_prepare
,
461 .hw_params
= ssm2602_hw_params
,
462 .shutdown
= ssm2602_shutdown
,
463 .digital_mute
= ssm2602_mute
,
464 .set_sysclk
= ssm2602_set_dai_sysclk
,
465 .set_fmt
= ssm2602_set_dai_fmt
,
468 static struct snd_soc_dai_driver ssm2602_dai
= {
469 .name
= "ssm2602-hifi",
471 .stream_name
= "Playback",
474 .rates
= SSM2602_RATES
,
475 .formats
= SSM2602_FORMATS
,},
477 .stream_name
= "Capture",
480 .rates
= SSM2602_RATES
,
481 .formats
= SSM2602_FORMATS
,},
482 .ops
= &ssm2602_dai_ops
,
485 static int ssm2602_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
487 ssm2602_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
491 static int ssm2602_resume(struct snd_soc_codec
*codec
)
493 snd_soc_cache_sync(codec
);
495 ssm2602_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
500 static int ssm2602_probe(struct snd_soc_codec
*codec
)
502 struct ssm2602_priv
*ssm2602
= snd_soc_codec_get_drvdata(codec
);
505 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION
);
507 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, ssm2602
->control_type
);
509 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
513 ret
= ssm2602_reset(codec
);
515 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
520 snd_soc_write(codec
, SSM2602_ACTIVE
, 0);
521 /* set the update bits */
522 reg
= snd_soc_read(codec
, SSM2602_LINVOL
);
523 snd_soc_write(codec
, SSM2602_LINVOL
, reg
| LINVOL_LRIN_BOTH
);
524 reg
= snd_soc_read(codec
, SSM2602_RINVOL
);
525 snd_soc_write(codec
, SSM2602_RINVOL
, reg
| RINVOL_RLIN_BOTH
);
526 reg
= snd_soc_read(codec
, SSM2602_LOUT1V
);
527 snd_soc_write(codec
, SSM2602_LOUT1V
, reg
| LOUT1V_LRHP_BOTH
);
528 reg
= snd_soc_read(codec
, SSM2602_ROUT1V
);
529 snd_soc_write(codec
, SSM2602_ROUT1V
, reg
| ROUT1V_RLHP_BOTH
);
530 /*select Line in as default input*/
531 snd_soc_write(codec
, SSM2602_APANA
, APANA_SELECT_DAC
|
532 APANA_ENABLE_MIC_BOOST
);
533 snd_soc_write(codec
, SSM2602_PWR
, 0);
535 snd_soc_add_controls(codec
, ssm2602_snd_controls
,
536 ARRAY_SIZE(ssm2602_snd_controls
));
537 ssm2602_add_widgets(codec
);
542 /* remove everything here */
543 static int ssm2602_remove(struct snd_soc_codec
*codec
)
545 ssm2602_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
549 static struct snd_soc_codec_driver soc_codec_dev_ssm2602
= {
550 .probe
= ssm2602_probe
,
551 .remove
= ssm2602_remove
,
552 .suspend
= ssm2602_suspend
,
553 .resume
= ssm2602_resume
,
554 .set_bias_level
= ssm2602_set_bias_level
,
555 .reg_cache_size
= sizeof(ssm2602_reg
),
556 .reg_word_size
= sizeof(u16
),
557 .reg_cache_default
= ssm2602_reg
,
560 #if defined(CONFIG_SPI_MASTER)
561 static int __devinit
ssm2602_spi_probe(struct spi_device
*spi
)
563 struct ssm2602_priv
*ssm2602
;
566 ssm2602
= kzalloc(sizeof(struct ssm2602_priv
), GFP_KERNEL
);
570 spi_set_drvdata(spi
, ssm2602
);
571 ssm2602
->control_type
= SND_SOC_SPI
;
573 ret
= snd_soc_register_codec(&spi
->dev
,
574 &soc_codec_dev_ssm2602
, &ssm2602_dai
, 1);
580 static int __devexit
ssm2602_spi_remove(struct spi_device
*spi
)
582 snd_soc_unregister_codec(&spi
->dev
);
583 kfree(spi_get_drvdata(spi
));
587 static struct spi_driver ssm2602_spi_driver
= {
590 .owner
= THIS_MODULE
,
592 .probe
= ssm2602_spi_probe
,
593 .remove
= __devexit_p(ssm2602_spi_remove
),
597 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
599 * ssm2602 2 wire address is determined by GPIO5
600 * state during powerup.
604 static int ssm2602_i2c_probe(struct i2c_client
*i2c
,
605 const struct i2c_device_id
*id
)
607 struct ssm2602_priv
*ssm2602
;
610 ssm2602
= kzalloc(sizeof(struct ssm2602_priv
), GFP_KERNEL
);
614 i2c_set_clientdata(i2c
, ssm2602
);
615 ssm2602
->control_type
= SND_SOC_I2C
;
617 ret
= snd_soc_register_codec(&i2c
->dev
,
618 &soc_codec_dev_ssm2602
, &ssm2602_dai
, 1);
624 static int ssm2602_i2c_remove(struct i2c_client
*client
)
626 snd_soc_unregister_codec(&client
->dev
);
627 kfree(i2c_get_clientdata(client
));
631 static const struct i2c_device_id ssm2602_i2c_id
[] = {
635 MODULE_DEVICE_TABLE(i2c
, ssm2602_i2c_id
);
637 /* corgi i2c codec control layer */
638 static struct i2c_driver ssm2602_i2c_driver
= {
641 .owner
= THIS_MODULE
,
643 .probe
= ssm2602_i2c_probe
,
644 .remove
= ssm2602_i2c_remove
,
645 .id_table
= ssm2602_i2c_id
,
650 static int __init
ssm2602_modinit(void)
654 #if defined(CONFIG_SPI_MASTER)
655 ret
= spi_register_driver(&ssm2602_spi_driver
);
660 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
661 ret
= i2c_add_driver(&ssm2602_i2c_driver
);
668 module_init(ssm2602_modinit
);
670 static void __exit
ssm2602_exit(void)
672 #if defined(CONFIG_SPI_MASTER)
673 spi_unregister_driver(&ssm2602_spi_driver
);
676 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
677 i2c_del_driver(&ssm2602_i2c_driver
);
680 module_exit(ssm2602_exit
);
682 MODULE_DESCRIPTION("ASoC ssm2602 driver");
683 MODULE_AUTHOR("Cliff Cai");
684 MODULE_LICENSE("GPL");