2 * tas5720.c - ALSA SoC Texas Instruments TAS5720 Mono Audio Amplifier
4 * Copyright (C)2015-2016 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Andreas Dannenberg <dannenberg@ti.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/device.h>
21 #include <linux/i2c.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/delay.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
36 /* Define how often to check (and clear) the fault status register (in ms) */
37 #define TAS5720_FAULT_CHECK_INTERVAL 200
39 static const char * const tas5720_supply_names
[] = {
40 "dvdd", /* Digital power supply. Connect to 3.3-V supply. */
41 "pvdd", /* Class-D amp and analog power supply (connected). */
44 #define TAS5720_NUM_SUPPLIES ARRAY_SIZE(tas5720_supply_names)
47 struct snd_soc_codec
*codec
;
48 struct regmap
*regmap
;
49 struct i2c_client
*tas5720_client
;
50 struct regulator_bulk_data supplies
[TAS5720_NUM_SUPPLIES
];
51 struct delayed_work fault_check_work
;
52 unsigned int last_fault
;
55 static int tas5720_hw_params(struct snd_pcm_substream
*substream
,
56 struct snd_pcm_hw_params
*params
,
57 struct snd_soc_dai
*dai
)
59 struct snd_soc_codec
*codec
= dai
->codec
;
60 unsigned int rate
= params_rate(params
);
74 dev_err(codec
->dev
, "unsupported sample rate: %u\n", rate
);
78 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL1_REG
,
79 TAS5720_SSZ_DS
, ssz_ds
);
81 dev_err(codec
->dev
, "error setting sample rate: %d\n", ret
);
88 static int tas5720_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
90 struct snd_soc_codec
*codec
= dai
->codec
;
94 if ((fmt
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
) {
95 dev_vdbg(codec
->dev
, "DAI Format master is not found\n");
99 switch (fmt
& (SND_SOC_DAIFMT_FORMAT_MASK
|
100 SND_SOC_DAIFMT_INV_MASK
)) {
101 case (SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
):
102 /* 1st data bit occur one BCLK cycle after the frame sync */
103 serial_format
= TAS5720_SAIF_I2S
;
105 case (SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_NB_NF
):
107 * Note that although the TAS5720 does not have a dedicated DSP
108 * mode it doesn't care about the LRCLK duty cycle during TDM
109 * operation. Therefore we can use the device's I2S mode with
110 * its delaying of the 1st data bit to receive DSP_A formatted
111 * data. See device datasheet for additional details.
113 serial_format
= TAS5720_SAIF_I2S
;
115 case (SND_SOC_DAIFMT_DSP_B
| SND_SOC_DAIFMT_NB_NF
):
117 * Similar to DSP_A, we can use the fact that the TAS5720 does
118 * not care about the LRCLK duty cycle during TDM to receive
119 * DSP_B formatted data in LEFTJ mode (no delaying of the 1st
122 serial_format
= TAS5720_SAIF_LEFTJ
;
124 case (SND_SOC_DAIFMT_LEFT_J
| SND_SOC_DAIFMT_NB_NF
):
125 /* No delay after the frame sync */
126 serial_format
= TAS5720_SAIF_LEFTJ
;
129 dev_vdbg(codec
->dev
, "DAI Format is not found\n");
133 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL1_REG
,
134 TAS5720_SAIF_FORMAT_MASK
,
137 dev_err(codec
->dev
, "error setting SAIF format: %d\n", ret
);
144 static int tas5720_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
145 unsigned int tx_mask
, unsigned int rx_mask
,
146 int slots
, int slot_width
)
148 struct snd_soc_codec
*codec
= dai
->codec
;
149 unsigned int first_slot
;
153 dev_err(codec
->dev
, "tx masks must not be 0\n");
158 * Determine the first slot that is being requested. We will only
159 * use the first slot that is found since the TAS5720 is a mono
162 first_slot
= __ffs(tx_mask
);
164 if (first_slot
> 7) {
165 dev_err(codec
->dev
, "slot selection out of bounds (%u)\n",
170 /* Enable manual TDM slot selection (instead of I2C ID based) */
171 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL1_REG
,
172 TAS5720_TDM_CFG_SRC
, TAS5720_TDM_CFG_SRC
);
174 goto error_snd_soc_update_bits
;
176 /* Configure the TDM slot to process audio from */
177 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL2_REG
,
178 TAS5720_TDM_SLOT_SEL_MASK
, first_slot
);
180 goto error_snd_soc_update_bits
;
184 error_snd_soc_update_bits
:
185 dev_err(codec
->dev
, "error configuring TDM mode: %d\n", ret
);
189 static int tas5720_mute(struct snd_soc_dai
*dai
, int mute
)
191 struct snd_soc_codec
*codec
= dai
->codec
;
194 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL2_REG
,
195 TAS5720_MUTE
, mute
? TAS5720_MUTE
: 0);
197 dev_err(codec
->dev
, "error (un-)muting device: %d\n", ret
);
204 static void tas5720_fault_check_work(struct work_struct
*work
)
206 struct tas5720_data
*tas5720
= container_of(work
, struct tas5720_data
,
207 fault_check_work
.work
);
208 struct device
*dev
= tas5720
->codec
->dev
;
209 unsigned int curr_fault
;
212 ret
= regmap_read(tas5720
->regmap
, TAS5720_FAULT_REG
, &curr_fault
);
214 dev_err(dev
, "failed to read FAULT register: %d\n", ret
);
218 /* Check/handle all errors except SAIF clock errors */
219 curr_fault
&= TAS5720_OCE
| TAS5720_DCE
| TAS5720_OTE
;
222 * Only flag errors once for a given occurrence. This is needed as
223 * the TAS5720 will take time clearing the fault condition internally
224 * during which we don't want to bombard the system with the same
225 * error message over and over.
227 if ((curr_fault
& TAS5720_OCE
) && !(tas5720
->last_fault
& TAS5720_OCE
))
228 dev_crit(dev
, "experienced an over current hardware fault\n");
230 if ((curr_fault
& TAS5720_DCE
) && !(tas5720
->last_fault
& TAS5720_DCE
))
231 dev_crit(dev
, "experienced a DC detection fault\n");
233 if ((curr_fault
& TAS5720_OTE
) && !(tas5720
->last_fault
& TAS5720_OTE
))
234 dev_crit(dev
, "experienced an over temperature fault\n");
236 /* Store current fault value so we can detect any changes next time */
237 tas5720
->last_fault
= curr_fault
;
243 * Periodically toggle SDZ (shutdown bit) H->L->H to clear any latching
244 * faults as long as a fault condition persists. Always going through
245 * the full sequence no matter the first return value to minimizes
246 * chances for the device to end up in shutdown mode.
248 ret
= regmap_write_bits(tas5720
->regmap
, TAS5720_POWER_CTRL_REG
,
251 dev_err(dev
, "failed to write POWER_CTRL register: %d\n", ret
);
253 ret
= regmap_write_bits(tas5720
->regmap
, TAS5720_POWER_CTRL_REG
,
254 TAS5720_SDZ
, TAS5720_SDZ
);
256 dev_err(dev
, "failed to write POWER_CTRL register: %d\n", ret
);
259 /* Schedule the next fault check at the specified interval */
260 schedule_delayed_work(&tas5720
->fault_check_work
,
261 msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL
));
264 static int tas5720_codec_probe(struct snd_soc_codec
*codec
)
266 struct tas5720_data
*tas5720
= snd_soc_codec_get_drvdata(codec
);
267 unsigned int device_id
;
270 tas5720
->codec
= codec
;
272 ret
= regulator_bulk_enable(ARRAY_SIZE(tas5720
->supplies
),
275 dev_err(codec
->dev
, "failed to enable supplies: %d\n", ret
);
279 ret
= regmap_read(tas5720
->regmap
, TAS5720_DEVICE_ID_REG
, &device_id
);
281 dev_err(codec
->dev
, "failed to read device ID register: %d\n",
286 if (device_id
!= TAS5720_DEVICE_ID
) {
287 dev_err(codec
->dev
, "wrong device ID. expected: %u read: %u\n",
288 TAS5720_DEVICE_ID
, device_id
);
293 /* Set device to mute */
294 ret
= snd_soc_update_bits(codec
, TAS5720_DIGITAL_CTRL2_REG
,
295 TAS5720_MUTE
, TAS5720_MUTE
);
297 goto error_snd_soc_update_bits
;
300 * Enter shutdown mode - our default when not playing audio - to
301 * minimize current consumption. On the TAS5720 there is no real down
302 * side doing so as all device registers are preserved and the wakeup
303 * of the codec is rather quick which we do using a dapm widget.
305 ret
= snd_soc_update_bits(codec
, TAS5720_POWER_CTRL_REG
,
308 goto error_snd_soc_update_bits
;
310 INIT_DELAYED_WORK(&tas5720
->fault_check_work
, tas5720_fault_check_work
);
314 error_snd_soc_update_bits
:
315 dev_err(codec
->dev
, "error configuring device registers: %d\n", ret
);
318 regulator_bulk_disable(ARRAY_SIZE(tas5720
->supplies
),
323 static int tas5720_codec_remove(struct snd_soc_codec
*codec
)
325 struct tas5720_data
*tas5720
= snd_soc_codec_get_drvdata(codec
);
328 cancel_delayed_work_sync(&tas5720
->fault_check_work
);
330 ret
= regulator_bulk_disable(ARRAY_SIZE(tas5720
->supplies
),
333 dev_err(codec
->dev
, "failed to disable supplies: %d\n", ret
);
338 static int tas5720_dac_event(struct snd_soc_dapm_widget
*w
,
339 struct snd_kcontrol
*kcontrol
, int event
)
341 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
342 struct tas5720_data
*tas5720
= snd_soc_codec_get_drvdata(codec
);
345 if (event
& SND_SOC_DAPM_POST_PMU
) {
346 /* Take TAS5720 out of shutdown mode */
347 ret
= snd_soc_update_bits(codec
, TAS5720_POWER_CTRL_REG
,
348 TAS5720_SDZ
, TAS5720_SDZ
);
350 dev_err(codec
->dev
, "error waking codec: %d\n", ret
);
355 * Observe codec shutdown-to-active time. The datasheet only
356 * lists a nominal value however just use-it as-is without
357 * additional padding to minimize the delay introduced in
358 * starting to play audio (actually there is other setup done
359 * by the ASoC framework that will provide additional delays,
360 * so we should always be safe).
364 /* Turn on TAS5720 periodic fault checking/handling */
365 tas5720
->last_fault
= 0;
366 schedule_delayed_work(&tas5720
->fault_check_work
,
367 msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL
));
368 } else if (event
& SND_SOC_DAPM_PRE_PMD
) {
369 /* Disable TAS5720 periodic fault checking/handling */
370 cancel_delayed_work_sync(&tas5720
->fault_check_work
);
372 /* Place TAS5720 in shutdown mode to minimize current draw */
373 ret
= snd_soc_update_bits(codec
, TAS5720_POWER_CTRL_REG
,
376 dev_err(codec
->dev
, "error shutting down codec: %d\n",
386 static int tas5720_suspend(struct snd_soc_codec
*codec
)
388 struct tas5720_data
*tas5720
= snd_soc_codec_get_drvdata(codec
);
391 regcache_cache_only(tas5720
->regmap
, true);
392 regcache_mark_dirty(tas5720
->regmap
);
394 ret
= regulator_bulk_disable(ARRAY_SIZE(tas5720
->supplies
),
397 dev_err(codec
->dev
, "failed to disable supplies: %d\n", ret
);
402 static int tas5720_resume(struct snd_soc_codec
*codec
)
404 struct tas5720_data
*tas5720
= snd_soc_codec_get_drvdata(codec
);
407 ret
= regulator_bulk_enable(ARRAY_SIZE(tas5720
->supplies
),
410 dev_err(codec
->dev
, "failed to enable supplies: %d\n", ret
);
414 regcache_cache_only(tas5720
->regmap
, false);
416 ret
= regcache_sync(tas5720
->regmap
);
418 dev_err(codec
->dev
, "failed to sync regcache: %d\n", ret
);
425 #define tas5720_suspend NULL
426 #define tas5720_resume NULL
429 static bool tas5720_is_volatile_reg(struct device
*dev
, unsigned int reg
)
432 case TAS5720_DEVICE_ID_REG
:
433 case TAS5720_FAULT_REG
:
440 static const struct regmap_config tas5720_regmap_config
= {
444 .max_register
= TAS5720_MAX_REG
,
445 .cache_type
= REGCACHE_RBTREE
,
446 .volatile_reg
= tas5720_is_volatile_reg
,
450 * DAC analog gain. There are four discrete values to select from, ranging
451 * from 19.2 dB to 26.3dB.
453 static const DECLARE_TLV_DB_RANGE(dac_analog_tlv
,
454 0x0, 0x0, TLV_DB_SCALE_ITEM(1920, 0, 0),
455 0x1, 0x1, TLV_DB_SCALE_ITEM(2070, 0, 0),
456 0x2, 0x2, TLV_DB_SCALE_ITEM(2350, 0, 0),
457 0x3, 0x3, TLV_DB_SCALE_ITEM(2630, 0, 0),
461 * DAC digital volumes. From -103.5 to 24 dB in 0.5 dB steps. Note that
462 * setting the gain below -100 dB (register value <0x7) is effectively a MUTE
463 * as per device datasheet.
465 static DECLARE_TLV_DB_SCALE(dac_tlv
, -10350, 50, 0);
467 static const struct snd_kcontrol_new tas5720_snd_controls
[] = {
468 SOC_SINGLE_TLV("Speaker Driver Playback Volume",
469 TAS5720_VOLUME_CTRL_REG
, 0, 0xff, 0, dac_tlv
),
470 SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG
,
471 TAS5720_ANALOG_GAIN_SHIFT
, 3, 0, dac_analog_tlv
),
474 static const struct snd_soc_dapm_widget tas5720_dapm_widgets
[] = {
475 SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM
, 0, 0),
476 SND_SOC_DAPM_DAC_E("DAC", NULL
, SND_SOC_NOPM
, 0, 0, tas5720_dac_event
,
477 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
478 SND_SOC_DAPM_OUTPUT("OUT")
481 static const struct snd_soc_dapm_route tas5720_audio_map
[] = {
482 { "DAC", NULL
, "DAC IN" },
483 { "OUT", NULL
, "DAC" },
486 static const struct snd_soc_codec_driver soc_codec_dev_tas5720
= {
487 .probe
= tas5720_codec_probe
,
488 .remove
= tas5720_codec_remove
,
489 .suspend
= tas5720_suspend
,
490 .resume
= tas5720_resume
,
492 .component_driver
= {
493 .controls
= tas5720_snd_controls
,
494 .num_controls
= ARRAY_SIZE(tas5720_snd_controls
),
495 .dapm_widgets
= tas5720_dapm_widgets
,
496 .num_dapm_widgets
= ARRAY_SIZE(tas5720_dapm_widgets
),
497 .dapm_routes
= tas5720_audio_map
,
498 .num_dapm_routes
= ARRAY_SIZE(tas5720_audio_map
),
502 /* PCM rates supported by the TAS5720 driver */
503 #define TAS5720_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
504 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
506 /* Formats supported by TAS5720 driver */
507 #define TAS5720_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE |\
508 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
510 static const struct snd_soc_dai_ops tas5720_speaker_dai_ops
= {
511 .hw_params
= tas5720_hw_params
,
512 .set_fmt
= tas5720_set_dai_fmt
,
513 .set_tdm_slot
= tas5720_set_dai_tdm_slot
,
514 .digital_mute
= tas5720_mute
,
518 * TAS5720 DAI structure
520 * Note that were are advertising .playback.channels_max = 2 despite this being
521 * a mono amplifier. The reason for that is that some serial ports such as TI's
522 * McASP module have a minimum number of channels (2) that they can output.
523 * Advertising more channels than we have will allow us to interface with such
524 * a serial port without really any negative side effects as the TAS5720 will
525 * simply ignore any extra channel(s) asides from the one channel that is
526 * configured to be played back.
528 static struct snd_soc_dai_driver tas5720_dai
[] = {
530 .name
= "tas5720-amplifier",
532 .stream_name
= "Playback",
535 .rates
= TAS5720_RATES
,
536 .formats
= TAS5720_FORMATS
,
538 .ops
= &tas5720_speaker_dai_ops
,
542 static int tas5720_probe(struct i2c_client
*client
,
543 const struct i2c_device_id
*id
)
545 struct device
*dev
= &client
->dev
;
546 struct tas5720_data
*data
;
550 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
554 data
->tas5720_client
= client
;
555 data
->regmap
= devm_regmap_init_i2c(client
, &tas5720_regmap_config
);
556 if (IS_ERR(data
->regmap
)) {
557 ret
= PTR_ERR(data
->regmap
);
558 dev_err(dev
, "failed to allocate register map: %d\n", ret
);
562 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
563 data
->supplies
[i
].supply
= tas5720_supply_names
[i
];
565 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->supplies
),
568 dev_err(dev
, "failed to request supplies: %d\n", ret
);
572 dev_set_drvdata(dev
, data
);
574 ret
= snd_soc_register_codec(&client
->dev
,
575 &soc_codec_dev_tas5720
,
576 tas5720_dai
, ARRAY_SIZE(tas5720_dai
));
578 dev_err(dev
, "failed to register codec: %d\n", ret
);
585 static int tas5720_remove(struct i2c_client
*client
)
587 struct device
*dev
= &client
->dev
;
589 snd_soc_unregister_codec(dev
);
594 static const struct i2c_device_id tas5720_id
[] = {
598 MODULE_DEVICE_TABLE(i2c
, tas5720_id
);
600 #if IS_ENABLED(CONFIG_OF)
601 static const struct of_device_id tas5720_of_match
[] = {
602 { .compatible
= "ti,tas5720", },
605 MODULE_DEVICE_TABLE(of
, tas5720_of_match
);
608 static struct i2c_driver tas5720_i2c_driver
= {
611 .of_match_table
= of_match_ptr(tas5720_of_match
),
613 .probe
= tas5720_probe
,
614 .remove
= tas5720_remove
,
615 .id_table
= tas5720_id
,
618 module_i2c_driver(tas5720_i2c_driver
);
620 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
621 MODULE_DESCRIPTION("TAS5720 Audio amplifier driver");
622 MODULE_LICENSE("GPL");