[ALSA] ASoC codecs: generic AC97 support
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / ac97.c
1 /*
2  * ac97.c  --  ALSA Soc AC97 codec support
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood
6  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  *  Revision history
14  *    17th Oct 2005   Initial version.
15  *
16  * Generic AC97 support.
17  */
18
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <sound/driver.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28
29 #define AC97_VERSION "0.5"
30
31 #define AC97_DIR \
32         (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
33
34 #define AC97_RATES \
35         (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
36         SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
37         SNDRV_PCM_RATE_48000)
38
39 /* may need to expand this */
40 static struct snd_soc_dai_mode soc_ac97[] = {
41         {0, 0,  SNDRV_PCM_FMTBIT_S16_LE,        AC97_RATES},
42         {0, 0,  SNDRV_PCM_FMTBIT_S18_3LE,       AC97_RATES},
43         {0, 0,  SNDRV_PCM_FMTBIT_S20_3LE,       AC97_RATES},
44 };
45
46 static int ac97_prepare(struct snd_pcm_substream *substream)
47 {
48         struct snd_pcm_runtime *runtime = substream->runtime;
49         struct snd_soc_pcm_runtime *rtd = substream->private_data;
50         struct snd_soc_device *socdev = rtd->socdev;
51         struct snd_soc_codec *codec = socdev->codec;
52
53         int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
54                   AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
55         return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
56 }
57
58 static struct snd_soc_codec_dai ac97_dai = {
59         .name = "AC97 HiFi",
60         .playback = {
61                 .stream_name = "AC97 Playback",
62                 .channels_min = 1,
63                 .channels_max = 2,},
64         .capture = {
65                 .stream_name = "AC97 Capture",
66                 .channels_min = 1,
67                 .channels_max = 2,},
68         .ops = {
69                 .prepare = ac97_prepare,},
70         .caps = {
71                 .num_modes = ARRAY_SIZE(soc_ac97),
72                 .mode = soc_ac97,},
73 };
74
75 static unsigned int ac97_read(struct snd_soc_codec *codec,
76         unsigned int reg)
77 {
78         return soc_ac97_ops.read(codec->ac97, reg);
79 }
80
81 static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
82         unsigned int val)
83 {
84         soc_ac97_ops.write(codec->ac97, reg, val);
85         return 0;
86 }
87
88 static int ac97_soc_probe(struct platform_device *pdev)
89 {
90         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
91         struct snd_soc_codec *codec;
92         struct snd_ac97_bus *ac97_bus;
93         struct snd_ac97_template ac97_template;
94         int ret = 0;
95
96         printk(KERN_INFO "AC97 SoC Audio Codec %s\n", AC97_VERSION);
97
98         socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
99         if (socdev->codec == NULL)
100                 return -ENOMEM;
101         codec = socdev->codec;
102         mutex_init(&codec->mutex);
103
104         codec->name = "AC97";
105         codec->owner = THIS_MODULE;
106         codec->dai = &ac97_dai;
107         codec->num_dai = 1;
108         codec->write = ac97_write;
109         codec->read = ac97_read;
110         INIT_LIST_HEAD(&codec->dapm_widgets);
111         INIT_LIST_HEAD(&codec->dapm_paths);
112
113         /* register pcms */
114         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
115         if(ret < 0)
116                 goto err;
117
118         /* add codec as bus device for standard ac97 */
119         ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
120         if(ret < 0)
121                 goto bus_err;
122
123         memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
124         ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
125         if(ret < 0)
126                 goto bus_err;
127
128         ret = snd_soc_register_card(socdev);
129         if (ret < 0)
130                 goto bus_err;
131         return 0;
132
133 bus_err:
134         snd_soc_free_pcms(socdev);
135
136 err:
137         kfree(socdev->codec->reg_cache);
138         kfree(socdev->codec);
139         socdev->codec = NULL;
140         return ret;
141 }
142
143 static int ac97_soc_remove(struct platform_device *pdev)
144 {
145         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
146         struct snd_soc_codec *codec = socdev->codec;
147
148         if(codec == NULL)
149                 return 0;
150
151         snd_soc_free_pcms(socdev);
152         kfree(socdev->codec->reg_cache);
153         kfree(socdev->codec);
154
155         return 0;
156 }
157
158 struct snd_soc_codec_device soc_codec_dev_ac97= {
159         .probe =        ac97_soc_probe,
160         .remove =       ac97_soc_remove,
161 };
162
163 EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
164
165 MODULE_DESCRIPTION("Soc Generic AC97 driver");
166 MODULE_AUTHOR("Liam Girdwood");
167 MODULE_LICENSE("GPL");