]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/soc/intel/boards/cht_bsw_rt5645.c
ASoC: Intel: cht_bsw_rt5645: harden ACPI device detection
[mirror_ubuntu-zesty-kernel.git] / sound / soc / intel / boards / cht_bsw_rt5645.c
CommitLineData
e18acdc0
FY
1/*
2 * cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
3 * Cherrytrail and Braswell, with RT5645 codec.
4 *
5 * Copyright (C) 2015 Intel Corp
6 * Author: Fang, Yang A <yang.a.fang@intel.com>
7 * N,Harshapriya <harshapriya.n@intel.com>
8 * This file is modified from cht_bsw_rt5672.c
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23#include <linux/module.h>
c4ba51ba 24#include <linux/acpi.h>
e18acdc0
FY
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
30#include <sound/jack.h>
e56c72d5 31#include "../../codecs/rt5645.h"
b97169da 32#include "../atom/sst-atom-controls.h"
07d5c17b 33#include "../common/sst-acpi.h"
e18acdc0
FY
34
35#define CHT_PLAT_CLK_3_HZ 19200000
36#define CHT_CODEC_DAI "rt5645-aif1"
37
c4ba51ba
FY
38struct cht_acpi_card {
39 char *codec_id;
40 int codec_type;
41 struct snd_soc_card *soc_card;
42};
43
e18acdc0 44struct cht_mc_private {
673c4f89 45 struct snd_soc_jack jack;
c4ba51ba 46 struct cht_acpi_card *acpi_card;
a823a179 47 char codec_name[16];
e18acdc0
FY
48};
49
50static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
51{
1a497983 52 struct snd_soc_pcm_runtime *rtd;
e18acdc0 53
1a497983 54 list_for_each_entry(rtd, &card->rtd_list, list) {
e18acdc0
FY
55 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
56 strlen(CHT_CODEC_DAI)))
57 return rtd->codec_dai;
58 }
59 return NULL;
60}
61
62static int platform_clock_control(struct snd_soc_dapm_widget *w,
63 struct snd_kcontrol *k, int event)
64{
65 struct snd_soc_dapm_context *dapm = w->dapm;
66 struct snd_soc_card *card = dapm->card;
67 struct snd_soc_dai *codec_dai;
68 int ret;
69
70 codec_dai = cht_get_codec_dai(card);
71 if (!codec_dai) {
72 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
73 return -EIO;
74 }
75
76 if (!SND_SOC_DAPM_EVENT_OFF(event))
77 return 0;
78
79 /* Set codec sysclk source to its internal clock because codec PLL will
80 * be off when idle and MCLK will also be off by ACPI when codec is
81 * runtime suspended. Codec needs clock for jack detection and button
82 * press.
83 */
84 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
85 0, SND_SOC_CLOCK_IN);
86 if (ret < 0) {
87 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
88 return ret;
89 }
90
91 return 0;
92}
93
94static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
95 SND_SOC_DAPM_HP("Headphone", NULL),
96 SND_SOC_DAPM_MIC("Headset Mic", NULL),
97 SND_SOC_DAPM_MIC("Int Mic", NULL),
98 SND_SOC_DAPM_SPK("Ext Spk", NULL),
99 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
100 platform_clock_control, SND_SOC_DAPM_POST_PMD),
101};
102
c4ba51ba 103static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
e18acdc0
FY
104 {"IN1P", NULL, "Headset Mic"},
105 {"IN1N", NULL, "Headset Mic"},
106 {"DMIC L1", NULL, "Int Mic"},
107 {"DMIC R1", NULL, "Int Mic"},
108 {"Headphone", NULL, "HPOL"},
109 {"Headphone", NULL, "HPOR"},
110 {"Ext Spk", NULL, "SPOL"},
111 {"Ext Spk", NULL, "SPOR"},
112 {"AIF1 Playback", NULL, "ssp2 Tx"},
113 {"ssp2 Tx", NULL, "codec_out0"},
114 {"ssp2 Tx", NULL, "codec_out1"},
115 {"codec_in0", NULL, "ssp2 Rx" },
116 {"codec_in1", NULL, "ssp2 Rx" },
117 {"ssp2 Rx", NULL, "AIF1 Capture"},
118 {"Headphone", NULL, "Platform Clock"},
119 {"Headset Mic", NULL, "Platform Clock"},
120 {"Int Mic", NULL, "Platform Clock"},
121 {"Ext Spk", NULL, "Platform Clock"},
122};
123
c4ba51ba
FY
124static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
125 {"IN1P", NULL, "Headset Mic"},
126 {"IN1N", NULL, "Headset Mic"},
127 {"DMIC L2", NULL, "Int Mic"},
128 {"DMIC R2", NULL, "Int Mic"},
129 {"Headphone", NULL, "HPOL"},
130 {"Headphone", NULL, "HPOR"},
131 {"Ext Spk", NULL, "SPOL"},
132 {"Ext Spk", NULL, "SPOR"},
133 {"AIF1 Playback", NULL, "ssp2 Tx"},
134 {"ssp2 Tx", NULL, "codec_out0"},
135 {"ssp2 Tx", NULL, "codec_out1"},
136 {"codec_in0", NULL, "ssp2 Rx" },
137 {"codec_in1", NULL, "ssp2 Rx" },
138 {"ssp2 Rx", NULL, "AIF1 Capture"},
139 {"Headphone", NULL, "Platform Clock"},
140 {"Headset Mic", NULL, "Platform Clock"},
141 {"Int Mic", NULL, "Platform Clock"},
142 {"Ext Spk", NULL, "Platform Clock"},
143};
144
e18acdc0
FY
145static const struct snd_kcontrol_new cht_mc_controls[] = {
146 SOC_DAPM_PIN_SWITCH("Headphone"),
147 SOC_DAPM_PIN_SWITCH("Headset Mic"),
148 SOC_DAPM_PIN_SWITCH("Int Mic"),
149 SOC_DAPM_PIN_SWITCH("Ext Spk"),
150};
151
2303b32f
CC
152static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
153 {
154 .pin = "Headphone",
155 .mask = SND_JACK_HEADPHONE,
156 },
157 {
158 .pin = "Headset Mic",
159 .mask = SND_JACK_MICROPHONE,
160 },
161};
162
e18acdc0
FY
163static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
164 struct snd_pcm_hw_params *params)
165{
166 struct snd_soc_pcm_runtime *rtd = substream->private_data;
167 struct snd_soc_dai *codec_dai = rtd->codec_dai;
168 int ret;
169
170 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
171 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
172 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
173 if (ret < 0) {
174 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
175 return ret;
176 }
177
178 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
179 params_rate(params) * 512, SND_SOC_CLOCK_IN);
180 if (ret < 0) {
181 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
182 return ret;
183 }
184
185 return 0;
186}
187
188static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
189{
190 int ret;
673c4f89 191 int jack_type;
e18acdc0
FY
192 struct snd_soc_codec *codec = runtime->codec;
193 struct snd_soc_dai *codec_dai = runtime->codec_dai;
194 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
195
196 /* Select clk_i2s1_asrc as ASRC clock source */
197 rt5645_sel_asrc_clk_src(codec,
198 RT5645_DA_STEREO_FILTER |
199 RT5645_DA_MONO_L_FILTER |
200 RT5645_DA_MONO_R_FILTER |
201 RT5645_AD_STEREO_FILTER,
202 RT5645_CLK_SEL_I2S1_ASRC);
203
204 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
205 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
206 if (ret < 0) {
207 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
208 return ret;
209 }
210
673c4f89
FY
211 if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
212 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
213 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
214 SND_JACK_BTN_2 | SND_JACK_BTN_3;
215 else
216 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
e18acdc0 217
2303b32f 218 ret = snd_soc_card_jack_new(runtime->card, "Headset",
673c4f89 219 jack_type, &ctx->jack,
2303b32f 220 cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
e18acdc0 221 if (ret) {
673c4f89 222 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
e18acdc0
FY
223 return ret;
224 }
225
673c4f89 226 rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
e18acdc0
FY
227
228 return ret;
229}
230
231static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
232 struct snd_pcm_hw_params *params)
233{
234 struct snd_interval *rate = hw_param_interval(params,
235 SNDRV_PCM_HW_PARAM_RATE);
236 struct snd_interval *channels = hw_param_interval(params,
237 SNDRV_PCM_HW_PARAM_CHANNELS);
238
239 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
240 rate->min = rate->max = 48000;
241 channels->min = channels->max = 2;
242
243 /* set SSP2 to 24-bit */
369a9f5f 244 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
e18acdc0
FY
245 return 0;
246}
247
e18acdc0
FY
248static int cht_aif1_startup(struct snd_pcm_substream *substream)
249{
3d6a76c4
LPC
250 return snd_pcm_hw_constraint_single(substream->runtime,
251 SNDRV_PCM_HW_PARAM_RATE, 48000);
e18acdc0
FY
252}
253
9b6fdef6 254static const struct snd_soc_ops cht_aif1_ops = {
e18acdc0
FY
255 .startup = cht_aif1_startup,
256};
257
9b6fdef6 258static const struct snd_soc_ops cht_be_ssp2_ops = {
e18acdc0
FY
259 .hw_params = cht_aif1_hw_params,
260};
261
262static struct snd_soc_dai_link cht_dailink[] = {
263 [MERR_DPCM_AUDIO] = {
264 .name = "Audio Port",
265 .stream_name = "Audio",
266 .cpu_dai_name = "media-cpu-dai",
267 .codec_dai_name = "snd-soc-dummy-dai",
268 .codec_name = "snd-soc-dummy",
269 .platform_name = "sst-mfld-platform",
c4ba51ba 270 .nonatomic = true,
e18acdc0
FY
271 .dynamic = 1,
272 .dpcm_playback = 1,
273 .dpcm_capture = 1,
274 .ops = &cht_aif1_ops,
275 },
d35eb96a
PLB
276 [MERR_DPCM_DEEP_BUFFER] = {
277 .name = "Deep-Buffer Audio Port",
278 .stream_name = "Deep-Buffer Audio",
279 .cpu_dai_name = "deepbuffer-cpu-dai",
280 .codec_dai_name = "snd-soc-dummy-dai",
281 .codec_name = "snd-soc-dummy",
282 .platform_name = "sst-mfld-platform",
283 .nonatomic = true,
284 .dynamic = 1,
285 .dpcm_playback = 1,
286 .ops = &cht_aif1_ops,
287 },
e18acdc0
FY
288 [MERR_DPCM_COMPR] = {
289 .name = "Compressed Port",
290 .stream_name = "Compress",
291 .cpu_dai_name = "compress-cpu-dai",
292 .codec_dai_name = "snd-soc-dummy-dai",
293 .codec_name = "snd-soc-dummy",
294 .platform_name = "sst-mfld-platform",
295 },
296 /* CODEC<->CODEC link */
297 /* back ends */
298 {
299 .name = "SSP2-Codec",
2f0ad491 300 .id = 1,
e18acdc0
FY
301 .cpu_dai_name = "ssp2-port",
302 .platform_name = "sst-mfld-platform",
303 .no_pcm = 1,
304 .codec_dai_name = "rt5645-aif1",
305 .codec_name = "i2c-10EC5645:00",
306 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
307 | SND_SOC_DAIFMT_CBS_CFS,
308 .init = cht_codec_init,
309 .be_hw_params_fixup = cht_codec_fixup,
c4ba51ba 310 .nonatomic = true,
e18acdc0
FY
311 .dpcm_playback = 1,
312 .dpcm_capture = 1,
313 .ops = &cht_be_ssp2_ops,
314 },
315};
316
317/* SoC card */
c4ba51ba 318static struct snd_soc_card snd_soc_card_chtrt5645 = {
e18acdc0 319 .name = "chtrt5645",
54d8697f 320 .owner = THIS_MODULE,
e18acdc0
FY
321 .dai_link = cht_dailink,
322 .num_links = ARRAY_SIZE(cht_dailink),
323 .dapm_widgets = cht_dapm_widgets,
324 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
c4ba51ba
FY
325 .dapm_routes = cht_rt5645_audio_map,
326 .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
e18acdc0
FY
327 .controls = cht_mc_controls,
328 .num_controls = ARRAY_SIZE(cht_mc_controls),
329};
330
c4ba51ba
FY
331static struct snd_soc_card snd_soc_card_chtrt5650 = {
332 .name = "chtrt5650",
54d8697f 333 .owner = THIS_MODULE,
c4ba51ba
FY
334 .dai_link = cht_dailink,
335 .num_links = ARRAY_SIZE(cht_dailink),
336 .dapm_widgets = cht_dapm_widgets,
337 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
338 .dapm_routes = cht_rt5650_audio_map,
339 .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
340 .controls = cht_mc_controls,
341 .num_controls = ARRAY_SIZE(cht_mc_controls),
342};
343
344static struct cht_acpi_card snd_soc_cards[] = {
07d5c17b 345 {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
c4ba51ba
FY
346 {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
347 {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
348};
349
07d5c17b
VK
350static char cht_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
351
e18acdc0
FY
352static int snd_cht_mc_probe(struct platform_device *pdev)
353{
354 int ret_val = 0;
c4ba51ba 355 int i;
e18acdc0 356 struct cht_mc_private *drv;
c4ba51ba 357 struct snd_soc_card *card = snd_soc_cards[0].soc_card;
07d5c17b
VK
358 struct sst_acpi_mach *mach;
359 const char *i2c_name = NULL;
5d554ea4 360 int dai_index = 0;
2fc7ede5 361 bool found = false;
e18acdc0
FY
362
363 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
364 if (!drv)
365 return -ENOMEM;
366
2fc7ede5
PLB
367 mach = (&pdev->dev)->platform_data;
368
c4ba51ba 369 for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
2fc7ede5
PLB
370 if (acpi_dev_found(snd_soc_cards[i].codec_id) &&
371 (!strncmp(snd_soc_cards[i].codec_id, mach->id, 8))) {
c4ba51ba
FY
372 dev_dbg(&pdev->dev,
373 "found codec %s\n", snd_soc_cards[i].codec_id);
374 card = snd_soc_cards[i].soc_card;
375 drv->acpi_card = &snd_soc_cards[i];
2fc7ede5 376 found = true;
c4ba51ba
FY
377 break;
378 }
379 }
2fc7ede5
PLB
380
381 if (!found) {
382 dev_err(&pdev->dev, "No matching HID found in supported list\n");
383 return -ENODEV;
384 }
385
c4ba51ba 386 card->dev = &pdev->dev;
a823a179 387 sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
c8560b7c 388
c4ba51ba 389 /* set correct codec name */
c8560b7c 390 for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
07d5c17b 391 if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
a823a179 392 card->dai_link[i].codec_name = drv->codec_name;
07d5c17b
VK
393 dai_index = i;
394 }
395
396 /* fixup codec name based on HID */
397 i2c_name = sst_acpi_find_name_from_hid(mach->id);
398 if (i2c_name != NULL) {
399 snprintf(cht_rt5640_codec_name, sizeof(cht_rt5640_codec_name),
400 "%s%s", "i2c-", i2c_name);
401 cht_dailink[dai_index].codec_name = cht_rt5640_codec_name;
402 }
c8560b7c 403
c4ba51ba
FY
404 snd_soc_card_set_drvdata(card, drv);
405 ret_val = devm_snd_soc_register_card(&pdev->dev, card);
e18acdc0
FY
406 if (ret_val) {
407 dev_err(&pdev->dev,
408 "snd_soc_register_card failed %d\n", ret_val);
409 return ret_val;
410 }
c4ba51ba 411 platform_set_drvdata(pdev, card);
e18acdc0
FY
412 return ret_val;
413}
414
415static struct platform_driver snd_cht_mc_driver = {
416 .driver = {
e18acdc0 417 .name = "cht-bsw-rt5645",
e18acdc0
FY
418 },
419 .probe = snd_cht_mc_probe,
420};
421
422module_platform_driver(snd_cht_mc_driver)
423
424MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
425MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
426MODULE_LICENSE("GPL v2");
427MODULE_ALIAS("platform:cht-bsw-rt5645");