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 	{"HiFi 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, "HiFi 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_ti_jack_event(struct notifier_block *nb,
105 		unsigned long event, void *data)
106 {
107 
108 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
109 	struct snd_soc_dai *codec_dai = jack->card->rtd->codec_dai;
110 	struct snd_soc_codec *codec = codec_dai->codec;
111 
112 	if (event & SND_JACK_MICROPHONE) {
113 
114 		snd_soc_dapm_force_enable_pin(&codec->dapm, "SHDN");
115 		snd_soc_dapm_force_enable_pin(&codec->dapm, "MICBIAS");
116 		snd_soc_dapm_sync(&codec->dapm);
117 	} else {
118 
119 		snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS");
120 		snd_soc_dapm_disable_pin(&codec->dapm, "SHDN");
121 		snd_soc_dapm_sync(&codec->dapm);
122 	}
123 
124 	return 0;
125 }
126 
127 static struct notifier_block cht_jack_nb = {
128 	.notifier_call = cht_ti_jack_event,
129 };
130 
131 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
132 {
133 	int ret;
134 	int jack_type;
135 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
136 	struct snd_soc_jack *jack = &ctx->jack;
137 
138 	/**
139 	* TI supports 4 butons headset detection
140 	* KEY_MEDIA
141 	* KEY_VOICECOMMAND
142 	* KEY_VOLUMEUP
143 	* KEY_VOLUMEDOWN
144 	*/
145 	if (ctx->ts3a227e_present)
146 		jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
147 					SND_JACK_BTN_0 | SND_JACK_BTN_1 |
148 					SND_JACK_BTN_2 | SND_JACK_BTN_3;
149 	else
150 		jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
151 
152 	ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
153 					jack_type, jack, NULL, 0);
154 
155 	if (ret) {
156 		dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
157 		return ret;
158 	}
159 
160 	if (ctx->ts3a227e_present)
161 		snd_soc_jack_notifier_register(jack, &cht_jack_nb);
162 
163 	return ret;
164 }
165 
166 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
167 			    struct snd_pcm_hw_params *params)
168 {
169 	struct snd_interval *rate = hw_param_interval(params,
170 			SNDRV_PCM_HW_PARAM_RATE);
171 	struct snd_interval *channels = hw_param_interval(params,
172 						SNDRV_PCM_HW_PARAM_CHANNELS);
173 	int ret = 0;
174 	unsigned int fmt = 0;
175 
176 	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
177 	if (ret < 0) {
178 		dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
179 		return ret;
180 	}
181 
182 	fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
183 				| SND_SOC_DAIFMT_CBS_CFS;
184 
185 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
186 	if (ret < 0) {
187 		dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
188 		return ret;
189 	}
190 
191 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
192 	rate->min = rate->max = 48000;
193 	channels->min = channels->max = 2;
194 
195 	/* set SSP2 to 24-bit */
196 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
197 	return 0;
198 }
199 
200 static unsigned int rates_48000[] = {
201 	48000,
202 };
203 
204 static struct snd_pcm_hw_constraint_list constraints_48000 = {
205 	.count = ARRAY_SIZE(rates_48000),
206 	.list  = rates_48000,
207 };
208 
209 static int cht_aif1_startup(struct snd_pcm_substream *substream)
210 {
211 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
212 			SNDRV_PCM_HW_PARAM_RATE,
213 			&constraints_48000);
214 }
215 
216 static int cht_max98090_headset_init(struct snd_soc_component *component)
217 {
218 	struct snd_soc_card *card = component->card;
219 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
220 
221 	return ts3a227e_enable_jack_detect(component, &ctx->jack);
222 }
223 
224 static struct snd_soc_ops cht_aif1_ops = {
225 	.startup = cht_aif1_startup,
226 };
227 
228 static struct snd_soc_ops cht_be_ssp2_ops = {
229 	.hw_params = cht_aif1_hw_params,
230 };
231 
232 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
233 	.name = "Headset Chip",
234 	.init = cht_max98090_headset_init,
235 	.codec_name = "i2c-104C227E:00",
236 };
237 
238 static struct snd_soc_dai_link cht_dailink[] = {
239 	[MERR_DPCM_AUDIO] = {
240 		.name = "Audio Port",
241 		.stream_name = "Audio",
242 		.cpu_dai_name = "media-cpu-dai",
243 		.codec_dai_name = "snd-soc-dummy-dai",
244 		.codec_name = "snd-soc-dummy",
245 		.platform_name = "sst-mfld-platform",
246 		.nonatomic = true,
247 		.dynamic = 1,
248 		.dpcm_playback = 1,
249 		.dpcm_capture = 1,
250 		.ops = &cht_aif1_ops,
251 	},
252 	[MERR_DPCM_COMPR] = {
253 		.name = "Compressed Port",
254 		.stream_name = "Compress",
255 		.cpu_dai_name = "compress-cpu-dai",
256 		.codec_dai_name = "snd-soc-dummy-dai",
257 		.codec_name = "snd-soc-dummy",
258 		.platform_name = "sst-mfld-platform",
259 	},
260 	/* back ends */
261 	{
262 		.name = "SSP2-Codec",
263 		.be_id = 1,
264 		.cpu_dai_name = "ssp2-port",
265 		.platform_name = "sst-mfld-platform",
266 		.no_pcm = 1,
267 		.codec_dai_name = "HiFi",
268 		.codec_name = "i2c-193C9890:00",
269 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
270 					| SND_SOC_DAIFMT_CBS_CFS,
271 		.init = cht_codec_init,
272 		.be_hw_params_fixup = cht_codec_fixup,
273 		.dpcm_playback = 1,
274 		.dpcm_capture = 1,
275 		.ops = &cht_be_ssp2_ops,
276 	},
277 };
278 
279 /* SoC card */
280 static struct snd_soc_card snd_soc_card_cht = {
281 	.name = "chtmax98090",
282 	.dai_link = cht_dailink,
283 	.num_links = ARRAY_SIZE(cht_dailink),
284 	.aux_dev = &cht_max98090_headset_dev,
285 	.num_aux_devs = 1,
286 	.dapm_widgets = cht_dapm_widgets,
287 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
288 	.dapm_routes = cht_audio_map,
289 	.num_dapm_routes = ARRAY_SIZE(cht_audio_map),
290 	.controls = cht_mc_controls,
291 	.num_controls = ARRAY_SIZE(cht_mc_controls),
292 };
293 
294 static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
295 						void *context, void **ret)
296 {
297 	*(bool *)context = true;
298 	return AE_OK;
299 }
300 
301 static int snd_cht_mc_probe(struct platform_device *pdev)
302 {
303 	int ret_val = 0;
304 	bool found = false;
305 	struct cht_mc_private *drv;
306 
307 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
308 	if (!drv)
309 		return -ENOMEM;
310 
311 	if (ACPI_SUCCESS(acpi_get_devices(
312 					"104C227E",
313 					snd_acpi_codec_match,
314 					&found, NULL)) && found) {
315 		drv->ts3a227e_present = true;
316 	} else {
317 		/* no need probe TI jack detection chip */
318 		snd_soc_card_cht.aux_dev = NULL;
319 		snd_soc_card_cht.num_aux_devs = 0;
320 		drv->ts3a227e_present = false;
321 	}
322 
323 	/* register the soc card */
324 	snd_soc_card_cht.dev = &pdev->dev;
325 	snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
326 	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
327 	if (ret_val) {
328 		dev_err(&pdev->dev,
329 			"snd_soc_register_card failed %d\n", ret_val);
330 		return ret_val;
331 	}
332 	platform_set_drvdata(pdev, &snd_soc_card_cht);
333 	return ret_val;
334 }
335 
336 static struct platform_driver snd_cht_mc_driver = {
337 	.driver = {
338 		.name = "cht-bsw-max98090",
339 	},
340 	.probe = snd_cht_mc_probe,
341 };
342 
343 module_platform_driver(snd_cht_mc_driver)
344 
345 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
346 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
347 MODULE_LICENSE("GPL v2");
348 MODULE_ALIAS("platform:cht-bsw-max98090");
349