xref: /linux/sound/soc/codecs/wm8737.c (revision ca275811fd2eef7e0121fefbc46cc5b47d680b10)
12a9ae13aSMark Brown /*
22a9ae13aSMark Brown  * wm8737.c  --  WM8737 ALSA SoC Audio driver
32a9ae13aSMark Brown  *
42a9ae13aSMark Brown  * Copyright 2010 Wolfson Microelectronics plc
52a9ae13aSMark Brown  *
62a9ae13aSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
72a9ae13aSMark Brown  *
82a9ae13aSMark Brown  * This program is free software; you can redistribute it and/or modify
92a9ae13aSMark Brown  * it under the terms of the GNU General Public License version 2 as
102a9ae13aSMark Brown  * published by the Free Software Foundation.
112a9ae13aSMark Brown  */
122a9ae13aSMark Brown 
132a9ae13aSMark Brown #include <linux/module.h>
142a9ae13aSMark Brown #include <linux/moduleparam.h>
152a9ae13aSMark Brown #include <linux/init.h>
162a9ae13aSMark Brown #include <linux/delay.h>
172a9ae13aSMark Brown #include <linux/pm.h>
182a9ae13aSMark Brown #include <linux/i2c.h>
193ef8ac0dSMark Brown #include <linux/regmap.h>
202a9ae13aSMark Brown #include <linux/regulator/consumer.h>
212a9ae13aSMark Brown #include <linux/spi/spi.h>
222a9ae13aSMark Brown #include <linux/slab.h>
2353a5a83dSMark Brown #include <linux/of_device.h>
242a9ae13aSMark Brown #include <sound/core.h>
252a9ae13aSMark Brown #include <sound/pcm.h>
262a9ae13aSMark Brown #include <sound/pcm_params.h>
272a9ae13aSMark Brown #include <sound/soc.h>
282a9ae13aSMark Brown #include <sound/soc-dapm.h>
292a9ae13aSMark Brown #include <sound/initval.h>
302a9ae13aSMark Brown #include <sound/tlv.h>
312a9ae13aSMark Brown 
322a9ae13aSMark Brown #include "wm8737.h"
332a9ae13aSMark Brown 
342a9ae13aSMark Brown #define WM8737_NUM_SUPPLIES 4
352a9ae13aSMark Brown static const char *wm8737_supply_names[WM8737_NUM_SUPPLIES] = {
362a9ae13aSMark Brown 	"DCVDD",
372a9ae13aSMark Brown 	"DBVDD",
382a9ae13aSMark Brown 	"AVDD",
392a9ae13aSMark Brown 	"MVDD",
402a9ae13aSMark Brown };
412a9ae13aSMark Brown 
422a9ae13aSMark Brown /* codec private data */
432a9ae13aSMark Brown struct wm8737_priv {
443ef8ac0dSMark Brown 	struct regmap *regmap;
452a9ae13aSMark Brown 	struct regulator_bulk_data supplies[WM8737_NUM_SUPPLIES];
462a9ae13aSMark Brown 	unsigned int mclk;
472a9ae13aSMark Brown };
482a9ae13aSMark Brown 
493ef8ac0dSMark Brown static const struct reg_default wm8737_reg_defaults[] = {
503ef8ac0dSMark Brown 	{  0, 0x00C3 },     /* R0  - Left PGA volume */
513ef8ac0dSMark Brown 	{  1, 0x00C3 },     /* R1  - Right PGA volume */
523ef8ac0dSMark Brown 	{  2, 0x0007 },     /* R2  - AUDIO path L */
533ef8ac0dSMark Brown 	{  3, 0x0007 },     /* R3  - AUDIO path R */
543ef8ac0dSMark Brown 	{  4, 0x0000 },     /* R4  - 3D Enhance */
553ef8ac0dSMark Brown 	{  5, 0x0000 },     /* R5  - ADC Control */
563ef8ac0dSMark Brown 	{  6, 0x0000 },     /* R6  - Power Management */
573ef8ac0dSMark Brown 	{  7, 0x000A },     /* R7  - Audio Format */
583ef8ac0dSMark Brown 	{  8, 0x0000 },     /* R8  - Clocking */
593ef8ac0dSMark Brown 	{  9, 0x000F },     /* R9  - MIC Preamp Control */
603ef8ac0dSMark Brown 	{ 10, 0x0003 },     /* R10 - Misc Bias Control */
613ef8ac0dSMark Brown 	{ 11, 0x0000 },     /* R11 - Noise Gate */
623ef8ac0dSMark Brown 	{ 12, 0x007C },     /* R12 - ALC1 */
633ef8ac0dSMark Brown 	{ 13, 0x0000 },     /* R13 - ALC2 */
643ef8ac0dSMark Brown 	{ 14, 0x0032 },     /* R14 - ALC3 */
652a9ae13aSMark Brown };
662a9ae13aSMark Brown 
673ef8ac0dSMark Brown static bool wm8737_volatile(struct device *dev, unsigned int reg)
683ef8ac0dSMark Brown {
693ef8ac0dSMark Brown 	switch (reg) {
703ef8ac0dSMark Brown 	case WM8737_RESET:
713ef8ac0dSMark Brown 		return true;
723ef8ac0dSMark Brown 	default:
733ef8ac0dSMark Brown 		return false;
743ef8ac0dSMark Brown 	}
753ef8ac0dSMark Brown }
763ef8ac0dSMark Brown 
772a9ae13aSMark Brown static int wm8737_reset(struct snd_soc_codec *codec)
782a9ae13aSMark Brown {
792a9ae13aSMark Brown 	return snd_soc_write(codec, WM8737_RESET, 0);
802a9ae13aSMark Brown }
812a9ae13aSMark Brown 
822a9ae13aSMark Brown static const unsigned int micboost_tlv[] = {
832a9ae13aSMark Brown 	TLV_DB_RANGE_HEAD(4),
842a9ae13aSMark Brown 	0, 0, TLV_DB_SCALE_ITEM(1300, 0, 0),
852a9ae13aSMark Brown 	1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
862a9ae13aSMark Brown 	2, 2, TLV_DB_SCALE_ITEM(2800, 0, 0),
872a9ae13aSMark Brown 	3, 3, TLV_DB_SCALE_ITEM(3300, 0, 0),
882a9ae13aSMark Brown };
892a9ae13aSMark Brown static const DECLARE_TLV_DB_SCALE(pga_tlv, -9750, 50, 1);
902a9ae13aSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -600, 600, 0);
912a9ae13aSMark Brown static const DECLARE_TLV_DB_SCALE(ng_tlv, -7800, 600, 0);
92fcd02e26SMark Brown static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -1200, 600, 0);
93fcd02e26SMark Brown static const DECLARE_TLV_DB_SCALE(alc_target_tlv, -1800, 100, 0);
942a9ae13aSMark Brown 
952a9ae13aSMark Brown static const char *micbias_enum_text[] = {
962a9ae13aSMark Brown 	"25%",
972a9ae13aSMark Brown 	"50%",
982a9ae13aSMark Brown 	"75%",
992a9ae13aSMark Brown 	"100%",
1002a9ae13aSMark Brown };
1012a9ae13aSMark Brown 
102*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(micbias_enum,
103*ca275811STakashi Iwai 			    WM8737_MIC_PREAMP_CONTROL, 0, micbias_enum_text);
1042a9ae13aSMark Brown 
1052a9ae13aSMark Brown static const char *low_cutoff_text[] = {
1062a9ae13aSMark Brown 	"Low", "High"
1072a9ae13aSMark Brown };
1082a9ae13aSMark Brown 
109*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(low_3d,
110*ca275811STakashi Iwai 			    WM8737_3D_ENHANCE, 6, low_cutoff_text);
1112a9ae13aSMark Brown 
1122a9ae13aSMark Brown static const char *high_cutoff_text[] = {
1132a9ae13aSMark Brown 	"High", "Low"
1142a9ae13aSMark Brown };
1152a9ae13aSMark Brown 
116*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(high_3d,
117*ca275811STakashi Iwai 			    WM8737_3D_ENHANCE, 5, high_cutoff_text);
1182a9ae13aSMark Brown 
119fcd02e26SMark Brown static const char *alc_fn_text[] = {
120fcd02e26SMark Brown 	"Disabled", "Right", "Left", "Stereo"
121fcd02e26SMark Brown };
122fcd02e26SMark Brown 
123*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_fn,
124*ca275811STakashi Iwai 			    WM8737_ALC1, 7, alc_fn_text);
125fcd02e26SMark Brown 
126fcd02e26SMark Brown static const char *alc_hold_text[] = {
127fcd02e26SMark Brown 	"0", "2.67ms", "5.33ms", "10.66ms", "21.32ms", "42.64ms", "85.28ms",
128fcd02e26SMark Brown 	"170.56ms", "341.12ms", "682.24ms", "1.364s", "2.728s", "5.458s",
129fcd02e26SMark Brown 	"10.916s", "21.832s", "43.691s"
130fcd02e26SMark Brown };
131fcd02e26SMark Brown 
132*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_hold,
133*ca275811STakashi Iwai 			    WM8737_ALC2, 0, alc_hold_text);
134fcd02e26SMark Brown 
135fcd02e26SMark Brown static const char *alc_atk_text[] = {
136fcd02e26SMark Brown 	"8.4ms", "16.8ms", "33.6ms", "67.2ms", "134.4ms", "268.8ms", "537.6ms",
137fcd02e26SMark Brown 	"1.075s", "2.15s", "4.3s", "8.6s"
138fcd02e26SMark Brown };
139fcd02e26SMark Brown 
140*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_atk,
141*ca275811STakashi Iwai 			    WM8737_ALC3, 0, alc_atk_text);
142fcd02e26SMark Brown 
143fcd02e26SMark Brown static const char *alc_dcy_text[] = {
144fcd02e26SMark Brown 	"33.6ms", "67.2ms", "134.4ms", "268.8ms", "537.6ms", "1.075s", "2.15s",
145fcd02e26SMark Brown 	"4.3s", "8.6s", "17.2s", "34.41s"
146fcd02e26SMark Brown };
147fcd02e26SMark Brown 
148*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_dcy,
149*ca275811STakashi Iwai 			    WM8737_ALC3, 4, alc_dcy_text);
150fcd02e26SMark Brown 
1512a9ae13aSMark Brown static const struct snd_kcontrol_new wm8737_snd_controls[] = {
1522a9ae13aSMark Brown SOC_DOUBLE_R_TLV("Mic Boost Volume", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
1532a9ae13aSMark Brown 		 6, 3, 0, micboost_tlv),
1542a9ae13aSMark Brown SOC_DOUBLE_R("Mic Boost Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
1552a9ae13aSMark Brown 	     4, 1, 0),
1562a9ae13aSMark Brown SOC_DOUBLE("Mic ZC Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
1572a9ae13aSMark Brown 	   3, 1, 0),
1582a9ae13aSMark Brown 
1592a9ae13aSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8737_LEFT_PGA_VOLUME,
1602a9ae13aSMark Brown 		 WM8737_RIGHT_PGA_VOLUME, 0, 255, 0, pga_tlv),
1612a9ae13aSMark Brown SOC_DOUBLE("Capture ZC Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
1622a9ae13aSMark Brown 	   2, 1, 0),
1632a9ae13aSMark Brown 
1642a9ae13aSMark Brown SOC_DOUBLE("INPUT1 DC Bias Switch", WM8737_MISC_BIAS_CONTROL, 0, 1, 1, 0),
1652a9ae13aSMark Brown 
1662a9ae13aSMark Brown SOC_ENUM("Mic PGA Bias", micbias_enum),
1672a9ae13aSMark Brown SOC_SINGLE("ADC Low Power Switch", WM8737_ADC_CONTROL, 2, 1, 0),
1682a9ae13aSMark Brown SOC_SINGLE("High Pass Filter Switch", WM8737_ADC_CONTROL, 0, 1, 1),
1692a9ae13aSMark Brown SOC_DOUBLE("Polarity Invert Switch", WM8737_ADC_CONTROL, 5, 6, 1, 0),
1702a9ae13aSMark Brown 
1712a9ae13aSMark Brown SOC_SINGLE("3D Switch", WM8737_3D_ENHANCE, 0, 1, 0),
1722a9ae13aSMark Brown SOC_SINGLE("3D Depth", WM8737_3D_ENHANCE, 1, 15, 0),
1732a9ae13aSMark Brown SOC_ENUM("3D Low Cut-off", low_3d),
1742a9ae13aSMark Brown SOC_ENUM("3D High Cut-off", low_3d),
1752a9ae13aSMark Brown SOC_SINGLE_TLV("3D ADC Volume", WM8737_3D_ENHANCE, 7, 1, 1, adc_tlv),
1762a9ae13aSMark Brown 
1772a9ae13aSMark Brown SOC_SINGLE("Noise Gate Switch", WM8737_NOISE_GATE, 0, 1, 0),
1782a9ae13aSMark Brown SOC_SINGLE_TLV("Noise Gate Threshold Volume", WM8737_NOISE_GATE, 2, 7, 0,
1792a9ae13aSMark Brown 	       ng_tlv),
180fcd02e26SMark Brown 
181fcd02e26SMark Brown SOC_ENUM("ALC", alc_fn),
182fcd02e26SMark Brown SOC_SINGLE_TLV("ALC Max Gain Volume", WM8737_ALC1, 4, 7, 0, alc_max_tlv),
183fcd02e26SMark Brown SOC_SINGLE_TLV("ALC Target Volume", WM8737_ALC1, 0, 15, 0, alc_target_tlv),
184fcd02e26SMark Brown SOC_ENUM("ALC Hold Time", alc_hold),
185fcd02e26SMark Brown SOC_SINGLE("ALC ZC Switch", WM8737_ALC2, 4, 1, 0),
186fcd02e26SMark Brown SOC_ENUM("ALC Attack Time", alc_atk),
187fcd02e26SMark Brown SOC_ENUM("ALC Decay Time", alc_dcy),
1882a9ae13aSMark Brown };
1892a9ae13aSMark Brown 
1902a9ae13aSMark Brown static const char *linsel_text[] = {
1912a9ae13aSMark Brown 	"LINPUT1", "LINPUT2", "LINPUT3", "LINPUT1 DC",
1922a9ae13aSMark Brown };
1932a9ae13aSMark Brown 
194*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(linsel_enum,
195*ca275811STakashi Iwai 			    WM8737_AUDIO_PATH_L, 7, linsel_text);
1962a9ae13aSMark Brown 
1972a9ae13aSMark Brown static const struct snd_kcontrol_new linsel_mux =
1982a9ae13aSMark Brown 	SOC_DAPM_ENUM("LINSEL", linsel_enum);
1992a9ae13aSMark Brown 
2002a9ae13aSMark Brown 
2012a9ae13aSMark Brown static const char *rinsel_text[] = {
2022a9ae13aSMark Brown 	"RINPUT1", "RINPUT2", "RINPUT3", "RINPUT1 DC",
2032a9ae13aSMark Brown };
2042a9ae13aSMark Brown 
205*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(rinsel_enum,
206*ca275811STakashi Iwai 			    WM8737_AUDIO_PATH_R, 7, rinsel_text);
2072a9ae13aSMark Brown 
2082a9ae13aSMark Brown static const struct snd_kcontrol_new rinsel_mux =
2092a9ae13aSMark Brown 	SOC_DAPM_ENUM("RINSEL", rinsel_enum);
2102a9ae13aSMark Brown 
2112a9ae13aSMark Brown static const char *bypass_text[] = {
2122a9ae13aSMark Brown 	"Direct", "Preamp"
2132a9ae13aSMark Brown };
2142a9ae13aSMark Brown 
215*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(lbypass_enum,
216*ca275811STakashi Iwai 			    WM8737_MIC_PREAMP_CONTROL, 2, bypass_text);
2172a9ae13aSMark Brown 
2182a9ae13aSMark Brown static const struct snd_kcontrol_new lbypass_mux =
2192a9ae13aSMark Brown 	SOC_DAPM_ENUM("Left Bypass", lbypass_enum);
2202a9ae13aSMark Brown 
2212a9ae13aSMark Brown 
222*ca275811STakashi Iwai static SOC_ENUM_SINGLE_DECL(rbypass_enum,
223*ca275811STakashi Iwai 			    WM8737_MIC_PREAMP_CONTROL, 3, bypass_text);
2242a9ae13aSMark Brown 
2252a9ae13aSMark Brown static const struct snd_kcontrol_new rbypass_mux =
2262a9ae13aSMark Brown 	SOC_DAPM_ENUM("Left Bypass", rbypass_enum);
2272a9ae13aSMark Brown 
2282a9ae13aSMark Brown static const struct snd_soc_dapm_widget wm8737_dapm_widgets[] = {
2292a9ae13aSMark Brown SND_SOC_DAPM_INPUT("LINPUT1"),
2302a9ae13aSMark Brown SND_SOC_DAPM_INPUT("LINPUT2"),
2312a9ae13aSMark Brown SND_SOC_DAPM_INPUT("LINPUT3"),
2322a9ae13aSMark Brown SND_SOC_DAPM_INPUT("RINPUT1"),
2332a9ae13aSMark Brown SND_SOC_DAPM_INPUT("RINPUT2"),
2342a9ae13aSMark Brown SND_SOC_DAPM_INPUT("RINPUT3"),
2352a9ae13aSMark Brown SND_SOC_DAPM_INPUT("LACIN"),
2362a9ae13aSMark Brown SND_SOC_DAPM_INPUT("RACIN"),
2372a9ae13aSMark Brown 
2382a9ae13aSMark Brown SND_SOC_DAPM_MUX("LINSEL", SND_SOC_NOPM, 0, 0, &linsel_mux),
2392a9ae13aSMark Brown SND_SOC_DAPM_MUX("RINSEL", SND_SOC_NOPM, 0, 0, &rinsel_mux),
2402a9ae13aSMark Brown 
2412a9ae13aSMark Brown SND_SOC_DAPM_MUX("Left Preamp Mux", SND_SOC_NOPM, 0, 0, &lbypass_mux),
2422a9ae13aSMark Brown SND_SOC_DAPM_MUX("Right Preamp Mux", SND_SOC_NOPM, 0, 0, &rbypass_mux),
2432a9ae13aSMark Brown 
2442a9ae13aSMark Brown SND_SOC_DAPM_PGA("PGAL", WM8737_POWER_MANAGEMENT, 5, 0, NULL, 0),
2452a9ae13aSMark Brown SND_SOC_DAPM_PGA("PGAR", WM8737_POWER_MANAGEMENT, 4, 0, NULL, 0),
2462a9ae13aSMark Brown 
2472a9ae13aSMark Brown SND_SOC_DAPM_DAC("ADCL", NULL, WM8737_POWER_MANAGEMENT, 3, 0),
2482a9ae13aSMark Brown SND_SOC_DAPM_DAC("ADCR", NULL, WM8737_POWER_MANAGEMENT, 2, 0),
2492a9ae13aSMark Brown 
2502a9ae13aSMark Brown SND_SOC_DAPM_AIF_OUT("AIF", "Capture", 0, WM8737_POWER_MANAGEMENT, 6, 0),
2512a9ae13aSMark Brown };
2522a9ae13aSMark Brown 
2532a9ae13aSMark Brown static const struct snd_soc_dapm_route intercon[] = {
2542a9ae13aSMark Brown 	{ "LINSEL", "LINPUT1", "LINPUT1" },
2552a9ae13aSMark Brown 	{ "LINSEL", "LINPUT2", "LINPUT2" },
2562a9ae13aSMark Brown 	{ "LINSEL", "LINPUT3", "LINPUT3" },
2572a9ae13aSMark Brown 	{ "LINSEL", "LINPUT1 DC", "LINPUT1" },
2582a9ae13aSMark Brown 
2592a9ae13aSMark Brown 	{ "RINSEL", "RINPUT1", "RINPUT1" },
2602a9ae13aSMark Brown 	{ "RINSEL", "RINPUT2", "RINPUT2" },
2612a9ae13aSMark Brown 	{ "RINSEL", "RINPUT3", "RINPUT3" },
2622a9ae13aSMark Brown 	{ "RINSEL", "RINPUT1 DC", "RINPUT1" },
2632a9ae13aSMark Brown 
2642a9ae13aSMark Brown 	{ "Left Preamp Mux", "Preamp", "LINSEL" },
2652a9ae13aSMark Brown 	{ "Left Preamp Mux", "Direct", "LACIN" },
2662a9ae13aSMark Brown 
2672a9ae13aSMark Brown 	{ "Right Preamp Mux", "Preamp", "RINSEL" },
2682a9ae13aSMark Brown 	{ "Right Preamp Mux", "Direct", "RACIN" },
2692a9ae13aSMark Brown 
2702a9ae13aSMark Brown 	{ "PGAL", NULL, "Left Preamp Mux" },
2712a9ae13aSMark Brown 	{ "PGAR", NULL, "Right Preamp Mux" },
2722a9ae13aSMark Brown 
2732a9ae13aSMark Brown 	{ "ADCL", NULL, "PGAL" },
2742a9ae13aSMark Brown 	{ "ADCR", NULL, "PGAR" },
2752a9ae13aSMark Brown 
2762a9ae13aSMark Brown 	{ "AIF", NULL, "ADCL" },
2772a9ae13aSMark Brown 	{ "AIF", NULL, "ADCR" },
2782a9ae13aSMark Brown };
2792a9ae13aSMark Brown 
2802a9ae13aSMark Brown static int wm8737_add_widgets(struct snd_soc_codec *codec)
2812a9ae13aSMark Brown {
2822a9ae13aSMark Brown 	struct snd_soc_dapm_context *dapm = &codec->dapm;
2832a9ae13aSMark Brown 
2842a9ae13aSMark Brown 	snd_soc_dapm_new_controls(dapm, wm8737_dapm_widgets,
2852a9ae13aSMark Brown 				  ARRAY_SIZE(wm8737_dapm_widgets));
2862a9ae13aSMark Brown 	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
2872a9ae13aSMark Brown 
2882a9ae13aSMark Brown 	return 0;
2892a9ae13aSMark Brown }
2902a9ae13aSMark Brown 
2912a9ae13aSMark Brown /* codec mclk clock divider coefficients */
2922a9ae13aSMark Brown static const struct {
2932a9ae13aSMark Brown 	u32 mclk;
2942a9ae13aSMark Brown 	u32 rate;
2952a9ae13aSMark Brown 	u8 usb;
2962a9ae13aSMark Brown 	u8 sr;
2972a9ae13aSMark Brown } coeff_div[] = {
2982a9ae13aSMark Brown 	{ 12288000,  8000, 0,  0x4 },
2992a9ae13aSMark Brown 	{ 12288000, 12000, 0,  0x8 },
3002a9ae13aSMark Brown 	{ 12288000, 16000, 0,  0xa },
3012a9ae13aSMark Brown 	{ 12288000, 24000, 0, 0x1c },
3022a9ae13aSMark Brown 	{ 12288000, 32000, 0,  0xc },
3032a9ae13aSMark Brown 	{ 12288000, 48000, 0,    0 },
3042a9ae13aSMark Brown 	{ 12288000, 96000, 0,  0xe },
3052a9ae13aSMark Brown 
3062a9ae13aSMark Brown 	{ 11289600,  8000, 0, 0x14 },
3072a9ae13aSMark Brown 	{ 11289600, 11025, 0, 0x18 },
3082a9ae13aSMark Brown 	{ 11289600, 22050, 0, 0x1a },
3092a9ae13aSMark Brown 	{ 11289600, 44100, 0, 0x10 },
3102a9ae13aSMark Brown 	{ 11289600, 88200, 0, 0x1e },
3112a9ae13aSMark Brown 
3122a9ae13aSMark Brown 	{ 18432000,  8000, 0,  0x5 },
3132a9ae13aSMark Brown 	{ 18432000, 12000, 0,  0x9 },
3142a9ae13aSMark Brown 	{ 18432000, 16000, 0,  0xb },
3152a9ae13aSMark Brown 	{ 18432000, 24000, 0, 0x1b },
3162a9ae13aSMark Brown 	{ 18432000, 32000, 0,  0xd },
3172a9ae13aSMark Brown 	{ 18432000, 48000, 0,  0x1 },
3182a9ae13aSMark Brown 	{ 18432000, 96000, 0, 0x1f },
3192a9ae13aSMark Brown 
3202a9ae13aSMark Brown 	{ 16934400,  8000, 0, 0x15 },
3212a9ae13aSMark Brown 	{ 16934400, 11025, 0, 0x19 },
3222a9ae13aSMark Brown 	{ 16934400, 22050, 0, 0x1b },
3232a9ae13aSMark Brown 	{ 16934400, 44100, 0, 0x11 },
3242a9ae13aSMark Brown 	{ 16934400, 88200, 0, 0x1f },
3252a9ae13aSMark Brown 
3262a9ae13aSMark Brown 	{ 12000000,  8000, 1,  0x4 },
3272a9ae13aSMark Brown 	{ 12000000, 11025, 1, 0x19 },
3282a9ae13aSMark Brown 	{ 12000000, 12000, 1,  0x8 },
3292a9ae13aSMark Brown 	{ 12000000, 16000, 1,  0xa },
3302a9ae13aSMark Brown 	{ 12000000, 22050, 1, 0x1b },
3312a9ae13aSMark Brown 	{ 12000000, 24000, 1, 0x1c },
3322a9ae13aSMark Brown 	{ 12000000, 32000, 1,  0xc },
3332a9ae13aSMark Brown 	{ 12000000, 44100, 1, 0x11 },
3342a9ae13aSMark Brown 	{ 12000000, 48000, 1,  0x0 },
3352a9ae13aSMark Brown 	{ 12000000, 88200, 1, 0x1f },
3362a9ae13aSMark Brown 	{ 12000000, 96000, 1,  0xe },
3372a9ae13aSMark Brown };
3382a9ae13aSMark Brown 
3392a9ae13aSMark Brown static int wm8737_hw_params(struct snd_pcm_substream *substream,
3402a9ae13aSMark Brown 			    struct snd_pcm_hw_params *params,
3412a9ae13aSMark Brown 			    struct snd_soc_dai *dai)
3422a9ae13aSMark Brown {
343e6968a17SMark Brown 	struct snd_soc_codec *codec = dai->codec;
3442a9ae13aSMark Brown 	struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
3452a9ae13aSMark Brown 	int i;
3462a9ae13aSMark Brown 	u16 clocking = 0;
3472a9ae13aSMark Brown 	u16 af = 0;
3482a9ae13aSMark Brown 
3492a9ae13aSMark Brown 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
3502a9ae13aSMark Brown 		if (coeff_div[i].rate != params_rate(params))
3512a9ae13aSMark Brown 			continue;
3522a9ae13aSMark Brown 
3532a9ae13aSMark Brown 		if (coeff_div[i].mclk == wm8737->mclk)
3542a9ae13aSMark Brown 			break;
3552a9ae13aSMark Brown 
3562a9ae13aSMark Brown 		if (coeff_div[i].mclk == wm8737->mclk * 2) {
3572a9ae13aSMark Brown 			clocking |= WM8737_CLKDIV2;
3582a9ae13aSMark Brown 			break;
3592a9ae13aSMark Brown 		}
3602a9ae13aSMark Brown 	}
3612a9ae13aSMark Brown 
3622a9ae13aSMark Brown 	if (i == ARRAY_SIZE(coeff_div)) {
3632a9ae13aSMark Brown 		dev_err(codec->dev, "%dHz MCLK can't support %dHz\n",
3642a9ae13aSMark Brown 			wm8737->mclk, params_rate(params));
3652a9ae13aSMark Brown 		return -EINVAL;
3662a9ae13aSMark Brown 	}
3672a9ae13aSMark Brown 
3682a9ae13aSMark Brown 	clocking |= coeff_div[i].usb | (coeff_div[i].sr << WM8737_SR_SHIFT);
3692a9ae13aSMark Brown 
3702a9ae13aSMark Brown 	switch (params_format(params)) {
3712a9ae13aSMark Brown 	case SNDRV_PCM_FORMAT_S16_LE:
3722a9ae13aSMark Brown 		break;
3732a9ae13aSMark Brown 	case SNDRV_PCM_FORMAT_S20_3LE:
3742a9ae13aSMark Brown 		af |= 0x8;
3752a9ae13aSMark Brown 		break;
3762a9ae13aSMark Brown 	case SNDRV_PCM_FORMAT_S24_LE:
3772a9ae13aSMark Brown 		af |= 0x10;
3782a9ae13aSMark Brown 		break;
3792a9ae13aSMark Brown 	case SNDRV_PCM_FORMAT_S32_LE:
3802a9ae13aSMark Brown 		af |= 0x18;
3812a9ae13aSMark Brown 		break;
3822a9ae13aSMark Brown 	default:
3832a9ae13aSMark Brown 		return -EINVAL;
3842a9ae13aSMark Brown 	}
3852a9ae13aSMark Brown 
3862a9ae13aSMark Brown 	snd_soc_update_bits(codec, WM8737_AUDIO_FORMAT, WM8737_WL_MASK, af);
3872a9ae13aSMark Brown 	snd_soc_update_bits(codec, WM8737_CLOCKING,
3882a9ae13aSMark Brown 			    WM8737_USB_MODE | WM8737_CLKDIV2 | WM8737_SR_MASK,
3892a9ae13aSMark Brown 			    clocking);
3902a9ae13aSMark Brown 
3912a9ae13aSMark Brown 	return 0;
3922a9ae13aSMark Brown }
3932a9ae13aSMark Brown 
3942a9ae13aSMark Brown static int wm8737_set_dai_sysclk(struct snd_soc_dai *codec_dai,
3952a9ae13aSMark Brown 				 int clk_id, unsigned int freq, int dir)
3962a9ae13aSMark Brown {
3972a9ae13aSMark Brown 	struct snd_soc_codec *codec = codec_dai->codec;
3982a9ae13aSMark Brown 	struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
3992a9ae13aSMark Brown 	int i;
4002a9ae13aSMark Brown 
4012a9ae13aSMark Brown 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
4022a9ae13aSMark Brown 		if (freq == coeff_div[i].mclk ||
4032a9ae13aSMark Brown 		    freq == coeff_div[i].mclk * 2) {
4042a9ae13aSMark Brown 			wm8737->mclk = freq;
4052a9ae13aSMark Brown 			return 0;
4062a9ae13aSMark Brown 		}
4072a9ae13aSMark Brown 	}
4082a9ae13aSMark Brown 
4092a9ae13aSMark Brown 	dev_err(codec->dev, "MCLK rate %dHz not supported\n", freq);
4102a9ae13aSMark Brown 
4112a9ae13aSMark Brown 	return -EINVAL;
4122a9ae13aSMark Brown }
4132a9ae13aSMark Brown 
4142a9ae13aSMark Brown 
4152a9ae13aSMark Brown static int wm8737_set_dai_fmt(struct snd_soc_dai *codec_dai,
4162a9ae13aSMark Brown 		unsigned int fmt)
4172a9ae13aSMark Brown {
4182a9ae13aSMark Brown 	struct snd_soc_codec *codec = codec_dai->codec;
4192a9ae13aSMark Brown 	u16 af = 0;
4202a9ae13aSMark Brown 
4212a9ae13aSMark Brown 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
4222a9ae13aSMark Brown 	case SND_SOC_DAIFMT_CBM_CFM:
4232a9ae13aSMark Brown 		af |= WM8737_MS;
4242a9ae13aSMark Brown 		break;
4252a9ae13aSMark Brown 	case SND_SOC_DAIFMT_CBS_CFS:
4262a9ae13aSMark Brown 		break;
4272a9ae13aSMark Brown 	default:
4282a9ae13aSMark Brown 		return -EINVAL;
4292a9ae13aSMark Brown 	}
4302a9ae13aSMark Brown 
4312a9ae13aSMark Brown 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
4322a9ae13aSMark Brown 	case SND_SOC_DAIFMT_I2S:
4332a9ae13aSMark Brown 		af |= 0x2;
4342a9ae13aSMark Brown 		break;
4352a9ae13aSMark Brown 	case SND_SOC_DAIFMT_RIGHT_J:
4362a9ae13aSMark Brown 		break;
4372a9ae13aSMark Brown 	case SND_SOC_DAIFMT_LEFT_J:
4382a9ae13aSMark Brown 		af |= 0x1;
4392a9ae13aSMark Brown 		break;
4402a9ae13aSMark Brown 	case SND_SOC_DAIFMT_DSP_A:
4412a9ae13aSMark Brown 		af |= 0x3;
4422a9ae13aSMark Brown 		break;
4432a9ae13aSMark Brown 	case SND_SOC_DAIFMT_DSP_B:
4442a9ae13aSMark Brown 		af |= 0x13;
4452a9ae13aSMark Brown 		break;
4462a9ae13aSMark Brown 	default:
4472a9ae13aSMark Brown 		return -EINVAL;
4482a9ae13aSMark Brown 	}
4492a9ae13aSMark Brown 
4502a9ae13aSMark Brown 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
4512a9ae13aSMark Brown 	case SND_SOC_DAIFMT_NB_NF:
4522a9ae13aSMark Brown 		break;
4532a9ae13aSMark Brown 	case SND_SOC_DAIFMT_NB_IF:
4542a9ae13aSMark Brown 		af |= WM8737_LRP;
4552a9ae13aSMark Brown 		break;
4562a9ae13aSMark Brown 	default:
4572a9ae13aSMark Brown 		return -EINVAL;
4582a9ae13aSMark Brown 	}
4592a9ae13aSMark Brown 
4602a9ae13aSMark Brown 	snd_soc_update_bits(codec, WM8737_AUDIO_FORMAT,
4612a9ae13aSMark Brown 			    WM8737_FORMAT_MASK | WM8737_LRP | WM8737_MS, af);
4622a9ae13aSMark Brown 
4632a9ae13aSMark Brown 	return 0;
4642a9ae13aSMark Brown }
4652a9ae13aSMark Brown 
4662a9ae13aSMark Brown static int wm8737_set_bias_level(struct snd_soc_codec *codec,
4672a9ae13aSMark Brown 				 enum snd_soc_bias_level level)
4682a9ae13aSMark Brown {
4692a9ae13aSMark Brown 	struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
4702a9ae13aSMark Brown 	int ret;
4712a9ae13aSMark Brown 
4722a9ae13aSMark Brown 	switch (level) {
4732a9ae13aSMark Brown 	case SND_SOC_BIAS_ON:
4742a9ae13aSMark Brown 		break;
4752a9ae13aSMark Brown 
4762a9ae13aSMark Brown 	case SND_SOC_BIAS_PREPARE:
4772a9ae13aSMark Brown 		/* VMID at 2*75k */
4782a9ae13aSMark Brown 		snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
4792a9ae13aSMark Brown 				    WM8737_VMIDSEL_MASK, 0);
4802a9ae13aSMark Brown 		break;
4812a9ae13aSMark Brown 
4822a9ae13aSMark Brown 	case SND_SOC_BIAS_STANDBY:
4832a9ae13aSMark Brown 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
4842a9ae13aSMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8737->supplies),
4852a9ae13aSMark Brown 						    wm8737->supplies);
4862a9ae13aSMark Brown 			if (ret != 0) {
4872a9ae13aSMark Brown 				dev_err(codec->dev,
4882a9ae13aSMark Brown 					"Failed to enable supplies: %d\n",
4892a9ae13aSMark Brown 					ret);
4902a9ae13aSMark Brown 				return ret;
4912a9ae13aSMark Brown 			}
4922a9ae13aSMark Brown 
4933ef8ac0dSMark Brown 			regcache_sync(wm8737->regmap);
4942a9ae13aSMark Brown 
4952a9ae13aSMark Brown 			/* Fast VMID ramp at 2*2.5k */
4962a9ae13aSMark Brown 			snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
4972a9ae13aSMark Brown 					    WM8737_VMIDSEL_MASK, 0x4);
4982a9ae13aSMark Brown 
4992a9ae13aSMark Brown 			/* Bring VMID up */
5002a9ae13aSMark Brown 			snd_soc_update_bits(codec, WM8737_POWER_MANAGEMENT,
5012a9ae13aSMark Brown 					    WM8737_VMID_MASK |
5022a9ae13aSMark Brown 					    WM8737_VREF_MASK,
5032a9ae13aSMark Brown 					    WM8737_VMID_MASK |
5042a9ae13aSMark Brown 					    WM8737_VREF_MASK);
5052a9ae13aSMark Brown 
5062a9ae13aSMark Brown 			msleep(500);
5072a9ae13aSMark Brown 		}
5082a9ae13aSMark Brown 
5092a9ae13aSMark Brown 		/* VMID at 2*300k */
5102a9ae13aSMark Brown 		snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
5112a9ae13aSMark Brown 				    WM8737_VMIDSEL_MASK, 2);
5122a9ae13aSMark Brown 
5132a9ae13aSMark Brown 		break;
5142a9ae13aSMark Brown 
5152a9ae13aSMark Brown 	case SND_SOC_BIAS_OFF:
5162a9ae13aSMark Brown 		snd_soc_update_bits(codec, WM8737_POWER_MANAGEMENT,
5172a9ae13aSMark Brown 				    WM8737_VMID_MASK | WM8737_VREF_MASK, 0);
5182a9ae13aSMark Brown 
5192a9ae13aSMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies),
5202a9ae13aSMark Brown 				       wm8737->supplies);
5212a9ae13aSMark Brown 		break;
5222a9ae13aSMark Brown 	}
5232a9ae13aSMark Brown 
5242a9ae13aSMark Brown 	codec->dapm.bias_level = level;
5252a9ae13aSMark Brown 	return 0;
5262a9ae13aSMark Brown }
5272a9ae13aSMark Brown 
5282a9ae13aSMark Brown #define WM8737_RATES SNDRV_PCM_RATE_8000_96000
5292a9ae13aSMark Brown 
5302a9ae13aSMark Brown #define WM8737_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
5312a9ae13aSMark Brown 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
5322a9ae13aSMark Brown 
53385e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8737_dai_ops = {
5342a9ae13aSMark Brown 	.hw_params	= wm8737_hw_params,
5352a9ae13aSMark Brown 	.set_sysclk	= wm8737_set_dai_sysclk,
5362a9ae13aSMark Brown 	.set_fmt	= wm8737_set_dai_fmt,
5372a9ae13aSMark Brown };
5382a9ae13aSMark Brown 
5392a9ae13aSMark Brown static struct snd_soc_dai_driver wm8737_dai = {
5402a9ae13aSMark Brown 	.name = "wm8737",
5412a9ae13aSMark Brown 	.capture = {
5422a9ae13aSMark Brown 		.stream_name = "Capture",
5432a9ae13aSMark Brown 		.channels_min = 2,  /* Mono modes not yet supported */
5442a9ae13aSMark Brown 		.channels_max = 2,
5452a9ae13aSMark Brown 		.rates = WM8737_RATES,
5462a9ae13aSMark Brown 		.formats = WM8737_FORMATS,
5472a9ae13aSMark Brown 	},
5482a9ae13aSMark Brown 	.ops = &wm8737_dai_ops,
5492a9ae13aSMark Brown };
5502a9ae13aSMark Brown 
5512a9ae13aSMark Brown #ifdef CONFIG_PM
55284b315eeSLars-Peter Clausen static int wm8737_suspend(struct snd_soc_codec *codec)
5532a9ae13aSMark Brown {
5542a9ae13aSMark Brown 	wm8737_set_bias_level(codec, SND_SOC_BIAS_OFF);
5552a9ae13aSMark Brown 	return 0;
5562a9ae13aSMark Brown }
5572a9ae13aSMark Brown 
5582a9ae13aSMark Brown static int wm8737_resume(struct snd_soc_codec *codec)
5592a9ae13aSMark Brown {
5602a9ae13aSMark Brown 	wm8737_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
5612a9ae13aSMark Brown 	return 0;
5622a9ae13aSMark Brown }
5632a9ae13aSMark Brown #else
5642a9ae13aSMark Brown #define wm8737_suspend NULL
5652a9ae13aSMark Brown #define wm8737_resume NULL
5662a9ae13aSMark Brown #endif
5672a9ae13aSMark Brown 
5682a9ae13aSMark Brown static int wm8737_probe(struct snd_soc_codec *codec)
5692a9ae13aSMark Brown {
5702a9ae13aSMark Brown 	struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
5714f69bb31SMark Brown 	int ret;
5722a9ae13aSMark Brown 
5733ef8ac0dSMark Brown 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
5742a9ae13aSMark Brown 	if (ret != 0) {
5752a9ae13aSMark Brown 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
5762a9ae13aSMark Brown 		return ret;
5772a9ae13aSMark Brown 	}
5782a9ae13aSMark Brown 
5792a9ae13aSMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8737->supplies),
5802a9ae13aSMark Brown 				    wm8737->supplies);
5812a9ae13aSMark Brown 	if (ret != 0) {
5822a9ae13aSMark Brown 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
5832a9ae13aSMark Brown 		goto err_get;
5842a9ae13aSMark Brown 	}
5852a9ae13aSMark Brown 
5862a9ae13aSMark Brown 	ret = wm8737_reset(codec);
5872a9ae13aSMark Brown 	if (ret < 0) {
5882a9ae13aSMark Brown 		dev_err(codec->dev, "Failed to issue reset\n");
5892a9ae13aSMark Brown 		goto err_enable;
5902a9ae13aSMark Brown 	}
5912a9ae13aSMark Brown 
5922a9ae13aSMark Brown 	snd_soc_update_bits(codec, WM8737_LEFT_PGA_VOLUME, WM8737_LVU,
5932a9ae13aSMark Brown 			    WM8737_LVU);
5942a9ae13aSMark Brown 	snd_soc_update_bits(codec, WM8737_RIGHT_PGA_VOLUME, WM8737_RVU,
5952a9ae13aSMark Brown 			    WM8737_RVU);
5962a9ae13aSMark Brown 
5972a9ae13aSMark Brown 	wm8737_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
5982a9ae13aSMark Brown 
5992a9ae13aSMark Brown 	/* Bias level configuration will have done an extra enable */
6002a9ae13aSMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies), wm8737->supplies);
6012a9ae13aSMark Brown 
602022658beSLiam Girdwood 	snd_soc_add_codec_controls(codec, wm8737_snd_controls,
6032a9ae13aSMark Brown 			     ARRAY_SIZE(wm8737_snd_controls));
6042a9ae13aSMark Brown 	wm8737_add_widgets(codec);
6052a9ae13aSMark Brown 
6062a9ae13aSMark Brown 	return 0;
6072a9ae13aSMark Brown 
6082a9ae13aSMark Brown err_enable:
6092a9ae13aSMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies), wm8737->supplies);
6102a9ae13aSMark Brown err_get:
6112a9ae13aSMark Brown 	return ret;
6122a9ae13aSMark Brown }
6132a9ae13aSMark Brown 
6142a9ae13aSMark Brown static int wm8737_remove(struct snd_soc_codec *codec)
6152a9ae13aSMark Brown {
6162a9ae13aSMark Brown 	wm8737_set_bias_level(codec, SND_SOC_BIAS_OFF);
6172a9ae13aSMark Brown 	return 0;
6182a9ae13aSMark Brown }
6192a9ae13aSMark Brown 
6202a9ae13aSMark Brown static struct snd_soc_codec_driver soc_codec_dev_wm8737 = {
6212a9ae13aSMark Brown 	.probe		= wm8737_probe,
6222a9ae13aSMark Brown 	.remove		= wm8737_remove,
6232a9ae13aSMark Brown 	.suspend	= wm8737_suspend,
6242a9ae13aSMark Brown 	.resume		= wm8737_resume,
6252a9ae13aSMark Brown 	.set_bias_level = wm8737_set_bias_level,
6262a9ae13aSMark Brown };
6272a9ae13aSMark Brown 
62853a5a83dSMark Brown static const struct of_device_id wm8737_of_match[] = {
62953a5a83dSMark Brown 	{ .compatible = "wlf,wm8737", },
63053a5a83dSMark Brown 	{ }
63153a5a83dSMark Brown };
63253a5a83dSMark Brown 
63353a5a83dSMark Brown MODULE_DEVICE_TABLE(of, wm8737_of_match);
63453a5a83dSMark Brown 
6353ef8ac0dSMark Brown static const struct regmap_config wm8737_regmap = {
6363ef8ac0dSMark Brown 	.reg_bits = 7,
6373ef8ac0dSMark Brown 	.val_bits = 9,
6383ef8ac0dSMark Brown 	.max_register = WM8737_MAX_REGISTER,
6393ef8ac0dSMark Brown 
6403ef8ac0dSMark Brown 	.reg_defaults = wm8737_reg_defaults,
6413ef8ac0dSMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8737_reg_defaults),
6423ef8ac0dSMark Brown 	.cache_type = REGCACHE_RBTREE,
6433ef8ac0dSMark Brown 
6443ef8ac0dSMark Brown 	.volatile_reg = wm8737_volatile,
6453ef8ac0dSMark Brown };
6463ef8ac0dSMark Brown 
6471037d567SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
6487a79e94eSBill Pemberton static int wm8737_i2c_probe(struct i2c_client *i2c,
6492a9ae13aSMark Brown 			    const struct i2c_device_id *id)
6502a9ae13aSMark Brown {
6512a9ae13aSMark Brown 	struct wm8737_priv *wm8737;
6524f69bb31SMark Brown 	int ret, i;
6532a9ae13aSMark Brown 
65465fdd9bfSMark Brown 	wm8737 = devm_kzalloc(&i2c->dev, sizeof(struct wm8737_priv),
65565fdd9bfSMark Brown 			      GFP_KERNEL);
6562a9ae13aSMark Brown 	if (wm8737 == NULL)
6572a9ae13aSMark Brown 		return -ENOMEM;
6582a9ae13aSMark Brown 
6594f69bb31SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8737->supplies); i++)
6604f69bb31SMark Brown 		wm8737->supplies[i].supply = wm8737_supply_names[i];
6614f69bb31SMark Brown 
6624f69bb31SMark Brown 	ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8737->supplies),
6634f69bb31SMark Brown 				      wm8737->supplies);
6644f69bb31SMark Brown 	if (ret != 0) {
6654f69bb31SMark Brown 		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
6664f69bb31SMark Brown 		return ret;
6674f69bb31SMark Brown 	}
6684f69bb31SMark Brown 
6693ef8ac0dSMark Brown 	wm8737->regmap = devm_regmap_init_i2c(i2c, &wm8737_regmap);
6703ef8ac0dSMark Brown 	if (IS_ERR(wm8737->regmap))
6713ef8ac0dSMark Brown 		return PTR_ERR(wm8737->regmap);
6723ef8ac0dSMark Brown 
6732a9ae13aSMark Brown 	i2c_set_clientdata(i2c, wm8737);
6742a9ae13aSMark Brown 
6752a9ae13aSMark Brown 	ret =  snd_soc_register_codec(&i2c->dev,
6762a9ae13aSMark Brown 				      &soc_codec_dev_wm8737, &wm8737_dai, 1);
67765fdd9bfSMark Brown 
6782a9ae13aSMark Brown 	return ret;
6792a9ae13aSMark Brown 
6802a9ae13aSMark Brown }
6812a9ae13aSMark Brown 
6827a79e94eSBill Pemberton static int wm8737_i2c_remove(struct i2c_client *client)
6832a9ae13aSMark Brown {
6842a9ae13aSMark Brown 	snd_soc_unregister_codec(&client->dev);
68565fdd9bfSMark Brown 
6862a9ae13aSMark Brown 	return 0;
6872a9ae13aSMark Brown }
6882a9ae13aSMark Brown 
6892a9ae13aSMark Brown static const struct i2c_device_id wm8737_i2c_id[] = {
6902a9ae13aSMark Brown 	{ "wm8737", 0 },
6912a9ae13aSMark Brown 	{ }
6922a9ae13aSMark Brown };
6932a9ae13aSMark Brown MODULE_DEVICE_TABLE(i2c, wm8737_i2c_id);
6942a9ae13aSMark Brown 
6952a9ae13aSMark Brown static struct i2c_driver wm8737_i2c_driver = {
6962a9ae13aSMark Brown 	.driver = {
6972a9ae13aSMark Brown 		.name = "wm8737",
6982a9ae13aSMark Brown 		.owner = THIS_MODULE,
69953a5a83dSMark Brown 		.of_match_table = wm8737_of_match,
7002a9ae13aSMark Brown 	},
7012a9ae13aSMark Brown 	.probe =    wm8737_i2c_probe,
7027a79e94eSBill Pemberton 	.remove =   wm8737_i2c_remove,
7032a9ae13aSMark Brown 	.id_table = wm8737_i2c_id,
7042a9ae13aSMark Brown };
7052a9ae13aSMark Brown #endif
7062a9ae13aSMark Brown 
7072a9ae13aSMark Brown #if defined(CONFIG_SPI_MASTER)
7087a79e94eSBill Pemberton static int wm8737_spi_probe(struct spi_device *spi)
7092a9ae13aSMark Brown {
7102a9ae13aSMark Brown 	struct wm8737_priv *wm8737;
7114f69bb31SMark Brown 	int ret, i;
7122a9ae13aSMark Brown 
71365fdd9bfSMark Brown 	wm8737 = devm_kzalloc(&spi->dev, sizeof(struct wm8737_priv),
71465fdd9bfSMark Brown 			      GFP_KERNEL);
7152a9ae13aSMark Brown 	if (wm8737 == NULL)
7162a9ae13aSMark Brown 		return -ENOMEM;
7172a9ae13aSMark Brown 
7184f69bb31SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8737->supplies); i++)
7194f69bb31SMark Brown 		wm8737->supplies[i].supply = wm8737_supply_names[i];
7204f69bb31SMark Brown 
7214f69bb31SMark Brown 	ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8737->supplies),
7224f69bb31SMark Brown 				      wm8737->supplies);
7234f69bb31SMark Brown 	if (ret != 0) {
7244f69bb31SMark Brown 		dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
7254f69bb31SMark Brown 		return ret;
7264f69bb31SMark Brown 	}
7274f69bb31SMark Brown 
7283ef8ac0dSMark Brown 	wm8737->regmap = devm_regmap_init_spi(spi, &wm8737_regmap);
7293ef8ac0dSMark Brown 	if (IS_ERR(wm8737->regmap))
7303ef8ac0dSMark Brown 		return PTR_ERR(wm8737->regmap);
7313ef8ac0dSMark Brown 
7322a9ae13aSMark Brown 	spi_set_drvdata(spi, wm8737);
7332a9ae13aSMark Brown 
7342a9ae13aSMark Brown 	ret = snd_soc_register_codec(&spi->dev,
7352a9ae13aSMark Brown 				     &soc_codec_dev_wm8737, &wm8737_dai, 1);
73665fdd9bfSMark Brown 
7372a9ae13aSMark Brown 	return ret;
7382a9ae13aSMark Brown }
7392a9ae13aSMark Brown 
7407a79e94eSBill Pemberton static int wm8737_spi_remove(struct spi_device *spi)
7412a9ae13aSMark Brown {
7422a9ae13aSMark Brown 	snd_soc_unregister_codec(&spi->dev);
74365fdd9bfSMark Brown 
7442a9ae13aSMark Brown 	return 0;
7452a9ae13aSMark Brown }
7462a9ae13aSMark Brown 
7472a9ae13aSMark Brown static struct spi_driver wm8737_spi_driver = {
7482a9ae13aSMark Brown 	.driver = {
7492a9ae13aSMark Brown 		.name	= "wm8737",
7502a9ae13aSMark Brown 		.owner	= THIS_MODULE,
75153a5a83dSMark Brown 		.of_match_table = wm8737_of_match,
7522a9ae13aSMark Brown 	},
7532a9ae13aSMark Brown 	.probe		= wm8737_spi_probe,
7547a79e94eSBill Pemberton 	.remove		= wm8737_spi_remove,
7552a9ae13aSMark Brown };
7562a9ae13aSMark Brown #endif /* CONFIG_SPI_MASTER */
7572a9ae13aSMark Brown 
7582a9ae13aSMark Brown static int __init wm8737_modinit(void)
7592a9ae13aSMark Brown {
7602a9ae13aSMark Brown 	int ret;
7611037d567SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7622a9ae13aSMark Brown 	ret = i2c_add_driver(&wm8737_i2c_driver);
7632a9ae13aSMark Brown 	if (ret != 0) {
7642a9ae13aSMark Brown 		printk(KERN_ERR "Failed to register WM8737 I2C driver: %d\n",
7652a9ae13aSMark Brown 		       ret);
7662a9ae13aSMark Brown 	}
7672a9ae13aSMark Brown #endif
7682a9ae13aSMark Brown #if defined(CONFIG_SPI_MASTER)
7692a9ae13aSMark Brown 	ret = spi_register_driver(&wm8737_spi_driver);
7702a9ae13aSMark Brown 	if (ret != 0) {
7712a9ae13aSMark Brown 		printk(KERN_ERR "Failed to register WM8737 SPI driver: %d\n",
7722a9ae13aSMark Brown 		       ret);
7732a9ae13aSMark Brown 	}
7742a9ae13aSMark Brown #endif
7752a9ae13aSMark Brown 	return 0;
7762a9ae13aSMark Brown }
7772a9ae13aSMark Brown module_init(wm8737_modinit);
7782a9ae13aSMark Brown 
7792a9ae13aSMark Brown static void __exit wm8737_exit(void)
7802a9ae13aSMark Brown {
7812a9ae13aSMark Brown #if defined(CONFIG_SPI_MASTER)
7822a9ae13aSMark Brown 	spi_unregister_driver(&wm8737_spi_driver);
7832a9ae13aSMark Brown #endif
7841037d567SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
7852a9ae13aSMark Brown 	i2c_del_driver(&wm8737_i2c_driver);
7862a9ae13aSMark Brown #endif
7872a9ae13aSMark Brown }
7882a9ae13aSMark Brown module_exit(wm8737_exit);
7892a9ae13aSMark Brown 
7902a9ae13aSMark Brown MODULE_DESCRIPTION("ASoC WM8737 driver");
7912a9ae13aSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
7922a9ae13aSMark Brown MODULE_LICENSE("GPL");
793