xref: /openbmc/linux/sound/soc/qcom/sc8280xp.c (revision 7587cdef)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022, Linaro Limited
3 
4 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/of_device.h>
7 #include <sound/soc.h>
8 #include <sound/soc-dapm.h>
9 #include <sound/pcm.h>
10 #include <linux/soundwire/sdw.h>
11 #include <sound/jack.h>
12 #include <linux/input-event-codes.h>
13 #include "qdsp6/q6afe.h"
14 #include "common.h"
15 
16 #define DRIVER_NAME		"sc8280xp"
17 
18 struct sc8280xp_snd_data {
19 	bool stream_prepared[AFE_PORT_MAX];
20 	struct snd_soc_card *card;
21 	struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
22 	struct snd_soc_jack jack;
23 	bool jack_setup;
24 };
25 
26 static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
27 {
28 	struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
29 
30 	return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
31 }
32 
33 static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
34 				     struct snd_pcm_hw_params *params)
35 {
36 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
37 	struct snd_interval *rate = hw_param_interval(params,
38 					SNDRV_PCM_HW_PARAM_RATE);
39 	struct snd_interval *channels = hw_param_interval(params,
40 					SNDRV_PCM_HW_PARAM_CHANNELS);
41 
42 	rate->min = rate->max = 48000;
43 	channels->min = 2;
44 	channels->max = 2;
45 	switch (cpu_dai->id) {
46 	case TX_CODEC_DMA_TX_0:
47 	case TX_CODEC_DMA_TX_1:
48 	case TX_CODEC_DMA_TX_2:
49 	case TX_CODEC_DMA_TX_3:
50 		channels->min = 1;
51 		break;
52 	default:
53 		break;
54 	}
55 
56 
57 	return 0;
58 }
59 
60 static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
61 				struct snd_pcm_hw_params *params)
62 {
63 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
65 	struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
66 
67 	return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
68 }
69 
70 static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
71 {
72 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
73 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
74 	struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
75 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
76 
77 	return qcom_snd_sdw_prepare(substream, sruntime,
78 				    &data->stream_prepared[cpu_dai->id]);
79 }
80 
81 static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
82 {
83 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
84 	struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
85 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
86 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
87 
88 	return qcom_snd_sdw_hw_free(substream, sruntime,
89 				    &data->stream_prepared[cpu_dai->id]);
90 }
91 
92 static const struct snd_soc_ops sc8280xp_be_ops = {
93 	.hw_params = sc8280xp_snd_hw_params,
94 	.hw_free = sc8280xp_snd_hw_free,
95 	.prepare = sc8280xp_snd_prepare,
96 };
97 
98 static void sc8280xp_add_be_ops(struct snd_soc_card *card)
99 {
100 	struct snd_soc_dai_link *link;
101 	int i;
102 
103 	for_each_card_prelinks(card, i, link) {
104 		if (link->no_pcm == 1) {
105 			link->init = sc8280xp_snd_init;
106 			link->be_hw_params_fixup = sc8280xp_be_hw_params_fixup;
107 			link->ops = &sc8280xp_be_ops;
108 		}
109 	}
110 }
111 
112 static int sc8280xp_platform_probe(struct platform_device *pdev)
113 {
114 	struct snd_soc_card *card;
115 	struct sc8280xp_snd_data *data;
116 	struct device *dev = &pdev->dev;
117 	int ret;
118 
119 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
120 	if (!card)
121 		return -ENOMEM;
122 	card->owner = THIS_MODULE;
123 	/* Allocate the private data */
124 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
125 	if (!data)
126 		return -ENOMEM;
127 
128 	card->dev = dev;
129 	dev_set_drvdata(dev, card);
130 	snd_soc_card_set_drvdata(card, data);
131 	ret = qcom_snd_parse_of(card);
132 	if (ret)
133 		return ret;
134 
135 	card->driver_name = DRIVER_NAME;
136 	sc8280xp_add_be_ops(card);
137 	return devm_snd_soc_register_card(dev, card);
138 }
139 
140 static const struct of_device_id snd_sc8280xp_dt_match[] = {
141 	{.compatible = "qcom,sc8280xp-sndcard",},
142 	{}
143 };
144 
145 MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
146 
147 static struct platform_driver snd_sc8280xp_driver = {
148 	.probe  = sc8280xp_platform_probe,
149 	.driver = {
150 		.name = "snd-sc8280xp",
151 		.of_match_table = snd_sc8280xp_dt_match,
152 	},
153 };
154 module_platform_driver(snd_sc8280xp_driver);
155 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
156 MODULE_DESCRIPTION("SC8280XP ASoC Machine Driver");
157 MODULE_LICENSE("GPL v2");
158