03645ce42063ac7ad65462e6b2f386e2d05d5f7a
[deliverable/linux.git] / sound / soc / codecs / si476x.c
1 /*
2 * sound/soc/codecs/si476x.c -- Codec driver for SI476X chips
3 *
4 * Copyright (C) 2012 Innovative Converged Devices(ICD)
5 * Copyright (C) 2013 Andrey Smirnov
6 *
7 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
8 *
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.
12 *
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.
17 *
18 */
19
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>
26
27 #include <linux/i2c.h>
28
29 #include <linux/mfd/si476x-core.h>
30
31 enum si476x_audio_registers {
32 SI476X_DIGITAL_IO_OUTPUT_FORMAT = 0x0203,
33 SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202,
34 };
35
36 enum si476x_digital_io_output_format {
37 SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT = 11,
38 SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT = 8,
39 };
40
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)
44
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),
51
52 SI476X_DAUDIO_MODE_IB = (1 << 5),
53 SI476X_DAUDIO_MODE_IF = (1 << 6),
54 };
55
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,
61 };
62
63 static const struct snd_soc_dapm_widget si476x_dapm_widgets[] = {
64 SND_SOC_DAPM_OUTPUT("LOUT"),
65 SND_SOC_DAPM_OUTPUT("ROUT"),
66 };
67
68 static const struct snd_soc_dapm_route si476x_dapm_routes[] = {
69 { "Capture", NULL, "LOUT" },
70 { "Capture", NULL, "ROUT" },
71 };
72
73 static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
74 unsigned int fmt)
75 {
76 int err;
77 u16 format = 0;
78
79 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
80 return -EINVAL;
81
82 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
83 case SND_SOC_DAIFMT_DSP_A:
84 format |= SI476X_DAUDIO_MODE_DSP_A;
85 break;
86 case SND_SOC_DAIFMT_DSP_B:
87 format |= SI476X_DAUDIO_MODE_DSP_B;
88 break;
89 case SND_SOC_DAIFMT_I2S:
90 format |= SI476X_DAUDIO_MODE_I2S;
91 break;
92 case SND_SOC_DAIFMT_RIGHT_J:
93 format |= SI476X_DAUDIO_MODE_RIGHT_J;
94 break;
95 case SND_SOC_DAIFMT_LEFT_J:
96 format |= SI476X_DAUDIO_MODE_LEFT_J;
97 break;
98 default:
99 return -EINVAL;
100 }
101
102 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
103 case SND_SOC_DAIFMT_DSP_A:
104 case SND_SOC_DAIFMT_DSP_B:
105 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
106 case SND_SOC_DAIFMT_NB_NF:
107 break;
108 case SND_SOC_DAIFMT_IB_NF:
109 format |= SI476X_DAUDIO_MODE_IB;
110 break;
111 default:
112 return -EINVAL;
113 }
114 break;
115 case SND_SOC_DAIFMT_I2S:
116 case SND_SOC_DAIFMT_RIGHT_J:
117 case SND_SOC_DAIFMT_LEFT_J:
118 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
119 case SND_SOC_DAIFMT_NB_NF:
120 break;
121 case SND_SOC_DAIFMT_IB_IF:
122 format |= SI476X_DAUDIO_MODE_IB |
123 SI476X_DAUDIO_MODE_IF;
124 break;
125 case SND_SOC_DAIFMT_IB_NF:
126 format |= SI476X_DAUDIO_MODE_IB;
127 break;
128 case SND_SOC_DAIFMT_NB_IF:
129 format |= SI476X_DAUDIO_MODE_IF;
130 break;
131 default:
132 return -EINVAL;
133 }
134 break;
135 default:
136 return -EINVAL;
137 }
138
139 err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
140 SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK,
141 format);
142 if (err < 0) {
143 dev_err(codec_dai->codec->dev, "Failed to set output format\n");
144 return err;
145 }
146
147 return 0;
148 }
149
150 static int si476x_codec_hw_params(struct snd_pcm_substream *substream,
151 struct snd_pcm_hw_params *params,
152 struct snd_soc_dai *dai)
153 {
154 int rate, width, err;
155
156 rate = params_rate(params);
157 if (rate < 32000 || rate > 48000) {
158 dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate);
159 return -EINVAL;
160 }
161
162 switch (params_format(params)) {
163 case SNDRV_PCM_FORMAT_S8:
164 width = SI476X_PCM_FORMAT_S8;
165 break;
166 case SNDRV_PCM_FORMAT_S16_LE:
167 width = SI476X_PCM_FORMAT_S16_LE;
168 break;
169 case SNDRV_PCM_FORMAT_S20_3LE:
170 width = SI476X_PCM_FORMAT_S20_3LE;
171 break;
172 case SNDRV_PCM_FORMAT_S24_LE:
173 width = SI476X_PCM_FORMAT_S24_LE;
174 break;
175 default:
176 return -EINVAL;
177 }
178
179 err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE,
180 rate);
181 if (err < 0) {
182 dev_err(dai->codec->dev, "Failed to set sample rate\n");
183 return err;
184 }
185
186 err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
187 SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK,
188 (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) |
189 (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT));
190 if (err < 0) {
191 dev_err(dai->codec->dev, "Failed to set output width\n");
192 return err;
193 }
194
195 return 0;
196 }
197
198 static int si476x_codec_probe(struct snd_soc_codec *codec)
199 {
200 codec->control_data = dev_get_regmap(codec->dev->parent, NULL);
201 return 0;
202 }
203
204 static struct snd_soc_dai_ops si476x_dai_ops = {
205 .hw_params = si476x_codec_hw_params,
206 .set_fmt = si476x_codec_set_dai_fmt,
207 };
208
209 static struct snd_soc_dai_driver si476x_dai = {
210 .name = "si476x-codec",
211 .capture = {
212 .stream_name = "Capture",
213 .channels_min = 2,
214 .channels_max = 2,
215
216 .rates = SNDRV_PCM_RATE_32000 |
217 SNDRV_PCM_RATE_44100 |
218 SNDRV_PCM_RATE_48000,
219 .formats = SNDRV_PCM_FMTBIT_S8 |
220 SNDRV_PCM_FMTBIT_S16_LE |
221 SNDRV_PCM_FMTBIT_S20_3LE |
222 SNDRV_PCM_FMTBIT_S24_LE
223 },
224 .ops = &si476x_dai_ops,
225 };
226
227 static struct snd_soc_codec_driver soc_codec_dev_si476x = {
228 .probe = si476x_codec_probe,
229 .dapm_widgets = si476x_dapm_widgets,
230 .num_dapm_widgets = ARRAY_SIZE(si476x_dapm_widgets),
231 .dapm_routes = si476x_dapm_routes,
232 .num_dapm_routes = ARRAY_SIZE(si476x_dapm_routes),
233 };
234
235 static int si476x_platform_probe(struct platform_device *pdev)
236 {
237 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x,
238 &si476x_dai, 1);
239 }
240
241 static int si476x_platform_remove(struct platform_device *pdev)
242 {
243 snd_soc_unregister_codec(&pdev->dev);
244 return 0;
245 }
246
247 MODULE_ALIAS("platform:si476x-codec");
248
249 static struct platform_driver si476x_platform_driver = {
250 .driver = {
251 .name = "si476x-codec",
252 .owner = THIS_MODULE,
253 },
254 .probe = si476x_platform_probe,
255 .remove = si476x_platform_remove,
256 };
257 module_platform_driver(si476x_platform_driver);
258
259 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
260 MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
261 MODULE_LICENSE("GPL");
This page took 0.039536 seconds and 4 git commands to generate.