ASoC: soc-core: remove legacy style dai_link
[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 SND_SOC_DAILINK_DEF(dummy,
179         DAILINK_COMP_ARRAY(COMP_DUMMY()));
180
181 SND_SOC_DAILINK_DEF(media,
182         DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
183
184 SND_SOC_DAILINK_DEF(deepbuffer,
185         DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
186
187 SND_SOC_DAILINK_DEF(compress,
188         DAILINK_COMP_ARRAY(COMP_CPU("compress-cpu-dai")));
189
190 SND_SOC_DAILINK_DEF(ssp2_port,
191         DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
192 SND_SOC_DAILINK_DEF(ssp2_codec,
193         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10508824:00",
194                                       NAU8824_CODEC_DAI)));
195
196 SND_SOC_DAILINK_DEF(platform,
197         DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
198
199 static struct snd_soc_dai_link cht_dailink[] = {
200         /* Front End DAI links */
201         [MERR_DPCM_AUDIO] = {
202                 .name = "Audio Port",
203                 .stream_name = "Audio",
204                 .nonatomic = true,
205                 .dynamic = 1,
206                 .dpcm_playback = 1,
207                 .dpcm_capture = 1,
208                 .ops = &cht_aif1_ops,
209                 SND_SOC_DAILINK_REG(media, dummy, platform),
210         },
211         [MERR_DPCM_DEEP_BUFFER] = {
212                 .name = "Deep-Buffer Audio Port",
213                 .stream_name = "Deep-Buffer Audio",
214                 .nonatomic = true,
215                 .dynamic = 1,
216                 .dpcm_playback = 1,
217                 .ops = &cht_aif1_ops,
218                 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
219         },
220         [MERR_DPCM_COMPR] = {
221                 .name = "Compressed Port",
222                 .stream_name = "Compress",
223                 SND_SOC_DAILINK_REG(compress, dummy, platform),
224         },
225         /* Back End DAI links */
226         {
227                 /* SSP2 - Codec */
228                 .name = "SSP2-Codec",
229                 .id = 1,
230                 .no_pcm = 1,
231                 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
232                         | SND_SOC_DAIFMT_CBS_CFS,
233                 .init = cht_codec_init,
234                 .be_hw_params_fixup = cht_codec_fixup,
235                 .dpcm_playback = 1,
236                 .dpcm_capture = 1,
237                 .ops = &cht_be_ssp2_ops,
238                 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
239         },
240 };
241
242 /* SoC card */
243 static struct snd_soc_card snd_soc_card_cht = {
244         .name = "chtnau8824",
245         .owner = THIS_MODULE,
246         .dai_link = cht_dailink,
247         .num_links = ARRAY_SIZE(cht_dailink),
248         .dapm_widgets = cht_dapm_widgets,
249         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
250         .dapm_routes = cht_audio_map,
251         .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
252         .controls = cht_mc_controls,
253         .num_controls = ARRAY_SIZE(cht_mc_controls),
254 };
255
256 static int snd_cht_mc_probe(struct platform_device *pdev)
257 {
258         struct cht_mc_private *drv;
259         struct snd_soc_acpi_mach *mach;
260         const char *platform_name;
261         int ret_val;
262
263         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
264         if (!drv)
265                 return -ENOMEM;
266         snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
267
268         /* override plaform name, if required */
269         mach = (&pdev->dev)->platform_data;
270         platform_name = mach->mach_params.platform;
271
272         ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht,
273                                                         platform_name);
274         if (ret_val)
275                 return ret_val;
276
277         /* register the soc card */
278         snd_soc_card_cht.dev = &pdev->dev;
279         ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
280         if (ret_val) {
281                 dev_err(&pdev->dev,
282                         "snd_soc_register_card failed %d\n", ret_val);
283                 return ret_val;
284         }
285         platform_set_drvdata(pdev, &snd_soc_card_cht);
286
287         return ret_val;
288 }
289
290 static struct platform_driver snd_cht_mc_driver = {
291         .driver = {
292                 .name = "cht-bsw-nau8824",
293         },
294         .probe = snd_cht_mc_probe,
295 };
296
297 module_platform_driver(snd_cht_mc_driver);
298
299 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
300 MODULE_AUTHOR("Wang, Joseph C <joequant@gmail.com>");
301 MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>");
302 MODULE_LICENSE("GPL v2");
303 MODULE_ALIAS("platform:cht-bsw-nau8824");