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);
37 else if (component->driver->read) {
38 *val = component->driver->read(component, reg);
46 EXPORT_SYMBOL_GPL(snd_soc_component_read);
48 unsigned int snd_soc_component_read32(struct snd_soc_component *component,
54 ret = snd_soc_component_read(component, reg, &val);
60 EXPORT_SYMBOL_GPL(snd_soc_component_read32);
63 * snd_soc_component_write() - Write register value
64 * @component: Component to write to
65 * @reg: Register to write
66 * @val: Value to write to the register
68 * Return: 0 on success, a negative error code otherwise.
70 int snd_soc_component_write(struct snd_soc_component *component,
71 unsigned int reg, unsigned int val)
73 if (component->regmap)
74 return regmap_write(component->regmap, reg, val);
75 else if (component->write)
76 return component->write(component, reg, val);
77 else if (component->driver->write)
78 return component->driver->write(component, reg, val);
82 EXPORT_SYMBOL_GPL(snd_soc_component_write);
84 static int snd_soc_component_update_bits_legacy(
85 struct snd_soc_component *component, unsigned int reg,
86 unsigned int mask, unsigned int val, bool *change)
88 unsigned int old, new;
91 mutex_lock(&component->io_mutex);
93 ret = snd_soc_component_read(component, reg, &old);
97 new = (old & ~mask) | (val & mask);
100 ret = snd_soc_component_write(component, reg, new);
102 mutex_unlock(&component->io_mutex);
108 * snd_soc_component_update_bits() - Perform read/modify/write cycle
109 * @component: Component to update
110 * @reg: Register to update
111 * @mask: Mask that specifies which bits to update
112 * @val: New value for the bits specified by mask
114 * Return: 1 if the operation was successful and the value of the register
115 * changed, 0 if the operation was successful, but the value did not change.
116 * Returns a negative error code otherwise.
118 int snd_soc_component_update_bits(struct snd_soc_component *component,
119 unsigned int reg, unsigned int mask, unsigned int val)
124 if (component->regmap)
125 ret = regmap_update_bits_check(component->regmap, reg, mask,
128 ret = snd_soc_component_update_bits_legacy(component, reg,
135 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
138 * snd_soc_component_update_bits_async() - Perform asynchronous
139 * read/modify/write cycle
140 * @component: Component to update
141 * @reg: Register to update
142 * @mask: Mask that specifies which bits to update
143 * @val: New value for the bits specified by mask
145 * This function is similar to snd_soc_component_update_bits(), but the update
146 * operation is scheduled asynchronously. This means it may not be completed
147 * when the function returns. To make sure that all scheduled updates have been
148 * completed snd_soc_component_async_complete() must be called.
150 * Return: 1 if the operation was successful and the value of the register
151 * changed, 0 if the operation was successful, but the value did not change.
152 * Returns a negative error code otherwise.
154 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
155 unsigned int reg, unsigned int mask, unsigned int val)
160 if (component->regmap)
161 ret = regmap_update_bits_check_async(component->regmap, reg,
164 ret = snd_soc_component_update_bits_legacy(component, reg,
171 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
174 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
175 * @component: Component for which to wait
177 * This function blocks until all asynchronous I/O which has previously been
178 * scheduled using snd_soc_component_update_bits_async() has completed.
180 void snd_soc_component_async_complete(struct snd_soc_component *component)
182 if (component->regmap)
183 regmap_async_complete(component->regmap);
185 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
188 * snd_soc_component_test_bits - Test register for change
189 * @component: component
190 * @reg: Register to test
191 * @mask: Mask that specifies which bits to test
192 * @value: Value to test against
194 * Tests a register with a new value and checks if the new value is
195 * different from the old value.
197 * Return: 1 for change, otherwise 0.
199 int snd_soc_component_test_bits(struct snd_soc_component *component,
200 unsigned int reg, unsigned int mask, unsigned int value)
202 unsigned int old, new;
205 ret = snd_soc_component_read(component, reg, &old);
208 new = (old & ~mask) | value;
211 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
213 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
218 ret = snd_soc_component_read(&codec->component, reg, &val);
224 EXPORT_SYMBOL_GPL(snd_soc_read);
226 int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg,
229 return snd_soc_component_write(&codec->component, reg, val);
231 EXPORT_SYMBOL_GPL(snd_soc_write);
234 * snd_soc_update_bits - update codec register bits
235 * @codec: audio codec
236 * @reg: codec register
237 * @mask: register mask
240 * Writes new register value.
242 * Returns 1 for change, 0 for no change, or negative error code.
244 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg,
245 unsigned int mask, unsigned int value)
247 return snd_soc_component_update_bits(&codec->component, reg, mask,
250 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
253 * snd_soc_test_bits - test register for change
254 * @codec: audio codec
255 * @reg: codec register
256 * @mask: register mask
259 * Tests a register with a new value and checks if the new value is
260 * different from the old value.
262 * Returns 1 for change else 0.
264 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg,
265 unsigned int mask, unsigned int value)
267 return snd_soc_component_test_bits(&codec->component, reg, mask, value);
269 EXPORT_SYMBOL_GPL(snd_soc_test_bits);