xref: /linux/sound/soc/codecs/cs4270.c (revision b6f7d7c8bf40800ac68e16302bb7627c59ea9168)
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>
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>
33b0c813ceSTimur Tabi 
34b0c813ceSTimur Tabi /*
35b0c813ceSTimur Tabi  * The codec isn't really big-endian or little-endian, since the I2S
36b0c813ceSTimur Tabi  * interface requires data to be sent serially with the MSbit first.
37b0c813ceSTimur Tabi  * However, to support BE and LE I2S devices, we specify both here.  That
38b0c813ceSTimur Tabi  * way, ALSA will always match the bit patterns.
39b0c813ceSTimur Tabi  */
40b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
41b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
42b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
46b0c813ceSTimur Tabi 
47b0c813ceSTimur Tabi /* CS4270 registers addresses */
48b0c813ceSTimur Tabi #define CS4270_CHIPID	0x01	/* Chip ID */
49b0c813ceSTimur Tabi #define CS4270_PWRCTL	0x02	/* Power Control */
50b0c813ceSTimur Tabi #define CS4270_MODE	0x03	/* Mode Control */
51b0c813ceSTimur Tabi #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
52b0c813ceSTimur Tabi #define CS4270_TRANS	0x05	/* Transition Control */
53b0c813ceSTimur Tabi #define CS4270_MUTE	0x06	/* Mute Control */
54b0c813ceSTimur Tabi #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
55b0c813ceSTimur Tabi #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
56b0c813ceSTimur Tabi 
57b0c813ceSTimur Tabi #define CS4270_FIRSTREG	0x01
58b0c813ceSTimur Tabi #define CS4270_LASTREG	0x08
59b0c813ceSTimur Tabi #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
6080ab8817SDaniel Mack #define CS4270_I2C_INCR	0x80
61b0c813ceSTimur Tabi 
62b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
63b0c813ceSTimur Tabi #define CS4270_CHIPID_ID	0xF0
64b0c813ceSTimur Tabi #define CS4270_CHIPID_REV	0x0F
65b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE	0x80
66b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC	0x20
67b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC	0x02
68b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN	0x01
695e7c0344SDaniel Mack #define CS4270_PWRCTL_PDN_ALL	\
705e7c0344SDaniel Mack 	(CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
71b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK	0x30
72b0c813ceSTimur Tabi #define CS4270_MODE_1X		0x00
73b0c813ceSTimur Tabi #define CS4270_MODE_2X		0x10
74b0c813ceSTimur Tabi #define CS4270_MODE_4X		0x20
75b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE	0x30
76b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK	0x0E
77b0c813ceSTimur Tabi #define CS4270_MODE_DIV1	0x00
78b0c813ceSTimur Tabi #define CS4270_MODE_DIV15	0x02
79b0c813ceSTimur Tabi #define CS4270_MODE_DIV2	0x04
80b0c813ceSTimur Tabi #define CS4270_MODE_DIV3	0x06
81b0c813ceSTimur Tabi #define CS4270_MODE_DIV4	0x08
82b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD	0x01
83b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A	0x80
84b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B	0x40
85b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK	0x20
86b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK	0x18
87b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ	0x00
88b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S	0x08
89b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16	0x18
90b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24	0x10
91b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK	0x01
92b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ	0x00
93b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S	0x01
94b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL	0x80
95b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT	0x40
96b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO	0x20
97b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A	0x08
98b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B	0x10
99b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A	0x02
100b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B	0x04
101b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH	0x01
102b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO	0x20
103b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A	0x08
104b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B	0x10
105b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY	0x04
106b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A	0x01
107b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B	0x02
108b0c813ceSTimur Tabi 
10911b8fca5STimur Tabi /* Power-on default values for the registers
11011b8fca5STimur Tabi  *
11111b8fca5STimur Tabi  * This array contains the power-on default values of the registers, with the
11211b8fca5STimur Tabi  * exception of the "CHIPID" register (01h).  The lower four bits of that
11311b8fca5STimur Tabi  * register contain the hardware revision, so it is treated as volatile.
11411b8fca5STimur Tabi  *
11511b8fca5STimur Tabi  * Also note that on the CS4270, the first readable register is 1, but ASoC
11611b8fca5STimur Tabi  * assumes the first register is 0.  Therfore, the array must have an entry for
11711b8fca5STimur Tabi  * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't
11811b8fca5STimur Tabi  * be read.
11911b8fca5STimur Tabi  */
12011b8fca5STimur Tabi static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = {
12111b8fca5STimur Tabi 	0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00
12211b8fca5STimur Tabi };
12311b8fca5STimur Tabi 
124ffbfd336SDaniel Mack static const char *supply_names[] = {
125ffbfd336SDaniel Mack 	"va", "vd", "vlc"
126ffbfd336SDaniel Mack };
127ffbfd336SDaniel Mack 
1280db4d070STimur Tabi /* Private data for the CS4270 */
1290db4d070STimur Tabi struct cs4270_private {
130f0fba2adSLiam Girdwood 	enum snd_soc_control_type control_type;
131f0fba2adSLiam Girdwood 	void *control_data;
1320db4d070STimur Tabi 	unsigned int mclk; /* Input frequency of the MCLK pin */
1330db4d070STimur Tabi 	unsigned int mode; /* The mode (I2S or left-justified) */
1344eae080dSDaniel Mack 	unsigned int slave_mode;
1351a4ba05eSDaniel Mack 	unsigned int manual_mute;
136ffbfd336SDaniel Mack 
137ffbfd336SDaniel Mack 	/* power domain regulators */
138ffbfd336SDaniel Mack 	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
1390db4d070STimur Tabi };
1400db4d070STimur Tabi 
141ff7bf02fSTimur Tabi /**
142ff7bf02fSTimur Tabi  * struct cs4270_mode_ratios - clock ratio tables
143ff7bf02fSTimur Tabi  * @ratio: the ratio of MCLK to the sample rate
144ff7bf02fSTimur Tabi  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
145ff7bf02fSTimur Tabi  *              this ratio
146ff7bf02fSTimur Tabi  * @mclk: the Ratio Select bits to set in the Mode Control register for this
147ff7bf02fSTimur Tabi  *        ratio
1488432395fSTimur Tabi  *
1498432395fSTimur Tabi  * The data for this chart is taken from Table 5 of the CS4270 reference
1508432395fSTimur Tabi  * manual.
1518432395fSTimur Tabi  *
1528432395fSTimur Tabi  * This table is used to determine how to program the Mode Control register.
1538432395fSTimur Tabi  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
1548432395fSTimur Tabi  * rates the CS4270 currently supports.
1558432395fSTimur Tabi  *
156ff7bf02fSTimur Tabi  * @speed_mode is the corresponding bit pattern to be written to the
1578432395fSTimur Tabi  * MODE bits of the Mode Control Register
1588432395fSTimur Tabi  *
159ff7bf02fSTimur Tabi  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
1608432395fSTimur Tabi  * the Mode Control Register.
1618432395fSTimur Tabi  *
1628432395fSTimur Tabi  * In situations where a single ratio is represented by multiple speed
1638432395fSTimur Tabi  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
1648432395fSTimur Tabi  * double-speed instead of quad-speed.  However, the CS4270 errata states
165ff7bf02fSTimur Tabi  * that divide-By-1.5 can cause failures, so we avoid that mode where
1668432395fSTimur Tabi  * possible.
1678432395fSTimur Tabi  *
168ff7bf02fSTimur Tabi  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
169ff7bf02fSTimur Tabi  * work if Vd is 3.3V.  If this effects you, select the
1708432395fSTimur Tabi  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
1718432395fSTimur Tabi  * never select any sample rates that require divide-by-1.5.
1728432395fSTimur Tabi  */
173ff7bf02fSTimur Tabi struct cs4270_mode_ratios {
1748432395fSTimur Tabi 	unsigned int ratio;
1758432395fSTimur Tabi 	u8 speed_mode;
1768432395fSTimur Tabi 	u8 mclk;
177ff7bf02fSTimur Tabi };
178ff7bf02fSTimur Tabi 
179d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
1808432395fSTimur Tabi 	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
1818432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
1828432395fSTimur Tabi 	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
1838432395fSTimur Tabi #endif
1848432395fSTimur Tabi 	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
1858432395fSTimur Tabi 	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
1868432395fSTimur Tabi 	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
1878432395fSTimur Tabi 	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
1888432395fSTimur Tabi 	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
1898432395fSTimur Tabi 	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
1908432395fSTimur Tabi 	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
1918432395fSTimur Tabi };
1928432395fSTimur Tabi 
1938432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
1948432395fSTimur Tabi #define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
1958432395fSTimur Tabi 
196d4754ec9SDimitris Papastamos static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
19711b8fca5STimur Tabi {
19811b8fca5STimur Tabi 	return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
19911b8fca5STimur Tabi }
20011b8fca5STimur Tabi 
201d4754ec9SDimitris Papastamos static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
20211b8fca5STimur Tabi {
20311b8fca5STimur Tabi 	/* Unreadable registers are considered volatile */
20411b8fca5STimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
20511b8fca5STimur Tabi 		return 1;
20611b8fca5STimur Tabi 
20711b8fca5STimur Tabi 	return reg == CS4270_CHIPID;
20811b8fca5STimur Tabi }
20911b8fca5STimur Tabi 
210ff7bf02fSTimur Tabi /**
211ff7bf02fSTimur Tabi  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
212ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
213ff7bf02fSTimur Tabi  * @clk_id: the clock ID (ignored)
214ff7bf02fSTimur Tabi  * @freq: the MCLK input frequency
215ff7bf02fSTimur Tabi  * @dir: the clock direction (ignored)
2168432395fSTimur Tabi  *
217ff7bf02fSTimur Tabi  * This function is used to tell the codec driver what the input MCLK
218ff7bf02fSTimur Tabi  * frequency is.
2198432395fSTimur Tabi  *
2208432395fSTimur Tabi  * The value of MCLK is used to determine which sample rates are supported
2218432395fSTimur Tabi  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
222ff7bf02fSTimur Tabi  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
2238432395fSTimur Tabi  *
2248432395fSTimur Tabi  * This function calculates the nine ratios and determines which ones match
2258432395fSTimur Tabi  * a standard sample rate.  If there's a match, then it is added to the list
226ff7bf02fSTimur Tabi  * of supported sample rates.
2278432395fSTimur Tabi  *
2288432395fSTimur Tabi  * This function must be called by the machine driver's 'startup' function,
2298432395fSTimur Tabi  * otherwise the list of supported sample rates will not be available in
2308432395fSTimur Tabi  * time for ALSA.
2316aababdfSDaniel Mack  *
2326aababdfSDaniel Mack  * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
2336aababdfSDaniel Mack  * theoretically possible sample rates to be enabled. Call it again with a
2346aababdfSDaniel Mack  * proper value set one the external clock is set (most probably you would do
2356aababdfSDaniel Mack  * that from a machine's driver 'hw_param' hook.
2368432395fSTimur Tabi  */
237e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2388432395fSTimur Tabi 				 int clk_id, unsigned int freq, int dir)
2398432395fSTimur Tabi {
2408432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
241b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
2428432395fSTimur Tabi 
2438432395fSTimur Tabi 	cs4270->mclk = freq;
2448432395fSTimur Tabi 	return 0;
2458432395fSTimur Tabi }
2468432395fSTimur Tabi 
247ff7bf02fSTimur Tabi /**
248ff7bf02fSTimur Tabi  * cs4270_set_dai_fmt - configure the codec for the selected audio format
249ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
250ff7bf02fSTimur Tabi  * @format: a SND_SOC_DAIFMT_x value indicating the data format
2518432395fSTimur Tabi  *
2528432395fSTimur Tabi  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
2538432395fSTimur Tabi  * codec accordingly.
2548432395fSTimur Tabi  *
2558432395fSTimur Tabi  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
2568432395fSTimur Tabi  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
2578432395fSTimur Tabi  * data for playback only, but ASoC currently does not support different
2588432395fSTimur Tabi  * formats for playback vs. record.
2598432395fSTimur Tabi  */
260e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
2618432395fSTimur Tabi 			      unsigned int format)
2628432395fSTimur Tabi {
2638432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
264b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
2658432395fSTimur Tabi 	int ret = 0;
2668432395fSTimur Tabi 
2674eae080dSDaniel Mack 	/* set DAI format */
2688432395fSTimur Tabi 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
2698432395fSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
2708432395fSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
2718432395fSTimur Tabi 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
2728432395fSTimur Tabi 		break;
2738432395fSTimur Tabi 	default:
274a6c255e0STimur Tabi 		dev_err(codec->dev, "invalid dai format\n");
2758432395fSTimur Tabi 		ret = -EINVAL;
2768432395fSTimur Tabi 	}
2778432395fSTimur Tabi 
2784eae080dSDaniel Mack 	/* set master/slave audio interface */
2794eae080dSDaniel Mack 	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
2804eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBS_CFS:
2814eae080dSDaniel Mack 		cs4270->slave_mode = 1;
2824eae080dSDaniel Mack 		break;
2834eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBM_CFM:
2844eae080dSDaniel Mack 		cs4270->slave_mode = 0;
2854eae080dSDaniel Mack 		break;
2864eae080dSDaniel Mack 	default:
287ff09d49aSDaniel Mack 		/* all other modes are unsupported by the hardware */
2884eae080dSDaniel Mack 		ret = -EINVAL;
2894eae080dSDaniel Mack 	}
2904eae080dSDaniel Mack 
2918432395fSTimur Tabi 	return ret;
2928432395fSTimur Tabi }
2938432395fSTimur Tabi 
294ff7bf02fSTimur Tabi /**
295ff7bf02fSTimur Tabi  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
296ff7bf02fSTimur Tabi  * @substream: the audio stream
297ff7bf02fSTimur Tabi  * @params: the hardware parameters to set
298ff7bf02fSTimur Tabi  * @dai: the SOC DAI (ignored)
299b0c813ceSTimur Tabi  *
300ff7bf02fSTimur Tabi  * This function programs the hardware with the values provided.
301ff7bf02fSTimur Tabi  * Specifically, the sample rate and the data format.
302ff7bf02fSTimur Tabi  *
303ff7bf02fSTimur Tabi  * The .ops functions are used to provide board-specific data, like input
304ff7bf02fSTimur Tabi  * frequencies, to this driver.  This function takes that information,
305b0c813ceSTimur Tabi  * combines it with the hardware parameters provided, and programs the
306b0c813ceSTimur Tabi  * hardware accordingly.
307b0c813ceSTimur Tabi  */
308b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
309dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
310dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
311b0c813ceSTimur Tabi {
312b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
313f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
314b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
315e34ba212SRoel Kluin 	int ret;
316b0c813ceSTimur Tabi 	unsigned int i;
317b0c813ceSTimur Tabi 	unsigned int rate;
318b0c813ceSTimur Tabi 	unsigned int ratio;
319b0c813ceSTimur Tabi 	int reg;
320b0c813ceSTimur Tabi 
321b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
322b0c813ceSTimur Tabi 
323b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
324b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
325b0c813ceSTimur Tabi 
3269dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
3278432395fSTimur Tabi 		if (cs4270_mode_ratios[i].ratio == ratio)
328b0c813ceSTimur Tabi 			break;
329b0c813ceSTimur Tabi 	}
330b0c813ceSTimur Tabi 
3319dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
332b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
333a6c255e0STimur Tabi 		dev_err(codec->dev, "could not find matching ratio\n");
334b0c813ceSTimur Tabi 		return -EINVAL;
335b0c813ceSTimur Tabi 	}
336b0c813ceSTimur Tabi 
337d5e9ba1dSTimur Tabi 	/* Set the sample rate */
338b0c813ceSTimur Tabi 
339b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
340b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
3414eae080dSDaniel Mack 	reg |= cs4270_mode_ratios[i].mclk;
3424eae080dSDaniel Mack 
3434eae080dSDaniel Mack 	if (cs4270->slave_mode)
3444eae080dSDaniel Mack 		reg |= CS4270_MODE_SLAVE;
3454eae080dSDaniel Mack 	else
3464eae080dSDaniel Mack 		reg |= cs4270_mode_ratios[i].speed_mode;
347b0c813ceSTimur Tabi 
348b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
349b0c813ceSTimur Tabi 	if (ret < 0) {
350a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
351b0c813ceSTimur Tabi 		return ret;
352b0c813ceSTimur Tabi 	}
353b0c813ceSTimur Tabi 
354d5e9ba1dSTimur Tabi 	/* Set the DAI format */
355b0c813ceSTimur Tabi 
356b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
357b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
358b0c813ceSTimur Tabi 
359b0c813ceSTimur Tabi 	switch (cs4270->mode) {
360b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
361b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
362b0c813ceSTimur Tabi 		break;
363b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
364b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
365b0c813ceSTimur Tabi 		break;
366b0c813ceSTimur Tabi 	default:
367a6c255e0STimur Tabi 		dev_err(codec->dev, "unknown dai format\n");
368b0c813ceSTimur Tabi 		return -EINVAL;
369b0c813ceSTimur Tabi 	}
370b0c813ceSTimur Tabi 
371b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
372b0c813ceSTimur Tabi 	if (ret < 0) {
373a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
374b0c813ceSTimur Tabi 		return ret;
375b0c813ceSTimur Tabi 	}
376b0c813ceSTimur Tabi 
377b0c813ceSTimur Tabi 	return ret;
378b0c813ceSTimur Tabi }
379b0c813ceSTimur Tabi 
380ff7bf02fSTimur Tabi /**
3811a4ba05eSDaniel Mack  * cs4270_dai_mute - enable/disable the CS4270 external mute
382ff7bf02fSTimur Tabi  * @dai: the SOC DAI
383ff7bf02fSTimur Tabi  * @mute: 0 = disable mute, 1 = enable mute
384b0c813ceSTimur Tabi  *
385b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
386b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
387b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
388b0c813ceSTimur Tabi  * then this function will do nothing.
389b0c813ceSTimur Tabi  */
3901a4ba05eSDaniel Mack static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
391b0c813ceSTimur Tabi {
392b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
393b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
394b0c813ceSTimur Tabi 	int reg6;
395b0c813ceSTimur Tabi 
396b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
397b0c813ceSTimur Tabi 
398b0c813ceSTimur Tabi 	if (mute)
399d5e9ba1dSTimur Tabi 		reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
4001a4ba05eSDaniel Mack 	else {
401d5e9ba1dSTimur Tabi 		reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
4021a4ba05eSDaniel Mack 		reg6 |= cs4270->manual_mute;
4031a4ba05eSDaniel Mack 	}
404b0c813ceSTimur Tabi 
405b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
406b0c813ceSTimur Tabi }
407b0c813ceSTimur Tabi 
4081a4ba05eSDaniel Mack /**
4091a4ba05eSDaniel Mack  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
4101a4ba05eSDaniel Mack  * 			 alsa control.
4111a4ba05eSDaniel Mack  * @kcontrol: mixer control
4121a4ba05eSDaniel Mack  * @ucontrol: control element information
4131a4ba05eSDaniel Mack  *
4141a4ba05eSDaniel Mack  * This function basically passes the arguments on to the generic
4151a4ba05eSDaniel Mack  * snd_soc_put_volsw() function and saves the mute information in
4161a4ba05eSDaniel Mack  * our private data structure. This is because we want to prevent
4171a4ba05eSDaniel Mack  * cs4270_dai_mute() neglecting the user's decision to manually
4181a4ba05eSDaniel Mack  * mute the codec's output.
4191a4ba05eSDaniel Mack  *
4201a4ba05eSDaniel Mack  * Returns 0 for success.
4211a4ba05eSDaniel Mack  */
4221a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
4231a4ba05eSDaniel Mack 				struct snd_ctl_elem_value *ucontrol)
4241a4ba05eSDaniel Mack {
4251a4ba05eSDaniel Mack 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
426b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
4271a4ba05eSDaniel Mack 	int left = !ucontrol->value.integer.value[0];
4281a4ba05eSDaniel Mack 	int right = !ucontrol->value.integer.value[1];
4291a4ba05eSDaniel Mack 
4301a4ba05eSDaniel Mack 	cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
4311a4ba05eSDaniel Mack 			      (right ? CS4270_MUTE_DAC_B : 0);
4321a4ba05eSDaniel Mack 
4331a4ba05eSDaniel Mack 	return snd_soc_put_volsw(kcontrol, ucontrol);
4341a4ba05eSDaniel Mack }
4351a4ba05eSDaniel Mack 
436b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
437b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
438b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
439d5e9ba1dSTimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
440d5e9ba1dSTimur Tabi 	SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
441d5e9ba1dSTimur Tabi 	SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
442d5e9ba1dSTimur Tabi 	SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
4437e1aa1dcSDaniel Mack 	SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
444d5e9ba1dSTimur Tabi 	SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
445d5e9ba1dSTimur Tabi 	SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
4461a4ba05eSDaniel Mack 	SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
4471a4ba05eSDaniel Mack 	SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
4481a4ba05eSDaniel Mack 		snd_soc_get_volsw, cs4270_soc_put_mute),
449b0c813ceSTimur Tabi };
450b0c813ceSTimur Tabi 
4516335d055SEric Miao static struct snd_soc_dai_ops cs4270_dai_ops = {
4526335d055SEric Miao 	.hw_params	= cs4270_hw_params,
4536335d055SEric Miao 	.set_sysclk	= cs4270_set_dai_sysclk,
4546335d055SEric Miao 	.set_fmt	= cs4270_set_dai_fmt,
4551a4ba05eSDaniel Mack 	.digital_mute	= cs4270_dai_mute,
4566335d055SEric Miao };
4576335d055SEric Miao 
4585c75848aSMark Brown static struct snd_soc_dai_driver cs4270_dai = {
459f0fba2adSLiam Girdwood 	.name = "cs4270-hifi",
460b0c813ceSTimur Tabi 	.playback = {
461b0c813ceSTimur Tabi 		.stream_name = "Playback",
462b0c813ceSTimur Tabi 		.channels_min = 1,
463b0c813ceSTimur Tabi 		.channels_max = 2,
464f0fba2adSLiam Girdwood 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
465f0fba2adSLiam Girdwood 		.rate_min = 4000,
466f0fba2adSLiam Girdwood 		.rate_max = 216000,
467b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
468b0c813ceSTimur Tabi 	},
469b0c813ceSTimur Tabi 	.capture = {
470b0c813ceSTimur Tabi 		.stream_name = "Capture",
471b0c813ceSTimur Tabi 		.channels_min = 1,
472b0c813ceSTimur Tabi 		.channels_max = 2,
473f0fba2adSLiam Girdwood 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
474f0fba2adSLiam Girdwood 		.rate_min = 4000,
475f0fba2adSLiam Girdwood 		.rate_max = 216000,
476b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
477b0c813ceSTimur Tabi 	},
4786335d055SEric Miao 	.ops = &cs4270_dai_ops,
479b0c813ceSTimur Tabi };
480b0c813ceSTimur Tabi 
481ff7bf02fSTimur Tabi /**
482ff7bf02fSTimur Tabi  * cs4270_probe - ASoC probe function
483ff7bf02fSTimur Tabi  * @pdev: platform device
484ff7bf02fSTimur Tabi  *
485ff7bf02fSTimur Tabi  * This function is called when ASoC has all the pieces it needs to
486ff7bf02fSTimur Tabi  * instantiate a sound driver.
48704eb093cSTimur Tabi  */
488f0fba2adSLiam Girdwood static int cs4270_probe(struct snd_soc_codec *codec)
48904eb093cSTimur Tabi {
490b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
49111b8fca5STimur Tabi 	int i, ret;
49204eb093cSTimur Tabi 
493f0fba2adSLiam Girdwood 	codec->control_data = cs4270->control_data;
494b0c813ceSTimur Tabi 
49511b8fca5STimur Tabi 	/* Tell ASoC what kind of I/O to use to read the registers.  ASoC will
49611b8fca5STimur Tabi 	 * then do the I2C transactions itself.
49711b8fca5STimur Tabi 	 */
49811b8fca5STimur Tabi 	ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type);
4990db4d070STimur Tabi 	if (ret < 0) {
50011b8fca5STimur Tabi 		dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
501f0fba2adSLiam Girdwood 		return ret;
5020db4d070STimur Tabi 	}
503b0c813ceSTimur Tabi 
504d5e9ba1dSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy.  In some
505d5e9ba1dSTimur Tabi 	 * situations, auto-mute will not deactivate when it should, so we want
506d5e9ba1dSTimur Tabi 	 * this feature disabled by default.  An application (e.g. alsactl) can
507d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
508d5e9ba1dSTimur Tabi 	 */
50911b8fca5STimur Tabi 	ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
510d5e9ba1dSTimur Tabi 	if (ret < 0) {
511f0fba2adSLiam Girdwood 		dev_err(codec->dev, "i2c write failed\n");
512d5e9ba1dSTimur Tabi 		return ret;
513d5e9ba1dSTimur Tabi 	}
514d5e9ba1dSTimur Tabi 
515d5e9ba1dSTimur Tabi 	/* Disable automatic volume control.  The hardware enables, and it
516d5e9ba1dSTimur Tabi 	 * causes volume change commands to be delayed, sometimes until after
517d5e9ba1dSTimur Tabi 	 * playback has started.  An application (e.g. alsactl) can
518d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
519d5e9ba1dSTimur Tabi 	 */
52011b8fca5STimur Tabi 	ret = snd_soc_update_bits(codec, CS4270_TRANS,
52111b8fca5STimur Tabi 		CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
522d5e9ba1dSTimur Tabi 	if (ret < 0) {
523f0fba2adSLiam Girdwood 		dev_err(codec->dev, "i2c write failed\n");
524d5e9ba1dSTimur Tabi 		return ret;
525d5e9ba1dSTimur Tabi 	}
526d5e9ba1dSTimur Tabi 
527f0fba2adSLiam Girdwood 	/* Add the non-DAPM controls */
528f0fba2adSLiam Girdwood 	ret = snd_soc_add_controls(codec, cs4270_snd_controls,
529f0fba2adSLiam Girdwood 				ARRAY_SIZE(cs4270_snd_controls));
530b0c813ceSTimur Tabi 	if (ret < 0) {
531f0fba2adSLiam Girdwood 		dev_err(codec->dev, "failed to add controls\n");
532f0fba2adSLiam Girdwood 		return ret;
533b0c813ceSTimur Tabi 	}
534b0c813ceSTimur Tabi 
535f0fba2adSLiam Girdwood 	/* get the power supply regulators */
536f0fba2adSLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
537f0fba2adSLiam Girdwood 		cs4270->supplies[i].supply = supply_names[i];
538f0fba2adSLiam Girdwood 
539f0fba2adSLiam Girdwood 	ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
540f0fba2adSLiam Girdwood 				 cs4270->supplies);
541f0fba2adSLiam Girdwood 	if (ret < 0)
542f0fba2adSLiam Girdwood 		return ret;
543f0fba2adSLiam Girdwood 
544f0fba2adSLiam Girdwood 	ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
545f0fba2adSLiam Girdwood 				    cs4270->supplies);
546f0fba2adSLiam Girdwood 	if (ret < 0)
547f0fba2adSLiam Girdwood 		goto error_free_regulators;
548e3145dfbSJean Delvare 
5490db4d070STimur Tabi 	return 0;
550e3145dfbSJean Delvare 
551f0fba2adSLiam Girdwood error_free_regulators:
552f0fba2adSLiam Girdwood 	regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
553f0fba2adSLiam Girdwood 			    cs4270->supplies);
554e3145dfbSJean Delvare 
555b0c813ceSTimur Tabi 	return ret;
556b0c813ceSTimur Tabi }
557b0c813ceSTimur Tabi 
558ff7bf02fSTimur Tabi /**
559f0fba2adSLiam Girdwood  * cs4270_remove - ASoC remove function
560f0fba2adSLiam Girdwood  * @pdev: platform device
561ff7bf02fSTimur Tabi  *
562f0fba2adSLiam Girdwood  * This function is the counterpart to cs4270_probe().
563ff7bf02fSTimur Tabi  */
564f0fba2adSLiam Girdwood static int cs4270_remove(struct snd_soc_codec *codec)
565b0c813ceSTimur Tabi {
566f0fba2adSLiam Girdwood 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
567b0c813ceSTimur Tabi 
568f0fba2adSLiam Girdwood 	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
569f0fba2adSLiam Girdwood 	regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
570b0c813ceSTimur Tabi 
5710db4d070STimur Tabi 	return 0;
5720db4d070STimur Tabi };
5730db4d070STimur Tabi 
5745e7c0344SDaniel Mack #ifdef CONFIG_PM
5755e7c0344SDaniel Mack 
5765e7c0344SDaniel Mack /* This suspend/resume implementation can handle both - a simple standby
5775e7c0344SDaniel Mack  * where the codec remains powered, and a full suspend, where the voltage
5785e7c0344SDaniel Mack  * domain the codec is connected to is teared down and/or any other hardware
5795e7c0344SDaniel Mack  * reset condition is asserted.
5805e7c0344SDaniel Mack  *
5815e7c0344SDaniel Mack  * The codec's own power saving features are enabled in the suspend callback,
5825e7c0344SDaniel Mack  * and all registers are written back to the hardware when resuming.
5835e7c0344SDaniel Mack  */
5845e7c0344SDaniel Mack 
585f0fba2adSLiam Girdwood static int cs4270_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg)
58615b5bdaeSDaniel Mack {
587b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
588ffbfd336SDaniel Mack 	int reg, ret;
58915b5bdaeSDaniel Mack 
590ffbfd336SDaniel Mack 	reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
591ffbfd336SDaniel Mack 	if (reg < 0)
592ffbfd336SDaniel Mack 		return reg;
593ffbfd336SDaniel Mack 
594ffbfd336SDaniel Mack 	ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
595ffbfd336SDaniel Mack 	if (ret < 0)
596ffbfd336SDaniel Mack 		return ret;
597ffbfd336SDaniel Mack 
598ffbfd336SDaniel Mack 	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
599ffbfd336SDaniel Mack 			       cs4270->supplies);
600ffbfd336SDaniel Mack 
601ffbfd336SDaniel Mack 	return 0;
60215b5bdaeSDaniel Mack }
60315b5bdaeSDaniel Mack 
604f0fba2adSLiam Girdwood static int cs4270_soc_resume(struct snd_soc_codec *codec)
60515b5bdaeSDaniel Mack {
606b2c812e2SMark Brown 	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
60715b5bdaeSDaniel Mack 	struct i2c_client *i2c_client = codec->control_data;
6085e7c0344SDaniel Mack 	int reg;
6095e7c0344SDaniel Mack 
610ffbfd336SDaniel Mack 	regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
611ffbfd336SDaniel Mack 			      cs4270->supplies);
612ffbfd336SDaniel Mack 
6135e7c0344SDaniel Mack 	/* In case the device was put to hard reset during sleep, we need to
6145e7c0344SDaniel Mack 	 * wait 500ns here before any I2C communication. */
6155e7c0344SDaniel Mack 	ndelay(500);
6165e7c0344SDaniel Mack 
6175e7c0344SDaniel Mack 	/* first restore the entire register cache ... */
6185e7c0344SDaniel Mack 	for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
6195e7c0344SDaniel Mack 		u8 val = snd_soc_read(codec, reg);
6205e7c0344SDaniel Mack 
62115b5bdaeSDaniel Mack 		if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
6225e7c0344SDaniel Mack 			dev_err(codec->dev, "i2c write failed\n");
6235e7c0344SDaniel Mack 			return -EIO;
6245e7c0344SDaniel Mack 		}
6255e7c0344SDaniel Mack 	}
6265e7c0344SDaniel Mack 
6275e7c0344SDaniel Mack 	/* ... then disable the power-down bits */
6285e7c0344SDaniel Mack 	reg = snd_soc_read(codec, CS4270_PWRCTL);
6295e7c0344SDaniel Mack 	reg &= ~CS4270_PWRCTL_PDN_ALL;
6305e7c0344SDaniel Mack 
6315e7c0344SDaniel Mack 	return snd_soc_write(codec, CS4270_PWRCTL, reg);
6325e7c0344SDaniel Mack }
6335e7c0344SDaniel Mack #else
63415b5bdaeSDaniel Mack #define cs4270_soc_suspend	NULL
63515b5bdaeSDaniel Mack #define cs4270_soc_resume	NULL
6365e7c0344SDaniel Mack #endif /* CONFIG_PM */
6375e7c0344SDaniel Mack 
638ff7bf02fSTimur Tabi /*
639*b6f7d7c8SDaniel Mack  * ASoC codec driver structure
640f0fba2adSLiam Girdwood  */
64111b8fca5STimur Tabi static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
642f0fba2adSLiam Girdwood 	.probe =		cs4270_probe,
643f0fba2adSLiam Girdwood 	.remove =		cs4270_remove,
644f0fba2adSLiam Girdwood 	.suspend =		cs4270_soc_suspend,
645f0fba2adSLiam Girdwood 	.resume =		cs4270_soc_resume,
64611b8fca5STimur Tabi 	.volatile_register =	cs4270_reg_is_volatile,
64711b8fca5STimur Tabi 	.readable_register =	cs4270_reg_is_readable,
64811b8fca5STimur Tabi 	.reg_cache_size =	CS4270_LASTREG + 1,
649f0fba2adSLiam Girdwood 	.reg_word_size =	sizeof(u8),
65011b8fca5STimur Tabi 	.reg_cache_default =	cs4270_default_reg_cache,
651f0fba2adSLiam Girdwood };
652f0fba2adSLiam Girdwood 
653f0fba2adSLiam Girdwood /**
654f0fba2adSLiam Girdwood  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
655f0fba2adSLiam Girdwood  * @i2c_client: the I2C client object
656f0fba2adSLiam Girdwood  * @id: the I2C device ID (ignored)
657f0fba2adSLiam Girdwood  *
658f0fba2adSLiam Girdwood  * This function is called whenever the I2C subsystem finds a device that
659f0fba2adSLiam Girdwood  * matches the device ID given via a prior call to i2c_add_driver().
660f0fba2adSLiam Girdwood  */
661f0fba2adSLiam Girdwood static int cs4270_i2c_probe(struct i2c_client *i2c_client,
662f0fba2adSLiam Girdwood 	const struct i2c_device_id *id)
663f0fba2adSLiam Girdwood {
664f0fba2adSLiam Girdwood 	struct cs4270_private *cs4270;
665f0fba2adSLiam Girdwood 	int ret;
666f0fba2adSLiam Girdwood 
667f0fba2adSLiam Girdwood 	/* Verify that we have a CS4270 */
668f0fba2adSLiam Girdwood 
669f0fba2adSLiam Girdwood 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
670f0fba2adSLiam Girdwood 	if (ret < 0) {
671f0fba2adSLiam Girdwood 		dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
672f0fba2adSLiam Girdwood 		       i2c_client->addr);
673f0fba2adSLiam Girdwood 		return ret;
674f0fba2adSLiam Girdwood 	}
675f0fba2adSLiam Girdwood 	/* The top four bits of the chip ID should be 1100. */
676f0fba2adSLiam Girdwood 	if ((ret & 0xF0) != 0xC0) {
677f0fba2adSLiam Girdwood 		dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
678f0fba2adSLiam Girdwood 		       i2c_client->addr);
679f0fba2adSLiam Girdwood 		return -ENODEV;
680f0fba2adSLiam Girdwood 	}
681f0fba2adSLiam Girdwood 
682f0fba2adSLiam Girdwood 	dev_info(&i2c_client->dev, "found device at i2c address %X\n",
683f0fba2adSLiam Girdwood 		i2c_client->addr);
684f0fba2adSLiam Girdwood 	dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
685f0fba2adSLiam Girdwood 
686f0fba2adSLiam Girdwood 	cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
687f0fba2adSLiam Girdwood 	if (!cs4270) {
688f0fba2adSLiam Girdwood 		dev_err(&i2c_client->dev, "could not allocate codec\n");
689f0fba2adSLiam Girdwood 		return -ENOMEM;
690f0fba2adSLiam Girdwood 	}
691f0fba2adSLiam Girdwood 
692f0fba2adSLiam Girdwood 	i2c_set_clientdata(i2c_client, cs4270);
693f0fba2adSLiam Girdwood 	cs4270->control_data = i2c_client;
694f0fba2adSLiam Girdwood 	cs4270->control_type = SND_SOC_I2C;
695f0fba2adSLiam Girdwood 
696f0fba2adSLiam Girdwood 	ret = snd_soc_register_codec(&i2c_client->dev,
697f0fba2adSLiam Girdwood 			&soc_codec_device_cs4270, &cs4270_dai, 1);
698f0fba2adSLiam Girdwood 	if (ret < 0)
699f0fba2adSLiam Girdwood 		kfree(cs4270);
700f0fba2adSLiam Girdwood 	return ret;
701f0fba2adSLiam Girdwood }
702f0fba2adSLiam Girdwood 
703f0fba2adSLiam Girdwood /**
704f0fba2adSLiam Girdwood  * cs4270_i2c_remove - remove an I2C device
705f0fba2adSLiam Girdwood  * @i2c_client: the I2C client object
706f0fba2adSLiam Girdwood  *
707f0fba2adSLiam Girdwood  * This function is the counterpart to cs4270_i2c_probe().
708f0fba2adSLiam Girdwood  */
709f0fba2adSLiam Girdwood static int cs4270_i2c_remove(struct i2c_client *i2c_client)
710f0fba2adSLiam Girdwood {
711f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&i2c_client->dev);
712f0fba2adSLiam Girdwood 	kfree(i2c_get_clientdata(i2c_client));
713f0fba2adSLiam Girdwood 	return 0;
714f0fba2adSLiam Girdwood }
715f0fba2adSLiam Girdwood 
716f0fba2adSLiam Girdwood /*
717f0fba2adSLiam Girdwood  * cs4270_id - I2C device IDs supported by this driver
718f0fba2adSLiam Girdwood  */
71979a54ea1SAxel Lin static const struct i2c_device_id cs4270_id[] = {
720f0fba2adSLiam Girdwood 	{"cs4270", 0},
721f0fba2adSLiam Girdwood 	{}
722f0fba2adSLiam Girdwood };
723f0fba2adSLiam Girdwood MODULE_DEVICE_TABLE(i2c, cs4270_id);
724f0fba2adSLiam Girdwood 
725f0fba2adSLiam Girdwood /*
726ff7bf02fSTimur Tabi  * cs4270_i2c_driver - I2C device identification
727ff7bf02fSTimur Tabi  *
728ff7bf02fSTimur Tabi  * This structure tells the I2C subsystem how to identify and support a
729ff7bf02fSTimur Tabi  * given I2C device type.
730ff7bf02fSTimur Tabi  */
7310db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = {
7320db4d070STimur Tabi 	.driver = {
733f0fba2adSLiam Girdwood 		.name = "cs4270-codec",
7340db4d070STimur Tabi 		.owner = THIS_MODULE,
7350db4d070STimur Tabi 	},
7360db4d070STimur Tabi 	.id_table = cs4270_id,
7370db4d070STimur Tabi 	.probe = cs4270_i2c_probe,
7380db4d070STimur Tabi 	.remove = cs4270_i2c_remove,
7390db4d070STimur Tabi };
7400db4d070STimur Tabi 
741c9b3a40fSTakashi Iwai static int __init cs4270_init(void)
74264089b84SMark Brown {
74304eb093cSTimur Tabi 	return i2c_add_driver(&cs4270_i2c_driver);
74464089b84SMark Brown }
74564089b84SMark Brown module_init(cs4270_init);
74664089b84SMark Brown 
74764089b84SMark Brown static void __exit cs4270_exit(void)
74864089b84SMark Brown {
74904eb093cSTimur Tabi 	i2c_del_driver(&cs4270_i2c_driver);
75064089b84SMark Brown }
75164089b84SMark Brown module_exit(cs4270_exit);
75264089b84SMark Brown 
753b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
754b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
755b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
756