1b0c813ceSTimur Tabi /* 2b0c813ceSTimur Tabi * CS4270 ALSA SoC (ASoC) codec driver 3b0c813ceSTimur Tabi * 4b0c813ceSTimur Tabi * Author: Timur Tabi <timur@freescale.com> 5b0c813ceSTimur Tabi * 6ff7bf02fSTimur Tabi * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed 7ff7bf02fSTimur Tabi * under the terms of the GNU General Public License version 2. This 8ff7bf02fSTimur Tabi * program is licensed "as is" without any warranty of any kind, whether 9ff7bf02fSTimur Tabi * express or implied. 10b0c813ceSTimur Tabi * 11b0c813ceSTimur Tabi * This is an ASoC device driver for the Cirrus Logic CS4270 codec. 12b0c813ceSTimur Tabi * 13b0c813ceSTimur Tabi * Current features/limitations: 14b0c813ceSTimur Tabi * 15ff637d38STimur Tabi * 1) Software mode is supported. Stand-alone mode is not supported. 16b0c813ceSTimur Tabi * 2) Only I2C is supported, not SPI 17b0c813ceSTimur Tabi * 3) Only Master mode is supported, not Slave. 18b0c813ceSTimur Tabi * 4) The machine driver's 'startup' function must call 19b0c813ceSTimur Tabi * cs4270_set_dai_sysclk() with the value of MCLK. 20b0c813ceSTimur Tabi * 5) Only I2S and left-justified modes are supported 21b0c813ceSTimur Tabi * 6) Power management is not supported 22b0c813ceSTimur Tabi * 7) The only supported control is volume and hardware mute (if enabled) 23b0c813ceSTimur Tabi */ 24b0c813ceSTimur Tabi 25b0c813ceSTimur Tabi #include <linux/module.h> 26b0c813ceSTimur Tabi #include <linux/platform_device.h> 27b0c813ceSTimur Tabi #include <sound/core.h> 28b0c813ceSTimur Tabi #include <sound/soc.h> 29b0c813ceSTimur Tabi #include <sound/initval.h> 30b0c813ceSTimur Tabi #include <linux/i2c.h> 31b0c813ceSTimur Tabi 3201e097d6SMark Brown #include "cs4270.h" 3301e097d6SMark Brown 34b0c813ceSTimur Tabi /* 35b0c813ceSTimur Tabi * The codec isn't really big-endian or little-endian, since the I2S 36b0c813ceSTimur Tabi * interface requires data to be sent serially with the MSbit first. 37b0c813ceSTimur Tabi * However, to support BE and LE I2S devices, we specify both here. That 38b0c813ceSTimur Tabi * way, ALSA will always match the bit patterns. 39b0c813ceSTimur Tabi */ 40b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 41b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ 42b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ 43b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ 44b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ 45b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) 46b0c813ceSTimur Tabi 47b0c813ceSTimur Tabi /* CS4270 registers addresses */ 48b0c813ceSTimur Tabi #define CS4270_CHIPID 0x01 /* Chip ID */ 49b0c813ceSTimur Tabi #define CS4270_PWRCTL 0x02 /* Power Control */ 50b0c813ceSTimur Tabi #define CS4270_MODE 0x03 /* Mode Control */ 51b0c813ceSTimur Tabi #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */ 52b0c813ceSTimur Tabi #define CS4270_TRANS 0x05 /* Transition Control */ 53b0c813ceSTimur Tabi #define CS4270_MUTE 0x06 /* Mute Control */ 54b0c813ceSTimur Tabi #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */ 55b0c813ceSTimur Tabi #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */ 56b0c813ceSTimur Tabi 57b0c813ceSTimur Tabi #define CS4270_FIRSTREG 0x01 58b0c813ceSTimur Tabi #define CS4270_LASTREG 0x08 59b0c813ceSTimur Tabi #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1) 60b0c813ceSTimur Tabi 61b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */ 62b0c813ceSTimur Tabi #define CS4270_CHIPID_ID 0xF0 63b0c813ceSTimur Tabi #define CS4270_CHIPID_REV 0x0F 64b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE 0x80 65b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC 0x20 66b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC 0x02 67b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN 0x01 68b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK 0x30 69b0c813ceSTimur Tabi #define CS4270_MODE_1X 0x00 70b0c813ceSTimur Tabi #define CS4270_MODE_2X 0x10 71b0c813ceSTimur Tabi #define CS4270_MODE_4X 0x20 72b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE 0x30 73b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK 0x0E 74b0c813ceSTimur Tabi #define CS4270_MODE_DIV1 0x00 75b0c813ceSTimur Tabi #define CS4270_MODE_DIV15 0x02 76b0c813ceSTimur Tabi #define CS4270_MODE_DIV2 0x04 77b0c813ceSTimur Tabi #define CS4270_MODE_DIV3 0x06 78b0c813ceSTimur Tabi #define CS4270_MODE_DIV4 0x08 79b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD 0x01 80b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A 0x80 81b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B 0x40 82b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK 0x20 83b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK 0x18 84b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ 0x00 85b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S 0x08 86b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16 0x18 87b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24 0x10 88b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK 0x01 89b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ 0x00 90b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S 0x01 91b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL 0x80 92b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT 0x40 93b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO 0x20 94b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A 0x08 95b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B 0x10 96b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A 0x02 97b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B 0x04 98b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH 0x01 99b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO 0x20 100b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A 0x08 101b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B 0x10 102b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY 0x04 103b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A 0x01 104b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B 0x02 105b0c813ceSTimur Tabi 1060db4d070STimur Tabi /* Private data for the CS4270 */ 1070db4d070STimur Tabi struct cs4270_private { 1080db4d070STimur Tabi struct snd_soc_codec codec; 1090db4d070STimur Tabi u8 reg_cache[CS4270_NUMREGS]; 1100db4d070STimur Tabi unsigned int mclk; /* Input frequency of the MCLK pin */ 1110db4d070STimur Tabi unsigned int mode; /* The mode (I2S or left-justified) */ 1124eae080dSDaniel Mack unsigned int slave_mode; 1130db4d070STimur Tabi }; 1140db4d070STimur Tabi 115ff7bf02fSTimur Tabi /** 116ff7bf02fSTimur Tabi * struct cs4270_mode_ratios - clock ratio tables 117ff7bf02fSTimur Tabi * @ratio: the ratio of MCLK to the sample rate 118ff7bf02fSTimur Tabi * @speed_mode: the Speed Mode bits to set in the Mode Control register for 119ff7bf02fSTimur Tabi * this ratio 120ff7bf02fSTimur Tabi * @mclk: the Ratio Select bits to set in the Mode Control register for this 121ff7bf02fSTimur Tabi * ratio 1228432395fSTimur Tabi * 1238432395fSTimur Tabi * The data for this chart is taken from Table 5 of the CS4270 reference 1248432395fSTimur Tabi * manual. 1258432395fSTimur Tabi * 1268432395fSTimur Tabi * This table is used to determine how to program the Mode Control register. 1278432395fSTimur Tabi * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling 1288432395fSTimur Tabi * rates the CS4270 currently supports. 1298432395fSTimur Tabi * 130ff7bf02fSTimur Tabi * @speed_mode is the corresponding bit pattern to be written to the 1318432395fSTimur Tabi * MODE bits of the Mode Control Register 1328432395fSTimur Tabi * 133ff7bf02fSTimur Tabi * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of 1348432395fSTimur Tabi * the Mode Control Register. 1358432395fSTimur Tabi * 1368432395fSTimur Tabi * In situations where a single ratio is represented by multiple speed 1378432395fSTimur Tabi * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick 1388432395fSTimur Tabi * double-speed instead of quad-speed. However, the CS4270 errata states 139ff7bf02fSTimur Tabi * that divide-By-1.5 can cause failures, so we avoid that mode where 1408432395fSTimur Tabi * possible. 1418432395fSTimur Tabi * 142ff7bf02fSTimur Tabi * Errata: There is an errata for the CS4270 where divide-by-1.5 does not 143ff7bf02fSTimur Tabi * work if Vd is 3.3V. If this effects you, select the 1448432395fSTimur Tabi * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will 1458432395fSTimur Tabi * never select any sample rates that require divide-by-1.5. 1468432395fSTimur Tabi */ 147ff7bf02fSTimur Tabi struct cs4270_mode_ratios { 1488432395fSTimur Tabi unsigned int ratio; 1498432395fSTimur Tabi u8 speed_mode; 1508432395fSTimur Tabi u8 mclk; 151ff7bf02fSTimur Tabi }; 152ff7bf02fSTimur Tabi 153d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = { 1548432395fSTimur Tabi {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, 1558432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA 1568432395fSTimur Tabi {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, 1578432395fSTimur Tabi #endif 1588432395fSTimur Tabi {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, 1598432395fSTimur Tabi {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, 1608432395fSTimur Tabi {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, 1618432395fSTimur Tabi {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, 1628432395fSTimur Tabi {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, 1638432395fSTimur Tabi {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, 1648432395fSTimur Tabi {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} 1658432395fSTimur Tabi }; 1668432395fSTimur Tabi 1678432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */ 1688432395fSTimur Tabi #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) 1698432395fSTimur Tabi 170ff7bf02fSTimur Tabi /** 171ff7bf02fSTimur Tabi * cs4270_set_dai_sysclk - determine the CS4270 samples rates. 172ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 173ff7bf02fSTimur Tabi * @clk_id: the clock ID (ignored) 174ff7bf02fSTimur Tabi * @freq: the MCLK input frequency 175ff7bf02fSTimur Tabi * @dir: the clock direction (ignored) 1768432395fSTimur Tabi * 177ff7bf02fSTimur Tabi * This function is used to tell the codec driver what the input MCLK 178ff7bf02fSTimur Tabi * frequency is. 1798432395fSTimur Tabi * 1808432395fSTimur Tabi * The value of MCLK is used to determine which sample rates are supported 1818432395fSTimur Tabi * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine 182ff7bf02fSTimur Tabi * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. 1838432395fSTimur Tabi * 1848432395fSTimur Tabi * This function calculates the nine ratios and determines which ones match 1858432395fSTimur Tabi * a standard sample rate. If there's a match, then it is added to the list 186ff7bf02fSTimur Tabi * of supported sample rates. 1878432395fSTimur Tabi * 1888432395fSTimur Tabi * This function must be called by the machine driver's 'startup' function, 1898432395fSTimur Tabi * otherwise the list of supported sample rates will not be available in 1908432395fSTimur Tabi * time for ALSA. 1918432395fSTimur Tabi */ 192e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, 1938432395fSTimur Tabi int clk_id, unsigned int freq, int dir) 1948432395fSTimur Tabi { 1958432395fSTimur Tabi struct snd_soc_codec *codec = codec_dai->codec; 1968432395fSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 1978432395fSTimur Tabi unsigned int rates = 0; 1988432395fSTimur Tabi unsigned int rate_min = -1; 1998432395fSTimur Tabi unsigned int rate_max = 0; 2008432395fSTimur Tabi unsigned int i; 2018432395fSTimur Tabi 2028432395fSTimur Tabi cs4270->mclk = freq; 2038432395fSTimur Tabi 2048432395fSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) { 2058432395fSTimur Tabi unsigned int rate = freq / cs4270_mode_ratios[i].ratio; 2068432395fSTimur Tabi rates |= snd_pcm_rate_to_rate_bit(rate); 2078432395fSTimur Tabi if (rate < rate_min) 2088432395fSTimur Tabi rate_min = rate; 2098432395fSTimur Tabi if (rate > rate_max) 2108432395fSTimur Tabi rate_max = rate; 2118432395fSTimur Tabi } 2128432395fSTimur Tabi /* FIXME: soc should support a rate list */ 2138432395fSTimur Tabi rates &= ~SNDRV_PCM_RATE_KNOT; 2148432395fSTimur Tabi 2158432395fSTimur Tabi if (!rates) { 216a6c255e0STimur Tabi dev_err(codec->dev, "could not find a valid sample rate\n"); 2178432395fSTimur Tabi return -EINVAL; 2188432395fSTimur Tabi } 2198432395fSTimur Tabi 2208432395fSTimur Tabi codec_dai->playback.rates = rates; 2218432395fSTimur Tabi codec_dai->playback.rate_min = rate_min; 2228432395fSTimur Tabi codec_dai->playback.rate_max = rate_max; 2238432395fSTimur Tabi 2248432395fSTimur Tabi codec_dai->capture.rates = rates; 2258432395fSTimur Tabi codec_dai->capture.rate_min = rate_min; 2268432395fSTimur Tabi codec_dai->capture.rate_max = rate_max; 2278432395fSTimur Tabi 2288432395fSTimur Tabi return 0; 2298432395fSTimur Tabi } 2308432395fSTimur Tabi 231ff7bf02fSTimur Tabi /** 232ff7bf02fSTimur Tabi * cs4270_set_dai_fmt - configure the codec for the selected audio format 233ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 234ff7bf02fSTimur Tabi * @format: a SND_SOC_DAIFMT_x value indicating the data format 2358432395fSTimur Tabi * 2368432395fSTimur Tabi * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the 2378432395fSTimur Tabi * codec accordingly. 2388432395fSTimur Tabi * 2398432395fSTimur Tabi * Currently, this function only supports SND_SOC_DAIFMT_I2S and 2408432395fSTimur Tabi * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified 2418432395fSTimur Tabi * data for playback only, but ASoC currently does not support different 2428432395fSTimur Tabi * formats for playback vs. record. 2438432395fSTimur Tabi */ 244e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, 2458432395fSTimur Tabi unsigned int format) 2468432395fSTimur Tabi { 2478432395fSTimur Tabi struct snd_soc_codec *codec = codec_dai->codec; 2488432395fSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 2498432395fSTimur Tabi int ret = 0; 2508432395fSTimur Tabi 2514eae080dSDaniel Mack /* set DAI format */ 2528432395fSTimur Tabi switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 2538432395fSTimur Tabi case SND_SOC_DAIFMT_I2S: 2548432395fSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 2558432395fSTimur Tabi cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; 2568432395fSTimur Tabi break; 2578432395fSTimur Tabi default: 258a6c255e0STimur Tabi dev_err(codec->dev, "invalid dai format\n"); 2598432395fSTimur Tabi ret = -EINVAL; 2608432395fSTimur Tabi } 2618432395fSTimur Tabi 2624eae080dSDaniel Mack /* set master/slave audio interface */ 2634eae080dSDaniel Mack switch (format & SND_SOC_DAIFMT_MASTER_MASK) { 2644eae080dSDaniel Mack case SND_SOC_DAIFMT_CBS_CFS: 2654eae080dSDaniel Mack cs4270->slave_mode = 1; 2664eae080dSDaniel Mack break; 2674eae080dSDaniel Mack case SND_SOC_DAIFMT_CBM_CFM: 2684eae080dSDaniel Mack cs4270->slave_mode = 0; 2694eae080dSDaniel Mack break; 2704eae080dSDaniel Mack default: 271*ff09d49aSDaniel Mack /* all other modes are unsupported by the hardware */ 2724eae080dSDaniel Mack ret = -EINVAL; 2734eae080dSDaniel Mack } 2744eae080dSDaniel Mack 2758432395fSTimur Tabi return ret; 2768432395fSTimur Tabi } 2778432395fSTimur Tabi 278ff7bf02fSTimur Tabi /** 279ff7bf02fSTimur Tabi * cs4270_fill_cache - pre-fill the CS4270 register cache. 280ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 281ff7bf02fSTimur Tabi * 282ff7bf02fSTimur Tabi * This function fills in the CS4270 register cache by reading the register 283ff7bf02fSTimur Tabi * values from the hardware. 284ff7bf02fSTimur Tabi * 285ff7bf02fSTimur Tabi * This CS4270 registers are cached to avoid excessive I2C I/O operations. 286ff7bf02fSTimur Tabi * After the initial read to pre-fill the cache, the CS4270 never updates 287ff7bf02fSTimur Tabi * the register values, so we won't have a cache coherency problem. 288b0c813ceSTimur Tabi * 289b0c813ceSTimur Tabi * We use the auto-increment feature of the CS4270 to read all registers in 290b0c813ceSTimur Tabi * one shot. 291b0c813ceSTimur Tabi */ 292b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec) 293b0c813ceSTimur Tabi { 294b0c813ceSTimur Tabi u8 *cache = codec->reg_cache; 295b0c813ceSTimur Tabi struct i2c_client *i2c_client = codec->control_data; 296b0c813ceSTimur Tabi s32 length; 297b0c813ceSTimur Tabi 298b0c813ceSTimur Tabi length = i2c_smbus_read_i2c_block_data(i2c_client, 299b0c813ceSTimur Tabi CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache); 300b0c813ceSTimur Tabi 301b0c813ceSTimur Tabi if (length != CS4270_NUMREGS) { 302a6c255e0STimur Tabi dev_err(codec->dev, "i2c read failure, addr=0x%x\n", 303b0c813ceSTimur Tabi i2c_client->addr); 304b0c813ceSTimur Tabi return -EIO; 305b0c813ceSTimur Tabi } 306b0c813ceSTimur Tabi 307b0c813ceSTimur Tabi return 0; 308b0c813ceSTimur Tabi } 309b0c813ceSTimur Tabi 310ff7bf02fSTimur Tabi /** 311ff7bf02fSTimur Tabi * cs4270_read_reg_cache - read from the CS4270 register cache. 312ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 313ff7bf02fSTimur Tabi * @reg: the register to read 314ff7bf02fSTimur Tabi * 315ff7bf02fSTimur Tabi * This function returns the value for a given register. It reads only from 316ff7bf02fSTimur Tabi * the register cache, not the hardware itself. 317b0c813ceSTimur Tabi * 318b0c813ceSTimur Tabi * This CS4270 registers are cached to avoid excessive I2C I/O operations. 319b0c813ceSTimur Tabi * After the initial read to pre-fill the cache, the CS4270 never updates 320ff7bf02fSTimur Tabi * the register values, so we won't have a cache coherency problem. 321b0c813ceSTimur Tabi */ 322b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec, 323b0c813ceSTimur Tabi unsigned int reg) 324b0c813ceSTimur Tabi { 325b0c813ceSTimur Tabi u8 *cache = codec->reg_cache; 326b0c813ceSTimur Tabi 327b0c813ceSTimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) 328b0c813ceSTimur Tabi return -EIO; 329b0c813ceSTimur Tabi 330b0c813ceSTimur Tabi return cache[reg - CS4270_FIRSTREG]; 331b0c813ceSTimur Tabi } 332b0c813ceSTimur Tabi 333ff7bf02fSTimur Tabi /** 334ff7bf02fSTimur Tabi * cs4270_i2c_write - write to a CS4270 register via the I2C bus. 335ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 336ff7bf02fSTimur Tabi * @reg: the register to write 337ff7bf02fSTimur Tabi * @value: the value to write to the register 338b0c813ceSTimur Tabi * 339b0c813ceSTimur Tabi * This function writes the given value to the given CS4270 register, and 340b0c813ceSTimur Tabi * also updates the register cache. 341b0c813ceSTimur Tabi * 342b0c813ceSTimur Tabi * Note that we don't use the hw_write function pointer of snd_soc_codec. 343b0c813ceSTimur Tabi * That's because it's too clunky: the hw_write_t prototype does not match 344b0c813ceSTimur Tabi * i2c_smbus_write_byte_data(), and it's just another layer of overhead. 345b0c813ceSTimur Tabi */ 346b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg, 347b0c813ceSTimur Tabi unsigned int value) 348b0c813ceSTimur Tabi { 349bfc4e861STimur Tabi u8 *cache = codec->reg_cache; 350bfc4e861STimur Tabi 351b0c813ceSTimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) 352b0c813ceSTimur Tabi return -EIO; 353b0c813ceSTimur Tabi 354bfc4e861STimur Tabi /* Only perform an I2C operation if the new value is different */ 355bfc4e861STimur Tabi if (cache[reg - CS4270_FIRSTREG] != value) { 356bfc4e861STimur Tabi struct i2c_client *client = codec->control_data; 357bfc4e861STimur Tabi if (i2c_smbus_write_byte_data(client, reg, value)) { 358a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 359b0c813ceSTimur Tabi return -EIO; 360b0c813ceSTimur Tabi } 361bfc4e861STimur Tabi 362bfc4e861STimur Tabi /* We've written to the hardware, so update the cache */ 363bfc4e861STimur Tabi cache[reg - CS4270_FIRSTREG] = value; 364bfc4e861STimur Tabi } 365bfc4e861STimur Tabi 366bfc4e861STimur Tabi return 0; 367b0c813ceSTimur Tabi } 368b0c813ceSTimur Tabi 369ff7bf02fSTimur Tabi /** 370ff7bf02fSTimur Tabi * cs4270_hw_params - program the CS4270 with the given hardware parameters. 371ff7bf02fSTimur Tabi * @substream: the audio stream 372ff7bf02fSTimur Tabi * @params: the hardware parameters to set 373ff7bf02fSTimur Tabi * @dai: the SOC DAI (ignored) 374b0c813ceSTimur Tabi * 375ff7bf02fSTimur Tabi * This function programs the hardware with the values provided. 376ff7bf02fSTimur Tabi * Specifically, the sample rate and the data format. 377ff7bf02fSTimur Tabi * 378ff7bf02fSTimur Tabi * The .ops functions are used to provide board-specific data, like input 379ff7bf02fSTimur Tabi * frequencies, to this driver. This function takes that information, 380b0c813ceSTimur Tabi * combines it with the hardware parameters provided, and programs the 381b0c813ceSTimur Tabi * hardware accordingly. 382b0c813ceSTimur Tabi */ 383b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream, 384dee89c4dSMark Brown struct snd_pcm_hw_params *params, 385dee89c4dSMark Brown struct snd_soc_dai *dai) 386b0c813ceSTimur Tabi { 387b0c813ceSTimur Tabi struct snd_soc_pcm_runtime *rtd = substream->private_data; 388b0c813ceSTimur Tabi struct snd_soc_device *socdev = rtd->socdev; 3896627a653SMark Brown struct snd_soc_codec *codec = socdev->card->codec; 390b0c813ceSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 391e34ba212SRoel Kluin int ret; 392b0c813ceSTimur Tabi unsigned int i; 393b0c813ceSTimur Tabi unsigned int rate; 394b0c813ceSTimur Tabi unsigned int ratio; 395b0c813ceSTimur Tabi int reg; 396b0c813ceSTimur Tabi 397b0c813ceSTimur Tabi /* Figure out which MCLK/LRCK ratio to use */ 398b0c813ceSTimur Tabi 399b0c813ceSTimur Tabi rate = params_rate(params); /* Sampling rate, in Hz */ 400b0c813ceSTimur Tabi ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ 401b0c813ceSTimur Tabi 4029dbd627bSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) { 4038432395fSTimur Tabi if (cs4270_mode_ratios[i].ratio == ratio) 404b0c813ceSTimur Tabi break; 405b0c813ceSTimur Tabi } 406b0c813ceSTimur Tabi 4079dbd627bSTimur Tabi if (i == NUM_MCLK_RATIOS) { 408b0c813ceSTimur Tabi /* We did not find a matching ratio */ 409a6c255e0STimur Tabi dev_err(codec->dev, "could not find matching ratio\n"); 410b0c813ceSTimur Tabi return -EINVAL; 411b0c813ceSTimur Tabi } 412b0c813ceSTimur Tabi 413d5e9ba1dSTimur Tabi /* Set the sample rate */ 414b0c813ceSTimur Tabi 415b0c813ceSTimur Tabi reg = snd_soc_read(codec, CS4270_MODE); 416b0c813ceSTimur Tabi reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); 4174eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].mclk; 4184eae080dSDaniel Mack 4194eae080dSDaniel Mack if (cs4270->slave_mode) 4204eae080dSDaniel Mack reg |= CS4270_MODE_SLAVE; 4214eae080dSDaniel Mack else 4224eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].speed_mode; 423b0c813ceSTimur Tabi 424b0c813ceSTimur Tabi ret = snd_soc_write(codec, CS4270_MODE, reg); 425b0c813ceSTimur Tabi if (ret < 0) { 426a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 427b0c813ceSTimur Tabi return ret; 428b0c813ceSTimur Tabi } 429b0c813ceSTimur Tabi 430d5e9ba1dSTimur Tabi /* Set the DAI format */ 431b0c813ceSTimur Tabi 432b0c813ceSTimur Tabi reg = snd_soc_read(codec, CS4270_FORMAT); 433b0c813ceSTimur Tabi reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); 434b0c813ceSTimur Tabi 435b0c813ceSTimur Tabi switch (cs4270->mode) { 436b0c813ceSTimur Tabi case SND_SOC_DAIFMT_I2S: 437b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; 438b0c813ceSTimur Tabi break; 439b0c813ceSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 440b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; 441b0c813ceSTimur Tabi break; 442b0c813ceSTimur Tabi default: 443a6c255e0STimur Tabi dev_err(codec->dev, "unknown dai format\n"); 444b0c813ceSTimur Tabi return -EINVAL; 445b0c813ceSTimur Tabi } 446b0c813ceSTimur Tabi 447b0c813ceSTimur Tabi ret = snd_soc_write(codec, CS4270_FORMAT, reg); 448b0c813ceSTimur Tabi if (ret < 0) { 449a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 450b0c813ceSTimur Tabi return ret; 451b0c813ceSTimur Tabi } 452b0c813ceSTimur Tabi 453b0c813ceSTimur Tabi return ret; 454b0c813ceSTimur Tabi } 455b0c813ceSTimur Tabi 456ff7bf02fSTimur Tabi /** 457ff7bf02fSTimur Tabi * cs4270_mute - enable/disable the CS4270 external mute 458ff7bf02fSTimur Tabi * @dai: the SOC DAI 459ff7bf02fSTimur Tabi * @mute: 0 = disable mute, 1 = enable mute 460b0c813ceSTimur Tabi * 461b0c813ceSTimur Tabi * This function toggles the mute bits in the MUTE register. The CS4270's 462b0c813ceSTimur Tabi * mute capability is intended for external muting circuitry, so if the 463b0c813ceSTimur Tabi * board does not have the MUTEA or MUTEB pins connected to such circuitry, 464b0c813ceSTimur Tabi * then this function will do nothing. 465b0c813ceSTimur Tabi */ 466e550e17fSLiam Girdwood static int cs4270_mute(struct snd_soc_dai *dai, int mute) 467b0c813ceSTimur Tabi { 468b0c813ceSTimur Tabi struct snd_soc_codec *codec = dai->codec; 469b0c813ceSTimur Tabi int reg6; 470b0c813ceSTimur Tabi 471b0c813ceSTimur Tabi reg6 = snd_soc_read(codec, CS4270_MUTE); 472b0c813ceSTimur Tabi 473b0c813ceSTimur Tabi if (mute) 474d5e9ba1dSTimur Tabi reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; 475b0c813ceSTimur Tabi else 476d5e9ba1dSTimur Tabi reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); 477b0c813ceSTimur Tabi 478b0c813ceSTimur Tabi return snd_soc_write(codec, CS4270_MUTE, reg6); 479b0c813ceSTimur Tabi } 480b0c813ceSTimur Tabi 481b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */ 482b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = { 483b0c813ceSTimur Tabi SOC_DOUBLE_R("Master Playback Volume", 484d5e9ba1dSTimur Tabi CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), 485d5e9ba1dSTimur Tabi SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), 486d5e9ba1dSTimur Tabi SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), 487d5e9ba1dSTimur Tabi SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), 488d5e9ba1dSTimur Tabi SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), 489d5e9ba1dSTimur Tabi SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), 490d5e9ba1dSTimur Tabi SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 0) 491b0c813ceSTimur Tabi }; 492b0c813ceSTimur Tabi 493b0c813ceSTimur Tabi /* 494ff7bf02fSTimur Tabi * cs4270_codec - global variable to store codec for the ASoC probe function 495b0c813ceSTimur Tabi * 496b0c813ceSTimur Tabi * If struct i2c_driver had a private_data field, we wouldn't need to use 49704eb093cSTimur Tabi * cs4270_codec. This is the only way to pass the codec structure from 49804eb093cSTimur Tabi * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good 49904eb093cSTimur Tabi * way to synchronize these two functions. cs4270_i2c_probe() can be called 50004eb093cSTimur Tabi * multiple times before cs4270_probe() is called even once. So for now, we 50104eb093cSTimur Tabi * also only allow cs4270_i2c_probe() to be run once. That means that we do 50204eb093cSTimur Tabi * not support more than one cs4270 device in the system, at least for now. 503b0c813ceSTimur Tabi */ 50404eb093cSTimur Tabi static struct snd_soc_codec *cs4270_codec; 505b0c813ceSTimur Tabi 506e550e17fSLiam Girdwood struct snd_soc_dai cs4270_dai = { 5070db4d070STimur Tabi .name = "cs4270", 508b0c813ceSTimur Tabi .playback = { 509b0c813ceSTimur Tabi .stream_name = "Playback", 510b0c813ceSTimur Tabi .channels_min = 1, 511b0c813ceSTimur Tabi .channels_max = 2, 512b0c813ceSTimur Tabi .rates = 0, 513b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 514b0c813ceSTimur Tabi }, 515b0c813ceSTimur Tabi .capture = { 516b0c813ceSTimur Tabi .stream_name = "Capture", 517b0c813ceSTimur Tabi .channels_min = 1, 518b0c813ceSTimur Tabi .channels_max = 2, 519b0c813ceSTimur Tabi .rates = 0, 520b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 521b0c813ceSTimur Tabi }, 5220db4d070STimur Tabi .ops = { 5230db4d070STimur Tabi .hw_params = cs4270_hw_params, 5240db4d070STimur Tabi .set_sysclk = cs4270_set_dai_sysclk, 5250db4d070STimur Tabi .set_fmt = cs4270_set_dai_fmt, 5260db4d070STimur Tabi .digital_mute = cs4270_mute, 5270db4d070STimur Tabi }, 528b0c813ceSTimur Tabi }; 529b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai); 530b0c813ceSTimur Tabi 531ff7bf02fSTimur Tabi /** 532ff7bf02fSTimur Tabi * cs4270_probe - ASoC probe function 533ff7bf02fSTimur Tabi * @pdev: platform device 534ff7bf02fSTimur Tabi * 535ff7bf02fSTimur Tabi * This function is called when ASoC has all the pieces it needs to 536ff7bf02fSTimur Tabi * instantiate a sound driver. 53704eb093cSTimur Tabi */ 53804eb093cSTimur Tabi static int cs4270_probe(struct platform_device *pdev) 53904eb093cSTimur Tabi { 54004eb093cSTimur Tabi struct snd_soc_device *socdev = platform_get_drvdata(pdev); 54104eb093cSTimur Tabi struct snd_soc_codec *codec = cs4270_codec; 54204eb093cSTimur Tabi unsigned int i; 54304eb093cSTimur Tabi int ret; 54404eb093cSTimur Tabi 54504eb093cSTimur Tabi /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */ 54604eb093cSTimur Tabi socdev->card->codec = codec; 54704eb093cSTimur Tabi 54804eb093cSTimur Tabi /* Register PCMs */ 54904eb093cSTimur Tabi ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 55004eb093cSTimur Tabi if (ret < 0) { 551a6c255e0STimur Tabi dev_err(codec->dev, "failed to create pcms\n"); 55204eb093cSTimur Tabi return ret; 55304eb093cSTimur Tabi } 55404eb093cSTimur Tabi 55504eb093cSTimur Tabi /* Add the non-DAPM controls */ 55604eb093cSTimur Tabi for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) { 55704eb093cSTimur Tabi struct snd_kcontrol *kctrl; 55804eb093cSTimur Tabi 55904eb093cSTimur Tabi kctrl = snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL); 56004eb093cSTimur Tabi if (!kctrl) { 561a6c255e0STimur Tabi dev_err(codec->dev, "error creating control '%s'\n", 56204eb093cSTimur Tabi cs4270_snd_controls[i].name); 56304eb093cSTimur Tabi ret = -ENOMEM; 56404eb093cSTimur Tabi goto error_free_pcms; 56504eb093cSTimur Tabi } 56604eb093cSTimur Tabi 56704eb093cSTimur Tabi ret = snd_ctl_add(codec->card, kctrl); 56804eb093cSTimur Tabi if (ret < 0) { 569a6c255e0STimur Tabi dev_err(codec->dev, "error adding control '%s'\n", 57004eb093cSTimur Tabi cs4270_snd_controls[i].name); 57104eb093cSTimur Tabi goto error_free_pcms; 57204eb093cSTimur Tabi } 57304eb093cSTimur Tabi } 57404eb093cSTimur Tabi 57504eb093cSTimur Tabi /* And finally, register the socdev */ 57604eb093cSTimur Tabi ret = snd_soc_init_card(socdev); 57704eb093cSTimur Tabi if (ret < 0) { 578a6c255e0STimur Tabi dev_err(codec->dev, "failed to register card\n"); 57904eb093cSTimur Tabi goto error_free_pcms; 58004eb093cSTimur Tabi } 58104eb093cSTimur Tabi 58204eb093cSTimur Tabi return 0; 58304eb093cSTimur Tabi 58404eb093cSTimur Tabi error_free_pcms: 58504eb093cSTimur Tabi snd_soc_free_pcms(socdev); 58604eb093cSTimur Tabi 58704eb093cSTimur Tabi return ret; 58804eb093cSTimur Tabi } 58904eb093cSTimur Tabi 590ff7bf02fSTimur Tabi /** 591ff7bf02fSTimur Tabi * cs4270_remove - ASoC remove function 592ff7bf02fSTimur Tabi * @pdev: platform device 593ff7bf02fSTimur Tabi * 594ff7bf02fSTimur Tabi * This function is the counterpart to cs4270_probe(). 595ff7bf02fSTimur Tabi */ 59604eb093cSTimur Tabi static int cs4270_remove(struct platform_device *pdev) 59704eb093cSTimur Tabi { 59804eb093cSTimur Tabi struct snd_soc_device *socdev = platform_get_drvdata(pdev); 59904eb093cSTimur Tabi 60004eb093cSTimur Tabi snd_soc_free_pcms(socdev); 60104eb093cSTimur Tabi 60204eb093cSTimur Tabi return 0; 60304eb093cSTimur Tabi }; 60404eb093cSTimur Tabi 605ff7bf02fSTimur Tabi /** 606ff7bf02fSTimur Tabi * cs4270_i2c_probe - initialize the I2C interface of the CS4270 607ff7bf02fSTimur Tabi * @i2c_client: the I2C client object 608ff7bf02fSTimur Tabi * @id: the I2C device ID (ignored) 609b0c813ceSTimur Tabi * 610ff7bf02fSTimur Tabi * This function is called whenever the I2C subsystem finds a device that 611ff7bf02fSTimur Tabi * matches the device ID given via a prior call to i2c_add_driver(). 612b0c813ceSTimur Tabi */ 6130db4d070STimur Tabi static int cs4270_i2c_probe(struct i2c_client *i2c_client, 6140db4d070STimur Tabi const struct i2c_device_id *id) 615b0c813ceSTimur Tabi { 616b0c813ceSTimur Tabi struct snd_soc_codec *codec; 6170db4d070STimur Tabi struct cs4270_private *cs4270; 618d5e9ba1dSTimur Tabi unsigned int reg; 61904eb093cSTimur Tabi int ret; 62004eb093cSTimur Tabi 62104eb093cSTimur Tabi /* For now, we only support one cs4270 device in the system. See the 62204eb093cSTimur Tabi * comment for cs4270_codec. 62304eb093cSTimur Tabi */ 62404eb093cSTimur Tabi if (cs4270_codec) { 625a6c255e0STimur Tabi dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n", 62604eb093cSTimur Tabi i2c_client->addr); 627a6c255e0STimur Tabi dev_err(&i2c_client->dev, "only one per board allowed\n"); 62804eb093cSTimur Tabi /* Should we return something other than ENODEV here? */ 62904eb093cSTimur Tabi return -ENODEV; 63004eb093cSTimur Tabi } 631b0c813ceSTimur Tabi 6320db4d070STimur Tabi /* Verify that we have a CS4270 */ 6330db4d070STimur Tabi 6340db4d070STimur Tabi ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID); 6350db4d070STimur Tabi if (ret < 0) { 636a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", 63704eb093cSTimur Tabi i2c_client->addr); 6380db4d070STimur Tabi return ret; 6390db4d070STimur Tabi } 6400db4d070STimur Tabi /* The top four bits of the chip ID should be 1100. */ 6410db4d070STimur Tabi if ((ret & 0xF0) != 0xC0) { 642a6c255e0STimur Tabi dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", 6430db4d070STimur Tabi i2c_client->addr); 6440db4d070STimur Tabi return -ENODEV; 6450db4d070STimur Tabi } 6460db4d070STimur Tabi 647a6c255e0STimur Tabi dev_info(&i2c_client->dev, "found device at i2c address %X\n", 6480db4d070STimur Tabi i2c_client->addr); 649a6c255e0STimur Tabi dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF); 650b0c813ceSTimur Tabi 651b0c813ceSTimur Tabi /* Allocate enough space for the snd_soc_codec structure 652b0c813ceSTimur Tabi and our private data together. */ 6530db4d070STimur Tabi cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL); 6540db4d070STimur Tabi if (!cs4270) { 655a6c255e0STimur Tabi dev_err(&i2c_client->dev, "could not allocate codec\n"); 656b0c813ceSTimur Tabi return -ENOMEM; 657b0c813ceSTimur Tabi } 6580db4d070STimur Tabi codec = &cs4270->codec; 659b0c813ceSTimur Tabi 660b0c813ceSTimur Tabi mutex_init(&codec->mutex); 661b0c813ceSTimur Tabi INIT_LIST_HEAD(&codec->dapm_widgets); 662b0c813ceSTimur Tabi INIT_LIST_HEAD(&codec->dapm_paths); 663b0c813ceSTimur Tabi 664a6c255e0STimur Tabi codec->dev = &i2c_client->dev; 665b0c813ceSTimur Tabi codec->name = "CS4270"; 666b0c813ceSTimur Tabi codec->owner = THIS_MODULE; 667b0c813ceSTimur Tabi codec->dai = &cs4270_dai; 668b0c813ceSTimur Tabi codec->num_dai = 1; 6690db4d070STimur Tabi codec->private_data = cs4270; 6700db4d070STimur Tabi codec->control_data = i2c_client; 6710db4d070STimur Tabi codec->read = cs4270_read_reg_cache; 6720db4d070STimur Tabi codec->write = cs4270_i2c_write; 6730db4d070STimur Tabi codec->reg_cache = cs4270->reg_cache; 6740db4d070STimur Tabi codec->reg_cache_size = CS4270_NUMREGS; 675b0c813ceSTimur Tabi 6760db4d070STimur Tabi /* The I2C interface is set up, so pre-fill our register cache */ 6770db4d070STimur Tabi 6780db4d070STimur Tabi ret = cs4270_fill_cache(codec); 6790db4d070STimur Tabi if (ret < 0) { 680a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to fill register cache\n"); 6810db4d070STimur Tabi goto error_free_codec; 6820db4d070STimur Tabi } 683b0c813ceSTimur Tabi 684d5e9ba1dSTimur Tabi /* Disable auto-mute. This feature appears to be buggy. In some 685d5e9ba1dSTimur Tabi * situations, auto-mute will not deactivate when it should, so we want 686d5e9ba1dSTimur Tabi * this feature disabled by default. An application (e.g. alsactl) can 687d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 688d5e9ba1dSTimur Tabi */ 689d5e9ba1dSTimur Tabi 690d5e9ba1dSTimur Tabi reg = cs4270_read_reg_cache(codec, CS4270_MUTE); 691d5e9ba1dSTimur Tabi reg &= ~CS4270_MUTE_AUTO; 692d5e9ba1dSTimur Tabi ret = cs4270_i2c_write(codec, CS4270_MUTE, reg); 693d5e9ba1dSTimur Tabi if (ret < 0) { 694d5e9ba1dSTimur Tabi dev_err(&i2c_client->dev, "i2c write failed\n"); 695d5e9ba1dSTimur Tabi return ret; 696d5e9ba1dSTimur Tabi } 697d5e9ba1dSTimur Tabi 698d5e9ba1dSTimur Tabi /* Disable automatic volume control. The hardware enables, and it 699d5e9ba1dSTimur Tabi * causes volume change commands to be delayed, sometimes until after 700d5e9ba1dSTimur Tabi * playback has started. An application (e.g. alsactl) can 701d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 702d5e9ba1dSTimur Tabi */ 703d5e9ba1dSTimur Tabi 704d5e9ba1dSTimur Tabi reg = cs4270_read_reg_cache(codec, CS4270_TRANS); 705d5e9ba1dSTimur Tabi reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO); 706d5e9ba1dSTimur Tabi ret = cs4270_i2c_write(codec, CS4270_TRANS, reg); 707d5e9ba1dSTimur Tabi if (ret < 0) { 708d5e9ba1dSTimur Tabi dev_err(&i2c_client->dev, "i2c write failed\n"); 709d5e9ba1dSTimur Tabi return ret; 710d5e9ba1dSTimur Tabi } 711d5e9ba1dSTimur Tabi 712a6c255e0STimur Tabi /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI 713a6c255e0STimur Tabi * structure for each CS4270 device, but the machine driver needs to 714a6c255e0STimur Tabi * have a pointer to the DAI structure, so for now it must be a global 715a6c255e0STimur Tabi * variable. 716a6c255e0STimur Tabi */ 717a6c255e0STimur Tabi cs4270_dai.dev = &i2c_client->dev; 718a6c255e0STimur Tabi 71904eb093cSTimur Tabi /* Register the DAI. If all the other ASoC driver have already 72004eb093cSTimur Tabi * registered, then this will call our probe function, so 72104eb093cSTimur Tabi * cs4270_codec needs to be ready. 72204eb093cSTimur Tabi */ 723a6c255e0STimur Tabi cs4270_codec = codec; 72404eb093cSTimur Tabi ret = snd_soc_register_dai(&cs4270_dai); 725b0c813ceSTimur Tabi if (ret < 0) { 726a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to register DAIe\n"); 727e3145dfbSJean Delvare goto error_free_codec; 728b0c813ceSTimur Tabi } 729b0c813ceSTimur Tabi 73004eb093cSTimur Tabi i2c_set_clientdata(i2c_client, cs4270); 731e3145dfbSJean Delvare 7320db4d070STimur Tabi return 0; 733e3145dfbSJean Delvare 734e3145dfbSJean Delvare error_free_codec: 7350db4d070STimur Tabi kfree(cs4270); 736a6c255e0STimur Tabi cs4270_codec = NULL; 737a6c255e0STimur Tabi cs4270_dai.dev = NULL; 738e3145dfbSJean Delvare 739b0c813ceSTimur Tabi return ret; 740b0c813ceSTimur Tabi } 741b0c813ceSTimur Tabi 742ff7bf02fSTimur Tabi /** 743ff7bf02fSTimur Tabi * cs4270_i2c_remove - remove an I2C device 744ff7bf02fSTimur Tabi * @i2c_client: the I2C client object 745ff7bf02fSTimur Tabi * 746ff7bf02fSTimur Tabi * This function is the counterpart to cs4270_i2c_probe(). 747ff7bf02fSTimur Tabi */ 7480db4d070STimur Tabi static int cs4270_i2c_remove(struct i2c_client *i2c_client) 749b0c813ceSTimur Tabi { 75004eb093cSTimur Tabi struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client); 751b0c813ceSTimur Tabi 7520db4d070STimur Tabi kfree(cs4270); 753a6c255e0STimur Tabi cs4270_codec = NULL; 754a6c255e0STimur Tabi cs4270_dai.dev = NULL; 755b0c813ceSTimur Tabi 7560db4d070STimur Tabi return 0; 7570db4d070STimur Tabi } 7580db4d070STimur Tabi 759ff7bf02fSTimur Tabi /* 760ff7bf02fSTimur Tabi * cs4270_id - I2C device IDs supported by this driver 761ff7bf02fSTimur Tabi */ 7620db4d070STimur Tabi static struct i2c_device_id cs4270_id[] = { 7630db4d070STimur Tabi {"cs4270", 0}, 7640db4d070STimur Tabi {} 7650db4d070STimur Tabi }; 7660db4d070STimur Tabi MODULE_DEVICE_TABLE(i2c, cs4270_id); 7670db4d070STimur Tabi 768ff7bf02fSTimur Tabi /* 769ff7bf02fSTimur Tabi * cs4270_i2c_driver - I2C device identification 770ff7bf02fSTimur Tabi * 771ff7bf02fSTimur Tabi * This structure tells the I2C subsystem how to identify and support a 772ff7bf02fSTimur Tabi * given I2C device type. 773ff7bf02fSTimur Tabi */ 7740db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = { 7750db4d070STimur Tabi .driver = { 7760db4d070STimur Tabi .name = "cs4270", 7770db4d070STimur Tabi .owner = THIS_MODULE, 7780db4d070STimur Tabi }, 7790db4d070STimur Tabi .id_table = cs4270_id, 7800db4d070STimur Tabi .probe = cs4270_i2c_probe, 7810db4d070STimur Tabi .remove = cs4270_i2c_remove, 7820db4d070STimur Tabi }; 7830db4d070STimur Tabi 7840db4d070STimur Tabi /* 785b0c813ceSTimur Tabi * ASoC codec device structure 786b0c813ceSTimur Tabi * 787b0c813ceSTimur Tabi * Assign this variable to the codec_dev field of the machine driver's 788b0c813ceSTimur Tabi * snd_soc_device structure. 789b0c813ceSTimur Tabi */ 790b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = { 791b0c813ceSTimur Tabi .probe = cs4270_probe, 792b0c813ceSTimur Tabi .remove = cs4270_remove 793b0c813ceSTimur Tabi }; 794b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270); 795b0c813ceSTimur Tabi 796c9b3a40fSTakashi Iwai static int __init cs4270_init(void) 79764089b84SMark Brown { 798a6c255e0STimur Tabi pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n"); 7990db4d070STimur Tabi 80004eb093cSTimur Tabi return i2c_add_driver(&cs4270_i2c_driver); 80164089b84SMark Brown } 80264089b84SMark Brown module_init(cs4270_init); 80364089b84SMark Brown 80464089b84SMark Brown static void __exit cs4270_exit(void) 80564089b84SMark Brown { 80604eb093cSTimur Tabi i2c_del_driver(&cs4270_i2c_driver); 80764089b84SMark Brown } 80864089b84SMark Brown module_exit(cs4270_exit); 80964089b84SMark Brown 810b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 811b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); 812b0c813ceSTimur Tabi MODULE_LICENSE("GPL"); 813