162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * HD-audio regmap helpers
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef __SOUND_HDA_REGMAP_H
762306a36Sopenharmony_ci#define __SOUND_HDA_REGMAP_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/regmap.h>
1062306a36Sopenharmony_ci#include <sound/core.h>
1162306a36Sopenharmony_ci#include <sound/hdaudio.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define AC_AMP_FAKE_MUTE	0x10	/* fake mute bit set to amp verbs */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciint snd_hdac_regmap_init(struct hdac_device *codec);
1662306a36Sopenharmony_civoid snd_hdac_regmap_exit(struct hdac_device *codec);
1762306a36Sopenharmony_ciint snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
1862306a36Sopenharmony_ci				    unsigned int verb);
1962306a36Sopenharmony_ciint snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
2062306a36Sopenharmony_ci			     unsigned int *val);
2162306a36Sopenharmony_ciint snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec,
2262306a36Sopenharmony_ci				      unsigned int reg, unsigned int *val);
2362306a36Sopenharmony_ciint snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
2462306a36Sopenharmony_ci			      unsigned int val);
2562306a36Sopenharmony_ciint snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
2662306a36Sopenharmony_ci			       unsigned int mask, unsigned int val);
2762306a36Sopenharmony_ciint snd_hdac_regmap_update_raw_once(struct hdac_device *codec, unsigned int reg,
2862306a36Sopenharmony_ci				    unsigned int mask, unsigned int val);
2962306a36Sopenharmony_civoid snd_hdac_regmap_sync(struct hdac_device *codec);
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * snd_hdac_regmap_encode_verb - encode the verb to a pseudo register
3362306a36Sopenharmony_ci * @nid: widget NID
3462306a36Sopenharmony_ci * @verb: codec verb
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Returns an encoded pseudo register.
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_ci#define snd_hdac_regmap_encode_verb(nid, verb)		\
3962306a36Sopenharmony_ci	(((verb) << 8) | 0x80000 | ((unsigned int)(nid) << 20))
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/**
4262306a36Sopenharmony_ci * snd_hdac_regmap_encode_amp - encode the AMP verb to a pseudo register
4362306a36Sopenharmony_ci * @nid: widget NID
4462306a36Sopenharmony_ci * @ch: channel (left = 0, right = 1)
4562306a36Sopenharmony_ci * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
4662306a36Sopenharmony_ci * @idx: input index value
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * Returns an encoded pseudo register.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_ci#define snd_hdac_regmap_encode_amp(nid, ch, dir, idx)			\
5162306a36Sopenharmony_ci	(snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) |	\
5262306a36Sopenharmony_ci	 ((ch) ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT) |			\
5362306a36Sopenharmony_ci	 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
5462306a36Sopenharmony_ci	 (idx))
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/**
5762306a36Sopenharmony_ci * snd_hdac_regmap_encode_amp_stereo - encode a pseudo register for stereo AMPs
5862306a36Sopenharmony_ci * @nid: widget NID
5962306a36Sopenharmony_ci * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
6062306a36Sopenharmony_ci * @idx: input index value
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Returns an encoded pseudo register.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_ci#define snd_hdac_regmap_encode_amp_stereo(nid, dir, idx)		\
6562306a36Sopenharmony_ci	(snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) |	\
6662306a36Sopenharmony_ci	 AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT | /* both bits set! */	\
6762306a36Sopenharmony_ci	 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
6862306a36Sopenharmony_ci	 (idx))
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/**
7162306a36Sopenharmony_ci * snd_hdac_regmap_write - Write a verb with caching
7262306a36Sopenharmony_ci * @nid: codec NID
7362306a36Sopenharmony_ci * @reg: verb to write
7462306a36Sopenharmony_ci * @val: value to write
7562306a36Sopenharmony_ci *
7662306a36Sopenharmony_ci * For writing an amp value, use snd_hdac_regmap_update_amp().
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_cistatic inline int
7962306a36Sopenharmony_cisnd_hdac_regmap_write(struct hdac_device *codec, hda_nid_t nid,
8062306a36Sopenharmony_ci		      unsigned int verb, unsigned int val)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	return snd_hdac_regmap_write_raw(codec, cmd, val);
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/**
8862306a36Sopenharmony_ci * snd_hda_regmap_update - Update a verb value with caching
8962306a36Sopenharmony_ci * @nid: codec NID
9062306a36Sopenharmony_ci * @verb: verb to update
9162306a36Sopenharmony_ci * @mask: bit mask to update
9262306a36Sopenharmony_ci * @val: value to update
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * For updating an amp value, use snd_hdac_regmap_update_amp().
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_cistatic inline int
9762306a36Sopenharmony_cisnd_hdac_regmap_update(struct hdac_device *codec, hda_nid_t nid,
9862306a36Sopenharmony_ci		       unsigned int verb, unsigned int mask,
9962306a36Sopenharmony_ci		       unsigned int val)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci/**
10762306a36Sopenharmony_ci * snd_hda_regmap_read - Read a verb with caching
10862306a36Sopenharmony_ci * @nid: codec NID
10962306a36Sopenharmony_ci * @verb: verb to read
11062306a36Sopenharmony_ci * @val: pointer to store the value
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci * For reading an amp value, use snd_hda_regmap_get_amp().
11362306a36Sopenharmony_ci */
11462306a36Sopenharmony_cistatic inline int
11562306a36Sopenharmony_cisnd_hdac_regmap_read(struct hdac_device *codec, hda_nid_t nid,
11662306a36Sopenharmony_ci		     unsigned int verb, unsigned int *val)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	return snd_hdac_regmap_read_raw(codec, cmd, val);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/**
12462306a36Sopenharmony_ci * snd_hdac_regmap_get_amp - Read AMP value
12562306a36Sopenharmony_ci * @codec: HD-audio codec
12662306a36Sopenharmony_ci * @nid: NID to read the AMP value
12762306a36Sopenharmony_ci * @ch: channel (left=0 or right=1)
12862306a36Sopenharmony_ci * @direction: #HDA_INPUT or #HDA_OUTPUT
12962306a36Sopenharmony_ci * @index: the index value (only for input direction)
13062306a36Sopenharmony_ci * @val: the pointer to store the value
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * Read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
13362306a36Sopenharmony_ci * Returns the value or a negative error.
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_cistatic inline int
13662306a36Sopenharmony_cisnd_hdac_regmap_get_amp(struct hdac_device *codec, hda_nid_t nid,
13762306a36Sopenharmony_ci			int ch, int dir, int idx)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
14062306a36Sopenharmony_ci	int err, val;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	err = snd_hdac_regmap_read_raw(codec, cmd, &val);
14362306a36Sopenharmony_ci	return err < 0 ? err : val;
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/**
14762306a36Sopenharmony_ci * snd_hdac_regmap_update_amp - update the AMP value
14862306a36Sopenharmony_ci * @codec: HD-audio codec
14962306a36Sopenharmony_ci * @nid: NID to read the AMP value
15062306a36Sopenharmony_ci * @ch: channel (left=0 or right=1)
15162306a36Sopenharmony_ci * @direction: #HDA_INPUT or #HDA_OUTPUT
15262306a36Sopenharmony_ci * @idx: the index value (only for input direction)
15362306a36Sopenharmony_ci * @mask: bit mask to set
15462306a36Sopenharmony_ci * @val: the bits value to set
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci * Update the AMP value with a bit mask.
15762306a36Sopenharmony_ci * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_cistatic inline int
16062306a36Sopenharmony_cisnd_hdac_regmap_update_amp(struct hdac_device *codec, hda_nid_t nid,
16162306a36Sopenharmony_ci			   int ch, int dir, int idx, int mask, int val)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci/**
16962306a36Sopenharmony_ci * snd_hdac_regmap_get_amp_stereo - Read stereo AMP values
17062306a36Sopenharmony_ci * @codec: HD-audio codec
17162306a36Sopenharmony_ci * @nid: NID to read the AMP value
17262306a36Sopenharmony_ci * @ch: channel (left=0 or right=1)
17362306a36Sopenharmony_ci * @direction: #HDA_INPUT or #HDA_OUTPUT
17462306a36Sopenharmony_ci * @index: the index value (only for input direction)
17562306a36Sopenharmony_ci * @val: the pointer to store the value
17662306a36Sopenharmony_ci *
17762306a36Sopenharmony_ci * Read stereo AMP values.  The lower byte is left, the upper byte is right.
17862306a36Sopenharmony_ci * Returns the value or a negative error.
17962306a36Sopenharmony_ci */
18062306a36Sopenharmony_cistatic inline int
18162306a36Sopenharmony_cisnd_hdac_regmap_get_amp_stereo(struct hdac_device *codec, hda_nid_t nid,
18262306a36Sopenharmony_ci			       int dir, int idx)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_amp_stereo(nid, dir, idx);
18562306a36Sopenharmony_ci	int err, val;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	err = snd_hdac_regmap_read_raw(codec, cmd, &val);
18862306a36Sopenharmony_ci	return err < 0 ? err : val;
18962306a36Sopenharmony_ci}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci/**
19262306a36Sopenharmony_ci * snd_hdac_regmap_update_amp_stereo - update the stereo AMP value
19362306a36Sopenharmony_ci * @codec: HD-audio codec
19462306a36Sopenharmony_ci * @nid: NID to read the AMP value
19562306a36Sopenharmony_ci * @direction: #HDA_INPUT or #HDA_OUTPUT
19662306a36Sopenharmony_ci * @idx: the index value (only for input direction)
19762306a36Sopenharmony_ci * @mask: bit mask to set
19862306a36Sopenharmony_ci * @val: the bits value to set
19962306a36Sopenharmony_ci *
20062306a36Sopenharmony_ci * Update the stereo AMP value with a bit mask.
20162306a36Sopenharmony_ci * The lower byte is left, the upper byte is right.
20262306a36Sopenharmony_ci * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
20362306a36Sopenharmony_ci */
20462306a36Sopenharmony_cistatic inline int
20562306a36Sopenharmony_cisnd_hdac_regmap_update_amp_stereo(struct hdac_device *codec, hda_nid_t nid,
20662306a36Sopenharmony_ci				  int dir, int idx, int mask, int val)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	unsigned int cmd = snd_hdac_regmap_encode_amp_stereo(nid, dir, idx);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/**
21462306a36Sopenharmony_ci * snd_hdac_regmap_sync_node - sync the widget node attributes
21562306a36Sopenharmony_ci * @codec: HD-audio codec
21662306a36Sopenharmony_ci * @nid: NID to sync
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_cistatic inline void
21962306a36Sopenharmony_cisnd_hdac_regmap_sync_node(struct hdac_device *codec, hda_nid_t nid)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	regcache_mark_dirty(codec->regmap);
22262306a36Sopenharmony_ci	regcache_sync_region(codec->regmap, nid << 20, ((nid + 1) << 20) - 1);
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci#endif /* __SOUND_HDA_REGMAP_H */
226