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