File Index
Files and directories
Sign in
FILE

aw88261.c

File preview with syntax highlighting.

linux-6.19.14/sound/soc/codecs/aw88261.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2//
   3// aw88261.c  --  AW88261 ALSA SoC Audio driver
   4//
   5// Copyright (c) 2023 awinic Technology CO., LTD
   6//
   7// Author: Jimmy Zhang <zhangjianming@awinic.com>
   8// Author: Weidong Wang <wangweidong.a@awinic.com>
   9//
  10
  11#include <linux/i2c.h>
  12#include <linux/firmware.h>
  13#include <linux/regmap.h>
  14#include <sound/soc.h>
  15#include "aw88261.h"
  16#include "aw88395/aw88395_data_type.h"
  17#include "aw88395/aw88395_device.h"
  18
  19static const struct regmap_config aw88261_remap_config = {
  20	.val_bits = 16,
  21	.reg_bits = 8,
  22	.max_register = AW88261_REG_MAX,
  23	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
  24	.val_format_endian = REGMAP_ENDIAN_BIG,
  25};
  26
  27static void aw88261_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
  28{
  29	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
  30	unsigned int real_value, volume;
  31	unsigned int reg_value;
  32
  33	volume = min((value + vol_desc->init_volume), (unsigned int)AW88261_MUTE_VOL);
  34	real_value = DB_TO_REG_VAL(volume);
  35
  36	regmap_read(aw_dev->regmap, AW88261_SYSCTRL2_REG, &reg_value);
  37
  38	real_value = (real_value | (reg_value & AW88261_VOL_START_MASK));
  39
  40	dev_dbg(aw_dev->dev, "value 0x%x , real_value:0x%x", value, real_value);
  41
  42	regmap_write(aw_dev->regmap, AW88261_SYSCTRL2_REG, real_value);
  43}
  44
  45static void aw88261_dev_fade_in(struct aw_device *aw_dev)
  46{
  47	struct aw_volume_desc *desc = &aw_dev->volume_desc;
  48	int fade_in_vol = desc->ctl_volume;
  49	int fade_step = aw_dev->fade_step;
  50	int i;
  51
  52	if (fade_step == 0 || aw_dev->fade_in_time == 0) {
  53		aw88261_dev_set_volume(aw_dev, fade_in_vol);
  54		return;
  55	}
  56
  57	for (i = AW88261_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
  58		aw88261_dev_set_volume(aw_dev, i);
  59		usleep_range(aw_dev->fade_in_time,
  60					aw_dev->fade_in_time + 10);
  61	}
  62
  63	if (i != fade_in_vol)
  64		aw88261_dev_set_volume(aw_dev, fade_in_vol);
  65}
  66
  67static void aw88261_dev_fade_out(struct aw_device *aw_dev)
  68{
  69	struct aw_volume_desc *desc = &aw_dev->volume_desc;
  70	int fade_step = aw_dev->fade_step;
  71	int i;
  72
  73	if (fade_step == 0 || aw_dev->fade_out_time == 0) {
  74		aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
  75		return;
  76	}
  77
  78	for (i = desc->ctl_volume; i <= AW88261_MUTE_VOL; i += fade_step) {
  79		aw88261_dev_set_volume(aw_dev, i);
  80		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
  81	}
  82
  83	if (i != AW88261_MUTE_VOL) {
  84		aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
  85		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
  86	}
  87}
  88
  89static void aw88261_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
  90{
  91	if (flag)
  92		regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
  93			~AW88261_I2STXEN_MASK, AW88261_I2STXEN_ENABLE_VALUE);
  94	else
  95		regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
  96			~AW88261_I2STXEN_MASK, AW88261_I2STXEN_DISABLE_VALUE);
  97}
  98
  99static void aw88261_dev_pwd(struct aw_device *aw_dev, bool pwd)
 100{
 101	if (pwd)
 102		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 103				~AW88261_PWDN_MASK, AW88261_PWDN_POWER_DOWN_VALUE);
 104	else
 105		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 106				~AW88261_PWDN_MASK, AW88261_PWDN_WORKING_VALUE);
 107}
 108
 109static void aw88261_dev_amppd(struct aw_device *aw_dev, bool amppd)
 110{
 111	if (amppd)
 112		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 113				~AW88261_AMPPD_MASK, AW88261_AMPPD_POWER_DOWN_VALUE);
 114	else
 115		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 116				~AW88261_AMPPD_MASK, AW88261_AMPPD_WORKING_VALUE);
 117}
 118
 119static void aw88261_dev_mute(struct aw_device *aw_dev, bool is_mute)
 120{
 121	if (is_mute) {
 122		aw88261_dev_fade_out(aw_dev);
 123		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 124				~AW88261_HMUTE_MASK, AW88261_HMUTE_ENABLE_VALUE);
 125	} else {
 126		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 127				~AW88261_HMUTE_MASK, AW88261_HMUTE_DISABLE_VALUE);
 128		aw88261_dev_fade_in(aw_dev);
 129	}
 130}
 131
 132static void aw88261_dev_clear_int_status(struct aw_device *aw_dev)
 133{
 134	unsigned int int_status;
 135
 136	/* read int status and clear */
 137	regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
 138	/* make sure int status is clear */
 139	regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
 140
 141	dev_dbg(aw_dev->dev, "read interrupt reg = 0x%04x", int_status);
 142}
 143
 144static int aw88261_dev_get_iis_status(struct aw_device *aw_dev)
 145{
 146	unsigned int reg_val;
 147	int ret;
 148
 149	ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, &reg_val);
 150	if (ret)
 151		return ret;
 152	if ((reg_val & AW88261_BIT_PLL_CHECK) != AW88261_BIT_PLL_CHECK) {
 153		dev_err(aw_dev->dev, "check pll lock fail,reg_val:0x%04x", reg_val);
 154		return -EINVAL;
 155	}
 156
 157	return ret;
 158}
 159
 160static int aw88261_dev_check_mode1_pll(struct aw_device *aw_dev)
 161{
 162	int ret, i;
 163
 164	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
 165		ret = aw88261_dev_get_iis_status(aw_dev);
 166		if (ret) {
 167			dev_err(aw_dev->dev, "mode1 iis signal check error");
 168			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 169		} else {
 170			return ret;
 171		}
 172	}
 173
 174	return -EPERM;
 175}
 176
 177static int aw88261_dev_check_mode2_pll(struct aw_device *aw_dev)
 178{
 179	unsigned int reg_val;
 180	int ret, i;
 181
 182	ret = regmap_read(aw_dev->regmap, AW88261_PLLCTRL1_REG, &reg_val);
 183	if (ret)
 184		return ret;
 185
 186	reg_val &= (~AW88261_CCO_MUX_MASK);
 187	if (reg_val == AW88261_CCO_MUX_DIVIDED_VALUE) {
 188		dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
 189		return -EPERM;
 190	}
 191
 192	/* change mode2 */
 193	ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
 194			~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_DIVIDED_VALUE);
 195	if (ret)
 196		return ret;
 197
 198	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
 199		ret = aw88261_dev_get_iis_status(aw_dev);
 200		if (ret) {
 201			dev_err(aw_dev->dev, "mode2 iis signal check error");
 202			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 203		} else {
 204			break;
 205		}
 206	}
 207
 208	/* change mode1 */
 209	ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
 210			~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_BYPASS_VALUE);
 211	if (ret == 0) {
 212		usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 213		for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
 214			ret = aw88261_dev_check_mode1_pll(aw_dev);
 215			if (ret) {
 216				dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
 217				usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 218			} else {
 219				break;
 220			}
 221		}
 222	}
 223
 224	return ret;
 225}
 226
 227static int aw88261_dev_check_syspll(struct aw_device *aw_dev)
 228{
 229	int ret;
 230
 231	ret = aw88261_dev_check_mode1_pll(aw_dev);
 232	if (ret) {
 233		dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
 234		ret = aw88261_dev_check_mode2_pll(aw_dev);
 235		if (ret) {
 236			dev_err(aw_dev->dev, "mode2 check iis failed");
 237			return ret;
 238		}
 239	}
 240
 241	return ret;
 242}
 243
 244static int aw88261_dev_check_sysst(struct aw_device *aw_dev)
 245{
 246	unsigned int check_val;
 247	unsigned int reg_val;
 248	int ret, i;
 249
 250	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
 251		ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, &reg_val);
 252		if (ret)
 253			return ret;
 254
 255		check_val = reg_val & (~AW88261_BIT_SYSST_CHECK_MASK)
 256							& AW88261_BIT_SYSST_CHECK;
 257		if (check_val != AW88261_BIT_SYSST_CHECK) {
 258			dev_err(aw_dev->dev, "check sysst fail, reg_val=0x%04x, check:0x%x",
 259				reg_val, AW88261_BIT_SYSST_CHECK);
 260			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 261		} else {
 262			return 0;
 263		}
 264	}
 265
 266	return -EPERM;
 267}
 268
 269static void aw88261_dev_uls_hmute(struct aw_device *aw_dev, bool uls_hmute)
 270{
 271	if (uls_hmute)
 272		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 273				~AW88261_ULS_HMUTE_MASK,
 274				AW88261_ULS_HMUTE_ENABLE_VALUE);
 275	else
 276		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
 277				~AW88261_ULS_HMUTE_MASK,
 278				AW88261_ULS_HMUTE_DISABLE_VALUE);
 279}
 280
 281static void aw88261_reg_force_set(struct aw88261 *aw88261)
 282{
 283	if (aw88261->frcset_en == AW88261_FRCSET_ENABLE) {
 284		/* set FORCE_PWM */
 285		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL3_REG,
 286				AW88261_FORCE_PWM_MASK, AW88261_FORCE_PWM_FORCEMINUS_PWM_VALUE);
 287		/* set BOOST_OS_WIDTH */
 288		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL5_REG,
 289				AW88261_BST_OS_WIDTH_MASK, AW88261_BST_OS_WIDTH_50NS_VALUE);
 290		/* set BURST_LOOPR */
 291		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL6_REG,
 292				AW88261_BST_LOOPR_MASK, AW88261_BST_LOOPR_340K_VALUE);
 293		/* set RSQN_DLY */
 294		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL7_REG,
 295				AW88261_RSQN_DLY_MASK, AW88261_RSQN_DLY_35NS_VALUE);
 296		/* set BURST_SSMODE */
 297		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL8_REG,
 298				AW88261_BURST_SSMODE_MASK, AW88261_BURST_SSMODE_FAST_VALUE);
 299		/* set BST_BURST */
 300		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL9_REG,
 301				AW88261_BST_BURST_MASK, AW88261_BST_BURST_30MA_VALUE);
 302	} else {
 303		dev_dbg(aw88261->aw_pa->dev, "needn't set reg value");
 304	}
 305}
 306
 307static int aw88261_dev_get_icalk(struct aw_device *aw_dev, int16_t *icalk)
 308{
 309	u16 reg_icalk, reg_icalkl;
 310	unsigned int reg_val;
 311	int ret;
 312
 313	ret = regmap_read(aw_dev->regmap, AW88261_EFRH4_REG, &reg_val);
 314	if (ret)
 315		return ret;
 316
 317	reg_icalk = reg_val & (~AW88261_EF_ISN_GESLP_H_MASK);
 318
 319	ret = regmap_read(aw_dev->regmap, AW88261_EFRL4_REG, &reg_val);
 320	if (ret)
 321		return ret;
 322
 323	reg_icalkl = reg_val & (~AW88261_EF_ISN_GESLP_L_MASK);
 324
 325	reg_icalk = (reg_icalk >> AW88261_ICALK_SHIFT) & (reg_icalkl >> AW88261_ICALKL_SHIFT);
 326
 327	if (reg_icalk & (~AW88261_EF_ISN_GESLP_SIGN_MASK))
 328		reg_icalk = reg_icalk | ~AW88261_EF_ISN_GESLP_NEG;
 329
 330	*icalk = (int16_t)reg_icalk;
 331
 332	return ret;
 333}
 334
 335static int aw88261_dev_get_vcalk(struct aw_device *aw_dev, int16_t *vcalk)
 336{
 337	u16 reg_vcalk, reg_vcalkl;
 338	unsigned int reg_val;
 339	int ret;
 340
 341	ret = regmap_read(aw_dev->regmap, AW88261_EFRH3_REG, &reg_val);
 342	if (ret)
 343		return ret;
 344
 345	reg_vcalk = (u16)reg_val & (~AW88261_EF_VSN_GESLP_H_MASK);
 346
 347	ret = regmap_read(aw_dev->regmap, AW88261_EFRL3_REG, &reg_val);
 348	if (ret)
 349		return ret;
 350
 351	reg_vcalkl = (u16)reg_val & (~AW88261_EF_VSN_GESLP_L_MASK);
 352
 353	reg_vcalk = (reg_vcalk >> AW88261_VCALK_SHIFT) & (reg_vcalkl >> AW88261_VCALKL_SHIFT);
 354
 355	if (reg_vcalk & AW88261_EF_VSN_GESLP_SIGN_MASK)
 356		reg_vcalk = reg_vcalk | (~AW88261_EF_VSN_GESLP_NEG);
 357	*vcalk = (int16_t)reg_vcalk;
 358
 359	return ret;
 360}
 361
 362static int aw88261_dev_set_vcalb(struct aw_device *aw_dev)
 363{
 364	int16_t icalk_val, vcalk_val;
 365	int icalk, vcalk, vcalb;
 366	u32 reg_val;
 367	int ret;
 368
 369	ret = aw88261_dev_get_icalk(aw_dev, &icalk_val);
 370	if (ret)
 371		return ret;
 372
 373	ret = aw88261_dev_get_vcalk(aw_dev, &vcalk_val);
 374	if (ret)
 375		return ret;
 376
 377	icalk = AW88261_CABL_BASE_VALUE + AW88261_ICABLK_FACTOR * icalk_val;
 378	vcalk = AW88261_CABL_BASE_VALUE + AW88261_VCABLK_FACTOR * vcalk_val;
 379	if (!vcalk)
 380		return -EINVAL;
 381
 382	vcalb = AW88261_VCAL_FACTOR * icalk / vcalk;
 383	reg_val = (unsigned int)vcalb;
 384
 385	dev_dbg(aw_dev->dev, "icalk=%d, vcalk=%d, vcalb=%d, reg_val=0x%04x",
 386			icalk, vcalk, vcalb, reg_val);
 387	ret = regmap_write(aw_dev->regmap, AW88261_VSNTM1_REG, reg_val);
 388
 389	return ret;
 390}
 391
 392static int aw88261_dev_reg_update(struct aw88261 *aw88261,
 393					unsigned char *data, unsigned int len)
 394{
 395	struct aw_device *aw_dev = aw88261->aw_pa;
 396	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
 397	unsigned int read_val, efcheck_val, read_vol;
 398	int data_len, i, ret;
 399	int16_t *reg_data;
 400	u16 reg_val;
 401	u8 reg_addr;
 402
 403	if (!len || !data) {
 404		dev_err(aw_dev->dev, "reg data is null or len is 0");
 405		return -EINVAL;
 406	}
 407
 408	reg_data = (int16_t *)data;
 409	data_len = len >> 1;
 410
 411	if (data_len & 0x1) {
 412		dev_err(aw_dev->dev, "data len:%d unsupported",	data_len);
 413		return -EINVAL;
 414	}
 415
 416	for (i = 0; i < data_len; i += 2) {
 417		reg_addr = reg_data[i];
 418		reg_val = reg_data[i + 1];
 419
 420		if (reg_addr == AW88261_SYSCTRL_REG) {
 421			aw88261->amppd_st = reg_val & (~AW88261_AMPPD_MASK);
 422			ret = regmap_read(aw_dev->regmap, reg_addr, &read_val);
 423			if (ret)
 424				break;
 425
 426			/* keep all three bits from current hw status */
 427			read_val &= (~AW88261_AMPPD_MASK) | (~AW88261_PWDN_MASK) |
 428								(~AW88261_HMUTE_MASK);
 429			reg_val &= (AW88261_AMPPD_MASK & AW88261_PWDN_MASK & AW88261_HMUTE_MASK);
 430			reg_val |= read_val;
 431
 432			/* enable uls hmute */
 433			reg_val &= AW88261_ULS_HMUTE_MASK;
 434			reg_val |= AW88261_ULS_HMUTE_ENABLE_VALUE;
 435		}
 436
 437		if (reg_addr == AW88261_DBGCTRL_REG) {
 438			efcheck_val = reg_val & (~AW88261_EF_DBMD_MASK);
 439			if (efcheck_val == AW88261_OR_VALUE)
 440				aw88261->efuse_check = AW88261_EF_OR_CHECK;
 441			else
 442				aw88261->efuse_check = AW88261_EF_AND_CHECK;
 443		}
 444
 445		/* i2stxen */
 446		if (reg_addr == AW88261_I2SCTRL3_REG) {
 447			/* close tx */
 448			reg_val &= AW88261_I2STXEN_MASK;
 449			reg_val |= AW88261_I2STXEN_DISABLE_VALUE;
 450		}
 451
 452		if (reg_addr == AW88261_SYSCTRL2_REG) {
 453			read_vol = (reg_val & (~AW88261_VOL_MASK)) >>
 454				AW88261_VOL_START_BIT;
 455			aw_dev->volume_desc.init_volume =
 456				REG_VAL_TO_DB(read_vol);
 457		}
 458
 459		if (reg_addr == AW88261_VSNTM1_REG)
 460			continue;
 461
 462		ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
 463		if (ret)
 464			break;
 465	}
 466
 467	ret = aw88261_dev_set_vcalb(aw_dev);
 468	if (ret)
 469		return ret;
 470
 471	if (aw_dev->prof_cur != aw_dev->prof_index)
 472		vol_desc->ctl_volume = 0;
 473
 474	/* keep min volume */
 475	aw88261_dev_set_volume(aw_dev, vol_desc->mute_volume);
 476
 477	return ret;
 478}
 479
 480static int aw88261_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
 481{
 482	struct aw_prof_info *prof_info = &aw_dev->prof_info;
 483	struct aw_prof_desc *prof_desc;
 484
 485	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
 486		dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
 487			index, aw_dev->prof_info.count);
 488		return -EINVAL;
 489	}
 490
 491	prof_desc = &aw_dev->prof_info.prof_desc[index];
 492
 493	*prof_name = prof_info->prof_name_list[prof_desc->id];
 494
 495	return 0;
 496}
 497
 498static int aw88261_dev_get_prof_data(struct aw_device *aw_dev, int index,
 499			struct aw_prof_desc **prof_desc)
 500{
 501	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
 502		dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
 503				__func__, index, aw_dev->prof_info.count);
 504		return -EINVAL;
 505	}
 506
 507	*prof_desc = &aw_dev->prof_info.prof_desc[index];
 508
 509	return 0;
 510}
 511
 512static int aw88261_dev_fw_update(struct aw88261 *aw88261)
 513{
 514	struct aw_device *aw_dev = aw88261->aw_pa;
 515	struct aw_prof_desc *prof_index_desc;
 516	struct aw_sec_data_desc *sec_desc;
 517	char *prof_name;
 518	int ret;
 519
 520	ret = aw88261_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
 521	if (ret) {
 522		dev_err(aw_dev->dev, "get prof name failed");
 523		return -EINVAL;
 524	}
 525
 526	dev_dbg(aw_dev->dev, "start update %s", prof_name);
 527
 528	ret = aw88261_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
 529	if (ret)
 530		return ret;
 531
 532	/* update reg */
 533	sec_desc = prof_index_desc->sec_desc;
 534	ret = aw88261_dev_reg_update(aw88261, sec_desc[AW88395_DATA_TYPE_REG].data,
 535					sec_desc[AW88395_DATA_TYPE_REG].len);
 536	if (ret) {
 537		dev_err(aw_dev->dev, "update reg failed");
 538		return ret;
 539	}
 540
 541	aw_dev->prof_cur = aw_dev->prof_index;
 542
 543	return ret;
 544}
 545
 546static int aw88261_dev_start(struct aw88261 *aw88261)
 547{
 548	struct aw_device *aw_dev = aw88261->aw_pa;
 549	int ret;
 550
 551	if (aw_dev->status == AW88261_DEV_PW_ON) {
 552		dev_info(aw_dev->dev, "already power on");
 553		return 0;
 554	}
 555
 556	/* power on */
 557	aw88261_dev_pwd(aw_dev, false);
 558	usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
 559
 560	ret = aw88261_dev_check_syspll(aw_dev);
 561	if (ret) {
 562		dev_err(aw_dev->dev, "pll check failed cannot start");
 563		goto pll_check_fail;
 564	}
 565
 566	/* amppd on */
 567	aw88261_dev_amppd(aw_dev, false);
 568	usleep_range(AW88261_1000_US, AW88261_1000_US + 50);
 569
 570	/* check i2s status */
 571	ret = aw88261_dev_check_sysst(aw_dev);
 572	if (ret) {
 573		dev_err(aw_dev->dev, "sysst check failed");
 574		goto sysst_check_fail;
 575	}
 576
 577	/* enable tx feedback */
 578	aw88261_dev_i2s_tx_enable(aw_dev, true);
 579
 580	if (aw88261->amppd_st)
 581		aw88261_dev_amppd(aw_dev, true);
 582
 583	aw88261_reg_force_set(aw88261);
 584
 585	/* close uls mute */
 586	aw88261_dev_uls_hmute(aw_dev, false);
 587
 588	/* close mute */
 589	if (!aw88261->mute_st)
 590		aw88261_dev_mute(aw_dev, false);
 591
 592	/* clear inturrupt */
 593	aw88261_dev_clear_int_status(aw_dev);
 594	aw_dev->status = AW88261_DEV_PW_ON;
 595
 596	return 0;
 597
 598sysst_check_fail:
 599	aw88261_dev_i2s_tx_enable(aw_dev, false);
 600	aw88261_dev_clear_int_status(aw_dev);
 601	aw88261_dev_amppd(aw_dev, true);
 602pll_check_fail:
 603	aw88261_dev_pwd(aw_dev, true);
 604	aw_dev->status = AW88261_DEV_PW_OFF;
 605
 606	return ret;
 607}
 608
 609static int aw88261_dev_stop(struct aw_device *aw_dev)
 610{
 611	if (aw_dev->status == AW88261_DEV_PW_OFF) {
 612		dev_info(aw_dev->dev, "already power off");
 613		return 0;
 614	}
 615
 616	aw_dev->status = AW88261_DEV_PW_OFF;
 617
 618	/* clear inturrupt */
 619	aw88261_dev_clear_int_status(aw_dev);
 620
 621	aw88261_dev_uls_hmute(aw_dev, true);
 622	/* set mute */
 623	aw88261_dev_mute(aw_dev, true);
 624
 625	/* close tx feedback */
 626	aw88261_dev_i2s_tx_enable(aw_dev, false);
 627	usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
 628
 629	/* enable amppd */
 630	aw88261_dev_amppd(aw_dev, true);
 631
 632	/* set power down */
 633	aw88261_dev_pwd(aw_dev, true);
 634
 635	return 0;
 636}
 637
 638static int aw88261_reg_update(struct aw88261 *aw88261, bool force)
 639{
 640	struct aw_device *aw_dev = aw88261->aw_pa;
 641	int ret;
 642
 643	if (force) {
 644		ret = regmap_write(aw_dev->regmap,
 645					AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
 646		if (ret)
 647			return ret;
 648
 649		ret = aw88261_dev_fw_update(aw88261);
 650		if (ret)
 651			return ret;
 652	} else {
 653		if (aw_dev->prof_cur != aw_dev->prof_index) {
 654			ret = aw88261_dev_fw_update(aw88261);
 655			if (ret)
 656				return ret;
 657		} else {
 658			ret = 0;
 659		}
 660	}
 661
 662	aw_dev->prof_cur = aw_dev->prof_index;
 663
 664	return ret;
 665}
 666
 667static void aw88261_start_pa(struct aw88261 *aw88261)
 668{
 669	int ret, i;
 670
 671	for (i = 0; i < AW88261_START_RETRIES; i++) {
 672		ret = aw88261_reg_update(aw88261, aw88261->phase_sync);
 673		if (ret) {
 674			dev_err(aw88261->aw_pa->dev, "fw update failed, cnt:%d\n", i);
 675			continue;
 676		}
 677		ret = aw88261_dev_start(aw88261);
 678		if (ret) {
 679			dev_err(aw88261->aw_pa->dev, "aw88261 device start failed. retry = %d", i);
 680			continue;
 681		} else {
 682			dev_info(aw88261->aw_pa->dev, "start success\n");
 683			break;
 684		}
 685	}
 686}
 687
 688static void aw88261_startup_work(struct work_struct *work)
 689{
 690	struct aw88261 *aw88261 =
 691		container_of(work, struct aw88261, start_work.work);
 692
 693	mutex_lock(&aw88261->lock);
 694	aw88261_start_pa(aw88261);
 695	mutex_unlock(&aw88261->lock);
 696}
 697
 698static void aw88261_start(struct aw88261 *aw88261, bool sync_start)
 699{
 700	if (aw88261->aw_pa->fw_status != AW88261_DEV_FW_OK)
 701		return;
 702
 703	if (aw88261->aw_pa->status == AW88261_DEV_PW_ON)
 704		return;
 705
 706	if (sync_start == AW88261_SYNC_START)
 707		aw88261_start_pa(aw88261);
 708	else
 709		queue_delayed_work(system_dfl_wq,
 710			&aw88261->start_work,
 711			AW88261_START_WORK_DELAY_MS);
 712}
 713
 714static struct snd_soc_dai_driver aw88261_dai[] = {
 715	{
 716		.name = "aw88261-aif",
 717		.id = 1,
 718		.playback = {
 719			.stream_name = "Speaker_Playback",
 720			.channels_min = 1,
 721			.channels_max = 2,
 722			.rates = AW88261_RATES,
 723			.formats = AW88261_FORMATS,
 724		},
 725		.capture = {
 726			.stream_name = "Speaker_Capture",
 727			.channels_min = 1,
 728			.channels_max = 2,
 729			.rates = AW88261_RATES,
 730			.formats = AW88261_FORMATS,
 731		},
 732	},
 733};
 734
 735static int aw88261_get_fade_in_time(struct snd_kcontrol *kcontrol,
 736	struct snd_ctl_elem_value *ucontrol)
 737{
 738	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 739	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
 740	struct aw_device *aw_dev = aw88261->aw_pa;
 741
 742	ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
 743
 744	return 0;
 745}
 746
 747static int aw88261_set_fade_in_time(struct snd_kcontrol *kcontrol,
 748	struct snd_ctl_elem_value *ucontrol)
 749{
 750	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 751	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
 752	struct soc_mixer_control *mc =
 753		(struct soc_mixer_control *)kcontrol->private_value;
 754	struct aw_device *aw_dev = aw88261->aw_pa;
 755	int time;
 756
 757	time = ucontrol->value.integer.value[0];
 758
 759	if (time < mc->min || time > mc->max)
 760		return -EINVAL;
 761
 762	if (time != aw_dev->fade_in_time) {
 763		aw_dev->fade_in_time = time;
 764		return 1;
 765	}
 766
 767	return 0;
 768}
 769
 770static int aw88261_get_fade_out_time(struct snd_kcontrol *kcontrol,
 771	struct snd_ctl_elem_value *ucontrol)
 772{
 773	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 774	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
 775	struct aw_device *aw_dev = aw88261->aw_pa;
 776
 777	ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
 778
 779	return 0;
 780}
 781
 782static int aw88261_set_fade_out_time(struct snd_kcontrol *kcontrol,
 783	struct snd_ctl_elem_value *ucontrol)
 784{
 785	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 786	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
 787	struct soc_mixer_control *mc =
 788		(struct soc_mixer_control *)kcontrol->private_value;
 789	struct aw_device *aw_dev = aw88261->aw_pa;
 790	int time;
 791
 792	time = ucontrol->value.integer.value[0];
 793	if (time < mc->min || time > mc->max)
 794		return -EINVAL;
 795
 796	if (time != aw_dev->fade_out_time) {
 797		aw_dev->fade_out_time = time;
 798		return 1;
 799	}
 800
 801	return 0;
 802}
 803
 804static int aw88261_dev_set_profile_index(struct aw_device *aw_dev, int index)
 805{
 806	/* check the index whether is valid */
 807	if ((index >= aw_dev->prof_info.count) || (index < 0))
 808		return -EINVAL;
 809	/* check the index whether change */
 810	if (aw_dev->prof_index == index)
 811		return -EPERM;
 812
 813	aw_dev->prof_index = index;
 814
 815	return 0;
 816}
 817
 818static int aw88261_profile_info(struct snd_kcontrol *kcontrol,
 819			 struct snd_ctl_elem_info *uinfo)
 820{
 821	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 822	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 823	char *prof_name;
 824	int count, ret;
 825
 826	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 827	uinfo->count = 1;
 828
 829	count = aw88261->aw_pa->prof_info.count;
 830	if (count <= 0) {
 831		uinfo->value.enumerated.items = 0;
 832		return 0;
 833	}
 834
 835	uinfo->value.enumerated.items = count;
 836
 837	if (uinfo->value.enumerated.item >= count)
 838		uinfo->value.enumerated.item = count - 1;
 839
 840	count = uinfo->value.enumerated.item;
 841
 842	ret = aw88261_dev_get_prof_name(aw88261->aw_pa, count, &prof_name);
 843	if (ret) {
 844		strscpy(uinfo->value.enumerated.name, "null");
 845		return 0;
 846	}
 847
 848	strscpy(uinfo->value.enumerated.name, prof_name);
 849
 850	return 0;
 851}
 852
 853static int aw88261_profile_get(struct snd_kcontrol *kcontrol,
 854			struct snd_ctl_elem_value *ucontrol)
 855{
 856	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 857	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 858
 859	ucontrol->value.integer.value[0] = aw88261->aw_pa->prof_index;
 860
 861	return 0;
 862}
 863
 864static int aw88261_profile_set(struct snd_kcontrol *kcontrol,
 865		struct snd_ctl_elem_value *ucontrol)
 866{
 867	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 868	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 869	int ret;
 870
 871	/* pa stop or stopping just set profile */
 872	mutex_lock(&aw88261->lock);
 873	ret = aw88261_dev_set_profile_index(aw88261->aw_pa, ucontrol->value.integer.value[0]);
 874	if (ret) {
 875		dev_dbg(codec->dev, "profile index does not change");
 876		mutex_unlock(&aw88261->lock);
 877		return 0;
 878	}
 879
 880	if (aw88261->aw_pa->status) {
 881		aw88261_dev_stop(aw88261->aw_pa);
 882		aw88261_start(aw88261, AW88261_SYNC_START);
 883	}
 884
 885	mutex_unlock(&aw88261->lock);
 886
 887	return 1;
 888}
 889
 890static int aw88261_volume_get(struct snd_kcontrol *kcontrol,
 891				struct snd_ctl_elem_value *ucontrol)
 892{
 893	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 894	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 895	struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
 896
 897	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
 898
 899	return 0;
 900}
 901
 902static int aw88261_volume_set(struct snd_kcontrol *kcontrol,
 903				struct snd_ctl_elem_value *ucontrol)
 904{
 905	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 906	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 907	struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
 908	struct soc_mixer_control *mc =
 909		(struct soc_mixer_control *)kcontrol->private_value;
 910	int value;
 911
 912	value = ucontrol->value.integer.value[0];
 913
 914	if (value < mc->min || value > mc->max)
 915		return -EINVAL;
 916
 917	if (vol_desc->ctl_volume != value) {
 918		vol_desc->ctl_volume = value;
 919		aw88261_dev_set_volume(aw88261->aw_pa, vol_desc->ctl_volume);
 920
 921		return 1;
 922	}
 923
 924	return 0;
 925}
 926
 927static int aw88261_get_fade_step(struct snd_kcontrol *kcontrol,
 928				struct snd_ctl_elem_value *ucontrol)
 929{
 930	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 931	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 932
 933	ucontrol->value.integer.value[0] = aw88261->aw_pa->fade_step;
 934
 935	return 0;
 936}
 937
 938static int aw88261_set_fade_step(struct snd_kcontrol *kcontrol,
 939				struct snd_ctl_elem_value *ucontrol)
 940{
 941	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
 942	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
 943	struct soc_mixer_control *mc =
 944		(struct soc_mixer_control *)kcontrol->private_value;
 945	int value;
 946
 947	value = ucontrol->value.integer.value[0];
 948	if (value < mc->min || value > mc->max)
 949		return -EINVAL;
 950
 951	if (aw88261->aw_pa->fade_step != value) {
 952		aw88261->aw_pa->fade_step = value;
 953		return 1;
 954	}
 955
 956	return 0;
 957}
 958
 959static const struct snd_kcontrol_new aw88261_controls[] = {
 960	SOC_SINGLE_EXT("PCM Playback Volume", AW88261_SYSCTRL2_REG,
 961		6, AW88261_MUTE_VOL, 0, aw88261_volume_get,
 962		aw88261_volume_set),
 963	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88261_MUTE_VOL, 0,
 964		aw88261_get_fade_step, aw88261_set_fade_step),
 965	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
 966		aw88261_get_fade_in_time, aw88261_set_fade_in_time),
 967	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
 968		aw88261_get_fade_out_time, aw88261_set_fade_out_time),
 969	AW88261_PROFILE_EXT("Profile Set", aw88261_profile_info,
 970		aw88261_profile_get, aw88261_profile_set),
 971};
 972
 973static int aw88261_playback_event(struct snd_soc_dapm_widget *w,
 974				struct snd_kcontrol *k, int event)
 975{
 976	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 977	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
 978
 979	mutex_lock(&aw88261->lock);
 980	switch (event) {
 981	case SND_SOC_DAPM_PRE_PMU:
 982		aw88261_start(aw88261, AW88261_ASYNC_START);
 983		break;
 984	case SND_SOC_DAPM_POST_PMD:
 985		aw88261_dev_stop(aw88261->aw_pa);
 986		break;
 987	default:
 988		break;
 989	}
 990	mutex_unlock(&aw88261->lock);
 991
 992	return 0;
 993}
 994
 995static const struct snd_soc_dapm_widget aw88261_dapm_widgets[] = {
 996	 /* playback */
 997	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
 998					aw88261_playback_event,
 999					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1000	SND_SOC_DAPM_OUTPUT("DAC Output"),
1001
1002	/* capture */
1003	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1004	SND_SOC_DAPM_INPUT("ADC Input"),
1005};
1006
1007static const struct snd_soc_dapm_route aw88261_audio_map[] = {
1008	{"DAC Output", NULL, "AIF_RX"},
1009	{"AIF_TX", NULL, "ADC Input"},
1010};
1011
1012static int aw88261_frcset_check(struct aw88261 *aw88261)
1013{
1014	unsigned int reg_val;
1015	u16 temh, teml, tem;
1016	int ret;
1017
1018	ret = regmap_read(aw88261->regmap, AW88261_EFRH3_REG, &reg_val);
1019	if (ret)
1020		return ret;
1021	temh = ((u16)reg_val & (~AW88261_TEMH_MASK));
1022
1023	ret = regmap_read(aw88261->regmap, AW88261_EFRL3_REG, &reg_val);
1024	if (ret)
1025		return ret;
1026	teml = ((u16)reg_val & (~AW88261_TEML_MASK));
1027
1028	if (aw88261->efuse_check == AW88261_EF_OR_CHECK)
1029		tem = (temh | teml);
1030	else
1031		tem = (temh & teml);
1032
1033	if (tem == AW88261_DEFAULT_CFG)
1034		aw88261->frcset_en = AW88261_FRCSET_ENABLE;
1035	else
1036		aw88261->frcset_en = AW88261_FRCSET_DISABLE;
1037
1038	dev_dbg(aw88261->aw_pa->dev, "tem is 0x%04x, frcset_en is %d",
1039						tem, aw88261->frcset_en);
1040
1041	return ret;
1042}
1043
1044static int aw88261_dev_init(struct aw88261 *aw88261, struct aw_container *aw_cfg)
1045{
1046	struct aw_device *aw_dev = aw88261->aw_pa;
1047	int ret;
1048
1049	ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1050	if (ret) {
1051		dev_err(aw_dev->dev, "aw_dev acf parse failed");
1052		return -EINVAL;
1053	}
1054
1055	ret = regmap_write(aw_dev->regmap, AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
1056	if (ret)
1057		return ret;
1058
1059	aw_dev->fade_in_time = AW88261_500_US;
1060	aw_dev->fade_out_time = AW88261_500_US;
1061	aw_dev->prof_cur = AW88261_INIT_PROFILE;
1062	aw_dev->prof_index = AW88261_INIT_PROFILE;
1063
1064	ret = aw88261_dev_fw_update(aw88261);
1065	if (ret) {
1066		dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1067		return ret;
1068	}
1069
1070	ret = aw88261_frcset_check(aw88261);
1071	if (ret) {
1072		dev_err(aw_dev->dev, "aw88261_frcset_check ret = %d\n", ret);
1073		return ret;
1074	}
1075
1076	aw88261_dev_clear_int_status(aw_dev);
1077
1078	aw88261_dev_uls_hmute(aw_dev, true);
1079
1080	aw88261_dev_mute(aw_dev, true);
1081
1082	aw88261_dev_i2s_tx_enable(aw_dev, false);
1083
1084	usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
1085
1086	aw88261_dev_amppd(aw_dev, true);
1087
1088	aw88261_dev_pwd(aw_dev, true);
1089
1090	return 0;
1091}
1092
1093static int aw88261_request_firmware_file(struct aw88261 *aw88261)
1094{
1095	const struct firmware *cont = NULL;
1096	int ret;
1097
1098	aw88261->aw_pa->fw_status = AW88261_DEV_FW_FAILED;
1099
1100	ret = request_firmware(&cont, AW88261_ACF_FILE, aw88261->aw_pa->dev);
1101	if (ret)
1102		return dev_err_probe(aw88261->aw_pa->dev, ret,
1103					"load [%s] failed!", AW88261_ACF_FILE);
1104
1105	dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
1106			AW88261_ACF_FILE, cont ? cont->size : 0);
1107
1108	aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1109	if (!aw88261->aw_cfg) {
1110		release_firmware(cont);
1111		return -ENOMEM;
1112	}
1113	aw88261->aw_cfg->len = (int)cont->size;
1114	memcpy(aw88261->aw_cfg->data, cont->data, cont->size);
1115	release_firmware(cont);
1116
1117	ret = aw88395_dev_load_acf_check(aw88261->aw_pa, aw88261->aw_cfg);
1118	if (ret) {
1119		dev_err(aw88261->aw_pa->dev, "load [%s] failed !", AW88261_ACF_FILE);
1120		return ret;
1121	}
1122
1123	mutex_lock(&aw88261->lock);
1124	/* aw device init */
1125	ret = aw88261_dev_init(aw88261, aw88261->aw_cfg);
1126	if (ret)
1127		dev_err(aw88261->aw_pa->dev, "dev init failed");
1128	mutex_unlock(&aw88261->lock);
1129
1130	return ret;
1131}
1132
1133static int aw88261_codec_probe(struct snd_soc_component *component)
1134{
1135	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1136	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
1137	int ret;
1138
1139	INIT_DELAYED_WORK(&aw88261->start_work, aw88261_startup_work);
1140
1141	ret = aw88261_request_firmware_file(aw88261);
1142	if (ret)
1143		return dev_err_probe(aw88261->aw_pa->dev, ret,
1144				"aw88261_request_firmware_file failed\n");
1145
1146	/* add widgets */
1147	ret = snd_soc_dapm_new_controls(dapm, aw88261_dapm_widgets,
1148							ARRAY_SIZE(aw88261_dapm_widgets));
1149	if (ret)
1150		return ret;
1151
1152	/* add route */
1153	ret = snd_soc_dapm_add_routes(dapm, aw88261_audio_map,
1154							ARRAY_SIZE(aw88261_audio_map));
1155	if (ret)
1156		return ret;
1157
1158	ret = snd_soc_add_component_controls(component, aw88261_controls,
1159							ARRAY_SIZE(aw88261_controls));
1160
1161	return ret;
1162}
1163
1164static void aw88261_codec_remove(struct snd_soc_component *aw_codec)
1165{
1166	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(aw_codec);
1167
1168	cancel_delayed_work_sync(&aw88261->start_work);
1169}
1170
1171static const struct snd_soc_component_driver soc_codec_dev_aw88261 = {
1172	.probe = aw88261_codec_probe,
1173	.remove = aw88261_codec_remove,
1174};
1175
1176static void aw88261_parse_channel_dt(struct aw88261 *aw88261)
1177{
1178	struct aw_device *aw_dev = aw88261->aw_pa;
1179	struct device_node *np = aw_dev->dev->of_node;
1180	u32 channel_value = AW88261_DEV_DEFAULT_CH;
1181
1182	of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1183	aw88261->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1184
1185	aw_dev->channel = channel_value;
1186}
1187
1188static int aw88261_init(struct aw88261 *aw88261, struct i2c_client *i2c, struct regmap *regmap)
1189{
1190	struct aw_device *aw_dev;
1191	unsigned int chip_id;
1192	int ret;
1193
1194	/* read chip id */
1195	ret = regmap_read(regmap, AW88261_ID_REG, &chip_id);
1196	if (ret) {
1197		dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1198		return ret;
1199	}
1200	if (chip_id != AW88261_CHIP_ID) {
1201		dev_err(&i2c->dev, "unsupported device");
1202		return -ENXIO;
1203	}
1204
1205	dev_info(&i2c->dev, "chip id = %x\n", chip_id);
1206
1207	aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1208	if (!aw_dev)
1209		return -ENOMEM;
1210
1211	aw88261->aw_pa = aw_dev;
1212	aw_dev->i2c = i2c;
1213	aw_dev->regmap = regmap;
1214	aw_dev->dev = &i2c->dev;
1215	aw_dev->chip_id = AW88261_CHIP_ID;
1216	aw_dev->acf = NULL;
1217	aw_dev->prof_info.prof_desc = NULL;
1218	aw_dev->prof_info.count = 0;
1219	aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1220	aw_dev->channel = 0;
1221	aw_dev->fw_status = AW88261_DEV_FW_FAILED;
1222	aw_dev->fade_step = AW88261_VOLUME_STEP_DB;
1223	aw_dev->volume_desc.ctl_volume = AW88261_VOL_DEFAULT_VALUE;
1224	aw_dev->volume_desc.mute_volume = AW88261_MUTE_VOL;
1225	aw88261_parse_channel_dt(aw88261);
1226
1227	return ret;
1228}
1229
1230static int aw88261_i2c_probe(struct i2c_client *i2c)
1231{
1232	struct aw88261 *aw88261;
1233	int ret;
1234
1235	ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1236	if (!ret)
1237		return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1238
1239	aw88261 = devm_kzalloc(&i2c->dev, sizeof(*aw88261), GFP_KERNEL);
1240	if (!aw88261)
1241		return -ENOMEM;
1242
1243	mutex_init(&aw88261->lock);
1244
1245	i2c_set_clientdata(i2c, aw88261);
1246
1247	aw88261->regmap = devm_regmap_init_i2c(i2c, &aw88261_remap_config);
1248	if (IS_ERR(aw88261->regmap)) {
1249		ret = PTR_ERR(aw88261->regmap);
1250		return dev_err_probe(&i2c->dev, ret, "failed to init regmap: %d\n", ret);
1251	}
1252
1253	/* aw pa init */
1254	ret = aw88261_init(aw88261, i2c, aw88261->regmap);
1255	if (ret)
1256		return ret;
1257
1258	ret = devm_snd_soc_register_component(&i2c->dev,
1259			&soc_codec_dev_aw88261,
1260			aw88261_dai, ARRAY_SIZE(aw88261_dai));
1261	if (ret)
1262		dev_err(&i2c->dev, "failed to register aw88261: %d", ret);
1263
1264	return ret;
1265}
1266
1267static const struct i2c_device_id aw88261_i2c_id[] = {
1268	{ AW88261_I2C_NAME },
1269	{ }
1270};
1271MODULE_DEVICE_TABLE(i2c, aw88261_i2c_id);
1272
1273static struct i2c_driver aw88261_i2c_driver = {
1274	.driver = {
1275		.name = AW88261_I2C_NAME,
1276	},
1277	.probe = aw88261_i2c_probe,
1278	.id_table = aw88261_i2c_id,
1279};
1280module_i2c_driver(aw88261_i2c_driver);
1281
1282MODULE_DESCRIPTION("ASoC AW88261 Smart PA Driver");
1283MODULE_LICENSE("GPL v2");
~/ Read-only file preview.