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