xref: /openbmc/linux/sound/soc/codecs/tas571x.c (revision 293d5b43)
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 			gpiod_set_value(priv->pdn_gpio, 0);
346 			usleep_range(5000, 6000);
347 
348 			regcache_cache_only(priv->regmap, false);
349 			ret = regcache_sync(priv->regmap);
350 			if (ret)
351 				return ret;
352 		}
353 		break;
354 	case SND_SOC_BIAS_OFF:
355 		regcache_cache_only(priv->regmap, true);
356 		gpiod_set_value(priv->pdn_gpio, 1);
357 
358 		if (!IS_ERR(priv->mclk))
359 			clk_disable_unprepare(priv->mclk);
360 		break;
361 	}
362 
363 	return 0;
364 }
365 
366 static const struct snd_soc_dai_ops tas571x_dai_ops = {
367 	.set_fmt	= tas571x_set_dai_fmt,
368 	.hw_params	= tas571x_hw_params,
369 	.digital_mute	= tas571x_mute,
370 };
371 
372 
373 #define BIQUAD_COEFS(xname, reg) \
374 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
375 	.info = tas571x_coefficient_info, \
376 	.get = tas571x_coefficient_get,\
377 	.put = tas571x_coefficient_put, \
378 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
379 	.private_value = reg | (5 << 16) }
380 
381 static const char *const tas5711_supply_names[] = {
382 	"AVDD",
383 	"DVDD",
384 	"PVDD_A",
385 	"PVDD_B",
386 	"PVDD_C",
387 	"PVDD_D",
388 };
389 
390 static const DECLARE_TLV_DB_SCALE(tas5711_volume_tlv, -10350, 50, 1);
391 
392 static const struct snd_kcontrol_new tas5711_controls[] = {
393 	SOC_SINGLE_TLV("Master Volume",
394 		       TAS571X_MVOL_REG,
395 		       0, 0xff, 1, tas5711_volume_tlv),
396 	SOC_DOUBLE_R_TLV("Speaker Volume",
397 			 TAS571X_CH1_VOL_REG,
398 			 TAS571X_CH2_VOL_REG,
399 			 0, 0xff, 1, tas5711_volume_tlv),
400 	SOC_DOUBLE("Speaker Switch",
401 		   TAS571X_SOFT_MUTE_REG,
402 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
403 		   1, 1),
404 
405 	SOC_DOUBLE_R_RANGE("CH1 Mixer Volume",
406 			   TAS5717_CH1_LEFT_CH_MIX_REG,
407 			   TAS5717_CH1_RIGHT_CH_MIX_REG,
408 			   16, 0, 0x80, 0),
409 
410 	SOC_DOUBLE_R_RANGE("CH2 Mixer Volume",
411 			   TAS5717_CH2_LEFT_CH_MIX_REG,
412 			   TAS5717_CH2_RIGHT_CH_MIX_REG,
413 			   16, 0, 0x80, 0),
414 };
415 
416 static const struct regmap_range tas571x_readonly_regs_range[] = {
417 	regmap_reg_range(TAS571X_CLK_CTRL_REG,  TAS571X_DEV_ID_REG),
418 };
419 
420 static const struct regmap_range tas571x_volatile_regs_range[] = {
421 	regmap_reg_range(TAS571X_CLK_CTRL_REG,  TAS571X_ERR_STATUS_REG),
422 	regmap_reg_range(TAS571X_OSC_TRIM_REG,  TAS571X_OSC_TRIM_REG),
423 };
424 
425 static const struct regmap_access_table tas571x_write_regs = {
426 	.no_ranges =	tas571x_readonly_regs_range,
427 	.n_no_ranges =	ARRAY_SIZE(tas571x_readonly_regs_range),
428 };
429 
430 static const struct regmap_access_table tas571x_volatile_regs = {
431 	.yes_ranges =	tas571x_volatile_regs_range,
432 	.n_yes_ranges =	ARRAY_SIZE(tas571x_volatile_regs_range),
433 
434 };
435 
436 static const struct reg_default tas5711_reg_defaults[] = {
437 	{ 0x04, 0x05 },
438 	{ 0x05, 0x40 },
439 	{ 0x06, 0x00 },
440 	{ 0x07, 0xff },
441 	{ 0x08, 0x30 },
442 	{ 0x09, 0x30 },
443 	{ 0x1b, 0x82 },
444 };
445 
446 static const struct regmap_config tas5711_regmap_config = {
447 	.reg_bits			= 8,
448 	.val_bits			= 32,
449 	.max_register			= 0xff,
450 	.reg_read			= tas571x_reg_read,
451 	.reg_write			= tas571x_reg_write,
452 	.reg_defaults			= tas5711_reg_defaults,
453 	.num_reg_defaults		= ARRAY_SIZE(tas5711_reg_defaults),
454 	.cache_type			= REGCACHE_RBTREE,
455 	.wr_table			= &tas571x_write_regs,
456 	.volatile_table			= &tas571x_volatile_regs,
457 };
458 
459 static const struct tas571x_chip tas5711_chip = {
460 	.supply_names			= tas5711_supply_names,
461 	.num_supply_names		= ARRAY_SIZE(tas5711_supply_names),
462 	.controls			= tas5711_controls,
463 	.num_controls			= ARRAY_SIZE(tas5711_controls),
464 	.regmap_config			= &tas5711_regmap_config,
465 	.vol_reg_size			= 1,
466 };
467 
468 static const char *const tas5717_supply_names[] = {
469 	"AVDD",
470 	"DVDD",
471 	"HPVDD",
472 	"PVDD_AB",
473 	"PVDD_CD",
474 };
475 
476 static const DECLARE_TLV_DB_SCALE(tas5717_volume_tlv, -10375, 25, 0);
477 
478 static const struct snd_kcontrol_new tas5717_controls[] = {
479 	/* MVOL LSB is ignored - see comments in tas571x_i2c_probe() */
480 	SOC_SINGLE_TLV("Master Volume",
481 		       TAS571X_MVOL_REG, 1, 0x1ff, 1,
482 		       tas5717_volume_tlv),
483 	SOC_DOUBLE_R_TLV("Speaker Volume",
484 			 TAS571X_CH1_VOL_REG, TAS571X_CH2_VOL_REG,
485 			 1, 0x1ff, 1, tas5717_volume_tlv),
486 	SOC_DOUBLE("Speaker Switch",
487 		   TAS571X_SOFT_MUTE_REG,
488 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
489 		   1, 1),
490 
491 	/*
492 	 * The biquads are named according to the register names.
493 	 * Please note that TI's TAS57xx Graphical Development Environment
494 	 * tool names them different.
495 	 */
496 	BIQUAD_COEFS("CH1 - Biquad 0", TAS5717_CH1_BQ0_REG),
497 	BIQUAD_COEFS("CH1 - Biquad 1", TAS5717_CH1_BQ1_REG),
498 	BIQUAD_COEFS("CH1 - Biquad 2", TAS5717_CH1_BQ2_REG),
499 	BIQUAD_COEFS("CH1 - Biquad 3", TAS5717_CH1_BQ3_REG),
500 	BIQUAD_COEFS("CH1 - Biquad 4", TAS5717_CH1_BQ4_REG),
501 	BIQUAD_COEFS("CH1 - Biquad 5", TAS5717_CH1_BQ5_REG),
502 	BIQUAD_COEFS("CH1 - Biquad 6", TAS5717_CH1_BQ6_REG),
503 	BIQUAD_COEFS("CH1 - Biquad 7", TAS5717_CH1_BQ7_REG),
504 	BIQUAD_COEFS("CH1 - Biquad 8", TAS5717_CH1_BQ8_REG),
505 	BIQUAD_COEFS("CH1 - Biquad 9", TAS5717_CH1_BQ9_REG),
506 	BIQUAD_COEFS("CH1 - Biquad 10", TAS5717_CH1_BQ10_REG),
507 	BIQUAD_COEFS("CH1 - Biquad 11", TAS5717_CH1_BQ11_REG),
508 
509 	BIQUAD_COEFS("CH2 - Biquad 0", TAS5717_CH2_BQ0_REG),
510 	BIQUAD_COEFS("CH2 - Biquad 1", TAS5717_CH2_BQ1_REG),
511 	BIQUAD_COEFS("CH2 - Biquad 2", TAS5717_CH2_BQ2_REG),
512 	BIQUAD_COEFS("CH2 - Biquad 3", TAS5717_CH2_BQ3_REG),
513 	BIQUAD_COEFS("CH2 - Biquad 4", TAS5717_CH2_BQ4_REG),
514 	BIQUAD_COEFS("CH2 - Biquad 5", TAS5717_CH2_BQ5_REG),
515 	BIQUAD_COEFS("CH2 - Biquad 6", TAS5717_CH2_BQ6_REG),
516 	BIQUAD_COEFS("CH2 - Biquad 7", TAS5717_CH2_BQ7_REG),
517 	BIQUAD_COEFS("CH2 - Biquad 8", TAS5717_CH2_BQ8_REG),
518 	BIQUAD_COEFS("CH2 - Biquad 9", TAS5717_CH2_BQ9_REG),
519 	BIQUAD_COEFS("CH2 - Biquad 10", TAS5717_CH2_BQ10_REG),
520 	BIQUAD_COEFS("CH2 - Biquad 11", TAS5717_CH2_BQ11_REG),
521 
522 	BIQUAD_COEFS("CH3 - Biquad 0", TAS5717_CH3_BQ0_REG),
523 	BIQUAD_COEFS("CH3 - Biquad 1", TAS5717_CH3_BQ1_REG),
524 
525 	BIQUAD_COEFS("CH4 - Biquad 0", TAS5717_CH4_BQ0_REG),
526 	BIQUAD_COEFS("CH4 - Biquad 1", TAS5717_CH4_BQ1_REG),
527 };
528 
529 static const struct reg_default tas5717_reg_defaults[] = {
530 	{ 0x04, 0x05 },
531 	{ 0x05, 0x40 },
532 	{ 0x06, 0x00 },
533 	{ 0x07, 0x03ff },
534 	{ 0x08, 0x00c0 },
535 	{ 0x09, 0x00c0 },
536 	{ 0x1b, 0x82 },
537 	{ TAS5717_CH1_RIGHT_CH_MIX_REG, 0x0 },
538 	{ TAS5717_CH1_LEFT_CH_MIX_REG, 0x800000},
539 	{ TAS5717_CH2_LEFT_CH_MIX_REG, 0x0 },
540 	{ TAS5717_CH2_RIGHT_CH_MIX_REG, 0x800000},
541 };
542 
543 static const struct regmap_config tas5717_regmap_config = {
544 	.reg_bits			= 8,
545 	.val_bits			= 32,
546 	.max_register			= 0xff,
547 	.reg_read			= tas571x_reg_read,
548 	.reg_write			= tas571x_reg_write,
549 	.reg_defaults			= tas5717_reg_defaults,
550 	.num_reg_defaults		= ARRAY_SIZE(tas5717_reg_defaults),
551 	.cache_type			= REGCACHE_RBTREE,
552 	.wr_table			= &tas571x_write_regs,
553 	.volatile_table			= &tas571x_volatile_regs,
554 };
555 
556 /* This entry is reused for tas5719 as the software interface is identical. */
557 static const struct tas571x_chip tas5717_chip = {
558 	.supply_names			= tas5717_supply_names,
559 	.num_supply_names		= ARRAY_SIZE(tas5717_supply_names),
560 	.controls			= tas5717_controls,
561 	.num_controls			= ARRAY_SIZE(tas5717_controls),
562 	.regmap_config			= &tas5717_regmap_config,
563 	.vol_reg_size			= 2,
564 };
565 
566 static const char *const tas5721_supply_names[] = {
567 	"AVDD",
568 	"DVDD",
569 	"DRVDD",
570 	"PVDD",
571 };
572 
573 static const struct snd_kcontrol_new tas5721_controls[] = {
574 	SOC_SINGLE_TLV("Master Volume",
575 		       TAS571X_MVOL_REG,
576 		       0, 0xff, 1, tas5711_volume_tlv),
577 	SOC_DOUBLE_R_TLV("Speaker Volume",
578 			 TAS571X_CH1_VOL_REG,
579 			 TAS571X_CH2_VOL_REG,
580 			 0, 0xff, 1, tas5711_volume_tlv),
581 	SOC_DOUBLE("Speaker Switch",
582 		   TAS571X_SOFT_MUTE_REG,
583 		   TAS571X_SOFT_MUTE_CH1_SHIFT, TAS571X_SOFT_MUTE_CH2_SHIFT,
584 		   1, 1),
585 };
586 
587 static const struct reg_default tas5721_reg_defaults[] = {
588 	{TAS571X_CLK_CTRL_REG,		0x6c},
589 	{TAS571X_DEV_ID_REG,		0x00},
590 	{TAS571X_ERR_STATUS_REG,	0x00},
591 	{TAS571X_SYS_CTRL_1_REG,	0xa0},
592 	{TAS571X_SDI_REG,		0x05},
593 	{TAS571X_SYS_CTRL_2_REG,	0x40},
594 	{TAS571X_SOFT_MUTE_REG,		0x00},
595 	{TAS571X_MVOL_REG,		0xff},
596 	{TAS571X_CH1_VOL_REG,		0x30},
597 	{TAS571X_CH2_VOL_REG,		0x30},
598 	{TAS571X_CH3_VOL_REG,		0x30},
599 	{TAS571X_VOL_CFG_REG,		0x91},
600 	{TAS571X_MODULATION_LIMIT_REG,	0x02},
601 	{TAS571X_IC_DELAY_CH1_REG,	0xac},
602 	{TAS571X_IC_DELAY_CH2_REG,	0x54},
603 	{TAS571X_IC_DELAY_CH3_REG,	0xac},
604 	{TAS571X_IC_DELAY_CH4_REG,	0x54},
605 	{TAS571X_PWM_CH_SDN_GROUP_REG,	0x30},
606 	{TAS571X_START_STOP_PERIOD_REG,	0x0f},
607 	{TAS571X_OSC_TRIM_REG,		0x82},
608 	{TAS571X_BKND_ERR_REG,		0x02},
609 	{TAS571X_INPUT_MUX_REG,		0x17772},
610 	{TAS571X_CH4_SRC_SELECT_REG,	0x4303},
611 	{TAS571X_PWM_MUX_REG,		0x1021345},
612 };
613 
614 static const struct regmap_config tas5721_regmap_config = {
615 	.reg_bits			= 8,
616 	.val_bits			= 32,
617 	.max_register			= 0xff,
618 	.reg_read			= tas571x_reg_read,
619 	.reg_write			= tas571x_reg_write,
620 	.reg_defaults			= tas5721_reg_defaults,
621 	.num_reg_defaults		= ARRAY_SIZE(tas5721_reg_defaults),
622 	.cache_type			= REGCACHE_RBTREE,
623 	.wr_table			= &tas571x_write_regs,
624 	.volatile_table			= &tas571x_volatile_regs,
625 };
626 
627 
628 static const struct tas571x_chip tas5721_chip = {
629 	.supply_names			= tas5721_supply_names,
630 	.num_supply_names		= ARRAY_SIZE(tas5721_supply_names),
631 	.controls			= tas5711_controls,
632 	.num_controls			= ARRAY_SIZE(tas5711_controls),
633 	.regmap_config			= &tas5721_regmap_config,
634 	.vol_reg_size			= 1,
635 };
636 
637 static const struct snd_soc_dapm_widget tas571x_dapm_widgets[] = {
638 	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
639 	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
640 
641 	SND_SOC_DAPM_OUTPUT("OUT_A"),
642 	SND_SOC_DAPM_OUTPUT("OUT_B"),
643 	SND_SOC_DAPM_OUTPUT("OUT_C"),
644 	SND_SOC_DAPM_OUTPUT("OUT_D"),
645 };
646 
647 static const struct snd_soc_dapm_route tas571x_dapm_routes[] = {
648 	{ "DACL",  NULL, "Playback" },
649 	{ "DACR",  NULL, "Playback" },
650 
651 	{ "OUT_A", NULL, "DACL" },
652 	{ "OUT_B", NULL, "DACL" },
653 	{ "OUT_C", NULL, "DACR" },
654 	{ "OUT_D", NULL, "DACR" },
655 };
656 
657 static const struct snd_soc_codec_driver tas571x_codec = {
658 	.set_bias_level = tas571x_set_bias_level,
659 	.idle_bias_off = true,
660 
661 	.dapm_widgets = tas571x_dapm_widgets,
662 	.num_dapm_widgets = ARRAY_SIZE(tas571x_dapm_widgets),
663 	.dapm_routes = tas571x_dapm_routes,
664 	.num_dapm_routes = ARRAY_SIZE(tas571x_dapm_routes),
665 };
666 
667 static struct snd_soc_dai_driver tas571x_dai = {
668 	.name = "tas571x-hifi",
669 	.playback = {
670 		.stream_name = "Playback",
671 		.channels_min = 2,
672 		.channels_max = 2,
673 		.rates = SNDRV_PCM_RATE_8000_48000,
674 		.formats = SNDRV_PCM_FMTBIT_S32_LE |
675 			   SNDRV_PCM_FMTBIT_S24_LE |
676 			   SNDRV_PCM_FMTBIT_S16_LE,
677 	},
678 	.ops = &tas571x_dai_ops,
679 };
680 
681 static const struct of_device_id tas571x_of_match[];
682 
683 static int tas571x_i2c_probe(struct i2c_client *client,
684 			     const struct i2c_device_id *id)
685 {
686 	struct tas571x_private *priv;
687 	struct device *dev = &client->dev;
688 	const struct of_device_id *of_id;
689 	int i, ret;
690 
691 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
692 	if (!priv)
693 		return -ENOMEM;
694 	i2c_set_clientdata(client, priv);
695 
696 	of_id = of_match_device(tas571x_of_match, dev);
697 	if (of_id)
698 		priv->chip = of_id->data;
699 	else
700 		priv->chip = (void *) id->driver_data;
701 
702 	priv->mclk = devm_clk_get(dev, "mclk");
703 	if (IS_ERR(priv->mclk) && PTR_ERR(priv->mclk) != -ENOENT) {
704 		dev_err(dev, "Failed to request mclk: %ld\n",
705 			PTR_ERR(priv->mclk));
706 		return PTR_ERR(priv->mclk);
707 	}
708 
709 	BUG_ON(priv->chip->num_supply_names > TAS571X_MAX_SUPPLIES);
710 	for (i = 0; i < priv->chip->num_supply_names; i++)
711 		priv->supplies[i].supply = priv->chip->supply_names[i];
712 
713 	ret = devm_regulator_bulk_get(dev, priv->chip->num_supply_names,
714 				      priv->supplies);
715 	if (ret) {
716 		dev_err(dev, "Failed to get supplies: %d\n", ret);
717 		return ret;
718 	}
719 	ret = regulator_bulk_enable(priv->chip->num_supply_names,
720 				    priv->supplies);
721 	if (ret) {
722 		dev_err(dev, "Failed to enable supplies: %d\n", ret);
723 		return ret;
724 	}
725 
726 	priv->regmap = devm_regmap_init(dev, NULL, client,
727 					priv->chip->regmap_config);
728 	if (IS_ERR(priv->regmap))
729 		return PTR_ERR(priv->regmap);
730 
731 	priv->pdn_gpio = devm_gpiod_get_optional(dev, "pdn", GPIOD_OUT_LOW);
732 	if (IS_ERR(priv->pdn_gpio)) {
733 		dev_err(dev, "error requesting pdn_gpio: %ld\n",
734 			PTR_ERR(priv->pdn_gpio));
735 		return PTR_ERR(priv->pdn_gpio);
736 	}
737 
738 	priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
739 						   GPIOD_OUT_HIGH);
740 	if (IS_ERR(priv->reset_gpio)) {
741 		dev_err(dev, "error requesting reset_gpio: %ld\n",
742 			PTR_ERR(priv->reset_gpio));
743 		return PTR_ERR(priv->reset_gpio);
744 	} else if (priv->reset_gpio) {
745 		/* pulse the active low reset line for ~100us */
746 		usleep_range(100, 200);
747 		gpiod_set_value(priv->reset_gpio, 0);
748 		usleep_range(12000, 20000);
749 	}
750 
751 	ret = regmap_write(priv->regmap, TAS571X_OSC_TRIM_REG, 0);
752 	if (ret)
753 		return ret;
754 
755 
756 	memcpy(&priv->codec_driver, &tas571x_codec, sizeof(priv->codec_driver));
757 	priv->codec_driver.controls = priv->chip->controls;
758 	priv->codec_driver.num_controls = priv->chip->num_controls;
759 
760 	if (priv->chip->vol_reg_size == 2) {
761 		/*
762 		 * The master volume defaults to 0x3ff (mute), but we ignore
763 		 * (zero) the LSB because the hardware step size is 0.125 dB
764 		 * and TLV_DB_SCALE_ITEM has a resolution of 0.01 dB.
765 		 */
766 		ret = regmap_update_bits(priv->regmap, TAS571X_MVOL_REG, 1, 0);
767 		if (ret)
768 			return ret;
769 	}
770 
771 	regcache_cache_only(priv->regmap, true);
772 	gpiod_set_value(priv->pdn_gpio, 1);
773 
774 	return snd_soc_register_codec(&client->dev, &priv->codec_driver,
775 				      &tas571x_dai, 1);
776 }
777 
778 static int tas571x_i2c_remove(struct i2c_client *client)
779 {
780 	struct tas571x_private *priv = i2c_get_clientdata(client);
781 
782 	snd_soc_unregister_codec(&client->dev);
783 	regulator_bulk_disable(priv->chip->num_supply_names, priv->supplies);
784 
785 	return 0;
786 }
787 
788 static const struct of_device_id tas571x_of_match[] = {
789 	{ .compatible = "ti,tas5711", .data = &tas5711_chip, },
790 	{ .compatible = "ti,tas5717", .data = &tas5717_chip, },
791 	{ .compatible = "ti,tas5719", .data = &tas5717_chip, },
792 	{ .compatible = "ti,tas5721", .data = &tas5721_chip, },
793 	{ }
794 };
795 MODULE_DEVICE_TABLE(of, tas571x_of_match);
796 
797 static const struct i2c_device_id tas571x_i2c_id[] = {
798 	{ "tas5711", (kernel_ulong_t) &tas5711_chip },
799 	{ "tas5717", (kernel_ulong_t) &tas5717_chip },
800 	{ "tas5719", (kernel_ulong_t) &tas5717_chip },
801 	{ "tas5721", (kernel_ulong_t) &tas5721_chip },
802 	{ }
803 };
804 MODULE_DEVICE_TABLE(i2c, tas571x_i2c_id);
805 
806 static struct i2c_driver tas571x_i2c_driver = {
807 	.driver = {
808 		.name = "tas571x",
809 		.of_match_table = of_match_ptr(tas571x_of_match),
810 	},
811 	.probe = tas571x_i2c_probe,
812 	.remove = tas571x_i2c_remove,
813 	.id_table = tas571x_i2c_id,
814 };
815 module_i2c_driver(tas571x_i2c_driver);
816 
817 MODULE_DESCRIPTION("ASoC TAS571x driver");
818 MODULE_AUTHOR("Kevin Cernekee <cernekee@chromium.org>");
819 MODULE_LICENSE("GPL");
820