2 * soc-cache.c -- ASoC register cache helpers
4 * Copyright 2009 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 <sound/soc.h>
17 #include <linux/bitmap.h>
18 #include <linux/rbtree.h>
19 #include <linux/export.h>
21 #include <trace/events/asoc.h>
23 static bool snd_soc_set_cache_val(void *base
, unsigned int idx
,
24 unsigned int val
, unsigned int word_size
)
29 if (cache
[idx
] == val
)
36 if (cache
[idx
] == val
)
47 static unsigned int snd_soc_get_cache_val(const void *base
, unsigned int idx
,
48 unsigned int word_size
)
55 const u8
*cache
= base
;
59 const u16
*cache
= base
;
69 static int snd_soc_flat_cache_sync(struct snd_soc_codec
*codec
)
73 const struct snd_soc_codec_driver
*codec_drv
;
76 codec_drv
= codec
->driver
;
77 for (i
= 0; i
< codec_drv
->reg_cache_size
; ++i
) {
78 ret
= snd_soc_cache_read(codec
, i
, &val
);
81 if (codec
->reg_def_copy
)
82 if (snd_soc_get_cache_val(codec
->reg_def_copy
,
83 i
, codec_drv
->reg_word_size
) == val
)
86 WARN_ON(!snd_soc_codec_writable_register(codec
, i
));
88 ret
= snd_soc_write(codec
, i
, val
);
91 dev_dbg(codec
->dev
, "Synced register %#x, value = %#x\n",
97 static int snd_soc_flat_cache_write(struct snd_soc_codec
*codec
,
98 unsigned int reg
, unsigned int value
)
100 snd_soc_set_cache_val(codec
->reg_cache
, reg
, value
,
101 codec
->driver
->reg_word_size
);
105 static int snd_soc_flat_cache_read(struct snd_soc_codec
*codec
,
106 unsigned int reg
, unsigned int *value
)
108 *value
= snd_soc_get_cache_val(codec
->reg_cache
, reg
,
109 codec
->driver
->reg_word_size
);
113 static int snd_soc_flat_cache_exit(struct snd_soc_codec
*codec
)
115 if (!codec
->reg_cache
)
117 kfree(codec
->reg_cache
);
118 codec
->reg_cache
= NULL
;
122 static int snd_soc_flat_cache_init(struct snd_soc_codec
*codec
)
124 if (codec
->reg_def_copy
)
125 codec
->reg_cache
= kmemdup(codec
->reg_def_copy
,
126 codec
->reg_size
, GFP_KERNEL
);
128 codec
->reg_cache
= kzalloc(codec
->reg_size
, GFP_KERNEL
);
129 if (!codec
->reg_cache
)
135 /* an array of all supported compression types */
136 static const struct snd_soc_cache_ops cache_types
[] = {
137 /* Flat *must* be the first entry for fallback */
139 .id
= SND_SOC_FLAT_COMPRESSION
,
141 .init
= snd_soc_flat_cache_init
,
142 .exit
= snd_soc_flat_cache_exit
,
143 .read
= snd_soc_flat_cache_read
,
144 .write
= snd_soc_flat_cache_write
,
145 .sync
= snd_soc_flat_cache_sync
149 int snd_soc_cache_init(struct snd_soc_codec
*codec
)
153 for (i
= 0; i
< ARRAY_SIZE(cache_types
); ++i
)
154 if (cache_types
[i
].id
== codec
->compress_type
)
157 /* Fall back to flat compression */
158 if (i
== ARRAY_SIZE(cache_types
)) {
159 dev_warn(codec
->dev
, "Could not match compress type: %d\n",
160 codec
->compress_type
);
164 mutex_init(&codec
->cache_rw_mutex
);
165 codec
->cache_ops
= &cache_types
[i
];
167 if (codec
->cache_ops
->init
) {
168 if (codec
->cache_ops
->name
)
169 dev_dbg(codec
->dev
, "Initializing %s cache for %s codec\n",
170 codec
->cache_ops
->name
, codec
->name
);
171 return codec
->cache_ops
->init(codec
);
177 * NOTE: keep in mind that this function might be called
180 int snd_soc_cache_exit(struct snd_soc_codec
*codec
)
182 if (codec
->cache_ops
&& codec
->cache_ops
->exit
) {
183 if (codec
->cache_ops
->name
)
184 dev_dbg(codec
->dev
, "Destroying %s cache for %s codec\n",
185 codec
->cache_ops
->name
, codec
->name
);
186 return codec
->cache_ops
->exit(codec
);
192 * snd_soc_cache_read: Fetch the value of a given register from the cache.
194 * @codec: CODEC to configure.
195 * @reg: The register index.
196 * @value: The value to be returned.
198 int snd_soc_cache_read(struct snd_soc_codec
*codec
,
199 unsigned int reg
, unsigned int *value
)
203 mutex_lock(&codec
->cache_rw_mutex
);
205 if (value
&& codec
->cache_ops
&& codec
->cache_ops
->read
) {
206 ret
= codec
->cache_ops
->read(codec
, reg
, value
);
207 mutex_unlock(&codec
->cache_rw_mutex
);
211 mutex_unlock(&codec
->cache_rw_mutex
);
214 EXPORT_SYMBOL_GPL(snd_soc_cache_read
);
217 * snd_soc_cache_write: Set the value of a given register in the cache.
219 * @codec: CODEC to configure.
220 * @reg: The register index.
221 * @value: The new register value.
223 int snd_soc_cache_write(struct snd_soc_codec
*codec
,
224 unsigned int reg
, unsigned int value
)
228 mutex_lock(&codec
->cache_rw_mutex
);
230 if (codec
->cache_ops
&& codec
->cache_ops
->write
) {
231 ret
= codec
->cache_ops
->write(codec
, reg
, value
);
232 mutex_unlock(&codec
->cache_rw_mutex
);
236 mutex_unlock(&codec
->cache_rw_mutex
);
239 EXPORT_SYMBOL_GPL(snd_soc_cache_write
);
242 * snd_soc_cache_sync: Sync the register cache with the hardware.
244 * @codec: CODEC to configure.
246 * Any registers that should not be synced should be marked as
247 * volatile. In general drivers can choose not to use the provided
248 * syncing functionality if they so require.
250 int snd_soc_cache_sync(struct snd_soc_codec
*codec
)
255 if (!codec
->cache_sync
) {
259 if (!codec
->cache_ops
|| !codec
->cache_ops
->sync
)
262 if (codec
->cache_ops
->name
)
263 name
= codec
->cache_ops
->name
;
267 if (codec
->cache_ops
->name
)
268 dev_dbg(codec
->dev
, "Syncing %s cache for %s codec\n",
269 codec
->cache_ops
->name
, codec
->name
);
270 trace_snd_soc_cache_sync(codec
, name
, "start");
271 ret
= codec
->cache_ops
->sync(codec
);
273 codec
->cache_sync
= 0;
274 trace_snd_soc_cache_sync(codec
, name
, "end");
277 EXPORT_SYMBOL_GPL(snd_soc_cache_sync
);
279 static int snd_soc_get_reg_access_index(struct snd_soc_codec
*codec
,
282 const struct snd_soc_codec_driver
*codec_drv
;
283 unsigned int min
, max
, index
;
285 codec_drv
= codec
->driver
;
287 max
= codec_drv
->reg_access_size
- 1;
289 index
= (min
+ max
) / 2;
290 if (codec_drv
->reg_access_default
[index
].reg
== reg
)
292 if (codec_drv
->reg_access_default
[index
].reg
< reg
)
296 } while (min
<= max
);
300 int snd_soc_default_volatile_register(struct snd_soc_codec
*codec
,
305 if (reg
>= codec
->driver
->reg_cache_size
)
307 index
= snd_soc_get_reg_access_index(codec
, reg
);
310 return codec
->driver
->reg_access_default
[index
].vol
;
312 EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register
);
314 int snd_soc_default_readable_register(struct snd_soc_codec
*codec
,
319 if (reg
>= codec
->driver
->reg_cache_size
)
321 index
= snd_soc_get_reg_access_index(codec
, reg
);
324 return codec
->driver
->reg_access_default
[index
].read
;
326 EXPORT_SYMBOL_GPL(snd_soc_default_readable_register
);
328 int snd_soc_default_writable_register(struct snd_soc_codec
*codec
,
333 if (reg
>= codec
->driver
->reg_cache_size
)
335 index
= snd_soc_get_reg_access_index(codec
, reg
);
338 return codec
->driver
->reg_access_default
[index
].write
;
340 EXPORT_SYMBOL_GPL(snd_soc_default_writable_register
);