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