Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ALSA SoC Texas Instruments TAS6424 Quad-Channel Audio Amplifier
0004  *
0005  * Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
0006  *  Author: Andreas Dannenberg <dannenberg@ti.com>
0007  *  Andrew F. Davis <afd@ti.com>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/errno.h>
0012 #include <linux/device.h>
0013 #include <linux/i2c.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/regmap.h>
0016 #include <linux/slab.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/delay.h>
0019 #include <linux/gpio/consumer.h>
0020 
0021 #include <sound/pcm.h>
0022 #include <sound/pcm_params.h>
0023 #include <sound/soc.h>
0024 #include <sound/soc-dapm.h>
0025 #include <sound/tlv.h>
0026 
0027 #include "tas6424.h"
0028 
0029 /* Define how often to check (and clear) the fault status register (in ms) */
0030 #define TAS6424_FAULT_CHECK_INTERVAL 200
0031 
0032 static const char * const tas6424_supply_names[] = {
0033     "dvdd", /* Digital power supply. Connect to 3.3-V supply. */
0034     "vbat", /* Supply used for higher voltage analog circuits. */
0035     "pvdd", /* Class-D amp output FETs supply. */
0036 };
0037 #define TAS6424_NUM_SUPPLIES ARRAY_SIZE(tas6424_supply_names)
0038 
0039 struct tas6424_data {
0040     struct device *dev;
0041     struct regmap *regmap;
0042     struct regulator_bulk_data supplies[TAS6424_NUM_SUPPLIES];
0043     struct delayed_work fault_check_work;
0044     unsigned int last_cfault;
0045     unsigned int last_fault1;
0046     unsigned int last_fault2;
0047     unsigned int last_warn;
0048     struct gpio_desc *standby_gpio;
0049     struct gpio_desc *mute_gpio;
0050 };
0051 
0052 /*
0053  * DAC digital volumes. From -103.5 to 24 dB in 0.5 dB steps. Note that
0054  * setting the gain below -100 dB (register value <0x7) is effectively a MUTE
0055  * as per device datasheet.
0056  */
0057 static DECLARE_TLV_DB_SCALE(dac_tlv, -10350, 50, 0);
0058 
0059 static const struct snd_kcontrol_new tas6424_snd_controls[] = {
0060     SOC_SINGLE_TLV("Speaker Driver CH1 Playback Volume",
0061                TAS6424_CH1_VOL_CTRL, 0, 0xff, 0, dac_tlv),
0062     SOC_SINGLE_TLV("Speaker Driver CH2 Playback Volume",
0063                TAS6424_CH2_VOL_CTRL, 0, 0xff, 0, dac_tlv),
0064     SOC_SINGLE_TLV("Speaker Driver CH3 Playback Volume",
0065                TAS6424_CH3_VOL_CTRL, 0, 0xff, 0, dac_tlv),
0066     SOC_SINGLE_TLV("Speaker Driver CH4 Playback Volume",
0067                TAS6424_CH4_VOL_CTRL, 0, 0xff, 0, dac_tlv),
0068     SOC_SINGLE_STROBE("Auto Diagnostics Switch", TAS6424_DC_DIAG_CTRL1,
0069               TAS6424_LDGBYPASS_SHIFT, 1),
0070 };
0071 
0072 static int tas6424_dac_event(struct snd_soc_dapm_widget *w,
0073                  struct snd_kcontrol *kcontrol, int event)
0074 {
0075     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0076     struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
0077 
0078     dev_dbg(component->dev, "%s() event=0x%0x\n", __func__, event);
0079 
0080     if (event & SND_SOC_DAPM_POST_PMU) {
0081         /* Observe codec shutdown-to-active time */
0082         msleep(12);
0083 
0084         /* Turn on TAS6424 periodic fault checking/handling */
0085         tas6424->last_fault1 = 0;
0086         tas6424->last_fault2 = 0;
0087         tas6424->last_warn = 0;
0088         schedule_delayed_work(&tas6424->fault_check_work,
0089                       msecs_to_jiffies(TAS6424_FAULT_CHECK_INTERVAL));
0090     } else if (event & SND_SOC_DAPM_PRE_PMD) {
0091         /* Disable TAS6424 periodic fault checking/handling */
0092         cancel_delayed_work_sync(&tas6424->fault_check_work);
0093     }
0094 
0095     return 0;
0096 }
0097 
0098 static const struct snd_soc_dapm_widget tas6424_dapm_widgets[] = {
0099     SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
0100     SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas6424_dac_event,
0101                SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
0102     SND_SOC_DAPM_OUTPUT("OUT")
0103 };
0104 
0105 static const struct snd_soc_dapm_route tas6424_audio_map[] = {
0106     { "DAC", NULL, "DAC IN" },
0107     { "OUT", NULL, "DAC" },
0108 };
0109 
0110 static int tas6424_hw_params(struct snd_pcm_substream *substream,
0111                  struct snd_pcm_hw_params *params,
0112                  struct snd_soc_dai *dai)
0113 {
0114     struct snd_soc_component *component = dai->component;
0115     unsigned int rate = params_rate(params);
0116     unsigned int width = params_width(params);
0117     u8 sap_ctrl = 0;
0118 
0119     dev_dbg(component->dev, "%s() rate=%u width=%u\n", __func__, rate, width);
0120 
0121     switch (rate) {
0122     case 44100:
0123         sap_ctrl |= TAS6424_SAP_RATE_44100;
0124         break;
0125     case 48000:
0126         sap_ctrl |= TAS6424_SAP_RATE_48000;
0127         break;
0128     case 96000:
0129         sap_ctrl |= TAS6424_SAP_RATE_96000;
0130         break;
0131     default:
0132         dev_err(component->dev, "unsupported sample rate: %u\n", rate);
0133         return -EINVAL;
0134     }
0135 
0136     switch (width) {
0137     case 16:
0138         sap_ctrl |= TAS6424_SAP_TDM_SLOT_SZ_16;
0139         break;
0140     case 24:
0141         break;
0142     default:
0143         dev_err(component->dev, "unsupported sample width: %u\n", width);
0144         return -EINVAL;
0145     }
0146 
0147     snd_soc_component_update_bits(component, TAS6424_SAP_CTRL,
0148                 TAS6424_SAP_RATE_MASK |
0149                 TAS6424_SAP_TDM_SLOT_SZ_16,
0150                 sap_ctrl);
0151 
0152     return 0;
0153 }
0154 
0155 static int tas6424_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0156 {
0157     struct snd_soc_component *component = dai->component;
0158     u8 serial_format = 0;
0159 
0160     dev_dbg(component->dev, "%s() fmt=0x%0x\n", __func__, fmt);
0161 
0162     /* clock masters */
0163     switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0164     case SND_SOC_DAIFMT_CBC_CFC:
0165         break;
0166     default:
0167         dev_err(component->dev, "Invalid DAI clocking\n");
0168         return -EINVAL;
0169     }
0170 
0171     /* signal polarity */
0172     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0173     case SND_SOC_DAIFMT_NB_NF:
0174         break;
0175     default:
0176         dev_err(component->dev, "Invalid DAI clock signal polarity\n");
0177         return -EINVAL;
0178     }
0179 
0180     /* interface format */
0181     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0182     case SND_SOC_DAIFMT_I2S:
0183         serial_format |= TAS6424_SAP_I2S;
0184         break;
0185     case SND_SOC_DAIFMT_DSP_A:
0186         serial_format |= TAS6424_SAP_DSP;
0187         break;
0188     case SND_SOC_DAIFMT_DSP_B:
0189         /*
0190          * We can use the fact that the TAS6424 does not care about the
0191          * LRCLK duty cycle during TDM to receive DSP_B formatted data
0192          * in LEFTJ mode (no delaying of the 1st data bit).
0193          */
0194         serial_format |= TAS6424_SAP_LEFTJ;
0195         break;
0196     case SND_SOC_DAIFMT_LEFT_J:
0197         serial_format |= TAS6424_SAP_LEFTJ;
0198         break;
0199     default:
0200         dev_err(component->dev, "Invalid DAI interface format\n");
0201         return -EINVAL;
0202     }
0203 
0204     snd_soc_component_update_bits(component, TAS6424_SAP_CTRL,
0205                 TAS6424_SAP_FMT_MASK, serial_format);
0206 
0207     return 0;
0208 }
0209 
0210 static int tas6424_set_dai_tdm_slot(struct snd_soc_dai *dai,
0211                     unsigned int tx_mask, unsigned int rx_mask,
0212                     int slots, int slot_width)
0213 {
0214     struct snd_soc_component *component = dai->component;
0215     unsigned int first_slot, last_slot;
0216     bool sap_tdm_slot_last;
0217 
0218     dev_dbg(component->dev, "%s() tx_mask=%d rx_mask=%d\n", __func__,
0219         tx_mask, rx_mask);
0220 
0221     if (!tx_mask || !rx_mask)
0222         return 0; /* nothing needed to disable TDM mode */
0223 
0224     /*
0225      * Determine the first slot and last slot that is being requested so
0226      * we'll be able to more easily enforce certain constraints as the
0227      * TAS6424's TDM interface is not fully configurable.
0228      */
0229     first_slot = __ffs(tx_mask);
0230     last_slot = __fls(rx_mask);
0231 
0232     if (last_slot - first_slot != 4) {
0233         dev_err(component->dev, "tdm mask must cover 4 contiguous slots\n");
0234         return -EINVAL;
0235     }
0236 
0237     switch (first_slot) {
0238     case 0:
0239         sap_tdm_slot_last = false;
0240         break;
0241     case 4:
0242         sap_tdm_slot_last = true;
0243         break;
0244     default:
0245         dev_err(component->dev, "tdm mask must start at slot 0 or 4\n");
0246         return -EINVAL;
0247     }
0248 
0249     snd_soc_component_update_bits(component, TAS6424_SAP_CTRL, TAS6424_SAP_TDM_SLOT_LAST,
0250                 sap_tdm_slot_last ? TAS6424_SAP_TDM_SLOT_LAST : 0);
0251 
0252     return 0;
0253 }
0254 
0255 static int tas6424_mute(struct snd_soc_dai *dai, int mute, int direction)
0256 {
0257     struct snd_soc_component *component = dai->component;
0258     struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
0259     unsigned int val;
0260 
0261     dev_dbg(component->dev, "%s() mute=%d\n", __func__, mute);
0262 
0263     if (tas6424->mute_gpio) {
0264         gpiod_set_value_cansleep(tas6424->mute_gpio, mute);
0265         return 0;
0266     }
0267 
0268     if (mute)
0269         val = TAS6424_ALL_STATE_MUTE;
0270     else
0271         val = TAS6424_ALL_STATE_PLAY;
0272 
0273     snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, val);
0274 
0275     return 0;
0276 }
0277 
0278 static int tas6424_power_off(struct snd_soc_component *component)
0279 {
0280     struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
0281     int ret;
0282 
0283     snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, TAS6424_ALL_STATE_HIZ);
0284 
0285     regcache_cache_only(tas6424->regmap, true);
0286     regcache_mark_dirty(tas6424->regmap);
0287 
0288     ret = regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies),
0289                      tas6424->supplies);
0290     if (ret < 0) {
0291         dev_err(component->dev, "failed to disable supplies: %d\n", ret);
0292         return ret;
0293     }
0294 
0295     return 0;
0296 }
0297 
0298 static int tas6424_power_on(struct snd_soc_component *component)
0299 {
0300     struct tas6424_data *tas6424 = snd_soc_component_get_drvdata(component);
0301     int ret;
0302     u8 chan_states;
0303     int no_auto_diags = 0;
0304     unsigned int reg_val;
0305 
0306     if (!regmap_read(tas6424->regmap, TAS6424_DC_DIAG_CTRL1, &reg_val))
0307         no_auto_diags = reg_val & TAS6424_LDGBYPASS_MASK;
0308 
0309     ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
0310                     tas6424->supplies);
0311     if (ret < 0) {
0312         dev_err(component->dev, "failed to enable supplies: %d\n", ret);
0313         return ret;
0314     }
0315 
0316     regcache_cache_only(tas6424->regmap, false);
0317 
0318     ret = regcache_sync(tas6424->regmap);
0319     if (ret < 0) {
0320         dev_err(component->dev, "failed to sync regcache: %d\n", ret);
0321         return ret;
0322     }
0323 
0324     if (tas6424->mute_gpio) {
0325         gpiod_set_value_cansleep(tas6424->mute_gpio, 0);
0326         /*
0327          * channels are muted via the mute pin.  Don't also mute
0328          * them via the registers so that subsequent register
0329          * access is not necessary to un-mute the channels
0330          */
0331         chan_states = TAS6424_ALL_STATE_PLAY;
0332     } else {
0333         chan_states = TAS6424_ALL_STATE_MUTE;
0334     }
0335     snd_soc_component_write(component, TAS6424_CH_STATE_CTRL, chan_states);
0336 
0337     /* any time we come out of HIZ, the output channels automatically run DC
0338      * load diagnostics if autodiagnotics are enabled. wait here until this
0339      * completes.
0340      */
0341     if (!no_auto_diags)
0342         msleep(230);
0343 
0344     return 0;
0345 }
0346 
0347 static int tas6424_set_bias_level(struct snd_soc_component *component,
0348                   enum snd_soc_bias_level level)
0349 {
0350     dev_dbg(component->dev, "%s() level=%d\n", __func__, level);
0351 
0352     switch (level) {
0353     case SND_SOC_BIAS_ON:
0354     case SND_SOC_BIAS_PREPARE:
0355         break;
0356     case SND_SOC_BIAS_STANDBY:
0357         if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
0358             tas6424_power_on(component);
0359         break;
0360     case SND_SOC_BIAS_OFF:
0361         tas6424_power_off(component);
0362         break;
0363     }
0364 
0365     return 0;
0366 }
0367 
0368 static struct snd_soc_component_driver soc_codec_dev_tas6424 = {
0369     .set_bias_level     = tas6424_set_bias_level,
0370     .controls       = tas6424_snd_controls,
0371     .num_controls       = ARRAY_SIZE(tas6424_snd_controls),
0372     .dapm_widgets       = tas6424_dapm_widgets,
0373     .num_dapm_widgets   = ARRAY_SIZE(tas6424_dapm_widgets),
0374     .dapm_routes        = tas6424_audio_map,
0375     .num_dapm_routes    = ARRAY_SIZE(tas6424_audio_map),
0376     .use_pmdown_time    = 1,
0377     .endianness     = 1,
0378 };
0379 
0380 static const struct snd_soc_dai_ops tas6424_speaker_dai_ops = {
0381     .hw_params  = tas6424_hw_params,
0382     .set_fmt    = tas6424_set_dai_fmt,
0383     .set_tdm_slot   = tas6424_set_dai_tdm_slot,
0384     .mute_stream    = tas6424_mute,
0385     .no_capture_mute = 1,
0386 };
0387 
0388 static struct snd_soc_dai_driver tas6424_dai[] = {
0389     {
0390         .name = "tas6424-amplifier",
0391         .playback = {
0392             .stream_name = "Playback",
0393             .channels_min = 1,
0394             .channels_max = 4,
0395             .rates = TAS6424_RATES,
0396             .formats = TAS6424_FORMATS,
0397         },
0398         .ops = &tas6424_speaker_dai_ops,
0399     },
0400 };
0401 
0402 static void tas6424_fault_check_work(struct work_struct *work)
0403 {
0404     struct tas6424_data *tas6424 = container_of(work, struct tas6424_data,
0405                             fault_check_work.work);
0406     struct device *dev = tas6424->dev;
0407     unsigned int reg;
0408     int ret;
0409 
0410     ret = regmap_read(tas6424->regmap, TAS6424_CHANNEL_FAULT, &reg);
0411     if (ret < 0) {
0412         dev_err(dev, "failed to read CHANNEL_FAULT register: %d\n", ret);
0413         goto out;
0414     }
0415 
0416     if (!reg) {
0417         tas6424->last_cfault = reg;
0418         goto check_global_fault1_reg;
0419     }
0420 
0421     /*
0422      * Only flag errors once for a given occurrence. This is needed as
0423      * the TAS6424 will take time clearing the fault condition internally
0424      * during which we don't want to bombard the system with the same
0425      * error message over and over.
0426      */
0427     if ((reg & TAS6424_FAULT_OC_CH1) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH1))
0428         dev_crit(dev, "experienced a channel 1 overcurrent fault\n");
0429 
0430     if ((reg & TAS6424_FAULT_OC_CH2) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH2))
0431         dev_crit(dev, "experienced a channel 2 overcurrent fault\n");
0432 
0433     if ((reg & TAS6424_FAULT_OC_CH3) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH3))
0434         dev_crit(dev, "experienced a channel 3 overcurrent fault\n");
0435 
0436     if ((reg & TAS6424_FAULT_OC_CH4) && !(tas6424->last_cfault & TAS6424_FAULT_OC_CH4))
0437         dev_crit(dev, "experienced a channel 4 overcurrent fault\n");
0438 
0439     if ((reg & TAS6424_FAULT_DC_CH1) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH1))
0440         dev_crit(dev, "experienced a channel 1 DC fault\n");
0441 
0442     if ((reg & TAS6424_FAULT_DC_CH2) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH2))
0443         dev_crit(dev, "experienced a channel 2 DC fault\n");
0444 
0445     if ((reg & TAS6424_FAULT_DC_CH3) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH3))
0446         dev_crit(dev, "experienced a channel 3 DC fault\n");
0447 
0448     if ((reg & TAS6424_FAULT_DC_CH4) && !(tas6424->last_cfault & TAS6424_FAULT_DC_CH4))
0449         dev_crit(dev, "experienced a channel 4 DC fault\n");
0450 
0451     /* Store current fault1 value so we can detect any changes next time */
0452     tas6424->last_cfault = reg;
0453 
0454 check_global_fault1_reg:
0455     ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT1, &reg);
0456     if (ret < 0) {
0457         dev_err(dev, "failed to read GLOB_FAULT1 register: %d\n", ret);
0458         goto out;
0459     }
0460 
0461     /*
0462      * Ignore any clock faults as there is no clean way to check for them.
0463      * We would need to start checking for those faults *after* the SAIF
0464      * stream has been setup, and stop checking *before* the stream is
0465      * stopped to avoid any false-positives. However there are no
0466      * appropriate hooks to monitor these events.
0467      */
0468     reg &= TAS6424_FAULT_PVDD_OV |
0469            TAS6424_FAULT_VBAT_OV |
0470            TAS6424_FAULT_PVDD_UV |
0471            TAS6424_FAULT_VBAT_UV;
0472 
0473     if (!reg) {
0474         tas6424->last_fault1 = reg;
0475         goto check_global_fault2_reg;
0476     }
0477 
0478     if ((reg & TAS6424_FAULT_PVDD_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_OV))
0479         dev_crit(dev, "experienced a PVDD overvoltage fault\n");
0480 
0481     if ((reg & TAS6424_FAULT_VBAT_OV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_OV))
0482         dev_crit(dev, "experienced a VBAT overvoltage fault\n");
0483 
0484     if ((reg & TAS6424_FAULT_PVDD_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_PVDD_UV))
0485         dev_crit(dev, "experienced a PVDD undervoltage fault\n");
0486 
0487     if ((reg & TAS6424_FAULT_VBAT_UV) && !(tas6424->last_fault1 & TAS6424_FAULT_VBAT_UV))
0488         dev_crit(dev, "experienced a VBAT undervoltage fault\n");
0489 
0490     /* Store current fault1 value so we can detect any changes next time */
0491     tas6424->last_fault1 = reg;
0492 
0493 check_global_fault2_reg:
0494     ret = regmap_read(tas6424->regmap, TAS6424_GLOB_FAULT2, &reg);
0495     if (ret < 0) {
0496         dev_err(dev, "failed to read GLOB_FAULT2 register: %d\n", ret);
0497         goto out;
0498     }
0499 
0500     reg &= TAS6424_FAULT_OTSD |
0501            TAS6424_FAULT_OTSD_CH1 |
0502            TAS6424_FAULT_OTSD_CH2 |
0503            TAS6424_FAULT_OTSD_CH3 |
0504            TAS6424_FAULT_OTSD_CH4;
0505 
0506     if (!reg) {
0507         tas6424->last_fault2 = reg;
0508         goto check_warn_reg;
0509     }
0510 
0511     if ((reg & TAS6424_FAULT_OTSD) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD))
0512         dev_crit(dev, "experienced a global overtemp shutdown\n");
0513 
0514     if ((reg & TAS6424_FAULT_OTSD_CH1) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH1))
0515         dev_crit(dev, "experienced an overtemp shutdown on CH1\n");
0516 
0517     if ((reg & TAS6424_FAULT_OTSD_CH2) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH2))
0518         dev_crit(dev, "experienced an overtemp shutdown on CH2\n");
0519 
0520     if ((reg & TAS6424_FAULT_OTSD_CH3) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH3))
0521         dev_crit(dev, "experienced an overtemp shutdown on CH3\n");
0522 
0523     if ((reg & TAS6424_FAULT_OTSD_CH4) && !(tas6424->last_fault2 & TAS6424_FAULT_OTSD_CH4))
0524         dev_crit(dev, "experienced an overtemp shutdown on CH4\n");
0525 
0526     /* Store current fault2 value so we can detect any changes next time */
0527     tas6424->last_fault2 = reg;
0528 
0529 check_warn_reg:
0530     ret = regmap_read(tas6424->regmap, TAS6424_WARN, &reg);
0531     if (ret < 0) {
0532         dev_err(dev, "failed to read WARN register: %d\n", ret);
0533         goto out;
0534     }
0535 
0536     reg &= TAS6424_WARN_VDD_UV |
0537            TAS6424_WARN_VDD_POR |
0538            TAS6424_WARN_VDD_OTW |
0539            TAS6424_WARN_VDD_OTW_CH1 |
0540            TAS6424_WARN_VDD_OTW_CH2 |
0541            TAS6424_WARN_VDD_OTW_CH3 |
0542            TAS6424_WARN_VDD_OTW_CH4;
0543 
0544     if (!reg) {
0545         tas6424->last_warn = reg;
0546         goto out;
0547     }
0548 
0549     if ((reg & TAS6424_WARN_VDD_UV) && !(tas6424->last_warn & TAS6424_WARN_VDD_UV))
0550         dev_warn(dev, "experienced a VDD under voltage condition\n");
0551 
0552     if ((reg & TAS6424_WARN_VDD_POR) && !(tas6424->last_warn & TAS6424_WARN_VDD_POR))
0553         dev_warn(dev, "experienced a VDD POR condition\n");
0554 
0555     if ((reg & TAS6424_WARN_VDD_OTW) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW))
0556         dev_warn(dev, "experienced a global overtemp warning\n");
0557 
0558     if ((reg & TAS6424_WARN_VDD_OTW_CH1) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH1))
0559         dev_warn(dev, "experienced an overtemp warning on CH1\n");
0560 
0561     if ((reg & TAS6424_WARN_VDD_OTW_CH2) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH2))
0562         dev_warn(dev, "experienced an overtemp warning on CH2\n");
0563 
0564     if ((reg & TAS6424_WARN_VDD_OTW_CH3) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH3))
0565         dev_warn(dev, "experienced an overtemp warning on CH3\n");
0566 
0567     if ((reg & TAS6424_WARN_VDD_OTW_CH4) && !(tas6424->last_warn & TAS6424_WARN_VDD_OTW_CH4))
0568         dev_warn(dev, "experienced an overtemp warning on CH4\n");
0569 
0570     /* Store current warn value so we can detect any changes next time */
0571     tas6424->last_warn = reg;
0572 
0573     /* Clear any warnings by toggling the CLEAR_FAULT control bit */
0574     ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
0575                 TAS6424_CLEAR_FAULT, TAS6424_CLEAR_FAULT);
0576     if (ret < 0)
0577         dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
0578 
0579     ret = regmap_write_bits(tas6424->regmap, TAS6424_MISC_CTRL3,
0580                 TAS6424_CLEAR_FAULT, 0);
0581     if (ret < 0)
0582         dev_err(dev, "failed to write MISC_CTRL3 register: %d\n", ret);
0583 
0584 out:
0585     /* Schedule the next fault check at the specified interval */
0586     schedule_delayed_work(&tas6424->fault_check_work,
0587                   msecs_to_jiffies(TAS6424_FAULT_CHECK_INTERVAL));
0588 }
0589 
0590 static const struct reg_default tas6424_reg_defaults[] = {
0591     { TAS6424_MODE_CTRL,        0x00 },
0592     { TAS6424_MISC_CTRL1,       0x32 },
0593     { TAS6424_MISC_CTRL2,       0x62 },
0594     { TAS6424_SAP_CTRL,     0x04 },
0595     { TAS6424_CH_STATE_CTRL,    0x55 },
0596     { TAS6424_CH1_VOL_CTRL,     0xcf },
0597     { TAS6424_CH2_VOL_CTRL,     0xcf },
0598     { TAS6424_CH3_VOL_CTRL,     0xcf },
0599     { TAS6424_CH4_VOL_CTRL,     0xcf },
0600     { TAS6424_DC_DIAG_CTRL1,    0x00 },
0601     { TAS6424_DC_DIAG_CTRL2,    0x11 },
0602     { TAS6424_DC_DIAG_CTRL3,    0x11 },
0603     { TAS6424_PIN_CTRL,     0xff },
0604     { TAS6424_AC_DIAG_CTRL1,    0x00 },
0605     { TAS6424_MISC_CTRL3,       0x00 },
0606     { TAS6424_CLIP_CTRL,        0x01 },
0607     { TAS6424_CLIP_WINDOW,      0x14 },
0608     { TAS6424_CLIP_WARN,        0x00 },
0609     { TAS6424_CBC_STAT,     0x00 },
0610     { TAS6424_MISC_CTRL4,       0x40 },
0611 };
0612 
0613 static bool tas6424_is_writable_reg(struct device *dev, unsigned int reg)
0614 {
0615     switch (reg) {
0616     case TAS6424_MODE_CTRL:
0617     case TAS6424_MISC_CTRL1:
0618     case TAS6424_MISC_CTRL2:
0619     case TAS6424_SAP_CTRL:
0620     case TAS6424_CH_STATE_CTRL:
0621     case TAS6424_CH1_VOL_CTRL:
0622     case TAS6424_CH2_VOL_CTRL:
0623     case TAS6424_CH3_VOL_CTRL:
0624     case TAS6424_CH4_VOL_CTRL:
0625     case TAS6424_DC_DIAG_CTRL1:
0626     case TAS6424_DC_DIAG_CTRL2:
0627     case TAS6424_DC_DIAG_CTRL3:
0628     case TAS6424_PIN_CTRL:
0629     case TAS6424_AC_DIAG_CTRL1:
0630     case TAS6424_MISC_CTRL3:
0631     case TAS6424_CLIP_CTRL:
0632     case TAS6424_CLIP_WINDOW:
0633     case TAS6424_CLIP_WARN:
0634     case TAS6424_CBC_STAT:
0635     case TAS6424_MISC_CTRL4:
0636         return true;
0637     default:
0638         return false;
0639     }
0640 }
0641 
0642 static bool tas6424_is_volatile_reg(struct device *dev, unsigned int reg)
0643 {
0644     switch (reg) {
0645     case TAS6424_DC_LOAD_DIAG_REP12:
0646     case TAS6424_DC_LOAD_DIAG_REP34:
0647     case TAS6424_DC_LOAD_DIAG_REPLO:
0648     case TAS6424_CHANNEL_STATE:
0649     case TAS6424_CHANNEL_FAULT:
0650     case TAS6424_GLOB_FAULT1:
0651     case TAS6424_GLOB_FAULT2:
0652     case TAS6424_WARN:
0653     case TAS6424_AC_LOAD_DIAG_REP1:
0654     case TAS6424_AC_LOAD_DIAG_REP2:
0655     case TAS6424_AC_LOAD_DIAG_REP3:
0656     case TAS6424_AC_LOAD_DIAG_REP4:
0657         return true;
0658     default:
0659         return false;
0660     }
0661 }
0662 
0663 static const struct regmap_config tas6424_regmap_config = {
0664     .reg_bits = 8,
0665     .val_bits = 8,
0666 
0667     .writeable_reg = tas6424_is_writable_reg,
0668     .volatile_reg = tas6424_is_volatile_reg,
0669 
0670     .max_register = TAS6424_MAX,
0671     .reg_defaults = tas6424_reg_defaults,
0672     .num_reg_defaults = ARRAY_SIZE(tas6424_reg_defaults),
0673     .cache_type = REGCACHE_RBTREE,
0674 };
0675 
0676 #if IS_ENABLED(CONFIG_OF)
0677 static const struct of_device_id tas6424_of_ids[] = {
0678     { .compatible = "ti,tas6424", },
0679     { },
0680 };
0681 MODULE_DEVICE_TABLE(of, tas6424_of_ids);
0682 #endif
0683 
0684 static int tas6424_i2c_probe(struct i2c_client *client)
0685 {
0686     struct device *dev = &client->dev;
0687     struct tas6424_data *tas6424;
0688     int ret;
0689     int i;
0690 
0691     tas6424 = devm_kzalloc(dev, sizeof(*tas6424), GFP_KERNEL);
0692     if (!tas6424)
0693         return -ENOMEM;
0694     dev_set_drvdata(dev, tas6424);
0695 
0696     tas6424->dev = dev;
0697 
0698     tas6424->regmap = devm_regmap_init_i2c(client, &tas6424_regmap_config);
0699     if (IS_ERR(tas6424->regmap)) {
0700         ret = PTR_ERR(tas6424->regmap);
0701         dev_err(dev, "unable to allocate register map: %d\n", ret);
0702         return ret;
0703     }
0704 
0705     /*
0706      * Get control of the standby pin and set it LOW to take the codec
0707      * out of the stand-by mode.
0708      * Note: The actual pin polarity is taken care of in the GPIO lib
0709      * according the polarity specified in the DTS.
0710      */
0711     tas6424->standby_gpio = devm_gpiod_get_optional(dev, "standby",
0712                               GPIOD_OUT_LOW);
0713     if (IS_ERR(tas6424->standby_gpio)) {
0714         if (PTR_ERR(tas6424->standby_gpio) == -EPROBE_DEFER)
0715             return -EPROBE_DEFER;
0716         dev_info(dev, "failed to get standby GPIO: %ld\n",
0717             PTR_ERR(tas6424->standby_gpio));
0718         tas6424->standby_gpio = NULL;
0719     }
0720 
0721     /*
0722      * Get control of the mute pin and set it HIGH in order to start with
0723      * all the output muted.
0724      * Note: The actual pin polarity is taken care of in the GPIO lib
0725      * according the polarity specified in the DTS.
0726      */
0727     tas6424->mute_gpio = devm_gpiod_get_optional(dev, "mute",
0728                               GPIOD_OUT_HIGH);
0729     if (IS_ERR(tas6424->mute_gpio)) {
0730         if (PTR_ERR(tas6424->mute_gpio) == -EPROBE_DEFER)
0731             return -EPROBE_DEFER;
0732         dev_info(dev, "failed to get nmute GPIO: %ld\n",
0733             PTR_ERR(tas6424->mute_gpio));
0734         tas6424->mute_gpio = NULL;
0735     }
0736 
0737     for (i = 0; i < ARRAY_SIZE(tas6424->supplies); i++)
0738         tas6424->supplies[i].supply = tas6424_supply_names[i];
0739     ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(tas6424->supplies),
0740                       tas6424->supplies);
0741     if (ret) {
0742         dev_err(dev, "unable to request supplies: %d\n", ret);
0743         return ret;
0744     }
0745 
0746     ret = regulator_bulk_enable(ARRAY_SIZE(tas6424->supplies),
0747                     tas6424->supplies);
0748     if (ret) {
0749         dev_err(dev, "unable to enable supplies: %d\n", ret);
0750         return ret;
0751     }
0752 
0753     /* Reset device to establish well-defined startup state */
0754     ret = regmap_update_bits(tas6424->regmap, TAS6424_MODE_CTRL,
0755                  TAS6424_RESET, TAS6424_RESET);
0756     if (ret) {
0757         dev_err(dev, "unable to reset device: %d\n", ret);
0758         goto disable_regs;
0759     }
0760 
0761     INIT_DELAYED_WORK(&tas6424->fault_check_work, tas6424_fault_check_work);
0762 
0763     ret = devm_snd_soc_register_component(dev, &soc_codec_dev_tas6424,
0764                      tas6424_dai, ARRAY_SIZE(tas6424_dai));
0765     if (ret < 0) {
0766         dev_err(dev, "unable to register codec: %d\n", ret);
0767         goto disable_regs;
0768     }
0769 
0770     return 0;
0771 
0772 disable_regs:
0773     regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies), tas6424->supplies);
0774     return ret;
0775 }
0776 
0777 static int tas6424_i2c_remove(struct i2c_client *client)
0778 {
0779     struct device *dev = &client->dev;
0780     struct tas6424_data *tas6424 = dev_get_drvdata(dev);
0781     int ret;
0782 
0783     cancel_delayed_work_sync(&tas6424->fault_check_work);
0784 
0785     /* put the codec in stand-by */
0786     if (tas6424->standby_gpio)
0787         gpiod_set_value_cansleep(tas6424->standby_gpio, 1);
0788 
0789     ret = regulator_bulk_disable(ARRAY_SIZE(tas6424->supplies),
0790                      tas6424->supplies);
0791     if (ret < 0)
0792         dev_err(dev, "unable to disable supplies: %d\n", ret);
0793 
0794     return 0;
0795 }
0796 
0797 static const struct i2c_device_id tas6424_i2c_ids[] = {
0798     { "tas6424", 0 },
0799     { }
0800 };
0801 MODULE_DEVICE_TABLE(i2c, tas6424_i2c_ids);
0802 
0803 static struct i2c_driver tas6424_i2c_driver = {
0804     .driver = {
0805         .name = "tas6424",
0806         .of_match_table = of_match_ptr(tas6424_of_ids),
0807     },
0808     .probe_new = tas6424_i2c_probe,
0809     .remove = tas6424_i2c_remove,
0810     .id_table = tas6424_i2c_ids,
0811 };
0812 module_i2c_driver(tas6424_i2c_driver);
0813 
0814 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
0815 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
0816 MODULE_DESCRIPTION("TAS6424 Audio amplifier driver");
0817 MODULE_LICENSE("GPL v2");