treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / intel / skylake / skl-sst-dsp.c
blob225706d148d8c2b84f28decf7cd1878e11b8e386
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * skl-sst-dsp.c - SKL SST library generic function
5 * Copyright (C) 2014-15, Intel Corporation.
6 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
7 * Jeeja KP <jeeja.kp@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 */
10 #include <sound/pcm.h>
12 #include "../common/sst-dsp.h"
13 #include "../common/sst-ipc.h"
14 #include "../common/sst-dsp-priv.h"
15 #include "skl.h"
17 /* various timeout values */
18 #define SKL_DSP_PU_TO 50
19 #define SKL_DSP_PD_TO 50
20 #define SKL_DSP_RESET_TO 50
22 void skl_dsp_set_state_locked(struct sst_dsp *ctx, int state)
24 mutex_lock(&ctx->mutex);
25 ctx->sst_state = state;
26 mutex_unlock(&ctx->mutex);
30 * Initialize core power state and usage count. To be called after
31 * successful first boot. Hence core 0 will be running and other cores
32 * will be reset
34 void skl_dsp_init_core_state(struct sst_dsp *ctx)
36 struct skl_dev *skl = ctx->thread_context;
37 int i;
39 skl->cores.state[SKL_DSP_CORE0_ID] = SKL_DSP_RUNNING;
40 skl->cores.usage_count[SKL_DSP_CORE0_ID] = 1;
42 for (i = SKL_DSP_CORE0_ID + 1; i < skl->cores.count; i++) {
43 skl->cores.state[i] = SKL_DSP_RESET;
44 skl->cores.usage_count[i] = 0;
48 /* Get the mask for all enabled cores */
49 unsigned int skl_dsp_get_enabled_cores(struct sst_dsp *ctx)
51 struct skl_dev *skl = ctx->thread_context;
52 unsigned int core_mask, en_cores_mask;
53 u32 val;
55 core_mask = SKL_DSP_CORES_MASK(skl->cores.count);
57 val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS);
59 /* Cores having CPA bit set */
60 en_cores_mask = (val & SKL_ADSPCS_CPA_MASK(core_mask)) >>
61 SKL_ADSPCS_CPA_SHIFT;
63 /* And cores having CRST bit cleared */
64 en_cores_mask &= (~val & SKL_ADSPCS_CRST_MASK(core_mask)) >>
65 SKL_ADSPCS_CRST_SHIFT;
67 /* And cores having CSTALL bit cleared */
68 en_cores_mask &= (~val & SKL_ADSPCS_CSTALL_MASK(core_mask)) >>
69 SKL_ADSPCS_CSTALL_SHIFT;
70 en_cores_mask &= core_mask;
72 dev_dbg(ctx->dev, "DSP enabled cores mask = %x\n", en_cores_mask);
74 return en_cores_mask;
77 static int
78 skl_dsp_core_set_reset_state(struct sst_dsp *ctx, unsigned int core_mask)
80 int ret;
82 /* update bits */
83 sst_dsp_shim_update_bits_unlocked(ctx,
84 SKL_ADSP_REG_ADSPCS, SKL_ADSPCS_CRST_MASK(core_mask),
85 SKL_ADSPCS_CRST_MASK(core_mask));
87 /* poll with timeout to check if operation successful */
88 ret = sst_dsp_register_poll(ctx,
89 SKL_ADSP_REG_ADSPCS,
90 SKL_ADSPCS_CRST_MASK(core_mask),
91 SKL_ADSPCS_CRST_MASK(core_mask),
92 SKL_DSP_RESET_TO,
93 "Set reset");
94 if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
95 SKL_ADSPCS_CRST_MASK(core_mask)) !=
96 SKL_ADSPCS_CRST_MASK(core_mask)) {
97 dev_err(ctx->dev, "Set reset state failed: core_mask %x\n",
98 core_mask);
99 ret = -EIO;
102 return ret;
105 int skl_dsp_core_unset_reset_state(
106 struct sst_dsp *ctx, unsigned int core_mask)
108 int ret;
110 dev_dbg(ctx->dev, "In %s\n", __func__);
112 /* update bits */
113 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
114 SKL_ADSPCS_CRST_MASK(core_mask), 0);
116 /* poll with timeout to check if operation successful */
117 ret = sst_dsp_register_poll(ctx,
118 SKL_ADSP_REG_ADSPCS,
119 SKL_ADSPCS_CRST_MASK(core_mask),
121 SKL_DSP_RESET_TO,
122 "Unset reset");
124 if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
125 SKL_ADSPCS_CRST_MASK(core_mask)) != 0) {
126 dev_err(ctx->dev, "Unset reset state failed: core_mask %x\n",
127 core_mask);
128 ret = -EIO;
131 return ret;
134 static bool
135 is_skl_dsp_core_enable(struct sst_dsp *ctx, unsigned int core_mask)
137 int val;
138 bool is_enable;
140 val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS);
142 is_enable = ((val & SKL_ADSPCS_CPA_MASK(core_mask)) &&
143 (val & SKL_ADSPCS_SPA_MASK(core_mask)) &&
144 !(val & SKL_ADSPCS_CRST_MASK(core_mask)) &&
145 !(val & SKL_ADSPCS_CSTALL_MASK(core_mask)));
147 dev_dbg(ctx->dev, "DSP core(s) enabled? %d : core_mask %x\n",
148 is_enable, core_mask);
150 return is_enable;
153 static int skl_dsp_reset_core(struct sst_dsp *ctx, unsigned int core_mask)
155 /* stall core */
156 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
157 SKL_ADSPCS_CSTALL_MASK(core_mask),
158 SKL_ADSPCS_CSTALL_MASK(core_mask));
160 /* set reset state */
161 return skl_dsp_core_set_reset_state(ctx, core_mask);
164 int skl_dsp_start_core(struct sst_dsp *ctx, unsigned int core_mask)
166 int ret;
168 /* unset reset state */
169 ret = skl_dsp_core_unset_reset_state(ctx, core_mask);
170 if (ret < 0)
171 return ret;
173 /* run core */
174 dev_dbg(ctx->dev, "unstall/run core: core_mask = %x\n", core_mask);
175 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
176 SKL_ADSPCS_CSTALL_MASK(core_mask), 0);
178 if (!is_skl_dsp_core_enable(ctx, core_mask)) {
179 skl_dsp_reset_core(ctx, core_mask);
180 dev_err(ctx->dev, "DSP start core failed: core_mask %x\n",
181 core_mask);
182 ret = -EIO;
185 return ret;
188 int skl_dsp_core_power_up(struct sst_dsp *ctx, unsigned int core_mask)
190 int ret;
192 /* update bits */
193 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
194 SKL_ADSPCS_SPA_MASK(core_mask),
195 SKL_ADSPCS_SPA_MASK(core_mask));
197 /* poll with timeout to check if operation successful */
198 ret = sst_dsp_register_poll(ctx,
199 SKL_ADSP_REG_ADSPCS,
200 SKL_ADSPCS_CPA_MASK(core_mask),
201 SKL_ADSPCS_CPA_MASK(core_mask),
202 SKL_DSP_PU_TO,
203 "Power up");
205 if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
206 SKL_ADSPCS_CPA_MASK(core_mask)) !=
207 SKL_ADSPCS_CPA_MASK(core_mask)) {
208 dev_err(ctx->dev, "DSP core power up failed: core_mask %x\n",
209 core_mask);
210 ret = -EIO;
213 return ret;
216 int skl_dsp_core_power_down(struct sst_dsp *ctx, unsigned int core_mask)
218 /* update bits */
219 sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
220 SKL_ADSPCS_SPA_MASK(core_mask), 0);
222 /* poll with timeout to check if operation successful */
223 return sst_dsp_register_poll(ctx,
224 SKL_ADSP_REG_ADSPCS,
225 SKL_ADSPCS_CPA_MASK(core_mask),
227 SKL_DSP_PD_TO,
228 "Power down");
231 int skl_dsp_enable_core(struct sst_dsp *ctx, unsigned int core_mask)
233 int ret;
235 /* power up */
236 ret = skl_dsp_core_power_up(ctx, core_mask);
237 if (ret < 0) {
238 dev_err(ctx->dev, "dsp core power up failed: core_mask %x\n",
239 core_mask);
240 return ret;
243 return skl_dsp_start_core(ctx, core_mask);
246 int skl_dsp_disable_core(struct sst_dsp *ctx, unsigned int core_mask)
248 int ret;
250 ret = skl_dsp_reset_core(ctx, core_mask);
251 if (ret < 0) {
252 dev_err(ctx->dev, "dsp core reset failed: core_mask %x\n",
253 core_mask);
254 return ret;
257 /* power down core*/
258 ret = skl_dsp_core_power_down(ctx, core_mask);
259 if (ret < 0) {
260 dev_err(ctx->dev, "dsp core power down fail mask %x: %d\n",
261 core_mask, ret);
262 return ret;
265 if (is_skl_dsp_core_enable(ctx, core_mask)) {
266 dev_err(ctx->dev, "dsp core disable fail mask %x: %d\n",
267 core_mask, ret);
268 ret = -EIO;
271 return ret;
274 int skl_dsp_boot(struct sst_dsp *ctx)
276 int ret;
278 if (is_skl_dsp_core_enable(ctx, SKL_DSP_CORE0_MASK)) {
279 ret = skl_dsp_reset_core(ctx, SKL_DSP_CORE0_MASK);
280 if (ret < 0) {
281 dev_err(ctx->dev, "dsp core0 reset fail: %d\n", ret);
282 return ret;
285 ret = skl_dsp_start_core(ctx, SKL_DSP_CORE0_MASK);
286 if (ret < 0) {
287 dev_err(ctx->dev, "dsp core0 start fail: %d\n", ret);
288 return ret;
290 } else {
291 ret = skl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
292 if (ret < 0) {
293 dev_err(ctx->dev, "dsp core0 disable fail: %d\n", ret);
294 return ret;
296 ret = skl_dsp_enable_core(ctx, SKL_DSP_CORE0_MASK);
299 return ret;
302 irqreturn_t skl_dsp_sst_interrupt(int irq, void *dev_id)
304 struct sst_dsp *ctx = dev_id;
305 u32 val;
306 irqreturn_t result = IRQ_NONE;
308 spin_lock(&ctx->spinlock);
310 val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPIS);
311 ctx->intr_status = val;
313 if (val == 0xffffffff) {
314 spin_unlock(&ctx->spinlock);
315 return IRQ_NONE;
318 if (val & SKL_ADSPIS_IPC) {
319 skl_ipc_int_disable(ctx);
320 result = IRQ_WAKE_THREAD;
323 if (val & SKL_ADSPIS_CL_DMA) {
324 skl_cldma_int_disable(ctx);
325 result = IRQ_WAKE_THREAD;
328 spin_unlock(&ctx->spinlock);
330 return result;
333 * skl_dsp_get_core/skl_dsp_put_core will be called inside DAPM context
334 * within the dapm mutex. Hence no separate lock is used.
336 int skl_dsp_get_core(struct sst_dsp *ctx, unsigned int core_id)
338 struct skl_dev *skl = ctx->thread_context;
339 int ret = 0;
341 if (core_id >= skl->cores.count) {
342 dev_err(ctx->dev, "invalid core id: %d\n", core_id);
343 return -EINVAL;
346 skl->cores.usage_count[core_id]++;
348 if (skl->cores.state[core_id] == SKL_DSP_RESET) {
349 ret = ctx->fw_ops.set_state_D0(ctx, core_id);
350 if (ret < 0) {
351 dev_err(ctx->dev, "unable to get core%d\n", core_id);
352 goto out;
356 out:
357 dev_dbg(ctx->dev, "core id %d state %d usage_count %d\n",
358 core_id, skl->cores.state[core_id],
359 skl->cores.usage_count[core_id]);
361 return ret;
363 EXPORT_SYMBOL_GPL(skl_dsp_get_core);
365 int skl_dsp_put_core(struct sst_dsp *ctx, unsigned int core_id)
367 struct skl_dev *skl = ctx->thread_context;
368 int ret = 0;
370 if (core_id >= skl->cores.count) {
371 dev_err(ctx->dev, "invalid core id: %d\n", core_id);
372 return -EINVAL;
375 if ((--skl->cores.usage_count[core_id] == 0) &&
376 (skl->cores.state[core_id] != SKL_DSP_RESET)) {
377 ret = ctx->fw_ops.set_state_D3(ctx, core_id);
378 if (ret < 0) {
379 dev_err(ctx->dev, "unable to put core %d: %d\n",
380 core_id, ret);
381 skl->cores.usage_count[core_id]++;
385 dev_dbg(ctx->dev, "core id %d state %d usage_count %d\n",
386 core_id, skl->cores.state[core_id],
387 skl->cores.usage_count[core_id]);
389 return ret;
391 EXPORT_SYMBOL_GPL(skl_dsp_put_core);
393 int skl_dsp_wake(struct sst_dsp *ctx)
395 return skl_dsp_get_core(ctx, SKL_DSP_CORE0_ID);
397 EXPORT_SYMBOL_GPL(skl_dsp_wake);
399 int skl_dsp_sleep(struct sst_dsp *ctx)
401 return skl_dsp_put_core(ctx, SKL_DSP_CORE0_ID);
403 EXPORT_SYMBOL_GPL(skl_dsp_sleep);
405 struct sst_dsp *skl_dsp_ctx_init(struct device *dev,
406 struct sst_dsp_device *sst_dev, int irq)
408 int ret;
409 struct sst_dsp *sst;
411 sst = devm_kzalloc(dev, sizeof(*sst), GFP_KERNEL);
412 if (sst == NULL)
413 return NULL;
415 spin_lock_init(&sst->spinlock);
416 mutex_init(&sst->mutex);
417 sst->dev = dev;
418 sst->sst_dev = sst_dev;
419 sst->irq = irq;
420 sst->ops = sst_dev->ops;
421 sst->thread_context = sst_dev->thread_context;
423 /* Initialise SST Audio DSP */
424 if (sst->ops->init) {
425 ret = sst->ops->init(sst, NULL);
426 if (ret < 0)
427 return NULL;
430 return sst;
433 int skl_dsp_acquire_irq(struct sst_dsp *sst)
435 struct sst_dsp_device *sst_dev = sst->sst_dev;
436 int ret;
438 /* Register the ISR */
439 ret = request_threaded_irq(sst->irq, sst->ops->irq_handler,
440 sst_dev->thread, IRQF_SHARED, "AudioDSP", sst);
441 if (ret)
442 dev_err(sst->dev, "unable to grab threaded IRQ %d, disabling device\n",
443 sst->irq);
445 return ret;
448 void skl_dsp_free(struct sst_dsp *dsp)
450 skl_ipc_int_disable(dsp);
452 free_irq(dsp->irq, dsp);
453 skl_ipc_op_int_disable(dsp);
454 skl_dsp_disable_core(dsp, SKL_DSP_CORE0_MASK);
456 EXPORT_SYMBOL_GPL(skl_dsp_free);
458 bool is_skl_dsp_running(struct sst_dsp *ctx)
460 return (ctx->sst_state == SKL_DSP_RUNNING);
462 EXPORT_SYMBOL_GPL(is_skl_dsp_running);