lm4857.c (07ccc0f4f190070aaba8fb587307f7fefad97981) lm4857.c (9b2709687a81297bca53f98100b6f13cd5e05b44)
1/*
2 * LM4857 AMP driver
3 *
4 * Copyright 2007 Wolfson Microelectronics PLC.
5 * Author: Graeme Gregory
6 * graeme.gregory@wolfsonmicro.com
7 * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de>
8 *
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.
13 *
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/i2c.h>
1/*
2 * LM4857 AMP driver
3 *
4 * Copyright 2007 Wolfson Microelectronics PLC.
5 * Author: Graeme Gregory
6 * graeme.gregory@wolfsonmicro.com
7 * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de>
8 *
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.
13 *
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/regmap.h>
19#include <linux/slab.h>
20
21#include <sound/core.h>
22#include <sound/soc.h>
23#include <sound/tlv.h>
24
25struct lm4857 {
20#include <linux/slab.h>
21
22#include <sound/core.h>
23#include <sound/soc.h>
24#include <sound/tlv.h>
25
26struct lm4857 {
26 struct i2c_client *i2c;
27 struct regmap *regmap;
27 uint8_t mode;
28};
29
28 uint8_t mode;
29};
30
30static const uint8_t lm4857_default_regs[] = {
31 0x00, 0x00, 0x00, 0x00,
31static const struct reg_default lm4857_default_regs[] = {
32 { 0x0, 0x00 },
33 { 0x1, 0x00 },
34 { 0x2, 0x00 },
35 { 0x3, 0x00 },
32};
33
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
39
40/* the shifts required to set these bits */
41#define LM4857_3D 5
42#define LM4857_WAKEUP 5
43#define LM4857_EPGAIN 4
44
36};
37
38/* The register offsets in the cache array */
39#define LM4857_MVOL 0
40#define LM4857_LVOL 1
41#define LM4857_RVOL 2
42#define LM4857_CTRL 3
43
44/* the shifts required to set these bits */
45#define LM4857_3D 5
46#define LM4857_WAKEUP 5
47#define LM4857_EPGAIN 4
48
45static int lm4857_write(struct snd_soc_codec *codec, unsigned int reg,
46 unsigned int value)
47{
48 uint8_t data;
49 int ret;
50
51 ret = snd_soc_cache_write(codec, reg, value);
52 if (ret < 0)
53 return ret;
54
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;
60 }
61
62 return 0;
63}
64
65static unsigned int lm4857_read(struct snd_soc_codec *codec,
66 unsigned int reg)
67{
68 unsigned int val;
69 int ret;
70
71 ret = snd_soc_cache_read(codec, reg, &val);
72 if (ret)
73 return -1;
74
75 return val;
76}
77
78static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
79 struct snd_ctl_elem_value *ucontrol)
80{
81 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
82 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
83
84 ucontrol->value.integer.value[0] = lm4857->mode;
85

--- 5 unchanged lines hidden (view full) ---

91{
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];
95
96 lm4857->mode = value;
97
98 if (codec->dapm.bias_level == SND_SOC_BIAS_ON)
49static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
50 struct snd_ctl_elem_value *ucontrol)
51{
52 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
53 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
54
55 ucontrol->value.integer.value[0] = lm4857->mode;
56

--- 5 unchanged lines hidden (view full) ---

62{
63 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
64 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
65 uint8_t value = ucontrol->value.integer.value[0];
66
67 lm4857->mode = value;
68
69 if (codec->dapm.bias_level == SND_SOC_BIAS_ON)
99 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, value + 6);
70 regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, value + 6);
100
101 return 1;
102}
103
104static int lm4857_set_bias_level(struct snd_soc_codec *codec,
105 enum snd_soc_bias_level level)
106{
107 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
108
109 switch (level) {
110 case SND_SOC_BIAS_ON:
71
72 return 1;
73}
74
75static int lm4857_set_bias_level(struct snd_soc_codec *codec,
76 enum snd_soc_bias_level level)
77{
78 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
79
80 switch (level) {
81 case SND_SOC_BIAS_ON:
111 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, lm4857->mode + 6);
82 regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F,
83 lm4857->mode + 6);
112 break;
113 case SND_SOC_BIAS_STANDBY:
84 break;
85 case SND_SOC_BIAS_STANDBY:
114 snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, 0);
86 regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, 0);
115 break;
116 default:
117 break;
118 }
119
120 codec->dapm.bias_level = level;
121
122 return 0;

