ASoC: tlv320aic23: Remove driver-specific version number
[deliverable/linux.git] / sound / soc / codecs / wm8741.c
CommitLineData
992bee40
IL
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>
39e9b8d2 20#include <linux/spi/spi.h>
992bee40
IL
21#include <linux/regulator/consumer.h>
22#include <linux/slab.h>
80080ec5 23#include <linux/of_device.h>
992bee40
IL
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
992bee40
IL
28#include <sound/initval.h>
29#include <sound/tlv.h>
30
31#include "wm8741.h"
32
992bee40
IL
33#define WM8741_NUM_SUPPLIES 2
34static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
35 "AVDD",
36 "DVDD",
37};
38
3fe4a5ee 39#define WM8741_NUM_RATES 6
992bee40
IL
40
41/* codec private data */
42struct wm8741_priv {
f0fba2ad 43 enum snd_soc_control_type control_type;
992bee40
IL
44 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
45 unsigned int sysclk;
3fe4a5ee 46 struct snd_pcm_hw_constraint_list *sysclk_constraints;
992bee40
IL
47};
48
49static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
50 0x0000, /* R0 - DACLLSB Attenuation */
51 0x0000, /* R1 - DACLMSB Attenuation */
52 0x0000, /* R2 - DACRLSB Attenuation */
53 0x0000, /* R3 - DACRMSB Attenuation */
54 0x0000, /* R4 - Volume Control */
55 0x000A, /* R5 - Format Control */
56 0x0000, /* R6 - Filter Control */
57 0x0000, /* R7 - Mode Control 1 */
58 0x0002, /* R8 - Mode Control 2 */
59 0x0000, /* R9 - Reset */
60 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
61};
62
63
64static int wm8741_reset(struct snd_soc_codec *codec)
65{
66 return snd_soc_write(codec, WM8741_RESET, 0);
67}
68
69static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
70static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
71
72static const struct snd_kcontrol_new wm8741_snd_controls[] = {
73SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
74 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
75SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
76 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
77};
78
79static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
80SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
81SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
82SND_SOC_DAPM_OUTPUT("VOUTLP"),
83SND_SOC_DAPM_OUTPUT("VOUTLN"),
84SND_SOC_DAPM_OUTPUT("VOUTRP"),
85SND_SOC_DAPM_OUTPUT("VOUTRN"),
86};
87
0e62780f 88static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
992bee40
IL
89 { "VOUTLP", NULL, "DACL" },
90 { "VOUTLN", NULL, "DACL" },
91 { "VOUTRP", NULL, "DACR" },
92 { "VOUTRN", NULL, "DACR" },
93};
94
992bee40
IL
95static struct {
96 int value;
97 int ratio;
98} lrclk_ratios[WM8741_NUM_RATES] = {
3fe4a5ee
IL
99 { 1, 128 },
100 { 2, 192 },
101 { 3, 256 },
102 { 4, 384 },
103 { 5, 512 },
104 { 6, 768 },
105};
106
107static unsigned int rates_11289[] = {
108 44100, 88235,
109};
110
111static struct snd_pcm_hw_constraint_list constraints_11289 = {
112 .count = ARRAY_SIZE(rates_11289),
113 .list = rates_11289,
114};
115
116static unsigned int rates_12288[] = {
117 32000, 48000, 96000,
118};
119
120static struct snd_pcm_hw_constraint_list constraints_12288 = {
121 .count = ARRAY_SIZE(rates_12288),
122 .list = rates_12288,
123};
124
125static unsigned int rates_16384[] = {
126 32000,
127};
128
129static struct snd_pcm_hw_constraint_list constraints_16384 = {
130 .count = ARRAY_SIZE(rates_16384),
131 .list = rates_16384,
132};
133
134static unsigned int rates_16934[] = {
135 44100, 88235,
136};
137
138static struct snd_pcm_hw_constraint_list constraints_16934 = {
139 .count = ARRAY_SIZE(rates_16934),
140 .list = rates_16934,
141};
142
143static unsigned int rates_18432[] = {
144 48000, 96000,
145};
146
147static struct snd_pcm_hw_constraint_list constraints_18432 = {
148 .count = ARRAY_SIZE(rates_18432),
149 .list = rates_18432,
150};
151
152static unsigned int rates_22579[] = {
153 44100, 88235, 1764000
154};
155
156static struct snd_pcm_hw_constraint_list constraints_22579 = {
157 .count = ARRAY_SIZE(rates_22579),
158 .list = rates_22579,
159};
160
161static unsigned int rates_24576[] = {
162 32000, 48000, 96000, 192000
163};
164
165static struct snd_pcm_hw_constraint_list constraints_24576 = {
166 .count = ARRAY_SIZE(rates_24576),
167 .list = rates_24576,
168};
169
170static unsigned int rates_36864[] = {
171 48000, 96000, 19200
172};
173
174static struct snd_pcm_hw_constraint_list constraints_36864 = {
175 .count = ARRAY_SIZE(rates_36864),
176 .list = rates_36864,
992bee40
IL
177};
178
179
180static int wm8741_startup(struct snd_pcm_substream *substream,
181 struct snd_soc_dai *dai)
182{
183 struct snd_soc_codec *codec = dai->codec;
184 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
185
186 /* The set of sample rates that can be supported depends on the
187 * MCLK supplied to the CODEC - enforce this.
188 */
189 if (!wm8741->sysclk) {
190 dev_err(codec->dev,
191 "No MCLK configured, call set_sysclk() on init\n");
192 return -EINVAL;
193 }
194
195 snd_pcm_hw_constraint_list(substream->runtime, 0,
196 SNDRV_PCM_HW_PARAM_RATE,
3fe4a5ee 197 wm8741->sysclk_constraints);
992bee40
IL
198
199 return 0;
200}
201
202static int wm8741_hw_params(struct snd_pcm_substream *substream,
203 struct snd_pcm_hw_params *params,
204 struct snd_soc_dai *dai)
205{
206 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 207 struct snd_soc_codec *codec = rtd->codec;
992bee40
IL
208 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
209 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
210 int i;
211
212 /* Find a supported LRCLK ratio */
213 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
214 if (wm8741->sysclk / params_rate(params) ==
215 lrclk_ratios[i].ratio)
216 break;
217 }
218
219 /* Should never happen, should be handled by constraints */
220 if (i == ARRAY_SIZE(lrclk_ratios)) {
221 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
222 wm8741->sysclk / params_rate(params));
223 return -EINVAL;
224 }
225
226 /* bit size */
227 switch (params_format(params)) {
228 case SNDRV_PCM_FORMAT_S16_LE:
229 break;
230 case SNDRV_PCM_FORMAT_S20_3LE:
231 iface |= 0x0001;
232 break;
233 case SNDRV_PCM_FORMAT_S24_LE:
234 iface |= 0x0002;
235 break;
236 case SNDRV_PCM_FORMAT_S32_LE:
237 iface |= 0x0003;
238 break;
239 default:
240 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
241 params_format(params));
242 return -EINVAL;
243 }
244
245 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
246 params_format(params));
247
248 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
249 return 0;
250}
251
252static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
253 int clk_id, unsigned int freq, int dir)
254{
255 struct snd_soc_codec *codec = codec_dai->codec;
256 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
992bee40
IL
257
258 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
259
3fe4a5ee
IL
260 switch (freq) {
261 case 11289600:
262 wm8741->sysclk_constraints = &constraints_11289;
263 wm8741->sysclk = freq;
264 return 0;
265
266 case 12288000:
267 wm8741->sysclk_constraints = &constraints_12288;
268 wm8741->sysclk = freq;
269 return 0;
270
271 case 16384000:
272 wm8741->sysclk_constraints = &constraints_16384;
273 wm8741->sysclk = freq;
274 return 0;
275
276 case 16934400:
277 wm8741->sysclk_constraints = &constraints_16934;
278 wm8741->sysclk = freq;
279 return 0;
280
281 case 18432000:
282 wm8741->sysclk_constraints = &constraints_18432;
283 wm8741->sysclk = freq;
284 return 0;
285
286 case 22579200:
287 case 33868800:
288 wm8741->sysclk_constraints = &constraints_22579;
289 wm8741->sysclk = freq;
290 return 0;
291
292 case 24576000:
293 wm8741->sysclk_constraints = &constraints_24576;
294 wm8741->sysclk = freq;
295 return 0;
296
297 case 36864000:
298 wm8741->sysclk_constraints = &constraints_36864;
299 wm8741->sysclk = freq;
300 return 0;
992bee40 301 }
3fe4a5ee 302 return -EINVAL;
992bee40
IL
303}
304
305static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
306 unsigned int fmt)
307{
308 struct snd_soc_codec *codec = codec_dai->codec;
309 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
310
311 /* check master/slave audio interface */
312 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
313 case SND_SOC_DAIFMT_CBS_CFS:
314 break;
315 default:
316 return -EINVAL;
317 }
318
319 /* interface format */
320 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
321 case SND_SOC_DAIFMT_I2S:
322 iface |= 0x0008;
323 break;
324 case SND_SOC_DAIFMT_RIGHT_J:
325 break;
326 case SND_SOC_DAIFMT_LEFT_J:
327 iface |= 0x0004;
328 break;
329 case SND_SOC_DAIFMT_DSP_A:
3a340104 330 iface |= 0x000C;
992bee40
IL
331 break;
332 case SND_SOC_DAIFMT_DSP_B:
3a340104 333 iface |= 0x001C;
992bee40
IL
334 break;
335 default:
336 return -EINVAL;
337 }
338
339 /* clock inversion */
340 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
341 case SND_SOC_DAIFMT_NB_NF:
342 break;
343 case SND_SOC_DAIFMT_IB_IF:
344 iface |= 0x0010;
345 break;
346 case SND_SOC_DAIFMT_IB_NF:
347 iface |= 0x0020;
348 break;
349 case SND_SOC_DAIFMT_NB_IF:
350 iface |= 0x0030;
351 break;
352 default:
353 return -EINVAL;
354 }
355
356
357 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
358 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
359 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
360
361 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
362 return 0;
363}
364
365#define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
366 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
367 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
368 SNDRV_PCM_RATE_192000)
369
370#define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
371 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
372
85e7652d 373static const struct snd_soc_dai_ops wm8741_dai_ops = {
992bee40
IL
374 .startup = wm8741_startup,
375 .hw_params = wm8741_hw_params,
376 .set_sysclk = wm8741_set_dai_sysclk,
377 .set_fmt = wm8741_set_dai_fmt,
378};
379
f0fba2ad 380static struct snd_soc_dai_driver wm8741_dai = {
30e2d368 381 .name = "wm8741",
992bee40
IL
382 .playback = {
383 .stream_name = "Playback",
384 .channels_min = 2, /* Mono modes not yet supported */
385 .channels_max = 2,
386 .rates = WM8741_RATES,
387 .formats = WM8741_FORMATS,
388 },
389 .ops = &wm8741_dai_ops,
390};
992bee40
IL
391
392#ifdef CONFIG_PM
f0fba2ad 393static int wm8741_resume(struct snd_soc_codec *codec)
992bee40 394{
df3431b7 395 snd_soc_cache_sync(codec);
992bee40
IL
396 return 0;
397}
398#else
399#define wm8741_suspend NULL
400#define wm8741_resume NULL
401#endif
402
f0fba2ad 403static int wm8741_probe(struct snd_soc_codec *codec)
992bee40 404{
f0fba2ad 405 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
992bee40 406 int ret = 0;
398575db
MB
407 int i;
408
409 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
410 wm8741->supplies[i].supply = wm8741_supply_names[i];
411
412 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8741->supplies),
413 wm8741->supplies);
414 if (ret != 0) {
415 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
416 goto err;
417 }
418
419 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
420 wm8741->supplies);
421 if (ret != 0) {
422 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
423 goto err_get;
424 }
992bee40 425
f0fba2ad
LG
426 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
427 if (ret != 0) {
428 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
398575db 429 goto err_enable;
992bee40
IL
430 }
431
f0fba2ad 432 ret = wm8741_reset(codec);
992bee40 433 if (ret < 0) {
f0fba2ad 434 dev_err(codec->dev, "Failed to issue reset\n");
398575db 435 goto err_enable;
992bee40
IL
436 }
437
f0fba2ad 438 /* Change some default settings - latch VU */
a1b3b5ee
MB
439 snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
440 WM8741_UPDATELL, WM8741_UPDATELL);
441 snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
442 WM8741_UPDATELM, WM8741_UPDATELM);
443 snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
444 WM8741_UPDATERL, WM8741_UPDATERL);
9b5999b1 445 snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
a1b3b5ee 446 WM8741_UPDATERM, WM8741_UPDATERM);
f0fba2ad 447
f0fba2ad 448 dev_dbg(codec->dev, "Successful registration\n");
992bee40 449 return ret;
398575db
MB
450
451err_enable:
452 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
453err_get:
454 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
455err:
456 return ret;
457}
458
459static int wm8741_remove(struct snd_soc_codec *codec)
460{
461 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
462
463 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
464 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
465
466 return 0;
992bee40
IL
467}
468
f0fba2ad 469static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
992bee40 470 .probe = wm8741_probe,
398575db 471 .remove = wm8741_remove,
992bee40 472 .resume = wm8741_resume,
e5eec34c 473 .reg_cache_size = ARRAY_SIZE(wm8741_reg_defaults),
f0fba2ad 474 .reg_word_size = sizeof(u16),
0aa34b16 475 .reg_cache_default = wm8741_reg_defaults,
0e62780f
MB
476
477 .controls = wm8741_snd_controls,
478 .num_controls = ARRAY_SIZE(wm8741_snd_controls),
479 .dapm_widgets = wm8741_dapm_widgets,
480 .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
481 .dapm_routes = wm8741_dapm_routes,
482 .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
992bee40 483};
992bee40 484
80080ec5
MB
485static const struct of_device_id wm8741_of_match[] = {
486 { .compatible = "wlf,wm8741", },
487 { }
488};
489MODULE_DEVICE_TABLE(of, wm8741_of_match);
490
f0fba2ad
LG
491#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
492static int wm8741_i2c_probe(struct i2c_client *i2c,
493 const struct i2c_device_id *id)
992bee40 494{
f0fba2ad 495 struct wm8741_priv *wm8741;
398575db 496 int ret;
992bee40 497
5aefb306
MB
498 wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
499 GFP_KERNEL);
f0fba2ad
LG
500 if (wm8741 == NULL)
501 return -ENOMEM;
992bee40 502
f0fba2ad 503 i2c_set_clientdata(i2c, wm8741);
f0fba2ad 504 wm8741->control_type = SND_SOC_I2C;
992bee40 505
398575db
MB
506 ret = snd_soc_register_codec(&i2c->dev,
507 &soc_codec_dev_wm8741, &wm8741_dai, 1);
992bee40 508
992bee40
IL
509 return ret;
510}
511
f0fba2ad 512static int wm8741_i2c_remove(struct i2c_client *client)
992bee40 513{
f0fba2ad 514 snd_soc_unregister_codec(&client->dev);
992bee40
IL
515 return 0;
516}
517
518static const struct i2c_device_id wm8741_i2c_id[] = {
519 { "wm8741", 0 },
520 { }
521};
522MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
523
992bee40
IL
524static struct i2c_driver wm8741_i2c_driver = {
525 .driver = {
0473e61b 526 .name = "wm8741",
992bee40 527 .owner = THIS_MODULE,
80080ec5 528 .of_match_table = wm8741_of_match,
992bee40
IL
529 },
530 .probe = wm8741_i2c_probe,
f0fba2ad 531 .remove = wm8741_i2c_remove,
992bee40
IL
532 .id_table = wm8741_i2c_id,
533};
534#endif
535
39e9b8d2
MB
536#if defined(CONFIG_SPI_MASTER)
537static int __devinit wm8741_spi_probe(struct spi_device *spi)
538{
539 struct wm8741_priv *wm8741;
540 int ret;
541
5aefb306
MB
542 wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
543 GFP_KERNEL);
39e9b8d2
MB
544 if (wm8741 == NULL)
545 return -ENOMEM;
546
547 wm8741->control_type = SND_SOC_SPI;
548 spi_set_drvdata(spi, wm8741);
549
550 ret = snd_soc_register_codec(&spi->dev,
551 &soc_codec_dev_wm8741, &wm8741_dai, 1);
39e9b8d2
MB
552 return ret;
553}
554
555static int __devexit wm8741_spi_remove(struct spi_device *spi)
556{
557 snd_soc_unregister_codec(&spi->dev);
39e9b8d2
MB
558 return 0;
559}
560
561static struct spi_driver wm8741_spi_driver = {
562 .driver = {
563 .name = "wm8741",
564 .owner = THIS_MODULE,
80080ec5 565 .of_match_table = wm8741_of_match,
39e9b8d2
MB
566 },
567 .probe = wm8741_spi_probe,
568 .remove = __devexit_p(wm8741_spi_remove),
569};
570#endif /* CONFIG_SPI_MASTER */
571
992bee40
IL
572static int __init wm8741_modinit(void)
573{
f0fba2ad
LG
574 int ret = 0;
575
992bee40
IL
576#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
577 ret = i2c_add_driver(&wm8741_i2c_driver);
3fe4a5ee 578 if (ret != 0)
f0fba2ad 579 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
992bee40 580#endif
39e9b8d2
MB
581#if defined(CONFIG_SPI_MASTER)
582 ret = spi_register_driver(&wm8741_spi_driver);
583 if (ret != 0) {
584 printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
585 ret);
586 }
587#endif
f0fba2ad
LG
588
589 return ret;
992bee40
IL
590}
591module_init(wm8741_modinit);
592
593static void __exit wm8741_exit(void)
594{
39e9b8d2
MB
595#if defined(CONFIG_SPI_MASTER)
596 spi_unregister_driver(&wm8741_spi_driver);
597#endif
992bee40
IL
598#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
599 i2c_del_driver(&wm8741_i2c_driver);
600#endif
601}
602module_exit(wm8741_exit);
603
604MODULE_DESCRIPTION("ASoC WM8741 driver");
605MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
606MODULE_LICENSE("GPL");
This page took 0.126471 seconds and 5 git commands to generate.