xref: /linux/sound/soc/codecs/wm8731.c (revision bd14016fbf31aa199026f1e2358eab695f374eb1)
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>
2840e0aa64SRichard Purdie #include <sound/core.h>
2940e0aa64SRichard Purdie #include <sound/pcm.h>
3040e0aa64SRichard Purdie #include <sound/pcm_params.h>
3140e0aa64SRichard Purdie #include <sound/soc.h>
3240e0aa64SRichard Purdie #include <sound/initval.h>
33d00efa64SMark Brown #include <sound/tlv.h>
3440e0aa64SRichard Purdie 
3540e0aa64SRichard Purdie #include "wm8731.h"
3640e0aa64SRichard Purdie 
377dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4
387dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
397dea7c01SMark Brown 	"AVDD",
407dea7c01SMark Brown 	"HPVDD",
417dea7c01SMark Brown 	"DCVDD",
427dea7c01SMark Brown 	"DBVDD",
437dea7c01SMark Brown };
447dea7c01SMark Brown 
45b36d61d4SFrank Mandarino /* codec private data */
46b36d61d4SFrank Mandarino struct wm8731_priv {
4705d448e2SMark Brown 	struct regmap *regmap;
487dea7c01SMark Brown 	struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
490890c2b7SRichard Genoud 	const struct snd_pcm_hw_constraint_list *constraints;
50b36d61d4SFrank Mandarino 	unsigned int sysclk;
519745e824SMark Brown 	int sysclk_type;
52dd31b310SMark Brown 	int playback_fs;
53dd31b310SMark Brown 	bool deemph;
54a51ff30fSLars-Peter Clausen 
55a51ff30fSLars-Peter Clausen 	struct mutex lock;
56b36d61d4SFrank Mandarino };
57b36d61d4SFrank Mandarino 
58a8035c8fSMark Brown 
5940e0aa64SRichard Purdie /*
6040e0aa64SRichard Purdie  * wm8731 register cache
6140e0aa64SRichard Purdie  */
6205d448e2SMark Brown static const struct reg_default wm8731_reg_defaults[] = {
6305d448e2SMark Brown 	{ 0, 0x0097 },
6405d448e2SMark Brown 	{ 1, 0x0097 },
6505d448e2SMark Brown 	{ 2, 0x0079 },
6605d448e2SMark Brown 	{ 3, 0x0079 },
6705d448e2SMark Brown 	{ 4, 0x000a },
6805d448e2SMark Brown 	{ 5, 0x0008 },
6905d448e2SMark Brown 	{ 6, 0x009f },
7005d448e2SMark Brown 	{ 7, 0x000a },
7105d448e2SMark Brown 	{ 8, 0x0000 },
7205d448e2SMark Brown 	{ 9, 0x0000 },
7340e0aa64SRichard Purdie };
7440e0aa64SRichard Purdie 
7505d448e2SMark Brown static bool wm8731_volatile(struct device *dev, unsigned int reg)
7605d448e2SMark Brown {
7705d448e2SMark Brown 	return reg == WM8731_RESET;
7805d448e2SMark Brown }
7905d448e2SMark Brown 
8005d448e2SMark Brown static bool wm8731_writeable(struct device *dev, unsigned int reg)
8105d448e2SMark Brown {
8205d448e2SMark Brown 	return reg <= WM8731_RESET;
8305d448e2SMark Brown }
8405d448e2SMark Brown 
8517a52fd6SMark Brown #define wm8731_reset(c)	snd_soc_write(c, WM8731_RESET, 0)
8640e0aa64SRichard Purdie 
8740e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
8859f72970SMark Brown 
899e74b14aSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
909e74b14aSTakashi Iwai 			    WM8731_APANA, 2, wm8731_input_select);
9159f72970SMark Brown 
92dd31b310SMark Brown static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
9359f72970SMark Brown 
94dd31b310SMark Brown static int wm8731_set_deemph(struct snd_soc_codec *codec)
95dd31b310SMark Brown {
96dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
97dd31b310SMark Brown 	int val, i, best;
98dd31b310SMark Brown 
99dd31b310SMark Brown 	/* If we're using deemphasis select the nearest available sample
100dd31b310SMark Brown 	 * rate.
101dd31b310SMark Brown 	 */
102dd31b310SMark Brown 	if (wm8731->deemph) {
103dd31b310SMark Brown 		best = 1;
104dd31b310SMark Brown 		for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
105dd31b310SMark Brown 			if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
106dd31b310SMark Brown 			    abs(wm8731_deemph[best] - wm8731->playback_fs))
107dd31b310SMark Brown 				best = i;
108dd31b310SMark Brown 		}
109dd31b310SMark Brown 
110dd31b310SMark Brown 		val = best << 1;
111dd31b310SMark Brown 	} else {
112dd31b310SMark Brown 		best = 0;
113dd31b310SMark Brown 		val = 0;
114dd31b310SMark Brown 	}
115dd31b310SMark Brown 
116dd31b310SMark Brown 	dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
117dd31b310SMark Brown 		best, wm8731_deemph[best]);
118dd31b310SMark Brown 
119dd31b310SMark Brown 	return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val);
120dd31b310SMark Brown }
121dd31b310SMark Brown 
122dd31b310SMark Brown static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
123dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
124dd31b310SMark Brown {
125ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
126dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
127dd31b310SMark Brown 
128*bd14016fSTakashi Iwai 	ucontrol->value.integer.value[0] = wm8731->deemph;
129dd31b310SMark Brown 
130dd31b310SMark Brown 	return 0;
131dd31b310SMark Brown }
132dd31b310SMark Brown 
133dd31b310SMark Brown static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
134dd31b310SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
135dd31b310SMark Brown {
136ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
137dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
138*bd14016fSTakashi Iwai 	int deemph = ucontrol->value.integer.value[0];
139dd31b310SMark Brown 	int ret = 0;
140dd31b310SMark Brown 
141dd31b310SMark Brown 	if (deemph > 1)
142dd31b310SMark Brown 		return -EINVAL;
143dd31b310SMark Brown 
144a51ff30fSLars-Peter Clausen 	mutex_lock(&wm8731->lock);
145dd31b310SMark Brown 	if (wm8731->deemph != deemph) {
146dd31b310SMark Brown 		wm8731->deemph = deemph;
147dd31b310SMark Brown 
148dd31b310SMark Brown 		wm8731_set_deemph(codec);
149dd31b310SMark Brown 
150dd31b310SMark Brown 		ret = 1;
151dd31b310SMark Brown 	}
152a51ff30fSLars-Peter Clausen 	mutex_unlock(&wm8731->lock);
153dd31b310SMark Brown 
154dd31b310SMark Brown 	return ret;
155dd31b310SMark Brown }
15640e0aa64SRichard Purdie 
157d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
158d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
159d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
160d921184eSMark Brown static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
161d00efa64SMark Brown 
16240e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
16340e0aa64SRichard Purdie 
164d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
165d00efa64SMark Brown 		 0, 127, 0, out_tlv),
166bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
167bd903b6eSLiam Girdwood 	7, 1, 0),
16840e0aa64SRichard Purdie 
169d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
170d00efa64SMark Brown 		 in_tlv),
17140e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
17240e0aa64SRichard Purdie 
173d921184eSMark Brown SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
174ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
17540e0aa64SRichard Purdie 
176d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
177d00efa64SMark Brown 	       sidetone_tlv),
17840e0aa64SRichard Purdie 
17940e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
18040e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
18140e0aa64SRichard Purdie 
182dd31b310SMark Brown SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
183dd31b310SMark Brown 		    wm8731_get_deemph, wm8731_put_deemph),
18440e0aa64SRichard Purdie };
18540e0aa64SRichard Purdie 
18640e0aa64SRichard Purdie /* Output Mixer */
18740e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
18840e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
18940e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
19040e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
19140e0aa64SRichard Purdie };
19240e0aa64SRichard Purdie 
19340e0aa64SRichard Purdie /* Input mux */
19440e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
19559f72970SMark Brown SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
19640e0aa64SRichard Purdie 
19740e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
1988a27bd9aSMark Brown SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
1999745e824SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
20040e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
20140e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
20240e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
20340e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
20440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
20540e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
20640e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
20740e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
20840e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
20940e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
21040e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
21140e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
21240e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
21340e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
21440e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
21540e0aa64SRichard Purdie };
21640e0aa64SRichard Purdie 
2179745e824SMark Brown static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
2189745e824SMark Brown 			    struct snd_soc_dapm_widget *sink)
2199745e824SMark Brown {
220d286b805SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
221d286b805SLars-Peter Clausen 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
2229745e824SMark Brown 
2235a195b44SNicolas Ferre 	return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
2249745e824SMark Brown }
2259745e824SMark Brown 
2265e251aecSMark Brown static const struct snd_soc_dapm_route wm8731_intercon[] = {
2279745e824SMark Brown 	{"DAC", NULL, "OSC", wm8731_check_osc},
2289745e824SMark Brown 	{"ADC", NULL, "OSC", wm8731_check_osc},
2298a27bd9aSMark Brown 	{"DAC", NULL, "ACTIVE"},
2308a27bd9aSMark Brown 	{"ADC", NULL, "ACTIVE"},
2319745e824SMark Brown 
23240e0aa64SRichard Purdie 	/* output mixer */
23340e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
23440e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
23540e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
23640e0aa64SRichard Purdie 
23740e0aa64SRichard Purdie 	/* outputs */
23840e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
23940e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
24040e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
24140e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
24240e0aa64SRichard Purdie 
24340e0aa64SRichard Purdie 	/* input mux */
24440e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
24540e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
24640e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
24740e0aa64SRichard Purdie 
24840e0aa64SRichard Purdie 	/* inputs */
24940e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
25040e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
25140e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
25240e0aa64SRichard Purdie };
25340e0aa64SRichard Purdie 
25440e0aa64SRichard Purdie struct _coeff_div {
25540e0aa64SRichard Purdie 	u32 mclk;
25640e0aa64SRichard Purdie 	u32 rate;
25740e0aa64SRichard Purdie 	u16 fs;
25840e0aa64SRichard Purdie 	u8 sr:4;
25940e0aa64SRichard Purdie 	u8 bosr:1;
26040e0aa64SRichard Purdie 	u8 usb:1;
26140e0aa64SRichard Purdie };
26240e0aa64SRichard Purdie 
26340e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
26440e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
26540e0aa64SRichard Purdie 	/* 48k */
26640e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
26740e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
26840e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
26940e0aa64SRichard Purdie 
27040e0aa64SRichard Purdie 	/* 32k */
27140e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
27240e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
273298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
27440e0aa64SRichard Purdie 
27540e0aa64SRichard Purdie 	/* 8k */
27640e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
27740e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
27840e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
27940e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
28040e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
28140e0aa64SRichard Purdie 
28240e0aa64SRichard Purdie 	/* 96k */
28340e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
28440e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
28540e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
28640e0aa64SRichard Purdie 
28740e0aa64SRichard Purdie 	/* 44.1k */
28840e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
28940e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
29040e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
29140e0aa64SRichard Purdie 
29240e0aa64SRichard Purdie 	/* 88.2k */
29340e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
29440e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
29540e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
29640e0aa64SRichard Purdie };
29740e0aa64SRichard Purdie 
2980890c2b7SRichard Genoud /* rates constraints */
2990890c2b7SRichard Genoud static const unsigned int wm8731_rates_12000000[] = {
3000890c2b7SRichard Genoud 	8000, 32000, 44100, 48000, 96000, 88200,
3010890c2b7SRichard Genoud };
3020890c2b7SRichard Genoud 
3030890c2b7SRichard Genoud static const unsigned int wm8731_rates_12288000_18432000[] = {
3040890c2b7SRichard Genoud 	8000, 32000, 48000, 96000,
3050890c2b7SRichard Genoud };
3060890c2b7SRichard Genoud 
3070890c2b7SRichard Genoud static const unsigned int wm8731_rates_11289600_16934400[] = {
3080890c2b7SRichard Genoud 	8000, 44100, 88200,
3090890c2b7SRichard Genoud };
3100890c2b7SRichard Genoud 
3110890c2b7SRichard Genoud static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
3120890c2b7SRichard Genoud 	.list = wm8731_rates_12000000,
3130890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12000000),
3140890c2b7SRichard Genoud };
3150890c2b7SRichard Genoud 
3160890c2b7SRichard Genoud static const
3170890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
3180890c2b7SRichard Genoud 	.list = wm8731_rates_12288000_18432000,
3190890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
3200890c2b7SRichard Genoud };
3210890c2b7SRichard Genoud 
3220890c2b7SRichard Genoud static const
3230890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
3240890c2b7SRichard Genoud 	.list = wm8731_rates_11289600_16934400,
3250890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
3260890c2b7SRichard Genoud };
3270890c2b7SRichard Genoud 
32840e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
32940e0aa64SRichard Purdie {
33040e0aa64SRichard Purdie 	int i;
33140e0aa64SRichard Purdie 
33240e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
33340e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
33440e0aa64SRichard Purdie 			return i;
33540e0aa64SRichard Purdie 	}
33640e0aa64SRichard Purdie 	return 0;
33740e0aa64SRichard Purdie }
33840e0aa64SRichard Purdie 
339b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
340dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
341dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
34240e0aa64SRichard Purdie {
343f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = dai->codec;
344b2c812e2SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
34517a52fd6SMark Brown 	u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
346b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
347b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
348b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
34940e0aa64SRichard Purdie 
350dd31b310SMark Brown 	wm8731->playback_fs = params_rate(params);
351dd31b310SMark Brown 
35217a52fd6SMark Brown 	snd_soc_write(codec, WM8731_SRATE, srate);
35340e0aa64SRichard Purdie 
354b36d61d4SFrank Mandarino 	/* bit size */
355dfb6778eSMark Brown 	switch (params_width(params)) {
356dfb6778eSMark Brown 	case 16:
357b36d61d4SFrank Mandarino 		break;
358dfb6778eSMark Brown 	case 20:
359b36d61d4SFrank Mandarino 		iface |= 0x0004;
360b36d61d4SFrank Mandarino 		break;
361dfb6778eSMark Brown 	case 24:
362b36d61d4SFrank Mandarino 		iface |= 0x0008;
363b36d61d4SFrank Mandarino 		break;
364b36d61d4SFrank Mandarino 	}
365b36d61d4SFrank Mandarino 
366dd31b310SMark Brown 	wm8731_set_deemph(codec);
367dd31b310SMark Brown 
36817a52fd6SMark Brown 	snd_soc_write(codec, 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 {
374b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
37517a52fd6SMark Brown 	u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
376b36d61d4SFrank Mandarino 
37740e0aa64SRichard Purdie 	if (mute)
37817a52fd6SMark Brown 		snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
37940e0aa64SRichard Purdie 	else
38017a52fd6SMark Brown 		snd_soc_write(codec, 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 {
387b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
388b2c812e2SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
389b36d61d4SFrank Mandarino 
3909745e824SMark Brown 	switch (clk_id) {
3919745e824SMark Brown 	case WM8731_SYSCLK_XTAL:
3929745e824SMark Brown 	case WM8731_SYSCLK_MCLK:
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 
420ce6120ccSLiam Girdwood 	snd_soc_dapm_sync(&codec->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 {
429b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
430b36d61d4SFrank Mandarino 	u16 iface = 0;
431b36d61d4SFrank Mandarino 
432b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
433b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
434b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
435b36d61d4SFrank Mandarino 		iface |= 0x0040;
436b36d61d4SFrank Mandarino 		break;
437b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
438b36d61d4SFrank Mandarino 		break;
439b36d61d4SFrank Mandarino 	default:
440b36d61d4SFrank Mandarino 		return -EINVAL;
441b36d61d4SFrank Mandarino 	}
442b36d61d4SFrank Mandarino 
443b36d61d4SFrank Mandarino 	/* interface format */
444b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
445b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
446b36d61d4SFrank Mandarino 		iface |= 0x0002;
447b36d61d4SFrank Mandarino 		break;
448b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
449b36d61d4SFrank Mandarino 		break;
450b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
451b36d61d4SFrank Mandarino 		iface |= 0x0001;
452b36d61d4SFrank Mandarino 		break;
453b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
454b4af6ef9SBo Shen 		iface |= 0x0013;
455b36d61d4SFrank Mandarino 		break;
456b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
457b4af6ef9SBo Shen 		iface |= 0x0003;
458b36d61d4SFrank Mandarino 		break;
459b36d61d4SFrank Mandarino 	default:
460b36d61d4SFrank Mandarino 		return -EINVAL;
461b36d61d4SFrank Mandarino 	}
462b36d61d4SFrank Mandarino 
463b36d61d4SFrank Mandarino 	/* clock inversion */
464b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
465b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
466b36d61d4SFrank Mandarino 		break;
467b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
468b36d61d4SFrank Mandarino 		iface |= 0x0090;
469b36d61d4SFrank Mandarino 		break;
470b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
471b36d61d4SFrank Mandarino 		iface |= 0x0080;
472b36d61d4SFrank Mandarino 		break;
473b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
474b36d61d4SFrank Mandarino 		iface |= 0x0010;
475b36d61d4SFrank Mandarino 		break;
476b36d61d4SFrank Mandarino 	default:
477b36d61d4SFrank Mandarino 		return -EINVAL;
478b36d61d4SFrank Mandarino 	}
479b36d61d4SFrank Mandarino 
480b36d61d4SFrank Mandarino 	/* set iface */
48117a52fd6SMark Brown 	snd_soc_write(codec, WM8731_IFACE, iface);
482b36d61d4SFrank Mandarino 	return 0;
483b36d61d4SFrank Mandarino }
484b36d61d4SFrank Mandarino 
4850be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
4860be9898aSMark Brown 				 enum snd_soc_bias_level level)
48740e0aa64SRichard Purdie {
48806ae9988SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
4899bf311feSAxel Lin 	int ret;
49022d22ee5SMark Brown 	u16 reg;
49140e0aa64SRichard Purdie 
4920be9898aSMark Brown 	switch (level) {
4930be9898aSMark Brown 	case SND_SOC_BIAS_ON:
49440e0aa64SRichard Purdie 		break;
4950be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
49640e0aa64SRichard Purdie 		break;
4970be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
498ce6120ccSLiam Girdwood 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
49906ae9988SMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
50006ae9988SMark Brown 						    wm8731->supplies);
50106ae9988SMark Brown 			if (ret != 0)
50206ae9988SMark Brown 				return ret;
50306ae9988SMark Brown 
50405d448e2SMark Brown 			regcache_sync(wm8731->regmap);
50506ae9988SMark Brown 		}
50606ae9988SMark Brown 
50722d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
50817a52fd6SMark Brown 		reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
50917a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
51040e0aa64SRichard Purdie 		break;
5110be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
51217a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, 0xffff);
51306ae9988SMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
51406ae9988SMark Brown 				       wm8731->supplies);
51505d448e2SMark Brown 		regcache_mark_dirty(wm8731->regmap);
51640e0aa64SRichard Purdie 		break;
51740e0aa64SRichard Purdie 	}
518ce6120ccSLiam Girdwood 	codec->dapm.bias_level = level;
51940e0aa64SRichard Purdie 	return 0;
52040e0aa64SRichard Purdie }
52140e0aa64SRichard Purdie 
5220890c2b7SRichard Genoud static int wm8731_startup(struct snd_pcm_substream *substream,
5230890c2b7SRichard Genoud 	struct snd_soc_dai *dai)
5240890c2b7SRichard Genoud {
5250890c2b7SRichard Genoud 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec);
5260890c2b7SRichard Genoud 
5270890c2b7SRichard Genoud 	if (wm8731->constraints)
5280890c2b7SRichard Genoud 		snd_pcm_hw_constraint_list(substream->runtime, 0,
5290890c2b7SRichard Genoud 					   SNDRV_PCM_HW_PARAM_RATE,
5300890c2b7SRichard Genoud 					   wm8731->constraints);
5310890c2b7SRichard Genoud 
5320890c2b7SRichard Genoud 	return 0;
5330890c2b7SRichard Genoud }
5340890c2b7SRichard Genoud 
535e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
536b36d61d4SFrank Mandarino 
537b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
538b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
539b36d61d4SFrank Mandarino 
54085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8731_dai_ops = {
5410890c2b7SRichard Genoud 	.startup	= wm8731_startup,
5426335d055SEric Miao 	.hw_params	= wm8731_hw_params,
5436335d055SEric Miao 	.digital_mute	= wm8731_mute,
5446335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
5456335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
5466335d055SEric Miao };
5476335d055SEric Miao 
548f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = {
549f0fba2adSLiam Girdwood 	.name = "wm8731-hifi",
55040e0aa64SRichard Purdie 	.playback = {
55140e0aa64SRichard Purdie 		.stream_name = "Playback",
55240e0aa64SRichard Purdie 		.channels_min = 1,
55340e0aa64SRichard Purdie 		.channels_max = 2,
554b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
555b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
55640e0aa64SRichard Purdie 	.capture = {
55740e0aa64SRichard Purdie 		.stream_name = "Capture",
55840e0aa64SRichard Purdie 		.channels_min = 1,
55940e0aa64SRichard Purdie 		.channels_max = 2,
560b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
561b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
5626335d055SEric Miao 	.ops = &wm8731_dai_ops,
5634934482dSMark Brown 	.symmetric_rates = 1,
56440e0aa64SRichard Purdie };
56540e0aa64SRichard Purdie 
566f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec)
56740e0aa64SRichard Purdie {
568f0fba2adSLiam Girdwood 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
569f0fba2adSLiam Girdwood 	int ret = 0, i;
57040e0aa64SRichard Purdie 
5717dea7c01SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
5727dea7c01SMark Brown 		wm8731->supplies[i].supply = wm8731_supply_names[i];
5737dea7c01SMark Brown 
5743598aad5SFabio Estevam 	ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
5757dea7c01SMark Brown 				 wm8731->supplies);
5767dea7c01SMark Brown 	if (ret != 0) {
5777dea7c01SMark Brown 		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
578f0fba2adSLiam Girdwood 		return ret;
5797dea7c01SMark Brown 	}
5807dea7c01SMark Brown 
5817dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
5827dea7c01SMark Brown 				    wm8731->supplies);
5837dea7c01SMark Brown 	if (ret != 0) {
5847dea7c01SMark Brown 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
5853598aad5SFabio Estevam 		return ret;
5867dea7c01SMark Brown 	}
5877dea7c01SMark Brown 
588519cf2dfSMark Brown 	ret = wm8731_reset(codec);
589519cf2dfSMark Brown 	if (ret < 0) {
590fe5422fcSMark Brown 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
5917dea7c01SMark Brown 		goto err_regulator_enable;
592519cf2dfSMark Brown 	}
593519cf2dfSMark Brown 
5945998102bSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
5955998102bSMark Brown 
5965998102bSMark Brown 	/* Latch the update bits */
59717a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
59817a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
59917a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
60017a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
6015998102bSMark Brown 
602ce3bdaa8SMark Brown 	/* Disable bypass path by default */
6032062ea52SDimitris Papastamos 	snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
604ce3bdaa8SMark Brown 
60506ae9988SMark Brown 	/* Regulators will have been enabled by bias management */
60606ae9988SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
60706ae9988SMark Brown 
608a8035c8fSMark Brown 	return 0;
609fe5422fcSMark Brown 
6107dea7c01SMark Brown err_regulator_enable:
6117dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
612f0fba2adSLiam Girdwood 
613fe5422fcSMark Brown 	return ret;
614a8035c8fSMark Brown }
615a8035c8fSMark Brown 
616f0fba2adSLiam Girdwood /* power down chip */
617f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec)
6185998102bSMark Brown {
619f0fba2adSLiam Girdwood 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
620f0fba2adSLiam Girdwood 
621f0fba2adSLiam Girdwood 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
622f0fba2adSLiam Girdwood 
623f0fba2adSLiam Girdwood 	return 0;
6245998102bSMark Brown }
625a8035c8fSMark Brown 
626f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
627f0fba2adSLiam Girdwood 	.probe =	wm8731_probe,
628f0fba2adSLiam Girdwood 	.remove =	wm8731_remove,
629f0fba2adSLiam Girdwood 	.set_bias_level = wm8731_set_bias_level,
6302081b2cfSLars-Peter Clausen 	.suspend_bias_off = true,
6312081b2cfSLars-Peter Clausen 
6325e251aecSMark Brown 	.dapm_widgets = wm8731_dapm_widgets,
6335e251aecSMark Brown 	.num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
6345e251aecSMark Brown 	.dapm_routes = wm8731_intercon,
6355e251aecSMark Brown 	.num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
636cb555318SMark Brown 	.controls =	wm8731_snd_controls,
637cb555318SMark Brown 	.num_controls = ARRAY_SIZE(wm8731_snd_controls),
638f0fba2adSLiam Girdwood };
639f0fba2adSLiam Girdwood 
640a7f96e4dSMark Brown static const struct of_device_id wm8731_of_match[] = {
641a7f96e4dSMark Brown 	{ .compatible = "wlf,wm8731", },
642a7f96e4dSMark Brown 	{ }
643a7f96e4dSMark Brown };
644a7f96e4dSMark Brown 
645a7f96e4dSMark Brown MODULE_DEVICE_TABLE(of, wm8731_of_match);
646a7f96e4dSMark Brown 
64705d448e2SMark Brown static const struct regmap_config wm8731_regmap = {
64805d448e2SMark Brown 	.reg_bits = 7,
64905d448e2SMark Brown 	.val_bits = 9,
65005d448e2SMark Brown 
65105d448e2SMark Brown 	.max_register = WM8731_RESET,
65205d448e2SMark Brown 	.volatile_reg = wm8731_volatile,
65305d448e2SMark Brown 	.writeable_reg = wm8731_writeable,
65405d448e2SMark Brown 
65505d448e2SMark Brown 	.cache_type = REGCACHE_RBTREE,
65605d448e2SMark Brown 	.reg_defaults = wm8731_reg_defaults,
65705d448e2SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
65805d448e2SMark Brown };
65905d448e2SMark Brown 
6605998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
6617a79e94eSBill Pemberton static int wm8731_spi_probe(struct spi_device *spi)
6625998102bSMark Brown {
6635998102bSMark Brown 	struct wm8731_priv *wm8731;
664f0fba2adSLiam Girdwood 	int ret;
6655998102bSMark Brown 
666cea82d8aSFabio Estevam 	wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL);
6675998102bSMark Brown 	if (wm8731 == NULL)
6685998102bSMark Brown 		return -ENOMEM;
6695998102bSMark Brown 
670a51ff30fSLars-Peter Clausen 	mutex_init(&wm8731->lock);
671a51ff30fSLars-Peter Clausen 
672f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
67305d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
67405d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
67505d448e2SMark Brown 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
67605d448e2SMark Brown 			ret);
677f1992ddeSMark Brown 		return ret;
67805d448e2SMark Brown 	}
67905d448e2SMark Brown 
680f0fba2adSLiam Girdwood 	spi_set_drvdata(spi, wm8731);
6815998102bSMark Brown 
682f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&spi->dev,
683f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
68405d448e2SMark Brown 	if (ret != 0) {
68505d448e2SMark Brown 		dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
686f1992ddeSMark Brown 		return ret;
68705d448e2SMark Brown 	}
68805d448e2SMark Brown 
68905d448e2SMark Brown 	return 0;
6905998102bSMark Brown }
6915998102bSMark Brown 
6927a79e94eSBill Pemberton static int wm8731_spi_remove(struct spi_device *spi)
6935998102bSMark Brown {
694f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&spi->dev);
6955998102bSMark Brown 	return 0;
6965998102bSMark Brown }
6975998102bSMark Brown 
6985998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
6995998102bSMark Brown 	.driver = {
70099b59f3cSMark Brown 		.name	= "wm8731",
7015998102bSMark Brown 		.owner	= THIS_MODULE,
702a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
7035998102bSMark Brown 	},
7045998102bSMark Brown 	.probe		= wm8731_spi_probe,
7057a79e94eSBill Pemberton 	.remove		= wm8731_spi_remove,
7065998102bSMark Brown };
707a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
708a8035c8fSMark Brown 
709b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7107a79e94eSBill Pemberton static int wm8731_i2c_probe(struct i2c_client *i2c,
711a8035c8fSMark Brown 			    const struct i2c_device_id *id)
712a8035c8fSMark Brown {
7135998102bSMark Brown 	struct wm8731_priv *wm8731;
714f0fba2adSLiam Girdwood 	int ret;
715a8035c8fSMark Brown 
716f1992ddeSMark Brown 	wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
717f1992ddeSMark Brown 			      GFP_KERNEL);
7185998102bSMark Brown 	if (wm8731 == NULL)
7195998102bSMark Brown 		return -ENOMEM;
7205998102bSMark Brown 
7218a6cf30bSManuel Lauss 	mutex_init(&wm8731->lock);
7228a6cf30bSManuel Lauss 
723f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
72405d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
72505d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
72605d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
72705d448e2SMark Brown 			ret);
728f1992ddeSMark Brown 		return ret;
72905d448e2SMark Brown 	}
73005d448e2SMark Brown 
7315998102bSMark Brown 	i2c_set_clientdata(i2c, wm8731);
732a8035c8fSMark Brown 
733f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&i2c->dev,
734f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
73505d448e2SMark Brown 	if (ret != 0) {
73605d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
737f1992ddeSMark Brown 		return ret;
73805d448e2SMark Brown 	}
73905d448e2SMark Brown 
74005d448e2SMark Brown 	return 0;
741a8035c8fSMark Brown }
742a8035c8fSMark Brown 
7437a79e94eSBill Pemberton static int wm8731_i2c_remove(struct i2c_client *client)
744a8035c8fSMark Brown {
745f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&client->dev);
746a8035c8fSMark Brown 	return 0;
747a8035c8fSMark Brown }
748a8035c8fSMark Brown 
749a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
750a8035c8fSMark Brown 	{ "wm8731", 0 },
751a8035c8fSMark Brown 	{ }
752a8035c8fSMark Brown };
753a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
754a8035c8fSMark Brown 
755a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
756a8035c8fSMark Brown 	.driver = {
75799b59f3cSMark Brown 		.name = "wm8731",
758a8035c8fSMark Brown 		.owner = THIS_MODULE,
759a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
760a8035c8fSMark Brown 	},
761a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
7627a79e94eSBill Pemberton 	.remove =   wm8731_i2c_remove,
763a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
764a8035c8fSMark Brown };
765a8035c8fSMark Brown #endif
766a8035c8fSMark Brown 
767c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
76864089b84SMark Brown {
769f0fba2adSLiam Girdwood 	int ret = 0;
770b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7715998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
7725998102bSMark Brown 	if (ret != 0) {
7735998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
7745998102bSMark Brown 		       ret);
7755998102bSMark Brown 	}
7765998102bSMark Brown #endif
7775998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7785998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
7795998102bSMark Brown 	if (ret != 0) {
7805998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
7815998102bSMark Brown 		       ret);
7825998102bSMark Brown 	}
7835998102bSMark Brown #endif
784f0fba2adSLiam Girdwood 	return ret;
78564089b84SMark Brown }
78664089b84SMark Brown module_init(wm8731_modinit);
78764089b84SMark Brown 
78864089b84SMark Brown static void __exit wm8731_exit(void)
78964089b84SMark Brown {
790b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7915998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
7925998102bSMark Brown #endif
7935998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7945998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
7955998102bSMark Brown #endif
79664089b84SMark Brown }
79764089b84SMark Brown module_exit(wm8731_exit);
79864089b84SMark Brown 
79940e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
80040e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
80140e0aa64SRichard Purdie MODULE_LICENSE("GPL");
802