Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / sof / ops.h
blob95e748b36903a0084e92769681d50204342449e9
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2 /*
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
6 * Copyright(c) 2018 Intel Corporation. All rights reserved.
8 * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 */
11 #ifndef __SOUND_SOC_SOF_IO_H
12 #define __SOUND_SOC_SOF_IO_H
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <sound/pcm.h>
19 #include "sof-priv.h"
21 #define sof_ops(sdev) \
22 ((sdev)->pdata->desc->ops)
24 /* Mandatory operations are verified during probing */
26 /* init */
27 static inline int snd_sof_probe(struct snd_sof_dev *sdev)
29 return sof_ops(sdev)->probe(sdev);
32 static inline int snd_sof_remove(struct snd_sof_dev *sdev)
34 if (sof_ops(sdev)->remove)
35 return sof_ops(sdev)->remove(sdev);
37 return 0;
40 /* control */
43 * snd_sof_dsp_run returns the core mask of the cores that are available
44 * after successful fw boot
46 static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
48 return sof_ops(sdev)->run(sdev);
51 static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
53 if (sof_ops(sdev)->stall)
54 return sof_ops(sdev)->stall(sdev, core_mask);
56 return 0;
59 static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
61 if (sof_ops(sdev)->reset)
62 return sof_ops(sdev)->reset(sdev);
64 return 0;
67 /* dsp core power up/power down */
68 static inline int snd_sof_dsp_core_power_up(struct snd_sof_dev *sdev,
69 unsigned int core_mask)
71 if (sof_ops(sdev)->core_power_up)
72 return sof_ops(sdev)->core_power_up(sdev, core_mask);
74 return 0;
77 static inline int snd_sof_dsp_core_power_down(struct snd_sof_dev *sdev,
78 unsigned int core_mask)
80 if (sof_ops(sdev)->core_power_down)
81 return sof_ops(sdev)->core_power_down(sdev, core_mask);
83 return 0;
86 /* pre/post fw load */
87 static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
89 if (sof_ops(sdev)->pre_fw_run)
90 return sof_ops(sdev)->pre_fw_run(sdev);
92 return 0;
95 static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
97 if (sof_ops(sdev)->post_fw_run)
98 return sof_ops(sdev)->post_fw_run(sdev);
100 return 0;
103 /* parse platform specific extended manifest */
104 static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
105 const struct sof_ext_man_elem_header *hdr)
107 if (sof_ops(sdev)->parse_platform_ext_manifest)
108 return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
110 return 0;
113 /* misc */
116 * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
118 * @sdev: sof device
119 * @type: section type as described by snd_sof_fw_blk_type
121 * Returns the corresponding BAR index (a positive integer) or -EINVAL
122 * in case there is no mapping
124 static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
126 if (sof_ops(sdev)->get_bar_index)
127 return sof_ops(sdev)->get_bar_index(sdev, type);
129 return sdev->mmio_bar;
132 static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
134 if (sof_ops(sdev)->get_mailbox_offset)
135 return sof_ops(sdev)->get_mailbox_offset(sdev);
137 dev_err(sdev->dev, "error: %s not defined\n", __func__);
138 return -ENOTSUPP;
141 static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
142 u32 id)
144 if (sof_ops(sdev)->get_window_offset)
145 return sof_ops(sdev)->get_window_offset(sdev, id);
147 dev_err(sdev->dev, "error: %s not defined\n", __func__);
148 return -ENOTSUPP;
150 /* power management */
151 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
153 if (sof_ops(sdev)->resume)
154 return sof_ops(sdev)->resume(sdev);
156 return 0;
159 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
160 u32 target_state)
162 if (sof_ops(sdev)->suspend)
163 return sof_ops(sdev)->suspend(sdev, target_state);
165 return 0;
168 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
170 if (sof_ops(sdev)->runtime_resume)
171 return sof_ops(sdev)->runtime_resume(sdev);
173 return 0;
176 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
178 if (sof_ops(sdev)->runtime_suspend)
179 return sof_ops(sdev)->runtime_suspend(sdev);
181 return 0;
184 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
186 if (sof_ops(sdev)->runtime_idle)
187 return sof_ops(sdev)->runtime_idle(sdev);
189 return 0;
192 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
194 if (sof_ops(sdev)->set_hw_params_upon_resume)
195 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
196 return 0;
199 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
201 if (sof_ops(sdev)->set_clk)
202 return sof_ops(sdev)->set_clk(sdev, freq);
204 return 0;
207 static inline int
208 snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
209 const struct sof_dsp_power_state *target_state)
211 if (sof_ops(sdev)->set_power_state)
212 return sof_ops(sdev)->set_power_state(sdev, target_state);
214 /* D0 substate is not supported, do nothing here. */
215 return 0;
218 /* debug */
219 static inline void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, u32 flags)
221 if (sof_ops(sdev)->dbg_dump)
222 return sof_ops(sdev)->dbg_dump(sdev, flags);
225 static inline void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
227 if (sof_ops(sdev)->ipc_dump)
228 return sof_ops(sdev)->ipc_dump(sdev);
231 /* register IO */
232 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
233 u32 offset, u32 value)
235 if (sof_ops(sdev)->write) {
236 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
237 return;
240 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
243 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
244 u32 offset, u64 value)
246 if (sof_ops(sdev)->write64) {
247 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
248 return;
251 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
254 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
255 u32 offset)
257 if (sof_ops(sdev)->read)
258 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
260 dev_err(sdev->dev, "error: %s not defined\n", __func__);
261 return -ENOTSUPP;
264 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
265 u32 offset)
267 if (sof_ops(sdev)->read64)
268 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
270 dev_err(sdev->dev, "error: %s not defined\n", __func__);
271 return -ENOTSUPP;
274 /* block IO */
275 static inline void snd_sof_dsp_block_read(struct snd_sof_dev *sdev, u32 bar,
276 u32 offset, void *dest, size_t bytes)
278 sof_ops(sdev)->block_read(sdev, bar, offset, dest, bytes);
281 static inline void snd_sof_dsp_block_write(struct snd_sof_dev *sdev, u32 bar,
282 u32 offset, void *src, size_t bytes)
284 sof_ops(sdev)->block_write(sdev, bar, offset, src, bytes);
287 /* ipc */
288 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
289 struct snd_sof_ipc_msg *msg)
291 return sof_ops(sdev)->send_msg(sdev, msg);
294 /* host DMA trace */
295 static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev,
296 u32 *stream_tag)
298 if (sof_ops(sdev)->trace_init)
299 return sof_ops(sdev)->trace_init(sdev, stream_tag);
301 return 0;
304 static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev)
306 if (sof_ops(sdev)->trace_release)
307 return sof_ops(sdev)->trace_release(sdev);
309 return 0;
312 static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd)
314 if (sof_ops(sdev)->trace_trigger)
315 return sof_ops(sdev)->trace_trigger(sdev, cmd);
317 return 0;
320 /* host PCM ops */
321 static inline int
322 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
323 struct snd_pcm_substream *substream)
325 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
326 return sof_ops(sdev)->pcm_open(sdev, substream);
328 return 0;
331 /* disconnect pcm substream to a host stream */
332 static inline int
333 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
334 struct snd_pcm_substream *substream)
336 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
337 return sof_ops(sdev)->pcm_close(sdev, substream);
339 return 0;
342 /* host stream hw params */
343 static inline int
344 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
345 struct snd_pcm_substream *substream,
346 struct snd_pcm_hw_params *params,
347 struct sof_ipc_stream_params *ipc_params)
349 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
350 return sof_ops(sdev)->pcm_hw_params(sdev, substream,
351 params, ipc_params);
353 return 0;
356 /* host stream hw free */
357 static inline int
358 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
359 struct snd_pcm_substream *substream)
361 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
362 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
364 return 0;
367 /* host stream trigger */
368 static inline int
369 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
370 struct snd_pcm_substream *substream, int cmd)
372 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
373 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
375 return 0;
378 /* host DSP message data */
379 static inline void snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
380 struct snd_pcm_substream *substream,
381 void *p, size_t sz)
383 sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
386 /* host configure DSP HW parameters */
387 static inline int
388 snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev,
389 struct snd_pcm_substream *substream,
390 const struct sof_ipc_pcm_params_reply *reply)
392 return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply);
395 /* host stream pointer */
396 static inline snd_pcm_uframes_t
397 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
398 struct snd_pcm_substream *substream)
400 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
401 return sof_ops(sdev)->pcm_pointer(sdev, substream);
403 return 0;
406 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
407 static inline int
408 snd_sof_probe_compr_assign(struct snd_sof_dev *sdev,
409 struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
411 return sof_ops(sdev)->probe_assign(sdev, cstream, dai);
414 static inline int
415 snd_sof_probe_compr_free(struct snd_sof_dev *sdev,
416 struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
418 return sof_ops(sdev)->probe_free(sdev, cstream, dai);
421 static inline int
422 snd_sof_probe_compr_set_params(struct snd_sof_dev *sdev,
423 struct snd_compr_stream *cstream,
424 struct snd_compr_params *params, struct snd_soc_dai *dai)
426 return sof_ops(sdev)->probe_set_params(sdev, cstream, params, dai);
429 static inline int
430 snd_sof_probe_compr_trigger(struct snd_sof_dev *sdev,
431 struct snd_compr_stream *cstream, int cmd,
432 struct snd_soc_dai *dai)
434 return sof_ops(sdev)->probe_trigger(sdev, cstream, cmd, dai);
437 static inline int
438 snd_sof_probe_compr_pointer(struct snd_sof_dev *sdev,
439 struct snd_compr_stream *cstream,
440 struct snd_compr_tstamp *tstamp, struct snd_soc_dai *dai)
442 if (sof_ops(sdev) && sof_ops(sdev)->probe_pointer)
443 return sof_ops(sdev)->probe_pointer(sdev, cstream, tstamp, dai);
445 return 0;
447 #endif
449 /* machine driver */
450 static inline int
451 snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
453 if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
454 return sof_ops(sdev)->machine_register(sdev, pdata);
456 return 0;
459 static inline void
460 snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
462 if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
463 sof_ops(sdev)->machine_unregister(sdev, pdata);
466 static inline void
467 snd_sof_machine_select(struct snd_sof_dev *sdev)
469 if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
470 sof_ops(sdev)->machine_select(sdev);
473 static inline void
474 snd_sof_set_mach_params(const struct snd_soc_acpi_mach *mach,
475 struct device *dev)
477 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
479 if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
480 sof_ops(sdev)->set_mach_params(mach, dev);
483 static inline const struct snd_sof_dsp_ops
484 *sof_get_ops(const struct sof_dev_desc *d,
485 const struct sof_ops_table mach_ops[], int asize)
487 int i;
489 for (i = 0; i < asize; i++) {
490 if (d == mach_ops[i].desc)
491 return mach_ops[i].ops;
494 /* not found */
495 return NULL;
499 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
500 * until a condition is met or a timeout occurs
501 * @op: accessor function (takes @addr as its only argument)
502 * @addr: Address to poll
503 * @val: Variable to read the value into
504 * @cond: Break condition (usually involving @val)
505 * @sleep_us: Maximum time to sleep between reads in us (0
506 * tight-loops). Should be less than ~20ms since usleep_range
507 * is used (see Documentation/timers/timers-howto.rst).
508 * @timeout_us: Timeout in us, 0 means never timeout
510 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
511 * case, the last read value at @addr is stored in @val. Must not
512 * be called from atomic context if sleep_us or timeout_us are used.
514 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
516 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
517 ({ \
518 u64 __timeout_us = (timeout_us); \
519 unsigned long __sleep_us = (sleep_us); \
520 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
521 might_sleep_if((__sleep_us) != 0); \
522 for (;;) { \
523 (val) = snd_sof_dsp_read(sdev, bar, offset); \
524 if (cond) { \
525 dev_dbg(sdev->dev, \
526 "FW Poll Status: reg=%#x successful\n", (val)); \
527 break; \
529 if (__timeout_us && \
530 ktime_compare(ktime_get(), __timeout) > 0) { \
531 (val) = snd_sof_dsp_read(sdev, bar, offset); \
532 dev_dbg(sdev->dev, \
533 "FW Poll Status: reg=%#x timedout\n", (val)); \
534 break; \
536 if (__sleep_us) \
537 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
539 (cond) ? 0 : -ETIMEDOUT; \
542 /* This is for registers bits with attribute RWC */
543 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
544 u32 mask, u32 value);
546 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
547 u32 offset, u32 mask, u32 value);
549 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
550 u32 offset, u64 mask, u64 value);
552 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
553 u32 mask, u32 value);
555 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
556 u32 offset, u64 mask, u64 value);
558 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
559 u32 offset, u32 mask, u32 value);
561 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
562 u32 mask, u32 target, u32 timeout_ms,
563 u32 interval_us);
565 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset);
566 #endif