~hui.wang/alsa-driver/dkms-packaging.audiosdw-ppa

« back to all changes in this revision

Viewing changes to buildroot/src/oem-audiosdw-lp1836324-0.6ubuntu1.2/soc/soc-core.c

  • Committer: Hui Wang
  • Date: 2019-12-13 02:41:40 UTC
  • Revision ID: hui.wang@canonical.com-20191213024140-1cprdcbl3122fn85
insert pc-oem-dkms

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// SPDX-License-Identifier: GPL-2.0+
2
 
//
3
 
// soc-core.c  --  ALSA SoC Audio Layer
4
 
//
5
 
// Copyright 2005 Wolfson Microelectronics PLC.
6
 
// Copyright 2005 Openedhand Ltd.
7
 
// Copyright (C) 2010 Slimlogic Ltd.
8
 
// Copyright (C) 2010 Texas Instruments Inc.
9
 
//
10
 
// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11
 
//         with code, comments and ideas from :-
12
 
//         Richard Purdie <richard@openedhand.com>
13
 
//
14
 
//  TODO:
15
 
//   o Add hw rules to enforce rates, etc.
16
 
//   o More testing with other codecs/machines.
17
 
//   o Add more codecs and platforms to ensure good API coverage.
18
 
//   o Support TDM on PCM and I2S
19
 
 
20
 
#include <linux/module.h>
21
 
#include <linux/moduleparam.h>
22
 
#include <linux/init.h>
23
 
#include <linux/delay.h>
24
 
#include <linux/pm.h>
25
 
#include <linux/bitops.h>
26
 
#include <linux/debugfs.h>
27
 
#include <linux/platform_device.h>
28
 
#include <linux/pinctrl/consumer.h>
29
 
#include <linux/ctype.h>
30
 
#include <linux/slab.h>
31
 
#include <linux/of.h>
32
 
#include <linux/of_graph.h>
33
 
#include <linux/dmi.h>
34
 
#include <dkms/sound/core.h>
35
 
#include <dkms/sound/jack.h>
36
 
#include <dkms/sound/pcm.h>
37
 
#include <dkms/sound/pcm_params.h>
38
 
#include <dkms/sound/soc.h>
39
 
#include <dkms/sound/soc-dpcm.h>
40
 
#include <dkms/sound/soc-topology.h>
41
 
#include <dkms/sound/initval.h>
42
 
 
43
 
#define CREATE_TRACE_POINTS
44
 
#include <trace/events/asoc.h>
45
 
 
46
 
#define NAME_SIZE       32
47
 
 
48
 
#ifdef CONFIG_DEBUG_FS
49
 
struct dentry *snd_soc_debugfs_root;
50
 
EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51
 
#endif
52
 
 
53
 
static DEFINE_MUTEX(client_mutex);
54
 
static LIST_HEAD(component_list);
55
 
static LIST_HEAD(unbind_card_list);
56
 
 
57
 
#define for_each_component(component)                   \
58
 
        list_for_each_entry(component, &component_list, list)
59
 
 
60
 
/*
61
 
 * This is used if driver don't need to have CPU/Codec/Platform
62
 
 * dai_link. see soc.h
63
 
 */
64
 
struct snd_soc_dai_link_component null_dailink_component[0];
65
 
EXPORT_SYMBOL_GPL(null_dailink_component);
66
 
 
67
 
/*
68
 
 * This is a timeout to do a DAPM powerdown after a stream is closed().
69
 
 * It can be used to eliminate pops between different playback streams, e.g.
70
 
 * between two audio tracks.
71
 
 */
72
 
static int pmdown_time = 5000;
73
 
module_param(pmdown_time, int, 0);
74
 
MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
 
 
76
 
#ifdef CONFIG_DMI
77
 
/*
78
 
 * If a DMI filed contain strings in this blacklist (e.g.
79
 
 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
80
 
 * as invalid and dropped when setting the card long name from DMI info.
81
 
 */
82
 
static const char * const dmi_blacklist[] = {
83
 
        "To be filled by OEM",
84
 
        "TBD by OEM",
85
 
        "Default String",
86
 
        "Board Manufacturer",
87
 
        "Board Vendor Name",
88
 
        "Board Product Name",
89
 
        NULL,   /* terminator */
90
 
};
91
 
#endif
92
 
 
93
 
static ssize_t pmdown_time_show(struct device *dev,
94
 
                                struct device_attribute *attr, char *buf)
95
 
{
96
 
        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97
 
 
98
 
        return sprintf(buf, "%ld\n", rtd->pmdown_time);
99
 
}
100
 
 
101
 
static ssize_t pmdown_time_set(struct device *dev,
102
 
                               struct device_attribute *attr,
103
 
                               const char *buf, size_t count)
104
 
{
105
 
        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106
 
        int ret;
107
 
 
108
 
        ret = kstrtol(buf, 10, &rtd->pmdown_time);
109
 
        if (ret)
110
 
                return ret;
111
 
 
112
 
        return count;
113
 
}
114
 
 
115
 
static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116
 
 
117
 
static struct attribute *soc_dev_attrs[] = {
118
 
        &dev_attr_pmdown_time.attr,
119
 
        NULL
120
 
};
121
 
 
122
 
static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123
 
                                       struct attribute *attr, int idx)
124
 
{
125
 
        struct device *dev = kobj_to_dev(kobj);
126
 
        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127
 
 
128
 
        if (!rtd)
129
 
                return 0;
130
 
 
131
 
        if (attr == &dev_attr_pmdown_time.attr)
132
 
                return attr->mode; /* always visible */
133
 
        return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
134
 
}
135
 
 
136
 
static const struct attribute_group soc_dapm_dev_group = {
137
 
        .attrs = soc_dapm_dev_attrs,
138
 
        .is_visible = soc_dev_attr_is_visible,
139
 
};
140
 
 
141
 
static const struct attribute_group soc_dev_group = {
142
 
        .attrs = soc_dev_attrs,
143
 
        .is_visible = soc_dev_attr_is_visible,
144
 
};
145
 
 
146
 
static const struct attribute_group *soc_dev_attr_groups[] = {
147
 
        &soc_dapm_dev_group,
148
 
        &soc_dev_group,
149
 
        NULL
150
 
};
151
 
 
152
 
#ifdef CONFIG_DEBUG_FS
153
 
static void soc_init_component_debugfs(struct snd_soc_component *component)
154
 
{
155
 
        if (!component->card->debugfs_card_root)
156
 
                return;
157
 
 
158
 
        if (component->debugfs_prefix) {
159
 
                char *name;
160
 
 
161
 
                name = kasprintf(GFP_KERNEL, "%s:%s",
162
 
                        component->debugfs_prefix, component->name);
163
 
                if (name) {
164
 
                        component->debugfs_root = debugfs_create_dir(name,
165
 
                                component->card->debugfs_card_root);
166
 
                        kfree(name);
167
 
                }
168
 
        } else {
169
 
                component->debugfs_root = debugfs_create_dir(component->name,
170
 
                                component->card->debugfs_card_root);
171
 
        }
172
 
 
173
 
        snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
174
 
                component->debugfs_root);
175
 
}
176
 
 
177
 
static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
178
 
{
179
 
        if (!component->debugfs_root)
180
 
                return;
181
 
        debugfs_remove_recursive(component->debugfs_root);
182
 
        component->debugfs_root = NULL;
183
 
}
184
 
 
185
 
static int dai_list_show(struct seq_file *m, void *v)
186
 
{
187
 
        struct snd_soc_component *component;
188
 
        struct snd_soc_dai *dai;
189
 
 
190
 
        mutex_lock(&client_mutex);
191
 
 
192
 
        for_each_component(component)
193
 
                for_each_component_dais(component, dai)
194
 
                        seq_printf(m, "%s\n", dai->name);
195
 
 
196
 
        mutex_unlock(&client_mutex);
197
 
 
198
 
        return 0;
199
 
}
200
 
DEFINE_SHOW_ATTRIBUTE(dai_list);
201
 
 
202
 
static int component_list_show(struct seq_file *m, void *v)
203
 
{
204
 
        struct snd_soc_component *component;
205
 
 
206
 
        mutex_lock(&client_mutex);
207
 
 
208
 
        for_each_component(component)
209
 
                seq_printf(m, "%s\n", component->name);
210
 
 
211
 
        mutex_unlock(&client_mutex);
212
 
 
213
 
        return 0;
214
 
}
215
 
DEFINE_SHOW_ATTRIBUTE(component_list);
216
 
 
217
 
static void soc_init_card_debugfs(struct snd_soc_card *card)
218
 
{
219
 
        card->debugfs_card_root = debugfs_create_dir(card->name,
220
 
                                                     snd_soc_debugfs_root);
221
 
 
222
 
        debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
223
 
                           &card->pop_time);
224
 
 
225
 
        snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
226
 
}
227
 
 
228
 
static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
229
 
{
230
 
        debugfs_remove_recursive(card->debugfs_card_root);
231
 
        card->debugfs_card_root = NULL;
232
 
}
233
 
 
234
 
static void snd_soc_debugfs_init(void)
235
 
{
236
 
        snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
237
 
 
238
 
        debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239
 
                            &dai_list_fops);
240
 
 
241
 
        debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242
 
                            &component_list_fops);
243
 
}
244
 
 
245
 
static void snd_soc_debugfs_exit(void)
246
 
{
247
 
        debugfs_remove_recursive(snd_soc_debugfs_root);
248
 
}
249
 
 
250
 
#else
251
 
 
252
 
static inline void soc_init_component_debugfs(
253
 
        struct snd_soc_component *component)
254
 
{
255
 
}
256
 
 
257
 
static inline void soc_cleanup_component_debugfs(
258
 
        struct snd_soc_component *component)
259
 
{
260
 
}
261
 
 
262
 
static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263
 
{
264
 
}
265
 
 
266
 
static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267
 
{
268
 
}
269
 
 
270
 
static inline void snd_soc_debugfs_init(void)
271
 
{
272
 
}
273
 
 
274
 
static inline void snd_soc_debugfs_exit(void)
275
 
{
276
 
}
277
 
 
278
 
#endif
279
 
 
280
 
/*
281
 
 * This is glue code between snd_pcm_lib_ioctl() and
282
 
 * snd_soc_component_driver :: ioctl
283
 
 */
284
 
int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
285
 
                          struct snd_pcm_substream *substream,
286
 
                          unsigned int cmd, void *arg)
287
 
{
288
 
        return snd_pcm_lib_ioctl(substream, cmd, arg);
289
 
}
290
 
EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
291
 
 
292
 
static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293
 
                              struct snd_soc_component *component)
294
 
{
295
 
        struct snd_soc_rtdcom_list *rtdcom;
296
 
        struct snd_soc_component *comp;
297
 
 
298
 
        for_each_rtd_components(rtd, rtdcom, comp) {
299
 
                /* already connected */
300
 
                if (comp == component)
301
 
                        return 0;
302
 
        }
303
 
 
304
 
        /*
305
 
         * created rtdcom here will be freed when rtd->dev was freed.
306
 
         * see
307
 
         *      soc_free_pcm_runtime() :: device_unregister(rtd->dev)
308
 
         */
309
 
        rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
310
 
        if (!rtdcom)
311
 
                return -ENOMEM;
312
 
 
313
 
        rtdcom->component = component;
314
 
        INIT_LIST_HEAD(&rtdcom->list);
315
 
 
316
 
        /*
317
 
         * When rtd was freed, created rtdcom here will be
318
 
         * also freed.
319
 
         * And we don't need to call list_del(&rtdcom->list)
320
 
         * when freed, because rtd is also freed.
321
 
         */
322
 
        list_add_tail(&rtdcom->list, &rtd->component_list);
323
 
 
324
 
        return 0;
325
 
}
326
 
 
327
 
struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328
 
                                                const char *driver_name)
329
 
{
330
 
        struct snd_soc_rtdcom_list *rtdcom;
331
 
        struct snd_soc_component *component;
332
 
 
333
 
        if (!driver_name)
334
 
                return NULL;
335
 
 
336
 
        /*
337
 
         * NOTE
338
 
         *
339
 
         * snd_soc_rtdcom_lookup() will find component from rtd by using
340
 
         * specified driver name.
341
 
         * But, if many components which have same driver name are connected
342
 
         * to 1 rtd, this function will return 1st found component.
343
 
         */
344
 
        for_each_rtd_components(rtd, rtdcom, component) {
345
 
                const char *component_name = component->driver->name;
346
 
 
347
 
                if (!component_name)
348
 
                        continue;
349
 
 
350
 
                if ((component_name == driver_name) ||
351
 
                    strcmp(component_name, driver_name) == 0)
352
 
                        return rtdcom->component;
353
 
        }
354
 
 
355
 
        return NULL;
356
 
}
357
 
EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
358
 
 
359
 
static struct snd_soc_component
360
 
*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
361
 
{
362
 
        struct snd_soc_component *component;
363
 
        struct snd_soc_component *found_component;
364
 
 
365
 
        found_component = NULL;
366
 
        for_each_component(component) {
367
 
                if ((dev == component->dev) &&
368
 
                    (!driver_name ||
369
 
                     (driver_name == component->driver->name) ||
370
 
                     (strcmp(component->driver->name, driver_name) == 0))) {
371
 
                        found_component = component;
372
 
                        break;
373
 
                }
374
 
        }
375
 
 
376
 
        return found_component;
377
 
}
378
 
 
379
 
struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
380
 
                                                   const char *driver_name)
381
 
{
382
 
        struct snd_soc_component *component;
383
 
 
384
 
        mutex_lock(&client_mutex);
385
 
        component = snd_soc_lookup_component_nolocked(dev, driver_name);
386
 
        mutex_unlock(&client_mutex);
387
 
 
388
 
        return component;
389
 
}
390
 
EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
391
 
 
392
 
struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
393
 
                const char *dai_link, int stream)
394
 
{
395
 
        struct snd_soc_pcm_runtime *rtd;
396
 
 
397
 
        for_each_card_rtds(card, rtd) {
398
 
                if (rtd->dai_link->no_pcm &&
399
 
                        !strcmp(rtd->dai_link->name, dai_link))
400
 
                        return rtd->pcm->streams[stream].substream;
401
 
        }
402
 
        dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
403
 
        return NULL;
404
 
}
405
 
EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
406
 
 
407
 
static const struct snd_soc_ops null_snd_soc_ops;
408
 
 
409
 
static void soc_release_rtd_dev(struct device *dev)
410
 
{
411
 
        /* "dev" means "rtd->dev" */
412
 
        kfree(dev);
413
 
}
414
 
 
415
 
static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
416
 
{
417
 
        if (!rtd)
418
 
                return;
419
 
 
420
 
        list_del(&rtd->list);
421
 
 
422
 
        /*
423
 
         * we don't need to call kfree() for rtd->dev
424
 
         * see
425
 
         *      soc_release_rtd_dev()
426
 
         *
427
 
         * We don't need rtd->dev NULL check, because
428
 
         * it is alloced *before* rtd.
429
 
         * see
430
 
         *      soc_new_pcm_runtime()
431
 
         */
432
 
        device_unregister(rtd->dev);
433
 
}
434
 
 
435
 
static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
436
 
        struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
437
 
{
438
 
        struct snd_soc_pcm_runtime *rtd;
439
 
        struct device *dev;
440
 
        int ret;
441
 
 
442
 
        /*
443
 
         * for rtd->dev
444
 
         */
445
 
        dev = kzalloc(sizeof(struct device), GFP_KERNEL);
446
 
        if (!dev)
447
 
                return NULL;
448
 
 
449
 
        dev->parent     = card->dev;
450
 
        dev->release    = soc_release_rtd_dev;
451
 
        dev->groups     = soc_dev_attr_groups;
452
 
 
453
 
        dev_set_name(dev, "%s", dai_link->name);
454
 
 
455
 
        ret = device_register(dev);
456
 
        if (ret < 0) {
457
 
                put_device(dev); /* soc_release_rtd_dev */
458
 
                return NULL;
459
 
        }
460
 
 
461
 
        /*
462
 
         * for rtd
463
 
         */
464
 
        rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
465
 
        if (!rtd)
466
 
                goto free_rtd;
467
 
 
468
 
        rtd->dev = dev;
469
 
        dev_set_drvdata(dev, rtd);
470
 
 
471
 
        /*
472
 
         * for rtd->codec_dais
473
 
         */
474
 
        rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
475
 
                                        sizeof(struct snd_soc_dai *),
476
 
                                        GFP_KERNEL);
