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/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/tlv.h>
33 /* max9850 register cache */
34 static const u8 max9850_reg
[MAX9850_CACHEREGNUM
] = {
35 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
38 /* these registers are not used at the moment but provided for the sake of
40 static int max9850_volatile_register(struct snd_soc_codec
*codec
,
52 static const unsigned int max9850_tlv
[] = {
54 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
55 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
56 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
57 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0),
60 static const struct snd_kcontrol_new max9850_controls
[] = {
61 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME
, 0, 0x3f, 1, max9850_tlv
),
62 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME
, 7, 1, 1),
63 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE
, 2, 1, 0),
66 static const struct snd_kcontrol_new max9850_mixer_controls
[] = {
67 SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE
, 1, 1, 0),
70 static const struct snd_soc_dapm_widget max9850_dapm_widgets
[] = {
71 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE
, 4, 0, NULL
, 0),
72 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE
, 5, 0, NULL
, 0),
73 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE
, 6, 0, NULL
, 0),
74 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE
, 7, 0, NULL
, 0),
75 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE
, 2, 0,
76 &max9850_mixer_controls
[0],
77 ARRAY_SIZE(max9850_mixer_controls
)),
78 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE
, 3, 0, NULL
, 0),
79 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE
, 0, 0),
80 SND_SOC_DAPM_OUTPUT("OUTL"),
81 SND_SOC_DAPM_OUTPUT("HPL"),
82 SND_SOC_DAPM_OUTPUT("OUTR"),
83 SND_SOC_DAPM_OUTPUT("HPR"),
84 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM
, 0, 0, NULL
, 0),
85 SND_SOC_DAPM_INPUT("INL"),
86 SND_SOC_DAPM_INPUT("INR"),
89 static const struct snd_soc_dapm_route max9850_dapm_routes
[] = {
91 {"Output Mixer", NULL
, "DAC"},
92 {"Output Mixer", "Line In Switch", "Line Input"},
95 {"Headphone Output", NULL
, "Output Mixer"},
96 {"HPL", NULL
, "Headphone Output"},
97 {"HPR", NULL
, "Headphone Output"},
98 {"OUTL", NULL
, "Output Mixer"},
99 {"OUTR", NULL
, "Output Mixer"},
102 {"Line Input", NULL
, "INL"},
103 {"Line Input", NULL
, "INR"},
106 {"Output Mixer", NULL
, "Charge Pump 1"},
107 {"Output Mixer", NULL
, "Charge Pump 2"},
108 {"Output Mixer", NULL
, "SHDN"},
109 {"DAC", NULL
, "MCLK"},
112 static int max9850_hw_params(struct snd_pcm_substream
*substream
,
113 struct snd_pcm_hw_params
*params
,
114 struct snd_soc_dai
*dai
)
116 struct snd_soc_codec
*codec
= dai
->codec
;
117 struct max9850_priv
*max9850
= snd_soc_codec_get_drvdata(codec
);
121 if (!max9850
->sysclk
)
124 /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
125 sf
= (snd_soc_read(codec
, MAX9850_CLOCK
) >> 2) + 1;
126 lrclk_div
= (1 << 22);
127 lrclk_div
*= params_rate(params
);
129 do_div(lrclk_div
, max9850
->sysclk
);
131 snd_soc_write(codec
, MAX9850_LRCLK_MSB
, (lrclk_div
>> 8) & 0x7f);
132 snd_soc_write(codec
, MAX9850_LRCLK_LSB
, lrclk_div
& 0xff);
134 switch (params_format(params
)) {
135 case SNDRV_PCM_FORMAT_S16_LE
:
138 case SNDRV_PCM_FORMAT_S20_3LE
:
141 case SNDRV_PCM_FORMAT_S24_LE
:
147 snd_soc_update_bits(codec
, MAX9850_DIGITAL_AUDIO
, 0x3, da
);
152 static int max9850_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
153 int clk_id
, unsigned int freq
, int dir
)
155 struct snd_soc_codec
*codec
= codec_dai
->codec
;
156 struct max9850_priv
*max9850
= snd_soc_codec_get_drvdata(codec
);
158 /* calculate mclk -> iclk divider */
159 if (freq
<= 13000000)
160 snd_soc_write(codec
, MAX9850_CLOCK
, 0x0);
161 else if (freq
<= 26000000)
162 snd_soc_write(codec
, MAX9850_CLOCK
, 0x4);
163 else if (freq
<= 40000000)
164 snd_soc_write(codec
, MAX9850_CLOCK
, 0x8);
168 max9850
->sysclk
= freq
;
172 static int max9850_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
174 struct snd_soc_codec
*codec
= codec_dai
->codec
;
177 /* set master/slave audio interface */
178 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
179 case SND_SOC_DAIFMT_CBM_CFM
:
180 da
|= MAX9850_MASTER
;
182 case SND_SOC_DAIFMT_CBS_CFS
:
188 /* interface format */
189 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
190 case SND_SOC_DAIFMT_I2S
:
193 case SND_SOC_DAIFMT_RIGHT_J
:
196 case SND_SOC_DAIFMT_LEFT_J
:
202 /* clock inversion */
203 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
204 case SND_SOC_DAIFMT_NB_NF
:
206 case SND_SOC_DAIFMT_IB_IF
:
207 da
|= MAX9850_BCINV
| MAX9850_INV
;
209 case SND_SOC_DAIFMT_IB_NF
:
212 case SND_SOC_DAIFMT_NB_IF
:
220 snd_soc_write(codec
, MAX9850_DIGITAL_AUDIO
, da
);
225 static int max9850_set_bias_level(struct snd_soc_codec
*codec
,
226 enum snd_soc_bias_level level
)
231 case SND_SOC_BIAS_ON
:
233 case SND_SOC_BIAS_PREPARE
:
235 case SND_SOC_BIAS_STANDBY
:
236 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
237 ret
= snd_soc_cache_sync(codec
);
240 "Failed to sync cache: %d\n", ret
);
245 case SND_SOC_BIAS_OFF
:
248 codec
->dapm
.bias_level
= level
;
252 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
254 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
255 SNDRV_PCM_FMTBIT_S24_LE)
257 static const struct snd_soc_dai_ops max9850_dai_ops
= {
258 .hw_params
= max9850_hw_params
,
259 .set_sysclk
= max9850_set_dai_sysclk
,
260 .set_fmt
= max9850_set_dai_fmt
,
263 static struct snd_soc_dai_driver max9850_dai
= {
264 .name
= "max9850-hifi",
266 .stream_name
= "Playback",
269 .rates
= MAX9850_RATES
,
270 .formats
= MAX9850_FORMATS
272 .ops
= &max9850_dai_ops
,
276 static int max9850_suspend(struct snd_soc_codec
*codec
)
278 max9850_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
283 static int max9850_resume(struct snd_soc_codec
*codec
)
285 max9850_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
290 #define max9850_suspend NULL
291 #define max9850_resume NULL
294 static int max9850_probe(struct snd_soc_codec
*codec
)
298 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, SND_SOC_I2C
);
300 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
304 /* enable zero-detect */
305 snd_soc_update_bits(codec
, MAX9850_GENERAL_PURPOSE
, 1, 1);
306 /* enable slew-rate control */
307 snd_soc_update_bits(codec
, MAX9850_VOLUME
, 0x40, 0x40);
308 /* set slew-rate 125ms */
309 snd_soc_update_bits(codec
, MAX9850_CHARGE_PUMP
, 0xff, 0xc0);
314 static struct snd_soc_codec_driver soc_codec_dev_max9850
= {
315 .probe
= max9850_probe
,
316 .suspend
= max9850_suspend
,
317 .resume
= max9850_resume
,
318 .set_bias_level
= max9850_set_bias_level
,
319 .reg_cache_size
= ARRAY_SIZE(max9850_reg
),
320 .reg_word_size
= sizeof(u8
),
321 .reg_cache_default
= max9850_reg
,
322 .volatile_register
= max9850_volatile_register
,
324 .controls
= max9850_controls
,
325 .num_controls
= ARRAY_SIZE(max9850_controls
),
326 .dapm_widgets
= max9850_dapm_widgets
,
327 .num_dapm_widgets
= ARRAY_SIZE(max9850_dapm_widgets
),
328 .dapm_routes
= max9850_dapm_routes
,
329 .num_dapm_routes
= ARRAY_SIZE(max9850_dapm_routes
),
332 static int __devinit
max9850_i2c_probe(struct i2c_client
*i2c
,
333 const struct i2c_device_id
*id
)
335 struct max9850_priv
*max9850
;
338 max9850
= devm_kzalloc(&i2c
->dev
, sizeof(struct max9850_priv
),
343 i2c_set_clientdata(i2c
, max9850
);
345 ret
= snd_soc_register_codec(&i2c
->dev
,
346 &soc_codec_dev_max9850
, &max9850_dai
, 1);
350 static __devexit
int max9850_i2c_remove(struct i2c_client
*client
)
352 snd_soc_unregister_codec(&client
->dev
);
356 static const struct i2c_device_id max9850_i2c_id
[] = {
360 MODULE_DEVICE_TABLE(i2c
, max9850_i2c_id
);
362 static struct i2c_driver max9850_i2c_driver
= {
365 .owner
= THIS_MODULE
,
367 .probe
= max9850_i2c_probe
,
368 .remove
= __devexit_p(max9850_i2c_remove
),
369 .id_table
= max9850_i2c_id
,
372 static int __init
max9850_init(void)
374 return i2c_add_driver(&max9850_i2c_driver
);
376 module_init(max9850_init
);
378 static void __exit
max9850_exit(void)
380 i2c_del_driver(&max9850_i2c_driver
);
382 module_exit(max9850_exit
);
384 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
385 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
386 MODULE_LICENSE("GPL");