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