2 * soc-io.c -- ASoC register I/O helpers
4 * Copyright 2009-2011 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 #include <trace/events/asoc.h>
20 #ifdef CONFIG_SPI_MASTER
21 static int do_spi_write(void *control
, const char *data
, int len
)
23 struct spi_device
*spi
= control
;
26 ret
= spi_write(spi
, data
, len
);
34 static int do_hw_write(struct snd_soc_codec
*codec
, unsigned int reg
,
35 unsigned int value
, const void *data
, int len
)
39 if (!snd_soc_codec_volatile_register(codec
, reg
) &&
40 reg
< codec
->driver
->reg_cache_size
&&
41 !codec
->cache_bypass
) {
42 ret
= snd_soc_cache_write(codec
, reg
, value
);
47 if (codec
->cache_only
) {
48 codec
->cache_sync
= 1;
52 ret
= codec
->hw_write(codec
->control_data
, data
, len
);
61 static unsigned int hw_read(struct snd_soc_codec
*codec
, unsigned int reg
)
66 if (reg
>= codec
->driver
->reg_cache_size
||
67 snd_soc_codec_volatile_register(codec
, reg
) ||
68 codec
->cache_bypass
) {
69 if (codec
->cache_only
)
72 BUG_ON(!codec
->hw_read
);
73 return codec
->hw_read(codec
, reg
);
76 ret
= snd_soc_cache_read(codec
, reg
, &val
);
82 static int snd_soc_4_12_write(struct snd_soc_codec
*codec
, unsigned int reg
,
87 data
= cpu_to_be16((reg
<< 12) | (value
& 0xffffff));
89 return do_hw_write(codec
, reg
, value
, &data
, 2);
92 static int snd_soc_7_9_write(struct snd_soc_codec
*codec
, unsigned int reg
,
97 data
= cpu_to_be16((reg
<< 9) | (value
& 0x1ff));
99 return do_hw_write(codec
, reg
, value
, &data
, 2);
102 static int snd_soc_8_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
109 data
[1] = value
& 0xff;
111 return do_hw_write(codec
, reg
, value
, data
, 2);
114 static int snd_soc_8_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
118 u16 val
= cpu_to_be16(value
);
121 memcpy(&data
[1], &val
, sizeof(val
));
123 return do_hw_write(codec
, reg
, value
, data
, 3);
126 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
127 static unsigned int do_i2c_read(struct snd_soc_codec
*codec
,
128 void *reg
, int reglen
,
129 void *data
, int datalen
)
131 struct i2c_msg xfer
[2];
133 struct i2c_client
*client
= codec
->control_data
;
136 xfer
[0].addr
= client
->addr
;
138 xfer
[0].len
= reglen
;
142 xfer
[1].addr
= client
->addr
;
143 xfer
[1].flags
= I2C_M_RD
;
144 xfer
[1].len
= datalen
;
147 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
157 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
158 static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec
*codec
,
165 ret
= do_i2c_read(codec
, ®
, 1, &data
, 1);
171 #define snd_soc_8_8_read_i2c NULL
174 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
175 static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec
*codec
,
182 ret
= do_i2c_read(codec
, ®
, 1, &data
, 2);
185 return (data
>> 8) | ((data
& 0xff) << 8);
188 #define snd_soc_8_16_read_i2c NULL
191 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
192 static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec
*codec
,
199 ret
= do_i2c_read(codec
, ®
, 2, &data
, 1);
205 #define snd_soc_16_8_read_i2c NULL
208 #if defined(CONFIG_SPI_MASTER)
209 static unsigned int snd_soc_16_8_read_spi(struct snd_soc_codec
*codec
,
212 struct spi_device
*spi
= codec
->control_data
;
214 const u16 reg
= cpu_to_be16(r
| 0x100);
218 ret
= spi_write_then_read(spi
, ®
, 2, &data
, 1);
224 #define snd_soc_16_8_read_spi NULL
227 static int snd_soc_16_8_write(struct snd_soc_codec
*codec
, unsigned int reg
,
231 u16 rval
= cpu_to_be16(reg
);
233 memcpy(data
, &rval
, sizeof(rval
));
236 return do_hw_write(codec
, reg
, value
, data
, 3);
239 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
240 static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec
*codec
,
243 u16 reg
= cpu_to_be16(r
);
247 ret
= do_i2c_read(codec
, ®
, 2, &data
, 2);
250 return be16_to_cpu(data
);
253 #define snd_soc_16_16_read_i2c NULL
256 static int snd_soc_16_16_write(struct snd_soc_codec
*codec
, unsigned int reg
,
261 data
[0] = cpu_to_be16(reg
);
262 data
[1] = cpu_to_be16(value
);
264 return do_hw_write(codec
, reg
, value
, data
, sizeof(data
));
267 /* Primitive bulk write support for soc-cache. The data pointed to by
268 * `data' needs to already be in the form the hardware expects
269 * including any leading register specific data. Any data written
270 * through this function will not go through the cache as it only
271 * handles writing to volatile or out of bounds registers.
273 static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec
*codec
, unsigned int reg
,
274 const void *data
, size_t len
)
278 /* To ensure that we don't get out of sync with the cache, check
279 * whether the base register is volatile or if we've directly asked
280 * to bypass the cache. Out of bounds registers are considered
283 if (!codec
->cache_bypass
284 && !snd_soc_codec_volatile_register(codec
, reg
)
285 && reg
< codec
->driver
->reg_cache_size
)
288 switch (codec
->control_type
) {
289 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
291 ret
= i2c_master_send(to_i2c_client(codec
->dev
), data
, len
);
294 #if defined(CONFIG_SPI_MASTER)
296 ret
= spi_write(to_spi_device(codec
->dev
), data
, len
);
314 int (*write
)(struct snd_soc_codec
*codec
, unsigned int, unsigned int);
315 unsigned int (*read
)(struct snd_soc_codec
*, unsigned int);
316 unsigned int (*i2c_read
)(struct snd_soc_codec
*, unsigned int);
317 unsigned int (*spi_read
)(struct snd_soc_codec
*, unsigned int);
320 .addr_bits
= 4, .data_bits
= 12,
321 .write
= snd_soc_4_12_write
,
324 .addr_bits
= 7, .data_bits
= 9,
325 .write
= snd_soc_7_9_write
,
328 .addr_bits
= 8, .data_bits
= 8,
329 .write
= snd_soc_8_8_write
,
330 .i2c_read
= snd_soc_8_8_read_i2c
,
333 .addr_bits
= 8, .data_bits
= 16,
334 .write
= snd_soc_8_16_write
,
335 .i2c_read
= snd_soc_8_16_read_i2c
,
338 .addr_bits
= 16, .data_bits
= 8,
339 .write
= snd_soc_16_8_write
,
340 .i2c_read
= snd_soc_16_8_read_i2c
,
341 .spi_read
= snd_soc_16_8_read_spi
,
344 .addr_bits
= 16, .data_bits
= 16,
345 .write
= snd_soc_16_16_write
,
346 .i2c_read
= snd_soc_16_16_read_i2c
,
351 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
353 * @codec: CODEC to configure.
354 * @addr_bits: Number of bits of register address data.
355 * @data_bits: Number of bits of data per register.
356 * @control: Control bus used.
358 * Register formats are frequently shared between many I2C and SPI
359 * devices. In order to promote code reuse the ASoC core provides
360 * some standard implementations of CODEC read and write operations
361 * which can be set up using this function.
363 * The caller is responsible for allocating and initialising the
366 * Note that at present this code cannot be used by CODECs with
367 * volatile registers.
369 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
370 int addr_bits
, int data_bits
,
371 enum snd_soc_control_type control
)
375 for (i
= 0; i
< ARRAY_SIZE(io_types
); i
++)
376 if (io_types
[i
].addr_bits
== addr_bits
&&
377 io_types
[i
].data_bits
== data_bits
)
379 if (i
== ARRAY_SIZE(io_types
)) {
381 "No I/O functions for %d bit address %d bit data\n",
382 addr_bits
, data_bits
);
386 codec
->write
= io_types
[i
].write
;
387 codec
->read
= hw_read
;
388 codec
->bulk_write_raw
= snd_soc_hw_bulk_write_raw
;
392 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
393 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
395 if (io_types
[i
].i2c_read
)
396 codec
->hw_read
= io_types
[i
].i2c_read
;
398 codec
->control_data
= container_of(codec
->dev
,
404 #ifdef CONFIG_SPI_MASTER
405 codec
->hw_write
= do_spi_write
;
407 if (io_types
[i
].spi_read
)
408 codec
->hw_read
= io_types
[i
].spi_read
;
410 codec
->control_data
= container_of(codec
->dev
,
418 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);