Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * CS4270 ALSA SoC (ASoC) codec driver |
| 3 | * |
| 4 | * Author: Timur Tabi <timur@freescale.com> |
| 5 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 6 | * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed |
| 7 | * under the terms of the GNU General Public License version 2. This |
| 8 | * program is licensed "as is" without any warranty of any kind, whether |
| 9 | * express or implied. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 10 | * |
| 11 | * This is an ASoC device driver for the Cirrus Logic CS4270 codec. |
| 12 | * |
| 13 | * Current features/limitations: |
| 14 | * |
Timur Tabi | ff637d38 | 2009-01-22 18:23:39 -0600 | [diff] [blame] | 15 | * 1) Software mode is supported. Stand-alone mode is not supported. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 16 | * 2) Only I2C is supported, not SPI |
| 17 | * 3) Only Master mode is supported, not Slave. |
| 18 | * 4) The machine driver's 'startup' function must call |
| 19 | * cs4270_set_dai_sysclk() with the value of MCLK. |
| 20 | * 5) Only I2S and left-justified modes are supported |
| 21 | * 6) Power management is not supported |
| 22 | * 7) The only supported control is volume and hardware mute (if enabled) |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/initval.h> |
| 30 | #include <linux/i2c.h> |
| 31 | |
Mark Brown | 01e097d | 2009-01-23 15:07:45 +0000 | [diff] [blame] | 32 | #include "cs4270.h" |
| 33 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 34 | /* |
| 35 | * The codec isn't really big-endian or little-endian, since the I2S |
| 36 | * interface requires data to be sent serially with the MSbit first. |
| 37 | * However, to support BE and LE I2S devices, we specify both here. That |
| 38 | * way, ALSA will always match the bit patterns. |
| 39 | */ |
| 40 | #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 41 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 42 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ |
| 43 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 44 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ |
| 45 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) |
| 46 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 47 | /* CS4270 registers addresses */ |
| 48 | #define CS4270_CHIPID 0x01 /* Chip ID */ |
| 49 | #define CS4270_PWRCTL 0x02 /* Power Control */ |
| 50 | #define CS4270_MODE 0x03 /* Mode Control */ |
| 51 | #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */ |
| 52 | #define CS4270_TRANS 0x05 /* Transition Control */ |
| 53 | #define CS4270_MUTE 0x06 /* Mute Control */ |
| 54 | #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */ |
| 55 | #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */ |
| 56 | |
| 57 | #define CS4270_FIRSTREG 0x01 |
| 58 | #define CS4270_LASTREG 0x08 |
| 59 | #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1) |
| 60 | |
| 61 | /* Bit masks for the CS4270 registers */ |
| 62 | #define CS4270_CHIPID_ID 0xF0 |
| 63 | #define CS4270_CHIPID_REV 0x0F |
| 64 | #define CS4270_PWRCTL_FREEZE 0x80 |
| 65 | #define CS4270_PWRCTL_PDN_ADC 0x20 |
| 66 | #define CS4270_PWRCTL_PDN_DAC 0x02 |
| 67 | #define CS4270_PWRCTL_PDN 0x01 |
| 68 | #define CS4270_MODE_SPEED_MASK 0x30 |
| 69 | #define CS4270_MODE_1X 0x00 |
| 70 | #define CS4270_MODE_2X 0x10 |
| 71 | #define CS4270_MODE_4X 0x20 |
| 72 | #define CS4270_MODE_SLAVE 0x30 |
| 73 | #define CS4270_MODE_DIV_MASK 0x0E |
| 74 | #define CS4270_MODE_DIV1 0x00 |
| 75 | #define CS4270_MODE_DIV15 0x02 |
| 76 | #define CS4270_MODE_DIV2 0x04 |
| 77 | #define CS4270_MODE_DIV3 0x06 |
| 78 | #define CS4270_MODE_DIV4 0x08 |
| 79 | #define CS4270_MODE_POPGUARD 0x01 |
| 80 | #define CS4270_FORMAT_FREEZE_A 0x80 |
| 81 | #define CS4270_FORMAT_FREEZE_B 0x40 |
| 82 | #define CS4270_FORMAT_LOOPBACK 0x20 |
| 83 | #define CS4270_FORMAT_DAC_MASK 0x18 |
| 84 | #define CS4270_FORMAT_DAC_LJ 0x00 |
| 85 | #define CS4270_FORMAT_DAC_I2S 0x08 |
| 86 | #define CS4270_FORMAT_DAC_RJ16 0x18 |
| 87 | #define CS4270_FORMAT_DAC_RJ24 0x10 |
| 88 | #define CS4270_FORMAT_ADC_MASK 0x01 |
| 89 | #define CS4270_FORMAT_ADC_LJ 0x00 |
| 90 | #define CS4270_FORMAT_ADC_I2S 0x01 |
| 91 | #define CS4270_TRANS_ONE_VOL 0x80 |
| 92 | #define CS4270_TRANS_SOFT 0x40 |
| 93 | #define CS4270_TRANS_ZERO 0x20 |
| 94 | #define CS4270_TRANS_INV_ADC_A 0x08 |
| 95 | #define CS4270_TRANS_INV_ADC_B 0x10 |
| 96 | #define CS4270_TRANS_INV_DAC_A 0x02 |
| 97 | #define CS4270_TRANS_INV_DAC_B 0x04 |
| 98 | #define CS4270_TRANS_DEEMPH 0x01 |
| 99 | #define CS4270_MUTE_AUTO 0x20 |
| 100 | #define CS4270_MUTE_ADC_A 0x08 |
| 101 | #define CS4270_MUTE_ADC_B 0x10 |
| 102 | #define CS4270_MUTE_POLARITY 0x04 |
| 103 | #define CS4270_MUTE_DAC_A 0x01 |
| 104 | #define CS4270_MUTE_DAC_B 0x02 |
| 105 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 106 | /* Private data for the CS4270 */ |
| 107 | struct cs4270_private { |
| 108 | struct snd_soc_codec codec; |
| 109 | u8 reg_cache[CS4270_NUMREGS]; |
| 110 | unsigned int mclk; /* Input frequency of the MCLK pin */ |
| 111 | unsigned int mode; /* The mode (I2S or left-justified) */ |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 112 | unsigned int slave_mode; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 113 | }; |
| 114 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 115 | /** |
| 116 | * struct cs4270_mode_ratios - clock ratio tables |
| 117 | * @ratio: the ratio of MCLK to the sample rate |
| 118 | * @speed_mode: the Speed Mode bits to set in the Mode Control register for |
| 119 | * this ratio |
| 120 | * @mclk: the Ratio Select bits to set in the Mode Control register for this |
| 121 | * ratio |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 122 | * |
| 123 | * The data for this chart is taken from Table 5 of the CS4270 reference |
| 124 | * manual. |
| 125 | * |
| 126 | * This table is used to determine how to program the Mode Control register. |
| 127 | * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling |
| 128 | * rates the CS4270 currently supports. |
| 129 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 130 | * @speed_mode is the corresponding bit pattern to be written to the |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 131 | * MODE bits of the Mode Control Register |
| 132 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 133 | * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 134 | * the Mode Control Register. |
| 135 | * |
| 136 | * In situations where a single ratio is represented by multiple speed |
| 137 | * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick |
| 138 | * double-speed instead of quad-speed. However, the CS4270 errata states |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 139 | * that divide-By-1.5 can cause failures, so we avoid that mode where |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 140 | * possible. |
| 141 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 142 | * Errata: There is an errata for the CS4270 where divide-by-1.5 does not |
| 143 | * work if Vd is 3.3V. If this effects you, select the |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 144 | * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will |
| 145 | * never select any sample rates that require divide-by-1.5. |
| 146 | */ |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 147 | struct cs4270_mode_ratios { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 148 | unsigned int ratio; |
| 149 | u8 speed_mode; |
| 150 | u8 mclk; |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 151 | }; |
| 152 | |
Timur Tabi | d9fb7fb | 2009-02-02 14:50:45 -0600 | [diff] [blame] | 153 | static struct cs4270_mode_ratios cs4270_mode_ratios[] = { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 154 | {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, |
| 155 | #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA |
| 156 | {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, |
| 157 | #endif |
| 158 | {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, |
| 159 | {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, |
| 160 | {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, |
| 161 | {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, |
| 162 | {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, |
| 163 | {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, |
| 164 | {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} |
| 165 | }; |
| 166 | |
| 167 | /* The number of MCLK/LRCK ratios supported by the CS4270 */ |
| 168 | #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) |
| 169 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 170 | /** |
| 171 | * cs4270_set_dai_sysclk - determine the CS4270 samples rates. |
| 172 | * @codec_dai: the codec DAI |
| 173 | * @clk_id: the clock ID (ignored) |
| 174 | * @freq: the MCLK input frequency |
| 175 | * @dir: the clock direction (ignored) |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 176 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 177 | * This function is used to tell the codec driver what the input MCLK |
| 178 | * frequency is. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 179 | * |
| 180 | * The value of MCLK is used to determine which sample rates are supported |
| 181 | * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 182 | * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 183 | * |
| 184 | * This function calculates the nine ratios and determines which ones match |
| 185 | * a standard sample rate. If there's a match, then it is added to the list |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 186 | * of supported sample rates. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 187 | * |
| 188 | * This function must be called by the machine driver's 'startup' function, |
| 189 | * otherwise the list of supported sample rates will not be available in |
| 190 | * time for ALSA. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 191 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 192 | static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 193 | int clk_id, unsigned int freq, int dir) |
| 194 | { |
| 195 | struct snd_soc_codec *codec = codec_dai->codec; |
| 196 | struct cs4270_private *cs4270 = codec->private_data; |
| 197 | unsigned int rates = 0; |
| 198 | unsigned int rate_min = -1; |
| 199 | unsigned int rate_max = 0; |
| 200 | unsigned int i; |
| 201 | |
| 202 | cs4270->mclk = freq; |
| 203 | |
| 204 | for (i = 0; i < NUM_MCLK_RATIOS; i++) { |
| 205 | unsigned int rate = freq / cs4270_mode_ratios[i].ratio; |
| 206 | rates |= snd_pcm_rate_to_rate_bit(rate); |
| 207 | if (rate < rate_min) |
| 208 | rate_min = rate; |
| 209 | if (rate > rate_max) |
| 210 | rate_max = rate; |
| 211 | } |
| 212 | /* FIXME: soc should support a rate list */ |
| 213 | rates &= ~SNDRV_PCM_RATE_KNOT; |
| 214 | |
| 215 | if (!rates) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 216 | dev_err(codec->dev, "could not find a valid sample rate\n"); |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | codec_dai->playback.rates = rates; |
| 221 | codec_dai->playback.rate_min = rate_min; |
| 222 | codec_dai->playback.rate_max = rate_max; |
| 223 | |
| 224 | codec_dai->capture.rates = rates; |
| 225 | codec_dai->capture.rate_min = rate_min; |
| 226 | codec_dai->capture.rate_max = rate_max; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 231 | /** |
| 232 | * cs4270_set_dai_fmt - configure the codec for the selected audio format |
| 233 | * @codec_dai: the codec DAI |
| 234 | * @format: a SND_SOC_DAIFMT_x value indicating the data format |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 235 | * |
| 236 | * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the |
| 237 | * codec accordingly. |
| 238 | * |
| 239 | * Currently, this function only supports SND_SOC_DAIFMT_I2S and |
| 240 | * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified |
| 241 | * data for playback only, but ASoC currently does not support different |
| 242 | * formats for playback vs. record. |
| 243 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 244 | static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 245 | unsigned int format) |
| 246 | { |
| 247 | struct snd_soc_codec *codec = codec_dai->codec; |
| 248 | struct cs4270_private *cs4270 = codec->private_data; |
| 249 | int ret = 0; |
| 250 | |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 251 | /* set DAI format */ |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 252 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 253 | case SND_SOC_DAIFMT_I2S: |
| 254 | case SND_SOC_DAIFMT_LEFT_J: |
| 255 | cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; |
| 256 | break; |
| 257 | default: |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 258 | dev_err(codec->dev, "invalid dai format\n"); |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 259 | ret = -EINVAL; |
| 260 | } |
| 261 | |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 262 | /* set master/slave audio interface */ |
| 263 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { |
| 264 | case SND_SOC_DAIFMT_CBS_CFS: |
| 265 | cs4270->slave_mode = 1; |
| 266 | break; |
| 267 | case SND_SOC_DAIFMT_CBM_CFM: |
| 268 | cs4270->slave_mode = 0; |
| 269 | break; |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 270 | default: |
Daniel Mack | ff09d49 | 2009-02-28 13:21:03 +0100 | [diff] [blame] | 271 | /* all other modes are unsupported by the hardware */ |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 272 | ret = -EINVAL; |
| 273 | } |
| 274 | |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 278 | /** |
| 279 | * cs4270_fill_cache - pre-fill the CS4270 register cache. |
| 280 | * @codec: the codec for this CS4270 |
| 281 | * |
| 282 | * This function fills in the CS4270 register cache by reading the register |
| 283 | * values from the hardware. |
| 284 | * |
| 285 | * This CS4270 registers are cached to avoid excessive I2C I/O operations. |
| 286 | * After the initial read to pre-fill the cache, the CS4270 never updates |
| 287 | * the register values, so we won't have a cache coherency problem. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 288 | * |
| 289 | * We use the auto-increment feature of the CS4270 to read all registers in |
| 290 | * one shot. |
| 291 | */ |
| 292 | static int cs4270_fill_cache(struct snd_soc_codec *codec) |
| 293 | { |
| 294 | u8 *cache = codec->reg_cache; |
| 295 | struct i2c_client *i2c_client = codec->control_data; |
| 296 | s32 length; |
| 297 | |
| 298 | length = i2c_smbus_read_i2c_block_data(i2c_client, |
| 299 | CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache); |
| 300 | |
| 301 | if (length != CS4270_NUMREGS) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 302 | dev_err(codec->dev, "i2c read failure, addr=0x%x\n", |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 303 | i2c_client->addr); |
| 304 | return -EIO; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 310 | /** |
| 311 | * cs4270_read_reg_cache - read from the CS4270 register cache. |
| 312 | * @codec: the codec for this CS4270 |
| 313 | * @reg: the register to read |
| 314 | * |
| 315 | * This function returns the value for a given register. It reads only from |
| 316 | * the register cache, not the hardware itself. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 317 | * |
| 318 | * This CS4270 registers are cached to avoid excessive I2C I/O operations. |
| 319 | * After the initial read to pre-fill the cache, the CS4270 never updates |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 320 | * the register values, so we won't have a cache coherency problem. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 321 | */ |
| 322 | static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec, |
| 323 | unsigned int reg) |
| 324 | { |
| 325 | u8 *cache = codec->reg_cache; |
| 326 | |
| 327 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) |
| 328 | return -EIO; |
| 329 | |
| 330 | return cache[reg - CS4270_FIRSTREG]; |
| 331 | } |
| 332 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 333 | /** |
| 334 | * cs4270_i2c_write - write to a CS4270 register via the I2C bus. |
| 335 | * @codec: the codec for this CS4270 |
| 336 | * @reg: the register to write |
| 337 | * @value: the value to write to the register |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 338 | * |
| 339 | * This function writes the given value to the given CS4270 register, and |
| 340 | * also updates the register cache. |
| 341 | * |
| 342 | * Note that we don't use the hw_write function pointer of snd_soc_codec. |
| 343 | * That's because it's too clunky: the hw_write_t prototype does not match |
| 344 | * i2c_smbus_write_byte_data(), and it's just another layer of overhead. |
| 345 | */ |
| 346 | static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg, |
| 347 | unsigned int value) |
| 348 | { |
Timur Tabi | bfc4e86 | 2007-09-11 00:45:50 +0200 | [diff] [blame] | 349 | u8 *cache = codec->reg_cache; |
| 350 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 351 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) |
| 352 | return -EIO; |
| 353 | |
Timur Tabi | bfc4e86 | 2007-09-11 00:45:50 +0200 | [diff] [blame] | 354 | /* Only perform an I2C operation if the new value is different */ |
| 355 | if (cache[reg - CS4270_FIRSTREG] != value) { |
| 356 | struct i2c_client *client = codec->control_data; |
| 357 | if (i2c_smbus_write_byte_data(client, reg, value)) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 358 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | bfc4e86 | 2007-09-11 00:45:50 +0200 | [diff] [blame] | 359 | return -EIO; |
| 360 | } |
| 361 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 362 | /* We've written to the hardware, so update the cache */ |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 363 | cache[reg - CS4270_FIRSTREG] = value; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 364 | } |
Timur Tabi | bfc4e86 | 2007-09-11 00:45:50 +0200 | [diff] [blame] | 365 | |
| 366 | return 0; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 369 | /** |
| 370 | * cs4270_hw_params - program the CS4270 with the given hardware parameters. |
| 371 | * @substream: the audio stream |
| 372 | * @params: the hardware parameters to set |
| 373 | * @dai: the SOC DAI (ignored) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 374 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 375 | * This function programs the hardware with the values provided. |
| 376 | * Specifically, the sample rate and the data format. |
| 377 | * |
| 378 | * The .ops functions are used to provide board-specific data, like input |
| 379 | * frequencies, to this driver. This function takes that information, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 380 | * combines it with the hardware parameters provided, and programs the |
| 381 | * hardware accordingly. |
| 382 | */ |
| 383 | static int cs4270_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 384 | struct snd_pcm_hw_params *params, |
| 385 | struct snd_soc_dai *dai) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 386 | { |
| 387 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 388 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 389 | struct snd_soc_codec *codec = socdev->card->codec; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 390 | struct cs4270_private *cs4270 = codec->private_data; |
Roel Kluin | e34ba21 | 2008-04-17 18:58:34 +0200 | [diff] [blame] | 391 | int ret; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 392 | unsigned int i; |
| 393 | unsigned int rate; |
| 394 | unsigned int ratio; |
| 395 | int reg; |
| 396 | |
| 397 | /* Figure out which MCLK/LRCK ratio to use */ |
| 398 | |
| 399 | rate = params_rate(params); /* Sampling rate, in Hz */ |
| 400 | ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ |
| 401 | |
Timur Tabi | 9dbd627 | 2007-08-01 12:22:07 +0200 | [diff] [blame] | 402 | for (i = 0; i < NUM_MCLK_RATIOS; i++) { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 403 | if (cs4270_mode_ratios[i].ratio == ratio) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | |
Timur Tabi | 9dbd627 | 2007-08-01 12:22:07 +0200 | [diff] [blame] | 407 | if (i == NUM_MCLK_RATIOS) { |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 408 | /* We did not find a matching ratio */ |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 409 | dev_err(codec->dev, "could not find matching ratio\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 410 | return -EINVAL; |
| 411 | } |
| 412 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 413 | /* Set the sample rate */ |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 414 | |
| 415 | reg = snd_soc_read(codec, CS4270_MODE); |
| 416 | reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 417 | reg |= cs4270_mode_ratios[i].mclk; |
| 418 | |
| 419 | if (cs4270->slave_mode) |
| 420 | reg |= CS4270_MODE_SLAVE; |
| 421 | else |
| 422 | reg |= cs4270_mode_ratios[i].speed_mode; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 423 | |
| 424 | ret = snd_soc_write(codec, CS4270_MODE, reg); |
| 425 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 426 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 430 | /* Set the DAI format */ |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 431 | |
| 432 | reg = snd_soc_read(codec, CS4270_FORMAT); |
| 433 | reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); |
| 434 | |
| 435 | switch (cs4270->mode) { |
| 436 | case SND_SOC_DAIFMT_I2S: |
| 437 | reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; |
| 438 | break; |
| 439 | case SND_SOC_DAIFMT_LEFT_J: |
| 440 | reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; |
| 441 | break; |
| 442 | default: |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 443 | dev_err(codec->dev, "unknown dai format\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 444 | return -EINVAL; |
| 445 | } |
| 446 | |
| 447 | ret = snd_soc_write(codec, CS4270_FORMAT, reg); |
| 448 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 449 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 450 | return ret; |
| 451 | } |
| 452 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 453 | return ret; |
| 454 | } |
| 455 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 456 | /** |
| 457 | * cs4270_mute - enable/disable the CS4270 external mute |
| 458 | * @dai: the SOC DAI |
| 459 | * @mute: 0 = disable mute, 1 = enable mute |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 460 | * |
| 461 | * This function toggles the mute bits in the MUTE register. The CS4270's |
| 462 | * mute capability is intended for external muting circuitry, so if the |
| 463 | * board does not have the MUTEA or MUTEB pins connected to such circuitry, |
| 464 | * then this function will do nothing. |
| 465 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 466 | static int cs4270_mute(struct snd_soc_dai *dai, int mute) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 467 | { |
| 468 | struct snd_soc_codec *codec = dai->codec; |
| 469 | int reg6; |
| 470 | |
| 471 | reg6 = snd_soc_read(codec, CS4270_MUTE); |
| 472 | |
| 473 | if (mute) |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 474 | reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 475 | else |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 476 | reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 477 | |
| 478 | return snd_soc_write(codec, CS4270_MUTE, reg6); |
| 479 | } |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 480 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 481 | /* A list of non-DAPM controls that the CS4270 supports */ |
| 482 | static const struct snd_kcontrol_new cs4270_snd_controls[] = { |
| 483 | SOC_DOUBLE_R("Master Playback Volume", |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 484 | CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), |
| 485 | SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), |
| 486 | SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), |
| 487 | SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), |
| 488 | SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), |
| 489 | SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), |
| 490 | SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 0) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 491 | }; |
| 492 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 493 | /* |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 494 | * cs4270_codec - global variable to store codec for the ASoC probe function |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 495 | * |
| 496 | * If struct i2c_driver had a private_data field, we wouldn't need to use |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 497 | * cs4270_codec. This is the only way to pass the codec structure from |
| 498 | * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good |
| 499 | * way to synchronize these two functions. cs4270_i2c_probe() can be called |
| 500 | * multiple times before cs4270_probe() is called even once. So for now, we |
| 501 | * also only allow cs4270_i2c_probe() to be run once. That means that we do |
| 502 | * not support more than one cs4270 device in the system, at least for now. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 503 | */ |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 504 | static struct snd_soc_codec *cs4270_codec; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 505 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame^] | 506 | static struct snd_soc_dai_ops cs4270_dai_ops = { |
| 507 | .hw_params = cs4270_hw_params, |
| 508 | .set_sysclk = cs4270_set_dai_sysclk, |
| 509 | .set_fmt = cs4270_set_dai_fmt, |
| 510 | .digital_mute = cs4270_mute, |
| 511 | }; |
| 512 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 513 | struct snd_soc_dai cs4270_dai = { |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 514 | .name = "cs4270", |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 515 | .playback = { |
| 516 | .stream_name = "Playback", |
| 517 | .channels_min = 1, |
| 518 | .channels_max = 2, |
| 519 | .rates = 0, |
| 520 | .formats = CS4270_FORMATS, |
| 521 | }, |
| 522 | .capture = { |
| 523 | .stream_name = "Capture", |
| 524 | .channels_min = 1, |
| 525 | .channels_max = 2, |
| 526 | .rates = 0, |
| 527 | .formats = CS4270_FORMATS, |
| 528 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame^] | 529 | .ops = &cs4270_dai_ops, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 530 | }; |
| 531 | EXPORT_SYMBOL_GPL(cs4270_dai); |
| 532 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 533 | /** |
| 534 | * cs4270_probe - ASoC probe function |
| 535 | * @pdev: platform device |
| 536 | * |
| 537 | * This function is called when ASoC has all the pieces it needs to |
| 538 | * instantiate a sound driver. |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 539 | */ |
| 540 | static int cs4270_probe(struct platform_device *pdev) |
| 541 | { |
| 542 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 543 | struct snd_soc_codec *codec = cs4270_codec; |
| 544 | unsigned int i; |
| 545 | int ret; |
| 546 | |
| 547 | /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */ |
| 548 | socdev->card->codec = codec; |
| 549 | |
| 550 | /* Register PCMs */ |
| 551 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 552 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 553 | dev_err(codec->dev, "failed to create pcms\n"); |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | /* Add the non-DAPM controls */ |
| 558 | for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) { |
| 559 | struct snd_kcontrol *kctrl; |
| 560 | |
| 561 | kctrl = snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL); |
| 562 | if (!kctrl) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 563 | dev_err(codec->dev, "error creating control '%s'\n", |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 564 | cs4270_snd_controls[i].name); |
| 565 | ret = -ENOMEM; |
| 566 | goto error_free_pcms; |
| 567 | } |
| 568 | |
| 569 | ret = snd_ctl_add(codec->card, kctrl); |
| 570 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 571 | dev_err(codec->dev, "error adding control '%s'\n", |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 572 | cs4270_snd_controls[i].name); |
| 573 | goto error_free_pcms; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* And finally, register the socdev */ |
| 578 | ret = snd_soc_init_card(socdev); |
| 579 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 580 | dev_err(codec->dev, "failed to register card\n"); |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 581 | goto error_free_pcms; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | |
| 586 | error_free_pcms: |
| 587 | snd_soc_free_pcms(socdev); |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 592 | /** |
| 593 | * cs4270_remove - ASoC remove function |
| 594 | * @pdev: platform device |
| 595 | * |
| 596 | * This function is the counterpart to cs4270_probe(). |
| 597 | */ |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 598 | static int cs4270_remove(struct platform_device *pdev) |
| 599 | { |
| 600 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 601 | |
| 602 | snd_soc_free_pcms(socdev); |
| 603 | |
| 604 | return 0; |
| 605 | }; |
| 606 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 607 | /** |
| 608 | * cs4270_i2c_probe - initialize the I2C interface of the CS4270 |
| 609 | * @i2c_client: the I2C client object |
| 610 | * @id: the I2C device ID (ignored) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 611 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 612 | * This function is called whenever the I2C subsystem finds a device that |
| 613 | * matches the device ID given via a prior call to i2c_add_driver(). |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 614 | */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 615 | static int cs4270_i2c_probe(struct i2c_client *i2c_client, |
| 616 | const struct i2c_device_id *id) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 617 | { |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 618 | struct snd_soc_codec *codec; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 619 | struct cs4270_private *cs4270; |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 620 | unsigned int reg; |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 621 | int ret; |
| 622 | |
| 623 | /* For now, we only support one cs4270 device in the system. See the |
| 624 | * comment for cs4270_codec. |
| 625 | */ |
| 626 | if (cs4270_codec) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 627 | dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n", |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 628 | i2c_client->addr); |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 629 | dev_err(&i2c_client->dev, "only one per board allowed\n"); |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 630 | /* Should we return something other than ENODEV here? */ |
| 631 | return -ENODEV; |
| 632 | } |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 633 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 634 | /* Verify that we have a CS4270 */ |
| 635 | |
| 636 | ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID); |
| 637 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 638 | dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 639 | i2c_client->addr); |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 640 | return ret; |
| 641 | } |
| 642 | /* The top four bits of the chip ID should be 1100. */ |
| 643 | if ((ret & 0xF0) != 0xC0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 644 | dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 645 | i2c_client->addr); |
| 646 | return -ENODEV; |
| 647 | } |
| 648 | |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 649 | dev_info(&i2c_client->dev, "found device at i2c address %X\n", |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 650 | i2c_client->addr); |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 651 | dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 652 | |
| 653 | /* Allocate enough space for the snd_soc_codec structure |
| 654 | and our private data together. */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 655 | cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL); |
| 656 | if (!cs4270) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 657 | dev_err(&i2c_client->dev, "could not allocate codec\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 658 | return -ENOMEM; |
| 659 | } |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 660 | codec = &cs4270->codec; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 661 | |
| 662 | mutex_init(&codec->mutex); |
| 663 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 664 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 665 | |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 666 | codec->dev = &i2c_client->dev; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 667 | codec->name = "CS4270"; |
| 668 | codec->owner = THIS_MODULE; |
| 669 | codec->dai = &cs4270_dai; |
| 670 | codec->num_dai = 1; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 671 | codec->private_data = cs4270; |
| 672 | codec->control_data = i2c_client; |
| 673 | codec->read = cs4270_read_reg_cache; |
| 674 | codec->write = cs4270_i2c_write; |
| 675 | codec->reg_cache = cs4270->reg_cache; |
| 676 | codec->reg_cache_size = CS4270_NUMREGS; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 677 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 678 | /* The I2C interface is set up, so pre-fill our register cache */ |
| 679 | |
| 680 | ret = cs4270_fill_cache(codec); |
| 681 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 682 | dev_err(&i2c_client->dev, "failed to fill register cache\n"); |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 683 | goto error_free_codec; |
| 684 | } |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 685 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 686 | /* Disable auto-mute. This feature appears to be buggy. In some |
| 687 | * situations, auto-mute will not deactivate when it should, so we want |
| 688 | * this feature disabled by default. An application (e.g. alsactl) can |
| 689 | * re-enabled it by using the controls. |
| 690 | */ |
| 691 | |
| 692 | reg = cs4270_read_reg_cache(codec, CS4270_MUTE); |
| 693 | reg &= ~CS4270_MUTE_AUTO; |
| 694 | ret = cs4270_i2c_write(codec, CS4270_MUTE, reg); |
| 695 | if (ret < 0) { |
| 696 | dev_err(&i2c_client->dev, "i2c write failed\n"); |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | /* Disable automatic volume control. The hardware enables, and it |
| 701 | * causes volume change commands to be delayed, sometimes until after |
| 702 | * playback has started. An application (e.g. alsactl) can |
| 703 | * re-enabled it by using the controls. |
| 704 | */ |
| 705 | |
| 706 | reg = cs4270_read_reg_cache(codec, CS4270_TRANS); |
| 707 | reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO); |
| 708 | ret = cs4270_i2c_write(codec, CS4270_TRANS, reg); |
| 709 | if (ret < 0) { |
| 710 | dev_err(&i2c_client->dev, "i2c write failed\n"); |
| 711 | return ret; |
| 712 | } |
| 713 | |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 714 | /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI |
| 715 | * structure for each CS4270 device, but the machine driver needs to |
| 716 | * have a pointer to the DAI structure, so for now it must be a global |
| 717 | * variable. |
| 718 | */ |
| 719 | cs4270_dai.dev = &i2c_client->dev; |
| 720 | |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 721 | /* Register the DAI. If all the other ASoC driver have already |
| 722 | * registered, then this will call our probe function, so |
| 723 | * cs4270_codec needs to be ready. |
| 724 | */ |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 725 | cs4270_codec = codec; |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 726 | ret = snd_soc_register_dai(&cs4270_dai); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 727 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 728 | dev_err(&i2c_client->dev, "failed to register DAIe\n"); |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 729 | goto error_free_codec; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 732 | i2c_set_clientdata(i2c_client, cs4270); |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 733 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 734 | return 0; |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 735 | |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 736 | error_free_codec: |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 737 | kfree(cs4270); |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 738 | cs4270_codec = NULL; |
| 739 | cs4270_dai.dev = NULL; |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 740 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 741 | return ret; |
| 742 | } |
| 743 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 744 | /** |
| 745 | * cs4270_i2c_remove - remove an I2C device |
| 746 | * @i2c_client: the I2C client object |
| 747 | * |
| 748 | * This function is the counterpart to cs4270_i2c_probe(). |
| 749 | */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 750 | static int cs4270_i2c_remove(struct i2c_client *i2c_client) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 751 | { |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 752 | struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 753 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 754 | kfree(cs4270); |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 755 | cs4270_codec = NULL; |
| 756 | cs4270_dai.dev = NULL; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 757 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 758 | return 0; |
| 759 | } |
| 760 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 761 | /* |
| 762 | * cs4270_id - I2C device IDs supported by this driver |
| 763 | */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 764 | static struct i2c_device_id cs4270_id[] = { |
| 765 | {"cs4270", 0}, |
| 766 | {} |
| 767 | }; |
| 768 | MODULE_DEVICE_TABLE(i2c, cs4270_id); |
| 769 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 770 | /* |
| 771 | * cs4270_i2c_driver - I2C device identification |
| 772 | * |
| 773 | * This structure tells the I2C subsystem how to identify and support a |
| 774 | * given I2C device type. |
| 775 | */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 776 | static struct i2c_driver cs4270_i2c_driver = { |
| 777 | .driver = { |
| 778 | .name = "cs4270", |
| 779 | .owner = THIS_MODULE, |
| 780 | }, |
| 781 | .id_table = cs4270_id, |
| 782 | .probe = cs4270_i2c_probe, |
| 783 | .remove = cs4270_i2c_remove, |
| 784 | }; |
| 785 | |
| 786 | /* |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 787 | * ASoC codec device structure |
| 788 | * |
| 789 | * Assign this variable to the codec_dev field of the machine driver's |
| 790 | * snd_soc_device structure. |
| 791 | */ |
| 792 | struct snd_soc_codec_device soc_codec_device_cs4270 = { |
| 793 | .probe = cs4270_probe, |
| 794 | .remove = cs4270_remove |
| 795 | }; |
| 796 | EXPORT_SYMBOL_GPL(soc_codec_device_cs4270); |
| 797 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 798 | static int __init cs4270_init(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 799 | { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 800 | pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n"); |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 801 | |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 802 | return i2c_add_driver(&cs4270_i2c_driver); |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 803 | } |
| 804 | module_init(cs4270_init); |
| 805 | |
| 806 | static void __exit cs4270_exit(void) |
| 807 | { |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 808 | i2c_del_driver(&cs4270_i2c_driver); |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 809 | } |
| 810 | module_exit(cs4270_exit); |
| 811 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 812 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
| 813 | MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); |
| 814 | MODULE_LICENSE("GPL"); |