2 * max98371.c -- ALSA SoC Stereo MAX98371 driver
4 * Copyright 2015-16 Maxim Integrated Products
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.
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>
21 static const char *const monomix_text
[] = {
22 "Left", "Right", "LeftRightDiv2",
25 static const char *const hpf_cutoff_txt
[] = {
26 "Disable", "DC Block", "50Hz",
27 "100Hz", "200Hz", "400Hz", "800Hz",
30 static SOC_ENUM_SINGLE_DECL(max98371_monomix
, MAX98371_MONOMIX_CFG
, 0,
33 static SOC_ENUM_SINGLE_DECL(max98371_hpf_cutoff
, MAX98371_HPF
, 0,
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),
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),
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),
63 static const struct reg_default max98371_reg
[] = {
132 static bool max98371_volatile_register(struct device
*dev
, unsigned int reg
)
135 case MAX98371_IRQ_CLEAR1
:
136 case MAX98371_IRQ_CLEAR2
:
137 case MAX98371_IRQ_CLEAR3
:
138 case MAX98371_VERSION
:
145 static bool max98371_readable_register(struct device
*dev
, unsigned int reg
)
148 case MAX98371_SOFT_RESET
:
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)
160 static const DECLARE_TLV_DB_RANGE(max98371_noload_gain_tlv
,
161 0, 11, TLV_DB_SCALE_ITEM(950, 100, 0),
164 static const DECLARE_TLV_DB_SCALE(digital_tlv
, -6300, 50, 1);
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,
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
),
187 static int max98371_dai_set_fmt(struct snd_soc_dai
*codec_dai
,
190 struct snd_soc_codec
*codec
= codec_dai
->codec
;
191 struct max98371_priv
*max98371
= snd_soc_codec_get_drvdata(codec
);
192 unsigned int val
= 0;
194 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
195 case SND_SOC_DAIFMT_CBS_CFS
:
198 dev_err(codec
->dev
, "DAI clock mode unsupported");
202 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
203 case SND_SOC_DAIFMT_I2S
:
206 case SND_SOC_DAIFMT_RIGHT_J
:
207 val
|= MAX98371_DAI_RIGHT
;
209 case SND_SOC_DAIFMT_LEFT_J
:
210 val
|= MAX98371_DAI_LEFT
;
213 dev_err(codec
->dev
, "DAI wrong mode unsupported");
216 regmap_update_bits(max98371
->regmap
, MAX98371_FMT
,
217 MAX98371_FMT_MODE_MASK
, val
);
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
)
225 struct snd_soc_codec
*codec
= dai
->codec
;
226 struct max98371_priv
*max98371
= snd_soc_codec_get_drvdata(codec
);
227 int blr_clk_ratio
, ch_size
, channels
= params_channels(params
);
228 int rate
= params_rate(params
);
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
);
236 case SNDRV_PCM_FORMAT_S16_LE
:
237 regmap_update_bits(max98371
->regmap
, MAX98371_FMT
,
238 MAX98371_FMT_MASK
, MAX98371_DAI_CHANSZ_16
);
241 case SNDRV_PCM_FORMAT_S24_LE
:
242 regmap_update_bits(max98371
->regmap
, MAX98371_FMT
,
243 MAX98371_FMT_MASK
, MAX98371_DAI_CHANSZ_32
);
246 case SNDRV_PCM_FORMAT_S32_LE
:
247 regmap_update_bits(max98371
->regmap
, MAX98371_FMT
,
248 MAX98371_FMT_MASK
, MAX98371_DAI_CHANSZ_32
);
255 /* BCLK/LRCLK ratio calculation */
256 blr_clk_ratio
= channels
* ch_size
;
257 switch (blr_clk_ratio
) {
259 regmap_update_bits(max98371
->regmap
,
261 MAX98371_DAI_BSEL_MASK
, MAX98371_DAI_BSEL_32
);
264 regmap_update_bits(max98371
->regmap
,
266 MAX98371_DAI_BSEL_MASK
, MAX98371_DAI_BSEL_48
);
269 regmap_update_bits(max98371
->regmap
,
271 MAX98371_DAI_BSEL_MASK
, MAX98371_DAI_BSEL_64
);
279 regmap_update_bits(max98371
->regmap
,
281 MAX98371_SPK_SR_MASK
, MAX98371_SPK_SR_32
);
284 regmap_update_bits(max98371
->regmap
,
286 MAX98371_SPK_SR_MASK
, MAX98371_SPK_SR_44
);
289 regmap_update_bits(max98371
->regmap
,
291 MAX98371_SPK_SR_MASK
, MAX98371_SPK_SR_48
);
294 regmap_update_bits(max98371
->regmap
,
296 MAX98371_SPK_SR_MASK
, MAX98371_SPK_SR_88
);
299 regmap_update_bits(max98371
->regmap
,
301 MAX98371_SPK_SR_MASK
, MAX98371_SPK_SR_96
);
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
);
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
,
319 SND_SOC_DAPM_OUTPUT("SPK_OUT"),
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"},
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)
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
,
337 static struct snd_soc_dai_driver max98371_dai
[] = {
339 .name
= "max98371-aif1",
341 .stream_name
= "HiFi Playback",
344 .rates
= SNDRV_PCM_RATE_8000_48000
,
345 .formats
= MAX98371_FORMATS
,
347 .ops
= &max98371_dai_ops
,
351 static const struct snd_soc_codec_driver max98371_codec
= {
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
),
360 static const struct regmap_config max98371_regmap
= {
363 .max_register
= MAX98371_VERSION
,
364 .reg_defaults
= max98371_reg
,
365 .num_reg_defaults
= ARRAY_SIZE(max98371_reg
),
366 .volatile_reg
= max98371_volatile_register
,
367 .readable_reg
= max98371_readable_register
,
368 .cache_type
= REGCACHE_RBTREE
,
371 static int max98371_i2c_probe(struct i2c_client
*i2c
,
372 const struct i2c_device_id
*id
)
374 struct max98371_priv
*max98371
;
377 max98371
= devm_kzalloc(&i2c
->dev
,
378 sizeof(*max98371
), GFP_KERNEL
);
382 i2c_set_clientdata(i2c
, max98371
);
383 max98371
->regmap
= devm_regmap_init_i2c(i2c
, &max98371_regmap
);
384 if (IS_ERR(max98371
->regmap
)) {
385 ret
= PTR_ERR(max98371
->regmap
);
387 "Failed to allocate regmap: %d\n", ret
);
391 ret
= regmap_read(max98371
->regmap
, MAX98371_VERSION
, ®
);
393 dev_info(&i2c
->dev
, "device error %d\n", ret
);
396 dev_info(&i2c
->dev
, "device version %x\n", reg
);
398 ret
= snd_soc_register_codec(&i2c
->dev
, &max98371_codec
,
399 max98371_dai
, ARRAY_SIZE(max98371_dai
));
401 dev_err(&i2c
->dev
, "Failed to register codec: %d\n", ret
);
407 static int max98371_i2c_remove(struct i2c_client
*client
)
409 snd_soc_unregister_codec(&client
->dev
);
413 static const struct i2c_device_id max98371_i2c_id
[] = {
418 MODULE_DEVICE_TABLE(i2c
, max98371_i2c_id
);
420 static const struct of_device_id max98371_of_match
[] = {
421 { .compatible
= "maxim,max98371", },
424 MODULE_DEVICE_TABLE(of
, max98371_of_match
);
426 static struct i2c_driver max98371_i2c_driver
= {
430 .of_match_table
= of_match_ptr(max98371_of_match
),
432 .probe
= max98371_i2c_probe
,
433 .remove
= max98371_i2c_remove
,
434 .id_table
= max98371_i2c_id
,
437 module_i2c_driver(max98371_i2c_driver
);
439 MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
440 MODULE_DESCRIPTION("ALSA SoC MAX98371 driver");
441 MODULE_LICENSE("GPL");