linux/sound/hda/hdac_device.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * HD-audio codec core device
   4 */
   5
   6#include <linux/init.h>
   7#include <linux/delay.h>
   8#include <linux/device.h>
   9#include <linux/slab.h>
  10#include <linux/module.h>
  11#include <linux/export.h>
  12#include <linux/pm_runtime.h>
  13#include <sound/hdaudio.h>
  14#include <sound/hda_regmap.h>
  15#include <sound/pcm.h>
  16#include "local.h"
  17
  18static void setup_fg_nodes(struct hdac_device *codec);
  19static int get_codec_vendor_name(struct hdac_device *codec);
  20
  21static void default_release(struct device *dev)
  22{
  23        snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
  24}
  25
  26/**
  27 * snd_hdac_device_init - initialize the HD-audio codec base device
  28 * @codec: device to initialize
  29 * @bus: but to attach
  30 * @name: device name string
  31 * @addr: codec address
  32 *
  33 * Returns zero for success or a negative error code.
  34 *
  35 * This function increments the runtime PM counter and marks it active.
  36 * The caller needs to turn it off appropriately later.
  37 *
  38 * The caller needs to set the device's release op properly by itself.
  39 */
  40int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
  41                         const char *name, unsigned int addr)
  42{
  43        struct device *dev;
  44        hda_nid_t fg;
  45        int err;
  46
  47        dev = &codec->dev;
  48        device_initialize(dev);
  49        dev->parent = bus->dev;
  50        dev->bus = &snd_hda_bus_type;
  51        dev->release = default_release;
  52        dev->groups = hdac_dev_attr_groups;
  53        dev_set_name(dev, "%s", name);
  54        device_enable_async_suspend(dev);
  55
  56        codec->bus = bus;
  57        codec->addr = addr;
  58        codec->type = HDA_DEV_CORE;
  59        mutex_init(&codec->widget_lock);
  60        pm_runtime_set_active(&codec->dev);
  61        pm_runtime_get_noresume(&codec->dev);
  62        atomic_set(&codec->in_pm, 0);
  63
  64        err = snd_hdac_bus_add_device(bus, codec);
  65        if (err < 0)
  66                goto error;
  67
  68        /* fill parameters */
  69        codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  70                                              AC_PAR_VENDOR_ID);
  71        if (codec->vendor_id == -1) {
  72                /* read again, hopefully the access method was corrected
  73                 * in the last read...
  74                 */
  75                codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  76                                                      AC_PAR_VENDOR_ID);
  77        }
  78
  79        codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  80                                                 AC_PAR_SUBSYSTEM_ID);
  81        codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  82                                                AC_PAR_REV_ID);
  83
  84        setup_fg_nodes(codec);
  85        if (!codec->afg && !codec->mfg) {
  86                dev_err(dev, "no AFG or MFG node found\n");
  87                err = -ENODEV;
  88                goto error;
  89        }
  90
  91        fg = codec->afg ? codec->afg : codec->mfg;
  92
  93        err = snd_hdac_refresh_widgets(codec);
  94        if (err < 0)
  95                goto error;
  96
  97        codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
  98        /* reread ssid if not set by parameter */
  99        if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
 100                snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
 101                              &codec->subsystem_id);
 102
 103        err = get_codec_vendor_name(codec);
 104        if (err < 0)
 105                goto error;
 106
 107        codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
 108                                     codec->vendor_id & 0xffff);
 109        if (!codec->chip_name) {
 110                err = -ENOMEM;
 111                goto error;
 112        }
 113
 114        return 0;
 115
 116 error:
 117        put_device(&codec->dev);
 118        return err;
 119}
 120EXPORT_SYMBOL_GPL(snd_hdac_device_init);
 121
 122/**
 123 * snd_hdac_device_exit - clean up the HD-audio codec base device
 124 * @codec: device to clean up
 125 */
 126void snd_hdac_device_exit(struct hdac_device *codec)
 127{
 128        pm_runtime_put_noidle(&codec->dev);
 129        snd_hdac_bus_remove_device(codec->bus, codec);
 130        kfree(codec->vendor_name);
 131        kfree(codec->chip_name);
 132}
 133EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
 134
 135/**
 136 * snd_hdac_device_register - register the hd-audio codec base device
 137 * codec: the device to register
 138 */
 139int snd_hdac_device_register(struct hdac_device *codec)
 140{
 141        int err;
 142
 143        err = device_add(&codec->dev);
 144        if (err < 0)
 145                return err;
 146        mutex_lock(&codec->widget_lock);
 147        err = hda_widget_sysfs_init(codec);
 148        mutex_unlock(&codec->widget_lock);
 149        if (err < 0) {
 150                device_del(&codec->dev);
 151                return err;
 152        }
 153
 154        return 0;
 155}
 156EXPORT_SYMBOL_GPL(snd_hdac_device_register);
 157
 158/**
 159 * snd_hdac_device_unregister - unregister the hd-audio codec base device
 160 * codec: the device to unregister
 161 */
 162void snd_hdac_device_unregister(struct hdac_device *codec)
 163{
 164        if (device_is_registered(&codec->dev)) {
 165                mutex_lock(&codec->widget_lock);
 166                hda_widget_sysfs_exit(codec);
 167                mutex_unlock(&codec->widget_lock);
 168                device_del(&codec->dev);
 169                snd_hdac_bus_remove_device(codec->bus, codec);
 170        }
 171}
 172EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
 173
 174/**
 175 * snd_hdac_device_set_chip_name - set/update the codec name
 176 * @codec: the HDAC device
 177 * @name: name string to set
 178 *
 179 * Returns 0 if the name is set or updated, or a negative error code.
 180 */
 181int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
 182{
 183        char *newname;
 184
 185        if (!name)
 186                return 0;
 187        newname = kstrdup(name, GFP_KERNEL);
 188        if (!newname)
 189                return -ENOMEM;
 190        kfree(codec->chip_name);
 191        codec->chip_name = newname;
 192        return 0;
 193}
 194EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
 195
 196/**
 197 * snd_hdac_codec_modalias - give the module alias name
 198 * @codec: HDAC device
 199 * @buf: string buffer to store
 200 * @size: string buffer size
 201 *
 202 * Returns the size of string, like snprintf(), or a negative error code.
 203 */
 204int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
 205{
 206        return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
 207                        codec->vendor_id, codec->revision_id, codec->type);
 208}
 209EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
 210
 211/**
 212 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
 213 *      HD-audio controller
 214 * @codec: the codec object
 215 * @nid: NID to encode
 216 * @verb: verb to encode
 217 * @parm: parameter to encode
 218 *
 219 * Return an encoded command verb or -1 for error.
 220 */
 221unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
 222                               unsigned int verb, unsigned int parm)
 223{
 224        u32 val, addr;
 225
 226        addr = codec->addr;
 227        if ((addr & ~0xf) || (nid & ~0x7f) ||
 228            (verb & ~0xfff) || (parm & ~0xffff)) {
 229                dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
 230                        addr, nid, verb, parm);
 231                return -1;
 232        }
 233
 234        val = addr << 28;
 235        val |= (u32)nid << 20;
 236        val |= verb << 8;
 237        val |= parm;
 238        return val;
 239}
 240EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
 241
 242/**
 243 * snd_hdac_exec_verb - execute an encoded verb
 244 * @codec: the codec object
 245 * @cmd: encoded verb to execute
 246 * @flags: optional flags, pass zero for default
 247 * @res: the pointer to store the result, NULL if running async
 248 *
 249 * Returns zero if successful, or a negative error code.
 250 *
 251 * This calls the exec_verb op when set in hdac_codec.  If not,
 252 * call the default snd_hdac_bus_exec_verb().
 253 */
 254int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
 255                       unsigned int flags, unsigned int *res)
 256{
 257        if (codec->exec_verb)
 258                return codec->exec_verb(codec, cmd, flags, res);
 259        return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
 260}
 261EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
 262
 263
 264/**
 265 * snd_hdac_read - execute a verb
 266 * @codec: the codec object
 267 * @nid: NID to execute a verb
 268 * @verb: verb to execute
 269 * @parm: parameter for a verb
 270 * @res: the pointer to store the result, NULL if running async
 271 *
 272 * Returns zero if successful, or a negative error code.
 273 */
 274int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
 275                  unsigned int verb, unsigned int parm, unsigned int *res)
 276{
 277        unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
 278
 279        return snd_hdac_exec_verb(codec, cmd, 0, res);
 280}
 281EXPORT_SYMBOL_GPL(snd_hdac_read);
 282
 283/**
 284 * _snd_hdac_read_parm - read a parmeter
 285 *
 286 * This function returns zero or an error unlike snd_hdac_read_parm().
 287 */
 288int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
 289                        unsigned int *res)
 290{
 291        unsigned int cmd;
 292
 293        cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
 294        return snd_hdac_regmap_read_raw(codec, cmd, res);
 295}
 296EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
 297
 298/**
 299 * snd_hdac_read_parm_uncached - read a codec parameter without caching
 300 * @codec: the codec object
 301 * @nid: NID to read a parameter
 302 * @parm: parameter to read
 303 *
 304 * Returns -1 for error.  If you need to distinguish the error more
 305 * strictly, use snd_hdac_read() directly.
 306 */
 307int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
 308                                int parm)
 309{
 310        unsigned int cmd, val;
 311
 312        cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
 313        if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
 314                return -1;
 315        return val;
 316}
 317EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
 318
 319/**
 320 * snd_hdac_override_parm - override read-only parameters
 321 * @codec: the codec object
 322 * @nid: NID for the parameter
 323 * @parm: the parameter to change
 324 * @val: the parameter value to overwrite
 325 */
 326int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
 327                           unsigned int parm, unsigned int val)
 328{
 329        unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
 330        int err;
 331
 332        if (!codec->regmap)
 333                return -EINVAL;
 334
 335        codec->caps_overwriting = true;
 336        err = snd_hdac_regmap_write_raw(codec, verb, val);
 337        codec->caps_overwriting = false;
 338        return err;
 339}
 340EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
 341
 342/**
 343 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
 344 * @codec: the codec object
 345 * @nid: NID to inspect
 346 * @start_id: the pointer to store the starting NID
 347 *
 348 * Returns the number of subtree nodes or zero if not found.
 349 * This function reads parameters always without caching.
 350 */
 351int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
 352                           hda_nid_t *start_id)
 353{
 354        unsigned int parm;
 355
 356        parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
 357        if (parm == -1) {
 358                *start_id = 0;
 359                return 0;
 360        }
 361        *start_id = (parm >> 16) & 0x7fff;
 362        return (int)(parm & 0x7fff);
 363}
 364EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
 365
 366/*
 367 * look for an AFG and MFG nodes
 368 */
 369static void setup_fg_nodes(struct hdac_device *codec)
 370{
 371        int i, total_nodes, function_id;
 372        hda_nid_t nid;
 373
 374        total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
 375        for (i = 0; i < total_nodes; i++, nid++) {
 376                function_id = snd_hdac_read_parm(codec, nid,
 377                                                 AC_PAR_FUNCTION_TYPE);
 378                switch (function_id & 0xff) {
 379                case AC_GRP_AUDIO_FUNCTION:
 380                        codec->afg = nid;
 381                        codec->afg_function_id = function_id & 0xff;
 382                        codec->afg_unsol = (function_id >> 8) & 1;
 383                        break;
 384                case AC_GRP_MODEM_FUNCTION:
 385                        codec->mfg = nid;
 386                        codec->mfg_function_id = function_id & 0xff;
 387                        codec->mfg_unsol = (function_id >> 8) & 1;
 388                        break;
 389                default:
 390                        break;
 391                }
 392        }
 393}
 394
 395/**
 396 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
 397 * @codec: the codec object
 398 */
 399int snd_hdac_refresh_widgets(struct hdac_device *codec)
 400{
 401        hda_nid_t start_nid;
 402        int nums, err = 0;
 403
 404        /*
 405         * Serialize against multiple threads trying to update the sysfs
 406         * widgets array.
 407         */
 408        mutex_lock(&codec->widget_lock);
 409        nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
 410        if (!start_nid || nums <= 0 || nums >= 0xff) {
 411                dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
 412                        codec->afg);
 413                err = -EINVAL;
 414                goto unlock;
 415        }
 416
 417        err = hda_widget_sysfs_reinit(codec, start_nid, nums);
 418        if (err < 0)
 419                goto unlock;
 420
 421        codec->num_nodes = nums;
 422        codec->start_nid = start_nid;
 423        codec->end_nid = start_nid + nums;
 424unlock:
 425        mutex_unlock(&codec->widget_lock);
 426        return err;
 427}
 428EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
 429
 430/* return CONNLIST_LEN parameter of the given widget */
 431static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
 432{
 433        unsigned int wcaps = get_wcaps(codec, nid);
 434        unsigned int parm;
 435
 436        if (!(wcaps & AC_WCAP_CONN_LIST) &&
 437            get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
 438                return 0;
 439
 440        parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
 441        if (parm == -1)
 442                parm = 0;
 443        return parm;
 444}
 445
 446/**
 447 * snd_hdac_get_connections - get a widget connection list
 448 * @codec: the codec object
 449 * @nid: NID
 450 * @conn_list: the array to store the results, can be NULL
 451 * @max_conns: the max size of the given array
 452 *
 453 * Returns the number of connected widgets, zero for no connection, or a
 454 * negative error code.  When the number of elements don't fit with the
 455 * given array size, it returns -ENOSPC.
 456 *
 457 * When @conn_list is NULL, it just checks the number of connections.
 458 */
 459int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
 460                             hda_nid_t *conn_list, int max_conns)
 461{
 462        unsigned int parm;
 463        int i, conn_len, conns, err;
 464        unsigned int shift, num_elems, mask;
 465        hda_nid_t prev_nid;
 466        int null_count = 0;
 467
 468        parm = get_num_conns(codec, nid);
 469        if (!parm)
 470                return 0;
 471
 472        if (parm & AC_CLIST_LONG) {
 473                /* long form */
 474                shift = 16;
 475                num_elems = 2;
 476        } else {
 477                /* short form */
 478                shift = 8;
 479                num_elems = 4;
 480        }
 481        conn_len = parm & AC_CLIST_LENGTH;
 482        mask = (1 << (shift-1)) - 1;
 483
 484        if (!conn_len)
 485                return 0; /* no connection */
 486
 487        if (conn_len == 1) {
 488                /* single connection */
 489                err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
 490                                    &parm);
 491                if (err < 0)
 492                        return err;
 493                if (conn_list)
 494                        conn_list[0] = parm & mask;
 495                return 1;
 496        }
 497
 498        /* multi connection */
 499        conns = 0;
 500        prev_nid = 0;
 501        for (i = 0; i < conn_len; i++) {
 502                int range_val;
 503                hda_nid_t val, n;
 504
 505                if (i % num_elems == 0) {
 506                        err = snd_hdac_read(codec, nid,
 507                                            AC_VERB_GET_CONNECT_LIST, i,
 508                                            &parm);
 509                        if (err < 0)
 510                                return -EIO;
 511                }
 512                range_val = !!(parm & (1 << (shift-1))); /* ranges */
 513                val = parm & mask;
 514                if (val == 0 && null_count++) {  /* no second chance */
 515                        dev_dbg(&codec->dev,
 516                                "invalid CONNECT_LIST verb %x[%i]:%x\n",
 517                                nid, i, parm);
 518                        return 0;
 519                }
 520                parm >>= shift;
 521                if (range_val) {
 522                        /* ranges between the previous and this one */
 523                        if (!prev_nid || prev_nid >= val) {
 524                                dev_warn(&codec->dev,
 525                                         "invalid dep_range_val %x:%x\n",
 526                                         prev_nid, val);
 527                                continue;
 528                        }
 529                        for (n = prev_nid + 1; n <= val; n++) {
 530                                if (conn_list) {
 531                                        if (conns >= max_conns)
 532                                                return -ENOSPC;
 533                                        conn_list[conns] = n;
 534                                }
 535                                conns++;
 536                        }
 537                } else {
 538                        if (conn_list) {
 539                                if (conns >= max_conns)
 540                                        return -ENOSPC;
 541                                conn_list[conns] = val;
 542                        }
 543                        conns++;
 544                }
 545                prev_nid = val;
 546        }
 547        return conns;
 548}
 549EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
 550
 551#ifdef CONFIG_PM
 552/**
 553 * snd_hdac_power_up - power up the codec
 554 * @codec: the codec object
 555 *
 556 * This function calls the runtime PM helper to power up the given codec.
 557 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
 558 * path that isn't included in PM path.  Otherwise it gets stuck.
 559 *
 560 * Returns zero if successful, or a negative error code.
 561 */
 562int snd_hdac_power_up(struct hdac_device *codec)
 563{
 564        return pm_runtime_get_sync(&codec->dev);
 565}
 566EXPORT_SYMBOL_GPL(snd_hdac_power_up);
 567
 568/**
 569 * snd_hdac_power_down - power down the codec
 570 * @codec: the codec object
 571 *
 572 * Returns zero if successful, or a negative error code.
 573 */
 574int snd_hdac_power_down(struct hdac_device *codec)
 575{
 576        struct device *dev = &codec->dev;
 577
 578        pm_runtime_mark_last_busy(dev);
 579        return pm_runtime_put_autosuspend(dev);
 580}
 581EXPORT_SYMBOL_GPL(snd_hdac_power_down);
 582
 583/**
 584 * snd_hdac_power_up_pm - power up the codec
 585 * @codec: the codec object
 586 *
 587 * This function can be called in a recursive code path like init code
 588 * which may be called by PM suspend/resume again.  OTOH, if a power-up
 589 * call must wake up the sleeper (e.g. in a kctl callback), use
 590 * snd_hdac_power_up() instead.
 591 *
 592 * Returns zero if successful, or a negative error code.
 593 */
 594int snd_hdac_power_up_pm(struct hdac_device *codec)
 595{
 596        if (!atomic_inc_not_zero(&codec->in_pm))
 597                return snd_hdac_power_up(codec);
 598        return 0;
 599}
 600EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
 601
 602/* like snd_hdac_power_up_pm(), but only increment the pm count when
 603 * already powered up.  Returns -1 if not powered up, 1 if incremented
 604 * or 0 if unchanged.  Only used in hdac_regmap.c
 605 */
 606int snd_hdac_keep_power_up(struct hdac_device *codec)
 607{
 608        if (!atomic_inc_not_zero(&codec->in_pm)) {
 609                int ret = pm_runtime_get_if_in_use(&codec->dev);
 610                if (!ret)
 611                        return -1;
 612                if (ret < 0)
 613                        return 0;
 614        }
 615        return 1;
 616}
 617
 618/**
 619 * snd_hdac_power_down_pm - power down the codec
 620 * @codec: the codec object
 621 *
 622 * Like snd_hdac_power_up_pm(), this function is used in a recursive
 623 * code path like init code which may be called by PM suspend/resume again.
 624 *
 625 * Returns zero if successful, or a negative error code.
 626 */
 627int snd_hdac_power_down_pm(struct hdac_device *codec)
 628{
 629        if (atomic_dec_if_positive(&codec->in_pm) < 0)
 630                return snd_hdac_power_down(codec);
 631        return 0;
 632}
 633EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
 634#endif
 635
 636/* codec vendor labels */
 637struct hda_vendor_id {
 638        unsigned int id;
 639        const char *name;
 640};
 641
 642static struct hda_vendor_id hda_vendor_ids[] = {
 643        { 0x1002, "ATI" },
 644        { 0x1013, "Cirrus Logic" },
 645        { 0x1057, "Motorola" },
 646        { 0x1095, "Silicon Image" },
 647        { 0x10de, "Nvidia" },
 648        { 0x10ec, "Realtek" },
 649        { 0x1102, "Creative" },
 650        { 0x1106, "VIA" },
 651        { 0x111d, "IDT" },
 652        { 0x11c1, "LSI" },
 653        { 0x11d4, "Analog Devices" },
 654        { 0x13f6, "C-Media" },
 655        { 0x14f1, "Conexant" },
 656        { 0x17e8, "Chrontel" },
 657        { 0x1854, "LG" },
 658        { 0x1aec, "Wolfson Microelectronics" },
 659        { 0x1af4, "QEMU" },
 660        { 0x434d, "C-Media" },
 661        { 0x8086, "Intel" },
 662        { 0x8384, "SigmaTel" },
 663        {} /* terminator */
 664};
 665
 666/* store the codec vendor name */
 667static int get_codec_vendor_name(struct hdac_device *codec)
 668{
 669        const struct hda_vendor_id *c;
 670        u16 vendor_id = codec->vendor_id >> 16;
 671
 672        for (c = hda_vendor_ids; c->id; c++) {
 673                if (c->id == vendor_id) {
 674                        codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
 675                        return codec->vendor_name ? 0 : -ENOMEM;
 676                }
 677        }
 678
 679        codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
 680        return codec->vendor_name ? 0 : -ENOMEM;
 681}
 682
 683/*
 684 * stream formats
 685 */
 686struct hda_rate_tbl {
 687        unsigned int hz;
 688        unsigned int alsa_bits;
 689        unsigned int hda_fmt;
 690};
 691
 692/* rate = base * mult / div */
 693#define HDA_RATE(base, mult, div) \
 694        (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
 695         (((div) - 1) << AC_FMT_DIV_SHIFT))
 696
 697static struct hda_rate_tbl rate_bits[] = {
 698        /* rate in Hz, ALSA rate bitmask, HDA format value */
 699
 700        /* autodetected value used in snd_hda_query_supported_pcm */
 701        { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
 702        { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
 703        { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
 704        { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
 705        { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
 706        { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
 707        { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
 708        { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
 709        { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
 710        { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
 711        { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
 712#define AC_PAR_PCM_RATE_BITS    11
 713        /* up to bits 10, 384kHZ isn't supported properly */
 714
 715        /* not autodetected value */
 716        { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
 717
 718        { 0 } /* terminator */
 719};
 720
 721/**
 722 * snd_hdac_calc_stream_format - calculate the format bitset
 723 * @rate: the sample rate
 724 * @channels: the number of channels
 725 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
 726 * @maxbps: the max. bps
 727 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
 728 *
 729 * Calculate the format bitset from the given rate, channels and th PCM format.
 730 *
 731 * Return zero if invalid.
 732 */
 733unsigned int snd_hdac_calc_stream_format(unsigned int rate,
 734                                         unsigned int channels,
 735                                         snd_pcm_format_t format,
 736                                         unsigned int maxbps,
 737                                         unsigned short spdif_ctls)
 738{
 739        int i;
 740        unsigned int val = 0;
 741
 742        for (i = 0; rate_bits[i].hz; i++)
 743                if (rate_bits[i].hz == rate) {
 744                        val = rate_bits[i].hda_fmt;
 745                        break;
 746                }
 747        if (!rate_bits[i].hz)
 748                return 0;
 749
 750        if (channels == 0 || channels > 8)
 751                return 0;
 752        val |= channels - 1;
 753
 754        switch (snd_pcm_format_width(format)) {
 755        case 8:
 756                val |= AC_FMT_BITS_8;
 757                break;
 758        case 16:
 759                val |= AC_FMT_BITS_16;
 760                break;
 761        case 20:
 762        case 24:
 763        case 32:
 764                if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
 765                        val |= AC_FMT_BITS_32;
 766                else if (maxbps >= 24)
 767                        val |= AC_FMT_BITS_24;
 768                else
 769                        val |= AC_FMT_BITS_20;
 770                break;
 771        default:
 772                return 0;
 773        }
 774
 775        if (spdif_ctls & AC_DIG1_NONAUDIO)
 776                val |= AC_FMT_TYPE_NON_PCM;
 777
 778        return val;
 779}
 780EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
 781
 782static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
 783{
 784        unsigned int val = 0;
 785
 786        if (nid != codec->afg &&
 787            (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
 788                val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
 789        if (!val || val == -1)
 790                val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
 791        if (!val || val == -1)
 792                return 0;
 793        return val;
 794}
 795
 796static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
 797{
 798        unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
 799
 800        if (!streams || streams == -1)
 801                streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
 802        if (!streams || streams == -1)
 803                return 0;
 804        return streams;
 805}
 806
 807/**
 808 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
 809 * @codec: the codec object
 810 * @nid: NID to query
 811 * @ratesp: the pointer to store the detected rate bitflags
 812 * @formatsp: the pointer to store the detected formats
 813 * @bpsp: the pointer to store the detected format widths
 814 *
 815 * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
 816 * or @bsps argument is ignored.
 817 *
 818 * Returns 0 if successful, otherwise a negative error code.
 819 */
 820int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
 821                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
 822{
 823        unsigned int i, val, wcaps;
 824
 825        wcaps = get_wcaps(codec, nid);
 826        val = query_pcm_param(codec, nid);
 827
 828        if (ratesp) {
 829                u32 rates = 0;
 830                for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
 831                        if (val & (1 << i))
 832                                rates |= rate_bits[i].alsa_bits;
 833                }
 834                if (rates == 0) {
 835                        dev_err(&codec->dev,
 836                                "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
 837                                nid, val,
 838                                (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
 839                        return -EIO;
 840                }
 841                *ratesp = rates;
 842        }
 843
 844        if (formatsp || bpsp) {
 845                u64 formats = 0;
 846                unsigned int streams, bps;
 847
 848                streams = query_stream_param(codec, nid);
 849                if (!streams)
 850                        return -EIO;
 851
 852                bps = 0;
 853                if (streams & AC_SUPFMT_PCM) {
 854                        if (val & AC_SUPPCM_BITS_8) {
 855                                formats |= SNDRV_PCM_FMTBIT_U8;
 856                                bps = 8;
 857                        }
 858                        if (val & AC_SUPPCM_BITS_16) {
 859                                formats |= SNDRV_PCM_FMTBIT_S16_LE;
 860                                bps = 16;
 861                        }
 862                        if (wcaps & AC_WCAP_DIGITAL) {
 863                                if (val & AC_SUPPCM_BITS_32)
 864                                        formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
 865                                if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
 866                                        formats |= SNDRV_PCM_FMTBIT_S32_LE;
 867                                if (val & AC_SUPPCM_BITS_24)
 868                                        bps = 24;
 869                                else if (val & AC_SUPPCM_BITS_20)
 870                                        bps = 20;
 871                        } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
 872                                          AC_SUPPCM_BITS_32)) {
 873                                formats |= SNDRV_PCM_FMTBIT_S32_LE;
 874                                if (val & AC_SUPPCM_BITS_32)
 875                                        bps = 32;
 876                                else if (val & AC_SUPPCM_BITS_24)
 877                                        bps = 24;
 878                                else if (val & AC_SUPPCM_BITS_20)
 879                                        bps = 20;
 880                        }
 881                }
 882#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
 883                if (streams & AC_SUPFMT_FLOAT32) {
 884                        formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
 885                        if (!bps)
 886                                bps = 32;
 887                }
 888#endif
 889                if (streams == AC_SUPFMT_AC3) {
 890                        /* should be exclusive */
 891                        /* temporary hack: we have still no proper support
 892                         * for the direct AC3 stream...
 893                         */
 894                        formats |= SNDRV_PCM_FMTBIT_U8;
 895                        bps = 8;
 896                }
 897                if (formats == 0) {
 898                        dev_err(&codec->dev,
 899                                "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
 900                                nid, val,
 901                                (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
 902                                streams);
 903                        return -EIO;
 904                }
 905                if (formatsp)
 906                        *formatsp = formats;
 907                if (bpsp)
 908                        *bpsp = bps;
 909        }
 910
 911        return 0;
 912}
 913EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
 914
 915/**
 916 * snd_hdac_is_supported_format - Check the validity of the format
 917 * @codec: the codec object
 918 * @nid: NID to check
 919 * @format: the HD-audio format value to check
 920 *
 921 * Check whether the given node supports the format value.
 922 *
 923 * Returns true if supported, false if not.
 924 */
 925bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
 926                                  unsigned int format)
 927{
 928        int i;
 929        unsigned int val = 0, rate, stream;
 930
 931        val = query_pcm_param(codec, nid);
 932        if (!val)
 933                return false;
 934
 935        rate = format & 0xff00;
 936        for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
 937                if (rate_bits[i].hda_fmt == rate) {
 938                        if (val & (1 << i))
 939                                break;
 940                        return false;
 941                }
 942        if (i >= AC_PAR_PCM_RATE_BITS)
 943                return false;
 944
 945        stream = query_stream_param(codec, nid);
 946        if (!stream)
 947                return false;
 948
 949        if (stream & AC_SUPFMT_PCM) {
 950                switch (format & 0xf0) {
 951                case 0x00:
 952                        if (!(val & AC_SUPPCM_BITS_8))
 953                                return false;
 954                        break;
 955                case 0x10:
 956                        if (!(val & AC_SUPPCM_BITS_16))
 957                                return false;
 958                        break;
 959                case 0x20:
 960                        if (!(val & AC_SUPPCM_BITS_20))
 961                                return false;
 962                        break;
 963                case 0x30:
 964                        if (!(val & AC_SUPPCM_BITS_24))
 965                                return false;
 966                        break;
 967                case 0x40:
 968                        if (!(val & AC_SUPPCM_BITS_32))
 969                                return false;
 970                        break;
 971                default:
 972                        return false;
 973                }
 974        } else {
 975                /* FIXME: check for float32 and AC3? */
 976        }
 977
 978        return true;
 979}
 980EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
 981
 982static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
 983                        int flags, unsigned int verb, unsigned int parm)
 984{
 985        unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
 986        unsigned int res;
 987
 988        if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
 989                return -1;
 990
 991        return res;
 992}
 993
 994static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
 995                        int flags, unsigned int verb, unsigned int parm)
 996{
 997        unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
 998
 999        return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1000}
1001
1002/**
1003 * snd_hdac_codec_read - send a command and get the response
1004 * @hdac: the HDAC device
1005 * @nid: NID to send the command
1006 * @flags: optional bit flags
1007 * @verb: the verb to send
1008 * @parm: the parameter for the verb
1009 *
1010 * Send a single command and read the corresponding response.
1011 *
1012 * Returns the obtained response value, or -1 for an error.
1013 */
1014int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1015                        int flags, unsigned int verb, unsigned int parm)
1016{
1017        return codec_read(hdac, nid, flags, verb, parm);
1018}
1019EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1020
1021/**
1022 * snd_hdac_codec_write - send a single command without waiting for response
1023 * @hdac: the HDAC device
1024 * @nid: NID to send the command
1025 * @flags: optional bit flags
1026 * @verb: the verb to send
1027 * @parm: the parameter for the verb
1028 *
1029 * Send a single command without waiting for response.
1030 *
1031 * Returns 0 if successful, or a negative error code.
1032 */
1033int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1034                        int flags, unsigned int verb, unsigned int parm)
1035{
1036        return codec_write(hdac, nid, flags, verb, parm);
1037}
1038EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1039
1040/**
1041 * snd_hdac_check_power_state - check whether the actual power state matches
1042 * with the target state
1043 *
1044 * @hdac: the HDAC device
1045 * @nid: NID to send the command
1046 * @target_state: target state to check for
1047 *
1048 * Return true if state matches, false if not
1049 */
1050bool snd_hdac_check_power_state(struct hdac_device *hdac,
1051                hda_nid_t nid, unsigned int target_state)
1052{
1053        unsigned int state = codec_read(hdac, nid, 0,
1054                                AC_VERB_GET_POWER_STATE, 0);
1055
1056        if (state & AC_PWRST_ERROR)
1057                return true;
1058        state = (state >> 4) & 0x0f;
1059        return (state == target_state);
1060}
1061EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1062/**
1063 * snd_hdac_sync_power_state - wait until actual power state matches
1064 * with the target state
1065 *
1066 * @hdac: the HDAC device
1067 * @nid: NID to send the command
1068 * @target_state: target state to check for
1069 *
1070 * Return power state or PS_ERROR if codec rejects GET verb.
1071 */
1072unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1073                        hda_nid_t nid, unsigned int power_state)
1074{
1075        unsigned long end_time = jiffies + msecs_to_jiffies(500);
1076        unsigned int state, actual_state, count;
1077
1078        for (count = 0; count < 500; count++) {
1079                state = snd_hdac_codec_read(codec, nid, 0,
1080                                AC_VERB_GET_POWER_STATE, 0);
1081                if (state & AC_PWRST_ERROR) {
1082                        msleep(20);
1083                        break;
1084                }
1085                actual_state = (state >> 4) & 0x0f;
1086                if (actual_state == power_state)
1087                        break;
1088                if (time_after_eq(jiffies, end_time))
1089                        break;
1090                /* wait until the codec reachs to the target state */
1091                msleep(1);
1092        }
1093        return state;
1094}
1095EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);
1096