1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, 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 <sound/pcm_params.h>
11 #include <linux/soundwire/sdw.h>
12 #include <sound/jack.h>
13 #include <linux/input-event-codes.h>
14 #include "qdsp6/q6afe.h"
15 #include "common.h"
16 #include "sdw.h"
17
18 #define DRIVER_NAME "sm8250"
19 #define MI2S_BCLK_RATE 1536000
20
21 struct sm8250_snd_data {
22 bool stream_prepared[AFE_PORT_MAX];
23 struct snd_soc_card *card;
24 struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
25 struct snd_soc_jack jack;
26 bool jack_setup;
27 };
28
sm8250_snd_init(struct snd_soc_pcm_runtime * rtd)29 static int sm8250_snd_init(struct snd_soc_pcm_runtime *rtd)
30 {
31 struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
32
33 return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
34 }
35
sm8250_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)36 static int sm8250_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
37 struct snd_pcm_hw_params *params)
38 {
39 struct snd_interval *rate = hw_param_interval(params,
40 SNDRV_PCM_HW_PARAM_RATE);
41 struct snd_interval *channels = hw_param_interval(params,
42 SNDRV_PCM_HW_PARAM_CHANNELS);
43 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
44
45 rate->min = rate->max = 48000;
46 channels->min = channels->max = 2;
47 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
48
49 return 0;
50 }
51
sm8250_snd_startup(struct snd_pcm_substream * substream)52 static int sm8250_snd_startup(struct snd_pcm_substream *substream)
53 {
54 unsigned int fmt = SND_SOC_DAIFMT_BP_FP;
55 unsigned int codec_dai_fmt = SND_SOC_DAIFMT_BC_FC;
56 struct snd_soc_pcm_runtime *rtd = substream->private_data;
57 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
58 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
59
60 switch (cpu_dai->id) {
61 case TERTIARY_MI2S_RX:
62 codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
63 snd_soc_dai_set_sysclk(cpu_dai,
64 Q6AFE_LPASS_CLK_ID_TER_MI2S_IBIT,
65 MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
66 snd_soc_dai_set_fmt(cpu_dai, fmt);
67 snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
68 break;
69 default:
70 break;
71 }
72 return 0;
73 }
74
sm8250_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)75 static int sm8250_snd_hw_params(struct snd_pcm_substream *substream,
76 struct snd_pcm_hw_params *params)
77 {
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
79 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
80 struct sm8250_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
81
82 return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
83 }
84
sm8250_snd_prepare(struct snd_pcm_substream * substream)85 static int sm8250_snd_prepare(struct snd_pcm_substream *substream)
86 {
87 struct snd_soc_pcm_runtime *rtd = substream->private_data;
88 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
89 struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
90 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
91
92 return qcom_snd_sdw_prepare(substream, sruntime,
93 &data->stream_prepared[cpu_dai->id]);
94 }
95
sm8250_snd_hw_free(struct snd_pcm_substream * substream)96 static int sm8250_snd_hw_free(struct snd_pcm_substream *substream)
97 {
98 struct snd_soc_pcm_runtime *rtd = substream->private_data;
99 struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
100 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
101 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
102
103 return qcom_snd_sdw_hw_free(substream, sruntime,
104 &data->stream_prepared[cpu_dai->id]);
105 }
106
107 static const struct snd_soc_ops sm8250_be_ops = {
108 .startup = sm8250_snd_startup,
109 .hw_params = sm8250_snd_hw_params,
110 .hw_free = sm8250_snd_hw_free,
111 .prepare = sm8250_snd_prepare,
112 };
113
sm8250_add_be_ops(struct snd_soc_card * card)114 static void sm8250_add_be_ops(struct snd_soc_card *card)
115 {
116 struct snd_soc_dai_link *link;
117 int i;
118
119 for_each_card_prelinks(card, i, link) {
120 if (link->no_pcm == 1) {
121 link->init = sm8250_snd_init;
122 link->be_hw_params_fixup = sm8250_be_hw_params_fixup;
123 link->ops = &sm8250_be_ops;
124 }
125 }
126 }
127
sm8250_platform_probe(struct platform_device * pdev)128 static int sm8250_platform_probe(struct platform_device *pdev)
129 {
130 struct snd_soc_card *card;
131 struct sm8250_snd_data *data;
132 struct device *dev = &pdev->dev;
133 int ret;
134
135 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
136 if (!card)
137 return -ENOMEM;
138
139 card->owner = THIS_MODULE;
140 /* Allocate the private data */
141 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
142 if (!data)
143 return -ENOMEM;
144
145 card->dev = dev;
146 dev_set_drvdata(dev, card);
147 snd_soc_card_set_drvdata(card, data);
148 ret = qcom_snd_parse_of(card);
149 if (ret)
150 return ret;
151
152 card->driver_name = DRIVER_NAME;
153 sm8250_add_be_ops(card);
154 return devm_snd_soc_register_card(dev, card);
155 }
156
157 static const struct of_device_id snd_sm8250_dt_match[] = {
158 {.compatible = "qcom,sm8250-sndcard"},
159 {.compatible = "qcom,qrb4210-rb2-sndcard"},
160 {.compatible = "qcom,qrb5165-rb5-sndcard"},
161 {}
162 };
163
164 MODULE_DEVICE_TABLE(of, snd_sm8250_dt_match);
165
166 static struct platform_driver snd_sm8250_driver = {
167 .probe = sm8250_platform_probe,
168 .driver = {
169 .name = "snd-sm8250",
170 .of_match_table = snd_sm8250_dt_match,
171 },
172 };
173 module_platform_driver(snd_sm8250_driver);
174 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
175 MODULE_DESCRIPTION("SM8250 ASoC Machine Driver");
176 MODULE_LICENSE("GPL");
177