linux/sound/pci/hda/hda_eld.c
<<
>>
Prefs
   1/*
   2 * Generic routines and proc interface for ELD(EDID Like Data) information
   3 *
   4 * Copyright(c) 2008 Intel Corporation.
   5 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
   6 *
   7 * Authors:
   8 *              Wu Fengguang <wfg@linux.intel.com>
   9 *
  10 *  This driver is free software; you can redistribute it and/or modify
  11 *  it under the terms of the GNU General Public License as published by
  12 *  the Free Software Foundation; either version 2 of the License, or
  13 *  (at your option) any later version.
  14 *
  15 *  This driver is distributed in the hope that it will be useful,
  16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 *  GNU General Public License for more details.
  19 *
  20 *  You should have received a copy of the GNU General Public License
  21 *  along with this program; if not, write to the Free Software
  22 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  23 */
  24
  25#include <linux/init.h>
  26#include <linux/slab.h>
  27#include <sound/core.h>
  28#include <asm/unaligned.h>
  29#include <sound/hda_chmap.h>
  30#include "hda_codec.h"
  31#include "hda_local.h"
  32
  33enum eld_versions {
  34        ELD_VER_CEA_861D        = 2,
  35        ELD_VER_PARTIAL         = 31,
  36};
  37
  38enum cea_edid_versions {
  39        CEA_EDID_VER_NONE       = 0,
  40        CEA_EDID_VER_CEA861     = 1,
  41        CEA_EDID_VER_CEA861A    = 2,
  42        CEA_EDID_VER_CEA861BCD  = 3,
  43        CEA_EDID_VER_RESERVED   = 4,
  44};
  45
  46static const char * const eld_connection_type_names[4] = {
  47        "HDMI",
  48        "DisplayPort",
  49        "2-reserved",
  50        "3-reserved"
  51};
  52
  53enum cea_audio_coding_types {
  54        AUDIO_CODING_TYPE_REF_STREAM_HEADER     =  0,
  55        AUDIO_CODING_TYPE_LPCM                  =  1,
  56        AUDIO_CODING_TYPE_AC3                   =  2,
  57        AUDIO_CODING_TYPE_MPEG1                 =  3,
  58        AUDIO_CODING_TYPE_MP3                   =  4,
  59        AUDIO_CODING_TYPE_MPEG2                 =  5,
  60        AUDIO_CODING_TYPE_AACLC                 =  6,
  61        AUDIO_CODING_TYPE_DTS                   =  7,
  62        AUDIO_CODING_TYPE_ATRAC                 =  8,
  63        AUDIO_CODING_TYPE_SACD                  =  9,
  64        AUDIO_CODING_TYPE_EAC3                  = 10,
  65        AUDIO_CODING_TYPE_DTS_HD                = 11,
  66        AUDIO_CODING_TYPE_MLP                   = 12,
  67        AUDIO_CODING_TYPE_DST                   = 13,
  68        AUDIO_CODING_TYPE_WMAPRO                = 14,
  69        AUDIO_CODING_TYPE_REF_CXT               = 15,
  70        /* also include valid xtypes below */
  71        AUDIO_CODING_TYPE_HE_AAC                = 15,
  72        AUDIO_CODING_TYPE_HE_AAC2               = 16,
  73        AUDIO_CODING_TYPE_MPEG_SURROUND         = 17,
  74};
  75
  76enum cea_audio_coding_xtypes {
  77        AUDIO_CODING_XTYPE_HE_REF_CT            = 0,
  78        AUDIO_CODING_XTYPE_HE_AAC               = 1,
  79        AUDIO_CODING_XTYPE_HE_AAC2              = 2,
  80        AUDIO_CODING_XTYPE_MPEG_SURROUND        = 3,
  81        AUDIO_CODING_XTYPE_FIRST_RESERVED       = 4,
  82};
  83
  84static const char * const cea_audio_coding_type_names[] = {
  85        /*  0 */ "undefined",
  86        /*  1 */ "LPCM",
  87        /*  2 */ "AC-3",
  88        /*  3 */ "MPEG1",
  89        /*  4 */ "MP3",
  90        /*  5 */ "MPEG2",
  91        /*  6 */ "AAC-LC",
  92        /*  7 */ "DTS",
  93        /*  8 */ "ATRAC",
  94        /*  9 */ "DSD (One Bit Audio)",
  95        /* 10 */ "E-AC-3/DD+ (Dolby Digital Plus)",
  96        /* 11 */ "DTS-HD",
  97        /* 12 */ "MLP (Dolby TrueHD)",
  98        /* 13 */ "DST",
  99        /* 14 */ "WMAPro",
 100        /* 15 */ "HE-AAC",
 101        /* 16 */ "HE-AACv2",
 102        /* 17 */ "MPEG Surround",
 103};
 104
 105/*
 106 * The following two lists are shared between
 107 *      - HDMI audio InfoFrame (source to sink)
 108 *      - CEA E-EDID Extension (sink to source)
 109 */
 110
 111/*
 112 * SS1:SS0 index => sample size
 113 */
 114static int cea_sample_sizes[4] = {
 115        0,                      /* 0: Refer to Stream Header */
 116        AC_SUPPCM_BITS_16,      /* 1: 16 bits */
 117        AC_SUPPCM_BITS_20,      /* 2: 20 bits */
 118        AC_SUPPCM_BITS_24,      /* 3: 24 bits */
 119};
 120
 121/*
 122 * SF2:SF1:SF0 index => sampling frequency
 123 */
 124static int cea_sampling_frequencies[8] = {
 125        0,                      /* 0: Refer to Stream Header */
 126        SNDRV_PCM_RATE_32000,   /* 1:  32000Hz */
 127        SNDRV_PCM_RATE_44100,   /* 2:  44100Hz */
 128        SNDRV_PCM_RATE_48000,   /* 3:  48000Hz */
 129        SNDRV_PCM_RATE_88200,   /* 4:  88200Hz */
 130        SNDRV_PCM_RATE_96000,   /* 5:  96000Hz */
 131        SNDRV_PCM_RATE_176400,  /* 6: 176400Hz */
 132        SNDRV_PCM_RATE_192000,  /* 7: 192000Hz */
 133};
 134
 135static unsigned int hdmi_get_eld_data(struct hda_codec *codec, hda_nid_t nid,
 136                                        int byte_index)
 137{
 138        unsigned int val;
 139
 140        val = snd_hda_codec_read(codec, nid, 0,
 141                                        AC_VERB_GET_HDMI_ELDD, byte_index);
 142#ifdef BE_PARANOID
 143        codec_info(codec, "HDMI: ELD data byte %d: 0x%x\n", byte_index, val);
 144#endif
 145        return val;
 146}
 147
 148#define GRAB_BITS(buf, byte, lowbit, bits)              \
 149({                                                      \
 150        BUILD_BUG_ON(lowbit > 7);                       \
 151        BUILD_BUG_ON(bits > 8);                         \
 152        BUILD_BUG_ON(bits <= 0);                        \
 153                                                        \
 154        (buf[byte] >> (lowbit)) & ((1 << (bits)) - 1);  \
 155})
 156
 157static void hdmi_update_short_audio_desc(struct hda_codec *codec,
 158                                         struct cea_sad *a,
 159                                         const unsigned char *buf)
 160{
 161        int i;
 162        int val;
 163
 164        val = GRAB_BITS(buf, 1, 0, 7);
 165        a->rates = 0;
 166        for (i = 0; i < 7; i++)
 167                if (val & (1 << i))
 168                        a->rates |= cea_sampling_frequencies[i + 1];
 169
 170        a->channels = GRAB_BITS(buf, 0, 0, 3);
 171        a->channels++;
 172
 173        a->sample_bits = 0;
 174        a->max_bitrate = 0;
 175
 176        a->format = GRAB_BITS(buf, 0, 3, 4);
 177        switch (a->format) {
 178        case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
 179                codec_info(codec, "HDMI: audio coding type 0 not expected\n");
 180                break;
 181
 182        case AUDIO_CODING_TYPE_LPCM:
 183                val = GRAB_BITS(buf, 2, 0, 3);
 184                for (i = 0; i < 3; i++)
 185                        if (val & (1 << i))
 186                                a->sample_bits |= cea_sample_sizes[i + 1];
 187                break;
 188
 189        case AUDIO_CODING_TYPE_AC3:
 190        case AUDIO_CODING_TYPE_MPEG1:
 191        case AUDIO_CODING_TYPE_MP3:
 192        case AUDIO_CODING_TYPE_MPEG2:
 193        case AUDIO_CODING_TYPE_AACLC:
 194        case AUDIO_CODING_TYPE_DTS:
 195        case AUDIO_CODING_TYPE_ATRAC:
 196                a->max_bitrate = GRAB_BITS(buf, 2, 0, 8);
 197                a->max_bitrate *= 8000;
 198                break;
 199
 200        case AUDIO_CODING_TYPE_SACD:
 201                break;
 202
 203        case AUDIO_CODING_TYPE_EAC3:
 204                break;
 205
 206        case AUDIO_CODING_TYPE_DTS_HD:
 207                break;
 208
 209        case AUDIO_CODING_TYPE_MLP:
 210                break;
 211
 212        case AUDIO_CODING_TYPE_DST:
 213                break;
 214
 215        case AUDIO_CODING_TYPE_WMAPRO:
 216                a->profile = GRAB_BITS(buf, 2, 0, 3);
 217                break;
 218
 219        case AUDIO_CODING_TYPE_REF_CXT:
 220                a->format = GRAB_BITS(buf, 2, 3, 5);
 221                if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
 222                    a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
 223                        codec_info(codec,
 224                                   "HDMI: audio coding xtype %d not expected\n",
 225                                   a->format);
 226                        a->format = 0;
 227                } else
 228                        a->format += AUDIO_CODING_TYPE_HE_AAC -
 229                                     AUDIO_CODING_XTYPE_HE_AAC;
 230                break;
 231        }
 232}
 233
 234/*
 235 * Be careful, ELD buf could be totally rubbish!
 236 */
 237int snd_hdmi_parse_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e,
 238                          const unsigned char *buf, int size)
 239{
 240        int mnl;
 241        int i;
 242
 243        memset(e, 0, sizeof(*e));
 244        e->eld_ver = GRAB_BITS(buf, 0, 3, 5);
 245        if (e->eld_ver != ELD_VER_CEA_861D &&
 246            e->eld_ver != ELD_VER_PARTIAL) {
 247                codec_info(codec, "HDMI: Unknown ELD version %d\n", e->eld_ver);
 248                goto out_fail;
 249        }
 250
 251        e->baseline_len = GRAB_BITS(buf, 2, 0, 8);
 252        mnl             = GRAB_BITS(buf, 4, 0, 5);
 253        e->cea_edid_ver = GRAB_BITS(buf, 4, 5, 3);
 254
 255        e->support_hdcp = GRAB_BITS(buf, 5, 0, 1);
 256        e->support_ai   = GRAB_BITS(buf, 5, 1, 1);
 257        e->conn_type    = GRAB_BITS(buf, 5, 2, 2);
 258        e->sad_count    = GRAB_BITS(buf, 5, 4, 4);
 259
 260        e->aud_synch_delay = GRAB_BITS(buf, 6, 0, 8) * 2;
 261        e->spk_alloc    = GRAB_BITS(buf, 7, 0, 7);
 262
 263        e->port_id        = get_unaligned_le64(buf + 8);
 264
 265        /* not specified, but the spec's tendency is little endian */
 266        e->manufacture_id = get_unaligned_le16(buf + 16);
 267        e->product_id     = get_unaligned_le16(buf + 18);
 268
 269        if (mnl > ELD_MAX_MNL) {
 270                codec_info(codec, "HDMI: MNL is reserved value %d\n", mnl);
 271                goto out_fail;
 272        } else if (ELD_FIXED_BYTES + mnl > size) {
 273                codec_info(codec, "HDMI: out of range MNL %d\n", mnl);
 274                goto out_fail;
 275        } else
 276                strlcpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl + 1);
 277
 278        for (i = 0; i < e->sad_count; i++) {
 279                if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
 280                        codec_info(codec, "HDMI: out of range SAD %d\n", i);
 281                        goto out_fail;
 282                }
 283                hdmi_update_short_audio_desc(codec, e->sad + i,
 284                                        buf + ELD_FIXED_BYTES + mnl + 3 * i);
 285        }
 286
 287        /*
 288         * HDMI sink's ELD info cannot always be retrieved for now, e.g.
 289         * in console or for audio devices. Assume the highest speakers
 290         * configuration, to _not_ prohibit multi-channel audio playback.
 291         */
 292        if (!e->spk_alloc)
 293                e->spk_alloc = 0xffff;
 294
 295        return 0;
 296
 297out_fail:
 298        return -EINVAL;
 299}
 300
 301int snd_hdmi_get_eld_size(struct hda_codec *codec, hda_nid_t nid)
 302{
 303        return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
 304                                                 AC_DIPSIZE_ELD_BUF);
 305}
 306
 307int snd_hdmi_get_eld(struct hda_codec *codec, hda_nid_t nid,
 308                     unsigned char *buf, int *eld_size)
 309{
 310        int i;
 311        int ret = 0;
 312        int size;
 313
 314        /*
 315         * ELD size is initialized to zero in caller function. If no errors and
 316         * ELD is valid, actual eld_size is assigned.
 317         */
 318
 319        size = snd_hdmi_get_eld_size(codec, nid);
 320        if (size == 0) {
 321                /* wfg: workaround for ASUS P5E-VM HDMI board */
 322                codec_info(codec, "HDMI: ELD buf size is 0, force 128\n");
 323                size = 128;
 324        }
 325        if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
 326                codec_info(codec, "HDMI: invalid ELD buf size %d\n", size);
 327                return -ERANGE;
 328        }
 329
 330        /* set ELD buffer */
 331        for (i = 0; i < size; i++) {
 332                unsigned int val = hdmi_get_eld_data(codec, nid, i);
 333                /*
 334                 * Graphics driver might be writing to ELD buffer right now.
 335                 * Just abort. The caller will repoll after a while.
 336                 */
 337                if (!(val & AC_ELDD_ELD_VALID)) {
 338                        codec_info(codec, "HDMI: invalid ELD data byte %d\n", i);
 339                        ret = -EINVAL;
 340                        goto error;
 341                }
 342                val &= AC_ELDD_ELD_DATA;
 343                /*
 344                 * The first byte cannot be zero. This can happen on some DVI
 345                 * connections. Some Intel chips may also need some 250ms delay
 346                 * to return non-zero ELD data, even when the graphics driver
 347                 * correctly writes ELD content before setting ELD_valid bit.
 348                 */
 349                if (!val && !i) {
 350                        codec_dbg(codec, "HDMI: 0 ELD data\n");
 351                        ret = -EINVAL;
 352                        goto error;
 353                }
 354                buf[i] = val;
 355        }
 356
 357        *eld_size = size;
 358error:
 359        return ret;
 360}
 361
 362/*
 363 * SNDRV_PCM_RATE_* and AC_PAR_PCM values don't match, print correct rates with
 364 * hdmi-specific routine.
 365 */
 366static void hdmi_print_pcm_rates(int pcm, char *buf, int buflen)
 367{
 368        static unsigned int alsa_rates[] = {
 369                5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
 370                88200, 96000, 176400, 192000, 384000
 371        };
 372        int i, j;
 373
 374        for (i = 0, j = 0; i < ARRAY_SIZE(alsa_rates); i++)
 375                if (pcm & (1 << i))
 376                        j += snprintf(buf + j, buflen - j,  " %d",
 377                                alsa_rates[i]);
 378
 379        buf[j] = '\0'; /* necessary when j == 0 */
 380}
 381
 382#define SND_PRINT_RATES_ADVISED_BUFSIZE 80
 383
 384static void hdmi_show_short_audio_desc(struct hda_codec *codec,
 385                                       struct cea_sad *a)
 386{
 387        char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
 388        char buf2[8 + SND_PRINT_BITS_ADVISED_BUFSIZE] = ", bits =";
 389
 390        if (!a->format)
 391                return;
 392
 393        hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
 394
 395        if (a->format == AUDIO_CODING_TYPE_LPCM)
 396                snd_print_pcm_bits(a->sample_bits, buf2 + 8, sizeof(buf2) - 8);
 397        else if (a->max_bitrate)
 398                snprintf(buf2, sizeof(buf2),
 399                                ", max bitrate = %d", a->max_bitrate);
 400        else
 401                buf2[0] = '\0';
 402
 403        codec_dbg(codec,
 404                  "HDMI: supports coding type %s: channels = %d, rates =%s%s\n",
 405                  cea_audio_coding_type_names[a->format],
 406                  a->channels, buf, buf2);
 407}
 408
 409void snd_hdmi_show_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e)
 410{
 411        int i;
 412
 413        codec_dbg(codec, "HDMI: detected monitor %s at connection type %s\n",
 414                        e->monitor_name,
 415                        eld_connection_type_names[e->conn_type]);
 416
 417        if (e->spk_alloc) {
 418                char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
 419                snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
 420                codec_dbg(codec, "HDMI: available speakers:%s\n", buf);
 421        }
 422
 423        for (i = 0; i < e->sad_count; i++)
 424                hdmi_show_short_audio_desc(codec, e->sad + i);
 425}
 426
 427#ifdef CONFIG_SND_PROC_FS
 428
 429static void hdmi_print_sad_info(int i, struct cea_sad *a,
 430                                struct snd_info_buffer *buffer)
 431{
 432        char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
 433
 434        snd_iprintf(buffer, "sad%d_coding_type\t[0x%x] %s\n",
 435                        i, a->format, cea_audio_coding_type_names[a->format]);
 436        snd_iprintf(buffer, "sad%d_channels\t\t%d\n", i, a->channels);
 437
 438        hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
 439        snd_iprintf(buffer, "sad%d_rates\t\t[0x%x]%s\n", i, a->rates, buf);
 440
 441        if (a->format == AUDIO_CODING_TYPE_LPCM) {
 442                snd_print_pcm_bits(a->sample_bits, buf, sizeof(buf));
 443                snd_iprintf(buffer, "sad%d_bits\t\t[0x%x]%s\n",
 444                                                        i, a->sample_bits, buf);
 445        }
 446
 447        if (a->max_bitrate)
 448                snd_iprintf(buffer, "sad%d_max_bitrate\t%d\n",
 449                                                        i, a->max_bitrate);
 450
 451        if (a->profile)
 452                snd_iprintf(buffer, "sad%d_profile\t\t%d\n", i, a->profile);
 453}
 454
 455void snd_hdmi_print_eld_info(struct hdmi_eld *eld,
 456                             struct snd_info_buffer *buffer)
 457{
 458        struct parsed_hdmi_eld *e = &eld->info;
 459        char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
 460        int i;
 461        static const char * const eld_version_names[32] = {
 462                "reserved",
 463                "reserved",
 464                "CEA-861D or below",
 465                [3 ... 30] = "reserved",
 466                [31] = "partial"
 467        };
 468        static const char * const cea_edid_version_names[8] = {
 469                "no CEA EDID Timing Extension block present",
 470                "CEA-861",
 471                "CEA-861-A",
 472                "CEA-861-B, C or D",
 473                [4 ... 7] = "reserved"
 474        };
 475
 476        snd_iprintf(buffer, "monitor_present\t\t%d\n", eld->monitor_present);
 477        snd_iprintf(buffer, "eld_valid\t\t%d\n", eld->eld_valid);
 478        if (!eld->eld_valid)
 479                return;
 480        snd_iprintf(buffer, "monitor_name\t\t%s\n", e->monitor_name);
 481        snd_iprintf(buffer, "connection_type\t\t%s\n",
 482                                eld_connection_type_names[e->conn_type]);
 483        snd_iprintf(buffer, "eld_version\t\t[0x%x] %s\n", e->eld_ver,
 484                                        eld_version_names[e->eld_ver]);
 485        snd_iprintf(buffer, "edid_version\t\t[0x%x] %s\n", e->cea_edid_ver,
 486                                cea_edid_version_names[e->cea_edid_ver]);
 487        snd_iprintf(buffer, "manufacture_id\t\t0x%x\n", e->manufacture_id);
 488        snd_iprintf(buffer, "product_id\t\t0x%x\n", e->product_id);
 489        snd_iprintf(buffer, "port_id\t\t\t0x%llx\n", (long long)e->port_id);
 490        snd_iprintf(buffer, "support_hdcp\t\t%d\n", e->support_hdcp);
 491        snd_iprintf(buffer, "support_ai\t\t%d\n", e->support_ai);
 492        snd_iprintf(buffer, "audio_sync_delay\t%d\n", e->aud_synch_delay);
 493
 494        snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
 495        snd_iprintf(buffer, "speakers\t\t[0x%x]%s\n", e->spk_alloc, buf);
 496
 497        snd_iprintf(buffer, "sad_count\t\t%d\n", e->sad_count);
 498
 499        for (i = 0; i < e->sad_count; i++)
 500                hdmi_print_sad_info(i, e->sad + i, buffer);
 501}
 502
 503void snd_hdmi_write_eld_info(struct hdmi_eld *eld,
 504                             struct snd_info_buffer *buffer)
 505{
 506        struct parsed_hdmi_eld *e = &eld->info;
 507        char line[64];
 508        char name[64];
 509        char *sname;
 510        long long val;
 511        unsigned int n;
 512
 513        while (!snd_info_get_line(buffer, line, sizeof(line))) {
 514                if (sscanf(line, "%s %llx", name, &val) != 2)
 515                        continue;
 516                /*
 517                 * We don't allow modification to these fields:
 518                 *      monitor_name manufacture_id product_id
 519                 *      eld_version edid_version
 520                 */
 521                if (!strcmp(name, "monitor_present"))
 522                        eld->monitor_present = val;
 523                else if (!strcmp(name, "eld_valid"))
 524                        eld->eld_valid = val;
 525                else if (!strcmp(name, "connection_type"))
 526                        e->conn_type = val;
 527                else if (!strcmp(name, "port_id"))
 528                        e->port_id = val;
 529                else if (!strcmp(name, "support_hdcp"))
 530                        e->support_hdcp = val;
 531                else if (!strcmp(name, "support_ai"))
 532                        e->support_ai = val;
 533                else if (!strcmp(name, "audio_sync_delay"))
 534                        e->aud_synch_delay = val;
 535                else if (!strcmp(name, "speakers"))
 536                        e->spk_alloc = val;
 537                else if (!strcmp(name, "sad_count"))
 538                        e->sad_count = val;
 539                else if (!strncmp(name, "sad", 3)) {
 540                        sname = name + 4;
 541                        n = name[3] - '0';
 542                        if (name[4] >= '0' && name[4] <= '9') {
 543                                sname++;
 544                                n = 10 * n + name[4] - '0';
 545                        }
 546                        if (n >= ELD_MAX_SAD)
 547                                continue;
 548                        if (!strcmp(sname, "_coding_type"))
 549                                e->sad[n].format = val;
 550                        else if (!strcmp(sname, "_channels"))
 551                                e->sad[n].channels = val;
 552                        else if (!strcmp(sname, "_rates"))
 553                                e->sad[n].rates = val;
 554                        else if (!strcmp(sname, "_bits"))
 555                                e->sad[n].sample_bits = val;
 556                        else if (!strcmp(sname, "_max_bitrate"))
 557                                e->sad[n].max_bitrate = val;
 558                        else if (!strcmp(sname, "_profile"))
 559                                e->sad[n].profile = val;
 560                        if (n >= e->sad_count)
 561                                e->sad_count = n + 1;
 562                }
 563        }
 564}
 565#endif /* CONFIG_SND_PROC_FS */
 566
 567/* update PCM info based on ELD */
 568void snd_hdmi_eld_update_pcm_info(struct parsed_hdmi_eld *e,
 569                              struct hda_pcm_stream *hinfo)
 570{
 571        u32 rates;
 572        u64 formats;
 573        unsigned int maxbps;
 574        unsigned int channels_max;
 575        int i;
 576
 577        /* assume basic audio support (the basic audio flag is not in ELD;
 578         * however, all audio capable sinks are required to support basic
 579         * audio) */
 580        rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
 581                SNDRV_PCM_RATE_48000;
 582        formats = SNDRV_PCM_FMTBIT_S16_LE;
 583        maxbps = 16;
 584        channels_max = 2;
 585        for (i = 0; i < e->sad_count; i++) {
 586                struct cea_sad *a = &e->sad[i];
 587                rates |= a->rates;
 588                if (a->channels > channels_max)
 589                        channels_max = a->channels;
 590                if (a->format == AUDIO_CODING_TYPE_LPCM) {
 591                        if (a->sample_bits & AC_SUPPCM_BITS_20) {
 592                                formats |= SNDRV_PCM_FMTBIT_S32_LE;
 593                                if (maxbps < 20)
 594                                        maxbps = 20;
 595                        }
 596                        if (a->sample_bits & AC_SUPPCM_BITS_24) {
 597                                formats |= SNDRV_PCM_FMTBIT_S32_LE;
 598                                if (maxbps < 24)
 599                                        maxbps = 24;
 600                        }
 601                }
 602        }
 603
 604        /* restrict the parameters by the values the codec provides */
 605        hinfo->rates &= rates;
 606        hinfo->formats &= formats;
 607        hinfo->maxbps = min(hinfo->maxbps, maxbps);
 608        hinfo->channels_max = min(hinfo->channels_max, channels_max);
 609}
 610
 611
 612/* ATI/AMD specific stuff (ELD emulation) */
 613
 614#define ATI_VERB_SET_AUDIO_DESCRIPTOR   0x776
 615#define ATI_VERB_SET_SINK_INFO_INDEX    0x780
 616#define ATI_VERB_GET_SPEAKER_ALLOCATION 0xf70
 617#define ATI_VERB_GET_AUDIO_DESCRIPTOR   0xf76
 618#define ATI_VERB_GET_AUDIO_VIDEO_DELAY  0xf7b
 619#define ATI_VERB_GET_SINK_INFO_INDEX    0xf80
 620#define ATI_VERB_GET_SINK_INFO_DATA     0xf81
 621
 622#define ATI_SPKALLOC_SPKALLOC           0x007f
 623#define ATI_SPKALLOC_TYPE_HDMI          0x0100
 624#define ATI_SPKALLOC_TYPE_DISPLAYPORT   0x0200
 625
 626/* first three bytes are just standard SAD */
 627#define ATI_AUDIODESC_CHANNELS          0x00000007
 628#define ATI_AUDIODESC_RATES             0x0000ff00
 629#define ATI_AUDIODESC_LPCM_STEREO_RATES 0xff000000
 630
 631/* in standard HDMI VSDB format */
 632#define ATI_DELAY_VIDEO_LATENCY         0x000000ff
 633#define ATI_DELAY_AUDIO_LATENCY         0x0000ff00
 634
 635enum ati_sink_info_idx {
 636        ATI_INFO_IDX_MANUFACTURER_ID    = 0,
 637        ATI_INFO_IDX_PRODUCT_ID         = 1,
 638        ATI_INFO_IDX_SINK_DESC_LEN      = 2,
 639        ATI_INFO_IDX_PORT_ID_LOW        = 3,
 640        ATI_INFO_IDX_PORT_ID_HIGH       = 4,
 641        ATI_INFO_IDX_SINK_DESC_FIRST    = 5,
 642        ATI_INFO_IDX_SINK_DESC_LAST     = 22, /* max len 18 bytes */
 643};
 644
 645int snd_hdmi_get_eld_ati(struct hda_codec *codec, hda_nid_t nid,
 646                         unsigned char *buf, int *eld_size, bool rev3_or_later)
 647{
 648        int spkalloc, ati_sad, aud_synch;
 649        int sink_desc_len = 0;
 650        int pos, i;
 651
 652        /* ATI/AMD does not have ELD, emulate it */
 653
 654        spkalloc = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SPEAKER_ALLOCATION, 0);
 655
 656        if (spkalloc <= 0) {
 657                codec_info(codec, "HDMI ATI/AMD: no speaker allocation for ELD\n");
 658                return -EINVAL;
 659        }
 660
 661        memset(buf, 0, ELD_FIXED_BYTES + ELD_MAX_MNL + ELD_MAX_SAD * 3);
 662
 663        /* version */
 664        buf[0] = ELD_VER_CEA_861D << 3;
 665
 666        /* speaker allocation from EDID */
 667        buf[7] = spkalloc & ATI_SPKALLOC_SPKALLOC;
 668
 669        /* is DisplayPort? */
 670        if (spkalloc & ATI_SPKALLOC_TYPE_DISPLAYPORT)
 671                buf[5] |= 0x04;
 672
 673        pos = ELD_FIXED_BYTES;
 674
 675        if (rev3_or_later) {
 676                int sink_info;
 677
 678                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_LOW);
 679                sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 680                put_unaligned_le32(sink_info, buf + 8);
 681
 682                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_HIGH);
 683                sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 684                put_unaligned_le32(sink_info, buf + 12);
 685
 686                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_MANUFACTURER_ID);
 687                sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 688                put_unaligned_le16(sink_info, buf + 16);
 689
 690                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PRODUCT_ID);
 691                sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 692                put_unaligned_le16(sink_info, buf + 18);
 693
 694                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_LEN);
 695                sink_desc_len = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 696
 697                if (sink_desc_len > ELD_MAX_MNL) {
 698                        codec_info(codec, "HDMI ATI/AMD: Truncating HDMI sink description with length %d\n",
 699                                   sink_desc_len);
 700                        sink_desc_len = ELD_MAX_MNL;
 701                }
 702
 703                buf[4] |= sink_desc_len;
 704
 705                for (i = 0; i < sink_desc_len; i++) {
 706                        snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_FIRST + i);
 707                        buf[pos++] = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
 708                }
 709        }
 710
 711        for (i = AUDIO_CODING_TYPE_LPCM; i <= AUDIO_CODING_TYPE_WMAPRO; i++) {
 712                if (i == AUDIO_CODING_TYPE_SACD || i == AUDIO_CODING_TYPE_DST)
 713                        continue; /* not handled by ATI/AMD */
 714
 715                snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_AUDIO_DESCRIPTOR, i << 3);
 716                ati_sad = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_DESCRIPTOR, 0);
 717
 718                if (ati_sad <= 0)
 719                        continue;
 720
 721                if (ati_sad & ATI_AUDIODESC_RATES) {
 722                        /* format is supported, copy SAD as-is */
 723                        buf[pos++] = (ati_sad & 0x0000ff) >> 0;
 724                        buf[pos++] = (ati_sad & 0x00ff00) >> 8;
 725                        buf[pos++] = (ati_sad & 0xff0000) >> 16;
 726                }
 727
 728                if (i == AUDIO_CODING_TYPE_LPCM
 729                    && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES)
 730                    && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES) >> 16 != (ati_sad & ATI_AUDIODESC_RATES)) {
 731                        /* for PCM there is a separate stereo rate mask */
 732                        buf[pos++] = ((ati_sad & 0x000000ff) & ~ATI_AUDIODESC_CHANNELS) | 0x1;
 733                        /* rates from the extra byte */
 734                        buf[pos++] = (ati_sad & 0xff000000) >> 24;
 735                        buf[pos++] = (ati_sad & 0x00ff0000) >> 16;
 736                }
 737        }
 738
 739        if (pos == ELD_FIXED_BYTES + sink_desc_len) {
 740                codec_info(codec, "HDMI ATI/AMD: no audio descriptors for ELD\n");
 741                return -EINVAL;
 742        }
 743
 744        /*
 745         * HDMI VSDB latency format:
 746         * separately for both audio and video:
 747         *  0          field not valid or unknown latency
 748         *  [1..251]   msecs = (x-1)*2  (max 500ms with x = 251 = 0xfb)
 749         *  255        audio/video not supported
 750         *
 751         * HDA latency format:
 752         * single value indicating video latency relative to audio:
 753         *  0          unknown or 0ms
 754         *  [1..250]   msecs = x*2  (max 500ms with x = 250 = 0xfa)
 755         *  [251..255] reserved
 756         */
 757        aud_synch = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_VIDEO_DELAY, 0);
 758        if ((aud_synch & ATI_DELAY_VIDEO_LATENCY) && (aud_synch & ATI_DELAY_AUDIO_LATENCY)) {
 759                int video_latency_hdmi = (aud_synch & ATI_DELAY_VIDEO_LATENCY);
 760                int audio_latency_hdmi = (aud_synch & ATI_DELAY_AUDIO_LATENCY) >> 8;
 761
 762                if (video_latency_hdmi <= 0xfb && audio_latency_hdmi <= 0xfb &&
 763                    video_latency_hdmi > audio_latency_hdmi)
 764                        buf[6] = video_latency_hdmi - audio_latency_hdmi;
 765                /* else unknown/invalid or 0ms or video ahead of audio, so use zero */
 766        }
 767
 768        /* SAD count */
 769        buf[5] |= ((pos - ELD_FIXED_BYTES - sink_desc_len) / 3) << 4;
 770
 771        /* Baseline ELD block length is 4-byte aligned */
 772        pos = round_up(pos, 4);
 773
 774        /* Baseline ELD length (4-byte header is not counted in) */
 775        buf[2] = (pos - 4) / 4;
 776
 777        *eld_size = pos;
 778
 779        return 0;
 780}
 781