Merge tag 'armsoc-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / sound / soc / samsung / smdk_wm8580.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2009 Samsung Electronics Co. Ltd
4 // Author: Jaswinder Singh <jassisinghbrar@gmail.com>
5
6 #include <linux/module.h>
7 #include <sound/soc.h>
8 #include <sound/pcm_params.h>
9
10 #include "../codecs/wm8580.h"
11 #include "i2s.h"
12
13 /*
14  * Default CFG switch settings to use this driver:
15  *
16  *   SMDK6410: Set CFG1 1-3 Off, CFG2 1-4 On
17  */
18
19 /* SMDK has a 12MHZ crystal attached to WM8580 */
20 #define SMDK_WM8580_FREQ 12000000
21
22 static int smdk_hw_params(struct snd_pcm_substream *substream,
23         struct snd_pcm_hw_params *params)
24 {
25         struct snd_soc_pcm_runtime *rtd = substream->private_data;
26         struct snd_soc_dai *codec_dai = rtd->codec_dai;
27         unsigned int pll_out;
28         int rfs, ret;
29
30         switch (params_width(params)) {
31         case 8:
32         case 16:
33                 break;
34         default:
35                 return -EINVAL;
36         }
37
38         /* The Fvco for WM8580 PLLs must fall within [90,100]MHz.
39          * This criterion can't be met if we request PLL output
40          * as {8000x256, 64000x256, 11025x256}Hz.
41          * As a wayout, we rather change rfs to a minimum value that
42          * results in (params_rate(params) * rfs), and itself, acceptable
43          * to both - the CODEC and the CPU.
44          */
45         switch (params_rate(params)) {
46         case 16000:
47         case 22050:
48         case 32000:
49         case 44100:
50         case 48000:
51         case 88200:
52         case 96000:
53                 rfs = 256;
54                 break;
55         case 64000:
56                 rfs = 384;
57                 break;
58         case 8000:
59         case 11025:
60                 rfs = 512;
61                 break;
62         default:
63                 return -EINVAL;
64         }
65         pll_out = params_rate(params) * rfs;
66
67         /* Set WM8580 to drive MCLK from its PLLA */
68         ret = snd_soc_dai_set_clkdiv(codec_dai, WM8580_MCLK,
69                                         WM8580_CLKSRC_PLLA);
70         if (ret < 0)
71                 return ret;
72
73         ret = snd_soc_dai_set_pll(codec_dai, WM8580_PLLA, 0,
74                                         SMDK_WM8580_FREQ, pll_out);
75         if (ret < 0)
76                 return ret;
77
78         ret = snd_soc_dai_set_sysclk(codec_dai, WM8580_CLKSRC_PLLA,
79                                      pll_out, SND_SOC_CLOCK_IN);
80         if (ret < 0)
81                 return ret;
82
83         return 0;
84 }
85
86 /*
87  * SMDK WM8580 DAI operations.
88  */
89 static struct snd_soc_ops smdk_ops = {
90         .hw_params = smdk_hw_params,
91 };
92
93 /* SMDK Playback widgets */
94 static const struct snd_soc_dapm_widget smdk_wm8580_dapm_widgets[] = {
95         SND_SOC_DAPM_HP("Front", NULL),
96         SND_SOC_DAPM_HP("Center+Sub", NULL),
97         SND_SOC_DAPM_HP("Rear", NULL),
98
99         SND_SOC_DAPM_MIC("MicIn", NULL),
100         SND_SOC_DAPM_LINE("LineIn", NULL),
101 };
102
103 /* SMDK-PAIFTX connections */
104 static const struct snd_soc_dapm_route smdk_wm8580_audio_map[] = {
105         /* MicIn feeds AINL */
106         {"AINL", NULL, "MicIn"},
107
108         /* LineIn feeds AINL/R */
109         {"AINL", NULL, "LineIn"},
110         {"AINR", NULL, "LineIn"},
111
112         /* Front Left/Right are fed VOUT1L/R */
113         {"Front", NULL, "VOUT1L"},
114         {"Front", NULL, "VOUT1R"},
115
116         /* Center/Sub are fed VOUT2L/R */
117         {"Center+Sub", NULL, "VOUT2L"},
118         {"Center+Sub", NULL, "VOUT2R"},
119
120         /* Rear Left/Right are fed VOUT3L/R */
121         {"Rear", NULL, "VOUT3L"},
122         {"Rear", NULL, "VOUT3R"},
123 };
124
125 static int smdk_wm8580_init_paiftx(struct snd_soc_pcm_runtime *rtd)
126 {
127         /* Enabling the microphone requires the fitting of a 0R
128          * resistor to connect the line from the microphone jack.
129          */
130         snd_soc_dapm_disable_pin(&rtd->card->dapm, "MicIn");
131
132         return 0;
133 }
134
135 enum {
136         PRI_PLAYBACK = 0,
137         PRI_CAPTURE,
138 };
139
140 #define SMDK_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
141         SND_SOC_DAIFMT_CBM_CFM)
142
143 static struct snd_soc_dai_link smdk_dai[] = {
144         [PRI_PLAYBACK] = { /* Primary Playback i/f */
145                 .name = "WM8580 PAIF RX",
146                 .stream_name = "Playback",
147                 .cpu_dai_name = "samsung-i2s.2",
148                 .codec_dai_name = "wm8580-hifi-playback",
149                 .platform_name = "samsung-i2s.0",
150                 .codec_name = "wm8580.0-001b",
151                 .dai_fmt = SMDK_DAI_FMT,
152                 .ops = &smdk_ops,
153         },
154         [PRI_CAPTURE] = { /* Primary Capture i/f */
155                 .name = "WM8580 PAIF TX",
156                 .stream_name = "Capture",
157                 .cpu_dai_name = "samsung-i2s.2",
158                 .codec_dai_name = "wm8580-hifi-capture",
159                 .platform_name = "samsung-i2s.0",
160                 .codec_name = "wm8580.0-001b",
161                 .dai_fmt = SMDK_DAI_FMT,
162                 .init = smdk_wm8580_init_paiftx,
163                 .ops = &smdk_ops,
164         },
165 };
166
167 static struct snd_soc_card smdk = {
168         .name = "SMDK-I2S",
169         .owner = THIS_MODULE,
170         .dai_link = smdk_dai,
171         .num_links = ARRAY_SIZE(smdk_dai),
172
173         .dapm_widgets = smdk_wm8580_dapm_widgets,
174         .num_dapm_widgets = ARRAY_SIZE(smdk_wm8580_dapm_widgets),
175         .dapm_routes = smdk_wm8580_audio_map,
176         .num_dapm_routes = ARRAY_SIZE(smdk_wm8580_audio_map),
177 };
178
179 static struct platform_device *smdk_snd_device;
180
181 static int __init smdk_audio_init(void)
182 {
183         int ret;
184
185         smdk_snd_device = platform_device_alloc("soc-audio", -1);
186         if (!smdk_snd_device)
187                 return -ENOMEM;
188
189         platform_set_drvdata(smdk_snd_device, &smdk);
190         ret = platform_device_add(smdk_snd_device);
191
192         if (ret)
193                 platform_device_put(smdk_snd_device);
194
195         return ret;
196 }
197 module_init(smdk_audio_init);
198
199 static void __exit smdk_audio_exit(void)
200 {
201         platform_device_unregister(smdk_snd_device);
202 }
203 module_exit(smdk_audio_exit);
204
205 MODULE_AUTHOR("Jaswinder Singh, jassisinghbrar@gmail.com");
206 MODULE_DESCRIPTION("ALSA SoC SMDK WM8580");
207 MODULE_LICENSE("GPL");