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