1/*
2 * Intel Broadwell Wildcatpoint SST Audio
3 *
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/soc.h>
22#include <sound/jack.h>
23#include <sound/pcm_params.h>
24
25#include "../common/sst-dsp.h"
26#include "../haswell/sst-haswell-ipc.h"
27
28#include "../../codecs/rt286.h"
29
30static struct snd_soc_jack broadwell_headset;
31/* Headset jack detection DAPM pins */
32static struct snd_soc_jack_pin broadwell_headset_pins[] = {
33	{
34		.pin = "Mic Jack",
35		.mask = SND_JACK_MICROPHONE,
36	},
37	{
38		.pin = "Headphone Jack",
39		.mask = SND_JACK_HEADPHONE,
40	},
41};
42
43static const struct snd_kcontrol_new broadwell_controls[] = {
44	SOC_DAPM_PIN_SWITCH("Speaker"),
45	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
46};
47
48static const struct snd_soc_dapm_widget broadwell_widgets[] = {
49	SND_SOC_DAPM_HP("Headphone Jack", NULL),
50	SND_SOC_DAPM_SPK("Speaker", NULL),
51	SND_SOC_DAPM_MIC("Mic Jack", NULL),
52	SND_SOC_DAPM_MIC("DMIC1", NULL),
53	SND_SOC_DAPM_MIC("DMIC2", NULL),
54	SND_SOC_DAPM_LINE("Line Jack", NULL),
55};
56
57static const struct snd_soc_dapm_route broadwell_rt286_map[] = {
58
59	/* speaker */
60	{"Speaker", NULL, "SPOR"},
61	{"Speaker", NULL, "SPOL"},
62
63	/* HP jack connectors - unknown if we have jack deteck */
64	{"Headphone Jack", NULL, "HPO Pin"},
65
66	/* other jacks */
67	{"MIC1", NULL, "Mic Jack"},
68	{"LINE1", NULL, "Line Jack"},
69
70	/* digital mics */
71	{"DMIC1 Pin", NULL, "DMIC1"},
72	{"DMIC2 Pin", NULL, "DMIC2"},
73
74	/* CODEC BE connections */
75	{"SSP0 CODEC IN", NULL, "AIF1 Capture"},
76	{"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
77};
78
79static int broadwell_rt286_codec_init(struct snd_soc_pcm_runtime *rtd)
80{
81	struct snd_soc_codec *codec = rtd->codec;
82	int ret = 0;
83	ret = snd_soc_card_jack_new(rtd->card, "Headset",
84		SND_JACK_HEADSET | SND_JACK_BTN_0, &broadwell_headset,
85		broadwell_headset_pins, ARRAY_SIZE(broadwell_headset_pins));
86	if (ret)
87		return ret;
88
89	rt286_mic_detect(codec, &broadwell_headset);
90	return 0;
91}
92
93
94static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
95			struct snd_pcm_hw_params *params)
96{
97	struct snd_interval *rate = hw_param_interval(params,
98			SNDRV_PCM_HW_PARAM_RATE);
99	struct snd_interval *channels = hw_param_interval(params,
100						SNDRV_PCM_HW_PARAM_CHANNELS);
101
102	/* The ADSP will covert the FE rate to 48k, stereo */
103	rate->min = rate->max = 48000;
104	channels->min = channels->max = 2;
105
106	/* set SSP0 to 16 bit */
107	params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
108	return 0;
109}
110
111static int broadwell_rt286_hw_params(struct snd_pcm_substream *substream,
112	struct snd_pcm_hw_params *params)
113{
114	struct snd_soc_pcm_runtime *rtd = substream->private_data;
115	struct snd_soc_dai *codec_dai = rtd->codec_dai;
116	int ret;
117
118	ret = snd_soc_dai_set_sysclk(codec_dai, RT286_SCLK_S_PLL, 24000000,
119		SND_SOC_CLOCK_IN);
120
121	if (ret < 0) {
122		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
123		return ret;
124	}
125
126	return ret;
127}
128
129static struct snd_soc_ops broadwell_rt286_ops = {
130	.hw_params = broadwell_rt286_hw_params,
131};
132
133static int broadwell_rtd_init(struct snd_soc_pcm_runtime *rtd)
134{
135	struct sst_pdata *pdata = dev_get_platdata(rtd->platform->dev);
136	struct sst_hsw *broadwell = pdata->dsp;
137	int ret;
138
139	/* Set ADSP SSP port settings */
140	ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
141		SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
142		SST_HSW_DEVICE_CLOCK_MASTER, 9);
143	if (ret < 0) {
144		dev_err(rtd->dev, "error: failed to set device config\n");
145		return ret;
146	}
147
148	return 0;
149}
150
151/* broadwell digital audio interface glue - connects codec <--> CPU */
152static struct snd_soc_dai_link broadwell_rt286_dais[] = {
153	/* Front End DAI links */
154	{
155		.name = "System PCM",
156		.stream_name = "System Playback/Capture",
157		.cpu_dai_name = "System Pin",
158		.platform_name = "haswell-pcm-audio",
159		.dynamic = 1,
160		.codec_name = "snd-soc-dummy",
161		.codec_dai_name = "snd-soc-dummy-dai",
162		.init = broadwell_rtd_init,
163		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
164		.dpcm_playback = 1,
165		.dpcm_capture = 1,
166	},
167	{
168		.name = "Offload0",
169		.stream_name = "Offload0 Playback",
170		.cpu_dai_name = "Offload0 Pin",
171		.platform_name = "haswell-pcm-audio",
172		.dynamic = 1,
173		.codec_name = "snd-soc-dummy",
174		.codec_dai_name = "snd-soc-dummy-dai",
175		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
176		.dpcm_playback = 1,
177	},
178	{
179		.name = "Offload1",
180		.stream_name = "Offload1 Playback",
181		.cpu_dai_name = "Offload1 Pin",
182		.platform_name = "haswell-pcm-audio",
183		.dynamic = 1,
184		.codec_name = "snd-soc-dummy",
185		.codec_dai_name = "snd-soc-dummy-dai",
186		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
187		.dpcm_playback = 1,
188	},
189	{
190		.name = "Loopback PCM",
191		.stream_name = "Loopback",
192		.cpu_dai_name = "Loopback Pin",
193		.platform_name = "haswell-pcm-audio",
194		.dynamic = 0,
195		.codec_name = "snd-soc-dummy",
196		.codec_dai_name = "snd-soc-dummy-dai",
197		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
198		.dpcm_capture = 1,
199	},
200	/* Back End DAI links */
201	{
202		/* SSP0 - Codec */
203		.name = "Codec",
204		.be_id = 0,
205		.cpu_dai_name = "snd-soc-dummy-dai",
206		.platform_name = "snd-soc-dummy",
207		.no_pcm = 1,
208		.codec_name = "i2c-INT343A:00",
209		.codec_dai_name = "rt286-aif1",
210		.init = broadwell_rt286_codec_init,
211		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
212			SND_SOC_DAIFMT_CBS_CFS,
213		.ignore_suspend = 1,
214		.ignore_pmdown_time = 1,
215		.be_hw_params_fixup = broadwell_ssp0_fixup,
216		.ops = &broadwell_rt286_ops,
217		.dpcm_playback = 1,
218		.dpcm_capture = 1,
219	},
220};
221
222static int broadwell_suspend(struct snd_soc_card *card){
223	struct snd_soc_codec *codec;
224
225	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
226		if (!strcmp(codec->component.name, "i2c-INT343A:00")) {
227			dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n");
228			rt286_mic_detect(codec, NULL);
229			break;
230		}
231	}
232	return 0;
233}
234
235static int broadwell_resume(struct snd_soc_card *card){
236	struct snd_soc_codec *codec;
237
238	list_for_each_entry(codec, &card->codec_dev_list, card_list) {
239		if (!strcmp(codec->component.name, "i2c-INT343A:00")) {
240			dev_dbg(codec->dev, "enabling jack detect for resume.\n");
241			rt286_mic_detect(codec, &broadwell_headset);
242			break;
243		}
244	}
245	return 0;
246}
247
248/* broadwell audio machine driver for WPT + RT286S */
249static struct snd_soc_card broadwell_rt286 = {
250	.name = "broadwell-rt286",
251	.owner = THIS_MODULE,
252	.dai_link = broadwell_rt286_dais,
253	.num_links = ARRAY_SIZE(broadwell_rt286_dais),
254	.controls = broadwell_controls,
255	.num_controls = ARRAY_SIZE(broadwell_controls),
256	.dapm_widgets = broadwell_widgets,
257	.num_dapm_widgets = ARRAY_SIZE(broadwell_widgets),
258	.dapm_routes = broadwell_rt286_map,
259	.num_dapm_routes = ARRAY_SIZE(broadwell_rt286_map),
260	.fully_routed = true,
261	.suspend_pre = broadwell_suspend,
262	.resume_post = broadwell_resume,
263};
264
265static int broadwell_audio_probe(struct platform_device *pdev)
266{
267	broadwell_rt286.dev = &pdev->dev;
268
269	return snd_soc_register_card(&broadwell_rt286);
270}
271
272static int broadwell_audio_remove(struct platform_device *pdev)
273{
274	snd_soc_unregister_card(&broadwell_rt286);
275	return 0;
276}
277
278static struct platform_driver broadwell_audio = {
279	.probe = broadwell_audio_probe,
280	.remove = broadwell_audio_remove,
281	.driver = {
282		.name = "broadwell-audio",
283	},
284};
285
286module_platform_driver(broadwell_audio)
287
288/* Module information */
289MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
290MODULE_DESCRIPTION("Intel SST Audio for WPT/Broadwell");
291MODULE_LICENSE("GPL v2");
292MODULE_ALIAS("platform:broadwell-audio");
293