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