Path: blob/master/sound/soc/mediatek/mt8186/mt8186-misc-control.c
26488 views
// SPDX-License-Identifier: GPL-2.01//2// MediaTek ALSA SoC Audio Misc Control3//4// Copyright (c) 2022 MediaTek Inc.5// Author: Jiaxin Yu <[email protected]>67#include <linux/delay.h>8#include <linux/dma-mapping.h>9#include <linux/io.h>10#include <linux/regmap.h>11#include <sound/soc.h>1213#include "../common/mtk-afe-fe-dai.h"14#include "../common/mtk-afe-platform-driver.h"15#include "mt8186-afe-common.h"1617static const char * const mt8186_sgen_mode_str[] = {18"I0I1", "I2", "I3I4", "I5I6",19"I7I8", "I9I22", "I10I11", "I12I13",20"I14I21", "I15I16", "I17I18", "I19I20",21"I23I24", "I25I26", "I27I28", "I33",22"I34I35", "I36I37", "I38I39", "I40I41",23"I42I43", "I44I45", "I46I47", "I48I49",24"I56I57", "I58I59", "I60I61", "I62I63",25"O0O1", "O2", "O3O4", "O5O6",26"O7O8", "O9O10", "O11", "O12",27"O13O14", "O15O16", "O17O18", "O19O20",28"O21O22", "O23O24", "O25", "O28O29",29"O34", "O35", "O32O33", "O36O37",30"O38O39", "O30O31", "O40O41", "O42O43",31"O44O45", "O46O47", "O48O49", "O50O51",32"O58O59", "O60O61", "O62O63", "O64O65",33"O66O67", "O68O69", "O26O27", "OFF",34};3536static const int mt8186_sgen_mode_idx[] = {370, 2, 4, 6,388, 22, 10, 12,3914, -1, 18, 20,4024, 26, 28, 33,4134, 36, 38, 40,4242, 44, 46, 48,4356, 58, 60, 62,44128, 130, 132, 134,45135, 138, 139, 140,46142, 144, 166, 148,47150, 152, 153, 156,48162, 163, 160, 164,49166, -1, 168, 170,50172, 174, 176, 178,51186, 188, 190, 192,52194, 196, -1, -1,53};5455static const char * const mt8186_sgen_rate_str[] = {56"8K", "11K", "12K", "16K",57"22K", "24K", "32K", "44K",58"48K", "88k", "96k", "176k",59"192k"60};6162static const int mt8186_sgen_rate_idx[] = {630, 1, 2, 4,645, 6, 8, 9,6510, 11, 12, 13,661467};6869/* this order must match reg bit amp_div_ch1/2 */70static const char * const mt8186_sgen_amp_str[] = {71"1/128", "1/64", "1/32", "1/16", "1/8", "1/4", "1/2", "1" };7273static int mt8186_sgen_get(struct snd_kcontrol *kcontrol,74struct snd_ctl_elem_value *ucontrol)75{76struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);77struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);78struct mt8186_afe_private *afe_priv = afe->platform_priv;7980ucontrol->value.integer.value[0] = afe_priv->sgen_mode;8182return 0;83}8485static int mt8186_sgen_set(struct snd_kcontrol *kcontrol,86struct snd_ctl_elem_value *ucontrol)87{88struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);89struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);90struct mt8186_afe_private *afe_priv = afe->platform_priv;91struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;92int mode;93int mode_idx;9495if (ucontrol->value.enumerated.item[0] >= e->items)96return -EINVAL;9798mode = ucontrol->value.integer.value[0];99mode_idx = mt8186_sgen_mode_idx[mode];100101dev_dbg(afe->dev, "%s(), mode %d, mode_idx %d\n",102__func__, mode, mode_idx);103104if (mode == afe_priv->sgen_mode)105return 0;106107if (mode_idx >= 0) {108regmap_update_bits(afe->regmap, AFE_SINEGEN_CON2,109INNER_LOOP_BACK_MODE_MASK_SFT,110mode_idx << INNER_LOOP_BACK_MODE_SFT);111regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,112DAC_EN_MASK_SFT, BIT(DAC_EN_SFT));113} else {114/* disable sgen */115regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,116DAC_EN_MASK_SFT, 0);117regmap_update_bits(afe->regmap, AFE_SINEGEN_CON2,118INNER_LOOP_BACK_MODE_MASK_SFT,1190x3f << INNER_LOOP_BACK_MODE_SFT);120}121122afe_priv->sgen_mode = mode;123124return 1;125}126127static int mt8186_sgen_rate_get(struct snd_kcontrol *kcontrol,128struct snd_ctl_elem_value *ucontrol)129{130struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);131struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);132struct mt8186_afe_private *afe_priv = afe->platform_priv;133134ucontrol->value.integer.value[0] = afe_priv->sgen_rate;135136return 0;137}138139static int mt8186_sgen_rate_set(struct snd_kcontrol *kcontrol,140struct snd_ctl_elem_value *ucontrol)141{142struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);143struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);144struct mt8186_afe_private *afe_priv = afe->platform_priv;145struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;146int rate;147148if (ucontrol->value.enumerated.item[0] >= e->items)149return -EINVAL;150151rate = ucontrol->value.integer.value[0];152153dev_dbg(afe->dev, "%s(), rate %d\n", __func__, rate);154155if (rate == afe_priv->sgen_rate)156return 0;157158regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,159SINE_MODE_CH1_MASK_SFT,160mt8186_sgen_rate_idx[rate] << SINE_MODE_CH1_SFT);161162regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,163SINE_MODE_CH2_MASK_SFT,164mt8186_sgen_rate_idx[rate] << SINE_MODE_CH2_SFT);165166afe_priv->sgen_rate = rate;167168return 1;169}170171static int mt8186_sgen_amplitude_get(struct snd_kcontrol *kcontrol,172struct snd_ctl_elem_value *ucontrol)173{174struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);175struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);176struct mt8186_afe_private *afe_priv = afe->platform_priv;177178ucontrol->value.integer.value[0] = afe_priv->sgen_amplitude;179return 0;180}181182static int mt8186_sgen_amplitude_set(struct snd_kcontrol *kcontrol,183struct snd_ctl_elem_value *ucontrol)184{185struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);186struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt);187struct mt8186_afe_private *afe_priv = afe->platform_priv;188struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;189int amplitude;190191if (ucontrol->value.enumerated.item[0] >= e->items)192return -EINVAL;193194amplitude = ucontrol->value.integer.value[0];195if (amplitude > AMP_DIV_CH1_MASK) {196dev_err(afe->dev, "%s(), amplitude %d invalid\n",197__func__, amplitude);198return -EINVAL;199}200201dev_dbg(afe->dev, "%s(), amplitude %d\n", __func__, amplitude);202203if (amplitude == afe_priv->sgen_amplitude)204return 0;205206regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,207AMP_DIV_CH1_MASK_SFT,208amplitude << AMP_DIV_CH1_SFT);209regmap_update_bits(afe->regmap, AFE_SINEGEN_CON0,210AMP_DIV_CH2_MASK_SFT,211amplitude << AMP_DIV_CH2_SFT);212213afe_priv->sgen_amplitude = amplitude;214215return 1;216}217218static const struct soc_enum mt8186_afe_sgen_enum[] = {219SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mt8186_sgen_mode_str),220mt8186_sgen_mode_str),221SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mt8186_sgen_rate_str),222mt8186_sgen_rate_str),223SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mt8186_sgen_amp_str),224mt8186_sgen_amp_str),225};226227static const struct snd_kcontrol_new mt8186_afe_sgen_controls[] = {228SOC_ENUM_EXT("Audio_SineGen_Switch", mt8186_afe_sgen_enum[0],229mt8186_sgen_get, mt8186_sgen_set),230SOC_ENUM_EXT("Audio_SineGen_SampleRate", mt8186_afe_sgen_enum[1],231mt8186_sgen_rate_get, mt8186_sgen_rate_set),232SOC_ENUM_EXT("Audio_SineGen_Amplitude", mt8186_afe_sgen_enum[2],233mt8186_sgen_amplitude_get, mt8186_sgen_amplitude_set),234SOC_SINGLE("Audio_SineGen_Mute_Ch1", AFE_SINEGEN_CON0,235MUTE_SW_CH1_MASK_SFT, MUTE_SW_CH1_MASK, 0),236SOC_SINGLE("Audio_SineGen_Mute_Ch2", AFE_SINEGEN_CON0,237MUTE_SW_CH2_MASK_SFT, MUTE_SW_CH2_MASK, 0),238SOC_SINGLE("Audio_SineGen_Freq_Div_Ch1", AFE_SINEGEN_CON0,239FREQ_DIV_CH1_SFT, FREQ_DIV_CH1_MASK, 0),240SOC_SINGLE("Audio_SineGen_Freq_Div_Ch2", AFE_SINEGEN_CON0,241FREQ_DIV_CH2_SFT, FREQ_DIV_CH2_MASK, 0),242};243244int mt8186_add_misc_control(struct snd_soc_component *component)245{246snd_soc_add_component_controls(component,247mt8186_afe_sgen_controls,248ARRAY_SIZE(mt8186_afe_sgen_controls));249250return 0;251}252253254