1f2644a2cSMark Brown /* 2f2644a2cSMark Brown * wm8960.c -- WM8960 ALSA SoC Audio driver 3f2644a2cSMark Brown * 4656baaebSMark Brown * Copyright 2007-11 Wolfson Microelectronics, plc 5656baaebSMark Brown * 6f2644a2cSMark Brown * Author: Liam Girdwood 7f2644a2cSMark Brown * 8f2644a2cSMark Brown * This program is free software; you can redistribute it and/or modify 9f2644a2cSMark Brown * it under the terms of the GNU General Public License version 2 as 10f2644a2cSMark Brown * published by the Free Software Foundation. 11f2644a2cSMark Brown */ 12f2644a2cSMark Brown 13f2644a2cSMark Brown #include <linux/module.h> 14f2644a2cSMark Brown #include <linux/moduleparam.h> 15f2644a2cSMark Brown #include <linux/init.h> 16f2644a2cSMark Brown #include <linux/delay.h> 17f2644a2cSMark Brown #include <linux/pm.h> 1875aa8868SZidan Wang #include <linux/clk.h> 19f2644a2cSMark Brown #include <linux/i2c.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21f2644a2cSMark Brown #include <sound/core.h> 22f2644a2cSMark Brown #include <sound/pcm.h> 23f2644a2cSMark Brown #include <sound/pcm_params.h> 24f2644a2cSMark Brown #include <sound/soc.h> 25f2644a2cSMark Brown #include <sound/initval.h> 26f2644a2cSMark Brown #include <sound/tlv.h> 27b6877a47SMark Brown #include <sound/wm8960.h> 28f2644a2cSMark Brown 29f2644a2cSMark Brown #include "wm8960.h" 30f2644a2cSMark Brown 31f2644a2cSMark Brown /* R25 - Power 1 */ 32913d7b4cSMark Brown #define WM8960_VMID_MASK 0x180 33f2644a2cSMark Brown #define WM8960_VREF 0x40 34f2644a2cSMark Brown 35913d7b4cSMark Brown /* R26 - Power 2 */ 36913d7b4cSMark Brown #define WM8960_PWR2_LOUT1 0x40 37913d7b4cSMark Brown #define WM8960_PWR2_ROUT1 0x20 38913d7b4cSMark Brown #define WM8960_PWR2_OUT3 0x02 39913d7b4cSMark Brown 40f2644a2cSMark Brown /* R28 - Anti-pop 1 */ 41f2644a2cSMark Brown #define WM8960_POBCTRL 0x80 42f2644a2cSMark Brown #define WM8960_BUFDCOPEN 0x10 43f2644a2cSMark Brown #define WM8960_BUFIOEN 0x08 44f2644a2cSMark Brown #define WM8960_SOFT_ST 0x04 45f2644a2cSMark Brown #define WM8960_HPSTBY 0x01 46f2644a2cSMark Brown 47f2644a2cSMark Brown /* R29 - Anti-pop 2 */ 48f2644a2cSMark Brown #define WM8960_DISOP 0x40 49913d7b4cSMark Brown #define WM8960_DRES_MASK 0x30 50f2644a2cSMark Brown 513176bf2dSZidan Wang static bool is_pll_freq_available(unsigned int source, unsigned int target); 523176bf2dSZidan Wang static int wm8960_set_pll(struct snd_soc_codec *codec, 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 123f2644a2cSMark Brown struct wm8960_priv { 12475aa8868SZidan Wang struct clk *mclk; 1250ebe36c6SMark Brown struct regmap *regmap; 126f0fba2adSLiam Girdwood int (*set_bias_level)(struct snd_soc_codec *, 127f0fba2adSLiam Girdwood enum snd_soc_bias_level level); 128913d7b4cSMark Brown struct snd_soc_dapm_widget *lout1; 129913d7b4cSMark Brown struct snd_soc_dapm_widget *rout1; 130913d7b4cSMark Brown struct snd_soc_dapm_widget *out3; 131afd6d36aSMark Brown bool deemph; 1323176bf2dSZidan Wang int lrclk; 1330e50b51aSZidan Wang int bclk; 1340e50b51aSZidan Wang int sysclk; 1353176bf2dSZidan Wang int clk_id; 1363176bf2dSZidan Wang int freq_in; 1373176bf2dSZidan Wang bool is_stream_in_use[2]; 138e2280c90SZidan Wang struct wm8960_data pdata; 139f2644a2cSMark Brown }; 140f2644a2cSMark Brown 1413ad5e861SZidan Wang #define wm8960_reset(c) regmap_write(c, WM8960_RESET, 0) 142f2644a2cSMark Brown 143f2644a2cSMark Brown /* enumerated controls */ 144f2644a2cSMark Brown static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted", 145f2644a2cSMark Brown "Right Inverted", "Stereo Inversion"}; 146f2644a2cSMark Brown static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"}; 147f2644a2cSMark Brown static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"}; 148f2644a2cSMark Brown static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"}; 149f2644a2cSMark Brown static const char *wm8960_alcmode[] = {"ALC", "Limiter"}; 150f2644a2cSMark Brown 151f2644a2cSMark Brown static const struct soc_enum wm8960_enum[] = { 152f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity), 153f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity), 154f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff), 155f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff), 156f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc), 157f2644a2cSMark Brown SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode), 158f2644a2cSMark Brown }; 159f2644a2cSMark Brown 160afd6d36aSMark Brown static const int deemph_settings[] = { 0, 32000, 44100, 48000 }; 161afd6d36aSMark Brown 162afd6d36aSMark Brown static int wm8960_set_deemph(struct snd_soc_codec *codec) 163afd6d36aSMark Brown { 164afd6d36aSMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 165afd6d36aSMark Brown int val, i, best; 166afd6d36aSMark Brown 167afd6d36aSMark Brown /* If we're using deemphasis select the nearest available sample 168afd6d36aSMark Brown * rate. 169afd6d36aSMark Brown */ 170afd6d36aSMark Brown if (wm8960->deemph) { 171afd6d36aSMark Brown best = 1; 172afd6d36aSMark Brown for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) { 1733176bf2dSZidan Wang if (abs(deemph_settings[i] - wm8960->lrclk) < 1743176bf2dSZidan Wang abs(deemph_settings[best] - wm8960->lrclk)) 175afd6d36aSMark Brown best = i; 176afd6d36aSMark Brown } 177afd6d36aSMark Brown 178afd6d36aSMark Brown val = best << 1; 179afd6d36aSMark Brown } else { 180afd6d36aSMark Brown val = 0; 181afd6d36aSMark Brown } 182afd6d36aSMark Brown 183afd6d36aSMark Brown dev_dbg(codec->dev, "Set deemphasis %d\n", val); 184afd6d36aSMark Brown 185afd6d36aSMark Brown return snd_soc_update_bits(codec, WM8960_DACCTL1, 186afd6d36aSMark Brown 0x6, val); 187afd6d36aSMark Brown } 188afd6d36aSMark Brown 189afd6d36aSMark Brown static int wm8960_get_deemph(struct snd_kcontrol *kcontrol, 190afd6d36aSMark Brown struct snd_ctl_elem_value *ucontrol) 191afd6d36aSMark Brown { 192ea53bf77SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); 193afd6d36aSMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 194afd6d36aSMark Brown 195b4a18c8bSTakashi Iwai ucontrol->value.integer.value[0] = wm8960->deemph; 1963f343f85SDmitry Artamonow return 0; 197afd6d36aSMark Brown } 198afd6d36aSMark Brown 199afd6d36aSMark Brown static int wm8960_put_deemph(struct snd_kcontrol *kcontrol, 200afd6d36aSMark Brown struct snd_ctl_elem_value *ucontrol) 201afd6d36aSMark Brown { 202ea53bf77SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); 203afd6d36aSMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 204c1fe81f2SDan Carpenter unsigned int deemph = ucontrol->value.integer.value[0]; 205afd6d36aSMark Brown 206afd6d36aSMark Brown if (deemph > 1) 207afd6d36aSMark Brown return -EINVAL; 208afd6d36aSMark Brown 209afd6d36aSMark Brown wm8960->deemph = deemph; 210afd6d36aSMark Brown 211afd6d36aSMark Brown return wm8960_set_deemph(codec); 212afd6d36aSMark Brown } 213afd6d36aSMark Brown 214f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0); 215f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1); 216f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0); 217f2644a2cSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 21821eb2693SMark Brown static const DECLARE_TLV_DB_SCALE(boost_tlv, -1200, 300, 1); 219f2644a2cSMark Brown 220f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_snd_controls[] = { 221f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL, 222f2644a2cSMark Brown 0, 63, 0, adc_tlv), 223f2644a2cSMark Brown SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL, 224f2644a2cSMark Brown 6, 1, 0), 225f2644a2cSMark Brown SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL, 226f2644a2cSMark Brown 7, 1, 0), 227f2644a2cSMark Brown 22821eb2693SMark Brown SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT3 Volume", 22921eb2693SMark Brown WM8960_INBMIX1, 4, 7, 0, boost_tlv), 23021eb2693SMark Brown SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT2 Volume", 23121eb2693SMark Brown WM8960_INBMIX1, 1, 7, 0, boost_tlv), 23221eb2693SMark Brown SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT3 Volume", 23321eb2693SMark Brown WM8960_INBMIX2, 4, 7, 0, boost_tlv), 23421eb2693SMark Brown SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT2 Volume", 23521eb2693SMark Brown WM8960_INBMIX2, 1, 7, 0, boost_tlv), 23621eb2693SMark Brown 237f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC, 238f2644a2cSMark Brown 0, 255, 0, dac_tlv), 239f2644a2cSMark Brown 240f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1, 241f2644a2cSMark Brown 0, 127, 0, out_tlv), 242f2644a2cSMark Brown SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1, 243f2644a2cSMark Brown 7, 1, 0), 244f2644a2cSMark Brown 245f2644a2cSMark Brown SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2, 246f2644a2cSMark Brown 0, 127, 0, out_tlv), 247f2644a2cSMark Brown SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2, 248f2644a2cSMark Brown 7, 1, 0), 249f2644a2cSMark Brown SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0), 250f2644a2cSMark Brown SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0), 251f2644a2cSMark Brown 252f2644a2cSMark Brown SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0), 2534faaa8d9SMark Brown SOC_ENUM("ADC Polarity", wm8960_enum[0]), 254f2644a2cSMark Brown SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0), 255f2644a2cSMark Brown 256a077e81eSZidan Wang SOC_ENUM("DAC Polarity", wm8960_enum[1]), 257afd6d36aSMark Brown SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0, 258afd6d36aSMark Brown wm8960_get_deemph, wm8960_put_deemph), 259f2644a2cSMark Brown 2604faaa8d9SMark Brown SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]), 2614faaa8d9SMark Brown SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]), 262f2644a2cSMark Brown SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0), 263f2644a2cSMark Brown SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0), 264f2644a2cSMark Brown 2654faaa8d9SMark Brown SOC_ENUM("ALC Function", wm8960_enum[4]), 266f2644a2cSMark Brown SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0), 267f2644a2cSMark Brown SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1), 268f2644a2cSMark Brown SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0), 269f2644a2cSMark Brown SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0), 2704faaa8d9SMark Brown SOC_ENUM("ALC Mode", wm8960_enum[5]), 271f2644a2cSMark Brown SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0), 272f2644a2cSMark Brown SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0), 273f2644a2cSMark Brown 274f2644a2cSMark Brown SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0), 275f2644a2cSMark Brown SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0), 276f2644a2cSMark Brown 277c324aac0SMa Haijun SOC_DOUBLE_R_TLV("ADC PCM Capture Volume", WM8960_LADC, WM8960_RADC, 278c324aac0SMa Haijun 0, 255, 0, adc_tlv), 279f2644a2cSMark Brown 280f2644a2cSMark Brown SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume", 281f2644a2cSMark Brown WM8960_BYPASS1, 4, 7, 1, bypass_tlv), 282f2644a2cSMark Brown SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume", 283f2644a2cSMark Brown WM8960_LOUTMIX, 4, 7, 1, bypass_tlv), 284f2644a2cSMark Brown SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume", 285f2644a2cSMark Brown WM8960_BYPASS2, 4, 7, 1, bypass_tlv), 286f2644a2cSMark Brown SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume", 287f2644a2cSMark Brown WM8960_ROUTMIX, 4, 7, 1, bypass_tlv), 288f2644a2cSMark Brown }; 289f2644a2cSMark Brown 290f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_lin_boost[] = { 291f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0), 292f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0), 293f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0), 294f2644a2cSMark Brown }; 295f2644a2cSMark Brown 296f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_lin[] = { 297f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0), 298f2644a2cSMark Brown }; 299f2644a2cSMark Brown 300f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_rin_boost[] = { 301f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0), 302f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0), 303f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0), 304f2644a2cSMark Brown }; 305f2644a2cSMark Brown 306f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_rin[] = { 307f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0), 308f2644a2cSMark Brown }; 309f2644a2cSMark Brown 310f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_loutput_mixer[] = { 311f2644a2cSMark Brown SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0), 312f2644a2cSMark Brown SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0), 313f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0), 314f2644a2cSMark Brown }; 315f2644a2cSMark Brown 316f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_routput_mixer[] = { 317f2644a2cSMark Brown SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0), 318f2644a2cSMark Brown SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0), 319f2644a2cSMark Brown SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0), 320f2644a2cSMark Brown }; 321f2644a2cSMark Brown 322f2644a2cSMark Brown static const struct snd_kcontrol_new wm8960_mono_out[] = { 323f2644a2cSMark Brown SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0), 324f2644a2cSMark Brown SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0), 325f2644a2cSMark Brown }; 326f2644a2cSMark Brown 327f2644a2cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = { 328f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT1"), 329f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT1"), 330f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT2"), 331f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT2"), 332f2644a2cSMark Brown SND_SOC_DAPM_INPUT("LINPUT3"), 333f2644a2cSMark Brown SND_SOC_DAPM_INPUT("RINPUT3"), 334f2644a2cSMark Brown 335187774cbSMark Brown SND_SOC_DAPM_SUPPLY("MICB", WM8960_POWER1, 1, 0, NULL, 0), 336f2644a2cSMark Brown 337f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0, 338f2644a2cSMark Brown wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)), 339f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0, 340f2644a2cSMark Brown wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)), 341f2644a2cSMark Brown 342f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0, 343f2644a2cSMark Brown wm8960_lin, ARRAY_SIZE(wm8960_lin)), 344f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0, 345f2644a2cSMark Brown wm8960_rin, ARRAY_SIZE(wm8960_rin)), 346f2644a2cSMark Brown 34744426de4SMark Brown SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER1, 3, 0), 34844426de4SMark Brown SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER1, 2, 0), 349f2644a2cSMark Brown 350f2644a2cSMark Brown SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0), 351f2644a2cSMark Brown SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0), 352f2644a2cSMark Brown 353f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0, 354f2644a2cSMark Brown &wm8960_loutput_mixer[0], 355f2644a2cSMark Brown ARRAY_SIZE(wm8960_loutput_mixer)), 356f2644a2cSMark Brown SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0, 357f2644a2cSMark Brown &wm8960_routput_mixer[0], 358f2644a2cSMark Brown ARRAY_SIZE(wm8960_routput_mixer)), 359f2644a2cSMark Brown 360f2644a2cSMark Brown SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0), 361f2644a2cSMark Brown SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0), 362f2644a2cSMark Brown 363f2644a2cSMark Brown SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0), 364f2644a2cSMark Brown SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0), 365f2644a2cSMark Brown 366f2644a2cSMark Brown SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0), 367f2644a2cSMark Brown SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0), 368f2644a2cSMark Brown 369f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_LP"), 370f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_LN"), 371f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("HP_L"), 372f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("HP_R"), 373f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_RP"), 374f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("SPK_RN"), 375f2644a2cSMark Brown SND_SOC_DAPM_OUTPUT("OUT3"), 376f2644a2cSMark Brown }; 377f2644a2cSMark Brown 378913d7b4cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = { 379913d7b4cSMark Brown SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0, 380913d7b4cSMark Brown &wm8960_mono_out[0], 381913d7b4cSMark Brown ARRAY_SIZE(wm8960_mono_out)), 382913d7b4cSMark Brown }; 383913d7b4cSMark Brown 384913d7b4cSMark Brown /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */ 385913d7b4cSMark Brown static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = { 386913d7b4cSMark Brown SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0), 387913d7b4cSMark Brown }; 388913d7b4cSMark Brown 389f2644a2cSMark Brown static const struct snd_soc_dapm_route audio_paths[] = { 390f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" }, 391f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" }, 392f2644a2cSMark Brown { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" }, 393f2644a2cSMark Brown 394f2644a2cSMark Brown { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", }, 395f2644a2cSMark Brown { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */ 396f2644a2cSMark Brown { "Left Input Mixer", NULL, "LINPUT2" }, 397f2644a2cSMark Brown { "Left Input Mixer", NULL, "LINPUT3" }, 398f2644a2cSMark Brown 399f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" }, 400f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" }, 401f2644a2cSMark Brown { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" }, 402f2644a2cSMark Brown 403f2644a2cSMark Brown { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", }, 404f2644a2cSMark Brown { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */ 405f2644a2cSMark Brown { "Right Input Mixer", NULL, "RINPUT2" }, 40685e36a1fSZidan Wang { "Right Input Mixer", NULL, "RINPUT3" }, 407f2644a2cSMark Brown 408f2644a2cSMark Brown { "Left ADC", NULL, "Left Input Mixer" }, 409f2644a2cSMark Brown { "Right ADC", NULL, "Right Input Mixer" }, 410f2644a2cSMark Brown 411f2644a2cSMark Brown { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" }, 412f2644a2cSMark Brown { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} , 413f2644a2cSMark Brown { "Left Output Mixer", "PCM Playback Switch", "Left DAC" }, 414f2644a2cSMark Brown 415f2644a2cSMark Brown { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" }, 416f2644a2cSMark Brown { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } , 417f2644a2cSMark Brown { "Right Output Mixer", "PCM Playback Switch", "Right DAC" }, 418f2644a2cSMark Brown 419f2644a2cSMark Brown { "LOUT1 PGA", NULL, "Left Output Mixer" }, 420f2644a2cSMark Brown { "ROUT1 PGA", NULL, "Right Output Mixer" }, 421f2644a2cSMark Brown 422f2644a2cSMark Brown { "HP_L", NULL, "LOUT1 PGA" }, 423f2644a2cSMark Brown { "HP_R", NULL, "ROUT1 PGA" }, 424f2644a2cSMark Brown 425f2644a2cSMark Brown { "Left Speaker PGA", NULL, "Left Output Mixer" }, 426f2644a2cSMark Brown { "Right Speaker PGA", NULL, "Right Output Mixer" }, 427f2644a2cSMark Brown 428f2644a2cSMark Brown { "Left Speaker Output", NULL, "Left Speaker PGA" }, 429f2644a2cSMark Brown { "Right Speaker Output", NULL, "Right Speaker PGA" }, 430f2644a2cSMark Brown 431f2644a2cSMark Brown { "SPK_LN", NULL, "Left Speaker Output" }, 432f2644a2cSMark Brown { "SPK_LP", NULL, "Left Speaker Output" }, 433f2644a2cSMark Brown { "SPK_RN", NULL, "Right Speaker Output" }, 434f2644a2cSMark Brown { "SPK_RP", NULL, "Right Speaker Output" }, 435913d7b4cSMark Brown }; 436913d7b4cSMark Brown 437913d7b4cSMark Brown static const struct snd_soc_dapm_route audio_paths_out3[] = { 438913d7b4cSMark Brown { "Mono Output Mixer", "Left Switch", "Left Output Mixer" }, 439913d7b4cSMark Brown { "Mono Output Mixer", "Right Switch", "Right Output Mixer" }, 440f2644a2cSMark Brown 441f2644a2cSMark Brown { "OUT3", NULL, "Mono Output Mixer", } 442f2644a2cSMark Brown }; 443f2644a2cSMark Brown 444913d7b4cSMark Brown static const struct snd_soc_dapm_route audio_paths_capless[] = { 445913d7b4cSMark Brown { "HP_L", NULL, "OUT3 VMID" }, 446913d7b4cSMark Brown { "HP_R", NULL, "OUT3 VMID" }, 447913d7b4cSMark Brown 448913d7b4cSMark Brown { "OUT3 VMID", NULL, "Left Output Mixer" }, 449913d7b4cSMark Brown { "OUT3 VMID", NULL, "Right Output Mixer" }, 450913d7b4cSMark Brown }; 451913d7b4cSMark Brown 452f2644a2cSMark Brown static int wm8960_add_widgets(struct snd_soc_codec *codec) 453f2644a2cSMark Brown { 454b2c812e2SMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 455e2280c90SZidan Wang struct wm8960_data *pdata = &wm8960->pdata; 45693f32f53SLars-Peter Clausen struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec); 457913d7b4cSMark Brown struct snd_soc_dapm_widget *w; 458913d7b4cSMark Brown 459ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets, 460f2644a2cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets)); 461f2644a2cSMark Brown 462ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths)); 463f2644a2cSMark Brown 464913d7b4cSMark Brown /* In capless mode OUT3 is used to provide VMID for the 465913d7b4cSMark Brown * headphone outputs, otherwise it is used as a mono mixer. 466913d7b4cSMark Brown */ 467913d7b4cSMark Brown if (pdata && pdata->capless) { 468ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_capless, 469913d7b4cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets_capless)); 470913d7b4cSMark Brown 471ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths_capless, 472913d7b4cSMark Brown ARRAY_SIZE(audio_paths_capless)); 473913d7b4cSMark Brown } else { 474ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_out3, 475913d7b4cSMark Brown ARRAY_SIZE(wm8960_dapm_widgets_out3)); 476913d7b4cSMark Brown 477ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, audio_paths_out3, 478913d7b4cSMark Brown ARRAY_SIZE(audio_paths_out3)); 479913d7b4cSMark Brown } 480913d7b4cSMark Brown 481913d7b4cSMark Brown /* We need to power up the headphone output stage out of 482913d7b4cSMark Brown * sequence for capless mode. To save scanning the widget 483913d7b4cSMark Brown * list each time to find the desired power state do so now 484913d7b4cSMark Brown * and save the result. 485913d7b4cSMark Brown */ 48600200107SLars-Peter Clausen list_for_each_entry(w, &codec->component.card->widgets, list) { 48793f32f53SLars-Peter Clausen if (w->dapm != dapm) 48897c866deSJarkko Nikula continue; 489913d7b4cSMark Brown if (strcmp(w->name, "LOUT1 PGA") == 0) 490913d7b4cSMark Brown wm8960->lout1 = w; 491913d7b4cSMark Brown if (strcmp(w->name, "ROUT1 PGA") == 0) 492913d7b4cSMark Brown wm8960->rout1 = w; 493913d7b4cSMark Brown if (strcmp(w->name, "OUT3 VMID") == 0) 494913d7b4cSMark Brown wm8960->out3 = w; 495913d7b4cSMark Brown } 496913d7b4cSMark Brown 497f2644a2cSMark Brown return 0; 498f2644a2cSMark Brown } 499f2644a2cSMark Brown 500f2644a2cSMark Brown static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai, 501f2644a2cSMark Brown unsigned int fmt) 502f2644a2cSMark Brown { 503f2644a2cSMark Brown struct snd_soc_codec *codec = codec_dai->codec; 504f2644a2cSMark Brown u16 iface = 0; 505f2644a2cSMark Brown 506f2644a2cSMark Brown /* set master/slave audio interface */ 507f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 508f2644a2cSMark Brown case SND_SOC_DAIFMT_CBM_CFM: 509f2644a2cSMark Brown iface |= 0x0040; 510f2644a2cSMark Brown break; 511f2644a2cSMark Brown case SND_SOC_DAIFMT_CBS_CFS: 512f2644a2cSMark Brown break; 513f2644a2cSMark Brown default: 514f2644a2cSMark Brown return -EINVAL; 515f2644a2cSMark Brown } 516f2644a2cSMark Brown 517f2644a2cSMark Brown /* interface format */ 518f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 519f2644a2cSMark Brown case SND_SOC_DAIFMT_I2S: 520f2644a2cSMark Brown iface |= 0x0002; 521f2644a2cSMark Brown break; 522f2644a2cSMark Brown case SND_SOC_DAIFMT_RIGHT_J: 523f2644a2cSMark Brown break; 524f2644a2cSMark Brown case SND_SOC_DAIFMT_LEFT_J: 525f2644a2cSMark Brown iface |= 0x0001; 526f2644a2cSMark Brown break; 527f2644a2cSMark Brown case SND_SOC_DAIFMT_DSP_A: 528f2644a2cSMark Brown iface |= 0x0003; 529f2644a2cSMark Brown break; 530f2644a2cSMark Brown case SND_SOC_DAIFMT_DSP_B: 531f2644a2cSMark Brown iface |= 0x0013; 532f2644a2cSMark Brown break; 533f2644a2cSMark Brown default: 534f2644a2cSMark Brown return -EINVAL; 535f2644a2cSMark Brown } 536f2644a2cSMark Brown 537f2644a2cSMark Brown /* clock inversion */ 538f2644a2cSMark Brown switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 539f2644a2cSMark Brown case SND_SOC_DAIFMT_NB_NF: 540f2644a2cSMark Brown break; 541f2644a2cSMark Brown case SND_SOC_DAIFMT_IB_IF: 542f2644a2cSMark Brown iface |= 0x0090; 543f2644a2cSMark Brown break; 544f2644a2cSMark Brown case SND_SOC_DAIFMT_IB_NF: 545f2644a2cSMark Brown iface |= 0x0080; 546f2644a2cSMark Brown break; 547f2644a2cSMark Brown case SND_SOC_DAIFMT_NB_IF: 548f2644a2cSMark Brown iface |= 0x0010; 549f2644a2cSMark Brown break; 550f2644a2cSMark Brown default: 551f2644a2cSMark Brown return -EINVAL; 552f2644a2cSMark Brown } 553f2644a2cSMark Brown 554f2644a2cSMark Brown /* set iface */ 55517a52fd6SMark Brown snd_soc_write(codec, WM8960_IFACE1, iface); 556f2644a2cSMark Brown return 0; 557f2644a2cSMark Brown } 558f2644a2cSMark Brown 559db059c0fSMark Brown static struct { 560db059c0fSMark Brown int rate; 561db059c0fSMark Brown unsigned int val; 562db059c0fSMark Brown } alc_rates[] = { 563db059c0fSMark Brown { 48000, 0 }, 564db059c0fSMark Brown { 44100, 0 }, 565db059c0fSMark Brown { 32000, 1 }, 566db059c0fSMark Brown { 22050, 2 }, 567db059c0fSMark Brown { 24000, 2 }, 568db059c0fSMark Brown { 16000, 3 }, 56922ee76daSZidan Wang { 11025, 4 }, 570db059c0fSMark Brown { 12000, 4 }, 571db059c0fSMark Brown { 8000, 5 }, 572db059c0fSMark Brown }; 573db059c0fSMark Brown 5743176bf2dSZidan Wang /* -1 for reserved value */ 5753176bf2dSZidan Wang static const int sysclk_divs[] = { 1, -1, 2, -1 }; 5763176bf2dSZidan Wang 5770e50b51aSZidan Wang /* Multiply 256 for internal 256 div */ 5780e50b51aSZidan Wang static const int dac_divs[] = { 256, 384, 512, 768, 1024, 1408, 1536 }; 5790e50b51aSZidan Wang 5800e50b51aSZidan Wang /* Multiply 10 to eliminate decimials */ 5810e50b51aSZidan Wang static const int bclk_divs[] = { 5820e50b51aSZidan Wang 10, 15, 20, 30, 40, 55, 60, 80, 110, 5830e50b51aSZidan Wang 120, 160, 220, 240, 320, 320, 320 5840e50b51aSZidan Wang }; 5850e50b51aSZidan Wang 5863176bf2dSZidan Wang static int wm8960_configure_clocking(struct snd_soc_codec *codec) 5870e50b51aSZidan Wang { 5880e50b51aSZidan Wang struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 5893176bf2dSZidan Wang int sysclk, bclk, lrclk, freq_out, freq_in; 5900e50b51aSZidan Wang u16 iface1 = snd_soc_read(codec, WM8960_IFACE1); 5913176bf2dSZidan Wang int i, j, k; 5920e50b51aSZidan Wang 5930e50b51aSZidan Wang if (!(iface1 & (1<<6))) { 5940e50b51aSZidan Wang dev_dbg(codec->dev, 5950e50b51aSZidan Wang "Codec is slave mode, no need to configure clock\n"); 5963176bf2dSZidan Wang return 0; 5970e50b51aSZidan Wang } 5980e50b51aSZidan Wang 5993176bf2dSZidan Wang if (wm8960->clk_id != WM8960_SYSCLK_MCLK && !wm8960->freq_in) { 6003176bf2dSZidan Wang dev_err(codec->dev, "No MCLK configured\n"); 6013176bf2dSZidan Wang return -EINVAL; 6020e50b51aSZidan Wang } 6030e50b51aSZidan Wang 6043176bf2dSZidan Wang freq_in = wm8960->freq_in; 6053176bf2dSZidan Wang bclk = wm8960->bclk; 6063176bf2dSZidan Wang lrclk = wm8960->lrclk; 6070e50b51aSZidan Wang /* 6083176bf2dSZidan Wang * If it's sysclk auto mode, check if the MCLK can provide sysclk or 6093176bf2dSZidan Wang * not. If MCLK can provide sysclk, using MCLK to provide sysclk 6103176bf2dSZidan Wang * directly. Otherwise, auto select a available pll out frequency 6113176bf2dSZidan Wang * and set PLL. 6120e50b51aSZidan Wang */ 6133176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO) { 6143176bf2dSZidan Wang /* disable the PLL and using MCLK to provide sysclk */ 6153176bf2dSZidan Wang wm8960_set_pll(codec, 0, 0); 6163176bf2dSZidan Wang freq_out = freq_in; 6173176bf2dSZidan Wang } else if (wm8960->sysclk) { 6183176bf2dSZidan Wang freq_out = wm8960->sysclk; 6193176bf2dSZidan Wang } else { 6203176bf2dSZidan Wang dev_err(codec->dev, "No SYSCLK configured\n"); 6213176bf2dSZidan Wang return -EINVAL; 6223176bf2dSZidan Wang } 6233176bf2dSZidan Wang 6243176bf2dSZidan Wang /* check if the sysclk frequency is available. */ 6253176bf2dSZidan Wang for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { 6263176bf2dSZidan Wang if (sysclk_divs[i] == -1) 6273176bf2dSZidan Wang continue; 6283176bf2dSZidan Wang sysclk = freq_out / sysclk_divs[i]; 6293176bf2dSZidan Wang for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { 6303176bf2dSZidan Wang if (sysclk == dac_divs[j] * lrclk) { 6313176bf2dSZidan Wang for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) 6323176bf2dSZidan Wang if (sysclk == bclk * bclk_divs[k] / 10) 6333176bf2dSZidan Wang break; 6343176bf2dSZidan Wang if (k != ARRAY_SIZE(bclk_divs)) 6353176bf2dSZidan Wang break; 6363176bf2dSZidan Wang } 6373176bf2dSZidan Wang } 6383176bf2dSZidan Wang if (j != ARRAY_SIZE(dac_divs)) 6393176bf2dSZidan Wang break; 6403176bf2dSZidan Wang } 6413176bf2dSZidan Wang 6423176bf2dSZidan Wang if (i != ARRAY_SIZE(sysclk_divs)) { 6433176bf2dSZidan Wang goto configure_clock; 6443176bf2dSZidan Wang } else if (wm8960->clk_id != WM8960_SYSCLK_AUTO) { 6453176bf2dSZidan Wang dev_err(codec->dev, "failed to configure clock\n"); 6463176bf2dSZidan Wang return -EINVAL; 6473176bf2dSZidan Wang } 6483176bf2dSZidan Wang /* get a available pll out frequency and set pll */ 6493176bf2dSZidan Wang for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { 6503176bf2dSZidan Wang if (sysclk_divs[i] == -1) 6513176bf2dSZidan Wang continue; 6523176bf2dSZidan Wang for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { 6533176bf2dSZidan Wang sysclk = lrclk * dac_divs[j]; 6543176bf2dSZidan Wang freq_out = sysclk * sysclk_divs[i]; 6553176bf2dSZidan Wang 6563176bf2dSZidan Wang for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { 6573176bf2dSZidan Wang if (sysclk == bclk * bclk_divs[k] / 10 && 6583176bf2dSZidan Wang is_pll_freq_available(freq_in, freq_out)) { 6593176bf2dSZidan Wang wm8960_set_pll(codec, 6603176bf2dSZidan Wang freq_in, freq_out); 6613176bf2dSZidan Wang break; 6623176bf2dSZidan Wang } else { 6633176bf2dSZidan Wang continue; 6643176bf2dSZidan Wang } 6653176bf2dSZidan Wang } 6663176bf2dSZidan Wang if (k != ARRAY_SIZE(bclk_divs)) 6673176bf2dSZidan Wang break; 6683176bf2dSZidan Wang } 6693176bf2dSZidan Wang if (j != ARRAY_SIZE(dac_divs)) 6703176bf2dSZidan Wang break; 6713176bf2dSZidan Wang } 6723176bf2dSZidan Wang 6733176bf2dSZidan Wang if (i == ARRAY_SIZE(sysclk_divs)) { 6743176bf2dSZidan Wang dev_err(codec->dev, "failed to configure clock\n"); 6753176bf2dSZidan Wang return -EINVAL; 6763176bf2dSZidan Wang } 6773176bf2dSZidan Wang 6783176bf2dSZidan Wang configure_clock: 6793176bf2dSZidan Wang /* configure sysclk clock */ 6803176bf2dSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK1, 3 << 1, i << 1); 6813176bf2dSZidan Wang 6823176bf2dSZidan Wang /* configure frame clock */ 6833176bf2dSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK1, 0x7 << 3, j << 3); 6843176bf2dSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK1, 0x7 << 6, j << 6); 6850e50b51aSZidan Wang 6860e50b51aSZidan Wang /* configure bit clock */ 6873176bf2dSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK2, 0xf, k); 6883176bf2dSZidan Wang 6893176bf2dSZidan Wang return 0; 6900e50b51aSZidan Wang } 6910e50b51aSZidan Wang 692f2644a2cSMark Brown static int wm8960_hw_params(struct snd_pcm_substream *substream, 693f2644a2cSMark Brown struct snd_pcm_hw_params *params, 694f2644a2cSMark Brown struct snd_soc_dai *dai) 695f2644a2cSMark Brown { 696e6968a17SMark Brown struct snd_soc_codec *codec = dai->codec; 697afd6d36aSMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 69817a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3; 6990e50b51aSZidan Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 700db059c0fSMark Brown int i; 701f2644a2cSMark Brown 7020e50b51aSZidan Wang wm8960->bclk = snd_soc_params_to_bclk(params); 7030e50b51aSZidan Wang if (params_channels(params) == 1) 7040e50b51aSZidan Wang wm8960->bclk *= 2; 7050e50b51aSZidan Wang 706f2644a2cSMark Brown /* bit size */ 70739e9cc46SMark Brown switch (params_width(params)) { 70839e9cc46SMark Brown case 16: 709f2644a2cSMark Brown break; 71039e9cc46SMark Brown case 20: 711f2644a2cSMark Brown iface |= 0x0004; 712f2644a2cSMark Brown break; 71339e9cc46SMark Brown case 24: 714f2644a2cSMark Brown iface |= 0x0008; 715f2644a2cSMark Brown break; 7167a8c7867SZidan Wang case 32: 7177a8c7867SZidan Wang /* right justify mode does not support 32 word length */ 7187a8c7867SZidan Wang if ((iface & 0x3) != 0) { 7197a8c7867SZidan Wang iface |= 0x000c; 7207a8c7867SZidan Wang break; 7217a8c7867SZidan Wang } 7224c2474c0STimur Tabi default: 72339e9cc46SMark Brown dev_err(codec->dev, "unsupported width %d\n", 72439e9cc46SMark Brown params_width(params)); 7254c2474c0STimur Tabi return -EINVAL; 726f2644a2cSMark Brown } 727f2644a2cSMark Brown 7283176bf2dSZidan Wang wm8960->lrclk = params_rate(params); 729afd6d36aSMark Brown /* Update filters for the new rate */ 7303176bf2dSZidan Wang if (tx) { 731afd6d36aSMark Brown wm8960_set_deemph(codec); 732db059c0fSMark Brown } else { 733db059c0fSMark Brown for (i = 0; i < ARRAY_SIZE(alc_rates); i++) 734db059c0fSMark Brown if (alc_rates[i].rate == params_rate(params)) 735db059c0fSMark Brown snd_soc_update_bits(codec, 736db059c0fSMark Brown WM8960_ADDCTL3, 0x7, 737db059c0fSMark Brown alc_rates[i].val); 738afd6d36aSMark Brown } 739afd6d36aSMark Brown 740f2644a2cSMark Brown /* set iface */ 74117a52fd6SMark Brown snd_soc_write(codec, WM8960_IFACE1, iface); 7420e50b51aSZidan Wang 7433176bf2dSZidan Wang wm8960->is_stream_in_use[tx] = true; 7443176bf2dSZidan Wang 7453176bf2dSZidan Wang if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON && 7463176bf2dSZidan Wang !wm8960->is_stream_in_use[!tx]) 7473176bf2dSZidan Wang return wm8960_configure_clocking(codec); 7483176bf2dSZidan Wang 7493176bf2dSZidan Wang return 0; 7503176bf2dSZidan Wang } 7513176bf2dSZidan Wang 7523176bf2dSZidan Wang static int wm8960_hw_free(struct snd_pcm_substream *substream, 7533176bf2dSZidan Wang struct snd_soc_dai *dai) 7543176bf2dSZidan Wang { 7553176bf2dSZidan Wang struct snd_soc_codec *codec = dai->codec; 7563176bf2dSZidan Wang struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 7573176bf2dSZidan Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 7583176bf2dSZidan Wang 7593176bf2dSZidan Wang wm8960->is_stream_in_use[tx] = false; 7600e50b51aSZidan Wang 761f2644a2cSMark Brown return 0; 762f2644a2cSMark Brown } 763f2644a2cSMark Brown 764f2644a2cSMark Brown static int wm8960_mute(struct snd_soc_dai *dai, int mute) 765f2644a2cSMark Brown { 766f2644a2cSMark Brown struct snd_soc_codec *codec = dai->codec; 767f2644a2cSMark Brown 768f2644a2cSMark Brown if (mute) 76916b24881SAxel Lin snd_soc_update_bits(codec, WM8960_DACCTL1, 0x8, 0x8); 770f2644a2cSMark Brown else 77116b24881SAxel Lin snd_soc_update_bits(codec, WM8960_DACCTL1, 0x8, 0); 772f2644a2cSMark Brown return 0; 773f2644a2cSMark Brown } 774f2644a2cSMark Brown 775913d7b4cSMark Brown static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec, 776f2644a2cSMark Brown enum snd_soc_bias_level level) 777f2644a2cSMark Brown { 7780ebe36c6SMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 7793176bf2dSZidan Wang u16 pm2 = snd_soc_read(codec, WM8960_POWER2); 78075aa8868SZidan Wang int ret; 7810ebe36c6SMark Brown 782f2644a2cSMark Brown switch (level) { 783f2644a2cSMark Brown case SND_SOC_BIAS_ON: 784f2644a2cSMark Brown break; 785f2644a2cSMark Brown 786f2644a2cSMark Brown case SND_SOC_BIAS_PREPARE: 78793f32f53SLars-Peter Clausen switch (snd_soc_codec_get_bias_level(codec)) { 78875aa8868SZidan Wang case SND_SOC_BIAS_STANDBY: 78975aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) { 79075aa8868SZidan Wang ret = clk_prepare_enable(wm8960->mclk); 79175aa8868SZidan Wang if (ret) { 79275aa8868SZidan Wang dev_err(codec->dev, 79375aa8868SZidan Wang "Failed to enable MCLK: %d\n", 79475aa8868SZidan Wang ret); 79575aa8868SZidan Wang return ret; 79675aa8868SZidan Wang } 79775aa8868SZidan Wang } 79875aa8868SZidan Wang 7993176bf2dSZidan Wang ret = wm8960_configure_clocking(codec); 8003176bf2dSZidan Wang if (ret) 8013176bf2dSZidan Wang return ret; 8023176bf2dSZidan Wang 803f2644a2cSMark Brown /* Set VMID to 2x50k */ 80416b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER1, 0x180, 0x80); 805f2644a2cSMark Brown break; 806f2644a2cSMark Brown 80775aa8868SZidan Wang case SND_SOC_BIAS_ON: 8083176bf2dSZidan Wang /* 8093176bf2dSZidan Wang * If it's sysclk auto mode, and the pll is enabled, 8103176bf2dSZidan Wang * disable the pll 8113176bf2dSZidan Wang */ 8123176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1)) 8133176bf2dSZidan Wang wm8960_set_pll(codec, 0, 0); 8143176bf2dSZidan Wang 81575aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) 81675aa8868SZidan Wang clk_disable_unprepare(wm8960->mclk); 81775aa8868SZidan Wang break; 81875aa8868SZidan Wang 81975aa8868SZidan Wang default: 82075aa8868SZidan Wang break; 82175aa8868SZidan Wang } 82275aa8868SZidan Wang 82375aa8868SZidan Wang break; 82475aa8868SZidan Wang 825f2644a2cSMark Brown case SND_SOC_BIAS_STANDBY: 82693f32f53SLars-Peter Clausen if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) { 8270ebe36c6SMark Brown regcache_sync(wm8960->regmap); 828bc45df2dSAxel Lin 829f2644a2cSMark Brown /* Enable anti-pop features */ 83017a52fd6SMark Brown snd_soc_write(codec, WM8960_APOP1, 831f2644a2cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 832f2644a2cSMark Brown WM8960_BUFDCOPEN | WM8960_BUFIOEN); 833f2644a2cSMark Brown 834f2644a2cSMark Brown /* Enable & ramp VMID at 2x50k */ 83516b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER1, 0x80, 0x80); 836f2644a2cSMark Brown msleep(100); 837f2644a2cSMark Brown 838f2644a2cSMark Brown /* Enable VREF */ 83916b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER1, WM8960_VREF, 84016b24881SAxel Lin WM8960_VREF); 841f2644a2cSMark Brown 842f2644a2cSMark Brown /* Disable anti-pop features */ 84317a52fd6SMark Brown snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN); 844f2644a2cSMark Brown } 845f2644a2cSMark Brown 846f2644a2cSMark Brown /* Set VMID to 2x250k */ 84716b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER1, 0x180, 0x100); 848f2644a2cSMark Brown break; 849f2644a2cSMark Brown 850f2644a2cSMark Brown case SND_SOC_BIAS_OFF: 851f2644a2cSMark Brown /* Enable anti-pop features */ 85217a52fd6SMark Brown snd_soc_write(codec, WM8960_APOP1, 853f2644a2cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 854f2644a2cSMark Brown WM8960_BUFDCOPEN | WM8960_BUFIOEN); 855f2644a2cSMark Brown 856f2644a2cSMark Brown /* Disable VMID and VREF, let them discharge */ 85717a52fd6SMark Brown snd_soc_write(codec, WM8960_POWER1, 0); 858f2644a2cSMark Brown msleep(600); 859913d7b4cSMark Brown break; 860913d7b4cSMark Brown } 861f2644a2cSMark Brown 862913d7b4cSMark Brown return 0; 863913d7b4cSMark Brown } 864913d7b4cSMark Brown 865913d7b4cSMark Brown static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec, 866913d7b4cSMark Brown enum snd_soc_bias_level level) 867913d7b4cSMark Brown { 868b2c812e2SMark Brown struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 8693176bf2dSZidan Wang u16 pm2 = snd_soc_read(codec, WM8960_POWER2); 87075aa8868SZidan Wang int reg, ret; 871913d7b4cSMark Brown 872913d7b4cSMark Brown switch (level) { 873913d7b4cSMark Brown case SND_SOC_BIAS_ON: 874913d7b4cSMark Brown break; 875913d7b4cSMark Brown 876913d7b4cSMark Brown case SND_SOC_BIAS_PREPARE: 87793f32f53SLars-Peter Clausen switch (snd_soc_codec_get_bias_level(codec)) { 878913d7b4cSMark Brown case SND_SOC_BIAS_STANDBY: 879913d7b4cSMark Brown /* Enable anti pop mode */ 880913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_APOP1, 881913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 882913d7b4cSMark Brown WM8960_BUFDCOPEN, 883913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 884913d7b4cSMark Brown WM8960_BUFDCOPEN); 885913d7b4cSMark Brown 886913d7b4cSMark Brown /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */ 887913d7b4cSMark Brown reg = 0; 888913d7b4cSMark Brown if (wm8960->lout1 && wm8960->lout1->power) 889913d7b4cSMark Brown reg |= WM8960_PWR2_LOUT1; 890913d7b4cSMark Brown if (wm8960->rout1 && wm8960->rout1->power) 891913d7b4cSMark Brown reg |= WM8960_PWR2_ROUT1; 892913d7b4cSMark Brown if (wm8960->out3 && wm8960->out3->power) 893913d7b4cSMark Brown reg |= WM8960_PWR2_OUT3; 894913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_POWER2, 895913d7b4cSMark Brown WM8960_PWR2_LOUT1 | 896913d7b4cSMark Brown WM8960_PWR2_ROUT1 | 897913d7b4cSMark Brown WM8960_PWR2_OUT3, reg); 898913d7b4cSMark Brown 899913d7b4cSMark Brown /* Enable VMID at 2*50k */ 900913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_POWER1, 901913d7b4cSMark Brown WM8960_VMID_MASK, 0x80); 902913d7b4cSMark Brown 903913d7b4cSMark Brown /* Ramp */ 904913d7b4cSMark Brown msleep(100); 905913d7b4cSMark Brown 906913d7b4cSMark Brown /* Enable VREF */ 907913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_POWER1, 908913d7b4cSMark Brown WM8960_VREF, WM8960_VREF); 909913d7b4cSMark Brown 910913d7b4cSMark Brown msleep(100); 91175aa8868SZidan Wang 91275aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) { 91375aa8868SZidan Wang ret = clk_prepare_enable(wm8960->mclk); 91475aa8868SZidan Wang if (ret) { 91575aa8868SZidan Wang dev_err(codec->dev, 91675aa8868SZidan Wang "Failed to enable MCLK: %d\n", 91775aa8868SZidan Wang ret); 91875aa8868SZidan Wang return ret; 91975aa8868SZidan Wang } 92075aa8868SZidan Wang } 9213176bf2dSZidan Wang 9223176bf2dSZidan Wang ret = wm8960_configure_clocking(codec); 9233176bf2dSZidan Wang if (ret) 9243176bf2dSZidan Wang return ret; 9253176bf2dSZidan Wang 926913d7b4cSMark Brown break; 927913d7b4cSMark Brown 928913d7b4cSMark Brown case SND_SOC_BIAS_ON: 9293176bf2dSZidan Wang /* 9303176bf2dSZidan Wang * If it's sysclk auto mode, and the pll is enabled, 9313176bf2dSZidan Wang * disable the pll 9323176bf2dSZidan Wang */ 9333176bf2dSZidan Wang if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1)) 9343176bf2dSZidan Wang wm8960_set_pll(codec, 0, 0); 9353176bf2dSZidan Wang 93675aa8868SZidan Wang if (!IS_ERR(wm8960->mclk)) 93775aa8868SZidan Wang clk_disable_unprepare(wm8960->mclk); 93875aa8868SZidan Wang 939913d7b4cSMark Brown /* Enable anti-pop mode */ 940913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_APOP1, 941913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 942913d7b4cSMark Brown WM8960_BUFDCOPEN, 943913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 944913d7b4cSMark Brown WM8960_BUFDCOPEN); 945913d7b4cSMark Brown 946913d7b4cSMark Brown /* Disable VMID and VREF */ 947913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_POWER1, 948913d7b4cSMark Brown WM8960_VREF | WM8960_VMID_MASK, 0); 949913d7b4cSMark Brown break; 950913d7b4cSMark Brown 951bc45df2dSAxel Lin case SND_SOC_BIAS_OFF: 9520ebe36c6SMark Brown regcache_sync(wm8960->regmap); 953bc45df2dSAxel Lin break; 954913d7b4cSMark Brown default: 955913d7b4cSMark Brown break; 956913d7b4cSMark Brown } 957913d7b4cSMark Brown break; 958913d7b4cSMark Brown 959913d7b4cSMark Brown case SND_SOC_BIAS_STANDBY: 96093f32f53SLars-Peter Clausen switch (snd_soc_codec_get_bias_level(codec)) { 961913d7b4cSMark Brown case SND_SOC_BIAS_PREPARE: 962913d7b4cSMark Brown /* Disable HP discharge */ 963913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_APOP2, 964913d7b4cSMark Brown WM8960_DISOP | WM8960_DRES_MASK, 965913d7b4cSMark Brown 0); 966913d7b4cSMark Brown 967913d7b4cSMark Brown /* Disable anti-pop features */ 968913d7b4cSMark Brown snd_soc_update_bits(codec, WM8960_APOP1, 969913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 970913d7b4cSMark Brown WM8960_BUFDCOPEN, 971913d7b4cSMark Brown WM8960_POBCTRL | WM8960_SOFT_ST | 972913d7b4cSMark Brown WM8960_BUFDCOPEN); 973913d7b4cSMark Brown break; 974913d7b4cSMark Brown 975913d7b4cSMark Brown default: 976913d7b4cSMark Brown break; 977913d7b4cSMark Brown } 978913d7b4cSMark Brown break; 979913d7b4cSMark Brown 980913d7b4cSMark Brown case SND_SOC_BIAS_OFF: 981f2644a2cSMark Brown break; 982f2644a2cSMark Brown } 983f2644a2cSMark Brown 984f2644a2cSMark Brown return 0; 985f2644a2cSMark Brown } 986f2644a2cSMark Brown 987f2644a2cSMark Brown /* PLL divisors */ 988f2644a2cSMark Brown struct _pll_div { 989f2644a2cSMark Brown u32 pre_div:1; 990f2644a2cSMark Brown u32 n:4; 991f2644a2cSMark Brown u32 k:24; 992f2644a2cSMark Brown }; 993f2644a2cSMark Brown 9943176bf2dSZidan Wang static bool is_pll_freq_available(unsigned int source, unsigned int target) 9953176bf2dSZidan Wang { 9963176bf2dSZidan Wang unsigned int Ndiv; 9973176bf2dSZidan Wang 9983176bf2dSZidan Wang if (source == 0 || target == 0) 9993176bf2dSZidan Wang return false; 10003176bf2dSZidan Wang 10013176bf2dSZidan Wang /* Scale up target to PLL operating frequency */ 10023176bf2dSZidan Wang target *= 4; 10033176bf2dSZidan Wang Ndiv = target / source; 10043176bf2dSZidan Wang 10053176bf2dSZidan Wang if (Ndiv < 6) { 10063176bf2dSZidan Wang source >>= 1; 10073176bf2dSZidan Wang Ndiv = target / source; 10083176bf2dSZidan Wang } 10093176bf2dSZidan Wang 10103176bf2dSZidan Wang if ((Ndiv < 6) || (Ndiv > 12)) 10113176bf2dSZidan Wang return false; 10123176bf2dSZidan Wang 10133176bf2dSZidan Wang return true; 10143176bf2dSZidan Wang } 10153176bf2dSZidan Wang 1016f2644a2cSMark Brown /* The size in bits of the pll divide multiplied by 10 1017f2644a2cSMark Brown * to allow rounding later */ 1018f2644a2cSMark Brown #define FIXED_PLL_SIZE ((1 << 24) * 10) 1019f2644a2cSMark Brown 1020f2644a2cSMark Brown static int pll_factors(unsigned int source, unsigned int target, 1021f2644a2cSMark Brown struct _pll_div *pll_div) 1022f2644a2cSMark Brown { 1023f2644a2cSMark Brown unsigned long long Kpart; 1024f2644a2cSMark Brown unsigned int K, Ndiv, Nmod; 1025f2644a2cSMark Brown 1026f2644a2cSMark Brown pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target); 1027f2644a2cSMark Brown 1028f2644a2cSMark Brown /* Scale up target to PLL operating frequency */ 1029f2644a2cSMark Brown target *= 4; 1030f2644a2cSMark Brown 1031f2644a2cSMark Brown Ndiv = target / source; 1032f2644a2cSMark Brown if (Ndiv < 6) { 1033f2644a2cSMark Brown source >>= 1; 1034f2644a2cSMark Brown pll_div->pre_div = 1; 1035f2644a2cSMark Brown Ndiv = target / source; 1036f2644a2cSMark Brown } else 1037f2644a2cSMark Brown pll_div->pre_div = 0; 1038f2644a2cSMark Brown 1039f2644a2cSMark Brown if ((Ndiv < 6) || (Ndiv > 12)) { 1040f2644a2cSMark Brown pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv); 1041f2644a2cSMark Brown return -EINVAL; 1042f2644a2cSMark Brown } 1043f2644a2cSMark Brown 1044f2644a2cSMark Brown pll_div->n = Ndiv; 1045f2644a2cSMark Brown Nmod = target % source; 1046f2644a2cSMark Brown Kpart = FIXED_PLL_SIZE * (long long)Nmod; 1047f2644a2cSMark Brown 1048f2644a2cSMark Brown do_div(Kpart, source); 1049f2644a2cSMark Brown 1050f2644a2cSMark Brown K = Kpart & 0xFFFFFFFF; 1051f2644a2cSMark Brown 1052f2644a2cSMark Brown /* Check if we need to round */ 1053f2644a2cSMark Brown if ((K % 10) >= 5) 1054f2644a2cSMark Brown K += 5; 1055f2644a2cSMark Brown 1056f2644a2cSMark Brown /* Move down to proper range now rounding is done */ 1057f2644a2cSMark Brown K /= 10; 1058f2644a2cSMark Brown 1059f2644a2cSMark Brown pll_div->k = K; 1060f2644a2cSMark Brown 1061f2644a2cSMark Brown pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n", 1062f2644a2cSMark Brown pll_div->n, pll_div->k, pll_div->pre_div); 1063f2644a2cSMark Brown 1064f2644a2cSMark Brown return 0; 1065f2644a2cSMark Brown } 1066f2644a2cSMark Brown 10673176bf2dSZidan Wang static int wm8960_set_pll(struct snd_soc_codec *codec, 10683176bf2dSZidan Wang unsigned int freq_in, unsigned int freq_out) 1069f2644a2cSMark Brown { 1070f2644a2cSMark Brown u16 reg; 1071f2644a2cSMark Brown static struct _pll_div pll_div; 1072f2644a2cSMark Brown int ret; 1073f2644a2cSMark Brown 1074f2644a2cSMark Brown if (freq_in && freq_out) { 1075f2644a2cSMark Brown ret = pll_factors(freq_in, freq_out, &pll_div); 1076f2644a2cSMark Brown if (ret != 0) 1077f2644a2cSMark Brown return ret; 1078f2644a2cSMark Brown } 1079f2644a2cSMark Brown 1080f2644a2cSMark Brown /* Disable the PLL: even if we are changing the frequency the 1081f2644a2cSMark Brown * PLL needs to be disabled while we do so. */ 108216b24881SAxel Lin snd_soc_update_bits(codec, WM8960_CLOCK1, 0x1, 0); 108316b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER2, 0x1, 0); 1084f2644a2cSMark Brown 1085f2644a2cSMark Brown if (!freq_in || !freq_out) 1086f2644a2cSMark Brown return 0; 1087f2644a2cSMark Brown 108817a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f; 1089f2644a2cSMark Brown reg |= pll_div.pre_div << 4; 1090f2644a2cSMark Brown reg |= pll_div.n; 1091f2644a2cSMark Brown 1092f2644a2cSMark Brown if (pll_div.k) { 1093f2644a2cSMark Brown reg |= 0x20; 1094f2644a2cSMark Brown 109585fa532bSMike Dyer snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 16) & 0xff); 109685fa532bSMike Dyer snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 8) & 0xff); 109785fa532bSMike Dyer snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0xff); 1098f2644a2cSMark Brown } 109917a52fd6SMark Brown snd_soc_write(codec, WM8960_PLL1, reg); 1100f2644a2cSMark Brown 1101f2644a2cSMark Brown /* Turn it on */ 110216b24881SAxel Lin snd_soc_update_bits(codec, WM8960_POWER2, 0x1, 0x1); 1103f2644a2cSMark Brown msleep(250); 110416b24881SAxel Lin snd_soc_update_bits(codec, WM8960_CLOCK1, 0x1, 0x1); 1105f2644a2cSMark Brown 1106f2644a2cSMark Brown return 0; 1107f2644a2cSMark Brown } 1108f2644a2cSMark Brown 11093176bf2dSZidan Wang static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, 11103176bf2dSZidan Wang int source, unsigned int freq_in, unsigned int freq_out) 11113176bf2dSZidan Wang { 11123176bf2dSZidan Wang struct snd_soc_codec *codec = codec_dai->codec; 11133176bf2dSZidan Wang struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 11143176bf2dSZidan Wang 11153176bf2dSZidan Wang wm8960->freq_in = freq_in; 11163176bf2dSZidan Wang 11173176bf2dSZidan Wang if (pll_id == WM8960_SYSCLK_AUTO) 11183176bf2dSZidan Wang return 0; 11193176bf2dSZidan Wang 11203176bf2dSZidan Wang return wm8960_set_pll(codec, freq_in, freq_out); 11213176bf2dSZidan Wang } 11223176bf2dSZidan Wang 1123f2644a2cSMark Brown static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai, 1124f2644a2cSMark Brown int div_id, int div) 1125f2644a2cSMark Brown { 1126f2644a2cSMark Brown struct snd_soc_codec *codec = codec_dai->codec; 1127f2644a2cSMark Brown u16 reg; 1128f2644a2cSMark Brown 1129f2644a2cSMark Brown switch (div_id) { 1130f2644a2cSMark Brown case WM8960_SYSCLKDIV: 113117a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9; 113217a52fd6SMark Brown snd_soc_write(codec, WM8960_CLOCK1, reg | div); 1133f2644a2cSMark Brown break; 1134f2644a2cSMark Brown case WM8960_DACDIV: 113517a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7; 113617a52fd6SMark Brown snd_soc_write(codec, WM8960_CLOCK1, reg | div); 1137f2644a2cSMark Brown break; 1138f2644a2cSMark Brown case WM8960_OPCLKDIV: 113917a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f; 114017a52fd6SMark Brown snd_soc_write(codec, WM8960_PLL1, reg | div); 1141f2644a2cSMark Brown break; 1142f2644a2cSMark Brown case WM8960_DCLKDIV: 114317a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f; 114417a52fd6SMark Brown snd_soc_write(codec, WM8960_CLOCK2, reg | div); 1145f2644a2cSMark Brown break; 1146f2644a2cSMark Brown case WM8960_TOCLKSEL: 114717a52fd6SMark Brown reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd; 114817a52fd6SMark Brown snd_soc_write(codec, WM8960_ADDCTL1, reg | div); 1149f2644a2cSMark Brown break; 1150f2644a2cSMark Brown default: 1151f2644a2cSMark Brown return -EINVAL; 1152f2644a2cSMark Brown } 1153f2644a2cSMark Brown 1154f2644a2cSMark Brown return 0; 1155f2644a2cSMark Brown } 1156f2644a2cSMark Brown 1157f0fba2adSLiam Girdwood static int wm8960_set_bias_level(struct snd_soc_codec *codec, 1158f0fba2adSLiam Girdwood enum snd_soc_bias_level level) 1159f0fba2adSLiam Girdwood { 1160f0fba2adSLiam Girdwood struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 1161f0fba2adSLiam Girdwood 1162f0fba2adSLiam Girdwood return wm8960->set_bias_level(codec, level); 1163f0fba2adSLiam Girdwood } 1164f0fba2adSLiam Girdwood 11650e50b51aSZidan Wang static int wm8960_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 11660e50b51aSZidan Wang unsigned int freq, int dir) 11670e50b51aSZidan Wang { 11680e50b51aSZidan Wang struct snd_soc_codec *codec = dai->codec; 11690e50b51aSZidan Wang struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 11700e50b51aSZidan Wang 11710e50b51aSZidan Wang switch (clk_id) { 11720e50b51aSZidan Wang case WM8960_SYSCLK_MCLK: 11730e50b51aSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK1, 11740e50b51aSZidan Wang 0x1, WM8960_SYSCLK_MCLK); 11750e50b51aSZidan Wang break; 11760e50b51aSZidan Wang case WM8960_SYSCLK_PLL: 11770e50b51aSZidan Wang snd_soc_update_bits(codec, WM8960_CLOCK1, 11780e50b51aSZidan Wang 0x1, WM8960_SYSCLK_PLL); 11790e50b51aSZidan Wang break; 11803176bf2dSZidan Wang case WM8960_SYSCLK_AUTO: 11813176bf2dSZidan Wang break; 11820e50b51aSZidan Wang default: 11830e50b51aSZidan Wang return -EINVAL; 11840e50b51aSZidan Wang } 11850e50b51aSZidan Wang 11860e50b51aSZidan Wang wm8960->sysclk = freq; 11873176bf2dSZidan Wang wm8960->clk_id = clk_id; 11880e50b51aSZidan Wang 11890e50b51aSZidan Wang return 0; 11900e50b51aSZidan Wang } 11910e50b51aSZidan Wang 1192f2644a2cSMark Brown #define WM8960_RATES SNDRV_PCM_RATE_8000_48000 1193f2644a2cSMark Brown 1194f2644a2cSMark Brown #define WM8960_FORMATS \ 1195f2644a2cSMark Brown (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 11967a8c7867SZidan Wang SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 1197f2644a2cSMark Brown 119885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8960_dai_ops = { 1199f2644a2cSMark Brown .hw_params = wm8960_hw_params, 12003176bf2dSZidan Wang .hw_free = wm8960_hw_free, 1201f2644a2cSMark Brown .digital_mute = wm8960_mute, 1202f2644a2cSMark Brown .set_fmt = wm8960_set_dai_fmt, 1203f2644a2cSMark Brown .set_clkdiv = wm8960_set_dai_clkdiv, 1204f2644a2cSMark Brown .set_pll = wm8960_set_dai_pll, 12050e50b51aSZidan Wang .set_sysclk = wm8960_set_dai_sysclk, 1206f2644a2cSMark Brown }; 1207f2644a2cSMark Brown 1208f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8960_dai = { 1209f0fba2adSLiam Girdwood .name = "wm8960-hifi", 1210f2644a2cSMark Brown .playback = { 1211f2644a2cSMark Brown .stream_name = "Playback", 1212f2644a2cSMark Brown .channels_min = 1, 1213f2644a2cSMark Brown .channels_max = 2, 1214f2644a2cSMark Brown .rates = WM8960_RATES, 1215f2644a2cSMark Brown .formats = WM8960_FORMATS,}, 1216f2644a2cSMark Brown .capture = { 1217f2644a2cSMark Brown .stream_name = "Capture", 1218f2644a2cSMark Brown .channels_min = 1, 1219f2644a2cSMark Brown .channels_max = 2, 1220f2644a2cSMark Brown .rates = WM8960_RATES, 1221f2644a2cSMark Brown .formats = WM8960_FORMATS,}, 1222f2644a2cSMark Brown .ops = &wm8960_dai_ops, 1223f2644a2cSMark Brown .symmetric_rates = 1, 1224f2644a2cSMark Brown }; 1225f2644a2cSMark Brown 1226f0fba2adSLiam Girdwood static int wm8960_probe(struct snd_soc_codec *codec) 1227f2644a2cSMark Brown { 1228f0fba2adSLiam Girdwood struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); 1229e2280c90SZidan Wang struct wm8960_data *pdata = &wm8960->pdata; 1230f2644a2cSMark Brown 1231913d7b4cSMark Brown if (pdata->capless) 1232f0fba2adSLiam Girdwood wm8960->set_bias_level = wm8960_set_bias_level_capless; 1233e2280c90SZidan Wang else 1234e2280c90SZidan Wang wm8960->set_bias_level = wm8960_set_bias_level_out3; 1235f2644a2cSMark Brown 1236022658beSLiam Girdwood snd_soc_add_codec_controls(codec, wm8960_snd_controls, 1237f0fba2adSLiam Girdwood ARRAY_SIZE(wm8960_snd_controls)); 1238f0fba2adSLiam Girdwood wm8960_add_widgets(codec); 1239f2644a2cSMark Brown 1240f2644a2cSMark Brown return 0; 1241f2644a2cSMark Brown } 1242f2644a2cSMark Brown 1243f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8960 = { 1244f0fba2adSLiam Girdwood .probe = wm8960_probe, 1245f0fba2adSLiam Girdwood .set_bias_level = wm8960_set_bias_level, 12460a87a6e1SLars-Peter Clausen .suspend_bias_off = true, 12470ebe36c6SMark Brown }; 12480ebe36c6SMark Brown 12490ebe36c6SMark Brown static const struct regmap_config wm8960_regmap = { 12500ebe36c6SMark Brown .reg_bits = 7, 12510ebe36c6SMark Brown .val_bits = 9, 12520ebe36c6SMark Brown .max_register = WM8960_PLL4, 12530ebe36c6SMark Brown 12540ebe36c6SMark Brown .reg_defaults = wm8960_reg_defaults, 12550ebe36c6SMark Brown .num_reg_defaults = ARRAY_SIZE(wm8960_reg_defaults), 12560ebe36c6SMark Brown .cache_type = REGCACHE_RBTREE, 12570ebe36c6SMark Brown 12580ebe36c6SMark Brown .volatile_reg = wm8960_volatile, 1259f0fba2adSLiam Girdwood }; 1260f0fba2adSLiam Girdwood 1261e2280c90SZidan Wang static void wm8960_set_pdata_from_of(struct i2c_client *i2c, 1262e2280c90SZidan Wang struct wm8960_data *pdata) 1263e2280c90SZidan Wang { 1264e2280c90SZidan Wang const struct device_node *np = i2c->dev.of_node; 1265e2280c90SZidan Wang 1266e2280c90SZidan Wang if (of_property_read_bool(np, "wlf,capless")) 1267e2280c90SZidan Wang pdata->capless = true; 1268e2280c90SZidan Wang 1269e2280c90SZidan Wang if (of_property_read_bool(np, "wlf,shared-lrclk")) 1270e2280c90SZidan Wang pdata->shared_lrclk = true; 1271e2280c90SZidan Wang } 1272e2280c90SZidan Wang 12737a79e94eSBill Pemberton static int wm8960_i2c_probe(struct i2c_client *i2c, 1274f2644a2cSMark Brown const struct i2c_device_id *id) 1275f2644a2cSMark Brown { 127637061631SMark Brown struct wm8960_data *pdata = dev_get_platdata(&i2c->dev); 1277f2644a2cSMark Brown struct wm8960_priv *wm8960; 1278f0fba2adSLiam Girdwood int ret; 1279f2644a2cSMark Brown 1280b9791c01SMark Brown wm8960 = devm_kzalloc(&i2c->dev, sizeof(struct wm8960_priv), 1281b9791c01SMark Brown GFP_KERNEL); 1282f2644a2cSMark Brown if (wm8960 == NULL) 1283f2644a2cSMark Brown return -ENOMEM; 1284f2644a2cSMark Brown 128575aa8868SZidan Wang wm8960->mclk = devm_clk_get(&i2c->dev, "mclk"); 128675aa8868SZidan Wang if (IS_ERR(wm8960->mclk)) { 128775aa8868SZidan Wang if (PTR_ERR(wm8960->mclk) == -EPROBE_DEFER) 128875aa8868SZidan Wang return -EPROBE_DEFER; 128975aa8868SZidan Wang } 129075aa8868SZidan Wang 1291c5e6f5faSSachin Kamat wm8960->regmap = devm_regmap_init_i2c(i2c, &wm8960_regmap); 12920ebe36c6SMark Brown if (IS_ERR(wm8960->regmap)) 12930ebe36c6SMark Brown return PTR_ERR(wm8960->regmap); 12940ebe36c6SMark Brown 1295e2280c90SZidan Wang if (pdata) 1296e2280c90SZidan Wang memcpy(&wm8960->pdata, pdata, sizeof(struct wm8960_data)); 1297e2280c90SZidan Wang else if (i2c->dev.of_node) 1298e2280c90SZidan Wang wm8960_set_pdata_from_of(i2c, &wm8960->pdata); 1299e2280c90SZidan Wang 13003ad5e861SZidan Wang ret = wm8960_reset(wm8960->regmap); 13013ad5e861SZidan Wang if (ret != 0) { 13023ad5e861SZidan Wang dev_err(&i2c->dev, "Failed to issue reset\n"); 13033ad5e861SZidan Wang return ret; 13043ad5e861SZidan Wang } 13053ad5e861SZidan Wang 13063ad5e861SZidan Wang if (wm8960->pdata.shared_lrclk) { 130737061631SMark Brown ret = regmap_update_bits(wm8960->regmap, WM8960_ADDCTL2, 130837061631SMark Brown 0x4, 0x4); 130937061631SMark Brown if (ret != 0) { 131037061631SMark Brown dev_err(&i2c->dev, "Failed to enable LRCM: %d\n", 131137061631SMark Brown ret); 131237061631SMark Brown return ret; 131337061631SMark Brown } 131437061631SMark Brown } 131537061631SMark Brown 13163ad5e861SZidan Wang /* Latch the update bits */ 13173ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LINVOL, 0x100, 0x100); 13183ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RINVOL, 0x100, 0x100); 13193ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LADC, 0x100, 0x100); 13203ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RADC, 0x100, 0x100); 13213ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LDAC, 0x100, 0x100); 13223ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_RDAC, 0x100, 0x100); 13233ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LOUT1, 0x100, 0x100); 13243ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_ROUT1, 0x100, 0x100); 13253ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_LOUT2, 0x100, 0x100); 13263ad5e861SZidan Wang regmap_update_bits(wm8960->regmap, WM8960_ROUT2, 0x100, 0x100); 13273ad5e861SZidan Wang 1328f2644a2cSMark Brown i2c_set_clientdata(i2c, wm8960); 1329f2644a2cSMark Brown 1330f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&i2c->dev, 1331f0fba2adSLiam Girdwood &soc_codec_dev_wm8960, &wm8960_dai, 1); 1332b9791c01SMark Brown 1333f0fba2adSLiam Girdwood return ret; 1334f2644a2cSMark Brown } 1335f2644a2cSMark Brown 13367a79e94eSBill Pemberton static int wm8960_i2c_remove(struct i2c_client *client) 1337f2644a2cSMark Brown { 1338f0fba2adSLiam Girdwood snd_soc_unregister_codec(&client->dev); 1339f2644a2cSMark Brown return 0; 1340f2644a2cSMark Brown } 1341f2644a2cSMark Brown 1342f2644a2cSMark Brown static const struct i2c_device_id wm8960_i2c_id[] = { 1343f2644a2cSMark Brown { "wm8960", 0 }, 1344f2644a2cSMark Brown { } 1345f2644a2cSMark Brown }; 1346f2644a2cSMark Brown MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id); 1347f2644a2cSMark Brown 1348e2280c90SZidan Wang static const struct of_device_id wm8960_of_match[] = { 1349e2280c90SZidan Wang { .compatible = "wlf,wm8960", }, 1350e2280c90SZidan Wang { } 1351e2280c90SZidan Wang }; 1352e2280c90SZidan Wang MODULE_DEVICE_TABLE(of, wm8960_of_match); 1353e2280c90SZidan Wang 1354f2644a2cSMark Brown static struct i2c_driver wm8960_i2c_driver = { 1355f2644a2cSMark Brown .driver = { 1356091edccfSMark Brown .name = "wm8960", 1357e2280c90SZidan Wang .of_match_table = wm8960_of_match, 1358f2644a2cSMark Brown }, 1359f2644a2cSMark Brown .probe = wm8960_i2c_probe, 13607a79e94eSBill Pemberton .remove = wm8960_i2c_remove, 1361f2644a2cSMark Brown .id_table = wm8960_i2c_id, 1362f2644a2cSMark Brown }; 1363f2644a2cSMark Brown 13643c010e60SSachin Kamat module_i2c_driver(wm8960_i2c_driver); 1365f2644a2cSMark Brown 1366f2644a2cSMark Brown MODULE_DESCRIPTION("ASoC WM8960 driver"); 1367f2644a2cSMark Brown MODULE_AUTHOR("Liam Girdwood"); 1368f2644a2cSMark Brown MODULE_LICENSE("GPL"); 1369