xref: /openbmc/linux/sound/soc/codecs/wm8988.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25409fb4eSMark Brown /*
35409fb4eSMark Brown  * wm8988.c -- WM8988 ALSA SoC audio driver
45409fb4eSMark Brown  *
55409fb4eSMark Brown  * Copyright 2009 Wolfson Microelectronics plc
65409fb4eSMark Brown  * Copyright 2005 Openedhand Ltd.
75409fb4eSMark Brown  *
85409fb4eSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
95409fb4eSMark Brown  */
105409fb4eSMark Brown 
115409fb4eSMark Brown #include <linux/module.h>
125409fb4eSMark Brown #include <linux/moduleparam.h>
135409fb4eSMark Brown #include <linux/init.h>
145409fb4eSMark Brown #include <linux/delay.h>
155409fb4eSMark Brown #include <linux/pm.h>
165409fb4eSMark Brown #include <linux/i2c.h>
175409fb4eSMark Brown #include <linux/spi/spi.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
195409fb4eSMark Brown #include <sound/core.h>
205409fb4eSMark Brown #include <sound/pcm.h>
215409fb4eSMark Brown #include <sound/pcm_params.h>
225409fb4eSMark Brown #include <sound/tlv.h>
235409fb4eSMark Brown #include <sound/soc.h>
245409fb4eSMark Brown #include <sound/initval.h>
255409fb4eSMark Brown 
265409fb4eSMark Brown #include "wm8988.h"
275409fb4eSMark Brown 
285409fb4eSMark Brown /*
295409fb4eSMark Brown  * wm8988 register cache
305409fb4eSMark Brown  * We can't read the WM8988 register space when we
315409fb4eSMark Brown  * are using 2 wire for device control, so we cache them instead.
325409fb4eSMark Brown  */
33d2dc0a77SMark Brown static const struct reg_default wm8988_reg_defaults[] = {
34d2dc0a77SMark Brown 	{ 0, 0x0097 },
35d2dc0a77SMark Brown 	{ 1, 0x0097 },
36d2dc0a77SMark Brown 	{ 2, 0x0079 },
37d2dc0a77SMark Brown 	{ 3, 0x0079 },
38d2dc0a77SMark Brown 	{ 5, 0x0008 },
39d2dc0a77SMark Brown 	{ 7, 0x000a },
40d2dc0a77SMark Brown 	{ 8, 0x0000 },
41d2dc0a77SMark Brown 	{ 10, 0x00ff },
42d2dc0a77SMark Brown 	{ 11, 0x00ff },
43d2dc0a77SMark Brown 	{ 12, 0x000f },
44d2dc0a77SMark Brown 	{ 13, 0x000f },
45d2dc0a77SMark Brown 	{ 16, 0x0000 },
46d2dc0a77SMark Brown 	{ 17, 0x007b },
47d2dc0a77SMark Brown 	{ 18, 0x0000 },
48d2dc0a77SMark Brown 	{ 19, 0x0032 },
49d2dc0a77SMark Brown 	{ 20, 0x0000 },
50d2dc0a77SMark Brown 	{ 21, 0x00c3 },
51d2dc0a77SMark Brown 	{ 22, 0x00c3 },
52d2dc0a77SMark Brown 	{ 23, 0x00c0 },
53d2dc0a77SMark Brown 	{ 24, 0x0000 },
54d2dc0a77SMark Brown 	{ 25, 0x0000 },
55d2dc0a77SMark Brown 	{ 26, 0x0000 },
56d2dc0a77SMark Brown 	{ 27, 0x0000 },
57d2dc0a77SMark Brown 	{ 31, 0x0000 },
58d2dc0a77SMark Brown 	{ 32, 0x0000 },
59d2dc0a77SMark Brown 	{ 33, 0x0000 },
60d2dc0a77SMark Brown 	{ 34, 0x0050 },
61d2dc0a77SMark Brown 	{ 35, 0x0050 },
62d2dc0a77SMark Brown 	{ 36, 0x0050 },
63d2dc0a77SMark Brown 	{ 37, 0x0050 },
64d2dc0a77SMark Brown 	{ 40, 0x0079 },
65d2dc0a77SMark Brown 	{ 41, 0x0079 },
66d2dc0a77SMark Brown 	{ 42, 0x0079 },
675409fb4eSMark Brown };
685409fb4eSMark Brown 
wm8988_writeable(struct device * dev,unsigned int reg)69d2dc0a77SMark Brown static bool wm8988_writeable(struct device *dev, unsigned int reg)
70d2dc0a77SMark Brown {
71d2dc0a77SMark Brown 	switch (reg) {
72d2dc0a77SMark Brown 	case WM8988_LINVOL:
73d2dc0a77SMark Brown 	case WM8988_RINVOL:
74d2dc0a77SMark Brown 	case WM8988_LOUT1V:
75d2dc0a77SMark Brown 	case WM8988_ROUT1V:
76d2dc0a77SMark Brown 	case WM8988_ADCDAC:
77d2dc0a77SMark Brown 	case WM8988_IFACE:
78d2dc0a77SMark Brown 	case WM8988_SRATE:
79d2dc0a77SMark Brown 	case WM8988_LDAC:
80d2dc0a77SMark Brown 	case WM8988_RDAC:
81d2dc0a77SMark Brown 	case WM8988_BASS:
82d2dc0a77SMark Brown 	case WM8988_TREBLE:
83d2dc0a77SMark Brown 	case WM8988_RESET:
84d2dc0a77SMark Brown 	case WM8988_3D:
85d2dc0a77SMark Brown 	case WM8988_ALC1:
86d2dc0a77SMark Brown 	case WM8988_ALC2:
87d2dc0a77SMark Brown 	case WM8988_ALC3:
88d2dc0a77SMark Brown 	case WM8988_NGATE:
89d2dc0a77SMark Brown 	case WM8988_LADC:
90d2dc0a77SMark Brown 	case WM8988_RADC:
91d2dc0a77SMark Brown 	case WM8988_ADCTL1:
92d2dc0a77SMark Brown 	case WM8988_ADCTL2:
93d2dc0a77SMark Brown 	case WM8988_PWR1:
94d2dc0a77SMark Brown 	case WM8988_PWR2:
95d2dc0a77SMark Brown 	case WM8988_ADCTL3:
96d2dc0a77SMark Brown 	case WM8988_ADCIN:
97d2dc0a77SMark Brown 	case WM8988_LADCIN:
98d2dc0a77SMark Brown 	case WM8988_RADCIN:
99d2dc0a77SMark Brown 	case WM8988_LOUTM1:
100d2dc0a77SMark Brown 	case WM8988_LOUTM2:
101d2dc0a77SMark Brown 	case WM8988_ROUTM1:
102d2dc0a77SMark Brown 	case WM8988_ROUTM2:
103d2dc0a77SMark Brown 	case WM8988_LOUT2V:
104d2dc0a77SMark Brown 	case WM8988_ROUT2V:
105d2dc0a77SMark Brown 	case WM8988_LPPB:
106d2dc0a77SMark Brown 		return true;
107d2dc0a77SMark Brown 	default:
108d2dc0a77SMark Brown 		return false;
109d2dc0a77SMark Brown 	}
110d2dc0a77SMark Brown }
111d2dc0a77SMark Brown 
1125409fb4eSMark Brown /* codec private data */
1135409fb4eSMark Brown struct wm8988_priv {
114d2dc0a77SMark Brown 	struct regmap *regmap;
1155409fb4eSMark Brown 	unsigned int sysclk;
116b57efda1SLars-Peter Clausen 	const struct snd_pcm_hw_constraint_list *sysclk_constraints;
1175409fb4eSMark Brown };
1185409fb4eSMark Brown 
119ab38ff38SKuninori Morimoto #define wm8988_reset(c)	snd_soc_component_write(c, WM8988_RESET, 0)
1205409fb4eSMark Brown 
1215409fb4eSMark Brown /*
1225409fb4eSMark Brown  * WM8988 Controls
1235409fb4eSMark Brown  */
1245409fb4eSMark Brown 
1255409fb4eSMark Brown static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"};
126b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(bass_boost,
127b13a054aSTakashi Iwai 			    WM8988_BASS, 7, bass_boost_txt);
1285409fb4eSMark Brown 
1295409fb4eSMark Brown static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
130b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(bass_filter,
131b13a054aSTakashi Iwai 			    WM8988_BASS, 6, bass_filter_txt);
1325409fb4eSMark Brown 
1335409fb4eSMark Brown static const char *treble_txt[] = {"8kHz", "4kHz"};
134b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(treble,
135b13a054aSTakashi Iwai 			    WM8988_TREBLE, 6, treble_txt);
1365409fb4eSMark Brown 
1375409fb4eSMark Brown static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"};
138b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(stereo_3d_lc,
139b13a054aSTakashi Iwai 			    WM8988_3D, 5, stereo_3d_lc_txt);
1405409fb4eSMark Brown 
1415409fb4eSMark Brown static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"};
142b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(stereo_3d_uc,
143b13a054aSTakashi Iwai 			    WM8988_3D, 6, stereo_3d_uc_txt);
1445409fb4eSMark Brown 
1455409fb4eSMark Brown static const char *stereo_3d_func_txt[] = {"Capture", "Playback"};
146b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(stereo_3d_func,
147b13a054aSTakashi Iwai 			    WM8988_3D, 7, stereo_3d_func_txt);
1485409fb4eSMark Brown 
1495409fb4eSMark Brown static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
150b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_func,
151b13a054aSTakashi Iwai 			    WM8988_ALC1, 7, alc_func_txt);
1525409fb4eSMark Brown 
1535409fb4eSMark Brown static const char *ng_type_txt[] = {"Constant PGA Gain",
1545409fb4eSMark Brown 				    "Mute ADC Output"};
155b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(ng_type,
156b13a054aSTakashi Iwai 			    WM8988_NGATE, 1, ng_type_txt);
1575409fb4eSMark Brown 
1585409fb4eSMark Brown static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
159b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(deemph,
160b13a054aSTakashi Iwai 			    WM8988_ADCDAC, 1, deemph_txt);
1615409fb4eSMark Brown 
1625409fb4eSMark Brown static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert",
1635409fb4eSMark Brown 				   "L + R Invert"};
164b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(adcpol,
165b13a054aSTakashi Iwai 			    WM8988_ADCDAC, 5, adcpol_txt);
1665409fb4eSMark Brown 
1675409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
1685409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
1695409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
1705409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
1715409fb4eSMark Brown static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
1725409fb4eSMark Brown 
1735409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_snd_controls[] = {
1745409fb4eSMark Brown 
1755409fb4eSMark Brown SOC_ENUM("Bass Boost", bass_boost),
1765409fb4eSMark Brown SOC_ENUM("Bass Filter", bass_filter),
1775409fb4eSMark Brown SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1),
1785409fb4eSMark Brown 
1795409fb4eSMark Brown SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0),
1805409fb4eSMark Brown SOC_ENUM("Treble Cut-off", treble),
1815409fb4eSMark Brown 
1825409fb4eSMark Brown SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0),
1835409fb4eSMark Brown SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0),
1845409fb4eSMark Brown SOC_ENUM("3D Lower Cut-off", stereo_3d_lc),
1855409fb4eSMark Brown SOC_ENUM("3D Upper Cut-off", stereo_3d_uc),
1865409fb4eSMark Brown SOC_ENUM("3D Mode", stereo_3d_func),
1875409fb4eSMark Brown 
1885409fb4eSMark Brown SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0),
1895409fb4eSMark Brown SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0),
1905409fb4eSMark Brown SOC_ENUM("ALC Capture Function", alc_func),
1915409fb4eSMark Brown SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0),
1925409fb4eSMark Brown SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0),
1935409fb4eSMark Brown SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0),
1945409fb4eSMark Brown SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0),
1955409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0),
1965409fb4eSMark Brown SOC_ENUM("ALC Capture NG Type", ng_type),
1975409fb4eSMark Brown SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0),
1985409fb4eSMark Brown 
1995409fb4eSMark Brown SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0),
2005409fb4eSMark Brown 
2015409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC,
2025409fb4eSMark Brown 		 0, 255, 0, adc_tlv),
2035409fb4eSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL,
2045409fb4eSMark Brown 		 0, 63, 0, pga_tlv),
2055409fb4eSMark Brown SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0),
2065409fb4eSMark Brown SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1),
2075409fb4eSMark Brown 
2085409fb4eSMark Brown SOC_ENUM("Playback De-emphasis", deemph),
2095409fb4eSMark Brown 
2105409fb4eSMark Brown SOC_ENUM("Capture Polarity", adcpol),
2115409fb4eSMark Brown SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0),
2125409fb4eSMark Brown SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0),
2135409fb4eSMark Brown 
2145409fb4eSMark Brown SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv),
2155409fb4eSMark Brown 
2165409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1,
2175409fb4eSMark Brown 	       bypass_tlv),
2185409fb4eSMark Brown SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1,
2195409fb4eSMark Brown 	       bypass_tlv),
2205409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1,
2215409fb4eSMark Brown 	       bypass_tlv),
2225409fb4eSMark Brown SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1,
2235409fb4eSMark Brown 	       bypass_tlv),
2245409fb4eSMark Brown 
2255409fb4eSMark Brown SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V,
2265409fb4eSMark Brown 	     WM8988_ROUT1V, 7, 1, 0),
2275409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V,
2285409fb4eSMark Brown 		 0, 127, 0, out_tlv),
2295409fb4eSMark Brown 
2305409fb4eSMark Brown SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V,
2315409fb4eSMark Brown 	     WM8988_ROUT2V, 7, 1, 0),
2325409fb4eSMark Brown SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V,
2335409fb4eSMark Brown 		 0, 127, 0, out_tlv),
2345409fb4eSMark Brown 
2355409fb4eSMark Brown };
2365409fb4eSMark Brown 
2375409fb4eSMark Brown /*
2385409fb4eSMark Brown  * DAPM Controls
2395409fb4eSMark Brown  */
2405409fb4eSMark Brown 
wm8988_lrc_control(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)2415409fb4eSMark Brown static int wm8988_lrc_control(struct snd_soc_dapm_widget *w,
2425409fb4eSMark Brown 			      struct snd_kcontrol *kcontrol, int event)
2435409fb4eSMark Brown {
244ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2456d75dfc3SKuninori Morimoto 	u16 adctl2 = snd_soc_component_read(component, WM8988_ADCTL2);
2465409fb4eSMark Brown 
2475409fb4eSMark Brown 	/* Use the DAC to gate LRC if active, otherwise use ADC */
2486d75dfc3SKuninori Morimoto 	if (snd_soc_component_read(component, WM8988_PWR2) & 0x180)
2495409fb4eSMark Brown 		adctl2 &= ~0x4;
2505409fb4eSMark Brown 	else
2515409fb4eSMark Brown 		adctl2 |= 0x4;
2525409fb4eSMark Brown 
253ab38ff38SKuninori Morimoto 	return snd_soc_component_write(component, WM8988_ADCTL2, adctl2);
2545409fb4eSMark Brown }
2555409fb4eSMark Brown 
2565409fb4eSMark Brown static const char *wm8988_line_texts[] = {
2575409fb4eSMark Brown 	"Line 1", "Line 2", "PGA", "Differential"};
2585409fb4eSMark Brown 
2595409fb4eSMark Brown static const unsigned int wm8988_line_values[] = {
2605409fb4eSMark Brown 	0, 1, 3, 4};
2615409fb4eSMark Brown 
2625409fb4eSMark Brown static const struct soc_enum wm8988_lline_enum =
2635409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7,
2645409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_line_texts),
2655409fb4eSMark Brown 			      wm8988_line_texts,
2665409fb4eSMark Brown 			      wm8988_line_values);
2675409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_line_controls =
268fb7d79e5SLars-Peter Clausen 	SOC_DAPM_ENUM("Route", wm8988_lline_enum);
2695409fb4eSMark Brown 
2705409fb4eSMark Brown static const struct soc_enum wm8988_rline_enum =
2715409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7,
2725409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_line_texts),
2735409fb4eSMark Brown 			      wm8988_line_texts,
2745409fb4eSMark Brown 			      wm8988_line_values);
2755409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_line_controls =
276c101fb29SYueHaibing 	SOC_DAPM_ENUM("Route", wm8988_rline_enum);
2775409fb4eSMark Brown 
2785409fb4eSMark Brown /* Left Mixer */
2795409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = {
2805409fb4eSMark Brown 	SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0),
2815409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0),
2825409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0),
2835409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0),
2845409fb4eSMark Brown };
2855409fb4eSMark Brown 
2865409fb4eSMark Brown /* Right Mixer */
2875409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = {
2885409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0),
2895409fb4eSMark Brown 	SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0),
2905409fb4eSMark Brown 	SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0),
2915409fb4eSMark Brown 	SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0),
2925409fb4eSMark Brown };
2935409fb4eSMark Brown 
2945409fb4eSMark Brown static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"};
2955409fb4eSMark Brown static const unsigned int wm8988_pga_val[] = { 0, 1, 3 };
2965409fb4eSMark Brown 
2975409fb4eSMark Brown /* Left PGA Mux */
2985409fb4eSMark Brown static const struct soc_enum wm8988_lpga_enum =
2995409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3,
3005409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_pga_sel),
3015409fb4eSMark Brown 			      wm8988_pga_sel,
3025409fb4eSMark Brown 			      wm8988_pga_val);
3035409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_left_pga_controls =
304fb7d79e5SLars-Peter Clausen 	SOC_DAPM_ENUM("Route", wm8988_lpga_enum);
3055409fb4eSMark Brown 
3065409fb4eSMark Brown /* Right PGA Mux */
3075409fb4eSMark Brown static const struct soc_enum wm8988_rpga_enum =
3085409fb4eSMark Brown 	SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3,
3095409fb4eSMark Brown 			      ARRAY_SIZE(wm8988_pga_sel),
3105409fb4eSMark Brown 			      wm8988_pga_sel,
3115409fb4eSMark Brown 			      wm8988_pga_val);
3125409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_right_pga_controls =
313fb7d79e5SLars-Peter Clausen 	SOC_DAPM_ENUM("Route", wm8988_rpga_enum);
3145409fb4eSMark Brown 
3155409fb4eSMark Brown /* Differential Mux */
3165409fb4eSMark Brown static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"};
317b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(diffmux,
318b13a054aSTakashi Iwai 			    WM8988_ADCIN, 8, wm8988_diff_sel);
3195409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_diffmux_controls =
3205409fb4eSMark Brown 	SOC_DAPM_ENUM("Route", diffmux);
3215409fb4eSMark Brown 
3225409fb4eSMark Brown /* Mono ADC Mux */
3235409fb4eSMark Brown static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)",
3245409fb4eSMark Brown 	"Mono (Right)", "Digital Mono"};
325b13a054aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(monomux,
326b13a054aSTakashi Iwai 			    WM8988_ADCIN, 6, wm8988_mono_mux);
3275409fb4eSMark Brown static const struct snd_kcontrol_new wm8988_monomux_controls =
3285409fb4eSMark Brown 	SOC_DAPM_ENUM("Route", monomux);
3295409fb4eSMark Brown 
3305409fb4eSMark Brown static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = {
331be48f20dSMark Brown 	SND_SOC_DAPM_SUPPLY("Mic Bias", WM8988_PWR1, 1, 0, NULL, 0),
3325409fb4eSMark Brown 
3335409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
3345409fb4eSMark Brown 		&wm8988_diffmux_controls),
3355409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
3365409fb4eSMark Brown 		&wm8988_monomux_controls),
3375409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
3385409fb4eSMark Brown 		&wm8988_monomux_controls),
3395409fb4eSMark Brown 
3405409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0,
3415409fb4eSMark Brown 		&wm8988_left_pga_controls),
3425409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0,
3435409fb4eSMark Brown 		&wm8988_right_pga_controls),
3445409fb4eSMark Brown 
3455409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
3465409fb4eSMark Brown 		&wm8988_left_line_controls),
3475409fb4eSMark Brown 	SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
3485409fb4eSMark Brown 		&wm8988_right_line_controls),
3495409fb4eSMark Brown 
3505409fb4eSMark Brown 	SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0),
3515409fb4eSMark Brown 	SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0),
3525409fb4eSMark Brown 
3535409fb4eSMark Brown 	SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0),
3545409fb4eSMark Brown 	SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0),
3555409fb4eSMark Brown 
3565409fb4eSMark Brown 	SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
3575409fb4eSMark Brown 		&wm8988_left_mixer_controls[0],
3585409fb4eSMark Brown 		ARRAY_SIZE(wm8988_left_mixer_controls)),
3595409fb4eSMark Brown 	SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
3605409fb4eSMark Brown 		&wm8988_right_mixer_controls[0],
3615409fb4eSMark Brown 		ARRAY_SIZE(wm8988_right_mixer_controls)),
3625409fb4eSMark Brown 
3635409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0),
3645409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0),
3655409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0),
3665409fb4eSMark Brown 	SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0),
3675409fb4eSMark Brown 
3685409fb4eSMark Brown 	SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control),
3695409fb4eSMark Brown 
3705409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("LOUT1"),
3715409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("ROUT1"),
3725409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("LOUT2"),
3735409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("ROUT2"),
3745409fb4eSMark Brown 	SND_SOC_DAPM_OUTPUT("VREF"),
3755409fb4eSMark Brown 
3765409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("LINPUT1"),
3775409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("LINPUT2"),
3785409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("RINPUT1"),
3795409fb4eSMark Brown 	SND_SOC_DAPM_INPUT("RINPUT2"),
3805409fb4eSMark Brown };
3815409fb4eSMark Brown 
382dd21353fSMark Brown static const struct snd_soc_dapm_route wm8988_dapm_routes[] = {
3835409fb4eSMark Brown 
3845409fb4eSMark Brown 	{ "Left Line Mux", "Line 1", "LINPUT1" },
3855409fb4eSMark Brown 	{ "Left Line Mux", "Line 2", "LINPUT2" },
3865409fb4eSMark Brown 	{ "Left Line Mux", "PGA", "Left PGA Mux" },
3875409fb4eSMark Brown 	{ "Left Line Mux", "Differential", "Differential Mux" },
3885409fb4eSMark Brown 
3895409fb4eSMark Brown 	{ "Right Line Mux", "Line 1", "RINPUT1" },
3905409fb4eSMark Brown 	{ "Right Line Mux", "Line 2", "RINPUT2" },
3915409fb4eSMark Brown 	{ "Right Line Mux", "PGA", "Right PGA Mux" },
3925409fb4eSMark Brown 	{ "Right Line Mux", "Differential", "Differential Mux" },
3935409fb4eSMark Brown 
3945409fb4eSMark Brown 	{ "Left PGA Mux", "Line 1", "LINPUT1" },
3955409fb4eSMark Brown 	{ "Left PGA Mux", "Line 2", "LINPUT2" },
3965409fb4eSMark Brown 	{ "Left PGA Mux", "Differential", "Differential Mux" },
3975409fb4eSMark Brown 
3985409fb4eSMark Brown 	{ "Right PGA Mux", "Line 1", "RINPUT1" },
3995409fb4eSMark Brown 	{ "Right PGA Mux", "Line 2", "RINPUT2" },
4005409fb4eSMark Brown 	{ "Right PGA Mux", "Differential", "Differential Mux" },
4015409fb4eSMark Brown 
4025409fb4eSMark Brown 	{ "Differential Mux", "Line 1", "LINPUT1" },
4035409fb4eSMark Brown 	{ "Differential Mux", "Line 1", "RINPUT1" },
4045409fb4eSMark Brown 	{ "Differential Mux", "Line 2", "LINPUT2" },
4055409fb4eSMark Brown 	{ "Differential Mux", "Line 2", "RINPUT2" },
4065409fb4eSMark Brown 
4075409fb4eSMark Brown 	{ "Left ADC Mux", "Stereo", "Left PGA Mux" },
4085409fb4eSMark Brown 	{ "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
4095409fb4eSMark Brown 	{ "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
4105409fb4eSMark Brown 
4115409fb4eSMark Brown 	{ "Right ADC Mux", "Stereo", "Right PGA Mux" },
4125409fb4eSMark Brown 	{ "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
4135409fb4eSMark Brown 	{ "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
4145409fb4eSMark Brown 
4155409fb4eSMark Brown 	{ "Left ADC", NULL, "Left ADC Mux" },
4165409fb4eSMark Brown 	{ "Right ADC", NULL, "Right ADC Mux" },
4175409fb4eSMark Brown 
4185409fb4eSMark Brown 	{ "Left Line Mux", "Line 1", "LINPUT1" },
4195409fb4eSMark Brown 	{ "Left Line Mux", "Line 2", "LINPUT2" },
4205409fb4eSMark Brown 	{ "Left Line Mux", "PGA", "Left PGA Mux" },
4215409fb4eSMark Brown 	{ "Left Line Mux", "Differential", "Differential Mux" },
4225409fb4eSMark Brown 
4235409fb4eSMark Brown 	{ "Right Line Mux", "Line 1", "RINPUT1" },
4245409fb4eSMark Brown 	{ "Right Line Mux", "Line 2", "RINPUT2" },
4255409fb4eSMark Brown 	{ "Right Line Mux", "PGA", "Right PGA Mux" },
4265409fb4eSMark Brown 	{ "Right Line Mux", "Differential", "Differential Mux" },
4275409fb4eSMark Brown 
4285409fb4eSMark Brown 	{ "Left Mixer", "Playback Switch", "Left DAC" },
4295409fb4eSMark Brown 	{ "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
4305409fb4eSMark Brown 	{ "Left Mixer", "Right Playback Switch", "Right DAC" },
4315409fb4eSMark Brown 	{ "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
4325409fb4eSMark Brown 
4335409fb4eSMark Brown 	{ "Right Mixer", "Left Playback Switch", "Left DAC" },
4345409fb4eSMark Brown 	{ "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
4355409fb4eSMark Brown 	{ "Right Mixer", "Playback Switch", "Right DAC" },
4365409fb4eSMark Brown 	{ "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
4375409fb4eSMark Brown 
4385409fb4eSMark Brown 	{ "Left Out 1", NULL, "Left Mixer" },
4395409fb4eSMark Brown 	{ "LOUT1", NULL, "Left Out 1" },
4405409fb4eSMark Brown 	{ "Right Out 1", NULL, "Right Mixer" },
4415409fb4eSMark Brown 	{ "ROUT1", NULL, "Right Out 1" },
4425409fb4eSMark Brown 
4435409fb4eSMark Brown 	{ "Left Out 2", NULL, "Left Mixer" },
4445409fb4eSMark Brown 	{ "LOUT2", NULL, "Left Out 2" },
4455409fb4eSMark Brown 	{ "Right Out 2", NULL, "Right Mixer" },
4465409fb4eSMark Brown 	{ "ROUT2", NULL, "Right Out 2" },
4475409fb4eSMark Brown };
4485409fb4eSMark Brown 
4495409fb4eSMark Brown struct _coeff_div {
4505409fb4eSMark Brown 	u32 mclk;
4515409fb4eSMark Brown 	u32 rate;
4525409fb4eSMark Brown 	u16 fs;
4535409fb4eSMark Brown 	u8 sr:5;
4545409fb4eSMark Brown 	u8 usb:1;
4555409fb4eSMark Brown };
4565409fb4eSMark Brown 
4575409fb4eSMark Brown /* codec hifi mclk clock divider coefficients */
4585409fb4eSMark Brown static const struct _coeff_div coeff_div[] = {
4595409fb4eSMark Brown 	/* 8k */
4605409fb4eSMark Brown 	{12288000, 8000, 1536, 0x6, 0x0},
4615409fb4eSMark Brown 	{11289600, 8000, 1408, 0x16, 0x0},
4625409fb4eSMark Brown 	{18432000, 8000, 2304, 0x7, 0x0},
4635409fb4eSMark Brown 	{16934400, 8000, 2112, 0x17, 0x0},
4645409fb4eSMark Brown 	{12000000, 8000, 1500, 0x6, 0x1},
4655409fb4eSMark Brown 
4665409fb4eSMark Brown 	/* 11.025k */
4675409fb4eSMark Brown 	{11289600, 11025, 1024, 0x18, 0x0},
4685409fb4eSMark Brown 	{16934400, 11025, 1536, 0x19, 0x0},
4695409fb4eSMark Brown 	{12000000, 11025, 1088, 0x19, 0x1},
4705409fb4eSMark Brown 
4715409fb4eSMark Brown 	/* 16k */
4725409fb4eSMark Brown 	{12288000, 16000, 768, 0xa, 0x0},
4735409fb4eSMark Brown 	{18432000, 16000, 1152, 0xb, 0x0},
4745409fb4eSMark Brown 	{12000000, 16000, 750, 0xa, 0x1},
4755409fb4eSMark Brown 
4765409fb4eSMark Brown 	/* 22.05k */
4775409fb4eSMark Brown 	{11289600, 22050, 512, 0x1a, 0x0},
4785409fb4eSMark Brown 	{16934400, 22050, 768, 0x1b, 0x0},
4795409fb4eSMark Brown 	{12000000, 22050, 544, 0x1b, 0x1},
4805409fb4eSMark Brown 
4815409fb4eSMark Brown 	/* 32k */
4825409fb4eSMark Brown 	{12288000, 32000, 384, 0xc, 0x0},
4835409fb4eSMark Brown 	{18432000, 32000, 576, 0xd, 0x0},
4845409fb4eSMark Brown 	{12000000, 32000, 375, 0xa, 0x1},
4855409fb4eSMark Brown 
4865409fb4eSMark Brown 	/* 44.1k */
4875409fb4eSMark Brown 	{11289600, 44100, 256, 0x10, 0x0},
4885409fb4eSMark Brown 	{16934400, 44100, 384, 0x11, 0x0},
4895409fb4eSMark Brown 	{12000000, 44100, 272, 0x11, 0x1},
4905409fb4eSMark Brown 
4915409fb4eSMark Brown 	/* 48k */
4925409fb4eSMark Brown 	{12288000, 48000, 256, 0x0, 0x0},
4935409fb4eSMark Brown 	{18432000, 48000, 384, 0x1, 0x0},
4945409fb4eSMark Brown 	{12000000, 48000, 250, 0x0, 0x1},
4955409fb4eSMark Brown 
4965409fb4eSMark Brown 	/* 88.2k */
4975409fb4eSMark Brown 	{11289600, 88200, 128, 0x1e, 0x0},
4985409fb4eSMark Brown 	{16934400, 88200, 192, 0x1f, 0x0},
4995409fb4eSMark Brown 	{12000000, 88200, 136, 0x1f, 0x1},
5005409fb4eSMark Brown 
5015409fb4eSMark Brown 	/* 96k */
5025409fb4eSMark Brown 	{12288000, 96000, 128, 0xe, 0x0},
5035409fb4eSMark Brown 	{18432000, 96000, 192, 0xf, 0x0},
5045409fb4eSMark Brown 	{12000000, 96000, 125, 0xe, 0x1},
5055409fb4eSMark Brown };
5065409fb4eSMark Brown 
get_coeff(int mclk,int rate)5075409fb4eSMark Brown static inline int get_coeff(int mclk, int rate)
5085409fb4eSMark Brown {
5095409fb4eSMark Brown 	int i;
5105409fb4eSMark Brown 
5115409fb4eSMark Brown 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
5125409fb4eSMark Brown 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
5135409fb4eSMark Brown 			return i;
5145409fb4eSMark Brown 	}
5155409fb4eSMark Brown 
5165409fb4eSMark Brown 	return -EINVAL;
5175409fb4eSMark Brown }
5185409fb4eSMark Brown 
5195409fb4eSMark Brown /* The set of rates we can generate from the above for each SYSCLK */
5205409fb4eSMark Brown 
521b57efda1SLars-Peter Clausen static const unsigned int rates_12288[] = {
522c9c9780dSLadislav Michl 	8000, 12000, 16000, 24000, 32000, 48000, 96000,
5235409fb4eSMark Brown };
5245409fb4eSMark Brown 
525b57efda1SLars-Peter Clausen static const struct snd_pcm_hw_constraint_list constraints_12288 = {
5265409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_12288),
5275409fb4eSMark Brown 	.list	= rates_12288,
5285409fb4eSMark Brown };
5295409fb4eSMark Brown 
530b57efda1SLars-Peter Clausen static const unsigned int rates_112896[] = {
5315409fb4eSMark Brown 	8000, 11025, 22050, 44100,
5325409fb4eSMark Brown };
5335409fb4eSMark Brown 
534b57efda1SLars-Peter Clausen static const struct snd_pcm_hw_constraint_list constraints_112896 = {
5355409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_112896),
5365409fb4eSMark Brown 	.list	= rates_112896,
5375409fb4eSMark Brown };
5385409fb4eSMark Brown 
539b57efda1SLars-Peter Clausen static const unsigned int rates_12[] = {
540c9c9780dSLadislav Michl 	8000, 11025, 12000, 16000, 22050, 24000, 32000, 41100, 48000,
5415409fb4eSMark Brown 	48000, 88235, 96000,
5425409fb4eSMark Brown };
5435409fb4eSMark Brown 
544b57efda1SLars-Peter Clausen static const struct snd_pcm_hw_constraint_list constraints_12 = {
5455409fb4eSMark Brown 	.count	= ARRAY_SIZE(rates_12),
5465409fb4eSMark Brown 	.list	= rates_12,
5475409fb4eSMark Brown };
5485409fb4eSMark Brown 
5495409fb4eSMark Brown /*
5505409fb4eSMark Brown  * Note that this should be called from init rather than from hw_params.
5515409fb4eSMark Brown  */
wm8988_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)5525409fb4eSMark Brown static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai,
5535409fb4eSMark Brown 		int clk_id, unsigned int freq, int dir)
5545409fb4eSMark Brown {
555ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
556ab38ff38SKuninori Morimoto 	struct wm8988_priv *wm8988 = snd_soc_component_get_drvdata(component);
5575409fb4eSMark Brown 
5585409fb4eSMark Brown 	switch (freq) {
5595409fb4eSMark Brown 	case 11289600:
5605409fb4eSMark Brown 	case 18432000:
5615409fb4eSMark Brown 	case 22579200:
5625409fb4eSMark Brown 	case 36864000:
5635409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_112896;
5645409fb4eSMark Brown 		wm8988->sysclk = freq;
5655409fb4eSMark Brown 		return 0;
5665409fb4eSMark Brown 
5675409fb4eSMark Brown 	case 12288000:
5685409fb4eSMark Brown 	case 16934400:
5695409fb4eSMark Brown 	case 24576000:
5705409fb4eSMark Brown 	case 33868800:
5715409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_12288;
5725409fb4eSMark Brown 		wm8988->sysclk = freq;
5735409fb4eSMark Brown 		return 0;
5745409fb4eSMark Brown 
5755409fb4eSMark Brown 	case 12000000:
5765409fb4eSMark Brown 	case 24000000:
5775409fb4eSMark Brown 		wm8988->sysclk_constraints = &constraints_12;
5785409fb4eSMark Brown 		wm8988->sysclk = freq;
5795409fb4eSMark Brown 		return 0;
5805409fb4eSMark Brown 	}
5815409fb4eSMark Brown 	return -EINVAL;
5825409fb4eSMark Brown }
5835409fb4eSMark Brown 
wm8988_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int fmt)5845409fb4eSMark Brown static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai,
5855409fb4eSMark Brown 		unsigned int fmt)
5865409fb4eSMark Brown {
587ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
5885409fb4eSMark Brown 	u16 iface = 0;
5895409fb4eSMark Brown 
5905409fb4eSMark Brown 	/* set master/slave audio interface */
5915409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
5925409fb4eSMark Brown 	case SND_SOC_DAIFMT_CBM_CFM:
5935409fb4eSMark Brown 		iface = 0x0040;
5945409fb4eSMark Brown 		break;
5955409fb4eSMark Brown 	case SND_SOC_DAIFMT_CBS_CFS:
5965409fb4eSMark Brown 		break;
5975409fb4eSMark Brown 	default:
5985409fb4eSMark Brown 		return -EINVAL;
5995409fb4eSMark Brown 	}
6005409fb4eSMark Brown 
6015409fb4eSMark Brown 	/* interface format */
6025409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6035409fb4eSMark Brown 	case SND_SOC_DAIFMT_I2S:
6045409fb4eSMark Brown 		iface |= 0x0002;
6055409fb4eSMark Brown 		break;
6065409fb4eSMark Brown 	case SND_SOC_DAIFMT_RIGHT_J:
6075409fb4eSMark Brown 		break;
6085409fb4eSMark Brown 	case SND_SOC_DAIFMT_LEFT_J:
6095409fb4eSMark Brown 		iface |= 0x0001;
6105409fb4eSMark Brown 		break;
6115409fb4eSMark Brown 	case SND_SOC_DAIFMT_DSP_A:
6125409fb4eSMark Brown 		iface |= 0x0003;
6135409fb4eSMark Brown 		break;
6145409fb4eSMark Brown 	case SND_SOC_DAIFMT_DSP_B:
6155409fb4eSMark Brown 		iface |= 0x0013;
6165409fb4eSMark Brown 		break;
6175409fb4eSMark Brown 	default:
6185409fb4eSMark Brown 		return -EINVAL;
6195409fb4eSMark Brown 	}
6205409fb4eSMark Brown 
6215409fb4eSMark Brown 	/* clock inversion */
6225409fb4eSMark Brown 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6235409fb4eSMark Brown 	case SND_SOC_DAIFMT_NB_NF:
6245409fb4eSMark Brown 		break;
6255409fb4eSMark Brown 	case SND_SOC_DAIFMT_IB_IF:
6265409fb4eSMark Brown 		iface |= 0x0090;
6275409fb4eSMark Brown 		break;
6285409fb4eSMark Brown 	case SND_SOC_DAIFMT_IB_NF:
6295409fb4eSMark Brown 		iface |= 0x0080;
6305409fb4eSMark Brown 		break;
6315409fb4eSMark Brown 	case SND_SOC_DAIFMT_NB_IF:
6325409fb4eSMark Brown 		iface |= 0x0010;
6335409fb4eSMark Brown 		break;
6345409fb4eSMark Brown 	default:
6355409fb4eSMark Brown 		return -EINVAL;
6365409fb4eSMark Brown 	}
6375409fb4eSMark Brown 
638ab38ff38SKuninori Morimoto 	snd_soc_component_write(component, WM8988_IFACE, iface);
6395409fb4eSMark Brown 	return 0;
6405409fb4eSMark Brown }
6415409fb4eSMark Brown 
wm8988_pcm_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)6425409fb4eSMark Brown static int wm8988_pcm_startup(struct snd_pcm_substream *substream,
6435409fb4eSMark Brown 			      struct snd_soc_dai *dai)
6445409fb4eSMark Brown {
645ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
646ab38ff38SKuninori Morimoto 	struct wm8988_priv *wm8988 = snd_soc_component_get_drvdata(component);
6475409fb4eSMark Brown 
6485409fb4eSMark Brown 	/* The set of sample rates that can be supported depends on the
6495409fb4eSMark Brown 	 * MCLK supplied to the CODEC - enforce this.
6505409fb4eSMark Brown 	 */
6515409fb4eSMark Brown 	if (!wm8988->sysclk) {
652ab38ff38SKuninori Morimoto 		dev_err(component->dev,
6535409fb4eSMark Brown 			"No MCLK configured, call set_sysclk() on init\n");
6545409fb4eSMark Brown 		return -EINVAL;
6555409fb4eSMark Brown 	}
6565409fb4eSMark Brown 
6575409fb4eSMark Brown 	snd_pcm_hw_constraint_list(substream->runtime, 0,
6585409fb4eSMark Brown 				   SNDRV_PCM_HW_PARAM_RATE,
6595409fb4eSMark Brown 				   wm8988->sysclk_constraints);
6605409fb4eSMark Brown 
6615409fb4eSMark Brown 	return 0;
6625409fb4eSMark Brown }
6635409fb4eSMark Brown 
wm8988_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)6645409fb4eSMark Brown static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream,
6655409fb4eSMark Brown 				struct snd_pcm_hw_params *params,
6665409fb4eSMark Brown 				struct snd_soc_dai *dai)
6675409fb4eSMark Brown {
668ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
669ab38ff38SKuninori Morimoto 	struct wm8988_priv *wm8988 = snd_soc_component_get_drvdata(component);
6706d75dfc3SKuninori Morimoto 	u16 iface = snd_soc_component_read(component, WM8988_IFACE) & 0x1f3;
6716d75dfc3SKuninori Morimoto 	u16 srate = snd_soc_component_read(component, WM8988_SRATE) & 0x180;
6725409fb4eSMark Brown 	int coeff;
6735409fb4eSMark Brown 
6745409fb4eSMark Brown 	coeff = get_coeff(wm8988->sysclk, params_rate(params));
6755409fb4eSMark Brown 	if (coeff < 0) {
6765409fb4eSMark Brown 		coeff = get_coeff(wm8988->sysclk / 2, params_rate(params));
6775409fb4eSMark Brown 		srate |= 0x40;
6785409fb4eSMark Brown 	}
6795409fb4eSMark Brown 	if (coeff < 0) {
680ab38ff38SKuninori Morimoto 		dev_err(component->dev,
6815409fb4eSMark Brown 			"Unable to configure sample rate %dHz with %dHz MCLK\n",
6825409fb4eSMark Brown 			params_rate(params), wm8988->sysclk);
6835409fb4eSMark Brown 		return coeff;
6845409fb4eSMark Brown 	}
6855409fb4eSMark Brown 
6865409fb4eSMark Brown 	/* bit size */
6878cd2ba09SMark Brown 	switch (params_width(params)) {
6888cd2ba09SMark Brown 	case 16:
6895409fb4eSMark Brown 		break;
6908cd2ba09SMark Brown 	case 20:
6915409fb4eSMark Brown 		iface |= 0x0004;
6925409fb4eSMark Brown 		break;
6938cd2ba09SMark Brown 	case 24:
6945409fb4eSMark Brown 		iface |= 0x0008;
6955409fb4eSMark Brown 		break;
6968cd2ba09SMark Brown 	case 32:
6975409fb4eSMark Brown 		iface |= 0x000c;
6985409fb4eSMark Brown 		break;
6995409fb4eSMark Brown 	}
7005409fb4eSMark Brown 
7015409fb4eSMark Brown 	/* set iface & srate */
702ab38ff38SKuninori Morimoto 	snd_soc_component_write(component, WM8988_IFACE, iface);
7035409fb4eSMark Brown 	if (coeff >= 0)
704ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_SRATE, srate |
7055409fb4eSMark Brown 			(coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
7065409fb4eSMark Brown 
7075409fb4eSMark Brown 	return 0;
7085409fb4eSMark Brown }
7095409fb4eSMark Brown 
wm8988_mute(struct snd_soc_dai * dai,int mute,int direction)71026d3c16eSKuninori Morimoto static int wm8988_mute(struct snd_soc_dai *dai, int mute, int direction)
7115409fb4eSMark Brown {
712ab38ff38SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
7136d75dfc3SKuninori Morimoto 	u16 mute_reg = snd_soc_component_read(component, WM8988_ADCDAC) & 0xfff7;
7145409fb4eSMark Brown 
7155409fb4eSMark Brown 	if (mute)
716ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_ADCDAC, mute_reg | 0x8);
7175409fb4eSMark Brown 	else
718ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_ADCDAC, mute_reg);
7195409fb4eSMark Brown 	return 0;
7205409fb4eSMark Brown }
7215409fb4eSMark Brown 
wm8988_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)722ab38ff38SKuninori Morimoto static int wm8988_set_bias_level(struct snd_soc_component *component,
7235409fb4eSMark Brown 				 enum snd_soc_bias_level level)
7245409fb4eSMark Brown {
725ab38ff38SKuninori Morimoto 	struct wm8988_priv *wm8988 = snd_soc_component_get_drvdata(component);
7266d75dfc3SKuninori Morimoto 	u16 pwr_reg = snd_soc_component_read(component, WM8988_PWR1) & ~0x1c1;
7275409fb4eSMark Brown 
7285409fb4eSMark Brown 	switch (level) {
7295409fb4eSMark Brown 	case SND_SOC_BIAS_ON:
7305409fb4eSMark Brown 		break;
7315409fb4eSMark Brown 
7325409fb4eSMark Brown 	case SND_SOC_BIAS_PREPARE:
7335409fb4eSMark Brown 		/* VREF, VMID=2x50k, digital enabled */
734ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_PWR1, pwr_reg | 0x00c0);
7355409fb4eSMark Brown 		break;
7365409fb4eSMark Brown 
7375409fb4eSMark Brown 	case SND_SOC_BIAS_STANDBY:
738ab38ff38SKuninori Morimoto 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
739d2dc0a77SMark Brown 			regcache_sync(wm8988->regmap);
740fa5fdb47SAxel Lin 
7415409fb4eSMark Brown 			/* VREF, VMID=2x5k */
742ab38ff38SKuninori Morimoto 			snd_soc_component_write(component, WM8988_PWR1, pwr_reg | 0x1c1);
7435409fb4eSMark Brown 
7445409fb4eSMark Brown 			/* Charge caps */
7455409fb4eSMark Brown 			msleep(100);
7465409fb4eSMark Brown 		}
7475409fb4eSMark Brown 
7485409fb4eSMark Brown 		/* VREF, VMID=2*500k, digital stopped */
749ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_PWR1, pwr_reg | 0x0141);
7505409fb4eSMark Brown 		break;
7515409fb4eSMark Brown 
7525409fb4eSMark Brown 	case SND_SOC_BIAS_OFF:
753ab38ff38SKuninori Morimoto 		snd_soc_component_write(component, WM8988_PWR1, 0x0000);
7545409fb4eSMark Brown 		break;
7555409fb4eSMark Brown 	}
7565409fb4eSMark Brown 	return 0;
7575409fb4eSMark Brown }
7585409fb4eSMark Brown 
7595409fb4eSMark Brown #define WM8988_RATES SNDRV_PCM_RATE_8000_96000
7605409fb4eSMark Brown 
7615409fb4eSMark Brown #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
7625409fb4eSMark Brown 	SNDRV_PCM_FMTBIT_S24_LE)
7635409fb4eSMark Brown 
76485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8988_ops = {
7655409fb4eSMark Brown 	.startup = wm8988_pcm_startup,
7665409fb4eSMark Brown 	.hw_params = wm8988_pcm_hw_params,
7675409fb4eSMark Brown 	.set_fmt = wm8988_set_dai_fmt,
7685409fb4eSMark Brown 	.set_sysclk = wm8988_set_dai_sysclk,
76926d3c16eSKuninori Morimoto 	.mute_stream = wm8988_mute,
77026d3c16eSKuninori Morimoto 	.no_capture_mute = 1,
7715409fb4eSMark Brown };
7725409fb4eSMark Brown 
773f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8988_dai = {
774f0fba2adSLiam Girdwood 	.name = "wm8988-hifi",
7755409fb4eSMark Brown 	.playback = {
7765409fb4eSMark Brown 		.stream_name = "Playback",
7775409fb4eSMark Brown 		.channels_min = 1,
7785409fb4eSMark Brown 		.channels_max = 2,
7795409fb4eSMark Brown 		.rates = WM8988_RATES,
7805409fb4eSMark Brown 		.formats = WM8988_FORMATS,
7815409fb4eSMark Brown 	},
7825409fb4eSMark Brown 	.capture = {
7835409fb4eSMark Brown 		.stream_name = "Capture",
7845409fb4eSMark Brown 		.channels_min = 1,
7855409fb4eSMark Brown 		.channels_max = 2,
7865409fb4eSMark Brown 		.rates = WM8988_RATES,
7875409fb4eSMark Brown 		.formats = WM8988_FORMATS,
7885409fb4eSMark Brown 	 },
7895409fb4eSMark Brown 	.ops = &wm8988_ops,
79007695752SKuninori Morimoto 	.symmetric_rate = 1,
7915409fb4eSMark Brown };
7925409fb4eSMark Brown 
wm8988_probe(struct snd_soc_component * component)793ab38ff38SKuninori Morimoto static int wm8988_probe(struct snd_soc_component *component)
7945409fb4eSMark Brown {
7955409fb4eSMark Brown 	int ret = 0;
7965409fb4eSMark Brown 
797ab38ff38SKuninori Morimoto 	ret = wm8988_reset(component);
7985409fb4eSMark Brown 	if (ret < 0) {
799ab38ff38SKuninori Morimoto 		dev_err(component->dev, "Failed to issue reset\n");
800f0fba2adSLiam Girdwood 		return ret;
8015409fb4eSMark Brown 	}
8025409fb4eSMark Brown 
8035409fb4eSMark Brown 	/* set the update bits (we always update left then right) */
804ab38ff38SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8988_RADC, 0x0100, 0x0100);
805ab38ff38SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8988_RDAC, 0x0100, 0x0100);
806ab38ff38SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8988_ROUT1V, 0x0100, 0x0100);
807ab38ff38SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8988_ROUT2V, 0x0100, 0x0100);
808ab38ff38SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8988_RINVOL, 0x0100, 0x0100);
8095409fb4eSMark Brown 
810f0fba2adSLiam Girdwood 	return 0;
8115409fb4eSMark Brown }
8125409fb4eSMark Brown 
813ab38ff38SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8988 = {
814f0fba2adSLiam Girdwood 	.probe			= wm8988_probe,
815f0fba2adSLiam Girdwood 	.set_bias_level		= wm8988_set_bias_level,
816dd21353fSMark Brown 	.controls		= wm8988_snd_controls,
817dd21353fSMark Brown 	.num_controls		= ARRAY_SIZE(wm8988_snd_controls),
818dd21353fSMark Brown 	.dapm_widgets		= wm8988_dapm_widgets,
819dd21353fSMark Brown 	.num_dapm_widgets	= ARRAY_SIZE(wm8988_dapm_widgets),
820dd21353fSMark Brown 	.dapm_routes		= wm8988_dapm_routes,
821dd21353fSMark Brown 	.num_dapm_routes	= ARRAY_SIZE(wm8988_dapm_routes),
822ab38ff38SKuninori Morimoto 	.suspend_bias_off	= 1,
823ab38ff38SKuninori Morimoto 	.idle_bias_on		= 1,
824ab38ff38SKuninori Morimoto 	.use_pmdown_time	= 1,
825ab38ff38SKuninori Morimoto 	.endianness		= 1,
826f0fba2adSLiam Girdwood };
827f0fba2adSLiam Girdwood 
828b682d7baSKrzysztof Kozlowski static const struct regmap_config wm8988_regmap = {
829d2dc0a77SMark Brown 	.reg_bits = 7,
830d2dc0a77SMark Brown 	.val_bits = 9,
831d2dc0a77SMark Brown 
832d2dc0a77SMark Brown 	.max_register = WM8988_LPPB,
833d2dc0a77SMark Brown 	.writeable_reg = wm8988_writeable,
834d2dc0a77SMark Brown 
835*fb60b65aSMark Brown 	.cache_type = REGCACHE_MAPLE,
836d2dc0a77SMark Brown 	.reg_defaults = wm8988_reg_defaults,
837d2dc0a77SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8988_reg_defaults),
838d2dc0a77SMark Brown };
839d2dc0a77SMark Brown 
840f0fba2adSLiam Girdwood #if defined(CONFIG_SPI_MASTER)
wm8988_spi_probe(struct spi_device * spi)8417a79e94eSBill Pemberton static int wm8988_spi_probe(struct spi_device *spi)
8425409fb4eSMark Brown {
8435409fb4eSMark Brown 	struct wm8988_priv *wm8988;
844f0fba2adSLiam Girdwood 	int ret;
8455409fb4eSMark Brown 
84682fa3670SMark Brown 	wm8988 = devm_kzalloc(&spi->dev, sizeof(struct wm8988_priv),
84782fa3670SMark Brown 			      GFP_KERNEL);
8485409fb4eSMark Brown 	if (wm8988 == NULL)
8495409fb4eSMark Brown 		return -ENOMEM;
8505409fb4eSMark Brown 
85128517f5cSTushar Behera 	wm8988->regmap = devm_regmap_init_spi(spi, &wm8988_regmap);
852d2dc0a77SMark Brown 	if (IS_ERR(wm8988->regmap)) {
853d2dc0a77SMark Brown 		ret = PTR_ERR(wm8988->regmap);
854d2dc0a77SMark Brown 		dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
855d2dc0a77SMark Brown 		return ret;
856d2dc0a77SMark Brown 	}
857d2dc0a77SMark Brown 
858f0fba2adSLiam Girdwood 	spi_set_drvdata(spi, wm8988);
8595409fb4eSMark Brown 
860ab38ff38SKuninori Morimoto 	ret = devm_snd_soc_register_component(&spi->dev,
861ab38ff38SKuninori Morimoto 			&soc_component_dev_wm8988, &wm8988_dai, 1);
862f0fba2adSLiam Girdwood 	return ret;
8635409fb4eSMark Brown }
8645409fb4eSMark Brown 
865f0fba2adSLiam Girdwood static struct spi_driver wm8988_spi_driver = {
866f0fba2adSLiam Girdwood 	.driver = {
867091edccfSMark Brown 		.name	= "wm8988",
868f0fba2adSLiam Girdwood 	},
869f0fba2adSLiam Girdwood 	.probe		= wm8988_spi_probe,
870f0fba2adSLiam Girdwood };
871f0fba2adSLiam Girdwood #endif /* CONFIG_SPI_MASTER */
872f0fba2adSLiam Girdwood 
87363587116SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
wm8988_i2c_probe(struct i2c_client * i2c)87497b0b6e3SStephen Kitt static int wm8988_i2c_probe(struct i2c_client *i2c)
875f0fba2adSLiam Girdwood {
876f0fba2adSLiam Girdwood 	struct wm8988_priv *wm8988;
877f0fba2adSLiam Girdwood 	int ret;
878f0fba2adSLiam Girdwood 
87982fa3670SMark Brown 	wm8988 = devm_kzalloc(&i2c->dev, sizeof(struct wm8988_priv),
88082fa3670SMark Brown 			      GFP_KERNEL);
881f0fba2adSLiam Girdwood 	if (wm8988 == NULL)
882f0fba2adSLiam Girdwood 		return -ENOMEM;
883f0fba2adSLiam Girdwood 
884f0fba2adSLiam Girdwood 	i2c_set_clientdata(i2c, wm8988);
885d2dc0a77SMark Brown 
88628517f5cSTushar Behera 	wm8988->regmap = devm_regmap_init_i2c(i2c, &wm8988_regmap);
887d2dc0a77SMark Brown 	if (IS_ERR(wm8988->regmap)) {
888d2dc0a77SMark Brown 		ret = PTR_ERR(wm8988->regmap);
889d2dc0a77SMark Brown 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
890d2dc0a77SMark Brown 		return ret;
891d2dc0a77SMark Brown 	}
892f0fba2adSLiam Girdwood 
893ab38ff38SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev,
894ab38ff38SKuninori Morimoto 			&soc_component_dev_wm8988, &wm8988_dai, 1);
895f0fba2adSLiam Girdwood 	return ret;
896f0fba2adSLiam Girdwood }
897f0fba2adSLiam Girdwood 
8985409fb4eSMark Brown static const struct i2c_device_id wm8988_i2c_id[] = {
8995409fb4eSMark Brown 	{ "wm8988", 0 },
9005409fb4eSMark Brown 	{ }
9015409fb4eSMark Brown };
9025409fb4eSMark Brown MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id);
9035409fb4eSMark Brown 
9045409fb4eSMark Brown static struct i2c_driver wm8988_i2c_driver = {
9055409fb4eSMark Brown 	.driver = {
9061f5cafb2SMark Brown 		.name = "wm8988",
9075409fb4eSMark Brown 	},
9089abcd240SUwe Kleine-König 	.probe = wm8988_i2c_probe,
9095409fb4eSMark Brown 	.id_table = wm8988_i2c_id,
9105409fb4eSMark Brown };
9115409fb4eSMark Brown #endif
9125409fb4eSMark Brown 
wm8988_modinit(void)9135409fb4eSMark Brown static int __init wm8988_modinit(void)
9145409fb4eSMark Brown {
915f0fba2adSLiam Girdwood 	int ret = 0;
91663587116SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
9175409fb4eSMark Brown 	ret = i2c_add_driver(&wm8988_i2c_driver);
918f0fba2adSLiam Girdwood 	if (ret != 0) {
919f0fba2adSLiam Girdwood 		printk(KERN_ERR "Failed to register WM8988 I2C driver: %d\n",
920f0fba2adSLiam Girdwood 		       ret);
921f0fba2adSLiam Girdwood 	}
9225409fb4eSMark Brown #endif
9235409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER)
9245409fb4eSMark Brown 	ret = spi_register_driver(&wm8988_spi_driver);
925f0fba2adSLiam Girdwood 	if (ret != 0) {
926f0fba2adSLiam Girdwood 		printk(KERN_ERR "Failed to register WM8988 SPI driver: %d\n",
927f0fba2adSLiam Girdwood 		       ret);
928f0fba2adSLiam Girdwood 	}
9295409fb4eSMark Brown #endif
9305409fb4eSMark Brown 	return ret;
9315409fb4eSMark Brown }
9325409fb4eSMark Brown module_init(wm8988_modinit);
9335409fb4eSMark Brown 
wm8988_exit(void)9345409fb4eSMark Brown static void __exit wm8988_exit(void)
9355409fb4eSMark Brown {
93663587116SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
9375409fb4eSMark Brown 	i2c_del_driver(&wm8988_i2c_driver);
9385409fb4eSMark Brown #endif
9395409fb4eSMark Brown #if defined(CONFIG_SPI_MASTER)
9405409fb4eSMark Brown 	spi_unregister_driver(&wm8988_spi_driver);
9415409fb4eSMark Brown #endif
9425409fb4eSMark Brown }
9435409fb4eSMark Brown module_exit(wm8988_exit);
9445409fb4eSMark Brown 
9455409fb4eSMark Brown 
9465409fb4eSMark Brown MODULE_DESCRIPTION("ASoC WM8988 driver");
9475409fb4eSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
9485409fb4eSMark Brown MODULE_LICENSE("GPL");
949