File Index
Files and directories
Sign in
FILE

tas2781-i2c.c

File preview with syntax highlighting.

linux-6.19.14/sound/soc/codecs/tas2781-i2c.c C
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
   4//
   5// Copyright (C) 2022 - 2025 Texas Instruments Incorporated
   6// https://www.ti.com
   7//
   8// The TAS2563/TAS2781 driver implements a flexible and configurable
   9// algo coefficient setting for one, two, or even multiple
  10// 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/tas2781-comlib-i2c.h>
  32#include <sound/tlv.h>
  33#include <sound/tas2x20-tlv.h>
  34#include <sound/tas2563-tlv.h>
  35#include <sound/tas2781-tlv.h>
  36#include <sound/tas5825-tlv.h>
  37#include <linux/unaligned.h>
  38
  39#define X2563_CL_STT_VAL(xreg, xval) \
  40{	.reg = xreg, \
  41	.val = { xval }, \
  42	.val_len = 1, }
  43
  44#define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \
  45{	.reg = xreg, \
  46	.val = { byte0, byte1, byte2, byte3 }, \
  47	.val_len = 4, }
  48
  49static const struct bulk_reg_val tas2563_cali_start_reg[] = {
  50	X2563_CL_STT_VAL(TAS2563_IDLE, 0x00),
  51	X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG, 0x40, 0x00, 0x00, 0x00),
  52	X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG, 0x40, 0x00, 0x00, 0x00),
  53	X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG, 0x7f, 0xff, 0xff, 0xff),
  54	X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG, 0x40, 0x00, 0x00, 0x00),
  55	X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG, 0x0a, 0x3d, 0x70, 0xa4),
  56	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG, 0x00, 0x36, 0x91, 0x5e),
  57	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG, 0x00, 0x36, 0x91, 0x5e),
  58	X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG, 0x00, 0x06, 0xd3, 0x72),
  59	X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG, 0x00, 0x36, 0x91, 0x5e),
  60	X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG, 0x00, 0x36, 0x91, 0x5e),
  61};
  62
  63#define X2781_CL_STT_VAL(xreg, xval, xlocked) \
  64{	.reg = xreg, \
  65	.val = { xval }, \
  66	.val_len = 1, \
  67	.is_locked = xlocked, }
  68
  69#define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \
  70{	.reg = xreg, \
  71	.val = { byte0, byte1, byte2, byte3 }, \
  72	.val_len = 4, \
  73	.is_locked = false, }
  74
  75#define X2781_CL_STT_LEN_UNLOCKED(xreg) \
  76{	.reg = xreg, \
  77	.val_len = 4, \
  78	.is_locked = false, }
  79
  80static const struct bulk_reg_val tas2781_cali_start_reg[] = {
  81	X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG, 0xfe, false),
  82	X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG, 0xdd, false),
  83	X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG, 0x20, false),
  84	X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG, 0x14, true),
  85	X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG, 0x45, true),
  86	X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG, 0x03, false),
  87	X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG, 0xa8, false),
  88	X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG, 0xb9, false),
  89	X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG, 0x92, false),
  90	/*
  91	 * This register is pilot tone threshold, different with the
  92	 * calibration tool version, it will be updated in
  93	 * tas2781_calib_start_put(), set to 1mA.
  94	 */
  95	X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56),
  96	X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG,
  97		0x40, 0x00, 0x00, 0x00),
  98	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG),
  99	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG),
 100};
 101
 102static const struct i2c_device_id tasdevice_id[] = {
 103	{ "tas2020", TAS2020 },
 104	{ "tas2118", TAS2118 },
 105	{ "tas2120", TAS2120 },
 106	{ "tas2320", TAS2320 },
 107	{ "tas2563", TAS2563 },
 108	{ "tas2568", TAS2568 },
 109	{ "tas2570", TAS2570 },
 110	{ "tas2572", TAS2572 },
 111	{ "tas2574", TAS2574 },
 112	{ "tas2781", TAS2781 },
 113	{ "tas5802", TAS5802 },
 114	{ "tas5806m", TAS5806M },
 115	{ "tas5806md", TAS5806MD },
 116	{ "tas5815", TAS5815 },
 117	{ "tas5822", TAS5822 },
 118	{ "tas5825", TAS5825 },
 119	{ "tas5827", TAS5827 },
 120	{ "tas5828", TAS5828 },
 121	{ "tas5830", TAS5830 },
 122	{}
 123};
 124MODULE_DEVICE_TABLE(i2c, tasdevice_id);
 125
 126#ifdef CONFIG_OF
 127static const struct of_device_id tasdevice_of_match[] = {
 128	{ .compatible = "ti,tas2020" },
 129	{ .compatible = "ti,tas2118" },
 130	{ .compatible = "ti,tas2120" },
 131	{ .compatible = "ti,tas2320" },
 132	{ .compatible = "ti,tas2563" },
 133	{ .compatible = "ti,tas2568" },
 134	{ .compatible = "ti,tas2570" },
 135	{ .compatible = "ti,tas2572" },
 136	{ .compatible = "ti,tas2574" },
 137	{ .compatible = "ti,tas2781" },
 138	{ .compatible = "ti,tas5802" },
 139	{ .compatible = "ti,tas5806m" },
 140	{ .compatible = "ti,tas5806md" },
 141	{ .compatible = "ti,tas5815" },
 142	{ .compatible = "ti,tas5822" },
 143	{ .compatible = "ti,tas5825" },
 144	{ .compatible = "ti,tas5827" },
 145	{ .compatible = "ti,tas5828" },
 146	{ .compatible = "ti,tas5830" },
 147	{},
 148};
 149MODULE_DEVICE_TABLE(of, tasdevice_of_match);
 150#endif
 151
 152/**
 153 * tas2781_digital_getvol - get the volum control
 154 * @kcontrol: control pointer
 155 * @ucontrol: User data
 156 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
 157 * depends on internal regmap mechanism.
 158 * tas2781 contains book and page two-level register map, especially
 159 * book switching will set the register BXXP00R7F, after switching to the
 160 * correct book, then leverage the mechanism for paging to access the
 161 * register.
 162 */
 163static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
 164	struct snd_ctl_elem_value *ucontrol)
 165{
 166	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 167	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
 168	struct soc_mixer_control *mc =
 169		(struct soc_mixer_control *)kcontrol->private_value;
 170
 171	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
 172}
 173
 174static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
 175	struct snd_ctl_elem_value *ucontrol)
 176{
 177	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 178	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
 179	struct soc_mixer_control *mc =
 180		(struct soc_mixer_control *)kcontrol->private_value;
 181
 182	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
 183}
 184
 185static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
 186	struct snd_ctl_elem_value *ucontrol)
 187{
 188	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 189	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
 190	struct soc_mixer_control *mc =
 191		(struct soc_mixer_control *)kcontrol->private_value;
 192
 193	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
 194}
 195
 196static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
 197	struct snd_ctl_elem_value *ucontrol)
 198{
 199	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 200	struct tasdevice_priv *tas_priv =
 201		snd_soc_component_get_drvdata(codec);
 202	struct soc_mixer_control *mc =
 203		(struct soc_mixer_control *)kcontrol->private_value;
 204
 205	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
 206}
 207
 208static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol,
 209	struct snd_ctl_elem_value *ucontrol)
 210{
 211	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 212	struct tasdevice_priv *tas_priv =
 213		snd_soc_component_get_drvdata(component);
 214
 215	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
 216	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
 217			tas_priv->force_fwload_status ? "ON" : "OFF");
 218
 219	return 0;
 220}
 221
 222static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol,
 223	struct snd_ctl_elem_value *ucontrol)
 224{
 225	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 226	struct tasdevice_priv *tas_priv =
 227		snd_soc_component_get_drvdata(component);
 228	bool change, val = (bool)ucontrol->value.integer.value[0];
 229
 230	if (tas_priv->force_fwload_status == val)
 231		change = false;
 232	else {
 233		change = true;
 234		tas_priv->force_fwload_status = val;
 235	}
 236	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
 237		tas_priv->force_fwload_status ? "ON" : "OFF");
 238
 239	return change;
 240}
 241
 242static int tasdev_cali_data_get(struct snd_kcontrol *kcontrol,
 243	struct snd_ctl_elem_value *ucontrol)
 244{
 245	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 246	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
 247	struct soc_bytes_ext *bytes_ext =
 248		(struct soc_bytes_ext *) kcontrol->private_value;
 249	struct calidata *cali_data = &priv->cali_data;
 250	struct cali_reg *p = &cali_data->cali_reg_array;
 251	unsigned char *dst = ucontrol->value.bytes.data;
 252	unsigned char *data = cali_data->data;
 253	unsigned int i = 0;
 254	unsigned int j, k;
 255	int rc;
 256
 257	guard(mutex)(&priv->codec_lock);
 258	if (!priv->is_user_space_calidata)
 259		return -1;
 260
 261	if (!p->r0_reg)
 262		return -1;
 263
 264	dst[i++] = bytes_ext->max;
 265	dst[i++] = 'r';
 266
 267	dst[i++] = TASDEVICE_BOOK_ID(p->r0_reg);
 268	dst[i++] = TASDEVICE_PAGE_ID(p->r0_reg);
 269	dst[i++] = TASDEVICE_PAGE_REG(p->r0_reg);
 270
 271	dst[i++] = TASDEVICE_BOOK_ID(p->r0_low_reg);
 272	dst[i++] = TASDEVICE_PAGE_ID(p->r0_low_reg);
 273	dst[i++] = TASDEVICE_PAGE_REG(p->r0_low_reg);
 274
 275	dst[i++] = TASDEVICE_BOOK_ID(p->invr0_reg);
 276	dst[i++] = TASDEVICE_PAGE_ID(p->invr0_reg);
 277	dst[i++] = TASDEVICE_PAGE_REG(p->invr0_reg);
 278
 279	dst[i++] = TASDEVICE_BOOK_ID(p->pow_reg);
 280	dst[i++] = TASDEVICE_PAGE_ID(p->pow_reg);
 281	dst[i++] = TASDEVICE_PAGE_REG(p->pow_reg);
 282
 283	dst[i++] = TASDEVICE_BOOK_ID(p->tlimit_reg);
 284	dst[i++] = TASDEVICE_PAGE_ID(p->tlimit_reg);
 285	dst[i++] = TASDEVICE_PAGE_REG(p->tlimit_reg);
 286
 287	for (j = 0, k = 0; j < priv->ndev; j++) {
 288		if (j == data[k]) {
 289			dst[i++] = j;
 290			k++;
 291		} else {
 292			dev_err(priv->dev, "chn %d device %u not match\n",
 293				j, data[k]);
 294			k += 21;
 295			continue;
 296		}
 297		rc = tasdevice_dev_bulk_read(priv, j, p->r0_reg, &dst[i], 4);
 298		if (rc < 0) {
 299			dev_err(priv->dev, "chn %d r0_reg bulk_rd err = %d\n",
 300				j, rc);
 301			i += 20;
 302			k += 20;
 303			continue;
 304		}
 305		rc = memcmp(&dst[i], &data[k], 4);
 306		if (rc != 0)
 307			dev_dbg(priv->dev, "chn %d r0_data is not same\n", j);
 308		k += 4;
 309		i += 4;
 310		rc = tasdevice_dev_bulk_read(priv, j, p->r0_low_reg,
 311			&dst[i], 4);
 312		if (rc < 0) {
 313			dev_err(priv->dev, "chn %d r0_low bulk_rd err = %d\n",
 314				j, rc);
 315			i += 16;
 316			k += 16;
 317			continue;
 318		}
 319		rc = memcmp(&dst[i], &data[k], 4);
 320		if (rc != 0)
 321			dev_dbg(priv->dev, "chn %d r0_low is not same\n", j);
 322		i += 4;
 323		k += 4;
 324		rc = tasdevice_dev_bulk_read(priv, j, p->invr0_reg,
 325			&dst[i], 4);
 326		if (rc < 0) {
 327			dev_err(priv->dev, "chn %d invr0 bulk_rd err = %d\n",
 328				j, rc);
 329			i += 12;
 330			k += 12;
 331			continue;
 332		}
 333		rc = memcmp(&dst[i], &data[k], 4);
 334		if (rc != 0)
 335			dev_dbg(priv->dev, "chn %d invr0 is not same\n", j);
 336		i += 4;
 337		k += 4;
 338		rc = tasdevice_dev_bulk_read(priv, j, p->pow_reg, &dst[i], 4);
 339		if (rc < 0) {
 340			dev_err(priv->dev, "chn %d pow_reg bulk_rd err = %d\n",
 341				j, rc);
 342			i += 8;
 343			k += 8;
 344			continue;
 345		}
 346		rc = memcmp(&dst[i], &data[k], 4);
 347		if (rc != 0)
 348			dev_dbg(priv->dev, "chn %d pow_reg is not same\n", j);
 349		i += 4;
 350		k += 4;
 351		rc = tasdevice_dev_bulk_read(priv, j, p->tlimit_reg,
 352			&dst[i], 4);
 353		if (rc < 0) {
 354			dev_err(priv->dev, "chn %d tlimit bulk_rd err = %d\n",
 355				j, rc);
 356		}
 357		rc = memcmp(&dst[i], &data[k], 4);
 358		if (rc != 0)
 359			dev_dbg(priv->dev, "chn %d tlimit is not same\n", j);
 360		i += 4;
 361		k += 4;
 362	}
 363	return 0;
 364}
 365
 366static int calib_data_get(struct tasdevice_priv *tas_priv, int reg,
 367	unsigned char *dst)
 368{
 369	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
 370	struct tasdevice *tasdev = tas_priv->tasdevice;
 371	int rc = -1;
 372	int i;
 373
 374	for (i = 0; i < tas_priv->ndev; i++) {
 375		if (clt->addr == tasdev[i].dev_addr) {
 376			/* First byte is the device index. */
 377			dst[0] = i;
 378			rc = tasdevice_dev_bulk_read(tas_priv, i, reg, &dst[1],
 379				4);
 380			break;
 381		}
 382	}
 383
 384	return rc;
 385}
 386
 387static int partial_cali_data_update(int *reg, int j)
 388{
 389	switch (tas2781_cali_start_reg[j].reg) {
 390	case 0:
 391		return reg[0];
 392	case TAS2781_PRM_PLT_FLAG_REG:
 393		return reg[1];
 394	case TAS2781_PRM_SINEGAIN_REG:
 395		return reg[2];
 396	case TAS2781_PRM_SINEGAIN2_REG:
 397		return reg[3];
 398	default:
 399		return 0;
 400	}
 401}
 402
 403static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i,
 404	int *reg, unsigned char *dat)
 405{
 406	struct tasdevice *tasdev = tas_priv->tasdevice;
 407	struct bulk_reg_val *p = tasdev[i].cali_data_backup;
 408	struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
 409	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
 410	unsigned char val[4];
 411	int j, r;
 412
 413	if (p == NULL)
 414		return;
 415
 416	/* Store the current setting from the chip */
 417	for (j = 0; j < sum; j++) {
 418		if (p[j].val_len == 1) {
 419			if (p[j].is_locked)
 420				tasdevice_dev_write(tas_priv, i,
 421					TAS2781_TEST_UNLOCK_REG,
 422					TAS2781_TEST_PAGE_UNLOCK);
 423			tasdevice_dev_read(tas_priv, i, p[j].reg,
 424				(int *)&p[j].val[0]);
 425		} else {
 426			if (!tas_priv->dspbin_typ) {
 427				r = partial_cali_data_update(reg, j);
 428				if (r)
 429					p[j].reg = r;
 430			}
 431
 432			if (p[j].reg)
 433				tasdevice_dev_bulk_read(tas_priv, i, p[j].reg,
 434					p[j].val, 4);
 435		}
 436	}
 437
 438	if (tas_priv->dspbin_typ == TASDEV_ALPHA)
 439		tasdevice_dev_bulk_read(tas_priv, i, t->reg, t->val, 4);
 440
 441	/* Update the setting for calibration */
 442	for (j = 0; j < sum - 4; j++) {
 443		if (p[j].val_len == 1) {
 444			if (p[j].is_locked)
 445				tasdevice_dev_write(tas_priv, i,
 446					TAS2781_TEST_UNLOCK_REG,
 447					TAS2781_TEST_PAGE_UNLOCK);
 448			tasdevice_dev_write(tas_priv, i, p[j].reg,
 449				tas2781_cali_start_reg[j].val[0]);
 450		}
 451	}
 452
 453	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
 454		val[0] = 0x00;
 455		val[1] = 0x00;
 456		val[2] = 0x21;
 457		val[3] = 0x8e;
 458	} else {
 459		val[0] = tas2781_cali_start_reg[j].val[0];
 460		val[1] = tas2781_cali_start_reg[j].val[1];
 461		val[2] = tas2781_cali_start_reg[j].val[2];
 462		val[3] = tas2781_cali_start_reg[j].val[3];
 463	}
 464	tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, val, 4);
 465	tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg,
 466		(unsigned char *)tas2781_cali_start_reg[j + 1].val, 4);
 467	tasdevice_dev_bulk_write(tas_priv, i, p[j + 2].reg, &dat[1], 4);
 468	tasdevice_dev_bulk_write(tas_priv, i, p[j + 3].reg, &dat[5], 4);
 469	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
 470		val[0] = 0x00;
 471		val[1] = 0x00;
 472		val[2] = 0x2a;
 473		val[3] = 0x0b;
 474
 475		tasdevice_dev_bulk_read(tas_priv, i, t->reg, val, 4);
 476	}
 477}
 478
 479static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol,
 480	struct snd_ctl_elem_value *ucontrol)
 481{
 482	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 483	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
 484	struct soc_bytes_ext *bytes_ext =
 485		(struct soc_bytes_ext *) kcontrol->private_value;
 486	unsigned char *dat = ucontrol->value.bytes.data;
 487	int i, reg[4];
 488	int j = 0;
 489
 490	guard(mutex)(&priv->codec_lock);
 491	if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] ||
 492		dat[1] != 'r') {
 493		dev_err(priv->dev, "%s: package fmt or chipid incorrect\n",
 494			__func__);
 495		return 0;
 496	}
 497	j += 2;
 498	/* refresh pilot tone and SineGain register */
 499	for (i = 0; i < ARRAY_SIZE(reg); i++) {
 500		reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]);
 501		j += 3;
 502	}
 503
 504	for (i = 0; i < priv->ndev; i++) {
 505		int k = i * 9 + j;
 506
 507		if (dat[k] != i) {
 508			dev_err(priv->dev, "%s:no cal-setting for dev %d\n",
 509				__func__, i);
 510			continue;
 511		}
 512		sngl_calib_start(priv, i, reg, dat + k);
 513	}
 514	return 1;
 515}
 516
 517static void tas2781_calib_stop_put(struct tasdevice_priv *priv)
 518{
 519	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
 520	int i, j;
 521
 522	for (i = 0; i < priv->ndev; i++) {
 523		struct tasdevice *tasdev = priv->tasdevice;
 524		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
 525		struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
 526
 527		if (p == NULL)
 528			continue;
 529
 530		for (j = 0; j < sum; j++) {
 531			if (p[j].val_len == 1) {
 532				if (p[j].is_locked)
 533					tasdevice_dev_write(priv, i,
 534						TAS2781_TEST_UNLOCK_REG,
 535						TAS2781_TEST_PAGE_UNLOCK);
 536				tasdevice_dev_write(priv, i, p[j].reg,
 537					p[j].val[0]);
 538			} else {
 539				if (!p[j].reg)
 540					continue;
 541				tasdevice_dev_bulk_write(priv, i, p[j].reg,
 542					p[j].val, 4);
 543			}
 544		}
 545
 546		if (priv->dspbin_typ == TASDEV_ALPHA)
 547			tasdevice_dev_bulk_write(priv, i, t->reg, t->val, 4);
 548	}
 549}
 550
 551static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol,
 552	struct snd_ctl_elem_value *ucontrol)
 553{
 554	struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg;
 555	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 556	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 557	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
 558	int i, j;
 559
 560	guard(mutex)(&tas_priv->codec_lock);
 561	if (tas_priv->chip_id != TAS2563)
 562		return -1;
 563
 564	for (i = 0; i < tas_priv->ndev; i++) {
 565		struct tasdevice *tasdev = tas_priv->tasdevice;
 566		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
 567
 568		if (p == NULL)
 569			continue;
 570		for (j = 0; j < sum; j++) {
 571			if (p[j].val_len == 1)
 572				tasdevice_dev_read(tas_priv,
 573					i, p[j].reg,
 574					(unsigned int *)&p[j].val[0]);
 575			else
 576				tasdevice_dev_bulk_read(tas_priv,
 577					i, p[j].reg, p[j].val, 4);
 578		}
 579
 580		for (j = 0; j < sum; j++) {
 581			if (p[j].val_len == 1)
 582				tasdevice_dev_write(tas_priv, i, p[j].reg,
 583					q[j].val[0]);
 584			else
 585				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
 586					q[j].val, 4);
 587		}
 588	}
 589
 590	return 1;
 591}
 592
 593static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv)
 594{
 595	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
 596	int i, j;
 597
 598	for (i = 0; i < tas_priv->ndev; i++) {
 599		struct tasdevice *tasdev = tas_priv->tasdevice;
 600		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
 601
 602		if (p == NULL)
 603			continue;
 604
 605		for (j = 0; j < sum; j++) {
 606			if (p[j].val_len == 1)
 607				tasdevice_dev_write(tas_priv, i, p[j].reg,
 608					p[j].val[0]);
 609			else
 610				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
 611					p[j].val, 4);
 612		}
 613	}
 614}
 615
 616static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,
 617	struct snd_ctl_elem_value *ucontrol)
 618{
 619	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 620	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
 621
 622	guard(mutex)(&priv->codec_lock);
 623	if (priv->chip_id == TAS2563)
 624		tas2563_calib_stop_put(priv);
 625	else
 626		tas2781_calib_stop_put(priv);
 627
 628	return 1;
 629}
 630
 631static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,
 632	struct snd_ctl_elem_value *ucontrol)
 633{
 634	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 635	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
 636	struct soc_bytes_ext *bytes_ext =
 637		(struct soc_bytes_ext *) kcontrol->private_value;
 638	struct calidata *cali_data = &priv->cali_data;
 639	struct cali_reg *p = &cali_data->cali_reg_array;
 640	unsigned char *src = ucontrol->value.bytes.data;
 641	unsigned char *dst = cali_data->data;
 642	int i = 0;
 643	int j;
 644
 645	guard(mutex)(&priv->codec_lock);
 646	if (src[0] != bytes_ext->max || src[1] != 'r') {
 647		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
 648		return 0;
 649	}
 650	for (j = 0; j < priv->ndev; j++) {
 651		if (src[17 + j * 21] != j) {
 652			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
 653			return 0;
 654		}
 655	}
 656	i += 2;
 657	priv->is_user_space_calidata = true;
 658
 659	if (priv->dspbin_typ == TASDEV_BASIC) {
 660		p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
 661		i += 3;
 662		p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
 663		i += 3;
 664		p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
 665		i += 3;
 666		p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
 667		i += 3;
 668		p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
 669		i += 3;
 670	} else {
 671		i += 15;
 672	}
 673
 674	memcpy(dst, &src[i], cali_data->total_sz);
 675	return 1;
 676}
 677
 678static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,
 679	struct snd_ctl_elem_value *ucontrol)
 680{
 681	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 682	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 683	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
 684	struct soc_bytes_ext *bytes_ext =
 685		(struct soc_bytes_ext *) kcontrol->private_value;
 686	struct tasdevice *tasdev = tas_priv->tasdevice;
 687	unsigned char *dst = ucontrol->value.bytes.data;
 688	int i, val, rc = -1;
 689
 690	dst[0] = bytes_ext->max;
 691	guard(mutex)(&tas_priv->codec_lock);
 692	for (i = 0; i < tas_priv->ndev; i++) {
 693		if (clt->addr == tasdev[i].dev_addr) {
 694			/* First byte is the device index. */
 695			dst[1] = i;
 696			rc = tasdevice_dev_read(tas_priv, i,
 697				TAS2781_RUNTIME_LATCH_RE_REG, &val);
 698			if (rc < 0)
 699				dev_err(tas_priv->dev, "%s, get value error\n",
 700					__func__);
 701			else
 702				dst[2] = val;
 703
 704			break;
 705		}
 706	}
 707
 708	return rc;
 709}
 710
 711static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,
 712	struct snd_ctl_elem_value *ucontrol)
 713{
 714	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 715	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 716	struct soc_bytes_ext *bytes_ext =
 717		(struct soc_bytes_ext *) kcontrol->private_value;
 718	unsigned char *dst = ucontrol->value.bytes.data;
 719	unsigned int reg = TAS2781_RUNTIME_RE_REG_TF;
 720
 721	if (tas_priv->chip_id == TAS2781) {
 722		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
 723		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
 724
 725		reg = TAS2781_RUNTIME_RE_REG_TF;
 726		if (tas_priv->dspbin_typ)
 727			reg = TASDEVICE_REG(p->tf_reg[0], p->tf_reg[1],
 728				p->tf_reg[2]);
 729	} else {
 730		reg = TAS2563_RUNTIME_RE_REG_TF;
 731	}
 732
 733	guard(mutex)(&tas_priv->codec_lock);
 734	dst[0] = bytes_ext->max;
 735	return calib_data_get(tas_priv, reg, &dst[1]);
 736}
 737
 738static int tasdev_re_data_get(struct snd_kcontrol *kcontrol,
 739	struct snd_ctl_elem_value *ucontrol)
 740{
 741	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 742	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 743	struct soc_bytes_ext *bytes_ext =
 744		(struct soc_bytes_ext *) kcontrol->private_value;
 745	unsigned char *dst = ucontrol->value.bytes.data;
 746	unsigned int reg = TAS2781_RUNTIME_RE_REG;
 747
 748	if (tas_priv->chip_id == TAS2781) {
 749		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
 750		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
 751
 752		if (tas_priv->dspbin_typ)
 753			reg = TASDEVICE_REG(p->r0_reg[0], p->r0_reg[1],
 754				p->r0_reg[2]);
 755	} else {
 756		reg = TAS2563_RUNTIME_RE_REG;
 757	}
 758
 759	guard(mutex)(&tas_priv->codec_lock);
 760	dst[0] = bytes_ext->max;
 761	return calib_data_get(tas_priv, reg, &dst[1]);
 762}
 763
 764static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,
 765	struct snd_ctl_elem_value *ucontrol)
 766{
 767	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 768	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 769	struct calidata *cali_data = &tas_priv->cali_data;
 770	struct soc_bytes_ext *bytes_ext =
 771		(struct soc_bytes_ext *) kcontrol->private_value;
 772	unsigned char *dst = ucontrol->value.bytes.data;
 773	unsigned int reg;
 774
 775	guard(mutex)(&tas_priv->codec_lock);
 776
 777	if (tas_priv->chip_id == TAS2563)
 778		reg = TAS2563_PRM_R0_REG;
 779	else if (cali_data->cali_reg_array.r0_reg)
 780		reg = cali_data->cali_reg_array.r0_reg;
 781	else
 782		return -1;
 783	dst[0] = bytes_ext->max;
 784	return calib_data_get(tas_priv, reg, &dst[1]);
 785}
 786
 787static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,
 788	struct snd_ctl_elem_value *ucontrol)
 789{
 790	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 791	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 792	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
 793	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
 794	struct soc_bytes_ext *bytes_ext =
 795		(struct soc_bytes_ext *) kcontrol->private_value;
 796	unsigned char *dst = ucontrol->value.bytes.data;
 797	unsigned int reg = TASDEVICE_XM_A1_REG;
 798
 799	if (tas_priv->dspbin_typ)
 800		reg = TASDEVICE_REG(p->a1_reg[0], p->a1_reg[1], p->a1_reg[2]);
 801
 802	guard(mutex)(&tas_priv->codec_lock);
 803	dst[0] = bytes_ext->max;
 804	return calib_data_get(tas_priv, reg, &dst[1]);
 805}
 806
 807static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,
 808	struct snd_ctl_elem_value *ucontrol)
 809{
 810	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
 811	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
 812	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
 813	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
 814	struct soc_bytes_ext *bytes_ext =
 815		(struct soc_bytes_ext *) kcontrol->private_value;
 816	unsigned char *dst = ucontrol->value.bytes.data;
 817	unsigned int reg = TASDEVICE_XM_A2_REG;
 818
 819	if (tas_priv->dspbin_typ)
 820		reg = TASDEVICE_REG(p->a2_reg[0], p->a2_reg[1], p->a2_reg[2]);
 821
 822	guard(mutex)(&tas_priv->codec_lock);
 823	dst[0] = bytes_ext->max;
 824	return calib_data_get(tas_priv, reg, &dst[1]);
 825}
 826
 827static int tasdev_nop_get(
 828	struct snd_kcontrol *kcontrol,
 829	struct snd_ctl_elem_value *ucontrol)
 830{
 831	return 0;
 832}
 833
 834static int tasdevice_digital_gain_get(
 835	struct snd_kcontrol *kcontrol,
 836	struct snd_ctl_elem_value *ucontrol)
 837{
 838	struct soc_mixer_control *mc =
 839		(struct soc_mixer_control *)kcontrol->private_value;
 840	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 841	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
 842	unsigned int l = 0, r = mc->max;
 843	unsigned int target, ar_mid, mid, ar_l, ar_r;
 844	unsigned int reg = mc->reg;
 845	unsigned char data[4];
 846	int ret;
 847
 848	mutex_lock(&tas_dev->codec_lock);
 849	/* Read the primary device */
 850	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
 851	if (ret) {
 852		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
 853		goto out;
 854	}
 855
 856	target = get_unaligned_be32(&data[0]);
 857
 858	while (r > 1 + l) {
 859		mid = (l + r) / 2;
 860		ar_mid = get_unaligned_be32(tas_dev->dvc_tlv_table[mid]);
 861		if (target < ar_mid)
 862			r = mid;
 863		else
 864			l = mid;
 865	}
 866
 867	ar_l = get_unaligned_be32(tas_dev->dvc_tlv_table[l]);
 868	ar_r = get_unaligned_be32(tas_dev->dvc_tlv_table[r]);
 869
 870	/* find out the member same as or closer to the current volume */
 871	ucontrol->value.integer.value[0] =
 872		abs(target - ar_l) <= abs(target - ar_r) ? l : r;
 873out:
 874	mutex_unlock(&tas_dev->codec_lock);
 875	return 0;
 876}
 877
 878static int tasdevice_digital_gain_put(
 879	struct snd_kcontrol *kcontrol,
 880	struct snd_ctl_elem_value *ucontrol)
 881{
 882	struct soc_mixer_control *mc =
 883		(struct soc_mixer_control *)kcontrol->private_value;
 884	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 885	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
 886	int vol = ucontrol->value.integer.value[0];
 887	int status = 0, max = mc->max, rc = 1;
 888	int i, ret;
 889	unsigned int reg = mc->reg;
 890	unsigned int volrd, volwr;
 891	unsigned char data[4];
 892
 893	vol = clamp(vol, 0, max);
 894	mutex_lock(&tas_dev->codec_lock);
 895	/* Read the primary device */
 896	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
 897	if (ret) {
 898		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
 899		rc = -1;
 900		goto out;
 901	}
 902
 903	volrd = get_unaligned_be32(&data[0]);
 904	volwr = get_unaligned_be32(tas_dev->dvc_tlv_table[vol]);
 905
 906	if (volrd == volwr) {
 907		rc = 0;
 908		goto out;
 909	}
 910
 911	for (i = 0; i < tas_dev->ndev; i++) {
 912		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
 913			(unsigned char *)tas_dev->dvc_tlv_table[vol], 4);
 914		if (ret) {
 915			dev_err(tas_dev->dev,
 916				"%s, set digital vol error in dev %d\n",
 917				__func__, i);
 918			status |= BIT(i);
 919		}
 920	}
 921
 922	if (status)
 923		rc = -1;
 924out:
 925	mutex_unlock(&tas_dev->codec_lock);
 926	return rc;
 927}
 928
 929static const struct snd_kcontrol_new tasdevice_cali_controls[] = {
 930	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
 931		tasdev_nop_get, tasdev_calib_stop_put),
 932	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),
 933	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),
 934	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),
 935	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),
 936	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),
 937};
 938
 939static const struct snd_kcontrol_new tas2x20_snd_controls[] = {
 940	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2X20_AMP_LEVEL,
 941		0, 0, 42, 1, tas2781_amp_getvol,
 942		tas2781_amp_putvol, tas2x20_amp_tlv),
 943	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2X20_DVC_LEVEL,
 944		0, 0, ARRAY_SIZE(tas2x20_dvc_table) - 1, 0,
 945		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
 946		tas2x20_dvc_tlv),
 947};
 948
 949static const struct snd_kcontrol_new tas2781_snd_controls[] = {
 950	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
 951		1, 0, 20, 0, tas2781_amp_getvol,
 952		tas2781_amp_putvol, tas2781_amp_tlv),
 953	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2781_DVC_LVL,
 954		0, 0, 200, 1, tas2781_digital_getvol,
 955		tas2781_digital_putvol, tas2781_dvc_tlv),
 956};
 957
 958static const struct snd_kcontrol_new tas5825_snd_controls[] = {
 959	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
 960		0, 0, 31, 1, tas2781_amp_getvol,
 961		tas2781_amp_putvol, tas5825_amp_tlv),
 962	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
 963		0, 0, 254, 1, tas2781_amp_getvol,
 964		tas2781_amp_putvol, tas5825_dvc_tlv),
 965};
 966
 967static const struct snd_kcontrol_new tas2781_cali_controls[] = {
 968	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),
 969};
 970
 971static const struct snd_kcontrol_new tas2563_snd_controls[] = {
 972	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
 973		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
 974		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
 975		tas2563_dvc_tlv),
 976};
 977
 978static const struct snd_kcontrol_new tas2563_cali_controls[] = {
 979	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
 980		tasdev_nop_get, tas2563_calib_start_put),
 981};
 982
 983static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
 984		struct snd_ctl_elem_value *ucontrol)
 985{
 986	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 987	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
 988	int ret = 0;
 989
 990	if (tas_priv->rcabin.profile_cfg_id !=
 991		ucontrol->value.integer.value[0]) {
 992		tas_priv->rcabin.profile_cfg_id =
 993			ucontrol->value.integer.value[0];
 994		ret = 1;
 995	}
 996
 997	return ret;
 998}
 999
