1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f2644a2cSMark Brown /* 3f2644a2cSMark Brown * wm8960.c -- WM8960 ALSA SoC Audio driver 4f2644a2cSMark Brown * 5656baaebSMark Brown * Copyright 2007-11 Wolfson Microelectronics, plc 6656baaebSMark Brown * 7f2644a2cSMark Brown * Author: Liam Girdwood 8f2644a2cSMark Brown */ 9f2644a2cSMark Brown 10f2644a2cSMark Brown #include <linux/module.h> 11f2644a2cSMark Brown #include <linux/moduleparam.h> 12f2644a2cSMark Brown #include <linux/init.h> 13f2644a2cSMark Brown #include <linux/delay.h> 14f2644a2cSMark Brown #include <linux/pm.h> 1575aa8868SZidan Wang #include <linux/clk.h> 16f2644a2cSMark Brown #include <linux/i2c.h> 177e0bdbaeSNicola Lunghi #include <linux/acpi.h> 185a0e3ad6STejun Heo #include <linux/slab.h> 19f2644a2cSMark Brown #include <sound/core.h> 20f2644a2cSMark Brown #include <sound/pcm.h> 21f2644a2cSMark Brown #include <sound/pcm_params.h> 22f2644a2cSMark Brown #include <sound/soc.h> 23f2644a2cSMark Brown #include <sound/initval.h> 24f2644a2cSMark Brown #include <sound/tlv.h> 25b6877a47SMark Brown #include <sound/wm8960.h> 26f2644a2cSMark Brown 27f2644a2cSMark Brown #include "wm8960.h" 28f2644a2cSMark Brown 29f2644a2cSMark Brown /* R25 - Power 1 */ 30913d7b4cSMark Brown #define WM8960_VMID_MASK 0x180 31f2644a2cSMark Brown #define WM8960_VREF 0x40 32f2644a2cSMark Brown 33913d7b4cSMark Brown /* R26 - Power 2 */ 34913d7b4cSMark Brown #define WM8960_PWR2_LOUT1 0x40 35913d7b4cSMark Brown #define WM8960_PWR2_ROUT1 0x20 36913d7b4cSMark Brown #define WM8960_PWR2_OUT3 0x02 37913d7b4cSMark Brown 38f2644a2cSMark Brown /* R28 - Anti-pop 1 */ 39f2644a2cSMark Brown #define WM8960_POBCTRL 0x80 40f2644a2cSMark Brown #define WM8960_BUFDCOPEN 0x10 41f2644a2cSMark Brown #define WM8960_BUFIOEN 0x08 42f2644a2cSMark Brown #define WM8960_SOFT_ST 0x04 43f2644a2cSMark Brown #define WM8960_HPSTBY 0x01 44f2644a2cSMark Brown 45f2644a2cSMark Brown /* R29 - Anti-pop 2 */ 46f2644a2cSMark Brown #define WM8960_DISOP 0x40 47913d7b4cSMark Brown #define WM8960_DRES_MASK 0x30 48f2644a2cSMark Brown 493c7a4c24SViorel Suman #define WM8960_DSCH_TOUT 600 /* discharge timeout, ms */ 503c7a4c24SViorel Suman 513176bf2dSZidan Wang static bool is_pll_freq_available(unsigned int source, unsigned int target); 52e075fc17SKuninori Morimoto static int wm8960_set_pll(struct snd_soc_component *component, 533176bf2dSZidan Wang unsigned int freq_in, unsigned int freq_out); 54f2644a2cSMark Brown /* 55f2644a2cSMark Brown * wm8960 register cache 56f2644a2cSMark Brown * We can't read the WM8960 register space when we are 57f2644a2cSMark Brown * using 2 wire for device control, so we cache them instead. 58f2644a2cSMark Brown */ 590ebe36c6SMark Brown static const struct reg_default wm8960_reg_defaults[] = { 60b3df026eSMark Brown { 0x0, 0x00a7 }, 61b3df026eSMark Brown { 0x1, 0x00a7 }, 620ebe36c6SMark Brown { 0x2, 0x0000 }, 630ebe36c6SMark Brown { 0x3, 0x0000 }, 640ebe36c6SMark Brown { 0x4, 0x0000 }, 650ebe36c6SMark Brown { 0x5, 0x0008 }, 660ebe36c6SMark Brown { 0x6, 0x0000 }, 670ebe36c6SMark Brown { 0x7, 0x000a }, 680ebe36c6SMark Brown { 0x8, 0x01c0 }, 690ebe36c6SMark Brown { 0x9, 0x0000 }, 700ebe36c6SMark Brown { 0xa, 0x00ff }, 710ebe36c6SMark Brown { 0xb, 0x00ff }, 720ebe36c6SMark Brown 730ebe36c6SMark Brown { 0x10, 0x0000 }, 740ebe36c6SMark Brown { 0x11, 0x007b }, 750ebe36c6SMark Brown { 0x12, 0x0100 }, 760ebe36c6SMark Brown { 0x13, 0x0032 }, 770ebe36c6SMark Brown { 0x14, 0x0000 }, 780ebe36c6SMark Brown { 0x15, 0x00c3 }, 790ebe36c6SMark Brown { 0x16, 0x00c3 }, 800ebe36c6SMark Brown { 0x17, 0x01c0 }, 810ebe36c6SMark Brown { 0x18, 0x0000 }, 820ebe36c6SMark Brown { 0x19, 0x0000 }, 830ebe36c6SMark Brown { 0x1a, 0x0000 }, 840ebe36c6SMark Brown { 0x1b, 0x0000 }, 850ebe36c6SMark Brown { 0x1c, 0x0000 }, 860ebe36c6SMark Brown { 0x1d, 0x0000 }, 870ebe36c6SMark Brown 880ebe36c6SMark Brown { 0x20, 0x0100 }, 890ebe36c6SMark Brown { 0x21, 0x0100 }, 900ebe36c6SMark Brown { 0x22, 0x0050 }, 910ebe36c6SMark Brown 920ebe36c6SMark Brown { 0x25, 0x0050 }, 930ebe36c6SMark Brown { 0x26, 0x0000 }, 940ebe36c6SMark Brown { 0x27, 0x0000 }, 950ebe36c6SMark Brown { 0x28, 0x0000 }, 960ebe36c6SMark Brown { 0x29, 0x0000 }, 970ebe36c6SMark Brown { 0x2a, 0x0040 }, 980ebe36c6SMark Brown { 0x2b, 0x0000 }, 990ebe36c6SMark Brown { 0x2c, 0x0000 }, 1000ebe36c6SMark Brown { 0x2d, 0x0050 }, 1010ebe36c6SMark Brown { 0x2e, 0x0050 }, 1020ebe36c6SMark Brown { 0x2f, 0x0000 }, 1030ebe36c6SMark Brown { 0x30, 0x0002 }, 1040ebe36c6SMark Brown { 0x31, 0x0037 }, 1050ebe36c6SMark Brown 1060ebe36c6SMark Brown { 0x33, 0x0080 }, 1070ebe36c6SMark Brown { 0x34, 0x0008 }, 1080ebe36c6SMark Brown { 0x35, 0x0031 }, 1090ebe36c6SMark Brown { 0x36, 0x0026 }, 1100ebe36c6SMark Brown { 0x37, 0x00e9 }, 111f2644a2cSMark Brown }; 112f2644a2cSMark Brown 1130ebe36c6SMark Brown static bool wm8960_volatile(struct device *dev, unsigned int reg) 1140ebe36c6SMark Brown { 1150ebe36c6SMark Brown switch (reg) { 1160ebe36c6SMark Brown case WM8960_RESET: 1170ebe36c6SMark Brown return true; 1180ebe36c6SMark Brown default: 1190ebe36c6SMark Brown return false; 1200ebe36c6SMark Brown } 1210ebe36c6SMark Brown } 1220ebe36c6SMark Brown 123*422f10adSFabio Estevam #define WM8960_NUM_SUPPLIES 5 124*422f10adSFabio Estevam static const char *wm8960_supply_names[WM8960_NUM_SUPPLIES] = { 125*422f10adSFabio Estevam "DCVDD", 126*422f10adSFabio Estevam "DBVDD", 127*422f10adSFabio Estevam "AVDD", 128*422f10adSFabio Estevam "SPKVDD1", 129*422f10adSFabio Estevam "SPKVDD2", 130*422f10adSFabio Estevam }; 131*422f10adSFabio Estevam 132f2644a2cSMark Brown struct wm8960_priv { 13375aa8868SZidan Wang struct clk *mclk; 1340ebe36c6SMark Brown struct regmap *regmap; 135e075fc17SKuninori Morimoto int (*set_bias_level)(struct snd_soc_component *, 136f0fba2adSLiam Girdwood enum snd_soc_bias_level level); 137913d7b4cSMark Brown struct snd_soc_dapm_widget *lout1; 138913d7b4cSMark Brown struct snd_soc_dapm_widget *rout1; 139913d7b4cSMark Brown struct snd_soc_dapm_widget *out3; 140afd6d36aSMark Brown bool deemph; 1413176bf2dSZidan Wang int lrclk; 1420e50b51aSZidan Wang int bclk; 1430e50b51aSZidan Wang int sysclk; 1443176bf2dSZidan Wang int clk_id; 1453176bf2dSZidan Wang int freq_in; 1463176bf2dSZidan Wang bool is_stream_in_use[2]; 147e2280c90SZidan Wang struct wm8960_data pdata; 1483c7a4c24SViorel Suman ktime_t dsch_start; 149*422f10adSFabio Estevam struct regulator_bulk_data supplies[WM8960_NUM_SUPPLIES]; 150f2644a2cSMark Brown }; 151f2644a2cSMark Brown 1523ad5e861SZidan Wang #define wm8960_reset(c) regmap_write(c, WM8960_RESET, 0) 153f2644a2cSMark Brown 154f2644a2cSMark Brown /* enumerated controls */ 155f2644a2cSMark Brown static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted", 156f2644a2cSMark Brown "Right Inverted", "Stereo Inversion"}; 157f2644a2cSMark Brown static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"}; 158f2644a2cSMark Brown static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"}; 159f2644a2cSMark Brown static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"}; 160f2644a2cSMark Brown static const char *wm8960_alcmode[] = {"ALC", "Limiter"}; 1614a5893cfSZidan Wang static const char *wm8960_adc_data_output_sel[] = { 1624a5893cfSZidan Wang "Left Data = Left ADC; Right Data = Right ADC", 1634a5893cfSZidan Wang "Left Data = Left ADC; Right Data = Left ADC", 1644a5893cfSZidan Wang "Left Data = Right ADC; Right Data = Right ADC", 1654a5893cfSZidan Wang "Left Data = Right ADC; Right Data = Left ADC", 1664a5893cfSZidan Wang }; 167defbf708SZidan Wang static const char *wm8960_dmonomix[] = {"Stereo", "Mono"}; 1688cf2e3b1SShengjiu Wang static const char *wm8960_dacslope[] = {"Normal", "Sloping"}; 169f2644a2cSMark Brown 170f2644a2cSMark Brown static const struct soc_enum wm8960_enum[] = { 171f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity), 172f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity), 173f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff), 174f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff), 175f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc), 176f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode), 1774a5893cfSZidan Wang SOC_ENUM_SINGLE(WM8960_ADDCTL1, 2, 4, wm8960_adc_data_output_sel), 178defbf708SZidan Wang SOC_ENUM_SINGLE(WM8960_ADDCTL1, 4, 2, wm8960_dmonomix), 1798cf2e3b1SShengjiu Wang SOC_ENUM_SINGLE(WM8960_DACCTL2, 1, 2, wm8960_dacslope), 180f2644a2cSMark Brown }; 181f2644a2cSMark Brown 182afd6d36aSMark Brown static const int deemph_settings[] = { 0, 32000, 44100, 48000 }; 183afd6d36aSMark Brown 184e075fc17SKuninori Morimoto static int wm8960_set_deemph(struct snd_soc_component *component) 185afd6d36aSMark Brown { 186e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 187afd6d36aSMark Brown int val, i, best; 188afd6d36aSMark Brown 189afd6d36aSMark Brown /* If we're using deemphasis select the nearest available sample 190afd6d36aSMark Brown * rate. 191afd6d36aSMark Brown */ 192afd6d36aSMark Brown if (wm8960->deemph) { 193afd6d36aSMark Brown best = 1; 194afd6d36aSMark Brown for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) { 1953176bf2dSZidan Wang if (abs(deemph_settings[i] - wm8960->lrclk) < 1963176bf2dSZidan Wang abs(deemph_settings[best] - wm8960->lrclk)) 197afd6d36aSMark Brown best = i; 198afd6d36aSMark Brown } 199afd6d36aSMark Brown 200afd6d36aSMark Brown val = best << 1; 201afd6d36aSMark Brown } else { 202afd6d36aSMark Brown val = 0; 203afd6d36aSMark Brown } 204afd6d36aSMark Brown 205e075fc17SKuninori Morimoto dev_dbg(component->dev, "Set deemphasis %d\n", val); 206afd6d36aSMark Brown 207e075fc17SKuninori Morimoto return snd_soc_component_update_bits(component, WM8960_DACCTL1, 208afd6d36aSMark Brown 0x6, val); 209afd6d36aSMark Brown } 210afd6d36aSMark Brown 211afd6d36aSMark Brown static int wm8960_get_deemph(struct snd_kcontrol *kcontrol, 212afd6d36aSMark Brown struct snd_ctl_elem_value *ucontrol) 213afd6d36aSMark Brown { 214e075fc17SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 215e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 216afd6d36aSMark Brown 217b4a18c8bSTakashi Iwai ucontrol->value.integer.value[0] = wm8960->deemph; 2183f343f85SDmitry Artamonow return 0; 219afd6d36aSMark Brown } 220afd6d36aSMark Brown 221afd6d36aSMark Brown static int wm8960_put_deemph(struct snd_kcontrol *kcontrol, 222afd6d36aSMark Brown struct snd_ctl_elem_value *ucontrol) 223afd6d36aSMark Brown { 224e075fc17SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 225e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 226c1fe81f2SDan Carpenter unsigned int deemph = ucontrol->value.integer.value[0]; 227afd6d36aSMark Brown 228afd6d36aSMark Brown if (deemph > 1) 229afd6d36aSMark Brown return -EINVAL; 230afd6d36aSMark Brown 231afd6d36aSMark Brown wm8960->deemph = deemph; 232afd6d36aSMark Brown 233e075fc17SKuninori Morimoto return wm8960_set_deemph(component); 234afd6d36aSMark Brown } 235afd6d36aSMark Brown 2363758ff5fSZidan Wang static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1); 2377e90f9b2SZidan Wang static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1725, 75, 0); 2383758ff5fSZidan Wang static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); 239f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0); 240f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 2417e90f9b2SZidan Wang static const DECLARE_TLV_DB_SCALE(lineinboost_tlv, -1500, 300, 1); 242b269cebfSTakashi Sakamoto static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(micboost_tlv, 2437e90f9b2SZidan Wang 0, 1, TLV_DB_SCALE_ITEM(0, 1300, 0), 2447e90f9b2SZidan Wang 2, 3, TLV_DB_SCALE_ITEM(2000, 900, 0), 245b269cebfSTakashi Sakamoto ); 246f2644a2cSMark Brown 247f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_snd_controls[] = { 248f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL, 2497e90f9b2SZidan Wang 0, 63, 0, inpga_tlv), 250f2644a2cSMark Brown SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL, 251f2644a2cSMark Brown 6, 1, 0), 252f2644a2cSMark Brown SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL, 25341a59caeSJongHo Kim 7, 1, 1), 254f2644a2cSMark Brown 25521eb2693SMark Brown SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT3 Volume", 25695826a37SStuart Henderson WM8960_INBMIX1, 4, 7, 0, lineinboost_tlv), 25721eb2693SMark Brown SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT2 Volume", 25895826a37SStuart Henderson WM8960_INBMIX1, 1, 7, 0, lineinboost_tlv), 25995826a37SStuart Henderson SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT3 Volume", 26095826a37SStuart Henderson WM8960_INBMIX2, 4, 7, 0, lineinboost_tlv), 26195826a37SStuart Henderson SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT2 Volume", 2627e90f9b2SZidan Wang WM8960_INBMIX2, 1, 7, 0, lineinboost_tlv), 2637e90f9b2SZidan Wang SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT1 Volume", 2648524bb0cSZidan Wang WM8960_RINPATH, 4, 3, 0, micboost_tlv), 2657e90f9b2SZidan Wang SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT1 Volume", 2668524bb0cSZidan Wang WM8960_LINPATH, 4, 3, 0, micboost_tlv), 26721eb2693SMark Brown 268f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC, 269f2644a2cSMark Brown 0, 255, 0, dac_tlv), 270f2644a2cSMark Brown 271f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1, 272f2644a2cSMark Brown 0, 127, 0, out_tlv), 273f2644a2cSMark Brown SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1, 274f2644a2cSMark Brown 7, 1, 0), 275f2644a2cSMark Brown 276f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2, 277f2644a2cSMark Brown 0, 127, 0, out_tlv), 278f2644a2cSMark Brown SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2, 279f2644a2cSMark Brown 7, 1, 0), 280f2644a2cSMark Brown SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0), 281f2644a2cSMark Brown SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0), 282f2644a2cSMark Brown 283f2644a2cSMark Brown SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0), 2844faaa8d9SMark Brown SOC_ENUM("ADC Polarity", wm8960_enum[0]), 285f2644a2cSMark Brown SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0), 286f2644a2cSMark Brown 287a077e81eSZidan Wang SOC_ENUM("DAC Polarity", wm8960_enum[1]), 288afd6d36aSMark Brown SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0, 289afd6d36aSMark Brown wm8960_get_deemph, wm8960_put_deemph), 290f2644a2cSMark Brown 2914faaa8d9SMark Brown SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]), 2924faaa8d9SMark Brown SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]), 293f2644a2cSMark Brown SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0), 294f2644a2cSMark Brown SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0), 295f2644a2cSMark Brown 2964faaa8d9SMark Brown SOC_ENUM("ALC Function", wm8960_enum[4]), 297f2644a2cSMark Brown SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0), 298f2644a2cSMark Brown SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1), 299f2644a2cSMark Brown SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0), 300f2644a2cSMark Brown SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0), 3014faaa8d9SMark Brown SOC_ENUM("ALC Mode", wm8960_enum[5]), 302f2644a2cSMark Brown SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0), 303f2644a2cSMark Brown SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0), 304f2644a2cSMark Brown 305f2644a2cSMark Brown SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0), 306f2644a2cSMark Brown SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0), 307f2644a2cSMark Brown 308c324aac0SMa Haijun SOC_DOUBLE_R_TLV("ADC PCM Capture Volume", WM8960_LADC, WM8960_RADC, 309c324aac0SMa Haijun 0, 255, 0, adc_tlv), 310f2644a2cSMark Brown 311f2644a2cSMark Brown SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume", 312f2644a2cSMark Brown WM8960_BYPASS1, 4, 7, 1, bypass_tlv), 313f2644a2cSMark Brown SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume", 314f2644a2cSMark Brown WM8960_LOUTMIX, 4, 7, 1, bypass_tlv), 315f2644a2cSMark Brown SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume", 316f2644a2cSMark Brown WM8960_BYPASS2, 4, 7, 1, bypass_tlv), 317f2644a2cSMark Brown SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume", 318f2644a2cSMark Brown WM8960_ROUTMIX, 4, 7, 1, bypass_tlv), 3194a5893cfSZidan Wang 3204a5893cfSZidan Wang SOC_ENUM("ADC Data Output Select", wm8960_enum[6]), 321defbf708SZidan Wang SOC_ENUM("DAC Mono Mix", wm8960_enum[7]), 3228cf2e3b1SShengjiu Wang SOC_ENUM("DAC Filter Characteristics", wm8960_enum[8]), 323f2644a2cSMark Brown }; 324f2644a2cSMark Brown 325f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_lin_boost[] = { 326f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0), 327f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0), 328f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0), 329f2644a2cSMark Brown }; 330f2644a2cSMark Brown 331f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_lin[] = { 332f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0), 333f2644a2cSMark Brown }; 334f2644a2cSMark Brown 335f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_rin_boost[] = { 336f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0), 337f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0), 338f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0), 339f2644a2cSMark Brown }; 340f2644a2cSMark Brown 341f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_rin[] = { 342f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0), 343f2644a2cSMark Brown }; 344f2644a2cSMark Brown 345f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_loutput_mixer[] = { 346f2644a2cSMark Brown SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0), 347f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0), 348f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0), 349f2644a2cSMark Brown }; 350f2644a2cSMark Brown 351f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_routput_mixer[] = { 352f2644a2cSMark Brown SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0), 353f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0), 354f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0), 355f2644a2cSMark Brown }; 356f2644a2cSMark Brown 357f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_mono_out[] = { 358f2644a2cSMark Brown SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0), 359f2644a2cSMark Brown SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0), 360f2644a2cSMark Brown }; 361f2644a2cSMark Brown 362f2644a2cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = { 363f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT1"), 364f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT1"), 365f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT2"), 366f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT2"), 367f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT3"), 368f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT3"), 369f2644a2cSMark Brown 370187774cbSMark Brown SND_SOC_DAPM_SUPPLY("MICB", WM8960_POWER1, 1, 0, NULL, 0), 371f2644a2cSMark Brown 372f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0, 373f2644a2cSMark Brown wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)), 374f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0, 375f2644a2cSMark Brown wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)), 376f2644a2cSMark Brown 377f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0, 378f2644a2cSMark Brown wm8960_lin, ARRAY_SIZE(wm8960_lin)), 379f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0, 380f2644a2cSMark Brown wm8960_rin, ARRAY_SIZE(wm8960_rin)), 381f2644a2cSMark Brown 38244426de4SMark Brown SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER1, 3, 0), 38344426de4SMark Brown SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER1, 2, 0), 384f2644a2cSMark Brown 385f2644a2cSMark Brown SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0), 386f2644a2cSMark Brown SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0), 387f2644a2cSMark Brown 388f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0, 389f2644a2cSMark Brown &wm8960_loutput_mixer[0], 390f2644a2cSMark Brown ARRAY_SIZE(wm8960_loutput_mixer)), 391f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0, 392f2644a2cSMark Brown &wm8960_routput_mixer[0], 393f2644a2cSMark Brown ARRAY_SIZE(wm8960_routput_mixer)), 394f2644a2cSMark Brown 395f2644a2cSMark Brown SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0), 396f2644a2cSMark Brown SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0), 397f2644a2cSMark Brown 398f2644a2cSMark Brown SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0), 399f2644a2cSMark Brown SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0), 400f2644a2cSMark Brown 401f2644a2cSMark Brown SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0), 402f2644a2cSMark Brown SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0), 403f2644a2cSMark Brown 404f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_LP"), 405f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_LN"), 406f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("HP_L"), 407f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("HP_R"), 408f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_RP"), 409f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_RN"), 410f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("OUT3"), 411f2644a2cSMark Brown }; 412f2644a2cSMark Brown 413913d7b4cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = { 414913d7b4cSMark Brown SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0, 415913d7b4cSMark Brown &wm8960_mono_out[0], 416913d7b4cSMark Brown ARRAY_SIZE(wm8960_mono_out)), 417913d7b4cSMark Brown }; 418913d7b4cSMark Brown 419913d7b4cSMark Brown /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */ 420913d7b4cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = { 421913d7b4cSMark Brown SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0), 422913d7b4cSMark Brown }; 423913d7b4cSMark Brown 424f2644a2cSMark Brown static const struct snd_soc_dapm_route audio_paths[] = { 425f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" }, 426f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" }, 427f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" }, 428f2644a2cSMark Brown 4292d4a3260SZidan Wang { "Left Input Mixer", "Boost Switch", "Left Boost Mixer" }, 4302d4a3260SZidan Wang { "Left Input Mixer", "Boost Switch", "LINPUT1" }, /* Really Boost Switch */ 431f2644a2cSMark Brown { "Left Input Mixer", NULL, "LINPUT2" }, 432f2644a2cSMark Brown { "Left Input Mixer", NULL, "LINPUT3" }, 433f2644a2cSMark Brown 434f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" }, 435f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" }, 436f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" }, 437f2644a2cSMark Brown 4382d4a3260SZidan Wang { "Right Input Mixer", "Boost Switch", "Right Boost Mixer" }, 4392d4a3260SZidan Wang { "Right Input Mixer", "Boost Switch", "RINPUT1" }, /* Really Boost Switch */ 440f2644a2cSMark Brown { "Right Input Mixer", NULL, "RINPUT2" }, 44185e36a1fSZidan Wang { "Right Input Mixer", NULL, "RINPUT3" }, 442f2644a2cSMark Brown 443f2644a2cSMark Brown { "Left ADC", NULL, "Left Input Mixer" }, 444f2644a2cSMark Brown { "Right ADC", NULL, "Right Input Mixer" }, 445f2644a2cSMark Brown 446f2644a2cSMark Brown { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" }, 447f2644a2cSMark Brown { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer" }, 448f2644a2cSMark Brown { "Left Output Mixer", "PCM Playback Switch", "Left DAC" }, 449f2644a2cSMark Brown 450f2644a2cSMark Brown { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" }, 451f2644a2cSMark Brown { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" }, 452f2644a2cSMark Brown { "Right Output Mixer", "PCM Playback Switch", "Right DAC" }, 453f2644a2cSMark Brown 454f2644a2cSMark Brown { "LOUT1 PGA", NULL, "Left Output Mixer" }, 455f2644a2cSMark Brown { "ROUT1 PGA", NULL, "Right Output Mixer" }, 456f2644a2cSMark Brown 457f2644a2cSMark Brown { "HP_L", NULL, "LOUT1 PGA" }, 458f2644a2cSMark Brown { "HP_R", NULL, "ROUT1 PGA" }, 459f2644a2cSMark Brown 460f2644a2cSMark Brown { "Left Speaker PGA", NULL, "Left Output Mixer" }, 461f2644a2cSMark Brown { "Right Speaker PGA", NULL, "Right Output Mixer" }, 462f2644a2cSMark Brown 463f2644a2cSMark Brown { "Left Speaker Output", NULL, "Left Speaker PGA" }, 464f2644a2cSMark Brown { "Right Speaker Output", NULL, "Right Speaker PGA" }, 465f2644a2cSMark Brown 466f2644a2cSMark Brown { "SPK_LN", NULL, "Left Speaker Output" }, 467f2644a2cSMark Brown { "SPK_LP", NULL, "Left Speaker Output" }, 468f2644a2cSMark Brown { "SPK_RN", NULL, "Right Speaker Output" }, 469f2644a2cSMark Brown { "SPK_RP", NULL, "Right Speaker Output" }, 470913d7b4cSMark Brown }; 471913d7b4cSMark Brown 472913d7b4cSMark Brown static const struct snd_soc_dapm_route audio_paths_out3[] = { 473913d7b4cSMark Brown { "Mono Output Mixer", "Left Switch", "Left Output Mixer" }, 474913d7b4cSMark Brown { "Mono Output Mixer", "Right Switch", "Right Output Mixer" }, 475f2644a2cSMark Brown 476f2644a2cSMark Brown { "OUT3", NULL, "Mono Output Mixer", } 477f2644a2cSMark Brown }; 478f2644a2cSMark Brown 479913d7b4cSMark Brown static const struct snd_soc_dapm_route audio_paths_capless[] = { 480913d7b4cSMark Brown { "HP_L", NULL, "OUT3 VMID" }, 481913d7b4cSMark Brown { "HP_R", NULL, "OUT3 VMID" }, 482913d7b4cSMark Brown 483913d7b4cSMark Brown { "OUT3 VMID", NULL, "Left Output Mixer" }, 484913d7b4cSMark Brown { "OUT3 VMID", NULL, "Right Output Mixer" }, 485913d7b4cSMark Brown }; 486913d7b4cSMark Brown 487e075fc17SKuninori Morimoto static int wm8960_add_widgets(struct snd_soc_component *component) 488f2644a2cSMark Brown { 489e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 490e2280c90SZidan Wang struct wm8960_data *pdata = &wm8960->pdata; 491e075fc17SKuninori Morimoto struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 492913d7b4cSMark Brown struct snd_soc_dapm_widget *w; 493913d7b4cSMark Brown 494ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets, 495f2644a2cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets)); 496f2644a2cSMark Brown 497ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths)); 498f2644a2cSMark Brown 499913d7b4cSMark Brown /* In capless mode OUT3 is used to provide VMID for the 500913d7b4cSMark Brown * headphone outputs, otherwise it is used as a mono mixer. 501913d7b4cSMark Brown */ 502913d7b4cSMark Brown if (pdata && pdata->capless) { 503ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_capless, 504913d7b4cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets_capless)); 505913d7b4cSMark Brown 506ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths_capless, 507913d7b4cSMark Brown ARRAY_SIZE(audio_paths_capless)); 508913d7b4cSMark Brown } else { 509ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_out3, 510913d7b4cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets_out3)); 511913d7b4cSMark Brown 512ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths_out3, 513913d7b4cSMark Brown ARRAY_SIZE(audio_paths_out3)); 514913d7b4cSMark Brown } 515913d7b4cSMark Brown 516913d7b4cSMark Brown /* We need to power up the headphone output stage out of 517913d7b4cSMark Brown * sequence for capless mode. To save scanning the widget 518913d7b4cSMark Brown * list each time to find the desired power state do so now 519913d7b4cSMark Brown * and save the result. 520913d7b4cSMark Brown */ 521e075fc17SKuninori Morimoto list_for_each_entry(w, &component->card->widgets, list) { 52293f32f53SLars-Peter Clausen if (w->dapm != dapm) 52397c866deSJarkko Nikula continue; 524913d7b4cSMark Brown if (strcmp(w->name, "LOUT1 PGA") == 0) 525913d7b4cSMark Brown wm8960->lout1 = w; 526913d7b4cSMark Brown if (strcmp(w->name, "ROUT1 PGA") == 0) 527913d7b4cSMark Brown wm8960->rout1 = w; 528913d7b4cSMark Brown if (strcmp(w->name, "OUT3 VMID") == 0) 529913d7b4cSMark Brown wm8960->out3 = w; 530913d7b4cSMark Brown } 531913d7b4cSMark Brown 532f2644a2cSMark Brown return 0; 533f2644a2cSMark Brown } 534f2644a2cSMark Brown 535f2644a2cSMark Brown static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai, 536f2644a2cSMark Brown unsigned int fmt) 537f2644a2cSMark Brown { 538e075fc17SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 539f2644a2cSMark Brown u16 iface = 0; 540f2644a2cSMark Brown 541f2644a2cSMark Brown /* set master/slave audio interface */ 542f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 543f2644a2cSMark Brown case SND_SOC_DAIFMT_CBM_CFM: 544f2644a2cSMark Brown iface |= 0x0040; 545f2644a2cSMark Brown break; 546f2644a2cSMark Brown case SND_SOC_DAIFMT_CBS_CFS: 547f2644a2cSMark Brown break; 548f2644a2cSMark Brown default: 549f2644a2cSMark Brown return -EINVAL; 550f2644a2cSMark Brown } 551f2644a2cSMark Brown 552f2644a2cSMark Brown /* interface format */ 553f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 554f2644a2cSMark Brown case SND_SOC_DAIFMT_I2S: 555f2644a2cSMark Brown iface |= 0x0002; 556f2644a2cSMark Brown break; 557f2644a2cSMark Brown case SND_SOC_DAIFMT_RIGHT_J: 558f2644a2cSMark Brown break; 559f2644a2cSMark Brown case SND_SOC_DAIFMT_LEFT_J: 560f2644a2cSMark Brown iface |= 0x0001; 561f2644a2cSMark Brown break; 562f2644a2cSMark Brown case SND_SOC_DAIFMT_DSP_A: 563f2644a2cSMark Brown iface |= 0x0003; 564f2644a2cSMark Brown break; 565f2644a2cSMark Brown case SND_SOC_DAIFMT_DSP_B: 566f2644a2cSMark Brown iface |= 0x0013; 567f2644a2cSMark Brown break; 568f2644a2cSMark Brown default: 569f2644a2cSMark Brown return -EINVAL; 570f2644a2cSMark Brown } 571f2644a2cSMark Brown 572f2644a2cSMark Brown /* clock inversion */ 573f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 574f2644a2cSMark Brown case SND_SOC_DAIFMT_NB_NF: 575f2644a2cSMark Brown break; 576f2644a2cSMark Brown case SND_SOC_DAIFMT_IB_IF: 577f2644a2cSMark Brown iface |= 0x0090; 578f2644a2cSMark Brown break; 579f2644a2cSMark Brown case SND_SOC_DAIFMT_IB_NF: 580f2644a2cSMark Brown iface |= 0x0080; 581f2644a2cSMark Brown break; 582f2644a2cSMark Brown case SND_SOC_DAIFMT_NB_IF: 583f2644a2cSMark Brown iface |= 0x0010; 584f2644a2cSMark Brown break; 585f2644a2cSMark Brown default: 586f2644a2cSMark Brown return -EINVAL; 587f2644a2cSMark Brown } 588f2644a2cSMark Brown 589f2644a2cSMark Brown /* set iface */ 590e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_IFACE1, iface); 591f2644a2cSMark Brown return 0; 592f2644a2cSMark Brown } 593f2644a2cSMark Brown 594db059c0fSMark Brown static struct { 595db059c0fSMark Brown int rate; 596db059c0fSMark Brown unsigned int val; 597db059c0fSMark Brown } alc_rates[] = { 598db059c0fSMark Brown { 48000, 0 }, 599db059c0fSMark Brown { 44100, 0 }, 600db059c0fSMark Brown { 32000, 1 }, 601db059c0fSMark Brown { 22050, 2 }, 602db059c0fSMark Brown { 24000, 2 }, 603db059c0fSMark Brown { 16000, 3 }, 60422ee76daSZidan Wang { 11025, 4 }, 605db059c0fSMark Brown { 12000, 4 }, 606db059c0fSMark Brown { 8000, 5 }, 607db059c0fSMark Brown }; 608db059c0fSMark Brown 6093176bf2dSZidan Wang /* -1 for reserved value */ 6103176bf2dSZidan Wang static const int sysclk_divs[] = { 1, -1, 2, -1 }; 6113176bf2dSZidan Wang 6120e50b51aSZidan Wang /* Multiply 256 for internal 256 div */ 6130e50b51aSZidan Wang static const int dac_divs[] = { 256, 384, 512, 768, 1024, 1408, 1536 }; 6140e50b51aSZidan Wang 6150e50b51aSZidan Wang /* Multiply 10 to eliminate decimials */ 6160e50b51aSZidan Wang static const int bclk_divs[] = { 6170e50b51aSZidan Wang 10, 15, 20, 30, 40, 55, 60, 80, 110, 6180e50b51aSZidan Wang 120, 160, 220, 240, 320, 320, 320 6190e50b51aSZidan Wang }; 6200e50b51aSZidan Wang 6213ddc9721SDaniel Baluta /** 6223ddc9721SDaniel Baluta * wm8960_configure_sysclk - checks if there is a sysclk frequency available 6233ddc9721SDaniel Baluta * The sysclk must be chosen such that: 6243ddc9721SDaniel Baluta * - sysclk = MCLK / sysclk_divs 6253ddc9721SDaniel Baluta * - lrclk = sysclk / dac_divs 6263ddc9721SDaniel Baluta * - 10 * bclk = sysclk / bclk_divs 6273ddc9721SDaniel Baluta * 628419eac3cSPierre-Louis Bossart * @wm8960: codec private data 6293ddc9721SDaniel Baluta * @mclk: MCLK used to derive sysclk 6303ddc9721SDaniel Baluta * @sysclk_idx: sysclk_divs index for found sysclk 6313ddc9721SDaniel Baluta * @dac_idx: dac_divs index for found lrclk 6323ddc9721SDaniel Baluta * @bclk_idx: bclk_divs index for found bclk 6333ddc9721SDaniel Baluta * 6343ddc9721SDaniel Baluta * Returns: 6353ddc9721SDaniel Baluta * -1, in case no sysclk frequency available found 6363c01b9eeSDaniel Baluta * >=0, in case we could derive bclk and lrclk from sysclk using 6373c01b9eeSDaniel Baluta * (@sysclk_idx, @dac_idx, @bclk_idx) dividers 6383ddc9721SDaniel Baluta */ 6393ddc9721SDaniel Baluta static 6403ddc9721SDaniel Baluta int wm8960_configure_sysclk(struct wm8960_priv *wm8960, int mclk, 6413ddc9721SDaniel Baluta int *sysclk_idx, int *dac_idx, int *bclk_idx) 6423ddc9721SDaniel Baluta { 6433ddc9721SDaniel Baluta int sysclk, bclk, lrclk; 6443ddc9721SDaniel Baluta int i, j, k; 64599067c07SShengjiu Wang int diff; 6463c01b9eeSDaniel Baluta 6473c01b9eeSDaniel Baluta /* marker for no match */ 6483c01b9eeSDaniel Baluta *bclk_idx = -1; 6493ddc9721SDaniel Baluta 6503ddc9721SDaniel Baluta bclk = wm8960->bclk; 6513ddc9721SDaniel Baluta lrclk = wm8960->lrclk; 6523ddc9721SDaniel Baluta 6533ddc9721SDaniel Baluta /* check if the sysclk frequency is available. */ 6543ddc9721SDaniel Baluta for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { 6553ddc9721SDaniel Baluta if (sysclk_divs[i] == -1) 6563ddc9721SDaniel Baluta continue; 6573ddc9721SDaniel Baluta sysclk = mclk / sysclk_divs[i]; 6583ddc9721SDaniel Baluta for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { 6593ddc9721SDaniel Baluta if (sysclk != dac_divs[j] * lrclk) 6603ddc9721SDaniel Baluta continue; 6613ddc9721SDaniel Baluta for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { 6623ddc9721SDaniel Baluta diff = sysclk - bclk * bclk_divs[k] / 10; 6633ddc9721SDaniel Baluta if (diff == 0) { 6643ddc9721SDaniel Baluta *sysclk_idx = i; 6653ddc9721SDaniel Baluta *dac_idx = j; 6663ddc9721SDaniel Baluta *bclk_idx = k; 6673ddc9721SDaniel Baluta break; 6683ddc9721SDaniel Baluta } 6693ddc9721SDaniel Baluta } 6703ddc9721SDaniel Baluta if (k != ARRAY_SIZE(bclk_divs)) 6713ddc9721SDaniel Baluta break; 6723ddc9721SDaniel Baluta } 6733ddc9721SDaniel Baluta if (j != ARRAY_SIZE(dac_divs)) 6743ddc9721SDaniel Baluta break; 6753ddc9721SDaniel Baluta } 6763c01b9eeSDaniel Baluta return *bclk_idx; 6773ddc9721SDaniel Baluta } 6783ddc9721SDaniel Baluta 67984fdc00dSDaniel Baluta /** 68084fdc00dSDaniel Baluta * wm8960_configure_pll - checks if there is a PLL out frequency available 68184fdc00dSDaniel Baluta * The PLL out frequency must be chosen such that: 68284fdc00dSDaniel Baluta * - sysclk = lrclk * dac_divs 68384fdc00dSDaniel Baluta * - freq_out = sysclk * sysclk_divs 68484fdc00dSDaniel Baluta * - 10 * sysclk = bclk * bclk_divs 68584fdc00dSDaniel Baluta * 68682bab889SDaniel Baluta * If we cannot find an exact match for (sysclk, lrclk, bclk) 68782bab889SDaniel Baluta * triplet, we relax the bclk such that bclk is chosen as the 68882bab889SDaniel Baluta * closest available frequency greater than expected bclk. 68982bab889SDaniel Baluta * 690e075fc17SKuninori Morimoto * @component: component structure 69184fdc00dSDaniel Baluta * @freq_in: input frequency used to derive freq out via PLL 69284fdc00dSDaniel Baluta * @sysclk_idx: sysclk_divs index for found sysclk 69384fdc00dSDaniel Baluta * @dac_idx: dac_divs index for found lrclk 69484fdc00dSDaniel Baluta * @bclk_idx: bclk_divs index for found bclk 69584fdc00dSDaniel Baluta * 69684fdc00dSDaniel Baluta * Returns: 69766772edaSDaniel Baluta * < 0, in case no PLL frequency out available was found 69884fdc00dSDaniel Baluta * >=0, in case we could derive bclk, lrclk, sysclk from PLL out using 69984fdc00dSDaniel Baluta * (@sysclk_idx, @dac_idx, @bclk_idx) dividers 70084fdc00dSDaniel Baluta */ 70184fdc00dSDaniel Baluta static 702e075fc17SKuninori Morimoto int wm8960_configure_pll(struct snd_soc_component *component, int freq_in, 70384fdc00dSDaniel Baluta int *sysclk_idx, int *dac_idx, int *bclk_idx) 70484fdc00dSDaniel Baluta { 705e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 70684fdc00dSDaniel Baluta int sysclk, bclk, lrclk, freq_out; 70782bab889SDaniel Baluta int diff, closest, best_freq_out; 70884fdc00dSDaniel Baluta int i, j, k; 70984fdc00dSDaniel Baluta 71084fdc00dSDaniel Baluta bclk = wm8960->bclk; 71184fdc00dSDaniel Baluta lrclk = wm8960->lrclk; 71282bab889SDaniel Baluta closest = freq_in; 71384fdc00dSDaniel Baluta 71482bab889SDaniel Baluta best_freq_out = -EINVAL; 71566772edaSDaniel Baluta *sysclk_idx = *dac_idx = *bclk_idx = -1; 71684fdc00dSDaniel Baluta 71716b82e75SShengjiu Wang /* 71816b82e75SShengjiu Wang * From Datasheet, the PLL performs best when f2 is between 71916b82e75SShengjiu Wang * 90MHz and 100MHz, the desired sysclk output is 11.2896MHz 72016b82e75SShengjiu Wang * or 12.288MHz, then sysclkdiv = 2 is the best choice. 72116b82e75SShengjiu Wang * So search sysclk_divs from 2 to 1 other than from 1 to 2. 72216b82e75SShengjiu Wang */ 72316b82e75SShengjiu Wang for (i = ARRAY_SIZE(sysclk_divs) - 1; i >= 0; --i) { 72484fdc00dSDaniel Baluta if (sysclk_divs[i] == -1) 72584fdc00dSDaniel Baluta continue; 72684fdc00dSDaniel Baluta for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { 72784fdc00dSDaniel Baluta sysclk = lrclk * dac_divs[j]; 72884fdc00dSDaniel Baluta freq_out = sysclk * sysclk_divs[i]; 72984fdc00dSDaniel Baluta 73084fdc00dSDaniel Baluta for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { 73184fdc00dSDaniel Baluta if (!is_pll_freq_available(freq_in, freq_out)) 73284fdc00dSDaniel Baluta continue; 73384fdc00dSDaniel Baluta 73484fdc00dSDaniel Baluta diff = sysclk - bclk * bclk_divs[k] / 10; 73584fdc00dSDaniel Baluta if (diff == 0) { 73684fdc00dSDaniel Baluta *sysclk_idx = i; 73784fdc00dSDaniel Baluta *dac_idx = j; 73884fdc00dSDaniel Baluta *bclk_idx = k; 73966772edaSDaniel Baluta return freq_out; 74084fdc00dSDaniel Baluta } 74182bab889SDaniel Baluta if (diff > 0 && closest > diff) { 74282bab889SDaniel Baluta *sysclk_idx = i; 74382bab889SDaniel Baluta *dac_idx = j; 74482bab889SDaniel Baluta *bclk_idx = k; 74582bab889SDaniel Baluta closest = diff; 74682bab889SDaniel Baluta best_freq_out = freq_out; 74784fdc00dSDaniel Baluta } 74884fdc00dSDaniel Baluta } 74984fdc00dSDaniel Baluta } 75082bab889SDaniel Baluta } 75182bab889SDaniel Baluta 75282bab889SDaniel Baluta return best_freq_out; 75384fdc00dSDaniel Baluta } 754e075fc17SKuninori Morimoto static int wm8960_configure_clocking(struct snd_soc_component *component) 7550e50b51aSZidan Wang { 756e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 75784fdc00dSDaniel Baluta int freq_out, freq_in; 7586d75dfc3SKuninori Morimoto u16 iface1 = snd_soc_component_read(component, WM8960_IFACE1); 7593176bf2dSZidan Wang int i, j, k; 7603ddc9721SDaniel Baluta int ret; 7610e50b51aSZidan Wang 7626b9b546dSShengjiu Wang /* 7636b9b546dSShengjiu Wang * For Slave mode clocking should still be configured, 7646b9b546dSShengjiu Wang * so this if statement should be removed, but some platform 7656b9b546dSShengjiu Wang * may not work if the sysclk is not configured, to avoid such 7666b9b546dSShengjiu Wang * compatible issue, just add '!wm8960->sysclk' condition in 7676b9b546dSShengjiu Wang * this if statement. 7686b9b546dSShengjiu Wang */ 7696b9b546dSShengjiu Wang if (!(iface1 & (1 << 6)) && !wm8960->sysclk) { 7706b9b546dSShengjiu Wang dev_warn(component->dev, 7716b9b546dSShengjiu Wang "slave mode, but proceeding with no clock configuration\n"); 7723176bf2dSZidan Wang return 0; 7730e50b51aSZidan Wang } 7740e50b51aSZidan Wang 7753176bf2dSZidan Wang if (wm8960->clk_id != WM8960_SYSCLK_MCLK && !wm8960->freq_in) { 776e075fc17SKuninori Morimoto dev_err(component->dev, "No MCLK configured\n"); 7773176bf2dSZidan Wang return -EINVAL; 7780e50b51aSZidan Wang } 7790e50b51aSZidan Wang 7803176bf2dSZidan Wang freq_in = wm8960->freq_in; 7810e50b51aSZidan Wang /* 7823176bf2dSZidan Wang * If it's sysclk auto mode, check if the MCLK can provide sysclk or 7833176bf2dSZidan Wang * not. If MCLK can provide sysclk, using MCLK to provide sysclk 7843176bf2dSZidan Wang * directly. Otherwise, auto select a available pll out frequency 7853176bf2dSZidan Wang * and set PLL. 7860e50b51aSZidan Wang */ 7873176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO) { 7883176bf2dSZidan Wang /* disable the PLL and using MCLK to provide sysclk */ 789e075fc17SKuninori Morimoto wm8960_set_pll(component, 0, 0); 7903176bf2dSZidan Wang freq_out = freq_in; 7913176bf2dSZidan Wang } else if (wm8960->sysclk) { 7923176bf2dSZidan Wang freq_out = wm8960->sysclk; 7933176bf2dSZidan Wang } else { 794e075fc17SKuninori Morimoto dev_err(component->dev, "No SYSCLK configured\n"); 7953176bf2dSZidan Wang return -EINVAL; 7963176bf2dSZidan Wang } 7973176bf2dSZidan Wang 7986bb74514SStuart Henderson if (wm8960->clk_id != WM8960_SYSCLK_PLL) { 7993ddc9721SDaniel Baluta ret = wm8960_configure_sysclk(wm8960, freq_out, &i, &j, &k); 8003c01b9eeSDaniel Baluta if (ret >= 0) { 8013176bf2dSZidan Wang goto configure_clock; 8023176bf2dSZidan Wang } else if (wm8960->clk_id != WM8960_SYSCLK_AUTO) { 803e075fc17SKuninori Morimoto dev_err(component->dev, "failed to configure clock\n"); 8043176bf2dSZidan Wang return -EINVAL; 8053176bf2dSZidan Wang } 8066bb74514SStuart Henderson } 8073176bf2dSZidan Wang 808e075fc17SKuninori Morimoto freq_out = wm8960_configure_pll(component, freq_in, &i, &j, &k); 80966772edaSDaniel Baluta if (freq_out < 0) { 810e075fc17SKuninori Morimoto dev_err(component->dev, "failed to configure clock via PLL\n"); 81166772edaSDaniel Baluta return freq_out; 8123176bf2dSZidan Wang } 813e075fc17SKuninori Morimoto wm8960_set_pll(component, freq_in, freq_out); 8143176bf2dSZidan Wang 8153176bf2dSZidan Wang configure_clock: 8163176bf2dSZidan Wang /* configure sysclk clock */ 817e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 3 << 1, i << 1); 8183176bf2dSZidan Wang 8193176bf2dSZidan Wang /* configure frame clock */ 820e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x7 << 3, j << 3); 821e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x7 << 6, j << 6); 8220e50b51aSZidan Wang 8230e50b51aSZidan Wang /* configure bit clock */ 824e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK2, 0xf, k); 8253176bf2dSZidan Wang 8263176bf2dSZidan Wang return 0; 8270e50b51aSZidan Wang } 8280e50b51aSZidan Wang 829f2644a2cSMark Brown static int wm8960_hw_params(struct snd_pcm_substream *substream, 830f2644a2cSMark Brown struct snd_pcm_hw_params *params, 831f2644a2cSMark Brown struct snd_soc_dai *dai) 832f2644a2cSMark Brown { 833e075fc17SKuninori Morimoto struct snd_soc_component *component = dai->component; 834e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 8356d75dfc3SKuninori Morimoto u16 iface = snd_soc_component_read(component, WM8960_IFACE1) & 0xfff3; 8360e50b51aSZidan Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 837db059c0fSMark Brown int i; 838f2644a2cSMark Brown 8390e50b51aSZidan Wang wm8960->bclk = snd_soc_params_to_bclk(params); 8400e50b51aSZidan Wang if (params_channels(params) == 1) 8410e50b51aSZidan Wang wm8960->bclk *= 2; 8420e50b51aSZidan Wang 843f2644a2cSMark Brown /* bit size */ 84439e9cc46SMark Brown switch (params_width(params)) { 84539e9cc46SMark Brown case 16: 846f2644a2cSMark Brown break; 84739e9cc46SMark Brown case 20: 848f2644a2cSMark Brown iface |= 0x0004; 849f2644a2cSMark Brown break; 85039e9cc46SMark Brown case 24: 851f2644a2cSMark Brown iface |= 0x0008; 852f2644a2cSMark Brown break; 8537a8c7867SZidan Wang case 32: 8547a8c7867SZidan Wang /* right justify mode does not support 32 word length */ 8557a8c7867SZidan Wang if ((iface & 0x3) != 0) { 8567a8c7867SZidan Wang iface |= 0x000c; 8577a8c7867SZidan Wang break; 8587a8c7867SZidan Wang } 8593e146b55SGustavo A. R. Silva fallthrough; 8604c2474c0STimur Tabi default: 861e075fc17SKuninori Morimoto dev_err(component->dev, "unsupported width %d\n", 86239e9cc46SMark Brown params_width(params)); 8634c2474c0STimur Tabi return -EINVAL; 864f2644a2cSMark Brown } 865f2644a2cSMark Brown 8663176bf2dSZidan Wang wm8960->lrclk = params_rate(params); 867afd6d36aSMark Brown /* Update filters for the new rate */ 8683176bf2dSZidan Wang if (tx) { 869e075fc17SKuninori Morimoto wm8960_set_deemph(component); 870db059c0fSMark Brown } else { 871db059c0fSMark Brown for (i = 0; i < ARRAY_SIZE(alc_rates); i++) 872db059c0fSMark Brown if (alc_rates[i].rate == params_rate(params)) 873e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, 874db059c0fSMark Brown WM8960_ADDCTL3, 0x7, 875db059c0fSMark Brown alc_rates[i].val); 876afd6d36aSMark Brown } 877afd6d36aSMark Brown 878f2644a2cSMark Brown /* set iface */ 879e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_IFACE1, iface); 8800e50b51aSZidan Wang 8813176bf2dSZidan Wang wm8960->is_stream_in_use[tx] = true; 8823176bf2dSZidan Wang 8831e060a45SShengjiu Wang if (!wm8960->is_stream_in_use[!tx]) 884e075fc17SKuninori Morimoto return wm8960_configure_clocking(component); 8853176bf2dSZidan Wang 8863176bf2dSZidan Wang return 0; 8873176bf2dSZidan Wang } 8883176bf2dSZidan Wang 8893176bf2dSZidan Wang static int wm8960_hw_free(struct snd_pcm_substream *substream, 8903176bf2dSZidan Wang struct snd_soc_dai *dai) 8913176bf2dSZidan Wang { 892e075fc17SKuninori Morimoto struct snd_soc_component *component = dai->component; 893e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 8943176bf2dSZidan Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 8953176bf2dSZidan Wang 8963176bf2dSZidan Wang wm8960->is_stream_in_use[tx] = false; 8970e50b51aSZidan Wang 898f2644a2cSMark Brown return 0; 899f2644a2cSMark Brown } 900f2644a2cSMark Brown 90126d3c16eSKuninori Morimoto static int wm8960_mute(struct snd_soc_dai *dai, int mute, int direction) 902f2644a2cSMark Brown { 903e075fc17SKuninori Morimoto struct snd_soc_component *component = dai->component; 904f2644a2cSMark Brown 905f2644a2cSMark Brown if (mute) 906e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_DACCTL1, 0x8, 0x8); 907f2644a2cSMark Brown else 908e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_DACCTL1, 0x8, 0); 909f2644a2cSMark Brown return 0; 910f2644a2cSMark Brown } 911f2644a2cSMark Brown 912e075fc17SKuninori Morimoto static int wm8960_set_bias_level_out3(struct snd_soc_component *component, 913f2644a2cSMark Brown enum snd_soc_bias_level level) 914f2644a2cSMark Brown { 915e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 9166d75dfc3SKuninori Morimoto u16 pm2 = snd_soc_component_read(component, WM8960_POWER2); 91775aa8868SZidan Wang int ret; 9183c7a4c24SViorel Suman ktime_t tout; 9190ebe36c6SMark Brown 920f2644a2cSMark Brown switch (level) { 921f2644a2cSMark Brown case SND_SOC_BIAS_ON: 922f2644a2cSMark Brown break; 923f2644a2cSMark Brown 924f2644a2cSMark Brown case SND_SOC_BIAS_PREPARE: 925e075fc17SKuninori Morimoto switch (snd_soc_component_get_bias_level(component)) { 92675aa8868SZidan Wang case SND_SOC_BIAS_STANDBY: 92775aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) { 92875aa8868SZidan Wang ret = clk_prepare_enable(wm8960->mclk); 92975aa8868SZidan Wang if (ret) { 930e075fc17SKuninori Morimoto dev_err(component->dev, 93175aa8868SZidan Wang "Failed to enable MCLK: %d\n", 93275aa8868SZidan Wang ret); 93375aa8868SZidan Wang return ret; 93475aa8868SZidan Wang } 93575aa8868SZidan Wang } 93675aa8868SZidan Wang 937e075fc17SKuninori Morimoto ret = wm8960_configure_clocking(component); 9383176bf2dSZidan Wang if (ret) 9393176bf2dSZidan Wang return ret; 9403176bf2dSZidan Wang 941f2644a2cSMark Brown /* Set VMID to 2x50k */ 942e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 0x180, 0x80); 943f2644a2cSMark Brown break; 944f2644a2cSMark Brown 94575aa8868SZidan Wang case SND_SOC_BIAS_ON: 9463176bf2dSZidan Wang /* 9473176bf2dSZidan Wang * If it's sysclk auto mode, and the pll is enabled, 9483176bf2dSZidan Wang * disable the pll 9493176bf2dSZidan Wang */ 9503176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1)) 951e075fc17SKuninori Morimoto wm8960_set_pll(component, 0, 0); 9523176bf2dSZidan Wang 95375aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) 95475aa8868SZidan Wang clk_disable_unprepare(wm8960->mclk); 95575aa8868SZidan Wang break; 95675aa8868SZidan Wang 95775aa8868SZidan Wang default: 95875aa8868SZidan Wang break; 95975aa8868SZidan Wang } 96075aa8868SZidan Wang 96175aa8868SZidan Wang break; 96275aa8868SZidan Wang 963f2644a2cSMark Brown case SND_SOC_BIAS_STANDBY: 964e075fc17SKuninori Morimoto if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { 9653c7a4c24SViorel Suman /* ensure discharge is complete */ 9663c7a4c24SViorel Suman tout = WM8960_DSCH_TOUT - ktime_ms_delta(ktime_get(), wm8960->dsch_start); 9673c7a4c24SViorel Suman if (tout > 0) 9683c7a4c24SViorel Suman msleep(tout); 9693c7a4c24SViorel Suman 9700ebe36c6SMark Brown regcache_sync(wm8960->regmap); 971bc45df2dSAxel Lin 972f2644a2cSMark Brown /* Enable anti-pop features */ 973e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_APOP1, 974f2644a2cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 975f2644a2cSMark Brown WM8960_BUFDCOPEN | WM8960_BUFIOEN); 976f2644a2cSMark Brown 977f2644a2cSMark Brown /* Enable & ramp VMID at 2x50k */ 978e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 0x80, 0x80); 979f2644a2cSMark Brown msleep(100); 980f2644a2cSMark Brown 981f2644a2cSMark Brown /* Enable VREF */ 982e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, WM8960_VREF, 98316b24881SAxel Lin WM8960_VREF); 984f2644a2cSMark Brown 985f2644a2cSMark Brown /* Disable anti-pop features */ 986e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_APOP1, WM8960_BUFIOEN); 987f2644a2cSMark Brown } 988f2644a2cSMark Brown 989f2644a2cSMark Brown /* Set VMID to 2x250k */ 990e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 0x180, 0x100); 991f2644a2cSMark Brown break; 992f2644a2cSMark Brown 993f2644a2cSMark Brown case SND_SOC_BIAS_OFF: 994f2644a2cSMark Brown /* Enable anti-pop features */ 995e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_APOP1, 996f2644a2cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 997f2644a2cSMark Brown WM8960_BUFDCOPEN | WM8960_BUFIOEN); 998f2644a2cSMark Brown 9993c7a4c24SViorel Suman /* Disable VMID and VREF, mark discharge */ 1000e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_POWER1, 0); 10013c7a4c24SViorel Suman wm8960->dsch_start = ktime_get(); 1002913d7b4cSMark Brown break; 1003913d7b4cSMark Brown } 1004f2644a2cSMark Brown 1005913d7b4cSMark Brown return 0; 1006913d7b4cSMark Brown } 1007913d7b4cSMark Brown 1008e075fc17SKuninori Morimoto static int wm8960_set_bias_level_capless(struct snd_soc_component *component, 1009913d7b4cSMark Brown enum snd_soc_bias_level level) 1010913d7b4cSMark Brown { 1011e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 10126d75dfc3SKuninori Morimoto u16 pm2 = snd_soc_component_read(component, WM8960_POWER2); 101375aa8868SZidan Wang int reg, ret; 1014913d7b4cSMark Brown 1015913d7b4cSMark Brown switch (level) { 1016913d7b4cSMark Brown case SND_SOC_BIAS_ON: 1017913d7b4cSMark Brown break; 1018913d7b4cSMark Brown 1019913d7b4cSMark Brown case SND_SOC_BIAS_PREPARE: 1020e075fc17SKuninori Morimoto switch (snd_soc_component_get_bias_level(component)) { 1021913d7b4cSMark Brown case SND_SOC_BIAS_STANDBY: 1022913d7b4cSMark Brown /* Enable anti pop mode */ 1023e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_APOP1, 1024913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1025913d7b4cSMark Brown WM8960_BUFDCOPEN, 1026913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1027913d7b4cSMark Brown WM8960_BUFDCOPEN); 1028913d7b4cSMark Brown 1029913d7b4cSMark Brown /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */ 1030913d7b4cSMark Brown reg = 0; 1031913d7b4cSMark Brown if (wm8960->lout1 && wm8960->lout1->power) 1032913d7b4cSMark Brown reg |= WM8960_PWR2_LOUT1; 1033913d7b4cSMark Brown if (wm8960->rout1 && wm8960->rout1->power) 1034913d7b4cSMark Brown reg |= WM8960_PWR2_ROUT1; 1035913d7b4cSMark Brown if (wm8960->out3 && wm8960->out3->power) 1036913d7b4cSMark Brown reg |= WM8960_PWR2_OUT3; 1037e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER2, 1038913d7b4cSMark Brown WM8960_PWR2_LOUT1 | 1039913d7b4cSMark Brown WM8960_PWR2_ROUT1 | 1040913d7b4cSMark Brown WM8960_PWR2_OUT3, reg); 1041913d7b4cSMark Brown 1042913d7b4cSMark Brown /* Enable VMID at 2*50k */ 1043e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 1044913d7b4cSMark Brown WM8960_VMID_MASK, 0x80); 1045913d7b4cSMark Brown 1046913d7b4cSMark Brown /* Ramp */ 1047913d7b4cSMark Brown msleep(100); 1048913d7b4cSMark Brown 1049913d7b4cSMark Brown /* Enable VREF */ 1050e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 1051913d7b4cSMark Brown WM8960_VREF, WM8960_VREF); 1052913d7b4cSMark Brown 1053913d7b4cSMark Brown msleep(100); 105475aa8868SZidan Wang 105575aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) { 105675aa8868SZidan Wang ret = clk_prepare_enable(wm8960->mclk); 105775aa8868SZidan Wang if (ret) { 1058e075fc17SKuninori Morimoto dev_err(component->dev, 105975aa8868SZidan Wang "Failed to enable MCLK: %d\n", 106075aa8868SZidan Wang ret); 106175aa8868SZidan Wang return ret; 106275aa8868SZidan Wang } 106375aa8868SZidan Wang } 10643176bf2dSZidan Wang 1065e075fc17SKuninori Morimoto ret = wm8960_configure_clocking(component); 10663176bf2dSZidan Wang if (ret) 10673176bf2dSZidan Wang return ret; 10683176bf2dSZidan Wang 1069913d7b4cSMark Brown break; 1070913d7b4cSMark Brown 1071913d7b4cSMark Brown case SND_SOC_BIAS_ON: 10723176bf2dSZidan Wang /* 10733176bf2dSZidan Wang * If it's sysclk auto mode, and the pll is enabled, 10743176bf2dSZidan Wang * disable the pll 10753176bf2dSZidan Wang */ 10763176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1)) 1077e075fc17SKuninori Morimoto wm8960_set_pll(component, 0, 0); 10783176bf2dSZidan Wang 107975aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) 108075aa8868SZidan Wang clk_disable_unprepare(wm8960->mclk); 108175aa8868SZidan Wang 1082913d7b4cSMark Brown /* Enable anti-pop mode */ 1083e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_APOP1, 1084913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1085913d7b4cSMark Brown WM8960_BUFDCOPEN, 1086913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1087913d7b4cSMark Brown WM8960_BUFDCOPEN); 1088913d7b4cSMark Brown 1089913d7b4cSMark Brown /* Disable VMID and VREF */ 1090e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER1, 1091913d7b4cSMark Brown WM8960_VREF | WM8960_VMID_MASK, 0); 1092913d7b4cSMark Brown break; 1093913d7b4cSMark Brown 1094bc45df2dSAxel Lin case SND_SOC_BIAS_OFF: 10950ebe36c6SMark Brown regcache_sync(wm8960->regmap); 1096bc45df2dSAxel Lin break; 1097913d7b4cSMark Brown default: 1098913d7b4cSMark Brown break; 1099913d7b4cSMark Brown } 1100913d7b4cSMark Brown break; 1101913d7b4cSMark Brown 1102913d7b4cSMark Brown case SND_SOC_BIAS_STANDBY: 1103e075fc17SKuninori Morimoto switch (snd_soc_component_get_bias_level(component)) { 1104913d7b4cSMark Brown case SND_SOC_BIAS_PREPARE: 1105913d7b4cSMark Brown /* Disable HP discharge */ 1106e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_APOP2, 1107913d7b4cSMark Brown WM8960_DISOP | WM8960_DRES_MASK, 1108913d7b4cSMark Brown 0); 1109913d7b4cSMark Brown 1110913d7b4cSMark Brown /* Disable anti-pop features */ 1111e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_APOP1, 1112913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1113913d7b4cSMark Brown WM8960_BUFDCOPEN, 1114913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 1115913d7b4cSMark Brown WM8960_BUFDCOPEN); 1116913d7b4cSMark Brown break; 1117913d7b4cSMark Brown 1118913d7b4cSMark Brown default: 1119913d7b4cSMark Brown break; 1120913d7b4cSMark Brown } 1121913d7b4cSMark Brown break; 1122913d7b4cSMark Brown 1123913d7b4cSMark Brown case SND_SOC_BIAS_OFF: 1124f2644a2cSMark Brown break; 1125f2644a2cSMark Brown } 1126f2644a2cSMark Brown 1127f2644a2cSMark Brown return 0; 1128f2644a2cSMark Brown } 1129f2644a2cSMark Brown 1130f2644a2cSMark Brown /* PLL divisors */ 1131f2644a2cSMark Brown struct _pll_div { 1132f2644a2cSMark Brown u32 pre_div:1; 1133f2644a2cSMark Brown u32 n:4; 1134f2644a2cSMark Brown u32 k:24; 1135f2644a2cSMark Brown }; 1136f2644a2cSMark Brown 11373176bf2dSZidan Wang static bool is_pll_freq_available(unsigned int source, unsigned int target) 11383176bf2dSZidan Wang { 11393176bf2dSZidan Wang unsigned int Ndiv; 11403176bf2dSZidan Wang 11413176bf2dSZidan Wang if (source == 0 || target == 0) 11423176bf2dSZidan Wang return false; 11433176bf2dSZidan Wang 11443176bf2dSZidan Wang /* Scale up target to PLL operating frequency */ 11453176bf2dSZidan Wang target *= 4; 11463176bf2dSZidan Wang Ndiv = target / source; 11473176bf2dSZidan Wang 11483176bf2dSZidan Wang if (Ndiv < 6) { 11493176bf2dSZidan Wang source >>= 1; 11503176bf2dSZidan Wang Ndiv = target / source; 11513176bf2dSZidan Wang } 11523176bf2dSZidan Wang 11533176bf2dSZidan Wang if ((Ndiv < 6) || (Ndiv > 12)) 11543176bf2dSZidan Wang return false; 11553176bf2dSZidan Wang 11563176bf2dSZidan Wang return true; 11573176bf2dSZidan Wang } 11583176bf2dSZidan Wang 1159f2644a2cSMark Brown /* The size in bits of the pll divide multiplied by 10 1160f2644a2cSMark Brown * to allow rounding later */ 1161f2644a2cSMark Brown #define FIXED_PLL_SIZE ((1 << 24) * 10) 1162f2644a2cSMark Brown 1163f2644a2cSMark Brown static int pll_factors(unsigned int source, unsigned int target, 1164f2644a2cSMark Brown struct _pll_div *pll_div) 1165f2644a2cSMark Brown { 1166f2644a2cSMark Brown unsigned long long Kpart; 1167f2644a2cSMark Brown unsigned int K, Ndiv, Nmod; 1168f2644a2cSMark Brown 1169f2644a2cSMark Brown pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target); 1170f2644a2cSMark Brown 1171f2644a2cSMark Brown /* Scale up target to PLL operating frequency */ 1172f2644a2cSMark Brown target *= 4; 1173f2644a2cSMark Brown 1174f2644a2cSMark Brown Ndiv = target / source; 1175f2644a2cSMark Brown if (Ndiv < 6) { 1176f2644a2cSMark Brown source >>= 1; 1177f2644a2cSMark Brown pll_div->pre_div = 1; 1178f2644a2cSMark Brown Ndiv = target / source; 1179f2644a2cSMark Brown } else 1180f2644a2cSMark Brown pll_div->pre_div = 0; 1181f2644a2cSMark Brown 1182f2644a2cSMark Brown if ((Ndiv < 6) || (Ndiv > 12)) { 1183f2644a2cSMark Brown pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv); 1184f2644a2cSMark Brown return -EINVAL; 1185f2644a2cSMark Brown } 1186f2644a2cSMark Brown 1187f2644a2cSMark Brown pll_div->n = Ndiv; 1188f2644a2cSMark Brown Nmod = target % source; 1189f2644a2cSMark Brown Kpart = FIXED_PLL_SIZE * (long long)Nmod; 1190f2644a2cSMark Brown 1191f2644a2cSMark Brown do_div(Kpart, source); 1192f2644a2cSMark Brown 1193f2644a2cSMark Brown K = Kpart & 0xFFFFFFFF; 1194f2644a2cSMark Brown 1195f2644a2cSMark Brown /* Check if we need to round */ 1196f2644a2cSMark Brown if ((K % 10) >= 5) 1197f2644a2cSMark Brown K += 5; 1198f2644a2cSMark Brown 1199f2644a2cSMark Brown /* Move down to proper range now rounding is done */ 1200f2644a2cSMark Brown K /= 10; 1201f2644a2cSMark Brown 1202f2644a2cSMark Brown pll_div->k = K; 1203f2644a2cSMark Brown 1204f2644a2cSMark Brown pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n", 1205f2644a2cSMark Brown pll_div->n, pll_div->k, pll_div->pre_div); 1206f2644a2cSMark Brown 1207f2644a2cSMark Brown return 0; 1208f2644a2cSMark Brown } 1209f2644a2cSMark Brown 1210e075fc17SKuninori Morimoto static int wm8960_set_pll(struct snd_soc_component *component, 12113176bf2dSZidan Wang unsigned int freq_in, unsigned int freq_out) 1212f2644a2cSMark Brown { 1213f2644a2cSMark Brown u16 reg; 1214f2644a2cSMark Brown static struct _pll_div pll_div; 1215f2644a2cSMark Brown int ret; 1216f2644a2cSMark Brown 1217f2644a2cSMark Brown if (freq_in && freq_out) { 1218f2644a2cSMark Brown ret = pll_factors(freq_in, freq_out, &pll_div); 1219f2644a2cSMark Brown if (ret != 0) 1220f2644a2cSMark Brown return ret; 1221f2644a2cSMark Brown } 1222f2644a2cSMark Brown 1223f2644a2cSMark Brown /* Disable the PLL: even if we are changing the frequency the 1224f2644a2cSMark Brown * PLL needs to be disabled while we do so. */ 1225e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x1, 0); 1226e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER2, 0x1, 0); 1227f2644a2cSMark Brown 1228f2644a2cSMark Brown if (!freq_in || !freq_out) 1229f2644a2cSMark Brown return 0; 1230f2644a2cSMark Brown 12316d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_PLL1) & ~0x3f; 1232f2644a2cSMark Brown reg |= pll_div.pre_div << 4; 1233f2644a2cSMark Brown reg |= pll_div.n; 1234f2644a2cSMark Brown 1235f2644a2cSMark Brown if (pll_div.k) { 1236f2644a2cSMark Brown reg |= 0x20; 1237f2644a2cSMark Brown 1238e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_PLL2, (pll_div.k >> 16) & 0xff); 1239e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_PLL3, (pll_div.k >> 8) & 0xff); 1240e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_PLL4, pll_div.k & 0xff); 1241f2644a2cSMark Brown } 1242e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_PLL1, reg); 1243f2644a2cSMark Brown 1244f2644a2cSMark Brown /* Turn it on */ 1245e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_POWER2, 0x1, 0x1); 1246f2644a2cSMark Brown msleep(250); 1247e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x1, 0x1); 1248f2644a2cSMark Brown 1249f2644a2cSMark Brown return 0; 1250f2644a2cSMark Brown } 1251f2644a2cSMark Brown 12523176bf2dSZidan Wang static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, 12533176bf2dSZidan Wang int source, unsigned int freq_in, unsigned int freq_out) 12543176bf2dSZidan Wang { 1255e075fc17SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 1256e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 12573176bf2dSZidan Wang 12583176bf2dSZidan Wang wm8960->freq_in = freq_in; 12593176bf2dSZidan Wang 12603176bf2dSZidan Wang if (pll_id == WM8960_SYSCLK_AUTO) 12613176bf2dSZidan Wang return 0; 12623176bf2dSZidan Wang 1263e075fc17SKuninori Morimoto return wm8960_set_pll(component, freq_in, freq_out); 12643176bf2dSZidan Wang } 12653176bf2dSZidan Wang 1266f2644a2cSMark Brown static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai, 1267f2644a2cSMark Brown int div_id, int div) 1268f2644a2cSMark Brown { 1269e075fc17SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 1270f2644a2cSMark Brown u16 reg; 1271f2644a2cSMark Brown 1272f2644a2cSMark Brown switch (div_id) { 1273f2644a2cSMark Brown case WM8960_SYSCLKDIV: 12746d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_CLOCK1) & 0x1f9; 1275e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_CLOCK1, reg | div); 1276f2644a2cSMark Brown break; 1277f2644a2cSMark Brown case WM8960_DACDIV: 12786d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_CLOCK1) & 0x1c7; 1279e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_CLOCK1, reg | div); 1280f2644a2cSMark Brown break; 1281f2644a2cSMark Brown case WM8960_OPCLKDIV: 12826d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_PLL1) & 0x03f; 1283e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_PLL1, reg | div); 1284f2644a2cSMark Brown break; 1285f2644a2cSMark Brown case WM8960_DCLKDIV: 12866d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_CLOCK2) & 0x03f; 1287e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_CLOCK2, reg | div); 1288f2644a2cSMark Brown break; 1289f2644a2cSMark Brown case WM8960_TOCLKSEL: 12906d75dfc3SKuninori Morimoto reg = snd_soc_component_read(component, WM8960_ADDCTL1) & 0x1fd; 1291e075fc17SKuninori Morimoto snd_soc_component_write(component, WM8960_ADDCTL1, reg | div); 1292f2644a2cSMark Brown break; 1293f2644a2cSMark Brown default: 1294f2644a2cSMark Brown return -EINVAL; 1295f2644a2cSMark Brown } 1296f2644a2cSMark Brown 1297f2644a2cSMark Brown return 0; 1298f2644a2cSMark Brown } 1299f2644a2cSMark Brown 1300e075fc17SKuninori Morimoto static int wm8960_set_bias_level(struct snd_soc_component *component, 1301f0fba2adSLiam Girdwood enum snd_soc_bias_level level) 1302f0fba2adSLiam Girdwood { 1303e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 1304f0fba2adSLiam Girdwood 1305e075fc17SKuninori Morimoto return wm8960->set_bias_level(component, level); 1306f0fba2adSLiam Girdwood } 1307f0fba2adSLiam Girdwood 13080e50b51aSZidan Wang static int wm8960_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 13090e50b51aSZidan Wang unsigned int freq, int dir) 13100e50b51aSZidan Wang { 1311e075fc17SKuninori Morimoto struct snd_soc_component *component = dai->component; 1312e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 13130e50b51aSZidan Wang 13140e50b51aSZidan Wang switch (clk_id) { 13150e50b51aSZidan Wang case WM8960_SYSCLK_MCLK: 1316e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 13170e50b51aSZidan Wang 0x1, WM8960_SYSCLK_MCLK); 13180e50b51aSZidan Wang break; 13190e50b51aSZidan Wang case WM8960_SYSCLK_PLL: 1320e075fc17SKuninori Morimoto snd_soc_component_update_bits(component, WM8960_CLOCK1, 13210e50b51aSZidan Wang 0x1, WM8960_SYSCLK_PLL); 13220e50b51aSZidan Wang break; 13233176bf2dSZidan Wang case WM8960_SYSCLK_AUTO: 13243176bf2dSZidan Wang break; 13250e50b51aSZidan Wang default: 13260e50b51aSZidan Wang return -EINVAL; 13270e50b51aSZidan Wang } 13280e50b51aSZidan Wang 13290e50b51aSZidan Wang wm8960->sysclk = freq; 13303176bf2dSZidan Wang wm8960->clk_id = clk_id; 13310e50b51aSZidan Wang 13320e50b51aSZidan Wang return 0; 13330e50b51aSZidan Wang } 13340e50b51aSZidan Wang 1335f2644a2cSMark Brown #define WM8960_RATES SNDRV_PCM_RATE_8000_48000 1336f2644a2cSMark Brown 1337f2644a2cSMark Brown #define WM8960_FORMATS \ 1338f2644a2cSMark Brown (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 13397a8c7867SZidan Wang SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 1340f2644a2cSMark Brown 134185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8960_dai_ops = { 1342f2644a2cSMark Brown .hw_params = wm8960_hw_params, 13433176bf2dSZidan Wang .hw_free = wm8960_hw_free, 134426d3c16eSKuninori Morimoto .mute_stream = wm8960_mute, 1345f2644a2cSMark Brown .set_fmt = wm8960_set_dai_fmt, 1346f2644a2cSMark Brown .set_clkdiv = wm8960_set_dai_clkdiv, 1347f2644a2cSMark Brown .set_pll = wm8960_set_dai_pll, 13480e50b51aSZidan Wang .set_sysclk = wm8960_set_dai_sysclk, 134926d3c16eSKuninori Morimoto .no_capture_mute = 1, 1350f2644a2cSMark Brown }; 1351f2644a2cSMark Brown 1352f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8960_dai = { 1353f0fba2adSLiam Girdwood .name = "wm8960-hifi", 1354f2644a2cSMark Brown .playback = { 1355f2644a2cSMark Brown .stream_name = "Playback", 1356f2644a2cSMark Brown .channels_min = 1, 1357f2644a2cSMark Brown .channels_max = 2, 1358f2644a2cSMark Brown .rates = WM8960_RATES, 1359f2644a2cSMark Brown .formats = WM8960_FORMATS,}, 1360f2644a2cSMark Brown .capture = { 1361f2644a2cSMark Brown .stream_name = "Capture", 1362f2644a2cSMark Brown .channels_min = 1, 1363f2644a2cSMark Brown .channels_max = 2, 1364f2644a2cSMark Brown .rates = WM8960_RATES, 1365f2644a2cSMark Brown .formats = WM8960_FORMATS,}, 1366f2644a2cSMark Brown .ops = &wm8960_dai_ops, 136707695752SKuninori Morimoto .symmetric_rate = 1, 1368f2644a2cSMark Brown }; 1369f2644a2cSMark Brown 1370e075fc17SKuninori Morimoto static int wm8960_probe(struct snd_soc_component *component) 1371f2644a2cSMark Brown { 1372e075fc17SKuninori Morimoto struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component); 1373e2280c90SZidan Wang struct wm8960_data *pdata = &wm8960->pdata; 1374f2644a2cSMark Brown 1375913d7b4cSMark Brown if (pdata->capless) 1376f0fba2adSLiam Girdwood wm8960->set_bias_level = wm8960_set_bias_level_capless; 1377e2280c90SZidan Wang else 1378e2280c90SZidan Wang wm8960->set_bias_level = wm8960_set_bias_level_out3; 1379f2644a2cSMark Brown 1380e075fc17SKuninori Morimoto snd_soc_add_component_controls(component, wm8960_snd_controls, 1381f0fba2adSLiam Girdwood ARRAY_SIZE(wm8960_snd_controls)); 1382e075fc17SKuninori Morimoto wm8960_add_widgets(component); 1383f2644a2cSMark Brown 1384f2644a2cSMark Brown return 0; 1385f2644a2cSMark Brown } 1386f2644a2cSMark Brown 1387e075fc17SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8960 = { 1388f0fba2adSLiam Girdwood .probe = wm8960_probe, 1389f0fba2adSLiam Girdwood .set_bias_level = wm8960_set_bias_level, 1390e075fc17SKuninori Morimoto .suspend_bias_off = 1, 1391e075fc17SKuninori Morimoto .idle_bias_on = 1, 1392e075fc17SKuninori Morimoto .use_pmdown_time = 1, 1393e075fc17SKuninori Morimoto .endianness = 1, 13940ebe36c6SMark Brown }; 13950ebe36c6SMark Brown 13960ebe36c6SMark Brown static const struct regmap_config wm8960_regmap = { 13970ebe36c6SMark Brown .reg_bits = 7, 13980ebe36c6SMark Brown .val_bits = 9, 13990ebe36c6SMark Brown .max_register = WM8960_PLL4, 14000ebe36c6SMark Brown 14010ebe36c6SMark Brown .reg_defaults = wm8960_reg_defaults, 14020ebe36c6SMark Brown .num_reg_defaults = ARRAY_SIZE(wm8960_reg_defaults), 140311e13540SMark Brown .cache_type = REGCACHE_MAPLE, 14040ebe36c6SMark Brown 14050ebe36c6SMark Brown .volatile_reg = wm8960_volatile, 1406f0fba2adSLiam Girdwood }; 1407f0fba2adSLiam Girdwood 1408e2280c90SZidan Wang static void wm8960_set_pdata_from_of(struct i2c_client *i2c, 1409e2280c90SZidan Wang struct wm8960_data *pdata) 1410e2280c90SZidan Wang { 1411e2280c90SZidan Wang const struct device_node *np = i2c->dev.of_node; 1412e2280c90SZidan Wang 1413e2280c90SZidan Wang if (of_property_read_bool(np, "wlf,capless")) 1414e2280c90SZidan Wang pdata->capless = true; 1415e2280c90SZidan Wang 1416e2280c90SZidan Wang if (of_property_read_bool(np, "wlf,shared-lrclk")) 1417e2280c90SZidan Wang pdata->shared_lrclk = true; 1418c9015a17SShengjiu Wang 1419c9015a17SShengjiu Wang of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_cfg, 1420c9015a17SShengjiu Wang ARRAY_SIZE(pdata->gpio_cfg)); 1421c9015a17SShengjiu Wang 1422c9015a17SShengjiu Wang of_property_read_u32_array(np, "wlf,hp-cfg", pdata->hp_cfg, 1423c9015a17SShengjiu Wang ARRAY_SIZE(pdata->hp_cfg)); 1424e2280c90SZidan Wang } 1425e2280c90SZidan Wang 142697b0b6e3SStephen Kitt static int wm8960_i2c_probe(struct i2c_client *i2c) 1427f2644a2cSMark Brown { 142837061631SMark Brown struct wm8960_data *pdata = dev_get_platdata(&i2c->dev); 1429f2644a2cSMark Brown struct wm8960_priv *wm8960; 1430*422f10adSFabio Estevam unsigned int i; 1431f0fba2adSLiam Girdwood int ret; 14328b30cdbeSFrank Li u8 val; 1433f2644a2cSMark Brown 1434b9791c01SMark Brown wm8960 = devm_kzalloc(&i2c->dev, sizeof(struct wm8960_priv), 1435b9791c01SMark Brown GFP_KERNEL); 1436f2644a2cSMark Brown if (wm8960 == NULL) 1437f2644a2cSMark Brown return -ENOMEM; 1438f2644a2cSMark Brown 143975aa8868SZidan Wang wm8960->mclk = devm_clk_get(&i2c->dev, "mclk"); 144075aa8868SZidan Wang if (IS_ERR(wm8960->mclk)) { 144175aa8868SZidan Wang if (PTR_ERR(wm8960->mclk) == -EPROBE_DEFER) 144275aa8868SZidan Wang return -EPROBE_DEFER; 14430169cc3fSMark Brown } else { 14440169cc3fSMark Brown ret = clk_get_rate(wm8960->mclk); 14450169cc3fSMark Brown if (ret >= 0) { 14460169cc3fSMark Brown wm8960->freq_in = ret; 14470169cc3fSMark Brown } else { 14480169cc3fSMark Brown dev_err(&i2c->dev, "Failed to read MCLK rate: %d\n", 14490169cc3fSMark Brown ret); 14500169cc3fSMark Brown } 145175aa8868SZidan Wang } 145275aa8868SZidan Wang 1453*422f10adSFabio Estevam for (i = 0; i < ARRAY_SIZE(wm8960->supplies); i++) 1454*422f10adSFabio Estevam wm8960->supplies[i].supply = wm8960_supply_names[i]; 1455*422f10adSFabio Estevam 1456*422f10adSFabio Estevam ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8960->supplies), 1457*422f10adSFabio Estevam wm8960->supplies); 1458*422f10adSFabio Estevam if (ret < 0) { 1459*422f10adSFabio Estevam dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); 1460*422f10adSFabio Estevam return ret; 1461*422f10adSFabio Estevam } 1462*422f10adSFabio Estevam 1463*422f10adSFabio Estevam ret = regulator_bulk_enable(ARRAY_SIZE(wm8960->supplies), 1464*422f10adSFabio Estevam wm8960->supplies); 1465*422f10adSFabio Estevam if (ret < 0) { 1466*422f10adSFabio Estevam dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); 1467*422f10adSFabio Estevam return ret; 1468*422f10adSFabio Estevam } 1469*422f10adSFabio Estevam 1470c5e6f5faSSachin Kamat wm8960->regmap = devm_regmap_init_i2c(i2c, &wm8960_regmap); 14710ebe36c6SMark Brown if (IS_ERR(wm8960->regmap)) 14720ebe36c6SMark Brown return PTR_ERR(wm8960->regmap); 14730ebe36c6SMark Brown 1474e2280c90SZidan Wang if (pdata) 1475e2280c90SZidan Wang memcpy(&wm8960->pdata, pdata, sizeof(struct wm8960_data)); 1476e2280c90SZidan Wang else if (i2c->dev.of_node) 1477e2280c90SZidan Wang wm8960_set_pdata_from_of(i2c, &wm8960->pdata); 1478e2280c90SZidan Wang 14798b30cdbeSFrank Li ret = i2c_master_recv(i2c, &val, sizeof(val)); 14808b30cdbeSFrank Li if (ret >= 0) { 14818b30cdbeSFrank Li dev_err(&i2c->dev, "Not wm8960, wm8960 reg can not read by i2c\n"); 14828b30cdbeSFrank Li return -EINVAL; 14838b30cdbeSFrank Li } 14848b30cdbeSFrank Li 14853ad5e861SZidan Wang ret = wm8960_reset(wm8960->regmap); 14863ad5e861SZidan Wang if (ret != 0) { 14873ad5e861SZidan Wang dev_err(&i2c->dev, "Failed to issue reset\n"); 14883ad5e861SZidan Wang return ret; 14893ad5e861SZidan Wang } 14903ad5e861SZidan Wang 14913ad5e861SZidan Wang if (wm8960->pdata.shared_lrclk) { 149237061631SMark Brown ret = regmap_update_bits(wm8960->regmap, WM8960_ADDCTL2, 149337061631SMark Brown 0x4, 0x4); 149437061631SMark Brown if (ret != 0) { 149537061631SMark Brown dev_err(&i2c->dev, "Failed to enable LRCM: %d\n", 149637061631SMark Brown ret); 149737061631SMark Brown return ret; 149837061631SMark Brown } 149937061631SMark Brown } 150037061631SMark Brown 15013ad5e861SZidan Wang /* Latch the update bits */ 15023ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LINVOL, 0x100, 0x100); 15033ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RINVOL, 0x100, 0x100); 15043ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LADC, 0x100, 0x100); 15053ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RADC, 0x100, 0x100); 15063ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LDAC, 0x100, 0x100); 15073ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RDAC, 0x100, 0x100); 15083ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LOUT1, 0x100, 0x100); 15093ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_ROUT1, 0x100, 0x100); 15103ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LOUT2, 0x100, 0x100); 15113ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_ROUT2, 0x100, 0x100); 15123ad5e861SZidan Wang 1513c9015a17SShengjiu Wang /* ADCLRC pin configured as GPIO. */ 1514c9015a17SShengjiu Wang regmap_update_bits(wm8960->regmap, WM8960_IFACE2, 1 << 6, 1515c9015a17SShengjiu Wang wm8960->pdata.gpio_cfg[0] << 6); 1516c9015a17SShengjiu Wang regmap_update_bits(wm8960->regmap, WM8960_ADDCTL4, 0xF << 4, 1517c9015a17SShengjiu Wang wm8960->pdata.gpio_cfg[1] << 4); 1518c9015a17SShengjiu Wang 1519c9015a17SShengjiu Wang /* Enable headphone jack detect */ 1520c9015a17SShengjiu Wang regmap_update_bits(wm8960->regmap, WM8960_ADDCTL4, 3 << 2, 1521c9015a17SShengjiu Wang wm8960->pdata.hp_cfg[0] << 2); 1522c9015a17SShengjiu Wang regmap_update_bits(wm8960->regmap, WM8960_ADDCTL2, 3 << 5, 1523c9015a17SShengjiu Wang wm8960->pdata.hp_cfg[1] << 5); 1524c9015a17SShengjiu Wang regmap_update_bits(wm8960->regmap, WM8960_ADDCTL1, 3, 1525c9015a17SShengjiu Wang wm8960->pdata.hp_cfg[2]); 1526c9015a17SShengjiu Wang 1527f2644a2cSMark Brown i2c_set_clientdata(i2c, wm8960); 1528f2644a2cSMark Brown 1529e075fc17SKuninori Morimoto ret = devm_snd_soc_register_component(&i2c->dev, 1530e075fc17SKuninori Morimoto &soc_component_dev_wm8960, &wm8960_dai, 1); 1531b9791c01SMark Brown 1532f0fba2adSLiam Girdwood return ret; 1533f2644a2cSMark Brown } 1534f2644a2cSMark Brown 1535ed5c2f5fSUwe Kleine-König static void wm8960_i2c_remove(struct i2c_client *client) 1536*422f10adSFabio Estevam { 1537*422f10adSFabio Estevam struct wm8960_priv *wm8960 = i2c_get_clientdata(client); 1538*422f10adSFabio Estevam 1539*422f10adSFabio Estevam regulator_bulk_disable(ARRAY_SIZE(wm8960->supplies), wm8960->supplies); 1540*422f10adSFabio Estevam } 1541f2644a2cSMark Brown 1542f2644a2cSMark Brown static const struct i2c_device_id wm8960_i2c_id[] = { 1543f2644a2cSMark Brown { "wm8960", 0 }, 1544f2644a2cSMark Brown { } 1545f2644a2cSMark Brown }; 1546f2644a2cSMark Brown MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id); 1547f2644a2cSMark Brown 15487e0bdbaeSNicola Lunghi #if defined(CONFIG_OF) 1549e2280c90SZidan Wang static const struct of_device_id wm8960_of_match[] = { 1550e2280c90SZidan Wang { .compatible = "wlf,wm8960", }, 1551e2280c90SZidan Wang { } 1552e2280c90SZidan Wang }; 1553e2280c90SZidan Wang MODULE_DEVICE_TABLE(of, wm8960_of_match); 15547e0bdbaeSNicola Lunghi #endif 15557e0bdbaeSNicola Lunghi 15567e0bdbaeSNicola Lunghi #if defined(CONFIG_ACPI) 15577e0bdbaeSNicola Lunghi static const struct acpi_device_id wm8960_acpi_match[] = { 15587e0bdbaeSNicola Lunghi { "1AEC8960", 0 }, /* Wolfson PCI ID + part ID */ 15597e0bdbaeSNicola Lunghi { "10138960", 0 }, /* Cirrus Logic PCI ID + part ID */ 15607e0bdbaeSNicola Lunghi { }, 15617e0bdbaeSNicola Lunghi }; 15627e0bdbaeSNicola Lunghi MODULE_DEVICE_TABLE(acpi, wm8960_acpi_match); 15637e0bdbaeSNicola Lunghi #endif 1564e2280c90SZidan Wang 1565f2644a2cSMark Brown static struct i2c_driver wm8960_i2c_driver = { 1566f2644a2cSMark Brown .driver = { 1567091edccfSMark Brown .name = "wm8960", 15687e0bdbaeSNicola Lunghi .of_match_table = of_match_ptr(wm8960_of_match), 15697e0bdbaeSNicola Lunghi .acpi_match_table = ACPI_PTR(wm8960_acpi_match), 1570f2644a2cSMark Brown }, 15719abcd240SUwe Kleine-König .probe = wm8960_i2c_probe, 15727a79e94eSBill Pemberton .remove = wm8960_i2c_remove, 1573f2644a2cSMark Brown .id_table = wm8960_i2c_id, 1574f2644a2cSMark Brown }; 1575f2644a2cSMark Brown 15763c010e60SSachin Kamat module_i2c_driver(wm8960_i2c_driver); 1577f2644a2cSMark Brown 1578f2644a2cSMark Brown MODULE_DESCRIPTION("ASoC WM8960 driver"); 1579f2644a2cSMark Brown MODULE_AUTHOR("Liam Girdwood"); 1580f2644a2cSMark Brown MODULE_LICENSE("GPL"); 1581