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 <linux/regmap.h>
25 #include <sound/soc.h>
26 #include <sound/initval.h>
28 #include <linux/i2c.h>
30 #include <linux/mfd/si476x-core.h>
32 enum si476x_audio_registers
{
33 SI476X_DIGITAL_IO_OUTPUT_FORMAT
= 0x0203,
34 SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE
= 0x0202,
37 enum si476x_digital_io_output_format
{
38 SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT
= 11,
39 SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT
= 8,
42 #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0x7 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \
43 (0x7 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT))
44 #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0x7e)
46 enum si476x_daudio_formats
{
47 SI476X_DAUDIO_MODE_I2S
= (0x0 << 1),
48 SI476X_DAUDIO_MODE_DSP_A
= (0x6 << 1),
49 SI476X_DAUDIO_MODE_DSP_B
= (0x7 << 1),
50 SI476X_DAUDIO_MODE_LEFT_J
= (0x8 << 1),
51 SI476X_DAUDIO_MODE_RIGHT_J
= (0x9 << 1),
53 SI476X_DAUDIO_MODE_IB
= (1 << 5),
54 SI476X_DAUDIO_MODE_IF
= (1 << 6),
57 enum si476x_pcm_format
{
58 SI476X_PCM_FORMAT_S8
= 2,
59 SI476X_PCM_FORMAT_S16_LE
= 4,
60 SI476X_PCM_FORMAT_S20_3LE
= 5,
61 SI476X_PCM_FORMAT_S24_LE
= 6,
64 static const struct snd_soc_dapm_widget si476x_dapm_widgets
[] = {
65 SND_SOC_DAPM_OUTPUT("LOUT"),
66 SND_SOC_DAPM_OUTPUT("ROUT"),
69 static const struct snd_soc_dapm_route si476x_dapm_routes
[] = {
70 { "Capture", NULL
, "LOUT" },
71 { "Capture", NULL
, "ROUT" },
74 static int si476x_codec_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
77 struct si476x_core
*core
= i2c_mfd_cell_to_core(codec_dai
->dev
);
81 if ((fmt
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
84 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
85 case SND_SOC_DAIFMT_DSP_A
:
86 format
|= SI476X_DAUDIO_MODE_DSP_A
;
88 case SND_SOC_DAIFMT_DSP_B
:
89 format
|= SI476X_DAUDIO_MODE_DSP_B
;
91 case SND_SOC_DAIFMT_I2S
:
92 format
|= SI476X_DAUDIO_MODE_I2S
;
94 case SND_SOC_DAIFMT_RIGHT_J
:
95 format
|= SI476X_DAUDIO_MODE_RIGHT_J
;
97 case SND_SOC_DAIFMT_LEFT_J
:
98 format
|= SI476X_DAUDIO_MODE_LEFT_J
;
104 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
105 case SND_SOC_DAIFMT_DSP_A
:
106 case SND_SOC_DAIFMT_DSP_B
:
107 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
108 case SND_SOC_DAIFMT_NB_NF
:
110 case SND_SOC_DAIFMT_IB_NF
:
111 format
|= SI476X_DAUDIO_MODE_IB
;
117 case SND_SOC_DAIFMT_I2S
:
118 case SND_SOC_DAIFMT_RIGHT_J
:
119 case SND_SOC_DAIFMT_LEFT_J
:
120 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
121 case SND_SOC_DAIFMT_NB_NF
:
123 case SND_SOC_DAIFMT_IB_IF
:
124 format
|= SI476X_DAUDIO_MODE_IB
|
125 SI476X_DAUDIO_MODE_IF
;
127 case SND_SOC_DAIFMT_IB_NF
:
128 format
|= SI476X_DAUDIO_MODE_IB
;
130 case SND_SOC_DAIFMT_NB_IF
:
131 format
|= SI476X_DAUDIO_MODE_IF
;
141 si476x_core_lock(core
);
143 err
= snd_soc_update_bits(codec_dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_FORMAT
,
144 SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK
,
147 si476x_core_unlock(core
);
150 dev_err(codec_dai
->codec
->dev
, "Failed to set output format\n");
157 static int si476x_codec_hw_params(struct snd_pcm_substream
*substream
,
158 struct snd_pcm_hw_params
*params
,
159 struct snd_soc_dai
*dai
)
161 struct si476x_core
*core
= i2c_mfd_cell_to_core(dai
->dev
);
162 int rate
, width
, err
;
164 rate
= params_rate(params
);
165 if (rate
< 32000 || rate
> 48000) {
166 dev_err(dai
->codec
->dev
, "Rate: %d is not supported\n", rate
);
170 switch (params_format(params
)) {
171 case SNDRV_PCM_FORMAT_S8
:
172 width
= SI476X_PCM_FORMAT_S8
;
174 case SNDRV_PCM_FORMAT_S16_LE
:
175 width
= SI476X_PCM_FORMAT_S16_LE
;
177 case SNDRV_PCM_FORMAT_S20_3LE
:
178 width
= SI476X_PCM_FORMAT_S20_3LE
;
180 case SNDRV_PCM_FORMAT_S24_LE
:
181 width
= SI476X_PCM_FORMAT_S24_LE
;
187 si476x_core_lock(core
);
189 err
= snd_soc_write(dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE
,
192 dev_err(dai
->codec
->dev
, "Failed to set sample rate\n");
196 err
= snd_soc_update_bits(dai
->codec
, SI476X_DIGITAL_IO_OUTPUT_FORMAT
,
197 SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK
,
198 (width
<< SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT
) |
199 (width
<< SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT
));
201 dev_err(dai
->codec
->dev
, "Failed to set output width\n");
206 si476x_core_unlock(core
);
211 static int si476x_codec_probe(struct snd_soc_codec
*codec
)
213 struct regmap
*regmap
= dev_get_regmap(codec
->dev
->parent
, NULL
);
215 return snd_soc_codec_set_cache_io(codec
, regmap
);
218 static struct snd_soc_dai_ops si476x_dai_ops
= {
219 .hw_params
= si476x_codec_hw_params
,
220 .set_fmt
= si476x_codec_set_dai_fmt
,
223 static struct snd_soc_dai_driver si476x_dai
= {
224 .name
= "si476x-codec",
226 .stream_name
= "Capture",
230 .rates
= SNDRV_PCM_RATE_32000
|
231 SNDRV_PCM_RATE_44100
|
232 SNDRV_PCM_RATE_48000
,
233 .formats
= SNDRV_PCM_FMTBIT_S8
|
234 SNDRV_PCM_FMTBIT_S16_LE
|
235 SNDRV_PCM_FMTBIT_S20_3LE
|
236 SNDRV_PCM_FMTBIT_S24_LE
238 .ops
= &si476x_dai_ops
,
241 static struct snd_soc_codec_driver soc_codec_dev_si476x
= {
242 .probe
= si476x_codec_probe
,
243 .dapm_widgets
= si476x_dapm_widgets
,
244 .num_dapm_widgets
= ARRAY_SIZE(si476x_dapm_widgets
),
245 .dapm_routes
= si476x_dapm_routes
,
246 .num_dapm_routes
= ARRAY_SIZE(si476x_dapm_routes
),
249 static int si476x_platform_probe(struct platform_device
*pdev
)
251 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_si476x
,
255 static int si476x_platform_remove(struct platform_device
*pdev
)
257 snd_soc_unregister_codec(&pdev
->dev
);
261 MODULE_ALIAS("platform:si476x-codec");
263 static struct platform_driver si476x_platform_driver
= {
265 .name
= "si476x-codec",
266 .owner
= THIS_MODULE
,
268 .probe
= si476x_platform_probe
,
269 .remove
= si476x_platform_remove
,
271 module_platform_driver(si476x_platform_driver
);
273 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
274 MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
275 MODULE_LICENSE("GPL");