18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci// cs4234.c -- ALSA SoC CS4234 driver
38c2ecf20Sopenharmony_ci//
48c2ecf20Sopenharmony_ci// Copyright (C) 2020 Cirrus Logic, Inc. and
58c2ecf20Sopenharmony_ci//                    Cirrus Logic International Semiconductor Ltd.
68c2ecf20Sopenharmony_ci//
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/completion.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
148c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <sound/pcm.h>
178c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
188c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
198c2ecf20Sopenharmony_ci#include <linux/regmap.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <sound/soc.h>
238c2ecf20Sopenharmony_ci#include <sound/tlv.h>
248c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "cs4234.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct cs4234 {
298c2ecf20Sopenharmony_ci	struct device *dev;
308c2ecf20Sopenharmony_ci	struct regmap *regmap;
318c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
328c2ecf20Sopenharmony_ci	struct regulator_bulk_data core_supplies[2];
338c2ecf20Sopenharmony_ci	int num_core_supplies;
348c2ecf20Sopenharmony_ci	struct completion vq_ramp_complete;
358c2ecf20Sopenharmony_ci	struct delayed_work vq_ramp_delay;
368c2ecf20Sopenharmony_ci	struct clk *mclk;
378c2ecf20Sopenharmony_ci	unsigned long mclk_rate;
388c2ecf20Sopenharmony_ci	unsigned long lrclk_rate;
398c2ecf20Sopenharmony_ci	unsigned int format;
408c2ecf20Sopenharmony_ci	struct snd_ratnum rate_dividers[2];
418c2ecf20Sopenharmony_ci	struct snd_pcm_hw_constraint_ratnums rate_constraint;
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* -89.92dB to +6.02dB with step of 0.38dB */
458c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(dac_tlv, -8992, 38, 0);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const char * const cs4234_dac14_delay_text[] = {
488c2ecf20Sopenharmony_ci	  "0us", "100us", "150us", "200us", "225us", "250us", "275us", "300us",
498c2ecf20Sopenharmony_ci	"325us", "350us", "375us", "400us", "425us", "450us", "475us", "500us",
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_dac14_group_delay, CS4234_TPS_CTRL,
528c2ecf20Sopenharmony_ci			    CS4234_GRP_DELAY_SHIFT, cs4234_dac14_delay_text);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const char * const cs4234_noise_gate_text[] = {
558c2ecf20Sopenharmony_ci	"72dB",  "78dB",  "84dB", "90dB", "96dB", "102dB", "138dB", "Disabled",
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_ll_noise_gate, CS4234_LOW_LAT_CTRL1,
588c2ecf20Sopenharmony_ci			    CS4234_LL_NG_SHIFT, cs4234_noise_gate_text);
598c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_dac14_noise_gate, CS4234_DAC_CTRL1,
608c2ecf20Sopenharmony_ci			    CS4234_DAC14_NG_SHIFT, cs4234_noise_gate_text);
618c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_dac5_noise_gate, CS4234_DAC_CTRL2,
628c2ecf20Sopenharmony_ci			    CS4234_DAC5_NG_SHIFT, cs4234_noise_gate_text);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const char * const cs4234_dac5_config_fltr_sel_text[] = {
658c2ecf20Sopenharmony_ci	"Interpolation Filter", "Sample and Hold"
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_dac5_config_fltr_sel, CS4234_DAC_CTRL1,
688c2ecf20Sopenharmony_ci			    CS4234_DAC5_CFG_FLTR_SHIFT,
698c2ecf20Sopenharmony_ci			    cs4234_dac5_config_fltr_sel_text);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const char * const cs4234_mute_delay_text[] = {
728c2ecf20Sopenharmony_ci	"1x",  "4x",  "16x", "64x",
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_mute_delay, CS4234_VOLUME_MODE,
758c2ecf20Sopenharmony_ci			    CS4234_MUTE_DELAY_SHIFT, cs4234_mute_delay_text);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic const char * const cs4234_minmax_delay_text[] = {
788c2ecf20Sopenharmony_ci	"1x",  "2x",  "4x", "8x", "16x",  "32x", "64x", "128x",
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_min_delay, CS4234_VOLUME_MODE,
818c2ecf20Sopenharmony_ci			    CS4234_MIN_DELAY_SHIFT, cs4234_minmax_delay_text);
828c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(cs4234_max_delay, CS4234_VOLUME_MODE,
838c2ecf20Sopenharmony_ci			    CS4234_MAX_DELAY_SHIFT, cs4234_minmax_delay_text);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic int cs4234_dac14_grp_delay_put(struct snd_kcontrol *kctrl,
868c2ecf20Sopenharmony_ci				      struct snd_ctl_elem_value *uctrl)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_kcontrol_component(kctrl);
898c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(component);
908c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
918c2ecf20Sopenharmony_ci	unsigned int val = 0;
928c2ecf20Sopenharmony_ci	int ret = 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_lock(dapm);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	regmap_read(cs4234->regmap, CS4234_ADC_CTRL2, &val);
978c2ecf20Sopenharmony_ci	if ((val & 0x0F) != 0x0F) { // are all the ADCs powerdown
988c2ecf20Sopenharmony_ci		ret = -EBUSY;
998c2ecf20Sopenharmony_ci		dev_err(component->dev, "Can't change group delay while ADC are ON\n");
1008c2ecf20Sopenharmony_ci		goto exit;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	regmap_read(cs4234->regmap, CS4234_DAC_CTRL4, &val);
1048c2ecf20Sopenharmony_ci	if ((val & 0x1F) != 0x1F) { // are all the DACs powerdown
1058c2ecf20Sopenharmony_ci		ret = -EBUSY;
1068c2ecf20Sopenharmony_ci		dev_err(component->dev, "Can't change group delay while DAC are ON\n");
1078c2ecf20Sopenharmony_ci		goto exit;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	ret = snd_soc_put_enum_double(kctrl, uctrl);
1118c2ecf20Sopenharmony_ciexit:
1128c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_unlock(dapm);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return ret;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void cs4234_vq_ramp_done(struct work_struct *work)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct delayed_work *dw = to_delayed_work(work);
1208c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = container_of(dw, struct cs4234, vq_ramp_delay);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	complete_all(&cs4234->vq_ramp_complete);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int cs4234_set_bias_level(struct snd_soc_component *component,
1268c2ecf20Sopenharmony_ci				 enum snd_soc_bias_level level)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(component);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	switch (level) {
1318c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_PREPARE:
1328c2ecf20Sopenharmony_ci		switch (snd_soc_component_get_bias_level(component)) {
1338c2ecf20Sopenharmony_ci		case SND_SOC_BIAS_STANDBY:
1348c2ecf20Sopenharmony_ci			wait_for_completion(&cs4234->vq_ramp_complete);
1358c2ecf20Sopenharmony_ci			break;
1368c2ecf20Sopenharmony_ci		default:
1378c2ecf20Sopenharmony_ci			break;
1388c2ecf20Sopenharmony_ci		}
1398c2ecf20Sopenharmony_ci		break;
1408c2ecf20Sopenharmony_ci	default:
1418c2ecf20Sopenharmony_ci		break;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget cs4234_dapm_widgets[] = {
1488c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("SDRX1", NULL,  0, SND_SOC_NOPM, 0, 0),
1498c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("SDRX2", NULL,  1, SND_SOC_NOPM, 0, 0),
1508c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("SDRX3", NULL,  2, SND_SOC_NOPM, 0, 0),
1518c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("SDRX4", NULL,  3, SND_SOC_NOPM, 0, 0),
1528c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("SDRX5", NULL,  4, SND_SOC_NOPM, 0, 0),
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC1", NULL, CS4234_DAC_CTRL4, CS4234_PDN_DAC1_SHIFT, 1),
1558c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC2", NULL, CS4234_DAC_CTRL4, CS4234_PDN_DAC2_SHIFT, 1),
1568c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC3", NULL, CS4234_DAC_CTRL4, CS4234_PDN_DAC3_SHIFT, 1),
1578c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC4", NULL, CS4234_DAC_CTRL4, CS4234_PDN_DAC4_SHIFT, 1),
1588c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC5", NULL, CS4234_DAC_CTRL4, CS4234_PDN_DAC5_SHIFT, 1),
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("AOUT1"),
1618c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("AOUT2"),
1628c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("AOUT3"),
1638c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("AOUT4"),
1648c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("AOUT5"),
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("AIN1"),
1678c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("AIN2"),
1688c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("AIN3"),
1698c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("AIN4"),
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC1", NULL, CS4234_ADC_CTRL2, CS4234_PDN_ADC1_SHIFT, 1),
1728c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC2", NULL, CS4234_ADC_CTRL2, CS4234_PDN_ADC2_SHIFT, 1),
1738c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC3", NULL, CS4234_ADC_CTRL2, CS4234_PDN_ADC3_SHIFT, 1),
1748c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC4", NULL, CS4234_ADC_CTRL2, CS4234_PDN_ADC4_SHIFT, 1),
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("SDTX1", NULL, 0, SND_SOC_NOPM, 0, 1),
1778c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("SDTX2", NULL, 1, SND_SOC_NOPM, 0, 1),
1788c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("SDTX3", NULL, 2, SND_SOC_NOPM, 0, 1),
1798c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("SDTX4", NULL, 3, SND_SOC_NOPM, 0, 1),
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route cs4234_dapm_routes[] = {
1838c2ecf20Sopenharmony_ci	/* Playback */
1848c2ecf20Sopenharmony_ci	{ "AOUT1", NULL, "DAC1" },
1858c2ecf20Sopenharmony_ci	{ "AOUT2", NULL, "DAC2" },
1868c2ecf20Sopenharmony_ci	{ "AOUT3", NULL, "DAC3" },
1878c2ecf20Sopenharmony_ci	{ "AOUT4", NULL, "DAC4" },
1888c2ecf20Sopenharmony_ci	{ "AOUT5", NULL, "DAC5" },
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	{ "DAC1", NULL, "SDRX1" },
1918c2ecf20Sopenharmony_ci	{ "DAC2", NULL, "SDRX2" },
1928c2ecf20Sopenharmony_ci	{ "DAC3", NULL, "SDRX3" },
1938c2ecf20Sopenharmony_ci	{ "DAC4", NULL, "SDRX4" },
1948c2ecf20Sopenharmony_ci	{ "DAC5", NULL, "SDRX5" },
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	{ "SDRX1", NULL, "Playback" },
1978c2ecf20Sopenharmony_ci	{ "SDRX2", NULL, "Playback" },
1988c2ecf20Sopenharmony_ci	{ "SDRX3", NULL, "Playback" },
1998c2ecf20Sopenharmony_ci	{ "SDRX4", NULL, "Playback" },
2008c2ecf20Sopenharmony_ci	{ "SDRX5", NULL, "Playback" },
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/* Capture */
2038c2ecf20Sopenharmony_ci	{ "ADC1", NULL, "AIN1" },
2048c2ecf20Sopenharmony_ci	{ "ADC2", NULL, "AIN2" },
2058c2ecf20Sopenharmony_ci	{ "ADC3", NULL, "AIN3" },
2068c2ecf20Sopenharmony_ci	{ "ADC4", NULL, "AIN4" },
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	{ "SDTX1", NULL, "ADC1" },
2098c2ecf20Sopenharmony_ci	{ "SDTX2", NULL, "ADC2" },
2108c2ecf20Sopenharmony_ci	{ "SDTX3", NULL, "ADC3" },
2118c2ecf20Sopenharmony_ci	{ "SDTX4", NULL, "ADC4" },
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	{ "Capture", NULL, "SDTX1" },
2148c2ecf20Sopenharmony_ci	{ "Capture", NULL, "SDTX2" },
2158c2ecf20Sopenharmony_ci	{ "Capture", NULL, "SDTX3" },
2168c2ecf20Sopenharmony_ci	{ "Capture", NULL, "SDTX4" },
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new cs4234_snd_controls[] = {
2208c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("Master Volume", CS4234_MASTER_VOL, 0, 0xff, 1, dac_tlv),
2218c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("DAC1 Volume", CS4234_DAC1_VOL, 0, 0xff, 1, dac_tlv),
2228c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("DAC2 Volume", CS4234_DAC2_VOL, 0, 0xff, 1, dac_tlv),
2238c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("DAC3 Volume", CS4234_DAC3_VOL, 0, 0xff, 1, dac_tlv),
2248c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("DAC4 Volume", CS4234_DAC4_VOL, 0, 0xff, 1, dac_tlv),
2258c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("DAC5 Volume", CS4234_DAC5_VOL, 0, 0xff, 1, dac_tlv),
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC5 Soft Ramp Switch", CS4234_DAC_CTRL3, CS4234_DAC5_ATT_SHIFT, 1, 1),
2288c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC1-4 Soft Ramp Switch", CS4234_DAC_CTRL3, CS4234_DAC14_ATT_SHIFT, 1, 1),
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC HPF Switch", CS4234_ADC_CTRL1, CS4234_ENA_HPF_SHIFT, 1, 0),
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	SOC_ENUM_EXT("DAC1-4 Group Delay", cs4234_dac14_group_delay,
2338c2ecf20Sopenharmony_ci		     snd_soc_get_enum_double, cs4234_dac14_grp_delay_put),
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC1 Invert Switch", CS4234_ADC_CTRL1, CS4234_INV_ADC1_SHIFT, 1, 0),
2368c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC2 Invert Switch", CS4234_ADC_CTRL1, CS4234_INV_ADC2_SHIFT, 1, 0),
2378c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC3 Invert Switch", CS4234_ADC_CTRL1, CS4234_INV_ADC3_SHIFT, 1, 0),
2388c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC4 Invert Switch", CS4234_ADC_CTRL1, CS4234_INV_ADC4_SHIFT, 1, 0),
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC1 Invert Switch", CS4234_DAC_CTRL2, CS4234_INV_DAC1_SHIFT, 1, 0),
2418c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC2 Invert Switch", CS4234_DAC_CTRL2, CS4234_INV_DAC2_SHIFT, 1, 0),
2428c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC3 Invert Switch", CS4234_DAC_CTRL2, CS4234_INV_DAC3_SHIFT, 1, 0),
2438c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC4 Invert Switch", CS4234_DAC_CTRL2, CS4234_INV_DAC4_SHIFT, 1, 0),
2448c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC5 Invert Switch", CS4234_DAC_CTRL2, CS4234_INV_DAC5_SHIFT, 1, 0),
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC1 Switch", CS4234_ADC_CTRL2, CS4234_MUTE_ADC1_SHIFT, 1, 1),
2478c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC2 Switch", CS4234_ADC_CTRL2, CS4234_MUTE_ADC2_SHIFT, 1, 1),
2488c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC3 Switch", CS4234_ADC_CTRL2, CS4234_MUTE_ADC3_SHIFT, 1, 1),
2498c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC4 Switch", CS4234_ADC_CTRL2, CS4234_MUTE_ADC4_SHIFT, 1, 1),
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC1 Switch", CS4234_DAC_CTRL3, CS4234_MUTE_DAC1_SHIFT, 1, 1),
2528c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC2 Switch", CS4234_DAC_CTRL3, CS4234_MUTE_DAC2_SHIFT, 1, 1),
2538c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC3 Switch", CS4234_DAC_CTRL3, CS4234_MUTE_DAC3_SHIFT, 1, 1),
2548c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC4 Switch", CS4234_DAC_CTRL3, CS4234_MUTE_DAC4_SHIFT, 1, 1),
2558c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC5 Switch", CS4234_DAC_CTRL3, CS4234_MUTE_DAC5_SHIFT, 1, 1),
2568c2ecf20Sopenharmony_ci	SOC_SINGLE("Low-latency Switch", CS4234_DAC_CTRL3, CS4234_MUTE_LL_SHIFT, 1, 1),
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC1 Low-latency Invert Switch", CS4234_LOW_LAT_CTRL1,
2598c2ecf20Sopenharmony_ci		   CS4234_INV_LL1_SHIFT, 1, 0),
2608c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC2 Low-latency Invert Switch", CS4234_LOW_LAT_CTRL1,
2618c2ecf20Sopenharmony_ci		   CS4234_INV_LL2_SHIFT, 1, 0),
2628c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC3 Low-latency Invert Switch", CS4234_LOW_LAT_CTRL1,
2638c2ecf20Sopenharmony_ci		   CS4234_INV_LL3_SHIFT, 1, 0),
2648c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC4 Low-latency Invert Switch", CS4234_LOW_LAT_CTRL1,
2658c2ecf20Sopenharmony_ci		   CS4234_INV_LL4_SHIFT, 1, 0),
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	SOC_ENUM("Low-latency Noise Gate", cs4234_ll_noise_gate),
2688c2ecf20Sopenharmony_ci	SOC_ENUM("DAC1-4 Noise Gate", cs4234_dac14_noise_gate),
2698c2ecf20Sopenharmony_ci	SOC_ENUM("DAC5 Noise Gate", cs4234_dac5_noise_gate),
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC1-4 De-emphasis Switch", CS4234_DAC_CTRL1,
2728c2ecf20Sopenharmony_ci		   CS4234_DAC14_DE_SHIFT, 1, 0),
2738c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC5 De-emphasis Switch", CS4234_DAC_CTRL1,
2748c2ecf20Sopenharmony_ci		   CS4234_DAC5_DE_SHIFT, 1, 0),
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	SOC_SINGLE("DAC5 Master Controlled Switch", CS4234_DAC_CTRL1,
2778c2ecf20Sopenharmony_ci		   CS4234_DAC5_MVC_SHIFT, 1, 0),
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	SOC_ENUM("DAC5 Filter", cs4234_dac5_config_fltr_sel),
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	SOC_ENUM("Mute Delay", cs4234_mute_delay),
2828c2ecf20Sopenharmony_ci	SOC_ENUM("Ramp Minimum Delay", cs4234_min_delay),
2838c2ecf20Sopenharmony_ci	SOC_ENUM("Ramp Maximum Delay", cs4234_max_delay),
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int cs4234_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int format)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
2908c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(component);
2918c2ecf20Sopenharmony_ci	unsigned int sp_ctrl = 0;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	cs4234->format = format & SND_SOC_DAIFMT_FORMAT_MASK;
2948c2ecf20Sopenharmony_ci	switch (cs4234->format) {
2958c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
2968c2ecf20Sopenharmony_ci		sp_ctrl |= CS4234_LEFT_J << CS4234_SP_FORMAT_SHIFT;
2978c2ecf20Sopenharmony_ci		break;
2988c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
2998c2ecf20Sopenharmony_ci		sp_ctrl |= CS4234_I2S << CS4234_SP_FORMAT_SHIFT;
3008c2ecf20Sopenharmony_ci		break;
3018c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A: /* TDM mode in datasheet */
3028c2ecf20Sopenharmony_ci		sp_ctrl |= CS4234_TDM << CS4234_SP_FORMAT_SHIFT;
3038c2ecf20Sopenharmony_ci		break;
3048c2ecf20Sopenharmony_ci	default:
3058c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported dai format\n");
3068c2ecf20Sopenharmony_ci		return -EINVAL;
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
3108c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
3118c2ecf20Sopenharmony_ci		break;
3128c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
3138c2ecf20Sopenharmony_ci		if (cs4234->format == SND_SOC_DAIFMT_DSP_A) {
3148c2ecf20Sopenharmony_ci			dev_err(component->dev, "Unsupported DSP A format in master mode\n");
3158c2ecf20Sopenharmony_ci			return -EINVAL;
3168c2ecf20Sopenharmony_ci		}
3178c2ecf20Sopenharmony_ci		sp_ctrl |= CS4234_MST_SLV_MASK;
3188c2ecf20Sopenharmony_ci		break;
3198c2ecf20Sopenharmony_ci	default:
3208c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported master/slave mode\n");
3218c2ecf20Sopenharmony_ci		return -EINVAL;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	switch (format & SND_SOC_DAIFMT_INV_MASK) {
3258c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
3268c2ecf20Sopenharmony_ci		break;
3278c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
3288c2ecf20Sopenharmony_ci		sp_ctrl |= CS4234_INVT_SCLK_MASK;
3298c2ecf20Sopenharmony_ci		break;
3308c2ecf20Sopenharmony_ci	default:
3318c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported inverted clock setting\n");
3328c2ecf20Sopenharmony_ci		return -EINVAL;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_SP_CTRL,
3368c2ecf20Sopenharmony_ci			   CS4234_SP_FORMAT_MASK | CS4234_MST_SLV_MASK | CS4234_INVT_SCLK_MASK,
3378c2ecf20Sopenharmony_ci			   sp_ctrl);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	return 0;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic int cs4234_dai_hw_params(struct snd_pcm_substream *sub,
3438c2ecf20Sopenharmony_ci				struct snd_pcm_hw_params *params,
3448c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3478c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(component);
3488c2ecf20Sopenharmony_ci	unsigned int mclk_mult, double_speed = 0;
3498c2ecf20Sopenharmony_ci	int ret = 0, rate_ad, sample_width;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	cs4234->lrclk_rate = params_rate(params);
3528c2ecf20Sopenharmony_ci	mclk_mult = cs4234->mclk_rate / cs4234->lrclk_rate;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (cs4234->lrclk_rate > 48000) {
3558c2ecf20Sopenharmony_ci		double_speed = 1;
3568c2ecf20Sopenharmony_ci		mclk_mult *= 2;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	switch (mclk_mult) {
3608c2ecf20Sopenharmony_ci	case 256:
3618c2ecf20Sopenharmony_ci	case 384:
3628c2ecf20Sopenharmony_ci	case 512:
3638c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_CLOCK_SP,
3648c2ecf20Sopenharmony_ci				   CS4234_SPEED_MODE_MASK,
3658c2ecf20Sopenharmony_ci				   double_speed << CS4234_SPEED_MODE_SHIFT);
3668c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_CLOCK_SP,
3678c2ecf20Sopenharmony_ci				   CS4234_MCLK_RATE_MASK,
3688c2ecf20Sopenharmony_ci				   ((mclk_mult / 128) - 2) << CS4234_MCLK_RATE_SHIFT);
3698c2ecf20Sopenharmony_ci		break;
3708c2ecf20Sopenharmony_ci	default:
3718c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported mclk/lrclk rate\n");
3728c2ecf20Sopenharmony_ci		return -EINVAL;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	switch (cs4234->lrclk_rate) {
3768c2ecf20Sopenharmony_ci	case 48000:
3778c2ecf20Sopenharmony_ci	case 96000:
3788c2ecf20Sopenharmony_ci		rate_ad = CS4234_48K;
3798c2ecf20Sopenharmony_ci		break;
3808c2ecf20Sopenharmony_ci	case 44100:
3818c2ecf20Sopenharmony_ci	case 88200:
3828c2ecf20Sopenharmony_ci		rate_ad = CS4234_44K1;
3838c2ecf20Sopenharmony_ci		break;
3848c2ecf20Sopenharmony_ci	case 32000:
3858c2ecf20Sopenharmony_ci	case 64000:
3868c2ecf20Sopenharmony_ci		rate_ad = CS4234_32K;
3878c2ecf20Sopenharmony_ci		break;
3888c2ecf20Sopenharmony_ci	default:
3898c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported LR clock\n");
3908c2ecf20Sopenharmony_ci		return -EINVAL;
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_CLOCK_SP, CS4234_BASE_RATE_MASK,
3938c2ecf20Sopenharmony_ci			   rate_ad << CS4234_BASE_RATE_SHIFT);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	sample_width = params_width(params);
3968c2ecf20Sopenharmony_ci	switch (sample_width) {
3978c2ecf20Sopenharmony_ci	case 16:
3988c2ecf20Sopenharmony_ci		sample_width = 0;
3998c2ecf20Sopenharmony_ci		break;
4008c2ecf20Sopenharmony_ci	case 18:
4018c2ecf20Sopenharmony_ci		sample_width = 1;
4028c2ecf20Sopenharmony_ci		break;
4038c2ecf20Sopenharmony_ci	case 20:
4048c2ecf20Sopenharmony_ci		sample_width = 2;
4058c2ecf20Sopenharmony_ci		break;
4068c2ecf20Sopenharmony_ci	case 24:
4078c2ecf20Sopenharmony_ci		sample_width = 3;
4088c2ecf20Sopenharmony_ci		break;
4098c2ecf20Sopenharmony_ci	default:
4108c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported sample width\n");
4118c2ecf20Sopenharmony_ci		return -EINVAL;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci	if (sub->stream == SNDRV_PCM_STREAM_CAPTURE)
4148c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_SAMPLE_WIDTH,
4158c2ecf20Sopenharmony_ci				   CS4234_SDOUTX_SW_MASK,
4168c2ecf20Sopenharmony_ci				   sample_width << CS4234_SDOUTX_SW_SHIFT);
4178c2ecf20Sopenharmony_ci	else
4188c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_SAMPLE_WIDTH,
4198c2ecf20Sopenharmony_ci				CS4234_INPUT_SW_MASK | CS4234_LOW_LAT_SW_MASK | CS4234_DAC5_SW_MASK,
4208c2ecf20Sopenharmony_ci				sample_width << CS4234_INPUT_SW_SHIFT |
4218c2ecf20Sopenharmony_ci				sample_width << CS4234_LOW_LAT_SW_SHIFT |
4228c2ecf20Sopenharmony_ci				sample_width << CS4234_DAC5_SW_SHIFT);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return ret;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci/* Scale MCLK rate by 64 to avoid overflow in the ratnum calculation */
4288c2ecf20Sopenharmony_ci#define CS4234_MCLK_SCALE  64
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic const struct snd_ratnum cs4234_dividers[] = {
4318c2ecf20Sopenharmony_ci	{
4328c2ecf20Sopenharmony_ci		.num = 0,
4338c2ecf20Sopenharmony_ci		.den_min = 256 / CS4234_MCLK_SCALE,
4348c2ecf20Sopenharmony_ci		.den_max = 512 / CS4234_MCLK_SCALE,
4358c2ecf20Sopenharmony_ci		.den_step = 128 / CS4234_MCLK_SCALE,
4368c2ecf20Sopenharmony_ci	},
4378c2ecf20Sopenharmony_ci	{
4388c2ecf20Sopenharmony_ci		.num = 0,
4398c2ecf20Sopenharmony_ci		.den_min = 128 / CS4234_MCLK_SCALE,
4408c2ecf20Sopenharmony_ci		.den_max = 192 / CS4234_MCLK_SCALE,
4418c2ecf20Sopenharmony_ci		.den_step = 64 / CS4234_MCLK_SCALE,
4428c2ecf20Sopenharmony_ci	},
4438c2ecf20Sopenharmony_ci};
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_cistatic int cs4234_dai_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = rule->private;
4488c2ecf20Sopenharmony_ci	int mclk = cs4234->mclk_rate;
4498c2ecf20Sopenharmony_ci	struct snd_interval ranges[] = {
4508c2ecf20Sopenharmony_ci		{ /* Single Speed Mode */
4518c2ecf20Sopenharmony_ci			.min = mclk / clamp(mclk / 30000, 256, 512),
4528c2ecf20Sopenharmony_ci			.max = mclk / clamp(mclk / 50000, 256, 512),
4538c2ecf20Sopenharmony_ci		},
4548c2ecf20Sopenharmony_ci		{ /* Double Speed Mode */
4558c2ecf20Sopenharmony_ci			.min = mclk / clamp(mclk / 60000,  128, 256),
4568c2ecf20Sopenharmony_ci			.max = mclk / clamp(mclk / 100000, 128, 256),
4578c2ecf20Sopenharmony_ci		},
4588c2ecf20Sopenharmony_ci	};
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	return snd_interval_ranges(hw_param_interval(params, rule->var),
4618c2ecf20Sopenharmony_ci				   ARRAY_SIZE(ranges), ranges, 0);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic int cs4234_dai_startup(struct snd_pcm_substream *sub, struct snd_soc_dai *dai)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	struct snd_soc_component *comp = dai->component;
4678c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(comp);
4688c2ecf20Sopenharmony_ci	int i, ret;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	switch (cs4234->format) {
4718c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
4728c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
4738c2ecf20Sopenharmony_ci		cs4234->rate_constraint.nrats = 2;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		/*
4768c2ecf20Sopenharmony_ci		 * Playback only supports 24-bit samples in these modes.
4778c2ecf20Sopenharmony_ci		 * Note: SNDRV_PCM_HW_PARAM_SAMPLE_BITS constrains the physical
4788c2ecf20Sopenharmony_ci		 * width, which we don't care about, so constrain the format.
4798c2ecf20Sopenharmony_ci		 */
4808c2ecf20Sopenharmony_ci		if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
4818c2ecf20Sopenharmony_ci			ret = snd_pcm_hw_constraint_mask64(
4828c2ecf20Sopenharmony_ci						sub->runtime,
4838c2ecf20Sopenharmony_ci						SNDRV_PCM_HW_PARAM_FORMAT,
4848c2ecf20Sopenharmony_ci						SNDRV_PCM_FMTBIT_S24_LE |
4858c2ecf20Sopenharmony_ci						SNDRV_PCM_FMTBIT_S24_3LE);
4868c2ecf20Sopenharmony_ci			if (ret < 0)
4878c2ecf20Sopenharmony_ci				return ret;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci			ret = snd_pcm_hw_constraint_minmax(sub->runtime,
4908c2ecf20Sopenharmony_ci							   SNDRV_PCM_HW_PARAM_CHANNELS,
4918c2ecf20Sopenharmony_ci							   1, 4);
4928c2ecf20Sopenharmony_ci			if (ret < 0)
4938c2ecf20Sopenharmony_ci				return ret;
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		break;
4978c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
4988c2ecf20Sopenharmony_ci		cs4234->rate_constraint.nrats = 1;
4998c2ecf20Sopenharmony_ci		break;
5008c2ecf20Sopenharmony_ci	default:
5018c2ecf20Sopenharmony_ci		dev_err(comp->dev, "Startup unsupported DAI format\n");
5028c2ecf20Sopenharmony_ci		return -EINVAL;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	for (i = 0; i < cs4234->rate_constraint.nrats; i++)
5068c2ecf20Sopenharmony_ci		cs4234->rate_dividers[i].num = cs4234->mclk_rate / CS4234_MCLK_SCALE;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	ret = snd_pcm_hw_constraint_ratnums(sub->runtime, 0,
5098c2ecf20Sopenharmony_ci					    SNDRV_PCM_HW_PARAM_RATE,
5108c2ecf20Sopenharmony_ci					    &cs4234->rate_constraint);
5118c2ecf20Sopenharmony_ci	if (ret < 0)
5128c2ecf20Sopenharmony_ci		return ret;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	/*
5158c2ecf20Sopenharmony_ci	 * MCLK/rate may be a valid ratio but out-of-spec (e.g. 24576000/64000)
5168c2ecf20Sopenharmony_ci	 * so this rule limits the range of sample rate for given MCLK.
5178c2ecf20Sopenharmony_ci	 */
5188c2ecf20Sopenharmony_ci	return snd_pcm_hw_rule_add(sub->runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5198c2ecf20Sopenharmony_ci				   cs4234_dai_rule_rate, cs4234, -1);
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic int cs4234_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
5238c2ecf20Sopenharmony_ci				   unsigned int rx_mask, int slots, int slot_width)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
5268c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = snd_soc_component_get_drvdata(component);
5278c2ecf20Sopenharmony_ci	unsigned int slot_offset, dac5_slot, dac5_mask_group;
5288c2ecf20Sopenharmony_ci	uint8_t dac5_masks[4];
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (slot_width != 32) {
5318c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported slot width\n");
5328c2ecf20Sopenharmony_ci		return -EINVAL;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* Either 4 or 5 consecutive bits, DAC5 is optional */
5368c2ecf20Sopenharmony_ci	slot_offset = ffs(tx_mask) - 1;
5378c2ecf20Sopenharmony_ci	tx_mask >>= slot_offset;
5388c2ecf20Sopenharmony_ci	if ((slot_offset % 4) || ((tx_mask != 0x0F) && (tx_mask != 0x1F))) {
5398c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported tx slots allocation\n");
5408c2ecf20Sopenharmony_ci		return -EINVAL;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_SP_DATA_SEL, CS4234_DAC14_SRC_MASK,
5448c2ecf20Sopenharmony_ci			   (slot_offset / 4) << CS4234_DAC14_SRC_SHIFT);
5458c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_SP_DATA_SEL, CS4234_LL_SRC_MASK,
5468c2ecf20Sopenharmony_ci			   (slot_offset / 4) << CS4234_LL_SRC_SHIFT);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	if (tx_mask == 0x1F) {
5498c2ecf20Sopenharmony_ci		dac5_slot = slot_offset + 4;
5508c2ecf20Sopenharmony_ci		memset(dac5_masks, 0xFF, sizeof(dac5_masks));
5518c2ecf20Sopenharmony_ci		dac5_mask_group = dac5_slot / 8;
5528c2ecf20Sopenharmony_ci		dac5_slot %= 8;
5538c2ecf20Sopenharmony_ci		dac5_masks[dac5_mask_group] ^= BIT(7 - dac5_slot);
5548c2ecf20Sopenharmony_ci		regmap_bulk_write(cs4234->regmap,
5558c2ecf20Sopenharmony_ci				  CS4234_SDIN1_MASK1,
5568c2ecf20Sopenharmony_ci				  dac5_masks,
5578c2ecf20Sopenharmony_ci				  ARRAY_SIZE(dac5_masks));
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	return 0;
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops cs4234_dai_ops = {
5648c2ecf20Sopenharmony_ci	.set_fmt	= cs4234_dai_set_fmt,
5658c2ecf20Sopenharmony_ci	.hw_params	= cs4234_dai_hw_params,
5668c2ecf20Sopenharmony_ci	.startup	= cs4234_dai_startup,
5678c2ecf20Sopenharmony_ci	.set_tdm_slot	= cs4234_dai_set_tdm_slot,
5688c2ecf20Sopenharmony_ci};
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver cs4234_dai[] = {
5718c2ecf20Sopenharmony_ci	{
5728c2ecf20Sopenharmony_ci		.name = "cs4234-dai",
5738c2ecf20Sopenharmony_ci		.playback = {
5748c2ecf20Sopenharmony_ci			.stream_name = "Playback",
5758c2ecf20Sopenharmony_ci			.channels_min = 1,
5768c2ecf20Sopenharmony_ci			.channels_max = 5,
5778c2ecf20Sopenharmony_ci			.rates = CS4234_PCM_RATES,
5788c2ecf20Sopenharmony_ci			.formats = CS4234_FORMATS,
5798c2ecf20Sopenharmony_ci		},
5808c2ecf20Sopenharmony_ci		.capture = {
5818c2ecf20Sopenharmony_ci			.stream_name = "Capture",
5828c2ecf20Sopenharmony_ci			.channels_min = 1,
5838c2ecf20Sopenharmony_ci			.channels_max = 4,
5848c2ecf20Sopenharmony_ci			.rates = CS4234_PCM_RATES,
5858c2ecf20Sopenharmony_ci			.formats = CS4234_FORMATS,
5868c2ecf20Sopenharmony_ci		},
5878c2ecf20Sopenharmony_ci		.ops = &cs4234_dai_ops,
5888c2ecf20Sopenharmony_ci		.symmetric_rates = 1,
5898c2ecf20Sopenharmony_ci	},
5908c2ecf20Sopenharmony_ci};
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic const struct reg_default cs4234_default_reg[] = {
5938c2ecf20Sopenharmony_ci	{ CS4234_CLOCK_SP,	 0x04},
5948c2ecf20Sopenharmony_ci	{ CS4234_SAMPLE_WIDTH,	 0xFF},
5958c2ecf20Sopenharmony_ci	{ CS4234_SP_CTRL,	 0x48},
5968c2ecf20Sopenharmony_ci	{ CS4234_SP_DATA_SEL,	 0x01},
5978c2ecf20Sopenharmony_ci	{ CS4234_SDIN1_MASK1,	 0xFF},
5988c2ecf20Sopenharmony_ci	{ CS4234_SDIN1_MASK2,	 0xFF},
5998c2ecf20Sopenharmony_ci	{ CS4234_SDIN2_MASK1,	 0xFF},
6008c2ecf20Sopenharmony_ci	{ CS4234_SDIN2_MASK2,	 0xFF},
6018c2ecf20Sopenharmony_ci	{ CS4234_TPS_CTRL,	 0x00},
6028c2ecf20Sopenharmony_ci	{ CS4234_ADC_CTRL1,	 0xC0},
6038c2ecf20Sopenharmony_ci	{ CS4234_ADC_CTRL2,	 0xFF},
6048c2ecf20Sopenharmony_ci	{ CS4234_LOW_LAT_CTRL1,	 0xE0},
6058c2ecf20Sopenharmony_ci	{ CS4234_DAC_CTRL1,	 0xE0},
6068c2ecf20Sopenharmony_ci	{ CS4234_DAC_CTRL2,	 0xE0},
6078c2ecf20Sopenharmony_ci	{ CS4234_DAC_CTRL3,	 0xBF},
6088c2ecf20Sopenharmony_ci	{ CS4234_DAC_CTRL4,	 0x1F},
6098c2ecf20Sopenharmony_ci	{ CS4234_VOLUME_MODE,	 0x87},
6108c2ecf20Sopenharmony_ci	{ CS4234_MASTER_VOL,	 0x10},
6118c2ecf20Sopenharmony_ci	{ CS4234_DAC1_VOL,	 0x10},
6128c2ecf20Sopenharmony_ci	{ CS4234_DAC2_VOL,	 0x10},
6138c2ecf20Sopenharmony_ci	{ CS4234_DAC3_VOL,	 0x10},
6148c2ecf20Sopenharmony_ci	{ CS4234_DAC4_VOL,	 0x10},
6158c2ecf20Sopenharmony_ci	{ CS4234_DAC5_VOL,	 0x10},
6168c2ecf20Sopenharmony_ci	{ CS4234_INT_CTRL,	 0x40},
6178c2ecf20Sopenharmony_ci	{ CS4234_INT_MASK1,	 0x10},
6188c2ecf20Sopenharmony_ci	{ CS4234_INT_MASK2,	 0x20},
6198c2ecf20Sopenharmony_ci};
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic bool cs4234_readable_register(struct device *dev, unsigned int reg)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	switch (reg) {
6248c2ecf20Sopenharmony_ci	case CS4234_DEVID_AB ... CS4234_DEVID_EF:
6258c2ecf20Sopenharmony_ci	case CS4234_REVID ... CS4234_DAC5_VOL:
6268c2ecf20Sopenharmony_ci	case CS4234_INT_CTRL ... CS4234_MAX_REGISTER:
6278c2ecf20Sopenharmony_ci		return true;
6288c2ecf20Sopenharmony_ci	default:
6298c2ecf20Sopenharmony_ci		return false;
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic bool cs4234_volatile_reg(struct device *dev, unsigned int reg)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	switch (reg) {
6368c2ecf20Sopenharmony_ci	case CS4234_INT_NOTIFY1:
6378c2ecf20Sopenharmony_ci	case CS4234_INT_NOTIFY2:
6388c2ecf20Sopenharmony_ci		return true;
6398c2ecf20Sopenharmony_ci	default:
6408c2ecf20Sopenharmony_ci		return false;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic bool cs4234_writeable_register(struct device *dev, unsigned int reg)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	switch (reg) {
6478c2ecf20Sopenharmony_ci	case CS4234_DEVID_AB ... CS4234_REVID:
6488c2ecf20Sopenharmony_ci	case CS4234_INT_NOTIFY1 ... CS4234_INT_NOTIFY2:
6498c2ecf20Sopenharmony_ci		return false;
6508c2ecf20Sopenharmony_ci	default:
6518c2ecf20Sopenharmony_ci		return true;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver soc_component_cs4234 = {
6568c2ecf20Sopenharmony_ci	.dapm_widgets		= cs4234_dapm_widgets,
6578c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(cs4234_dapm_widgets),
6588c2ecf20Sopenharmony_ci	.dapm_routes		= cs4234_dapm_routes,
6598c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(cs4234_dapm_routes),
6608c2ecf20Sopenharmony_ci	.controls		= cs4234_snd_controls,
6618c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(cs4234_snd_controls),
6628c2ecf20Sopenharmony_ci	.set_bias_level		= cs4234_set_bias_level,
6638c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
6648c2ecf20Sopenharmony_ci	.idle_bias_on		= 1,
6658c2ecf20Sopenharmony_ci	.suspend_bias_off	= 1,
6668c2ecf20Sopenharmony_ci};
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic const struct regmap_config cs4234_regmap = {
6698c2ecf20Sopenharmony_ci	.reg_bits = 8,
6708c2ecf20Sopenharmony_ci	.val_bits = 8,
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	.max_register = CS4234_MAX_REGISTER,
6738c2ecf20Sopenharmony_ci	.readable_reg = cs4234_readable_register,
6748c2ecf20Sopenharmony_ci	.volatile_reg = cs4234_volatile_reg,
6758c2ecf20Sopenharmony_ci	.writeable_reg = cs4234_writeable_register,
6768c2ecf20Sopenharmony_ci	.reg_defaults = cs4234_default_reg,
6778c2ecf20Sopenharmony_ci	.num_reg_defaults = ARRAY_SIZE(cs4234_default_reg),
6788c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
6798c2ecf20Sopenharmony_ci	.use_single_read = true,
6808c2ecf20Sopenharmony_ci	.use_single_write = true,
6818c2ecf20Sopenharmony_ci};
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic const char * const cs4234_core_supplies[] = {
6848c2ecf20Sopenharmony_ci	"VA",
6858c2ecf20Sopenharmony_ci	"VL",
6868c2ecf20Sopenharmony_ci};
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic void cs4234_shutdown(struct cs4234 *cs4234)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&cs4234->vq_ramp_delay);
6918c2ecf20Sopenharmony_ci	reinit_completion(&cs4234->vq_ramp_complete);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_DAC_CTRL4, CS4234_VQ_RAMP_MASK,
6948c2ecf20Sopenharmony_ci			   CS4234_VQ_RAMP_MASK);
6958c2ecf20Sopenharmony_ci	msleep(50);
6968c2ecf20Sopenharmony_ci	regcache_cache_only(cs4234->regmap, true);
6978c2ecf20Sopenharmony_ci	/* Clear VQ Ramp Bit in cache for the next PowerUp */
6988c2ecf20Sopenharmony_ci	regmap_update_bits(cs4234->regmap, CS4234_DAC_CTRL4, CS4234_VQ_RAMP_MASK, 0);
6998c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(cs4234->reset_gpio, 0);
7008c2ecf20Sopenharmony_ci	regulator_bulk_disable(cs4234->num_core_supplies, cs4234->core_supplies);
7018c2ecf20Sopenharmony_ci	clk_disable_unprepare(cs4234->mclk);
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic int cs4234_powerup(struct cs4234 *cs4234)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	int ret;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(cs4234->mclk);
7098c2ecf20Sopenharmony_ci	if (ret) {
7108c2ecf20Sopenharmony_ci		dev_err(cs4234->dev, "Failed to enable mclk: %d\n", ret);
7118c2ecf20Sopenharmony_ci		return ret;
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(cs4234->num_core_supplies, cs4234->core_supplies);
7158c2ecf20Sopenharmony_ci	if (ret) {
7168c2ecf20Sopenharmony_ci		dev_err(cs4234->dev, "Failed to enable core supplies: %d\n", ret);
7178c2ecf20Sopenharmony_ci		clk_disable_unprepare(cs4234->mclk);
7188c2ecf20Sopenharmony_ci		return ret;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	usleep_range(CS4234_HOLD_RESET_TIME_US, 2 * CS4234_HOLD_RESET_TIME_US);
7228c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(cs4234->reset_gpio, 1);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* Make sure hardware reset done 2 ms + (3000/MCLK) */
7258c2ecf20Sopenharmony_ci	usleep_range(CS4234_BOOT_TIME_US, CS4234_BOOT_TIME_US * 2);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq,
7288c2ecf20Sopenharmony_ci			   &cs4234->vq_ramp_delay,
7298c2ecf20Sopenharmony_ci			   msecs_to_jiffies(CS4234_VQ_CHARGE_MS));
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	return 0;
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_cistatic int cs4234_i2c_probe(struct i2c_client *i2c_client, const struct i2c_device_id *id)
7358c2ecf20Sopenharmony_ci{
7368c2ecf20Sopenharmony_ci	struct cs4234 *cs4234;
7378c2ecf20Sopenharmony_ci	struct device *dev = &i2c_client->dev;
7388c2ecf20Sopenharmony_ci	unsigned int revid;
7398c2ecf20Sopenharmony_ci	uint32_t devid;
7408c2ecf20Sopenharmony_ci	uint8_t ids[3];
7418c2ecf20Sopenharmony_ci	int ret = 0, i;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	cs4234 = devm_kzalloc(dev, sizeof(*cs4234), GFP_KERNEL);
7448c2ecf20Sopenharmony_ci	if (!cs4234)
7458c2ecf20Sopenharmony_ci		return -ENOMEM;
7468c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c_client, cs4234);
7478c2ecf20Sopenharmony_ci	cs4234->dev = dev;
7488c2ecf20Sopenharmony_ci	init_completion(&cs4234->vq_ramp_complete);
7498c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&cs4234->vq_ramp_delay, cs4234_vq_ramp_done);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	cs4234->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
7528c2ecf20Sopenharmony_ci	if (IS_ERR(cs4234->reset_gpio))
7538c2ecf20Sopenharmony_ci		return PTR_ERR(cs4234->reset_gpio);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(cs4234->core_supplies) < ARRAY_SIZE(cs4234_core_supplies));
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	cs4234->num_core_supplies = ARRAY_SIZE(cs4234_core_supplies);
7588c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cs4234_core_supplies); i++)
7598c2ecf20Sopenharmony_ci		cs4234->core_supplies[i].supply = cs4234_core_supplies[i];
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(dev, cs4234->num_core_supplies, cs4234->core_supplies);
7628c2ecf20Sopenharmony_ci	if (ret) {
7638c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to request core supplies %d\n", ret);
7648c2ecf20Sopenharmony_ci		return ret;
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	cs4234->mclk = devm_clk_get(dev, "mclk");
7688c2ecf20Sopenharmony_ci	if (IS_ERR(cs4234->mclk)) {
7698c2ecf20Sopenharmony_ci		ret = PTR_ERR(cs4234->mclk);
7708c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get the mclk: %d\n", ret);
7718c2ecf20Sopenharmony_ci		return ret;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci	cs4234->mclk_rate = clk_get_rate(cs4234->mclk);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	if (cs4234->mclk_rate < 7680000 || cs4234->mclk_rate > 25600000) {
7768c2ecf20Sopenharmony_ci		dev_err(dev, "Invalid Master Clock rate\n");
7778c2ecf20Sopenharmony_ci		return -EINVAL;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	cs4234->regmap = devm_regmap_init_i2c(i2c_client, &cs4234_regmap);
7818c2ecf20Sopenharmony_ci	if (IS_ERR(cs4234->regmap)) {
7828c2ecf20Sopenharmony_ci		ret = PTR_ERR(cs4234->regmap);
7838c2ecf20Sopenharmony_ci		dev_err(dev, "regmap_init() failed: %d\n", ret);
7848c2ecf20Sopenharmony_ci		return ret;
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	ret = cs4234_powerup(cs4234);
7888c2ecf20Sopenharmony_ci	if (ret)
7898c2ecf20Sopenharmony_ci		return ret;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(cs4234->regmap, CS4234_DEVID_AB, ids, ARRAY_SIZE(ids));
7928c2ecf20Sopenharmony_ci	if (ret < 0) {
7938c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to read DEVID: %d\n", ret);
7948c2ecf20Sopenharmony_ci		goto fail_shutdown;
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	devid = (ids[0] << 16) | (ids[1] << 8) | ids[2];
7988c2ecf20Sopenharmony_ci	if (devid != CS4234_SUPPORTED_ID) {
7998c2ecf20Sopenharmony_ci		dev_err(dev, "Unknown device ID: %x\n", devid);
8008c2ecf20Sopenharmony_ci		ret = -EINVAL;
8018c2ecf20Sopenharmony_ci		goto fail_shutdown;
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	ret = regmap_read(cs4234->regmap, CS4234_REVID, &revid);
8058c2ecf20Sopenharmony_ci	if (ret < 0) {
8068c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to read CS4234_REVID: %d\n", ret);
8078c2ecf20Sopenharmony_ci		goto fail_shutdown;
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	dev_info(dev, "Cirrus Logic CS4234, Alpha Rev: %02X, Numeric Rev: %02X\n",
8118c2ecf20Sopenharmony_ci		 (revid & 0xF0) >> 4, revid & 0x0F);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	ret = regulator_get_voltage(cs4234->core_supplies[CS4234_SUPPLY_VA].consumer);
8148c2ecf20Sopenharmony_ci	switch (ret) {
8158c2ecf20Sopenharmony_ci	case 3135000 ... 3650000:
8168c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_ADC_CTRL1,
8178c2ecf20Sopenharmony_ci				   CS4234_VA_SEL_MASK,
8188c2ecf20Sopenharmony_ci				   CS4234_3V3 << CS4234_VA_SEL_SHIFT);
8198c2ecf20Sopenharmony_ci		break;
8208c2ecf20Sopenharmony_ci	case 4750000 ... 5250000:
8218c2ecf20Sopenharmony_ci		regmap_update_bits(cs4234->regmap, CS4234_ADC_CTRL1,
8228c2ecf20Sopenharmony_ci				   CS4234_VA_SEL_MASK,
8238c2ecf20Sopenharmony_ci				   CS4234_5V << CS4234_VA_SEL_SHIFT);
8248c2ecf20Sopenharmony_ci		break;
8258c2ecf20Sopenharmony_ci	default:
8268c2ecf20Sopenharmony_ci		dev_err(dev, "Invalid VA voltage\n");
8278c2ecf20Sopenharmony_ci		ret = -EINVAL;
8288c2ecf20Sopenharmony_ci		goto fail_shutdown;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	pm_runtime_set_active(&i2c_client->dev);
8328c2ecf20Sopenharmony_ci	pm_runtime_enable(&i2c_client->dev);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	memcpy(&cs4234->rate_dividers, &cs4234_dividers, sizeof(cs4234_dividers));
8358c2ecf20Sopenharmony_ci	cs4234->rate_constraint.rats = cs4234->rate_dividers;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	ret = snd_soc_register_component(dev, &soc_component_cs4234, cs4234_dai,
8388c2ecf20Sopenharmony_ci					 ARRAY_SIZE(cs4234_dai));
8398c2ecf20Sopenharmony_ci	if (ret < 0) {
8408c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to register component:%d\n", ret);
8418c2ecf20Sopenharmony_ci		pm_runtime_disable(&i2c_client->dev);
8428c2ecf20Sopenharmony_ci		goto fail_shutdown;
8438c2ecf20Sopenharmony_ci	}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	return ret;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_cifail_shutdown:
8488c2ecf20Sopenharmony_ci	cs4234_shutdown(cs4234);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	return ret;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic int cs4234_i2c_remove(struct i2c_client *i2c_client)
8548c2ecf20Sopenharmony_ci{
8558c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = i2c_get_clientdata(i2c_client);
8568c2ecf20Sopenharmony_ci	struct device *dev = &i2c_client->dev;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	snd_soc_unregister_component(dev);
8598c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
8608c2ecf20Sopenharmony_ci	cs4234_shutdown(cs4234);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	return 0;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic int __maybe_unused cs4234_runtime_resume(struct device *dev)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = dev_get_drvdata(dev);
8688c2ecf20Sopenharmony_ci	int ret;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	ret = cs4234_powerup(cs4234);
8718c2ecf20Sopenharmony_ci	if (ret)
8728c2ecf20Sopenharmony_ci		return ret;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	regcache_mark_dirty(cs4234->regmap);
8758c2ecf20Sopenharmony_ci	regcache_cache_only(cs4234->regmap, false);
8768c2ecf20Sopenharmony_ci	ret = regcache_sync(cs4234->regmap);
8778c2ecf20Sopenharmony_ci	if (ret) {
8788c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to sync regmap: %d\n", ret);
8798c2ecf20Sopenharmony_ci		cs4234_shutdown(cs4234);
8808c2ecf20Sopenharmony_ci		return ret;
8818c2ecf20Sopenharmony_ci	}
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	return 0;
8848c2ecf20Sopenharmony_ci}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_cistatic int __maybe_unused cs4234_runtime_suspend(struct device *dev)
8878c2ecf20Sopenharmony_ci{
8888c2ecf20Sopenharmony_ci	struct cs4234 *cs4234 = dev_get_drvdata(dev);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	cs4234_shutdown(cs4234);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	return 0;
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistatic const struct dev_pm_ops cs4234_pm = {
8968c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(cs4234_runtime_suspend, cs4234_runtime_resume, NULL)
8978c2ecf20Sopenharmony_ci};
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_cistatic const struct of_device_id cs4234_of_match[] = {
9008c2ecf20Sopenharmony_ci	{ .compatible = "cirrus,cs4234", },
9018c2ecf20Sopenharmony_ci	{ }
9028c2ecf20Sopenharmony_ci};
9038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cs4234_of_match);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_cistatic struct i2c_driver cs4234_i2c_driver = {
9068c2ecf20Sopenharmony_ci	.driver = {
9078c2ecf20Sopenharmony_ci		.name = "cs4234",
9088c2ecf20Sopenharmony_ci		.pm = &cs4234_pm,
9098c2ecf20Sopenharmony_ci		.of_match_table = cs4234_of_match,
9108c2ecf20Sopenharmony_ci	},
9118c2ecf20Sopenharmony_ci	.probe =	cs4234_i2c_probe,
9128c2ecf20Sopenharmony_ci	.remove =	cs4234_i2c_remove,
9138c2ecf20Sopenharmony_ci};
9148c2ecf20Sopenharmony_cimodule_i2c_driver(cs4234_i2c_driver);
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC Cirrus Logic CS4234 driver");
9178c2ecf20Sopenharmony_ciMODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
9188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
919