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> 255a0e3ad6STejun Heo #include <linux/slab.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> 31ffbfd336SDaniel Mack #include <linux/regulator/consumer.h> 32f98acd8aSDaniel Mack #include <linux/gpio/consumer.h> 3385d07e4dSDaniel Mack #include <linux/of_device.h> 34b0c813ceSTimur Tabi 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 11011b8fca5STimur Tabi /* Power-on default values for the registers 11111b8fca5STimur Tabi * 11211b8fca5STimur Tabi * This array contains the power-on default values of the registers, with the 11311b8fca5STimur Tabi * exception of the "CHIPID" register (01h). The lower four bits of that 11411b8fca5STimur Tabi * register contain the hardware revision, so it is treated as volatile. 11511b8fca5STimur Tabi */ 1161ca65175SMark Brown static const struct reg_default cs4270_reg_defaults[] = { 1171ca65175SMark Brown { 2, 0x00 }, 1181ca65175SMark Brown { 3, 0x30 }, 1191ca65175SMark Brown { 4, 0x00 }, 1201ca65175SMark Brown { 5, 0x60 }, 1211ca65175SMark Brown { 6, 0x20 }, 1221ca65175SMark Brown { 7, 0x00 }, 1231ca65175SMark Brown { 8, 0x00 }, 12411b8fca5STimur Tabi }; 12511b8fca5STimur Tabi 126ffbfd336SDaniel Mack static const char *supply_names[] = { 127ffbfd336SDaniel Mack "va", "vd", "vlc" 128ffbfd336SDaniel Mack }; 129ffbfd336SDaniel Mack 1300db4d070STimur Tabi /* Private data for the CS4270 */ 1310db4d070STimur Tabi struct cs4270_private { 1321ca65175SMark Brown struct regmap *regmap; 1330db4d070STimur Tabi unsigned int mclk; /* Input frequency of the MCLK pin */ 1340db4d070STimur Tabi unsigned int mode; /* The mode (I2S or left-justified) */ 1354eae080dSDaniel Mack unsigned int slave_mode; 1361a4ba05eSDaniel Mack unsigned int manual_mute; 137ffbfd336SDaniel Mack 138ffbfd336SDaniel Mack /* power domain regulators */ 139ffbfd336SDaniel Mack struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; 140ccfc5316SMike Willard 141ccfc5316SMike Willard /* reset gpio */ 142ccfc5316SMike Willard struct gpio_desc *reset_gpio; 1430db4d070STimur Tabi }; 1440db4d070STimur Tabi 145782fbabaSMark Brown static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = { 146782fbabaSMark Brown SND_SOC_DAPM_INPUT("AINL"), 147782fbabaSMark Brown SND_SOC_DAPM_INPUT("AINR"), 148782fbabaSMark Brown 149782fbabaSMark Brown SND_SOC_DAPM_OUTPUT("AOUTL"), 150782fbabaSMark Brown SND_SOC_DAPM_OUTPUT("AOUTR"), 151782fbabaSMark Brown }; 152782fbabaSMark Brown 153782fbabaSMark Brown static const struct snd_soc_dapm_route cs4270_dapm_routes[] = { 154aa5f9209Smurray foster { "Capture", NULL, "AINL" }, 155aa5f9209Smurray foster { "Capture", NULL, "AINR" }, 156782fbabaSMark Brown 157aa5f9209Smurray foster { "AOUTL", NULL, "Playback" }, 158aa5f9209Smurray foster { "AOUTR", NULL, "Playback" }, 159782fbabaSMark Brown }; 160782fbabaSMark Brown 161ff7bf02fSTimur Tabi /** 162ff7bf02fSTimur Tabi * struct cs4270_mode_ratios - clock ratio tables 163ff7bf02fSTimur Tabi * @ratio: the ratio of MCLK to the sample rate 164ff7bf02fSTimur Tabi * @speed_mode: the Speed Mode bits to set in the Mode Control register for 165ff7bf02fSTimur Tabi * this ratio 166ff7bf02fSTimur Tabi * @mclk: the Ratio Select bits to set in the Mode Control register for this 167ff7bf02fSTimur Tabi * ratio 1688432395fSTimur Tabi * 1698432395fSTimur Tabi * The data for this chart is taken from Table 5 of the CS4270 reference 1708432395fSTimur Tabi * manual. 1718432395fSTimur Tabi * 1728432395fSTimur Tabi * This table is used to determine how to program the Mode Control register. 1738432395fSTimur Tabi * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling 1748432395fSTimur Tabi * rates the CS4270 currently supports. 1758432395fSTimur Tabi * 176ff7bf02fSTimur Tabi * @speed_mode is the corresponding bit pattern to be written to the 1778432395fSTimur Tabi * MODE bits of the Mode Control Register 1788432395fSTimur Tabi * 179ff7bf02fSTimur Tabi * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of 1808432395fSTimur Tabi * the Mode Control Register. 1818432395fSTimur Tabi * 1828432395fSTimur Tabi * In situations where a single ratio is represented by multiple speed 1838432395fSTimur Tabi * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick 1848432395fSTimur Tabi * double-speed instead of quad-speed. However, the CS4270 errata states 185ff7bf02fSTimur Tabi * that divide-By-1.5 can cause failures, so we avoid that mode where 1868432395fSTimur Tabi * possible. 1878432395fSTimur Tabi * 188ff7bf02fSTimur Tabi * Errata: There is an errata for the CS4270 where divide-by-1.5 does not 189ff7bf02fSTimur Tabi * work if Vd is 3.3V. If this effects you, select the 1908432395fSTimur Tabi * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will 1918432395fSTimur Tabi * never select any sample rates that require divide-by-1.5. 1928432395fSTimur Tabi */ 193ff7bf02fSTimur Tabi struct cs4270_mode_ratios { 1948432395fSTimur Tabi unsigned int ratio; 1958432395fSTimur Tabi u8 speed_mode; 1968432395fSTimur Tabi u8 mclk; 197ff7bf02fSTimur Tabi }; 198ff7bf02fSTimur Tabi 199d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = { 2008432395fSTimur Tabi {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, 2018432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA 2028432395fSTimur Tabi {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, 2038432395fSTimur Tabi #endif 2048432395fSTimur Tabi {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, 2058432395fSTimur Tabi {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, 2068432395fSTimur Tabi {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, 2078432395fSTimur Tabi {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, 2088432395fSTimur Tabi {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, 2098432395fSTimur Tabi {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, 2108432395fSTimur Tabi {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} 2118432395fSTimur Tabi }; 2128432395fSTimur Tabi 2138432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */ 2148432395fSTimur Tabi #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) 2158432395fSTimur Tabi 2161ca65175SMark Brown static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg) 21711b8fca5STimur Tabi { 21811b8fca5STimur Tabi return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG); 21911b8fca5STimur Tabi } 22011b8fca5STimur Tabi 2211ca65175SMark Brown static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg) 22211b8fca5STimur Tabi { 22311b8fca5STimur Tabi /* Unreadable registers are considered volatile */ 22411b8fca5STimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) 225c34c4515SGustavo A. R. Silva return true; 22611b8fca5STimur Tabi 22711b8fca5STimur Tabi return reg == CS4270_CHIPID; 22811b8fca5STimur Tabi } 22911b8fca5STimur Tabi 230ff7bf02fSTimur Tabi /** 231ff7bf02fSTimur Tabi * cs4270_set_dai_sysclk - determine the CS4270 samples rates. 232ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 233ff7bf02fSTimur Tabi * @clk_id: the clock ID (ignored) 234ff7bf02fSTimur Tabi * @freq: the MCLK input frequency 235ff7bf02fSTimur Tabi * @dir: the clock direction (ignored) 2368432395fSTimur Tabi * 237ff7bf02fSTimur Tabi * This function is used to tell the codec driver what the input MCLK 238ff7bf02fSTimur Tabi * frequency is. 2398432395fSTimur Tabi * 2408432395fSTimur Tabi * The value of MCLK is used to determine which sample rates are supported 2418432395fSTimur Tabi * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine 242ff7bf02fSTimur Tabi * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. 2438432395fSTimur Tabi * 2448432395fSTimur Tabi * This function calculates the nine ratios and determines which ones match 2458432395fSTimur Tabi * a standard sample rate. If there's a match, then it is added to the list 246ff7bf02fSTimur Tabi * of supported sample rates. 2478432395fSTimur Tabi * 2488432395fSTimur Tabi * This function must be called by the machine driver's 'startup' function, 2498432395fSTimur Tabi * otherwise the list of supported sample rates will not be available in 2508432395fSTimur Tabi * time for ALSA. 2516aababdfSDaniel Mack * 2526aababdfSDaniel Mack * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause 2536aababdfSDaniel Mack * theoretically possible sample rates to be enabled. Call it again with a 2546aababdfSDaniel Mack * proper value set one the external clock is set (most probably you would do 2556aababdfSDaniel Mack * that from a machine's driver 'hw_param' hook. 2568432395fSTimur Tabi */ 257e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, 2588432395fSTimur Tabi int clk_id, unsigned int freq, int dir) 2598432395fSTimur Tabi { 26070c9a803SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 26170c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 2628432395fSTimur Tabi 2638432395fSTimur Tabi cs4270->mclk = freq; 2648432395fSTimur Tabi return 0; 2658432395fSTimur Tabi } 2668432395fSTimur Tabi 267ff7bf02fSTimur Tabi /** 268ff7bf02fSTimur Tabi * cs4270_set_dai_fmt - configure the codec for the selected audio format 269ff7bf02fSTimur Tabi * @codec_dai: the codec DAI 270ff7bf02fSTimur Tabi * @format: a SND_SOC_DAIFMT_x value indicating the data format 2718432395fSTimur Tabi * 2728432395fSTimur Tabi * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the 2738432395fSTimur Tabi * codec accordingly. 2748432395fSTimur Tabi * 2758432395fSTimur Tabi * Currently, this function only supports SND_SOC_DAIFMT_I2S and 2768432395fSTimur Tabi * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified 2778432395fSTimur Tabi * data for playback only, but ASoC currently does not support different 2788432395fSTimur Tabi * formats for playback vs. record. 2798432395fSTimur Tabi */ 280e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, 2818432395fSTimur Tabi unsigned int format) 2828432395fSTimur Tabi { 28370c9a803SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 28470c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 2858432395fSTimur Tabi 2864eae080dSDaniel Mack /* set DAI format */ 2878432395fSTimur Tabi switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 2888432395fSTimur Tabi case SND_SOC_DAIFMT_I2S: 2898432395fSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 2908432395fSTimur Tabi cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; 2918432395fSTimur Tabi break; 2928432395fSTimur Tabi default: 29370c9a803SKuninori Morimoto dev_err(component->dev, "invalid dai format\n"); 294ac60155fSAxel Lin return -EINVAL; 2958432395fSTimur Tabi } 2968432395fSTimur Tabi 2974eae080dSDaniel Mack /* set master/slave audio interface */ 2984eae080dSDaniel Mack switch (format & SND_SOC_DAIFMT_MASTER_MASK) { 2994eae080dSDaniel Mack case SND_SOC_DAIFMT_CBS_CFS: 3004eae080dSDaniel Mack cs4270->slave_mode = 1; 3014eae080dSDaniel Mack break; 3024eae080dSDaniel Mack case SND_SOC_DAIFMT_CBM_CFM: 3034eae080dSDaniel Mack cs4270->slave_mode = 0; 3044eae080dSDaniel Mack break; 3054eae080dSDaniel Mack default: 306ff09d49aSDaniel Mack /* all other modes are unsupported by the hardware */ 30770c9a803SKuninori Morimoto dev_err(component->dev, "Unknown master/slave configuration\n"); 308ac60155fSAxel Lin return -EINVAL; 3094eae080dSDaniel Mack } 3104eae080dSDaniel Mack 311ac60155fSAxel Lin return 0; 3128432395fSTimur Tabi } 3138432395fSTimur Tabi 314ff7bf02fSTimur Tabi /** 315ff7bf02fSTimur Tabi * cs4270_hw_params - program the CS4270 with the given hardware parameters. 316ff7bf02fSTimur Tabi * @substream: the audio stream 317ff7bf02fSTimur Tabi * @params: the hardware parameters to set 318ff7bf02fSTimur Tabi * @dai: the SOC DAI (ignored) 319b0c813ceSTimur Tabi * 320ff7bf02fSTimur Tabi * This function programs the hardware with the values provided. 321ff7bf02fSTimur Tabi * Specifically, the sample rate and the data format. 322ff7bf02fSTimur Tabi * 323ff7bf02fSTimur Tabi * The .ops functions are used to provide board-specific data, like input 324ff7bf02fSTimur Tabi * frequencies, to this driver. This function takes that information, 325b0c813ceSTimur Tabi * combines it with the hardware parameters provided, and programs the 326b0c813ceSTimur Tabi * hardware accordingly. 327b0c813ceSTimur Tabi */ 328b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream, 329dee89c4dSMark Brown struct snd_pcm_hw_params *params, 330dee89c4dSMark Brown struct snd_soc_dai *dai) 331b0c813ceSTimur Tabi { 33270c9a803SKuninori Morimoto struct snd_soc_component *component = dai->component; 33370c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 334e34ba212SRoel Kluin int ret; 335b0c813ceSTimur Tabi unsigned int i; 336b0c813ceSTimur Tabi unsigned int rate; 337b0c813ceSTimur Tabi unsigned int ratio; 338b0c813ceSTimur Tabi int reg; 339b0c813ceSTimur Tabi 340b0c813ceSTimur Tabi /* Figure out which MCLK/LRCK ratio to use */ 341b0c813ceSTimur Tabi 342b0c813ceSTimur Tabi rate = params_rate(params); /* Sampling rate, in Hz */ 343b0c813ceSTimur Tabi ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ 344b0c813ceSTimur Tabi 3459dbd627bSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) { 3468432395fSTimur Tabi if (cs4270_mode_ratios[i].ratio == ratio) 347b0c813ceSTimur Tabi break; 348b0c813ceSTimur Tabi } 349b0c813ceSTimur Tabi 3509dbd627bSTimur Tabi if (i == NUM_MCLK_RATIOS) { 351b0c813ceSTimur Tabi /* We did not find a matching ratio */ 35270c9a803SKuninori Morimoto dev_err(component->dev, "could not find matching ratio\n"); 353b0c813ceSTimur Tabi return -EINVAL; 354b0c813ceSTimur Tabi } 355b0c813ceSTimur Tabi 356d5e9ba1dSTimur Tabi /* Set the sample rate */ 357b0c813ceSTimur Tabi 358a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_MODE); 359b0c813ceSTimur Tabi reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); 3604eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].mclk; 3614eae080dSDaniel Mack 3624eae080dSDaniel Mack if (cs4270->slave_mode) 3634eae080dSDaniel Mack reg |= CS4270_MODE_SLAVE; 3644eae080dSDaniel Mack else 3654eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].speed_mode; 366b0c813ceSTimur Tabi 36770c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_MODE, reg); 368b0c813ceSTimur Tabi if (ret < 0) { 36970c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n"); 370b0c813ceSTimur Tabi return ret; 371b0c813ceSTimur Tabi } 372b0c813ceSTimur Tabi 373d5e9ba1dSTimur Tabi /* Set the DAI format */ 374b0c813ceSTimur Tabi 375a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_FORMAT); 376b0c813ceSTimur Tabi reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); 377b0c813ceSTimur Tabi 378b0c813ceSTimur Tabi switch (cs4270->mode) { 379b0c813ceSTimur Tabi case SND_SOC_DAIFMT_I2S: 380b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; 381b0c813ceSTimur Tabi break; 382b0c813ceSTimur Tabi case SND_SOC_DAIFMT_LEFT_J: 383b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; 384b0c813ceSTimur Tabi break; 385b0c813ceSTimur Tabi default: 38670c9a803SKuninori Morimoto dev_err(component->dev, "unknown dai format\n"); 387b0c813ceSTimur Tabi return -EINVAL; 388b0c813ceSTimur Tabi } 389b0c813ceSTimur Tabi 39070c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_FORMAT, reg); 391b0c813ceSTimur Tabi if (ret < 0) { 39270c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n"); 393b0c813ceSTimur Tabi return ret; 394b0c813ceSTimur Tabi } 395b0c813ceSTimur Tabi 396b0c813ceSTimur Tabi return ret; 397b0c813ceSTimur Tabi } 398b0c813ceSTimur Tabi 399ff7bf02fSTimur Tabi /** 4001a4ba05eSDaniel Mack * cs4270_dai_mute - enable/disable the CS4270 external mute 401ff7bf02fSTimur Tabi * @dai: the SOC DAI 402ff7bf02fSTimur Tabi * @mute: 0 = disable mute, 1 = enable mute 403b0c813ceSTimur Tabi * 404b0c813ceSTimur Tabi * This function toggles the mute bits in the MUTE register. The CS4270's 405b0c813ceSTimur Tabi * mute capability is intended for external muting circuitry, so if the 406b0c813ceSTimur Tabi * board does not have the MUTEA or MUTEB pins connected to such circuitry, 407b0c813ceSTimur Tabi * then this function will do nothing. 408b0c813ceSTimur Tabi */ 409*03c0f1b5SKuninori Morimoto static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute, int direction) 410b0c813ceSTimur Tabi { 41170c9a803SKuninori Morimoto struct snd_soc_component *component = dai->component; 41270c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 413b0c813ceSTimur Tabi int reg6; 414b0c813ceSTimur Tabi 415a11f8a1cSKuninori Morimoto reg6 = snd_soc_component_read(component, CS4270_MUTE); 416b0c813ceSTimur Tabi 417b0c813ceSTimur Tabi if (mute) 418d5e9ba1dSTimur Tabi reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; 4191a4ba05eSDaniel Mack else { 420d5e9ba1dSTimur Tabi reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); 4211a4ba05eSDaniel Mack reg6 |= cs4270->manual_mute; 4221a4ba05eSDaniel Mack } 423b0c813ceSTimur Tabi 42470c9a803SKuninori Morimoto return snd_soc_component_write(component, CS4270_MUTE, reg6); 425b0c813ceSTimur Tabi } 426b0c813ceSTimur Tabi 4271a4ba05eSDaniel Mack /** 4281a4ba05eSDaniel Mack * cs4270_soc_put_mute - put callback for the 'Master Playback switch' 4291a4ba05eSDaniel Mack * alsa control. 4301a4ba05eSDaniel Mack * @kcontrol: mixer control 4311a4ba05eSDaniel Mack * @ucontrol: control element information 4321a4ba05eSDaniel Mack * 4331a4ba05eSDaniel Mack * This function basically passes the arguments on to the generic 4341a4ba05eSDaniel Mack * snd_soc_put_volsw() function and saves the mute information in 4351a4ba05eSDaniel Mack * our private data structure. This is because we want to prevent 4361a4ba05eSDaniel Mack * cs4270_dai_mute() neglecting the user's decision to manually 4371a4ba05eSDaniel Mack * mute the codec's output. 4381a4ba05eSDaniel Mack * 4391a4ba05eSDaniel Mack * Returns 0 for success. 4401a4ba05eSDaniel Mack */ 4411a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol, 4421a4ba05eSDaniel Mack struct snd_ctl_elem_value *ucontrol) 4431a4ba05eSDaniel Mack { 44470c9a803SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 44570c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 4461a4ba05eSDaniel Mack int left = !ucontrol->value.integer.value[0]; 4471a4ba05eSDaniel Mack int right = !ucontrol->value.integer.value[1]; 4481a4ba05eSDaniel Mack 4491a4ba05eSDaniel Mack cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) | 4501a4ba05eSDaniel Mack (right ? CS4270_MUTE_DAC_B : 0); 4511a4ba05eSDaniel Mack 4521a4ba05eSDaniel Mack return snd_soc_put_volsw(kcontrol, ucontrol); 4531a4ba05eSDaniel Mack } 4541a4ba05eSDaniel Mack 455b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */ 456b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = { 457b0c813ceSTimur Tabi SOC_DOUBLE_R("Master Playback Volume", 458d5e9ba1dSTimur Tabi CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), 459d5e9ba1dSTimur Tabi SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), 460d5e9ba1dSTimur Tabi SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), 461d5e9ba1dSTimur Tabi SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), 4627e1aa1dcSDaniel Mack SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0), 463d5e9ba1dSTimur Tabi SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), 464d5e9ba1dSTimur Tabi SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), 4651a4ba05eSDaniel Mack SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1), 4661a4ba05eSDaniel Mack SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1, 4671a4ba05eSDaniel Mack snd_soc_get_volsw, cs4270_soc_put_mute), 468b0c813ceSTimur Tabi }; 469b0c813ceSTimur Tabi 47085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops cs4270_dai_ops = { 4716335d055SEric Miao .hw_params = cs4270_hw_params, 4726335d055SEric Miao .set_sysclk = cs4270_set_dai_sysclk, 4736335d055SEric Miao .set_fmt = cs4270_set_dai_fmt, 474*03c0f1b5SKuninori Morimoto .mute_stream = cs4270_dai_mute, 475*03c0f1b5SKuninori Morimoto .no_capture_mute = 1, 4766335d055SEric Miao }; 4776335d055SEric Miao 4785c75848aSMark Brown static struct snd_soc_dai_driver cs4270_dai = { 479f0fba2adSLiam Girdwood .name = "cs4270-hifi", 480b0c813ceSTimur Tabi .playback = { 481b0c813ceSTimur Tabi .stream_name = "Playback", 482f76fe059SFabio Estevam .channels_min = 2, 483b0c813ceSTimur Tabi .channels_max = 2, 484f0fba2adSLiam Girdwood .rates = SNDRV_PCM_RATE_CONTINUOUS, 485f0fba2adSLiam Girdwood .rate_min = 4000, 486f0fba2adSLiam Girdwood .rate_max = 216000, 487b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 488b0c813ceSTimur Tabi }, 489b0c813ceSTimur Tabi .capture = { 490b0c813ceSTimur Tabi .stream_name = "Capture", 491f76fe059SFabio Estevam .channels_min = 2, 492b0c813ceSTimur Tabi .channels_max = 2, 493f0fba2adSLiam Girdwood .rates = SNDRV_PCM_RATE_CONTINUOUS, 494f0fba2adSLiam Girdwood .rate_min = 4000, 495f0fba2adSLiam Girdwood .rate_max = 216000, 496b0c813ceSTimur Tabi .formats = CS4270_FORMATS, 497b0c813ceSTimur Tabi }, 4986335d055SEric Miao .ops = &cs4270_dai_ops, 499b0c813ceSTimur Tabi }; 500b0c813ceSTimur Tabi 501ff7bf02fSTimur Tabi /** 502ff7bf02fSTimur Tabi * cs4270_probe - ASoC probe function 5037fdc1512SPierre-Louis Bossart * @component: ASoC component 504ff7bf02fSTimur Tabi * 505ff7bf02fSTimur Tabi * This function is called when ASoC has all the pieces it needs to 506ff7bf02fSTimur Tabi * instantiate a sound driver. 50704eb093cSTimur Tabi */ 50870c9a803SKuninori Morimoto static int cs4270_probe(struct snd_soc_component *component) 50904eb093cSTimur Tabi { 51070c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 511b61d6d40SMark Brown int ret; 51204eb093cSTimur Tabi 513d5e9ba1dSTimur Tabi /* Disable auto-mute. This feature appears to be buggy. In some 514d5e9ba1dSTimur Tabi * situations, auto-mute will not deactivate when it should, so we want 515d5e9ba1dSTimur Tabi * this feature disabled by default. An application (e.g. alsactl) can 516d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 517d5e9ba1dSTimur Tabi */ 51870c9a803SKuninori Morimoto ret = snd_soc_component_update_bits(component, CS4270_MUTE, CS4270_MUTE_AUTO, 0); 519d5e9ba1dSTimur Tabi if (ret < 0) { 52070c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n"); 521d5e9ba1dSTimur Tabi return ret; 522d5e9ba1dSTimur Tabi } 523d5e9ba1dSTimur Tabi 524d5e9ba1dSTimur Tabi /* Disable automatic volume control. The hardware enables, and it 525d5e9ba1dSTimur Tabi * causes volume change commands to be delayed, sometimes until after 526d5e9ba1dSTimur Tabi * playback has started. An application (e.g. alsactl) can 527d5e9ba1dSTimur Tabi * re-enabled it by using the controls. 528d5e9ba1dSTimur Tabi */ 52970c9a803SKuninori Morimoto ret = snd_soc_component_update_bits(component, CS4270_TRANS, 53011b8fca5STimur Tabi CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0); 531d5e9ba1dSTimur Tabi if (ret < 0) { 53270c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n"); 533d5e9ba1dSTimur Tabi return ret; 534d5e9ba1dSTimur Tabi } 535d5e9ba1dSTimur Tabi 536f0fba2adSLiam Girdwood ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), 537f0fba2adSLiam Girdwood cs4270->supplies); 538e3145dfbSJean Delvare 539b0c813ceSTimur Tabi return ret; 540b0c813ceSTimur Tabi } 541b0c813ceSTimur Tabi 542ff7bf02fSTimur Tabi /** 543f0fba2adSLiam Girdwood * cs4270_remove - ASoC remove function 5447fdc1512SPierre-Louis Bossart * @component: ASoC component 545ff7bf02fSTimur Tabi * 546f0fba2adSLiam Girdwood * This function is the counterpart to cs4270_probe(). 547ff7bf02fSTimur Tabi */ 54870c9a803SKuninori Morimoto static void cs4270_remove(struct snd_soc_component *component) 549b0c813ceSTimur Tabi { 55070c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 551b0c813ceSTimur Tabi 552f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies); 5530db4d070STimur Tabi }; 5540db4d070STimur Tabi 5555e7c0344SDaniel Mack #ifdef CONFIG_PM 5565e7c0344SDaniel Mack 5575e7c0344SDaniel Mack /* This suspend/resume implementation can handle both - a simple standby 5585e7c0344SDaniel Mack * where the codec remains powered, and a full suspend, where the voltage 5595e7c0344SDaniel Mack * domain the codec is connected to is teared down and/or any other hardware 5605e7c0344SDaniel Mack * reset condition is asserted. 5615e7c0344SDaniel Mack * 5625e7c0344SDaniel Mack * The codec's own power saving features are enabled in the suspend callback, 5635e7c0344SDaniel Mack * and all registers are written back to the hardware when resuming. 5645e7c0344SDaniel Mack */ 5655e7c0344SDaniel Mack 56670c9a803SKuninori Morimoto static int cs4270_soc_suspend(struct snd_soc_component *component) 56715b5bdaeSDaniel Mack { 56870c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 569ffbfd336SDaniel Mack int reg, ret; 57015b5bdaeSDaniel Mack 571a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL; 572ffbfd336SDaniel Mack if (reg < 0) 573ffbfd336SDaniel Mack return reg; 574ffbfd336SDaniel Mack 57570c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_PWRCTL, reg); 576ffbfd336SDaniel Mack if (ret < 0) 577ffbfd336SDaniel Mack return ret; 578ffbfd336SDaniel Mack 579ffbfd336SDaniel Mack regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), 580ffbfd336SDaniel Mack cs4270->supplies); 581ffbfd336SDaniel Mack 582ffbfd336SDaniel Mack return 0; 58315b5bdaeSDaniel Mack } 58415b5bdaeSDaniel Mack 58570c9a803SKuninori Morimoto static int cs4270_soc_resume(struct snd_soc_component *component) 58615b5bdaeSDaniel Mack { 58770c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component); 588ab92d09dSMark Brown int reg, ret; 5895e7c0344SDaniel Mack 590ab92d09dSMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), 591ffbfd336SDaniel Mack cs4270->supplies); 592ab92d09dSMark Brown if (ret != 0) 593ab92d09dSMark Brown return ret; 594ffbfd336SDaniel Mack 5955e7c0344SDaniel Mack /* In case the device was put to hard reset during sleep, we need to 5965e7c0344SDaniel Mack * wait 500ns here before any I2C communication. */ 5975e7c0344SDaniel Mack ndelay(500); 5985e7c0344SDaniel Mack 5995e7c0344SDaniel Mack /* first restore the entire register cache ... */ 6001ca65175SMark Brown regcache_sync(cs4270->regmap); 6015e7c0344SDaniel Mack 6025e7c0344SDaniel Mack /* ... then disable the power-down bits */ 603a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_PWRCTL); 6045e7c0344SDaniel Mack reg &= ~CS4270_PWRCTL_PDN_ALL; 6055e7c0344SDaniel Mack 60670c9a803SKuninori Morimoto return snd_soc_component_write(component, CS4270_PWRCTL, reg); 6075e7c0344SDaniel Mack } 6085e7c0344SDaniel Mack #else 60915b5bdaeSDaniel Mack #define cs4270_soc_suspend NULL 61015b5bdaeSDaniel Mack #define cs4270_soc_resume NULL 6115e7c0344SDaniel Mack #endif /* CONFIG_PM */ 6125e7c0344SDaniel Mack 613ff7bf02fSTimur Tabi /* 614b6f7d7c8SDaniel Mack * ASoC codec driver structure 615f0fba2adSLiam Girdwood */ 61670c9a803SKuninori Morimoto static const struct snd_soc_component_driver soc_component_device_cs4270 = { 617f0fba2adSLiam Girdwood .probe = cs4270_probe, 618f0fba2adSLiam Girdwood .remove = cs4270_remove, 619f0fba2adSLiam Girdwood .suspend = cs4270_soc_suspend, 620f0fba2adSLiam Girdwood .resume = cs4270_soc_resume, 62119ace0e9SMark Brown .controls = cs4270_snd_controls, 62219ace0e9SMark Brown .num_controls = ARRAY_SIZE(cs4270_snd_controls), 623782fbabaSMark Brown .dapm_widgets = cs4270_dapm_widgets, 624782fbabaSMark Brown .num_dapm_widgets = ARRAY_SIZE(cs4270_dapm_widgets), 625782fbabaSMark Brown .dapm_routes = cs4270_dapm_routes, 626782fbabaSMark Brown .num_dapm_routes = ARRAY_SIZE(cs4270_dapm_routes), 62770c9a803SKuninori Morimoto .idle_bias_on = 1, 62870c9a803SKuninori Morimoto .use_pmdown_time = 1, 62970c9a803SKuninori Morimoto .endianness = 1, 63070c9a803SKuninori Morimoto .non_legacy_dai_naming = 1, 631f0fba2adSLiam Girdwood }; 632f0fba2adSLiam Girdwood 63385d07e4dSDaniel Mack /* 63485d07e4dSDaniel Mack * cs4270_of_match - the device tree bindings 63585d07e4dSDaniel Mack */ 63685d07e4dSDaniel Mack static const struct of_device_id cs4270_of_match[] = { 63785d07e4dSDaniel Mack { .compatible = "cirrus,cs4270", }, 63885d07e4dSDaniel Mack { } 63985d07e4dSDaniel Mack }; 64085d07e4dSDaniel Mack MODULE_DEVICE_TABLE(of, cs4270_of_match); 64185d07e4dSDaniel Mack 6421ca65175SMark Brown static const struct regmap_config cs4270_regmap = { 6431ca65175SMark Brown .reg_bits = 8, 6441ca65175SMark Brown .val_bits = 8, 6451ca65175SMark Brown .max_register = CS4270_LASTREG, 6461ca65175SMark Brown .reg_defaults = cs4270_reg_defaults, 6471ca65175SMark Brown .num_reg_defaults = ARRAY_SIZE(cs4270_reg_defaults), 6481ca65175SMark Brown .cache_type = REGCACHE_RBTREE, 649f0f2338aSDaniel Mack .write_flag_mask = CS4270_I2C_INCR, 6501ca65175SMark Brown 6511ca65175SMark Brown .readable_reg = cs4270_reg_is_readable, 6521ca65175SMark Brown .volatile_reg = cs4270_reg_is_volatile, 6531ca65175SMark Brown }; 6541ca65175SMark Brown 655f0fba2adSLiam Girdwood /** 656ccfc5316SMike Willard * cs4270_i2c_remove - deinitialize the I2C interface of the CS4270 657ccfc5316SMike Willard * @i2c_client: the I2C client object 658ccfc5316SMike Willard * 659ccfc5316SMike Willard * This function puts the chip into low power mode when the i2c device 660ccfc5316SMike Willard * is removed. 661ccfc5316SMike Willard */ 662ccfc5316SMike Willard static int cs4270_i2c_remove(struct i2c_client *i2c_client) 663ccfc5316SMike Willard { 664ccfc5316SMike Willard struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client); 665ccfc5316SMike Willard 666ccfc5316SMike Willard gpiod_set_value_cansleep(cs4270->reset_gpio, 0); 667ccfc5316SMike Willard 668ccfc5316SMike Willard return 0; 669ccfc5316SMike Willard } 670ccfc5316SMike Willard 671ccfc5316SMike Willard /** 672f0fba2adSLiam Girdwood * cs4270_i2c_probe - initialize the I2C interface of the CS4270 673f0fba2adSLiam Girdwood * @i2c_client: the I2C client object 674f0fba2adSLiam Girdwood * @id: the I2C device ID (ignored) 675f0fba2adSLiam Girdwood * 676f0fba2adSLiam Girdwood * This function is called whenever the I2C subsystem finds a device that 677f0fba2adSLiam Girdwood * matches the device ID given via a prior call to i2c_add_driver(). 678f0fba2adSLiam Girdwood */ 679f0fba2adSLiam Girdwood static int cs4270_i2c_probe(struct i2c_client *i2c_client, 680f0fba2adSLiam Girdwood const struct i2c_device_id *id) 681f0fba2adSLiam Girdwood { 682f0fba2adSLiam Girdwood struct cs4270_private *cs4270; 6831ca65175SMark Brown unsigned int val; 684b61d6d40SMark Brown int ret, i; 685b61d6d40SMark Brown 686b61d6d40SMark Brown cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private), 687b61d6d40SMark Brown GFP_KERNEL); 6880e0327f2SSachin Kamat if (!cs4270) 689b61d6d40SMark Brown return -ENOMEM; 690b61d6d40SMark Brown 691b61d6d40SMark Brown /* get the power supply regulators */ 692b61d6d40SMark Brown for (i = 0; i < ARRAY_SIZE(supply_names); i++) 693b61d6d40SMark Brown cs4270->supplies[i].supply = supply_names[i]; 694b61d6d40SMark Brown 695b61d6d40SMark Brown ret = devm_regulator_bulk_get(&i2c_client->dev, 696b61d6d40SMark Brown ARRAY_SIZE(cs4270->supplies), 697b61d6d40SMark Brown cs4270->supplies); 698b61d6d40SMark Brown if (ret < 0) 699b61d6d40SMark Brown return ret; 700f0fba2adSLiam Girdwood 701ccfc5316SMike Willard /* reset the device */ 702ccfc5316SMike Willard cs4270->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev, "reset", 703ccfc5316SMike Willard GPIOD_OUT_LOW); 704ccfc5316SMike Willard if (IS_ERR(cs4270->reset_gpio)) { 705ccfc5316SMike Willard dev_dbg(&i2c_client->dev, "Error getting CS4270 reset GPIO\n"); 706ccfc5316SMike Willard return PTR_ERR(cs4270->reset_gpio); 707ccfc5316SMike Willard } 708ccfc5316SMike Willard 709ccfc5316SMike Willard if (cs4270->reset_gpio) { 710ccfc5316SMike Willard dev_dbg(&i2c_client->dev, "Found reset GPIO\n"); 711ccfc5316SMike Willard gpiod_set_value_cansleep(cs4270->reset_gpio, 1); 712ccfc5316SMike Willard } 713ccfc5316SMike Willard 714ccfc5316SMike Willard /* Sleep 500ns before i2c communications */ 715ccfc5316SMike Willard ndelay(500); 71602286190SDaniel Mack 7171ca65175SMark Brown cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap); 7181ca65175SMark Brown if (IS_ERR(cs4270->regmap)) 7191ca65175SMark Brown return PTR_ERR(cs4270->regmap); 720f0fba2adSLiam Girdwood 7211ca65175SMark Brown /* Verify that we have a CS4270 */ 7221ca65175SMark Brown ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val); 723f0fba2adSLiam Girdwood if (ret < 0) { 724f0fba2adSLiam Girdwood dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", 725f0fba2adSLiam Girdwood i2c_client->addr); 726f0fba2adSLiam Girdwood return ret; 727f0fba2adSLiam Girdwood } 728f0fba2adSLiam Girdwood /* The top four bits of the chip ID should be 1100. */ 7291ca65175SMark Brown if ((val & 0xF0) != 0xC0) { 730f0fba2adSLiam Girdwood dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", 731f0fba2adSLiam Girdwood i2c_client->addr); 732f0fba2adSLiam Girdwood return -ENODEV; 733f0fba2adSLiam Girdwood } 734f0fba2adSLiam Girdwood 735f0fba2adSLiam Girdwood dev_info(&i2c_client->dev, "found device at i2c address %X\n", 736f0fba2adSLiam Girdwood i2c_client->addr); 7371ca65175SMark Brown dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF); 738f0fba2adSLiam Girdwood 739f0fba2adSLiam Girdwood i2c_set_clientdata(i2c_client, cs4270); 740f0fba2adSLiam Girdwood 74170c9a803SKuninori Morimoto ret = devm_snd_soc_register_component(&i2c_client->dev, 74270c9a803SKuninori Morimoto &soc_component_device_cs4270, &cs4270_dai, 1); 743f0fba2adSLiam Girdwood return ret; 744f0fba2adSLiam Girdwood } 745f0fba2adSLiam Girdwood 746f0fba2adSLiam Girdwood /* 747f0fba2adSLiam Girdwood * cs4270_id - I2C device IDs supported by this driver 748f0fba2adSLiam Girdwood */ 74979a54ea1SAxel Lin static const struct i2c_device_id cs4270_id[] = { 750f0fba2adSLiam Girdwood {"cs4270", 0}, 751f0fba2adSLiam Girdwood {} 752f0fba2adSLiam Girdwood }; 753f0fba2adSLiam Girdwood MODULE_DEVICE_TABLE(i2c, cs4270_id); 754f0fba2adSLiam Girdwood 755f0fba2adSLiam Girdwood /* 756ff7bf02fSTimur Tabi * cs4270_i2c_driver - I2C device identification 757ff7bf02fSTimur Tabi * 758ff7bf02fSTimur Tabi * This structure tells the I2C subsystem how to identify and support a 759ff7bf02fSTimur Tabi * given I2C device type. 760ff7bf02fSTimur Tabi */ 7610db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = { 7620db4d070STimur Tabi .driver = { 76364902b29SShawn Guo .name = "cs4270", 76485d07e4dSDaniel Mack .of_match_table = cs4270_of_match, 7650db4d070STimur Tabi }, 7660db4d070STimur Tabi .id_table = cs4270_id, 7670db4d070STimur Tabi .probe = cs4270_i2c_probe, 768ccfc5316SMike Willard .remove = cs4270_i2c_remove, 7690db4d070STimur Tabi }; 7700db4d070STimur Tabi 7715e383f53SSachin Kamat module_i2c_driver(cs4270_i2c_driver); 77264089b84SMark Brown 773b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 774b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); 775b0c813ceSTimur Tabi MODULE_LICENSE("GPL"); 776