GNU Linux-libre 4.9.331-gnu1
[releases.git] / sound / soc / intel / boards / cht_bsw_rt5645.c
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>
24 #include <linux/acpi.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <asm/cpu_device_id.h>
28 #include <asm/platform_sst_audio.h>
29 #include <linux/clk.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/jack.h>
34 #include "../../codecs/rt5645.h"
35 #include "../atom/sst-atom-controls.h"
36 #include "../common/sst-acpi.h"
37
38 #define CHT_PLAT_CLK_3_HZ       19200000
39 #define CHT_CODEC_DAI   "rt5645-aif1"
40
41 struct cht_acpi_card {
42         char *codec_id;
43         int codec_type;
44         struct snd_soc_card *soc_card;
45 };
46
47 struct cht_mc_private {
48         struct snd_soc_jack jack;
49         struct cht_acpi_card *acpi_card;
50         char codec_name[16];
51         struct clk *mclk;
52 };
53
54 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
55 {
56         struct snd_soc_pcm_runtime *rtd;
57
58         list_for_each_entry(rtd, &card->rtd_list, list) {
59                 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
60                              strlen(CHT_CODEC_DAI)))
61                         return rtd->codec_dai;
62         }
63         return NULL;
64 }
65
66 static int platform_clock_control(struct snd_soc_dapm_widget *w,
67                 struct snd_kcontrol *k, int  event)
68 {
69         struct snd_soc_dapm_context *dapm = w->dapm;
70         struct snd_soc_card *card = dapm->card;
71         struct snd_soc_dai *codec_dai;
72         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
73         int ret;
74
75         codec_dai = cht_get_codec_dai(card);
76         if (!codec_dai) {
77                 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
78                 return -EIO;
79         }
80
81         if (SND_SOC_DAPM_EVENT_ON(event)) {
82                 if (ctx->mclk) {
83                         ret = clk_prepare_enable(ctx->mclk);
84                         if (ret < 0) {
85                                 dev_err(card->dev,
86                                         "could not configure MCLK state");
87                                 return ret;
88                         }
89                 }
90         } else {
91                 /* Set codec sysclk source to its internal clock because codec PLL will
92                  * be off when idle and MCLK will also be off when codec is
93                  * runtime suspended. Codec needs clock for jack detection and button
94                  * press. MCLK is turned off with clock framework or ACPI.
95                  */
96                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
97                                         48000 * 512, SND_SOC_CLOCK_IN);
98                 if (ret < 0) {
99                         dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
100                         return ret;
101                 }
102
103                 if (ctx->mclk)
104                         clk_disable_unprepare(ctx->mclk);
105         }
106
107         return 0;
108 }
109
110 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
111         SND_SOC_DAPM_HP("Headphone", NULL),
112         SND_SOC_DAPM_MIC("Headset Mic", NULL),
113         SND_SOC_DAPM_MIC("Int Mic", NULL),
114         SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
115         SND_SOC_DAPM_SPK("Ext Spk", NULL),
116         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
117                         platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
118 };
119
120 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
121         {"IN1P", NULL, "Headset Mic"},
122         {"IN1N", NULL, "Headset Mic"},
123         {"DMIC L1", NULL, "Int Mic"},
124         {"DMIC R1", NULL, "Int Mic"},
125         {"IN2P", NULL, "Int Analog Mic"},
126         {"IN2N", NULL, "Int Analog Mic"},
127         {"Headphone", NULL, "HPOL"},
128         {"Headphone", NULL, "HPOR"},
129         {"Ext Spk", NULL, "SPOL"},
130         {"Ext Spk", NULL, "SPOR"},
131         {"AIF1 Playback", NULL, "ssp2 Tx"},
132         {"ssp2 Tx", NULL, "codec_out0"},
133         {"ssp2 Tx", NULL, "codec_out1"},
134         {"codec_in0", NULL, "ssp2 Rx" },
135         {"codec_in1", NULL, "ssp2 Rx" },
136         {"ssp2 Rx", NULL, "AIF1 Capture"},
137         {"Headphone", NULL, "Platform Clock"},
138         {"Headset Mic", NULL, "Platform Clock"},
139         {"Int Mic", NULL, "Platform Clock"},
140         {"Int Analog Mic", NULL, "Platform Clock"},
141         {"Int Analog Mic", NULL, "micbias1"},
142         {"Int Analog Mic", NULL, "micbias2"},
143         {"Ext Spk", NULL, "Platform Clock"},
144 };
145
146 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
147         {"IN1P", NULL, "Headset Mic"},
148         {"IN1N", NULL, "Headset Mic"},
149         {"DMIC L2", NULL, "Int Mic"},
150         {"DMIC R2", NULL, "Int Mic"},
151         {"Headphone", NULL, "HPOL"},
152         {"Headphone", NULL, "HPOR"},
153         {"Ext Spk", NULL, "SPOL"},
154         {"Ext Spk", NULL, "SPOR"},
155         {"AIF1 Playback", NULL, "ssp2 Tx"},
156         {"ssp2 Tx", NULL, "codec_out0"},
157         {"ssp2 Tx", NULL, "codec_out1"},
158         {"codec_in0", NULL, "ssp2 Rx" },
159         {"codec_in1", NULL, "ssp2 Rx" },
160         {"ssp2 Rx", NULL, "AIF1 Capture"},
161         {"Headphone", NULL, "Platform Clock"},
162         {"Headset Mic", NULL, "Platform Clock"},
163         {"Int Mic", NULL, "Platform Clock"},
164         {"Ext Spk", NULL, "Platform Clock"},
165 };
166
167 static const struct snd_kcontrol_new cht_mc_controls[] = {
168         SOC_DAPM_PIN_SWITCH("Headphone"),
169         SOC_DAPM_PIN_SWITCH("Headset Mic"),
170         SOC_DAPM_PIN_SWITCH("Int Mic"),
171         SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
172         SOC_DAPM_PIN_SWITCH("Ext Spk"),
173 };
174
175 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
176         {
177                 .pin    = "Headphone",
178                 .mask   = SND_JACK_HEADPHONE,
179         },
180         {
181                 .pin    = "Headset Mic",
182                 .mask   = SND_JACK_MICROPHONE,
183         },
184 };
185
186 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
187                              struct snd_pcm_hw_params *params)
188 {
189         struct snd_soc_pcm_runtime *rtd = substream->private_data;
190         struct snd_soc_dai *codec_dai = rtd->codec_dai;
191         int ret;
192
193         /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
194         ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
195                                   CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
196         if (ret < 0) {
197                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
198                 return ret;
199         }
200
201         ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
202                                 params_rate(params) * 512, SND_SOC_CLOCK_IN);
203         if (ret < 0) {
204                 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
205                 return ret;
206         }
207
208         return 0;
209 }
210
211 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
212 {
213         int ret;
214         int jack_type;
215         struct snd_soc_codec *codec = runtime->codec;
216         struct snd_soc_dai *codec_dai = runtime->codec_dai;
217         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
218
219         /* Select clk_i2s1_asrc as ASRC clock source */
220         rt5645_sel_asrc_clk_src(codec,
221                                 RT5645_DA_STEREO_FILTER |
222                                 RT5645_DA_MONO_L_FILTER |
223                                 RT5645_DA_MONO_R_FILTER |
224                                 RT5645_AD_STEREO_FILTER,
225                                 RT5645_CLK_SEL_I2S1_ASRC);
226
227         /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
228         ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
229         if (ret < 0) {
230                 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
231                 return ret;
232         }
233
234         if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
235                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
236                                         SND_JACK_BTN_0 | SND_JACK_BTN_1 |
237                                         SND_JACK_BTN_2 | SND_JACK_BTN_3;
238         else
239                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
240
241         ret = snd_soc_card_jack_new(runtime->card, "Headset",
242                                     jack_type, &ctx->jack,
243                                     cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
244         if (ret) {
245                 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
246                 return ret;
247         }
248
249         rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
250
251         if (ctx->mclk) {
252                 /*
253                  * The firmware might enable the clock at
254                  * boot (this information may or may not
255                  * be reflected in the enable clock register).
256                  * To change the rate we must disable the clock
257                  * first to cover these cases. Due to common
258                  * clock framework restrictions that do not allow
259                  * to disable a clock that has not been enabled,
260                  * we need to enable the clock first.
261                  */
262                 ret = clk_prepare_enable(ctx->mclk);
263                 if (!ret)
264                         clk_disable_unprepare(ctx->mclk);
265
266                 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
267
268                 if (ret)
269                         dev_err(runtime->dev, "unable to set MCLK rate\n");
270         }
271         return ret;
272 }
273
274 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
275                             struct snd_pcm_hw_params *params)
276 {
277         struct snd_interval *rate = hw_param_interval(params,
278                         SNDRV_PCM_HW_PARAM_RATE);
279         struct snd_interval *channels = hw_param_interval(params,
280                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
281
282         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
283         rate->min = rate->max = 48000;
284         channels->min = channels->max = 2;
285
286         /* set SSP2 to 24-bit */
287         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
288         return 0;
289 }
290
291 static int cht_aif1_startup(struct snd_pcm_substream *substream)
292 {
293         return snd_pcm_hw_constraint_single(substream->runtime,
294                         SNDRV_PCM_HW_PARAM_RATE, 48000);
295 }
296
297 static struct snd_soc_ops cht_aif1_ops = {
298         .startup = cht_aif1_startup,
299 };
300
301 static struct snd_soc_ops cht_be_ssp2_ops = {
302         .hw_params = cht_aif1_hw_params,
303 };
304
305 static struct snd_soc_dai_link cht_dailink[] = {
306         [MERR_DPCM_AUDIO] = {
307                 .name = "Audio Port",
308                 .stream_name = "Audio",
309                 .cpu_dai_name = "media-cpu-dai",
310                 .codec_dai_name = "snd-soc-dummy-dai",
311                 .codec_name = "snd-soc-dummy",
312                 .platform_name = "sst-mfld-platform",
313                 .nonatomic = true,
314                 .dynamic = 1,
315                 .dpcm_playback = 1,
316                 .dpcm_capture = 1,
317                 .ops = &cht_aif1_ops,
318         },
319         [MERR_DPCM_DEEP_BUFFER] = {
320                 .name = "Deep-Buffer Audio Port",
321                 .stream_name = "Deep-Buffer Audio",
322                 .cpu_dai_name = "deepbuffer-cpu-dai",
323                 .codec_dai_name = "snd-soc-dummy-dai",
324                 .codec_name = "snd-soc-dummy",
325                 .platform_name = "sst-mfld-platform",
326                 .nonatomic = true,
327                 .dynamic = 1,
328                 .dpcm_playback = 1,
329                 .ops = &cht_aif1_ops,
330         },
331         [MERR_DPCM_COMPR] = {
332                 .name = "Compressed Port",
333                 .stream_name = "Compress",
334                 .cpu_dai_name = "compress-cpu-dai",
335                 .codec_dai_name = "snd-soc-dummy-dai",
336                 .codec_name = "snd-soc-dummy",
337                 .platform_name = "sst-mfld-platform",
338         },
339         /* CODEC<->CODEC link */
340         /* back ends */
341         {
342                 .name = "SSP2-Codec",
343                 .id = 1,
344                 .cpu_dai_name = "ssp2-port",
345                 .platform_name = "sst-mfld-platform",
346                 .no_pcm = 1,
347                 .codec_dai_name = "rt5645-aif1",
348                 .codec_name = "i2c-10EC5645:00",
349                 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
350                                         | SND_SOC_DAIFMT_CBS_CFS,
351                 .init = cht_codec_init,
352                 .be_hw_params_fixup = cht_codec_fixup,
353                 .nonatomic = true,
354                 .dpcm_playback = 1,
355                 .dpcm_capture = 1,
356                 .ops = &cht_be_ssp2_ops,
357         },
358 };
359
360 /* SoC card */
361 static struct snd_soc_card snd_soc_card_chtrt5645 = {
362         .name = "chtrt5645",
363         .owner = THIS_MODULE,
364         .dai_link = cht_dailink,
365         .num_links = ARRAY_SIZE(cht_dailink),
366         .dapm_widgets = cht_dapm_widgets,
367         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
368         .dapm_routes = cht_rt5645_audio_map,
369         .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
370         .controls = cht_mc_controls,
371         .num_controls = ARRAY_SIZE(cht_mc_controls),
372 };
373
374 static struct snd_soc_card snd_soc_card_chtrt5650 = {
375         .name = "chtrt5650",
376         .owner = THIS_MODULE,
377         .dai_link = cht_dailink,
378         .num_links = ARRAY_SIZE(cht_dailink),
379         .dapm_widgets = cht_dapm_widgets,
380         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
381         .dapm_routes = cht_rt5650_audio_map,
382         .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
383         .controls = cht_mc_controls,
384         .num_controls = ARRAY_SIZE(cht_mc_controls),
385 };
386
387 static struct cht_acpi_card snd_soc_cards[] = {
388         {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
389         {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
390         {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
391 };
392
393 static char cht_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
394
395 static bool is_valleyview(void)
396 {
397         static const struct x86_cpu_id cpu_ids[] = {
398                 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
399                 {}
400         };
401
402         if (!x86_match_cpu(cpu_ids))
403                 return false;
404         return true;
405 }
406
407 static int snd_cht_mc_probe(struct platform_device *pdev)
408 {
409         int ret_val = 0;
410         int i;
411         struct cht_mc_private *drv;
412         struct snd_soc_card *card = snd_soc_cards[0].soc_card;
413         struct sst_acpi_mach *mach;
414         const char *i2c_name = NULL;
415         int dai_index = 0;
416         bool found = false;
417
418         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
419         if (!drv)
420                 return -ENOMEM;
421
422         mach = (&pdev->dev)->platform_data;
423
424         for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
425                 if (acpi_dev_found(snd_soc_cards[i].codec_id) &&
426                         (!strncmp(snd_soc_cards[i].codec_id, mach->id, 8))) {
427                         dev_dbg(&pdev->dev,
428                                 "found codec %s\n", snd_soc_cards[i].codec_id);
429                         card = snd_soc_cards[i].soc_card;
430                         drv->acpi_card = &snd_soc_cards[i];
431                         found = true;
432                         break;
433                 }
434         }
435
436         if (!found) {
437                 dev_err(&pdev->dev, "No matching HID found in supported list\n");
438                 return -ENODEV;
439         }
440
441         card->dev = &pdev->dev;
442         sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
443
444         /* set correct codec name */
445         for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
446                 if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
447                         card->dai_link[i].codec_name = drv->codec_name;
448                         dai_index = i;
449                 }
450
451         /* fixup codec name based on HID */
452         i2c_name = sst_acpi_find_name_from_hid(mach->id);
453         if (i2c_name != NULL) {
454                 snprintf(cht_rt5640_codec_name, sizeof(cht_rt5640_codec_name),
455                         "%s%s", "i2c-", i2c_name);
456                 cht_dailink[dai_index].codec_name = cht_rt5640_codec_name;
457         }
458
459         if (is_valleyview()) {
460                 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
461                 if (IS_ERR(drv->mclk)) {
462                         dev_err(&pdev->dev,
463                                 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
464                                 PTR_ERR(drv->mclk));
465                         return PTR_ERR(drv->mclk);
466                 }
467         }
468
469         snd_soc_card_set_drvdata(card, drv);
470         ret_val = devm_snd_soc_register_card(&pdev->dev, card);
471         if (ret_val) {
472                 dev_err(&pdev->dev,
473                         "snd_soc_register_card failed %d\n", ret_val);
474                 return ret_val;
475         }
476         platform_set_drvdata(pdev, card);
477         return ret_val;
478 }
479
480 static struct platform_driver snd_cht_mc_driver = {
481         .driver = {
482                 .name = "cht-bsw-rt5645",
483         },
484         .probe = snd_cht_mc_probe,
485 };
486
487 module_platform_driver(snd_cht_mc_driver)
488
489 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
490 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
491 MODULE_LICENSE("GPL v2");
492 MODULE_ALIAS("platform:cht-bsw-rt5645");