477
 
        if (!rtd->codec_dais)
478
 
                goto free_rtd;
479
 
 
480
 
        /*
481
 
         * rtd remaining settings
482
 
         */
483
 
        INIT_LIST_HEAD(&rtd->component_list);
484
 
        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
485
 
        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
486
 
        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
487
 
        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
488
 
 
489
 
        rtd->card = card;
490
 
        rtd->dai_link = dai_link;
491
 
        if (!rtd->dai_link->ops)
492
 
                rtd->dai_link->ops = &null_snd_soc_ops;
493
 
 
494
 
        /* see for_each_card_rtds */
495
 
        list_add_tail(&rtd->list, &card->rtd_list);
496
 
        rtd->num = card->num_rtd;
497
 
        card->num_rtd++;
498
 
 
499
 
        return rtd;
500
 
 
501
 
free_rtd:
502
 
        soc_free_pcm_runtime(rtd);
503
 
        return NULL;
504
 
}
505
 
 
506
 
struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
507
 
                const char *dai_link)
508
 
{
509
 
        struct snd_soc_pcm_runtime *rtd;
510
 
 
511
 
        for_each_card_rtds(card, rtd) {
512
 
                if (!strcmp(rtd->dai_link->name, dai_link))
513
 
                        return rtd;
514
 
        }
515
 
        dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
516
 
        return NULL;
517
 
}
518
 
EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
519
 
 
520
 
static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
521
 
{
522
 
        struct snd_soc_pcm_runtime *rtd;
523
 
 
524
 
        for_each_card_rtds(card, rtd)
525
 
                flush_delayed_work(&rtd->delayed_work);
526
 
}
527
 
 
528
 
#ifdef CONFIG_PM_SLEEP
529
 
/* powers down audio subsystem for suspend */
530
 
int snd_soc_suspend(struct device *dev)
531
 
{
532
 
        struct snd_soc_card *card = dev_get_drvdata(dev);
533
 
        struct snd_soc_component *component;
534
 
        struct snd_soc_pcm_runtime *rtd;
535
 
        int i;
536
 
 
537
 
        /* If the card is not initialized yet there is nothing to do */
538
 
        if (!card->instantiated)
539
 
                return 0;
540
 
 
541
 
        /*
542
 
         * Due to the resume being scheduled into a workqueue we could
543
 
         * suspend before that's finished - wait for it to complete.
544
 
         */
545
 
        snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
546
 
 
547
 
        /* we're going to block userspace touching us until resume completes */
548
 
        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
549
 
 
550
 
        /* mute any active DACs */
551
 
        for_each_card_rtds(card, rtd) {
552
 
                struct snd_soc_dai *dai;
553
 
 
554
 
                if (rtd->dai_link->ignore_suspend)
555
 
                        continue;
556
 
 
557
 
                for_each_rtd_codec_dai(rtd, i, dai) {
558
 
                        if (dai->playback_active)
559
 
                                snd_soc_dai_digital_mute(dai, 1,
560
 
                                                SNDRV_PCM_STREAM_PLAYBACK);
561
 
                }
562
 
        }
563
 
 
564
 
        /* suspend all pcms */
565
 
        for_each_card_rtds(card, rtd) {
566
 
                if (rtd->dai_link->ignore_suspend)
567
 
                        continue;
568
 
 
569
 
                snd_pcm_suspend_all(rtd->pcm);
570
 
        }
571
 
 
572
 
        if (card->suspend_pre)
573
 
                card->suspend_pre(card);
574
 
 
575
 
        for_each_card_rtds(card, rtd) {
576
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
577
 
 
578
 
                if (rtd->dai_link->ignore_suspend)
579
 
                        continue;
580
 
 
581
 
                if (!cpu_dai->driver->bus_control)
582
 
                        snd_soc_dai_suspend(cpu_dai);
583
 
        }
584
 
 
585
 
        /* close any waiting streams */
586
 
        snd_soc_flush_all_delayed_work(card);
587
 
 
588
 
        for_each_card_rtds(card, rtd) {
589
 
 
590
 
                if (rtd->dai_link->ignore_suspend)
591
 
                        continue;
592
 
 
593
 
                snd_soc_dapm_stream_event(rtd,
594
 
                                          SNDRV_PCM_STREAM_PLAYBACK,
595
 
                                          SND_SOC_DAPM_STREAM_SUSPEND);
596
 
 
597
 
                snd_soc_dapm_stream_event(rtd,
598
 
                                          SNDRV_PCM_STREAM_CAPTURE,
599
 
                                          SND_SOC_DAPM_STREAM_SUSPEND);
600
 
        }
601
 
 
602
 
        /* Recheck all endpoints too, their state is affected by suspend */
603
 
        dapm_mark_endpoints_dirty(card);
604
 
        snd_soc_dapm_sync(&card->dapm);
605
 
 
606
 
        /* suspend all COMPONENTs */
607
 
        for_each_card_components(card, component) {
608
 
                struct snd_soc_dapm_context *dapm =
609
 
                                snd_soc_component_get_dapm(component);
610
 
 
611
 
                /*
612
 
                 * If there are paths active then the COMPONENT will be held
613
 
                 * with bias _ON and should not be suspended.
614
 
                 */
615
 
                if (!snd_soc_component_is_suspended(component)) {
616
 
                        switch (snd_soc_dapm_get_bias_level(dapm)) {
617
 
                        case SND_SOC_BIAS_STANDBY:
618
 
                                /*
619
 
                                 * If the COMPONENT is capable of idle
620
 
                                 * bias off then being in STANDBY
621
 
                                 * means it's doing something,
622
 
                                 * otherwise fall through.
623
 
                                 */
624
 
                                if (dapm->idle_bias_off) {
625
 
                                        dev_dbg(component->dev,
626
 
                                                "ASoC: idle_bias_off CODEC on over suspend\n");
627
 
                                        break;
628
 
                                }
629
 
                                /* fall through */
630
 
 
631
 
                        case SND_SOC_BIAS_OFF:
632
 
                                snd_soc_component_suspend(component);
633
 
                                if (component->regmap)
634
 
                                        regcache_mark_dirty(component->regmap);
635
 
                                /* deactivate pins to sleep state */
636
 
                                pinctrl_pm_select_sleep_state(component->dev);
637
 
                                break;
638
 
                        default:
639
 
                                dev_dbg(component->dev,
640
 
                                        "ASoC: COMPONENT is on over suspend\n");
641
 
                                break;
642
 
                        }
643
 
                }
644
 
        }
645
 
 
646
 
        for_each_card_rtds(card, rtd) {
647
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
648
 
 
649
 
                if (rtd->dai_link->ignore_suspend)
650
 
                        continue;
651
 
 
652
 
                if (cpu_dai->driver->bus_control)
653
 
                        snd_soc_dai_suspend(cpu_dai);
654
 
 
655
 
                /* deactivate pins to sleep state */
656
 
                pinctrl_pm_select_sleep_state(cpu_dai->dev);
657
 
        }
658
 
 
659
 
        if (card->suspend_post)
660
 
                card->suspend_post(card);
661
 
 
662
 
        return 0;
663
 
}
664
 
EXPORT_SYMBOL_GPL(snd_soc_suspend);
665
 
 
666
 
/*
667
 
 * deferred resume work, so resume can complete before we finished
668
 
 * setting our codec back up, which can be very slow on I2C
669
 
 */
670
 
static void soc_resume_deferred(struct work_struct *work)
671
 
{
672
 
        struct snd_soc_card *card =
673
 
                        container_of(work, struct snd_soc_card,
674
 
                                     deferred_resume_work);
675
 
        struct snd_soc_pcm_runtime *rtd;
676
 
        struct snd_soc_component *component;
677
 
        int i;
678
 
 
679
 
        /*
680
 
         * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
681
 
         * so userspace apps are blocked from touching us
682
 
         */
683
 
 
684
 
        dev_dbg(card->dev, "ASoC: starting resume work\n");
685
 
 
686
 
        /* Bring us up into D2 so that DAPM starts enabling things */
687
 
        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
688
 
 
689
 
        if (card->resume_pre)
690
 
                card->resume_pre(card);
691
 
 
692
 
        /* resume control bus DAIs */
693
 
        for_each_card_rtds(card, rtd) {
694
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
695
 
 
696
 
                if (rtd->dai_link->ignore_suspend)
697
 
                        continue;
698
 
 
699
 
                if (cpu_dai->driver->bus_control)
700
 
                        snd_soc_dai_resume(cpu_dai);
701
 
        }
702
 
 
703
 
        for_each_card_components(card, component) {
704
 
                if (snd_soc_component_is_suspended(component))
705
 
                        snd_soc_component_resume(component);
706
 
        }
707
 
 
708
 
        for_each_card_rtds(card, rtd) {
709
 
 
710
 
                if (rtd->dai_link->ignore_suspend)
711
 
                        continue;
712
 
 
713
 
                snd_soc_dapm_stream_event(rtd,
714
 
                                          SNDRV_PCM_STREAM_PLAYBACK,
715
 
                                          SND_SOC_DAPM_STREAM_RESUME);
716
 
 
717
 
                snd_soc_dapm_stream_event(rtd,
718
 
                                          SNDRV_PCM_STREAM_CAPTURE,
719
 
                                          SND_SOC_DAPM_STREAM_RESUME);
720
 
        }
721
 
 
722
 
        /* unmute any active DACs */
723
 
        for_each_card_rtds(card, rtd) {
724
 
                struct snd_soc_dai *dai;
725
 
 
726
 
                if (rtd->dai_link->ignore_suspend)
727
 
                        continue;
728
 
 
729
 
                for_each_rtd_codec_dai(rtd, i, dai) {
730
 
                        if (dai->playback_active)
731
 
                                snd_soc_dai_digital_mute(dai, 0,
732
 
                                                SNDRV_PCM_STREAM_PLAYBACK);
733
 
                }
734
 
        }
735
 
 
736
 
        for_each_card_rtds(card, rtd) {
737
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
738
 
 
739
 
                if (rtd->dai_link->ignore_suspend)
740
 
                        continue;
741
 
 
742
 
                if (!cpu_dai->driver->bus_control)
743
 
                        snd_soc_dai_resume(cpu_dai);
744
 
        }
745
 
 
746
 
        if (card->resume_post)
747
 
                card->resume_post(card);
748
 
 
749
 
        dev_dbg(card->dev, "ASoC: resume work completed\n");
750
 
 
751
 
        /* Recheck all endpoints too, their state is affected by suspend */
752
 
        dapm_mark_endpoints_dirty(card);
753
 
        snd_soc_dapm_sync(&card->dapm);
754
 
 
755
 
        /* userspace can access us now we are back as we were before */
756
 
        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
757
 
}
758
 
 
759
 
/* powers up audio subsystem after a suspend */
760
 
int snd_soc_resume(struct device *dev)
761
 
{
762
 
        struct snd_soc_card *card = dev_get_drvdata(dev);
763
 
        bool bus_control = false;
764
 
        struct snd_soc_pcm_runtime *rtd;
765
 
        struct snd_soc_dai *codec_dai;
766
 
        int i;
767
 
 
768
 
        /* If the card is not initialized yet there is nothing to do */
769
 
        if (!card->instantiated)
770
 
                return 0;
771
 
 
772
 
        /* activate pins from sleep state */
773
 
        for_each_card_rtds(card, rtd) {
774
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
775
 
 
776
 
                if (cpu_dai->active)
777
 
                        pinctrl_pm_select_default_state(cpu_dai->dev);
778
 
 
779
 
                for_each_rtd_codec_dai(rtd, i, codec_dai) {
780
 
                        if (codec_dai->active)
781
 
                                pinctrl_pm_select_default_state(codec_dai->dev);
782
 
                }
783
 
        }
784
 
 
785
 
        /*
786
 
         * DAIs that also act as the control bus master might have other drivers
787
 
         * hanging off them so need to resume immediately. Other drivers don't
788
 
         * have that problem and may take a substantial amount of time to resume
789
 
         * due to I/O costs and anti-pop so handle them out of line.
790
 
         */
791
 
        for_each_card_rtds(card, rtd) {
792
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
793
 
 
794
 
                bus_control |= cpu_dai->driver->bus_control;
795
 
        }
796
 
        if (bus_control) {
797
 
                dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
798
 
                soc_resume_deferred(&card->deferred_resume_work);
799
 
        } else {
800
 
                dev_dbg(dev, "ASoC: Scheduling resume work\n");
801
 
                if (!schedule_work(&card->deferred_resume_work))
802
 
                        dev_err(dev, "ASoC: resume work item may be lost\n");
803
 
        }
804
 
 
805
 
        return 0;
806
 
}
807
 
EXPORT_SYMBOL_GPL(snd_soc_resume);
808
 
 
809
 
static void soc_resume_init(struct snd_soc_card *card)
810
 
{
811
 
        /* deferred resume work */
812
 
        INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
813
 
}
814
 
#else
815
 
#define snd_soc_suspend NULL
816
 
#define snd_soc_resume NULL
817
 
static inline void soc_resume_init(struct snd_soc_card *card)
818
 
{
819
 
}
820
 
#endif
821
 
 
822
 
static const struct snd_soc_dai_ops null_dai_ops = {
823
 
};
824
 
 
825
 
static struct device_node
826
 
*soc_component_to_node(struct snd_soc_component *component)
827
 
{
828
 
        struct device_node *of_node;
829
 
 
830
 
        of_node = component->dev->of_node;
831
 
        if (!of_node && component->dev->parent)
832
 
                of_node = component->dev->parent->of_node;
833
 
 
834
 
        return of_node;
835
 
}
836
 
 
837
 
static int snd_soc_is_matching_component(
838
 
        const struct snd_soc_dai_link_component *dlc,
839
 
        struct snd_soc_component *component)
840
 
{
841
 
        struct device_node *component_of_node;
842
 
 
843
 
        if (!dlc)
844
 
                return 0;
845
 
 
846
 
        component_of_node = soc_component_to_node(component);
847
 
 
848
 
        if (dlc->of_node && component_of_node != dlc->of_node)
849
 
                return 0;
850
 
        if (dlc->name && strcmp(component->name, dlc->name))
851
 
                return 0;
852
 
 
853
 
        return 1;
854
 
}
855
 
 
856
 
static struct snd_soc_component *soc_find_component(
857
 
        const struct snd_soc_dai_link_component *dlc)
858
 
{
859
 
        struct snd_soc_component *component;
860
 
 
861
 
        lockdep_assert_held(&client_mutex);
862
 
 
863
 
        /*
864
 
         * NOTE
865
 
         *
866
 
         * It returns *1st* found component, but some driver
867
 
         * has few components by same of_node/name
868
 
         * ex)
869
 
         *      CPU component and generic DMAEngine component
870
 
         */
871
 
        for_each_component(component)
872
 
                if (snd_soc_is_matching_component(dlc, component))
873
 
                        return component;
874
 
 
875
 
        return NULL;
876
 
}
877
 
 
878
 
/**
879
 
 * snd_soc_find_dai - Find a registered DAI
880
 
 *
881
 
 * @dlc: name of the DAI or the DAI driver and optional component info to match
882
 
 *
883
 
 * This function will search all registered components and their DAIs to
884
 
 * find the DAI of the same name. The component's of_node and name
885
 
 * should also match if being specified.
886
 
 *
887
 
 * Return: pointer of DAI, or NULL if not found.
888
 
 */
889
 
struct snd_soc_dai *snd_soc_find_dai(
890
 
        const struct snd_soc_dai_link_component *dlc)
891
 
{
892
 
        struct snd_soc_component *component;
893
 
        struct snd_soc_dai *dai;
894
 
 
895
 
        lockdep_assert_held(&client_mutex);
896
 
 
897
 
        /* Find CPU DAI from registered DAIs */
898
 
        for_each_component(component) {
899
 
                if (!snd_soc_is_matching_component(dlc, component))
900
 
                        continue;
901
 
                for_each_component_dais(component, dai) {
902
 
                        if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
903
 
                            && (!dai->driver->name
904
 
                                || strcmp(dai->driver->name, dlc->dai_name)))
905
 
                                continue;
906
 
 
907
 
                        return dai;
908
 
                }
909
 
        }
