xref: /openbmc/linux/sound/soc/codecs/cs35l35.c (revision 86c2eddfba351824cffccdd5182954c1cf0b9a8a)
16387f866SBrian Austin /*
26387f866SBrian Austin  * cs35l35.c -- CS35L35 ALSA SoC audio driver
36387f866SBrian Austin  *
46387f866SBrian Austin  * Copyright 2017 Cirrus Logic, Inc.
56387f866SBrian Austin  *
66387f866SBrian Austin  * Author: Brian Austin <brian.austin@cirrus.com>
76387f866SBrian Austin  *
86387f866SBrian Austin  * This program is free software; you can redistribute it and/or modify
96387f866SBrian Austin  * it under the terms of the GNU General Public License version 2 as
106387f866SBrian Austin  * published by the Free Software Foundation.
116387f866SBrian Austin  *
126387f866SBrian Austin  */
136387f866SBrian Austin 
146387f866SBrian Austin #include <linux/module.h>
156387f866SBrian Austin #include <linux/moduleparam.h>
166387f866SBrian Austin #include <linux/version.h>
176387f866SBrian Austin #include <linux/kernel.h>
186387f866SBrian Austin #include <linux/init.h>
196387f866SBrian Austin #include <linux/delay.h>
206387f866SBrian Austin #include <linux/i2c.h>
216387f866SBrian Austin #include <linux/slab.h>
226387f866SBrian Austin #include <linux/platform_device.h>
236387f866SBrian Austin #include <linux/regulator/consumer.h>
246387f866SBrian Austin #include <linux/gpio/consumer.h>
256387f866SBrian Austin #include <linux/of_device.h>
266387f866SBrian Austin #include <linux/of_gpio.h>
276387f866SBrian Austin #include <linux/regmap.h>
286387f866SBrian Austin #include <sound/core.h>
296387f866SBrian Austin #include <sound/pcm.h>
306387f866SBrian Austin #include <sound/pcm_params.h>
316387f866SBrian Austin #include <sound/soc.h>
326387f866SBrian Austin #include <sound/soc-dapm.h>
336387f866SBrian Austin #include <linux/gpio.h>
346387f866SBrian Austin #include <sound/initval.h>
356387f866SBrian Austin #include <sound/tlv.h>
366387f866SBrian Austin #include <sound/cs35l35.h>
376387f866SBrian Austin #include <linux/of_irq.h>
386387f866SBrian Austin #include <linux/completion.h>
396387f866SBrian Austin 
406387f866SBrian Austin #include "cs35l35.h"
416387f866SBrian Austin 
426387f866SBrian Austin /*
436387f866SBrian Austin  * Some fields take zero as a valid value so use a high bit flag that won't
446387f866SBrian Austin  * get written to the device to mark those.
456387f866SBrian Austin  */
466387f866SBrian Austin #define CS35L35_VALID_PDATA 0x80000000
476387f866SBrian Austin 
486387f866SBrian Austin static const struct reg_default cs35l35_reg[] = {
496387f866SBrian Austin 	{CS35L35_PWRCTL1,		0x01},
506387f866SBrian Austin 	{CS35L35_PWRCTL2,		0x11},
516387f866SBrian Austin 	{CS35L35_PWRCTL3,		0x00},
526387f866SBrian Austin 	{CS35L35_CLK_CTL1,		0x04},
53fbeea237SCharles Keepax 	{CS35L35_CLK_CTL2,		0x12},
546387f866SBrian Austin 	{CS35L35_CLK_CTL3,		0xCF},
556387f866SBrian Austin 	{CS35L35_SP_FMT_CTL1,		0x20},
566387f866SBrian Austin 	{CS35L35_SP_FMT_CTL2,		0x00},
576387f866SBrian Austin 	{CS35L35_SP_FMT_CTL3,		0x02},
586387f866SBrian Austin 	{CS35L35_MAG_COMP_CTL,		0x00},
596387f866SBrian Austin 	{CS35L35_AMP_INP_DRV_CTL,	0x01},
606387f866SBrian Austin 	{CS35L35_AMP_DIG_VOL_CTL,	0x12},
616387f866SBrian Austin 	{CS35L35_AMP_DIG_VOL,		0x00},
626387f866SBrian Austin 	{CS35L35_ADV_DIG_VOL,		0x00},
636387f866SBrian Austin 	{CS35L35_PROTECT_CTL,		0x06},
646387f866SBrian Austin 	{CS35L35_AMP_GAIN_AUD_CTL,	0x13},
656387f866SBrian Austin 	{CS35L35_AMP_GAIN_PDM_CTL,	0x00},
666387f866SBrian Austin 	{CS35L35_AMP_GAIN_ADV_CTL,	0x00},
676387f866SBrian Austin 	{CS35L35_GPI_CTL,		0x00},
686387f866SBrian Austin 	{CS35L35_BST_CVTR_V_CTL,	0x00},
696387f866SBrian Austin 	{CS35L35_BST_PEAK_I,		0x07},
706387f866SBrian Austin 	{CS35L35_BST_RAMP_CTL,		0x85},
716387f866SBrian Austin 	{CS35L35_BST_CONV_COEF_1,	0x24},
726387f866SBrian Austin 	{CS35L35_BST_CONV_COEF_2,	0x24},
73fbeea237SCharles Keepax 	{CS35L35_BST_CONV_SLOPE_COMP,	0x4E},
746387f866SBrian Austin 	{CS35L35_BST_CONV_SW_FREQ,	0x04},
756387f866SBrian Austin 	{CS35L35_CLASS_H_CTL,		0x0B},
766387f866SBrian Austin 	{CS35L35_CLASS_H_HEADRM_CTL,	0x0B},
776387f866SBrian Austin 	{CS35L35_CLASS_H_RELEASE_RATE,	0x08},
786387f866SBrian Austin 	{CS35L35_CLASS_H_FET_DRIVE_CTL, 0x41},
796387f866SBrian Austin 	{CS35L35_CLASS_H_VP_CTL,	0xC5},
806387f866SBrian Austin 	{CS35L35_VPBR_CTL,		0x0A},
81fbeea237SCharles Keepax 	{CS35L35_VPBR_VOL_CTL,		0x90},
826387f866SBrian Austin 	{CS35L35_VPBR_TIMING_CTL,	0x6A},
83fbeea237SCharles Keepax 	{CS35L35_VPBR_MODE_VOL_CTL,	0x00},
846387f866SBrian Austin 	{CS35L35_SPKR_MON_CTL,		0xC0},
856387f866SBrian Austin 	{CS35L35_IMON_SCALE_CTL,	0x30},
866387f866SBrian Austin 	{CS35L35_AUDIN_RXLOC_CTL,	0x00},
876387f866SBrian Austin 	{CS35L35_ADVIN_RXLOC_CTL,	0x80},
886387f866SBrian Austin 	{CS35L35_VMON_TXLOC_CTL,	0x00},
896387f866SBrian Austin 	{CS35L35_IMON_TXLOC_CTL,	0x80},
906387f866SBrian Austin 	{CS35L35_VPMON_TXLOC_CTL,	0x04},
916387f866SBrian Austin 	{CS35L35_VBSTMON_TXLOC_CTL,	0x84},
926387f866SBrian Austin 	{CS35L35_VPBR_STATUS_TXLOC_CTL,	0x04},
936387f866SBrian Austin 	{CS35L35_ZERO_FILL_LOC_CTL,	0x00},
946387f866SBrian Austin 	{CS35L35_AUDIN_DEPTH_CTL,	0x0F},
956387f866SBrian Austin 	{CS35L35_SPKMON_DEPTH_CTL,	0x0F},
966387f866SBrian Austin 	{CS35L35_SUPMON_DEPTH_CTL,	0x0F},
976387f866SBrian Austin 	{CS35L35_ZEROFILL_DEPTH_CTL,	0x00},
986387f866SBrian Austin 	{CS35L35_MULT_DEV_SYNCH1,	0x02},
996387f866SBrian Austin 	{CS35L35_MULT_DEV_SYNCH2,	0x80},
1006387f866SBrian Austin 	{CS35L35_PROT_RELEASE_CTL,	0x00},
1016387f866SBrian Austin 	{CS35L35_DIAG_MODE_REG_LOCK,	0x00},
1026387f866SBrian Austin 	{CS35L35_DIAG_MODE_CTL_1,	0x40},
1036387f866SBrian Austin 	{CS35L35_DIAG_MODE_CTL_2,	0x00},
1046387f866SBrian Austin 	{CS35L35_INT_MASK_1,		0xFF},
1056387f866SBrian Austin 	{CS35L35_INT_MASK_2,		0xFF},
1066387f866SBrian Austin 	{CS35L35_INT_MASK_3,		0xFF},
1076387f866SBrian Austin 	{CS35L35_INT_MASK_4,		0xFF},
1086387f866SBrian Austin 
1096387f866SBrian Austin };
1106387f866SBrian Austin 
1116387f866SBrian Austin static bool cs35l35_volatile_register(struct device *dev, unsigned int reg)
1126387f866SBrian Austin {
1136387f866SBrian Austin 	switch (reg) {
1146387f866SBrian Austin 	case CS35L35_INT_STATUS_1:
1156387f866SBrian Austin 	case CS35L35_INT_STATUS_2:
1166387f866SBrian Austin 	case CS35L35_INT_STATUS_3:
1176387f866SBrian Austin 	case CS35L35_INT_STATUS_4:
1186387f866SBrian Austin 	case CS35L35_PLL_STATUS:
1196387f866SBrian Austin 	case CS35L35_OTP_TRIM_STATUS:
1206387f866SBrian Austin 		return true;
1216387f866SBrian Austin 	default:
1226387f866SBrian Austin 		return false;
1236387f866SBrian Austin 	}
1246387f866SBrian Austin }
1256387f866SBrian Austin 
1266387f866SBrian Austin static bool cs35l35_readable_register(struct device *dev, unsigned int reg)
1276387f866SBrian Austin {
1286387f866SBrian Austin 	switch (reg) {
1296387f866SBrian Austin 	case CS35L35_DEVID_AB ... CS35L35_PWRCTL3:
1306387f866SBrian Austin 	case CS35L35_CLK_CTL1 ... CS35L35_SP_FMT_CTL3:
1316387f866SBrian Austin 	case CS35L35_MAG_COMP_CTL ... CS35L35_AMP_GAIN_AUD_CTL:
1326387f866SBrian Austin 	case CS35L35_AMP_GAIN_PDM_CTL ... CS35L35_BST_PEAK_I:
1336387f866SBrian Austin 	case CS35L35_BST_RAMP_CTL ... CS35L35_BST_CONV_SW_FREQ:
1346387f866SBrian Austin 	case CS35L35_CLASS_H_CTL ... CS35L35_CLASS_H_VP_CTL:
1356387f866SBrian Austin 	case CS35L35_CLASS_H_STATUS:
1366387f866SBrian Austin 	case CS35L35_VPBR_CTL ... CS35L35_VPBR_MODE_VOL_CTL:
1376387f866SBrian Austin 	case CS35L35_VPBR_ATTEN_STATUS:
1386387f866SBrian Austin 	case CS35L35_SPKR_MON_CTL:
1396387f866SBrian Austin 	case CS35L35_IMON_SCALE_CTL ... CS35L35_ZEROFILL_DEPTH_CTL:
1406387f866SBrian Austin 	case CS35L35_MULT_DEV_SYNCH1 ... CS35L35_PROT_RELEASE_CTL:
1416387f866SBrian Austin 	case CS35L35_DIAG_MODE_REG_LOCK ... CS35L35_DIAG_MODE_CTL_2:
1426387f866SBrian Austin 	case CS35L35_INT_MASK_1 ... CS35L35_PLL_STATUS:
1436387f866SBrian Austin 	case CS35L35_OTP_TRIM_STATUS:
1446387f866SBrian Austin 		return true;
1456387f866SBrian Austin 	default:
1466387f866SBrian Austin 		return false;
1476387f866SBrian Austin 	}
1486387f866SBrian Austin }
1496387f866SBrian Austin 
1506387f866SBrian Austin static bool cs35l35_precious_register(struct device *dev, unsigned int reg)
1516387f866SBrian Austin {
1526387f866SBrian Austin 	switch (reg) {
1536387f866SBrian Austin 	case CS35L35_INT_STATUS_1:
1546387f866SBrian Austin 	case CS35L35_INT_STATUS_2:
1556387f866SBrian Austin 	case CS35L35_INT_STATUS_3:
1566387f866SBrian Austin 	case CS35L35_INT_STATUS_4:
1576387f866SBrian Austin 	case CS35L35_PLL_STATUS:
1586387f866SBrian Austin 	case CS35L35_OTP_TRIM_STATUS:
1596387f866SBrian Austin 		return true;
1606387f866SBrian Austin 	default:
1616387f866SBrian Austin 		return false;
1626387f866SBrian Austin 	}
1636387f866SBrian Austin }
1646387f866SBrian Austin 
165dc43f46aSCharles Keepax static void cs35l35_reset(struct cs35l35_private *cs35l35)
166dc43f46aSCharles Keepax {
167dc43f46aSCharles Keepax 	gpiod_set_value_cansleep(cs35l35->reset_gpio, 0);
168dc43f46aSCharles Keepax 	usleep_range(2000, 2100);
169dc43f46aSCharles Keepax 	gpiod_set_value_cansleep(cs35l35->reset_gpio, 1);
170dc43f46aSCharles Keepax 	usleep_range(1000, 1100);
171dc43f46aSCharles Keepax }
172dc43f46aSCharles Keepax 
17377b329d1SCharles Keepax static int cs35l35_wait_for_pdn(struct cs35l35_private *cs35l35)
17477b329d1SCharles Keepax {
17577b329d1SCharles Keepax 	int ret;
17677b329d1SCharles Keepax 
17777b329d1SCharles Keepax 	if (cs35l35->pdata.ext_bst) {
17877b329d1SCharles Keepax 		usleep_range(5000, 5500);
17977b329d1SCharles Keepax 		return 0;
18077b329d1SCharles Keepax 	}
18177b329d1SCharles Keepax 
18277b329d1SCharles Keepax 	reinit_completion(&cs35l35->pdn_done);
18377b329d1SCharles Keepax 
18477b329d1SCharles Keepax 	ret = wait_for_completion_timeout(&cs35l35->pdn_done,
18577b329d1SCharles Keepax 					  msecs_to_jiffies(100));
18677b329d1SCharles Keepax 	if (ret == 0) {
18777b329d1SCharles Keepax 		dev_err(cs35l35->dev, "PDN_DONE did not complete\n");
18877b329d1SCharles Keepax 		return -ETIMEDOUT;
18977b329d1SCharles Keepax 	}
19077b329d1SCharles Keepax 
19177b329d1SCharles Keepax 	return 0;
19277b329d1SCharles Keepax }
19377b329d1SCharles Keepax 
1946387f866SBrian Austin static int cs35l35_sdin_event(struct snd_soc_dapm_widget *w,
1956387f866SBrian Austin 		struct snd_kcontrol *kcontrol, int event)
1966387f866SBrian Austin {
197*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
198*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
1996387f866SBrian Austin 	int ret = 0;
2006387f866SBrian Austin 
2016387f866SBrian Austin 	switch (event) {
2026387f866SBrian Austin 	case SND_SOC_DAPM_PRE_PMU:
2036387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
2046387f866SBrian Austin 					CS35L35_MCLK_DIS_MASK,
2056387f866SBrian Austin 					0 << CS35L35_MCLK_DIS_SHIFT);
2066387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
2076387f866SBrian Austin 					CS35L35_DISCHG_FILT_MASK,
2086387f866SBrian Austin 					0 << CS35L35_DISCHG_FILT_SHIFT);
2096387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
2106387f866SBrian Austin 					CS35L35_PDN_ALL_MASK, 0);
2116387f866SBrian Austin 		break;
2126387f866SBrian Austin 	case SND_SOC_DAPM_POST_PMD:
2136387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
2146387f866SBrian Austin 					CS35L35_DISCHG_FILT_MASK,
2156387f866SBrian Austin 					1 << CS35L35_DISCHG_FILT_SHIFT);
2166387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
2176387f866SBrian Austin 					  CS35L35_PDN_ALL_MASK, 1);
2186387f866SBrian Austin 
2192c84afb5SCharles Keepax 		/* Already muted, so disable volume ramp for faster shutdown */
2202c84afb5SCharles Keepax 		regmap_update_bits(cs35l35->regmap, CS35L35_AMP_DIG_VOL_CTL,
2212c84afb5SCharles Keepax 				   CS35L35_AMP_DIGSFT_MASK, 0);
2222c84afb5SCharles Keepax 
22377b329d1SCharles Keepax 		ret = cs35l35_wait_for_pdn(cs35l35);
2246387f866SBrian Austin 
2256387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
2266387f866SBrian Austin 					CS35L35_MCLK_DIS_MASK,
2276387f866SBrian Austin 					1 << CS35L35_MCLK_DIS_SHIFT);
2282c84afb5SCharles Keepax 
2292c84afb5SCharles Keepax 		regmap_update_bits(cs35l35->regmap, CS35L35_AMP_DIG_VOL_CTL,
2302c84afb5SCharles Keepax 				   CS35L35_AMP_DIGSFT_MASK,
2312c84afb5SCharles Keepax 				   1 << CS35L35_AMP_DIGSFT_SHIFT);
2326387f866SBrian Austin 		break;
2336387f866SBrian Austin 	default:
234*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Invalid event = 0x%x\n", event);
2356387f866SBrian Austin 		ret = -EINVAL;
2366387f866SBrian Austin 	}
2376387f866SBrian Austin 	return ret;
2386387f866SBrian Austin }
2396387f866SBrian Austin 
2406387f866SBrian Austin static int cs35l35_main_amp_event(struct snd_soc_dapm_widget *w,
2416387f866SBrian Austin 		struct snd_kcontrol *kcontrol, int event)
2426387f866SBrian Austin {
243*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
244*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
2456387f866SBrian Austin 	unsigned int reg[4];
2466387f866SBrian Austin 	int i;
2476387f866SBrian Austin 
2486387f866SBrian Austin 	switch (event) {
2496387f866SBrian Austin 	case SND_SOC_DAPM_PRE_PMU:
2506387f866SBrian Austin 		if (cs35l35->pdata.bst_pdn_fet_on)
2516387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
2526387f866SBrian Austin 				CS35L35_PDN_BST_MASK,
2536387f866SBrian Austin 				0 << CS35L35_PDN_BST_FETON_SHIFT);
2546387f866SBrian Austin 		else
2556387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
2566387f866SBrian Austin 				CS35L35_PDN_BST_MASK,
2576387f866SBrian Austin 				0 << CS35L35_PDN_BST_FETOFF_SHIFT);
2586387f866SBrian Austin 		break;
2596387f866SBrian Austin 	case SND_SOC_DAPM_POST_PMU:
2606387f866SBrian Austin 		usleep_range(5000, 5100);
2616387f866SBrian Austin 		/* If in PDM mode we must use VP for Voltage control */
2626387f866SBrian Austin 		if (cs35l35->pdm_mode)
2636387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
2646387f866SBrian Austin 					CS35L35_BST_CVTR_V_CTL,
2656387f866SBrian Austin 					CS35L35_BST_CTL_MASK,
2666387f866SBrian Austin 					0 << CS35L35_BST_CTL_SHIFT);
2676387f866SBrian Austin 
2686387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
2696387f866SBrian Austin 			CS35L35_AMP_MUTE_MASK, 0);
2706387f866SBrian Austin 
2716387f866SBrian Austin 		for (i = 0; i < 2; i++)
2726387f866SBrian Austin 			regmap_bulk_read(cs35l35->regmap, CS35L35_INT_STATUS_1,
2736387f866SBrian Austin 					&reg, ARRAY_SIZE(reg));
2746387f866SBrian Austin 
2756387f866SBrian Austin 		break;
2766387f866SBrian Austin 	case SND_SOC_DAPM_PRE_PMD:
2776387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
2786387f866SBrian Austin 				CS35L35_AMP_MUTE_MASK,
2796387f866SBrian Austin 				1 << CS35L35_AMP_MUTE_SHIFT);
2806387f866SBrian Austin 		if (cs35l35->pdata.bst_pdn_fet_on)
2816387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
2826387f866SBrian Austin 				CS35L35_PDN_BST_MASK,
2836387f866SBrian Austin 				1 << CS35L35_PDN_BST_FETON_SHIFT);
2846387f866SBrian Austin 		else
2856387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
2866387f866SBrian Austin 				CS35L35_PDN_BST_MASK,
2876387f866SBrian Austin 				1 << CS35L35_PDN_BST_FETOFF_SHIFT);
2886387f866SBrian Austin 		break;
2896387f866SBrian Austin 	case SND_SOC_DAPM_POST_PMD:
2906387f866SBrian Austin 		usleep_range(5000, 5100);
2916387f866SBrian Austin 		/*
2926387f866SBrian Austin 		 * If PDM mode we should switch back to pdata value
2936387f866SBrian Austin 		 * for Voltage control when we go down
2946387f866SBrian Austin 		 */
2956387f866SBrian Austin 		if (cs35l35->pdm_mode)
2966387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
2976387f866SBrian Austin 					CS35L35_BST_CVTR_V_CTL,
2986387f866SBrian Austin 					CS35L35_BST_CTL_MASK,
2996387f866SBrian Austin 					cs35l35->pdata.bst_vctl
3006387f866SBrian Austin 					<< CS35L35_BST_CTL_SHIFT);
3016387f866SBrian Austin 
3026387f866SBrian Austin 		break;
3036387f866SBrian Austin 	default:
304*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Invalid event = 0x%x\n", event);
3056387f866SBrian Austin 	}
3066387f866SBrian Austin 	return 0;
3076387f866SBrian Austin }
3086387f866SBrian Austin 
3096387f866SBrian Austin static DECLARE_TLV_DB_SCALE(amp_gain_tlv, 0, 1, 1);
3106387f866SBrian Austin static DECLARE_TLV_DB_SCALE(dig_vol_tlv, -10200, 50, 0);
3116387f866SBrian Austin 
3126387f866SBrian Austin static const struct snd_kcontrol_new cs35l35_aud_controls[] = {
3136387f866SBrian Austin 	SOC_SINGLE_SX_TLV("Digital Audio Volume", CS35L35_AMP_DIG_VOL,
3146387f866SBrian Austin 		      0, 0x34, 0xE4, dig_vol_tlv),
3156387f866SBrian Austin 	SOC_SINGLE_TLV("Analog Audio Volume", CS35L35_AMP_GAIN_AUD_CTL, 0, 19, 0,
3166387f866SBrian Austin 			amp_gain_tlv),
3176387f866SBrian Austin 	SOC_SINGLE_TLV("PDM Volume", CS35L35_AMP_GAIN_PDM_CTL, 0, 19, 0,
3186387f866SBrian Austin 			amp_gain_tlv),
3196387f866SBrian Austin };
3206387f866SBrian Austin 
3216387f866SBrian Austin static const struct snd_kcontrol_new cs35l35_adv_controls[] = {
3226387f866SBrian Austin 	SOC_SINGLE_SX_TLV("Digital Advisory Volume", CS35L35_ADV_DIG_VOL,
3236387f866SBrian Austin 		      0, 0x34, 0xE4, dig_vol_tlv),
3246387f866SBrian Austin 	SOC_SINGLE_TLV("Analog Advisory Volume", CS35L35_AMP_GAIN_ADV_CTL, 0, 19, 0,
3256387f866SBrian Austin 			amp_gain_tlv),
3266387f866SBrian Austin };
3276387f866SBrian Austin 
3286387f866SBrian Austin static const struct snd_soc_dapm_widget cs35l35_dapm_widgets[] = {
3296387f866SBrian Austin 	SND_SOC_DAPM_AIF_IN_E("SDIN", NULL, 0, CS35L35_PWRCTL3, 1, 1,
3306387f866SBrian Austin 				cs35l35_sdin_event, SND_SOC_DAPM_PRE_PMU |
3316387f866SBrian Austin 				SND_SOC_DAPM_POST_PMD),
3326387f866SBrian Austin 	SND_SOC_DAPM_AIF_OUT("SDOUT", NULL, 0, CS35L35_PWRCTL3, 2, 1),
3336387f866SBrian Austin 
3346387f866SBrian Austin 	SND_SOC_DAPM_OUTPUT("SPK"),
3356387f866SBrian Austin 
3366387f866SBrian Austin 	SND_SOC_DAPM_INPUT("VP"),
3376387f866SBrian Austin 	SND_SOC_DAPM_INPUT("VBST"),
3386387f866SBrian Austin 	SND_SOC_DAPM_INPUT("ISENSE"),
3396387f866SBrian Austin 	SND_SOC_DAPM_INPUT("VSENSE"),
3406387f866SBrian Austin 
3416387f866SBrian Austin 	SND_SOC_DAPM_ADC("VMON ADC", NULL, CS35L35_PWRCTL2, 7, 1),
3426387f866SBrian Austin 	SND_SOC_DAPM_ADC("IMON ADC", NULL, CS35L35_PWRCTL2, 6, 1),
3436387f866SBrian Austin 	SND_SOC_DAPM_ADC("VPMON ADC", NULL, CS35L35_PWRCTL3, 3, 1),
3446387f866SBrian Austin 	SND_SOC_DAPM_ADC("VBSTMON ADC", NULL, CS35L35_PWRCTL3, 4, 1),
3456387f866SBrian Austin 	SND_SOC_DAPM_ADC("CLASS H", NULL, CS35L35_PWRCTL2, 5, 1),
3466387f866SBrian Austin 
3476387f866SBrian Austin 	SND_SOC_DAPM_OUT_DRV_E("Main AMP", CS35L35_PWRCTL2, 0, 1, NULL, 0,
3486387f866SBrian Austin 		cs35l35_main_amp_event, SND_SOC_DAPM_PRE_PMU |
3496387f866SBrian Austin 				SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU |
3506387f866SBrian Austin 				SND_SOC_DAPM_PRE_PMD),
3516387f866SBrian Austin };
3526387f866SBrian Austin 
3536387f866SBrian Austin static const struct snd_soc_dapm_route cs35l35_audio_map[] = {
3546387f866SBrian Austin 	{"VPMON ADC", NULL, "VP"},
3556387f866SBrian Austin 	{"VBSTMON ADC", NULL, "VBST"},
3566387f866SBrian Austin 	{"IMON ADC", NULL, "ISENSE"},
3576387f866SBrian Austin 	{"VMON ADC", NULL, "VSENSE"},
3586387f866SBrian Austin 	{"SDOUT", NULL, "IMON ADC"},
3596387f866SBrian Austin 	{"SDOUT", NULL, "VMON ADC"},
3606387f866SBrian Austin 	{"SDOUT", NULL, "VBSTMON ADC"},
3616387f866SBrian Austin 	{"SDOUT", NULL, "VPMON ADC"},
3626387f866SBrian Austin 	{"AMP Capture", NULL, "SDOUT"},
3636387f866SBrian Austin 
3646387f866SBrian Austin 	{"SDIN", NULL, "AMP Playback"},
3656387f866SBrian Austin 	{"CLASS H", NULL, "SDIN"},
3666387f866SBrian Austin 	{"Main AMP", NULL, "CLASS H"},
3676387f866SBrian Austin 	{"SPK", NULL, "Main AMP"},
3686387f866SBrian Austin };
3696387f866SBrian Austin 
3706387f866SBrian Austin static int cs35l35_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
3716387f866SBrian Austin {
372*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
373*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
3746387f866SBrian Austin 
3756387f866SBrian Austin 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
3766387f866SBrian Austin 	case SND_SOC_DAIFMT_CBM_CFM:
3776387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
3786387f866SBrian Austin 				    CS35L35_MS_MASK, 1 << CS35L35_MS_SHIFT);
3796387f866SBrian Austin 		cs35l35->slave_mode = false;
3806387f866SBrian Austin 		break;
3816387f866SBrian Austin 	case SND_SOC_DAIFMT_CBS_CFS:
3826387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
3836387f866SBrian Austin 				    CS35L35_MS_MASK, 0 << CS35L35_MS_SHIFT);
3846387f866SBrian Austin 		cs35l35->slave_mode = true;
3856387f866SBrian Austin 		break;
3866387f866SBrian Austin 	default:
3876387f866SBrian Austin 		return -EINVAL;
3886387f866SBrian Austin 	}
3896387f866SBrian Austin 
3906387f866SBrian Austin 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
3916387f866SBrian Austin 	case SND_SOC_DAIFMT_I2S:
3926387f866SBrian Austin 		cs35l35->i2s_mode = true;
3936387f866SBrian Austin 		cs35l35->pdm_mode = false;
3946387f866SBrian Austin 		break;
3956387f866SBrian Austin 	case SND_SOC_DAIFMT_PDM:
3966387f866SBrian Austin 		cs35l35->pdm_mode = true;
3976387f866SBrian Austin 		cs35l35->i2s_mode = false;
3986387f866SBrian Austin 		break;
3996387f866SBrian Austin 	default:
4006387f866SBrian Austin 		return -EINVAL;
4016387f866SBrian Austin 	}
4026387f866SBrian Austin 
4036387f866SBrian Austin 	return 0;
4046387f866SBrian Austin }
4056387f866SBrian Austin 
4066387f866SBrian Austin struct cs35l35_sysclk_config {
4076387f866SBrian Austin 	int sysclk;
4086387f866SBrian Austin 	int srate;
4096387f866SBrian Austin 	u8 clk_cfg;
4106387f866SBrian Austin };
4116387f866SBrian Austin 
4126387f866SBrian Austin static struct cs35l35_sysclk_config cs35l35_clk_ctl[] = {
4136387f866SBrian Austin 
4146387f866SBrian Austin 	/* SYSCLK, Sample Rate, Serial Port Cfg */
4156387f866SBrian Austin 	{5644800, 44100, 0x00},
4166387f866SBrian Austin 	{5644800, 88200, 0x40},
4176387f866SBrian Austin 	{6144000, 48000, 0x10},
4186387f866SBrian Austin 	{6144000, 96000, 0x50},
4196387f866SBrian Austin 	{11289600, 44100, 0x01},
4206387f866SBrian Austin 	{11289600, 88200, 0x41},
4216387f866SBrian Austin 	{11289600, 176400, 0x81},
4226387f866SBrian Austin 	{12000000, 44100, 0x03},
4236387f866SBrian Austin 	{12000000, 48000, 0x13},
4246387f866SBrian Austin 	{12000000, 88200, 0x43},
4256387f866SBrian Austin 	{12000000, 96000, 0x53},
4266387f866SBrian Austin 	{12000000, 176400, 0x83},
4276387f866SBrian Austin 	{12000000, 192000, 0x93},
4286387f866SBrian Austin 	{12288000, 48000, 0x11},
4296387f866SBrian Austin 	{12288000, 96000, 0x51},
4306387f866SBrian Austin 	{12288000, 192000, 0x91},
4316387f866SBrian Austin 	{13000000, 44100, 0x07},
4326387f866SBrian Austin 	{13000000, 48000, 0x17},
4336387f866SBrian Austin 	{13000000, 88200, 0x47},
4346387f866SBrian Austin 	{13000000, 96000, 0x57},
4356387f866SBrian Austin 	{13000000, 176400, 0x87},
4366387f866SBrian Austin 	{13000000, 192000, 0x97},
4376387f866SBrian Austin 	{22579200, 44100, 0x02},
4386387f866SBrian Austin 	{22579200, 88200, 0x42},
4396387f866SBrian Austin 	{22579200, 176400, 0x82},
4406387f866SBrian Austin 	{24000000, 44100, 0x0B},
4416387f866SBrian Austin 	{24000000, 48000, 0x1B},
4426387f866SBrian Austin 	{24000000, 88200, 0x4B},
4436387f866SBrian Austin 	{24000000, 96000, 0x5B},
4446387f866SBrian Austin 	{24000000, 176400, 0x8B},
4456387f866SBrian Austin 	{24000000, 192000, 0x9B},
4466387f866SBrian Austin 	{24576000, 48000, 0x12},
4476387f866SBrian Austin 	{24576000, 96000, 0x52},
4486387f866SBrian Austin 	{24576000, 192000, 0x92},
4496387f866SBrian Austin 	{26000000, 44100, 0x0F},
4506387f866SBrian Austin 	{26000000, 48000, 0x1F},
4516387f866SBrian Austin 	{26000000, 88200, 0x4F},
4526387f866SBrian Austin 	{26000000, 96000, 0x5F},
4536387f866SBrian Austin 	{26000000, 176400, 0x8F},
4546387f866SBrian Austin 	{26000000, 192000, 0x9F},
4556387f866SBrian Austin };
4566387f866SBrian Austin 
4576387f866SBrian Austin static int cs35l35_get_clk_config(int sysclk, int srate)
4586387f866SBrian Austin {
4596387f866SBrian Austin 	int i;
4606387f866SBrian Austin 
4616387f866SBrian Austin 	for (i = 0; i < ARRAY_SIZE(cs35l35_clk_ctl); i++) {
4626387f866SBrian Austin 		if (cs35l35_clk_ctl[i].sysclk == sysclk &&
4636387f866SBrian Austin 			cs35l35_clk_ctl[i].srate == srate)
4646387f866SBrian Austin 			return cs35l35_clk_ctl[i].clk_cfg;
4656387f866SBrian Austin 	}
4666387f866SBrian Austin 	return -EINVAL;
4676387f866SBrian Austin }
4686387f866SBrian Austin 
4696387f866SBrian Austin static int cs35l35_hw_params(struct snd_pcm_substream *substream,
4706387f866SBrian Austin 				 struct snd_pcm_hw_params *params,
4716387f866SBrian Austin 				 struct snd_soc_dai *dai)
4726387f866SBrian Austin {
473*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
474*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
4756387f866SBrian Austin 	struct classh_cfg *classh = &cs35l35->pdata.classh_algo;
4766387f866SBrian Austin 	int srate = params_rate(params);
4776387f866SBrian Austin 	int ret = 0;
4786387f866SBrian Austin 	u8 sp_sclks;
4796387f866SBrian Austin 	int audin_format;
4806387f866SBrian Austin 	int errata_chk;
4816387f866SBrian Austin 
4826387f866SBrian Austin 	int clk_ctl = cs35l35_get_clk_config(cs35l35->sysclk, srate);
4836387f866SBrian Austin 
4846387f866SBrian Austin 	if (clk_ctl < 0) {
485*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Invalid CLK:Rate %d:%d\n",
4866387f866SBrian Austin 			cs35l35->sysclk, srate);
4876387f866SBrian Austin 		return -EINVAL;
4886387f866SBrian Austin 	}
4896387f866SBrian Austin 
4906387f866SBrian Austin 	ret = regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL2,
4916387f866SBrian Austin 			  CS35L35_CLK_CTL2_MASK, clk_ctl);
4926387f866SBrian Austin 	if (ret != 0) {
493*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Failed to set port config %d\n", ret);
4946387f866SBrian Austin 		return ret;
4956387f866SBrian Austin 	}
4966387f866SBrian Austin 
4976387f866SBrian Austin 	/*
4986387f866SBrian Austin 	 * Rev A0 Errata
4996387f866SBrian Austin 	 * When configured for the weak-drive detection path (CH_WKFET_DIS = 0)
5006387f866SBrian Austin 	 * the Class H algorithm does not enable weak-drive operation for
5016387f866SBrian Austin 	 * nonzero values of CH_WKFET_DELAY if SP_RATE = 01 or 10
5026387f866SBrian Austin 	 */
5036387f866SBrian Austin 	errata_chk = clk_ctl & CS35L35_SP_RATE_MASK;
5046387f866SBrian Austin 
5056387f866SBrian Austin 	if (classh->classh_wk_fet_disable == 0x00 &&
5066387f866SBrian Austin 		(errata_chk == 0x01 || errata_chk == 0x03)) {
5076387f866SBrian Austin 		ret = regmap_update_bits(cs35l35->regmap,
5086387f866SBrian Austin 					CS35L35_CLASS_H_FET_DRIVE_CTL,
5096387f866SBrian Austin 					CS35L35_CH_WKFET_DEL_MASK,
5106387f866SBrian Austin 					0 << CS35L35_CH_WKFET_DEL_SHIFT);
5116387f866SBrian Austin 		if (ret != 0) {
512*86c2eddfSKuninori Morimoto 			dev_err(component->dev, "Failed to set fet config %d\n",
5136387f866SBrian Austin 				ret);
5146387f866SBrian Austin 			return ret;
5156387f866SBrian Austin 		}
5166387f866SBrian Austin 	}
5176387f866SBrian Austin 
5186387f866SBrian Austin 	/*
5196387f866SBrian Austin 	 * You can pull more Monitor data from the SDOUT pin than going to SDIN
5206387f866SBrian Austin 	 * Just make sure your SCLK is fast enough to fill the frame
5216387f866SBrian Austin 	 */
5226387f866SBrian Austin 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5236387f866SBrian Austin 		switch (params_width(params)) {
5246387f866SBrian Austin 		case 8:
5256387f866SBrian Austin 			audin_format = CS35L35_SDIN_DEPTH_8;
5266387f866SBrian Austin 			break;
5276387f866SBrian Austin 		case 16:
5286387f866SBrian Austin 			audin_format = CS35L35_SDIN_DEPTH_16;
5296387f866SBrian Austin 			break;
5306387f866SBrian Austin 		case 24:
5316387f866SBrian Austin 			audin_format = CS35L35_SDIN_DEPTH_24;
5326387f866SBrian Austin 			break;
5336387f866SBrian Austin 		default:
534*86c2eddfSKuninori Morimoto 			dev_err(component->dev, "Unsupported Width %d\n",
5356387f866SBrian Austin 				params_width(params));
5366387f866SBrian Austin 			return -EINVAL;
5376387f866SBrian Austin 		}
5386387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap,
5396387f866SBrian Austin 				CS35L35_AUDIN_DEPTH_CTL,
5406387f866SBrian Austin 				CS35L35_AUDIN_DEPTH_MASK,
5416387f866SBrian Austin 				audin_format <<
5426387f866SBrian Austin 				CS35L35_AUDIN_DEPTH_SHIFT);
5436387f866SBrian Austin 		if (cs35l35->pdata.stereo) {
5446387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
5456387f866SBrian Austin 					CS35L35_AUDIN_DEPTH_CTL,
5466387f866SBrian Austin 					CS35L35_ADVIN_DEPTH_MASK,
5476387f866SBrian Austin 					audin_format <<
5486387f866SBrian Austin 					CS35L35_ADVIN_DEPTH_SHIFT);
5496387f866SBrian Austin 		}
5506387f866SBrian Austin 	}
5516387f866SBrian Austin 
5526387f866SBrian Austin 	if (cs35l35->i2s_mode) {
5536387f866SBrian Austin 		/* We have to take the SCLK to derive num sclks
5546387f866SBrian Austin 		 * to configure the CLOCK_CTL3 register correctly
5556387f866SBrian Austin 		 */
5566387f866SBrian Austin 		if ((cs35l35->sclk / srate) % 4) {
557*86c2eddfSKuninori Morimoto 			dev_err(component->dev, "Unsupported sclk/fs ratio %d:%d\n",
5586387f866SBrian Austin 					cs35l35->sclk, srate);
5596387f866SBrian Austin 			return -EINVAL;
5606387f866SBrian Austin 		}
5616387f866SBrian Austin 		sp_sclks = ((cs35l35->sclk / srate) / 4) - 1;
5626387f866SBrian Austin 
5636387f866SBrian Austin 		/* Only certain ratios are supported in I2S Slave Mode */
5646387f866SBrian Austin 		if (cs35l35->slave_mode) {
5656387f866SBrian Austin 			switch (sp_sclks) {
5666387f866SBrian Austin 			case CS35L35_SP_SCLKS_32FS:
5676387f866SBrian Austin 			case CS35L35_SP_SCLKS_48FS:
5686387f866SBrian Austin 			case CS35L35_SP_SCLKS_64FS:
5696387f866SBrian Austin 				break;
5706387f866SBrian Austin 			default:
571*86c2eddfSKuninori Morimoto 				dev_err(component->dev, "ratio not supported\n");
5726387f866SBrian Austin 				return -EINVAL;
573f3a612a6Skbuild test robot 			}
5746387f866SBrian Austin 		} else {
5756387f866SBrian Austin 			/* Only certain ratios supported in I2S MASTER Mode */
5766387f866SBrian Austin 			switch (sp_sclks) {
5776387f866SBrian Austin 			case CS35L35_SP_SCLKS_32FS:
5786387f866SBrian Austin 			case CS35L35_SP_SCLKS_64FS:
5796387f866SBrian Austin 				break;
5806387f866SBrian Austin 			default:
581*86c2eddfSKuninori Morimoto 				dev_err(component->dev, "ratio not supported\n");
5826387f866SBrian Austin 				return -EINVAL;
583f3a612a6Skbuild test robot 			}
5846387f866SBrian Austin 		}
5856387f866SBrian Austin 		ret = regmap_update_bits(cs35l35->regmap,
5866387f866SBrian Austin 					CS35L35_CLK_CTL3,
5876387f866SBrian Austin 					CS35L35_SP_SCLKS_MASK, sp_sclks <<
5886387f866SBrian Austin 					CS35L35_SP_SCLKS_SHIFT);
5896387f866SBrian Austin 		if (ret != 0) {
590*86c2eddfSKuninori Morimoto 			dev_err(component->dev, "Failed to set fsclk %d\n", ret);
5916387f866SBrian Austin 			return ret;
5926387f866SBrian Austin 		}
5936387f866SBrian Austin 	}
5946387f866SBrian Austin 
5956387f866SBrian Austin 	return ret;
5966387f866SBrian Austin }
5976387f866SBrian Austin 
5986387f866SBrian Austin static const unsigned int cs35l35_src_rates[] = {
5996387f866SBrian Austin 	44100, 48000, 88200, 96000, 176400, 192000
6006387f866SBrian Austin };
6016387f866SBrian Austin 
6026387f866SBrian Austin static const struct snd_pcm_hw_constraint_list cs35l35_constraints = {
6036387f866SBrian Austin 	.count  = ARRAY_SIZE(cs35l35_src_rates),
6046387f866SBrian Austin 	.list   = cs35l35_src_rates,
6056387f866SBrian Austin };
6066387f866SBrian Austin 
6076387f866SBrian Austin static int cs35l35_pcm_startup(struct snd_pcm_substream *substream,
6086387f866SBrian Austin 			       struct snd_soc_dai *dai)
6096387f866SBrian Austin {
610*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
611*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
6126387f866SBrian Austin 
6136387f866SBrian Austin 	if (!substream->runtime)
6146387f866SBrian Austin 		return 0;
6156387f866SBrian Austin 
6166387f866SBrian Austin 	snd_pcm_hw_constraint_list(substream->runtime, 0,
6176387f866SBrian Austin 				SNDRV_PCM_HW_PARAM_RATE, &cs35l35_constraints);
6186387f866SBrian Austin 
6196387f866SBrian Austin 	regmap_update_bits(cs35l35->regmap, CS35L35_AMP_INP_DRV_CTL,
6206387f866SBrian Austin 					CS35L35_PDM_MODE_MASK,
6216387f866SBrian Austin 					0 << CS35L35_PDM_MODE_SHIFT);
6226387f866SBrian Austin 
6236387f866SBrian Austin 	return 0;
6246387f866SBrian Austin }
6256387f866SBrian Austin 
6266387f866SBrian Austin static const unsigned int cs35l35_pdm_rates[] = {
6276387f866SBrian Austin 	44100, 48000, 88200, 96000
6286387f866SBrian Austin };
6296387f866SBrian Austin 
6306387f866SBrian Austin static const struct snd_pcm_hw_constraint_list cs35l35_pdm_constraints = {
6316387f866SBrian Austin 	.count  = ARRAY_SIZE(cs35l35_pdm_rates),
6326387f866SBrian Austin 	.list   = cs35l35_pdm_rates,
6336387f866SBrian Austin };
6346387f866SBrian Austin 
6356387f866SBrian Austin static int cs35l35_pdm_startup(struct snd_pcm_substream *substream,
6366387f866SBrian Austin 			       struct snd_soc_dai *dai)
6376387f866SBrian Austin {
638*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
639*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
6406387f866SBrian Austin 
6416387f866SBrian Austin 	if (!substream->runtime)
6426387f866SBrian Austin 		return 0;
6436387f866SBrian Austin 
6446387f866SBrian Austin 	snd_pcm_hw_constraint_list(substream->runtime, 0,
6456387f866SBrian Austin 				SNDRV_PCM_HW_PARAM_RATE,
6466387f866SBrian Austin 				&cs35l35_pdm_constraints);
6476387f866SBrian Austin 
6486387f866SBrian Austin 	regmap_update_bits(cs35l35->regmap, CS35L35_AMP_INP_DRV_CTL,
6496387f866SBrian Austin 					CS35L35_PDM_MODE_MASK,
6506387f866SBrian Austin 					1 << CS35L35_PDM_MODE_SHIFT);
6516387f866SBrian Austin 
6526387f866SBrian Austin 	return 0;
6536387f866SBrian Austin }
6546387f866SBrian Austin 
6556387f866SBrian Austin static int cs35l35_dai_set_sysclk(struct snd_soc_dai *dai,
6566387f866SBrian Austin 				int clk_id, unsigned int freq, int dir)
6576387f866SBrian Austin {
658*86c2eddfSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
659*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
6606387f866SBrian Austin 
6616387f866SBrian Austin 	/* Need the SCLK Frequency regardless of sysclk source for I2S */
6626387f866SBrian Austin 	cs35l35->sclk = freq;
6636387f866SBrian Austin 
6646387f866SBrian Austin 	return 0;
6656387f866SBrian Austin }
6666387f866SBrian Austin 
6676387f866SBrian Austin static const struct snd_soc_dai_ops cs35l35_ops = {
6686387f866SBrian Austin 	.startup = cs35l35_pcm_startup,
6696387f866SBrian Austin 	.set_fmt = cs35l35_set_dai_fmt,
6706387f866SBrian Austin 	.hw_params = cs35l35_hw_params,
6716387f866SBrian Austin 	.set_sysclk = cs35l35_dai_set_sysclk,
6726387f866SBrian Austin };
6736387f866SBrian Austin 
6746387f866SBrian Austin static const struct snd_soc_dai_ops cs35l35_pdm_ops = {
6756387f866SBrian Austin 	.startup = cs35l35_pdm_startup,
6766387f866SBrian Austin 	.set_fmt = cs35l35_set_dai_fmt,
6776387f866SBrian Austin 	.hw_params = cs35l35_hw_params,
6786387f866SBrian Austin };
6796387f866SBrian Austin 
6806387f866SBrian Austin static struct snd_soc_dai_driver cs35l35_dai[] = {
6816387f866SBrian Austin 	{
6826387f866SBrian Austin 		.name = "cs35l35-pcm",
6836387f866SBrian Austin 		.id = 0,
6846387f866SBrian Austin 		.playback = {
6856387f866SBrian Austin 			.stream_name = "AMP Playback",
6866387f866SBrian Austin 			.channels_min = 1,
6876387f866SBrian Austin 			.channels_max = 8,
6886387f866SBrian Austin 			.rates = SNDRV_PCM_RATE_KNOT,
6896387f866SBrian Austin 			.formats = CS35L35_FORMATS,
6906387f866SBrian Austin 		},
6916387f866SBrian Austin 		.capture = {
6926387f866SBrian Austin 			.stream_name = "AMP Capture",
6936387f866SBrian Austin 			.channels_min = 1,
6946387f866SBrian Austin 			.channels_max = 8,
6956387f866SBrian Austin 			.rates = SNDRV_PCM_RATE_KNOT,
6966387f866SBrian Austin 			.formats = CS35L35_FORMATS,
6976387f866SBrian Austin 		},
6986387f866SBrian Austin 		.ops = &cs35l35_ops,
6996387f866SBrian Austin 		.symmetric_rates = 1,
7006387f866SBrian Austin 	},
7016387f866SBrian Austin 	{
7026387f866SBrian Austin 		.name = "cs35l35-pdm",
7036387f866SBrian Austin 		.id = 1,
7046387f866SBrian Austin 		.playback = {
7056387f866SBrian Austin 			.stream_name = "PDM Playback",
7066387f866SBrian Austin 			.channels_min = 1,
7076387f866SBrian Austin 			.channels_max = 2,
7086387f866SBrian Austin 			.rates = SNDRV_PCM_RATE_KNOT,
7096387f866SBrian Austin 			.formats = CS35L35_FORMATS,
7106387f866SBrian Austin 		},
7116387f866SBrian Austin 		.ops = &cs35l35_pdm_ops,
7126387f866SBrian Austin 	},
7136387f866SBrian Austin };
7146387f866SBrian Austin 
715*86c2eddfSKuninori Morimoto static int cs35l35_component_set_sysclk(struct snd_soc_component *component,
7166387f866SBrian Austin 				int clk_id, int source, unsigned int freq,
7176387f866SBrian Austin 				int dir)
7186387f866SBrian Austin {
719*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
7206387f866SBrian Austin 	int clksrc;
7216387f866SBrian Austin 	int ret = 0;
7226387f866SBrian Austin 
7236387f866SBrian Austin 	switch (clk_id) {
7246387f866SBrian Austin 	case 0:
7256387f866SBrian Austin 		clksrc = CS35L35_CLK_SOURCE_MCLK;
7266387f866SBrian Austin 		break;
7276387f866SBrian Austin 	case 1:
7286387f866SBrian Austin 		clksrc = CS35L35_CLK_SOURCE_SCLK;
7296387f866SBrian Austin 		break;
7306387f866SBrian Austin 	case 2:
7316387f866SBrian Austin 		clksrc = CS35L35_CLK_SOURCE_PDM;
7326387f866SBrian Austin 		break;
7336387f866SBrian Austin 	default:
734*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Invalid CLK Source\n");
7356387f866SBrian Austin 		return -EINVAL;
736f3a612a6Skbuild test robot 	}
7376387f866SBrian Austin 
7386387f866SBrian Austin 	switch (freq) {
7396387f866SBrian Austin 	case 5644800:
7406387f866SBrian Austin 	case 6144000:
7416387f866SBrian Austin 	case 11289600:
7426387f866SBrian Austin 	case 12000000:
7436387f866SBrian Austin 	case 12288000:
7446387f866SBrian Austin 	case 13000000:
7456387f866SBrian Austin 	case 22579200:
7466387f866SBrian Austin 	case 24000000:
7476387f866SBrian Austin 	case 24576000:
7486387f866SBrian Austin 	case 26000000:
7496387f866SBrian Austin 		cs35l35->sysclk = freq;
7506387f866SBrian Austin 		break;
7516387f866SBrian Austin 	default:
752*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Invalid CLK Frequency Input : %d\n", freq);
7536387f866SBrian Austin 		return -EINVAL;
7546387f866SBrian Austin 	}
7556387f866SBrian Austin 
7566387f866SBrian Austin 	ret = regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
7576387f866SBrian Austin 				CS35L35_CLK_SOURCE_MASK,
7586387f866SBrian Austin 				clksrc << CS35L35_CLK_SOURCE_SHIFT);
7596387f866SBrian Austin 	if (ret != 0) {
760*86c2eddfSKuninori Morimoto 		dev_err(component->dev, "Failed to set sysclk %d\n", ret);
7616387f866SBrian Austin 		return ret;
7626387f866SBrian Austin 	}
7636387f866SBrian Austin 
7646387f866SBrian Austin 	return ret;
7656387f866SBrian Austin }
7666387f866SBrian Austin 
767b7c752d6SBrian Austin static int cs35l35_boost_inductor(struct cs35l35_private *cs35l35,
768b7c752d6SBrian Austin 				  int inductor)
769b7c752d6SBrian Austin {
770b7c752d6SBrian Austin 	struct regmap *regmap = cs35l35->regmap;
771b7c752d6SBrian Austin 	unsigned int bst_ipk = 0;
772b7c752d6SBrian Austin 
773b7c752d6SBrian Austin 	/*
774b7c752d6SBrian Austin 	 * Digital Boost Converter Configuration for feedback,
775b7c752d6SBrian Austin 	 * ramping, switching frequency, and estimation block seeding.
776b7c752d6SBrian Austin 	 */
777b7c752d6SBrian Austin 
778b7c752d6SBrian Austin 	regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
779b7c752d6SBrian Austin 			   CS35L35_BST_CONV_SWFREQ_MASK, 0x00);
780b7c752d6SBrian Austin 
781b7c752d6SBrian Austin 	regmap_read(regmap, CS35L35_BST_PEAK_I, &bst_ipk);
782b7c752d6SBrian Austin 	bst_ipk &= CS35L35_BST_IPK_MASK;
783b7c752d6SBrian Austin 
784b7c752d6SBrian Austin 	switch (inductor) {
785b7c752d6SBrian Austin 	case 1000: /* 1 uH */
786b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x24);
787b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x24);
788b7c752d6SBrian Austin 		regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
789b7c752d6SBrian Austin 				   CS35L35_BST_CONV_LBST_MASK, 0x00);
790b7c752d6SBrian Austin 
791b7c752d6SBrian Austin 		if (bst_ipk < 0x04)
792b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
793b7c752d6SBrian Austin 		else
794b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x4E);
795b7c752d6SBrian Austin 		break;
796b7c752d6SBrian Austin 	case 1200: /* 1.2 uH */
797b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x20);
798b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x20);
799b7c752d6SBrian Austin 		regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
800b7c752d6SBrian Austin 				   CS35L35_BST_CONV_LBST_MASK, 0x01);
801b7c752d6SBrian Austin 
802b7c752d6SBrian Austin 		if (bst_ipk < 0x04)
803b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
804b7c752d6SBrian Austin 		else
805b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x47);
806b7c752d6SBrian Austin 		break;
807b7c752d6SBrian Austin 	case 1500: /* 1.5uH */
808b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x20);
809b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x20);
810b7c752d6SBrian Austin 		regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
811b7c752d6SBrian Austin 				   CS35L35_BST_CONV_LBST_MASK, 0x02);
812b7c752d6SBrian Austin 
813b7c752d6SBrian Austin 		if (bst_ipk < 0x04)
814b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
815b7c752d6SBrian Austin 		else
816b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x3C);
817b7c752d6SBrian Austin 		break;
818b7c752d6SBrian Austin 	case 2200: /* 2.2uH */
819b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x19);
820b7c752d6SBrian Austin 		regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x25);
821b7c752d6SBrian Austin 		regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
822b7c752d6SBrian Austin 				   CS35L35_BST_CONV_LBST_MASK, 0x03);
823b7c752d6SBrian Austin 
824b7c752d6SBrian Austin 		if (bst_ipk < 0x04)
825b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
826b7c752d6SBrian Austin 		else
827b7c752d6SBrian Austin 			regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x23);
828b7c752d6SBrian Austin 		break;
829b7c752d6SBrian Austin 	default:
830b7c752d6SBrian Austin 		dev_err(cs35l35->dev, "Invalid Inductor Value %d uH\n",
831b7c752d6SBrian Austin 			inductor);
832b7c752d6SBrian Austin 		return -EINVAL;
833b7c752d6SBrian Austin 	}
834b7c752d6SBrian Austin 	return 0;
835b7c752d6SBrian Austin }
836b7c752d6SBrian Austin 
837*86c2eddfSKuninori Morimoto static int cs35l35_component_probe(struct snd_soc_component *component)
8386387f866SBrian Austin {
839*86c2eddfSKuninori Morimoto 	struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
8406387f866SBrian Austin 	struct classh_cfg *classh = &cs35l35->pdata.classh_algo;
8416387f866SBrian Austin 	struct monitor_cfg *monitor_config = &cs35l35->pdata.mon_cfg;
8426387f866SBrian Austin 	int ret;
8436387f866SBrian Austin 
8446387f866SBrian Austin 	/* Set Platform Data */
8456387f866SBrian Austin 	if (cs35l35->pdata.bst_vctl)
8466387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_BST_CVTR_V_CTL,
8476387f866SBrian Austin 				CS35L35_BST_CTL_MASK,
8486387f866SBrian Austin 				cs35l35->pdata.bst_vctl);
8496387f866SBrian Austin 
8506387f866SBrian Austin 	if (cs35l35->pdata.bst_ipk)
8516387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_BST_PEAK_I,
8526387f866SBrian Austin 				CS35L35_BST_IPK_MASK,
8536387f866SBrian Austin 				cs35l35->pdata.bst_ipk <<
8546387f866SBrian Austin 				CS35L35_BST_IPK_SHIFT);
8556387f866SBrian Austin 
856b7c752d6SBrian Austin 	ret = cs35l35_boost_inductor(cs35l35, cs35l35->pdata.boost_ind);
857b7c752d6SBrian Austin 	if (ret)
858b7c752d6SBrian Austin 		return ret;
859b7c752d6SBrian Austin 
8606387f866SBrian Austin 	if (cs35l35->pdata.gain_zc)
8616387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
8626387f866SBrian Austin 				CS35L35_AMP_GAIN_ZC_MASK,
8636387f866SBrian Austin 				cs35l35->pdata.gain_zc <<
8646387f866SBrian Austin 				CS35L35_AMP_GAIN_ZC_SHIFT);
8656387f866SBrian Austin 
8666387f866SBrian Austin 	if (cs35l35->pdata.aud_channel)
8676387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap,
8686387f866SBrian Austin 				CS35L35_AUDIN_RXLOC_CTL,
8696387f866SBrian Austin 				CS35L35_AUD_IN_LR_MASK,
8706387f866SBrian Austin 				cs35l35->pdata.aud_channel <<
8716387f866SBrian Austin 				CS35L35_AUD_IN_LR_SHIFT);
8726387f866SBrian Austin 
8736387f866SBrian Austin 	if (cs35l35->pdata.stereo) {
8746387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap,
8756387f866SBrian Austin 				CS35L35_ADVIN_RXLOC_CTL,
8766387f866SBrian Austin 				CS35L35_ADV_IN_LR_MASK,
8776387f866SBrian Austin 				cs35l35->pdata.adv_channel <<
8786387f866SBrian Austin 				CS35L35_ADV_IN_LR_SHIFT);
8796387f866SBrian Austin 		if (cs35l35->pdata.shared_bst)
8806387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap, CS35L35_CLASS_H_CTL,
8816387f866SBrian Austin 					CS35L35_CH_STEREO_MASK,
8826387f866SBrian Austin 					1 << CS35L35_CH_STEREO_SHIFT);
883*86c2eddfSKuninori Morimoto 		ret = snd_soc_add_component_controls(component, cs35l35_adv_controls,
8846387f866SBrian Austin 					ARRAY_SIZE(cs35l35_adv_controls));
8856387f866SBrian Austin 		if (ret)
8866387f866SBrian Austin 			return ret;
8876387f866SBrian Austin 	}
8886387f866SBrian Austin 
8896387f866SBrian Austin 	if (cs35l35->pdata.sp_drv_str)
8906387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
8916387f866SBrian Austin 				CS35L35_SP_DRV_MASK,
8926387f866SBrian Austin 				cs35l35->pdata.sp_drv_str <<
8936387f866SBrian Austin 				CS35L35_SP_DRV_SHIFT);
8948d45f2d2SCharles Keepax 	if (cs35l35->pdata.sp_drv_unused)
8958d45f2d2SCharles Keepax 		regmap_update_bits(cs35l35->regmap, CS35L35_SP_FMT_CTL3,
8968d45f2d2SCharles Keepax 				   CS35L35_SP_I2S_DRV_MASK,
8978d45f2d2SCharles Keepax 				   cs35l35->pdata.sp_drv_unused <<
8988d45f2d2SCharles Keepax 				   CS35L35_SP_I2S_DRV_SHIFT);
8996387f866SBrian Austin 
9006387f866SBrian Austin 	if (classh->classh_algo_enable) {
9016387f866SBrian Austin 		if (classh->classh_bst_override)
9026387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9036387f866SBrian Austin 					CS35L35_CLASS_H_CTL,
9046387f866SBrian Austin 					CS35L35_CH_BST_OVR_MASK,
9056387f866SBrian Austin 					classh->classh_bst_override <<
9066387f866SBrian Austin 					CS35L35_CH_BST_OVR_SHIFT);
9076387f866SBrian Austin 		if (classh->classh_bst_max_limit)
9086387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9096387f866SBrian Austin 					CS35L35_CLASS_H_CTL,
9106387f866SBrian Austin 					CS35L35_CH_BST_LIM_MASK,
9116387f866SBrian Austin 					classh->classh_bst_max_limit <<
9126387f866SBrian Austin 					CS35L35_CH_BST_LIM_SHIFT);
9136387f866SBrian Austin 		if (classh->classh_mem_depth)
9146387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9156387f866SBrian Austin 					CS35L35_CLASS_H_CTL,
9166387f866SBrian Austin 					CS35L35_CH_MEM_DEPTH_MASK,
9176387f866SBrian Austin 					classh->classh_mem_depth <<
9186387f866SBrian Austin 					CS35L35_CH_MEM_DEPTH_SHIFT);
9196387f866SBrian Austin 		if (classh->classh_headroom)
9206387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9216387f866SBrian Austin 					CS35L35_CLASS_H_HEADRM_CTL,
9226387f866SBrian Austin 					CS35L35_CH_HDRM_CTL_MASK,
9236387f866SBrian Austin 					classh->classh_headroom <<
9246387f866SBrian Austin 					CS35L35_CH_HDRM_CTL_SHIFT);
9256387f866SBrian Austin 		if (classh->classh_release_rate)
9266387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9276387f866SBrian Austin 					CS35L35_CLASS_H_RELEASE_RATE,
9286387f866SBrian Austin 					CS35L35_CH_REL_RATE_MASK,
9296387f866SBrian Austin 					classh->classh_release_rate <<
9306387f866SBrian Austin 					CS35L35_CH_REL_RATE_SHIFT);
9316387f866SBrian Austin 		if (classh->classh_wk_fet_disable)
9326387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9336387f866SBrian Austin 					CS35L35_CLASS_H_FET_DRIVE_CTL,
9346387f866SBrian Austin 					CS35L35_CH_WKFET_DIS_MASK,
9356387f866SBrian Austin 					classh->classh_wk_fet_disable <<
9366387f866SBrian Austin 					CS35L35_CH_WKFET_DIS_SHIFT);
9376387f866SBrian Austin 		if (classh->classh_wk_fet_delay)
9386387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9396387f866SBrian Austin 					CS35L35_CLASS_H_FET_DRIVE_CTL,
9406387f866SBrian Austin 					CS35L35_CH_WKFET_DEL_MASK,
9416387f866SBrian Austin 					classh->classh_wk_fet_delay <<
9426387f866SBrian Austin 					CS35L35_CH_WKFET_DEL_SHIFT);
9436387f866SBrian Austin 		if (classh->classh_wk_fet_thld)
9446387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9456387f866SBrian Austin 					CS35L35_CLASS_H_FET_DRIVE_CTL,
9466387f866SBrian Austin 					CS35L35_CH_WKFET_THLD_MASK,
9476387f866SBrian Austin 					classh->classh_wk_fet_thld <<
9486387f866SBrian Austin 					CS35L35_CH_WKFET_THLD_SHIFT);
9496387f866SBrian Austin 		if (classh->classh_vpch_auto)
9506387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9516387f866SBrian Austin 					CS35L35_CLASS_H_VP_CTL,
9526387f866SBrian Austin 					CS35L35_CH_VP_AUTO_MASK,
9536387f866SBrian Austin 					classh->classh_vpch_auto <<
9546387f866SBrian Austin 					CS35L35_CH_VP_AUTO_SHIFT);
9556387f866SBrian Austin 		if (classh->classh_vpch_rate)
9566387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9576387f866SBrian Austin 					CS35L35_CLASS_H_VP_CTL,
9586387f866SBrian Austin 					CS35L35_CH_VP_RATE_MASK,
9596387f866SBrian Austin 					classh->classh_vpch_rate <<
9606387f866SBrian Austin 					CS35L35_CH_VP_RATE_SHIFT);
9616387f866SBrian Austin 		if (classh->classh_vpch_man)
9626387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9636387f866SBrian Austin 					CS35L35_CLASS_H_VP_CTL,
9646387f866SBrian Austin 					CS35L35_CH_VP_MAN_MASK,
9656387f866SBrian Austin 					classh->classh_vpch_man <<
9666387f866SBrian Austin 					CS35L35_CH_VP_MAN_SHIFT);
9676387f866SBrian Austin 	}
9686387f866SBrian Austin 
9696387f866SBrian Austin 	if (monitor_config->is_present) {
9706387f866SBrian Austin 		if (monitor_config->vmon_specs) {
9716387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9726387f866SBrian Austin 					CS35L35_SPKMON_DEPTH_CTL,
9736387f866SBrian Austin 					CS35L35_VMON_DEPTH_MASK,
9746387f866SBrian Austin 					monitor_config->vmon_dpth <<
9756387f866SBrian Austin 					CS35L35_VMON_DEPTH_SHIFT);
9766387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9776387f866SBrian Austin 					CS35L35_VMON_TXLOC_CTL,
9786387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
9796387f866SBrian Austin 					monitor_config->vmon_loc <<
9806387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
9816387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9826387f866SBrian Austin 					CS35L35_VMON_TXLOC_CTL,
9836387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
9846387f866SBrian Austin 					monitor_config->vmon_frm <<
9856387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
9866387f866SBrian Austin 		}
9876387f866SBrian Austin 		if (monitor_config->imon_specs) {
9886387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9896387f866SBrian Austin 					CS35L35_SPKMON_DEPTH_CTL,
9906387f866SBrian Austin 					CS35L35_IMON_DEPTH_MASK,
9916387f866SBrian Austin 					monitor_config->imon_dpth <<
9926387f866SBrian Austin 					CS35L35_IMON_DEPTH_SHIFT);
9936387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9946387f866SBrian Austin 					CS35L35_IMON_TXLOC_CTL,
9956387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
9966387f866SBrian Austin 					monitor_config->imon_loc <<
9976387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
9986387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
9996387f866SBrian Austin 					CS35L35_IMON_TXLOC_CTL,
10006387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
10016387f866SBrian Austin 					monitor_config->imon_frm <<
10026387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
100306bdf385SCharles Keepax 			regmap_update_bits(cs35l35->regmap,
100406bdf385SCharles Keepax 					CS35L35_IMON_SCALE_CTL,
100506bdf385SCharles Keepax 					CS35L35_IMON_SCALE_MASK,
100606bdf385SCharles Keepax 					monitor_config->imon_scale <<
100706bdf385SCharles Keepax 					CS35L35_IMON_SCALE_SHIFT);
10086387f866SBrian Austin 		}
10096387f866SBrian Austin 		if (monitor_config->vpmon_specs) {
10106387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10116387f866SBrian Austin 					CS35L35_SUPMON_DEPTH_CTL,
10126387f866SBrian Austin 					CS35L35_VPMON_DEPTH_MASK,
10136387f866SBrian Austin 					monitor_config->vpmon_dpth <<
10146387f866SBrian Austin 					CS35L35_VPMON_DEPTH_SHIFT);
10156387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10166387f866SBrian Austin 					CS35L35_VPMON_TXLOC_CTL,
10176387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
10186387f866SBrian Austin 					monitor_config->vpmon_loc <<
10196387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
10206387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10216387f866SBrian Austin 					CS35L35_VPMON_TXLOC_CTL,
10226387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
10236387f866SBrian Austin 					monitor_config->vpmon_frm <<
10246387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
10256387f866SBrian Austin 		}
10266387f866SBrian Austin 		if (monitor_config->vbstmon_specs) {
10276387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10286387f866SBrian Austin 					CS35L35_SUPMON_DEPTH_CTL,
10296387f866SBrian Austin 					CS35L35_VBSTMON_DEPTH_MASK,
10306387f866SBrian Austin 					monitor_config->vpmon_dpth <<
10316387f866SBrian Austin 					CS35L35_VBSTMON_DEPTH_SHIFT);
10326387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10336387f866SBrian Austin 					CS35L35_VBSTMON_TXLOC_CTL,
10346387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
10356387f866SBrian Austin 					monitor_config->vbstmon_loc <<
10366387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
10376387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10386387f866SBrian Austin 					CS35L35_VBSTMON_TXLOC_CTL,
10396387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
10406387f866SBrian Austin 					monitor_config->vbstmon_frm <<
10416387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
10426387f866SBrian Austin 		}
10436387f866SBrian Austin 		if (monitor_config->vpbrstat_specs) {
10446387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10456387f866SBrian Austin 					CS35L35_SUPMON_DEPTH_CTL,
10466387f866SBrian Austin 					CS35L35_VPBRSTAT_DEPTH_MASK,
10476387f866SBrian Austin 					monitor_config->vpbrstat_dpth <<
10486387f866SBrian Austin 					CS35L35_VPBRSTAT_DEPTH_SHIFT);
10496387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10506387f866SBrian Austin 					CS35L35_VPBR_STATUS_TXLOC_CTL,
10516387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
10526387f866SBrian Austin 					monitor_config->vpbrstat_loc <<
10536387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
10546387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10556387f866SBrian Austin 					CS35L35_VPBR_STATUS_TXLOC_CTL,
10566387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
10576387f866SBrian Austin 					monitor_config->vpbrstat_frm <<
10586387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
10596387f866SBrian Austin 		}
10606387f866SBrian Austin 		if (monitor_config->zerofill_specs) {
10616387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10626387f866SBrian Austin 					CS35L35_SUPMON_DEPTH_CTL,
10636387f866SBrian Austin 					CS35L35_ZEROFILL_DEPTH_MASK,
10646387f866SBrian Austin 					monitor_config->zerofill_dpth <<
10656387f866SBrian Austin 					CS35L35_ZEROFILL_DEPTH_SHIFT);
10666387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10676387f866SBrian Austin 					CS35L35_ZERO_FILL_LOC_CTL,
10686387f866SBrian Austin 					CS35L35_MON_TXLOC_MASK,
10696387f866SBrian Austin 					monitor_config->zerofill_loc <<
10706387f866SBrian Austin 					CS35L35_MON_TXLOC_SHIFT);
10716387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
10726387f866SBrian Austin 					CS35L35_ZERO_FILL_LOC_CTL,
10736387f866SBrian Austin 					CS35L35_MON_FRM_MASK,
10746387f866SBrian Austin 					monitor_config->zerofill_frm <<
10756387f866SBrian Austin 					CS35L35_MON_FRM_SHIFT);
10766387f866SBrian Austin 		}
10776387f866SBrian Austin 	}
10786387f866SBrian Austin 
1079bfe41c67SDan Carpenter 	return 0;
10806387f866SBrian Austin }
10816387f866SBrian Austin 
1082*86c2eddfSKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_cs35l35 = {
1083*86c2eddfSKuninori Morimoto 	.probe			= cs35l35_component_probe,
1084*86c2eddfSKuninori Morimoto 	.set_sysclk		= cs35l35_component_set_sysclk,
10856387f866SBrian Austin 	.dapm_widgets		= cs35l35_dapm_widgets,
10866387f866SBrian Austin 	.num_dapm_widgets	= ARRAY_SIZE(cs35l35_dapm_widgets),
10876387f866SBrian Austin 	.dapm_routes		= cs35l35_audio_map,
10886387f866SBrian Austin 	.num_dapm_routes	= ARRAY_SIZE(cs35l35_audio_map),
10896387f866SBrian Austin 	.controls		= cs35l35_aud_controls,
10906387f866SBrian Austin 	.num_controls		= ARRAY_SIZE(cs35l35_aud_controls),
1091*86c2eddfSKuninori Morimoto 	.idle_bias_on		= 1,
1092*86c2eddfSKuninori Morimoto 	.use_pmdown_time	= 1,
1093*86c2eddfSKuninori Morimoto 	.endianness		= 1,
1094*86c2eddfSKuninori Morimoto 	.non_legacy_dai_naming	= 1,
10956387f866SBrian Austin };
10966387f866SBrian Austin 
10976387f866SBrian Austin static struct regmap_config cs35l35_regmap = {
10986387f866SBrian Austin 	.reg_bits = 8,
10996387f866SBrian Austin 	.val_bits = 8,
11006387f866SBrian Austin 
11016387f866SBrian Austin 	.max_register = CS35L35_MAX_REGISTER,
11026387f866SBrian Austin 	.reg_defaults = cs35l35_reg,
11036387f866SBrian Austin 	.num_reg_defaults = ARRAY_SIZE(cs35l35_reg),
11046387f866SBrian Austin 	.volatile_reg = cs35l35_volatile_register,
11056387f866SBrian Austin 	.readable_reg = cs35l35_readable_register,
11066387f866SBrian Austin 	.precious_reg = cs35l35_precious_register,
11076387f866SBrian Austin 	.cache_type = REGCACHE_RBTREE,
11086387f866SBrian Austin };
11096387f866SBrian Austin 
11106387f866SBrian Austin static irqreturn_t cs35l35_irq(int irq, void *data)
11116387f866SBrian Austin {
11126387f866SBrian Austin 	struct cs35l35_private *cs35l35 = data;
11136387f866SBrian Austin 	unsigned int sticky1, sticky2, sticky3, sticky4;
11146387f866SBrian Austin 	unsigned int mask1, mask2, mask3, mask4, current1;
11156387f866SBrian Austin 
11166387f866SBrian Austin 	/* ack the irq by reading all status registers */
11176387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_4, &sticky4);
11186387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_3, &sticky3);
11196387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_2, &sticky2);
11206387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_1, &sticky1);
11216387f866SBrian Austin 
11226387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_MASK_4, &mask4);
11236387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_MASK_3, &mask3);
11246387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_MASK_2, &mask2);
11256387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_MASK_1, &mask1);
11266387f866SBrian Austin 
11276387f866SBrian Austin 	/* Check to see if unmasked bits are active */
11286387f866SBrian Austin 	if (!(sticky1 & ~mask1) && !(sticky2 & ~mask2) && !(sticky3 & ~mask3)
11296387f866SBrian Austin 			&& !(sticky4 & ~mask4))
11306387f866SBrian Austin 		return IRQ_NONE;
11316387f866SBrian Austin 
11326387f866SBrian Austin 	if (sticky2 & CS35L35_PDN_DONE)
11336387f866SBrian Austin 		complete(&cs35l35->pdn_done);
11346387f866SBrian Austin 
11356387f866SBrian Austin 	/* read the current values */
11366387f866SBrian Austin 	regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_1, &current1);
11376387f866SBrian Austin 
11386387f866SBrian Austin 	/* handle the interrupts */
11396387f866SBrian Austin 	if (sticky1 & CS35L35_CAL_ERR) {
11405d3d0ad6SCharles Keepax 		dev_crit(cs35l35->dev, "Calibration Error\n");
11416387f866SBrian Austin 
11426387f866SBrian Austin 		/* error is no longer asserted; safe to reset */
11436387f866SBrian Austin 		if (!(current1 & CS35L35_CAL_ERR)) {
11446387f866SBrian Austin 			pr_debug("%s : Cal error release\n", __func__);
11456387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11466387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11476387f866SBrian Austin 					CS35L35_CAL_ERR_RLS, 0);
11486387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11496387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11506387f866SBrian Austin 					CS35L35_CAL_ERR_RLS,
11516387f866SBrian Austin 					CS35L35_CAL_ERR_RLS);
11526387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11536387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11546387f866SBrian Austin 					CS35L35_CAL_ERR_RLS, 0);
11556387f866SBrian Austin 		}
11566387f866SBrian Austin 	}
11576387f866SBrian Austin 
11586387f866SBrian Austin 	if (sticky1 & CS35L35_AMP_SHORT) {
11595d3d0ad6SCharles Keepax 		dev_crit(cs35l35->dev, "AMP Short Error\n");
11606387f866SBrian Austin 		/* error is no longer asserted; safe to reset */
11616387f866SBrian Austin 		if (!(current1 & CS35L35_AMP_SHORT)) {
11625d3d0ad6SCharles Keepax 			dev_dbg(cs35l35->dev, "Amp short error release\n");
11636387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11646387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11656387f866SBrian Austin 					CS35L35_SHORT_RLS, 0);
11666387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11676387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11686387f866SBrian Austin 					CS35L35_SHORT_RLS,
11696387f866SBrian Austin 					CS35L35_SHORT_RLS);
11706387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11716387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11726387f866SBrian Austin 					CS35L35_SHORT_RLS, 0);
11736387f866SBrian Austin 		}
11746387f866SBrian Austin 	}
11756387f866SBrian Austin 
11766387f866SBrian Austin 	if (sticky1 & CS35L35_OTW) {
11775d3d0ad6SCharles Keepax 		dev_warn(cs35l35->dev, "Over temperature warning\n");
11786387f866SBrian Austin 
11796387f866SBrian Austin 		/* error is no longer asserted; safe to reset */
11806387f866SBrian Austin 		if (!(current1 & CS35L35_OTW)) {
11815d3d0ad6SCharles Keepax 			dev_dbg(cs35l35->dev, "Over temperature warn release\n");
11826387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11836387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11846387f866SBrian Austin 					CS35L35_OTW_RLS, 0);
11856387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11866387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11876387f866SBrian Austin 					CS35L35_OTW_RLS,
11886387f866SBrian Austin 					CS35L35_OTW_RLS);
11896387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
11906387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
11916387f866SBrian Austin 					CS35L35_OTW_RLS, 0);
11926387f866SBrian Austin 		}
11936387f866SBrian Austin 	}
11946387f866SBrian Austin 
11956387f866SBrian Austin 	if (sticky1 & CS35L35_OTE) {
11965d3d0ad6SCharles Keepax 		dev_crit(cs35l35->dev, "Over temperature error\n");
11976387f866SBrian Austin 		/* error is no longer asserted; safe to reset */
11986387f866SBrian Austin 		if (!(current1 & CS35L35_OTE)) {
11995d3d0ad6SCharles Keepax 			dev_dbg(cs35l35->dev, "Over temperature error release\n");
12006387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
12016387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
12026387f866SBrian Austin 					CS35L35_OTE_RLS, 0);
12036387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
12046387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
12056387f866SBrian Austin 					CS35L35_OTE_RLS,
12066387f866SBrian Austin 					CS35L35_OTE_RLS);
12076387f866SBrian Austin 			regmap_update_bits(cs35l35->regmap,
12086387f866SBrian Austin 					CS35L35_PROT_RELEASE_CTL,
12096387f866SBrian Austin 					CS35L35_OTE_RLS, 0);
12106387f866SBrian Austin 		}
12116387f866SBrian Austin 	}
12126387f866SBrian Austin 
12136387f866SBrian Austin 	if (sticky3 & CS35L35_BST_HIGH) {
12145d3d0ad6SCharles Keepax 		dev_crit(cs35l35->dev, "VBST error: powering off!\n");
12156387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
12166387f866SBrian Austin 			CS35L35_PDN_AMP, CS35L35_PDN_AMP);
12176387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
12186387f866SBrian Austin 			CS35L35_PDN_ALL, CS35L35_PDN_ALL);
12196387f866SBrian Austin 	}
12206387f866SBrian Austin 
12216387f866SBrian Austin 	if (sticky3 & CS35L35_LBST_SHORT) {
12225d3d0ad6SCharles Keepax 		dev_crit(cs35l35->dev, "LBST error: powering off!\n");
12236387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
12246387f866SBrian Austin 			CS35L35_PDN_AMP, CS35L35_PDN_AMP);
12256387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
12266387f866SBrian Austin 			CS35L35_PDN_ALL, CS35L35_PDN_ALL);
12276387f866SBrian Austin 	}
12286387f866SBrian Austin 
12296387f866SBrian Austin 	if (sticky2 & CS35L35_VPBR_ERR)
12305d3d0ad6SCharles Keepax 		dev_dbg(cs35l35->dev, "Error: Reactive Brownout\n");
12316387f866SBrian Austin 
12326387f866SBrian Austin 	if (sticky4 & CS35L35_VMON_OVFL)
12335d3d0ad6SCharles Keepax 		dev_dbg(cs35l35->dev, "Error: VMON overflow\n");
12346387f866SBrian Austin 
12356387f866SBrian Austin 	if (sticky4 & CS35L35_IMON_OVFL)
12365d3d0ad6SCharles Keepax 		dev_dbg(cs35l35->dev, "Error: IMON overflow\n");
12376387f866SBrian Austin 
12386387f866SBrian Austin 	return IRQ_HANDLED;
12396387f866SBrian Austin }
12406387f866SBrian Austin 
12416387f866SBrian Austin 
12426387f866SBrian Austin static int cs35l35_handle_of_data(struct i2c_client *i2c_client,
12436387f866SBrian Austin 				struct cs35l35_platform_data *pdata)
12446387f866SBrian Austin {
12456387f866SBrian Austin 	struct device_node *np = i2c_client->dev.of_node;
12466387f866SBrian Austin 	struct device_node *classh, *signal_format;
12476387f866SBrian Austin 	struct classh_cfg *classh_config = &pdata->classh_algo;
12486387f866SBrian Austin 	struct monitor_cfg *monitor_config = &pdata->mon_cfg;
12496387f866SBrian Austin 	unsigned int val32 = 0;
125006bdf385SCharles Keepax 	u8 monitor_array[4];
125106bdf385SCharles Keepax 	const int imon_array_size = ARRAY_SIZE(monitor_array);
125206bdf385SCharles Keepax 	const int mon_array_size = imon_array_size - 1;
12536387f866SBrian Austin 	int ret = 0;
12546387f866SBrian Austin 
12556387f866SBrian Austin 	if (!np)
12566387f866SBrian Austin 		return 0;
12576387f866SBrian Austin 
12586387f866SBrian Austin 	pdata->bst_pdn_fet_on = of_property_read_bool(np,
12596387f866SBrian Austin 					"cirrus,boost-pdn-fet-on");
12606387f866SBrian Austin 
12616387f866SBrian Austin 	ret = of_property_read_u32(np, "cirrus,boost-ctl-millivolt", &val32);
12626387f866SBrian Austin 	if (ret >= 0) {
12636387f866SBrian Austin 		if (val32 < 2600 || val32 > 9000) {
12646387f866SBrian Austin 			dev_err(&i2c_client->dev,
12656387f866SBrian Austin 				"Invalid Boost Voltage %d mV\n", val32);
12666387f866SBrian Austin 			return -EINVAL;
12676387f866SBrian Austin 		}
12686387f866SBrian Austin 		pdata->bst_vctl = ((val32 - 2600) / 100) + 1;
12696387f866SBrian Austin 	}
12706387f866SBrian Austin 
12716387f866SBrian Austin 	ret = of_property_read_u32(np, "cirrus,boost-peak-milliamp", &val32);
12726387f866SBrian Austin 	if (ret >= 0) {
12736387f866SBrian Austin 		if (val32 < 1680 || val32 > 4480) {
12746387f866SBrian Austin 			dev_err(&i2c_client->dev,
12756387f866SBrian Austin 				"Invalid Boost Peak Current %u mA\n", val32);
12766387f866SBrian Austin 			return -EINVAL;
12776387f866SBrian Austin 		}
12786387f866SBrian Austin 
1279486fb959SCharles Keepax 		pdata->bst_ipk = ((val32 - 1680) / 110) | CS35L35_VALID_PDATA;
12806387f866SBrian Austin 	}
12816387f866SBrian Austin 
1282b7c752d6SBrian Austin 	ret = of_property_read_u32(np, "cirrus,boost-ind-nanohenry", &val32);
1283b7c752d6SBrian Austin 	if (ret >= 0) {
1284b7c752d6SBrian Austin 		pdata->boost_ind = val32;
1285b7c752d6SBrian Austin 	} else {
1286b7c752d6SBrian Austin 		dev_err(&i2c_client->dev, "Inductor not specified.\n");
1287b7c752d6SBrian Austin 		return -EINVAL;
12886387f866SBrian Austin 	}
12896387f866SBrian Austin 
12906387f866SBrian Austin 	if (of_property_read_u32(np, "cirrus,sp-drv-strength", &val32) >= 0)
12916387f866SBrian Austin 		pdata->sp_drv_str = val32;
12928d45f2d2SCharles Keepax 	if (of_property_read_u32(np, "cirrus,sp-drv-unused", &val32) >= 0)
12938d45f2d2SCharles Keepax 		pdata->sp_drv_unused = val32 | CS35L35_VALID_PDATA;
12946387f866SBrian Austin 
12956387f866SBrian Austin 	pdata->stereo = of_property_read_bool(np, "cirrus,stereo-config");
12966387f866SBrian Austin 
12976387f866SBrian Austin 	if (pdata->stereo) {
12986387f866SBrian Austin 		ret = of_property_read_u32(np, "cirrus,audio-channel", &val32);
12996387f866SBrian Austin 		if (ret >= 0)
13006387f866SBrian Austin 			pdata->aud_channel = val32;
13016387f866SBrian Austin 
13026387f866SBrian Austin 		ret = of_property_read_u32(np, "cirrus,advisory-channel",
13036387f866SBrian Austin 					   &val32);
13046387f866SBrian Austin 		if (ret >= 0)
13056387f866SBrian Austin 			pdata->adv_channel = val32;
13066387f866SBrian Austin 
13076387f866SBrian Austin 		pdata->shared_bst = of_property_read_bool(np,
13086387f866SBrian Austin 						"cirrus,shared-boost");
13096387f866SBrian Austin 	}
13106387f866SBrian Austin 
131177b329d1SCharles Keepax 	pdata->ext_bst = of_property_read_bool(np, "cirrus,external-boost");
131277b329d1SCharles Keepax 
13136387f866SBrian Austin 	pdata->gain_zc = of_property_read_bool(np, "cirrus,amp-gain-zc");
13146387f866SBrian Austin 
13156387f866SBrian Austin 	classh = of_get_child_by_name(np, "cirrus,classh-internal-algo");
13166387f866SBrian Austin 	classh_config->classh_algo_enable = classh ? true : false;
13176387f866SBrian Austin 
13186387f866SBrian Austin 	if (classh_config->classh_algo_enable) {
13196387f866SBrian Austin 		classh_config->classh_bst_override =
13206387f866SBrian Austin 			of_property_read_bool(np, "cirrus,classh-bst-overide");
13216387f866SBrian Austin 
13226387f866SBrian Austin 		ret = of_property_read_u32(classh,
13236387f866SBrian Austin 					   "cirrus,classh-bst-max-limit",
13246387f866SBrian Austin 					   &val32);
13256387f866SBrian Austin 		if (ret >= 0) {
13266387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13276387f866SBrian Austin 			classh_config->classh_bst_max_limit = val32;
13286387f866SBrian Austin 		}
13296387f866SBrian Austin 
13306387f866SBrian Austin 		ret = of_property_read_u32(classh,
13316387f866SBrian Austin 					   "cirrus,classh-bst-max-limit",
13326387f866SBrian Austin 					   &val32);
13336387f866SBrian Austin 		if (ret >= 0) {
13346387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13356387f866SBrian Austin 			classh_config->classh_bst_max_limit = val32;
13366387f866SBrian Austin 		}
13376387f866SBrian Austin 
13386387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-mem-depth",
13396387f866SBrian Austin 					   &val32);
13406387f866SBrian Austin 		if (ret >= 0) {
13416387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13426387f866SBrian Austin 			classh_config->classh_mem_depth = val32;
13436387f866SBrian Austin 		}
13446387f866SBrian Austin 
13456387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-release-rate",
13466387f866SBrian Austin 					   &val32);
13476387f866SBrian Austin 		if (ret >= 0)
13486387f866SBrian Austin 			classh_config->classh_release_rate = val32;
13496387f866SBrian Austin 
13506387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-headroom",
13516387f866SBrian Austin 					   &val32);
13526387f866SBrian Austin 		if (ret >= 0) {
13536387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13546387f866SBrian Austin 			classh_config->classh_headroom = val32;
13556387f866SBrian Austin 		}
13566387f866SBrian Austin 
13576387f866SBrian Austin 		ret = of_property_read_u32(classh,
13586387f866SBrian Austin 					   "cirrus,classh-wk-fet-disable",
13596387f866SBrian Austin 					   &val32);
13606387f866SBrian Austin 		if (ret >= 0)
13616387f866SBrian Austin 			classh_config->classh_wk_fet_disable = val32;
13626387f866SBrian Austin 
13636387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-wk-fet-delay",
13646387f866SBrian Austin 					   &val32);
13656387f866SBrian Austin 		if (ret >= 0) {
13666387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13676387f866SBrian Austin 			classh_config->classh_wk_fet_delay = val32;
13686387f866SBrian Austin 		}
13696387f866SBrian Austin 
13706387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-wk-fet-thld",
13716387f866SBrian Austin 					   &val32);
13726387f866SBrian Austin 		if (ret >= 0)
13736387f866SBrian Austin 			classh_config->classh_wk_fet_thld = val32;
13746387f866SBrian Austin 
13756387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-vpch-auto",
13766387f866SBrian Austin 					   &val32);
13776387f866SBrian Austin 		if (ret >= 0) {
13786387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13796387f866SBrian Austin 			classh_config->classh_vpch_auto = val32;
13806387f866SBrian Austin 		}
13816387f866SBrian Austin 
13826387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-vpch-rate",
13836387f866SBrian Austin 					   &val32);
13846387f866SBrian Austin 		if (ret >= 0) {
13856387f866SBrian Austin 			val32 |= CS35L35_VALID_PDATA;
13866387f866SBrian Austin 			classh_config->classh_vpch_rate = val32;
13876387f866SBrian Austin 		}
13886387f866SBrian Austin 
13896387f866SBrian Austin 		ret = of_property_read_u32(classh, "cirrus,classh-vpch-man",
13906387f866SBrian Austin 					   &val32);
13916387f866SBrian Austin 		if (ret >= 0)
13926387f866SBrian Austin 			classh_config->classh_vpch_man = val32;
13936387f866SBrian Austin 	}
13946387f866SBrian Austin 	of_node_put(classh);
13956387f866SBrian Austin 
13966387f866SBrian Austin 	/* frame depth location */
13976387f866SBrian Austin 	signal_format = of_get_child_by_name(np, "cirrus,monitor-signal-format");
13986387f866SBrian Austin 	monitor_config->is_present = signal_format ? true : false;
13996387f866SBrian Austin 	if (monitor_config->is_present) {
14006387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,imon",
140106bdf385SCharles Keepax 						monitor_array, imon_array_size);
14026387f866SBrian Austin 		if (!ret) {
14036387f866SBrian Austin 			monitor_config->imon_specs = true;
14046387f866SBrian Austin 			monitor_config->imon_dpth = monitor_array[0];
14056387f866SBrian Austin 			monitor_config->imon_loc = monitor_array[1];
14066387f866SBrian Austin 			monitor_config->imon_frm = monitor_array[2];
140706bdf385SCharles Keepax 			monitor_config->imon_scale = monitor_array[3];
14086387f866SBrian Austin 		}
14096387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,vmon",
141006bdf385SCharles Keepax 						monitor_array, mon_array_size);
14116387f866SBrian Austin 		if (!ret) {
14126387f866SBrian Austin 			monitor_config->vmon_specs = true;
14136387f866SBrian Austin 			monitor_config->vmon_dpth = monitor_array[0];
14146387f866SBrian Austin 			monitor_config->vmon_loc = monitor_array[1];
14156387f866SBrian Austin 			monitor_config->vmon_frm = monitor_array[2];
14166387f866SBrian Austin 		}
14176387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,vpmon",
141806bdf385SCharles Keepax 						monitor_array, mon_array_size);
14196387f866SBrian Austin 		if (!ret) {
14206387f866SBrian Austin 			monitor_config->vpmon_specs = true;
14216387f866SBrian Austin 			monitor_config->vpmon_dpth = monitor_array[0];
14226387f866SBrian Austin 			monitor_config->vpmon_loc = monitor_array[1];
14236387f866SBrian Austin 			monitor_config->vpmon_frm = monitor_array[2];
14246387f866SBrian Austin 		}
14256387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,vbstmon",
142606bdf385SCharles Keepax 						monitor_array, mon_array_size);
14276387f866SBrian Austin 		if (!ret) {
14286387f866SBrian Austin 			monitor_config->vbstmon_specs = true;
14296387f866SBrian Austin 			monitor_config->vbstmon_dpth = monitor_array[0];
14306387f866SBrian Austin 			monitor_config->vbstmon_loc = monitor_array[1];
14316387f866SBrian Austin 			monitor_config->vbstmon_frm = monitor_array[2];
14326387f866SBrian Austin 		}
14336387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,vpbrstat",
143406bdf385SCharles Keepax 						monitor_array, mon_array_size);
14356387f866SBrian Austin 		if (!ret) {
14366387f866SBrian Austin 			monitor_config->vpbrstat_specs = true;
14376387f866SBrian Austin 			monitor_config->vpbrstat_dpth = monitor_array[0];
14386387f866SBrian Austin 			monitor_config->vpbrstat_loc = monitor_array[1];
14396387f866SBrian Austin 			monitor_config->vpbrstat_frm = monitor_array[2];
14406387f866SBrian Austin 		}
14416387f866SBrian Austin 		ret = of_property_read_u8_array(signal_format, "cirrus,zerofill",
144206bdf385SCharles Keepax 						monitor_array, mon_array_size);
14436387f866SBrian Austin 		if (!ret) {
14446387f866SBrian Austin 			monitor_config->zerofill_specs = true;
14456387f866SBrian Austin 			monitor_config->zerofill_dpth = monitor_array[0];
14466387f866SBrian Austin 			monitor_config->zerofill_loc = monitor_array[1];
14476387f866SBrian Austin 			monitor_config->zerofill_frm = monitor_array[2];
14486387f866SBrian Austin 		}
14496387f866SBrian Austin 	}
14506387f866SBrian Austin 	of_node_put(signal_format);
14516387f866SBrian Austin 
14526387f866SBrian Austin 	return 0;
14536387f866SBrian Austin }
14546387f866SBrian Austin 
14556387f866SBrian Austin /* Errata Rev A0 */
14566387f866SBrian Austin static const struct reg_sequence cs35l35_errata_patch[] = {
14576387f866SBrian Austin 
14586387f866SBrian Austin 	{ 0x7F, 0x99 },
14596387f866SBrian Austin 	{ 0x00, 0x99 },
14606387f866SBrian Austin 	{ 0x52, 0x22 },
14616387f866SBrian Austin 	{ 0x04, 0x14 },
14626387f866SBrian Austin 	{ 0x6D, 0x44 },
14636387f866SBrian Austin 	{ 0x24, 0x10 },
14646387f866SBrian Austin 	{ 0x58, 0xC4 },
14656387f866SBrian Austin 	{ 0x00, 0x98 },
14666387f866SBrian Austin 	{ 0x18, 0x08 },
14676387f866SBrian Austin 	{ 0x00, 0x00 },
14686387f866SBrian Austin 	{ 0x7F, 0x00 },
14696387f866SBrian Austin };
14706387f866SBrian Austin 
14716387f866SBrian Austin static int cs35l35_i2c_probe(struct i2c_client *i2c_client,
14726387f866SBrian Austin 			      const struct i2c_device_id *id)
14736387f866SBrian Austin {
14746387f866SBrian Austin 	struct cs35l35_private *cs35l35;
14751f758cd9SCharles Keepax 	struct device *dev = &i2c_client->dev;
14761f758cd9SCharles Keepax 	struct cs35l35_platform_data *pdata = dev_get_platdata(dev);
14776387f866SBrian Austin 	int i;
14786387f866SBrian Austin 	int ret;
14796387f866SBrian Austin 	unsigned int devid = 0;
14806387f866SBrian Austin 	unsigned int reg;
14816387f866SBrian Austin 
14821f758cd9SCharles Keepax 	cs35l35 = devm_kzalloc(dev, sizeof(struct cs35l35_private), GFP_KERNEL);
14836387f866SBrian Austin 	if (!cs35l35)
14846387f866SBrian Austin 		return -ENOMEM;
14856387f866SBrian Austin 
14865d3d0ad6SCharles Keepax 	cs35l35->dev = dev;
14875d3d0ad6SCharles Keepax 
14886387f866SBrian Austin 	i2c_set_clientdata(i2c_client, cs35l35);
14896387f866SBrian Austin 	cs35l35->regmap = devm_regmap_init_i2c(i2c_client, &cs35l35_regmap);
14906387f866SBrian Austin 	if (IS_ERR(cs35l35->regmap)) {
14916387f866SBrian Austin 		ret = PTR_ERR(cs35l35->regmap);
14921f758cd9SCharles Keepax 		dev_err(dev, "regmap_init() failed: %d\n", ret);
14936387f866SBrian Austin 		goto err;
14946387f866SBrian Austin 	}
14956387f866SBrian Austin 
14966387f866SBrian Austin 	for (i = 0; i < ARRAY_SIZE(cs35l35_supplies); i++)
14976387f866SBrian Austin 		cs35l35->supplies[i].supply = cs35l35_supplies[i];
149803ff570cSColin Ian King 
14996387f866SBrian Austin 	cs35l35->num_supplies = ARRAY_SIZE(cs35l35_supplies);
15006387f866SBrian Austin 
15011f758cd9SCharles Keepax 	ret = devm_regulator_bulk_get(dev, cs35l35->num_supplies,
15026387f866SBrian Austin 				      cs35l35->supplies);
15036387f866SBrian Austin 	if (ret != 0) {
15041f758cd9SCharles Keepax 		dev_err(dev, "Failed to request core supplies: %d\n", ret);
15056387f866SBrian Austin 		return ret;
15066387f866SBrian Austin 	}
15076387f866SBrian Austin 
15086387f866SBrian Austin 	if (pdata) {
15096387f866SBrian Austin 		cs35l35->pdata = *pdata;
15106387f866SBrian Austin 	} else {
15111f758cd9SCharles Keepax 		pdata = devm_kzalloc(dev, sizeof(struct cs35l35_platform_data),
15126387f866SBrian Austin 				     GFP_KERNEL);
15136387f866SBrian Austin 		if (!pdata)
15146387f866SBrian Austin 			return -ENOMEM;
15156387f866SBrian Austin 		if (i2c_client->dev.of_node) {
15166387f866SBrian Austin 			ret = cs35l35_handle_of_data(i2c_client, pdata);
15176387f866SBrian Austin 			if (ret != 0)
15186387f866SBrian Austin 				return ret;
15196387f866SBrian Austin 
15206387f866SBrian Austin 		}
15216387f866SBrian Austin 		cs35l35->pdata = *pdata;
15226387f866SBrian Austin 	}
15236387f866SBrian Austin 
15246387f866SBrian Austin 	ret = regulator_bulk_enable(cs35l35->num_supplies,
15256387f866SBrian Austin 					cs35l35->supplies);
15266387f866SBrian Austin 	if (ret != 0) {
15271f758cd9SCharles Keepax 		dev_err(dev, "Failed to enable core supplies: %d\n", ret);
15286387f866SBrian Austin 		return ret;
15296387f866SBrian Austin 	}
15306387f866SBrian Austin 
15316387f866SBrian Austin 	/* returning NULL can be valid if in stereo mode */
15321f758cd9SCharles Keepax 	cs35l35->reset_gpio = devm_gpiod_get_optional(dev, "reset",
15331f758cd9SCharles Keepax 						      GPIOD_OUT_LOW);
15346387f866SBrian Austin 	if (IS_ERR(cs35l35->reset_gpio)) {
15356387f866SBrian Austin 		ret = PTR_ERR(cs35l35->reset_gpio);
15368e71321dSCharles Keepax 		cs35l35->reset_gpio = NULL;
15376387f866SBrian Austin 		if (ret == -EBUSY) {
15381f758cd9SCharles Keepax 			dev_info(dev,
15396387f866SBrian Austin 				 "Reset line busy, assuming shared reset\n");
15406387f866SBrian Austin 		} else {
15411f758cd9SCharles Keepax 			dev_err(dev, "Failed to get reset GPIO: %d\n", ret);
15426387f866SBrian Austin 			goto err;
15436387f866SBrian Austin 		}
15446387f866SBrian Austin 	}
15456387f866SBrian Austin 
1546dc43f46aSCharles Keepax 	cs35l35_reset(cs35l35);
15476387f866SBrian Austin 
15486387f866SBrian Austin 	init_completion(&cs35l35->pdn_done);
15496387f866SBrian Austin 
15501f758cd9SCharles Keepax 	ret = devm_request_threaded_irq(dev, i2c_client->irq, NULL, cs35l35_irq,
1551bf5043d6SCharles Keepax 					IRQF_ONESHOT | IRQF_TRIGGER_LOW |
1552bf5043d6SCharles Keepax 					IRQF_SHARED, "cs35l35", cs35l35);
15536387f866SBrian Austin 	if (ret != 0) {
15541f758cd9SCharles Keepax 		dev_err(dev, "Failed to request IRQ: %d\n", ret);
15556387f866SBrian Austin 		goto err;
15566387f866SBrian Austin 	}
15576387f866SBrian Austin 	/* initialize codec */
15586387f866SBrian Austin 	ret = regmap_read(cs35l35->regmap, CS35L35_DEVID_AB, &reg);
15596387f866SBrian Austin 
15606387f866SBrian Austin 	devid = (reg & 0xFF) << 12;
15616387f866SBrian Austin 	ret = regmap_read(cs35l35->regmap, CS35L35_DEVID_CD, &reg);
15626387f866SBrian Austin 	devid |= (reg & 0xFF) << 4;
15636387f866SBrian Austin 	ret = regmap_read(cs35l35->regmap, CS35L35_DEVID_E, &reg);
15646387f866SBrian Austin 	devid |= (reg & 0xF0) >> 4;
15656387f866SBrian Austin 
15666387f866SBrian Austin 	if (devid != CS35L35_CHIP_ID) {
15671f758cd9SCharles Keepax 		dev_err(dev, "CS35L35 Device ID (%X). Expected ID %X\n",
15686387f866SBrian Austin 			devid, CS35L35_CHIP_ID);
15696387f866SBrian Austin 		ret = -ENODEV;
15706387f866SBrian Austin 		goto err;
15716387f866SBrian Austin 	}
15726387f866SBrian Austin 
15736387f866SBrian Austin 	ret = regmap_read(cs35l35->regmap, CS35L35_REV_ID, &reg);
15746387f866SBrian Austin 	if (ret < 0) {
15751f758cd9SCharles Keepax 		dev_err(dev, "Get Revision ID failed: %d\n", ret);
15766387f866SBrian Austin 		goto err;
15776387f866SBrian Austin 	}
15786387f866SBrian Austin 
15796387f866SBrian Austin 	ret = regmap_register_patch(cs35l35->regmap, cs35l35_errata_patch,
15806387f866SBrian Austin 				    ARRAY_SIZE(cs35l35_errata_patch));
15816387f866SBrian Austin 	if (ret < 0) {
15821f758cd9SCharles Keepax 		dev_err(dev, "Failed to apply errata patch: %d\n", ret);
15836387f866SBrian Austin 		goto err;
15846387f866SBrian Austin 	}
15856387f866SBrian Austin 
15861f758cd9SCharles Keepax 	dev_info(dev, "Cirrus Logic CS35L35 (%x), Revision: %02X\n",
158782875163SAxel Lin 		 devid, reg & 0xFF);
15886387f866SBrian Austin 
15896387f866SBrian Austin 	/* Set the INT Masks for critical errors */
15906387f866SBrian Austin 	regmap_write(cs35l35->regmap, CS35L35_INT_MASK_1,
15916387f866SBrian Austin 				CS35L35_INT1_CRIT_MASK);
15926387f866SBrian Austin 	regmap_write(cs35l35->regmap, CS35L35_INT_MASK_2,
15936387f866SBrian Austin 				CS35L35_INT2_CRIT_MASK);
15946387f866SBrian Austin 	regmap_write(cs35l35->regmap, CS35L35_INT_MASK_3,
15956387f866SBrian Austin 				CS35L35_INT3_CRIT_MASK);
15966387f866SBrian Austin 	regmap_write(cs35l35->regmap, CS35L35_INT_MASK_4,
15976387f866SBrian Austin 				CS35L35_INT4_CRIT_MASK);
15986387f866SBrian Austin 
15996387f866SBrian Austin 	regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
16006387f866SBrian Austin 			CS35L35_PWR2_PDN_MASK,
16016387f866SBrian Austin 			CS35L35_PWR2_PDN_MASK);
16026387f866SBrian Austin 
16036387f866SBrian Austin 	if (cs35l35->pdata.bst_pdn_fet_on)
16046387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
16056387f866SBrian Austin 					CS35L35_PDN_BST_MASK,
16066387f866SBrian Austin 					1 << CS35L35_PDN_BST_FETON_SHIFT);
16076387f866SBrian Austin 	else
16086387f866SBrian Austin 		regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
16096387f866SBrian Austin 					CS35L35_PDN_BST_MASK,
16106387f866SBrian Austin 					1 << CS35L35_PDN_BST_FETOFF_SHIFT);
16116387f866SBrian Austin 
16126387f866SBrian Austin 	regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL3,
16136387f866SBrian Austin 			CS35L35_PWR3_PDN_MASK,
16146387f866SBrian Austin 			CS35L35_PWR3_PDN_MASK);
16156387f866SBrian Austin 
16166387f866SBrian Austin 	regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
16176387f866SBrian Austin 		CS35L35_AMP_MUTE_MASK, 1 << CS35L35_AMP_MUTE_SHIFT);
16186387f866SBrian Austin 
1619*86c2eddfSKuninori Morimoto 	ret = devm_snd_soc_register_component(dev, &soc_component_dev_cs35l35,
1620*86c2eddfSKuninori Morimoto 					cs35l35_dai, ARRAY_SIZE(cs35l35_dai));
16216387f866SBrian Austin 	if (ret < 0) {
1622*86c2eddfSKuninori Morimoto 		dev_err(dev, "Failed to register component: %d\n", ret);
16236387f866SBrian Austin 		goto err;
16246387f866SBrian Austin 	}
16256387f866SBrian Austin 
16261bb06adaSCharles Keepax 	return 0;
16271bb06adaSCharles Keepax 
16286387f866SBrian Austin err:
16296387f866SBrian Austin 	regulator_bulk_disable(cs35l35->num_supplies,
16306387f866SBrian Austin 			       cs35l35->supplies);
16316387f866SBrian Austin 	gpiod_set_value_cansleep(cs35l35->reset_gpio, 0);
16326387f866SBrian Austin 
16336387f866SBrian Austin 	return ret;
16346387f866SBrian Austin }
16356387f866SBrian Austin 
16366387f866SBrian Austin static const struct of_device_id cs35l35_of_match[] = {
16376387f866SBrian Austin 	{.compatible = "cirrus,cs35l35"},
16386387f866SBrian Austin 	{},
16396387f866SBrian Austin };
16406387f866SBrian Austin MODULE_DEVICE_TABLE(of, cs35l35_of_match);
16416387f866SBrian Austin 
16426387f866SBrian Austin static const struct i2c_device_id cs35l35_id[] = {
16436387f866SBrian Austin 	{"cs35l35", 0},
16446387f866SBrian Austin 	{}
16456387f866SBrian Austin };
16466387f866SBrian Austin 
16476387f866SBrian Austin MODULE_DEVICE_TABLE(i2c, cs35l35_id);
16486387f866SBrian Austin 
16496387f866SBrian Austin static struct i2c_driver cs35l35_i2c_driver = {
16506387f866SBrian Austin 	.driver = {
16516387f866SBrian Austin 		.name = "cs35l35",
16526387f866SBrian Austin 		.of_match_table = cs35l35_of_match,
16536387f866SBrian Austin 	},
16546387f866SBrian Austin 	.id_table = cs35l35_id,
16556387f866SBrian Austin 	.probe = cs35l35_i2c_probe,
16566387f866SBrian Austin };
16576387f866SBrian Austin 
16586387f866SBrian Austin module_i2c_driver(cs35l35_i2c_driver);
16596387f866SBrian Austin 
16606387f866SBrian Austin MODULE_DESCRIPTION("ASoC CS35L35 driver");
16616387f866SBrian Austin MODULE_AUTHOR("Brian Austin, Cirrus Logic Inc, <brian.austin@cirrus.com>");
16626387f866SBrian Austin MODULE_LICENSE("GPL");
1663