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 51 static 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 71 static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg) 72 { 73 return !((reg == 0x00) || (reg == 0x0f)); 74 } 75 76 static bool pcm1681_writeable_reg(struct device *dev, unsigned register reg) 77 { 78 return pcm1681_accessible_reg(dev, reg) && 79 (reg != PCM1681_ZERO_DETECT_STATUS); 80 } 81 82 struct 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 91 static const int pcm1681_deemph[] = { 44100, 48000, 32000 }; 92 93 static int pcm1681_set_deemph(struct snd_soc_codec *codec) 94 { 95 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 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 103 if (val != -1) { 104 regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 105 PCM1681_DEEMPH_RATE_MASK, val); 106 enable = 1; 107 } else 108 enable = 0; 109 110 /* enable/disable deemphasis functionality */ 111 return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL, 112 PCM1681_DEEMPH_MASK, enable); 113 } 114 115 static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol, 116 struct snd_ctl_elem_value *ucontrol) 117 { 118 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 119 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 120 121 ucontrol->value.enumerated.item[0] = priv->deemph; 122 123 return 0; 124 } 125 126 static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol, 127 struct snd_ctl_elem_value *ucontrol) 128 { 129 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 130 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 131 132 priv->deemph = ucontrol->value.enumerated.item[0]; 133 134 return pcm1681_set_deemph(codec); 135 } 136 137 static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai, 138 unsigned int format) 139 { 140 struct snd_soc_codec *codec = codec_dai->codec; 141 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 142 143 /* The PCM1681 can only be slave to all clocks */ 144 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) { 145 dev_err(codec->dev, "Invalid clocking mode\n"); 146 return -EINVAL; 147 } 148 149 priv->format = format; 150 151 return 0; 152 } 153 154 static int pcm1681_digital_mute(struct snd_soc_dai *dai, int mute) 155 { 156 struct snd_soc_codec *codec = dai->codec; 157 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 158 int val; 159 160 if (mute) 161 val = PCM1681_SOFT_MUTE_ALL; 162 else 163 val = 0; 164 165 return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val); 166 } 167 168 static int pcm1681_hw_params(struct snd_pcm_substream *substream, 169 struct snd_pcm_hw_params *params, 170 struct snd_soc_dai *dai) 171 { 172 struct snd_soc_codec *codec = dai->codec; 173 struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec); 174 int val = 0, ret; 175 int pcm_format = params_format(params); 176 177 priv->rate = params_rate(params); 178 179 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { 180 case SND_SOC_DAIFMT_RIGHT_J: 181 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE) 182 val = 0x00; 183 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE) 184 val = 0x03; 185 break; 186 case SND_SOC_DAIFMT_I2S: 187 val = 0x04; 188 break; 189 case SND_SOC_DAIFMT_LEFT_J: 190 val = 0x05; 191 break; 192 default: 193 dev_err(codec->dev, "Invalid DAI format\n"); 194 return -EINVAL; 195 } 196 197 ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val); 198 if (ret < 0) 199 return ret; 200 201 return pcm1681_set_deemph(codec); 202 } 203 204 static const struct snd_soc_dai_ops pcm1681_dai_ops = { 205 .set_fmt = pcm1681_set_dai_fmt, 206 .hw_params = pcm1681_hw_params, 207 .digital_mute = pcm1681_digital_mute, 208 }; 209 210 static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = { 211 SND_SOC_DAPM_OUTPUT("VOUT1"), 212 SND_SOC_DAPM_OUTPUT("VOUT2"), 213 SND_SOC_DAPM_OUTPUT("VOUT3"), 214 SND_SOC_DAPM_OUTPUT("VOUT4"), 215 SND_SOC_DAPM_OUTPUT("VOUT5"), 216 SND_SOC_DAPM_OUTPUT("VOUT6"), 217 SND_SOC_DAPM_OUTPUT("VOUT7"), 218 SND_SOC_DAPM_OUTPUT("VOUT8"), 219 }; 220 221 static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = { 222 { "VOUT1", NULL, "Playback" }, 223 { "VOUT2", NULL, "Playback" }, 224 { "VOUT3", NULL, "Playback" }, 225 { "VOUT4", NULL, "Playback" }, 226 { "VOUT5", NULL, "Playback" }, 227 { "VOUT6", NULL, "Playback" }, 228 { "VOUT7", NULL, "Playback" }, 229 { "VOUT8", NULL, "Playback" }, 230 }; 231 232 static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1); 233 234 static const struct snd_kcontrol_new pcm1681_controls[] = { 235 SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume", 236 PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0, 237 0x7f, 0, pcm1681_dac_tlv), 238 SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume", 239 PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0, 240 0x7f, 0, pcm1681_dac_tlv), 241 SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume", 242 PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0, 243 0x7f, 0, pcm1681_dac_tlv), 244 SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume", 245 PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0, 246 0x7f, 0, pcm1681_dac_tlv), 247 SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, 248 pcm1681_get_deemph, pcm1681_put_deemph), 249 }; 250 251 static struct snd_soc_dai_driver pcm1681_dai = { 252 .name = "pcm1681-hifi", 253 .playback = { 254 .stream_name = "Playback", 255 .channels_min = 2, 256 .channels_max = 8, 257 .rates = PCM1681_PCM_RATES, 258 .formats = PCM1681_PCM_FORMATS, 259 }, 260 .ops = &pcm1681_dai_ops, 261 }; 262 263 #ifdef CONFIG_OF 264 static const struct of_device_id pcm1681_dt_ids[] = { 265 { .compatible = "ti,pcm1681", }, 266 { } 267 }; 268 MODULE_DEVICE_TABLE(of, pcm1681_dt_ids); 269 #endif 270 271 static const struct regmap_config pcm1681_regmap = { 272 .reg_bits = 8, 273 .val_bits = 8, 274 .max_register = ARRAY_SIZE(pcm1681_reg_defaults) + 1, 275 .reg_defaults = pcm1681_reg_defaults, 276 .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults), 277 .writeable_reg = pcm1681_writeable_reg, 278 .readable_reg = pcm1681_accessible_reg, 279 }; 280 281 static struct snd_soc_codec_driver soc_codec_dev_pcm1681 = { 282 .controls = pcm1681_controls, 283 .num_controls = ARRAY_SIZE(pcm1681_controls), 284 .dapm_widgets = pcm1681_dapm_widgets, 285 .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets), 286 .dapm_routes = pcm1681_dapm_routes, 287 .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes), 288 }; 289 290 static const struct i2c_device_id pcm1681_i2c_id[] = { 291 {"pcm1681", 0}, 292 {} 293 }; 294 MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id); 295 296 static int pcm1681_i2c_probe(struct i2c_client *client, 297 const struct i2c_device_id *id) 298 { 299 int ret; 300 struct pcm1681_private *priv; 301 302 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); 303 if (!priv) 304 return -ENOMEM; 305 306 priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap); 307 if (IS_ERR(priv->regmap)) { 308 ret = PTR_ERR(priv->regmap); 309 dev_err(&client->dev, "Failed to create regmap: %d\n", ret); 310 return ret; 311 } 312 313 i2c_set_clientdata(client, priv); 314 315 return snd_soc_register_codec(&client->dev, &soc_codec_dev_pcm1681, 316 &pcm1681_dai, 1); 317 } 318 319 static int pcm1681_i2c_remove(struct i2c_client *client) 320 { 321 snd_soc_unregister_codec(&client->dev); 322 return 0; 323 } 324 325 static struct i2c_driver pcm1681_i2c_driver = { 326 .driver = { 327 .name = "pcm1681", 328 .owner = THIS_MODULE, 329 .of_match_table = of_match_ptr(pcm1681_dt_ids), 330 }, 331 .id_table = pcm1681_i2c_id, 332 .probe = pcm1681_i2c_probe, 333 .remove = pcm1681_i2c_remove, 334 }; 335 336 module_i2c_driver(pcm1681_i2c_driver); 337 338 MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver"); 339 MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>"); 340 MODULE_LICENSE("GPL"); 341