Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * AK4104 ALSA SoC (ASoC) driver
0004  *
0005  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/spi/spi.h>
0011 #include <linux/of_device.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <sound/asoundef.h>
0015 #include <sound/core.h>
0016 #include <sound/soc.h>
0017 #include <sound/initval.h>
0018 
0019 /* AK4104 registers addresses */
0020 #define AK4104_REG_CONTROL1     0x00
0021 #define AK4104_REG_RESERVED     0x01
0022 #define AK4104_REG_CONTROL2     0x02
0023 #define AK4104_REG_TX           0x03
0024 #define AK4104_REG_CHN_STATUS(x)    ((x) + 0x04)
0025 #define AK4104_NUM_REGS         10
0026 
0027 #define AK4104_REG_MASK         0x1f
0028 #define AK4104_READ         0xc0
0029 #define AK4104_WRITE            0xe0
0030 #define AK4104_RESERVED_VAL     0x5b
0031 
0032 /* Bit masks for AK4104 registers */
0033 #define AK4104_CONTROL1_RSTN        (1 << 0)
0034 #define AK4104_CONTROL1_PW      (1 << 1)
0035 #define AK4104_CONTROL1_DIF0        (1 << 2)
0036 #define AK4104_CONTROL1_DIF1        (1 << 3)
0037 
0038 #define AK4104_CONTROL2_SEL0        (1 << 0)
0039 #define AK4104_CONTROL2_SEL1        (1 << 1)
0040 #define AK4104_CONTROL2_MODE        (1 << 2)
0041 
0042 #define AK4104_TX_TXE           (1 << 0)
0043 #define AK4104_TX_V         (1 << 1)
0044 
0045 struct ak4104_private {
0046     struct regmap *regmap;
0047     struct regulator *regulator;
0048 };
0049 
0050 static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
0051 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
0052 
0053 SND_SOC_DAPM_OUTPUT("TX"),
0054 };
0055 
0056 static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
0057     { "TXE", NULL, "Playback" },
0058     { "TX", NULL, "TXE" },
0059 };
0060 
0061 static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
0062                   unsigned int format)
0063 {
0064     struct snd_soc_component *component = codec_dai->component;
0065     struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
0066     int val = 0;
0067     int ret;
0068 
0069     /* set DAI format */
0070     switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
0071     case SND_SOC_DAIFMT_RIGHT_J:
0072         break;
0073     case SND_SOC_DAIFMT_LEFT_J:
0074         val |= AK4104_CONTROL1_DIF0;
0075         break;
0076     case SND_SOC_DAIFMT_I2S:
0077         val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
0078         break;
0079     default:
0080         dev_err(component->dev, "invalid dai format\n");
0081         return -EINVAL;
0082     }
0083 
0084     /* This device can only be consumer */
0085     if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC)
0086         return -EINVAL;
0087 
0088     ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
0089                  AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
0090                  val);
0091     if (ret < 0)
0092         return ret;
0093 
0094     return 0;
0095 }
0096 
0097 static int ak4104_hw_params(struct snd_pcm_substream *substream,
0098                 struct snd_pcm_hw_params *params,
0099                 struct snd_soc_dai *dai)
0100 {
0101     struct snd_soc_component *component = dai->component;
0102     struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
0103     int ret, val = 0;
0104 
0105     /* set the IEC958 bits: consumer mode, no copyright bit */
0106     val |= IEC958_AES0_CON_NOT_COPYRIGHT;
0107     regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
0108 
0109     val = 0;
0110 
0111     switch (params_rate(params)) {
0112     case 22050:
0113         val |= IEC958_AES3_CON_FS_22050;
0114         break;
0115     case 24000:
0116         val |= IEC958_AES3_CON_FS_24000;
0117         break;
0118     case 32000:
0119         val |= IEC958_AES3_CON_FS_32000;
0120         break;
0121     case 44100:
0122         val |= IEC958_AES3_CON_FS_44100;
0123         break;
0124     case 48000:
0125         val |= IEC958_AES3_CON_FS_48000;
0126         break;
0127     case 88200:
0128         val |= IEC958_AES3_CON_FS_88200;
0129         break;
0130     case 96000:
0131         val |= IEC958_AES3_CON_FS_96000;
0132         break;
0133     case 176400:
0134         val |= IEC958_AES3_CON_FS_176400;
0135         break;
0136     case 192000:
0137         val |= IEC958_AES3_CON_FS_192000;
0138         break;
0139     default:
0140         dev_err(component->dev, "unsupported sampling rate\n");
0141         return -EINVAL;
0142     }
0143 
0144     ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
0145     if (ret < 0)
0146         return ret;
0147 
0148     return 0;
0149 }
0150 
0151 static const struct snd_soc_dai_ops ak4101_dai_ops = {
0152     .hw_params = ak4104_hw_params,
0153     .set_fmt = ak4104_set_dai_fmt,
0154 };
0155 
0156 static struct snd_soc_dai_driver ak4104_dai = {
0157     .name = "ak4104-hifi",
0158     .playback = {
0159         .stream_name = "Playback",
0160         .channels_min = 2,
0161         .channels_max = 2,
0162         .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
0163              SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
0164              SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
0165              SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
0166         .formats = SNDRV_PCM_FMTBIT_S16_LE  |
0167                SNDRV_PCM_FMTBIT_S24_3LE |
0168                SNDRV_PCM_FMTBIT_S24_LE
0169     },
0170     .ops = &ak4101_dai_ops,
0171 };
0172 
0173 static int ak4104_probe(struct snd_soc_component *component)
0174 {
0175     struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
0176     int ret;
0177 
0178     ret = regulator_enable(ak4104->regulator);
0179     if (ret < 0) {
0180         dev_err(component->dev, "Unable to enable regulator: %d\n", ret);
0181         return ret;
0182     }
0183 
0184     /* set power-up and non-reset bits */
0185     ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
0186                  AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
0187                  AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
0188     if (ret < 0)
0189         goto exit_disable_regulator;
0190 
0191     /* enable transmitter */
0192     ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
0193                  AK4104_TX_TXE, AK4104_TX_TXE);
0194     if (ret < 0)
0195         goto exit_disable_regulator;
0196 
0197     return 0;
0198 
0199 exit_disable_regulator:
0200     regulator_disable(ak4104->regulator);
0201     return ret;
0202 }
0203 
0204 static void ak4104_remove(struct snd_soc_component *component)
0205 {
0206     struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
0207 
0208     regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
0209                AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
0210     regulator_disable(ak4104->regulator);
0211 }
0212 
0213 #ifdef CONFIG_PM
0214 static int ak4104_soc_suspend(struct snd_soc_component *component)
0215 {
0216     struct ak4104_private *priv = snd_soc_component_get_drvdata(component);
0217 
0218     regulator_disable(priv->regulator);
0219 
0220     return 0;
0221 }
0222 
0223 static int ak4104_soc_resume(struct snd_soc_component *component)
0224 {
0225     struct ak4104_private *priv = snd_soc_component_get_drvdata(component);
0226     int ret;
0227 
0228     ret = regulator_enable(priv->regulator);
0229     if (ret < 0)
0230         return ret;
0231 
0232     return 0;
0233 }
0234 #else
0235 #define ak4104_soc_suspend  NULL
0236 #define ak4104_soc_resume   NULL
0237 #endif /* CONFIG_PM */
0238 
0239 static const struct snd_soc_component_driver soc_component_device_ak4104 = {
0240     .probe          = ak4104_probe,
0241     .remove         = ak4104_remove,
0242     .suspend        = ak4104_soc_suspend,
0243     .resume         = ak4104_soc_resume,
0244     .dapm_widgets       = ak4104_dapm_widgets,
0245     .num_dapm_widgets   = ARRAY_SIZE(ak4104_dapm_widgets),
0246     .dapm_routes        = ak4104_dapm_routes,
0247     .num_dapm_routes    = ARRAY_SIZE(ak4104_dapm_routes),
0248     .idle_bias_on       = 1,
0249     .use_pmdown_time    = 1,
0250     .endianness     = 1,
0251 };
0252 
0253 static const struct regmap_config ak4104_regmap = {
0254     .reg_bits = 8,
0255     .val_bits = 8,
0256 
0257     .max_register = AK4104_NUM_REGS - 1,
0258     .read_flag_mask = AK4104_READ,
0259     .write_flag_mask = AK4104_WRITE,
0260 
0261     .cache_type = REGCACHE_RBTREE,
0262 };
0263 
0264 static int ak4104_spi_probe(struct spi_device *spi)
0265 {
0266     struct ak4104_private *ak4104;
0267     struct gpio_desc *reset_gpiod;
0268     unsigned int val;
0269     int ret;
0270 
0271     spi->bits_per_word = 8;
0272     spi->mode = SPI_MODE_0;
0273     ret = spi_setup(spi);
0274     if (ret < 0)
0275         return ret;
0276 
0277     ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
0278                   GFP_KERNEL);
0279     if (ak4104 == NULL)
0280         return -ENOMEM;
0281 
0282     ak4104->regulator = devm_regulator_get(&spi->dev, "vdd");
0283     if (IS_ERR(ak4104->regulator)) {
0284         ret = PTR_ERR(ak4104->regulator);
0285         dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret);
0286         return ret;
0287     }
0288 
0289     ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
0290     if (IS_ERR(ak4104->regmap)) {
0291         ret = PTR_ERR(ak4104->regmap);
0292         return ret;
0293     }
0294 
0295     reset_gpiod = devm_gpiod_get_optional(&spi->dev, "reset",
0296                           GPIOD_OUT_HIGH);
0297     if (PTR_ERR(reset_gpiod) == -EPROBE_DEFER)
0298         return -EPROBE_DEFER;
0299 
0300     /* read the 'reserved' register - according to the datasheet, it
0301      * should contain 0x5b. Not a good way to verify the presence of
0302      * the device, but there is no hardware ID register. */
0303     ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
0304     if (ret != 0)
0305         return ret;
0306     if (val != AK4104_RESERVED_VAL)
0307         return -ENODEV;
0308 
0309     spi_set_drvdata(spi, ak4104);
0310 
0311     ret = devm_snd_soc_register_component(&spi->dev,
0312             &soc_component_device_ak4104, &ak4104_dai, 1);
0313     return ret;
0314 }
0315 
0316 static const struct of_device_id ak4104_of_match[] = {
0317     { .compatible = "asahi-kasei,ak4104", },
0318     { }
0319 };
0320 MODULE_DEVICE_TABLE(of, ak4104_of_match);
0321 
0322 static const struct spi_device_id ak4104_id_table[] = {
0323     { "ak4104", 0 },
0324     { }
0325 };
0326 MODULE_DEVICE_TABLE(spi, ak4104_id_table);
0327 
0328 static struct spi_driver ak4104_spi_driver = {
0329     .driver  = {
0330         .name   = "ak4104",
0331         .of_match_table = ak4104_of_match,
0332     },
0333     .id_table = ak4104_id_table,
0334     .probe  = ak4104_spi_probe,
0335 };
0336 
0337 module_spi_driver(ak4104_spi_driver);
0338 
0339 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
0340 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
0341 MODULE_LICENSE("GPL");
0342