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