• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 0;
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 
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 
tas2770_mute(struct snd_soc_dai * dai,int mute,int direction)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 
tas2770_set_bitwidth(struct tas2770_priv * tas2770,int bitwidth)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 
tas2770_set_samplerate(struct tas2770_priv * tas2770,int samplerate)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 
tas2770_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
tas2770_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)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_MASTER_MASK) {
315 	case SND_SOC_DAIFMT_CBS_CFS:
316 		break;
317 	default:
318 		dev_err(tas2770->dev, "ASI format master is not found\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 
tas2770_set_dai_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)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 	left_slot = __ffs(tx_mask);
399 	tx_mask &= ~(1 << left_slot);
400 	if (tx_mask == 0) {
401 		right_slot = left_slot;
402 	} else {
403 		right_slot = __ffs(tx_mask);
404 		tx_mask &= ~(1 << right_slot);
405 	}
406 
407 	if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
408 		return -EINVAL;
409 
410 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
411 					    TAS2770_TDM_CFG_REG3_30_MASK,
412 					    (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT));
413 	if (ret < 0)
414 		return ret;
415 	ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
416 					    TAS2770_TDM_CFG_REG3_RXS_MASK,
417 					    (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT));
418 	if (ret < 0)
419 		return ret;
420 
421 	switch (slot_width) {
422 	case 16:
423 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
424 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
425 						    TAS2770_TDM_CFG_REG2_RXS_16BITS);
426 		break;
427 	case 24:
428 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
429 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
430 						    TAS2770_TDM_CFG_REG2_RXS_24BITS);
431 		break;
432 	case 32:
433 		ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
434 						    TAS2770_TDM_CFG_REG2_RXS_MASK,
435 						    TAS2770_TDM_CFG_REG2_RXS_32BITS);
436 		break;
437 	case 0:
438 		/* Do not change slot width */
439 		ret = 0;
440 		break;
441 	default:
442 		ret = -EINVAL;
443 	}
444 
445 	if (ret < 0)
446 		return ret;
447 
448 	return 0;
449 }
450 
451 static struct snd_soc_dai_ops tas2770_dai_ops = {
452 	.mute_stream = tas2770_mute,
453 	.hw_params  = tas2770_hw_params,
454 	.set_fmt    = tas2770_set_fmt,
455 	.set_tdm_slot = tas2770_set_dai_tdm_slot,
456 	.no_capture_mute = 1,
457 };
458 
459 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
460 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
461 
462 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
463 					   SNDRV_PCM_RATE_96000 |\
464 					    SNDRV_PCM_RATE_192000\
465 					  )
466 
467 static struct snd_soc_dai_driver tas2770_dai_driver[] = {
468 	{
469 		.name = "tas2770 ASI1",
470 		.id = 0,
471 		.playback = {
472 			.stream_name    = "ASI1 Playback",
473 			.channels_min   = 1,
474 			.channels_max   = 2,
475 			.rates      = TAS2770_RATES,
476 			.formats    = TAS2770_FORMATS,
477 		},
478 		.capture = {
479 			.stream_name    = "ASI1 Capture",
480 			.channels_min   = 0,
481 			.channels_max   = 2,
482 			.rates          = TAS2770_RATES,
483 			.formats    = TAS2770_FORMATS,
484 		},
485 		.ops = &tas2770_dai_ops,
486 		.symmetric_rates = 1,
487 	},
488 };
489 
490 static const struct regmap_config tas2770_i2c_regmap;
491 
tas2770_codec_probe(struct snd_soc_component * component)492 static int tas2770_codec_probe(struct snd_soc_component *component)
493 {
494 	struct tas2770_priv *tas2770 =
495 			snd_soc_component_get_drvdata(component);
496 
497 	tas2770->component = component;
498 
499 	if (tas2770->sdz_gpio) {
500 		gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
501 		usleep_range(1000, 2000);
502 	}
503 
504 	tas2770_reset(tas2770);
505 	regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap);
506 
507 	return 0;
508 }
509 
510 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
511 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -12750, 50, 0);
512 
513 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
514 	SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2,
515 		       0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume),
516 	SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0,
517 		       tas2770_digital_tlv),
518 };
519 
520 static const struct snd_soc_component_driver soc_component_driver_tas2770 = {
521 	.probe			= tas2770_codec_probe,
522 	.suspend		= tas2770_codec_suspend,
523 	.resume			= tas2770_codec_resume,
524 	.controls		= tas2770_snd_controls,
525 	.num_controls		= ARRAY_SIZE(tas2770_snd_controls),
526 	.dapm_widgets		= tas2770_dapm_widgets,
527 	.num_dapm_widgets	= ARRAY_SIZE(tas2770_dapm_widgets),
528 	.dapm_routes		= tas2770_audio_map,
529 	.num_dapm_routes	= ARRAY_SIZE(tas2770_audio_map),
530 	.idle_bias_on		= 1,
531 	.endianness		= 1,
532 	.non_legacy_dai_naming	= 1,
533 };
534 
tas2770_register_codec(struct tas2770_priv * tas2770)535 static int tas2770_register_codec(struct tas2770_priv *tas2770)
536 {
537 	return devm_snd_soc_register_component(tas2770->dev,
538 		&soc_component_driver_tas2770,
539 		tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
540 }
541 
542 static const struct reg_default tas2770_reg_defaults[] = {
543 	{ TAS2770_PAGE, 0x00 },
544 	{ TAS2770_SW_RST, 0x00 },
545 	{ TAS2770_PWR_CTRL, 0x0e },
546 	{ TAS2770_PLAY_CFG_REG0, 0x10 },
547 	{ TAS2770_PLAY_CFG_REG1, 0x01 },
548 	{ TAS2770_PLAY_CFG_REG2, 0x00 },
549 	{ TAS2770_MSC_CFG_REG0, 0x07 },
550 	{ TAS2770_TDM_CFG_REG1, 0x02 },
551 	{ TAS2770_TDM_CFG_REG2, 0x0a },
552 	{ TAS2770_TDM_CFG_REG3, 0x10 },
553 	{ TAS2770_INT_MASK_REG0, 0xfc },
554 	{ TAS2770_INT_MASK_REG1, 0xb1 },
555 	{ TAS2770_INT_CFG, 0x05 },
556 	{ TAS2770_MISC_IRQ, 0x81 },
557 	{ TAS2770_CLK_CGF, 0x0c },
558 
559 };
560 
tas2770_volatile(struct device * dev,unsigned int reg)561 static bool tas2770_volatile(struct device *dev, unsigned int reg)
562 {
563 	switch (reg) {
564 	case TAS2770_PAGE: /* regmap implementation requires this */
565 	case TAS2770_SW_RST: /* always clears after write */
566 	case TAS2770_BO_PRV_REG0:/* has a self clearing bit */
567 	case TAS2770_LVE_INT_REG0:
568 	case TAS2770_LVE_INT_REG1:
569 	case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */
570 	case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */
571 	case TAS2770_VBAT_MSB:
572 	case TAS2770_VBAT_LSB:
573 	case TAS2770_TEMP_MSB:
574 	case TAS2770_TEMP_LSB:
575 		return true;
576 	}
577 
578 	return false;
579 }
580 
tas2770_writeable(struct device * dev,unsigned int reg)581 static bool tas2770_writeable(struct device *dev, unsigned int reg)
582 {
583 	switch (reg) {
584 	case TAS2770_LVE_INT_REG0:
585 	case TAS2770_LVE_INT_REG1:
586 	case TAS2770_LAT_INT_REG0:
587 	case TAS2770_LAT_INT_REG1:
588 	case TAS2770_VBAT_MSB:
589 	case TAS2770_VBAT_LSB:
590 	case TAS2770_TEMP_MSB:
591 	case TAS2770_TEMP_LSB:
592 	case TAS2770_TDM_CLK_DETC:
593 	case TAS2770_REV_AND_GPID:
594 		return false;
595 	}
596 
597 	return true;
598 }
599 
600 static const struct regmap_range_cfg tas2770_regmap_ranges[] = {
601 	{
602 		.range_min = 0,
603 		.range_max = 1 * 128,
604 		.selector_reg = TAS2770_PAGE,
605 		.selector_mask = 0xff,
606 		.selector_shift = 0,
607 		.window_start = 0,
608 		.window_len = 128,
609 	},
610 };
611 
612 static const struct regmap_config tas2770_i2c_regmap = {
613 	.reg_bits = 8,
614 	.val_bits = 8,
615 	.writeable_reg = tas2770_writeable,
616 	.volatile_reg = tas2770_volatile,
617 	.reg_defaults = tas2770_reg_defaults,
618 	.num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults),
619 	.cache_type = REGCACHE_RBTREE,
620 	.ranges = tas2770_regmap_ranges,
621 	.num_ranges = ARRAY_SIZE(tas2770_regmap_ranges),
622 	.max_register = 1 * 128,
623 };
624 
tas2770_parse_dt(struct device * dev,struct tas2770_priv * tas2770)625 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770)
626 {
627 	int rc = 0;
628 
629 	rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
630 				      &tas2770->i_sense_slot);
631 	if (rc) {
632 		dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
633 			 "ti,imon-slot-no");
634 
635 		tas2770->i_sense_slot = 0;
636 	}
637 
638 	rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
639 				      &tas2770->v_sense_slot);
640 	if (rc) {
641 		dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
642 			 "ti,vmon-slot-no");
643 
644 		tas2770->v_sense_slot = 2;
645 	}
646 
647 	tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
648 	if (IS_ERR(tas2770->sdz_gpio)) {
649 		if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER)
650 			return -EPROBE_DEFER;
651 
652 		tas2770->sdz_gpio = NULL;
653 	}
654 
655 	return 0;
656 }
657 
tas2770_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)658 static int tas2770_i2c_probe(struct i2c_client *client,
659 			const struct i2c_device_id *id)
660 {
661 	struct tas2770_priv *tas2770;
662 	int result;
663 
664 	tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv),
665 			       GFP_KERNEL);
666 	if (!tas2770)
667 		return -ENOMEM;
668 
669 	tas2770->dev = &client->dev;
670 	i2c_set_clientdata(client, tas2770);
671 	dev_set_drvdata(&client->dev, tas2770);
672 
673 	tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap);
674 	if (IS_ERR(tas2770->regmap)) {
675 		result = PTR_ERR(tas2770->regmap);
676 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
677 			result);
678 		return result;
679 	}
680 
681 	if (client->dev.of_node) {
682 		result = tas2770_parse_dt(&client->dev, tas2770);
683 		if (result) {
684 			dev_err(tas2770->dev, "%s: Failed to parse devicetree\n",
685 				__func__);
686 			return result;
687 		}
688 	}
689 
690 	tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset",
691 						      GPIOD_OUT_HIGH);
692 	if (IS_ERR(tas2770->reset_gpio)) {
693 		if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) {
694 			tas2770->reset_gpio = NULL;
695 			return -EPROBE_DEFER;
696 		}
697 	}
698 
699 	result = tas2770_register_codec(tas2770);
700 	if (result)
701 		dev_err(tas2770->dev, "Register codec failed.\n");
702 
703 	return result;
704 }
705 
706 static const struct i2c_device_id tas2770_i2c_id[] = {
707 	{ "tas2770", 0},
708 	{ }
709 };
710 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id);
711 
712 #if defined(CONFIG_OF)
713 static const struct of_device_id tas2770_of_match[] = {
714 	{ .compatible = "ti,tas2770" },
715 	{},
716 };
717 MODULE_DEVICE_TABLE(of, tas2770_of_match);
718 #endif
719 
720 static struct i2c_driver tas2770_i2c_driver = {
721 	.driver = {
722 		.name   = "tas2770",
723 		.of_match_table = of_match_ptr(tas2770_of_match),
724 	},
725 	.probe      = tas2770_i2c_probe,
726 	.id_table   = tas2770_i2c_id,
727 };
728 module_i2c_driver(tas2770_i2c_driver);
729 
730 MODULE_AUTHOR("Shi Fu <shifu0704@thundersoft.com>");
731 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver");
732 MODULE_LICENSE("GPL v2");
733