2 * soc-cache.c -- ASoC register cache helpers
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <sound/soc.h>
18 static unsigned int snd_soc_4_12_read(struct snd_soc_codec
*codec
,
21 u16
*cache
= codec
->reg_cache
;
22 if (reg
>= codec
->reg_cache_size
)
27 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
30 u16
*cache
= codec
->reg_cache
;
34 BUG_ON(codec
->volatile_register
);
36 data
[0] = (reg
<< 4) | ((value
>> 8) & 0x000f);
37 data
[1] = value
& 0x00ff;
39 if (reg
< codec
->reg_cache_size
)
42 if (codec
->cache_only
) {
43 codec
->cache_sync
= 1;
47 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
49 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
58 #if defined(CONFIG_SPI_MASTER)
59 static int snd_soc_4_12_spi_write(void *control_data
, const char *data
,
62 struct spi_device
*spi
= control_data
;
63 struct spi_transfer t
;
74 memset(&t
, 0, (sizeof t
));
79 spi_message_add_tail(&t
, &m
);
85 #define snd_soc_4_12_spi_write NULL
88 static unsigned int snd_soc_7_9_read(struct snd_soc_codec
*codec
,
91 u16
*cache
= codec
->reg_cache
;
92 if (reg
>= codec
->reg_cache_size
)
97 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
100 u16
*cache
= codec
->reg_cache
;
104 BUG_ON(codec
->volatile_register
);
106 data
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
107 data
[1] = value
& 0x00ff;
109 if (reg
< codec
->reg_cache_size
)
112 if (codec
->cache_only
) {
113 codec
->cache_sync
= 1;
117 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
119 ret
= codec
->hw_write(codec
->control_data
, data
, 2);
128 #if defined(CONFIG_SPI_MASTER)
129 static int snd_soc_7_9_spi_write(void *control_data
, const char *data
,
132 struct spi_device
*spi
= control_data
;
133 struct spi_transfer t
;
134 struct spi_message m
;
143 spi_message_init(&m
);
144 memset(&t
, 0, (sizeof t
));
149 spi_message_add_tail(&t
, &m
);
155 #define snd_soc_7_9_spi_write NULL
158 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
161 u8
*cache
= codec
->reg_cache
;
164 BUG_ON(codec
->volatile_register
);
168 data
[1] = value
& 0xff;
170 if (reg
< codec
->reg_cache_size
)
173 if (codec
->cache_only
) {
174 codec
->cache_sync
= 1;
178 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
180 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2)
186 static unsigned int snd_soc_8_8_read(struct snd_soc_codec
*codec
,
189 u8
*cache
= codec
->reg_cache
;
191 if (reg
>= codec
->reg_cache_size
)
196 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
199 u16
*reg_cache
= codec
->reg_cache
;
203 data
[1] = (value
>> 8) & 0xff;
204 data
[2] = value
& 0xff;
206 if (!snd_soc_codec_volatile_register(codec
, reg
))
207 reg_cache
[reg
] = value
;
209 if (codec
->cache_only
) {
210 codec
->cache_sync
= 1;
214 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
216 if (codec
->hw_write(codec
->control_data
, data
, 3) == 3)
222 static unsigned int snd_soc_8_16_read(struct snd_soc_codec
*codec
,
225 u16
*cache
= codec
->reg_cache
;
227 if (reg
>= codec
->reg_cache_size
||
228 snd_soc_codec_volatile_register(codec
, reg
)) {
229 if (codec
->cache_only
)
232 return codec
->hw_read(codec
, reg
);
238 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
239 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
242 struct i2c_msg xfer
[2];
246 struct i2c_client
*client
= codec
->control_data
;
249 xfer
[0].addr
= client
->addr
;
255 xfer
[1].addr
= client
->addr
;
256 xfer
[1].flags
= I2C_M_RD
;
260 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
262 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
269 #define snd_soc_8_8_read_i2c NULL
272 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
273 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
276 struct i2c_msg xfer
[2];
280 struct i2c_client
*client
= codec
->control_data
;
283 xfer
[0].addr
= client
->addr
;
289 xfer
[1].addr
= client
->addr
;
290 xfer
[1].flags
= I2C_M_RD
;
292 xfer
[1].buf
= (u8
*)&data
;
294 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
296 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
300 return (data
>> 8) | ((data
& 0xff) << 8);
303 #define snd_soc_8_16_read_i2c NULL
306 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
307 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
310 struct i2c_msg xfer
[2];
314 struct i2c_client
*client
= codec
->control_data
;
317 xfer
[0].addr
= client
->addr
;
320 xfer
[0].buf
= (u8
*)®
;
323 xfer
[1].addr
= client
->addr
;
324 xfer
[1].flags
= I2C_M_RD
;
328 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
330 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
337 #define snd_soc_16_8_read_i2c NULL
340 static unsigned int snd_soc_16_8_read(struct snd_soc_codec
*codec
,
343 u16
*cache
= codec
->reg_cache
;
346 if (reg
>= codec
->reg_cache_size
)
351 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
354 u16
*cache
= codec
->reg_cache
;
358 BUG_ON(codec
->volatile_register
);
360 data
[0] = (reg
>> 8) & 0xff;
361 data
[1] = reg
& 0xff;
365 if (reg
< codec
->reg_cache_size
)
368 if (codec
->cache_only
) {
369 codec
->cache_sync
= 1;
373 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
375 ret
= codec
->hw_write(codec
->control_data
, data
, 3);
384 #if defined(CONFIG_SPI_MASTER)
385 static int snd_soc_16_8_spi_write(void *control_data
, const char *data
,
388 struct spi_device
*spi
= control_data
;
389 struct spi_transfer t
;
390 struct spi_message m
;
400 spi_message_init(&m
);
401 memset(&t
, 0, (sizeof t
));
406 spi_message_add_tail(&t
, &m
);
412 #define snd_soc_16_8_spi_write NULL
415 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
416 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
419 struct i2c_msg xfer
[2];
420 u16 reg
= cpu_to_be16(r
);
423 struct i2c_client
*client
= codec
->control_data
;
426 xfer
[0].addr
= client
->addr
;
429 xfer
[0].buf
= (u8
*)®
;
432 xfer
[1].addr
= client
->addr
;
433 xfer
[1].flags
= I2C_M_RD
;
435 xfer
[1].buf
= (u8
*)&data
;
437 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
439 dev_err(&client
->dev
, "i2c_transfer() returned %d\n", ret
);
443 return be16_to_cpu(data
);
446 #define snd_soc_16_16_read_i2c NULL
449 static unsigned int snd_soc_16_16_read(struct snd_soc_codec
*codec
,
452 u16
*cache
= codec
->reg_cache
;
454 if (reg
>= codec
->reg_cache_size
||
455 snd_soc_codec_volatile_register(codec
, reg
)) {
456 if (codec
->cache_only
)
459 return codec
->hw_read(codec
, reg
);
465 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
468 u16
*cache
= codec
->reg_cache
;
472 data
[0] = (reg
>> 8) & 0xff;
473 data
[1] = reg
& 0xff;
474 data
[2] = (value
>> 8) & 0xff;
475 data
[3] = value
& 0xff;
477 if (reg
< codec
->reg_cache_size
)
480 if (codec
->cache_only
) {
481 codec
->cache_sync
= 1;
485 dev_dbg(codec
->dev
, "0x%x = 0x%x\n", reg
, value
);
487 ret
= codec
->hw_write(codec
->control_data
, data
, 4);
499 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
500 int (*spi_write
)(void *, const char *, int);
501 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
502 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
505 .addr_bits
= 4, .data_bits
= 12,
506 .write
= snd_soc_4_12_write
, .read
= snd_soc_4_12_read
,
507 .spi_write
= snd_soc_4_12_spi_write
,
510 .addr_bits
= 7, .data_bits
= 9,
511 .write
= snd_soc_7_9_write
, .read
= snd_soc_7_9_read
,
512 .spi_write
= snd_soc_7_9_spi_write
,
515 .addr_bits
= 8, .data_bits
= 8,
516 .write
= snd_soc_8_8_write
, .read
= snd_soc_8_8_read
,
517 .i2c_read
= snd_soc_8_8_read_i2c
,
520 .addr_bits
= 8, .data_bits
= 16,
521 .write
= snd_soc_8_16_write
, .read
= snd_soc_8_16_read
,
522 .i2c_read
= snd_soc_8_16_read_i2c
,
525 .addr_bits
= 16, .data_bits
= 8,
526 .write
= snd_soc_16_8_write
, .read
= snd_soc_16_8_read
,
527 .i2c_read
= snd_soc_16_8_read_i2c
,
528 .spi_write
= snd_soc_16_8_spi_write
,
531 .addr_bits
= 16, .data_bits
= 16,
532 .write
= snd_soc_16_16_write
, .read
= snd_soc_16_16_read
,
533 .i2c_read
= snd_soc_16_16_read_i2c
,
538 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
540 * @codec: CODEC to configure.
541 * @type: Type of cache.
542 * @addr_bits: Number of bits of register address data.
543 * @data_bits: Number of bits of data per register.
544 * @control: Control bus used.
546 * Register formats are frequently shared between many I2C and SPI
547 * devices. In order to promote code reuse the ASoC core provides
548 * some standard implementations of CODEC read and write operations
549 * which can be set up using this function.
551 * The caller is responsible for allocating and initialising the
554 * Note that at present this code cannot be used by CODECs with
555 * volatile registers.
557 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
558 int addr_bits
, int data_bits
,
559 enum snd_soc_control_type control
)
563 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
564 if (io_types
[i
].addr_bits
== addr_bits
&&
565 io_types
[i
].data_bits
== data_bits
)
567 if (i
== ARRAY_SIZE(io_types
)) {
569 "No I/O functions for %d bit address %d bit data\n",
570 addr_bits
, data_bits
);
574 codec
->write
= io_types
[i
].write
;
575 codec
->read
= io_types
[i
].read
;
582 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
583 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
585 if (io_types
[i
].i2c_read
)
586 codec
->hw_read
= io_types
[i
].i2c_read
;
590 if (io_types
[i
].spi_write
)
591 codec
->hw_write
= io_types
[i
].spi_write
;
597 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);