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