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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* PCM1681 ASoC codec driver
*
* Copyright (c) StreamUnlimited GmbH 2013
* Marek Belisko <marek.belisko@streamunlimited.com>
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/of.h>
#include <linux/clk.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
SNDRV_PCM_FMTBIT_S24_LE)
#define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
#define PCM1681_SOFT_MUTE_ALL 0xff
#define PCM1681_DEEMPH_RATE_MASK 0x18
#define PCM1681_DEEMPH_MASK 0x01
#define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */
#define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */
#define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */
#define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */
#define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */
#define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */
/*
* The DAC requires 65536 system clock cycles after the clock is supplied to
* complete its internal reset sequence before it is ready to be used.
*/
#define PCM1681_SCK_SETTLE_CYCLES 65536
static const struct reg_default pcm1681_reg_defaults[] = {
{ 0x01, 0xff },
{ 0x02, 0xff },
{ 0x03, 0xff },
{ 0x04, 0xff },
{ 0x05, 0xff },
{ 0x06, 0xff },
{ 0x07, 0x00 },
{ 0x08, 0x00 },
{ 0x09, 0x05 },
{ 0x0A, 0x00 },
{ 0x0B, 0xff },
{ 0x0C, 0x0f },
{ 0x0D, 0x00 },
{ 0x10, 0xff },
{ 0x11, 0xff },
{ 0x12, 0x00 },
{ 0x13, 0x00 },
};
static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg)
{
return !((reg == 0x00) || (reg == 0x0f));
}
static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg)
{
return pcm1681_accessible_reg(dev, reg) &&
(reg != PCM1681_ZERO_DETECT_STATUS);
}
static bool pcm1681_volatile_reg(struct device *dev, unsigned int reg)
{
/* Status is updated by the hardware and must never be cached */
return reg == PCM1681_ZERO_DETECT_STATUS;
}
struct pcm1681_private {
struct regmap *regmap;
unsigned int format;
/* Current deemphasis status */
unsigned int deemph;
/* Current rate for deemphasis control */
unsigned int rate;
struct clk *sck;
};
/* Wait for the DAC to come out of its internal reset after sck is enabled */
static void pcm1681_sck_settle(struct pcm1681_private *priv)
{
unsigned long rate = clk_get_rate(priv->sck);
/*
* sck is optional, so there may be no clock to wait on, and its rate
* is not always discoverable. Only wait when the cycle count can be
* converted into a delay.
*/
if (rate)
fsleep(DIV_ROUND_UP_ULL(PCM1681_SCK_SETTLE_CYCLES *
(u64)USEC_PER_SEC, rate));
}
static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
static int pcm1681_set_deemph(struct snd_soc_component *component)
{
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
int i, val = -1, enable = 0;
if (priv->deemph) {
for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) {
if (pcm1681_deemph[i] == priv->rate) {
val = i;
break;
}
}
}
if (val != -1) {
regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
PCM1681_DEEMPH_RATE_MASK, val << 3);
enable = 1;
} else {
enable = 0;
}
/* enable/disable deemphasis functionality */
return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
PCM1681_DEEMPH_MASK, enable);
}
static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
ucontrol->value.integer.value[0] = priv->deemph;
return 0;
}
static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
priv->deemph = ucontrol->value.integer.value[0];
return pcm1681_set_deemph(component);
}
static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai,
unsigned int format)
{
struct snd_soc_component *component = codec_dai->component;
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
/* The PCM1681 can only be consumer to all clocks */
if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
dev_err(component->dev, "Invalid clocking mode\n");
return -EINVAL;
}
priv->format = format;
return 0;
}
static int pcm1681_mute(struct snd_soc_dai *dai, int mute, int direction)
{
struct snd_soc_component *component = dai->component;
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
int val;
if (mute)
val = PCM1681_SOFT_MUTE_ALL;
else
val = 0;
return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val);
}
static int pcm1681_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
int val = 0, ret;
priv->rate = params_rate(params);
switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_RIGHT_J:
switch (params_width(params)) {
case 24:
val = 0;
break;
case 16:
val = 3;
break;
default:
return -EINVAL;
}
break;
case SND_SOC_DAIFMT_I2S:
val = 0x04;
break;
case SND_SOC_DAIFMT_LEFT_J:
val = 0x05;
break;
case SND_SOC_DAIFMT_DSP_A:
val = 0x06;
break;
default:
dev_err(component->dev, "Invalid DAI format\n");
return -EINVAL;
}
ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val);
if (ret < 0)
return ret;
return pcm1681_set_deemph(component);
}
static const u64 pcm1681_selectable_formats =
SND_SOC_POSSIBLE_DAIFMT_I2S |
SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
SND_SOC_POSSIBLE_DAIFMT_DSP_A;
static const struct snd_soc_dai_ops pcm1681_dai_ops = {
.set_fmt = pcm1681_set_dai_fmt,
.hw_params = pcm1681_hw_params,
.mute_stream = pcm1681_mute,
.auto_selectable_formats = &pcm1681_selectable_formats,
.num_auto_selectable_formats = 1,
.no_capture_mute = 1,
};
static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = {
SND_SOC_DAPM_OUTPUT("VOUT1"),
SND_SOC_DAPM_OUTPUT("VOUT2"),
SND_SOC_DAPM_OUTPUT("VOUT3"),
SND_SOC_DAPM_OUTPUT("VOUT4"),
SND_SOC_DAPM_OUTPUT("VOUT5"),
SND_SOC_DAPM_OUTPUT("VOUT6"),
SND_SOC_DAPM_OUTPUT("VOUT7"),
SND_SOC_DAPM_OUTPUT("VOUT8"),
};
static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = {
{ "VOUT1", NULL, "Playback" },
{ "VOUT2", NULL, "Playback" },
{ "VOUT3", NULL, "Playback" },
{ "VOUT4", NULL, "Playback" },
{ "VOUT5", NULL, "Playback" },
{ "VOUT6", NULL, "Playback" },
{ "VOUT7", NULL, "Playback" },
{ "VOUT8", NULL, "Playback" },
};
static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1);
static const struct snd_kcontrol_new pcm1681_controls[] = {
SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0,
0x7f, 0, pcm1681_dac_tlv),
SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0,
0x7f, 0, pcm1681_dac_tlv),
SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0,
0x7f, 0, pcm1681_dac_tlv),
SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume",
PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0,
0x7f, 0, pcm1681_dac_tlv),
SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
pcm1681_get_deemph, pcm1681_put_deemph),
};
static struct snd_soc_dai_driver pcm1681_dai = {
.name = "pcm1681-hifi",
.playback = {
.stream_name = "Playback",
.channels_min = 2,
.channels_max = 8,
.rates = PCM1681_PCM_RATES,
.formats = PCM1681_PCM_FORMATS,
},
.ops = &pcm1681_dai_ops,
};
#ifdef CONFIG_OF
static const struct of_device_id pcm1681_dt_ids[] = {
{ .compatible = "ti,pcm1681", },
{ }
};
MODULE_DEVICE_TABLE(of, pcm1681_dt_ids);
#endif
static const struct regmap_config pcm1681_regmap = {
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x13,
.reg_defaults = pcm1681_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults),
.writeable_reg = pcm1681_writeable_reg,
.readable_reg = pcm1681_accessible_reg,
.volatile_reg = pcm1681_volatile_reg,
.cache_type = REGCACHE_MAPLE,
};
static const struct snd_soc_component_driver soc_component_dev_pcm1681 = {
.controls = pcm1681_controls,
.num_controls = ARRAY_SIZE(pcm1681_controls),
.dapm_widgets = pcm1681_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets),
.dapm_routes = pcm1681_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes),
.use_pmdown_time = 1,
.endianness = 1,
};
static const struct i2c_device_id pcm1681_i2c_id[] = {
{ .name = "pcm1681" },
{ }
};
MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id);
static const char * const pcm1681_supply_names[] = {
"VCC1", "VCC2", "VDD",
};
static int pcm1681_i2c_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
int ret;
struct pcm1681_private *priv;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
ret = devm_regulator_bulk_get_enable(dev,
ARRAY_SIZE(pcm1681_supply_names),
pcm1681_supply_names);
if (ret)
return dev_err_probe(dev, ret,
"Failed to enable supplies\n");
priv->sck = devm_clk_get_optional(dev, "sck");
if (IS_ERR(priv->sck))
return dev_err_probe(dev, PTR_ERR(priv->sck),
"Failed to get sck\n");
priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap);
if (IS_ERR(priv->regmap)) {
ret = PTR_ERR(priv->regmap);
dev_err(dev, "Failed to create regmap: %d\n", ret);
return ret;
}
/* Must be set before runtime PM is enabled, the callbacks use it */
i2c_set_clientdata(client, priv);
ret = clk_prepare_enable(priv->sck);
if (ret)
return dev_err_probe(dev, ret, "Failed to enable sck\n");
pcm1681_sck_settle(priv);
/* The clock is on, so hand the now-active device over to runtime PM */
pm_runtime_set_autosuspend_delay(dev, 100);
pm_runtime_use_autosuspend(dev);
pm_runtime_set_active(dev);
pm_runtime_enable(dev);
pm_runtime_idle(dev);
ret = devm_snd_soc_register_component(dev,
&soc_component_dev_pcm1681,
&pcm1681_dai, 1);
if (ret) {
dev_err(dev, "Failed to register component: %d\n", ret);
goto err_pm;
}
return 0;
err_pm:
pm_runtime_dont_use_autosuspend(dev);
pm_runtime_disable(dev);
if (!pm_runtime_status_suspended(dev))
clk_disable_unprepare(priv->sck);
pm_runtime_set_suspended(dev);
return ret;
}
static void pcm1681_i2c_remove(struct i2c_client *client)
{
struct pcm1681_private *priv = i2c_get_clientdata(client);
struct device *dev = &client->dev;
pm_runtime_dont_use_autosuspend(dev);
pm_runtime_disable(dev);
/* Runtime PM may already have gated the clock */
if (!pm_runtime_status_suspended(dev))
clk_disable_unprepare(priv->sck);
pm_runtime_set_suspended(dev);
}
static int pcm1681_runtime_suspend(struct device *dev)
{
struct pcm1681_private *priv = dev_get_drvdata(dev);
/*
* Registers lose their contents once the clock is gated, so serve
* further access from the cache and replay it on resume.
*/
regcache_cache_only(priv->regmap, true);
regcache_mark_dirty(priv->regmap);
clk_disable_unprepare(priv->sck);
return 0;
}
static int pcm1681_runtime_resume(struct device *dev)
{
struct pcm1681_private *priv = dev_get_drvdata(dev);
int ret;
ret = clk_prepare_enable(priv->sck);
if (ret) {
dev_err(dev, "Failed to enable sck: %d\n", ret);
return ret;
}
pcm1681_sck_settle(priv);
regcache_cache_only(priv->regmap, false);
ret = regcache_sync(priv->regmap);
if (ret) {
dev_err(dev, "Failed to sync regcache: %d\n", ret);
regcache_cache_only(priv->regmap, true);
clk_disable_unprepare(priv->sck);
return ret;
}
return 0;
}
static DEFINE_RUNTIME_DEV_PM_OPS(pcm1681_pm_ops, pcm1681_runtime_suspend,
pcm1681_runtime_resume, NULL);
static struct i2c_driver pcm1681_i2c_driver = {
.driver = {
.name = "pcm1681",
.of_match_table = of_match_ptr(pcm1681_dt_ids),
.pm = pm_ptr(&pcm1681_pm_ops),
},
.id_table = pcm1681_i2c_id,
.probe = pcm1681_i2c_probe,
.remove = pcm1681_i2c_remove,
};
module_i2c_driver(pcm1681_i2c_driver);
MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver");
MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>");
MODULE_LICENSE("GPL");
|