Merge remote-tracking branches 'asoc/topic/tdm-slot', 'asoc/topic/tegra', 'asoc/topic...
[cascardo/linux.git] / sound / soc / intel / cht_bsw_rt5672.c
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
32 static 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
47 static 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
74 static 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
83 static 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
106 static 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
113 static 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
139 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
140 {
141         int ret;
142         struct snd_soc_dai *codec_dai = runtime->codec_dai;
143         struct snd_soc_codec *codec = codec_dai->codec;
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
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);
165         return 0;
166 }
167
168 static 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 */
181         snd_mask_set(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT -
182                                     SNDRV_PCM_HW_PARAM_FIRST_MASK],
183                                     SNDRV_PCM_FORMAT_S24_LE);
184         return 0;
185 }
186
187 static unsigned int rates_48000[] = {
188         48000,
189 };
190
191 static struct snd_pcm_hw_constraint_list constraints_48000 = {
192         .count = ARRAY_SIZE(rates_48000),
193         .list  = rates_48000,
194 };
195
196 static int cht_aif1_startup(struct snd_pcm_substream *substream)
197 {
198         return snd_pcm_hw_constraint_list(substream->runtime, 0,
199                         SNDRV_PCM_HW_PARAM_RATE,
200                         &constraints_48000);
201 }
202
203 static struct snd_soc_ops cht_aif1_ops = {
204         .startup = cht_aif1_startup,
205 };
206
207 static struct snd_soc_ops cht_be_ssp2_ops = {
208         .hw_params = cht_aif1_hw_params,
209 };
210
211 static struct snd_soc_dai_link cht_dailink[] = {
212         /* Front End DAI links */
213         [MERR_DPCM_AUDIO] = {
214                 .name = "Audio Port",
215                 .stream_name = "Audio",
216                 .cpu_dai_name = "media-cpu-dai",
217                 .codec_dai_name = "snd-soc-dummy-dai",
218                 .codec_name = "snd-soc-dummy",
219                 .platform_name = "sst-mfld-platform",
220                 .ignore_suspend = 1,
221                 .dynamic = 1,
222                 .dpcm_playback = 1,
223                 .dpcm_capture = 1,
224                 .ops = &cht_aif1_ops,
225         },
226         [MERR_DPCM_COMPR] = {
227                 .name = "Compressed Port",
228                 .stream_name = "Compress",
229                 .cpu_dai_name = "compress-cpu-dai",
230                 .codec_dai_name = "snd-soc-dummy-dai",
231                 .codec_name = "snd-soc-dummy",
232                 .platform_name = "sst-mfld-platform",
233         },
234
235         /* Back End DAI links */
236         {
237                 /* SSP2 - Codec */
238                 .name = "SSP2-Codec",
239                 .be_id = 1,
240                 .cpu_dai_name = "ssp2-port",
241                 .platform_name = "sst-mfld-platform",
242                 .no_pcm = 1,
243                 .codec_dai_name = "rt5670-aif1",
244                 .codec_name = "i2c-10EC5670:00",
245                 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
246                                         | SND_SOC_DAIFMT_CBS_CFS,
247                 .init = cht_codec_init,
248                 .be_hw_params_fixup = cht_codec_fixup,
249                 .ignore_suspend = 1,
250                 .dpcm_playback = 1,
251                 .dpcm_capture = 1,
252                 .ops = &cht_be_ssp2_ops,
253         },
254 };
255
256 /* SoC card */
257 static struct snd_soc_card snd_soc_card_cht = {
258         .name = "cherrytrailcraudio",
259         .dai_link = cht_dailink,
260         .num_links = ARRAY_SIZE(cht_dailink),
261         .dapm_widgets = cht_dapm_widgets,
262         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
263         .dapm_routes = cht_audio_map,
264         .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
265         .controls = cht_mc_controls,
266         .num_controls = ARRAY_SIZE(cht_mc_controls),
267 };
268
269 static int snd_cht_mc_probe(struct platform_device *pdev)
270 {
271         int ret_val = 0;
272
273         /* register the soc card */
274         snd_soc_card_cht.dev = &pdev->dev;
275         ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
276         if (ret_val) {
277                 dev_err(&pdev->dev,
278                         "snd_soc_register_card failed %d\n", ret_val);
279                 return ret_val;
280         }
281         platform_set_drvdata(pdev, &snd_soc_card_cht);
282         return ret_val;
283 }
284
285 static struct platform_driver snd_cht_mc_driver = {
286         .driver = {
287                 .name = "cht-bsw-rt5672",
288                 .pm = &snd_soc_pm_ops,
289         },
290         .probe = snd_cht_mc_probe,
291 };
292
293 module_platform_driver(snd_cht_mc_driver);
294
295 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
296 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
297 MODULE_LICENSE("GPL v2");
298 MODULE_ALIAS("platform:cht-bsw-rt5672");