]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/intel/boards/cht_bsw_rt5645.c
Merge remote-tracking branches 'asoc/topic/compress', 'asoc/topic/const' and 'asoc...
[karo-tx-linux.git] / sound / soc / intel / boards / cht_bsw_rt5645.c
1 /*
2  *  cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
3  *                     Cherrytrail and Braswell, with RT5645 codec.
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  Author: Fang, Yang A <yang.a.fang@intel.com>
7  *              N,Harshapriya <harshapriya.n@intel.com>
8  *  This file is modified from cht_bsw_rt5672.c
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22
23 #include <linux/module.h>
24 #include <linux/acpi.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/jack.h>
31 #include "../../codecs/rt5645.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../common/sst-acpi.h"
34
35 #define CHT_PLAT_CLK_3_HZ       19200000
36 #define CHT_CODEC_DAI   "rt5645-aif1"
37
38 struct cht_acpi_card {
39         char *codec_id;
40         int codec_type;
41         struct snd_soc_card *soc_card;
42 };
43
44 struct cht_mc_private {
45         struct snd_soc_jack jack;
46         struct cht_acpi_card *acpi_card;
47         char codec_name[16];
48 };
49
50 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
51 {
52         struct snd_soc_pcm_runtime *rtd;
53
54         list_for_each_entry(rtd, &card->rtd_list, list) {
55                 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
56                              strlen(CHT_CODEC_DAI)))
57                         return rtd->codec_dai;
58         }
59         return NULL;
60 }
61
62 static int platform_clock_control(struct snd_soc_dapm_widget *w,
63                 struct snd_kcontrol *k, int  event)
64 {
65         struct snd_soc_dapm_context *dapm = w->dapm;
66         struct snd_soc_card *card = dapm->card;
67         struct snd_soc_dai *codec_dai;
68         int ret;
69
70         codec_dai = cht_get_codec_dai(card);
71         if (!codec_dai) {
72                 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
73                 return -EIO;
74         }
75
76         if (!SND_SOC_DAPM_EVENT_OFF(event))
77                 return 0;
78
79         /* Set codec sysclk source to its internal clock because codec PLL will
80          * be off when idle and MCLK will also be off by ACPI when codec is
81          * runtime suspended. Codec needs clock for jack detection and button
82          * press.
83          */
84         ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
85                         0, SND_SOC_CLOCK_IN);
86         if (ret < 0) {
87                 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
88                 return ret;
89         }
90
91         return 0;
92 }
93
94 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
95         SND_SOC_DAPM_HP("Headphone", NULL),
96         SND_SOC_DAPM_MIC("Headset Mic", NULL),
97         SND_SOC_DAPM_MIC("Int Mic", NULL),
98         SND_SOC_DAPM_SPK("Ext Spk", NULL),
99         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
100                         platform_clock_control, SND_SOC_DAPM_POST_PMD),
101 };
102
103 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
104         {"IN1P", NULL, "Headset Mic"},
105         {"IN1N", NULL, "Headset Mic"},
106         {"DMIC L1", NULL, "Int Mic"},
107         {"DMIC R1", NULL, "Int Mic"},
108         {"Headphone", NULL, "HPOL"},
109         {"Headphone", NULL, "HPOR"},
110         {"Ext Spk", NULL, "SPOL"},
111         {"Ext Spk", NULL, "SPOR"},
112         {"AIF1 Playback", NULL, "ssp2 Tx"},
113         {"ssp2 Tx", NULL, "codec_out0"},
114         {"ssp2 Tx", NULL, "codec_out1"},
115         {"codec_in0", NULL, "ssp2 Rx" },
116         {"codec_in1", NULL, "ssp2 Rx" },
117         {"ssp2 Rx", NULL, "AIF1 Capture"},
118         {"Headphone", NULL, "Platform Clock"},
119         {"Headset Mic", NULL, "Platform Clock"},
120         {"Int Mic", NULL, "Platform Clock"},
121         {"Ext Spk", NULL, "Platform Clock"},
122 };
123
124 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
125         {"IN1P", NULL, "Headset Mic"},
126         {"IN1N", NULL, "Headset Mic"},
127         {"DMIC L2", NULL, "Int Mic"},
128         {"DMIC R2", NULL, "Int Mic"},
129         {"Headphone", NULL, "HPOL"},
130         {"Headphone", NULL, "HPOR"},
131         {"Ext Spk", NULL, "SPOL"},
132         {"Ext Spk", NULL, "SPOR"},
133         {"AIF1 Playback", NULL, "ssp2 Tx"},
134         {"ssp2 Tx", NULL, "codec_out0"},
135         {"ssp2 Tx", NULL, "codec_out1"},
136         {"codec_in0", NULL, "ssp2 Rx" },
137         {"codec_in1", NULL, "ssp2 Rx" },
138         {"ssp2 Rx", NULL, "AIF1 Capture"},
139         {"Headphone", NULL, "Platform Clock"},
140         {"Headset Mic", NULL, "Platform Clock"},
141         {"Int Mic", NULL, "Platform Clock"},
142         {"Ext Spk", NULL, "Platform Clock"},
143 };
144
145 static const struct snd_kcontrol_new cht_mc_controls[] = {
146         SOC_DAPM_PIN_SWITCH("Headphone"),
147         SOC_DAPM_PIN_SWITCH("Headset Mic"),
148         SOC_DAPM_PIN_SWITCH("Int Mic"),
149         SOC_DAPM_PIN_SWITCH("Ext Spk"),
150 };
151
152 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
153         {
154                 .pin    = "Headphone",
155                 .mask   = SND_JACK_HEADPHONE,
156         },
157         {
158                 .pin    = "Headset Mic",
159                 .mask   = SND_JACK_MICROPHONE,
160         },
161 };
162
163 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
164                              struct snd_pcm_hw_params *params)
165 {
166         struct snd_soc_pcm_runtime *rtd = substream->private_data;
167         struct snd_soc_dai *codec_dai = rtd->codec_dai;
168         int ret;
169
170         /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
171         ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
172                                   CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
173         if (ret < 0) {
174                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
175                 return ret;
176         }
177
178         ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
179                                 params_rate(params) * 512, SND_SOC_CLOCK_IN);
180         if (ret < 0) {
181                 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
182                 return ret;
183         }
184
185         return 0;
186 }
187
188 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
189 {
190         int ret;
191         int jack_type;
192         struct snd_soc_codec *codec = runtime->codec;
193         struct snd_soc_dai *codec_dai = runtime->codec_dai;
194         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
195
196         /* Select clk_i2s1_asrc as ASRC clock source */
197         rt5645_sel_asrc_clk_src(codec,
198                                 RT5645_DA_STEREO_FILTER |
199                                 RT5645_DA_MONO_L_FILTER |
200                                 RT5645_DA_MONO_R_FILTER |
201                                 RT5645_AD_STEREO_FILTER,
202                                 RT5645_CLK_SEL_I2S1_ASRC);
203
204         /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
205         ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
206         if (ret < 0) {
207                 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
208                 return ret;
209         }
210
211         if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
212                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
213                                         SND_JACK_BTN_0 | SND_JACK_BTN_1 |
214                                         SND_JACK_BTN_2 | SND_JACK_BTN_3;
215         else
216                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
217
218         ret = snd_soc_card_jack_new(runtime->card, "Headset",
219                                     jack_type, &ctx->jack,
220                                     cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
221         if (ret) {
222                 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
223                 return ret;
224         }
225
226         rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
227
228         return ret;
229 }
230
231 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
232                             struct snd_pcm_hw_params *params)
233 {
234         struct snd_interval *rate = hw_param_interval(params,
235                         SNDRV_PCM_HW_PARAM_RATE);
236         struct snd_interval *channels = hw_param_interval(params,
237                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
238
239         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
240         rate->min = rate->max = 48000;
241         channels->min = channels->max = 2;
242
243         /* set SSP2 to 24-bit */
244         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
245         return 0;
246 }
247
248 static int cht_aif1_startup(struct snd_pcm_substream *substream)
249 {
250         return snd_pcm_hw_constraint_single(substream->runtime,
251                         SNDRV_PCM_HW_PARAM_RATE, 48000);
252 }
253
254 static const struct snd_soc_ops cht_aif1_ops = {
255         .startup = cht_aif1_startup,
256 };
257
258 static const struct snd_soc_ops cht_be_ssp2_ops = {
259         .hw_params = cht_aif1_hw_params,
260 };
261
262 static struct snd_soc_dai_link cht_dailink[] = {
263         [MERR_DPCM_AUDIO] = {
264                 .name = "Audio Port",
265                 .stream_name = "Audio",
266                 .cpu_dai_name = "media-cpu-dai",
267                 .codec_dai_name = "snd-soc-dummy-dai",
268                 .codec_name = "snd-soc-dummy",
269                 .platform_name = "sst-mfld-platform",
270                 .nonatomic = true,
271                 .dynamic = 1,
272                 .dpcm_playback = 1,
273                 .dpcm_capture = 1,
274                 .ops = &cht_aif1_ops,
275         },
276         [MERR_DPCM_DEEP_BUFFER] = {
277                 .name = "Deep-Buffer Audio Port",
278                 .stream_name = "Deep-Buffer Audio",
279                 .cpu_dai_name = "deepbuffer-cpu-dai",
280                 .codec_dai_name = "snd-soc-dummy-dai",
281                 .codec_name = "snd-soc-dummy",
282                 .platform_name = "sst-mfld-platform",
283                 .nonatomic = true,
284                 .dynamic = 1,
285                 .dpcm_playback = 1,
286                 .ops = &cht_aif1_ops,
287         },
288         [MERR_DPCM_COMPR] = {
289                 .name = "Compressed Port",
290                 .stream_name = "Compress",
291                 .cpu_dai_name = "compress-cpu-dai",
292                 .codec_dai_name = "snd-soc-dummy-dai",
293                 .codec_name = "snd-soc-dummy",
294                 .platform_name = "sst-mfld-platform",
295         },
296         /* CODEC<->CODEC link */
297         /* back ends */
298         {
299                 .name = "SSP2-Codec",
300                 .id = 1,
301                 .cpu_dai_name = "ssp2-port",
302                 .platform_name = "sst-mfld-platform",
303                 .no_pcm = 1,
304                 .codec_dai_name = "rt5645-aif1",
305                 .codec_name = "i2c-10EC5645:00",
306                 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
307                                         | SND_SOC_DAIFMT_CBS_CFS,
308                 .init = cht_codec_init,
309                 .be_hw_params_fixup = cht_codec_fixup,
310                 .nonatomic = true,
311                 .dpcm_playback = 1,
312                 .dpcm_capture = 1,
313                 .ops = &cht_be_ssp2_ops,
314         },
315 };
316
317 /* SoC card */
318 static struct snd_soc_card snd_soc_card_chtrt5645 = {
319         .name = "chtrt5645",
320         .owner = THIS_MODULE,
321         .dai_link = cht_dailink,
322         .num_links = ARRAY_SIZE(cht_dailink),
323         .dapm_widgets = cht_dapm_widgets,
324         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
325         .dapm_routes = cht_rt5645_audio_map,
326         .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
327         .controls = cht_mc_controls,
328         .num_controls = ARRAY_SIZE(cht_mc_controls),
329 };
330
331 static struct snd_soc_card snd_soc_card_chtrt5650 = {
332         .name = "chtrt5650",
333         .owner = THIS_MODULE,
334         .dai_link = cht_dailink,
335         .num_links = ARRAY_SIZE(cht_dailink),
336         .dapm_widgets = cht_dapm_widgets,
337         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
338         .dapm_routes = cht_rt5650_audio_map,
339         .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
340         .controls = cht_mc_controls,
341         .num_controls = ARRAY_SIZE(cht_mc_controls),
342 };
343
344 static struct cht_acpi_card snd_soc_cards[] = {
345         {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
346         {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
347         {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
348 };
349
350 static char cht_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
351
352 static int snd_cht_mc_probe(struct platform_device *pdev)
353 {
354         int ret_val = 0;
355         int i;
356         struct cht_mc_private *drv;
357         struct snd_soc_card *card = snd_soc_cards[0].soc_card;
358         struct sst_acpi_mach *mach;
359         const char *i2c_name = NULL;
360         int dai_index = 0;
361
362         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
363         if (!drv)
364                 return -ENOMEM;
365
366         for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
367                 if (acpi_dev_found(snd_soc_cards[i].codec_id)) {
368                         dev_dbg(&pdev->dev,
369                                 "found codec %s\n", snd_soc_cards[i].codec_id);
370                         card = snd_soc_cards[i].soc_card;
371                         drv->acpi_card = &snd_soc_cards[i];
372                         break;
373                 }
374         }
375         card->dev = &pdev->dev;
376         mach = card->dev->platform_data;
377         sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
378
379         /* set correct codec name */
380         for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
381                 if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
382                         card->dai_link[i].codec_name = drv->codec_name;
383                         dai_index = i;
384                 }
385
386         /* fixup codec name based on HID */
387         i2c_name = sst_acpi_find_name_from_hid(mach->id);
388         if (i2c_name != NULL) {
389                 snprintf(cht_rt5640_codec_name, sizeof(cht_rt5640_codec_name),
390                         "%s%s", "i2c-", i2c_name);
391                 cht_dailink[dai_index].codec_name = cht_rt5640_codec_name;
392         }
393
394         snd_soc_card_set_drvdata(card, drv);
395         ret_val = devm_snd_soc_register_card(&pdev->dev, card);
396         if (ret_val) {
397                 dev_err(&pdev->dev,
398                         "snd_soc_register_card failed %d\n", ret_val);
399                 return ret_val;
400         }
401         platform_set_drvdata(pdev, card);
402         return ret_val;
403 }
404
405 static struct platform_driver snd_cht_mc_driver = {
406         .driver = {
407                 .name = "cht-bsw-rt5645",
408         },
409         .probe = snd_cht_mc_probe,
410 };
411
412 module_platform_driver(snd_cht_mc_driver)
413
414 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
415 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
416 MODULE_LICENSE("GPL v2");
417 MODULE_ALIAS("platform:cht-bsw-rt5645");