~ubuntu-audio-dev/alsa-driver/tiwai-trunk2

« back to all changes in this revision

Viewing changes to soc/codecs/hdac_hdmi.c

  • Committer: Canonistack server
  • Date: 2016-05-16 13:04:41 UTC
  • Revision ID: david.henningsson@canonical.com-20160516130441-xmcvo5v7n0mupdei
Test run of 623 machines: 3 failing with 3 errors and 0 warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <sound/hdaudio_ext.h>
30
30
#include <sound/hda_i915.h>
31
31
#include <sound/pcm_drm_eld.h>
 
32
#include <sound/hda_chmap.h>
32
33
#include "../../hda/local.h"
33
34
#include "hdac_hdmi.h"
34
35
 
60
61
        struct hdac_hdmi_cvt_params params;
61
62
};
62
63
 
 
64
/* Currently only spk_alloc, more to be added */
 
65
struct hdac_hdmi_parsed_eld {
 
66
        u8 spk_alloc;
 
67
};
 
68
 
63
69
struct hdac_hdmi_eld {
64
70
        bool    monitor_present;
65
71
        bool    eld_valid;
66
72
        int     eld_size;
67
73
        char    eld_buffer[ELD_MAX_SIZE];
 
74
        struct  hdac_hdmi_parsed_eld info;
68
75
};
69
76
 
70
77
struct hdac_hdmi_pin {
76
83
        struct hdac_ext_device *edev;
77
84
        int repoll_count;
78
85
        struct delayed_work work;
 
86
        struct mutex lock;
 
87
        bool chmap_set;
 
88
        unsigned char chmap[8]; /* ALSA API channel-map */
 
89
        int channels; /* current number of channels */
79
90
};
80
91
 
81
92
struct hdac_hdmi_pcm {
100
111
        int num_pin;
101
112
        int num_cvt;
102
113
        struct mutex pin_mutex;
 
114
        struct hdac_chmap chmap;
103
115
};
104
116
 
 
117
static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
 
118
                                                int pcm_idx)
 
119
{
 
120
        struct hdac_hdmi_pcm *pcm;
 
121
 
 
122
        list_for_each_entry(pcm, &hdmi->pcm_list, head) {
 
123
                if (pcm->pcm_id == pcm_idx)
 
124
                        return pcm;
 
125
        }
 
126
 
 
127
        return NULL;
 
128
}
 
129
 
105
130
static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106
131
{
107
132
        struct hdac_device *hdac = dev_to_hdac_dev(dev);
278
303
        int i;
279
304
        const u8 *eld_buf;
280
305
        u8 conn_type;
281
 
        int channels = 2;
 
306
        int channels, ca;
282
307
 
283
308
        list_for_each_entry(pin, &hdmi->pin_list, head) {
284
309
                if (pin->nid == pin_nid)
285
310
                        break;
286
311
        }
287
312
 
 
313
        ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
 
314
                        pin->channels, pin->chmap_set, true, pin->chmap);
 
315
 
 
316
        channels = snd_hdac_get_active_channels(ca);
 
317
        hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
 
318
 
 
319
        snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
 
320
                                pin->channels, pin->chmap, pin->chmap_set);
 
321
 
288
322
        eld_buf = pin->eld.eld_buffer;
289
323
        conn_type = drm_eld_get_conn_type(eld_buf);
290
324
 
291
 
        /* setup channel count */
292
 
        snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
293
 
                            AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
294
 
 
295
325
        switch (conn_type) {
296
326
        case DRM_ELD_CONN_TYPE_HDMI:
297
327
                hdmi_audio_infoframe_init(&frame);
298
328
 
299
 
                /* Default stereo for now */
300
329
                frame.channels = channels;
 
330
                frame.channel_allocation = ca;
301
331
 
302
332
                ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
303
333
                if (ret < 0)
311
341
                dp_ai.len       = 0x1b;
312
342
                dp_ai.ver       = 0x11 << 2;
313
343
                dp_ai.CC02_CT47 = channels - 1;
314
 
                dp_ai.CA        = 0;
 
344
                dp_ai.CA        = ca;
315
345
 
316
346
                dip = (u8 *)&dp_ai;
317
347
                break;
370
400
        struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
371
401
        struct hdac_hdmi_priv *hdmi = hdac->private_data;
372
402
        struct hdac_hdmi_dai_pin_map *dai_map;
 
403
        struct hdac_hdmi_pin *pin;
373
404
        struct hdac_ext_dma_params *dd;
374
405
        int ret;
375
406
 
376
407
        dai_map = &hdmi->dai_map[dai->id];
 
408
        pin = dai_map->pin;
377
409
 
378
410
        dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
379
411
        dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
380
412
                        dd->stream_tag, dd->format);
381
413
 
 
414
        mutex_lock(&pin->lock);
 
415
        pin->channels = substream->runtime->channels;
 
416
 
382
417
        ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
383
418
                                                dai_map->pin->nid);
 
419
        mutex_unlock(&pin->lock);
384
420
        if (ret < 0)
