xref: /openbmc/linux/sound/soc/codecs/tfa989x.c (revision dd2939ef)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Stephan Gerhold
4  *
5  * Register definitions/sequences taken from various tfa98xx kernel drivers:
6  * Copyright (C) 2014-2020 NXP Semiconductors, All Rights Reserved.
7  * Copyright (C) 2013 Sony Mobile Communications Inc.
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/consumer.h>
14 #include <sound/soc.h>
15 
16 #define TFA989X_STATUSREG		0x00
17 #define TFA989X_BATTERYVOLTAGE		0x01
18 #define TFA989X_TEMPERATURE		0x02
19 #define TFA989X_REVISIONNUMBER		0x03
20 #define TFA989X_REVISIONNUMBER_REV_MSK	GENMASK(7, 0)	/* device revision */
21 #define TFA989X_I2SREG			0x04
22 #define TFA989X_I2SREG_CHSA		6	/* amplifier input select */
23 #define TFA989X_I2SREG_CHSA_MSK		GENMASK(7, 6)
24 #define TFA989X_I2SREG_I2SSR		12	/* sample rate */
25 #define TFA989X_I2SREG_I2SSR_MSK	GENMASK(15, 12)
26 #define TFA989X_BAT_PROT		0x05
27 #define TFA989X_AUDIO_CTR		0x06
28 #define TFA989X_DCDCBOOST		0x07
29 #define TFA989X_SPKR_CALIBRATION	0x08
30 #define TFA989X_SYS_CTRL		0x09
31 #define TFA989X_SYS_CTRL_PWDN		0	/* power down */
32 #define TFA989X_SYS_CTRL_I2CR		1	/* I2C reset */
33 #define TFA989X_SYS_CTRL_CFE		2	/* enable CoolFlux DSP */
34 #define TFA989X_SYS_CTRL_AMPE		3	/* enable amplifier */
35 #define TFA989X_SYS_CTRL_DCA		4	/* enable boost */
36 #define TFA989X_SYS_CTRL_SBSL		5	/* DSP configured */
37 #define TFA989X_SYS_CTRL_AMPC		6	/* amplifier enabled by DSP */
38 #define TFA989X_I2S_SEL_REG		0x0a
39 #define TFA989X_I2S_SEL_REG_SPKR_MSK	GENMASK(10, 9)	/* speaker impedance */
40 #define TFA989X_I2S_SEL_REG_DCFG_MSK	GENMASK(14, 11)	/* DCDC compensation */
41 #define TFA989X_PWM_CONTROL		0x41
42 #define TFA989X_CURRENTSENSE1		0x46
43 #define TFA989X_CURRENTSENSE2		0x47
44 #define TFA989X_CURRENTSENSE3		0x48
45 #define TFA989X_CURRENTSENSE4		0x49
46 
47 #define TFA9895_REVISION		0x12
48 #define TFA9897_REVISION		0x97
49 
50 struct tfa989x_rev {
51 	unsigned int rev;
52 	int (*init)(struct regmap *regmap);
53 };
54 
55 struct tfa989x {
56 	struct regulator *vddd_supply;
57 };
58 
59 static bool tfa989x_writeable_reg(struct device *dev, unsigned int reg)
60 {
61 	return reg > TFA989X_REVISIONNUMBER;
62 }
63 
64 static bool tfa989x_volatile_reg(struct device *dev, unsigned int reg)
65 {
66 	return reg < TFA989X_REVISIONNUMBER;
67 }
68 
69 static const struct regmap_config tfa989x_regmap = {
70 	.reg_bits = 8,
71 	.val_bits = 16,
72 
73 	.writeable_reg	= tfa989x_writeable_reg,
74 	.volatile_reg	= tfa989x_volatile_reg,
75 	.cache_type	= REGCACHE_RBTREE,
76 };
77 
78 static const char * const chsa_text[] = { "Left", "Right", /* "DSP" */ };
79 static SOC_ENUM_SINGLE_DECL(chsa_enum, TFA989X_I2SREG, TFA989X_I2SREG_CHSA, chsa_text);
80 static const struct snd_kcontrol_new chsa_mux = SOC_DAPM_ENUM("Amp Input", chsa_enum);
81 
82 static const struct snd_soc_dapm_widget tfa989x_dapm_widgets[] = {
83 	SND_SOC_DAPM_OUTPUT("OUT"),
84 	SND_SOC_DAPM_SUPPLY("POWER", TFA989X_SYS_CTRL, TFA989X_SYS_CTRL_PWDN, 1, NULL, 0),
85 	SND_SOC_DAPM_OUT_DRV("AMPE", TFA989X_SYS_CTRL, TFA989X_SYS_CTRL_AMPE, 0, NULL, 0),
86 
87 	SND_SOC_DAPM_MUX("Amp Input", SND_SOC_NOPM, 0, 0, &chsa_mux),
88 	SND_SOC_DAPM_AIF_IN("AIFINL", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
89 	SND_SOC_DAPM_AIF_IN("AIFINR", "HiFi Playback", 1, SND_SOC_NOPM, 0, 0),
90 };
91 
92 static const struct snd_soc_dapm_route tfa989x_dapm_routes[] = {
93 	{"OUT", NULL, "AMPE"},
94 	{"AMPE", NULL, "POWER"},
95 	{"AMPE", NULL, "Amp Input"},
96 	{"Amp Input", "Left", "AIFINL"},
97 	{"Amp Input", "Right", "AIFINR"},
98 };
99 
100 static const struct snd_soc_component_driver tfa989x_component = {
101 	.dapm_widgets		= tfa989x_dapm_widgets,
102 	.num_dapm_widgets	= ARRAY_SIZE(tfa989x_dapm_widgets),
103 	.dapm_routes		= tfa989x_dapm_routes,
104 	.num_dapm_routes	= ARRAY_SIZE(tfa989x_dapm_routes),
105 	.use_pmdown_time	= 1,
106 	.endianness		= 1,
107 	.non_legacy_dai_naming	= 1,
108 };
109 
110 static const unsigned int tfa989x_rates[] = {
111 	8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
112 };
113 
114 static int tfa989x_find_sample_rate(unsigned int rate)
115 {
116 	int i;
117 
118 	for (i = 0; i < ARRAY_SIZE(tfa989x_rates); ++i)
119 		if (tfa989x_rates[i] == rate)
120 			return i;
121 
122 	return -EINVAL;
123 }
124 
125 static int tfa989x_hw_params(struct snd_pcm_substream *substream,
126 			     struct snd_pcm_hw_params *params,
127 			     struct snd_soc_dai *dai)
128 {
129 	struct snd_soc_component *component = dai->component;
130 	int sr;
131 
132 	sr = tfa989x_find_sample_rate(params_rate(params));
133 	if (sr < 0)
134 		return sr;
135 
136 	return snd_soc_component_update_bits(component, TFA989X_I2SREG,
137 					     TFA989X_I2SREG_I2SSR_MSK,
138 					     sr << TFA989X_I2SREG_I2SSR);
139 }
140 
141 static const struct snd_soc_dai_ops tfa989x_dai_ops = {
142 	.hw_params = tfa989x_hw_params,
143 };
144 
145 static struct snd_soc_dai_driver tfa989x_dai = {
146 	.name = "tfa989x-hifi",
147 	.playback = {
148 		.stream_name	= "HiFi Playback",
149 		.formats	= SNDRV_PCM_FMTBIT_S16_LE,
150 		.rates		= SNDRV_PCM_RATE_8000_48000,
151 		.rate_min	= 8000,
152 		.rate_max	= 48000,
153 		.channels_min	= 1,
154 		.channels_max	= 2,
155 	},
156 	.ops = &tfa989x_dai_ops,
157 };
158 
159 static const struct reg_sequence tfa9895_reg_init[] = {
160 	/* some other registers must be set for optimal amplifier behaviour */
161 	{ TFA989X_BAT_PROT, 0x13ab },
162 	{ TFA989X_AUDIO_CTR, 0x001f },
163 
164 	/* peak voltage protection is always on, but may be written */
165 	{ TFA989X_SPKR_CALIBRATION, 0x3c4e },
166 
167 	/* TFA989X_SYSCTRL_DCA = 0 */
168 	{ TFA989X_SYS_CTRL, 0x024d },
169 	{ TFA989X_PWM_CONTROL, 0x0308 },
170 	{ TFA989X_CURRENTSENSE4, 0x0e82 },
171 };
172 
173 static int tfa9895_init(struct regmap *regmap)
174 {
175 	return regmap_multi_reg_write(regmap, tfa9895_reg_init,
176 				      ARRAY_SIZE(tfa9895_reg_init));
177 }
178 
179 static const struct tfa989x_rev tfa9895_rev = {
180 	.rev	= TFA9895_REVISION,
181 	.init	= tfa9895_init,
182 };
183 
184 static int tfa9897_init(struct regmap *regmap)
185 {
186 	int ret;
187 
188 	/* Reduce slewrate by clearing iddqtestbst to avoid booster damage */
189 	ret = regmap_write(regmap, TFA989X_CURRENTSENSE3, 0x0300);
190 	if (ret)
191 		return ret;
192 
193 	/* Enable clipping */
194 	ret = regmap_clear_bits(regmap, TFA989X_CURRENTSENSE4, 0x1);
195 	if (ret)
196 		return ret;
197 
198 	/* Set required TDM configuration */
199 	return regmap_write(regmap, 0x14, 0x0);
200 }
201 
202 static const struct tfa989x_rev tfa9897_rev = {
203 	.rev	= TFA9897_REVISION,
204 	.init	= tfa9897_init,
205 };
206 
207 /*
208  * Note: At the moment this driver bypasses the "CoolFlux DSP" built into the
209  * TFA989X amplifiers. Unfortunately, there seems to be absolutely
210  * no documentation for it - the public "short datasheets" do not provide
211  * any information about the DSP or available registers.
212  *
213  * Usually the TFA989X amplifiers are configured through proprietary userspace
214  * libraries. There are also some (rather complex) kernel drivers but even those
215  * rely on obscure firmware blobs for configuration (so-called "containers").
216  * They seem to contain different "profiles" with tuned speaker settings, sample
217  * rates and volume steps (which would be better exposed as separate ALSA mixers).
218  *
219  * Bypassing the DSP disables volume control (and perhaps some speaker
220  * optimization?), but at least allows using the speaker without obscure
221  * kernel drivers and firmware.
222  *
223  * Ideally NXP (or now Goodix) should release proper documentation for these
224  * amplifiers so that support for the "CoolFlux DSP" can be implemented properly.
225  */
226 static int tfa989x_dsp_bypass(struct regmap *regmap)
227 {
228 	int ret;
229 
230 	/* Clear CHSA to bypass DSP and take input from I2S 1 left channel */
231 	ret = regmap_clear_bits(regmap, TFA989X_I2SREG, TFA989X_I2SREG_CHSA_MSK);
232 	if (ret)
233 		return ret;
234 
235 	/* Set DCDC compensation to off and speaker impedance to 8 ohm */
236 	ret = regmap_update_bits(regmap, TFA989X_I2S_SEL_REG,
237 				 TFA989X_I2S_SEL_REG_DCFG_MSK |
238 				 TFA989X_I2S_SEL_REG_SPKR_MSK,
239 				 TFA989X_I2S_SEL_REG_SPKR_MSK);
240 	if (ret)
241 		return ret;
242 
243 	/* Set DCDC to follower mode and disable CoolFlux DSP */
244 	return regmap_clear_bits(regmap, TFA989X_SYS_CTRL,
245 				 BIT(TFA989X_SYS_CTRL_DCA) |
246 				 BIT(TFA989X_SYS_CTRL_CFE) |
247 				 BIT(TFA989X_SYS_CTRL_AMPC));
248 }
249 
250 static void tfa989x_regulator_disable(void *data)
251 {
252 	struct tfa989x *tfa989x = data;
253 
254 	regulator_disable(tfa989x->vddd_supply);
255 }
256 
257 static int tfa989x_i2c_probe(struct i2c_client *i2c)
258 {
259 	struct device *dev = &i2c->dev;
260 	const struct tfa989x_rev *rev;
261 	struct tfa989x *tfa989x;
262 	struct regmap *regmap;
263 	unsigned int val;
264 	int ret;
265 
266 	rev = device_get_match_data(dev);
267 	if (!rev) {
268 		dev_err(dev, "unknown device revision\n");
269 		return -ENODEV;
270 	}
271 
272 	tfa989x = devm_kzalloc(dev, sizeof(*tfa989x), GFP_KERNEL);
273 	if (!tfa989x)
274 		return -ENOMEM;
275 
276 	i2c_set_clientdata(i2c, tfa989x);
277 
278 	tfa989x->vddd_supply = devm_regulator_get(dev, "vddd");
279 	if (IS_ERR(tfa989x->vddd_supply))
280 		return dev_err_probe(dev, PTR_ERR(tfa989x->vddd_supply),
281 				     "Failed to get vddd regulator\n");
282 
283 	regmap = devm_regmap_init_i2c(i2c, &tfa989x_regmap);
284 	if (IS_ERR(regmap))
285 		return PTR_ERR(regmap);
286 
287 	ret = regulator_enable(tfa989x->vddd_supply);
288 	if (ret) {
289 		dev_err(dev, "Failed to enable vddd regulator: %d\n", ret);
290 		return ret;
291 	}
292 
293 	ret = devm_add_action_or_reset(dev, tfa989x_regulator_disable, tfa989x);
294 	if (ret)
295 		return ret;
296 
297 	/* Bypass regcache for reset and init sequence */
298 	regcache_cache_bypass(regmap, true);
299 
300 	/* Dummy read to generate i2c clocks, required on some devices */
301 	regmap_read(regmap, TFA989X_REVISIONNUMBER, &val);
302 
303 	ret = regmap_read(regmap, TFA989X_REVISIONNUMBER, &val);
304 	if (ret) {
305 		dev_err(dev, "failed to read revision number: %d\n", ret);
306 		return ret;
307 	}
308 
309 	val &= TFA989X_REVISIONNUMBER_REV_MSK;
310 	if (val != rev->rev) {
311 		dev_err(dev, "invalid revision number, expected %#x, got %#x\n",
312 			rev->rev, val);
313 		return -ENODEV;
314 	}
315 
316 	ret = regmap_write(regmap, TFA989X_SYS_CTRL, BIT(TFA989X_SYS_CTRL_I2CR));
317 	if (ret) {
318 		dev_err(dev, "failed to reset I2C registers: %d\n", ret);
319 		return ret;
320 	}
321 
322 	ret = rev->init(regmap);
323 	if (ret) {
324 		dev_err(dev, "failed to initialize registers: %d\n", ret);
325 		return ret;
326 	}
327 
328 	ret = tfa989x_dsp_bypass(regmap);
329 	if (ret) {
330 		dev_err(dev, "failed to enable DSP bypass: %d\n", ret);
331 		return ret;
332 	}
333 	regcache_cache_bypass(regmap, false);
334 
335 	return devm_snd_soc_register_component(dev, &tfa989x_component,
336 					       &tfa989x_dai, 1);
337 }
338 
339 static const struct of_device_id tfa989x_of_match[] = {
340 	{ .compatible = "nxp,tfa9895", .data = &tfa9895_rev },
341 	{ .compatible = "nxp,tfa9897", .data = &tfa9897_rev },
342 	{ }
343 };
344 MODULE_DEVICE_TABLE(of, tfa989x_of_match);
345 
346 static struct i2c_driver tfa989x_i2c_driver = {
347 	.driver = {
348 		.name = "tfa989x",
349 		.of_match_table = tfa989x_of_match,
350 	},
351 	.probe_new = tfa989x_i2c_probe,
352 };
353 module_i2c_driver(tfa989x_i2c_driver);
354 
355 MODULE_DESCRIPTION("ASoC NXP/Goodix TFA989X (TFA1) driver");
356 MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
357 MODULE_LICENSE("GPL");
358