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