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