2 * max98090.c -- MAX98090 ALSA SoC Audio driver
3 * based on Rev0p8 datasheet
5 * Copyright (C) 2012 Renesas Solutions Corp.
6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
11 * Copyright 2011 Maxim Integrated Products
13 * https://github.com/hardkernel/linux/commit/\
14 * 3417d7166b17113b3b33b0a337c74d1c7cc313df#sound/soc/codecs/max98090.c
15 * Copyright 2011 Maxim Integrated Products
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
30 * MAX98090 Registers Definition
34 /* RESET / STATUS / INTERRUPT REGISTERS */
35 #define MAX98090_0x00_SW_RESET 0x00
36 #define MAX98090_0x01_INT_STS 0x01
37 #define MAX98090_0x02_JACK_STS 0x02
38 #define MAX98090_0x03_INT_MASK 0x03
40 /* QUICK SETUP REGISTERS */
41 #define MAX98090_0x04_SYS_CLK 0x04
42 #define MAX98090_0x05_SAMPLE_RATE 0x05
43 #define MAX98090_0x06_DAI_IF 0x06
44 #define MAX98090_0x07_DAC_PATH 0x07
45 #define MAX98090_0x08_MIC_TO_ADC 0x08
46 #define MAX98090_0x09_LINE_TO_ADC 0x09
47 #define MAX98090_0x0A_ANALOG_MIC_LOOP 0x0A
48 #define MAX98090_0x0B_ANALOG_LINE_LOOP 0x0B
50 /* ANALOG INPUT CONFIGURATION REGISTERS */
51 #define MAX98090_0x0D_INPUT_CONFIG 0x0D
52 #define MAX98090_0x0E_LINE_IN_LVL 0x0E
53 #define MAX98090_0x0F_LINI_IN_CFG 0x0F
54 #define MAX98090_0x10_MIC1_IN_LVL 0x10
55 #define MAX98090_0x11_MIC2_IN_LVL 0x11
57 /* MICROPHONE CONFIGURATION REGISTERS */
58 #define MAX98090_0x12_MIC_BIAS_VOL 0x12
59 #define MAX98090_0x13_DIGITAL_MIC_CFG 0x13
60 #define MAX98090_0x14_DIGITAL_MIC_MODE 0x14
62 /* ADC PATH AND CONFIGURATION REGISTERS */
63 #define MAX98090_0x15_L_ADC_MIX 0x15
64 #define MAX98090_0x16_R_ADC_MIX 0x16
65 #define MAX98090_0x17_L_ADC_LVL 0x17
66 #define MAX98090_0x18_R_ADC_LVL 0x18
67 #define MAX98090_0x19_ADC_BIQUAD_LVL 0x19
68 #define MAX98090_0x1A_ADC_SIDETONE 0x1A
70 /* CLOCK CONFIGURATION REGISTERS */
71 #define MAX98090_0x1B_SYS_CLK 0x1B
72 #define MAX98090_0x1C_CLK_MODE 0x1C
73 #define MAX98090_0x1D_ANY_CLK1 0x1D
74 #define MAX98090_0x1E_ANY_CLK2 0x1E
75 #define MAX98090_0x1F_ANY_CLK3 0x1F
76 #define MAX98090_0x20_ANY_CLK4 0x20
77 #define MAX98090_0x21_MASTER_MODE 0x21
79 /* INTERFACE CONTROL REGISTERS */
80 #define MAX98090_0x22_DAI_IF_FMT 0x22
81 #define MAX98090_0x23_DAI_TDM_FMT1 0x23
82 #define MAX98090_0x24_DAI_TDM_FMT2 0x24
83 #define MAX98090_0x25_DAI_IO_CFG 0x25
84 #define MAX98090_0x26_FILTER_CFG 0x26
85 #define MAX98090_0x27_DAI_PLAYBACK_LVL 0x27
86 #define MAX98090_0x28_EQ_PLAYBACK_LVL 0x28
88 /* HEADPHONE CONTROL REGISTERS */
89 #define MAX98090_0x29_L_HP_MIX 0x29
90 #define MAX98090_0x2A_R_HP_MIX 0x2A
91 #define MAX98090_0x2B_HP_CTR 0x2B
92 #define MAX98090_0x2C_L_HP_VOL 0x2C
93 #define MAX98090_0x2D_R_HP_VOL 0x2D
95 /* SPEAKER CONFIGURATION REGISTERS */
96 #define MAX98090_0x2E_L_SPK_MIX 0x2E
97 #define MAX98090_0x2F_R_SPK_MIX 0x2F
98 #define MAX98090_0x30_SPK_CTR 0x30
99 #define MAX98090_0x31_L_SPK_VOL 0x31
100 #define MAX98090_0x32_R_SPK_VOL 0x32
102 /* ALC CONFIGURATION REGISTERS */
103 #define MAX98090_0x33_ALC_TIMING 0x33
104 #define MAX98090_0x34_ALC_COMPRESSOR 0x34
105 #define MAX98090_0x35_ALC_EXPANDER 0x35
106 #define MAX98090_0x36_ALC_GAIN 0x36
108 /* RECEIVER AND LINE_OUTPUT REGISTERS */
109 #define MAX98090_0x37_RCV_LOUT_L_MIX 0x37
110 #define MAX98090_0x38_RCV_LOUT_L_CNTL 0x38
111 #define MAX98090_0x39_RCV_LOUT_L_VOL 0x39
112 #define MAX98090_0x3A_LOUT_R_MIX 0x3A
113 #define MAX98090_0x3B_LOUT_R_CNTL 0x3B
114 #define MAX98090_0x3C_LOUT_R_VOL 0x3C
116 /* JACK DETECT AND ENABLE REGISTERS */
117 #define MAX98090_0x3D_JACK_DETECT 0x3D
118 #define MAX98090_0x3E_IN_ENABLE 0x3E
119 #define MAX98090_0x3F_OUT_ENABLE 0x3F
120 #define MAX98090_0x40_LVL_CTR 0x40
121 #define MAX98090_0x41_DSP_FILTER_ENABLE 0x41
123 /* BIAS AND POWER MODE CONFIGURATION REGISTERS */
124 #define MAX98090_0x42_BIAS_CTR 0x42
125 #define MAX98090_0x43_DAC_CTR 0x43
126 #define MAX98090_0x44_ADC_CTR 0x44
127 #define MAX98090_0x45_DEV_SHUTDOWN 0x45
129 /* REVISION ID REGISTER */
130 #define MAX98090_0xFF_REV_ID 0xFF
132 #define MAX98090_REG_MAX_CACHED 0x45
133 #define MAX98090_REG_END 0xFF
137 * MAX98090 Registers Bit Fields
141 /* MAX98090_0x06_DAI_IF */
142 #define MAX98090_DAI_IF_MASK 0x3F
143 #define MAX98090_RJ_M (1 << 5)
144 #define MAX98090_RJ_S (1 << 4)
145 #define MAX98090_LJ_M (1 << 3)
146 #define MAX98090_LJ_S (1 << 2)
147 #define MAX98090_I2S_M (1 << 1)
148 #define MAX98090_I2S_S (1 << 0)
150 /* MAX98090_0x45_DEV_SHUTDOWN */
151 #define MAX98090_SHDNRUN (1 << 7)
153 /* codec private data */
154 struct max98090_priv
{
155 struct regmap
*regmap
;
158 static const struct reg_default max98090_reg_defaults
[] = {
159 /* RESET / STATUS / INTERRUPT REGISTERS */
160 {MAX98090_0x00_SW_RESET
, 0x00},
161 {MAX98090_0x01_INT_STS
, 0x00},
162 {MAX98090_0x02_JACK_STS
, 0x00},
163 {MAX98090_0x03_INT_MASK
, 0x04},
165 /* QUICK SETUP REGISTERS */
166 {MAX98090_0x04_SYS_CLK
, 0x00},
167 {MAX98090_0x05_SAMPLE_RATE
, 0x00},
168 {MAX98090_0x06_DAI_IF
, 0x00},
169 {MAX98090_0x07_DAC_PATH
, 0x00},
170 {MAX98090_0x08_MIC_TO_ADC
, 0x00},
171 {MAX98090_0x09_LINE_TO_ADC
, 0x00},
172 {MAX98090_0x0A_ANALOG_MIC_LOOP
, 0x00},
173 {MAX98090_0x0B_ANALOG_LINE_LOOP
, 0x00},
175 /* ANALOG INPUT CONFIGURATION REGISTERS */
176 {MAX98090_0x0D_INPUT_CONFIG
, 0x00},
177 {MAX98090_0x0E_LINE_IN_LVL
, 0x1B},
178 {MAX98090_0x0F_LINI_IN_CFG
, 0x00},
179 {MAX98090_0x10_MIC1_IN_LVL
, 0x11},
180 {MAX98090_0x11_MIC2_IN_LVL
, 0x11},
182 /* MICROPHONE CONFIGURATION REGISTERS */
183 {MAX98090_0x12_MIC_BIAS_VOL
, 0x00},
184 {MAX98090_0x13_DIGITAL_MIC_CFG
, 0x00},
185 {MAX98090_0x14_DIGITAL_MIC_MODE
, 0x00},
187 /* ADC PATH AND CONFIGURATION REGISTERS */
188 {MAX98090_0x15_L_ADC_MIX
, 0x00},
189 {MAX98090_0x16_R_ADC_MIX
, 0x00},
190 {MAX98090_0x17_L_ADC_LVL
, 0x03},
191 {MAX98090_0x18_R_ADC_LVL
, 0x03},
192 {MAX98090_0x19_ADC_BIQUAD_LVL
, 0x00},
193 {MAX98090_0x1A_ADC_SIDETONE
, 0x00},
195 /* CLOCK CONFIGURATION REGISTERS */
196 {MAX98090_0x1B_SYS_CLK
, 0x00},
197 {MAX98090_0x1C_CLK_MODE
, 0x00},
198 {MAX98090_0x1D_ANY_CLK1
, 0x00},
199 {MAX98090_0x1E_ANY_CLK2
, 0x00},
200 {MAX98090_0x1F_ANY_CLK3
, 0x00},
201 {MAX98090_0x20_ANY_CLK4
, 0x00},
202 {MAX98090_0x21_MASTER_MODE
, 0x00},
204 /* INTERFACE CONTROL REGISTERS */
205 {MAX98090_0x22_DAI_IF_FMT
, 0x00},
206 {MAX98090_0x23_DAI_TDM_FMT1
, 0x00},
207 {MAX98090_0x24_DAI_TDM_FMT2
, 0x00},
208 {MAX98090_0x25_DAI_IO_CFG
, 0x00},
209 {MAX98090_0x26_FILTER_CFG
, 0x80},
210 {MAX98090_0x27_DAI_PLAYBACK_LVL
, 0x00},
211 {MAX98090_0x28_EQ_PLAYBACK_LVL
, 0x00},
213 /* HEADPHONE CONTROL REGISTERS */
214 {MAX98090_0x29_L_HP_MIX
, 0x00},
215 {MAX98090_0x2A_R_HP_MIX
, 0x00},
216 {MAX98090_0x2B_HP_CTR
, 0x00},
217 {MAX98090_0x2C_L_HP_VOL
, 0x1A},
218 {MAX98090_0x2D_R_HP_VOL
, 0x1A},
220 /* SPEAKER CONFIGURATION REGISTERS */
221 {MAX98090_0x2E_L_SPK_MIX
, 0x00},
222 {MAX98090_0x2F_R_SPK_MIX
, 0x00},
223 {MAX98090_0x30_SPK_CTR
, 0x00},
224 {MAX98090_0x31_L_SPK_VOL
, 0x2C},
225 {MAX98090_0x32_R_SPK_VOL
, 0x2C},
227 /* ALC CONFIGURATION REGISTERS */
228 {MAX98090_0x33_ALC_TIMING
, 0x00},
229 {MAX98090_0x34_ALC_COMPRESSOR
, 0x00},
230 {MAX98090_0x35_ALC_EXPANDER
, 0x00},
231 {MAX98090_0x36_ALC_GAIN
, 0x00},
233 /* RECEIVER AND LINE_OUTPUT REGISTERS */
234 {MAX98090_0x37_RCV_LOUT_L_MIX
, 0x00},
235 {MAX98090_0x38_RCV_LOUT_L_CNTL
, 0x00},
236 {MAX98090_0x39_RCV_LOUT_L_VOL
, 0x15},
237 {MAX98090_0x3A_LOUT_R_MIX
, 0x00},
238 {MAX98090_0x3B_LOUT_R_CNTL
, 0x00},
239 {MAX98090_0x3C_LOUT_R_VOL
, 0x15},
241 /* JACK DETECT AND ENABLE REGISTERS */
242 {MAX98090_0x3D_JACK_DETECT
, 0x00},
243 {MAX98090_0x3E_IN_ENABLE
, 0x00},
244 {MAX98090_0x3F_OUT_ENABLE
, 0x00},
245 {MAX98090_0x40_LVL_CTR
, 0x00},
246 {MAX98090_0x41_DSP_FILTER_ENABLE
, 0x00},
248 /* BIAS AND POWER MODE CONFIGURATION REGISTERS */
249 {MAX98090_0x42_BIAS_CTR
, 0x00},
250 {MAX98090_0x43_DAC_CTR
, 0x00},
251 {MAX98090_0x44_ADC_CTR
, 0x06},
252 {MAX98090_0x45_DEV_SHUTDOWN
, 0x00},
255 static const unsigned int max98090_hp_tlv
[] = {
256 TLV_DB_RANGE_HEAD(5),
257 0x0, 0x6, TLV_DB_SCALE_ITEM(-6700, 400, 0),
258 0x7, 0xE, TLV_DB_SCALE_ITEM(-4000, 300, 0),
259 0xF, 0x15, TLV_DB_SCALE_ITEM(-1700, 200, 0),
260 0x16, 0x1B, TLV_DB_SCALE_ITEM(-400, 100, 0),
261 0x1C, 0x1F, TLV_DB_SCALE_ITEM(150, 50, 0),
264 static struct snd_kcontrol_new max98090_snd_controls
[] = {
265 SOC_DOUBLE_R_TLV("Headphone Volume", MAX98090_0x2C_L_HP_VOL
,
266 MAX98090_0x2D_R_HP_VOL
, 0, 31, 0, max98090_hp_tlv
),
269 /* Left HeadPhone Mixer Switch */
270 static struct snd_kcontrol_new max98090_left_hp_mixer_controls
[] = {
271 SOC_DAPM_SINGLE("DACR Switch", MAX98090_0x29_L_HP_MIX
, 1, 1, 0),
272 SOC_DAPM_SINGLE("DACL Switch", MAX98090_0x29_L_HP_MIX
, 0, 1, 0),
275 /* Right HeadPhone Mixer Switch */
276 static struct snd_kcontrol_new max98090_right_hp_mixer_controls
[] = {
277 SOC_DAPM_SINGLE("DACR Switch", MAX98090_0x2A_R_HP_MIX
, 1, 1, 0),
278 SOC_DAPM_SINGLE("DACL Switch", MAX98090_0x2A_R_HP_MIX
, 0, 1, 0),
281 static struct snd_soc_dapm_widget max98090_dapm_widgets
[] = {
283 SND_SOC_DAPM_OUTPUT("HPL"),
284 SND_SOC_DAPM_OUTPUT("HPR"),
287 SND_SOC_DAPM_PGA("HPL Out", MAX98090_0x3F_OUT_ENABLE
, 7, 0, NULL
, 0),
288 SND_SOC_DAPM_PGA("HPR Out", MAX98090_0x3F_OUT_ENABLE
, 6, 0, NULL
, 0),
291 SND_SOC_DAPM_MIXER("HPL Mixer", SND_SOC_NOPM
, 0, 0,
292 max98090_left_hp_mixer_controls
,
293 ARRAY_SIZE(max98090_left_hp_mixer_controls
)),
295 SND_SOC_DAPM_MIXER("HPR Mixer", SND_SOC_NOPM
, 0, 0,
296 max98090_right_hp_mixer_controls
,
297 ARRAY_SIZE(max98090_right_hp_mixer_controls
)),
300 SND_SOC_DAPM_DAC("DACL", "Hifi Playback", MAX98090_0x3F_OUT_ENABLE
, 0, 0),
301 SND_SOC_DAPM_DAC("DACR", "Hifi Playback", MAX98090_0x3F_OUT_ENABLE
, 1, 0),
304 static struct snd_soc_dapm_route max98090_audio_map
[] = {
306 {"HPL", NULL
, "HPL Out"},
307 {"HPR", NULL
, "HPR Out"},
310 {"HPL Out", NULL
, "HPL Mixer"},
311 {"HPR Out", NULL
, "HPR Mixer"},
314 {"HPL Mixer", "DACR Switch", "DACR"},
315 {"HPL Mixer", "DACL Switch", "DACL"},
317 {"HPR Mixer", "DACR Switch", "DACR"},
318 {"HPR Mixer", "DACL Switch", "DACL"},
321 static bool max98090_volatile(struct device
*dev
, unsigned int reg
)
323 if ((reg
== MAX98090_0x01_INT_STS
) ||
324 (reg
== MAX98090_0x02_JACK_STS
) ||
325 (reg
> MAX98090_REG_MAX_CACHED
))
331 static int max98090_dai_hw_params(struct snd_pcm_substream
*substream
,
332 struct snd_pcm_hw_params
*params
,
333 struct snd_soc_dai
*dai
)
335 struct snd_soc_codec
*codec
= dai
->codec
;
338 switch (params_rate(params
)) {
358 dev_err(codec
->dev
, "unsupported rate\n");
361 snd_soc_update_bits(codec
, MAX98090_0x05_SAMPLE_RATE
, 0x03F, val
);
366 static int max98090_dai_set_sysclk(struct snd_soc_dai
*dai
,
367 int clk_id
, unsigned int freq
, int dir
)
369 struct snd_soc_codec
*codec
= dai
->codec
;
372 snd_soc_update_bits(codec
, MAX98090_0x45_DEV_SHUTDOWN
,
373 MAX98090_SHDNRUN
, 0);
395 dev_err(codec
->dev
, "Invalid master clock frequency\n");
398 snd_soc_update_bits(codec
, MAX98090_0x04_SYS_CLK
, 0xFD, val
);
400 snd_soc_update_bits(codec
, MAX98090_0x45_DEV_SHUTDOWN
,
401 MAX98090_SHDNRUN
, MAX98090_SHDNRUN
);
403 dev_dbg(dai
->dev
, "sysclk is %uHz\n", freq
);
408 static int max98090_dai_set_fmt(struct snd_soc_dai
*dai
,
411 struct snd_soc_codec
*codec
= dai
->codec
;
415 /* master/slave mode */
416 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
417 case SND_SOC_DAIFMT_CBM_CFM
:
420 case SND_SOC_DAIFMT_CBS_CFS
:
424 dev_err(codec
->dev
, "unsupported clock\n");
429 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
430 case SND_SOC_DAIFMT_I2S
:
431 val
= (is_master
) ? MAX98090_I2S_M
: MAX98090_I2S_S
;
433 case SND_SOC_DAIFMT_RIGHT_J
:
434 val
= (is_master
) ? MAX98090_RJ_M
: MAX98090_RJ_S
;
436 case SND_SOC_DAIFMT_LEFT_J
:
437 val
= (is_master
) ? MAX98090_LJ_M
: MAX98090_LJ_S
;
440 dev_err(codec
->dev
, "unsupported format\n");
443 snd_soc_update_bits(codec
, MAX98090_0x06_DAI_IF
,
444 MAX98090_DAI_IF_MASK
, val
);
449 #define MAX98090_RATES SNDRV_PCM_RATE_8000_96000
450 #define MAX98090_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
452 static struct snd_soc_dai_ops max98090_dai_ops
= {
453 .set_sysclk
= max98090_dai_set_sysclk
,
454 .set_fmt
= max98090_dai_set_fmt
,
455 .hw_params
= max98090_dai_hw_params
,
458 static struct snd_soc_dai_driver max98090_dai
= {
459 .name
= "max98090-Hifi",
461 .stream_name
= "Playback",
464 .rates
= MAX98090_RATES
,
465 .formats
= MAX98090_FORMATS
,
467 .ops
= &max98090_dai_ops
,
470 static int max98090_probe(struct snd_soc_codec
*codec
)
472 struct max98090_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
473 struct device
*dev
= codec
->dev
;
476 codec
->control_data
= priv
->regmap
;
477 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, SND_SOC_REGMAP
);
479 dev_err(dev
, "Failed to set cache I/O: %d\n", ret
);
484 snd_soc_update_bits(codec
, MAX98090_0x45_DEV_SHUTDOWN
,
485 MAX98090_SHDNRUN
, MAX98090_SHDNRUN
);
490 static int max98090_remove(struct snd_soc_codec
*codec
)
495 static struct snd_soc_codec_driver soc_codec_dev_max98090
= {
496 .probe
= max98090_probe
,
497 .remove
= max98090_remove
,
498 .controls
= max98090_snd_controls
,
499 .num_controls
= ARRAY_SIZE(max98090_snd_controls
),
500 .dapm_widgets
= max98090_dapm_widgets
,
501 .num_dapm_widgets
= ARRAY_SIZE(max98090_dapm_widgets
),
502 .dapm_routes
= max98090_audio_map
,
503 .num_dapm_routes
= ARRAY_SIZE(max98090_audio_map
),
506 static const struct regmap_config max98090_regmap
= {
509 .max_register
= MAX98090_REG_END
,
510 .volatile_reg
= max98090_volatile
,
511 .cache_type
= REGCACHE_RBTREE
,
512 .reg_defaults
= max98090_reg_defaults
,
513 .num_reg_defaults
= ARRAY_SIZE(max98090_reg_defaults
),
516 static int max98090_i2c_probe(struct i2c_client
*i2c
,
517 const struct i2c_device_id
*id
)
519 struct max98090_priv
*priv
;
520 struct device
*dev
= &i2c
->dev
;
524 priv
= devm_kzalloc(dev
, sizeof(struct max98090_priv
),
529 priv
->regmap
= devm_regmap_init_i2c(i2c
, &max98090_regmap
);
530 if (IS_ERR(priv
->regmap
)) {
531 ret
= PTR_ERR(priv
->regmap
);
532 dev_err(dev
, "Failed to init regmap: %d\n", ret
);
536 i2c_set_clientdata(i2c
, priv
);
538 ret
= regmap_read(priv
->regmap
, MAX98090_0xFF_REV_ID
, &val
);
540 dev_err(dev
, "Failed to read device revision: %d\n", ret
);
543 dev_info(dev
, "revision 0x%02x\n", val
);
545 ret
= snd_soc_register_codec(dev
,
546 &soc_codec_dev_max98090
,
552 static int max98090_i2c_remove(struct i2c_client
*client
)
554 snd_soc_unregister_codec(&client
->dev
);
558 static const struct i2c_device_id max98090_i2c_id
[] = {
562 MODULE_DEVICE_TABLE(i2c
, max98090_i2c_id
);
564 static struct i2c_driver max98090_i2c_driver
= {
567 .owner
= THIS_MODULE
,
569 .probe
= max98090_i2c_probe
,
570 .remove
= max98090_i2c_remove
,
571 .id_table
= max98090_i2c_id
,
573 module_i2c_driver(max98090_i2c_driver
);
575 MODULE_DESCRIPTION("ALSA SoC MAX98090 driver");
576 MODULE_AUTHOR("Peter Hsiang, Kuninori Morimoto");
577 MODULE_LICENSE("GPL");