1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* max9877.c -- amp driver for max9877
*
* Copyright (C) 2009 Samsung Electronics Co.Ltd
* Author: Joonyoung Shim <jy0922.shim@samsung.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#include "max9877.h"
static struct regmap *regmap;
static struct reg_default max9877_regs[] = {
{ 0, 0x40 },
{ 1, 0x00 },
{ 2, 0x00 },
{ 3, 0x00 },
{ 4, 0x49 },
};
static int max9877_get_reg(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int reg = mc->reg;
unsigned int shift = mc->shift;
unsigned int mask = mc->max;
unsigned int invert = mc->invert;
unsigned int val;
int ret;
ret = regmap_read(regmap, reg, &val);
if (ret != 0)
return ret;
ucontrol->value.integer.value[0] = (val >> shift) & mask;
if (invert)
ucontrol->value.integer.value[0] =
mask - ucontrol->value.integer.value[0];
return 0;
}
static int max9877_set_reg(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int reg = mc->reg;
unsigned int shift = mc->shift;
unsigned int mask = mc->max;
unsigned int invert = mc->invert;
unsigned int val = (ucontrol->value.integer.value[0] & mask);
bool change;
int ret;
if (invert)
val = mask - val;
ret = regmap_update_bits_check(regmap, reg, mask << shift,
val << shift, &change);
if (ret != 0)
return ret;
if (change)
return 1;
else
return 0;
}
static int max9877_get_2reg(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int reg = mc->reg;
unsigned int reg2 = mc->rreg;
unsigned int shift = mc->shift;
unsigned int mask = mc->max;
unsigned int val;
int ret;
ret = regmap_read(regmap, reg, &val);
if (ret != 0)
return ret;
ucontrol->value.integer.value[0] = (val >> shift) & mask;
ret = regmap_read(regmap, reg2, &val);
if (ret != 0)
return ret;
ucontrol->value.integer.value[1] = (val >> shift) & mask;
return 0;
}
static int max9877_set_2reg(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int reg = mc->reg;
unsigned int reg2 = mc->rreg;
unsigned int shift = mc->shift;
unsigned int mask = mc->max;
unsigned int val = (ucontrol->value.integer.value[0] & mask);
unsigned int val2 = (ucontrol->value.integer.value[1] & mask);
bool change1, change2;
int ret;
ret = regmap_update_bits_check(regmap, reg, mask << shift,
val << shift, &change1);
if (ret != 0)
return ret;
ret = regmap_update_bits_check(regmap, reg2, mask << shift,
val2 << shift, &change2);
if (ret != 0)
return ret;
if (change1 || change2)
return 1;
else
return 0;
}
static int max9877_get_out_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
unsigned int val;
int ret;
ret = regmap_read(regmap, MAX9877_OUTPUT_MODE, &val);
if (ret != 0)
return ret;
val &= MAX9877_OUTMODE_MASK;
if (val)
val--;
ucontrol->value.integer.value[0] = val;
return 0;
}
static int max9877_set_out_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
unsigned int val;
bool change;
int ret;
val = ucontrol->value.integer.value[0] + 1;
ret = regmap_update_bits_check(regmap, MAX9877_OUTPUT_MODE,
MAX9877_OUTMODE_MASK, val, &change);
if (ret != 0)
return ret;
if (change)
return 1;
else
return 0;
}
static int max9877_get_osc_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
unsigned int val;
int ret;
ret = regmap_read(regmap, MAX9877_OUTPUT_MODE, &val);
if (ret != 0)
return ret;
val &= MAX9877_OSC_MASK;
val >>= MAX9877_OSC_OFFSET;
ucontrol->value.integer.value[0] = val;
return 0;
}
static int max9877_set_osc_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
unsigned int val;
bool change;
int ret;
val = ucontrol->value.integer.value[0] << MAX9877_OSC_OFFSET;
ret = regmap_update_bits_check(regmap, MAX9877_OUTPUT_MODE,
MAX9877_OSC_MASK, val, &change);
if (ret != 0)
return ret;
if (change)
return 1;
else
return 0;
}
static const unsigned int max9877_pgain_tlv[] = {
TLV_DB_RANGE_HEAD(2),
0, 1, TLV_DB_SCALE_ITEM(0, 900, 0),
2, 2, TLV_DB_SCALE_ITEM(2000, 0, 0),
};
static const unsigned int max9877_output_tlv[] = {
TLV_DB_RANGE_HEAD(4),
0, 7, TLV_DB_SCALE_ITEM(-7900, 400, 1),
8, 15, TLV_DB_SCALE_ITEM(-4700, 300, 0),
16, 23, TLV_DB_SCALE_ITEM(-2300, 200, 0),
24, 31, TLV_DB_SCALE_ITEM(-700, 100, 0),
};
static const char *max9877_out_mode[] = {
"INA -> SPK",
"INA -> HP",
"INA -> SPK and HP",
"INB -> SPK",
"INB -> HP",
"INB -> SPK and HP",
"INA + INB -> SPK",
"INA + INB -> HP",
"INA + INB -> SPK and HP",
};
static const char *max9877_osc_mode[] = {
"1176KHz",
"1100KHz",
"700KHz",
};
static const struct soc_enum max9877_enum[] = {
SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(max9877_out_mode), max9877_out_mode),
SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(max9877_osc_mode), max9877_osc_mode),
};
static const struct snd_kcontrol_new max9877_controls[] = {
SOC_SINGLE_EXT_TLV("MAX9877 PGAINA Playback Volume",
MAX9877_INPUT_MODE, 0, 2, 0,
max9877_get_reg, max9877_set_reg, max9877_pgain_tlv),
SOC_SINGLE_EXT_TLV("MAX9877 PGAINB Playback Volume",
MAX9877_INPUT_MODE, 2, 2, 0,
max9877_get_reg, max9877_set_reg, max9877_pgain_tlv),
SOC_SINGLE_EXT_TLV("MAX9877 Amp Speaker Playback Volume",
MAX9877_SPK_VOLUME, 0, 31, 0,
max9877_get_reg, max9877_set_reg, max9877_output_tlv),
SOC_DOUBLE_R_EXT_TLV("MAX9877 Amp HP Playback Volume",
MAX9877_HPL_VOLUME, MAX9877_HPR_VOLUME, 0, 31, 0,
max9877_get_2reg, max9877_set_2reg, max9877_output_tlv),
SOC_SINGLE_EXT("MAX9877 INB Stereo Switch",
MAX9877_INPUT_MODE, 4, 1, 1,
max9877_get_reg, max9877_set_reg),
SOC_SINGLE_EXT("MAX9877 INA Stereo Switch",
MAX9877_INPUT_MODE, 5, 1, 1,
max9877_get_reg, max9877_set_reg),
SOC_SINGLE_EXT("MAX9877 Zero-crossing detection Switch",
MAX9877_INPUT_MODE, 6, 1, 0,
max9877_get_reg, max9877_set_reg),
SOC_SINGLE_EXT("MAX9877 Bypass Mode Switch",
MAX9877_OUTPUT_MODE, 6, 1, 0,
max9877_get_reg, max9877_set_reg),
SOC_SINGLE_EXT("MAX9877 Shutdown Mode Switch",
MAX9877_OUTPUT_MODE, 7, 1, 1,
max9877_get_reg, max9877_set_reg),
SOC_ENUM_EXT("MAX9877 Output Mode", max9877_enum[0],
max9877_get_out_mode, max9877_set_out_mode),
SOC_ENUM_EXT("MAX9877 Oscillator Mode", max9877_enum[1],
max9877_get_osc_mode, max9877_set_osc_mode),
};
/* This function is called from ASoC machine driver */
int max9877_add_controls(struct snd_soc_codec *codec)
{
return snd_soc_add_codec_controls(codec, max9877_controls,
ARRAY_SIZE(max9877_controls));
}
EXPORT_SYMBOL_GPL(max9877_add_controls);
static const struct regmap_config max9877_regmap = {
.reg_bits = 8,
.val_bits = 8,
.reg_defaults = max9877_regs,
.num_reg_defaults = ARRAY_SIZE(max9877_regs),
.cache_type = REGCACHE_RBTREE,
};
static int max9877_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int i;
regmap = devm_regmap_init_i2c(client, &max9877_regmap);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
/* Ensure the device is in reset state */
for (i = 0; i < ARRAY_SIZE(max9877_regs); i++)
regmap_write(regmap, max9877_regs[i].reg, max9877_regs[i].def);
return 0;
}
static int max9877_i2c_remove(struct i2c_client *client)
{
regmap = NULL;
return 0;
}
static const struct i2c_device_id max9877_i2c_id[] = {
{ "max9877", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, max9877_i2c_id);
static struct i2c_driver max9877_i2c_driver = {
.driver = {
.name = "max9877",
.owner = THIS_MODULE,
},
.probe = max9877_i2c_probe,
.remove = max9877_i2c_remove,
.id_table = max9877_i2c_id,
};
module_i2c_driver(max9877_i2c_driver);
MODULE_DESCRIPTION("ASoC MAX9877 amp driver");
MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
MODULE_LICENSE("GPL");
|