xref: /openbmc/linux/sound/soc/codecs/tfa9879.c (revision 04a7279f)
1 /*
2  * tfa9879.c  --  driver for NXP Semiconductors TFA9879
3  *
4  * Copyright (C) 2014 Axentia Technologies AB
5  * Author: Peter Rosin <peda@axentia.se>
6  *
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.
11  *
12  */
13 
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>
21 
22 #include "tfa9879.h"
23 
24 struct tfa9879_priv {
25 	struct regmap *regmap;
26 	int lsb_justified;
27 };
28 
29 static int tfa9879_hw_params(struct snd_pcm_substream *substream,
30 			     struct snd_pcm_hw_params *params,
31 			     struct snd_soc_dai *dai)
32 {
33 	struct snd_soc_codec *codec = dai->codec;
34 	struct tfa9879_priv *tfa9879 = snd_soc_codec_get_drvdata(codec);
35 	int fs;
36 	int i2s_set = 0;
37 
38 	switch (params_rate(params)) {
39 	case 8000:
40 		fs = TFA9879_I2S_FS_8000;
41 		break;
42 	case 11025:
43 		fs = TFA9879_I2S_FS_11025;
44 		break;
45 	case 12000:
46 		fs = TFA9879_I2S_FS_12000;
47 		break;
48 	case 16000:
49 		fs = TFA9879_I2S_FS_16000;
50 		break;
51 	case 22050:
52 		fs = TFA9879_I2S_FS_22050;
53 		break;
54 	case 24000:
55 		fs = TFA9879_I2S_FS_24000;
56 		break;
57 	case 32000:
58 		fs = TFA9879_I2S_FS_32000;
59 		break;
60 	case 44100:
61 		fs = TFA9879_I2S_FS_44100;
62 		break;
63 	case 48000:
64 		fs = TFA9879_I2S_FS_48000;
65 		break;
66 	case 64000:
67 		fs = TFA9879_I2S_FS_64000;
68 		break;
69 	case 88200:
70 		fs = TFA9879_I2S_FS_88200;
71 		break;
72 	case 96000:
73 		fs = TFA9879_I2S_FS_96000;
74 		break;
75 	default:
76 		return -EINVAL;
77 	}
78 
79 	switch (params_width(params)) {
80 	case 16:
81 		i2s_set = TFA9879_I2S_SET_LSB_J_16;
82 		break;
83 	case 24:
84 		i2s_set = TFA9879_I2S_SET_LSB_J_24;
85 		break;
86 	default:
87 		return -EINVAL;
88 	}
89 
90 	if (tfa9879->lsb_justified)
91 		snd_soc_update_bits(codec, TFA9879_SERIAL_INTERFACE_1,
92 				    TFA9879_I2S_SET_MASK,
93 				    i2s_set << TFA9879_I2S_SET_SHIFT);
94 
95 	snd_soc_update_bits(codec, TFA9879_SERIAL_INTERFACE_1,
96 			    TFA9879_I2S_FS_MASK,
97 			    fs << TFA9879_I2S_FS_SHIFT);
98 	return 0;
99 }
100 
101 static int tfa9879_digital_mute(struct snd_soc_dai *dai, int mute)
102 {
103 	struct snd_soc_codec *codec = dai->codec;
104 
105 	snd_soc_update_bits(codec, TFA9879_MISC_CONTROL,
106 			    TFA9879_S_MUTE_MASK,
107 			    !!mute << TFA9879_S_MUTE_SHIFT);
108 
109 	return 0;
110 }
111 
112 static int tfa9879_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
113 {
114 	struct snd_soc_codec *codec = dai->codec;
115 	struct tfa9879_priv *tfa9879 = snd_soc_codec_get_drvdata(codec);
116 	int i2s_set;
117 	int sck_pol;
118 
119 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
120 	case SND_SOC_DAIFMT_CBS_CFS:
121 		break;
122 	default:
123 		return -EINVAL;
124 	}
125 
126 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
127 	case SND_SOC_DAIFMT_NB_NF:
128 		sck_pol = TFA9879_SCK_POL_NORMAL;
129 		break;
130 	case SND_SOC_DAIFMT_IB_NF:
131 		sck_pol = TFA9879_SCK_POL_INVERSE;
132 		break;
133 	default:
134 		return -EINVAL;
135 	}
136 
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;
141 		break;
142 	case SND_SOC_DAIFMT_LEFT_J:
143 		tfa9879->lsb_justified = 0;
144 		i2s_set = TFA9879_I2S_SET_MSB_J_24;
145 		break;
146 	case SND_SOC_DAIFMT_RIGHT_J:
147 		tfa9879->lsb_justified = 1;
148 		i2s_set = TFA9879_I2S_SET_LSB_J_24;
149 		break;
150 	default:
151 		return -EINVAL;
152 	}
153 
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);
160 	return 0;
161 }
162 
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 */
185 };
186 
187 static bool tfa9879_volatile_reg(struct device *dev, unsigned int reg)
188 {
189 	return reg == TFA9879_MISC_STATUS;
190 }
191 
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[] = {
195 	"Low", "Mid", "High"
196 };
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);
203 
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),
213 };
214 
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,
221 		    NULL, 0),
222 };
223 
224 static const struct snd_soc_dapm_route tfa9879_dapm_routes[] = {
225 	{ "DAC", NULL, "AIFINL" },
226 	{ "DAC", NULL, "AIFINR" },
227 
228 	{ "LINEOUT", NULL, "DAC" },
229 
230 	{ "DAC", NULL, "POWER" },
231 };
232 
233 static const struct snd_soc_codec_driver tfa9879_codec = {
234 	.component_driver = {
235 		.controls		= tfa9879_controls,
236 		.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),
241 	},
242 };
243 
244 static const struct regmap_config tfa9879_regmap = {
245 	.reg_bits = 8,
246 	.val_bits = 16,
247 
248 	.volatile_reg = tfa9879_volatile_reg,
249 	.max_register = TFA9879_MISC_STATUS,
250 	.reg_defaults = tfa9879_regs,
251 	.num_reg_defaults = ARRAY_SIZE(tfa9879_regs),
252 	.cache_type = REGCACHE_RBTREE,
253 };
254 
255 static const struct snd_soc_dai_ops tfa9879_dai_ops = {
256 	.hw_params = tfa9879_hw_params,
257 	.digital_mute = tfa9879_digital_mute,
258 	.set_fmt = tfa9879_set_fmt,
259 };
260 
261 #define TFA9879_RATES SNDRV_PCM_RATE_8000_96000
262 
263 #define TFA9879_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
264 			 SNDRV_PCM_FMTBIT_S24_LE)
265 
266 static struct snd_soc_dai_driver tfa9879_dai = {
267 	.name = "tfa9879-hifi",
268 	.playback = {
269 		.stream_name = "Playback",
270 		.channels_min = 2,
271 		.channels_max = 2,
272 		.rates = TFA9879_RATES,
273 		.formats = TFA9879_FORMATS, },
274 	.ops = &tfa9879_dai_ops,
275 };
276 
277 static int tfa9879_i2c_probe(struct i2c_client *i2c,
278 			     const struct i2c_device_id *id)
279 {
280 	struct tfa9879_priv *tfa9879;
281 	int i;
282 
283 	tfa9879 = devm_kzalloc(&i2c->dev, sizeof(*tfa9879), GFP_KERNEL);
284 	if (!tfa9879)
285 		return -ENOMEM;
286 
287 	i2c_set_clientdata(i2c, tfa9879);
288 
289 	tfa9879->regmap = devm_regmap_init_i2c(i2c, &tfa9879_regmap);
290 	if (IS_ERR(tfa9879->regmap))
291 		return PTR_ERR(tfa9879->regmap);
292 
293 	/* Ensure the device is in reset state */
294 	for (i = 0; i < ARRAY_SIZE(tfa9879_regs); i++)
295 		regmap_write(tfa9879->regmap,
296 			     tfa9879_regs[i].reg, tfa9879_regs[i].def);
297 
298 	return snd_soc_register_codec(&i2c->dev, &tfa9879_codec,
299 				      &tfa9879_dai, 1);
300 }
301 
302 static int tfa9879_i2c_remove(struct i2c_client *client)
303 {
304 	snd_soc_unregister_codec(&client->dev);
305 
306 	return 0;
307 }
308 
309 static const struct i2c_device_id tfa9879_i2c_id[] = {
310 	{ "tfa9879", 0 },
311 	{ }
312 };
313 MODULE_DEVICE_TABLE(i2c, tfa9879_i2c_id);
314 
315 static const struct of_device_id tfa9879_of_match[] = {
316 	{ .compatible = "nxp,tfa9879", },
317 	{ }
318 };
319 
320 static struct i2c_driver tfa9879_i2c_driver = {
321 	.driver = {
322 		.name = "tfa9879",
323 		.of_match_table = tfa9879_of_match,
324 	},
325 	.probe = tfa9879_i2c_probe,
326 	.remove = tfa9879_i2c_remove,
327 	.id_table = tfa9879_i2c_id,
328 };
329 
330 module_i2c_driver(tfa9879_i2c_driver);
331 
332 MODULE_DESCRIPTION("ASoC NXP Semiconductors TFA9879 driver");
333 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
334 MODULE_LICENSE("GPL");
335