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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* Copyright (C) ST-Ericsson SA 2010
*
* Author: Mikko J. Lehto <mikko.lehto@symbio.com>,
* Mikko Sarmanne <mikko.sarmanne@symbio.com>,
* Jarmo K. Kuronen <jarmo.kuronen@symbio.com>.
* Ola Lilja <ola.o.lilja@stericsson.com>
* for ST-Ericsson.
*
* License terms:
*
* 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/clk.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/io.h>
#include <sound/soc.h>
#include <linux/regulator/consumer.h>
#include <sound/pcm.h>
#include <sound/jack.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <mach/hardware.h>
#include "ux500_pcm.h"
#include "ux500_msp_dai.h"
#include "../codecs/ab8500_audio.h"
#define TX_SLOT_MONO 0x0008
#define TX_SLOT_STEREO 0x000a
#define RX_SLOT_MONO 0x0001
#define RX_SLOT_STEREO 0x0003
#define TX_SLOT_8CH 0x00FF
#define RX_SLOT_8CH 0x00FF
#define DEF_TX_SLOTS TX_SLOT_STEREO
#define DEF_RX_SLOTS RX_SLOT_MONO
#define DRIVERMODE_NORMAL 0
#define DRIVERMODE_CODEC_ONLY 1
static struct snd_soc_jack jack;
/* Power-control */
static DEFINE_MUTEX(power_lock);
static int ab8500_power_count;
/* Clocks */
/* audioclk -> intclk -> sysclk/ulpclk */
static int master_clock_sel;
static struct clk *clk_ptr_audioclk;
static struct clk *clk_ptr_intclk;
static struct clk *clk_ptr_sysclk;
static struct clk *clk_ptr_ulpclk;
/* Regulators */
static enum regulator_idx {
REGULATOR_AUDIO,
REGULATOR_DMIC,
REGULATOR_AMIC1,
REGULATOR_AMIC2
};
static struct regulator_bulk_data reg_info[4] = {
{ .supply = "v-audio" },
{ .supply = "v-dmic" },
{ .supply = "v-amic1" },
{ .supply = "v-amic2" }
};
static bool reg_enabled[4] = {
false,
false,
false,
false
};
/* Slot configuration */
static unsigned int tx_slots = DEF_TX_SLOTS;
static unsigned int rx_slots = DEF_RX_SLOTS;
/* Machine-driver ALSA-controls */
static int mclk_input_control_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
uinfo->count = 1;
uinfo->value.enumerated.items = 2;
if (uinfo->value.enumerated.item) {
uinfo->value.enumerated.item = 1;
strcpy(uinfo->value.enumerated.name, "ULPCLK");
} else {
strcpy(uinfo->value.enumerated.name, "SYSCLK");
}
return 0;
}
static int mclk_input_control_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
ucontrol->value.enumerated.item[0] = master_clock_sel;
return 0;
}
static int mclk_input_control_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
unsigned int val;
val = (ucontrol->value.enumerated.item[0] != 0);
if (master_clock_sel == val)
return 0;
master_clock_sel = val;
return 1;
}
static const struct snd_kcontrol_new mclk_input_control = {
.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
.name = "Master Clock Select",
.index = 0,
.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
.info = mclk_input_control_info,
.get = mclk_input_control_get,
.put = mclk_input_control_put,
.private_value = 1 /* ULPCLK */
};
/* Regulators */
static int enable_regulator(enum regulator_idx idx)
{
int ret;
pr_err("%s: DORIAN regulator %d.\n", __func__, (int)idx);
if (reg_enabled[idx])
return 0;
ret = regulator_enable(reg_info[idx].consumer);
if (ret != 0) {
pr_err("%s: Failure to enable regulator '%s' (ret = %d)\n",
__func__, reg_info[idx].supply, ret);
return ret;
};
reg_enabled[idx] = true;
pr_debug("%s: Enabled regulator '%s', status: %d, %d, %d, %d\n",
__func__,
reg_info[idx].supply,
(int)reg_enabled[0],
(int)reg_enabled[1],
(int)reg_enabled[2],
(int)reg_enabled[3]);
return 0;
}
static void disable_regulator(enum regulator_idx idx)
{
if (!reg_enabled[idx])
return;
regulator_disable(reg_info[idx].consumer);
reg_enabled[idx] = false;
pr_debug("%s: Disabled regulator '%s', status: %d, %d, %d, %d\n",
__func__,
reg_info[idx].supply,
(int)reg_enabled[0],
(int)reg_enabled[1],
(int)reg_enabled[2],
(int)reg_enabled[3]);
}
static int create_regulators(void)
{
int i, status = 0;
pr_debug("%s: Enter.\n", __func__);
for (i = 0; i < ARRAY_SIZE(reg_info); ++i)
reg_info[i].consumer = NULL;
for (i = 0; i < ARRAY_SIZE(reg_info); ++i) {
reg_info[i].consumer = regulator_get(NULL, reg_info[i].supply);
if (IS_ERR(reg_info[i].consumer)) {
status = PTR_ERR(reg_info[i].consumer);
pr_err("%s: ERROR: Failed to get regulator '%s' (ret = %d)!\n",
__func__, reg_info[i].supply, status);
reg_info[i].consumer = NULL;
goto err_get;
}
}
return 0;
err_get:
for (i = 0; i < ARRAY_SIZE(reg_info); ++i) {
if (reg_info[i].consumer) {
regulator_put(reg_info[i].consumer);
reg_info[i].consumer = NULL;
}
}
return status;
}
/* Power/clock control */
static int ux500_ab8500_power_control_inc(void)
{
int ret;
mutex_lock(&power_lock);
ab8500_power_count++;
pr_debug("%s: ab8500_power_count changed from %d to %d",
__func__,
ab8500_power_count-1,
ab8500_power_count);
if (ab8500_power_count == 1) {
ret = clk_set_parent(clk_ptr_intclk,
(master_clock_sel == 0) ? clk_ptr_sysclk : clk_ptr_ulpclk);
if (ret) {
pr_err("%s: ERROR: Setting master-clock to %s failed (ret = %d)!",
__func__,
(master_clock_sel == 0) ? "SYSCLK" : "ULPCLK",
ret);
return ret;
}
pr_debug("%s: Enabling master-clock (%s).",
__func__,
(master_clock_sel == 0) ? "SYSCLK" : "ULPCLK");
/* Enable audio-clock */
ret = clk_enable(clk_ptr_audioclk);
if (ret) {
pr_err("%s: ERROR: clk_enable failed (ret = %d)!", __func__, ret);
ab8500_power_count = 0;
return ret;
}
/* Power on audio-parts of AB8500 */
ab8500_audio_power_control(true);
/* Turn on audio-regulator */
ret = enable_regulator(REGULATOR_AUDIO);
}
mutex_unlock(&power_lock);
return 0;
}
static void ux500_ab8500_power_control_dec(void)
{
mutex_lock(&power_lock);
ab8500_power_count--;
pr_debug("%s: ab8500_power_count changed from %d to %d",
__func__,
ab8500_power_count+1,
ab8500_power_count);
if (ab8500_power_count == 0) {
pr_debug("%s: Disabling master-clock (%s).",
__func__,
(master_clock_sel == 0) ? "SYSCLK" : "ULPCLK");
/* Disable audio-clock */
clk_disable(clk_ptr_audioclk);
/* Power off audio-parts of AB8500 */
ab8500_audio_power_control(false);
/* Turn off audio-regulator */
disable_regulator(REGULATOR_AUDIO);
}
mutex_unlock(&power_lock);
}
/* ASoC */
int ux500_ab8500_startup(struct snd_pcm_substream *substream)
{
int ret = 0;
pr_debug("%s: Enter\n", __func__);
/* If we start recording we better enable the needed mic-regulators */
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
if (ab8500_audio_dapm_path_active(AB8500_AUDIO_DAPM_PATH_DMIC))
ret += enable_regulator(REGULATOR_DMIC);
if (ab8500_audio_dapm_path_active(AB8500_AUDIO_DAPM_PATH_AMIC1))
ret += enable_regulator(REGULATOR_AMIC1);
if (ab8500_audio_dapm_path_active(AB8500_AUDIO_DAPM_PATH_AMIC2))
ret += enable_regulator(REGULATOR_AMIC2);
if (ret != 0)
return ret;
}
return ux500_ab8500_power_control_inc();
return ret;
}
void ux500_ab8500_shutdown(struct snd_pcm_substream *substream)
{
pr_info("%s: Enter\n", __func__);
/* Reset slots configuration to default(s) */
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
tx_slots = DEF_TX_SLOTS;
else
rx_slots = DEF_RX_SLOTS;
/* Disable all mic-regulators that were enabled when we stop recording */
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
disable_regulator(REGULATOR_DMIC);
disable_regulator(REGULATOR_AMIC1);
disable_regulator(REGULATOR_AMIC2);
}
ux500_ab8500_power_control_dec();
}
int ux500_ab8500_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_dai *codec_dai = rtd->codec_dai;
struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
unsigned int fmt, fmt_if1;
int channels, ret = 0, slots, slot_width, driver_mode;
bool streamIsPlayback;
pr_debug("%s: Enter\n", __func__);
pr_debug("%s: substream->pcm->name = %s\n"
"substream->pcm->id = %s.\n"
"substream->name = %s.\n"
"substream->number = %d.\n",
__func__,
substream->pcm->name,
substream->pcm->id,
substream->name,
substream->number);
channels = params_channels(params);
/* Setup codec depending on driver-mode */
driver_mode = (channels == 8) ?
DRIVERMODE_CODEC_ONLY : DRIVERMODE_NORMAL;
pr_debug("%s: Driver-mode: %s.\n",
__func__,
(driver_mode == DRIVERMODE_NORMAL) ? "NORMAL" : "CODEC_ONLY");
if (driver_mode == DRIVERMODE_NORMAL) {
ab8500_audio_set_bit_delay(codec_dai, 0);
ab8500_audio_set_word_length(codec_dai, 16);
fmt = SND_SOC_DAIFMT_DSP_B |
SND_SOC_DAIFMT_CBM_CFM |
SND_SOC_DAIFMT_NB_NF |
SND_SOC_DAIFMT_CONT;
} else {
ab8500_audio_set_bit_delay(codec_dai, 1);
ab8500_audio_set_word_length(codec_dai, 20);
fmt = SND_SOC_DAIFMT_DSP_B |
SND_SOC_DAIFMT_CBM_CFM |
SND_SOC_DAIFMT_NB_NF |
SND_SOC_DAIFMT_GATED;
}
ret = snd_soc_dai_set_fmt(codec_dai, fmt);
if (ret < 0) {
pr_err("%s: ERROR: snd_soc_dai_set_fmt failed for codec_dai (ret = %d)!\n",
__func__,
ret);
return ret;
}
ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
if (ret < 0) {
pr_err("%s: ERROR: snd_soc_dai_set_fmt for cpu_dai (ret = %d)!\n",
__func__,
ret);
return ret;
}
/* Setup TDM-slots */
streamIsPlayback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
switch (channels) {
case 1:
slots = 16;
slot_width = 16;
tx_slots = (streamIsPlayback) ? TX_SLOT_MONO : 0;
rx_slots = (streamIsPlayback) ? 0 : RX_SLOT_MONO;
break;
case 2:
slots = 16;
slot_width = 16;
tx_slots = (streamIsPlayback) ? TX_SLOT_STEREO : 0;
rx_slots = (streamIsPlayback) ? 0 : RX_SLOT_STEREO;
break;
case 8:
slots = 16;
slot_width = 16;
tx_slots = (streamIsPlayback) ? TX_SLOT_8CH : 0;
rx_slots = (streamIsPlayback) ? 0 : RX_SLOT_8CH;
break;
default:
return -EINVAL;
}
pr_debug("%s: CPU-DAI TDM: TX=0x%04X RX=0x%04x\n",
__func__, tx_slots, rx_slots);
ret = snd_soc_dai_set_tdm_slot(cpu_dai, tx_slots, rx_slots, slots, slot_width);
if (ret)
return ret;
pr_debug("%s: CODEC-DAI TDM: TX=0x%04X RX=0x%04x\n",
__func__, tx_slots, rx_slots);
ret = snd_soc_dai_set_tdm_slot(codec_dai, tx_slots, rx_slots, slots, slot_width);
if (ret)
return ret;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
pr_debug("%s: Setup IF1 for FM-radio.\n", __func__);
fmt_if1 = SND_SOC_DAIFMT_CBM_CFM | SND_SOC_DAIFMT_I2S;
ret = ab8500_audio_setup_if1(codec_dai->codec, fmt_if1, 16, 1);
if (ret)
return ret;
}
return 0;
}
struct snd_soc_ops ux500_ab8500_ops[] = {
{
.hw_params = ux500_ab8500_hw_params,
.startup = ux500_ab8500_startup,
.shutdown = ux500_ab8500_shutdown,
}
};
int ux500_ab8500_machine_codec_init(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_codec *codec = rtd->codec;
int ret;
pr_debug("%s Enter.\n", __func__);
/* TODO: Add required DAPM routes to control regulators on demand */
ret = snd_soc_jack_new(codec,
"AB8500 Hs Status",
SND_JACK_HEADPHONE |
SND_JACK_MICROPHONE |
SND_JACK_HEADSET |
SND_JACK_LINEOUT |
SND_JACK_MECHANICAL |
SND_JACK_VIDEOOUT,
&jack);
if (ret < 0) {
pr_err("%s: ERROR: Failed to create Jack (ret = %d)!\n", __func__, ret);
return ret;
}
/* Add controls */
snd_ctl_add(codec->card->snd_card, snd_ctl_new1(&mclk_input_control, codec));
/* Get references to clock-nodes */
clk_ptr_sysclk = NULL;
clk_ptr_ulpclk = NULL;
clk_ptr_intclk = NULL;
clk_ptr_audioclk = NULL;
clk_ptr_sysclk = clk_get(codec->dev, "sysclk");
if (IS_ERR(clk_ptr_sysclk)) {
pr_err("ERROR: clk_get failed (ret = %d)!", -EFAULT);
return -EFAULT;
}
clk_ptr_ulpclk = clk_get(codec->dev, "ulpclk");
if (IS_ERR(clk_ptr_sysclk)) {
pr_err("ERROR: clk_get failed (ret = %d)!", -EFAULT);
return -EFAULT;
}
clk_ptr_intclk = clk_get(codec->dev, "intclk");
if (IS_ERR(clk_ptr_audioclk)) {
pr_err("ERROR: clk_get failed (ret = %d)!", -EFAULT);
return -EFAULT;
}
clk_ptr_audioclk = clk_get(codec->dev, "audioclk");
if (IS_ERR(clk_ptr_audioclk)) {
pr_err("ERROR: clk_get failed (ret = %d)!", -EFAULT);
return -EFAULT;
}
/* Set intclk default parent to ulpclk */
ret = clk_set_parent(clk_ptr_intclk, clk_ptr_ulpclk);
if (ret) {
pr_err("%s: ERROR: Setting intclk parent to ulpclk failed (ret = %d)!",
__func__,
ret);
return -EFAULT;
}
master_clock_sel = 1;
ab8500_power_count = 0;
return 0;
}
int ux500_ab8500_soc_machine_drv_init(void)
{
int status = 0;
pr_debug("%s: Enter.\n", __func__);
status = create_regulators();
if (status < 0) {
pr_err("%s: ERROR: Failed to instantiate regulators (ret = %d)!\n",
__func__, status);
return status;
}
return 0;
}
void ux500_ab8500_soc_machine_drv_cleanup(void)
{
pr_debug("%s: Enter.\n", __func__);
regulator_bulk_free(ARRAY_SIZE(reg_info), reg_info);
if (clk_ptr_sysclk != NULL)
clk_put(clk_ptr_sysclk);
if (clk_ptr_ulpclk != NULL)
clk_put(clk_ptr_ulpclk);
if (clk_ptr_intclk != NULL)
clk_put(clk_ptr_intclk);
if (clk_ptr_audioclk != NULL)
clk_put(clk_ptr_audioclk);
}
/* Extended interface */
void ux500_ab8500_jack_report(int value)
{
if (jack.jack)
snd_soc_jack_report(&jack, value, 0xFF);
}
EXPORT_SYMBOL_GPL(ux500_ab8500_jack_report);
|