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 <linux/clk.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/max98090.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../../codecs/ts3a227e.h"
34 
35 #define CHT_PLAT_CLK_3_HZ	19200000
36 #define CHT_CODEC_DAI	"HiFi"
37 
38 struct cht_mc_private {
39 	struct clk *mclk;
40 	struct snd_soc_jack jack;
41 	bool ts3a227e_present;
42 };
43 
44 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
45 {
46 	struct snd_soc_pcm_runtime *rtd;
47 
48 	list_for_each_entry(rtd, &card->rtd_list, list) {
49 		if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
50 			     strlen(CHT_CODEC_DAI)))
51 			return rtd->codec_dai;
52 	}
53 	return NULL;
54 }
55 
56 static int platform_clock_control(struct snd_soc_dapm_widget *w,
57 					  struct snd_kcontrol *k, int  event)
58 {
59 	struct snd_soc_dapm_context *dapm = w->dapm;
60 	struct snd_soc_card *card = dapm->card;
61 	struct snd_soc_dai *codec_dai;
62 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
63 	int ret;
64 
65 	codec_dai = cht_get_codec_dai(card);
66 	if (!codec_dai) {
67 		dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
68 		return -EIO;
69 	}
70 
71 	if (SND_SOC_DAPM_EVENT_ON(event)) {
72 		ret = clk_prepare_enable(ctx->mclk);
73 		if (ret < 0) {
74 			dev_err(card->dev,
75 				"could not configure MCLK state");
76 			return ret;
77 		}
78 	} else {
79 		clk_disable_unprepare(ctx->mclk);
80 	}
81 
82 	return 0;
83 }
84 
85 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
86 	SND_SOC_DAPM_HP("Headphone", NULL),
87 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
88 	SND_SOC_DAPM_MIC("Int Mic", NULL),
89 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
90 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
91 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
92 			    SND_SOC_DAPM_POST_PMD),
93 };
94 
95 static const struct snd_soc_dapm_route cht_audio_map[] = {
96 	{"IN34", NULL, "Headset Mic"},
97 	{"Headset Mic", NULL, "MICBIAS"},
98 	{"DMICL", NULL, "Int Mic"},
99 	{"Headphone", NULL, "HPL"},
100 	{"Headphone", NULL, "HPR"},
101 	{"Ext Spk", NULL, "SPKL"},
102 	{"Ext Spk", NULL, "SPKR"},
103 	{"HiFi Playback", NULL, "ssp2 Tx"},
104 	{"ssp2 Tx", NULL, "codec_out0"},
105 	{"ssp2 Tx", NULL, "codec_out1"},
106 	{"codec_in0", NULL, "ssp2 Rx" },
107 	{"codec_in1", NULL, "ssp2 Rx" },
108 	{"ssp2 Rx", NULL, "HiFi Capture"},
109 	{"Headphone", NULL, "Platform Clock"},
110 	{"Headset Mic", NULL, "Platform Clock"},
111 	{"Int Mic", NULL, "Platform Clock"},
112 	{"Ext Spk", NULL, "Platform Clock"},
113 };
114 
115 static const struct snd_kcontrol_new cht_mc_controls[] = {
116 	SOC_DAPM_PIN_SWITCH("Headphone"),
117 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
118 	SOC_DAPM_PIN_SWITCH("Int Mic"),
119 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
120 };
121 
122 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
123 			     struct snd_pcm_hw_params *params)
124 {
125 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
126 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
127 	int ret;
128 
129 	ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
130 				     CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
131 	if (ret < 0) {
132 		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
133 		return ret;
134 	}
135 
136 	return 0;
137 }
138 
139 static int cht_ti_jack_event(struct notifier_block *nb,
140 		unsigned long event, void *data)
141 {
142 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
143 	struct snd_soc_dapm_context *dapm = &jack->card->dapm;
144 
145 	if (event & SND_JACK_MICROPHONE) {
146 		snd_soc_dapm_force_enable_pin(dapm, "SHDN");
147 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
148 		snd_soc_dapm_sync(dapm);
149 	} else {
150 		snd_soc_dapm_disable_pin(dapm, "MICBIAS");
151 		snd_soc_dapm_disable_pin(dapm, "SHDN");
152 		snd_soc_dapm_sync(dapm);
153 	}
154 
155 	return 0;
156 }
157 
158 static struct notifier_block cht_jack_nb = {
159 	.notifier_call = cht_ti_jack_event,
160 };
161 
162 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
163 {
164 	int ret;
165 	int jack_type;
166 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
167 	struct snd_soc_jack *jack = &ctx->jack;
168 
169 	if (ctx->ts3a227e_present) {
170 		/*
171 		 * The jack has already been created in the
172 		 * cht_max98090_headset_init() function.
173 		 */
174 		snd_soc_jack_notifier_register(jack, &cht_jack_nb);
175 		return 0;
176 	}
177 
178 	jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
179 
180 	ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
181 					jack_type, jack, NULL, 0);
182 	if (ret) {
183 		dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
184 		return ret;
185 	}
186 
187 	if (ctx->ts3a227e_present)
188 		snd_soc_jack_notifier_register(jack, &cht_jack_nb);
189 
190 	/*
191 	 * The firmware might enable the clock at
192 	 * boot (this information may or may not
193 	 * be reflected in the enable clock register).
194 	 * To change the rate we must disable the clock
195 	 * first to cover these cases. Due to common
196 	 * clock framework restrictions that do not allow
197 	 * to disable a clock that has not been enabled,
198 	 * we need to enable the clock first.
199 	 */
200 	ret = clk_prepare_enable(ctx->mclk);
201 	if (!ret)
202 		clk_disable_unprepare(ctx->mclk);
203 
204 	ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
205 
206 	if (ret)
207 		dev_err(runtime->dev, "unable to set MCLK rate\n");
208 
209 	return ret;
210 }
211 
212 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
213 			    struct snd_pcm_hw_params *params)
214 {
215 	struct snd_interval *rate = hw_param_interval(params,
216 			SNDRV_PCM_HW_PARAM_RATE);
217 	struct snd_interval *channels = hw_param_interval(params,
218 						SNDRV_PCM_HW_PARAM_CHANNELS);
219 	int ret = 0;
220 	unsigned int fmt = 0;
221 
222 	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
223 	if (ret < 0) {
224 		dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
225 		return ret;
226 	}
227 
228 	fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
229 				| SND_SOC_DAIFMT_CBS_CFS;
230 
231 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
232 	if (ret < 0) {
233 		dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
234 		return ret;
235 	}
236 
237 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
238 	rate->min = rate->max = 48000;
239 	channels->min = channels->max = 2;
240 
241 	/* set SSP2 to 24-bit */
242 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
243 	return 0;
244 }
245 
246 static int cht_aif1_startup(struct snd_pcm_substream *substream)
247 {
248 	return snd_pcm_hw_constraint_single(substream->runtime,
249 			SNDRV_PCM_HW_PARAM_RATE, 48000);
250 }
251 
252 static int cht_max98090_headset_init(struct snd_soc_component *component)
253 {
254 	struct snd_soc_card *card = component->card;
255 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
256 	struct snd_soc_jack *jack = &ctx->jack;
257 	int jack_type;
258 	int ret;
259 
260 	/*
261 	 * TI supports 4 butons headset detection
262 	 * KEY_MEDIA
263 	 * KEY_VOICECOMMAND
264 	 * KEY_VOLUMEUP
265 	 * KEY_VOLUMEDOWN
266 	 */
267 	jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
268 		    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
269 		    SND_JACK_BTN_2 | SND_JACK_BTN_3;
270 
271 	ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
272 				    jack, NULL, 0);
273 	if (ret) {
274 		dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
275 		return ret;
276 	}
277 
278 	return ts3a227e_enable_jack_detect(component, &ctx->jack);
279 }
280 
281 static const struct snd_soc_ops cht_aif1_ops = {
282 	.startup = cht_aif1_startup,
283 };
284 
285 static const struct snd_soc_ops cht_be_ssp2_ops = {
286 	.hw_params = cht_aif1_hw_params,
287 };
288 
289 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
290 	.name = "Headset Chip",
291 	.init = cht_max98090_headset_init,
292 	.codec_name = "i2c-104C227E:00",
293 };
294 
295 static struct snd_soc_dai_link cht_dailink[] = {
296 	[MERR_DPCM_AUDIO] = {
297 		.name = "Audio Port",
298 		.stream_name = "Audio",
299 		.cpu_dai_name = "media-cpu-dai",
300 		.codec_dai_name = "snd-soc-dummy-dai",
301 		.codec_name = "snd-soc-dummy",
302 		.platform_name = "sst-mfld-platform",
303 		.nonatomic = true,
304 		.dynamic = 1,
305 		.dpcm_playback = 1,
306 		.dpcm_capture = 1,
307 		.ops = &cht_aif1_ops,
308 	},
309 	[MERR_DPCM_DEEP_BUFFER] = {
310 		.name = "Deep-Buffer Audio Port",
311 		.stream_name = "Deep-Buffer Audio",
312 		.cpu_dai_name = "deepbuffer-cpu-dai",
313 		.codec_dai_name = "snd-soc-dummy-dai",
314 		.codec_name = "snd-soc-dummy",
315 		.platform_name = "sst-mfld-platform",
316 		.nonatomic = true,
317 		.dynamic = 1,
318 		.dpcm_playback = 1,
319 		.ops = &cht_aif1_ops,
320 	},
321 	[MERR_DPCM_COMPR] = {
322 		.name = "Compressed Port",
323 		.stream_name = "Compress",
324 		.cpu_dai_name = "compress-cpu-dai",
325 		.codec_dai_name = "snd-soc-dummy-dai",
326 		.codec_name = "snd-soc-dummy",
327 		.platform_name = "sst-mfld-platform",
328 	},
329 	/* back ends */
330 	{
331 		.name = "SSP2-Codec",
332 		.id = 1,
333 		.cpu_dai_name = "ssp2-port",
334 		.platform_name = "sst-mfld-platform",
335 		.no_pcm = 1,
336 		.codec_dai_name = "HiFi",
337 		.codec_name = "i2c-193C9890:00",
338 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
339 					| SND_SOC_DAIFMT_CBS_CFS,
340 		.init = cht_codec_init,
341 		.be_hw_params_fixup = cht_codec_fixup,
342 		.dpcm_playback = 1,
343 		.dpcm_capture = 1,
344 		.ops = &cht_be_ssp2_ops,
345 	},
346 };
347 
348 /* SoC card */
349 static struct snd_soc_card snd_soc_card_cht = {
350 	.name = "chtmax98090",
351 	.owner = THIS_MODULE,
352 	.dai_link = cht_dailink,
353 	.num_links = ARRAY_SIZE(cht_dailink),
354 	.aux_dev = &cht_max98090_headset_dev,
355 	.num_aux_devs = 1,
356 	.dapm_widgets = cht_dapm_widgets,
357 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
358 	.dapm_routes = cht_audio_map,
359 	.num_dapm_routes = ARRAY_SIZE(cht_audio_map),
360 	.controls = cht_mc_controls,
361 	.num_controls = ARRAY_SIZE(cht_mc_controls),
362 };
363 
364 static int snd_cht_mc_probe(struct platform_device *pdev)
365 {
366 	int ret_val = 0;
367 	struct cht_mc_private *drv;
368 
369 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
370 	if (!drv)
371 		return -ENOMEM;
372 
373 	drv->ts3a227e_present = acpi_dev_found("104C227E");
374 	if (!drv->ts3a227e_present) {
375 		/* no need probe TI jack detection chip */
376 		snd_soc_card_cht.aux_dev = NULL;
377 		snd_soc_card_cht.num_aux_devs = 0;
378 	}
379 
380 	/* register the soc card */
381 	snd_soc_card_cht.dev = &pdev->dev;
382 	snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
383 
384 	drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
385 	if (IS_ERR(drv->mclk)) {
386 		dev_err(&pdev->dev,
387 			"Failed to get MCLK from pmc_plt_clk_3: %ld\n",
388 			PTR_ERR(drv->mclk));
389 		return PTR_ERR(drv->mclk);
390 	}
391 
392 	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
393 	if (ret_val) {
394 		dev_err(&pdev->dev,
395 			"snd_soc_register_card failed %d\n", ret_val);
396 		return ret_val;
397 	}
398 	platform_set_drvdata(pdev, &snd_soc_card_cht);
399 	return ret_val;
400 }
401 
402 static struct platform_driver snd_cht_mc_driver = {
403 	.driver = {
404 		.name = "cht-bsw-max98090",
405 	},
406 	.probe = snd_cht_mc_probe,
407 };
408 
409 module_platform_driver(snd_cht_mc_driver)
410 
411 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
412 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
413 MODULE_LICENSE("GPL v2");
414 MODULE_ALIAS("platform:cht-bsw-max98090");
415