910
 
 
911
 
        return NULL;
912
 
}
913
 
EXPORT_SYMBOL_GPL(snd_soc_find_dai);
914
 
 
915
 
/**
916
 
 * snd_soc_find_dai_link - Find a DAI link
917
 
 *
918
 
 * @card: soc card
919
 
 * @id: DAI link ID to match
920
 
 * @name: DAI link name to match, optional
921
 
 * @stream_name: DAI link stream name to match, optional
922
 
 *
923
 
 * This function will search all existing DAI links of the soc card to
924
 
 * find the link of the same ID. Since DAI links may not have their
925
 
 * unique ID, so name and stream name should also match if being
926
 
 * specified.
927
 
 *
928
 
 * Return: pointer of DAI link, or NULL if not found.
929
 
 */
930
 
struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
931
 
                                               int id, const char *name,
932
 
                                               const char *stream_name)
933
 
{
934
 
        struct snd_soc_dai_link *link;
935
 
 
936
 
        lockdep_assert_held(&client_mutex);
937
 
 
938
 
        for_each_card_links(card, link) {
939
 
                if (link->id != id)
940
 
                        continue;
941
 
 
942
 
                if (name && (!link->name || strcmp(name, link->name)))
943
 
                        continue;
944
 
 
945
 
                if (stream_name && (!link->stream_name
946
 
                        || strcmp(stream_name, link->stream_name)))
947
 
                        continue;
948
 
 
949
 
                return link;
950
 
        }
951
 
 
952
 
        return NULL;
953
 
}
954
 
EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
955
 
 
956
 
static bool soc_is_dai_link_bound(struct snd_soc_card *card,
957
 
                struct snd_soc_dai_link *dai_link)
958
 
{
959
 
        struct snd_soc_pcm_runtime *rtd;
960
 
 
961
 
        for_each_card_rtds(card, rtd) {
962
 
                if (rtd->dai_link == dai_link)
963
 
                        return true;
964
 
        }
965
 
 
966
 
        return false;
967
 
}
968
 
 
969
 
static int soc_dai_link_sanity_check(struct snd_soc_card *card,
970
 
                                     struct snd_soc_dai_link *link)
971
 
{
972
 
        int i;
973
 
        struct snd_soc_dai_link_component *codec, *platform;
974
 
 
975
 
        for_each_link_codecs(link, i, codec) {
976
 
                /*
977
 
                 * Codec must be specified by 1 of name or OF node,
978
 
                 * not both or neither.
979
 
                 */
980
 
                if (!!codec->name == !!codec->of_node) {
981
 
                        dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
982
 
                                link->name);
983
 
                        return -EINVAL;
984
 
                }
985
 
 
986
 
                /* Codec DAI name must be specified */
987
 
                if (!codec->dai_name) {
988
 
                        dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
989
 
                                link->name);
990
 
                        return -EINVAL;
991
 
                }
992
 
 
993
 
                /*
994
 
                 * Defer card registration if codec component is not added to
995
 
                 * component list.
996
 
                 */
997
 
                if (!soc_find_component(codec))
998
 
                        return -EPROBE_DEFER;
999
 
        }
1000
 
 
1001
 
        for_each_link_platforms(link, i, platform) {
1002
 
                /*
1003
 
                 * Platform may be specified by either name or OF node, but it
1004
 
                 * can be left unspecified, then no components will be inserted
1005
 
                 * in the rtdcom list
1006
 
                 */
1007
 
                if (!!platform->name == !!platform->of_node) {
1008
 
                        dev_err(card->dev,
1009
 
                                "ASoC: Neither/both platform name/of_node are set for %s\n",
1010
 
                                link->name);
1011
 
                        return -EINVAL;
1012
 
                }
1013
 
 
1014
 
                /*
1015
 
                 * Defer card registration if platform component is not added to
1016
 
                 * component list.
1017
 
                 */
1018
 
                if (!soc_find_component(platform))
1019
 
                        return -EPROBE_DEFER;
1020
 
        }
1021
 
 
1022
 
        /* FIXME */
1023
 
        if (link->num_cpus > 1) {
1024
 
                dev_err(card->dev,
1025
 
                        "ASoC: multi cpu is not yet supported %s\n",
1026
 
                        link->name);
1027
 
                return -EINVAL;
1028
 
        }
1029
 
 
1030
 
        /*
1031
 
         * CPU device may be specified by either name or OF node, but
1032
 
         * can be left unspecified, and will be matched based on DAI
1033
 
         * name alone..
1034
 
         */
1035
 
        if (link->cpus->name && link->cpus->of_node) {
1036
 
                dev_err(card->dev,
1037
 
                        "ASoC: Neither/both cpu name/of_node are set for %s\n",
1038
 
                        link->name);
1039
 
                return -EINVAL;
1040
 
        }
1041
 
 
1042
 
        /*
1043
 
         * Defer card registration if cpu dai component is not added to
1044
 
         * component list.
1045
 
         */
1046
 
        if ((link->cpus->of_node || link->cpus->name) &&
1047
 
            !soc_find_component(link->cpus))
1048
 
                return -EPROBE_DEFER;
1049
 
 
1050
 
        /*
1051
 
         * At least one of CPU DAI name or CPU device name/node must be
1052
 
         * specified
1053
 
         */
1054
 
        if (!link->cpus->dai_name &&
1055
 
            !(link->cpus->name || link->cpus->of_node)) {
1056
 
                dev_err(card->dev,
1057
 
                        "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1058
 
                        link->name);
1059
 
                return -EINVAL;
1060
 
        }
1061
 
 
1062
 
        return 0;
1063
 
}
1064
 
 
1065
 
static void soc_unbind_dai_link(struct snd_soc_card *card,
1066
 
                                struct snd_soc_dai_link *dai_link)
1067
 
{
1068
 
        struct snd_soc_pcm_runtime *rtd;
1069
 
 
1070
 
        rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1071
 
        if (rtd)
1072
 
                soc_free_pcm_runtime(rtd);
1073
 
}
1074
 
 
1075
 
static int soc_bind_dai_link(struct snd_soc_card *card,
1076
 
        struct snd_soc_dai_link *dai_link)
1077
 
{
1078
 
        struct snd_soc_pcm_runtime *rtd;
1079
 
        struct snd_soc_dai_link_component *codec, *platform;
1080
 
        struct snd_soc_component *component;
1081
 
        int i, ret;
1082
 
 
1083
 
        if (dai_link->ignore)
1084
 
                return 0;
1085
 
 
1086
 
        dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1087
 
 
1088
 
        if (soc_is_dai_link_bound(card, dai_link)) {
1089
 
                dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1090
 
                        dai_link->name);
1091
 
                return 0;
1092
 
        }
1093
 
 
1094
 
        ret = soc_dai_link_sanity_check(card, dai_link);
1095
 
        if (ret < 0)
1096
 
                return ret;
1097
 
 
1098
 
        rtd = soc_new_pcm_runtime(card, dai_link);
1099
 
        if (!rtd)
1100
 
                return -ENOMEM;
1101
 
 
1102
 
        /* FIXME: we need multi CPU support in the future */
1103
 
        rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1104
 
        if (!rtd->cpu_dai) {
1105
 
                dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1106
 
                         dai_link->cpus->dai_name);
1107
 
                goto _err_defer;
1108
 
        }
1109
 
        snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1110
 
 
1111
 
        /* Find CODEC from registered CODECs */
1112
 
        rtd->num_codecs = dai_link->num_codecs;
1113
 
        for_each_link_codecs(dai_link, i, codec) {
1114
 
                rtd->codec_dais[i] = snd_soc_find_dai(codec);
1115
 
                if (!rtd->codec_dais[i]) {
1116
 
                        dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1117
 
                                 codec->dai_name);
1118
 
                        goto _err_defer;
1119
 
                }
1120
 
 
1121
 
                snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
1122
 
        }
1123
 
 
1124
 
        /* Single codec links expect codec and codec_dai in runtime data */
1125
 
        rtd->codec_dai = rtd->codec_dais[0];
1126
 
 
1127
 
        /* Find PLATFORM from registered PLATFORMs */
1128
 
        for_each_link_platforms(dai_link, i, platform) {
1129
 
                for_each_component(component) {
1130
 
                        if (!snd_soc_is_matching_component(platform, component))
1131
 
                                continue;
1132
 
 
1133
 
                        snd_soc_rtdcom_add(rtd, component);
1134
 
                }
1135
 
        }
1136
 
 
1137
 
        return 0;
1138
 
 
1139
 
_err_defer:
1140
 
        soc_free_pcm_runtime(rtd);
1141
 
        return -EPROBE_DEFER;
1142
 
}
1143
 
 
1144
 
static void soc_set_of_name_prefix(struct snd_soc_component *component)
1145
 
{
1146
 
        struct device_node *of_node = soc_component_to_node(component);
1147
 
        const char *str;
1148
 
        int ret;
1149
 
 
1150
 
        ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1151
 
        if (!ret)
1152
 
                component->name_prefix = str;
1153
 
}
1154
 
 
1155
 
static void soc_set_name_prefix(struct snd_soc_card *card,
1156
 
                                struct snd_soc_component *component)
1157
 
{
1158
 
        int i;
1159
 
 
1160
 
        for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1161
 
                struct snd_soc_codec_conf *map = &card->codec_conf[i];
1162
 
                struct device_node *of_node = soc_component_to_node(component);
1163
 
 
1164
 
                if (map->of_node && of_node != map->of_node)
1165
 
                        continue;
1166
 
                if (map->dev_name && strcmp(component->name, map->dev_name))
1167
 
                        continue;
1168
 
                component->name_prefix = map->name_prefix;
1169
 
                return;
1170
 
        }
1171
 
 
1172
 
        /*
1173
 
         * If there is no configuration table or no match in the table,
1174
 
         * check if a prefix is provided in the node
1175
 
         */
1176
 
        soc_set_of_name_prefix(component);
1177
 
}
1178
 
 
1179
 
static void soc_cleanup_component(struct snd_soc_component *component)
1180
 
{
1181
 
        /* For framework level robustness */
1182
 
        snd_soc_component_set_jack(component, NULL, NULL);
1183
 
 
1184
 
        list_del_init(&component->card_list);
1185
 
        snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1186
 
        soc_cleanup_component_debugfs(component);
1187
 
        component->card = NULL;
1188
 
        snd_soc_component_module_put_when_remove(component);
1189
 
}
1190
 
 
1191
 
static void soc_remove_component(struct snd_soc_component *component)
1192
 
{
1193
 
        if (!component->card)
1194
 
                return;
1195
 
 
1196
 
        snd_soc_component_remove(component);
1197
 
 
1198
 
        soc_cleanup_component(component);
1199
 
}
1200
 
 
1201
 
static int soc_probe_component(struct snd_soc_card *card,
1202
 
                               struct snd_soc_component *component)
1203
 
{
1204
 
        struct snd_soc_dapm_context *dapm =
1205
 
                snd_soc_component_get_dapm(component);
1206
 
        struct snd_soc_dai *dai;
1207
 
        int ret;
1208
 
 
1209
 
        if (!strcmp(component->name, "snd-soc-dummy"))
1210
 
                return 0;
1211
 
 
1212
 
        if (component->card) {
1213
 
                if (component->card != card) {
1214
 
                        dev_err(component->dev,
1215
 
                                "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1216
 
                                card->name, component->card->name);
1217
 
                        return -ENODEV;
1218
 
                }
1219
 
                return 0;
1220
 
        }
1221
 
 
1222
 
        ret = snd_soc_component_module_get_when_probe(component);
1223
 
        if (ret < 0)
1224
 
                return ret;
1225
 
 
1226
 
        component->card = card;
1227
 
        soc_set_name_prefix(card, component);
1228
 
 
1229
 
        soc_init_component_debugfs(component);
1230
 
 
1231
 
        snd_soc_dapm_init(dapm, card, component);
1232
 
 
1233
 
        ret = snd_soc_dapm_new_controls(dapm,
1234
 
                                        component->driver->dapm_widgets,
1235
 
                                        component->driver->num_dapm_widgets);
1236
 
 
1237
 
        if (ret != 0) {
1238
 
                dev_err(component->dev,
1239
 
                        "Failed to create new controls %d\n", ret);
1240
 
                goto err_probe;
1241
 
        }
1242
 
 
1243
 
        for_each_component_dais(component, dai) {
1244
 
                ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1245
 
                if (ret != 0) {
1246
 
                        dev_err(component->dev,
1247
 
                                "Failed to create DAI widgets %d\n", ret);
1248
 
                        goto err_probe;
1249
 
                }
1250
 
        }
1251
 
 
1252
 
        ret = snd_soc_component_probe(component);
1253
 
        if (ret < 0) {
1254
 
                dev_err(component->dev,
1255
 
                        "ASoC: failed to probe component %d\n", ret);
1256
 
                goto err_probe;
1257
 
        }
1258
 
        WARN(dapm->idle_bias_off &&
1259
 
             dapm->bias_level != SND_SOC_BIAS_OFF,
1260
 
             "codec %s can not start from non-off bias with idle_bias_off==1\n",
1261
 
             component->name);
1262
 
 
1263
 
        /* machine specific init */
1264
 
        if (component->init) {
1265
 
                ret = component->init(component);
1266
 
                if (ret < 0) {
1267
 
                        dev_err(component->dev,
1268
 
                                "Failed to do machine specific init %d\n", ret);
1269
 
                        goto err_probe;
1270
 
                }
1271
 
        }
1272
 
 
1273
 
        ret = snd_soc_add_component_controls(component,
1274
 
                                             component->driver->controls,
1275
 
                                             component->driver->num_controls);
1276
 
        if (ret < 0)
1277
 
                goto err_probe;
1278
 
 
1279
 
        ret = snd_soc_dapm_add_routes(dapm,
1280
 
                                      component->driver->dapm_routes,
1281
 
                                      component->driver->num_dapm_routes);
1282
 
        if (ret < 0)
1283
 
                goto err_probe;
1284
 
 
1285
 
        /* see for_each_card_components */
1286
 
        list_add(&component->card_list, &card->component_dev_list);
1287
 
 
1288
 
err_probe:
1289
 
        if (ret < 0)
1290
 
                soc_cleanup_component(component);
1291
 
 
1292
 
        return ret;
1293
 
}
1294
 
 
1295
 
static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1296
 
{
1297
 
        int err;
1298
 
 
1299
 
        if (!dai || !dai->probed || !dai->driver ||
1300
 
            dai->driver->remove_order != order)
1301
 
                return;
1302
 
 
1303
 
        err = snd_soc_dai_remove(dai);
1304
 
        if (err < 0)
1305
 
                dev_err(dai->dev,
1306
 
                        "ASoC: failed to remove %s: %d\n",
1307
 
                        dai->name, err);
1308
 
 
1309
 
        dai->probed = 0;
1310
 
}
1311
 
 
1312
 
static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1313
 
{
1314
 
        int ret;
1315
 
 
1316
 
        if (dai->probed ||
1317
 
            dai->driver->probe_order != order)
1318
 
                return 0;
1319
 
 
1320
 
        ret = snd_soc_dai_probe(dai);
1321
 
        if (ret < 0) {
1322
 
                dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1323
 
                        dai->name, ret);
1324
 
                return ret;
1325
 
        }
1326
 
 
1327
 
        dai->probed = 1;
1328
 
 
1329
 
        return 0;
1330
 
}
1331
 
 
1332
 
static void soc_remove_link_dais(struct snd_soc_card *card)
1333
 
{
1334
 
        int i;
1335
 
        struct snd_soc_dai *codec_dai;
1336
 
        struct snd_soc_pcm_runtime *rtd;
1337
 
        int order;
1338
 
 
1339
 
        for_each_comp_order(order) {
1340
 
                for_each_card_rtds(card, rtd) {
1341
 
                        /* remove the CODEC DAI */
1342
 
                        for_each_rtd_codec_dai(rtd, i, codec_dai)
1343
 
                                soc_remove_dai(codec_dai, order);
1344
 
 
1345
 
                        soc_remove_dai(rtd->cpu_dai, order);
1346
 
                }
1347
 
        }
1348
 
}
1349
 
 
1350
 
