xref: /linux/sound/soc/codecs/wm8731.c (revision 3f4fb905510911f6149593a7321ae1825259b242)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
240e0aa64SRichard Purdie /*
340e0aa64SRichard Purdie  * wm8731.c  --  WM8731 ALSA SoC Audio driver
440e0aa64SRichard Purdie  *
540e0aa64SRichard Purdie  * Copyright 2005 Openedhand Ltd.
6656baaebSMark Brown  * Copyright 2006-12 Wolfson Microelectronics, plc
740e0aa64SRichard Purdie  *
840e0aa64SRichard Purdie  * Author: Richard Purdie <richard@openedhand.com>
940e0aa64SRichard Purdie  *
1040e0aa64SRichard Purdie  * Based on wm8753.c by Liam Girdwood
1140e0aa64SRichard Purdie  */
1240e0aa64SRichard Purdie 
1340e0aa64SRichard Purdie #include <linux/module.h>
1440e0aa64SRichard Purdie #include <linux/moduleparam.h>
1540e0aa64SRichard Purdie #include <linux/init.h>
1640e0aa64SRichard Purdie #include <linux/delay.h>
1740e0aa64SRichard Purdie #include <linux/pm.h>
1840e0aa64SRichard Purdie #include <linux/i2c.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
2005d448e2SMark Brown #include <linux/regmap.h>
217dea7c01SMark Brown #include <linux/regulator/consumer.h>
22d2a40355SCliff Cai #include <linux/spi/spi.h>
23a7f96e4dSMark Brown #include <linux/of_device.h>
24a51ff30fSLars-Peter Clausen #include <linux/mutex.h>
2599d42234SSongjun Wu #include <linux/clk.h>
2640e0aa64SRichard Purdie #include <sound/core.h>
2740e0aa64SRichard Purdie #include <sound/pcm.h>
2840e0aa64SRichard Purdie #include <sound/pcm_params.h>
2940e0aa64SRichard Purdie #include <sound/soc.h>
3040e0aa64SRichard Purdie #include <sound/initval.h>
31d00efa64SMark Brown #include <sound/tlv.h>
3240e0aa64SRichard Purdie 
3340e0aa64SRichard Purdie #include "wm8731.h"
3440e0aa64SRichard Purdie 
357dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4
367dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
377dea7c01SMark Brown 	"AVDD",
387dea7c01SMark Brown 	"HPVDD",
397dea7c01SMark Brown 	"DCVDD",
407dea7c01SMark Brown 	"DBVDD",
417dea7c01SMark Brown };
427dea7c01SMark Brown 
43b36d61d4SFrank Mandarino /* codec private data */
44b36d61d4SFrank Mandarino struct wm8731_priv {
4505d448e2SMark Brown 	struct regmap *regmap;
4699d42234SSongjun Wu 	struct clk *mclk;
477dea7c01SMark Brown 	struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
480890c2b7SRichard Genoud 	const struct snd_pcm_hw_constraint_list *constraints;
49b36d61d4SFrank Mandarino 	unsigned int sysclk;
509745e824SMark Brown 	int sysclk_type;
51dd31b310SMark Brown 	int playback_fs;
52dd31b310SMark Brown 	bool deemph;
53a51ff30fSLars-Peter Clausen 
54a51ff30fSLars-Peter Clausen 	struct mutex lock;
55b36d61d4SFrank Mandarino };
56b36d61d4SFrank Mandarino 
57a8035c8fSMark Brown 
5840e0aa64SRichard Purdie /*
5940e0aa64SRichard Purdie  * wm8731 register cache
6040e0aa64SRichard Purdie  */
6105d448e2SMark Brown static const struct reg_default wm8731_reg_defaults[] = {
6205d448e2SMark Brown 	{ 0, 0x0097 },
6305d448e2SMark Brown 	{ 1, 0x0097 },
6405d448e2SMark Brown 	{ 2, 0x0079 },
6505d448e2SMark Brown 	{ 3, 0x0079 },
6605d448e2SMark Brown 	{ 4, 0x000a },
6705d448e2SMark Brown 	{ 5, 0x0008 },
6805d448e2SMark Brown 	{ 6, 0x009f },
6905d448e2SMark Brown 	{ 7, 0x000a },
7005d448e2SMark Brown 	{ 8, 0x0000 },
7105d448e2SMark Brown 	{ 9, 0x0000 },
7240e0aa64SRichard Purdie };
7340e0aa64SRichard Purdie 
7405d448e2SMark Brown static bool wm8731_volatile(struct device *dev, unsigned int reg)
7505d448e2SMark Brown {
7605d448e2SMark Brown 	return reg == WM8731_RESET;
7705d448e2SMark Brown }
7805d448e2SMark Brown 
796702dfccSSergey Kiselev #define wm8731_reset(m)	regmap_write(m, WM8731_RESET, 0)
8040e0aa64SRichard Purdie 
8140e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
8259f72970SMark Brown 
839e74b14aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
849e74b14aSTakashi Iwai 			    WM8731_APANA, 2, wm8731_input_select);
8559f72970SMark Brown 
86dd31b310SMark Brown static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
8759f72970SMark Brown 
88cde596c7SKuninori Morimoto static int wm8731_set_deemph(struct snd_soc_component *component)
89dd31b310SMark Brown {
90cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
91dd31b310SMark Brown 	int val, i, best;
92dd31b310SMark Brown 
93dd31b310SMark Brown 	/* If we're using deemphasis select the nearest available sample
94dd31b310SMark Brown 	 * rate.
95dd31b310SMark Brown 	 */
96dd31b310SMark Brown 	if (wm8731->deemph) {
97dd31b310SMark Brown 		best = 1;
98dd31b310SMark Brown 		for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
99dd31b310SMark Brown 			if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
100dd31b310SMark Brown 			    abs(wm8731_deemph[best] - wm8731->playback_fs))
101dd31b310SMark Brown 				best = i;
102dd31b310SMark Brown 		}
103dd31b310SMark Brown 
104dd31b310SMark Brown 		val = best << 1;
105dd31b310SMark Brown 	} else {
106dd31b310SMark Brown 		best = 0;
107dd31b310SMark Brown 		val = 0;
108dd31b310SMark Brown 	}
109dd31b310SMark Brown 
110cde596c7SKuninori Morimoto 	dev_dbg(component->dev, "Set deemphasis %d (%dHz)\n",
111dd31b310SMark Brown 		best, wm8731_deemph[best]);
112dd31b310SMark Brown 
113cde596c7SKuninori Morimoto 	return snd_soc_component_update_bits(component, WM8731_APDIGI, 0x6, val);
114dd31b310SMark Brown }
115dd31b310SMark Brown 
116dd31b310SMark Brown static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
117dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
118dd31b310SMark Brown {
119cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
120cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
121dd31b310SMark Brown 
122bd14016fSTakashi Iwai 	ucontrol->value.integer.value[0] = wm8731->deemph;
123dd31b310SMark Brown 
124dd31b310SMark Brown 	return 0;
125dd31b310SMark Brown }
126dd31b310SMark Brown 
127dd31b310SMark Brown static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
128dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
129dd31b310SMark Brown {
130cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
131cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
132523bade2SDan Carpenter 	unsigned int deemph = ucontrol->value.integer.value[0];
133dd31b310SMark Brown 	int ret = 0;
134dd31b310SMark Brown 
135dd31b310SMark Brown 	if (deemph > 1)
136dd31b310SMark Brown 		return -EINVAL;
137dd31b310SMark Brown 
138a51ff30fSLars-Peter Clausen 	mutex_lock(&wm8731->lock);
139dd31b310SMark Brown 	if (wm8731->deemph != deemph) {
140dd31b310SMark Brown 		wm8731->deemph = deemph;
141dd31b310SMark Brown 
142cde596c7SKuninori Morimoto 		wm8731_set_deemph(component);
143dd31b310SMark Brown 
144dd31b310SMark Brown 		ret = 1;
145dd31b310SMark Brown 	}
146a51ff30fSLars-Peter Clausen 	mutex_unlock(&wm8731->lock);
147dd31b310SMark Brown 
148dd31b310SMark Brown 	return ret;
149dd31b310SMark Brown }
15040e0aa64SRichard Purdie 
151d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
152d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
153d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
154d921184eSMark Brown static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
155d00efa64SMark Brown 
15640e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
15740e0aa64SRichard Purdie 
158d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
159d00efa64SMark Brown 		 0, 127, 0, out_tlv),
160bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
161bd903b6eSLiam Girdwood 	7, 1, 0),
16240e0aa64SRichard Purdie 
163d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
164d00efa64SMark Brown 		 in_tlv),
16540e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
16640e0aa64SRichard Purdie 
167d921184eSMark Brown SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
168ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
16940e0aa64SRichard Purdie 
170d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
171d00efa64SMark Brown 	       sidetone_tlv),
17240e0aa64SRichard Purdie 
17340e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
17440e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
17540e0aa64SRichard Purdie 
176dd31b310SMark Brown SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
177dd31b310SMark Brown 		    wm8731_get_deemph, wm8731_put_deemph),
17840e0aa64SRichard Purdie };
17940e0aa64SRichard Purdie 
18040e0aa64SRichard Purdie /* Output Mixer */
18140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
18240e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
18340e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
18440e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
18540e0aa64SRichard Purdie };
18640e0aa64SRichard Purdie 
18740e0aa64SRichard Purdie /* Input mux */
18840e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
18959f72970SMark Brown SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
19040e0aa64SRichard Purdie 
19140e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
1928a27bd9aSMark Brown SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
1939745e824SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
19440e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
19540e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
19640e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
19740e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
19840e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
19940e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
20040e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
20140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
20240e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
20340e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
20440e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
20540e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
20640e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
20740e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
20840e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
20940e0aa64SRichard Purdie };
21040e0aa64SRichard Purdie 
2119745e824SMark Brown static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
2129745e824SMark Brown 			    struct snd_soc_dapm_widget *sink)
2139745e824SMark Brown {
214cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
215cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
2169745e824SMark Brown 
2175a195b44SNicolas Ferre 	return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
2189745e824SMark Brown }
2199745e824SMark Brown 
2205e251aecSMark Brown static const struct snd_soc_dapm_route wm8731_intercon[] = {
2219745e824SMark Brown 	{"DAC", NULL, "OSC", wm8731_check_osc},
2229745e824SMark Brown 	{"ADC", NULL, "OSC", wm8731_check_osc},
2238a27bd9aSMark Brown 	{"DAC", NULL, "ACTIVE"},
2248a27bd9aSMark Brown 	{"ADC", NULL, "ACTIVE"},
2259745e824SMark Brown 
22640e0aa64SRichard Purdie 	/* output mixer */
22740e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
22840e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
22940e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
23040e0aa64SRichard Purdie 
23140e0aa64SRichard Purdie 	/* outputs */
23240e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
23340e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
23440e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
23540e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
23640e0aa64SRichard Purdie 
23740e0aa64SRichard Purdie 	/* input mux */
23840e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
23940e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
24040e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
24140e0aa64SRichard Purdie 
24240e0aa64SRichard Purdie 	/* inputs */
24340e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
24440e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
24540e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
24640e0aa64SRichard Purdie };
24740e0aa64SRichard Purdie 
24840e0aa64SRichard Purdie struct _coeff_div {
24940e0aa64SRichard Purdie 	u32 mclk;
25040e0aa64SRichard Purdie 	u32 rate;
25140e0aa64SRichard Purdie 	u16 fs;
25240e0aa64SRichard Purdie 	u8 sr:4;
25340e0aa64SRichard Purdie 	u8 bosr:1;
25440e0aa64SRichard Purdie 	u8 usb:1;
25540e0aa64SRichard Purdie };
25640e0aa64SRichard Purdie 
25740e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
25840e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
25940e0aa64SRichard Purdie 	/* 48k */
26040e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
26140e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
26240e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
26340e0aa64SRichard Purdie 
26440e0aa64SRichard Purdie 	/* 32k */
26540e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
26640e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
267298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
26840e0aa64SRichard Purdie 
26940e0aa64SRichard Purdie 	/* 8k */
27040e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
27140e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
27240e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
27340e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
27440e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
27540e0aa64SRichard Purdie 
27640e0aa64SRichard Purdie 	/* 96k */
27740e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
27840e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
27940e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
28040e0aa64SRichard Purdie 
28140e0aa64SRichard Purdie 	/* 44.1k */
28240e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
28340e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
28440e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
28540e0aa64SRichard Purdie 
28640e0aa64SRichard Purdie 	/* 88.2k */
28740e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
28840e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
28940e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
29040e0aa64SRichard Purdie };
29140e0aa64SRichard Purdie 
2920890c2b7SRichard Genoud /* rates constraints */
2930890c2b7SRichard Genoud static const unsigned int wm8731_rates_12000000[] = {
2940890c2b7SRichard Genoud 	8000, 32000, 44100, 48000, 96000, 88200,
2950890c2b7SRichard Genoud };
2960890c2b7SRichard Genoud 
2970890c2b7SRichard Genoud static const unsigned int wm8731_rates_12288000_18432000[] = {
2980890c2b7SRichard Genoud 	8000, 32000, 48000, 96000,
2990890c2b7SRichard Genoud };
3000890c2b7SRichard Genoud 
3010890c2b7SRichard Genoud static const unsigned int wm8731_rates_11289600_16934400[] = {
3020890c2b7SRichard Genoud 	8000, 44100, 88200,
3030890c2b7SRichard Genoud };
3040890c2b7SRichard Genoud 
3050890c2b7SRichard Genoud static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
3060890c2b7SRichard Genoud 	.list = wm8731_rates_12000000,
3070890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12000000),
3080890c2b7SRichard Genoud };
3090890c2b7SRichard Genoud 
3100890c2b7SRichard Genoud static const
3110890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
3120890c2b7SRichard Genoud 	.list = wm8731_rates_12288000_18432000,
3130890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
3140890c2b7SRichard Genoud };
3150890c2b7SRichard Genoud 
3160890c2b7SRichard Genoud static const
3170890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
3180890c2b7SRichard Genoud 	.list = wm8731_rates_11289600_16934400,
3190890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
3200890c2b7SRichard Genoud };
3210890c2b7SRichard Genoud 
32240e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
32340e0aa64SRichard Purdie {
32440e0aa64SRichard Purdie 	int i;
32540e0aa64SRichard Purdie 
32640e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
32740e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
32840e0aa64SRichard Purdie 			return i;
32940e0aa64SRichard Purdie 	}
33040e0aa64SRichard Purdie 	return 0;
33140e0aa64SRichard Purdie }
33240e0aa64SRichard Purdie 
333b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
334dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
335dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
33640e0aa64SRichard Purdie {
337cde596c7SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
338cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
3396d75dfc3SKuninori Morimoto 	u16 iface = snd_soc_component_read(component, WM8731_IFACE) & 0xfff3;
340b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
341b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
342b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
34340e0aa64SRichard Purdie 
344dd31b310SMark Brown 	wm8731->playback_fs = params_rate(params);
345dd31b310SMark Brown 
346cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_SRATE, srate);
34740e0aa64SRichard Purdie 
348b36d61d4SFrank Mandarino 	/* bit size */
349dfb6778eSMark Brown 	switch (params_width(params)) {
350dfb6778eSMark Brown 	case 16:
351b36d61d4SFrank Mandarino 		break;
352dfb6778eSMark Brown 	case 20:
353b36d61d4SFrank Mandarino 		iface |= 0x0004;
354b36d61d4SFrank Mandarino 		break;
355dfb6778eSMark Brown 	case 24:
356b36d61d4SFrank Mandarino 		iface |= 0x0008;
357b36d61d4SFrank Mandarino 		break;
358cf5ef3a2SMatt Flax 	case 32:
359cf5ef3a2SMatt Flax 		iface |= 0x000c;
360cf5ef3a2SMatt Flax 		break;
361b36d61d4SFrank Mandarino 	}
362b36d61d4SFrank Mandarino 
363cde596c7SKuninori Morimoto 	wm8731_set_deemph(component);
364dd31b310SMark Brown 
365cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_IFACE, iface);
366b36d61d4SFrank Mandarino 	return 0;
36740e0aa64SRichard Purdie }
36840e0aa64SRichard Purdie 
36926d3c16eSKuninori Morimoto static int wm8731_mute(struct snd_soc_dai *dai, int mute, int direction)
37040e0aa64SRichard Purdie {
371cde596c7SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
3726d75dfc3SKuninori Morimoto 	u16 mute_reg = snd_soc_component_read(component, WM8731_APDIGI) & 0xfff7;
373b36d61d4SFrank Mandarino 
37440e0aa64SRichard Purdie 	if (mute)
375cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_APDIGI, mute_reg | 0x8);
37640e0aa64SRichard Purdie 	else
377cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_APDIGI, mute_reg);
37840e0aa64SRichard Purdie 	return 0;
37940e0aa64SRichard Purdie }
38040e0aa64SRichard Purdie 
381e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
382b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
383b36d61d4SFrank Mandarino {
384cde596c7SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
385cde596c7SKuninori Morimoto 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
386cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
387b36d61d4SFrank Mandarino 
3889745e824SMark Brown 	switch (clk_id) {
3899745e824SMark Brown 	case WM8731_SYSCLK_XTAL:
3909745e824SMark Brown 	case WM8731_SYSCLK_MCLK:
39199d42234SSongjun Wu 		if (wm8731->mclk && clk_set_rate(wm8731->mclk, freq))
39299d42234SSongjun Wu 			return -EINVAL;
3939745e824SMark Brown 		wm8731->sysclk_type = clk_id;
3949745e824SMark Brown 		break;
3959745e824SMark Brown 	default:
3969745e824SMark Brown 		return -EINVAL;
3979745e824SMark Brown 	}
3989745e824SMark Brown 
399b36d61d4SFrank Mandarino 	switch (freq) {
4000890c2b7SRichard Genoud 	case 0:
4010890c2b7SRichard Genoud 		wm8731->constraints = NULL;
4020890c2b7SRichard Genoud 		break;
403b36d61d4SFrank Mandarino 	case 12000000:
4040890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12000000;
4050890c2b7SRichard Genoud 		break;
406b36d61d4SFrank Mandarino 	case 12288000:
407b36d61d4SFrank Mandarino 	case 18432000:
4080890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12288000_18432000;
4090890c2b7SRichard Genoud 		break;
4100890c2b7SRichard Genoud 	case 16934400:
4110890c2b7SRichard Genoud 	case 11289600:
4120890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_11289600_16934400;
4139745e824SMark Brown 		break;
4149745e824SMark Brown 	default:
415b36d61d4SFrank Mandarino 		return -EINVAL;
416b36d61d4SFrank Mandarino 	}
417b36d61d4SFrank Mandarino 
4180890c2b7SRichard Genoud 	wm8731->sysclk = freq;
4190890c2b7SRichard Genoud 
420fc31fda6SLars-Peter Clausen 	snd_soc_dapm_sync(dapm);
4219745e824SMark Brown 
4229745e824SMark Brown 	return 0;
4239745e824SMark Brown }
4249745e824SMark Brown 
425b36d61d4SFrank Mandarino 
426e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
427b36d61d4SFrank Mandarino 		unsigned int fmt)
428b36d61d4SFrank Mandarino {
429cde596c7SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
430b36d61d4SFrank Mandarino 	u16 iface = 0;
431b36d61d4SFrank Mandarino 
43200b87e18SMark Brown 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
43300b87e18SMark Brown 	case SND_SOC_DAIFMT_CBP_CFP:
434b36d61d4SFrank Mandarino 		iface |= 0x0040;
435b36d61d4SFrank Mandarino 		break;
43600b87e18SMark Brown 	case SND_SOC_DAIFMT_CBC_CFC:
437b36d61d4SFrank Mandarino 		break;
438b36d61d4SFrank Mandarino 	default:
439b36d61d4SFrank Mandarino 		return -EINVAL;
440b36d61d4SFrank Mandarino 	}
441b36d61d4SFrank Mandarino 
442b36d61d4SFrank Mandarino 	/* interface format */
443b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
444b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
445b36d61d4SFrank Mandarino 		iface |= 0x0002;
446b36d61d4SFrank Mandarino 		break;
447b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
448b36d61d4SFrank Mandarino 		break;
449b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
450b36d61d4SFrank Mandarino 		iface |= 0x0001;
451b36d61d4SFrank Mandarino 		break;
452b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
453b4af6ef9SBo Shen 		iface |= 0x0013;
454b36d61d4SFrank Mandarino 		break;
455b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
456b4af6ef9SBo Shen 		iface |= 0x0003;
457b36d61d4SFrank Mandarino 		break;
458b36d61d4SFrank Mandarino 	default:
459b36d61d4SFrank Mandarino 		return -EINVAL;
460b36d61d4SFrank Mandarino 	}
461b36d61d4SFrank Mandarino 
462b36d61d4SFrank Mandarino 	/* clock inversion */
463b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
464b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
465b36d61d4SFrank Mandarino 		break;
466b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
467b36d61d4SFrank Mandarino 		iface |= 0x0090;
468b36d61d4SFrank Mandarino 		break;
469b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
470b36d61d4SFrank Mandarino 		iface |= 0x0080;
471b36d61d4SFrank Mandarino 		break;
472b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
473b36d61d4SFrank Mandarino 		iface |= 0x0010;
474b36d61d4SFrank Mandarino 		break;
475b36d61d4SFrank Mandarino 	default:
476b36d61d4SFrank Mandarino 		return -EINVAL;
477b36d61d4SFrank Mandarino 	}
478b36d61d4SFrank Mandarino 
479b36d61d4SFrank Mandarino 	/* set iface */
480cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_IFACE, iface);
481b36d61d4SFrank Mandarino 	return 0;
482b36d61d4SFrank Mandarino }
483b36d61d4SFrank Mandarino 
484cde596c7SKuninori Morimoto static int wm8731_set_bias_level(struct snd_soc_component *component,
4850be9898aSMark Brown 				 enum snd_soc_bias_level level)
48640e0aa64SRichard Purdie {
487cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
4889bf311feSAxel Lin 	int ret;
48922d22ee5SMark Brown 	u16 reg;
49040e0aa64SRichard Purdie 
4910be9898aSMark Brown 	switch (level) {
4920be9898aSMark Brown 	case SND_SOC_BIAS_ON:
493cef6daa9SFabio Estevam 		if (wm8731->mclk) {
494cef6daa9SFabio Estevam 			ret = clk_prepare_enable(wm8731->mclk);
495cef6daa9SFabio Estevam 			if (ret)
496cef6daa9SFabio Estevam 				return ret;
497cef6daa9SFabio Estevam 		}
49840e0aa64SRichard Purdie 		break;
4990be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
50040e0aa64SRichard Purdie 		break;
5010be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
502cde596c7SKuninori Morimoto 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
50306ae9988SMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
50406ae9988SMark Brown 						    wm8731->supplies);
50506ae9988SMark Brown 			if (ret != 0)
50606ae9988SMark Brown 				return ret;
50706ae9988SMark Brown 
50805d448e2SMark Brown 			regcache_sync(wm8731->regmap);
50906ae9988SMark Brown 		}
51006ae9988SMark Brown 
51122d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
5126d75dfc3SKuninori Morimoto 		reg = snd_soc_component_read(component, WM8731_PWR) & 0xff7f;
513cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_PWR, reg | 0x0040);
51440e0aa64SRichard Purdie 		break;
5150be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
51699d42234SSongjun Wu 		if (wm8731->mclk)
51799d42234SSongjun Wu 			clk_disable_unprepare(wm8731->mclk);
518cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_PWR, 0xffff);
51906ae9988SMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
52006ae9988SMark Brown 				       wm8731->supplies);
52105d448e2SMark Brown 		regcache_mark_dirty(wm8731->regmap);
52240e0aa64SRichard Purdie 		break;
52340e0aa64SRichard Purdie 	}
52440e0aa64SRichard Purdie 	return 0;
52540e0aa64SRichard Purdie }
52640e0aa64SRichard Purdie 
5270890c2b7SRichard Genoud static int wm8731_startup(struct snd_pcm_substream *substream,
5280890c2b7SRichard Genoud 	struct snd_soc_dai *dai)
5290890c2b7SRichard Genoud {
530cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(dai->component);
5310890c2b7SRichard Genoud 
5320890c2b7SRichard Genoud 	if (wm8731->constraints)
5330890c2b7SRichard Genoud 		snd_pcm_hw_constraint_list(substream->runtime, 0,
5340890c2b7SRichard Genoud 					   SNDRV_PCM_HW_PARAM_RATE,
5350890c2b7SRichard Genoud 					   wm8731->constraints);
5360890c2b7SRichard Genoud 
5370890c2b7SRichard Genoud 	return 0;
5380890c2b7SRichard Genoud }
5390890c2b7SRichard Genoud 
540e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
541b36d61d4SFrank Mandarino 
542b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
543cf5ef3a2SMatt Flax 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
544b36d61d4SFrank Mandarino 
54585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8731_dai_ops = {
5460890c2b7SRichard Genoud 	.startup	= wm8731_startup,
5476335d055SEric Miao 	.hw_params	= wm8731_hw_params,
54826d3c16eSKuninori Morimoto 	.mute_stream	= wm8731_mute,
5496335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
5506335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
55126d3c16eSKuninori Morimoto 	.no_capture_mute = 1,
5526335d055SEric Miao };
5536335d055SEric Miao 
554f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = {
555f0fba2adSLiam Girdwood 	.name = "wm8731-hifi",
55640e0aa64SRichard Purdie 	.playback = {
55740e0aa64SRichard Purdie 		.stream_name = "Playback",
55840e0aa64SRichard Purdie 		.channels_min = 1,
55940e0aa64SRichard Purdie 		.channels_max = 2,
560b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
561b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
56240e0aa64SRichard Purdie 	.capture = {
56340e0aa64SRichard Purdie 		.stream_name = "Capture",
56440e0aa64SRichard Purdie 		.channels_min = 1,
56540e0aa64SRichard Purdie 		.channels_max = 2,
566b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
567b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
5686335d055SEric Miao 	.ops = &wm8731_dai_ops,
56907695752SKuninori Morimoto 	.symmetric_rate = 1,
57040e0aa64SRichard Purdie };
57140e0aa64SRichard Purdie 
5726702dfccSSergey Kiselev static int wm8731_request_supplies(struct device *dev,
5736702dfccSSergey Kiselev 		struct wm8731_priv *wm8731)
57440e0aa64SRichard Purdie {
575f0fba2adSLiam Girdwood 	int ret = 0, i;
57640e0aa64SRichard Purdie 
5777dea7c01SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
5787dea7c01SMark Brown 		wm8731->supplies[i].supply = wm8731_supply_names[i];
5797dea7c01SMark Brown 
5806702dfccSSergey Kiselev 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(wm8731->supplies),
5817dea7c01SMark Brown 				 wm8731->supplies);
5827dea7c01SMark Brown 	if (ret != 0) {
5836702dfccSSergey Kiselev 		dev_err(dev, "Failed to request supplies: %d\n", ret);
584f0fba2adSLiam Girdwood 		return ret;
5857dea7c01SMark Brown 	}
5867dea7c01SMark Brown 
5877dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
5887dea7c01SMark Brown 				    wm8731->supplies);
5897dea7c01SMark Brown 	if (ret != 0) {
5906702dfccSSergey Kiselev 		dev_err(dev, "Failed to enable supplies: %d\n", ret);
5913598aad5SFabio Estevam 		return ret;
5927dea7c01SMark Brown 	}
5937dea7c01SMark Brown 
5946702dfccSSergey Kiselev 	return 0;
5956702dfccSSergey Kiselev }
5966702dfccSSergey Kiselev 
597*3f4fb905SMark Brown static const struct snd_soc_component_driver soc_component_dev_wm8731 = {
598*3f4fb905SMark Brown 	.set_bias_level		= wm8731_set_bias_level,
599*3f4fb905SMark Brown 	.controls		= wm8731_snd_controls,
600*3f4fb905SMark Brown 	.num_controls		= ARRAY_SIZE(wm8731_snd_controls),
601*3f4fb905SMark Brown 	.dapm_widgets		= wm8731_dapm_widgets,
602*3f4fb905SMark Brown 	.num_dapm_widgets	= ARRAY_SIZE(wm8731_dapm_widgets),
603*3f4fb905SMark Brown 	.dapm_routes		= wm8731_intercon,
604*3f4fb905SMark Brown 	.num_dapm_routes	= ARRAY_SIZE(wm8731_intercon),
605*3f4fb905SMark Brown 	.suspend_bias_off	= 1,
606*3f4fb905SMark Brown 	.idle_bias_on		= 1,
607*3f4fb905SMark Brown 	.use_pmdown_time	= 1,
608*3f4fb905SMark Brown 	.endianness		= 1,
609*3f4fb905SMark Brown 	.non_legacy_dai_naming	= 1,
610*3f4fb905SMark Brown };
611*3f4fb905SMark Brown 
612*3f4fb905SMark Brown static int wm8731_init(struct device *dev, struct wm8731_priv *wm8731)
6136702dfccSSergey Kiselev {
6146702dfccSSergey Kiselev 	int ret = 0;
6156702dfccSSergey Kiselev 
6166702dfccSSergey Kiselev 	ret = wm8731_reset(wm8731->regmap);
617519cf2dfSMark Brown 	if (ret < 0) {
6186702dfccSSergey Kiselev 		dev_err(dev, "Failed to issue reset: %d\n", ret);
6197dea7c01SMark Brown 		goto err_regulator_enable;
620519cf2dfSMark Brown 	}
621519cf2dfSMark Brown 
6226702dfccSSergey Kiselev 	/* Clear POWEROFF, keep everything else disabled */
6236702dfccSSergey Kiselev 	regmap_write(wm8731->regmap, WM8731_PWR, 0x7f);
6245998102bSMark Brown 
6255998102bSMark Brown 	/* Latch the update bits */
6266702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_LOUT1V, 0x100, 0);
6276702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_ROUT1V, 0x100, 0);
6286702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_LINVOL, 0x100, 0);
6296702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_RINVOL, 0x100, 0);
6305998102bSMark Brown 
631ce3bdaa8SMark Brown 	/* Disable bypass path by default */
6326702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_APANA, 0x8, 0);
633ce3bdaa8SMark Brown 
6346702dfccSSergey Kiselev 	regcache_mark_dirty(wm8731->regmap);
635fe5422fcSMark Brown 
636*3f4fb905SMark Brown 	ret = devm_snd_soc_register_component(dev,
637*3f4fb905SMark Brown 			&soc_component_dev_wm8731, &wm8731_dai, 1);
638*3f4fb905SMark Brown 	if (ret != 0) {
639*3f4fb905SMark Brown 		dev_err(dev, "Failed to register CODEC: %d\n", ret);
640*3f4fb905SMark Brown 		goto err_regulator_enable;
641*3f4fb905SMark Brown 	}
642*3f4fb905SMark Brown 
643*3f4fb905SMark Brown 	return 0;
644*3f4fb905SMark Brown 
6457dea7c01SMark Brown err_regulator_enable:
6466702dfccSSergey Kiselev 	/* Regulators will be enabled by bias management */
6477dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
648f0fba2adSLiam Girdwood 
649fe5422fcSMark Brown 	return ret;
650a8035c8fSMark Brown }
651a8035c8fSMark Brown 
652a7f96e4dSMark Brown static const struct of_device_id wm8731_of_match[] = {
653a7f96e4dSMark Brown 	{ .compatible = "wlf,wm8731", },
654a7f96e4dSMark Brown 	{ }
655a7f96e4dSMark Brown };
656a7f96e4dSMark Brown 
657a7f96e4dSMark Brown MODULE_DEVICE_TABLE(of, wm8731_of_match);
658a7f96e4dSMark Brown 
65905d448e2SMark Brown static const struct regmap_config wm8731_regmap = {
66005d448e2SMark Brown 	.reg_bits = 7,
66105d448e2SMark Brown 	.val_bits = 9,
66205d448e2SMark Brown 
66305d448e2SMark Brown 	.max_register = WM8731_RESET,
66405d448e2SMark Brown 	.volatile_reg = wm8731_volatile,
66505d448e2SMark Brown 
66605d448e2SMark Brown 	.cache_type = REGCACHE_RBTREE,
66705d448e2SMark Brown 	.reg_defaults = wm8731_reg_defaults,
66805d448e2SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
66905d448e2SMark Brown };
67005d448e2SMark Brown 
6715998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
6727a79e94eSBill Pemberton static int wm8731_spi_probe(struct spi_device *spi)
6735998102bSMark Brown {
6745998102bSMark Brown 	struct wm8731_priv *wm8731;
675f0fba2adSLiam Girdwood 	int ret;
6765998102bSMark Brown 
677cea82d8aSFabio Estevam 	wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL);
6785998102bSMark Brown 	if (wm8731 == NULL)
6795998102bSMark Brown 		return -ENOMEM;
6805998102bSMark Brown 
68199d42234SSongjun Wu 	wm8731->mclk = devm_clk_get(&spi->dev, "mclk");
68299d42234SSongjun Wu 	if (IS_ERR(wm8731->mclk)) {
68399d42234SSongjun Wu 		ret = PTR_ERR(wm8731->mclk);
68499d42234SSongjun Wu 		if (ret == -ENOENT) {
68599d42234SSongjun Wu 			wm8731->mclk = NULL;
68699d42234SSongjun Wu 			dev_warn(&spi->dev, "Assuming static MCLK\n");
68799d42234SSongjun Wu 		} else {
68899d42234SSongjun Wu 			dev_err(&spi->dev, "Failed to get MCLK: %d\n",
68999d42234SSongjun Wu 				ret);
69099d42234SSongjun Wu 			return ret;
69199d42234SSongjun Wu 		}
69299d42234SSongjun Wu 	}
69399d42234SSongjun Wu 
694a51ff30fSLars-Peter Clausen 	mutex_init(&wm8731->lock);
695a51ff30fSLars-Peter Clausen 
6966702dfccSSergey Kiselev 	spi_set_drvdata(spi, wm8731);
6976702dfccSSergey Kiselev 
6986702dfccSSergey Kiselev 	ret = wm8731_request_supplies(&spi->dev, wm8731);
6996702dfccSSergey Kiselev 	if (ret != 0)
7006702dfccSSergey Kiselev 		return ret;
7016702dfccSSergey Kiselev 
702f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
70305d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
70405d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
70505d448e2SMark Brown 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
70605d448e2SMark Brown 			ret);
707f1992ddeSMark Brown 		return ret;
70805d448e2SMark Brown 	}
70905d448e2SMark Brown 
710*3f4fb905SMark Brown 	return wm8731_init(&spi->dev, wm8731);
7115998102bSMark Brown }
7125998102bSMark Brown 
7135998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
7145998102bSMark Brown 	.driver = {
71599b59f3cSMark Brown 		.name	= "wm8731",
716a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
7175998102bSMark Brown 	},
7185998102bSMark Brown 	.probe		= wm8731_spi_probe,
7195998102bSMark Brown };
720a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
721a8035c8fSMark Brown 
722b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7237a79e94eSBill Pemberton static int wm8731_i2c_probe(struct i2c_client *i2c,
724a8035c8fSMark Brown 			    const struct i2c_device_id *id)
725a8035c8fSMark Brown {
7265998102bSMark Brown 	struct wm8731_priv *wm8731;
727f0fba2adSLiam Girdwood 	int ret;
728a8035c8fSMark Brown 
729f1992ddeSMark Brown 	wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
730f1992ddeSMark Brown 			      GFP_KERNEL);
7315998102bSMark Brown 	if (wm8731 == NULL)
7325998102bSMark Brown 		return -ENOMEM;
7335998102bSMark Brown 
73499d42234SSongjun Wu 	wm8731->mclk = devm_clk_get(&i2c->dev, "mclk");
73599d42234SSongjun Wu 	if (IS_ERR(wm8731->mclk)) {
73699d42234SSongjun Wu 		ret = PTR_ERR(wm8731->mclk);
73799d42234SSongjun Wu 		if (ret == -ENOENT) {
73899d42234SSongjun Wu 			wm8731->mclk = NULL;
73999d42234SSongjun Wu 			dev_warn(&i2c->dev, "Assuming static MCLK\n");
74099d42234SSongjun Wu 		} else {
74199d42234SSongjun Wu 			dev_err(&i2c->dev, "Failed to get MCLK: %d\n",
74299d42234SSongjun Wu 				ret);
74399d42234SSongjun Wu 			return ret;
74499d42234SSongjun Wu 		}
74599d42234SSongjun Wu 	}
74699d42234SSongjun Wu 
7478a6cf30bSManuel Lauss 	mutex_init(&wm8731->lock);
7488a6cf30bSManuel Lauss 
7496702dfccSSergey Kiselev 	i2c_set_clientdata(i2c, wm8731);
7506702dfccSSergey Kiselev 
7516702dfccSSergey Kiselev 	ret = wm8731_request_supplies(&i2c->dev, wm8731);
7526702dfccSSergey Kiselev 	if (ret != 0)
7536702dfccSSergey Kiselev 		return ret;
7546702dfccSSergey Kiselev 
755f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
75605d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
75705d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
75805d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
75905d448e2SMark Brown 			ret);
760f1992ddeSMark Brown 		return ret;
76105d448e2SMark Brown 	}
76205d448e2SMark Brown 
763*3f4fb905SMark Brown 	return wm8731_init(&i2c->dev, wm8731);
764a8035c8fSMark Brown }
765a8035c8fSMark Brown 
766a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
767a8035c8fSMark Brown 	{ "wm8731", 0 },
768a8035c8fSMark Brown 	{ }
769a8035c8fSMark Brown };
770a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
771a8035c8fSMark Brown 
772a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
773a8035c8fSMark Brown 	.driver = {
77499b59f3cSMark Brown 		.name = "wm8731",
775a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
776a8035c8fSMark Brown 	},
777a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
778a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
779a8035c8fSMark Brown };
780a8035c8fSMark Brown #endif
781a8035c8fSMark Brown 
782c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
78364089b84SMark Brown {
784f0fba2adSLiam Girdwood 	int ret = 0;
785b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7865998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
7875998102bSMark Brown 	if (ret != 0) {
7885998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
7895998102bSMark Brown 		       ret);
7905998102bSMark Brown 	}
7915998102bSMark Brown #endif
7925998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7935998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
7945998102bSMark Brown 	if (ret != 0) {
7955998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
7965998102bSMark Brown 		       ret);
7975998102bSMark Brown 	}
7985998102bSMark Brown #endif
799f0fba2adSLiam Girdwood 	return ret;
80064089b84SMark Brown }
80164089b84SMark Brown module_init(wm8731_modinit);
80264089b84SMark Brown 
80364089b84SMark Brown static void __exit wm8731_exit(void)
80464089b84SMark Brown {
805b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
8065998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
8075998102bSMark Brown #endif
8085998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8095998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
8105998102bSMark Brown #endif
81164089b84SMark Brown }
81264089b84SMark Brown module_exit(wm8731_exit);
81364089b84SMark Brown 
81440e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
81540e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
81640e0aa64SRichard Purdie MODULE_LICENSE("GPL");
817