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 /* these registers are not used at the moment but provided for the sake of
32 static bool max9850_volatile_register(struct device
*dev
, unsigned int reg
)
43 static const struct regmap_config max9850_regmap
= {
47 .max_register
= MAX9850_DIGITAL_AUDIO
,
48 .volatile_reg
= max9850_volatile_register
,
49 .cache_type
= REGCACHE_RBTREE
,
52 static const DECLARE_TLV_DB_RANGE(max9850_tlv
,
53 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
54 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
55 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
56 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
59 static const struct snd_kcontrol_new max9850_controls
[] = {
60 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME
, 0, 0x3f, 1, max9850_tlv
),
61 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME
, 7, 1, 1),
62 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE
, 2, 1, 0),
65 static const struct snd_kcontrol_new max9850_mixer_controls
[] = {
66 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE
, 1, 1, 0),
69 static const struct snd_soc_dapm_widget max9850_dapm_widgets
[] = {
70 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE
, 4, 0, NULL
, 0),
71 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE
, 5, 0, NULL
, 0),
72 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE
, 6, 0, NULL
, 0),
73 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE
, 7, 0, NULL
, 0),
74 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE
, 2, 0,
75 &max9850_mixer_controls
[0],
76 ARRAY_SIZE(max9850_mixer_controls
)),
77 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE
, 3, 0, NULL
, 0),
78 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE
, 0, 0),
79 SND_SOC_DAPM_OUTPUT("OUTL"),
80 SND_SOC_DAPM_OUTPUT("HPL"),
81 SND_SOC_DAPM_OUTPUT("OUTR"),
82 SND_SOC_DAPM_OUTPUT("HPR"),
83 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM
, 0, 0, NULL
, 0),
84 SND_SOC_DAPM_INPUT("INL"),
85 SND_SOC_DAPM_INPUT("INR"),
88 static const struct snd_soc_dapm_route max9850_dapm_routes
[] = {
90 {"Output Mixer", NULL
, "DAC"},
91 {"Output Mixer", "Line In Switch", "Line Input"},
94 {"Headphone Output", NULL
, "Output Mixer"},
95 {"HPL", NULL
, "Headphone Output"},
96 {"HPR", NULL
, "Headphone Output"},
97 {"OUTL", NULL
, "Output Mixer"},
98 {"OUTR", NULL
, "Output Mixer"},
101 {"Line Input", NULL
, "INL"},
102 {"Line Input", NULL
, "INR"},
105 {"Output Mixer", NULL
, "Charge Pump 1"},
106 {"Output Mixer", NULL
, "Charge Pump 2"},
107 {"Output Mixer", NULL
, "SHDN"},
108 {"DAC", NULL
, "MCLK"},
111 static int max9850_hw_params(struct snd_pcm_substream
*substream
,
112 struct snd_pcm_hw_params
*params
,
113 struct snd_soc_dai
*dai
)
115 struct snd_soc_component
*component
= dai
->component
;
116 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
120 if (!max9850
->sysclk
)
123 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
124 sf
= (snd_soc_component_read32(component
, MAX9850_CLOCK
) >> 2) + 1;
125 lrclk_div
= (1 << 22);
126 lrclk_div
*= params_rate(params
);
128 do_div(lrclk_div
, max9850
->sysclk
);
130 snd_soc_component_write(component
, MAX9850_LRCLK_MSB
, (lrclk_div
>> 8) & 0x7f);
131 snd_soc_component_write(component
, MAX9850_LRCLK_LSB
, lrclk_div
& 0xff);
133 switch (params_width(params
)) {
146 snd_soc_component_update_bits(component
, MAX9850_DIGITAL_AUDIO
, 0x3, da
);
151 static int max9850_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
152 int clk_id
, unsigned int freq
, int dir
)
154 struct snd_soc_component
*component
= codec_dai
->component
;
155 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
157 /* calculate mclk -> iclk divider */
158 if (freq
<= 13000000)
159 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x0);
160 else if (freq
<= 26000000)
161 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x4);
162 else if (freq
<= 40000000)
163 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x8);
167 max9850
->sysclk
= freq
;
171 static int max9850_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
173 struct snd_soc_component
*component
= codec_dai
->component
;
176 /* set master/slave audio interface */
177 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
178 case SND_SOC_DAIFMT_CBM_CFM
:
179 da
|= MAX9850_MASTER
;
181 case SND_SOC_DAIFMT_CBS_CFS
:
187 /* interface format */
188 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
189 case SND_SOC_DAIFMT_I2S
:
192 case SND_SOC_DAIFMT_RIGHT_J
:
195 case SND_SOC_DAIFMT_LEFT_J
:
201 /* clock inversion */
202 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
203 case SND_SOC_DAIFMT_NB_NF
:
205 case SND_SOC_DAIFMT_IB_IF
:
206 da
|= MAX9850_BCINV
| MAX9850_INV
;
208 case SND_SOC_DAIFMT_IB_NF
:
211 case SND_SOC_DAIFMT_NB_IF
:
219 snd_soc_component_write(component
, MAX9850_DIGITAL_AUDIO
, da
);
224 static int max9850_set_bias_level(struct snd_soc_component
*component
,
225 enum snd_soc_bias_level level
)
227 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
231 case SND_SOC_BIAS_ON
:
233 case SND_SOC_BIAS_PREPARE
:
235 case SND_SOC_BIAS_STANDBY
:
236 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
237 ret
= regcache_sync(max9850
->regmap
);
239 dev_err(component
->dev
,
240 "Failed to sync cache: %d\n", ret
);
245 case SND_SOC_BIAS_OFF
:
251 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
253 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
254 SNDRV_PCM_FMTBIT_S24_LE)
256 static const struct snd_soc_dai_ops max9850_dai_ops
= {
257 .hw_params
= max9850_hw_params
,
258 .set_sysclk
= max9850_set_dai_sysclk
,
259 .set_fmt
= max9850_set_dai_fmt
,
262 static struct snd_soc_dai_driver max9850_dai
= {
263 .name
= "max9850-hifi",
265 .stream_name
= "Playback",
268 .rates
= MAX9850_RATES
,
269 .formats
= MAX9850_FORMATS
271 .ops
= &max9850_dai_ops
,
274 static int max9850_probe(struct snd_soc_component
*component
)
276 /* enable zero-detect */
277 snd_soc_component_update_bits(component
, MAX9850_GENERAL_PURPOSE
, 1, 1);
278 /* enable slew-rate control */
279 snd_soc_component_update_bits(component
, MAX9850_VOLUME
, 0x40, 0x40);
280 /* set slew-rate 125ms */
281 snd_soc_component_update_bits(component
, MAX9850_CHARGE_PUMP
, 0xff, 0xc0);
286 static const struct snd_soc_component_driver soc_component_dev_max9850
= {
287 .probe
= max9850_probe
,
288 .set_bias_level
= max9850_set_bias_level
,
289 .controls
= max9850_controls
,
290 .num_controls
= ARRAY_SIZE(max9850_controls
),
291 .dapm_widgets
= max9850_dapm_widgets
,
292 .num_dapm_widgets
= ARRAY_SIZE(max9850_dapm_widgets
),
293 .dapm_routes
= max9850_dapm_routes
,
294 .num_dapm_routes
= ARRAY_SIZE(max9850_dapm_routes
),
295 .suspend_bias_off
= 1,
297 .use_pmdown_time
= 1,
299 .non_legacy_dai_naming
= 1,
302 static int max9850_i2c_probe(struct i2c_client
*i2c
,
303 const struct i2c_device_id
*id
)
305 struct max9850_priv
*max9850
;
308 max9850
= devm_kzalloc(&i2c
->dev
, sizeof(struct max9850_priv
),
313 max9850
->regmap
= devm_regmap_init_i2c(i2c
, &max9850_regmap
);
314 if (IS_ERR(max9850
->regmap
))
315 return PTR_ERR(max9850
->regmap
);
317 i2c_set_clientdata(i2c
, max9850
);
319 ret
= devm_snd_soc_register_component(&i2c
->dev
,
320 &soc_component_dev_max9850
, &max9850_dai
, 1);
324 static const struct i2c_device_id max9850_i2c_id
[] = {
328 MODULE_DEVICE_TABLE(i2c
, max9850_i2c_id
);
330 static struct i2c_driver max9850_i2c_driver
= {
334 .probe
= max9850_i2c_probe
,
335 .id_table
= max9850_i2c_id
,
338 module_i2c_driver(max9850_i2c_driver
);
340 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
341 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
342 MODULE_LICENSE("GPL");