Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 1 | /* |
| 2 | * CS4271 ASoC codec driver |
| 3 | * |
| 4 | * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * This driver support CS4271 codec being master or slave, working |
| 17 | * in control port mode, connected either via SPI or I2C. |
| 18 | * The data format accepted is I2S or left-justified. |
| 19 | * DAPM support not implemented. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/tlv.h> |
| 28 | #include <linux/gpio.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/spi/spi.h> |
| 31 | #include <sound/cs4271.h> |
| 32 | |
| 33 | #define CS4271_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 34 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 35 | SNDRV_PCM_FMTBIT_S32_LE) |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 36 | #define CS4271_PCM_RATES SNDRV_PCM_RATE_8000_192000 |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * CS4271 registers |
| 40 | * High byte represents SPI chip address (0x10) + write command (0) |
| 41 | * Low byte - codec register address |
| 42 | */ |
| 43 | #define CS4271_MODE1 0x2001 /* Mode Control 1 */ |
| 44 | #define CS4271_DACCTL 0x2002 /* DAC Control */ |
| 45 | #define CS4271_DACVOL 0x2003 /* DAC Volume & Mixing Control */ |
| 46 | #define CS4271_VOLA 0x2004 /* DAC Channel A Volume Control */ |
| 47 | #define CS4271_VOLB 0x2005 /* DAC Channel B Volume Control */ |
| 48 | #define CS4271_ADCCTL 0x2006 /* ADC Control */ |
| 49 | #define CS4271_MODE2 0x2007 /* Mode Control 2 */ |
| 50 | #define CS4271_CHIPID 0x2008 /* Chip ID */ |
| 51 | |
| 52 | #define CS4271_FIRSTREG CS4271_MODE1 |
| 53 | #define CS4271_LASTREG CS4271_MODE2 |
| 54 | #define CS4271_NR_REGS ((CS4271_LASTREG & 0xFF) + 1) |
| 55 | |
| 56 | /* Bit masks for the CS4271 registers */ |
| 57 | #define CS4271_MODE1_MODE_MASK 0xC0 |
| 58 | #define CS4271_MODE1_MODE_1X 0x00 |
| 59 | #define CS4271_MODE1_MODE_2X 0x80 |
| 60 | #define CS4271_MODE1_MODE_4X 0xC0 |
| 61 | |
| 62 | #define CS4271_MODE1_DIV_MASK 0x30 |
| 63 | #define CS4271_MODE1_DIV_1 0x00 |
| 64 | #define CS4271_MODE1_DIV_15 0x10 |
| 65 | #define CS4271_MODE1_DIV_2 0x20 |
| 66 | #define CS4271_MODE1_DIV_3 0x30 |
| 67 | |
| 68 | #define CS4271_MODE1_MASTER 0x08 |
| 69 | |
| 70 | #define CS4271_MODE1_DAC_DIF_MASK 0x07 |
| 71 | #define CS4271_MODE1_DAC_DIF_LJ 0x00 |
| 72 | #define CS4271_MODE1_DAC_DIF_I2S 0x01 |
| 73 | #define CS4271_MODE1_DAC_DIF_RJ16 0x02 |
| 74 | #define CS4271_MODE1_DAC_DIF_RJ24 0x03 |
| 75 | #define CS4271_MODE1_DAC_DIF_RJ20 0x04 |
| 76 | #define CS4271_MODE1_DAC_DIF_RJ18 0x05 |
| 77 | |
| 78 | #define CS4271_DACCTL_AMUTE 0x80 |
| 79 | #define CS4271_DACCTL_IF_SLOW 0x40 |
| 80 | |
| 81 | #define CS4271_DACCTL_DEM_MASK 0x30 |
| 82 | #define CS4271_DACCTL_DEM_DIS 0x00 |
| 83 | #define CS4271_DACCTL_DEM_441 0x10 |
| 84 | #define CS4271_DACCTL_DEM_48 0x20 |
| 85 | #define CS4271_DACCTL_DEM_32 0x30 |
| 86 | |
| 87 | #define CS4271_DACCTL_SVRU 0x08 |
| 88 | #define CS4271_DACCTL_SRD 0x04 |
| 89 | #define CS4271_DACCTL_INVA 0x02 |
| 90 | #define CS4271_DACCTL_INVB 0x01 |
| 91 | |
| 92 | #define CS4271_DACVOL_BEQUA 0x40 |
| 93 | #define CS4271_DACVOL_SOFT 0x20 |
| 94 | #define CS4271_DACVOL_ZEROC 0x10 |
| 95 | |
| 96 | #define CS4271_DACVOL_ATAPI_MASK 0x0F |
| 97 | #define CS4271_DACVOL_ATAPI_M_M 0x00 |
| 98 | #define CS4271_DACVOL_ATAPI_M_BR 0x01 |
| 99 | #define CS4271_DACVOL_ATAPI_M_BL 0x02 |
| 100 | #define CS4271_DACVOL_ATAPI_M_BLR2 0x03 |
| 101 | #define CS4271_DACVOL_ATAPI_AR_M 0x04 |
| 102 | #define CS4271_DACVOL_ATAPI_AR_BR 0x05 |
| 103 | #define CS4271_DACVOL_ATAPI_AR_BL 0x06 |
| 104 | #define CS4271_DACVOL_ATAPI_AR_BLR2 0x07 |
| 105 | #define CS4271_DACVOL_ATAPI_AL_M 0x08 |
| 106 | #define CS4271_DACVOL_ATAPI_AL_BR 0x09 |
| 107 | #define CS4271_DACVOL_ATAPI_AL_BL 0x0A |
| 108 | #define CS4271_DACVOL_ATAPI_AL_BLR2 0x0B |
| 109 | #define CS4271_DACVOL_ATAPI_ALR2_M 0x0C |
| 110 | #define CS4271_DACVOL_ATAPI_ALR2_BR 0x0D |
| 111 | #define CS4271_DACVOL_ATAPI_ALR2_BL 0x0E |
| 112 | #define CS4271_DACVOL_ATAPI_ALR2_BLR2 0x0F |
| 113 | |
| 114 | #define CS4271_VOLA_MUTE 0x80 |
| 115 | #define CS4271_VOLA_VOL_MASK 0x7F |
| 116 | #define CS4271_VOLB_MUTE 0x80 |
| 117 | #define CS4271_VOLB_VOL_MASK 0x7F |
| 118 | |
| 119 | #define CS4271_ADCCTL_DITHER16 0x20 |
| 120 | |
| 121 | #define CS4271_ADCCTL_ADC_DIF_MASK 0x10 |
| 122 | #define CS4271_ADCCTL_ADC_DIF_LJ 0x00 |
| 123 | #define CS4271_ADCCTL_ADC_DIF_I2S 0x10 |
| 124 | |
| 125 | #define CS4271_ADCCTL_MUTEA 0x08 |
| 126 | #define CS4271_ADCCTL_MUTEB 0x04 |
| 127 | #define CS4271_ADCCTL_HPFDA 0x02 |
| 128 | #define CS4271_ADCCTL_HPFDB 0x01 |
| 129 | |
| 130 | #define CS4271_MODE2_LOOP 0x10 |
| 131 | #define CS4271_MODE2_MUTECAEQUB 0x08 |
| 132 | #define CS4271_MODE2_FREEZE 0x04 |
| 133 | #define CS4271_MODE2_CPEN 0x02 |
| 134 | #define CS4271_MODE2_PDN 0x01 |
| 135 | |
| 136 | #define CS4271_CHIPID_PART_MASK 0xF0 |
| 137 | #define CS4271_CHIPID_REV_MASK 0x0F |
| 138 | |
| 139 | /* |
| 140 | * Default CS4271 power-up configuration |
| 141 | * Array contains non-existing in hw register at address 0 |
| 142 | * Array do not include Chip ID, as codec driver does not use |
| 143 | * registers read operations at all |
| 144 | */ |
| 145 | static const u8 cs4271_dflt_reg[CS4271_NR_REGS] = { |
| 146 | 0, |
| 147 | 0, |
| 148 | CS4271_DACCTL_AMUTE, |
| 149 | CS4271_DACVOL_SOFT | CS4271_DACVOL_ATAPI_AL_BR, |
| 150 | 0, |
| 151 | 0, |
| 152 | 0, |
| 153 | 0, |
| 154 | }; |
| 155 | |
| 156 | struct cs4271_private { |
| 157 | /* SND_SOC_I2C or SND_SOC_SPI */ |
| 158 | enum snd_soc_control_type bus_type; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 159 | unsigned int mclk; |
| 160 | bool master; |
| 161 | bool deemph; |
| 162 | /* Current sample rate for de-emphasis control */ |
| 163 | int rate; |
| 164 | /* GPIO driving Reset pin, if any */ |
| 165 | int gpio_nreset; |
| 166 | /* GPIO that disable serial bus, if any */ |
| 167 | int gpio_disable; |
| 168 | }; |
| 169 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 170 | /* |
| 171 | * @freq is the desired MCLK rate |
| 172 | * MCLK rate should (c) be the sample rate, multiplied by one of the |
| 173 | * ratios listed in cs4271_mclk_fs_ratios table |
| 174 | */ |
| 175 | static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 176 | int clk_id, unsigned int freq, int dir) |
| 177 | { |
| 178 | struct snd_soc_codec *codec = codec_dai->codec; |
| 179 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
| 180 | |
| 181 | cs4271->mclk = freq; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 186 | unsigned int format) |
| 187 | { |
| 188 | struct snd_soc_codec *codec = codec_dai->codec; |
| 189 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
| 190 | unsigned int val = 0; |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 191 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 192 | |
| 193 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { |
| 194 | case SND_SOC_DAIFMT_CBS_CFS: |
| 195 | cs4271->master = 0; |
| 196 | break; |
| 197 | case SND_SOC_DAIFMT_CBM_CFM: |
| 198 | cs4271->master = 1; |
| 199 | val |= CS4271_MODE1_MASTER; |
| 200 | break; |
| 201 | default: |
| 202 | dev_err(codec->dev, "Invalid DAI format\n"); |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 207 | case SND_SOC_DAIFMT_LEFT_J: |
| 208 | val |= CS4271_MODE1_DAC_DIF_LJ; |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 209 | ret = snd_soc_update_bits(codec, CS4271_ADCCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 210 | CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 211 | if (ret < 0) |
| 212 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 213 | break; |
| 214 | case SND_SOC_DAIFMT_I2S: |
| 215 | val |= CS4271_MODE1_DAC_DIF_I2S; |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 216 | ret = snd_soc_update_bits(codec, CS4271_ADCCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 217 | CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 218 | if (ret < 0) |
| 219 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 220 | break; |
| 221 | default: |
| 222 | dev_err(codec->dev, "Invalid DAI format\n"); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 226 | ret = snd_soc_update_bits(codec, CS4271_MODE1, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 227 | CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 228 | if (ret < 0) |
| 229 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int cs4271_deemph[] = {0, 44100, 48000, 32000}; |
| 234 | |
| 235 | static int cs4271_set_deemph(struct snd_soc_codec *codec) |
| 236 | { |
| 237 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 238 | int i, ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 239 | int val = CS4271_DACCTL_DEM_DIS; |
| 240 | |
| 241 | if (cs4271->deemph) { |
| 242 | /* Find closest de-emphasis freq */ |
| 243 | val = 1; |
| 244 | for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++) |
| 245 | if (abs(cs4271_deemph[i] - cs4271->rate) < |
| 246 | abs(cs4271_deemph[val] - cs4271->rate)) |
| 247 | val = i; |
| 248 | val <<= 4; |
| 249 | } |
| 250 | |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 251 | ret = snd_soc_update_bits(codec, CS4271_DACCTL, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 252 | CS4271_DACCTL_DEM_MASK, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 253 | if (ret < 0) |
| 254 | return ret; |
| 255 | return 0; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int cs4271_get_deemph(struct snd_kcontrol *kcontrol, |
| 259 | struct snd_ctl_elem_value *ucontrol) |
| 260 | { |
| 261 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 262 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
| 263 | |
| 264 | ucontrol->value.enumerated.item[0] = cs4271->deemph; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int cs4271_put_deemph(struct snd_kcontrol *kcontrol, |
| 269 | struct snd_ctl_elem_value *ucontrol) |
| 270 | { |
| 271 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 272 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
| 273 | |
| 274 | cs4271->deemph = ucontrol->value.enumerated.item[0]; |
| 275 | return cs4271_set_deemph(codec); |
| 276 | } |
| 277 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 278 | struct cs4271_clk_cfg { |
| 279 | bool master; /* codec mode */ |
| 280 | u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */ |
| 281 | unsigned short ratio; /* MCLK / sample rate */ |
| 282 | u8 ratio_mask; /* ratio bit mask for Master mode */ |
| 283 | }; |
| 284 | |
| 285 | static struct cs4271_clk_cfg cs4271_clk_tab[] = { |
| 286 | {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1}, |
| 287 | {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15}, |
| 288 | {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2}, |
| 289 | {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3}, |
| 290 | {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1}, |
| 291 | {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15}, |
| 292 | {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2}, |
| 293 | {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3}, |
| 294 | {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1}, |
| 295 | {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15}, |
| 296 | {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2}, |
| 297 | {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3}, |
| 298 | {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1}, |
| 299 | {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1}, |
| 300 | {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1}, |
| 301 | {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2}, |
| 302 | {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2}, |
| 303 | {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1}, |
| 304 | {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1}, |
| 305 | {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1}, |
| 306 | {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2}, |
| 307 | {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2}, |
| 308 | {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1}, |
| 309 | {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1}, |
| 310 | {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1}, |
| 311 | {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2}, |
| 312 | {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2}, |
| 313 | }; |
| 314 | |
| 315 | #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab) |
| 316 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 317 | static int cs4271_hw_params(struct snd_pcm_substream *substream, |
| 318 | struct snd_pcm_hw_params *params, |
| 319 | struct snd_soc_dai *dai) |
| 320 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 321 | struct snd_soc_codec *codec = dai->codec; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 322 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 323 | int i, ret; |
| 324 | unsigned int ratio, val; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 325 | |
| 326 | cs4271->rate = params_rate(params); |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 327 | |
| 328 | /* Configure DAC */ |
| 329 | if (cs4271->rate < 50000) |
| 330 | val = CS4271_MODE1_MODE_1X; |
| 331 | else if (cs4271->rate < 100000) |
| 332 | val = CS4271_MODE1_MODE_2X; |
| 333 | else |
| 334 | val = CS4271_MODE1_MODE_4X; |
| 335 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 336 | ratio = cs4271->mclk / cs4271->rate; |
| 337 | for (i = 0; i < CS4171_NR_RATIOS; i++) |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 338 | if ((cs4271_clk_tab[i].master == cs4271->master) && |
| 339 | (cs4271_clk_tab[i].speed_mode == val) && |
| 340 | (cs4271_clk_tab[i].ratio == ratio)) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 341 | break; |
| 342 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 343 | if (i == CS4171_NR_RATIOS) { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 344 | dev_err(codec->dev, "Invalid sample rate\n"); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
Alexander Sverdlin | 5c3a12e | 2011-03-07 20:29:45 +0300 | [diff] [blame] | 348 | val |= cs4271_clk_tab[i].ratio_mask; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 349 | |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 350 | ret = snd_soc_update_bits(codec, CS4271_MODE1, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 351 | CS4271_MODE1_MODE_MASK | CS4271_MODE1_DIV_MASK, val); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 352 | if (ret < 0) |
| 353 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 354 | |
| 355 | return cs4271_set_deemph(codec); |
| 356 | } |
| 357 | |
| 358 | static int cs4271_digital_mute(struct snd_soc_dai *dai, int mute) |
| 359 | { |
| 360 | struct snd_soc_codec *codec = dai->codec; |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 361 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 362 | int val_a = 0; |
| 363 | int val_b = 0; |
| 364 | |
| 365 | if (mute) { |
| 366 | val_a = CS4271_VOLA_MUTE; |
| 367 | val_b = CS4271_VOLB_MUTE; |
| 368 | } |
| 369 | |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 370 | ret = snd_soc_update_bits(codec, CS4271_VOLA, CS4271_VOLA_MUTE, val_a); |
| 371 | if (ret < 0) |
| 372 | return ret; |
| 373 | ret = snd_soc_update_bits(codec, CS4271_VOLB, CS4271_VOLB_MUTE, val_b); |
| 374 | if (ret < 0) |
| 375 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* CS4271 controls */ |
| 381 | static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0); |
| 382 | |
| 383 | static const struct snd_kcontrol_new cs4271_snd_controls[] = { |
| 384 | SOC_DOUBLE_R_TLV("Master Playback Volume", CS4271_VOLA, CS4271_VOLB, |
| 385 | 0, 0x7F, 1, cs4271_dac_tlv), |
| 386 | SOC_SINGLE("Digital Loopback Switch", CS4271_MODE2, 4, 1, 0), |
| 387 | SOC_SINGLE("Soft Ramp Switch", CS4271_DACVOL, 5, 1, 0), |
| 388 | SOC_SINGLE("Zero Cross Switch", CS4271_DACVOL, 4, 1, 0), |
| 389 | SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, |
| 390 | cs4271_get_deemph, cs4271_put_deemph), |
| 391 | SOC_SINGLE("Auto-Mute Switch", CS4271_DACCTL, 7, 1, 0), |
| 392 | SOC_SINGLE("Slow Roll Off Filter Switch", CS4271_DACCTL, 6, 1, 0), |
| 393 | SOC_SINGLE("Soft Volume Ramp-Up Switch", CS4271_DACCTL, 3, 1, 0), |
| 394 | SOC_SINGLE("Soft Ramp-Down Switch", CS4271_DACCTL, 2, 1, 0), |
| 395 | SOC_SINGLE("Left Channel Inversion Switch", CS4271_DACCTL, 1, 1, 0), |
| 396 | SOC_SINGLE("Right Channel Inversion Switch", CS4271_DACCTL, 0, 1, 0), |
| 397 | SOC_DOUBLE("Master Capture Switch", CS4271_ADCCTL, 3, 2, 1, 1), |
| 398 | SOC_SINGLE("Dither 16-Bit Data Switch", CS4271_ADCCTL, 5, 1, 0), |
| 399 | SOC_DOUBLE("High Pass Filter Switch", CS4271_ADCCTL, 1, 0, 1, 1), |
| 400 | SOC_DOUBLE_R("Master Playback Switch", CS4271_VOLA, CS4271_VOLB, |
| 401 | 7, 1, 1), |
| 402 | }; |
| 403 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 404 | static const struct snd_soc_dai_ops cs4271_dai_ops = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 405 | .hw_params = cs4271_hw_params, |
| 406 | .set_sysclk = cs4271_set_dai_sysclk, |
| 407 | .set_fmt = cs4271_set_dai_fmt, |
| 408 | .digital_mute = cs4271_digital_mute, |
| 409 | }; |
| 410 | |
Mark Brown | 16af7d6 | 2011-01-26 11:35:28 +0000 | [diff] [blame] | 411 | static struct snd_soc_dai_driver cs4271_dai = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 412 | .name = "cs4271-hifi", |
| 413 | .playback = { |
| 414 | .stream_name = "Playback", |
| 415 | .channels_min = 2, |
| 416 | .channels_max = 2, |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 417 | .rates = CS4271_PCM_RATES, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 418 | .formats = CS4271_PCM_FORMATS, |
| 419 | }, |
| 420 | .capture = { |
| 421 | .stream_name = "Capture", |
| 422 | .channels_min = 2, |
| 423 | .channels_max = 2, |
Alexander Sverdlin | 383f846 | 2011-03-07 20:29:36 +0300 | [diff] [blame] | 424 | .rates = CS4271_PCM_RATES, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 425 | .formats = CS4271_PCM_FORMATS, |
| 426 | }, |
| 427 | .ops = &cs4271_dai_ops, |
| 428 | .symmetric_rates = 1, |
| 429 | }; |
| 430 | |
| 431 | #ifdef CONFIG_PM |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 432 | static int cs4271_soc_suspend(struct snd_soc_codec *codec) |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 433 | { |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 434 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 435 | /* Set power-down bit */ |
Axel Lin | ef0cd47 | 2011-11-19 14:41:07 +0800 | [diff] [blame] | 436 | ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, |
| 437 | CS4271_MODE2_PDN); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 438 | if (ret < 0) |
| 439 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int cs4271_soc_resume(struct snd_soc_codec *codec) |
| 444 | { |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 445 | int ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 446 | /* Restore codec state */ |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 447 | ret = snd_soc_cache_sync(codec); |
| 448 | if (ret < 0) |
| 449 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 450 | /* then disable the power-down bit */ |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 451 | ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0); |
| 452 | if (ret < 0) |
| 453 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | #else |
| 457 | #define cs4271_soc_suspend NULL |
| 458 | #define cs4271_soc_resume NULL |
| 459 | #endif /* CONFIG_PM */ |
| 460 | |
| 461 | static int cs4271_probe(struct snd_soc_codec *codec) |
| 462 | { |
| 463 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
| 464 | struct cs4271_platform_data *cs4271plat = codec->dev->platform_data; |
| 465 | int ret; |
| 466 | int gpio_nreset = -EINVAL; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 467 | |
Alexander Sverdlin | a98a0bc | 2011-02-03 03:11:45 +0300 | [diff] [blame] | 468 | if (cs4271plat && gpio_is_valid(cs4271plat->gpio_nreset)) |
| 469 | gpio_nreset = cs4271plat->gpio_nreset; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 470 | |
| 471 | if (gpio_nreset >= 0) |
| 472 | if (gpio_request(gpio_nreset, "CS4271 Reset")) |
| 473 | gpio_nreset = -EINVAL; |
| 474 | if (gpio_nreset >= 0) { |
| 475 | /* Reset codec */ |
| 476 | gpio_direction_output(gpio_nreset, 0); |
| 477 | udelay(1); |
| 478 | gpio_set_value(gpio_nreset, 1); |
| 479 | /* Give the codec time to wake up */ |
| 480 | udelay(1); |
| 481 | } |
| 482 | |
| 483 | cs4271->gpio_nreset = gpio_nreset; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * In case of I2C, chip address specified in board data. |
| 487 | * So cache IO operations use 8 bit codec register address. |
| 488 | * In case of SPI, chip address and register address |
| 489 | * passed together as 16 bit value. |
| 490 | * Anyway, register address is masked with 0xFF inside |
| 491 | * soc-cache code. |
| 492 | */ |
| 493 | if (cs4271->bus_type == SND_SOC_SPI) |
| 494 | ret = snd_soc_codec_set_cache_io(codec, 16, 8, |
| 495 | cs4271->bus_type); |
| 496 | else |
| 497 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, |
| 498 | cs4271->bus_type); |
| 499 | if (ret) { |
| 500 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 501 | return ret; |
| 502 | } |
| 503 | |
Axel Lin | ef0cd47 | 2011-11-19 14:41:07 +0800 | [diff] [blame] | 504 | ret = snd_soc_update_bits(codec, CS4271_MODE2, |
| 505 | CS4271_MODE2_PDN | CS4271_MODE2_CPEN, |
| 506 | CS4271_MODE2_PDN | CS4271_MODE2_CPEN); |
Alexander Sverdlin | 0d42e6e | 2011-01-21 22:22:07 +0300 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | return ret; |
| 509 | ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0); |
| 510 | if (ret < 0) |
| 511 | return ret; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 512 | /* Power-up sequence requires 85 uS */ |
| 513 | udelay(85); |
| 514 | |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 515 | return snd_soc_add_codec_controls(codec, cs4271_snd_controls, |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 516 | ARRAY_SIZE(cs4271_snd_controls)); |
| 517 | } |
| 518 | |
| 519 | static int cs4271_remove(struct snd_soc_codec *codec) |
| 520 | { |
| 521 | struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec); |
Alexander Sverdlin | a98a0bc | 2011-02-03 03:11:45 +0300 | [diff] [blame] | 522 | int gpio_nreset; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 523 | |
| 524 | gpio_nreset = cs4271->gpio_nreset; |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 525 | |
| 526 | if (gpio_is_valid(gpio_nreset)) { |
| 527 | /* Set codec to the reset state */ |
| 528 | gpio_set_value(gpio_nreset, 0); |
| 529 | gpio_free(gpio_nreset); |
| 530 | } |
| 531 | |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 532 | return 0; |
| 533 | }; |
| 534 | |
Mark Brown | 16af7d6 | 2011-01-26 11:35:28 +0000 | [diff] [blame] | 535 | static struct snd_soc_codec_driver soc_codec_dev_cs4271 = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 536 | .probe = cs4271_probe, |
| 537 | .remove = cs4271_remove, |
| 538 | .suspend = cs4271_soc_suspend, |
| 539 | .resume = cs4271_soc_resume, |
| 540 | .reg_cache_default = cs4271_dflt_reg, |
| 541 | .reg_cache_size = ARRAY_SIZE(cs4271_dflt_reg), |
| 542 | .reg_word_size = sizeof(cs4271_dflt_reg[0]), |
| 543 | .compress_type = SND_SOC_FLAT_COMPRESSION, |
| 544 | }; |
| 545 | |
| 546 | #if defined(CONFIG_SPI_MASTER) |
| 547 | static int __devinit cs4271_spi_probe(struct spi_device *spi) |
| 548 | { |
| 549 | struct cs4271_private *cs4271; |
| 550 | |
| 551 | cs4271 = devm_kzalloc(&spi->dev, sizeof(*cs4271), GFP_KERNEL); |
| 552 | if (!cs4271) |
| 553 | return -ENOMEM; |
| 554 | |
| 555 | spi_set_drvdata(spi, cs4271); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 556 | cs4271->bus_type = SND_SOC_SPI; |
| 557 | |
| 558 | return snd_soc_register_codec(&spi->dev, &soc_codec_dev_cs4271, |
| 559 | &cs4271_dai, 1); |
| 560 | } |
| 561 | |
| 562 | static int __devexit cs4271_spi_remove(struct spi_device *spi) |
| 563 | { |
| 564 | snd_soc_unregister_codec(&spi->dev); |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static struct spi_driver cs4271_spi_driver = { |
| 569 | .driver = { |
| 570 | .name = "cs4271", |
| 571 | .owner = THIS_MODULE, |
| 572 | }, |
| 573 | .probe = cs4271_spi_probe, |
| 574 | .remove = __devexit_p(cs4271_spi_remove), |
| 575 | }; |
| 576 | #endif /* defined(CONFIG_SPI_MASTER) */ |
| 577 | |
| 578 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Axel Lin | 79a54ea | 2011-03-04 15:22:03 +0800 | [diff] [blame] | 579 | static const struct i2c_device_id cs4271_i2c_id[] = { |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 580 | {"cs4271", 0}, |
| 581 | {} |
| 582 | }; |
| 583 | MODULE_DEVICE_TABLE(i2c, cs4271_i2c_id); |
| 584 | |
| 585 | static int __devinit cs4271_i2c_probe(struct i2c_client *client, |
| 586 | const struct i2c_device_id *id) |
| 587 | { |
| 588 | struct cs4271_private *cs4271; |
| 589 | |
| 590 | cs4271 = devm_kzalloc(&client->dev, sizeof(*cs4271), GFP_KERNEL); |
| 591 | if (!cs4271) |
| 592 | return -ENOMEM; |
| 593 | |
| 594 | i2c_set_clientdata(client, cs4271); |
Alexander Sverdlin | 67b2251 | 2011-01-19 21:22:06 +0300 | [diff] [blame] | 595 | cs4271->bus_type = SND_SOC_I2C; |
| 596 | |
| 597 | return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4271, |
| 598 | &cs4271_dai, 1); |
| 599 | } |
| 600 | |
| 601 | static int __devexit cs4271_i2c_remove(struct i2c_client *client) |
| 602 | { |
| 603 | snd_soc_unregister_codec(&client->dev); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static struct i2c_driver cs4271_i2c_driver = { |
| 608 | .driver = { |
| 609 | .name = "cs4271", |
| 610 | .owner = THIS_MODULE, |
| 611 | }, |
| 612 | .id_table = cs4271_i2c_id, |
| 613 | .probe = cs4271_i2c_probe, |
| 614 | .remove = __devexit_p(cs4271_i2c_remove), |
| 615 | }; |
| 616 | #endif /* defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) */ |
| 617 | |
| 618 | /* |
| 619 | * We only register our serial bus driver here without |
| 620 | * assignment to particular chip. So if any of the below |
| 621 | * fails, there is some problem with I2C or SPI subsystem. |
| 622 | * In most cases this module will be compiled with support |
| 623 | * of only one serial bus. |
| 624 | */ |
| 625 | static int __init cs4271_modinit(void) |
| 626 | { |
| 627 | int ret; |
| 628 | |
| 629 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 630 | ret = i2c_add_driver(&cs4271_i2c_driver); |
| 631 | if (ret) { |
| 632 | pr_err("Failed to register CS4271 I2C driver: %d\n", ret); |
| 633 | return ret; |
| 634 | } |
| 635 | #endif |
| 636 | |
| 637 | #if defined(CONFIG_SPI_MASTER) |
| 638 | ret = spi_register_driver(&cs4271_spi_driver); |
| 639 | if (ret) { |
| 640 | pr_err("Failed to register CS4271 SPI driver: %d\n", ret); |
| 641 | return ret; |
| 642 | } |
| 643 | #endif |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | module_init(cs4271_modinit); |
| 648 | |
| 649 | static void __exit cs4271_modexit(void) |
| 650 | { |
| 651 | #if defined(CONFIG_SPI_MASTER) |
| 652 | spi_unregister_driver(&cs4271_spi_driver); |
| 653 | #endif |
| 654 | |
| 655 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 656 | i2c_del_driver(&cs4271_i2c_driver); |
| 657 | #endif |
| 658 | } |
| 659 | module_exit(cs4271_modexit); |
| 660 | |
| 661 | MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>"); |
| 662 | MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver"); |
| 663 | MODULE_LICENSE("GPL"); |