static int soc_probe_link_dais(struct snd_soc_card *card)
1351
 
{
1352
 
        struct snd_soc_dai *codec_dai;
1353
 
        struct snd_soc_pcm_runtime *rtd;
1354
 
        int i, order, ret;
1355
 
 
1356
 
        for_each_comp_order(order) {
1357
 
                for_each_card_rtds(card, rtd) {
1358
 
 
1359
 
                        dev_dbg(card->dev,
1360
 
                                "ASoC: probe %s dai link %d late %d\n",
1361
 
                                card->name, rtd->num, order);
1362
 
 
1363
 
                        ret = soc_probe_dai(rtd->cpu_dai, order);
1364
 
                        if (ret)
1365
 
                                return ret;
1366
 
 
1367
 
                        /* probe the CODEC DAI */
1368
 
                        for_each_rtd_codec_dai(rtd, i, codec_dai) {
1369
 
                                ret = soc_probe_dai(codec_dai, order);
1370
 
                                if (ret)
1371
 
                                        return ret;
1372
 
                        }
1373
 
                }
1374
 
        }
1375
 
 
1376
 
        return 0;
1377
 
}
1378
 
 
1379
 
static void soc_remove_link_components(struct snd_soc_card *card)
1380
 
{
1381
 
        struct snd_soc_component *component;
1382
 
        struct snd_soc_pcm_runtime *rtd;
1383
 
        struct snd_soc_rtdcom_list *rtdcom;
1384
 
        int order;
1385
 
 
1386
 
        for_each_comp_order(order) {
1387
 
                for_each_card_rtds(card, rtd) {
1388
 
                        for_each_rtd_components(rtd, rtdcom, component) {
1389
 
                                if (component->driver->remove_order != order)
1390
 
                                        continue;
1391
 
 
1392
 
                                soc_remove_component(component);
1393
 
                        }
1394
 
                }
1395
 
        }
1396
 
}
1397
 
 
1398
 
static int soc_probe_link_components(struct snd_soc_card *card)
1399
 
{
1400
 
        struct snd_soc_component *component;
1401
 
        struct snd_soc_pcm_runtime *rtd;
1402
 
        struct snd_soc_rtdcom_list *rtdcom;
1403
 
        int ret, order;
1404
 
 
1405
 
        for_each_comp_order(order) {
1406
 
                for_each_card_rtds(card, rtd) {
1407
 
                        for_each_rtd_components(rtd, rtdcom, component) {
1408
 
                                if (component->driver->probe_order != order)
1409
 
                                        continue;
1410
 
 
1411
 
                                ret = soc_probe_component(card, component);
1412
 
                                if (ret < 0)
1413
 
                                        return ret;
1414
 
                        }
1415
 
                }
1416
 
        }
1417
 
 
1418
 
        return 0;
1419
 
}
1420
 
 
1421
 
void snd_soc_disconnect_sync(struct device *dev)
1422
 
{
1423
 
        struct snd_soc_component *component =
1424
 
                        snd_soc_lookup_component(dev, NULL);
1425
 
 
1426
 
        if (!component || !component->card)
1427
 
                return;
1428
 
 
1429
 
        snd_card_disconnect_sync(component->card->snd_card);
1430
 
}
1431
 
EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1432
 
 
1433
 
/**
1434
 
 * snd_soc_add_dai_link - Add a DAI link dynamically
1435
 
 * @card: The ASoC card to which the DAI link is added
1436
 
 * @dai_link: The new DAI link to add
1437
 
 *
1438
 
 * This function adds a DAI link to the ASoC card's link list.
1439
 
 *
1440
 
 * Note: Topology can use this API to add DAI links when probing the
1441
 
 * topology component. And machine drivers can still define static
1442
 
 * DAI links in dai_link array.
1443
 
 */
1444
 
int snd_soc_add_dai_link(struct snd_soc_card *card,
1445
 
                struct snd_soc_dai_link *dai_link)
1446
 
{
1447
 
        int ret;
1448
 
 
1449
 
        lockdep_assert_held(&client_mutex);
1450
 
 
1451
 
        /*
1452
 
         * Notify the machine driver for extra initialization
1453
 
         */
1454
 
        if (card->add_dai_link)
1455
 
                card->add_dai_link(card, dai_link);
1456
 
 
1457
 
        ret = soc_bind_dai_link(card, dai_link);
1458
 
        if (ret < 0)
1459
 
                return ret;
1460
 
 
1461
 
        /* see for_each_card_links */
1462
 
        list_add_tail(&dai_link->list, &card->dai_link_list);
1463
 
 
1464
 
        return 0;
1465
 
}
1466
 
EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1467
 
 
1468
 
/**
1469
 
 * snd_soc_remove_dai_link - Remove a DAI link from the list
1470
 
 * @card: The ASoC card that owns the link
1471
 
 * @dai_link: The DAI link to remove
1472
 
 * @unbind: unbind dai link
1473
 
 *
1474
 
 * This function removes a DAI link from the ASoC card's link list.
1475
 
 *
1476
 
 * For DAI links previously added by topology, topology should
1477
 
 * remove them by using the dobj embedded in the link.
1478
 
 */
1479
 
void snd_soc_remove_dai_link(struct snd_soc_card *card,
1480
 
                             struct snd_soc_dai_link *dai_link,
1481
 
                             bool unbind)
1482
 
{
1483
 
        lockdep_assert_held(&client_mutex);
1484
 
 
1485
 
        /*
1486
 
         * Notify the machine driver for extra destruction
1487
 
         */
1488
 
        if (card->remove_dai_link)
1489
 
                card->remove_dai_link(card, dai_link);
1490
 
 
1491
 
        list_del(&dai_link->list);
1492
 
 
1493
 
        if (unbind)
1494
 
                soc_unbind_dai_link(card, dai_link);
1495
 
}
1496
 
EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1497
 
 
1498
 
static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1499
 
                                struct snd_soc_pcm_runtime *rtd)
1500
 
{
1501
 
        int i, ret = 0;
1502
 
 
1503
 
        for (i = 0; i < num_dais; ++i) {
1504
 
                struct snd_soc_dai_driver *drv = dais[i]->driver;
1505
 
 
1506
 
                if (drv->pcm_new)
1507
 
                        ret = drv->pcm_new(rtd, dais[i]);
1508
 
                if (ret < 0) {
1509
 
                        dev_err(dais[i]->dev,
1510
 
                                "ASoC: Failed to bind %s with pcm device\n",
1511
 
                                dais[i]->name);
1512
 
                        return ret;
1513
 
                }
1514
 
        }
1515
 
 
1516
 
        return 0;
1517
 
}
1518
 
 
1519
 
static int soc_link_init(struct snd_soc_card *card,
1520
 
                         struct snd_soc_pcm_runtime *rtd)
1521
 
{
1522
 
        struct snd_soc_dai_link *dai_link = rtd->dai_link;
1523
 
        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1524
 
        struct snd_soc_rtdcom_list *rtdcom;
1525
 
        struct snd_soc_component *component;
1526
 
        int ret, num;
1527
 
 
1528
 
        /* set default power off timeout */
1529
 
        rtd->pmdown_time = pmdown_time;
1530
 
 
1531
 
        /* do machine specific initialization */
1532
 
        if (dai_link->init) {
1533
 
                ret = dai_link->init(rtd);
1534
 
                if (ret < 0) {
1535
 
                        dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1536
 
                                dai_link->name, ret);
1537
 
                        return ret;
1538
 
                }
1539
 
        }
1540
 
 
1541
 
        if (dai_link->dai_fmt) {
1542
 
                ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1543
 
                if (ret)
1544
 
                        return ret;
1545
 
        }
1546
 
 
1547
 
        /* add DPCM sysfs entries */
1548
 
        soc_dpcm_debugfs_add(rtd);
1549
 
 
1550
 
        num = rtd->num;
1551
 
 
1552
 
        /*
1553
 
         * most drivers will register their PCMs using DAI link ordering but
1554
 
         * topology based drivers can use the DAI link id field to set PCM
1555
 
         * device number and then use rtd + a base offset of the BEs.
1556
 
         */
1557
 
        for_each_rtd_components(rtd, rtdcom, component) {
1558
 
                if (!component->driver->use_dai_pcm_id)
1559
 
                        continue;
1560
 
 
1561
 
                if (rtd->dai_link->no_pcm)
1562
 
                        num += component->driver->be_pcm_base;
1563
 
                else
1564
 
                        num = rtd->dai_link->id;
1565
 
        }
1566
 
 
1567
 
        /* create compress_device if possible */
1568
 
        ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1569
 
        if (ret != -ENOTSUPP) {
1570
 
                if (ret < 0)
1571
 
                        dev_err(card->dev, "ASoC: can't create compress %s\n",
1572
 
                                         dai_link->stream_name);
1573
 
                return ret;
1574
 
        }
1575
 
 
1576
 
        /* create the pcm */
1577
 
        ret = soc_new_pcm(rtd, num);
1578
 
        if (ret < 0) {
1579
 
                dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1580
 
                        dai_link->stream_name, ret);
1581
 
                return ret;
1582
 
        }
1583
 
        ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1584
 
        if (ret < 0)
1585
 
                return ret;
1586
 
        ret = soc_link_dai_pcm_new(rtd->codec_dais,
1587
 
                                   rtd->num_codecs, rtd);
1588
 
        return ret;
1589
 
}
1590
 
 
1591
 
static void soc_unbind_aux_dev(struct snd_soc_card *card)
1592
 
{
1593
 
        struct snd_soc_component *component, *_component;
1594
 
 
1595
 
        for_each_card_auxs_safe(card, component, _component) {
1596
 
                component->init = NULL;
1597
 
                list_del(&component->card_aux_list);
1598
 
        }
1599
 
}
1600
 
 
1601
 
static int soc_bind_aux_dev(struct snd_soc_card *card)
1602
 
{
1603
 
        struct snd_soc_component *component;
1604
 
        struct snd_soc_aux_dev *aux;
1605
 
        int i;
1606
 
 
1607
 
        for_each_card_pre_auxs(card, i, aux) {
1608
 
                /* codecs, usually analog devices */
1609
 
                component = soc_find_component(&aux->dlc);
1610
 
                if (!component)
1611
 
                        return -EPROBE_DEFER;
1612
 
 
1613
 
                component->init = aux->init;
1614
 
                /* see for_each_card_auxs */
1615
 
                list_add(&component->card_aux_list, &card->aux_comp_list);
1616
 
        }
1617
 
        return 0;
1618
 
}
1619
 
 
1620
 
static int soc_probe_aux_devices(struct snd_soc_card *card)
1621
 
{
1622
 
        struct snd_soc_component *comp;
1623
 
        int order;
1624
 
        int ret;
1625
 
 
1626
 
        for_each_comp_order(order) {
1627
 
                for_each_card_auxs(card, comp) {
1628
 
                        if (comp->driver->probe_order == order) {
1629
 
                                ret = soc_probe_component(card, comp);
1630
 
                                if (ret < 0) {
1631
 
                                        dev_err(card->dev,
1632
 
                                                "ASoC: failed to probe aux component %s %d\n",
1633
 
                                                comp->name, ret);
1634
 
                                        return ret;
1635
 
                                }
1636
 
                        }
1637
 
                }
1638
 
        }
1639
 
 
1640
 
        return 0;
1641
 
}
1642
 
 
1643
 
static void soc_remove_aux_devices(struct snd_soc_card *card)
1644
 
{
1645
 
        struct snd_soc_component *comp, *_comp;
1646
 
        int order;
1647
 
 
1648
 
        for_each_comp_order(order) {
1649
 
                for_each_card_auxs_safe(card, comp, _comp) {
1650
 
                        if (comp->driver->remove_order == order)
1651
 
                                soc_remove_component(comp);
1652
 
                }
1653
 
        }
1654
 
}
1655
 
 
1656
 
/**
1657
 
 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1658
 
 * @rtd: The runtime for which the DAI link format should be changed
1659
 
 * @dai_fmt: The new DAI link format
1660
 
 *
1661
 
 * This function updates the DAI link format for all DAIs connected to the DAI
1662
 
 * link for the specified runtime.
1663
 
 *
1664
 
 * Note: For setups with a static format set the dai_fmt field in the
1665
 
 * corresponding snd_dai_link struct instead of using this function.
1666
 
 *
1667
 
 * Returns 0 on success, otherwise a negative error code.
1668
 
 */
1669
 
int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1670
 
        unsigned int dai_fmt)
1671
 
{
1672
 
        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1673
 
        struct snd_soc_dai *codec_dai;
1674
 
        unsigned int i;
1675
 
        int ret;
1676
 
 
1677
 
        for_each_rtd_codec_dai(rtd, i, codec_dai) {
1678
 
                ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1679
 
                if (ret != 0 && ret != -ENOTSUPP) {
1680
 
                        dev_warn(codec_dai->dev,
1681
 
                                 "ASoC: Failed to set DAI format: %d\n", ret);
1682
 
                        return ret;
1683
 
                }
1684
 
        }
1685
 
 
1686
 
        /*
1687
 
         * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1688
 
         * the component which has non_legacy_dai_naming is Codec
1689
 
         */
1690
 
        if (cpu_dai->component->driver->non_legacy_dai_naming) {
1691
 
                unsigned int inv_dai_fmt;
1692
 
 
1693
 
                inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1694
 
                switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1695
 
                case SND_SOC_DAIFMT_CBM_CFM:
1696
 
                        inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1697
 
                        break;
1698
 
                case SND_SOC_DAIFMT_CBM_CFS:
1699
 
                        inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1700
 
                        break;
1701
 
                case SND_SOC_DAIFMT_CBS_CFM:
1702
 
                        inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1703
 
                        break;
1704
 
                case SND_SOC_DAIFMT_CBS_CFS:
1705
 
                        inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1706
 
                        break;
1707
 
                }
1708
 
 
1709
 
                dai_fmt = inv_dai_fmt;
1710
 
        }
1711
 
 
1712
 
        ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1713
 
        if (ret != 0 && ret != -ENOTSUPP) {
1714
 
                dev_warn(cpu_dai->dev,
1715
 
                         "ASoC: Failed to set DAI format: %d\n", ret);
1716
 
                return ret;
1717
 
        }
1718
 
 
1719
 
        return 0;
1720
 
}
1721
 
EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1722
 
 
1723
 
#ifdef CONFIG_DMI
1724
 
/*
1725
 
 * Trim special characters, and replace '-' with '_' since '-' is used to
1726
 
 * separate different DMI fields in the card long name. Only number and
1727
 
 * alphabet characters and a few separator characters are kept.
1728
 
 */
1729
 
static void cleanup_dmi_name(char *name)
1730
 
{
1731
 
        int i, j = 0;
1732
 
 
1733
 
        for (i = 0; name[i]; i++) {
1734
 
                if (isalnum(name[i]) || (name[i] == '.')
1735
 
                    || (name[i] == '_'))
1736
 
                        name[j++] = name[i];
1737
 
                else if (name[i] == '-')
1738
 
                        name[j++] = '_';
1739
 
        }
1740
 
 
1741
 
        name[j] = '\0';
1742
 
}
1743
 
 
1744
 
/*
1745
 
 * Check if a DMI field is valid, i.e. not containing any string
1746
 
 * in the black list.
1747
 
 */
1748
 
static int is_dmi_valid(const char *field)
1749
 
{
1750
 
        int i = 0;
1751
 
 
1752
 
        while (dmi_blacklist[i]) {
1753
 
                if (strstr(field, dmi_blacklist[i]))
1754
 
                        return 0;
1755
 
                i++;
1756
 
        }
1757
 
 
1758
 
        return 1;
1759
 
}
1760
 
 
1761
 
