Commit 4d75faa0 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: hda - Add regmap support

This patch adds an infrastructure to support regmap-based verb
accesses.  Because o the asymmetric nature of HD-audio verbs,
especially the amp verbs, we need to translate the verbs as a sort of
pseudo registers to be mapped uniquely in regmap.

In this patch, a pseudo register is built from the NID, the
AC_VERB_GET_* and 8bit parameters, i.e. almost in the form to be sent
to HD-audio bus but without codec address field.  OTOH, for writing,
the same pseudo register is translated to AC_VERB_SET_* automatically.
The AC_VERB_SET_AMP_* verb is re-encoded from the corresponding
AC_VERB_GET_AMP_* verb and parameter at writing.

Some verbs has a single command for read but multiple for writes.  A
write for such a verb is split automatically to multiple verbs.

The patch provides also a few handy helper functions.  They are
designed to be accessible even without regmap.  When no regmap is set
up (e.g. before the codec device instantiation), the direct hardware
access is used.  Also, it tries to avoid the unnecessary power-up.
The power up/down sequence is performed only on demand.

The codec driver needs to call snd_hdac_regmap_exit() and
snd_hdac_regmap_exit() at probe and remove if it wants the regmap
access.

There is one flag added to hdac_device.  When the flag lazy_cache is
set, regmap helper ignores a write for a suspended device and returns
as if it was actually written.  It reduces the hardware access pretty
much, e.g. when adjusting the mixer volume while in idle.  This
assumes that the driver will sync the cache later at resume properly,
so use it carefully.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 71fc4c7e
/*
* HD-audio regmap helpers
*/
#ifndef __SOUND_HDA_REGMAP_H
#define __SOUND_HDA_REGMAP_H
#include <linux/regmap.h>
#include <sound/core.h>
#include <sound/hdaudio.h>
int snd_hdac_regmap_init(struct hdac_device *codec);
void snd_hdac_regmap_exit(struct hdac_device *codec);
int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
unsigned int *val);
int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
unsigned int val);
int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
unsigned int mask, unsigned int val);
/**
* snd_hdac_regmap_encode_verb - encode the verb to a pseudo register
* @nid: widget NID
* @verb: codec verb
*
* Returns an encoded pseudo register.
*/
#define snd_hdac_regmap_encode_verb(nid, verb) \
(((verb) << 8) | 0x80000 | ((unsigned int)(nid) << 20))
/**
* snd_hdac_regmap_encode_amp - encode the AMP verb to a pseudo register
* @nid: widget NID
* @ch: channel (left = 0, right = 1)
* @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
* @idx: input index value
*
* Returns an encoded pseudo register.
*/
#define snd_hdac_regmap_encode_amp(nid, ch, dir, idx) \
(snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) | \
((ch) ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT) | \
((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
(idx))
/**
* snd_hdac_regmap_write - Write a verb with caching
* @nid: codec NID
* @reg: verb to write
* @val: value to write
*
* For writing an amp value, use snd_hda_regmap_amp_update().
*/
static inline int
snd_hdac_regmap_write(struct hdac_device *codec, hda_nid_t nid,
unsigned int verb, unsigned int val)
{
unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
return snd_hdac_regmap_write_raw(codec, cmd, val);
}
/**
* snd_hda_regmap_update - Update a verb value with caching
* @nid: codec NID
* @verb: verb to update
* @mask: bit mask to update
* @val: value to update
*
* For updating an amp value, use snd_hda_regmap_amp_update().
*/
static inline int
snd_hdac_regmap_update(struct hdac_device *codec, hda_nid_t nid,
unsigned int verb, unsigned int mask,
unsigned int val)
{
unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
}
/**
* snd_hda_regmap_read - Read a verb with caching
* @nid: codec NID
* @verb: verb to read
* @val: pointer to store the value
*
* For reading an amp value, use snd_hda_regmap_get_amp().
*/
static inline int
snd_hdac_regmap_read(struct hdac_device *codec, hda_nid_t nid,
unsigned int verb, unsigned int *val)
{
unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
return snd_hdac_regmap_read_raw(codec, cmd, val);
}
/**
* snd_hdac_regmap_get_amp - Read AMP value
* @codec: HD-audio codec
* @nid: NID to read the AMP value
* @ch: channel (left=0 or right=1)
* @direction: #HDA_INPUT or #HDA_OUTPUT
* @index: the index value (only for input direction)
* @val: the pointer to store the value
*
* Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
* Returns the value or a negative error.
*/
static inline int
snd_hdac_regmap_get_amp(struct hdac_device *codec, hda_nid_t nid,
int ch, int dir, int idx)
{
unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
int err, val;
err = snd_hdac_regmap_read_raw(codec, cmd, &val);
return err < 0 ? err : val;
}
/**
* snd_hdac_regmap_update_amp - update the AMP value
* @codec: HD-audio codec
* @nid: NID to read the AMP value
* @ch: channel (left=0 or right=1)
* @direction: #HDA_INPUT or #HDA_OUTPUT
* @idx: the index value (only for input direction)
* @mask: bit mask to set
* @val: the bits value to set
*
* Update the AMP value with a bit mask.
* Returns 0 if the value is unchanged, 1 if changed, or a negative error.
*/
static inline int
snd_hdac_regmap_update_amp(struct hdac_device *codec, hda_nid_t nid,
int ch, int dir, int idx, int mask, int val)
{
unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
}
#endif /* __SOUND_HDA_REGMAP_H */
......@@ -72,6 +72,10 @@ struct hdac_device {
/* sysfs */
struct hdac_widget_tree *widgets;
/* regmap */
struct regmap *regmap;
bool lazy_cache:1; /* don't wake up for writes */
};
/* device/driver type used for matching */
......
config SND_HDA_CORE
tristate
select REGMAP
snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o \
array.o
hdac_regmap.o array.o
snd-hda-core-objs += trace.o
CFLAGS_trace.o := -I$(src)
......
/*
* Regmap support for HD-audio verbs
*
* A virtual register is translated to one or more hda verbs for write,
* vice versa for read.
*
* A few limitations:
* - Provided for not all verbs but only subset standard non-volatile verbs.
* - For reading, only AC_VERB_GET_* variants can be used.
* - For writing, mapped to the *corresponding* AC_VERB_SET_* variants,
* so can't handle asymmetric verbs for read and write
*/
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/regmap.h>
#include <linux/export.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <sound/core.h>
#include <sound/hdaudio.h>
#include <sound/hda_regmap.h>
#ifdef CONFIG_PM
#define codec_is_running(codec) \
(atomic_read(&(codec)->in_pm) || \
!pm_runtime_suspended(&(codec)->dev))
#else
#define codec_is_running(codec) true
#endif
#define get_verb(reg) (((reg) >> 8) & 0xfff)
static bool hda_volatile_reg(struct device *dev, unsigned int reg)
{
unsigned int verb = get_verb(reg);
switch (verb) {
case AC_VERB_GET_PROC_COEF:
case AC_VERB_GET_COEF_INDEX:
case AC_VERB_GET_PROC_STATE:
case AC_VERB_GET_POWER_STATE:
case AC_VERB_GET_PIN_SENSE:
case AC_VERB_GET_HDMI_DIP_SIZE:
case AC_VERB_GET_HDMI_ELDD:
case AC_VERB_GET_HDMI_DIP_INDEX:
case AC_VERB_GET_HDMI_DIP_DATA:
case AC_VERB_GET_HDMI_DIP_XMIT:
case AC_VERB_GET_HDMI_CP_CTRL:
case AC_VERB_GET_HDMI_CHAN_SLOT:
case AC_VERB_GET_DEVICE_SEL:
case AC_VERB_GET_DEVICE_LIST: /* read-only volatile */
return true;
}
return false;
}
static bool hda_writeable_reg(struct device *dev, unsigned int reg)
{
unsigned int verb = get_verb(reg);
switch (verb & 0xf00) {
case AC_VERB_GET_STREAM_FORMAT:
case AC_VERB_GET_AMP_GAIN_MUTE:
return true;
case 0xf00:
break;
default:
return false;
}
switch (verb) {
case AC_VERB_GET_CONNECT_SEL:
case AC_VERB_GET_SDI_SELECT:
case AC_VERB_GET_CONV:
case AC_VERB_GET_PIN_WIDGET_CONTROL:
case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */
case AC_VERB_GET_BEEP_CONTROL:
case AC_VERB_GET_EAPD_BTLENABLE:
case AC_VERB_GET_DIGI_CONVERT_1:
case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */
case AC_VERB_GET_VOLUME_KNOB_CONTROL:
case AC_VERB_GET_CONFIG_DEFAULT:
case AC_VERB_GET_GPIO_MASK:
case AC_VERB_GET_GPIO_DIRECTION:
case AC_VERB_GET_GPIO_DATA: /* not for volatile read */
case AC_VERB_GET_GPIO_WAKE_MASK:
case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK:
case AC_VERB_GET_GPIO_STICKY_MASK:
case AC_VERB_GET_CVT_CHAN_COUNT:
return true;
}
return false;
}
static bool hda_readable_reg(struct device *dev, unsigned int reg)
{
unsigned int verb = get_verb(reg);
switch (verb) {
case AC_VERB_PARAMETERS:
case AC_VERB_GET_CONNECT_LIST:
case AC_VERB_GET_SUBSYSTEM_ID:
return true;
}
return hda_writeable_reg(dev, reg);
}
static int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
{
struct hdac_device *codec = context;
if (!codec_is_running(codec))
return -EAGAIN;
reg |= (codec->addr << 28);
return snd_hdac_exec_verb(codec, reg, 0, val);
}
static int hda_reg_write(void *context, unsigned int reg, unsigned int val)
{
struct hdac_device *codec = context;
unsigned int verb;
int i, bytes, err;
if (!codec_is_running(codec))
return codec->lazy_cache ? 0 : -EAGAIN;
reg &= ~0x00080000U; /* drop GET bit */
reg |= (codec->addr << 28);
verb = get_verb(reg);
switch (verb & 0xf00) {
case AC_VERB_SET_AMP_GAIN_MUTE:
verb = AC_VERB_SET_AMP_GAIN_MUTE;
if (reg & AC_AMP_GET_LEFT)
verb |= AC_AMP_SET_LEFT >> 8;
else
verb |= AC_AMP_SET_RIGHT >> 8;
if (reg & AC_AMP_GET_OUTPUT) {
verb |= AC_AMP_SET_OUTPUT >> 8;
} else {
verb |= AC_AMP_SET_INPUT >> 8;
verb |= reg & 0xf;
}
break;
}
switch (verb) {
case AC_VERB_SET_DIGI_CONVERT_1:
bytes = 2;
break;
case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
bytes = 4;
break;
default:
bytes = 1;
break;
}
for (i = 0; i < bytes; i++) {
reg &= ~0xfffff;
reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
err = snd_hdac_exec_verb(codec, reg, 0, NULL);
if (err < 0)
return err;
}
return 0;
}
static const struct regmap_config hda_regmap_cfg = {
.name = "hdaudio",
.reg_bits = 32,
.val_bits = 32,
.max_register = 0xfffffff,
.writeable_reg = hda_writeable_reg,
.readable_reg = hda_readable_reg,
.volatile_reg = hda_volatile_reg,
.cache_type = REGCACHE_RBTREE,
.reg_read = hda_reg_read,
.reg_write = hda_reg_write,
};
int snd_hdac_regmap_init(struct hdac_device *codec)
{
struct regmap *regmap;
regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
codec->regmap = regmap;
return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
void snd_hdac_regmap_exit(struct hdac_device *codec)
{
if (codec->regmap) {
regmap_exit(codec->regmap);
codec->regmap = NULL;
}
}
EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
/*
* helper functions
*/
/* write a pseudo-register value (w/o power sequence) */
static int reg_raw_write(struct hdac_device *codec, unsigned int reg,
unsigned int val)
{
if (!codec->regmap)
return hda_reg_write(codec, reg, val);
else
return regmap_write(codec->regmap, reg, val);
}
/**
* snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
* @codec: the codec object
* @reg: pseudo register
* @val: value to write
*
* Returns zero if successful or a negative error code.
*/
int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
unsigned int val)
{
int err;
err = reg_raw_write(codec, reg, val);
if (err == -EAGAIN) {
snd_hdac_power_up(codec);
err = reg_raw_write(codec, reg, val);
snd_hdac_power_down(codec);
}
return err;
}
EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
static int reg_raw_read(struct hdac_device *codec, unsigned int reg,
unsigned int *val)
{
if (!codec->regmap)
return hda_reg_read(codec, reg, val);
else
return regmap_read(codec->regmap, reg, val);
}
/**
* snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
* @codec: the codec object
* @reg: pseudo register
* @val: pointer to store the read value
*
* Returns zero if successful or a negative error code.
*/
int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
unsigned int *val)
{
int err;
err = reg_raw_read(codec, reg, val);
if (err == -EAGAIN) {
snd_hdac_power_up(codec);
err = reg_raw_read(codec, reg, val);
snd_hdac_power_down(codec);
}
return err;
}
EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
/**
* snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
* @codec: the codec object
* @reg: pseudo register
* @mask: bit mask to udpate
* @val: value to update
*
* Returns zero if successful or a negative error code.
*/
int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
unsigned int mask, unsigned int val)
{
unsigned int orig;
int err;
val &= mask;
err = snd_hdac_regmap_read_raw(codec, reg, &orig);
if (err < 0)
return err;
val |= orig & ~mask;
if (val == orig)
return 0;
err = snd_hdac_regmap_write_raw(codec, reg, val);
if (err < 0)
return err;
return 1;
}
EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);
......@@ -73,6 +73,9 @@ static int hda_codec_driver_probe(struct device *dev)
return -EINVAL;
err = codec_refresh_name(codec, codec->preset->name);
if (err < 0)
goto error;
err = snd_hdac_regmap_init(&codec->core);
if (err < 0)
goto error;
......@@ -98,6 +101,7 @@ static int hda_codec_driver_probe(struct device *dev)
snd_hda_codec_register(codec);
}
codec->core.lazy_cache = true;
return 0;
error_module:
......
......@@ -945,6 +945,7 @@ void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
snd_array_free(&codec->mixers);
snd_array_free(&codec->nids);
remove_conn_list(codec);
snd_hdac_regmap_exit(&codec->core);
}
static unsigned int hda_set_power_state(struct hda_codec *codec,
......
......@@ -28,6 +28,7 @@
#include <sound/hwdep.h>
#include <sound/hdaudio.h>
#include <sound/hda_verbs.h>
#include <sound/hda_regmap.h>
/*
* Structures
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment