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