/**
1762
 
 * snd_soc_set_dmi_name() - Register DMI names to card
1763
 
 * @card: The card to register DMI names
1764
 
 * @flavour: The flavour "differentiator" for the card amongst its peers.
1765
 
 *
1766
 
 * An Intel machine driver may be used by many different devices but are
1767
 
 * difficult for userspace to differentiate, since machine drivers ususally
1768
 
 * use their own name as the card short name and leave the card long name
1769
 
 * blank. To differentiate such devices and fix bugs due to lack of
1770
 
 * device-specific configurations, this function allows DMI info to be used
1771
 
 * as the sound card long name, in the format of
1772
 
 * "vendor-product-version-board"
1773
 
 * (Character '-' is used to separate different DMI fields here).
1774
 
 * This will help the user space to load the device-specific Use Case Manager
1775
 
 * (UCM) configurations for the card.
1776
 
 *
1777
 
 * Possible card long names may be:
1778
 
 * DellInc.-XPS139343-01-0310JH
1779
 
 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1780
 
 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1781
 
 *
1782
 
 * This function also supports flavoring the card longname to provide
1783
 
 * the extra differentiation, like "vendor-product-version-board-flavor".
1784
 
 *
1785
 
 * We only keep number and alphabet characters and a few separator characters
1786
 
 * in the card long name since UCM in the user space uses the card long names
1787
 
 * as card configuration directory names and AudoConf cannot support special
1788
 
 * charactors like SPACE.
1789
 
 *
1790
 
 * Returns 0 on success, otherwise a negative error code.
1791
 
 */
1792
 
int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1793
 
{
1794
 
        const char *vendor, *product, *product_version, *board;
1795
 
        size_t longname_buf_size = sizeof(card->snd_card->longname);
1796
 
        size_t len;
1797
 
 
1798
 
        if (card->long_name)
1799
 
                return 0; /* long name already set by driver or from DMI */
1800
 
 
1801
 
        /* make up dmi long name as: vendor.product.version.board */
1802
 
        vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1803
 
        if (!vendor || !is_dmi_valid(vendor)) {
1804
 
                dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1805
 
                return 0;
1806
 
        }
1807
 
 
1808
 
        snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1809
 
                         "%s", vendor);
1810
 
        cleanup_dmi_name(card->dmi_longname);
1811
 
 
1812
 
        product = dmi_get_system_info(DMI_PRODUCT_NAME);
1813
 
        if (product && is_dmi_valid(product)) {
1814
 
                len = strlen(card->dmi_longname);
1815
 
                snprintf(card->dmi_longname + len,
1816
 
                         longname_buf_size - len,
1817
 
                         "-%s", product);
1818
 
 
1819
 
                len++;  /* skip the separator "-" */
1820
 
                if (len < longname_buf_size)
1821
 
                        cleanup_dmi_name(card->dmi_longname + len);
1822
 
 
1823
 
                /*
1824
 
                 * some vendors like Lenovo may only put a self-explanatory
1825
 
                 * name in the product version field
1826
 
                 */
1827
 
                product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1828
 
                if (product_version && is_dmi_valid(product_version)) {
1829
 
                        len = strlen(card->dmi_longname);
1830
 
                        snprintf(card->dmi_longname + len,
1831
 
                                 longname_buf_size - len,
1832
 
                                 "-%s", product_version);
1833
 
 
1834
 
                        len++;
1835
 
                        if (len < longname_buf_size)
1836
 
                                cleanup_dmi_name(card->dmi_longname + len);
1837
 
                }
1838
 
        }
1839
 
 
1840
 
        board = dmi_get_system_info(DMI_BOARD_NAME);
1841
 
        if (board && is_dmi_valid(board)) {
1842
 
                len = strlen(card->dmi_longname);
1843
 
                snprintf(card->dmi_longname + len,
1844
 
                         longname_buf_size - len,
1845
 
                         "-%s", board);
1846
 
 
1847
 
                len++;
1848
 
                if (len < longname_buf_size)
1849
 
                        cleanup_dmi_name(card->dmi_longname + len);
1850
 
        } else if (!product) {
1851
 
                /* fall back to using legacy name */
1852
 
                dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1853
 
                return 0;
1854
 
        }
1855
 
 
1856
 
        /* Add flavour to dmi long name */
1857
 
        if (flavour) {
1858
 
                len = strlen(card->dmi_longname);
1859
 
                snprintf(card->dmi_longname + len,
1860
 
                         longname_buf_size - len,
1861
 
                         "-%s", flavour);
1862
 
 
1863
 
                len++;
1864
 
                if (len < longname_buf_size)
1865
 
                        cleanup_dmi_name(card->dmi_longname + len);
1866
 
        }
1867
 
 
1868
 
        /* set the card long name */
1869
 
        card->long_name = card->dmi_longname;
1870
 
 
1871
 
        return 0;
1872
 
}
1873
 
EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1874
 
#endif /* CONFIG_DMI */
1875
 
 
1876
 
static void soc_check_tplg_fes(struct snd_soc_card *card)
1877
 
{
1878
 
        struct snd_soc_component *component;
1879
 
        const struct snd_soc_component_driver *comp_drv;
1880
 
        struct snd_soc_dai_link *dai_link;
1881
 
        int i;
1882
 
 
1883
 
        for_each_component(component) {
1884
 
 
1885
 
                /* does this component override BEs ? */
1886
 
                if (!component->driver->ignore_machine)
1887
 
                        continue;
1888
 
 
1889
 
                /* for this machine ? */
1890
 
                if (!strcmp(component->driver->ignore_machine,
1891
 
                            card->dev->driver->name))
1892
 
                        goto match;
1893
 
                if (strcmp(component->driver->ignore_machine,
1894
 
                           dev_name(card->dev)))
1895
 
                        continue;
1896
 
match:
1897
 
                /* machine matches, so override the rtd data */
1898
 
                for_each_card_prelinks(card, i, dai_link) {
1899
 
 
1900
 
                        /* ignore this FE */
1901
 
                        if (dai_link->dynamic) {
1902
 
                                dai_link->ignore = true;
1903
 
                                continue;
1904
 
                        }
1905
 
 
1906
 
                        dev_info(card->dev, "info: override BE DAI link %s\n",
1907
 
                                 card->dai_link[i].name);
1908
 
 
1909
 
                        /* override platform component */
1910
 
                        if (!dai_link->platforms) {
1911
 
                                dev_err(card->dev, "init platform error");
1912
 
                                continue;
1913
 
                        }
1914
 
                        dai_link->platforms->name = component->name;
1915
 
 
1916
 
                        /* convert non BE into BE */
1917
 
                        dai_link->no_pcm = 1;
1918
 
 
1919
 
                        /* override any BE fixups */
1920
 
                        dai_link->be_hw_params_fixup =
1921
 
                                component->driver->be_hw_params_fixup;
1922
 
 
1923
 
                        /*
1924
 
                         * most BE links don't set stream name, so set it to
1925
 
                         * dai link name if it's NULL to help bind widgets.
1926
 
                         */
1927
 
                        if (!dai_link->stream_name)
1928
 
                                dai_link->stream_name = dai_link->name;
1929
 
                }
1930
 
 
1931
 
                /* Inform userspace we are using alternate topology */
1932
 
                if (component->driver->topology_name_prefix) {
1933
 
 
1934
 
                        /* topology shortname created? */
1935
 
                        if (!card->topology_shortname_created) {
1936
 
                                comp_drv = component->driver;
1937
 
 
1938
 
                                snprintf(card->topology_shortname, 32, "%s-%s",
1939
 
                                         comp_drv->topology_name_prefix,
1940
 
                                         card->name);
1941
 
                                card->topology_shortname_created = true;
1942
 
                        }
1943
 
 
1944
 
                        /* use topology shortname */
1945
 
                        card->name = card->topology_shortname;
1946
 
                }
1947
 
        }
1948
 
}
1949
 
 
1950
 
#define soc_setup_card_name(name, name1, name2, norm)           \
1951
 
        __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1952
 
static void __soc_setup_card_name(char *name, int len,
1953
 
                                  const char *name1, const char *name2,
1954
 
                                  int normalization)
1955
 
{
1956
 
        int i;
1957
 
 
1958
 
        snprintf(name, len, "%s", name1 ? name1 : name2);
1959
 
 
1960
 
        if (!normalization)
1961
 
                return;
1962
 
 
1963
 
        /*
1964
 
         * Name normalization
1965
 
         *
1966
 
         * The driver name is somewhat special, as it's used as a key for
1967
 
         * searches in the user-space.
1968
 
         *
1969
 
         * ex)
1970
 
         *      "abcd??efg" -> "abcd__efg"
1971
 
         */
1972
 
        for (i = 0; i < len; i++) {
1973
 
                switch (name[i]) {
1974
 
                case '_':
1975
 
                case '-':
1976
 
                case '\0':
1977
 
                        break;
1978
 
                default:
1979
 
                        if (!isalnum(name[i]))
1980
 
                                name[i] = '_';
1981
 
                        break;
1982
 
                }
1983
 
        }
1984
 
}
1985
 
 
1986
 
static void soc_cleanup_card_resources(struct snd_soc_card *card)
1987
 
{
1988
 
        struct snd_soc_dai_link *link, *_link;
1989
 
 
1990
 
        /* This should be called before snd_card_free() */
1991
 
        soc_remove_link_components(card);
1992
 
 
1993
 
        /* free the ALSA card at first; this syncs with pending operations */
1994
 
        if (card->snd_card) {
1995
 
                snd_card_free(card->snd_card);
1996
 
                card->snd_card = NULL;
1997
 
        }
1998
 
 
1999
 
        /* remove and free each DAI */
2000
 
        soc_remove_link_dais(card);
2001
 
 
2002
 
        for_each_card_links_safe(card, link, _link)
2003
 
                snd_soc_remove_dai_link(card, link, true);
2004
 
 
2005
 
        /* remove auxiliary devices */
2006
 
        soc_remove_aux_devices(card);
2007
 
        soc_unbind_aux_dev(card);
2008
 
 
2009
 
        snd_soc_dapm_free(&card->dapm);
2010
 
        soc_cleanup_card_debugfs(card);
2011
 
 
2012
 
        /* remove the card */
2013
 
        if (card->remove)
2014
 
                card->remove(card);
2015
 
}
2016
 
 
2017
 
static int snd_soc_instantiate_card(struct snd_soc_card *card)
2018
 
{
2019
 
        struct snd_soc_pcm_runtime *rtd;
2020
 
        struct snd_soc_dai_link *dai_link;
2021
 
        int ret, i;
2022
 
 
2023
 
        mutex_lock(&client_mutex);
2024
 
        mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2025
 
 
2026
 
        snd_soc_dapm_init(&card->dapm, card, NULL);
2027
 
 
2028
 
        /* check whether any platform is ignore machine FE and using topology */
2029
 
        soc_check_tplg_fes(card);
2030
 
 
2031
 
        /* bind aux_devs too */
2032
 
        ret = soc_bind_aux_dev(card);
2033
 
        if (ret < 0)
2034
 
                goto probe_end;
2035
 
 
2036
 
        /* add predefined DAI links to the list */
2037
 
        card->num_rtd = 0;
2038
 
        for_each_card_prelinks(card, i, dai_link) {
2039
 
                ret = snd_soc_add_dai_link(card, dai_link);
2040
 
                if (ret < 0)
2041
 
                        goto probe_end;
2042
 
        }
2043
 
 
2044
 
        /* card bind complete so register a sound card */
2045
 
        ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2046
 
                        card->owner, 0, &card->snd_card);
2047
 
        if (ret < 0) {
2048
 
                dev_err(card->dev,
2049
 
                        "ASoC: can't create sound card for card %s: %d\n",
2050
 
                        card->name, ret);
2051
 
                goto probe_end;
2052
 
        }
2053
 
 
2054
 
        soc_init_card_debugfs(card);
2055
 
 
2056
 
        soc_resume_init(card);
2057
 
 
2058
 
        ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2059
 
                                        card->num_dapm_widgets);
2060
 
        if (ret < 0)
2061
 
                goto probe_end;
2062
 
 
2063
 
        ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2064
 
                                        card->num_of_dapm_widgets);
2065
 
        if (ret < 0)
2066
 
                goto probe_end;
2067
 
 
2068
 
        /* initialise the sound card only once */
2069
 
        if (card->probe) {
2070
 
                ret = card->probe(card);
2071
 
                if (ret < 0)
2072
 
                        goto probe_end;
2073
 
        }
2074
 
 
2075
 
        /* probe all components used by DAI links on this card */
2076
 
        ret = soc_probe_link_components(card);
2077
 
        if (ret < 0) {
2078
 
                dev_err(card->dev,
2079
 
                        "ASoC: failed to instantiate card %d\n", ret);
2080
 
                goto probe_end;
2081
 
        }
2082
 
 
2083
 
        /* probe auxiliary components */
2084
 
        ret = soc_probe_aux_devices(card);
2085
 
        if (ret < 0)
2086
 
                goto probe_end;
2087
 
 
2088
 
        /* probe all DAI links on this card */
2089
 
        ret = soc_probe_link_dais(card);
2090
 
        if (ret < 0) {
2091
 
                dev_err(card->dev,
2092
 
                        "ASoC: failed to instantiate card %d\n", ret);
2093
 
                goto probe_end;
2094
 
        }
2095
 
 
2096
 
        for_each_card_rtds(card, rtd)
2097
 
                soc_link_init(card, rtd);
2098
 
 
2099
 
        snd_soc_dapm_link_dai_widgets(card);
2100
 
        snd_soc_dapm_connect_dai_link_widgets(card);
2101
 
 
2102
 
        ret = snd_soc_add_card_controls(card, card->controls,
2103
 
                                        card->num_controls);
2104
 
        if (ret < 0)
2105
 
                goto probe_end;
2106
 
 
2107
 
        ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2108
 
                                      card->num_dapm_routes);
2109
 
        if (ret < 0)
2110
 
                goto probe_end;
2111
 
 
2112
 
        ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2113
 
                                      card->num_of_dapm_routes);
2114
 
        if (ret < 0)
2115
 
                goto probe_end;
2116
 
 
2117
 
        /* try to set some sane longname if DMI is available */
2118
 
        snd_soc_set_dmi_name(card, NULL);
2119
 
 
2120
 
        soc_setup_card_name(card->snd_card->shortname,
2121
 
                            card->name, NULL, 0);
2122
 
        soc_setup_card_name(card->snd_card->longname,
2123
 
                            card->long_name, card->name, 0);
2124
 
        soc_setup_card_name(card->snd_card->driver,
2125
 
                            card->driver_name, card->name, 1);
2126
 
 
2127
 
        if (card->late_probe) {
2128
 
                ret = card->late_probe(card);
2129
 
                if (ret < 0) {
2130
 
                        dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2131
 
                                card->name, ret);
2132
 
                        goto probe_end;
2133
 
                }
2134
 
        }
2135
 
 
2136
 
        snd_soc_dapm_new_widgets(card);
2137
 
 
2138
 
        ret = snd_card_register(card->snd_card);
2139
 
        if (ret < 0) {
2140
 
                dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2141
 
                                ret);
2142
 
                goto probe_end;
2143
 
        }
2144
 
 
2145
 
        card->instantiated = 1;
2146
 
        dapm_mark_endpoints_dirty(card);
2147
 
        snd_soc_dapm_sync(&card->dapm);
2148
 
 
2149
 
probe_end:
2150
 
        if (ret < 0)
2151
 
                soc_cleanup_card_resources(card);
2152
 
 
2153
 
        mutex_unlock(&card->mutex);
2154
 
        mutex_unlock(&client_mutex);
2155
 
 
2156
 
        return ret;
2157
 
}
2158
 
 
2159
 
/* probes a new socdev */
2160
 
static int soc_probe(struct platform_device *pdev)
2161
 
{
2162
 
        struct snd_soc_card *card = platform_get_drvdata(pdev);
2163
 
 
2164
 
        /*
2165
 
         * no card, so machine driver should be registering card
2166
 
         * we should not be here in that case so ret error
2167
 
         */
2168
 
        if (!card)
2169
 
                return -EINVAL;
2170
 
 
2171
 
        dev_warn(&pdev->dev,
2172
 
                 "ASoC: machine %s should use snd_soc_register_card()\n",
2173
 
                 card->name);
2174
 
 
2175
 
        /* Bodge while we unpick instantiation */
2176
 
        card->dev = &pdev->dev;
2177
 
 
2178
 
        return snd_soc_register_card(card);
2179
 
}
2180
 
 
2181
 
