ASoC: multi-component - ASoC Multi-Component Support
[deliverable/linux.git] / sound / soc / codecs / wm8741.c
1 /*
2 * wm8741.c -- WM8741 ALSA SoC Audio driver
3 *
4 * Copyright 2010 Wolfson Microelectronics plc
5 *
6 * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
7 *
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 version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
30
31 #include "wm8741.h"
32
33 #define WM8741_NUM_SUPPLIES 2
34 static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
35 "AVDD",
36 "DVDD",
37 };
38
39 #define WM8741_NUM_RATES 4
40
41 /* codec private data */
42 struct wm8741_priv {
43 enum snd_soc_control_type control_type;
44 void *control_data;
45 u16 reg_cache[WM8741_REGISTER_COUNT];
46 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
47 unsigned int sysclk;
48 unsigned int rate_constraint_list[WM8741_NUM_RATES];
49 struct snd_pcm_hw_constraint_list rate_constraint;
50 };
51
52 static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
53 0x0000, /* R0 - DACLLSB Attenuation */
54 0x0000, /* R1 - DACLMSB Attenuation */
55 0x0000, /* R2 - DACRLSB Attenuation */
56 0x0000, /* R3 - DACRMSB Attenuation */
57 0x0000, /* R4 - Volume Control */
58 0x000A, /* R5 - Format Control */
59 0x0000, /* R6 - Filter Control */
60 0x0000, /* R7 - Mode Control 1 */
61 0x0002, /* R8 - Mode Control 2 */
62 0x0000, /* R9 - Reset */
63 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
64 };
65
66
67 static int wm8741_reset(struct snd_soc_codec *codec)
68 {
69 return snd_soc_write(codec, WM8741_RESET, 0);
70 }
71
72 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
73 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
74
75 static const struct snd_kcontrol_new wm8741_snd_controls[] = {
76 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
77 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
78 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
79 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
80 };
81
82 static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
83 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
84 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
85 SND_SOC_DAPM_OUTPUT("VOUTLP"),
86 SND_SOC_DAPM_OUTPUT("VOUTLN"),
87 SND_SOC_DAPM_OUTPUT("VOUTRP"),
88 SND_SOC_DAPM_OUTPUT("VOUTRN"),
89 };
90
91 static const struct snd_soc_dapm_route intercon[] = {
92 { "VOUTLP", NULL, "DACL" },
93 { "VOUTLN", NULL, "DACL" },
94 { "VOUTRP", NULL, "DACR" },
95 { "VOUTRN", NULL, "DACR" },
96 };
97
98 static int wm8741_add_widgets(struct snd_soc_codec *codec)
99 {
100 snd_soc_dapm_new_controls(codec, wm8741_dapm_widgets,
101 ARRAY_SIZE(wm8741_dapm_widgets));
102
103 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
104
105 return 0;
106 }
107
108 static struct {
109 int value;
110 int ratio;
111 } lrclk_ratios[WM8741_NUM_RATES] = {
112 { 1, 256 },
113 { 2, 384 },
114 { 3, 512 },
115 { 4, 768 },
116 };
117
118
119 static int wm8741_startup(struct snd_pcm_substream *substream,
120 struct snd_soc_dai *dai)
121 {
122 struct snd_soc_codec *codec = dai->codec;
123 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
124
125 /* The set of sample rates that can be supported depends on the
126 * MCLK supplied to the CODEC - enforce this.
127 */
128 if (!wm8741->sysclk) {
129 dev_err(codec->dev,
130 "No MCLK configured, call set_sysclk() on init\n");
131 return -EINVAL;
132 }
133
134 snd_pcm_hw_constraint_list(substream->runtime, 0,
135 SNDRV_PCM_HW_PARAM_RATE,
136 &wm8741->rate_constraint);
137
138 return 0;
139 }
140
141 static int wm8741_hw_params(struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *params,
143 struct snd_soc_dai *dai)
144 {
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 struct snd_soc_codec *codec = rtd->codec;
147 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
148 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
149 int i;
150
151 /* Find a supported LRCLK ratio */
152 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
153 if (wm8741->sysclk / params_rate(params) ==
154 lrclk_ratios[i].ratio)
155 break;
156 }
157
158 /* Should never happen, should be handled by constraints */
159 if (i == ARRAY_SIZE(lrclk_ratios)) {
160 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
161 wm8741->sysclk / params_rate(params));
162 return -EINVAL;
163 }
164
165 /* bit size */
166 switch (params_format(params)) {
167 case SNDRV_PCM_FORMAT_S16_LE:
168 break;
169 case SNDRV_PCM_FORMAT_S20_3LE:
170 iface |= 0x0001;
171 break;
172 case SNDRV_PCM_FORMAT_S24_LE:
173 iface |= 0x0002;
174 break;
175 case SNDRV_PCM_FORMAT_S32_LE:
176 iface |= 0x0003;
177 break;
178 default:
179 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
180 params_format(params));
181 return -EINVAL;
182 }
183
184 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
185 params_format(params));
186
187 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
188 return 0;
189 }
190
191 static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
192 int clk_id, unsigned int freq, int dir)
193 {
194 struct snd_soc_codec *codec = codec_dai->codec;
195 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
196 unsigned int val;
197 int i;
198
199 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
200
201 wm8741->sysclk = freq;
202
203 wm8741->rate_constraint.count = 0;
204
205 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
206 dev_dbg(codec->dev, "index = %d, ratio = %d, freq = %d",
207 i, lrclk_ratios[i].ratio, freq);
208
209 val = freq / lrclk_ratios[i].ratio;
210 /* Check that it's a standard rate since core can't
211 * cope with others and having the odd rates confuses
212 * constraint matching.
213 */
214 switch (val) {
215 case 32000:
216 case 44100:
217 case 48000:
218 case 64000:
219 case 88200:
220 case 96000:
221 dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
222 val);
223 wm8741->rate_constraint_list[i] = val;
224 wm8741->rate_constraint.count++;
225 break;
226 default:
227 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
228 val);
229 }
230 }
231
232 /* Need at least one supported rate... */
233 if (wm8741->rate_constraint.count == 0)
234 return -EINVAL;
235
236 return 0;
237 }
238
239 static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
240 unsigned int fmt)
241 {
242 struct snd_soc_codec *codec = codec_dai->codec;
243 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
244
245 /* check master/slave audio interface */
246 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
247 case SND_SOC_DAIFMT_CBS_CFS:
248 break;
249 default:
250 return -EINVAL;
251 }
252
253 /* interface format */
254 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
255 case SND_SOC_DAIFMT_I2S:
256 iface |= 0x0008;
257 break;
258 case SND_SOC_DAIFMT_RIGHT_J:
259 break;
260 case SND_SOC_DAIFMT_LEFT_J:
261 iface |= 0x0004;
262 break;
263 case SND_SOC_DAIFMT_DSP_A:
264 iface |= 0x0003;
265 break;
266 case SND_SOC_DAIFMT_DSP_B:
267 iface |= 0x0013;
268 break;
269 default:
270 return -EINVAL;
271 }
272
273 /* clock inversion */
274 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
275 case SND_SOC_DAIFMT_NB_NF:
276 break;
277 case SND_SOC_DAIFMT_IB_IF:
278 iface |= 0x0010;
279 break;
280 case SND_SOC_DAIFMT_IB_NF:
281 iface |= 0x0020;
282 break;
283 case SND_SOC_DAIFMT_NB_IF:
284 iface |= 0x0030;
285 break;
286 default:
287 return -EINVAL;
288 }
289
290
291 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
292 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
293 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
294
295 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
296 return 0;
297 }
298
299 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
300 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
301 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
302 SNDRV_PCM_RATE_192000)
303
304 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
305 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
306
307 static struct snd_soc_dai_ops wm8741_dai_ops = {
308 .startup = wm8741_startup,
309 .hw_params = wm8741_hw_params,
310 .set_sysclk = wm8741_set_dai_sysclk,
311 .set_fmt = wm8741_set_dai_fmt,
312 };
313
314 static struct snd_soc_dai_driver wm8741_dai = {
315 .name = "WM8741",
316 .playback = {
317 .stream_name = "Playback",
318 .channels_min = 2, /* Mono modes not yet supported */
319 .channels_max = 2,
320 .rates = WM8741_RATES,
321 .formats = WM8741_FORMATS,
322 },
323 .ops = &wm8741_dai_ops,
324 };
325
326 #ifdef CONFIG_PM
327 static int wm8741_resume(struct snd_soc_codec *codec)
328 {
329 u16 *cache = codec->reg_cache;
330 int i;
331
332 /* RESTORE REG Cache */
333 for (i = 0; i < WM8741_REGISTER_COUNT; i++) {
334 if (cache[i] == wm8741_reg_defaults[i] || WM8741_RESET == i)
335 continue;
336 snd_soc_write(codec, i, cache[i]);
337 }
338 return 0;
339 }
340 #else
341 #define wm8741_suspend NULL
342 #define wm8741_resume NULL
343 #endif
344
345 static int wm8741_probe(struct snd_soc_codec *codec)
346 {
347 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
348 int ret = 0;
349
350 codec->control_data = wm8741->control_data;
351 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
352 if (ret != 0) {
353 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
354 return ret;
355 }
356
357 ret = wm8741_reset(codec);
358 if (ret < 0) {
359 dev_err(codec->dev, "Failed to issue reset\n");
360 return ret;
361 }
362
363 /* Change some default settings - latch VU */
364 wm8741->reg_cache[WM8741_DACLLSB_ATTENUATION] |= WM8741_UPDATELL;
365 wm8741->reg_cache[WM8741_DACLMSB_ATTENUATION] |= WM8741_UPDATELM;
366 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERL;
367 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERM;
368
369 snd_soc_add_controls(codec, wm8741_snd_controls,
370 ARRAY_SIZE(wm8741_snd_controls));
371 wm8741_add_widgets(codec);
372
373 dev_dbg(codec->dev, "Successful registration\n");
374 return ret;
375 }
376
377 static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
378 .probe = wm8741_probe,
379 .resume = wm8741_resume,
380 .reg_cache_size = sizeof(wm8741_reg_defaults),
381 .reg_word_size = sizeof(u16),
382 .reg_cache_default = &wm8741_reg_defaults,
383 };
384
385 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
386 static int wm8741_i2c_probe(struct i2c_client *i2c,
387 const struct i2c_device_id *id)
388 {
389 struct wm8741_priv *wm8741;
390 int ret, i;
391
392 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
393 if (wm8741 == NULL)
394 return -ENOMEM;
395
396 wm8741->rate_constraint.list = &wm8741->rate_constraint_list[0];
397 wm8741->rate_constraint.count =
398 ARRAY_SIZE(wm8741->rate_constraint_list);
399
400 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
401 wm8741->supplies[i].supply = wm8741_supply_names[i];
402
403 ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
404 wm8741->supplies);
405 if (ret != 0) {
406 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
407 goto err;
408 }
409
410 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
411 wm8741->supplies);
412 if (ret != 0) {
413 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
414 goto err_get;
415 }
416
417 i2c_set_clientdata(i2c, wm8741);
418 wm8741->control_data = i2c;
419 wm8741->control_type = SND_SOC_I2C;
420
421 ret = snd_soc_register_codec(&i2c->dev,
422 &soc_codec_dev_wm8741, &wm8741_dai, 1);
423 if (ret < 0)
424 goto err_enable;
425 return ret;
426
427 err_enable:
428 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
429
430 err_get:
431 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
432 err:
433 kfree(wm8741);
434 return ret;
435 }
436
437 static int wm8741_i2c_remove(struct i2c_client *client)
438 {
439 struct wm8741_priv *wm8741 = i2c_get_clientdata(client);
440
441 snd_soc_unregister_codec(&client->dev);
442 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
443 kfree(i2c_get_clientdata(client));
444 return 0;
445 }
446
447 static const struct i2c_device_id wm8741_i2c_id[] = {
448 { "wm8741", 0 },
449 { }
450 };
451 MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
452
453 static struct i2c_driver wm8741_i2c_driver = {
454 .driver = {
455 .name = "wm8741-codec",
456 .owner = THIS_MODULE,
457 },
458 .probe = wm8741_i2c_probe,
459 .remove = wm8741_i2c_remove,
460 .id_table = wm8741_i2c_id,
461 };
462 #endif
463
464 static int __init wm8741_modinit(void)
465 {
466 int ret = 0;
467
468 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
469 ret = i2c_add_driver(&wm8741_i2c_driver);
470 if (ret != 0) {
471 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
472 }
473 #endif
474
475 return ret;
476 }
477 module_init(wm8741_modinit);
478
479 static void __exit wm8741_exit(void)
480 {
481 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
482 i2c_del_driver(&wm8741_i2c_driver);
483 #endif
484 }
485 module_exit(wm8741_exit);
486
487 MODULE_DESCRIPTION("ASoC WM8741 driver");
488 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
489 MODULE_LICENSE("GPL");
This page took 0.041775 seconds and 5 git commands to generate.