Merge tag 'drm-next-2019-03-15' of git://anongit.freedesktop.org/drm/drm
[sfrench/cifs-2.6.git] / sound / soc / intel / boards / cht_bsw_nau8824.c
1 /*
2  *  cht-bsw-nau8824.c - ASoc Machine driver for Intel Cherryview-based
3  *          platforms Cherrytrail and Braswell, with nau8824 codec.
4  *
5  *  Copyright (C) 2018 Intel Corp
6  *  Copyright (C) 2018 Nuvoton Technology Corp
7  *
8  *  Author: Wang, Joseph C <joequant@gmail.com>
9  *  Co-author: John Hsu <KCHSU0@nuvoton.com>
10  *  This file is based on cht_bsw_rt5672.c and cht-bsw-max98090.c
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; version 2 of the License.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  */
21
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-acpi.h>
29 #include <sound/jack.h>
30 #include <linux/input.h>
31 #include "../atom/sst-atom-controls.h"
32 #include "../../codecs/nau8824.h"
33
34 struct cht_mc_private {
35         struct snd_soc_jack jack;
36 };
37
38 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
39         {
40                 .pin = "Headphone",
41                 .mask = SND_JACK_HEADPHONE,
42         },
43         {
44                 .pin = "Headset Mic",
45                 .mask = SND_JACK_MICROPHONE,
46         },
47 };
48
49 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
50         SND_SOC_DAPM_HP("Headphone", NULL),
51         SND_SOC_DAPM_MIC("Headset Mic", NULL),
52         SND_SOC_DAPM_MIC("Int Mic", NULL),
53         SND_SOC_DAPM_SPK("Ext Spk", NULL),
54 };
55
56 static const struct snd_soc_dapm_route cht_audio_map[] = {
57         {"Ext Spk", NULL, "SPKOUTL"},
58         {"Ext Spk", NULL, "SPKOUTR"},
59         {"Headphone", NULL, "HPOL"},
60         {"Headphone", NULL, "HPOR"},
61         {"MIC1", NULL, "Int Mic"},
62         {"MIC2", NULL, "Int Mic"},
63         {"HSMIC1", NULL, "Headset Mic"},
64         {"HSMIC2", NULL, "Headset Mic"},
65         {"Playback", NULL, "ssp2 Tx"},
66         {"ssp2 Tx", NULL, "codec_out0"},
67         {"ssp2 Tx", NULL, "codec_out1"},
68         {"codec_in0", NULL, "ssp2 Rx" },
69         {"codec_in1", NULL, "ssp2 Rx" },
70         {"ssp2 Rx", NULL, "Capture"},
71 };
72
73 static const struct snd_kcontrol_new cht_mc_controls[] = {
74         SOC_DAPM_PIN_SWITCH("Headphone"),
75         SOC_DAPM_PIN_SWITCH("Headset Mic"),
76         SOC_DAPM_PIN_SWITCH("Int Mic"),
77         SOC_DAPM_PIN_SWITCH("Ext Spk"),
78 };
79
80 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
81         struct snd_pcm_hw_params *params)
82 {
83         struct snd_soc_pcm_runtime *rtd = substream->private_data;
84         struct snd_soc_dai *codec_dai = rtd->codec_dai;
85         int ret;
86
87         ret = snd_soc_dai_set_sysclk(codec_dai, NAU8824_CLK_FLL_FS, 0,
88                 SND_SOC_CLOCK_IN);
89         if (ret < 0) {
90                 dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
91                 return ret;
92         }
93         ret = snd_soc_dai_set_pll(codec_dai, 0, 0, params_rate(params),
94                 params_rate(params) * 256);
95         if (ret < 0) {
96                 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
97                 return ret;
98         }
99
100         return 0;
101 }
102
103 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
104 {
105         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
106         struct snd_soc_jack *jack = &ctx->jack;
107         struct snd_soc_dai *codec_dai = runtime->codec_dai;
108         struct snd_soc_component *component = codec_dai->component;
109         int ret, jack_type;
110
111         /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
112         ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xf, 0x1, 4, 24);
113         if (ret < 0) {
114                 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
115                 return ret;
116         }
117
118         /* NAU88L24 supports 4 butons headset detection
119          * KEY_MEDIA
120          * KEY_VOICECOMMAND
121          * KEY_VOLUMEUP
122          * KEY_VOLUMEDOWN
123          */
124         jack_type = SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
125                 SND_JACK_BTN_2 | SND_JACK_BTN_3;
126         ret = snd_soc_card_jack_new(runtime->card, "Headset", jack_type, jack,
127                 cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
128         if (ret) {
129                 dev_err(runtime->dev,
130                         "Headset Jack creation failed %d\n", ret);
131                 return ret;
132         }
133         snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
134         snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
135         snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
136         snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
137
138         nau8824_enable_jack_detect(component, jack);
139
140         return ret;
141 }
142
143 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
144         struct snd_pcm_hw_params *params)
145 {
146         struct snd_interval *rate = hw_param_interval(params,
147                 SNDRV_PCM_HW_PARAM_RATE);
148         struct snd_interval *channels = hw_param_interval(params,
149                 SNDRV_PCM_HW_PARAM_CHANNELS);
150         struct snd_mask *fmt =
151                 hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
152
153         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
154         rate->min = rate->max = 48000;
155         channels->min = channels->max = 2;
156
157         /* set SSP2 to 24-bit */
158         snd_mask_none(fmt);
159         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
160
161         return 0;
162 }
163
164 static int cht_aif1_startup(struct snd_pcm_substream *substream)
165 {
166         return snd_pcm_hw_constraint_single(substream->runtime,
167                 SNDRV_PCM_HW_PARAM_RATE, 48000);
168 }
169
170 static const struct snd_soc_ops cht_aif1_ops = {
171         .startup = cht_aif1_startup,
172 };
173
174 static const struct snd_soc_ops cht_be_ssp2_ops = {
175         .hw_params = cht_aif1_hw_params,
176 };
177
178 static struct snd_soc_dai_link cht_dailink[] = {
179         /* Front End DAI links */
180         [MERR_DPCM_AUDIO] = {
181                 .name = "Audio Port",
182                 .stream_name = "Audio",
183                 .cpu_dai_name = "media-cpu-dai",
184                 .codec_dai_name = "snd-soc-dummy-dai",
185                 .codec_name = "snd-soc-dummy",
186                 .platform_name = "sst-mfld-platform",
187                 .nonatomic = true,
188                 .dynamic = 1,
189                 .dpcm_playback = 1,
190                 .dpcm_capture = 1,
191                 .ops = &cht_aif1_ops,
192         },
193         [MERR_DPCM_DEEP_BUFFER] = {
194                 .name = "Deep-Buffer Audio Port",
195                 .stream_name = "Deep-Buffer Audio",
196                 .cpu_dai_name = "deepbuffer-cpu-dai",
197                 .codec_dai_name = "snd-soc-dummy-dai",
198                 .codec_name = "snd-soc-dummy",
199                 .platform_name = "sst-mfld-platform",
200                 .nonatomic = true,
201                 .dynamic = 1,
202                 .dpcm_playback = 1,
203                 .ops = &cht_aif1_ops,
204         },
205         [MERR_DPCM_COMPR] = {
206                 .name = "Compressed Port",
207                 .stream_name = "Compress",
208                 .cpu_dai_name = "compress-cpu-dai",
209                 .codec_dai_name = "snd-soc-dummy-dai",
210                 .codec_name = "snd-soc-dummy",
211                 .platform_name = "sst-mfld-platform",
212         },
213         /* Back End DAI links */
214         {
215                 /* SSP2 - Codec */
216                 .name = "SSP2-Codec",
217                 .id = 1,
218                 .cpu_dai_name = "ssp2-port",
219                 .platform_name = "sst-mfld-platform",
220                 .no_pcm = 1,
221                 .codec_dai_name = NAU8824_CODEC_DAI,
222                 .codec_name = "i2c-10508824:00",
223                 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
224                         | SND_SOC_DAIFMT_CBS_CFS,
225                 .init = cht_codec_init,
226                 .be_hw_params_fixup = cht_codec_fixup,
227                 .dpcm_playback = 1,
228                 .dpcm_capture = 1,
229                 .ops = &cht_be_ssp2_ops,
230         },
231 };
232
233 /* SoC card */
234 static struct snd_soc_card snd_soc_card_cht = {
235         .name = "chtnau8824",
236         .owner = THIS_MODULE,
237         .dai_link = cht_dailink,
238         .num_links = ARRAY_SIZE(cht_dailink),
239         .dapm_widgets = cht_dapm_widgets,
240         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
241         .dapm_routes = cht_audio_map,
242         .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
243         .controls = cht_mc_controls,
244         .num_controls = ARRAY_SIZE(cht_mc_controls),
245 };
246
247 static int snd_cht_mc_probe(struct platform_device *pdev)
248 {
249         struct cht_mc_private *drv;
250         struct snd_soc_acpi_mach *mach;
251         const char *platform_name;
252         int ret_val;
253
254         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
255         if (!drv)
256                 return -ENOMEM;
257         snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
258
259         /* override plaform name, if required */
260         mach = (&pdev->dev)->platform_data;
261         platform_name = mach->mach_params.platform;
262
263         ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht,
264                                                         platform_name);
265         if (ret_val)
266                 return ret_val;
267
268         /* register the soc card */
269         snd_soc_card_cht.dev = &pdev->dev;
270         ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
271         if (ret_val) {
272                 dev_err(&pdev->dev,
273                         "snd_soc_register_card failed %d\n", ret_val);
274                 return ret_val;
275         }
276         platform_set_drvdata(pdev, &snd_soc_card_cht);
277
278         return ret_val;
279 }
280
281 static struct platform_driver snd_cht_mc_driver = {
282         .driver = {
283                 .name = "cht-bsw-nau8824",
284         },
285         .probe = snd_cht_mc_probe,
286 };
287
288 module_platform_driver(snd_cht_mc_driver);
289
290 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
291 MODULE_AUTHOR("Wang, Joseph C <joequant@gmail.com>");
292 MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>");
293 MODULE_LICENSE("GPL v2");
294 MODULE_ALIAS("platform:cht-bsw-nau8824");