xref: /linux/sound/soc/codecs/tas571x.c (revision a7f7f6248d9740d710fd6bd190293fe5e16410ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * TAS571x amplifier audio driver
4  *
5  * Copyright (C) 2015 Google, Inc.
6  * Copyright (c) 2013 Daniel Mack <zonque@gmail.com>
7  *
8  * TAS5721 support:
9  * Copyright (C) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
10  *
11  * TAS5707 support:
12  * Copyright (C) 2018 Jerome Brunet, Baylibre SAS <jbrunet@baylibre.com>
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/stddef.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <asm/unaligned.h>
31 
32 #include "tas571x.h"
33 
34 #define TAS571X_MAX_SUPPLIES		6
35 
36 struct tas571x_chip {
37 	const char			*const *supply_names;
38 	int				num_supply_names;
39 	const struct snd_kcontrol_new	*controls;
40 	int				num_controls;
41 	const struct regmap_config	*regmap_config;
42 	int				vol_reg_size;
43 };
44 
45 struct tas571x_private {
46 	const struct tas571x_chip	*chip;
47 	struct regmap			*regmap;
48 	struct regulator_bulk_data	supplies[TAS571X_MAX_SUPPLIES];
49 	struct clk			*mclk;
50 	unsigned int			format;
51 	struct gpio_desc		*reset_gpio;
52 	struct gpio_desc		*pdn_gpio;
53 	struct snd_soc_component_driver	component_driver;
54 };
55 
56 static int tas571x_register_size(struct tas571x_private *priv, unsigned int reg)
57 {
58 	switch (reg) {
59 	case TAS571X_MVOL_REG:
60 	case TAS571X_CH1_VOL_REG:
61 	case TAS571X_CH2_VOL_REG:
62 		return priv->chip->vol_reg_size;
63 	case TAS571X_INPUT_MUX_REG:
64 	case TAS571X_CH4_SRC_SELECT_REG:
65 	case TAS571X_PWM_MUX_REG:
66 	case TAS5717_CH1_RIGHT_CH_MIX_REG:
67 	case TAS5717_CH1_LEFT_CH_MIX_REG:
68 	case TAS5717_CH2_LEFT_CH_MIX_REG:
69 	case TAS5717_CH2_RIGHT_CH_MIX_REG:
70 		return 4;
71 	default:
72 		return 1;
73 	}
74 }
75 
76 static int tas571x_reg_write(void *context, unsigned int reg,
77 			     unsigned int value)
78 {
79 	struct i2c_client *client = context;
80 	struct tas571x_private *priv = i2c_get_clientdata(client);
81 	unsigned int i, size;
82 	uint8_t buf[5];
83 	int ret;
84 
85 	size = tas571x_register_size(priv, reg);
86 	buf[0] = reg;
87 
88 	for (i = size; i >= 1; --i) {
89 		buf[i] = value;
90 		value >>= 8;
91 	}
92 
93 	ret = i2c_master_send(client, buf, size + 1);
94 	if (ret == size + 1)
95 		return 0;
96 	else if (ret < 0)
97 		return ret;
98 	else
99 		return -EIO;
100 }
101 
102 static int tas571x_reg_read(void *context, unsigned int reg,
103 			    unsigned int *value)
104 {
105 	struct i2c_client *client = context;
106 	struct tas571x_private *priv = i2c_get_clientdata(client);
107 	uint8_t send_buf, recv_buf[4];
108 	struct i2c_msg msgs[2];
109 	unsigned int size;
110 	unsigned int i;
111 	int ret;
112 
113 	size = tas571x_register_size(priv, reg);
114 	send_buf = reg;
115 
116 	msgs[0].addr = client->addr;
117 	msgs[0].len = sizeof(send_buf);
118 	msgs[0].buf = &send_buf;
119 	msgs[0].flags = 0;
120 
121 	msgs[1].addr = client->addr;
122 	msgs[1].len = size;
123 	msgs[1].buf = recv_buf;
124 	msgs[1].flags = I2C_M_RD;
125 
126 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
127 	if (ret < 0)
128 		return ret;
129 	else if (ret != ARRAY_SIZE(msgs))
130 		return -EIO;
131 
132 	*value = 0;
133 
134 	for (i = 0; i < size; i++) {
135 		*value <<= 8;
136 		*value |= recv_buf[i];
137 	}
138 
139 	return 0;
140 }
141 
142 /*
143  * register write for 8- and 20-byte registers
144  */
145 static int tas571x_reg_write_multiword(struct i2c_client *client,
146 		unsigned int reg, const long values[], size_t len)
147 {
148 	size_t i;
149 	uint8_t *buf, *p;
150 	int ret;
151 	size_t send_size = 1 + len * sizeof(uint32_t);
152 
153 	buf = kzalloc(send_size, GFP_KERNEL | GFP_DMA);
154 	if (!buf)
155 		return -ENOMEM;
156 	buf[0] = reg;
157 
158 	for (i = 0, p = buf + 1; i < len; i++, p += sizeof(uint32_t))
159 		put_unaligned_be32(values[i], p);
160 
161 	ret = i2c_master_send(client, buf, send_size);
162 
163 	kfree(buf);
164 
165 	if (ret == send_size)
166 		return 0;
167 	else if (ret < 0)
168 		return ret;
169 	else
170 		return -EIO;
171 }
172 
173 /*
174  * register read for 8- and 20-byte registers
175  */
176 static int tas571x_reg_read_multiword(struct i2c_client *client,
177 		unsigned int reg, long values[], size_t len)
178 {
179 	unsigned int i;
180 	uint8_t send_buf;
181 	uint8_t *recv_buf, *p;
182 	struct i2c_msg msgs[2];
183 	unsigned int recv_size = len * sizeof(uint32_t);
184 	int ret;
185 
186 	recv_buf = kzalloc(recv_size, GFP_KERNEL | GFP_DMA);
187 	if (!recv_buf)
188 		return -ENOMEM;
189 
190 	send_buf = reg;
191 
192 	msgs[0].addr = client->addr;
193 	msgs[0].len = sizeof(send_buf);
194 	msgs[0].buf = &send_buf;
195 	msgs[0].flags = 0;
196 
197 	msgs[1].addr = client->addr;
198 	msgs[1].len = recv_size;
199 	msgs[1].buf = recv_buf;
200 	msgs[1].flags = I2C_M_RD;
201 
202 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
203 	if (ret < 0)
204 		goto err_ret;
205 	else if (ret != ARRAY_SIZE(msgs)) {
206 		ret = -EIO;
207 		goto err_ret;
208 	}
209 
210 	for (i = 0, p = recv_buf; i < len; i++, p += sizeof(uint32_t))
211 		values[i] = get_unaligned_be32(p);
212 
213 err_ret:
214 	kfree(recv_buf);
215 	return ret;
216 }
217 
218 /*
219  * Integer array controls for setting biquad, mixer, DRC coefficients.
220  * According to the datasheet each coefficient is effectively 26bits,
221  * i.e. stored as 32bits, where bits [31:26] are ignored.
222  * TI's TAS57xx Graphical Development Environment tool however produces
223  * coefficients with more than 26 bits. For this reason we allow values
224  * in the full 32-bits reange.
225  * The coefficients are ordered as given in the TAS571x data sheet:
226  * b0, b1, b2, a1, a2
227  */
228 
229 static int tas571x_coefficient_info(struct snd_kcontrol *kcontrol,
230 				   struct snd_ctl_elem_info *uinfo)
231 {
232 	int numcoef = kcontrol->private_value >> 16;
233 
234 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
235 	uinfo->count = numcoef;
236 	uinfo->value.integer.min = 0;
237 	uinfo->value.integer.max = 0xffffffff;
238 	return 0;
239 }
240 
241 static int tas571x_coefficient_get(struct snd_kcontrol *kcontrol,
242 				  struct snd_ctl_elem_value *ucontrol)
243 {
244 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
245 	struct i2c_client *i2c = to_i2c_client(component->dev);
246 	int numcoef = kcontrol->private_value >> 16;
247 	int index = kcontrol->private_value & 0xffff;
248 
249 	return tas571x_reg_read_multiword(i2c, index,
250 		ucontrol->value.integer.value, numcoef);
251 }
252 
253 static int tas571x_coefficient_put(struct snd_kcontrol *kcontrol,
254 				  struct snd_ctl_elem_value *ucontrol)
255 {
256 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
257 	struct i2c_client *i2c = to_i2c_client(component->dev);
258 	int numcoef = kcontrol->private_value >> 16;
259 	int index = kcontrol->private_value & 0xffff;
260 
261 	return tas571x_reg_write_multiword(i2c, index,
262 		ucontrol->value.integer.value, numcoef);
263 }
264 
265 static int tas571x_set_dai_fmt(struct snd_soc_dai *dai, unsigned int format)
266 {
267 	struct tas571x_private *priv = snd_soc_component_get_drvdata(dai->component);
268 
269 	priv->format = format;
270 
271 	return 0;
272 }
273 
274 static int tas571x_hw_params(struct snd_pcm_substream *substream,
275 			     struct snd_pcm_hw_params *params,
276 			     struct snd_soc_dai *dai)
277 {
278 	struct tas571x_private *priv = snd_soc_component_get_drvdata(dai->component);
279 	u32 val;
280 
281 	switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
282 	case SND_SOC_DAIFMT_RIGHT_J:
283 		val = 0x00;
284 		break;
285 	case SND_SOC_DAIFMT_I2S:
286 		val = 0x03;
287 		break;
288 	case SND_SOC_DAIFMT_LEFT_J:
289 		val = 0x06;
290 		break;
291 	default:
292 		return -EINVAL;
293 	}
294 
295 	if (params_width(params) >= 24)
296 		val += 2;
297 	else if (params_width(params) >= 20)
298 		val += 1;
299 
300 	return regmap_update_bits(priv->regmap, TAS571X_SDI_REG,
301 				  TAS571X_SDI_FMT_MASK, val);
302 }
303 
304 static int tas571x_mute(struct snd_soc_dai *dai, int mute)
305 {
306 	struct snd_soc_component *component = dai->component;
307 	u8 sysctl2;
308 	int ret;
309 
310 	sysctl2 = mute ? TAS571X_SYS_CTRL_2_SDN_MASK : 0;
311 
312 	ret = snd_soc_component_update_bits(component,
313 			    TAS571X_SYS_CTRL_2_REG,
314 		     TAS571X_SYS_CTRL_2_SDN_MASK,
315 		     sysctl2);
316 	usleep_range(1000, 2000);
317 
318 	return ret;
319 }
320 
321 static int tas571x_set_bias_level(struct snd_soc_component *component,
322 				  enum snd_soc_bias_level level)
323 {
324 	struct tas571x_private *priv = snd_soc_component_get_drvdata(component);
325 	int ret;
326 
327 	switch (level) {
328 	case SND_SOC_BIAS_ON:
329 		break;
330 	case SND_SOC_BIAS_PREPARE:
331 		break;
332 	case SND_SOC_BIAS_STANDBY:
333 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
334 			if (!IS_ERR(priv->mclk)) {
335 				ret = clk_prepare_enable(priv->mclk);
336 				if (ret) {
337 					dev_err(component->dev,
338 						"Failed to enable master clock: %d\n",
339 						ret);
340 					return ret;
341 				}
342 			}
343 		}
344 		break;
345 	case SND_SOC_BIAS_OFF:
346 		if (!IS_ERR(priv->mclk))
347 			clk_disable_unprepare(priv->mclk);
348 		break;
349 	}
350 
351 	return 0;
352 }
353 
354 static const struct snd_soc_dai_ops tas571x_dai_ops = {
355 	.set_fmt	= tas571x_set_dai_fmt,
356 	.hw_params	= tas571x_hw_params,
357 	.digital_mute	= tas571x_mute,
358 };
359 
360 
361 #define BIQUAD_COEFS(xname, reg) \
362 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
363 	.info = tas571x_coefficient_info, \
364 	.get = tas571x_coefficient_get,\
365 	.put = tas571x_coefficient_put, \
366 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
367 	.private_value = reg | (5 << 16) }
368 
369 static const char *const tas5711_supply_names[] = {
370 	"AVDD",
371 	"DVDD",
372 	"PVDD_A",
373 	"PVDD_B",
374 	"PVDD_C",
375 	"PVDD_D",
376 };
377 
378 static const DECLARE_TLV_DB_SCALE(tas5711_volume_tlv, -10350, 50, 1);
379 
380 static const struct snd_kcontrol_new tas5711_controls[] = {
381 	SOC_SINGLE_TLV("Master Volume",
382 		       TAS571X_MVOL_REG,
383 		       0, 0xff, 1, tas5711_volume_tlv),
384 	SOC_DOUBLE_R_TLV("Speaker Volume",
385 			 TAS571X_CH1_VOL_REG,
386 			 TAS571X_CH2_VOL_REG,
387 			 0, 0xff, 1, tas5711_volume_tlv),
388 	SOC_DOUBLE("Speaker Switch",
389 		   TAS571X_SOFT_MUTE_REG,
390 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
391 		   1, 1),
392 };
393 
394 static const struct regmap_range tas571x_readonly_regs_range[] = {
395 	regmap_reg_range(TAS571X_CLK_CTRL_REG,  TAS571X_DEV_ID_REG),
396 };
397 
398 static const struct regmap_range tas571x_volatile_regs_range[] = {
399 	regmap_reg_range(TAS571X_CLK_CTRL_REG,  TAS571X_ERR_STATUS_REG),
400 	regmap_reg_range(TAS571X_OSC_TRIM_REG,  TAS571X_OSC_TRIM_REG),
401 };
402 
403 static const struct regmap_access_table tas571x_write_regs = {
404 	.no_ranges =	tas571x_readonly_regs_range,
405 	.n_no_ranges =	ARRAY_SIZE(tas571x_readonly_regs_range),
406 };
407 
408 static const struct regmap_access_table tas571x_volatile_regs = {
409 	.yes_ranges =	tas571x_volatile_regs_range,
410 	.n_yes_ranges =	ARRAY_SIZE(tas571x_volatile_regs_range),
411 
412 };
413 
414 static const struct reg_default tas5711_reg_defaults[] = {
415 	{ 0x04, 0x05 },
416 	{ 0x05, 0x40 },
417 	{ 0x06, 0x00 },
418 	{ 0x07, 0xff },
419 	{ 0x08, 0x30 },
420 	{ 0x09, 0x30 },
421 	{ 0x1b, 0x82 },
422 };
423 
424 static const struct regmap_config tas5711_regmap_config = {
425 	.reg_bits			= 8,
426 	.val_bits			= 32,
427 	.max_register			= 0xff,
428 	.reg_read			= tas571x_reg_read,
429 	.reg_write			= tas571x_reg_write,
430 	.reg_defaults			= tas5711_reg_defaults,
431 	.num_reg_defaults		= ARRAY_SIZE(tas5711_reg_defaults),
432 	.cache_type			= REGCACHE_RBTREE,
433 	.wr_table			= &tas571x_write_regs,
434 	.volatile_table			= &tas571x_volatile_regs,
435 };
436 
437 static const struct tas571x_chip tas5711_chip = {
438 	.supply_names			= tas5711_supply_names,
439 	.num_supply_names		= ARRAY_SIZE(tas5711_supply_names),
440 	.controls			= tas5711_controls,
441 	.num_controls			= ARRAY_SIZE(tas5711_controls),
442 	.regmap_config			= &tas5711_regmap_config,
443 	.vol_reg_size			= 1,
444 };
445 
446 static const struct regmap_range tas5707_volatile_regs_range[] = {
447 	regmap_reg_range(TAS571X_CLK_CTRL_REG,  TAS571X_ERR_STATUS_REG),
448 	regmap_reg_range(TAS571X_OSC_TRIM_REG,  TAS571X_OSC_TRIM_REG),
449 	regmap_reg_range(TAS5707_CH1_BQ0_REG, TAS5707_CH2_BQ6_REG),
450 };
451 
452 static const struct regmap_access_table tas5707_volatile_regs = {
453 	.yes_ranges =	tas5707_volatile_regs_range,
454 	.n_yes_ranges =	ARRAY_SIZE(tas5707_volatile_regs_range),
455 
456 };
457 
458 static const DECLARE_TLV_DB_SCALE(tas5707_volume_tlv, -7900, 50, 1);
459 
460 static const char * const tas5707_volume_slew_step_txt[] = {
461 	"256", "512", "1024", "2048",
462 };
463 
464 static const unsigned int tas5707_volume_slew_step_values[] = {
465 	3, 0, 1, 2,
466 };
467 
468 static SOC_VALUE_ENUM_SINGLE_DECL(tas5707_volume_slew_step_enum,
469 				  TAS571X_VOL_CFG_REG, 0, 0x3,
470 				  tas5707_volume_slew_step_txt,
471 				  tas5707_volume_slew_step_values);
472 
473 static const struct snd_kcontrol_new tas5707_controls[] = {
474 	SOC_SINGLE_TLV("Master Volume",
475 		       TAS571X_MVOL_REG,
476 		       0, 0xff, 1, tas5707_volume_tlv),
477 	SOC_DOUBLE_R_TLV("Speaker Volume",
478 			 TAS571X_CH1_VOL_REG,
479 			 TAS571X_CH2_VOL_REG,
480 			 0, 0xff, 1, tas5707_volume_tlv),
481 	SOC_DOUBLE("Speaker Switch",
482 		   TAS571X_SOFT_MUTE_REG,
483 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
484 		   1, 1),
485 
486 	SOC_ENUM("Slew Rate Steps", tas5707_volume_slew_step_enum),
487 
488 	BIQUAD_COEFS("CH1 - Biquad 0", TAS5707_CH1_BQ0_REG),
489 	BIQUAD_COEFS("CH1 - Biquad 1", TAS5707_CH1_BQ1_REG),
490 	BIQUAD_COEFS("CH1 - Biquad 2", TAS5707_CH1_BQ2_REG),
491 	BIQUAD_COEFS("CH1 - Biquad 3", TAS5707_CH1_BQ3_REG),
492 	BIQUAD_COEFS("CH1 - Biquad 4", TAS5707_CH1_BQ4_REG),
493 	BIQUAD_COEFS("CH1 - Biquad 5", TAS5707_CH1_BQ5_REG),
494 	BIQUAD_COEFS("CH1 - Biquad 6", TAS5707_CH1_BQ6_REG),
495 
496 	BIQUAD_COEFS("CH2 - Biquad 0", TAS5707_CH2_BQ0_REG),
497 	BIQUAD_COEFS("CH2 - Biquad 1", TAS5707_CH2_BQ1_REG),
498 	BIQUAD_COEFS("CH2 - Biquad 2", TAS5707_CH2_BQ2_REG),
499 	BIQUAD_COEFS("CH2 - Biquad 3", TAS5707_CH2_BQ3_REG),
500 	BIQUAD_COEFS("CH2 - Biquad 4", TAS5707_CH2_BQ4_REG),
501 	BIQUAD_COEFS("CH2 - Biquad 5", TAS5707_CH2_BQ5_REG),
502 	BIQUAD_COEFS("CH2 - Biquad 6", TAS5707_CH2_BQ6_REG),
503 };
504 
505 static const struct reg_default tas5707_reg_defaults[] = {
506 	{TAS571X_CLK_CTRL_REG,		0x6c},
507 	{TAS571X_DEV_ID_REG,		0x70},
508 	{TAS571X_ERR_STATUS_REG,	0x00},
509 	{TAS571X_SYS_CTRL_1_REG,	0xa0},
510 	{TAS571X_SDI_REG,		0x05},
511 	{TAS571X_SYS_CTRL_2_REG,	0x40},
512 	{TAS571X_SOFT_MUTE_REG,		0x00},
513 	{TAS571X_MVOL_REG,		0xff},
514 	{TAS571X_CH1_VOL_REG,		0x30},
515 	{TAS571X_CH2_VOL_REG,		0x30},
516 	{TAS571X_VOL_CFG_REG,		0x91},
517 	{TAS571X_MODULATION_LIMIT_REG,	0x02},
518 	{TAS571X_IC_DELAY_CH1_REG,	0xac},
519 	{TAS571X_IC_DELAY_CH2_REG,	0x54},
520 	{TAS571X_IC_DELAY_CH3_REG,	0xac},
521 	{TAS571X_IC_DELAY_CH4_REG,	0x54},
522 	{TAS571X_START_STOP_PERIOD_REG,	0x0f},
523 	{TAS571X_OSC_TRIM_REG,		0x82},
524 	{TAS571X_BKND_ERR_REG,		0x02},
525 	{TAS571X_INPUT_MUX_REG,		0x17772},
526 	{TAS571X_PWM_MUX_REG,		0x1021345},
527 };
528 
529 static const struct regmap_config tas5707_regmap_config = {
530 	.reg_bits			= 8,
531 	.val_bits			= 32,
532 	.max_register			= 0xff,
533 	.reg_read			= tas571x_reg_read,
534 	.reg_write			= tas571x_reg_write,
535 	.reg_defaults			= tas5707_reg_defaults,
536 	.num_reg_defaults		= ARRAY_SIZE(tas5707_reg_defaults),
537 	.cache_type			= REGCACHE_RBTREE,
538 	.wr_table			= &tas571x_write_regs,
539 	.volatile_table			= &tas5707_volatile_regs,
540 };
541 
542 static const struct tas571x_chip tas5707_chip = {
543 	.supply_names			= tas5711_supply_names,
544 	.num_supply_names		= ARRAY_SIZE(tas5711_supply_names),
545 	.controls			= tas5707_controls,
546 	.num_controls			= ARRAY_SIZE(tas5707_controls),
547 	.regmap_config			= &tas5707_regmap_config,
548 	.vol_reg_size			= 1,
549 };
550 
551 static const char *const tas5717_supply_names[] = {
552 	"AVDD",
553 	"DVDD",
554 	"HPVDD",
555 	"PVDD_AB",
556 	"PVDD_CD",
557 };
558 
559 static const DECLARE_TLV_DB_SCALE(tas5717_volume_tlv, -10375, 25, 0);
560 
561 static const struct snd_kcontrol_new tas5717_controls[] = {
562 	/* MVOL LSB is ignored - see comments in tas571x_i2c_probe() */
563 	SOC_SINGLE_TLV("Master Volume",
564 		       TAS571X_MVOL_REG, 1, 0x1ff, 1,
565 		       tas5717_volume_tlv),
566 	SOC_DOUBLE_R_TLV("Speaker Volume",
567 			 TAS571X_CH1_VOL_REG, TAS571X_CH2_VOL_REG,
568 			 1, 0x1ff, 1, tas5717_volume_tlv),
569 	SOC_DOUBLE("Speaker Switch",
570 		   TAS571X_SOFT_MUTE_REG,
571 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
572 		   1, 1),
573 
574 	SOC_DOUBLE_R_RANGE("CH1 Mixer Volume",
575 			   TAS5717_CH1_LEFT_CH_MIX_REG,
576 			   TAS5717_CH1_RIGHT_CH_MIX_REG,
577 			   16, 0, 0x80, 0),
578 
579 	SOC_DOUBLE_R_RANGE("CH2 Mixer Volume",
580 			   TAS5717_CH2_LEFT_CH_MIX_REG,
581 			   TAS5717_CH2_RIGHT_CH_MIX_REG,
582 			   16, 0, 0x80, 0),
583 
584 	/*
585 	 * The biquads are named according to the register names.
586 	 * Please note that TI's TAS57xx Graphical Development Environment
587 	 * tool names them different.
588 	 */
589 	BIQUAD_COEFS("CH1 - Biquad 0", TAS5717_CH1_BQ0_REG),
590 	BIQUAD_COEFS("CH1 - Biquad 1", TAS5717_CH1_BQ1_REG),
591 	BIQUAD_COEFS("CH1 - Biquad 2", TAS5717_CH1_BQ2_REG),
592 	BIQUAD_COEFS("CH1 - Biquad 3", TAS5717_CH1_BQ3_REG),
593 	BIQUAD_COEFS("CH1 - Biquad 4", TAS5717_CH1_BQ4_REG),
594 	BIQUAD_COEFS("CH1 - Biquad 5", TAS5717_CH1_BQ5_REG),
595 	BIQUAD_COEFS("CH1 - Biquad 6", TAS5717_CH1_BQ6_REG),
596 	BIQUAD_COEFS("CH1 - Biquad 7", TAS5717_CH1_BQ7_REG),
597 	BIQUAD_COEFS("CH1 - Biquad 8", TAS5717_CH1_BQ8_REG),
598 	BIQUAD_COEFS("CH1 - Biquad 9", TAS5717_CH1_BQ9_REG),
599 	BIQUAD_COEFS("CH1 - Biquad 10", TAS5717_CH1_BQ10_REG),
600 	BIQUAD_COEFS("CH1 - Biquad 11", TAS5717_CH1_BQ11_REG),
601 
602 	BIQUAD_COEFS("CH2 - Biquad 0", TAS5717_CH2_BQ0_REG),
603 	BIQUAD_COEFS("CH2 - Biquad 1", TAS5717_CH2_BQ1_REG),
604 	BIQUAD_COEFS("CH2 - Biquad 2", TAS5717_CH2_BQ2_REG),
605 	BIQUAD_COEFS("CH2 - Biquad 3", TAS5717_CH2_BQ3_REG),
606 	BIQUAD_COEFS("CH2 - Biquad 4", TAS5717_CH2_BQ4_REG),
607 	BIQUAD_COEFS("CH2 - Biquad 5", TAS5717_CH2_BQ5_REG),
608 	BIQUAD_COEFS("CH2 - Biquad 6", TAS5717_CH2_BQ6_REG),
609 	BIQUAD_COEFS("CH2 - Biquad 7", TAS5717_CH2_BQ7_REG),
610 	BIQUAD_COEFS("CH2 - Biquad 8", TAS5717_CH2_BQ8_REG),
611 	BIQUAD_COEFS("CH2 - Biquad 9", TAS5717_CH2_BQ9_REG),
612 	BIQUAD_COEFS("CH2 - Biquad 10", TAS5717_CH2_BQ10_REG),
613 	BIQUAD_COEFS("CH2 - Biquad 11", TAS5717_CH2_BQ11_REG),
614 
615 	BIQUAD_COEFS("CH3 - Biquad 0", TAS5717_CH3_BQ0_REG),
616 	BIQUAD_COEFS("CH3 - Biquad 1", TAS5717_CH3_BQ1_REG),
617 
618 	BIQUAD_COEFS("CH4 - Biquad 0", TAS5717_CH4_BQ0_REG),
619 	BIQUAD_COEFS("CH4 - Biquad 1", TAS5717_CH4_BQ1_REG),
620 };
621 
622 static const struct reg_default tas5717_reg_defaults[] = {
623 	{ 0x04, 0x05 },
624 	{ 0x05, 0x40 },
625 	{ 0x06, 0x00 },
626 	{ 0x07, 0x03ff },
627 	{ 0x08, 0x00c0 },
628 	{ 0x09, 0x00c0 },
629 	{ 0x1b, 0x82 },
630 	{ TAS5717_CH1_RIGHT_CH_MIX_REG, 0x0 },
631 	{ TAS5717_CH1_LEFT_CH_MIX_REG, 0x800000},
632 	{ TAS5717_CH2_LEFT_CH_MIX_REG, 0x0 },
633 	{ TAS5717_CH2_RIGHT_CH_MIX_REG, 0x800000},
634 };
635 
636 static const struct regmap_config tas5717_regmap_config = {
637 	.reg_bits			= 8,
638 	.val_bits			= 32,
639 	.max_register			= 0xff,
640 	.reg_read			= tas571x_reg_read,
641 	.reg_write			= tas571x_reg_write,
642 	.reg_defaults			= tas5717_reg_defaults,
643 	.num_reg_defaults		= ARRAY_SIZE(tas5717_reg_defaults),
644 	.cache_type			= REGCACHE_RBTREE,
645 	.wr_table			= &tas571x_write_regs,
646 	.volatile_table			= &tas571x_volatile_regs,
647 };
648 
649 /* This entry is reused for tas5719 as the software interface is identical. */
650 static const struct tas571x_chip tas5717_chip = {
651 	.supply_names			= tas5717_supply_names,
652 	.num_supply_names		= ARRAY_SIZE(tas5717_supply_names),
653 	.controls			= tas5717_controls,
654 	.num_controls			= ARRAY_SIZE(tas5717_controls),
655 	.regmap_config			= &tas5717_regmap_config,
656 	.vol_reg_size			= 2,
657 };
658 
659 static const char *const tas5721_supply_names[] = {
660 	"AVDD",
661 	"DVDD",
662 	"DRVDD",
663 	"PVDD",
664 };
665 
666 static const struct snd_kcontrol_new tas5721_controls[] = {
667 	SOC_SINGLE_TLV("Master Volume",
668 		       TAS571X_MVOL_REG,
669 		       0, 0xff, 1, tas5711_volume_tlv),
670 	SOC_DOUBLE_R_TLV("Speaker Volume",
671 			 TAS571X_CH1_VOL_REG,
672 			 TAS571X_CH2_VOL_REG,
673 			 0, 0xff, 1, tas5711_volume_tlv),
674 	SOC_DOUBLE("Speaker Switch",
675 		   TAS571X_SOFT_MUTE_REG,
676 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
677 		   1, 1),
678 };
679 
680 static const struct reg_default tas5721_reg_defaults[] = {
681 	{TAS571X_CLK_CTRL_REG,		0x6c},
682 	{TAS571X_DEV_ID_REG,		0x00},
683 	{TAS571X_ERR_STATUS_REG,	0x00},
684 	{TAS571X_SYS_CTRL_1_REG,	0xa0},
685 	{TAS571X_SDI_REG,		0x05},
686 	{TAS571X_SYS_CTRL_2_REG,	0x40},
687 	{TAS571X_SOFT_MUTE_REG,		0x00},
688 	{TAS571X_MVOL_REG,		0xff},
689 	{TAS571X_CH1_VOL_REG,		0x30},
690 	{TAS571X_CH2_VOL_REG,		0x30},
691 	{TAS571X_CH3_VOL_REG,		0x30},
692 	{TAS571X_VOL_CFG_REG,		0x91},
693 	{TAS571X_MODULATION_LIMIT_REG,	0x02},
694 	{TAS571X_IC_DELAY_CH1_REG,	0xac},
695 	{TAS571X_IC_DELAY_CH2_REG,	0x54},
696 	{TAS571X_IC_DELAY_CH3_REG,	0xac},
697 	{TAS571X_IC_DELAY_CH4_REG,	0x54},
698 	{TAS571X_PWM_CH_SDN_GROUP_REG,	0x30},
699 	{TAS571X_START_STOP_PERIOD_REG,	0x0f},
700 	{TAS571X_OSC_TRIM_REG,		0x82},
701 	{TAS571X_BKND_ERR_REG,		0x02},
702 	{TAS571X_INPUT_MUX_REG,		0x17772},
703 	{TAS571X_CH4_SRC_SELECT_REG,	0x4303},
704 	{TAS571X_PWM_MUX_REG,		0x1021345},
705 };
706 
707 static const struct regmap_config tas5721_regmap_config = {
708 	.reg_bits			= 8,
709 	.val_bits			= 32,
710 	.max_register			= 0xff,
711 	.reg_read			= tas571x_reg_read,
712 	.reg_write			= tas571x_reg_write,
713 	.reg_defaults			= tas5721_reg_defaults,
714 	.num_reg_defaults		= ARRAY_SIZE(tas5721_reg_defaults),
715 	.cache_type			= REGCACHE_RBTREE,
716 	.wr_table			= &tas571x_write_regs,
717 	.volatile_table			= &tas571x_volatile_regs,
718 };
719 
720 
721 static const struct tas571x_chip tas5721_chip = {
722 	.supply_names			= tas5721_supply_names,
723 	.num_supply_names		= ARRAY_SIZE(tas5721_supply_names),
724 	.controls			= tas5721_controls,
725 	.num_controls			= ARRAY_SIZE(tas5721_controls),
726 	.regmap_config			= &tas5721_regmap_config,
727 	.vol_reg_size			= 1,
728 };
729 
730 static const struct snd_soc_dapm_widget tas571x_dapm_widgets[] = {
731 	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
732 	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
733 
734 	SND_SOC_DAPM_OUTPUT("OUT_A"),
735 	SND_SOC_DAPM_OUTPUT("OUT_B"),
736 	SND_SOC_DAPM_OUTPUT("OUT_C"),
737 	SND_SOC_DAPM_OUTPUT("OUT_D"),
738 };
739 
740 static const struct snd_soc_dapm_route tas571x_dapm_routes[] = {
741 	{ "DACL",  NULL, "Playback" },
742 	{ "DACR",  NULL, "Playback" },
743 
744 	{ "OUT_A", NULL, "DACL" },
745 	{ "OUT_B", NULL, "DACL" },
746 	{ "OUT_C", NULL, "DACR" },
747 	{ "OUT_D", NULL, "DACR" },
748 };
749 
750 static const struct snd_soc_component_driver tas571x_component = {
751 	.set_bias_level		= tas571x_set_bias_level,
752 	.dapm_widgets		= tas571x_dapm_widgets,
753 	.num_dapm_widgets	= ARRAY_SIZE(tas571x_dapm_widgets),
754 	.dapm_routes		= tas571x_dapm_routes,
755 	.num_dapm_routes	= ARRAY_SIZE(tas571x_dapm_routes),
756 	.use_pmdown_time	= 1,
757 	.endianness		= 1,
758 	.non_legacy_dai_naming	= 1,
759 };
760 
761 static struct snd_soc_dai_driver tas571x_dai = {
762 	.name = "tas571x-hifi",
763 	.playback = {
764 		.stream_name = "Playback",
765 		.channels_min = 2,
766 		.channels_max = 2,
767 		.rates = SNDRV_PCM_RATE_8000_48000,
768 		.formats = SNDRV_PCM_FMTBIT_S32_LE |
769 			   SNDRV_PCM_FMTBIT_S24_LE |
770 			   SNDRV_PCM_FMTBIT_S16_LE,
771 	},
772 	.ops = &tas571x_dai_ops,
773 };
774 
775 static const struct of_device_id tas571x_of_match[];
776 
777 static int tas571x_i2c_probe(struct i2c_client *client,
778 			     const struct i2c_device_id *id)
779 {
780 	struct tas571x_private *priv;
781 	struct device *dev = &client->dev;
782 	const struct of_device_id *of_id;
783 	int i, ret;
784 
785 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
786 	if (!priv)
787 		return -ENOMEM;
788 	i2c_set_clientdata(client, priv);
789 
790 	of_id = of_match_device(tas571x_of_match, dev);
791 	if (of_id)
792 		priv->chip = of_id->data;
793 	else
794 		priv->chip = (void *) id->driver_data;
795 
796 	priv->mclk = devm_clk_get(dev, "mclk");
797 	if (IS_ERR(priv->mclk) && PTR_ERR(priv->mclk) != -ENOENT) {
798 		dev_err(dev, "Failed to request mclk: %ld\n",
799 			PTR_ERR(priv->mclk));
800 		return PTR_ERR(priv->mclk);
801 	}
802 
803 	if (WARN_ON(priv->chip->num_supply_names > TAS571X_MAX_SUPPLIES))
804 		return -EINVAL;
805 	for (i = 0; i < priv->chip->num_supply_names; i++)
806 		priv->supplies[i].supply = priv->chip->supply_names[i];
807 
808 	ret = devm_regulator_bulk_get(dev, priv->chip->num_supply_names,
809 				      priv->supplies);
810 	if (ret) {
811 		dev_err(dev, "Failed to get supplies: %d\n", ret);
812 		return ret;
813 	}
814 	ret = regulator_bulk_enable(priv->chip->num_supply_names,
815 				    priv->supplies);
816 	if (ret) {
817 		dev_err(dev, "Failed to enable supplies: %d\n", ret);
818 		return ret;
819 	}
820 
821 	priv->regmap = devm_regmap_init(dev, NULL, client,
822 					priv->chip->regmap_config);
823 	if (IS_ERR(priv->regmap)) {
824 		ret = PTR_ERR(priv->regmap);
825 		goto disable_regs;
826 	}
827 
828 	priv->pdn_gpio = devm_gpiod_get_optional(dev, "pdn", GPIOD_OUT_LOW);
829 	if (IS_ERR(priv->pdn_gpio)) {
830 		dev_err(dev, "error requesting pdn_gpio: %ld\n",
831 			PTR_ERR(priv->pdn_gpio));
832 		return PTR_ERR(priv->pdn_gpio);
833 	}
834 
835 	priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
836 						   GPIOD_OUT_HIGH);
837 	if (IS_ERR(priv->reset_gpio)) {
838 		dev_err(dev, "error requesting reset_gpio: %ld\n",
839 			PTR_ERR(priv->reset_gpio));
840 		return PTR_ERR(priv->reset_gpio);
841 	} else if (priv->reset_gpio) {
842 		/* pulse the active low reset line for ~100us */
843 		usleep_range(100, 200);
844 		gpiod_set_value(priv->reset_gpio, 0);
845 		usleep_range(13500, 20000);
846 	}
847 
848 	ret = regmap_write(priv->regmap, TAS571X_OSC_TRIM_REG, 0);
849 	if (ret)
850 		goto disable_regs;
851 
852 	usleep_range(50000, 60000);
853 
854 	memcpy(&priv->component_driver, &tas571x_component, sizeof(priv->component_driver));
855 	priv->component_driver.controls = priv->chip->controls;
856 	priv->component_driver.num_controls = priv->chip->num_controls;
857 
858 	if (priv->chip->vol_reg_size == 2) {
859 		/*
860 		 * The master volume defaults to 0x3ff (mute), but we ignore
861 		 * (zero) the LSB because the hardware step size is 0.125 dB
862 		 * and TLV_DB_SCALE_ITEM has a resolution of 0.01 dB.
863 		 */
864 		ret = regmap_update_bits(priv->regmap, TAS571X_MVOL_REG, 1, 0);
865 		if (ret)
866 			goto disable_regs;
867 	}
868 
869 	ret = devm_snd_soc_register_component(&client->dev,
870 				      &priv->component_driver,
871 				      &tas571x_dai, 1);
872 	if (ret)
873 		goto disable_regs;
874 
875 	return ret;
876 
877 disable_regs:
878 	regulator_bulk_disable(priv->chip->num_supply_names, priv->supplies);
879 	return ret;
880 }
881 
882 static int tas571x_i2c_remove(struct i2c_client *client)
883 {
884 	struct tas571x_private *priv = i2c_get_clientdata(client);
885 
886 	regulator_bulk_disable(priv->chip->num_supply_names, priv->supplies);
887 
888 	return 0;
889 }
890 
891 static const struct of_device_id tas571x_of_match[] = {
892 	{ .compatible = "ti,tas5707", .data = &tas5707_chip, },
893 	{ .compatible = "ti,tas5711", .data = &tas5711_chip, },
894 	{ .compatible = "ti,tas5717", .data = &tas5717_chip, },
895 	{ .compatible = "ti,tas5719", .data = &tas5717_chip, },
896 	{ .compatible = "ti,tas5721", .data = &tas5721_chip, },
897 	{ }
898 };
899 MODULE_DEVICE_TABLE(of, tas571x_of_match);
900 
901 static const struct i2c_device_id tas571x_i2c_id[] = {
902 	{ "tas5707", (kernel_ulong_t) &tas5707_chip },
903 	{ "tas5711", (kernel_ulong_t) &tas5711_chip },
904 	{ "tas5717", (kernel_ulong_t) &tas5717_chip },
905 	{ "tas5719", (kernel_ulong_t) &tas5717_chip },
906 	{ "tas5721", (kernel_ulong_t) &tas5721_chip },
907 	{ }
908 };
909 MODULE_DEVICE_TABLE(i2c, tas571x_i2c_id);
910 
911 static struct i2c_driver tas571x_i2c_driver = {
912 	.driver = {
913 		.name = "tas571x",
914 		.of_match_table = of_match_ptr(tas571x_of_match),
915 	},
916 	.probe = tas571x_i2c_probe,
917 	.remove = tas571x_i2c_remove,
918 	.id_table = tas571x_i2c_id,
919 };
920 module_i2c_driver(tas571x_i2c_driver);
921 
922 MODULE_DESCRIPTION("ASoC TAS571x driver");
923 MODULE_AUTHOR("Kevin Cernekee <cernekee@chromium.org>");
924 MODULE_LICENSE("GPL");
925