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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* max98371.c -- ALSA SoC Stereo MAX98371 driver
*
* Copyright 2015-16 Maxim Integrated Products
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#include "max98371.h"
static const char *const monomix_text[] = {
"Left", "Right", "LeftRightDiv2",
};
static const char *const hpf_cutoff_txt[] = {
"Disable", "DC Block", "50Hz",
"100Hz", "200Hz", "400Hz", "800Hz",
};
static SOC_ENUM_SINGLE_DECL(max98371_monomix, MAX98371_MONOMIX_CFG, 0,
monomix_text);
static SOC_ENUM_SINGLE_DECL(max98371_hpf_cutoff, MAX98371_HPF, 0,
hpf_cutoff_txt);
static const DECLARE_TLV_DB_RANGE(max98371_dht_min_gain,
0, 1, TLV_DB_SCALE_ITEM(537, 66, 0),
2, 3, TLV_DB_SCALE_ITEM(677, 82, 0),
4, 5, TLV_DB_SCALE_ITEM(852, 104, 0),
6, 7, TLV_DB_SCALE_ITEM(1072, 131, 0),
8, 9, TLV_DB_SCALE_ITEM(1350, 165, 0),
10, 11, TLV_DB_SCALE_ITEM(1699, 101, 0),
);
static const DECLARE_TLV_DB_RANGE(max98371_dht_max_gain,
0, 1, TLV_DB_SCALE_ITEM(537, 66, 0),
2, 3, TLV_DB_SCALE_ITEM(677, 82, 0),
4, 5, TLV_DB_SCALE_ITEM(852, 104, 0),
6, 7, TLV_DB_SCALE_ITEM(1072, 131, 0),
8, 9, TLV_DB_SCALE_ITEM(1350, 165, 0),
10, 11, TLV_DB_SCALE_ITEM(1699, 208, 0),
);
static const DECLARE_TLV_DB_RANGE(max98371_dht_rot_gain,
0, 1, TLV_DB_SCALE_ITEM(-50, -50, 0),
2, 6, TLV_DB_SCALE_ITEM(-100, -100, 0),
7, 8, TLV_DB_SCALE_ITEM(-800, -200, 0),
9, 11, TLV_DB_SCALE_ITEM(-1200, -300, 0),
12, 13, TLV_DB_SCALE_ITEM(-2000, -200, 0),
14, 15, TLV_DB_SCALE_ITEM(-2500, -500, 0),
);
static const struct reg_default max98371_reg[] = {
{ 0x01, 0x00 },
{ 0x02, 0x00 },
{ 0x03, 0x00 },
{ 0x04, 0x00 },
{ 0x05, 0x00 },
{ 0x06, 0x00 },
{ 0x07, 0x00 },
{ 0x08, 0x00 },
{ 0x09, 0x00 },
{ 0x0A, 0x00 },
{ 0x10, 0x06 },
{ 0x11, 0x08 },
{ 0x14, 0x80 },
{ 0x15, 0x00 },
{ 0x16, 0x00 },
{ 0x18, 0x00 },
{ 0x19, 0x00 },
{ 0x1C, 0x00 },
{ 0x1D, 0x00 },
{ 0x1E, 0x00 },
{ 0x1F, 0x00 },
{ 0x20, 0x00 },
{ 0x21, 0x00 },
{ 0x22, 0x00 },
{ 0x23, 0x00 },
{ 0x24, 0x00 },
{ 0x25, 0x00 },
{ 0x26, 0x00 },
{ 0x27, 0x00 },
{ 0x28, 0x00 },
{ 0x29, 0x00 },
{ 0x2A, 0x00 },
{ 0x2B, 0x00 },
{ 0x2C, 0x00 },
{ 0x2D, 0x00 },
{ 0x2E, 0x0B },
{ 0x31, 0x00 },
{ 0x32, 0x18 },
{ 0x33, 0x00 },
{ 0x34, 0x00 },
{ 0x36, 0x00 },
{ 0x37, 0x00 },
{ 0x38, 0x00 },
{ 0x39, 0x00 },
{ 0x3A, 0x00 },
{ 0x3B, 0x00 },
{ 0x3C, 0x00 },
{ 0x3D, 0x00 },
{ 0x3E, 0x00 },
{ 0x3F, 0x00 },
{ 0x40, 0x00 },
{ 0x41, 0x00 },
{ 0x42, 0x00 },
{ 0x43, 0x00 },
{ 0x4A, 0x00 },
{ 0x4B, 0x00 },
{ 0x4C, 0x00 },
{ 0x4D, 0x00 },
{ 0x4E, 0x00 },
{ 0x50, 0x00 },
{ 0x51, 0x00 },
{ 0x55, 0x00 },
{ 0x58, 0x00 },
{ 0x59, 0x00 },
{ 0x5C, 0x00 },
{ 0xFF, 0x43 },
};
static bool max98371_volatile_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case MAX98371_IRQ_CLEAR1:
case MAX98371_IRQ_CLEAR2:
case MAX98371_IRQ_CLEAR3:
case MAX98371_VERSION:
return true;
default:
return false;
}
}
static bool max98371_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case MAX98371_SOFT_RESET:
return false;
default:
return true;
}
};
static const DECLARE_TLV_DB_RANGE(max98371_gain_tlv,
0, 7, TLV_DB_SCALE_ITEM(0, 50, 0),
8, 10, TLV_DB_SCALE_ITEM(400, 100, 0)
);
static const DECLARE_TLV_DB_RANGE(max98371_noload_gain_tlv,
0, 11, TLV_DB_SCALE_ITEM(950, 100, 0),
);
static const DECLARE_TLV_DB_SCALE(digital_tlv, -6300, 50, 1);
static const struct snd_kcontrol_new max98371_snd_controls[] = {
SOC_SINGLE_TLV("Speaker Volume", MAX98371_GAIN,
MAX98371_GAIN_SHIFT, (1<<MAX98371_GAIN_WIDTH)-1, 0,
max98371_gain_tlv),
SOC_SINGLE_TLV("Digital Volume", MAX98371_DIGITAL_GAIN, 0,
(1<<MAX98371_DIGITAL_GAIN_WIDTH)-1, 1, digital_tlv),
SOC_SINGLE_TLV("Speaker DHT Max Volume", MAX98371_GAIN,
0, (1<<MAX98371_DHT_MAX_WIDTH)-1, 0,
max98371_dht_max_gain),
SOC_SINGLE_TLV("Speaker DHT Min Volume", MAX98371_DHT_GAIN,
0, (1<<MAX98371_DHT_GAIN_WIDTH)-1, 0,
max98371_dht_min_gain),
SOC_SINGLE_TLV("Speaker DHT Rotation Volume", MAX98371_DHT_GAIN,
0, (1<<MAX98371_DHT_ROT_WIDTH)-1, 0,
max98371_dht_rot_gain),
SOC_SINGLE("DHT Attack Step", MAX98371_DHT, MAX98371_DHT_STEP, 3, 0),
SOC_SINGLE("DHT Attack Rate", MAX98371_DHT, 0, 7, 0),
SOC_ENUM("Monomix Select", max98371_monomix),
SOC_ENUM("HPF Cutoff", max98371_hpf_cutoff),
};
static int max98371_dai_set_fmt(struct snd_soc_dai *codec_dai,
unsigned int fmt)
{
struct snd_soc_codec *codec = codec_dai->codec;
struct max98371_priv *max98371 = snd_soc_codec_get_drvdata(codec);
unsigned int val = 0;
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
case SND_SOC_DAIFMT_CBS_CFS:
break;
default:
dev_err(codec->dev, "DAI clock mode unsupported");
return -EINVAL;
}
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_I2S:
val |= 0;
break;
case SND_SOC_DAIFMT_RIGHT_J:
val |= MAX98371_DAI_RIGHT;
break;
case SND_SOC_DAIFMT_LEFT_J:
val |= MAX98371_DAI_LEFT;
break;
default:
dev_err(codec->dev, "DAI wrong mode unsupported");
return -EINVAL;
}
regmap_update_bits(max98371->regmap, MAX98371_FMT,
MAX98371_FMT_MODE_MASK, val);
return 0;
}
static int max98371_dai_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
struct snd_soc_codec *codec = dai->codec;
struct max98371_priv *max98371 = snd_soc_codec_get_drvdata(codec);
int blr_clk_ratio, ch_size, channels = params_channels(params);
int rate = params_rate(params);
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S8:
regmap_update_bits(max98371->regmap, MAX98371_FMT,
MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_16);
ch_size = 8;
break;
case SNDRV_PCM_FORMAT_S16_LE:
regmap_update_bits(max98371->regmap, MAX98371_FMT,
MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_16);
ch_size = 16;
break;
case SNDRV_PCM_FORMAT_S24_LE:
regmap_update_bits(max98371->regmap, MAX98371_FMT,
MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_32);
ch_size = 24;
break;
case SNDRV_PCM_FORMAT_S32_LE:
regmap_update_bits(max98371->regmap, MAX98371_FMT,
MAX98371_FMT_MASK, MAX98371_DAI_CHANSZ_32);
ch_size = 32;
break;
default:
return -EINVAL;
}
/* BCLK/LRCLK ratio calculation */
blr_clk_ratio = channels * ch_size;
switch (blr_clk_ratio) {
case 32:
regmap_update_bits(max98371->regmap,
MAX98371_DAI_CLK,
MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_32);
break;
case 48:
regmap_update_bits(max98371->regmap,
MAX98371_DAI_CLK,
MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_48);
break;
case 64:
regmap_update_bits(max98371->regmap,
MAX98371_DAI_CLK,
MAX98371_DAI_BSEL_MASK, MAX98371_DAI_BSEL_64);
break;
default:
return -EINVAL;
}
switch (rate) {
case 32000:
regmap_update_bits(max98371->regmap,
MAX98371_SPK_SR,
MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_32);
break;
case 44100:
regmap_update_bits(max98371->regmap,
MAX98371_SPK_SR,
MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_44);
break;
case 48000:
regmap_update_bits(max98371->regmap,
MAX98371_SPK_SR,
MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_48);
break;
case 88200:
regmap_update_bits(max98371->regmap,
MAX98371_SPK_SR,
MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_88);
break;
case 96000:
regmap_update_bits(max98371->regmap,
MAX98371_SPK_SR,
MAX98371_SPK_SR_MASK, MAX98371_SPK_SR_96);
break;
default:
return -EINVAL;
}
/* enabling both the RX channels*/
regmap_update_bits(max98371->regmap, MAX98371_MONOMIX_SRC,
MAX98371_MONOMIX_SRC_MASK, MONOMIX_RX_0_1);
regmap_update_bits(max98371->regmap, MAX98371_DAI_CHANNEL,
MAX98371_CHANNEL_MASK, MAX98371_CHANNEL_MASK);
return 0;
}
static const struct snd_soc_dapm_widget max98371_dapm_widgets[] = {
SND_SOC_DAPM_DAC("DAC", NULL, MAX98371_SPK_ENABLE, 0, 0),
SND_SOC_DAPM_SUPPLY("Global Enable", MAX98371_GLOBAL_ENABLE,
0, 0, NULL, 0),
SND_SOC_DAPM_OUTPUT("SPK_OUT"),
};
static const struct snd_soc_dapm_route max98371_audio_map[] = {
{"DAC", NULL, "HiFi Playback"},
{"SPK_OUT", NULL, "DAC"},
{"SPK_OUT", NULL, "Global Enable"},
};
#define MAX98371_RATES SNDRV_PCM_RATE_8000_48000
#define MAX98371_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
static const struct snd_soc_dai_ops max98371_dai_ops = {
.set_fmt = max98371_dai_set_fmt,
.hw_params = max98371_dai_hw_params,
};
static struct snd_soc_dai_driver max98371_dai[] = {
{
.name = "max98371-aif1",
.playback = {
.stream_name = "HiFi Playback",
.channels_min = 1,
.channels_max = 2,
.rates = SNDRV_PCM_RATE_8000_48000,
.formats = MAX98371_FORMATS,
},
.ops = &max98371_dai_ops,
}
};
static const struct snd_soc_codec_driver max98371_codec = {
.controls = max98371_snd_controls,
.num_controls = ARRAY_SIZE(max98371_snd_controls),
.dapm_routes = max98371_audio_map,
.num_dapm_routes = ARRAY_SIZE(max98371_audio_map),
.dapm_widgets = max98371_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(max98371_dapm_widgets),
};
static const struct regmap_config max98371_regmap = {
.reg_bits = 8,
.val_bits = 8,
.max_register = MAX98371_VERSION,
.reg_defaults = max98371_reg,
.num_reg_defaults = ARRAY_SIZE(max98371_reg),
.volatile_reg = max98371_volatile_register,
.readable_reg = max98371_readable_register,
.cache_type = REGCACHE_RBTREE,
};
static int max98371_i2c_probe(struct i2c_client *i2c,
const struct i2c_device_id *id)
{
struct max98371_priv *max98371;
int ret, reg;
max98371 = devm_kzalloc(&i2c->dev,
sizeof(*max98371), GFP_KERNEL);
if (!max98371)
return -ENOMEM;
i2c_set_clientdata(i2c, max98371);
max98371->regmap = devm_regmap_init_i2c(i2c, &max98371_regmap);
if (IS_ERR(max98371->regmap)) {
ret = PTR_ERR(max98371->regmap);
dev_err(&i2c->dev,
"Failed to allocate regmap: %d\n", ret);
return ret;
}
ret = regmap_read(max98371->regmap, MAX98371_VERSION, ®);
if (ret < 0) {
dev_info(&i2c->dev, "device error %d\n", ret);
return ret;
}
dev_info(&i2c->dev, "device version %x\n", reg);
ret = snd_soc_register_codec(&i2c->dev, &max98371_codec,
max98371_dai, ARRAY_SIZE(max98371_dai));
if (ret < 0) {
dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
return ret;
}
return ret;
}
static int max98371_i2c_remove(struct i2c_client *client)
{
snd_soc_unregister_codec(&client->dev);
return 0;
}
static const struct i2c_device_id max98371_i2c_id[] = {
{ "max98371", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, max98371_i2c_id);
static const struct of_device_id max98371_of_match[] = {
{ .compatible = "maxim,max98371", },
{ }
};
MODULE_DEVICE_TABLE(of, max98371_of_match);
static struct i2c_driver max98371_i2c_driver = {
.driver = {
.name = "max98371",
.owner = THIS_MODULE,
.pm = NULL,
.of_match_table = of_match_ptr(max98371_of_match),
},
.probe = max98371_i2c_probe,
.remove = max98371_i2c_remove,
.id_table = max98371_i2c_id,
};
module_i2c_driver(max98371_i2c_driver);
MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
MODULE_DESCRIPTION("ALSA SoC MAX98371 driver");
MODULE_LICENSE("GPL");
|