brintos

brintos / linux-shallow public Read only

0
0
Text · 48.0 KiB · 12d0934 Raw
1769 lines · c
1// SPDX-License-Identifier: GPL-2.02//3// ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier4//5// Copyright (C) 2022 - 2024 Texas Instruments Incorporated6// https://www.ti.com7//8// The TAS2563/TAS2781 driver implements a flexible and configurable9// algo coefficient setting for one, two, or even multiple10// TAS2563/TAS2781 chips.11//12// Author: Shenghao Ding <shenghao-ding@ti.com>13// Author: Kevin Lu <kevin-lu@ti.com>14//15 16#include <linux/crc8.h>17#include <linux/firmware.h>18#include <linux/gpio/consumer.h>19#include <linux/i2c.h>20#include <linux/init.h>21#include <linux/interrupt.h>22#include <linux/module.h>23#include <linux/of.h>24#include <linux/of_address.h>25#include <linux/of_irq.h>26#include <linux/regmap.h>27#include <linux/slab.h>28#include <sound/pcm_params.h>29#include <sound/soc.h>30#include <sound/tas2781.h>31#include <sound/tlv.h>32#include <sound/tas2563-tlv.h>33#include <sound/tas2781-tlv.h>34#include <linux/unaligned.h>35 36#define X2563_CL_STT_VAL(xreg, xval) \37{	.reg = xreg, \38	.val = { xval }, \39	.val_len = 1, }40 41#define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \42{	.reg = xreg, \43	.val = { byte0, byte1, byte2, byte3 }, \44	.val_len = 4, }45 46static const struct bulk_reg_val tas2563_cali_start_reg[] = {47	X2563_CL_STT_VAL(TAS2563_IDLE, 0x00),48	X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG, 0x40, 0x00, 0x00, 0x00),49	X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG, 0x40, 0x00, 0x00, 0x00),50	X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG, 0x7f, 0xff, 0xff, 0xff),51	X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG, 0x40, 0x00, 0x00, 0x00),52	X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG, 0x0a, 0x3d, 0x70, 0xa4),53	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG, 0x00, 0x36, 0x91, 0x5e),54	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG, 0x00, 0x36, 0x91, 0x5e),55	X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG, 0x00, 0x06, 0xd3, 0x72),56	X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG, 0x00, 0x36, 0x91, 0x5e),57	X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG, 0x00, 0x36, 0x91, 0x5e),58};59 60#define X2781_CL_STT_VAL(xreg, xval, xlocked) \61{	.reg = xreg, \62	.val = { xval }, \63	.val_len = 1, \64	.is_locked = xlocked, }65 66#define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \67{	.reg = xreg, \68	.val = { byte0, byte1, byte2, byte3 }, \69	.val_len = 4, \70	.is_locked = false, }71 72#define X2781_CL_STT_LEN_UNLOCKED(xreg) \73{	.reg = xreg, \74	.val_len = 4, \75	.is_locked = false, }76 77static const struct bulk_reg_val tas2781_cali_start_reg[] = {78	X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG, 0xfe, false),79	X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG, 0xdd, false),80	X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG, 0x20, false),81	X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG, 0x14, false),82	X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG, 0x45, true),83	X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG, 0x03, false),84	X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG, 0xa8, false),85	X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG, 0xb9, false),86	X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG, 0x92, false),87	/*88	 * This register is pilot tone threshold, different with the89	 * calibration tool version, it will be updated in90	 * tas2781_calib_start_put(), set to 1mA.91	 */92	X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56),93	X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG,94		0x40, 0x00, 0x00, 0x00),95	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG),96	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG),97};98 99static const struct i2c_device_id tasdevice_id[] = {100	{ "tas2563", TAS2563 },101	{ "tas2781", TAS2781 },102	{}103};104MODULE_DEVICE_TABLE(i2c, tasdevice_id);105 106#ifdef CONFIG_OF107static const struct of_device_id tasdevice_of_match[] = {108	{ .compatible = "ti,tas2563" },109	{ .compatible = "ti,tas2781" },110	{},111};112MODULE_DEVICE_TABLE(of, tasdevice_of_match);113#endif114 115/**116 * tas2781_digital_getvol - get the volum control117 * @kcontrol: control pointer118 * @ucontrol: User data119 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging120 * depends on internal regmap mechanism.121 * tas2781 contains book and page two-level register map, especially122 * book switching will set the register BXXP00R7F, after switching to the123 * correct book, then leverage the mechanism for paging to access the124 * register.125 */126static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,127	struct snd_ctl_elem_value *ucontrol)128{129	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);130	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);131	struct soc_mixer_control *mc =132		(struct soc_mixer_control *)kcontrol->private_value;133 134	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);135}136 137static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,138	struct snd_ctl_elem_value *ucontrol)139{140	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);141	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);142	struct soc_mixer_control *mc =143		(struct soc_mixer_control *)kcontrol->private_value;144 145	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);146}147 148static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,149	struct snd_ctl_elem_value *ucontrol)150{151	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);152	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);153	struct soc_mixer_control *mc =154		(struct soc_mixer_control *)kcontrol->private_value;155 156	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);157}158 159static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,160	struct snd_ctl_elem_value *ucontrol)161{162	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);163	struct tasdevice_priv *tas_priv =164		snd_soc_component_get_drvdata(codec);165	struct soc_mixer_control *mc =166		(struct soc_mixer_control *)kcontrol->private_value;167 168	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);169}170 171static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol,172	struct snd_ctl_elem_value *ucontrol)173{174	struct snd_soc_component *component =175		snd_soc_kcontrol_component(kcontrol);176	struct tasdevice_priv *tas_priv =177		snd_soc_component_get_drvdata(component);178 179	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;180	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,181			tas_priv->force_fwload_status ? "ON" : "OFF");182 183	return 0;184}185 186static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol,187	struct snd_ctl_elem_value *ucontrol)188{189	struct snd_soc_component *component =190		snd_soc_kcontrol_component(kcontrol);191	struct tasdevice_priv *tas_priv =192		snd_soc_component_get_drvdata(component);193	bool change, val = (bool)ucontrol->value.integer.value[0];194 195	if (tas_priv->force_fwload_status == val)196		change = false;197	else {198		change = true;199		tas_priv->force_fwload_status = val;200	}201	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,202		tas_priv->force_fwload_status ? "ON" : "OFF");203 204	return change;205}206 207static int tasdev_cali_data_get(struct snd_kcontrol *kcontrol,208	struct snd_ctl_elem_value *ucontrol)209{210	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);211	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);212	struct soc_bytes_ext *bytes_ext =213		(struct soc_bytes_ext *) kcontrol->private_value;214	struct calidata *cali_data = &priv->cali_data;215	struct cali_reg *p = &cali_data->cali_reg_array;216	unsigned char *dst = ucontrol->value.bytes.data;217	unsigned char *data = cali_data->data;218	unsigned int i = 0;219	unsigned int j, k;220	int rc;221 222	guard(mutex)(&priv->codec_lock);223	if (!priv->is_user_space_calidata)224		return -1;225 226	if (!p->r0_reg)227		return -1;228 229	dst[i++] = bytes_ext->max;230	dst[i++] = 'r';231 232	dst[i++] = TASDEVICE_BOOK_ID(p->r0_reg);233	dst[i++] = TASDEVICE_PAGE_ID(p->r0_reg);234	dst[i++] = TASDEVICE_PAGE_REG(p->r0_reg);235 236	dst[i++] = TASDEVICE_BOOK_ID(p->r0_low_reg);237	dst[i++] = TASDEVICE_PAGE_ID(p->r0_low_reg);238	dst[i++] = TASDEVICE_PAGE_REG(p->r0_low_reg);239 240	dst[i++] = TASDEVICE_BOOK_ID(p->invr0_reg);241	dst[i++] = TASDEVICE_PAGE_ID(p->invr0_reg);242	dst[i++] = TASDEVICE_PAGE_REG(p->invr0_reg);243 244	dst[i++] = TASDEVICE_BOOK_ID(p->pow_reg);245	dst[i++] = TASDEVICE_PAGE_ID(p->pow_reg);246	dst[i++] = TASDEVICE_PAGE_REG(p->pow_reg);247 248	dst[i++] = TASDEVICE_BOOK_ID(p->tlimit_reg);249	dst[i++] = TASDEVICE_PAGE_ID(p->tlimit_reg);250	dst[i++] = TASDEVICE_PAGE_REG(p->tlimit_reg);251 252	for (j = 0, k = 0; j < priv->ndev; j++) {253		if (j == data[k]) {254			dst[i++] = j;255			k++;256		} else {257			dev_err(priv->dev, "chn %d device %u not match\n",258				j, data[k]);259			k += 21;260			continue;261		}262		rc = tasdevice_dev_bulk_read(priv, j, p->r0_reg, &dst[i], 4);263		if (rc < 0) {264			dev_err(priv->dev, "chn %d r0_reg bulk_rd err = %d\n",265				j, rc);266			i += 20;267			k += 20;268			continue;269		}270		rc = memcmp(&dst[i], &data[k], 4);271		if (rc != 0)272			dev_dbg(priv->dev, "chn %d r0_data is not same\n", j);273		k += 4;274		i += 4;275		rc = tasdevice_dev_bulk_read(priv, j, p->r0_low_reg,276			&dst[i], 4);277		if (rc < 0) {278			dev_err(priv->dev, "chn %d r0_low bulk_rd err = %d\n",279				j, rc);280			i += 16;281			k += 16;282			continue;283		}284		rc = memcmp(&dst[i], &data[k], 4);285		if (rc != 0)286			dev_dbg(priv->dev, "chn %d r0_low is not same\n", j);287		i += 4;288		k += 4;289		rc = tasdevice_dev_bulk_read(priv, j, p->invr0_reg,290			&dst[i], 4);291		if (rc < 0) {292			dev_err(priv->dev, "chn %d invr0 bulk_rd err = %d\n",293				j, rc);294			i += 12;295			k += 12;296			continue;297		}298		rc = memcmp(&dst[i], &data[k], 4);299		if (rc != 0)300			dev_dbg(priv->dev, "chn %d invr0 is not same\n", j);301		i += 4;302		k += 4;303		rc = tasdevice_dev_bulk_read(priv, j, p->pow_reg, &dst[i], 4);304		if (rc < 0) {305			dev_err(priv->dev, "chn %d pow_reg bulk_rd err = %d\n",306				j, rc);307			i += 8;308			k += 8;309			continue;310		}311		rc = memcmp(&dst[i], &data[k], 4);312		if (rc != 0)313			dev_dbg(priv->dev, "chn %d pow_reg is not same\n", j);314		i += 4;315		k += 4;316		rc = tasdevice_dev_bulk_read(priv, j, p->tlimit_reg,317			&dst[i], 4);318		if (rc < 0) {319			dev_err(priv->dev, "chn %d tlimit bulk_rd err = %d\n",320				j, rc);321		}322		rc = memcmp(&dst[i], &data[k], 4);323		if (rc != 0)324			dev_dbg(priv->dev, "chn %d tlimit is not same\n", j);325		i += 4;326		k += 4;327	}328	return 0;329}330 331static int calib_data_get(struct tasdevice_priv *tas_priv, int reg,332	unsigned char *dst)333{334	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;335	struct tasdevice *tasdev = tas_priv->tasdevice;336	int rc = -1;337	int i;338 339	for (i = 0; i < tas_priv->ndev; i++) {340		if (clt->addr == tasdev[i].dev_addr) {341			/* First byte is the device index. */342			dst[0] = i;343			rc = tasdevice_dev_bulk_read(tas_priv, i, reg, &dst[1],344				4);345			break;346		}347	}348 349	return rc;350}351 352static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i,353	int *reg, unsigned char *dat)354{355	struct tasdevice *tasdev = tas_priv->tasdevice;356	struct bulk_reg_val *p = tasdev[i].cali_data_backup;357	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);358	int j;359 360	if (p == NULL)361		return;362 363	/* Store the current setting from the chip */364	for (j = 0; j < sum; j++) {365		if (p[j].val_len == 1) {366			if (p[j].is_locked)367				tasdevice_dev_write(tas_priv, i,368					TAS2781_TEST_UNLOCK_REG,369					TAS2781_TEST_PAGE_UNLOCK);370			tasdevice_dev_read(tas_priv, i, p[j].reg,371				(int *)&p[j].val[0]);372		} else {373			switch (p[j].reg) {374			case 0: {375				if (!reg[0])376					continue;377				p[j].reg = reg[0];378			}379				break;380			case TAS2781_PRM_PLT_FLAG_REG:381			p[j].reg = reg[1];382				break;383			case TAS2781_PRM_SINEGAIN_REG:384			p[j].reg = reg[2];385				break;386			case TAS2781_PRM_SINEGAIN2_REG:387			p[j].reg = reg[3];388				break;389			}390			tasdevice_dev_bulk_read(tas_priv, i, p[j].reg,391				p[j].val, 4);392		}393	}394 395	/* Update the setting for calibration */396	for (j = 0; j < sum - 2; j++) {397		if (p[j].val_len == 1) {398			if (p[j].is_locked)399				tasdevice_dev_write(tas_priv, i,400					TAS2781_TEST_UNLOCK_REG,401					TAS2781_TEST_PAGE_UNLOCK);402			tasdevice_dev_write(tas_priv, i, p[j].reg,403				tas2781_cali_start_reg[j].val[0]);404		} else {405			if (!p[j].reg)406				continue;407			tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,408				(unsigned char *)409				tas2781_cali_start_reg[j].val, 4);410		}411	}412 413	tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, &dat[1], 4);414	tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg, &dat[5], 4);415}416 417static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol,418	struct snd_ctl_elem_value *ucontrol)419{420	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);421	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);422	struct soc_bytes_ext *bytes_ext =423		(struct soc_bytes_ext *) kcontrol->private_value;424	unsigned char *dat = ucontrol->value.bytes.data;425	int i, reg[4];426	int j = 0;427 428	guard(mutex)(&priv->codec_lock);429	if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] ||430		dat[1] != 'r') {431		dev_err(priv->dev, "%s: package fmt or chipid incorrect\n",432			__func__);433		return 0;434	}435	j += 2;436	/* refresh pilot tone and SineGain register */437	for (i = 0; i < ARRAY_SIZE(reg); i++) {438		reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]);439		j += 3;440	}441 442	for (i = 0; i < priv->ndev; i++) {443		int k = i * 9 + j;444 445		if (dat[k] != i) {446			dev_err(priv->dev, "%s:no cal-setting for dev %d\n",447				__func__, i);448			continue;449		}450		sngl_calib_start(priv, i, reg, dat + k);451	}452	return 1;453}454 455static void tas2781_calib_stop_put(struct tasdevice_priv *tas_priv)456{457	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);458	int i, j;459 460	for (i = 0; i < tas_priv->ndev; i++) {461		struct tasdevice *tasdev = tas_priv->tasdevice;462		struct bulk_reg_val *p = tasdev[i].cali_data_backup;463 464		if (p == NULL)465			continue;466 467		for (j = 0; j < sum; j++) {468			if (p[j].val_len == 1) {469				if (p[j].is_locked)470					tasdevice_dev_write(tas_priv, i,471						TAS2781_TEST_UNLOCK_REG,472						TAS2781_TEST_PAGE_UNLOCK);473				tasdevice_dev_write(tas_priv, i, p[j].reg,474					p[j].val[0]);475			} else {476				if (!p[j].reg)477					continue;478				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,479					p[j].val, 4);480			}481		}482	}483}484 485static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol,486	struct snd_ctl_elem_value *ucontrol)487{488	struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg;489	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);490	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);491	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);492	int rc = 1;493	int i, j;494 495	guard(mutex)(&tas_priv->codec_lock);496	if (tas_priv->chip_id != TAS2563) {497		rc = -1;498		goto out;499	}500 501	for (i = 0; i < tas_priv->ndev; i++) {502		struct tasdevice *tasdev = tas_priv->tasdevice;503		struct bulk_reg_val *p = tasdev[i].cali_data_backup;504 505		if (p == NULL)506			continue;507		for (j = 0; j < sum; j++) {508			if (p[j].val_len == 1)509				tasdevice_dev_read(tas_priv,510					i, p[j].reg,511					(unsigned int *)&p[j].val[0]);512			else513				tasdevice_dev_bulk_read(tas_priv,514					i, p[j].reg, p[j].val, 4);515		}516 517		for (j = 0; j < sum; j++) {518			if (p[j].val_len == 1)519				tasdevice_dev_write(tas_priv, i, p[j].reg,520					q[j].val[0]);521			else522				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,523					q[j].val, 4);524		}525	}526out:527	return rc;528}529 530static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv)531{532	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);533	int i, j;534 535	for (i = 0; i < tas_priv->ndev; i++) {536		struct tasdevice *tasdev = tas_priv->tasdevice;537		struct bulk_reg_val *p = tasdev[i].cali_data_backup;538 539		if (p == NULL)540			continue;541 542		for (j = 0; j < sum; j++) {543			if (p[j].val_len == 1)544				tasdevice_dev_write(tas_priv, i, p[j].reg,545					p[j].val[0]);546			else547				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,548					p[j].val, 4);549		}550	}551}552 553static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,554	struct snd_ctl_elem_value *ucontrol)555{556	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);557	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);558 559	guard(mutex)(&priv->codec_lock);560	if (priv->chip_id == TAS2563)561		tas2563_calib_stop_put(priv);562	else563		tas2781_calib_stop_put(priv);564 565	return 1;566}567 568static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,569	struct snd_ctl_elem_value *ucontrol)570{571	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);572	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);573	struct soc_bytes_ext *bytes_ext =574		(struct soc_bytes_ext *) kcontrol->private_value;575	struct calidata *cali_data = &priv->cali_data;576	struct cali_reg *p = &cali_data->cali_reg_array;577	unsigned char *src = ucontrol->value.bytes.data;578	unsigned char *dst = cali_data->data;579	int rc = 1, i = 0;580	int j;581 582	guard(mutex)(&priv->codec_lock);583	if (src[0] != bytes_ext->max || src[1] != 'r') {584		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);585		return 0;586	}587	for (j = 0; j < priv->ndev; j++) {588		if (src[17 + j * 21] != j) {589			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);590			return 0;591		}592	}593	i += 2;594	priv->is_user_space_calidata = true;595 596	p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);597	i += 3;598	p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);599	i += 3;600	p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);601	i += 3;602	p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);603	i += 3;604	p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);605	i += 3;606 607	memcpy(dst, &src[i], cali_data->total_sz);608	return rc;609}610 611static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,612	struct snd_ctl_elem_value *ucontrol)613{614	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);615	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);616	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;617	struct soc_bytes_ext *bytes_ext =618		(struct soc_bytes_ext *) kcontrol->private_value;619	struct tasdevice *tasdev = tas_priv->tasdevice;620	unsigned char *dst = ucontrol->value.bytes.data;621	int i, val, rc = -1;622 623	dst[0] = bytes_ext->max;624	guard(mutex)(&tas_priv->codec_lock);625	for (i = 0; i < tas_priv->ndev; i++) {626		if (clt->addr == tasdev[i].dev_addr) {627			/* First byte is the device index. */628			dst[1] = i;629			rc = tasdevice_dev_read(tas_priv, i,630				TAS2781_RUNTIME_LATCH_RE_REG, &val);631			if (rc < 0)632				dev_err(tas_priv->dev, "%s, get value error\n",633					__func__);634			else635				dst[2] = val;636 637			break;638		}639	}640 641	return rc;642}643 644static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,645	struct snd_ctl_elem_value *ucontrol)646{647	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);648	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);649	struct soc_bytes_ext *bytes_ext =650		(struct soc_bytes_ext *) kcontrol->private_value;651	unsigned char *dst = ucontrol->value.bytes.data;652	unsigned int reg;653	int rc = -1;654 655	if (tas_priv->chip_id == TAS2781)656		reg = TAS2781_RUNTIME_RE_REG_TF;657	else658		reg = TAS2563_RUNTIME_RE_REG_TF;659 660	guard(mutex)(&tas_priv->codec_lock);661	dst[0] = bytes_ext->max;662	rc = calib_data_get(tas_priv, reg, &dst[1]);663 664	return rc;665}666 667static int tasdev_re_data_get(struct snd_kcontrol *kcontrol,668	struct snd_ctl_elem_value *ucontrol)669{670	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);671	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);672	struct soc_bytes_ext *bytes_ext =673		(struct soc_bytes_ext *) kcontrol->private_value;674	unsigned char *dst = ucontrol->value.bytes.data;675	unsigned int reg;676	int rc = -1;677 678	if (tas_priv->chip_id == TAS2781)679		reg = TAS2781_RUNTIME_RE_REG;680	else681		reg = TAS2563_RUNTIME_RE_REG;682	guard(mutex)(&tas_priv->codec_lock);683	dst[0] = bytes_ext->max;684	rc = calib_data_get(tas_priv, reg, &dst[1]);685 686	return rc;687}688 689static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,690	struct snd_ctl_elem_value *ucontrol)691{692	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);693	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);694	struct calidata *cali_data = &tas_priv->cali_data;695	struct soc_bytes_ext *bytes_ext =696		(struct soc_bytes_ext *) kcontrol->private_value;697	unsigned char *dst = ucontrol->value.bytes.data;698	unsigned int reg;699	int rc = -1;700 701	guard(mutex)(&tas_priv->codec_lock);702 703	if (tas_priv->chip_id == TAS2563)704		reg = TAS2563_PRM_R0_REG;705	else if (cali_data->cali_reg_array.r0_reg)706		reg = cali_data->cali_reg_array.r0_reg;707	else708		return -1;709	dst[0] = bytes_ext->max;710	rc = calib_data_get(tas_priv, reg, &dst[1]);711 712	return rc;713}714 715static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,716	struct snd_ctl_elem_value *ucontrol)717{718	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);719	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);720	struct soc_bytes_ext *bytes_ext =721		(struct soc_bytes_ext *) kcontrol->private_value;722	unsigned char *dst = ucontrol->value.bytes.data;723	unsigned int reg = TASDEVICE_XM_A1_REG;724	int rc = -1;725 726	guard(mutex)(&tas_priv->codec_lock);727	dst[0] = bytes_ext->max;728	rc = calib_data_get(tas_priv, reg, &dst[1]);729 730	return rc;731}732 733static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,734	struct snd_ctl_elem_value *ucontrol)735{736	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);737	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);738	struct soc_bytes_ext *bytes_ext =739		(struct soc_bytes_ext *) kcontrol->private_value;740	unsigned char *dst = ucontrol->value.bytes.data;741	unsigned int reg = TASDEVICE_XM_A2_REG;742	int rc = -1;743 744	guard(mutex)(&tas_priv->codec_lock);745	dst[0] = bytes_ext->max;746	rc = calib_data_get(tas_priv, reg, &dst[1]);747 748	return rc;749}750 751static int tasdev_nop_get(752	struct snd_kcontrol *kcontrol,753	struct snd_ctl_elem_value *ucontrol)754{755	return 0;756}757 758static int tas2563_digital_gain_get(759	struct snd_kcontrol *kcontrol,760	struct snd_ctl_elem_value *ucontrol)761{762	struct soc_mixer_control *mc =763		(struct soc_mixer_control *)kcontrol->private_value;764	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);765	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);766	unsigned int l = 0, r = mc->max;767	unsigned int target, ar_mid, mid, ar_l, ar_r;768	unsigned int reg = mc->reg;769	unsigned char data[4];770	int ret;771 772	mutex_lock(&tas_dev->codec_lock);773	/* Read the primary device */774	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);775	if (ret) {776		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);777		goto out;778	}779 780	target = get_unaligned_be32(&data[0]);781 782	while (r > 1 + l) {783		mid = (l + r) / 2;784		ar_mid = get_unaligned_be32(tas2563_dvc_table[mid]);785		if (target < ar_mid)786			r = mid;787		else788			l = mid;789	}790 791	ar_l = get_unaligned_be32(tas2563_dvc_table[l]);792	ar_r = get_unaligned_be32(tas2563_dvc_table[r]);793 794	/* find out the member same as or closer to the current volume */795	ucontrol->value.integer.value[0] =796		abs(target - ar_l) <= abs(target - ar_r) ? l : r;797out:798	mutex_unlock(&tas_dev->codec_lock);799	return 0;800}801 802static int tas2563_digital_gain_put(803	struct snd_kcontrol *kcontrol,804	struct snd_ctl_elem_value *ucontrol)805{806	struct soc_mixer_control *mc =807		(struct soc_mixer_control *)kcontrol->private_value;808	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);809	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);810	int vol = ucontrol->value.integer.value[0];811	int status = 0, max = mc->max, rc = 1;812	int i, ret;813	unsigned int reg = mc->reg;814	unsigned int volrd, volwr;815	unsigned char data[4];816 817	vol = clamp(vol, 0, max);818	mutex_lock(&tas_dev->codec_lock);819	/* Read the primary device */820	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);821	if (ret) {822		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);823		rc = -1;824		goto out;825	}826 827	volrd = get_unaligned_be32(&data[0]);828	volwr = get_unaligned_be32(tas2563_dvc_table[vol]);829 830	if (volrd == volwr) {831		rc = 0;832		goto out;833	}834 835	for (i = 0; i < tas_dev->ndev; i++) {836		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,837			(unsigned char *)tas2563_dvc_table[vol], 4);838		if (ret) {839			dev_err(tas_dev->dev,840				"%s, set digital vol error in dev %d\n",841				__func__, i);842			status |= BIT(i);843		}844	}845 846	if (status)847		rc = -1;848out:849	mutex_unlock(&tas_dev->codec_lock);850	return rc;851}852 853static const struct snd_kcontrol_new tasdevice_snd_controls[] = {854	SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,855		tasdev_force_fwload_get, tasdev_force_fwload_put),856};857 858static const struct snd_kcontrol_new tasdevice_cali_controls[] = {859	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,860		tasdev_nop_get, tasdev_calib_stop_put),861	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),862	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),863	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),864	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),865	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),866};867 868static const struct snd_kcontrol_new tas2781_snd_controls[] = {869	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,870		1, 0, 20, 0, tas2781_amp_getvol,871		tas2781_amp_putvol, amp_vol_tlv),872	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,873		0, 0, 200, 1, tas2781_digital_getvol,874		tas2781_digital_putvol, dvc_tlv),875};876 877static const struct snd_kcontrol_new tas2781_cali_controls[] = {878	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),879};880 881static const struct snd_kcontrol_new tas2563_snd_controls[] = {882	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,883		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,884		tas2563_digital_gain_get, tas2563_digital_gain_put,885		tas2563_dvc_tlv),886};887 888static const struct snd_kcontrol_new tas2563_cali_controls[] = {889	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,890		tasdev_nop_get, tas2563_calib_start_put),891};892 893static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,894		struct snd_ctl_elem_value *ucontrol)895{896	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);897	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);898	int ret = 0;899 900	if (tas_priv->rcabin.profile_cfg_id !=901		ucontrol->value.integer.value[0]) {902		tas_priv->rcabin.profile_cfg_id =903			ucontrol->value.integer.value[0];904		ret = 1;905	}906 907	return ret;908}909 910static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol,911			struct snd_ctl_elem_info *uinfo)912{913	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);914	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);915 916	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;917	uinfo->count = 1;918	uinfo->value.integer.min = 0;919	uinfo->value.integer.max = tas_priv->ndev - 1;920 921	return 0;922}923 924static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,925			struct snd_ctl_elem_info *uinfo)926{927	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;928	uinfo->count = 1;929	uinfo->value.integer.min = TAS2563;930	uinfo->value.integer.max = TAS2781;931 932	return 0;933}934 935static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,936			struct snd_ctl_elem_info *uinfo)937{938	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);939	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);940	struct tasdevice_fw *tas_fw = tas_priv->fmw;941 942	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;943	uinfo->count = 1;944	uinfo->value.integer.min = 0;945	uinfo->value.integer.max = (int)tas_fw->nr_programs;946 947	return 0;948}949 950static int tasdevice_info_configurations(951	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)952{953	struct snd_soc_component *codec =954		snd_soc_kcontrol_component(kcontrol);955	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);956	struct tasdevice_fw *tas_fw = tas_priv->fmw;957 958	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;959	uinfo->count = 1;960	uinfo->value.integer.min = 0;961	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;962 963	return 0;964}965 966static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,967			struct snd_ctl_elem_info *uinfo)968{969	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);970	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);971 972	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;973	uinfo->count = 1;974	uinfo->value.integer.min = 0;975	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;976 977	return 0;978}979 980static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,981			struct snd_ctl_elem_value *ucontrol)982{983	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);984	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);985 986	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;987 988	return 0;989}990 991static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,992			struct snd_ctl_elem_value *ucontrol)993{994	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);995	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);996 997	ucontrol->value.integer.value[0] = tas_priv->chip_id;998 999	return 0;1000}1001 1002static int tasdevice_create_control(struct tasdevice_priv *tas_priv)1003{1004	struct snd_kcontrol_new *prof_ctrls;1005	int nr_controls = 1;1006	int mix_index = 0;1007	int ret;1008	char *name;1009 1010	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,1011		sizeof(prof_ctrls[0]), GFP_KERNEL);1012	if (!prof_ctrls) {1013		ret = -ENOMEM;1014		goto out;1015	}1016 1017	/* Create a mixer item for selecting the active profile */1018	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);1019	if (!name) {1020		ret = -ENOMEM;1021		goto out;1022	}1023	prof_ctrls[mix_index].name = name;1024	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1025	prof_ctrls[mix_index].info = tasdevice_info_profile;1026	prof_ctrls[mix_index].get = tasdevice_get_profile_id;1027	prof_ctrls[mix_index].put = tasdevice_set_profile_id;1028	mix_index++;1029 1030	ret = snd_soc_add_component_controls(tas_priv->codec,1031		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);1032 1033out:1034	return ret;1035}1036 1037static int tasdevice_program_get(struct snd_kcontrol *kcontrol,1038	struct snd_ctl_elem_value *ucontrol)1039{1040	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1041	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1042 1043	ucontrol->value.integer.value[0] = tas_priv->cur_prog;1044 1045	return 0;1046}1047 1048static int tasdevice_program_put(struct snd_kcontrol *kcontrol,1049	struct snd_ctl_elem_value *ucontrol)1050{1051	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1052	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1053	unsigned int nr_program = ucontrol->value.integer.value[0];1054	int ret = 0;1055 1056	if (tas_priv->cur_prog != nr_program) {1057		tas_priv->cur_prog = nr_program;1058		ret = 1;1059	}1060 1061	return ret;1062}1063 1064static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,1065	struct snd_ctl_elem_value *ucontrol)1066{1067 1068	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1069	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1070 1071	ucontrol->value.integer.value[0] = tas_priv->cur_conf;1072 1073	return 0;1074}1075 1076static int tasdevice_configuration_put(1077	struct snd_kcontrol *kcontrol,1078	struct snd_ctl_elem_value *ucontrol)1079{1080	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1081	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1082	unsigned int nr_configuration = ucontrol->value.integer.value[0];1083	int ret = 0;1084 1085	if (tas_priv->cur_conf != nr_configuration) {1086		tas_priv->cur_conf = nr_configuration;1087		ret = 1;1088	}1089 1090	return ret;1091}1092 1093static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,1094	struct snd_ctl_elem_value *ucontrol)1095{1096	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1097	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1098	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;1099	struct tasdevice *tasdev = tas_priv->tasdevice;1100	int i;1101 1102	for (i = 0; i < tas_priv->ndev; i++) {1103		if (clt->addr == tasdev[i].dev_addr) {1104			ucontrol->value.integer.value[0] = i;1105			return 0;1106		}1107	}1108 1109	return -1;1110}1111 1112static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,1113	struct snd_ctl_elem_value *ucontrol)1114{1115	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);1116	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1117	int dev_id = ucontrol->value.integer.value[0];1118	int max = tas_priv->ndev - 1, rc;1119 1120	dev_id = clamp(dev_id, 0, max);1121 1122	guard(mutex)(&tas_priv->codec_lock);1123	rc = tasdev_chn_switch(tas_priv, dev_id);1124 1125	return rc;1126}1127 1128static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)1129{1130	struct snd_kcontrol_new *dsp_ctrls;1131	char *active_dev_num, *chip_id;1132	char *conf_name, *prog_name;1133	int nr_controls = 4;1134	int mix_index = 0;1135	int ret;1136 1137	/* Alloc kcontrol via devm_kzalloc, which don't manually1138	 * free the kcontrol1139	 */1140	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,1141		sizeof(dsp_ctrls[0]), GFP_KERNEL);1142	if (!dsp_ctrls) {1143		ret = -ENOMEM;1144		goto out;1145	}1146 1147	/* Create mixer items for selecting the active Program and Config */1148	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",1149		GFP_KERNEL);1150	if (!prog_name) {1151		ret = -ENOMEM;1152		goto out;1153	}1154	dsp_ctrls[mix_index].name = prog_name;1155	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1156	dsp_ctrls[mix_index].info = tasdevice_info_programs;1157	dsp_ctrls[mix_index].get = tasdevice_program_get;1158	dsp_ctrls[mix_index].put = tasdevice_program_put;1159	mix_index++;1160 1161	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",1162		GFP_KERNEL);1163	if (!conf_name) {1164		ret = -ENOMEM;1165		goto out;1166	}1167	dsp_ctrls[mix_index].name = conf_name;1168	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1169	dsp_ctrls[mix_index].info = tasdevice_info_configurations;1170	dsp_ctrls[mix_index].get = tasdevice_configuration_get;1171	dsp_ctrls[mix_index].put = tasdevice_configuration_put;1172	mix_index++;1173 1174	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",1175		GFP_KERNEL);1176	if (!active_dev_num) {1177		ret = -ENOMEM;1178		goto out;1179	}1180	dsp_ctrls[mix_index].name = active_dev_num;1181	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1182	dsp_ctrls[mix_index].info = tasdevice_info_active_num;1183	dsp_ctrls[mix_index].get = tasdevice_active_num_get;1184	dsp_ctrls[mix_index].put = tasdevice_active_num_put;1185	mix_index++;1186 1187	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);1188	if (!chip_id) {1189		ret = -ENOMEM;1190		goto out;1191	}1192	dsp_ctrls[mix_index].name = chip_id;1193	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1194	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;1195	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;1196	mix_index++;1197 1198	ret = snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,1199		nr_controls < mix_index ? nr_controls : mix_index);1200 1201out:1202	return ret;1203}1204 1205static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)1206{1207	struct calidata *cali_data = &priv->cali_data;1208	struct tasdevice *tasdev = priv->tasdevice;1209	struct soc_bytes_ext *ext_cali_data;1210	struct snd_kcontrol_new *cali_ctrls;1211	unsigned int nctrls;1212	char *cali_name;1213	int rc, i;1214 1215	rc = snd_soc_add_component_controls(priv->codec,1216		tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls));1217	if (rc < 0) {1218		dev_err(priv->dev, "%s: Add cali controls err rc = %d",1219			__func__, rc);1220		return rc;1221	}1222 1223	if (priv->chip_id == TAS2781) {1224		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;1225		nctrls = ARRAY_SIZE(tas2781_cali_controls);1226		for (i = 0; i < priv->ndev; i++) {1227			tasdev[i].cali_data_backup =1228				kmemdup(tas2781_cali_start_reg,1229				sizeof(tas2781_cali_start_reg), GFP_KERNEL);1230			if (!tasdev[i].cali_data_backup)1231				return -ENOMEM;1232		}1233	} else {1234		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;1235		nctrls = ARRAY_SIZE(tas2563_cali_controls);1236		for (i = 0; i < priv->ndev; i++) {1237			tasdev[i].cali_data_backup =1238				kmemdup(tas2563_cali_start_reg,1239				sizeof(tas2563_cali_start_reg), GFP_KERNEL);1240			if (!tasdev[i].cali_data_backup)1241				return -ENOMEM;1242		}1243	}1244 1245	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);1246	if (rc < 0) {1247		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",1248			__func__, rc);1249		return rc;1250	}1251 1252	/* index for cali_ctrls */1253	i = 0;1254	if (priv->chip_id == TAS2781)1255		nctrls = 2;1256	else1257		nctrls = 1;1258 1259	/*1260	 * Alloc kcontrol via devm_kzalloc(), which don't manually1261	 * free the kcontrol。1262	 */1263	cali_ctrls = devm_kcalloc(priv->dev, nctrls,1264		sizeof(cali_ctrls[0]), GFP_KERNEL);1265	if (!cali_ctrls)1266		return -ENOMEM;1267 1268	ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data),1269		GFP_KERNEL);1270	if (!ext_cali_data)1271		return -ENOMEM;1272 1273	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",1274		GFP_KERNEL);1275	if (!cali_name)1276		return -ENOMEM;1277	/* the number of calibrated data per tas2563/tas2781 */1278	cali_data->cali_dat_sz_per_dev = 20;1279	/*1280	 * Data structure for tas2563/tas2781 calibrated data:1281	 *	Pkg len (1 byte)1282	 *	Reg id (1 byte, constant 'r')1283	 *	book, page, register array for calibrated data (15 bytes)1284	 *	for (i = 0; i < Device-Sum; i++) {1285	 *		Device #i index_info (1 byte)1286	 *		Calibrated data for Device #i (20 bytes)1287	 *	}1288	 */1289	ext_cali_data->max = priv->ndev *1290		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;1291	priv->cali_data.total_sz = priv->ndev *1292		(cali_data->cali_dat_sz_per_dev + 1);1293	priv->cali_data.data = devm_kzalloc(priv->dev,1294		ext_cali_data->max, GFP_KERNEL);1295	cali_ctrls[i].name = cali_name;1296	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1297	cali_ctrls[i].info = snd_soc_bytes_info_ext;1298	cali_ctrls[i].get = tasdev_cali_data_get;1299	cali_ctrls[i].put = tasdev_cali_data_put;1300	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;1301	i++;1302 1303	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,1304		GFP_KERNEL);1305	if (!cali_data->data)1306		return -ENOMEM;1307 1308	if (priv->chip_id == TAS2781) {1309		struct soc_bytes_ext *ext_cali_start;1310		char *cali_start_name;1311 1312		ext_cali_start = devm_kzalloc(priv->dev,1313			sizeof(*ext_cali_start), GFP_KERNEL);1314		if (!ext_cali_start)1315			return -ENOMEM;1316 1317		cali_start_name = devm_kstrdup(priv->dev,1318			"Calibration Start", GFP_KERNEL);1319		if (!cali_start_name)1320			return -ENOMEM;1321		/*1322		 * package structure for tas2781 ftc start:1323		 *	Pkg len (1 byte)1324		 *	Reg id (1 byte, constant 'r')1325		 *	book, page, register for pilot threshold, pilot tone1326		 *		and sine gain (12 bytes)1327		 *	for (i = 0; i < Device-Sum; i++) {1328		 *		Device #i index_info (1 byte)1329		 *		Sine gain for Device #i (8 bytes)1330		 *	}1331		 */1332		ext_cali_start->max = 14 + priv->ndev * 9;1333		cali_ctrls[i].name = cali_start_name;1334		cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;1335		cali_ctrls[i].info = snd_soc_bytes_info_ext;1336		cali_ctrls[i].put = tas2781_calib_start_put;1337		cali_ctrls[i].get = tasdev_nop_get;1338		cali_ctrls[i].private_value = (unsigned long)ext_cali_start;1339		i++;1340	}1341 1342	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls,1343		nctrls < i ? nctrls : i);1344 1345	return rc;1346}1347 1348static void tasdevice_fw_ready(const struct firmware *fmw,1349	void *context)1350{1351	struct tasdevice_priv *tas_priv = context;1352	int ret = 0;1353	int i;1354 1355	mutex_lock(&tas_priv->codec_lock);1356 1357	ret = tasdevice_rca_parser(tas_priv, fmw);1358	if (ret) {1359		tasdevice_config_info_remove(tas_priv);1360		goto out;1361	}1362	tasdevice_create_control(tas_priv);1363 1364	tasdevice_dsp_remove(tas_priv);1365	tasdevice_calbin_remove(tas_priv);1366	/*1367	 * The baseline is the RCA-only case, and then the code attempts to1368	 * load DSP firmware but in case of failures just keep going, i.e.1369	 * failing to load DSP firmware is NOT an error.1370	 */1371	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;1372	if (tas_priv->name_prefix)1373		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",1374			tas_priv->name_prefix, tas_priv->dev_name);1375	else1376		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",1377			tas_priv->dev_name);1378	ret = tasdevice_dsp_parser(tas_priv);1379	if (ret) {1380		dev_err(tas_priv->dev, "dspfw load %s error\n",1381			tas_priv->coef_binaryname);1382		goto out;1383	}1384 1385	/*1386	 * If no dsp-related kcontrol created, the dsp resource will be freed.1387	 */1388	ret = tasdevice_dsp_create_ctrls(tas_priv);1389	if (ret) {1390		dev_err(tas_priv->dev, "dsp controls error\n");1391		goto out;1392	}1393 1394	ret = tasdevice_create_cali_ctrls(tas_priv);1395	if (ret) {1396		dev_err(tas_priv->dev, "cali controls error\n");1397		goto out;1398	}1399 1400	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;1401 1402	/* If calibrated data occurs error, dsp will still works with default1403	 * calibrated data inside algo.1404	 */1405	for (i = 0; i < tas_priv->ndev; i++) {1406		if (tas_priv->name_prefix)1407			scnprintf(tas_priv->cal_binaryname[i], 64,1408				"%s-%s_cal_0x%02x.bin", tas_priv->name_prefix,1409				tas_priv->dev_name,1410				tas_priv->tasdevice[i].dev_addr);1411		else1412			scnprintf(tas_priv->cal_binaryname[i], 64,1413				"%s_cal_0x%02x.bin", tas_priv->dev_name,1414				tas_priv->tasdevice[i].dev_addr);1415		ret = tas2781_load_calibration(tas_priv,1416			tas_priv->cal_binaryname[i], i);1417		if (ret != 0)1418			dev_err(tas_priv->dev,1419				"%s: load %s error, default will effect\n",1420				__func__, tas_priv->cal_binaryname[i]);1421	}1422 1423	tasdevice_prmg_load(tas_priv, 0);1424	tas_priv->cur_prog = 0;1425out:1426	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {1427		/* If DSP FW fail, DSP kcontrol won't be created. */1428		tasdevice_dsp_remove(tas_priv);1429	}1430	mutex_unlock(&tas_priv->codec_lock);1431	if (fmw)1432		release_firmware(fmw);1433}1434 1435static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,1436			struct snd_kcontrol *kcontrol, int event)1437{1438	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);1439	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1440	int state = 0;1441 1442	/* Codec Lock Hold */1443	mutex_lock(&tas_priv->codec_lock);1444	if (event == SND_SOC_DAPM_PRE_PMD)1445		state = 1;1446	tasdevice_tuning_switch(tas_priv, state);1447	/* Codec Lock Release*/1448	mutex_unlock(&tas_priv->codec_lock);1449 1450	return 0;1451}1452 1453static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {1454	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),1455	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,1456		0, 0, tasdevice_dapm_event,1457		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),1458	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),1459	SND_SOC_DAPM_OUTPUT("OUT"),1460	SND_SOC_DAPM_INPUT("DMIC"),1461};1462 1463static const struct snd_soc_dapm_route tasdevice_audio_map[] = {1464	{"SPK", NULL, "ASI"},1465	{"OUT", NULL, "SPK"},1466	{"ASI OUT", NULL, "DMIC"},1467};1468 1469static int tasdevice_startup(struct snd_pcm_substream *substream,1470						struct snd_soc_dai *dai)1471{1472	struct snd_soc_component *codec = dai->component;1473	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1474 1475	switch (tas_priv->fw_state) {1476	case TASDEVICE_RCA_FW_OK:1477	case TASDEVICE_DSP_FW_ALL_OK:1478		return 0;1479	default:1480		return -EINVAL;1481	}1482}1483 1484static int tasdevice_hw_params(struct snd_pcm_substream *substream,1485	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)1486{1487	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);1488	unsigned int slot_width;1489	unsigned int fsrate;1490	int bclk_rate;1491	int rc = 0;1492 1493	fsrate = params_rate(params);1494	switch (fsrate) {1495	case 48000:1496	case 44100:1497		break;1498	default:1499		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",1500			__func__, fsrate);1501		rc = -EINVAL;1502		goto out;1503	}1504 1505	slot_width = params_width(params);1506	switch (slot_width) {1507	case 16:1508	case 20:1509	case 24:1510	case 32:1511		break;1512	default:1513		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",1514			__func__, slot_width);1515		rc = -EINVAL;1516		goto out;1517	}1518 1519	bclk_rate = snd_soc_params_to_bclk(params);1520	if (bclk_rate < 0) {1521		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",1522			__func__, bclk_rate);1523		rc = bclk_rate;1524		goto out;1525	}1526 1527out:1528	return rc;1529}1530 1531static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,1532	int clk_id, unsigned int freq, int dir)1533{1534	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);1535 1536	tas_priv->sysclk = freq;1537 1538	return 0;1539}1540 1541static const struct snd_soc_dai_ops tasdevice_dai_ops = {1542	.startup = tasdevice_startup,1543	.hw_params = tasdevice_hw_params,1544	.set_sysclk = tasdevice_set_dai_sysclk,1545};1546 1547static struct snd_soc_dai_driver tasdevice_dai_driver[] = {1548	{1549		.name = "tasdev_codec",1550		.id = 0,1551		.playback = {1552			.stream_name = "Playback",1553			.channels_min = 1,1554			.channels_max = 4,1555			.rates	 = TASDEVICE_RATES,1556			.formats	= TASDEVICE_FORMATS,1557		},1558		.capture = {1559			.stream_name = "Capture",1560			.channels_min = 1,1561			.channels_max = 4,1562			.rates	 = TASDEVICE_RATES,1563			.formats	= TASDEVICE_FORMATS,1564		},1565		.ops = &tasdevice_dai_ops,1566		.symmetric_rate = 1,1567	},1568};1569 1570static int tasdevice_codec_probe(struct snd_soc_component *codec)1571{1572	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1573	struct snd_kcontrol_new *p;1574	unsigned int size;1575	int rc;1576 1577	switch (tas_priv->chip_id) {1578	case TAS2781:1579		p = (struct snd_kcontrol_new *)tas2781_snd_controls;1580		size = ARRAY_SIZE(tas2781_snd_controls);1581		break;1582	default:1583		p = (struct snd_kcontrol_new *)tas2563_snd_controls;1584		size = ARRAY_SIZE(tas2563_snd_controls);1585	}1586 1587	rc = snd_soc_add_component_controls(codec, p, size);1588	if (rc < 0) {1589		dev_err(tas_priv->dev, "%s: Add control err rc = %d",1590			__func__, rc);1591		return rc;1592	}1593 1594	tas_priv->name_prefix = codec->name_prefix;1595	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);1596}1597 1598static void tasdevice_deinit(void *context)1599{1600	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;1601	struct tasdevice *tasdev = tas_priv->tasdevice;1602	int i;1603 1604	for (i = 0; i < tas_priv->ndev; i++)1605		kfree(tasdev[i].cali_data_backup);1606 1607	tasdevice_config_info_remove(tas_priv);1608	tasdevice_dsp_remove(tas_priv);1609	tasdevice_calbin_remove(tas_priv);1610	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;1611}1612 1613static void tasdevice_codec_remove(struct snd_soc_component *codec)1614{1615	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);1616 1617	tasdevice_deinit(tas_priv);1618}1619 1620static const struct snd_soc_component_driver1621	soc_codec_driver_tasdevice = {1622	.probe			= tasdevice_codec_probe,1623	.remove			= tasdevice_codec_remove,1624	.controls		= tasdevice_snd_controls,1625	.num_controls		= ARRAY_SIZE(tasdevice_snd_controls),1626	.dapm_widgets		= tasdevice_dapm_widgets,1627	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),1628	.dapm_routes		= tasdevice_audio_map,1629	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),1630	.idle_bias_on		= 1,1631	.endianness		= 1,1632};1633 1634static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)1635{1636	struct i2c_client *client = (struct i2c_client *)tas_priv->client;1637	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];1638	int i, ndev = 0;1639 1640	if (tas_priv->isacpi) {1641		ndev = device_property_read_u32_array(&client->dev,1642			"ti,audio-slots", NULL, 0);1643		if (ndev <= 0) {1644			ndev = 1;1645			dev_addrs[0] = client->addr;1646		} else {1647			ndev = (ndev < ARRAY_SIZE(dev_addrs))1648				? ndev : ARRAY_SIZE(dev_addrs);1649			ndev = device_property_read_u32_array(&client->dev,1650				"ti,audio-slots", dev_addrs, ndev);1651		}1652 1653		tas_priv->irq =1654			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);1655	} else if (IS_ENABLED(CONFIG_OF)) {1656		struct device_node *np = tas_priv->dev->of_node;1657		u64 addr;1658 1659		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {1660			if (of_property_read_reg(np, i, &addr, NULL))1661				break;1662			dev_addrs[ndev++] = addr;1663		}1664 1665		tas_priv->irq = of_irq_get(np, 0);1666	} else {1667		ndev = 1;1668		dev_addrs[0] = client->addr;1669	}1670	tas_priv->ndev = ndev;1671	for (i = 0; i < ndev; i++)1672		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];1673 1674	tas_priv->reset = devm_gpiod_get_optional(&client->dev,1675			"reset", GPIOD_OUT_HIGH);1676	if (IS_ERR(tas_priv->reset))1677		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",1678			__func__);1679 1680	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);1681}1682 1683static int tasdevice_i2c_probe(struct i2c_client *i2c)1684{1685	const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);1686	const struct acpi_device_id *acpi_id;1687	struct tasdevice_priv *tas_priv;1688	int ret;1689 1690	tas_priv = tasdevice_kzalloc(i2c);1691	if (!tas_priv)1692		return -ENOMEM;1693 1694	dev_set_drvdata(&i2c->dev, tas_priv);1695 1696	if (ACPI_HANDLE(&i2c->dev)) {1697		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,1698				&i2c->dev);1699		if (!acpi_id) {1700			dev_err(&i2c->dev, "No driver data\n");1701			ret = -EINVAL;1702			goto err;1703		}1704		tas_priv->chip_id = acpi_id->driver_data;1705		tas_priv->isacpi = true;1706	} else {1707		tas_priv->chip_id = id ? id->driver_data : 0;1708		tas_priv->isacpi = false;1709	}1710 1711	tasdevice_parse_dt(tas_priv);1712 1713	ret = tasdevice_init(tas_priv);1714	if (ret)1715		goto err;1716 1717	tasdevice_reset(tas_priv);1718 1719	ret = devm_snd_soc_register_component(tas_priv->dev,1720		&soc_codec_driver_tasdevice,1721		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));1722	if (ret) {1723		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",1724			__func__, ret);1725		goto err;1726	}1727err:1728	if (ret < 0)1729		tasdevice_remove(tas_priv);1730	return ret;1731}1732 1733static void tasdevice_i2c_remove(struct i2c_client *client)1734{1735	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);1736 1737	tasdevice_remove(tas_priv);1738}1739 1740#ifdef CONFIG_ACPI1741static const struct acpi_device_id tasdevice_acpi_match[] = {1742	{ "TAS2781", TAS2781 },1743	{},1744};1745 1746MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);1747#endif1748 1749static struct i2c_driver tasdevice_i2c_driver = {1750	.driver = {1751		.name = "tasdev-codec",1752		.of_match_table = of_match_ptr(tasdevice_of_match),1753#ifdef CONFIG_ACPI1754		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),1755#endif1756	},1757	.probe	= tasdevice_i2c_probe,1758	.remove = tasdevice_i2c_remove,1759	.id_table = tasdevice_id,1760};1761 1762module_i2c_driver(tasdevice_i2c_driver);1763 1764MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");1765MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");1766MODULE_DESCRIPTION("ASoC TAS2781 Driver");1767MODULE_LICENSE("GPL");1768MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);1769