blob: 3bd9eb3cc688b2623a13470a7684fd4719a15bb5 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Kenneth Westfield80beab82015-03-03 16:21:54 -08002/*
3 * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
4 *
Kenneth Westfield80beab82015-03-03 16:21:54 -08005 * lpass-cpu.c -- ALSA SoC CPU DAI driver for QTi LPASS
6 */
7
8#include <linux/clk.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -08009#include <linux/kernel.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -080010#include <linux/module.h>
11#include <linux/of.h>
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +010012#include <linux/of_device.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -080013#include <linux/platform_device.h>
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16#include <linux/regmap.h>
17#include <sound/soc.h>
18#include <sound/soc-dai.h>
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +010019#include "lpass-lpaif-reg.h"
Kenneth Westfield80beab82015-03-03 16:21:54 -080020#include "lpass.h"
21
Stephan Gerhold4ff028f62020-04-25 20:46:57 +020022#define LPASS_CPU_MAX_MI2S_LINES 4
23#define LPASS_CPU_I2S_SD0_MASK BIT(0)
24#define LPASS_CPU_I2S_SD1_MASK BIT(1)
25#define LPASS_CPU_I2S_SD2_MASK BIT(2)
26#define LPASS_CPU_I2S_SD3_MASK BIT(3)
27#define LPASS_CPU_I2S_SD0_1_MASK GENMASK(1, 0)
28#define LPASS_CPU_I2S_SD2_3_MASK GENMASK(3, 2)
29#define LPASS_CPU_I2S_SD0_1_2_MASK GENMASK(2, 0)
30#define LPASS_CPU_I2S_SD0_1_2_3_MASK GENMASK(3, 0)
31
Srinivasa Rao Mandadapuc223f412021-06-09 19:00:39 +053032/*
33 * Channel maps for Quad channel playbacks on MI2S Secondary
34 */
35static struct snd_pcm_chmap_elem lpass_quad_chmaps[] = {
36 { .channels = 4,
37 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_RL,
38 SNDRV_CHMAP_FR, SNDRV_CHMAP_RR } },
39 { }
40};
Rohit kumarb5022a32020-08-14 16:23:01 +053041static int lpass_cpu_init_i2sctl_bitfields(struct device *dev,
42 struct lpaif_i2sctl *i2sctl, struct regmap *map)
43{
44 struct lpass_data *drvdata = dev_get_drvdata(dev);
45 struct lpass_variant *v = drvdata->variant;
46
47 i2sctl->loopback = devm_regmap_field_alloc(dev, map, v->loopback);
48 i2sctl->spken = devm_regmap_field_alloc(dev, map, v->spken);
49 i2sctl->spkmode = devm_regmap_field_alloc(dev, map, v->spkmode);
50 i2sctl->spkmono = devm_regmap_field_alloc(dev, map, v->spkmono);
51 i2sctl->micen = devm_regmap_field_alloc(dev, map, v->micen);
52 i2sctl->micmode = devm_regmap_field_alloc(dev, map, v->micmode);
53 i2sctl->micmono = devm_regmap_field_alloc(dev, map, v->micmono);
54 i2sctl->wssrc = devm_regmap_field_alloc(dev, map, v->wssrc);
55 i2sctl->bitwidth = devm_regmap_field_alloc(dev, map, v->bitwidth);
56
57 if (IS_ERR(i2sctl->loopback) || IS_ERR(i2sctl->spken) ||
58 IS_ERR(i2sctl->spkmode) || IS_ERR(i2sctl->spkmono) ||
59 IS_ERR(i2sctl->micen) || IS_ERR(i2sctl->micmode) ||
60 IS_ERR(i2sctl->micmono) || IS_ERR(i2sctl->wssrc) ||
61 IS_ERR(i2sctl->bitwidth))
62 return -EINVAL;
63
64 return 0;
65}
66
Kenneth Westfield80beab82015-03-03 16:21:54 -080067static int lpass_cpu_daiops_set_sysclk(struct snd_soc_dai *dai, int clk_id,
68 unsigned int freq, int dir)
69{
70 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
71 int ret;
72
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010073 ret = clk_set_rate(drvdata->mi2s_osr_clk[dai->driver->id], freq);
Kenneth Westfield80beab82015-03-03 16:21:54 -080074 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080075 dev_err(dai->dev, "error setting mi2s osrclk to %u: %d\n",
76 freq, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -080077
78 return ret;
79}
80
81static int lpass_cpu_daiops_startup(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *dai)
83{
84 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
85 int ret;
86
Bjorn Andersson46dccc32017-01-30 13:03:36 -080087 ret = clk_prepare_enable(drvdata->mi2s_osr_clk[dai->driver->id]);
88 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080089 dev_err(dai->dev, "error in enabling mi2s osr clk: %d\n", ret);
Bjorn Andersson46dccc32017-01-30 13:03:36 -080090 return ret;
Kenneth Westfield80beab82015-03-03 16:21:54 -080091 }
V Sujith Kumar Reddy6ec6c362020-10-19 14:36:03 +053092 ret = clk_prepare(drvdata->mi2s_bit_clk[dai->driver->id]);
93 if (ret) {
94 dev_err(dai->dev, "error in enabling mi2s bit clk: %d\n", ret);
95 clk_disable_unprepare(drvdata->mi2s_osr_clk[dai->driver->id]);
96 return ret;
97 }
Kenneth Westfield80beab82015-03-03 16:21:54 -080098 return 0;
99}
100
101static void lpass_cpu_daiops_shutdown(struct snd_pcm_substream *substream,
102 struct snd_soc_dai *dai)
103{
104 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800105 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
106 unsigned int id = dai->driver->id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800107
Bjorn Andersson46dccc32017-01-30 13:03:36 -0800108 clk_disable_unprepare(drvdata->mi2s_osr_clk[dai->driver->id]);
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800109 /*
110 * Ensure LRCLK is disabled even in device node validation.
111 * Will not impact if disabled in lpass_cpu_daiops_trigger()
112 * suspend.
113 */
114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
115 regmap_fields_write(i2sctl->spken, id, LPAIF_I2SCTL_SPKEN_DISABLE);
116 else
117 regmap_fields_write(i2sctl->micen, id, LPAIF_I2SCTL_MICEN_DISABLE);
118
119 /*
120 * BCLK may not be enabled if lpass_cpu_daiops_prepare is called before
121 * lpass_cpu_daiops_shutdown. It's paired with the clk_enable in
122 * lpass_cpu_daiops_prepare.
123 */
124 if (drvdata->mi2s_was_prepared[dai->driver->id]) {
125 drvdata->mi2s_was_prepared[dai->driver->id] = false;
126 clk_disable(drvdata->mi2s_bit_clk[dai->driver->id]);
127 }
128
V Sujith Kumar Reddy6ec6c362020-10-19 14:36:03 +0530129 clk_unprepare(drvdata->mi2s_bit_clk[dai->driver->id]);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800130}
131
132static int lpass_cpu_daiops_hw_params(struct snd_pcm_substream *substream,
133 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
134{
135 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Rohit kumarb5022a32020-08-14 16:23:01 +0530136 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
137 unsigned int id = dai->driver->id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800138 snd_pcm_format_t format = params_format(params);
139 unsigned int channels = params_channels(params);
140 unsigned int rate = params_rate(params);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200141 unsigned int mode;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800142 unsigned int regval;
143 int bitwidth, ret;
144
145 bitwidth = snd_pcm_format_width(format);
146 if (bitwidth < 0) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800147 dev_err(dai->dev, "invalid bit width given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800148 return bitwidth;
149 }
150
Rohit kumarb5022a32020-08-14 16:23:01 +0530151 ret = regmap_fields_write(i2sctl->loopback, id,
152 LPAIF_I2SCTL_LOOPBACK_DISABLE);
153 if (ret) {
154 dev_err(dai->dev, "error updating loopback field: %d\n", ret);
155 return ret;
156 }
157
158 ret = regmap_fields_write(i2sctl->wssrc, id,
159 LPAIF_I2SCTL_WSSRC_INTERNAL);
160 if (ret) {
161 dev_err(dai->dev, "error updating wssrc field: %d\n", ret);
162 return ret;
163 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800164
165 switch (bitwidth) {
166 case 16:
Rohit kumarb5022a32020-08-14 16:23:01 +0530167 regval = LPAIF_I2SCTL_BITWIDTH_16;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800168 break;
169 case 24:
Rohit kumarb5022a32020-08-14 16:23:01 +0530170 regval = LPAIF_I2SCTL_BITWIDTH_24;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800171 break;
172 case 32:
Rohit kumarb5022a32020-08-14 16:23:01 +0530173 regval = LPAIF_I2SCTL_BITWIDTH_32;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800174 break;
175 default:
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800176 dev_err(dai->dev, "invalid bitwidth given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800177 return -EINVAL;
178 }
179
Rohit kumarb5022a32020-08-14 16:23:01 +0530180 ret = regmap_fields_write(i2sctl->bitwidth, id, regval);
181 if (ret) {
182 dev_err(dai->dev, "error updating bitwidth field: %d\n", ret);
183 return ret;
184 }
185
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200186 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Rohit kumarb5022a32020-08-14 16:23:01 +0530187 mode = drvdata->mi2s_playback_sd_mode[id];
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200188 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530189 mode = drvdata->mi2s_capture_sd_mode[id];
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200190
191 if (!mode) {
192 dev_err(dai->dev, "no line is assigned\n");
193 return -EINVAL;
194 }
195
196 switch (channels) {
197 case 1:
198 case 2:
199 switch (mode) {
200 case LPAIF_I2SCTL_MODE_QUAD01:
201 case LPAIF_I2SCTL_MODE_6CH:
202 case LPAIF_I2SCTL_MODE_8CH:
203 mode = LPAIF_I2SCTL_MODE_SD0;
204 break;
205 case LPAIF_I2SCTL_MODE_QUAD23:
206 mode = LPAIF_I2SCTL_MODE_SD2;
207 break;
208 }
209
210 break;
211 case 4:
212 if (mode < LPAIF_I2SCTL_MODE_QUAD01) {
213 dev_err(dai->dev, "cannot configure 4 channels with mode %d\n",
214 mode);
215 return -EINVAL;
216 }
217
218 switch (mode) {
219 case LPAIF_I2SCTL_MODE_6CH:
220 case LPAIF_I2SCTL_MODE_8CH:
221 mode = LPAIF_I2SCTL_MODE_QUAD01;
222 break;
223 }
224 break;
225 case 6:
226 if (mode < LPAIF_I2SCTL_MODE_6CH) {
227 dev_err(dai->dev, "cannot configure 6 channels with mode %d\n",
228 mode);
229 return -EINVAL;
230 }
231
232 switch (mode) {
233 case LPAIF_I2SCTL_MODE_8CH:
234 mode = LPAIF_I2SCTL_MODE_6CH;
235 break;
236 }
237 break;
238 case 8:
239 if (mode < LPAIF_I2SCTL_MODE_8CH) {
240 dev_err(dai->dev, "cannot configure 8 channels with mode %d\n",
241 mode);
242 return -EINVAL;
243 }
244 break;
245 default:
246 dev_err(dai->dev, "invalid channels given: %u\n", channels);
247 return -EINVAL;
248 }
249
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530251 ret = regmap_fields_write(i2sctl->spkmode, id,
252 LPAIF_I2SCTL_SPKMODE(mode));
253 if (ret) {
254 dev_err(dai->dev, "error writing to i2sctl spkr mode: %d\n",
255 ret);
256 return ret;
257 }
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200258 if (channels >= 2)
Rohit kumarb5022a32020-08-14 16:23:01 +0530259 ret = regmap_fields_write(i2sctl->spkmono, id,
260 LPAIF_I2SCTL_SPKMONO_STEREO);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200261 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530262 ret = regmap_fields_write(i2sctl->spkmono, id,
263 LPAIF_I2SCTL_SPKMONO_MONO);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000264 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530265 ret = regmap_fields_write(i2sctl->micmode, id,
266 LPAIF_I2SCTL_MICMODE(mode));
267 if (ret) {
268 dev_err(dai->dev, "error writing to i2sctl mic mode: %d\n",
269 ret);
270 return ret;
271 }
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200272 if (channels >= 2)
Rohit kumarb5022a32020-08-14 16:23:01 +0530273 ret = regmap_fields_write(i2sctl->micmono, id,
274 LPAIF_I2SCTL_MICMONO_STEREO);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200275 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530276 ret = regmap_fields_write(i2sctl->micmono, id,
277 LPAIF_I2SCTL_MICMONO_MONO);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800278 }
279
Kenneth Westfield80beab82015-03-03 16:21:54 -0800280 if (ret) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530281 dev_err(dai->dev, "error writing to i2sctl channels mode: %d\n",
282 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800283 return ret;
284 }
285
Rohit kumarb5022a32020-08-14 16:23:01 +0530286 ret = clk_set_rate(drvdata->mi2s_bit_clk[id],
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100287 rate * bitwidth * 2);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800288 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800289 dev_err(dai->dev, "error setting mi2s bitclk to %u: %d\n",
290 rate * bitwidth * 2, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800291 return ret;
292 }
293
294 return 0;
295}
296
Kenneth Westfield80beab82015-03-03 16:21:54 -0800297static int lpass_cpu_daiops_trigger(struct snd_pcm_substream *substream,
298 int cmd, struct snd_soc_dai *dai)
299{
300 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Rohit kumarb5022a32020-08-14 16:23:01 +0530301 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
302 unsigned int id = dai->driver->id;
Takashi Iwai96f05be2015-04-13 14:23:29 +0200303 int ret = -EINVAL;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800304
305 switch (cmd) {
306 case SNDRV_PCM_TRIGGER_START:
307 case SNDRV_PCM_TRIGGER_RESUME:
308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800309 /*
310 * Ensure lpass BCLK/LRCLK is enabled during
311 * device resume as lpass_cpu_daiops_prepare() is not called
312 * after the device resumes. We don't check mi2s_was_prepared before
313 * enable/disable BCLK in trigger events because:
314 * 1. These trigger events are paired, so the BCLK
315 * enable_count is balanced.
316 * 2. the BCLK can be shared (ex: headset and headset mic),
317 * we need to increase the enable_count so that we don't
318 * turn off the shared BCLK while other devices are using
319 * it.
320 */
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000321 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530322 ret = regmap_fields_write(i2sctl->spken, id,
323 LPAIF_I2SCTL_SPKEN_ENABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000324 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530325 ret = regmap_fields_write(i2sctl->micen, id,
326 LPAIF_I2SCTL_MICEN_ENABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000327 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800328 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800329 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
330 ret);
V Sujith Kumar Reddy7e6799d2020-09-18 22:24:33 +0530331
Srinivasa Rao Mandadapu6c283772021-01-27 20:48:24 +0530332 ret = clk_enable(drvdata->mi2s_bit_clk[id]);
333 if (ret) {
334 dev_err(dai->dev, "error in enabling mi2s bit clk: %d\n", ret);
335 clk_disable(drvdata->mi2s_osr_clk[id]);
336 return ret;
V Sujith Kumar Reddy7e6799d2020-09-18 22:24:33 +0530337 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800338 break;
339 case SNDRV_PCM_TRIGGER_STOP:
340 case SNDRV_PCM_TRIGGER_SUSPEND:
341 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800342 /*
343 * To ensure lpass BCLK/LRCLK is disabled during
344 * device suspend.
345 */
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530347 ret = regmap_fields_write(i2sctl->spken, id,
348 LPAIF_I2SCTL_SPKEN_DISABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000349 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530350 ret = regmap_fields_write(i2sctl->micen, id,
351 LPAIF_I2SCTL_MICEN_DISABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000352 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800353 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800354 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
355 ret);
Srinivasa Rao Mandadapu6c283772021-01-27 20:48:24 +0530356
357 clk_disable(drvdata->mi2s_bit_clk[dai->driver->id]);
358
Kenneth Westfield80beab82015-03-03 16:21:54 -0800359 break;
360 }
361
362 return ret;
363}
364
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800365static int lpass_cpu_daiops_prepare(struct snd_pcm_substream *substream,
366 struct snd_soc_dai *dai)
367{
368 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
369 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
370 unsigned int id = dai->driver->id;
371 int ret;
372
373 /*
374 * Ensure lpass BCLK/LRCLK is enabled bit before playback/capture
375 * data flow starts. This allows other codec to have some delay before
376 * the data flow.
377 * (ex: to drop start up pop noise before capture starts).
378 */
379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
380 ret = regmap_fields_write(i2sctl->spken, id, LPAIF_I2SCTL_SPKEN_ENABLE);
381 else
382 ret = regmap_fields_write(i2sctl->micen, id, LPAIF_I2SCTL_MICEN_ENABLE);
383
384 if (ret) {
385 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
386 return ret;
387 }
388
389 /*
390 * Check mi2s_was_prepared before enabling BCLK as lpass_cpu_daiops_prepare can
391 * be called multiple times. It's paired with the clk_disable in
392 * lpass_cpu_daiops_shutdown.
393 */
394 if (!drvdata->mi2s_was_prepared[dai->driver->id]) {
395 ret = clk_enable(drvdata->mi2s_bit_clk[id]);
396 if (ret) {
397 dev_err(dai->dev, "error in enabling mi2s bit clk: %d\n", ret);
398 return ret;
399 }
400 drvdata->mi2s_was_prepared[dai->driver->id] = true;
401 }
402 return 0;
403}
404
Axel Lin618718d2015-08-28 10:53:31 +0800405const struct snd_soc_dai_ops asoc_qcom_lpass_cpu_dai_ops = {
Kenneth Westfield80beab82015-03-03 16:21:54 -0800406 .set_sysclk = lpass_cpu_daiops_set_sysclk,
407 .startup = lpass_cpu_daiops_startup,
408 .shutdown = lpass_cpu_daiops_shutdown,
409 .hw_params = lpass_cpu_daiops_hw_params,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800410 .trigger = lpass_cpu_daiops_trigger,
Srinivasa Rao Mandadapuc8a45562021-06-04 23:45:45 +0800411 .prepare = lpass_cpu_daiops_prepare,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800412};
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100413EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_ops);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800414
Srinivasa Rao Mandadapuc223f412021-06-09 19:00:39 +0530415int lpass_cpu_pcm_new(struct snd_soc_pcm_runtime *rtd,
416 struct snd_soc_dai *dai)
417{
418 int ret;
419 struct snd_soc_dai_driver *drv = dai->driver;
420 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
421
422 if (drvdata->mi2s_playback_sd_mode[dai->id] == LPAIF_I2SCTL_MODE_QUAD01) {
423 ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
424 lpass_quad_chmaps, drv->playback.channels_max, 0,
425 NULL);
426 if (ret < 0)
427 return ret;
428 }
429
430 return 0;
431}
432EXPORT_SYMBOL_GPL(lpass_cpu_pcm_new);
433
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100434int asoc_qcom_lpass_cpu_dai_probe(struct snd_soc_dai *dai)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800435{
436 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
437 int ret;
438
439 /* ensure audio hardware is disabled */
440 ret = regmap_write(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100441 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id), 0);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800442 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800443 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800444
445 return ret;
446}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100447EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800448
Srinivas Kandagatlacd3484f2021-01-19 17:15:27 +0000449static int asoc_qcom_of_xlate_dai_name(struct snd_soc_component *component,
Krzysztof Kozlowski933f98b2021-02-21 16:30:24 +0100450 const struct of_phandle_args *args,
Srinivas Kandagatlacd3484f2021-01-19 17:15:27 +0000451 const char **dai_name)
452{
453 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
454 struct lpass_variant *variant = drvdata->variant;
455 int id = args->args[0];
456 int ret = -EINVAL;
457 int i;
458
459 for (i = 0; i < variant->num_dai; i++) {
460 if (variant->dai_driver[i].id == id) {
461 *dai_name = variant->dai_driver[i].name;
462 ret = 0;
463 break;
464 }
465 }
466
467 return ret;
468}
469
Kenneth Westfield80beab82015-03-03 16:21:54 -0800470static const struct snd_soc_component_driver lpass_cpu_comp_driver = {
471 .name = "lpass-cpu",
Srinivas Kandagatlacd3484f2021-01-19 17:15:27 +0000472 .of_xlate_dai_name = asoc_qcom_of_xlate_dai_name,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800473};
474
475static bool lpass_cpu_regmap_writeable(struct device *dev, unsigned int reg)
476{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100477 struct lpass_data *drvdata = dev_get_drvdata(dev);
478 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800479 int i;
480
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100481 for (i = 0; i < v->i2s_ports; ++i)
482 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800483 return true;
484
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100485 for (i = 0; i < v->irq_ports; ++i) {
486 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800487 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100488 if (reg == LPAIF_IRQCLEAR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800489 return true;
490 }
491
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100492 for (i = 0; i < v->rdma_channels; ++i) {
493 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800494 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100495 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800496 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100497 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800498 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100499 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800500 return true;
501 }
502
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000503 for (i = 0; i < v->wrdma_channels; ++i) {
504 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
505 return true;
506 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
507 return true;
508 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
509 return true;
510 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
511 return true;
512 }
513
Kenneth Westfield80beab82015-03-03 16:21:54 -0800514 return false;
515}
516
517static bool lpass_cpu_regmap_readable(struct device *dev, unsigned int reg)
518{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100519 struct lpass_data *drvdata = dev_get_drvdata(dev);
520 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800521 int i;
522
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100523 for (i = 0; i < v->i2s_ports; ++i)
524 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800525 return true;
526
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100527 for (i = 0; i < v->irq_ports; ++i) {
Srinivas Kandagatla6a7f5bd2021-06-24 10:21:53 +0100528 if (reg == LPAIF_IRQCLEAR_REG(v, i))
529 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100530 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800531 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100532 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800533 return true;
534 }
535
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100536 for (i = 0; i < v->rdma_channels; ++i) {
537 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800538 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100539 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800540 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100541 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800542 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100543 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800544 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100545 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800546 return true;
547 }
548
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000549 for (i = 0; i < v->wrdma_channels; ++i) {
550 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
551 return true;
552 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
553 return true;
554 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
555 return true;
556 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
557 return true;
558 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
559 return true;
560 }
561
Kenneth Westfield80beab82015-03-03 16:21:54 -0800562 return false;
563}
564
565static bool lpass_cpu_regmap_volatile(struct device *dev, unsigned int reg)
566{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100567 struct lpass_data *drvdata = dev_get_drvdata(dev);
568 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800569 int i;
570
Srinivas Kandagatla6a7f5bd2021-06-24 10:21:53 +0100571 for (i = 0; i < v->irq_ports; ++i) {
572 if (reg == LPAIF_IRQCLEAR_REG(v, i))
573 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100574 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800575 return true;
Srinivas Kandagatla6a7f5bd2021-06-24 10:21:53 +0100576 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800577
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100578 for (i = 0; i < v->rdma_channels; ++i)
Srinivasa Rao Mandadapu315fbe4c2020-12-17 13:38:33 +0530579 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800580 return true;
581
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000582 for (i = 0; i < v->wrdma_channels; ++i)
Srinivasa Rao Mandadapu315fbe4c2020-12-17 13:38:33 +0530583 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000584 return true;
585
Kenneth Westfield80beab82015-03-03 16:21:54 -0800586 return false;
587}
588
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100589static struct regmap_config lpass_cpu_regmap_config = {
Stephen Boyd03b49bf2021-01-14 19:43:25 -0800590 .name = "lpass_cpu",
Kenneth Westfield80beab82015-03-03 16:21:54 -0800591 .reg_bits = 32,
592 .reg_stride = 4,
593 .val_bits = 32,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800594 .writeable_reg = lpass_cpu_regmap_writeable,
595 .readable_reg = lpass_cpu_regmap_readable,
596 .volatile_reg = lpass_cpu_regmap_volatile,
597 .cache_type = REGCACHE_FLAT,
598};
599
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530600static int lpass_hdmi_init_bitfields(struct device *dev, struct regmap *map)
601{
602 struct lpass_data *drvdata = dev_get_drvdata(dev);
603 struct lpass_variant *v = drvdata->variant;
604 unsigned int i;
605 struct lpass_hdmi_tx_ctl *tx_ctl;
606 struct regmap_field *legacy_en;
607 struct lpass_vbit_ctrl *vbit_ctl;
608 struct regmap_field *tx_parity;
609 struct lpass_dp_metadata_ctl *meta_ctl;
610 struct lpass_sstream_ctl *sstream_ctl;
611 struct regmap_field *ch_msb;
612 struct regmap_field *ch_lsb;
613 struct lpass_hdmitx_dmactl *tx_dmactl;
614 int rval;
615
616 tx_ctl = devm_kzalloc(dev, sizeof(*tx_ctl), GFP_KERNEL);
617 if (!tx_ctl)
618 return -ENOMEM;
619
620 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->soft_reset, tx_ctl->soft_reset);
621 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->force_reset, tx_ctl->force_reset);
622 drvdata->tx_ctl = tx_ctl;
623
624 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->legacy_en, legacy_en);
625 drvdata->hdmitx_legacy_en = legacy_en;
626
627 vbit_ctl = devm_kzalloc(dev, sizeof(*vbit_ctl), GFP_KERNEL);
628 if (!vbit_ctl)
629 return -ENOMEM;
630
631 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->replace_vbit, vbit_ctl->replace_vbit);
632 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->vbit_stream, vbit_ctl->vbit_stream);
633 drvdata->vbit_ctl = vbit_ctl;
634
635
636 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->calc_en, tx_parity);
637 drvdata->hdmitx_parity_calc_en = tx_parity;
638
639 meta_ctl = devm_kzalloc(dev, sizeof(*meta_ctl), GFP_KERNEL);
640 if (!meta_ctl)
641 return -ENOMEM;
642
643 rval = devm_regmap_field_bulk_alloc(dev, map, &meta_ctl->mute, &v->mute, 7);
644 if (rval)
645 return rval;
646 drvdata->meta_ctl = meta_ctl;
647
648 sstream_ctl = devm_kzalloc(dev, sizeof(*sstream_ctl), GFP_KERNEL);
649 if (!sstream_ctl)
650 return -ENOMEM;
651
652 rval = devm_regmap_field_bulk_alloc(dev, map, &sstream_ctl->sstream_en, &v->sstream_en, 9);
653 if (rval)
654 return rval;
655
656 drvdata->sstream_ctl = sstream_ctl;
657
658 for (i = 0; i < LPASS_MAX_HDMI_DMA_CHANNELS; i++) {
659 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->msb_bits, ch_msb);
660 drvdata->hdmitx_ch_msb[i] = ch_msb;
661
662 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->lsb_bits, ch_lsb);
663 drvdata->hdmitx_ch_lsb[i] = ch_lsb;
664
665 tx_dmactl = devm_kzalloc(dev, sizeof(*tx_dmactl), GFP_KERNEL);
666 if (!tx_dmactl)
667 return -ENOMEM;
668
669 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->use_hw_chs, tx_dmactl->use_hw_chs);
670 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->use_hw_usr, tx_dmactl->use_hw_usr);
671 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->hw_chs_sel, tx_dmactl->hw_chs_sel);
672 QCOM_REGMAP_FIELD_ALLOC(dev, map, v->hw_usr_sel, tx_dmactl->hw_usr_sel);
673 drvdata->hdmi_tx_dmactl[i] = tx_dmactl;
674 }
675 return 0;
676}
677
678static bool lpass_hdmi_regmap_writeable(struct device *dev, unsigned int reg)
679{
680 struct lpass_data *drvdata = dev_get_drvdata(dev);
681 struct lpass_variant *v = drvdata->variant;
682 int i;
683
684 if (reg == LPASS_HDMI_TX_CTL_ADDR(v))
685 return true;
686 if (reg == LPASS_HDMI_TX_LEGACY_ADDR(v))
687 return true;
688 if (reg == LPASS_HDMI_TX_VBIT_CTL_ADDR(v))
689 return true;
690 if (reg == LPASS_HDMI_TX_PARITY_ADDR(v))
691 return true;
692 if (reg == LPASS_HDMI_TX_DP_ADDR(v))
693 return true;
694 if (reg == LPASS_HDMI_TX_SSTREAM_ADDR(v))
695 return true;
696 if (reg == LPASS_HDMITX_APP_IRQEN_REG(v))
697 return true;
698 if (reg == LPASS_HDMITX_APP_IRQCLEAR_REG(v))
699 return true;
700
701 for (i = 0; i < v->hdmi_rdma_channels; i++) {
702 if (reg == LPASS_HDMI_TX_CH_LSB_ADDR(v, i))
703 return true;
704 if (reg == LPASS_HDMI_TX_CH_MSB_ADDR(v, i))
705 return true;
706 if (reg == LPASS_HDMI_TX_DMA_ADDR(v, i))
707 return true;
708 }
709
Srinivasa Rao Mandadapue681b1a2021-02-02 11:57:27 +0530710 for (i = 0; i < v->hdmi_rdma_channels; ++i) {
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530711 if (reg == LPAIF_HDMI_RDMACTL_REG(v, i))
712 return true;
713 if (reg == LPAIF_HDMI_RDMABASE_REG(v, i))
714 return true;
715 if (reg == LPAIF_HDMI_RDMABUFF_REG(v, i))
716 return true;
717 if (reg == LPAIF_HDMI_RDMAPER_REG(v, i))
718 return true;
719 }
720 return false;
721}
722
723static bool lpass_hdmi_regmap_readable(struct device *dev, unsigned int reg)
724{
725 struct lpass_data *drvdata = dev_get_drvdata(dev);
726 struct lpass_variant *v = drvdata->variant;
727 int i;
728
729 if (reg == LPASS_HDMI_TX_CTL_ADDR(v))
730 return true;
731 if (reg == LPASS_HDMI_TX_LEGACY_ADDR(v))
732 return true;
733 if (reg == LPASS_HDMI_TX_VBIT_CTL_ADDR(v))
734 return true;
735
736 for (i = 0; i < v->hdmi_rdma_channels; i++) {
737 if (reg == LPASS_HDMI_TX_CH_LSB_ADDR(v, i))
738 return true;
739 if (reg == LPASS_HDMI_TX_CH_MSB_ADDR(v, i))
740 return true;
741 if (reg == LPASS_HDMI_TX_DMA_ADDR(v, i))
742 return true;
743 }
744
745 if (reg == LPASS_HDMI_TX_PARITY_ADDR(v))
746 return true;
747 if (reg == LPASS_HDMI_TX_DP_ADDR(v))
748 return true;
749 if (reg == LPASS_HDMI_TX_SSTREAM_ADDR(v))
750 return true;
751 if (reg == LPASS_HDMITX_APP_IRQEN_REG(v))
752 return true;
753 if (reg == LPASS_HDMITX_APP_IRQSTAT_REG(v))
754 return true;
755
Srinivasa Rao Mandadapue681b1a2021-02-02 11:57:27 +0530756 for (i = 0; i < v->hdmi_rdma_channels; ++i) {
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530757 if (reg == LPAIF_HDMI_RDMACTL_REG(v, i))
758 return true;
759 if (reg == LPAIF_HDMI_RDMABASE_REG(v, i))
760 return true;
761 if (reg == LPAIF_HDMI_RDMABUFF_REG(v, i))
762 return true;
763 if (reg == LPAIF_HDMI_RDMAPER_REG(v, i))
764 return true;
765 if (reg == LPAIF_HDMI_RDMACURR_REG(v, i))
766 return true;
767 }
768
769 return false;
770}
771
772static bool lpass_hdmi_regmap_volatile(struct device *dev, unsigned int reg)
773{
774 struct lpass_data *drvdata = dev_get_drvdata(dev);
775 struct lpass_variant *v = drvdata->variant;
776 int i;
777
778 if (reg == LPASS_HDMITX_APP_IRQSTAT_REG(v))
779 return true;
780 if (reg == LPASS_HDMI_TX_LEGACY_ADDR(v))
781 return true;
782
Srinivasa Rao Mandadapue681b1a2021-02-02 11:57:27 +0530783 for (i = 0; i < v->hdmi_rdma_channels; ++i) {
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530784 if (reg == LPAIF_HDMI_RDMACURR_REG(v, i))
785 return true;
786 }
787 return false;
788}
789
Srinivas Kandagatla20f64a12020-11-05 11:41:00 +0000790static struct regmap_config lpass_hdmi_regmap_config = {
Stephen Boyd03b49bf2021-01-14 19:43:25 -0800791 .name = "lpass_hdmi",
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530792 .reg_bits = 32,
793 .reg_stride = 4,
794 .val_bits = 32,
795 .writeable_reg = lpass_hdmi_regmap_writeable,
796 .readable_reg = lpass_hdmi_regmap_readable,
797 .volatile_reg = lpass_hdmi_regmap_volatile,
798 .cache_type = REGCACHE_FLAT,
799};
800
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200801static unsigned int of_lpass_cpu_parse_sd_lines(struct device *dev,
802 struct device_node *node,
803 const char *name)
804{
805 unsigned int lines[LPASS_CPU_MAX_MI2S_LINES];
806 unsigned int sd_line_mask = 0;
807 int num_lines, i;
808
809 num_lines = of_property_read_variable_u32_array(node, name, lines, 0,
810 LPASS_CPU_MAX_MI2S_LINES);
811 if (num_lines < 0)
812 return LPAIF_I2SCTL_MODE_NONE;
813
814 for (i = 0; i < num_lines; i++)
815 sd_line_mask |= BIT(lines[i]);
816
817 switch (sd_line_mask) {
818 case LPASS_CPU_I2S_SD0_MASK:
819 return LPAIF_I2SCTL_MODE_SD0;
820 case LPASS_CPU_I2S_SD1_MASK:
821 return LPAIF_I2SCTL_MODE_SD1;
822 case LPASS_CPU_I2S_SD2_MASK:
823 return LPAIF_I2SCTL_MODE_SD2;
824 case LPASS_CPU_I2S_SD3_MASK:
825 return LPAIF_I2SCTL_MODE_SD3;
826 case LPASS_CPU_I2S_SD0_1_MASK:
827 return LPAIF_I2SCTL_MODE_QUAD01;
828 case LPASS_CPU_I2S_SD2_3_MASK:
829 return LPAIF_I2SCTL_MODE_QUAD23;
830 case LPASS_CPU_I2S_SD0_1_2_MASK:
831 return LPAIF_I2SCTL_MODE_6CH;
832 case LPASS_CPU_I2S_SD0_1_2_3_MASK:
833 return LPAIF_I2SCTL_MODE_8CH;
834 default:
835 dev_err(dev, "Unsupported SD line mask: %#x\n", sd_line_mask);
836 return LPAIF_I2SCTL_MODE_NONE;
837 }
838}
839
840static void of_lpass_cpu_parse_dai_data(struct device *dev,
841 struct lpass_data *data)
842{
843 struct device_node *node;
844 int ret, id;
845
846 /* Allow all channels by default for backwards compatibility */
847 for (id = 0; id < data->variant->num_dai; id++) {
848 data->mi2s_playback_sd_mode[id] = LPAIF_I2SCTL_MODE_8CH;
849 data->mi2s_capture_sd_mode[id] = LPAIF_I2SCTL_MODE_8CH;
850 }
851
852 for_each_child_of_node(dev->of_node, node) {
853 ret = of_property_read_u32(node, "reg", &id);
Srinivasa Rao Mandadapu9922f502021-03-11 21:15:57 +0530854 if (ret || id < 0) {
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200855 dev_err(dev, "valid dai id not found: %d\n", ret);
856 continue;
857 }
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530858 if (id == LPASS_DP_RX) {
859 data->hdmi_port_enable = 1;
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530860 } else {
861 data->mi2s_playback_sd_mode[id] =
862 of_lpass_cpu_parse_sd_lines(dev, node,
863 "qcom,playback-sd-lines");
864 data->mi2s_capture_sd_mode[id] =
865 of_lpass_cpu_parse_sd_lines(dev, node,
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200866 "qcom,capture-sd-lines");
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530867 }
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200868 }
869}
870
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100871int asoc_qcom_lpass_cpu_platform_probe(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800872{
873 struct lpass_data *drvdata;
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700874 struct device_node *dsp_of_node;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100875 struct lpass_variant *variant;
876 struct device *dev = &pdev->dev;
877 const struct of_device_id *match;
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100878 int ret, i, dai_id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800879
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700880 dsp_of_node = of_parse_phandle(pdev->dev.of_node, "qcom,adsp", 0);
881 if (dsp_of_node) {
Tang Bin952c0e22020-05-04 14:59:47 +0800882 dev_err(dev, "DSP exists and holds audio resources\n");
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700883 return -EBUSY;
884 }
885
Tang Bin952c0e22020-05-04 14:59:47 +0800886 drvdata = devm_kzalloc(dev, sizeof(struct lpass_data), GFP_KERNEL);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800887 if (!drvdata)
888 return -ENOMEM;
889 platform_set_drvdata(pdev, drvdata);
890
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100891 match = of_match_device(dev->driver->of_match_table, dev);
892 if (!match || !match->data)
893 return -EINVAL;
894
895 drvdata->variant = (struct lpass_variant *)match->data;
896 variant = drvdata->variant;
897
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200898 of_lpass_cpu_parse_dai_data(dev, drvdata);
899
Yang Yingliang77b7bae2021-06-15 22:07:11 +0800900 drvdata->lpaif = devm_platform_ioremap_resource_byname(pdev, "lpass-lpaif");
Stephen Boyd4e15f502021-01-14 19:43:27 -0800901 if (IS_ERR(drvdata->lpaif))
Stephen Boyde697df62021-01-14 19:43:26 -0800902 return PTR_ERR(drvdata->lpaif);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800903
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000904 lpass_cpu_regmap_config.max_register = LPAIF_WRDMAPER_REG(variant,
905 variant->wrdma_channels +
906 variant->wrdma_channel_start);
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100907
Tang Bin952c0e22020-05-04 14:59:47 +0800908 drvdata->lpaif_map = devm_regmap_init_mmio(dev, drvdata->lpaif,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800909 &lpass_cpu_regmap_config);
910 if (IS_ERR(drvdata->lpaif_map)) {
Tang Bin952c0e22020-05-04 14:59:47 +0800911 dev_err(dev, "error initializing regmap: %ld\n",
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800912 PTR_ERR(drvdata->lpaif_map));
Kenneth Westfield80beab82015-03-03 16:21:54 -0800913 return PTR_ERR(drvdata->lpaif_map);
914 }
915
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530916 if (drvdata->hdmi_port_enable) {
Yang Yingliang77b7bae2021-06-15 22:07:11 +0800917 drvdata->hdmiif = devm_platform_ioremap_resource_byname(pdev, "lpass-hdmiif");
Stephen Boyd4e15f502021-01-14 19:43:27 -0800918 if (IS_ERR(drvdata->hdmiif))
Stephen Boyde697df62021-01-14 19:43:26 -0800919 return PTR_ERR(drvdata->hdmiif);
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530920
921 lpass_hdmi_regmap_config.max_register = LPAIF_HDMI_RDMAPER_REG(variant,
Srinivasa Rao Mandadapue681b1a2021-02-02 11:57:27 +0530922 variant->hdmi_rdma_channels - 1);
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530923 drvdata->hdmiif_map = devm_regmap_init_mmio(dev, drvdata->hdmiif,
924 &lpass_hdmi_regmap_config);
925 if (IS_ERR(drvdata->hdmiif_map)) {
926 dev_err(dev, "error initializing regmap: %ld\n",
927 PTR_ERR(drvdata->hdmiif_map));
928 return PTR_ERR(drvdata->hdmiif_map);
929 }
930 }
931
Rohit kumara5035672020-08-14 16:22:58 +0530932 if (variant->init) {
933 ret = variant->init(pdev);
934 if (ret) {
935 dev_err(dev, "error initializing variant: %d\n", ret);
936 return ret;
937 }
938 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800939
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100940 for (i = 0; i < variant->num_dai; i++) {
941 dai_id = variant->dai_driver[i].id;
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530942 if (dai_id == LPASS_DP_RX)
943 continue;
944
Stephen Boydaf270252021-05-19 18:48:07 -0700945 drvdata->mi2s_osr_clk[dai_id] = devm_clk_get_optional(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200946 variant->dai_osr_clk_names[i]);
Tang Bin952c0e22020-05-04 14:59:47 +0800947 drvdata->mi2s_bit_clk[dai_id] = devm_clk_get(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200948 variant->dai_bit_clk_names[i]);
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100949 if (IS_ERR(drvdata->mi2s_bit_clk[dai_id])) {
Tang Bin952c0e22020-05-04 14:59:47 +0800950 dev_err(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200951 "error getting %s: %ld\n",
952 variant->dai_bit_clk_names[i],
Julia Lawall89857292015-09-17 10:47:33 +0200953 PTR_ERR(drvdata->mi2s_bit_clk[dai_id]));
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100954 return PTR_ERR(drvdata->mi2s_bit_clk[dai_id]);
955 }
Srinivasa Rao Mandadapuc223f412021-06-09 19:00:39 +0530956 if (drvdata->mi2s_playback_sd_mode[dai_id] ==
957 LPAIF_I2SCTL_MODE_QUAD01) {
958 variant->dai_driver[dai_id].playback.channels_min = 4;
959 variant->dai_driver[dai_id].playback.channels_max = 4;
960 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800961 }
962
Rohit kumarb5022a32020-08-14 16:23:01 +0530963 /* Allocation for i2sctl regmap fields */
964 drvdata->i2sctl = devm_kzalloc(&pdev->dev, sizeof(struct lpaif_i2sctl),
965 GFP_KERNEL);
966
967 /* Initialize bitfields for dai I2SCTL register */
968 ret = lpass_cpu_init_i2sctl_bitfields(dev, drvdata->i2sctl,
969 drvdata->lpaif_map);
970 if (ret) {
971 dev_err(dev, "error init i2sctl field: %d\n", ret);
972 return ret;
973 }
974
V Sujith Kumar Reddy7cb37b72020-10-08 10:47:01 +0530975 if (drvdata->hdmi_port_enable) {
976 ret = lpass_hdmi_init_bitfields(dev, drvdata->hdmiif_map);
977 if (ret) {
978 dev_err(dev, "%s error hdmi init failed\n", __func__);
979 return ret;
980 }
981 }
Tang Bin952c0e22020-05-04 14:59:47 +0800982 ret = devm_snd_soc_register_component(dev,
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100983 &lpass_cpu_comp_driver,
984 variant->dai_driver,
985 variant->num_dai);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800986 if (ret) {
Tang Bin952c0e22020-05-04 14:59:47 +0800987 dev_err(dev, "error registering cpu driver: %d\n", ret);
Rohit kumara5035672020-08-14 16:22:58 +0530988 goto err;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800989 }
990
991 ret = asoc_qcom_lpass_platform_register(pdev);
992 if (ret) {
Tang Bin952c0e22020-05-04 14:59:47 +0800993 dev_err(dev, "error registering platform driver: %d\n", ret);
Rohit kumara5035672020-08-14 16:22:58 +0530994 goto err;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800995 }
996
Rohit kumara5035672020-08-14 16:22:58 +0530997err:
Kenneth Westfield80beab82015-03-03 16:21:54 -0800998 return ret;
999}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +01001000EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -08001001
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +01001002int asoc_qcom_lpass_cpu_platform_remove(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -08001003{
1004 struct lpass_data *drvdata = platform_get_drvdata(pdev);
1005
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +01001006 if (drvdata->variant->exit)
1007 drvdata->variant->exit(pdev);
1008
Kenneth Westfield80beab82015-03-03 16:21:54 -08001009
1010 return 0;
1011}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +01001012EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_remove);
Srinivas Kandagatla94201792016-10-31 11:25:45 +00001013
V Sujith Kumar Reddy60a97382020-11-14 00:08:22 +05301014void asoc_qcom_lpass_cpu_platform_shutdown(struct platform_device *pdev)
1015{
1016 struct lpass_data *drvdata = platform_get_drvdata(pdev);
1017
1018 if (drvdata->variant->exit)
1019 drvdata->variant->exit(pdev);
1020
1021}
1022EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_shutdown);
1023
Srinivas Kandagatla94201792016-10-31 11:25:45 +00001024MODULE_DESCRIPTION("QTi LPASS CPU Driver");
1025MODULE_LICENSE("GPL v2");