1 /*
2  *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
3  *
4  *  Copyright (C) 2014 Intel Corp
5  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18  */
19 
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/device.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include "../../codecs/rt5640.h"
30 #include "../atom/sst-atom-controls.h"
31 
32 static const struct snd_soc_dapm_widget byt_dapm_widgets[] = {
33 	SND_SOC_DAPM_HP("Headphone", NULL),
34 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
35 	SND_SOC_DAPM_MIC("Int Mic", NULL),
36 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
37 };
38 
39 static const struct snd_soc_dapm_route byt_audio_map[] = {
40 	{"IN2P", NULL, "Headset Mic"},
41 	{"IN2N", NULL, "Headset Mic"},
42 	{"Headset Mic", NULL, "MICBIAS1"},
43 	{"IN1P", NULL, "MICBIAS1"},
44 	{"LDO2", NULL, "Int Mic"},
45 	{"Headphone", NULL, "HPOL"},
46 	{"Headphone", NULL, "HPOR"},
47 	{"Ext Spk", NULL, "SPOLP"},
48 	{"Ext Spk", NULL, "SPOLN"},
49 	{"Ext Spk", NULL, "SPORP"},
50 	{"Ext Spk", NULL, "SPORN"},
51 
52 	{"AIF1 Playback", NULL, "ssp2 Tx"},
53 	{"ssp2 Tx", NULL, "codec_out0"},
54 	{"ssp2 Tx", NULL, "codec_out1"},
55 	{"codec_in0", NULL, "ssp2 Rx"},
56 	{"codec_in1", NULL, "ssp2 Rx"},
57 	{"ssp2 Rx", NULL, "AIF1 Capture"},
58 };
59 
60 static const struct snd_kcontrol_new byt_mc_controls[] = {
61 	SOC_DAPM_PIN_SWITCH("Headphone"),
62 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
63 	SOC_DAPM_PIN_SWITCH("Int Mic"),
64 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
65 };
66 
67 static int byt_aif1_hw_params(struct snd_pcm_substream *substream,
68 					struct snd_pcm_hw_params *params)
69 {
70 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
72 	int ret;
73 
74 	snd_soc_dai_set_bclk_ratio(codec_dai, 50);
75 
76 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
77 				     params_rate(params) * 512,
78 				     SND_SOC_CLOCK_IN);
79 	if (ret < 0) {
80 		dev_err(rtd->dev, "can't set codec clock %d\n", ret);
81 		return ret;
82 	}
83 
84 	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1,
85 				  params_rate(params) * 50,
86 				  params_rate(params) * 512);
87 	if (ret < 0) {
88 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 static const struct snd_soc_pcm_stream byt_dai_params = {
96 	.formats = SNDRV_PCM_FMTBIT_S24_LE,
97 	.rate_min = 48000,
98 	.rate_max = 48000,
99 	.channels_min = 2,
100 	.channels_max = 2,
101 };
102 
103 static int byt_codec_fixup(struct snd_soc_pcm_runtime *rtd,
104 			    struct snd_pcm_hw_params *params)
105 {
106 	struct snd_interval *rate = hw_param_interval(params,
107 			SNDRV_PCM_HW_PARAM_RATE);
108 	struct snd_interval *channels = hw_param_interval(params,
109 						SNDRV_PCM_HW_PARAM_CHANNELS);
110 
111 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
112 	rate->min = rate->max = 48000;
113 	channels->min = channels->max = 2;
114 
115 	/* set SSP2 to 24-bit */
116 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
117 	return 0;
118 }
119 
120 static unsigned int rates_48000[] = {
121 	48000,
122 };
123 
124 static struct snd_pcm_hw_constraint_list constraints_48000 = {
125 	.count = ARRAY_SIZE(rates_48000),
126 	.list  = rates_48000,
127 };
128 
129 static int byt_aif1_startup(struct snd_pcm_substream *substream)
130 {
131 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
132 			SNDRV_PCM_HW_PARAM_RATE,
133 			&constraints_48000);
134 }
135 
136 static struct snd_soc_ops byt_aif1_ops = {
137 	.startup = byt_aif1_startup,
138 };
139 
140 static struct snd_soc_ops byt_be_ssp2_ops = {
141 	.hw_params = byt_aif1_hw_params,
142 };
143 
144 static struct snd_soc_dai_link byt_dailink[] = {
145 	[MERR_DPCM_AUDIO] = {
146 		.name = "Baytrail Audio Port",
147 		.stream_name = "Baytrail Audio",
148 		.cpu_dai_name = "media-cpu-dai",
149 		.codec_dai_name = "snd-soc-dummy-dai",
150 		.codec_name = "snd-soc-dummy",
151 		.platform_name = "sst-mfld-platform",
152 		.ignore_suspend = 1,
153 		.dynamic = 1,
154 		.dpcm_playback = 1,
155 		.dpcm_capture = 1,
156 		.ops = &byt_aif1_ops,
157 	},
158 	[MERR_DPCM_COMPR] = {
159 		.name = "Baytrail Compressed Port",
160 		.stream_name = "Baytrail Compress",
161 		.cpu_dai_name = "compress-cpu-dai",
162 		.codec_dai_name = "snd-soc-dummy-dai",
163 		.codec_name = "snd-soc-dummy",
164 		.platform_name = "sst-mfld-platform",
165 	},
166 		/* back ends */
167 	{
168 		.name = "SSP2-Codec",
169 		.be_id = 1,
170 		.cpu_dai_name = "ssp2-port",
171 		.platform_name = "sst-mfld-platform",
172 		.no_pcm = 1,
173 		.codec_dai_name = "rt5640-aif1",
174 		.codec_name = "i2c-10EC5640:00",
175 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
176 						| SND_SOC_DAIFMT_CBS_CFS,
177 		.be_hw_params_fixup = byt_codec_fixup,
178 		.ignore_suspend = 1,
179 		.dpcm_playback = 1,
180 		.dpcm_capture = 1,
181 		.ops = &byt_be_ssp2_ops,
182 	},
183 };
184 
185 /* SoC card */
186 static struct snd_soc_card snd_soc_card_byt = {
187 	.name = "baytrailcraudio",
188 	.dai_link = byt_dailink,
189 	.num_links = ARRAY_SIZE(byt_dailink),
190 	.dapm_widgets = byt_dapm_widgets,
191 	.num_dapm_widgets = ARRAY_SIZE(byt_dapm_widgets),
192 	.dapm_routes = byt_audio_map,
193 	.num_dapm_routes = ARRAY_SIZE(byt_audio_map),
194 	.controls = byt_mc_controls,
195 	.num_controls = ARRAY_SIZE(byt_mc_controls),
196 };
197 
198 static int snd_byt_mc_probe(struct platform_device *pdev)
199 {
200 	int ret_val = 0;
201 
202 	/* register the soc card */
203 	snd_soc_card_byt.dev = &pdev->dev;
204 
205 	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_byt);
206 	if (ret_val) {
207 		dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", ret_val);
208 		return ret_val;
209 	}
210 	platform_set_drvdata(pdev, &snd_soc_card_byt);
211 	return ret_val;
212 }
213 
214 static struct platform_driver snd_byt_mc_driver = {
215 	.driver = {
216 		.name = "bytt100_rt5640",
217 		.pm = &snd_soc_pm_ops,
218 	},
219 	.probe = snd_byt_mc_probe,
220 };
221 
222 module_platform_driver(snd_byt_mc_driver);
223 
224 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
225 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
226 MODULE_LICENSE("GPL v2");
227 MODULE_ALIAS("platform:bytt100_rt5640");
228