15409fb4eSMark Brown /* 25409fb4eSMark Brown * wm8988.c -- WM8988 ALSA SoC audio driver 35409fb4eSMark Brown * 45409fb4eSMark Brown * Copyright 2009 Wolfson Microelectronics plc 55409fb4eSMark Brown * Copyright 2005 Openedhand Ltd. 65409fb4eSMark Brown * 75409fb4eSMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 85409fb4eSMark Brown * 95409fb4eSMark Brown * This program is free software; you can redistribute it and/or modify 105409fb4eSMark Brown * it under the terms of the GNU General Public License version 2 as 115409fb4eSMark Brown * published by the Free Software Foundation. 125409fb4eSMark Brown */ 135409fb4eSMark Brown 145409fb4eSMark Brown #include <linux/module.h> 155409fb4eSMark Brown #include <linux/moduleparam.h> 165409fb4eSMark Brown #include <linux/init.h> 175409fb4eSMark Brown #include <linux/delay.h> 185409fb4eSMark Brown #include <linux/pm.h> 195409fb4eSMark Brown #include <linux/i2c.h> 205409fb4eSMark Brown #include <linux/spi/spi.h> 215409fb4eSMark Brown #include <linux/platform_device.h> 22*5a0e3ad6STejun Heo #include <linux/slab.h> 235409fb4eSMark Brown #include <sound/core.h> 245409fb4eSMark Brown #include <sound/pcm.h> 255409fb4eSMark Brown #include <sound/pcm_params.h> 265409fb4eSMark Brown #include <sound/tlv.h> 275409fb4eSMark Brown #include <sound/soc.h> 285409fb4eSMark Brown #include <sound/soc-dapm.h> 295409fb4eSMark Brown #include <sound/initval.h> 305409fb4eSMark Brown 315409fb4eSMark Brown #include "wm8988.h" 325409fb4eSMark Brown 335409fb4eSMark Brown /* 345409fb4eSMark Brown * wm8988 register cache 355409fb4eSMark Brown * We can't read the WM8988 register space when we 365409fb4eSMark Brown * are using 2 wire for device control, so we cache them instead. 375409fb4eSMark Brown */ 385409fb4eSMark Brown static const u16 wm8988_reg[] = { 395409fb4eSMark Brown 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */ 405409fb4eSMark Brown 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */ 415409fb4eSMark Brown 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */ 425409fb4eSMark Brown 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */ 435409fb4eSMark Brown 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */ 445409fb4eSMark Brown 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */ 455409fb4eSMark Brown 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */ 465409fb4eSMark Brown 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */ 475409fb4eSMark Brown 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */ 485409fb4eSMark Brown 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */ 495409fb4eSMark Brown 0x0079, 0x0079, 0x0079, /* 40 */ 505409fb4eSMark Brown }; 515409fb4eSMark Brown 525409fb4eSMark Brown /* codec private data */ 535409fb4eSMark Brown struct wm8988_priv { 545409fb4eSMark Brown unsigned int sysclk; 555409fb4eSMark Brown struct snd_soc_codec codec; 565409fb4eSMark Brown struct snd_pcm_hw_constraint_list *sysclk_constraints; 575409fb4eSMark Brown u16 reg_cache[WM8988_NUM_REG]; 585409fb4eSMark Brown }; 595409fb4eSMark Brown 605409fb4eSMark Brown 6117a52fd6SMark Brown #define wm8988_reset(c) snd_soc_write(c, WM8988_RESET, 0) 625409fb4eSMark Brown 635409fb4eSMark Brown /* 645409fb4eSMark Brown * WM8988 Controls 655409fb4eSMark Brown */ 665409fb4eSMark Brown 675409fb4eSMark Brown static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"}; 685409fb4eSMark Brown static const struct soc_enum bass_boost = 695409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_BASS, 7, 2, bass_boost_txt); 705409fb4eSMark Brown 715409fb4eSMark Brown static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" }; 725409fb4eSMark Brown static const struct soc_enum bass_filter = 735409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_BASS, 6, 2, bass_filter_txt); 745409fb4eSMark Brown 755409fb4eSMark Brown static const char *treble_txt[] = {"8kHz", "4kHz"}; 765409fb4eSMark Brown static const struct soc_enum treble = 775409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_TREBLE, 6, 2, treble_txt); 785409fb4eSMark Brown 795409fb4eSMark Brown static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"}; 805409fb4eSMark Brown static const struct soc_enum stereo_3d_lc = 815409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_3D, 5, 2, stereo_3d_lc_txt); 825409fb4eSMark Brown 835409fb4eSMark Brown static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"}; 845409fb4eSMark Brown static const struct soc_enum stereo_3d_uc = 855409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_3D, 6, 2, stereo_3d_uc_txt); 865409fb4eSMark Brown 875409fb4eSMark Brown static const char *stereo_3d_func_txt[] = {"Capture", "Playback"}; 885409fb4eSMark Brown static const struct soc_enum stereo_3d_func = 895409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_3D, 7, 2, stereo_3d_func_txt); 905409fb4eSMark Brown 915409fb4eSMark Brown static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"}; 925409fb4eSMark Brown static const struct soc_enum alc_func = 935409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_ALC1, 7, 4, alc_func_txt); 945409fb4eSMark Brown 955409fb4eSMark Brown static const char *ng_type_txt[] = {"Constant PGA Gain", 965409fb4eSMark Brown "Mute ADC Output"}; 975409fb4eSMark Brown static const struct soc_enum ng_type = 985409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_NGATE, 1, 2, ng_type_txt); 995409fb4eSMark Brown 1005409fb4eSMark Brown static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 1015409fb4eSMark Brown static const struct soc_enum deemph = 1025409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_ADCDAC, 1, 4, deemph_txt); 1035409fb4eSMark Brown 1045409fb4eSMark Brown static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert", 1055409fb4eSMark Brown "L + R Invert"}; 1065409fb4eSMark Brown static const struct soc_enum adcpol = 1075409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_ADCDAC, 5, 4, adcpol_txt); 1085409fb4eSMark Brown 1095409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0); 1105409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1); 1115409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); 1125409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 1135409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0); 1145409fb4eSMark Brown 1155409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_snd_controls[] = { 1165409fb4eSMark Brown 1175409fb4eSMark Brown SOC_ENUM("Bass Boost", bass_boost), 1185409fb4eSMark Brown SOC_ENUM("Bass Filter", bass_filter), 1195409fb4eSMark Brown SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1), 1205409fb4eSMark Brown 1215409fb4eSMark Brown SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0), 1225409fb4eSMark Brown SOC_ENUM("Treble Cut-off", treble), 1235409fb4eSMark Brown 1245409fb4eSMark Brown SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0), 1255409fb4eSMark Brown SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0), 1265409fb4eSMark Brown SOC_ENUM("3D Lower Cut-off", stereo_3d_lc), 1275409fb4eSMark Brown SOC_ENUM("3D Upper Cut-off", stereo_3d_uc), 1285409fb4eSMark Brown SOC_ENUM("3D Mode", stereo_3d_func), 1295409fb4eSMark Brown 1305409fb4eSMark Brown SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0), 1315409fb4eSMark Brown SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0), 1325409fb4eSMark Brown SOC_ENUM("ALC Capture Function", alc_func), 1335409fb4eSMark Brown SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0), 1345409fb4eSMark Brown SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0), 1355409fb4eSMark Brown SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0), 1365409fb4eSMark Brown SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0), 1375409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0), 1385409fb4eSMark Brown SOC_ENUM("ALC Capture NG Type", ng_type), 1395409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0), 1405409fb4eSMark Brown 1415409fb4eSMark Brown SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0), 1425409fb4eSMark Brown 1435409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC, 1445409fb4eSMark Brown 0, 255, 0, adc_tlv), 1455409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL, 1465409fb4eSMark Brown 0, 63, 0, pga_tlv), 1475409fb4eSMark Brown SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0), 1485409fb4eSMark Brown SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1), 1495409fb4eSMark Brown 1505409fb4eSMark Brown SOC_ENUM("Playback De-emphasis", deemph), 1515409fb4eSMark Brown 1525409fb4eSMark Brown SOC_ENUM("Capture Polarity", adcpol), 1535409fb4eSMark Brown SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0), 1545409fb4eSMark Brown SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0), 1555409fb4eSMark Brown 1565409fb4eSMark Brown SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv), 1575409fb4eSMark Brown 1585409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1, 1595409fb4eSMark Brown bypass_tlv), 1605409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1, 1615409fb4eSMark Brown bypass_tlv), 1625409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1, 1635409fb4eSMark Brown bypass_tlv), 1645409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1, 1655409fb4eSMark Brown bypass_tlv), 1665409fb4eSMark Brown 1675409fb4eSMark Brown SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V, 1685409fb4eSMark Brown WM8988_ROUT1V, 7, 1, 0), 1695409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V, 1705409fb4eSMark Brown 0, 127, 0, out_tlv), 1715409fb4eSMark Brown 1725409fb4eSMark Brown SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V, 1735409fb4eSMark Brown WM8988_ROUT2V, 7, 1, 0), 1745409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V, 1755409fb4eSMark Brown 0, 127, 0, out_tlv), 1765409fb4eSMark Brown 1775409fb4eSMark Brown }; 1785409fb4eSMark Brown 1795409fb4eSMark Brown /* 1805409fb4eSMark Brown * DAPM Controls 1815409fb4eSMark Brown */ 1825409fb4eSMark Brown 1835409fb4eSMark Brown static int wm8988_lrc_control(struct snd_soc_dapm_widget *w, 1845409fb4eSMark Brown struct snd_kcontrol *kcontrol, int event) 1855409fb4eSMark Brown { 1865409fb4eSMark Brown struct snd_soc_codec *codec = w->codec; 18717a52fd6SMark Brown u16 adctl2 = snd_soc_read(codec, WM8988_ADCTL2); 1885409fb4eSMark Brown 1895409fb4eSMark Brown /* Use the DAC to gate LRC if active, otherwise use ADC */ 19017a52fd6SMark Brown if (snd_soc_read(codec, WM8988_PWR2) & 0x180) 1915409fb4eSMark Brown adctl2 &= ~0x4; 1925409fb4eSMark Brown else 1935409fb4eSMark Brown adctl2 |= 0x4; 1945409fb4eSMark Brown 19517a52fd6SMark Brown return snd_soc_write(codec, WM8988_ADCTL2, adctl2); 1965409fb4eSMark Brown } 1975409fb4eSMark Brown 1985409fb4eSMark Brown static const char *wm8988_line_texts[] = { 1995409fb4eSMark Brown "Line 1", "Line 2", "PGA", "Differential"}; 2005409fb4eSMark Brown 2015409fb4eSMark Brown static const unsigned int wm8988_line_values[] = { 2025409fb4eSMark Brown 0, 1, 3, 4}; 2035409fb4eSMark Brown 2045409fb4eSMark Brown static const struct soc_enum wm8988_lline_enum = 2055409fb4eSMark Brown SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7, 2065409fb4eSMark Brown ARRAY_SIZE(wm8988_line_texts), 2075409fb4eSMark Brown wm8988_line_texts, 2085409fb4eSMark Brown wm8988_line_values); 2095409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_line_controls = 2105409fb4eSMark Brown SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum); 2115409fb4eSMark Brown 2125409fb4eSMark Brown static const struct soc_enum wm8988_rline_enum = 2135409fb4eSMark Brown SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7, 2145409fb4eSMark Brown ARRAY_SIZE(wm8988_line_texts), 2155409fb4eSMark Brown wm8988_line_texts, 2165409fb4eSMark Brown wm8988_line_values); 2175409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_line_controls = 2185409fb4eSMark Brown SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum); 2195409fb4eSMark Brown 2205409fb4eSMark Brown /* Left Mixer */ 2215409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = { 2225409fb4eSMark Brown SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0), 2235409fb4eSMark Brown SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0), 2245409fb4eSMark Brown SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0), 2255409fb4eSMark Brown SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0), 2265409fb4eSMark Brown }; 2275409fb4eSMark Brown 2285409fb4eSMark Brown /* Right Mixer */ 2295409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = { 2305409fb4eSMark Brown SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0), 2315409fb4eSMark Brown SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0), 2325409fb4eSMark Brown SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0), 2335409fb4eSMark Brown SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0), 2345409fb4eSMark Brown }; 2355409fb4eSMark Brown 2365409fb4eSMark Brown static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"}; 2375409fb4eSMark Brown static const unsigned int wm8988_pga_val[] = { 0, 1, 3 }; 2385409fb4eSMark Brown 2395409fb4eSMark Brown /* Left PGA Mux */ 2405409fb4eSMark Brown static const struct soc_enum wm8988_lpga_enum = 2415409fb4eSMark Brown SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3, 2425409fb4eSMark Brown ARRAY_SIZE(wm8988_pga_sel), 2435409fb4eSMark Brown wm8988_pga_sel, 2445409fb4eSMark Brown wm8988_pga_val); 2455409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_pga_controls = 2465409fb4eSMark Brown SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum); 2475409fb4eSMark Brown 2485409fb4eSMark Brown /* Right PGA Mux */ 2495409fb4eSMark Brown static const struct soc_enum wm8988_rpga_enum = 2505409fb4eSMark Brown SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3, 2515409fb4eSMark Brown ARRAY_SIZE(wm8988_pga_sel), 2525409fb4eSMark Brown wm8988_pga_sel, 2535409fb4eSMark Brown wm8988_pga_val); 2545409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_pga_controls = 2555409fb4eSMark Brown SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum); 2565409fb4eSMark Brown 2575409fb4eSMark Brown /* Differential Mux */ 2585409fb4eSMark Brown static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"}; 2595409fb4eSMark Brown static const struct soc_enum diffmux = 2605409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_ADCIN, 8, 2, wm8988_diff_sel); 2615409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_diffmux_controls = 2625409fb4eSMark Brown SOC_DAPM_ENUM("Route", diffmux); 2635409fb4eSMark Brown 2645409fb4eSMark Brown /* Mono ADC Mux */ 2655409fb4eSMark Brown static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)", 2665409fb4eSMark Brown "Mono (Right)", "Digital Mono"}; 2675409fb4eSMark Brown static const struct soc_enum monomux = 2685409fb4eSMark Brown SOC_ENUM_SINGLE(WM8988_ADCIN, 6, 4, wm8988_mono_mux); 2695409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_monomux_controls = 2705409fb4eSMark Brown SOC_DAPM_ENUM("Route", monomux); 2715409fb4eSMark Brown 2725409fb4eSMark Brown static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = { 2735409fb4eSMark Brown SND_SOC_DAPM_MICBIAS("Mic Bias", WM8988_PWR1, 1, 0), 2745409fb4eSMark Brown 2755409fb4eSMark Brown SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0, 2765409fb4eSMark Brown &wm8988_diffmux_controls), 2775409fb4eSMark Brown SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0, 2785409fb4eSMark Brown &wm8988_monomux_controls), 2795409fb4eSMark Brown SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0, 2805409fb4eSMark Brown &wm8988_monomux_controls), 2815409fb4eSMark Brown 2825409fb4eSMark Brown SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0, 2835409fb4eSMark Brown &wm8988_left_pga_controls), 2845409fb4eSMark Brown SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0, 2855409fb4eSMark Brown &wm8988_right_pga_controls), 2865409fb4eSMark Brown 2875409fb4eSMark Brown SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0, 2885409fb4eSMark Brown &wm8988_left_line_controls), 2895409fb4eSMark Brown SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0, 2905409fb4eSMark Brown &wm8988_right_line_controls), 2915409fb4eSMark Brown 2925409fb4eSMark Brown SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0), 2935409fb4eSMark Brown SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0), 2945409fb4eSMark Brown 2955409fb4eSMark Brown SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0), 2965409fb4eSMark Brown SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0), 2975409fb4eSMark Brown 2985409fb4eSMark Brown SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0, 2995409fb4eSMark Brown &wm8988_left_mixer_controls[0], 3005409fb4eSMark Brown ARRAY_SIZE(wm8988_left_mixer_controls)), 3015409fb4eSMark Brown SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0, 3025409fb4eSMark Brown &wm8988_right_mixer_controls[0], 3035409fb4eSMark Brown ARRAY_SIZE(wm8988_right_mixer_controls)), 3045409fb4eSMark Brown 3055409fb4eSMark Brown SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0), 3065409fb4eSMark Brown SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0), 3075409fb4eSMark Brown SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0), 3085409fb4eSMark Brown SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0), 3095409fb4eSMark Brown 3105409fb4eSMark Brown SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control), 3115409fb4eSMark Brown 3125409fb4eSMark Brown SND_SOC_DAPM_OUTPUT("LOUT1"), 3135409fb4eSMark Brown SND_SOC_DAPM_OUTPUT("ROUT1"), 3145409fb4eSMark Brown SND_SOC_DAPM_OUTPUT("LOUT2"), 3155409fb4eSMark Brown SND_SOC_DAPM_OUTPUT("ROUT2"), 3165409fb4eSMark Brown SND_SOC_DAPM_OUTPUT("VREF"), 3175409fb4eSMark Brown 3185409fb4eSMark Brown SND_SOC_DAPM_INPUT("LINPUT1"), 3195409fb4eSMark Brown SND_SOC_DAPM_INPUT("LINPUT2"), 3205409fb4eSMark Brown SND_SOC_DAPM_INPUT("RINPUT1"), 3215409fb4eSMark Brown SND_SOC_DAPM_INPUT("RINPUT2"), 3225409fb4eSMark Brown }; 3235409fb4eSMark Brown 3245409fb4eSMark Brown static const struct snd_soc_dapm_route audio_map[] = { 3255409fb4eSMark Brown 3265409fb4eSMark Brown { "Left Line Mux", "Line 1", "LINPUT1" }, 3275409fb4eSMark Brown { "Left Line Mux", "Line 2", "LINPUT2" }, 3285409fb4eSMark Brown { "Left Line Mux", "PGA", "Left PGA Mux" }, 3295409fb4eSMark Brown { "Left Line Mux", "Differential", "Differential Mux" }, 3305409fb4eSMark Brown 3315409fb4eSMark Brown { "Right Line Mux", "Line 1", "RINPUT1" }, 3325409fb4eSMark Brown { "Right Line Mux", "Line 2", "RINPUT2" }, 3335409fb4eSMark Brown { "Right Line Mux", "PGA", "Right PGA Mux" }, 3345409fb4eSMark Brown { "Right Line Mux", "Differential", "Differential Mux" }, 3355409fb4eSMark Brown 3365409fb4eSMark Brown { "Left PGA Mux", "Line 1", "LINPUT1" }, 3375409fb4eSMark Brown { "Left PGA Mux", "Line 2", "LINPUT2" }, 3385409fb4eSMark Brown { "Left PGA Mux", "Differential", "Differential Mux" }, 3395409fb4eSMark Brown 3405409fb4eSMark Brown { "Right PGA Mux", "Line 1", "RINPUT1" }, 3415409fb4eSMark Brown { "Right PGA Mux", "Line 2", "RINPUT2" }, 3425409fb4eSMark Brown { "Right PGA Mux", "Differential", "Differential Mux" }, 3435409fb4eSMark Brown 3445409fb4eSMark Brown { "Differential Mux", "Line 1", "LINPUT1" }, 3455409fb4eSMark Brown { "Differential Mux", "Line 1", "RINPUT1" }, 3465409fb4eSMark Brown { "Differential Mux", "Line 2", "LINPUT2" }, 3475409fb4eSMark Brown { "Differential Mux", "Line 2", "RINPUT2" }, 3485409fb4eSMark Brown 3495409fb4eSMark Brown { "Left ADC Mux", "Stereo", "Left PGA Mux" }, 3505409fb4eSMark Brown { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" }, 3515409fb4eSMark Brown { "Left ADC Mux", "Digital Mono", "Left PGA Mux" }, 3525409fb4eSMark Brown 3535409fb4eSMark Brown { "Right ADC Mux", "Stereo", "Right PGA Mux" }, 3545409fb4eSMark Brown { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" }, 3555409fb4eSMark Brown { "Right ADC Mux", "Digital Mono", "Right PGA Mux" }, 3565409fb4eSMark Brown 3575409fb4eSMark Brown { "Left ADC", NULL, "Left ADC Mux" }, 3585409fb4eSMark Brown { "Right ADC", NULL, "Right ADC Mux" }, 3595409fb4eSMark Brown 3605409fb4eSMark Brown { "Left Line Mux", "Line 1", "LINPUT1" }, 3615409fb4eSMark Brown { "Left Line Mux", "Line 2", "LINPUT2" }, 3625409fb4eSMark Brown { "Left Line Mux", "PGA", "Left PGA Mux" }, 3635409fb4eSMark Brown { "Left Line Mux", "Differential", "Differential Mux" }, 3645409fb4eSMark Brown 3655409fb4eSMark Brown { "Right Line Mux", "Line 1", "RINPUT1" }, 3665409fb4eSMark Brown { "Right Line Mux", "Line 2", "RINPUT2" }, 3675409fb4eSMark Brown { "Right Line Mux", "PGA", "Right PGA Mux" }, 3685409fb4eSMark Brown { "Right Line Mux", "Differential", "Differential Mux" }, 3695409fb4eSMark Brown 3705409fb4eSMark Brown { "Left Mixer", "Playback Switch", "Left DAC" }, 3715409fb4eSMark Brown { "Left Mixer", "Left Bypass Switch", "Left Line Mux" }, 3725409fb4eSMark Brown { "Left Mixer", "Right Playback Switch", "Right DAC" }, 3735409fb4eSMark Brown { "Left Mixer", "Right Bypass Switch", "Right Line Mux" }, 3745409fb4eSMark Brown 3755409fb4eSMark Brown { "Right Mixer", "Left Playback Switch", "Left DAC" }, 3765409fb4eSMark Brown { "Right Mixer", "Left Bypass Switch", "Left Line Mux" }, 3775409fb4eSMark Brown { "Right Mixer", "Playback Switch", "Right DAC" }, 3785409fb4eSMark Brown { "Right Mixer", "Right Bypass Switch", "Right Line Mux" }, 3795409fb4eSMark Brown 3805409fb4eSMark Brown { "Left Out 1", NULL, "Left Mixer" }, 3815409fb4eSMark Brown { "LOUT1", NULL, "Left Out 1" }, 3825409fb4eSMark Brown { "Right Out 1", NULL, "Right Mixer" }, 3835409fb4eSMark Brown { "ROUT1", NULL, "Right Out 1" }, 3845409fb4eSMark Brown 3855409fb4eSMark Brown { "Left Out 2", NULL, "Left Mixer" }, 3865409fb4eSMark Brown { "LOUT2", NULL, "Left Out 2" }, 3875409fb4eSMark Brown { "Right Out 2", NULL, "Right Mixer" }, 3885409fb4eSMark Brown { "ROUT2", NULL, "Right Out 2" }, 3895409fb4eSMark Brown }; 3905409fb4eSMark Brown 3915409fb4eSMark Brown struct _coeff_div { 3925409fb4eSMark Brown u32 mclk; 3935409fb4eSMark Brown u32 rate; 3945409fb4eSMark Brown u16 fs; 3955409fb4eSMark Brown u8 sr:5; 3965409fb4eSMark Brown u8 usb:1; 3975409fb4eSMark Brown }; 3985409fb4eSMark Brown 3995409fb4eSMark Brown /* codec hifi mclk clock divider coefficients */ 4005409fb4eSMark Brown static const struct _coeff_div coeff_div[] = { 4015409fb4eSMark Brown /* 8k */ 4025409fb4eSMark Brown {12288000, 8000, 1536, 0x6, 0x0}, 4035409fb4eSMark Brown {11289600, 8000, 1408, 0x16, 0x0}, 4045409fb4eSMark Brown {18432000, 8000, 2304, 0x7, 0x0}, 4055409fb4eSMark Brown {16934400, 8000, 2112, 0x17, 0x0}, 4065409fb4eSMark Brown {12000000, 8000, 1500, 0x6, 0x1}, 4075409fb4eSMark Brown 4085409fb4eSMark Brown /* 11.025k */ 4095409fb4eSMark Brown {11289600, 11025, 1024, 0x18, 0x0}, 4105409fb4eSMark Brown {16934400, 11025, 1536, 0x19, 0x0}, 4115409fb4eSMark Brown {12000000, 11025, 1088, 0x19, 0x1}, 4125409fb4eSMark Brown 4135409fb4eSMark Brown /* 16k */ 4145409fb4eSMark Brown {12288000, 16000, 768, 0xa, 0x0}, 4155409fb4eSMark Brown {18432000, 16000, 1152, 0xb, 0x0}, 4165409fb4eSMark Brown {12000000, 16000, 750, 0xa, 0x1}, 4175409fb4eSMark Brown 4185409fb4eSMark Brown /* 22.05k */ 4195409fb4eSMark Brown {11289600, 22050, 512, 0x1a, 0x0}, 4205409fb4eSMark Brown {16934400, 22050, 768, 0x1b, 0x0}, 4215409fb4eSMark Brown {12000000, 22050, 544, 0x1b, 0x1}, 4225409fb4eSMark Brown 4235409fb4eSMark Brown /* 32k */ 4245409fb4eSMark Brown {12288000, 32000, 384, 0xc, 0x0}, 4255409fb4eSMark Brown {18432000, 32000, 576, 0xd, 0x0}, 4265409fb4eSMark Brown {12000000, 32000, 375, 0xa, 0x1}, 4275409fb4eSMark Brown 4285409fb4eSMark Brown /* 44.1k */ 4295409fb4eSMark Brown {11289600, 44100, 256, 0x10, 0x0}, 4305409fb4eSMark Brown {16934400, 44100, 384, 0x11, 0x0}, 4315409fb4eSMark Brown {12000000, 44100, 272, 0x11, 0x1}, 4325409fb4eSMark Brown 4335409fb4eSMark Brown /* 48k */ 4345409fb4eSMark Brown {12288000, 48000, 256, 0x0, 0x0}, 4355409fb4eSMark Brown {18432000, 48000, 384, 0x1, 0x0}, 4365409fb4eSMark Brown {12000000, 48000, 250, 0x0, 0x1}, 4375409fb4eSMark Brown 4385409fb4eSMark Brown /* 88.2k */ 4395409fb4eSMark Brown {11289600, 88200, 128, 0x1e, 0x0}, 4405409fb4eSMark Brown {16934400, 88200, 192, 0x1f, 0x0}, 4415409fb4eSMark Brown {12000000, 88200, 136, 0x1f, 0x1}, 4425409fb4eSMark Brown 4435409fb4eSMark Brown /* 96k */ 4445409fb4eSMark Brown {12288000, 96000, 128, 0xe, 0x0}, 4455409fb4eSMark Brown {18432000, 96000, 192, 0xf, 0x0}, 4465409fb4eSMark Brown {12000000, 96000, 125, 0xe, 0x1}, 4475409fb4eSMark Brown }; 4485409fb4eSMark Brown 4495409fb4eSMark Brown static inline int get_coeff(int mclk, int rate) 4505409fb4eSMark Brown { 4515409fb4eSMark Brown int i; 4525409fb4eSMark Brown 4535409fb4eSMark Brown for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 4545409fb4eSMark Brown if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 4555409fb4eSMark Brown return i; 4565409fb4eSMark Brown } 4575409fb4eSMark Brown 4585409fb4eSMark Brown return -EINVAL; 4595409fb4eSMark Brown } 4605409fb4eSMark Brown 4615409fb4eSMark Brown /* The set of rates we can generate from the above for each SYSCLK */ 4625409fb4eSMark Brown 4635409fb4eSMark Brown static unsigned int rates_12288[] = { 4645409fb4eSMark Brown 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000, 4655409fb4eSMark Brown }; 4665409fb4eSMark Brown 4675409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_12288 = { 4685409fb4eSMark Brown .count = ARRAY_SIZE(rates_12288), 4695409fb4eSMark Brown .list = rates_12288, 4705409fb4eSMark Brown }; 4715409fb4eSMark Brown 4725409fb4eSMark Brown static unsigned int rates_112896[] = { 4735409fb4eSMark Brown 8000, 11025, 22050, 44100, 4745409fb4eSMark Brown }; 4755409fb4eSMark Brown 4765409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_112896 = { 4775409fb4eSMark Brown .count = ARRAY_SIZE(rates_112896), 4785409fb4eSMark Brown .list = rates_112896, 4795409fb4eSMark Brown }; 4805409fb4eSMark Brown 4815409fb4eSMark Brown static unsigned int rates_12[] = { 4825409fb4eSMark Brown 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000, 4835409fb4eSMark Brown 48000, 88235, 96000, 4845409fb4eSMark Brown }; 4855409fb4eSMark Brown 4865409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_12 = { 4875409fb4eSMark Brown .count = ARRAY_SIZE(rates_12), 4885409fb4eSMark Brown .list = rates_12, 4895409fb4eSMark Brown }; 4905409fb4eSMark Brown 4915409fb4eSMark Brown /* 4925409fb4eSMark Brown * Note that this should be called from init rather than from hw_params. 4935409fb4eSMark Brown */ 4945409fb4eSMark Brown static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai, 4955409fb4eSMark Brown int clk_id, unsigned int freq, int dir) 4965409fb4eSMark Brown { 4975409fb4eSMark Brown struct snd_soc_codec *codec = codec_dai->codec; 4985409fb4eSMark Brown struct wm8988_priv *wm8988 = codec->private_data; 4995409fb4eSMark Brown 5005409fb4eSMark Brown switch (freq) { 5015409fb4eSMark Brown case 11289600: 5025409fb4eSMark Brown case 18432000: 5035409fb4eSMark Brown case 22579200: 5045409fb4eSMark Brown case 36864000: 5055409fb4eSMark Brown wm8988->sysclk_constraints = &constraints_112896; 5065409fb4eSMark Brown wm8988->sysclk = freq; 5075409fb4eSMark Brown return 0; 5085409fb4eSMark Brown 5095409fb4eSMark Brown case 12288000: 5105409fb4eSMark Brown case 16934400: 5115409fb4eSMark Brown case 24576000: 5125409fb4eSMark Brown case 33868800: 5135409fb4eSMark Brown wm8988->sysclk_constraints = &constraints_12288; 5145409fb4eSMark Brown wm8988->sysclk = freq; 5155409fb4eSMark Brown return 0; 5165409fb4eSMark Brown 5175409fb4eSMark Brown case 12000000: 5185409fb4eSMark Brown case 24000000: 5195409fb4eSMark Brown wm8988->sysclk_constraints = &constraints_12; 5205409fb4eSMark Brown wm8988->sysclk = freq; 5215409fb4eSMark Brown return 0; 5225409fb4eSMark Brown } 5235409fb4eSMark Brown return -EINVAL; 5245409fb4eSMark Brown } 5255409fb4eSMark Brown 5265409fb4eSMark Brown static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai, 5275409fb4eSMark Brown unsigned int fmt) 5285409fb4eSMark Brown { 5295409fb4eSMark Brown struct snd_soc_codec *codec = codec_dai->codec; 5305409fb4eSMark Brown u16 iface = 0; 5315409fb4eSMark Brown 5325409fb4eSMark Brown /* set master/slave audio interface */ 5335409fb4eSMark Brown switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 5345409fb4eSMark Brown case SND_SOC_DAIFMT_CBM_CFM: 5355409fb4eSMark Brown iface = 0x0040; 5365409fb4eSMark Brown break; 5375409fb4eSMark Brown case SND_SOC_DAIFMT_CBS_CFS: 5385409fb4eSMark Brown break; 5395409fb4eSMark Brown default: 5405409fb4eSMark Brown return -EINVAL; 5415409fb4eSMark Brown } 5425409fb4eSMark Brown 5435409fb4eSMark Brown /* interface format */ 5445409fb4eSMark Brown switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 5455409fb4eSMark Brown case SND_SOC_DAIFMT_I2S: 5465409fb4eSMark Brown iface |= 0x0002; 5475409fb4eSMark Brown break; 5485409fb4eSMark Brown case SND_SOC_DAIFMT_RIGHT_J: 5495409fb4eSMark Brown break; 5505409fb4eSMark Brown case SND_SOC_DAIFMT_LEFT_J: 5515409fb4eSMark Brown iface |= 0x0001; 5525409fb4eSMark Brown break; 5535409fb4eSMark Brown case SND_SOC_DAIFMT_DSP_A: 5545409fb4eSMark Brown iface |= 0x0003; 5555409fb4eSMark Brown break; 5565409fb4eSMark Brown case SND_SOC_DAIFMT_DSP_B: 5575409fb4eSMark Brown iface |= 0x0013; 5585409fb4eSMark Brown break; 5595409fb4eSMark Brown default: 5605409fb4eSMark Brown return -EINVAL; 5615409fb4eSMark Brown } 5625409fb4eSMark Brown 5635409fb4eSMark Brown /* clock inversion */ 5645409fb4eSMark Brown switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 5655409fb4eSMark Brown case SND_SOC_DAIFMT_NB_NF: 5665409fb4eSMark Brown break; 5675409fb4eSMark Brown case SND_SOC_DAIFMT_IB_IF: 5685409fb4eSMark Brown iface |= 0x0090; 5695409fb4eSMark Brown break; 5705409fb4eSMark Brown case SND_SOC_DAIFMT_IB_NF: 5715409fb4eSMark Brown iface |= 0x0080; 5725409fb4eSMark Brown break; 5735409fb4eSMark Brown case SND_SOC_DAIFMT_NB_IF: 5745409fb4eSMark Brown iface |= 0x0010; 5755409fb4eSMark Brown break; 5765409fb4eSMark Brown default: 5775409fb4eSMark Brown return -EINVAL; 5785409fb4eSMark Brown } 5795409fb4eSMark Brown 58017a52fd6SMark Brown snd_soc_write(codec, WM8988_IFACE, iface); 5815409fb4eSMark Brown return 0; 5825409fb4eSMark Brown } 5835409fb4eSMark Brown 5845409fb4eSMark Brown static int wm8988_pcm_startup(struct snd_pcm_substream *substream, 5855409fb4eSMark Brown struct snd_soc_dai *dai) 5865409fb4eSMark Brown { 5875409fb4eSMark Brown struct snd_soc_codec *codec = dai->codec; 5885409fb4eSMark Brown struct wm8988_priv *wm8988 = codec->private_data; 5895409fb4eSMark Brown 5905409fb4eSMark Brown /* The set of sample rates that can be supported depends on the 5915409fb4eSMark Brown * MCLK supplied to the CODEC - enforce this. 5925409fb4eSMark Brown */ 5935409fb4eSMark Brown if (!wm8988->sysclk) { 5945409fb4eSMark Brown dev_err(codec->dev, 5955409fb4eSMark Brown "No MCLK configured, call set_sysclk() on init\n"); 5965409fb4eSMark Brown return -EINVAL; 5975409fb4eSMark Brown } 5985409fb4eSMark Brown 5995409fb4eSMark Brown snd_pcm_hw_constraint_list(substream->runtime, 0, 6005409fb4eSMark Brown SNDRV_PCM_HW_PARAM_RATE, 6015409fb4eSMark Brown wm8988->sysclk_constraints); 6025409fb4eSMark Brown 6035409fb4eSMark Brown return 0; 6045409fb4eSMark Brown } 6055409fb4eSMark Brown 6065409fb4eSMark Brown static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream, 6075409fb4eSMark Brown struct snd_pcm_hw_params *params, 6085409fb4eSMark Brown struct snd_soc_dai *dai) 6095409fb4eSMark Brown { 6105409fb4eSMark Brown struct snd_soc_pcm_runtime *rtd = substream->private_data; 6115409fb4eSMark Brown struct snd_soc_device *socdev = rtd->socdev; 6125409fb4eSMark Brown struct snd_soc_codec *codec = socdev->card->codec; 6135409fb4eSMark Brown struct wm8988_priv *wm8988 = codec->private_data; 61417a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8988_IFACE) & 0x1f3; 61517a52fd6SMark Brown u16 srate = snd_soc_read(codec, WM8988_SRATE) & 0x180; 6165409fb4eSMark Brown int coeff; 6175409fb4eSMark Brown 6185409fb4eSMark Brown coeff = get_coeff(wm8988->sysclk, params_rate(params)); 6195409fb4eSMark Brown if (coeff < 0) { 6205409fb4eSMark Brown coeff = get_coeff(wm8988->sysclk / 2, params_rate(params)); 6215409fb4eSMark Brown srate |= 0x40; 6225409fb4eSMark Brown } 6235409fb4eSMark Brown if (coeff < 0) { 6245409fb4eSMark Brown dev_err(codec->dev, 6255409fb4eSMark Brown "Unable to configure sample rate %dHz with %dHz MCLK\n", 6265409fb4eSMark Brown params_rate(params), wm8988->sysclk); 6275409fb4eSMark Brown return coeff; 6285409fb4eSMark Brown } 6295409fb4eSMark Brown 6305409fb4eSMark Brown /* bit size */ 6315409fb4eSMark Brown switch (params_format(params)) { 6325409fb4eSMark Brown case SNDRV_PCM_FORMAT_S16_LE: 6335409fb4eSMark Brown break; 6345409fb4eSMark Brown case SNDRV_PCM_FORMAT_S20_3LE: 6355409fb4eSMark Brown iface |= 0x0004; 6365409fb4eSMark Brown break; 6375409fb4eSMark Brown case SNDRV_PCM_FORMAT_S24_LE: 6385409fb4eSMark Brown iface |= 0x0008; 6395409fb4eSMark Brown break; 6405409fb4eSMark Brown case SNDRV_PCM_FORMAT_S32_LE: 6415409fb4eSMark Brown iface |= 0x000c; 6425409fb4eSMark Brown break; 6435409fb4eSMark Brown } 6445409fb4eSMark Brown 6455409fb4eSMark Brown /* set iface & srate */ 64617a52fd6SMark Brown snd_soc_write(codec, WM8988_IFACE, iface); 6475409fb4eSMark Brown if (coeff >= 0) 64817a52fd6SMark Brown snd_soc_write(codec, WM8988_SRATE, srate | 6495409fb4eSMark Brown (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb); 6505409fb4eSMark Brown 6515409fb4eSMark Brown return 0; 6525409fb4eSMark Brown } 6535409fb4eSMark Brown 6545409fb4eSMark Brown static int wm8988_mute(struct snd_soc_dai *dai, int mute) 6555409fb4eSMark Brown { 6565409fb4eSMark Brown struct snd_soc_codec *codec = dai->codec; 65717a52fd6SMark Brown u16 mute_reg = snd_soc_read(codec, WM8988_ADCDAC) & 0xfff7; 6585409fb4eSMark Brown 6595409fb4eSMark Brown if (mute) 66017a52fd6SMark Brown snd_soc_write(codec, WM8988_ADCDAC, mute_reg | 0x8); 6615409fb4eSMark Brown else 66217a52fd6SMark Brown snd_soc_write(codec, WM8988_ADCDAC, mute_reg); 6635409fb4eSMark Brown return 0; 6645409fb4eSMark Brown } 6655409fb4eSMark Brown 6665409fb4eSMark Brown static int wm8988_set_bias_level(struct snd_soc_codec *codec, 6675409fb4eSMark Brown enum snd_soc_bias_level level) 6685409fb4eSMark Brown { 66917a52fd6SMark Brown u16 pwr_reg = snd_soc_read(codec, WM8988_PWR1) & ~0x1c1; 6705409fb4eSMark Brown 6715409fb4eSMark Brown switch (level) { 6725409fb4eSMark Brown case SND_SOC_BIAS_ON: 6735409fb4eSMark Brown break; 6745409fb4eSMark Brown 6755409fb4eSMark Brown case SND_SOC_BIAS_PREPARE: 6765409fb4eSMark Brown /* VREF, VMID=2x50k, digital enabled */ 67717a52fd6SMark Brown snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x00c0); 6785409fb4eSMark Brown break; 6795409fb4eSMark Brown 6805409fb4eSMark Brown case SND_SOC_BIAS_STANDBY: 6815409fb4eSMark Brown if (codec->bias_level == SND_SOC_BIAS_OFF) { 6825409fb4eSMark Brown /* VREF, VMID=2x5k */ 68317a52fd6SMark Brown snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x1c1); 6845409fb4eSMark Brown 6855409fb4eSMark Brown /* Charge caps */ 6865409fb4eSMark Brown msleep(100); 6875409fb4eSMark Brown } 6885409fb4eSMark Brown 6895409fb4eSMark Brown /* VREF, VMID=2*500k, digital stopped */ 69017a52fd6SMark Brown snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x0141); 6915409fb4eSMark Brown break; 6925409fb4eSMark Brown 6935409fb4eSMark Brown case SND_SOC_BIAS_OFF: 69417a52fd6SMark Brown snd_soc_write(codec, WM8988_PWR1, 0x0000); 6955409fb4eSMark Brown break; 6965409fb4eSMark Brown } 6975409fb4eSMark Brown codec->bias_level = level; 6985409fb4eSMark Brown return 0; 6995409fb4eSMark Brown } 7005409fb4eSMark Brown 7015409fb4eSMark Brown #define WM8988_RATES SNDRV_PCM_RATE_8000_96000 7025409fb4eSMark Brown 7035409fb4eSMark Brown #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 7045409fb4eSMark Brown SNDRV_PCM_FMTBIT_S24_LE) 7055409fb4eSMark Brown 7065409fb4eSMark Brown static struct snd_soc_dai_ops wm8988_ops = { 7075409fb4eSMark Brown .startup = wm8988_pcm_startup, 7085409fb4eSMark Brown .hw_params = wm8988_pcm_hw_params, 7095409fb4eSMark Brown .set_fmt = wm8988_set_dai_fmt, 7105409fb4eSMark Brown .set_sysclk = wm8988_set_dai_sysclk, 7115409fb4eSMark Brown .digital_mute = wm8988_mute, 7125409fb4eSMark Brown }; 7135409fb4eSMark Brown 7145409fb4eSMark Brown struct snd_soc_dai wm8988_dai = { 7155409fb4eSMark Brown .name = "WM8988", 7165409fb4eSMark Brown .playback = { 7175409fb4eSMark Brown .stream_name = "Playback", 7185409fb4eSMark Brown .channels_min = 1, 7195409fb4eSMark Brown .channels_max = 2, 7205409fb4eSMark Brown .rates = WM8988_RATES, 7215409fb4eSMark Brown .formats = WM8988_FORMATS, 7225409fb4eSMark Brown }, 7235409fb4eSMark Brown .capture = { 7245409fb4eSMark Brown .stream_name = "Capture", 7255409fb4eSMark Brown .channels_min = 1, 7265409fb4eSMark Brown .channels_max = 2, 7275409fb4eSMark Brown .rates = WM8988_RATES, 7285409fb4eSMark Brown .formats = WM8988_FORMATS, 7295409fb4eSMark Brown }, 7305409fb4eSMark Brown .ops = &wm8988_ops, 7315409fb4eSMark Brown .symmetric_rates = 1, 7325409fb4eSMark Brown }; 7335409fb4eSMark Brown EXPORT_SYMBOL_GPL(wm8988_dai); 7345409fb4eSMark Brown 7355409fb4eSMark Brown static int wm8988_suspend(struct platform_device *pdev, pm_message_t state) 7365409fb4eSMark Brown { 7375409fb4eSMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 7385409fb4eSMark Brown struct snd_soc_codec *codec = socdev->card->codec; 7395409fb4eSMark Brown 7405409fb4eSMark Brown wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF); 7415409fb4eSMark Brown return 0; 7425409fb4eSMark Brown } 7435409fb4eSMark Brown 7445409fb4eSMark Brown static int wm8988_resume(struct platform_device *pdev) 7455409fb4eSMark Brown { 7465409fb4eSMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 7475409fb4eSMark Brown struct snd_soc_codec *codec = socdev->card->codec; 7485409fb4eSMark Brown int i; 7495409fb4eSMark Brown u8 data[2]; 7505409fb4eSMark Brown u16 *cache = codec->reg_cache; 7515409fb4eSMark Brown 7525409fb4eSMark Brown /* Sync reg_cache with the hardware */ 7535409fb4eSMark Brown for (i = 0; i < WM8988_NUM_REG; i++) { 7545409fb4eSMark Brown if (i == WM8988_RESET) 7555409fb4eSMark Brown continue; 7565409fb4eSMark Brown data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); 7575409fb4eSMark Brown data[1] = cache[i] & 0x00ff; 7585409fb4eSMark Brown codec->hw_write(codec->control_data, data, 2); 7595409fb4eSMark Brown } 7605409fb4eSMark Brown 7615409fb4eSMark Brown wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 7625409fb4eSMark Brown 7635409fb4eSMark Brown return 0; 7645409fb4eSMark Brown } 7655409fb4eSMark Brown 7665409fb4eSMark Brown static struct snd_soc_codec *wm8988_codec; 7675409fb4eSMark Brown 7685409fb4eSMark Brown static int wm8988_probe(struct platform_device *pdev) 7695409fb4eSMark Brown { 7705409fb4eSMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 7715409fb4eSMark Brown struct snd_soc_codec *codec; 7725409fb4eSMark Brown int ret = 0; 7735409fb4eSMark Brown 7745409fb4eSMark Brown if (wm8988_codec == NULL) { 7755409fb4eSMark Brown dev_err(&pdev->dev, "Codec device not registered\n"); 7765409fb4eSMark Brown return -ENODEV; 7775409fb4eSMark Brown } 7785409fb4eSMark Brown 7795409fb4eSMark Brown socdev->card->codec = wm8988_codec; 7805409fb4eSMark Brown codec = wm8988_codec; 7815409fb4eSMark Brown 7825409fb4eSMark Brown /* register pcms */ 7835409fb4eSMark Brown ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 7845409fb4eSMark Brown if (ret < 0) { 7855409fb4eSMark Brown dev_err(codec->dev, "failed to create pcms: %d\n", ret); 7865409fb4eSMark Brown goto pcm_err; 7875409fb4eSMark Brown } 7885409fb4eSMark Brown 7895409fb4eSMark Brown snd_soc_add_controls(codec, wm8988_snd_controls, 7905409fb4eSMark Brown ARRAY_SIZE(wm8988_snd_controls)); 7915409fb4eSMark Brown snd_soc_dapm_new_controls(codec, wm8988_dapm_widgets, 7925409fb4eSMark Brown ARRAY_SIZE(wm8988_dapm_widgets)); 7935409fb4eSMark Brown snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); 7945409fb4eSMark Brown 7955409fb4eSMark Brown return ret; 7965409fb4eSMark Brown 7975409fb4eSMark Brown pcm_err: 7985409fb4eSMark Brown return ret; 7995409fb4eSMark Brown } 8005409fb4eSMark Brown 8015409fb4eSMark Brown static int wm8988_remove(struct platform_device *pdev) 8025409fb4eSMark Brown { 8035409fb4eSMark Brown struct snd_soc_device *socdev = platform_get_drvdata(pdev); 8045409fb4eSMark Brown 8055409fb4eSMark Brown snd_soc_free_pcms(socdev); 8065409fb4eSMark Brown snd_soc_dapm_free(socdev); 8075409fb4eSMark Brown 8085409fb4eSMark Brown return 0; 8095409fb4eSMark Brown } 8105409fb4eSMark Brown 8115409fb4eSMark Brown struct snd_soc_codec_device soc_codec_dev_wm8988 = { 8125409fb4eSMark Brown .probe = wm8988_probe, 8135409fb4eSMark Brown .remove = wm8988_remove, 8145409fb4eSMark Brown .suspend = wm8988_suspend, 8155409fb4eSMark Brown .resume = wm8988_resume, 8165409fb4eSMark Brown }; 8175409fb4eSMark Brown EXPORT_SYMBOL_GPL(soc_codec_dev_wm8988); 8185409fb4eSMark Brown 8197084a42bSMark Brown static int wm8988_register(struct wm8988_priv *wm8988, 8207084a42bSMark Brown enum snd_soc_control_type control) 8215409fb4eSMark Brown { 8225409fb4eSMark Brown struct snd_soc_codec *codec = &wm8988->codec; 8235409fb4eSMark Brown int ret; 8245409fb4eSMark Brown u16 reg; 8255409fb4eSMark Brown 8265409fb4eSMark Brown if (wm8988_codec) { 8275409fb4eSMark Brown dev_err(codec->dev, "Another WM8988 is registered\n"); 8285409fb4eSMark Brown ret = -EINVAL; 8295409fb4eSMark Brown goto err; 8305409fb4eSMark Brown } 8315409fb4eSMark Brown 8325409fb4eSMark Brown mutex_init(&codec->mutex); 8335409fb4eSMark Brown INIT_LIST_HEAD(&codec->dapm_widgets); 8345409fb4eSMark Brown INIT_LIST_HEAD(&codec->dapm_paths); 8355409fb4eSMark Brown 8365409fb4eSMark Brown codec->private_data = wm8988; 8375409fb4eSMark Brown codec->name = "WM8988"; 8385409fb4eSMark Brown codec->owner = THIS_MODULE; 8395409fb4eSMark Brown codec->dai = &wm8988_dai; 8405409fb4eSMark Brown codec->num_dai = 1; 8415409fb4eSMark Brown codec->reg_cache_size = ARRAY_SIZE(wm8988->reg_cache); 8425409fb4eSMark Brown codec->reg_cache = &wm8988->reg_cache; 8435409fb4eSMark Brown codec->bias_level = SND_SOC_BIAS_OFF; 8445409fb4eSMark Brown codec->set_bias_level = wm8988_set_bias_level; 8455409fb4eSMark Brown 8465409fb4eSMark Brown memcpy(codec->reg_cache, wm8988_reg, 8475409fb4eSMark Brown sizeof(wm8988_reg)); 8485409fb4eSMark Brown 8497084a42bSMark Brown ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); 85017a52fd6SMark Brown if (ret < 0) { 85117a52fd6SMark Brown dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 85217a52fd6SMark Brown goto err; 85317a52fd6SMark Brown } 85417a52fd6SMark Brown 8555409fb4eSMark Brown ret = wm8988_reset(codec); 8565409fb4eSMark Brown if (ret < 0) { 8575409fb4eSMark Brown dev_err(codec->dev, "Failed to issue reset\n"); 8585420f307SMark Brown goto err; 8595409fb4eSMark Brown } 8605409fb4eSMark Brown 8615409fb4eSMark Brown /* set the update bits (we always update left then right) */ 86217a52fd6SMark Brown reg = snd_soc_read(codec, WM8988_RADC); 86317a52fd6SMark Brown snd_soc_write(codec, WM8988_RADC, reg | 0x100); 86417a52fd6SMark Brown reg = snd_soc_read(codec, WM8988_RDAC); 86517a52fd6SMark Brown snd_soc_write(codec, WM8988_RDAC, reg | 0x0100); 86617a52fd6SMark Brown reg = snd_soc_read(codec, WM8988_ROUT1V); 86717a52fd6SMark Brown snd_soc_write(codec, WM8988_ROUT1V, reg | 0x0100); 86817a52fd6SMark Brown reg = snd_soc_read(codec, WM8988_ROUT2V); 86917a52fd6SMark Brown snd_soc_write(codec, WM8988_ROUT2V, reg | 0x0100); 87017a52fd6SMark Brown reg = snd_soc_read(codec, WM8988_RINVOL); 87117a52fd6SMark Brown snd_soc_write(codec, WM8988_RINVOL, reg | 0x0100); 8725409fb4eSMark Brown 8735409fb4eSMark Brown wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_STANDBY); 8745409fb4eSMark Brown 8755409fb4eSMark Brown wm8988_dai.dev = codec->dev; 8765409fb4eSMark Brown 8775409fb4eSMark Brown wm8988_codec = codec; 8785409fb4eSMark Brown 8795409fb4eSMark Brown ret = snd_soc_register_codec(codec); 8805409fb4eSMark Brown if (ret != 0) { 8815409fb4eSMark Brown dev_err(codec->dev, "Failed to register codec: %d\n", ret); 8825420f307SMark Brown goto err; 8835409fb4eSMark Brown } 8845409fb4eSMark Brown 8855409fb4eSMark Brown ret = snd_soc_register_dai(&wm8988_dai); 8865409fb4eSMark Brown if (ret != 0) { 8875409fb4eSMark Brown dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 8885409fb4eSMark Brown snd_soc_unregister_codec(codec); 8895420f307SMark Brown goto err_codec; 8905409fb4eSMark Brown } 8915409fb4eSMark Brown 8925409fb4eSMark Brown return 0; 8935409fb4eSMark Brown 8945420f307SMark Brown err_codec: 8955420f307SMark Brown snd_soc_unregister_codec(codec); 8965409fb4eSMark Brown err: 8975409fb4eSMark Brown kfree(wm8988); 8985409fb4eSMark Brown return ret; 8995409fb4eSMark Brown } 9005409fb4eSMark Brown 9015409fb4eSMark Brown static void wm8988_unregister(struct wm8988_priv *wm8988) 9025409fb4eSMark Brown { 9035409fb4eSMark Brown wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_OFF); 9045409fb4eSMark Brown snd_soc_unregister_dai(&wm8988_dai); 9055409fb4eSMark Brown snd_soc_unregister_codec(&wm8988->codec); 9065409fb4eSMark Brown kfree(wm8988); 9075409fb4eSMark Brown wm8988_codec = NULL; 9085409fb4eSMark Brown } 9095409fb4eSMark Brown 9105409fb4eSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 9115409fb4eSMark Brown static int wm8988_i2c_probe(struct i2c_client *i2c, 9125409fb4eSMark Brown const struct i2c_device_id *id) 9135409fb4eSMark Brown { 9145409fb4eSMark Brown struct wm8988_priv *wm8988; 9155409fb4eSMark Brown struct snd_soc_codec *codec; 9165409fb4eSMark Brown 9175409fb4eSMark Brown wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL); 9185409fb4eSMark Brown if (wm8988 == NULL) 9195409fb4eSMark Brown return -ENOMEM; 9205409fb4eSMark Brown 9215409fb4eSMark Brown codec = &wm8988->codec; 9225409fb4eSMark Brown 9235409fb4eSMark Brown i2c_set_clientdata(i2c, wm8988); 9245409fb4eSMark Brown codec->control_data = i2c; 9255409fb4eSMark Brown 9265409fb4eSMark Brown codec->dev = &i2c->dev; 9275409fb4eSMark Brown 9287084a42bSMark Brown return wm8988_register(wm8988, SND_SOC_I2C); 9295409fb4eSMark Brown } 9305409fb4eSMark Brown 9315409fb4eSMark Brown static int wm8988_i2c_remove(struct i2c_client *client) 9325409fb4eSMark Brown { 9335409fb4eSMark Brown struct wm8988_priv *wm8988 = i2c_get_clientdata(client); 9345409fb4eSMark Brown wm8988_unregister(wm8988); 9355409fb4eSMark Brown return 0; 9365409fb4eSMark Brown } 9375409fb4eSMark Brown 9385409fb4eSMark Brown static const struct i2c_device_id wm8988_i2c_id[] = { 9395409fb4eSMark Brown { "wm8988", 0 }, 9405409fb4eSMark Brown { } 9415409fb4eSMark Brown }; 9425409fb4eSMark Brown MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id); 9435409fb4eSMark Brown 9445409fb4eSMark Brown static struct i2c_driver wm8988_i2c_driver = { 9455409fb4eSMark Brown .driver = { 9465409fb4eSMark Brown .name = "WM8988", 9475409fb4eSMark Brown .owner = THIS_MODULE, 9485409fb4eSMark Brown }, 9495409fb4eSMark Brown .probe = wm8988_i2c_probe, 9505409fb4eSMark Brown .remove = wm8988_i2c_remove, 9515409fb4eSMark Brown .id_table = wm8988_i2c_id, 9525409fb4eSMark Brown }; 9535409fb4eSMark Brown #endif 9545409fb4eSMark Brown 9555409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER) 9565409fb4eSMark Brown static int __devinit wm8988_spi_probe(struct spi_device *spi) 9575409fb4eSMark Brown { 9585409fb4eSMark Brown struct wm8988_priv *wm8988; 9595409fb4eSMark Brown struct snd_soc_codec *codec; 9605409fb4eSMark Brown 9615409fb4eSMark Brown wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL); 9625409fb4eSMark Brown if (wm8988 == NULL) 9635409fb4eSMark Brown return -ENOMEM; 9645409fb4eSMark Brown 9655409fb4eSMark Brown codec = &wm8988->codec; 9665409fb4eSMark Brown codec->control_data = spi; 9675409fb4eSMark Brown codec->dev = &spi->dev; 9685409fb4eSMark Brown 969864e1e8dSGreg Kroah-Hartman dev_set_drvdata(&spi->dev, wm8988); 9705409fb4eSMark Brown 9717084a42bSMark Brown return wm8988_register(wm8988, SND_SOC_SPI); 9725409fb4eSMark Brown } 9735409fb4eSMark Brown 9745409fb4eSMark Brown static int __devexit wm8988_spi_remove(struct spi_device *spi) 9755409fb4eSMark Brown { 976864e1e8dSGreg Kroah-Hartman struct wm8988_priv *wm8988 = dev_get_drvdata(&spi->dev); 9775409fb4eSMark Brown 9785409fb4eSMark Brown wm8988_unregister(wm8988); 9795409fb4eSMark Brown 9805409fb4eSMark Brown return 0; 9815409fb4eSMark Brown } 9825409fb4eSMark Brown 9835409fb4eSMark Brown static struct spi_driver wm8988_spi_driver = { 9845409fb4eSMark Brown .driver = { 9855409fb4eSMark Brown .name = "wm8988", 9865409fb4eSMark Brown .bus = &spi_bus_type, 9875409fb4eSMark Brown .owner = THIS_MODULE, 9885409fb4eSMark Brown }, 9895409fb4eSMark Brown .probe = wm8988_spi_probe, 9905409fb4eSMark Brown .remove = __devexit_p(wm8988_spi_remove), 9915409fb4eSMark Brown }; 9925409fb4eSMark Brown #endif 9935409fb4eSMark Brown 9945409fb4eSMark Brown static int __init wm8988_modinit(void) 9955409fb4eSMark Brown { 9965409fb4eSMark Brown int ret; 9975409fb4eSMark Brown 9985409fb4eSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 9995409fb4eSMark Brown ret = i2c_add_driver(&wm8988_i2c_driver); 10005409fb4eSMark Brown if (ret != 0) 10015409fb4eSMark Brown pr_err("WM8988: Unable to register I2C driver: %d\n", ret); 10025409fb4eSMark Brown #endif 10035409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER) 10045409fb4eSMark Brown ret = spi_register_driver(&wm8988_spi_driver); 10055409fb4eSMark Brown if (ret != 0) 10065409fb4eSMark Brown pr_err("WM8988: Unable to register SPI driver: %d\n", ret); 10075409fb4eSMark Brown #endif 10085409fb4eSMark Brown return ret; 10095409fb4eSMark Brown } 10105409fb4eSMark Brown module_init(wm8988_modinit); 10115409fb4eSMark Brown 10125409fb4eSMark Brown static void __exit wm8988_exit(void) 10135409fb4eSMark Brown { 10145409fb4eSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 10155409fb4eSMark Brown i2c_del_driver(&wm8988_i2c_driver); 10165409fb4eSMark Brown #endif 10175409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER) 10185409fb4eSMark Brown spi_unregister_driver(&wm8988_spi_driver); 10195409fb4eSMark Brown #endif 10205409fb4eSMark Brown } 10215409fb4eSMark Brown module_exit(wm8988_exit); 10225409fb4eSMark Brown 10235409fb4eSMark Brown 10245409fb4eSMark Brown MODULE_DESCRIPTION("ASoC WM8988 driver"); 10255409fb4eSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 10265409fb4eSMark Brown MODULE_LICENSE("GPL"); 1027