diff options
Diffstat (limited to 'sound/soc/soc-io.c')
-rw-r--r-- | sound/soc/soc-io.c | 357 |
1 files changed, 50 insertions, 307 deletions
diff --git a/sound/soc/soc-io.c b/sound/soc/soc-io.c index a62f7dd4ba96..dd89933e2c72 100644 --- a/sound/soc/soc-io.c +++ b/sound/soc/soc-io.c | |||
@@ -13,26 +13,14 @@ | |||
13 | 13 | ||
14 | #include <linux/i2c.h> | 14 | #include <linux/i2c.h> |
15 | #include <linux/spi/spi.h> | 15 | #include <linux/spi/spi.h> |
16 | #include <linux/regmap.h> | ||
16 | #include <sound/soc.h> | 17 | #include <sound/soc.h> |
17 | 18 | ||
18 | #include <trace/events/asoc.h> | 19 | #include <trace/events/asoc.h> |
19 | 20 | ||
20 | #ifdef CONFIG_SPI_MASTER | 21 | #ifdef CONFIG_REGMAP |
21 | static int do_spi_write(void *control, const char *data, int len) | 22 | static int hw_write(struct snd_soc_codec *codec, unsigned int reg, |
22 | { | 23 | unsigned int value) |
23 | struct spi_device *spi = control; | ||
24 | int ret; | ||
25 | |||
26 | ret = spi_write(spi, data, len); | ||
27 | if (ret < 0) | ||
28 | return ret; | ||
29 | |||
30 | return len; | ||
31 | } | ||
32 | #endif | ||
33 | |||
34 | static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg, | ||
35 | unsigned int value, const void *data, int len) | ||
36 | { | 24 | { |
37 | int ret; | 25 | int ret; |
38 | 26 | ||
@@ -49,13 +37,7 @@ static int do_hw_write(struct snd_soc_codec *codec, unsigned int reg, | |||
49 | return 0; | 37 | return 0; |
50 | } | 38 | } |
51 | 39 | ||
52 | ret = codec->hw_write(codec->control_data, data, len); | 40 | return regmap_write(codec->control_data, reg, value); |
53 | if (ret == len) | ||
54 | return 0; | ||
55 | if (ret < 0) | ||
56 | return ret; | ||
57 | else | ||
58 | return -EIO; | ||
59 | } | 41 | } |
60 | 42 | ||
61 | static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) | 43 | static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) |
@@ -69,8 +51,11 @@ static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) | |||
69 | if (codec->cache_only) | 51 | if (codec->cache_only) |
70 | return -1; | 52 | return -1; |
71 | 53 | ||
72 | BUG_ON(!codec->hw_read); | 54 | ret = regmap_read(codec->control_data, reg, &val); |
73 | return codec->hw_read(codec, reg); | 55 | if (ret == 0) |
56 | return val; | ||
57 | else | ||
58 | return -1; | ||
74 | } | 59 | } |
75 | 60 | ||
76 | ret = snd_soc_cache_read(codec, reg, &val); | 61 | ret = snd_soc_cache_read(codec, reg, &val); |
@@ -79,202 +64,18 @@ static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg) | |||
79 | return val; | 64 | return val; |
80 | } | 65 | } |
81 | 66 | ||
82 | static int snd_soc_4_12_write(struct snd_soc_codec *codec, unsigned int reg, | ||
83 | unsigned int value) | ||
84 | { | ||
85 | u16 data; | ||
86 | |||
87 | data = cpu_to_be16((reg << 12) | (value & 0xffffff)); | ||
88 | |||
89 | return do_hw_write(codec, reg, value, &data, 2); | ||
90 | } | ||
91 | |||
92 | static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg, | ||
93 | unsigned int value) | ||
94 | { | ||
95 | u16 data; | ||
96 | |||
97 | data = cpu_to_be16((reg << 9) | (value & 0x1ff)); | ||
98 | |||
99 | return do_hw_write(codec, reg, value, &data, 2); | ||
100 | } | ||
101 | |||
102 | static int snd_soc_8_8_write(struct snd_soc_codec *codec, unsigned int reg, | ||
103 | unsigned int value) | ||
104 | { | ||
105 | u8 data[2]; | ||
106 | |||
107 | reg &= 0xff; | ||
108 | data[0] = reg; | ||
109 | data[1] = value & 0xff; | ||
110 | |||
111 | return do_hw_write(codec, reg, value, data, 2); | ||
112 | } | ||
113 | |||
114 | static int snd_soc_8_16_write(struct snd_soc_codec *codec, unsigned int reg, | ||
115 | unsigned int value) | ||
116 | { | ||
117 | u8 data[3]; | ||
118 | u16 val = cpu_to_be16(value); | ||
119 | |||
120 | data[0] = reg; | ||
121 | memcpy(&data[1], &val, sizeof(val)); | ||
122 | |||
123 | return do_hw_write(codec, reg, value, data, 3); | ||
124 | } | ||
125 | |||
126 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
127 | static unsigned int do_i2c_read(struct snd_soc_codec *codec, | ||
128 | void *reg, int reglen, | ||
129 | void *data, int datalen) | ||
130 | { | ||
131 | struct i2c_msg xfer[2]; | ||
132 | int ret; | ||
133 | struct i2c_client *client = codec->control_data; | ||
134 | |||
135 | /* Write register */ | ||
136 | xfer[0].addr = client->addr; | ||
137 | xfer[0].flags = 0; | ||
138 | xfer[0].len = reglen; | ||
139 | xfer[0].buf = reg; | ||
140 | |||
141 | /* Read data */ | ||
142 | xfer[1].addr = client->addr; | ||
143 | xfer[1].flags = I2C_M_RD; | ||
144 | xfer[1].len = datalen; | ||
145 | xfer[1].buf = data; | ||
146 | |||
147 | ret = i2c_transfer(client->adapter, xfer, 2); | ||
148 | if (ret == 2) | ||
149 | return 0; | ||
150 | else if (ret < 0) | ||
151 | return ret; | ||
152 | else | ||
153 | return -EIO; | ||
154 | } | ||
155 | #endif | ||
156 | |||
157 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
158 | static unsigned int snd_soc_8_8_read_i2c(struct snd_soc_codec *codec, | ||
159 | unsigned int r) | ||
160 | { | ||
161 | u8 reg = r; | ||
162 | u8 data; | ||
163 | int ret; | ||
164 | |||
165 | ret = do_i2c_read(codec, ®, 1, &data, 1); | ||
166 | if (ret < 0) | ||
167 | return 0; | ||
168 | return data; | ||
169 | } | ||
170 | #else | ||
171 | #define snd_soc_8_8_read_i2c NULL | ||
172 | #endif | ||
173 | |||
174 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
175 | static unsigned int snd_soc_8_16_read_i2c(struct snd_soc_codec *codec, | ||
176 | unsigned int r) | ||
177 | { | ||
178 | u8 reg = r; | ||
179 | u16 data; | ||
180 | int ret; | ||
181 | |||
182 | ret = do_i2c_read(codec, ®, 1, &data, 2); | ||
183 | if (ret < 0) | ||
184 | return 0; | ||
185 | return (data >> 8) | ((data & 0xff) << 8); | ||
186 | } | ||
187 | #else | ||
188 | #define snd_soc_8_16_read_i2c NULL | ||
189 | #endif | ||
190 | |||
191 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
192 | static unsigned int snd_soc_16_8_read_i2c(struct snd_soc_codec *codec, | ||
193 | unsigned int r) | ||
194 | { | ||
195 | u16 reg = r; | ||
196 | u8 data; | ||
197 | int ret; | ||
198 | |||
199 | ret = do_i2c_read(codec, ®, 2, &data, 1); | ||
200 | if (ret < 0) | ||
201 | return 0; | ||
202 | return data; | ||
203 | } | ||
204 | #else | ||
205 | #define snd_soc_16_8_read_i2c NULL | ||
206 | #endif | ||
207 | |||
208 | #if defined(CONFIG_SPI_MASTER) | ||
209 | static unsigned int snd_soc_16_8_read_spi(struct snd_soc_codec *codec, | ||
210 | unsigned int r) | ||
211 | { | ||
212 | struct spi_device *spi = codec->control_data; | ||
213 | |||
214 | const u16 reg = cpu_to_be16(r | 0x100); | ||
215 | u8 data; | ||
216 | int ret; | ||
217 | |||
218 | ret = spi_write_then_read(spi, ®, 2, &data, 1); | ||
219 | if (ret < 0) | ||
220 | return 0; | ||
221 | return data; | ||
222 | } | ||
223 | #else | ||
224 | #define snd_soc_16_8_read_spi NULL | ||
225 | #endif | ||
226 | |||
227 | static int snd_soc_16_8_write(struct snd_soc_codec *codec, unsigned int reg, | ||
228 | unsigned int value) | ||
229 | { | ||
230 | u8 data[3]; | ||
231 | u16 rval = cpu_to_be16(reg); | ||
232 | |||
233 | memcpy(data, &rval, sizeof(rval)); | ||
234 | data[2] = value; | ||
235 | |||
236 | return do_hw_write(codec, reg, value, data, 3); | ||
237 | } | ||
238 | |||
239 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
240 | static unsigned int snd_soc_16_16_read_i2c(struct snd_soc_codec *codec, | ||
241 | unsigned int r) | ||
242 | { | ||
243 | u16 reg = cpu_to_be16(r); | ||
244 | u16 data; | ||
245 | int ret; | ||
246 | |||
247 | ret = do_i2c_read(codec, ®, 2, &data, 2); | ||
248 | if (ret < 0) | ||
249 | return 0; | ||
250 | return be16_to_cpu(data); | ||
251 | } | ||
252 | #else | ||
253 | #define snd_soc_16_16_read_i2c NULL | ||
254 | #endif | ||
255 | |||
256 | static int snd_soc_16_16_write(struct snd_soc_codec *codec, unsigned int reg, | ||
257 | unsigned int value) | ||
258 | { | ||
259 | u16 data[2]; | ||
260 | |||
261 | data[0] = cpu_to_be16(reg); | ||
262 | data[1] = cpu_to_be16(value); | ||
263 | |||
264 | return do_hw_write(codec, reg, value, data, sizeof(data)); | ||
265 | } | ||
266 | |||
267 | /* Primitive bulk write support for soc-cache. The data pointed to by | 67 | /* Primitive bulk write support for soc-cache. The data pointed to by |
268 | * `data' needs to already be in the form the hardware expects | 68 | * `data' needs to already be in the form the hardware expects. Any |
269 | * including any leading register specific data. Any data written | 69 | * data written through this function will not go through the cache as |
270 | * through this function will not go through the cache as it only | 70 | * it only handles writing to volatile or out of bounds registers. |
271 | * handles writing to volatile or out of bounds registers. | 71 | * |
72 | * This is currently only supported for devices using the regmap API | ||
73 | * wrappers. | ||
272 | */ | 74 | */ |
273 | static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int reg, | 75 | static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, |
76 | unsigned int reg, | ||
274 | const void *data, size_t len) | 77 | const void *data, size_t len) |
275 | { | 78 | { |
276 | int ret; | ||
277 | |||
278 | /* To ensure that we don't get out of sync with the cache, check | 79 | /* To ensure that we don't get out of sync with the cache, check |
279 | * whether the base register is volatile or if we've directly asked | 80 | * whether the base register is volatile or if we've directly asked |
280 | * to bypass the cache. Out of bounds registers are considered | 81 | * to bypass the cache. Out of bounds registers are considered |
@@ -285,68 +86,9 @@ static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec, unsigned int r | |||
285 | && reg < codec->driver->reg_cache_size) | 86 | && reg < codec->driver->reg_cache_size) |
286 | return -EINVAL; | 87 | return -EINVAL; |
287 | 88 | ||
288 | switch (codec->control_type) { | 89 | return regmap_raw_write(codec->control_data, reg, data, len); |
289 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | ||
290 | case SND_SOC_I2C: | ||
291 | ret = i2c_master_send(to_i2c_client(codec->dev), data, len); | ||
292 | break; | ||
293 | #endif | ||
294 | #if defined(CONFIG_SPI_MASTER) | ||
295 | case SND_SOC_SPI: | ||
296 | ret = spi_write(to_spi_device(codec->dev), data, len); | ||
297 | break; | ||
298 | #endif | ||
299 | default: | ||
300 | BUG(); | ||
301 | } | ||
302 | |||
303 | if (ret == len) | ||
304 | return 0; | ||
305 | if (ret < 0) | ||
306 | return ret; | ||
307 | else | ||
308 | return -EIO; | ||
309 | } | 90 | } |
310 | 91 | ||
311 | static struct { | ||
312 | int addr_bits; | ||
313 | int data_bits; | ||
314 | int (*write)(struct snd_soc_codec *codec, unsigned int, unsigned int); | ||
315 | unsigned int (*read)(struct snd_soc_codec *, unsigned int); | ||
316 | unsigned int (*i2c_read)(struct snd_soc_codec *, unsigned int); | ||
317 | unsigned int (*spi_read)(struct snd_soc_codec *, unsigned int); | ||
318 | } io_types[] = { | ||
319 | { | ||
320 | .addr_bits = 4, .data_bits = 12, | ||
321 | .write = snd_soc_4_12_write, | ||
322 | }, | ||
323 | { | ||
324 | .addr_bits = 7, .data_bits = 9, | ||
325 | .write = snd_soc_7_9_write, | ||
326 | }, | ||
327 | { | ||
328 | .addr_bits = 8, .data_bits = 8, | ||
329 | .write = snd_soc_8_8_write, | ||
330 | .i2c_read = snd_soc_8_8_read_i2c, | ||
331 | }, | ||
332 | { | ||
333 | .addr_bits = 8, .data_bits = 16, | ||
334 | .write = snd_soc_8_16_write, | ||
335 | .i2c_read = snd_soc_8_16_read_i2c, | ||
336 | }, | ||
337 | { | ||
338 | .addr_bits = 16, .data_bits = 8, | ||
339 | .write = snd_soc_16_8_write, | ||
340 | .i2c_read = snd_soc_16_8_read_i2c, | ||
341 | .spi_read = snd_soc_16_8_read_spi, | ||
342 | }, | ||
343 | { | ||
344 | .addr_bits = 16, .data_bits = 16, | ||
345 | .write = snd_soc_16_16_write, | ||
346 | .i2c_read = snd_soc_16_16_read_i2c, | ||
347 | }, | ||
348 | }; | ||
349 | |||
350 | /** | 92 | /** |
351 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. | 93 | * snd_soc_codec_set_cache_io: Set up standard I/O functions. |
352 | * | 94 | * |
@@ -370,50 +112,51 @@ int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, | |||
370 | int addr_bits, int data_bits, | 112 | int addr_bits, int data_bits, |
371 | enum snd_soc_control_type control) | 113 | enum snd_soc_control_type control) |
372 | { | 114 | { |
373 | int i; | 115 | struct regmap_config config; |
374 | |||
375 | for (i = 0; i < ARRAY_SIZE(io_types); i++) | ||
376 | if (io_types[i].addr_bits == addr_bits && | ||
377 | io_types[i].data_bits == data_bits) | ||
378 | break; | ||
379 | if (i == ARRAY_SIZE(io_types)) { | ||
380 | printk(KERN_ERR | ||
381 | "No I/O functions for %d bit address %d bit data\n", | ||
382 | addr_bits, data_bits); | ||
383 | return -EINVAL; | ||
384 | } | ||
385 | 116 | ||
386 | codec->write = io_types[i].write; | 117 | memset(&config, 0, sizeof(config)); |
118 | codec->write = hw_write; | ||
387 | codec->read = hw_read; | 119 | codec->read = hw_read; |
388 | codec->bulk_write_raw = snd_soc_hw_bulk_write_raw; | 120 | codec->bulk_write_raw = snd_soc_hw_bulk_write_raw; |
389 | 121 | ||
122 | config.reg_bits = addr_bits; | ||
123 | config.val_bits = data_bits; | ||
124 | |||
390 | switch (control) { | 125 | switch (control) { |
126 | #if defined(CONFIG_REGMAP_I2C) || defined(CONFIG_REGMAP_I2C_MODULE) | ||
391 | case SND_SOC_I2C: | 127 | case SND_SOC_I2C: |
392 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) | 128 | codec->control_data = regmap_init_i2c(to_i2c_client(codec->dev), |
393 | codec->hw_write = (hw_write_t)i2c_master_send; | 129 | &config); |
394 | #endif | ||
395 | if (io_types[i].i2c_read) | ||
396 | codec->hw_read = io_types[i].i2c_read; | ||
397 | |||
398 | codec->control_data = container_of(codec->dev, | ||
399 | struct i2c_client, | ||
400 | dev); | ||
401 | break; | 130 | break; |
131 | #endif | ||
402 | 132 | ||
133 | #if defined(CONFIG_REGMAP_SPI) || defined(CONFIG_REGMAP_SPI_MODULE) | ||
403 | case SND_SOC_SPI: | 134 | case SND_SOC_SPI: |
404 | #ifdef CONFIG_SPI_MASTER | 135 | codec->control_data = regmap_init_spi(to_spi_device(codec->dev), |
405 | codec->hw_write = do_spi_write; | 136 | &config); |
137 | break; | ||
406 | #endif | 138 | #endif |
407 | if (io_types[i].spi_read) | ||
408 | codec->hw_read = io_types[i].spi_read; | ||
409 | 139 | ||
410 | codec->control_data = container_of(codec->dev, | 140 | case SND_SOC_REGMAP: |
411 | struct spi_device, | 141 | /* Device has made its own regmap arrangements */ |
412 | dev); | ||
413 | break; | 142 | break; |
143 | |||
144 | default: | ||
145 | return -EINVAL; | ||
414 | } | 146 | } |
415 | 147 | ||
148 | if (IS_ERR(codec->control_data)) | ||
149 | return PTR_ERR(codec->control_data); | ||
150 | |||
416 | return 0; | 151 | return 0; |
417 | } | 152 | } |
418 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | 153 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); |
419 | 154 | #else | |
155 | int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec, | ||
156 | int addr_bits, int data_bits, | ||
157 | enum snd_soc_control_type control) | ||
158 | { | ||
159 | return -ENOTSUPP; | ||
160 | } | ||
161 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io); | ||
162 | #endif | ||