xref: /openbmc/linux/sound/soc/codecs/tas2552.c (revision da097dcc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tas2552.c - ALSA SoC Texas Instruments TAS2552 Mono Audio Amplifier
4  *
5  * Copyright (C) 2014 - 2024 Texas Instruments Incorporated -
6  *	https://www.ti.com
7  *
8  * Author: Dan Murphy <dmurphy@ti.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/device.h>
14 #include <linux/i2c.h>
15 #include <linux/gpio.h>
16 #include <linux/of_gpio.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 
21 #include <linux/gpio/consumer.h>
22 #include <linux/regulator/consumer.h>
23 
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/tlv.h>
29 #include <sound/tas2552-plat.h>
30 #include <dt-bindings/sound/tas2552.h>
31 
32 #include "tas2552.h"
33 
34 static const struct reg_default tas2552_reg_defs[] = {
35 	{TAS2552_CFG_1, 0x22},
36 	{TAS2552_CFG_3, 0x80},
37 	{TAS2552_DOUT, 0x00},
38 	{TAS2552_OUTPUT_DATA, 0xc0},
39 	{TAS2552_PDM_CFG, 0x01},
40 	{TAS2552_PGA_GAIN, 0x00},
41 	{TAS2552_BOOST_APT_CTRL, 0x0f},
42 	{TAS2552_RESERVED_0D, 0xbe},
43 	{TAS2552_LIMIT_RATE_HYS, 0x08},
44 	{TAS2552_CFG_2, 0xef},
45 	{TAS2552_SER_CTRL_1, 0x00},
46 	{TAS2552_SER_CTRL_2, 0x00},
47 	{TAS2552_PLL_CTRL_1, 0x10},
48 	{TAS2552_PLL_CTRL_2, 0x00},
49 	{TAS2552_PLL_CTRL_3, 0x00},
50 	{TAS2552_BTIP, 0x8f},
51 	{TAS2552_BTS_CTRL, 0x80},
52 	{TAS2552_LIMIT_RELEASE, 0x04},
53 	{TAS2552_LIMIT_INT_COUNT, 0x00},
54 	{TAS2552_EDGE_RATE_CTRL, 0x40},
55 	{TAS2552_VBAT_DATA, 0x00},
56 };
57 
58 #define TAS2552_NUM_SUPPLIES	3
59 static const char *tas2552_supply_names[TAS2552_NUM_SUPPLIES] = {
60 	"vbat",		/* vbat voltage */
61 	"iovdd",	/* I/O Voltage */
62 	"avdd",		/* Analog DAC Voltage */
63 };
64 
65 struct tas2552_data {
66 	struct snd_soc_component *component;
67 	struct regmap *regmap;
68 	struct i2c_client *tas2552_client;
69 	struct regulator_bulk_data supplies[TAS2552_NUM_SUPPLIES];
70 	struct gpio_desc *enable_gpio;
71 	unsigned char regs[TAS2552_VBAT_DATA];
72 	unsigned int pll_clkin;
73 	int pll_clk_id;
74 	unsigned int pdm_clk;
75 	int pdm_clk_id;
76 
77 	unsigned int dai_fmt;
78 	unsigned int tdm_delay;
79 };
80 
81 static int tas2552_post_event(struct snd_soc_dapm_widget *w,
82 			      struct snd_kcontrol *kcontrol, int event)
83 {
84 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
85 
86 	switch (event) {
87 	case SND_SOC_DAPM_POST_PMU:
88 		snd_soc_component_write(component, TAS2552_RESERVED_0D, 0xc0);
89 		snd_soc_component_update_bits(component, TAS2552_LIMIT_RATE_HYS, (1 << 5),
90 				    (1 << 5));
91 		snd_soc_component_update_bits(component, TAS2552_CFG_2, 1, 0);
92 		snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_SWS, 0);
93 		break;
94 	case SND_SOC_DAPM_POST_PMD:
95 		snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_SWS,
96 				    TAS2552_SWS);
97 		snd_soc_component_update_bits(component, TAS2552_CFG_2, 1, 1);
98 		snd_soc_component_update_bits(component, TAS2552_LIMIT_RATE_HYS, (1 << 5), 0);
99 		snd_soc_component_write(component, TAS2552_RESERVED_0D, 0xbe);
100 		break;
101 	}
102 	return 0;
103 }
104 
105 /* Input mux controls */
106 static const char * const tas2552_input_texts[] = {
107 	"Digital", "Analog" };
108 static SOC_ENUM_SINGLE_DECL(tas2552_input_mux_enum, TAS2552_CFG_3, 7,
109 			    tas2552_input_texts);
110 
111 static const struct snd_kcontrol_new tas2552_input_mux_control =
112 	SOC_DAPM_ENUM("Route", tas2552_input_mux_enum);
113 
114 static const struct snd_soc_dapm_widget tas2552_dapm_widgets[] =
115 {
116 	SND_SOC_DAPM_INPUT("IN"),
117 
118 	/* MUX Controls */
119 	SND_SOC_DAPM_MUX("Input selection", SND_SOC_NOPM, 0, 0,
120 			 &tas2552_input_mux_control),
121 
122 	SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
123 	SND_SOC_DAPM_AIF_OUT("ASI OUT", "DAC Capture", 0, SND_SOC_NOPM, 0, 0),
124 	SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
125 	SND_SOC_DAPM_OUT_DRV("ClassD", TAS2552_CFG_2, 7, 0, NULL, 0),
126 	SND_SOC_DAPM_SUPPLY("PLL", TAS2552_CFG_2, 3, 0, NULL, 0),
127 	SND_SOC_DAPM_POST("Post Event", tas2552_post_event),
128 
129 	SND_SOC_DAPM_OUTPUT("OUT"),
130 	SND_SOC_DAPM_INPUT("DMIC")
131 };
132 
133 static const struct snd_soc_dapm_route tas2552_audio_map[] = {
134 	{"DAC", NULL, "DAC IN"},
135 	{"Input selection", "Digital", "DAC"},
136 	{"Input selection", "Analog", "IN"},
137 	{"ClassD", NULL, "Input selection"},
138 	{"OUT", NULL, "ClassD"},
139 	{"ClassD", NULL, "PLL"},
140 	{"ASI OUT", NULL, "DMIC"}
141 };
142 
143 #ifdef CONFIG_PM
144 static void tas2552_sw_shutdown(struct tas2552_data *tas2552, int sw_shutdown)
145 {
146 	u8 cfg1_reg = 0;
147 
148 	if (!tas2552->component)
149 		return;
150 
151 	if (sw_shutdown)
152 		cfg1_reg = TAS2552_SWS;
153 
154 	snd_soc_component_update_bits(tas2552->component, TAS2552_CFG_1, TAS2552_SWS,
155 			    cfg1_reg);
156 }
157 #endif
158 
159 static int tas2552_setup_pll(struct snd_soc_component *component,
160 			     struct snd_pcm_hw_params *params)
161 {
162 	struct tas2552_data *tas2552 = dev_get_drvdata(component->dev);
163 	bool bypass_pll = false;
164 	unsigned int pll_clk = params_rate(params) * 512;
165 	unsigned int pll_clkin = tas2552->pll_clkin;
166 	u8 pll_enable;
167 
168 	if (!pll_clkin) {
169 		if (tas2552->pll_clk_id != TAS2552_PLL_CLKIN_BCLK)
170 			return -EINVAL;
171 
172 		pll_clkin = snd_soc_params_to_bclk(params);
173 		pll_clkin += tas2552->tdm_delay;
174 	}
175 
176 	pll_enable = snd_soc_component_read(component, TAS2552_CFG_2) & TAS2552_PLL_ENABLE;
177 	snd_soc_component_update_bits(component, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 0);
178 
179 	if (pll_clkin == pll_clk)
180 		bypass_pll = true;
181 
182 	if (bypass_pll) {
183 		/* By pass the PLL configuration */
184 		snd_soc_component_update_bits(component, TAS2552_PLL_CTRL_2,
185 				    TAS2552_PLL_BYPASS, TAS2552_PLL_BYPASS);
186 	} else {
187 		/* Fill in the PLL control registers for J & D
188 		 * pll_clk = (.5 * pll_clkin * J.D) / 2^p
189 		 * Need to fill in J and D here based on incoming freq
190 		 */
191 		unsigned int d, q, t;
192 		u8 j;
193 		u8 pll_sel = (tas2552->pll_clk_id << 3) & TAS2552_PLL_SRC_MASK;
194 		u8 p = snd_soc_component_read(component, TAS2552_PLL_CTRL_1);
195 
196 		p = (p >> 7);
197 
198 recalc:
199 		t = (pll_clk * 2) << p;
200 		j = t / pll_clkin;
201 		d = t % pll_clkin;
202 		t = pll_clkin / 10000;
203 		q = d / (t + 1);
204 		d = q + ((9999 - pll_clkin % 10000) * (d / t - q)) / 10000;
205 
206 		if (d && (pll_clkin < 512000 || pll_clkin > 9200000)) {
207 			if (tas2552->pll_clk_id == TAS2552_PLL_CLKIN_BCLK) {
208 				pll_clkin = 1800000;
209 				pll_sel = (TAS2552_PLL_CLKIN_1_8_FIXED << 3) &
210 							TAS2552_PLL_SRC_MASK;
211 			} else {
212 				pll_clkin = snd_soc_params_to_bclk(params);
213 				pll_clkin += tas2552->tdm_delay;
214 				pll_sel = (TAS2552_PLL_CLKIN_BCLK << 3) &
215 							TAS2552_PLL_SRC_MASK;
216 			}
217 			goto recalc;
218 		}
219 
220 		snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_PLL_SRC_MASK,
221 				    pll_sel);
222 
223 		snd_soc_component_update_bits(component, TAS2552_PLL_CTRL_1,
224 				    TAS2552_PLL_J_MASK, j);
225 		/* Will clear the PLL_BYPASS bit */
226 		snd_soc_component_write(component, TAS2552_PLL_CTRL_2,
227 			      TAS2552_PLL_D_UPPER(d));
228 		snd_soc_component_write(component, TAS2552_PLL_CTRL_3,
229 			      TAS2552_PLL_D_LOWER(d));
230 	}
231 
232 	/* Restore PLL status */
233 	snd_soc_component_update_bits(component, TAS2552_CFG_2, TAS2552_PLL_ENABLE,
234 			    pll_enable);
235 
236 	return 0;
237 }
238 
239 static int tas2552_hw_params(struct snd_pcm_substream *substream,
240 			     struct snd_pcm_hw_params *params,
241 			     struct snd_soc_dai *dai)
242 {
243 	struct snd_soc_component *component = dai->component;
244 	struct tas2552_data *tas2552 = dev_get_drvdata(component->dev);
245 	int cpf;
246 	u8 ser_ctrl1_reg, wclk_rate;
247 
248 	switch (params_width(params)) {
249 	case 16:
250 		ser_ctrl1_reg = TAS2552_WORDLENGTH_16BIT;
251 		cpf = 32 + tas2552->tdm_delay;
252 		break;
253 	case 20:
254 		ser_ctrl1_reg = TAS2552_WORDLENGTH_20BIT;
255 		cpf = 64 + tas2552->tdm_delay;
256 		break;
257 	case 24:
258 		ser_ctrl1_reg = TAS2552_WORDLENGTH_24BIT;
259 		cpf = 64 + tas2552->tdm_delay;
260 		break;
261 	case 32:
262 		ser_ctrl1_reg = TAS2552_WORDLENGTH_32BIT;
263 		cpf = 64 + tas2552->tdm_delay;
264 		break;
265 	default:
266 		dev_err(component->dev, "Not supported sample size: %d\n",
267 			params_width(params));
268 		return -EINVAL;
269 	}
270 
271 	if (cpf <= 32)
272 		ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_32;
273 	else if (cpf <= 64)
274 		ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_64;
275 	else if (cpf <= 128)
276 		ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_128;
277 	else
278 		ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_256;
279 
280 	snd_soc_component_update_bits(component, TAS2552_SER_CTRL_1,
281 			    TAS2552_WORDLENGTH_MASK | TAS2552_CLKSPERFRAME_MASK,
282 			    ser_ctrl1_reg);
283 
284 	switch (params_rate(params)) {
285 	case 8000:
286 		wclk_rate = TAS2552_WCLK_FREQ_8KHZ;
287 		break;
288 	case 11025:
289 	case 12000:
290 		wclk_rate = TAS2552_WCLK_FREQ_11_12KHZ;
291 		break;
292 	case 16000:
293 		wclk_rate = TAS2552_WCLK_FREQ_16KHZ;
294 		break;
295 	case 22050:
296 	case 24000:
297 		wclk_rate = TAS2552_WCLK_FREQ_22_24KHZ;
298 		break;
299 	case 32000:
300 		wclk_rate = TAS2552_WCLK_FREQ_32KHZ;
301 		break;
302 	case 44100:
303 	case 48000:
304 		wclk_rate = TAS2552_WCLK_FREQ_44_48KHZ;
305 		break;
306 	case 88200:
307 	case 96000:
308 		wclk_rate = TAS2552_WCLK_FREQ_88_96KHZ;
309 		break;
310 	case 176400:
311 	case 192000:
312 		wclk_rate = TAS2552_WCLK_FREQ_176_192KHZ;
313 		break;
314 	default:
315 		dev_err(component->dev, "Not supported sample rate: %d\n",
316 			params_rate(params));
317 		return -EINVAL;
318 	}
319 
320 	snd_soc_component_update_bits(component, TAS2552_CFG_3, TAS2552_WCLK_FREQ_MASK,
321 			    wclk_rate);
322 
323 	return tas2552_setup_pll(component, params);
324 }
325 
326 #define TAS2552_DAI_FMT_MASK	(TAS2552_BCLKDIR | \
327 				 TAS2552_WCLKDIR | \
328 				 TAS2552_DATAFORMAT_MASK)
329 static int tas2552_prepare(struct snd_pcm_substream *substream,
330 			   struct snd_soc_dai *dai)
331 {
332 	struct snd_soc_component *component = dai->component;
333 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
334 	int delay = 0;
335 
336 	/* TDM slot selection only valid in DSP_A/_B mode */
337 	if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_A)
338 		delay += (tas2552->tdm_delay + 1);
339 	else if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_B)
340 		delay += tas2552->tdm_delay;
341 
342 	/* Configure data delay */
343 	snd_soc_component_write(component, TAS2552_SER_CTRL_2, delay);
344 
345 	return 0;
346 }
347 
348 static int tas2552_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
349 {
350 	struct snd_soc_component *component = dai->component;
351 	struct tas2552_data *tas2552 = dev_get_drvdata(component->dev);
352 	u8 serial_format;
353 
354 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
355 	case SND_SOC_DAIFMT_CBC_CFC:
356 		serial_format = 0x00;
357 		break;
358 	case SND_SOC_DAIFMT_CBC_CFP:
359 		serial_format = TAS2552_WCLKDIR;
360 		break;
361 	case SND_SOC_DAIFMT_CBP_CFC:
362 		serial_format = TAS2552_BCLKDIR;
363 		break;
364 	case SND_SOC_DAIFMT_CBP_CFP:
365 		serial_format = (TAS2552_BCLKDIR | TAS2552_WCLKDIR);
366 		break;
367 	default:
368 		dev_vdbg(component->dev, "DAI Format master is not found\n");
369 		return -EINVAL;
370 	}
371 
372 	switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
373 		       SND_SOC_DAIFMT_INV_MASK)) {
374 	case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
375 		break;
376 	case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
377 	case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
378 		serial_format |= TAS2552_DATAFORMAT_DSP;
379 		break;
380 	case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
381 		serial_format |= TAS2552_DATAFORMAT_RIGHT_J;
382 		break;
383 	case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
384 		serial_format |= TAS2552_DATAFORMAT_LEFT_J;
385 		break;
386 	default:
387 		dev_vdbg(component->dev, "DAI Format is not found\n");
388 		return -EINVAL;
389 	}
390 	tas2552->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
391 
392 	snd_soc_component_update_bits(component, TAS2552_SER_CTRL_1, TAS2552_DAI_FMT_MASK,
393 			    serial_format);
394 	return 0;
395 }
396 
397 static int tas2552_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
398 				  unsigned int freq, int dir)
399 {
400 	struct snd_soc_component *component = dai->component;
401 	struct tas2552_data *tas2552 = dev_get_drvdata(component->dev);
402 	u8 reg, mask, val;
403 
404 	switch (clk_id) {
405 	case TAS2552_PLL_CLKIN_MCLK:
406 	case TAS2552_PLL_CLKIN_IVCLKIN:
407 		if (freq < 512000 || freq > 24576000) {
408 			/* out of range PLL_CLKIN, fall back to use BCLK */
409 			dev_warn(component->dev, "Out of range PLL_CLKIN: %u\n",
410 				 freq);
411 			clk_id = TAS2552_PLL_CLKIN_BCLK;
412 			freq = 0;
413 		}
414 		fallthrough;
415 	case TAS2552_PLL_CLKIN_BCLK:
416 	case TAS2552_PLL_CLKIN_1_8_FIXED:
417 		mask = TAS2552_PLL_SRC_MASK;
418 		val = (clk_id << 3) & mask; /* bit 4:5 in the register */
419 		reg = TAS2552_CFG_1;
420 		tas2552->pll_clk_id = clk_id;
421 		tas2552->pll_clkin = freq;
422 		break;
423 	case TAS2552_PDM_CLK_PLL:
424 	case TAS2552_PDM_CLK_IVCLKIN:
425 	case TAS2552_PDM_CLK_BCLK:
426 	case TAS2552_PDM_CLK_MCLK:
427 		mask = TAS2552_PDM_CLK_SEL_MASK;
428 		val = (clk_id >> 1) & mask; /* bit 0:1 in the register */
429 		reg = TAS2552_PDM_CFG;
430 		tas2552->pdm_clk_id = clk_id;
431 		tas2552->pdm_clk = freq;
432 		break;
433 	default:
434 		dev_err(component->dev, "Invalid clk id: %d\n", clk_id);
435 		return -EINVAL;
436 	}
437 
438 	snd_soc_component_update_bits(component, reg, mask, val);
439 
440 	return 0;
441 }
442 
443 static int tas2552_set_dai_tdm_slot(struct snd_soc_dai *dai,
444 				    unsigned int tx_mask, unsigned int rx_mask,
445 				    int slots, int slot_width)
446 {
447 	struct snd_soc_component *component = dai->component;
448 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
449 	unsigned int lsb;
450 
451 	if (unlikely(!tx_mask)) {
452 		dev_err(component->dev, "tx masks need to be non 0\n");
453 		return -EINVAL;
454 	}
455 
456 	/* TDM based on DSP mode requires slots to be adjacent */
457 	lsb = __ffs(tx_mask);
458 	if ((lsb + 1) != __fls(tx_mask)) {
459 		dev_err(component->dev, "Invalid mask, slots must be adjacent\n");
460 		return -EINVAL;
461 	}
462 
463 	tas2552->tdm_delay = lsb * slot_width;
464 
465 	/* DOUT in high-impedance on inactive bit clocks */
466 	snd_soc_component_update_bits(component, TAS2552_DOUT,
467 			    TAS2552_SDOUT_TRISTATE, TAS2552_SDOUT_TRISTATE);
468 
469 	return 0;
470 }
471 
472 static int tas2552_mute(struct snd_soc_dai *dai, int mute, int direction)
473 {
474 	u8 cfg1_reg = 0;
475 	struct snd_soc_component *component = dai->component;
476 
477 	if (mute)
478 		cfg1_reg |= TAS2552_MUTE;
479 
480 	snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_MUTE, cfg1_reg);
481 
482 	return 0;
483 }
484 
485 #ifdef CONFIG_PM
486 static int tas2552_runtime_suspend(struct device *dev)
487 {
488 	struct tas2552_data *tas2552 = dev_get_drvdata(dev);
489 
490 	tas2552_sw_shutdown(tas2552, 1);
491 
492 	regcache_cache_only(tas2552->regmap, true);
493 	regcache_mark_dirty(tas2552->regmap);
494 
495 	gpiod_set_value(tas2552->enable_gpio, 0);
496 
497 	return 0;
498 }
499 
500 static int tas2552_runtime_resume(struct device *dev)
501 {
502 	struct tas2552_data *tas2552 = dev_get_drvdata(dev);
503 
504 	gpiod_set_value(tas2552->enable_gpio, 1);
505 
506 	tas2552_sw_shutdown(tas2552, 0);
507 
508 	regcache_cache_only(tas2552->regmap, false);
509 	regcache_sync(tas2552->regmap);
510 
511 	return 0;
512 }
513 #endif
514 
515 static const struct dev_pm_ops tas2552_pm = {
516 	SET_RUNTIME_PM_OPS(tas2552_runtime_suspend, tas2552_runtime_resume,
517 			   NULL)
518 };
519 
520 static const struct snd_soc_dai_ops tas2552_speaker_dai_ops = {
521 	.hw_params	= tas2552_hw_params,
522 	.prepare	= tas2552_prepare,
523 	.set_sysclk	= tas2552_set_dai_sysclk,
524 	.set_fmt	= tas2552_set_dai_fmt,
525 	.set_tdm_slot	= tas2552_set_dai_tdm_slot,
526 	.mute_stream	= tas2552_mute,
527 	.no_capture_mute = 1,
528 };
529 
530 /* Formats supported by TAS2552 driver. */
531 #define TAS2552_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
532 			 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
533 
534 /* TAS2552 dai structure. */
535 static struct snd_soc_dai_driver tas2552_dai[] = {
536 	{
537 		.name = "tas2552-amplifier",
538 		.playback = {
539 			.stream_name = "Playback",
540 			.channels_min = 2,
541 			.channels_max = 2,
542 			.rates = SNDRV_PCM_RATE_8000_192000,
543 			.formats = TAS2552_FORMATS,
544 		},
545 		.capture = {
546 			.stream_name = "Capture",
547 			.channels_min = 2,
548 			.channels_max = 2,
549 			.rates = SNDRV_PCM_RATE_8000_192000,
550 			.formats = TAS2552_FORMATS,
551 		},
552 		.ops = &tas2552_speaker_dai_ops,
553 	},
554 };
555 
556 /*
557  * DAC digital volumes. From -7 to 24 dB in 1 dB steps
558  */
559 static DECLARE_TLV_DB_SCALE(dac_tlv, -700, 100, 0);
560 
561 static const char * const tas2552_din_source_select[] = {
562 	"Muted",
563 	"Left",
564 	"Right",
565 	"Left + Right average",
566 };
567 static SOC_ENUM_SINGLE_DECL(tas2552_din_source_enum,
568 			    TAS2552_CFG_3, 3,
569 			    tas2552_din_source_select);
570 
571 static const struct snd_kcontrol_new tas2552_snd_controls[] = {
572 	SOC_SINGLE_TLV("Speaker Driver Playback Volume",
573 			 TAS2552_PGA_GAIN, 0, 0x1f, 0, dac_tlv),
574 	SOC_ENUM("DIN source", tas2552_din_source_enum),
575 };
576 
577 static int tas2552_component_probe(struct snd_soc_component *component)
578 {
579 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
580 	int ret;
581 
582 	tas2552->component = component;
583 
584 	ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
585 				    tas2552->supplies);
586 
587 	if (ret != 0) {
588 		dev_err(component->dev, "Failed to enable supplies: %d\n",
589 			ret);
590 		return ret;
591 	}
592 
593 	gpiod_set_value(tas2552->enable_gpio, 1);
594 
595 	ret = pm_runtime_resume_and_get(component->dev);
596 	if (ret < 0) {
597 		dev_err(component->dev, "Enabling device failed: %d\n",
598 			ret);
599 		goto probe_fail;
600 	}
601 
602 	snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_MUTE, TAS2552_MUTE);
603 	snd_soc_component_write(component, TAS2552_CFG_3, TAS2552_I2S_OUT_SEL |
604 					    TAS2552_DIN_SRC_SEL_AVG_L_R);
605 	snd_soc_component_write(component, TAS2552_OUTPUT_DATA,
606 		      TAS2552_PDM_DATA_SEL_V_I |
607 		      TAS2552_R_DATA_OUT(TAS2552_DATA_OUT_V_DATA));
608 	snd_soc_component_write(component, TAS2552_BOOST_APT_CTRL, TAS2552_APT_DELAY_200 |
609 						     TAS2552_APT_THRESH_20_17);
610 
611 	snd_soc_component_write(component, TAS2552_CFG_2, TAS2552_BOOST_EN | TAS2552_APT_EN |
612 					    TAS2552_LIM_EN);
613 
614 	return 0;
615 
616 probe_fail:
617 	pm_runtime_put_noidle(component->dev);
618 	gpiod_set_value(tas2552->enable_gpio, 0);
619 
620 	regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
621 					tas2552->supplies);
622 	return ret;
623 }
624 
625 static void tas2552_component_remove(struct snd_soc_component *component)
626 {
627 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
628 
629 	pm_runtime_put(component->dev);
630 
631 	gpiod_set_value(tas2552->enable_gpio, 0);
632 };
633 
634 #ifdef CONFIG_PM
635 static int tas2552_suspend(struct snd_soc_component *component)
636 {
637 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
638 	int ret;
639 
640 	ret = regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
641 					tas2552->supplies);
642 
643 	if (ret != 0)
644 		dev_err(component->dev, "Failed to disable supplies: %d\n",
645 			ret);
646 	return ret;
647 }
648 
649 static int tas2552_resume(struct snd_soc_component *component)
650 {
651 	struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component);
652 	int ret;
653 
654 	ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
655 				    tas2552->supplies);
656 
657 	if (ret != 0) {
658 		dev_err(component->dev, "Failed to enable supplies: %d\n",
659 			ret);
660 	}
661 
662 	return ret;
663 }
664 #else
665 #define tas2552_suspend NULL
666 #define tas2552_resume NULL
667 #endif
668 
669 static const struct snd_soc_component_driver soc_component_dev_tas2552 = {
670 	.probe			= tas2552_component_probe,
671 	.remove			= tas2552_component_remove,
672 	.suspend		= tas2552_suspend,
673 	.resume			= tas2552_resume,
674 	.controls		= tas2552_snd_controls,
675 	.num_controls		= ARRAY_SIZE(tas2552_snd_controls),
676 	.dapm_widgets		= tas2552_dapm_widgets,
677 	.num_dapm_widgets	= ARRAY_SIZE(tas2552_dapm_widgets),
678 	.dapm_routes		= tas2552_audio_map,
679 	.num_dapm_routes	= ARRAY_SIZE(tas2552_audio_map),
680 	.idle_bias_on		= 1,
681 	.endianness		= 1,
682 };
683 
684 static const struct regmap_config tas2552_regmap_config = {
685 	.reg_bits = 8,
686 	.val_bits = 8,
687 
688 	.max_register = TAS2552_MAX_REG,
689 	.reg_defaults = tas2552_reg_defs,
690 	.num_reg_defaults = ARRAY_SIZE(tas2552_reg_defs),
691 	.cache_type = REGCACHE_RBTREE,
692 };
693 
694 static int tas2552_probe(struct i2c_client *client)
695 {
696 	struct device *dev;
697 	struct tas2552_data *data;
698 	int ret;
699 	int i;
700 
701 	dev = &client->dev;
702 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
703 	if (data == NULL)
704 		return -ENOMEM;
705 
706 	data->enable_gpio = devm_gpiod_get_optional(dev, "enable",
707 						    GPIOD_OUT_LOW);
708 	if (IS_ERR(data->enable_gpio))
709 		return PTR_ERR(data->enable_gpio);
710 
711 	data->tas2552_client = client;
712 	data->regmap = devm_regmap_init_i2c(client, &tas2552_regmap_config);
713 	if (IS_ERR(data->regmap)) {
714 		ret = PTR_ERR(data->regmap);
715 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
716 			ret);
717 		return ret;
718 	}
719 
720 	for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
721 		data->supplies[i].supply = tas2552_supply_names[i];
722 
723 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
724 				      data->supplies);
725 	if (ret != 0) {
726 		dev_err(dev, "Failed to request supplies: %d\n", ret);
727 		return ret;
728 	}
729 
730 	pm_runtime_set_active(&client->dev);
731 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
732 	pm_runtime_use_autosuspend(&client->dev);
733 	pm_runtime_enable(&client->dev);
734 	pm_runtime_mark_last_busy(&client->dev);
735 	pm_runtime_put_sync_autosuspend(&client->dev);
736 
737 	dev_set_drvdata(&client->dev, data);
738 
739 	ret = devm_snd_soc_register_component(&client->dev,
740 				      &soc_component_dev_tas2552,
741 				      tas2552_dai, ARRAY_SIZE(tas2552_dai));
742 	if (ret < 0) {
743 		dev_err(&client->dev, "Failed to register component: %d\n", ret);
744 		pm_runtime_get_noresume(&client->dev);
745 	}
746 
747 	return ret;
748 }
749 
750 static void tas2552_i2c_remove(struct i2c_client *client)
751 {
752 	pm_runtime_disable(&client->dev);
753 }
754 
755 static const struct i2c_device_id tas2552_id[] = {
756 	{ "tas2552", 0 },
757 	{ }
758 };
759 MODULE_DEVICE_TABLE(i2c, tas2552_id);
760 
761 #if IS_ENABLED(CONFIG_OF)
762 static const struct of_device_id tas2552_of_match[] = {
763 	{ .compatible = "ti,tas2552", },
764 	{},
765 };
766 MODULE_DEVICE_TABLE(of, tas2552_of_match);
767 #endif
768 
769 static struct i2c_driver tas2552_i2c_driver = {
770 	.driver = {
771 		.name = "tas2552",
772 		.of_match_table = of_match_ptr(tas2552_of_match),
773 		.pm = &tas2552_pm,
774 	},
775 	.probe = tas2552_probe,
776 	.remove = tas2552_i2c_remove,
777 	.id_table = tas2552_id,
778 };
779 
780 module_i2c_driver(tas2552_i2c_driver);
781 
782 MODULE_AUTHOR("Dan Muprhy <dmurphy@ti.com>");
783 MODULE_DESCRIPTION("TAS2552 Audio amplifier driver");
784 MODULE_LICENSE("GPL");
785