1 // SPDX-License-Identifier: GPL-2.0+
3 // tfa9879.c -- driver for NXP Semiconductors TFA9879
5 // Copyright (C) 2014 Axentia Technologies AB
6 // Author: Peter Rosin <peda@axentia.se>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/i2c.h>
11 #include <linux/regmap.h>
12 #include <sound/soc.h>
13 #include <sound/tlv.h>
14 #include <sound/pcm_params.h>
19 struct regmap
*regmap
;
23 static int tfa9879_hw_params(struct snd_pcm_substream
*substream
,
24 struct snd_pcm_hw_params
*params
,
25 struct snd_soc_dai
*dai
)
27 struct snd_soc_component
*component
= dai
->component
;
28 struct tfa9879_priv
*tfa9879
= snd_soc_component_get_drvdata(component
);
32 switch (params_rate(params
)) {
34 fs
= TFA9879_I2S_FS_8000
;
37 fs
= TFA9879_I2S_FS_11025
;
40 fs
= TFA9879_I2S_FS_12000
;
43 fs
= TFA9879_I2S_FS_16000
;
46 fs
= TFA9879_I2S_FS_22050
;
49 fs
= TFA9879_I2S_FS_24000
;
52 fs
= TFA9879_I2S_FS_32000
;
55 fs
= TFA9879_I2S_FS_44100
;
58 fs
= TFA9879_I2S_FS_48000
;
61 fs
= TFA9879_I2S_FS_64000
;
64 fs
= TFA9879_I2S_FS_88200
;
67 fs
= TFA9879_I2S_FS_96000
;
73 switch (params_width(params
)) {
75 i2s_set
= TFA9879_I2S_SET_LSB_J_16
;
78 i2s_set
= TFA9879_I2S_SET_LSB_J_24
;
84 if (tfa9879
->lsb_justified
)
85 snd_soc_component_update_bits(component
,
86 TFA9879_SERIAL_INTERFACE_1
,
88 i2s_set
<< TFA9879_I2S_SET_SHIFT
);
90 snd_soc_component_update_bits(component
, TFA9879_SERIAL_INTERFACE_1
,
92 fs
<< TFA9879_I2S_FS_SHIFT
);
96 static int tfa9879_digital_mute(struct snd_soc_dai
*dai
, int mute
)
98 struct snd_soc_component
*component
= dai
->component
;
100 snd_soc_component_update_bits(component
, TFA9879_MISC_CONTROL
,
102 !!mute
<< TFA9879_S_MUTE_SHIFT
);
107 static int tfa9879_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
109 struct snd_soc_component
*component
= dai
->component
;
110 struct tfa9879_priv
*tfa9879
= snd_soc_component_get_drvdata(component
);
114 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
115 case SND_SOC_DAIFMT_CBS_CFS
:
121 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
122 case SND_SOC_DAIFMT_NB_NF
:
123 sck_pol
= TFA9879_SCK_POL_NORMAL
;
125 case SND_SOC_DAIFMT_IB_NF
:
126 sck_pol
= TFA9879_SCK_POL_INVERSE
;
132 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
133 case SND_SOC_DAIFMT_I2S
:
134 tfa9879
->lsb_justified
= 0;
135 i2s_set
= TFA9879_I2S_SET_I2S_24
;
137 case SND_SOC_DAIFMT_LEFT_J
:
138 tfa9879
->lsb_justified
= 0;
139 i2s_set
= TFA9879_I2S_SET_MSB_J_24
;
141 case SND_SOC_DAIFMT_RIGHT_J
:
142 tfa9879
->lsb_justified
= 1;
143 i2s_set
= TFA9879_I2S_SET_LSB_J_24
;
149 snd_soc_component_update_bits(component
, TFA9879_SERIAL_INTERFACE_1
,
150 TFA9879_SCK_POL_MASK
,
151 sck_pol
<< TFA9879_SCK_POL_SHIFT
);
152 snd_soc_component_update_bits(component
, TFA9879_SERIAL_INTERFACE_1
,
153 TFA9879_I2S_SET_MASK
,
154 i2s_set
<< TFA9879_I2S_SET_SHIFT
);
158 static const struct reg_default tfa9879_regs
[] = {
159 { TFA9879_DEVICE_CONTROL
, 0x0000 }, /* 0x00 */
160 { TFA9879_SERIAL_INTERFACE_1
, 0x0a18 }, /* 0x01 */
161 { TFA9879_PCM_IOM2_FORMAT_1
, 0x0007 }, /* 0x02 */
162 { TFA9879_SERIAL_INTERFACE_2
, 0x0a18 }, /* 0x03 */
163 { TFA9879_PCM_IOM2_FORMAT_2
, 0x0007 }, /* 0x04 */
164 { TFA9879_EQUALIZER_A1
, 0x59dd }, /* 0x05 */
165 { TFA9879_EQUALIZER_A2
, 0xc63e }, /* 0x06 */
166 { TFA9879_EQUALIZER_B1
, 0x651a }, /* 0x07 */
167 { TFA9879_EQUALIZER_B2
, 0xe53e }, /* 0x08 */
168 { TFA9879_EQUALIZER_C1
, 0x4616 }, /* 0x09 */
169 { TFA9879_EQUALIZER_C2
, 0xd33e }, /* 0x0a */
170 { TFA9879_EQUALIZER_D1
, 0x4df3 }, /* 0x0b */
171 { TFA9879_EQUALIZER_D2
, 0xea3e }, /* 0x0c */
172 { TFA9879_EQUALIZER_E1
, 0x5ee0 }, /* 0x0d */
173 { TFA9879_EQUALIZER_E2
, 0xf93e }, /* 0x0e */
174 { TFA9879_BYPASS_CONTROL
, 0x0093 }, /* 0x0f */
175 { TFA9879_DYNAMIC_RANGE_COMPR
, 0x92ba }, /* 0x10 */
176 { TFA9879_BASS_TREBLE
, 0x12a5 }, /* 0x11 */
177 { TFA9879_HIGH_PASS_FILTER
, 0x0004 }, /* 0x12 */
178 { TFA9879_VOLUME_CONTROL
, 0x10bd }, /* 0x13 */
179 { TFA9879_MISC_CONTROL
, 0x0000 }, /* 0x14 */
182 static bool tfa9879_volatile_reg(struct device
*dev
, unsigned int reg
)
184 return reg
== TFA9879_MISC_STATUS
;
187 static const DECLARE_TLV_DB_SCALE(volume_tlv
, -7050, 50, 1);
188 static const DECLARE_TLV_DB_SCALE(tb_gain_tlv
, -1800, 200, 0);
189 static const char * const tb_freq_text
[] = {
192 static const struct soc_enum treble_freq_enum
=
193 SOC_ENUM_SINGLE(TFA9879_BASS_TREBLE
, TFA9879_F_TRBLE_SHIFT
,
194 ARRAY_SIZE(tb_freq_text
), tb_freq_text
);
195 static const struct soc_enum bass_freq_enum
=
196 SOC_ENUM_SINGLE(TFA9879_BASS_TREBLE
, TFA9879_F_BASS_SHIFT
,
197 ARRAY_SIZE(tb_freq_text
), tb_freq_text
);
199 static const struct snd_kcontrol_new tfa9879_controls
[] = {
200 SOC_SINGLE_TLV("PCM Playback Volume", TFA9879_VOLUME_CONTROL
,
201 TFA9879_VOL_SHIFT
, 0xbd, 1, volume_tlv
),
202 SOC_SINGLE_TLV("Treble Volume", TFA9879_BASS_TREBLE
,
203 TFA9879_G_TRBLE_SHIFT
, 18, 0, tb_gain_tlv
),
204 SOC_SINGLE_TLV("Bass Volume", TFA9879_BASS_TREBLE
,
205 TFA9879_G_BASS_SHIFT
, 18, 0, tb_gain_tlv
),
206 SOC_ENUM("Treble Corner Freq", treble_freq_enum
),
207 SOC_ENUM("Bass Corner Freq", bass_freq_enum
),
210 static const struct snd_soc_dapm_widget tfa9879_dapm_widgets
[] = {
211 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM
, 0, 0),
212 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM
, 0, 0),
213 SND_SOC_DAPM_DAC("DAC", NULL
, TFA9879_DEVICE_CONTROL
, TFA9879_OPMODE_SHIFT
, 0),
214 SND_SOC_DAPM_OUTPUT("LINEOUT"),
215 SND_SOC_DAPM_SUPPLY("POWER", TFA9879_DEVICE_CONTROL
, TFA9879_POWERUP_SHIFT
, 0,
219 static const struct snd_soc_dapm_route tfa9879_dapm_routes
[] = {
220 { "DAC", NULL
, "AIFINL" },
221 { "DAC", NULL
, "AIFINR" },
223 { "LINEOUT", NULL
, "DAC" },
225 { "DAC", NULL
, "POWER" },
228 static const struct snd_soc_component_driver tfa9879_component
= {
229 .controls
= tfa9879_controls
,
230 .num_controls
= ARRAY_SIZE(tfa9879_controls
),
231 .dapm_widgets
= tfa9879_dapm_widgets
,
232 .num_dapm_widgets
= ARRAY_SIZE(tfa9879_dapm_widgets
),
233 .dapm_routes
= tfa9879_dapm_routes
,
234 .num_dapm_routes
= ARRAY_SIZE(tfa9879_dapm_routes
),
236 .use_pmdown_time
= 1,
238 .non_legacy_dai_naming
= 1,
241 static const struct regmap_config tfa9879_regmap
= {
245 .volatile_reg
= tfa9879_volatile_reg
,
246 .max_register
= TFA9879_MISC_STATUS
,
247 .reg_defaults
= tfa9879_regs
,
248 .num_reg_defaults
= ARRAY_SIZE(tfa9879_regs
),
249 .cache_type
= REGCACHE_RBTREE
,
252 static const struct snd_soc_dai_ops tfa9879_dai_ops
= {
253 .hw_params
= tfa9879_hw_params
,
254 .digital_mute
= tfa9879_digital_mute
,
255 .set_fmt
= tfa9879_set_fmt
,
258 #define TFA9879_RATES SNDRV_PCM_RATE_8000_96000
260 #define TFA9879_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
261 SNDRV_PCM_FMTBIT_S24_LE)
263 static struct snd_soc_dai_driver tfa9879_dai
= {
264 .name
= "tfa9879-hifi",
266 .stream_name
= "Playback",
269 .rates
= TFA9879_RATES
,
270 .formats
= TFA9879_FORMATS
, },
271 .ops
= &tfa9879_dai_ops
,
274 static int tfa9879_i2c_probe(struct i2c_client
*i2c
)
276 struct tfa9879_priv
*tfa9879
;
279 tfa9879
= devm_kzalloc(&i2c
->dev
, sizeof(*tfa9879
), GFP_KERNEL
);
283 i2c_set_clientdata(i2c
, tfa9879
);
285 tfa9879
->regmap
= devm_regmap_init_i2c(i2c
, &tfa9879_regmap
);
286 if (IS_ERR(tfa9879
->regmap
))
287 return PTR_ERR(tfa9879
->regmap
);
289 /* Ensure the device is in reset state */
290 for (i
= 0; i
< ARRAY_SIZE(tfa9879_regs
); i
++)
291 regmap_write(tfa9879
->regmap
,
292 tfa9879_regs
[i
].reg
, tfa9879_regs
[i
].def
);
294 return devm_snd_soc_register_component(&i2c
->dev
, &tfa9879_component
,
298 static const struct i2c_device_id tfa9879_i2c_id
[] = {
302 MODULE_DEVICE_TABLE(i2c
, tfa9879_i2c_id
);
304 static const struct of_device_id tfa9879_of_match
[] = {
305 { .compatible
= "nxp,tfa9879", },
308 MODULE_DEVICE_TABLE(of
, tfa9879_of_match
);
310 static struct i2c_driver tfa9879_i2c_driver
= {
313 .of_match_table
= tfa9879_of_match
,
315 .probe_new
= tfa9879_i2c_probe
,
316 .id_table
= tfa9879_i2c_id
,
319 module_i2c_driver(tfa9879_i2c_driver
);
321 MODULE_DESCRIPTION("ASoC NXP Semiconductors TFA9879 driver");
322 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
323 MODULE_LICENSE("GPL");