apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cs42l51.c |
| 3 | * |
| 4 | * ASoC Driver for Cirrus Logic CS42L51 codecs |
| 5 | * |
| 6 | * Copyright (c) 2010 Arnaud Patard <apatard@mandriva.com> |
| 7 | * |
| 8 | * Based on cs4270.c - Copyright (c) Freescale Semiconductor |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * For now: |
| 20 | * - Only I2C is support. Not SPI |
| 21 | * - master mode *NOT* supported |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/soc.h> |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 28 | #include <sound/tlv.h> |
| 29 | #include <sound/initval.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/pcm.h> |
| 32 | #include <linux/i2c.h> |
| 33 | |
| 34 | #include "cs42l51.h" |
| 35 | |
| 36 | enum master_slave_mode { |
| 37 | MODE_SLAVE, |
| 38 | MODE_SLAVE_AUTO, |
| 39 | MODE_MASTER, |
| 40 | }; |
| 41 | |
| 42 | struct cs42l51_private { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 43 | enum snd_soc_control_type control_type; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 44 | unsigned int mclk; |
| 45 | unsigned int audio_mode; /* The mode (I2S or left-justified) */ |
| 46 | enum master_slave_mode func; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 49 | #define CS42L51_FORMATS ( \ |
| 50 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 51 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ |
| 52 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 53 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) |
| 54 | |
| 55 | static int cs42l51_fill_cache(struct snd_soc_codec *codec) |
| 56 | { |
| 57 | u8 *cache = codec->reg_cache + 1; |
Axel Lin | 6d4f709 | 2011-09-28 10:11:54 +0800 | [diff] [blame] | 58 | struct i2c_client *i2c_client = to_i2c_client(codec->dev); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 59 | s32 length; |
| 60 | |
| 61 | length = i2c_smbus_read_i2c_block_data(i2c_client, |
| 62 | CS42L51_FIRSTREG | 0x80, CS42L51_NUMREGS, cache); |
| 63 | if (length != CS42L51_NUMREGS) { |
| 64 | dev_err(&i2c_client->dev, |
| 65 | "I2C read failure, addr=0x%x (ret=%d vs %d)\n", |
| 66 | i2c_client->addr, length, CS42L51_NUMREGS); |
| 67 | return -EIO; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 73 | static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol, |
| 74 | struct snd_ctl_elem_value *ucontrol) |
| 75 | { |
| 76 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 77 | unsigned long value = snd_soc_read(codec, CS42L51_PCM_MIXER)&3; |
| 78 | |
| 79 | switch (value) { |
| 80 | default: |
| 81 | case 0: |
| 82 | ucontrol->value.integer.value[0] = 0; |
| 83 | break; |
| 84 | /* same value : (L+R)/2 and (R+L)/2 */ |
| 85 | case 1: |
| 86 | case 2: |
| 87 | ucontrol->value.integer.value[0] = 1; |
| 88 | break; |
| 89 | case 3: |
| 90 | ucontrol->value.integer.value[0] = 2; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | #define CHAN_MIX_NORMAL 0x00 |
| 98 | #define CHAN_MIX_BOTH 0x55 |
| 99 | #define CHAN_MIX_SWAP 0xFF |
| 100 | |
| 101 | static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol, |
| 102 | struct snd_ctl_elem_value *ucontrol) |
| 103 | { |
| 104 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 105 | unsigned char val; |
| 106 | |
| 107 | switch (ucontrol->value.integer.value[0]) { |
| 108 | default: |
| 109 | case 0: |
| 110 | val = CHAN_MIX_NORMAL; |
| 111 | break; |
| 112 | case 1: |
| 113 | val = CHAN_MIX_BOTH; |
| 114 | break; |
| 115 | case 2: |
| 116 | val = CHAN_MIX_SWAP; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | snd_soc_write(codec, CS42L51_PCM_MIXER, val); |
| 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0); |
| 126 | static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0); |
| 127 | /* This is a lie. after -102 db, it stays at -102 */ |
| 128 | /* maybe a range would be better */ |
| 129 | static const DECLARE_TLV_DB_SCALE(aout_tlv, -11550, 50, 0); |
| 130 | |
| 131 | static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0); |
| 132 | static const char *chan_mix[] = { |
| 133 | "L R", |
| 134 | "L+R", |
| 135 | "R L", |
| 136 | }; |
| 137 | |
| 138 | static const struct soc_enum cs42l51_chan_mix = |
| 139 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(chan_mix), chan_mix); |
| 140 | |
| 141 | static const struct snd_kcontrol_new cs42l51_snd_controls[] = { |
| 142 | SOC_DOUBLE_R_SX_TLV("PCM Playback Volume", |
| 143 | CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, |
| 144 | 7, 0xffffff99, 0x18, adc_pcm_tlv), |
| 145 | SOC_DOUBLE_R("PCM Playback Switch", |
| 146 | CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1), |
| 147 | SOC_DOUBLE_R_SX_TLV("Analog Playback Volume", |
| 148 | CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL, |
| 149 | 8, 0xffffff19, 0x18, aout_tlv), |
| 150 | SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume", |
| 151 | CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, |
| 152 | 7, 0xffffff99, 0x18, adc_pcm_tlv), |
| 153 | SOC_DOUBLE_R("ADC Mixer Switch", |
| 154 | CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1), |
| 155 | SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0), |
| 156 | SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0), |
| 157 | SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0), |
| 158 | SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0), |
| 159 | SOC_DOUBLE_TLV("Mic Boost Volume", |
| 160 | CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv), |
| 161 | SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv), |
| 162 | SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv), |
| 163 | SOC_ENUM_EXT("PCM channel mixer", |
| 164 | cs42l51_chan_mix, |
| 165 | cs42l51_get_chan_mix, cs42l51_set_chan_mix), |
| 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * to power down, one must: |
| 170 | * 1.) Enable the PDN bit |
| 171 | * 2.) enable power-down for the select channels |
| 172 | * 3.) disable the PDN bit. |
| 173 | */ |
| 174 | static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w, |
| 175 | struct snd_kcontrol *kcontrol, int event) |
| 176 | { |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 177 | switch (event) { |
| 178 | case SND_SOC_DAPM_PRE_PMD: |
Axel Lin | e94de1e | 2011-11-01 15:17:57 +0800 | [diff] [blame] | 179 | snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1, |
| 180 | CS42L51_POWER_CTL1_PDN, |
| 181 | CS42L51_POWER_CTL1_PDN); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 182 | break; |
| 183 | default: |
| 184 | case SND_SOC_DAPM_POST_PMD: |
Axel Lin | e94de1e | 2011-11-01 15:17:57 +0800 | [diff] [blame] | 185 | snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1, |
| 186 | CS42L51_POWER_CTL1_PDN, 0); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 187 | break; |
| 188 | } |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const char *cs42l51_dac_names[] = {"Direct PCM", |
| 194 | "DSP PCM", "ADC"}; |
| 195 | static const struct soc_enum cs42l51_dac_mux_enum = |
| 196 | SOC_ENUM_SINGLE(CS42L51_DAC_CTL, 6, 3, cs42l51_dac_names); |
| 197 | static const struct snd_kcontrol_new cs42l51_dac_mux_controls = |
| 198 | SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum); |
| 199 | |
| 200 | static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left", |
| 201 | "MIC Left", "MIC+preamp Left"}; |
| 202 | static const struct soc_enum cs42l51_adcl_mux_enum = |
| 203 | SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 4, 4, cs42l51_adcl_names); |
| 204 | static const struct snd_kcontrol_new cs42l51_adcl_mux_controls = |
| 205 | SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum); |
| 206 | |
| 207 | static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right", |
| 208 | "MIC Right", "MIC+preamp Right"}; |
| 209 | static const struct soc_enum cs42l51_adcr_mux_enum = |
| 210 | SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 6, 4, cs42l51_adcr_names); |
| 211 | static const struct snd_kcontrol_new cs42l51_adcr_mux_controls = |
| 212 | SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum); |
| 213 | |
| 214 | static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = { |
| 215 | SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1), |
| 216 | SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0, |
| 217 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 218 | SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0, |
| 219 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 220 | SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture", |
| 221 | CS42L51_POWER_CTL1, 1, 1, |
| 222 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 223 | SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture", |
| 224 | CS42L51_POWER_CTL1, 2, 1, |
| 225 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 226 | SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback", |
| 227 | CS42L51_POWER_CTL1, 5, 1, |
| 228 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 229 | SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback", |
| 230 | CS42L51_POWER_CTL1, 6, 1, |
| 231 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
| 232 | |
| 233 | /* analog/mic */ |
| 234 | SND_SOC_DAPM_INPUT("AIN1L"), |
| 235 | SND_SOC_DAPM_INPUT("AIN1R"), |
| 236 | SND_SOC_DAPM_INPUT("AIN2L"), |
| 237 | SND_SOC_DAPM_INPUT("AIN2R"), |
| 238 | SND_SOC_DAPM_INPUT("MICL"), |
| 239 | SND_SOC_DAPM_INPUT("MICR"), |
| 240 | |
| 241 | SND_SOC_DAPM_MIXER("Mic Preamp Left", |
| 242 | CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0), |
| 243 | SND_SOC_DAPM_MIXER("Mic Preamp Right", |
| 244 | CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0), |
| 245 | |
| 246 | /* HP */ |
| 247 | SND_SOC_DAPM_OUTPUT("HPL"), |
| 248 | SND_SOC_DAPM_OUTPUT("HPR"), |
| 249 | |
| 250 | /* mux */ |
| 251 | SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0, |
| 252 | &cs42l51_dac_mux_controls), |
| 253 | SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0, |
| 254 | &cs42l51_adcl_mux_controls), |
| 255 | SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0, |
| 256 | &cs42l51_adcr_mux_controls), |
| 257 | }; |
| 258 | |
| 259 | static const struct snd_soc_dapm_route cs42l51_routes[] = { |
| 260 | {"HPL", NULL, "Left DAC"}, |
| 261 | {"HPR", NULL, "Right DAC"}, |
| 262 | |
| 263 | {"Left ADC", NULL, "Left PGA"}, |
| 264 | {"Right ADC", NULL, "Right PGA"}, |
| 265 | |
| 266 | {"Mic Preamp Left", NULL, "MICL"}, |
| 267 | {"Mic Preamp Right", NULL, "MICR"}, |
| 268 | |
| 269 | {"PGA-ADC Mux Left", "AIN1 Left", "AIN1L" }, |
| 270 | {"PGA-ADC Mux Left", "AIN2 Left", "AIN2L" }, |
| 271 | {"PGA-ADC Mux Left", "MIC Left", "MICL" }, |
| 272 | {"PGA-ADC Mux Left", "MIC+preamp Left", "Mic Preamp Left" }, |
| 273 | {"PGA-ADC Mux Right", "AIN1 Right", "AIN1R" }, |
| 274 | {"PGA-ADC Mux Right", "AIN2 Right", "AIN2R" }, |
| 275 | {"PGA-ADC Mux Right", "MIC Right", "MICR" }, |
| 276 | {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" }, |
| 277 | |
| 278 | {"Left PGA", NULL, "PGA-ADC Mux Left"}, |
| 279 | {"Right PGA", NULL, "PGA-ADC Mux Right"}, |
| 280 | }; |
| 281 | |
| 282 | static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 283 | unsigned int format) |
| 284 | { |
| 285 | struct snd_soc_codec *codec = codec_dai->codec; |
| 286 | struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 287 | |
| 288 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 289 | case SND_SOC_DAIFMT_I2S: |
| 290 | case SND_SOC_DAIFMT_LEFT_J: |
| 291 | case SND_SOC_DAIFMT_RIGHT_J: |
| 292 | cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK; |
| 293 | break; |
| 294 | default: |
| 295 | dev_err(codec->dev, "invalid DAI format\n"); |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 296 | return -EINVAL; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { |
| 300 | case SND_SOC_DAIFMT_CBM_CFM: |
| 301 | cs42l51->func = MODE_MASTER; |
| 302 | break; |
| 303 | case SND_SOC_DAIFMT_CBS_CFS: |
| 304 | cs42l51->func = MODE_SLAVE_AUTO; |
| 305 | break; |
| 306 | default: |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 307 | dev_err(codec->dev, "Unknown master/slave configuration\n"); |
| 308 | return -EINVAL; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 311 | return 0; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | struct cs42l51_ratios { |
| 315 | unsigned int ratio; |
| 316 | unsigned char speed_mode; |
| 317 | unsigned char mclk; |
| 318 | }; |
| 319 | |
| 320 | static struct cs42l51_ratios slave_ratios[] = { |
| 321 | { 512, CS42L51_QSM_MODE, 0 }, { 768, CS42L51_QSM_MODE, 0 }, |
| 322 | { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 }, |
| 323 | { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 }, |
| 324 | { 256, CS42L51_HSM_MODE, 0 }, { 384, CS42L51_HSM_MODE, 0 }, |
| 325 | { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 }, |
| 326 | { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 }, |
| 327 | { 128, CS42L51_SSM_MODE, 0 }, { 192, CS42L51_SSM_MODE, 0 }, |
| 328 | { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 }, |
| 329 | { 512, CS42L51_SSM_MODE, 0 }, { 768, CS42L51_SSM_MODE, 0 }, |
| 330 | { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 }, |
| 331 | { 256, CS42L51_DSM_MODE, 0 }, { 384, CS42L51_DSM_MODE, 0 }, |
| 332 | }; |
| 333 | |
| 334 | static struct cs42l51_ratios slave_auto_ratios[] = { |
| 335 | { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 }, |
| 336 | { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 }, |
| 337 | { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 }, |
| 338 | { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 }, |
| 339 | { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 }, |
| 340 | { 512, CS42L51_SSM_MODE, 1 }, { 768, CS42L51_SSM_MODE, 1 }, |
| 341 | { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 }, |
| 342 | { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 }, |
| 343 | }; |
| 344 | |
| 345 | static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 346 | int clk_id, unsigned int freq, int dir) |
| 347 | { |
| 348 | struct snd_soc_codec *codec = codec_dai->codec; |
| 349 | struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 350 | |
| 351 | cs42l51->mclk = freq; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int cs42l51_hw_params(struct snd_pcm_substream *substream, |
| 356 | struct snd_pcm_hw_params *params, |
| 357 | struct snd_soc_dai *dai) |
| 358 | { |
| 359 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 360 | struct snd_soc_codec *codec = rtd->codec; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 361 | struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec); |
| 362 | int ret; |
| 363 | unsigned int i; |
| 364 | unsigned int rate; |
| 365 | unsigned int ratio; |
| 366 | struct cs42l51_ratios *ratios = NULL; |
| 367 | int nr_ratios = 0; |
| 368 | int intf_ctl, power_ctl, fmt; |
| 369 | |
| 370 | switch (cs42l51->func) { |
| 371 | case MODE_MASTER: |
| 372 | return -EINVAL; |
| 373 | case MODE_SLAVE: |
| 374 | ratios = slave_ratios; |
| 375 | nr_ratios = ARRAY_SIZE(slave_ratios); |
| 376 | break; |
| 377 | case MODE_SLAVE_AUTO: |
| 378 | ratios = slave_auto_ratios; |
| 379 | nr_ratios = ARRAY_SIZE(slave_auto_ratios); |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | /* Figure out which MCLK/LRCK ratio to use */ |
| 384 | rate = params_rate(params); /* Sampling rate, in Hz */ |
| 385 | ratio = cs42l51->mclk / rate; /* MCLK/LRCK ratio */ |
| 386 | for (i = 0; i < nr_ratios; i++) { |
| 387 | if (ratios[i].ratio == ratio) |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | if (i == nr_ratios) { |
| 392 | /* We did not find a matching ratio */ |
| 393 | dev_err(codec->dev, "could not find matching ratio\n"); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | intf_ctl = snd_soc_read(codec, CS42L51_INTF_CTL); |
| 398 | power_ctl = snd_soc_read(codec, CS42L51_MIC_POWER_CTL); |
| 399 | |
| 400 | intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S |
| 401 | | CS42L51_INTF_CTL_DAC_FORMAT(7)); |
| 402 | power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3) |
| 403 | | CS42L51_MIC_POWER_CTL_MCLK_DIV2); |
| 404 | |
| 405 | switch (cs42l51->func) { |
| 406 | case MODE_MASTER: |
| 407 | intf_ctl |= CS42L51_INTF_CTL_MASTER; |
| 408 | power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode); |
| 409 | break; |
| 410 | case MODE_SLAVE: |
| 411 | power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode); |
| 412 | break; |
| 413 | case MODE_SLAVE_AUTO: |
| 414 | power_ctl |= CS42L51_MIC_POWER_CTL_AUTO; |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | switch (cs42l51->audio_mode) { |
| 419 | case SND_SOC_DAIFMT_I2S: |
| 420 | intf_ctl |= CS42L51_INTF_CTL_ADC_I2S; |
| 421 | intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S); |
| 422 | break; |
| 423 | case SND_SOC_DAIFMT_LEFT_J: |
| 424 | intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24); |
| 425 | break; |
| 426 | case SND_SOC_DAIFMT_RIGHT_J: |
| 427 | switch (params_format(params)) { |
| 428 | case SNDRV_PCM_FORMAT_S16_LE: |
| 429 | case SNDRV_PCM_FORMAT_S16_BE: |
| 430 | fmt = CS42L51_DAC_DIF_RJ16; |
| 431 | break; |
| 432 | case SNDRV_PCM_FORMAT_S18_3LE: |
| 433 | case SNDRV_PCM_FORMAT_S18_3BE: |
| 434 | fmt = CS42L51_DAC_DIF_RJ18; |
| 435 | break; |
| 436 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 437 | case SNDRV_PCM_FORMAT_S20_3BE: |
| 438 | fmt = CS42L51_DAC_DIF_RJ20; |
| 439 | break; |
| 440 | case SNDRV_PCM_FORMAT_S24_LE: |
| 441 | case SNDRV_PCM_FORMAT_S24_BE: |
| 442 | fmt = CS42L51_DAC_DIF_RJ24; |
| 443 | break; |
| 444 | default: |
| 445 | dev_err(codec->dev, "unknown format\n"); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt); |
| 449 | break; |
| 450 | default: |
| 451 | dev_err(codec->dev, "unknown format\n"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | if (ratios[i].mclk) |
| 456 | power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2; |
| 457 | |
| 458 | ret = snd_soc_write(codec, CS42L51_INTF_CTL, intf_ctl); |
| 459 | if (ret < 0) |
| 460 | return ret; |
| 461 | |
| 462 | ret = snd_soc_write(codec, CS42L51_MIC_POWER_CTL, power_ctl); |
| 463 | if (ret < 0) |
| 464 | return ret; |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute) |
| 470 | { |
| 471 | struct snd_soc_codec *codec = dai->codec; |
| 472 | int reg; |
| 473 | int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE; |
| 474 | |
| 475 | reg = snd_soc_read(codec, CS42L51_DAC_OUT_CTL); |
| 476 | |
| 477 | if (mute) |
| 478 | reg |= mask; |
| 479 | else |
| 480 | reg &= ~mask; |
| 481 | |
| 482 | return snd_soc_write(codec, CS42L51_DAC_OUT_CTL, reg); |
| 483 | } |
| 484 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 485 | static const struct snd_soc_dai_ops cs42l51_dai_ops = { |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 486 | .hw_params = cs42l51_hw_params, |
| 487 | .set_sysclk = cs42l51_set_dai_sysclk, |
| 488 | .set_fmt = cs42l51_set_dai_fmt, |
| 489 | .digital_mute = cs42l51_dai_mute, |
| 490 | }; |
| 491 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 492 | static struct snd_soc_dai_driver cs42l51_dai = { |
| 493 | .name = "cs42l51-hifi", |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 494 | .playback = { |
| 495 | .stream_name = "Playback", |
| 496 | .channels_min = 1, |
| 497 | .channels_max = 2, |
| 498 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 499 | .formats = CS42L51_FORMATS, |
| 500 | }, |
| 501 | .capture = { |
| 502 | .stream_name = "Capture", |
| 503 | .channels_min = 1, |
| 504 | .channels_max = 2, |
| 505 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 506 | .formats = CS42L51_FORMATS, |
| 507 | }, |
| 508 | .ops = &cs42l51_dai_ops, |
| 509 | }; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 510 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 511 | static int cs42l51_probe(struct snd_soc_codec *codec) |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 512 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 513 | struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 514 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 515 | int ret, reg; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 516 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 517 | ret = cs42l51_fill_cache(codec); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 518 | if (ret < 0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 519 | dev_err(codec->dev, "failed to fill register cache\n"); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 523 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs42l51->control_type); |
| 524 | if (ret < 0) { |
| 525 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * DAC configuration |
| 531 | * - Use signal processor |
| 532 | * - auto mute |
| 533 | * - vol changes immediate |
| 534 | * - no de-emphasize |
| 535 | */ |
| 536 | reg = CS42L51_DAC_CTL_DATA_SEL(1) |
| 537 | | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0); |
| 538 | ret = snd_soc_write(codec, CS42L51_DAC_CTL, reg); |
| 539 | if (ret < 0) |
| 540 | return ret; |
| 541 | |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 542 | snd_soc_add_controls(codec, cs42l51_snd_controls, |
| 543 | ARRAY_SIZE(cs42l51_snd_controls)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 544 | snd_soc_dapm_new_controls(dapm, cs42l51_dapm_widgets, |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 545 | ARRAY_SIZE(cs42l51_dapm_widgets)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 546 | snd_soc_dapm_add_routes(dapm, cs42l51_routes, |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 547 | ARRAY_SIZE(cs42l51_routes)); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 552 | static struct snd_soc_codec_driver soc_codec_device_cs42l51 = { |
| 553 | .probe = cs42l51_probe, |
Axel Lin | b284362 | 2011-11-23 12:46:11 +0800 | [diff] [blame] | 554 | .reg_cache_size = CS42L51_NUMREGS + 1, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 555 | .reg_word_size = sizeof(u8), |
| 556 | }; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 557 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 558 | static int cs42l51_i2c_probe(struct i2c_client *i2c_client, |
| 559 | const struct i2c_device_id *id) |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 560 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 561 | struct cs42l51_private *cs42l51; |
| 562 | int ret; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 563 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 564 | /* Verify that we have a CS42L51 */ |
| 565 | ret = i2c_smbus_read_byte_data(i2c_client, CS42L51_CHIP_REV_ID); |
| 566 | if (ret < 0) { |
| 567 | dev_err(&i2c_client->dev, "failed to read I2C\n"); |
| 568 | goto error; |
| 569 | } |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 570 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 571 | if ((ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) && |
| 572 | (ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) { |
| 573 | dev_err(&i2c_client->dev, "Invalid chip id\n"); |
| 574 | ret = -ENODEV; |
| 575 | goto error; |
| 576 | } |
| 577 | |
| 578 | dev_info(&i2c_client->dev, "found device cs42l51 rev %d\n", |
| 579 | ret & 7); |
| 580 | |
| 581 | cs42l51 = kzalloc(sizeof(struct cs42l51_private), GFP_KERNEL); |
| 582 | if (!cs42l51) { |
| 583 | dev_err(&i2c_client->dev, "could not allocate codec\n"); |
| 584 | return -ENOMEM; |
| 585 | } |
| 586 | |
| 587 | i2c_set_clientdata(i2c_client, cs42l51); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 588 | cs42l51->control_type = SND_SOC_I2C; |
| 589 | |
| 590 | ret = snd_soc_register_codec(&i2c_client->dev, |
| 591 | &soc_codec_device_cs42l51, &cs42l51_dai, 1); |
| 592 | if (ret < 0) |
| 593 | kfree(cs42l51); |
| 594 | error: |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static int cs42l51_i2c_remove(struct i2c_client *client) |
| 599 | { |
| 600 | struct cs42l51_private *cs42l51 = i2c_get_clientdata(client); |
| 601 | |
| 602 | snd_soc_unregister_codec(&client->dev); |
| 603 | kfree(cs42l51); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 607 | static const struct i2c_device_id cs42l51_id[] = { |
| 608 | {"cs42l51", 0}, |
| 609 | {} |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 610 | }; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 611 | MODULE_DEVICE_TABLE(i2c, cs42l51_id); |
| 612 | |
| 613 | static struct i2c_driver cs42l51_i2c_driver = { |
| 614 | .driver = { |
Arnaud Patard (Rtp) | c88e7b9 | 2010-08-30 16:00:05 +0200 | [diff] [blame] | 615 | .name = "cs42l51-codec", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 616 | .owner = THIS_MODULE, |
| 617 | }, |
| 618 | .id_table = cs42l51_id, |
| 619 | .probe = cs42l51_i2c_probe, |
| 620 | .remove = cs42l51_i2c_remove, |
| 621 | }; |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 622 | |
| 623 | static int __init cs42l51_init(void) |
| 624 | { |
| 625 | int ret; |
| 626 | |
| 627 | ret = i2c_add_driver(&cs42l51_i2c_driver); |
| 628 | if (ret != 0) { |
| 629 | printk(KERN_ERR "%s: can't add i2c driver\n", __func__); |
| 630 | return ret; |
| 631 | } |
| 632 | return 0; |
| 633 | } |
| 634 | module_init(cs42l51_init); |
| 635 | |
| 636 | static void __exit cs42l51_exit(void) |
| 637 | { |
| 638 | i2c_del_driver(&cs42l51_i2c_driver); |
| 639 | } |
| 640 | module_exit(cs42l51_exit); |
| 641 | |
Arnaud Patard (Rtp) | 6973789 | 2010-09-09 14:10:33 +0200 | [diff] [blame] | 642 | MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>"); |
apatard@mandriva.com | 72ed5a8 | 2010-05-27 14:57:41 +0200 | [diff] [blame] | 643 | MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver"); |
| 644 | MODULE_LICENSE("GPL"); |