195169d08SMarek Belisko /* 295169d08SMarek Belisko * PCM1681 ASoC codec driver 395169d08SMarek Belisko * 495169d08SMarek Belisko * Copyright (c) StreamUnlimited GmbH 2013 595169d08SMarek Belisko * Marek Belisko <marek.belisko@streamunlimited.com> 695169d08SMarek Belisko * 795169d08SMarek Belisko * This program is free software; you can redistribute it and/or 895169d08SMarek Belisko * modify it under the terms of the GNU General Public License 995169d08SMarek Belisko * as published by the Free Software Foundation; either version 2 1095169d08SMarek Belisko * of the License, or (at your option) any later version. 1195169d08SMarek Belisko * 1295169d08SMarek Belisko * This program is distributed in the hope that it will be useful, 1395169d08SMarek Belisko * but WITHOUT ANY WARRANTY; without even the implied warranty of 1495169d08SMarek Belisko * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1595169d08SMarek Belisko * GNU General Public License for more details. 1695169d08SMarek Belisko */ 1795169d08SMarek Belisko 1895169d08SMarek Belisko #include <linux/module.h> 1995169d08SMarek Belisko #include <linux/slab.h> 2095169d08SMarek Belisko #include <linux/delay.h> 2195169d08SMarek Belisko #include <linux/gpio.h> 2295169d08SMarek Belisko #include <linux/i2c.h> 2395169d08SMarek Belisko #include <linux/regmap.h> 24193a4716SSachin Kamat #include <linux/of.h> 2595169d08SMarek Belisko #include <linux/of_device.h> 2695169d08SMarek Belisko #include <linux/of_gpio.h> 2795169d08SMarek Belisko #include <sound/pcm.h> 2895169d08SMarek Belisko #include <sound/pcm_params.h> 2995169d08SMarek Belisko #include <sound/soc.h> 3095169d08SMarek Belisko #include <sound/tlv.h> 3195169d08SMarek Belisko 3295169d08SMarek Belisko #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ 3395169d08SMarek Belisko SNDRV_PCM_FMTBIT_S24_LE) 3495169d08SMarek Belisko 3595169d08SMarek Belisko #define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \ 3695169d08SMarek Belisko SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ 3795169d08SMarek Belisko SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ 3895169d08SMarek Belisko SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000) 3995169d08SMarek Belisko 4095169d08SMarek Belisko #define PCM1681_SOFT_MUTE_ALL 0xff 4195169d08SMarek Belisko #define PCM1681_DEEMPH_RATE_MASK 0x18 4295169d08SMarek Belisko #define PCM1681_DEEMPH_MASK 0x01 4395169d08SMarek Belisko 4495169d08SMarek Belisko #define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */ 4595169d08SMarek Belisko #define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */ 4695169d08SMarek Belisko #define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */ 4795169d08SMarek Belisko #define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */ 4895169d08SMarek Belisko #define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */ 4995169d08SMarek Belisko #define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */ 5095169d08SMarek Belisko 5195169d08SMarek Belisko static const struct reg_default pcm1681_reg_defaults[] = { 5295169d08SMarek Belisko { 0x01, 0xff }, 5395169d08SMarek Belisko { 0x02, 0xff }, 5495169d08SMarek Belisko { 0x03, 0xff }, 5595169d08SMarek Belisko { 0x04, 0xff }, 5695169d08SMarek Belisko { 0x05, 0xff }, 5795169d08SMarek Belisko { 0x06, 0xff }, 5895169d08SMarek Belisko { 0x07, 0x00 }, 5995169d08SMarek Belisko { 0x08, 0x00 }, 6095169d08SMarek Belisko { 0x09, 0x06 }, 6195169d08SMarek Belisko { 0x0A, 0x00 }, 6295169d08SMarek Belisko { 0x0B, 0xff }, 6395169d08SMarek Belisko { 0x0C, 0x0f }, 6495169d08SMarek Belisko { 0x0D, 0x00 }, 6595169d08SMarek Belisko { 0x10, 0xff }, 6695169d08SMarek Belisko { 0x11, 0xff }, 6795169d08SMarek Belisko { 0x12, 0x00 }, 6895169d08SMarek Belisko { 0x13, 0x00 }, 6995169d08SMarek Belisko }; 7095169d08SMarek Belisko 7195169d08SMarek Belisko static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg) 7295169d08SMarek Belisko { 7395169d08SMarek Belisko return !((reg == 0x00) || (reg == 0x0f)); 7495169d08SMarek Belisko } 7595169d08SMarek Belisko 7695169d08SMarek Belisko static bool pcm1681_writeable_reg(struct device *dev, unsigned register reg) 7795169d08SMarek Belisko { 7895169d08SMarek Belisko return pcm1681_accessible_reg(dev, reg) && 7995169d08SMarek Belisko (reg != PCM1681_ZERO_DETECT_STATUS); 8095169d08SMarek Belisko } 8195169d08SMarek Belisko 8295169d08SMarek Belisko struct pcm1681_private { 8395169d08SMarek Belisko struct regmap *regmap; 8495169d08SMarek Belisko unsigned int format; 8595169d08SMarek Belisko /* Current deemphasis status */ 8695169d08SMarek Belisko unsigned int deemph; 8795169d08SMarek Belisko /* Current rate for deemphasis control */ 8895169d08SMarek Belisko unsigned int rate; 8995169d08SMarek Belisko }; 9095169d08SMarek Belisko 9195169d08SMarek Belisko static const int pcm1681_deemph[] = { 44100, 48000, 32000 }; 9295169d08SMarek Belisko 9395169d08SMarek Belisko static int pcm1681_set_deemph(struct snd_soc_codec *codec) 9495169d08SMarek Belisko { 9595169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 9695169d08SMarek Belisko int i = 0, val = -1, enable = 0; 9795169d08SMarek Belisko 9895169d08SMarek Belisko if (priv->deemph) 9995169d08SMarek Belisko for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) 10095169d08SMarek Belisko if (pcm1681_deemph[i] == priv->rate) 10195169d08SMarek Belisko val = i; 10295169d08SMarek Belisko 10395169d08SMarek Belisko if (val != -1) { 10495169d08SMarek Belisko regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 10595169d08SMarek Belisko PCM1681_DEEMPH_RATE_MASK, val); 10695169d08SMarek Belisko enable = 1; 10795169d08SMarek Belisko } else 10895169d08SMarek Belisko enable = 0; 10995169d08SMarek Belisko 11095169d08SMarek Belisko /* enable/disable deemphasis functionality */ 11195169d08SMarek Belisko return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 11295169d08SMarek Belisko PCM1681_DEEMPH_MASK, enable); 11395169d08SMarek Belisko } 11495169d08SMarek Belisko 11595169d08SMarek Belisko static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol, 11695169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol) 11795169d08SMarek Belisko { 118*ea53bf77SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); 11995169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 12095169d08SMarek Belisko 12195169d08SMarek Belisko ucontrol->value.enumerated.item[0] = priv->deemph; 12295169d08SMarek Belisko 12395169d08SMarek Belisko return 0; 12495169d08SMarek Belisko } 12595169d08SMarek Belisko 12695169d08SMarek Belisko static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol, 12795169d08SMarek Belisko struct snd_ctl_elem_value *ucontrol) 12895169d08SMarek Belisko { 129*ea53bf77SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); 13095169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 13195169d08SMarek Belisko 13295169d08SMarek Belisko priv->deemph = ucontrol->value.enumerated.item[0]; 13395169d08SMarek Belisko 13495169d08SMarek Belisko return pcm1681_set_deemph(codec); 13595169d08SMarek Belisko } 13695169d08SMarek Belisko 13795169d08SMarek Belisko static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai, 13895169d08SMarek Belisko unsigned int format) 13995169d08SMarek Belisko { 14095169d08SMarek Belisko struct snd_soc_codec *codec = codec_dai->codec; 14195169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 14295169d08SMarek Belisko 14395169d08SMarek Belisko /* The PCM1681 can only be slave to all clocks */ 14495169d08SMarek Belisko if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) { 14595169d08SMarek Belisko dev_err(codec->dev, "Invalid clocking mode\n"); 14695169d08SMarek Belisko return -EINVAL; 14795169d08SMarek Belisko } 14895169d08SMarek Belisko 14995169d08SMarek Belisko priv->format = format; 15095169d08SMarek Belisko 15195169d08SMarek Belisko return 0; 15295169d08SMarek Belisko } 15395169d08SMarek Belisko 15495169d08SMarek Belisko static int pcm1681_digital_mute(struct snd_soc_dai *dai, int mute) 15595169d08SMarek Belisko { 15695169d08SMarek Belisko struct snd_soc_codec *codec = dai->codec; 15795169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 15895169d08SMarek Belisko int val; 15995169d08SMarek Belisko 16095169d08SMarek Belisko if (mute) 16195169d08SMarek Belisko val = PCM1681_SOFT_MUTE_ALL; 16295169d08SMarek Belisko else 16395169d08SMarek Belisko val = 0; 16495169d08SMarek Belisko 16595169d08SMarek Belisko return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val); 16695169d08SMarek Belisko } 16795169d08SMarek Belisko 16895169d08SMarek Belisko static int pcm1681_hw_params(struct snd_pcm_substream *substream, 16995169d08SMarek Belisko struct snd_pcm_hw_params *params, 17095169d08SMarek Belisko struct snd_soc_dai *dai) 17195169d08SMarek Belisko { 17295169d08SMarek Belisko struct snd_soc_codec *codec = dai->codec; 17395169d08SMarek Belisko struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 17495169d08SMarek Belisko int val = 0, ret; 17595169d08SMarek Belisko 17695169d08SMarek Belisko priv->rate = params_rate(params); 17795169d08SMarek Belisko 17895169d08SMarek Belisko switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { 17995169d08SMarek Belisko case SND_SOC_DAIFMT_RIGHT_J: 18050a68fb4SMark Brown switch (params_width(params)) { 18150a68fb4SMark Brown case 24: 18250a68fb4SMark Brown val = 0; 18350a68fb4SMark Brown break; 18450a68fb4SMark Brown case 16: 18550a68fb4SMark Brown val = 3; 18650a68fb4SMark Brown break; 18750a68fb4SMark Brown default: 18850a68fb4SMark Brown return -EINVAL; 18950a68fb4SMark Brown } 19095169d08SMarek Belisko break; 19195169d08SMarek Belisko case SND_SOC_DAIFMT_I2S: 19295169d08SMarek Belisko val = 0x04; 19395169d08SMarek Belisko break; 19495169d08SMarek Belisko case SND_SOC_DAIFMT_LEFT_J: 19595169d08SMarek Belisko val = 0x05; 19695169d08SMarek Belisko break; 19795169d08SMarek Belisko default: 19895169d08SMarek Belisko dev_err(codec->dev, "Invalid DAI format\n"); 19995169d08SMarek Belisko return -EINVAL; 20095169d08SMarek Belisko } 20195169d08SMarek Belisko 20295169d08SMarek Belisko ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val); 20395169d08SMarek Belisko if (ret < 0) 20495169d08SMarek Belisko return ret; 20595169d08SMarek Belisko 20695169d08SMarek Belisko return pcm1681_set_deemph(codec); 20795169d08SMarek Belisko } 20895169d08SMarek Belisko 20995169d08SMarek Belisko static const struct snd_soc_dai_ops pcm1681_dai_ops = { 21095169d08SMarek Belisko .set_fmt = pcm1681_set_dai_fmt, 21195169d08SMarek Belisko .hw_params = pcm1681_hw_params, 21295169d08SMarek Belisko .digital_mute = pcm1681_digital_mute, 21395169d08SMarek Belisko }; 21495169d08SMarek Belisko 215b9281f99SMark Brown static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = { 216b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT1"), 217b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT2"), 218b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT3"), 219b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT4"), 220b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT5"), 221b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT6"), 222b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT7"), 223b9281f99SMark Brown SND_SOC_DAPM_OUTPUT("VOUT8"), 224b9281f99SMark Brown }; 225b9281f99SMark Brown 226b9281f99SMark Brown static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = { 227b9281f99SMark Brown { "VOUT1", NULL, "Playback" }, 228b9281f99SMark Brown { "VOUT2", NULL, "Playback" }, 229b9281f99SMark Brown { "VOUT3", NULL, "Playback" }, 230b9281f99SMark Brown { "VOUT4", NULL, "Playback" }, 231b9281f99SMark Brown { "VOUT5", NULL, "Playback" }, 232b9281f99SMark Brown { "VOUT6", NULL, "Playback" }, 233b9281f99SMark Brown { "VOUT7", NULL, "Playback" }, 234b9281f99SMark Brown { "VOUT8", NULL, "Playback" }, 235b9281f99SMark Brown }; 236b9281f99SMark Brown 23795169d08SMarek Belisko static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1); 23895169d08SMarek Belisko 23995169d08SMarek Belisko static const struct snd_kcontrol_new pcm1681_controls[] = { 24095169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume", 24195169d08SMarek Belisko PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0, 24295169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24395169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume", 24495169d08SMarek Belisko PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0, 24595169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24695169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume", 24795169d08SMarek Belisko PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0, 24895169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 24995169d08SMarek Belisko SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume", 25095169d08SMarek Belisko PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0, 25195169d08SMarek Belisko 0x7f, 0, pcm1681_dac_tlv), 25295169d08SMarek Belisko SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, 25395169d08SMarek Belisko pcm1681_get_deemph, pcm1681_put_deemph), 25495169d08SMarek Belisko }; 25595169d08SMarek Belisko 25616695971SMark Brown static struct snd_soc_dai_driver pcm1681_dai = { 25795169d08SMarek Belisko .name = "pcm1681-hifi", 25895169d08SMarek Belisko .playback = { 25995169d08SMarek Belisko .stream_name = "Playback", 26095169d08SMarek Belisko .channels_min = 2, 26195169d08SMarek Belisko .channels_max = 8, 26295169d08SMarek Belisko .rates = PCM1681_PCM_RATES, 26395169d08SMarek Belisko .formats = PCM1681_PCM_FORMATS, 26495169d08SMarek Belisko }, 26595169d08SMarek Belisko .ops = &pcm1681_dai_ops, 26695169d08SMarek Belisko }; 26795169d08SMarek Belisko 26895169d08SMarek Belisko #ifdef CONFIG_OF 26995169d08SMarek Belisko static const struct of_device_id pcm1681_dt_ids[] = { 27095169d08SMarek Belisko { .compatible = "ti,pcm1681", }, 27195169d08SMarek Belisko { } 27295169d08SMarek Belisko }; 27395169d08SMarek Belisko MODULE_DEVICE_TABLE(of, pcm1681_dt_ids); 27495169d08SMarek Belisko #endif 27595169d08SMarek Belisko 27695169d08SMarek Belisko static const struct regmap_config pcm1681_regmap = { 27795169d08SMarek Belisko .reg_bits = 8, 27895169d08SMarek Belisko .val_bits = 8, 27964256ac6SAxel Lin .max_register = 0x13, 28095169d08SMarek Belisko .reg_defaults = pcm1681_reg_defaults, 28195169d08SMarek Belisko .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults), 28295169d08SMarek Belisko .writeable_reg = pcm1681_writeable_reg, 28395169d08SMarek Belisko .readable_reg = pcm1681_accessible_reg, 28495169d08SMarek Belisko }; 28595169d08SMarek Belisko 28695169d08SMarek Belisko static struct snd_soc_codec_driver soc_codec_dev_pcm1681 = { 28795169d08SMarek Belisko .controls = pcm1681_controls, 28895169d08SMarek Belisko .num_controls = ARRAY_SIZE(pcm1681_controls), 289b9281f99SMark Brown .dapm_widgets = pcm1681_dapm_widgets, 290b9281f99SMark Brown .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets), 291b9281f99SMark Brown .dapm_routes = pcm1681_dapm_routes, 292b9281f99SMark Brown .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes), 29395169d08SMarek Belisko }; 29495169d08SMarek Belisko 29595169d08SMarek Belisko static const struct i2c_device_id pcm1681_i2c_id[] = { 29695169d08SMarek Belisko {"pcm1681", 0}, 29795169d08SMarek Belisko {} 29895169d08SMarek Belisko }; 29995169d08SMarek Belisko MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id); 30095169d08SMarek Belisko 30195169d08SMarek Belisko static int pcm1681_i2c_probe(struct i2c_client *client, 30295169d08SMarek Belisko const struct i2c_device_id *id) 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 32095169d08SMarek Belisko return snd_soc_register_codec(&client->dev, &soc_codec_dev_pcm1681, 32195169d08SMarek Belisko &pcm1681_dai, 1); 32295169d08SMarek Belisko } 32395169d08SMarek Belisko 32495169d08SMarek Belisko static int pcm1681_i2c_remove(struct i2c_client *client) 32595169d08SMarek Belisko { 32695169d08SMarek Belisko snd_soc_unregister_codec(&client->dev); 32795169d08SMarek Belisko return 0; 32895169d08SMarek Belisko } 32995169d08SMarek Belisko 33095169d08SMarek Belisko static struct i2c_driver pcm1681_i2c_driver = { 33195169d08SMarek Belisko .driver = { 33295169d08SMarek Belisko .name = "pcm1681", 33395169d08SMarek Belisko .owner = THIS_MODULE, 33495169d08SMarek Belisko .of_match_table = of_match_ptr(pcm1681_dt_ids), 33595169d08SMarek Belisko }, 33695169d08SMarek Belisko .id_table = pcm1681_i2c_id, 33795169d08SMarek Belisko .probe = pcm1681_i2c_probe, 33895169d08SMarek Belisko .remove = pcm1681_i2c_remove, 33995169d08SMarek Belisko }; 34095169d08SMarek Belisko 34195169d08SMarek Belisko module_i2c_driver(pcm1681_i2c_driver); 34295169d08SMarek Belisko 34395169d08SMarek Belisko MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver"); 34495169d08SMarek Belisko MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>"); 34595169d08SMarek Belisko MODULE_LICENSE("GPL"); 346