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