linux/sound/soc/codecs/pcm1681.c
<<
>>
Prefs
   1/*
   2 * PCM1681 ASoC codec driver
   3 *
   4 * Copyright (c) StreamUnlimited GmbH 2013
   5 *      Marek Belisko <marek.belisko@streamunlimited.com>
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License
   9 * as published by the Free Software Foundation; either version 2
  10 * of the License, or (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/slab.h>
  20#include <linux/delay.h>
  21#include <linux/gpio.h>
  22#include <linux/i2c.h>
  23#include <linux/regmap.h>
  24#include <linux/of.h>
  25#include <linux/of_device.h>
  26#include <linux/of_gpio.h>
  27#include <sound/pcm.h>
  28#include <sound/pcm_params.h>
  29#include <sound/soc.h>
  30#include <sound/tlv.h>
  31
  32#define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE  |         \
  33                             SNDRV_PCM_FMTBIT_S24_LE)
  34
  35#define PCM1681_PCM_RATES   (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
  36                             SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100  | \
  37                             SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200  | \
  38                             SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
  39
  40#define PCM1681_SOFT_MUTE_ALL           0xff
  41#define PCM1681_DEEMPH_RATE_MASK        0x18
  42#define PCM1681_DEEMPH_MASK             0x01
  43
  44#define PCM1681_ATT_CONTROL(X)  (X <= 6 ? X : X + 9) /* Attenuation level */
  45#define PCM1681_SOFT_MUTE       0x07    /* Soft mute control register */
  46#define PCM1681_DAC_CONTROL     0x08    /* DAC operation control */
  47#define PCM1681_FMT_CONTROL     0x09    /* Audio interface data format */
  48#define PCM1681_DEEMPH_CONTROL  0x0a    /* De-emphasis control */
  49#define PCM1681_ZERO_DETECT_STATUS      0x0e    /* Zero detect status reg */
  50
  51static const struct reg_default pcm1681_reg_defaults[] = {
  52        { 0x01, 0xff },
  53        { 0x02, 0xff },
  54        { 0x03, 0xff },
  55        { 0x04, 0xff },
  56        { 0x05, 0xff },
  57        { 0x06, 0xff },
  58        { 0x07, 0x00 },
  59        { 0x08, 0x00 },
  60        { 0x09, 0x06 },
  61        { 0x0A, 0x00 },
  62        { 0x0B, 0xff },
  63        { 0x0C, 0x0f },
  64        { 0x0D, 0x00 },
  65        { 0x10, 0xff },
  66        { 0x11, 0xff },
  67        { 0x12, 0x00 },
  68        { 0x13, 0x00 },
  69};
  70
  71static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg)
  72{
  73        return !((reg == 0x00) || (reg == 0x0f));
  74}
  75
  76static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg)
  77{
  78        return pcm1681_accessible_reg(dev, reg) &&
  79                (reg != PCM1681_ZERO_DETECT_STATUS);
  80}
  81
  82struct pcm1681_private {
  83        struct regmap *regmap;
  84        unsigned int format;
  85        /* Current deemphasis status */
  86        unsigned int deemph;
  87        /* Current rate for deemphasis control */
  88        unsigned int rate;
  89};
  90
  91static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
  92
  93static int pcm1681_set_deemph(struct snd_soc_component *component)
  94{
  95        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
  96        int i = 0, val = -1, enable = 0;
  97
  98        if (priv->deemph) {
  99                for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) {
 100                        if (pcm1681_deemph[i] == priv->rate) {
 101                                val = i;
 102                                break;
 103                        }
 104                }
 105        }
 106
 107        if (val != -1) {
 108                regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
 109                                   PCM1681_DEEMPH_RATE_MASK, val << 3);
 110                enable = 1;
 111        } else {
 112                enable = 0;
 113        }
 114
 115        /* enable/disable deemphasis functionality */
 116        return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
 117                                        PCM1681_DEEMPH_MASK, enable);
 118}
 119
 120static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol,
 121                              struct snd_ctl_elem_value *ucontrol)
 122{
 123        struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 124        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 125
 126        ucontrol->value.integer.value[0] = priv->deemph;
 127
 128        return 0;
 129}
 130
 131static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol,
 132                              struct snd_ctl_elem_value *ucontrol)
 133{
 134        struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
 135        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 136
 137        priv->deemph = ucontrol->value.integer.value[0];
 138
 139        return pcm1681_set_deemph(component);
 140}
 141
 142static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai,
 143                              unsigned int format)
 144{
 145        struct snd_soc_component *component = codec_dai->component;
 146        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 147
 148        /* The PCM1681 can only be slave to all clocks */
 149        if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
 150                dev_err(component->dev, "Invalid clocking mode\n");
 151                return -EINVAL;
 152        }
 153
 154        priv->format = format;
 155
 156        return 0;
 157}
 158
 159static int pcm1681_digital_mute(struct snd_soc_dai *dai, int mute)
 160{
 161        struct snd_soc_component *component = dai->component;
 162        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 163        int val;
 164
 165        if (mute)
 166                val = PCM1681_SOFT_MUTE_ALL;
 167        else
 168                val = 0;
 169
 170        return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val);
 171}
 172
 173static int pcm1681_hw_params(struct snd_pcm_substream *substream,
 174                             struct snd_pcm_hw_params *params,
 175                             struct snd_soc_dai *dai)
 176{
 177        struct snd_soc_component *component = dai->component;
 178        struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 179        int val = 0, ret;
 180
 181        priv->rate = params_rate(params);
 182
 183        switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
 184        case SND_SOC_DAIFMT_RIGHT_J:
 185                switch (params_width(params)) {
 186                case 24:
 187                        val = 0;
 188                        break;
 189                case 16:
 190                        val = 3;
 191                        break;
 192                default:
 193                        return -EINVAL;
 194                }
 195                break;
 196        case SND_SOC_DAIFMT_I2S:
 197                val = 0x04;
 198                break;
 199        case SND_SOC_DAIFMT_LEFT_J:
 200                val = 0x05;
 201                break;
 202        default:
 203                dev_err(component->dev, "Invalid DAI format\n");
 204                return -EINVAL;
 205        }
 206
 207        ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val);
 208        if (ret < 0)
 209                return ret;
 210
 211        return pcm1681_set_deemph(component);
 212}
 213
 214static const struct snd_soc_dai_ops pcm1681_dai_ops = {
 215        .set_fmt        = pcm1681_set_dai_fmt,
 216        .hw_params      = pcm1681_hw_params,
 217        .digital_mute   = pcm1681_digital_mute,
 218};
 219
 220static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = {
 221SND_SOC_DAPM_OUTPUT("VOUT1"),
 222SND_SOC_DAPM_OUTPUT("VOUT2"),
 223SND_SOC_DAPM_OUTPUT("VOUT3"),
 224SND_SOC_DAPM_OUTPUT("VOUT4"),
 225SND_SOC_DAPM_OUTPUT("VOUT5"),
 226SND_SOC_DAPM_OUTPUT("VOUT6"),
 227SND_SOC_DAPM_OUTPUT("VOUT7"),
 228SND_SOC_DAPM_OUTPUT("VOUT8"),
 229};
 230
 231static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = {
 232        { "VOUT1", NULL, "Playback" },
 233        { "VOUT2", NULL, "Playback" },
 234        { "VOUT3", NULL, "Playback" },
 235        { "VOUT4", NULL, "Playback" },
 236        { "VOUT5", NULL, "Playback" },
 237        { "VOUT6", NULL, "Playback" },
 238        { "VOUT7", NULL, "Playback" },
 239        { "VOUT8", NULL, "Playback" },
 240};
 241
 242static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1);
 243
 244static const struct snd_kcontrol_new pcm1681_controls[] = {
 245        SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
 246                        PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0,
 247                        0x7f, 0, pcm1681_dac_tlv),
 248        SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
 249                        PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0,
 250                        0x7f, 0, pcm1681_dac_tlv),
 251        SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
 252                        PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0,
 253                        0x7f, 0, pcm1681_dac_tlv),
 254        SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume",
 255                        PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0,
 256                        0x7f, 0, pcm1681_dac_tlv),
 257        SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
 258                            pcm1681_get_deemph, pcm1681_put_deemph),
 259};
 260
 261static struct snd_soc_dai_driver pcm1681_dai = {
 262        .name = "pcm1681-hifi",
 263        .playback = {
 264                .stream_name = "Playback",
 265                .channels_min = 2,
 266                .channels_max = 8,
 267                .rates = PCM1681_PCM_RATES,
 268                .formats = PCM1681_PCM_FORMATS,
 269        },
 270        .ops = &pcm1681_dai_ops,
 271};
 272
 273#ifdef CONFIG_OF
 274static const struct of_device_id pcm1681_dt_ids[] = {
 275        { .compatible = "ti,pcm1681", },
 276        { }
 277};
 278MODULE_DEVICE_TABLE(of, pcm1681_dt_ids);
 279#endif
 280
 281static const struct regmap_config pcm1681_regmap = {
 282        .reg_bits               = 8,
 283        .val_bits               = 8,
 284        .max_register           = 0x13,
 285        .reg_defaults           = pcm1681_reg_defaults,
 286        .num_reg_defaults       = ARRAY_SIZE(pcm1681_reg_defaults),
 287        .writeable_reg          = pcm1681_writeable_reg,
 288        .readable_reg           = pcm1681_accessible_reg,
 289};
 290
 291static const struct snd_soc_component_driver soc_component_dev_pcm1681 = {
 292        .controls               = pcm1681_controls,
 293        .num_controls           = ARRAY_SIZE(pcm1681_controls),
 294        .dapm_widgets           = pcm1681_dapm_widgets,
 295        .num_dapm_widgets       = ARRAY_SIZE(pcm1681_dapm_widgets),
 296        .dapm_routes            = pcm1681_dapm_routes,
 297        .num_dapm_routes        = ARRAY_SIZE(pcm1681_dapm_routes),
 298        .idle_bias_on           = 1,
 299        .use_pmdown_time        = 1,
 300        .endianness             = 1,
 301        .non_legacy_dai_naming  = 1,
 302};
 303
 304static const struct i2c_device_id pcm1681_i2c_id[] = {
 305        {"pcm1681", 0},
 306        {}
 307};
 308MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id);
 309
 310static int pcm1681_i2c_probe(struct i2c_client *client,
 311                              const struct i2c_device_id *id)
 312{
 313        int ret;
 314        struct pcm1681_private *priv;
 315
 316        priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
 317        if (!priv)
 318                return -ENOMEM;
 319
 320        priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap);
 321        if (IS_ERR(priv->regmap)) {
 322                ret = PTR_ERR(priv->regmap);
 323                dev_err(&client->dev, "Failed to create regmap: %d\n", ret);
 324                return ret;
 325        }
 326
 327        i2c_set_clientdata(client, priv);
 328
 329        return devm_snd_soc_register_component(&client->dev,
 330                &soc_component_dev_pcm1681,
 331                &pcm1681_dai, 1);
 332}
 333
 334static struct i2c_driver pcm1681_i2c_driver = {
 335        .driver = {
 336                .name   = "pcm1681",
 337                .of_match_table = of_match_ptr(pcm1681_dt_ids),
 338        },
 339        .id_table       = pcm1681_i2c_id,
 340        .probe          = pcm1681_i2c_probe,
 341};
 342
 343module_i2c_driver(pcm1681_i2c_driver);
 344
 345MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver");
 346MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>");
 347MODULE_LICENSE("GPL");
 348