xref: /linux/sound/soc/codecs/tas5720.c (revision 50282fd57bcd3525c9d81eef58df8718e4337c6d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tas5720.c - ALSA SoC Texas Instruments TAS5720 Mono Audio Amplifier
4  *
5  * Copyright (C)2015-2016 Texas Instruments Incorporated -  https://www.ti.com
6  *
7  * Author: Andreas Dannenberg <dannenberg@ti.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/delay.h>
19 
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/tlv.h>
25 
26 #include "tas5720.h"
27 
28 /* Define how often to check (and clear) the fault status register (in ms) */
29 #define TAS5720_FAULT_CHECK_INTERVAL		200
30 
31 enum tas572x_type {
32 	TAS5720,
33 	TAS5720A_Q1,
34 	TAS5722,
35 };
36 
37 static const char * const tas5720_supply_names[] = {
38 	"dvdd",		/* Digital power supply. Connect to 3.3-V supply. */
39 	"pvdd",		/* Class-D amp and analog power supply (connected). */
40 };
41 
42 #define TAS5720_NUM_SUPPLIES	ARRAY_SIZE(tas5720_supply_names)
43 
44 struct tas5720_data {
45 	struct snd_soc_component *component;
46 	struct regmap *regmap;
47 	struct i2c_client *tas5720_client;
48 	enum tas572x_type devtype;
49 	struct regulator_bulk_data supplies[TAS5720_NUM_SUPPLIES];
50 	struct delayed_work fault_check_work;
51 	unsigned int last_fault;
52 };
53 
54 static int tas5720_hw_params(struct snd_pcm_substream *substream,
55 			     struct snd_pcm_hw_params *params,
56 			     struct snd_soc_dai *dai)
57 {
58 	struct snd_soc_component *component = dai->component;
59 	unsigned int rate = params_rate(params);
60 	bool ssz_ds;
61 	int ret;
62 
63 	switch (rate) {
64 	case 44100:
65 	case 48000:
66 		ssz_ds = false;
67 		break;
68 	case 88200:
69 	case 96000:
70 		ssz_ds = true;
71 		break;
72 	default:
73 		dev_err(component->dev, "unsupported sample rate: %u\n", rate);
74 		return -EINVAL;
75 	}
76 
77 	ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG,
78 				  TAS5720_SSZ_DS, ssz_ds);
79 	if (ret < 0) {
80 		dev_err(component->dev, "error setting sample rate: %d\n", ret);
81 		return ret;
82 	}
83 
84 	return 0;
85 }
86 
87 static int tas5720_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
88 {
89 	struct snd_soc_component *component = dai->component;
90 	u8 serial_format;
91 	int ret;
92 
93 	if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
94 		dev_vdbg(component->dev, "DAI clocking invalid\n");
95 		return -EINVAL;
96 	}
97 
98 	switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
99 		       SND_SOC_DAIFMT_INV_MASK)) {
100 	case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
101 		/* 1st data bit occur one BCLK cycle after the frame sync */
102 		serial_format = TAS5720_SAIF_I2S;
103 		break;
104 	case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF):
105 		/*
106 		 * Note that although the TAS5720 does not have a dedicated DSP
107 		 * mode it doesn't care about the LRCLK duty cycle during TDM
108 		 * operation. Therefore we can use the device's I2S mode with
109 		 * its delaying of the 1st data bit to receive DSP_A formatted
110 		 * data. See device datasheet for additional details.
111 		 */
112 		serial_format = TAS5720_SAIF_I2S;
113 		break;
114 	case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF):
115 		/*
116 		 * Similar to DSP_A, we can use the fact that the TAS5720 does
117 		 * not care about the LRCLK duty cycle during TDM to receive
118 		 * DSP_B formatted data in LEFTJ mode (no delaying of the 1st
119 		 * data bit).
120 		 */
121 		serial_format = TAS5720_SAIF_LEFTJ;
122 		break;
123 	case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
124 		/* No delay after the frame sync */
125 		serial_format = TAS5720_SAIF_LEFTJ;
126 		break;
127 	default:
128 		dev_vdbg(component->dev, "DAI Format is not found\n");
129 		return -EINVAL;
130 	}
131 
132 	ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG,
133 				  TAS5720_SAIF_FORMAT_MASK,
134 				  serial_format);
135 	if (ret < 0) {
136 		dev_err(component->dev, "error setting SAIF format: %d\n", ret);
137 		return ret;
138 	}
139 
140 	return 0;
141 }
142 
143 static int tas5720_set_dai_tdm_slot(struct snd_soc_dai *dai,
144 				    unsigned int tx_mask, unsigned int rx_mask,
145 				    int slots, int slot_width)
146 {
147 	struct snd_soc_component *component = dai->component;
148 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
149 	unsigned int first_slot;
150 	int ret;
151 
152 	if (!tx_mask) {
153 		dev_err(component->dev, "tx masks must not be 0\n");
154 		return -EINVAL;
155 	}
156 
157 	/*
158 	 * Determine the first slot that is being requested. We will only
159 	 * use the first slot that is found since the TAS5720 is a mono
160 	 * amplifier.
161 	 */
162 	first_slot = __ffs(tx_mask);
163 
164 	if (first_slot > 7) {
165 		dev_err(component->dev, "slot selection out of bounds (%u)\n",
166 			first_slot);
167 		return -EINVAL;
168 	}
169 
170 	/*
171 	 * Enable manual TDM slot selection (instead of I2C ID based).
172 	 * This is not applicable to TAS5720A-Q1.
173 	 */
174 	switch (tas5720->devtype) {
175 	case TAS5720A_Q1:
176 		break;
177 	default:
178 		ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG,
179 					  TAS5720_TDM_CFG_SRC, TAS5720_TDM_CFG_SRC);
180 		if (ret < 0)
181 			goto error_snd_soc_component_update_bits;
182 
183 		/* Configure the TDM slot to process audio from */
184 		ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL2_REG,
185 					  TAS5720_TDM_SLOT_SEL_MASK, first_slot);
186 		if (ret < 0)
187 			goto error_snd_soc_component_update_bits;
188 		break;
189 	}
190 
191 	/* Configure TDM slot width. This is only applicable to TAS5722. */
192 	switch (tas5720->devtype) {
193 	case TAS5722:
194 		ret = snd_soc_component_update_bits(component, TAS5722_DIGITAL_CTRL2_REG,
195 						    TAS5722_TDM_SLOT_16B,
196 						    slot_width == 16 ?
197 						    TAS5722_TDM_SLOT_16B : 0);
198 		if (ret < 0)
199 			goto error_snd_soc_component_update_bits;
200 		break;
201 	default:
202 		break;
203 	}
204 
205 	return 0;
206 
207 error_snd_soc_component_update_bits:
208 	dev_err(component->dev, "error configuring TDM mode: %d\n", ret);
209 	return ret;
210 }
211 
212 static int tas5720_mute_soc_component(struct snd_soc_component *component, int mute)
213 {
214 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
215 	unsigned int reg, mask;
216 	int ret;
217 
218 	switch (tas5720->devtype) {
219 	case TAS5720A_Q1:
220 		reg = TAS5720_Q1_VOLUME_CTRL_CFG_REG;
221 		mask = TAS5720_Q1_MUTE;
222 		break;
223 	default:
224 		reg = TAS5720_DIGITAL_CTRL2_REG;
225 		mask = TAS5720_MUTE;
226 		break;
227 	}
228 
229 	ret = snd_soc_component_update_bits(component, reg, mask, mute ? mask : 0);
230 	if (ret < 0) {
231 		dev_err(component->dev, "error (un-)muting device: %d\n", ret);
232 		return ret;
233 	}
234 
235 	return 0;
236 }
237 
238 static int tas5720_mute(struct snd_soc_dai *dai, int mute, int direction)
239 {
240 	return tas5720_mute_soc_component(dai->component, mute);
241 }
242 
243 static void tas5720_fault_check_work(struct work_struct *work)
244 {
245 	struct tas5720_data *tas5720 = container_of(work, struct tas5720_data,
246 			fault_check_work.work);
247 	struct device *dev = tas5720->component->dev;
248 	unsigned int curr_fault;
249 	int ret;
250 
251 	ret = regmap_read(tas5720->regmap, TAS5720_FAULT_REG, &curr_fault);
252 	if (ret < 0) {
253 		dev_err(dev, "failed to read FAULT register: %d\n", ret);
254 		goto out;
255 	}
256 
257 	/* Check/handle all errors except SAIF clock errors */
258 	curr_fault &= TAS5720_OCE | TAS5720_DCE | TAS5720_OTE;
259 
260 	/*
261 	 * Only flag errors once for a given occurrence. This is needed as
262 	 * the TAS5720 will take time clearing the fault condition internally
263 	 * during which we don't want to bombard the system with the same
264 	 * error message over and over.
265 	 */
266 	if ((curr_fault & TAS5720_OCE) && !(tas5720->last_fault & TAS5720_OCE))
267 		dev_crit(dev, "experienced an over current hardware fault\n");
268 
269 	if ((curr_fault & TAS5720_DCE) && !(tas5720->last_fault & TAS5720_DCE))
270 		dev_crit(dev, "experienced a DC detection fault\n");
271 
272 	if ((curr_fault & TAS5720_OTE) && !(tas5720->last_fault & TAS5720_OTE))
273 		dev_crit(dev, "experienced an over temperature fault\n");
274 
275 	/* Store current fault value so we can detect any changes next time */
276 	tas5720->last_fault = curr_fault;
277 
278 	if (!curr_fault)
279 		goto out;
280 
281 	/*
282 	 * Periodically toggle SDZ (shutdown bit) H->L->H to clear any latching
283 	 * faults as long as a fault condition persists. Always going through
284 	 * the full sequence no matter the first return value to minimizes
285 	 * chances for the device to end up in shutdown mode.
286 	 */
287 	ret = regmap_write_bits(tas5720->regmap, TAS5720_POWER_CTRL_REG,
288 				TAS5720_SDZ, 0);
289 	if (ret < 0)
290 		dev_err(dev, "failed to write POWER_CTRL register: %d\n", ret);
291 
292 	ret = regmap_write_bits(tas5720->regmap, TAS5720_POWER_CTRL_REG,
293 				TAS5720_SDZ, TAS5720_SDZ);
294 	if (ret < 0)
295 		dev_err(dev, "failed to write POWER_CTRL register: %d\n", ret);
296 
297 out:
298 	/* Schedule the next fault check at the specified interval */
299 	schedule_delayed_work(&tas5720->fault_check_work,
300 			      msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL));
301 }
302 
303 static int tas5720_codec_probe(struct snd_soc_component *component)
304 {
305 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
306 	unsigned int device_id, expected_device_id;
307 	int ret;
308 
309 	tas5720->component = component;
310 
311 	ret = regulator_bulk_enable(ARRAY_SIZE(tas5720->supplies),
312 				    tas5720->supplies);
313 	if (ret != 0) {
314 		dev_err(component->dev, "failed to enable supplies: %d\n", ret);
315 		return ret;
316 	}
317 
318 	/*
319 	 * Take a liberal approach to checking the device ID to allow the
320 	 * driver to be used even if the device ID does not match, however
321 	 * issue a warning if there is a mismatch.
322 	 */
323 	ret = regmap_read(tas5720->regmap, TAS5720_DEVICE_ID_REG, &device_id);
324 	if (ret < 0) {
325 		dev_err(component->dev, "failed to read device ID register: %d\n",
326 			ret);
327 		goto probe_fail;
328 	}
329 
330 	switch (tas5720->devtype) {
331 	case TAS5720:
332 		expected_device_id = TAS5720_DEVICE_ID;
333 		break;
334 	case TAS5720A_Q1:
335 		expected_device_id = TAS5720A_Q1_DEVICE_ID;
336 		break;
337 	case TAS5722:
338 		expected_device_id = TAS5722_DEVICE_ID;
339 		break;
340 	default:
341 		dev_err(component->dev, "unexpected private driver data\n");
342 		ret = -EINVAL;
343 		goto probe_fail;
344 	}
345 
346 	if (device_id != expected_device_id)
347 		dev_warn(component->dev, "wrong device ID. expected: %u read: %u\n",
348 			 expected_device_id, device_id);
349 
350 	/* Set device to mute */
351 	ret = tas5720_mute_soc_component(component, 1);
352 	if (ret < 0)
353 		goto error_snd_soc_component_update_bits;
354 
355 	/* Set Bit 7 in TAS5720_ANALOG_CTRL_REG to 1 for TAS5720A_Q1 */
356 	switch (tas5720->devtype) {
357 	case TAS5720A_Q1:
358 		ret = snd_soc_component_update_bits(component, TAS5720_ANALOG_CTRL_REG,
359 						    TAS5720_Q1_RESERVED7_BIT,
360 						    TAS5720_Q1_RESERVED7_BIT);
361 		break;
362 	default:
363 		break;
364 	}
365 	if (ret < 0)
366 		goto error_snd_soc_component_update_bits;
367 
368 	/*
369 	 * Enter shutdown mode - our default when not playing audio - to
370 	 * minimize current consumption. On the TAS5720 there is no real down
371 	 * side doing so as all device registers are preserved and the wakeup
372 	 * of the codec is rather quick which we do using a dapm widget.
373 	 */
374 	ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG,
375 				  TAS5720_SDZ, 0);
376 	if (ret < 0)
377 		goto error_snd_soc_component_update_bits;
378 
379 	INIT_DELAYED_WORK(&tas5720->fault_check_work, tas5720_fault_check_work);
380 
381 	return 0;
382 
383 error_snd_soc_component_update_bits:
384 	dev_err(component->dev, "error configuring device registers: %d\n", ret);
385 
386 probe_fail:
387 	regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies),
388 			       tas5720->supplies);
389 	return ret;
390 }
391 
392 static void tas5720_codec_remove(struct snd_soc_component *component)
393 {
394 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
395 	int ret;
396 
397 	cancel_delayed_work_sync(&tas5720->fault_check_work);
398 
399 	ret = regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies),
400 				     tas5720->supplies);
401 	if (ret < 0)
402 		dev_err(component->dev, "failed to disable supplies: %d\n", ret);
403 };
404 
405 static int tas5720_dac_event(struct snd_soc_dapm_widget *w,
406 			     struct snd_kcontrol *kcontrol, int event)
407 {
408 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
409 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
410 	int ret;
411 
412 	if (event & SND_SOC_DAPM_POST_PMU) {
413 		/* Take TAS5720 out of shutdown mode */
414 		ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG,
415 					  TAS5720_SDZ, TAS5720_SDZ);
416 		if (ret < 0) {
417 			dev_err(component->dev, "error waking component: %d\n", ret);
418 			return ret;
419 		}
420 
421 		/*
422 		 * Observe codec shutdown-to-active time. The datasheet only
423 		 * lists a nominal value however just use-it as-is without
424 		 * additional padding to minimize the delay introduced in
425 		 * starting to play audio (actually there is other setup done
426 		 * by the ASoC framework that will provide additional delays,
427 		 * so we should always be safe).
428 		 */
429 		msleep(25);
430 
431 		/* Turn on TAS5720 periodic fault checking/handling */
432 		tas5720->last_fault = 0;
433 		schedule_delayed_work(&tas5720->fault_check_work,
434 				msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL));
435 	} else if (event & SND_SOC_DAPM_PRE_PMD) {
436 		/* Disable TAS5720 periodic fault checking/handling */
437 		cancel_delayed_work_sync(&tas5720->fault_check_work);
438 
439 		/* Place TAS5720 in shutdown mode to minimize current draw */
440 		ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG,
441 					  TAS5720_SDZ, 0);
442 		if (ret < 0) {
443 			dev_err(component->dev, "error shutting down component: %d\n",
444 				ret);
445 			return ret;
446 		}
447 	}
448 
449 	return 0;
450 }
451 
452 #ifdef CONFIG_PM
453 static int tas5720_suspend(struct snd_soc_component *component)
454 {
455 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
456 	int ret;
457 
458 	regcache_cache_only(tas5720->regmap, true);
459 	regcache_mark_dirty(tas5720->regmap);
460 
461 	ret = regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies),
462 				     tas5720->supplies);
463 	if (ret < 0)
464 		dev_err(component->dev, "failed to disable supplies: %d\n", ret);
465 
466 	return ret;
467 }
468 
469 static int tas5720_resume(struct snd_soc_component *component)
470 {
471 	struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component);
472 	int ret;
473 
474 	ret = regulator_bulk_enable(ARRAY_SIZE(tas5720->supplies),
475 				    tas5720->supplies);
476 	if (ret < 0) {
477 		dev_err(component->dev, "failed to enable supplies: %d\n", ret);
478 		return ret;
479 	}
480 
481 	regcache_cache_only(tas5720->regmap, false);
482 
483 	ret = regcache_sync(tas5720->regmap);
484 	if (ret < 0) {
485 		dev_err(component->dev, "failed to sync regcache: %d\n", ret);
486 		return ret;
487 	}
488 
489 	return 0;
490 }
491 #else
492 #define tas5720_suspend NULL
493 #define tas5720_resume NULL
494 #endif
495 
496 static bool tas5720_is_volatile_reg(struct device *dev, unsigned int reg)
497 {
498 	switch (reg) {
499 	case TAS5720_DEVICE_ID_REG:
500 	case TAS5720_FAULT_REG:
501 		return true;
502 	default:
503 		return false;
504 	}
505 }
506 
507 static const struct regmap_config tas5720_regmap_config = {
508 	.reg_bits = 8,
509 	.val_bits = 8,
510 
511 	.max_register = TAS5720_MAX_REG,
512 	.cache_type = REGCACHE_RBTREE,
513 	.volatile_reg = tas5720_is_volatile_reg,
514 };
515 
516 static const struct regmap_config tas5720a_q1_regmap_config = {
517 	.reg_bits = 8,
518 	.val_bits = 8,
519 
520 	.max_register = TAS5720_MAX_REG,
521 	.cache_type = REGCACHE_RBTREE,
522 	.volatile_reg = tas5720_is_volatile_reg,
523 };
524 
525 static const struct regmap_config tas5722_regmap_config = {
526 	.reg_bits = 8,
527 	.val_bits = 8,
528 
529 	.max_register = TAS5722_MAX_REG,
530 	.cache_type = REGCACHE_RBTREE,
531 	.volatile_reg = tas5720_is_volatile_reg,
532 };
533 
534 /*
535  * DAC analog gain. There are four discrete values to select from, ranging
536  * from 19.2 dB to 26.3dB.
537  */
538 static const DECLARE_TLV_DB_RANGE(dac_analog_tlv,
539 	0x0, 0x0, TLV_DB_SCALE_ITEM(1920, 0, 0),
540 	0x1, 0x1, TLV_DB_SCALE_ITEM(2070, 0, 0),
541 	0x2, 0x2, TLV_DB_SCALE_ITEM(2350, 0, 0),
542 	0x3, 0x3, TLV_DB_SCALE_ITEM(2630, 0, 0),
543 );
544 
545 /*
546  * DAC analog gain for TAS5720A-Q1. There are three discrete values to select from, ranging
547  * from 19.2 dB to 25.0dB.
548  */
549 static const DECLARE_TLV_DB_RANGE(dac_analog_tlv_a_q1,
550 	0x0, 0x0, TLV_DB_SCALE_ITEM(1920, 0, 0),
551 	0x1, 0x1, TLV_DB_SCALE_ITEM(2260, 0, 0),
552 	0x2, 0x2, TLV_DB_SCALE_ITEM(2500, 0, 0),
553 );
554 
555 /*
556  * DAC digital volumes. From -103.5 to 24 dB in 0.5 dB or 0.25 dB steps
557  * depending on the device. Note that setting the gain below -100 dB
558  * (register value <0x7) is effectively a MUTE as per device datasheet.
559  *
560  * Note that for the TAS5722 the digital volume controls are actually split
561  * over two registers, so we need custom getters/setters for access.
562  */
563 static DECLARE_TLV_DB_SCALE(tas5720_dac_tlv, -10350, 50, 0);
564 static DECLARE_TLV_DB_SCALE(tas5722_dac_tlv, -10350, 25, 0);
565 
566 static int tas5722_volume_get(struct snd_kcontrol *kcontrol,
567 			      struct snd_ctl_elem_value *ucontrol)
568 {
569 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
570 	unsigned int val;
571 
572 	val = snd_soc_component_read(component, TAS5720_VOLUME_CTRL_REG);
573 	ucontrol->value.integer.value[0] = val << 1;
574 
575 	val = snd_soc_component_read(component, TAS5722_DIGITAL_CTRL2_REG);
576 	ucontrol->value.integer.value[0] |= val & TAS5722_VOL_CONTROL_LSB;
577 
578 	return 0;
579 }
580 
581 static int tas5722_volume_set(struct snd_kcontrol *kcontrol,
582 			      struct snd_ctl_elem_value *ucontrol)
583 {
584 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
585 	unsigned int sel = ucontrol->value.integer.value[0];
586 
587 	snd_soc_component_write(component, TAS5720_VOLUME_CTRL_REG, sel >> 1);
588 	snd_soc_component_update_bits(component, TAS5722_DIGITAL_CTRL2_REG,
589 				      TAS5722_VOL_CONTROL_LSB, sel);
590 
591 	return 0;
592 }
593 
594 static const struct snd_kcontrol_new tas5720_snd_controls[] = {
595 	SOC_SINGLE_TLV("Speaker Driver Playback Volume",
596 		       TAS5720_VOLUME_CTRL_REG, 0, 0xff, 0, tas5720_dac_tlv),
597 	SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG,
598 		       TAS5720_ANALOG_GAIN_SHIFT, 3, 0, dac_analog_tlv),
599 };
600 
601 static const struct snd_kcontrol_new tas5720a_q1_snd_controls[] = {
602 	SOC_DOUBLE_R_TLV("Speaker Driver Playback Volume",
603 				TAS5720_Q1_VOLUME_CTRL_LEFT_REG,
604 				TAS5720_Q1_VOLUME_CTRL_RIGHT_REG,
605 				0, 0xff, 0, tas5720_dac_tlv),
606 	SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG,
607 				TAS5720_ANALOG_GAIN_SHIFT, 3, 0, dac_analog_tlv_a_q1),
608 };
609 
610 static const struct snd_kcontrol_new tas5722_snd_controls[] = {
611 	SOC_SINGLE_EXT_TLV("Speaker Driver Playback Volume",
612 			   0, 0, 511, 0,
613 			   tas5722_volume_get, tas5722_volume_set,
614 			   tas5722_dac_tlv),
615 	SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG,
616 		       TAS5720_ANALOG_GAIN_SHIFT, 3, 0, dac_analog_tlv),
617 };
618 
619 static const struct snd_soc_dapm_widget tas5720_dapm_widgets[] = {
620 	SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
621 	SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas5720_dac_event,
622 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
623 	SND_SOC_DAPM_OUTPUT("OUT")
624 };
625 
626 static const struct snd_soc_dapm_route tas5720_audio_map[] = {
627 	{ "DAC", NULL, "DAC IN" },
628 	{ "OUT", NULL, "DAC" },
629 };
630 
631 static const struct snd_soc_component_driver soc_component_dev_tas5720 = {
632 	.probe			= tas5720_codec_probe,
633 	.remove			= tas5720_codec_remove,
634 	.suspend		= tas5720_suspend,
635 	.resume			= tas5720_resume,
636 	.controls		= tas5720_snd_controls,
637 	.num_controls		= ARRAY_SIZE(tas5720_snd_controls),
638 	.dapm_widgets		= tas5720_dapm_widgets,
639 	.num_dapm_widgets	= ARRAY_SIZE(tas5720_dapm_widgets),
640 	.dapm_routes		= tas5720_audio_map,
641 	.num_dapm_routes	= ARRAY_SIZE(tas5720_audio_map),
642 	.idle_bias_on		= 1,
643 	.use_pmdown_time	= 1,
644 	.endianness		= 1,
645 };
646 
647 static const struct snd_soc_component_driver soc_component_dev_tas5720_a_q1 = {
648 	.probe			= tas5720_codec_probe,
649 	.remove			= tas5720_codec_remove,
650 	.suspend		= tas5720_suspend,
651 	.resume			= tas5720_resume,
652 	.controls		= tas5720a_q1_snd_controls,
653 	.num_controls		= ARRAY_SIZE(tas5720a_q1_snd_controls),
654 	.dapm_widgets		= tas5720_dapm_widgets,
655 	.num_dapm_widgets	= ARRAY_SIZE(tas5720_dapm_widgets),
656 	.dapm_routes		= tas5720_audio_map,
657 	.num_dapm_routes	= ARRAY_SIZE(tas5720_audio_map),
658 	.idle_bias_on		= 1,
659 	.use_pmdown_time	= 1,
660 	.endianness		= 1,
661 };
662 
663 static const struct snd_soc_component_driver soc_component_dev_tas5722 = {
664 	.probe = tas5720_codec_probe,
665 	.remove = tas5720_codec_remove,
666 	.suspend = tas5720_suspend,
667 	.resume = tas5720_resume,
668 	.controls = tas5722_snd_controls,
669 	.num_controls = ARRAY_SIZE(tas5722_snd_controls),
670 	.dapm_widgets = tas5720_dapm_widgets,
671 	.num_dapm_widgets = ARRAY_SIZE(tas5720_dapm_widgets),
672 	.dapm_routes = tas5720_audio_map,
673 	.num_dapm_routes = ARRAY_SIZE(tas5720_audio_map),
674 	.idle_bias_on		= 1,
675 	.use_pmdown_time	= 1,
676 	.endianness		= 1,
677 };
678 
679 /* PCM rates supported by the TAS5720 driver */
680 #define TAS5720_RATES	(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
681 			 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
682 
683 /* Formats supported by TAS5720 driver */
684 #define TAS5720_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE |\
685 			 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
686 
687 static const struct snd_soc_dai_ops tas5720_speaker_dai_ops = {
688 	.hw_params	= tas5720_hw_params,
689 	.set_fmt	= tas5720_set_dai_fmt,
690 	.set_tdm_slot	= tas5720_set_dai_tdm_slot,
691 	.mute_stream	= tas5720_mute,
692 	.no_capture_mute = 1,
693 };
694 
695 /*
696  * TAS5720 DAI structure
697  *
698  * Note that were are advertising .playback.channels_max = 2 despite this being
699  * a mono amplifier. The reason for that is that some serial ports such as TI's
700  * McASP module have a minimum number of channels (2) that they can output.
701  * Advertising more channels than we have will allow us to interface with such
702  * a serial port without really any negative side effects as the TAS5720 will
703  * simply ignore any extra channel(s) asides from the one channel that is
704  * configured to be played back.
705  */
706 static struct snd_soc_dai_driver tas5720_dai[] = {
707 	{
708 		.name = "tas5720-amplifier",
709 		.playback = {
710 			.stream_name = "Playback",
711 			.channels_min = 1,
712 			.channels_max = 2,
713 			.rates = TAS5720_RATES,
714 			.formats = TAS5720_FORMATS,
715 		},
716 		.ops = &tas5720_speaker_dai_ops,
717 	},
718 };
719 
720 static const struct i2c_device_id tas5720_id[] = {
721 	{ "tas5720", TAS5720 },
722 	{ "tas5720a-q1", TAS5720A_Q1 },
723 	{ "tas5722", TAS5722 },
724 	{ }
725 };
726 MODULE_DEVICE_TABLE(i2c, tas5720_id);
727 
728 static int tas5720_probe(struct i2c_client *client)
729 {
730 	struct device *dev = &client->dev;
731 	struct tas5720_data *data;
732 	const struct regmap_config *regmap_config;
733 	const struct i2c_device_id *id;
734 	int ret;
735 	int i;
736 
737 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
738 	if (!data)
739 		return -ENOMEM;
740 
741 	id = i2c_match_id(tas5720_id, client);
742 	data->tas5720_client = client;
743 	data->devtype = id->driver_data;
744 
745 	switch (id->driver_data) {
746 	case TAS5720:
747 		regmap_config = &tas5720_regmap_config;
748 		break;
749 	case TAS5720A_Q1:
750 		regmap_config = &tas5720a_q1_regmap_config;
751 		break;
752 	case TAS5722:
753 		regmap_config = &tas5722_regmap_config;
754 		break;
755 	default:
756 		dev_err(dev, "unexpected private driver data\n");
757 		return -EINVAL;
758 	}
759 	data->regmap = devm_regmap_init_i2c(client, regmap_config);
760 	if (IS_ERR(data->regmap)) {
761 		ret = PTR_ERR(data->regmap);
762 		dev_err(dev, "failed to allocate register map: %d\n", ret);
763 		return ret;
764 	}
765 
766 	for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
767 		data->supplies[i].supply = tas5720_supply_names[i];
768 
769 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
770 				      data->supplies);
771 	if (ret != 0) {
772 		dev_err(dev, "failed to request supplies: %d\n", ret);
773 		return ret;
774 	}
775 
776 	dev_set_drvdata(dev, data);
777 
778 	switch (id->driver_data) {
779 	case TAS5720:
780 		ret = devm_snd_soc_register_component(&client->dev,
781 					&soc_component_dev_tas5720,
782 					tas5720_dai,
783 					ARRAY_SIZE(tas5720_dai));
784 		break;
785 	case TAS5720A_Q1:
786 		ret = devm_snd_soc_register_component(&client->dev,
787 					&soc_component_dev_tas5720_a_q1,
788 					tas5720_dai,
789 					ARRAY_SIZE(tas5720_dai));
790 		break;
791 	case TAS5722:
792 		ret = devm_snd_soc_register_component(&client->dev,
793 					&soc_component_dev_tas5722,
794 					tas5720_dai,
795 					ARRAY_SIZE(tas5720_dai));
796 		break;
797 	default:
798 		dev_err(dev, "unexpected private driver data\n");
799 		return -EINVAL;
800 	}
801 	if (ret < 0) {
802 		dev_err(dev, "failed to register component: %d\n", ret);
803 		return ret;
804 	}
805 
806 	return 0;
807 }
808 
809 #if IS_ENABLED(CONFIG_OF)
810 static const struct of_device_id tas5720_of_match[] = {
811 	{ .compatible = "ti,tas5720", },
812 	{ .compatible = "ti,tas5720a-q1", },
813 	{ .compatible = "ti,tas5722", },
814 	{ },
815 };
816 MODULE_DEVICE_TABLE(of, tas5720_of_match);
817 #endif
818 
819 static struct i2c_driver tas5720_i2c_driver = {
820 	.driver = {
821 		.name = "tas5720",
822 		.of_match_table = of_match_ptr(tas5720_of_match),
823 	},
824 	.probe_new = tas5720_probe,
825 	.id_table = tas5720_id,
826 };
827 
828 module_i2c_driver(tas5720_i2c_driver);
829 
830 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
831 MODULE_DESCRIPTION("TAS5720 Audio amplifier driver");
832 MODULE_LICENSE("GPL");
833