xref: /openbmc/linux/sound/soc/codecs/wm8988.c (revision f0fba2ad1b6b53d5360125c41953b7afcd6deff0)
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>
225a0e3ad6STejun 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;
55*f0fba2adSLiam Girdwood 	enum snd_soc_control_type control_type;
56*f0fba2adSLiam Girdwood 	void *control_data;
575409fb4eSMark Brown 	struct snd_pcm_hw_constraint_list *sysclk_constraints;
585409fb4eSMark Brown 	u16 reg_cache[WM8988_NUM_REG];
595409fb4eSMark Brown };
605409fb4eSMark Brown 
615409fb4eSMark Brown 
6217a52fd6SMark Brown #define wm8988_reset(c)	snd_soc_write(c, WM8988_RESET, 0)
635409fb4eSMark Brown 
645409fb4eSMark Brown /*
655409fb4eSMark Brown  * WM8988 Controls
665409fb4eSMark Brown  */
675409fb4eSMark Brown 
685409fb4eSMark Brown static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"};
695409fb4eSMark Brown static const struct soc_enum bass_boost =
705409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_BASS, 7, 2, bass_boost_txt);
715409fb4eSMark Brown 
725409fb4eSMark Brown static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
735409fb4eSMark Brown static const struct soc_enum bass_filter =
745409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_BASS, 6, 2, bass_filter_txt);
755409fb4eSMark Brown 
765409fb4eSMark Brown static const char *treble_txt[] = {"8kHz", "4kHz"};
775409fb4eSMark Brown static const struct soc_enum treble =
785409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_TREBLE, 6, 2, treble_txt);
795409fb4eSMark Brown 
805409fb4eSMark Brown static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"};
815409fb4eSMark Brown static const struct soc_enum stereo_3d_lc =
825409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_3D, 5, 2, stereo_3d_lc_txt);
835409fb4eSMark Brown 
845409fb4eSMark Brown static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"};
855409fb4eSMark Brown static const struct soc_enum stereo_3d_uc =
865409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_3D, 6, 2, stereo_3d_uc_txt);
875409fb4eSMark Brown 
885409fb4eSMark Brown static const char *stereo_3d_func_txt[] = {"Capture", "Playback"};
895409fb4eSMark Brown static const struct soc_enum stereo_3d_func =
905409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_3D, 7, 2, stereo_3d_func_txt);
915409fb4eSMark Brown 
925409fb4eSMark Brown static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
935409fb4eSMark Brown static const struct soc_enum alc_func =
945409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_ALC1, 7, 4, alc_func_txt);
955409fb4eSMark Brown 
965409fb4eSMark Brown static const char *ng_type_txt[] = {"Constant PGA Gain",
975409fb4eSMark Brown 				    "Mute ADC Output"};
985409fb4eSMark Brown static const struct soc_enum ng_type =
995409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_NGATE, 1, 2, ng_type_txt);
1005409fb4eSMark Brown 
1015409fb4eSMark Brown static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
1025409fb4eSMark Brown static const struct soc_enum deemph =
1035409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_ADCDAC, 1, 4, deemph_txt);
1045409fb4eSMark Brown 
1055409fb4eSMark Brown static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert",
1065409fb4eSMark Brown 				   "L + R Invert"};
1075409fb4eSMark Brown static const struct soc_enum adcpol =
1085409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_ADCDAC, 5, 4, adcpol_txt);
1095409fb4eSMark Brown 
1105409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
1115409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
1125409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
1135409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
1145409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
1155409fb4eSMark Brown 
1165409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_snd_controls[] = {
1175409fb4eSMark Brown 
1185409fb4eSMark Brown SOC_ENUM("Bass Boost", bass_boost),
1195409fb4eSMark Brown SOC_ENUM("Bass Filter", bass_filter),
1205409fb4eSMark Brown SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1),
1215409fb4eSMark Brown 
1225409fb4eSMark Brown SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0),
1235409fb4eSMark Brown SOC_ENUM("Treble Cut-off", treble),
1245409fb4eSMark Brown 
1255409fb4eSMark Brown SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0),
1265409fb4eSMark Brown SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0),
1275409fb4eSMark Brown SOC_ENUM("3D Lower Cut-off", stereo_3d_lc),
1285409fb4eSMark Brown SOC_ENUM("3D Upper Cut-off", stereo_3d_uc),
1295409fb4eSMark Brown SOC_ENUM("3D Mode", stereo_3d_func),
1305409fb4eSMark Brown 
1315409fb4eSMark Brown SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0),
1325409fb4eSMark Brown SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0),
1335409fb4eSMark Brown SOC_ENUM("ALC Capture Function", alc_func),
1345409fb4eSMark Brown SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0),
1355409fb4eSMark Brown SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0),
1365409fb4eSMark Brown SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0),
1375409fb4eSMark Brown SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0),
1385409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0),
1395409fb4eSMark Brown SOC_ENUM("ALC Capture NG Type", ng_type),
1405409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0),
1415409fb4eSMark Brown 
1425409fb4eSMark Brown SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0),
1435409fb4eSMark Brown 
1445409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC,
1455409fb4eSMark Brown 		 0, 255, 0, adc_tlv),
1465409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL,
1475409fb4eSMark Brown 		 0, 63, 0, pga_tlv),
1485409fb4eSMark Brown SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0),
1495409fb4eSMark Brown SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1),
1505409fb4eSMark Brown 
1515409fb4eSMark Brown SOC_ENUM("Playback De-emphasis", deemph),
1525409fb4eSMark Brown 
1535409fb4eSMark Brown SOC_ENUM("Capture Polarity", adcpol),
1545409fb4eSMark Brown SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0),
1555409fb4eSMark Brown SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0),
1565409fb4eSMark Brown 
1575409fb4eSMark Brown SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv),
1585409fb4eSMark Brown 
1595409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1,
1605409fb4eSMark Brown 	       bypass_tlv),
1615409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1,
1625409fb4eSMark Brown 	       bypass_tlv),
1635409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1,
1645409fb4eSMark Brown 	       bypass_tlv),
1655409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1,
1665409fb4eSMark Brown 	       bypass_tlv),
1675409fb4eSMark Brown 
1685409fb4eSMark Brown SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V,
1695409fb4eSMark Brown 	     WM8988_ROUT1V, 7, 1, 0),
1705409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V,
1715409fb4eSMark Brown 		 0, 127, 0, out_tlv),
1725409fb4eSMark Brown 
1735409fb4eSMark Brown SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V,
1745409fb4eSMark Brown 	     WM8988_ROUT2V, 7, 1, 0),
1755409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V,
1765409fb4eSMark Brown 		 0, 127, 0, out_tlv),
1775409fb4eSMark Brown 
1785409fb4eSMark Brown };
1795409fb4eSMark Brown 
1805409fb4eSMark Brown /*
1815409fb4eSMark Brown  * DAPM Controls
1825409fb4eSMark Brown  */
1835409fb4eSMark Brown 
1845409fb4eSMark Brown static int wm8988_lrc_control(struct snd_soc_dapm_widget *w,
1855409fb4eSMark Brown 			      struct snd_kcontrol *kcontrol, int event)
1865409fb4eSMark Brown {
1875409fb4eSMark Brown 	struct snd_soc_codec *codec = w->codec;
18817a52fd6SMark Brown 	u16 adctl2 = snd_soc_read(codec, WM8988_ADCTL2);
1895409fb4eSMark Brown 
1905409fb4eSMark Brown 	/* Use the DAC to gate LRC if active, otherwise use ADC */
19117a52fd6SMark Brown 	if (snd_soc_read(codec, WM8988_PWR2) & 0x180)
1925409fb4eSMark Brown 		adctl2 &= ~0x4;
1935409fb4eSMark Brown 	else
1945409fb4eSMark Brown 		adctl2 |= 0x4;
1955409fb4eSMark Brown 
19617a52fd6SMark Brown 	return snd_soc_write(codec, WM8988_ADCTL2, adctl2);
1975409fb4eSMark Brown }
1985409fb4eSMark Brown 
1995409fb4eSMark Brown static const char *wm8988_line_texts[] = {
2005409fb4eSMark Brown 	"Line 1", "Line 2", "PGA", "Differential"};
2015409fb4eSMark Brown 
2025409fb4eSMark Brown static const unsigned int wm8988_line_values[] = {
2035409fb4eSMark Brown 	0, 1, 3, 4};
2045409fb4eSMark Brown 
2055409fb4eSMark Brown static const struct soc_enum wm8988_lline_enum =
2065409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7,
2075409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_line_texts),
2085409fb4eSMark Brown 			      wm8988_line_texts,
2095409fb4eSMark Brown 			      wm8988_line_values);
2105409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_line_controls =
2115409fb4eSMark Brown 	SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
2125409fb4eSMark Brown 
2135409fb4eSMark Brown static const struct soc_enum wm8988_rline_enum =
2145409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7,
2155409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_line_texts),
2165409fb4eSMark Brown 			      wm8988_line_texts,
2175409fb4eSMark Brown 			      wm8988_line_values);
2185409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_line_controls =
2195409fb4eSMark Brown 	SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
2205409fb4eSMark Brown 
2215409fb4eSMark Brown /* Left Mixer */
2225409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = {
2235409fb4eSMark Brown 	SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0),
2245409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0),
2255409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0),
2265409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0),
2275409fb4eSMark Brown };
2285409fb4eSMark Brown 
2295409fb4eSMark Brown /* Right Mixer */
2305409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = {
2315409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0),
2325409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0),
2335409fb4eSMark Brown 	SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0),
2345409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0),
2355409fb4eSMark Brown };
2365409fb4eSMark Brown 
2375409fb4eSMark Brown static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"};
2385409fb4eSMark Brown static const unsigned int wm8988_pga_val[] = { 0, 1, 3 };
2395409fb4eSMark Brown 
2405409fb4eSMark Brown /* Left PGA Mux */
2415409fb4eSMark Brown static const struct soc_enum wm8988_lpga_enum =
2425409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3,
2435409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_pga_sel),
2445409fb4eSMark Brown 			      wm8988_pga_sel,
2455409fb4eSMark Brown 			      wm8988_pga_val);
2465409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_pga_controls =
2475409fb4eSMark Brown 	SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum);
2485409fb4eSMark Brown 
2495409fb4eSMark Brown /* Right PGA Mux */
2505409fb4eSMark Brown static const struct soc_enum wm8988_rpga_enum =
2515409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3,
2525409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_pga_sel),
2535409fb4eSMark Brown 			      wm8988_pga_sel,
2545409fb4eSMark Brown 			      wm8988_pga_val);
2555409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_pga_controls =
2565409fb4eSMark Brown 	SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum);
2575409fb4eSMark Brown 
2585409fb4eSMark Brown /* Differential Mux */
2595409fb4eSMark Brown static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"};
2605409fb4eSMark Brown static const struct soc_enum diffmux =
2615409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_ADCIN, 8, 2, wm8988_diff_sel);
2625409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_diffmux_controls =
2635409fb4eSMark Brown 	SOC_DAPM_ENUM("Route", diffmux);
2645409fb4eSMark Brown 
2655409fb4eSMark Brown /* Mono ADC Mux */
2665409fb4eSMark Brown static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)",
2675409fb4eSMark Brown 	"Mono (Right)", "Digital Mono"};
2685409fb4eSMark Brown static const struct soc_enum monomux =
2695409fb4eSMark Brown 	SOC_ENUM_SINGLE(WM8988_ADCIN, 6, 4, wm8988_mono_mux);
2705409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_monomux_controls =
2715409fb4eSMark Brown 	SOC_DAPM_ENUM("Route", monomux);
2725409fb4eSMark Brown 
2735409fb4eSMark Brown static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = {
2745409fb4eSMark Brown 	SND_SOC_DAPM_MICBIAS("Mic Bias", WM8988_PWR1, 1, 0),
2755409fb4eSMark Brown 
2765409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
2775409fb4eSMark Brown 		&wm8988_diffmux_controls),
2785409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
2795409fb4eSMark Brown 		&wm8988_monomux_controls),
2805409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
2815409fb4eSMark Brown 		&wm8988_monomux_controls),
2825409fb4eSMark Brown 
2835409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0,
2845409fb4eSMark Brown 		&wm8988_left_pga_controls),
2855409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0,
2865409fb4eSMark Brown 		&wm8988_right_pga_controls),
2875409fb4eSMark Brown 
2885409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
2895409fb4eSMark Brown 		&wm8988_left_line_controls),
2905409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
2915409fb4eSMark Brown 		&wm8988_right_line_controls),
2925409fb4eSMark Brown 
2935409fb4eSMark Brown 	SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0),
2945409fb4eSMark Brown 	SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0),
2955409fb4eSMark Brown 
2965409fb4eSMark Brown 	SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0),
2975409fb4eSMark Brown 	SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0),
2985409fb4eSMark Brown 
2995409fb4eSMark Brown 	SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
3005409fb4eSMark Brown 		&wm8988_left_mixer_controls[0],
3015409fb4eSMark Brown 		ARRAY_SIZE(wm8988_left_mixer_controls)),
3025409fb4eSMark Brown 	SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
3035409fb4eSMark Brown 		&wm8988_right_mixer_controls[0],
3045409fb4eSMark Brown 		ARRAY_SIZE(wm8988_right_mixer_controls)),
3055409fb4eSMark Brown 
3065409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0),
3075409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0),
3085409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0),
3095409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0),
3105409fb4eSMark Brown 
3115409fb4eSMark Brown 	SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control),
3125409fb4eSMark Brown 
3135409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("LOUT1"),
3145409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("ROUT1"),
3155409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("LOUT2"),
3165409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("ROUT2"),
3175409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("VREF"),
3185409fb4eSMark Brown 
3195409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("LINPUT1"),
3205409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("LINPUT2"),
3215409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("RINPUT1"),
3225409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("RINPUT2"),
3235409fb4eSMark Brown };
3245409fb4eSMark Brown 
3255409fb4eSMark Brown static const struct snd_soc_dapm_route audio_map[] = {
3265409fb4eSMark Brown 
3275409fb4eSMark Brown 	{ "Left Line Mux", "Line 1", "LINPUT1" },
3285409fb4eSMark Brown 	{ "Left Line Mux", "Line 2", "LINPUT2" },
3295409fb4eSMark Brown 	{ "Left Line Mux", "PGA", "Left PGA Mux" },
3305409fb4eSMark Brown 	{ "Left Line Mux", "Differential", "Differential Mux" },
3315409fb4eSMark Brown 
3325409fb4eSMark Brown 	{ "Right Line Mux", "Line 1", "RINPUT1" },
3335409fb4eSMark Brown 	{ "Right Line Mux", "Line 2", "RINPUT2" },
3345409fb4eSMark Brown 	{ "Right Line Mux", "PGA", "Right PGA Mux" },
3355409fb4eSMark Brown 	{ "Right Line Mux", "Differential", "Differential Mux" },
3365409fb4eSMark Brown 
3375409fb4eSMark Brown 	{ "Left PGA Mux", "Line 1", "LINPUT1" },
3385409fb4eSMark Brown 	{ "Left PGA Mux", "Line 2", "LINPUT2" },
3395409fb4eSMark Brown 	{ "Left PGA Mux", "Differential", "Differential Mux" },
3405409fb4eSMark Brown 
3415409fb4eSMark Brown 	{ "Right PGA Mux", "Line 1", "RINPUT1" },
3425409fb4eSMark Brown 	{ "Right PGA Mux", "Line 2", "RINPUT2" },
3435409fb4eSMark Brown 	{ "Right PGA Mux", "Differential", "Differential Mux" },
3445409fb4eSMark Brown 
3455409fb4eSMark Brown 	{ "Differential Mux", "Line 1", "LINPUT1" },
3465409fb4eSMark Brown 	{ "Differential Mux", "Line 1", "RINPUT1" },
3475409fb4eSMark Brown 	{ "Differential Mux", "Line 2", "LINPUT2" },
3485409fb4eSMark Brown 	{ "Differential Mux", "Line 2", "RINPUT2" },
3495409fb4eSMark Brown 
3505409fb4eSMark Brown 	{ "Left ADC Mux", "Stereo", "Left PGA Mux" },
3515409fb4eSMark Brown 	{ "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
3525409fb4eSMark Brown 	{ "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
3535409fb4eSMark Brown 
3545409fb4eSMark Brown 	{ "Right ADC Mux", "Stereo", "Right PGA Mux" },
3555409fb4eSMark Brown 	{ "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
3565409fb4eSMark Brown 	{ "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
3575409fb4eSMark Brown 
3585409fb4eSMark Brown 	{ "Left ADC", NULL, "Left ADC Mux" },
3595409fb4eSMark Brown 	{ "Right ADC", NULL, "Right ADC Mux" },
3605409fb4eSMark Brown 
3615409fb4eSMark Brown 	{ "Left Line Mux", "Line 1", "LINPUT1" },
3625409fb4eSMark Brown 	{ "Left Line Mux", "Line 2", "LINPUT2" },
3635409fb4eSMark Brown 	{ "Left Line Mux", "PGA", "Left PGA Mux" },
3645409fb4eSMark Brown 	{ "Left Line Mux", "Differential", "Differential Mux" },
3655409fb4eSMark Brown 
3665409fb4eSMark Brown 	{ "Right Line Mux", "Line 1", "RINPUT1" },
3675409fb4eSMark Brown 	{ "Right Line Mux", "Line 2", "RINPUT2" },
3685409fb4eSMark Brown 	{ "Right Line Mux", "PGA", "Right PGA Mux" },
3695409fb4eSMark Brown 	{ "Right Line Mux", "Differential", "Differential Mux" },
3705409fb4eSMark Brown 
3715409fb4eSMark Brown 	{ "Left Mixer", "Playback Switch", "Left DAC" },
3725409fb4eSMark Brown 	{ "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
3735409fb4eSMark Brown 	{ "Left Mixer", "Right Playback Switch", "Right DAC" },
3745409fb4eSMark Brown 	{ "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
3755409fb4eSMark Brown 
3765409fb4eSMark Brown 	{ "Right Mixer", "Left Playback Switch", "Left DAC" },
3775409fb4eSMark Brown 	{ "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
3785409fb4eSMark Brown 	{ "Right Mixer", "Playback Switch", "Right DAC" },
3795409fb4eSMark Brown 	{ "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
3805409fb4eSMark Brown 
3815409fb4eSMark Brown 	{ "Left Out 1", NULL, "Left Mixer" },
3825409fb4eSMark Brown 	{ "LOUT1", NULL, "Left Out 1" },
3835409fb4eSMark Brown 	{ "Right Out 1", NULL, "Right Mixer" },
3845409fb4eSMark Brown 	{ "ROUT1", NULL, "Right Out 1" },
3855409fb4eSMark Brown 
3865409fb4eSMark Brown 	{ "Left Out 2", NULL, "Left Mixer" },
3875409fb4eSMark Brown 	{ "LOUT2", NULL, "Left Out 2" },
3885409fb4eSMark Brown 	{ "Right Out 2", NULL, "Right Mixer" },
3895409fb4eSMark Brown 	{ "ROUT2", NULL, "Right Out 2" },
3905409fb4eSMark Brown };
3915409fb4eSMark Brown 
3925409fb4eSMark Brown struct _coeff_div {
3935409fb4eSMark Brown 	u32 mclk;
3945409fb4eSMark Brown 	u32 rate;
3955409fb4eSMark Brown 	u16 fs;
3965409fb4eSMark Brown 	u8 sr:5;
3975409fb4eSMark Brown 	u8 usb:1;
3985409fb4eSMark Brown };
3995409fb4eSMark Brown 
4005409fb4eSMark Brown /* codec hifi mclk clock divider coefficients */
4015409fb4eSMark Brown static const struct _coeff_div coeff_div[] = {
4025409fb4eSMark Brown 	/* 8k */
4035409fb4eSMark Brown 	{12288000, 8000, 1536, 0x6, 0x0},
4045409fb4eSMark Brown 	{11289600, 8000, 1408, 0x16, 0x0},
4055409fb4eSMark Brown 	{18432000, 8000, 2304, 0x7, 0x0},
4065409fb4eSMark Brown 	{16934400, 8000, 2112, 0x17, 0x0},
4075409fb4eSMark Brown 	{12000000, 8000, 1500, 0x6, 0x1},
4085409fb4eSMark Brown 
4095409fb4eSMark Brown 	/* 11.025k */
4105409fb4eSMark Brown 	{11289600, 11025, 1024, 0x18, 0x0},
4115409fb4eSMark Brown 	{16934400, 11025, 1536, 0x19, 0x0},
4125409fb4eSMark Brown 	{12000000, 11025, 1088, 0x19, 0x1},
4135409fb4eSMark Brown 
4145409fb4eSMark Brown 	/* 16k */
4155409fb4eSMark Brown 	{12288000, 16000, 768, 0xa, 0x0},
4165409fb4eSMark Brown 	{18432000, 16000, 1152, 0xb, 0x0},
4175409fb4eSMark Brown 	{12000000, 16000, 750, 0xa, 0x1},
4185409fb4eSMark Brown 
4195409fb4eSMark Brown 	/* 22.05k */
4205409fb4eSMark Brown 	{11289600, 22050, 512, 0x1a, 0x0},
4215409fb4eSMark Brown 	{16934400, 22050, 768, 0x1b, 0x0},
4225409fb4eSMark Brown 	{12000000, 22050, 544, 0x1b, 0x1},
4235409fb4eSMark Brown 
4245409fb4eSMark Brown 	/* 32k */
4255409fb4eSMark Brown 	{12288000, 32000, 384, 0xc, 0x0},
4265409fb4eSMark Brown 	{18432000, 32000, 576, 0xd, 0x0},
4275409fb4eSMark Brown 	{12000000, 32000, 375, 0xa, 0x1},
4285409fb4eSMark Brown 
4295409fb4eSMark Brown 	/* 44.1k */
4305409fb4eSMark Brown 	{11289600, 44100, 256, 0x10, 0x0},
4315409fb4eSMark Brown 	{16934400, 44100, 384, 0x11, 0x0},
4325409fb4eSMark Brown 	{12000000, 44100, 272, 0x11, 0x1},
4335409fb4eSMark Brown 
4345409fb4eSMark Brown 	/* 48k */
4355409fb4eSMark Brown 	{12288000, 48000, 256, 0x0, 0x0},
4365409fb4eSMark Brown 	{18432000, 48000, 384, 0x1, 0x0},
4375409fb4eSMark Brown 	{12000000, 48000, 250, 0x0, 0x1},
4385409fb4eSMark Brown 
4395409fb4eSMark Brown 	/* 88.2k */
4405409fb4eSMark Brown 	{11289600, 88200, 128, 0x1e, 0x0},
4415409fb4eSMark Brown 	{16934400, 88200, 192, 0x1f, 0x0},
4425409fb4eSMark Brown 	{12000000, 88200, 136, 0x1f, 0x1},
4435409fb4eSMark Brown 
4445409fb4eSMark Brown 	/* 96k */
4455409fb4eSMark Brown 	{12288000, 96000, 128, 0xe, 0x0},
4465409fb4eSMark Brown 	{18432000, 96000, 192, 0xf, 0x0},
4475409fb4eSMark Brown 	{12000000, 96000, 125, 0xe, 0x1},
4485409fb4eSMark Brown };
4495409fb4eSMark Brown 
4505409fb4eSMark Brown static inline int get_coeff(int mclk, int rate)
4515409fb4eSMark Brown {
4525409fb4eSMark Brown 	int i;
4535409fb4eSMark Brown 
4545409fb4eSMark Brown 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
4555409fb4eSMark Brown 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
4565409fb4eSMark Brown 			return i;
4575409fb4eSMark Brown 	}
4585409fb4eSMark Brown 
4595409fb4eSMark Brown 	return -EINVAL;
4605409fb4eSMark Brown }
4615409fb4eSMark Brown 
4625409fb4eSMark Brown /* The set of rates we can generate from the above for each SYSCLK */
4635409fb4eSMark Brown 
4645409fb4eSMark Brown static unsigned int rates_12288[] = {
4655409fb4eSMark Brown 	8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
4665409fb4eSMark Brown };
4675409fb4eSMark Brown 
4685409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_12288 = {
4695409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_12288),
4705409fb4eSMark Brown 	.list	= rates_12288,
4715409fb4eSMark Brown };
4725409fb4eSMark Brown 
4735409fb4eSMark Brown static unsigned int rates_112896[] = {
4745409fb4eSMark Brown 	8000, 11025, 22050, 44100,
4755409fb4eSMark Brown };
4765409fb4eSMark Brown 
4775409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_112896 = {
4785409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_112896),
4795409fb4eSMark Brown 	.list	= rates_112896,
4805409fb4eSMark Brown };
4815409fb4eSMark Brown 
4825409fb4eSMark Brown static unsigned int rates_12[] = {
4835409fb4eSMark Brown 	8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
4845409fb4eSMark Brown 	48000, 88235, 96000,
4855409fb4eSMark Brown };
4865409fb4eSMark Brown 
4875409fb4eSMark Brown static struct snd_pcm_hw_constraint_list constraints_12 = {
4885409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_12),
4895409fb4eSMark Brown 	.list	= rates_12,
4905409fb4eSMark Brown };
4915409fb4eSMark Brown 
4925409fb4eSMark Brown /*
4935409fb4eSMark Brown  * Note that this should be called from init rather than from hw_params.
4945409fb4eSMark Brown  */
4955409fb4eSMark Brown static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai,
4965409fb4eSMark Brown 		int clk_id, unsigned int freq, int dir)
4975409fb4eSMark Brown {
4985409fb4eSMark Brown 	struct snd_soc_codec *codec = codec_dai->codec;
499b2c812e2SMark Brown 	struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
5005409fb4eSMark Brown 
5015409fb4eSMark Brown 	switch (freq) {
5025409fb4eSMark Brown 	case 11289600:
5035409fb4eSMark Brown 	case 18432000:
5045409fb4eSMark Brown 	case 22579200:
5055409fb4eSMark Brown 	case 36864000:
5065409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_112896;
5075409fb4eSMark Brown 		wm8988->sysclk = freq;
5085409fb4eSMark Brown 		return 0;
5095409fb4eSMark Brown 
5105409fb4eSMark Brown 	case 12288000:
5115409fb4eSMark Brown 	case 16934400:
5125409fb4eSMark Brown 	case 24576000:
5135409fb4eSMark Brown 	case 33868800:
5145409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_12288;
5155409fb4eSMark Brown 		wm8988->sysclk = freq;
5165409fb4eSMark Brown 		return 0;
5175409fb4eSMark Brown 
5185409fb4eSMark Brown 	case 12000000:
5195409fb4eSMark Brown 	case 24000000:
5205409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_12;
5215409fb4eSMark Brown 		wm8988->sysclk = freq;
5225409fb4eSMark Brown 		return 0;
5235409fb4eSMark Brown 	}
5245409fb4eSMark Brown 	return -EINVAL;
5255409fb4eSMark Brown }
5265409fb4eSMark Brown 
5275409fb4eSMark Brown static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai,
5285409fb4eSMark Brown 		unsigned int fmt)
5295409fb4eSMark Brown {
5305409fb4eSMark Brown 	struct snd_soc_codec *codec = codec_dai->codec;
5315409fb4eSMark Brown 	u16 iface = 0;
5325409fb4eSMark Brown 
5335409fb4eSMark Brown 	/* set master/slave audio interface */
5345409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
5355409fb4eSMark Brown 	case SND_SOC_DAIFMT_CBM_CFM:
5365409fb4eSMark Brown 		iface = 0x0040;
5375409fb4eSMark Brown 		break;
5385409fb4eSMark Brown 	case SND_SOC_DAIFMT_CBS_CFS:
5395409fb4eSMark Brown 		break;
5405409fb4eSMark Brown 	default:
5415409fb4eSMark Brown 		return -EINVAL;
5425409fb4eSMark Brown 	}
5435409fb4eSMark Brown 
5445409fb4eSMark Brown 	/* interface format */
5455409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
5465409fb4eSMark Brown 	case SND_SOC_DAIFMT_I2S:
5475409fb4eSMark Brown 		iface |= 0x0002;
5485409fb4eSMark Brown 		break;
5495409fb4eSMark Brown 	case SND_SOC_DAIFMT_RIGHT_J:
5505409fb4eSMark Brown 		break;
5515409fb4eSMark Brown 	case SND_SOC_DAIFMT_LEFT_J:
5525409fb4eSMark Brown 		iface |= 0x0001;
5535409fb4eSMark Brown 		break;
5545409fb4eSMark Brown 	case SND_SOC_DAIFMT_DSP_A:
5555409fb4eSMark Brown 		iface |= 0x0003;
5565409fb4eSMark Brown 		break;
5575409fb4eSMark Brown 	case SND_SOC_DAIFMT_DSP_B:
5585409fb4eSMark Brown 		iface |= 0x0013;
5595409fb4eSMark Brown 		break;
5605409fb4eSMark Brown 	default:
5615409fb4eSMark Brown 		return -EINVAL;
5625409fb4eSMark Brown 	}
5635409fb4eSMark Brown 
5645409fb4eSMark Brown 	/* clock inversion */
5655409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
5665409fb4eSMark Brown 	case SND_SOC_DAIFMT_NB_NF:
5675409fb4eSMark Brown 		break;
5685409fb4eSMark Brown 	case SND_SOC_DAIFMT_IB_IF:
5695409fb4eSMark Brown 		iface |= 0x0090;
5705409fb4eSMark Brown 		break;
5715409fb4eSMark Brown 	case SND_SOC_DAIFMT_IB_NF:
5725409fb4eSMark Brown 		iface |= 0x0080;
5735409fb4eSMark Brown 		break;
5745409fb4eSMark Brown 	case SND_SOC_DAIFMT_NB_IF:
5755409fb4eSMark Brown 		iface |= 0x0010;
5765409fb4eSMark Brown 		break;
5775409fb4eSMark Brown 	default:
5785409fb4eSMark Brown 		return -EINVAL;
5795409fb4eSMark Brown 	}
5805409fb4eSMark Brown 
58117a52fd6SMark Brown 	snd_soc_write(codec, WM8988_IFACE, iface);
5825409fb4eSMark Brown 	return 0;
5835409fb4eSMark Brown }
5845409fb4eSMark Brown 
5855409fb4eSMark Brown static int wm8988_pcm_startup(struct snd_pcm_substream *substream,
5865409fb4eSMark Brown 			      struct snd_soc_dai *dai)
5875409fb4eSMark Brown {
5885409fb4eSMark Brown 	struct snd_soc_codec *codec = dai->codec;
589b2c812e2SMark Brown 	struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
5905409fb4eSMark Brown 
5915409fb4eSMark Brown 	/* The set of sample rates that can be supported depends on the
5925409fb4eSMark Brown 	 * MCLK supplied to the CODEC - enforce this.
5935409fb4eSMark Brown 	 */
5945409fb4eSMark Brown 	if (!wm8988->sysclk) {
5955409fb4eSMark Brown 		dev_err(codec->dev,
5965409fb4eSMark Brown 			"No MCLK configured, call set_sysclk() on init\n");
5975409fb4eSMark Brown 		return -EINVAL;
5985409fb4eSMark Brown 	}
5995409fb4eSMark Brown 
6005409fb4eSMark Brown 	snd_pcm_hw_constraint_list(substream->runtime, 0,
6015409fb4eSMark Brown 				   SNDRV_PCM_HW_PARAM_RATE,
6025409fb4eSMark Brown 				   wm8988->sysclk_constraints);
6035409fb4eSMark Brown 
6045409fb4eSMark Brown 	return 0;
6055409fb4eSMark Brown }
6065409fb4eSMark Brown 
6075409fb4eSMark Brown static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream,
6085409fb4eSMark Brown 				struct snd_pcm_hw_params *params,
6095409fb4eSMark Brown 				struct snd_soc_dai *dai)
6105409fb4eSMark Brown {
6115409fb4eSMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
612*f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
613b2c812e2SMark Brown 	struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
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 
714*f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8988_dai = {
715*f0fba2adSLiam Girdwood 	.name = "wm8988-hifi",
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 
734*f0fba2adSLiam Girdwood static int wm8988_suspend(struct snd_soc_codec *codec, pm_message_t state)
7355409fb4eSMark Brown {
7365409fb4eSMark Brown 	wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF);
7375409fb4eSMark Brown 	return 0;
7385409fb4eSMark Brown }
7395409fb4eSMark Brown 
740*f0fba2adSLiam Girdwood static int wm8988_resume(struct snd_soc_codec *codec)
7415409fb4eSMark Brown {
7425409fb4eSMark Brown 	int i;
7435409fb4eSMark Brown 	u8 data[2];
7445409fb4eSMark Brown 	u16 *cache = codec->reg_cache;
7455409fb4eSMark Brown 
7465409fb4eSMark Brown 	/* Sync reg_cache with the hardware */
7475409fb4eSMark Brown 	for (i = 0; i < WM8988_NUM_REG; i++) {
7485409fb4eSMark Brown 		if (i == WM8988_RESET)
7495409fb4eSMark Brown 			continue;
7505409fb4eSMark Brown 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
7515409fb4eSMark Brown 		data[1] = cache[i] & 0x00ff;
7525409fb4eSMark Brown 		codec->hw_write(codec->control_data, data, 2);
7535409fb4eSMark Brown 	}
7545409fb4eSMark Brown 
7555409fb4eSMark Brown 	wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
7565409fb4eSMark Brown 
7575409fb4eSMark Brown 	return 0;
7585409fb4eSMark Brown }
7595409fb4eSMark Brown 
760*f0fba2adSLiam Girdwood static int wm8988_probe(struct snd_soc_codec *codec)
7615409fb4eSMark Brown {
762*f0fba2adSLiam Girdwood 	struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
7635409fb4eSMark Brown 	int ret = 0;
7645409fb4eSMark Brown 	u16 reg;
7655409fb4eSMark Brown 
766*f0fba2adSLiam Girdwood 	codec->control_data = wm8988->control_data;
767*f0fba2adSLiam Girdwood 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8988->control_type);
76817a52fd6SMark Brown 	if (ret < 0) {
76917a52fd6SMark Brown 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
770*f0fba2adSLiam Girdwood 		return ret;
77117a52fd6SMark Brown 	}
77217a52fd6SMark Brown 
7735409fb4eSMark Brown 	ret = wm8988_reset(codec);
7745409fb4eSMark Brown 	if (ret < 0) {
7755409fb4eSMark Brown 		dev_err(codec->dev, "Failed to issue reset\n");
776*f0fba2adSLiam Girdwood 		return ret;
7775409fb4eSMark Brown 	}
7785409fb4eSMark Brown 
7795409fb4eSMark Brown 	/* set the update bits (we always update left then right) */
78017a52fd6SMark Brown 	reg = snd_soc_read(codec, WM8988_RADC);
78117a52fd6SMark Brown 	snd_soc_write(codec, WM8988_RADC, reg | 0x100);
78217a52fd6SMark Brown 	reg = snd_soc_read(codec, WM8988_RDAC);
78317a52fd6SMark Brown 	snd_soc_write(codec, WM8988_RDAC, reg | 0x0100);
78417a52fd6SMark Brown 	reg = snd_soc_read(codec, WM8988_ROUT1V);
78517a52fd6SMark Brown 	snd_soc_write(codec, WM8988_ROUT1V, reg | 0x0100);
78617a52fd6SMark Brown 	reg = snd_soc_read(codec, WM8988_ROUT2V);
78717a52fd6SMark Brown 	snd_soc_write(codec, WM8988_ROUT2V, reg | 0x0100);
78817a52fd6SMark Brown 	reg = snd_soc_read(codec, WM8988_RINVOL);
78917a52fd6SMark Brown 	snd_soc_write(codec, WM8988_RINVOL, reg | 0x0100);
7905409fb4eSMark Brown 
791*f0fba2adSLiam Girdwood 	wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
7925409fb4eSMark Brown 
793*f0fba2adSLiam Girdwood 	snd_soc_add_controls(codec, wm8988_snd_controls,
794*f0fba2adSLiam Girdwood 				ARRAY_SIZE(wm8988_snd_controls));
795*f0fba2adSLiam Girdwood 	snd_soc_dapm_new_controls(codec, wm8988_dapm_widgets,
796*f0fba2adSLiam Girdwood 				  ARRAY_SIZE(wm8988_dapm_widgets));
797*f0fba2adSLiam Girdwood 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
7985409fb4eSMark Brown 
7995409fb4eSMark Brown 	return 0;
8005409fb4eSMark Brown }
8015409fb4eSMark Brown 
802*f0fba2adSLiam Girdwood static int wm8988_remove(struct snd_soc_codec *codec)
8035409fb4eSMark Brown {
804*f0fba2adSLiam Girdwood 	wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF);
805*f0fba2adSLiam Girdwood 	return 0;
8065409fb4eSMark Brown }
8075409fb4eSMark Brown 
808*f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8988 = {
809*f0fba2adSLiam Girdwood 	.probe =	wm8988_probe,
810*f0fba2adSLiam Girdwood 	.remove =	wm8988_remove,
811*f0fba2adSLiam Girdwood 	.suspend =	wm8988_suspend,
812*f0fba2adSLiam Girdwood 	.resume =	wm8988_resume,
813*f0fba2adSLiam Girdwood 	.set_bias_level = wm8988_set_bias_level,
814*f0fba2adSLiam Girdwood 	.reg_cache_size = sizeof(wm8988_reg),
815*f0fba2adSLiam Girdwood 	.reg_word_size = sizeof(u16),
816*f0fba2adSLiam Girdwood 	.reg_cache_default = wm8988_reg,
817*f0fba2adSLiam Girdwood };
818*f0fba2adSLiam Girdwood 
819*f0fba2adSLiam Girdwood #if defined(CONFIG_SPI_MASTER)
820*f0fba2adSLiam Girdwood static int __devinit wm8988_spi_probe(struct spi_device *spi)
8215409fb4eSMark Brown {
8225409fb4eSMark Brown 	struct wm8988_priv *wm8988;
823*f0fba2adSLiam Girdwood 	int ret;
8245409fb4eSMark Brown 
8255409fb4eSMark Brown 	wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
8265409fb4eSMark Brown 	if (wm8988 == NULL)
8275409fb4eSMark Brown 		return -ENOMEM;
8285409fb4eSMark Brown 
829*f0fba2adSLiam Girdwood 	wm8988->control_data = spi;
830*f0fba2adSLiam Girdwood 	wm8988->control_type = SND_SOC_SPI;
831*f0fba2adSLiam Girdwood 	spi_set_drvdata(spi, wm8988);
8325409fb4eSMark Brown 
833*f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&spi->dev,
834*f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8988, &wm8988_dai, 1);
835*f0fba2adSLiam Girdwood 	if (ret < 0)
836*f0fba2adSLiam Girdwood 		kfree(wm8988);
837*f0fba2adSLiam Girdwood 	return ret;
8385409fb4eSMark Brown }
8395409fb4eSMark Brown 
840*f0fba2adSLiam Girdwood static int __devexit wm8988_spi_remove(struct spi_device *spi)
8415409fb4eSMark Brown {
842*f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&spi->dev);
843*f0fba2adSLiam Girdwood 	kfree(spi_get_drvdata(spi));
844*f0fba2adSLiam Girdwood 	return 0;
845*f0fba2adSLiam Girdwood }
846*f0fba2adSLiam Girdwood 
847*f0fba2adSLiam Girdwood static struct spi_driver wm8988_spi_driver = {
848*f0fba2adSLiam Girdwood 	.driver = {
849*f0fba2adSLiam Girdwood 		.name	= "wm8988-codec",
850*f0fba2adSLiam Girdwood 		.bus	= &spi_bus_type,
851*f0fba2adSLiam Girdwood 		.owner	= THIS_MODULE,
852*f0fba2adSLiam Girdwood 	},
853*f0fba2adSLiam Girdwood 	.probe		= wm8988_spi_probe,
854*f0fba2adSLiam Girdwood 	.remove		= __devexit_p(wm8988_spi_remove),
855*f0fba2adSLiam Girdwood };
856*f0fba2adSLiam Girdwood #endif /* CONFIG_SPI_MASTER */
857*f0fba2adSLiam Girdwood 
858*f0fba2adSLiam Girdwood #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
859*f0fba2adSLiam Girdwood static __devinit int wm8988_i2c_probe(struct i2c_client *i2c,
860*f0fba2adSLiam Girdwood 				      const struct i2c_device_id *id)
861*f0fba2adSLiam Girdwood {
862*f0fba2adSLiam Girdwood 	struct wm8988_priv *wm8988;
863*f0fba2adSLiam Girdwood 	int ret;
864*f0fba2adSLiam Girdwood 
865*f0fba2adSLiam Girdwood 	wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
866*f0fba2adSLiam Girdwood 	if (wm8988 == NULL)
867*f0fba2adSLiam Girdwood 		return -ENOMEM;
868*f0fba2adSLiam Girdwood 
869*f0fba2adSLiam Girdwood 	i2c_set_clientdata(i2c, wm8988);
870*f0fba2adSLiam Girdwood 	wm8988->control_data = i2c;
871*f0fba2adSLiam Girdwood 	wm8988->control_type = SND_SOC_I2C;
872*f0fba2adSLiam Girdwood 
873*f0fba2adSLiam Girdwood 	ret =  snd_soc_register_codec(&i2c->dev,
874*f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8988, &wm8988_dai, 1);
875*f0fba2adSLiam Girdwood 	if (ret < 0)
876*f0fba2adSLiam Girdwood 		kfree(wm8988);
877*f0fba2adSLiam Girdwood 	return ret;
878*f0fba2adSLiam Girdwood }
879*f0fba2adSLiam Girdwood 
880*f0fba2adSLiam Girdwood static __devexit int wm8988_i2c_remove(struct i2c_client *client)
881*f0fba2adSLiam Girdwood {
882*f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&client->dev);
883*f0fba2adSLiam Girdwood 	kfree(i2c_get_clientdata(client));
8845409fb4eSMark Brown 	return 0;
8855409fb4eSMark Brown }
8865409fb4eSMark Brown 
8875409fb4eSMark Brown static const struct i2c_device_id wm8988_i2c_id[] = {
8885409fb4eSMark Brown 	{ "wm8988", 0 },
8895409fb4eSMark Brown 	{ }
8905409fb4eSMark Brown };
8915409fb4eSMark Brown MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id);
8925409fb4eSMark Brown 
8935409fb4eSMark Brown static struct i2c_driver wm8988_i2c_driver = {
8945409fb4eSMark Brown 	.driver = {
895*f0fba2adSLiam Girdwood 		.name = "wm8988-codec",
8965409fb4eSMark Brown 		.owner = THIS_MODULE,
8975409fb4eSMark Brown 	},
8985409fb4eSMark Brown 	.probe =    wm8988_i2c_probe,
899*f0fba2adSLiam Girdwood 	.remove =   __devexit_p(wm8988_i2c_remove),
9005409fb4eSMark Brown 	.id_table = wm8988_i2c_id,
9015409fb4eSMark Brown };
9025409fb4eSMark Brown #endif
9035409fb4eSMark Brown 
9045409fb4eSMark Brown static int __init wm8988_modinit(void)
9055409fb4eSMark Brown {
906*f0fba2adSLiam Girdwood 	int ret = 0;
9075409fb4eSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
9085409fb4eSMark Brown 	ret = i2c_add_driver(&wm8988_i2c_driver);
909*f0fba2adSLiam Girdwood 	if (ret != 0) {
910*f0fba2adSLiam Girdwood 		printk(KERN_ERR "Failed to register WM8988 I2C driver: %d\n",
911*f0fba2adSLiam Girdwood 		       ret);
912*f0fba2adSLiam Girdwood 	}
9135409fb4eSMark Brown #endif
9145409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER)
9155409fb4eSMark Brown 	ret = spi_register_driver(&wm8988_spi_driver);
916*f0fba2adSLiam Girdwood 	if (ret != 0) {
917*f0fba2adSLiam Girdwood 		printk(KERN_ERR "Failed to register WM8988 SPI driver: %d\n",
918*f0fba2adSLiam Girdwood 		       ret);
919*f0fba2adSLiam Girdwood 	}
9205409fb4eSMark Brown #endif
9215409fb4eSMark Brown 	return ret;
9225409fb4eSMark Brown }
9235409fb4eSMark Brown module_init(wm8988_modinit);
9245409fb4eSMark Brown 
9255409fb4eSMark Brown static void __exit wm8988_exit(void)
9265409fb4eSMark Brown {
9275409fb4eSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
9285409fb4eSMark Brown 	i2c_del_driver(&wm8988_i2c_driver);
9295409fb4eSMark Brown #endif
9305409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER)
9315409fb4eSMark Brown 	spi_unregister_driver(&wm8988_spi_driver);
9325409fb4eSMark Brown #endif
9335409fb4eSMark Brown }
9345409fb4eSMark Brown module_exit(wm8988_exit);
9355409fb4eSMark Brown 
9365409fb4eSMark Brown 
9375409fb4eSMark Brown MODULE_DESCRIPTION("ASoC WM8988 driver");
9385409fb4eSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
9395409fb4eSMark Brown MODULE_LICENSE("GPL");
940