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