1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
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>
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>
21 #define sof_ops(sdev) \
22 ((sdev)->pdata->desc->ops)
24 /* Mandatory operations are verified during probing */
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);
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)
53 if (sof_ops(sdev)->stall)
54 return sof_ops(sdev)->stall(sdev);
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);
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);
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);
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);
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);
103 /* power management */
104 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
106 if (sof_ops(sdev)->resume)
107 return sof_ops(sdev)->resume(sdev);
112 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev, int state)
114 if (sof_ops(sdev)->suspend)
115 return sof_ops(sdev)->suspend(sdev, state);
120 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
122 if (sof_ops(sdev)->runtime_resume)
123 return sof_ops(sdev)->runtime_resume(sdev);
128 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev,
131 if (sof_ops(sdev)->runtime_suspend)
132 return sof_ops(sdev)->runtime_suspend(sdev, state);
137 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
139 if (sof_ops(sdev)->runtime_idle)
140 return sof_ops(sdev)->runtime_idle(sdev);
145 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
147 if (sof_ops(sdev)->set_hw_params_upon_resume)
148 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
152 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
154 if (sof_ops(sdev)->set_clk)
155 return sof_ops(sdev)->set_clk(sdev, freq);
161 static inline void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, u32 flags)
163 if (sof_ops(sdev)->dbg_dump)
164 return sof_ops(sdev)->dbg_dump(sdev, flags);
167 static inline void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
169 if (sof_ops(sdev)->ipc_dump)
170 return sof_ops(sdev)->ipc_dump(sdev);
174 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
175 u32 offset, u32 value)
177 if (sof_ops(sdev)->write) {
178 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
182 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
185 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
186 u32 offset, u64 value)
188 if (sof_ops(sdev)->write64) {
189 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
193 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
196 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
199 if (sof_ops(sdev)->read)
200 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
202 dev_err(sdev->dev, "error: %s not defined\n", __func__);
206 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
209 if (sof_ops(sdev)->read64)
210 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
212 dev_err(sdev->dev, "error: %s not defined\n", __func__);
217 static inline void snd_sof_dsp_block_read(struct snd_sof_dev *sdev, u32 bar,
218 u32 offset, void *dest, size_t bytes)
220 sof_ops(sdev)->block_read(sdev, bar, offset, dest, bytes);
223 static inline void snd_sof_dsp_block_write(struct snd_sof_dev *sdev, u32 bar,
224 u32 offset, void *src, size_t bytes)
226 sof_ops(sdev)->block_write(sdev, bar, offset, src, bytes);
230 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
231 struct snd_sof_ipc_msg *msg)
233 return sof_ops(sdev)->send_msg(sdev, msg);
237 static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev,
240 if (sof_ops(sdev)->trace_init)
241 return sof_ops(sdev)->trace_init(sdev, stream_tag);
246 static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev)
248 if (sof_ops(sdev)->trace_release)
249 return sof_ops(sdev)->trace_release(sdev);
254 static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd)
256 if (sof_ops(sdev)->trace_trigger)
257 return sof_ops(sdev)->trace_trigger(sdev, cmd);
264 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
265 struct snd_pcm_substream *substream)
267 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
268 return sof_ops(sdev)->pcm_open(sdev, substream);
273 /* disconnect pcm substream to a host stream */
275 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
276 struct snd_pcm_substream *substream)
278 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
279 return sof_ops(sdev)->pcm_close(sdev, substream);
284 /* host stream hw params */
286 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
287 struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *params,
289 struct sof_ipc_stream_params *ipc_params)
291 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
292 return sof_ops(sdev)->pcm_hw_params(sdev, substream,
298 /* host stream hw free */
300 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
301 struct snd_pcm_substream *substream)
303 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
304 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
309 /* host stream trigger */
311 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
312 struct snd_pcm_substream *substream, int cmd)
314 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
315 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
320 /* host DSP message data */
321 static inline void snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
322 struct snd_pcm_substream *substream,
325 sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
328 /* host configure DSP HW parameters */
330 snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev,
331 struct snd_pcm_substream *substream,
332 const struct sof_ipc_pcm_params_reply *reply)
334 return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply);
337 /* host stream pointer */
338 static inline snd_pcm_uframes_t
339 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
340 struct snd_pcm_substream *substream)
342 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
343 return sof_ops(sdev)->pcm_pointer(sdev, substream);
348 static inline const struct snd_sof_dsp_ops
349 *sof_get_ops(const struct sof_dev_desc *d,
350 const struct sof_ops_table mach_ops[], int asize)
354 for (i = 0; i < asize; i++) {
355 if (d == mach_ops[i].desc)
356 return mach_ops[i].ops;
364 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
365 * until a condition is met or a timeout occurs
366 * @op: accessor function (takes @addr as its only argument)
367 * @addr: Address to poll
368 * @val: Variable to read the value into
369 * @cond: Break condition (usually involving @val)
370 * @sleep_us: Maximum time to sleep between reads in us (0
371 * tight-loops). Should be less than ~20ms since usleep_range
372 * is used (see Documentation/timers/timers-howto.rst).
373 * @timeout_us: Timeout in us, 0 means never timeout
375 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
376 * case, the last read value at @addr is stored in @val. Must not
377 * be called from atomic context if sleep_us or timeout_us are used.
379 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
381 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
383 u64 __timeout_us = (timeout_us); \
384 unsigned long __sleep_us = (sleep_us); \
385 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
386 might_sleep_if((__sleep_us) != 0); \
388 (val) = snd_sof_dsp_read(sdev, bar, offset); \
391 "FW Poll Status: reg=%#x successful\n", (val)); \
394 if (__timeout_us && \
395 ktime_compare(ktime_get(), __timeout) > 0) { \
396 (val) = snd_sof_dsp_read(sdev, bar, offset); \
398 "FW Poll Status: reg=%#x timedout\n", (val)); \
402 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
404 (cond) ? 0 : -ETIMEDOUT; \
407 /* This is for registers bits with attribute RWC */
408 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
409 u32 mask, u32 value);
411 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
412 u32 offset, u32 mask, u32 value);
414 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
415 u32 offset, u64 mask, u64 value);
417 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
418 u32 mask, u32 value);
420 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
421 u32 offset, u64 mask, u64 value);
423 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
424 u32 offset, u32 mask, u32 value);
426 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
427 u32 mask, u32 target, u32 timeout_ms,
430 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset);