ASoC: Change return type of snd_soc_write() to int
[deliverable/linux.git] / sound / soc / soc-io.c
1 /*
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>
16 #include <linux/regmap.h>
17 #include <linux/export.h>
18 #include <sound/soc.h>
19
20 #include <trace/events/asoc.h>
21
22 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
23 {
24 unsigned int ret;
25
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);
29
30 return ret;
31 }
32 EXPORT_SYMBOL_GPL(snd_soc_read);
33
34 int snd_soc_write(struct snd_soc_codec *codec, unsigned int reg,
35 unsigned int val)
36 {
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);
40 }
41 EXPORT_SYMBOL_GPL(snd_soc_write);
42
43 /**
44 * snd_soc_update_bits - update codec register bits
45 * @codec: audio codec
46 * @reg: codec register
47 * @mask: register mask
48 * @value: new value
49 *
50 * Writes new register value.
51 *
52 * Returns 1 for change, 0 for no change, or negative error code.
53 */
54 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned int reg,
55 unsigned int mask, unsigned int value)
56 {
57 bool change;
58 unsigned int old, new;
59 int ret;
60
61 if (codec->using_regmap) {
62 ret = regmap_update_bits_check(codec->control_data, reg,
63 mask, value, &change);
64 } else {
65 ret = snd_soc_read(codec, reg);
66 if (ret < 0)
67 return ret;
68
69 old = ret;
70 new = (old & ~mask) | (value & mask);
71 change = old != new;
72 if (change)
73 ret = snd_soc_write(codec, reg, new);
74 }
75
76 if (ret < 0)
77 return ret;
78
79 return change;
80 }
81 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
82
83 /**
84 * snd_soc_update_bits_locked - update codec register bits
85 * @codec: audio codec
86 * @reg: codec register
87 * @mask: register mask
88 * @value: new value
89 *
90 * Writes new register value, and takes the codec mutex.
91 *
92 * Returns 1 for change else 0.
93 */
94 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
95 unsigned int reg, unsigned int mask,
96 unsigned int value)
97 {
98 int change;
99
100 mutex_lock(&codec->mutex);
101 change = snd_soc_update_bits(codec, reg, mask, value);
102 mutex_unlock(&codec->mutex);
103
104 return change;
105 }
106 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
107
108 /**
109 * snd_soc_test_bits - test register for change
110 * @codec: audio codec
111 * @reg: codec register
112 * @mask: register mask
113 * @value: new value
114 *
115 * Tests a register with a new value and checks if the new value is
116 * different from the old value.
117 *
118 * Returns 1 for change else 0.
119 */
120 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned int reg,
121 unsigned int mask, unsigned int value)
122 {
123 int change;
124 unsigned int old, new;
125
126 old = snd_soc_read(codec, reg);
127 new = (old & ~mask) | value;
128 change = old != new;
129
130 return change;
131 }
132 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
133
134 int snd_soc_platform_read(struct snd_soc_platform *platform,
135 unsigned int reg)
136 {
137 unsigned int ret;
138
139 if (!platform->driver->read) {
140 dev_err(platform->dev, "ASoC: platform has no read back\n");
141 return -1;
142 }
143
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);
147
148 return ret;
149 }
150 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
151
152 int snd_soc_platform_write(struct snd_soc_platform *platform,
153 unsigned int reg, unsigned int val)
154 {
155 if (!platform->driver->write) {
156 dev_err(platform->dev, "ASoC: platform has no write back\n");
157 return -1;
158 }
159
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);
163 }
164 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
165
166 #ifdef CONFIG_REGMAP
167 static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
168 unsigned int value)
169 {
170 return regmap_write(codec->control_data, reg, value);
171 }
172
173 static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
174 {
175 int ret;
176 unsigned int val;
177
178 ret = regmap_read(codec->control_data, reg, &val);
179 if (ret == 0)
180 return val;
181 else
182 return -1;
183 }
184
185 /**
186 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
187 *
188 * @codec: CODEC to configure.
189 * @map: Register map to write to
190 *
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.
195 *
196 * The caller is responsible for allocating and initialising the
197 * actual cache.
198 *
199 * Note that at present this code cannot be used by CODECs with
200 * volatile registers.
201 */
202 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
203 struct regmap *regmap)
204 {
205 int ret;
206
207 /* Device has made its own regmap arrangements */
208 if (!regmap)
209 codec->control_data = dev_get_regmap(codec->dev, NULL);
210 else
211 codec->control_data = regmap;
212
213 if (IS_ERR(codec->control_data))
214 return PTR_ERR(codec->control_data);
215
216 codec->write = hw_write;
217 codec->read = hw_read;
218
219 ret = regmap_get_val_bytes(codec->control_data);
220 /* Errors are legitimate for non-integer byte
221 * multiples */
222 if (ret > 0)
223 codec->val_bytes = ret;
224
225 codec->using_regmap = true;
226
227 return 0;
228 }
229 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
230 #else
231 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
232 struct regmap *regmap)
233 {
234 return -ENOTSUPP;
235 }
236 EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
237 #endif
This page took 0.099227 seconds and 5 git commands to generate.