xref: /openbmc/linux/sound/soc/codecs/wm8711.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bd6d4177SMike Arthur /*
3bd6d4177SMike Arthur  * wm8711.c  --  WM8711 ALSA SoC Audio driver
4bd6d4177SMike Arthur  *
5bd6d4177SMike Arthur  * Copyright 2006 Wolfson Microelectronics
6bd6d4177SMike Arthur  *
79a185b9aSMark Brown  * Author: Mike Arthur <Mike.Arthur@wolfsonmicro.com>
8bd6d4177SMike Arthur  *
9bd6d4177SMike Arthur  * Based on wm8731.c by Richard Purdie
10bd6d4177SMike Arthur  */
11bd6d4177SMike Arthur 
12bd6d4177SMike Arthur #include <linux/module.h>
13bd6d4177SMike Arthur #include <linux/moduleparam.h>
14bd6d4177SMike Arthur #include <linux/init.h>
15bd6d4177SMike Arthur #include <linux/delay.h>
16bd6d4177SMike Arthur #include <linux/pm.h>
17bd6d4177SMike Arthur #include <linux/i2c.h>
185aa5fa9fSMark Brown #include <linux/regmap.h>
19bb262767STakashi Iwai #include <linux/spi/spi.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
211552c8f6SMark Brown #include <linux/of_device.h>
22bd6d4177SMike Arthur #include <sound/core.h>
23bd6d4177SMike Arthur #include <sound/pcm.h>
24bd6d4177SMike Arthur #include <sound/pcm_params.h>
25bd6d4177SMike Arthur #include <sound/soc.h>
26b5ab887eSMark Brown #include <sound/tlv.h>
27bd6d4177SMike Arthur #include <sound/initval.h>
28bd6d4177SMike Arthur 
29bd6d4177SMike Arthur #include "wm8711.h"
30bd6d4177SMike Arthur 
31bd6d4177SMike Arthur /* codec private data */
32bd6d4177SMike Arthur struct wm8711_priv {
335aa5fa9fSMark Brown 	struct regmap *regmap;
34bd6d4177SMike Arthur 	unsigned int sysclk;
35bd6d4177SMike Arthur };
36bd6d4177SMike Arthur 
37bd6d4177SMike Arthur /*
38bd6d4177SMike Arthur  * wm8711 register cache
39bd6d4177SMike Arthur  * We can't read the WM8711 register space when we are
40bd6d4177SMike Arthur  * using 2 wire for device control, so we cache them instead.
41bd6d4177SMike Arthur  * There is no point in caching the reset register
42bd6d4177SMike Arthur  */
435aa5fa9fSMark Brown static const struct reg_default wm8711_reg_defaults[] = {
445aa5fa9fSMark Brown 	{ 0, 0x0079 }, { 1, 0x0079 }, { 2, 0x000a }, { 3, 0x0008 },
455aa5fa9fSMark Brown 	{ 4, 0x009f }, { 5, 0x000a }, { 6, 0x0000 }, { 7, 0x0000 },
46bd6d4177SMike Arthur };
47bd6d4177SMike Arthur 
wm8711_volatile(struct device * dev,unsigned int reg)485aa5fa9fSMark Brown static bool wm8711_volatile(struct device *dev, unsigned int reg)
495aa5fa9fSMark Brown {
505aa5fa9fSMark Brown 	switch (reg) {
515aa5fa9fSMark Brown 	case WM8711_RESET:
525aa5fa9fSMark Brown 		return true;
535aa5fa9fSMark Brown 	default:
545aa5fa9fSMark Brown 		return false;
555aa5fa9fSMark Brown 	}
565aa5fa9fSMark Brown }
575aa5fa9fSMark Brown 
5856fc4d2aSKuninori Morimoto #define wm8711_reset(c)	snd_soc_component_write(c, WM8711_RESET, 0)
59bd6d4177SMike Arthur 
60b5ab887eSMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
61b5ab887eSMark Brown 
62bd6d4177SMike Arthur static const struct snd_kcontrol_new wm8711_snd_controls[] = {
63bd6d4177SMike Arthur 
64b5ab887eSMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
65b5ab887eSMark Brown 		 0, 127, 0, out_tlv),
66bd6d4177SMike Arthur SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
67bd6d4177SMike Arthur 	7, 1, 0),
68bd6d4177SMike Arthur 
69bd6d4177SMike Arthur };
70bd6d4177SMike Arthur 
71bd6d4177SMike Arthur /* Output Mixer */
72bd6d4177SMike Arthur static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
73bd6d4177SMike Arthur SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
74bd6d4177SMike Arthur SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
75bd6d4177SMike Arthur };
76bd6d4177SMike Arthur 
77bd6d4177SMike Arthur static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
78bd6d4177SMike Arthur SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
79bd6d4177SMike Arthur 	&wm8711_output_mixer_controls[0],
80bd6d4177SMike Arthur 	ARRAY_SIZE(wm8711_output_mixer_controls)),
81bd6d4177SMike Arthur SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
82bd6d4177SMike Arthur SND_SOC_DAPM_OUTPUT("LOUT"),
83bd6d4177SMike Arthur SND_SOC_DAPM_OUTPUT("LHPOUT"),
84bd6d4177SMike Arthur SND_SOC_DAPM_OUTPUT("ROUT"),
85bd6d4177SMike Arthur SND_SOC_DAPM_OUTPUT("RHPOUT"),
86bd6d4177SMike Arthur };
87bd6d4177SMike Arthur 
8820314000SLu Guanqun static const struct snd_soc_dapm_route wm8711_intercon[] = {
89bd6d4177SMike Arthur 	/* output mixer */
90bd6d4177SMike Arthur 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
91bd6d4177SMike Arthur 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
92bd6d4177SMike Arthur 
93bd6d4177SMike Arthur 	/* outputs */
94bd6d4177SMike Arthur 	{"RHPOUT", NULL, "Output Mixer"},
95bd6d4177SMike Arthur 	{"ROUT", NULL, "Output Mixer"},
96bd6d4177SMike Arthur 	{"LHPOUT", NULL, "Output Mixer"},
97bd6d4177SMike Arthur 	{"LOUT", NULL, "Output Mixer"},
98bd6d4177SMike Arthur };
99bd6d4177SMike Arthur 
100bd6d4177SMike Arthur struct _coeff_div {
101bd6d4177SMike Arthur 	u32 mclk;
102bd6d4177SMike Arthur 	u32 rate;
103bd6d4177SMike Arthur 	u16 fs;
104bd6d4177SMike Arthur 	u8 sr:4;
105bd6d4177SMike Arthur 	u8 bosr:1;
106bd6d4177SMike Arthur 	u8 usb:1;
107bd6d4177SMike Arthur };
108bd6d4177SMike Arthur 
109bd6d4177SMike Arthur /* codec mclk clock divider coefficients */
110bd6d4177SMike Arthur static const struct _coeff_div coeff_div[] = {
111bd6d4177SMike Arthur 	/* 48k */
112bd6d4177SMike Arthur 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
113bd6d4177SMike Arthur 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
114bd6d4177SMike Arthur 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
115bd6d4177SMike Arthur 
116bd6d4177SMike Arthur 	/* 32k */
117bd6d4177SMike Arthur 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
118bd6d4177SMike Arthur 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
119bd6d4177SMike Arthur 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
120bd6d4177SMike Arthur 
121bd6d4177SMike Arthur 	/* 8k */
122bd6d4177SMike Arthur 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
123bd6d4177SMike Arthur 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
124bd6d4177SMike Arthur 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
125bd6d4177SMike Arthur 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
126bd6d4177SMike Arthur 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
127bd6d4177SMike Arthur 
128bd6d4177SMike Arthur 	/* 96k */
129bd6d4177SMike Arthur 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
130bd6d4177SMike Arthur 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
131bd6d4177SMike Arthur 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
132bd6d4177SMike Arthur 
133bd6d4177SMike Arthur 	/* 44.1k */
134bd6d4177SMike Arthur 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
135bd6d4177SMike Arthur 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
136bd6d4177SMike Arthur 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
137bd6d4177SMike Arthur 
138bd6d4177SMike Arthur 	/* 88.2k */
139bd6d4177SMike Arthur 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
140bd6d4177SMike Arthur 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
141bd6d4177SMike Arthur 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
142bd6d4177SMike Arthur };
143bd6d4177SMike Arthur 
get_coeff(int mclk,int rate)144bd6d4177SMike Arthur static inline int get_coeff(int mclk, int rate)
145bd6d4177SMike Arthur {
146bd6d4177SMike Arthur 	int i;
147bd6d4177SMike Arthur 
148bd6d4177SMike Arthur 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
149bd6d4177SMike Arthur 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
150bd6d4177SMike Arthur 			return i;
151bd6d4177SMike Arthur 	}
152bd6d4177SMike Arthur 	return 0;
153bd6d4177SMike Arthur }
154bd6d4177SMike Arthur 
wm8711_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)155bd6d4177SMike Arthur static int wm8711_hw_params(struct snd_pcm_substream *substream,
156bd6d4177SMike Arthur 	struct snd_pcm_hw_params *params,
157bd6d4177SMike Arthur 	struct snd_soc_dai *dai)
158bd6d4177SMike Arthur {
15956fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
16056fc4d2aSKuninori Morimoto 	struct wm8711_priv *wm8711 =  snd_soc_component_get_drvdata(component);
1616d75dfc3SKuninori Morimoto 	u16 iface = snd_soc_component_read(component, WM8711_IFACE) & 0xfff3;
162bd6d4177SMike Arthur 	int i = get_coeff(wm8711->sysclk, params_rate(params));
163bd6d4177SMike Arthur 	u16 srate = (coeff_div[i].sr << 2) |
164bd6d4177SMike Arthur 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
165bd6d4177SMike Arthur 
16656fc4d2aSKuninori Morimoto 	snd_soc_component_write(component, WM8711_SRATE, srate);
167bd6d4177SMike Arthur 
168bd6d4177SMike Arthur 	/* bit size */
169b9abb548SMark Brown 	switch (params_width(params)) {
170b9abb548SMark Brown 	case 16:
171bd6d4177SMike Arthur 		break;
172b9abb548SMark Brown 	case 20:
173bd6d4177SMike Arthur 		iface |= 0x0004;
174bd6d4177SMike Arthur 		break;
175b9abb548SMark Brown 	case 24:
176bd6d4177SMike Arthur 		iface |= 0x0008;
177bd6d4177SMike Arthur 		break;
178bd6d4177SMike Arthur 	}
179bd6d4177SMike Arthur 
18056fc4d2aSKuninori Morimoto 	snd_soc_component_write(component, WM8711_IFACE, iface);
181bd6d4177SMike Arthur 	return 0;
182bd6d4177SMike Arthur }
183bd6d4177SMike Arthur 
wm8711_pcm_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)184bd6d4177SMike Arthur static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
185bd6d4177SMike Arthur 			      struct snd_soc_dai *dai)
186bd6d4177SMike Arthur {
18756fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
188bd6d4177SMike Arthur 
189bd6d4177SMike Arthur 	/* set active */
19056fc4d2aSKuninori Morimoto 	snd_soc_component_write(component, WM8711_ACTIVE, 0x0001);
191bd6d4177SMike Arthur 
192bd6d4177SMike Arthur 	return 0;
193bd6d4177SMike Arthur }
194bd6d4177SMike Arthur 
wm8711_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)195bd6d4177SMike Arthur static void wm8711_shutdown(struct snd_pcm_substream *substream,
196bd6d4177SMike Arthur 			    struct snd_soc_dai *dai)
197bd6d4177SMike Arthur {
19856fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
199bd6d4177SMike Arthur 
200bd6d4177SMike Arthur 	/* deactivate */
2015e518eddSKuninori Morimoto 	if (!snd_soc_component_active(component)) {
202bd6d4177SMike Arthur 		udelay(50);
20356fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_ACTIVE, 0x0);
204bd6d4177SMike Arthur 	}
205bd6d4177SMike Arthur }
206bd6d4177SMike Arthur 
wm8711_mute(struct snd_soc_dai * dai,int mute,int direction)20726d3c16eSKuninori Morimoto static int wm8711_mute(struct snd_soc_dai *dai, int mute, int direction)
208bd6d4177SMike Arthur {
20956fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
2106d75dfc3SKuninori Morimoto 	u16 mute_reg = snd_soc_component_read(component, WM8711_APDIGI) & 0xfff7;
211bd6d4177SMike Arthur 
212bd6d4177SMike Arthur 	if (mute)
21356fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_APDIGI, mute_reg | 0x8);
214bd6d4177SMike Arthur 	else
21556fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_APDIGI, mute_reg);
216bd6d4177SMike Arthur 
217bd6d4177SMike Arthur 	return 0;
218bd6d4177SMike Arthur }
219bd6d4177SMike Arthur 
wm8711_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)220bd6d4177SMike Arthur static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
221bd6d4177SMike Arthur 		int clk_id, unsigned int freq, int dir)
222bd6d4177SMike Arthur {
22356fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
22456fc4d2aSKuninori Morimoto 	struct wm8711_priv *wm8711 =  snd_soc_component_get_drvdata(component);
225bd6d4177SMike Arthur 
226bd6d4177SMike Arthur 	switch (freq) {
227bd6d4177SMike Arthur 	case 11289600:
228bd6d4177SMike Arthur 	case 12000000:
229bd6d4177SMike Arthur 	case 12288000:
230bd6d4177SMike Arthur 	case 16934400:
231bd6d4177SMike Arthur 	case 18432000:
232bd6d4177SMike Arthur 		wm8711->sysclk = freq;
233bd6d4177SMike Arthur 		return 0;
234bd6d4177SMike Arthur 	}
235bd6d4177SMike Arthur 	return -EINVAL;
236bd6d4177SMike Arthur }
237bd6d4177SMike Arthur 
wm8711_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int fmt)238bd6d4177SMike Arthur static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
239bd6d4177SMike Arthur 		unsigned int fmt)
240bd6d4177SMike Arthur {
24156fc4d2aSKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
2426d75dfc3SKuninori Morimoto 	u16 iface = snd_soc_component_read(component, WM8711_IFACE) & 0x000c;
243bd6d4177SMike Arthur 
244bd6d4177SMike Arthur 	/* set master/slave audio interface */
245bd6d4177SMike Arthur 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
246bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_CBM_CFM:
247bd6d4177SMike Arthur 		iface |= 0x0040;
248bd6d4177SMike Arthur 		break;
249bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_CBS_CFS:
250bd6d4177SMike Arthur 		break;
251bd6d4177SMike Arthur 	default:
252bd6d4177SMike Arthur 		return -EINVAL;
253bd6d4177SMike Arthur 	}
254bd6d4177SMike Arthur 
255bd6d4177SMike Arthur 	/* interface format */
256bd6d4177SMike Arthur 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
257bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_I2S:
258bd6d4177SMike Arthur 		iface |= 0x0002;
259bd6d4177SMike Arthur 		break;
260bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_RIGHT_J:
261bd6d4177SMike Arthur 		break;
262bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_LEFT_J:
263bd6d4177SMike Arthur 		iface |= 0x0001;
264bd6d4177SMike Arthur 		break;
265bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_DSP_A:
266bd6d4177SMike Arthur 		iface |= 0x0003;
267bd6d4177SMike Arthur 		break;
268bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_DSP_B:
269bd6d4177SMike Arthur 		iface |= 0x0013;
270bd6d4177SMike Arthur 		break;
271bd6d4177SMike Arthur 	default:
272bd6d4177SMike Arthur 		return -EINVAL;
273bd6d4177SMike Arthur 	}
274bd6d4177SMike Arthur 
275bd6d4177SMike Arthur 	/* clock inversion */
276bd6d4177SMike Arthur 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
277bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_NB_NF:
278bd6d4177SMike Arthur 		break;
279bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_IB_IF:
280bd6d4177SMike Arthur 		iface |= 0x0090;
281bd6d4177SMike Arthur 		break;
282bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_IB_NF:
283bd6d4177SMike Arthur 		iface |= 0x0080;
284bd6d4177SMike Arthur 		break;
285bd6d4177SMike Arthur 	case SND_SOC_DAIFMT_NB_IF:
286bd6d4177SMike Arthur 		iface |= 0x0010;
287bd6d4177SMike Arthur 		break;
288bd6d4177SMike Arthur 	default:
289bd6d4177SMike Arthur 		return -EINVAL;
290bd6d4177SMike Arthur 	}
291bd6d4177SMike Arthur 
292bd6d4177SMike Arthur 	/* set iface */
29356fc4d2aSKuninori Morimoto 	snd_soc_component_write(component, WM8711_IFACE, iface);
294bd6d4177SMike Arthur 	return 0;
295bd6d4177SMike Arthur }
296bd6d4177SMike Arthur 
wm8711_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)29756fc4d2aSKuninori Morimoto static int wm8711_set_bias_level(struct snd_soc_component *component,
298bd6d4177SMike Arthur 	enum snd_soc_bias_level level)
299bd6d4177SMike Arthur {
30056fc4d2aSKuninori Morimoto 	struct wm8711_priv *wm8711 = snd_soc_component_get_drvdata(component);
3016d75dfc3SKuninori Morimoto 	u16 reg = snd_soc_component_read(component, WM8711_PWR) & 0xff7f;
302bd6d4177SMike Arthur 
303bd6d4177SMike Arthur 	switch (level) {
304bd6d4177SMike Arthur 	case SND_SOC_BIAS_ON:
30556fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_PWR, reg);
306bd6d4177SMike Arthur 		break;
307bd6d4177SMike Arthur 	case SND_SOC_BIAS_PREPARE:
308bd6d4177SMike Arthur 		break;
309bd6d4177SMike Arthur 	case SND_SOC_BIAS_STANDBY:
31056fc4d2aSKuninori Morimoto 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
3115aa5fa9fSMark Brown 			regcache_sync(wm8711->regmap);
312960622daSAxel Lin 
31356fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_PWR, reg | 0x0040);
314bd6d4177SMike Arthur 		break;
315bd6d4177SMike Arthur 	case SND_SOC_BIAS_OFF:
31656fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_ACTIVE, 0x0);
31756fc4d2aSKuninori Morimoto 		snd_soc_component_write(component, WM8711_PWR, 0xffff);
318bd6d4177SMike Arthur 		break;
319bd6d4177SMike Arthur 	}
320bd6d4177SMike Arthur 	return 0;
321bd6d4177SMike Arthur }
322bd6d4177SMike Arthur 
323431f7771SMark Brown #define WM8711_RATES SNDRV_PCM_RATE_8000_96000
324bd6d4177SMike Arthur 
325bd6d4177SMike Arthur #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
326bd6d4177SMike Arthur 	SNDRV_PCM_FMTBIT_S24_LE)
327bd6d4177SMike Arthur 
32885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8711_ops = {
329bd6d4177SMike Arthur 	.prepare = wm8711_pcm_prepare,
330bd6d4177SMike Arthur 	.hw_params = wm8711_hw_params,
331bd6d4177SMike Arthur 	.shutdown = wm8711_shutdown,
33226d3c16eSKuninori Morimoto 	.mute_stream = wm8711_mute,
333bd6d4177SMike Arthur 	.set_sysclk = wm8711_set_dai_sysclk,
334bd6d4177SMike Arthur 	.set_fmt = wm8711_set_dai_fmt,
33526d3c16eSKuninori Morimoto 	.no_capture_mute = 1,
336bd6d4177SMike Arthur };
337bd6d4177SMike Arthur 
338f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8711_dai = {
339f0fba2adSLiam Girdwood 	.name = "wm8711-hifi",
340bd6d4177SMike Arthur 	.playback = {
341bd6d4177SMike Arthur 		.stream_name = "Playback",
342bd6d4177SMike Arthur 		.channels_min = 1,
343bd6d4177SMike Arthur 		.channels_max = 2,
344bd6d4177SMike Arthur 		.rates = WM8711_RATES,
345431f7771SMark Brown 		.formats = WM8711_FORMATS,
346431f7771SMark Brown 	},
347bd6d4177SMike Arthur 	.ops = &wm8711_ops,
348bd6d4177SMike Arthur };
349bd6d4177SMike Arthur 
wm8711_probe(struct snd_soc_component * component)35056fc4d2aSKuninori Morimoto static int wm8711_probe(struct snd_soc_component *component)
351bd6d4177SMike Arthur {
352d434bc32SAxel Lin 	int ret;
353bd6d4177SMike Arthur 
35456fc4d2aSKuninori Morimoto 	ret = wm8711_reset(component);
355318b0b8dSMark Brown 	if (ret < 0) {
35656fc4d2aSKuninori Morimoto 		dev_err(component->dev, "Failed to issue reset\n");
357f0fba2adSLiam Girdwood 		return ret;
358318b0b8dSMark Brown 	}
359318b0b8dSMark Brown 
360318b0b8dSMark Brown 	/* Latch the update bits */
36156fc4d2aSKuninori Morimoto 	snd_soc_component_update_bits(component, WM8711_LOUT1V, 0x0100, 0x0100);
36256fc4d2aSKuninori Morimoto 	snd_soc_component_update_bits(component, WM8711_ROUT1V, 0x0100, 0x0100);
363318b0b8dSMark Brown 
364d97d2e35SMark Brown 	return ret;
365f0fba2adSLiam Girdwood 
366318b0b8dSMark Brown }
367318b0b8dSMark Brown 
36856fc4d2aSKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8711 = {
369f0fba2adSLiam Girdwood 	.probe			= wm8711_probe,
370f0fba2adSLiam Girdwood 	.set_bias_level		= wm8711_set_bias_level,
3712f5374d8SMark Brown 	.controls		= wm8711_snd_controls,
3722f5374d8SMark Brown 	.num_controls		= ARRAY_SIZE(wm8711_snd_controls),
37320314000SLu Guanqun 	.dapm_widgets		= wm8711_dapm_widgets,
37420314000SLu Guanqun 	.num_dapm_widgets	= ARRAY_SIZE(wm8711_dapm_widgets),
37520314000SLu Guanqun 	.dapm_routes		= wm8711_intercon,
37620314000SLu Guanqun 	.num_dapm_routes	= ARRAY_SIZE(wm8711_intercon),
37756fc4d2aSKuninori Morimoto 	.suspend_bias_off	= 1,
37856fc4d2aSKuninori Morimoto 	.idle_bias_on		= 1,
37956fc4d2aSKuninori Morimoto 	.use_pmdown_time	= 1,
38056fc4d2aSKuninori Morimoto 	.endianness		= 1,
381f0fba2adSLiam Girdwood };
382f0fba2adSLiam Girdwood 
3831552c8f6SMark Brown static const struct of_device_id wm8711_of_match[] = {
3841552c8f6SMark Brown 	{ .compatible = "wlf,wm8711", },
3851552c8f6SMark Brown 	{ }
3861552c8f6SMark Brown };
3871552c8f6SMark Brown MODULE_DEVICE_TABLE(of, wm8711_of_match);
3881552c8f6SMark Brown 
3895aa5fa9fSMark Brown static const struct regmap_config wm8711_regmap = {
3905aa5fa9fSMark Brown 	.reg_bits = 7,
3915aa5fa9fSMark Brown 	.val_bits = 9,
3925aa5fa9fSMark Brown 	.max_register = WM8711_RESET,
3935aa5fa9fSMark Brown 
3945aa5fa9fSMark Brown 	.reg_defaults = wm8711_reg_defaults,
3955aa5fa9fSMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8711_reg_defaults),
396*368a233bSMark Brown 	.cache_type = REGCACHE_MAPLE,
3975aa5fa9fSMark Brown 
3985aa5fa9fSMark Brown 	.volatile_reg = wm8711_volatile,
3995aa5fa9fSMark Brown };
4005aa5fa9fSMark Brown 
40108aff8cdSMark Brown #if defined(CONFIG_SPI_MASTER)
wm8711_spi_probe(struct spi_device * spi)4027a79e94eSBill Pemberton static int wm8711_spi_probe(struct spi_device *spi)
40308aff8cdSMark Brown {
40408aff8cdSMark Brown 	struct wm8711_priv *wm8711;
405f0fba2adSLiam Girdwood 	int ret;
40608aff8cdSMark Brown 
407e908ef40SMark Brown 	wm8711 = devm_kzalloc(&spi->dev, sizeof(struct wm8711_priv),
408e908ef40SMark Brown 			      GFP_KERNEL);
40908aff8cdSMark Brown 	if (wm8711 == NULL)
41008aff8cdSMark Brown 		return -ENOMEM;
41108aff8cdSMark Brown 
4125aa5fa9fSMark Brown 	wm8711->regmap = devm_regmap_init_spi(spi, &wm8711_regmap);
4135aa5fa9fSMark Brown 	if (IS_ERR(wm8711->regmap))
4145aa5fa9fSMark Brown 		return PTR_ERR(wm8711->regmap);
4155aa5fa9fSMark Brown 
416f0fba2adSLiam Girdwood 	spi_set_drvdata(spi, wm8711);
41708aff8cdSMark Brown 
41856fc4d2aSKuninori Morimoto 	ret = devm_snd_soc_register_component(&spi->dev,
41956fc4d2aSKuninori Morimoto 			&soc_component_dev_wm8711, &wm8711_dai, 1);
420e908ef40SMark Brown 
421f0fba2adSLiam Girdwood 	return ret;
42208aff8cdSMark Brown }
42308aff8cdSMark Brown 
42408aff8cdSMark Brown static struct spi_driver wm8711_spi_driver = {
42508aff8cdSMark Brown 	.driver = {
4260473e61bSMark Brown 		.name	= "wm8711",
4271552c8f6SMark Brown 		.of_match_table = wm8711_of_match,
42808aff8cdSMark Brown 	},
42908aff8cdSMark Brown 	.probe		= wm8711_spi_probe,
43008aff8cdSMark Brown };
43108aff8cdSMark Brown #endif /* CONFIG_SPI_MASTER */
43208aff8cdSMark Brown 
4332309d675SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
wm8711_i2c_probe(struct i2c_client * client)43497b0b6e3SStephen Kitt static int wm8711_i2c_probe(struct i2c_client *client)
435318b0b8dSMark Brown {
436318b0b8dSMark Brown 	struct wm8711_priv *wm8711;
437f0fba2adSLiam Girdwood 	int ret;
438318b0b8dSMark Brown 
439e908ef40SMark Brown 	wm8711 = devm_kzalloc(&client->dev, sizeof(struct wm8711_priv),
440e908ef40SMark Brown 			      GFP_KERNEL);
441318b0b8dSMark Brown 	if (wm8711 == NULL)
442318b0b8dSMark Brown 		return -ENOMEM;
443318b0b8dSMark Brown 
4445aa5fa9fSMark Brown 	wm8711->regmap = devm_regmap_init_i2c(client, &wm8711_regmap);
4455aa5fa9fSMark Brown 	if (IS_ERR(wm8711->regmap))
4465aa5fa9fSMark Brown 		return PTR_ERR(wm8711->regmap);
4475aa5fa9fSMark Brown 
448f0fba2adSLiam Girdwood 	i2c_set_clientdata(client, wm8711);
449318b0b8dSMark Brown 
45056fc4d2aSKuninori Morimoto 	ret = devm_snd_soc_register_component(&client->dev,
45156fc4d2aSKuninori Morimoto 			&soc_component_dev_wm8711, &wm8711_dai, 1);
452e908ef40SMark Brown 
453f0fba2adSLiam Girdwood 	return ret;
454318b0b8dSMark Brown }
455318b0b8dSMark Brown 
456318b0b8dSMark Brown static const struct i2c_device_id wm8711_i2c_id[] = {
457318b0b8dSMark Brown 	{ "wm8711", 0 },
458318b0b8dSMark Brown 	{ }
459318b0b8dSMark Brown };
460318b0b8dSMark Brown MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
461318b0b8dSMark Brown 
462318b0b8dSMark Brown static struct i2c_driver wm8711_i2c_driver = {
463318b0b8dSMark Brown 	.driver = {
4640473e61bSMark Brown 		.name = "wm8711",
4651552c8f6SMark Brown 		.of_match_table = wm8711_of_match,
466318b0b8dSMark Brown 	},
4679abcd240SUwe Kleine-König 	.probe = wm8711_i2c_probe,
468318b0b8dSMark Brown 	.id_table = wm8711_i2c_id,
469318b0b8dSMark Brown };
470318b0b8dSMark Brown #endif
471318b0b8dSMark Brown 
wm8711_modinit(void)472bd6d4177SMike Arthur static int __init wm8711_modinit(void)
473bd6d4177SMike Arthur {
474318b0b8dSMark Brown 	int ret;
4752309d675SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
476318b0b8dSMark Brown 	ret = i2c_add_driver(&wm8711_i2c_driver);
477318b0b8dSMark Brown 	if (ret != 0) {
478318b0b8dSMark Brown 		printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
479318b0b8dSMark Brown 		       ret);
480318b0b8dSMark Brown 	}
481318b0b8dSMark Brown #endif
48208aff8cdSMark Brown #if defined(CONFIG_SPI_MASTER)
483bb262767STakashi Iwai 	ret = spi_register_driver(&wm8711_spi_driver);
48408aff8cdSMark Brown 	if (ret != 0) {
485bb262767STakashi Iwai 		printk(KERN_ERR "Failed to register WM8711 SPI driver: %d\n",
48608aff8cdSMark Brown 		       ret);
48708aff8cdSMark Brown 	}
48808aff8cdSMark Brown #endif
489318b0b8dSMark Brown 	return 0;
490bd6d4177SMike Arthur }
491bd6d4177SMike Arthur module_init(wm8711_modinit);
492bd6d4177SMike Arthur 
wm8711_exit(void)493bd6d4177SMike Arthur static void __exit wm8711_exit(void)
494bd6d4177SMike Arthur {
4952309d675SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
496318b0b8dSMark Brown 	i2c_del_driver(&wm8711_i2c_driver);
497318b0b8dSMark Brown #endif
49808aff8cdSMark Brown #if defined(CONFIG_SPI_MASTER)
499bb262767STakashi Iwai 	spi_unregister_driver(&wm8711_spi_driver);
50008aff8cdSMark Brown #endif
501bd6d4177SMike Arthur }
502bd6d4177SMike Arthur module_exit(wm8711_exit);
503bd6d4177SMike Arthur 
504bd6d4177SMike Arthur MODULE_DESCRIPTION("ASoC WM8711 driver");
505bd6d4177SMike Arthur MODULE_AUTHOR("Mike Arthur");
506bd6d4177SMike Arthur MODULE_LICENSE("GPL");
507