blob: a7f83c0c62cec55de9e3cae440a0d3ce053499e0 [file] [log] [blame]
Mark Brown17a52fd2009-07-05 17:24:50 +01001/*
2 * soc-cache.c -- ASoC register cache helpers
3 *
4 * Copyright 2009 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
Mark Brown7084a422009-07-10 22:24:27 +010014#include <linux/i2c.h>
Mark Brown27ded042009-07-10 23:28:16 +010015#include <linux/spi/spi.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010016#include <sound/soc.h>
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +000017#include <linux/bitmap.h>
Dimitris Papastamosa7f387d2010-11-11 10:04:59 +000018#include <linux/rbtree.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040019#include <linux/export.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010020
Dimitris Papastamosc358e642011-01-21 15:29:02 +000021#include <trace/events/asoc.h>
22
Dimitris Papastamos1321e882011-01-11 11:29:49 +000023static bool snd_soc_set_cache_val(void *base, unsigned int idx,
24 unsigned int val, unsigned int word_size)
25{
26 switch (word_size) {
27 case 1: {
28 u8 *cache = base;
29 if (cache[idx] == val)
30 return true;
31 cache[idx] = val;
32 break;
33 }
34 case 2: {
35 u16 *cache = base;
36 if (cache[idx] == val)
37 return true;
38 cache[idx] = val;
39 break;
40 }
41 default:
42 BUG();
43 }
44 return false;
45}
46
47static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
48 unsigned int word_size)
49{
Mark Brownfd137e22011-06-06 11:26:15 +010050 if (!base)
51 return -1;
52
Dimitris Papastamos1321e882011-01-11 11:29:49 +000053 switch (word_size) {
54 case 1: {
55 const u8 *cache = base;
56 return cache[idx];
57 }
58 case 2: {
59 const u16 *cache = base;
60 return cache[idx];
61 }
62 default:
63 BUG();
64 }
65 /* unreachable */
66 return -1;
67}
68
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000069static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
70{
71 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +000072 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +000073 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000074 unsigned int val;
75
76 codec_drv = codec->driver;
77 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +000078 ret = snd_soc_cache_read(codec, i, &val);
79 if (ret)
80 return ret;
Lars-Peter Clausenb012aa612013-08-31 20:31:13 +020081 if (codec_drv->reg_cache_default)
82 if (snd_soc_get_cache_val(codec_drv->reg_cache_default,
Dimitris Papastamos1321e882011-01-11 11:29:49 +000083 i, codec_drv->reg_word_size) == val)
84 continue;
Lars-Peter Clausen6c5b7562011-08-27 18:24:13 +020085
86 WARN_ON(!snd_soc_codec_writable_register(codec, i));
87
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +000088 ret = snd_soc_write(codec, i, val);
89 if (ret)
90 return ret;
Liam Girdwood204b62c2012-11-19 14:39:13 +000091 dev_dbg(codec->dev, "ASoC: Synced register %#x, value = %#x\n",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +000092 i, val);
93 }
94 return 0;
95}
96
97static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
98 unsigned int reg, unsigned int value)
99{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000100 snd_soc_set_cache_val(codec->reg_cache, reg, value,
101 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000102 return 0;
103}
104
105static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
106 unsigned int reg, unsigned int *value)
107{
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000108 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
109 codec->driver->reg_word_size);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000110 return 0;
111}
112
113static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
114{
115 if (!codec->reg_cache)
116 return 0;
117 kfree(codec->reg_cache);
118 codec->reg_cache = NULL;
119 return 0;
120}
121
122static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
123{
Lars-Peter Clausenb012aa612013-08-31 20:31:13 +0200124 const struct snd_soc_codec_driver *codec_drv = codec->driver;
125
126 if (codec_drv->reg_cache_default)
127 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000128 codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000129 else
Dimitris Papastamosaea170a2011-01-12 10:38:58 +0000130 codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000131 if (!codec->reg_cache)
132 return -ENOMEM;
133
134 return 0;
135}
136
137/* an array of all supported compression types */
138static const struct snd_soc_cache_ops cache_types[] = {
Mark Brownbe4fcdd2010-12-21 17:09:48 +0000139 /* Flat *must* be the first entry for fallback */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000140 {
Dimitris Papastamosdf0701b2010-11-29 10:54:28 +0000141 .id = SND_SOC_FLAT_COMPRESSION,
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000142 .name = "flat",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000143 .init = snd_soc_flat_cache_init,
144 .exit = snd_soc_flat_cache_exit,
145 .read = snd_soc_flat_cache_read,
146 .write = snd_soc_flat_cache_write,
147 .sync = snd_soc_flat_cache_sync
Dimitris Papastamoscc28fb82010-11-11 10:04:58 +0000148 },
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000149};
150
151int snd_soc_cache_init(struct snd_soc_codec *codec)
152{
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
Dimitris Papastamos23bbce32010-12-02 14:53:01 +0000156 if (cache_types[i].id == codec->compress_type)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000157 break;
Mark Brownbe4fcdd2010-12-21 17:09:48 +0000158
159 /* Fall back to flat compression */
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000160 if (i == ARRAY_SIZE(cache_types)) {
Liam Girdwood204b62c2012-11-19 14:39:13 +0000161 dev_warn(codec->dev, "ASoC: Could not match compress type: %d\n",
Mark Brownbe4fcdd2010-12-21 17:09:48 +0000162 codec->compress_type);
163 i = 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000164 }
165
166 mutex_init(&codec->cache_rw_mutex);
167 codec->cache_ops = &cache_types[i];
168
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000169 if (codec->cache_ops->init) {
170 if (codec->cache_ops->name)
Liam Girdwood204b62c2012-11-19 14:39:13 +0000171 dev_dbg(codec->dev, "ASoC: Initializing %s cache for %s codec\n",
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000172 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000173 return codec->cache_ops->init(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000174 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +0000175 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000176}
177
178/*
179 * NOTE: keep in mind that this function might be called
180 * multiple times.
181 */
182int snd_soc_cache_exit(struct snd_soc_codec *codec)
183{
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000184 if (codec->cache_ops && codec->cache_ops->exit) {
185 if (codec->cache_ops->name)
Liam Girdwood204b62c2012-11-19 14:39:13 +0000186 dev_dbg(codec->dev, "ASoC: Destroying %s cache for %s codec\n",
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000187 codec->cache_ops->name, codec->name);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000188 return codec->cache_ops->exit(codec);
Dimitris Papastamos0d735ea2010-12-06 09:51:57 +0000189 }
Dimitris Papastamosacd61452011-03-22 10:48:49 +0000190 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000191}
192
193/**
194 * snd_soc_cache_read: Fetch the value of a given register from the cache.
195 *
196 * @codec: CODEC to configure.
197 * @reg: The register index.
198 * @value: The value to be returned.
199 */
200int snd_soc_cache_read(struct snd_soc_codec *codec,
201 unsigned int reg, unsigned int *value)
202{
203 int ret;
204
205 mutex_lock(&codec->cache_rw_mutex);
206
207 if (value && codec->cache_ops && codec->cache_ops->read) {
208 ret = codec->cache_ops->read(codec, reg, value);
209 mutex_unlock(&codec->cache_rw_mutex);
210 return ret;
211 }
212
213 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +0000214 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000215}
216EXPORT_SYMBOL_GPL(snd_soc_cache_read);
217
218/**
219 * snd_soc_cache_write: Set the value of a given register in the cache.
220 *
221 * @codec: CODEC to configure.
222 * @reg: The register index.
223 * @value: The new register value.
224 */
225int snd_soc_cache_write(struct snd_soc_codec *codec,
226 unsigned int reg, unsigned int value)
227{
228 int ret;
229
230 mutex_lock(&codec->cache_rw_mutex);
231
232 if (codec->cache_ops && codec->cache_ops->write) {
233 ret = codec->cache_ops->write(codec, reg, value);
234 mutex_unlock(&codec->cache_rw_mutex);
235 return ret;
236 }
237
238 mutex_unlock(&codec->cache_rw_mutex);
Dimitris Papastamosacd61452011-03-22 10:48:49 +0000239 return -ENOSYS;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000240}
241EXPORT_SYMBOL_GPL(snd_soc_cache_write);
242
243/**
244 * snd_soc_cache_sync: Sync the register cache with the hardware.
245 *
246 * @codec: CODEC to configure.
247 *
248 * Any registers that should not be synced should be marked as
249 * volatile. In general drivers can choose not to use the provided
250 * syncing functionality if they so require.
251 */
252int snd_soc_cache_sync(struct snd_soc_codec *codec)
253{
254 int ret;
Dimitris Papastamosc358e642011-01-21 15:29:02 +0000255 const char *name;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000256
257 if (!codec->cache_sync) {
258 return 0;
259 }
260
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300261 if (!codec->cache_ops || !codec->cache_ops->sync)
Dimitris Papastamosacd61452011-03-22 10:48:49 +0000262 return -ENOSYS;
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300263
Dimitris Papastamosc358e642011-01-21 15:29:02 +0000264 if (codec->cache_ops->name)
265 name = codec->cache_ops->name;
266 else
267 name = "unknown";
268
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300269 if (codec->cache_ops->name)
Liam Girdwood204b62c2012-11-19 14:39:13 +0000270 dev_dbg(codec->dev, "ASoC: Syncing %s cache for %s codec\n",
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300271 codec->cache_ops->name, codec->name);
272 trace_snd_soc_cache_sync(codec, name, "start");
273 ret = codec->cache_ops->sync(codec);
274 if (!ret)
275 codec->cache_sync = 0;
276 trace_snd_soc_cache_sync(codec, name, "end");
277 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000278}
279EXPORT_SYMBOL_GPL(snd_soc_cache_sync);