Commit | Line | Data |
---|---|---|
06d7c133 AS |
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 | ||
330345eb AS |
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 ((0b111 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \ | |
42 | (0b111 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT)) | |
43 | #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0b1111110) | |
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 unsigned int si476x_codec_read(struct snd_soc_codec *codec, | |
64 | unsigned int reg) | |
65 | { | |
66 | int err; | |
d686500a | 67 | unsigned int val; |
330345eb AS |
68 | struct si476x_core *core = codec->control_data; |
69 | ||
70 | si476x_core_lock(core); | |
d686500a AS |
71 | if (!si476x_core_is_powered_up(core)) |
72 | regcache_cache_only(core->regmap, true); | |
73 | ||
74 | err = regmap_read(core->regmap, reg, &val); | |
75 | ||
76 | if (!si476x_core_is_powered_up(core)) | |
77 | regcache_cache_only(core->regmap, false); | |
330345eb AS |
78 | si476x_core_unlock(core); |
79 | ||
d686500a AS |
80 | if (err < 0) |
81 | return err; | |
82 | ||
83 | return val; | |
330345eb AS |
84 | } |
85 | ||
86 | static int si476x_codec_write(struct snd_soc_codec *codec, | |
87 | unsigned int reg, unsigned int val) | |
88 | { | |
89 | int err; | |
90 | struct si476x_core *core = codec->control_data; | |
91 | ||
92 | si476x_core_lock(core); | |
d686500a AS |
93 | if (!si476x_core_is_powered_up(core)) |
94 | regcache_cache_only(core->regmap, true); | |
95 | ||
96 | err = regmap_write(core->regmap, reg, val); | |
97 | ||
98 | if (!si476x_core_is_powered_up(core)) | |
99 | regcache_cache_only(core->regmap, false); | |
330345eb AS |
100 | si476x_core_unlock(core); |
101 | ||
102 | return err; | |
103 | } | |
104 | ||
105 | static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
106 | unsigned int fmt) | |
107 | { | |
108 | int err; | |
109 | u16 format = 0; | |
110 | ||
111 | if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) | |
112 | return -EINVAL; | |
113 | ||
114 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
115 | case SND_SOC_DAIFMT_DSP_A: | |
116 | format |= SI476X_DAUDIO_MODE_DSP_A; | |
117 | break; | |
118 | case SND_SOC_DAIFMT_DSP_B: | |
119 | format |= SI476X_DAUDIO_MODE_DSP_B; | |
120 | break; | |
121 | case SND_SOC_DAIFMT_I2S: | |
122 | format |= SI476X_DAUDIO_MODE_I2S; | |
123 | break; | |
124 | case SND_SOC_DAIFMT_RIGHT_J: | |
125 | format |= SI476X_DAUDIO_MODE_RIGHT_J; | |
126 | break; | |
127 | case SND_SOC_DAIFMT_LEFT_J: | |
128 | format |= SI476X_DAUDIO_MODE_LEFT_J; | |
129 | break; | |
130 | default: | |
131 | return -EINVAL; | |
132 | } | |
133 | ||
134 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
135 | case SND_SOC_DAIFMT_DSP_A: | |
136 | case SND_SOC_DAIFMT_DSP_B: | |
137 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
138 | case SND_SOC_DAIFMT_NB_NF: | |
139 | break; | |
140 | case SND_SOC_DAIFMT_IB_NF: | |
141 | format |= SI476X_DAUDIO_MODE_IB; | |
142 | break; | |
143 | default: | |
144 | return -EINVAL; | |
145 | } | |
146 | break; | |
147 | case SND_SOC_DAIFMT_I2S: | |
148 | case SND_SOC_DAIFMT_RIGHT_J: | |
149 | case SND_SOC_DAIFMT_LEFT_J: | |
150 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
151 | case SND_SOC_DAIFMT_NB_NF: | |
152 | break; | |
153 | case SND_SOC_DAIFMT_IB_IF: | |
154 | format |= SI476X_DAUDIO_MODE_IB | | |
155 | SI476X_DAUDIO_MODE_IF; | |
156 | break; | |
157 | case SND_SOC_DAIFMT_IB_NF: | |
158 | format |= SI476X_DAUDIO_MODE_IB; | |
159 | break; | |
160 | case SND_SOC_DAIFMT_NB_IF: | |
161 | format |= SI476X_DAUDIO_MODE_IF; | |
162 | break; | |
163 | default: | |
164 | return -EINVAL; | |
165 | } | |
166 | break; | |
167 | default: | |
168 | return -EINVAL; | |
169 | } | |
170 | ||
171 | err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT, | |
172 | SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK, | |
173 | format); | |
174 | if (err < 0) { | |
175 | dev_err(codec_dai->codec->dev, "Failed to set output format\n"); | |
176 | return err; | |
177 | } | |
06d7c133 | 178 | |
330345eb AS |
179 | return 0; |
180 | } | |
181 | ||
182 | static int si476x_codec_hw_params(struct snd_pcm_substream *substream, | |
183 | struct snd_pcm_hw_params *params, | |
184 | struct snd_soc_dai *dai) | |
185 | { | |
186 | int rate, width, err; | |
187 | ||
188 | rate = params_rate(params); | |
189 | if (rate < 32000 || rate > 48000) { | |
190 | dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate); | |
191 | return -EINVAL; | |
192 | } | |
193 | ||
194 | switch (params_format(params)) { | |
195 | case SNDRV_PCM_FORMAT_S8: | |
196 | width = SI476X_PCM_FORMAT_S8; | |
4e38b745 | 197 | break; |
330345eb AS |
198 | case SNDRV_PCM_FORMAT_S16_LE: |
199 | width = SI476X_PCM_FORMAT_S16_LE; | |
200 | break; | |
201 | case SNDRV_PCM_FORMAT_S20_3LE: | |
202 | width = SI476X_PCM_FORMAT_S20_3LE; | |
203 | break; | |
204 | case SNDRV_PCM_FORMAT_S24_LE: | |
205 | width = SI476X_PCM_FORMAT_S24_LE; | |
206 | break; | |
207 | default: | |
208 | return -EINVAL; | |
209 | } | |
210 | ||
211 | err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE, | |
212 | rate); | |
213 | if (err < 0) { | |
214 | dev_err(dai->codec->dev, "Failed to set sample rate\n"); | |
215 | return err; | |
216 | } | |
217 | ||
218 | err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT, | |
219 | SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK, | |
06d7c133 | 220 | (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | |
330345eb AS |
221 | (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT)); |
222 | if (err < 0) { | |
223 | dev_err(dai->codec->dev, "Failed to set output width\n"); | |
224 | return err; | |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | static int si476x_codec_probe(struct snd_soc_codec *codec) | |
231 | { | |
232 | codec->control_data = i2c_mfd_cell_to_core(codec->dev); | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static struct snd_soc_dai_ops si476x_dai_ops = { | |
237 | .hw_params = si476x_codec_hw_params, | |
238 | .set_fmt = si476x_codec_set_dai_fmt, | |
239 | }; | |
240 | ||
241 | static struct snd_soc_dai_driver si476x_dai = { | |
242 | .name = "si476x-codec", | |
243 | .capture = { | |
244 | .stream_name = "Capture", | |
245 | .channels_min = 2, | |
246 | .channels_max = 2, | |
247 | ||
248 | .rates = SNDRV_PCM_RATE_32000 | | |
249 | SNDRV_PCM_RATE_44100 | | |
250 | SNDRV_PCM_RATE_48000, | |
251 | .formats = SNDRV_PCM_FMTBIT_S8 | | |
252 | SNDRV_PCM_FMTBIT_S16_LE | | |
253 | SNDRV_PCM_FMTBIT_S20_3LE | | |
254 | SNDRV_PCM_FMTBIT_S24_LE | |
255 | }, | |
256 | .ops = &si476x_dai_ops, | |
257 | }; | |
258 | ||
259 | static struct snd_soc_codec_driver soc_codec_dev_si476x = { | |
260 | .probe = si476x_codec_probe, | |
261 | .read = si476x_codec_read, | |
262 | .write = si476x_codec_write, | |
263 | }; | |
264 | ||
7a79e94e | 265 | static int si476x_platform_probe(struct platform_device *pdev) |
330345eb AS |
266 | { |
267 | return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x, | |
268 | &si476x_dai, 1); | |
269 | } | |
270 | ||
7a79e94e | 271 | static int si476x_platform_remove(struct platform_device *pdev) |
330345eb AS |
272 | { |
273 | snd_soc_unregister_codec(&pdev->dev); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | MODULE_ALIAS("platform:si476x-codec"); | |
278 | ||
279 | static struct platform_driver si476x_platform_driver = { | |
280 | .driver = { | |
281 | .name = "si476x-codec", | |
282 | .owner = THIS_MODULE, | |
283 | }, | |
284 | .probe = si476x_platform_probe, | |
7a79e94e | 285 | .remove = si476x_platform_remove, |
330345eb AS |
286 | }; |
287 | module_platform_driver(si476x_platform_driver); | |
288 | ||
06d7c133 | 289 | MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>"); |
330345eb AS |
290 | MODULE_DESCRIPTION("ASoC Si4761/64 codec driver"); |
291 | MODULE_LICENSE("GPL"); |