xref: /openbmc/linux/sound/soc/codecs/max9867.c (revision 0cb4228f)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // MAX9867 ALSA SoC codec driver
4 //
5 // Copyright 2013-2015 Maxim Integrated Products
6 // Copyright 2018 Ladislav Michl <ladis@linux-mips.org>
7 //
8 
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/tlv.h>
16 #include "max9867.h"
17 
18 struct max9867_priv {
19 	struct regmap *regmap;
20 	const struct snd_pcm_hw_constraint_list *constraints;
21 	unsigned int sysclk, pclk;
22 	bool provider, dsp_a;
23 	unsigned int adc_dac_active;
24 };
25 
26 static const char *const max9867_spmode[] = {
27 	"Stereo Diff", "Mono Diff",
28 	"Stereo Cap", "Mono Cap",
29 	"Stereo Single", "Mono Single",
30 	"Stereo Single Fast", "Mono Single Fast"
31 };
32 static const char *const max9867_filter_text[] = {"IIR", "FIR"};
33 
34 static const char *const max9867_adc_dac_filter_text[] = {
35 	"Disabled",
36 	"Elliptical/16/256",
37 	"Butterworth/16/500",
38 	"Elliptical/8/256",
39 	"Butterworth/8/500",
40 	"Butterworth/8-24"
41 };
42 
43 enum max9867_adc_dac {
44 	MAX9867_ADC_LEFT,
45 	MAX9867_ADC_RIGHT,
46 	MAX9867_DAC_LEFT,
47 	MAX9867_DAC_RIGHT,
48 };
49 
50 static int max9867_adc_dac_event(struct snd_soc_dapm_widget *w,
51 	struct snd_kcontrol *kcontrol, int event)
52 {
53 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
54 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
55 	enum max9867_adc_dac adc_dac;
56 
57 	if (!strcmp(w->name, "ADCL"))
58 		adc_dac = MAX9867_ADC_LEFT;
59 	else if (!strcmp(w->name, "ADCR"))
60 		adc_dac = MAX9867_ADC_RIGHT;
61 	else if (!strcmp(w->name, "DACL"))
62 		adc_dac = MAX9867_DAC_LEFT;
63 	else if (!strcmp(w->name, "DACR"))
64 		adc_dac = MAX9867_DAC_RIGHT;
65 	else
66 		return 0;
67 
68 	if (SND_SOC_DAPM_EVENT_ON(event))
69 		max9867->adc_dac_active |= BIT(adc_dac);
70 	else if (SND_SOC_DAPM_EVENT_OFF(event))
71 		max9867->adc_dac_active &= ~BIT(adc_dac);
72 
73 	return 0;
74 }
75 
76 static int max9867_filter_get(struct snd_kcontrol *kcontrol,
77 			      struct snd_ctl_elem_value *ucontrol)
78 {
79 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
80 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
81 	unsigned int reg;
82 	int ret;
83 
84 	ret = regmap_read(max9867->regmap, MAX9867_CODECFLTR, &reg);
85 	if (ret)
86 		return -EINVAL;
87 
88 	if (reg & MAX9867_CODECFLTR_MODE)
89 		ucontrol->value.enumerated.item[0] = 1;
90 	else
91 		ucontrol->value.enumerated.item[0] = 0;
92 
93 	return 0;
94 }
95 
96 static int max9867_filter_set(struct snd_kcontrol *kcontrol,
97 			      struct snd_ctl_elem_value *ucontrol)
98 {
99 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
100 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
101 	unsigned int reg, mode = ucontrol->value.enumerated.item[0];
102 	int ret;
103 
104 	if (mode > 1)
105 		return -EINVAL;
106 
107 	/* don't allow change if ADC/DAC active */
108 	if (max9867->adc_dac_active)
109 		return -EBUSY;
110 
111 	/* read current filter mode */
112 	ret = regmap_read(max9867->regmap, MAX9867_CODECFLTR, &reg);
113 	if (ret)
114 		return -EINVAL;
115 
116 	if (mode)
117 		mode = MAX9867_CODECFLTR_MODE;
118 
119 	/* check if change is needed */
120 	if ((reg & MAX9867_CODECFLTR_MODE) == mode)
121 		return 0;
122 
123 	/* shutdown codec before switching filter mode */
124 	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
125 		MAX9867_PWRMAN_SHDN, 0);
126 
127 	/* switch filter mode */
128 	regmap_update_bits(max9867->regmap, MAX9867_CODECFLTR,
129 		MAX9867_CODECFLTR_MODE, mode);
130 
131 	/* out of shutdown now */
132 	regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
133 		MAX9867_PWRMAN_SHDN, MAX9867_PWRMAN_SHDN);
134 
135 	return 0;
136 }
137 
138 static SOC_ENUM_SINGLE_EXT_DECL(max9867_filter, max9867_filter_text);
139 static SOC_ENUM_SINGLE_DECL(max9867_dac_filter, MAX9867_CODECFLTR, 0,
140 	max9867_adc_dac_filter_text);
141 static SOC_ENUM_SINGLE_DECL(max9867_adc_filter, MAX9867_CODECFLTR, 4,
142 	max9867_adc_dac_filter_text);
143 static SOC_ENUM_SINGLE_DECL(max9867_spkmode, MAX9867_MODECONFIG, 0,
144 	max9867_spmode);
145 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_master_tlv,
146 	 0,  2, TLV_DB_SCALE_ITEM(-8600, 200, 1),
147 	 3, 17, TLV_DB_SCALE_ITEM(-7800, 400, 0),
148 	18, 25, TLV_DB_SCALE_ITEM(-2000, 200, 0),
149 	26, 34, TLV_DB_SCALE_ITEM( -500, 100, 0),
150 	35, 40, TLV_DB_SCALE_ITEM(  350,  50, 0),
151 );
152 static DECLARE_TLV_DB_SCALE(max9867_mic_tlv, 0, 100, 0);
153 static DECLARE_TLV_DB_SCALE(max9867_line_tlv, -600, 200, 0);
154 static DECLARE_TLV_DB_SCALE(max9867_adc_tlv, -1200, 100, 0);
155 static DECLARE_TLV_DB_SCALE(max9867_dac_tlv, -1500, 100, 0);
156 static DECLARE_TLV_DB_SCALE(max9867_dacboost_tlv, 0, 600, 0);
157 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_micboost_tlv,
158 	0, 2, TLV_DB_SCALE_ITEM(-2000, 2000, 1),
159 	3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
160 );
161 
162 static const struct snd_kcontrol_new max9867_snd_controls[] = {
163 	SOC_DOUBLE_R_TLV("Master Playback Volume", MAX9867_LEFTVOL,
164 			MAX9867_RIGHTVOL, 0, 40, 1, max9867_master_tlv),
165 	SOC_DOUBLE_R_TLV("Line Capture Volume", MAX9867_LEFTLINELVL,
166 			MAX9867_RIGHTLINELVL, 0, 15, 1, max9867_line_tlv),
167 	SOC_DOUBLE_R_TLV("Mic Capture Volume", MAX9867_LEFTMICGAIN,
168 			MAX9867_RIGHTMICGAIN, 0, 20, 1, max9867_mic_tlv),
169 	SOC_DOUBLE_R_TLV("Mic Boost Capture Volume", MAX9867_LEFTMICGAIN,
170 			MAX9867_RIGHTMICGAIN, 5, 3, 0, max9867_micboost_tlv),
171 	SOC_SINGLE("Digital Sidetone Volume", MAX9867_SIDETONE, 0, 31, 1),
172 	SOC_SINGLE_TLV("Digital Playback Volume", MAX9867_DACLEVEL, 0, 15, 1,
173 			max9867_dac_tlv),
174 	SOC_SINGLE_TLV("Digital Boost Playback Volume", MAX9867_DACLEVEL, 4, 3, 0,
175 			max9867_dacboost_tlv),
176 	SOC_DOUBLE_TLV("Digital Capture Volume", MAX9867_ADCLEVEL, 4, 0, 15, 1,
177 			max9867_adc_tlv),
178 	SOC_ENUM("Speaker Mode", max9867_spkmode),
179 	SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG, 6, 1, 0),
180 	SOC_SINGLE("Line ZC Switch", MAX9867_MODECONFIG, 5, 1, 0),
181 	SOC_ENUM_EXT("DSP Filter", max9867_filter, max9867_filter_get, max9867_filter_set),
182 	SOC_ENUM("ADC Filter", max9867_adc_filter),
183 	SOC_ENUM("DAC Filter", max9867_dac_filter),
184 	SOC_SINGLE("Mono Playback Switch", MAX9867_IFC1B, 3, 1, 0),
185 };
186 
187 /* Input mixer */
188 static const struct snd_kcontrol_new max9867_input_mixer_controls[] = {
189 	SOC_DAPM_DOUBLE("Line Capture Switch", MAX9867_INPUTCONFIG, 7, 5, 1, 0),
190 	SOC_DAPM_DOUBLE("Mic Capture Switch", MAX9867_INPUTCONFIG, 6, 4, 1, 0),
191 };
192 
193 /* Output mixer */
194 static const struct snd_kcontrol_new max9867_output_mixer_controls[] = {
195 	SOC_DAPM_DOUBLE_R("Line Bypass Switch",
196 			  MAX9867_LEFTLINELVL, MAX9867_RIGHTLINELVL, 6, 1, 1),
197 };
198 
199 /* Sidetone mixer */
200 static const struct snd_kcontrol_new max9867_sidetone_mixer_controls[] = {
201 	SOC_DAPM_DOUBLE("Sidetone Switch", MAX9867_SIDETONE, 6, 7, 1, 0),
202 };
203 
204 /* Line out switch */
205 static const struct snd_kcontrol_new max9867_line_out_control =
206 	SOC_DAPM_DOUBLE_R("Switch",
207 			  MAX9867_LEFTVOL, MAX9867_RIGHTVOL, 6, 1, 1);
208 
209 /* DMIC mux */
210 static const char *const dmic_mux_text[] = {
211 	"ADC", "DMIC"
212 };
213 static SOC_ENUM_SINGLE_DECL(left_dmic_mux_enum,
214 			    MAX9867_MICCONFIG, 5, dmic_mux_text);
215 static SOC_ENUM_SINGLE_DECL(right_dmic_mux_enum,
216 			    MAX9867_MICCONFIG, 4, dmic_mux_text);
217 static const struct snd_kcontrol_new max9867_left_dmic_mux =
218 	SOC_DAPM_ENUM("DMICL Mux", left_dmic_mux_enum);
219 static const struct snd_kcontrol_new max9867_right_dmic_mux =
220 	SOC_DAPM_ENUM("DMICR Mux", right_dmic_mux_enum);
221 
222 static const struct snd_soc_dapm_widget max9867_dapm_widgets[] = {
223 	SND_SOC_DAPM_INPUT("MICL"),
224 	SND_SOC_DAPM_INPUT("MICR"),
225 	SND_SOC_DAPM_INPUT("DMICL"),
226 	SND_SOC_DAPM_INPUT("DMICR"),
227 	SND_SOC_DAPM_INPUT("LINL"),
228 	SND_SOC_DAPM_INPUT("LINR"),
229 
230 	SND_SOC_DAPM_PGA("Left Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
231 	SND_SOC_DAPM_PGA("Right Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
232 	SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM, 0, 0,
233 				     max9867_input_mixer_controls,
234 				     ARRAY_SIZE(max9867_input_mixer_controls)),
235 	SND_SOC_DAPM_MUX("DMICL Mux", SND_SOC_NOPM, 0, 0,
236 			 &max9867_left_dmic_mux),
237 	SND_SOC_DAPM_MUX("DMICR Mux", SND_SOC_NOPM, 0, 0,
238 			 &max9867_right_dmic_mux),
239 	SND_SOC_DAPM_ADC_E("ADCL", "HiFi Capture", SND_SOC_NOPM, 0, 0,
240 			   max9867_adc_dac_event,
241 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
242 	SND_SOC_DAPM_ADC_E("ADCR", "HiFi Capture", SND_SOC_NOPM, 0, 0,
243 			   max9867_adc_dac_event,
244 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
245 
246 	SND_SOC_DAPM_MIXER("Digital", SND_SOC_NOPM, 0, 0,
247 			   max9867_sidetone_mixer_controls,
248 			   ARRAY_SIZE(max9867_sidetone_mixer_controls)),
249 	SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", SND_SOC_NOPM, 0, 0,
250 				     max9867_output_mixer_controls,
251 				     ARRAY_SIZE(max9867_output_mixer_controls)),
252 	SND_SOC_DAPM_DAC_E("DACL", "HiFi Playback", SND_SOC_NOPM, 0, 0,
253 			   max9867_adc_dac_event,
254 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
255 	SND_SOC_DAPM_DAC_E("DACR", "HiFi Playback", SND_SOC_NOPM, 0, 0,
256 			   max9867_adc_dac_event,
257 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
258 	SND_SOC_DAPM_SWITCH("Master Playback", SND_SOC_NOPM, 0, 0,
259 			    &max9867_line_out_control),
260 	SND_SOC_DAPM_OUTPUT("LOUT"),
261 	SND_SOC_DAPM_OUTPUT("ROUT"),
262 };
263 
264 static const struct snd_soc_dapm_route max9867_audio_map[] = {
265 	{"Left Line Input", NULL, "LINL"},
266 	{"Right Line Input", NULL, "LINR"},
267 	{"Input Mixer", "Mic Capture Switch", "MICL"},
268 	{"Input Mixer", "Mic Capture Switch", "MICR"},
269 	{"Input Mixer", "Line Capture Switch", "Left Line Input"},
270 	{"Input Mixer", "Line Capture Switch", "Right Line Input"},
271 	{"DMICL Mux", "DMIC", "DMICL"},
272 	{"DMICR Mux", "DMIC", "DMICR"},
273 	{"DMICL Mux", "ADC", "Input Mixer"},
274 	{"DMICR Mux", "ADC", "Input Mixer"},
275 	{"ADCL", NULL, "DMICL Mux"},
276 	{"ADCR", NULL, "DMICR Mux"},
277 
278 	{"Digital", "Sidetone Switch", "ADCL"},
279 	{"Digital", "Sidetone Switch", "ADCR"},
280 	{"DACL", NULL, "Digital"},
281 	{"DACR", NULL, "Digital"},
282 
283 	{"Output Mixer", "Line Bypass Switch", "Left Line Input"},
284 	{"Output Mixer", "Line Bypass Switch", "Right Line Input"},
285 	{"Output Mixer", NULL, "DACL"},
286 	{"Output Mixer", NULL, "DACR"},
287 	{"Master Playback", "Switch", "Output Mixer"},
288 	{"LOUT", NULL, "Master Playback"},
289 	{"ROUT", NULL, "Master Playback"},
290 };
291 
292 static const unsigned int max9867_rates_44k1[] = {
293 	11025, 22050, 44100,
294 };
295 
296 static const struct snd_pcm_hw_constraint_list max9867_constraints_44k1 = {
297 	.list = max9867_rates_44k1,
298 	.count = ARRAY_SIZE(max9867_rates_44k1),
299 };
300 
301 static const unsigned int max9867_rates_48k[] = {
302 	8000, 16000, 32000, 48000,
303 };
304 
305 static const struct snd_pcm_hw_constraint_list max9867_constraints_48k = {
306 	.list = max9867_rates_48k,
307 	.count = ARRAY_SIZE(max9867_rates_48k),
308 };
309 
310 static int max9867_startup(struct snd_pcm_substream *substream,
311 			   struct snd_soc_dai *dai)
312 {
313         struct max9867_priv *max9867 =
314 		snd_soc_component_get_drvdata(dai->component);
315 
316 	if (max9867->constraints)
317 		snd_pcm_hw_constraint_list(substream->runtime, 0,
318 			SNDRV_PCM_HW_PARAM_RATE, max9867->constraints);
319 
320 	return 0;
321 }
322 
323 static int max9867_dai_hw_params(struct snd_pcm_substream *substream,
324 		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
325 {
326 	int value, freq = 0;
327 	unsigned long int rate, ratio;
328 	struct snd_soc_component *component = dai->component;
329 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
330 	unsigned int ni = DIV_ROUND_CLOSEST_ULL(96ULL * 0x10000 * params_rate(params),
331 						max9867->pclk);
332 
333 	/* set up the ni value */
334 	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
335 		MAX9867_NI_HIGH_MASK, (0xFF00 & ni) >> 8);
336 	regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
337 		MAX9867_NI_LOW_MASK, 0x00FF & ni);
338 	if (max9867->provider) {
339 		if (max9867->dsp_a) {
340 			value = MAX9867_IFC1B_48X;
341 		} else {
342 			rate = params_rate(params) * 2 * params_width(params);
343 			ratio = max9867->pclk / rate;
344 			switch (params_width(params)) {
345 			case 8:
346 			case 16:
347 				switch (ratio) {
348 				case 2:
349 					value = MAX9867_IFC1B_PCLK_2;
350 					break;
351 				case 4:
352 					value = MAX9867_IFC1B_PCLK_4;
353 					break;
354 				case 8:
355 					value = MAX9867_IFC1B_PCLK_8;
356 					break;
357 				case 16:
358 					value = MAX9867_IFC1B_PCLK_16;
359 					break;
360 				default:
361 					return -EINVAL;
362 				}
363 				break;
364 			case 24:
365 				value = MAX9867_IFC1B_48X;
366 				break;
367 			case 32:
368 				value = MAX9867_IFC1B_64X;
369 				break;
370 			default:
371 				return -EINVAL;
372 			}
373 		}
374 		regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
375 			MAX9867_IFC1B_BCLK_MASK, value);
376 
377 		/* Exact integer mode available for 8kHz and 16kHz sample rates
378 		 * and certain PCLK (prescaled MCLK) values.
379 		 */
380 		if (params_rate(params) == 8000 ||
381 		    params_rate(params) == 16000) {
382 			switch (max9867->pclk) {
383 			case 12000000:
384 				freq = 0x08;
385 				break;
386 			case 13000000:
387 				freq = 0x0A;
388 				break;
389 			case 16000000:
390 				freq = 0x0C;
391 				break;
392 			case 19200000:
393 				freq = 0x0E;
394 				break;
395 			}
396 		}
397 		if (freq && params_rate(params) == 16000)
398 			freq++;
399 
400 		/* If exact integer mode not available, the freq value
401 		 * remains zero, i.e. normal mode is used.
402 		 */
403 		regmap_update_bits(max9867->regmap, MAX9867_SYSCLK,
404 				   MAX9867_FREQ_MASK, freq);
405 	} else {
406 		/*
407 		 * digital pll locks on to any externally supplied LRCLK signal
408 		 * and also enable rapid lock mode.
409 		 */
410 		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
411 			MAX9867_RAPID_LOCK, MAX9867_RAPID_LOCK);
412 		regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
413 			MAX9867_PLL, MAX9867_PLL);
414 	}
415 	return 0;
416 }
417 
418 static int max9867_mute(struct snd_soc_dai *dai, int mute, int direction)
419 {
420 	struct snd_soc_component *component = dai->component;
421 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
422 
423 	return regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
424 				  1 << 6, !!mute << 6);
425 }
426 
427 static int max9867_set_dai_sysclk(struct snd_soc_dai *codec_dai,
428 		int clk_id, unsigned int freq, int dir)
429 {
430 	struct snd_soc_component *component = codec_dai->component;
431 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
432 	int value = 0;
433 
434 	/* Set the prescaler based on the master clock frequency*/
435 	if (freq >= 10000000 && freq <= 20000000) {
436 		value |= MAX9867_PSCLK_10_20;
437 		max9867->pclk = freq;
438 	} else if (freq >= 20000000 && freq <= 40000000) {
439 		value |= MAX9867_PSCLK_20_40;
440 		max9867->pclk = freq / 2;
441 	} else if (freq >= 40000000 && freq <= 60000000) {
442 		value |= MAX9867_PSCLK_40_60;
443 		max9867->pclk = freq / 4;
444 	} else {
445 		dev_err(component->dev,
446 			"Invalid clock frequency %uHz (required 10-60MHz)\n",
447 			freq);
448 		return -EINVAL;
449 	}
450 	if (freq % 48000 == 0)
451 		max9867->constraints = &max9867_constraints_48k;
452 	else if (freq % 44100 == 0)
453 		max9867->constraints = &max9867_constraints_44k1;
454 	else
455 		dev_warn(component->dev,
456 			 "Unable to set exact rate with %uHz clock frequency\n",
457 			 freq);
458 	max9867->sysclk = freq;
459 	value = value << MAX9867_PSCLK_SHIFT;
460 	regmap_update_bits(max9867->regmap, MAX9867_SYSCLK,
461 			MAX9867_PSCLK_MASK, value);
462 	return 0;
463 }
464 
465 static int max9867_dai_set_fmt(struct snd_soc_dai *codec_dai,
466 		unsigned int fmt)
467 {
468 	struct snd_soc_component *component = codec_dai->component;
469 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
470 	u8 iface1A, iface1B;
471 
472 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
473 	case SND_SOC_DAIFMT_CBP_CFP:
474 		max9867->provider = true;
475 		iface1A = MAX9867_MASTER;
476 		iface1B = MAX9867_IFC1B_48X;
477 		break;
478 	case SND_SOC_DAIFMT_CBC_CFC:
479 		max9867->provider = false;
480 		iface1A = iface1B = 0;
481 		break;
482 	default:
483 		return -EINVAL;
484 	}
485 
486 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
487 	case SND_SOC_DAIFMT_I2S:
488 		max9867->dsp_a = false;
489 		iface1A |= MAX9867_I2S_DLY;
490 		break;
491 	case SND_SOC_DAIFMT_DSP_A:
492 		max9867->dsp_a = true;
493 		iface1A |= MAX9867_TDM_MODE | MAX9867_SDOUT_HIZ;
494 		break;
495 	default:
496 		return -EINVAL;
497 	}
498 
499 	/* Clock inversion bits, BCI and WCI */
500 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
501 	case SND_SOC_DAIFMT_NB_NF:
502 		break;
503 	case SND_SOC_DAIFMT_IB_IF:
504 		iface1A |= MAX9867_WCI_MODE | MAX9867_BCI_MODE;
505 		break;
506 	case SND_SOC_DAIFMT_IB_NF:
507 		iface1A |= MAX9867_BCI_MODE;
508 		break;
509 	case SND_SOC_DAIFMT_NB_IF:
510 		iface1A |= MAX9867_WCI_MODE;
511 		break;
512 	default:
513 		return -EINVAL;
514 	}
515 
516 	regmap_write(max9867->regmap, MAX9867_IFC1A, iface1A);
517 	regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
518 			   MAX9867_IFC1B_BCLK_MASK, iface1B);
519 
520 	return 0;
521 }
522 
523 static const struct snd_soc_dai_ops max9867_dai_ops = {
524 	.set_sysclk	= max9867_set_dai_sysclk,
525 	.set_fmt	= max9867_dai_set_fmt,
526 	.mute_stream	= max9867_mute,
527 	.startup	= max9867_startup,
528 	.hw_params	= max9867_dai_hw_params,
529 	.no_capture_mute = 1,
530 };
531 
532 static struct snd_soc_dai_driver max9867_dai[] = {
533 	{
534 	.name = "max9867-aif1",
535 	.playback = {
536 		.stream_name = "HiFi Playback",
537 		.channels_min = 2,
538 		.channels_max = 2,
539 		.rates = SNDRV_PCM_RATE_8000_48000,
540 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
541 	},
542 	.capture = {
543 		.stream_name = "HiFi Capture",
544 		.channels_min = 2,
545 		.channels_max = 2,
546 		.rates = SNDRV_PCM_RATE_8000_48000,
547 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
548 	},
549 	.ops = &max9867_dai_ops,
550 	.symmetric_rate = 1,
551 	}
552 };
553 
554 #ifdef CONFIG_PM
555 static int max9867_suspend(struct snd_soc_component *component)
556 {
557 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
558 
559 	return 0;
560 }
561 
562 static int max9867_resume(struct snd_soc_component *component)
563 {
564 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
565 
566 	return 0;
567 }
568 #else
569 #define max9867_suspend	NULL
570 #define max9867_resume	NULL
571 #endif
572 
573 static int max9867_set_bias_level(struct snd_soc_component *component,
574 				  enum snd_soc_bias_level level)
575 {
576 	int err;
577 	struct max9867_priv *max9867 = snd_soc_component_get_drvdata(component);
578 
579 	switch (level) {
580 	case SND_SOC_BIAS_STANDBY:
581 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
582 			err = regcache_sync(max9867->regmap);
583 			if (err)
584 				return err;
585 
586 			err = regmap_write(max9867->regmap,
587 					   MAX9867_PWRMAN, 0xff);
588 			if (err)
589 				return err;
590 		}
591 		break;
592 	case SND_SOC_BIAS_OFF:
593 		err = regmap_write(max9867->regmap, MAX9867_PWRMAN, 0);
594 		if (err)
595 			return err;
596 
597 		regcache_mark_dirty(max9867->regmap);
598 		break;
599 	default:
600 		break;
601 	}
602 
603 	return 0;
604 }
605 
606 static const struct snd_soc_component_driver max9867_component = {
607 	.controls		= max9867_snd_controls,
608 	.num_controls		= ARRAY_SIZE(max9867_snd_controls),
609 	.dapm_routes		= max9867_audio_map,
610 	.num_dapm_routes	= ARRAY_SIZE(max9867_audio_map),
611 	.dapm_widgets		= max9867_dapm_widgets,
612 	.num_dapm_widgets	= ARRAY_SIZE(max9867_dapm_widgets),
613 	.suspend		= max9867_suspend,
614 	.resume			= max9867_resume,
615 	.set_bias_level		= max9867_set_bias_level,
616 	.idle_bias_on		= 1,
617 	.use_pmdown_time	= 1,
618 	.endianness		= 1,
619 };
620 
621 static bool max9867_volatile_register(struct device *dev, unsigned int reg)
622 {
623 	switch (reg) {
624 	case MAX9867_STATUS:
625 	case MAX9867_JACKSTATUS:
626 	case MAX9867_AUXHIGH:
627 	case MAX9867_AUXLOW:
628 		return true;
629 	default:
630 		return false;
631 	}
632 }
633 
634 static const struct regmap_config max9867_regmap = {
635 	.reg_bits	= 8,
636 	.val_bits	= 8,
637 	.max_register	= MAX9867_REVISION,
638 	.volatile_reg	= max9867_volatile_register,
639 	.cache_type	= REGCACHE_RBTREE,
640 };
641 
642 static int max9867_i2c_probe(struct i2c_client *i2c)
643 {
644 	struct max9867_priv *max9867;
645 	int ret, reg;
646 
647 	max9867 = devm_kzalloc(&i2c->dev, sizeof(*max9867), GFP_KERNEL);
648 	if (!max9867)
649 		return -ENOMEM;
650 
651 	i2c_set_clientdata(i2c, max9867);
652 	max9867->regmap = devm_regmap_init_i2c(i2c, &max9867_regmap);
653 	if (IS_ERR(max9867->regmap)) {
654 		ret = PTR_ERR(max9867->regmap);
655 		dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
656 		return ret;
657 	}
658 	ret = regmap_read(max9867->regmap, MAX9867_REVISION, &reg);
659 	if (ret < 0) {
660 		dev_err(&i2c->dev, "Failed to read: %d\n", ret);
661 		return ret;
662 	}
663 	dev_info(&i2c->dev, "device revision: %x\n", reg);
664 	ret = devm_snd_soc_register_component(&i2c->dev, &max9867_component,
665 			max9867_dai, ARRAY_SIZE(max9867_dai));
666 	if (ret < 0)
667 		dev_err(&i2c->dev, "Failed to register component: %d\n", ret);
668 	return ret;
669 }
670 
671 static const struct i2c_device_id max9867_i2c_id[] = {
672 	{ "max9867", 0 },
673 	{ }
674 };
675 MODULE_DEVICE_TABLE(i2c, max9867_i2c_id);
676 
677 #ifdef CONFIG_OF
678 static const struct of_device_id max9867_of_match[] = {
679 	{ .compatible = "maxim,max9867", },
680 	{ }
681 };
682 MODULE_DEVICE_TABLE(of, max9867_of_match);
683 #endif
684 
685 static struct i2c_driver max9867_i2c_driver = {
686 	.driver = {
687 		.name = "max9867",
688 		.of_match_table = of_match_ptr(max9867_of_match),
689 	},
690 	.probe_new  = max9867_i2c_probe,
691 	.id_table = max9867_i2c_id,
692 };
693 
694 module_i2c_driver(max9867_i2c_driver);
695 
696 MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
697 MODULE_DESCRIPTION("ASoC MAX9867 driver");
698 MODULE_LICENSE("GPL");
699