summaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/lochnagar-sc.c
blob: 5e0bd0d24ed3709343a07fa8a433dce565d49198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// SPDX-License-Identifier: GPL-2.0
//
// Lochnagar sound card driver
//
// Copyright (c) 2017-2019 Cirrus Logic, Inc. and
//                         Cirrus Logic International Semiconductor Ltd.
//
// Author: Charles Keepax <ckeepax@opensource.cirrus.com>
//         Piotr Stankiewicz <piotrs@opensource.cirrus.com>

#include <linux/clk.h>
#include <linux/module.h>
#include <sound/soc.h>

#include <linux/mfd/lochnagar.h>
#include <linux/mfd/lochnagar1_regs.h>
#include <linux/mfd/lochnagar2_regs.h>

struct lochnagar_sc_priv {
	struct clk *mclk;
};

static const struct snd_soc_dapm_widget lochnagar_sc_widgets[] = {
	SND_SOC_DAPM_LINE("Line Jack", NULL),
	SND_SOC_DAPM_LINE("USB Audio", NULL),
};

static const struct snd_soc_dapm_route lochnagar_sc_routes[] = {
	{ "Line Jack", NULL, "AIF1 Playback" },
	{ "AIF1 Capture", NULL, "Line Jack" },

	{ "USB Audio", NULL, "USB1 Playback" },
	{ "USB Audio", NULL, "USB2 Playback" },
	{ "USB1 Capture", NULL, "USB Audio" },
	{ "USB2 Capture", NULL, "USB Audio" },
};

static const unsigned int lochnagar_sc_chan_vals[] = {
	4, 8,
};

static const struct snd_pcm_hw_constraint_list lochnagar_sc_chan_constraint = {
	.count = ARRAY_SIZE(lochnagar_sc_chan_vals),
	.list = lochnagar_sc_chan_vals,
};

static const unsigned int lochnagar_sc_rate_vals[] = {
	8000, 16000, 24000, 32000, 48000, 96000, 192000,
	22050, 44100, 88200, 176400,
};

static const struct snd_pcm_hw_constraint_list lochnagar_sc_rate_constraint = {
	.count = ARRAY_SIZE(lochnagar_sc_rate_vals),
	.list = lochnagar_sc_rate_vals,
};

static int lochnagar_sc_hw_rule_rate(struct snd_pcm_hw_params *params,
				     struct snd_pcm_hw_rule *rule)
{
	struct snd_interval range = {
		.min = 8000,
		.max = 24576000 / hw_param_interval(params, rule->deps[0])->max,
	};

	return snd_interval_refine(hw_param_interval(params, rule->var),
				   &range);
}

static int lochnagar_sc_startup(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	struct snd_soc_component *comp = dai->component;
	struct lochnagar_sc_priv *priv = snd_soc_component_get_drvdata(comp);
	int ret;

	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
					 SNDRV_PCM_HW_PARAM_RATE,
					 &lochnagar_sc_rate_constraint);
	if (ret)
		return ret;

	return snd_pcm_hw_rule_add(substream->runtime, 0,
				   SNDRV_PCM_HW_PARAM_RATE,
				   lochnagar_sc_hw_rule_rate, priv,
				   SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
}

static int lochnagar_sc_line_startup(struct snd_pcm_substream *substream,
				     struct snd_soc_dai *dai)
{
	struct snd_soc_component *comp = dai->component;
	struct lochnagar_sc_priv *priv = snd_soc_component_get_drvdata(comp);
	int ret;

	ret = clk_prepare_enable(priv->mclk);
	if (ret < 0) {
		dev_err(dai->dev, "Failed to enable MCLK: %d\n", ret);
		return ret;
	}

	ret = lochnagar_sc_startup(substream, dai);
	if (ret)
		return ret;

	return snd_pcm_hw_constraint_list(substream->runtime, 0,
					  SNDRV_PCM_HW_PARAM_CHANNELS,
					  &lochnagar_sc_chan_constraint);
}

static void lochnagar_sc_line_shutdown(struct snd_pcm_substream *substream,
				       struct snd_soc_dai *dai)
{
	struct snd_soc_component *comp = dai->component;
	struct lochnagar_sc_priv *priv = snd_soc_component_get_drvdata(comp);

