1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 295169d08SMarek Belisko /* 395169d08SMarek Belisko * PCM1681 ASoC codec driver 495169d08SMarek Belisko * 595169d08SMarek Belisko * Copyright (c) StreamUnlimited GmbH 2013 695169d08SMarek Belisko * Marek Belisko <marek.belisko@streamunlimited.com> 795169d08SMarek Belisko */ 895169d08SMarek Belisko 995169d08SMarek Belisko #include <linux/module.h> 1095169d08SMarek Belisko #include <linux/slab.h> 1195169d08SMarek Belisko #include <linux/delay.h> 1295169d08SMarek Belisko #include <linux/gpio.h> 1395169d08SMarek Belisko #include <linux/i2c.h> 1495169d08SMarek Belisko #include <linux/regmap.h> 15193a4716SSachin Kamat #include <linux/of.h> 1695169d08SMarek Belisko #include <linux/of_device.h> 1795169d08SMarek Belisko #include <linux/of_gpio.h> 1895169d08SMarek Belisko #include <sound/pcm.h> 1995169d08SMarek Belisko #include <sound/pcm_params.h> 2095169d08SMarek Belisko #include <sound/soc.h> 2195169d08SMarek Belisko #include <sound/tlv.h> 2295169d08SMarek Belisko 2395169d08SMarek Belisko #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ 2495169d08SMarek Belisko SNDRV_PCM_FMTBIT_S24_LE) 2595169d08SMarek Belisko 2695169d08SMarek Belisko #define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \ 2795169d08SMarek Belisko SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ 2895169d08SMarek Belisko SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ 2995169d08SMarek Belisko SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000) 3095169d08SMarek Belisko 3195169d08SMarek Belisko #define PCM1681_SOFT_MUTE_ALL 0xff 3295169d08SMarek Belisko #define PCM1681_DEEMPH_RATE_MASK 0x18 3395169d08SMarek Belisko #define PCM1681_DEEMPH_MASK 0x01 3495169d08SMarek Belisko 3595169d08SMarek Belisko #define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */ 3695169d08SMarek Belisko #define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */ 3795169d08SMarek Belisko #define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */ 3895169d08SMarek Belisko #define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */ 3995169d08SMarek Belisko #define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */ 4095169d08SMarek Belisko #define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */ 4195169d08SMarek Belisko 4295169d08SMarek Belisko static const struct reg_default pcm1681_reg_defaults[] = { 4395169d08SMarek Belisko { 0x01, 0xff }, 4495169d08SMarek Belisko { 0x02, 0xff }, 4595169d08SMarek Belisko { 0x03, 0xff }, 4695169d08SMarek Belisko { 0x04, 0xff }, 4795169d08SMarek Belisko { 0x05, 0xff }, 4895169d08SMarek Belisko { 0x06, 0xff }, 4995169d08SMarek Belisko { 0x07, 0x00 }, 5095169d08SMarek Belisko { 0x08, 0x00 }, 5195169d08SMarek Belisko { 0x09, 0x06 }, 5295169d08SMarek Belisko { 0x0A, 0x00 }, 5395169d08SMarek Belisko { 0x0B, 0xff }, 5495169d08SMarek Belisko { 0x0C, 0x0f }, 5595169d08SMarek Belisko { 0x0D, 0x00 }, 5695169d08SMarek Belisko { 0x10, 0xff }, 5795169d08SMarek Belisko { 0x11, 0xff }, 5895169d08SMarek Belisko { 0x12, 0x00 }, 5995169d08SMarek Belisko { 0x13, 0x00 }, 6095169d08SMarek Belisko }; 6195169d08SMarek Belisko 6295169d08SMarek Belisko static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg) 6395169d08SMarek Belisko { 6495169d08SMarek Belisko return !((reg == 0x00) || (reg == 0x0f)); 6595169d08SMarek Belisko } 6695169d08SMarek Belisko 67a074ae0eSArnd Bergmann static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg) 6895169d08SMarek Belisko { 6995169d08SMarek Belisko return pcm1681_accessible_reg(dev, reg) && 7095169d08SMarek Belisko (reg != PCM1681_ZERO_DETECT_STATUS); 7195169d08SMarek Belisko } 7295169d08SMarek Belisko 7395169d08SMarek Belisko struct pcm1681_private { 7495169d08SMarek Belisko struct regmap *regmap; 7595169d08SMarek Belisko unsigned int format; 7695169d08SMarek Belisko /* Current deemphasis status */ 7795169d08SMarek Belisko unsigned int deemph; 7895169d08SMarek Belisko /* Current rate for deemphasis control */ 7995169d08SMarek Belisko unsigned int rate; 8095169d08SMarek Belisko }; 8195169d08SMarek Belisko 8295169d08SMarek Belisko static const int pcm1681_deemph[] = { 44100, 48000, 32000 }; 8395169d08SMarek Belisko 847dbbaa51SKuninori Morimoto static int pcm1681_set_deemph(struct snd_soc_component *component) 8595169d08SMarek Belisko { 867dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 87f10280d5SPierre-Louis Bossart int i, val = -1, enable = 0; 8895169d08SMarek Belisko 8948f403beSAxel Lin if (priv->deemph) { 9048f403beSAxel Lin for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) { 9148f403beSAxel Lin if (pcm1681_deemph[i] == priv->rate) { 9295169d08SMarek Belisko val = i; 9348f403beSAxel Lin break; 9448f403beSAxel Lin } 9548f403beSAxel Lin } 9648f403beSAxel Lin } 9795169d08SMarek Belisko 9895169d08SMarek Belisko if (val != -1) { 9995169d08SMarek Belisko regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 100fa8173a3SAxel Lin PCM1681_DEEMPH_RATE_MASK, val << 3); 10195169d08SMarek Belisko enable = 1; 10248f403beSAxel Lin } else { 10395169d08SMarek Belisko enable = 0; 10448f403beSAxel Lin } 10595169d08SMarek Belisko 10695169d08SMarek Belisko /* enable/disable deemphasis functionality */ 10795169d08SMarek Belisko return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 10895169d08SMarek Belisko PCM1681_DEEMPH_MASK, enable); 10995169d08SMarek Belisko } 11095169d08SMarek Belisko 11195169d08SMarek Belisko static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol, 11295169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol) 11395169d08SMarek Belisko { 1147dbbaa51SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 1157dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 11695169d08SMarek Belisko 117d7f58db4STakashi Iwai ucontrol->value.integer.value[0] = priv->deemph; 11895169d08SMarek Belisko 11995169d08SMarek Belisko return 0; 12095169d08SMarek Belisko } 12195169d08SMarek Belisko 12295169d08SMarek Belisko static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol, 12395169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol) 12495169d08SMarek Belisko { 1257dbbaa51SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 1267dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 12795169d08SMarek Belisko 128d7f58db4STakashi Iwai priv->deemph = ucontrol->value.integer.value[0]; 12995169d08SMarek Belisko 1307dbbaa51SKuninori Morimoto return pcm1681_set_deemph(component); 13195169d08SMarek Belisko } 13295169d08SMarek Belisko 13395169d08SMarek Belisko static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai, 13495169d08SMarek Belisko unsigned int format) 13595169d08SMarek Belisko { 1367dbbaa51SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 1377dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 13895169d08SMarek Belisko 1391900cb53SMark Brown /* The PCM1681 can only be consumer to all clocks */ 1401900cb53SMark Brown if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) { 1417dbbaa51SKuninori Morimoto dev_err(component->dev, "Invalid clocking mode\n"); 14295169d08SMarek Belisko return -EINVAL; 14395169d08SMarek Belisko } 14495169d08SMarek Belisko 14595169d08SMarek Belisko priv->format = format; 14695169d08SMarek Belisko 14795169d08SMarek Belisko return 0; 14895169d08SMarek Belisko } 14995169d08SMarek Belisko 1501eb2c43dSKuninori Morimoto static int pcm1681_mute(struct snd_soc_dai *dai, int mute, int direction) 15195169d08SMarek Belisko { 1527dbbaa51SKuninori Morimoto struct snd_soc_component *component = dai->component; 1537dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 15495169d08SMarek Belisko int val; 15595169d08SMarek Belisko 15695169d08SMarek Belisko if (mute) 15795169d08SMarek Belisko val = PCM1681_SOFT_MUTE_ALL; 15895169d08SMarek Belisko else 15995169d08SMarek Belisko val = 0; 16095169d08SMarek Belisko 16195169d08SMarek Belisko return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val); 16295169d08SMarek Belisko } 16395169d08SMarek Belisko 16495169d08SMarek Belisko static int pcm1681_hw_params(struct snd_pcm_substream *substream, 16595169d08SMarek Belisko struct snd_pcm_hw_params *params, 16695169d08SMarek Belisko struct snd_soc_dai *dai) 16795169d08SMarek Belisko { 1687dbbaa51SKuninori Morimoto struct snd_soc_component *component = dai->component; 1697dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component); 17095169d08SMarek Belisko int val = 0, ret; 17195169d08SMarek Belisko 17295169d08SMarek Belisko priv->rate = params_rate(params); 17395169d08SMarek Belisko 17495169d08SMarek Belisko switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { 17595169d08SMarek Belisko case SND_SOC_DAIFMT_RIGHT_J: 17650a68fb4SMark Brown switch (params_width(params)) { 17750a68fb4SMark Brown case 24: 17850a68fb4SMark Brown val = 0; 17950a68fb4SMark Brown break; 18050a68fb4SMark Brown case 16: 18150a68fb4SMark Brown val = 3; 18250a68fb4SMark Brown break; 18350a68fb4SMark Brown default: 18450a68fb4SMark Brown return -EINVAL; 18550a68fb4SMark Brown } 18695169d08SMarek Belisko break; 18795169d08SMarek Belisko case SND_SOC_DAIFMT_I2S: 18895169d08SMarek Belisko val = 0x04; 18995169d08SMarek Belisko break; 19095169d08SMarek Belisko case SND_SOC_DAIFMT_LEFT_J: 19195169d08SMarek Belisko val = 0x05; 19295169d08SMarek Belisko break; 19395169d08SMarek Belisko default: 1947dbbaa51SKuninori Morimoto dev_err(component->dev, "Invalid DAI format\n"); 19595169d08SMarek Belisko return -EINVAL; 19695169d08SMarek Belisko } 19795169d08SMarek Belisko 19895169d08SMarek Belisko ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val); 19995169d08SMarek Belisko if (ret < 0) 20095169d08SMarek Belisko return ret; 20195169d08SMarek Belisko 2027dbbaa51SKuninori Morimoto return pcm1681_set_deemph(component); 20395169d08SMarek Belisko } 20495169d08SMarek Belisko 20595169d08SMarek Belisko static const struct snd_soc_dai_ops pcm1681_dai_ops = { 20695169d08SMarek Belisko .set_fmt = pcm1681_set_dai_fmt, 20795169d08SMarek Belisko .hw_params = pcm1681_hw_params, 2081eb2c43dSKuninori Morimoto .mute_stream = pcm1681_mute, 2091eb2c43dSKuninori Morimoto .no_capture_mute = 1, 21095169d08SMarek Belisko }; 21195169d08SMarek Belisko 212b9281f99SMark Brown static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = { 213b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT1"), 214b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT2"), 215b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT3"), 216b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT4"), 217b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT5"), 218b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT6"), 219b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT7"), 220b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT8"), 221b9281f99SMark Brown }; 222b9281f99SMark Brown 223b9281f99SMark Brown static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = { 224b9281f99SMark Brown { "VOUT1", NULL, "Playback" }, 225b9281f99SMark Brown { "VOUT2", NULL, "Playback" }, 226b9281f99SMark Brown { "VOUT3", NULL, "Playback" }, 227b9281f99SMark Brown { "VOUT4", NULL, "Playback" }, 228b9281f99SMark Brown { "VOUT5", NULL, "Playback" }, 229b9281f99SMark Brown { "VOUT6", NULL, "Playback" }, 230b9281f99SMark Brown { "VOUT7", NULL, "Playback" }, 231b9281f99SMark Brown { "VOUT8", NULL, "Playback" }, 232b9281f99SMark Brown }; 233b9281f99SMark Brown 23495169d08SMarek Belisko static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1); 23595169d08SMarek Belisko 23695169d08SMarek Belisko static const struct snd_kcontrol_new pcm1681_controls[] = { 23795169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume", 23895169d08SMarek Belisko PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0, 23995169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24095169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume", 24195169d08SMarek Belisko PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0, 24295169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24395169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume", 24495169d08SMarek Belisko PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0, 24595169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24695169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume", 24795169d08SMarek Belisko PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0, 24895169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24995169d08SMarek Belisko SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, 25095169d08SMarek Belisko pcm1681_get_deemph, pcm1681_put_deemph), 25195169d08SMarek Belisko }; 25295169d08SMarek Belisko 25316695971SMark Brown static struct snd_soc_dai_driver pcm1681_dai = { 25495169d08SMarek Belisko .name = "pcm1681-hifi", 25595169d08SMarek Belisko .playback = { 25695169d08SMarek Belisko .stream_name = "Playback", 25795169d08SMarek Belisko .channels_min = 2, 25895169d08SMarek Belisko .channels_max = 8, 25995169d08SMarek Belisko .rates = PCM1681_PCM_RATES, 26095169d08SMarek Belisko .formats = PCM1681_PCM_FORMATS, 26195169d08SMarek Belisko }, 26295169d08SMarek Belisko .ops = &pcm1681_dai_ops, 26395169d08SMarek Belisko }; 26495169d08SMarek Belisko 26595169d08SMarek Belisko #ifdef CONFIG_OF 26695169d08SMarek Belisko static const struct of_device_id pcm1681_dt_ids[] = { 26795169d08SMarek Belisko { .compatible = "ti,pcm1681", }, 26895169d08SMarek Belisko { } 26995169d08SMarek Belisko }; 27095169d08SMarek Belisko MODULE_DEVICE_TABLE(of, pcm1681_dt_ids); 27195169d08SMarek Belisko #endif 27295169d08SMarek Belisko 27395169d08SMarek Belisko static const struct regmap_config pcm1681_regmap = { 27495169d08SMarek Belisko .reg_bits = 8, 27595169d08SMarek Belisko .val_bits = 8, 27664256ac6SAxel Lin .max_register = 0x13, 27795169d08SMarek Belisko .reg_defaults = pcm1681_reg_defaults, 27895169d08SMarek Belisko .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults), 27995169d08SMarek Belisko .writeable_reg = pcm1681_writeable_reg, 28095169d08SMarek Belisko .readable_reg = pcm1681_accessible_reg, 28195169d08SMarek Belisko }; 28295169d08SMarek Belisko 2837dbbaa51SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_pcm1681 = { 28495169d08SMarek Belisko .controls = pcm1681_controls, 28595169d08SMarek Belisko .num_controls = ARRAY_SIZE(pcm1681_controls), 286b9281f99SMark Brown .dapm_widgets = pcm1681_dapm_widgets, 287b9281f99SMark Brown .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets), 288b9281f99SMark Brown .dapm_routes = pcm1681_dapm_routes, 289b9281f99SMark Brown .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes), 2907dbbaa51SKuninori Morimoto .idle_bias_on = 1, 2917dbbaa51SKuninori Morimoto .use_pmdown_time = 1, 2927dbbaa51SKuninori Morimoto .endianness = 1, 2937dbbaa51SKuninori Morimoto .non_legacy_dai_naming = 1, 29495169d08SMarek Belisko }; 29595169d08SMarek Belisko 29695169d08SMarek Belisko static const struct i2c_device_id pcm1681_i2c_id[] = { 29795169d08SMarek Belisko {"pcm1681", 0}, 29895169d08SMarek Belisko {} 29995169d08SMarek Belisko }; 30095169d08SMarek Belisko MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id); 30195169d08SMarek Belisko 302*7f7d7214SStephen Kitt static int pcm1681_i2c_probe(struct i2c_client *client) 30395169d08SMarek Belisko { 30495169d08SMarek Belisko int ret; 30595169d08SMarek Belisko struct pcm1681_private *priv; 30695169d08SMarek Belisko 30795169d08SMarek Belisko priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); 30895169d08SMarek Belisko if (!priv) 30995169d08SMarek Belisko return -ENOMEM; 31095169d08SMarek Belisko 31195169d08SMarek Belisko priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap); 31295169d08SMarek Belisko if (IS_ERR(priv->regmap)) { 31395169d08SMarek Belisko ret = PTR_ERR(priv->regmap); 31495169d08SMarek Belisko dev_err(&client->dev, "Failed to create regmap: %d\n", ret); 31595169d08SMarek Belisko return ret; 31695169d08SMarek Belisko } 31795169d08SMarek Belisko 31895169d08SMarek Belisko i2c_set_clientdata(client, priv); 31995169d08SMarek Belisko 3207dbbaa51SKuninori Morimoto return devm_snd_soc_register_component(&client->dev, 3217dbbaa51SKuninori Morimoto &soc_component_dev_pcm1681, 32295169d08SMarek Belisko &pcm1681_dai, 1); 32395169d08SMarek Belisko } 32495169d08SMarek Belisko 32595169d08SMarek Belisko static struct i2c_driver pcm1681_i2c_driver = { 32695169d08SMarek Belisko .driver = { 32795169d08SMarek Belisko .name = "pcm1681", 32895169d08SMarek Belisko .of_match_table = of_match_ptr(pcm1681_dt_ids), 32995169d08SMarek Belisko }, 33095169d08SMarek Belisko .id_table = pcm1681_i2c_id, 331*7f7d7214SStephen Kitt .probe_new = pcm1681_i2c_probe, 33295169d08SMarek Belisko }; 33395169d08SMarek Belisko 33495169d08SMarek Belisko module_i2c_driver(pcm1681_i2c_driver); 33595169d08SMarek Belisko 33695169d08SMarek Belisko MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver"); 33795169d08SMarek Belisko MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>"); 33895169d08SMarek Belisko MODULE_LICENSE("GPL"); 339