1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2decbc00eSZhengShunQian /* 3decbc00eSZhengShunQian * Driver of Inno codec for rk3036 by Rockchip Inc. 4decbc00eSZhengShunQian * 5decbc00eSZhengShunQian * Author: Rockchip Inc. 6decbc00eSZhengShunQian * Author: Zheng ShunQian<zhengsq@rock-chips.com> 7decbc00eSZhengShunQian */ 8decbc00eSZhengShunQian 9decbc00eSZhengShunQian #include <sound/soc.h> 10decbc00eSZhengShunQian #include <sound/tlv.h> 11decbc00eSZhengShunQian #include <sound/soc-dapm.h> 12decbc00eSZhengShunQian #include <sound/soc-dai.h> 13decbc00eSZhengShunQian #include <sound/pcm.h> 14decbc00eSZhengShunQian #include <sound/pcm_params.h> 15decbc00eSZhengShunQian 16decbc00eSZhengShunQian #include <linux/platform_device.h> 17decbc00eSZhengShunQian #include <linux/of.h> 18decbc00eSZhengShunQian #include <linux/clk.h> 19decbc00eSZhengShunQian #include <linux/regmap.h> 20decbc00eSZhengShunQian #include <linux/device.h> 21decbc00eSZhengShunQian #include <linux/mfd/syscon.h> 22decbc00eSZhengShunQian #include <linux/module.h> 23decbc00eSZhengShunQian #include <linux/io.h> 24decbc00eSZhengShunQian 25decbc00eSZhengShunQian #include "inno_rk3036.h" 26decbc00eSZhengShunQian 27decbc00eSZhengShunQian struct rk3036_codec_priv { 28decbc00eSZhengShunQian void __iomem *base; 29decbc00eSZhengShunQian struct clk *pclk; 30decbc00eSZhengShunQian struct regmap *regmap; 31decbc00eSZhengShunQian struct device *dev; 32decbc00eSZhengShunQian }; 33decbc00eSZhengShunQian 34decbc00eSZhengShunQian static const DECLARE_TLV_DB_MINMAX(rk3036_codec_hp_tlv, -39, 0); 35decbc00eSZhengShunQian 36decbc00eSZhengShunQian static int rk3036_codec_antipop_info(struct snd_kcontrol *kcontrol, 37decbc00eSZhengShunQian struct snd_ctl_elem_info *uinfo) 38decbc00eSZhengShunQian { 39decbc00eSZhengShunQian uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 40decbc00eSZhengShunQian uinfo->count = 2; 41decbc00eSZhengShunQian uinfo->value.integer.min = 0; 42decbc00eSZhengShunQian uinfo->value.integer.max = 1; 43decbc00eSZhengShunQian 44decbc00eSZhengShunQian return 0; 45decbc00eSZhengShunQian } 46decbc00eSZhengShunQian 47decbc00eSZhengShunQian static int rk3036_codec_antipop_get(struct snd_kcontrol *kcontrol, 48decbc00eSZhengShunQian struct snd_ctl_elem_value *ucontrol) 49decbc00eSZhengShunQian { 50decbc00eSZhengShunQian struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 51decbc00eSZhengShunQian int val, ret, regval; 52decbc00eSZhengShunQian 53decbc00eSZhengShunQian ret = snd_soc_component_read(component, INNO_R09, ®val); 54decbc00eSZhengShunQian if (ret) 55decbc00eSZhengShunQian return ret; 56decbc00eSZhengShunQian val = ((regval >> INNO_R09_HPL_ANITPOP_SHIFT) & 57decbc00eSZhengShunQian INNO_R09_HP_ANTIPOP_MSK) == INNO_R09_HP_ANTIPOP_ON; 58decbc00eSZhengShunQian ucontrol->value.integer.value[0] = val; 59decbc00eSZhengShunQian 60decbc00eSZhengShunQian val = ((regval >> INNO_R09_HPR_ANITPOP_SHIFT) & 61decbc00eSZhengShunQian INNO_R09_HP_ANTIPOP_MSK) == INNO_R09_HP_ANTIPOP_ON; 62decbc00eSZhengShunQian ucontrol->value.integer.value[1] = val; 63decbc00eSZhengShunQian 64decbc00eSZhengShunQian return 0; 65decbc00eSZhengShunQian } 66decbc00eSZhengShunQian 67decbc00eSZhengShunQian static int rk3036_codec_antipop_put(struct snd_kcontrol *kcontrol, 68decbc00eSZhengShunQian struct snd_ctl_elem_value *ucontrol) 69decbc00eSZhengShunQian { 70decbc00eSZhengShunQian struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 71decbc00eSZhengShunQian int val, ret, regmsk; 72decbc00eSZhengShunQian 73decbc00eSZhengShunQian val = (ucontrol->value.integer.value[0] ? 74decbc00eSZhengShunQian INNO_R09_HP_ANTIPOP_ON : INNO_R09_HP_ANTIPOP_OFF) << 75decbc00eSZhengShunQian INNO_R09_HPL_ANITPOP_SHIFT; 76decbc00eSZhengShunQian val |= (ucontrol->value.integer.value[1] ? 77decbc00eSZhengShunQian INNO_R09_HP_ANTIPOP_ON : INNO_R09_HP_ANTIPOP_OFF) << 78decbc00eSZhengShunQian INNO_R09_HPR_ANITPOP_SHIFT; 79decbc00eSZhengShunQian 80decbc00eSZhengShunQian regmsk = INNO_R09_HP_ANTIPOP_MSK << INNO_R09_HPL_ANITPOP_SHIFT | 81decbc00eSZhengShunQian INNO_R09_HP_ANTIPOP_MSK << INNO_R09_HPR_ANITPOP_SHIFT; 82decbc00eSZhengShunQian 83decbc00eSZhengShunQian ret = snd_soc_component_update_bits(component, INNO_R09, 84decbc00eSZhengShunQian regmsk, val); 85decbc00eSZhengShunQian if (ret < 0) 86decbc00eSZhengShunQian return ret; 87decbc00eSZhengShunQian 88decbc00eSZhengShunQian return 0; 89decbc00eSZhengShunQian } 90decbc00eSZhengShunQian 91decbc00eSZhengShunQian #define SOC_RK3036_CODEC_ANTIPOP_DECL(xname) \ 92decbc00eSZhengShunQian { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ 93decbc00eSZhengShunQian .info = rk3036_codec_antipop_info, .get = rk3036_codec_antipop_get, \ 94decbc00eSZhengShunQian .put = rk3036_codec_antipop_put, } 95decbc00eSZhengShunQian 96decbc00eSZhengShunQian static const struct snd_kcontrol_new rk3036_codec_dapm_controls[] = { 97decbc00eSZhengShunQian SOC_DOUBLE_R_RANGE_TLV("Headphone Volume", INNO_R07, INNO_R08, 98decbc00eSZhengShunQian INNO_HP_GAIN_SHIFT, INNO_HP_GAIN_N39DB, 99decbc00eSZhengShunQian INNO_HP_GAIN_0DB, 0, rk3036_codec_hp_tlv), 100decbc00eSZhengShunQian SOC_DOUBLE("Zero Cross Switch", INNO_R06, INNO_R06_VOUTL_CZ_SHIFT, 101decbc00eSZhengShunQian INNO_R06_VOUTR_CZ_SHIFT, 1, 0), 102decbc00eSZhengShunQian SOC_DOUBLE("Headphone Switch", INNO_R09, INNO_R09_HPL_MUTE_SHIFT, 103decbc00eSZhengShunQian INNO_R09_HPR_MUTE_SHIFT, 1, 0), 104decbc00eSZhengShunQian SOC_RK3036_CODEC_ANTIPOP_DECL("Anti-pop Switch"), 105decbc00eSZhengShunQian }; 106decbc00eSZhengShunQian 107decbc00eSZhengShunQian static const struct snd_kcontrol_new rk3036_codec_hpl_mixer_controls[] = { 108decbc00eSZhengShunQian SOC_DAPM_SINGLE("DAC Left Out Switch", INNO_R09, 109decbc00eSZhengShunQian INNO_R09_DACL_SWITCH_SHIFT, 1, 0), 110decbc00eSZhengShunQian }; 111decbc00eSZhengShunQian 112decbc00eSZhengShunQian static const struct snd_kcontrol_new rk3036_codec_hpr_mixer_controls[] = { 113decbc00eSZhengShunQian SOC_DAPM_SINGLE("DAC Right Out Switch", INNO_R09, 114decbc00eSZhengShunQian INNO_R09_DACR_SWITCH_SHIFT, 1, 0), 115decbc00eSZhengShunQian }; 116decbc00eSZhengShunQian 117decbc00eSZhengShunQian static const struct snd_kcontrol_new rk3036_codec_hpl_switch_controls[] = { 118decbc00eSZhengShunQian SOC_DAPM_SINGLE("HP Left Out Switch", INNO_R05, 119decbc00eSZhengShunQian INNO_R05_HPL_WORK_SHIFT, 1, 0), 120decbc00eSZhengShunQian }; 121decbc00eSZhengShunQian 122decbc00eSZhengShunQian static const struct snd_kcontrol_new rk3036_codec_hpr_switch_controls[] = { 123decbc00eSZhengShunQian SOC_DAPM_SINGLE("HP Right Out Switch", INNO_R05, 124decbc00eSZhengShunQian INNO_R05_HPR_WORK_SHIFT, 1, 0), 125decbc00eSZhengShunQian }; 126decbc00eSZhengShunQian 127decbc00eSZhengShunQian static const struct snd_soc_dapm_widget rk3036_codec_dapm_widgets[] = { 128decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DAC PWR", 1, INNO_R06, 129decbc00eSZhengShunQian INNO_R06_DAC_EN_SHIFT, 0, NULL, 0), 130decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACL VREF", 2, INNO_R04, 131decbc00eSZhengShunQian INNO_R04_DACL_VREF_SHIFT, 0, NULL, 0), 132decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACR VREF", 2, INNO_R04, 133decbc00eSZhengShunQian INNO_R04_DACR_VREF_SHIFT, 0, NULL, 0), 134decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACL HiLo VREF", 3, INNO_R06, 135decbc00eSZhengShunQian INNO_R06_DACL_HILO_VREF_SHIFT, 0, NULL, 0), 136decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACR HiLo VREF", 3, INNO_R06, 137decbc00eSZhengShunQian INNO_R06_DACR_HILO_VREF_SHIFT, 0, NULL, 0), 138decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACR CLK", 3, INNO_R04, 139decbc00eSZhengShunQian INNO_R04_DACR_CLK_SHIFT, 0, NULL, 0), 140decbc00eSZhengShunQian SND_SOC_DAPM_SUPPLY_S("DACL CLK", 3, INNO_R04, 141decbc00eSZhengShunQian INNO_R04_DACL_CLK_SHIFT, 0, NULL, 0), 142decbc00eSZhengShunQian 143decbc00eSZhengShunQian SND_SOC_DAPM_DAC("DACL", "Left Playback", INNO_R04, 144decbc00eSZhengShunQian INNO_R04_DACL_SW_SHIFT, 0), 145decbc00eSZhengShunQian SND_SOC_DAPM_DAC("DACR", "Right Playback", INNO_R04, 146decbc00eSZhengShunQian INNO_R04_DACR_SW_SHIFT, 0), 147decbc00eSZhengShunQian 148decbc00eSZhengShunQian SND_SOC_DAPM_MIXER("Left Headphone Mixer", SND_SOC_NOPM, 0, 0, 149decbc00eSZhengShunQian rk3036_codec_hpl_mixer_controls, 150decbc00eSZhengShunQian ARRAY_SIZE(rk3036_codec_hpl_mixer_controls)), 151decbc00eSZhengShunQian SND_SOC_DAPM_MIXER("Right Headphone Mixer", SND_SOC_NOPM, 0, 0, 152decbc00eSZhengShunQian rk3036_codec_hpr_mixer_controls, 153decbc00eSZhengShunQian ARRAY_SIZE(rk3036_codec_hpr_mixer_controls)), 154decbc00eSZhengShunQian 155decbc00eSZhengShunQian SND_SOC_DAPM_PGA("HP Left Out", INNO_R05, 156decbc00eSZhengShunQian INNO_R05_HPL_EN_SHIFT, 0, NULL, 0), 157decbc00eSZhengShunQian SND_SOC_DAPM_PGA("HP Right Out", INNO_R05, 158decbc00eSZhengShunQian INNO_R05_HPR_EN_SHIFT, 0, NULL, 0), 159decbc00eSZhengShunQian 160decbc00eSZhengShunQian SND_SOC_DAPM_MIXER("HP Left Switch", SND_SOC_NOPM, 0, 0, 161decbc00eSZhengShunQian rk3036_codec_hpl_switch_controls, 162decbc00eSZhengShunQian ARRAY_SIZE(rk3036_codec_hpl_switch_controls)), 163decbc00eSZhengShunQian SND_SOC_DAPM_MIXER("HP Right Switch", SND_SOC_NOPM, 0, 0, 164decbc00eSZhengShunQian rk3036_codec_hpr_switch_controls, 165decbc00eSZhengShunQian ARRAY_SIZE(rk3036_codec_hpr_switch_controls)), 166decbc00eSZhengShunQian 167decbc00eSZhengShunQian SND_SOC_DAPM_OUTPUT("HPL"), 168decbc00eSZhengShunQian SND_SOC_DAPM_OUTPUT("HPR"), 169decbc00eSZhengShunQian }; 170decbc00eSZhengShunQian 171decbc00eSZhengShunQian static const struct snd_soc_dapm_route rk3036_codec_dapm_routes[] = { 172decbc00eSZhengShunQian {"DACL VREF", NULL, "DAC PWR"}, 173decbc00eSZhengShunQian {"DACR VREF", NULL, "DAC PWR"}, 174decbc00eSZhengShunQian {"DACL HiLo VREF", NULL, "DAC PWR"}, 175decbc00eSZhengShunQian {"DACR HiLo VREF", NULL, "DAC PWR"}, 176decbc00eSZhengShunQian {"DACL CLK", NULL, "DAC PWR"}, 177decbc00eSZhengShunQian {"DACR CLK", NULL, "DAC PWR"}, 178decbc00eSZhengShunQian 179decbc00eSZhengShunQian {"DACL", NULL, "DACL VREF"}, 180decbc00eSZhengShunQian {"DACL", NULL, "DACL HiLo VREF"}, 181decbc00eSZhengShunQian {"DACL", NULL, "DACL CLK"}, 182decbc00eSZhengShunQian {"DACR", NULL, "DACR VREF"}, 183decbc00eSZhengShunQian {"DACR", NULL, "DACR HiLo VREF"}, 184decbc00eSZhengShunQian {"DACR", NULL, "DACR CLK"}, 185decbc00eSZhengShunQian 186decbc00eSZhengShunQian {"Left Headphone Mixer", "DAC Left Out Switch", "DACL"}, 187decbc00eSZhengShunQian {"Right Headphone Mixer", "DAC Right Out Switch", "DACR"}, 188decbc00eSZhengShunQian {"HP Left Out", NULL, "Left Headphone Mixer"}, 189decbc00eSZhengShunQian {"HP Right Out", NULL, "Right Headphone Mixer"}, 190decbc00eSZhengShunQian 191decbc00eSZhengShunQian {"HP Left Switch", "HP Left Out Switch", "HP Left Out"}, 192decbc00eSZhengShunQian {"HP Right Switch", "HP Right Out Switch", "HP Right Out"}, 193decbc00eSZhengShunQian 194decbc00eSZhengShunQian {"HPL", NULL, "HP Left Switch"}, 195decbc00eSZhengShunQian {"HPR", NULL, "HP Right Switch"}, 196decbc00eSZhengShunQian }; 197decbc00eSZhengShunQian 198decbc00eSZhengShunQian static int rk3036_codec_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 199decbc00eSZhengShunQian { 20040aa60a2SKuninori Morimoto struct snd_soc_component *component = dai->component; 201decbc00eSZhengShunQian unsigned int reg01_val = 0, reg02_val = 0, reg03_val = 0; 202decbc00eSZhengShunQian 20340aa60a2SKuninori Morimoto dev_dbg(component->dev, "rk3036_codec dai set fmt : %08x\n", fmt); 204decbc00eSZhengShunQian 205decbc00eSZhengShunQian switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 206decbc00eSZhengShunQian case SND_SOC_DAIFMT_CBS_CFS: 207decbc00eSZhengShunQian reg01_val |= INNO_R01_PINDIR_IN_SLAVE | 208decbc00eSZhengShunQian INNO_R01_I2SMODE_SLAVE; 209decbc00eSZhengShunQian break; 210decbc00eSZhengShunQian case SND_SOC_DAIFMT_CBM_CFM: 211decbc00eSZhengShunQian reg01_val |= INNO_R01_PINDIR_OUT_MASTER | 212decbc00eSZhengShunQian INNO_R01_I2SMODE_MASTER; 213decbc00eSZhengShunQian break; 214decbc00eSZhengShunQian default: 21540aa60a2SKuninori Morimoto dev_err(component->dev, "invalid fmt\n"); 216decbc00eSZhengShunQian return -EINVAL; 217decbc00eSZhengShunQian } 218decbc00eSZhengShunQian 219decbc00eSZhengShunQian switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 220decbc00eSZhengShunQian case SND_SOC_DAIFMT_DSP_A: 221decbc00eSZhengShunQian reg02_val |= INNO_R02_DACM_PCM; 222decbc00eSZhengShunQian break; 223decbc00eSZhengShunQian case SND_SOC_DAIFMT_I2S: 224decbc00eSZhengShunQian reg02_val |= INNO_R02_DACM_I2S; 225decbc00eSZhengShunQian break; 226decbc00eSZhengShunQian case SND_SOC_DAIFMT_RIGHT_J: 227decbc00eSZhengShunQian reg02_val |= INNO_R02_DACM_RJM; 228decbc00eSZhengShunQian break; 229decbc00eSZhengShunQian case SND_SOC_DAIFMT_LEFT_J: 230decbc00eSZhengShunQian reg02_val |= INNO_R02_DACM_LJM; 231decbc00eSZhengShunQian break; 232decbc00eSZhengShunQian default: 23340aa60a2SKuninori Morimoto dev_err(component->dev, "set dai format failed\n"); 234decbc00eSZhengShunQian return -EINVAL; 235decbc00eSZhengShunQian } 236decbc00eSZhengShunQian 237decbc00eSZhengShunQian switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 238decbc00eSZhengShunQian case SND_SOC_DAIFMT_NB_NF: 239decbc00eSZhengShunQian reg02_val |= INNO_R02_LRCP_NORMAL; 240decbc00eSZhengShunQian reg03_val |= INNO_R03_BCP_NORMAL; 241decbc00eSZhengShunQian break; 242decbc00eSZhengShunQian case SND_SOC_DAIFMT_IB_IF: 243decbc00eSZhengShunQian reg02_val |= INNO_R02_LRCP_REVERSAL; 244decbc00eSZhengShunQian reg03_val |= INNO_R03_BCP_REVERSAL; 245decbc00eSZhengShunQian break; 246decbc00eSZhengShunQian case SND_SOC_DAIFMT_IB_NF: 247decbc00eSZhengShunQian reg02_val |= INNO_R02_LRCP_REVERSAL; 248decbc00eSZhengShunQian reg03_val |= INNO_R03_BCP_NORMAL; 249decbc00eSZhengShunQian break; 250decbc00eSZhengShunQian case SND_SOC_DAIFMT_NB_IF: 251decbc00eSZhengShunQian reg02_val |= INNO_R02_LRCP_NORMAL; 252decbc00eSZhengShunQian reg03_val |= INNO_R03_BCP_REVERSAL; 253decbc00eSZhengShunQian break; 254decbc00eSZhengShunQian default: 25540aa60a2SKuninori Morimoto dev_err(component->dev, "set dai format failed\n"); 256decbc00eSZhengShunQian return -EINVAL; 257decbc00eSZhengShunQian } 258decbc00eSZhengShunQian 25940aa60a2SKuninori Morimoto snd_soc_component_update_bits(component, INNO_R01, INNO_R01_I2SMODE_MSK | 260decbc00eSZhengShunQian INNO_R01_PINDIR_MSK, reg01_val); 26140aa60a2SKuninori Morimoto snd_soc_component_update_bits(component, INNO_R02, INNO_R02_LRCP_MSK | 262decbc00eSZhengShunQian INNO_R02_DACM_MSK, reg02_val); 26340aa60a2SKuninori Morimoto snd_soc_component_update_bits(component, INNO_R03, INNO_R03_BCP_MSK, reg03_val); 264decbc00eSZhengShunQian 265decbc00eSZhengShunQian return 0; 266decbc00eSZhengShunQian } 267decbc00eSZhengShunQian 268decbc00eSZhengShunQian static int rk3036_codec_dai_hw_params(struct snd_pcm_substream *substream, 269decbc00eSZhengShunQian struct snd_pcm_hw_params *hw_params, 270decbc00eSZhengShunQian struct snd_soc_dai *dai) 271decbc00eSZhengShunQian { 27240aa60a2SKuninori Morimoto struct snd_soc_component *component = dai->component; 273decbc00eSZhengShunQian unsigned int reg02_val = 0, reg03_val = 0; 274decbc00eSZhengShunQian 275decbc00eSZhengShunQian switch (params_format(hw_params)) { 276decbc00eSZhengShunQian case SNDRV_PCM_FORMAT_S16_LE: 277decbc00eSZhengShunQian reg02_val |= INNO_R02_VWL_16BIT; 278decbc00eSZhengShunQian break; 279decbc00eSZhengShunQian case SNDRV_PCM_FORMAT_S20_3LE: 280decbc00eSZhengShunQian reg02_val |= INNO_R02_VWL_20BIT; 281decbc00eSZhengShunQian break; 282decbc00eSZhengShunQian case SNDRV_PCM_FORMAT_S24_LE: 283decbc00eSZhengShunQian reg02_val |= INNO_R02_VWL_24BIT; 284decbc00eSZhengShunQian break; 285decbc00eSZhengShunQian case SNDRV_PCM_FORMAT_S32_LE: 286decbc00eSZhengShunQian reg02_val |= INNO_R02_VWL_32BIT; 287decbc00eSZhengShunQian break; 288decbc00eSZhengShunQian default: 289decbc00eSZhengShunQian return -EINVAL; 290decbc00eSZhengShunQian } 291decbc00eSZhengShunQian 292decbc00eSZhengShunQian reg02_val |= INNO_R02_LRCP_NORMAL; 293decbc00eSZhengShunQian reg03_val |= INNO_R03_FWL_32BIT | INNO_R03_DACR_WORK; 294decbc00eSZhengShunQian 29540aa60a2SKuninori Morimoto snd_soc_component_update_bits(component, INNO_R02, INNO_R02_LRCP_MSK | 296decbc00eSZhengShunQian INNO_R02_VWL_MSK, reg02_val); 29740aa60a2SKuninori Morimoto snd_soc_component_update_bits(component, INNO_R03, INNO_R03_DACR_MSK | 298decbc00eSZhengShunQian INNO_R03_FWL_MSK, reg03_val); 299decbc00eSZhengShunQian return 0; 300decbc00eSZhengShunQian } 301decbc00eSZhengShunQian 302decbc00eSZhengShunQian #define RK3036_CODEC_RATES (SNDRV_PCM_RATE_8000 | \ 303decbc00eSZhengShunQian SNDRV_PCM_RATE_16000 | \ 304decbc00eSZhengShunQian SNDRV_PCM_RATE_32000 | \ 305decbc00eSZhengShunQian SNDRV_PCM_RATE_44100 | \ 306decbc00eSZhengShunQian SNDRV_PCM_RATE_48000 | \ 307decbc00eSZhengShunQian SNDRV_PCM_RATE_96000) 308decbc00eSZhengShunQian 309decbc00eSZhengShunQian #define RK3036_CODEC_FMTS (SNDRV_PCM_FMTBIT_S16_LE | \ 310decbc00eSZhengShunQian SNDRV_PCM_FMTBIT_S20_3LE | \ 311decbc00eSZhengShunQian SNDRV_PCM_FMTBIT_S24_LE | \ 312decbc00eSZhengShunQian SNDRV_PCM_FMTBIT_S32_LE) 313decbc00eSZhengShunQian 314eb59d73cSArvind Yadav static const struct snd_soc_dai_ops rk3036_codec_dai_ops = { 315decbc00eSZhengShunQian .set_fmt = rk3036_codec_dai_set_fmt, 316decbc00eSZhengShunQian .hw_params = rk3036_codec_dai_hw_params, 317decbc00eSZhengShunQian }; 318decbc00eSZhengShunQian 319decbc00eSZhengShunQian static struct snd_soc_dai_driver rk3036_codec_dai_driver[] = { 320decbc00eSZhengShunQian { 321decbc00eSZhengShunQian .name = "rk3036-codec-dai", 322decbc00eSZhengShunQian .playback = { 323decbc00eSZhengShunQian .stream_name = "Playback", 324decbc00eSZhengShunQian .channels_min = 1, 325decbc00eSZhengShunQian .channels_max = 2, 326decbc00eSZhengShunQian .rates = RK3036_CODEC_RATES, 327decbc00eSZhengShunQian .formats = RK3036_CODEC_FMTS, 328decbc00eSZhengShunQian }, 329decbc00eSZhengShunQian .ops = &rk3036_codec_dai_ops, 330decbc00eSZhengShunQian .symmetric_rates = 1, 331decbc00eSZhengShunQian }, 332decbc00eSZhengShunQian }; 333decbc00eSZhengShunQian 33440aa60a2SKuninori Morimoto static void rk3036_codec_reset(struct snd_soc_component *component) 335decbc00eSZhengShunQian { 33640aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R00, 337decbc00eSZhengShunQian INNO_R00_CSR_RESET | INNO_R00_CDCR_RESET); 33840aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R00, 339decbc00eSZhengShunQian INNO_R00_CSR_WORK | INNO_R00_CDCR_WORK); 340decbc00eSZhengShunQian } 341decbc00eSZhengShunQian 34240aa60a2SKuninori Morimoto static int rk3036_codec_probe(struct snd_soc_component *component) 343decbc00eSZhengShunQian { 34440aa60a2SKuninori Morimoto rk3036_codec_reset(component); 345decbc00eSZhengShunQian return 0; 346decbc00eSZhengShunQian } 347decbc00eSZhengShunQian 34840aa60a2SKuninori Morimoto static void rk3036_codec_remove(struct snd_soc_component *component) 349decbc00eSZhengShunQian { 35040aa60a2SKuninori Morimoto rk3036_codec_reset(component); 351decbc00eSZhengShunQian } 352decbc00eSZhengShunQian 35340aa60a2SKuninori Morimoto static int rk3036_codec_set_bias_level(struct snd_soc_component *component, 354decbc00eSZhengShunQian enum snd_soc_bias_level level) 355decbc00eSZhengShunQian { 356decbc00eSZhengShunQian switch (level) { 357decbc00eSZhengShunQian case SND_SOC_BIAS_STANDBY: 358decbc00eSZhengShunQian /* set a big current for capacitor charging. */ 35940aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R10, INNO_R10_MAX_CUR); 360decbc00eSZhengShunQian /* start precharge */ 36140aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R06, INNO_R06_DAC_PRECHARGE); 362decbc00eSZhengShunQian 363decbc00eSZhengShunQian break; 364decbc00eSZhengShunQian 365decbc00eSZhengShunQian case SND_SOC_BIAS_OFF: 366decbc00eSZhengShunQian /* set a big current for capacitor discharging. */ 36740aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R10, INNO_R10_MAX_CUR); 368decbc00eSZhengShunQian /* start discharge. */ 36940aa60a2SKuninori Morimoto snd_soc_component_write(component, INNO_R06, INNO_R06_DAC_DISCHARGE); 370decbc00eSZhengShunQian 371decbc00eSZhengShunQian break; 372decbc00eSZhengShunQian default: 373decbc00eSZhengShunQian break; 374decbc00eSZhengShunQian } 375decbc00eSZhengShunQian 376decbc00eSZhengShunQian return 0; 377decbc00eSZhengShunQian } 378decbc00eSZhengShunQian 37940aa60a2SKuninori Morimoto static const struct snd_soc_component_driver rk3036_codec_driver = { 380decbc00eSZhengShunQian .probe = rk3036_codec_probe, 381decbc00eSZhengShunQian .remove = rk3036_codec_remove, 382decbc00eSZhengShunQian .set_bias_level = rk3036_codec_set_bias_level, 383decbc00eSZhengShunQian .controls = rk3036_codec_dapm_controls, 384decbc00eSZhengShunQian .num_controls = ARRAY_SIZE(rk3036_codec_dapm_controls), 385decbc00eSZhengShunQian .dapm_routes = rk3036_codec_dapm_routes, 386decbc00eSZhengShunQian .num_dapm_routes = ARRAY_SIZE(rk3036_codec_dapm_routes), 387decbc00eSZhengShunQian .dapm_widgets = rk3036_codec_dapm_widgets, 388decbc00eSZhengShunQian .num_dapm_widgets = ARRAY_SIZE(rk3036_codec_dapm_widgets), 38940aa60a2SKuninori Morimoto .idle_bias_on = 1, 39040aa60a2SKuninori Morimoto .use_pmdown_time = 1, 39140aa60a2SKuninori Morimoto .endianness = 1, 39240aa60a2SKuninori Morimoto .non_legacy_dai_naming = 1, 393decbc00eSZhengShunQian }; 394decbc00eSZhengShunQian 395decbc00eSZhengShunQian static const struct regmap_config rk3036_codec_regmap_config = { 396decbc00eSZhengShunQian .reg_bits = 32, 397decbc00eSZhengShunQian .reg_stride = 4, 398decbc00eSZhengShunQian .val_bits = 32, 399decbc00eSZhengShunQian }; 400decbc00eSZhengShunQian 401decbc00eSZhengShunQian #define GRF_SOC_CON0 0x00140 402decbc00eSZhengShunQian #define GRF_ACODEC_SEL (BIT(10) | BIT(16 + 10)) 403decbc00eSZhengShunQian 404decbc00eSZhengShunQian static int rk3036_codec_platform_probe(struct platform_device *pdev) 405decbc00eSZhengShunQian { 406decbc00eSZhengShunQian struct rk3036_codec_priv *priv; 407decbc00eSZhengShunQian struct device_node *of_node = pdev->dev.of_node; 408decbc00eSZhengShunQian struct resource *res; 409decbc00eSZhengShunQian void __iomem *base; 410decbc00eSZhengShunQian struct regmap *grf; 411decbc00eSZhengShunQian int ret; 412decbc00eSZhengShunQian 413decbc00eSZhengShunQian priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 414decbc00eSZhengShunQian if (!priv) 415decbc00eSZhengShunQian return -ENOMEM; 416decbc00eSZhengShunQian 417decbc00eSZhengShunQian res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 418decbc00eSZhengShunQian base = devm_ioremap_resource(&pdev->dev, res); 419decbc00eSZhengShunQian if (IS_ERR(base)) 420decbc00eSZhengShunQian return PTR_ERR(base); 421decbc00eSZhengShunQian 422decbc00eSZhengShunQian priv->base = base; 423decbc00eSZhengShunQian priv->regmap = devm_regmap_init_mmio(&pdev->dev, priv->base, 424decbc00eSZhengShunQian &rk3036_codec_regmap_config); 425decbc00eSZhengShunQian if (IS_ERR(priv->regmap)) { 426decbc00eSZhengShunQian dev_err(&pdev->dev, "init regmap failed\n"); 427decbc00eSZhengShunQian return PTR_ERR(priv->regmap); 428decbc00eSZhengShunQian } 429decbc00eSZhengShunQian 430decbc00eSZhengShunQian grf = syscon_regmap_lookup_by_phandle(of_node, "rockchip,grf"); 431decbc00eSZhengShunQian if (IS_ERR(grf)) { 432decbc00eSZhengShunQian dev_err(&pdev->dev, "needs 'rockchip,grf' property\n"); 433decbc00eSZhengShunQian return PTR_ERR(grf); 434decbc00eSZhengShunQian } 435decbc00eSZhengShunQian ret = regmap_write(grf, GRF_SOC_CON0, GRF_ACODEC_SEL); 436decbc00eSZhengShunQian if (ret) { 437decbc00eSZhengShunQian dev_err(&pdev->dev, "Could not write to GRF: %d\n", ret); 438decbc00eSZhengShunQian return ret; 439decbc00eSZhengShunQian } 440decbc00eSZhengShunQian 441decbc00eSZhengShunQian priv->pclk = devm_clk_get(&pdev->dev, "acodec_pclk"); 442decbc00eSZhengShunQian if (IS_ERR(priv->pclk)) 443decbc00eSZhengShunQian return PTR_ERR(priv->pclk); 444decbc00eSZhengShunQian 445decbc00eSZhengShunQian ret = clk_prepare_enable(priv->pclk); 446decbc00eSZhengShunQian if (ret < 0) { 447decbc00eSZhengShunQian dev_err(&pdev->dev, "failed to enable clk\n"); 448decbc00eSZhengShunQian return ret; 449decbc00eSZhengShunQian } 450decbc00eSZhengShunQian 451decbc00eSZhengShunQian priv->dev = &pdev->dev; 452decbc00eSZhengShunQian dev_set_drvdata(&pdev->dev, priv); 453decbc00eSZhengShunQian 45440aa60a2SKuninori Morimoto ret = devm_snd_soc_register_component(&pdev->dev, &rk3036_codec_driver, 455decbc00eSZhengShunQian rk3036_codec_dai_driver, 456decbc00eSZhengShunQian ARRAY_SIZE(rk3036_codec_dai_driver)); 457decbc00eSZhengShunQian if (ret) { 458decbc00eSZhengShunQian clk_disable_unprepare(priv->pclk); 459decbc00eSZhengShunQian dev_set_drvdata(&pdev->dev, NULL); 460decbc00eSZhengShunQian } 461decbc00eSZhengShunQian 462decbc00eSZhengShunQian return ret; 463decbc00eSZhengShunQian } 464decbc00eSZhengShunQian 465decbc00eSZhengShunQian static int rk3036_codec_platform_remove(struct platform_device *pdev) 466decbc00eSZhengShunQian { 467decbc00eSZhengShunQian struct rk3036_codec_priv *priv = dev_get_drvdata(&pdev->dev); 468decbc00eSZhengShunQian 469decbc00eSZhengShunQian clk_disable_unprepare(priv->pclk); 470decbc00eSZhengShunQian 471decbc00eSZhengShunQian return 0; 472decbc00eSZhengShunQian } 473decbc00eSZhengShunQian 474decbc00eSZhengShunQian static const struct of_device_id rk3036_codec_of_match[] = { 475decbc00eSZhengShunQian { .compatible = "rockchip,rk3036-codec", }, 476decbc00eSZhengShunQian {} 477decbc00eSZhengShunQian }; 478decbc00eSZhengShunQian MODULE_DEVICE_TABLE(of, rk3036_codec_of_match); 479decbc00eSZhengShunQian 480decbc00eSZhengShunQian static struct platform_driver rk3036_codec_platform_driver = { 481decbc00eSZhengShunQian .driver = { 482decbc00eSZhengShunQian .name = "rk3036-codec-platform", 483decbc00eSZhengShunQian .of_match_table = of_match_ptr(rk3036_codec_of_match), 484decbc00eSZhengShunQian }, 485decbc00eSZhengShunQian .probe = rk3036_codec_platform_probe, 486decbc00eSZhengShunQian .remove = rk3036_codec_platform_remove, 487decbc00eSZhengShunQian }; 488decbc00eSZhengShunQian 489decbc00eSZhengShunQian module_platform_driver(rk3036_codec_platform_driver); 490decbc00eSZhengShunQian 491decbc00eSZhengShunQian MODULE_AUTHOR("Rockchip Inc."); 492decbc00eSZhengShunQian MODULE_DESCRIPTION("Rockchip rk3036 codec driver"); 493decbc00eSZhengShunQian MODULE_LICENSE("GPL"); 494