/*
* BIOS auto-parser helper functions for HD-audio
*
* Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
*
* This driver is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/slab.h>
#include <linux/export.h>
#include <linux/sort.h>
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"
#include "hda_auto_parser.h"
#define SFX "hda_codec: "
/*
* Helper for automatic pin configuration
*/
static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
{
for (; *list; list++)
if (*list == nid)
return 1;
return 0;
}
/* a pair of input pin and its sequence */
struct auto_out_pin {
hda_nid_t pin;
short seq;
};
static int compare_seq(const void *ap, const void *bp)
{
const struct auto_out_pin *a = ap;
const struct auto_out_pin *b = bp;
return (int)(a->seq - b->seq);
}
/*
* Sort an associated group of pins according to their sequence numbers.
* then store it to a pin array.
*/
static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
int num_pins)
{
int i;
sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
for (i = 0; i < num_pins; i++)
pins[i] = list[i].pin;
}
/* add the found input-pin to the cfg->inputs[] table */
static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
int type)
{
if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
cfg->inputs[cfg->num_inputs].pin = nid;
cfg->inputs[cfg->num_inputs].type = type;
cfg->num_inputs++;
}
}
static int compare_input_type(const void *ap, const void *bp)
{
const struct auto_pin_cfg_item *a = ap;
const struct auto_pin_cfg_item *b = bp;
return (int)(a->type - b->type);
}
/* Reorder the surround channels
* ALSA sequence is front/surr/clfe/side
* HDA sequence is:
* 4-ch: front/surr => OK as it is
* 6-ch: front/clfe/surr
* 8-ch: front/clfe/rear/side|fc
*/
static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
{
hda_nid_t nid;
switch (nums) {
case 3:
case 4:
nid = pins[1];
pins[1] = pins[2];
pins[2] = nid;
break;
}
}
/* check whether the given pin has a proper pin I/O capability bit */
static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
unsigned int dev)
{
unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
/* some old hardware don't return the proper pincaps */
if (!pincap)
return true;
switch (dev) {
case AC_JACK_LINE_OUT:
case AC_JACK_SPEAKER:
case AC_JACK_HP_OUT:
case AC_JACK_SPDIF_OUT:
case AC_JACK_DIG_OTHER_OUT:
return !!(pincap & AC_PINCAP_OUT);
default:
return !!(pincap & AC_PINCAP_IN);
}
}
static bool can_be_headset_mic(struct hda_codec *codec,
struct auto_pin_cfg_item *item,
int seq_number)
{
int attr;
unsigned int def_conf;
if (item->type != AUTO_PIN_MIC)
return false;
if (item->is_headset_mic || item->is_headphone_mic)
return false; /* Already assigned */
def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
attr = snd_hda_get_input_pin_attr(def_conf);
if (attr <= INPUT_PIN_ATTR_DOCK)
return false;
if (seq_number >= 0) {
int seq = get_defcfg_sequence(def_conf);
if (seq != seq_number)
return false;
}
return true;
}
/*
* Parse all pin widgets and store the useful pin nids to cfg
*
* The number of line-outs or any primary output is stored in line_outs,
* and the corresponding output pins are assigned to line_out_pins[],
* in the order of front, rear, CLFE, side, ...
*
* If more extra outputs (speaker and headphone) are found, the pins are
* assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
* is detected, one of speaker of HP pins is assigned as the primary
* output, i.e. to line_out_pins[0]. So, line_outs is always positive
* if any analog output exists.
*
* The analog input pins are assigned to inputs array.
* The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
* respectively.
*/
int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
struct auto_pin_cfg *cfg,
const hda_nid_t *ignore_nids,
unsigned int cond_flags)
{
hda_nid_t nid, end_nid;
short seq, assoc_line_out;
struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
int i;
if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
cond_flags = i;
memset(cfg, 0