Merge branch 'for-2.6.29' into for-2.6.30
[deliverable/linux.git] / sound / soc / codecs / wm8728.c
CommitLineData
71cfc902
MB
1/*
2 * wm8728.c -- WM8728 ALSA SoC Audio driver
3 *
4 * Copyright 2008 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
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/pm.h>
18#include <linux/i2c.h>
19#include <linux/platform_device.h>
20#include <linux/spi/spi.h>
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26#include <sound/initval.h>
27#include <sound/tlv.h>
28
29#include "wm8728.h"
30
31struct snd_soc_codec_device soc_codec_dev_wm8728;
32
33/*
34 * We can't read the WM8728 register space so we cache them instead.
35 * Note that the defaults here aren't the physical defaults, we latch
36 * the volume update bits, mute the output and enable infinite zero
37 * detect.
38 */
39static const u16 wm8728_reg_defaults[] = {
40 0x1ff,
41 0x1ff,
42 0x001,
43 0x100,
44};
45
46static inline unsigned int wm8728_read_reg_cache(struct snd_soc_codec *codec,
47 unsigned int reg)
48{
49 u16 *cache = codec->reg_cache;
91432e97 50 BUG_ON(reg >= ARRAY_SIZE(wm8728_reg_defaults));
71cfc902
MB
51 return cache[reg];
52}
53
54static inline void wm8728_write_reg_cache(struct snd_soc_codec *codec,
55 u16 reg, unsigned int value)
56{
57 u16 *cache = codec->reg_cache;
91432e97 58 BUG_ON(reg >= ARRAY_SIZE(wm8728_reg_defaults));
71cfc902
MB
59 cache[reg] = value;
60}
61
62/*
63 * write to the WM8728 register space
64 */
65static int wm8728_write(struct snd_soc_codec *codec, unsigned int reg,
66 unsigned int value)
67{
68 u8 data[2];
69
70 /* data is
71 * D15..D9 WM8728 register offset
72 * D8...D0 register data
73 */
74 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
75 data[1] = value & 0x00ff;
76
77 wm8728_write_reg_cache(codec, reg, value);
78
79 if (codec->hw_write(codec->control_data, data, 2) == 2)
80 return 0;
81 else
82 return -EIO;
83}
84
85static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
86
87static const struct snd_kcontrol_new wm8728_snd_controls[] = {
88
89SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
90 0, 255, 0, wm8728_tlv),
91
92SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
93};
94
71cfc902
MB
95/*
96 * DAPM controls.
97 */
98static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
99SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
100SND_SOC_DAPM_OUTPUT("VOUTL"),
101SND_SOC_DAPM_OUTPUT("VOUTR"),
102};
103
104static const struct snd_soc_dapm_route intercon[] = {
105 {"VOUTL", NULL, "DAC"},
106 {"VOUTR", NULL, "DAC"},
107};
108
109static int wm8728_add_widgets(struct snd_soc_codec *codec)
110{
111 snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
112 ARRAY_SIZE(wm8728_dapm_widgets));
113
114 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
115
116 snd_soc_dapm_new_widgets(codec);
117
118 return 0;
119}
120
121static int wm8728_mute(struct snd_soc_dai *dai, int mute)
122{
123 struct snd_soc_codec *codec = dai->codec;
124 u16 mute_reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
125
126 if (mute)
127 wm8728_write(codec, WM8728_DACCTL, mute_reg | 1);
128 else
129 wm8728_write(codec, WM8728_DACCTL, mute_reg & ~1);
130
131 return 0;
132}
133
134static int wm8728_hw_params(struct snd_pcm_substream *substream,
dee89c4d
MB
135 struct snd_pcm_hw_params *params,
136 struct snd_soc_dai *dai)
71cfc902
MB
137{
138 struct snd_soc_pcm_runtime *rtd = substream->private_data;
139 struct snd_soc_device *socdev = rtd->socdev;
6627a653 140 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
141 u16 dac = wm8728_read_reg_cache(codec, WM8728_DACCTL);
142
143 dac &= ~0x18;
144
145 switch (params_format(params)) {
146 case SNDRV_PCM_FORMAT_S16_LE:
147 break;
148 case SNDRV_PCM_FORMAT_S20_3LE:
149 dac |= 0x10;
150 break;
151 case SNDRV_PCM_FORMAT_S24_LE:
152 dac |= 0x08;
153 break;
154 default:
155 return -EINVAL;
156 }
157
158 wm8728_write(codec, WM8728_DACCTL, dac);
159
160 return 0;
161}
162
163static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
164 unsigned int fmt)
165{
166 struct snd_soc_codec *codec = codec_dai->codec;
167 u16 iface = wm8728_read_reg_cache(codec, WM8728_IFCTL);
168
169 /* Currently only I2S is supported by the driver, though the
170 * hardware is more flexible.
171 */
172 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
173 case SND_SOC_DAIFMT_I2S:
174 iface |= 1;
175 break;
176 default:
177 return -EINVAL;
178 }
179
180 /* The hardware only support full slave mode */
181 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
182 case SND_SOC_DAIFMT_CBS_CFS:
183 break;
184 default:
185 return -EINVAL;
186 }
187
188 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
189 case SND_SOC_DAIFMT_NB_NF:
190 iface &= ~0x22;
191 break;
192 case SND_SOC_DAIFMT_IB_NF:
193 iface |= 0x20;
194 iface &= ~0x02;
195 break;
196 case SND_SOC_DAIFMT_NB_IF:
197 iface |= 0x02;
198 iface &= ~0x20;
199 break;
200 case SND_SOC_DAIFMT_IB_IF:
201 iface |= 0x22;
202 break;
203 default:
204 return -EINVAL;
205 }
206
207 wm8728_write(codec, WM8728_IFCTL, iface);
208 return 0;
209}
210
211static int wm8728_set_bias_level(struct snd_soc_codec *codec,
212 enum snd_soc_bias_level level)
213{
214 u16 reg;
215 int i;
216
217 switch (level) {
218 case SND_SOC_BIAS_ON:
219 case SND_SOC_BIAS_PREPARE:
220 case SND_SOC_BIAS_STANDBY:
221 if (codec->bias_level == SND_SOC_BIAS_OFF) {
222 /* Power everything up... */
223 reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
224 wm8728_write(codec, WM8728_DACCTL, reg & ~0x4);
225
226 /* ..then sync in the register cache. */
227 for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
228 wm8728_write(codec, i,
229 wm8728_read_reg_cache(codec, i));
230 }
231 break;
232
233 case SND_SOC_BIAS_OFF:
234 reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
235 wm8728_write(codec, WM8728_DACCTL, reg | 0x4);
236 break;
237 }
238 codec->bias_level = level;
239 return 0;
240}
241
242#define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
243
244#define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
245 SNDRV_PCM_FMTBIT_S24_LE)
246
247struct snd_soc_dai wm8728_dai = {
248 .name = "WM8728",
249 .playback = {
250 .stream_name = "Playback",
251 .channels_min = 2,
252 .channels_max = 2,
253 .rates = WM8728_RATES,
254 .formats = WM8728_FORMATS,
255 },
256 .ops = {
257 .hw_params = wm8728_hw_params,
71cfc902
MB
258 .digital_mute = wm8728_mute,
259 .set_fmt = wm8728_set_dai_fmt,
260 }
261};
262EXPORT_SYMBOL_GPL(wm8728_dai);
263
264static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
265{
266 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 267 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
268
269 wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
270
271 return 0;
272}
273
274static int wm8728_resume(struct platform_device *pdev)
275{
276 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 277 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
278
279 wm8728_set_bias_level(codec, codec->suspend_bias_level);
280
281 return 0;
282}
283
284/*
285 * initialise the WM8728 driver
286 * register the mixer and dsp interfaces with the kernel
287 */
288static int wm8728_init(struct snd_soc_device *socdev)
289{
6627a653 290 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
291 int ret = 0;
292
293 codec->name = "WM8728";
294 codec->owner = THIS_MODULE;
295 codec->read = wm8728_read_reg_cache;
296 codec->write = wm8728_write;
297 codec->set_bias_level = wm8728_set_bias_level;
298 codec->dai = &wm8728_dai;
299 codec->num_dai = 1;
300 codec->bias_level = SND_SOC_BIAS_OFF;
301 codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
302 codec->reg_cache = kmemdup(wm8728_reg_defaults,
303 sizeof(wm8728_reg_defaults),
304 GFP_KERNEL);
305 if (codec->reg_cache == NULL)
306 return -ENOMEM;
307
308 /* register pcms */
309 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
310 if (ret < 0) {
311 printk(KERN_ERR "wm8728: failed to create pcms\n");
312 goto pcm_err;
313 }
314
315 /* power on device */
316 wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
317
3e8e1952
IM
318 snd_soc_add_controls(codec, wm8728_snd_controls,
319 ARRAY_SIZE(wm8728_snd_controls));
71cfc902 320 wm8728_add_widgets(codec);
968a6025 321 ret = snd_soc_init_card(socdev);
71cfc902
MB
322 if (ret < 0) {
323 printk(KERN_ERR "wm8728: failed to register card\n");
324 goto card_err;
325 }
326
327 return ret;
328
329card_err:
330 snd_soc_free_pcms(socdev);
331 snd_soc_dapm_free(socdev);
332pcm_err:
333 kfree(codec->reg_cache);
334 return ret;
335}
336
337static struct snd_soc_device *wm8728_socdev;
338
339#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
340
341/*
342 * WM8728 2 wire address is determined by GPIO5
343 * state during powerup.
344 * low = 0x1a
345 * high = 0x1b
346 */
347
348static int wm8728_i2c_probe(struct i2c_client *i2c,
349 const struct i2c_device_id *id)
350{
351 struct snd_soc_device *socdev = wm8728_socdev;
6627a653 352 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
353 int ret;
354
355 i2c_set_clientdata(i2c, codec);
356 codec->control_data = i2c;
357
358 ret = wm8728_init(socdev);
359 if (ret < 0)
360 pr_err("failed to initialise WM8728\n");
361
362 return ret;
363}
364
365static int wm8728_i2c_remove(struct i2c_client *client)
366{
367 struct snd_soc_codec *codec = i2c_get_clientdata(client);
368 kfree(codec->reg_cache);
369 return 0;
370}
371
372static const struct i2c_device_id wm8728_i2c_id[] = {
373 { "wm8728", 0 },
374 { }
375};
376MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
377
378static struct i2c_driver wm8728_i2c_driver = {
379 .driver = {
380 .name = "WM8728 I2C Codec",
381 .owner = THIS_MODULE,
382 },
383 .probe = wm8728_i2c_probe,
384 .remove = wm8728_i2c_remove,
385 .id_table = wm8728_i2c_id,
386};
387
388static int wm8728_add_i2c_device(struct platform_device *pdev,
389 const struct wm8728_setup_data *setup)
390{
391 struct i2c_board_info info;
392 struct i2c_adapter *adapter;
393 struct i2c_client *client;
394 int ret;
395
396 ret = i2c_add_driver(&wm8728_i2c_driver);
397 if (ret != 0) {
398 dev_err(&pdev->dev, "can't add i2c driver\n");
399 return ret;
400 }
401
402 memset(&info, 0, sizeof(struct i2c_board_info));
403 info.addr = setup->i2c_address;
404 strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
405
406 adapter = i2c_get_adapter(setup->i2c_bus);
407 if (!adapter) {
408 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
409 setup->i2c_bus);
410 goto err_driver;
411 }
412
413 client = i2c_new_device(adapter, &info);
414 i2c_put_adapter(adapter);
415 if (!client) {
416 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
417 (unsigned int)info.addr);
418 goto err_driver;
419 }
420
421 return 0;
422
423err_driver:
424 i2c_del_driver(&wm8728_i2c_driver);
425 return -ENODEV;
426}
427#endif
428
429#if defined(CONFIG_SPI_MASTER)
430static int __devinit wm8728_spi_probe(struct spi_device *spi)
431{
432 struct snd_soc_device *socdev = wm8728_socdev;
6627a653 433 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
434 int ret;
435
436 codec->control_data = spi;
437
438 ret = wm8728_init(socdev);
439 if (ret < 0)
440 dev_err(&spi->dev, "failed to initialise WM8728\n");
441
442 return ret;
443}
444
445static int __devexit wm8728_spi_remove(struct spi_device *spi)
446{
447 return 0;
448}
449
450static struct spi_driver wm8728_spi_driver = {
451 .driver = {
452 .name = "wm8728",
453 .bus = &spi_bus_type,
454 .owner = THIS_MODULE,
455 },
456 .probe = wm8728_spi_probe,
457 .remove = __devexit_p(wm8728_spi_remove),
458};
459
460static int wm8728_spi_write(struct spi_device *spi, const char *data, int len)
461{
462 struct spi_transfer t;
463 struct spi_message m;
464 u8 msg[2];
465
466 if (len <= 0)
467 return 0;
468
469 msg[0] = data[0];
470 msg[1] = data[1];
471
472 spi_message_init(&m);
473 memset(&t, 0, (sizeof t));
474
475 t.tx_buf = &msg[0];
476 t.len = len;
477
478 spi_message_add_tail(&t, &m);
479 spi_sync(spi, &m);
480
481 return len;
482}
483#endif /* CONFIG_SPI_MASTER */
484
485static int wm8728_probe(struct platform_device *pdev)
486{
487 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
488 struct wm8728_setup_data *setup;
489 struct snd_soc_codec *codec;
490 int ret = 0;
491
492 setup = socdev->codec_data;
493 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
494 if (codec == NULL)
495 return -ENOMEM;
496
6627a653 497 socdev->card->codec = codec;
71cfc902
MB
498 mutex_init(&codec->mutex);
499 INIT_LIST_HEAD(&codec->dapm_widgets);
500 INIT_LIST_HEAD(&codec->dapm_paths);
501
502 wm8728_socdev = socdev;
503 ret = -ENODEV;
504
505#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
506 if (setup->i2c_address) {
507 codec->hw_write = (hw_write_t)i2c_master_send;
508 ret = wm8728_add_i2c_device(pdev, setup);
509 }
510#endif
511#if defined(CONFIG_SPI_MASTER)
512 if (setup->spi) {
513 codec->hw_write = (hw_write_t)wm8728_spi_write;
514 ret = spi_register_driver(&wm8728_spi_driver);
515 if (ret != 0)
516 printk(KERN_ERR "can't add spi driver");
517 }
518#endif
519
520 if (ret != 0)
521 kfree(codec);
522
523 return ret;
524}
525
526/* power down chip */
527static int wm8728_remove(struct platform_device *pdev)
528{
529 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 530 struct snd_soc_codec *codec = socdev->card->codec;
71cfc902
MB
531
532 if (codec->control_data)
533 wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
534
535 snd_soc_free_pcms(socdev);
536 snd_soc_dapm_free(socdev);
537#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
538 i2c_unregister_device(codec->control_data);
539 i2c_del_driver(&wm8728_i2c_driver);
540#endif
541#if defined(CONFIG_SPI_MASTER)
542 spi_unregister_driver(&wm8728_spi_driver);
543#endif
544 kfree(codec);
545
546 return 0;
547}
548
549struct snd_soc_codec_device soc_codec_dev_wm8728 = {
550 .probe = wm8728_probe,
551 .remove = wm8728_remove,
552 .suspend = wm8728_suspend,
553 .resume = wm8728_resume,
554};
555EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
556
c9b3a40f 557static int __init wm8728_modinit(void)
64089b84
MB
558{
559 return snd_soc_register_dai(&wm8728_dai);
560}
561module_init(wm8728_modinit);
562
563static void __exit wm8728_exit(void)
564{
565 snd_soc_unregister_dai(&wm8728_dai);
566}
567module_exit(wm8728_exit);
568
71cfc902
MB
569MODULE_DESCRIPTION("ASoC WM8728 driver");
570MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
571MODULE_LICENSE("GPL");
This page took 0.056667 seconds and 5 git commands to generate.