xref: /linux/sound/soc/codecs/max98371.c (revision 856e7c4b619af622d56b3b454f7bec32a170ac99)
1 /*
2  * max98371.c -- ALSA SoC Stereo MAX98371 driver
3  *
4  * Copyright 2015-16 Maxim Integrated Products
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/tlv.h>
19 #include "max98371.h"
20 
21 static const char *const monomix_text[] = {
22 	"Left", "Right", "LeftRightDiv2",
23 };
24 
25 static const char *const hpf_cutoff_txt[] = {
26 	"Disable", "DC Block", "50Hz",
27 	"100Hz", "200Hz", "400Hz", "800Hz",
28 };
29 
30 static SOC_ENUM_SINGLE_DECL(max98371_monomix, MAX98371_MONOMIX_CFG, 0,
31 		monomix_text);
32 
33 static SOC_ENUM_SINGLE_DECL(max98371_hpf_cutoff, MAX98371_HPF, 0,
34 		hpf_cutoff_txt);
35 
36 static const DECLARE_TLV_DB_RANGE(max98371_dht_min_gain,
37 	0, 1, TLV_DB_SCALE_ITEM(537, 66, 0),
38 	2, 3, TLV_DB_SCALE_ITEM(677, 82, 0),
39 	4, 5, TLV_DB_SCALE_ITEM(852, 104, 0),
40 	6, 7, TLV_DB_SCALE_ITEM(1072, 131, 0),
41 	8, 9, TLV_DB_SCALE_ITEM(1350, 165, 0),
42 	10, 11, TLV_DB_SCALE_ITEM(1699, 101, 0),
43 );
44 
45 static const DECLARE_TLV_DB_RANGE(max98371_dht_max_gain,
46 	0, 1, TLV_DB_SCALE_ITEM(537, 66, 0),
47 	2, 3, TLV_DB_SCALE_ITEM(677, 82, 0),
48 	4, 5, TLV_DB_SCALE_ITEM(852, 104, 0),
49 	6, 7, TLV_DB_SCALE_ITEM(1072, 131, 0),
50 	8, 9, TLV_DB_SCALE_ITEM(1350, 165, 0),
51 	10, 11, TLV_DB_SCALE_ITEM(1699, 208, 0),
52 );
53 
54 static const DECLARE_TLV_DB_RANGE(max98371_dht_rot_gain,
55 	0, 1, TLV_DB_SCALE_ITEM(-50, -50, 0),
56 	2, 6, TLV_DB_SCALE_ITEM(-100, -100, 0),
57 	7, 8, TLV_DB_SCALE_ITEM(-800, -200, 0),
58 	9, 11, TLV_DB_SCALE_ITEM(-1200, -300, 0),
59 	12, 13, TLV_DB_SCALE_ITEM(-2000, -200, 0),
60 	14, 15, TLV_DB_SCALE_ITEM(-2500, -500, 0),
61 );
62 
63 static const struct reg_default max98371_reg[] = {
64 	{ 0x01, 0x00 },
65 	{ 0x02, 0x00 },
66 	{ 0x03, 0x00 },
67 	{ 0x04, 0x00 },
68 	{ 0x05, 0x00 },
69 	{ 0x06, 0x00 },
70 	{ 0x07, 0x00 },
71 	{ 0x08, 0x00 },
72 	{ 0x09, 0x00 },
73 	{ 0x0A, 0x00 },
74 	{ 0x10, 0x06 },
75 	{ 0x11, 0x08 },
76 	{ 0x14, 0x80 },
77 	{ 0x15, 0x00 },
78 	{ 0x16, 0x00 },
79 	{ 0x18, 0x00 },
80 	{ 0x19, 0x00 },
81 	{ 0x1C, 0x00 },
82 	{ 0x1D, 0x00 },
83 	{ 0x1E, 0x00 },
84 	{ 0x1F, 0x00 },
85 	{ 0x20, 0x00 },
86 	{ 0x21, 0x00 },
87 	{ 0x22, 0x00 },
88 	{ 0x23, 0x00 },
89 	{ 0x24, 0x00 },
90 	{ 0x25, 0x00 },
91 	{ 0x26, 0x00 },
92 	{ 0x27, 0x00 },
93 	{ 0x28, 0x00 },
94 	{ 0x29, 0x00 },
95 	{ 0x2A, 0x00 },
96 	{ 0x2B, 0x00 },
97 	{ 0x2C, 0x00 },
98 	{ 0x2D, 0x00 },
99 	{ 0x2E, 0x0B },
100 	{ 0x31, 0x00 },
101 	{ 0x32, 0x18 },
102 	{ 0x33, 0x00 },
103 	{ 0x34, 0x00 },
104 	{ 0x36, 0x00 },
105 	{ 0x37, 0x00 },
106 	{ 0x38, 0x00 },
107 	{ 0x39, 0x00 },
108 	{ 0x3A, 0x00 },
109 	{ 0x3B, 0x00 },
110 	{ 0x3C, 0x00 },
111 	{ 0x3D, 0x00 },
112 	{ 0x3E, 0x00 },
113 	{ 0x3F, 0x00 },
114 	{ 0x40, 0x00 },
115 	{ 0x41, 0x00 },
116 	{ 0x42, 0x00 },
117 	{ 0x43, 0x00 },
118 	{ 0x4A, 0x00 },
119 	{ 0x4B, 0x00 },
120 	{ 0x4C, 0x00 },
121 	{ 0x4D, 0x00 },
122 	{ 0x4E, 0x00 },
123 	{ 0x50, 0x00 },
124 	{ 0x51, 0x00 },
125 	{ 0x55, 0x00 },
126 	{ 0x58, 0x00 },
127 	{ 0x59, 0x00 },
128 	{ 0x5C, 0x00 },
129 	{ 0xFF, 0x43 },
130 };
131 
132 static bool max98371_volatile_register(struct device *dev, unsigned int reg)
133 {
134 	switch (reg) {
135 	case MAX98371_IRQ_CLEAR1:
136 	case MAX98371_IRQ_CLEAR2:
137 	case MAX98371_IRQ_CLEAR3:
138 	case MAX98371_VERSION:
139 		return true;
140 	default:
141 		return false;
142 	}
143 }
144 
145 static bool max98371_readable_register(struct device *dev, unsigned int reg)
146 {
147 	switch (reg) {
148 	case MAX98371_SOFT_RESET:
149 		return false;
150 	default:
151 		return true;
152 	}
153 };
154 
155 static const DECLARE_TLV_DB_RANGE(max98371_gain_tlv,
156 	0, 7, TLV_DB_SCALE_ITEM(0, 50, 0),
157 	8, 10, TLV_DB_SCALE_ITEM(400, 100, 0)
158 );
159 
160 static const DECLARE_TLV_DB_RANGE(max98371_noload_gain_tlv,
161 	0, 11, TLV_DB_SCALE_ITEM(950, 100, 0),
162 );
163 
164 static const DECLARE_TLV_DB_SCALE(digital_tlv, -6300, 50, 1);
165 
166 static const struct snd_kcontrol_new max98371_snd_controls[] = {
167 	SOC_SINGLE_TLV("Speaker Volume", MAX98371_GAIN,
168 			MAX98371_GAIN_SHIFT, (1<<MAX98371_GAIN_WIDTH)-1, 0,
169 			max98371_gain_tlv),
170 	SOC_SINGLE_TLV("Digital Volume", MAX98371_DIGITAL_GAIN, 0,
171 			(1<<MAX98371_DIGITAL_GAIN_WIDTH)-1, 1, digital_tlv),
172 	SOC_SINGLE_TLV("Speaker DHT Max Volume", MAX98371_GAIN,
173 			0, (1<<MAX98371_DHT_MAX_WIDTH)-1, 0,
174 			max98371_dht_max_gain),
175 	SOC_SINGLE_TLV("Speaker DHT Min Volume", MAX98371_DHT_GAIN,
176 			0, (1<<MAX98371_DHT_GAIN_WIDTH)-1, 0,
177 			max98371_dht_min_gain),
178 	SOC_SINGLE_TLV("Speaker DHT Rotation Volume", MAX98371_DHT_GAIN,
179 			0, (1<<MAX98371_DHT_ROT_WIDTH)-1, 0,
180 			max98371_dht_rot_gain),
181 	SOC_SINGLE("DHT Attack Step", MAX98371_DHT, MAX98371_DHT_STEP, 3, 0),
182 	SOC_SINGLE("DHT Attack Rate", MAX98371_DHT, 0, 7, 0),
183 	SOC_ENUM("Monomix Select", max98371_monomix),
184 	SOC_ENUM("HPF Cutoff", max98371_hpf_cutoff),
185 };
186 
187 static int max98371_dai_set_fmt(struct snd_soc_dai *codec_dai,
188 		unsigned int fmt)
189 {
190 	struct snd_soc_component *component = codec_dai->component;
191 	struct max98371_priv *max98371 = snd_soc_component_get_drvdata(component);
192 	unsigned int val = 0;
193 
194 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
195 	case SND_SOC_DAIFMT_CBS_CFS:
196 		break;
197 	default:
198 		dev_err(component->dev, "DAI clock mode unsupported");
199 		return -EINVAL;
200 	}
201 
202 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
203 	case SND_SOC_DAIFMT_I2S:
204 		val |= 0;
205 		break;
206 	case SND_SOC_DAIFMT_RIGHT_J:
207 		val |= MAX98371_DAI_RIGHT;
208 		break;
209 	case SND_SOC_DAIFMT_LEFT_J:
210 		val |= MAX98371_DAI_LEFT;
211 		break;
212 	default:
213 		dev_err(component->dev, "DAI wrong mode unsupported");
214 		return -EINVAL;
215 	}
216 	regmap_update_bits(max98371->regmap, MAX98371_FMT,
217 			MAX98371_FMT_MODE_MASK, val);
218 	return 0;
219 }
220 
221 static int max98371_dai_hw_params(struct snd_pcm_substream *substream,
222 		struct snd_pcm_hw_params *params,
223 		struct snd_soc_dai *dai)
224 {
225 	struct snd_soc_component *component = dai->component;
226 	struct max98371_priv *max98371 = snd_soc_component_get_drvdata(component);
227 	int blr_clk_ratio, ch_size, channels = params_channels(params);
228 	int rate = params_rate(params);
229 
230 	switch (params_format(params)) {
231 	case SNDRV_PCM_FORMAT_S8:
232 		regmap_update_bits(max98371->regmap, MAX98371_FMT,
233 				MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_16);
234 		ch_size = 8;
235 		break;
236 	case SNDRV_PCM_FORMAT_S16_LE:
237 		regmap_update_bits(max98371->regmap, MAX98371_FMT,
238 				MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_16);
239 		ch_size = 16;
240 		break;
241 	case SNDRV_PCM_FORMAT_S24_LE:
242 		regmap_update_bits(max98371->regmap, MAX98371_FMT,
243 				MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_32);
244 		ch_size = 24;
245 		break;
246 	case SNDRV_PCM_FORMAT_S32_LE:
247 		regmap_update_bits(max98371->regmap, MAX98371_FMT,
248 				MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_32);
249 		ch_size = 32;
250 		break;
251 	default:
252 		return -EINVAL;
253 	}
254 
255 	/* BCLK/LRCLK ratio calculation */
256 	blr_clk_ratio = channels * ch_size;
257 	switch (blr_clk_ratio) {
258 	case 32:
259 		regmap_update_bits(max98371->regmap,
260 			MAX98371_DAI_CLK,
261 			MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_32);
262 		break;
263 	case 48:
264 		regmap_update_bits(max98371->regmap,
265 			MAX98371_DAI_CLK,
266 			MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_48);
267 		break;
268 	case 64:
269 		regmap_update_bits(max98371->regmap,
270 			MAX98371_DAI_CLK,
271 			MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_64);
272 		break;
273 	default:
274 		return -EINVAL;
275 	}
276 
277 	switch (rate) {
278 	case 32000:
279 		regmap_update_bits(max98371->regmap,
280 			MAX98371_SPK_SR,
281 			MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_32);
282 		break;
283 	case 44100:
284 		regmap_update_bits(max98371->regmap,
285 			MAX98371_SPK_SR,
286 			MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_44);
287 		break;
288 	case 48000:
289 		regmap_update_bits(max98371->regmap,
290 			MAX98371_SPK_SR,
291 			MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_48);
292 		break;
293 	case 88200:
294 		regmap_update_bits(max98371->regmap,
295 			MAX98371_SPK_SR,
296 			MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_88);
297 		break;
298 	case 96000:
299 		regmap_update_bits(max98371->regmap,
300 			MAX98371_SPK_SR,
301 			MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_96);
302 		break;
303 	default:
304 		return -EINVAL;
305 	}
306 
307 	/* enabling both the RX channels*/
308 	regmap_update_bits(max98371->regmap, MAX98371_MONOMIX_SRC,
309 			MAX98371_MONOMIX_SRC_MASK, MONOMIX_RX_0_1);
310 	regmap_update_bits(max98371->regmap, MAX98371_DAI_CHANNEL,
311 			MAX98371_CHANNEL_MASK, MAX98371_CHANNEL_MASK);
312 	return 0;
313 }
314 
315 static const struct snd_soc_dapm_widget max98371_dapm_widgets[] = {
316 	SND_SOC_DAPM_DAC("DAC", NULL, MAX98371_SPK_ENABLE, 0, 0),
317 	SND_SOC_DAPM_SUPPLY("Global Enable", MAX98371_GLOBAL_ENABLE,
318 		0, 0, NULL, 0),
319 	SND_SOC_DAPM_OUTPUT("SPK_OUT"),
320 };
321 
322 static const struct snd_soc_dapm_route max98371_audio_map[] = {
323 	{"DAC", NULL, "HiFi Playback"},
324 	{"SPK_OUT", NULL, "DAC"},
325 	{"SPK_OUT", NULL, "Global Enable"},
326 };
327 
328 #define MAX98371_RATES SNDRV_PCM_RATE_8000_48000
329 #define MAX98371_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
330 		SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
331 
332 static const struct snd_soc_dai_ops max98371_dai_ops = {
333 	.set_fmt = max98371_dai_set_fmt,
334 	.hw_params = max98371_dai_hw_params,
335 };
336 
337 static struct snd_soc_dai_driver max98371_dai[] = {
338 	{
339 		.name = "max98371-aif1",
340 		.playback = {
341 			.stream_name = "HiFi Playback",
342 			.channels_min = 1,
343 			.channels_max = 2,
344 			.rates = SNDRV_PCM_RATE_8000_48000,
345 			.formats = MAX98371_FORMATS,
346 		},
347 		.ops = &max98371_dai_ops,
348 	}
349 };
350 
351 static const struct snd_soc_component_driver max98371_component = {
352 	.controls		= max98371_snd_controls,
353 	.num_controls		= ARRAY_SIZE(max98371_snd_controls),
354 	.dapm_routes		= max98371_audio_map,
355 	.num_dapm_routes	= ARRAY_SIZE(max98371_audio_map),
356 	.dapm_widgets		= max98371_dapm_widgets,
357 	.num_dapm_widgets	= ARRAY_SIZE(max98371_dapm_widgets),
358 	.idle_bias_on		= 1,
359 	.use_pmdown_time	= 1,
360 	.endianness		= 1,
361 	.non_legacy_dai_naming	= 1,
362 };
363 
364 static const struct regmap_config max98371_regmap = {
365 	.reg_bits         = 8,
366 	.val_bits         = 8,
367 	.max_register     = MAX98371_VERSION,
368 	.reg_defaults     = max98371_reg,
369 	.num_reg_defaults = ARRAY_SIZE(max98371_reg),
370 	.volatile_reg     = max98371_volatile_register,
371 	.readable_reg     = max98371_readable_register,
372 	.cache_type       = REGCACHE_RBTREE,
373 };
374 
375 static int max98371_i2c_probe(struct i2c_client *i2c,
376 		const struct i2c_device_id *id)
377 {
378 	struct max98371_priv *max98371;
379 	int ret, reg;
380 
381 	max98371 = devm_kzalloc(&i2c->dev,
382 			sizeof(*max98371), GFP_KERNEL);
383 	if (!max98371)
384 		return -ENOMEM;
385 
386 	i2c_set_clientdata(i2c, max98371);
387 	max98371->regmap = devm_regmap_init_i2c(i2c, &max98371_regmap);
388 	if (IS_ERR(max98371->regmap)) {
389 		ret = PTR_ERR(max98371->regmap);
390 		dev_err(&i2c->dev,
391 				"Failed to allocate regmap: %d\n", ret);
392 		return ret;
393 	}
394 
395 	ret = regmap_read(max98371->regmap, MAX98371_VERSION, &reg);
396 	if (ret < 0) {
397 		dev_info(&i2c->dev, "device error %d\n", ret);
398 		return ret;
399 	}
400 	dev_info(&i2c->dev, "device version %x\n", reg);
401 
402 	ret = devm_snd_soc_register_component(&i2c->dev, &max98371_component,
403 			max98371_dai, ARRAY_SIZE(max98371_dai));
404 	if (ret < 0) {
405 		dev_err(&i2c->dev, "Failed to register component: %d\n", ret);
406 		return ret;
407 	}
408 	return ret;
409 }
410 
411 static const struct i2c_device_id max98371_i2c_id[] = {
412 	{ "max98371", 0 },
413 	{ }
414 };
415 
416 MODULE_DEVICE_TABLE(i2c, max98371_i2c_id);
417 
418 static const struct of_device_id max98371_of_match[] = {
419 	{ .compatible = "maxim,max98371", },
420 	{ }
421 };
422 MODULE_DEVICE_TABLE(of, max98371_of_match);
423 
424 static struct i2c_driver max98371_i2c_driver = {
425 	.driver = {
426 		.name = "max98371",
427 		.pm = NULL,
428 		.of_match_table = of_match_ptr(max98371_of_match),
429 	},
430 	.probe  = max98371_i2c_probe,
431 	.id_table = max98371_i2c_id,
432 };
433 
434 module_i2c_driver(max98371_i2c_driver);
435 
436 MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
437 MODULE_DESCRIPTION("ALSA SoC MAX98371 driver");
438 MODULE_LICENSE("GPL");
439