1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max9850.c -- codec driver for max9850
5 * Copyright (C) 2011 taskit GmbH
7 * Author: Christian Glindkamp <christian.glindkamp@taskit.de>
9 * Initial development of this code was funded by
10 * MICRONIC Computer Systeme GmbH, http://www.mcsberlin.de/
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/tlv.h>
26 struct regmap
*regmap
;
30 /* max9850 register cache */
31 static const struct reg_default max9850_reg
[] = {
43 /* these registers are not used at the moment but provided for the sake of
45 static bool max9850_volatile_register(struct device
*dev
, unsigned int reg
)
56 static const struct regmap_config max9850_regmap
= {
60 .max_register
= MAX9850_DIGITAL_AUDIO
,
61 .volatile_reg
= max9850_volatile_register
,
62 .cache_type
= REGCACHE_RBTREE
,
65 static const DECLARE_TLV_DB_RANGE(max9850_tlv
,
66 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
67 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
68 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
69 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
72 static const struct snd_kcontrol_new max9850_controls
[] = {
73 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME
, 0, 0x3f, 1, max9850_tlv
),
74 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME
, 7, 1, 1),
75 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE
, 2, 1, 0),
78 static const struct snd_kcontrol_new max9850_mixer_controls
[] = {
79 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE
, 1, 1, 0),
82 static const struct snd_soc_dapm_widget max9850_dapm_widgets
[] = {
83 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE
, 4, 0, NULL
, 0),
84 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE
, 5, 0, NULL
, 0),
85 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE
, 6, 0, NULL
, 0),
86 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE
, 7, 0, NULL
, 0),
87 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE
, 2, 0,
88 &max9850_mixer_controls
[0],
89 ARRAY_SIZE(max9850_mixer_controls
)),
90 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE
, 3, 0, NULL
, 0),
91 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE
, 0, 0),
92 SND_SOC_DAPM_OUTPUT("OUTL"),
93 SND_SOC_DAPM_OUTPUT("HPL"),
94 SND_SOC_DAPM_OUTPUT("OUTR"),
95 SND_SOC_DAPM_OUTPUT("HPR"),
96 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM
, 0, 0, NULL
, 0),
97 SND_SOC_DAPM_INPUT("INL"),
98 SND_SOC_DAPM_INPUT("INR"),
101 static const struct snd_soc_dapm_route max9850_dapm_routes
[] = {
103 {"Output Mixer", NULL
, "DAC"},
104 {"Output Mixer", "Line In Switch", "Line Input"},
107 {"Headphone Output", NULL
, "Output Mixer"},
108 {"HPL", NULL
, "Headphone Output"},
109 {"HPR", NULL
, "Headphone Output"},
110 {"OUTL", NULL
, "Output Mixer"},
111 {"OUTR", NULL
, "Output Mixer"},
114 {"Line Input", NULL
, "INL"},
115 {"Line Input", NULL
, "INR"},
118 {"Output Mixer", NULL
, "Charge Pump 1"},
119 {"Output Mixer", NULL
, "Charge Pump 2"},
120 {"Output Mixer", NULL
, "SHDN"},
121 {"DAC", NULL
, "MCLK"},
124 static int max9850_hw_params(struct snd_pcm_substream
*substream
,
125 struct snd_pcm_hw_params
*params
,
126 struct snd_soc_dai
*dai
)
128 struct snd_soc_component
*component
= dai
->component
;
129 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
133 if (!max9850
->sysclk
)
136 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
137 sf
= (snd_soc_component_read32(component
, MAX9850_CLOCK
) >> 2) + 1;
138 lrclk_div
= (1 << 22);
139 lrclk_div
*= params_rate(params
);
141 do_div(lrclk_div
, max9850
->sysclk
);
143 snd_soc_component_write(component
, MAX9850_LRCLK_MSB
, (lrclk_div
>> 8) & 0x7f);
144 snd_soc_component_write(component
, MAX9850_LRCLK_LSB
, lrclk_div
& 0xff);
146 switch (params_width(params
)) {
159 snd_soc_component_update_bits(component
, MAX9850_DIGITAL_AUDIO
, 0x3, da
);
164 static int max9850_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
165 int clk_id
, unsigned int freq
, int dir
)
167 struct snd_soc_component
*component
= codec_dai
->component
;
168 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
170 /* calculate mclk -> iclk divider */
171 if (freq
<= 13000000)
172 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x0);
173 else if (freq
<= 26000000)
174 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x4);
175 else if (freq
<= 40000000)
176 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x8);
180 max9850
->sysclk
= freq
;
184 static int max9850_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
186 struct snd_soc_component
*component
= codec_dai
->component
;
189 /* set master/slave audio interface */
190 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
191 case SND_SOC_DAIFMT_CBM_CFM
:
192 da
|= MAX9850_MASTER
;
194 case SND_SOC_DAIFMT_CBS_CFS
:
200 /* interface format */
201 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
202 case SND_SOC_DAIFMT_I2S
:
205 case SND_SOC_DAIFMT_RIGHT_J
:
208 case SND_SOC_DAIFMT_LEFT_J
:
214 /* clock inversion */
215 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
216 case SND_SOC_DAIFMT_NB_NF
:
218 case SND_SOC_DAIFMT_IB_IF
:
219 da
|= MAX9850_BCINV
| MAX9850_INV
;
221 case SND_SOC_DAIFMT_IB_NF
:
224 case SND_SOC_DAIFMT_NB_IF
:
232 snd_soc_component_write(component
, MAX9850_DIGITAL_AUDIO
, da
);
237 static int max9850_set_bias_level(struct snd_soc_component
*component
,
238 enum snd_soc_bias_level level
)
240 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
244 case SND_SOC_BIAS_ON
:
246 case SND_SOC_BIAS_PREPARE
:
248 case SND_SOC_BIAS_STANDBY
:
249 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
250 ret
= regcache_sync(max9850
->regmap
);
252 dev_err(component
->dev
,
253 "Failed to sync cache: %d\n", ret
);
258 case SND_SOC_BIAS_OFF
:
264 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
266 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
267 SNDRV_PCM_FMTBIT_S24_LE)
269 static const struct snd_soc_dai_ops max9850_dai_ops
= {
270 .hw_params
= max9850_hw_params
,
271 .set_sysclk
= max9850_set_dai_sysclk
,
272 .set_fmt
= max9850_set_dai_fmt
,
275 static struct snd_soc_dai_driver max9850_dai
= {
276 .name
= "max9850-hifi",
278 .stream_name
= "Playback",
281 .rates
= MAX9850_RATES
,
282 .formats
= MAX9850_FORMATS
284 .ops
= &max9850_dai_ops
,
287 static int max9850_probe(struct snd_soc_component
*component
)
289 /* enable zero-detect */
290 snd_soc_component_update_bits(component
, MAX9850_GENERAL_PURPOSE
, 1, 1);
291 /* enable slew-rate control */
292 snd_soc_component_update_bits(component
, MAX9850_VOLUME
, 0x40, 0x40);
293 /* set slew-rate 125ms */
294 snd_soc_component_update_bits(component
, MAX9850_CHARGE_PUMP
, 0xff, 0xc0);
299 static const struct snd_soc_component_driver soc_component_dev_max9850
= {
300 .probe
= max9850_probe
,
301 .set_bias_level
= max9850_set_bias_level
,
302 .controls
= max9850_controls
,
303 .num_controls
= ARRAY_SIZE(max9850_controls
),
304 .dapm_widgets
= max9850_dapm_widgets
,
305 .num_dapm_widgets
= ARRAY_SIZE(max9850_dapm_widgets
),
306 .dapm_routes
= max9850_dapm_routes
,
307 .num_dapm_routes
= ARRAY_SIZE(max9850_dapm_routes
),
308 .suspend_bias_off
= 1,
310 .use_pmdown_time
= 1,
312 .non_legacy_dai_naming
= 1,
315 static int max9850_i2c_probe(struct i2c_client
*i2c
,
316 const struct i2c_device_id
*id
)
318 struct max9850_priv
*max9850
;
321 max9850
= devm_kzalloc(&i2c
->dev
, sizeof(struct max9850_priv
),
326 max9850
->regmap
= devm_regmap_init_i2c(i2c
, &max9850_regmap
);
327 if (IS_ERR(max9850
->regmap
))
328 return PTR_ERR(max9850
->regmap
);
330 i2c_set_clientdata(i2c
, max9850
);
332 ret
= devm_snd_soc_register_component(&i2c
->dev
,
333 &soc_component_dev_max9850
, &max9850_dai
, 1);
337 static const struct i2c_device_id max9850_i2c_id
[] = {
341 MODULE_DEVICE_TABLE(i2c
, max9850_i2c_id
);
343 static struct i2c_driver max9850_i2c_driver
= {
347 .probe
= max9850_i2c_probe
,
348 .id_table
= max9850_i2c_id
,
351 module_i2c_driver(max9850_i2c_driver
);
353 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
354 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
355 MODULE_LICENSE("GPL");