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. 5 * 6 * Copyright(c) 2019 Intel Corporation. All rights reserved. 7 * 8 * Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> 9 */ 10 11 #ifndef __SOUND_SOC_SOF_AUDIO_H 12 #define __SOUND_SOC_SOF_AUDIO_H 13 14 #include <linux/workqueue.h> 15 16 #include <sound/soc.h> 17 #include <sound/control.h> 18 #include <sound/sof/stream.h> /* needs to be included before control.h */ 19 #include <sound/sof/control.h> 20 #include <sound/sof/dai.h> 21 #include <sound/sof/topology.h> 22 #include "sof-priv.h" 23 24 #define SOF_AUDIO_PCM_DRV_NAME "sof-audio-component" 25 26 /* max number of FE PCMs before BEs */ 27 #define SOF_BE_PCM_BASE 16 28 29 #define DMA_CHAN_INVALID 0xFFFFFFFF 30 31 #define WIDGET_IS_DAI(id) ((id) == snd_soc_dapm_dai_in || (id) == snd_soc_dapm_dai_out) 32 33 /* PCM stream, mapped to FW component */ 34 struct snd_sof_pcm_stream { 35 u32 comp_id; 36 struct snd_dma_buffer page_table; 37 struct sof_ipc_stream_posn posn; 38 struct snd_pcm_substream *substream; 39 struct snd_compr_stream *cstream; 40 struct work_struct period_elapsed_work; 41 struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */ 42 bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */ 43 /* 44 * flag to indicate that the DSP pipelines should be kept 45 * active or not while suspending the stream 46 */ 47 bool suspend_ignored; 48 }; 49 50 /* ALSA SOF PCM device */ 51 struct snd_sof_pcm { 52 struct snd_soc_component *scomp; 53 struct snd_soc_tplg_pcm pcm; 54 struct snd_sof_pcm_stream stream[2]; 55 struct list_head list; /* list in sdev pcm list */ 56 struct snd_pcm_hw_params params[2]; 57 bool prepared[2]; /* PCM_PARAMS set successfully */ 58 }; 59 60 struct snd_sof_led_control { 61 unsigned int use_led; 62 unsigned int direction; 63 int led_value; 64 }; 65 66 /* ALSA SOF Kcontrol device */ 67 struct snd_sof_control { 68 struct snd_soc_component *scomp; 69 int comp_id; 70 int min_volume_step; /* min volume step for volume_table */ 71 int max_volume_step; /* max volume step for volume_table */ 72 int num_channels; 73 unsigned int access; 74 u32 readback_offset; /* offset to mmapped data if used */ 75 struct sof_ipc_ctrl_data *control_data; 76 u32 size; /* cdata size */ 77 u32 *volume_table; /* volume table computed from tlv data*/ 78 79 struct list_head list; /* list in sdev control list */ 80 81 struct snd_sof_led_control led_ctl; 82 83 /* if true, the control's data needs to be updated from Firmware */ 84 bool comp_data_dirty; 85 }; 86 87 struct snd_sof_widget; 88 89 /* ASoC SOF DAPM widget */ 90 struct snd_sof_widget { 91 struct snd_soc_component *scomp; 92 int comp_id; 93 int pipeline_id; 94 int complete; 95 int use_count; /* use_count will be protected by the PCM mutex held by the core */ 96 int core; 97 int id; 98 99 /* 100 * Flag indicating if the widget should be set up dynamically when a PCM is opened. 101 * This flag is only set for the scheduler type widget in topology. During topology 102 * loading, this flag is propagated to all the widgets belonging to the same pipeline. 103 * When this flag is not set, a widget is set up at the time of topology loading 104 * and retained until the DSP enters D3. It will need to be set up again when resuming 105 * from D3. 106 */ 107 bool dynamic_pipeline_widget; 108 109 struct snd_soc_dapm_widget *widget; 110 struct list_head list; /* list in sdev widget list */ 111 struct snd_sof_widget *pipe_widget; 112 113 const guid_t uuid; 114 115 void *private; /* core does not touch this */ 116 }; 117 118 /* ASoC SOF DAPM route */ 119 struct snd_sof_route { 120 struct snd_soc_component *scomp; 121 122 struct snd_soc_dapm_route *route; 123 struct list_head list; /* list in sdev route list */ 124 struct snd_sof_widget *src_widget; 125 struct snd_sof_widget *sink_widget; 126 bool setup; 127 128 void *private; 129 }; 130 131 /* ASoC DAI device */ 132 struct snd_sof_dai { 133 struct snd_soc_component *scomp; 134 const char *name; 135 136 struct sof_ipc_comp_dai *comp_dai; 137 int number_configs; 138 int current_config; 139 bool configured; /* DAI configured during BE hw_params */ 140 struct sof_ipc_dai_config *dai_config; 141 struct list_head list; /* list in sdev dai list */ 142 }; 143 144 /* 145 * Kcontrols. 146 */ 147 148 int snd_sof_volume_get(struct snd_kcontrol *kcontrol, 149 struct snd_ctl_elem_value *ucontrol); 150 int snd_sof_volume_put(struct snd_kcontrol *kcontrol, 151 struct snd_ctl_elem_value *ucontrol); 152 int snd_sof_volume_info(struct snd_kcontrol *kcontrol, 153 struct snd_ctl_elem_info *uinfo); 154 int snd_sof_switch_get(struct snd_kcontrol *kcontrol, 155 struct snd_ctl_elem_value *ucontrol); 156 int snd_sof_switch_put(struct snd_kcontrol *kcontrol, 157 struct snd_ctl_elem_value *ucontrol); 158 int snd_sof_enum_get(struct snd_kcontrol *kcontrol, 159 struct snd_ctl_elem_value *ucontrol); 160 int snd_sof_enum_put(struct snd_kcontrol *kcontrol, 161 struct snd_ctl_elem_value *ucontrol); 162 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol, 163 struct snd_ctl_elem_value *ucontrol); 164 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol, 165 struct snd_ctl_elem_value *ucontrol); 166 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol, 167 const unsigned int __user *binary_data, 168 unsigned int size); 169 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol, 170 unsigned int __user *binary_data, 171 unsigned int size); 172 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data, 173 unsigned int size); 174 void snd_sof_control_notify(struct snd_sof_dev *sdev, 175 struct sof_ipc_ctrl_data *cdata); 176 177 /* 178 * Topology. 179 * There is no snd_sof_free_topology since topology components will 180 * be freed by snd_soc_unregister_component, 181 */ 182 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file); 183 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev, 184 struct snd_sof_widget *swidget); 185 186 /* 187 * Stream IPC 188 */ 189 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp, 190 struct snd_sof_pcm *spcm, int direction, 191 struct sof_ipc_stream_posn *posn); 192 193 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp, 194 const char *name); 195 struct snd_sof_widget * 196 snd_sof_find_swidget_sname(struct snd_soc_component *scomp, 197 const char *pcm_name, int dir); 198 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp, 199 const char *name); 200 201 static inline 202 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp, 203 struct snd_soc_pcm_runtime *rtd) 204 { 205 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 206 207 struct snd_sof_pcm *spcm = NULL; 208 209 list_for_each_entry(spcm, &sdev->pcm_list, list) { 210 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id) 211 return spcm; 212 } 213 214 return NULL; 215 } 216 217 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp, 218 const char *name); 219 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp, 220 unsigned int comp_id, 221 int *direction); 222 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream); 223 void snd_sof_pcm_init_elapsed_work(struct work_struct *work); 224 225 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS) 226 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream); 227 void snd_sof_compr_init_elapsed_work(struct work_struct *work); 228 #else 229 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { } 230 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { } 231 #endif 232 233 /* 234 * Mixer IPC 235 */ 236 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set); 237 238 /* DAI link fixup */ 239 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params); 240 241 /* PM */ 242 int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify); 243 int sof_tear_down_pipelines(struct snd_sof_dev *sdev, bool verify); 244 int sof_set_hw_params_upon_resume(struct device *dev); 245 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev); 246 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev); 247 248 /* Machine driver enumeration */ 249 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata); 250 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata); 251 252 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget); 253 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget); 254 255 /* PCM */ 256 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir); 257 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir); 258 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev, 259 struct snd_sof_pcm *spcm); 260 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream, 261 struct snd_sof_pcm *spcm, int dir, bool free_widget_list); 262 int get_token_u32(void *elem, void *object, u32 offset); 263 int get_token_u16(void *elem, void *object, u32 offset); 264 int get_token_comp_format(void *elem, void *object, u32 offset); 265 int get_token_dai_type(void *elem, void *object, u32 offset); 266 int get_token_uuid(void *elem, void *object, u32 offset); 267 #endif 268