xref: /linux/sound/soc/codecs/cs4270.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CS4270 ALSA SoC (ASoC) codec driver
4  *
5  * Author: Timur Tabi <timur@freescale.com>
6  *
7  * Copyright 2007-2009 Freescale Semiconductor, Inc.
8  *
9  * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
10  *
11  * Current features/limitations:
12  *
13  * - Software mode is supported.  Stand-alone mode is not supported.
14  * - Only I2C is supported, not SPI
15  * - Support for master and slave mode
16  * - The machine driver's 'startup' function must call
17  *   cs4270_set_dai_sysclk() with the value of MCLK.
18  * - Only I2S and left-justified modes are supported
19  * - Power management is supported
20  */
21 
22 #include <linux/mod_devicetable.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/gpio/consumer.h>
32 
33 #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | SNDRV_PCM_FMTBIT_S16_LE  | \
34 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
35 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
36 
37 /* CS4270 registers addresses */
38 #define CS4270_CHIPID	0x01	/* Chip ID */
39 #define CS4270_PWRCTL	0x02	/* Power Control */
40 #define CS4270_MODE	0x03	/* Mode Control */
41 #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
42 #define CS4270_TRANS	0x05	/* Transition Control */
43 #define CS4270_MUTE	0x06	/* Mute Control */
44 #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
45 #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
46 
47 #define CS4270_FIRSTREG	0x01
48 #define CS4270_LASTREG	0x08
49 #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
50 #define CS4270_I2C_INCR	0x80
51 
52 /* Bit masks for the CS4270 registers */
53 #define CS4270_CHIPID_ID	0xF0
54 #define CS4270_CHIPID_REV	0x0F
55 #define CS4270_PWRCTL_FREEZE	0x80
56 #define CS4270_PWRCTL_PDN_ADC	0x20
57 #define CS4270_PWRCTL_PDN_DAC	0x02
58 #define CS4270_PWRCTL_PDN	0x01
59 #define CS4270_PWRCTL_PDN_ALL	\
60 	(CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
61 #define CS4270_MODE_SPEED_MASK	0x30
62 #define CS4270_MODE_1X		0x00
63 #define CS4270_MODE_2X		0x10
64 #define CS4270_MODE_4X		0x20
65 #define CS4270_MODE_SLAVE	0x30
66 #define CS4270_MODE_DIV_MASK	0x0E
67 #define CS4270_MODE_DIV1	0x00
68 #define CS4270_MODE_DIV15	0x02
69 #define CS4270_MODE_DIV2	0x04
70 #define CS4270_MODE_DIV3	0x06
71 #define CS4270_MODE_DIV4	0x08
72 #define CS4270_MODE_POPGUARD	0x01
73 #define CS4270_FORMAT_FREEZE_A	0x80
74 #define CS4270_FORMAT_FREEZE_B	0x40
75 #define CS4270_FORMAT_LOOPBACK	0x20
76 #define CS4270_FORMAT_DAC_MASK	0x18
77 #define CS4270_FORMAT_DAC_LJ	0x00
78 #define CS4270_FORMAT_DAC_I2S	0x08
79 #define CS4270_FORMAT_DAC_RJ16	0x18
80 #define CS4270_FORMAT_DAC_RJ24	0x10
81 #define CS4270_FORMAT_ADC_MASK	0x01
82 #define CS4270_FORMAT_ADC_LJ	0x00
83 #define CS4270_FORMAT_ADC_I2S	0x01
84 #define CS4270_TRANS_ONE_VOL	0x80
85 #define CS4270_TRANS_SOFT	0x40
86 #define CS4270_TRANS_ZERO	0x20
87 #define CS4270_TRANS_INV_ADC_A	0x08
88 #define CS4270_TRANS_INV_ADC_B	0x10
89 #define CS4270_TRANS_INV_DAC_A	0x02
90 #define CS4270_TRANS_INV_DAC_B	0x04
91 #define CS4270_TRANS_DEEMPH	0x01
92 #define CS4270_MUTE_AUTO	0x20
93 #define CS4270_MUTE_ADC_A	0x08
94 #define CS4270_MUTE_ADC_B	0x10
95 #define CS4270_MUTE_POLARITY	0x04
96 #define CS4270_MUTE_DAC_A	0x01
97 #define CS4270_MUTE_DAC_B	0x02
98 
99 /* Power-on default values for the registers
100  *
101  * This array contains the power-on default values of the registers, with the
102  * exception of the "CHIPID" register (01h).  The lower four bits of that
103  * register contain the hardware revision, so it is treated as volatile.
104  */
105 static const struct reg_default cs4270_reg_defaults[] = {
106 	{ 2, 0x00 },
107 	{ 3, 0x30 },
108 	{ 4, 0x00 },
109 	{ 5, 0x60 },
110 	{ 6, 0x20 },
111 	{ 7, 0x00 },
112 	{ 8, 0x00 },
113 };
114 
115 static const char *supply_names[] = {
116 	"va", "vd", "vlc"
117 };
118 
119 /* Private data for the CS4270 */
120 struct cs4270_private {
121 	struct regmap *regmap;
122 	unsigned int mclk; /* Input frequency of the MCLK pin */
123 	unsigned int mode; /* The mode (I2S or left-justified) */
124 	unsigned int slave_mode;
125 	unsigned int manual_mute;
126 
127 	/* power domain regulators */
128 	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
129 
130 	/* reset gpio */
131 	struct gpio_desc *reset_gpio;
132 };
133 
134 static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = {
135 SND_SOC_DAPM_INPUT("AINL"),
136 SND_SOC_DAPM_INPUT("AINR"),
137 
138 SND_SOC_DAPM_OUTPUT("AOUTL"),
139 SND_SOC_DAPM_OUTPUT("AOUTR"),
140 };
141 
142 static const struct snd_soc_dapm_route cs4270_dapm_routes[] = {
143 	{ "Capture", NULL, "AINL" },
144 	{ "Capture", NULL, "AINR" },
145 
146 	{ "AOUTL", NULL, "Playback" },
147 	{ "AOUTR", NULL, "Playback" },
148 };
149 
150 /**
151  * struct cs4270_mode_ratios - clock ratio tables
152  * @ratio: the ratio of MCLK to the sample rate
153  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
154  *              this ratio
155  * @mclk: the Ratio Select bits to set in the Mode Control register for this
156  *        ratio
157  *
158  * The data for this chart is taken from Table 5 of the CS4270 reference
159  * manual.
160  *
161  * This table is used to determine how to program the Mode Control register.
162  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
163  * rates the CS4270 currently supports.
164  *
165  * @speed_mode is the corresponding bit pattern to be written to the
166  * MODE bits of the Mode Control Register
167  *
168  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
169  * the Mode Control Register.
170  *
171  * In situations where a single ratio is represented by multiple speed
172  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
173  * double-speed instead of quad-speed.  However, the CS4270 errata states
174  * that divide-By-1.5 can cause failures, so we avoid that mode where
175  * possible.
176  *
177  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
178  * work if Vd is 3.3V.  If this effects you, select the
179  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
180  * never select any sample rates that require divide-by-1.5.
181  */
182 struct cs4270_mode_ratios {
183 	unsigned int ratio;
184 	u8 speed_mode;
185 	u8 mclk;
186 };
187 
188 static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
189 	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
190 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
191 	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
192 #endif
193 	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
194 	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
195 	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
196 	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
197 	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
198 	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
199 	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
200 };
201 
202 /* The number of MCLK/LRCK ratios supported by the CS4270 */
203 #define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
204 
205 static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg)
206 {
207 	return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
208 }
209 
210 static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg)
211 {
212 	/* Unreadable registers are considered volatile */
213 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
214 		return true;
215 
216 	return reg == CS4270_CHIPID;
217 }
218 
219 /**
220  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
221  * @codec_dai: the codec DAI
222  * @clk_id: the clock ID (ignored)
223  * @freq: the MCLK input frequency
224  * @dir: the clock direction (ignored)
225  *
226  * This function is used to tell the codec driver what the input MCLK
227  * frequency is.
228  *
229  * The value of MCLK is used to determine which sample rates are supported
230  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
231  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
232  *
233  * This function calculates the nine ratios and determines which ones match
234  * a standard sample rate.  If there's a match, then it is added to the list
235  * of supported sample rates.
236  *
237  * This function must be called by the machine driver's 'startup' function,
238  * otherwise the list of supported sample rates will not be available in
239  * time for ALSA.
240  *
241  * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
242  * theoretically possible sample rates to be enabled. Call it again with a
243  * proper value set one the external clock is set (most probably you would do
244  * that from a machine's driver 'hw_param' hook.
245  */
246 static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
247 				 int clk_id, unsigned int freq, int dir)
248 {
249 	struct snd_soc_component *component = codec_dai->component;
250 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
251 
252 	cs4270->mclk = freq;
253 	return 0;
254 }
255 
256 /**
257  * cs4270_set_dai_fmt - configure the codec for the selected audio format
258  * @codec_dai: the codec DAI
259  * @format: a SND_SOC_DAIFMT_x value indicating the data format
260  *
261  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
262  * codec accordingly.
263  *
264  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
265  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
266  * data for playback only, but ASoC currently does not support different
267  * formats for playback vs. record.
268  */
269 static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
270 			      unsigned int format)
271 {
272 	struct snd_soc_component *component = codec_dai->component;
273 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
274 
275 	/* set DAI format */
276 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
277 	case SND_SOC_DAIFMT_I2S:
278 	case SND_SOC_DAIFMT_LEFT_J:
279 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
280 		break;
281 	default:
282 		dev_err(component->dev, "invalid dai format\n");
283 		return -EINVAL;
284 	}
285 
286 	/* set master/slave audio interface */
287 	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
288 	case SND_SOC_DAIFMT_CBC_CFC:
289 		cs4270->slave_mode = 1;
290 		break;
291 	case SND_SOC_DAIFMT_CBP_CFP:
292 		cs4270->slave_mode = 0;
293 		break;
294 	default:
295 		/* all other modes are unsupported by the hardware */
296 		dev_err(component->dev, "Unknown master/slave configuration\n");
297 		return -EINVAL;
298 	}
299 
300 	return 0;
301 }
302 
303 /**
304  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
305  * @substream: the audio stream
306  * @params: the hardware parameters to set
307  * @dai: the SOC DAI (ignored)
308  *
309  * This function programs the hardware with the values provided.
310  * Specifically, the sample rate and the data format.
311  *
312  * The .ops functions are used to provide board-specific data, like input
313  * frequencies, to this driver.  This function takes that information,
314  * combines it with the hardware parameters provided, and programs the
315  * hardware accordingly.
316  */
317 static int cs4270_hw_params(struct snd_pcm_substream *substream,
318 			    struct snd_pcm_hw_params *params,
319 			    struct snd_soc_dai *dai)
320 {
321 	struct snd_soc_component *component = dai->component;
322 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
323 	int ret;
324 	unsigned int i;
325 	unsigned int rate;
326 	unsigned int ratio;
327 	int reg;
328 
329 	/* Figure out which MCLK/LRCK ratio to use */
330 
331 	rate = params_rate(params);	/* Sampling rate, in Hz */
332 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
333 
334 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
335 		if (cs4270_mode_ratios[i].ratio == ratio)
336 			break;
337 	}
338 
339 	if (i == NUM_MCLK_RATIOS) {
340 		/* We did not find a matching ratio */
341 		dev_err(component->dev, "could not find matching ratio\n");
342 		return -EINVAL;
343 	}
344 
345 	/* Set the sample rate */
346 
347 	reg = snd_soc_component_read(component, CS4270_MODE);
348 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
349 	reg |= cs4270_mode_ratios[i].mclk;
350 
351 	if (cs4270->slave_mode)
352 		reg |= CS4270_MODE_SLAVE;
353 	else
354 		reg |= cs4270_mode_ratios[i].speed_mode;
355 
356 	ret = snd_soc_component_write(component, CS4270_MODE, reg);
357 	if (ret < 0) {
358 		dev_err(component->dev, "i2c write failed\n");
359 		return ret;
360 	}
361 
362 	/* Set the DAI format */
363 
364 	reg = snd_soc_component_read(component, CS4270_FORMAT);
365 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
366 
367 	switch (cs4270->mode) {
368 	case SND_SOC_DAIFMT_I2S:
369 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
370 		break;
371 	case SND_SOC_DAIFMT_LEFT_J:
372 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
373 		break;
374 	default:
375 		dev_err(component->dev, "unknown dai format\n");
376 		return -EINVAL;
377 	}
378 
379 	ret = snd_soc_component_write(component, CS4270_FORMAT, reg);
380 	if (ret < 0) {
381 		dev_err(component->dev, "i2c write failed\n");
382 		return ret;
383 	}
384 
385 	return ret;
386 }
387 
388 /**
389  * cs4270_dai_mute - enable/disable the CS4270 external mute
390  * @dai: the SOC DAI
391  * @mute: 0 = disable mute, 1 = enable mute
392  * @direction: (ignored)
393  *
394  * This function toggles the mute bits in the MUTE register.  The CS4270's
395  * mute capability is intended for external muting circuitry, so if the
396  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
397  * then this function will do nothing.
398  */
399 static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
400 {
401 	struct snd_soc_component *component = dai->component;
402 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
403 	int reg6;
404 
405 	reg6 = snd_soc_component_read(component, CS4270_MUTE);
406 
407 	if (mute)
408 		reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
409 	else {
410 		reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
411 		reg6 |= cs4270->manual_mute;
412 	}
413 
414 	return snd_soc_component_write(component, CS4270_MUTE, reg6);
415 }
416 
417 /**
418  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
419  * 			 alsa control.
420  * @kcontrol: mixer control
421  * @ucontrol: control element information
422  *
423  * This function basically passes the arguments on to the generic
424  * snd_soc_put_volsw() function and saves the mute information in
425  * our private data structure. This is because we want to prevent
426  * cs4270_dai_mute() neglecting the user's decision to manually
427  * mute the codec's output.
428  *
429  * Returns 0 for success.
430  */
431 static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
432 				struct snd_ctl_elem_value *ucontrol)
433 {
434 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
435 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
436 	int left = !ucontrol->value.integer.value[0];
437 	int right = !ucontrol->value.integer.value[1];
438 
439 	cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
440 			      (right ? CS4270_MUTE_DAC_B : 0);
441 
442 	return snd_soc_put_volsw(kcontrol, ucontrol);
443 }
444 
445 /* A list of non-DAPM controls that the CS4270 supports */
446 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
447 	SOC_DOUBLE_R("Master Playback Volume",
448 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
449 	SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
450 	SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
451 	SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
452 	SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
453 	SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
454 	SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
455 	SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
456 	SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
457 		snd_soc_get_volsw, cs4270_soc_put_mute),
458 };
459 
460 static const struct snd_soc_dai_ops cs4270_dai_ops = {
461 	.hw_params	= cs4270_hw_params,
462 	.set_sysclk	= cs4270_set_dai_sysclk,
463 	.set_fmt	= cs4270_set_dai_fmt,
464 	.mute_stream	= cs4270_dai_mute,
465 	.no_capture_mute = 1,
466 };
467 
468 static struct snd_soc_dai_driver cs4270_dai = {
469 	.name = "cs4270-hifi",
470 	.playback = {
471 		.stream_name = "Playback",
472 		.channels_min = 2,
473 		.channels_max = 2,
474 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
475 		.rate_min = 4000,
476 		.rate_max = 216000,
477 		.formats = CS4270_FORMATS,
478 	},
479 	.capture = {
480 		.stream_name = "Capture",
481 		.channels_min = 2,
482 		.channels_max = 2,
483 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
484 		.rate_min = 4000,
485 		.rate_max = 216000,
486 		.formats = CS4270_FORMATS,
487 	},
488 	.ops = &cs4270_dai_ops,
489 };
490 
491 /**
492  * cs4270_probe - ASoC probe function
493  * @component: ASoC component
494  *
495  * This function is called when ASoC has all the pieces it needs to
496  * instantiate a sound driver.
497  */
498 static int cs4270_probe(struct snd_soc_component *component)
499 {
500 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
501 	int ret;
502 
503 	/* Disable auto-mute.  This feature appears to be buggy.  In some
504 	 * situations, auto-mute will not deactivate when it should, so we want
505 	 * this feature disabled by default.  An application (e.g. alsactl) can
506 	 * re-enabled it by using the controls.
507 	 */
508 	ret = snd_soc_component_update_bits(component, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
509 	if (ret < 0) {
510 		dev_err(component->dev, "i2c write failed\n");
511 		return ret;
512 	}
513 
514 	/* Disable automatic volume control.  The hardware enables, and it
515 	 * causes volume change commands to be delayed, sometimes until after
516 	 * playback has started.  An application (e.g. alsactl) can
517 	 * re-enabled it by using the controls.
518 	 */
519 	ret = snd_soc_component_update_bits(component, CS4270_TRANS,
520 		CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
521 	if (ret < 0) {
522 		dev_err(component->dev, "i2c write failed\n");
523 		return ret;
524 	}
525 
526 	ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
527 				    cs4270->supplies);
528 
529 	return ret;
530 }
531 
532 /**
533  * cs4270_remove - ASoC remove function
534  * @component: ASoC component
535  *
536  * This function is the counterpart to cs4270_probe().
537  */
538 static void cs4270_remove(struct snd_soc_component *component)
539 {
540 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
541 
542 	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
543 };
544 
545 #ifdef CONFIG_PM
546 
547 /* This suspend/resume implementation can handle both - a simple standby
548  * where the codec remains powered, and a full suspend, where the voltage
549  * domain the codec is connected to is teared down and/or any other hardware
550  * reset condition is asserted.
551  *
552  * The codec's own power saving features are enabled in the suspend callback,
553  * and all registers are written back to the hardware when resuming.
554  */
555 
556 static int cs4270_soc_suspend(struct snd_soc_component *component)
557 {
558 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
559 	int reg, ret;
560 
561 	reg = snd_soc_component_read(component, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
562 	if (reg < 0)
563 		return reg;
564 
565 	ret = snd_soc_component_write(component, CS4270_PWRCTL, reg);
566 	if (ret < 0)
567 		return ret;
568 
569 	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
570 			       cs4270->supplies);
571 
572 	return 0;
573 }
574 
575 static int cs4270_soc_resume(struct snd_soc_component *component)
576 {
577 	struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
578 	int reg, ret;
579 
580 	ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
581 				    cs4270->supplies);
582 	if (ret != 0)
583 		return ret;
584 
585 	/* In case the device was put to hard reset during sleep, we need to
586 	 * wait 500ns here before any I2C communication. */
587 	ndelay(500);
588 
589 	/* first restore the entire register cache ... */
590 	regcache_sync(cs4270->regmap);
591 
592 	/* ... then disable the power-down bits */
593 	reg = snd_soc_component_read(component, CS4270_PWRCTL);
594 	reg &= ~CS4270_PWRCTL_PDN_ALL;
595 
596 	return snd_soc_component_write(component, CS4270_PWRCTL, reg);
597 }
598 #else
599 #define cs4270_soc_suspend	NULL
600 #define cs4270_soc_resume	NULL
601 #endif /* CONFIG_PM */
602 
603 /*
604  * ASoC codec driver structure
605  */
606 static const struct snd_soc_component_driver soc_component_device_cs4270 = {
607 	.probe			= cs4270_probe,
608 	.remove			= cs4270_remove,
609 	.suspend		= cs4270_soc_suspend,
610 	.resume			= cs4270_soc_resume,
611 	.controls		= cs4270_snd_controls,
612 	.num_controls		= ARRAY_SIZE(cs4270_snd_controls),
613 	.dapm_widgets		= cs4270_dapm_widgets,
614 	.num_dapm_widgets	= ARRAY_SIZE(cs4270_dapm_widgets),
615 	.dapm_routes		= cs4270_dapm_routes,
616 	.num_dapm_routes	= ARRAY_SIZE(cs4270_dapm_routes),
617 	.idle_bias_on		= 1,
618 	.use_pmdown_time	= 1,
619 	.endianness		= 1,
620 };
621 
622 /*
623  * cs4270_of_match - the device tree bindings
624  */
625 static const struct of_device_id cs4270_of_match[] = {
626 	{ .compatible = "cirrus,cs4270", },
627 	{ }
628 };
629 MODULE_DEVICE_TABLE(of, cs4270_of_match);
630 
631 static const struct regmap_config cs4270_regmap = {
632 	.reg_bits =		8,
633 	.val_bits =		8,
634 	.max_register =		CS4270_LASTREG,
635 	.reg_defaults =		cs4270_reg_defaults,
636 	.num_reg_defaults =	ARRAY_SIZE(cs4270_reg_defaults),
637 	.cache_type =		REGCACHE_MAPLE,
638 	.write_flag_mask =	CS4270_I2C_INCR,
639 
640 	.readable_reg =		cs4270_reg_is_readable,
641 	.volatile_reg =		cs4270_reg_is_volatile,
642 };
643 
644 /**
645  * cs4270_i2c_remove - deinitialize the I2C interface of the CS4270
646  * @i2c_client: the I2C client object
647  *
648  * This function puts the chip into low power mode when the i2c device
649  * is removed.
650  */
651 static void cs4270_i2c_remove(struct i2c_client *i2c_client)
652 {
653 	struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
654 
655 	gpiod_set_value_cansleep(cs4270->reset_gpio, 0);
656 }
657 
658 /**
659  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
660  * @i2c_client: the I2C client object
661  *
662  * This function is called whenever the I2C subsystem finds a device that
663  * matches the device ID given via a prior call to i2c_add_driver().
664  */
665 static int cs4270_i2c_probe(struct i2c_client *i2c_client)
666 {
667 	struct cs4270_private *cs4270;
668 	unsigned int val;
669 	int ret, i;
670 
671 	cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
672 			      GFP_KERNEL);
673 	if (!cs4270)
674 		return -ENOMEM;
675 
676 	/* get the power supply regulators */
677 	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
678 		cs4270->supplies[i].supply = supply_names[i];
679 
680 	ret = devm_regulator_bulk_get(&i2c_client->dev,
681 				      ARRAY_SIZE(cs4270->supplies),
682 				      cs4270->supplies);
683 	if (ret < 0)
684 		return ret;
685 
686 	/* reset the device */
687 	cs4270->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev, "reset",
688 						     GPIOD_OUT_LOW);
689 	if (IS_ERR(cs4270->reset_gpio)) {
690 		dev_dbg(&i2c_client->dev, "Error getting CS4270 reset GPIO\n");
691 		return PTR_ERR(cs4270->reset_gpio);
692 	}
693 
694 	if (cs4270->reset_gpio) {
695 		dev_dbg(&i2c_client->dev, "Found reset GPIO\n");
696 		gpiod_set_value_cansleep(cs4270->reset_gpio, 1);
697 	}
698 
699 	/* Sleep 500ns before i2c communications */
700 	ndelay(500);
701 
702 	cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap);
703 	if (IS_ERR(cs4270->regmap))
704 		return PTR_ERR(cs4270->regmap);
705 
706 	/* Verify that we have a CS4270 */
707 	ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val);
708 	if (ret < 0) {
709 		dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
710 		       i2c_client->addr);
711 		return ret;
712 	}
713 	/* The top four bits of the chip ID should be 1100. */
714 	if ((val & 0xF0) != 0xC0) {
715 		dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
716 		       i2c_client->addr);
717 		return -ENODEV;
718 	}
719 
720 	dev_info(&i2c_client->dev, "found device at i2c address %X\n",
721 		i2c_client->addr);
722 	dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF);
723 
724 	i2c_set_clientdata(i2c_client, cs4270);
725 
726 	ret = devm_snd_soc_register_component(&i2c_client->dev,
727 			&soc_component_device_cs4270, &cs4270_dai, 1);
728 	return ret;
729 }
730 
731 /*
732  * cs4270_id - I2C device IDs supported by this driver
733  */
734 static const struct i2c_device_id cs4270_id[] = {
735 	{"cs4270"},
736 	{}
737 };
738 MODULE_DEVICE_TABLE(i2c, cs4270_id);
739 
740 /*
741  * cs4270_i2c_driver - I2C device identification
742  *
743  * This structure tells the I2C subsystem how to identify and support a
744  * given I2C device type.
745  */
746 static struct i2c_driver cs4270_i2c_driver = {
747 	.driver = {
748 		.name = "cs4270",
749 		.of_match_table = cs4270_of_match,
750 	},
751 	.id_table = cs4270_id,
752 	.probe = cs4270_i2c_probe,
753 	.remove = cs4270_i2c_remove,
754 };
755 
756 module_i2c_driver(cs4270_i2c_driver);
757 
758 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
759 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
760 MODULE_LICENSE("GPL");
761