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 <sound/pcm.h>
1795169d08SMarek Belisko #include <sound/pcm_params.h>
1895169d08SMarek Belisko #include <sound/soc.h>
1995169d08SMarek Belisko #include <sound/tlv.h>
2095169d08SMarek Belisko
2195169d08SMarek Belisko #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
2295169d08SMarek Belisko SNDRV_PCM_FMTBIT_S24_LE)
2395169d08SMarek Belisko
2495169d08SMarek Belisko #define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
2595169d08SMarek Belisko SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
2695169d08SMarek Belisko SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
2795169d08SMarek Belisko SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
2895169d08SMarek Belisko
2995169d08SMarek Belisko #define PCM1681_SOFT_MUTE_ALL 0xff
3095169d08SMarek Belisko #define PCM1681_DEEMPH_RATE_MASK 0x18
3195169d08SMarek Belisko #define PCM1681_DEEMPH_MASK 0x01
3295169d08SMarek Belisko
3395169d08SMarek Belisko #define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */
3495169d08SMarek Belisko #define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */
3595169d08SMarek Belisko #define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */
3695169d08SMarek Belisko #define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */
3795169d08SMarek Belisko #define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */
3895169d08SMarek Belisko #define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */
3995169d08SMarek Belisko
4095169d08SMarek Belisko static const struct reg_default pcm1681_reg_defaults[] = {
4195169d08SMarek Belisko { 0x01, 0xff },
4295169d08SMarek Belisko { 0x02, 0xff },
4395169d08SMarek Belisko { 0x03, 0xff },
4495169d08SMarek Belisko { 0x04, 0xff },
4595169d08SMarek Belisko { 0x05, 0xff },
4695169d08SMarek Belisko { 0x06, 0xff },
4795169d08SMarek Belisko { 0x07, 0x00 },
4895169d08SMarek Belisko { 0x08, 0x00 },
4995169d08SMarek Belisko { 0x09, 0x06 },
5095169d08SMarek Belisko { 0x0A, 0x00 },
5195169d08SMarek Belisko { 0x0B, 0xff },
5295169d08SMarek Belisko { 0x0C, 0x0f },
5395169d08SMarek Belisko { 0x0D, 0x00 },
5495169d08SMarek Belisko { 0x10, 0xff },
5595169d08SMarek Belisko { 0x11, 0xff },
5695169d08SMarek Belisko { 0x12, 0x00 },
5795169d08SMarek Belisko { 0x13, 0x00 },
5895169d08SMarek Belisko };
5995169d08SMarek Belisko
pcm1681_accessible_reg(struct device * dev,unsigned int reg)6095169d08SMarek Belisko static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg)
6195169d08SMarek Belisko {
6295169d08SMarek Belisko return !((reg == 0x00) || (reg == 0x0f));
6395169d08SMarek Belisko }
6495169d08SMarek Belisko
pcm1681_writeable_reg(struct device * dev,unsigned int reg)65a074ae0eSArnd Bergmann static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg)
6695169d08SMarek Belisko {
6795169d08SMarek Belisko return pcm1681_accessible_reg(dev, reg) &&
6895169d08SMarek Belisko (reg != PCM1681_ZERO_DETECT_STATUS);
6995169d08SMarek Belisko }
7095169d08SMarek Belisko
7195169d08SMarek Belisko struct pcm1681_private {
7295169d08SMarek Belisko struct regmap *regmap;
7395169d08SMarek Belisko unsigned int format;
7495169d08SMarek Belisko /* Current deemphasis status */
7595169d08SMarek Belisko unsigned int deemph;
7695169d08SMarek Belisko /* Current rate for deemphasis control */
7795169d08SMarek Belisko unsigned int rate;
7895169d08SMarek Belisko };
7995169d08SMarek Belisko
8095169d08SMarek Belisko static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
8195169d08SMarek Belisko
pcm1681_set_deemph(struct snd_soc_component * component)827dbbaa51SKuninori Morimoto static int pcm1681_set_deemph(struct snd_soc_component *component)
8395169d08SMarek Belisko {
847dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
85f10280d5SPierre-Louis Bossart int i, val = -1, enable = 0;
8695169d08SMarek Belisko
8748f403beSAxel Lin if (priv->deemph) {
8848f403beSAxel Lin for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) {
8948f403beSAxel Lin if (pcm1681_deemph[i] == priv->rate) {
9095169d08SMarek Belisko val = i;
9148f403beSAxel Lin break;
9248f403beSAxel Lin }
9348f403beSAxel Lin }
9448f403beSAxel Lin }
9595169d08SMarek Belisko
9695169d08SMarek Belisko if (val != -1) {
9795169d08SMarek Belisko regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
98fa8173a3SAxel Lin PCM1681_DEEMPH_RATE_MASK, val << 3);
9995169d08SMarek Belisko enable = 1;
10048f403beSAxel Lin } else {
10195169d08SMarek Belisko enable = 0;
10248f403beSAxel Lin }
10395169d08SMarek Belisko
10495169d08SMarek Belisko /* enable/disable deemphasis functionality */
10595169d08SMarek Belisko return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
10695169d08SMarek Belisko PCM1681_DEEMPH_MASK, enable);
10795169d08SMarek Belisko }
10895169d08SMarek Belisko
pcm1681_get_deemph(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)10995169d08SMarek Belisko static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol,
11095169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol)
11195169d08SMarek Belisko {
1127dbbaa51SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1137dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
11495169d08SMarek Belisko
115d7f58db4STakashi Iwai ucontrol->value.integer.value[0] = priv->deemph;
11695169d08SMarek Belisko
11795169d08SMarek Belisko return 0;
11895169d08SMarek Belisko }
11995169d08SMarek Belisko
pcm1681_put_deemph(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)12095169d08SMarek Belisko static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol,
12195169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol)
12295169d08SMarek Belisko {
1237dbbaa51SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1247dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
12595169d08SMarek Belisko
126d7f58db4STakashi Iwai priv->deemph = ucontrol->value.integer.value[0];
12795169d08SMarek Belisko
1287dbbaa51SKuninori Morimoto return pcm1681_set_deemph(component);
12995169d08SMarek Belisko }
13095169d08SMarek Belisko
pcm1681_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int format)13195169d08SMarek Belisko static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai,
13295169d08SMarek Belisko unsigned int format)
13395169d08SMarek Belisko {
1347dbbaa51SKuninori Morimoto struct snd_soc_component *component = codec_dai->component;
1357dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
13695169d08SMarek Belisko
1371900cb53SMark Brown /* The PCM1681 can only be consumer to all clocks */
1381900cb53SMark Brown if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
1397dbbaa51SKuninori Morimoto dev_err(component->dev, "Invalid clocking mode\n");
14095169d08SMarek Belisko return -EINVAL;
14195169d08SMarek Belisko }
14295169d08SMarek Belisko
14395169d08SMarek Belisko priv->format = format;
14495169d08SMarek Belisko
14595169d08SMarek Belisko return 0;
14695169d08SMarek Belisko }
14795169d08SMarek Belisko
pcm1681_mute(struct snd_soc_dai * dai,int mute,int direction)1481eb2c43dSKuninori Morimoto static int pcm1681_mute(struct snd_soc_dai *dai, int mute, int direction)
14995169d08SMarek Belisko {
1507dbbaa51SKuninori Morimoto struct snd_soc_component *component = dai->component;
1517dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
15295169d08SMarek Belisko int val;
15395169d08SMarek Belisko
15495169d08SMarek Belisko if (mute)
15595169d08SMarek Belisko val = PCM1681_SOFT_MUTE_ALL;
15695169d08SMarek Belisko else
15795169d08SMarek Belisko val = 0;
15895169d08SMarek Belisko
15995169d08SMarek Belisko return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val);
16095169d08SMarek Belisko }
16195169d08SMarek Belisko
pcm1681_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)16295169d08SMarek Belisko static int pcm1681_hw_params(struct snd_pcm_substream *substream,
16395169d08SMarek Belisko struct snd_pcm_hw_params *params,
16495169d08SMarek Belisko struct snd_soc_dai *dai)
16595169d08SMarek Belisko {
1667dbbaa51SKuninori Morimoto struct snd_soc_component *component = dai->component;
1677dbbaa51SKuninori Morimoto struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
16895169d08SMarek Belisko int val = 0, ret;
16995169d08SMarek Belisko
17095169d08SMarek Belisko priv->rate = params_rate(params);
17195169d08SMarek Belisko
17295169d08SMarek Belisko switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
17395169d08SMarek Belisko case SND_SOC_DAIFMT_RIGHT_J:
17450a68fb4SMark Brown switch (params_width(params)) {
17550a68fb4SMark Brown case 24:
17650a68fb4SMark Brown val = 0;
17750a68fb4SMark Brown break;
17850a68fb4SMark Brown case 16:
17950a68fb4SMark Brown val = 3;
18050a68fb4SMark Brown break;
18150a68fb4SMark Brown default:
18250a68fb4SMark Brown return -EINVAL;
18350a68fb4SMark Brown }
18495169d08SMarek Belisko break;
18595169d08SMarek Belisko case SND_SOC_DAIFMT_I2S:
18695169d08SMarek Belisko val = 0x04;
18795169d08SMarek Belisko break;
18895169d08SMarek Belisko case SND_SOC_DAIFMT_LEFT_J:
18995169d08SMarek Belisko val = 0x05;
19095169d08SMarek Belisko break;
19195169d08SMarek Belisko default:
1927dbbaa51SKuninori Morimoto dev_err(component->dev, "Invalid DAI format\n");
19395169d08SMarek Belisko return -EINVAL;
19495169d08SMarek Belisko }
19595169d08SMarek Belisko
19695169d08SMarek Belisko ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val);
19795169d08SMarek Belisko if (ret < 0)
19895169d08SMarek Belisko return ret;
19995169d08SMarek Belisko
2007dbbaa51SKuninori Morimoto return pcm1681_set_deemph(component);
20195169d08SMarek Belisko }
20295169d08SMarek Belisko
20395169d08SMarek Belisko static const struct snd_soc_dai_ops pcm1681_dai_ops = {
20495169d08SMarek Belisko .set_fmt = pcm1681_set_dai_fmt,
20595169d08SMarek Belisko .hw_params = pcm1681_hw_params,
2061eb2c43dSKuninori Morimoto .mute_stream = pcm1681_mute,
2071eb2c43dSKuninori Morimoto .no_capture_mute = 1,
20895169d08SMarek Belisko };
20995169d08SMarek Belisko
210b9281f99SMark Brown static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = {
211b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT1"),
212b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT2"),
213b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT3"),
214b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT4"),
215b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT5"),
216b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT6"),
217b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT7"),
218b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT8"),
219b9281f99SMark Brown };
220b9281f99SMark Brown
221b9281f99SMark Brown static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = {
222b9281f99SMark Brown { "VOUT1", NULL, "Playback" },
223b9281f99SMark Brown { "VOUT2", NULL, "Playback" },
224b9281f99SMark Brown { "VOUT3", NULL, "Playback" },
225b9281f99SMark Brown { "VOUT4", NULL, "Playback" },
226b9281f99SMark Brown { "VOUT5", NULL, "Playback" },
227b9281f99SMark Brown { "VOUT6", NULL, "Playback" },
228b9281f99SMark Brown { "VOUT7", NULL, "Playback" },
229b9281f99SMark Brown { "VOUT8", NULL, "Playback" },
230b9281f99SMark Brown };
231b9281f99SMark Brown
23295169d08SMarek Belisko static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1);
23395169d08SMarek Belisko
23495169d08SMarek Belisko static const struct snd_kcontrol_new pcm1681_controls[] = {
23595169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
23695169d08SMarek Belisko PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0,
23795169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv),
23895169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
23995169d08SMarek Belisko PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0,
24095169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv),
24195169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
24295169d08SMarek Belisko PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0,
24395169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv),
24495169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume",
24595169d08SMarek Belisko PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0,
24695169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv),
24795169d08SMarek Belisko SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
24895169d08SMarek Belisko pcm1681_get_deemph, pcm1681_put_deemph),
24995169d08SMarek Belisko };
25095169d08SMarek Belisko
25116695971SMark Brown static struct snd_soc_dai_driver pcm1681_dai = {
25295169d08SMarek Belisko .name = "pcm1681-hifi",
25395169d08SMarek Belisko .playback = {
25495169d08SMarek Belisko .stream_name = "Playback",
25595169d08SMarek Belisko .channels_min = 2,
25695169d08SMarek Belisko .channels_max = 8,
25795169d08SMarek Belisko .rates = PCM1681_PCM_RATES,
25895169d08SMarek Belisko .formats = PCM1681_PCM_FORMATS,
25995169d08SMarek Belisko },
26095169d08SMarek Belisko .ops = &pcm1681_dai_ops,
26195169d08SMarek Belisko };
26295169d08SMarek Belisko
26395169d08SMarek Belisko #ifdef CONFIG_OF
26495169d08SMarek Belisko static const struct of_device_id pcm1681_dt_ids[] = {
26595169d08SMarek Belisko { .compatible = "ti,pcm1681", },
26695169d08SMarek Belisko { }
26795169d08SMarek Belisko };
26895169d08SMarek Belisko MODULE_DEVICE_TABLE(of, pcm1681_dt_ids);
26995169d08SMarek Belisko #endif
27095169d08SMarek Belisko
27195169d08SMarek Belisko static const struct regmap_config pcm1681_regmap = {
27295169d08SMarek Belisko .reg_bits = 8,
27395169d08SMarek Belisko .val_bits = 8,
27464256ac6SAxel Lin .max_register = 0x13,
27595169d08SMarek Belisko .reg_defaults = pcm1681_reg_defaults,
27695169d08SMarek Belisko .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults),
27795169d08SMarek Belisko .writeable_reg = pcm1681_writeable_reg,
27895169d08SMarek Belisko .readable_reg = pcm1681_accessible_reg,
27995169d08SMarek Belisko };
28095169d08SMarek Belisko
2817dbbaa51SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_pcm1681 = {
28295169d08SMarek Belisko .controls = pcm1681_controls,
28395169d08SMarek Belisko .num_controls = ARRAY_SIZE(pcm1681_controls),
284b9281f99SMark Brown .dapm_widgets = pcm1681_dapm_widgets,
285b9281f99SMark Brown .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets),
286b9281f99SMark Brown .dapm_routes = pcm1681_dapm_routes,
287b9281f99SMark Brown .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes),
2887dbbaa51SKuninori Morimoto .idle_bias_on = 1,
2897dbbaa51SKuninori Morimoto .use_pmdown_time = 1,
2907dbbaa51SKuninori Morimoto .endianness = 1,
29195169d08SMarek Belisko };
29295169d08SMarek Belisko
29395169d08SMarek Belisko static const struct i2c_device_id pcm1681_i2c_id[] = {
294*ba2a2c37SUwe Kleine-König {"pcm1681"},
29595169d08SMarek Belisko {}
29695169d08SMarek Belisko };
29795169d08SMarek Belisko MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id);
29895169d08SMarek Belisko
pcm1681_i2c_probe(struct i2c_client * client)2997f7d7214SStephen Kitt static int pcm1681_i2c_probe(struct i2c_client *client)
30095169d08SMarek Belisko {
30195169d08SMarek Belisko int ret;
30295169d08SMarek Belisko struct pcm1681_private *priv;
30395169d08SMarek Belisko
30495169d08SMarek Belisko priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
30595169d08SMarek Belisko if (!priv)
30695169d08SMarek Belisko return -ENOMEM;
30795169d08SMarek Belisko
30895169d08SMarek Belisko priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap);
30995169d08SMarek Belisko if (IS_ERR(priv->regmap)) {
31095169d08SMarek Belisko ret = PTR_ERR(priv->regmap);
31195169d08SMarek Belisko dev_err(&client->dev, "Failed to create regmap: %d\n", ret);
31295169d08SMarek Belisko return ret;
31395169d08SMarek Belisko }
31495169d08SMarek Belisko
31595169d08SMarek Belisko i2c_set_clientdata(client, priv);
31695169d08SMarek Belisko
3177dbbaa51SKuninori Morimoto return devm_snd_soc_register_component(&client->dev,
3187dbbaa51SKuninori Morimoto &soc_component_dev_pcm1681,
31995169d08SMarek Belisko &pcm1681_dai, 1);
32095169d08SMarek Belisko }
32195169d08SMarek Belisko
32295169d08SMarek Belisko static struct i2c_driver pcm1681_i2c_driver = {
32395169d08SMarek Belisko .driver = {
32495169d08SMarek Belisko .name = "pcm1681",
32595169d08SMarek Belisko .of_match_table = of_match_ptr(pcm1681_dt_ids),
32695169d08SMarek Belisko },
32795169d08SMarek Belisko .id_table = pcm1681_i2c_id,
3289abcd240SUwe Kleine-König .probe = pcm1681_i2c_probe,
32995169d08SMarek Belisko };
33095169d08SMarek Belisko
33195169d08SMarek Belisko module_i2c_driver(pcm1681_i2c_driver);
33295169d08SMarek Belisko
33395169d08SMarek Belisko MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver");
33495169d08SMarek Belisko MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>");
33595169d08SMarek Belisko MODULE_LICENSE("GPL");
336