1 // SPDX-License-Identifier: GPL-2.0
3 // Framer ALSA SoC driver
5 // Copyright 2023 CS GROUP France
7 // Author: Herve Codina <herve.codina@bootlin.com>
10 #include <linux/framer/framer.h>
11 #include <linux/module.h>
12 #include <linux/notifier.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <sound/jack.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/tlv.h>
20 #define FRAMER_NB_CHANNEL 32
21 #define FRAMER_JACK_MASK (SND_JACK_LINEIN | SND_JACK_LINEOUT)
24 struct framer *framer;
26 struct snd_soc_jack jack;
27 struct notifier_block nb;
28 struct work_struct carrier_work;
29 int max_chan_playback;
33 static int framer_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
34 unsigned int rx_mask, int slots, int width)
36 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
40 /* Not set -> default 8 */
44 dev_err(dai->dev, "tdm slot width %d not supported\n", width);
48 framer->max_chan_playback = hweight32(tx_mask);
49 if (framer->max_chan_playback > FRAMER_NB_CHANNEL) {
50 dev_err(dai->dev, "too many tx slots defined (mask = 0x%x) supported max %d\n",
51 tx_mask, FRAMER_NB_CHANNEL);
55 framer->max_chan_capture = hweight32(rx_mask);
56 if (framer->max_chan_capture > FRAMER_NB_CHANNEL) {
57 dev_err(dai->dev, "too many rx slots defined (mask = 0x%x) supported max %d\n",
58 rx_mask, FRAMER_NB_CHANNEL);
66 * The constraints for format/channel is to match with the number of 8bit
67 * time-slots available.
69 static int framer_dai_hw_rule_channels_by_format(struct snd_soc_dai *dai,
70 struct snd_pcm_hw_params *params,
73 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
74 snd_pcm_format_t format = params_format(params);
75 struct snd_interval ch = {0};
78 width = snd_pcm_format_physical_width(format);
79 if (width == 8 || width == 16 || width == 32 || width == 64) {
80 ch.max = nb_ts * 8 / width;
82 dev_err(dai->dev, "format physical width %d not supported\n", width);
86 ch.min = ch.max ? 1 : 0;
88 return snd_interval_refine(c, &ch);
91 static int framer_dai_hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
92 struct snd_pcm_hw_rule *rule)
94 struct snd_soc_dai *dai = rule->private;
95 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
97 return framer_dai_hw_rule_channels_by_format(dai, params, framer->max_chan_playback);
100 static int framer_dai_hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
101 struct snd_pcm_hw_rule *rule)
103 struct snd_soc_dai *dai = rule->private;
104 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
106 return framer_dai_hw_rule_channels_by_format(dai, params, framer->max_chan_capture);
109 static int framer_dai_hw_rule_format_by_channels(struct snd_soc_dai *dai,
110 struct snd_pcm_hw_params *params,
113 struct snd_mask *f_old = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
114 unsigned int channels = params_channels(params);
115 unsigned int slot_width;
116 snd_pcm_format_t format;
117 struct snd_mask f_new;
119 if (!channels || channels > nb_ts) {
120 dev_err(dai->dev, "channels %u not supported\n", nb_ts);
124 slot_width = (nb_ts / channels) * 8;
126 snd_mask_none(&f_new);
127 pcm_for_each_format(format) {
128 if (snd_mask_test_format(f_old, format)) {
129 if (snd_pcm_format_physical_width(format) <= slot_width)
130 snd_mask_set_format(&f_new, format);
134 return snd_mask_refine(f_old, &f_new);
137 static int framer_dai_hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
138 struct snd_pcm_hw_rule *rule)
140 struct snd_soc_dai *dai = rule->private;
141 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
143 return framer_dai_hw_rule_format_by_channels(dai, params, framer->max_chan_playback);
146 static int framer_dai_hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
147 struct snd_pcm_hw_rule *rule)
149 struct snd_soc_dai *dai = rule->private;
150 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
152 return framer_dai_hw_rule_format_by_channels(dai, params, framer->max_chan_capture);
155 static u64 framer_formats(u8 nb_ts)
157 unsigned int format_width;
158 unsigned int chan_width;
159 snd_pcm_format_t format;
166 chan_width = nb_ts * 8;
167 pcm_for_each_format(format) {
168 /* Support physical width multiple of 8bit */
169 format_width = snd_pcm_format_physical_width(format);
170 if (format_width == 0 || format_width % 8)
174 * And support physical width that can fit N times in the
177 if (format_width > chan_width || chan_width % format_width)
180 formats_mask |= pcm_format_to_bits(format);
185 static int framer_dai_startup(struct snd_pcm_substream *substream,
186 struct snd_soc_dai *dai)
188 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);
189 snd_pcm_hw_rule_func_t hw_rule_channels_by_format;
190 snd_pcm_hw_rule_func_t hw_rule_format_by_channels;
191 unsigned int frame_bits;
195 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
196 format = framer_formats(framer->max_chan_capture);
197 hw_rule_channels_by_format = framer_dai_hw_rule_capture_channels_by_format;
198 hw_rule_format_by_channels = framer_dai_hw_rule_capture_format_by_channels;
199 frame_bits = framer->max_chan_capture * 8;
201 format = framer_formats(framer->max_chan_playback);
202 hw_rule_channels_by_format = framer_dai_hw_rule_playback_channels_by_format;
203 hw_rule_format_by_channels = framer_dai_hw_rule_playback_format_by_channels;
204 frame_bits = framer->max_chan_playback * 8;
207 ret = snd_pcm_hw_constraint_mask64(substream->runtime,
208 SNDRV_PCM_HW_PARAM_FORMAT, format);
210 dev_err(dai->dev, "Failed to add format constraint (%d)\n", ret);
214 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
215 hw_rule_channels_by_format, dai,
216 SNDRV_PCM_HW_PARAM_FORMAT, -1);
218 dev_err(dai->dev, "Failed to add channels rule (%d)\n", ret);
222 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
223 hw_rule_format_by_channels, dai,
224 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
226 dev_err(dai->dev, "Failed to add format rule (%d)\n", ret);
230 ret = snd_pcm_hw_constraint_single(substream->runtime,
231 SNDRV_PCM_HW_PARAM_FRAME_BITS,
234 dev_err(dai->dev, "Failed to add frame_bits constraint (%d)\n", ret);
241 static u64 framer_dai_formats[] = {
242 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
245 static const struct snd_soc_dai_ops framer_dai_ops = {
246 .startup = framer_dai_startup,
247 .set_tdm_slot = framer_dai_set_tdm_slot,
248 .auto_selectable_formats = framer_dai_formats,
249 .num_auto_selectable_formats = ARRAY_SIZE(framer_dai_formats),
252 static struct snd_soc_dai_driver framer_dai_driver = {
255 .stream_name = "Playback",
257 .channels_max = FRAMER_NB_CHANNEL,
258 .rates = SNDRV_PCM_RATE_8000,
259 .formats = U64_MAX, /* Will be refined on DAI .startup() */
262 .stream_name = "Capture",
264 .channels_max = FRAMER_NB_CHANNEL,
265 .rates = SNDRV_PCM_RATE_8000,
266 .formats = U64_MAX, /* Will be refined on DAI .startup() */
268 .ops = &framer_dai_ops,
271 static void framer_carrier_work(struct work_struct *work)
273 struct framer_codec *framer = container_of(work, struct framer_codec, carrier_work);
274 struct framer_status framer_status;
278 ret = framer_get_status(framer->framer, &framer_status);
280 dev_err(framer->dev, "get framer status failed (%d)\n", ret);
284 jack_status = framer_status.link_is_on ? FRAMER_JACK_MASK : 0;
285 snd_soc_jack_report(&framer->jack, jack_status, FRAMER_JACK_MASK);
288 static int framer_carrier_notifier(struct notifier_block *nb, unsigned long action,
291 struct framer_codec *framer = container_of(nb, struct framer_codec, nb);
294 case FRAMER_EVENT_STATUS:
295 queue_work(system_power_efficient_wq, &framer->carrier_work);
304 static int framer_component_probe(struct snd_soc_component *component)
306 struct framer_codec *framer = snd_soc_component_get_drvdata(component);
307 struct framer_status status;
311 INIT_WORK(&framer->carrier_work, framer_carrier_work);
314 if (component->name_prefix) {
315 name = kasprintf(GFP_KERNEL, "%s carrier", component->name_prefix);
320 ret = snd_soc_card_jack_new(component->card, name, FRAMER_JACK_MASK, &framer->jack);
321 if (component->name_prefix)
322 kfree(name); /* A copy is done by snd_soc_card_jack_new */
324 dev_err(component->dev, "Cannot create jack\n");
328 ret = framer_init(framer->framer);
330 dev_err(component->dev, "framer init failed (%d)\n", ret);
334 ret = framer_power_on(framer->framer);
336 dev_err(component->dev, "framer power-on failed (%d)\n", ret);
340 /* Be sure that get_status is supported */
341 ret = framer_get_status(framer->framer, &status);
343 dev_err(component->dev, "get framer status failed (%d)\n", ret);
344 goto framer_power_off;
347 framer->nb.notifier_call = framer_carrier_notifier;
348 ret = framer_notifier_register(framer->framer, &framer->nb);
350 dev_err(component->dev, "Cannot register event notifier\n");
351 goto framer_power_off;
354 /* Queue work to set the initial value */
355 queue_work(system_power_efficient_wq, &framer->carrier_work);
360 framer_power_off(framer->framer);
362 framer_exit(framer->framer);
366 static void framer_component_remove(struct snd_soc_component *component)
368 struct framer_codec *framer = snd_soc_component_get_drvdata(component);
370 framer_notifier_unregister(framer->framer, &framer->nb);
371 cancel_work_sync(&framer->carrier_work);
372 framer_power_off(framer->framer);
373 framer_exit(framer->framer);
376 static const struct snd_soc_component_driver framer_component_driver = {
377 .probe = framer_component_probe,
378 .remove = framer_component_remove,
382 static int framer_codec_probe(struct platform_device *pdev)
384 struct framer_codec *framer;
386 framer = devm_kzalloc(&pdev->dev, sizeof(*framer), GFP_KERNEL);
390 framer->dev = &pdev->dev;
392 /* Get framer from parents node */
393 framer->framer = devm_framer_get(&pdev->dev, NULL);
394 if (IS_ERR(framer->framer))
395 return dev_err_probe(&pdev->dev, PTR_ERR(framer->framer), "get framer failed\n");
397 platform_set_drvdata(pdev, framer);
399 return devm_snd_soc_register_component(&pdev->dev, &framer_component_driver,
400 &framer_dai_driver, 1);
403 static struct platform_driver framer_codec_driver = {
405 .name = "framer-codec",
407 .probe = framer_codec_probe,
409 module_platform_driver(framer_codec_driver);
411 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
412 MODULE_DESCRIPTION("FRAMER ALSA SoC driver");
413 MODULE_LICENSE("GPL");