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
24340d79a1SRob Herring #include <linux/mod_devicetable.h>
25b0c813ceSTimur Tabi #include <linux/module.h>
265a0e3ad6STejun Heo #include <linux/slab.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>
315e7c0344SDaniel Mack #include <linux/delay.h>
32ffbfd336SDaniel Mack #include <linux/regulator/consumer.h>
33f98acd8aSDaniel Mack #include <linux/gpio/consumer.h>
34b0c813ceSTimur Tabi
3599a4b91aSCharles Keepax #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
3699a4b91aSCharles Keepax SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
3799a4b91aSCharles Keepax SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
38b0c813ceSTimur Tabi
39b0c813ceSTimur Tabi /* CS4270 registers addresses */
40b0c813ceSTimur Tabi #define CS4270_CHIPID 0x01 /* Chip ID */
41b0c813ceSTimur Tabi #define CS4270_PWRCTL 0x02 /* Power Control */
42b0c813ceSTimur Tabi #define CS4270_MODE 0x03 /* Mode Control */
43b0c813ceSTimur Tabi #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
44b0c813ceSTimur Tabi #define CS4270_TRANS 0x05 /* Transition Control */
45b0c813ceSTimur Tabi #define CS4270_MUTE 0x06 /* Mute Control */
46b0c813ceSTimur Tabi #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
47b0c813ceSTimur Tabi #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
48b0c813ceSTimur Tabi
49b0c813ceSTimur Tabi #define CS4270_FIRSTREG 0x01
50b0c813ceSTimur Tabi #define CS4270_LASTREG 0x08
51b0c813ceSTimur Tabi #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
5280ab8817SDaniel Mack #define CS4270_I2C_INCR 0x80
53b0c813ceSTimur Tabi
54b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
55b0c813ceSTimur Tabi #define CS4270_CHIPID_ID 0xF0
56b0c813ceSTimur Tabi #define CS4270_CHIPID_REV 0x0F
57b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE 0x80
58b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC 0x20
59b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC 0x02
60b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN 0x01
615e7c0344SDaniel Mack #define CS4270_PWRCTL_PDN_ALL \
625e7c0344SDaniel Mack (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
63b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK 0x30
64b0c813ceSTimur Tabi #define CS4270_MODE_1X 0x00
65b0c813ceSTimur Tabi #define CS4270_MODE_2X 0x10
66b0c813ceSTimur Tabi #define CS4270_MODE_4X 0x20
67b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE 0x30
68b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK 0x0E
69b0c813ceSTimur Tabi #define CS4270_MODE_DIV1 0x00
70b0c813ceSTimur Tabi #define CS4270_MODE_DIV15 0x02
71b0c813ceSTimur Tabi #define CS4270_MODE_DIV2 0x04
72b0c813ceSTimur Tabi #define CS4270_MODE_DIV3 0x06
73b0c813ceSTimur Tabi #define CS4270_MODE_DIV4 0x08
74b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD 0x01
75b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A 0x80
76b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B 0x40
77b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK 0x20
78b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK 0x18
79b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ 0x00
80b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S 0x08
81b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16 0x18
82b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24 0x10
83b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK 0x01
84b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ 0x00
85b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S 0x01
86b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL 0x80
87b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT 0x40
88b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO 0x20
89b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A 0x08
90b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B 0x10
91b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A 0x02
92b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B 0x04
93b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH 0x01
94b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO 0x20
95b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A 0x08
96b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B 0x10
97b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY 0x04
98b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A 0x01
99b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B 0x02
100b0c813ceSTimur Tabi
10111b8fca5STimur Tabi /* Power-on default values for the registers
10211b8fca5STimur Tabi *
10311b8fca5STimur Tabi * This array contains the power-on default values of the registers, with the
10411b8fca5STimur Tabi * exception of the "CHIPID" register (01h). The lower four bits of that
10511b8fca5STimur Tabi * register contain the hardware revision, so it is treated as volatile.
10611b8fca5STimur Tabi */
1071ca65175SMark Brown static const struct reg_default cs4270_reg_defaults[] = {
1081ca65175SMark Brown { 2, 0x00 },
1091ca65175SMark Brown { 3, 0x30 },
1101ca65175SMark Brown { 4, 0x00 },
1111ca65175SMark Brown { 5, 0x60 },
1121ca65175SMark Brown { 6, 0x20 },
1131ca65175SMark Brown { 7, 0x00 },
1141ca65175SMark Brown { 8, 0x00 },
11511b8fca5STimur Tabi };
11611b8fca5STimur Tabi
117ffbfd336SDaniel Mack static const char *supply_names[] = {
118ffbfd336SDaniel Mack "va", "vd", "vlc"
119ffbfd336SDaniel Mack };
120ffbfd336SDaniel Mack
1210db4d070STimur Tabi /* Private data for the CS4270 */
1220db4d070STimur Tabi struct cs4270_private {
1231ca65175SMark Brown struct regmap *regmap;
1240db4d070STimur Tabi unsigned int mclk; /* Input frequency of the MCLK pin */
1250db4d070STimur Tabi unsigned int mode; /* The mode (I2S or left-justified) */
1264eae080dSDaniel Mack unsigned int slave_mode;
1271a4ba05eSDaniel Mack unsigned int manual_mute;
128ffbfd336SDaniel Mack
129ffbfd336SDaniel Mack /* power domain regulators */
130ffbfd336SDaniel Mack struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
131ccfc5316SMike Willard
132ccfc5316SMike Willard /* reset gpio */
133ccfc5316SMike Willard struct gpio_desc *reset_gpio;
1340db4d070STimur Tabi };
1350db4d070STimur Tabi
136782fbabaSMark Brown static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = {
137782fbabaSMark Brown SND_SOC_DAPM_INPUT("AINL"),
138782fbabaSMark Brown SND_SOC_DAPM_INPUT("AINR"),
139782fbabaSMark Brown
140782fbabaSMark Brown SND_SOC_DAPM_OUTPUT("AOUTL"),
141782fbabaSMark Brown SND_SOC_DAPM_OUTPUT("AOUTR"),
142782fbabaSMark Brown };
143782fbabaSMark Brown
144782fbabaSMark Brown static const struct snd_soc_dapm_route cs4270_dapm_routes[] = {
145aa5f9209Smurray foster { "Capture", NULL, "AINL" },
146aa5f9209Smurray foster { "Capture", NULL, "AINR" },
147782fbabaSMark Brown
148aa5f9209Smurray foster { "AOUTL", NULL, "Playback" },
149aa5f9209Smurray foster { "AOUTR", NULL, "Playback" },
150782fbabaSMark Brown };
151782fbabaSMark Brown
152ff7bf02fSTimur Tabi /**
153ff7bf02fSTimur Tabi * struct cs4270_mode_ratios - clock ratio tables
154ff7bf02fSTimur Tabi * @ratio: the ratio of MCLK to the sample rate
155ff7bf02fSTimur Tabi * @speed_mode: the Speed Mode bits to set in the Mode Control register for
156ff7bf02fSTimur Tabi * this ratio
157ff7bf02fSTimur Tabi * @mclk: the Ratio Select bits to set in the Mode Control register for this
158ff7bf02fSTimur Tabi * ratio
1598432395fSTimur Tabi *
1608432395fSTimur Tabi * The data for this chart is taken from Table 5 of the CS4270 reference
1618432395fSTimur Tabi * manual.
1628432395fSTimur Tabi *
1638432395fSTimur Tabi * This table is used to determine how to program the Mode Control register.
1648432395fSTimur Tabi * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
1658432395fSTimur Tabi * rates the CS4270 currently supports.
1668432395fSTimur Tabi *
167ff7bf02fSTimur Tabi * @speed_mode is the corresponding bit pattern to be written to the
1688432395fSTimur Tabi * MODE bits of the Mode Control Register
1698432395fSTimur Tabi *
170ff7bf02fSTimur Tabi * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
1718432395fSTimur Tabi * the Mode Control Register.
1728432395fSTimur Tabi *
1738432395fSTimur Tabi * In situations where a single ratio is represented by multiple speed
1748432395fSTimur Tabi * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
1758432395fSTimur Tabi * double-speed instead of quad-speed. However, the CS4270 errata states
176ff7bf02fSTimur Tabi * that divide-By-1.5 can cause failures, so we avoid that mode where
1778432395fSTimur Tabi * possible.
1788432395fSTimur Tabi *
179ff7bf02fSTimur Tabi * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
180ff7bf02fSTimur Tabi * work if Vd is 3.3V. If this effects you, select the
1818432395fSTimur Tabi * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
1828432395fSTimur Tabi * never select any sample rates that require divide-by-1.5.
1838432395fSTimur Tabi */
184ff7bf02fSTimur Tabi struct cs4270_mode_ratios {
1858432395fSTimur Tabi unsigned int ratio;
1868432395fSTimur Tabi u8 speed_mode;
1878432395fSTimur Tabi u8 mclk;
188ff7bf02fSTimur Tabi };
189ff7bf02fSTimur Tabi
190d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
1918432395fSTimur Tabi {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
1928432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
1938432395fSTimur Tabi {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
1948432395fSTimur Tabi #endif
1958432395fSTimur Tabi {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
1968432395fSTimur Tabi {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
1978432395fSTimur Tabi {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
1988432395fSTimur Tabi {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
1998432395fSTimur Tabi {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
2008432395fSTimur Tabi {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
2018432395fSTimur Tabi {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
2028432395fSTimur Tabi };
2038432395fSTimur Tabi
2048432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
2058432395fSTimur Tabi #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
2068432395fSTimur Tabi
cs4270_reg_is_readable(struct device * dev,unsigned int reg)2071ca65175SMark Brown static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg)
20811b8fca5STimur Tabi {
20911b8fca5STimur Tabi return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
21011b8fca5STimur Tabi }
21111b8fca5STimur Tabi
cs4270_reg_is_volatile(struct device * dev,unsigned int reg)2121ca65175SMark Brown static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg)
21311b8fca5STimur Tabi {
21411b8fca5STimur Tabi /* Unreadable registers are considered volatile */
21511b8fca5STimur Tabi if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
216c34c4515SGustavo A. R. Silva return true;
21711b8fca5STimur Tabi
21811b8fca5STimur Tabi return reg == CS4270_CHIPID;
21911b8fca5STimur Tabi }
22011b8fca5STimur Tabi
221ff7bf02fSTimur Tabi /**
222ff7bf02fSTimur Tabi * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
223ff7bf02fSTimur Tabi * @codec_dai: the codec DAI
224ff7bf02fSTimur Tabi * @clk_id: the clock ID (ignored)
225ff7bf02fSTimur Tabi * @freq: the MCLK input frequency
226ff7bf02fSTimur Tabi * @dir: the clock direction (ignored)
2278432395fSTimur Tabi *
228ff7bf02fSTimur Tabi * This function is used to tell the codec driver what the input MCLK
229ff7bf02fSTimur Tabi * frequency is.
2308432395fSTimur Tabi *
2318432395fSTimur Tabi * The value of MCLK is used to determine which sample rates are supported
2328432395fSTimur Tabi * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
233ff7bf02fSTimur Tabi * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
2348432395fSTimur Tabi *
2358432395fSTimur Tabi * This function calculates the nine ratios and determines which ones match
2368432395fSTimur Tabi * a standard sample rate. If there's a match, then it is added to the list
237ff7bf02fSTimur Tabi * of supported sample rates.
2388432395fSTimur Tabi *
2398432395fSTimur Tabi * This function must be called by the machine driver's 'startup' function,
2408432395fSTimur Tabi * otherwise the list of supported sample rates will not be available in
2418432395fSTimur Tabi * time for ALSA.
2426aababdfSDaniel Mack *
2436aababdfSDaniel Mack * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
2446aababdfSDaniel Mack * theoretically possible sample rates to be enabled. Call it again with a
2456aababdfSDaniel Mack * proper value set one the external clock is set (most probably you would do
2466aababdfSDaniel Mack * that from a machine's driver 'hw_param' hook.
2478432395fSTimur Tabi */
cs4270_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)248e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2498432395fSTimur Tabi int clk_id, unsigned int freq, int dir)
2508432395fSTimur Tabi {
25170c9a803SKuninori Morimoto struct snd_soc_component *component = codec_dai->component;
25270c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
2538432395fSTimur Tabi
2548432395fSTimur Tabi cs4270->mclk = freq;
2558432395fSTimur Tabi return 0;
2568432395fSTimur Tabi }
2578432395fSTimur Tabi
258ff7bf02fSTimur Tabi /**
259ff7bf02fSTimur Tabi * cs4270_set_dai_fmt - configure the codec for the selected audio format
260ff7bf02fSTimur Tabi * @codec_dai: the codec DAI
261ff7bf02fSTimur Tabi * @format: a SND_SOC_DAIFMT_x value indicating the data format
2628432395fSTimur Tabi *
2638432395fSTimur Tabi * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
2648432395fSTimur Tabi * codec accordingly.
2658432395fSTimur Tabi *
2668432395fSTimur Tabi * Currently, this function only supports SND_SOC_DAIFMT_I2S and
2678432395fSTimur Tabi * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
2688432395fSTimur Tabi * data for playback only, but ASoC currently does not support different
2698432395fSTimur Tabi * formats for playback vs. record.
2708432395fSTimur Tabi */
cs4270_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int format)271e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
2728432395fSTimur Tabi unsigned int format)
2738432395fSTimur Tabi {
27470c9a803SKuninori Morimoto struct snd_soc_component *component = codec_dai->component;
27570c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
2768432395fSTimur Tabi
2774eae080dSDaniel Mack /* set DAI format */
2788432395fSTimur Tabi switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
2798432395fSTimur Tabi case SND_SOC_DAIFMT_I2S:
2808432395fSTimur Tabi case SND_SOC_DAIFMT_LEFT_J:
2818432395fSTimur Tabi cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
2828432395fSTimur Tabi break;
2838432395fSTimur Tabi default:
28470c9a803SKuninori Morimoto dev_err(component->dev, "invalid dai format\n");
285ac60155fSAxel Lin return -EINVAL;
2868432395fSTimur Tabi }
2878432395fSTimur Tabi
2884eae080dSDaniel Mack /* set master/slave audio interface */
2894eae080dSDaniel Mack switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
2904eae080dSDaniel Mack case SND_SOC_DAIFMT_CBS_CFS:
2914eae080dSDaniel Mack cs4270->slave_mode = 1;
2924eae080dSDaniel Mack break;
2934eae080dSDaniel Mack case SND_SOC_DAIFMT_CBM_CFM:
2944eae080dSDaniel Mack cs4270->slave_mode = 0;
2954eae080dSDaniel Mack break;
2964eae080dSDaniel Mack default:
297ff09d49aSDaniel Mack /* all other modes are unsupported by the hardware */
29870c9a803SKuninori Morimoto dev_err(component->dev, "Unknown master/slave configuration\n");
299ac60155fSAxel Lin return -EINVAL;
3004eae080dSDaniel Mack }
3014eae080dSDaniel Mack
302ac60155fSAxel Lin return 0;
3038432395fSTimur Tabi }
3048432395fSTimur Tabi
305ff7bf02fSTimur Tabi /**
306ff7bf02fSTimur Tabi * cs4270_hw_params - program the CS4270 with the given hardware parameters.
307ff7bf02fSTimur Tabi * @substream: the audio stream
308ff7bf02fSTimur Tabi * @params: the hardware parameters to set
309ff7bf02fSTimur Tabi * @dai: the SOC DAI (ignored)
310b0c813ceSTimur Tabi *
311ff7bf02fSTimur Tabi * This function programs the hardware with the values provided.
312ff7bf02fSTimur Tabi * Specifically, the sample rate and the data format.
313ff7bf02fSTimur Tabi *
314ff7bf02fSTimur Tabi * The .ops functions are used to provide board-specific data, like input
315ff7bf02fSTimur Tabi * frequencies, to this driver. This function takes that information,
316b0c813ceSTimur Tabi * combines it with the hardware parameters provided, and programs the
317b0c813ceSTimur Tabi * hardware accordingly.
318b0c813ceSTimur Tabi */
cs4270_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)319b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
320dee89c4dSMark Brown struct snd_pcm_hw_params *params,
321dee89c4dSMark Brown struct snd_soc_dai *dai)
322b0c813ceSTimur Tabi {
32370c9a803SKuninori Morimoto struct snd_soc_component *component = dai->component;
32470c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
325e34ba212SRoel Kluin int ret;
326b0c813ceSTimur Tabi unsigned int i;
327b0c813ceSTimur Tabi unsigned int rate;
328b0c813ceSTimur Tabi unsigned int ratio;
329b0c813ceSTimur Tabi int reg;
330b0c813ceSTimur Tabi
331b0c813ceSTimur Tabi /* Figure out which MCLK/LRCK ratio to use */
332b0c813ceSTimur Tabi
333b0c813ceSTimur Tabi rate = params_rate(params); /* Sampling rate, in Hz */
334b0c813ceSTimur Tabi ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
335b0c813ceSTimur Tabi
3369dbd627bSTimur Tabi for (i = 0; i < NUM_MCLK_RATIOS; i++) {
3378432395fSTimur Tabi if (cs4270_mode_ratios[i].ratio == ratio)
338b0c813ceSTimur Tabi break;
339b0c813ceSTimur Tabi }
340b0c813ceSTimur Tabi
3419dbd627bSTimur Tabi if (i == NUM_MCLK_RATIOS) {
342b0c813ceSTimur Tabi /* We did not find a matching ratio */
34370c9a803SKuninori Morimoto dev_err(component->dev, "could not find matching ratio\n");
344b0c813ceSTimur Tabi return -EINVAL;
345b0c813ceSTimur Tabi }
346b0c813ceSTimur Tabi
347d5e9ba1dSTimur Tabi /* Set the sample rate */
348b0c813ceSTimur Tabi
349a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_MODE);
350b0c813ceSTimur Tabi reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
3514eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].mclk;
3524eae080dSDaniel Mack
3534eae080dSDaniel Mack if (cs4270->slave_mode)
3544eae080dSDaniel Mack reg |= CS4270_MODE_SLAVE;
3554eae080dSDaniel Mack else
3564eae080dSDaniel Mack reg |= cs4270_mode_ratios[i].speed_mode;
357b0c813ceSTimur Tabi
35870c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_MODE, reg);
359b0c813ceSTimur Tabi if (ret < 0) {
36070c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n");
361b0c813ceSTimur Tabi return ret;
362b0c813ceSTimur Tabi }
363b0c813ceSTimur Tabi
364d5e9ba1dSTimur Tabi /* Set the DAI format */
365b0c813ceSTimur Tabi
366a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_FORMAT);
367b0c813ceSTimur Tabi reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
368b0c813ceSTimur Tabi
369b0c813ceSTimur Tabi switch (cs4270->mode) {
370b0c813ceSTimur Tabi case SND_SOC_DAIFMT_I2S:
371b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
372b0c813ceSTimur Tabi break;
373b0c813ceSTimur Tabi case SND_SOC_DAIFMT_LEFT_J:
374b0c813ceSTimur Tabi reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
375b0c813ceSTimur Tabi break;
376b0c813ceSTimur Tabi default:
37770c9a803SKuninori Morimoto dev_err(component->dev, "unknown dai format\n");
378b0c813ceSTimur Tabi return -EINVAL;
379b0c813ceSTimur Tabi }
380b0c813ceSTimur Tabi
38170c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_FORMAT, reg);
382b0c813ceSTimur Tabi if (ret < 0) {
38370c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n");
384b0c813ceSTimur Tabi return ret;
385b0c813ceSTimur Tabi }
386b0c813ceSTimur Tabi
387b0c813ceSTimur Tabi return ret;
388b0c813ceSTimur Tabi }
389b0c813ceSTimur Tabi
390ff7bf02fSTimur Tabi /**
3911a4ba05eSDaniel Mack * cs4270_dai_mute - enable/disable the CS4270 external mute
392ff7bf02fSTimur Tabi * @dai: the SOC DAI
393ff7bf02fSTimur Tabi * @mute: 0 = disable mute, 1 = enable mute
39480cd7309SPierre-Louis Bossart * @direction: (ignored)
395b0c813ceSTimur Tabi *
396b0c813ceSTimur Tabi * This function toggles the mute bits in the MUTE register. The CS4270's
397b0c813ceSTimur Tabi * mute capability is intended for external muting circuitry, so if the
398b0c813ceSTimur Tabi * board does not have the MUTEA or MUTEB pins connected to such circuitry,
399b0c813ceSTimur Tabi * then this function will do nothing.
400b0c813ceSTimur Tabi */
cs4270_dai_mute(struct snd_soc_dai * dai,int mute,int direction)40103c0f1b5SKuninori Morimoto static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
402b0c813ceSTimur Tabi {
40370c9a803SKuninori Morimoto struct snd_soc_component *component = dai->component;
40470c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
405b0c813ceSTimur Tabi int reg6;
406b0c813ceSTimur Tabi
407a11f8a1cSKuninori Morimoto reg6 = snd_soc_component_read(component, CS4270_MUTE);
408b0c813ceSTimur Tabi
409b0c813ceSTimur Tabi if (mute)
410d5e9ba1dSTimur Tabi reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
4111a4ba05eSDaniel Mack else {
412d5e9ba1dSTimur Tabi reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
4131a4ba05eSDaniel Mack reg6 |= cs4270->manual_mute;
4141a4ba05eSDaniel Mack }
415b0c813ceSTimur Tabi
41670c9a803SKuninori Morimoto return snd_soc_component_write(component, CS4270_MUTE, reg6);
417b0c813ceSTimur Tabi }
418b0c813ceSTimur Tabi
4191a4ba05eSDaniel Mack /**
4201a4ba05eSDaniel Mack * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
4211a4ba05eSDaniel Mack * alsa control.
4221a4ba05eSDaniel Mack * @kcontrol: mixer control
4231a4ba05eSDaniel Mack * @ucontrol: control element information
4241a4ba05eSDaniel Mack *
4251a4ba05eSDaniel Mack * This function basically passes the arguments on to the generic
4261a4ba05eSDaniel Mack * snd_soc_put_volsw() function and saves the mute information in
4271a4ba05eSDaniel Mack * our private data structure. This is because we want to prevent
4281a4ba05eSDaniel Mack * cs4270_dai_mute() neglecting the user's decision to manually
4291a4ba05eSDaniel Mack * mute the codec's output.
4301a4ba05eSDaniel Mack *
4311a4ba05eSDaniel Mack * Returns 0 for success.
4321a4ba05eSDaniel Mack */
cs4270_soc_put_mute(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)4331a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
4341a4ba05eSDaniel Mack struct snd_ctl_elem_value *ucontrol)
4351a4ba05eSDaniel Mack {
43670c9a803SKuninori Morimoto struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
43770c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
4381a4ba05eSDaniel Mack int left = !ucontrol->value.integer.value[0];
4391a4ba05eSDaniel Mack int right = !ucontrol->value.integer.value[1];
4401a4ba05eSDaniel Mack
4411a4ba05eSDaniel Mack cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
4421a4ba05eSDaniel Mack (right ? CS4270_MUTE_DAC_B : 0);
4431a4ba05eSDaniel Mack
4441a4ba05eSDaniel Mack return snd_soc_put_volsw(kcontrol, ucontrol);
4451a4ba05eSDaniel Mack }
4461a4ba05eSDaniel Mack
447b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
448b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
449b0c813ceSTimur Tabi SOC_DOUBLE_R("Master Playback Volume",
450d5e9ba1dSTimur Tabi CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
451d5e9ba1dSTimur Tabi SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
452d5e9ba1dSTimur Tabi SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
453d5e9ba1dSTimur Tabi SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
4547e1aa1dcSDaniel Mack SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
455d5e9ba1dSTimur Tabi SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
456d5e9ba1dSTimur Tabi SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
4571a4ba05eSDaniel Mack SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
4581a4ba05eSDaniel Mack SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
4591a4ba05eSDaniel Mack snd_soc_get_volsw, cs4270_soc_put_mute),
460b0c813ceSTimur Tabi };
461b0c813ceSTimur Tabi
46285e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops cs4270_dai_ops = {
4636335d055SEric Miao .hw_params = cs4270_hw_params,
4646335d055SEric Miao .set_sysclk = cs4270_set_dai_sysclk,
4656335d055SEric Miao .set_fmt = cs4270_set_dai_fmt,
46603c0f1b5SKuninori Morimoto .mute_stream = cs4270_dai_mute,
46703c0f1b5SKuninori Morimoto .no_capture_mute = 1,
4686335d055SEric Miao };
4696335d055SEric Miao
4705c75848aSMark Brown static struct snd_soc_dai_driver cs4270_dai = {
471f0fba2adSLiam Girdwood .name = "cs4270-hifi",
472b0c813ceSTimur Tabi .playback = {
473b0c813ceSTimur Tabi .stream_name = "Playback",
474f76fe059SFabio Estevam .channels_min = 2,
475b0c813ceSTimur Tabi .channels_max = 2,
476f0fba2adSLiam Girdwood .rates = SNDRV_PCM_RATE_CONTINUOUS,
477f0fba2adSLiam Girdwood .rate_min = 4000,
478f0fba2adSLiam Girdwood .rate_max = 216000,
479b0c813ceSTimur Tabi .formats = CS4270_FORMATS,
480b0c813ceSTimur Tabi },
481b0c813ceSTimur Tabi .capture = {
482b0c813ceSTimur Tabi .stream_name = "Capture",
483f76fe059SFabio Estevam .channels_min = 2,
484b0c813ceSTimur Tabi .channels_max = 2,
485f0fba2adSLiam Girdwood .rates = SNDRV_PCM_RATE_CONTINUOUS,
486f0fba2adSLiam Girdwood .rate_min = 4000,
487f0fba2adSLiam Girdwood .rate_max = 216000,
488b0c813ceSTimur Tabi .formats = CS4270_FORMATS,
489b0c813ceSTimur Tabi },
4906335d055SEric Miao .ops = &cs4270_dai_ops,
491b0c813ceSTimur Tabi };
492b0c813ceSTimur Tabi
493ff7bf02fSTimur Tabi /**
494ff7bf02fSTimur Tabi * cs4270_probe - ASoC probe function
4957fdc1512SPierre-Louis Bossart * @component: ASoC component
496ff7bf02fSTimur Tabi *
497ff7bf02fSTimur Tabi * This function is called when ASoC has all the pieces it needs to
498ff7bf02fSTimur Tabi * instantiate a sound driver.
49904eb093cSTimur Tabi */
cs4270_probe(struct snd_soc_component * component)50070c9a803SKuninori Morimoto static int cs4270_probe(struct snd_soc_component *component)
50104eb093cSTimur Tabi {
50270c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
503b61d6d40SMark Brown int ret;
50404eb093cSTimur Tabi
505d5e9ba1dSTimur Tabi /* Disable auto-mute. This feature appears to be buggy. In some
506d5e9ba1dSTimur Tabi * situations, auto-mute will not deactivate when it should, so we want
507d5e9ba1dSTimur Tabi * this feature disabled by default. An application (e.g. alsactl) can
508d5e9ba1dSTimur Tabi * re-enabled it by using the controls.
509d5e9ba1dSTimur Tabi */
51070c9a803SKuninori Morimoto ret = snd_soc_component_update_bits(component, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
511d5e9ba1dSTimur Tabi if (ret < 0) {
51270c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n");
513d5e9ba1dSTimur Tabi return ret;
514d5e9ba1dSTimur Tabi }
515d5e9ba1dSTimur Tabi
516d5e9ba1dSTimur Tabi /* Disable automatic volume control. The hardware enables, and it
517d5e9ba1dSTimur Tabi * causes volume change commands to be delayed, sometimes until after
518d5e9ba1dSTimur Tabi * playback has started. An application (e.g. alsactl) can
519d5e9ba1dSTimur Tabi * re-enabled it by using the controls.
520d5e9ba1dSTimur Tabi */
52170c9a803SKuninori Morimoto ret = snd_soc_component_update_bits(component, CS4270_TRANS,
52211b8fca5STimur Tabi CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
523d5e9ba1dSTimur Tabi if (ret < 0) {
52470c9a803SKuninori Morimoto dev_err(component->dev, "i2c write failed\n");
525d5e9ba1dSTimur Tabi return ret;
526d5e9ba1dSTimur Tabi }
527d5e9ba1dSTimur Tabi
528f0fba2adSLiam Girdwood ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
529f0fba2adSLiam Girdwood cs4270->supplies);
530e3145dfbSJean Delvare
531b0c813ceSTimur Tabi return ret;
532b0c813ceSTimur Tabi }
533b0c813ceSTimur Tabi
534ff7bf02fSTimur Tabi /**
535f0fba2adSLiam Girdwood * cs4270_remove - ASoC remove function
5367fdc1512SPierre-Louis Bossart * @component: ASoC component
537ff7bf02fSTimur Tabi *
538f0fba2adSLiam Girdwood * This function is the counterpart to cs4270_probe().
539ff7bf02fSTimur Tabi */
cs4270_remove(struct snd_soc_component * component)54070c9a803SKuninori Morimoto static void cs4270_remove(struct snd_soc_component *component)
541b0c813ceSTimur Tabi {
54270c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
543b0c813ceSTimur Tabi
544f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
5450db4d070STimur Tabi };
5460db4d070STimur Tabi
5475e7c0344SDaniel Mack #ifdef CONFIG_PM
5485e7c0344SDaniel Mack
5495e7c0344SDaniel Mack /* This suspend/resume implementation can handle both - a simple standby
5505e7c0344SDaniel Mack * where the codec remains powered, and a full suspend, where the voltage
5515e7c0344SDaniel Mack * domain the codec is connected to is teared down and/or any other hardware
5525e7c0344SDaniel Mack * reset condition is asserted.
5535e7c0344SDaniel Mack *
5545e7c0344SDaniel Mack * The codec's own power saving features are enabled in the suspend callback,
5555e7c0344SDaniel Mack * and all registers are written back to the hardware when resuming.
5565e7c0344SDaniel Mack */
5575e7c0344SDaniel Mack
cs4270_soc_suspend(struct snd_soc_component * component)55870c9a803SKuninori Morimoto static int cs4270_soc_suspend(struct snd_soc_component *component)
55915b5bdaeSDaniel Mack {
56070c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
561ffbfd336SDaniel Mack int reg, ret;
56215b5bdaeSDaniel Mack
563a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
564ffbfd336SDaniel Mack if (reg < 0)
565ffbfd336SDaniel Mack return reg;
566ffbfd336SDaniel Mack
56770c9a803SKuninori Morimoto ret = snd_soc_component_write(component, CS4270_PWRCTL, reg);
568ffbfd336SDaniel Mack if (ret < 0)
569ffbfd336SDaniel Mack return ret;
570ffbfd336SDaniel Mack
571ffbfd336SDaniel Mack regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
572ffbfd336SDaniel Mack cs4270->supplies);
573ffbfd336SDaniel Mack
574ffbfd336SDaniel Mack return 0;
57515b5bdaeSDaniel Mack }
57615b5bdaeSDaniel Mack
cs4270_soc_resume(struct snd_soc_component * component)57770c9a803SKuninori Morimoto static int cs4270_soc_resume(struct snd_soc_component *component)
57815b5bdaeSDaniel Mack {
57970c9a803SKuninori Morimoto struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
580ab92d09dSMark Brown int reg, ret;
5815e7c0344SDaniel Mack
582ab92d09dSMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
583ffbfd336SDaniel Mack cs4270->supplies);
584ab92d09dSMark Brown if (ret != 0)
585ab92d09dSMark Brown return ret;
586ffbfd336SDaniel Mack
5875e7c0344SDaniel Mack /* In case the device was put to hard reset during sleep, we need to
5885e7c0344SDaniel Mack * wait 500ns here before any I2C communication. */
5895e7c0344SDaniel Mack ndelay(500);
5905e7c0344SDaniel Mack
5915e7c0344SDaniel Mack /* first restore the entire register cache ... */
5921ca65175SMark Brown regcache_sync(cs4270->regmap);
5935e7c0344SDaniel Mack
5945e7c0344SDaniel Mack /* ... then disable the power-down bits */
595a11f8a1cSKuninori Morimoto reg = snd_soc_component_read(component, CS4270_PWRCTL);
5965e7c0344SDaniel Mack reg &= ~CS4270_PWRCTL_PDN_ALL;
5975e7c0344SDaniel Mack
59870c9a803SKuninori Morimoto return snd_soc_component_write(component, CS4270_PWRCTL, reg);
5995e7c0344SDaniel Mack }
6005e7c0344SDaniel Mack #else
60115b5bdaeSDaniel Mack #define cs4270_soc_suspend NULL
60215b5bdaeSDaniel Mack #define cs4270_soc_resume NULL
6035e7c0344SDaniel Mack #endif /* CONFIG_PM */
6045e7c0344SDaniel Mack
605ff7bf02fSTimur Tabi /*
606b6f7d7c8SDaniel Mack * ASoC codec driver structure
607f0fba2adSLiam Girdwood */
60870c9a803SKuninori Morimoto static const struct snd_soc_component_driver soc_component_device_cs4270 = {
609f0fba2adSLiam Girdwood .probe = cs4270_probe,
610f0fba2adSLiam Girdwood .remove = cs4270_remove,
611f0fba2adSLiam Girdwood .suspend = cs4270_soc_suspend,
612f0fba2adSLiam Girdwood .resume = cs4270_soc_resume,
61319ace0e9SMark Brown .controls = cs4270_snd_controls,
61419ace0e9SMark Brown .num_controls = ARRAY_SIZE(cs4270_snd_controls),
615782fbabaSMark Brown .dapm_widgets = cs4270_dapm_widgets,
616782fbabaSMark Brown .num_dapm_widgets = ARRAY_SIZE(cs4270_dapm_widgets),
617782fbabaSMark Brown .dapm_routes = cs4270_dapm_routes,
618782fbabaSMark Brown .num_dapm_routes = ARRAY_SIZE(cs4270_dapm_routes),
61970c9a803SKuninori Morimoto .idle_bias_on = 1,
62070c9a803SKuninori Morimoto .use_pmdown_time = 1,
62170c9a803SKuninori Morimoto .endianness = 1,
622f0fba2adSLiam Girdwood };
623f0fba2adSLiam Girdwood
62485d07e4dSDaniel Mack /*
62585d07e4dSDaniel Mack * cs4270_of_match - the device tree bindings
62685d07e4dSDaniel Mack */
62785d07e4dSDaniel Mack static const struct of_device_id cs4270_of_match[] = {
62885d07e4dSDaniel Mack { .compatible = "cirrus,cs4270", },
62985d07e4dSDaniel Mack { }
63085d07e4dSDaniel Mack };
63185d07e4dSDaniel Mack MODULE_DEVICE_TABLE(of, cs4270_of_match);
63285d07e4dSDaniel Mack
6331ca65175SMark Brown static const struct regmap_config cs4270_regmap = {
6341ca65175SMark Brown .reg_bits = 8,
6351ca65175SMark Brown .val_bits = 8,
6361ca65175SMark Brown .max_register = CS4270_LASTREG,
6371ca65175SMark Brown .reg_defaults = cs4270_reg_defaults,
6381ca65175SMark Brown .num_reg_defaults = ARRAY_SIZE(cs4270_reg_defaults),
6397a2827adSMark Brown .cache_type = REGCACHE_MAPLE,
640f0f2338aSDaniel Mack .write_flag_mask = CS4270_I2C_INCR,
6411ca65175SMark Brown
6421ca65175SMark Brown .readable_reg = cs4270_reg_is_readable,
6431ca65175SMark Brown .volatile_reg = cs4270_reg_is_volatile,
6441ca65175SMark Brown };
6451ca65175SMark Brown
646f0fba2adSLiam Girdwood /**
647ccfc5316SMike Willard * cs4270_i2c_remove - deinitialize the I2C interface of the CS4270
648ccfc5316SMike Willard * @i2c_client: the I2C client object
649ccfc5316SMike Willard *
650ccfc5316SMike Willard * This function puts the chip into low power mode when the i2c device
651ccfc5316SMike Willard * is removed.
652ccfc5316SMike Willard */
cs4270_i2c_remove(struct i2c_client * i2c_client)653ed5c2f5fSUwe Kleine-König static void cs4270_i2c_remove(struct i2c_client *i2c_client)
654ccfc5316SMike Willard {
655ccfc5316SMike Willard struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
656ccfc5316SMike Willard
657ccfc5316SMike Willard gpiod_set_value_cansleep(cs4270->reset_gpio, 0);
658ccfc5316SMike Willard }
659ccfc5316SMike Willard
660ccfc5316SMike Willard /**
661f0fba2adSLiam Girdwood * cs4270_i2c_probe - initialize the I2C interface of the CS4270
662f0fba2adSLiam Girdwood * @i2c_client: the I2C client object
663f0fba2adSLiam Girdwood *
664f0fba2adSLiam Girdwood * This function is called whenever the I2C subsystem finds a device that
665f0fba2adSLiam Girdwood * matches the device ID given via a prior call to i2c_add_driver().
666f0fba2adSLiam Girdwood */
cs4270_i2c_probe(struct i2c_client * i2c_client)6674a404345SStephen Kitt static int cs4270_i2c_probe(struct i2c_client *i2c_client)
668f0fba2adSLiam Girdwood {
669f0fba2adSLiam Girdwood struct cs4270_private *cs4270;
6701ca65175SMark Brown unsigned int val;
671b61d6d40SMark Brown int ret, i;
672b61d6d40SMark Brown
673b61d6d40SMark Brown cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
674b61d6d40SMark Brown GFP_KERNEL);
6750e0327f2SSachin Kamat if (!cs4270)
676b61d6d40SMark Brown return -ENOMEM;
677b61d6d40SMark Brown
678b61d6d40SMark Brown /* get the power supply regulators */
679b61d6d40SMark Brown for (i = 0; i < ARRAY_SIZE(supply_names); i++)
680b61d6d40SMark Brown cs4270->supplies[i].supply = supply_names[i];
681b61d6d40SMark Brown
682b61d6d40SMark Brown ret = devm_regulator_bulk_get(&i2c_client->dev,
683b61d6d40SMark Brown ARRAY_SIZE(cs4270->supplies),
684b61d6d40SMark Brown cs4270->supplies);
685b61d6d40SMark Brown if (ret < 0)
686b61d6d40SMark Brown return ret;
687f0fba2adSLiam Girdwood
688ccfc5316SMike Willard /* reset the device */
689ccfc5316SMike Willard cs4270->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev, "reset",
690ccfc5316SMike Willard GPIOD_OUT_LOW);
691ccfc5316SMike Willard if (IS_ERR(cs4270->reset_gpio)) {
692ccfc5316SMike Willard dev_dbg(&i2c_client->dev, "Error getting CS4270 reset GPIO\n");
693ccfc5316SMike Willard return PTR_ERR(cs4270->reset_gpio);
694ccfc5316SMike Willard }
695ccfc5316SMike Willard
696ccfc5316SMike Willard if (cs4270->reset_gpio) {
697ccfc5316SMike Willard dev_dbg(&i2c_client->dev, "Found reset GPIO\n");
698ccfc5316SMike Willard gpiod_set_value_cansleep(cs4270->reset_gpio, 1);
699ccfc5316SMike Willard }
700ccfc5316SMike Willard
701ccfc5316SMike Willard /* Sleep 500ns before i2c communications */
702ccfc5316SMike Willard ndelay(500);
70302286190SDaniel Mack
7041ca65175SMark Brown cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap);
7051ca65175SMark Brown if (IS_ERR(cs4270->regmap))
7061ca65175SMark Brown return PTR_ERR(cs4270->regmap);
707f0fba2adSLiam Girdwood
7081ca65175SMark Brown /* Verify that we have a CS4270 */
7091ca65175SMark Brown ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val);
710f0fba2adSLiam Girdwood if (ret < 0) {
711f0fba2adSLiam Girdwood dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
712f0fba2adSLiam Girdwood i2c_client->addr);
713f0fba2adSLiam Girdwood return ret;
714f0fba2adSLiam Girdwood }
715f0fba2adSLiam Girdwood /* The top four bits of the chip ID should be 1100. */
7161ca65175SMark Brown if ((val & 0xF0) != 0xC0) {
717f0fba2adSLiam Girdwood dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
718f0fba2adSLiam Girdwood i2c_client->addr);
719f0fba2adSLiam Girdwood return -ENODEV;
720f0fba2adSLiam Girdwood }
721f0fba2adSLiam Girdwood
722f0fba2adSLiam Girdwood dev_info(&i2c_client->dev, "found device at i2c address %X\n",
723f0fba2adSLiam Girdwood i2c_client->addr);
7241ca65175SMark Brown dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF);
725f0fba2adSLiam Girdwood
726f0fba2adSLiam Girdwood i2c_set_clientdata(i2c_client, cs4270);
727f0fba2adSLiam Girdwood
72870c9a803SKuninori Morimoto ret = devm_snd_soc_register_component(&i2c_client->dev,
72970c9a803SKuninori Morimoto &soc_component_device_cs4270, &cs4270_dai, 1);
730f0fba2adSLiam Girdwood return ret;
731f0fba2adSLiam Girdwood }
732f0fba2adSLiam Girdwood
733f0fba2adSLiam Girdwood /*
734f0fba2adSLiam Girdwood * cs4270_id - I2C device IDs supported by this driver
735f0fba2adSLiam Girdwood */
73679a54ea1SAxel Lin static const struct i2c_device_id cs4270_id[] = {
737*ba2a2c37SUwe Kleine-König {"cs4270"},
738f0fba2adSLiam Girdwood {}
739f0fba2adSLiam Girdwood };
740f0fba2adSLiam Girdwood MODULE_DEVICE_TABLE(i2c, cs4270_id);
741f0fba2adSLiam Girdwood
742f0fba2adSLiam Girdwood /*
743ff7bf02fSTimur Tabi * cs4270_i2c_driver - I2C device identification
744ff7bf02fSTimur Tabi *
745ff7bf02fSTimur Tabi * This structure tells the I2C subsystem how to identify and support a
746ff7bf02fSTimur Tabi * given I2C device type.
747ff7bf02fSTimur Tabi */
7480db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = {
7490db4d070STimur Tabi .driver = {
75064902b29SShawn Guo .name = "cs4270",
75185d07e4dSDaniel Mack .of_match_table = cs4270_of_match,
7520db4d070STimur Tabi },
7530db4d070STimur Tabi .id_table = cs4270_id,
7549abcd240SUwe Kleine-König .probe = cs4270_i2c_probe,
755ccfc5316SMike Willard .remove = cs4270_i2c_remove,
7560db4d070STimur Tabi };
7570db4d070STimur Tabi
7585e383f53SSachin Kamat module_i2c_driver(cs4270_i2c_driver);
75964089b84SMark Brown
760b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
761b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
762b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
763