2 * max9850.c -- codec driver for max9850
4 * Copyright (C) 2011 taskit GmbH
6 * Author: Christian Glindkamp <christian.glindkamp@taskit.de>
8 * Initial development of this code was funded by
9 * MICRONIC Computer Systeme GmbH, http://www.mcsberlin.de/
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
31 struct regmap
*regmap
;
35 /* max9850 register cache */
36 static const struct reg_default max9850_reg
[] = {
48 /* these registers are not used at the moment but provided for the sake of
50 static bool max9850_volatile_register(struct device
*dev
, unsigned int reg
)
61 static const struct regmap_config max9850_regmap
= {
65 .max_register
= MAX9850_DIGITAL_AUDIO
,
66 .volatile_reg
= max9850_volatile_register
,
67 .cache_type
= REGCACHE_RBTREE
,
70 static const DECLARE_TLV_DB_RANGE(max9850_tlv
,
71 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
72 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
73 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
74 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
77 static const struct snd_kcontrol_new max9850_controls
[] = {
78 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME
, 0, 0x3f, 1, max9850_tlv
),
79 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME
, 7, 1, 1),
80 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE
, 2, 1, 0),
83 static const struct snd_kcontrol_new max9850_mixer_controls
[] = {
84 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE
, 1, 1, 0),
87 static const struct snd_soc_dapm_widget max9850_dapm_widgets
[] = {
88 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE
, 4, 0, NULL
, 0),
89 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE
, 5, 0, NULL
, 0),
90 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE
, 6, 0, NULL
, 0),
91 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE
, 7, 0, NULL
, 0),
92 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE
, 2, 0,
93 &max9850_mixer_controls
[0],
94 ARRAY_SIZE(max9850_mixer_controls
)),
95 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE
, 3, 0, NULL
, 0),
96 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE
, 0, 0),
97 SND_SOC_DAPM_OUTPUT("OUTL"),
98 SND_SOC_DAPM_OUTPUT("HPL"),
99 SND_SOC_DAPM_OUTPUT("OUTR"),
100 SND_SOC_DAPM_OUTPUT("HPR"),
101 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM
, 0, 0, NULL
, 0),
102 SND_SOC_DAPM_INPUT("INL"),
103 SND_SOC_DAPM_INPUT("INR"),
106 static const struct snd_soc_dapm_route max9850_dapm_routes
[] = {
108 {"Output Mixer", NULL
, "DAC"},
109 {"Output Mixer", "Line In Switch", "Line Input"},
112 {"Headphone Output", NULL
, "Output Mixer"},
113 {"HPL", NULL
, "Headphone Output"},
114 {"HPR", NULL
, "Headphone Output"},
115 {"OUTL", NULL
, "Output Mixer"},
116 {"OUTR", NULL
, "Output Mixer"},
119 {"Line Input", NULL
, "INL"},
120 {"Line Input", NULL
, "INR"},
123 {"Output Mixer", NULL
, "Charge Pump 1"},
124 {"Output Mixer", NULL
, "Charge Pump 2"},
125 {"Output Mixer", NULL
, "SHDN"},
126 {"DAC", NULL
, "MCLK"},
129 static int max9850_hw_params(struct snd_pcm_substream
*substream
,
130 struct snd_pcm_hw_params
*params
,
131 struct snd_soc_dai
*dai
)
133 struct snd_soc_component
*component
= dai
->component
;
134 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
138 if (!max9850
->sysclk
)
141 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
142 sf
= (snd_soc_component_read32(component
, MAX9850_CLOCK
) >> 2) + 1;
143 lrclk_div
= (1 << 22);
144 lrclk_div
*= params_rate(params
);
146 do_div(lrclk_div
, max9850
->sysclk
);
148 snd_soc_component_write(component
, MAX9850_LRCLK_MSB
, (lrclk_div
>> 8) & 0x7f);
149 snd_soc_component_write(component
, MAX9850_LRCLK_LSB
, lrclk_div
& 0xff);
151 switch (params_width(params
)) {
164 snd_soc_component_update_bits(component
, MAX9850_DIGITAL_AUDIO
, 0x3, da
);
169 static int max9850_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
170 int clk_id
, unsigned int freq
, int dir
)
172 struct snd_soc_component
*component
= codec_dai
->component
;
173 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
175 /* calculate mclk -> iclk divider */
176 if (freq
<= 13000000)
177 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x0);
178 else if (freq
<= 26000000)
179 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x4);
180 else if (freq
<= 40000000)
181 snd_soc_component_write(component
, MAX9850_CLOCK
, 0x8);
185 max9850
->sysclk
= freq
;
189 static int max9850_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
191 struct snd_soc_component
*component
= codec_dai
->component
;
194 /* set master/slave audio interface */
195 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
196 case SND_SOC_DAIFMT_CBM_CFM
:
197 da
|= MAX9850_MASTER
;
199 case SND_SOC_DAIFMT_CBS_CFS
:
205 /* interface format */
206 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
207 case SND_SOC_DAIFMT_I2S
:
210 case SND_SOC_DAIFMT_RIGHT_J
:
213 case SND_SOC_DAIFMT_LEFT_J
:
219 /* clock inversion */
220 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
221 case SND_SOC_DAIFMT_NB_NF
:
223 case SND_SOC_DAIFMT_IB_IF
:
224 da
|= MAX9850_BCINV
| MAX9850_INV
;
226 case SND_SOC_DAIFMT_IB_NF
:
229 case SND_SOC_DAIFMT_NB_IF
:
237 snd_soc_component_write(component
, MAX9850_DIGITAL_AUDIO
, da
);
242 static int max9850_set_bias_level(struct snd_soc_component
*component
,
243 enum snd_soc_bias_level level
)
245 struct max9850_priv
*max9850
= snd_soc_component_get_drvdata(component
);
249 case SND_SOC_BIAS_ON
:
251 case SND_SOC_BIAS_PREPARE
:
253 case SND_SOC_BIAS_STANDBY
:
254 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
255 ret
= regcache_sync(max9850
->regmap
);
257 dev_err(component
->dev
,
258 "Failed to sync cache: %d\n", ret
);
263 case SND_SOC_BIAS_OFF
:
269 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
271 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
272 SNDRV_PCM_FMTBIT_S24_LE)
274 static const struct snd_soc_dai_ops max9850_dai_ops
= {
275 .hw_params
= max9850_hw_params
,
276 .set_sysclk
= max9850_set_dai_sysclk
,
277 .set_fmt
= max9850_set_dai_fmt
,
280 static struct snd_soc_dai_driver max9850_dai
= {
281 .name
= "max9850-hifi",
283 .stream_name
= "Playback",
286 .rates
= MAX9850_RATES
,
287 .formats
= MAX9850_FORMATS
289 .ops
= &max9850_dai_ops
,
292 static int max9850_probe(struct snd_soc_component
*component
)
294 /* enable zero-detect */
295 snd_soc_component_update_bits(component
, MAX9850_GENERAL_PURPOSE
, 1, 1);
296 /* enable slew-rate control */
297 snd_soc_component_update_bits(component
, MAX9850_VOLUME
, 0x40, 0x40);
298 /* set slew-rate 125ms */
299 snd_soc_component_update_bits(component
, MAX9850_CHARGE_PUMP
, 0xff, 0xc0);
304 static const struct snd_soc_component_driver soc_component_dev_max9850
= {
305 .probe
= max9850_probe
,
306 .set_bias_level
= max9850_set_bias_level
,
307 .controls
= max9850_controls
,
308 .num_controls
= ARRAY_SIZE(max9850_controls
),
309 .dapm_widgets
= max9850_dapm_widgets
,
310 .num_dapm_widgets
= ARRAY_SIZE(max9850_dapm_widgets
),
311 .dapm_routes
= max9850_dapm_routes
,
312 .num_dapm_routes
= ARRAY_SIZE(max9850_dapm_routes
),
313 .suspend_bias_off
= 1,
315 .use_pmdown_time
= 1,
317 .non_legacy_dai_naming
= 1,
320 static int max9850_i2c_probe(struct i2c_client
*i2c
,
321 const struct i2c_device_id
*id
)
323 struct max9850_priv
*max9850
;
326 max9850
= devm_kzalloc(&i2c
->dev
, sizeof(struct max9850_priv
),
331 max9850
->regmap
= devm_regmap_init_i2c(i2c
, &max9850_regmap
);
332 if (IS_ERR(max9850
->regmap
))
333 return PTR_ERR(max9850
->regmap
);
335 i2c_set_clientdata(i2c
, max9850
);
337 ret
= devm_snd_soc_register_component(&i2c
->dev
,
338 &soc_component_dev_max9850
, &max9850_dai
, 1);
342 static const struct i2c_device_id max9850_i2c_id
[] = {
346 MODULE_DEVICE_TABLE(i2c
, max9850_i2c_id
);
348 static struct i2c_driver max9850_i2c_driver
= {
352 .probe
= max9850_i2c_probe
,
353 .id_table
= max9850_i2c_id
,
356 module_i2c_driver(max9850_i2c_driver
);
358 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
359 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
360 MODULE_LICENSE("GPL");