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