385
421
                return ret;
386
422
 
640
676
                snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
641
677
                        AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
642
678
 
 
679
                mutex_lock(&dai_map->pin->lock);
 
680
                dai_map->pin->chmap_set = false;
 
681
                memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
 
682
                dai_map->pin->channels = 0;
 
683
                mutex_unlock(&dai_map->pin->lock);
 
684
 
643
685
                dai_map->pin = NULL;
644
686
        }
645
687
}
647
689
static int
648
690
hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
649
691
{
 
692
        unsigned int chans;
 
693
        struct hdac_ext_device *edev = to_ehdac_device(hdac);
 
694
        struct hdac_hdmi_priv *hdmi = edev->private_data;
650
695
        int err;
651
696
 
652
 
        /* Only stereo supported as of now */
653
 
        cvt->params.channels_min = cvt->params.channels_max = 2;
 
697
        chans = get_wcaps(hdac, cvt->nid);
 
698
        chans = get_wcaps_channels(chans);
 
699
 
 
700
        cvt->params.channels_min = 2;
 
701
 
 
702
        cvt->params.channels_max = chans;
 
703
        if (chans > hdmi->chmap.channels_max)
 
704
                hdmi->chmap.channels_max = chans;
654
705
 
655
706
        err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
656
707
                        &cvt->params.rates,
1008
1059
        return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1009
1060
}
1010
1061
 
 
1062
static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
 
1063
                        struct hdac_hdmi_pin *pin)
 
1064
{
 
1065
        pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
 
1066
}
 
1067
 
1011
1068
static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1012
1069
{
1013
1070
        struct hdac_ext_device *edev = pin->edev;
1065
1122
 
1066
1123
                                snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1067
1124
                        }
 
1125
                        hdac_hdmi_parse_eld(edev, pin);
1068
1126
 
1069
1127
                        print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1070
1128
                                        pin->eld.eld_buffer, pin->eld.eld_size);
1123
1181
        hdmi->num_pin++;
1124
1182
 
1125
1183
        pin->edev = edev;
 
1184
        mutex_init(&pin->lock);
1126
1185
        INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1127
1186
 
1128
1187
        return 0;
1342
1401
        .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1343
1402
};
1344
1403
 
 
1404
static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
 
1405
                                                int device)
 
1406
{
 
1407
        struct snd_soc_pcm_runtime *rtd;
 
1408
 
 
1409
        list_for_each_entry(rtd, &card->rtd_list, list) {
 
1410
                if (rtd->pcm && (rtd->pcm->device == device))
 
1411
                        return rtd->pcm;
 
1412
        }
 
1413
 
 
1414
        return NULL;
 
1415
}
 
1416
 
1345
1417
int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1346
1418
{
1347
1419
        char jack_name[NAME_SIZE];
1351
1423
                snd_soc_component_get_dapm(&codec->component);
1352
1424
        struct hdac_hdmi_priv *hdmi = edev->private_data;
1353
1425
        struct hdac_hdmi_pcm *pcm;
 
1426
        struct snd_pcm *snd_pcm;
 
1427
        int err;
1354
1428
 
1355
1429
        /*
1356
1430
         * this is a new PCM device, create new pcm and
1362
1436
        pcm->pcm_id = device;
1363
1437
        pcm->cvt = hdmi->dai_map[dai->id].cvt;
1364
1438
 
 
1439
        snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
 
1440
        if (snd_pcm) {
 
1441
                err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
 
1442
                if (err < 0) {
 
1443
                        dev_err(&edev->hdac.dev,
 
1444
                                "chmap control add failed with err: %d for pcm: %d\n",
 
1445
                                err, device);
 
1446
                        kfree(pcm);
 
1447
                        return err;
 
1448
                }
 
1449
        }
 
1450
 
1365
1451
        list_add_tail(&pcm->head, &hdmi->pcm_list);
1366
1452
 
1367
1453
        sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1378
1464
        struct snd_soc_dapm_context *dapm =
1379
1465
                snd_soc_component_get_dapm(&codec->component);
1380
1466
        struct hdac_hdmi_pin *pin;
 
1467
        struct hdac_ext_link *hlink = NULL;
1381
1468
        int ret;
1382
1469
 
1383
1470
        edev->scodec = codec;
1384
1471
 
 
1472
        /*
 
1473
         * hold the ref while we probe, also no need to drop the ref on
 
1474
         * exit, we call pm_runtime_suspend() so that will do for us
 
1475
         */
 
1476
        hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
 
1477
        snd_hdac_ext_bus_link_get(edev->ebus, hlink);
 
1478
 
1385
1479
        ret = create_fill_widget_route_map(dapm);
1386
1480
        if (ret < 0)
1387
1481
                return ret;
1475
1569
        .idle_bias_off  = true,
1476
1570
};
1477
1571
 
 
1572
static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
 
1573
                                        unsigned char *chmap)
 