/* removes a socdev */
2182
 
static int soc_remove(struct platform_device *pdev)
2183
 
{
2184
 
        struct snd_soc_card *card = platform_get_drvdata(pdev);
2185
 
 
2186
 
        snd_soc_unregister_card(card);
2187
 
        return 0;
2188
 
}
2189
 
 
2190
 
int snd_soc_poweroff(struct device *dev)
2191
 
{
2192
 
        struct snd_soc_card *card = dev_get_drvdata(dev);
2193
 
        struct snd_soc_pcm_runtime *rtd;
2194
 
 
2195
 
        if (!card->instantiated)
2196
 
                return 0;
2197
 
 
2198
 
        /*
2199
 
         * Flush out pmdown_time work - we actually do want to run it
2200
 
         * now, we're shutting down so no imminent restart.
2201
 
         */
2202
 
        snd_soc_flush_all_delayed_work(card);
2203
 
 
2204
 
        snd_soc_dapm_shutdown(card);
2205
 
 
2206
 
        /* deactivate pins to sleep state */
2207
 
        for_each_card_rtds(card, rtd) {
2208
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2209
 
                struct snd_soc_dai *codec_dai;
2210
 
                int i;
2211
 
 
2212
 
                pinctrl_pm_select_sleep_state(cpu_dai->dev);
2213
 
                for_each_rtd_codec_dai(rtd, i, codec_dai) {
2214
 
                        pinctrl_pm_select_sleep_state(codec_dai->dev);
2215
 
                }
2216
 
        }
2217
 
 
2218
 
        return 0;
2219
 
}
2220
 
EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2221
 
 
2222
 
const struct dev_pm_ops snd_soc_pm_ops = {
2223
 
        .suspend = snd_soc_suspend,
2224
 
        .resume = snd_soc_resume,
2225
 
        .freeze = snd_soc_suspend,
2226
 
        .thaw = snd_soc_resume,
2227
 
        .poweroff = snd_soc_poweroff,
2228
 
        .restore = snd_soc_resume,
2229
 
};
2230
 
EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2231
 
 
2232
 
/* ASoC platform driver */
2233
 
static struct platform_driver soc_driver = {
2234
 
        .driver         = {
2235
 
                .name           = "soc-audio",
2236
 
                .pm             = &snd_soc_pm_ops,
2237
 
        },
2238
 
        .probe          = soc_probe,
2239
 
        .remove         = soc_remove,
2240
 
};
2241
 
 
2242
 
/**
2243
 
 * snd_soc_cnew - create new control
2244
 
 * @_template: control template
2245
 
 * @data: control private data
2246
 
 * @long_name: control long name
2247
 
 * @prefix: control name prefix
2248
 
 *
2249
 
 * Create a new mixer control from a template control.
2250
 
 *
2251
 
 * Returns 0 for success, else error.
2252
 
 */
2253
 
struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2254
 
                                  void *data, const char *long_name,
2255
 
                                  const char *prefix)
2256
 
{
2257
 
        struct snd_kcontrol_new template;
2258
 
        struct snd_kcontrol *kcontrol;
2259
 
        char *name = NULL;
2260
 
 
2261
 
        memcpy(&template, _template, sizeof(template));
2262
 
        template.index = 0;
2263
 
 
2264
 
        if (!long_name)
2265
 
                long_name = template.name;
2266
 
 
2267
 
        if (prefix) {
2268
 
                name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2269
 
                if (!name)
2270
 
                        return NULL;
2271
 
 
2272
 
                template.name = name;
2273
 
        } else {
2274
 
                template.name = long_name;
2275
 
        }
2276
 
 
2277
 
        kcontrol = snd_ctl_new1(&template, data);
2278
 
 
2279
 
        kfree(name);
2280
 
 
2281
 
        return kcontrol;
2282
 
}
2283
 
EXPORT_SYMBOL_GPL(snd_soc_cnew);
2284
 
 
2285
 
static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2286
 
        const struct snd_kcontrol_new *controls, int num_controls,
2287
 
        const char *prefix, void *data)
2288
 
{
2289
 
        int err, i;
2290
 
 
2291
 
        for (i = 0; i < num_controls; i++) {
2292
 
                const struct snd_kcontrol_new *control = &controls[i];
2293
 
 
2294
 
                err = snd_ctl_add(card, snd_soc_cnew(control, data,
2295
 
                                                     control->name, prefix));
2296
 
                if (err < 0) {
2297
 
                        dev_err(dev, "ASoC: Failed to add %s: %d\n",
2298
 
                                control->name, err);
2299
 
                        return err;
2300
 
                }
2301
 
        }
2302
 
 
2303
 
        return 0;
2304
 
}
2305
 
 
2306
 
struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2307
 
                                               const char *name)
2308
 
{
2309
 
        struct snd_card *card = soc_card->snd_card;
2310
 
        struct snd_kcontrol *kctl;
2311
 
 
2312
 
        if (unlikely(!name))
2313
 
                return NULL;
2314
 
 
2315
 
        list_for_each_entry(kctl, &card->controls, list)
2316
 
                if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2317
 
                        return kctl;
2318
 
        return NULL;
2319
 
}
2320
 
EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2321
 
 
2322
 
/**
2323
 
 * snd_soc_add_component_controls - Add an array of controls to a component.
2324
 
 *
2325
 
 * @component: Component to add controls to
2326
 
 * @controls: Array of controls to add
2327
 
 * @num_controls: Number of elements in the array
2328
 
 *
2329
 
 * Return: 0 for success, else error.
2330
 
 */
2331
 
int snd_soc_add_component_controls(struct snd_soc_component *component,
2332
 
        const struct snd_kcontrol_new *controls, unsigned int num_controls)
2333
 
{
2334
 
        struct snd_card *card = component->card->snd_card;
2335
 
 
2336
 
        return snd_soc_add_controls(card, component->dev, controls,
2337
 
                        num_controls, component->name_prefix, component);
2338
 
}
2339
 
EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2340
 
 
2341
 
/**
2342
 
 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2343
 
 * Convenience function to add a list of controls.
2344
 
 *
2345
 
 * @soc_card: SoC card to add controls to
2346
 
 * @controls: array of controls to add
2347
 
 * @num_controls: number of elements in the array
2348
 
 *
2349
 
 * Return 0 for success, else error.
2350
 
 */
2351
 
int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2352
 
        const struct snd_kcontrol_new *controls, int num_controls)
2353
 
{
2354
 
        struct snd_card *card = soc_card->snd_card;
2355
 
 
2356
 
        return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2357
 
                        NULL, soc_card);
2358
 
}
2359
 
EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2360
 
 
2361
 
/**
2362
 
 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2363
 
 * Convienience function to add a list of controls.
2364
 
 *
2365
 
 * @dai: DAI to add controls to
2366
 
 * @controls: array of controls to add
2367
 
 * @num_controls: number of elements in the array
2368
 
 *
2369
 
 * Return 0 for success, else error.
2370
 
 */
2371
 
int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2372
 
        const struct snd_kcontrol_new *controls, int num_controls)
2373
 
{
2374
 
        struct snd_card *card = dai->component->card->snd_card;
2375
 
 
2376
 
        return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2377
 
                        NULL, dai);
2378
 
}
2379
 
EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2380
 
 
2381
 
static int snd_soc_bind_card(struct snd_soc_card *card)
2382
 
{
2383
 
        struct snd_soc_pcm_runtime *rtd;
2384
 
        int ret;
2385
 
 
2386
 
        ret = snd_soc_instantiate_card(card);
2387
 
        if (ret != 0)
2388
 
                return ret;
2389
 
 
2390
 
        /* deactivate pins to sleep state */
2391
 
        for_each_card_rtds(card, rtd) {
2392
 
                struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2393
 
                struct snd_soc_dai *codec_dai;
2394
 
                int j;
2395
 
 
2396
 
                for_each_rtd_codec_dai(rtd, j, codec_dai) {
2397
 
                        if (!codec_dai->active)
2398
 
                                pinctrl_pm_select_sleep_state(codec_dai->dev);
2399
 
                }
2400
 
 
2401
 
                if (!cpu_dai->active)
2402
 
                        pinctrl_pm_select_sleep_state(cpu_dai->dev);
2403
 
        }
2404
 
 
2405
 
        return ret;
2406
 
}
2407
 
 
2408
 
/**
2409
 
 * snd_soc_register_card - Register a card with the ASoC core
2410
 
 *
2411
 
 * @card: Card to register
2412
 
 *
2413
 
 */
2414
 
int snd_soc_register_card(struct snd_soc_card *card)
2415
 
{
2416
 
        if (!card->name || !card->dev)
2417
 
                return -EINVAL;
2418
 
 
2419
 
        dev_set_drvdata(card->dev, card);
2420
 
 
2421
 
        INIT_LIST_HEAD(&card->widgets);
2422
 
        INIT_LIST_HEAD(&card->paths);
2423
 
        INIT_LIST_HEAD(&card->dapm_list);
2424
 
        INIT_LIST_HEAD(&card->aux_comp_list);
2425
 
        INIT_LIST_HEAD(&card->component_dev_list);
2426
 
        INIT_LIST_HEAD(&card->list);
2427
 
        INIT_LIST_HEAD(&card->dai_link_list);
2428
 
        INIT_LIST_HEAD(&card->rtd_list);
2429
 
        INIT_LIST_HEAD(&card->dapm_dirty);
2430
 
        INIT_LIST_HEAD(&card->dobj_list);
2431
 
 
2432
 
        card->instantiated = 0;
2433
 
        mutex_init(&card->mutex);
2434
 
        mutex_init(&card->dapm_mutex);
2435
 
        mutex_init(&card->pcm_mutex);
2436
 
        spin_lock_init(&card->dpcm_lock);
2437
 
 
2438
 
        return snd_soc_bind_card(card);
2439
 
}
2440
 
EXPORT_SYMBOL_GPL(snd_soc_register_card);
2441
 
 
2442
 
static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2443
 
{
2444
 
        if (card->instantiated) {
2445
 
                card->instantiated = false;
2446
 
                snd_soc_dapm_shutdown(card);
2447
 
                snd_soc_flush_all_delayed_work(card);
2448
 
 
2449
 
                soc_cleanup_card_resources(card);
2450
 
                if (!unregister)
2451
 
                        list_add(&card->list, &unbind_card_list);
2452
 
        } else {
2453
 
                if (unregister)
2454
 
                        list_del(&card->list);
2455
 
        }
2456
 
}
2457
 
 
2458
 
/**
2459
 
 * snd_soc_unregister_card - Unregister a card with the ASoC core
2460
 
 *
2461
 
 * @card: Card to unregister
2462
 
 *
2463
 
 */
2464
 
int snd_soc_unregister_card(struct snd_soc_card *card)
2465
 
{
2466
 
        mutex_lock(&client_mutex);
2467
 
        snd_soc_unbind_card(card, true);
2468
 
        mutex_unlock(&client_mutex);
2469
 
        dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2470
 
 
2471
 
        return 0;
2472
 
}
2473
 
EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2474
 
 
2475
 
/*
2476
 
 * Simplify DAI link configuration by removing ".-1" from device names
2477
 
 * and sanitizing names.
2478
 
 */
2479
 
static char *fmt_single_name(struct device *dev, int *id)
2480
 
{
2481
 
        char *found, name[NAME_SIZE];
2482
 
        int id1, id2;
2483
 
 
2484
 
        if (dev_name(dev) == NULL)
2485
 
                return NULL;
2486
 
 
2487
 
        strlcpy(name, dev_name(dev), NAME_SIZE);
2488
 
 
2489
 
        /* are we a "%s.%d" name (platform and SPI components) */
2490
 
        found = strstr(name, dev->driver->name);
2491
 
        if (found) {
2492
 
                /* get ID */
2493
 
                if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2494
 
 
2495
 
                        /* discard ID from name if ID == -1 */
2496
 
                        if (*id == -1)
2497
 
                                found[strlen(dev->driver->name)] = '\0';
2498
 
                }
2499
 
 
2500
 
        } else {
2501
 
                /* I2C component devices are named "bus-addr" */
2502
 
                if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2503
 
                        char tmp[NAME_SIZE];
2504
 
 
2505
 
                        /* create unique ID number from I2C addr and bus */
2506
 
                        *id = ((id1 & 0xffff) << 16) + id2;
2507
 
 
2508
 
                        /* sanitize component name for DAI link creation */
2509
 
                        snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2510
 
                                 name);
2511
 
                        strlcpy(name, tmp, NAME_SIZE);
2512
 
                } else
2513
 
                        *id = 0;
2514
 
        }
2515
 
 
2516
 
        return devm_kstrdup(dev, name, GFP_KERNEL);
2517
 
}
2518
 
 
2519
 
/*
2520
 
 * Simplify DAI link naming for single devices with multiple DAIs by removing
2521
 
 * any ".-1" and using the DAI name (instead of device name).
2522
 
 */
2523
 
static inline char *fmt_multiple_name(struct device *dev,
2524
 
                struct snd_soc_dai_driver *dai_drv)
2525
 
{
2526
 
        if (dai_drv->name == NULL) {
2527
 
                dev_err(dev,
2528
 
                        "ASoC: error - multiple DAI %s registered with no name\n",
2529
 
                        dev_name(dev));
2530
 
                return NULL;
2531
 
        }
2532
 
 
2533
 
        return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2534
 
}
2535
 
 
2536
 
static void soc_del_dai(struct snd_soc_dai *dai)
2537
 
{
2538
 
        dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2539
 
        list_del(&dai->list);
2540
 
}
2541
 
 
2542
 
/* Create a DAI and add it to the component's DAI list */
2543
 
static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2544
 
        struct snd_soc_dai_driver *dai_drv,
2545
 
        bool legacy_dai_naming)
2546
 
{
2547
 
        struct device *dev = component->dev;
2548
 
        struct snd_soc_dai *dai;
2549
 
 
2550
 
        dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2551
 
 
2552
 
        dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2553
 
        if (dai == NULL)
2554
 
                return NULL;
2555
 
 
2556
 
        /*
2557
 
         * Back in the old days when we still had component-less DAIs,
2558
 
         * instead of having a static name, component-less DAIs would
2559
 
         * inherit the name of the parent device so it is possible to
2560
 
         * register multiple instances of the DAI. We still need to keep
2561
 
         * the same naming style even though those DAIs are not
2562
 
         * component-less anymore.
2563
 
         */
2564
 
        if (legacy_dai_naming &&
2565
 
            (dai_drv->id == 0 || dai_drv->name == NULL)) {
2566
 
                dai->name = fmt_single_name(dev, &dai->id);
2567
 
        } else {
2568
 
                dai->name = fmt_multiple_name(dev, dai_drv);
2569
 
                if (dai_drv->id)
2570
 
                        dai->id = dai_drv->id;
2571
 
                else
2572
 
                        dai->id = component->num_dai;
2573
 
        }
2574
 
        if (!dai->name)
2575
 
                return NULL;
2576
 
 
2577
 
        dai->component = component;
2578
 
        dai->dev = dev;
2579
 
        dai->driver = dai_drv;
2580
 
        if (!dai->driver->ops)
2581
 
                dai->driver->ops = &null_dai_ops;
2582
 
 
2583
 
        /* see for_each_component_dais */
2584
 
        list_add_tail(&dai->list, &component->dai_list);
2585
 
        component->num_dai++;
2586
 
 
2587
 
        dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2588
 
        return dai;
2589
 
}
2590
 
 
2591
 
void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2592
 
{
2593
 
        soc_del_dai(dai);
2594
 
}
2595
 
EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2596
 
 
2597
 
/**
2598
 
 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2599
 
 *
2600
 
 * @component: The component the DAIs are registered for
2601
 
 * @dai_drv: DAI driver to use for the DAI
2602
 
 *
2603
 
 * Topology can use this API to register DAIs when probing a component.
2604
 
 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2605
 
 * will be freed in the component cleanup.
2606
 
 */
2607
 
struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2608
 
                                         struct snd_soc_dai_driver *dai_drv,
2609
 
                                         bool legacy_dai_naming)
2610
 
