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 struct snd_soc_jack_pin hs_jack_pins[] = {
163 	{
164 		.pin	= "Headphone",
165 		.mask	= SND_JACK_HEADPHONE,
166 	},
167 	{
168 		.pin	= "Headset Mic",
169 		.mask	= SND_JACK_MICROPHONE,
170 	},
171 };
172 
173 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
174 	{
175 		.name		= "hp",
176 		.report		= SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
177 		.debounce_time	= 200,
178 	},
179 	{
180 		.name		= "mic",
181 		.invert		= 1,
182 		.report		= SND_JACK_MICROPHONE,
183 		.debounce_time	= 200,
184 	},
185 };
186 
187 static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
188 static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
189 
190 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
191 	{ "hp-gpios", &hp_gpios, 1 },
192 	{ "mic-gpios", &mic_gpios, 1 },
193 	{},
194 };
195 
196 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
197 {
198 	int ret;
199 	int jack_type;
200 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
201 	struct snd_soc_jack *jack = &ctx->jack;
202 
203 	if (ctx->ts3a227e_present) {
204 		/*
205 		 * The jack has already been created in the
206 		 * cht_max98090_headset_init() function.
207 		 */
208 		snd_soc_jack_notifier_register(jack, &cht_jack_nb);
209 		return 0;
210 	}
211 
212 	jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
213 
214 	ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
215 				    jack_type, jack,
216 				    hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
217 	if (ret) {
218 		dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
219 		return ret;
220 	}
221 
222 
223 	if (ctx->ts3a227e_present)
224 		snd_soc_jack_notifier_register(jack, &cht_jack_nb);
225 
226 	ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
227 				      ARRAY_SIZE(hs_jack_gpios),
228 				      hs_jack_gpios);
229 	if (ret) {
230 		/*
231 		 * flag error but don't bail if jack detect is broken
232 		 * due to platform issues or bad BIOS/configuration
233 		 */
234 		dev_err(runtime->dev,
235 			"jack detection gpios not added, error %d\n", ret);
236 	}
237 
238 	/*
239 	 * The firmware might enable the clock at
240 	 * boot (this information may or may not
241 	 * be reflected in the enable clock register).
242 	 * To change the rate we must disable the clock
243 	 * first to cover these cases. Due to common
244 	 * clock framework restrictions that do not allow
245 	 * to disable a clock that has not been enabled,
246 	 * we need to enable the clock first.
247 	 */
248 	ret = clk_prepare_enable(ctx->mclk);
249 	if (!ret)
250 		clk_disable_unprepare(ctx->mclk);
251 
252 	ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
253 
254 	if (ret)
255 		dev_err(runtime->dev, "unable to set MCLK rate\n");
256 
257 	return ret;
258 }
259 
260 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
261 			    struct snd_pcm_hw_params *params)
262 {
263 	struct snd_interval *rate = hw_param_interval(params,
264 			SNDRV_PCM_HW_PARAM_RATE);
265 	struct snd_interval *channels = hw_param_interval(params,
266 						SNDRV_PCM_HW_PARAM_CHANNELS);
267 	int ret = 0;
268 	unsigned int fmt = 0;
269 
270 	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
271 	if (ret < 0) {
272 		dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
273 		return ret;
274 	}
275 
276 	fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
277 				| SND_SOC_DAIFMT_CBS_CFS;
278 
279 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
280 	if (ret < 0) {
281 		dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
282 		return ret;
283 	}
284 
285 	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
286 	rate->min = rate->max = 48000;
287 	channels->min = channels->max = 2;
288 
289 	/* set SSP2 to 24-bit */
290 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
291 	return 0;
292 }
293 
294 static int cht_aif1_startup(struct snd_pcm_substream *substream)
295 {
296 	return snd_pcm_hw_constraint_single(substream->runtime,
297 			SNDRV_PCM_HW_PARAM_RATE, 48000);
298 }
299 
300 static int cht_max98090_headset_init(struct snd_soc_component *component)
301 {
302 	struct snd_soc_card *card = component->card;
303 	struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
304 	struct snd_soc_jack *jack = &ctx->jack;
305 	int jack_type;
306 	int ret;
307 
308 	/*
309 	 * TI supports 4 butons headset detection
310 	 * KEY_MEDIA
311 	 * KEY_VOICECOMMAND
312 	 * KEY_VOLUMEUP
313 	 * KEY_VOLUMEDOWN
314 	 */
315 	jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
316 		    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
317 		    SND_JACK_BTN_2 | SND_JACK_BTN_3;
318 
319 	ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
320 				    jack, NULL, 0);
321 	if (ret) {
322 		dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
323 		return ret;
324 	}
325 
326 	return ts3a227e_enable_jack_detect(component, &ctx->jack);
327 }
328 
329 static const struct snd_soc_ops cht_aif1_ops = {
330 	.startup = cht_aif1_startup,
331 };
332 
333 static const struct snd_soc_ops cht_be_ssp2_ops = {
334 	.hw_params = cht_aif1_hw_params,
335 };
336 
337 static struct snd_soc_aux_dev cht_max98090_headset_dev = {
338 	.name = "Headset Chip",
339 	.init = cht_max98090_headset_init,
340 	.codec_name = "i2c-104C227E:00",
341 };
342 
343 static struct snd_soc_dai_link cht_dailink[] = {
344 	[MERR_DPCM_AUDIO] = {
345 		.name = "Audio Port",
346 		.stream_name = "Audio",
347 		.cpu_dai_name = "media-cpu-dai",
348 		.codec_dai_name = "snd-soc-dummy-dai",
349 		.codec_name = "snd-soc-dummy",
350 		.platform_name = "sst-mfld-platform",
351 		.nonatomic = true,
352 		.dynamic = 1,
353 		.dpcm_playback = 1,
354 		.dpcm_capture = 1,
355 		.ops = &cht_aif1_ops,
356 	},
357 	[MERR_DPCM_DEEP_BUFFER] = {
358 		.name = "Deep-Buffer Audio Port",
359 		.stream_name = "Deep-Buffer Audio",
360 		.cpu_dai_name = "deepbuffer-cpu-dai",
361 		.codec_dai_name = "snd-soc-dummy-dai",
362 		.codec_name = "snd-soc-dummy",
363 		.platform_name = "sst-mfld-platform",
364 		.nonatomic = true,
365 		.dynamic = 1,
366 		.dpcm_playback = 1,
367 		.ops = &cht_aif1_ops,
368 	},
369 	[MERR_DPCM_COMPR] = {
370 		.name = "Compressed Port",
371 		.stream_name = "Compress",
372 		.cpu_dai_name = "compress-cpu-dai",
373 		.codec_dai_name = "snd-soc-dummy-dai",
374 		.codec_name = "snd-soc-dummy",
375 		.platform_name = "sst-mfld-platform",
376 	},
377 	/* back ends */
378 	{
379 		.name = "SSP2-Codec",
380 		.id = 1,
381 		.cpu_dai_name = "ssp2-port",
382 		.platform_name = "sst-mfld-platform",
383 		.no_pcm = 1,
384 		.codec_dai_name = "HiFi",
385 		.codec_name = "i2c-193C9890:00",
386 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
387 					| SND_SOC_DAIFMT_CBS_CFS,
388 		.init = cht_codec_init,
389 		.be_hw_params_fixup = cht_codec_fixup,
390 		.dpcm_playback = 1,
391 		.dpcm_capture = 1,
392 		.ops = &cht_be_ssp2_ops,
393 	},
394 };
395 
396 /* SoC card */
397 static struct snd_soc_card snd_soc_card_cht = {
398 	.name = "chtmax98090",
399 	.owner = THIS_MODULE,
400 	.dai_link = cht_dailink,
401 	.num_links = ARRAY_SIZE(cht_dailink),
402 	.aux_dev = &cht_max98090_headset_dev,
403 	.num_aux_devs = 1,
404 	.dapm_widgets = cht_dapm_widgets,
405 	.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
406 	.dapm_routes = cht_audio_map,
407 	.num_dapm_routes = ARRAY_SIZE(cht_audio_map),
408 	.controls = cht_mc_controls,
409 	.num_controls = ARRAY_SIZE(cht_mc_controls),
410 };
411 
412 static int snd_cht_mc_probe(struct platform_device *pdev)
413 {
414 	struct device *dev = &pdev->dev;
415 	int ret_val = 0;
416 	struct cht_mc_private *drv;
417 
418 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
419 	if (!drv)
420 		return -ENOMEM;
421 
422 	drv->ts3a227e_present = acpi_dev_found("104C227E");
423 	if (!drv->ts3a227e_present) {
424 		/* no need probe TI jack detection chip */
425 		snd_soc_card_cht.aux_dev = NULL;
426 		snd_soc_card_cht.num_aux_devs = 0;
427 
428 		ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
429 							 acpi_max98090_gpios);
430 		if (ret_val)
431 			dev_dbg(dev, "Unable to add GPIO mapping table\n");
432 	}
433 
434 	/* register the soc card */
435 	snd_soc_card_cht.dev = &pdev->dev;
436 	snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
437 
438 	drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
439 	if (IS_ERR(drv->mclk)) {
440 		dev_err(&pdev->dev,
441 			"Failed to get MCLK from pmc_plt_clk_3: %ld\n",
442 			PTR_ERR(drv->mclk));
443 		return PTR_ERR(drv->mclk);
444 	}
445 
446 	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
447 	if (ret_val) {
448 		dev_err(&pdev->dev,
449 			"snd_soc_register_card failed %d\n", ret_val);
450 		return ret_val;
451 	}
452 	platform_set_drvdata(pdev, &snd_soc_card_cht);
453 	return ret_val;
454 }
455 
456 static struct platform_driver snd_cht_mc_driver = {
457 	.driver = {
458 		.name = "cht-bsw-max98090",
459 	},
460 	.probe = snd_cht_mc_probe,
461 };
462 
463 module_platform_driver(snd_cht_mc_driver)
464 
465 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
466 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
467 MODULE_LICENSE("GPL v2");
468 MODULE_ALIAS("platform:cht-bsw-max98090");
469