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