sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / soc / intel / boards / cht_bsw_rt5672.c
blobe4d46d4360d7e88cfdac008e9804bb7fdb310e6f
1 /*
2 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
3 * Cherrytrail and Braswell, with RT5672 codec.
5 * Copyright (C) 2014 Intel Corp
6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * Mengdong Lin <mengdong.lin@intel.com>
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.
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.
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 <sound/jack.h>
26 #include "../../codecs/rt5670.h"
27 #include "../atom/sst-atom-controls.h"
28 #include "../common/sst-acpi.h"
30 /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
31 #define CHT_PLAT_CLK_3_HZ 19200000
32 #define CHT_CODEC_DAI "rt5670-aif1"
34 static struct snd_soc_jack cht_bsw_headset;
35 static char cht_bsw_codec_name[16];
37 /* Headset jack detection DAPM pins */
38 static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
40 .pin = "Headset Mic",
41 .mask = SND_JACK_MICROPHONE,
44 .pin = "Headphone",
45 .mask = SND_JACK_HEADPHONE,
49 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
51 struct snd_soc_pcm_runtime *rtd;
53 list_for_each_entry(rtd, &card->rtd_list, list) {
54 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
55 strlen(CHT_CODEC_DAI)))
56 return rtd->codec_dai;
58 return NULL;
61 static int platform_clock_control(struct snd_soc_dapm_widget *w,
62 struct snd_kcontrol *k, int event)
64 struct snd_soc_dapm_context *dapm = w->dapm;
65 struct snd_soc_card *card = dapm->card;
66 struct snd_soc_dai *codec_dai;
67 int ret;
69 codec_dai = cht_get_codec_dai(card);
70 if (!codec_dai) {
71 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
72 return -EIO;
75 if (SND_SOC_DAPM_EVENT_ON(event)) {
76 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
77 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
78 CHT_PLAT_CLK_3_HZ, 48000 * 512);
79 if (ret < 0) {
80 dev_err(card->dev, "can't set codec pll: %d\n", ret);
81 return ret;
84 /* set codec sysclk source to PLL */
85 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
86 48000 * 512, SND_SOC_CLOCK_IN);
87 if (ret < 0) {
88 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
89 return ret;
91 } else {
92 /* Set codec sysclk source to its internal clock because codec
93 * PLL will be off when idle and MCLK will also be off by ACPI
94 * when codec is runtime suspended. Codec needs clock for jack
95 * detection and button press.
97 snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
98 48000 * 512, SND_SOC_CLOCK_IN);
100 return 0;
103 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
104 SND_SOC_DAPM_HP("Headphone", NULL),
105 SND_SOC_DAPM_MIC("Headset Mic", NULL),
106 SND_SOC_DAPM_MIC("Int Mic", NULL),
107 SND_SOC_DAPM_SPK("Ext Spk", NULL),
108 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
109 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
110 SND_SOC_DAPM_POST_PMD),
113 static const struct snd_soc_dapm_route cht_audio_map[] = {
114 {"IN1P", NULL, "Headset Mic"},
115 {"IN1N", NULL, "Headset Mic"},
116 {"DMIC L1", NULL, "Int Mic"},
117 {"DMIC R1", NULL, "Int Mic"},
118 {"Headphone", NULL, "HPOL"},
119 {"Headphone", NULL, "HPOR"},
120 {"Ext Spk", NULL, "SPOLP"},
121 {"Ext Spk", NULL, "SPOLN"},
122 {"Ext Spk", NULL, "SPORP"},
123 {"Ext Spk", NULL, "SPORN"},
124 {"AIF1 Playback", NULL, "ssp2 Tx"},
125 {"ssp2 Tx", NULL, "codec_out0"},
126 {"ssp2 Tx", NULL, "codec_out1"},
127 {"codec_in0", NULL, "ssp2 Rx"},
128 {"codec_in1", NULL, "ssp2 Rx"},
129 {"ssp2 Rx", NULL, "AIF1 Capture"},
130 {"Headphone", NULL, "Platform Clock"},
131 {"Headset Mic", NULL, "Platform Clock"},
132 {"Int Mic", NULL, "Platform Clock"},
133 {"Ext Spk", NULL, "Platform Clock"},
136 static const struct snd_kcontrol_new cht_mc_controls[] = {
137 SOC_DAPM_PIN_SWITCH("Headphone"),
138 SOC_DAPM_PIN_SWITCH("Headset Mic"),
139 SOC_DAPM_PIN_SWITCH("Int Mic"),
140 SOC_DAPM_PIN_SWITCH("Ext Spk"),
143 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
146 struct snd_soc_pcm_runtime *rtd = substream->private_data;
147 struct snd_soc_dai *codec_dai = rtd->codec_dai;
148 int ret;
150 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
151 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
152 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
153 if (ret < 0) {
154 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
155 return ret;
158 /* set codec sysclk source to PLL */
159 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
160 params_rate(params) * 512,
161 SND_SOC_CLOCK_IN);
162 if (ret < 0) {
163 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
164 return ret;
166 return 0;
169 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
171 int ret;
172 struct snd_soc_dai *codec_dai = runtime->codec_dai;
173 struct snd_soc_codec *codec = codec_dai->codec;
175 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
176 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
177 if (ret < 0) {
178 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
179 return ret;
182 /* Select codec ASRC clock source to track I2S1 clock, because codec
183 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
184 * be supported by RT5672. Otherwise, ASRC will be disabled and cause
185 * noise.
187 rt5670_sel_asrc_clk_src(codec,
188 RT5670_DA_STEREO_FILTER
189 | RT5670_DA_MONO_L_FILTER
190 | RT5670_DA_MONO_R_FILTER
191 | RT5670_AD_STEREO_FILTER
192 | RT5670_AD_MONO_L_FILTER
193 | RT5670_AD_MONO_R_FILTER,
194 RT5670_CLK_SEL_I2S1_ASRC);
196 ret = snd_soc_card_jack_new(runtime->card, "Headset",
197 SND_JACK_HEADSET | SND_JACK_BTN_0 |
198 SND_JACK_BTN_1 | SND_JACK_BTN_2, &cht_bsw_headset,
199 cht_bsw_headset_pins, ARRAY_SIZE(cht_bsw_headset_pins));
200 if (ret)
201 return ret;
203 rt5670_set_jack_detect(codec, &cht_bsw_headset);
204 return 0;
207 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
208 struct snd_pcm_hw_params *params)
210 struct snd_interval *rate = hw_param_interval(params,
211 SNDRV_PCM_HW_PARAM_RATE);
212 struct snd_interval *channels = hw_param_interval(params,
213 SNDRV_PCM_HW_PARAM_CHANNELS);
215 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
216 rate->min = rate->max = 48000;
217 channels->min = channels->max = 2;
219 /* set SSP2 to 24-bit */
220 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
221 return 0;
224 static int cht_aif1_startup(struct snd_pcm_substream *substream)
226 return snd_pcm_hw_constraint_single(substream->runtime,
227 SNDRV_PCM_HW_PARAM_RATE, 48000);
230 static const struct snd_soc_ops cht_aif1_ops = {
231 .startup = cht_aif1_startup,
234 static const struct snd_soc_ops cht_be_ssp2_ops = {
235 .hw_params = cht_aif1_hw_params,
238 static struct snd_soc_dai_link cht_dailink[] = {
239 /* Front End DAI links */
240 [MERR_DPCM_AUDIO] = {
241 .name = "Audio Port",
242 .stream_name = "Audio",
243 .cpu_dai_name = "media-cpu-dai",
244 .codec_dai_name = "snd-soc-dummy-dai",
245 .codec_name = "snd-soc-dummy",
246 .platform_name = "sst-mfld-platform",
247 .nonatomic = true,
248 .dynamic = 1,
249 .dpcm_playback = 1,
250 .dpcm_capture = 1,
251 .ops = &cht_aif1_ops,
253 [MERR_DPCM_DEEP_BUFFER] = {
254 .name = "Deep-Buffer Audio Port",
255 .stream_name = "Deep-Buffer Audio",
256 .cpu_dai_name = "deepbuffer-cpu-dai",
257 .codec_dai_name = "snd-soc-dummy-dai",
258 .codec_name = "snd-soc-dummy",
259 .platform_name = "sst-mfld-platform",
260 .nonatomic = true,
261 .dynamic = 1,
262 .dpcm_playback = 1,
263 .ops = &cht_aif1_ops,
265 [MERR_DPCM_COMPR] = {
266 .name = "Compressed Port",
267 .stream_name = "Compress",
268 .cpu_dai_name = "compress-cpu-dai",
269 .codec_dai_name = "snd-soc-dummy-dai",
270 .codec_name = "snd-soc-dummy",
271 .platform_name = "sst-mfld-platform",
274 /* Back End DAI links */
276 /* SSP2 - Codec */
277 .name = "SSP2-Codec",
278 .id = 1,
279 .cpu_dai_name = "ssp2-port",
280 .platform_name = "sst-mfld-platform",
281 .no_pcm = 1,
282 .nonatomic = true,
283 .codec_dai_name = "rt5670-aif1",
284 .codec_name = "i2c-10EC5670:00",
285 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
286 | SND_SOC_DAIFMT_CBS_CFS,
287 .init = cht_codec_init,
288 .be_hw_params_fixup = cht_codec_fixup,
289 .dpcm_playback = 1,
290 .dpcm_capture = 1,
291 .ops = &cht_be_ssp2_ops,
295 static int cht_suspend_pre(struct snd_soc_card *card)
297 struct snd_soc_component *component;
299 list_for_each_entry(component, &card->component_dev_list, card_list) {
300 if (!strcmp(component->name, "i2c-10EC5670:00")) {
301 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
303 dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n");
304 rt5670_jack_suspend(codec);
305 break;
308 return 0;
311 static int cht_resume_post(struct snd_soc_card *card)
313 struct snd_soc_component *component;
315 list_for_each_entry(component, &card->component_dev_list, card_list) {
316 if (!strcmp(component->name, "i2c-10EC5670:00")) {
317 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
319 dev_dbg(codec->dev, "enabling jack detect for resume.\n");
320 rt5670_jack_resume(codec);
321 break;
325 return 0;
328 /* SoC card */
329 static struct snd_soc_card snd_soc_card_cht = {
330 .name = "cherrytrailcraudio",
331 .owner = THIS_MODULE,
332 .dai_link = cht_dailink,
333 .num_links = ARRAY_SIZE(cht_dailink),
334 .dapm_widgets = cht_dapm_widgets,
335 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
336 .dapm_routes = cht_audio_map,
337 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
338 .controls = cht_mc_controls,
339 .num_controls = ARRAY_SIZE(cht_mc_controls),
340 .suspend_pre = cht_suspend_pre,
341 .resume_post = cht_resume_post,
344 #define RT5672_I2C_DEFAULT "i2c-10EC5670:00"
346 static int snd_cht_mc_probe(struct platform_device *pdev)
348 int ret_val = 0;
349 struct sst_acpi_mach *mach = pdev->dev.platform_data;
350 const char *i2c_name;
351 int i;
353 strcpy(cht_bsw_codec_name, RT5672_I2C_DEFAULT);
355 /* fixup codec name based on HID */
356 if (mach) {
357 i2c_name = sst_acpi_find_name_from_hid(mach->id);
358 if (i2c_name) {
359 snprintf(cht_bsw_codec_name, sizeof(cht_bsw_codec_name),
360 "i2c-%s", i2c_name);
361 for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
362 if (!strcmp(cht_dailink[i].codec_name,
363 RT5672_I2C_DEFAULT)) {
364 cht_dailink[i].codec_name =
365 cht_bsw_codec_name;
366 break;
372 /* register the soc card */
373 snd_soc_card_cht.dev = &pdev->dev;
374 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
375 if (ret_val) {
376 dev_err(&pdev->dev,
377 "snd_soc_register_card failed %d\n", ret_val);
378 return ret_val;
380 platform_set_drvdata(pdev, &snd_soc_card_cht);
381 return ret_val;
384 static struct platform_driver snd_cht_mc_driver = {
385 .driver = {
386 .name = "cht-bsw-rt5672",
388 .probe = snd_cht_mc_probe,
391 module_platform_driver(snd_cht_mc_driver);
393 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
394 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
395 MODULE_LICENSE("GPL v2");
396 MODULE_ALIAS("platform:cht-bsw-rt5672");