]> git.karo-electronics.de Git - linux-beck.git/blob - sound/soc/codecs/wm8711.c
ASoC: Factor out WM8711 cache I/O
[linux-beck.git] / sound / soc / codecs / wm8711.c
1 /*
2  * wm8711.c  --  WM8711 ALSA SoC Audio driver
3  *
4  * Copyright 2006 Wolfson Microelectronics
5  *
6  * Author: Mike Arthur <linux@wolfsonmicro.com>
7  *
8  * Based on wm8731.c by Richard Purdie
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
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 <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28
29 #include "wm8711.h"
30
31 static struct snd_soc_codec *wm8711_codec;
32
33 /* codec private data */
34 struct wm8711_priv {
35         struct snd_soc_codec codec;
36         u16 reg_cache[WM8711_CACHEREGNUM];
37         unsigned int sysclk;
38 };
39
40 /*
41  * wm8711 register cache
42  * We can't read the WM8711 register space when we are
43  * using 2 wire for device control, so we cache them instead.
44  * There is no point in caching the reset register
45  */
46 static const u16 wm8711_reg[WM8711_CACHEREGNUM] = {
47         0x0079, 0x0079, 0x000a, 0x0008,
48         0x009f, 0x000a, 0x0000, 0x0000
49 };
50
51 #define wm8711_reset(c) snd_soc_write(c, WM8711_RESET, 0)
52
53 static const struct snd_kcontrol_new wm8711_snd_controls[] = {
54
55 SOC_DOUBLE_R("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
56         0, 127, 0),
57 SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
58         7, 1, 0),
59
60 };
61
62 /* add non dapm controls */
63 static int wm8711_add_controls(struct snd_soc_codec *codec)
64 {
65         int err, i;
66
67         for (i = 0; i < ARRAY_SIZE(wm8711_snd_controls); i++) {
68                 err = snd_ctl_add(codec->card,
69                                 snd_soc_cnew(&wm8711_snd_controls[i], codec,
70                                         NULL));
71                 if (err < 0)
72                         return err;
73         }
74
75         return 0;
76 }
77
78 /* Output Mixer */
79 static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
80 SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
81 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
82 };
83
84 static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
85 SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
86         &wm8711_output_mixer_controls[0],
87         ARRAY_SIZE(wm8711_output_mixer_controls)),
88 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
89 SND_SOC_DAPM_OUTPUT("LOUT"),
90 SND_SOC_DAPM_OUTPUT("LHPOUT"),
91 SND_SOC_DAPM_OUTPUT("ROUT"),
92 SND_SOC_DAPM_OUTPUT("RHPOUT"),
93 };
94
95 static const struct snd_soc_dapm_route intercon[] = {
96         /* output mixer */
97         {"Output Mixer", "Line Bypass Switch", "Line Input"},
98         {"Output Mixer", "HiFi Playback Switch", "DAC"},
99
100         /* outputs */
101         {"RHPOUT", NULL, "Output Mixer"},
102         {"ROUT", NULL, "Output Mixer"},
103         {"LHPOUT", NULL, "Output Mixer"},
104         {"LOUT", NULL, "Output Mixer"},
105 };
106
107 static int wm8711_add_widgets(struct snd_soc_codec *codec)
108 {
109         snd_soc_dapm_new_controls(codec, wm8711_dapm_widgets,
110                                   ARRAY_SIZE(wm8711_dapm_widgets));
111
112         snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
113
114         snd_soc_dapm_new_widgets(codec);
115         return 0;
116 }
117
118 struct _coeff_div {
119         u32 mclk;
120         u32 rate;
121         u16 fs;
122         u8 sr:4;
123         u8 bosr:1;
124         u8 usb:1;
125 };
126
127 /* codec mclk clock divider coefficients */
128 static const struct _coeff_div coeff_div[] = {
129         /* 48k */
130         {12288000, 48000, 256, 0x0, 0x0, 0x0},
131         {18432000, 48000, 384, 0x0, 0x1, 0x0},
132         {12000000, 48000, 250, 0x0, 0x0, 0x1},
133
134         /* 32k */
135         {12288000, 32000, 384, 0x6, 0x0, 0x0},
136         {18432000, 32000, 576, 0x6, 0x1, 0x0},
137         {12000000, 32000, 375, 0x6, 0x0, 0x1},
138
139         /* 8k */
140         {12288000, 8000, 1536, 0x3, 0x0, 0x0},
141         {18432000, 8000, 2304, 0x3, 0x1, 0x0},
142         {11289600, 8000, 1408, 0xb, 0x0, 0x0},
143         {16934400, 8000, 2112, 0xb, 0x1, 0x0},
144         {12000000, 8000, 1500, 0x3, 0x0, 0x1},
145
146         /* 96k */
147         {12288000, 96000, 128, 0x7, 0x0, 0x0},
148         {18432000, 96000, 192, 0x7, 0x1, 0x0},
149         {12000000, 96000, 125, 0x7, 0x0, 0x1},
150
151         /* 44.1k */
152         {11289600, 44100, 256, 0x8, 0x0, 0x0},
153         {16934400, 44100, 384, 0x8, 0x1, 0x0},
154         {12000000, 44100, 272, 0x8, 0x1, 0x1},
155
156         /* 88.2k */
157         {11289600, 88200, 128, 0xf, 0x0, 0x0},
158         {16934400, 88200, 192, 0xf, 0x1, 0x0},
159         {12000000, 88200, 136, 0xf, 0x1, 0x1},
160 };
161
162 static inline int get_coeff(int mclk, int rate)
163 {
164         int i;
165
166         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
167                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
168                         return i;
169         }
170         return 0;
171 }
172
173 static int wm8711_hw_params(struct snd_pcm_substream *substream,
174         struct snd_pcm_hw_params *params,
175         struct snd_soc_dai *dai)
176 {
177         struct snd_soc_codec *codec = dai->codec;
178         struct wm8711_priv *wm8711 = codec->private_data;
179         u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0xfffc;
180         int i = get_coeff(wm8711->sysclk, params_rate(params));
181         u16 srate = (coeff_div[i].sr << 2) |
182                 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
183
184         snd_soc_write(codec, WM8711_SRATE, srate);
185
186         /* bit size */
187         switch (params_format(params)) {
188         case SNDRV_PCM_FORMAT_S16_LE:
189                 break;
190         case SNDRV_PCM_FORMAT_S20_3LE:
191                 iface |= 0x0004;
192                 break;
193         case SNDRV_PCM_FORMAT_S24_LE:
194                 iface |= 0x0008;
195                 break;
196         }
197
198         snd_soc_write(codec, WM8711_IFACE, iface);
199         return 0;
200 }
201
202 static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
203                               struct snd_soc_dai *dai)
204 {
205         struct snd_soc_codec *codec = dai->codec;
206
207         /* set active */
208         snd_soc_write(codec, WM8711_ACTIVE, 0x0001);
209
210         return 0;
211 }
212
213 static void wm8711_shutdown(struct snd_pcm_substream *substream,
214                             struct snd_soc_dai *dai)
215 {
216         struct snd_soc_codec *codec = dai->codec;
217
218         /* deactivate */
219         if (!codec->active) {
220                 udelay(50);
221                 snd_soc_write(codec, WM8711_ACTIVE, 0x0);
222         }
223 }
224
225 static int wm8711_mute(struct snd_soc_dai *dai, int mute)
226 {
227         struct snd_soc_codec *codec = dai->codec;
228         u16 mute_reg = snd_soc_read(codec, WM8711_APDIGI) & 0xfff7;
229
230         if (mute)
231                 snd_soc_write(codec, WM8711_APDIGI, mute_reg | 0x8);
232         else
233                 snd_soc_write(codec, WM8711_APDIGI, mute_reg);
234
235         return 0;
236 }
237
238 static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
239                 int clk_id, unsigned int freq, int dir)
240 {
241         struct snd_soc_codec *codec = codec_dai->codec;
242         struct wm8711_priv *wm8711 = codec->private_data;
243
244         switch (freq) {
245         case 11289600:
246         case 12000000:
247         case 12288000:
248         case 16934400:
249         case 18432000:
250                 wm8711->sysclk = freq;
251                 return 0;
252         }
253         return -EINVAL;
254 }
255
256 static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
257                 unsigned int fmt)
258 {
259         struct snd_soc_codec *codec = codec_dai->codec;
260         u16 iface = 0;
261
262         /* set master/slave audio interface */
263         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
264         case SND_SOC_DAIFMT_CBM_CFM:
265                 iface |= 0x0040;
266                 break;
267         case SND_SOC_DAIFMT_CBS_CFS:
268                 break;
269         default:
270                 return -EINVAL;
271         }
272
273         /* interface format */
274         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
275         case SND_SOC_DAIFMT_I2S:
276                 iface |= 0x0002;
277                 break;
278         case SND_SOC_DAIFMT_RIGHT_J:
279                 break;
280         case SND_SOC_DAIFMT_LEFT_J:
281                 iface |= 0x0001;
282                 break;
283         case SND_SOC_DAIFMT_DSP_A:
284                 iface |= 0x0003;
285                 break;
286         case SND_SOC_DAIFMT_DSP_B:
287                 iface |= 0x0013;
288                 break;
289         default:
290                 return -EINVAL;
291         }
292
293         /* clock inversion */
294         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
295         case SND_SOC_DAIFMT_NB_NF:
296                 break;
297         case SND_SOC_DAIFMT_IB_IF:
298                 iface |= 0x0090;
299                 break;
300         case SND_SOC_DAIFMT_IB_NF:
301                 iface |= 0x0080;
302                 break;
303         case SND_SOC_DAIFMT_NB_IF:
304                 iface |= 0x0010;
305                 break;
306         default:
307                 return -EINVAL;
308         }
309
310         /* set iface */
311         snd_soc_write(codec, WM8711_IFACE, iface);
312         return 0;
313 }
314
315
316 static int wm8711_set_bias_level(struct snd_soc_codec *codec,
317         enum snd_soc_bias_level level)
318 {
319         u16 reg = snd_soc_read(codec, WM8711_PWR) & 0xff7f;
320
321         switch (level) {
322         case SND_SOC_BIAS_ON:
323                 snd_soc_write(codec, WM8711_PWR, reg);
324                 break;
325         case SND_SOC_BIAS_PREPARE:
326                 break;
327         case SND_SOC_BIAS_STANDBY:
328                 snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
329                 break;
330         case SND_SOC_BIAS_OFF:
331                 snd_soc_write(codec, WM8711_ACTIVE, 0x0);
332                 snd_soc_write(codec, WM8711_PWR, 0xffff);
333                 break;
334         }
335         codec->bias_level = level;
336         return 0;
337 }
338
339 #define WM8711_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
340                 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
341                 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
342                 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
343                 SNDRV_PCM_RATE_96000)
344
345 #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
346         SNDRV_PCM_FMTBIT_S24_LE)
347
348 static struct snd_soc_dai_ops wm8711_ops = {
349         .prepare = wm8711_pcm_prepare,
350         .hw_params = wm8711_hw_params,
351         .shutdown = wm8711_shutdown,
352         .digital_mute = wm8711_mute,
353         .set_sysclk = wm8711_set_dai_sysclk,
354         .set_fmt = wm8711_set_dai_fmt,
355 };
356
357 struct snd_soc_dai wm8711_dai = {
358         .name = "WM8711",
359         .playback = {
360                 .stream_name = "Playback",
361                 .channels_min = 1,
362                 .channels_max = 2,
363                 .rates = WM8711_RATES,
364                 .formats = WM8711_FORMATS,},
365         .ops = &wm8711_ops,
366 };
367 EXPORT_SYMBOL_GPL(wm8711_dai);
368
369 static int wm8711_suspend(struct platform_device *pdev, pm_message_t state)
370 {
371         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
372         struct snd_soc_codec *codec = socdev->card->codec;
373
374         snd_soc_write(codec, WM8711_ACTIVE, 0x0);
375         wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
376         return 0;
377 }
378
379 static int wm8711_resume(struct platform_device *pdev)
380 {
381         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
382         struct snd_soc_codec *codec = socdev->card->codec;
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(wm8711_reg); i++) {
389                 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
390                 data[1] = cache[i] & 0x00ff;
391                 codec->hw_write(codec->control_data, data, 2);
392         }
393         wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
394         wm8711_set_bias_level(codec, codec->suspend_bias_level);
395         return 0;
396 }
397
398 static int wm8711_probe(struct platform_device *pdev)
399 {
400         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
401         struct snd_soc_codec *codec;
402         int ret = 0;
403
404         if (wm8711_codec == NULL) {
405                 dev_err(&pdev->dev, "Codec device not registered\n");
406                 return -ENODEV;
407         }
408
409         socdev->card->codec = wm8711_codec;
410         codec = wm8711_codec;
411
412         /* register pcms */
413         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
414         if (ret < 0) {
415                 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
416                 goto pcm_err;
417         }
418
419         snd_soc_add_controls(codec, wm8711_snd_controls,
420                              ARRAY_SIZE(wm8711_snd_controls));
421         wm8711_add_widgets(codec);
422         ret = snd_soc_init_card(socdev);
423         if (ret < 0) {
424                 dev_err(codec->dev, "failed to register card: %d\n", ret);
425                 goto card_err;
426         }
427
428         return ret;
429
430 card_err:
431         snd_soc_free_pcms(socdev);
432         snd_soc_dapm_free(socdev);
433 pcm_err:
434         return ret;
435 }
436
437 /* power down chip */
438 static int wm8711_remove(struct platform_device *pdev)
439 {
440         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
441
442         snd_soc_free_pcms(socdev);
443         snd_soc_dapm_free(socdev);
444
445         return 0;
446 }
447
448 struct snd_soc_codec_device soc_codec_dev_wm8711 = {
449         .probe =        wm8711_probe,
450         .remove =       wm8711_remove,
451         .suspend =      wm8711_suspend,
452         .resume =       wm8711_resume,
453 };
454 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8711);
455
456 static int wm8711_register(struct wm8711_priv *wm8711,
457                            enum snd_soc_control_type control)
458 {
459         int ret;
460         struct snd_soc_codec *codec = &wm8711->codec;
461         u16 reg;
462
463         if (wm8711_codec) {
464                 dev_err(codec->dev, "Another WM8711 is registered\n");
465                 return -EINVAL;
466         }
467
468         mutex_init(&codec->mutex);
469         INIT_LIST_HEAD(&codec->dapm_widgets);
470         INIT_LIST_HEAD(&codec->dapm_paths);
471
472         codec->private_data = wm8711;
473         codec->name = "WM8711";
474         codec->owner = THIS_MODULE;
475         codec->bias_level = SND_SOC_BIAS_OFF;
476         codec->set_bias_level = wm8711_set_bias_level;
477         codec->dai = &wm8711_dai;
478         codec->num_dai = 1;
479         codec->reg_cache_size = WM8711_CACHEREGNUM;
480         codec->reg_cache = &wm8711->reg_cache;
481
482         memcpy(codec->reg_cache, wm8711_reg, sizeof(wm8711_reg));
483
484         ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
485         if (ret < 0) {
486                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
487                 goto err;
488         }
489
490         ret = wm8711_reset(codec);
491         if (ret < 0) {
492                 dev_err(codec->dev, "Failed to issue reset\n");
493                 goto err;
494         }
495
496         wm8711_dai.dev = codec->dev;
497
498         wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
499
500         /* Latch the update bits */
501         reg = snd_soc_read(codec, WM8711_LOUT1V);
502         snd_soc_write(codec, WM8711_LOUT1V, reg | 0x0100);
503         reg = snd_soc_read(codec, WM8711_ROUT1V);
504         snd_soc_write(codec, WM8711_ROUT1V, reg | 0x0100);
505
506         wm8711_codec = codec;
507
508         ret = snd_soc_register_codec(codec);
509         if (ret != 0) {
510                 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
511                 goto err;
512         }
513
514         ret = snd_soc_register_dai(&wm8711_dai);
515         if (ret != 0) {
516                 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
517                 goto err_codec;
518         }
519
520         return 0;
521
522 err_codec:
523         snd_soc_unregister_codec(codec);
524 err:
525         kfree(wm8711);
526         return ret;
527 }
528
529 static void wm8711_unregister(struct wm8711_priv *wm8711)
530 {
531         wm8711_set_bias_level(&wm8711->codec, SND_SOC_BIAS_OFF);
532         snd_soc_unregister_dai(&wm8711_dai);
533         snd_soc_unregister_codec(&wm8711->codec);
534         kfree(wm8711);
535         wm8711_codec = NULL;
536 }
537
538 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
539 static __devinit int wm8711_i2c_probe(struct i2c_client *i2c,
540                                       const struct i2c_device_id *id)
541 {
542         struct wm8711_priv *wm8711;
543         struct snd_soc_codec *codec;
544
545         wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
546         if (wm8711 == NULL)
547                 return -ENOMEM;
548
549         codec = &wm8711->codec;
550         codec->hw_write = (hw_write_t)i2c_master_send;
551
552         i2c_set_clientdata(i2c, wm8711);
553         codec->control_data = i2c;
554
555         codec->dev = &i2c->dev;
556
557         return wm8711_register(wm8711, SND_SOC_I2C);
558 }
559
560 static __devexit int wm8711_i2c_remove(struct i2c_client *client)
561 {
562         struct wm8711_priv *wm8711 = i2c_get_clientdata(client);
563         wm8711_unregister(wm8711);
564         return 0;
565 }
566
567 static const struct i2c_device_id wm8711_i2c_id[] = {
568         { "wm8711", 0 },
569         { }
570 };
571 MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
572
573 static struct i2c_driver wm8711_i2c_driver = {
574         .driver = {
575                 .name = "WM8711 I2C Codec",
576                 .owner = THIS_MODULE,
577         },
578         .probe =    wm8711_i2c_probe,
579         .remove =   __devexit_p(wm8711_i2c_remove),
580         .id_table = wm8711_i2c_id,
581 };
582 #endif
583
584 static int __init wm8711_modinit(void)
585 {
586         int ret;
587 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
588         ret = i2c_add_driver(&wm8711_i2c_driver);
589         if (ret != 0) {
590                 printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
591                        ret);
592         }
593 #endif
594         return 0;
595 }
596 module_init(wm8711_modinit);
597
598 static void __exit wm8711_exit(void)
599 {
600 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
601         i2c_del_driver(&wm8711_i2c_driver);
602 #endif
603 }
604 module_exit(wm8711_exit);
605
606 MODULE_DESCRIPTION("ASoC WM8711 driver");
607 MODULE_AUTHOR("Mike Arthur");
608 MODULE_LICENSE("GPL");