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>
21 * snd_soc_component_read() - Read register value
22 * @component: Component to read from
23 * @reg: Register to read
24 * @val: Pointer to where the read value is stored
26 * Return: 0 on success, a negative error code otherwise.
28 int snd_soc_component_read(struct snd_soc_component
*component
,
29 unsigned int reg
, unsigned int *val
)
33 if (component
->regmap
)
34 ret
= regmap_read(component
->regmap
, reg
, val
);
35 else if (component
->read
)
36 ret
= component
->read(component
, reg
, val
);
42 EXPORT_SYMBOL_GPL(snd_soc_component_read
);
44 unsigned int snd_soc_component_read32(struct snd_soc_component
*component
,
50 ret
= snd_soc_component_read(component
, reg
, &val
);
56 EXPORT_SYMBOL_GPL(snd_soc_component_read32
);
59 * snd_soc_component_write() - Write register value
60 * @component: Component to write to
61 * @reg: Register to write
62 * @val: Value to write to the register
64 * Return: 0 on success, a negative error code otherwise.
66 int snd_soc_component_write(struct snd_soc_component
*component
,
67 unsigned int reg
, unsigned int val
)
69 if (component
->regmap
)
70 return regmap_write(component
->regmap
, reg
, val
);
71 else if (component
->write
)
72 return component
->write(component
, reg
, val
);
76 EXPORT_SYMBOL_GPL(snd_soc_component_write
);
78 static int snd_soc_component_update_bits_legacy(
79 struct snd_soc_component
*component
, unsigned int reg
,
80 unsigned int mask
, unsigned int val
, bool *change
)
82 unsigned int old
, new;
85 if (!component
->read
|| !component
->write
)
88 mutex_lock(&component
->io_mutex
);
90 ret
= component
->read(component
, reg
, &old
);
94 new = (old
& ~mask
) | (val
& mask
);
97 ret
= component
->write(component
, reg
, new);
99 mutex_unlock(&component
->io_mutex
);
105 * snd_soc_component_update_bits() - Perform read/modify/write cycle
106 * @component: Component to update
107 * @reg: Register to update
108 * @mask: Mask that specifies which bits to update
109 * @val: New value for the bits specified by mask
111 * Return: 1 if the operation was successful and the value of the register
112 * changed, 0 if the operation was successful, but the value did not change.
113 * Returns a negative error code otherwise.
115 int snd_soc_component_update_bits(struct snd_soc_component
*component
,
116 unsigned int reg
, unsigned int mask
, unsigned int val
)
121 if (component
->regmap
)
122 ret
= regmap_update_bits_check(component
->regmap
, reg
, mask
,
125 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
132 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits
);
135 * snd_soc_component_update_bits_async() - Perform asynchronous
136 * read/modify/write cycle
137 * @component: Component to update
138 * @reg: Register to update
139 * @mask: Mask that specifies which bits to update
140 * @val: New value for the bits specified by mask
142 * This function is similar to snd_soc_component_update_bits(), but the update
143 * operation is scheduled asynchronously. This means it may not be completed
144 * when the function returns. To make sure that all scheduled updates have been
145 * completed snd_soc_component_async_complete() must be called.
147 * Return: 1 if the operation was successful and the value of the register
148 * changed, 0 if the operation was successful, but the value did not change.
149 * Returns a negative error code otherwise.
151 int snd_soc_component_update_bits_async(struct snd_soc_component
*component
,
152 unsigned int reg
, unsigned int mask
, unsigned int val
)
157 if (component
->regmap
)
158 ret
= regmap_update_bits_check_async(component
->regmap
, reg
,
161 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
168 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async
);
171 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
172 * @component: Component for which to wait
174 * This function blocks until all asynchronous I/O which has previously been
175 * scheduled using snd_soc_component_update_bits_async() has completed.
177 void snd_soc_component_async_complete(struct snd_soc_component
*component
)
179 if (component
->regmap
)
180 regmap_async_complete(component
->regmap
);
182 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete
);
185 * snd_soc_component_test_bits - Test register for change
186 * @component: component
187 * @reg: Register to test
188 * @mask: Mask that specifies which bits to test
189 * @value: Value to test against
191 * Tests a register with a new value and checks if the new value is
192 * different from the old value.
194 * Return: 1 for change, otherwise 0.
196 int snd_soc_component_test_bits(struct snd_soc_component
*component
,
197 unsigned int reg
, unsigned int mask
, unsigned int value
)
199 unsigned int old
, new;
202 ret
= snd_soc_component_read(component
, reg
, &old
);
205 new = (old
& ~mask
) | value
;
208 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits
);
210 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
215 ret
= snd_soc_component_read(&codec
->component
, reg
, &val
);
221 EXPORT_SYMBOL_GPL(snd_soc_read
);
223 int snd_soc_write(struct snd_soc_codec
*codec
, unsigned int reg
,
226 return snd_soc_component_write(&codec
->component
, reg
, val
);
228 EXPORT_SYMBOL_GPL(snd_soc_write
);
231 * snd_soc_update_bits - update codec register bits
232 * @codec: audio codec
233 * @reg: codec register
234 * @mask: register mask
237 * Writes new register value.
239 * Returns 1 for change, 0 for no change, or negative error code.
241 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned int reg
,
242 unsigned int mask
, unsigned int value
)
244 return snd_soc_component_update_bits(&codec
->component
, reg
, mask
,
247 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
250 * snd_soc_test_bits - test register for change
251 * @codec: audio codec
252 * @reg: codec register
253 * @mask: register mask
256 * Tests a register with a new value and checks if the new value is
257 * different from the old value.
259 * Returns 1 for change else 0.
261 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned int reg
,
262 unsigned int mask
, unsigned int value
)
264 return snd_soc_component_test_bits(&codec
->component
, reg
, mask
, value
);
266 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
268 int snd_soc_platform_read(struct snd_soc_platform
*platform
,
274 ret
= snd_soc_component_read(&platform
->component
, reg
, &val
);
280 EXPORT_SYMBOL_GPL(snd_soc_platform_read
);
282 int snd_soc_platform_write(struct snd_soc_platform
*platform
,
283 unsigned int reg
, unsigned int val
)
285 return snd_soc_component_write(&platform
->component
, reg
, val
);
287 EXPORT_SYMBOL_GPL(snd_soc_platform_write
);