2 * sound/soc/codecs/si476x.c -- Codec driver for SI476X chips
4 * Copyright (C) 2012 Innovative Converged Devices(ICD)
5 * Copyright (C) 2013 Andrey Smirnov
7 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
27 #include <linux/i2c.h>
29 #include <linux/mfd/si476x-core.h>
31 enum si476x_audio_registers
{
32 SI476X_DIGITAL_IO_OUTPUT_FORMAT
= 0x0203,
33 SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE
= 0x0202,
36 enum si476x_digital_io_output_format
{
37 SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT
= 11,
38 SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT
= 8,
41 #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0x7 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \
42 (0x7 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT))
43 #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0x7e)
45 enum si476x_daudio_formats
{
46 SI476X_DAUDIO_MODE_I2S
= (0x0 << 1),
47 SI476X_DAUDIO_MODE_DSP_A
= (0x6 << 1),
48 SI476X_DAUDIO_MODE_DSP_B
= (0x7 << 1),
49 SI476X_DAUDIO_MODE_LEFT_J
= (0x8 << 1),
50 SI476X_DAUDIO_MODE_RIGHT_J
= (0x9 << 1),
52 SI476X_DAUDIO_MODE_IB
= (1 << 5),
53 SI476X_DAUDIO_MODE_IF
= (1 << 6),
56 enum si476x_pcm_format
{
57 SI476X_PCM_FORMAT_S8
= 2,
58 SI476X_PCM_FORMAT_S16_LE
= 4,
59 SI476X_PCM_FORMAT_S20_3LE
= 5,
60 SI476X_PCM_FORMAT_S24_LE
= 6,
63 static unsigned int si476x_codec_read(struct snd_soc_codec
*codec
,
68 struct si476x_core
*core
= codec
->control_data
;
70 si476x_core_lock(core
);
71 if (!si476x_core_is_powered_up(core
))
72 regcache_cache_only(core
->regmap
, true);
74 err
= regmap_read(core
->regmap
, reg
, &val
);
76 if (!si476x_core_is_powered_up(core
))
77 regcache_cache_only(core
->regmap
, false);
78 si476x_core_unlock(core
);
86 static int si476x_codec_write(struct snd_soc_codec
*codec
,
87 unsigned int reg
, unsigned int val
)
90 struct si476x_core
*core
= codec
->control_data
;
92 si476x_core_lock(core
);
93 if (!si476x_core_is_powered_up(core
))
94 regcache_cache_only(core
->regmap
, true);
96 err
= regmap_write(core
->regmap
, reg
, val
);
98 if (!si476x_core_is_powered_up(core
))
99 regcache_cache_only(core
->regmap
, false);
100 si476x_core_unlock(core
);
105 static const struct snd_soc_dapm_widget si476x_dapm_widgets
[] = {
106 SND_SOC_DAPM_OUTPUT("LOUT"),
107 SND_SOC_DAPM_OUTPUT("ROUT"),
110 static const struct snd_soc_dapm_route si476x_dapm_routes
[] = {
111 { "Capture", NULL
, "LOUT" },
112 { "Capture", NULL
, "ROUT" },
115 static int si476x_codec_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
121 if ((fmt
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
124 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
125 case SND_SOC_DAIFMT_DSP_A
:
126 format
|= SI476X_DAUDIO_MODE_DSP_A
;
128 case SND_SOC_DAIFMT_DSP_B
:
129 format
|= SI476X_DAUDIO_MODE_DSP_B
;
131 case SND_SOC_DAIFMT_I2S
:
132 format
|= SI476X_DAUDIO_MODE_I2S
;
134 case SND_SOC_DAIFMT_RIGHT_J
:
135 format
|= SI476X_DAUDIO_MODE_RIGHT_J
;
137 case SND_SOC_DAIFMT_LEFT_J
:
138 format
|= SI476X_DAUDIO_MODE_LEFT_J
;
144 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
145 case SND_SOC_DAIFMT_DSP_A
:
146 case SND_SOC_DAIFMT_DSP_B
:
147 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
148 case SND_SOC_DAIFMT_NB_NF
:
150 case SND_SOC_DAIFMT_IB_NF
:
151 format
|= SI476X_DAUDIO_MODE_IB
;
157 case SND_SOC_DAIFMT_I2S
:
158 case SND_SOC_DAIFMT_RIGHT_J
:
159 case SND_SOC_DAIFMT_LEFT_J
:
160 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
161 case SND_SOC_DAIFMT_NB_NF
:
163 case SND_SOC_DAIFMT_IB_IF
:
164 format
|= SI476X_DAUDIO_MODE_IB
|
165 SI476X_DAUDIO_MODE_IF
;
167 case SND_SOC_DAIFMT_IB_NF
:
168 format
|= SI476X_DAUDIO_MODE_IB
;
170 case SND_SOC_DAIFMT_NB_IF
:
171 format
|= SI476X_DAUDIO_MODE_IF
;
181 err
= snd_soc_update_bits(codec_dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_FORMAT
,
182 SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK
,
185 dev_err(codec_dai
->codec
->dev
, "Failed to set output format\n");
192 static int si476x_codec_hw_params(struct snd_pcm_substream
*substream
,
193 struct snd_pcm_hw_params
*params
,
194 struct snd_soc_dai
*dai
)
196 int rate
, width
, err
;
198 rate
= params_rate(params
);
199 if (rate
< 32000 || rate
> 48000) {
200 dev_err(dai
->codec
->dev
, "Rate: %d is not supported\n", rate
);
204 switch (params_format(params
)) {
205 case SNDRV_PCM_FORMAT_S8
:
206 width
= SI476X_PCM_FORMAT_S8
;
208 case SNDRV_PCM_FORMAT_S16_LE
:
209 width
= SI476X_PCM_FORMAT_S16_LE
;
211 case SNDRV_PCM_FORMAT_S20_3LE
:
212 width
= SI476X_PCM_FORMAT_S20_3LE
;
214 case SNDRV_PCM_FORMAT_S24_LE
:
215 width
= SI476X_PCM_FORMAT_S24_LE
;
221 err
= snd_soc_write(dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE
,
224 dev_err(dai
->codec
->dev
, "Failed to set sample rate\n");
228 err
= snd_soc_update_bits(dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_FORMAT
,
229 SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK
,
230 (width
<< SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT
) |
231 (width
<< SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT
));
233 dev_err(dai
->codec
->dev
, "Failed to set output width\n");
240 static int si476x_codec_probe(struct snd_soc_codec
*codec
)
242 codec
->control_data
= i2c_mfd_cell_to_core(codec
->dev
);
246 static struct snd_soc_dai_ops si476x_dai_ops
= {
247 .hw_params
= si476x_codec_hw_params
,
248 .set_fmt
= si476x_codec_set_dai_fmt
,
251 static struct snd_soc_dai_driver si476x_dai
= {
252 .name
= "si476x-codec",
254 .stream_name
= "Capture",
258 .rates
= SNDRV_PCM_RATE_32000
|
259 SNDRV_PCM_RATE_44100
|
260 SNDRV_PCM_RATE_48000
,
261 .formats
= SNDRV_PCM_FMTBIT_S8
|
262 SNDRV_PCM_FMTBIT_S16_LE
|
263 SNDRV_PCM_FMTBIT_S20_3LE
|
264 SNDRV_PCM_FMTBIT_S24_LE
266 .ops
= &si476x_dai_ops
,
269 static struct snd_soc_codec_driver soc_codec_dev_si476x
= {
270 .probe
= si476x_codec_probe
,
271 .read
= si476x_codec_read
,
272 .write
= si476x_codec_write
,
273 .dapm_widgets
= si476x_dapm_widgets
,
274 .num_dapm_widgets
= ARRAY_SIZE(si476x_dapm_widgets
),
275 .dapm_routes
= si476x_dapm_routes
,
276 .num_dapm_routes
= ARRAY_SIZE(si476x_dapm_routes
),
279 static int si476x_platform_probe(struct platform_device
*pdev
)
281 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_si476x
,
285 static int si476x_platform_remove(struct platform_device
*pdev
)
287 snd_soc_unregister_codec(&pdev
->dev
);
291 MODULE_ALIAS("platform:si476x-codec");
293 static struct platform_driver si476x_platform_driver
= {
295 .name
= "si476x-codec",
296 .owner
= THIS_MODULE
,
298 .probe
= si476x_platform_probe
,
299 .remove
= si476x_platform_remove
,
301 module_platform_driver(si476x_platform_driver
);
303 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
304 MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
305 MODULE_LICENSE("GPL");