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