Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Audio driver for AK5558 ADC
0004 //
0005 // Copyright (C) 2015 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/pm_runtime.h>
0014 #include <linux/regmap.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/slab.h>
0017 
0018 #include <sound/initval.h>
0019 #include <sound/pcm.h>
0020 #include <sound/pcm_params.h>
0021 #include <sound/soc.h>
0022 #include <sound/soc-dapm.h>
0023 #include <sound/tlv.h>
0024 
0025 #include "ak5558.h"
0026 
0027 enum ak555x_type {
0028     AK5558,
0029     AK5552,
0030 };
0031 
0032 #define AK5558_NUM_SUPPLIES 2
0033 static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = {
0034     "DVDD",
0035     "AVDD",
0036 };
0037 
0038 /* AK5558 Codec Private Data */
0039 struct ak5558_priv {
0040     struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES];
0041     struct snd_soc_component component;
0042     struct regmap *regmap;
0043     struct i2c_client *i2c;
0044     struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */
0045     int slots;
0046     int slot_width;
0047 };
0048 
0049 /* ak5558 register cache & default register settings */
0050 static const struct reg_default ak5558_reg[] = {
0051     { 0x0, 0xFF },  /*  0x00    AK5558_00_POWER_MANAGEMENT1 */
0052     { 0x1, 0x01 },  /*  0x01    AK5558_01_POWER_MANAGEMENT2 */
0053     { 0x2, 0x01 },  /*  0x02    AK5558_02_CONTROL1      */
0054     { 0x3, 0x00 },  /*  0x03    AK5558_03_CONTROL2      */
0055     { 0x4, 0x00 },  /*  0x04    AK5558_04_CONTROL3      */
0056     { 0x5, 0x00 }   /*  0x05    AK5558_05_DSD           */
0057 };
0058 
0059 static const char * const mono_texts[] = {
0060     "8 Slot", "2 Slot", "4 Slot", "1 Slot",
0061 };
0062 
0063 static const struct soc_enum ak5558_mono_enum[] = {
0064     SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
0065             ARRAY_SIZE(mono_texts), mono_texts),
0066 };
0067 
0068 static const char * const mono_5552_texts[] = {
0069     "2 Slot", "1 Slot (Fixed)", "2 Slot", "1 Slot (Optimal)",
0070 };
0071 
0072 static const struct soc_enum ak5552_mono_enum[] = {
0073     SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
0074             ARRAY_SIZE(mono_5552_texts), mono_5552_texts),
0075 };
0076 
0077 static const char * const digfil_texts[] = {
0078     "Sharp Roll-Off", "Slow Roll-Off",
0079     "Short Delay Sharp Roll-Off", "Short Delay Slow Roll-Off",
0080 };
0081 
0082 static const struct soc_enum ak5558_adcset_enum[] = {
0083     SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
0084             ARRAY_SIZE(digfil_texts), digfil_texts),
0085 };
0086 
0087 static const struct snd_kcontrol_new ak5558_snd_controls[] = {
0088     SOC_ENUM("Monaural Mode", ak5558_mono_enum[0]),
0089     SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
0090 };
0091 
0092 static const struct snd_kcontrol_new ak5552_snd_controls[] = {
0093     SOC_ENUM("Monaural Mode", ak5552_mono_enum[0]),
0094     SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
0095 };
0096 
0097 static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
0098     /* Analog Input */
0099     SND_SOC_DAPM_INPUT("AIN1"),
0100     SND_SOC_DAPM_INPUT("AIN2"),
0101     SND_SOC_DAPM_INPUT("AIN3"),
0102     SND_SOC_DAPM_INPUT("AIN4"),
0103     SND_SOC_DAPM_INPUT("AIN5"),
0104     SND_SOC_DAPM_INPUT("AIN6"),
0105     SND_SOC_DAPM_INPUT("AIN7"),
0106     SND_SOC_DAPM_INPUT("AIN8"),
0107 
0108     SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
0109     SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
0110     SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
0111     SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
0112     SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
0113     SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
0114     SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
0115     SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),
0116 
0117     SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
0118 };
0119 
0120 static const struct snd_soc_dapm_widget ak5552_dapm_widgets[] = {
0121     /* Analog Input */
0122     SND_SOC_DAPM_INPUT("AIN1"),
0123     SND_SOC_DAPM_INPUT("AIN2"),
0124 
0125     SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
0126     SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
0127 
0128     SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
0129 };
0130 
0131 static const struct snd_soc_dapm_route ak5558_intercon[] = {
0132     {"ADC Ch1", NULL, "AIN1"},
0133     {"SDTO", NULL, "ADC Ch1"},
0134 
0135     {"ADC Ch2", NULL, "AIN2"},
0136     {"SDTO", NULL, "ADC Ch2"},
0137 
0138     {"ADC Ch3", NULL, "AIN3"},
0139     {"SDTO", NULL, "ADC Ch3"},
0140 
0141     {"ADC Ch4", NULL, "AIN4"},
0142     {"SDTO", NULL, "ADC Ch4"},
0143 
0144     {"ADC Ch5", NULL, "AIN5"},
0145     {"SDTO", NULL, "ADC Ch5"},
0146 
0147     {"ADC Ch6", NULL, "AIN6"},
0148     {"SDTO", NULL, "ADC Ch6"},
0149 
0150     {"ADC Ch7", NULL, "AIN7"},
0151     {"SDTO", NULL, "ADC Ch7"},
0152 
0153     {"ADC Ch8", NULL, "AIN8"},
0154     {"SDTO", NULL, "ADC Ch8"},
0155 };
0156 
0157 static const struct snd_soc_dapm_route ak5552_intercon[] = {
0158     {"ADC Ch1", NULL, "AIN1"},
0159     {"SDTO", NULL, "ADC Ch1"},
0160 
0161     {"ADC Ch2", NULL, "AIN2"},
0162     {"SDTO", NULL, "ADC Ch2"},
0163 };
0164 
0165 static int ak5558_set_mcki(struct snd_soc_component *component)
0166 {
0167     return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
0168                    AK5558_CKS_AUTO);
0169 }
0170 
0171 static int ak5558_hw_params(struct snd_pcm_substream *substream,
0172                 struct snd_pcm_hw_params *params,
0173                 struct snd_soc_dai *dai)
0174 {
0175     struct snd_soc_component *component = dai->component;
0176     struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
0177     u8 bits;
0178     int pcm_width = max(params_physical_width(params), ak5558->slot_width);
0179 
0180     switch (pcm_width) {
0181     case 16:
0182         bits = AK5558_DIF_24BIT_MODE;
0183         break;
0184     case 32:
0185         bits = AK5558_DIF_32BIT_MODE;
0186         break;
0187     default:
0188         return -EINVAL;
0189     }
0190 
0191     snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
0192 
0193     return 0;
0194 }
0195 
0196 static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0197 {
0198     struct snd_soc_component *component = dai->component;
0199     u8 format;
0200 
0201     switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0202     case SND_SOC_DAIFMT_CBC_CFC:
0203         break;
0204     case SND_SOC_DAIFMT_CBP_CFP:
0205         break;
0206     case SND_SOC_DAIFMT_CBC_CFP:
0207     case SND_SOC_DAIFMT_CBP_CFC:
0208     default:
0209         dev_err(dai->dev, "Clock mode unsupported");
0210         return -EINVAL;
0211     }
0212 
0213     /* set master/slave audio interface */
0214     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0215     case SND_SOC_DAIFMT_I2S:
0216         format = AK5558_DIF_I2S_MODE;
0217         break;
0218     case SND_SOC_DAIFMT_LEFT_J:
0219         format = AK5558_DIF_MSB_MODE;
0220         break;
0221     case SND_SOC_DAIFMT_DSP_B:
0222         format = AK5558_DIF_MSB_MODE;
0223         break;
0224     default:
0225         return -EINVAL;
0226     }
0227 
0228     snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
0229 
0230     return 0;
0231 }
0232 
0233 static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
0234                    unsigned int rx_mask, int slots,
0235                    int slot_width)
0236 {
0237     struct snd_soc_component *component = dai->component;
0238     struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
0239     int tdm_mode;
0240 
0241     ak5558->slots = slots;
0242     ak5558->slot_width = slot_width;
0243 
0244     switch (slots * slot_width) {
0245     case 128:
0246         tdm_mode = AK5558_MODE_TDM128;
0247         break;
0248     case 256:
0249         tdm_mode = AK5558_MODE_TDM256;
0250         break;
0251     case 512:
0252         tdm_mode = AK5558_MODE_TDM512;
0253         break;
0254     default:
0255         tdm_mode = AK5558_MODE_NORMAL;
0256         break;
0257     }
0258 
0259     snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
0260                 tdm_mode);
0261     return 0;
0262 }
0263 
0264 #define AK5558_FORMATS  (SNDRV_PCM_FMTBIT_S16_LE |\
0265              SNDRV_PCM_FMTBIT_S24_LE |\
0266              SNDRV_PCM_FMTBIT_S32_LE)
0267 
0268 static const unsigned int ak5558_rates[] = {
0269     8000, 11025,  16000, 22050,
0270     32000, 44100, 48000, 88200,
0271     96000, 176400, 192000, 352800,
0272     384000, 705600, 768000, 1411200,
0273     2822400,
0274 };
0275 
0276 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
0277     .count = ARRAY_SIZE(ak5558_rates),
0278     .list = ak5558_rates,
0279 };
0280 
0281 static int ak5558_startup(struct snd_pcm_substream *substream,
0282               struct snd_soc_dai *dai)
0283 {
0284     return snd_pcm_hw_constraint_list(substream->runtime, 0,
0285                       SNDRV_PCM_HW_PARAM_RATE,
0286                       &ak5558_rate_constraints);
0287 }
0288 
0289 static const struct snd_soc_dai_ops ak5558_dai_ops = {
0290     .startup        = ak5558_startup,
0291     .hw_params  = ak5558_hw_params,
0292 
0293     .set_fmt    = ak5558_set_dai_fmt,
0294     .set_tdm_slot   = ak5558_set_tdm_slot,
0295 };
0296 
0297 static struct snd_soc_dai_driver ak5558_dai = {
0298     .name = "ak5558-aif",
0299     .capture = {
0300         .stream_name = "Capture",
0301         .channels_min = 1,
0302         .channels_max = 8,
0303         .rates = SNDRV_PCM_RATE_KNOT,
0304         .formats = AK5558_FORMATS,
0305     },
0306     .ops = &ak5558_dai_ops,
0307 };
0308 
0309 static struct snd_soc_dai_driver ak5552_dai = {
0310     .name = "ak5552-aif",
0311     .capture = {
0312         .stream_name = "Capture",
0313         .channels_min = 1,
0314         .channels_max = 2,
0315         .rates = SNDRV_PCM_RATE_KNOT,
0316         .formats = AK5558_FORMATS,
0317     },
0318     .ops = &ak5558_dai_ops,
0319 };
0320 
0321 static void ak5558_reset(struct ak5558_priv *ak5558, bool active)
0322 {
0323     if (!ak5558->reset_gpiod)
0324         return;
0325 
0326     gpiod_set_value_cansleep(ak5558->reset_gpiod, active);
0327     usleep_range(1000, 2000);
0328 }
0329 
0330 static int ak5558_probe(struct snd_soc_component *component)
0331 {
0332     struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
0333 
0334     ak5558_reset(ak5558, false);
0335     return ak5558_set_mcki(component);
0336 }
0337 
0338 static void ak5558_remove(struct snd_soc_component *component)
0339 {
0340     struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
0341 
0342     ak5558_reset(ak5558, true);
0343 }
0344 
0345 static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
0346 {
0347     struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
0348 
0349     regcache_cache_only(ak5558->regmap, true);
0350     ak5558_reset(ak5558, true);
0351 
0352     regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies),
0353                    ak5558->supplies);
0354     return 0;
0355 }
0356 
0357 static int __maybe_unused ak5558_runtime_resume(struct device *dev)
0358 {
0359     struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
0360     int ret;
0361 
0362     ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies),
0363                     ak5558->supplies);
0364     if (ret != 0) {
0365         dev_err(dev, "Failed to enable supplies: %d\n", ret);
0366         return ret;
0367     }
0368 
0369     ak5558_reset(ak5558, true);
0370     ak5558_reset(ak5558, false);
0371 
0372     regcache_cache_only(ak5558->regmap, false);
0373     regcache_mark_dirty(ak5558->regmap);
0374 
0375     return regcache_sync(ak5558->regmap);
0376 }
0377 
0378 static const struct dev_pm_ops ak5558_pm = {
0379     SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
0380     SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
0381                 pm_runtime_force_resume)
0382 };
0383 
0384 static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
0385     .probe          = ak5558_probe,
0386     .remove         = ak5558_remove,
0387     .controls       = ak5558_snd_controls,
0388     .num_controls       = ARRAY_SIZE(ak5558_snd_controls),
0389     .dapm_widgets       = ak5558_dapm_widgets,
0390     .num_dapm_widgets   = ARRAY_SIZE(ak5558_dapm_widgets),
0391     .dapm_routes        = ak5558_intercon,
0392     .num_dapm_routes    = ARRAY_SIZE(ak5558_intercon),
0393     .idle_bias_on       = 1,
0394     .use_pmdown_time    = 1,
0395     .endianness     = 1,
0396 };
0397 
0398 static const struct snd_soc_component_driver soc_codec_dev_ak5552 = {
0399     .probe          = ak5558_probe,
0400     .remove         = ak5558_remove,
0401     .controls       = ak5552_snd_controls,
0402     .num_controls       = ARRAY_SIZE(ak5552_snd_controls),
0403     .dapm_widgets       = ak5552_dapm_widgets,
0404     .num_dapm_widgets   = ARRAY_SIZE(ak5552_dapm_widgets),
0405     .dapm_routes        = ak5552_intercon,
0406     .num_dapm_routes    = ARRAY_SIZE(ak5552_intercon),
0407     .idle_bias_on       = 1,
0408     .use_pmdown_time    = 1,
0409     .endianness     = 1,
0410 };
0411 
0412 static const struct regmap_config ak5558_regmap = {
0413     .reg_bits = 8,
0414     .val_bits = 8,
0415 
0416     .max_register = AK5558_05_DSD,
0417     .reg_defaults = ak5558_reg,
0418     .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
0419     .cache_type = REGCACHE_RBTREE,
0420 };
0421 
0422 static int ak5558_i2c_probe(struct i2c_client *i2c)
0423 {
0424     struct ak5558_priv *ak5558;
0425     int ret = 0;
0426     int dev_id;
0427     int i;
0428 
0429     ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
0430     if (!ak5558)
0431         return -ENOMEM;
0432 
0433     ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
0434     if (IS_ERR(ak5558->regmap))
0435         return PTR_ERR(ak5558->regmap);
0436 
0437     i2c_set_clientdata(i2c, ak5558);
0438     ak5558->i2c = i2c;
0439 
0440     ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
0441                               GPIOD_OUT_LOW);
0442     if (IS_ERR(ak5558->reset_gpiod))
0443         return PTR_ERR(ak5558->reset_gpiod);
0444 
0445     for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++)
0446         ak5558->supplies[i].supply = ak5558_supply_names[i];
0447 
0448     ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies),
0449                       ak5558->supplies);
0450     if (ret != 0) {
0451         dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
0452         return ret;
0453     }
0454 
0455     dev_id = (uintptr_t)of_device_get_match_data(&i2c->dev);
0456     switch (dev_id) {
0457     case AK5552:
0458         ret = devm_snd_soc_register_component(&i2c->dev,
0459                               &soc_codec_dev_ak5552,
0460                               &ak5552_dai, 1);
0461         break;
0462     case AK5558:
0463         ret = devm_snd_soc_register_component(&i2c->dev,
0464                               &soc_codec_dev_ak5558,
0465                               &ak5558_dai, 1);
0466         break;
0467     default:
0468         dev_err(&i2c->dev, "unexpected device type\n");
0469         return -EINVAL;
0470     }
0471     if (ret < 0) {
0472         dev_err(&i2c->dev, "failed to register component: %d\n", ret);
0473         return ret;
0474     }
0475 
0476     pm_runtime_enable(&i2c->dev);
0477     regcache_cache_only(ak5558->regmap, true);
0478 
0479     return 0;
0480 }
0481 
0482 static int ak5558_i2c_remove(struct i2c_client *i2c)
0483 {
0484     pm_runtime_disable(&i2c->dev);
0485 
0486     return 0;
0487 }
0488 
0489 static const struct of_device_id ak5558_i2c_dt_ids[] __maybe_unused = {
0490     { .compatible = "asahi-kasei,ak5558", .data = (void *) AK5558 },
0491     { .compatible = "asahi-kasei,ak5552", .data = (void *) AK5552 },
0492     { }
0493 };
0494 MODULE_DEVICE_TABLE(of, ak5558_i2c_dt_ids);
0495 
0496 static struct i2c_driver ak5558_i2c_driver = {
0497     .driver = {
0498         .name = "ak5558",
0499         .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
0500         .pm = &ak5558_pm,
0501     },
0502     .probe_new = ak5558_i2c_probe,
0503     .remove = ak5558_i2c_remove,
0504 };
0505 
0506 module_i2c_driver(ak5558_i2c_driver);
0507 
0508 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
0509 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
0510 MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
0511 MODULE_LICENSE("GPL v2");