{
2611
 
        struct device *dev = component->dev;
2612
 
 
2613
 
        dev_dbg(dev, "ASoC: dai register %s\n", dai_drv->name);
2614
 
 
2615
 
        lockdep_assert_held(&client_mutex);
2616
 
        return soc_add_dai(component, dai_drv, legacy_dai_naming);
2617
 
}
2618
 
EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2619
 
 
2620
 
/**
2621
 
 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2622
 
 *
2623
 
 * @component: The component for which the DAIs should be unregistered
2624
 
 */
2625
 
static void snd_soc_unregister_dais(struct snd_soc_component *component)
2626
 
{
2627
 
        struct snd_soc_dai *dai, *_dai;
2628
 
 
2629
 
        for_each_component_dais_safe(component, dai, _dai)
2630
 
                snd_soc_unregister_dai(dai);
2631
 
}
2632
 
 
2633
 
/**
2634
 
 * snd_soc_register_dais - Register a DAI with the ASoC core
2635
 
 *
2636
 
 * @component: The component the DAIs are registered for
2637
 
 * @dai_drv: DAI driver to use for the DAIs
2638
 
 * @count: Number of DAIs
2639
 
 */
2640
 
static int snd_soc_register_dais(struct snd_soc_component *component,
2641
 
                                 struct snd_soc_dai_driver *dai_drv,
2642
 
                                 size_t count)
2643
 
{
2644
 
        struct snd_soc_dai *dai;
2645
 
        unsigned int i;
2646
 
        int ret;
2647
 
 
2648
 
        for (i = 0; i < count; i++) {
2649
 
                dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2650
 
                                  !component->driver->non_legacy_dai_naming);
2651
 
                if (dai == NULL) {
2652
 
                        ret = -ENOMEM;
2653
 
                        goto err;
2654
 
                }
2655
 
        }
2656
 
 
2657
 
        return 0;
2658
 
 
2659
 
err:
2660
 
        snd_soc_unregister_dais(component);
2661
 
 
2662
 
        return ret;
2663
 
}
2664
 
 
2665
 
static int snd_soc_component_initialize(struct snd_soc_component *component,
2666
 
        const struct snd_soc_component_driver *driver, struct device *dev)
2667
 
{
2668
 
        INIT_LIST_HEAD(&component->dai_list);
2669
 
        INIT_LIST_HEAD(&component->dobj_list);
2670
 
        INIT_LIST_HEAD(&component->card_list);
2671
 
        mutex_init(&component->io_mutex);
2672
 
 
2673
 
        component->name = fmt_single_name(dev, &component->id);
2674
 
        if (!component->name) {
2675
 
                dev_err(dev, "ASoC: Failed to allocate name\n");
2676
 
                return -ENOMEM;
2677
 
        }
2678
 
 
2679
 
        component->dev = dev;
2680
 
        component->driver = driver;
2681
 
 
2682
 
        return 0;
2683
 
}
2684
 
 
2685
 
static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2686
 
{
2687
 
        int val_bytes = regmap_get_val_bytes(component->regmap);
2688
 
 
2689
 
        /* Errors are legitimate for non-integer byte multiples */
2690
 
        if (val_bytes > 0)
2691
 
                component->val_bytes = val_bytes;
2692
 
}
2693
 
 
2694
 
#ifdef CONFIG_REGMAP
2695
 
 
2696
 
/**
2697
 
 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2698
 
 *                                   component
2699
 
 * @component: The component for which to initialize the regmap instance
2700
 
 * @regmap: The regmap instance that should be used by the component
2701
 
 *
2702
 
 * This function allows deferred assignment of the regmap instance that is
2703
 
 * associated with the component. Only use this if the regmap instance is not
2704
 
 * yet ready when the component is registered. The function must also be called
2705
 
 * before the first IO attempt of the component.
2706
 
 */
2707
 
void snd_soc_component_init_regmap(struct snd_soc_component *component,
2708
 
        struct regmap *regmap)
2709
 
{
2710
 
        component->regmap = regmap;
2711
 
        snd_soc_component_setup_regmap(component);
2712
 
}
2713
 
EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2714
 
 
2715
 
/**
2716
 
 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2717
 
 *                                   component
2718
 
 * @component: The component for which to de-initialize the regmap instance
2719
 
 *
2720
 
 * Calls regmap_exit() on the regmap instance associated to the component and
2721
 
 * removes the regmap instance from the component.
2722
 
 *
2723
 
 * This function should only be used if snd_soc_component_init_regmap() was used
2724
 
 * to initialize the regmap instance.
2725
 
 */
2726
 
void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2727
 
{
2728
 
        regmap_exit(component->regmap);
2729
 
        component->regmap = NULL;
2730
 
}
2731
 
EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2732
 
 
2733
 
#endif
2734
 
 
2735
 
#define ENDIANNESS_MAP(name) \
2736
 
        (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2737
 
static u64 endianness_format_map[] = {
2738
 
        ENDIANNESS_MAP(S16_),
2739
 
        ENDIANNESS_MAP(U16_),
2740
 
        ENDIANNESS_MAP(S24_),
2741
 
        ENDIANNESS_MAP(U24_),
2742
 
        ENDIANNESS_MAP(S32_),
2743
 
        ENDIANNESS_MAP(U32_),
2744
 
        ENDIANNESS_MAP(S24_3),
2745
 
        ENDIANNESS_MAP(U24_3),
2746
 
        ENDIANNESS_MAP(S20_3),
2747
 
        ENDIANNESS_MAP(U20_3),
2748
 
        ENDIANNESS_MAP(S18_3),
2749
 
        ENDIANNESS_MAP(U18_3),
2750
 
        ENDIANNESS_MAP(FLOAT_),
2751
 
        ENDIANNESS_MAP(FLOAT64_),
2752
 
        ENDIANNESS_MAP(IEC958_SUBFRAME_),
2753
 
};
2754
 
 
2755
 
/*
2756
 
 * Fix up the DAI formats for endianness: codecs don't actually see
2757
 
 * the endianness of the data but we're using the CPU format
2758
 
 * definitions which do need to include endianness so we ensure that
2759
 
 * codec DAIs always have both big and little endian variants set.
2760
 
 */
2761
 
static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2762
 
{
2763
 
        int i;
2764
 
 
2765
 
        for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2766
 
                if (stream->formats & endianness_format_map[i])
2767
 
                        stream->formats |= endianness_format_map[i];
2768
 
}
2769
 
 
2770
 
static void snd_soc_try_rebind_card(void)
2771
 
{
2772
 
        struct snd_soc_card *card, *c;
2773
 
 
2774
 
        list_for_each_entry_safe(card, c, &unbind_card_list, list)
2775
 
                if (!snd_soc_bind_card(card))
2776
 
                        list_del(&card->list);
2777
 
}
2778
 
 
2779
 
static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2780
 
{
2781
 
        struct snd_soc_card *card = component->card;
2782
 
 
2783
 
        snd_soc_unregister_dais(component);
2784
 
 
2785
 
        if (card)
2786
 
                snd_soc_unbind_card(card, false);
2787
 
 
2788
 
        list_del(&component->list);
2789
 
}
2790
 
 
2791
 
int snd_soc_add_component(struct device *dev,
2792
 
                        struct snd_soc_component *component,
2793
 
                        const struct snd_soc_component_driver *component_driver,
2794
 
                        struct snd_soc_dai_driver *dai_drv,
2795
 
                        int num_dai)
2796
 
{
2797
 
        int ret;
2798
 
        int i;
2799
 
 
2800
 
        mutex_lock(&client_mutex);
2801
 
 
2802
 
        ret = snd_soc_component_initialize(component, component_driver, dev);
2803
 
        if (ret)
2804
 
                goto err_free;
2805
 
 
2806
 
        if (component_driver->endianness) {
2807
 
                for (i = 0; i < num_dai; i++) {
2808
 
                        convert_endianness_formats(&dai_drv[i].playback);
2809
 
                        convert_endianness_formats(&dai_drv[i].capture);
2810
 
                }
2811
 
        }
2812
 
 
2813
 
        ret = snd_soc_register_dais(component, dai_drv, num_dai);
2814
 
        if (ret < 0) {
2815
 
                dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2816
 
                goto err_cleanup;
2817
 
        }
2818
 
 
2819
 
        if (!component->driver->write && !component->driver->read) {
2820
 
                if (!component->regmap)
2821
 
                        component->regmap = dev_get_regmap(component->dev,
2822
 
                                                           NULL);
2823
 
                if (component->regmap)
2824
 
                        snd_soc_component_setup_regmap(component);
2825
 
        }
2826
 
 
2827
 
        /* see for_each_component */
2828
 
        list_add(&component->list, &component_list);
2829
 
 
2830
 
err_cleanup:
2831
 
        if (ret < 0)
2832
 
                snd_soc_del_component_unlocked(component);
2833
 
err_free:
2834
 
        mutex_unlock(&client_mutex);
2835
 
 
2836
 
        if (ret == 0)
2837
 
                snd_soc_try_rebind_card();
2838
 
 
2839
 
        return ret;
2840
 
}
2841
 
EXPORT_SYMBOL_GPL(snd_soc_add_component);
2842
 
 
2843
 
int snd_soc_register_component(struct device *dev,
2844
 
                        const struct snd_soc_component_driver *component_driver,
2845
 
                        struct snd_soc_dai_driver *dai_drv,
2846
 
                        int num_dai)
2847
 
{
2848
 
        struct snd_soc_component *component;
2849
 
 
2850
 
        component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2851
 
        if (!component)
2852
 
                return -ENOMEM;
2853
 
 
2854
 
        return snd_soc_add_component(dev, component, component_driver,
2855
 
                                     dai_drv, num_dai);
2856
 
}
2857
 
EXPORT_SYMBOL_GPL(snd_soc_register_component);
2858
 
 
2859
 
/**
2860
 
 * snd_soc_unregister_component - Unregister all related component
2861
 
 * from the ASoC core
2862
 
 *
2863
 
 * @dev: The device to unregister
2864
 
 */
2865
 
void snd_soc_unregister_component(struct device *dev)
2866
 
{
2867
 
        struct snd_soc_component *component;
2868
 
 
2869
 
        mutex_lock(&client_mutex);
2870
 
        while (1) {
2871
 
                component = snd_soc_lookup_component_nolocked(dev, NULL);
2872
 
                if (!component)
2873
 
                        break;
2874
 
 
2875
 
                snd_soc_del_component_unlocked(component);
2876
 
        }
2877
 
        mutex_unlock(&client_mutex);
2878
 
}
2879
 
EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2880
 
 
2881
 
/* Retrieve a card's name from device tree */
2882
 
int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2883
 
                               const char *propname)
2884
 
{
2885
 
        struct device_node *np;
2886
 
        int ret;
2887
 
 
2888
 
        if (!card->dev) {
2889
 
                pr_err("card->dev is not set before calling %s\n", __func__);
2890
 
                return -EINVAL;
2891
 
        }
2892
 
 
2893
 
        np = card->dev->of_node;
2894
 
 
2895
 
        ret = of_property_read_string_index(np, propname, 0, &card->name);
2896
 
        /*
2897
 
         * EINVAL means the property does not exist. This is fine providing
2898
 
         * card->name was previously set, which is checked later in
2899
 
         * snd_soc_register_card.
2900
 
         */
2901
 
        if (ret < 0 && ret != -EINVAL) {
2902
 
                dev_err(card->dev,
2903
 
                        "ASoC: Property '%s' could not be read: %d\n",
2904
 
                        propname, ret);
2905
 
                return ret;
2906
 
        }
2907
 
 
2908
 
        return 0;
2909
 
}
2910
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2911
 
 
2912
 
static const struct snd_soc_dapm_widget simple_widgets[] = {
2913
 
        SND_SOC_DAPM_MIC("Microphone", NULL),
2914
 
        SND_SOC_DAPM_LINE("Line", NULL),
2915
 
        SND_SOC_DAPM_HP("Headphone", NULL),
2916
 
        SND_SOC_DAPM_SPK("Speaker", NULL),
2917
 
};
2918
 
 
2919
 
int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2920
 
                                          const char *propname)
2921
 
{
2922
 
        struct device_node *np = card->dev->of_node;
2923
 
        struct snd_soc_dapm_widget *widgets;
2924
 
        const char *template, *wname;
2925
 
        int i, j, num_widgets, ret;
2926
 
 
2927
 
        num_widgets = of_property_count_strings(np, propname);
2928
 
        if (num_widgets < 0) {
2929
 
                dev_err(card->dev,
2930
 
                        "ASoC: Property '%s' does not exist\n", propname);
2931
 
                return -EINVAL;
2932
 
        }
2933
 
        if (num_widgets & 1) {
2934
 
                dev_err(card->dev,
2935
 
                        "ASoC: Property '%s' length is not even\n", propname);
2936
 
                return -EINVAL;
2937
 
        }
2938
 
 
2939
 
        num_widgets /= 2;
2940
 
        if (!num_widgets) {
2941
 
                dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2942
 
                        propname);
2943
 
                return -EINVAL;
2944
 
        }
2945
 
 
2946
 
        widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2947
 
                               GFP_KERNEL);
2948
 
        if (!widgets) {
2949
 
                dev_err(card->dev,
2950
 
                        "ASoC: Could not allocate memory for widgets\n");
2951
 
                return -ENOMEM;
2952
 
        }
2953
 
 
2954
 
        for (i = 0; i < num_widgets; i++) {
2955
 
                ret = of_property_read_string_index(np, propname,
2956
 
                        2 * i, &template);
2957
 
                if (ret) {
2958
 
                        dev_err(card->dev,
2959
 
                                "ASoC: Property '%s' index %d read error:%d\n",
2960
 
                                propname, 2 * i, ret);
2961
 
                        return -EINVAL;
2962
 
                }
2963
 
 
2964
 
                for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2965
 
                        if (!strncmp(template, simple_widgets[j].name,
2966
 
                                     strlen(simple_widgets[j].name))) {
2967
 
                                widgets[i] = simple_widgets[j];
2968
 
                                break;
2969
 
                        }
2970
 
                }
2971
 
 
2972
 
                if (j >= ARRAY_SIZE(simple_widgets)) {
2973
 
                        dev_err(card->dev,
2974
 
                                "ASoC: DAPM widget '%s' is not supported\n",
2975
 
                                template);
2976
 
                        return -EINVAL;
2977
 
                }
2978
 
 
2979
 
                ret = of_property_read_string_index(np, propname,
2980
 
                                                    (2 * i) + 1,
2981
 
                                                    &wname);
2982
 
                if (ret) {
2983
 
                        dev_err(card->dev,
2984
 
                                "ASoC: Property '%s' index %d read error:%d\n",
2985
 
                                propname, (2 * i) + 1, ret);
2986
 
                        return -EINVAL;
2987
 
                }
2988
 
 
2989
 
                widgets[i].name = wname;
2990
 
        }
2991
 
 
2992
 
        card->of_dapm_widgets = widgets;
2993
 
        card->num_of_dapm_widgets = num_widgets;
2994
 
 
2995
 
        return 0;
2996
 
}
2997
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2998
 
 
2999
 
int snd_soc_of_get_slot_mask(struct device_node *np,
3000
 
                             const char *prop_name,
3001
 
                             unsigned int *mask)
3002
 
{
3003
 
        u32 val;
3004
 
        const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3005
 
        int i;
3006
 
 
3007
 
        if (!of_slot_mask)
3008
 
                return 0;
3009
 
        val /= sizeof(u32);
3010
 
        for (i = 0; i < val; i++)
3011
 
                if (be32_to_cpup(&of_slot_mask[i]))
3012
 
                        *mask |= (1 << i);
3013
 
 
3014
 
        return val;
3015
 
}
3016
 
EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3017
 
 
3018
 
int snd_soc_of_parse_tdm_slot(struct device_node *np,
3019
 
                              unsigned int *tx_mask,
3020
 
                              unsigned int *rx_mask,
3021
 
                              unsigned int *slots,
3022
 
                              unsigned int *slot_width)
3023
 