1574
{
 
1575
        struct hdac_ext_device *edev = to_ehdac_device(hdac);
 
1576
        struct hdac_hdmi_priv *hdmi = edev->private_data;
 
1577
        struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
 
1578
        struct hdac_hdmi_pin *pin = pcm->pin;
 
1579
 
 
1580
        /* chmap is already set to 0 in caller */
 
1581
        if (!pin)
 
1582
                return;
 
1583
 
 
1584
        memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
 
1585
}
 
1586
 
 
1587
static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
 
1588
                                unsigned char *chmap, int prepared)
 
1589
{
 
1590
        struct hdac_ext_device *edev = to_ehdac_device(hdac);
 
1591
        struct hdac_hdmi_priv *hdmi = edev->private_data;
 
1592
        struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
 
1593
        struct hdac_hdmi_pin *pin = pcm->pin;
 
1594
 
 
1595
        mutex_lock(&pin->lock);
 
1596
        pin->chmap_set = true;
 
1597
        memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
 
1598
        if (prepared)
 
1599
                hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
 
1600
        mutex_unlock(&pin->lock);
 
1601
}
 
1602
 
 
1603
static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
 
1604
{
 
1605
        struct hdac_ext_device *edev = to_ehdac_device(hdac);
 
1606
        struct hdac_hdmi_priv *hdmi = edev->private_data;
 
1607
        struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
 
1608
        struct hdac_hdmi_pin *pin = pcm->pin;
 
1609
 
 
1610
        return pin ? true:false;
 
1611
}
 
1612
 
 
1613
static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
 
1614
{
 
1615
        struct hdac_ext_device *edev = to_ehdac_device(hdac);
 
1616
        struct hdac_hdmi_priv *hdmi = edev->private_data;
 
1617
        struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
 
1618
        struct hdac_hdmi_pin *pin = pcm->pin;
 
1619
 
 
1620
        if (!pin || !pin->eld.eld_valid)
 
1621
                return 0;
 
1622
 
 
1623
        return pin->eld.info.spk_alloc;
 
1624
}
 
1625
 
1478
1626
static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1479
1627
{
1480
1628
        struct hdac_device *codec = &edev->hdac;
1481
1629
        struct hdac_hdmi_priv *hdmi_priv;
1482
1630
        struct snd_soc_dai_driver *hdmi_dais = NULL;
 
1631
        struct hdac_ext_link *hlink = NULL;
1483
1632
        int num_dais = 0;
1484
1633
        int ret = 0;
1485
1634
 
 
1635
        /* hold the ref while we probe */
 
1636
        hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
 
1637
        snd_hdac_ext_bus_link_get(edev->ebus, hlink);
 
1638
 
1486
1639
        hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1487
1640
        if (hdmi_priv == NULL)
1488
1641
                return -ENOMEM;
1489
1642
 
1490
1643
        edev->private_data = hdmi_priv;
 
1644
        snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
 
1645
        hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
 
1646
        hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
 
1647
        hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
 
1648
        hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
1491
1649
 
1492
1650
        dev_set_drvdata(&codec->dev, edev);
1493
1651
 
1516
1674
        }
1517
1675
 
1518
1676
        /* ASoC specific initialization */
1519
 
        return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1520
 
                        hdmi_dais, num_dais);
 
1677
        ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
 
1678
                                        hdmi_dais, num_dais);
 
1679
 
 
1680
        snd_hdac_ext_bus_link_put(edev->ebus, hlink);
 
1681
 
 
1682
        return ret;
1521
1683
}
1522
1684
 
1523
1685
static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1556
1718
        struct hdac_ext_device *edev = to_hda_ext_device(dev);
1557
1719
        struct hdac_device *hdac = &edev->hdac;
1558
1720
        struct hdac_bus *bus = hdac->bus;
 
1721
        unsigned long timeout;
 
1722
        struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
 
1723
        struct hdac_ext_link *hlink = NULL;
1559
1724
        int err;
1560
1725
 
1561
1726
        dev_dbg(dev, "Enter: %s\n", __func__);
1579
1744
                return err;
1580
1745
        }
1581
1746
 
 
1747
        hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
 
1748
        snd_hdac_ext_bus_link_put(ebus, hlink);
 
1749
 
1582
1750
        return 0;
1583
1751
}
1584
1752
 
1587
1755
        struct hdac_ext_device *edev = to_hda_ext_device(dev);
1588
1756
        struct hdac_device *hdac = &edev->hdac;
1589
1757
        struct hdac_bus *bus = hdac->bus;
 
1758
        struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
 
1759
        struct hdac_ext_link *hlink = NULL;
1590
1760
        int err;
1591
1761
 
1592
1762
        dev_dbg(dev, "Enter: %s\n", __func__);
1595
1765
        if (!bus)
1596
1766
                return 0;
1597
1767
 
 
1768
        hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
 
1769
        snd_hdac_ext_bus_link_get(ebus, hlink);
 
1770
 
1598
1771
        err = snd_hdac_display_power(bus, true);
1599
1772
        if (err < 0) {
1600
1773
                dev_err(bus->dev, "Cannot turn on display power on i915\n");