xref: /linux/sound/soc/codecs/wm8731.c (revision 3598aad5470deb5eb4f643ef89abb9b172803e76)
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>
2740e0aa64SRichard Purdie #include <sound/core.h>
2840e0aa64SRichard Purdie #include <sound/pcm.h>
2940e0aa64SRichard Purdie #include <sound/pcm_params.h>
3040e0aa64SRichard Purdie #include <sound/soc.h>
3140e0aa64SRichard Purdie #include <sound/initval.h>
32d00efa64SMark Brown #include <sound/tlv.h>
3340e0aa64SRichard Purdie 
3440e0aa64SRichard Purdie #include "wm8731.h"
3540e0aa64SRichard Purdie 
367dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4
377dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
387dea7c01SMark Brown 	"AVDD",
397dea7c01SMark Brown 	"HPVDD",
407dea7c01SMark Brown 	"DCVDD",
417dea7c01SMark Brown 	"DBVDD",
427dea7c01SMark Brown };
437dea7c01SMark Brown 
44b36d61d4SFrank Mandarino /* codec private data */
45b36d61d4SFrank Mandarino struct wm8731_priv {
4605d448e2SMark Brown 	struct regmap *regmap;
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;
53b36d61d4SFrank Mandarino };
54b36d61d4SFrank Mandarino 
55a8035c8fSMark Brown 
5640e0aa64SRichard Purdie /*
5740e0aa64SRichard Purdie  * wm8731 register cache
5840e0aa64SRichard Purdie  */
5905d448e2SMark Brown static const struct reg_default wm8731_reg_defaults[] = {
6005d448e2SMark Brown 	{ 0, 0x0097 },
6105d448e2SMark Brown 	{ 1, 0x0097 },
6205d448e2SMark Brown 	{ 2, 0x0079 },
6305d448e2SMark Brown 	{ 3, 0x0079 },
6405d448e2SMark Brown 	{ 4, 0x000a },
6505d448e2SMark Brown 	{ 5, 0x0008 },
6605d448e2SMark Brown 	{ 6, 0x009f },
6705d448e2SMark Brown 	{ 7, 0x000a },
6805d448e2SMark Brown 	{ 8, 0x0000 },
6905d448e2SMark Brown 	{ 9, 0x0000 },
7040e0aa64SRichard Purdie };
7140e0aa64SRichard Purdie 
7205d448e2SMark Brown static bool wm8731_volatile(struct device *dev, unsigned int reg)
7305d448e2SMark Brown {
7405d448e2SMark Brown 	return reg == WM8731_RESET;
7505d448e2SMark Brown }
7605d448e2SMark Brown 
7705d448e2SMark Brown static bool wm8731_writeable(struct device *dev, unsigned int reg)
7805d448e2SMark Brown {
7905d448e2SMark Brown 	return reg <= WM8731_RESET;
8005d448e2SMark Brown }
8105d448e2SMark Brown 
8217a52fd6SMark Brown #define wm8731_reset(c)	snd_soc_write(c, 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 
91dd31b310SMark Brown static int wm8731_set_deemph(struct snd_soc_codec *codec)
92dd31b310SMark Brown {
93dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
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 
113dd31b310SMark Brown 	dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
114dd31b310SMark Brown 		best, wm8731_deemph[best]);
115dd31b310SMark Brown 
116dd31b310SMark Brown 	return snd_soc_update_bits(codec, 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 {
122dd31b310SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
123dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
124dd31b310SMark Brown 
125dd31b310SMark Brown 	ucontrol->value.enumerated.item[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 {
133dd31b310SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
134dd31b310SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
135dd31b310SMark Brown 	int deemph = ucontrol->value.enumerated.item[0];
136dd31b310SMark Brown 	int ret = 0;
137dd31b310SMark Brown 
138dd31b310SMark Brown 	if (deemph > 1)
139dd31b310SMark Brown 		return -EINVAL;
140dd31b310SMark Brown 
141dd31b310SMark Brown 	mutex_lock(&codec->mutex);
142dd31b310SMark Brown 	if (wm8731->deemph != deemph) {
143dd31b310SMark Brown 		wm8731->deemph = deemph;
144dd31b310SMark Brown 
145dd31b310SMark Brown 		wm8731_set_deemph(codec);
146dd31b310SMark Brown 
147dd31b310SMark Brown 		ret = 1;
148dd31b310SMark Brown 	}
149dd31b310SMark Brown 	mutex_unlock(&codec->mutex);
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 {
2179745e824SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
2189745e824SMark Brown 
2195a195b44SNicolas Ferre 	return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
2209745e824SMark Brown }
2219745e824SMark Brown 
2225e251aecSMark Brown static const struct snd_soc_dapm_route wm8731_intercon[] = {
2239745e824SMark Brown 	{"DAC", NULL, "OSC", wm8731_check_osc},
2249745e824SMark Brown 	{"ADC", NULL, "OSC", wm8731_check_osc},
2258a27bd9aSMark Brown 	{"DAC", NULL, "ACTIVE"},
2268a27bd9aSMark Brown 	{"ADC", NULL, "ACTIVE"},
2279745e824SMark Brown 
22840e0aa64SRichard Purdie 	/* output mixer */
22940e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
23040e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
23140e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
23240e0aa64SRichard Purdie 
23340e0aa64SRichard Purdie 	/* outputs */
23440e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
23540e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
23640e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
23740e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
23840e0aa64SRichard Purdie 
23940e0aa64SRichard Purdie 	/* input mux */
24040e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
24140e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
24240e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
24340e0aa64SRichard Purdie 
24440e0aa64SRichard Purdie 	/* inputs */
24540e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
24640e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
24740e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
24840e0aa64SRichard Purdie };
24940e0aa64SRichard Purdie 
25040e0aa64SRichard Purdie struct _coeff_div {
25140e0aa64SRichard Purdie 	u32 mclk;
25240e0aa64SRichard Purdie 	u32 rate;
25340e0aa64SRichard Purdie 	u16 fs;
25440e0aa64SRichard Purdie 	u8 sr:4;
25540e0aa64SRichard Purdie 	u8 bosr:1;
25640e0aa64SRichard Purdie 	u8 usb:1;
25740e0aa64SRichard Purdie };
25840e0aa64SRichard Purdie 
25940e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
26040e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
26140e0aa64SRichard Purdie 	/* 48k */
26240e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
26340e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
26440e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
26540e0aa64SRichard Purdie 
26640e0aa64SRichard Purdie 	/* 32k */
26740e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
26840e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
269298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
27040e0aa64SRichard Purdie 
27140e0aa64SRichard Purdie 	/* 8k */
27240e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
27340e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
27440e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
27540e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
27640e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
27740e0aa64SRichard Purdie 
27840e0aa64SRichard Purdie 	/* 96k */
27940e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
28040e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
28140e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
28240e0aa64SRichard Purdie 
28340e0aa64SRichard Purdie 	/* 44.1k */
28440e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
28540e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
28640e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
28740e0aa64SRichard Purdie 
28840e0aa64SRichard Purdie 	/* 88.2k */
28940e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
29040e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
29140e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
29240e0aa64SRichard Purdie };
29340e0aa64SRichard Purdie 
2940890c2b7SRichard Genoud /* rates constraints */
2950890c2b7SRichard Genoud static const unsigned int wm8731_rates_12000000[] = {
2960890c2b7SRichard Genoud 	8000, 32000, 44100, 48000, 96000, 88200,
2970890c2b7SRichard Genoud };
2980890c2b7SRichard Genoud 
2990890c2b7SRichard Genoud static const unsigned int wm8731_rates_12288000_18432000[] = {
3000890c2b7SRichard Genoud 	8000, 32000, 48000, 96000,
3010890c2b7SRichard Genoud };
3020890c2b7SRichard Genoud 
3030890c2b7SRichard Genoud static const unsigned int wm8731_rates_11289600_16934400[] = {
3040890c2b7SRichard Genoud 	8000, 44100, 88200,
3050890c2b7SRichard Genoud };
3060890c2b7SRichard Genoud 
3070890c2b7SRichard Genoud static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
3080890c2b7SRichard Genoud 	.list = wm8731_rates_12000000,
3090890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12000000),
3100890c2b7SRichard Genoud };
3110890c2b7SRichard Genoud 
3120890c2b7SRichard Genoud static const
3130890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
3140890c2b7SRichard Genoud 	.list = wm8731_rates_12288000_18432000,
3150890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
3160890c2b7SRichard Genoud };
3170890c2b7SRichard Genoud 
3180890c2b7SRichard Genoud static const
3190890c2b7SRichard Genoud struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
3200890c2b7SRichard Genoud 	.list = wm8731_rates_11289600_16934400,
3210890c2b7SRichard Genoud 	.count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
3220890c2b7SRichard Genoud };
3230890c2b7SRichard Genoud 
32440e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
32540e0aa64SRichard Purdie {
32640e0aa64SRichard Purdie 	int i;
32740e0aa64SRichard Purdie 
32840e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
32940e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
33040e0aa64SRichard Purdie 			return i;
33140e0aa64SRichard Purdie 	}
33240e0aa64SRichard Purdie 	return 0;
33340e0aa64SRichard Purdie }
33440e0aa64SRichard Purdie 
335b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
336dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
337dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
33840e0aa64SRichard Purdie {
339f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = dai->codec;
340b2c812e2SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
34117a52fd6SMark Brown 	u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
342b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
343b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
344b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
34540e0aa64SRichard Purdie 
346dd31b310SMark Brown 	wm8731->playback_fs = params_rate(params);
347dd31b310SMark Brown 
34817a52fd6SMark Brown 	snd_soc_write(codec, WM8731_SRATE, srate);
34940e0aa64SRichard Purdie 
350b36d61d4SFrank Mandarino 	/* bit size */
351b36d61d4SFrank Mandarino 	switch (params_format(params)) {
352b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
353b36d61d4SFrank Mandarino 		break;
354b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
355b36d61d4SFrank Mandarino 		iface |= 0x0004;
356b36d61d4SFrank Mandarino 		break;
357b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
358b36d61d4SFrank Mandarino 		iface |= 0x0008;
359b36d61d4SFrank Mandarino 		break;
360b36d61d4SFrank Mandarino 	}
361b36d61d4SFrank Mandarino 
362dd31b310SMark Brown 	wm8731_set_deemph(codec);
363dd31b310SMark Brown 
36417a52fd6SMark Brown 	snd_soc_write(codec, WM8731_IFACE, iface);
365b36d61d4SFrank Mandarino 	return 0;
36640e0aa64SRichard Purdie }
36740e0aa64SRichard Purdie 
368e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
36940e0aa64SRichard Purdie {
370b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
37117a52fd6SMark Brown 	u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
372b36d61d4SFrank Mandarino 
37340e0aa64SRichard Purdie 	if (mute)
37417a52fd6SMark Brown 		snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
37540e0aa64SRichard Purdie 	else
37617a52fd6SMark Brown 		snd_soc_write(codec, WM8731_APDIGI, mute_reg);
37740e0aa64SRichard Purdie 	return 0;
37840e0aa64SRichard Purdie }
37940e0aa64SRichard Purdie 
380e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
381b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
382b36d61d4SFrank Mandarino {
383b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
384b2c812e2SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
385b36d61d4SFrank Mandarino 
3869745e824SMark Brown 	switch (clk_id) {
3879745e824SMark Brown 	case WM8731_SYSCLK_XTAL:
3889745e824SMark Brown 	case WM8731_SYSCLK_MCLK:
3899745e824SMark Brown 		wm8731->sysclk_type = clk_id;
3909745e824SMark Brown 		break;
3919745e824SMark Brown 	default:
3929745e824SMark Brown 		return -EINVAL;
3939745e824SMark Brown 	}
3949745e824SMark Brown 
395b36d61d4SFrank Mandarino 	switch (freq) {
3960890c2b7SRichard Genoud 	case 0:
3970890c2b7SRichard Genoud 		wm8731->constraints = NULL;
3980890c2b7SRichard Genoud 		break;
399b36d61d4SFrank Mandarino 	case 12000000:
4000890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12000000;
4010890c2b7SRichard Genoud 		break;
402b36d61d4SFrank Mandarino 	case 12288000:
403b36d61d4SFrank Mandarino 	case 18432000:
4040890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_12288000_18432000;
4050890c2b7SRichard Genoud 		break;
4060890c2b7SRichard Genoud 	case 16934400:
4070890c2b7SRichard Genoud 	case 11289600:
4080890c2b7SRichard Genoud 		wm8731->constraints = &wm8731_constraints_11289600_16934400;
4099745e824SMark Brown 		break;
4109745e824SMark Brown 	default:
411b36d61d4SFrank Mandarino 		return -EINVAL;
412b36d61d4SFrank Mandarino 	}
413b36d61d4SFrank Mandarino 
4140890c2b7SRichard Genoud 	wm8731->sysclk = freq;
4150890c2b7SRichard Genoud 
416ce6120ccSLiam Girdwood 	snd_soc_dapm_sync(&codec->dapm);
4179745e824SMark Brown 
4189745e824SMark Brown 	return 0;
4199745e824SMark Brown }
4209745e824SMark Brown 
421b36d61d4SFrank Mandarino 
422e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
423b36d61d4SFrank Mandarino 		unsigned int fmt)
424b36d61d4SFrank Mandarino {
425b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
426b36d61d4SFrank Mandarino 	u16 iface = 0;
427b36d61d4SFrank Mandarino 
428b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
429b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
430b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
431b36d61d4SFrank Mandarino 		iface |= 0x0040;
432b36d61d4SFrank Mandarino 		break;
433b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
434b36d61d4SFrank Mandarino 		break;
435b36d61d4SFrank Mandarino 	default:
436b36d61d4SFrank Mandarino 		return -EINVAL;
437b36d61d4SFrank Mandarino 	}
438b36d61d4SFrank Mandarino 
439b36d61d4SFrank Mandarino 	/* interface format */
440b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
441b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
442b36d61d4SFrank Mandarino 		iface |= 0x0002;
443b36d61d4SFrank Mandarino 		break;
444b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
445b36d61d4SFrank Mandarino 		break;
446b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
447b36d61d4SFrank Mandarino 		iface |= 0x0001;
448b36d61d4SFrank Mandarino 		break;
449b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
450b4af6ef9SBo Shen 		iface |= 0x0013;
451b36d61d4SFrank Mandarino 		break;
452b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
453b4af6ef9SBo Shen 		iface |= 0x0003;
454b36d61d4SFrank Mandarino 		break;
455b36d61d4SFrank Mandarino 	default:
456b36d61d4SFrank Mandarino 		return -EINVAL;
457b36d61d4SFrank Mandarino 	}
458b36d61d4SFrank Mandarino 
459b36d61d4SFrank Mandarino 	/* clock inversion */
460b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
461b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
462b36d61d4SFrank Mandarino 		break;
463b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
464b36d61d4SFrank Mandarino 		iface |= 0x0090;
465b36d61d4SFrank Mandarino 		break;
466b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
467b36d61d4SFrank Mandarino 		iface |= 0x0080;
468b36d61d4SFrank Mandarino 		break;
469b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
470b36d61d4SFrank Mandarino 		iface |= 0x0010;
471b36d61d4SFrank Mandarino 		break;
472b36d61d4SFrank Mandarino 	default:
473b36d61d4SFrank Mandarino 		return -EINVAL;
474b36d61d4SFrank Mandarino 	}
475b36d61d4SFrank Mandarino 
476b36d61d4SFrank Mandarino 	/* set iface */
47717a52fd6SMark Brown 	snd_soc_write(codec, WM8731_IFACE, iface);
478b36d61d4SFrank Mandarino 	return 0;
479b36d61d4SFrank Mandarino }
480b36d61d4SFrank Mandarino 
4810be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
4820be9898aSMark Brown 				 enum snd_soc_bias_level level)
48340e0aa64SRichard Purdie {
48406ae9988SMark Brown 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
4859bf311feSAxel Lin 	int ret;
48622d22ee5SMark Brown 	u16 reg;
48740e0aa64SRichard Purdie 
4880be9898aSMark Brown 	switch (level) {
4890be9898aSMark Brown 	case SND_SOC_BIAS_ON:
49040e0aa64SRichard Purdie 		break;
4910be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
49240e0aa64SRichard Purdie 		break;
4930be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
494ce6120ccSLiam Girdwood 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
49506ae9988SMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
49606ae9988SMark Brown 						    wm8731->supplies);
49706ae9988SMark Brown 			if (ret != 0)
49806ae9988SMark Brown 				return ret;
49906ae9988SMark Brown 
50005d448e2SMark Brown 			regcache_sync(wm8731->regmap);
50106ae9988SMark Brown 		}
50206ae9988SMark Brown 
50322d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
50417a52fd6SMark Brown 		reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
50517a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
50640e0aa64SRichard Purdie 		break;
5070be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
50817a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, 0xffff);
50906ae9988SMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
51006ae9988SMark Brown 				       wm8731->supplies);
51105d448e2SMark Brown 		regcache_mark_dirty(wm8731->regmap);
51240e0aa64SRichard Purdie 		break;
51340e0aa64SRichard Purdie 	}
514ce6120ccSLiam Girdwood 	codec->dapm.bias_level = level;
51540e0aa64SRichard Purdie 	return 0;
51640e0aa64SRichard Purdie }
51740e0aa64SRichard Purdie 
5180890c2b7SRichard Genoud static int wm8731_startup(struct snd_pcm_substream *substream,
5190890c2b7SRichard Genoud 	struct snd_soc_dai *dai)
5200890c2b7SRichard Genoud {
5210890c2b7SRichard Genoud 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec);
5220890c2b7SRichard Genoud 
5230890c2b7SRichard Genoud 	if (wm8731->constraints)
5240890c2b7SRichard Genoud 		snd_pcm_hw_constraint_list(substream->runtime, 0,
5250890c2b7SRichard Genoud 					   SNDRV_PCM_HW_PARAM_RATE,
5260890c2b7SRichard Genoud 					   wm8731->constraints);
5270890c2b7SRichard Genoud 
5280890c2b7SRichard Genoud 	return 0;
5290890c2b7SRichard Genoud }
5300890c2b7SRichard Genoud 
531e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
532b36d61d4SFrank Mandarino 
533b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
534b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
535b36d61d4SFrank Mandarino 
53685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8731_dai_ops = {
5370890c2b7SRichard Genoud 	.startup	= wm8731_startup,
5386335d055SEric Miao 	.hw_params	= wm8731_hw_params,
5396335d055SEric Miao 	.digital_mute	= wm8731_mute,
5406335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
5416335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
5426335d055SEric Miao };
5436335d055SEric Miao 
544f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = {
545f0fba2adSLiam Girdwood 	.name = "wm8731-hifi",
54640e0aa64SRichard Purdie 	.playback = {
54740e0aa64SRichard Purdie 		.stream_name = "Playback",
54840e0aa64SRichard Purdie 		.channels_min = 1,
54940e0aa64SRichard Purdie 		.channels_max = 2,
550b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
551b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
55240e0aa64SRichard Purdie 	.capture = {
55340e0aa64SRichard Purdie 		.stream_name = "Capture",
55440e0aa64SRichard Purdie 		.channels_min = 1,
55540e0aa64SRichard Purdie 		.channels_max = 2,
556b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
557b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
5586335d055SEric Miao 	.ops = &wm8731_dai_ops,
5594934482dSMark Brown 	.symmetric_rates = 1,
56040e0aa64SRichard Purdie };
56140e0aa64SRichard Purdie 
562b3b50b3fSMark Brown #ifdef CONFIG_PM
56384b315eeSLars-Peter Clausen static int wm8731_suspend(struct snd_soc_codec *codec)
56440e0aa64SRichard Purdie {
5650be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
56606ae9988SMark Brown 
56740e0aa64SRichard Purdie 	return 0;
56840e0aa64SRichard Purdie }
56940e0aa64SRichard Purdie 
570f0fba2adSLiam Girdwood static int wm8731_resume(struct snd_soc_codec *codec)
57140e0aa64SRichard Purdie {
5720be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
5737dea7c01SMark Brown 
57440e0aa64SRichard Purdie 	return 0;
57540e0aa64SRichard Purdie }
576b3b50b3fSMark Brown #else
577b3b50b3fSMark Brown #define wm8731_suspend NULL
578b3b50b3fSMark Brown #define wm8731_resume NULL
579b3b50b3fSMark Brown #endif
58040e0aa64SRichard Purdie 
581f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec)
58240e0aa64SRichard Purdie {
583f0fba2adSLiam Girdwood 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
584f0fba2adSLiam Girdwood 	int ret = 0, i;
58540e0aa64SRichard Purdie 
5867dea7c01SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
5877dea7c01SMark Brown 		wm8731->supplies[i].supply = wm8731_supply_names[i];
5887dea7c01SMark Brown 
589*3598aad5SFabio Estevam 	ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
5907dea7c01SMark Brown 				 wm8731->supplies);
5917dea7c01SMark Brown 	if (ret != 0) {
5927dea7c01SMark Brown 		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
593f0fba2adSLiam Girdwood 		return ret;
5947dea7c01SMark Brown 	}
5957dea7c01SMark Brown 
5967dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
5977dea7c01SMark Brown 				    wm8731->supplies);
5987dea7c01SMark Brown 	if (ret != 0) {
5997dea7c01SMark Brown 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
600*3598aad5SFabio Estevam 		return ret;
6017dea7c01SMark Brown 	}
6027dea7c01SMark Brown 
603519cf2dfSMark Brown 	ret = wm8731_reset(codec);
604519cf2dfSMark Brown 	if (ret < 0) {
605fe5422fcSMark Brown 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
6067dea7c01SMark Brown 		goto err_regulator_enable;
607519cf2dfSMark Brown 	}
608519cf2dfSMark Brown 
6095998102bSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
6105998102bSMark Brown 
6115998102bSMark Brown 	/* Latch the update bits */
61217a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
61317a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
61417a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
61517a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
6165998102bSMark Brown 
617ce3bdaa8SMark Brown 	/* Disable bypass path by default */
6182062ea52SDimitris Papastamos 	snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
619ce3bdaa8SMark Brown 
62006ae9988SMark Brown 	/* Regulators will have been enabled by bias management */
62106ae9988SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
62206ae9988SMark Brown 
623a8035c8fSMark Brown 	return 0;
624fe5422fcSMark Brown 
6257dea7c01SMark Brown err_regulator_enable:
6267dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
627f0fba2adSLiam Girdwood 
628fe5422fcSMark Brown 	return ret;
629a8035c8fSMark Brown }
630a8035c8fSMark Brown 
631f0fba2adSLiam Girdwood /* power down chip */
632f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec)
6335998102bSMark Brown {
634f0fba2adSLiam Girdwood 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
635f0fba2adSLiam Girdwood 
636f0fba2adSLiam Girdwood 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
637f0fba2adSLiam Girdwood 
638f0fba2adSLiam Girdwood 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
639f0fba2adSLiam Girdwood 
640f0fba2adSLiam Girdwood 	return 0;
6415998102bSMark Brown }
642a8035c8fSMark Brown 
643f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
644f0fba2adSLiam Girdwood 	.probe =	wm8731_probe,
645f0fba2adSLiam Girdwood 	.remove =	wm8731_remove,
646f0fba2adSLiam Girdwood 	.suspend =	wm8731_suspend,
647f0fba2adSLiam Girdwood 	.resume =	wm8731_resume,
648f0fba2adSLiam Girdwood 	.set_bias_level = wm8731_set_bias_level,
6495e251aecSMark Brown 	.dapm_widgets = wm8731_dapm_widgets,
6505e251aecSMark Brown 	.num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
6515e251aecSMark Brown 	.dapm_routes = wm8731_intercon,
6525e251aecSMark Brown 	.num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
653cb555318SMark Brown 	.controls =	wm8731_snd_controls,
654cb555318SMark Brown 	.num_controls = ARRAY_SIZE(wm8731_snd_controls),
655f0fba2adSLiam Girdwood };
656f0fba2adSLiam Girdwood 
657a7f96e4dSMark Brown static const struct of_device_id wm8731_of_match[] = {
658a7f96e4dSMark Brown 	{ .compatible = "wlf,wm8731", },
659a7f96e4dSMark Brown 	{ }
660a7f96e4dSMark Brown };
661a7f96e4dSMark Brown 
662a7f96e4dSMark Brown MODULE_DEVICE_TABLE(of, wm8731_of_match);
663a7f96e4dSMark Brown 
66405d448e2SMark Brown static const struct regmap_config wm8731_regmap = {
66505d448e2SMark Brown 	.reg_bits = 7,
66605d448e2SMark Brown 	.val_bits = 9,
66705d448e2SMark Brown 
66805d448e2SMark Brown 	.max_register = WM8731_RESET,
66905d448e2SMark Brown 	.volatile_reg = wm8731_volatile,
67005d448e2SMark Brown 	.writeable_reg = wm8731_writeable,
67105d448e2SMark Brown 
67205d448e2SMark Brown 	.cache_type = REGCACHE_RBTREE,
67305d448e2SMark Brown 	.reg_defaults = wm8731_reg_defaults,
67405d448e2SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
67505d448e2SMark Brown };
67605d448e2SMark Brown 
6775998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
6787a79e94eSBill Pemberton static int wm8731_spi_probe(struct spi_device *spi)
6795998102bSMark Brown {
6805998102bSMark Brown 	struct wm8731_priv *wm8731;
681f0fba2adSLiam Girdwood 	int ret;
6825998102bSMark Brown 
683f1992ddeSMark Brown 	wm8731 = devm_kzalloc(&spi->dev, sizeof(struct wm8731_priv),
684f1992ddeSMark Brown 			      GFP_KERNEL);
6855998102bSMark Brown 	if (wm8731 == NULL)
6865998102bSMark Brown 		return -ENOMEM;
6875998102bSMark Brown 
688f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
68905d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
69005d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
69105d448e2SMark Brown 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
69205d448e2SMark Brown 			ret);
693f1992ddeSMark Brown 		return ret;
69405d448e2SMark Brown 	}
69505d448e2SMark Brown 
696f0fba2adSLiam Girdwood 	spi_set_drvdata(spi, wm8731);
6975998102bSMark Brown 
698f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&spi->dev,
699f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
70005d448e2SMark Brown 	if (ret != 0) {
70105d448e2SMark Brown 		dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
702f1992ddeSMark Brown 		return ret;
70305d448e2SMark Brown 	}
70405d448e2SMark Brown 
70505d448e2SMark Brown 	return 0;
7065998102bSMark Brown }
7075998102bSMark Brown 
7087a79e94eSBill Pemberton static int wm8731_spi_remove(struct spi_device *spi)
7095998102bSMark Brown {
710f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&spi->dev);
7115998102bSMark Brown 	return 0;
7125998102bSMark Brown }
7135998102bSMark Brown 
7145998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
7155998102bSMark Brown 	.driver = {
71699b59f3cSMark Brown 		.name	= "wm8731",
7175998102bSMark Brown 		.owner	= THIS_MODULE,
718a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
7195998102bSMark Brown 	},
7205998102bSMark Brown 	.probe		= wm8731_spi_probe,
7217a79e94eSBill Pemberton 	.remove		= wm8731_spi_remove,
7225998102bSMark Brown };
723a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
724a8035c8fSMark Brown 
725b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7267a79e94eSBill Pemberton static int wm8731_i2c_probe(struct i2c_client *i2c,
727a8035c8fSMark Brown 			    const struct i2c_device_id *id)
728a8035c8fSMark Brown {
7295998102bSMark Brown 	struct wm8731_priv *wm8731;
730f0fba2adSLiam Girdwood 	int ret;
731a8035c8fSMark Brown 
732f1992ddeSMark Brown 	wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
733f1992ddeSMark Brown 			      GFP_KERNEL);
7345998102bSMark Brown 	if (wm8731 == NULL)
7355998102bSMark Brown 		return -ENOMEM;
7365998102bSMark Brown 
737f1992ddeSMark Brown 	wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
73805d448e2SMark Brown 	if (IS_ERR(wm8731->regmap)) {
73905d448e2SMark Brown 		ret = PTR_ERR(wm8731->regmap);
74005d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
74105d448e2SMark Brown 			ret);
742f1992ddeSMark Brown 		return ret;
74305d448e2SMark Brown 	}
74405d448e2SMark Brown 
7455998102bSMark Brown 	i2c_set_clientdata(i2c, wm8731);
746a8035c8fSMark Brown 
747f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&i2c->dev,
748f0fba2adSLiam Girdwood 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
74905d448e2SMark Brown 	if (ret != 0) {
75005d448e2SMark Brown 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
751f1992ddeSMark Brown 		return ret;
75205d448e2SMark Brown 	}
75305d448e2SMark Brown 
75405d448e2SMark Brown 	return 0;
755a8035c8fSMark Brown }
756a8035c8fSMark Brown 
7577a79e94eSBill Pemberton static int wm8731_i2c_remove(struct i2c_client *client)
758a8035c8fSMark Brown {
759f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&client->dev);
760a8035c8fSMark Brown 	return 0;
761a8035c8fSMark Brown }
762a8035c8fSMark Brown 
763a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
764a8035c8fSMark Brown 	{ "wm8731", 0 },
765a8035c8fSMark Brown 	{ }
766a8035c8fSMark Brown };
767a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
768a8035c8fSMark Brown 
769a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
770a8035c8fSMark Brown 	.driver = {
77199b59f3cSMark Brown 		.name = "wm8731",
772a8035c8fSMark Brown 		.owner = THIS_MODULE,
773a7f96e4dSMark Brown 		.of_match_table = wm8731_of_match,
774a8035c8fSMark Brown 	},
775a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
7767a79e94eSBill Pemberton 	.remove =   wm8731_i2c_remove,
777a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
778a8035c8fSMark Brown };
779a8035c8fSMark Brown #endif
780a8035c8fSMark Brown 
781c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
78264089b84SMark Brown {
783f0fba2adSLiam Girdwood 	int ret = 0;
784b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7855998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
7865998102bSMark Brown 	if (ret != 0) {
7875998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
7885998102bSMark Brown 		       ret);
7895998102bSMark Brown 	}
7905998102bSMark Brown #endif
7915998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7925998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
7935998102bSMark Brown 	if (ret != 0) {
7945998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
7955998102bSMark Brown 		       ret);
7965998102bSMark Brown 	}
7975998102bSMark Brown #endif
798f0fba2adSLiam Girdwood 	return ret;
79964089b84SMark Brown }
80064089b84SMark Brown module_init(wm8731_modinit);
80164089b84SMark Brown 
80264089b84SMark Brown static void __exit wm8731_exit(void)
80364089b84SMark Brown {
804b65ab73eSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
8055998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
8065998102bSMark Brown #endif
8075998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8085998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
8095998102bSMark Brown #endif
81064089b84SMark Brown }
81164089b84SMark Brown module_exit(wm8731_exit);
81264089b84SMark Brown 
81340e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
81440e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
81540e0aa64SRichard Purdie MODULE_LICENSE("GPL");
816