18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Regmap support for HD-audio verbs
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * A virtual register is translated to one or more hda verbs for write,
68c2ecf20Sopenharmony_ci * vice versa for read.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * A few limitations:
98c2ecf20Sopenharmony_ci * - Provided for not all verbs but only subset standard non-volatile verbs.
108c2ecf20Sopenharmony_ci * - For reading, only AC_VERB_GET_* variants can be used.
118c2ecf20Sopenharmony_ci * - For writing, mapped to the *corresponding* AC_VERB_SET_* variants,
128c2ecf20Sopenharmony_ci *   so can't handle asymmetric verbs for read and write
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci#include <linux/export.h>
198c2ecf20Sopenharmony_ci#include <linux/pm.h>
208c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
218c2ecf20Sopenharmony_ci#include <sound/core.h>
228c2ecf20Sopenharmony_ci#include <sound/hdaudio.h>
238c2ecf20Sopenharmony_ci#include <sound/hda_regmap.h>
248c2ecf20Sopenharmony_ci#include "local.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int codec_pm_lock(struct hdac_device *codec)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	return snd_hdac_keep_power_up(codec);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void codec_pm_unlock(struct hdac_device *codec, int lock)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	if (lock == 1)
348c2ecf20Sopenharmony_ci		snd_hdac_power_down_pm(codec);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define get_verb(reg)	(((reg) >> 8) & 0xfff)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic bool hda_volatile_reg(struct device *dev, unsigned int reg)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct hdac_device *codec = dev_to_hdac_dev(dev);
428c2ecf20Sopenharmony_ci	unsigned int verb = get_verb(reg);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	switch (verb) {
458c2ecf20Sopenharmony_ci	case AC_VERB_GET_PROC_COEF:
468c2ecf20Sopenharmony_ci		return !codec->cache_coef;
478c2ecf20Sopenharmony_ci	case AC_VERB_GET_COEF_INDEX:
488c2ecf20Sopenharmony_ci	case AC_VERB_GET_PROC_STATE:
498c2ecf20Sopenharmony_ci	case AC_VERB_GET_POWER_STATE:
508c2ecf20Sopenharmony_ci	case AC_VERB_GET_PIN_SENSE:
518c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_DIP_SIZE:
528c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_ELDD:
538c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_DIP_INDEX:
548c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_DIP_DATA:
558c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_DIP_XMIT:
568c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_CP_CTRL:
578c2ecf20Sopenharmony_ci	case AC_VERB_GET_HDMI_CHAN_SLOT:
588c2ecf20Sopenharmony_ci	case AC_VERB_GET_DEVICE_SEL:
598c2ecf20Sopenharmony_ci	case AC_VERB_GET_DEVICE_LIST:	/* read-only volatile */
608c2ecf20Sopenharmony_ci		return true;
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return false;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic bool hda_writeable_reg(struct device *dev, unsigned int reg)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct hdac_device *codec = dev_to_hdac_dev(dev);
698c2ecf20Sopenharmony_ci	unsigned int verb = get_verb(reg);
708c2ecf20Sopenharmony_ci	const unsigned int *v;
718c2ecf20Sopenharmony_ci	int i;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	snd_array_for_each(&codec->vendor_verbs, i, v) {
748c2ecf20Sopenharmony_ci		if (verb == *v)
758c2ecf20Sopenharmony_ci			return true;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (codec->caps_overwriting)
798c2ecf20Sopenharmony_ci		return true;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	switch (verb & 0xf00) {
828c2ecf20Sopenharmony_ci	case AC_VERB_GET_STREAM_FORMAT:
838c2ecf20Sopenharmony_ci	case AC_VERB_GET_AMP_GAIN_MUTE:
848c2ecf20Sopenharmony_ci		return true;
858c2ecf20Sopenharmony_ci	case AC_VERB_GET_PROC_COEF:
868c2ecf20Sopenharmony_ci		return codec->cache_coef;
878c2ecf20Sopenharmony_ci	case 0xf00:
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	default:
908c2ecf20Sopenharmony_ci		return false;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	switch (verb) {
948c2ecf20Sopenharmony_ci	case AC_VERB_GET_CONNECT_SEL:
958c2ecf20Sopenharmony_ci	case AC_VERB_GET_SDI_SELECT:
968c2ecf20Sopenharmony_ci	case AC_VERB_GET_PIN_WIDGET_CONTROL:
978c2ecf20Sopenharmony_ci	case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */
988c2ecf20Sopenharmony_ci	case AC_VERB_GET_BEEP_CONTROL:
998c2ecf20Sopenharmony_ci	case AC_VERB_GET_EAPD_BTLENABLE:
1008c2ecf20Sopenharmony_ci	case AC_VERB_GET_DIGI_CONVERT_1:
1018c2ecf20Sopenharmony_ci	case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */
1028c2ecf20Sopenharmony_ci	case AC_VERB_GET_VOLUME_KNOB_CONTROL:
1038c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_MASK:
1048c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_DIRECTION:
1058c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_DATA: /* not for volatile read */
1068c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_WAKE_MASK:
1078c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK:
1088c2ecf20Sopenharmony_ci	case AC_VERB_GET_GPIO_STICKY_MASK:
1098c2ecf20Sopenharmony_ci		return true;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return false;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic bool hda_readable_reg(struct device *dev, unsigned int reg)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct hdac_device *codec = dev_to_hdac_dev(dev);
1188c2ecf20Sopenharmony_ci	unsigned int verb = get_verb(reg);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (codec->caps_overwriting)
1218c2ecf20Sopenharmony_ci		return true;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	switch (verb) {
1248c2ecf20Sopenharmony_ci	case AC_VERB_PARAMETERS:
1258c2ecf20Sopenharmony_ci	case AC_VERB_GET_CONNECT_LIST:
1268c2ecf20Sopenharmony_ci	case AC_VERB_GET_SUBSYSTEM_ID:
1278c2ecf20Sopenharmony_ci		return true;
1288c2ecf20Sopenharmony_ci	/* below are basically writable, but disabled for reducing unnecessary
1298c2ecf20Sopenharmony_ci	 * writes at sync
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	case AC_VERB_GET_CONFIG_DEFAULT: /* usually just read */
1328c2ecf20Sopenharmony_ci	case AC_VERB_GET_CONV: /* managed in PCM code */
1338c2ecf20Sopenharmony_ci	case AC_VERB_GET_CVT_CHAN_COUNT: /* managed in HDMI CA code */
1348c2ecf20Sopenharmony_ci		return true;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return hda_writeable_reg(dev, reg);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/*
1418c2ecf20Sopenharmony_ci * Stereo amp pseudo register:
1428c2ecf20Sopenharmony_ci * for making easier to handle the stereo volume control, we provide a
1438c2ecf20Sopenharmony_ci * fake register to deal both left and right channels by a single
1448c2ecf20Sopenharmony_ci * (pseudo) register access.  A verb consisting of SET_AMP_GAIN with
1458c2ecf20Sopenharmony_ci * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit
1468c2ecf20Sopenharmony_ci * for the left and the upper 8bit for the right channel.
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_cistatic bool is_stereo_amp_verb(unsigned int reg)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	if (((reg >> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE)
1518c2ecf20Sopenharmony_ci		return false;
1528c2ecf20Sopenharmony_ci	return (reg & (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT)) ==
1538c2ecf20Sopenharmony_ci		(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/* read a pseudo stereo amp register (16bit left+right) */
1578c2ecf20Sopenharmony_cistatic int hda_reg_read_stereo_amp(struct hdac_device *codec,
1588c2ecf20Sopenharmony_ci				   unsigned int reg, unsigned int *val)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	unsigned int left, right;
1618c2ecf20Sopenharmony_ci	int err;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	reg &= ~(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
1648c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_LEFT, 0, &left);
1658c2ecf20Sopenharmony_ci	if (err < 0)
1668c2ecf20Sopenharmony_ci		return err;
1678c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_RIGHT, 0, &right);
1688c2ecf20Sopenharmony_ci	if (err < 0)
1698c2ecf20Sopenharmony_ci		return err;
1708c2ecf20Sopenharmony_ci	*val = left | (right << 8);
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/* write a pseudo stereo amp register (16bit left+right) */
1758c2ecf20Sopenharmony_cistatic int hda_reg_write_stereo_amp(struct hdac_device *codec,
1768c2ecf20Sopenharmony_ci				    unsigned int reg, unsigned int val)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	int err;
1798c2ecf20Sopenharmony_ci	unsigned int verb, left, right;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	verb = AC_VERB_SET_AMP_GAIN_MUTE << 8;
1828c2ecf20Sopenharmony_ci	if (reg & AC_AMP_GET_OUTPUT)
1838c2ecf20Sopenharmony_ci		verb |= AC_AMP_SET_OUTPUT;
1848c2ecf20Sopenharmony_ci	else
1858c2ecf20Sopenharmony_ci		verb |= AC_AMP_SET_INPUT | ((reg & 0xf) << 8);
1868c2ecf20Sopenharmony_ci	reg = (reg & ~0xfffff) | verb;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	left = val & 0xff;
1898c2ecf20Sopenharmony_ci	right = (val >> 8) & 0xff;
1908c2ecf20Sopenharmony_ci	if (left == right) {
1918c2ecf20Sopenharmony_ci		reg |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
1928c2ecf20Sopenharmony_ci		return snd_hdac_exec_verb(codec, reg | left, 0, NULL);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_LEFT | left, 0, NULL);
1968c2ecf20Sopenharmony_ci	if (err < 0)
1978c2ecf20Sopenharmony_ci		return err;
1988c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_RIGHT | right, 0, NULL);
1998c2ecf20Sopenharmony_ci	if (err < 0)
2008c2ecf20Sopenharmony_ci		return err;
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* read a pseudo coef register (16bit) */
2058c2ecf20Sopenharmony_cistatic int hda_reg_read_coef(struct hdac_device *codec, unsigned int reg,
2068c2ecf20Sopenharmony_ci			     unsigned int *val)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	unsigned int verb;
2098c2ecf20Sopenharmony_ci	int err;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (!codec->cache_coef)
2128c2ecf20Sopenharmony_ci		return -EINVAL;
2138c2ecf20Sopenharmony_ci	/* LSB 8bit = coef index */
2148c2ecf20Sopenharmony_ci	verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
2158c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, verb, 0, NULL);
2168c2ecf20Sopenharmony_ci	if (err < 0)
2178c2ecf20Sopenharmony_ci		return err;
2188c2ecf20Sopenharmony_ci	verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8);
2198c2ecf20Sopenharmony_ci	return snd_hdac_exec_verb(codec, verb, 0, val);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/* write a pseudo coef register (16bit) */
2238c2ecf20Sopenharmony_cistatic int hda_reg_write_coef(struct hdac_device *codec, unsigned int reg,
2248c2ecf20Sopenharmony_ci			      unsigned int val)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	unsigned int verb;
2278c2ecf20Sopenharmony_ci	int err;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (!codec->cache_coef)
2308c2ecf20Sopenharmony_ci		return -EINVAL;
2318c2ecf20Sopenharmony_ci	/* LSB 8bit = coef index */
2328c2ecf20Sopenharmony_ci	verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
2338c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, verb, 0, NULL);
2348c2ecf20Sopenharmony_ci	if (err < 0)
2358c2ecf20Sopenharmony_ci		return err;
2368c2ecf20Sopenharmony_ci	verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8) |
2378c2ecf20Sopenharmony_ci		(val & 0xffff);
2388c2ecf20Sopenharmony_ci	return snd_hdac_exec_verb(codec, verb, 0, NULL);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct hdac_device *codec = context;
2448c2ecf20Sopenharmony_ci	int verb = get_verb(reg);
2458c2ecf20Sopenharmony_ci	int err;
2468c2ecf20Sopenharmony_ci	int pm_lock = 0;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (verb != AC_VERB_GET_POWER_STATE) {
2498c2ecf20Sopenharmony_ci		pm_lock = codec_pm_lock(codec);
2508c2ecf20Sopenharmony_ci		if (pm_lock < 0)
2518c2ecf20Sopenharmony_ci			return -EAGAIN;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci	reg |= (codec->addr << 28);
2548c2ecf20Sopenharmony_ci	if (is_stereo_amp_verb(reg)) {
2558c2ecf20Sopenharmony_ci		err = hda_reg_read_stereo_amp(codec, reg, val);
2568c2ecf20Sopenharmony_ci		goto out;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	if (verb == AC_VERB_GET_PROC_COEF) {
2598c2ecf20Sopenharmony_ci		err = hda_reg_read_coef(codec, reg, val);
2608c2ecf20Sopenharmony_ci		goto out;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci	if ((verb & 0x700) == AC_VERB_SET_AMP_GAIN_MUTE)
2638c2ecf20Sopenharmony_ci		reg &= ~AC_AMP_FAKE_MUTE;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	err = snd_hdac_exec_verb(codec, reg, 0, val);
2668c2ecf20Sopenharmony_ci	if (err < 0)
2678c2ecf20Sopenharmony_ci		goto out;
2688c2ecf20Sopenharmony_ci	/* special handling for asymmetric reads */
2698c2ecf20Sopenharmony_ci	if (verb == AC_VERB_GET_POWER_STATE) {
2708c2ecf20Sopenharmony_ci		if (*val & AC_PWRST_ERROR)
2718c2ecf20Sopenharmony_ci			*val = -1;
2728c2ecf20Sopenharmony_ci		else /* take only the actual state */
2738c2ecf20Sopenharmony_ci			*val = (*val >> 4) & 0x0f;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci out:
2768c2ecf20Sopenharmony_ci	codec_pm_unlock(codec, pm_lock);
2778c2ecf20Sopenharmony_ci	return err;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic int hda_reg_write(void *context, unsigned int reg, unsigned int val)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	struct hdac_device *codec = context;
2838c2ecf20Sopenharmony_ci	unsigned int verb;
2848c2ecf20Sopenharmony_ci	int i, bytes, err;
2858c2ecf20Sopenharmony_ci	int pm_lock = 0;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (codec->caps_overwriting)
2888c2ecf20Sopenharmony_ci		return 0;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	reg &= ~0x00080000U; /* drop GET bit */
2918c2ecf20Sopenharmony_ci	reg |= (codec->addr << 28);
2928c2ecf20Sopenharmony_ci	verb = get_verb(reg);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (verb != AC_VERB_SET_POWER_STATE) {
2958c2ecf20Sopenharmony_ci		pm_lock = codec_pm_lock(codec);
2968c2ecf20Sopenharmony_ci		if (pm_lock < 0)
2978c2ecf20Sopenharmony_ci			return codec->lazy_cache ? 0 : -EAGAIN;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if (is_stereo_amp_verb(reg)) {
3018c2ecf20Sopenharmony_ci		err = hda_reg_write_stereo_amp(codec, reg, val);
3028c2ecf20Sopenharmony_ci		goto out;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (verb == AC_VERB_SET_PROC_COEF) {
3068c2ecf20Sopenharmony_ci		err = hda_reg_write_coef(codec, reg, val);
3078c2ecf20Sopenharmony_ci		goto out;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	switch (verb & 0xf00) {
3118c2ecf20Sopenharmony_ci	case AC_VERB_SET_AMP_GAIN_MUTE:
3128c2ecf20Sopenharmony_ci		if ((reg & AC_AMP_FAKE_MUTE) && (val & AC_AMP_MUTE))
3138c2ecf20Sopenharmony_ci			val = 0;
3148c2ecf20Sopenharmony_ci		verb = AC_VERB_SET_AMP_GAIN_MUTE;
3158c2ecf20Sopenharmony_ci		if (reg & AC_AMP_GET_LEFT)
3168c2ecf20Sopenharmony_ci			verb |= AC_AMP_SET_LEFT >> 8;
3178c2ecf20Sopenharmony_ci		else
3188c2ecf20Sopenharmony_ci			verb |= AC_AMP_SET_RIGHT >> 8;
3198c2ecf20Sopenharmony_ci		if (reg & AC_AMP_GET_OUTPUT) {
3208c2ecf20Sopenharmony_ci			verb |= AC_AMP_SET_OUTPUT >> 8;
3218c2ecf20Sopenharmony_ci		} else {
3228c2ecf20Sopenharmony_ci			verb |= AC_AMP_SET_INPUT >> 8;
3238c2ecf20Sopenharmony_ci			verb |= reg & 0xf;
3248c2ecf20Sopenharmony_ci		}
3258c2ecf20Sopenharmony_ci		break;
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	switch (verb) {
3298c2ecf20Sopenharmony_ci	case AC_VERB_SET_DIGI_CONVERT_1:
3308c2ecf20Sopenharmony_ci		bytes = 2;
3318c2ecf20Sopenharmony_ci		break;
3328c2ecf20Sopenharmony_ci	case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
3338c2ecf20Sopenharmony_ci		bytes = 4;
3348c2ecf20Sopenharmony_ci		break;
3358c2ecf20Sopenharmony_ci	default:
3368c2ecf20Sopenharmony_ci		bytes = 1;
3378c2ecf20Sopenharmony_ci		break;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i++) {
3418c2ecf20Sopenharmony_ci		reg &= ~0xfffff;
3428c2ecf20Sopenharmony_ci		reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
3438c2ecf20Sopenharmony_ci		err = snd_hdac_exec_verb(codec, reg, 0, NULL);
3448c2ecf20Sopenharmony_ci		if (err < 0)
3458c2ecf20Sopenharmony_ci			goto out;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci out:
3498c2ecf20Sopenharmony_ci	codec_pm_unlock(codec, pm_lock);
3508c2ecf20Sopenharmony_ci	return err;
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic const struct regmap_config hda_regmap_cfg = {
3548c2ecf20Sopenharmony_ci	.name = "hdaudio",
3558c2ecf20Sopenharmony_ci	.reg_bits = 32,
3568c2ecf20Sopenharmony_ci	.val_bits = 32,
3578c2ecf20Sopenharmony_ci	.max_register = 0xfffffff,
3588c2ecf20Sopenharmony_ci	.writeable_reg = hda_writeable_reg,
3598c2ecf20Sopenharmony_ci	.readable_reg = hda_readable_reg,
3608c2ecf20Sopenharmony_ci	.volatile_reg = hda_volatile_reg,
3618c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
3628c2ecf20Sopenharmony_ci	.reg_read = hda_reg_read,
3638c2ecf20Sopenharmony_ci	.reg_write = hda_reg_write,
3648c2ecf20Sopenharmony_ci	.use_single_read = true,
3658c2ecf20Sopenharmony_ci	.use_single_write = true,
3668c2ecf20Sopenharmony_ci	.disable_locking = true,
3678c2ecf20Sopenharmony_ci};
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/**
3708c2ecf20Sopenharmony_ci * snd_hdac_regmap_init - Initialize regmap for HDA register accesses
3718c2ecf20Sopenharmony_ci * @codec: the codec object
3728c2ecf20Sopenharmony_ci *
3738c2ecf20Sopenharmony_ci * Returns zero for success or a negative error code.
3748c2ecf20Sopenharmony_ci */
3758c2ecf20Sopenharmony_ciint snd_hdac_regmap_init(struct hdac_device *codec)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	struct regmap *regmap;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
3808c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
3818c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
3828c2ecf20Sopenharmony_ci	codec->regmap = regmap;
3838c2ecf20Sopenharmony_ci	snd_array_init(&codec->vendor_verbs, sizeof(unsigned int), 8);
3848c2ecf20Sopenharmony_ci	return 0;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/**
3898c2ecf20Sopenharmony_ci * snd_hdac_regmap_init - Release the regmap from HDA codec
3908c2ecf20Sopenharmony_ci * @codec: the codec object
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_civoid snd_hdac_regmap_exit(struct hdac_device *codec)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	if (codec->regmap) {
3958c2ecf20Sopenharmony_ci		regmap_exit(codec->regmap);
3968c2ecf20Sopenharmony_ci		codec->regmap = NULL;
3978c2ecf20Sopenharmony_ci		snd_array_free(&codec->vendor_verbs);
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/**
4038c2ecf20Sopenharmony_ci * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
4048c2ecf20Sopenharmony_ci * @codec: the codec object
4058c2ecf20Sopenharmony_ci * @verb: verb to allow accessing via regmap
4068c2ecf20Sopenharmony_ci *
4078c2ecf20Sopenharmony_ci * Returns zero for success or a negative error code.
4088c2ecf20Sopenharmony_ci */
4098c2ecf20Sopenharmony_ciint snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
4108c2ecf20Sopenharmony_ci				    unsigned int verb)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	unsigned int *p = snd_array_new(&codec->vendor_verbs);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	if (!p)
4158c2ecf20Sopenharmony_ci		return -ENOMEM;
4168c2ecf20Sopenharmony_ci	*p = verb | 0x800; /* set GET bit */
4178c2ecf20Sopenharmony_ci	return 0;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci/*
4228c2ecf20Sopenharmony_ci * helper functions
4238c2ecf20Sopenharmony_ci */
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci/* write a pseudo-register value (w/o power sequence) */
4268c2ecf20Sopenharmony_cistatic int reg_raw_write(struct hdac_device *codec, unsigned int reg,
4278c2ecf20Sopenharmony_ci			 unsigned int val)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	int err;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	mutex_lock(&codec->regmap_lock);
4328c2ecf20Sopenharmony_ci	if (!codec->regmap)
4338c2ecf20Sopenharmony_ci		err = hda_reg_write(codec, reg, val);
4348c2ecf20Sopenharmony_ci	else
4358c2ecf20Sopenharmony_ci		err = regmap_write(codec->regmap, reg, val);
4368c2ecf20Sopenharmony_ci	mutex_unlock(&codec->regmap_lock);
4378c2ecf20Sopenharmony_ci	return err;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* a helper macro to call @func_call; retry with power-up if failed */
4418c2ecf20Sopenharmony_ci#define CALL_RAW_FUNC(codec, func_call)				\
4428c2ecf20Sopenharmony_ci	({							\
4438c2ecf20Sopenharmony_ci		int _err = func_call;				\
4448c2ecf20Sopenharmony_ci		if (_err == -EAGAIN) {				\
4458c2ecf20Sopenharmony_ci			_err = snd_hdac_power_up_pm(codec);	\
4468c2ecf20Sopenharmony_ci			if (_err >= 0)				\
4478c2ecf20Sopenharmony_ci				_err = func_call;		\
4488c2ecf20Sopenharmony_ci			snd_hdac_power_down_pm(codec);		\
4498c2ecf20Sopenharmony_ci		}						\
4508c2ecf20Sopenharmony_ci		_err;})
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci/**
4538c2ecf20Sopenharmony_ci * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
4548c2ecf20Sopenharmony_ci * @codec: the codec object
4558c2ecf20Sopenharmony_ci * @reg: pseudo register
4568c2ecf20Sopenharmony_ci * @val: value to write
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci * Returns zero if successful or a negative error code.
4598c2ecf20Sopenharmony_ci */
4608c2ecf20Sopenharmony_ciint snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
4618c2ecf20Sopenharmony_ci			      unsigned int val)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return CALL_RAW_FUNC(codec, reg_raw_write(codec, reg, val));
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic int reg_raw_read(struct hdac_device *codec, unsigned int reg,
4688c2ecf20Sopenharmony_ci			unsigned int *val, bool uncached)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	int err;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	mutex_lock(&codec->regmap_lock);
4738c2ecf20Sopenharmony_ci	if (uncached || !codec->regmap)
4748c2ecf20Sopenharmony_ci		err = hda_reg_read(codec, reg, val);
4758c2ecf20Sopenharmony_ci	else
4768c2ecf20Sopenharmony_ci		err = regmap_read(codec->regmap, reg, val);
4778c2ecf20Sopenharmony_ci	mutex_unlock(&codec->regmap_lock);
4788c2ecf20Sopenharmony_ci	return err;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int __snd_hdac_regmap_read_raw(struct hdac_device *codec,
4828c2ecf20Sopenharmony_ci				      unsigned int reg, unsigned int *val,
4838c2ecf20Sopenharmony_ci				      bool uncached)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	return CALL_RAW_FUNC(codec, reg_raw_read(codec, reg, val, uncached));
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/**
4898c2ecf20Sopenharmony_ci * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
4908c2ecf20Sopenharmony_ci * @codec: the codec object
4918c2ecf20Sopenharmony_ci * @reg: pseudo register
4928c2ecf20Sopenharmony_ci * @val: pointer to store the read value
4938c2ecf20Sopenharmony_ci *
4948c2ecf20Sopenharmony_ci * Returns zero if successful or a negative error code.
4958c2ecf20Sopenharmony_ci */
4968c2ecf20Sopenharmony_ciint snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
4978c2ecf20Sopenharmony_ci			     unsigned int *val)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	return __snd_hdac_regmap_read_raw(codec, reg, val, false);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/* Works like snd_hdac_regmap_read_raw(), but this doesn't read from the
5048c2ecf20Sopenharmony_ci * cache but always via hda verbs.
5058c2ecf20Sopenharmony_ci */
5068c2ecf20Sopenharmony_ciint snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec,
5078c2ecf20Sopenharmony_ci				      unsigned int reg, unsigned int *val)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	return __snd_hdac_regmap_read_raw(codec, reg, val, true);
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic int reg_raw_update(struct hdac_device *codec, unsigned int reg,
5138c2ecf20Sopenharmony_ci			  unsigned int mask, unsigned int val)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	unsigned int orig;
5168c2ecf20Sopenharmony_ci	bool change;
5178c2ecf20Sopenharmony_ci	int err;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	mutex_lock(&codec->regmap_lock);
5208c2ecf20Sopenharmony_ci	if (codec->regmap) {
5218c2ecf20Sopenharmony_ci		err = regmap_update_bits_check(codec->regmap, reg, mask, val,
5228c2ecf20Sopenharmony_ci					       &change);
5238c2ecf20Sopenharmony_ci		if (!err)
5248c2ecf20Sopenharmony_ci			err = change ? 1 : 0;
5258c2ecf20Sopenharmony_ci	} else {
5268c2ecf20Sopenharmony_ci		err = hda_reg_read(codec, reg, &orig);
5278c2ecf20Sopenharmony_ci		if (!err) {
5288c2ecf20Sopenharmony_ci			val &= mask;
5298c2ecf20Sopenharmony_ci			val |= orig & ~mask;
5308c2ecf20Sopenharmony_ci			if (val != orig) {
5318c2ecf20Sopenharmony_ci				err = hda_reg_write(codec, reg, val);
5328c2ecf20Sopenharmony_ci				if (!err)
5338c2ecf20Sopenharmony_ci					err = 1;
5348c2ecf20Sopenharmony_ci			}
5358c2ecf20Sopenharmony_ci		}
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	mutex_unlock(&codec->regmap_lock);
5388c2ecf20Sopenharmony_ci	return err;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci/**
5428c2ecf20Sopenharmony_ci * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
5438c2ecf20Sopenharmony_ci * @codec: the codec object
5448c2ecf20Sopenharmony_ci * @reg: pseudo register
5458c2ecf20Sopenharmony_ci * @mask: bit mask to update
5468c2ecf20Sopenharmony_ci * @val: value to update
5478c2ecf20Sopenharmony_ci *
5488c2ecf20Sopenharmony_ci * Returns zero if successful or a negative error code.
5498c2ecf20Sopenharmony_ci */
5508c2ecf20Sopenharmony_ciint snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
5518c2ecf20Sopenharmony_ci			       unsigned int mask, unsigned int val)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	return CALL_RAW_FUNC(codec, reg_raw_update(codec, reg, mask, val));
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic int reg_raw_update_once(struct hdac_device *codec, unsigned int reg,
5588c2ecf20Sopenharmony_ci			       unsigned int mask, unsigned int val)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	unsigned int orig;
5618c2ecf20Sopenharmony_ci	int err;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (!codec->regmap)
5648c2ecf20Sopenharmony_ci		return reg_raw_update(codec, reg, mask, val);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	mutex_lock(&codec->regmap_lock);
5678c2ecf20Sopenharmony_ci	regcache_cache_only(codec->regmap, true);
5688c2ecf20Sopenharmony_ci	err = regmap_read(codec->regmap, reg, &orig);
5698c2ecf20Sopenharmony_ci	regcache_cache_only(codec->regmap, false);
5708c2ecf20Sopenharmony_ci	if (err < 0)
5718c2ecf20Sopenharmony_ci		err = regmap_update_bits(codec->regmap, reg, mask, val);
5728c2ecf20Sopenharmony_ci	mutex_unlock(&codec->regmap_lock);
5738c2ecf20Sopenharmony_ci	return err;
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci/**
5778c2ecf20Sopenharmony_ci * snd_hdac_regmap_update_raw_once - initialize the register value only once
5788c2ecf20Sopenharmony_ci * @codec: the codec object
5798c2ecf20Sopenharmony_ci * @reg: pseudo register
5808c2ecf20Sopenharmony_ci * @mask: bit mask to update
5818c2ecf20Sopenharmony_ci * @val: value to update
5828c2ecf20Sopenharmony_ci *
5838c2ecf20Sopenharmony_ci * Performs the update of the register bits only once when the register
5848c2ecf20Sopenharmony_ci * hasn't been initialized yet.  Used in HD-audio legacy driver.
5858c2ecf20Sopenharmony_ci * Returns zero if successful or a negative error code
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_ciint snd_hdac_regmap_update_raw_once(struct hdac_device *codec, unsigned int reg,
5888c2ecf20Sopenharmony_ci				    unsigned int mask, unsigned int val)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	return CALL_RAW_FUNC(codec, reg_raw_update_once(codec, reg, mask, val));
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw_once);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci/**
5958c2ecf20Sopenharmony_ci * snd_hdac_regmap_sync - sync out the cached values for PM resume
5968c2ecf20Sopenharmony_ci * @codec: the codec object
5978c2ecf20Sopenharmony_ci */
5988c2ecf20Sopenharmony_civoid snd_hdac_regmap_sync(struct hdac_device *codec)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	mutex_lock(&codec->regmap_lock);
6018c2ecf20Sopenharmony_ci	if (codec->regmap)
6028c2ecf20Sopenharmony_ci		regcache_sync(codec->regmap);
6038c2ecf20Sopenharmony_ci	mutex_unlock(&codec->regmap_lock);
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(snd_hdac_regmap_sync);
606