I2C: Add helper macro for i2c_driver boilerplate
[zen-stable.git] / sound / soc / codecs / lm4857.c
blobc387dafc6ab6970245f03ec8ad6a32712e0c31fd
1 /*
2 * LM4857 AMP driver
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>
25 struct lm4857 {
26 struct i2c_client *i2c;
27 uint8_t mode;
30 static const uint8_t lm4857_default_regs[] = {
31 0x00, 0x00, 0x00, 0x00,
34 /* The register offsets in the cache array */
35 #define LM4857_MVOL 0
36 #define LM4857_LVOL 1
37 #define LM4857_RVOL 2
38 #define LM4857_CTRL 3
40 /* the shifts required to set these bits */
41 #define LM4857_3D 5
42 #define LM4857_WAKEUP 5
43 #define LM4857_EPGAIN 4
45 static int lm4857_write(struct snd_soc_codec *codec, unsigned int reg,
46 unsigned int value)
48 uint8_t data;
49 int ret;
51 ret = snd_soc_cache_write(codec, reg, value);
52 if (ret < 0)
53 return ret;
55 data = (reg << 6) | value;
56 ret = i2c_master_send(codec->control_data, &data, 1);
57 if (ret != 1) {
58 dev_err(codec->dev, "Failed to write register: %d\n", ret);
59 return ret;
62 return 0;
65 static unsigned int lm4857_read(struct snd_soc_codec *codec,
66 unsigned int reg)
68 unsigned int val;
69 int ret;
71 ret = snd_soc_cache_read(codec, reg, &val);
72 if (ret)
73 return -1;
75 return 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;
86 return 0;
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];
96 lm4857->mode = value;
98 if (codec->dapm.bias_level == SND_SOC_BIAS_ON)
99 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, value + 6);
101 return 1;
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);
109 switch (level) {
110 case SND_SOC_BIAS_ON:
111 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, lm4857->mode + 6);
112 break;
113 case SND_SOC_BIAS_STANDBY:
114 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, 0);
115 break;
116 default:
117 break;
120 codec->dapm.bias_level = level;
122 return 0;
125 static const char *lm4857_mode[] = {
126 "Earpiece",
127 "Loudspeaker",
128 "Loudspeaker + Headphone",
129 "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,
148 stereo_tlv),
149 SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL, 0, 31, 0,
150 stereo_tlv),
151 SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
152 mono_tlv),
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
167 * outputs. */
168 static const struct snd_soc_dapm_route lm4857_routes[] = {
169 {"LS", NULL, "IN"},
170 {"HP", NULL, "IN"},
171 {"EP", NULL, "IN"},
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;
178 int ret;
180 codec->control_data = lm4857->i2c;
182 ret = snd_soc_add_controls(codec, lm4857_controls,
183 ARRAY_SIZE(lm4857_controls));
184 if (ret)
185 return ret;
187 ret = snd_soc_dapm_new_controls(dapm, lm4857_dapm_widgets,
188 ARRAY_SIZE(lm4857_dapm_widgets));
189 if (ret)
190 return ret;
192 ret = snd_soc_dapm_add_routes(dapm, lm4857_routes,
193 ARRAY_SIZE(lm4857_routes));
194 if (ret)
195 return ret;
197 snd_soc_dapm_new_widgets(dapm);
199 return 0;
202 static struct snd_soc_codec_driver soc_codec_dev_lm4857 = {
203 .write = lm4857_write,
204 .read = lm4857_read,
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;
216 int ret;
218 lm4857 = kzalloc(sizeof(*lm4857), GFP_KERNEL);
219 if (!lm4857)
220 return -ENOMEM;
222 i2c_set_clientdata(i2c, lm4857);
224 lm4857->i2c = i2c;
226 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0);
228 if (ret) {
229 kfree(lm4857);
230 return ret;
233 return 0;
236 static int __devexit lm4857_i2c_remove(struct i2c_client *i2c)
238 struct lm4857 *lm4857 = i2c_get_clientdata(i2c);
240 snd_soc_unregister_codec(&i2c->dev);
241 kfree(lm4857);
243 return 0;
246 static const struct i2c_device_id lm4857_i2c_id[] = {
247 { "lm4857", 0 },
250 MODULE_DEVICE_TABLE(i2c, lm4857_i2c_id);
252 static struct i2c_driver lm4857_i2c_driver = {
253 .driver = {
254 .name = "lm4857",
255 .owner = THIS_MODULE,
257 .probe = lm4857_i2c_probe,
258 .remove = __devexit_p(lm4857_i2c_remove),
259 .id_table = lm4857_i2c_id,
262 static int __init lm4857_init(void)
264 return i2c_add_driver(&lm4857_i2c_driver);
266 module_init(lm4857_init);
268 static void __exit lm4857_exit(void)
270 i2c_del_driver(&lm4857_i2c_driver);
272 module_exit(lm4857_exit);
274 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
275 MODULE_DESCRIPTION("LM4857 amplifier driver");
276 MODULE_LICENSE("GPL");