xref: /openbmc/linux/sound/soc/codecs/tfa989x.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1af00978aSStephan Gerhold // SPDX-License-Identifier: GPL-2.0-only
2af00978aSStephan Gerhold /*
3af00978aSStephan Gerhold  * Copyright (C) 2021 Stephan Gerhold
4af00978aSStephan Gerhold  *
5af00978aSStephan Gerhold  * Register definitions/sequences taken from various tfa98xx kernel drivers:
6af00978aSStephan Gerhold  * Copyright (C) 2014-2020 NXP Semiconductors, All Rights Reserved.
7af00978aSStephan Gerhold  * Copyright (C) 2013 Sony Mobile Communications Inc.
8af00978aSStephan Gerhold  */
9af00978aSStephan Gerhold 
109da52c39SVincent Knecht #include <linux/gpio/consumer.h>
11af00978aSStephan Gerhold #include <linux/i2c.h>
12af00978aSStephan Gerhold #include <linux/module.h>
13af00978aSStephan Gerhold #include <linux/regmap.h>
148e5607e9SVincent Knecht #include <linux/regulator/consumer.h>
15af00978aSStephan Gerhold #include <sound/soc.h>
16af00978aSStephan Gerhold 
17af00978aSStephan Gerhold #define TFA989X_STATUSREG		0x00
18af00978aSStephan Gerhold #define TFA989X_BATTERYVOLTAGE		0x01
19af00978aSStephan Gerhold #define TFA989X_TEMPERATURE		0x02
20af00978aSStephan Gerhold #define TFA989X_REVISIONNUMBER		0x03
21af00978aSStephan Gerhold #define TFA989X_REVISIONNUMBER_REV_MSK	GENMASK(7, 0)	/* device revision */
22af00978aSStephan Gerhold #define TFA989X_I2SREG			0x04
23b6a4e209SVincent Knecht #define TFA989X_I2SREG_RCV		2	/* receiver mode */
24af00978aSStephan Gerhold #define TFA989X_I2SREG_CHSA		6	/* amplifier input select */
25af00978aSStephan Gerhold #define TFA989X_I2SREG_CHSA_MSK		GENMASK(7, 6)
26af00978aSStephan Gerhold #define TFA989X_I2SREG_I2SSR		12	/* sample rate */
27af00978aSStephan Gerhold #define TFA989X_I2SREG_I2SSR_MSK	GENMASK(15, 12)
28af00978aSStephan Gerhold #define TFA989X_BAT_PROT		0x05
29af00978aSStephan Gerhold #define TFA989X_AUDIO_CTR		0x06
30af00978aSStephan Gerhold #define TFA989X_DCDCBOOST		0x07
31af00978aSStephan Gerhold #define TFA989X_SPKR_CALIBRATION	0x08
32af00978aSStephan Gerhold #define TFA989X_SYS_CTRL		0x09
33af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_PWDN		0	/* power down */
34af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_I2CR		1	/* I2C reset */
35af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_CFE		2	/* enable CoolFlux DSP */
36af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_AMPE		3	/* enable amplifier */
37af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_DCA		4	/* enable boost */
38af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_SBSL		5	/* DSP configured */
39af00978aSStephan Gerhold #define TFA989X_SYS_CTRL_AMPC		6	/* amplifier enabled by DSP */
40af00978aSStephan Gerhold #define TFA989X_I2S_SEL_REG		0x0a
41af00978aSStephan Gerhold #define TFA989X_I2S_SEL_REG_SPKR_MSK	GENMASK(10, 9)	/* speaker impedance */
42af00978aSStephan Gerhold #define TFA989X_I2S_SEL_REG_DCFG_MSK	GENMASK(14, 11)	/* DCDC compensation */
43ef6c3209SAlexander Martinz #define TFA989X_HIDE_UNHIDE_KEY	0x40
44af00978aSStephan Gerhold #define TFA989X_PWM_CONTROL		0x41
45af00978aSStephan Gerhold #define TFA989X_CURRENTSENSE1		0x46
46af00978aSStephan Gerhold #define TFA989X_CURRENTSENSE2		0x47
47af00978aSStephan Gerhold #define TFA989X_CURRENTSENSE3		0x48
48af00978aSStephan Gerhold #define TFA989X_CURRENTSENSE4		0x49
49af00978aSStephan Gerhold 
50ef6c3209SAlexander Martinz #define TFA9890_REVISION		0x80
51af00978aSStephan Gerhold #define TFA9895_REVISION		0x12
521ba1d69dSVincent Knecht #define TFA9897_REVISION		0x97
53af00978aSStephan Gerhold 
54af00978aSStephan Gerhold struct tfa989x_rev {
55af00978aSStephan Gerhold 	unsigned int rev;
56af00978aSStephan Gerhold 	int (*init)(struct regmap *regmap);
57af00978aSStephan Gerhold };
58af00978aSStephan Gerhold 
598e5607e9SVincent Knecht struct tfa989x {
60b6a4e209SVincent Knecht 	const struct tfa989x_rev *rev;
618e5607e9SVincent Knecht 	struct regulator *vddd_supply;
629da52c39SVincent Knecht 	struct gpio_desc *rcv_gpiod;
638e5607e9SVincent Knecht };
648e5607e9SVincent Knecht 
tfa989x_writeable_reg(struct device * dev,unsigned int reg)65af00978aSStephan Gerhold static bool tfa989x_writeable_reg(struct device *dev, unsigned int reg)
66af00978aSStephan Gerhold {
67af00978aSStephan Gerhold 	return reg > TFA989X_REVISIONNUMBER;
68af00978aSStephan Gerhold }
69af00978aSStephan Gerhold 
tfa989x_volatile_reg(struct device * dev,unsigned int reg)70af00978aSStephan Gerhold static bool tfa989x_volatile_reg(struct device *dev, unsigned int reg)
71af00978aSStephan Gerhold {
72af00978aSStephan Gerhold 	return reg < TFA989X_REVISIONNUMBER;
73af00978aSStephan Gerhold }
74af00978aSStephan Gerhold 
75af00978aSStephan Gerhold static const struct regmap_config tfa989x_regmap = {
76af00978aSStephan Gerhold 	.reg_bits = 8,
77af00978aSStephan Gerhold 	.val_bits = 16,
78af00978aSStephan Gerhold 
79af00978aSStephan Gerhold 	.writeable_reg	= tfa989x_writeable_reg,
80af00978aSStephan Gerhold 	.volatile_reg	= tfa989x_volatile_reg,
81af00978aSStephan Gerhold 	.cache_type	= REGCACHE_RBTREE,
82af00978aSStephan Gerhold };
83af00978aSStephan Gerhold 
84af00978aSStephan Gerhold static const char * const chsa_text[] = { "Left", "Right", /* "DSP" */ };
85af00978aSStephan Gerhold static SOC_ENUM_SINGLE_DECL(chsa_enum, TFA989X_I2SREG, TFA989X_I2SREG_CHSA, chsa_text);
86af00978aSStephan Gerhold static const struct snd_kcontrol_new chsa_mux = SOC_DAPM_ENUM("Amp Input", chsa_enum);
87af00978aSStephan Gerhold 
88af00978aSStephan Gerhold static const struct snd_soc_dapm_widget tfa989x_dapm_widgets[] = {
89af00978aSStephan Gerhold 	SND_SOC_DAPM_OUTPUT("OUT"),
90af00978aSStephan Gerhold 	SND_SOC_DAPM_SUPPLY("POWER", TFA989X_SYS_CTRL, TFA989X_SYS_CTRL_PWDN, 1, NULL, 0),
91af00978aSStephan Gerhold 	SND_SOC_DAPM_OUT_DRV("AMPE", TFA989X_SYS_CTRL, TFA989X_SYS_CTRL_AMPE, 0, NULL, 0),
92af00978aSStephan Gerhold 
93af00978aSStephan Gerhold 	SND_SOC_DAPM_MUX("Amp Input", SND_SOC_NOPM, 0, 0, &chsa_mux),
94af00978aSStephan Gerhold 	SND_SOC_DAPM_AIF_IN("AIFINL", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
95af00978aSStephan Gerhold 	SND_SOC_DAPM_AIF_IN("AIFINR", "HiFi Playback", 1, SND_SOC_NOPM, 0, 0),
96af00978aSStephan Gerhold };
97af00978aSStephan Gerhold 
98af00978aSStephan Gerhold static const struct snd_soc_dapm_route tfa989x_dapm_routes[] = {
99af00978aSStephan Gerhold 	{"OUT", NULL, "AMPE"},
100af00978aSStephan Gerhold 	{"AMPE", NULL, "POWER"},
101af00978aSStephan Gerhold 	{"AMPE", NULL, "Amp Input"},
102af00978aSStephan Gerhold 	{"Amp Input", "Left", "AIFINL"},
103af00978aSStephan Gerhold 	{"Amp Input", "Right", "AIFINR"},
104af00978aSStephan Gerhold };
105af00978aSStephan Gerhold 
tfa989x_put_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1069da52c39SVincent Knecht static int tfa989x_put_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1079da52c39SVincent Knecht {
1089da52c39SVincent Knecht 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1099da52c39SVincent Knecht 	struct tfa989x *tfa989x = snd_soc_component_get_drvdata(component);
1109da52c39SVincent Knecht 
1119da52c39SVincent Knecht 	gpiod_set_value_cansleep(tfa989x->rcv_gpiod, ucontrol->value.enumerated.item[0]);
1129da52c39SVincent Knecht 
1139da52c39SVincent Knecht 	return snd_soc_put_enum_double(kcontrol, ucontrol);
1149da52c39SVincent Knecht }
1159da52c39SVincent Knecht 
116b6a4e209SVincent Knecht static const char * const mode_text[] = { "Speaker", "Receiver" };
117b6a4e209SVincent Knecht static SOC_ENUM_SINGLE_DECL(mode_enum, TFA989X_I2SREG, TFA989X_I2SREG_RCV, mode_text);
118b6a4e209SVincent Knecht static const struct snd_kcontrol_new tfa989x_mode_controls[] = {
1199da52c39SVincent Knecht 	SOC_ENUM_EXT("Mode", mode_enum, snd_soc_get_enum_double, tfa989x_put_mode),
120b6a4e209SVincent Knecht };
121b6a4e209SVincent Knecht 
tfa989x_probe(struct snd_soc_component * component)122b6a4e209SVincent Knecht static int tfa989x_probe(struct snd_soc_component *component)
123b6a4e209SVincent Knecht {
124b6a4e209SVincent Knecht 	struct tfa989x *tfa989x = snd_soc_component_get_drvdata(component);
125b6a4e209SVincent Knecht 
126b6a4e209SVincent Knecht 	if (tfa989x->rev->rev == TFA9897_REVISION)
127b6a4e209SVincent Knecht 		return snd_soc_add_component_controls(component, tfa989x_mode_controls,
128b6a4e209SVincent Knecht 						      ARRAY_SIZE(tfa989x_mode_controls));
129b6a4e209SVincent Knecht 
130b6a4e209SVincent Knecht 	return 0;
131b6a4e209SVincent Knecht }
132b6a4e209SVincent Knecht 
133af00978aSStephan Gerhold static const struct snd_soc_component_driver tfa989x_component = {
134b6a4e209SVincent Knecht 	.probe			= tfa989x_probe,
135af00978aSStephan Gerhold 	.dapm_widgets		= tfa989x_dapm_widgets,
136af00978aSStephan Gerhold 	.num_dapm_widgets	= ARRAY_SIZE(tfa989x_dapm_widgets),
137af00978aSStephan Gerhold 	.dapm_routes		= tfa989x_dapm_routes,
138af00978aSStephan Gerhold 	.num_dapm_routes	= ARRAY_SIZE(tfa989x_dapm_routes),
139af00978aSStephan Gerhold 	.use_pmdown_time	= 1,
140af00978aSStephan Gerhold 	.endianness		= 1,
141af00978aSStephan Gerhold };
142af00978aSStephan Gerhold 
143af00978aSStephan Gerhold static const unsigned int tfa989x_rates[] = {
144af00978aSStephan Gerhold 	8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
145af00978aSStephan Gerhold };
146af00978aSStephan Gerhold 
tfa989x_find_sample_rate(unsigned int rate)147af00978aSStephan Gerhold static int tfa989x_find_sample_rate(unsigned int rate)
148af00978aSStephan Gerhold {
149af00978aSStephan Gerhold 	int i;
150af00978aSStephan Gerhold 
151af00978aSStephan Gerhold 	for (i = 0; i < ARRAY_SIZE(tfa989x_rates); ++i)
152af00978aSStephan Gerhold 		if (tfa989x_rates[i] == rate)
153af00978aSStephan Gerhold 			return i;
154af00978aSStephan Gerhold 
155af00978aSStephan Gerhold 	return -EINVAL;
156af00978aSStephan Gerhold }
157af00978aSStephan Gerhold 
tfa989x_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)158af00978aSStephan Gerhold static int tfa989x_hw_params(struct snd_pcm_substream *substream,
159af00978aSStephan Gerhold 			     struct snd_pcm_hw_params *params,
160af00978aSStephan Gerhold 			     struct snd_soc_dai *dai)
161af00978aSStephan Gerhold {
162af00978aSStephan Gerhold 	struct snd_soc_component *component = dai->component;
163af00978aSStephan Gerhold 	int sr;
164af00978aSStephan Gerhold 
165af00978aSStephan Gerhold 	sr = tfa989x_find_sample_rate(params_rate(params));
166af00978aSStephan Gerhold 	if (sr < 0)
167af00978aSStephan Gerhold 		return sr;
168af00978aSStephan Gerhold 
169af00978aSStephan Gerhold 	return snd_soc_component_update_bits(component, TFA989X_I2SREG,
170af00978aSStephan Gerhold 					     TFA989X_I2SREG_I2SSR_MSK,
171af00978aSStephan Gerhold 					     sr << TFA989X_I2SREG_I2SSR);
172af00978aSStephan Gerhold }
173af00978aSStephan Gerhold 
174af00978aSStephan Gerhold static const struct snd_soc_dai_ops tfa989x_dai_ops = {
175af00978aSStephan Gerhold 	.hw_params = tfa989x_hw_params,
176af00978aSStephan Gerhold };
177af00978aSStephan Gerhold 
178af00978aSStephan Gerhold static struct snd_soc_dai_driver tfa989x_dai = {
179af00978aSStephan Gerhold 	.name = "tfa989x-hifi",
180af00978aSStephan Gerhold 	.playback = {
181af00978aSStephan Gerhold 		.stream_name	= "HiFi Playback",
182af00978aSStephan Gerhold 		.formats	= SNDRV_PCM_FMTBIT_S16_LE,
183af00978aSStephan Gerhold 		.rates		= SNDRV_PCM_RATE_8000_48000,
184af00978aSStephan Gerhold 		.rate_min	= 8000,
185af00978aSStephan Gerhold 		.rate_max	= 48000,
186af00978aSStephan Gerhold 		.channels_min	= 1,
187af00978aSStephan Gerhold 		.channels_max	= 2,
188af00978aSStephan Gerhold 	},
189af00978aSStephan Gerhold 	.ops = &tfa989x_dai_ops,
190af00978aSStephan Gerhold };
191af00978aSStephan Gerhold 
tfa9890_init(struct regmap * regmap)192ef6c3209SAlexander Martinz static int tfa9890_init(struct regmap *regmap)
193ef6c3209SAlexander Martinz {
194ef6c3209SAlexander Martinz 	int ret;
195ef6c3209SAlexander Martinz 
1969b9def51SAlexander Martinz 	/* temporarily allow access to hidden registers */
197ef6c3209SAlexander Martinz 	ret = regmap_write(regmap, TFA989X_HIDE_UNHIDE_KEY, 0x5a6b);
198ef6c3209SAlexander Martinz 	if (ret)
199ef6c3209SAlexander Martinz 		return ret;
200ef6c3209SAlexander Martinz 
201ef6c3209SAlexander Martinz 	/* update PLL registers */
202ef6c3209SAlexander Martinz 	ret = regmap_set_bits(regmap, 0x59, 0x3);
203ef6c3209SAlexander Martinz 	if (ret)
204ef6c3209SAlexander Martinz 		return ret;
205ef6c3209SAlexander Martinz 
2069b9def51SAlexander Martinz 	/* hide registers again */
207ef6c3209SAlexander Martinz 	ret = regmap_write(regmap, TFA989X_HIDE_UNHIDE_KEY, 0x0000);
208ef6c3209SAlexander Martinz 	if (ret)
209ef6c3209SAlexander Martinz 		return ret;
210ef6c3209SAlexander Martinz 
211ef6c3209SAlexander Martinz 	return regmap_write(regmap, TFA989X_CURRENTSENSE2, 0x7BE1);
212ef6c3209SAlexander Martinz }
213ef6c3209SAlexander Martinz 
214ef6c3209SAlexander Martinz static const struct tfa989x_rev tfa9890_rev = {
215ef6c3209SAlexander Martinz 	.rev	= TFA9890_REVISION,
216ef6c3209SAlexander Martinz 	.init	= tfa9890_init,
217ef6c3209SAlexander Martinz };
218ef6c3209SAlexander Martinz 
219af00978aSStephan Gerhold static const struct reg_sequence tfa9895_reg_init[] = {
220af00978aSStephan Gerhold 	/* some other registers must be set for optimal amplifier behaviour */
221af00978aSStephan Gerhold 	{ TFA989X_BAT_PROT, 0x13ab },
222af00978aSStephan Gerhold 	{ TFA989X_AUDIO_CTR, 0x001f },
223af00978aSStephan Gerhold 
224af00978aSStephan Gerhold 	/* peak voltage protection is always on, but may be written */
225af00978aSStephan Gerhold 	{ TFA989X_SPKR_CALIBRATION, 0x3c4e },
226af00978aSStephan Gerhold 
227af00978aSStephan Gerhold 	/* TFA989X_SYSCTRL_DCA = 0 */
228af00978aSStephan Gerhold 	{ TFA989X_SYS_CTRL, 0x024d },
229af00978aSStephan Gerhold 	{ TFA989X_PWM_CONTROL, 0x0308 },
230af00978aSStephan Gerhold 	{ TFA989X_CURRENTSENSE4, 0x0e82 },
231af00978aSStephan Gerhold };
232af00978aSStephan Gerhold 
tfa9895_init(struct regmap * regmap)233af00978aSStephan Gerhold static int tfa9895_init(struct regmap *regmap)
234af00978aSStephan Gerhold {
235af00978aSStephan Gerhold 	return regmap_multi_reg_write(regmap, tfa9895_reg_init,
236af00978aSStephan Gerhold 				      ARRAY_SIZE(tfa9895_reg_init));
237af00978aSStephan Gerhold }
238af00978aSStephan Gerhold 
239af00978aSStephan Gerhold static const struct tfa989x_rev tfa9895_rev = {
240af00978aSStephan Gerhold 	.rev	= TFA9895_REVISION,
241af00978aSStephan Gerhold 	.init	= tfa9895_init,
242af00978aSStephan Gerhold };
243af00978aSStephan Gerhold 
tfa9897_init(struct regmap * regmap)2441ba1d69dSVincent Knecht static int tfa9897_init(struct regmap *regmap)
2451ba1d69dSVincent Knecht {
2461ba1d69dSVincent Knecht 	int ret;
2471ba1d69dSVincent Knecht 
2481ba1d69dSVincent Knecht 	/* Reduce slewrate by clearing iddqtestbst to avoid booster damage */
2491ba1d69dSVincent Knecht 	ret = regmap_write(regmap, TFA989X_CURRENTSENSE3, 0x0300);
2501ba1d69dSVincent Knecht 	if (ret)
2511ba1d69dSVincent Knecht 		return ret;
2521ba1d69dSVincent Knecht 
2531ba1d69dSVincent Knecht 	/* Enable clipping */
2541ba1d69dSVincent Knecht 	ret = regmap_clear_bits(regmap, TFA989X_CURRENTSENSE4, 0x1);
2551ba1d69dSVincent Knecht 	if (ret)
2561ba1d69dSVincent Knecht 		return ret;
2571ba1d69dSVincent Knecht 
2581ba1d69dSVincent Knecht 	/* Set required TDM configuration */
2591ba1d69dSVincent Knecht 	return regmap_write(regmap, 0x14, 0x0);
2601ba1d69dSVincent Knecht }
2611ba1d69dSVincent Knecht 
2621ba1d69dSVincent Knecht static const struct tfa989x_rev tfa9897_rev = {
2631ba1d69dSVincent Knecht 	.rev	= TFA9897_REVISION,
2641ba1d69dSVincent Knecht 	.init	= tfa9897_init,
2651ba1d69dSVincent Knecht };
2661ba1d69dSVincent Knecht 
267af00978aSStephan Gerhold /*
268af00978aSStephan Gerhold  * Note: At the moment this driver bypasses the "CoolFlux DSP" built into the
269af00978aSStephan Gerhold  * TFA989X amplifiers. Unfortunately, there seems to be absolutely
270af00978aSStephan Gerhold  * no documentation for it - the public "short datasheets" do not provide
271af00978aSStephan Gerhold  * any information about the DSP or available registers.
272af00978aSStephan Gerhold  *
273af00978aSStephan Gerhold  * Usually the TFA989X amplifiers are configured through proprietary userspace
274af00978aSStephan Gerhold  * libraries. There are also some (rather complex) kernel drivers but even those
275af00978aSStephan Gerhold  * rely on obscure firmware blobs for configuration (so-called "containers").
276af00978aSStephan Gerhold  * They seem to contain different "profiles" with tuned speaker settings, sample
277af00978aSStephan Gerhold  * rates and volume steps (which would be better exposed as separate ALSA mixers).
278af00978aSStephan Gerhold  *
279af00978aSStephan Gerhold  * Bypassing the DSP disables volume control (and perhaps some speaker
280af00978aSStephan Gerhold  * optimization?), but at least allows using the speaker without obscure
281af00978aSStephan Gerhold  * kernel drivers and firmware.
282af00978aSStephan Gerhold  *
283af00978aSStephan Gerhold  * Ideally NXP (or now Goodix) should release proper documentation for these
284af00978aSStephan Gerhold  * amplifiers so that support for the "CoolFlux DSP" can be implemented properly.
285af00978aSStephan Gerhold  */
tfa989x_dsp_bypass(struct regmap * regmap)286af00978aSStephan Gerhold static int tfa989x_dsp_bypass(struct regmap *regmap)
287af00978aSStephan Gerhold {
288af00978aSStephan Gerhold 	int ret;
289af00978aSStephan Gerhold 
290af00978aSStephan Gerhold 	/* Clear CHSA to bypass DSP and take input from I2S 1 left channel */
291af00978aSStephan Gerhold 	ret = regmap_clear_bits(regmap, TFA989X_I2SREG, TFA989X_I2SREG_CHSA_MSK);
292af00978aSStephan Gerhold 	if (ret)
293af00978aSStephan Gerhold 		return ret;
294af00978aSStephan Gerhold 
295af00978aSStephan Gerhold 	/* Set DCDC compensation to off and speaker impedance to 8 ohm */
296af00978aSStephan Gerhold 	ret = regmap_update_bits(regmap, TFA989X_I2S_SEL_REG,
297af00978aSStephan Gerhold 				 TFA989X_I2S_SEL_REG_DCFG_MSK |
298af00978aSStephan Gerhold 				 TFA989X_I2S_SEL_REG_SPKR_MSK,
299af00978aSStephan Gerhold 				 TFA989X_I2S_SEL_REG_SPKR_MSK);
300af00978aSStephan Gerhold 	if (ret)
301af00978aSStephan Gerhold 		return ret;
302af00978aSStephan Gerhold 
303af00978aSStephan Gerhold 	/* Set DCDC to follower mode and disable CoolFlux DSP */
304af00978aSStephan Gerhold 	return regmap_clear_bits(regmap, TFA989X_SYS_CTRL,
305af00978aSStephan Gerhold 				 BIT(TFA989X_SYS_CTRL_DCA) |
306af00978aSStephan Gerhold 				 BIT(TFA989X_SYS_CTRL_CFE) |
307af00978aSStephan Gerhold 				 BIT(TFA989X_SYS_CTRL_AMPC));
308af00978aSStephan Gerhold }
309af00978aSStephan Gerhold 
tfa989x_regulator_disable(void * data)3108e5607e9SVincent Knecht static void tfa989x_regulator_disable(void *data)
3118e5607e9SVincent Knecht {
3128e5607e9SVincent Knecht 	struct tfa989x *tfa989x = data;
3138e5607e9SVincent Knecht 
3148e5607e9SVincent Knecht 	regulator_disable(tfa989x->vddd_supply);
3158e5607e9SVincent Knecht }
3168e5607e9SVincent Knecht 
tfa989x_i2c_probe(struct i2c_client * i2c)317af00978aSStephan Gerhold static int tfa989x_i2c_probe(struct i2c_client *i2c)
318af00978aSStephan Gerhold {
319af00978aSStephan Gerhold 	struct device *dev = &i2c->dev;
320af00978aSStephan Gerhold 	const struct tfa989x_rev *rev;
3218e5607e9SVincent Knecht 	struct tfa989x *tfa989x;
322af00978aSStephan Gerhold 	struct regmap *regmap;
323af00978aSStephan Gerhold 	unsigned int val;
324af00978aSStephan Gerhold 	int ret;
325af00978aSStephan Gerhold 
326af00978aSStephan Gerhold 	rev = device_get_match_data(dev);
327af00978aSStephan Gerhold 	if (!rev) {
328af00978aSStephan Gerhold 		dev_err(dev, "unknown device revision\n");
329af00978aSStephan Gerhold 		return -ENODEV;
330af00978aSStephan Gerhold 	}
331af00978aSStephan Gerhold 
3328e5607e9SVincent Knecht 	tfa989x = devm_kzalloc(dev, sizeof(*tfa989x), GFP_KERNEL);
3338e5607e9SVincent Knecht 	if (!tfa989x)
3348e5607e9SVincent Knecht 		return -ENOMEM;
3358e5607e9SVincent Knecht 
336b6a4e209SVincent Knecht 	tfa989x->rev = rev;
3378e5607e9SVincent Knecht 	i2c_set_clientdata(i2c, tfa989x);
3388e5607e9SVincent Knecht 
3398e5607e9SVincent Knecht 	tfa989x->vddd_supply = devm_regulator_get(dev, "vddd");
3408e5607e9SVincent Knecht 	if (IS_ERR(tfa989x->vddd_supply))
3418e5607e9SVincent Knecht 		return dev_err_probe(dev, PTR_ERR(tfa989x->vddd_supply),
3428e5607e9SVincent Knecht 				     "Failed to get vddd regulator\n");
3438e5607e9SVincent Knecht 
3449da52c39SVincent Knecht 	if (tfa989x->rev->rev == TFA9897_REVISION) {
3459da52c39SVincent Knecht 		tfa989x->rcv_gpiod = devm_gpiod_get_optional(dev, "rcv", GPIOD_OUT_LOW);
3469da52c39SVincent Knecht 		if (IS_ERR(tfa989x->rcv_gpiod))
3479da52c39SVincent Knecht 			return PTR_ERR(tfa989x->rcv_gpiod);
3489da52c39SVincent Knecht 	}
3499da52c39SVincent Knecht 
350af00978aSStephan Gerhold 	regmap = devm_regmap_init_i2c(i2c, &tfa989x_regmap);
351af00978aSStephan Gerhold 	if (IS_ERR(regmap))
352af00978aSStephan Gerhold 		return PTR_ERR(regmap);
353af00978aSStephan Gerhold 
3548e5607e9SVincent Knecht 	ret = regulator_enable(tfa989x->vddd_supply);
3558e5607e9SVincent Knecht 	if (ret) {
3568e5607e9SVincent Knecht 		dev_err(dev, "Failed to enable vddd regulator: %d\n", ret);
3578e5607e9SVincent Knecht 		return ret;
3588e5607e9SVincent Knecht 	}
3598e5607e9SVincent Knecht 
3608e5607e9SVincent Knecht 	ret = devm_add_action_or_reset(dev, tfa989x_regulator_disable, tfa989x);
3618e5607e9SVincent Knecht 	if (ret)
3628e5607e9SVincent Knecht 		return ret;
3638e5607e9SVincent Knecht 
364af00978aSStephan Gerhold 	/* Bypass regcache for reset and init sequence */
365af00978aSStephan Gerhold 	regcache_cache_bypass(regmap, true);
366af00978aSStephan Gerhold 
367af00978aSStephan Gerhold 	/* Dummy read to generate i2c clocks, required on some devices */
368af00978aSStephan Gerhold 	regmap_read(regmap, TFA989X_REVISIONNUMBER, &val);
369af00978aSStephan Gerhold 
370af00978aSStephan Gerhold 	ret = regmap_read(regmap, TFA989X_REVISIONNUMBER, &val);
371af00978aSStephan Gerhold 	if (ret) {
372af00978aSStephan Gerhold 		dev_err(dev, "failed to read revision number: %d\n", ret);
373af00978aSStephan Gerhold 		return ret;
374af00978aSStephan Gerhold 	}
375af00978aSStephan Gerhold 
376af00978aSStephan Gerhold 	val &= TFA989X_REVISIONNUMBER_REV_MSK;
377af00978aSStephan Gerhold 	if (val != rev->rev) {
378af00978aSStephan Gerhold 		dev_err(dev, "invalid revision number, expected %#x, got %#x\n",
379af00978aSStephan Gerhold 			rev->rev, val);
380af00978aSStephan Gerhold 		return -ENODEV;
381af00978aSStephan Gerhold 	}
382af00978aSStephan Gerhold 
383af00978aSStephan Gerhold 	ret = regmap_write(regmap, TFA989X_SYS_CTRL, BIT(TFA989X_SYS_CTRL_I2CR));
384af00978aSStephan Gerhold 	if (ret) {
385af00978aSStephan Gerhold 		dev_err(dev, "failed to reset I2C registers: %d\n", ret);
386af00978aSStephan Gerhold 		return ret;
387af00978aSStephan Gerhold 	}
388af00978aSStephan Gerhold 
389af00978aSStephan Gerhold 	ret = rev->init(regmap);
390af00978aSStephan Gerhold 	if (ret) {
391af00978aSStephan Gerhold 		dev_err(dev, "failed to initialize registers: %d\n", ret);
392af00978aSStephan Gerhold 		return ret;
393af00978aSStephan Gerhold 	}
394af00978aSStephan Gerhold 
395af00978aSStephan Gerhold 	ret = tfa989x_dsp_bypass(regmap);
396af00978aSStephan Gerhold 	if (ret) {
397af00978aSStephan Gerhold 		dev_err(dev, "failed to enable DSP bypass: %d\n", ret);
398af00978aSStephan Gerhold 		return ret;
399af00978aSStephan Gerhold 	}
400af00978aSStephan Gerhold 	regcache_cache_bypass(regmap, false);
401af00978aSStephan Gerhold 
402af00978aSStephan Gerhold 	return devm_snd_soc_register_component(dev, &tfa989x_component,
403af00978aSStephan Gerhold 					       &tfa989x_dai, 1);
404af00978aSStephan Gerhold }
405af00978aSStephan Gerhold 
406af00978aSStephan Gerhold static const struct of_device_id tfa989x_of_match[] = {
407ef6c3209SAlexander Martinz 	{ .compatible = "nxp,tfa9890", .data = &tfa9890_rev },
408af00978aSStephan Gerhold 	{ .compatible = "nxp,tfa9895", .data = &tfa9895_rev },
4091ba1d69dSVincent Knecht 	{ .compatible = "nxp,tfa9897", .data = &tfa9897_rev },
410af00978aSStephan Gerhold 	{ }
411af00978aSStephan Gerhold };
412af00978aSStephan Gerhold MODULE_DEVICE_TABLE(of, tfa989x_of_match);
413af00978aSStephan Gerhold 
414af00978aSStephan Gerhold static struct i2c_driver tfa989x_i2c_driver = {
415af00978aSStephan Gerhold 	.driver = {
416af00978aSStephan Gerhold 		.name = "tfa989x",
417af00978aSStephan Gerhold 		.of_match_table = tfa989x_of_match,
418af00978aSStephan Gerhold 	},
419*9abcd240SUwe Kleine-König 	.probe = tfa989x_i2c_probe,
420af00978aSStephan Gerhold };
421af00978aSStephan Gerhold module_i2c_driver(tfa989x_i2c_driver);
422af00978aSStephan Gerhold 
423af00978aSStephan Gerhold MODULE_DESCRIPTION("ASoC NXP/Goodix TFA989X (TFA1) driver");
424af00978aSStephan Gerhold MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
425af00978aSStephan Gerhold MODULE_LICENSE("GPL");
426