blob: ac64fd7252b2806ff5d8a32f6da6d031620e627e [file] [log] [blame]
Mark Brown5bef44f2011-06-13 17:49:55 +01001/*
2 * soc-io.c -- ASoC register I/O helpers
3 *
4 * Copyright 2009-2011 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 */
13
14#include <linux/i2c.h>
15#include <linux/spi/spi.h>
Mark Brownbe3ea3b2011-06-13 19:35:29 +010016#include <linux/regmap.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040017#include <linux/export.h>
Mark Brown5bef44f2011-06-13 17:49:55 +010018#include <sound/soc.h>
19
20#include <trace/events/asoc.h>
21
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +020022/**
23 * snd_soc_component_read() - Read register value
24 * @component: Component to read from
25 * @reg: Register to read
26 * @val: Pointer to where the read value is stored
27 *
28 * Return: 0 on success, a negative error code otherwise.
29 */
30int snd_soc_component_read(struct snd_soc_component *component,
31 unsigned int reg, unsigned int *val)
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010032{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +020033 int ret;
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010034
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +020035 if (component->regmap)
36 ret = regmap_read(component->regmap, reg, val);
37 else if (component->read)
38 ret = component->read(component, reg, val);
39 else
40 ret = -EIO;
41
42 dev_dbg(component->dev, "read %x => %x\n", reg, *val);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +010043
44 return ret;
45}
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +020046EXPORT_SYMBOL_GPL(snd_soc_component_read);
47
48/**
49 * snd_soc_component_write() - Write register value
50 * @component: Component to write to
51 * @reg: Register to write
52 * @val: Value to write to the register
53 *
54 * Return: 0 on success, a negative error code otherwise.
55 */
56int snd_soc_component_write(struct snd_soc_component *component,
57 unsigned int reg, unsigned int val)
58{
59 dev_dbg(component->dev, "write %x = %x\n", reg, val);
60
61 if (component->regmap)
62 return regmap_write(component->regmap, reg, val);
63 else if (component->write)
64 return component->write(component, reg, val);
65 else
66 return -EIO;
67}
68EXPORT_SYMBOL_GPL(snd_soc_component_write);
69
70static int snd_soc_component_update_bits_legacy(
71 struct snd_soc_component *component, unsigned int reg,
72 unsigned int mask, unsigned int val, bool *change)
73{
74 unsigned int old, new;
75 int ret;
76
77 if (!component->read || !component->write)
78 return -EIO;
79
80 mutex_lock(&component->io_mutex);
81
82 ret = component->read(component, reg, &old);
83 if (ret < 0)
84 goto out_unlock;
85
86 new = (old & ~mask) | (val & mask);
87 *change = old != new;
88 if (*change)
89 ret = component->write(component, reg, new);
90out_unlock:
91 mutex_unlock(&component->io_mutex);
92
93 return ret;
94}
95
96/**
97 * snd_soc_component_update_bits() - Perform read/modify/write cycle
98 * @component: Component to update
99 * @reg: Register to update
100 * @mask: Mask that specifies which bits to update
101 * @val: New value for the bits specified by mask
102 *
103 * Return: 1 if the operation was successful and the value of the register
104 * changed, 0 if the operation was successful, but the value did not change.
105 * Returns a negative error code otherwise.
106 */
107int snd_soc_component_update_bits(struct snd_soc_component *component,
108 unsigned int reg, unsigned int mask, unsigned int val)
109{
110 bool change;
111 int ret;
112
113 if (component->regmap)
114 ret = regmap_update_bits_check(component->regmap, reg, mask,
115 val, &change);
116 else
117 ret = snd_soc_component_update_bits_legacy(component, reg,
118 mask, val, &change);
119
120 if (ret < 0)
121 return ret;
122 return change;
123}
124EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
125
126/**
127 * snd_soc_component_update_bits_async() - Perform asynchronous
128 * read/modify/write cycle
129 * @component: Component to update
130 * @reg: Register to update
131 * @mask: Mask that specifies which bits to update
132 * @val: New value for the bits specified by mask
133 *
134 * This function is similar to snd_soc_component_update_bits(), but the update
135 * operation is scheduled asynchronously. This means it may not be completed
136 * when the function returns. To make sure that all scheduled updates have been
137 * completed snd_soc_component_async_complete() must be called.
138 *
139 * Return: 1 if the operation was successful and the value of the register
140 * changed, 0 if the operation was successful, but the value did not change.
141 * Returns a negative error code otherwise.
142 */
143int snd_soc_component_update_bits_async(struct snd_soc_component *component,
144 unsigned int reg, unsigned int mask, unsigned int val)
145{
146 bool change;
147 int ret;
148
149 if (component->regmap)
150 ret = regmap_update_bits_check_async(component->regmap, reg,
151 mask, val, &change);
152 else
153 ret = snd_soc_component_update_bits_legacy(component, reg,
154 mask, val, &change);
155
156 if (ret < 0)
157 return ret;
158 return change;
159}
160EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
161
162/**
163 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
164 * @component: Component for which to wait
165 *
166 * This function blocks until all asynchronous I/O which has previously been
167 * scheduled using snd_soc_component_update_bits_async() has completed.
168 */
169void snd_soc_component_async_complete(struct snd_soc_component *component)
170{
171 if (component->regmap)
172 regmap_async_complete(component->regmap);
173}
174EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
175
176/**
177 * snd_soc_component_test_bits - Test register for change
178 * @component: component
179 * @reg: Register to test
180 * @mask: Mask that specifies which bits to test
181 * @value: Value to test against
182 *
183 * Tests a register with a new value and checks if the new value is
184 * different from the old value.
185 *
186 * Return: 1 for change, otherwise 0.
187 */
188int snd_soc_component_test_bits(struct snd_soc_component *component,
189 unsigned int reg, unsigned int mask, unsigned int value)
190{
191 unsigned int old, new;
192 int ret;
193
194 ret = snd_soc_component_read(component, reg, &old);
195 if (ret < 0)
196 return ret;
197 new = (old & ~mask) | value;
198 return old != new;
199}
200EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
201
202unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
203{
204 unsigned int val;
205 int ret;
206
207 ret = snd_soc_component_read(&codec->component, reg, &val);
208 if (ret < 0)
209 return -1;
210 trace_snd_soc_reg_read(codec, reg, val);
211
212 return val;
213}
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100214EXPORT_SYMBOL_GPL(snd_soc_read);
215
Lars-Peter Clausenab2874a2014-04-19 10:43:57 +0200216int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg,
217 unsigned int val)
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100218{
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100219 trace_snd_soc_reg_write(codec, reg, val);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200220 return snd_soc_component_write(&codec->component, reg, val);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100221}
222EXPORT_SYMBOL_GPL(snd_soc_write);
223
224/**
225 * snd_soc_update_bits - update codec register bits
226 * @codec: audio codec
227 * @reg: codec register
228 * @mask: register mask
229 * @value: new value
230 *
231 * Writes new register value.
232 *
233 * Returns 1 for change, 0 for no change, or negative error code.
234 */
Mark Brownaa0258a2014-04-14 17:42:28 +0100235int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100236 unsigned int mask, unsigned int value)
237{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200238 return snd_soc_component_update_bits(&codec->component, reg, mask,
239 value);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100240}
241EXPORT_SYMBOL_GPL(snd_soc_update_bits);
242
243/**
244 * snd_soc_update_bits_locked - update codec register bits
245 * @codec: audio codec
246 * @reg: codec register
247 * @mask: register mask
248 * @value: new value
249 *
250 * Writes new register value, and takes the codec mutex.
251 *
252 * Returns 1 for change else 0.
253 */
254int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
Mark Brownaa0258a2014-04-14 17:42:28 +0100255 unsigned int reg, unsigned int mask,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100256 unsigned int value)
257{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200258 return snd_soc_component_update_bits(&codec->component, reg, mask,
259 value);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100260}
261EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
262
263/**
264 * snd_soc_test_bits - test register for change
265 * @codec: audio codec
266 * @reg: codec register
267 * @mask: register mask
268 * @value: new value
269 *
270 * Tests a register with a new value and checks if the new value is
271 * different from the old value.
272 *
273 * Returns 1 for change else 0.
274 */
Mark Brownaa0258a2014-04-14 17:42:28 +0100275int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg,
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100276 unsigned int mask, unsigned int value)
277{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200278 return snd_soc_component_test_bits(&codec->component, reg, mask, value);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100279}
280EXPORT_SYMBOL_GPL(snd_soc_test_bits);
281
282int snd_soc_platform_read(struct snd_soc_platform *platform,
283 unsigned int reg)
284{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200285 unsigned int val;
286 int ret;
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100287
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200288 ret = snd_soc_component_read(&platform->component, reg, &val);
289 if (ret < 0)
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100290 return -1;
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100291
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200292 trace_snd_soc_preg_read(platform, reg, val);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100293
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200294 return val;
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100295}
296EXPORT_SYMBOL_GPL(snd_soc_platform_read);
297
298int snd_soc_platform_write(struct snd_soc_platform *platform,
299 unsigned int reg, unsigned int val)
300{
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100301 trace_snd_soc_preg_write(platform, reg, val);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200302 return snd_soc_component_write(&platform->component, reg, val);
Lars-Peter Clausen96241c832014-03-18 09:02:07 +0100303}
304EXPORT_SYMBOL_GPL(snd_soc_platform_write);
305
Mark Brown5bef44f2011-06-13 17:49:55 +0100306/**
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200307 * snd_soc_component_init_io() - Initialize regmap IO
Mark Brown5bef44f2011-06-13 17:49:55 +0100308 *
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200309 * @component: component to initialize
310 * @regmap: regmap instance to use for IO operations
Mark Brown5bef44f2011-06-13 17:49:55 +0100311 *
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200312 * Return: 0 on success, a negative error code otherwise
Mark Brown5bef44f2011-06-13 17:49:55 +0100313 */
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200314int snd_soc_component_init_io(struct snd_soc_component *component,
315 struct regmap *regmap)
Mark Brown5bef44f2011-06-13 17:49:55 +0100316{
Mark Brown2b4bdee2012-02-17 14:33:29 -0800317 int ret;
Mark Brown5bef44f2011-06-13 17:49:55 +0100318
Xiubo Li092eba92014-03-11 12:43:21 +0800319 if (!regmap)
Xiubo Lia39f75f2014-03-26 13:40:23 +0800320 return -EINVAL;
Xiubo Li092eba92014-03-11 12:43:21 +0800321
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200322 ret = regmap_get_val_bytes(regmap);
Xiubo Li092eba92014-03-11 12:43:21 +0800323 /* Errors are legitimate for non-integer byte
324 * multiples */
325 if (ret > 0)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200326 component->val_bytes = ret;
Mark Brown2b4bdee2012-02-17 14:33:29 -0800327
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200328 component->regmap = regmap;
Mark Brown0671da12011-07-24 12:23:37 +0100329
Xiubo Li092eba92014-03-11 12:43:21 +0800330 return 0;
Mark Brown5bef44f2011-06-13 17:49:55 +0100331}
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200332EXPORT_SYMBOL_GPL(snd_soc_component_init_io);