xref: /linux/sound/soc/codecs/wm8731.c (revision cde596c7c07343789e3e0f1c09fc7d3c756c88d0)
140e0aa64SRichard Purdie /*
240e0aa64SRichard Purdie  * wm8731.c  --  WM8731 ALSA SoC Audio driver
340e0aa64SRichard Purdie  *
440e0aa64SRichard Purdie  * Copyright 2005 Openedhand Ltd.
5656baaebSMark Brown  * Copyright 2006-12 Wolfson Microelectronics, plc
640e0aa64SRichard Purdie  *
740e0aa64SRichard Purdie  * Author: Richard Purdie <richard@openedhand.com>
840e0aa64SRichard Purdie  *
940e0aa64SRichard Purdie  * Based on wm8753.c by Liam Girdwood
1040e0aa64SRichard Purdie  *
1140e0aa64SRichard Purdie  * This program is free software; you can redistribute it and/or modify
1240e0aa64SRichard Purdie  * it under the terms of the GNU General Public License version 2 as
1340e0aa64SRichard Purdie  * published by the Free Software Foundation.
1440e0aa64SRichard Purdie  */
1540e0aa64SRichard Purdie 
1640e0aa64SRichard Purdie #include <linux/module.h>
1740e0aa64SRichard Purdie #include <linux/moduleparam.h>
1840e0aa64SRichard Purdie #include <linux/init.h>
1940e0aa64SRichard Purdie #include <linux/delay.h>
2040e0aa64SRichard Purdie #include <linux/pm.h>
2140e0aa64SRichard Purdie #include <linux/i2c.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
2305d448e2SMark Brown #include <linux/regmap.h>
247dea7c01SMark Brown #include <linux/regulator/consumer.h>
25d2a40355SCliff Cai #include <linux/spi/spi.h>
26a7f96e4dSMark Brown #include <linux/of_device.h>
27a51ff30fSLars-Peter Clausen #include <linux/mutex.h>
2899d42234SSongjun Wu #include <linux/clk.h>
2940e0aa64SRichard Purdie #include <sound/core.h>
3040e0aa64SRichard Purdie #include <sound/pcm.h>
3140e0aa64SRichard Purdie #include <sound/pcm_params.h>
3240e0aa64SRichard Purdie #include <sound/soc.h>
3340e0aa64SRichard Purdie #include <sound/initval.h>
34d00efa64SMark Brown #include <sound/tlv.h>
3540e0aa64SRichard Purdie 
3640e0aa64SRichard Purdie #include "wm8731.h"
3740e0aa64SRichard Purdie 
387dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4
397dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
407dea7c01SMark Brown 	"AVDD",
417dea7c01SMark Brown 	"HPVDD",
427dea7c01SMark Brown 	"DCVDD",
437dea7c01SMark Brown 	"DBVDD",
447dea7c01SMark Brown };
457dea7c01SMark Brown 
46b36d61d4SFrank Mandarino /* codec private data */
47b36d61d4SFrank Mandarino struct wm8731_priv {
4805d448e2SMark Brown 	struct regmap *regmap;
4999d42234SSongjun Wu 	struct clk *mclk;
507dea7c01SMark Brown 	struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
510890c2b7SRichard Genoud 	const struct snd_pcm_hw_constraint_list *constraints;
52b36d61d4SFrank Mandarino 	unsigned int sysclk;
539745e824SMark Brown 	int sysclk_type;
54dd31b310SMark Brown 	int playback_fs;
55dd31b310SMark Brown 	bool deemph;
56a51ff30fSLars-Peter Clausen 
57a51ff30fSLars-Peter Clausen 	struct mutex lock;
58b36d61d4SFrank Mandarino };
59b36d61d4SFrank Mandarino 
60a8035c8fSMark Brown 
6140e0aa64SRichard Purdie /*
6240e0aa64SRichard Purdie  * wm8731 register cache
6340e0aa64SRichard Purdie  */
6405d448e2SMark Brown static const struct reg_default wm8731_reg_defaults[] = {
6505d448e2SMark Brown 	{ 0, 0x0097 },
6605d448e2SMark Brown 	{ 1, 0x0097 },
6705d448e2SMark Brown 	{ 2, 0x0079 },
6805d448e2SMark Brown 	{ 3, 0x0079 },
6905d448e2SMark Brown 	{ 4, 0x000a },
7005d448e2SMark Brown 	{ 5, 0x0008 },
7105d448e2SMark Brown 	{ 6, 0x009f },
7205d448e2SMark Brown 	{ 7, 0x000a },
7305d448e2SMark Brown 	{ 8, 0x0000 },
7405d448e2SMark Brown 	{ 9, 0x0000 },
7540e0aa64SRichard Purdie };
7640e0aa64SRichard Purdie 
7705d448e2SMark Brown static bool wm8731_volatile(struct device *dev, unsigned int reg)
7805d448e2SMark Brown {
7905d448e2SMark Brown 	return reg == WM8731_RESET;
8005d448e2SMark Brown }
8105d448e2SMark Brown 
826702dfccSSergey Kiselev #define wm8731_reset(m)	regmap_write(m, WM8731_RESET, 0)
8340e0aa64SRichard Purdie 
8440e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
8559f72970SMark Brown 
869e74b14aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
879e74b14aSTakashi Iwai 			    WM8731_APANA, 2, wm8731_input_select);
8859f72970SMark Brown 
89dd31b310SMark Brown static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
9059f72970SMark Brown 
91*cde596c7SKuninori Morimoto static int wm8731_set_deemph(struct snd_soc_component *component)
92dd31b310SMark Brown {
93*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
94dd31b310SMark Brown 	int val, i, best;
95dd31b310SMark Brown 
96dd31b310SMark Brown 	/* If we're using deemphasis select the nearest available sample
97dd31b310SMark Brown 	 * rate.
98dd31b310SMark Brown 	 */
99dd31b310SMark Brown 	if (wm8731->deemph) {
100dd31b310SMark Brown 		best = 1;
101dd31b310SMark Brown 		for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
102dd31b310SMark Brown 			if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
103dd31b310SMark Brown 			    abs(wm8731_deemph[best] - wm8731->playback_fs))
104dd31b310SMark Brown 				best = i;
105dd31b310SMark Brown 		}
106dd31b310SMark Brown 
107dd31b310SMark Brown 		val = best << 1;
108dd31b310SMark Brown 	} else {
109dd31b310SMark Brown 		best = 0;
110dd31b310SMark Brown 		val = 0;
111dd31b310SMark Brown 	}
112dd31b310SMark Brown 
113*cde596c7SKuninori Morimoto 	dev_dbg(component->dev, "Set deemphasis %d (%dHz)\n",
114dd31b310SMark Brown 		best, wm8731_deemph[best]);
115dd31b310SMark Brown 
116*cde596c7SKuninori Morimoto 	return snd_soc_component_update_bits(component, WM8731_APDIGI, 0x6, val);
117dd31b310SMark Brown }
118dd31b310SMark Brown 
119dd31b310SMark Brown static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
120dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
121dd31b310SMark Brown {
122*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
123*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
124dd31b310SMark Brown 
125bd14016fSTakashi Iwai 	ucontrol->value.integer.value[0] = wm8731->deemph;
126dd31b310SMark Brown 
127dd31b310SMark Brown 	return 0;
128dd31b310SMark Brown }
129dd31b310SMark Brown 
130dd31b310SMark Brown static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
131dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
132dd31b310SMark Brown {
133*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
134*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
135523bade2SDan Carpenter 	unsigned int deemph = ucontrol->value.integer.value[0];
136dd31b310SMark Brown 	int ret = 0;
137dd31b310SMark Brown 
138dd31b310SMark Brown 	if (deemph > 1)
139dd31b310SMark Brown 		return -EINVAL;
140dd31b310SMark Brown 
141a51ff30fSLars-Peter Clausen 	mutex_lock(&wm8731->lock);
142dd31b310SMark Brown 	if (wm8731->deemph != deemph) {
143dd31b310SMark Brown 		wm8731->deemph = deemph;
144dd31b310SMark Brown 
145*cde596c7SKuninori Morimoto 		wm8731_set_deemph(component);
146dd31b310SMark Brown 
147dd31b310SMark Brown 		ret = 1;
148dd31b310SMark Brown 	}
149a51ff30fSLars-Peter Clausen 	mutex_unlock(&wm8731->lock);
150dd31b310SMark Brown 
151dd31b310SMark Brown 	return ret;
152dd31b310SMark Brown }
15340e0aa64SRichard Purdie 
154d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
155d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
156d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
157d921184eSMark Brown static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
158d00efa64SMark Brown 
15940e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
16040e0aa64SRichard Purdie 
161d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
162d00efa64SMark Brown 		 0, 127, 0, out_tlv),
163bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
164bd903b6eSLiam Girdwood 	7, 1, 0),
16540e0aa64SRichard Purdie 
166d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
167d00efa64SMark Brown 		 in_tlv),
16840e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
16940e0aa64SRichard Purdie 
170d921184eSMark Brown SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
171ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
17240e0aa64SRichard Purdie 
173d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
174d00efa64SMark Brown 	       sidetone_tlv),
17540e0aa64SRichard Purdie 
17640e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
17740e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
17840e0aa64SRichard Purdie 
179dd31b310SMark Brown SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
180dd31b310SMark Brown 		    wm8731_get_deemph, wm8731_put_deemph),
18140e0aa64SRichard Purdie };
18240e0aa64SRichard Purdie 
18340e0aa64SRichard Purdie /* Output Mixer */
18440e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
18540e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
18640e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
18740e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
18840e0aa64SRichard Purdie };
18940e0aa64SRichard Purdie 
19040e0aa64SRichard Purdie /* Input mux */
19140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
19259f72970SMark Brown SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
19340e0aa64SRichard Purdie 
19440e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
1958a27bd9aSMark Brown SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
1969745e824SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
19740e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
19840e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
19940e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
20040e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
20140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
20240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
20340e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
20440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
20540e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
20640e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
20740e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
20840e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
20940e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
21040e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
21140e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
21240e0aa64SRichard Purdie };
21340e0aa64SRichard Purdie 
2149745e824SMark Brown static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
2159745e824SMark Brown 			    struct snd_soc_dapm_widget *sink)
2169745e824SMark Brown {
217*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
218*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
2199745e824SMark Brown 
2205a195b44SNicolas Ferre 	return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
2219745e824SMark Brown }
2229745e824SMark Brown 
2235e251aecSMark Brown static const struct snd_soc_dapm_route wm8731_intercon[] = {
2249745e824SMark Brown 	{"DAC", NULL, "OSC", wm8731_check_osc},
2259745e824SMark Brown 	{"ADC", NULL, "OSC", wm8731_check_osc},
2268a27bd9aSMark Brown 	{"DAC", NULL, "ACTIVE"},
2278a27bd9aSMark Brown 	{"ADC", NULL, "ACTIVE"},
2289745e824SMark Brown 
22940e0aa64SRichard Purdie 	/* output mixer */
23040e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
23140e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
23240e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
23340e0aa64SRichard Purdie 
23440e0aa64SRichard Purdie 	/* outputs */
23540e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
23640e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
23740e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
23840e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
23940e0aa64SRichard Purdie 
24040e0aa64SRichard Purdie 	/* input mux */
24140e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
24240e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
24340e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
24440e0aa64SRichard Purdie 
24540e0aa64SRichard Purdie 	/* inputs */
24640e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
24740e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
24840e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
24940e0aa64SRichard Purdie };
25040e0aa64SRichard Purdie 
25140e0aa64SRichard Purdie struct _coeff_div {
25240e0aa64SRichard Purdie 	u32 mclk;
25340e0aa64SRichard Purdie 	u32 rate;
25440e0aa64SRichard Purdie 	u16 fs;
25540e0aa64SRichard Purdie 	u8 sr:4;
25640e0aa64SRichard Purdie 	u8 bosr:1;
25740e0aa64SRichard Purdie 	u8 usb:1;
25840e0aa64SRichard Purdie };
25940e0aa64SRichard Purdie 
26040e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
26140e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
26240e0aa64SRichard Purdie 	/* 48k */
26340e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
26440e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
26540e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
26640e0aa64SRichard Purdie 
26740e0aa64SRichard Purdie 	/* 32k */
26840e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
26940e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
270298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
27140e0aa64SRichard Purdie 
27240e0aa64SRichard Purdie 	/* 8k */
27340e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
27440e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
27540e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
27640e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
27740e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
27840e0aa64SRichard Purdie 
27940e0aa64SRichard Purdie 	/* 96k */
28040e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
28140e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
28240e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
28340e0aa64SRichard Purdie 
28440e0aa64SRichard Purdie 	/* 44.1k */
28540e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
28640e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
28740e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
28840e0aa64SRichard Purdie 
28940e0aa64SRichard Purdie 	/* 88.2k */
29040e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
29140e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
29240e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
29340e0aa64SRichard Purdie };
29440e0aa64SRichard Purdie 
2950890c2b7SRichard Genoud /* rates constraints */
2960890c2b7SRichard Genoud static const unsigned int wm8731_rates_12000000[] = {
2970890c2b7SRichard Genoud 	8000, 32000, 44100, 48000, 96000, 88200,
2980890c2b7SRichard Genoud };
2990890c2b7SRichard Genoud 
3000890c2b7SRichard Genoud static const unsigned int wm8731_rates_12288000_18432000[] = {
3010890c2b7SRichard Genoud 	8000, 32000, 48000, 96000,
3020890c2b7SRichard Genoud };
3030890c2b7SRichard Genoud 
3040890c2b7SRichard Genoud static const unsigned int wm8731_rates_11289600_16934400[] = {
3050890c2b7SRichard Genoud 	8000, 44100, 88200,
3060890c2b7SRichard Genoud };
3070890c2b7SRichard Genoud 
3080890c2b7SRichard Genoud static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
3090890c2b7SRichard Genoud 	.list = wm8731_rates_12000000,
3100890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12000000),
3110890c2b7SRichard Genoud };
3120890c2b7SRichard Genoud 
3130890c2b7SRichard Genoud static const
3140890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
3150890c2b7SRichard Genoud 	.list = wm8731_rates_12288000_18432000,
3160890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
3170890c2b7SRichard Genoud };
3180890c2b7SRichard Genoud 
3190890c2b7SRichard Genoud static const
3200890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
3210890c2b7SRichard Genoud 	.list = wm8731_rates_11289600_16934400,
3220890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
3230890c2b7SRichard Genoud };
3240890c2b7SRichard Genoud 
32540e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
32640e0aa64SRichard Purdie {
32740e0aa64SRichard Purdie 	int i;
32840e0aa64SRichard Purdie 
32940e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
33040e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
33140e0aa64SRichard Purdie 			return i;
33240e0aa64SRichard Purdie 	}
33340e0aa64SRichard Purdie 	return 0;
33440e0aa64SRichard Purdie }
33540e0aa64SRichard Purdie 
336b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
337dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
338dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
33940e0aa64SRichard Purdie {
340*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
341*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
342*cde596c7SKuninori Morimoto 	u16 iface = snd_soc_component_read32(component, WM8731_IFACE) & 0xfff3;
343b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
344b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
345b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
34640e0aa64SRichard Purdie 
347dd31b310SMark Brown 	wm8731->playback_fs = params_rate(params);
348dd31b310SMark Brown 
349*cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_SRATE, srate);
35040e0aa64SRichard Purdie 
351b36d61d4SFrank Mandarino 	/* bit size */
352dfb6778eSMark Brown 	switch (params_width(params)) {
353dfb6778eSMark Brown 	case 16:
354b36d61d4SFrank Mandarino 		break;
355dfb6778eSMark Brown 	case 20:
356b36d61d4SFrank Mandarino 		iface |= 0x0004;
357b36d61d4SFrank Mandarino 		break;
358dfb6778eSMark Brown 	case 24:
359b36d61d4SFrank Mandarino 		iface |= 0x0008;
360b36d61d4SFrank Mandarino 		break;
361cf5ef3a2SMatt Flax 	case 32:
362cf5ef3a2SMatt Flax 		iface |= 0x000c;
363cf5ef3a2SMatt Flax 		break;
364b36d61d4SFrank Mandarino 	}
365b36d61d4SFrank Mandarino 
366*cde596c7SKuninori Morimoto 	wm8731_set_deemph(component);
367dd31b310SMark Brown 
368*cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_IFACE, iface);
369b36d61d4SFrank Mandarino 	return 0;
37040e0aa64SRichard Purdie }
37140e0aa64SRichard Purdie 
372e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
37340e0aa64SRichard Purdie {
374*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
375*cde596c7SKuninori Morimoto 	u16 mute_reg = snd_soc_component_read32(component, WM8731_APDIGI) & 0xfff7;
376b36d61d4SFrank Mandarino 
37740e0aa64SRichard Purdie 	if (mute)
378*cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_APDIGI, mute_reg | 0x8);
37940e0aa64SRichard Purdie 	else
380*cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_APDIGI, mute_reg);
38140e0aa64SRichard Purdie 	return 0;
38240e0aa64SRichard Purdie }
38340e0aa64SRichard Purdie 
384e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
385b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
386b36d61d4SFrank Mandarino {
387*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
388*cde596c7SKuninori Morimoto 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
389*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
390b36d61d4SFrank Mandarino 
3919745e824SMark Brown 	switch (clk_id) {
3929745e824SMark Brown 	case WM8731_SYSCLK_XTAL:
3939745e824SMark Brown 	case WM8731_SYSCLK_MCLK:
39499d42234SSongjun Wu 		if (wm8731->mclk && clk_set_rate(wm8731->mclk, freq))
39599d42234SSongjun Wu 			return -EINVAL;
3969745e824SMark Brown 		wm8731->sysclk_type = clk_id;
3979745e824SMark Brown 		break;
3989745e824SMark Brown 	default:
3999745e824SMark Brown 		return -EINVAL;
4009745e824SMark Brown 	}
4019745e824SMark Brown 
402b36d61d4SFrank Mandarino 	switch (freq) {
4030890c2b7SRichard Genoud 	case 0:
4040890c2b7SRichard Genoud 		wm8731->constraints = NULL;
4050890c2b7SRichard Genoud 		break;
406b36d61d4SFrank Mandarino 	case 12000000:
4070890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12000000;
4080890c2b7SRichard Genoud 		break;
409b36d61d4SFrank Mandarino 	case 12288000:
410b36d61d4SFrank Mandarino 	case 18432000:
4110890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12288000_18432000;
4120890c2b7SRichard Genoud 		break;
4130890c2b7SRichard Genoud 	case 16934400:
4140890c2b7SRichard Genoud 	case 11289600:
4150890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_11289600_16934400;
4169745e824SMark Brown 		break;
4179745e824SMark Brown 	default:
418b36d61d4SFrank Mandarino 		return -EINVAL;
419b36d61d4SFrank Mandarino 	}
420b36d61d4SFrank Mandarino 
4210890c2b7SRichard Genoud 	wm8731->sysclk = freq;
4220890c2b7SRichard Genoud 
423fc31fda6SLars-Peter Clausen 	snd_soc_dapm_sync(dapm);
4249745e824SMark Brown 
4259745e824SMark Brown 	return 0;
4269745e824SMark Brown }
4279745e824SMark Brown 
428b36d61d4SFrank Mandarino 
429e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
430b36d61d4SFrank Mandarino 		unsigned int fmt)
431b36d61d4SFrank Mandarino {
432*cde596c7SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
433b36d61d4SFrank Mandarino 	u16 iface = 0;
434b36d61d4SFrank Mandarino 
435b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
436b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
437b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
438b36d61d4SFrank Mandarino 		iface |= 0x0040;
439b36d61d4SFrank Mandarino 		break;
440b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
441b36d61d4SFrank Mandarino 		break;
442b36d61d4SFrank Mandarino 	default:
443b36d61d4SFrank Mandarino 		return -EINVAL;
444b36d61d4SFrank Mandarino 	}
445b36d61d4SFrank Mandarino 
446b36d61d4SFrank Mandarino 	/* interface format */
447b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
448b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
449b36d61d4SFrank Mandarino 		iface |= 0x0002;
450b36d61d4SFrank Mandarino 		break;
451b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
452b36d61d4SFrank Mandarino 		break;
453b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
454b36d61d4SFrank Mandarino 		iface |= 0x0001;
455b36d61d4SFrank Mandarino 		break;
456b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
457b4af6ef9SBo Shen 		iface |= 0x0013;
458b36d61d4SFrank Mandarino 		break;
459b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
460b4af6ef9SBo Shen 		iface |= 0x0003;
461b36d61d4SFrank Mandarino 		break;
462b36d61d4SFrank Mandarino 	default:
463b36d61d4SFrank Mandarino 		return -EINVAL;
464b36d61d4SFrank Mandarino 	}
465b36d61d4SFrank Mandarino 
466b36d61d4SFrank Mandarino 	/* clock inversion */
467b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
468b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
469b36d61d4SFrank Mandarino 		break;
470b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
471b36d61d4SFrank Mandarino 		iface |= 0x0090;
472b36d61d4SFrank Mandarino 		break;
473b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
474b36d61d4SFrank Mandarino 		iface |= 0x0080;
475b36d61d4SFrank Mandarino 		break;
476b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
477b36d61d4SFrank Mandarino 		iface |= 0x0010;
478b36d61d4SFrank Mandarino 		break;
479b36d61d4SFrank Mandarino 	default:
480b36d61d4SFrank Mandarino 		return -EINVAL;
481b36d61d4SFrank Mandarino 	}
482b36d61d4SFrank Mandarino 
483b36d61d4SFrank Mandarino 	/* set iface */
484*cde596c7SKuninori Morimoto 	snd_soc_component_write(component, WM8731_IFACE, iface);
485b36d61d4SFrank Mandarino 	return 0;
486b36d61d4SFrank Mandarino }
487b36d61d4SFrank Mandarino 
488*cde596c7SKuninori Morimoto static int wm8731_set_bias_level(struct snd_soc_component *component,
4890be9898aSMark Brown 				 enum snd_soc_bias_level level)
49040e0aa64SRichard Purdie {
491*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
4929bf311feSAxel Lin 	int ret;
49322d22ee5SMark Brown 	u16 reg;
49440e0aa64SRichard Purdie 
4950be9898aSMark Brown 	switch (level) {
4960be9898aSMark Brown 	case SND_SOC_BIAS_ON:
497cef6daa9SFabio Estevam 		if (wm8731->mclk) {
498cef6daa9SFabio Estevam 			ret = clk_prepare_enable(wm8731->mclk);
499cef6daa9SFabio Estevam 			if (ret)
500cef6daa9SFabio Estevam 				return ret;
501cef6daa9SFabio Estevam 		}
50240e0aa64SRichard Purdie 		break;
5030be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
50440e0aa64SRichard Purdie 		break;
5050be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
506*cde596c7SKuninori Morimoto 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
50706ae9988SMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
50806ae9988SMark Brown 						    wm8731->supplies);
50906ae9988SMark Brown 			if (ret != 0)
51006ae9988SMark Brown 				return ret;
51106ae9988SMark Brown 
51205d448e2SMark Brown 			regcache_sync(wm8731->regmap);
51306ae9988SMark Brown 		}
51406ae9988SMark Brown 
51522d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
516*cde596c7SKuninori Morimoto 		reg = snd_soc_component_read32(component, WM8731_PWR) & 0xff7f;
517*cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_PWR, reg | 0x0040);
51840e0aa64SRichard Purdie 		break;
5190be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
52099d42234SSongjun Wu 		if (wm8731->mclk)
52199d42234SSongjun Wu 			clk_disable_unprepare(wm8731->mclk);
522*cde596c7SKuninori Morimoto 		snd_soc_component_write(component, WM8731_PWR, 0xffff);
52306ae9988SMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
52406ae9988SMark Brown 				       wm8731->supplies);
52505d448e2SMark Brown 		regcache_mark_dirty(wm8731->regmap);
52640e0aa64SRichard Purdie 		break;
52740e0aa64SRichard Purdie 	}
52840e0aa64SRichard Purdie 	return 0;
52940e0aa64SRichard Purdie }
53040e0aa64SRichard Purdie 
5310890c2b7SRichard Genoud static int wm8731_startup(struct snd_pcm_substream *substream,
5320890c2b7SRichard Genoud 	struct snd_soc_dai *dai)
5330890c2b7SRichard Genoud {
534*cde596c7SKuninori Morimoto 	struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(dai->component);
5350890c2b7SRichard Genoud 
5360890c2b7SRichard Genoud 	if (wm8731->constraints)
5370890c2b7SRichard Genoud 		snd_pcm_hw_constraint_list(substream->runtime, 0,
5380890c2b7SRichard Genoud 					   SNDRV_PCM_HW_PARAM_RATE,
5390890c2b7SRichard Genoud 					   wm8731->constraints);
5400890c2b7SRichard Genoud 
5410890c2b7SRichard Genoud 	return 0;
5420890c2b7SRichard Genoud }
5430890c2b7SRichard Genoud 
544e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
545b36d61d4SFrank Mandarino 
546b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
547cf5ef3a2SMatt Flax 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
548b36d61d4SFrank Mandarino 
54985e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8731_dai_ops = {
5500890c2b7SRichard Genoud 	.startup	= wm8731_startup,
5516335d055SEric Miao 	.hw_params	= wm8731_hw_params,
5526335d055SEric Miao 	.digital_mute	= wm8731_mute,
5536335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
5546335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
5556335d055SEric Miao };
5566335d055SEric Miao 
557f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = {
558f0fba2adSLiam Girdwood 	.name = "wm8731-hifi",
55940e0aa64SRichard Purdie 	.playback = {
56040e0aa64SRichard Purdie 		.stream_name = "Playback",
56140e0aa64SRichard Purdie 		.channels_min = 1,
56240e0aa64SRichard Purdie 		.channels_max = 2,
563b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
564b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
56540e0aa64SRichard Purdie 	.capture = {
56640e0aa64SRichard Purdie 		.stream_name = "Capture",
56740e0aa64SRichard Purdie 		.channels_min = 1,
56840e0aa64SRichard Purdie 		.channels_max = 2,
569b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
570b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
5716335d055SEric Miao 	.ops = &wm8731_dai_ops,
5724934482dSMark Brown 	.symmetric_rates = 1,
57340e0aa64SRichard Purdie };
57440e0aa64SRichard Purdie 
5756702dfccSSergey Kiselev static int wm8731_request_supplies(struct device *dev,
5766702dfccSSergey Kiselev 		struct wm8731_priv *wm8731)
57740e0aa64SRichard Purdie {
578f0fba2adSLiam Girdwood 	int ret = 0, i;
57940e0aa64SRichard Purdie 
5807dea7c01SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
5817dea7c01SMark Brown 		wm8731->supplies[i].supply = wm8731_supply_names[i];
5827dea7c01SMark Brown 
5836702dfccSSergey Kiselev 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(wm8731->supplies),
5847dea7c01SMark Brown 				 wm8731->supplies);
5857dea7c01SMark Brown 	if (ret != 0) {
5866702dfccSSergey Kiselev 		dev_err(dev, "Failed to request supplies: %d\n", ret);
587f0fba2adSLiam Girdwood 		return ret;
5887dea7c01SMark Brown 	}
5897dea7c01SMark Brown 
5907dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
5917dea7c01SMark Brown 				    wm8731->supplies);
5927dea7c01SMark Brown 	if (ret != 0) {
5936702dfccSSergey Kiselev 		dev_err(dev, "Failed to enable supplies: %d\n", ret);
5943598aad5SFabio Estevam 		return ret;
5957dea7c01SMark Brown 	}
5967dea7c01SMark Brown 
5976702dfccSSergey Kiselev 	return 0;
5986702dfccSSergey Kiselev }
5996702dfccSSergey Kiselev 
6006702dfccSSergey Kiselev static int wm8731_hw_init(struct device *dev, struct wm8731_priv *wm8731)
6016702dfccSSergey Kiselev {
6026702dfccSSergey Kiselev 	int ret = 0;
6036702dfccSSergey Kiselev 
6046702dfccSSergey Kiselev 	ret = wm8731_reset(wm8731->regmap);
605519cf2dfSMark Brown 	if (ret < 0) {
6066702dfccSSergey Kiselev 		dev_err(dev, "Failed to issue reset: %d\n", ret);
6077dea7c01SMark Brown 		goto err_regulator_enable;
608519cf2dfSMark Brown 	}
609519cf2dfSMark Brown 
6106702dfccSSergey Kiselev 	/* Clear POWEROFF, keep everything else disabled */
6116702dfccSSergey Kiselev 	regmap_write(wm8731->regmap, WM8731_PWR, 0x7f);
6125998102bSMark Brown 
6135998102bSMark Brown 	/* Latch the update bits */
6146702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_LOUT1V, 0x100, 0);
6156702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_ROUT1V, 0x100, 0);
6166702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_LINVOL, 0x100, 0);
6176702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_RINVOL, 0x100, 0);
6185998102bSMark Brown 
619ce3bdaa8SMark Brown 	/* Disable bypass path by default */
6206702dfccSSergey Kiselev 	regmap_update_bits(wm8731->regmap, WM8731_APANA, 0x8, 0);
621ce3bdaa8SMark Brown 
6226702dfccSSergey Kiselev 	regcache_mark_dirty(wm8731->regmap);
623fe5422fcSMark Brown 
6247dea7c01SMark Brown err_regulator_enable:
6256702dfccSSergey Kiselev 	/* Regulators will be enabled by bias management */
6267dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
627f0fba2adSLiam Girdwood 
628fe5422fcSMark Brown 	return ret;
629a8035c8fSMark Brown }
630a8035c8fSMark Brown 
631*cde596c7SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8731 = {
632f0fba2adSLiam Girdwood 	.set_bias_level		= wm8731_set_bias_level,
6332cb7474bSKuninori Morimoto 	.controls		= wm8731_snd_controls,
6342cb7474bSKuninori Morimoto 	.num_controls		= ARRAY_SIZE(wm8731_snd_controls),
6355e251aecSMark Brown 	.dapm_widgets		= wm8731_dapm_widgets,
6365e251aecSMark Brown 	.num_dapm_widgets	= ARRAY_SIZE(wm8731_dapm_widgets),
6375e251aecSMark Brown 	.dapm_routes		= wm8731_intercon,
6385e251aecSMark Brown 	.num_dapm_routes	= ARRAY_SIZE(wm8731_intercon),
639*cde596c7SKuninori Morimoto 	.suspend_bias_off	= 1,
640*cde596c7SKuninori Morimoto 	.idle_bias_on		= 1,
641*cde596c7SKuninori Morimoto 	.use_pmdown_time	= 1,
642*cde596c7SKuninori Morimoto 	.endianness		= 1,
643*cde596c7SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
644f0fba2adSLiam Girdwood };
645f0fba2adSLiam Girdwood 
646a7f96e4dSMark Brown static const struct of_device_id wm8731_of_match[] = {
647a7f96e4dSMark Brown 	{ .compatible = "wlf,wm8731", },
648a7f96e4dSMark Brown 	{ }
649a7f96e4dSMark Brown };
650a7f96e4dSMark Brown 
651a7f96e4dSMark Brown MODULE_DEVICE_TABLE(of, wm8731_of_match);
652a7f96e4dSMark Brown 
65305d448e2SMark Brown static const struct regmap_config wm8731_regmap = {
65405d448e2SMark Brown 	.reg_bits = 7,
65505d448e2SMark Brown 	.val_bits = 9,
65605d448e2SMark Brown 
65705d448e2SMark Brown 	.max_register = WM8731_RESET,
65805d448e2SMark Brown 	.volatile_reg = wm8731_volatile,
65905d448e2SMark Brown 
66005d448e2SMark Brown 	.cache_type = REGCACHE_RBTREE,
66105d448e2SMark Brown 	.reg_defaults = wm8731_reg_defaults,
66205d448e2SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
66305d448e2SMark Brown };
66405d448e2SMark Brown 
6655998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
6667a79e94eSBill Pemberton static int wm8731_spi_probe(struct spi_device *spi)
6675998102bSMark Brown {
6685998102bSMark Brown 	struct wm8731_priv *wm8731;
669f0fba2adSLiam Girdwood 	int ret;
6705998102bSMark Brown 
671cea82d8aSFabio Estevam 	wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL);
6725998102bSMark Brown 	if (wm8731 == NULL)
6735998102bSMark Brown 		return -ENOMEM;
6745998102bSMark Brown 
67599d42234SSongjun Wu 	wm8731->mclk = devm_clk_get(&spi->dev, "mclk");
67699d42234SSongjun Wu 	if (IS_ERR(wm8731->mclk)) {
67799d42234SSongjun Wu 		ret = PTR_ERR(wm8731->mclk);
67899d42234SSongjun Wu 		if (ret == -ENOENT) {
67999d42234SSongjun Wu 			wm8731->mclk = NULL;
68099d42234SSongjun Wu 			dev_warn(&spi->dev, "Assuming static MCLK\n");
68199d42234SSongjun Wu 		} else {
68299d42234SSongjun Wu 			dev_err(&spi->dev, "Failed to get MCLK: %d\n",
68399d42234SSongjun Wu 				ret);
68499d42234SSongjun Wu 			return ret;
68599d42234SSongjun Wu 		}
68699d42234SSongjun Wu 	}
68799d42234SSongjun Wu 
688a51ff30fSLars-Peter Clausen 	mutex_init(&wm8731->lock);
689a51ff30fSLars-Peter Clausen 
6906702dfccSSergey Kiselev 	spi_set_drvdata(spi, wm8731);
6916702dfccSSergey Kiselev 
6926702dfccSSergey Kiselev 	ret = wm8731_request_supplies(&spi->dev, wm8731);
6936702dfccSSergey Kiselev 	if (ret != 0)
6946702dfccSSergey Kiselev 		return ret;
6956702dfccSSergey Kiselev 
696f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
69705d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
69805d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
69905d448e2SMark Brown 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
70005d448e2SMark Brown 			ret);
701f1992ddeSMark Brown 		return ret;
70205d448e2SMark Brown 	}
70305d448e2SMark Brown 
7046702dfccSSergey Kiselev 	ret = wm8731_hw_init(&spi->dev, wm8731);
7056702dfccSSergey Kiselev 	if (ret != 0)
7066702dfccSSergey Kiselev 		return ret;
7075998102bSMark Brown 
708*cde596c7SKuninori Morimoto 	ret = devm_snd_soc_register_component(&spi->dev,
709*cde596c7SKuninori Morimoto 			&soc_component_dev_wm8731, &wm8731_dai, 1);
71005d448e2SMark Brown 	if (ret != 0) {
71105d448e2SMark Brown 		dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
712f1992ddeSMark Brown 		return ret;
71305d448e2SMark Brown 	}
71405d448e2SMark Brown 
71505d448e2SMark Brown 	return 0;
7165998102bSMark Brown }
7175998102bSMark Brown 
7187a79e94eSBill Pemberton static int wm8731_spi_remove(struct spi_device *spi)
7195998102bSMark Brown {
7205998102bSMark Brown 	return 0;
7215998102bSMark Brown }
7225998102bSMark Brown 
7235998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
7245998102bSMark Brown 	.driver = {
72599b59f3cSMark Brown 		.name	= "wm8731",
726a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
7275998102bSMark Brown 	},
7285998102bSMark Brown 	.probe		= wm8731_spi_probe,
7297a79e94eSBill Pemberton 	.remove		= wm8731_spi_remove,
7305998102bSMark Brown };
731a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
732a8035c8fSMark Brown 
733b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7347a79e94eSBill Pemberton static int wm8731_i2c_probe(struct i2c_client *i2c,
735a8035c8fSMark Brown 			    const struct i2c_device_id *id)
736a8035c8fSMark Brown {
7375998102bSMark Brown 	struct wm8731_priv *wm8731;
738f0fba2adSLiam Girdwood 	int ret;
739a8035c8fSMark Brown 
740f1992ddeSMark Brown 	wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
741f1992ddeSMark Brown 			      GFP_KERNEL);
7425998102bSMark Brown 	if (wm8731 == NULL)
7435998102bSMark Brown 		return -ENOMEM;
7445998102bSMark Brown 
74599d42234SSongjun Wu 	wm8731->mclk = devm_clk_get(&i2c->dev, "mclk");
74699d42234SSongjun Wu 	if (IS_ERR(wm8731->mclk)) {
74799d42234SSongjun Wu 		ret = PTR_ERR(wm8731->mclk);
74899d42234SSongjun Wu 		if (ret == -ENOENT) {
74999d42234SSongjun Wu 			wm8731->mclk = NULL;
75099d42234SSongjun Wu 			dev_warn(&i2c->dev, "Assuming static MCLK\n");
75199d42234SSongjun Wu 		} else {
75299d42234SSongjun Wu 			dev_err(&i2c->dev, "Failed to get MCLK: %d\n",
75399d42234SSongjun Wu 				ret);
75499d42234SSongjun Wu 			return ret;
75599d42234SSongjun Wu 		}
75699d42234SSongjun Wu 	}
75799d42234SSongjun Wu 
7588a6cf30bSManuel Lauss 	mutex_init(&wm8731->lock);
7598a6cf30bSManuel Lauss 
7606702dfccSSergey Kiselev 	i2c_set_clientdata(i2c, wm8731);
7616702dfccSSergey Kiselev 
7626702dfccSSergey Kiselev 	ret = wm8731_request_supplies(&i2c->dev, wm8731);
7636702dfccSSergey Kiselev 	if (ret != 0)
7646702dfccSSergey Kiselev 		return ret;
7656702dfccSSergey Kiselev 
766f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
76705d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
76805d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
76905d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
77005d448e2SMark Brown 			ret);
771f1992ddeSMark Brown 		return ret;
77205d448e2SMark Brown 	}
77305d448e2SMark Brown 
7746702dfccSSergey Kiselev 	ret = wm8731_hw_init(&i2c->dev, wm8731);
7756702dfccSSergey Kiselev 	if (ret != 0)
7766702dfccSSergey Kiselev 		return ret;
777a8035c8fSMark Brown 
778*cde596c7SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev,
779*cde596c7SKuninori Morimoto 			&soc_component_dev_wm8731, &wm8731_dai, 1);
78005d448e2SMark Brown 	if (ret != 0) {
78105d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
782f1992ddeSMark Brown 		return ret;
78305d448e2SMark Brown 	}
78405d448e2SMark Brown 
78505d448e2SMark Brown 	return 0;
786a8035c8fSMark Brown }
787a8035c8fSMark Brown 
7887a79e94eSBill Pemberton static int wm8731_i2c_remove(struct i2c_client *client)
789a8035c8fSMark Brown {
790a8035c8fSMark Brown 	return 0;
791a8035c8fSMark Brown }
792a8035c8fSMark Brown 
793a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
794a8035c8fSMark Brown 	{ "wm8731", 0 },
795a8035c8fSMark Brown 	{ }
796a8035c8fSMark Brown };
797a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
798a8035c8fSMark Brown 
799a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
800a8035c8fSMark Brown 	.driver = {
80199b59f3cSMark Brown 		.name = "wm8731",
802a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
803a8035c8fSMark Brown 	},
804a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
8057a79e94eSBill Pemberton 	.remove =   wm8731_i2c_remove,
806a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
807a8035c8fSMark Brown };
808a8035c8fSMark Brown #endif
809a8035c8fSMark Brown 
810c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
81164089b84SMark Brown {
812f0fba2adSLiam Girdwood 	int ret = 0;
813b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
8145998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
8155998102bSMark Brown 	if (ret != 0) {
8165998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
8175998102bSMark Brown 		       ret);
8185998102bSMark Brown 	}
8195998102bSMark Brown #endif
8205998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8215998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
8225998102bSMark Brown 	if (ret != 0) {
8235998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
8245998102bSMark Brown 		       ret);
8255998102bSMark Brown 	}
8265998102bSMark Brown #endif
827f0fba2adSLiam Girdwood 	return ret;
82864089b84SMark Brown }
82964089b84SMark Brown module_init(wm8731_modinit);
83064089b84SMark Brown 
83164089b84SMark Brown static void __exit wm8731_exit(void)
83264089b84SMark Brown {
833b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
8345998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
8355998102bSMark Brown #endif
8365998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8375998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
8385998102bSMark Brown #endif
83964089b84SMark Brown }
84064089b84SMark Brown module_exit(wm8731_exit);
84164089b84SMark Brown 
84240e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
84340e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
84440e0aa64SRichard Purdie MODULE_LICENSE("GPL");
845