2 * tfa9879.c -- driver for NXP Semiconductors TFA9879
4 * Copyright (C) 2014 Axentia Technologies AB
5 * Author: Peter Rosin <peda@axentia.se>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
20 #include <sound/pcm_params.h>
25 struct regmap
*regmap
;
29 static int tfa9879_hw_params(struct snd_pcm_substream
*substream
,
30 struct snd_pcm_hw_params
*params
,
31 struct snd_soc_dai
*dai
)
33 struct snd_soc_codec
*codec
= dai
->codec
;
34 struct tfa9879_priv
*tfa9879
= snd_soc_codec_get_drvdata(codec
);
38 switch (params_rate(params
)) {
40 fs
= TFA9879_I2S_FS_8000
;
43 fs
= TFA9879_I2S_FS_11025
;
46 fs
= TFA9879_I2S_FS_12000
;
49 fs
= TFA9879_I2S_FS_16000
;
52 fs
= TFA9879_I2S_FS_22050
;
55 fs
= TFA9879_I2S_FS_24000
;
58 fs
= TFA9879_I2S_FS_32000
;
61 fs
= TFA9879_I2S_FS_44100
;
64 fs
= TFA9879_I2S_FS_48000
;
67 fs
= TFA9879_I2S_FS_64000
;
70 fs
= TFA9879_I2S_FS_88200
;
73 fs
= TFA9879_I2S_FS_96000
;
79 switch (params_width(params
)) {
81 i2s_set
= TFA9879_I2S_SET_LSB_J_16
;
84 i2s_set
= TFA9879_I2S_SET_LSB_J_24
;
90 if (tfa9879
->lsb_justified
)
91 snd_soc_update_bits(codec
, TFA9879_SERIAL_INTERFACE_1
,
93 i2s_set
<< TFA9879_I2S_SET_SHIFT
);
95 snd_soc_update_bits(codec
, TFA9879_SERIAL_INTERFACE_1
,
97 fs
<< TFA9879_I2S_FS_SHIFT
);
101 static int tfa9879_digital_mute(struct snd_soc_dai
*dai
, int mute
)
103 struct snd_soc_codec
*codec
= dai
->codec
;
105 snd_soc_update_bits(codec
, TFA9879_MISC_CONTROL
,
107 !!mute
<< TFA9879_S_MUTE_SHIFT
);
112 static int tfa9879_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
114 struct snd_soc_codec
*codec
= dai
->codec
;
115 struct tfa9879_priv
*tfa9879
= snd_soc_codec_get_drvdata(codec
);
119 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
120 case SND_SOC_DAIFMT_CBS_CFS
:
126 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
127 case SND_SOC_DAIFMT_NB_NF
:
128 sck_pol
= TFA9879_SCK_POL_NORMAL
;
130 case SND_SOC_DAIFMT_IB_NF
:
131 sck_pol
= TFA9879_SCK_POL_INVERSE
;
137 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
138 case SND_SOC_DAIFMT_I2S
:
139 tfa9879
->lsb_justified
= 0;
140 i2s_set
= TFA9879_I2S_SET_I2S_24
;
142 case SND_SOC_DAIFMT_LEFT_J
:
143 tfa9879
->lsb_justified
= 0;
144 i2s_set
= TFA9879_I2S_SET_MSB_J_24
;
146 case SND_SOC_DAIFMT_RIGHT_J
:
147 tfa9879
->lsb_justified
= 1;
148 i2s_set
= TFA9879_I2S_SET_LSB_J_24
;
154 snd_soc_update_bits(codec
, TFA9879_SERIAL_INTERFACE_1
,
155 TFA9879_SCK_POL_MASK
,
156 sck_pol
<< TFA9879_SCK_POL_SHIFT
);
157 snd_soc_update_bits(codec
, TFA9879_SERIAL_INTERFACE_1
,
158 TFA9879_I2S_SET_MASK
,
159 i2s_set
<< TFA9879_I2S_SET_SHIFT
);
163 static const struct reg_default tfa9879_regs
[] = {
164 { TFA9879_DEVICE_CONTROL
, 0x0000 }, /* 0x00 */
165 { TFA9879_SERIAL_INTERFACE_1
, 0x0a18 }, /* 0x01 */
166 { TFA9879_PCM_IOM2_FORMAT_1
, 0x0007 }, /* 0x02 */
167 { TFA9879_SERIAL_INTERFACE_2
, 0x0a18 }, /* 0x03 */
168 { TFA9879_PCM_IOM2_FORMAT_2
, 0x0007 }, /* 0x04 */
169 { TFA9879_EQUALIZER_A1
, 0x59dd }, /* 0x05 */
170 { TFA9879_EQUALIZER_A2
, 0xc63e }, /* 0x06 */
171 { TFA9879_EQUALIZER_B1
, 0x651a }, /* 0x07 */
172 { TFA9879_EQUALIZER_B2
, 0xe53e }, /* 0x08 */
173 { TFA9879_EQUALIZER_C1
, 0x4616 }, /* 0x09 */
174 { TFA9879_EQUALIZER_C2
, 0xd33e }, /* 0x0a */
175 { TFA9879_EQUALIZER_D1
, 0x4df3 }, /* 0x0b */
176 { TFA9879_EQUALIZER_D2
, 0xea3e }, /* 0x0c */
177 { TFA9879_EQUALIZER_E1
, 0x5ee0 }, /* 0x0d */
178 { TFA9879_EQUALIZER_E2
, 0xf93e }, /* 0x0e */
179 { TFA9879_BYPASS_CONTROL
, 0x0093 }, /* 0x0f */
180 { TFA9879_DYNAMIC_RANGE_COMPR
, 0x92ba }, /* 0x10 */
181 { TFA9879_BASS_TREBLE
, 0x12a5 }, /* 0x11 */
182 { TFA9879_HIGH_PASS_FILTER
, 0x0004 }, /* 0x12 */
183 { TFA9879_VOLUME_CONTROL
, 0x10bd }, /* 0x13 */
184 { TFA9879_MISC_CONTROL
, 0x0000 }, /* 0x14 */
187 static bool tfa9879_volatile_reg(struct device
*dev
, unsigned int reg
)
189 return reg
== TFA9879_MISC_STATUS
;
192 static const DECLARE_TLV_DB_SCALE(volume_tlv
, -7050, 50, 1);
193 static const DECLARE_TLV_DB_SCALE(tb_gain_tlv
, -1800, 200, 0);
194 static const char * const tb_freq_text
[] = {
197 static const struct soc_enum treble_freq_enum
=
198 SOC_ENUM_SINGLE(TFA9879_BASS_TREBLE
, TFA9879_F_TRBLE_SHIFT
,
199 ARRAY_SIZE(tb_freq_text
), tb_freq_text
);
200 static const struct soc_enum bass_freq_enum
=
201 SOC_ENUM_SINGLE(TFA9879_BASS_TREBLE
, TFA9879_F_BASS_SHIFT
,
202 ARRAY_SIZE(tb_freq_text
), tb_freq_text
);
204 static const struct snd_kcontrol_new tfa9879_controls
[] = {
205 SOC_SINGLE_TLV("PCM Playback Volume", TFA9879_VOLUME_CONTROL
,
206 TFA9879_VOL_SHIFT
, 0xbd, 1, volume_tlv
),
207 SOC_SINGLE_TLV("Treble Volume", TFA9879_BASS_TREBLE
,
208 TFA9879_G_TRBLE_SHIFT
, 18, 0, tb_gain_tlv
),
209 SOC_SINGLE_TLV("Bass Volume", TFA9879_BASS_TREBLE
,
210 TFA9879_G_BASS_SHIFT
, 18, 0, tb_gain_tlv
),
211 SOC_ENUM("Treble Corner Freq", treble_freq_enum
),
212 SOC_ENUM("Bass Corner Freq", bass_freq_enum
),
215 static const struct snd_soc_dapm_widget tfa9879_dapm_widgets
[] = {
216 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM
, 0, 0),
217 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM
, 0, 0),
218 SND_SOC_DAPM_DAC("DAC", NULL
, TFA9879_DEVICE_CONTROL
, TFA9879_OPMODE_SHIFT
, 0),
219 SND_SOC_DAPM_OUTPUT("LINEOUT"),
220 SND_SOC_DAPM_SUPPLY("POWER", TFA9879_DEVICE_CONTROL
, TFA9879_POWERUP_SHIFT
, 0,
224 static const struct snd_soc_dapm_route tfa9879_dapm_routes
[] = {
225 { "DAC", NULL
, "AIFINL" },
226 { "DAC", NULL
, "AIFINR" },
228 { "LINEOUT", NULL
, "DAC" },
230 { "DAC", NULL
, "POWER" },
233 static const struct snd_soc_codec_driver tfa9879_codec
= {
234 .controls
= tfa9879_controls
,
235 .num_controls
= ARRAY_SIZE(tfa9879_controls
),
237 .dapm_widgets
= tfa9879_dapm_widgets
,
238 .num_dapm_widgets
= ARRAY_SIZE(tfa9879_dapm_widgets
),
239 .dapm_routes
= tfa9879_dapm_routes
,
240 .num_dapm_routes
= ARRAY_SIZE(tfa9879_dapm_routes
),
243 static const struct regmap_config tfa9879_regmap
= {
247 .volatile_reg
= tfa9879_volatile_reg
,
248 .max_register
= TFA9879_MISC_STATUS
,
249 .reg_defaults
= tfa9879_regs
,
250 .num_reg_defaults
= ARRAY_SIZE(tfa9879_regs
),
251 .cache_type
= REGCACHE_RBTREE
,
254 static const struct snd_soc_dai_ops tfa9879_dai_ops
= {
255 .hw_params
= tfa9879_hw_params
,
256 .digital_mute
= tfa9879_digital_mute
,
257 .set_fmt
= tfa9879_set_fmt
,
260 #define TFA9879_RATES SNDRV_PCM_RATE_8000_96000
262 #define TFA9879_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
263 SNDRV_PCM_FMTBIT_S24_LE)
265 static struct snd_soc_dai_driver tfa9879_dai
= {
266 .name
= "tfa9879-hifi",
268 .stream_name
= "Playback",
271 .rates
= TFA9879_RATES
,
272 .formats
= TFA9879_FORMATS
, },
273 .ops
= &tfa9879_dai_ops
,
276 static int tfa9879_i2c_probe(struct i2c_client
*i2c
,
277 const struct i2c_device_id
*id
)
279 struct tfa9879_priv
*tfa9879
;
282 tfa9879
= devm_kzalloc(&i2c
->dev
, sizeof(*tfa9879
), GFP_KERNEL
);
286 i2c_set_clientdata(i2c
, tfa9879
);
288 tfa9879
->regmap
= devm_regmap_init_i2c(i2c
, &tfa9879_regmap
);
289 if (IS_ERR(tfa9879
->regmap
))
290 return PTR_ERR(tfa9879
->regmap
);
292 /* Ensure the device is in reset state */
293 for (i
= 0; i
< ARRAY_SIZE(tfa9879_regs
); i
++)
294 regmap_write(tfa9879
->regmap
,
295 tfa9879_regs
[i
].reg
, tfa9879_regs
[i
].def
);
297 return snd_soc_register_codec(&i2c
->dev
, &tfa9879_codec
,
301 static int tfa9879_i2c_remove(struct i2c_client
*client
)
303 snd_soc_unregister_codec(&client
->dev
);
308 static const struct i2c_device_id tfa9879_i2c_id
[] = {
312 MODULE_DEVICE_TABLE(i2c
, tfa9879_i2c_id
);
314 static struct i2c_driver tfa9879_i2c_driver
= {
318 .probe
= tfa9879_i2c_probe
,
319 .remove
= tfa9879_i2c_remove
,
320 .id_table
= tfa9879_i2c_id
,
323 module_i2c_driver(tfa9879_i2c_driver
);
325 MODULE_DESCRIPTION("ASoC NXP Semiconductors TFA9879 driver");
326 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
327 MODULE_LICENSE("GPL");