1 // SPDX-License-Identifier: GPL-2.0
3 // Lochnagar sound card driver
5 // Copyright (c) 2017-2019 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
8 // Author: Charles Keepax <ckeepax@opensource.cirrus.com>
9 // Piotr Stankiewicz <piotrs@opensource.cirrus.com>
11 #include <linux/clk.h>
12 #include <linux/module.h>
13 #include <sound/soc.h>
15 #include <linux/mfd/lochnagar.h>
16 #include <linux/mfd/lochnagar1_regs.h>
17 #include <linux/mfd/lochnagar2_regs.h>
19 struct lochnagar_sc_priv
{
23 static const struct snd_soc_dapm_widget lochnagar_sc_widgets
[] = {
24 SND_SOC_DAPM_LINE("Line Jack", NULL
),
25 SND_SOC_DAPM_LINE("USB Audio", NULL
),
28 static const struct snd_soc_dapm_route lochnagar_sc_routes
[] = {
29 { "Line Jack", NULL
, "AIF1 Playback" },
30 { "AIF1 Capture", NULL
, "Line Jack" },
32 { "USB Audio", NULL
, "USB1 Playback" },
33 { "USB Audio", NULL
, "USB2 Playback" },
34 { "USB1 Capture", NULL
, "USB Audio" },
35 { "USB2 Capture", NULL
, "USB Audio" },
38 static const unsigned int lochnagar_sc_chan_vals
[] = {
42 static const struct snd_pcm_hw_constraint_list lochnagar_sc_chan_constraint
= {
43 .count
= ARRAY_SIZE(lochnagar_sc_chan_vals
),
44 .list
= lochnagar_sc_chan_vals
,
47 static const unsigned int lochnagar_sc_rate_vals
[] = {
48 8000, 16000, 24000, 32000, 48000, 96000, 192000,
49 22050, 44100, 88200, 176400,
52 static const struct snd_pcm_hw_constraint_list lochnagar_sc_rate_constraint
= {
53 .count
= ARRAY_SIZE(lochnagar_sc_rate_vals
),
54 .list
= lochnagar_sc_rate_vals
,
57 static int lochnagar_sc_hw_rule_rate(struct snd_pcm_hw_params
*params
,
58 struct snd_pcm_hw_rule
*rule
)
60 struct snd_interval range
= {
62 .max
= 24576000 / hw_param_interval(params
, rule
->deps
[0])->max
,
65 return snd_interval_refine(hw_param_interval(params
, rule
->var
),
69 static int lochnagar_sc_startup(struct snd_pcm_substream
*substream
,
70 struct snd_soc_dai
*dai
)
72 struct snd_soc_component
*comp
= dai
->component
;
73 struct lochnagar_sc_priv
*priv
= snd_soc_component_get_drvdata(comp
);
76 ret
= snd_pcm_hw_constraint_list(substream
->runtime
, 0,
77 SNDRV_PCM_HW_PARAM_RATE
,
78 &lochnagar_sc_rate_constraint
);
82 return snd_pcm_hw_rule_add(substream
->runtime
, 0,
83 SNDRV_PCM_HW_PARAM_RATE
,
84 lochnagar_sc_hw_rule_rate
, priv
,
85 SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
88 static int lochnagar_sc_line_startup(struct snd_pcm_substream
*substream
,
89 struct snd_soc_dai
*dai
)
91 struct snd_soc_component
*comp
= dai
->component
;
92 struct lochnagar_sc_priv
*priv
= snd_soc_component_get_drvdata(comp
);
95 ret
= clk_prepare_enable(priv
->mclk
);
97 dev_err(dai
->dev
, "Failed to enable MCLK: %d\n", ret
);
101 ret
= lochnagar_sc_startup(substream
, dai
);
105 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
106 SNDRV_PCM_HW_PARAM_CHANNELS
,
107 &lochnagar_sc_chan_constraint
);
110 static void lochnagar_sc_line_shutdown(struct snd_pcm_substream
*substream
,
111 struct snd_soc_dai
*dai
)
113 struct snd_soc_component
*comp
= dai
->component
;
114 struct lochnagar_sc_priv
*priv
= snd_soc_component_get_drvdata(comp
);
116 clk_disable_unprepare(priv
->mclk
);
119 static int lochnagar_sc_check_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
,
122 tar
|= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
;
124 if ((fmt
& ~SND_SOC_DAIFMT_CLOCK_MASK
) != tar
)
130 static int lochnagar_sc_set_line_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
132 return lochnagar_sc_check_fmt(dai
, fmt
, SND_SOC_DAIFMT_CBS_CFS
);
135 static int lochnagar_sc_set_usb_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
137 return lochnagar_sc_check_fmt(dai
, fmt
, SND_SOC_DAIFMT_CBM_CFM
);
140 static const struct snd_soc_dai_ops lochnagar_sc_line_ops
= {
141 .startup
= lochnagar_sc_line_startup
,
142 .shutdown
= lochnagar_sc_line_shutdown
,
143 .set_fmt
= lochnagar_sc_set_line_fmt
,
146 static const struct snd_soc_dai_ops lochnagar_sc_usb_ops
= {
147 .startup
= lochnagar_sc_startup
,
148 .set_fmt
= lochnagar_sc_set_usb_fmt
,
151 static struct snd_soc_dai_driver lochnagar_sc_dai
[] = {
153 .name
= "lochnagar-line",
155 .stream_name
= "AIF1 Playback",
158 .rates
= SNDRV_PCM_RATE_KNOT
,
159 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
162 .stream_name
= "AIF1 Capture",
165 .rates
= SNDRV_PCM_RATE_KNOT
,
166 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
168 .ops
= &lochnagar_sc_line_ops
,
169 .symmetric_rates
= true,
170 .symmetric_samplebits
= true,
173 .name
= "lochnagar-usb1",
175 .stream_name
= "USB1 Playback",
178 .rates
= SNDRV_PCM_RATE_KNOT
,
179 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
182 .stream_name
= "USB1 Capture",
185 .rates
= SNDRV_PCM_RATE_KNOT
,
186 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
188 .ops
= &lochnagar_sc_usb_ops
,
189 .symmetric_rates
= true,
190 .symmetric_samplebits
= true,
193 .name
= "lochnagar-usb2",
195 .stream_name
= "USB2 Playback",
198 .rates
= SNDRV_PCM_RATE_KNOT
,
199 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
202 .stream_name
= "USB2 Capture",
205 .rates
= SNDRV_PCM_RATE_KNOT
,
206 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
208 .ops
= &lochnagar_sc_usb_ops
,
209 .symmetric_rates
= true,
210 .symmetric_samplebits
= true,
214 static const struct snd_soc_component_driver lochnagar_sc_driver
= {
215 .non_legacy_dai_naming
= 1,
217 .dapm_widgets
= lochnagar_sc_widgets
,
218 .num_dapm_widgets
= ARRAY_SIZE(lochnagar_sc_widgets
),
219 .dapm_routes
= lochnagar_sc_routes
,
220 .num_dapm_routes
= ARRAY_SIZE(lochnagar_sc_routes
),
223 static int lochnagar_sc_probe(struct platform_device
*pdev
)
225 struct lochnagar_sc_priv
*priv
;
228 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
232 priv
->mclk
= devm_clk_get(&pdev
->dev
, "mclk");
233 if (IS_ERR(priv
->mclk
)) {
234 ret
= PTR_ERR(priv
->mclk
);
235 dev_err(&pdev
->dev
, "Failed to get MCLK: %d\n", ret
);
239 platform_set_drvdata(pdev
, priv
);
241 return devm_snd_soc_register_component(&pdev
->dev
,
242 &lochnagar_sc_driver
,
244 ARRAY_SIZE(lochnagar_sc_dai
));
247 static const struct of_device_id lochnagar_of_match
[] = {
248 { .compatible
= "cirrus,lochnagar2-soundcard" },
251 MODULE_DEVICE_TABLE(of
, lochnagar_of_match
);
253 static struct platform_driver lochnagar_sc_codec_driver
= {
255 .name
= "lochnagar-soundcard",
256 .of_match_table
= of_match_ptr(lochnagar_of_match
),
259 .probe
= lochnagar_sc_probe
,
261 module_platform_driver(lochnagar_sc_codec_driver
);
263 MODULE_DESCRIPTION("ASoC Lochnagar Sound Card Driver");
264 MODULE_AUTHOR("Piotr Stankiewicz <piotrs@opensource.cirrus.com>");
265 MODULE_LICENSE("GPL v2");
266 MODULE_ALIAS("platform:lochnagar-soundcard");