{
3024
 
        u32 val;
3025
 
        int ret;
3026
 
 
3027
 
        if (tx_mask)
3028
 
                snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3029
 
        if (rx_mask)
3030
 
                snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3031
 
 
3032
 
        if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3033
 
                ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3034
 
                if (ret)
3035
 
                        return ret;
3036
 
 
3037
 
                if (slots)
3038
 
                        *slots = val;
3039
 
        }
3040
 
 
3041
 
        if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3042
 
                ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3043
 
                if (ret)
3044
 
                        return ret;
3045
 
 
3046
 
                if (slot_width)
3047
 
                        *slot_width = val;
3048
 
        }
3049
 
 
3050
 
        return 0;
3051
 
}
3052
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3053
 
 
3054
 
void snd_soc_of_parse_node_prefix(struct device_node *np,
3055
 
                                  struct snd_soc_codec_conf *codec_conf,
3056
 
                                  struct device_node *of_node,
3057
 
                                  const char *propname)
3058
 
{
3059
 
        const char *str;
3060
 
        int ret;
3061
 
 
3062
 
        ret = of_property_read_string(np, propname, &str);
3063
 
        if (ret < 0) {
3064
 
                /* no prefix is not error */
3065
 
                return;
3066
 
        }
3067
 
 
3068
 
        codec_conf->of_node     = of_node;
3069
 
        codec_conf->name_prefix = str;
3070
 
}
3071
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3072
 
 
3073
 
int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3074
 
                                   const char *propname)
3075
 
{
3076
 
        struct device_node *np = card->dev->of_node;
3077
 
        int num_routes;
3078
 
        struct snd_soc_dapm_route *routes;
3079
 
        int i, ret;
3080
 
 
3081
 
        num_routes = of_property_count_strings(np, propname);
3082
 
        if (num_routes < 0 || num_routes & 1) {
3083
 
                dev_err(card->dev,
3084
 
                        "ASoC: Property '%s' does not exist or its length is not even\n",
3085
 
                        propname);
3086
 
                return -EINVAL;
3087
 
        }
3088
 
        num_routes /= 2;
3089
 
        if (!num_routes) {
3090
 
                dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3091
 
                        propname);
3092
 
                return -EINVAL;
3093
 
        }
3094
 
 
3095
 
        routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3096
 
                              GFP_KERNEL);
3097
 
        if (!routes) {
3098
 
                dev_err(card->dev,
3099
 
                        "ASoC: Could not allocate DAPM route table\n");
3100
 
                return -EINVAL;
3101
 
        }
3102
 
 
3103
 
        for (i = 0; i < num_routes; i++) {
3104
 
                ret = of_property_read_string_index(np, propname,
3105
 
                        2 * i, &routes[i].sink);
3106
 
                if (ret) {
3107
 
                        dev_err(card->dev,
3108
 
                                "ASoC: Property '%s' index %d could not be read: %d\n",
3109
 
                                propname, 2 * i, ret);
3110
 
                        return -EINVAL;
3111
 
                }
3112
 
                ret = of_property_read_string_index(np, propname,
3113
 
                        (2 * i) + 1, &routes[i].source);
3114
 
                if (ret) {
3115
 
                        dev_err(card->dev,
3116
 
                                "ASoC: Property '%s' index %d could not be read: %d\n",
3117
 
                                propname, (2 * i) + 1, ret);
3118
 
                        return -EINVAL;
3119
 
                }
3120
 
        }
3121
 
 
3122
 
        card->num_of_dapm_routes = num_routes;
3123
 
        card->of_dapm_routes = routes;
3124
 
 
3125
 
        return 0;
3126
 
}
3127
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3128
 
 
3129
 
unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3130
 
                                     const char *prefix,
3131
 
                                     struct device_node **bitclkmaster,
3132
 
                                     struct device_node **framemaster)
3133
 
{
3134
 
        int ret, i;
3135
 
        char prop[128];
3136
 
        unsigned int format = 0;
3137
 
        int bit, frame;
3138
 
        const char *str;
3139
 
        struct {
3140
 
                char *name;
3141
 
                unsigned int val;
3142
 
        } of_fmt_table[] = {
3143
 
                { "i2s",        SND_SOC_DAIFMT_I2S },
3144
 
                { "right_j",    SND_SOC_DAIFMT_RIGHT_J },
3145
 
                { "left_j",     SND_SOC_DAIFMT_LEFT_J },
3146
 
                { "dsp_a",      SND_SOC_DAIFMT_DSP_A },
3147
 
                { "dsp_b",      SND_SOC_DAIFMT_DSP_B },
3148
 
                { "ac97",       SND_SOC_DAIFMT_AC97 },
3149
 
                { "pdm",        SND_SOC_DAIFMT_PDM},
3150
 
                { "msb",        SND_SOC_DAIFMT_MSB },
3151
 
                { "lsb",        SND_SOC_DAIFMT_LSB },
3152
 
        };
3153
 
 
3154
 
        if (!prefix)
3155
 
                prefix = "";
3156
 
 
3157
 
        /*
3158
 
         * check "dai-format = xxx"
3159
 
         * or    "[prefix]format = xxx"
3160
 
         * SND_SOC_DAIFMT_FORMAT_MASK area
3161
 
         */
3162
 
        ret = of_property_read_string(np, "dai-format", &str);
3163
 
        if (ret < 0) {
3164
 
                snprintf(prop, sizeof(prop), "%sformat", prefix);
3165
 
                ret = of_property_read_string(np, prop, &str);
3166
 
        }
3167
 
        if (ret == 0) {
3168
 
                for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3169
 
                        if (strcmp(str, of_fmt_table[i].name) == 0) {
3170
 
                                format |= of_fmt_table[i].val;
3171
 
                                break;
3172
 
                        }
3173
 
                }
3174
 
        }
3175
 
 
3176
 
        /*
3177
 
         * check "[prefix]continuous-clock"
3178
 
         * SND_SOC_DAIFMT_CLOCK_MASK area
3179
 
         */
3180
 
        snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3181
 
        if (of_property_read_bool(np, prop))
3182
 
                format |= SND_SOC_DAIFMT_CONT;
3183
 
        else
3184
 
                format |= SND_SOC_DAIFMT_GATED;
3185
 
 
3186
 
        /*
3187
 
         * check "[prefix]bitclock-inversion"
3188
 
         * check "[prefix]frame-inversion"
3189
 
         * SND_SOC_DAIFMT_INV_MASK area
3190
 
         */
3191
 
        snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3192
 
        bit = !!of_get_property(np, prop, NULL);
3193
 
 
3194
 
        snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3195
 
        frame = !!of_get_property(np, prop, NULL);
3196
 
 
3197
 
        switch ((bit << 4) + frame) {
3198
 
        case 0x11:
3199
 
                format |= SND_SOC_DAIFMT_IB_IF;
3200
 
                break;
3201
 
        case 0x10:
3202
 
                format |= SND_SOC_DAIFMT_IB_NF;
3203
 
                break;
3204
 
        case 0x01:
3205
 
                format |= SND_SOC_DAIFMT_NB_IF;
3206
 
                break;
3207
 
        default:
3208
 
                /* SND_SOC_DAIFMT_NB_NF is default */
3209
 
                break;
3210
 
        }
3211
 
 
3212
 
        /*
3213
 
         * check "[prefix]bitclock-master"
3214
 
         * check "[prefix]frame-master"
3215
 
         * SND_SOC_DAIFMT_MASTER_MASK area
3216
 
         */
3217
 
        snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3218
 
        bit = !!of_get_property(np, prop, NULL);
3219
 
        if (bit && bitclkmaster)
3220
 
                *bitclkmaster = of_parse_phandle(np, prop, 0);
3221
 
 
3222
 
        snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3223
 
        frame = !!of_get_property(np, prop, NULL);
3224
 
        if (frame && framemaster)
3225
 
                *framemaster = of_parse_phandle(np, prop, 0);
3226
 
 
3227
 
        switch ((bit << 4) + frame) {
3228
 
        case 0x11:
3229
 
                format |= SND_SOC_DAIFMT_CBM_CFM;
3230
 
                break;
3231
 
        case 0x10:
3232
 
                format |= SND_SOC_DAIFMT_CBM_CFS;
3233
 
                break;
3234
 
        case 0x01:
3235
 
                format |= SND_SOC_DAIFMT_CBS_CFM;
3236
 
                break;
3237
 
        default:
3238
 
                format |= SND_SOC_DAIFMT_CBS_CFS;
3239
 
                break;
3240
 
        }
3241
 
 
3242
 
        return format;
3243
 
}
3244
 
EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3245
 
 
3246
 
int snd_soc_get_dai_id(struct device_node *ep)
3247
 
{
3248
 
        struct snd_soc_component *component;
3249
 
        struct snd_soc_dai_link_component dlc;
3250
 
        int ret;
3251
 
 
3252
 
        dlc.of_node     = of_graph_get_port_parent(ep);
3253
 
        dlc.name        = NULL;
3254
 
        /*
3255
 
         * For example HDMI case, HDMI has video/sound port,
3256
 
         * but ALSA SoC needs sound port number only.
3257
 
         * Thus counting HDMI DT port/endpoint doesn't work.
3258
 
         * Then, it should have .of_xlate_dai_id
3259
 
         */
3260
 
        ret = -ENOTSUPP;
3261
 
        mutex_lock(&client_mutex);
3262
 
        component = soc_find_component(&dlc);
3263
 
        if (component)
3264
 
                ret = snd_soc_component_of_xlate_dai_id(component, ep);
3265
 
        mutex_unlock(&client_mutex);
3266
 
 
3267
 
        of_node_put(dlc.of_node);
3268
 
 
3269
 
        return ret;
3270
 
}
3271
 
EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3272
 
 
3273
 
int snd_soc_get_dai_name(struct of_phandle_args *args,
3274
 
                                const char **dai_name)
3275
 
{
3276
 
        struct snd_soc_component *pos;
3277
 
        struct device_node *component_of_node;
3278
 
        int ret = -EPROBE_DEFER;
3279
 
 
3280
 
        mutex_lock(&client_mutex);
3281
 
        for_each_component(pos) {
3282
 
                component_of_node = soc_component_to_node(pos);
3283
 
 
3284
 
                if (component_of_node != args->np)
3285
 
                        continue;
3286
 
 
3287
 
                ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3288
 
                if (ret == -ENOTSUPP) {
3289
 
                        struct snd_soc_dai *dai;
3290
 
                        int id = -1;
3291
 
 
3292
 
                        switch (args->args_count) {
3293
 
                        case 0:
3294
 
                                id = 0; /* same as dai_drv[0] */
3295
 
                                break;
3296
 
                        case 1:
3297
 
                                id = args->args[0];
3298
 
                                break;
3299
 
                        default:
3300
 
                                /* not supported */
3301
 
                                break;
3302
 
                        }
3303
 
 
3304
 
                        if (id < 0 || id >= pos->num_dai) {
3305
 
                                ret = -EINVAL;
3306
 
                                continue;
3307
 
                        }
3308
 
 
3309
 
                        ret = 0;
3310
 
 
3311
 
                        /* find target DAI */
3312
 
                        for_each_component_dais(pos, dai) {
3313
 
                                if (id == 0)
3314
 
                                        break;
3315
 
                                id--;
3316
 
                        }
3317
 
 
3318
 
                        *dai_name = dai->driver->name;
3319
 
                        if (!*dai_name)
3320
 
                                *dai_name = pos->name;
3321
 
                }
3322
 
 
3323
 
                break;
3324
 
        }
3325
 
        mutex_unlock(&client_mutex);
3326
 
        return ret;
3327
 
}
3328
 
EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3329
 
 
3330
 
int snd_soc_of_get_dai_name(struct device_node *of_node,
3331
 
                            const char **dai_name)
3332
 
{
3333
 
        struct of_phandle_args args;
3334
 
        int ret;
3335
 
 
3336
 
        ret = of_parse_phandle_with_args(of_node, "sound-dai",
3337
 
                                         "#sound-dai-cells", 0, &args);
3338
 
        if (ret)
3339
 
                return ret;
3340
 
 
3341
 
        ret = snd_soc_get_dai_name(&args, dai_name);
3342
 
 
3343
 
        of_node_put(args.np);
3344
 
 
3345
 
        return ret;
3346
 
}
3347
 
EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3348
 
 
3349
 
/*
3350
 
 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3351
 
 * @dai_link: DAI link
3352
 
 *
3353
 
 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3354
 
 */
3355
 
void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3356
 
{
3357
 
        struct snd_soc_dai_link_component *component;
3358
 
        int index;
3359
 
 
3360
 
        for_each_link_codecs(dai_link, index, component) {
3361
 
                if (!component->of_node)
3362
 
                        break;
3363
 
                of_node_put(component->of_node);
3364
 
                component->of_node = NULL;
3365
 
        }
3366
 
}
3367
 
EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3368
 
 
3369
 
/*
3370
 
 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3371
 
 * @dev: Card device
3372
 
 * @of_node: Device node
3373
 
 * @dai_link: DAI link
3374
 
 *
3375
 
 * Builds an array of CODEC DAI components from the DAI link property
3376
 
 * 'sound-dai'.
3377
 
 * The array is set in the DAI link and the number of DAIs is set accordingly.
3378
 
 * The device nodes in the array (of_node) must be dereferenced by calling
3379
 
 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3380
 
 *
3381
 
 * Returns 0 for success
3382
 
 */
3383
 
int snd_soc_of_get_dai_link_codecs(struct device *dev,
3384
 
                                   struct device_node *of_node,
3385
 
                                   struct snd_soc_dai_link *dai_link)
3386
 
{
3387
 
        struct of_phandle_args args;
3388
 
        struct snd_soc_dai_link_component *component;
3389
 
        char *name;
3390
 
        int index, num_codecs, ret;
3391
 
 
3392
 
        /* Count the number of CODECs */
3393
 
        name = "sound-dai";
3394
 
        num_codecs = of_count_phandle_with_args(of_node, name,
3395
 
                                                "#sound-dai-cells");
3396
 
        if (num_codecs <= 0) {
3397
 
                if (num_codecs == -ENOENT)
3398
 
                        dev_err(dev, "No 'sound-dai' property\n");
3399
 
                else
3400
 
                        dev_err(dev, "Bad phandle in 'sound-dai'\n");
3401
 
                return num_codecs;
3402
 
        }
3403
 
        component = devm_kcalloc(dev,
3404
 
                                 num_codecs, sizeof(*component),
3405
 
                                 GFP_KERNEL);
3406
 
        if (!component)
3407
 
                return -ENOMEM;
3408
 
        dai_link->codecs = component;
3409
 
        dai_link->num_codecs = num_codecs;
3410
 
 
3411
 
        /* Parse the list */
3412
 
        for_each_link_codecs(dai_link, index, component) {
3413
 
                ret = of_parse_phandle_with_args(of_node, name,
3414
 
                                                 "#sound-dai-cells",
3415
 
                                                 index, &args);
3416
 
                if (ret)
3417
 
                        goto err;
3418
 
                component->of_node = args.np;
3419
 
                ret = snd_soc_get_dai_name(&args, &component->dai_name);
3420
 
                if (ret < 0)
3421
 
                        goto err;
3422
 
        }
3423
 
        return 0;
3424
 
err:
3425
 
        snd_soc_of_put_dai_link_codecs(dai_link);
3426
 
        dai_link->codecs = NULL;
3427
 
        dai_link->num_codecs = 0;
3428
 
        return ret;
3429
 
}
3430
 
EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3431
 
 
3432
 
static int __init snd_soc_init(void)
3433
 
{
3434
 
        snd_soc_debugfs_init();
3435
 
        snd_soc_util_init();
3436
 
 
3437
 
        return platform_driver_register(&soc_driver);
3438
 
}
3439
 
module_init(snd_soc_init);
3440
 
 
3441
 
static void __exit snd_soc_exit(void)
3442
 
{
3443
 
        snd_soc_util_exit();
3444
 
        snd_soc_debugfs_exit();
3445
 
 
3446
 
        platform_driver_unregister(&soc_driver);
3447
 
}
3448
 
module_exit(snd_soc_exit);
3449
 
 
3450
 
/* Module information */
3451
 
MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3452
 
MODULE_DESCRIPTION("ALSA SoC Core");
3453
 
MODULE_LICENSE("GPL");
3454
 
MODULE_ALIAS("platform:soc-audio");