	clk_disable_unprepare(priv->mclk);
}

static int lochnagar_sc_check_fmt(struct snd_soc_dai *dai, unsigned int fmt,
				  unsigned int tar)
{
	tar |= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF;

	if ((fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) != tar)
		return -EINVAL;

	return 0;
}

static int lochnagar_sc_set_line_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	return lochnagar_sc_check_fmt(dai, fmt, SND_SOC_DAIFMT_CBS_CFS);
}

static int lochnagar_sc_set_usb_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	return lochnagar_sc_check_fmt(dai, fmt, SND_SOC_DAIFMT_CBM_CFM);
}

static const struct snd_soc_dai_ops lochnagar_sc_line_ops = {
	.startup = lochnagar_sc_line_startup,
	.shutdown = lochnagar_sc_line_shutdown,
	.set_fmt = lochnagar_sc_set_line_fmt,
};

static const struct snd_soc_dai_ops lochnagar_sc_usb_ops = {
	.startup = lochnagar_sc_startup,
	.set_fmt = lochnagar_sc_set_usb_fmt,
};

static struct snd_soc_dai_driver lochnagar_sc_dai[] = {
	{
		.name = "lochnagar-line",
		.playback = {
			.stream_name = "AIF1 Playback",
			.channels_min = 4,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.capture = {
			.stream_name = "AIF1 Capture",
			.channels_min = 4,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.ops = &lochnagar_sc_line_ops,
		.symmetric_rate = true,
		.symmetric_sample_bits = true,
	},
	{
		.name = "lochnagar-usb1",
		.playback = {
			.stream_name = "USB1 Playback",
			.channels_min = 1,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.capture = {
			.stream_name = "USB1 Capture",
			.channels_min = 1,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.ops = &lochnagar_sc_usb_ops,
		.symmetric_rate = true,
		.symmetric_sample_bits = true,
	},
	{
		.name = "lochnagar-usb2",
		.playback = {
			.stream_name = "USB2 Playback",
			.channels_min = 1,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.capture = {
			.stream_name = "USB2 Capture",
			.channels_min = 1,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_KNOT,
			.formats = SNDRV_PCM_FMTBIT_S32_LE,
		},
		.ops = &lochnagar_sc_usb_ops,
		.symmetric_rate = true,
		.symmetric_sample_bits = true,
	},
};

static const struct snd_soc_component_driver lochnagar_sc_driver = {
	.dapm_widgets = lochnagar_sc_widgets,
	.num_dapm_widgets = ARRAY_SIZE(lochnagar_sc_widgets),
	.dapm_routes = lochnagar_sc_routes,
	.num_dapm_routes = ARRAY_SIZE(lochnagar_sc_routes),

	.endianness = 1,
};

static int lochnagar_sc_probe(struct platform_device *pdev)
{
	struct lochnagar_sc_priv *priv;
	int ret;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->mclk = devm_clk_get(&pdev->dev, "mclk");
	if (IS_ERR(priv->mclk)) {
		ret = PTR_ERR(priv->mclk);
		dev_err(&pdev->dev, "Failed to get MCLK: %d\n", ret);
		return ret;
	}

	platform_set_drvdata(pdev, priv);

	return devm_snd_soc_register_component(&pdev->dev,
					       &lochnagar_sc_driver,
					       lochnagar_sc_dai,
					       ARRAY_SIZE(lochnagar_sc_dai));
}

static const struct of_device_id lochnagar_of_match[] = {
	{ .compatible = "cirrus,lochnagar2-soundcard" },
	{}
};
MODULE_DEVICE_TABLE(of, lochnagar_of_match);

static struct platform_driver lochnagar_sc_codec_driver = {
	.driver = {
		.name = "lochnagar-soundcard",
		.of_match_table = lochnagar_of_match,
	},

	.probe = lochnagar_sc_probe,
};
module_platform_driver(lochnagar_sc_codec_driver);

MODULE_DESCRIPTION("ASoC Lochnagar Sound Card Driver");
MODULE_AUTHOR("Piotr Stankiewicz <piotrs@opensource.cirrus.com>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:lochnagar-soundcard");