blob: 5202a584e0c6fcb021708ddd65844dd5e57dc7e2 [file] [log] [blame]
Kenneth Westfield80beab82015-03-03 16:21:54 -08001/*
2 * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * lpass-cpu.c -- ALSA SoC CPU DAI driver for QTi LPASS
14 */
15
16#include <linux/clk.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -080017#include <linux/kernel.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -080018#include <linux/module.h>
19#include <linux/of.h>
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +010020#include <linux/of_device.h>
Kenneth Westfield80beab82015-03-03 16:21:54 -080021#include <linux/platform_device.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <linux/regmap.h>
25#include <sound/soc.h>
26#include <sound/soc-dai.h>
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +010027#include "lpass-lpaif-reg.h"
Kenneth Westfield80beab82015-03-03 16:21:54 -080028#include "lpass.h"
29
30static int lpass_cpu_daiops_set_sysclk(struct snd_soc_dai *dai, int clk_id,
31 unsigned int freq, int dir)
32{
33 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
34 int ret;
35
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010036 ret = clk_set_rate(drvdata->mi2s_osr_clk[dai->driver->id], freq);
Kenneth Westfield80beab82015-03-03 16:21:54 -080037 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080038 dev_err(dai->dev, "error setting mi2s osrclk to %u: %d\n",
39 freq, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -080040
41 return ret;
42}
43
44static int lpass_cpu_daiops_startup(struct snd_pcm_substream *substream,
45 struct snd_soc_dai *dai)
46{
47 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
48 int ret;
49
Bjorn Andersson46dccc32017-01-30 13:03:36 -080050 ret = clk_prepare_enable(drvdata->mi2s_osr_clk[dai->driver->id]);
51 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080052 dev_err(dai->dev, "error in enabling mi2s osr clk: %d\n", ret);
Bjorn Andersson46dccc32017-01-30 13:03:36 -080053 return ret;
Kenneth Westfield80beab82015-03-03 16:21:54 -080054 }
55
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010056 ret = clk_prepare_enable(drvdata->mi2s_bit_clk[dai->driver->id]);
Kenneth Westfield80beab82015-03-03 16:21:54 -080057 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080058 dev_err(dai->dev, "error in enabling mi2s bit clk: %d\n", ret);
Bjorn Andersson46dccc32017-01-30 13:03:36 -080059 clk_disable_unprepare(drvdata->mi2s_osr_clk[dai->driver->id]);
Kenneth Westfield80beab82015-03-03 16:21:54 -080060 return ret;
61 }
62
63 return 0;
64}
65
66static void lpass_cpu_daiops_shutdown(struct snd_pcm_substream *substream,
67 struct snd_soc_dai *dai)
68{
69 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
70
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +010071 clk_disable_unprepare(drvdata->mi2s_bit_clk[dai->driver->id]);
Srinivas Kandagatla3e53ac82015-05-21 22:52:57 +010072
Bjorn Andersson46dccc32017-01-30 13:03:36 -080073 clk_disable_unprepare(drvdata->mi2s_osr_clk[dai->driver->id]);
Kenneth Westfield80beab82015-03-03 16:21:54 -080074}
75
76static int lpass_cpu_daiops_hw_params(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
78{
79 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
80 snd_pcm_format_t format = params_format(params);
81 unsigned int channels = params_channels(params);
82 unsigned int rate = params_rate(params);
83 unsigned int regval;
84 int bitwidth, ret;
85
86 bitwidth = snd_pcm_format_width(format);
87 if (bitwidth < 0) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -080088 dev_err(dai->dev, "invalid bit width given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -080089 return bitwidth;
90 }
91
92 regval = LPAIF_I2SCTL_LOOPBACK_DISABLE |
93 LPAIF_I2SCTL_WSSRC_INTERNAL;
94
95 switch (bitwidth) {
96 case 16:
97 regval |= LPAIF_I2SCTL_BITWIDTH_16;
98 break;
99 case 24:
100 regval |= LPAIF_I2SCTL_BITWIDTH_24;
101 break;
102 case 32:
103 regval |= LPAIF_I2SCTL_BITWIDTH_32;
104 break;
105 default:
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800106 dev_err(dai->dev, "invalid bitwidth given: %d\n", bitwidth);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800107 return -EINVAL;
108 }
109
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000110 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
111 switch (channels) {
112 case 1:
113 regval |= LPAIF_I2SCTL_SPKMODE_SD0;
114 regval |= LPAIF_I2SCTL_SPKMONO_MONO;
115 break;
116 case 2:
117 regval |= LPAIF_I2SCTL_SPKMODE_SD0;
118 regval |= LPAIF_I2SCTL_SPKMONO_STEREO;
119 break;
120 case 4:
121 regval |= LPAIF_I2SCTL_SPKMODE_QUAD01;
122 regval |= LPAIF_I2SCTL_SPKMONO_STEREO;
123 break;
124 case 6:
125 regval |= LPAIF_I2SCTL_SPKMODE_6CH;
126 regval |= LPAIF_I2SCTL_SPKMONO_STEREO;
127 break;
128 case 8:
129 regval |= LPAIF_I2SCTL_SPKMODE_8CH;
130 regval |= LPAIF_I2SCTL_SPKMONO_STEREO;
131 break;
132 default:
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800133 dev_err(dai->dev, "invalid channels given: %u\n",
134 channels);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000135 return -EINVAL;
136 }
137 } else {
138 switch (channels) {
139 case 1:
140 regval |= LPAIF_I2SCTL_MICMODE_SD0;
141 regval |= LPAIF_I2SCTL_MICMONO_MONO;
142 break;
143 case 2:
144 regval |= LPAIF_I2SCTL_MICMODE_SD0;
145 regval |= LPAIF_I2SCTL_MICMONO_STEREO;
146 break;
147 case 4:
148 regval |= LPAIF_I2SCTL_MICMODE_QUAD01;
149 regval |= LPAIF_I2SCTL_MICMONO_STEREO;
150 break;
151 case 6:
152 regval |= LPAIF_I2SCTL_MICMODE_6CH;
153 regval |= LPAIF_I2SCTL_MICMONO_STEREO;
154 break;
155 case 8:
156 regval |= LPAIF_I2SCTL_MICMODE_8CH;
157 regval |= LPAIF_I2SCTL_MICMONO_STEREO;
158 break;
159 default:
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800160 dev_err(dai->dev, "invalid channels given: %u\n",
161 channels);
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000162 return -EINVAL;
163 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800164 }
165
166 ret = regmap_write(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100167 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id),
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100168 regval);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800169 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800170 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800171 return ret;
172 }
173
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100174 ret = clk_set_rate(drvdata->mi2s_bit_clk[dai->driver->id],
175 rate * bitwidth * 2);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800176 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800177 dev_err(dai->dev, "error setting mi2s bitclk to %u: %d\n",
178 rate * bitwidth * 2, ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800179 return ret;
180 }
181
182 return 0;
183}
184
185static int lpass_cpu_daiops_hw_free(struct snd_pcm_substream *substream,
186 struct snd_soc_dai *dai)
187{
188 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
189 int ret;
190
191 ret = regmap_write(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100192 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id),
193 0);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800194 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800195 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800196
197 return ret;
198}
199
200static int lpass_cpu_daiops_prepare(struct snd_pcm_substream *substream,
201 struct snd_soc_dai *dai)
202{
203 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
204 int ret;
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000205 unsigned int val, mask;
206
207 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
208 val = LPAIF_I2SCTL_SPKEN_ENABLE;
209 mask = LPAIF_I2SCTL_SPKEN_MASK;
210 } else {
211 val = LPAIF_I2SCTL_MICEN_ENABLE;
212 mask = LPAIF_I2SCTL_MICEN_MASK;
213 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800214
215 ret = regmap_update_bits(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100216 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id),
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000217 mask, val);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800218 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800219 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800220
221 return ret;
222}
223
224static int lpass_cpu_daiops_trigger(struct snd_pcm_substream *substream,
225 int cmd, struct snd_soc_dai *dai)
226{
227 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
Takashi Iwai96f05be2015-04-13 14:23:29 +0200228 int ret = -EINVAL;
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000229 unsigned int val, mask;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800230
231 switch (cmd) {
232 case SNDRV_PCM_TRIGGER_START:
233 case SNDRV_PCM_TRIGGER_RESUME:
234 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
236 val = LPAIF_I2SCTL_SPKEN_ENABLE;
237 mask = LPAIF_I2SCTL_SPKEN_MASK;
238 } else {
239 val = LPAIF_I2SCTL_MICEN_ENABLE;
240 mask = LPAIF_I2SCTL_MICEN_MASK;
241 }
242
Kenneth Westfield80beab82015-03-03 16:21:54 -0800243 ret = regmap_update_bits(drvdata->lpaif_map,
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100244 LPAIF_I2SCTL_REG(drvdata->variant,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100245 dai->driver->id),
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000246 mask, val);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800247 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800248 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
249 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800250 break;
251 case SNDRV_PCM_TRIGGER_STOP:
252 case SNDRV_PCM_TRIGGER_SUSPEND:
253 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000254 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
255 val = LPAIF_I2SCTL_SPKEN_DISABLE;
256 mask = LPAIF_I2SCTL_SPKEN_MASK;
257 } else {
258 val = LPAIF_I2SCTL_MICEN_DISABLE;
259 mask = LPAIF_I2SCTL_MICEN_MASK;
260 }
261
Kenneth Westfield80beab82015-03-03 16:21:54 -0800262 ret = regmap_update_bits(drvdata->lpaif_map,
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100263 LPAIF_I2SCTL_REG(drvdata->variant,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100264 dai->driver->id),
Srinivas Kandagatlafb5d1152016-02-11 12:18:33 +0000265 mask, val);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800266 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800267 dev_err(dai->dev, "error writing to i2sctl reg: %d\n",
268 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800269 break;
270 }
271
272 return ret;
273}
274
Axel Lin618718d2015-08-28 10:53:31 +0800275const struct snd_soc_dai_ops asoc_qcom_lpass_cpu_dai_ops = {
Kenneth Westfield80beab82015-03-03 16:21:54 -0800276 .set_sysclk = lpass_cpu_daiops_set_sysclk,
277 .startup = lpass_cpu_daiops_startup,
278 .shutdown = lpass_cpu_daiops_shutdown,
279 .hw_params = lpass_cpu_daiops_hw_params,
280 .hw_free = lpass_cpu_daiops_hw_free,
281 .prepare = lpass_cpu_daiops_prepare,
282 .trigger = lpass_cpu_daiops_trigger,
283};
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100284EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_ops);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800285
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100286int asoc_qcom_lpass_cpu_dai_probe(struct snd_soc_dai *dai)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800287{
288 struct lpass_data *drvdata = snd_soc_dai_get_drvdata(dai);
289 int ret;
290
291 /* ensure audio hardware is disabled */
292 ret = regmap_write(drvdata->lpaif_map,
Srinivas Kandagatla0ae9fd32015-05-16 13:32:25 +0100293 LPAIF_I2SCTL_REG(drvdata->variant, dai->driver->id), 0);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800294 if (ret)
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800295 dev_err(dai->dev, "error writing to i2sctl reg: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800296
297 return ret;
298}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100299EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_dai_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800300
301static const struct snd_soc_component_driver lpass_cpu_comp_driver = {
302 .name = "lpass-cpu",
303};
304
305static bool lpass_cpu_regmap_writeable(struct device *dev, unsigned int reg)
306{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100307 struct lpass_data *drvdata = dev_get_drvdata(dev);
308 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800309 int i;
310
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100311 for (i = 0; i < v->i2s_ports; ++i)
312 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800313 return true;
314
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100315 for (i = 0; i < v->irq_ports; ++i) {
316 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800317 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100318 if (reg == LPAIF_IRQCLEAR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800319 return true;
320 }
321
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100322 for (i = 0; i < v->rdma_channels; ++i) {
323 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800324 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100325 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800326 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100327 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800328 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100329 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800330 return true;
331 }
332
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000333 for (i = 0; i < v->wrdma_channels; ++i) {
334 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
335 return true;
336 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
337 return true;
338 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
339 return true;
340 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
341 return true;
342 }
343
Kenneth Westfield80beab82015-03-03 16:21:54 -0800344 return false;
345}
346
347static bool lpass_cpu_regmap_readable(struct device *dev, unsigned int reg)
348{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100349 struct lpass_data *drvdata = dev_get_drvdata(dev);
350 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800351 int i;
352
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100353 for (i = 0; i < v->i2s_ports; ++i)
354 if (reg == LPAIF_I2SCTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800355 return true;
356
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100357 for (i = 0; i < v->irq_ports; ++i) {
358 if (reg == LPAIF_IRQEN_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800359 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100360 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800361 return true;
362 }
363
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100364 for (i = 0; i < v->rdma_channels; ++i) {
365 if (reg == LPAIF_RDMACTL_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800366 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100367 if (reg == LPAIF_RDMABASE_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800368 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100369 if (reg == LPAIF_RDMABUFF_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800370 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100371 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800372 return true;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100373 if (reg == LPAIF_RDMAPER_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800374 return true;
375 }
376
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000377 for (i = 0; i < v->wrdma_channels; ++i) {
378 if (reg == LPAIF_WRDMACTL_REG(v, i + v->wrdma_channel_start))
379 return true;
380 if (reg == LPAIF_WRDMABASE_REG(v, i + v->wrdma_channel_start))
381 return true;
382 if (reg == LPAIF_WRDMABUFF_REG(v, i + v->wrdma_channel_start))
383 return true;
384 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
385 return true;
386 if (reg == LPAIF_WRDMAPER_REG(v, i + v->wrdma_channel_start))
387 return true;
388 }
389
Kenneth Westfield80beab82015-03-03 16:21:54 -0800390 return false;
391}
392
393static bool lpass_cpu_regmap_volatile(struct device *dev, unsigned int reg)
394{
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100395 struct lpass_data *drvdata = dev_get_drvdata(dev);
396 struct lpass_variant *v = drvdata->variant;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800397 int i;
398
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100399 for (i = 0; i < v->irq_ports; ++i)
400 if (reg == LPAIF_IRQSTAT_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800401 return true;
402
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100403 for (i = 0; i < v->rdma_channels; ++i)
404 if (reg == LPAIF_RDMACURR_REG(v, i))
Kenneth Westfield80beab82015-03-03 16:21:54 -0800405 return true;
406
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000407 for (i = 0; i < v->wrdma_channels; ++i)
408 if (reg == LPAIF_WRDMACURR_REG(v, i + v->wrdma_channel_start))
409 return true;
410
Kenneth Westfield80beab82015-03-03 16:21:54 -0800411 return false;
412}
413
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100414static struct regmap_config lpass_cpu_regmap_config = {
Kenneth Westfield80beab82015-03-03 16:21:54 -0800415 .reg_bits = 32,
416 .reg_stride = 4,
417 .val_bits = 32,
Kenneth Westfield80beab82015-03-03 16:21:54 -0800418 .writeable_reg = lpass_cpu_regmap_writeable,
419 .readable_reg = lpass_cpu_regmap_readable,
420 .volatile_reg = lpass_cpu_regmap_volatile,
421 .cache_type = REGCACHE_FLAT,
422};
423
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100424int asoc_qcom_lpass_cpu_platform_probe(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800425{
426 struct lpass_data *drvdata;
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700427 struct device_node *dsp_of_node;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800428 struct resource *res;
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100429 struct lpass_variant *variant;
430 struct device *dev = &pdev->dev;
431 const struct of_device_id *match;
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100432 char clk_name[16];
433 int ret, i, dai_id;
Kenneth Westfield80beab82015-03-03 16:21:54 -0800434
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700435 dsp_of_node = of_parse_phandle(pdev->dev.of_node, "qcom,adsp", 0);
436 if (dsp_of_node) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800437 dev_err(&pdev->dev, "DSP exists and holds audio resources\n");
Kenneth Westfield8ebe1482015-03-13 00:54:17 -0700438 return -EBUSY;
439 }
440
Kenneth Westfield80beab82015-03-03 16:21:54 -0800441 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct lpass_data),
442 GFP_KERNEL);
443 if (!drvdata)
444 return -ENOMEM;
445 platform_set_drvdata(pdev, drvdata);
446
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100447 match = of_match_device(dev->driver->of_match_table, dev);
448 if (!match || !match->data)
449 return -EINVAL;
450
451 drvdata->variant = (struct lpass_variant *)match->data;
452 variant = drvdata->variant;
453
Kenneth Westfield80beab82015-03-03 16:21:54 -0800454 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpass-lpaif");
Kenneth Westfield80beab82015-03-03 16:21:54 -0800455
456 drvdata->lpaif = devm_ioremap_resource(&pdev->dev, res);
457 if (IS_ERR((void const __force *)drvdata->lpaif)) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800458 dev_err(&pdev->dev, "error mapping reg resource: %ld\n",
Kenneth Westfield80beab82015-03-03 16:21:54 -0800459 PTR_ERR((void const __force *)drvdata->lpaif));
460 return PTR_ERR((void const __force *)drvdata->lpaif);
461 }
462
Srinivas Kandagatladad80612016-02-11 12:18:27 +0000463 lpass_cpu_regmap_config.max_register = LPAIF_WRDMAPER_REG(variant,
464 variant->wrdma_channels +
465 variant->wrdma_channel_start);
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100466
Kenneth Westfield80beab82015-03-03 16:21:54 -0800467 drvdata->lpaif_map = devm_regmap_init_mmio(&pdev->dev, drvdata->lpaif,
468 &lpass_cpu_regmap_config);
469 if (IS_ERR(drvdata->lpaif_map)) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800470 dev_err(&pdev->dev, "error initializing regmap: %ld\n",
471 PTR_ERR(drvdata->lpaif_map));
Kenneth Westfield80beab82015-03-03 16:21:54 -0800472 return PTR_ERR(drvdata->lpaif_map);
473 }
474
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100475 if (variant->init)
476 variant->init(pdev);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800477
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100478 for (i = 0; i < variant->num_dai; i++) {
479 dai_id = variant->dai_driver[i].id;
480 if (variant->num_dai > 1)
481 sprintf(clk_name, "mi2s-osr-clk%d", i);
482 else
483 sprintf(clk_name, "mi2s-osr-clk");
Kenneth Westfield80beab82015-03-03 16:21:54 -0800484
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100485 drvdata->mi2s_osr_clk[dai_id] = devm_clk_get(&pdev->dev,
486 clk_name);
487 if (IS_ERR(drvdata->mi2s_osr_clk[dai_id])) {
Srinivas Kandagatla3e53ac82015-05-21 22:52:57 +0100488 dev_warn(&pdev->dev,
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800489 "error getting optional mi2s-osr-clk: %ld\n",
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100490 PTR_ERR(drvdata->mi2s_osr_clk[dai_id]));
Bjorn Andersson46dccc32017-01-30 13:03:36 -0800491
492 drvdata->mi2s_osr_clk[dai_id] = NULL;
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100493 }
494
495 if (variant->num_dai > 1)
496 sprintf(clk_name, "mi2s-bit-clk%d", i);
497 else
498 sprintf(clk_name, "mi2s-bit-clk");
499
500 drvdata->mi2s_bit_clk[dai_id] = devm_clk_get(&pdev->dev,
501 clk_name);
502 if (IS_ERR(drvdata->mi2s_bit_clk[dai_id])) {
503 dev_err(&pdev->dev,
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800504 "error getting mi2s-bit-clk: %ld\n",
Julia Lawall89857292015-09-17 10:47:33 +0200505 PTR_ERR(drvdata->mi2s_bit_clk[dai_id]));
Srinivas Kandagatla9a127cf2015-05-21 22:52:49 +0100506 return PTR_ERR(drvdata->mi2s_bit_clk[dai_id]);
507 }
Kenneth Westfield80beab82015-03-03 16:21:54 -0800508 }
509
510 drvdata->ahbix_clk = devm_clk_get(&pdev->dev, "ahbix-clk");
511 if (IS_ERR(drvdata->ahbix_clk)) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800512 dev_err(&pdev->dev, "error getting ahbix-clk: %ld\n",
513 PTR_ERR(drvdata->ahbix_clk));
Kenneth Westfield80beab82015-03-03 16:21:54 -0800514 return PTR_ERR(drvdata->ahbix_clk);
515 }
516
517 ret = clk_set_rate(drvdata->ahbix_clk, LPASS_AHBIX_CLOCK_FREQUENCY);
518 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800519 dev_err(&pdev->dev, "error setting rate on ahbix_clk: %d\n",
520 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800521 return ret;
522 }
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800523 dev_dbg(&pdev->dev, "set ahbix_clk rate to %lu\n",
524 clk_get_rate(drvdata->ahbix_clk));
Kenneth Westfield80beab82015-03-03 16:21:54 -0800525
526 ret = clk_prepare_enable(drvdata->ahbix_clk);
527 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800528 dev_err(&pdev->dev, "error enabling ahbix_clk: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800529 return ret;
530 }
531
532 ret = devm_snd_soc_register_component(&pdev->dev,
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100533 &lpass_cpu_comp_driver,
534 variant->dai_driver,
535 variant->num_dai);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800536 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800537 dev_err(&pdev->dev, "error registering cpu driver: %d\n", ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800538 goto err_clk;
539 }
540
541 ret = asoc_qcom_lpass_platform_register(pdev);
542 if (ret) {
Bjorn Anderssonb6e643a2017-01-30 13:03:37 -0800543 dev_err(&pdev->dev, "error registering platform driver: %d\n",
544 ret);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800545 goto err_clk;
546 }
547
548 return 0;
549
550err_clk:
551 clk_disable_unprepare(drvdata->ahbix_clk);
552 return ret;
553}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100554EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_probe);
Kenneth Westfield80beab82015-03-03 16:21:54 -0800555
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100556int asoc_qcom_lpass_cpu_platform_remove(struct platform_device *pdev)
Kenneth Westfield80beab82015-03-03 16:21:54 -0800557{
558 struct lpass_data *drvdata = platform_get_drvdata(pdev);
559
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100560 if (drvdata->variant->exit)
561 drvdata->variant->exit(pdev);
562
Kenneth Westfield80beab82015-03-03 16:21:54 -0800563 clk_disable_unprepare(drvdata->ahbix_clk);
564
565 return 0;
566}
Srinivas Kandagatla9bae4882015-05-16 13:32:17 +0100567EXPORT_SYMBOL_GPL(asoc_qcom_lpass_cpu_platform_remove);
Srinivas Kandagatla94201792016-10-31 11:25:45 +0000568
569MODULE_DESCRIPTION("QTi LPASS CPU Driver");
570MODULE_LICENSE("GPL v2");