]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/soc/codecs/wm8776.c
ASoC: Remove unneeded use of spi_bus_type
[net-next-2.6.git] / sound / soc / codecs / wm8776.c
CommitLineData
924914ee
MB
1/*
2 * wm8776.c -- WM8776 ALSA SoC Audio driver
3 *
4 * Copyright 2009 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * TODO: Input ALC/limiter support
13 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
22#include <linux/spi/spi.h>
5a0e3ad6 23#include <linux/slab.h>
924914ee
MB
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/soc-dapm.h>
29#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "wm8776.h"
33
924914ee
MB
34/* codec private data */
35struct wm8776_priv {
f0fba2ad 36 enum snd_soc_control_type control_type;
924914ee
MB
37 u16 reg_cache[WM8776_CACHEREGNUM];
38 int sysclk[2];
39};
40
924914ee
MB
41static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
42 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
43 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
44 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
45 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
46 0xa6, 0x01, 0x01
47};
48
924914ee
MB
49static int wm8776_reset(struct snd_soc_codec *codec)
50{
35b1207b 51 return snd_soc_write(codec, WM8776_RESET, 0);
924914ee
MB
52}
53
54static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
55static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
56static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
57
58static const struct snd_kcontrol_new wm8776_snd_controls[] = {
59SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
60 0, 127, 0, hp_tlv),
61SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
62 0, 255, 0, dac_tlv),
63SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
64
65SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
66
67SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
68 0, 255, 0, adc_tlv),
69SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
70SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
71SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
72};
73
74static const struct snd_kcontrol_new inmix_controls[] = {
75SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
76SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
77SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
78SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
79SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
80};
81
82static const struct snd_kcontrol_new outmix_controls[] = {
83SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
84SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
85SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
86};
87
88static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
89SND_SOC_DAPM_INPUT("AUX"),
924914ee
MB
90
91SND_SOC_DAPM_INPUT("AIN1"),
92SND_SOC_DAPM_INPUT("AIN2"),
93SND_SOC_DAPM_INPUT("AIN3"),
94SND_SOC_DAPM_INPUT("AIN4"),
95SND_SOC_DAPM_INPUT("AIN5"),
96
97SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
98 inmix_controls, ARRAY_SIZE(inmix_controls)),
99
100SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
101SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
102
103SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
104 outmix_controls, ARRAY_SIZE(outmix_controls)),
105
106SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
107
108SND_SOC_DAPM_OUTPUT("VOUT"),
109
110SND_SOC_DAPM_OUTPUT("HPOUTL"),
111SND_SOC_DAPM_OUTPUT("HPOUTR"),
112};
113
114static const struct snd_soc_dapm_route routes[] = {
115 { "Input Mixer", "AIN1 Switch", "AIN1" },
116 { "Input Mixer", "AIN2 Switch", "AIN2" },
117 { "Input Mixer", "AIN3 Switch", "AIN3" },
118 { "Input Mixer", "AIN4 Switch", "AIN4" },
119 { "Input Mixer", "AIN5 Switch", "AIN5" },
120
121 { "ADC", NULL, "Input Mixer" },
122
123 { "Output Mixer", "DAC Switch", "DAC" },
124 { "Output Mixer", "AUX Switch", "AUX" },
125 { "Output Mixer", "Bypass Switch", "Input Mixer" },
126
127 { "VOUT", NULL, "Output Mixer" },
128
129 { "Headphone PGA", NULL, "Output Mixer" },
130
131 { "HPOUTL", NULL, "Headphone PGA" },
132 { "HPOUTR", NULL, "Headphone PGA" },
133};
134
135static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
136{
137 struct snd_soc_codec *codec = dai->codec;
138 int reg, iface, master;
139
f0fba2ad 140 switch (dai->driver->id) {
924914ee
MB
141 case WM8776_DAI_DAC:
142 reg = WM8776_DACIFCTRL;
143 master = 0x80;
144 break;
145 case WM8776_DAI_ADC:
146 reg = WM8776_ADCIFCTRL;
147 master = 0x100;
148 break;
149 default:
150 return -EINVAL;
151 }
152
153 iface = 0;
154
155 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
156 case SND_SOC_DAIFMT_CBM_CFM:
157 break;
158 case SND_SOC_DAIFMT_CBS_CFS:
159 master = 0;
160 break;
161 default:
162 return -EINVAL;
163 }
164
165 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
166 case SND_SOC_DAIFMT_I2S:
167 iface |= 0x0002;
168 break;
169 case SND_SOC_DAIFMT_RIGHT_J:
170 break;
171 case SND_SOC_DAIFMT_LEFT_J:
172 iface |= 0x0001;
173 break;
924914ee
MB
174 default:
175 return -EINVAL;
176 }
177
178 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
179 case SND_SOC_DAIFMT_NB_NF:
180 break;
181 case SND_SOC_DAIFMT_IB_IF:
182 iface |= 0x00c;
183 break;
184 case SND_SOC_DAIFMT_IB_NF:
185 iface |= 0x008;
186 break;
187 case SND_SOC_DAIFMT_NB_IF:
188 iface |= 0x004;
189 break;
190 default:
191 return -EINVAL;
192 }
193
194 /* Finally, write out the values */
195 snd_soc_update_bits(codec, reg, 0xf, iface);
196 snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
197
198 return 0;
199}
200
201static int mclk_ratios[] = {
202 128,
203 192,
204 256,
205 384,
206 512,
207 768,
208};
209
210static int wm8776_hw_params(struct snd_pcm_substream *substream,
211 struct snd_pcm_hw_params *params,
212 struct snd_soc_dai *dai)
213{
214 struct snd_soc_codec *codec = dai->codec;
b2c812e2 215 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
924914ee
MB
216 int iface_reg, iface;
217 int ratio_shift, master;
218 int i;
219
220 iface = 0;
221
f0fba2ad 222 switch (dai->driver->id) {
924914ee
MB
223 case WM8776_DAI_DAC:
224 iface_reg = WM8776_DACIFCTRL;
225 master = 0x80;
226 ratio_shift = 4;
227 break;
228 case WM8776_DAI_ADC:
229 iface_reg = WM8776_ADCIFCTRL;
230 master = 0x100;
231 ratio_shift = 0;
232 break;
233 default:
234 return -EINVAL;
235 }
236
237
238 /* Set word length */
239 switch (params_format(params)) {
240 case SNDRV_PCM_FORMAT_S16_LE:
241 break;
242 case SNDRV_PCM_FORMAT_S20_3LE:
243 iface |= 0x10;
244 break;
245 case SNDRV_PCM_FORMAT_S24_LE:
246 iface |= 0x20;
247 break;
248 case SNDRV_PCM_FORMAT_S32_LE:
249 iface |= 0x30;
250 break;
251 }
252
253 /* Only need to set MCLK/LRCLK ratio if we're master */
254 if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
255 for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
f0fba2ad 256 if (wm8776->sysclk[dai->driver->id] / params_rate(params)
924914ee
MB
257 == mclk_ratios[i])
258 break;
259 }
260
261 if (i == ARRAY_SIZE(mclk_ratios)) {
262 dev_err(codec->dev,
263 "Unable to configure MCLK ratio %d/%d\n",
f0fba2ad 264 wm8776->sysclk[dai->driver->id], params_rate(params));
924914ee
MB
265 return -EINVAL;
266 }
267
268 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
269
270 snd_soc_update_bits(codec, WM8776_MSTRCTRL,
271 0x7 << ratio_shift, i << ratio_shift);
272 } else {
273 dev_dbg(codec->dev, "DAI in slave mode\n");
274 }
275
276 snd_soc_update_bits(codec, iface_reg, 0x30, iface);
277
278 return 0;
279}
280
281static int wm8776_mute(struct snd_soc_dai *dai, int mute)
282{
283 struct snd_soc_codec *codec = dai->codec;
284
285 return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
286}
287
288static int wm8776_set_sysclk(struct snd_soc_dai *dai,
289 int clk_id, unsigned int freq, int dir)
290{
291 struct snd_soc_codec *codec = dai->codec;
b2c812e2 292 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
924914ee 293
f0fba2ad 294 BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
924914ee 295
f0fba2ad 296 wm8776->sysclk[dai->driver->id] = freq;
924914ee
MB
297
298 return 0;
299}
300
301static int wm8776_set_bias_level(struct snd_soc_codec *codec,
302 enum snd_soc_bias_level level)
303{
304 switch (level) {
305 case SND_SOC_BIAS_ON:
306 break;
307 case SND_SOC_BIAS_PREPARE:
308 break;
309 case SND_SOC_BIAS_STANDBY:
310 if (codec->bias_level == SND_SOC_BIAS_OFF) {
311 /* Disable the global powerdown; DAPM does the rest */
312 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
313 }
314
315 break;
316 case SND_SOC_BIAS_OFF:
317 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
318 break;
319 }
320
321 codec->bias_level = level;
322 return 0;
323}
324
325#define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
326 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
327 SNDRV_PCM_RATE_96000)
328
329
330#define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
331 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
332
333static struct snd_soc_dai_ops wm8776_dac_ops = {
334 .digital_mute = wm8776_mute,
335 .hw_params = wm8776_hw_params,
336 .set_fmt = wm8776_set_fmt,
337 .set_sysclk = wm8776_set_sysclk,
338};
339
340static struct snd_soc_dai_ops wm8776_adc_ops = {
341 .hw_params = wm8776_hw_params,
342 .set_fmt = wm8776_set_fmt,
343 .set_sysclk = wm8776_set_sysclk,
344};
345
f0fba2ad 346static struct snd_soc_dai_driver wm8776_dai[] = {
924914ee 347 {
f0fba2ad
LG
348 .name = "wm8776-hifi-playback",
349 .id = WM8776_DAI_DAC,
924914ee
MB
350 .playback = {
351 .stream_name = "Playback",
352 .channels_min = 2,
353 .channels_max = 2,
354 .rates = WM8776_RATES,
355 .formats = WM8776_FORMATS,
356 },
357 .ops = &wm8776_dac_ops,
358 },
359 {
f0fba2ad
LG
360 .name = "wm8776-hifi-capture",
361 .id = WM8776_DAI_ADC,
924914ee
MB
362 .capture = {
363 .stream_name = "Capture",
364 .channels_min = 2,
365 .channels_max = 2,
366 .rates = WM8776_RATES,
367 .formats = WM8776_FORMATS,
368 },
369 .ops = &wm8776_adc_ops,
370 },
371};
924914ee
MB
372
373#ifdef CONFIG_PM
f0fba2ad 374static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
924914ee 375{
924914ee
MB
376 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
377
378 return 0;
379}
380
f0fba2ad 381static int wm8776_resume(struct snd_soc_codec *codec)
924914ee 382{
924914ee
MB
383 int i;
384 u8 data[2];
385 u16 *cache = codec->reg_cache;
386
387 /* Sync reg_cache with the hardware */
388 for (i = 0; i < ARRAY_SIZE(wm8776_reg); i++) {
e0fb28e0
MB
389 if (cache[i] == wm8776_reg[i])
390 continue;
924914ee
MB
391 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
392 data[1] = cache[i] & 0x00ff;
393 codec->hw_write(codec->control_data, data, 2);
394 }
395
396 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
397
398 return 0;
399}
400#else
401#define wm8776_suspend NULL
402#define wm8776_resume NULL
403#endif
404
f0fba2ad 405static int wm8776_probe(struct snd_soc_codec *codec)
924914ee 406{
f0fba2ad 407 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
924914ee
MB
408 int ret = 0;
409
f0fba2ad
LG
410 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
411 if (ret < 0) {
412 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
413 return ret;
924914ee
MB
414 }
415
f0fba2ad 416 ret = wm8776_reset(codec);
924914ee 417 if (ret < 0) {
f0fba2ad
LG
418 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
419 return ret;
924914ee
MB
420 }
421
f0fba2ad
LG
422 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
423
424 /* Latch the update bits; right channel only since we always
425 * update both. */
426 snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
427 snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
428
924914ee
MB
429 snd_soc_add_controls(codec, wm8776_snd_controls,
430 ARRAY_SIZE(wm8776_snd_controls));
431 snd_soc_dapm_new_controls(codec, wm8776_dapm_widgets,
432 ARRAY_SIZE(wm8776_dapm_widgets));
433 snd_soc_dapm_add_routes(codec, routes, ARRAY_SIZE(routes));
434
924914ee 435 return ret;
924914ee
MB
436}
437
438/* power down chip */
f0fba2ad 439static int wm8776_remove(struct snd_soc_codec *codec)
924914ee 440{
f0fba2ad 441 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
924914ee
MB
442 return 0;
443}
444
f0fba2ad 445static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
924914ee
MB
446 .probe = wm8776_probe,
447 .remove = wm8776_remove,
448 .suspend = wm8776_suspend,
449 .resume = wm8776_resume,
f0fba2ad 450 .set_bias_level = wm8776_set_bias_level,
e5eec34c 451 .reg_cache_size = ARRAY_SIZE(wm8776_reg),
f0fba2ad
LG
452 .reg_word_size = sizeof(u16),
453 .reg_cache_default = wm8776_reg,
924914ee 454};
924914ee
MB
455
456#if defined(CONFIG_SPI_MASTER)
924914ee
MB
457static int __devinit wm8776_spi_probe(struct spi_device *spi)
458{
924914ee 459 struct wm8776_priv *wm8776;
f0fba2ad 460 int ret;
924914ee
MB
461
462 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
463 if (wm8776 == NULL)
464 return -ENOMEM;
465
f0fba2ad
LG
466 wm8776->control_type = SND_SOC_SPI;
467 spi_set_drvdata(spi, wm8776);
924914ee 468
f0fba2ad
LG
469 ret = snd_soc_register_codec(&spi->dev,
470 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
471 if (ret < 0)
472 kfree(wm8776);
473 return ret;
924914ee
MB
474}
475
476static int __devexit wm8776_spi_remove(struct spi_device *spi)
477{
f0fba2ad
LG
478 snd_soc_unregister_codec(&spi->dev);
479 kfree(spi_get_drvdata(spi));
924914ee
MB
480 return 0;
481}
482
924914ee
MB
483static struct spi_driver wm8776_spi_driver = {
484 .driver = {
f0fba2ad 485 .name = "wm8776-codec",
924914ee
MB
486 .owner = THIS_MODULE,
487 },
488 .probe = wm8776_spi_probe,
924914ee
MB
489 .remove = __devexit_p(wm8776_spi_remove),
490};
491#endif /* CONFIG_SPI_MASTER */
492
493#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
494static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
495 const struct i2c_device_id *id)
496{
497 struct wm8776_priv *wm8776;
f0fba2ad 498 int ret;
924914ee
MB
499
500 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
501 if (wm8776 == NULL)
502 return -ENOMEM;
503
924914ee 504 i2c_set_clientdata(i2c, wm8776);
f0fba2ad 505 wm8776->control_type = SND_SOC_I2C;
924914ee 506
f0fba2ad
LG
507 ret = snd_soc_register_codec(&i2c->dev,
508 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
509 if (ret < 0)
510 kfree(wm8776);
511 return ret;
924914ee
MB
512}
513
514static __devexit int wm8776_i2c_remove(struct i2c_client *client)
515{
f0fba2ad
LG
516 snd_soc_unregister_codec(&client->dev);
517 kfree(i2c_get_clientdata(client));
924914ee
MB
518 return 0;
519}
520
924914ee
MB
521static const struct i2c_device_id wm8776_i2c_id[] = {
522 { "wm8776", 0 },
523 { }
524};
525MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
526
527static struct i2c_driver wm8776_i2c_driver = {
528 .driver = {
f0fba2ad 529 .name = "wm8776-codec",
924914ee
MB
530 .owner = THIS_MODULE,
531 },
532 .probe = wm8776_i2c_probe,
533 .remove = __devexit_p(wm8776_i2c_remove),
924914ee
MB
534 .id_table = wm8776_i2c_id,
535};
536#endif
537
538static int __init wm8776_modinit(void)
539{
f0fba2ad 540 int ret = 0;
924914ee
MB
541#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
542 ret = i2c_add_driver(&wm8776_i2c_driver);
543 if (ret != 0) {
f0fba2ad 544 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
924914ee
MB
545 ret);
546 }
547#endif
548#if defined(CONFIG_SPI_MASTER)
549 ret = spi_register_driver(&wm8776_spi_driver);
550 if (ret != 0) {
f0fba2ad 551 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
924914ee
MB
552 ret);
553 }
554#endif
f0fba2ad 555 return ret;
924914ee
MB
556}
557module_init(wm8776_modinit);
558
559static void __exit wm8776_exit(void)
560{
561#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
562 i2c_del_driver(&wm8776_i2c_driver);
563#endif
564#if defined(CONFIG_SPI_MASTER)
565 spi_unregister_driver(&wm8776_spi_driver);
566#endif
567}
568module_exit(wm8776_exit);
569
570MODULE_DESCRIPTION("ASoC WM8776 driver");
571MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
572MODULE_LICENSE("GPL");