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
->driver
->read
) {
36 *val
= component
->driver
->read(component
, reg
);
44 EXPORT_SYMBOL_GPL(snd_soc_component_read
);
46 unsigned int snd_soc_component_read32(struct snd_soc_component
*component
,
52 ret
= snd_soc_component_read(component
, reg
, &val
);
58 EXPORT_SYMBOL_GPL(snd_soc_component_read32
);
61 * snd_soc_component_write() - Write register value
62 * @component: Component to write to
63 * @reg: Register to write
64 * @val: Value to write to the register
66 * Return: 0 on success, a negative error code otherwise.
68 int snd_soc_component_write(struct snd_soc_component
*component
,
69 unsigned int reg
, unsigned int val
)
71 if (component
->regmap
)
72 return regmap_write(component
->regmap
, reg
, val
);
73 else if (component
->driver
->write
)
74 return component
->driver
->write(component
, reg
, val
);
78 EXPORT_SYMBOL_GPL(snd_soc_component_write
);
80 static int snd_soc_component_update_bits_legacy(
81 struct snd_soc_component
*component
, unsigned int reg
,
82 unsigned int mask
, unsigned int val
, bool *change
)
84 unsigned int old
, new;
87 mutex_lock(&component
->io_mutex
);
89 ret
= snd_soc_component_read(component
, reg
, &old
);
93 new = (old
& ~mask
) | (val
& mask
);
96 ret
= snd_soc_component_write(component
, reg
, new);
98 mutex_unlock(&component
->io_mutex
);
104 * snd_soc_component_update_bits() - Perform read/modify/write cycle
105 * @component: Component to update
106 * @reg: Register to update
107 * @mask: Mask that specifies which bits to update
108 * @val: New value for the bits specified by mask
110 * Return: 1 if the operation was successful and the value of the register
111 * changed, 0 if the operation was successful, but the value did not change.
112 * Returns a negative error code otherwise.
114 int snd_soc_component_update_bits(struct snd_soc_component
*component
,
115 unsigned int reg
, unsigned int mask
, unsigned int val
)
120 if (component
->regmap
)
121 ret
= regmap_update_bits_check(component
->regmap
, reg
, mask
,
124 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
131 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits
);
134 * snd_soc_component_update_bits_async() - Perform asynchronous
135 * read/modify/write cycle
136 * @component: Component to update
137 * @reg: Register to update
138 * @mask: Mask that specifies which bits to update
139 * @val: New value for the bits specified by mask
141 * This function is similar to snd_soc_component_update_bits(), but the update
142 * operation is scheduled asynchronously. This means it may not be completed
143 * when the function returns. To make sure that all scheduled updates have been
144 * completed snd_soc_component_async_complete() must be called.
146 * Return: 1 if the operation was successful and the value of the register
147 * changed, 0 if the operation was successful, but the value did not change.
148 * Returns a negative error code otherwise.
150 int snd_soc_component_update_bits_async(struct snd_soc_component
*component
,
151 unsigned int reg
, unsigned int mask
, unsigned int val
)
156 if (component
->regmap
)
157 ret
= regmap_update_bits_check_async(component
->regmap
, reg
,
160 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
167 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async
);
170 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
171 * @component: Component for which to wait
173 * This function blocks until all asynchronous I/O which has previously been
174 * scheduled using snd_soc_component_update_bits_async() has completed.
176 void snd_soc_component_async_complete(struct snd_soc_component
*component
)
178 if (component
->regmap
)
179 regmap_async_complete(component
->regmap
);
181 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete
);
184 * snd_soc_component_test_bits - Test register for change
185 * @component: component
186 * @reg: Register to test
187 * @mask: Mask that specifies which bits to test
188 * @value: Value to test against
190 * Tests a register with a new value and checks if the new value is
191 * different from the old value.
193 * Return: 1 for change, otherwise 0.
195 int snd_soc_component_test_bits(struct snd_soc_component
*component
,
196 unsigned int reg
, unsigned int mask
, unsigned int value
)
198 unsigned int old
, new;
201 ret
= snd_soc_component_read(component
, reg
, &old
);
204 new = (old
& ~mask
) | value
;
207 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits
);