GNU Linux-libre 4.9.317-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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/jack.h>
30 #include "../../codecs/max98090.h"
31 #include "../atom/sst-atom-controls.h"
32 #include "../../codecs/ts3a227e.h"
33
34 #define CHT_PLAT_CLK_3_HZ       19200000
35 #define CHT_CODEC_DAI   "HiFi"
36
37 struct cht_mc_private {
38         struct snd_soc_jack jack;
39         bool ts3a227e_present;
40 };
41
42 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
43 {
44         struct snd_soc_pcm_runtime *rtd;
45
46         list_for_each_entry(rtd, &card->rtd_list, list) {
47                 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
48                              strlen(CHT_CODEC_DAI)))
49                         return rtd->codec_dai;
50         }
51         return NULL;
52 }
53
54 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
55         SND_SOC_DAPM_HP("Headphone", NULL),
56         SND_SOC_DAPM_MIC("Headset Mic", NULL),
57         SND_SOC_DAPM_MIC("Int Mic", NULL),
58         SND_SOC_DAPM_SPK("Ext Spk", NULL),
59 };
60
61 static const struct snd_soc_dapm_route cht_audio_map[] = {
62         {"IN34", NULL, "Headset Mic"},
63         {"Headset Mic", NULL, "MICBIAS"},
64         {"DMICL", NULL, "Int Mic"},
65         {"Headphone", NULL, "HPL"},
66         {"Headphone", NULL, "HPR"},
67         {"Ext Spk", NULL, "SPKL"},
68         {"Ext Spk", NULL, "SPKR"},
69         {"HiFi Playback", NULL, "ssp2 Tx"},
70         {"ssp2 Tx", NULL, "codec_out0"},
71         {"ssp2 Tx", NULL, "codec_out1"},
72         {"codec_in0", NULL, "ssp2 Rx" },
73         {"codec_in1", NULL, "ssp2 Rx" },
74         {"ssp2 Rx", NULL, "HiFi Capture"},
75 };
76
77 static const struct snd_kcontrol_new cht_mc_controls[] = {
78         SOC_DAPM_PIN_SWITCH("Headphone"),
79         SOC_DAPM_PIN_SWITCH("Headset Mic"),
80         SOC_DAPM_PIN_SWITCH("Int Mic"),
81         SOC_DAPM_PIN_SWITCH("Ext Spk"),
82 };
83
84 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
85                              struct snd_pcm_hw_params *params)
86 {
87         struct snd_soc_pcm_runtime *rtd = substream->private_data;
88         struct snd_soc_dai *codec_dai = rtd->codec_dai;
89         int ret;
90
91         ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
92                                      CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
93         if (ret < 0) {
94                 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
95                 return ret;
96         }
97
98         return 0;
99 }
100
101 static int cht_ti_jack_event(struct notifier_block *nb,
102                 unsigned long event, void *data)
103 {
104         struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
105         struct snd_soc_dapm_context *dapm = &jack->card->dapm;
106
107         if (event & SND_JACK_MICROPHONE) {
108                 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
109                 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
110                 snd_soc_dapm_sync(dapm);
111         } else {
112                 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
113                 snd_soc_dapm_disable_pin(dapm, "SHDN");
114                 snd_soc_dapm_sync(dapm);
115         }
116
117         return 0;
118 }
119
120 static struct notifier_block cht_jack_nb = {
121         .notifier_call = cht_ti_jack_event,
122 };
123
124 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
125 {
126         int ret;
127         int jack_type;
128         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
129         struct snd_soc_jack *jack = &ctx->jack;
130
131         if (ctx->ts3a227e_present) {
132                 /*
133                  * The jack has already been created in the
134                  * cht_max98090_headset_init() function.
135                  */
136                 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
137                 return 0;
138         }
139
140         jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
141
142         ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
143                                         jack_type, jack, NULL, 0);
144         if (ret) {
145                 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
146                 return ret;
147         }
148
149         if (ctx->ts3a227e_present)
150                 snd_soc_jack_notifier_register(jack, &cht_jack_nb);
151
152         return ret;
153 }
154
155 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
156                             struct snd_pcm_hw_params *params)
157 {
158         struct snd_interval *rate = hw_param_interval(params,
159                         SNDRV_PCM_HW_PARAM_RATE);
160         struct snd_interval *channels = hw_param_interval(params,
161                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
162         int ret = 0;
163         unsigned int fmt = 0;
164
165         ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
166         if (ret < 0) {
167                 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
168                 return ret;
169         }
170
171         fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
172                                 | SND_SOC_DAIFMT_CBS_CFS;
173
174         ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
175         if (ret < 0) {
176                 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
177                 return ret;
178         }
179
180         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
181         rate->min = rate->max = 48000;
182         channels->min = channels->max = 2;
183
184         /* set SSP2 to 24-bit */
185         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
186         return 0;
187 }
188
189 static int cht_aif1_startup(struct snd_pcm_substream *substream)
190 {
191         return snd_pcm_hw_constraint_single(substream->runtime,
192                         SNDRV_PCM_HW_PARAM_RATE, 48000);
193 }
194
195 static int cht_max98090_headset_init(struct snd_soc_component *component)
196 {
197         struct snd_soc_card *card = component->card;
198         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
199         struct snd_soc_jack *jack = &ctx->jack;
200         int jack_type;
201         int ret;
202
203         /*
204          * TI supports 4 butons headset detection
205          * KEY_MEDIA
206          * KEY_VOICECOMMAND
207          * KEY_VOLUMEUP
208          * KEY_VOLUMEDOWN
209          */
210         jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
211                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
212                     SND_JACK_BTN_2 | SND_JACK_BTN_3;
213
214         ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
215                                     jack, NULL, 0);
216         if (ret) {
217                 dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
218                 return ret;
219         }
220
221         return ts3a227e_enable_jack_detect(component, &ctx->jack);
222 }
223
224 static struct snd_soc_ops cht_aif1_ops = {
225         .startup = cht_aif1_startup,
226 };
227
228 static struct snd_soc_ops cht_be_ssp2_ops = {
229         .hw_params = cht_aif1_hw_params,
230 };
231
232 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
233         .name = "Headset Chip",
234         .init = cht_max98090_headset_init,
235         .codec_name = "i2c-104C227E:00",
236 };
237
238 static struct snd_soc_dai_link cht_dailink[] = {
239         [MERR_DPCM_AUDIO] = {
240                 .name = "Audio Port",
241                 .stream_name = "Audio",
242                 .cpu_dai_name = "media-cpu-dai",
243                 .codec_dai_name = "snd-soc-dummy-dai",
244                 .codec_name = "snd-soc-dummy",
245                 .platform_name = "sst-mfld-platform",
246                 .nonatomic = true,
247                 .dynamic = 1,
248                 .dpcm_playback = 1,
249                 .dpcm_capture = 1,
250                 .ops = &cht_aif1_ops,
251         },
252         [MERR_DPCM_DEEP_BUFFER] = {
253                 .name = "Deep-Buffer Audio Port",
254                 .stream_name = "Deep-Buffer Audio",
255                 .cpu_dai_name = "deepbuffer-cpu-dai",
256                 .codec_dai_name = "snd-soc-dummy-dai",
257                 .codec_name = "snd-soc-dummy",
258                 .platform_name = "sst-mfld-platform",
259                 .nonatomic = true,
260                 .dynamic = 1,
261                 .dpcm_playback = 1,
262                 .ops = &cht_aif1_ops,
263         },
264         [MERR_DPCM_COMPR] = {
265                 .name = "Compressed Port",
266                 .stream_name = "Compress",
267                 .cpu_dai_name = "compress-cpu-dai",
268                 .codec_dai_name = "snd-soc-dummy-dai",
269                 .codec_name = "snd-soc-dummy",
270                 .platform_name = "sst-mfld-platform",
271         },
272         /* back ends */
273         {
274                 .name = "SSP2-Codec",
275                 .id = 1,
276                 .cpu_dai_name = "ssp2-port",
277                 .platform_name = "sst-mfld-platform",
278                 .no_pcm = 1,
279                 .codec_dai_name = "HiFi",
280                 .codec_name = "i2c-193C9890:00",
281                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
282                                         | SND_SOC_DAIFMT_CBS_CFS,
283                 .init = cht_codec_init,
284                 .be_hw_params_fixup = cht_codec_fixup,
285                 .dpcm_playback = 1,
286                 .dpcm_capture = 1,
287                 .ops = &cht_be_ssp2_ops,
288         },
289 };
290
291 /* SoC card */
292 static struct snd_soc_card snd_soc_card_cht = {
293         .name = "chtmax98090",
294         .owner = THIS_MODULE,
295         .dai_link = cht_dailink,
296         .num_links = ARRAY_SIZE(cht_dailink),
297         .aux_dev = &cht_max98090_headset_dev,
298         .num_aux_devs = 1,
299         .dapm_widgets = cht_dapm_widgets,
300         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
301         .dapm_routes = cht_audio_map,
302         .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
303         .controls = cht_mc_controls,
304         .num_controls = ARRAY_SIZE(cht_mc_controls),
305 };
306
307 static int snd_cht_mc_probe(struct platform_device *pdev)
308 {
309         int ret_val = 0;
310         struct cht_mc_private *drv;
311
312         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
313         if (!drv)
314                 return -ENOMEM;
315
316         drv->ts3a227e_present = acpi_dev_found("104C227E");
317         if (!drv->ts3a227e_present) {
318                 /* no need probe TI jack detection chip */
319                 snd_soc_card_cht.aux_dev = NULL;
320                 snd_soc_card_cht.num_aux_devs = 0;
321         }
322
323         /* register the soc card */
324         snd_soc_card_cht.dev = &pdev->dev;
325         snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
326         ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
327         if (ret_val) {
328                 dev_err(&pdev->dev,
329                         "snd_soc_register_card failed %d\n", ret_val);
330                 return ret_val;
331         }
332         platform_set_drvdata(pdev, &snd_soc_card_cht);
333         return ret_val;
334 }
335
336 static struct platform_driver snd_cht_mc_driver = {
337         .driver = {
338                 .name = "cht-bsw-max98090",
339         },
340         .probe = snd_cht_mc_probe,
341 };
342
343 module_platform_driver(snd_cht_mc_driver)
344
345 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
346 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
347 MODULE_LICENSE("GPL v2");
348 MODULE_ALIAS("platform:cht-bsw-max98090");