ASoC: Intel: Remove soc pm handling to allow platform driver handle it
[linux-2.6-block.git] / sound / soc / intel / cht_bsw_rt5672.c
CommitLineData
026da220
ML
1/*
2 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
3 * Cherrytrail and Braswell, with RT5672 codec.
4 *
5 * Copyright (C) 2014 Intel Corp
6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * Mengdong Lin <mengdong.lin@intel.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include "../codecs/rt5670.h"
26#include "sst-atom-controls.h"
27
28/* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
29#define CHT_PLAT_CLK_3_HZ 19200000
30#define CHT_CODEC_DAI "rt5670-aif1"
31
32static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
33{
34 int i;
35
36 for (i = 0; i < card->num_rtd; i++) {
37 struct snd_soc_pcm_runtime *rtd;
38
39 rtd = card->rtd + i;
40 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
41 strlen(CHT_CODEC_DAI)))
42 return rtd->codec_dai;
43 }
44 return NULL;
45}
46
47static int platform_clock_control(struct snd_soc_dapm_widget *w,
48 struct snd_kcontrol *k, int event)
49{
50 struct snd_soc_dapm_context *dapm = w->dapm;
51 struct snd_soc_card *card = dapm->card;
52 struct snd_soc_dai *codec_dai;
53
54 codec_dai = cht_get_codec_dai(card);
55 if (!codec_dai) {
56 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
57 return -EIO;
58 }
59
60 if (!SND_SOC_DAPM_EVENT_OFF(event))
61 return 0;
62
63 /* Set codec sysclk source to its internal clock because codec PLL will
64 * be off when idle and MCLK will also be off by ACPI when codec is
65 * runtime suspended. Codec needs clock for jack detection and button
66 * press.
67 */
68 snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
69 0, SND_SOC_CLOCK_IN);
70
71 return 0;
72}
73
74static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
75 SND_SOC_DAPM_HP("Headphone", NULL),
76 SND_SOC_DAPM_MIC("Headset Mic", NULL),
77 SND_SOC_DAPM_MIC("Int Mic", NULL),
78 SND_SOC_DAPM_SPK("Ext Spk", NULL),
79 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
80 platform_clock_control, SND_SOC_DAPM_POST_PMD),
81};
82
83static const struct snd_soc_dapm_route cht_audio_map[] = {
84 {"IN1P", NULL, "Headset Mic"},
85 {"IN1N", NULL, "Headset Mic"},
86 {"DMIC L1", NULL, "Int Mic"},
87 {"DMIC R1", NULL, "Int Mic"},
88 {"Headphone", NULL, "HPOL"},
89 {"Headphone", NULL, "HPOR"},
90 {"Ext Spk", NULL, "SPOLP"},
91 {"Ext Spk", NULL, "SPOLN"},
92 {"Ext Spk", NULL, "SPORP"},
93 {"Ext Spk", NULL, "SPORN"},
94 {"AIF1 Playback", NULL, "ssp2 Tx"},
95 {"ssp2 Tx", NULL, "codec_out0"},
96 {"ssp2 Tx", NULL, "codec_out1"},
97 {"codec_in0", NULL, "ssp2 Rx"},
98 {"codec_in1", NULL, "ssp2 Rx"},
99 {"ssp2 Rx", NULL, "AIF1 Capture"},
100 {"Headphone", NULL, "Platform Clock"},
101 {"Headset Mic", NULL, "Platform Clock"},
102 {"Int Mic", NULL, "Platform Clock"},
103 {"Ext Spk", NULL, "Platform Clock"},
104};
105
106static const struct snd_kcontrol_new cht_mc_controls[] = {
107 SOC_DAPM_PIN_SWITCH("Headphone"),
108 SOC_DAPM_PIN_SWITCH("Headset Mic"),
109 SOC_DAPM_PIN_SWITCH("Int Mic"),
110 SOC_DAPM_PIN_SWITCH("Ext Spk"),
111};
112
113static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
115{
116 struct snd_soc_pcm_runtime *rtd = substream->private_data;
117 struct snd_soc_dai *codec_dai = rtd->codec_dai;
118 int ret;
119
120 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
121 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
122 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
123 if (ret < 0) {
124 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
125 return ret;
126 }
127
128 /* set codec sysclk source to PLL */
129 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
130 params_rate(params) * 512,
131 SND_SOC_CLOCK_IN);
132 if (ret < 0) {
133 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
134 return ret;
135 }
136 return 0;
137}
138
139static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
140{
141 int ret;
142 struct snd_soc_dai *codec_dai = runtime->codec_dai;
eb55fab9 143 struct snd_soc_codec *codec = codec_dai->codec;
026da220
ML
144
145 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
146 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
147 if (ret < 0) {
148 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
149 return ret;
150 }
151
eb55fab9
ML
152 /* Select codec ASRC clock source to track I2S1 clock, because codec
153 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
154 * be supported by RT5672. Otherwise, ASRC will be disabled and cause
155 * noise.
156 */
157 rt5670_sel_asrc_clk_src(codec,
158 RT5670_DA_STEREO_FILTER
159 | RT5670_DA_MONO_L_FILTER
160 | RT5670_DA_MONO_R_FILTER
161 | RT5670_AD_STEREO_FILTER
162 | RT5670_AD_MONO_L_FILTER
163 | RT5670_AD_MONO_R_FILTER,
164 RT5670_CLK_SEL_I2S1_ASRC);
026da220
ML
165 return 0;
166}
167
168static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
169 struct snd_pcm_hw_params *params)
170{
171 struct snd_interval *rate = hw_param_interval(params,
172 SNDRV_PCM_HW_PARAM_RATE);
173 struct snd_interval *channels = hw_param_interval(params,
174 SNDRV_PCM_HW_PARAM_CHANNELS);
175
176 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
177 rate->min = rate->max = 48000;
178 channels->min = channels->max = 2;
179
180 /* set SSP2 to 24-bit */
369a9f5f 181 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
026da220
ML
182 return 0;
183}
184
185static unsigned int rates_48000[] = {
186 48000,
187};
188
189static struct snd_pcm_hw_constraint_list constraints_48000 = {
190 .count = ARRAY_SIZE(rates_48000),
191 .list = rates_48000,
192};
193
194static int cht_aif1_startup(struct snd_pcm_substream *substream)
195{
196 return snd_pcm_hw_constraint_list(substream->runtime, 0,
197 SNDRV_PCM_HW_PARAM_RATE,
198 &constraints_48000);
199}
200
201static struct snd_soc_ops cht_aif1_ops = {
202 .startup = cht_aif1_startup,
203};
204
205static struct snd_soc_ops cht_be_ssp2_ops = {
206 .hw_params = cht_aif1_hw_params,
207};
208
209static struct snd_soc_dai_link cht_dailink[] = {
210 /* Front End DAI links */
211 [MERR_DPCM_AUDIO] = {
212 .name = "Audio Port",
213 .stream_name = "Audio",
214 .cpu_dai_name = "media-cpu-dai",
215 .codec_dai_name = "snd-soc-dummy-dai",
216 .codec_name = "snd-soc-dummy",
217 .platform_name = "sst-mfld-platform",
76ca1c2c 218 .nonatomic = true,
026da220
ML
219 .dynamic = 1,
220 .dpcm_playback = 1,
221 .dpcm_capture = 1,
222 .ops = &cht_aif1_ops,
223 },
224 [MERR_DPCM_COMPR] = {
225 .name = "Compressed Port",
226 .stream_name = "Compress",
227 .cpu_dai_name = "compress-cpu-dai",
228 .codec_dai_name = "snd-soc-dummy-dai",
229 .codec_name = "snd-soc-dummy",
230 .platform_name = "sst-mfld-platform",
231 },
232
233 /* Back End DAI links */
234 {
235 /* SSP2 - Codec */
236 .name = "SSP2-Codec",
237 .be_id = 1,
238 .cpu_dai_name = "ssp2-port",
239 .platform_name = "sst-mfld-platform",
240 .no_pcm = 1,
76ca1c2c 241 .nonatomic = true,
026da220
ML
242 .codec_dai_name = "rt5670-aif1",
243 .codec_name = "i2c-10EC5670:00",
244 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
245 | SND_SOC_DAIFMT_CBS_CFS,
246 .init = cht_codec_init,
247 .be_hw_params_fixup = cht_codec_fixup,
026da220
ML
248 .dpcm_playback = 1,
249 .dpcm_capture = 1,
250 .ops = &cht_be_ssp2_ops,
251 },
252};
253
254/* SoC card */
255static struct snd_soc_card snd_soc_card_cht = {
256 .name = "cherrytrailcraudio",
257 .dai_link = cht_dailink,
258 .num_links = ARRAY_SIZE(cht_dailink),
259 .dapm_widgets = cht_dapm_widgets,
260 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
261 .dapm_routes = cht_audio_map,
262 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
263 .controls = cht_mc_controls,
264 .num_controls = ARRAY_SIZE(cht_mc_controls),
265};
266
267static int snd_cht_mc_probe(struct platform_device *pdev)
268{
269 int ret_val = 0;
270
271 /* register the soc card */
272 snd_soc_card_cht.dev = &pdev->dev;
273 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
274 if (ret_val) {
275 dev_err(&pdev->dev,
276 "snd_soc_register_card failed %d\n", ret_val);
277 return ret_val;
278 }
279 platform_set_drvdata(pdev, &snd_soc_card_cht);
280 return ret_val;
281}
282
283static struct platform_driver snd_cht_mc_driver = {
284 .driver = {
026da220 285 .name = "cht-bsw-rt5672",
026da220
ML
286 },
287 .probe = snd_cht_mc_probe,
288};
289
290module_platform_driver(snd_cht_mc_driver);
291
292MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
293MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
294MODULE_LICENSE("GPL v2");
295MODULE_ALIAS("platform:cht-bsw-rt5672");