123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
#ifndef __SOUND_SOC_SOF_IO_H
#define __SOUND_SOC_SOF_IO_H
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <sound/compress_driver.h>
#include <sound/pcm.h>
#include "sof-priv.h"
#define sof_ops(sdev) \
((sdev)->pdata->desc->ops)
static inline int sof_ops_init(struct snd_sof_dev *sdev)
{
if (sdev->pdata->desc->ops_init)
return sdev->pdata->desc->ops_init(sdev);
return 0;
}
static inline void sof_ops_free(struct snd_sof_dev *sdev)
{
if (sdev->pdata->desc->ops_free)
sdev->pdata->desc->ops_free(sdev);
}
static inline int snd_sof_probe_early(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->probe_early)
return sof_ops(sdev)->probe_early(sdev);
return 0;
}
static inline int snd_sof_probe(struct snd_sof_dev *sdev)
{
return sof_ops(sdev)->probe(sdev);
}
static inline void snd_sof_remove(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->remove)
sof_ops(sdev)->remove(sdev);
}
static inline void snd_sof_remove_late(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->remove_late)
sof_ops(sdev)->remove_late(sdev);
}
static inline int snd_sof_shutdown(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->shutdown)
return sof_ops(sdev)->shutdown(sdev);
return 0;
}
static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
{
return sof_ops(sdev)->run(sdev);
}
static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
{
if (sof_ops(sdev)->stall)
return sof_ops(sdev)->stall(sdev, core_mask);
return 0;
}
static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->reset)
return sof_ops(sdev)->reset(sdev);
return 0;
}
static inline int snd_sof_dsp_core_get(struct snd_sof_dev *sdev, int core)
{
if (core > sdev->num_cores - 1) {
dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
sdev->num_cores);
return -EINVAL;
}
if (sof_ops(sdev)->core_get) {
int ret;
if (sdev->dsp_core_ref_count[core] > 0) {
sdev->dsp_core_ref_count[core]++;
return 0;
}
ret = sof_ops(sdev)->core_get(sdev, core);
if (ret < 0)
return ret;
sdev->dsp_core_ref_count[core]++;
sdev->enabled_cores_mask |= BIT(core);
dev_dbg(sdev->dev, "Core %d powered up\n", core);
}
return 0;
}
static inline int snd_sof_dsp_core_put(struct snd_sof_dev *sdev, int core)
{
if (core > sdev->num_cores - 1) {
dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
sdev->num_cores);
return -EINVAL;
}
if (sof_ops(sdev)->core_put) {
int ret;
if (--(sdev->dsp_core_ref_count[core]) > 0)
return 0;
ret = sof_ops(sdev)->core_put(sdev, core);
if (ret < 0)
return ret;
sdev->enabled_cores_mask &= ~BIT(core);
dev_dbg(sdev->dev, "Core %d powered down\n", core);
}
return 0;
}
static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->pre_fw_run)
return sof_ops(sdev)->pre_fw_run(sdev);
return 0;
}
static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->post_fw_run)
return sof_ops(sdev)->post_fw_run(sdev);
return 0;
}
static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
const struct sof_ext_man_elem_header *hdr)
{
if (sof_ops(sdev)->parse_platform_ext_manifest)
return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
return 0;
}
static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
{
if (sof_ops(sdev)->get_bar_index)
return sof_ops(sdev)->get_bar_index(sdev, type);
return sdev->mmio_bar;
}
static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->get_mailbox_offset)
return sof_ops(sdev)->get_mailbox_offset(sdev);
dev_err(sdev->dev, "error: %s not defined\n", __func__);
return -EOPNOTSUPP;
}
static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
u32 id)
{
if (sof_ops(sdev)->get_window_offset)
return sof_ops(sdev)->get_window_offset(sdev, id);
dev_err(sdev->dev, "error: %s not defined\n", __func__);
return -EOPNOTSUPP;
}
static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->resume)
return sof_ops(sdev)->resume(sdev);
return 0;
}
static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
u32 target_state)
{
if (sof_ops(sdev)->suspend)
return sof_ops(sdev)->suspend(sdev, target_state);
return 0;
}
static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->runtime_resume)
return sof_ops(sdev)->runtime_resume(sdev);
return 0;
}
static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->runtime_suspend)
return sof_ops(sdev)->runtime_suspend(sdev);
return 0;
}
static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->runtime_idle)
return sof_ops(sdev)->runtime_idle(sdev);
return 0;
}
static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev)->set_hw_params_upon_resume)
return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
return 0;
}
static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
{
if (sof_ops(sdev)->set_clk)
return sof_ops(sdev)->set_clk(sdev, freq);
return 0;
}
static inline int
snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
const struct sof_dsp_power_state *target_state)
{
guard(mutex)(&sdev->power_state_access);
if (sof_ops(sdev)->set_power_state)
return sof_ops(sdev)->set_power_state(sdev, target_state);
return 0;
}
void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags);
static inline int snd_sof_debugfs_add_region_item(struct snd_sof_dev *sdev,
enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size,
const char *name, enum sof_debugfs_access_type access_type)
{
if (sof_ops(sdev) && sof_ops(sdev)->debugfs_add_region_item)
return sof_ops(sdev)->debugfs_add_region_item(sdev, blk_type, offset,
size, name, access_type);
return 0;
}
static inline void snd_sof_dsp_write8(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u8 value)
{
if (sof_ops(sdev)->write8)
sof_ops(sdev)->write8(sdev, sdev->bar[bar] + offset, value);
else
writeb(value, sdev->bar[bar] + offset);
}
static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u32 value)
{
if (sof_ops(sdev)->write)
sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
else
writel(value, sdev->bar[bar] + offset);
}
static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u64 value)
{
if (sof_ops(sdev)->write64)
sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
else
writeq(value, sdev->bar[bar] + offset);
}
static inline u8 snd_sof_dsp_read8(struct snd_sof_dev *sdev, u32 bar,
u32 offset)
{
if (sof_ops(sdev)->read8)
return sof_ops(sdev)->read8(sdev, sdev->bar[bar] + offset);
else
return readb(sdev->bar[bar] + offset);
}
static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
u32 offset)
{
if (sof_ops(sdev)->read)
return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
else
return readl(sdev->bar[bar] + offset);
}
static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
u32 offset)
{
if (sof_ops(sdev)->read64)
return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
else
return readq(sdev->bar[bar] + offset);
}
static inline void snd_sof_dsp_update8(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u8 mask, u8 value)
{
u8 reg;
reg = snd_sof_dsp_read8(sdev, bar, offset);
reg &= ~mask;
reg |= value;
snd_sof_dsp_write8(sdev, bar, offset, reg);
}
static inline int snd_sof_dsp_block_read(struct snd_sof_dev *sdev,
enum snd_sof_fw_blk_type blk_type,
u32 offset, void *dest, size_t bytes)
{
return sof_ops(sdev)->block_read(sdev, blk_type, offset, dest, bytes);
}
static inline int snd_sof_dsp_block_write(struct snd_sof_dev *sdev,
enum snd_sof_fw_blk_type blk_type,
u32 offset, void *src, size_t bytes)
{
return sof_ops(sdev)->block_write(sdev, blk_type, offset, src, bytes);
}
static inline void snd_sof_dsp_mailbox_read(struct snd_sof_dev *sdev,
u32 offset, void *dest, size_t bytes)
{
if (sof_ops(sdev)->mailbox_read)
sof_ops(sdev)->mailbox_read(sdev, offset, dest, bytes);
}
static inline void snd_sof_dsp_mailbox_write(struct snd_sof_dev *sdev,
u32 offset, void *src, size_t bytes)
{
if (sof_ops(sdev)->mailbox_write)
sof_ops(sdev)->mailbox_write(sdev, offset, src, bytes);
}
static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
struct snd_sof_ipc_msg *msg)
{
return sof_ops(sdev)->send_msg(sdev, msg);
}
static inline int
snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
return sof_ops(sdev)->pcm_open(sdev, substream);
return 0;
}
static inline int
snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
return sof_ops(sdev)->pcm_close(sdev, substream);
return 0;
}
static inline int
snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_sof_platform_stream_params *platform_params)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
return sof_ops(sdev)->pcm_hw_params(sdev, substream, params,
platform_params);
return 0;
}
static inline int
snd_sof_compr_platform_open(struct snd_sof_dev *sdev, struct snd_compr_stream *cstream)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_open)
return sof_ops(sdev)->compr_open(sdev, cstream);
return 0;
}
static inline int
snd_sof_compr_platform_close(struct snd_sof_dev *sdev, struct snd_compr_stream *cstream)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_close)
return sof_ops(sdev)->compr_close(sdev, cstream);
return 0;
}
static inline int
snd_sof_compr_platform_hw_params(struct snd_sof_dev *sdev,
struct snd_compr_stream *cstream,
struct snd_compr_params *params,
struct snd_sof_platform_stream_params *platform_params)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_hw_params)
return sof_ops(sdev)->compr_hw_params(sdev, cstream, params, platform_params);
return 0;
}
static inline int
snd_sof_compr_platform_hw_free(struct snd_sof_dev *sdev,
struct snd_compr_stream *cstream)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_hw_free)
return sof_ops(sdev)->compr_hw_free(sdev, cstream);
return 0;
}
static inline int
snd_sof_compr_platform_trigger(struct snd_sof_dev *sdev,
struct snd_compr_stream *cstream, int cmd)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_trigger)
return sof_ops(sdev)->compr_trigger(sdev, cstream, cmd);
return 0;
}
static inline int
snd_sof_compr_platform_pointer(struct snd_sof_dev *sdev,
struct snd_compr_stream *cstream,
struct snd_compr_tstamp64 *tstamp)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_pointer)
return sof_ops(sdev)->compr_pointer(sdev, cstream, tstamp);
return 0;
}
static inline u64
snd_sof_compr_get_dai_frame_counter(struct snd_sof_dev *sdev,
struct snd_compr_stream *cstream)
{
if (sof_ops(sdev) && sof_ops(sdev)->compr_get_dai_frame_counter)
return sof_ops(sdev)->compr_get_dai_frame_counter(sdev, cstream);
return 0;
}
static inline int
snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
return sof_ops(sdev)->pcm_hw_free(sdev, substream);
return 0;
}
static inline int
snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream, int cmd)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
return 0;
}
static inline int snd_sof_load_firmware(struct snd_sof_dev *sdev)
{
dev_dbg(sdev->dev, "loading firmware\n");
return sof_ops(sdev)->load_firmware(sdev);
}
static inline int snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
struct snd_sof_pcm_stream *sps,
void *p, size_t sz)
{
return sof_ops(sdev)->ipc_msg_data(sdev, sps, p, sz);
}
static inline int
snd_sof_set_stream_data_offset(struct snd_sof_dev *sdev,
struct snd_sof_pcm_stream *sps,
size_t posn_offset)
{
if (sof_ops(sdev) && sof_ops(sdev)->set_stream_data_offset)
return sof_ops(sdev)->set_stream_data_offset(sdev, sps,
posn_offset);
return 0;
}
static inline snd_pcm_uframes_t
snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
return sof_ops(sdev)->pcm_pointer(sdev, substream);
return 0;
}
static inline int snd_sof_pcm_platform_ack(struct snd_sof_dev *sdev,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->pcm_ack)
return sof_ops(sdev)->pcm_ack(sdev, substream);
return 0;
}
static inline u64
snd_sof_pcm_get_dai_frame_counter(struct snd_sof_dev *sdev,
struct snd_soc_component *component,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->get_dai_frame_counter)
return sof_ops(sdev)->get_dai_frame_counter(sdev, component,
substream);
return 0;
}
static inline u64
snd_sof_pcm_get_host_byte_counter(struct snd_sof_dev *sdev,
struct snd_soc_component *component,
struct snd_pcm_substream *substream)
{
if (sof_ops(sdev) && sof_ops(sdev)->get_host_byte_counter)
return sof_ops(sdev)->get_host_byte_counter(sdev, component,
substream);
return 0;
}
static inline int
snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
{
if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
return sof_ops(sdev)->machine_register(sdev, pdata);
return 0;
}
static inline void
snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
{
if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
sof_ops(sdev)->machine_unregister(sdev, pdata);
}
static inline struct snd_soc_acpi_mach *
snd_sof_machine_select(struct snd_sof_dev *sdev)
{
if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
return sof_ops(sdev)->machine_select(sdev);
return NULL;
}
static inline void
snd_sof_set_mach_params(struct snd_soc_acpi_mach *mach,
struct snd_sof_dev *sdev)
{
if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
sof_ops(sdev)->set_mach_params(mach, sdev);
}
static inline bool
snd_sof_is_chain_dma_supported(struct snd_sof_dev *sdev, u32 dai_type)
{
if (sof_ops(sdev) && sof_ops(sdev)->is_chain_dma_supported)
return sof_ops(sdev)->is_chain_dma_supported(sdev, dai_type);
return false;
}
#define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
({ \
u64 __timeout_us = (timeout_us); \
unsigned long __sleep_us = (sleep_us); \
ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
might_sleep_if((__sleep_us) != 0); \
for (;;) { \
(val) = snd_sof_dsp_read(sdev, bar, offset); \
if (cond) { \
dev_dbg(sdev->dev, \
"FW Poll Status: reg[%#x]=%#x successful\n", \
(offset), (val)); \
break; \
} \
if (__timeout_us && \
ktime_compare(ktime_get(), __timeout) > 0) { \
(val) = snd_sof_dsp_read(sdev, bar, offset); \
dev_dbg(sdev->dev, \
"FW Poll Status: reg[%#x]=%#x timedout\n", \
(offset), (val)); \
break; \
} \
if (__sleep_us) \
usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
} \
(cond) ? 0 : -ETIMEDOUT; \
})
bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
u32 mask, u32 value);
bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u32 mask, u32 value);
bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u64 mask, u64 value);
bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
u32 mask, u32 value);
bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u64 mask, u64 value);
void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
u32 offset, u32 mask, u32 value);
int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
u32 mask, u32 target, u32 timeout_ms,
u32 interval_us);
void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset, bool non_recoverable);
#endif