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