18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Codec driver for ST STA350 2.1-channel high-efficiency digital audio system
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright: 2014 Raumfeld GmbH
68c2ecf20Sopenharmony_ci * Author: Sven Brandau <info@brandau.biz>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * based on code from:
98c2ecf20Sopenharmony_ci *	Raumfeld GmbH
108c2ecf20Sopenharmony_ci *	  Johannes Stezenbach <js@sig21.net>
118c2ecf20Sopenharmony_ci *	Wolfson Microelectronics PLC.
128c2ecf20Sopenharmony_ci *	  Mark Brown <broonie@opensource.wolfsonmicro.com>
138c2ecf20Sopenharmony_ci *	Freescale Semiconductor, Inc.
148c2ecf20Sopenharmony_ci *	  Timur Tabi <timur@freescale.com>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ":%s:%d: " fmt, __func__, __LINE__
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
218c2ecf20Sopenharmony_ci#include <linux/init.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/pm.h>
248c2ecf20Sopenharmony_ci#include <linux/i2c.h>
258c2ecf20Sopenharmony_ci#include <linux/of_device.h>
268c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
278c2ecf20Sopenharmony_ci#include <linux/regmap.h>
288c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
298c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
308c2ecf20Sopenharmony_ci#include <linux/slab.h>
318c2ecf20Sopenharmony_ci#include <sound/core.h>
328c2ecf20Sopenharmony_ci#include <sound/pcm.h>
338c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
348c2ecf20Sopenharmony_ci#include <sound/soc.h>
358c2ecf20Sopenharmony_ci#include <sound/soc-dapm.h>
368c2ecf20Sopenharmony_ci#include <sound/initval.h>
378c2ecf20Sopenharmony_ci#include <sound/tlv.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <sound/sta350.h>
408c2ecf20Sopenharmony_ci#include "sta350.h"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define STA350_RATES (SNDRV_PCM_RATE_32000 | \
438c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_44100 | \
448c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_48000 | \
458c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_88200 | \
468c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_96000 | \
478c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_176400 | \
488c2ecf20Sopenharmony_ci		      SNDRV_PCM_RATE_192000)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define STA350_FORMATS \
518c2ecf20Sopenharmony_ci	(SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
528c2ecf20Sopenharmony_ci	 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
538c2ecf20Sopenharmony_ci	 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
548c2ecf20Sopenharmony_ci	 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
558c2ecf20Sopenharmony_ci	 SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE  | \
568c2ecf20Sopenharmony_ci	 SNDRV_PCM_FMTBIT_S32_LE  | SNDRV_PCM_FMTBIT_S32_BE)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* Power-up register defaults */
598c2ecf20Sopenharmony_cistatic const struct reg_default sta350_regs[] = {
608c2ecf20Sopenharmony_ci	{  0x0, 0x63 },
618c2ecf20Sopenharmony_ci	{  0x1, 0x80 },
628c2ecf20Sopenharmony_ci	{  0x2, 0xdf },
638c2ecf20Sopenharmony_ci	{  0x3, 0x40 },
648c2ecf20Sopenharmony_ci	{  0x4, 0xc2 },
658c2ecf20Sopenharmony_ci	{  0x5, 0x5c },
668c2ecf20Sopenharmony_ci	{  0x6, 0x00 },
678c2ecf20Sopenharmony_ci	{  0x7, 0xff },
688c2ecf20Sopenharmony_ci	{  0x8, 0x60 },
698c2ecf20Sopenharmony_ci	{  0x9, 0x60 },
708c2ecf20Sopenharmony_ci	{  0xa, 0x60 },
718c2ecf20Sopenharmony_ci	{  0xb, 0x00 },
728c2ecf20Sopenharmony_ci	{  0xc, 0x00 },
738c2ecf20Sopenharmony_ci	{  0xd, 0x00 },
748c2ecf20Sopenharmony_ci	{  0xe, 0x00 },
758c2ecf20Sopenharmony_ci	{  0xf, 0x40 },
768c2ecf20Sopenharmony_ci	{ 0x10, 0x80 },
778c2ecf20Sopenharmony_ci	{ 0x11, 0x77 },
788c2ecf20Sopenharmony_ci	{ 0x12, 0x6a },
798c2ecf20Sopenharmony_ci	{ 0x13, 0x69 },
808c2ecf20Sopenharmony_ci	{ 0x14, 0x6a },
818c2ecf20Sopenharmony_ci	{ 0x15, 0x69 },
828c2ecf20Sopenharmony_ci	{ 0x16, 0x00 },
838c2ecf20Sopenharmony_ci	{ 0x17, 0x00 },
848c2ecf20Sopenharmony_ci	{ 0x18, 0x00 },
858c2ecf20Sopenharmony_ci	{ 0x19, 0x00 },
868c2ecf20Sopenharmony_ci	{ 0x1a, 0x00 },
878c2ecf20Sopenharmony_ci	{ 0x1b, 0x00 },
888c2ecf20Sopenharmony_ci	{ 0x1c, 0x00 },
898c2ecf20Sopenharmony_ci	{ 0x1d, 0x00 },
908c2ecf20Sopenharmony_ci	{ 0x1e, 0x00 },
918c2ecf20Sopenharmony_ci	{ 0x1f, 0x00 },
928c2ecf20Sopenharmony_ci	{ 0x20, 0x00 },
938c2ecf20Sopenharmony_ci	{ 0x21, 0x00 },
948c2ecf20Sopenharmony_ci	{ 0x22, 0x00 },
958c2ecf20Sopenharmony_ci	{ 0x23, 0x00 },
968c2ecf20Sopenharmony_ci	{ 0x24, 0x00 },
978c2ecf20Sopenharmony_ci	{ 0x25, 0x00 },
988c2ecf20Sopenharmony_ci	{ 0x26, 0x00 },
998c2ecf20Sopenharmony_ci	{ 0x27, 0x2a },
1008c2ecf20Sopenharmony_ci	{ 0x28, 0xc0 },
1018c2ecf20Sopenharmony_ci	{ 0x29, 0xf3 },
1028c2ecf20Sopenharmony_ci	{ 0x2a, 0x33 },
1038c2ecf20Sopenharmony_ci	{ 0x2b, 0x00 },
1048c2ecf20Sopenharmony_ci	{ 0x2c, 0x0c },
1058c2ecf20Sopenharmony_ci	{ 0x31, 0x00 },
1068c2ecf20Sopenharmony_ci	{ 0x36, 0x00 },
1078c2ecf20Sopenharmony_ci	{ 0x37, 0x00 },
1088c2ecf20Sopenharmony_ci	{ 0x38, 0x00 },
1098c2ecf20Sopenharmony_ci	{ 0x39, 0x01 },
1108c2ecf20Sopenharmony_ci	{ 0x3a, 0xee },
1118c2ecf20Sopenharmony_ci	{ 0x3b, 0xff },
1128c2ecf20Sopenharmony_ci	{ 0x3c, 0x7e },
1138c2ecf20Sopenharmony_ci	{ 0x3d, 0xc0 },
1148c2ecf20Sopenharmony_ci	{ 0x3e, 0x26 },
1158c2ecf20Sopenharmony_ci	{ 0x3f, 0x00 },
1168c2ecf20Sopenharmony_ci	{ 0x48, 0x00 },
1178c2ecf20Sopenharmony_ci	{ 0x49, 0x00 },
1188c2ecf20Sopenharmony_ci	{ 0x4a, 0x00 },
1198c2ecf20Sopenharmony_ci	{ 0x4b, 0x04 },
1208c2ecf20Sopenharmony_ci	{ 0x4c, 0x00 },
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const struct regmap_range sta350_write_regs_range[] = {
1248c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_CONFA,  STA350_AUTO2),
1258c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_C1CFG,  STA350_FDRC2),
1268c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_EQCFG,  STA350_EVOLRES),
1278c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_NSHAPE, STA350_MISC2),
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct regmap_range sta350_read_regs_range[] = {
1318c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_CONFA,  STA350_AUTO2),
1328c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_C1CFG,  STA350_STATUS),
1338c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_EQCFG,  STA350_EVOLRES),
1348c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_NSHAPE, STA350_MISC2),
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic const struct regmap_range sta350_volatile_regs_range[] = {
1388c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_CFADDR2, STA350_CFUD),
1398c2ecf20Sopenharmony_ci	regmap_reg_range(STA350_STATUS,  STA350_STATUS),
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic const struct regmap_access_table sta350_write_regs = {
1438c2ecf20Sopenharmony_ci	.yes_ranges =	sta350_write_regs_range,
1448c2ecf20Sopenharmony_ci	.n_yes_ranges =	ARRAY_SIZE(sta350_write_regs_range),
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic const struct regmap_access_table sta350_read_regs = {
1488c2ecf20Sopenharmony_ci	.yes_ranges =	sta350_read_regs_range,
1498c2ecf20Sopenharmony_ci	.n_yes_ranges =	ARRAY_SIZE(sta350_read_regs_range),
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct regmap_access_table sta350_volatile_regs = {
1538c2ecf20Sopenharmony_ci	.yes_ranges =	sta350_volatile_regs_range,
1548c2ecf20Sopenharmony_ci	.n_yes_ranges =	ARRAY_SIZE(sta350_volatile_regs_range),
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* regulator power supply names */
1588c2ecf20Sopenharmony_cistatic const char * const sta350_supply_names[] = {
1598c2ecf20Sopenharmony_ci	"vdd-dig",	/* digital supply, 3.3V */
1608c2ecf20Sopenharmony_ci	"vdd-pll",	/* pll supply, 3.3V */
1618c2ecf20Sopenharmony_ci	"vcc"		/* power amp supply, 5V - 26V */
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* codec private data */
1658c2ecf20Sopenharmony_cistruct sta350_priv {
1668c2ecf20Sopenharmony_ci	struct regmap *regmap;
1678c2ecf20Sopenharmony_ci	struct regulator_bulk_data supplies[ARRAY_SIZE(sta350_supply_names)];
1688c2ecf20Sopenharmony_ci	struct sta350_platform_data *pdata;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	unsigned int mclk;
1718c2ecf20Sopenharmony_ci	unsigned int format;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	u32 coef_shadow[STA350_COEF_COUNT];
1748c2ecf20Sopenharmony_ci	int shutdown;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	struct gpio_desc *gpiod_nreset;
1778c2ecf20Sopenharmony_ci	struct gpio_desc *gpiod_power_down;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	struct mutex coeff_lock;
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(mvol_tlv, -12750, 50, 1);
1838c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(chvol_tlv, -7950, 50, 1);
1848c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(tone_tlv, -1200, 200, 0);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const char * const sta350_drc_ac[] = {
1878c2ecf20Sopenharmony_ci	"Anti-Clipping", "Dynamic Range Compression"
1888c2ecf20Sopenharmony_ci};
1898c2ecf20Sopenharmony_cistatic const char * const sta350_auto_gc_mode[] = {
1908c2ecf20Sopenharmony_ci	"User", "AC no clipping", "AC limited clipping (10%)",
1918c2ecf20Sopenharmony_ci	"DRC nighttime listening mode"
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_cistatic const char * const sta350_auto_xo_mode[] = {
1948c2ecf20Sopenharmony_ci	"User", "80Hz", "100Hz", "120Hz", "140Hz", "160Hz", "180Hz",
1958c2ecf20Sopenharmony_ci	"200Hz", "220Hz", "240Hz", "260Hz", "280Hz", "300Hz", "320Hz",
1968c2ecf20Sopenharmony_ci	"340Hz", "360Hz"
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_cistatic const char * const sta350_binary_output[] = {
1998c2ecf20Sopenharmony_ci	"FFX 3-state output - normal operation", "Binary output"
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_cistatic const char * const sta350_limiter_select[] = {
2028c2ecf20Sopenharmony_ci	"Limiter Disabled", "Limiter #1", "Limiter #2"
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_cistatic const char * const sta350_limiter_attack_rate[] = {
2058c2ecf20Sopenharmony_ci	"3.1584", "2.7072", "2.2560", "1.8048", "1.3536", "0.9024",
2068c2ecf20Sopenharmony_ci	"0.4512", "0.2256", "0.1504", "0.1123", "0.0902", "0.0752",
2078c2ecf20Sopenharmony_ci	"0.0645", "0.0564", "0.0501", "0.0451"
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_cistatic const char * const sta350_limiter_release_rate[] = {
2108c2ecf20Sopenharmony_ci	"0.5116", "0.1370", "0.0744", "0.0499", "0.0360", "0.0299",
2118c2ecf20Sopenharmony_ci	"0.0264", "0.0208", "0.0198", "0.0172", "0.0147", "0.0137",
2128c2ecf20Sopenharmony_ci	"0.0134", "0.0117", "0.0110", "0.0104"
2138c2ecf20Sopenharmony_ci};
2148c2ecf20Sopenharmony_cistatic const char * const sta350_noise_shaper_type[] = {
2158c2ecf20Sopenharmony_ci	"Third order", "Fourth order"
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_RANGE(sta350_limiter_ac_attack_tlv,
2198c2ecf20Sopenharmony_ci	0, 7, TLV_DB_SCALE_ITEM(-1200, 200, 0),
2208c2ecf20Sopenharmony_ci	8, 16, TLV_DB_SCALE_ITEM(300, 100, 0),
2218c2ecf20Sopenharmony_ci);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_RANGE(sta350_limiter_ac_release_tlv,
2248c2ecf20Sopenharmony_ci	0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
2258c2ecf20Sopenharmony_ci	1, 1, TLV_DB_SCALE_ITEM(-2900, 0, 0),
2268c2ecf20Sopenharmony_ci	2, 2, TLV_DB_SCALE_ITEM(-2000, 0, 0),
2278c2ecf20Sopenharmony_ci	3, 8, TLV_DB_SCALE_ITEM(-1400, 200, 0),
2288c2ecf20Sopenharmony_ci	8, 16, TLV_DB_SCALE_ITEM(-700, 100, 0),
2298c2ecf20Sopenharmony_ci);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_RANGE(sta350_limiter_drc_attack_tlv,
2328c2ecf20Sopenharmony_ci	0, 7, TLV_DB_SCALE_ITEM(-3100, 200, 0),
2338c2ecf20Sopenharmony_ci	8, 13, TLV_DB_SCALE_ITEM(-1600, 100, 0),
2348c2ecf20Sopenharmony_ci	14, 16, TLV_DB_SCALE_ITEM(-1000, 300, 0),
2358c2ecf20Sopenharmony_ci);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_RANGE(sta350_limiter_drc_release_tlv,
2388c2ecf20Sopenharmony_ci	0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
2398c2ecf20Sopenharmony_ci	1, 2, TLV_DB_SCALE_ITEM(-3800, 200, 0),
2408c2ecf20Sopenharmony_ci	3, 4, TLV_DB_SCALE_ITEM(-3300, 200, 0),
2418c2ecf20Sopenharmony_ci	5, 12, TLV_DB_SCALE_ITEM(-3000, 200, 0),
2428c2ecf20Sopenharmony_ci	13, 16, TLV_DB_SCALE_ITEM(-1500, 300, 0),
2438c2ecf20Sopenharmony_ci);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_drc_ac_enum,
2468c2ecf20Sopenharmony_ci			    STA350_CONFD, STA350_CONFD_DRC_SHIFT,
2478c2ecf20Sopenharmony_ci			    sta350_drc_ac);
2488c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_noise_shaper_enum,
2498c2ecf20Sopenharmony_ci			    STA350_CONFE, STA350_CONFE_NSBW_SHIFT,
2508c2ecf20Sopenharmony_ci			    sta350_noise_shaper_type);
2518c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_auto_gc_enum,
2528c2ecf20Sopenharmony_ci			    STA350_AUTO1, STA350_AUTO1_AMGC_SHIFT,
2538c2ecf20Sopenharmony_ci			    sta350_auto_gc_mode);
2548c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_auto_xo_enum,
2558c2ecf20Sopenharmony_ci			    STA350_AUTO2, STA350_AUTO2_XO_SHIFT,
2568c2ecf20Sopenharmony_ci			    sta350_auto_xo_mode);
2578c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch1_enum,
2588c2ecf20Sopenharmony_ci			    STA350_C1CFG, STA350_CxCFG_BO_SHIFT,
2598c2ecf20Sopenharmony_ci			    sta350_binary_output);
2608c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch2_enum,
2618c2ecf20Sopenharmony_ci			    STA350_C2CFG, STA350_CxCFG_BO_SHIFT,
2628c2ecf20Sopenharmony_ci			    sta350_binary_output);
2638c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch3_enum,
2648c2ecf20Sopenharmony_ci			    STA350_C3CFG, STA350_CxCFG_BO_SHIFT,
2658c2ecf20Sopenharmony_ci			    sta350_binary_output);
2668c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter_ch1_enum,
2678c2ecf20Sopenharmony_ci			    STA350_C1CFG, STA350_CxCFG_LS_SHIFT,
2688c2ecf20Sopenharmony_ci			    sta350_limiter_select);
2698c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter_ch2_enum,
2708c2ecf20Sopenharmony_ci			    STA350_C2CFG, STA350_CxCFG_LS_SHIFT,
2718c2ecf20Sopenharmony_ci			    sta350_limiter_select);
2728c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter_ch3_enum,
2738c2ecf20Sopenharmony_ci			    STA350_C3CFG, STA350_CxCFG_LS_SHIFT,
2748c2ecf20Sopenharmony_ci			    sta350_limiter_select);
2758c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter1_attack_rate_enum,
2768c2ecf20Sopenharmony_ci			    STA350_L1AR, STA350_LxA_SHIFT,
2778c2ecf20Sopenharmony_ci			    sta350_limiter_attack_rate);
2788c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter2_attack_rate_enum,
2798c2ecf20Sopenharmony_ci			    STA350_L2AR, STA350_LxA_SHIFT,
2808c2ecf20Sopenharmony_ci			    sta350_limiter_attack_rate);
2818c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter1_release_rate_enum,
2828c2ecf20Sopenharmony_ci			    STA350_L1AR, STA350_LxR_SHIFT,
2838c2ecf20Sopenharmony_ci			    sta350_limiter_release_rate);
2848c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(sta350_limiter2_release_rate_enum,
2858c2ecf20Sopenharmony_ci			    STA350_L2AR, STA350_LxR_SHIFT,
2868c2ecf20Sopenharmony_ci			    sta350_limiter_release_rate);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/*
2898c2ecf20Sopenharmony_ci * byte array controls for setting biquad, mixer, scaling coefficients;
2908c2ecf20Sopenharmony_ci * for biquads all five coefficients need to be set in one go,
2918c2ecf20Sopenharmony_ci * mixer and pre/postscale coefs can be set individually;
2928c2ecf20Sopenharmony_ci * each coef is 24bit, the bytes are ordered in the same way
2938c2ecf20Sopenharmony_ci * as given in the STA350 data sheet (big endian; b1, b2, a1, a2, b0)
2948c2ecf20Sopenharmony_ci */
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int sta350_coefficient_info(struct snd_kcontrol *kcontrol,
2978c2ecf20Sopenharmony_ci				   struct snd_ctl_elem_info *uinfo)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	int numcoef = kcontrol->private_value >> 16;
3008c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3018c2ecf20Sopenharmony_ci	uinfo->count = 3 * numcoef;
3028c2ecf20Sopenharmony_ci	return 0;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int sta350_coefficient_get(struct snd_kcontrol *kcontrol,
3068c2ecf20Sopenharmony_ci				  struct snd_ctl_elem_value *ucontrol)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
3098c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
3108c2ecf20Sopenharmony_ci	int numcoef = kcontrol->private_value >> 16;
3118c2ecf20Sopenharmony_ci	int index = kcontrol->private_value & 0xffff;
3128c2ecf20Sopenharmony_ci	unsigned int cfud, val;
3138c2ecf20Sopenharmony_ci	int i, ret = 0;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	mutex_lock(&sta350->coeff_lock);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* preserve reserved bits in STA350_CFUD */
3188c2ecf20Sopenharmony_ci	regmap_read(sta350->regmap, STA350_CFUD, &cfud);
3198c2ecf20Sopenharmony_ci	cfud &= 0xf0;
3208c2ecf20Sopenharmony_ci	/*
3218c2ecf20Sopenharmony_ci	 * chip documentation does not say if the bits are self clearing,
3228c2ecf20Sopenharmony_ci	 * so do it explicitly
3238c2ecf20Sopenharmony_ci	 */
3248c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_CFUD, cfud);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_CFADDR2, index);
3278c2ecf20Sopenharmony_ci	if (numcoef == 1) {
3288c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x04);
3298c2ecf20Sopenharmony_ci	} else if (numcoef == 5) {
3308c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x08);
3318c2ecf20Sopenharmony_ci	} else {
3328c2ecf20Sopenharmony_ci		ret = -EINVAL;
3338c2ecf20Sopenharmony_ci		goto exit_unlock;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	for (i = 0; i < 3 * numcoef; i++) {
3378c2ecf20Sopenharmony_ci		regmap_read(sta350->regmap, STA350_B1CF1 + i, &val);
3388c2ecf20Sopenharmony_ci		ucontrol->value.bytes.data[i] = val;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ciexit_unlock:
3428c2ecf20Sopenharmony_ci	mutex_unlock(&sta350->coeff_lock);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return ret;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int sta350_coefficient_put(struct snd_kcontrol *kcontrol,
3488c2ecf20Sopenharmony_ci				  struct snd_ctl_elem_value *ucontrol)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
3518c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
3528c2ecf20Sopenharmony_ci	int numcoef = kcontrol->private_value >> 16;
3538c2ecf20Sopenharmony_ci	int index = kcontrol->private_value & 0xffff;
3548c2ecf20Sopenharmony_ci	unsigned int cfud;
3558c2ecf20Sopenharmony_ci	int i;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* preserve reserved bits in STA350_CFUD */
3588c2ecf20Sopenharmony_ci	regmap_read(sta350->regmap, STA350_CFUD, &cfud);
3598c2ecf20Sopenharmony_ci	cfud &= 0xf0;
3608c2ecf20Sopenharmony_ci	/*
3618c2ecf20Sopenharmony_ci	 * chip documentation does not say if the bits are self clearing,
3628c2ecf20Sopenharmony_ci	 * so do it explicitly
3638c2ecf20Sopenharmony_ci	 */
3648c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_CFUD, cfud);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_CFADDR2, index);
3678c2ecf20Sopenharmony_ci	for (i = 0; i < numcoef && (index + i < STA350_COEF_COUNT); i++)
3688c2ecf20Sopenharmony_ci		sta350->coef_shadow[index + i] =
3698c2ecf20Sopenharmony_ci			  (ucontrol->value.bytes.data[3 * i] << 16)
3708c2ecf20Sopenharmony_ci			| (ucontrol->value.bytes.data[3 * i + 1] << 8)
3718c2ecf20Sopenharmony_ci			| (ucontrol->value.bytes.data[3 * i + 2]);
3728c2ecf20Sopenharmony_ci	for (i = 0; i < 3 * numcoef; i++)
3738c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_B1CF1 + i,
3748c2ecf20Sopenharmony_ci			     ucontrol->value.bytes.data[i]);
3758c2ecf20Sopenharmony_ci	if (numcoef == 1)
3768c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x01);
3778c2ecf20Sopenharmony_ci	else if (numcoef == 5)
3788c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x02);
3798c2ecf20Sopenharmony_ci	else
3808c2ecf20Sopenharmony_ci		return -EINVAL;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	return 0;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic int sta350_sync_coef_shadow(struct snd_soc_component *component)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
3888c2ecf20Sopenharmony_ci	unsigned int cfud;
3898c2ecf20Sopenharmony_ci	int i;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/* preserve reserved bits in STA350_CFUD */
3928c2ecf20Sopenharmony_ci	regmap_read(sta350->regmap, STA350_CFUD, &cfud);
3938c2ecf20Sopenharmony_ci	cfud &= 0xf0;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	for (i = 0; i < STA350_COEF_COUNT; i++) {
3968c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFADDR2, i);
3978c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_B1CF1,
3988c2ecf20Sopenharmony_ci			     (sta350->coef_shadow[i] >> 16) & 0xff);
3998c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_B1CF2,
4008c2ecf20Sopenharmony_ci			     (sta350->coef_shadow[i] >> 8) & 0xff);
4018c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_B1CF3,
4028c2ecf20Sopenharmony_ci			     (sta350->coef_shadow[i]) & 0xff);
4038c2ecf20Sopenharmony_ci		/*
4048c2ecf20Sopenharmony_ci		 * chip documentation does not say if the bits are
4058c2ecf20Sopenharmony_ci		 * self-clearing, so do it explicitly
4068c2ecf20Sopenharmony_ci		 */
4078c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud);
4088c2ecf20Sopenharmony_ci		regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x01);
4098c2ecf20Sopenharmony_ci	}
4108c2ecf20Sopenharmony_ci	return 0;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic int sta350_cache_sync(struct snd_soc_component *component)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
4168c2ecf20Sopenharmony_ci	unsigned int mute;
4178c2ecf20Sopenharmony_ci	int rc;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* mute during register sync */
4208c2ecf20Sopenharmony_ci	regmap_read(sta350->regmap, STA350_CFUD, &mute);
4218c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_MMUTE, mute | STA350_MMUTE_MMUTE);
4228c2ecf20Sopenharmony_ci	sta350_sync_coef_shadow(component);
4238c2ecf20Sopenharmony_ci	rc = regcache_sync(sta350->regmap);
4248c2ecf20Sopenharmony_ci	regmap_write(sta350->regmap, STA350_MMUTE, mute);
4258c2ecf20Sopenharmony_ci	return rc;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci#define SINGLE_COEF(xname, index) \
4298c2ecf20Sopenharmony_ci{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
4308c2ecf20Sopenharmony_ci	.info = sta350_coefficient_info, \
4318c2ecf20Sopenharmony_ci	.get = sta350_coefficient_get,\
4328c2ecf20Sopenharmony_ci	.put = sta350_coefficient_put, \
4338c2ecf20Sopenharmony_ci	.private_value = index | (1 << 16) }
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci#define BIQUAD_COEFS(xname, index) \
4368c2ecf20Sopenharmony_ci{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
4378c2ecf20Sopenharmony_ci	.info = sta350_coefficient_info, \
4388c2ecf20Sopenharmony_ci	.get = sta350_coefficient_get,\
4398c2ecf20Sopenharmony_ci	.put = sta350_coefficient_put, \
4408c2ecf20Sopenharmony_ci	.private_value = index | (5 << 16) }
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new sta350_snd_controls[] = {
4438c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Master Volume", STA350_MVOL, 0, 0xff, 1, mvol_tlv),
4448c2ecf20Sopenharmony_ci/* VOL */
4458c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Ch1 Volume", STA350_C1VOL, 0, 0xff, 1, chvol_tlv),
4468c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Ch2 Volume", STA350_C2VOL, 0, 0xff, 1, chvol_tlv),
4478c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Ch3 Volume", STA350_C3VOL, 0, 0xff, 1, chvol_tlv),
4488c2ecf20Sopenharmony_ci/* CONFD */
4498c2ecf20Sopenharmony_ciSOC_SINGLE("High Pass Filter Bypass Switch",
4508c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_HPB_SHIFT, 1, 1),
4518c2ecf20Sopenharmony_ciSOC_SINGLE("De-emphasis Filter Switch",
4528c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_DEMP_SHIFT, 1, 0),
4538c2ecf20Sopenharmony_ciSOC_SINGLE("DSP Bypass Switch",
4548c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_DSPB_SHIFT, 1, 0),
4558c2ecf20Sopenharmony_ciSOC_SINGLE("Post-scale Link Switch",
4568c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_PSL_SHIFT, 1, 0),
4578c2ecf20Sopenharmony_ciSOC_SINGLE("Biquad Coefficient Link Switch",
4588c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_BQL_SHIFT, 1, 0),
4598c2ecf20Sopenharmony_ciSOC_ENUM("Compressor/Limiter Switch", sta350_drc_ac_enum),
4608c2ecf20Sopenharmony_ciSOC_ENUM("Noise Shaper Bandwidth", sta350_noise_shaper_enum),
4618c2ecf20Sopenharmony_ciSOC_SINGLE("Zero-detect Mute Enable Switch",
4628c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_ZDE_SHIFT, 1, 0),
4638c2ecf20Sopenharmony_ciSOC_SINGLE("Submix Mode Switch",
4648c2ecf20Sopenharmony_ci	   STA350_CONFD, STA350_CONFD_SME_SHIFT, 1, 0),
4658c2ecf20Sopenharmony_ci/* CONFE */
4668c2ecf20Sopenharmony_ciSOC_SINGLE("Zero Cross Switch", STA350_CONFE, STA350_CONFE_ZCE_SHIFT, 1, 0),
4678c2ecf20Sopenharmony_ciSOC_SINGLE("Soft Ramp Switch", STA350_CONFE, STA350_CONFE_SVE_SHIFT, 1, 0),
4688c2ecf20Sopenharmony_ci/* MUTE */
4698c2ecf20Sopenharmony_ciSOC_SINGLE("Master Switch", STA350_MMUTE, STA350_MMUTE_MMUTE_SHIFT, 1, 1),
4708c2ecf20Sopenharmony_ciSOC_SINGLE("Ch1 Switch", STA350_MMUTE, STA350_MMUTE_C1M_SHIFT, 1, 1),
4718c2ecf20Sopenharmony_ciSOC_SINGLE("Ch2 Switch", STA350_MMUTE, STA350_MMUTE_C2M_SHIFT, 1, 1),
4728c2ecf20Sopenharmony_ciSOC_SINGLE("Ch3 Switch", STA350_MMUTE, STA350_MMUTE_C3M_SHIFT, 1, 1),
4738c2ecf20Sopenharmony_ci/* AUTOx */
4748c2ecf20Sopenharmony_ciSOC_ENUM("Automode GC", sta350_auto_gc_enum),
4758c2ecf20Sopenharmony_ciSOC_ENUM("Automode XO", sta350_auto_xo_enum),
4768c2ecf20Sopenharmony_ci/* CxCFG */
4778c2ecf20Sopenharmony_ciSOC_SINGLE("Ch1 Tone Control Bypass Switch",
4788c2ecf20Sopenharmony_ci	   STA350_C1CFG, STA350_CxCFG_TCB_SHIFT, 1, 0),
4798c2ecf20Sopenharmony_ciSOC_SINGLE("Ch2 Tone Control Bypass Switch",
4808c2ecf20Sopenharmony_ci	   STA350_C2CFG, STA350_CxCFG_TCB_SHIFT, 1, 0),
4818c2ecf20Sopenharmony_ciSOC_SINGLE("Ch1 EQ Bypass Switch",
4828c2ecf20Sopenharmony_ci	   STA350_C1CFG, STA350_CxCFG_EQBP_SHIFT, 1, 0),
4838c2ecf20Sopenharmony_ciSOC_SINGLE("Ch2 EQ Bypass Switch",
4848c2ecf20Sopenharmony_ci	   STA350_C2CFG, STA350_CxCFG_EQBP_SHIFT, 1, 0),
4858c2ecf20Sopenharmony_ciSOC_SINGLE("Ch1 Master Volume Bypass Switch",
4868c2ecf20Sopenharmony_ci	   STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
4878c2ecf20Sopenharmony_ciSOC_SINGLE("Ch2 Master Volume Bypass Switch",
4888c2ecf20Sopenharmony_ci	   STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
4898c2ecf20Sopenharmony_ciSOC_SINGLE("Ch3 Master Volume Bypass Switch",
4908c2ecf20Sopenharmony_ci	   STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
4918c2ecf20Sopenharmony_ciSOC_ENUM("Ch1 Binary Output Select", sta350_binary_output_ch1_enum),
4928c2ecf20Sopenharmony_ciSOC_ENUM("Ch2 Binary Output Select", sta350_binary_output_ch2_enum),
4938c2ecf20Sopenharmony_ciSOC_ENUM("Ch3 Binary Output Select", sta350_binary_output_ch3_enum),
4948c2ecf20Sopenharmony_ciSOC_ENUM("Ch1 Limiter Select", sta350_limiter_ch1_enum),
4958c2ecf20Sopenharmony_ciSOC_ENUM("Ch2 Limiter Select", sta350_limiter_ch2_enum),
4968c2ecf20Sopenharmony_ciSOC_ENUM("Ch3 Limiter Select", sta350_limiter_ch3_enum),
4978c2ecf20Sopenharmony_ci/* TONE */
4988c2ecf20Sopenharmony_ciSOC_SINGLE_RANGE_TLV("Bass Tone Control Volume",
4998c2ecf20Sopenharmony_ci		     STA350_TONE, STA350_TONE_BTC_SHIFT, 1, 13, 0, tone_tlv),
5008c2ecf20Sopenharmony_ciSOC_SINGLE_RANGE_TLV("Treble Tone Control Volume",
5018c2ecf20Sopenharmony_ci		     STA350_TONE, STA350_TONE_TTC_SHIFT, 1, 13, 0, tone_tlv),
5028c2ecf20Sopenharmony_ciSOC_ENUM("Limiter1 Attack Rate (dB/ms)", sta350_limiter1_attack_rate_enum),
5038c2ecf20Sopenharmony_ciSOC_ENUM("Limiter2 Attack Rate (dB/ms)", sta350_limiter2_attack_rate_enum),
5048c2ecf20Sopenharmony_ciSOC_ENUM("Limiter1 Release Rate (dB/ms)", sta350_limiter1_release_rate_enum),
5058c2ecf20Sopenharmony_ciSOC_ENUM("Limiter2 Release Rate (dB/ms)", sta350_limiter2_release_rate_enum),
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci/*
5088c2ecf20Sopenharmony_ci * depending on mode, the attack/release thresholds have
5098c2ecf20Sopenharmony_ci * two different enum definitions; provide both
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter1 Attack Threshold (AC Mode)",
5128c2ecf20Sopenharmony_ci	       STA350_L1ATRT, STA350_LxA_SHIFT,
5138c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_ac_attack_tlv),
5148c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter2 Attack Threshold (AC Mode)",
5158c2ecf20Sopenharmony_ci	       STA350_L2ATRT, STA350_LxA_SHIFT,
5168c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_ac_attack_tlv),
5178c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter1 Release Threshold (AC Mode)",
5188c2ecf20Sopenharmony_ci	       STA350_L1ATRT, STA350_LxR_SHIFT,
5198c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_ac_release_tlv),
5208c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter2 Release Threshold (AC Mode)",
5218c2ecf20Sopenharmony_ci	       STA350_L2ATRT, STA350_LxR_SHIFT,
5228c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_ac_release_tlv),
5238c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter1 Attack Threshold (DRC Mode)",
5248c2ecf20Sopenharmony_ci	       STA350_L1ATRT, STA350_LxA_SHIFT,
5258c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_drc_attack_tlv),
5268c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter2 Attack Threshold (DRC Mode)",
5278c2ecf20Sopenharmony_ci	       STA350_L2ATRT, STA350_LxA_SHIFT,
5288c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_drc_attack_tlv),
5298c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter1 Release Threshold (DRC Mode)",
5308c2ecf20Sopenharmony_ci	       STA350_L1ATRT, STA350_LxR_SHIFT,
5318c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_drc_release_tlv),
5328c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter2 Release Threshold (DRC Mode)",
5338c2ecf20Sopenharmony_ci	       STA350_L2ATRT, STA350_LxR_SHIFT,
5348c2ecf20Sopenharmony_ci	       16, 0, sta350_limiter_drc_release_tlv),
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch1 - Biquad 1", 0),
5378c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch1 - Biquad 2", 5),
5388c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch1 - Biquad 3", 10),
5398c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch1 - Biquad 4", 15),
5408c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch2 - Biquad 1", 20),
5418c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch2 - Biquad 2", 25),
5428c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch2 - Biquad 3", 30),
5438c2ecf20Sopenharmony_ciBIQUAD_COEFS("Ch2 - Biquad 4", 35),
5448c2ecf20Sopenharmony_ciBIQUAD_COEFS("High-pass", 40),
5458c2ecf20Sopenharmony_ciBIQUAD_COEFS("Low-pass", 45),
5468c2ecf20Sopenharmony_ciSINGLE_COEF("Ch1 - Prescale", 50),
5478c2ecf20Sopenharmony_ciSINGLE_COEF("Ch2 - Prescale", 51),
5488c2ecf20Sopenharmony_ciSINGLE_COEF("Ch1 - Postscale", 52),
5498c2ecf20Sopenharmony_ciSINGLE_COEF("Ch2 - Postscale", 53),
5508c2ecf20Sopenharmony_ciSINGLE_COEF("Ch3 - Postscale", 54),
5518c2ecf20Sopenharmony_ciSINGLE_COEF("Thermal warning - Postscale", 55),
5528c2ecf20Sopenharmony_ciSINGLE_COEF("Ch1 - Mix 1", 56),
5538c2ecf20Sopenharmony_ciSINGLE_COEF("Ch1 - Mix 2", 57),
5548c2ecf20Sopenharmony_ciSINGLE_COEF("Ch2 - Mix 1", 58),
5558c2ecf20Sopenharmony_ciSINGLE_COEF("Ch2 - Mix 2", 59),
5568c2ecf20Sopenharmony_ciSINGLE_COEF("Ch3 - Mix 1", 60),
5578c2ecf20Sopenharmony_ciSINGLE_COEF("Ch3 - Mix 2", 61),
5588c2ecf20Sopenharmony_ci};
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget sta350_dapm_widgets[] = {
5618c2ecf20Sopenharmony_ciSND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
5628c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("LEFT"),
5638c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("RIGHT"),
5648c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("SUB"),
5658c2ecf20Sopenharmony_ci};
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route sta350_dapm_routes[] = {
5688c2ecf20Sopenharmony_ci	{ "LEFT", NULL, "DAC" },
5698c2ecf20Sopenharmony_ci	{ "RIGHT", NULL, "DAC" },
5708c2ecf20Sopenharmony_ci	{ "SUB", NULL, "DAC" },
5718c2ecf20Sopenharmony_ci	{ "DAC", NULL, "Playback" },
5728c2ecf20Sopenharmony_ci};
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci/* MCLK interpolation ratio per fs */
5758c2ecf20Sopenharmony_cistatic struct {
5768c2ecf20Sopenharmony_ci	int fs;
5778c2ecf20Sopenharmony_ci	int ir;
5788c2ecf20Sopenharmony_ci} interpolation_ratios[] = {
5798c2ecf20Sopenharmony_ci	{ 32000, 0 },
5808c2ecf20Sopenharmony_ci	{ 44100, 0 },
5818c2ecf20Sopenharmony_ci	{ 48000, 0 },
5828c2ecf20Sopenharmony_ci	{ 88200, 1 },
5838c2ecf20Sopenharmony_ci	{ 96000, 1 },
5848c2ecf20Sopenharmony_ci	{ 176400, 2 },
5858c2ecf20Sopenharmony_ci	{ 192000, 2 },
5868c2ecf20Sopenharmony_ci};
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/* MCLK to fs clock ratios */
5898c2ecf20Sopenharmony_cistatic int mcs_ratio_table[3][6] = {
5908c2ecf20Sopenharmony_ci	{ 768, 512, 384, 256, 128, 576 },
5918c2ecf20Sopenharmony_ci	{ 384, 256, 192, 128,  64,   0 },
5928c2ecf20Sopenharmony_ci	{ 192, 128,  96,  64,  32,   0 },
5938c2ecf20Sopenharmony_ci};
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci/**
5968c2ecf20Sopenharmony_ci * sta350_set_dai_sysclk - configure MCLK
5978c2ecf20Sopenharmony_ci * @codec_dai: the codec DAI
5988c2ecf20Sopenharmony_ci * @clk_id: the clock ID (ignored)
5998c2ecf20Sopenharmony_ci * @freq: the MCLK input frequency
6008c2ecf20Sopenharmony_ci * @dir: the clock direction (ignored)
6018c2ecf20Sopenharmony_ci *
6028c2ecf20Sopenharmony_ci * The value of MCLK is used to determine which sample rates are supported
6038c2ecf20Sopenharmony_ci * by the STA350, based on the mcs_ratio_table.
6048c2ecf20Sopenharmony_ci *
6058c2ecf20Sopenharmony_ci * This function must be called by the machine driver's 'startup' function,
6068c2ecf20Sopenharmony_ci * otherwise the list of supported sample rates will not be available in
6078c2ecf20Sopenharmony_ci * time for ALSA.
6088c2ecf20Sopenharmony_ci */
6098c2ecf20Sopenharmony_cistatic int sta350_set_dai_sysclk(struct snd_soc_dai *codec_dai,
6108c2ecf20Sopenharmony_ci				 int clk_id, unsigned int freq, int dir)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
6138c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "mclk=%u\n", freq);
6168c2ecf20Sopenharmony_ci	sta350->mclk = freq;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return 0;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci/**
6228c2ecf20Sopenharmony_ci * sta350_set_dai_fmt - configure the codec for the selected audio format
6238c2ecf20Sopenharmony_ci * @codec_dai: the codec DAI
6248c2ecf20Sopenharmony_ci * @fmt: a SND_SOC_DAIFMT_x value indicating the data format
6258c2ecf20Sopenharmony_ci *
6268c2ecf20Sopenharmony_ci * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
6278c2ecf20Sopenharmony_ci * codec accordingly.
6288c2ecf20Sopenharmony_ci */
6298c2ecf20Sopenharmony_cistatic int sta350_set_dai_fmt(struct snd_soc_dai *codec_dai,
6308c2ecf20Sopenharmony_ci			      unsigned int fmt)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
6338c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
6348c2ecf20Sopenharmony_ci	unsigned int confb = 0;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6378c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
6388c2ecf20Sopenharmony_ci		break;
6398c2ecf20Sopenharmony_ci	default:
6408c2ecf20Sopenharmony_ci		return -EINVAL;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6448c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
6458c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_RIGHT_J:
6468c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
6478c2ecf20Sopenharmony_ci		sta350->format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci	default:
6508c2ecf20Sopenharmony_ci		return -EINVAL;
6518c2ecf20Sopenharmony_ci	}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6548c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
6558c2ecf20Sopenharmony_ci		confb |= STA350_CONFB_C2IM;
6568c2ecf20Sopenharmony_ci		break;
6578c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
6588c2ecf20Sopenharmony_ci		confb |= STA350_CONFB_C1IM;
6598c2ecf20Sopenharmony_ci		break;
6608c2ecf20Sopenharmony_ci	default:
6618c2ecf20Sopenharmony_ci		return -EINVAL;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return regmap_update_bits(sta350->regmap, STA350_CONFB,
6658c2ecf20Sopenharmony_ci				  STA350_CONFB_C1IM | STA350_CONFB_C2IM, confb);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci/**
6698c2ecf20Sopenharmony_ci * sta350_hw_params - program the STA350 with the given hardware parameters.
6708c2ecf20Sopenharmony_ci * @substream: the audio stream
6718c2ecf20Sopenharmony_ci * @params: the hardware parameters to set
6728c2ecf20Sopenharmony_ci * @dai: the SOC DAI (ignored)
6738c2ecf20Sopenharmony_ci *
6748c2ecf20Sopenharmony_ci * This function programs the hardware with the values provided.
6758c2ecf20Sopenharmony_ci * Specifically, the sample rate and the data format.
6768c2ecf20Sopenharmony_ci */
6778c2ecf20Sopenharmony_cistatic int sta350_hw_params(struct snd_pcm_substream *substream,
6788c2ecf20Sopenharmony_ci			    struct snd_pcm_hw_params *params,
6798c2ecf20Sopenharmony_ci			    struct snd_soc_dai *dai)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
6828c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
6838c2ecf20Sopenharmony_ci	int i, mcs = -EINVAL, ir = -EINVAL;
6848c2ecf20Sopenharmony_ci	unsigned int confa, confb;
6858c2ecf20Sopenharmony_ci	unsigned int rate, ratio;
6868c2ecf20Sopenharmony_ci	int ret;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	if (!sta350->mclk) {
6898c2ecf20Sopenharmony_ci		dev_err(component->dev,
6908c2ecf20Sopenharmony_ci			"sta350->mclk is unset. Unable to determine ratio\n");
6918c2ecf20Sopenharmony_ci		return -EIO;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	rate = params_rate(params);
6958c2ecf20Sopenharmony_ci	ratio = sta350->mclk / rate;
6968c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "rate: %u, ratio: %u\n", rate, ratio);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) {
6998c2ecf20Sopenharmony_ci		if (interpolation_ratios[i].fs == rate) {
7008c2ecf20Sopenharmony_ci			ir = interpolation_ratios[i].ir;
7018c2ecf20Sopenharmony_ci			break;
7028c2ecf20Sopenharmony_ci		}
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (ir < 0) {
7068c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported samplerate: %u\n", rate);
7078c2ecf20Sopenharmony_ci		return -EINVAL;
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++) {
7118c2ecf20Sopenharmony_ci		if (mcs_ratio_table[ir][i] == ratio) {
7128c2ecf20Sopenharmony_ci			mcs = i;
7138c2ecf20Sopenharmony_ci			break;
7148c2ecf20Sopenharmony_ci		}
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (mcs < 0) {
7188c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unresolvable ratio: %u\n", ratio);
7198c2ecf20Sopenharmony_ci		return -EINVAL;
7208c2ecf20Sopenharmony_ci	}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	confa = (ir << STA350_CONFA_IR_SHIFT) |
7238c2ecf20Sopenharmony_ci		(mcs << STA350_CONFA_MCS_SHIFT);
7248c2ecf20Sopenharmony_ci	confb = 0;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	switch (params_width(params)) {
7278c2ecf20Sopenharmony_ci	case 24:
7288c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "24bit\n");
7298c2ecf20Sopenharmony_ci		fallthrough;
7308c2ecf20Sopenharmony_ci	case 32:
7318c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "24bit or 32bit\n");
7328c2ecf20Sopenharmony_ci		switch (sta350->format) {
7338c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_I2S:
7348c2ecf20Sopenharmony_ci			confb |= 0x0;
7358c2ecf20Sopenharmony_ci			break;
7368c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_LEFT_J:
7378c2ecf20Sopenharmony_ci			confb |= 0x1;
7388c2ecf20Sopenharmony_ci			break;
7398c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_RIGHT_J:
7408c2ecf20Sopenharmony_ci			confb |= 0x2;
7418c2ecf20Sopenharmony_ci			break;
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		break;
7458c2ecf20Sopenharmony_ci	case 20:
7468c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "20bit\n");
7478c2ecf20Sopenharmony_ci		switch (sta350->format) {
7488c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_I2S:
7498c2ecf20Sopenharmony_ci			confb |= 0x4;
7508c2ecf20Sopenharmony_ci			break;
7518c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_LEFT_J:
7528c2ecf20Sopenharmony_ci			confb |= 0x5;
7538c2ecf20Sopenharmony_ci			break;
7548c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_RIGHT_J:
7558c2ecf20Sopenharmony_ci			confb |= 0x6;
7568c2ecf20Sopenharmony_ci			break;
7578c2ecf20Sopenharmony_ci		}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci		break;
7608c2ecf20Sopenharmony_ci	case 18:
7618c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "18bit\n");
7628c2ecf20Sopenharmony_ci		switch (sta350->format) {
7638c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_I2S:
7648c2ecf20Sopenharmony_ci			confb |= 0x8;
7658c2ecf20Sopenharmony_ci			break;
7668c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_LEFT_J:
7678c2ecf20Sopenharmony_ci			confb |= 0x9;
7688c2ecf20Sopenharmony_ci			break;
7698c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_RIGHT_J:
7708c2ecf20Sopenharmony_ci			confb |= 0xa;
7718c2ecf20Sopenharmony_ci			break;
7728c2ecf20Sopenharmony_ci		}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci		break;
7758c2ecf20Sopenharmony_ci	case 16:
7768c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "16bit\n");
7778c2ecf20Sopenharmony_ci		switch (sta350->format) {
7788c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_I2S:
7798c2ecf20Sopenharmony_ci			confb |= 0x0;
7808c2ecf20Sopenharmony_ci			break;
7818c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_LEFT_J:
7828c2ecf20Sopenharmony_ci			confb |= 0xd;
7838c2ecf20Sopenharmony_ci			break;
7848c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_RIGHT_J:
7858c2ecf20Sopenharmony_ci			confb |= 0xe;
7868c2ecf20Sopenharmony_ci			break;
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci		break;
7908c2ecf20Sopenharmony_ci	default:
7918c2ecf20Sopenharmony_ci		return -EINVAL;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	ret = regmap_update_bits(sta350->regmap, STA350_CONFA,
7958c2ecf20Sopenharmony_ci				 STA350_CONFA_MCS_MASK | STA350_CONFA_IR_MASK,
7968c2ecf20Sopenharmony_ci				 confa);
7978c2ecf20Sopenharmony_ci	if (ret < 0)
7988c2ecf20Sopenharmony_ci		return ret;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	ret = regmap_update_bits(sta350->regmap, STA350_CONFB,
8018c2ecf20Sopenharmony_ci				 STA350_CONFB_SAI_MASK | STA350_CONFB_SAIFB,
8028c2ecf20Sopenharmony_ci				 confb);
8038c2ecf20Sopenharmony_ci	if (ret < 0)
8048c2ecf20Sopenharmony_ci		return ret;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	return 0;
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistatic int sta350_startup_sequence(struct sta350_priv *sta350)
8108c2ecf20Sopenharmony_ci{
8118c2ecf20Sopenharmony_ci	if (sta350->gpiod_power_down)
8128c2ecf20Sopenharmony_ci		gpiod_set_value(sta350->gpiod_power_down, 1);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	if (sta350->gpiod_nreset) {
8158c2ecf20Sopenharmony_ci		gpiod_set_value(sta350->gpiod_nreset, 0);
8168c2ecf20Sopenharmony_ci		mdelay(1);
8178c2ecf20Sopenharmony_ci		gpiod_set_value(sta350->gpiod_nreset, 1);
8188c2ecf20Sopenharmony_ci		mdelay(1);
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	return 0;
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci/**
8258c2ecf20Sopenharmony_ci * sta350_set_bias_level - DAPM callback
8268c2ecf20Sopenharmony_ci * @component: the component device
8278c2ecf20Sopenharmony_ci * @level: DAPM power level
8288c2ecf20Sopenharmony_ci *
8298c2ecf20Sopenharmony_ci * This is called by ALSA to put the component into low power mode
8308c2ecf20Sopenharmony_ci * or to wake it up.  If the component is powered off completely
8318c2ecf20Sopenharmony_ci * all registers must be restored after power on.
8328c2ecf20Sopenharmony_ci */
8338c2ecf20Sopenharmony_cistatic int sta350_set_bias_level(struct snd_soc_component *component,
8348c2ecf20Sopenharmony_ci				 enum snd_soc_bias_level level)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
8378c2ecf20Sopenharmony_ci	int ret;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "level = %d\n", level);
8408c2ecf20Sopenharmony_ci	switch (level) {
8418c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_ON:
8428c2ecf20Sopenharmony_ci		break;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_PREPARE:
8458c2ecf20Sopenharmony_ci		/* Full power on */
8468c2ecf20Sopenharmony_ci		regmap_update_bits(sta350->regmap, STA350_CONFF,
8478c2ecf20Sopenharmony_ci				   STA350_CONFF_PWDN | STA350_CONFF_EAPD,
8488c2ecf20Sopenharmony_ci				   STA350_CONFF_PWDN | STA350_CONFF_EAPD);
8498c2ecf20Sopenharmony_ci		break;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_STANDBY:
8528c2ecf20Sopenharmony_ci		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
8538c2ecf20Sopenharmony_ci			ret = regulator_bulk_enable(
8548c2ecf20Sopenharmony_ci				ARRAY_SIZE(sta350->supplies),
8558c2ecf20Sopenharmony_ci				sta350->supplies);
8568c2ecf20Sopenharmony_ci			if (ret < 0) {
8578c2ecf20Sopenharmony_ci				dev_err(component->dev,
8588c2ecf20Sopenharmony_ci					"Failed to enable supplies: %d\n",
8598c2ecf20Sopenharmony_ci					ret);
8608c2ecf20Sopenharmony_ci				return ret;
8618c2ecf20Sopenharmony_ci			}
8628c2ecf20Sopenharmony_ci			sta350_startup_sequence(sta350);
8638c2ecf20Sopenharmony_ci			sta350_cache_sync(component);
8648c2ecf20Sopenharmony_ci		}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci		/* Power down */
8678c2ecf20Sopenharmony_ci		regmap_update_bits(sta350->regmap, STA350_CONFF,
8688c2ecf20Sopenharmony_ci				   STA350_CONFF_PWDN | STA350_CONFF_EAPD,
8698c2ecf20Sopenharmony_ci				   0);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci		break;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_OFF:
8748c2ecf20Sopenharmony_ci		/* The chip runs through the power down sequence for us */
8758c2ecf20Sopenharmony_ci		regmap_update_bits(sta350->regmap, STA350_CONFF,
8768c2ecf20Sopenharmony_ci				   STA350_CONFF_PWDN | STA350_CONFF_EAPD, 0);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci		/* power down: low */
8798c2ecf20Sopenharmony_ci		if (sta350->gpiod_power_down)
8808c2ecf20Sopenharmony_ci			gpiod_set_value(sta350->gpiod_power_down, 0);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci		if (sta350->gpiod_nreset)
8838c2ecf20Sopenharmony_ci			gpiod_set_value(sta350->gpiod_nreset, 0);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci		regulator_bulk_disable(ARRAY_SIZE(sta350->supplies),
8868c2ecf20Sopenharmony_ci				       sta350->supplies);
8878c2ecf20Sopenharmony_ci		break;
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci	return 0;
8908c2ecf20Sopenharmony_ci}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops sta350_dai_ops = {
8938c2ecf20Sopenharmony_ci	.hw_params	= sta350_hw_params,
8948c2ecf20Sopenharmony_ci	.set_sysclk	= sta350_set_dai_sysclk,
8958c2ecf20Sopenharmony_ci	.set_fmt	= sta350_set_dai_fmt,
8968c2ecf20Sopenharmony_ci};
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver sta350_dai = {
8998c2ecf20Sopenharmony_ci	.name = "sta350-hifi",
9008c2ecf20Sopenharmony_ci	.playback = {
9018c2ecf20Sopenharmony_ci		.stream_name = "Playback",
9028c2ecf20Sopenharmony_ci		.channels_min = 2,
9038c2ecf20Sopenharmony_ci		.channels_max = 2,
9048c2ecf20Sopenharmony_ci		.rates = STA350_RATES,
9058c2ecf20Sopenharmony_ci		.formats = STA350_FORMATS,
9068c2ecf20Sopenharmony_ci	},
9078c2ecf20Sopenharmony_ci	.ops = &sta350_dai_ops,
9088c2ecf20Sopenharmony_ci};
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cistatic int sta350_probe(struct snd_soc_component *component)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
9138c2ecf20Sopenharmony_ci	struct sta350_platform_data *pdata = sta350->pdata;
9148c2ecf20Sopenharmony_ci	int i, ret = 0, thermal = 0;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(ARRAY_SIZE(sta350->supplies),
9178c2ecf20Sopenharmony_ci				    sta350->supplies);
9188c2ecf20Sopenharmony_ci	if (ret < 0) {
9198c2ecf20Sopenharmony_ci		dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
9208c2ecf20Sopenharmony_ci		return ret;
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	ret = sta350_startup_sequence(sta350);
9248c2ecf20Sopenharmony_ci	if (ret < 0) {
9258c2ecf20Sopenharmony_ci		dev_err(component->dev, "Failed to startup device\n");
9268c2ecf20Sopenharmony_ci		return ret;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* CONFA */
9308c2ecf20Sopenharmony_ci	if (!pdata->thermal_warning_recovery)
9318c2ecf20Sopenharmony_ci		thermal |= STA350_CONFA_TWAB;
9328c2ecf20Sopenharmony_ci	if (!pdata->thermal_warning_adjustment)
9338c2ecf20Sopenharmony_ci		thermal |= STA350_CONFA_TWRB;
9348c2ecf20Sopenharmony_ci	if (!pdata->fault_detect_recovery)
9358c2ecf20Sopenharmony_ci		thermal |= STA350_CONFA_FDRB;
9368c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFA,
9378c2ecf20Sopenharmony_ci			   STA350_CONFA_TWAB | STA350_CONFA_TWRB |
9388c2ecf20Sopenharmony_ci			   STA350_CONFA_FDRB,
9398c2ecf20Sopenharmony_ci			   thermal);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	/* CONFC */
9428c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFC,
9438c2ecf20Sopenharmony_ci			   STA350_CONFC_OM_MASK,
9448c2ecf20Sopenharmony_ci			   pdata->ffx_power_output_mode
9458c2ecf20Sopenharmony_ci				<< STA350_CONFC_OM_SHIFT);
9468c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFC,
9478c2ecf20Sopenharmony_ci			   STA350_CONFC_CSZ_MASK,
9488c2ecf20Sopenharmony_ci			   pdata->drop_compensation_ns
9498c2ecf20Sopenharmony_ci				<< STA350_CONFC_CSZ_SHIFT);
9508c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap,
9518c2ecf20Sopenharmony_ci			   STA350_CONFC,
9528c2ecf20Sopenharmony_ci			   STA350_CONFC_OCRB,
9538c2ecf20Sopenharmony_ci			   pdata->oc_warning_adjustment ?
9548c2ecf20Sopenharmony_ci				STA350_CONFC_OCRB : 0);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	/* CONFE */
9578c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFE,
9588c2ecf20Sopenharmony_ci			   STA350_CONFE_MPCV,
9598c2ecf20Sopenharmony_ci			   pdata->max_power_use_mpcc ?
9608c2ecf20Sopenharmony_ci				STA350_CONFE_MPCV : 0);
9618c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFE,
9628c2ecf20Sopenharmony_ci			   STA350_CONFE_MPC,
9638c2ecf20Sopenharmony_ci			   pdata->max_power_correction ?
9648c2ecf20Sopenharmony_ci				STA350_CONFE_MPC : 0);
9658c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFE,
9668c2ecf20Sopenharmony_ci			   STA350_CONFE_AME,
9678c2ecf20Sopenharmony_ci			   pdata->am_reduction_mode ?
9688c2ecf20Sopenharmony_ci				STA350_CONFE_AME : 0);
9698c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFE,
9708c2ecf20Sopenharmony_ci			   STA350_CONFE_PWMS,
9718c2ecf20Sopenharmony_ci			   pdata->odd_pwm_speed_mode ?
9728c2ecf20Sopenharmony_ci				STA350_CONFE_PWMS : 0);
9738c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFE,
9748c2ecf20Sopenharmony_ci			   STA350_CONFE_DCCV,
9758c2ecf20Sopenharmony_ci			   pdata->distortion_compensation ?
9768c2ecf20Sopenharmony_ci				STA350_CONFE_DCCV : 0);
9778c2ecf20Sopenharmony_ci	/*  CONFF */
9788c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFF,
9798c2ecf20Sopenharmony_ci			   STA350_CONFF_IDE,
9808c2ecf20Sopenharmony_ci			   pdata->invalid_input_detect_mute ?
9818c2ecf20Sopenharmony_ci				STA350_CONFF_IDE : 0);
9828c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_CONFF,
9838c2ecf20Sopenharmony_ci			   STA350_CONFF_OCFG_MASK,
9848c2ecf20Sopenharmony_ci			   pdata->output_conf
9858c2ecf20Sopenharmony_ci				<< STA350_CONFF_OCFG_SHIFT);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	/* channel to output mapping */
9888c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_C1CFG,
9898c2ecf20Sopenharmony_ci			   STA350_CxCFG_OM_MASK,
9908c2ecf20Sopenharmony_ci			   pdata->ch1_output_mapping
9918c2ecf20Sopenharmony_ci				<< STA350_CxCFG_OM_SHIFT);
9928c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_C2CFG,
9938c2ecf20Sopenharmony_ci			   STA350_CxCFG_OM_MASK,
9948c2ecf20Sopenharmony_ci			   pdata->ch2_output_mapping
9958c2ecf20Sopenharmony_ci				<< STA350_CxCFG_OM_SHIFT);
9968c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_C3CFG,
9978c2ecf20Sopenharmony_ci			   STA350_CxCFG_OM_MASK,
9988c2ecf20Sopenharmony_ci			   pdata->ch3_output_mapping
9998c2ecf20Sopenharmony_ci				<< STA350_CxCFG_OM_SHIFT);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	/* miscellaneous registers */
10028c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_MISC1,
10038c2ecf20Sopenharmony_ci			   STA350_MISC1_CPWMEN,
10048c2ecf20Sopenharmony_ci			   pdata->activate_mute_output ?
10058c2ecf20Sopenharmony_ci				STA350_MISC1_CPWMEN : 0);
10068c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_MISC1,
10078c2ecf20Sopenharmony_ci			   STA350_MISC1_BRIDGOFF,
10088c2ecf20Sopenharmony_ci			   pdata->bridge_immediate_off ?
10098c2ecf20Sopenharmony_ci				STA350_MISC1_BRIDGOFF : 0);
10108c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_MISC1,
10118c2ecf20Sopenharmony_ci			   STA350_MISC1_NSHHPEN,
10128c2ecf20Sopenharmony_ci			   pdata->noise_shape_dc_cut ?
10138c2ecf20Sopenharmony_ci				STA350_MISC1_NSHHPEN : 0);
10148c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_MISC1,
10158c2ecf20Sopenharmony_ci			   STA350_MISC1_RPDNEN,
10168c2ecf20Sopenharmony_ci			   pdata->powerdown_master_vol ?
10178c2ecf20Sopenharmony_ci				STA350_MISC1_RPDNEN: 0);
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	regmap_update_bits(sta350->regmap, STA350_MISC2,
10208c2ecf20Sopenharmony_ci			   STA350_MISC2_PNDLSL_MASK,
10218c2ecf20Sopenharmony_ci			   pdata->powerdown_delay_divider
10228c2ecf20Sopenharmony_ci				<< STA350_MISC2_PNDLSL_SHIFT);
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	/* initialize coefficient shadow RAM with reset values */
10258c2ecf20Sopenharmony_ci	for (i = 4; i <= 49; i += 5)
10268c2ecf20Sopenharmony_ci		sta350->coef_shadow[i] = 0x400000;
10278c2ecf20Sopenharmony_ci	for (i = 50; i <= 54; i++)
10288c2ecf20Sopenharmony_ci		sta350->coef_shadow[i] = 0x7fffff;
10298c2ecf20Sopenharmony_ci	sta350->coef_shadow[55] = 0x5a9df7;
10308c2ecf20Sopenharmony_ci	sta350->coef_shadow[56] = 0x7fffff;
10318c2ecf20Sopenharmony_ci	sta350->coef_shadow[59] = 0x7fffff;
10328c2ecf20Sopenharmony_ci	sta350->coef_shadow[60] = 0x400000;
10338c2ecf20Sopenharmony_ci	sta350->coef_shadow[61] = 0x400000;
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
10368c2ecf20Sopenharmony_ci	/* Bias level configuration will have done an extra enable */
10378c2ecf20Sopenharmony_ci	regulator_bulk_disable(ARRAY_SIZE(sta350->supplies), sta350->supplies);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	return 0;
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_cistatic void sta350_remove(struct snd_soc_component *component)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	struct sta350_priv *sta350 = snd_soc_component_get_drvdata(component);
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	regulator_bulk_disable(ARRAY_SIZE(sta350->supplies), sta350->supplies);
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver sta350_component = {
10508c2ecf20Sopenharmony_ci	.probe			= sta350_probe,
10518c2ecf20Sopenharmony_ci	.remove			= sta350_remove,
10528c2ecf20Sopenharmony_ci	.set_bias_level		= sta350_set_bias_level,
10538c2ecf20Sopenharmony_ci	.controls		= sta350_snd_controls,
10548c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(sta350_snd_controls),
10558c2ecf20Sopenharmony_ci	.dapm_widgets		= sta350_dapm_widgets,
10568c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(sta350_dapm_widgets),
10578c2ecf20Sopenharmony_ci	.dapm_routes		= sta350_dapm_routes,
10588c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(sta350_dapm_routes),
10598c2ecf20Sopenharmony_ci	.suspend_bias_off	= 1,
10608c2ecf20Sopenharmony_ci	.idle_bias_on		= 1,
10618c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
10628c2ecf20Sopenharmony_ci	.endianness		= 1,
10638c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
10648c2ecf20Sopenharmony_ci};
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_cistatic const struct regmap_config sta350_regmap = {
10678c2ecf20Sopenharmony_ci	.reg_bits =		8,
10688c2ecf20Sopenharmony_ci	.val_bits =		8,
10698c2ecf20Sopenharmony_ci	.max_register =		STA350_MISC2,
10708c2ecf20Sopenharmony_ci	.reg_defaults =		sta350_regs,
10718c2ecf20Sopenharmony_ci	.num_reg_defaults =	ARRAY_SIZE(sta350_regs),
10728c2ecf20Sopenharmony_ci	.cache_type =		REGCACHE_RBTREE,
10738c2ecf20Sopenharmony_ci	.wr_table =		&sta350_write_regs,
10748c2ecf20Sopenharmony_ci	.rd_table =		&sta350_read_regs,
10758c2ecf20Sopenharmony_ci	.volatile_table =	&sta350_volatile_regs,
10768c2ecf20Sopenharmony_ci};
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
10798c2ecf20Sopenharmony_cistatic const struct of_device_id st350_dt_ids[] = {
10808c2ecf20Sopenharmony_ci	{ .compatible = "st,sta350", },
10818c2ecf20Sopenharmony_ci	{ }
10828c2ecf20Sopenharmony_ci};
10838c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, st350_dt_ids);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_cistatic const char * const sta350_ffx_modes[] = {
10868c2ecf20Sopenharmony_ci	[STA350_FFX_PM_DROP_COMP]		= "drop-compensation",
10878c2ecf20Sopenharmony_ci	[STA350_FFX_PM_TAPERED_COMP]		= "tapered-compensation",
10888c2ecf20Sopenharmony_ci	[STA350_FFX_PM_FULL_POWER]		= "full-power-mode",
10898c2ecf20Sopenharmony_ci	[STA350_FFX_PM_VARIABLE_DROP_COMP]	= "variable-drop-compensation",
10908c2ecf20Sopenharmony_ci};
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_cistatic int sta350_probe_dt(struct device *dev, struct sta350_priv *sta350)
10938c2ecf20Sopenharmony_ci{
10948c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
10958c2ecf20Sopenharmony_ci	struct sta350_platform_data *pdata;
10968c2ecf20Sopenharmony_ci	const char *ffx_power_mode;
10978c2ecf20Sopenharmony_ci	u16 tmp;
10988c2ecf20Sopenharmony_ci	u8 tmp8;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
11018c2ecf20Sopenharmony_ci	if (!pdata)
11028c2ecf20Sopenharmony_ci		return -ENOMEM;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	of_property_read_u8(np, "st,output-conf",
11058c2ecf20Sopenharmony_ci			    &pdata->output_conf);
11068c2ecf20Sopenharmony_ci	of_property_read_u8(np, "st,ch1-output-mapping",
11078c2ecf20Sopenharmony_ci			    &pdata->ch1_output_mapping);
11088c2ecf20Sopenharmony_ci	of_property_read_u8(np, "st,ch2-output-mapping",
11098c2ecf20Sopenharmony_ci			    &pdata->ch2_output_mapping);
11108c2ecf20Sopenharmony_ci	of_property_read_u8(np, "st,ch3-output-mapping",
11118c2ecf20Sopenharmony_ci			    &pdata->ch3_output_mapping);
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,thermal-warning-recovery", NULL))
11148c2ecf20Sopenharmony_ci		pdata->thermal_warning_recovery = 1;
11158c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,thermal-warning-adjustment", NULL))
11168c2ecf20Sopenharmony_ci		pdata->thermal_warning_adjustment = 1;
11178c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,fault-detect-recovery", NULL))
11188c2ecf20Sopenharmony_ci		pdata->fault_detect_recovery = 1;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	pdata->ffx_power_output_mode = STA350_FFX_PM_VARIABLE_DROP_COMP;
11218c2ecf20Sopenharmony_ci	if (!of_property_read_string(np, "st,ffx-power-output-mode",
11228c2ecf20Sopenharmony_ci				     &ffx_power_mode)) {
11238c2ecf20Sopenharmony_ci		int i, mode = -EINVAL;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(sta350_ffx_modes); i++)
11268c2ecf20Sopenharmony_ci			if (!strcasecmp(ffx_power_mode, sta350_ffx_modes[i]))
11278c2ecf20Sopenharmony_ci				mode = i;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci		if (mode < 0)
11308c2ecf20Sopenharmony_ci			dev_warn(dev, "Unsupported ffx output mode: %s\n",
11318c2ecf20Sopenharmony_ci				 ffx_power_mode);
11328c2ecf20Sopenharmony_ci		else
11338c2ecf20Sopenharmony_ci			pdata->ffx_power_output_mode = mode;
11348c2ecf20Sopenharmony_ci	}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	tmp = 140;
11378c2ecf20Sopenharmony_ci	of_property_read_u16(np, "st,drop-compensation-ns", &tmp);
11388c2ecf20Sopenharmony_ci	pdata->drop_compensation_ns = clamp_t(u16, tmp, 0, 300) / 20;
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,overcurrent-warning-adjustment", NULL))
11418c2ecf20Sopenharmony_ci		pdata->oc_warning_adjustment = 1;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	/* CONFE */
11448c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,max-power-use-mpcc", NULL))
11458c2ecf20Sopenharmony_ci		pdata->max_power_use_mpcc = 1;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,max-power-correction", NULL))
11488c2ecf20Sopenharmony_ci		pdata->max_power_correction = 1;
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,am-reduction-mode", NULL))
11518c2ecf20Sopenharmony_ci		pdata->am_reduction_mode = 1;
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,odd-pwm-speed-mode", NULL))
11548c2ecf20Sopenharmony_ci		pdata->odd_pwm_speed_mode = 1;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,distortion-compensation", NULL))
11578c2ecf20Sopenharmony_ci		pdata->distortion_compensation = 1;
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	/* CONFF */
11608c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,invalid-input-detect-mute", NULL))
11618c2ecf20Sopenharmony_ci		pdata->invalid_input_detect_mute = 1;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	/* MISC */
11648c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,activate-mute-output", NULL))
11658c2ecf20Sopenharmony_ci		pdata->activate_mute_output = 1;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,bridge-immediate-off", NULL))
11688c2ecf20Sopenharmony_ci		pdata->bridge_immediate_off = 1;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,noise-shape-dc-cut", NULL))
11718c2ecf20Sopenharmony_ci		pdata->noise_shape_dc_cut = 1;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	if (of_get_property(np, "st,powerdown-master-volume", NULL))
11748c2ecf20Sopenharmony_ci		pdata->powerdown_master_vol = 1;
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	if (!of_property_read_u8(np, "st,powerdown-delay-divider", &tmp8)) {
11778c2ecf20Sopenharmony_ci		if (is_power_of_2(tmp8) && tmp8 >= 1 && tmp8 <= 128)
11788c2ecf20Sopenharmony_ci			pdata->powerdown_delay_divider = ilog2(tmp8);
11798c2ecf20Sopenharmony_ci		else
11808c2ecf20Sopenharmony_ci			dev_warn(dev, "Unsupported powerdown delay divider %d\n",
11818c2ecf20Sopenharmony_ci				 tmp8);
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	sta350->pdata = pdata;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	return 0;
11878c2ecf20Sopenharmony_ci}
11888c2ecf20Sopenharmony_ci#endif
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic int sta350_i2c_probe(struct i2c_client *i2c,
11918c2ecf20Sopenharmony_ci			    const struct i2c_device_id *id)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	struct device *dev = &i2c->dev;
11948c2ecf20Sopenharmony_ci	struct sta350_priv *sta350;
11958c2ecf20Sopenharmony_ci	int ret, i;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	sta350 = devm_kzalloc(dev, sizeof(struct sta350_priv), GFP_KERNEL);
11988c2ecf20Sopenharmony_ci	if (!sta350)
11998c2ecf20Sopenharmony_ci		return -ENOMEM;
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	mutex_init(&sta350->coeff_lock);
12028c2ecf20Sopenharmony_ci	sta350->pdata = dev_get_platdata(dev);
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
12058c2ecf20Sopenharmony_ci	if (dev->of_node) {
12068c2ecf20Sopenharmony_ci		ret = sta350_probe_dt(dev, sta350);
12078c2ecf20Sopenharmony_ci		if (ret < 0)
12088c2ecf20Sopenharmony_ci			return ret;
12098c2ecf20Sopenharmony_ci	}
12108c2ecf20Sopenharmony_ci#endif
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	/* GPIOs */
12138c2ecf20Sopenharmony_ci	sta350->gpiod_nreset = devm_gpiod_get_optional(dev, "reset",
12148c2ecf20Sopenharmony_ci						       GPIOD_OUT_LOW);
12158c2ecf20Sopenharmony_ci	if (IS_ERR(sta350->gpiod_nreset))
12168c2ecf20Sopenharmony_ci		return PTR_ERR(sta350->gpiod_nreset);
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	sta350->gpiod_power_down = devm_gpiod_get_optional(dev, "power-down",
12198c2ecf20Sopenharmony_ci							   GPIOD_OUT_LOW);
12208c2ecf20Sopenharmony_ci	if (IS_ERR(sta350->gpiod_power_down))
12218c2ecf20Sopenharmony_ci		return PTR_ERR(sta350->gpiod_power_down);
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	/* regulators */
12248c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sta350->supplies); i++)
12258c2ecf20Sopenharmony_ci		sta350->supplies[i].supply = sta350_supply_names[i];
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sta350->supplies),
12288c2ecf20Sopenharmony_ci				      sta350->supplies);
12298c2ecf20Sopenharmony_ci	if (ret < 0) {
12308c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to request supplies: %d\n", ret);
12318c2ecf20Sopenharmony_ci		return ret;
12328c2ecf20Sopenharmony_ci	}
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	sta350->regmap = devm_regmap_init_i2c(i2c, &sta350_regmap);
12358c2ecf20Sopenharmony_ci	if (IS_ERR(sta350->regmap)) {
12368c2ecf20Sopenharmony_ci		ret = PTR_ERR(sta350->regmap);
12378c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to init regmap: %d\n", ret);
12388c2ecf20Sopenharmony_ci		return ret;
12398c2ecf20Sopenharmony_ci	}
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, sta350);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	ret = devm_snd_soc_register_component(dev, &sta350_component, &sta350_dai, 1);
12448c2ecf20Sopenharmony_ci	if (ret < 0)
12458c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to register component (%d)\n", ret);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	return ret;
12488c2ecf20Sopenharmony_ci}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_cistatic int sta350_i2c_remove(struct i2c_client *client)
12518c2ecf20Sopenharmony_ci{
12528c2ecf20Sopenharmony_ci	return 0;
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_cistatic const struct i2c_device_id sta350_i2c_id[] = {
12568c2ecf20Sopenharmony_ci	{ "sta350", 0 },
12578c2ecf20Sopenharmony_ci	{ }
12588c2ecf20Sopenharmony_ci};
12598c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sta350_i2c_id);
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_cistatic struct i2c_driver sta350_i2c_driver = {
12628c2ecf20Sopenharmony_ci	.driver = {
12638c2ecf20Sopenharmony_ci		.name = "sta350",
12648c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(st350_dt_ids),
12658c2ecf20Sopenharmony_ci	},
12668c2ecf20Sopenharmony_ci	.probe =    sta350_i2c_probe,
12678c2ecf20Sopenharmony_ci	.remove =   sta350_i2c_remove,
12688c2ecf20Sopenharmony_ci	.id_table = sta350_i2c_id,
12698c2ecf20Sopenharmony_ci};
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_cimodule_i2c_driver(sta350_i2c_driver);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC STA350 driver");
12748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sven Brandau <info@brandau.biz>");
12758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1276