1000static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol,
1001			struct snd_ctl_elem_info *uinfo)
1002{
1003	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1004	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1005
1006	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1007	uinfo->count = 1;
1008	uinfo->value.integer.min = 0;
1009	uinfo->value.integer.max = tas_priv->ndev - 1;
1010
1011	return 0;
1012}
1013
1014static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,
1015			struct snd_ctl_elem_info *uinfo)
1016{
1017	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1018	uinfo->count = 1;
1019	uinfo->value.integer.min = TAS2020;
1020	uinfo->value.integer.max = TAS_OTHERS;
1021
1022	return 0;
1023}
1024
1025static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
1026			struct snd_ctl_elem_info *uinfo)
1027{
1028	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1029	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1030	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1031
1032	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1033	uinfo->count = 1;
1034	uinfo->value.integer.min = 0;
1035	uinfo->value.integer.max = (int)tas_fw->nr_programs;
1036
1037	return 0;
1038}
1039
1040static int tasdevice_info_configurations(
1041	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1042{
1043	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1044	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1045	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1046
1047	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1048	uinfo->count = 1;
1049	uinfo->value.integer.min = 0;
1050	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
1051
1052	return 0;
1053}
1054
1055static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
1056			struct snd_ctl_elem_info *uinfo)
1057{
1058	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1059	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1060
1061	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1062	uinfo->count = 1;
1063	uinfo->value.integer.min = 0;
1064	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
1065
1066	return 0;
1067}
1068
1069static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
1070			struct snd_ctl_elem_value *ucontrol)
1071{
1072	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1073	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1074
1075	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
1076
1077	return 0;
1078}
1079
1080static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,
1081			struct snd_ctl_elem_value *ucontrol)
1082{
1083	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1084	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1085
1086	ucontrol->value.integer.value[0] = tas_priv->chip_id;
1087
1088	return 0;
1089}
1090
1091static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
1092{
1093	struct snd_kcontrol_new *prof_ctrls;
1094	int nr_controls = 1;
1095	int mix_index = 0;
1096	int ret;
1097	char *name;
1098
1099	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1100		sizeof(prof_ctrls[0]), GFP_KERNEL);
1101	if (!prof_ctrls) {
1102		ret = -ENOMEM;
1103		goto out;
1104	}
1105
1106	/* Create a mixer item for selecting the active profile */
1107	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);
1108	if (!name) {
1109		ret = -ENOMEM;
1110		goto out;
1111	}
1112	prof_ctrls[mix_index].name = name;
1113	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1114	prof_ctrls[mix_index].info = tasdevice_info_profile;
1115	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
1116	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
1117	mix_index++;
1118
1119	ret = snd_soc_add_component_controls(tas_priv->codec,
1120		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
1121
1122out:
1123	return ret;
1124}
1125
1126static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
1127	struct snd_ctl_elem_value *ucontrol)
1128{
1129	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1130	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1131
1132	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
1133
1134	return 0;
1135}
1136
1137static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
1138	struct snd_ctl_elem_value *ucontrol)
1139{
1140	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1141	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1142	unsigned int nr_program = ucontrol->value.integer.value[0];
1143	int ret = 0;
1144
1145	if (tas_priv->cur_prog != nr_program) {
1146		tas_priv->cur_prog = nr_program;
1147		ret = 1;
1148	}
1149
1150	return ret;
1151}
1152
1153static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
1154	struct snd_ctl_elem_value *ucontrol)
1155{
1156
1157	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1158	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1159
1160	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
1161
1162	return 0;
1163}
1164
1165static int tasdevice_configuration_put(
1166	struct snd_kcontrol *kcontrol,
1167	struct snd_ctl_elem_value *ucontrol)
1168{
1169	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1170	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1171	unsigned int nr_configuration = ucontrol->value.integer.value[0];
1172	int ret = 0;
1173
1174	if (tas_priv->cur_conf != nr_configuration) {
1175		tas_priv->cur_conf = nr_configuration;
1176		ret = 1;
1177	}
1178
1179	return ret;
1180}
1181
1182static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,
1183	struct snd_ctl_elem_value *ucontrol)
1184{
1185	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1186	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1187	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
1188	struct tasdevice *tasdev = tas_priv->tasdevice;
1189	int i;
1190
1191	for (i = 0; i < tas_priv->ndev; i++) {
1192		if (clt->addr == tasdev[i].dev_addr) {
1193			ucontrol->value.integer.value[0] = i;
1194			return 0;
1195		}
1196	}
1197
1198	return -1;
1199}
1200
1201static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,
1202	struct snd_ctl_elem_value *ucontrol)
1203{
1204	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1205	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1206	int dev_id = ucontrol->value.integer.value[0];
1207	int max = tas_priv->ndev - 1;
1208
1209	dev_id = clamp(dev_id, 0, max);
1210
1211	guard(mutex)(&tas_priv->codec_lock);
1212	return tasdev_chn_switch(tas_priv, dev_id);
1213}
1214
1215static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)
1216{
1217	struct snd_kcontrol_new *dsp_ctrls;
1218	char *active_dev_num, *chip_id, *fw_load;
1219	char *conf_name, *prog_name;
1220	int nr_controls = 5;
1221	int mix_index = 0;
1222
1223	/* Alloc kcontrol via devm_kzalloc, which don't manually
1224	 * free the kcontrol
1225	 */
1226	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1227		sizeof(dsp_ctrls[0]), GFP_KERNEL);
1228	if (!dsp_ctrls)
1229		return -ENOMEM;
1230
1231	/* Create mixer items for selecting the active Program and Config */
1232	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",
1233		GFP_KERNEL);
1234	if (!prog_name)
1235		return -ENOMEM;
1236
1237	dsp_ctrls[mix_index].name = prog_name;
1238	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1239	dsp_ctrls[mix_index].info = tasdevice_info_programs;
1240	dsp_ctrls[mix_index].get = tasdevice_program_get;
1241	dsp_ctrls[mix_index].put = tasdevice_program_put;
1242	mix_index++;
1243
1244	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",
1245		GFP_KERNEL);
1246	if (!conf_name)
1247		return -ENOMEM;
1248
1249	dsp_ctrls[mix_index].name = conf_name;
1250	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1251	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
1252	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
1253	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
1254	mix_index++;
1255
1256	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",
1257		GFP_KERNEL);
1258	if (!active_dev_num)
1259		return -ENOMEM;
1260
1261	dsp_ctrls[mix_index].name = active_dev_num;
1262	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1263	dsp_ctrls[mix_index].info = tasdevice_info_active_num;
1264	dsp_ctrls[mix_index].get = tasdevice_active_num_get;
1265	dsp_ctrls[mix_index].put = tasdevice_active_num_put;
1266	mix_index++;
1267
1268	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);
1269	if (!chip_id)
1270		return -ENOMEM;
1271
1272	dsp_ctrls[mix_index].name = chip_id;
1273	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1274	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;
1275	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;
1276	mix_index++;
1277
1278	fw_load = devm_kstrdup(tas_priv->dev, "Speaker Force Firmware Load",
1279		GFP_KERNEL);
1280	if (!fw_load)
1281		return -ENOMEM;
1282
1283	dsp_ctrls[mix_index].name = fw_load;
1284	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1285	dsp_ctrls[mix_index].info = snd_soc_info_bool_ext;
1286	dsp_ctrls[mix_index].put = tasdev_force_fwload_put;
1287	dsp_ctrls[mix_index].get = tasdev_force_fwload_get;
1288	dsp_ctrls[mix_index].private_value = 0UL;
1289	mix_index++;
1290
1291	return snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
1292		nr_controls < mix_index ? nr_controls : mix_index);
1293}
1294
1295static void cali_reg_update(struct bulk_reg_val *p,
1296	struct fct_param_address *t)
1297{
1298	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
1299	int reg, j;
1300
1301	for (j = 0; j < sum; j++) {
1302		switch (tas2781_cali_start_reg[j].reg) {
1303		case 0:
1304			reg = TASDEVICE_REG(t->thr[0], t->thr[1], t->thr[2]);
1305			break;
1306		case TAS2781_PRM_PLT_FLAG_REG:
1307			reg = TASDEVICE_REG(t->plt_flg[0], t->plt_flg[1],
1308				t->plt_flg[2]);
1309			break;
1310		case TAS2781_PRM_SINEGAIN_REG:
1311			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1312				t->sin_gn[2]);
1313			break;
1314		case TAS2781_PRM_SINEGAIN2_REG:
1315			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1316				t->sin_gn[2]);
1317			break;
1318		default:
1319			reg = 0;
1320			break;
1321		}
1322		if (reg)
1323			p[j].reg = reg;
1324	}
1325}
1326
1327static void alpa_cali_update(struct bulk_reg_val *p,
1328	struct fct_param_address *t)
1329{
1330	p->is_locked = false;
1331	p->reg = TASDEVICE_REG(t->thr2[0], t->thr2[1], t->thr2[2]);
1332	p->val_len = 4;
1333}
1334
1335static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)
1336{
1337	struct calidata *cali_data = &priv->cali_data;
1338	struct tasdevice *tasdev = priv->tasdevice;
1339	struct tasdevice_fw *fmw = priv->fmw;
1340	struct soc_bytes_ext *ext_cali_data;
1341	struct snd_kcontrol_new *cali_ctrls;
1342	unsigned int nctrls;
1343	char *cali_name;
1344	int rc, i;
1345
1346	rc = snd_soc_add_component_controls(priv->codec,
1347		tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls));
1348	if (rc < 0) {
1349		dev_err(priv->dev, "%s: Add cali controls err rc = %d",
1350			__func__, rc);
1351		return rc;
1352	}
1353
1354	if (priv->chip_id == TAS2781) {
1355		struct fct_param_address *t = &(fmw->fct_par_addr);
1356
1357		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;
1358		nctrls = ARRAY_SIZE(tas2781_cali_controls);
1359		for (i = 0; i < priv->ndev; i++) {
1360			struct bulk_reg_val *p;
1361
1362			p = tasdev[i].cali_data_backup =
1363				kmemdup(tas2781_cali_start_reg,
1364				sizeof(tas2781_cali_start_reg), GFP_KERNEL);
1365			if (!tasdev[i].cali_data_backup)
1366				return -ENOMEM;
1367			if (priv->dspbin_typ) {
1368				cali_reg_update(p, t);
1369				if (priv->dspbin_typ == TASDEV_ALPHA) {
1370					p = &tasdev[i].alp_cali_bckp;
1371					alpa_cali_update(p, t);
1372				}
1373			}
1374		}
1375	} else {
1376		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;
1377		nctrls = ARRAY_SIZE(tas2563_cali_controls);
1378		for (i = 0; i < priv->ndev; i++) {
1379			tasdev[i].cali_data_backup =
1380				kmemdup(tas2563_cali_start_reg,
1381				sizeof(tas2563_cali_start_reg), GFP_KERNEL);
1382			if (!tasdev[i].cali_data_backup)
1383				return -ENOMEM;
1384		}
1385	}
1386
1387	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);
1388	if (rc < 0) {
1389		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",
1390			__func__, rc);
1391		return rc;
1392	}
1393
1394	/* index for cali_ctrls */
1395	i = 0;
1396	if (priv->chip_id == TAS2781)
1397		nctrls = 2;
1398	else
1399		nctrls = 1;
1400
1401	/*
1402	 * Alloc kcontrol via devm_kzalloc(), which don't manually
1403	 * free the kcontrol.
1404	 */
1405	cali_ctrls = devm_kcalloc(priv->dev, nctrls,
1406		sizeof(cali_ctrls[0]), GFP_KERNEL);
1407	if (!cali_ctrls)
1408		return -ENOMEM;
1409
1410	ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data),
1411		GFP_KERNEL);
1412	if (!ext_cali_data)
1413		return -ENOMEM;
1414
1415	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",
1416		GFP_KERNEL);
1417	if (!cali_name)
1418		return -ENOMEM;
1419	/* the number of calibrated data per tas2563/tas2781 */
1420	cali_data->cali_dat_sz_per_dev = 20;
1421	/*
1422	 * Data structure for tas2563/tas2781 calibrated data:
1423	 *	Pkg len (1 byte)
1424	 *	Reg id (1 byte, constant 'r')
1425	 *	book, page, register array for calibrated data (15 bytes)
1426	 *	for (i = 0; i < Device-Sum; i++) {
1427	 *		Device #i index_info (1 byte)
1428	 *		Calibrated data for Device #i (20 bytes)
1429	 *	}
1430	 */
1431	ext_cali_data->max = priv->ndev *
1432		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;
1433	priv->cali_data.total_sz = priv->ndev *
1434		(cali_data->cali_dat_sz_per_dev + 1);
1435	cali_ctrls[i].name = cali_name;
1436	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1437	cali_ctrls[i].info = snd_soc_bytes_info_ext;
1438	cali_ctrls[i].get = tasdev_cali_data_get;
1439	cali_ctrls[i].put = tasdev_cali_data_put;
1440	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;
1441	i++;
1442
1443	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,
1444		GFP_KERNEL);
1445	if (!cali_data->data)
1446		return -ENOMEM;
1447
1448	if (priv->chip_id == TAS2781) {
1449		struct soc_bytes_ext *ext_cali_start;
1450		char *cali_start_name;
1451
1452		ext_cali_start = devm_kzalloc(priv->dev,
1453			sizeof(*ext_cali_start), GFP_KERNEL);
1454		if (!ext_cali_start)
1455			return -ENOMEM;
1456
1457		cali_start_name = devm_kstrdup(priv->dev,
1458			"Calibration Start", GFP_KERNEL);
1459		if (!cali_start_name)
1460			return -ENOMEM;
1461		/*
1462		 * package structure for tas2781 ftc start:
1463		 *	Pkg len (1 byte)
1464		 *	Reg id (1 byte, constant 'r')
1465		 *	book, page, register for pilot threshold, pilot tone
1466		 *		and sine gain (12 bytes)
1467		 *	for (i = 0; i < Device-Sum; i++) {
1468		 *		Device #i index_info (1 byte)
1469		 *		Sine gain for Device #i (8 bytes)
1470		 *	}
1471		 */
1472		ext_cali_start->max = 14 + priv->ndev * 9;
1473		cali_ctrls[i].name = cali_start_name;
1474		cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1475		cali_ctrls[i].info = snd_soc_bytes_info_ext;
1476		cali_ctrls[i].put = tas2781_calib_start_put;
1477		cali_ctrls[i].get = tasdev_nop_get;
1478		cali_ctrls[i].private_value = (unsigned long)ext_cali_start;
1479		i++;
1480	}
1481
1482	return snd_soc_add_component_controls(priv->codec, cali_ctrls,
1483		nctrls < i ? nctrls : i);
1484}
1485
1486#ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1487/*
1488 * This debugfs node is a bridge to the acoustic tuning application
1489 * tool which can tune the chips' acoustic effect.
1490 *
1491 * package structure for PPC3 communications:
1492 *	Pkg len (1 byte)
1493 *	Pkg id (1 byte, 'r' or 'w')
1494 *	Dev id (1 byte, i2c address)
1495 *	Book id (1 byte)
1496 *	Page id (1 byte)
1497 *	Reg id (1 byte)
1498 *	switch (pkg id) {
1499 *	case 'w':
1500 *		1 byte, length of data to read
1501 *	case 'r':
1502 *		data payload (1~128 bytes)
1503 *	}
1504 */
1505static ssize_t acoustic_ctl_read(struct file *file, char __user *to,
1506	size_t count, loff_t *ppos)
1507{
1508	struct snd_soc_component *comp = file->private_data;
1509	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
1510	struct acoustic_data *p = &tas_priv->acou_data;
1511	int ret = -1;
1512
1513	if (p->id == 'r' && p->len == count && count <= sizeof(*p))
1514		ret = simple_read_from_buffer(to, count, ppos, p, p->len);
1515	else
1516		dev_err(tas_priv->dev, "Not ready for get.\n");
1517	return ret;
1518}
1519
1520static ssize_t acoustic_ctl_write(struct file *file,
1521	const char __user *from, size_t count, loff_t *ppos)
1522{
1523	struct snd_soc_component *comp = file->private_data;
1524	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
1525	struct acoustic_data *p = &priv->acou_data;
1526	unsigned int max_pkg_len = sizeof(*p);
1527	unsigned char *src;
1528	int j, len, reg, val;
1529	unsigned short chn;
1530	int ret = -1;
1531
1532	if (count > sizeof(*p)) {
1533		dev_err(priv->dev, "count(%u) is larger than max(%u).\n",
1534			(unsigned int)count, max_pkg_len);
1535		return ret;
1536	}
1537
1538	src = memdup_user(from, count);
1539	if (IS_ERR(src))
1540		return PTR_ERR(src);
1541
1542	if (src[0] > max_pkg_len && src[0] != count) {
1543		dev_err(priv->dev, "pkg(%u), max(%u), count(%u) mismatch.\n",
1544			src[0], max_pkg_len, (unsigned int)count);
1545		ret = 0;
1546		goto exit;
1547	}
1548
1549	switch (src[1]) {
1550	case 'r':
1551		/* length of data to read */
1552		len = src[6];
1553		break;
1554	case 'w':
1555		/* Skip 6 bytes for package type and register address */
1556		len = src[0] - 6;
1557		break;
1558	default:
1559		dev_err(priv->dev, "%s Wrong code %02x.\n", __func__, src[1]);
1560		ret = 0;
1561		goto exit;
1562	}
1563
1564	if (len < 1) {
1565		dev_err(priv->dev, "pkg fmt invalid %02x.\n", len);
1566		ret = 0;
1567		goto exit;
1568	}
1569
1570	for (j = 0; j < priv->ndev; j++)
1571		if (src[2] == priv->tasdevice[j].dev_addr) {
1572			chn = j;
1573			break;
1574		}
1575	if (j >= priv->ndev) {
1576		dev_err(priv->dev, "no such device 0x%02x.\n", src[2]);
1577		ret = 0;
1578		goto exit;
1579	}
1580
1581	reg = TASDEVICE_REG(src[3], src[4], src[5]);
1582
1583	guard(mutex)(&priv->codec_lock);
1584
1585	if (src[1] == 'w') {
1586		if (len > 1)
1587			ret = tasdevice_dev_bulk_write(priv, chn, reg,
1588				 &src[6], len);
1589		else
1590			ret = tasdevice_dev_write(priv, chn, reg, src[6]);
1591	} else {
1592		struct acoustic_data *p = &priv->acou_data;
1593
1594		memcpy(p, src, 6);
1595		if (len > 1) {
1596			ret = tasdevice_dev_bulk_read(priv, chn, reg,
1597				p->data, len);
1598		} else {
1599			ret = tasdevice_dev_read(priv, chn, reg, &val);
1600			p->data[0] = val;
1601		}
1602		p->len = len + 6;
1603	}
1604
1605	if (ret)
1606		dev_err(priv->dev, "i2c communication error.\n");
1607	else
1608		ret = count;
1609exit:
1610	kfree(src);
1611	return ret;
1612}
1613
1614static const struct file_operations acoustic_ctl_fops = {
1615	.open = simple_open,
1616	.read = acoustic_ctl_read,
1617	.write = acoustic_ctl_write,
1618};
1619#endif
1620
1621static void tasdevice_fw_ready(const struct firmware *fmw,
1622	void *context)
1623{
1624	struct tasdevice_priv *tas_priv = context;
1625#ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1626	struct snd_soc_component *comp = tas_priv->codec;
1627	struct dentry *debugfs_root = comp->debugfs_root;
1628	char *acoustic_debugfs_node;
1629#endif
1630	int ret = 0;
1631	int i;
1632
1633	mutex_lock(&tas_priv->codec_lock);
1634
1635	ret = tasdevice_rca_parser(tas_priv, fmw);
1636	if (ret) {
1637		tasdevice_config_info_remove(tas_priv);
1638		goto out;
1639	}
1640	tasdevice_create_control(tas_priv);
1641
1642	tasdevice_dsp_remove(tas_priv);
1643	tasdevice_calbin_remove(tas_priv);
1644	/*
1645	 * The baseline is the RCA-only case, and then the code attempts to
1646	 * load DSP firmware but in case of failures just keep going, i.e.
1647	 * failing to load DSP firmware is NOT an error.
1648	 */
1649	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
1650	/* There is no DSP firmware required for TAS2118/2X20/257X. */
1651	switch (tas_priv->chip_id) {
1652	case TAS2020:
1653	case TAS2118:
1654	case TAS2120:
1655	case TAS2320:
1656	case TAS2568:
1657	case TAS2570:
1658	case TAS2572:
1659	case TAS2574:
1660		goto out;
1661	}
1662	if (tas_priv->name_prefix)
1663		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",
1664			tas_priv->name_prefix, tas_priv->dev_name);
1665	else
1666		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
1667			tas_priv->dev_name);
1668	ret = tasdevice_dsp_parser(tas_priv);
1669	if (ret) {
1670		dev_err(tas_priv->dev, "dspfw load %s error\n",
1671			tas_priv->coef_binaryname);
1672		goto out;
1673	}
1674
1675	/*
1676	 * If no dsp-related kcontrol created, the dsp resource will be freed.
1677	 */
1678	ret = tasdevice_dsp_create_ctrls(tas_priv);
1679	if (ret) {
1680		dev_err(tas_priv->dev, "dsp controls error\n");
1681		goto out;
1682	}
1683	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
1684
1685	/* There is no calibration required for TAS58XX. */
1686	if (tas_priv->chip_id < TAS5802) {
1687		ret = tasdevice_create_cali_ctrls(tas_priv);
1688		if (ret) {
1689			dev_err(tas_priv->dev, "cali controls error\n");
1690			goto out;
1691		}
1692		/* If calibrated data occurs error, dsp will still works
1693		 * with default calibrated data inside algo.
1694		 */
1695		for (i = 0; i < tas_priv->ndev; i++) {
1696			if (tas_priv->name_prefix)
1697				scnprintf(tas_priv->cal_binaryname[i], 64,
1698					  "%s-%s_cal_0x%02x.bin",
1699					  tas_priv->name_prefix,
1700					  tas_priv->dev_name,
1701					  tas_priv->tasdevice[i].dev_addr);
1702			else
1703				scnprintf(tas_priv->cal_binaryname[i], 64,
1704					  "%s_cal_0x%02x.bin",
1705					  tas_priv->dev_name,
1706					  tas_priv->tasdevice[i].dev_addr);
1707			ret = tas2781_load_calibration(tas_priv,
1708				tas_priv->cal_binaryname[i], i);
1709			if (ret != 0)
1710				dev_err(tas_priv->dev,
1711					"%s: load %s error, keep default.\n",
1712					__func__, tas_priv->cal_binaryname[i]);
1713		}
1714	}
1715
1716	tasdevice_prmg_load(tas_priv, 0);
1717	tas_priv->cur_prog = 0;
1718
1719	/* Init common setting for different audio profiles */
1720	if (tas_priv->rcabin.init_profile_id >= 0)
1721		tasdevice_select_cfg_blk(tas_priv,
1722			tas_priv->rcabin.init_profile_id,
1723			TASDEVICE_BIN_BLK_PRE_POWER_UP);
1724
1725#ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1726	if (tas_priv->name_prefix)
1727		acoustic_debugfs_node = devm_kasprintf(tas_priv->dev,
1728			GFP_KERNEL, "%s_acoustic_ctl", tas_priv->name_prefix);
1729	else
1730		acoustic_debugfs_node = devm_kstrdup(tas_priv->dev,
1731			"acoustic_ctl", GFP_KERNEL);
1732	debugfs_create_file(acoustic_debugfs_node, 0644, debugfs_root,
1733		comp, &acoustic_ctl_fops);
1734#endif
1735out:
1736	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
1737		switch (tas_priv->chip_id) {
1738		case TAS2563:
1739		case TAS2781:
1740		case TAS5802:
1741		case TAS5806M:
1742		case TAS5806MD:
1743		case TAS5815:
1744		case TAS5822:
1745		case TAS5825:
1746		case TAS5827:
1747		case TAS5828:
1748		case TAS5830:
1749			/* If DSP FW fail, DSP kcontrol won't be created. */
1750			tasdevice_dsp_remove(tas_priv);
1751		}
1752	}
1753	mutex_unlock(&tas_priv->codec_lock);
1754	release_firmware(fmw);
1755}
1756
1757static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
1758			struct snd_kcontrol *kcontrol, int event)
1759{
1760	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
1761	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1762	int state = 0;
1763
1764	/* Codec Lock Hold */
1765	mutex_lock(&tas_priv->codec_lock);
1766	if (event == SND_SOC_DAPM_PRE_PMD)
1767		state = 1;
1768	tasdevice_tuning_switch(tas_priv, state);
1769	/* Codec Lock Release*/
1770	mutex_unlock(&tas_priv->codec_lock);
1771
1772	return 0;
1773}
1774
1775static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
1776	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
1777	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
1778		0, 0, tasdevice_dapm_event,
1779		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1780	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
1781	SND_SOC_DAPM_OUTPUT("OUT"),
1782	SND_SOC_DAPM_INPUT("DMIC"),
1783};
1784
1785static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
1786	{"SPK", NULL, "ASI"},
1787	{"OUT", NULL, "SPK"},
1788	{"ASI OUT", NULL, "DMIC"},
1789};
1790
1791static int tasdevice_startup(struct snd_pcm_substream *substream,
1792						struct snd_soc_dai *dai)
1793{
1794	struct snd_soc_component *codec = dai->component;
1795	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1796
1797	switch (tas_priv->fw_state) {
1798	case TASDEVICE_RCA_FW_OK:
1799	case TASDEVICE_DSP_FW_ALL_OK:
1800		return 0;
1801	default:
1802		return -EINVAL;
1803	}
1804}
1805
1806static int tasdevice_hw_params(struct snd_pcm_substream *substream,
1807	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1808{
1809	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
1810	unsigned int slot_width;
1811	unsigned int fsrate;
1812	int bclk_rate;
1813
1814	fsrate = params_rate(params);
1815	switch (fsrate) {
1816	case 48000:
1817	case 44100:
1818		break;
1819	default:
1820		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
1821			__func__, fsrate);
1822		return -EINVAL;
1823	}
1824
1825	slot_width = params_width(params);
1826	switch (slot_width) {
1827	case 16:
1828	case 20:
1829	case 24:
1830	case 32:
1831		break;
1832	default:
1833		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
1834			__func__, slot_width);
1835		return -EINVAL;
1836	}
1837
1838	bclk_rate = snd_soc_params_to_bclk(params);
1839	if (bclk_rate < 0) {
1840		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
1841			__func__, bclk_rate);
1842		return bclk_rate;
1843	}
1844
1845	return 0;
1846}
1847
1848static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1849	int clk_id, unsigned int freq, int dir)
1850{
1851	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
1852
1853	tas_priv->sysclk = freq;
1854
1855	return 0;
1856}
1857
1858static const struct snd_soc_dai_ops tasdevice_dai_ops = {
1859	.startup = tasdevice_startup,
1860	.hw_params = tasdevice_hw_params,
1861	.set_sysclk = tasdevice_set_dai_sysclk,
1862};
1863
1864static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
1865	{
1866		.name = "tasdev_codec",
1867		.id = 0,
1868		.playback = {
1869			.stream_name = "Playback",
1870			.channels_min = 1,
1871			.channels_max = 4,
1872			.rates	 = TASDEVICE_RATES,
1873			.formats	= TASDEVICE_FORMATS,
1874		},
1875		.capture = {
1876			.stream_name = "Capture",
1877			.channels_min = 1,
1878			.channels_max = 4,
1879			.rates	 = TASDEVICE_RATES,
1880			.formats	= TASDEVICE_FORMATS,
1881		},
1882		.ops = &tasdevice_dai_ops,
1883		.symmetric_rate = 1,
1884	},
1885};
1886
1887static int tasdevice_codec_probe(struct snd_soc_component *codec)
1888{
1889	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1890	struct snd_kcontrol_new *p;
1891	unsigned int size;
1892	int rc;
1893
1894	switch (tas_priv->chip_id) {
1895	case TAS2020:
1896	case TAS2118:
1897	case TAS2120:
1898	case TAS2320:
1899	case TAS2568:
1900	case TAS2570:
1901	case TAS2572:
1902	case TAS2574:
1903		p = (struct snd_kcontrol_new *)tas2x20_snd_controls;
1904		size = ARRAY_SIZE(tas2x20_snd_controls);
1905		tas_priv->dvc_tlv_table = tas2x20_dvc_table;
1906		break;
1907	case TAS2781:
1908		p = (struct snd_kcontrol_new *)tas2781_snd_controls;
1909		size = ARRAY_SIZE(tas2781_snd_controls);
1910		break;
1911	case TAS5802:
1912	case TAS5806M:
1913	case TAS5806MD:
1914	case TAS5815:
1915	case TAS5822:
1916	case TAS5825:
1917	case TAS5827:
1918	case TAS5828:
1919	case TAS5830:
1920		p = (struct snd_kcontrol_new *)tas5825_snd_controls;
1921		size = ARRAY_SIZE(tas5825_snd_controls);
1922		break;
1923	default:
1924		p = (struct snd_kcontrol_new *)tas2563_snd_controls;
1925		size = ARRAY_SIZE(tas2563_snd_controls);
1926		tas_priv->dvc_tlv_table = tas2563_dvc_table;
1927		break;
1928	}
1929
1930	rc = snd_soc_add_component_controls(codec, p, size);
1931	if (rc < 0) {
1932		dev_err(tas_priv->dev, "%s: Add control err rc = %d",
1933			__func__, rc);
1934		return rc;
1935	}
1936
1937	tas_priv->name_prefix = codec->name_prefix;
1938	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
1939}
1940
1941static void tasdevice_deinit(void *context)
1942{
1943	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
1944	struct tasdevice *tasdev = tas_priv->tasdevice;
1945	int i;
1946
1947	for (i = 0; i < tas_priv->ndev; i++)
1948		kfree(tasdev[i].cali_data_backup);
1949
1950	tasdevice_config_info_remove(tas_priv);
1951	tasdevice_dsp_remove(tas_priv);
1952	tasdevice_calbin_remove(tas_priv);
1953	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
1954}
1955
1956static void tasdevice_codec_remove(struct snd_soc_component *codec)
1957{
1958	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1959
1960	tasdevice_deinit(tas_priv);
1961}
1962
1963static const struct snd_soc_component_driver
1964	soc_codec_driver_tasdevice = {
1965	.probe			= tasdevice_codec_probe,
1966	.remove			= tasdevice_codec_remove,
1967	.dapm_widgets		= tasdevice_dapm_widgets,
1968	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
1969	.dapm_routes		= tasdevice_audio_map,
1970	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
1971	.idle_bias_on		= 1,
1972	.endianness		= 1,
1973};
1974
1975static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
1976{
1977	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
1978	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
1979	int ndev = 0;
1980	int i, rc;
1981
1982	if (tas_priv->isacpi) {
1983		ndev = device_property_read_u32_array(&client->dev,
1984			"ti,audio-slots", NULL, 0);
1985		if (ndev <= 0) {
1986			ndev = 1;
1987			dev_addrs[0] = client->addr;
1988		} else {
1989			ndev = (ndev < ARRAY_SIZE(dev_addrs))
1990				? ndev : ARRAY_SIZE(dev_addrs);
1991			rc = device_property_read_u32_array(&client->dev,
1992				"ti,audio-slots", dev_addrs, ndev);
1993			if (rc != 0) {
1994				ndev = 1;
1995				dev_addrs[0] = client->addr;
1996			}
1997		}
1998
1999		tas_priv->irq =
2000			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
2001	} else if (IS_ENABLED(CONFIG_OF)) {
2002		struct device_node *np = tas_priv->dev->of_node;
2003		u64 addr;
2004
2005		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
2006			if (of_property_read_reg(np, i, &addr, NULL))
2007				break;
2008			dev_addrs[ndev++] = addr;
2009		}
2010
2011		tas_priv->irq = of_irq_get(np, 0);
2012	} else {
2013		ndev = 1;
2014		dev_addrs[0] = client->addr;
2015	}
2016	tas_priv->ndev = ndev;
2017	for (i = 0; i < ndev; i++)
2018		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
2019
2020	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
2021			"reset", GPIOD_OUT_HIGH);
2022	if (IS_ERR(tas_priv->reset))
2023		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
2024			__func__);
2025
2026	strscpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name,
2027		sizeof(tas_priv->dev_name));
2028}
2029
2030static int tasdevice_i2c_probe(struct i2c_client *i2c)
2031{
2032	const struct acpi_device_id *acpi_id;
2033	struct tasdevice_priv *tas_priv;
2034	int ret;
2035
2036	tas_priv = tasdevice_kzalloc(i2c);
2037	if (!tas_priv)
2038		return -ENOMEM;
2039
2040	dev_set_drvdata(&i2c->dev, tas_priv);
2041
2042	if (ACPI_HANDLE(&i2c->dev)) {
2043		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
2044				&i2c->dev);
2045		if (!acpi_id) {
2046			dev_err(&i2c->dev, "No driver data\n");
2047			ret = -EINVAL;
2048			goto err;
2049		}
2050		tas_priv->chip_id = acpi_id->driver_data;
2051		tas_priv->isacpi = true;
2052	} else {
2053		tas_priv->chip_id = (uintptr_t)i2c_get_match_data(i2c);
2054		tas_priv->isacpi = false;
2055	}
2056
2057	tasdevice_parse_dt(tas_priv);
2058
2059	ret = tasdevice_init(tas_priv);
2060	if (ret)
2061		goto err;
2062
2063	tasdevice_reset(tas_priv);
2064
2065	ret = devm_snd_soc_register_component(tas_priv->dev,
2066		&soc_codec_driver_tasdevice,
2067		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
2068	if (ret) {
2069		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
2070			__func__, ret);
2071		goto err;
2072	}
2073err:
2074	if (ret < 0)
2075		tasdevice_remove(tas_priv);
2076	return ret;
2077}
2078
2079static void tasdevice_i2c_remove(struct i2c_client *client)
2080{
2081	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
2082
2083	tasdevice_remove(tas_priv);
2084}
2085
2086#ifdef CONFIG_ACPI
2087static const struct acpi_device_id tasdevice_acpi_match[] = {
2088	{ "TXNW2020", TAS2020 },
2089	{ "TXNW2118", TAS2118 },
2090	{ "TXNW2120", TAS2120 },
2091	{ "TXNW2320", TAS2320 },
2092	{ "TXNW2563", TAS2563 },
2093	{ "TXNW2568", TAS2568 },
2094	{ "TXNW2570", TAS2570 },
2095	{ "TXNW2572", TAS2572 },
2096	{ "TXNW2574", TAS2574 },
2097	{ "TXNW2781", TAS2781 },
2098	{ "TXNW5802", TAS5802 },
2099	{ "TXNW806M", TAS5806M },
2100	{ "TXNW806D", TAS5806MD },
2101	{ "TXNW5815", TAS5815 },
2102	{ "TXNW5822", TAS5822 },
2103	{ "TXNW5825", TAS5825 },
2104	{ "TXNW5827", TAS5827 },
2105	{ "TXNW5828", TAS5828 },
2106	{ "TXNW5830", TAS5830 },
2107	{},
2108};
2109
2110MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
2111#endif
2112
2113static struct i2c_driver tasdevice_i2c_driver = {
2114	.driver = {
2115		.name = "tasdev-codec",
2116		.of_match_table = of_match_ptr(tasdevice_of_match),
2117#ifdef CONFIG_ACPI
2118		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
2119#endif
2120	},
2121	.probe	= tasdevice_i2c_probe,
2122	.remove = tasdevice_i2c_remove,
2123	.id_table = tasdevice_id,
2124};
2125
2126module_i2c_driver(tasdevice_i2c_driver);
2127
2128MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
2129MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
2130MODULE_DESCRIPTION("ASoC TAS2781 Driver");
2131MODULE_LICENSE("GPL");
2132MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
~/ Read-only file preview.