xref: /openbmc/linux/sound/soc/codecs/tas2770.c (revision af9b2ff010f593d81e2f5fb04155e9fc25b9dfd0)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAS2770 20-W Digital Input Mono Class-D
4 // Audio Amplifier with Speaker I/V Sense
5 //
6 // Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
7 //	Author: Tracy Yi <tracy-yi@ti.com>
8 //	Frank Shi <shifu0704@thundersoft.com>
9 
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/pm.h>
16 #include <linux/i2c.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/firmware.h>
21 #include <linux/regmap.h>
22 #include <linux/of.h>
23 #include <linux/of_gpio.h>
24 #include <linux/slab.h>
25 #include <sound/soc.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
30 
31 #include "tas2770.h"
32 
33 #define TAS2770_MDELAY 0xFFFFFFFE
34 
tas2770_reset(struct tas2770_priv * tas2770)35 static void tas2770_reset(struct tas2770_priv *tas2770)
36 {
37 	if (tas2770->reset_gpio) {
38 		gpiod_set_value_cansleep(tas2770->reset_gpio, 0);
39 		msleep(20);
40 		gpiod_set_value_cansleep(tas2770->reset_gpio, 1);
41 		usleep_range(1000, 2000);
42 	}
43 
44 	snd_soc_component_write(tas2770->component, TAS2770_SW_RST,
45 		TAS2770_RST);
46 	usleep_range(1000, 2000);
47 }
48 
tas2770_update_pwr_ctrl(struct tas2770_priv * tas2770)49 static int tas2770_update_pwr_ctrl(struct tas2770_priv *tas2770)
50 {
51 	struct snd_soc_component *component = tas2770->component;
52 	unsigned int val;
53 	int ret;
54 
55 	if (tas2770->dac_powered)
56 		val = tas2770->unmuted ?
57 			TAS2770_PWR_CTRL_ACTIVE : TAS2770_PWR_CTRL_MUTE;
58 	else
59 		val = TAS2770_PWR_CTRL_SHUTDOWN;
60 
61 	ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
62 					    TAS2770_PWR_CTRL_MASK, val);
63 	if (ret < 0)
64 		return ret;
65 
66 	return 0;
67 }
68 
69 #ifdef CONFIG_PM
tas2770_codec_suspend(struct snd_soc_component * component)70 static int tas2770_codec_suspend(struct snd_soc_component *component)
71 {
72 	struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
73 	int ret = 0;
74 
75 	regcache_cache_only(tas2770->regmap, true);
76 	regcache_mark_dirty(tas2770->regmap);
77 
78 	if (tas2770->sdz_gpio) {
79 		gpiod_set_value_cansleep(tas2770->sdz_gpio, 0);
80 	} else {
81 		ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
82 						    TAS2770_PWR_CTRL_MASK,
83 						    TAS2770_PWR_CTRL_SHUTDOWN);
84 		if (ret < 0) {
85 			regcache_cache_only(tas2770->regmap, false);
86 			regcache_sync(tas2770->regmap);
87 			return ret;
88 		}
89 
90 		ret = 0;
91 	}
92 
93 	return ret;
94 }
95 
tas2770_codec_resume(struct snd_soc_component * component)96 static int tas2770_codec_resume(struct snd_soc_component *component)
97 {
98 	struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
99 	int ret;
100 
101 	if (tas2770->sdz_gpio) {
102 		gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
103 		usleep_range(1000, 2000);
104 	} else {
105 		ret = tas2770_update_pwr_ctrl(tas2770);
106 		if (ret < 0)
107 			return ret;
108 	}
109 
110 	regcache_cache_only(tas2770->regmap, false);
111 
112 	return regcache_sync(tas2770->regmap);
113 }
114 #else
115 #define tas2770_codec_suspend NULL
116 #define tas2770_codec_resume NULL
117 #endif
118 
119 static const char * const tas2770_ASI1_src[] = {
120 	"I2C offset", "Left", "Right", "LeftRightDiv2",
121 };
122 
123 static SOC_ENUM_SINGLE_DECL(
124 	tas2770_ASI1_src_enum, TAS2770_TDM_CFG_REG2,
125 	4, tas2770_ASI1_src);
126 
127 static const struct snd_kcontrol_new tas2770_asi1_mux =
128 	SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum);
129 
tas2770_dac_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)130 static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
131 			     struct snd_kcontrol *kcontrol, int event)
132 {
133 	struct snd_soc_component *component =
134 			snd_soc_dapm_to_component(w->dapm);
135 	struct tas2770_priv *tas2770 =
136 			snd_soc_component_get_drvdata(component);
137 	int ret;
138 
139 	switch (event) {
140 	case SND_SOC_DAPM_POST_PMU:
141 		tas2770->dac_powered = 1;
142 		ret = tas2770_update_pwr_ctrl(tas2770);
143 		break;
144 	case SND_SOC_DAPM_PRE_PMD:
145 		tas2770->dac_powered = 0;
146 		ret = tas2770_update_pwr_ctrl(tas2770);
147 		break;
148 	default:
149 		dev_err(tas2770->dev, "Not supported evevt\n");
150 		return -EINVAL;
151 	}
152 
153 	return ret;
154 }
155 
156 static const struct snd_kcontrol_new isense_switch =
157 	SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 3, 1, 1);
158 static const struct snd_kcontrol_new vsense_switch =
159 	SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 2, 1, 1);
160 
sense_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)161 static int sense_event(struct snd_soc_dapm_widget *w,
162 			struct snd_kcontrol *kcontrol, int event)
163 {
164 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
165 	struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
166 
167 	/*
168 	 * Powering up ISENSE/VSENSE requires a trip through the shutdown state.
169 	 * Do that here to ensure that our changes are applied properly, otherwise
170 	 * we might end up with non-functional IVSENSE if playback started earlier,
171 	 * which would break software speaker protection.
172 	 */
173 	switch (event) {
174 	case SND_SOC_DAPM_PRE_REG:
175 		return snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
176 						    TAS2770_PWR_CTRL_MASK,
177 						    TAS2770_PWR_CTRL_SHUTDOWN);
178 	case SND_SOC_DAPM_POST_REG:
179 		return tas2770_update_pwr_ctrl(tas2770);
180 	default:
181 		return 0;
182 	}
183 }
184 
185 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = {
186 	SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
187 	SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2770_asi1_mux),
188 	SND_SOC_DAPM_SWITCH_E("ISENSE", TAS2770_PWR_CTRL, 3, 1, &isense_switch,
189 		sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
190 	SND_SOC_DAPM_SWITCH_E("VSENSE", TAS2770_PWR_CTRL, 2, 1, &vsense_switch,
191 		sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
192 	SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event,
193 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
194 	SND_SOC_DAPM_OUTPUT("OUT"),
195 	SND_SOC_DAPM_SIGGEN("VMON"),
196 	SND_SOC_DAPM_SIGGEN("IMON")
197 };
198 
199 static const struct snd_soc_dapm_route tas2770_audio_map[] = {
200 	{"ASI1 Sel", "I2C offset", "ASI1"},
201 	{"ASI1 Sel", "Left", "ASI1"},
202 	{"ASI1 Sel", "Right", "ASI1"},
203 	{"ASI1 Sel", "LeftRightDiv2", "ASI1"},
204 	{"DAC", NULL, "ASI1 Sel"},
205 	{"OUT", NULL, "DAC"},
206 	{"ISENSE", "Switch", "IMON"},
207 	{"VSENSE", "Switch", "VMON"},
208 };
209 
tas2770_mute(struct snd_soc_dai * dai,int mute,int direction)210 static int tas2770_mute(struct snd_soc_dai *dai, int mute, int direction)
211 {
212 	struct snd_soc_component *component = dai->component;
213 	struct tas2770_priv *tas2770 =
214 			snd_soc_component_get_drvdata(component);
215 
216 	tas2770->unmuted = !mute;
217 	return tas2770_update_pwr_ctrl(tas2770);
218 }
219 
tas2770_set_bitwidth(struct tas2770_priv * tas2770,int bitwidth)220 static int tas2770_set_bitwidth(struct tas2770_priv *tas2770, int bitwidth)
221 {
222 	int ret;
223 	struct snd_soc_component *component = tas2770->component;
224 
225 	switch (bitwidth) {
226 	case SNDRV_PCM_FORMAT_S16_LE:
227 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
228 						    TAS2770_TDM_CFG_REG2_RXW_MASK,
229 						    TAS2770_TDM_CFG_REG2_RXW_16BITS);
230 		tas2770->v_sense_slot = tas2770->i_sense_slot + 2;
231 		break;
232 	case SNDRV_PCM_FORMAT_S24_LE:
233 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
234 						    TAS2770_TDM_CFG_REG2_RXW_MASK,
235 						    TAS2770_TDM_CFG_REG2_RXW_24BITS);
236 		tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
237 		break;
238 	case SNDRV_PCM_FORMAT_S32_LE:
239 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
240 						    TAS2770_TDM_CFG_REG2_RXW_MASK,
241 						    TAS2770_TDM_CFG_REG2_RXW_32BITS);
242 		tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
243 		break;
244 
245 	default:
246 		return -EINVAL;
247 	}
248 
249 	if (ret < 0)
250 		return ret;
251 
252 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG5,
253 					    TAS2770_TDM_CFG_REG5_VSNS_MASK |
254 					    TAS2770_TDM_CFG_REG5_50_MASK,
255 					    TAS2770_TDM_CFG_REG5_VSNS_ENABLE |
256 		tas2770->v_sense_slot);
257 	if (ret < 0)
258 		return ret;
259 
260 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG6,
261 					    TAS2770_TDM_CFG_REG6_ISNS_MASK |
262 					    TAS2770_TDM_CFG_REG6_50_MASK,
263 					    TAS2770_TDM_CFG_REG6_ISNS_ENABLE |
264 					    tas2770->i_sense_slot);
265 	if (ret < 0)
266 		return ret;
267 
268 	return 0;
269 }
270 
tas2770_set_samplerate(struct tas2770_priv * tas2770,int samplerate)271 static int tas2770_set_samplerate(struct tas2770_priv *tas2770, int samplerate)
272 {
273 	struct snd_soc_component *component = tas2770->component;
274 	int ramp_rate_val;
275 	int ret;
276 
277 	switch (samplerate) {
278 	case 48000:
279 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
280 				TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
281 		break;
282 	case 44100:
283 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
284 				TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
285 		break;
286 	case 96000:
287 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
288 				TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
289 		break;
290 	case 88200:
291 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
292 				TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
293 		break;
294 	case 192000:
295 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
296 				TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
297 		break;
298 	case 176400:
299 		ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
300 				TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
301 		break;
302 	default:
303 		return -EINVAL;
304 	}
305 
306 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
307 					    TAS2770_TDM_CFG_REG0_SMP_MASK |
308 					    TAS2770_TDM_CFG_REG0_31_MASK,
309 					    ramp_rate_val);
310 	if (ret < 0)
311 		return ret;
312 
313 	return 0;
314 }
315 
tas2770_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)316 static int tas2770_hw_params(struct snd_pcm_substream *substream,
317 			     struct snd_pcm_hw_params *params,
318 			     struct snd_soc_dai *dai)
319 {
320 	struct snd_soc_component *component = dai->component;
321 	struct tas2770_priv *tas2770 =
322 			snd_soc_component_get_drvdata(component);
323 	int ret;
324 
325 	ret = tas2770_set_bitwidth(tas2770, params_format(params));
326 	if (ret)
327 		return ret;
328 
329 	return tas2770_set_samplerate(tas2770, params_rate(params));
330 }
331 
tas2770_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)332 static int tas2770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
333 {
334 	struct snd_soc_component *component = dai->component;
335 	struct tas2770_priv *tas2770 =
336 			snd_soc_component_get_drvdata(component);
337 	u8 tdm_rx_start_slot = 0, invert_fpol = 0, fpol_preinv = 0, asi_cfg_1 = 0;
338 	int ret;
339 
340 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
341 	case SND_SOC_DAIFMT_CBC_CFC:
342 		break;
343 	default:
344 		dev_err(tas2770->dev, "ASI invalid DAI clocking\n");
345 		return -EINVAL;
346 	}
347 
348 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
349 	case SND_SOC_DAIFMT_NB_IF:
350 		invert_fpol = 1;
351 		fallthrough;
352 	case SND_SOC_DAIFMT_NB_NF:
353 		asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_RSING;
354 		break;
355 	case SND_SOC_DAIFMT_IB_IF:
356 		invert_fpol = 1;
357 		fallthrough;
358 	case SND_SOC_DAIFMT_IB_NF:
359 		asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_FALING;
360 		break;
361 	default:
362 		dev_err(tas2770->dev, "ASI format Inverse is not found\n");
363 		return -EINVAL;
364 	}
365 
366 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
367 					    TAS2770_TDM_CFG_REG1_RX_MASK,
368 					    asi_cfg_1);
369 	if (ret < 0)
370 		return ret;
371 
372 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
373 	case SND_SOC_DAIFMT_I2S:
374 		tdm_rx_start_slot = 1;
375 		fpol_preinv = 0;
376 		break;
377 	case SND_SOC_DAIFMT_DSP_A:
378 		tdm_rx_start_slot = 0;
379 		fpol_preinv = 1;
380 		break;
381 	case SND_SOC_DAIFMT_DSP_B:
382 		tdm_rx_start_slot = 1;
383 		fpol_preinv = 1;
384 		break;
385 	case SND_SOC_DAIFMT_LEFT_J:
386 		tdm_rx_start_slot = 0;
387 		fpol_preinv = 1;
388 		break;
389 	default:
390 		dev_err(tas2770->dev,
391 			"DAI Format is not found, fmt=0x%x\n", fmt);
392 		return -EINVAL;
393 	}
394 
395 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
396 					    TAS2770_TDM_CFG_REG1_MASK,
397 					    (tdm_rx_start_slot << TAS2770_TDM_CFG_REG1_51_SHIFT));
398 	if (ret < 0)
399 		return ret;
400 
401 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
402 					    TAS2770_TDM_CFG_REG0_FPOL_MASK,
403 					    (fpol_preinv ^ invert_fpol)
404 					     ? TAS2770_TDM_CFG_REG0_FPOL_RSING
405 					     : TAS2770_TDM_CFG_REG0_FPOL_FALING);
406 	if (ret < 0)
407 		return ret;
408 
409 	return 0;
410 }
411 
tas2770_set_dai_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)412 static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai,
413 				unsigned int tx_mask,
414 				unsigned int rx_mask,
415 				int slots, int slot_width)
416 {
417 	struct snd_soc_component *component = dai->component;
418 	int left_slot, right_slot;
419 	int ret;
420 
421 	if (tx_mask == 0 || rx_mask != 0)
422 		return -EINVAL;
423 
424 	left_slot = __ffs(tx_mask);
425 	tx_mask &= ~(1 << left_slot);
426 	if (tx_mask == 0) {
427 		right_slot = left_slot;
428 	} else {
429 		right_slot = __ffs(tx_mask);
430 		tx_mask &= ~(1 << right_slot);
431 	}
432 
433 	if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
434 		return -EINVAL;
435 
436 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
437 					    TAS2770_TDM_CFG_REG3_30_MASK,
438 					    (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT));
439 	if (ret < 0)
440 		return ret;
441 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
442 					    TAS2770_TDM_CFG_REG3_RXS_MASK,
443 					    (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT));
444 	if (ret < 0)
445 		return ret;
446 
447 	switch (slot_width) {
448 	case 16:
449 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
450 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
451 						    TAS2770_TDM_CFG_REG2_RXS_16BITS);
452 		break;
453 	case 24:
454 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
455 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
456 						    TAS2770_TDM_CFG_REG2_RXS_24BITS);
457 		break;
458 	case 32:
459 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
460 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
461 						    TAS2770_TDM_CFG_REG2_RXS_32BITS);
462 		break;
463 	case 0:
464 		/* Do not change slot width */
465 		ret = 0;
466 		break;
467 	default:
468 		ret = -EINVAL;
469 	}
470 
471 	if (ret < 0)
472 		return ret;
473 
474 	return 0;
475 }
476 
477 static const struct snd_soc_dai_ops tas2770_dai_ops = {
478 	.mute_stream = tas2770_mute,
479 	.hw_params  = tas2770_hw_params,
480 	.set_fmt    = tas2770_set_fmt,
481 	.set_tdm_slot = tas2770_set_dai_tdm_slot,
482 	.no_capture_mute = 1,
483 };
484 
485 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
486 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
487 
488 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
489 					   SNDRV_PCM_RATE_96000 |\
490 					    SNDRV_PCM_RATE_192000\
491 					  )
492 
493 static struct snd_soc_dai_driver tas2770_dai_driver[] = {
494 	{
495 		.name = "tas2770 ASI1",
496 		.id = 0,
497 		.playback = {
498 			.stream_name    = "ASI1 Playback",
499 			.channels_min   = 1,
500 			.channels_max   = 2,
501 			.rates      = TAS2770_RATES,
502 			.formats    = TAS2770_FORMATS,
503 		},
504 		.capture = {
505 			.stream_name    = "ASI1 Capture",
506 			.channels_min   = 0,
507 			.channels_max   = 2,
508 			.rates          = TAS2770_RATES,
509 			.formats    = TAS2770_FORMATS,
510 		},
511 		.ops = &tas2770_dai_ops,
512 		.symmetric_rate = 1,
513 	},
514 };
515 
516 static const struct regmap_config tas2770_i2c_regmap;
517 
tas2770_codec_probe(struct snd_soc_component * component)518 static int tas2770_codec_probe(struct snd_soc_component *component)
519 {
520 	struct tas2770_priv *tas2770 =
521 			snd_soc_component_get_drvdata(component);
522 
523 	tas2770->component = component;
524 
525 	if (tas2770->sdz_gpio) {
526 		gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
527 		usleep_range(1000, 2000);
528 	}
529 
530 	tas2770_reset(tas2770);
531 	regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap);
532 
533 	return 0;
534 }
535 
536 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
537 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -10050, 50, 0);
538 
539 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
540 	SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2,
541 		       0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume),
542 	SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0,
543 		       tas2770_digital_tlv),
544 };
545 
546 static const struct snd_soc_component_driver soc_component_driver_tas2770 = {
547 	.probe			= tas2770_codec_probe,
548 	.suspend		= tas2770_codec_suspend,
549 	.resume			= tas2770_codec_resume,
550 	.controls		= tas2770_snd_controls,
551 	.num_controls		= ARRAY_SIZE(tas2770_snd_controls),
552 	.dapm_widgets		= tas2770_dapm_widgets,
553 	.num_dapm_widgets	= ARRAY_SIZE(tas2770_dapm_widgets),
554 	.dapm_routes		= tas2770_audio_map,
555 	.num_dapm_routes	= ARRAY_SIZE(tas2770_audio_map),
556 	.idle_bias_on		= 1,
557 	.endianness		= 1,
558 };
559 
tas2770_register_codec(struct tas2770_priv * tas2770)560 static int tas2770_register_codec(struct tas2770_priv *tas2770)
561 {
562 	return devm_snd_soc_register_component(tas2770->dev,
563 		&soc_component_driver_tas2770,
564 		tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
565 }
566 
567 static const struct reg_default tas2770_reg_defaults[] = {
568 	{ TAS2770_PAGE, 0x00 },
569 	{ TAS2770_SW_RST, 0x00 },
570 	{ TAS2770_PWR_CTRL, 0x0e },
571 	{ TAS2770_PLAY_CFG_REG0, 0x10 },
572 	{ TAS2770_PLAY_CFG_REG1, 0x01 },
573 	{ TAS2770_PLAY_CFG_REG2, 0x00 },
574 	{ TAS2770_MSC_CFG_REG0, 0x07 },
575 	{ TAS2770_TDM_CFG_REG1, 0x02 },
576 	{ TAS2770_TDM_CFG_REG2, 0x0a },
577 	{ TAS2770_TDM_CFG_REG3, 0x10 },
578 	{ TAS2770_INT_MASK_REG0, 0xfc },
579 	{ TAS2770_INT_MASK_REG1, 0xb1 },
580 	{ TAS2770_INT_CFG, 0x05 },
581 	{ TAS2770_MISC_IRQ, 0x81 },
582 	{ TAS2770_CLK_CGF, 0x0c },
583 
584 };
585 
tas2770_volatile(struct device * dev,unsigned int reg)586 static bool tas2770_volatile(struct device *dev, unsigned int reg)
587 {
588 	switch (reg) {
589 	case TAS2770_PAGE: /* regmap implementation requires this */
590 	case TAS2770_SW_RST: /* always clears after write */
591 	case TAS2770_BO_PRV_REG0:/* has a self clearing bit */
592 	case TAS2770_LVE_INT_REG0:
593 	case TAS2770_LVE_INT_REG1:
594 	case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */
595 	case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */
596 	case TAS2770_VBAT_MSB:
597 	case TAS2770_VBAT_LSB:
598 	case TAS2770_TEMP_MSB:
599 	case TAS2770_TEMP_LSB:
600 		return true;
601 	}
602 
603 	return false;
604 }
605 
tas2770_writeable(struct device * dev,unsigned int reg)606 static bool tas2770_writeable(struct device *dev, unsigned int reg)
607 {
608 	switch (reg) {
609 	case TAS2770_LVE_INT_REG0:
610 	case TAS2770_LVE_INT_REG1:
611 	case TAS2770_LAT_INT_REG0:
612 	case TAS2770_LAT_INT_REG1:
613 	case TAS2770_VBAT_MSB:
614 	case TAS2770_VBAT_LSB:
615 	case TAS2770_TEMP_MSB:
616 	case TAS2770_TEMP_LSB:
617 	case TAS2770_TDM_CLK_DETC:
618 	case TAS2770_REV_AND_GPID:
619 		return false;
620 	}
621 
622 	return true;
623 }
624 
625 static const struct regmap_range_cfg tas2770_regmap_ranges[] = {
626 	{
627 		.range_min = 0,
628 		.range_max = 1 * 128,
629 		.selector_reg = TAS2770_PAGE,
630 		.selector_mask = 0xff,
631 		.selector_shift = 0,
632 		.window_start = 0,
633 		.window_len = 128,
634 	},
635 };
636 
637 static const struct regmap_config tas2770_i2c_regmap = {
638 	.reg_bits = 8,
639 	.val_bits = 8,
640 	.writeable_reg = tas2770_writeable,
641 	.volatile_reg = tas2770_volatile,
642 	.reg_defaults = tas2770_reg_defaults,
643 	.num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults),
644 	.cache_type = REGCACHE_RBTREE,
645 	.ranges = tas2770_regmap_ranges,
646 	.num_ranges = ARRAY_SIZE(tas2770_regmap_ranges),
647 	.max_register = 1 * 128,
648 };
649 
tas2770_parse_dt(struct device * dev,struct tas2770_priv * tas2770)650 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770)
651 {
652 	int rc = 0;
653 
654 	rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
655 				      &tas2770->i_sense_slot);
656 	if (rc) {
657 		dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
658 			 "ti,imon-slot-no");
659 
660 		tas2770->i_sense_slot = 0;
661 	}
662 
663 	rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
664 				      &tas2770->v_sense_slot);
665 	if (rc) {
666 		dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
667 			 "ti,vmon-slot-no");
668 
669 		tas2770->v_sense_slot = 2;
670 	}
671 
672 	tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
673 	if (IS_ERR(tas2770->sdz_gpio)) {
674 		if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER)
675 			return -EPROBE_DEFER;
676 
677 		tas2770->sdz_gpio = NULL;
678 	}
679 
680 	return 0;
681 }
682 
tas2770_i2c_probe(struct i2c_client * client)683 static int tas2770_i2c_probe(struct i2c_client *client)
684 {
685 	struct tas2770_priv *tas2770;
686 	int result;
687 
688 	tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv),
689 			       GFP_KERNEL);
690 	if (!tas2770)
691 		return -ENOMEM;
692 
693 	tas2770->dev = &client->dev;
694 	i2c_set_clientdata(client, tas2770);
695 	dev_set_drvdata(&client->dev, tas2770);
696 
697 	tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap);
698 	if (IS_ERR(tas2770->regmap)) {
699 		result = PTR_ERR(tas2770->regmap);
700 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
701 			result);
702 		return result;
703 	}
704 
705 	if (client->dev.of_node) {
706 		result = tas2770_parse_dt(&client->dev, tas2770);
707 		if (result) {
708 			dev_err(tas2770->dev, "%s: Failed to parse devicetree\n",
709 				__func__);
710 			return result;
711 		}
712 	}
713 
714 	tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset",
715 						      GPIOD_OUT_HIGH);
716 	if (IS_ERR(tas2770->reset_gpio)) {
717 		if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) {
718 			tas2770->reset_gpio = NULL;
719 			return -EPROBE_DEFER;
720 		}
721 	}
722 
723 	result = tas2770_register_codec(tas2770);
724 	if (result)
725 		dev_err(tas2770->dev, "Register codec failed.\n");
726 
727 	return result;
728 }
729 
730 static const struct i2c_device_id tas2770_i2c_id[] = {
731 	{ "tas2770", 0},
732 	{ }
733 };
734 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id);
735 
736 #if defined(CONFIG_OF)
737 static const struct of_device_id tas2770_of_match[] = {
738 	{ .compatible = "ti,tas2770" },
739 	{},
740 };
741 MODULE_DEVICE_TABLE(of, tas2770_of_match);
742 #endif
743 
744 static struct i2c_driver tas2770_i2c_driver = {
745 	.driver = {
746 		.name   = "tas2770",
747 		.of_match_table = of_match_ptr(tas2770_of_match),
748 	},
749 	.probe      = tas2770_i2c_probe,
750 	.id_table   = tas2770_i2c_id,
751 };
752 module_i2c_driver(tas2770_i2c_driver);
753 
754 MODULE_AUTHOR("Shi Fu <shifu0704@thundersoft.com>");
755 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver");
756 MODULE_LICENSE("GPL v2");
757