18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
48c2ecf20Sopenharmony_ci *  Universal routines for AK4531 codec
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/mutex.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <sound/core.h>
148c2ecf20Sopenharmony_ci#include <sound/ak4531_codec.h>
158c2ecf20Sopenharmony_ci#include <sound/tlv.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Universal routines for AK4531 codec");
208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
218c2ecf20Sopenharmony_ci*/
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic void snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#if 0
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void snd_ak4531_dump(struct snd_ak4531 *ak4531)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	int idx;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	for (idx = 0; idx < 0x19; idx++)
368c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "ak4531 0x%x: 0x%x\n",
378c2ecf20Sopenharmony_ci		       idx, ak4531->regs[idx]);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#endif
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define AK4531_SINGLE(xname, xindex, reg, shift, mask, invert) \
478c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
488c2ecf20Sopenharmony_ci  .info = snd_ak4531_info_single, \
498c2ecf20Sopenharmony_ci  .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
508c2ecf20Sopenharmony_ci  .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22) }
518c2ecf20Sopenharmony_ci#define AK4531_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv)    \
528c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
538c2ecf20Sopenharmony_ci  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
548c2ecf20Sopenharmony_ci  .name = xname, .index = xindex, \
558c2ecf20Sopenharmony_ci  .info = snd_ak4531_info_single, \
568c2ecf20Sopenharmony_ci  .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
578c2ecf20Sopenharmony_ci  .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22), \
588c2ecf20Sopenharmony_ci  .tlv = { .p = (xtlv) } }
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int snd_ak4531_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
658c2ecf20Sopenharmony_ci	uinfo->count = 1;
668c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
678c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int snd_ak4531_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
748c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
758c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 16) & 0x07;
768c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
778c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
788c2ecf20Sopenharmony_ci	int val;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
818c2ecf20Sopenharmony_ci	val = (ak4531->regs[reg] >> shift) & mask;
828c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
838c2ecf20Sopenharmony_ci	if (invert) {
848c2ecf20Sopenharmony_ci		val = mask - val;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = val;
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int snd_ak4531_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
938c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
948c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 16) & 0x07;
958c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
968c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
978c2ecf20Sopenharmony_ci	int change;
988c2ecf20Sopenharmony_ci	int val;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	val = ucontrol->value.integer.value[0] & mask;
1018c2ecf20Sopenharmony_ci	if (invert) {
1028c2ecf20Sopenharmony_ci		val = mask - val;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci	val <<= shift;
1058c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
1068c2ecf20Sopenharmony_ci	val = (ak4531->regs[reg] & ~(mask << shift)) | val;
1078c2ecf20Sopenharmony_ci	change = val != ak4531->regs[reg];
1088c2ecf20Sopenharmony_ci	ak4531->write(ak4531, reg, ak4531->regs[reg] = val);
1098c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
1108c2ecf20Sopenharmony_ci	return change;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define AK4531_DOUBLE(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert) \
1148c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1158c2ecf20Sopenharmony_ci  .info = snd_ak4531_info_double, \
1168c2ecf20Sopenharmony_ci  .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
1178c2ecf20Sopenharmony_ci  .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22) }
1188c2ecf20Sopenharmony_ci#define AK4531_DOUBLE_TLV(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert, xtlv) \
1198c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1208c2ecf20Sopenharmony_ci  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1218c2ecf20Sopenharmony_ci  .name = xname, .index = xindex, \
1228c2ecf20Sopenharmony_ci  .info = snd_ak4531_info_double, \
1238c2ecf20Sopenharmony_ci  .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
1248c2ecf20Sopenharmony_ci  .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22), \
1258c2ecf20Sopenharmony_ci  .tlv = { .p = (xtlv) } }
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int snd_ak4531_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1328c2ecf20Sopenharmony_ci	uinfo->count = 2;
1338c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
1348c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int snd_ak4531_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
1418c2ecf20Sopenharmony_ci	int left_reg = kcontrol->private_value & 0xff;
1428c2ecf20Sopenharmony_ci	int right_reg = (kcontrol->private_value >> 8) & 0xff;
1438c2ecf20Sopenharmony_ci	int left_shift = (kcontrol->private_value >> 16) & 0x07;
1448c2ecf20Sopenharmony_ci	int right_shift = (kcontrol->private_value >> 19) & 0x07;
1458c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
1468c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
1478c2ecf20Sopenharmony_ci	int left, right;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
1508c2ecf20Sopenharmony_ci	left = (ak4531->regs[left_reg] >> left_shift) & mask;
1518c2ecf20Sopenharmony_ci	right = (ak4531->regs[right_reg] >> right_shift) & mask;
1528c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
1538c2ecf20Sopenharmony_ci	if (invert) {
1548c2ecf20Sopenharmony_ci		left = mask - left;
1558c2ecf20Sopenharmony_ci		right = mask - right;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = left;
1588c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[1] = right;
1598c2ecf20Sopenharmony_ci	return 0;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic int snd_ak4531_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
1658c2ecf20Sopenharmony_ci	int left_reg = kcontrol->private_value & 0xff;
1668c2ecf20Sopenharmony_ci	int right_reg = (kcontrol->private_value >> 8) & 0xff;
1678c2ecf20Sopenharmony_ci	int left_shift = (kcontrol->private_value >> 16) & 0x07;
1688c2ecf20Sopenharmony_ci	int right_shift = (kcontrol->private_value >> 19) & 0x07;
1698c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
1708c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
1718c2ecf20Sopenharmony_ci	int change;
1728c2ecf20Sopenharmony_ci	int left, right;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	left = ucontrol->value.integer.value[0] & mask;
1758c2ecf20Sopenharmony_ci	right = ucontrol->value.integer.value[1] & mask;
1768c2ecf20Sopenharmony_ci	if (invert) {
1778c2ecf20Sopenharmony_ci		left = mask - left;
1788c2ecf20Sopenharmony_ci		right = mask - right;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci	left <<= left_shift;
1818c2ecf20Sopenharmony_ci	right <<= right_shift;
1828c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
1838c2ecf20Sopenharmony_ci	if (left_reg == right_reg) {
1848c2ecf20Sopenharmony_ci		left = (ak4531->regs[left_reg] & ~((mask << left_shift) | (mask << right_shift))) | left | right;
1858c2ecf20Sopenharmony_ci		change = left != ak4531->regs[left_reg];
1868c2ecf20Sopenharmony_ci		ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
1878c2ecf20Sopenharmony_ci	} else {
1888c2ecf20Sopenharmony_ci		left = (ak4531->regs[left_reg] & ~(mask << left_shift)) | left;
1898c2ecf20Sopenharmony_ci		right = (ak4531->regs[right_reg] & ~(mask << right_shift)) | right;
1908c2ecf20Sopenharmony_ci		change = left != ak4531->regs[left_reg] || right != ak4531->regs[right_reg];
1918c2ecf20Sopenharmony_ci		ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
1928c2ecf20Sopenharmony_ci		ak4531->write(ak4531, right_reg, ak4531->regs[right_reg] = right);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
1958c2ecf20Sopenharmony_ci	return change;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define AK4531_INPUT_SW(xname, xindex, reg1, reg2, left_shift, right_shift) \
1998c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
2008c2ecf20Sopenharmony_ci  .info = snd_ak4531_info_input_sw, \
2018c2ecf20Sopenharmony_ci  .get = snd_ak4531_get_input_sw, .put = snd_ak4531_put_input_sw, \
2028c2ecf20Sopenharmony_ci  .private_value = reg1 | (reg2 << 8) | (left_shift << 16) | (right_shift << 24) }
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic int snd_ak4531_info_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2078c2ecf20Sopenharmony_ci	uinfo->count = 4;
2088c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
2098c2ecf20Sopenharmony_ci	uinfo->value.integer.max = 1;
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int snd_ak4531_get_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
2168c2ecf20Sopenharmony_ci	int reg1 = kcontrol->private_value & 0xff;
2178c2ecf20Sopenharmony_ci	int reg2 = (kcontrol->private_value >> 8) & 0xff;
2188c2ecf20Sopenharmony_ci	int left_shift = (kcontrol->private_value >> 16) & 0x0f;
2198c2ecf20Sopenharmony_ci	int right_shift = (kcontrol->private_value >> 24) & 0x0f;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
2228c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (ak4531->regs[reg1] >> left_shift) & 1;
2238c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[1] = (ak4531->regs[reg2] >> left_shift) & 1;
2248c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[2] = (ak4531->regs[reg1] >> right_shift) & 1;
2258c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[3] = (ak4531->regs[reg2] >> right_shift) & 1;
2268c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
2278c2ecf20Sopenharmony_ci	return 0;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic int snd_ak4531_put_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
2338c2ecf20Sopenharmony_ci	int reg1 = kcontrol->private_value & 0xff;
2348c2ecf20Sopenharmony_ci	int reg2 = (kcontrol->private_value >> 8) & 0xff;
2358c2ecf20Sopenharmony_ci	int left_shift = (kcontrol->private_value >> 16) & 0x0f;
2368c2ecf20Sopenharmony_ci	int right_shift = (kcontrol->private_value >> 24) & 0x0f;
2378c2ecf20Sopenharmony_ci	int change;
2388c2ecf20Sopenharmony_ci	int val1, val2;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	mutex_lock(&ak4531->reg_mutex);
2418c2ecf20Sopenharmony_ci	val1 = ak4531->regs[reg1] & ~((1 << left_shift) | (1 << right_shift));
2428c2ecf20Sopenharmony_ci	val2 = ak4531->regs[reg2] & ~((1 << left_shift) | (1 << right_shift));
2438c2ecf20Sopenharmony_ci	val1 |= (ucontrol->value.integer.value[0] & 1) << left_shift;
2448c2ecf20Sopenharmony_ci	val2 |= (ucontrol->value.integer.value[1] & 1) << left_shift;
2458c2ecf20Sopenharmony_ci	val1 |= (ucontrol->value.integer.value[2] & 1) << right_shift;
2468c2ecf20Sopenharmony_ci	val2 |= (ucontrol->value.integer.value[3] & 1) << right_shift;
2478c2ecf20Sopenharmony_ci	change = val1 != ak4531->regs[reg1] || val2 != ak4531->regs[reg2];
2488c2ecf20Sopenharmony_ci	ak4531->write(ak4531, reg1, ak4531->regs[reg1] = val1);
2498c2ecf20Sopenharmony_ci	ak4531->write(ak4531, reg2, ak4531->regs[reg2] = val2);
2508c2ecf20Sopenharmony_ci	mutex_unlock(&ak4531->reg_mutex);
2518c2ecf20Sopenharmony_ci	return change;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_master, -6200, 200, 0);
2558c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_mono, -2800, 400, 0);
2568c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_input, -5000, 200, 0);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_ak4531_controls[] = {
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("Master Playback Switch", 0,
2618c2ecf20Sopenharmony_ci		  AK4531_LMASTER, AK4531_RMASTER, 7, 7, 1, 1,
2628c2ecf20Sopenharmony_ci		  db_scale_master),
2638c2ecf20Sopenharmony_ciAK4531_DOUBLE("Master Playback Volume", 0, AK4531_LMASTER, AK4531_RMASTER, 0, 0, 0x1f, 1),
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ciAK4531_SINGLE_TLV("Master Mono Playback Switch", 0, AK4531_MONO_OUT, 7, 1, 1,
2668c2ecf20Sopenharmony_ci		  db_scale_mono),
2678c2ecf20Sopenharmony_ciAK4531_SINGLE("Master Mono Playback Volume", 0, AK4531_MONO_OUT, 0, 0x07, 1),
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ciAK4531_DOUBLE("PCM Switch", 0, AK4531_LVOICE, AK4531_RVOICE, 7, 7, 1, 1),
2708c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("PCM Volume", 0, AK4531_LVOICE, AK4531_RVOICE, 0, 0, 0x1f, 1,
2718c2ecf20Sopenharmony_ci		  db_scale_input),
2728c2ecf20Sopenharmony_ciAK4531_DOUBLE("PCM Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 3, 2, 1, 0),
2738c2ecf20Sopenharmony_ciAK4531_DOUBLE("PCM Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 2, 2, 1, 0),
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ciAK4531_DOUBLE("PCM Switch", 1, AK4531_LFM, AK4531_RFM, 7, 7, 1, 1),
2768c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("PCM Volume", 1, AK4531_LFM, AK4531_RFM, 0, 0, 0x1f, 1,
2778c2ecf20Sopenharmony_ci		  db_scale_input),
2788c2ecf20Sopenharmony_ciAK4531_DOUBLE("PCM Playback Switch", 1, AK4531_OUT_SW1, AK4531_OUT_SW1, 6, 5, 1, 0),
2798c2ecf20Sopenharmony_ciAK4531_INPUT_SW("PCM Capture Route", 1, AK4531_LIN_SW1, AK4531_RIN_SW1, 6, 5),
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ciAK4531_DOUBLE("CD Switch", 0, AK4531_LCD, AK4531_RCD, 7, 7, 1, 1),
2828c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("CD Volume", 0, AK4531_LCD, AK4531_RCD, 0, 0, 0x1f, 1,
2838c2ecf20Sopenharmony_ci		  db_scale_input),
2848c2ecf20Sopenharmony_ciAK4531_DOUBLE("CD Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 2, 1, 1, 0),
2858c2ecf20Sopenharmony_ciAK4531_INPUT_SW("CD Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 2, 1),
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciAK4531_DOUBLE("Line Switch", 0, AK4531_LLINE, AK4531_RLINE, 7, 7, 1, 1),
2888c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("Line Volume", 0, AK4531_LLINE, AK4531_RLINE, 0, 0, 0x1f, 1,
2898c2ecf20Sopenharmony_ci		  db_scale_input),
2908c2ecf20Sopenharmony_ciAK4531_DOUBLE("Line Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 4, 3, 1, 0),
2918c2ecf20Sopenharmony_ciAK4531_INPUT_SW("Line Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 4, 3),
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciAK4531_DOUBLE("Aux Switch", 0, AK4531_LAUXA, AK4531_RAUXA, 7, 7, 1, 1),
2948c2ecf20Sopenharmony_ciAK4531_DOUBLE_TLV("Aux Volume", 0, AK4531_LAUXA, AK4531_RAUXA, 0, 0, 0x1f, 1,
2958c2ecf20Sopenharmony_ci		  db_scale_input),
2968c2ecf20Sopenharmony_ciAK4531_DOUBLE("Aux Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 5, 4, 1, 0),
2978c2ecf20Sopenharmony_ciAK4531_INPUT_SW("Aux Capture Route", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 4, 3),
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ciAK4531_SINGLE("Mono Switch", 0, AK4531_MONO1, 7, 1, 1),
3008c2ecf20Sopenharmony_ciAK4531_SINGLE_TLV("Mono Volume", 0, AK4531_MONO1, 0, 0x1f, 1, db_scale_input),
3018c2ecf20Sopenharmony_ciAK4531_SINGLE("Mono Playback Switch", 0, AK4531_OUT_SW2, 0, 1, 0),
3028c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mono Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 0, 0, 1, 0),
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciAK4531_SINGLE("Mono Switch", 1, AK4531_MONO2, 7, 1, 1),
3058c2ecf20Sopenharmony_ciAK4531_SINGLE_TLV("Mono Volume", 1, AK4531_MONO2, 0, 0x1f, 1, db_scale_input),
3068c2ecf20Sopenharmony_ciAK4531_SINGLE("Mono Playback Switch", 1, AK4531_OUT_SW2, 1, 1, 0),
3078c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mono Capture Switch", 1, AK4531_LIN_SW2, AK4531_RIN_SW2, 1, 1, 1, 0),
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ciAK4531_SINGLE_TLV("Mic Volume", 0, AK4531_MIC, 0, 0x1f, 1, db_scale_input),
3108c2ecf20Sopenharmony_ciAK4531_SINGLE("Mic Switch", 0, AK4531_MIC, 7, 1, 1),
3118c2ecf20Sopenharmony_ciAK4531_SINGLE("Mic Playback Switch", 0, AK4531_OUT_SW1, 0, 1, 0),
3128c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mic Capture Switch", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 0, 0, 1, 0),
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mic Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 7, 7, 1, 0),
3158c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mono1 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 6, 6, 1, 0),
3168c2ecf20Sopenharmony_ciAK4531_DOUBLE("Mono2 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 5, 5, 1, 0),
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ciAK4531_SINGLE("AD Input Select", 0, AK4531_AD_IN, 0, 1, 0),
3198c2ecf20Sopenharmony_ciAK4531_SINGLE("Mic Boost (+30dB)", 0, AK4531_MIC_GAIN, 0, 1, 0)
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic int snd_ak4531_free(struct snd_ak4531 *ak4531)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	if (ak4531) {
3258c2ecf20Sopenharmony_ci		if (ak4531->private_free)
3268c2ecf20Sopenharmony_ci			ak4531->private_free(ak4531);
3278c2ecf20Sopenharmony_ci		kfree(ak4531);
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci	return 0;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic int snd_ak4531_dev_free(struct snd_device *device)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = device->device_data;
3358c2ecf20Sopenharmony_ci	return snd_ak4531_free(ak4531);
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic const u8 snd_ak4531_initial_map[0x19 + 1] = {
3398c2ecf20Sopenharmony_ci	0x9f,		/* 00: Master Volume Lch */
3408c2ecf20Sopenharmony_ci	0x9f,		/* 01: Master Volume Rch */
3418c2ecf20Sopenharmony_ci	0x9f,		/* 02: Voice Volume Lch */
3428c2ecf20Sopenharmony_ci	0x9f,		/* 03: Voice Volume Rch */
3438c2ecf20Sopenharmony_ci	0x9f,		/* 04: FM Volume Lch */
3448c2ecf20Sopenharmony_ci	0x9f,		/* 05: FM Volume Rch */
3458c2ecf20Sopenharmony_ci	0x9f,		/* 06: CD Audio Volume Lch */
3468c2ecf20Sopenharmony_ci	0x9f,		/* 07: CD Audio Volume Rch */
3478c2ecf20Sopenharmony_ci	0x9f,		/* 08: Line Volume Lch */
3488c2ecf20Sopenharmony_ci	0x9f,		/* 09: Line Volume Rch */
3498c2ecf20Sopenharmony_ci	0x9f,		/* 0a: Aux Volume Lch */
3508c2ecf20Sopenharmony_ci	0x9f,		/* 0b: Aux Volume Rch */
3518c2ecf20Sopenharmony_ci	0x9f,		/* 0c: Mono1 Volume */
3528c2ecf20Sopenharmony_ci	0x9f,		/* 0d: Mono2 Volume */
3538c2ecf20Sopenharmony_ci	0x9f,		/* 0e: Mic Volume */
3548c2ecf20Sopenharmony_ci	0x87,		/* 0f: Mono-out Volume */
3558c2ecf20Sopenharmony_ci	0x00,		/* 10: Output Mixer SW1 */
3568c2ecf20Sopenharmony_ci	0x00,		/* 11: Output Mixer SW2 */
3578c2ecf20Sopenharmony_ci	0x00,		/* 12: Lch Input Mixer SW1 */
3588c2ecf20Sopenharmony_ci	0x00,		/* 13: Rch Input Mixer SW1 */
3598c2ecf20Sopenharmony_ci	0x00,		/* 14: Lch Input Mixer SW2 */
3608c2ecf20Sopenharmony_ci	0x00,		/* 15: Rch Input Mixer SW2 */
3618c2ecf20Sopenharmony_ci	0x00,		/* 16: Reset & Power Down */
3628c2ecf20Sopenharmony_ci	0x00,		/* 17: Clock Select */
3638c2ecf20Sopenharmony_ci	0x00,		/* 18: AD Input Select */
3648c2ecf20Sopenharmony_ci	0x01		/* 19: Mic Amp Setup */
3658c2ecf20Sopenharmony_ci};
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ciint snd_ak4531_mixer(struct snd_card *card,
3688c2ecf20Sopenharmony_ci		     struct snd_ak4531 *_ak4531,
3698c2ecf20Sopenharmony_ci		     struct snd_ak4531 **rak4531)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	unsigned int idx;
3728c2ecf20Sopenharmony_ci	int err;
3738c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531;
3748c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
3758c2ecf20Sopenharmony_ci		.dev_free =	snd_ak4531_dev_free,
3768c2ecf20Sopenharmony_ci	};
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!card || !_ak4531))
3798c2ecf20Sopenharmony_ci		return -EINVAL;
3808c2ecf20Sopenharmony_ci	if (rak4531)
3818c2ecf20Sopenharmony_ci		*rak4531 = NULL;
3828c2ecf20Sopenharmony_ci	ak4531 = kzalloc(sizeof(*ak4531), GFP_KERNEL);
3838c2ecf20Sopenharmony_ci	if (ak4531 == NULL)
3848c2ecf20Sopenharmony_ci		return -ENOMEM;
3858c2ecf20Sopenharmony_ci	*ak4531 = *_ak4531;
3868c2ecf20Sopenharmony_ci	mutex_init(&ak4531->reg_mutex);
3878c2ecf20Sopenharmony_ci	if ((err = snd_component_add(card, "AK4531")) < 0) {
3888c2ecf20Sopenharmony_ci		snd_ak4531_free(ak4531);
3898c2ecf20Sopenharmony_ci		return err;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci	strcpy(card->mixername, "Asahi Kasei AK4531");
3928c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_RESET, 0x03);	/* no RST, PD */
3938c2ecf20Sopenharmony_ci	udelay(100);
3948c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_CLOCK, 0x00);	/* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off LRCLK2 PLL */
3958c2ecf20Sopenharmony_ci	for (idx = 0; idx <= 0x19; idx++) {
3968c2ecf20Sopenharmony_ci		if (idx == AK4531_RESET || idx == AK4531_CLOCK)
3978c2ecf20Sopenharmony_ci			continue;
3988c2ecf20Sopenharmony_ci		ak4531->write(ak4531, idx, ak4531->regs[idx] = snd_ak4531_initial_map[idx]);	/* recording source is mixer */
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci	for (idx = 0; idx < ARRAY_SIZE(snd_ak4531_controls); idx++) {
4018c2ecf20Sopenharmony_ci		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ak4531_controls[idx], ak4531))) < 0) {
4028c2ecf20Sopenharmony_ci			snd_ak4531_free(ak4531);
4038c2ecf20Sopenharmony_ci			return err;
4048c2ecf20Sopenharmony_ci		}
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci	snd_ak4531_proc_init(card, ak4531);
4078c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ak4531, &ops)) < 0) {
4088c2ecf20Sopenharmony_ci		snd_ak4531_free(ak4531);
4098c2ecf20Sopenharmony_ci		return err;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci#if 0
4138c2ecf20Sopenharmony_ci	snd_ak4531_dump(ak4531);
4148c2ecf20Sopenharmony_ci#endif
4158c2ecf20Sopenharmony_ci	if (rak4531)
4168c2ecf20Sopenharmony_ci		*rak4531 = ak4531;
4178c2ecf20Sopenharmony_ci	return 0;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci/*
4218c2ecf20Sopenharmony_ci * power management
4228c2ecf20Sopenharmony_ci */
4238c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4248c2ecf20Sopenharmony_civoid snd_ak4531_suspend(struct snd_ak4531 *ak4531)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	/* mute */
4278c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_LMASTER, 0x9f);
4288c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_RMASTER, 0x9f);
4298c2ecf20Sopenharmony_ci	/* powerdown */
4308c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_RESET, 0x01);
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_civoid snd_ak4531_resume(struct snd_ak4531 *ak4531)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	int idx;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* initialize */
4388c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_RESET, 0x03);
4398c2ecf20Sopenharmony_ci	udelay(100);
4408c2ecf20Sopenharmony_ci	ak4531->write(ak4531, AK4531_CLOCK, 0x00);
4418c2ecf20Sopenharmony_ci	/* restore mixer registers */
4428c2ecf20Sopenharmony_ci	for (idx = 0; idx <= 0x19; idx++) {
4438c2ecf20Sopenharmony_ci		if (idx == AK4531_RESET || idx == AK4531_CLOCK)
4448c2ecf20Sopenharmony_ci			continue;
4458c2ecf20Sopenharmony_ci		ak4531->write(ak4531, idx, ak4531->regs[idx]);
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci#endif
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci/*
4518c2ecf20Sopenharmony_ci * /proc interface
4528c2ecf20Sopenharmony_ci */
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void snd_ak4531_proc_read(struct snd_info_entry *entry,
4558c2ecf20Sopenharmony_ci				 struct snd_info_buffer *buffer)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct snd_ak4531 *ak4531 = entry->private_data;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Asahi Kasei AK4531\n\n");
4608c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Recording source   : %s\n"
4618c2ecf20Sopenharmony_ci		    "MIC gain           : %s\n",
4628c2ecf20Sopenharmony_ci		    ak4531->regs[AK4531_AD_IN] & 1 ? "external" : "mixer",
4638c2ecf20Sopenharmony_ci		    ak4531->regs[AK4531_MIC_GAIN] & 1 ? "+30dB" : "+0dB");
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic void
4678c2ecf20Sopenharmony_cisnd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(card, "ak4531", ak4531, snd_ak4531_proc_read);
4708c2ecf20Sopenharmony_ci}
471