GNU Linux-libre 6.9.1-gnu
[releases.git] / sound / soc / codecs / cs42l43-sdw.c
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 <linux/soundwire/sdw.h>
13 #include <sound/pcm.h>
14 #include <sound/sdw.h>
15 #include <sound/soc-component.h>
16 #include <sound/soc-dai.h>
17 #include <sound/soc.h>
18
19 #include "cs42l43.h"
20
21 int cs42l43_sdw_add_peripheral(struct snd_pcm_substream *substream,
22                                struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
23 {
24         struct cs42l43_codec *priv = snd_soc_component_get_drvdata(dai->component);
25         struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
26         struct sdw_slave *sdw = dev_to_sdw_dev(priv->dev->parent);
27         struct sdw_stream_config sconfig = {0};
28         struct sdw_port_config pconfig = {0};
29         int ret;
30
31         if (!sdw_stream)
32                 return -EINVAL;
33
34         snd_sdw_params_to_config(substream, params, &sconfig, &pconfig);
35         pconfig.num = dai->id;
36
37         ret = sdw_stream_add_slave(sdw, &sconfig, &pconfig, 1, sdw_stream);
38         if (ret) {
39                 dev_err(priv->dev, "Failed to add sdw stream: %d\n", ret);
40                 return ret;
41         }
42
43         return 0;
44 }
45 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_add_peripheral, SND_SOC_CS42L43);
46
47 int cs42l43_sdw_remove_peripheral(struct snd_pcm_substream *substream,
48                                   struct snd_soc_dai *dai)
49 {
50         struct cs42l43_codec *priv = snd_soc_component_get_drvdata(dai->component);
51         struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
52         struct sdw_slave *sdw = dev_to_sdw_dev(priv->dev->parent);
53
54         if (!sdw_stream)
55                 return -EINVAL;
56
57         return sdw_stream_remove_slave(sdw, sdw_stream);
58 }
59 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_remove_peripheral, SND_SOC_CS42L43);
60
61 int cs42l43_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stream, int direction)
62 {
63         snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
64
65         return 0;
66 }
67 EXPORT_SYMBOL_NS_GPL(cs42l43_sdw_set_stream, SND_SOC_CS42L43);
68
69 MODULE_DESCRIPTION("CS42L43 CODEC SoundWire Driver");
70 MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
71 MODULE_LICENSE("GPL");