1 /* SPDX-License-Identifier: GPL-2.0 2 * 3 * soc-component.h 4 * 5 * Copyright (C) 2019 Renesas Electronics Corp. 6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 7 */ 8 #ifndef __SOC_COMPONENT_H 9 #define __SOC_COMPONENT_H 10 11 #include <sound/soc.h> 12 13 /* 14 * Component probe and remove ordering levels for components with runtime 15 * dependencies. 16 */ 17 #define SND_SOC_COMP_ORDER_FIRST -2 18 #define SND_SOC_COMP_ORDER_EARLY -1 19 #define SND_SOC_COMP_ORDER_NORMAL 0 20 #define SND_SOC_COMP_ORDER_LATE 1 21 #define SND_SOC_COMP_ORDER_LAST 2 22 23 #define for_each_comp_order(order) \ 24 for (order = SND_SOC_COMP_ORDER_FIRST; \ 25 order <= SND_SOC_COMP_ORDER_LAST; \ 26 order++) 27 28 /* component interface */ 29 struct snd_compress_ops { 30 int (*open)(struct snd_soc_component *component, 31 struct snd_compr_stream *stream); 32 int (*free)(struct snd_soc_component *component, 33 struct snd_compr_stream *stream); 34 int (*set_params)(struct snd_soc_component *component, 35 struct snd_compr_stream *stream, 36 struct snd_compr_params *params); 37 int (*get_params)(struct snd_soc_component *component, 38 struct snd_compr_stream *stream, 39 struct snd_codec *params); 40 int (*set_metadata)(struct snd_soc_component *component, 41 struct snd_compr_stream *stream, 42 struct snd_compr_metadata *metadata); 43 int (*get_metadata)(struct snd_soc_component *component, 44 struct snd_compr_stream *stream, 45 struct snd_compr_metadata *metadata); 46 int (*trigger)(struct snd_soc_component *component, 47 struct snd_compr_stream *stream, int cmd); 48 int (*pointer)(struct snd_soc_component *component, 49 struct snd_compr_stream *stream, 50 struct snd_compr_tstamp *tstamp); 51 int (*copy)(struct snd_soc_component *component, 52 struct snd_compr_stream *stream, char __user *buf, 53 size_t count); 54 int (*mmap)(struct snd_soc_component *component, 55 struct snd_compr_stream *stream, 56 struct vm_area_struct *vma); 57 int (*ack)(struct snd_soc_component *component, 58 struct snd_compr_stream *stream, size_t bytes); 59 int (*get_caps)(struct snd_soc_component *component, 60 struct snd_compr_stream *stream, 61 struct snd_compr_caps *caps); 62 int (*get_codec_caps)(struct snd_soc_component *component, 63 struct snd_compr_stream *stream, 64 struct snd_compr_codec_caps *codec); 65 }; 66 67 struct snd_soc_component_driver { 68 const char *name; 69 70 /* Default control and setup, added after probe() is run */ 71 const struct snd_kcontrol_new *controls; 72 unsigned int num_controls; 73 const struct snd_soc_dapm_widget *dapm_widgets; 74 unsigned int num_dapm_widgets; 75 const struct snd_soc_dapm_route *dapm_routes; 76 unsigned int num_dapm_routes; 77 78 int (*probe)(struct snd_soc_component *component); 79 void (*remove)(struct snd_soc_component *component); 80 int (*suspend)(struct snd_soc_component *component); 81 int (*resume)(struct snd_soc_component *component); 82 83 unsigned int (*read)(struct snd_soc_component *component, 84 unsigned int reg); 85 int (*write)(struct snd_soc_component *component, 86 unsigned int reg, unsigned int val); 87 88 /* pcm creation and destruction */ 89 int (*pcm_construct)(struct snd_soc_component *component, 90 struct snd_soc_pcm_runtime *rtd); 91 void (*pcm_destruct)(struct snd_soc_component *component, 92 struct snd_pcm *pcm); 93 94 /* component wide operations */ 95 int (*set_sysclk)(struct snd_soc_component *component, 96 int clk_id, int source, unsigned int freq, int dir); 97 int (*set_pll)(struct snd_soc_component *component, int pll_id, 98 int source, unsigned int freq_in, unsigned int freq_out); 99 int (*set_jack)(struct snd_soc_component *component, 100 struct snd_soc_jack *jack, void *data); 101 int (*get_jack_type)(struct snd_soc_component *component); 102 103 /* DT */ 104 int (*of_xlate_dai_name)(struct snd_soc_component *component, 105 const struct of_phandle_args *args, 106 const char **dai_name); 107 int (*of_xlate_dai_id)(struct snd_soc_component *comment, 108 struct device_node *endpoint); 109 void (*seq_notifier)(struct snd_soc_component *component, 110 enum snd_soc_dapm_type type, int subseq); 111 int (*stream_event)(struct snd_soc_component *component, int event); 112 int (*set_bias_level)(struct snd_soc_component *component, 113 enum snd_soc_bias_level level); 114 115 int (*open)(struct snd_soc_component *component, 116 struct snd_pcm_substream *substream); 117 int (*close)(struct snd_soc_component *component, 118 struct snd_pcm_substream *substream); 119 int (*ioctl)(struct snd_soc_component *component, 120 struct snd_pcm_substream *substream, 121 unsigned int cmd, void *arg); 122 int (*hw_params)(struct snd_soc_component *component, 123 struct snd_pcm_substream *substream, 124 struct snd_pcm_hw_params *params); 125 int (*hw_free)(struct snd_soc_component *component, 126 struct snd_pcm_substream *substream); 127 int (*prepare)(struct snd_soc_component *component, 128 struct snd_pcm_substream *substream); 129 int (*trigger)(struct snd_soc_component *component, 130 struct snd_pcm_substream *substream, int cmd); 131 int (*sync_stop)(struct snd_soc_component *component, 132 struct snd_pcm_substream *substream); 133 snd_pcm_uframes_t (*pointer)(struct snd_soc_component *component, 134 struct snd_pcm_substream *substream); 135 int (*get_time_info)(struct snd_soc_component *component, 136 struct snd_pcm_substream *substream, struct timespec64 *system_ts, 137 struct timespec64 *audio_ts, 138 struct snd_pcm_audio_tstamp_config *audio_tstamp_config, 139 struct snd_pcm_audio_tstamp_report *audio_tstamp_report); 140 int (*copy_user)(struct snd_soc_component *component, 141 struct snd_pcm_substream *substream, int channel, 142 unsigned long pos, void __user *buf, 143 unsigned long bytes); 144 struct page *(*page)(struct snd_soc_component *component, 145 struct snd_pcm_substream *substream, 146 unsigned long offset); 147 int (*mmap)(struct snd_soc_component *component, 148 struct snd_pcm_substream *substream, 149 struct vm_area_struct *vma); 150 int (*ack)(struct snd_soc_component *component, 151 struct snd_pcm_substream *substream); 152 snd_pcm_sframes_t (*delay)(struct snd_soc_component *component, 153 struct snd_pcm_substream *substream); 154 155 const struct snd_compress_ops *compress_ops; 156 157 /* probe ordering - for components with runtime dependencies */ 158 int probe_order; 159 int remove_order; 160 161 /* 162 * signal if the module handling the component should not be removed 163 * if a pcm is open. Setting this would prevent the module 164 * refcount being incremented in probe() but allow it be incremented 165 * when a pcm is opened and decremented when it is closed. 166 */ 167 unsigned int module_get_upon_open:1; 168 169 /* bits */ 170 unsigned int idle_bias_on:1; 171 unsigned int suspend_bias_off:1; 172 unsigned int use_pmdown_time:1; /* care pmdown_time at stop */ 173 /* 174 * Indicates that the component does not care about the endianness of 175 * PCM audio data and the core will ensure that both LE and BE variants 176 * of each used format are present. Typically this is because the 177 * component sits behind a bus that abstracts away the endian of the 178 * original data, ie. one for which the transmission endian is defined 179 * (I2S/SLIMbus/SoundWire), or the concept of endian doesn't exist (PDM, 180 * analogue). 181 */ 182 unsigned int endianness:1; 183 unsigned int legacy_dai_naming:1; 184 185 /* this component uses topology and ignore machine driver FEs */ 186 const char *ignore_machine; 187 const char *topology_name_prefix; 188 int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd, 189 struct snd_pcm_hw_params *params); 190 bool use_dai_pcm_id; /* use DAI link PCM ID as PCM device number */ 191 int be_pcm_base; /* base device ID for all BE PCMs */ 192 193 #ifdef CONFIG_DEBUG_FS 194 const char *debugfs_prefix; 195 #endif 196 }; 197 198 struct snd_soc_component { 199 const char *name; 200 int id; 201 const char *name_prefix; 202 struct device *dev; 203 struct snd_soc_card *card; 204 205 unsigned int active; 206 207 unsigned int suspended:1; /* is in suspend PM state */ 208 209 struct list_head list; 210 struct list_head card_aux_list; /* for auxiliary bound components */ 211 struct list_head card_list; 212 213 const struct snd_soc_component_driver *driver; 214 215 struct list_head dai_list; 216 int num_dai; 217 218 struct regmap *regmap; 219 int val_bytes; 220 221 struct mutex io_mutex; 222 223 /* attached dynamic objects */ 224 struct list_head dobj_list; 225 226 /* 227 * DO NOT use any of the fields below in drivers, they are temporary and 228 * are going to be removed again soon. If you use them in driver code 229 * the driver will be marked as BROKEN when these fields are removed. 230 */ 231 232 /* Don't use these, use snd_soc_component_get_dapm() */ 233 struct snd_soc_dapm_context dapm; 234 235 /* machine specific init */ 236 int (*init)(struct snd_soc_component *component); 237 238 /* function mark */ 239 void *mark_module; 240 struct snd_pcm_substream *mark_open; 241 struct snd_pcm_substream *mark_hw_params; 242 struct snd_pcm_substream *mark_trigger; 243 struct snd_compr_stream *mark_compr_open; 244 void *mark_pm; 245 246 struct dentry *debugfs_root; 247 const char *debugfs_prefix; 248 }; 249 250 #define for_each_component_dais(component, dai)\ 251 list_for_each_entry(dai, &(component)->dai_list, list) 252 #define for_each_component_dais_safe(component, dai, _dai)\ 253 list_for_each_entry_safe(dai, _dai, &(component)->dai_list, list) 254 255 /** 256 * snd_soc_dapm_to_component() - Casts a DAPM context to the component it is 257 * embedded in 258 * @dapm: The DAPM context to cast to the component 259 * 260 * This function must only be used on DAPM contexts that are known to be part of 261 * a component (e.g. in a component driver). Otherwise the behavior is 262 * undefined. 263 */ 264 static inline struct snd_soc_component *snd_soc_dapm_to_component( 265 struct snd_soc_dapm_context *dapm) 266 { 267 return container_of(dapm, struct snd_soc_component, dapm); 268 } 269 270 /** 271 * snd_soc_component_get_dapm() - Returns the DAPM context associated with a 272 * component 273 * @component: The component for which to get the DAPM context 274 */ 275 static inline struct snd_soc_dapm_context *snd_soc_component_get_dapm( 276 struct snd_soc_component *component) 277 { 278 return &component->dapm; 279 } 280 281 /** 282 * snd_soc_component_init_bias_level() - Initialize COMPONENT DAPM bias level 283 * @component: The COMPONENT for which to initialize the DAPM bias level 284 * @level: The DAPM level to initialize to 285 * 286 * Initializes the COMPONENT DAPM bias level. See snd_soc_dapm_init_bias_level() 287 */ 288 static inline void 289 snd_soc_component_init_bias_level(struct snd_soc_component *component, 290 enum snd_soc_bias_level level) 291 { 292 snd_soc_dapm_init_bias_level( 293 snd_soc_component_get_dapm(component), level); 294 } 295 296 /** 297 * snd_soc_component_get_bias_level() - Get current COMPONENT DAPM bias level 298 * @component: The COMPONENT for which to get the DAPM bias level 299 * 300 * Returns: The current DAPM bias level of the COMPONENT. 301 */ 302 static inline enum snd_soc_bias_level 303 snd_soc_component_get_bias_level(struct snd_soc_component *component) 304 { 305 return snd_soc_dapm_get_bias_level( 306 snd_soc_component_get_dapm(component)); 307 } 308 309 /** 310 * snd_soc_component_force_bias_level() - Set the COMPONENT DAPM bias level 311 * @component: The COMPONENT for which to set the level 312 * @level: The level to set to 313 * 314 * Forces the COMPONENT bias level to a specific state. See 315 * snd_soc_dapm_force_bias_level(). 316 */ 317 static inline int 318 snd_soc_component_force_bias_level(struct snd_soc_component *component, 319 enum snd_soc_bias_level level) 320 { 321 return snd_soc_dapm_force_bias_level( 322 snd_soc_component_get_dapm(component), 323 level); 324 } 325 326 /** 327 * snd_soc_dapm_kcontrol_component() - Returns the component associated to a 328 * kcontrol 329 * @kcontrol: The kcontrol 330 * 331 * This function must only be used on DAPM contexts that are known to be part of 332 * a COMPONENT (e.g. in a COMPONENT driver). Otherwise the behavior is undefined 333 */ 334 static inline struct snd_soc_component *snd_soc_dapm_kcontrol_component( 335 struct snd_kcontrol *kcontrol) 336 { 337 return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_dapm(kcontrol)); 338 } 339 340 /** 341 * snd_soc_component_cache_sync() - Sync the register cache with the hardware 342 * @component: COMPONENT to sync 343 * 344 * Note: This function will call regcache_sync() 345 */ 346 static inline int snd_soc_component_cache_sync( 347 struct snd_soc_component *component) 348 { 349 return regcache_sync(component->regmap); 350 } 351 352 void snd_soc_component_set_aux(struct snd_soc_component *component, 353 struct snd_soc_aux_dev *aux); 354 int snd_soc_component_init(struct snd_soc_component *component); 355 int snd_soc_component_is_dummy(struct snd_soc_component *component); 356 357 /* component IO */ 358 unsigned int snd_soc_component_read(struct snd_soc_component *component, 359 unsigned int reg); 360 int snd_soc_component_write(struct snd_soc_component *component, 361 unsigned int reg, unsigned int val); 362 int snd_soc_component_update_bits(struct snd_soc_component *component, 363 unsigned int reg, unsigned int mask, 364 unsigned int val); 365 int snd_soc_component_update_bits_async(struct snd_soc_component *component, 366 unsigned int reg, unsigned int mask, 367 unsigned int val); 368 void snd_soc_component_async_complete(struct snd_soc_component *component); 369 int snd_soc_component_test_bits(struct snd_soc_component *component, 370 unsigned int reg, unsigned int mask, 371 unsigned int value); 372 373 unsigned int snd_soc_component_read_field(struct snd_soc_component *component, 374 unsigned int reg, unsigned int mask); 375 int snd_soc_component_write_field(struct snd_soc_component *component, 376 unsigned int reg, unsigned int mask, 377 unsigned int val); 378 379 /* component wide operations */ 380 int snd_soc_component_set_sysclk(struct snd_soc_component *component, 381 int clk_id, int source, 382 unsigned int freq, int dir); 383 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id, 384 int source, unsigned int freq_in, 385 unsigned int freq_out); 386 int snd_soc_component_set_jack(struct snd_soc_component *component, 387 struct snd_soc_jack *jack, void *data); 388 int snd_soc_component_get_jack_type(struct snd_soc_component *component); 389 390 void snd_soc_component_seq_notifier(struct snd_soc_component *component, 391 enum snd_soc_dapm_type type, int subseq); 392 int snd_soc_component_stream_event(struct snd_soc_component *component, 393 int event); 394 int snd_soc_component_set_bias_level(struct snd_soc_component *component, 395 enum snd_soc_bias_level level); 396 397 void snd_soc_component_setup_regmap(struct snd_soc_component *component); 398 #ifdef CONFIG_REGMAP 399 void snd_soc_component_init_regmap(struct snd_soc_component *component, 400 struct regmap *regmap); 401 void snd_soc_component_exit_regmap(struct snd_soc_component *component); 402 #endif 403 404 #define snd_soc_component_module_get_when_probe(component)\ 405 snd_soc_component_module_get(component, NULL, 0) 406 #define snd_soc_component_module_get_when_open(component, substream) \ 407 snd_soc_component_module_get(component, substream, 1) 408 int snd_soc_component_module_get(struct snd_soc_component *component, 409 void *mark, int upon_open); 410 #define snd_soc_component_module_put_when_remove(component) \ 411 snd_soc_component_module_put(component, NULL, 0, 0) 412 #define snd_soc_component_module_put_when_close(component, substream, rollback) \ 413 snd_soc_component_module_put(component, substream, 1, rollback) 414 void snd_soc_component_module_put(struct snd_soc_component *component, 415 void *mark, int upon_open, int rollback); 416 417 static inline void snd_soc_component_set_drvdata(struct snd_soc_component *c, 418 void *data) 419 { 420 dev_set_drvdata(c->dev, data); 421 } 422 423 static inline void *snd_soc_component_get_drvdata(struct snd_soc_component *c) 424 { 425 return dev_get_drvdata(c->dev); 426 } 427 428 static inline unsigned int 429 snd_soc_component_active(struct snd_soc_component *component) 430 { 431 return component->active; 432 } 433 434 /* component pin */ 435 int snd_soc_component_enable_pin(struct snd_soc_component *component, 436 const char *pin); 437 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component, 438 const char *pin); 439 int snd_soc_component_disable_pin(struct snd_soc_component *component, 440 const char *pin); 441 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component, 442 const char *pin); 443 int snd_soc_component_nc_pin(struct snd_soc_component *component, 444 const char *pin); 445 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component, 446 const char *pin); 447 int snd_soc_component_get_pin_status(struct snd_soc_component *component, 448 const char *pin); 449 int snd_soc_component_force_enable_pin(struct snd_soc_component *component, 450 const char *pin); 451 int snd_soc_component_force_enable_pin_unlocked( 452 struct snd_soc_component *component, 453 const char *pin); 454 455 /* component driver ops */ 456 int snd_soc_component_open(struct snd_soc_component *component, 457 struct snd_pcm_substream *substream); 458 int snd_soc_component_close(struct snd_soc_component *component, 459 struct snd_pcm_substream *substream, 460 int rollback); 461 void snd_soc_component_suspend(struct snd_soc_component *component); 462 void snd_soc_component_resume(struct snd_soc_component *component); 463 int snd_soc_component_is_suspended(struct snd_soc_component *component); 464 int snd_soc_component_probe(struct snd_soc_component *component); 465 void snd_soc_component_remove(struct snd_soc_component *component); 466 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component, 467 struct device_node *ep); 468 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component, 469 const struct of_phandle_args *args, 470 const char **dai_name); 471 int snd_soc_component_compr_open(struct snd_soc_component *component, 472 struct snd_compr_stream *cstream); 473 void snd_soc_component_compr_free(struct snd_soc_component *component, 474 struct snd_compr_stream *cstream, 475 int rollback); 476 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd); 477 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream, 478 struct snd_compr_params *params); 479 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream, 480 struct snd_codec *params); 481 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream, 482 struct snd_compr_caps *caps); 483 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream, 484 struct snd_compr_codec_caps *codec); 485 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes); 486 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream, 487 struct snd_compr_tstamp *tstamp); 488 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream, 489 char __user *buf, size_t count); 490 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream, 491 struct snd_compr_metadata *metadata); 492 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream, 493 struct snd_compr_metadata *metadata); 494 495 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream); 496 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream, 497 unsigned int cmd, void *arg); 498 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream); 499 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream, 500 int channel, unsigned long pos, 501 void __user *buf, unsigned long bytes); 502 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream, 503 unsigned long offset); 504 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream, 505 struct vm_area_struct *vma); 506 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd); 507 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd); 508 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream); 509 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream, 510 struct snd_pcm_hw_params *params); 511 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream, 512 int rollback); 513 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream, 514 int cmd, int rollback); 515 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd, 516 void *stream); 517 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd, 518 void *stream, int rollback); 519 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream); 520 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream, 521 snd_pcm_sframes_t *cpu_delay, snd_pcm_sframes_t *codec_delay); 522 523 #endif /* __SOC_COMPONENT_H */ 524