xref: /openbmc/linux/sound/soc/codecs/cs42l43-sdw.c (revision 3ddc8b84)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // CS42L43 CODEC driver SoundWire handling
4 //
5 // Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6 //                         Cirrus Logic International Semiconductor Ltd.
7 
8 #include <linux/errno.h>
9 #include <linux/mfd/cs42l43.h>
10 #include <linux/mfd/cs42l43-regs.h>
11 #include <linux/module.h>
12 #include <sound/pcm.h>
13 #include <sound/sdw.h>
14 #include <sound/soc-component.h>
15 #include <sound/soc-dai.h>
16 #include <sound/soc.h>
17 
18 #include "cs42l43.h"
19 
20 int cs42l43_sdw_add_peripheral(struct snd_pcm_substream *substream,
21 			       struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
22 {
23 	struct cs42l43_codec *priv = snd_soc_component_get_drvdata(dai->component);
24 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
25 	struct sdw_slave *sdw = dev_to_sdw_dev(priv->dev->parent);
26 	struct sdw_stream_config sconfig = {0};
27 	struct sdw_port_config pconfig = {0};
28 	int ret;
29 
30 	if (!sdw_stream)
31 		return -EINVAL;
32 
33 	snd_sdw_params_to_config(substream, params, &sconfig, &pconfig);
34 
35 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
36 		pconfig.num = dai->id;
37 	else
38 		pconfig.num = dai->id;
39 
40 	ret = sdw_stream_add_slave(sdw, &sconfig, &pconfig, 1, sdw_stream);
41 	if (ret) {
42 		dev_err(priv->dev, "Failed to add sdw stream: %d\n", ret);
43 		return ret;
44 	}
45 
46 	return 0;
47 }
48 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_add_peripheral, SND_SOC_CS42L43);
49 
50 int cs42l43_sdw_remove_peripheral(struct snd_pcm_substream *substream,
51 				  struct snd_soc_dai *dai)
52 {
53 	struct cs42l43_codec *priv = snd_soc_component_get_drvdata(dai->component);
54 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
55 	struct sdw_slave *sdw = dev_to_sdw_dev(priv->dev->parent);
56 
57 	if (!sdw_stream)
58 		return -EINVAL;
59 
60 	return sdw_stream_remove_slave(sdw, sdw_stream);
61 }
62 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_remove_peripheral, SND_SOC_CS42L43);
63 
64 int cs42l43_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stream, int direction)
65 {
66 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
67 
68 	return 0;
69 }
70 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_set_stream, SND_SOC_CS42L43);
71 
72 MODULE_DESCRIPTION("CS42L43 CODEC SoundWire Driver");
73 MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
74 MODULE_LICENSE("GPL");
75