]>
Commit | Line | Data |
---|---|---|
5bef44f9 MB |
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> | |
be3ea3b9 | 16 | #include <linux/regmap.h> |
d81a6d71 | 17 | #include <linux/export.h> |
5bef44f9 MB |
18 | #include <sound/soc.h> |
19 | ||
20 | #include <trace/events/asoc.h> | |
21 | ||
96241c83 LPC |
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 | unsigned int snd_soc_write(struct snd_soc_codec *codec, | |
35 | unsigned int reg, 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 short 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 short 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 short 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 | ||
4835ff9a | 166 | #ifdef CONFIG_REGMAP |
be3ea3b9 MB |
167 | static int hw_write(struct snd_soc_codec *codec, unsigned int reg, |
168 | unsigned int value) | |
5bef44f9 | 169 | { |
be3ea3b9 | 170 | return regmap_write(codec->control_data, reg, value); |
5bef44f9 MB |
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 | ||
65725471 MB |
178 | ret = regmap_read(codec->control_data, reg, &val); |
179 | if (ret == 0) | |
180 | return val; | |
181 | else | |
5bef44f9 | 182 | return -1; |
5bef44f9 MB |
183 | } |
184 | ||
5bef44f9 MB |
185 | /** |
186 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. | |
187 | * | |
188 | * @codec: CODEC to configure. | |
092eba93 | 189 | * @map: Register map to write to |
5bef44f9 MB |
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, | |
092eba93 | 203 | struct regmap *regmap) |
5bef44f9 | 204 | { |
2b4bdee2 | 205 | int ret; |
5bef44f9 | 206 | |
092eba93 XL |
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 | ||
be3ea3b9 | 216 | codec->write = hw_write; |
5bef44f9 | 217 | codec->read = hw_read; |
5bef44f9 | 218 | |
092eba93 XL |
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; | |
5bef44f9 | 226 | |
092eba93 | 227 | return 0; |
5bef44f9 MB |
228 | } |
229 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | |
4835ff9a MB |
230 | #else |
231 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, | |
092eba93 | 232 | struct regmap *regmap) |
4835ff9a MB |
233 | { |
234 | return -ENOTSUPP; | |
235 | } | |
236 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | |
237 | #endif |