Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Audio driver for AK4458 DAC
0004 //
0005 // Copyright (C) 2016 Asahi Kasei Microdevices Corporation
0006 // Copyright 2018 NXP
0007 
0008 #include <linux/delay.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/i2c.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 #include <linux/of_gpio.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/slab.h>
0017 #include <sound/initval.h>
0018 #include <sound/pcm_params.h>
0019 #include <sound/soc.h>
0020 #include <sound/soc-dapm.h>
0021 #include <sound/tlv.h>
0022 
0023 #include "ak4458.h"
0024 
0025 #define AK4458_NUM_SUPPLIES 2
0026 static const char *ak4458_supply_names[AK4458_NUM_SUPPLIES] = {
0027     "DVDD",
0028     "AVDD",
0029 };
0030 
0031 enum ak4458_type {
0032     AK4458 = 0,
0033     AK4497 = 1,
0034 };
0035 
0036 struct ak4458_drvdata {
0037     struct snd_soc_dai_driver *dai_drv;
0038     const struct snd_soc_component_driver *comp_drv;
0039     enum ak4458_type type;
0040 };
0041 
0042 /* AK4458 Codec Private Data */
0043 struct ak4458_priv {
0044     struct regulator_bulk_data supplies[AK4458_NUM_SUPPLIES];
0045     const struct ak4458_drvdata *drvdata;
0046     struct device *dev;
0047     struct regmap *regmap;
0048     struct gpio_desc *reset_gpiod;
0049     struct gpio_desc *mute_gpiod;
0050     int digfil; /* SSLOW, SD, SLOW bits */
0051     int fs;     /* sampling rate */
0052     int fmt;
0053     int slots;
0054     int slot_width;
0055     u32 dsd_path;    /* For ak4497 */
0056 };
0057 
0058 static const struct reg_default ak4458_reg_defaults[] = {
0059     { 0x00, 0x0C }, /*  0x00    AK4458_00_CONTROL1  */
0060     { 0x01, 0x22 }, /*  0x01    AK4458_01_CONTROL2  */
0061     { 0x02, 0x00 }, /*  0x02    AK4458_02_CONTROL3  */
0062     { 0x03, 0xFF }, /*  0x03    AK4458_03_LCHATT    */
0063     { 0x04, 0xFF }, /*  0x04    AK4458_04_RCHATT    */
0064     { 0x05, 0x00 }, /*  0x05    AK4458_05_CONTROL4  */
0065     { 0x06, 0x00 }, /*  0x06    AK4458_06_DSD1      */
0066     { 0x07, 0x03 }, /*  0x07    AK4458_07_CONTROL5  */
0067     { 0x08, 0x00 }, /*  0x08    AK4458_08_SOUND_CONTROL */
0068     { 0x09, 0x00 }, /*  0x09    AK4458_09_DSD2      */
0069     { 0x0A, 0x0D }, /*  0x0A    AK4458_0A_CONTROL6  */
0070     { 0x0B, 0x0C }, /*  0x0B    AK4458_0B_CONTROL7  */
0071     { 0x0C, 0x00 }, /*  0x0C    AK4458_0C_CONTROL8  */
0072     { 0x0D, 0x00 }, /*  0x0D    AK4458_0D_CONTROL9  */
0073     { 0x0E, 0x50 }, /*  0x0E    AK4458_0E_CONTROL10 */
0074     { 0x0F, 0xFF }, /*  0x0F    AK4458_0F_L2CHATT   */
0075     { 0x10, 0xFF }, /*  0x10    AK4458_10_R2CHATT   */
0076     { 0x11, 0xFF }, /*  0x11    AK4458_11_L3CHATT   */
0077     { 0x12, 0xFF }, /*  0x12    AK4458_12_R3CHATT   */
0078     { 0x13, 0xFF }, /*  0x13    AK4458_13_L4CHATT   */
0079     { 0x14, 0xFF }, /*  0x14    AK4458_14_R4CHATT   */
0080 };
0081 
0082 /*
0083  * Volume control:
0084  * from -127 to 0 dB in 0.5 dB steps (mute instead of -127.5 dB)
0085  */
0086 static DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
0087 
0088 /*
0089  * DEM1 bit DEM0 bit Mode
0090  * 0 0 44.1kHz
0091  * 0 1 OFF (default)
0092  * 1 0 48kHz
0093  * 1 1 32kHz
0094  */
0095 static const char * const ak4458_dem_select_texts[] = {
0096     "44.1kHz", "OFF", "48kHz", "32kHz"
0097 };
0098 
0099 /*
0100  * SSLOW, SD, SLOW bits Digital Filter Setting
0101  * 0, 0, 0 : Sharp Roll-Off Filter
0102  * 0, 0, 1 : Slow Roll-Off Filter
0103  * 0, 1, 0 : Short delay Sharp Roll-Off Filter
0104  * 0, 1, 1 : Short delay Slow Roll-Off Filter
0105  * 1, *, * : Super Slow Roll-Off Filter
0106  */
0107 static const char * const ak4458_digfil_select_texts[] = {
0108     "Sharp Roll-Off Filter",
0109     "Slow Roll-Off Filter",
0110     "Short delay Sharp Roll-Off Filter",
0111     "Short delay Slow Roll-Off Filter",
0112     "Super Slow Roll-Off Filter"
0113 };
0114 
0115 /*
0116  * DZFB: Inverting Enable of DZF
0117  * 0: DZF goes H at Zero Detection
0118  * 1: DZF goes L at Zero Detection
0119  */
0120 static const char * const ak4458_dzfb_select_texts[] = {"H", "L"};
0121 
0122 /*
0123  * SC1-0 bits: Sound Mode Setting
0124  * 0 0 : Sound Mode 0
0125  * 0 1 : Sound Mode 1
0126  * 1 0 : Sound Mode 2
0127  * 1 1 : Reserved
0128  */
0129 static const char * const ak4458_sc_select_texts[] = {
0130     "Sound Mode 0", "Sound Mode 1", "Sound Mode 2"
0131 };
0132 
0133 /* FIR2-0 bits: FIR Filter Mode Setting */
0134 static const char * const ak4458_fir_select_texts[] = {
0135     "Mode 0", "Mode 1", "Mode 2", "Mode 3",
0136     "Mode 4", "Mode 5", "Mode 6", "Mode 7",
0137 };
0138 
0139 /* ATS1-0 bits Attenuation Speed */
0140 static const char * const ak4458_ats_select_texts[] = {
0141     "4080/fs", "2040/fs", "510/fs", "255/fs",
0142 };
0143 
0144 /* DIF2 bit Audio Interface Format Setting(BICK fs) */
0145 static const char * const ak4458_dif_select_texts[] = {"32fs,48fs", "64fs",};
0146 
0147 static const struct soc_enum ak4458_dac1_dem_enum =
0148     SOC_ENUM_SINGLE(AK4458_01_CONTROL2, 1,
0149             ARRAY_SIZE(ak4458_dem_select_texts),
0150             ak4458_dem_select_texts);
0151 static const struct soc_enum ak4458_dac2_dem_enum =
0152     SOC_ENUM_SINGLE(AK4458_0A_CONTROL6, 0,
0153             ARRAY_SIZE(ak4458_dem_select_texts),
0154             ak4458_dem_select_texts);
0155 static const struct soc_enum ak4458_dac3_dem_enum =
0156     SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 4,
0157             ARRAY_SIZE(ak4458_dem_select_texts),
0158             ak4458_dem_select_texts);
0159 static const struct soc_enum ak4458_dac4_dem_enum =
0160     SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 6,
0161             ARRAY_SIZE(ak4458_dem_select_texts),
0162             ak4458_dem_select_texts);
0163 static const struct soc_enum ak4458_digfil_enum =
0164     SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ak4458_digfil_select_texts),
0165                 ak4458_digfil_select_texts);
0166 static const struct soc_enum ak4458_dzfb_enum =
0167     SOC_ENUM_SINGLE(AK4458_02_CONTROL3, 2,
0168             ARRAY_SIZE(ak4458_dzfb_select_texts),
0169             ak4458_dzfb_select_texts);
0170 static const struct soc_enum ak4458_sm_enum =
0171     SOC_ENUM_SINGLE(AK4458_08_SOUND_CONTROL, 0,
0172             ARRAY_SIZE(ak4458_sc_select_texts),
0173             ak4458_sc_select_texts);
0174 static const struct soc_enum ak4458_fir_enum =
0175     SOC_ENUM_SINGLE(AK4458_0C_CONTROL8, 0,
0176             ARRAY_SIZE(ak4458_fir_select_texts),
0177             ak4458_fir_select_texts);
0178 static const struct soc_enum ak4458_ats_enum =
0179     SOC_ENUM_SINGLE(AK4458_0B_CONTROL7, 6,
0180             ARRAY_SIZE(ak4458_ats_select_texts),
0181             ak4458_ats_select_texts);
0182 static const struct soc_enum ak4458_dif_enum =
0183     SOC_ENUM_SINGLE(AK4458_00_CONTROL1, 3,
0184             ARRAY_SIZE(ak4458_dif_select_texts),
0185             ak4458_dif_select_texts);
0186 
0187 static int get_digfil(struct snd_kcontrol *kcontrol,
0188               struct snd_ctl_elem_value *ucontrol)
0189 {
0190     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0191     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0192 
0193     ucontrol->value.enumerated.item[0] = ak4458->digfil;
0194 
0195     return 0;
0196 }
0197 
0198 static int set_digfil(struct snd_kcontrol *kcontrol,
0199               struct snd_ctl_elem_value *ucontrol)
0200 {
0201     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0202     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0203     int num;
0204 
0205     num = ucontrol->value.enumerated.item[0];
0206     if (num > 4)
0207         return -EINVAL;
0208 
0209     ak4458->digfil = num;
0210 
0211     /* write SD bit */
0212     snd_soc_component_update_bits(component, AK4458_01_CONTROL2,
0213                 AK4458_SD_MASK,
0214                 ((ak4458->digfil & 0x02) << 4));
0215 
0216     /* write SLOW bit */
0217     snd_soc_component_update_bits(component, AK4458_02_CONTROL3,
0218                 AK4458_SLOW_MASK,
0219                 (ak4458->digfil & 0x01));
0220 
0221     /* write SSLOW bit */
0222     snd_soc_component_update_bits(component, AK4458_05_CONTROL4,
0223                 AK4458_SSLOW_MASK,
0224                 ((ak4458->digfil & 0x04) >> 2));
0225 
0226     return 0;
0227 }
0228 
0229 static const struct snd_kcontrol_new ak4458_snd_controls[] = {
0230     SOC_DOUBLE_R_TLV("DAC1 Playback Volume", AK4458_03_LCHATT,
0231              AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
0232     SOC_DOUBLE_R_TLV("DAC2 Playback Volume", AK4458_0F_L2CHATT,
0233              AK4458_10_R2CHATT, 0, 0xFF, 0, dac_tlv),
0234     SOC_DOUBLE_R_TLV("DAC3 Playback Volume", AK4458_11_L3CHATT,
0235              AK4458_12_R3CHATT, 0, 0xFF, 0, dac_tlv),
0236     SOC_DOUBLE_R_TLV("DAC4 Playback Volume", AK4458_13_L4CHATT,
0237              AK4458_14_R4CHATT, 0, 0xFF, 0, dac_tlv),
0238     SOC_ENUM("AK4458 De-emphasis Response DAC1", ak4458_dac1_dem_enum),
0239     SOC_ENUM("AK4458 De-emphasis Response DAC2", ak4458_dac2_dem_enum),
0240     SOC_ENUM("AK4458 De-emphasis Response DAC3", ak4458_dac3_dem_enum),
0241     SOC_ENUM("AK4458 De-emphasis Response DAC4", ak4458_dac4_dem_enum),
0242     SOC_ENUM_EXT("AK4458 Digital Filter Setting", ak4458_digfil_enum,
0243              get_digfil, set_digfil),
0244     SOC_ENUM("AK4458 Inverting Enable of DZFB", ak4458_dzfb_enum),
0245     SOC_ENUM("AK4458 Sound Mode", ak4458_sm_enum),
0246     SOC_ENUM("AK4458 FIR Filter Mode Setting", ak4458_fir_enum),
0247     SOC_ENUM("AK4458 Attenuation transition Time Setting",
0248          ak4458_ats_enum),
0249     SOC_ENUM("AK4458 BICK fs Setting", ak4458_dif_enum),
0250 };
0251 
0252 /* ak4458 dapm widgets */
0253 static const struct snd_soc_dapm_widget ak4458_dapm_widgets[] = {
0254     SND_SOC_DAPM_DAC("AK4458 DAC1", NULL, AK4458_0A_CONTROL6, 2, 0),/*pw*/
0255     SND_SOC_DAPM_AIF_IN("AK4458 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
0256     SND_SOC_DAPM_OUTPUT("AK4458 AOUTA"),
0257 
0258     SND_SOC_DAPM_DAC("AK4458 DAC2", NULL, AK4458_0A_CONTROL6, 3, 0),/*pw*/
0259     SND_SOC_DAPM_OUTPUT("AK4458 AOUTB"),
0260 
0261     SND_SOC_DAPM_DAC("AK4458 DAC3", NULL, AK4458_0B_CONTROL7, 2, 0),/*pw*/
0262     SND_SOC_DAPM_OUTPUT("AK4458 AOUTC"),
0263 
0264     SND_SOC_DAPM_DAC("AK4458 DAC4", NULL, AK4458_0B_CONTROL7, 3, 0),/*pw*/
0265     SND_SOC_DAPM_OUTPUT("AK4458 AOUTD"),
0266 };
0267 
0268 static const struct snd_soc_dapm_route ak4458_intercon[] = {
0269     {"AK4458 DAC1",     NULL,   "AK4458 SDTI"},
0270     {"AK4458 AOUTA",    NULL,   "AK4458 DAC1"},
0271 
0272     {"AK4458 DAC2",     NULL,   "AK4458 SDTI"},
0273     {"AK4458 AOUTB",    NULL,   "AK4458 DAC2"},
0274 
0275     {"AK4458 DAC3",     NULL,   "AK4458 SDTI"},
0276     {"AK4458 AOUTC",    NULL,   "AK4458 DAC3"},
0277 
0278     {"AK4458 DAC4",     NULL,   "AK4458 SDTI"},
0279     {"AK4458 AOUTD",    NULL,   "AK4458 DAC4"},
0280 };
0281 
0282 /* ak4497 controls */
0283 static const struct snd_kcontrol_new ak4497_snd_controls[] = {
0284     SOC_DOUBLE_R_TLV("DAC Playback Volume", AK4458_03_LCHATT,
0285              AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
0286     SOC_ENUM("AK4497 De-emphasis Response DAC", ak4458_dac1_dem_enum),
0287     SOC_ENUM_EXT("AK4497 Digital Filter Setting", ak4458_digfil_enum,
0288              get_digfil, set_digfil),
0289     SOC_ENUM("AK4497 Inverting Enable of DZFB", ak4458_dzfb_enum),
0290     SOC_ENUM("AK4497 Sound Mode", ak4458_sm_enum),
0291     SOC_ENUM("AK4497 Attenuation transition Time Setting",
0292          ak4458_ats_enum),
0293 };
0294 
0295 /* ak4497 dapm widgets */
0296 static const struct snd_soc_dapm_widget ak4497_dapm_widgets[] = {
0297     SND_SOC_DAPM_DAC("AK4497 DAC", NULL, AK4458_0A_CONTROL6, 2, 0),
0298     SND_SOC_DAPM_AIF_IN("AK4497 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
0299     SND_SOC_DAPM_OUTPUT("AK4497 AOUT"),
0300 };
0301 
0302 /* ak4497 dapm routes */
0303 static const struct snd_soc_dapm_route ak4497_intercon[] = {
0304     {"AK4497 DAC",      NULL,   "AK4497 SDTI"},
0305     {"AK4497 AOUT",     NULL,   "AK4497 DAC"},
0306 
0307 };
0308 
0309 static int ak4458_get_tdm_mode(struct ak4458_priv *ak4458)
0310 {
0311     switch (ak4458->slots * ak4458->slot_width) {
0312     case 128:
0313         return 1;
0314     case 256:
0315         return 2;
0316     case 512:
0317         return 3;
0318     default:
0319         return 0;
0320     }
0321 }
0322 
0323 static int ak4458_rstn_control(struct snd_soc_component *component, int bit)
0324 {
0325     int ret;
0326 
0327     if (bit)
0328         ret = snd_soc_component_update_bits(component,
0329                       AK4458_00_CONTROL1,
0330                       AK4458_RSTN_MASK,
0331                       0x1);
0332     else
0333         ret = snd_soc_component_update_bits(component,
0334                       AK4458_00_CONTROL1,
0335                       AK4458_RSTN_MASK,
0336                       0x0);
0337     if (ret < 0)
0338         return ret;
0339 
0340     return 0;
0341 }
0342 
0343 static int ak4458_hw_params(struct snd_pcm_substream *substream,
0344                 struct snd_pcm_hw_params *params,
0345                 struct snd_soc_dai *dai)
0346 {
0347     struct snd_soc_component *component = dai->component;
0348     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0349     int pcm_width = max(params_physical_width(params), ak4458->slot_width);
0350     u8 format, dsdsel0, dsdsel1, dchn;
0351     int nfs1, dsd_bclk, ret, channels, channels_max;
0352 
0353     nfs1 = params_rate(params);
0354     ak4458->fs = nfs1;
0355 
0356     /* calculate bit clock */
0357     channels = params_channels(params);
0358     channels_max = dai->driver->playback.channels_max;
0359 
0360     switch (params_format(params)) {
0361     case SNDRV_PCM_FORMAT_DSD_U8:
0362     case SNDRV_PCM_FORMAT_DSD_U16_LE:
0363     case SNDRV_PCM_FORMAT_DSD_U16_BE:
0364     case SNDRV_PCM_FORMAT_DSD_U32_LE:
0365     case SNDRV_PCM_FORMAT_DSD_U32_BE:
0366         dsd_bclk = nfs1 * params_physical_width(params);
0367         switch (dsd_bclk) {
0368         case 2822400:
0369             dsdsel0 = 0;
0370             dsdsel1 = 0;
0371             break;
0372         case 5644800:
0373             dsdsel0 = 1;
0374             dsdsel1 = 0;
0375             break;
0376         case 11289600:
0377             dsdsel0 = 0;
0378             dsdsel1 = 1;
0379             break;
0380         case 22579200:
0381             if (ak4458->drvdata->type == AK4497) {
0382                 dsdsel0 = 1;
0383                 dsdsel1 = 1;
0384             } else {
0385                 dev_err(dai->dev, "DSD512 not supported.\n");
0386                 return -EINVAL;
0387             }
0388             break;
0389         default:
0390             dev_err(dai->dev, "Unsupported dsd bclk.\n");
0391             return -EINVAL;
0392         }
0393 
0394         snd_soc_component_update_bits(component, AK4458_06_DSD1,
0395                           AK4458_DSDSEL_MASK, dsdsel0);
0396         snd_soc_component_update_bits(component, AK4458_09_DSD2,
0397                           AK4458_DSDSEL_MASK, dsdsel1);
0398         break;
0399     }
0400 
0401     /* Master Clock Frequency Auto Setting Mode Enable */
0402     snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 0x80, 0x80);
0403 
0404     switch (pcm_width) {
0405     case 16:
0406         if (ak4458->fmt == SND_SOC_DAIFMT_I2S)
0407             format = AK4458_DIF_24BIT_I2S;
0408         else
0409             format = AK4458_DIF_16BIT_LSB;
0410         break;
0411     case 32:
0412         switch (ak4458->fmt) {
0413         case SND_SOC_DAIFMT_I2S:
0414             format = AK4458_DIF_32BIT_I2S;
0415             break;
0416         case SND_SOC_DAIFMT_LEFT_J:
0417             format = AK4458_DIF_32BIT_MSB;
0418             break;
0419         case SND_SOC_DAIFMT_RIGHT_J:
0420             format = AK4458_DIF_32BIT_LSB;
0421             break;
0422         case SND_SOC_DAIFMT_DSP_B:
0423             format = AK4458_DIF_32BIT_MSB;
0424             break;
0425         case SND_SOC_DAIFMT_PDM:
0426             format = AK4458_DIF_32BIT_MSB;
0427             break;
0428         default:
0429             return -EINVAL;
0430         }
0431         break;
0432     default:
0433         return -EINVAL;
0434     }
0435 
0436     snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
0437                 AK4458_DIF_MASK, format);
0438 
0439     /*
0440      * Enable/disable Daisy Chain if in TDM mode and the number of played
0441      * channels is bigger than the maximum supported number of channels
0442      */
0443     dchn = ak4458_get_tdm_mode(ak4458) &&
0444         (ak4458->fmt == SND_SOC_DAIFMT_DSP_B) &&
0445         (channels > channels_max) ? AK4458_DCHAIN_MASK : 0;
0446 
0447     snd_soc_component_update_bits(component, AK4458_0B_CONTROL7,
0448                       AK4458_DCHAIN_MASK, dchn);
0449 
0450     ret = ak4458_rstn_control(component, 0);
0451     if (ret)
0452         return ret;
0453 
0454     ret = ak4458_rstn_control(component, 1);
0455     if (ret)
0456         return ret;
0457 
0458     return 0;
0459 }
0460 
0461 static int ak4458_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0462 {
0463     struct snd_soc_component *component = dai->component;
0464     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0465     int ret;
0466 
0467     switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0468     case SND_SOC_DAIFMT_CBC_CFC: /* Consumer Mode */
0469         break;
0470     case SND_SOC_DAIFMT_CBP_CFP: /* Provider Mode is not supported */
0471     case SND_SOC_DAIFMT_CBC_CFP:
0472     case SND_SOC_DAIFMT_CBP_CFC:
0473     default:
0474         dev_err(component->dev, "Clock provider mode unsupported\n");
0475         return -EINVAL;
0476     }
0477 
0478     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0479     case SND_SOC_DAIFMT_I2S:
0480     case SND_SOC_DAIFMT_LEFT_J:
0481     case SND_SOC_DAIFMT_RIGHT_J:
0482     case SND_SOC_DAIFMT_DSP_B:
0483     case SND_SOC_DAIFMT_PDM:
0484         ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
0485         break;
0486     default:
0487         dev_err(component->dev, "Audio format 0x%02X unsupported\n",
0488             fmt & SND_SOC_DAIFMT_FORMAT_MASK);
0489         return -EINVAL;
0490     }
0491 
0492     /* DSD mode */
0493     snd_soc_component_update_bits(component, AK4458_02_CONTROL3,
0494                       AK4458_DP_MASK,
0495                       ak4458->fmt == SND_SOC_DAIFMT_PDM ?
0496                       AK4458_DP_MASK : 0);
0497 
0498     ret = ak4458_rstn_control(component, 0);
0499     if (ret)
0500         return ret;
0501 
0502     ret = ak4458_rstn_control(component, 1);
0503     if (ret)
0504         return ret;
0505 
0506     return 0;
0507 }
0508 
0509 static const int att_speed[] = { 4080, 2040, 510, 255 };
0510 
0511 static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
0512 {
0513     struct snd_soc_component *component = dai->component;
0514     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0515     int nfs, ndt, reg;
0516     int ats;
0517 
0518     nfs = ak4458->fs;
0519 
0520     reg = snd_soc_component_read(component, AK4458_0B_CONTROL7);
0521     ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT;
0522 
0523     ndt = att_speed[ats] / (nfs / 1000);
0524 
0525     if (mute) {
0526         snd_soc_component_update_bits(component, AK4458_01_CONTROL2,  0x01, 1);
0527         mdelay(ndt);
0528         if (ak4458->mute_gpiod)
0529             gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
0530     } else {
0531         if (ak4458->mute_gpiod)
0532             gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
0533         snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0);
0534         mdelay(ndt);
0535     }
0536 
0537     return 0;
0538 }
0539 
0540 static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
0541                    unsigned int rx_mask, int slots, int slot_width)
0542 {
0543     struct snd_soc_component *component = dai->component;
0544     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0545     int mode;
0546 
0547     ak4458->slots = slots;
0548     ak4458->slot_width = slot_width;
0549 
0550     mode = ak4458_get_tdm_mode(ak4458) << AK4458_MODE_SHIFT;
0551 
0552     snd_soc_component_update_bits(component, AK4458_0A_CONTROL6,
0553                 AK4458_MODE_MASK,
0554                 mode);
0555 
0556     return 0;
0557 }
0558 
0559 #define AK4458_FORMATS  (SNDRV_PCM_FMTBIT_S16_LE |\
0560              SNDRV_PCM_FMTBIT_S24_LE |\
0561              SNDRV_PCM_FMTBIT_S32_LE |\
0562              SNDRV_PCM_FMTBIT_DSD_U8 |\
0563              SNDRV_PCM_FMTBIT_DSD_U16_LE |\
0564              SNDRV_PCM_FMTBIT_DSD_U32_LE)
0565 
0566 static const unsigned int ak4458_rates[] = {
0567     8000, 11025,  16000, 22050,
0568     32000, 44100, 48000, 88200,
0569     96000, 176400, 192000, 352800,
0570     384000, 705600, 768000, 1411200,
0571     2822400,
0572 };
0573 
0574 static const struct snd_pcm_hw_constraint_list ak4458_rate_constraints = {
0575     .count = ARRAY_SIZE(ak4458_rates),
0576     .list = ak4458_rates,
0577 };
0578 
0579 static int ak4458_startup(struct snd_pcm_substream *substream,
0580               struct snd_soc_dai *dai)
0581 {
0582     int ret;
0583 
0584     ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
0585                      SNDRV_PCM_HW_PARAM_RATE,
0586                      &ak4458_rate_constraints);
0587 
0588     return ret;
0589 }
0590 
0591 static const struct snd_soc_dai_ops ak4458_dai_ops = {
0592     .startup        = ak4458_startup,
0593     .hw_params  = ak4458_hw_params,
0594     .set_fmt    = ak4458_set_dai_fmt,
0595     .mute_stream    = ak4458_set_dai_mute,
0596     .set_tdm_slot   = ak4458_set_tdm_slot,
0597     .no_capture_mute = 1,
0598 };
0599 
0600 static struct snd_soc_dai_driver ak4458_dai = {
0601     .name = "ak4458-aif",
0602     .playback = {
0603         .stream_name = "Playback",
0604         .channels_min = 1,
0605         .channels_max = 8,
0606         .rates = SNDRV_PCM_RATE_KNOT,
0607         .formats = AK4458_FORMATS,
0608     },
0609     .ops = &ak4458_dai_ops,
0610 };
0611 
0612 static struct snd_soc_dai_driver ak4497_dai = {
0613     .name = "ak4497-aif",
0614     .playback = {
0615         .stream_name = "Playback",
0616         .channels_min = 1,
0617         .channels_max = 2,
0618         .rates = SNDRV_PCM_RATE_KNOT,
0619         .formats = AK4458_FORMATS,
0620     },
0621     .ops = &ak4458_dai_ops,
0622 };
0623 
0624 static void ak4458_reset(struct ak4458_priv *ak4458, bool active)
0625 {
0626     if (ak4458->reset_gpiod) {
0627         gpiod_set_value_cansleep(ak4458->reset_gpiod, active);
0628         usleep_range(1000, 2000);
0629     }
0630 }
0631 
0632 static int ak4458_init(struct snd_soc_component *component)
0633 {
0634     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0635     int ret;
0636 
0637     /* External Mute ON */
0638     if (ak4458->mute_gpiod)
0639         gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
0640 
0641     ak4458_reset(ak4458, false);
0642 
0643     ret = snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
0644                 0x80, 0x80);   /* ACKS bit = 1; 10000000 */
0645     if (ret < 0)
0646         return ret;
0647 
0648     if (ak4458->drvdata->type == AK4497) {
0649         ret = snd_soc_component_update_bits(component, AK4458_09_DSD2,
0650                             0x4, (ak4458->dsd_path << 2));
0651         if (ret < 0)
0652             return ret;
0653     }
0654 
0655     return ak4458_rstn_control(component, 1);
0656 }
0657 
0658 static int ak4458_probe(struct snd_soc_component *component)
0659 {
0660     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0661 
0662     ak4458->fs = 48000;
0663 
0664     return ak4458_init(component);
0665 }
0666 
0667 static void ak4458_remove(struct snd_soc_component *component)
0668 {
0669     struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
0670 
0671     ak4458_reset(ak4458, true);
0672 }
0673 
0674 #ifdef CONFIG_PM
0675 static int __maybe_unused ak4458_runtime_suspend(struct device *dev)
0676 {
0677     struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
0678 
0679     regcache_cache_only(ak4458->regmap, true);
0680 
0681     ak4458_reset(ak4458, true);
0682 
0683     if (ak4458->mute_gpiod)
0684         gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
0685 
0686     regulator_bulk_disable(ARRAY_SIZE(ak4458->supplies),
0687                    ak4458->supplies);
0688     return 0;
0689 }
0690 
0691 static int __maybe_unused ak4458_runtime_resume(struct device *dev)
0692 {
0693     struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
0694     int ret;
0695 
0696     ret = regulator_bulk_enable(ARRAY_SIZE(ak4458->supplies),
0697                     ak4458->supplies);
0698     if (ret != 0) {
0699         dev_err(ak4458->dev, "Failed to enable supplies: %d\n", ret);
0700         return ret;
0701     }
0702 
0703     if (ak4458->mute_gpiod)
0704         gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
0705 
0706     ak4458_reset(ak4458, true);
0707     ak4458_reset(ak4458, false);
0708 
0709     regcache_cache_only(ak4458->regmap, false);
0710     regcache_mark_dirty(ak4458->regmap);
0711 
0712     return regcache_sync(ak4458->regmap);
0713 }
0714 #endif /* CONFIG_PM */
0715 
0716 static const struct snd_soc_component_driver soc_codec_dev_ak4458 = {
0717     .probe          = ak4458_probe,
0718     .remove         = ak4458_remove,
0719     .controls       = ak4458_snd_controls,
0720     .num_controls       = ARRAY_SIZE(ak4458_snd_controls),
0721     .dapm_widgets       = ak4458_dapm_widgets,
0722     .num_dapm_widgets   = ARRAY_SIZE(ak4458_dapm_widgets),
0723     .dapm_routes        = ak4458_intercon,
0724     .num_dapm_routes    = ARRAY_SIZE(ak4458_intercon),
0725     .idle_bias_on       = 1,
0726     .use_pmdown_time    = 1,
0727     .endianness     = 1,
0728 };
0729 
0730 static const struct snd_soc_component_driver soc_codec_dev_ak4497 = {
0731     .probe          = ak4458_probe,
0732     .remove         = ak4458_remove,
0733     .controls       = ak4497_snd_controls,
0734     .num_controls       = ARRAY_SIZE(ak4497_snd_controls),
0735     .dapm_widgets       = ak4497_dapm_widgets,
0736     .num_dapm_widgets   = ARRAY_SIZE(ak4497_dapm_widgets),
0737     .dapm_routes        = ak4497_intercon,
0738     .num_dapm_routes    = ARRAY_SIZE(ak4497_intercon),
0739     .idle_bias_on       = 1,
0740     .use_pmdown_time    = 1,
0741     .endianness     = 1,
0742 };
0743 
0744 static const struct regmap_config ak4458_regmap = {
0745     .reg_bits = 8,
0746     .val_bits = 8,
0747 
0748     .max_register = AK4458_14_R4CHATT,
0749     .reg_defaults = ak4458_reg_defaults,
0750     .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults),
0751     .cache_type = REGCACHE_RBTREE,
0752 };
0753 
0754 static const struct ak4458_drvdata ak4458_drvdata = {
0755     .dai_drv = &ak4458_dai,
0756     .comp_drv = &soc_codec_dev_ak4458,
0757     .type = AK4458,
0758 };
0759 
0760 static const struct ak4458_drvdata ak4497_drvdata = {
0761     .dai_drv = &ak4497_dai,
0762     .comp_drv = &soc_codec_dev_ak4497,
0763     .type = AK4497,
0764 };
0765 
0766 static const struct dev_pm_ops ak4458_pm = {
0767     SET_RUNTIME_PM_OPS(ak4458_runtime_suspend, ak4458_runtime_resume, NULL)
0768     SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
0769                 pm_runtime_force_resume)
0770 };
0771 
0772 static int ak4458_i2c_probe(struct i2c_client *i2c)
0773 {
0774     struct ak4458_priv *ak4458;
0775     int ret, i;
0776 
0777     ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL);
0778     if (!ak4458)
0779         return -ENOMEM;
0780 
0781     ak4458->regmap = devm_regmap_init_i2c(i2c, &ak4458_regmap);
0782     if (IS_ERR(ak4458->regmap))
0783         return PTR_ERR(ak4458->regmap);
0784 
0785     i2c_set_clientdata(i2c, ak4458);
0786     ak4458->dev = &i2c->dev;
0787 
0788     ak4458->drvdata = of_device_get_match_data(&i2c->dev);
0789 
0790     ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset",
0791                               GPIOD_OUT_LOW);
0792     if (IS_ERR(ak4458->reset_gpiod))
0793         return PTR_ERR(ak4458->reset_gpiod);
0794 
0795     ak4458->mute_gpiod = devm_gpiod_get_optional(ak4458->dev, "mute",
0796                              GPIOD_OUT_LOW);
0797     if (IS_ERR(ak4458->mute_gpiod))
0798         return PTR_ERR(ak4458->mute_gpiod);
0799 
0800     /* Optional property for ak4497 */
0801     of_property_read_u32(i2c->dev.of_node, "dsd-path", &ak4458->dsd_path);
0802 
0803     for (i = 0; i < ARRAY_SIZE(ak4458->supplies); i++)
0804         ak4458->supplies[i].supply = ak4458_supply_names[i];
0805 
0806     ret = devm_regulator_bulk_get(ak4458->dev, ARRAY_SIZE(ak4458->supplies),
0807                       ak4458->supplies);
0808     if (ret != 0) {
0809         dev_err(ak4458->dev, "Failed to request supplies: %d\n", ret);
0810         return ret;
0811     }
0812 
0813     ret = devm_snd_soc_register_component(ak4458->dev,
0814                           ak4458->drvdata->comp_drv,
0815                           ak4458->drvdata->dai_drv, 1);
0816     if (ret < 0) {
0817         dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret);
0818         return ret;
0819     }
0820 
0821     pm_runtime_enable(&i2c->dev);
0822     regcache_cache_only(ak4458->regmap, true);
0823 
0824     return 0;
0825 }
0826 
0827 static int ak4458_i2c_remove(struct i2c_client *i2c)
0828 {
0829     pm_runtime_disable(&i2c->dev);
0830 
0831     return 0;
0832 }
0833 
0834 static const struct of_device_id ak4458_of_match[] = {
0835     { .compatible = "asahi-kasei,ak4458", .data = &ak4458_drvdata},
0836     { .compatible = "asahi-kasei,ak4497", .data = &ak4497_drvdata},
0837     { },
0838 };
0839 MODULE_DEVICE_TABLE(of, ak4458_of_match);
0840 
0841 static struct i2c_driver ak4458_i2c_driver = {
0842     .driver = {
0843         .name = "ak4458",
0844         .pm = &ak4458_pm,
0845         .of_match_table = ak4458_of_match,
0846         },
0847     .probe_new = ak4458_i2c_probe,
0848     .remove = ak4458_i2c_remove,
0849 };
0850 
0851 module_i2c_driver(ak4458_i2c_driver);
0852 
0853 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
0854 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
0855 MODULE_DESCRIPTION("ASoC AK4458 DAC driver");
0856 MODULE_LICENSE("GPL v2");