Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * max9850.c  --  codec driver for max9850
0004  *
0005  * Copyright (C) 2011 taskit GmbH
0006  *
0007  * Author: Christian Glindkamp <christian.glindkamp@taskit.de>
0008  *
0009  * Initial development of this code was funded by
0010  * MICRONIC Computer Systeme GmbH, https://www.mcsberlin.de/
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/i2c.h>
0016 #include <linux/regmap.h>
0017 #include <linux/slab.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/tlv.h>
0022 
0023 #include "max9850.h"
0024 
0025 struct max9850_priv {
0026     struct regmap *regmap;
0027     unsigned int sysclk;
0028 };
0029 
0030 /* these registers are not used at the moment but provided for the sake of
0031  * completeness */
0032 static bool max9850_volatile_register(struct device *dev, unsigned int reg)
0033 {
0034     switch (reg) {
0035     case MAX9850_STATUSA:
0036     case MAX9850_STATUSB:
0037         return true;
0038     default:
0039         return false;
0040     }
0041 }
0042 
0043 static const struct regmap_config max9850_regmap = {
0044     .reg_bits = 8,
0045     .val_bits = 8,
0046 
0047     .max_register = MAX9850_DIGITAL_AUDIO,
0048     .volatile_reg = max9850_volatile_register,
0049     .cache_type = REGCACHE_RBTREE,
0050 };
0051 
0052 static const DECLARE_TLV_DB_RANGE(max9850_tlv,
0053     0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
0054     0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
0055     0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
0056     0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
0057 );
0058 
0059 static const struct snd_kcontrol_new max9850_controls[] = {
0060 SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
0061 SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
0062 SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
0063 };
0064 
0065 static const struct snd_kcontrol_new max9850_mixer_controls[] = {
0066     SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
0067 };
0068 
0069 static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
0070 SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
0071 SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
0072 SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
0073 SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
0074 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
0075         &max9850_mixer_controls[0],
0076         ARRAY_SIZE(max9850_mixer_controls)),
0077 SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
0078 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
0079 SND_SOC_DAPM_OUTPUT("OUTL"),
0080 SND_SOC_DAPM_OUTPUT("HPL"),
0081 SND_SOC_DAPM_OUTPUT("OUTR"),
0082 SND_SOC_DAPM_OUTPUT("HPR"),
0083 SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
0084 SND_SOC_DAPM_INPUT("INL"),
0085 SND_SOC_DAPM_INPUT("INR"),
0086 };
0087 
0088 static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
0089     /* output mixer */
0090     {"Output Mixer", NULL, "DAC"},
0091     {"Output Mixer", "Line In Switch", "Line Input"},
0092 
0093     /* outputs */
0094     {"Headphone Output", NULL, "Output Mixer"},
0095     {"HPL", NULL, "Headphone Output"},
0096     {"HPR", NULL, "Headphone Output"},
0097     {"OUTL", NULL, "Output Mixer"},
0098     {"OUTR", NULL, "Output Mixer"},
0099 
0100     /* inputs */
0101     {"Line Input", NULL, "INL"},
0102     {"Line Input", NULL, "INR"},
0103 
0104     /* supplies */
0105     {"Output Mixer", NULL, "Charge Pump 1"},
0106     {"Output Mixer", NULL, "Charge Pump 2"},
0107     {"Output Mixer", NULL, "SHDN"},
0108     {"DAC", NULL, "MCLK"},
0109 };
0110 
0111 static int max9850_hw_params(struct snd_pcm_substream *substream,
0112                  struct snd_pcm_hw_params *params,
0113                  struct snd_soc_dai *dai)
0114 {
0115     struct snd_soc_component *component = dai->component;
0116     struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
0117     u64 lrclk_div;
0118     u8 sf, da;
0119 
0120     if (!max9850->sysclk)
0121         return -EINVAL;
0122 
0123     /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
0124     sf = (snd_soc_component_read(component, MAX9850_CLOCK) >> 2) + 1;
0125     lrclk_div = (1 << 22);
0126     lrclk_div *= params_rate(params);
0127     lrclk_div *= sf;
0128     do_div(lrclk_div, max9850->sysclk);
0129 
0130     snd_soc_component_write(component, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f);
0131     snd_soc_component_write(component, MAX9850_LRCLK_LSB, lrclk_div & 0xff);
0132 
0133     switch (params_width(params)) {
0134     case 16:
0135         da = 0;
0136         break;
0137     case 20:
0138         da = 0x2;
0139         break;
0140     case 24:
0141         da = 0x3;
0142         break;
0143     default:
0144         return -EINVAL;
0145     }
0146     snd_soc_component_update_bits(component, MAX9850_DIGITAL_AUDIO, 0x3, da);
0147 
0148     return 0;
0149 }
0150 
0151 static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0152         int clk_id, unsigned int freq, int dir)
0153 {
0154     struct snd_soc_component *component = codec_dai->component;
0155     struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
0156 
0157     /* calculate mclk -> iclk divider */
0158     if (freq <= 13000000)
0159         snd_soc_component_write(component, MAX9850_CLOCK, 0x0);
0160     else if (freq <= 26000000)
0161         snd_soc_component_write(component, MAX9850_CLOCK, 0x4);
0162     else if (freq <= 40000000)
0163         snd_soc_component_write(component, MAX9850_CLOCK, 0x8);
0164     else
0165         return -EINVAL;
0166 
0167     max9850->sysclk = freq;
0168     return 0;
0169 }
0170 
0171 static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
0172 {
0173     struct snd_soc_component *component = codec_dai->component;
0174     u8 da = 0;
0175 
0176     /* set clock provider for audio interface */
0177     switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
0178     case SND_SOC_DAIFMT_CBP_CFP:
0179         da |= MAX9850_MASTER;
0180         break;
0181     case SND_SOC_DAIFMT_CBC_CFC:
0182         break;
0183     default:
0184         return -EINVAL;
0185     }
0186 
0187     /* interface format */
0188     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0189     case SND_SOC_DAIFMT_I2S:
0190         da |= MAX9850_DLY;
0191         break;
0192     case SND_SOC_DAIFMT_RIGHT_J:
0193         da |= MAX9850_RTJ;
0194         break;
0195     case SND_SOC_DAIFMT_LEFT_J:
0196         break;
0197     default:
0198         return -EINVAL;
0199     }
0200 
0201     /* clock inversion */
0202     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0203     case SND_SOC_DAIFMT_NB_NF:
0204         break;
0205     case SND_SOC_DAIFMT_IB_IF:
0206         da |= MAX9850_BCINV | MAX9850_INV;
0207         break;
0208     case SND_SOC_DAIFMT_IB_NF:
0209         da |= MAX9850_BCINV;
0210         break;
0211     case SND_SOC_DAIFMT_NB_IF:
0212         da |= MAX9850_INV;
0213         break;
0214     default:
0215         return -EINVAL;
0216     }
0217 
0218     /* set da */
0219     snd_soc_component_write(component, MAX9850_DIGITAL_AUDIO, da);
0220 
0221     return 0;
0222 }
0223 
0224 static int max9850_set_bias_level(struct snd_soc_component *component,
0225                   enum snd_soc_bias_level level)
0226 {
0227     struct max9850_priv *max9850 = snd_soc_component_get_drvdata(component);
0228     int ret;
0229 
0230     switch (level) {
0231     case SND_SOC_BIAS_ON:
0232         break;
0233     case SND_SOC_BIAS_PREPARE:
0234         break;
0235     case SND_SOC_BIAS_STANDBY:
0236         if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
0237             ret = regcache_sync(max9850->regmap);
0238             if (ret) {
0239                 dev_err(component->dev,
0240                     "Failed to sync cache: %d\n", ret);
0241                 return ret;
0242             }
0243         }
0244         break;
0245     case SND_SOC_BIAS_OFF:
0246         break;
0247     }
0248     return 0;
0249 }
0250 
0251 #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
0252 
0253 #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
0254     SNDRV_PCM_FMTBIT_S24_LE)
0255 
0256 static const struct snd_soc_dai_ops max9850_dai_ops = {
0257     .hw_params  = max9850_hw_params,
0258     .set_sysclk = max9850_set_dai_sysclk,
0259     .set_fmt    = max9850_set_dai_fmt,
0260 };
0261 
0262 static struct snd_soc_dai_driver max9850_dai = {
0263     .name = "max9850-hifi",
0264     .playback = {
0265         .stream_name = "Playback",
0266         .channels_min = 1,
0267         .channels_max = 2,
0268         .rates = MAX9850_RATES,
0269         .formats = MAX9850_FORMATS
0270     },
0271     .ops = &max9850_dai_ops,
0272 };
0273 
0274 static int max9850_probe(struct snd_soc_component *component)
0275 {
0276     /* enable zero-detect */
0277     snd_soc_component_update_bits(component, MAX9850_GENERAL_PURPOSE, 1, 1);
0278     /* enable slew-rate control */
0279     snd_soc_component_update_bits(component, MAX9850_VOLUME, 0x40, 0x40);
0280     /* set slew-rate 125ms */
0281     snd_soc_component_update_bits(component, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
0282 
0283     return 0;
0284 }
0285 
0286 static const struct snd_soc_component_driver soc_component_dev_max9850 = {
0287     .probe          = max9850_probe,
0288     .set_bias_level     = max9850_set_bias_level,
0289     .controls       = max9850_controls,
0290     .num_controls       = ARRAY_SIZE(max9850_controls),
0291     .dapm_widgets       = max9850_dapm_widgets,
0292     .num_dapm_widgets   = ARRAY_SIZE(max9850_dapm_widgets),
0293     .dapm_routes        = max9850_dapm_routes,
0294     .num_dapm_routes    = ARRAY_SIZE(max9850_dapm_routes),
0295     .suspend_bias_off   = 1,
0296     .idle_bias_on       = 1,
0297     .use_pmdown_time    = 1,
0298     .endianness     = 1,
0299 };
0300 
0301 static int max9850_i2c_probe(struct i2c_client *i2c)
0302 {
0303     struct max9850_priv *max9850;
0304     int ret;
0305 
0306     max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
0307                    GFP_KERNEL);
0308     if (max9850 == NULL)
0309         return -ENOMEM;
0310 
0311     max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
0312     if (IS_ERR(max9850->regmap))
0313         return PTR_ERR(max9850->regmap);
0314 
0315     i2c_set_clientdata(i2c, max9850);
0316 
0317     ret = devm_snd_soc_register_component(&i2c->dev,
0318             &soc_component_dev_max9850, &max9850_dai, 1);
0319     return ret;
0320 }
0321 
0322 static const struct i2c_device_id max9850_i2c_id[] = {
0323     { "max9850", 0 },
0324     { }
0325 };
0326 MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
0327 
0328 static struct i2c_driver max9850_i2c_driver = {
0329     .driver = {
0330         .name = "max9850",
0331     },
0332     .probe_new = max9850_i2c_probe,
0333     .id_table = max9850_i2c_id,
0334 };
0335 
0336 module_i2c_driver(max9850_i2c_driver);
0337 
0338 MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
0339 MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
0340 MODULE_LICENSE("GPL");