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