--- 43 unchanged lines hidden (view full) ---

166 * much of a difference in practice simply connect the input direclty to the
167 * outputs. */
168static const struct snd_soc_dapm_route lm4857_routes[] = {
169 {"LS", NULL, "IN"},
170 {"HP", NULL, "IN"},
171 {"EP", NULL, "IN"},
172};
173
87 break;
88 default:
89 break;
90 }
91
92 codec->dapm.bias_level = level;
93
94 return 0;

--- 43 unchanged lines hidden (view full) ---

138 * much of a difference in practice simply connect the input direclty to the
139 * outputs. */
140static const struct snd_soc_dapm_route lm4857_routes[] = {
141 {"LS", NULL, "IN"},
142 {"HP", NULL, "IN"},
143 {"EP", NULL, "IN"},
144};
145
174static int lm4857_probe(struct snd_soc_codec *codec)
175{
176 struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
177
178 codec->control_data = lm4857->i2c;
179
180 return 0;
181}
182
183static struct snd_soc_codec_driver soc_codec_dev_lm4857 = {
146static struct snd_soc_codec_driver soc_codec_dev_lm4857 = {
184 .write = lm4857_write,
185 .read = lm4857_read,
186 .probe = lm4857_probe,
187 .reg_cache_size = ARRAY_SIZE(lm4857_default_regs),
188 .reg_word_size = sizeof(uint8_t),
189 .reg_cache_default = lm4857_default_regs,
190 .set_bias_level = lm4857_set_bias_level,
191
192 .controls = lm4857_controls,
193 .num_controls = ARRAY_SIZE(lm4857_controls),
194 .dapm_widgets = lm4857_dapm_widgets,
195 .num_dapm_widgets = ARRAY_SIZE(lm4857_dapm_widgets),
196 .dapm_routes = lm4857_routes,
197 .num_dapm_routes = ARRAY_SIZE(lm4857_routes),
198};
199
147 .set_bias_level = lm4857_set_bias_level,
148
149 .controls = lm4857_controls,
150 .num_controls = ARRAY_SIZE(lm4857_controls),
151 .dapm_widgets = lm4857_dapm_widgets,
152 .num_dapm_widgets = ARRAY_SIZE(lm4857_dapm_widgets),
153 .dapm_routes = lm4857_routes,
154 .num_dapm_routes = ARRAY_SIZE(lm4857_routes),
155};
156
157static const struct regmap_config lm4857_regmap_config = {
158 .val_bits = 6,
159 .reg_bits = 2,
160
161 .max_register = LM4857_CTRL,
162
163 .cache_type = REGCACHE_FLAT,
164 .reg_defaults = lm4857_default_regs,
165 .num_reg_defaults = ARRAY_SIZE(lm4857_default_regs),
166};
167
200static int lm4857_i2c_probe(struct i2c_client *i2c,
201 const struct i2c_device_id *id)
202{
203 struct lm4857 *lm4857;
168static int lm4857_i2c_probe(struct i2c_client *i2c,
169 const struct i2c_device_id *id)
170{
171 struct lm4857 *lm4857;
204 int ret;
205
206 lm4857 = devm_kzalloc(&i2c->dev, sizeof(*lm4857), GFP_KERNEL);
207 if (!lm4857)
208 return -ENOMEM;
209
210 i2c_set_clientdata(i2c, lm4857);
211
172
173 lm4857 = devm_kzalloc(&i2c->dev, sizeof(*lm4857), GFP_KERNEL);
174 if (!lm4857)
175 return -ENOMEM;
176
177 i2c_set_clientdata(i2c, lm4857);
178
212 lm4857->i2c = i2c;
179 lm4857->regmap = devm_regmap_init_i2c(i2c, &lm4857_regmap_config);
180 if (IS_ERR(lm4857->regmap))
181 return PTR_ERR(lm4857->regmap);
213
182
214 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0);
215
216 return ret;
183 return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0);
217}
218
219static int lm4857_i2c_remove(struct i2c_client *i2c)
220{
221 snd_soc_unregister_codec(&i2c->dev);
222 return 0;
223}
224

--- 21 unchanged lines hidden ---
184}
185
186static int lm4857_i2c_remove(struct i2c_client *i2c)
187{
188 snd_soc_unregister_codec(&i2c->dev);
189 return 0;
190}
191

--- 21 unchanged lines hidden ---