GNU Linux-libre 4.19.281-gnu1
[releases.git] / sound / soc / intel / boards / cht_bsw_max98090_ti.c
1 /*
2  *  cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
3  *  platforms Cherrytrail and Braswell, with max98090 & TI codec.
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  Author: Fang, Yang A <yang.a.fang@intel.com>
7  *  This file is modified from cht_bsw_rt5645.c
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; version 2 of the License.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20  */
21
22 #include <linux/dmi.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/acpi.h>
27 #include <linux/clk.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
32 #include "../../codecs/max98090.h"
33 #include "../atom/sst-atom-controls.h"
34 #include "../../codecs/ts3a227e.h"
35
36 #define CHT_PLAT_CLK_3_HZ       19200000
37 #define CHT_CODEC_DAI   "HiFi"
38
39 #define QUIRK_PMC_PLT_CLK_0                             0x01
40
41 struct cht_mc_private {
42         struct clk *mclk;
43         struct snd_soc_jack jack;
44         bool ts3a227e_present;
45         int quirks;
46 };
47
48 static int platform_clock_control(struct snd_soc_dapm_widget *w,
49                                           struct snd_kcontrol *k, int  event)
50 {
51         struct snd_soc_dapm_context *dapm = w->dapm;
52         struct snd_soc_card *card = dapm->card;
53         struct snd_soc_dai *codec_dai;
54         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
55         int ret;
56
57         /* See the comment in snd_cht_mc_probe() */
58         if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
59                 return 0;
60
61         codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
62         if (!codec_dai) {
63                 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
64                 return -EIO;
65         }
66
67         if (SND_SOC_DAPM_EVENT_ON(event)) {
68                 ret = clk_prepare_enable(ctx->mclk);
69                 if (ret < 0) {
70                         dev_err(card->dev,
71                                 "could not configure MCLK state");
72                         return ret;
73                 }
74         } else {
75                 clk_disable_unprepare(ctx->mclk);
76         }
77
78         return 0;
79 }
80
81 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
82         SND_SOC_DAPM_HP("Headphone", NULL),
83         SND_SOC_DAPM_MIC("Headset Mic", NULL),
84         SND_SOC_DAPM_MIC("Int Mic", NULL),
85         SND_SOC_DAPM_SPK("Ext Spk", NULL),
86         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
87                             platform_clock_control, SND_SOC_DAPM_PRE_PMU |
88                             SND_SOC_DAPM_POST_PMD),
89 };
90
91 static const struct snd_soc_dapm_route cht_audio_map[] = {
92         {"IN34", NULL, "Headset Mic"},
93         {"Headset Mic", NULL, "MICBIAS"},
94         {"DMICL", NULL, "Int Mic"},
95         {"Headphone", NULL, "HPL"},
96         {"Headphone", NULL, "HPR"},
97         {"Ext Spk", NULL, "SPKL"},
98         {"Ext Spk", NULL, "SPKR"},
99         {"HiFi Playback", NULL, "ssp2 Tx"},
100         {"ssp2 Tx", NULL, "codec_out0"},
101         {"ssp2 Tx", NULL, "codec_out1"},
102         {"codec_in0", NULL, "ssp2 Rx" },
103         {"codec_in1", NULL, "ssp2 Rx" },
104         {"ssp2 Rx", NULL, "HiFi Capture"},
105         {"Headphone", NULL, "Platform Clock"},
106         {"Headset Mic", NULL, "Platform Clock"},
107         {"Int Mic", NULL, "Platform Clock"},
108         {"Ext Spk", NULL, "Platform Clock"},
109 };
110
111 static const struct snd_kcontrol_new cht_mc_controls[] = {
112         SOC_DAPM_PIN_SWITCH("Headphone"),
113         SOC_DAPM_PIN_SWITCH("Headset Mic"),
114         SOC_DAPM_PIN_SWITCH("Int Mic"),
115         SOC_DAPM_PIN_SWITCH("Ext Spk"),
116 };
117
118 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
119                              struct snd_pcm_hw_params *params)
120 {
121         struct snd_soc_pcm_runtime *rtd = substream->private_data;
122         struct snd_soc_dai *codec_dai = rtd->codec_dai;
123         int ret;
124
125         ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
126                                      CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
127         if (ret < 0) {
128                 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
129                 return ret;
130         }
131
132         return 0;
133 }
134
135 static int cht_ti_jack_event(struct notifier_block *nb,
136                 unsigned long event, void *data)
137 {
138         struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
139         struct snd_soc_dapm_context *dapm = &jack->card->dapm;
140
141         if (event & SND_JACK_MICROPHONE) {
142                 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
143                 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
144                 snd_soc_dapm_sync(dapm);
145         } else {
146                 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
147                 snd_soc_dapm_disable_pin(dapm, "SHDN");
148                 snd_soc_dapm_sync(dapm);
149         }
150
151         return 0;
152 }
153
154 static struct notifier_block cht_jack_nb = {
155         .notifier_call = cht_ti_jack_event,
156 };
157
158 static struct snd_soc_jack_pin hs_jack_pins[] = {
159         {
160                 .pin    = "Headphone",
161                 .mask   = SND_JACK_HEADPHONE,
162         },
163         {
164                 .pin    = "Headset Mic",
165                 .mask   = SND_JACK_MICROPHONE,
166         },
167 };
168
169 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
170         {
171                 .name           = "hp",
172                 .report         = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
173                 .debounce_time  = 200,
174         },
175         {
176                 .name           = "mic",
177                 .invert         = 1,
178                 .report         = SND_JACK_MICROPHONE,
179                 .debounce_time  = 200,
180         },
181 };
182
183 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
184 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
185
186 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
187         { "hp-gpios", &hp_gpios, 1 },
188         { "mic-gpios", &mic_gpios, 1 },
189         {},
190 };
191
192 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
193 {
194         int ret;
195         int jack_type;
196         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
197         struct snd_soc_jack *jack = &ctx->jack;
198
199         if (ctx->ts3a227e_present) {
200                 /*
201                  * The jack has already been created in the
202                  * cht_max98090_headset_init() function.
203                  */
204                 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
205                 return 0;
206         }
207
208         jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
209
210         ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
211                                     jack_type, jack,
212                                     hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
213         if (ret) {
214                 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
215                 return ret;
216         }
217
218         ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
219                                       ARRAY_SIZE(hs_jack_gpios),
220                                       hs_jack_gpios);
221         if (ret) {
222                 /*
223                  * flag error but don't bail if jack detect is broken
224                  * due to platform issues or bad BIOS/configuration
225                  */
226                 dev_err(runtime->dev,
227                         "jack detection gpios not added, error %d\n", ret);
228         }
229
230         /* See the comment in snd_cht_mc_probe() */
231         if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
232                 return 0;
233
234         /*
235          * The firmware might enable the clock at
236          * boot (this information may or may not
237          * be reflected in the enable clock register).
238          * To change the rate we must disable the clock
239          * first to cover these cases. Due to common
240          * clock framework restrictions that do not allow
241          * to disable a clock that has not been enabled,
242          * we need to enable the clock first.
243          */
244         ret = clk_prepare_enable(ctx->mclk);
245         if (!ret)
246                 clk_disable_unprepare(ctx->mclk);
247
248         ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
249
250         if (ret)
251                 dev_err(runtime->dev, "unable to set MCLK rate\n");
252
253         return ret;
254 }
255
256 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
257                             struct snd_pcm_hw_params *params)
258 {
259         struct snd_interval *rate = hw_param_interval(params,
260                         SNDRV_PCM_HW_PARAM_RATE);
261         struct snd_interval *channels = hw_param_interval(params,
262                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
263         int ret = 0;
264         unsigned int fmt = 0;
265
266         ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
267         if (ret < 0) {
268                 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
269                 return ret;
270         }
271
272         fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
273                                 | SND_SOC_DAIFMT_CBS_CFS;
274
275         ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
276         if (ret < 0) {
277                 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
278                 return ret;
279         }
280
281         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
282         rate->min = rate->max = 48000;
283         channels->min = channels->max = 2;
284
285         /* set SSP2 to 16-bit */
286         params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
287         return 0;
288 }
289
290 static int cht_aif1_startup(struct snd_pcm_substream *substream)
291 {
292         return snd_pcm_hw_constraint_single(substream->runtime,
293                         SNDRV_PCM_HW_PARAM_RATE, 48000);
294 }
295
296 static int cht_max98090_headset_init(struct snd_soc_component *component)
297 {
298         struct snd_soc_card *card = component->card;
299         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
300         struct snd_soc_jack *jack = &ctx->jack;
301         int jack_type;
302         int ret;
303
304         /*
305          * TI supports 4 butons headset detection
306          * KEY_MEDIA
307          * KEY_VOICECOMMAND
308          * KEY_VOLUMEUP
309          * KEY_VOLUMEDOWN
310          */
311         jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
312                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
313                     SND_JACK_BTN_2 | SND_JACK_BTN_3;
314
315         ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
316                                     jack, NULL, 0);
317         if (ret) {
318                 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
319                 return ret;
320         }
321
322         return ts3a227e_enable_jack_detect(component, jack);
323 }
324
325 static const struct snd_soc_ops cht_aif1_ops = {
326         .startup = cht_aif1_startup,
327 };
328
329 static const struct snd_soc_ops cht_be_ssp2_ops = {
330         .hw_params = cht_aif1_hw_params,
331 };
332
333 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
334         .name = "Headset Chip",
335         .init = cht_max98090_headset_init,
336         .codec_name = "i2c-104C227E:00",
337 };
338
339 static struct snd_soc_dai_link cht_dailink[] = {
340         [MERR_DPCM_AUDIO] = {
341                 .name = "Audio Port",
342                 .stream_name = "Audio",
343                 .cpu_dai_name = "media-cpu-dai",
344                 .codec_dai_name = "snd-soc-dummy-dai",
345                 .codec_name = "snd-soc-dummy",
346                 .platform_name = "sst-mfld-platform",
347                 .nonatomic = true,
348                 .dynamic = 1,
349                 .dpcm_playback = 1,
350                 .dpcm_capture = 1,
351                 .ops = &cht_aif1_ops,
352         },
353         [MERR_DPCM_DEEP_BUFFER] = {
354                 .name = "Deep-Buffer Audio Port",
355                 .stream_name = "Deep-Buffer Audio",
356                 .cpu_dai_name = "deepbuffer-cpu-dai",
357                 .codec_dai_name = "snd-soc-dummy-dai",
358                 .codec_name = "snd-soc-dummy",
359                 .platform_name = "sst-mfld-platform",
360                 .nonatomic = true,
361                 .dynamic = 1,
362                 .dpcm_playback = 1,
363                 .ops = &cht_aif1_ops,
364         },
365         /* back ends */
366         {
367                 .name = "SSP2-Codec",
368                 .id = 0,
369                 .cpu_dai_name = "ssp2-port",
370                 .platform_name = "sst-mfld-platform",
371                 .no_pcm = 1,
372                 .codec_dai_name = "HiFi",
373                 .codec_name = "i2c-193C9890:00",
374                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
375                                         | SND_SOC_DAIFMT_CBS_CFS,
376                 .init = cht_codec_init,
377                 .be_hw_params_fixup = cht_codec_fixup,
378                 .dpcm_playback = 1,
379                 .dpcm_capture = 1,
380                 .ops = &cht_be_ssp2_ops,
381         },
382 };
383
384 /* SoC card */
385 static struct snd_soc_card snd_soc_card_cht = {
386         .name = "chtmax98090",
387         .owner = THIS_MODULE,
388         .dai_link = cht_dailink,
389         .num_links = ARRAY_SIZE(cht_dailink),
390         .aux_dev = &cht_max98090_headset_dev,
391         .num_aux_devs = 1,
392         .dapm_widgets = cht_dapm_widgets,
393         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
394         .dapm_routes = cht_audio_map,
395         .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
396         .controls = cht_mc_controls,
397         .num_controls = ARRAY_SIZE(cht_mc_controls),
398 };
399
400 static const struct dmi_system_id cht_max98090_quirk_table[] = {
401         {
402                 /* Clapper model Chromebook */
403                 .matches = {
404                         DMI_MATCH(DMI_PRODUCT_NAME, "Clapper"),
405                 },
406                 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
407         },
408         {
409                 /* Gnawty model Chromebook (Acer Chromebook CB3-111) */
410                 .matches = {
411                         DMI_MATCH(DMI_PRODUCT_NAME, "Gnawty"),
412                 },
413                 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
414         },
415         {
416                 /* Swanky model Chromebook (Toshiba Chromebook 2) */
417                 .matches = {
418                         DMI_MATCH(DMI_PRODUCT_NAME, "Swanky"),
419                 },
420                 .driver_data = (void *)QUIRK_PMC_PLT_CLK_0,
421         },
422         {}
423 };
424
425 static int snd_cht_mc_probe(struct platform_device *pdev)
426 {
427         const struct dmi_system_id *dmi_id;
428         struct device *dev = &pdev->dev;
429         int ret_val = 0;
430         struct cht_mc_private *drv;
431         const char *mclk_name;
432
433         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
434         if (!drv)
435                 return -ENOMEM;
436
437         dmi_id = dmi_first_match(cht_max98090_quirk_table);
438         if (dmi_id)
439                 drv->quirks = (unsigned long)dmi_id->driver_data;
440
441         drv->ts3a227e_present = acpi_dev_found("104C227E");
442         if (!drv->ts3a227e_present) {
443                 /* no need probe TI jack detection chip */
444                 snd_soc_card_cht.aux_dev = NULL;
445                 snd_soc_card_cht.num_aux_devs = 0;
446
447                 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
448                                                          acpi_max98090_gpios);
449                 if (ret_val)
450                         dev_dbg(dev, "Unable to add GPIO mapping table\n");
451         }
452
453         /* register the soc card */
454         snd_soc_card_cht.dev = &pdev->dev;
455         snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
456
457         if (drv->quirks & QUIRK_PMC_PLT_CLK_0)
458                 mclk_name = "pmc_plt_clk_0";
459         else
460                 mclk_name = "pmc_plt_clk_3";
461
462         drv->mclk = devm_clk_get(&pdev->dev, mclk_name);
463         if (IS_ERR(drv->mclk)) {
464                 dev_err(&pdev->dev,
465                         "Failed to get MCLK from %s: %ld\n",
466                         mclk_name, PTR_ERR(drv->mclk));
467                 return PTR_ERR(drv->mclk);
468         }
469
470         /*
471          * Boards which have the MAX98090's clk connected to clk_0 do not seem
472          * to like it if we muck with the clock. If we disable the clock when
473          * it is unused we get "max98090 i2c-193C9890:00: PLL unlocked" errors
474          * and the PLL never seems to lock again.
475          * So for these boards we enable it here once and leave it at that.
476          */
477         if (drv->quirks & QUIRK_PMC_PLT_CLK_0) {
478                 ret_val = clk_prepare_enable(drv->mclk);
479                 if (ret_val < 0) {
480                         dev_err(&pdev->dev, "MCLK enable error: %d\n", ret_val);
481                         return ret_val;
482                 }
483         }
484
485         ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
486         if (ret_val) {
487                 dev_err(&pdev->dev,
488                         "snd_soc_register_card failed %d\n", ret_val);
489                 return ret_val;
490         }
491         platform_set_drvdata(pdev, &snd_soc_card_cht);
492         return ret_val;
493 }
494
495 static int snd_cht_mc_remove(struct platform_device *pdev)
496 {
497         struct snd_soc_card *card = platform_get_drvdata(pdev);
498         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
499
500         if (ctx->quirks & QUIRK_PMC_PLT_CLK_0)
501                 clk_disable_unprepare(ctx->mclk);
502
503         return 0;
504 }
505
506 static struct platform_driver snd_cht_mc_driver = {
507         .driver = {
508                 .name = "cht-bsw-max98090",
509         },
510         .probe = snd_cht_mc_probe,
511         .remove = snd_cht_mc_remove,
512 };
513
514 module_platform_driver(snd_cht_mc_driver)
515
516 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
517 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
518 MODULE_LICENSE("GPL v2");
519 MODULE_ALIAS("platform:cht-bsw-max98090");