Merge branch 'fix/intel' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[sfrench/cifs-2.6.git] / sound / soc / intel / boards / bytcr_rt5651.c
1 /*
2  *  bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
3  *  (derived from bytcr_rt5640.c)
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/acpi.h>
24 #include <linux/clk.h>
25 #include <linux/device.h>
26 #include <linux/dmi.h>
27 #include <linux/slab.h>
28 #include <asm/platform_sst_audio.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/jack.h>
33 #include <sound/soc-acpi.h>
34 #include "../../codecs/rt5651.h"
35 #include "../atom/sst-atom-controls.h"
36
37 enum {
38         BYT_RT5651_DMIC_MAP,
39         BYT_RT5651_IN1_MAP,
40         BYT_RT5651_IN2_MAP,
41         BYT_RT5651_IN1_IN2_MAP,
42 };
43
44 #define BYT_RT5651_MAP(quirk)   ((quirk) & GENMASK(7, 0))
45 #define BYT_RT5651_DMIC_EN      BIT(16)
46 #define BYT_RT5651_MCLK_EN      BIT(17)
47 #define BYT_RT5651_MCLK_25MHZ   BIT(18)
48
49 struct byt_rt5651_private {
50         struct clk *mclk;
51         struct snd_soc_jack jack;
52 };
53
54 static unsigned long byt_rt5651_quirk = BYT_RT5651_DMIC_MAP |
55                                         BYT_RT5651_DMIC_EN |
56                                         BYT_RT5651_MCLK_EN;
57
58 static void log_quirks(struct device *dev)
59 {
60         if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
61                 dev_info(dev, "quirk DMIC_MAP enabled");
62         if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
63                 dev_info(dev, "quirk IN1_MAP enabled");
64         if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
65                 dev_info(dev, "quirk IN2_MAP enabled");
66         if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
67                 dev_info(dev, "quirk DMIC enabled");
68         if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
69                 dev_info(dev, "quirk MCLK_EN enabled");
70         if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
71                 dev_info(dev, "quirk MCLK_25MHZ enabled");
72 }
73
74 #define BYT_CODEC_DAI1  "rt5651-aif1"
75
76 static int platform_clock_control(struct snd_soc_dapm_widget *w,
77                                   struct snd_kcontrol *k, int  event)
78 {
79         struct snd_soc_dapm_context *dapm = w->dapm;
80         struct snd_soc_card *card = dapm->card;
81         struct snd_soc_dai *codec_dai;
82         struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
83         int ret;
84
85         codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
86         if (!codec_dai) {
87                 dev_err(card->dev,
88                         "Codec dai not found; Unable to set platform clock\n");
89                 return -EIO;
90         }
91
92         if (SND_SOC_DAPM_EVENT_ON(event)) {
93                 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
94                         ret = clk_prepare_enable(priv->mclk);
95                         if (ret < 0) {
96                                 dev_err(card->dev,
97                                         "could not configure MCLK state");
98                                 return ret;
99                         }
100                 }
101                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
102                                              48000 * 512,
103                                              SND_SOC_CLOCK_IN);
104         } else {
105                 /*
106                  * Set codec clock source to internal clock before
107                  * turning off the platform clock. Codec needs clock
108                  * for Jack detection and button press
109                  */
110                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
111                                              48000 * 512,
112                                              SND_SOC_CLOCK_IN);
113                 if (!ret)
114                         if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
115                                 clk_disable_unprepare(priv->mclk);
116         }
117
118         if (ret < 0) {
119                 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
120                 return ret;
121         }
122
123         return 0;
124 }
125
126 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
127         SND_SOC_DAPM_HP("Headphone", NULL),
128         SND_SOC_DAPM_MIC("Headset Mic", NULL),
129         SND_SOC_DAPM_MIC("Internal Mic", NULL),
130         SND_SOC_DAPM_SPK("Speaker", NULL),
131         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
132                             platform_clock_control, SND_SOC_DAPM_PRE_PMU |
133                             SND_SOC_DAPM_POST_PMD),
134
135 };
136
137 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
138         {"Headphone", NULL, "Platform Clock"},
139         {"Headset Mic", NULL, "Platform Clock"},
140         {"Internal Mic", NULL, "Platform Clock"},
141         {"Speaker", NULL, "Platform Clock"},
142
143         {"AIF1 Playback", NULL, "ssp2 Tx"},
144         {"ssp2 Tx", NULL, "codec_out0"},
145         {"ssp2 Tx", NULL, "codec_out1"},
146         {"codec_in0", NULL, "ssp2 Rx"},
147         {"codec_in1", NULL, "ssp2 Rx"},
148         {"ssp2 Rx", NULL, "AIF1 Capture"},
149
150         {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
151         {"Headphone", NULL, "HPOL"},
152         {"Headphone", NULL, "HPOR"},
153         {"Speaker", NULL, "LOUTL"},
154         {"Speaker", NULL, "LOUTR"},
155 };
156
157 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
158         {"IN2P", NULL, "Headset Mic"},
159         {"DMIC L1", NULL, "Internal Mic"},
160         {"DMIC R1", NULL, "Internal Mic"},
161 };
162
163 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
164         {"Internal Mic", NULL, "micbias1"},
165         {"IN2P", NULL, "Headset Mic"},
166         {"IN1P", NULL, "Internal Mic"},
167 };
168
169 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
170         {"Internal Mic", NULL, "micbias1"},
171         {"IN1P", NULL, "Headset Mic"},
172         {"IN2P", NULL, "Internal Mic"},
173 };
174
175 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
176         {"Internal Mic", NULL, "micbias1"},
177         {"IN1P", NULL, "Internal Mic"},
178         {"IN2P", NULL, "Internal Mic"},
179         {"IN3P", NULL, "Headset Mic"},
180 };
181
182 static const struct snd_kcontrol_new byt_rt5651_controls[] = {
183         SOC_DAPM_PIN_SWITCH("Headphone"),
184         SOC_DAPM_PIN_SWITCH("Headset Mic"),
185         SOC_DAPM_PIN_SWITCH("Internal Mic"),
186         SOC_DAPM_PIN_SWITCH("Speaker"),
187 };
188
189 static struct snd_soc_jack_pin bytcr_jack_pins[] = {
190         {
191                 .pin    = "Headphone",
192                 .mask   = SND_JACK_HEADPHONE,
193         },
194         {
195                 .pin    = "Headset Mic",
196                 .mask   = SND_JACK_MICROPHONE,
197         },
198 };
199
200 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
201                                         struct snd_pcm_hw_params *params)
202 {
203         struct snd_soc_pcm_runtime *rtd = substream->private_data;
204         struct snd_soc_dai *codec_dai = rtd->codec_dai;
205         int ret;
206
207         snd_soc_dai_set_bclk_ratio(codec_dai, 50);
208
209         ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
210                                      params_rate(params) * 512,
211                                      SND_SOC_CLOCK_IN);
212         if (ret < 0) {
213                 dev_err(rtd->dev, "can't set codec clock %d\n", ret);
214                 return ret;
215         }
216
217         if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
218                 /* 2x25 bit slots on SSP2 */
219                 ret = snd_soc_dai_set_pll(codec_dai, 0,
220                                         RT5651_PLL1_S_BCLK1,
221                                         params_rate(params) * 50,
222                                         params_rate(params) * 512);
223         } else {
224                 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ) {
225                         ret = snd_soc_dai_set_pll(codec_dai, 0,
226                                                 RT5651_PLL1_S_MCLK,
227                                                 25000000,
228                                                 params_rate(params) * 512);
229                 } else {
230                         ret = snd_soc_dai_set_pll(codec_dai, 0,
231                                                 RT5651_PLL1_S_MCLK,
232                                                 19200000,
233                                                 params_rate(params) * 512);
234                 }
235         }
236
237         if (ret < 0) {
238                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
239                 return ret;
240         }
241
242         return 0;
243 }
244
245 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
246 {
247         byt_rt5651_quirk = (unsigned long)id->driver_data;
248         return 1;
249 }
250
251 static const struct dmi_system_id byt_rt5651_quirk_table[] = {
252         {
253                 .callback = byt_rt5651_quirk_cb,
254                 .matches = {
255                         DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
256                         DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
257                 },
258                 .driver_data = (void *)(BYT_RT5651_DMIC_MAP |
259                                         BYT_RT5651_DMIC_EN),
260         },
261         {
262                 .callback = byt_rt5651_quirk_cb,
263                 .matches = {
264                         DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
265                         DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
266                 },
267                 .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP),
268         },
269         {}
270 };
271
272 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
273 {
274         struct snd_soc_card *card = runtime->card;
275         struct snd_soc_codec *codec = runtime->codec;
276         struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
277         const struct snd_soc_dapm_route *custom_map;
278         int num_routes;
279         int ret;
280
281         card->dapm.idle_bias_off = true;
282
283         switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
284         case BYT_RT5651_IN1_MAP:
285                 custom_map = byt_rt5651_intmic_in1_map;
286                 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
287                 break;
288         case BYT_RT5651_IN2_MAP:
289                 custom_map = byt_rt5651_intmic_in2_map;
290                 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
291                 break;
292         case BYT_RT5651_IN1_IN2_MAP:
293                 custom_map = byt_rt5651_intmic_in1_in2_map;
294                 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
295                 break;
296         default:
297                 custom_map = byt_rt5651_intmic_dmic_map;
298                 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
299         }
300         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
301         if (ret)
302                 return ret;
303
304         ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
305                                         ARRAY_SIZE(byt_rt5651_controls));
306         if (ret) {
307                 dev_err(card->dev, "unable to add card controls\n");
308                 return ret;
309         }
310         snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
311         snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
312
313         if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
314                 /*
315                  * The firmware might enable the clock at
316                  * boot (this information may or may not
317                  * be reflected in the enable clock register).
318                  * To change the rate we must disable the clock
319                  * first to cover these cases. Due to common
320                  * clock framework restrictions that do not allow
321                  * to disable a clock that has not been enabled,
322                  * we need to enable the clock first.
323                  */
324                 ret = clk_prepare_enable(priv->mclk);
325                 if (!ret)
326                         clk_disable_unprepare(priv->mclk);
327
328                 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
329                         ret = clk_set_rate(priv->mclk, 25000000);
330                 else
331                         ret = clk_set_rate(priv->mclk, 19200000);
332
333                 if (ret)
334                         dev_err(card->dev, "unable to set MCLK rate\n");
335         }
336
337         ret = snd_soc_card_jack_new(runtime->card, "Headset",
338                                     SND_JACK_HEADSET, &priv->jack,
339                                     bytcr_jack_pins, ARRAY_SIZE(bytcr_jack_pins));
340         if (ret) {
341                 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
342                 return ret;
343         }
344
345         rt5651_set_jack_detect(codec, &priv->jack);
346
347         return ret;
348 }
349
350 static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
351         .formats = SNDRV_PCM_FMTBIT_S24_LE,
352         .rate_min = 48000,
353         .rate_max = 48000,
354         .channels_min = 2,
355         .channels_max = 2,
356 };
357
358 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
359                             struct snd_pcm_hw_params *params)
360 {
361         struct snd_interval *rate = hw_param_interval(params,
362                         SNDRV_PCM_HW_PARAM_RATE);
363         struct snd_interval *channels = hw_param_interval(params,
364                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
365         int ret;
366
367         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
368         rate->min = rate->max = 48000;
369         channels->min = channels->max = 2;
370
371         /* set SSP2 to 24-bit */
372         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
373
374         /*
375          * Default mode for SSP configuration is TDM 4 slot, override config
376          * with explicit setting to I2S 2ch 24-bit. The word length is set with
377          * dai_set_tdm_slot() since there is no other API exposed
378          */
379         ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
380                                   SND_SOC_DAIFMT_I2S     |
381                                   SND_SOC_DAIFMT_NB_NF   |
382                                   SND_SOC_DAIFMT_CBS_CFS
383                                   );
384
385         if (ret < 0) {
386                 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
387                 return ret;
388         }
389
390         ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
391         if (ret < 0) {
392                 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
393                 return ret;
394         }
395
396         return 0;
397 }
398
399 static const unsigned int rates_48000[] = {
400         48000,
401 };
402
403 static const struct snd_pcm_hw_constraint_list constraints_48000 = {
404         .count = ARRAY_SIZE(rates_48000),
405         .list  = rates_48000,
406 };
407
408 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
409 {
410         return snd_pcm_hw_constraint_list(substream->runtime, 0,
411                         SNDRV_PCM_HW_PARAM_RATE,
412                         &constraints_48000);
413 }
414
415 static const struct snd_soc_ops byt_rt5651_aif1_ops = {
416         .startup = byt_rt5651_aif1_startup,
417 };
418
419 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
420         .hw_params = byt_rt5651_aif1_hw_params,
421 };
422
423 static struct snd_soc_dai_link byt_rt5651_dais[] = {
424         [MERR_DPCM_AUDIO] = {
425                 .name = "Audio Port",
426                 .stream_name = "Audio",
427                 .cpu_dai_name = "media-cpu-dai",
428                 .codec_dai_name = "snd-soc-dummy-dai",
429                 .codec_name = "snd-soc-dummy",
430                 .platform_name = "sst-mfld-platform",
431                 .nonatomic = true,
432                 .dynamic = 1,
433                 .dpcm_playback = 1,
434                 .dpcm_capture = 1,
435                 .ops = &byt_rt5651_aif1_ops,
436         },
437         [MERR_DPCM_DEEP_BUFFER] = {
438                 .name = "Deep-Buffer Audio Port",
439                 .stream_name = "Deep-Buffer Audio",
440                 .cpu_dai_name = "deepbuffer-cpu-dai",
441                 .codec_dai_name = "snd-soc-dummy-dai",
442                 .codec_name = "snd-soc-dummy",
443                 .platform_name = "sst-mfld-platform",
444                 .nonatomic = true,
445                 .dynamic = 1,
446                 .dpcm_playback = 1,
447                 .ops = &byt_rt5651_aif1_ops,
448         },
449         /* CODEC<->CODEC link */
450         /* back ends */
451         {
452                 .name = "SSP2-Codec",
453                 .id = 0,
454                 .cpu_dai_name = "ssp2-port",
455                 .platform_name = "sst-mfld-platform",
456                 .no_pcm = 1,
457                 .codec_dai_name = "rt5651-aif1",
458                 .codec_name = "i2c-10EC5651:00",
459                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
460                                                 | SND_SOC_DAIFMT_CBS_CFS,
461                 .be_hw_params_fixup = byt_rt5651_codec_fixup,
462                 .ignore_suspend = 1,
463                 .nonatomic = true,
464                 .dpcm_playback = 1,
465                 .dpcm_capture = 1,
466                 .init = byt_rt5651_init,
467                 .ops = &byt_rt5651_be_ssp2_ops,
468         },
469 };
470
471 /* SoC card */
472 static struct snd_soc_card byt_rt5651_card = {
473         .name = "bytcr-rt5651",
474         .owner = THIS_MODULE,
475         .dai_link = byt_rt5651_dais,
476         .num_links = ARRAY_SIZE(byt_rt5651_dais),
477         .dapm_widgets = byt_rt5651_widgets,
478         .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
479         .dapm_routes = byt_rt5651_audio_map,
480         .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
481         .fully_routed = true,
482 };
483
484 static char byt_rt5651_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
485
486 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
487 {
488         struct byt_rt5651_private *priv;
489         struct snd_soc_acpi_mach *mach;
490         const char *i2c_name = NULL;
491         int ret_val = 0;
492         int dai_index = 0;
493         int i;
494
495         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
496         if (!priv)
497                 return -ENOMEM;
498
499         /* register the soc card */
500         byt_rt5651_card.dev = &pdev->dev;
501
502         mach = byt_rt5651_card.dev->platform_data;
503         snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
504
505         /* fix index of codec dai */
506         for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
507                 if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) {
508                         dai_index = i;
509                         break;
510                 }
511         }
512
513         /* fixup codec name based on HID */
514         i2c_name = snd_soc_acpi_find_name_from_hid(mach->id);
515         if (i2c_name) {
516                 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
517                         "%s%s", "i2c-", i2c_name);
518
519                 byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name;
520         }
521
522         /* check quirks before creating card */
523         dmi_check_system(byt_rt5651_quirk_table);
524         log_quirks(&pdev->dev);
525
526         if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
527                 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
528                 if (IS_ERR(priv->mclk)) {
529                         dev_err(&pdev->dev,
530                                 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
531                                 PTR_ERR(priv->mclk));
532                         /*
533                          * Fall back to bit clock usage for -ENOENT (clock not
534                          * available likely due to missing dependencies), bail
535                          * for all other errors, including -EPROBE_DEFER
536                          */
537                         if (ret_val != -ENOENT)
538                                 return ret_val;
539                         byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
540                 }
541         }
542
543         ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
544
545         if (ret_val) {
546                 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
547                         ret_val);
548                 return ret_val;
549         }
550         platform_set_drvdata(pdev, &byt_rt5651_card);
551         return ret_val;
552 }
553
554 static struct platform_driver snd_byt_rt5651_mc_driver = {
555         .driver = {
556                 .name = "bytcr_rt5651",
557         },
558         .probe = snd_byt_rt5651_mc_probe,
559 };
560
561 module_platform_driver(snd_byt_rt5651_mc_driver);
562
563 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
564 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
565 MODULE_LICENSE("GPL v2");
566 MODULE_ALIAS("platform:bytcr_rt5651");