2 * CS4270 ALSA SoC (ASoC) codec driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
13 * Current features/limitations:
15 * - Software mode is supported. Stand-alone mode is not supported.
16 * - Only I2C is supported, not SPI
17 * - Support for master and slave mode
18 * - The machine driver's 'startup' function must call
19 * cs4270_set_dai_sysclk() with the value of MCLK.
20 * - Only I2S and left-justified modes are supported
21 * - Power management is supported
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 #include <linux/i2c.h>
31 #include <linux/delay.h>
32 #include <linux/regulator/consumer.h>
35 * The codec isn't really big-endian or little-endian, since the I2S
36 * interface requires data to be sent serially with the MSbit first.
37 * However, to support BE and LE I2S devices, we specify both here. That
38 * way, ALSA will always match the bit patterns.
40 #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
41 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
42 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
47 /* CS4270 registers addresses */
48 #define CS4270_CHIPID 0x01 /* Chip ID */
49 #define CS4270_PWRCTL 0x02 /* Power Control */
50 #define CS4270_MODE 0x03 /* Mode Control */
51 #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
52 #define CS4270_TRANS 0x05 /* Transition Control */
53 #define CS4270_MUTE 0x06 /* Mute Control */
54 #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
55 #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
57 #define CS4270_FIRSTREG 0x01
58 #define CS4270_LASTREG 0x08
59 #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
60 #define CS4270_I2C_INCR 0x80
62 /* Bit masks for the CS4270 registers */
63 #define CS4270_CHIPID_ID 0xF0
64 #define CS4270_CHIPID_REV 0x0F
65 #define CS4270_PWRCTL_FREEZE 0x80
66 #define CS4270_PWRCTL_PDN_ADC 0x20
67 #define CS4270_PWRCTL_PDN_DAC 0x02
68 #define CS4270_PWRCTL_PDN 0x01
69 #define CS4270_PWRCTL_PDN_ALL \
70 (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
71 #define CS4270_MODE_SPEED_MASK 0x30
72 #define CS4270_MODE_1X 0x00
73 #define CS4270_MODE_2X 0x10
74 #define CS4270_MODE_4X 0x20
75 #define CS4270_MODE_SLAVE 0x30
76 #define CS4270_MODE_DIV_MASK 0x0E
77 #define CS4270_MODE_DIV1 0x00
78 #define CS4270_MODE_DIV15 0x02
79 #define CS4270_MODE_DIV2 0x04
80 #define CS4270_MODE_DIV3 0x06
81 #define CS4270_MODE_DIV4 0x08
82 #define CS4270_MODE_POPGUARD 0x01
83 #define CS4270_FORMAT_FREEZE_A 0x80
84 #define CS4270_FORMAT_FREEZE_B 0x40
85 #define CS4270_FORMAT_LOOPBACK 0x20
86 #define CS4270_FORMAT_DAC_MASK 0x18
87 #define CS4270_FORMAT_DAC_LJ 0x00
88 #define CS4270_FORMAT_DAC_I2S 0x08
89 #define CS4270_FORMAT_DAC_RJ16 0x18
90 #define CS4270_FORMAT_DAC_RJ24 0x10
91 #define CS4270_FORMAT_ADC_MASK 0x01
92 #define CS4270_FORMAT_ADC_LJ 0x00
93 #define CS4270_FORMAT_ADC_I2S 0x01
94 #define CS4270_TRANS_ONE_VOL 0x80
95 #define CS4270_TRANS_SOFT 0x40
96 #define CS4270_TRANS_ZERO 0x20
97 #define CS4270_TRANS_INV_ADC_A 0x08
98 #define CS4270_TRANS_INV_ADC_B 0x10
99 #define CS4270_TRANS_INV_DAC_A 0x02
100 #define CS4270_TRANS_INV_DAC_B 0x04
101 #define CS4270_TRANS_DEEMPH 0x01
102 #define CS4270_MUTE_AUTO 0x20
103 #define CS4270_MUTE_ADC_A 0x08
104 #define CS4270_MUTE_ADC_B 0x10
105 #define CS4270_MUTE_POLARITY 0x04
106 #define CS4270_MUTE_DAC_A 0x01
107 #define CS4270_MUTE_DAC_B 0x02
109 static const char *supply_names
[] = {
113 /* Private data for the CS4270 */
114 struct cs4270_private
{
115 enum snd_soc_control_type control_type
;
117 u8 reg_cache
[CS4270_NUMREGS
];
118 unsigned int mclk
; /* Input frequency of the MCLK pin */
119 unsigned int mode
; /* The mode (I2S or left-justified) */
120 unsigned int slave_mode
;
121 unsigned int manual_mute
;
123 /* power domain regulators */
124 struct regulator_bulk_data supplies
[ARRAY_SIZE(supply_names
)];
128 * struct cs4270_mode_ratios - clock ratio tables
129 * @ratio: the ratio of MCLK to the sample rate
130 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
132 * @mclk: the Ratio Select bits to set in the Mode Control register for this
135 * The data for this chart is taken from Table 5 of the CS4270 reference
138 * This table is used to determine how to program the Mode Control register.
139 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
140 * rates the CS4270 currently supports.
142 * @speed_mode is the corresponding bit pattern to be written to the
143 * MODE bits of the Mode Control Register
145 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
146 * the Mode Control Register.
148 * In situations where a single ratio is represented by multiple speed
149 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
150 * double-speed instead of quad-speed. However, the CS4270 errata states
151 * that divide-By-1.5 can cause failures, so we avoid that mode where
154 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
155 * work if Vd is 3.3V. If this effects you, select the
156 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
157 * never select any sample rates that require divide-by-1.5.
159 struct cs4270_mode_ratios
{
165 static struct cs4270_mode_ratios cs4270_mode_ratios
[] = {
166 {64, CS4270_MODE_4X
, CS4270_MODE_DIV1
},
167 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
168 {96, CS4270_MODE_4X
, CS4270_MODE_DIV15
},
170 {128, CS4270_MODE_2X
, CS4270_MODE_DIV1
},
171 {192, CS4270_MODE_4X
, CS4270_MODE_DIV3
},
172 {256, CS4270_MODE_1X
, CS4270_MODE_DIV1
},
173 {384, CS4270_MODE_2X
, CS4270_MODE_DIV3
},
174 {512, CS4270_MODE_1X
, CS4270_MODE_DIV2
},
175 {768, CS4270_MODE_1X
, CS4270_MODE_DIV3
},
176 {1024, CS4270_MODE_1X
, CS4270_MODE_DIV4
}
179 /* The number of MCLK/LRCK ratios supported by the CS4270 */
180 #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
183 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
184 * @codec_dai: the codec DAI
185 * @clk_id: the clock ID (ignored)
186 * @freq: the MCLK input frequency
187 * @dir: the clock direction (ignored)
189 * This function is used to tell the codec driver what the input MCLK
192 * The value of MCLK is used to determine which sample rates are supported
193 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
194 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
196 * This function calculates the nine ratios and determines which ones match
197 * a standard sample rate. If there's a match, then it is added to the list
198 * of supported sample rates.
200 * This function must be called by the machine driver's 'startup' function,
201 * otherwise the list of supported sample rates will not be available in
204 * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
205 * theoretically possible sample rates to be enabled. Call it again with a
206 * proper value set one the external clock is set (most probably you would do
207 * that from a machine's driver 'hw_param' hook.
209 static int cs4270_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
210 int clk_id
, unsigned int freq
, int dir
)
212 struct snd_soc_codec
*codec
= codec_dai
->codec
;
213 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
220 * cs4270_set_dai_fmt - configure the codec for the selected audio format
221 * @codec_dai: the codec DAI
222 * @format: a SND_SOC_DAIFMT_x value indicating the data format
224 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
227 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
228 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
229 * data for playback only, but ASoC currently does not support different
230 * formats for playback vs. record.
232 static int cs4270_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
235 struct snd_soc_codec
*codec
= codec_dai
->codec
;
236 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
240 switch (format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
241 case SND_SOC_DAIFMT_I2S
:
242 case SND_SOC_DAIFMT_LEFT_J
:
243 cs4270
->mode
= format
& SND_SOC_DAIFMT_FORMAT_MASK
;
246 dev_err(codec
->dev
, "invalid dai format\n");
250 /* set master/slave audio interface */
251 switch (format
& SND_SOC_DAIFMT_MASTER_MASK
) {
252 case SND_SOC_DAIFMT_CBS_CFS
:
253 cs4270
->slave_mode
= 1;
255 case SND_SOC_DAIFMT_CBM_CFM
:
256 cs4270
->slave_mode
= 0;
259 /* all other modes are unsupported by the hardware */
267 * cs4270_fill_cache - pre-fill the CS4270 register cache.
268 * @codec: the codec for this CS4270
270 * This function fills in the CS4270 register cache by reading the register
271 * values from the hardware.
273 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
274 * After the initial read to pre-fill the cache, the CS4270 never updates
275 * the register values, so we won't have a cache coherency problem.
277 * We use the auto-increment feature of the CS4270 to read all registers in
280 static int cs4270_fill_cache(struct snd_soc_codec
*codec
)
282 u8
*cache
= codec
->reg_cache
;
283 struct i2c_client
*i2c_client
= codec
->control_data
;
286 length
= i2c_smbus_read_i2c_block_data(i2c_client
,
287 CS4270_FIRSTREG
| CS4270_I2C_INCR
, CS4270_NUMREGS
, cache
);
289 if (length
!= CS4270_NUMREGS
) {
290 dev_err(codec
->dev
, "i2c read failure, addr=0x%x\n",
299 * cs4270_read_reg_cache - read from the CS4270 register cache.
300 * @codec: the codec for this CS4270
301 * @reg: the register to read
303 * This function returns the value for a given register. It reads only from
304 * the register cache, not the hardware itself.
306 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
307 * After the initial read to pre-fill the cache, the CS4270 never updates
308 * the register values, so we won't have a cache coherency problem.
310 static unsigned int cs4270_read_reg_cache(struct snd_soc_codec
*codec
,
313 u8
*cache
= codec
->reg_cache
;
315 if ((reg
< CS4270_FIRSTREG
) || (reg
> CS4270_LASTREG
))
318 return cache
[reg
- CS4270_FIRSTREG
];
322 * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
323 * @codec: the codec for this CS4270
324 * @reg: the register to write
325 * @value: the value to write to the register
327 * This function writes the given value to the given CS4270 register, and
328 * also updates the register cache.
330 * Note that we don't use the hw_write function pointer of snd_soc_codec.
331 * That's because it's too clunky: the hw_write_t prototype does not match
332 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
334 static int cs4270_i2c_write(struct snd_soc_codec
*codec
, unsigned int reg
,
337 u8
*cache
= codec
->reg_cache
;
339 if ((reg
< CS4270_FIRSTREG
) || (reg
> CS4270_LASTREG
))
342 /* Only perform an I2C operation if the new value is different */
343 if (cache
[reg
- CS4270_FIRSTREG
] != value
) {
344 struct i2c_client
*client
= codec
->control_data
;
345 if (i2c_smbus_write_byte_data(client
, reg
, value
)) {
346 dev_err(codec
->dev
, "i2c write failed\n");
350 /* We've written to the hardware, so update the cache */
351 cache
[reg
- CS4270_FIRSTREG
] = value
;
358 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
359 * @substream: the audio stream
360 * @params: the hardware parameters to set
361 * @dai: the SOC DAI (ignored)
363 * This function programs the hardware with the values provided.
364 * Specifically, the sample rate and the data format.
366 * The .ops functions are used to provide board-specific data, like input
367 * frequencies, to this driver. This function takes that information,
368 * combines it with the hardware parameters provided, and programs the
369 * hardware accordingly.
371 static int cs4270_hw_params(struct snd_pcm_substream
*substream
,
372 struct snd_pcm_hw_params
*params
,
373 struct snd_soc_dai
*dai
)
375 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
376 struct snd_soc_codec
*codec
= rtd
->codec
;
377 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
384 /* Figure out which MCLK/LRCK ratio to use */
386 rate
= params_rate(params
); /* Sampling rate, in Hz */
387 ratio
= cs4270
->mclk
/ rate
; /* MCLK/LRCK ratio */
389 for (i
= 0; i
< NUM_MCLK_RATIOS
; i
++) {
390 if (cs4270_mode_ratios
[i
].ratio
== ratio
)
394 if (i
== NUM_MCLK_RATIOS
) {
395 /* We did not find a matching ratio */
396 dev_err(codec
->dev
, "could not find matching ratio\n");
400 /* Set the sample rate */
402 reg
= snd_soc_read(codec
, CS4270_MODE
);
403 reg
&= ~(CS4270_MODE_SPEED_MASK
| CS4270_MODE_DIV_MASK
);
404 reg
|= cs4270_mode_ratios
[i
].mclk
;
406 if (cs4270
->slave_mode
)
407 reg
|= CS4270_MODE_SLAVE
;
409 reg
|= cs4270_mode_ratios
[i
].speed_mode
;
411 ret
= snd_soc_write(codec
, CS4270_MODE
, reg
);
413 dev_err(codec
->dev
, "i2c write failed\n");
417 /* Set the DAI format */
419 reg
= snd_soc_read(codec
, CS4270_FORMAT
);
420 reg
&= ~(CS4270_FORMAT_DAC_MASK
| CS4270_FORMAT_ADC_MASK
);
422 switch (cs4270
->mode
) {
423 case SND_SOC_DAIFMT_I2S
:
424 reg
|= CS4270_FORMAT_DAC_I2S
| CS4270_FORMAT_ADC_I2S
;
426 case SND_SOC_DAIFMT_LEFT_J
:
427 reg
|= CS4270_FORMAT_DAC_LJ
| CS4270_FORMAT_ADC_LJ
;
430 dev_err(codec
->dev
, "unknown dai format\n");
434 ret
= snd_soc_write(codec
, CS4270_FORMAT
, reg
);
436 dev_err(codec
->dev
, "i2c write failed\n");
444 * cs4270_dai_mute - enable/disable the CS4270 external mute
446 * @mute: 0 = disable mute, 1 = enable mute
448 * This function toggles the mute bits in the MUTE register. The CS4270's
449 * mute capability is intended for external muting circuitry, so if the
450 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
451 * then this function will do nothing.
453 static int cs4270_dai_mute(struct snd_soc_dai
*dai
, int mute
)
455 struct snd_soc_codec
*codec
= dai
->codec
;
456 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
459 reg6
= snd_soc_read(codec
, CS4270_MUTE
);
462 reg6
|= CS4270_MUTE_DAC_A
| CS4270_MUTE_DAC_B
;
464 reg6
&= ~(CS4270_MUTE_DAC_A
| CS4270_MUTE_DAC_B
);
465 reg6
|= cs4270
->manual_mute
;
468 return snd_soc_write(codec
, CS4270_MUTE
, reg6
);
472 * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
474 * @kcontrol: mixer control
475 * @ucontrol: control element information
477 * This function basically passes the arguments on to the generic
478 * snd_soc_put_volsw() function and saves the mute information in
479 * our private data structure. This is because we want to prevent
480 * cs4270_dai_mute() neglecting the user's decision to manually
481 * mute the codec's output.
483 * Returns 0 for success.
485 static int cs4270_soc_put_mute(struct snd_kcontrol
*kcontrol
,
486 struct snd_ctl_elem_value
*ucontrol
)
488 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
489 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
490 int left
= !ucontrol
->value
.integer
.value
[0];
491 int right
= !ucontrol
->value
.integer
.value
[1];
493 cs4270
->manual_mute
= (left
? CS4270_MUTE_DAC_A
: 0) |
494 (right
? CS4270_MUTE_DAC_B
: 0);
496 return snd_soc_put_volsw(kcontrol
, ucontrol
);
499 /* A list of non-DAPM controls that the CS4270 supports */
500 static const struct snd_kcontrol_new cs4270_snd_controls
[] = {
501 SOC_DOUBLE_R("Master Playback Volume",
502 CS4270_VOLA
, CS4270_VOLB
, 0, 0xFF, 1),
503 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT
, 5, 1, 0),
504 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS
, 6, 1, 0),
505 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS
, 5, 1, 0),
506 SOC_SINGLE("De-emphasis filter", CS4270_TRANS
, 0, 1, 0),
507 SOC_SINGLE("Popguard Switch", CS4270_MODE
, 0, 1, 1),
508 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE
, 5, 1, 0),
509 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE
, 3, 4, 1, 1),
510 SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE
, 0, 1, 1, 1,
511 snd_soc_get_volsw
, cs4270_soc_put_mute
),
514 static struct snd_soc_dai_ops cs4270_dai_ops
= {
515 .hw_params
= cs4270_hw_params
,
516 .set_sysclk
= cs4270_set_dai_sysclk
,
517 .set_fmt
= cs4270_set_dai_fmt
,
518 .digital_mute
= cs4270_dai_mute
,
521 struct snd_soc_dai_driver cs4270_dai
= {
522 .name
= "cs4270-hifi",
524 .stream_name
= "Playback",
527 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
530 .formats
= CS4270_FORMATS
,
533 .stream_name
= "Capture",
536 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
539 .formats
= CS4270_FORMATS
,
541 .ops
= &cs4270_dai_ops
,
543 EXPORT_SYMBOL_GPL(cs4270_dai
);
546 * cs4270_probe - ASoC probe function
547 * @pdev: platform device
549 * This function is called when ASoC has all the pieces it needs to
550 * instantiate a sound driver.
552 static int cs4270_probe(struct snd_soc_codec
*codec
)
554 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
557 codec
->control_data
= cs4270
->control_data
;
559 /* The I2C interface is set up, so pre-fill our register cache */
561 ret
= cs4270_fill_cache(codec
);
563 dev_err(codec
->dev
, "failed to fill register cache\n");
567 /* Disable auto-mute. This feature appears to be buggy. In some
568 * situations, auto-mute will not deactivate when it should, so we want
569 * this feature disabled by default. An application (e.g. alsactl) can
570 * re-enabled it by using the controls.
573 reg
= cs4270_read_reg_cache(codec
, CS4270_MUTE
);
574 reg
&= ~CS4270_MUTE_AUTO
;
575 ret
= cs4270_i2c_write(codec
, CS4270_MUTE
, reg
);
577 dev_err(codec
->dev
, "i2c write failed\n");
581 /* Disable automatic volume control. The hardware enables, and it
582 * causes volume change commands to be delayed, sometimes until after
583 * playback has started. An application (e.g. alsactl) can
584 * re-enabled it by using the controls.
587 reg
= cs4270_read_reg_cache(codec
, CS4270_TRANS
);
588 reg
&= ~(CS4270_TRANS_SOFT
| CS4270_TRANS_ZERO
);
589 ret
= cs4270_i2c_write(codec
, CS4270_TRANS
, reg
);
591 dev_err(codec
->dev
, "i2c write failed\n");
595 /* Add the non-DAPM controls */
596 ret
= snd_soc_add_controls(codec
, cs4270_snd_controls
,
597 ARRAY_SIZE(cs4270_snd_controls
));
599 dev_err(codec
->dev
, "failed to add controls\n");
603 /* get the power supply regulators */
604 for (i
= 0; i
< ARRAY_SIZE(supply_names
); i
++)
605 cs4270
->supplies
[i
].supply
= supply_names
[i
];
607 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(cs4270
->supplies
),
612 ret
= regulator_bulk_enable(ARRAY_SIZE(cs4270
->supplies
),
615 goto error_free_regulators
;
619 error_free_regulators
:
620 regulator_bulk_free(ARRAY_SIZE(cs4270
->supplies
),
627 * cs4270_remove - ASoC remove function
628 * @pdev: platform device
630 * This function is the counterpart to cs4270_probe().
632 static int cs4270_remove(struct snd_soc_codec
*codec
)
634 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
636 regulator_bulk_disable(ARRAY_SIZE(cs4270
->supplies
), cs4270
->supplies
);
637 regulator_bulk_free(ARRAY_SIZE(cs4270
->supplies
), cs4270
->supplies
);
644 /* This suspend/resume implementation can handle both - a simple standby
645 * where the codec remains powered, and a full suspend, where the voltage
646 * domain the codec is connected to is teared down and/or any other hardware
647 * reset condition is asserted.
649 * The codec's own power saving features are enabled in the suspend callback,
650 * and all registers are written back to the hardware when resuming.
653 static int cs4270_soc_suspend(struct snd_soc_codec
*codec
, pm_message_t mesg
)
655 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
658 reg
= snd_soc_read(codec
, CS4270_PWRCTL
) | CS4270_PWRCTL_PDN_ALL
;
662 ret
= snd_soc_write(codec
, CS4270_PWRCTL
, reg
);
666 regulator_bulk_disable(ARRAY_SIZE(cs4270
->supplies
),
672 static int cs4270_soc_resume(struct snd_soc_codec
*codec
)
674 struct cs4270_private
*cs4270
= snd_soc_codec_get_drvdata(codec
);
675 struct i2c_client
*i2c_client
= codec
->control_data
;
678 regulator_bulk_enable(ARRAY_SIZE(cs4270
->supplies
),
681 /* In case the device was put to hard reset during sleep, we need to
682 * wait 500ns here before any I2C communication. */
685 /* first restore the entire register cache ... */
686 for (reg
= CS4270_FIRSTREG
; reg
<= CS4270_LASTREG
; reg
++) {
687 u8 val
= snd_soc_read(codec
, reg
);
689 if (i2c_smbus_write_byte_data(i2c_client
, reg
, val
)) {
690 dev_err(codec
->dev
, "i2c write failed\n");
695 /* ... then disable the power-down bits */
696 reg
= snd_soc_read(codec
, CS4270_PWRCTL
);
697 reg
&= ~CS4270_PWRCTL_PDN_ALL
;
699 return snd_soc_write(codec
, CS4270_PWRCTL
, reg
);
702 #define cs4270_soc_suspend NULL
703 #define cs4270_soc_resume NULL
704 #endif /* CONFIG_PM */
707 * ASoC codec device structure
709 * Assign this variable to the codec_dev field of the machine driver's
710 * snd_soc_device structure.
712 static struct snd_soc_codec_driver soc_codec_device_cs4270
= {
713 .probe
= cs4270_probe
,
714 .remove
= cs4270_remove
,
715 .suspend
= cs4270_soc_suspend
,
716 .resume
= cs4270_soc_resume
,
717 .read
= cs4270_read_reg_cache
,
718 .write
= cs4270_i2c_write
,
719 .reg_cache_size
= CS4270_NUMREGS
,
720 .reg_word_size
= sizeof(u8
),
724 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
725 * @i2c_client: the I2C client object
726 * @id: the I2C device ID (ignored)
728 * This function is called whenever the I2C subsystem finds a device that
729 * matches the device ID given via a prior call to i2c_add_driver().
731 static int cs4270_i2c_probe(struct i2c_client
*i2c_client
,
732 const struct i2c_device_id
*id
)
734 struct cs4270_private
*cs4270
;
737 /* Verify that we have a CS4270 */
739 ret
= i2c_smbus_read_byte_data(i2c_client
, CS4270_CHIPID
);
741 dev_err(&i2c_client
->dev
, "failed to read i2c at addr %X\n",
745 /* The top four bits of the chip ID should be 1100. */
746 if ((ret
& 0xF0) != 0xC0) {
747 dev_err(&i2c_client
->dev
, "device at addr %X is not a CS4270\n",
752 dev_info(&i2c_client
->dev
, "found device at i2c address %X\n",
754 dev_info(&i2c_client
->dev
, "hardware revision %X\n", ret
& 0xF);
756 cs4270
= kzalloc(sizeof(struct cs4270_private
), GFP_KERNEL
);
758 dev_err(&i2c_client
->dev
, "could not allocate codec\n");
762 i2c_set_clientdata(i2c_client
, cs4270
);
763 cs4270
->control_data
= i2c_client
;
764 cs4270
->control_type
= SND_SOC_I2C
;
766 ret
= snd_soc_register_codec(&i2c_client
->dev
,
767 &soc_codec_device_cs4270
, &cs4270_dai
, 1);
774 * cs4270_i2c_remove - remove an I2C device
775 * @i2c_client: the I2C client object
777 * This function is the counterpart to cs4270_i2c_probe().
779 static int cs4270_i2c_remove(struct i2c_client
*i2c_client
)
781 snd_soc_unregister_codec(&i2c_client
->dev
);
782 kfree(i2c_get_clientdata(i2c_client
));
787 * cs4270_id - I2C device IDs supported by this driver
789 static struct i2c_device_id cs4270_id
[] = {
793 MODULE_DEVICE_TABLE(i2c
, cs4270_id
);
796 * cs4270_i2c_driver - I2C device identification
798 * This structure tells the I2C subsystem how to identify and support a
799 * given I2C device type.
801 static struct i2c_driver cs4270_i2c_driver
= {
803 .name
= "cs4270-codec",
804 .owner
= THIS_MODULE
,
806 .id_table
= cs4270_id
,
807 .probe
= cs4270_i2c_probe
,
808 .remove
= cs4270_i2c_remove
,
811 static int __init
cs4270_init(void)
813 pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
815 return i2c_add_driver(&cs4270_i2c_driver
);
817 module_init(cs4270_init
);
819 static void __exit
cs4270_exit(void)
821 i2c_del_driver(&cs4270_i2c_driver
);
823 module_exit(cs4270_exit
);
825 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
826 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
827 MODULE_LICENSE("GPL");