4 * Copyright 2007 Wolfson Microelectronics PLC.
5 * Author: Graeme Gregory
6 * graeme.gregory@wolfsonmicro.com
7 * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/soc.h>
23 #include <sound/tlv.h>
26 struct i2c_client
*i2c
;
30 static const uint8_t lm4857_default_regs
[] = {
31 0x00, 0x00, 0x00, 0x00,
34 /* The register offsets in the cache array */
40 /* the shifts required to set these bits */
42 #define LM4857_WAKEUP 5
43 #define LM4857_EPGAIN 4
45 static int lm4857_write(struct snd_soc_codec
*codec
, unsigned int reg
,
51 ret
= snd_soc_cache_write(codec
, reg
, value
);
55 data
= (reg
<< 6) | value
;
56 ret
= i2c_master_send(codec
->control_data
, &data
, 1);
58 dev_err(codec
->dev
, "Failed to write register: %d\n", ret
);
65 static unsigned int lm4857_read(struct snd_soc_codec
*codec
,
71 ret
= snd_soc_cache_read(codec
, reg
, &val
);
78 static int lm4857_get_mode(struct snd_kcontrol
*kcontrol
,
79 struct snd_ctl_elem_value
*ucontrol
)
81 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
82 struct lm4857
*lm4857
= snd_soc_codec_get_drvdata(codec
);
84 ucontrol
->value
.integer
.value
[0] = lm4857
->mode
;
89 static int lm4857_set_mode(struct snd_kcontrol
*kcontrol
,
90 struct snd_ctl_elem_value
*ucontrol
)
92 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
93 struct lm4857
*lm4857
= snd_soc_codec_get_drvdata(codec
);
94 uint8_t value
= ucontrol
->value
.integer
.value
[0];
98 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_ON
)
99 snd_soc_update_bits(codec
, LM4857_CTRL
, 0x0F, value
+ 6);
104 static int lm4857_set_bias_level(struct snd_soc_codec
*codec
,
105 enum snd_soc_bias_level level
)
107 struct lm4857
*lm4857
= snd_soc_codec_get_drvdata(codec
);
110 case SND_SOC_BIAS_ON
:
111 snd_soc_update_bits(codec
, LM4857_CTRL
, 0x0F, lm4857
->mode
+ 6);
113 case SND_SOC_BIAS_STANDBY
:
114 snd_soc_update_bits(codec
, LM4857_CTRL
, 0x0F, 0);
120 codec
->dapm
.bias_level
= level
;
125 static const char *lm4857_mode
[] = {
128 "Loudspeaker + Headphone",
132 static const struct soc_enum lm4857_mode_enum
=
133 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode
), lm4857_mode
);
135 static const struct snd_soc_dapm_widget lm4857_dapm_widgets
[] = {
136 SND_SOC_DAPM_INPUT("IN"),
138 SND_SOC_DAPM_OUTPUT("LS"),
139 SND_SOC_DAPM_OUTPUT("HP"),
140 SND_SOC_DAPM_OUTPUT("EP"),
143 static const DECLARE_TLV_DB_SCALE(stereo_tlv
, -4050, 150, 0);
144 static const DECLARE_TLV_DB_SCALE(mono_tlv
, -3450, 150, 0);
146 static const struct snd_kcontrol_new lm4857_controls
[] = {
147 SOC_SINGLE_TLV("Left Playback Volume", LM4857_LVOL
, 0, 31, 0,
149 SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL
, 0, 31, 0,
151 SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL
, 0, 31, 0,
153 SOC_SINGLE("Spk 3D Playback Switch", LM4857_LVOL
, LM4857_3D
, 1, 0),
154 SOC_SINGLE("HP 3D Playback Switch", LM4857_RVOL
, LM4857_3D
, 1, 0),
155 SOC_SINGLE("Fast Wakeup Playback Switch", LM4857_CTRL
,
156 LM4857_WAKEUP
, 1, 0),
157 SOC_SINGLE("Earpiece 6dB Playback Switch", LM4857_CTRL
,
158 LM4857_EPGAIN
, 1, 0),
160 SOC_ENUM_EXT("Mode", lm4857_mode_enum
,
161 lm4857_get_mode
, lm4857_set_mode
),
164 /* There is a demux between the input signal and the output signals.
165 * Currently there is no easy way to model it in ASoC and since it does not make
166 * much of a difference in practice simply connect the input direclty to the
168 static const struct snd_soc_dapm_route lm4857_routes
[] = {
174 static int lm4857_probe(struct snd_soc_codec
*codec
)
176 struct lm4857
*lm4857
= snd_soc_codec_get_drvdata(codec
);
177 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
180 codec
->control_data
= lm4857
->i2c
;
182 ret
= snd_soc_add_codec_controls(codec
, lm4857_controls
,
183 ARRAY_SIZE(lm4857_controls
));
187 ret
= snd_soc_dapm_new_controls(dapm
, lm4857_dapm_widgets
,
188 ARRAY_SIZE(lm4857_dapm_widgets
));
192 ret
= snd_soc_dapm_add_routes(dapm
, lm4857_routes
,
193 ARRAY_SIZE(lm4857_routes
));
197 snd_soc_dapm_new_widgets(dapm
);
202 static struct snd_soc_codec_driver soc_codec_dev_lm4857
= {
203 .write
= lm4857_write
,
205 .probe
= lm4857_probe
,
206 .reg_cache_size
= ARRAY_SIZE(lm4857_default_regs
),
207 .reg_word_size
= sizeof(uint8_t),
208 .reg_cache_default
= lm4857_default_regs
,
209 .set_bias_level
= lm4857_set_bias_level
,
212 static int __devinit
lm4857_i2c_probe(struct i2c_client
*i2c
,
213 const struct i2c_device_id
*id
)
215 struct lm4857
*lm4857
;
218 lm4857
= devm_kzalloc(&i2c
->dev
, sizeof(*lm4857
), GFP_KERNEL
);
222 i2c_set_clientdata(i2c
, lm4857
);
226 ret
= snd_soc_register_codec(&i2c
->dev
, &soc_codec_dev_lm4857
, NULL
, 0);
231 static int __devexit
lm4857_i2c_remove(struct i2c_client
*i2c
)
233 snd_soc_unregister_codec(&i2c
->dev
);
237 static const struct i2c_device_id lm4857_i2c_id
[] = {
241 MODULE_DEVICE_TABLE(i2c
, lm4857_i2c_id
);
243 static struct i2c_driver lm4857_i2c_driver
= {
246 .owner
= THIS_MODULE
,
248 .probe
= lm4857_i2c_probe
,
249 .remove
= __devexit_p(lm4857_i2c_remove
),
250 .id_table
= lm4857_i2c_id
,
253 static int __init
lm4857_init(void)
255 return i2c_add_driver(&lm4857_i2c_driver
);
257 module_init(lm4857_init
);
259 static void __exit
lm4857_exit(void)
261 i2c_del_driver(&lm4857_i2c_driver
);
263 module_exit(lm4857_exit
);
265 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
266 MODULE_DESCRIPTION("LM4857 amplifier driver");
267 MODULE_LICENSE("GPL");