1 /* SPDX-License-Identifier: (GPL-2.0-only 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 static inline int sof_ops_init(struct snd_sof_dev *sdev)
26 if (sdev->pdata->desc->ops_init)
27 return sdev->pdata->desc->ops_init(sdev);
32 /* Mandatory operations are verified during probing */
35 static inline int snd_sof_probe(struct snd_sof_dev *sdev)
37 return sof_ops(sdev)->probe(sdev);
40 static inline int snd_sof_remove(struct snd_sof_dev *sdev)
42 if (sof_ops(sdev)->remove)
43 return sof_ops(sdev)->remove(sdev);
48 static inline int snd_sof_shutdown(struct snd_sof_dev *sdev)
50 if (sof_ops(sdev)->shutdown)
51 return sof_ops(sdev)->shutdown(sdev);
59 * snd_sof_dsp_run returns the core mask of the cores that are available
60 * after successful fw boot
62 static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
64 return sof_ops(sdev)->run(sdev);
67 static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
69 if (sof_ops(sdev)->stall)
70 return sof_ops(sdev)->stall(sdev, core_mask);
75 static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
77 if (sof_ops(sdev)->reset)
78 return sof_ops(sdev)->reset(sdev);
83 /* dsp core get/put */
84 static inline int snd_sof_dsp_core_get(struct snd_sof_dev *sdev, int core)
86 if (core > sdev->num_cores - 1) {
87 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
92 if (sof_ops(sdev)->core_get) {
95 /* if current ref_count is > 0, increment it and return */
96 if (sdev->dsp_core_ref_count[core] > 0) {
97 sdev->dsp_core_ref_count[core]++;
101 /* power up the core */
102 ret = sof_ops(sdev)->core_get(sdev, core);
106 /* increment ref_count */
107 sdev->dsp_core_ref_count[core]++;
109 /* and update enabled_cores_mask */
110 sdev->enabled_cores_mask |= BIT(core);
112 dev_dbg(sdev->dev, "Core %d powered up\n", core);
118 static inline int snd_sof_dsp_core_put(struct snd_sof_dev *sdev, int core)
120 if (core > sdev->num_cores - 1) {
121 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
126 if (sof_ops(sdev)->core_put) {
129 /* decrement ref_count and return if it is > 0 */
130 if (--(sdev->dsp_core_ref_count[core]) > 0)
133 /* power down the core */
134 ret = sof_ops(sdev)->core_put(sdev, core);
138 /* and update enabled_cores_mask */
139 sdev->enabled_cores_mask &= ~BIT(core);
141 dev_dbg(sdev->dev, "Core %d powered down\n", core);
147 /* pre/post fw load */
148 static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
150 if (sof_ops(sdev)->pre_fw_run)
151 return sof_ops(sdev)->pre_fw_run(sdev);
156 static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
158 if (sof_ops(sdev)->post_fw_run)
159 return sof_ops(sdev)->post_fw_run(sdev);
164 /* parse platform specific extended manifest */
165 static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
166 const struct sof_ext_man_elem_header *hdr)
168 if (sof_ops(sdev)->parse_platform_ext_manifest)
169 return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
177 * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
180 * @type: section type as described by snd_sof_fw_blk_type
182 * Returns the corresponding BAR index (a positive integer) or -EINVAL
183 * in case there is no mapping
185 static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
187 if (sof_ops(sdev)->get_bar_index)
188 return sof_ops(sdev)->get_bar_index(sdev, type);
190 return sdev->mmio_bar;
193 static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
195 if (sof_ops(sdev)->get_mailbox_offset)
196 return sof_ops(sdev)->get_mailbox_offset(sdev);
198 dev_err(sdev->dev, "error: %s not defined\n", __func__);
202 static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
205 if (sof_ops(sdev)->get_window_offset)
206 return sof_ops(sdev)->get_window_offset(sdev, id);
208 dev_err(sdev->dev, "error: %s not defined\n", __func__);
211 /* power management */
212 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
214 if (sof_ops(sdev)->resume)
215 return sof_ops(sdev)->resume(sdev);
220 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
223 if (sof_ops(sdev)->suspend)
224 return sof_ops(sdev)->suspend(sdev, target_state);
229 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
231 if (sof_ops(sdev)->runtime_resume)
232 return sof_ops(sdev)->runtime_resume(sdev);
237 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
239 if (sof_ops(sdev)->runtime_suspend)
240 return sof_ops(sdev)->runtime_suspend(sdev);
245 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
247 if (sof_ops(sdev)->runtime_idle)
248 return sof_ops(sdev)->runtime_idle(sdev);
253 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
255 if (sof_ops(sdev)->set_hw_params_upon_resume)
256 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
260 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
262 if (sof_ops(sdev)->set_clk)
263 return sof_ops(sdev)->set_clk(sdev, freq);
269 snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
270 const struct sof_dsp_power_state *target_state)
274 mutex_lock(&sdev->power_state_access);
276 if (sof_ops(sdev)->set_power_state)
277 ret = sof_ops(sdev)->set_power_state(sdev, target_state);
279 mutex_unlock(&sdev->power_state_access);
285 void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags);
287 static inline int snd_sof_debugfs_add_region_item(struct snd_sof_dev *sdev,
288 enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size,
289 const char *name, enum sof_debugfs_access_type access_type)
291 if (sof_ops(sdev) && sof_ops(sdev)->debugfs_add_region_item)
292 return sof_ops(sdev)->debugfs_add_region_item(sdev, blk_type, offset,
293 size, name, access_type);
299 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
300 u32 offset, u32 value)
302 if (sof_ops(sdev)->write) {
303 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
307 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
310 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
311 u32 offset, u64 value)
313 if (sof_ops(sdev)->write64) {
314 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
318 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
321 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
324 if (sof_ops(sdev)->read)
325 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
327 dev_err(sdev->dev, "error: %s not defined\n", __func__);
331 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
334 if (sof_ops(sdev)->read64)
335 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
337 dev_err(sdev->dev, "error: %s not defined\n", __func__);
342 static inline int snd_sof_dsp_block_read(struct snd_sof_dev *sdev,
343 enum snd_sof_fw_blk_type blk_type,
344 u32 offset, void *dest, size_t bytes)
346 return sof_ops(sdev)->block_read(sdev, blk_type, offset, dest, bytes);
349 static inline int snd_sof_dsp_block_write(struct snd_sof_dev *sdev,
350 enum snd_sof_fw_blk_type blk_type,
351 u32 offset, void *src, size_t bytes)
353 return sof_ops(sdev)->block_write(sdev, blk_type, offset, src, bytes);
357 static inline void snd_sof_dsp_mailbox_read(struct snd_sof_dev *sdev,
358 u32 offset, void *dest, size_t bytes)
360 if (sof_ops(sdev)->mailbox_read)
361 sof_ops(sdev)->mailbox_read(sdev, offset, dest, bytes);
364 static inline void snd_sof_dsp_mailbox_write(struct snd_sof_dev *sdev,
365 u32 offset, void *src, size_t bytes)
367 if (sof_ops(sdev)->mailbox_write)
368 sof_ops(sdev)->mailbox_write(sdev, offset, src, bytes);
372 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
373 struct snd_sof_ipc_msg *msg)
375 return sof_ops(sdev)->send_msg(sdev, msg);
380 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
381 struct snd_pcm_substream *substream)
383 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
384 return sof_ops(sdev)->pcm_open(sdev, substream);
389 /* disconnect pcm substream to a host stream */
391 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
392 struct snd_pcm_substream *substream)
394 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
395 return sof_ops(sdev)->pcm_close(sdev, substream);
400 /* host stream hw params */
402 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
403 struct snd_pcm_substream *substream,
404 struct snd_pcm_hw_params *params,
405 struct snd_sof_platform_stream_params *platform_params)
407 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
408 return sof_ops(sdev)->pcm_hw_params(sdev, substream, params,
414 /* host stream hw free */
416 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
417 struct snd_pcm_substream *substream)
419 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
420 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
425 /* host stream trigger */
427 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
428 struct snd_pcm_substream *substream, int cmd)
430 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
431 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
436 /* Firmware loading */
437 static inline int snd_sof_load_firmware(struct snd_sof_dev *sdev)
439 dev_dbg(sdev->dev, "loading firmware\n");
441 return sof_ops(sdev)->load_firmware(sdev);
444 /* host DSP message data */
445 static inline int snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
446 struct snd_pcm_substream *substream,
449 return sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
451 /* host side configuration of the stream's data offset in stream mailbox area */
453 snd_sof_set_stream_data_offset(struct snd_sof_dev *sdev,
454 struct snd_pcm_substream *substream,
457 if (sof_ops(sdev) && sof_ops(sdev)->set_stream_data_offset)
458 return sof_ops(sdev)->set_stream_data_offset(sdev, substream,
464 /* host stream pointer */
465 static inline snd_pcm_uframes_t
466 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
467 struct snd_pcm_substream *substream)
469 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
470 return sof_ops(sdev)->pcm_pointer(sdev, substream);
476 static inline int snd_sof_pcm_platform_ack(struct snd_sof_dev *sdev,
477 struct snd_pcm_substream *substream)
479 if (sof_ops(sdev) && sof_ops(sdev)->pcm_ack)
480 return sof_ops(sdev)->pcm_ack(sdev, substream);
487 snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
489 if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
490 return sof_ops(sdev)->machine_register(sdev, pdata);
496 snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
498 if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
499 sof_ops(sdev)->machine_unregister(sdev, pdata);
502 static inline struct snd_soc_acpi_mach *
503 snd_sof_machine_select(struct snd_sof_dev *sdev)
505 if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
506 return sof_ops(sdev)->machine_select(sdev);
512 snd_sof_set_mach_params(struct snd_soc_acpi_mach *mach,
513 struct snd_sof_dev *sdev)
515 if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
516 sof_ops(sdev)->set_mach_params(mach, sdev);
520 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
521 * until a condition is met or a timeout occurs
522 * @op: accessor function (takes @addr as its only argument)
523 * @addr: Address to poll
524 * @val: Variable to read the value into
525 * @cond: Break condition (usually involving @val)
526 * @sleep_us: Maximum time to sleep between reads in us (0
527 * tight-loops). Should be less than ~20ms since usleep_range
528 * is used (see Documentation/timers/timers-howto.rst).
529 * @timeout_us: Timeout in us, 0 means never timeout
531 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
532 * case, the last read value at @addr is stored in @val. Must not
533 * be called from atomic context if sleep_us or timeout_us are used.
535 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
537 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
539 u64 __timeout_us = (timeout_us); \
540 unsigned long __sleep_us = (sleep_us); \
541 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
542 might_sleep_if((__sleep_us) != 0); \
544 (val) = snd_sof_dsp_read(sdev, bar, offset); \
547 "FW Poll Status: reg[%#x]=%#x successful\n", \
551 if (__timeout_us && \
552 ktime_compare(ktime_get(), __timeout) > 0) { \
553 (val) = snd_sof_dsp_read(sdev, bar, offset); \
555 "FW Poll Status: reg[%#x]=%#x timedout\n", \
560 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
562 (cond) ? 0 : -ETIMEDOUT; \
565 /* This is for registers bits with attribute RWC */
566 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
567 u32 mask, u32 value);
569 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
570 u32 offset, u32 mask, u32 value);
572 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
573 u32 offset, u64 mask, u64 value);
575 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
576 u32 mask, u32 value);
578 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
579 u32 offset, u64 mask, u64 value);
581 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
582 u32 offset, u32 mask, u32 value);
584 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
585 u32 mask, u32 target, u32 timeout_ms,
588 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset, bool non_recoverable);