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 * 15b191f63cSDaniel Mack * - Software mode is supported. Stand-alone mode is not supported. 16b191f63cSDaniel Mack * - Only I2C is supported, not SPI 17b191f63cSDaniel Mack * - Support for master and slave mode 18b191f63cSDaniel Mack * - The machine driver's 'startup' function must call 19b0c813ceSTimur Tabi * cs4270_set_dai_sysclk() with the value of MCLK. 20b191f63cSDaniel Mack * - Only I2S and left-justified modes are supported 215e7c0344SDaniel Mack * - Power management is supported 22b0c813ceSTimur Tabi */ 23b0c813ceSTimur Tabi 24b0c813ceSTimur Tabi #include <linux/module.h> 25b0c813ceSTimur Tabi #include <linux/platform_device.h> 26b0c813ceSTimur Tabi #include <sound/core.h> 27b0c813ceSTimur Tabi #include <sound/soc.h> 28b0c813ceSTimur Tabi #include <sound/initval.h> 29b0c813ceSTimur Tabi #include <linux/i2c.h> 305e7c0344SDaniel Mack #include <linux/delay.h> 31*ffbfd336SDaniel Mack #include <linux/regulator/consumer.h> 32b0c813ceSTimur Tabi 3301e097d6SMark Brown #include "cs4270.h" 3401e097d6SMark Brown 35b0c813ceSTimur Tabi /* 36b0c813ceSTimur Tabi * The codec isn't really big-endian or little-endian, since the I2S 37b0c813ceSTimur Tabi * interface requires data to be sent serially with the MSbit first. 38b0c813ceSTimur Tabi * However, to support BE and LE I2S devices, we specify both here. That 39b0c813ceSTimur Tabi * way, ALSA will always match the bit patterns. 40b0c813ceSTimur Tabi */ 41b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 42b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ 43b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ 44b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ 45b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ 46b0c813ceSTimur Tabi SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) 47b0c813ceSTimur Tabi 48b0c813ceSTimur Tabi /* CS4270 registers addresses */ 49b0c813ceSTimur Tabi #define CS4270_CHIPID 0x01 /* Chip ID */ 50b0c813ceSTimur Tabi #define CS4270_PWRCTL 0x02 /* Power Control */ 51b0c813ceSTimur Tabi #define CS4270_MODE 0x03 /* Mode Control */ 52b0c813ceSTimur Tabi #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */ 53b0c813ceSTimur Tabi #define CS4270_TRANS 0x05 /* Transition Control */ 54b0c813ceSTimur Tabi #define CS4270_MUTE 0x06 /* Mute Control */ 55b0c813ceSTimur Tabi #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */ 56b0c813ceSTimur Tabi #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */ 57b0c813ceSTimur Tabi 58b0c813ceSTimur Tabi #define CS4270_FIRSTREG 0x01 59b0c813ceSTimur Tabi #define CS4270_LASTREG 0x08 60b0c813ceSTimur Tabi #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1) 6180ab8817SDaniel Mack #define CS4270_I2C_INCR 0x80 62b0c813ceSTimur Tabi 63b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */ 64b0c813ceSTimur Tabi #define CS4270_CHIPID_ID 0xF0 65b0c813ceSTimur Tabi #define CS4270_CHIPID_REV 0x0F 66b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE 0x80 67b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC 0x20 68b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC 0x02 69b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN 0x01 705e7c0344SDaniel Mack #define CS4270_PWRCTL_PDN_ALL \ 715e7c0344SDaniel Mack (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN) 72b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK 0x30 73b0c813ceSTimur Tabi #define CS4270_MODE_1X 0x00 74b0c813ceSTimur Tabi #define CS4270_MODE_2X 0x10 75b0c813ceSTimur Tabi #define CS4270_MODE_4X 0x20 76b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE 0x30 77b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK 0x0E 78b0c813ceSTimur Tabi #define CS4270_MODE_DIV1 0x00 79b0c813ceSTimur Tabi #define CS4270_MODE_DIV15 0x02 80b0c813ceSTimur Tabi #define CS4270_MODE_DIV2 0x04 81b0c813ceSTimur Tabi #define CS4270_MODE_DIV3 0x06 82b0c813ceSTimur Tabi #define CS4270_MODE_DIV4 0x08 83b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD 0x01 84b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A 0x80 85b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B 0x40 86b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK 0x20 87b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK 0x18 88b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ 0x00 89b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S 0x08 90b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16 0x18 91b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24 0x10 92b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK 0x01 93b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ 0x00 94b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S 0x01 95b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL 0x80 96b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT 0x40 97b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO 0x20 98b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A 0x08 99b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B 0x10 100b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A 0x02 101b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B 0x04 102b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH 0x01 103b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO 0x20 104b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A 0x08 105b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B 0x10 106b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY 0x04 107b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A 0x01 108b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B 0x02 109b0c813ceSTimur Tabi 110*ffbfd336SDaniel Mack static const char *supply_names[] = { 111*ffbfd336SDaniel Mack "va", "vd", "vlc" 112*ffbfd336SDaniel Mack }; 113*ffbfd336SDaniel Mack 1140db4d070STimur Tabi /* Private data for the CS4270 */ 1150db4d070STimur Tabi struct cs4270_private { 1160db4d070STimur Tabi struct snd_soc_codec codec; 1170db4d070STimur Tabi u8 reg_cache[CS4270_NUMREGS]; 1180db4d070STimur Tabi unsigned int mclk; /* Input frequency of the MCLK pin */ 1190db4d070STimur Tabi unsigned int mode; /* The mode (I2S or left-justified) */ 1204eae080dSDaniel Mack unsigned int slave_mode; 1211a4ba05eSDaniel Mack unsigned int manual_mute; 122*ffbfd336SDaniel Mack 123*ffbfd336SDaniel Mack /* power domain regulators */ 124*ffbfd336SDaniel Mack struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; 1250db4d070STimur Tabi }; 1260db4d070STimur Tabi 127ff7bf02fSTimur Tabi /** 128ff7bf02fSTimur Tabi * struct cs4270_mode_ratios - clock ratio tables 129ff7bf02fSTimur Tabi * @ratio: the ratio of MCLK to the sample rate 130ff7bf02fSTimur Tabi * @speed_mode: the Speed Mode bits to set in the Mode Control register for 131ff7bf02fSTimur Tabi * this ratio 132ff7bf02fSTimur Tabi * @mclk: the Ratio Select bits to set in the Mode Control register for this 133ff7bf02fSTimur Tabi * ratio 1348432395fSTimur Tabi * 1358432395fSTimur Tabi * The data for this chart is taken from Table 5 of the CS4270 reference 1368432395fSTimur Tabi * manual. 1378432395fSTimur Tabi * 1388432395fSTimur Tabi * This table is used to determine how to program the Mode Control register. 1398432395fSTimur Tabi * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling 1408432395fSTimur Tabi * rates the CS4270 currently supports. 1418432395fSTimur Tabi * 142ff7bf02fSTimur Tabi * @speed_mode is the corresponding bit pattern to be written to the 1438432395fSTimur Tabi * MODE bits of the Mode Control Register 1448432395fSTimur Tabi * 145ff7bf02fSTimur Tabi * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of 1468432395fSTimur Tabi * the Mode Control Register. 1478432395fSTimur Tabi * 1488432395fSTimur Tabi * In situations where a single ratio is represented by multiple speed 1498432395fSTimur Tabi * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick 1508432395fSTimur Tabi * double-speed instead of quad-speed. However, the CS4270 errata states 151ff7bf02fSTimur Tabi * that divide-By-1.5 can cause failures, so we avoid that mode where 1528432395fSTimur Tabi * possible. 1538432395fSTimur Tabi * 154ff7bf02fSTimur Tabi * Errata: There is an errata for the CS4270 where divide-by-1.5 does not 155ff7bf02fSTimur Tabi * work if Vd is 3.3V. If this effects you, select the 1568432395fSTimur Tabi * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will 1578432395fSTimur Tabi * never select any sample rates that require divide-by-1.5. 1588432395fSTimur Tabi */ 159ff7bf02fSTimur Tabi struct cs4270_mode_ratios { 1608432395fSTimur Tabi unsigned int ratio; 1618432395fSTimur Tabi u8 speed_mode; 1628432395fSTimur Tabi u8 mclk; 163ff7bf02fSTimur Tabi }; 164ff7bf02fSTimur Tabi 165d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = { 1668432395fSTimur Tabi {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, 1678432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA 1688432395fSTimur Tabi {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, 1698432395fSTimur Tabi #endif 1708432395fSTimur Tabi {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, 1718432395fSTimur Tabi {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, 1728432395fSTimur Tabi {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, 1738432395fSTimur Tabi {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, 1748432395fSTimur Tabi {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, 1758432395fSTimur Tabi {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, 1768432395fSTimur Tabi {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} 1778432395fSTimur Tabi }; 1788432395fSTimur Tabi 1798432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */ 1808432395fSTimur Tabi #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) 1818432395fSTimur Tabi 182ff7bf02fSTimur Tabi /** 183ff7bf02fSTimur Tabi * cs4270_set_dai_sysclk - determine the CS4270 samples rates. 184ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 185ff7bf02fSTimur Tabi * @clk_id: the clock ID (ignored) 186ff7bf02fSTimur Tabi * @freq: the MCLK input frequency 187ff7bf02fSTimur Tabi * @dir: the clock direction (ignored) 1888432395fSTimur Tabi * 189ff7bf02fSTimur Tabi * This function is used to tell the codec driver what the input MCLK 190ff7bf02fSTimur Tabi * frequency is. 1918432395fSTimur Tabi * 1928432395fSTimur Tabi * The value of MCLK is used to determine which sample rates are supported 1938432395fSTimur Tabi * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine 194ff7bf02fSTimur Tabi * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. 1958432395fSTimur Tabi * 1968432395fSTimur Tabi * This function calculates the nine ratios and determines which ones match 1978432395fSTimur Tabi * a standard sample rate. If there's a match, then it is added to the list 198ff7bf02fSTimur Tabi * of supported sample rates. 1998432395fSTimur Tabi * 2008432395fSTimur Tabi * This function must be called by the machine driver's 'startup' function, 2018432395fSTimur Tabi * otherwise the list of supported sample rates will not be available in 2028432395fSTimur Tabi * time for ALSA. 2038432395fSTimur Tabi */ 204e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, 2058432395fSTimur Tabi int clk_id, unsigned int freq, int dir) 2068432395fSTimur Tabi { 2078432395fSTimur Tabi struct snd_soc_codec *codec = codec_dai->codec; 2088432395fSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 2098432395fSTimur Tabi unsigned int rates = 0; 2108432395fSTimur Tabi unsigned int rate_min = -1; 2118432395fSTimur Tabi unsigned int rate_max = 0; 2128432395fSTimur Tabi unsigned int i; 2138432395fSTimur Tabi 2148432395fSTimur Tabi cs4270->mclk = freq; 2158432395fSTimur Tabi 2168432395fSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) { 2178432395fSTimur Tabi unsigned int rate = freq / cs4270_mode_ratios[i].ratio; 2188432395fSTimur Tabi rates |= snd_pcm_rate_to_rate_bit(rate); 2198432395fSTimur Tabi if (rate < rate_min) 2208432395fSTimur Tabi rate_min = rate; 2218432395fSTimur Tabi if (rate > rate_max) 2228432395fSTimur Tabi rate_max = rate; 2238432395fSTimur Tabi } 2248432395fSTimur Tabi /* FIXME: soc should support a rate list */ 2258432395fSTimur Tabi rates &= ~SNDRV_PCM_RATE_KNOT; 2268432395fSTimur Tabi 2278432395fSTimur Tabi if (!rates) { 228a6c255e0STimur Tabi dev_err(codec->dev, "could not find a valid sample rate\n"); 2298432395fSTimur Tabi return -EINVAL; 2308432395fSTimur Tabi } 2318432395fSTimur Tabi 2328432395fSTimur Tabi codec_dai->playback.rates = rates; 2338432395fSTimur Tabi codec_dai->playback.rate_min = rate_min; 2348432395fSTimur Tabi codec_dai->playback.rate_max = rate_max; 2358432395fSTimur Tabi 2368432395fSTimur Tabi codec_dai->capture.rates = rates; 2378432395fSTimur Tabi codec_dai->capture.rate_min = rate_min; 2388432395fSTimur Tabi codec_dai->capture.rate_max = rate_max; 2398432395fSTimur Tabi 2408432395fSTimur Tabi return 0; 2418432395fSTimur Tabi } 2428432395fSTimur Tabi 243ff7bf02fSTimur Tabi /** 244ff7bf02fSTimur Tabi * cs4270_set_dai_fmt - configure the codec for the selected audio format 245ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 246ff7bf02fSTimur Tabi * @format: a SND_SOC_DAIFMT_x value indicating the data format 2478432395fSTimur Tabi * 2488432395fSTimur Tabi * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the 2498432395fSTimur Tabi * codec accordingly. 2508432395fSTimur Tabi * 2518432395fSTimur Tabi * Currently, this function only supports SND_SOC_DAIFMT_I2S and 2528432395fSTimur Tabi * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified 2538432395fSTimur Tabi * data for playback only, but ASoC currently does not support different 2548432395fSTimur Tabi * formats for playback vs. record. 2558432395fSTimur Tabi */ 256e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, 2578432395fSTimur Tabi unsigned int format) 2588432395fSTimur Tabi { 2598432395fSTimur Tabi struct snd_soc_codec *codec = codec_dai->codec; 2608432395fSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 2618432395fSTimur Tabi int ret = 0; 2628432395fSTimur Tabi 2634eae080dSDaniel Mack /* set DAI format */ 2648432395fSTimur Tabi switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 2658432395fSTimur Tabi case SND_SOC_DAIFMT_I2S: 2668432395fSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 2678432395fSTimur Tabi cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; 2688432395fSTimur Tabi break; 2698432395fSTimur Tabi default: 270a6c255e0STimur Tabi dev_err(codec->dev, "invalid dai format\n"); 2718432395fSTimur Tabi ret = -EINVAL; 2728432395fSTimur Tabi } 2738432395fSTimur Tabi 2744eae080dSDaniel Mack /* set master/slave audio interface */ 2754eae080dSDaniel Mack switch (format & SND_SOC_DAIFMT_MASTER_MASK) { 2764eae080dSDaniel Mack case SND_SOC_DAIFMT_CBS_CFS: 2774eae080dSDaniel Mack cs4270->slave_mode = 1; 2784eae080dSDaniel Mack break; 2794eae080dSDaniel Mack case SND_SOC_DAIFMT_CBM_CFM: 2804eae080dSDaniel Mack cs4270->slave_mode = 0; 2814eae080dSDaniel Mack break; 2824eae080dSDaniel Mack default: 283ff09d49aSDaniel Mack /* all other modes are unsupported by the hardware */ 2844eae080dSDaniel Mack ret = -EINVAL; 2854eae080dSDaniel Mack } 2864eae080dSDaniel Mack 2878432395fSTimur Tabi return ret; 2888432395fSTimur Tabi } 2898432395fSTimur Tabi 290ff7bf02fSTimur Tabi /** 291ff7bf02fSTimur Tabi * cs4270_fill_cache - pre-fill the CS4270 register cache. 292ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 293ff7bf02fSTimur Tabi * 294ff7bf02fSTimur Tabi * This function fills in the CS4270 register cache by reading the register 295ff7bf02fSTimur Tabi * values from the hardware. 296ff7bf02fSTimur Tabi * 297ff7bf02fSTimur Tabi * This CS4270 registers are cached to avoid excessive I2C I/O operations. 298ff7bf02fSTimur Tabi * After the initial read to pre-fill the cache, the CS4270 never updates 299ff7bf02fSTimur Tabi * the register values, so we won't have a cache coherency problem. 300b0c813ceSTimur Tabi * 301b0c813ceSTimur Tabi * We use the auto-increment feature of the CS4270 to read all registers in 302b0c813ceSTimur Tabi * one shot. 303b0c813ceSTimur Tabi */ 304b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec) 305b0c813ceSTimur Tabi { 306b0c813ceSTimur Tabi u8 *cache = codec->reg_cache; 307b0c813ceSTimur Tabi struct i2c_client *i2c_client = codec->control_data; 308b0c813ceSTimur Tabi s32 length; 309b0c813ceSTimur Tabi 310b0c813ceSTimur Tabi length = i2c_smbus_read_i2c_block_data(i2c_client, 31180ab8817SDaniel Mack CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache); 312b0c813ceSTimur Tabi 313b0c813ceSTimur Tabi if (length != CS4270_NUMREGS) { 314a6c255e0STimur Tabi dev_err(codec->dev, "i2c read failure, addr=0x%x\n", 315b0c813ceSTimur Tabi i2c_client->addr); 316b0c813ceSTimur Tabi return -EIO; 317b0c813ceSTimur Tabi } 318b0c813ceSTimur Tabi 319b0c813ceSTimur Tabi return 0; 320b0c813ceSTimur Tabi } 321b0c813ceSTimur Tabi 322ff7bf02fSTimur Tabi /** 323ff7bf02fSTimur Tabi * cs4270_read_reg_cache - read from the CS4270 register cache. 324ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 325ff7bf02fSTimur Tabi * @reg: the register to read 326ff7bf02fSTimur Tabi * 327ff7bf02fSTimur Tabi * This function returns the value for a given register. It reads only from 328ff7bf02fSTimur Tabi * the register cache, not the hardware itself. 329b0c813ceSTimur Tabi * 330b0c813ceSTimur Tabi * This CS4270 registers are cached to avoid excessive I2C I/O operations. 331b0c813ceSTimur Tabi * After the initial read to pre-fill the cache, the CS4270 never updates 332ff7bf02fSTimur Tabi * the register values, so we won't have a cache coherency problem. 333b0c813ceSTimur Tabi */ 334b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec, 335b0c813ceSTimur Tabi unsigned int reg) 336b0c813ceSTimur Tabi { 337b0c813ceSTimur Tabi u8 *cache = codec->reg_cache; 338b0c813ceSTimur Tabi 339b0c813ceSTimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) 340b0c813ceSTimur Tabi return -EIO; 341b0c813ceSTimur Tabi 342b0c813ceSTimur Tabi return cache[reg - CS4270_FIRSTREG]; 343b0c813ceSTimur Tabi } 344b0c813ceSTimur Tabi 345ff7bf02fSTimur Tabi /** 346ff7bf02fSTimur Tabi * cs4270_i2c_write - write to a CS4270 register via the I2C bus. 347ff7bf02fSTimur Tabi * @codec: the codec for this CS4270 348ff7bf02fSTimur Tabi * @reg: the register to write 349ff7bf02fSTimur Tabi * @value: the value to write to the register 350b0c813ceSTimur Tabi * 351b0c813ceSTimur Tabi * This function writes the given value to the given CS4270 register, and 352b0c813ceSTimur Tabi * also updates the register cache. 353b0c813ceSTimur Tabi * 354b0c813ceSTimur Tabi * Note that we don't use the hw_write function pointer of snd_soc_codec. 355b0c813ceSTimur Tabi * That's because it's too clunky: the hw_write_t prototype does not match 356b0c813ceSTimur Tabi * i2c_smbus_write_byte_data(), and it's just another layer of overhead. 357b0c813ceSTimur Tabi */ 358b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg, 359b0c813ceSTimur Tabi unsigned int value) 360b0c813ceSTimur Tabi { 361bfc4e861STimur Tabi u8 *cache = codec->reg_cache; 362bfc4e861STimur Tabi 363b0c813ceSTimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) 364b0c813ceSTimur Tabi return -EIO; 365b0c813ceSTimur Tabi 366bfc4e861STimur Tabi /* Only perform an I2C operation if the new value is different */ 367bfc4e861STimur Tabi if (cache[reg - CS4270_FIRSTREG] != value) { 368bfc4e861STimur Tabi struct i2c_client *client = codec->control_data; 369bfc4e861STimur Tabi if (i2c_smbus_write_byte_data(client, reg, value)) { 370a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 371b0c813ceSTimur Tabi return -EIO; 372b0c813ceSTimur Tabi } 373bfc4e861STimur Tabi 374bfc4e861STimur Tabi /* We've written to the hardware, so update the cache */ 375bfc4e861STimur Tabi cache[reg - CS4270_FIRSTREG] = value; 376bfc4e861STimur Tabi } 377bfc4e861STimur Tabi 378bfc4e861STimur Tabi return 0; 379b0c813ceSTimur Tabi } 380b0c813ceSTimur Tabi 381ff7bf02fSTimur Tabi /** 382ff7bf02fSTimur Tabi * cs4270_hw_params - program the CS4270 with the given hardware parameters. 383ff7bf02fSTimur Tabi * @substream: the audio stream 384ff7bf02fSTimur Tabi * @params: the hardware parameters to set 385ff7bf02fSTimur Tabi * @dai: the SOC DAI (ignored) 386b0c813ceSTimur Tabi * 387ff7bf02fSTimur Tabi * This function programs the hardware with the values provided. 388ff7bf02fSTimur Tabi * Specifically, the sample rate and the data format. 389ff7bf02fSTimur Tabi * 390ff7bf02fSTimur Tabi * The .ops functions are used to provide board-specific data, like input 391ff7bf02fSTimur Tabi * frequencies, to this driver. This function takes that information, 392b0c813ceSTimur Tabi * combines it with the hardware parameters provided, and programs the 393b0c813ceSTimur Tabi * hardware accordingly. 394b0c813ceSTimur Tabi */ 395b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream, 396dee89c4dSMark Brown struct snd_pcm_hw_params *params, 397dee89c4dSMark Brown struct snd_soc_dai *dai) 398b0c813ceSTimur Tabi { 399b0c813ceSTimur Tabi struct snd_soc_pcm_runtime *rtd = substream->private_data; 400b0c813ceSTimur Tabi struct snd_soc_device *socdev = rtd->socdev; 4016627a653SMark Brown struct snd_soc_codec *codec = socdev->card->codec; 402b0c813ceSTimur Tabi struct cs4270_private *cs4270 = codec->private_data; 403e34ba212SRoel Kluin int ret; 404b0c813ceSTimur Tabi unsigned int i; 405b0c813ceSTimur Tabi unsigned int rate; 406b0c813ceSTimur Tabi unsigned int ratio; 407b0c813ceSTimur Tabi int reg; 408b0c813ceSTimur Tabi 409b0c813ceSTimur Tabi /* Figure out which MCLK/LRCK ratio to use */ 410b0c813ceSTimur Tabi 411b0c813ceSTimur Tabi rate = params_rate(params); /* Sampling rate, in Hz */ 412b0c813ceSTimur Tabi ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ 413b0c813ceSTimur Tabi 4149dbd627bSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) { 4158432395fSTimur Tabi if (cs4270_mode_ratios[i].ratio == ratio) 416b0c813ceSTimur Tabi break; 417b0c813ceSTimur Tabi } 418b0c813ceSTimur Tabi 4199dbd627bSTimur Tabi if (i == NUM_MCLK_RATIOS) { 420b0c813ceSTimur Tabi /* We did not find a matching ratio */ 421a6c255e0STimur Tabi dev_err(codec->dev, "could not find matching ratio\n"); 422b0c813ceSTimur Tabi return -EINVAL; 423b0c813ceSTimur Tabi } 424b0c813ceSTimur Tabi 425d5e9ba1dSTimur Tabi /* Set the sample rate */ 426b0c813ceSTimur Tabi 427b0c813ceSTimur Tabi reg = snd_soc_read(codec, CS4270_MODE); 428b0c813ceSTimur Tabi reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); 4294eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].mclk; 4304eae080dSDaniel Mack 4314eae080dSDaniel Mack if (cs4270->slave_mode) 4324eae080dSDaniel Mack reg |= CS4270_MODE_SLAVE; 4334eae080dSDaniel Mack else 4344eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].speed_mode; 435b0c813ceSTimur Tabi 436b0c813ceSTimur Tabi ret = snd_soc_write(codec, CS4270_MODE, reg); 437b0c813ceSTimur Tabi if (ret < 0) { 438a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 439b0c813ceSTimur Tabi return ret; 440b0c813ceSTimur Tabi } 441b0c813ceSTimur Tabi 442d5e9ba1dSTimur Tabi /* Set the DAI format */ 443b0c813ceSTimur Tabi 444b0c813ceSTimur Tabi reg = snd_soc_read(codec, CS4270_FORMAT); 445b0c813ceSTimur Tabi reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); 446b0c813ceSTimur Tabi 447b0c813ceSTimur Tabi switch (cs4270->mode) { 448b0c813ceSTimur Tabi case SND_SOC_DAIFMT_I2S: 449b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; 450b0c813ceSTimur Tabi break; 451b0c813ceSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 452b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; 453b0c813ceSTimur Tabi break; 454b0c813ceSTimur Tabi default: 455a6c255e0STimur Tabi dev_err(codec->dev, "unknown dai format\n"); 456b0c813ceSTimur Tabi return -EINVAL; 457b0c813ceSTimur Tabi } 458b0c813ceSTimur Tabi 459b0c813ceSTimur Tabi ret = snd_soc_write(codec, CS4270_FORMAT, reg); 460b0c813ceSTimur Tabi if (ret < 0) { 461a6c255e0STimur Tabi dev_err(codec->dev, "i2c write failed\n"); 462b0c813ceSTimur Tabi return ret; 463b0c813ceSTimur Tabi } 464b0c813ceSTimur Tabi 465b0c813ceSTimur Tabi return ret; 466b0c813ceSTimur Tabi } 467b0c813ceSTimur Tabi 468ff7bf02fSTimur Tabi /** 4691a4ba05eSDaniel Mack * cs4270_dai_mute - enable/disable the CS4270 external mute 470ff7bf02fSTimur Tabi * @dai: the SOC DAI 471ff7bf02fSTimur Tabi * @mute: 0 = disable mute, 1 = enable mute 472b0c813ceSTimur Tabi * 473b0c813ceSTimur Tabi * This function toggles the mute bits in the MUTE register. The CS4270's 474b0c813ceSTimur Tabi * mute capability is intended for external muting circuitry, so if the 475b0c813ceSTimur Tabi * board does not have the MUTEA or MUTEB pins connected to such circuitry, 476b0c813ceSTimur Tabi * then this function will do nothing. 477b0c813ceSTimur Tabi */ 4781a4ba05eSDaniel Mack static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute) 479b0c813ceSTimur Tabi { 480b0c813ceSTimur Tabi struct snd_soc_codec *codec = dai->codec; 4811a4ba05eSDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 482b0c813ceSTimur Tabi int reg6; 483b0c813ceSTimur Tabi 484b0c813ceSTimur Tabi reg6 = snd_soc_read(codec, CS4270_MUTE); 485b0c813ceSTimur Tabi 486b0c813ceSTimur Tabi if (mute) 487d5e9ba1dSTimur Tabi reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; 4881a4ba05eSDaniel Mack else { 489d5e9ba1dSTimur Tabi reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); 4901a4ba05eSDaniel Mack reg6 |= cs4270->manual_mute; 4911a4ba05eSDaniel Mack } 492b0c813ceSTimur Tabi 493b0c813ceSTimur Tabi return snd_soc_write(codec, CS4270_MUTE, reg6); 494b0c813ceSTimur Tabi } 495b0c813ceSTimur Tabi 4961a4ba05eSDaniel Mack /** 4971a4ba05eSDaniel Mack * cs4270_soc_put_mute - put callback for the 'Master Playback switch' 4981a4ba05eSDaniel Mack * alsa control. 4991a4ba05eSDaniel Mack * @kcontrol: mixer control 5001a4ba05eSDaniel Mack * @ucontrol: control element information 5011a4ba05eSDaniel Mack * 5021a4ba05eSDaniel Mack * This function basically passes the arguments on to the generic 5031a4ba05eSDaniel Mack * snd_soc_put_volsw() function and saves the mute information in 5041a4ba05eSDaniel Mack * our private data structure. This is because we want to prevent 5051a4ba05eSDaniel Mack * cs4270_dai_mute() neglecting the user's decision to manually 5061a4ba05eSDaniel Mack * mute the codec's output. 5071a4ba05eSDaniel Mack * 5081a4ba05eSDaniel Mack * Returns 0 for success. 5091a4ba05eSDaniel Mack */ 5101a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol, 5111a4ba05eSDaniel Mack struct snd_ctl_elem_value *ucontrol) 5121a4ba05eSDaniel Mack { 5131a4ba05eSDaniel Mack struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 5141a4ba05eSDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 5151a4ba05eSDaniel Mack int left = !ucontrol->value.integer.value[0]; 5161a4ba05eSDaniel Mack int right = !ucontrol->value.integer.value[1]; 5171a4ba05eSDaniel Mack 5181a4ba05eSDaniel Mack cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) | 5191a4ba05eSDaniel Mack (right ? CS4270_MUTE_DAC_B : 0); 5201a4ba05eSDaniel Mack 5211a4ba05eSDaniel Mack return snd_soc_put_volsw(kcontrol, ucontrol); 5221a4ba05eSDaniel Mack } 5231a4ba05eSDaniel Mack 524b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */ 525b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = { 526b0c813ceSTimur Tabi SOC_DOUBLE_R("Master Playback Volume", 527d5e9ba1dSTimur Tabi CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), 528d5e9ba1dSTimur Tabi SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), 529d5e9ba1dSTimur Tabi SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), 530d5e9ba1dSTimur Tabi SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), 5317e1aa1dcSDaniel Mack SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0), 532d5e9ba1dSTimur Tabi SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), 533d5e9ba1dSTimur Tabi SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), 5341a4ba05eSDaniel Mack SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1), 5351a4ba05eSDaniel Mack SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1, 5361a4ba05eSDaniel Mack snd_soc_get_volsw, cs4270_soc_put_mute), 537b0c813ceSTimur Tabi }; 538b0c813ceSTimur Tabi 539b0c813ceSTimur Tabi /* 540ff7bf02fSTimur Tabi * cs4270_codec - global variable to store codec for the ASoC probe function 541b0c813ceSTimur Tabi * 542b0c813ceSTimur Tabi * If struct i2c_driver had a private_data field, we wouldn't need to use 54304eb093cSTimur Tabi * cs4270_codec. This is the only way to pass the codec structure from 54404eb093cSTimur Tabi * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good 54504eb093cSTimur Tabi * way to synchronize these two functions. cs4270_i2c_probe() can be called 54604eb093cSTimur Tabi * multiple times before cs4270_probe() is called even once. So for now, we 54704eb093cSTimur Tabi * also only allow cs4270_i2c_probe() to be run once. That means that we do 54804eb093cSTimur Tabi * not support more than one cs4270 device in the system, at least for now. 549b0c813ceSTimur Tabi */ 55004eb093cSTimur Tabi static struct snd_soc_codec *cs4270_codec; 551b0c813ceSTimur Tabi 5526335d055SEric Miao static struct snd_soc_dai_ops cs4270_dai_ops = { 5536335d055SEric Miao .hw_params = cs4270_hw_params, 5546335d055SEric Miao .set_sysclk = cs4270_set_dai_sysclk, 5556335d055SEric Miao .set_fmt = cs4270_set_dai_fmt, 5561a4ba05eSDaniel Mack .digital_mute = cs4270_dai_mute, 5576335d055SEric Miao }; 5586335d055SEric Miao 559e550e17fSLiam Girdwood struct snd_soc_dai cs4270_dai = { 5600db4d070STimur Tabi .name = "cs4270", 561b0c813ceSTimur Tabi .playback = { 562b0c813ceSTimur Tabi .stream_name = "Playback", 563b0c813ceSTimur Tabi .channels_min = 1, 564b0c813ceSTimur Tabi .channels_max = 2, 565b0c813ceSTimur Tabi .rates = 0, 566b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 567b0c813ceSTimur Tabi }, 568b0c813ceSTimur Tabi .capture = { 569b0c813ceSTimur Tabi .stream_name = "Capture", 570b0c813ceSTimur Tabi .channels_min = 1, 571b0c813ceSTimur Tabi .channels_max = 2, 572b0c813ceSTimur Tabi .rates = 0, 573b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 574b0c813ceSTimur Tabi }, 5756335d055SEric Miao .ops = &cs4270_dai_ops, 576b0c813ceSTimur Tabi }; 577b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai); 578b0c813ceSTimur Tabi 579ff7bf02fSTimur Tabi /** 580ff7bf02fSTimur Tabi * cs4270_probe - ASoC probe function 581ff7bf02fSTimur Tabi * @pdev: platform device 582ff7bf02fSTimur Tabi * 583ff7bf02fSTimur Tabi * This function is called when ASoC has all the pieces it needs to 584ff7bf02fSTimur Tabi * instantiate a sound driver. 58504eb093cSTimur Tabi */ 58604eb093cSTimur Tabi static int cs4270_probe(struct platform_device *pdev) 58704eb093cSTimur Tabi { 58804eb093cSTimur Tabi struct snd_soc_device *socdev = platform_get_drvdata(pdev); 58904eb093cSTimur Tabi struct snd_soc_codec *codec = cs4270_codec; 590*ffbfd336SDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 591*ffbfd336SDaniel Mack int i, ret; 59204eb093cSTimur Tabi 59304eb093cSTimur Tabi /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */ 59404eb093cSTimur Tabi socdev->card->codec = codec; 59504eb093cSTimur Tabi 59604eb093cSTimur Tabi /* Register PCMs */ 59704eb093cSTimur Tabi ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 59804eb093cSTimur Tabi if (ret < 0) { 599a6c255e0STimur Tabi dev_err(codec->dev, "failed to create pcms\n"); 60004eb093cSTimur Tabi return ret; 60104eb093cSTimur Tabi } 60204eb093cSTimur Tabi 60304eb093cSTimur Tabi /* Add the non-DAPM controls */ 604eb5f6d75SPhilipp Zabel ret = snd_soc_add_controls(codec, cs4270_snd_controls, 605eb5f6d75SPhilipp Zabel ARRAY_SIZE(cs4270_snd_controls)); 60604eb093cSTimur Tabi if (ret < 0) { 607eb5f6d75SPhilipp Zabel dev_err(codec->dev, "failed to add controls\n"); 60804eb093cSTimur Tabi goto error_free_pcms; 60904eb093cSTimur Tabi } 61004eb093cSTimur Tabi 611*ffbfd336SDaniel Mack /* get the power supply regulators */ 612*ffbfd336SDaniel Mack for (i = 0; i < ARRAY_SIZE(supply_names); i++) 613*ffbfd336SDaniel Mack cs4270->supplies[i].supply = supply_names[i]; 614*ffbfd336SDaniel Mack 615*ffbfd336SDaniel Mack ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies), 616*ffbfd336SDaniel Mack cs4270->supplies); 617*ffbfd336SDaniel Mack if (ret < 0) 618*ffbfd336SDaniel Mack goto error_free_pcms; 619*ffbfd336SDaniel Mack 62004eb093cSTimur Tabi return 0; 62104eb093cSTimur Tabi 62204eb093cSTimur Tabi error_free_pcms: 62304eb093cSTimur Tabi snd_soc_free_pcms(socdev); 62404eb093cSTimur Tabi 62504eb093cSTimur Tabi return ret; 62604eb093cSTimur Tabi } 62704eb093cSTimur Tabi 628ff7bf02fSTimur Tabi /** 629ff7bf02fSTimur Tabi * cs4270_remove - ASoC remove function 630ff7bf02fSTimur Tabi * @pdev: platform device 631ff7bf02fSTimur Tabi * 632ff7bf02fSTimur Tabi * This function is the counterpart to cs4270_probe(). 633ff7bf02fSTimur Tabi */ 63404eb093cSTimur Tabi static int cs4270_remove(struct platform_device *pdev) 63504eb093cSTimur Tabi { 63604eb093cSTimur Tabi struct snd_soc_device *socdev = platform_get_drvdata(pdev); 637*ffbfd336SDaniel Mack struct snd_soc_codec *codec = cs4270_codec; 638*ffbfd336SDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 63904eb093cSTimur Tabi 64004eb093cSTimur Tabi snd_soc_free_pcms(socdev); 641*ffbfd336SDaniel Mack regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies); 64204eb093cSTimur Tabi 64304eb093cSTimur Tabi return 0; 64404eb093cSTimur Tabi }; 64504eb093cSTimur Tabi 646ff7bf02fSTimur Tabi /** 647ff7bf02fSTimur Tabi * cs4270_i2c_probe - initialize the I2C interface of the CS4270 648ff7bf02fSTimur Tabi * @i2c_client: the I2C client object 649ff7bf02fSTimur Tabi * @id: the I2C device ID (ignored) 650b0c813ceSTimur Tabi * 651ff7bf02fSTimur Tabi * This function is called whenever the I2C subsystem finds a device that 652ff7bf02fSTimur Tabi * matches the device ID given via a prior call to i2c_add_driver(). 653b0c813ceSTimur Tabi */ 6540db4d070STimur Tabi static int cs4270_i2c_probe(struct i2c_client *i2c_client, 6550db4d070STimur Tabi const struct i2c_device_id *id) 656b0c813ceSTimur Tabi { 657b0c813ceSTimur Tabi struct snd_soc_codec *codec; 6580db4d070STimur Tabi struct cs4270_private *cs4270; 659d5e9ba1dSTimur Tabi unsigned int reg; 66004eb093cSTimur Tabi int ret; 66104eb093cSTimur Tabi 66204eb093cSTimur Tabi /* For now, we only support one cs4270 device in the system. See the 66304eb093cSTimur Tabi * comment for cs4270_codec. 66404eb093cSTimur Tabi */ 66504eb093cSTimur Tabi if (cs4270_codec) { 666a6c255e0STimur Tabi dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n", 66704eb093cSTimur Tabi i2c_client->addr); 668a6c255e0STimur Tabi dev_err(&i2c_client->dev, "only one per board allowed\n"); 66904eb093cSTimur Tabi /* Should we return something other than ENODEV here? */ 67004eb093cSTimur Tabi return -ENODEV; 67104eb093cSTimur Tabi } 672b0c813ceSTimur Tabi 6730db4d070STimur Tabi /* Verify that we have a CS4270 */ 6740db4d070STimur Tabi 6750db4d070STimur Tabi ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID); 6760db4d070STimur Tabi if (ret < 0) { 677a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", 67804eb093cSTimur Tabi i2c_client->addr); 6790db4d070STimur Tabi return ret; 6800db4d070STimur Tabi } 6810db4d070STimur Tabi /* The top four bits of the chip ID should be 1100. */ 6820db4d070STimur Tabi if ((ret & 0xF0) != 0xC0) { 683a6c255e0STimur Tabi dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", 6840db4d070STimur Tabi i2c_client->addr); 6850db4d070STimur Tabi return -ENODEV; 6860db4d070STimur Tabi } 6870db4d070STimur Tabi 688a6c255e0STimur Tabi dev_info(&i2c_client->dev, "found device at i2c address %X\n", 6890db4d070STimur Tabi i2c_client->addr); 690a6c255e0STimur Tabi dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF); 691b0c813ceSTimur Tabi 692b0c813ceSTimur Tabi /* Allocate enough space for the snd_soc_codec structure 693b0c813ceSTimur Tabi and our private data together. */ 6940db4d070STimur Tabi cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL); 6950db4d070STimur Tabi if (!cs4270) { 696a6c255e0STimur Tabi dev_err(&i2c_client->dev, "could not allocate codec\n"); 697b0c813ceSTimur Tabi return -ENOMEM; 698b0c813ceSTimur Tabi } 6990db4d070STimur Tabi codec = &cs4270->codec; 700b0c813ceSTimur Tabi 701b0c813ceSTimur Tabi mutex_init(&codec->mutex); 702b0c813ceSTimur Tabi INIT_LIST_HEAD(&codec->dapm_widgets); 703b0c813ceSTimur Tabi INIT_LIST_HEAD(&codec->dapm_paths); 704b0c813ceSTimur Tabi 705a6c255e0STimur Tabi codec->dev = &i2c_client->dev; 706b0c813ceSTimur Tabi codec->name = "CS4270"; 707b0c813ceSTimur Tabi codec->owner = THIS_MODULE; 708b0c813ceSTimur Tabi codec->dai = &cs4270_dai; 709b0c813ceSTimur Tabi codec->num_dai = 1; 7100db4d070STimur Tabi codec->private_data = cs4270; 7110db4d070STimur Tabi codec->control_data = i2c_client; 7120db4d070STimur Tabi codec->read = cs4270_read_reg_cache; 7130db4d070STimur Tabi codec->write = cs4270_i2c_write; 7140db4d070STimur Tabi codec->reg_cache = cs4270->reg_cache; 7150db4d070STimur Tabi codec->reg_cache_size = CS4270_NUMREGS; 716b0c813ceSTimur Tabi 7170db4d070STimur Tabi /* The I2C interface is set up, so pre-fill our register cache */ 7180db4d070STimur Tabi 7190db4d070STimur Tabi ret = cs4270_fill_cache(codec); 7200db4d070STimur Tabi if (ret < 0) { 721a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to fill register cache\n"); 7220db4d070STimur Tabi goto error_free_codec; 7230db4d070STimur Tabi } 724b0c813ceSTimur Tabi 725d5e9ba1dSTimur Tabi /* Disable auto-mute. This feature appears to be buggy. In some 726d5e9ba1dSTimur Tabi * situations, auto-mute will not deactivate when it should, so we want 727d5e9ba1dSTimur Tabi * this feature disabled by default. An application (e.g. alsactl) can 728d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 729d5e9ba1dSTimur Tabi */ 730d5e9ba1dSTimur Tabi 731d5e9ba1dSTimur Tabi reg = cs4270_read_reg_cache(codec, CS4270_MUTE); 732d5e9ba1dSTimur Tabi reg &= ~CS4270_MUTE_AUTO; 733d5e9ba1dSTimur Tabi ret = cs4270_i2c_write(codec, CS4270_MUTE, reg); 734d5e9ba1dSTimur Tabi if (ret < 0) { 735d5e9ba1dSTimur Tabi dev_err(&i2c_client->dev, "i2c write failed\n"); 736d5e9ba1dSTimur Tabi return ret; 737d5e9ba1dSTimur Tabi } 738d5e9ba1dSTimur Tabi 739d5e9ba1dSTimur Tabi /* Disable automatic volume control. The hardware enables, and it 740d5e9ba1dSTimur Tabi * causes volume change commands to be delayed, sometimes until after 741d5e9ba1dSTimur Tabi * playback has started. An application (e.g. alsactl) can 742d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 743d5e9ba1dSTimur Tabi */ 744d5e9ba1dSTimur Tabi 745d5e9ba1dSTimur Tabi reg = cs4270_read_reg_cache(codec, CS4270_TRANS); 746d5e9ba1dSTimur Tabi reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO); 747d5e9ba1dSTimur Tabi ret = cs4270_i2c_write(codec, CS4270_TRANS, reg); 748d5e9ba1dSTimur Tabi if (ret < 0) { 749d5e9ba1dSTimur Tabi dev_err(&i2c_client->dev, "i2c write failed\n"); 750d5e9ba1dSTimur Tabi return ret; 751d5e9ba1dSTimur Tabi } 752d5e9ba1dSTimur Tabi 753a6c255e0STimur Tabi /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI 754a6c255e0STimur Tabi * structure for each CS4270 device, but the machine driver needs to 755a6c255e0STimur Tabi * have a pointer to the DAI structure, so for now it must be a global 756a6c255e0STimur Tabi * variable. 757a6c255e0STimur Tabi */ 758a6c255e0STimur Tabi cs4270_dai.dev = &i2c_client->dev; 759a6c255e0STimur Tabi 76004eb093cSTimur Tabi /* Register the DAI. If all the other ASoC driver have already 76104eb093cSTimur Tabi * registered, then this will call our probe function, so 76204eb093cSTimur Tabi * cs4270_codec needs to be ready. 76304eb093cSTimur Tabi */ 764a6c255e0STimur Tabi cs4270_codec = codec; 76504eb093cSTimur Tabi ret = snd_soc_register_dai(&cs4270_dai); 766b0c813ceSTimur Tabi if (ret < 0) { 767a6c255e0STimur Tabi dev_err(&i2c_client->dev, "failed to register DAIe\n"); 768e3145dfbSJean Delvare goto error_free_codec; 769b0c813ceSTimur Tabi } 770b0c813ceSTimur Tabi 77104eb093cSTimur Tabi i2c_set_clientdata(i2c_client, cs4270); 772e3145dfbSJean Delvare 7730db4d070STimur Tabi return 0; 774e3145dfbSJean Delvare 775e3145dfbSJean Delvare error_free_codec: 7760db4d070STimur Tabi kfree(cs4270); 777a6c255e0STimur Tabi cs4270_codec = NULL; 778a6c255e0STimur Tabi cs4270_dai.dev = NULL; 779e3145dfbSJean Delvare 780b0c813ceSTimur Tabi return ret; 781b0c813ceSTimur Tabi } 782b0c813ceSTimur Tabi 783ff7bf02fSTimur Tabi /** 784ff7bf02fSTimur Tabi * cs4270_i2c_remove - remove an I2C device 785ff7bf02fSTimur Tabi * @i2c_client: the I2C client object 786ff7bf02fSTimur Tabi * 787ff7bf02fSTimur Tabi * This function is the counterpart to cs4270_i2c_probe(). 788ff7bf02fSTimur Tabi */ 7890db4d070STimur Tabi static int cs4270_i2c_remove(struct i2c_client *i2c_client) 790b0c813ceSTimur Tabi { 79104eb093cSTimur Tabi struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client); 792b0c813ceSTimur Tabi 7930db4d070STimur Tabi kfree(cs4270); 794a6c255e0STimur Tabi cs4270_codec = NULL; 795a6c255e0STimur Tabi cs4270_dai.dev = NULL; 796b0c813ceSTimur Tabi 7970db4d070STimur Tabi return 0; 7980db4d070STimur Tabi } 7990db4d070STimur Tabi 800ff7bf02fSTimur Tabi /* 801ff7bf02fSTimur Tabi * cs4270_id - I2C device IDs supported by this driver 802ff7bf02fSTimur Tabi */ 8030db4d070STimur Tabi static struct i2c_device_id cs4270_id[] = { 8040db4d070STimur Tabi {"cs4270", 0}, 8050db4d070STimur Tabi {} 8060db4d070STimur Tabi }; 8070db4d070STimur Tabi MODULE_DEVICE_TABLE(i2c, cs4270_id); 8080db4d070STimur Tabi 8095e7c0344SDaniel Mack #ifdef CONFIG_PM 8105e7c0344SDaniel Mack 8115e7c0344SDaniel Mack /* This suspend/resume implementation can handle both - a simple standby 8125e7c0344SDaniel Mack * where the codec remains powered, and a full suspend, where the voltage 8135e7c0344SDaniel Mack * domain the codec is connected to is teared down and/or any other hardware 8145e7c0344SDaniel Mack * reset condition is asserted. 8155e7c0344SDaniel Mack * 8165e7c0344SDaniel Mack * The codec's own power saving features are enabled in the suspend callback, 8175e7c0344SDaniel Mack * and all registers are written back to the hardware when resuming. 8185e7c0344SDaniel Mack */ 8195e7c0344SDaniel Mack 82015b5bdaeSDaniel Mack static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg) 82115b5bdaeSDaniel Mack { 82215b5bdaeSDaniel Mack struct snd_soc_codec *codec = cs4270_codec; 823*ffbfd336SDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 824*ffbfd336SDaniel Mack int reg, ret; 82515b5bdaeSDaniel Mack 826*ffbfd336SDaniel Mack reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL; 827*ffbfd336SDaniel Mack if (reg < 0) 828*ffbfd336SDaniel Mack return reg; 829*ffbfd336SDaniel Mack 830*ffbfd336SDaniel Mack ret = snd_soc_write(codec, CS4270_PWRCTL, reg); 831*ffbfd336SDaniel Mack if (ret < 0) 832*ffbfd336SDaniel Mack return ret; 833*ffbfd336SDaniel Mack 834*ffbfd336SDaniel Mack regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), 835*ffbfd336SDaniel Mack cs4270->supplies); 836*ffbfd336SDaniel Mack 837*ffbfd336SDaniel Mack return 0; 83815b5bdaeSDaniel Mack } 83915b5bdaeSDaniel Mack 84015b5bdaeSDaniel Mack static int cs4270_soc_resume(struct platform_device *pdev) 84115b5bdaeSDaniel Mack { 84215b5bdaeSDaniel Mack struct snd_soc_codec *codec = cs4270_codec; 843*ffbfd336SDaniel Mack struct cs4270_private *cs4270 = codec->private_data; 84415b5bdaeSDaniel Mack struct i2c_client *i2c_client = codec->control_data; 8455e7c0344SDaniel Mack int reg; 8465e7c0344SDaniel Mack 847*ffbfd336SDaniel Mack regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), 848*ffbfd336SDaniel Mack cs4270->supplies); 849*ffbfd336SDaniel Mack 8505e7c0344SDaniel Mack /* In case the device was put to hard reset during sleep, we need to 8515e7c0344SDaniel Mack * wait 500ns here before any I2C communication. */ 8525e7c0344SDaniel Mack ndelay(500); 8535e7c0344SDaniel Mack 8545e7c0344SDaniel Mack /* first restore the entire register cache ... */ 8555e7c0344SDaniel Mack for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) { 8565e7c0344SDaniel Mack u8 val = snd_soc_read(codec, reg); 8575e7c0344SDaniel Mack 85815b5bdaeSDaniel Mack if (i2c_smbus_write_byte_data(i2c_client, reg, val)) { 8595e7c0344SDaniel Mack dev_err(codec->dev, "i2c write failed\n"); 8605e7c0344SDaniel Mack return -EIO; 8615e7c0344SDaniel Mack } 8625e7c0344SDaniel Mack } 8635e7c0344SDaniel Mack 8645e7c0344SDaniel Mack /* ... then disable the power-down bits */ 8655e7c0344SDaniel Mack reg = snd_soc_read(codec, CS4270_PWRCTL); 8665e7c0344SDaniel Mack reg &= ~CS4270_PWRCTL_PDN_ALL; 8675e7c0344SDaniel Mack 8685e7c0344SDaniel Mack return snd_soc_write(codec, CS4270_PWRCTL, reg); 8695e7c0344SDaniel Mack } 8705e7c0344SDaniel Mack #else 87115b5bdaeSDaniel Mack #define cs4270_soc_suspend NULL 87215b5bdaeSDaniel Mack #define cs4270_soc_resume NULL 8735e7c0344SDaniel Mack #endif /* CONFIG_PM */ 8745e7c0344SDaniel Mack 875ff7bf02fSTimur Tabi /* 876ff7bf02fSTimur Tabi * cs4270_i2c_driver - I2C device identification 877ff7bf02fSTimur Tabi * 878ff7bf02fSTimur Tabi * This structure tells the I2C subsystem how to identify and support a 879ff7bf02fSTimur Tabi * given I2C device type. 880ff7bf02fSTimur Tabi */ 8810db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = { 8820db4d070STimur Tabi .driver = { 8830db4d070STimur Tabi .name = "cs4270", 8840db4d070STimur Tabi .owner = THIS_MODULE, 8850db4d070STimur Tabi }, 8860db4d070STimur Tabi .id_table = cs4270_id, 8870db4d070STimur Tabi .probe = cs4270_i2c_probe, 8880db4d070STimur Tabi .remove = cs4270_i2c_remove, 8890db4d070STimur Tabi }; 8900db4d070STimur Tabi 8910db4d070STimur Tabi /* 892b0c813ceSTimur Tabi * ASoC codec device structure 893b0c813ceSTimur Tabi * 894b0c813ceSTimur Tabi * Assign this variable to the codec_dev field of the machine driver's 895b0c813ceSTimur Tabi * snd_soc_device structure. 896b0c813ceSTimur Tabi */ 897b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = { 898b0c813ceSTimur Tabi .probe = cs4270_probe, 89915b5bdaeSDaniel Mack .remove = cs4270_remove, 90015b5bdaeSDaniel Mack .suspend = cs4270_soc_suspend, 90115b5bdaeSDaniel Mack .resume = cs4270_soc_resume, 902b0c813ceSTimur Tabi }; 903b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270); 904b0c813ceSTimur Tabi 905c9b3a40fSTakashi Iwai static int __init cs4270_init(void) 90664089b84SMark Brown { 907a6c255e0STimur Tabi pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n"); 9080db4d070STimur Tabi 90904eb093cSTimur Tabi return i2c_add_driver(&cs4270_i2c_driver); 91064089b84SMark Brown } 91164089b84SMark Brown module_init(cs4270_init); 91264089b84SMark Brown 91364089b84SMark Brown static void __exit cs4270_exit(void) 91464089b84SMark Brown { 91504eb093cSTimur Tabi i2c_del_driver(&cs4270_i2c_driver); 91664089b84SMark Brown } 91764089b84SMark Brown module_exit(cs4270_exit); 91864089b84SMark Brown 919b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 920b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); 921b0c813ceSTimur Tabi MODULE_LICENSE("GPL"); 922