1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
4 *
5 * lpass-sc7180.c -- ALSA SoC platform-machine driver for QTi LPASS
6 */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <dt-bindings/sound/sc7180-lpass.h>
16 #include <sound/pcm.h>
17 #include <sound/soc.h>
18
19 #include "lpass-lpaif-reg.h"
20 #include "lpass.h"
21
22 static struct snd_soc_dai_driver sc7180_lpass_cpu_dai_driver[] = {
23 {
24 .id = MI2S_PRIMARY,
25 .name = "Primary MI2S",
26 .playback = {
27 .stream_name = "Primary Playback",
28 .formats = SNDRV_PCM_FMTBIT_S16,
29 .rates = SNDRV_PCM_RATE_48000,
30 .rate_min = 48000,
31 .rate_max = 48000,
32 .channels_min = 2,
33 .channels_max = 2,
34 },
35 .capture = {
36 .stream_name = "Primary Capture",
37 .formats = SNDRV_PCM_FMTBIT_S16,
38 .rates = SNDRV_PCM_RATE_48000,
39 .rate_min = 48000,
40 .rate_max = 48000,
41 .channels_min = 2,
42 .channels_max = 2,
43 },
44 .probe = &asoc_qcom_lpass_cpu_dai_probe,
45 .ops = &asoc_qcom_lpass_cpu_dai_ops,
46 }, {
47 .id = MI2S_SECONDARY,
48 .name = "Secondary MI2S",
49 .playback = {
50 .stream_name = "Secondary Playback",
51 .formats = SNDRV_PCM_FMTBIT_S16,
52 .rates = SNDRV_PCM_RATE_48000,
53 .rate_min = 48000,
54 .rate_max = 48000,
55 .channels_min = 2,
56 .channels_max = 2,
57 },
58 .probe = &asoc_qcom_lpass_cpu_dai_probe,
59 .ops = &asoc_qcom_lpass_cpu_dai_ops,
60 }, {
61 .id = LPASS_DP_RX,
62 .name = "Hdmi",
63 .playback = {
64 .stream_name = "Hdmi Playback",
65 .formats = SNDRV_PCM_FMTBIT_S24,
66 .rates = SNDRV_PCM_RATE_48000,
67 .rate_min = 48000,
68 .rate_max = 48000,
69 .channels_min = 2,
70 .channels_max = 2,
71 },
72 .ops = &asoc_qcom_lpass_hdmi_dai_ops,
73 },
74 };
75
sc7180_lpass_alloc_dma_channel(struct lpass_data * drvdata,int direction,unsigned int dai_id)76 static int sc7180_lpass_alloc_dma_channel(struct lpass_data *drvdata,
77 int direction, unsigned int dai_id)
78 {
79 struct lpass_variant *v = drvdata->variant;
80 int chan = 0;
81
82 if (dai_id == LPASS_DP_RX) {
83 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
84 chan = find_first_zero_bit(&drvdata->hdmi_dma_ch_bit_map,
85 v->hdmi_rdma_channels);
86
87 if (chan >= v->hdmi_rdma_channels)
88 return -EBUSY;
89 }
90 set_bit(chan, &drvdata->hdmi_dma_ch_bit_map);
91 } else {
92 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
93 chan = find_first_zero_bit(&drvdata->dma_ch_bit_map,
94 v->rdma_channels);
95
96 if (chan >= v->rdma_channels)
97 return -EBUSY;
98 } else {
99 chan = find_next_zero_bit(&drvdata->dma_ch_bit_map,
100 v->wrdma_channel_start +
101 v->wrdma_channels,
102 v->wrdma_channel_start);
103
104 if (chan >= v->wrdma_channel_start + v->wrdma_channels)
105 return -EBUSY;
106 }
107
108 set_bit(chan, &drvdata->dma_ch_bit_map);
109 }
110 return chan;
111 }
112
sc7180_lpass_free_dma_channel(struct lpass_data * drvdata,int chan,unsigned int dai_id)113 static int sc7180_lpass_free_dma_channel(struct lpass_data *drvdata, int chan, unsigned int dai_id)
114 {
115 if (dai_id == LPASS_DP_RX)
116 clear_bit(chan, &drvdata->hdmi_dma_ch_bit_map);
117 else
118 clear_bit(chan, &drvdata->dma_ch_bit_map);
119
120 return 0;
121 }
122
sc7180_lpass_init(struct platform_device * pdev)123 static int sc7180_lpass_init(struct platform_device *pdev)
124 {
125 struct lpass_data *drvdata = platform_get_drvdata(pdev);
126 struct lpass_variant *variant = drvdata->variant;
127 struct device *dev = &pdev->dev;
128 int ret, i;
129
130 drvdata->clks = devm_kcalloc(dev, variant->num_clks,
131 sizeof(*drvdata->clks), GFP_KERNEL);
132 drvdata->num_clks = variant->num_clks;
133
134 for (i = 0; i < drvdata->num_clks; i++)
135 drvdata->clks[i].id = variant->clk_name[i];
136
137 ret = devm_clk_bulk_get(dev, drvdata->num_clks, drvdata->clks);
138 if (ret) {
139 dev_err(dev, "Failed to get clocks %d\n", ret);
140 return ret;
141 }
142
143 ret = clk_bulk_prepare_enable(drvdata->num_clks, drvdata->clks);
144 if (ret) {
145 dev_err(dev, "sc7180 clk_enable failed\n");
146 return ret;
147 }
148
149 return 0;
150 }
151
sc7180_lpass_exit(struct platform_device * pdev)152 static int sc7180_lpass_exit(struct platform_device *pdev)
153 {
154 struct lpass_data *drvdata = platform_get_drvdata(pdev);
155
156 clk_bulk_disable_unprepare(drvdata->num_clks, drvdata->clks);
157
158 return 0;
159 }
160
161 static struct lpass_variant sc7180_data = {
162 .i2sctrl_reg_base = 0x1000,
163 .i2sctrl_reg_stride = 0x1000,
164 .i2s_ports = 3,
165 .irq_reg_base = 0x9000,
166 .irq_reg_stride = 0x1000,
167 .irq_ports = 3,
168 .rdma_reg_base = 0xC000,
169 .rdma_reg_stride = 0x1000,
170 .rdma_channels = 5,
171 .hdmi_rdma_reg_base = 0x64000,
172 .hdmi_rdma_reg_stride = 0x1000,
173 .hdmi_rdma_channels = 4,
174 .dmactl_audif_start = 1,
175 .wrdma_reg_base = 0x18000,
176 .wrdma_reg_stride = 0x1000,
177 .wrdma_channel_start = 5,
178 .wrdma_channels = 4,
179
180 .loopback = REG_FIELD_ID(0x1000, 17, 17, 3, 0x1000),
181 .spken = REG_FIELD_ID(0x1000, 16, 16, 3, 0x1000),
182 .spkmode = REG_FIELD_ID(0x1000, 11, 15, 3, 0x1000),
183 .spkmono = REG_FIELD_ID(0x1000, 10, 10, 3, 0x1000),
184 .micen = REG_FIELD_ID(0x1000, 9, 9, 3, 0x1000),
185 .micmode = REG_FIELD_ID(0x1000, 4, 8, 3, 0x1000),
186 .micmono = REG_FIELD_ID(0x1000, 3, 3, 3, 0x1000),
187 .wssrc = REG_FIELD_ID(0x1000, 2, 2, 3, 0x1000),
188 .bitwidth = REG_FIELD_ID(0x1000, 0, 1, 3, 0x1000),
189
190 .rdma_dyncclk = REG_FIELD_ID(0xC000, 21, 21, 5, 0x1000),
191 .rdma_bursten = REG_FIELD_ID(0xC000, 20, 20, 5, 0x1000),
192 .rdma_wpscnt = REG_FIELD_ID(0xC000, 16, 19, 5, 0x1000),
193 .rdma_intf = REG_FIELD_ID(0xC000, 12, 15, 5, 0x1000),
194 .rdma_fifowm = REG_FIELD_ID(0xC000, 1, 5, 5, 0x1000),
195 .rdma_enable = REG_FIELD_ID(0xC000, 0, 0, 5, 0x1000),
196
197 .wrdma_dyncclk = REG_FIELD_ID(0x18000, 22, 22, 4, 0x1000),
198 .wrdma_bursten = REG_FIELD_ID(0x18000, 21, 21, 4, 0x1000),
199 .wrdma_wpscnt = REG_FIELD_ID(0x18000, 17, 20, 4, 0x1000),
200 .wrdma_intf = REG_FIELD_ID(0x18000, 12, 16, 4, 0x1000),
201 .wrdma_fifowm = REG_FIELD_ID(0x18000, 1, 5, 4, 0x1000),
202 .wrdma_enable = REG_FIELD_ID(0x18000, 0, 0, 4, 0x1000),
203
204 .hdmi_tx_ctl_addr = 0x1000,
205 .hdmi_legacy_addr = 0x1008,
206 .hdmi_vbit_addr = 0x610c0,
207 .hdmi_ch_lsb_addr = 0x61048,
208 .hdmi_ch_msb_addr = 0x6104c,
209 .ch_stride = 0x8,
210 .hdmi_parity_addr = 0x61034,
211 .hdmi_dmactl_addr = 0x61038,
212 .hdmi_dma_stride = 0x4,
213 .hdmi_DP_addr = 0x610c8,
214 .hdmi_sstream_addr = 0x6101c,
215 .hdmi_irq_reg_base = 0x63000,
216 .hdmi_irq_ports = 1,
217
218 .hdmi_rdma_dyncclk = REG_FIELD_ID(0x64000, 14, 14, 4, 0x1000),
219 .hdmi_rdma_bursten = REG_FIELD_ID(0x64000, 13, 13, 4, 0x1000),
220 .hdmi_rdma_burst8 = REG_FIELD_ID(0x64000, 15, 15, 4, 0x1000),
221 .hdmi_rdma_burst16 = REG_FIELD_ID(0x64000, 16, 16, 4, 0x1000),
222 .hdmi_rdma_dynburst = REG_FIELD_ID(0x64000, 18, 18, 4, 0x1000),
223 .hdmi_rdma_wpscnt = REG_FIELD_ID(0x64000, 10, 12, 4, 0x1000),
224 .hdmi_rdma_fifowm = REG_FIELD_ID(0x64000, 1, 5, 4, 0x1000),
225 .hdmi_rdma_enable = REG_FIELD_ID(0x64000, 0, 0, 4, 0x1000),
226
227 .sstream_en = REG_FIELD(0x6101c, 0, 0),
228 .dma_sel = REG_FIELD(0x6101c, 1, 2),
229 .auto_bbit_en = REG_FIELD(0x6101c, 3, 3),
230 .layout = REG_FIELD(0x6101c, 4, 4),
231 .layout_sp = REG_FIELD(0x6101c, 5, 8),
232 .set_sp_on_en = REG_FIELD(0x6101c, 10, 10),
233 .dp_audio = REG_FIELD(0x6101c, 11, 11),
234 .dp_staffing_en = REG_FIELD(0x6101c, 12, 12),
235 .dp_sp_b_hw_en = REG_FIELD(0x6101c, 13, 13),
236
237 .mute = REG_FIELD(0x610c8, 0, 0),
238 .as_sdp_cc = REG_FIELD(0x610c8, 1, 3),
239 .as_sdp_ct = REG_FIELD(0x610c8, 4, 7),
240 .aif_db4 = REG_FIELD(0x610c8, 8, 15),
241 .frequency = REG_FIELD(0x610c8, 16, 21),
242 .mst_index = REG_FIELD(0x610c8, 28, 29),
243 .dptx_index = REG_FIELD(0x610c8, 30, 31),
244
245 .soft_reset = REG_FIELD(0x1000, 31, 31),
246 .force_reset = REG_FIELD(0x1000, 30, 30),
247
248 .use_hw_chs = REG_FIELD(0x61038, 0, 0),
249 .use_hw_usr = REG_FIELD(0x61038, 1, 1),
250 .hw_chs_sel = REG_FIELD(0x61038, 2, 4),
251 .hw_usr_sel = REG_FIELD(0x61038, 5, 6),
252
253 .replace_vbit = REG_FIELD(0x610c0, 0, 0),
254 .vbit_stream = REG_FIELD(0x610c0, 1, 1),
255
256 .legacy_en = REG_FIELD(0x1008, 0, 0),
257 .calc_en = REG_FIELD(0x61034, 0, 0),
258 .lsb_bits = REG_FIELD(0x61048, 0, 31),
259 .msb_bits = REG_FIELD(0x6104c, 0, 31),
260
261
262 .clk_name = (const char*[]) {
263 "pcnoc-sway-clk",
264 "audio-core",
265 "pcnoc-mport-clk",
266 },
267 .num_clks = 3,
268 .dai_driver = sc7180_lpass_cpu_dai_driver,
269 .num_dai = ARRAY_SIZE(sc7180_lpass_cpu_dai_driver),
270 .dai_osr_clk_names = (const char *[]) {
271 "mclk0",
272 "null",
273 },
274 .dai_bit_clk_names = (const char *[]) {
275 "mi2s-bit-clk0",
276 "mi2s-bit-clk1",
277 },
278 .init = sc7180_lpass_init,
279 .exit = sc7180_lpass_exit,
280 .alloc_dma_channel = sc7180_lpass_alloc_dma_channel,
281 .free_dma_channel = sc7180_lpass_free_dma_channel,
282 };
283
284 static const struct of_device_id sc7180_lpass_cpu_device_id[] = {
285 {.compatible = "qcom,sc7180-lpass-cpu", .data = &sc7180_data},
286 {}
287 };
288 MODULE_DEVICE_TABLE(of, sc7180_lpass_cpu_device_id);
289
290 static struct platform_driver sc7180_lpass_cpu_platform_driver = {
291 .driver = {
292 .name = "sc7180-lpass-cpu",
293 .of_match_table = of_match_ptr(sc7180_lpass_cpu_device_id),
294 },
295 .probe = asoc_qcom_lpass_cpu_platform_probe,
296 .remove = asoc_qcom_lpass_cpu_platform_remove,
297 };
298
299 module_platform_driver(sc7180_lpass_cpu_platform_driver);
300
301 MODULE_DESCRIPTION("SC7180 LPASS CPU DRIVER");
302 MODULE_LICENSE("GPL v2");
303