diff options
Diffstat (limited to 'sound/soc/codecs/cs4270.c')
-rw-r--r-- | sound/soc/codecs/cs4270.c | 162 |
1 files changed, 48 insertions, 114 deletions
diff --git a/sound/soc/codecs/cs4270.c b/sound/soc/codecs/cs4270.c index 6d4bdc609ac8..8b51245f2318 100644 --- a/sound/soc/codecs/cs4270.c +++ b/sound/soc/codecs/cs4270.c | |||
@@ -106,6 +106,21 @@ | |||
106 | #define CS4270_MUTE_DAC_A 0x01 | 106 | #define CS4270_MUTE_DAC_A 0x01 |
107 | #define CS4270_MUTE_DAC_B 0x02 | 107 | #define CS4270_MUTE_DAC_B 0x02 |
108 | 108 | ||
109 | /* Power-on default values for the registers | ||
110 | * | ||
111 | * This array contains the power-on default values of the registers, with the | ||
112 | * exception of the "CHIPID" register (01h). The lower four bits of that | ||
113 | * register contain the hardware revision, so it is treated as volatile. | ||
114 | * | ||
115 | * Also note that on the CS4270, the first readable register is 1, but ASoC | ||
116 | * assumes the first register is 0. Therfore, the array must have an entry for | ||
117 | * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't | ||
118 | * be read. | ||
119 | */ | ||
120 | static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = { | ||
121 | 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00 | ||
122 | }; | ||
123 | |||
109 | static const char *supply_names[] = { | 124 | static const char *supply_names[] = { |
110 | "va", "vd", "vlc" | 125 | "va", "vd", "vlc" |
111 | }; | 126 | }; |
@@ -114,7 +129,6 @@ static const char *supply_names[] = { | |||
114 | struct cs4270_private { | 129 | struct cs4270_private { |
115 | enum snd_soc_control_type control_type; | 130 | enum snd_soc_control_type control_type; |
116 | void *control_data; | 131 | void *control_data; |
117 | u8 reg_cache[CS4270_NUMREGS]; | ||
118 | unsigned int mclk; /* Input frequency of the MCLK pin */ | 132 | unsigned int mclk; /* Input frequency of the MCLK pin */ |
119 | unsigned int mode; /* The mode (I2S or left-justified) */ | 133 | unsigned int mode; /* The mode (I2S or left-justified) */ |
120 | unsigned int slave_mode; | 134 | unsigned int slave_mode; |
@@ -179,6 +193,20 @@ static struct cs4270_mode_ratios cs4270_mode_ratios[] = { | |||
179 | /* The number of MCLK/LRCK ratios supported by the CS4270 */ | 193 | /* The number of MCLK/LRCK ratios supported by the CS4270 */ |
180 | #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) | 194 | #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) |
181 | 195 | ||
196 | static int cs4270_reg_is_readable(unsigned int reg) | ||
197 | { | ||
198 | return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG); | ||
199 | } | ||
200 | |||
201 | static int cs4270_reg_is_volatile(unsigned int reg) | ||
202 | { | ||
203 | /* Unreadable registers are considered volatile */ | ||
204 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) | ||
205 | return 1; | ||
206 | |||
207 | return reg == CS4270_CHIPID; | ||
208 | } | ||
209 | |||
182 | /** | 210 | /** |
183 | * cs4270_set_dai_sysclk - determine the CS4270 samples rates. | 211 | * cs4270_set_dai_sysclk - determine the CS4270 samples rates. |
184 | * @codec_dai: the codec DAI | 212 | * @codec_dai: the codec DAI |
@@ -264,97 +292,6 @@ static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, | |||
264 | } | 292 | } |
265 | 293 | ||
266 | /** | 294 | /** |
267 | * cs4270_fill_cache - pre-fill the CS4270 register cache. | ||
268 | * @codec: the codec for this CS4270 | ||
269 | * | ||
270 | * This function fills in the CS4270 register cache by reading the register | ||
271 | * values from the hardware. | ||
272 | * | ||
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. | ||
276 | * | ||
277 | * We use the auto-increment feature of the CS4270 to read all registers in | ||
278 | * one shot. | ||
279 | */ | ||
280 | static int cs4270_fill_cache(struct snd_soc_codec *codec) | ||
281 | { | ||
282 | u8 *cache = codec->reg_cache; | ||
283 | struct i2c_client *i2c_client = codec->control_data; | ||
284 | s32 length; | ||
285 | |||
286 | length = i2c_smbus_read_i2c_block_data(i2c_client, | ||
287 | CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache); | ||
288 | |||
289 | if (length != CS4270_NUMREGS) { | ||
290 | dev_err(codec->dev, "i2c read failure, addr=0x%x\n", | ||
291 | i2c_client->addr); | ||
292 | return -EIO; | ||
293 | } | ||
294 | |||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | /** | ||
299 | * cs4270_read_reg_cache - read from the CS4270 register cache. | ||
300 | * @codec: the codec for this CS4270 | ||
301 | * @reg: the register to read | ||
302 | * | ||
303 | * This function returns the value for a given register. It reads only from | ||
304 | * the register cache, not the hardware itself. | ||
305 | * | ||
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. | ||
309 | */ | ||
310 | static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec, | ||
311 | unsigned int reg) | ||
312 | { | ||
313 | u8 *cache = codec->reg_cache; | ||
314 | |||
315 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) | ||
316 | return -EIO; | ||
317 | |||
318 | return cache[reg - CS4270_FIRSTREG]; | ||
319 | } | ||
320 | |||
321 | /** | ||
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 | ||
326 | * | ||
327 | * This function writes the given value to the given CS4270 register, and | ||
328 | * also updates the register cache. | ||
329 | * | ||
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. | ||
333 | */ | ||
334 | static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg, | ||
335 | unsigned int value) | ||
336 | { | ||
337 | u8 *cache = codec->reg_cache; | ||
338 | |||
339 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) | ||
340 | return -EIO; | ||
341 | |||
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"); | ||
347 | return -EIO; | ||
348 | } | ||
349 | |||
350 | /* We've written to the hardware, so update the cache */ | ||
351 | cache[reg - CS4270_FIRSTREG] = value; | ||
352 | } | ||
353 | |||
354 | return 0; | ||
355 | } | ||
356 | |||
357 | /** | ||
358 | * cs4270_hw_params - program the CS4270 with the given hardware parameters. | 295 | * cs4270_hw_params - program the CS4270 with the given hardware parameters. |
359 | * @substream: the audio stream | 296 | * @substream: the audio stream |
360 | * @params: the hardware parameters to set | 297 | * @params: the hardware parameters to set |
@@ -551,15 +488,16 @@ static struct snd_soc_dai_driver cs4270_dai = { | |||
551 | static int cs4270_probe(struct snd_soc_codec *codec) | 488 | static int cs4270_probe(struct snd_soc_codec *codec) |
552 | { | 489 | { |
553 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); | 490 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
554 | int i, ret, reg; | 491 | int i, ret; |
555 | 492 | ||
556 | codec->control_data = cs4270->control_data; | 493 | codec->control_data = cs4270->control_data; |
557 | 494 | ||
558 | /* The I2C interface is set up, so pre-fill our register cache */ | 495 | /* Tell ASoC what kind of I/O to use to read the registers. ASoC will |
559 | 496 | * then do the I2C transactions itself. | |
560 | ret = cs4270_fill_cache(codec); | 497 | */ |
498 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type); | ||
561 | if (ret < 0) { | 499 | if (ret < 0) { |
562 | dev_err(codec->dev, "failed to fill register cache\n"); | 500 | dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret); |
563 | return ret; | 501 | return ret; |
564 | } | 502 | } |
565 | 503 | ||
@@ -568,10 +506,7 @@ static int cs4270_probe(struct snd_soc_codec *codec) | |||
568 | * this feature disabled by default. An application (e.g. alsactl) can | 506 | * this feature disabled by default. An application (e.g. alsactl) can |
569 | * re-enabled it by using the controls. | 507 | * re-enabled it by using the controls. |
570 | */ | 508 | */ |
571 | 509 | ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0); | |
572 | reg = cs4270_read_reg_cache(codec, CS4270_MUTE); | ||
573 | reg &= ~CS4270_MUTE_AUTO; | ||
574 | ret = cs4270_i2c_write(codec, CS4270_MUTE, reg); | ||
575 | if (ret < 0) { | 510 | if (ret < 0) { |
576 | dev_err(codec->dev, "i2c write failed\n"); | 511 | dev_err(codec->dev, "i2c write failed\n"); |
577 | return ret; | 512 | return ret; |
@@ -582,10 +517,8 @@ static int cs4270_probe(struct snd_soc_codec *codec) | |||
582 | * playback has started. An application (e.g. alsactl) can | 517 | * playback has started. An application (e.g. alsactl) can |
583 | * re-enabled it by using the controls. | 518 | * re-enabled it by using the controls. |
584 | */ | 519 | */ |
585 | 520 | ret = snd_soc_update_bits(codec, CS4270_TRANS, | |
586 | reg = cs4270_read_reg_cache(codec, CS4270_TRANS); | 521 | CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0); |
587 | reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO); | ||
588 | ret = cs4270_i2c_write(codec, CS4270_TRANS, reg); | ||
589 | if (ret < 0) { | 522 | if (ret < 0) { |
590 | dev_err(codec->dev, "i2c write failed\n"); | 523 | dev_err(codec->dev, "i2c write failed\n"); |
591 | return ret; | 524 | return ret; |
@@ -708,15 +641,16 @@ static int cs4270_soc_resume(struct snd_soc_codec *codec) | |||
708 | * Assign this variable to the codec_dev field of the machine driver's | 641 | * Assign this variable to the codec_dev field of the machine driver's |
709 | * snd_soc_device structure. | 642 | * snd_soc_device structure. |
710 | */ | 643 | */ |
711 | static struct snd_soc_codec_driver soc_codec_device_cs4270 = { | 644 | static const struct snd_soc_codec_driver soc_codec_device_cs4270 = { |
712 | .probe = cs4270_probe, | 645 | .probe = cs4270_probe, |
713 | .remove = cs4270_remove, | 646 | .remove = cs4270_remove, |
714 | .suspend = cs4270_soc_suspend, | 647 | .suspend = cs4270_soc_suspend, |
715 | .resume = cs4270_soc_resume, | 648 | .resume = cs4270_soc_resume, |
716 | .read = cs4270_read_reg_cache, | 649 | .volatile_register = cs4270_reg_is_volatile, |
717 | .write = cs4270_i2c_write, | 650 | .readable_register = cs4270_reg_is_readable, |
718 | .reg_cache_size = CS4270_NUMREGS, | 651 | .reg_cache_size = CS4270_LASTREG + 1, |
719 | .reg_word_size = sizeof(u8), | 652 | .reg_word_size = sizeof(u8), |
653 | .reg_cache_default = cs4270_default_reg_cache, | ||
720 | }; | 654 | }; |
721 | 655 | ||
722 | /** | 656 | /** |