2018-07-02 09:23:30 +03:00
// SPDX-License-Identifier: GPL-2.0+
//
// soc-util.c -- ALSA SoC Audio Layer utility functions
//
// Copyright 2009 Wolfson Microelectronics PLC.
//
// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
// Liam Girdwood <lrg@slimlogic.co.uk>
2009-11-10 19:08:04 +03:00
2011-04-27 21:16:32 +04:00
# include <linux/platform_device.h>
2011-09-22 17:34:58 +04:00
# include <linux/export.h>
2022-04-05 16:54:16 +03:00
# include <linux/math.h>
2009-11-10 19:08:04 +03:00
# include <sound/core.h>
# include <sound/pcm.h>
# include <sound/pcm_params.h>
# include <sound/soc.h>
int snd_soc_calc_frame_size ( int sample_size , int channels , int tdm_slots )
{
return sample_size * channels * tdm_slots ;
}
EXPORT_SYMBOL_GPL ( snd_soc_calc_frame_size ) ;
int snd_soc_params_to_frame_size ( struct snd_pcm_hw_params * params )
{
int sample_size ;
2011-01-31 23:14:38 +03:00
sample_size = snd_pcm_format_width ( params_format ( params ) ) ;
if ( sample_size < 0 )
return sample_size ;
2009-11-10 19:08:04 +03:00
return snd_soc_calc_frame_size ( sample_size , params_channels ( params ) ,
1 ) ;
}
EXPORT_SYMBOL_GPL ( snd_soc_params_to_frame_size ) ;
2009-11-19 14:36:10 +03:00
int snd_soc_calc_bclk ( int fs , int sample_size , int channels , int tdm_slots )
{
return fs * snd_soc_calc_frame_size ( sample_size , channels , tdm_slots ) ;
}
EXPORT_SYMBOL_GPL ( snd_soc_calc_bclk ) ;
2009-11-10 19:08:04 +03:00
int snd_soc_params_to_bclk ( struct snd_pcm_hw_params * params )
{
int ret ;
ret = snd_soc_params_to_frame_size ( params ) ;
if ( ret > 0 )
return ret * params_rate ( params ) ;
else
return ret ;
}
EXPORT_SYMBOL_GPL ( snd_soc_params_to_bclk ) ;
2011-04-27 21:16:32 +04:00
2022-04-05 16:54:16 +03:00
/**
* snd_soc_tdm_params_to_bclk - calculate bclk from params and tdm slot info .
*
2022-08-15 14:33:46 +03:00
* Calculate the bclk from the params sample rate , the tdm slot count and the
* tdm slot width . Optionally round - up the slot count to a given multiple .
* Either or both of tdm_width and tdm_slots can be 0.
2022-04-05 16:54:16 +03:00
*
2022-08-15 14:33:46 +03:00
* If tdm_width = = 0 : use params_width ( ) as the slot width .
* If tdm_slots = = 0 : use params_channels ( ) as the slot count .
2022-04-05 16:54:16 +03:00
*
2022-08-15 14:33:46 +03:00
* If slot_multiple > 1 the slot count ( or params_channels ( ) if tdm_slots = = 0 )
* will be rounded up to a multiple of slot_multiple . This is mainly useful for
2022-04-05 16:54:16 +03:00
* I2S mode , which has a left and right phase so the number of slots is always
* a multiple of 2.
*
2022-08-15 14:33:46 +03:00
* If tdm_width = = 0 & & tdm_slots = = 0 & & slot_multiple < 2 , this is equivalent
* to calling snd_soc_params_to_bclk ( ) .
*
2022-04-05 16:54:16 +03:00
* @ params : Pointer to struct_pcm_hw_params .
2022-08-15 14:33:46 +03:00
* @ tdm_width : Width in bits of the tdm slots . Must be > = 0.
* @ tdm_slots : Number of tdm slots per frame . Must be > = 0.
2022-04-05 16:54:16 +03:00
* @ slot_multiple : If > 1 roundup slot count to a multiple of this value .
*
* Return : bclk frequency in Hz , else a negative error code if params format
* is invalid .
*/
int snd_soc_tdm_params_to_bclk ( struct snd_pcm_hw_params * params ,
int tdm_width , int tdm_slots , int slot_multiple )
{
if ( ! tdm_slots )
tdm_slots = params_channels ( params ) ;
if ( slot_multiple > 1 )
tdm_slots = roundup ( tdm_slots , slot_multiple ) ;
if ( ! tdm_width ) {
tdm_width = snd_pcm_format_width ( params_format ( params ) ) ;
if ( tdm_width < 0 )
return tdm_width ;
}
return snd_soc_calc_bclk ( params_rate ( params ) , tdm_width , 1 , tdm_slots ) ;
}
EXPORT_SYMBOL_GPL ( snd_soc_tdm_params_to_bclk ) ;
2011-12-06 00:50:45 +04:00
static const struct snd_pcm_hardware dummy_dma_hardware = {
/* Random values to keep userspace happy when checking constraints */
. info = SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_BLOCK_TRANSFER ,
. buffer_bytes_max = 128 * 1024 ,
. period_bytes_min = PAGE_SIZE ,
. period_bytes_max = PAGE_SIZE * 2 ,
. periods_min = 2 ,
. periods_max = 128 ,
} ;
2021-10-15 19:12:57 +03:00
static const struct snd_soc_component_driver dummy_platform ;
2019-10-02 08:35:05 +03:00
static int dummy_dma_open ( struct snd_soc_component * component ,
struct snd_pcm_substream * substream )
2011-12-06 00:50:45 +04:00
{
2023-09-12 02:52:02 +03:00
struct snd_soc_pcm_runtime * rtd = snd_soc_substream_to_rtd ( substream ) ;
2021-10-15 19:12:57 +03:00
int i ;
/*
* If there are other components associated with rtd , we shouldn ' t
* override their hwparams
*/
for_each_rtd_components ( rtd , i , component ) {
if ( component - > driver = = & dummy_platform )
return 0 ;
}
2013-09-13 21:09:47 +04:00
/* BE's dont need dummy params */
if ( ! rtd - > dai_link - > no_pcm )
snd_soc_set_runtime_hwparams ( substream , & dummy_dma_hardware ) ;
2011-12-06 00:50:45 +04:00
return 0 ;
}
2018-01-29 05:40:52 +03:00
static const struct snd_soc_component_driver dummy_platform = {
2019-10-02 08:35:05 +03:00
. open = dummy_dma_open ,
2011-12-06 00:50:45 +04:00
} ;
2011-04-27 21:16:32 +04:00
2018-01-29 05:58:01 +03:00
static const struct snd_soc_component_driver dummy_codec = {
. idle_bias_on = 1 ,
. use_pmdown_time = 1 ,
. endianness = 1 ,
} ;
2013-04-17 16:34:06 +04:00
2020-06-27 16:37:10 +03:00
# define STUB_RATES SNDRV_PCM_RATE_8000_384000
2013-04-17 16:34:06 +04:00
# define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
SNDRV_PCM_FMTBIT_U8 | \
SNDRV_PCM_FMTBIT_S16_LE | \
SNDRV_PCM_FMTBIT_U16_LE | \
SNDRV_PCM_FMTBIT_S24_LE | \
2020-06-27 16:37:10 +03:00
SNDRV_PCM_FMTBIT_S24_3LE | \
2013-04-17 16:34:06 +04:00
SNDRV_PCM_FMTBIT_U24_LE | \
SNDRV_PCM_FMTBIT_S32_LE | \
SNDRV_PCM_FMTBIT_U32_LE | \
SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE )
ASoC: soc-core: add snd_soc_runtime_get_dai_fmt()
ASoC is using dai_link which specify DAI format (= dai_link->dai_fmt),
and it is selected by "Sound Card" driver in corrent implementation.
In other words, Sound Card *needs* to setup it.
But, it should be possible to automatically selected from CPU and
Codec driver settings.
This patch adds new .auto_selectable_formats support
at snd_soc_dai_ops.
By this patch, dai_fmt can be automatically selected from each
driver if both CPU / Codec driver had it.
Automatically selectable *field* is depends on each drivers.
For example, some driver want to select format "automatically",
but want to select other fields "manually", because of complex limitation.
Or other example, in case of both CPU and Codec are possible to be
clock provider, but the quality was different.
In these case, user need/want to *manually* select each fields
from Sound Card driver.
This .auto_selectable_formats can set priority.
For example, no limitaion format can be HI priority,
supported but has picky limitation format can be next priority, etc.
It uses Sound Card specified fields preferentially, and try to select
non-specific fields from CPU and Codec driver automatically
if all drivers have .auto_selectable_formats.
In other words, we can select all dai_fmt via Sound Card driver
same as before.
Link: https://lore.kernel.org/r/871rb3hypy.wl-kuninori.morimoto.gx@renesas.com
Link: https://lore.kernel.org/r/871racbx0w.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Link: https://lore.kernel.org/r/87h7ionc8s.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2021-05-27 05:26:12 +03:00
/*
* Select these from Sound Card Manually
* SND_SOC_POSSIBLE_DAIFMT_CBP_CFP
* SND_SOC_POSSIBLE_DAIFMT_CBP_CFC
* SND_SOC_POSSIBLE_DAIFMT_CBC_CFP
* SND_SOC_POSSIBLE_DAIFMT_CBC_CFC
*/
static u64 dummy_dai_formats =
SND_SOC_POSSIBLE_DAIFMT_I2S |
SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
SND_SOC_POSSIBLE_DAIFMT_DSP_A |
SND_SOC_POSSIBLE_DAIFMT_DSP_B |
SND_SOC_POSSIBLE_DAIFMT_AC97 |
SND_SOC_POSSIBLE_DAIFMT_PDM |
SND_SOC_POSSIBLE_DAIFMT_GATED |
SND_SOC_POSSIBLE_DAIFMT_CONT |
SND_SOC_POSSIBLE_DAIFMT_NB_NF |
SND_SOC_POSSIBLE_DAIFMT_NB_IF |
SND_SOC_POSSIBLE_DAIFMT_IB_NF |
SND_SOC_POSSIBLE_DAIFMT_IB_IF ;
static const struct snd_soc_dai_ops dummy_dai_ops = {
. auto_selectable_formats = & dummy_dai_formats ,
. num_auto_selectable_formats = 1 ,
} ;
2015-08-31 18:24:23 +03:00
/*
* The dummy CODEC is only meant to be used in situations where there is no
* actual hardware .
*
* If there is actual hardware even if it does not have a control bus
* the hardware will still have constraints like supported samplerates , etc .
* which should be modelled . And the data flow graph also should be modelled
* using DAPM .
*/
2012-01-09 16:36:24 +04:00
static struct snd_soc_dai_driver dummy_dai = {
. name = " snd-soc-dummy-dai " ,
2013-04-17 16:34:06 +04:00
. playback = {
. stream_name = " Playback " ,
. channels_min = 1 ,
. channels_max = 384 ,
. rates = STUB_RATES ,
. formats = STUB_FORMATS ,
} ,
. capture = {
. stream_name = " Capture " ,
. channels_min = 1 ,
. channels_max = 384 ,
. rates = STUB_RATES ,
. formats = STUB_FORMATS ,
} ,
ASoC: soc-core: add snd_soc_runtime_get_dai_fmt()
ASoC is using dai_link which specify DAI format (= dai_link->dai_fmt),
and it is selected by "Sound Card" driver in corrent implementation.
In other words, Sound Card *needs* to setup it.
But, it should be possible to automatically selected from CPU and
Codec driver settings.
This patch adds new .auto_selectable_formats support
at snd_soc_dai_ops.
By this patch, dai_fmt can be automatically selected from each
driver if both CPU / Codec driver had it.
Automatically selectable *field* is depends on each drivers.
For example, some driver want to select format "automatically",
but want to select other fields "manually", because of complex limitation.
Or other example, in case of both CPU and Codec are possible to be
clock provider, but the quality was different.
In these case, user need/want to *manually* select each fields
from Sound Card driver.
This .auto_selectable_formats can set priority.
For example, no limitaion format can be HI priority,
supported but has picky limitation format can be next priority, etc.
It uses Sound Card specified fields preferentially, and try to select
non-specific fields from CPU and Codec driver automatically
if all drivers have .auto_selectable_formats.
In other words, we can select all dai_fmt via Sound Card driver
same as before.
Link: https://lore.kernel.org/r/871rb3hypy.wl-kuninori.morimoto.gx@renesas.com
Link: https://lore.kernel.org/r/871racbx0w.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Link: https://lore.kernel.org/r/87h7ionc8s.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2021-05-27 05:26:12 +03:00
. ops = & dummy_dai_ops ,
2012-01-09 16:36:24 +04:00
} ;
2014-01-08 14:40:18 +04:00
int snd_soc_dai_is_dummy ( struct snd_soc_dai * dai )
{
if ( dai - > driver = = & dummy_dai )
return 1 ;
return 0 ;
}
2023-09-07 18:02:24 +03:00
EXPORT_SYMBOL_GPL ( snd_soc_dai_is_dummy ) ;
2014-01-08 14:40:18 +04:00
2021-04-16 05:00:26 +03:00
int snd_soc_component_is_dummy ( struct snd_soc_component * component )
{
return ( ( component - > driver = = & dummy_platform ) | |
( component - > driver = = & dummy_codec ) ) ;
}
2023-09-12 02:47:02 +03:00
struct snd_soc_dai_link_component snd_soc_dummy_dlc = {
2023-04-24 05:35:32 +03:00
. of_node = NULL ,
. dai_name = " snd-soc-dummy-dai " ,
. name = " snd-soc-dummy " ,
} ;
2023-09-12 02:47:02 +03:00
EXPORT_SYMBOL_GPL ( snd_soc_dummy_dlc ) ;
2023-04-24 05:35:32 +03:00
2012-12-07 18:26:36 +04:00
static int snd_soc_dummy_probe ( struct platform_device * pdev )
2011-04-27 21:16:32 +04:00
{
2012-01-09 16:36:24 +04:00
int ret ;
2018-01-29 05:58:01 +03:00
ret = devm_snd_soc_register_component ( & pdev - > dev ,
& dummy_codec , & dummy_dai , 1 ) ;
2012-01-09 16:36:24 +04:00
if ( ret < 0 )
return ret ;
2018-01-29 05:40:52 +03:00
ret = devm_snd_soc_register_component ( & pdev - > dev , & dummy_platform ,
NULL , 0 ) ;
2012-01-09 16:36:24 +04:00
return ret ;
2011-04-27 21:16:32 +04:00
}
static struct platform_driver soc_dummy_driver = {
. driver = {
. name = " snd-soc-dummy " ,
} ,
. probe = snd_soc_dummy_probe ,
} ;
static struct platform_device * soc_dummy_dev ;
2011-04-28 13:57:54 +04:00
int __init snd_soc_util_init ( void )
2011-04-27 21:16:32 +04:00
{
int ret ;
2013-05-17 13:26:15 +04:00
soc_dummy_dev =
platform_device_register_simple ( " snd-soc-dummy " , - 1 , NULL , 0 ) ;
if ( IS_ERR ( soc_dummy_dev ) )
return PTR_ERR ( soc_dummy_dev ) ;
2011-04-27 21:16:32 +04:00
ret = platform_driver_register ( & soc_dummy_driver ) ;
if ( ret ! = 0 )
platform_device_unregister ( soc_dummy_dev ) ;
return ret ;
}
2022-10-31 16:40:31 +03:00
void snd_soc_util_exit ( void )
2011-04-27 21:16:32 +04:00
{
platform_driver_unregister ( & soc_dummy_driver ) ;
2018-06-26 14:58:35 +03:00
platform_device_unregister ( soc_dummy_dev ) ;
2011-04-27 21:16:32 +04:00
}