xref: /openbmc/linux/sound/soc/codecs/max9867.c (revision e6ceb922ca802711db4b48639a9c73f9dc412f87)
1805d132dSanish kumar /*
2805d132dSanish kumar  * max9867.c -- max9867 ALSA SoC Audio driver
3805d132dSanish kumar  *
4805d132dSanish kumar  * Copyright 2013-15 Maxim Integrated Products
5805d132dSanish kumar  *
6805d132dSanish kumar  * This program is free software; you can redistribute it and/or modify
7805d132dSanish kumar  * it under the terms of the GNU General Public License version 2 as
8805d132dSanish kumar  * published by the Free Software Foundation.
9805d132dSanish kumar  */
10805d132dSanish kumar 
11805d132dSanish kumar #include <linux/delay.h>
12805d132dSanish kumar #include <linux/i2c.h>
13805d132dSanish kumar #include <linux/module.h>
14805d132dSanish kumar #include <linux/regmap.h>
15805d132dSanish kumar #include <sound/pcm_params.h>
16805d132dSanish kumar #include <sound/soc.h>
17805d132dSanish kumar #include <sound/tlv.h>
18805d132dSanish kumar #include "max9867.h"
19805d132dSanish kumar 
20805d132dSanish kumar static const char *const max9867_spmode[] = {
21805d132dSanish kumar 	"Stereo Diff", "Mono Diff",
22805d132dSanish kumar 	"Stereo Cap", "Mono Cap",
23805d132dSanish kumar 	"Stereo Single", "Mono Single",
24805d132dSanish kumar 	"Stereo Single Fast", "Mono Single Fast"
25805d132dSanish kumar };
26805d132dSanish kumar static const char *const max9867_sidetone_text[] = {
27805d132dSanish kumar 	"None", "Left", "Right", "LeftRight", "LeftRightDiv2",
28805d132dSanish kumar };
29805d132dSanish kumar static const char *const max9867_filter_text[] = {"IIR", "FIR"};
30805d132dSanish kumar 
31805d132dSanish kumar static SOC_ENUM_SINGLE_DECL(max9867_filter, MAX9867_CODECFLTR, 7,
32805d132dSanish kumar 	max9867_filter_text);
33805d132dSanish kumar static SOC_ENUM_SINGLE_DECL(max9867_spkmode, MAX9867_MODECONFIG, 0,
34805d132dSanish kumar 	max9867_spmode);
35805d132dSanish kumar static SOC_ENUM_SINGLE_DECL(max9867_sidetone, MAX9867_DACGAIN, 6,
36805d132dSanish kumar 	max9867_sidetone_text);
37805d132dSanish kumar static DECLARE_TLV_DB_SCALE(max9860_capture_tlv, -600, 200, 0);
38805d132dSanish kumar static DECLARE_TLV_DB_SCALE(max9860_mic_tlv, 2000, 100, 1);
39805d132dSanish kumar static DECLARE_TLV_DB_SCALE(max9860_adc_left_tlv, -1200, 100, 1);
40805d132dSanish kumar static DECLARE_TLV_DB_SCALE(max9860_adc_right_tlv, -1200, 100, 1);
41349fa18cSTakashi Sakamoto static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max98088_micboost_tlv,
42805d132dSanish kumar 	0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
43805d132dSanish kumar 	2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
44349fa18cSTakashi Sakamoto );
45805d132dSanish kumar 
46805d132dSanish kumar static const struct snd_kcontrol_new max9867_snd_controls[] = {
47805d132dSanish kumar 	SOC_DOUBLE_R("Master Playback Volume", MAX9867_LEFTVOL,
48805d132dSanish kumar 				MAX9867_RIGHTVOL, 0, 63, 1),
49805d132dSanish kumar 	SOC_DOUBLE_R_TLV("Capture Volume", MAX9867_LEFTMICGAIN,
50805d132dSanish kumar 			MAX9867_RIGHTMICGAIN,
51805d132dSanish kumar 			0, 15, 1, max9860_capture_tlv),
52805d132dSanish kumar 	SOC_DOUBLE_R_TLV("Mic Volume", MAX9867_LEFTMICGAIN,
53805d132dSanish kumar 			MAX9867_RIGHTMICGAIN, 0, 31, 1, max9860_mic_tlv),
54805d132dSanish kumar 	SOC_DOUBLE_R_TLV("Mic Boost Volume", MAX9867_LEFTMICGAIN,
55805d132dSanish kumar 			MAX9867_RIGHTMICGAIN, 5, 3, 0, max98088_micboost_tlv),
56805d132dSanish kumar 	SOC_ENUM("Digital Sidetone Src", max9867_sidetone),
57805d132dSanish kumar 	SOC_SINGLE("Sidetone Volume", MAX9867_DACGAIN, 0, 31, 1),
58805d132dSanish kumar 	SOC_SINGLE("DAC Volume", MAX9867_DACLEVEL, 4, 3, 0),
59805d132dSanish kumar 	SOC_SINGLE("DAC Attenuation", MAX9867_DACLEVEL, 0, 15, 1),
60805d132dSanish kumar 	SOC_SINGLE_TLV("ADC Left Volume", MAX9867_ADCLEVEL,
61805d132dSanish kumar 			4, 15, 1, max9860_adc_left_tlv),
62805d132dSanish kumar 	SOC_SINGLE_TLV("ADC Right Volume", MAX9867_ADCLEVEL,
63805d132dSanish kumar 			0, 15, 1, max9860_adc_right_tlv),
64805d132dSanish kumar 	SOC_ENUM("Speaker Mode", max9867_spkmode),
65805d132dSanish kumar 	SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG, 6, 1, 0),
66805d132dSanish kumar 	SOC_SINGLE("ZCD Switch", MAX9867_MODECONFIG, 5, 1, 0),
67805d132dSanish kumar 	SOC_ENUM("DSP Filter", max9867_filter),
68805d132dSanish kumar };
69805d132dSanish kumar 
70805d132dSanish kumar static const char *const max9867_mux[] = {"None", "Mic", "Line", "Mic_Line"};
71805d132dSanish kumar 
72805d132dSanish kumar static SOC_ENUM_SINGLE_DECL(max9867_mux_enum,
73805d132dSanish kumar 	MAX9867_INPUTCONFIG, MAX9867_INPUT_SHIFT,
74805d132dSanish kumar 	max9867_mux);
75805d132dSanish kumar 
76805d132dSanish kumar static const struct snd_kcontrol_new max9867_dapm_mux_controls =
77805d132dSanish kumar 	SOC_DAPM_ENUM("Route", max9867_mux_enum);
78805d132dSanish kumar 
79805d132dSanish kumar static const struct snd_kcontrol_new max9867_left_dapm_control =
80805d132dSanish kumar 	SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 6, 1, 0);
81805d132dSanish kumar static const struct snd_kcontrol_new max9867_right_dapm_control =
82805d132dSanish kumar 	SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 5, 1, 0);
83805d132dSanish kumar static const struct snd_kcontrol_new max9867_line_dapm_control =
84805d132dSanish kumar 	SOC_DAPM_SINGLE("Switch", MAX9867_LEFTLINELVL, 6, 1, 1);
85805d132dSanish kumar 
86805d132dSanish kumar static const struct snd_soc_dapm_widget max9867_dapm_widgets[] = {
87805d132dSanish kumar 	SND_SOC_DAPM_AIF_IN("DAI_OUT", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
88805d132dSanish kumar 	SND_SOC_DAPM_DAC("Left DAC", NULL, MAX9867_PWRMAN, 3, 0),
89805d132dSanish kumar 	SND_SOC_DAPM_DAC("Right DAC", NULL, MAX9867_PWRMAN, 2, 0),
90805d132dSanish kumar 	SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
91805d132dSanish kumar 	SND_SOC_DAPM_OUTPUT("HPOUT"),
92805d132dSanish kumar 
93805d132dSanish kumar 	SND_SOC_DAPM_AIF_IN("DAI_IN", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
94805d132dSanish kumar 	SND_SOC_DAPM_ADC("Left ADC", "HiFi Capture", MAX9867_PWRMAN, 1, 0),
95805d132dSanish kumar 	SND_SOC_DAPM_ADC("Right ADC", "HiFi Capture", MAX9867_PWRMAN, 0, 0),
96805d132dSanish kumar 	SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
97805d132dSanish kumar 		&max9867_dapm_mux_controls),
98805d132dSanish kumar 
99805d132dSanish kumar 	SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
100805d132dSanish kumar 	SND_SOC_DAPM_SWITCH("Left Line", MAX9867_LEFTLINELVL, 6, 1,
101805d132dSanish kumar 		&max9867_left_dapm_control),
102805d132dSanish kumar 	SND_SOC_DAPM_SWITCH("Right Line", MAX9867_RIGTHLINELVL, 6, 1,
103805d132dSanish kumar 		&max9867_right_dapm_control),
104805d132dSanish kumar 	SND_SOC_DAPM_SWITCH("Line Mixer", SND_SOC_NOPM, 0, 0,
105805d132dSanish kumar 		&max9867_line_dapm_control),
106805d132dSanish kumar 	SND_SOC_DAPM_INPUT("LINE_IN"),
107805d132dSanish kumar };
108805d132dSanish kumar 
109805d132dSanish kumar static const struct snd_soc_dapm_route max9867_audio_map[] = {
110805d132dSanish kumar 	{"Left DAC", NULL, "DAI_OUT"},
111805d132dSanish kumar 	{"Right DAC", NULL, "DAI_OUT"},
112805d132dSanish kumar 	{"Output Mixer", NULL, "Left DAC"},
113805d132dSanish kumar 	{"Output Mixer", NULL, "Right DAC"},
114805d132dSanish kumar 	{"HPOUT", NULL, "Output Mixer"},
115805d132dSanish kumar 
116805d132dSanish kumar 	{"Left ADC", NULL, "DAI_IN"},
117805d132dSanish kumar 	{"Right ADC", NULL, "DAI_IN"},
118805d132dSanish kumar 	{"Input Mixer", NULL, "Left ADC"},
119805d132dSanish kumar 	{"Input Mixer", NULL, "Right ADC"},
120805d132dSanish kumar 	{"Input Mux", "Line", "Input Mixer"},
121805d132dSanish kumar 	{"Input Mux", "Mic", "Input Mixer"},
122805d132dSanish kumar 	{"Input Mux", "Mic_Line", "Input Mixer"},
123805d132dSanish kumar 	{"Right Line", "Switch", "Input Mux"},
124805d132dSanish kumar 	{"Left Line", "Switch", "Input Mux"},
125805d132dSanish kumar 	{"LINE_IN", NULL, "Left Line"},
126805d132dSanish kumar 	{"LINE_IN", NULL, "Right Line"},
127805d132dSanish kumar };
128805d132dSanish kumar 
129805d132dSanish kumar enum rates {
130805d132dSanish kumar 	pcm_rate_8, pcm_rate_16, pcm_rate_24,
131805d132dSanish kumar 	pcm_rate_32, pcm_rate_44,
132805d132dSanish kumar 	pcm_rate_48, max_pcm_rate,
133805d132dSanish kumar };
134805d132dSanish kumar 
1351943b066SColin Ian King static const struct ni_div_rates {
136805d132dSanish kumar 	u32 mclk;
137805d132dSanish kumar 	u16 ni[max_pcm_rate];
138805d132dSanish kumar } ni_div[] = {
139805d132dSanish kumar 	{11289600, {0x116A, 0x22D4, 0x343F, 0x45A9, 0x6000, 0x687D} },
140805d132dSanish kumar 	{12000000, {0x1062, 0x20C5, 0x3127, 0x4189, 0x5A51, 0x624E} },
141805d132dSanish kumar 	{12288000, {0x1000, 0x2000, 0x3000, 0x4000, 0x5833, 0x6000} },
142805d132dSanish kumar 	{13000000, {0x0F20, 0x1E3F, 0x2D5F, 0x3C7F, 0x535F, 0x5ABE} },
143805d132dSanish kumar 	{19200000, {0x0A3D, 0x147B, 0x1EB8, 0x28F6, 0x3873, 0x3D71} },
144805d132dSanish kumar 	{24000000, {0x1062, 0x20C5, 0x1893, 0x4189, 0x5A51, 0x624E} },
145805d132dSanish kumar 	{26000000, {0x0F20, 0x1E3F, 0x16AF, 0x3C7F, 0x535F, 0x5ABE} },
146805d132dSanish kumar 	{27000000, {0x0E90, 0x1D21, 0x15D8, 0x3A41, 0x5048, 0x5762} },
147805d132dSanish kumar };
148805d132dSanish kumar 
149805d132dSanish kumar static inline int get_ni_value(int mclk, int rate)
150805d132dSanish kumar {
151805d132dSanish kumar 	int i, ret = 0;
152805d132dSanish kumar 
153805d132dSanish kumar 	/* find the closest rate index*/
154805d132dSanish kumar 	for (i = 0; i < ARRAY_SIZE(ni_div); i++) {
155805d132dSanish kumar 		if (ni_div[i].mclk >= mclk)
156805d132dSanish kumar 			break;
157805d132dSanish kumar 	}
15890966391SDan Carpenter 	if (i == ARRAY_SIZE(ni_div))
15990966391SDan Carpenter 		return -EINVAL;
160805d132dSanish kumar 
161805d132dSanish kumar 	switch (rate) {
162805d132dSanish kumar 	case 8000:
163805d132dSanish kumar 		return ni_div[i].ni[pcm_rate_8];
164805d132dSanish kumar 	case 16000:
165805d132dSanish kumar 		return ni_div[i].ni[pcm_rate_16];
166805d132dSanish kumar 	case 32000:
167805d132dSanish kumar 		return ni_div[i].ni[pcm_rate_32];
168805d132dSanish kumar 	case 44100:
169805d132dSanish kumar 		return ni_div[i].ni[pcm_rate_44];
170805d132dSanish kumar 	case 48000:
171805d132dSanish kumar 		return ni_div[i].ni[pcm_rate_48];
172805d132dSanish kumar 	default:
173805d132dSanish kumar 		pr_err("%s wrong rate %d\n", __func__, rate);
174805d132dSanish kumar 		ret = -EINVAL;
175805d132dSanish kumar 	}
176805d132dSanish kumar 	return ret;
177805d132dSanish kumar }
178805d132dSanish kumar 
179805d132dSanish kumar static int max9867_dai_hw_params(struct snd_pcm_substream *substream,
180805d132dSanish kumar 		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
181805d132dSanish kumar {
182805d132dSanish kumar 	struct snd_soc_codec *codec = dai->codec;
183805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
184127a9cbbSAndrzej Hajda 	unsigned int ni_h, ni_l;
185127a9cbbSAndrzej Hajda 	int value;
186805d132dSanish kumar 
187805d132dSanish kumar 	value = get_ni_value(max9867->sysclk, params_rate(params));
188805d132dSanish kumar 	if (value < 0)
189805d132dSanish kumar 		return value;
190805d132dSanish kumar 
191805d132dSanish kumar 	ni_h = (0xFF00 & value) >> 8;
192805d132dSanish kumar 	ni_l = 0x00FF & value;
193805d132dSanish kumar 	/* set up the ni value */
194805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
195805d132dSanish kumar 		MAX9867_NI_HIGH_MASK, ni_h);
196805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
197805d132dSanish kumar 		MAX9867_NI_LOW_MASK, ni_l);
198805d132dSanish kumar 	if (!max9867->master) {
199805d132dSanish kumar 		/*
200805d132dSanish kumar 		 * digital pll locks on to any externally supplied LRCLK signal
201805d132dSanish kumar 		 * and also enable rapid lock mode.
202805d132dSanish kumar 		 */
203805d132dSanish kumar 		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
204805d132dSanish kumar 			MAX9867_RAPID_LOCK, MAX9867_RAPID_LOCK);
205805d132dSanish kumar 		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
206805d132dSanish kumar 			MAX9867_PLL, MAX9867_PLL);
207805d132dSanish kumar 	} else {
208805d132dSanish kumar 		unsigned long int bclk_rate, pclk_bclk_ratio;
209805d132dSanish kumar 		int bclk_value;
210805d132dSanish kumar 
2119dcc72efSAxel Lin 		bclk_rate = params_rate(params) * 2 * params_width(params);
212805d132dSanish kumar 		pclk_bclk_ratio = max9867->pclk/bclk_rate;
2139dcc72efSAxel Lin 		switch (params_width(params)) {
214805d132dSanish kumar 		case 8:
215805d132dSanish kumar 		case 16:
216b1461450Sanish kumar 			switch (pclk_bclk_ratio) {
217b1461450Sanish kumar 			case 2:
218805d132dSanish kumar 				bclk_value = MAX9867_IFC1B_PCLK_2;
219b1461450Sanish kumar 				break;
220b1461450Sanish kumar 			case 4:
221805d132dSanish kumar 				bclk_value = MAX9867_IFC1B_PCLK_4;
222b1461450Sanish kumar 				break;
223b1461450Sanish kumar 			case 8:
224805d132dSanish kumar 				bclk_value = MAX9867_IFC1B_PCLK_8;
225b1461450Sanish kumar 				break;
226b1461450Sanish kumar 			case 16:
227805d132dSanish kumar 				bclk_value = MAX9867_IFC1B_PCLK_16;
228b1461450Sanish kumar 				break;
229b1461450Sanish kumar 			default:
230b1461450Sanish kumar 				dev_err(codec->dev,
231b1461450Sanish kumar 					"unsupported sampling rate\n");
232805d132dSanish kumar 				return -EINVAL;
233805d132dSanish kumar 			}
234805d132dSanish kumar 			break;
235805d132dSanish kumar 		case 24:
236805d132dSanish kumar 			bclk_value = MAX9867_IFC1B_24BIT;
237805d132dSanish kumar 			break;
238805d132dSanish kumar 		case 32:
239805d132dSanish kumar 			bclk_value = MAX9867_IFC1B_32BIT;
240805d132dSanish kumar 			break;
241805d132dSanish kumar 		default:
242805d132dSanish kumar 			dev_err(codec->dev, "unsupported sampling rate\n");
243805d132dSanish kumar 			return -EINVAL;
244805d132dSanish kumar 		}
245805d132dSanish kumar 		regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
246805d132dSanish kumar 			MAX9867_IFC1B_BCLK_MASK, bclk_value);
247805d132dSanish kumar 	}
248805d132dSanish kumar 	return 0;
249805d132dSanish kumar }
250805d132dSanish kumar 
251805d132dSanish kumar static int max9867_prepare(struct snd_pcm_substream *substream,
252805d132dSanish kumar 			 struct snd_soc_dai *dai)
253805d132dSanish kumar {
254805d132dSanish kumar 	struct snd_soc_codec *codec = dai->codec;
255805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
256805d132dSanish kumar 
257805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
258805d132dSanish kumar 		MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK);
259805d132dSanish kumar 	return 0;
260805d132dSanish kumar }
261805d132dSanish kumar 
262805d132dSanish kumar static int max9867_mute(struct snd_soc_dai *dai, int mute)
263805d132dSanish kumar {
264805d132dSanish kumar 	struct snd_soc_codec *codec = dai->codec;
265805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
266805d132dSanish kumar 
267805d132dSanish kumar 	if (mute)
268805d132dSanish kumar 		regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
269805d132dSanish kumar 			MAX9867_DAC_MUTE_MASK, MAX9867_DAC_MUTE_MASK);
270805d132dSanish kumar 	else
271805d132dSanish kumar 		regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
272805d132dSanish kumar 			MAX9867_DAC_MUTE_MASK, 0);
273805d132dSanish kumar 	return 0;
274805d132dSanish kumar }
275805d132dSanish kumar 
276805d132dSanish kumar static int max9867_set_dai_sysclk(struct snd_soc_dai *codec_dai,
277805d132dSanish kumar 		int clk_id, unsigned int freq, int dir)
278805d132dSanish kumar {
279805d132dSanish kumar 	struct snd_soc_codec *codec = codec_dai->codec;
280805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
281805d132dSanish kumar 	int value = 0;
282805d132dSanish kumar 
283805d132dSanish kumar 	/* Set the prescaler based on the master clock frequency*/
284805d132dSanish kumar 	if (freq >= 10000000 && freq <= 20000000) {
285805d132dSanish kumar 		value |= MAX9867_PSCLK_10_20;
286805d132dSanish kumar 		max9867->pclk =  freq;
287805d132dSanish kumar 	} else if (freq >= 20000000 && freq <= 40000000) {
288805d132dSanish kumar 		value |= MAX9867_PSCLK_20_40;
289805d132dSanish kumar 		max9867->pclk =  freq/2;
290805d132dSanish kumar 	} else if (freq >= 40000000 && freq <= 60000000) {
291805d132dSanish kumar 		value |= MAX9867_PSCLK_40_60;
292805d132dSanish kumar 		max9867->pclk =  freq/4;
293805d132dSanish kumar 	} else {
2948b9c716aSLadislav Michl 		dev_err(component->dev,
2958b9c716aSLadislav Michl 			"Invalid clock frequency %uHz (required 10-60MHz)\n",
2968b9c716aSLadislav Michl 			freq);
297805d132dSanish kumar 		return -EINVAL;
298805d132dSanish kumar 	}
299805d132dSanish kumar 	value = value << MAX9867_PSCLK_SHIFT;
300805d132dSanish kumar 	max9867->sysclk = freq;
301805d132dSanish kumar 	/* exact integer mode is not supported */
302805d132dSanish kumar 	value &= ~MAX9867_FREQ_MASK;
303805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_SYSCLK,
304805d132dSanish kumar 			MAX9867_PSCLK_MASK, value);
305805d132dSanish kumar 	return 0;
306805d132dSanish kumar }
307805d132dSanish kumar 
308805d132dSanish kumar static int max9867_dai_set_fmt(struct snd_soc_dai *codec_dai,
309805d132dSanish kumar 		unsigned int fmt)
310805d132dSanish kumar {
311805d132dSanish kumar 	struct snd_soc_codec *codec = codec_dai->codec;
312805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
313805d132dSanish kumar 	u8 iface1A = 0, iface1B = 0;
314805d132dSanish kumar 
315805d132dSanish kumar 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
316805d132dSanish kumar 	case SND_SOC_DAIFMT_CBM_CFM:
317805d132dSanish kumar 		max9867->master = 1;
318805d132dSanish kumar 		iface1A |= MAX9867_MASTER;
319805d132dSanish kumar 		break;
320805d132dSanish kumar 	case SND_SOC_DAIFMT_CBS_CFS:
321805d132dSanish kumar 		max9867->master = 0;
322805d132dSanish kumar 		iface1A &= ~MAX9867_MASTER;
323805d132dSanish kumar 		break;
324805d132dSanish kumar 	default:
325805d132dSanish kumar 		return -EINVAL;
326805d132dSanish kumar 	}
327805d132dSanish kumar 
328805d132dSanish kumar 	/* for i2s compatible mode */
329805d132dSanish kumar 	iface1A |= MAX9867_I2S_DLY;
330805d132dSanish kumar 	/* SDOUT goes to hiz state after all data is transferred */
331805d132dSanish kumar 	iface1A |= MAX9867_SDOUT_HIZ;
332805d132dSanish kumar 
333805d132dSanish kumar 	/* Clock inversion bits, BCI and WCI */
334805d132dSanish kumar 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
335805d132dSanish kumar 	case SND_SOC_DAIFMT_NB_NF:
336805d132dSanish kumar 		break;
337805d132dSanish kumar 	case SND_SOC_DAIFMT_IB_IF:
338805d132dSanish kumar 		iface1A |= MAX9867_WCI_MODE | MAX9867_BCI_MODE;
339805d132dSanish kumar 		break;
340805d132dSanish kumar 	case SND_SOC_DAIFMT_IB_NF:
341805d132dSanish kumar 		iface1A |= MAX9867_BCI_MODE;
342805d132dSanish kumar 		break;
343805d132dSanish kumar 	case SND_SOC_DAIFMT_NB_IF:
344805d132dSanish kumar 		iface1A |= MAX9867_WCI_MODE;
345805d132dSanish kumar 		break;
346805d132dSanish kumar 	default:
347805d132dSanish kumar 		return -EINVAL;
348805d132dSanish kumar 	}
349805d132dSanish kumar 
3509fe78b28SVinod Koul 	regmap_write(max9867->regmap, MAX9867_IFC1A, iface1A);
3519fe78b28SVinod Koul 	regmap_write(max9867->regmap, MAX9867_IFC1B, iface1B);
352805d132dSanish kumar 	return 0;
353805d132dSanish kumar }
354805d132dSanish kumar 
355eb59d73cSArvind Yadav static const struct snd_soc_dai_ops max9867_dai_ops = {
356805d132dSanish kumar 	.set_fmt = max9867_dai_set_fmt,
357805d132dSanish kumar 	.set_sysclk	= max9867_set_dai_sysclk,
358805d132dSanish kumar 	.prepare	= max9867_prepare,
359805d132dSanish kumar 	.digital_mute	= max9867_mute,
360805d132dSanish kumar 	.hw_params = max9867_dai_hw_params,
361805d132dSanish kumar };
362805d132dSanish kumar 
363805d132dSanish kumar #define MAX9867_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
364805d132dSanish kumar 	SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
365805d132dSanish kumar #define MAX9867_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
366805d132dSanish kumar 
367805d132dSanish kumar static struct snd_soc_dai_driver max9867_dai[] = {
368805d132dSanish kumar 	{
369805d132dSanish kumar 	.name = "max9867-aif1",
370805d132dSanish kumar 	.playback = {
371805d132dSanish kumar 		.stream_name = "HiFi Playback",
372*e6ceb922SLadislav Michl 		.channels_min = 2,
373805d132dSanish kumar 		.channels_max = 2,
374805d132dSanish kumar 		.rates = MAX9867_RATES,
375805d132dSanish kumar 		.formats = MAX9867_FORMATS,
376805d132dSanish kumar 	},
377805d132dSanish kumar 	.capture = {
378805d132dSanish kumar 		.stream_name = "HiFi Capture",
379*e6ceb922SLadislav Michl 		.channels_min = 2,
380805d132dSanish kumar 		.channels_max = 2,
381805d132dSanish kumar 		.rates = MAX9867_RATES,
382805d132dSanish kumar 		.formats = MAX9867_FORMATS,
383805d132dSanish kumar 	},
384805d132dSanish kumar 	.ops = &max9867_dai_ops,
385*e6ceb922SLadislav Michl 	.symmetric_rates = 1,
386805d132dSanish kumar 	}
387805d132dSanish kumar };
388805d132dSanish kumar 
389805d132dSanish kumar #ifdef CONFIG_PM_SLEEP
390805d132dSanish kumar static int max9867_suspend(struct device *dev)
391805d132dSanish kumar {
392805d132dSanish kumar 	struct max9867_priv *max9867 = dev_get_drvdata(dev);
393805d132dSanish kumar 
394805d132dSanish kumar 	/* Drop down to power saving mode when system is suspended */
395805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
396805d132dSanish kumar 		MAX9867_SHTDOWN_MASK, ~MAX9867_SHTDOWN_MASK);
397805d132dSanish kumar 	return 0;
398805d132dSanish kumar }
399805d132dSanish kumar 
400805d132dSanish kumar static int max9867_resume(struct device *dev)
401805d132dSanish kumar {
402805d132dSanish kumar 	struct max9867_priv *max9867 = dev_get_drvdata(dev);
403805d132dSanish kumar 
404805d132dSanish kumar 	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
405805d132dSanish kumar 		MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK);
406805d132dSanish kumar 	return 0;
407805d132dSanish kumar }
408805d132dSanish kumar #endif
409805d132dSanish kumar 
410805d132dSanish kumar static int max9867_probe(struct snd_soc_codec *codec)
411805d132dSanish kumar {
412805d132dSanish kumar 	struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
413805d132dSanish kumar 
414805d132dSanish kumar 	dev_dbg(codec->dev, "max98090_probe\n");
415805d132dSanish kumar 	max9867->codec = codec;
416805d132dSanish kumar 	return 0;
417805d132dSanish kumar }
418805d132dSanish kumar 
419a180ba45SBhumika Goyal static const struct snd_soc_codec_driver max9867_codec = {
420805d132dSanish kumar 	.probe = max9867_probe,
421e92b48aaSKuninori Morimoto 	.component_driver = {
422805d132dSanish kumar 		.controls		= max9867_snd_controls,
423805d132dSanish kumar 		.num_controls		= ARRAY_SIZE(max9867_snd_controls),
424805d132dSanish kumar 		.dapm_routes		= max9867_audio_map,
425805d132dSanish kumar 		.num_dapm_routes	= ARRAY_SIZE(max9867_audio_map),
426805d132dSanish kumar 		.dapm_widgets		= max9867_dapm_widgets,
427805d132dSanish kumar 		.num_dapm_widgets	= ARRAY_SIZE(max9867_dapm_widgets),
428e92b48aaSKuninori Morimoto 	},
429805d132dSanish kumar };
430805d132dSanish kumar 
431805d132dSanish kumar static bool max9867_volatile_register(struct device *dev, unsigned int reg)
432805d132dSanish kumar {
433805d132dSanish kumar 	switch (reg) {
434805d132dSanish kumar 	case MAX9867_STATUS:
435805d132dSanish kumar 	case MAX9867_JACKSTATUS:
436805d132dSanish kumar 	case MAX9867_AUXHIGH:
437805d132dSanish kumar 	case MAX9867_AUXLOW:
438805d132dSanish kumar 		return true;
439805d132dSanish kumar 	default:
440805d132dSanish kumar 		return false;
441805d132dSanish kumar 	}
442805d132dSanish kumar }
443805d132dSanish kumar 
444250a99e7SAxel Lin static const struct reg_default max9867_reg[] = {
445805d132dSanish kumar 	{ 0x04, 0x00 },
446805d132dSanish kumar 	{ 0x05, 0x00 },
447805d132dSanish kumar 	{ 0x06, 0x00 },
448805d132dSanish kumar 	{ 0x07, 0x00 },
449805d132dSanish kumar 	{ 0x08, 0x00 },
450805d132dSanish kumar 	{ 0x09, 0x00 },
451805d132dSanish kumar 	{ 0x0A, 0x00 },
452805d132dSanish kumar 	{ 0x0B, 0x00 },
453805d132dSanish kumar 	{ 0x0C, 0x00 },
454805d132dSanish kumar 	{ 0x0D, 0x00 },
455805d132dSanish kumar 	{ 0x0E, 0x00 },
456805d132dSanish kumar 	{ 0x0F, 0x00 },
457805d132dSanish kumar 	{ 0x10, 0x00 },
458805d132dSanish kumar 	{ 0x11, 0x00 },
459805d132dSanish kumar 	{ 0x12, 0x00 },
460805d132dSanish kumar 	{ 0x13, 0x00 },
461805d132dSanish kumar 	{ 0x14, 0x00 },
462805d132dSanish kumar 	{ 0x15, 0x00 },
463805d132dSanish kumar 	{ 0x16, 0x00 },
464805d132dSanish kumar 	{ 0x17, 0x00 },
465805d132dSanish kumar };
466805d132dSanish kumar 
467250a99e7SAxel Lin static const struct regmap_config max9867_regmap = {
468805d132dSanish kumar 	.reg_bits	= 8,
469805d132dSanish kumar 	.val_bits	= 8,
470805d132dSanish kumar 	.max_register	= MAX9867_REVISION,
471805d132dSanish kumar 	.reg_defaults	= max9867_reg,
472805d132dSanish kumar 	.num_reg_defaults = ARRAY_SIZE(max9867_reg),
473805d132dSanish kumar 	.volatile_reg	= max9867_volatile_register,
474805d132dSanish kumar 	.cache_type	= REGCACHE_RBTREE,
475805d132dSanish kumar };
476805d132dSanish kumar 
477805d132dSanish kumar static int max9867_i2c_probe(struct i2c_client *i2c,
478805d132dSanish kumar 		const struct i2c_device_id *id)
479805d132dSanish kumar {
480805d132dSanish kumar 	struct max9867_priv *max9867;
481805d132dSanish kumar 	int ret = 0, reg;
482805d132dSanish kumar 
483805d132dSanish kumar 	max9867 = devm_kzalloc(&i2c->dev,
484805d132dSanish kumar 			sizeof(*max9867), GFP_KERNEL);
485805d132dSanish kumar 	if (!max9867)
486805d132dSanish kumar 		return -ENOMEM;
487805d132dSanish kumar 
488805d132dSanish kumar 	i2c_set_clientdata(i2c, max9867);
489805d132dSanish kumar 	max9867->regmap = devm_regmap_init_i2c(i2c, &max9867_regmap);
490805d132dSanish kumar 	if (IS_ERR(max9867->regmap)) {
491805d132dSanish kumar 		ret = PTR_ERR(max9867->regmap);
4928b9c716aSLadislav Michl 		dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
493805d132dSanish kumar 		return ret;
494805d132dSanish kumar 	}
495805d132dSanish kumar 	ret = regmap_read(max9867->regmap,
496805d132dSanish kumar 			MAX9867_REVISION, &reg);
497805d132dSanish kumar 	if (ret < 0) {
498805d132dSanish kumar 		dev_err(&i2c->dev, "Failed to read: %d\n", ret);
499805d132dSanish kumar 		return ret;
500805d132dSanish kumar 	}
501805d132dSanish kumar 	dev_info(&i2c->dev, "device revision: %x\n", reg);
502805d132dSanish kumar 	ret = snd_soc_register_codec(&i2c->dev, &max9867_codec,
503805d132dSanish kumar 			max9867_dai, ARRAY_SIZE(max9867_dai));
504805d132dSanish kumar 	if (ret < 0) {
505805d132dSanish kumar 		dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
506805d132dSanish kumar 		return ret;
507805d132dSanish kumar 	}
508805d132dSanish kumar 	return ret;
509805d132dSanish kumar }
510805d132dSanish kumar 
511805d132dSanish kumar static int max9867_i2c_remove(struct i2c_client *client)
512805d132dSanish kumar {
513805d132dSanish kumar 	snd_soc_unregister_codec(&client->dev);
514805d132dSanish kumar 	return 0;
515805d132dSanish kumar }
516805d132dSanish kumar 
517805d132dSanish kumar static const struct i2c_device_id max9867_i2c_id[] = {
518805d132dSanish kumar 	{ "max9867", 0 },
5191b36e4a2SAxel Lin 	{ }
520805d132dSanish kumar };
52156af0e4cSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, max9867_i2c_id);
522805d132dSanish kumar 
523805d132dSanish kumar static const struct of_device_id max9867_of_match[] = {
524805d132dSanish kumar 	{ .compatible = "maxim,max9867", },
525805d132dSanish kumar 	{ }
526805d132dSanish kumar };
52756af0e4cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, max9867_of_match);
528805d132dSanish kumar 
529805d132dSanish kumar static const struct dev_pm_ops max9867_pm_ops = {
530805d132dSanish kumar 	SET_SYSTEM_SLEEP_PM_OPS(max9867_suspend, max9867_resume)
531805d132dSanish kumar };
532805d132dSanish kumar 
533805d132dSanish kumar static struct i2c_driver max9867_i2c_driver = {
534805d132dSanish kumar 	.driver = {
535805d132dSanish kumar 		.name = "max9867",
536805d132dSanish kumar 		.of_match_table = of_match_ptr(max9867_of_match),
537805d132dSanish kumar 		.pm = &max9867_pm_ops,
538805d132dSanish kumar 	},
539805d132dSanish kumar 	.probe  = max9867_i2c_probe,
540805d132dSanish kumar 	.remove = max9867_i2c_remove,
541805d132dSanish kumar 	.id_table = max9867_i2c_id,
542805d132dSanish kumar };
543805d132dSanish kumar 
544805d132dSanish kumar module_i2c_driver(max9867_i2c_driver);
545805d132dSanish kumar 
546805d132dSanish kumar MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
547805d132dSanish kumar MODULE_DESCRIPTION("ALSA SoC MAX9867 driver");
548805d132dSanish kumar MODULE_LICENSE("GPL");
549