blob: 0718a0fff9cb676a05d55b290c59afdb8ccda188 [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
Rohit kumarb5022a32020-08-14 16:23:01 +053032static int lpass_cpu_init_i2sctl_bitfields(struct device *dev,
33 struct lpaif_i2sctl *i2sctl, struct regmap *map)
34{
35 struct lpass_data *drvdata = dev_get_drvdata(dev);
36 struct lpass_variant *v = drvdata->variant;
37
38 i2sctl->loopback = devm_regmap_field_alloc(dev, map, v->loopback);
39 i2sctl->spken = devm_regmap_field_alloc(dev, map, v->spken);
40 i2sctl->spkmode = devm_regmap_field_alloc(dev, map, v->spkmode);
41 i2sctl->spkmono = devm_regmap_field_alloc(dev, map, v->spkmono);
42 i2sctl->micen = devm_regmap_field_alloc(dev, map, v->micen);
43 i2sctl->micmode = devm_regmap_field_alloc(dev, map, v->micmode);
44 i2sctl->micmono = devm_regmap_field_alloc(dev, map, v->micmono);
45 i2sctl->wssrc = devm_regmap_field_alloc(dev, map, v->wssrc);
46 i2sctl->bitwidth = devm_regmap_field_alloc(dev, map, v->bitwidth);
47
48 if (IS_ERR(i2sctl->loopback) || IS_ERR(i2sctl->spken) ||
49 IS_ERR(i2sctl->spkmode) || IS_ERR(i2sctl->spkmono) ||
50 IS_ERR(i2sctl->micen) || IS_ERR(i2sctl->micmode) ||
51 IS_ERR(i2sctl->micmono) || IS_ERR(i2sctl->wssrc) ||
52 IS_ERR(i2sctl->bitwidth))
53 return -EINVAL;
54
55 return 0;
56}
57
Kenneth Westfield80beab82015-03-03 16:21:54 -080058static int lpass_cpu_daiops_set_sysclk(struct snd_soc_dai *dai, int clk_id,
59 unsigned int freq, int dir)
60{
61 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
62 int ret;
63
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010064 ret = clk_set_rate(drvdata->mi2s_osr_clk[dai->driver->id], freq);
Kenneth Westfield80beab82015-03-03 16:21:54 -080065 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080066 dev_err(dai->dev, "error setting mi2s osrclk to %u: %d\n",
67 freq, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -080068
69 return ret;
70}
71
72static int lpass_cpu_daiops_startup(struct snd_pcm_substream *substream,
73 struct snd_soc_dai *dai)
74{
75 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
76 int ret;
77
Bjorn Andersson46dccc32017-01-30 13:03:36 -080078 ret = clk_prepare_enable(drvdata->mi2s_osr_clk[dai->driver->id]);
79 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080080 dev_err(dai->dev, "error in enabling mi2s osr clk: %d\n", ret);
Bjorn Andersson46dccc32017-01-30 13:03:36 -080081 return ret;
Kenneth Westfield80beab82015-03-03 16:21:54 -080082 }
Kenneth Westfield80beab82015-03-03 16:21:54 -080083 return 0;
84}
85
86static void lpass_cpu_daiops_shutdown(struct snd_pcm_substream *substream,
87 struct snd_soc_dai *dai)
88{
89 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
90
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010091 clk_disable_unprepare(drvdata->mi2s_bit_clk[dai->driver->id]);
Srinivas Kandagatla3e53ac82015-05-21 22:52:57 +010092
Bjorn Andersson46dccc32017-01-30 13:03:36 -080093 clk_disable_unprepare(drvdata->mi2s_osr_clk[dai->driver->id]);
Kenneth Westfield80beab82015-03-03 16:21:54 -080094}
95
96static int lpass_cpu_daiops_hw_params(struct snd_pcm_substream *substream,
97 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
98{
99 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Rohit kumarb5022a32020-08-14 16:23:01 +0530100 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
101 unsigned int id = dai->driver->id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800102 snd_pcm_format_t format = params_format(params);
103 unsigned int channels = params_channels(params);
104 unsigned int rate = params_rate(params);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200105 unsigned int mode;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800106 unsigned int regval;
107 int bitwidth, ret;
108
109 bitwidth = snd_pcm_format_width(format);
110 if (bitwidth < 0) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800111 dev_err(dai->dev, "invalid bit width given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800112 return bitwidth;
113 }
114
Rohit kumarb5022a32020-08-14 16:23:01 +0530115 ret = regmap_fields_write(i2sctl->loopback, id,
116 LPAIF_I2SCTL_LOOPBACK_DISABLE);
117 if (ret) {
118 dev_err(dai->dev, "error updating loopback field: %d\n", ret);
119 return ret;
120 }
121
122 ret = regmap_fields_write(i2sctl->wssrc, id,
123 LPAIF_I2SCTL_WSSRC_INTERNAL);
124 if (ret) {
125 dev_err(dai->dev, "error updating wssrc field: %d\n", ret);
126 return ret;
127 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800128
129 switch (bitwidth) {
130 case 16:
Rohit kumarb5022a32020-08-14 16:23:01 +0530131 regval = LPAIF_I2SCTL_BITWIDTH_16;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800132 break;
133 case 24:
Rohit kumarb5022a32020-08-14 16:23:01 +0530134 regval = LPAIF_I2SCTL_BITWIDTH_24;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800135 break;
136 case 32:
Rohit kumarb5022a32020-08-14 16:23:01 +0530137 regval = LPAIF_I2SCTL_BITWIDTH_32;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800138 break;
139 default:
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800140 dev_err(dai->dev, "invalid bitwidth given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800141 return -EINVAL;
142 }
143
Rohit kumarb5022a32020-08-14 16:23:01 +0530144 ret = regmap_fields_write(i2sctl->bitwidth, id, regval);
145 if (ret) {
146 dev_err(dai->dev, "error updating bitwidth field: %d\n", ret);
147 return ret;
148 }
149
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Rohit kumarb5022a32020-08-14 16:23:01 +0530151 mode = drvdata->mi2s_playback_sd_mode[id];
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200152 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530153 mode = drvdata->mi2s_capture_sd_mode[id];
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200154
155 if (!mode) {
156 dev_err(dai->dev, "no line is assigned\n");
157 return -EINVAL;
158 }
159
160 switch (channels) {
161 case 1:
162 case 2:
163 switch (mode) {
164 case LPAIF_I2SCTL_MODE_QUAD01:
165 case LPAIF_I2SCTL_MODE_6CH:
166 case LPAIF_I2SCTL_MODE_8CH:
167 mode = LPAIF_I2SCTL_MODE_SD0;
168 break;
169 case LPAIF_I2SCTL_MODE_QUAD23:
170 mode = LPAIF_I2SCTL_MODE_SD2;
171 break;
172 }
173
174 break;
175 case 4:
176 if (mode < LPAIF_I2SCTL_MODE_QUAD01) {
177 dev_err(dai->dev, "cannot configure 4 channels with mode %d\n",
178 mode);
179 return -EINVAL;
180 }
181
182 switch (mode) {
183 case LPAIF_I2SCTL_MODE_6CH:
184 case LPAIF_I2SCTL_MODE_8CH:
185 mode = LPAIF_I2SCTL_MODE_QUAD01;
186 break;
187 }
188 break;
189 case 6:
190 if (mode < LPAIF_I2SCTL_MODE_6CH) {
191 dev_err(dai->dev, "cannot configure 6 channels with mode %d\n",
192 mode);
193 return -EINVAL;
194 }
195
196 switch (mode) {
197 case LPAIF_I2SCTL_MODE_8CH:
198 mode = LPAIF_I2SCTL_MODE_6CH;
199 break;
200 }
201 break;
202 case 8:
203 if (mode < LPAIF_I2SCTL_MODE_8CH) {
204 dev_err(dai->dev, "cannot configure 8 channels with mode %d\n",
205 mode);
206 return -EINVAL;
207 }
208 break;
209 default:
210 dev_err(dai->dev, "invalid channels given: %u\n", channels);
211 return -EINVAL;
212 }
213
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530215 ret = regmap_fields_write(i2sctl->spkmode, id,
216 LPAIF_I2SCTL_SPKMODE(mode));
217 if (ret) {
218 dev_err(dai->dev, "error writing to i2sctl spkr mode: %d\n",
219 ret);
220 return ret;
221 }
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200222 if (channels >= 2)
Rohit kumarb5022a32020-08-14 16:23:01 +0530223 ret = regmap_fields_write(i2sctl->spkmono, id,
224 LPAIF_I2SCTL_SPKMONO_STEREO);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200225 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530226 ret = regmap_fields_write(i2sctl->spkmono, id,
227 LPAIF_I2SCTL_SPKMONO_MONO);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000228 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530229 ret = regmap_fields_write(i2sctl->micmode, id,
230 LPAIF_I2SCTL_MICMODE(mode));
231 if (ret) {
232 dev_err(dai->dev, "error writing to i2sctl mic mode: %d\n",
233 ret);
234 return ret;
235 }
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200236 if (channels >= 2)
Rohit kumarb5022a32020-08-14 16:23:01 +0530237 ret = regmap_fields_write(i2sctl->micmono, id,
238 LPAIF_I2SCTL_MICMONO_STEREO);
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200239 else
Rohit kumarb5022a32020-08-14 16:23:01 +0530240 ret = regmap_fields_write(i2sctl->micmono, id,
241 LPAIF_I2SCTL_MICMONO_MONO);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800242 }
243
Kenneth Westfield80beab82015-03-03 16:21:54 -0800244 if (ret) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530245 dev_err(dai->dev, "error writing to i2sctl channels mode: %d\n",
246 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800247 return ret;
248 }
249
Rohit kumarb5022a32020-08-14 16:23:01 +0530250 ret = clk_set_rate(drvdata->mi2s_bit_clk[id],
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100251 rate * bitwidth * 2);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800252 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800253 dev_err(dai->dev, "error setting mi2s bitclk to %u: %d\n",
254 rate * bitwidth * 2, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800255 return ret;
256 }
257
258 return 0;
259}
260
Kenneth Westfield80beab82015-03-03 16:21:54 -0800261static int lpass_cpu_daiops_prepare(struct snd_pcm_substream *substream,
262 struct snd_soc_dai *dai)
263{
264 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Rohit kumarb5022a32020-08-14 16:23:01 +0530265 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
266 unsigned int id = dai->driver->id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800267 int ret;
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000268
269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530270 ret = regmap_fields_write(i2sctl->spken, id,
271 LPAIF_I2SCTL_SPKEN_ENABLE);
272 } else {
273 ret = regmap_fields_write(i2sctl->micen, id,
274 LPAIF_I2SCTL_MICEN_ENABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000275 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800276
Kenneth Westfield80beab82015-03-03 16:21:54 -0800277 if (ret)
Rohit kumarb5022a32020-08-14 16:23:01 +0530278 dev_err(dai->dev, "error writing to i2sctl enable: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800279
280 return ret;
281}
282
283static int lpass_cpu_daiops_trigger(struct snd_pcm_substream *substream,
284 int cmd, struct snd_soc_dai *dai)
285{
286 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Rohit kumarb5022a32020-08-14 16:23:01 +0530287 struct lpaif_i2sctl *i2sctl = drvdata->i2sctl;
288 unsigned int id = dai->driver->id;
Takashi Iwai96f05be2015-04-13 14:23:29 +0200289 int ret = -EINVAL;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800290
291 switch (cmd) {
292 case SNDRV_PCM_TRIGGER_START:
293 case SNDRV_PCM_TRIGGER_RESUME:
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000295 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530296 ret = regmap_fields_write(i2sctl->spken, id,
297 LPAIF_I2SCTL_SPKEN_ENABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000298 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530299 ret = regmap_fields_write(i2sctl->micen, id,
300 LPAIF_I2SCTL_MICEN_ENABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000301 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800302 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800303 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
304 ret);
V Sujith Kumar Reddy7e6799d2020-09-18 22:24:33 +0530305
306 ret = clk_prepare_enable(drvdata->mi2s_bit_clk[id]);
307 if (ret) {
308 dev_err(dai->dev, "error in enabling mi2s bit clk: %d\n", ret);
309 clk_disable_unprepare(drvdata->mi2s_osr_clk[id]);
310 return ret;
311 }
312
Kenneth Westfield80beab82015-03-03 16:21:54 -0800313 break;
314 case SNDRV_PCM_TRIGGER_STOP:
315 case SNDRV_PCM_TRIGGER_SUSPEND:
316 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000317 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Rohit kumarb5022a32020-08-14 16:23:01 +0530318 ret = regmap_fields_write(i2sctl->spken, id,
319 LPAIF_I2SCTL_SPKEN_DISABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000320 } else {
Rohit kumarb5022a32020-08-14 16:23:01 +0530321 ret = regmap_fields_write(i2sctl->micen, id,
322 LPAIF_I2SCTL_MICEN_DISABLE);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000323 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800324 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800325 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
326 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800327 break;
328 }
329
330 return ret;
331}
332
Axel Lin618718d2015-08-28 10:53:31 +0800333const struct snd_soc_dai_ops asoc_qcom_lpass_cpu_dai_ops = {
Kenneth Westfield80beab82015-03-03 16:21:54 -0800334 .set_sysclk = lpass_cpu_daiops_set_sysclk,
335 .startup = lpass_cpu_daiops_startup,
336 .shutdown = lpass_cpu_daiops_shutdown,
337 .hw_params = lpass_cpu_daiops_hw_params,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800338 .prepare = lpass_cpu_daiops_prepare,
339 .trigger = lpass_cpu_daiops_trigger,
340};
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100341EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_ops);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800342
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100343int asoc_qcom_lpass_cpu_dai_probe(struct snd_soc_dai *dai)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800344{
345 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
346 int ret;
347
348 /* ensure audio hardware is disabled */
349 ret = regmap_write(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100350 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id), 0);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800351 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800352 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800353
354 return ret;
355}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100356EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800357
358static const struct snd_soc_component_driver lpass_cpu_comp_driver = {
359 .name = "lpass-cpu",
360};
361
362static bool lpass_cpu_regmap_writeable(struct device *dev, unsigned int reg)
363{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100364 struct lpass_data *drvdata = dev_get_drvdata(dev);
365 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800366 int i;
367
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100368 for (i = 0; i < v->i2s_ports; ++i)
369 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800370 return true;
371
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100372 for (i = 0; i < v->irq_ports; ++i) {
373 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800374 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100375 if (reg == LPAIF_IRQCLEAR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800376 return true;
377 }
378
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100379 for (i = 0; i < v->rdma_channels; ++i) {
380 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800381 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100382 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800383 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100384 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800385 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100386 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800387 return true;
388 }
389
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000390 for (i = 0; i < v->wrdma_channels; ++i) {
391 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
392 return true;
393 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
394 return true;
395 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
396 return true;
397 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
398 return true;
399 }
400
Kenneth Westfield80beab82015-03-03 16:21:54 -0800401 return false;
402}
403
404static bool lpass_cpu_regmap_readable(struct device *dev, unsigned int reg)
405{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100406 struct lpass_data *drvdata = dev_get_drvdata(dev);
407 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800408 int i;
409
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100410 for (i = 0; i < v->i2s_ports; ++i)
411 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800412 return true;
413
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100414 for (i = 0; i < v->irq_ports; ++i) {
415 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800416 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100417 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800418 return true;
419 }
420
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100421 for (i = 0; i < v->rdma_channels; ++i) {
422 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800423 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100424 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800425 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100426 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800427 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100428 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800429 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100430 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800431 return true;
432 }
433
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000434 for (i = 0; i < v->wrdma_channels; ++i) {
435 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
436 return true;
437 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
438 return true;
439 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
440 return true;
441 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
442 return true;
443 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
444 return true;
445 }
446
Kenneth Westfield80beab82015-03-03 16:21:54 -0800447 return false;
448}
449
450static bool lpass_cpu_regmap_volatile(struct device *dev, unsigned int reg)
451{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100452 struct lpass_data *drvdata = dev_get_drvdata(dev);
453 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800454 int i;
455
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100456 for (i = 0; i < v->irq_ports; ++i)
457 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800458 return true;
459
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100460 for (i = 0; i < v->rdma_channels; ++i)
461 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800462 return true;
463
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000464 for (i = 0; i < v->wrdma_channels; ++i)
465 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
466 return true;
467
Kenneth Westfield80beab82015-03-03 16:21:54 -0800468 return false;
469}
470
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100471static struct regmap_config lpass_cpu_regmap_config = {
Kenneth Westfield80beab82015-03-03 16:21:54 -0800472 .reg_bits = 32,
473 .reg_stride = 4,
474 .val_bits = 32,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800475 .writeable_reg = lpass_cpu_regmap_writeable,
476 .readable_reg = lpass_cpu_regmap_readable,
477 .volatile_reg = lpass_cpu_regmap_volatile,
478 .cache_type = REGCACHE_FLAT,
479};
480
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200481static unsigned int of_lpass_cpu_parse_sd_lines(struct device *dev,
482 struct device_node *node,
483 const char *name)
484{
485 unsigned int lines[LPASS_CPU_MAX_MI2S_LINES];
486 unsigned int sd_line_mask = 0;
487 int num_lines, i;
488
489 num_lines = of_property_read_variable_u32_array(node, name, lines, 0,
490 LPASS_CPU_MAX_MI2S_LINES);
491 if (num_lines < 0)
492 return LPAIF_I2SCTL_MODE_NONE;
493
494 for (i = 0; i < num_lines; i++)
495 sd_line_mask |= BIT(lines[i]);
496
497 switch (sd_line_mask) {
498 case LPASS_CPU_I2S_SD0_MASK:
499 return LPAIF_I2SCTL_MODE_SD0;
500 case LPASS_CPU_I2S_SD1_MASK:
501 return LPAIF_I2SCTL_MODE_SD1;
502 case LPASS_CPU_I2S_SD2_MASK:
503 return LPAIF_I2SCTL_MODE_SD2;
504 case LPASS_CPU_I2S_SD3_MASK:
505 return LPAIF_I2SCTL_MODE_SD3;
506 case LPASS_CPU_I2S_SD0_1_MASK:
507 return LPAIF_I2SCTL_MODE_QUAD01;
508 case LPASS_CPU_I2S_SD2_3_MASK:
509 return LPAIF_I2SCTL_MODE_QUAD23;
510 case LPASS_CPU_I2S_SD0_1_2_MASK:
511 return LPAIF_I2SCTL_MODE_6CH;
512 case LPASS_CPU_I2S_SD0_1_2_3_MASK:
513 return LPAIF_I2SCTL_MODE_8CH;
514 default:
515 dev_err(dev, "Unsupported SD line mask: %#x\n", sd_line_mask);
516 return LPAIF_I2SCTL_MODE_NONE;
517 }
518}
519
520static void of_lpass_cpu_parse_dai_data(struct device *dev,
521 struct lpass_data *data)
522{
523 struct device_node *node;
524 int ret, id;
525
526 /* Allow all channels by default for backwards compatibility */
527 for (id = 0; id < data->variant->num_dai; id++) {
528 data->mi2s_playback_sd_mode[id] = LPAIF_I2SCTL_MODE_8CH;
529 data->mi2s_capture_sd_mode[id] = LPAIF_I2SCTL_MODE_8CH;
530 }
531
532 for_each_child_of_node(dev->of_node, node) {
533 ret = of_property_read_u32(node, "reg", &id);
534 if (ret || id < 0 || id >= data->variant->num_dai) {
535 dev_err(dev, "valid dai id not found: %d\n", ret);
536 continue;
537 }
538
539 data->mi2s_playback_sd_mode[id] =
540 of_lpass_cpu_parse_sd_lines(dev, node,
541 "qcom,playback-sd-lines");
542 data->mi2s_capture_sd_mode[id] =
543 of_lpass_cpu_parse_sd_lines(dev, node,
544 "qcom,capture-sd-lines");
545 }
546}
547
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100548int asoc_qcom_lpass_cpu_platform_probe(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800549{
550 struct lpass_data *drvdata;
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700551 struct device_node *dsp_of_node;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800552 struct resource *res;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100553 struct lpass_variant *variant;
554 struct device *dev = &pdev->dev;
555 const struct of_device_id *match;
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100556 int ret, i, dai_id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800557
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700558 dsp_of_node = of_parse_phandle(pdev->dev.of_node, "qcom,adsp", 0);
559 if (dsp_of_node) {
Tang Bin952c0e22020-05-04 14:59:47 +0800560 dev_err(dev, "DSP exists and holds audio resources\n");
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700561 return -EBUSY;
562 }
563
Tang Bin952c0e22020-05-04 14:59:47 +0800564 drvdata = devm_kzalloc(dev, sizeof(struct lpass_data), GFP_KERNEL);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800565 if (!drvdata)
566 return -ENOMEM;
567 platform_set_drvdata(pdev, drvdata);
568
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100569 match = of_match_device(dev->driver->of_match_table, dev);
570 if (!match || !match->data)
571 return -EINVAL;
572
573 drvdata->variant = (struct lpass_variant *)match->data;
574 variant = drvdata->variant;
575
Stephan Gerhold4ff028f62020-04-25 20:46:57 +0200576 of_lpass_cpu_parse_dai_data(dev, drvdata);
577
Rohit kumar93dbbd62020-08-14 16:23:06 +0530578 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800579
Tang Bin952c0e22020-05-04 14:59:47 +0800580 drvdata->lpaif = devm_ioremap_resource(dev, res);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800581 if (IS_ERR((void const __force *)drvdata->lpaif)) {
Tang Bin952c0e22020-05-04 14:59:47 +0800582 dev_err(dev, "error mapping reg resource: %ld\n",
Kenneth Westfield80beab82015-03-03 16:21:54 -0800583 PTR_ERR((void const __force *)drvdata->lpaif));
584 return PTR_ERR((void const __force *)drvdata->lpaif);
585 }
586
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000587 lpass_cpu_regmap_config.max_register = LPAIF_WRDMAPER_REG(variant,
588 variant->wrdma_channels +
589 variant->wrdma_channel_start);
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100590
Tang Bin952c0e22020-05-04 14:59:47 +0800591 drvdata->lpaif_map = devm_regmap_init_mmio(dev, drvdata->lpaif,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800592 &lpass_cpu_regmap_config);
593 if (IS_ERR(drvdata->lpaif_map)) {
Tang Bin952c0e22020-05-04 14:59:47 +0800594 dev_err(dev, "error initializing regmap: %ld\n",
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800595 PTR_ERR(drvdata->lpaif_map));
Kenneth Westfield80beab82015-03-03 16:21:54 -0800596 return PTR_ERR(drvdata->lpaif_map);
597 }
598
Rohit kumara5035672020-08-14 16:22:58 +0530599 if (variant->init) {
600 ret = variant->init(pdev);
601 if (ret) {
602 dev_err(dev, "error initializing variant: %d\n", ret);
603 return ret;
604 }
605 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800606
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100607 for (i = 0; i < variant->num_dai; i++) {
608 dai_id = variant->dai_driver[i].id;
Tang Bin952c0e22020-05-04 14:59:47 +0800609 drvdata->mi2s_osr_clk[dai_id] = devm_clk_get(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200610 variant->dai_osr_clk_names[i]);
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100611 if (IS_ERR(drvdata->mi2s_osr_clk[dai_id])) {
Tang Bin952c0e22020-05-04 14:59:47 +0800612 dev_warn(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200613 "%s() error getting optional %s: %ld\n",
614 __func__,
615 variant->dai_osr_clk_names[i],
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100616 PTR_ERR(drvdata->mi2s_osr_clk[dai_id]));
Bjorn Andersson46dccc32017-01-30 13:03:36 -0800617
618 drvdata->mi2s_osr_clk[dai_id] = NULL;
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100619 }
620
Tang Bin952c0e22020-05-04 14:59:47 +0800621 drvdata->mi2s_bit_clk[dai_id] = devm_clk_get(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200622 variant->dai_bit_clk_names[i]);
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100623 if (IS_ERR(drvdata->mi2s_bit_clk[dai_id])) {
Tang Bin952c0e22020-05-04 14:59:47 +0800624 dev_err(dev,
Linus Walleij97c52eb2017-04-05 10:34:10 +0200625 "error getting %s: %ld\n",
626 variant->dai_bit_clk_names[i],
Julia Lawall89857292015-09-17 10:47:33 +0200627 PTR_ERR(drvdata->mi2s_bit_clk[dai_id]));
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100628 return PTR_ERR(drvdata->mi2s_bit_clk[dai_id]);
629 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800630 }
631
Rohit kumarb5022a32020-08-14 16:23:01 +0530632 /* Allocation for i2sctl regmap fields */
633 drvdata->i2sctl = devm_kzalloc(&pdev->dev, sizeof(struct lpaif_i2sctl),
634 GFP_KERNEL);
635
636 /* Initialize bitfields for dai I2SCTL register */
637 ret = lpass_cpu_init_i2sctl_bitfields(dev, drvdata->i2sctl,
638 drvdata->lpaif_map);
639 if (ret) {
640 dev_err(dev, "error init i2sctl field: %d\n", ret);
641 return ret;
642 }
643
Tang Bin952c0e22020-05-04 14:59:47 +0800644 ret = devm_snd_soc_register_component(dev,
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100645 &lpass_cpu_comp_driver,
646 variant->dai_driver,
647 variant->num_dai);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800648 if (ret) {
Tang Bin952c0e22020-05-04 14:59:47 +0800649 dev_err(dev, "error registering cpu driver: %d\n", ret);
Rohit kumara5035672020-08-14 16:22:58 +0530650 goto err;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800651 }
652
653 ret = asoc_qcom_lpass_platform_register(pdev);
654 if (ret) {
Tang Bin952c0e22020-05-04 14:59:47 +0800655 dev_err(dev, "error registering platform driver: %d\n", ret);
Rohit kumara5035672020-08-14 16:22:58 +0530656 goto err;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800657 }
658
Rohit kumara5035672020-08-14 16:22:58 +0530659err:
Kenneth Westfield80beab82015-03-03 16:21:54 -0800660 return ret;
661}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100662EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800663
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100664int asoc_qcom_lpass_cpu_platform_remove(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800665{
666 struct lpass_data *drvdata = platform_get_drvdata(pdev);
667
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100668 if (drvdata->variant->exit)
669 drvdata->variant->exit(pdev);
670
Kenneth Westfield80beab82015-03-03 16:21:54 -0800671
672 return 0;
673}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100674EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_remove);
Srinivas Kandagatla94201792016-10-31 11:25:45 +0000675
676MODULE_DESCRIPTION("QTi LPASS CPU Driver");
677MODULE_LICENSE("GPL v2");