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 <linux/regmap.h>
17 #include <linux/export.h>
18 #include <sound/soc.h>
20 #include <trace/events/asoc.h>
22 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
26 ret
= codec
->read(codec
, reg
);
27 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
28 trace_snd_soc_reg_read(codec
, reg
, ret
);
32 EXPORT_SYMBOL_GPL(snd_soc_read
);
34 int snd_soc_write(struct snd_soc_codec
*codec
, unsigned int reg
,
37 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
38 trace_snd_soc_reg_write(codec
, reg
, val
);
39 return codec
->write(codec
, reg
, val
);
41 EXPORT_SYMBOL_GPL(snd_soc_write
);
44 * snd_soc_update_bits - update codec register bits
46 * @reg: codec register
47 * @mask: register mask
50 * Writes new register value.
52 * Returns 1 for change, 0 for no change, or negative error code.
54 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned int reg
,
55 unsigned int mask
, unsigned int value
)
58 unsigned int old
, new;
61 if (codec
->using_regmap
) {
62 ret
= regmap_update_bits_check(codec
->control_data
, reg
,
63 mask
, value
, &change
);
65 ret
= snd_soc_read(codec
, reg
);
70 new = (old
& ~mask
) | (value
& mask
);
73 ret
= snd_soc_write(codec
, reg
, new);
81 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
84 * snd_soc_update_bits_locked - update codec register bits
86 * @reg: codec register
87 * @mask: register mask
90 * Writes new register value, and takes the codec mutex.
92 * Returns 1 for change else 0.
94 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
95 unsigned int reg
, unsigned int mask
,
100 mutex_lock(&codec
->mutex
);
101 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
102 mutex_unlock(&codec
->mutex
);
106 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
109 * snd_soc_test_bits - test register for change
110 * @codec: audio codec
111 * @reg: codec register
112 * @mask: register mask
115 * Tests a register with a new value and checks if the new value is
116 * different from the old value.
118 * Returns 1 for change else 0.
120 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned int reg
,
121 unsigned int mask
, unsigned int value
)
124 unsigned int old
, new;
126 old
= snd_soc_read(codec
, reg
);
127 new = (old
& ~mask
) | value
;
132 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
134 int snd_soc_platform_read(struct snd_soc_platform
*platform
,
139 if (!platform
->driver
->read
) {
140 dev_err(platform
->dev
, "ASoC: platform has no read back\n");
144 ret
= platform
->driver
->read(platform
, reg
);
145 dev_dbg(platform
->dev
, "read %x => %x\n", reg
, ret
);
146 trace_snd_soc_preg_read(platform
, reg
, ret
);
150 EXPORT_SYMBOL_GPL(snd_soc_platform_read
);
152 int snd_soc_platform_write(struct snd_soc_platform
*platform
,
153 unsigned int reg
, unsigned int val
)
155 if (!platform
->driver
->write
) {
156 dev_err(platform
->dev
, "ASoC: platform has no write back\n");
160 dev_dbg(platform
->dev
, "write %x = %x\n", reg
, val
);
161 trace_snd_soc_preg_write(platform
, reg
, val
);
162 return platform
->driver
->write(platform
, reg
, val
);
164 EXPORT_SYMBOL_GPL(snd_soc_platform_write
);
167 static int hw_write(struct snd_soc_codec
*codec
, unsigned int reg
,
170 return regmap_write(codec
->control_data
, reg
, value
);
173 static unsigned int hw_read(struct snd_soc_codec
*codec
, unsigned int reg
)
178 ret
= regmap_read(codec
->control_data
, reg
, &val
);
186 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
188 * @codec: CODEC to configure.
189 * @map: Register map to write to
191 * Register formats are frequently shared between many I2C and SPI
192 * devices. In order to promote code reuse the ASoC core provides
193 * some standard implementations of CODEC read and write operations
194 * which can be set up using this function.
196 * The caller is responsible for allocating and initialising the
199 * Note that at present this code cannot be used by CODECs with
200 * volatile registers.
202 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
203 struct regmap
*regmap
)
207 /* Device has made its own regmap arrangements */
209 codec
->control_data
= dev_get_regmap(codec
->dev
, NULL
);
211 codec
->control_data
= regmap
;
213 if (IS_ERR(codec
->control_data
))
214 return PTR_ERR(codec
->control_data
);
216 codec
->write
= hw_write
;
217 codec
->read
= hw_read
;
219 ret
= regmap_get_val_bytes(codec
->control_data
);
220 /* Errors are legitimate for non-integer byte
223 codec
->val_bytes
= ret
;
225 codec
->using_regmap
= true;
229 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);
231 int snd_soc_codec_set_cache_io(struct snd_soc_codec
*codec
,
232 struct regmap
*regmap
)
236 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io
);