1/*
2 *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
3 *
4 *  Copyright (C) 2014 Intel Corp
5 *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; version 2 of the License.
11 *
12 *  This program is distributed in the hope that it will be useful, but
13 *  WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 *  General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/device.h>
24#include <linux/slab.h>
25#include <linux/input.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
29#include "../../codecs/rt5640.h"
30#include "../atom/sst-atom-controls.h"
31
32static const struct snd_soc_dapm_widget byt_dapm_widgets[] = {
33	SND_SOC_DAPM_HP("Headphone", NULL),
34	SND_SOC_DAPM_MIC("Headset Mic", NULL),
35	SND_SOC_DAPM_MIC("Int Mic", NULL),
36	SND_SOC_DAPM_SPK("Ext Spk", NULL),
37};
38
39static const struct snd_soc_dapm_route byt_audio_map[] = {
40	{"IN2P", NULL, "Headset Mic"},
41	{"IN2N", NULL, "Headset Mic"},
42	{"Headset Mic", NULL, "MICBIAS1"},
43	{"IN1P", NULL, "MICBIAS1"},
44	{"LDO2", NULL, "Int Mic"},
45	{"Headphone", NULL, "HPOL"},
46	{"Headphone", NULL, "HPOR"},
47	{"Ext Spk", NULL, "SPOLP"},
48	{"Ext Spk", NULL, "SPOLN"},
49	{"Ext Spk", NULL, "SPORP"},
50	{"Ext Spk", NULL, "SPORN"},
51
52	{"AIF1 Playback", NULL, "ssp2 Tx"},
53	{"ssp2 Tx", NULL, "codec_out0"},
54	{"ssp2 Tx", NULL, "codec_out1"},
55	{"codec_in0", NULL, "ssp2 Rx"},
56	{"codec_in1", NULL, "ssp2 Rx"},
57	{"ssp2 Rx", NULL, "AIF1 Capture"},
58};
59
60static const struct snd_kcontrol_new byt_mc_controls[] = {
61	SOC_DAPM_PIN_SWITCH("Headphone"),
62	SOC_DAPM_PIN_SWITCH("Headset Mic"),
63	SOC_DAPM_PIN_SWITCH("Int Mic"),
64	SOC_DAPM_PIN_SWITCH("Ext Spk"),
65};
66
67static int byt_aif1_hw_params(struct snd_pcm_substream *substream,
68					struct snd_pcm_hw_params *params)
69{
70	struct snd_soc_pcm_runtime *rtd = substream->private_data;
71	struct snd_soc_dai *codec_dai = rtd->codec_dai;
72	int ret;
73
74	snd_soc_dai_set_bclk_ratio(codec_dai, 50);
75
76	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
77				     params_rate(params) * 512,
78				     SND_SOC_CLOCK_IN);
79	if (ret < 0) {
80		dev_err(rtd->dev, "can't set codec clock %d\n", ret);
81		return ret;
82	}
83
84	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1,
85				  params_rate(params) * 50,
86				  params_rate(params) * 512);
87	if (ret < 0) {
88		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
89		return ret;
90	}
91
92	return 0;
93}
94
95static const struct snd_soc_pcm_stream byt_dai_params = {
96	.formats = SNDRV_PCM_FMTBIT_S24_LE,
97	.rate_min = 48000,
98	.rate_max = 48000,
99	.channels_min = 2,
100	.channels_max = 2,
101};
102
103static int byt_codec_fixup(struct snd_soc_pcm_runtime *rtd,
104			    struct snd_pcm_hw_params *params)
105{
106	struct snd_interval *rate = hw_param_interval(params,
107			SNDRV_PCM_HW_PARAM_RATE);
108	struct snd_interval *channels = hw_param_interval(params,
109						SNDRV_PCM_HW_PARAM_CHANNELS);
110
111	/* The DSP will covert the FE rate to 48k, stereo, 24bits */
112	rate->min = rate->max = 48000;
113	channels->min = channels->max = 2;
114
115	/* set SSP2 to 24-bit */
116	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
117	return 0;
118}
119
120static int byt_aif1_startup(struct snd_pcm_substream *substream)
121{
122	return snd_pcm_hw_constraint_single(substream->runtime,
123			SNDRV_PCM_HW_PARAM_RATE, 48000);
124}
125
126static struct snd_soc_ops byt_aif1_ops = {
127	.startup = byt_aif1_startup,
128};
129
130static struct snd_soc_ops byt_be_ssp2_ops = {
131	.hw_params = byt_aif1_hw_params,
132};
133
134static struct snd_soc_dai_link byt_dailink[] = {
135	[MERR_DPCM_AUDIO] = {
136		.name = "Baytrail Audio Port",
137		.stream_name = "Baytrail Audio",
138		.cpu_dai_name = "media-cpu-dai",
139		.codec_dai_name = "snd-soc-dummy-dai",
140		.codec_name = "snd-soc-dummy",
141		.platform_name = "sst-mfld-platform",
142		.ignore_suspend = 1,
143		.dynamic = 1,
144		.dpcm_playback = 1,
145		.dpcm_capture = 1,
146		.ops = &byt_aif1_ops,
147	},
148	[MERR_DPCM_COMPR] = {
149		.name = "Baytrail Compressed Port",
150		.stream_name = "Baytrail Compress",
151		.cpu_dai_name = "compress-cpu-dai",
152		.codec_dai_name = "snd-soc-dummy-dai",
153		.codec_name = "snd-soc-dummy",
154		.platform_name = "sst-mfld-platform",
155	},
156		/* back ends */
157	{
158		.name = "SSP2-Codec",
159		.be_id = 1,
160		.cpu_dai_name = "ssp2-port",
161		.platform_name = "sst-mfld-platform",
162		.no_pcm = 1,
163		.codec_dai_name = "rt5640-aif1",
164		.codec_name = "i2c-10EC5640:00",
165		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
166						| SND_SOC_DAIFMT_CBS_CFS,
167		.be_hw_params_fixup = byt_codec_fixup,
168		.ignore_suspend = 1,
169		.dpcm_playback = 1,
170		.dpcm_capture = 1,
171		.ops = &byt_be_ssp2_ops,
172	},
173};
174
175/* SoC card */
176static struct snd_soc_card snd_soc_card_byt = {
177	.name = "baytrailcraudio",
178	.owner = THIS_MODULE,
179	.dai_link = byt_dailink,
180	.num_links = ARRAY_SIZE(byt_dailink),
181	.dapm_widgets = byt_dapm_widgets,
182	.num_dapm_widgets = ARRAY_SIZE(byt_dapm_widgets),
183	.dapm_routes = byt_audio_map,
184	.num_dapm_routes = ARRAY_SIZE(byt_audio_map),
185	.controls = byt_mc_controls,
186	.num_controls = ARRAY_SIZE(byt_mc_controls),
187};
188
189static int snd_byt_mc_probe(struct platform_device *pdev)
190{
191	int ret_val = 0;
192
193	/* register the soc card */
194	snd_soc_card_byt.dev = &pdev->dev;
195
196	ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_byt);
197	if (ret_val) {
198		dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", ret_val);
199		return ret_val;
200	}
201	platform_set_drvdata(pdev, &snd_soc_card_byt);
202	return ret_val;
203}
204
205static struct platform_driver snd_byt_mc_driver = {
206	.driver = {
207		.name = "bytt100_rt5640",
208		.pm = &snd_soc_pm_ops,
209	},
210	.probe = snd_byt_mc_probe,
211};
212
213module_platform_driver(snd_byt_mc_driver);
214
215MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
216MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
217MODULE_LICENSE("GPL v2");
218MODULE_ALIAS("platform:bytt100_rt5640");
219