Merge remote-tracking branch 'asoc/fix/wm8962' into asoc-linus
[sfrench/cifs-2.6.git] / sound / soc / soc-cache.c
1 /*
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
14 #include <sound/soc.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17
18 #include <trace/events/asoc.h>
19
20 static bool snd_soc_set_cache_val(void *base, unsigned int idx,
21                                   unsigned int val, unsigned int word_size)
22 {
23         switch (word_size) {
24         case 1: {
25                 u8 *cache = base;
26                 if (cache[idx] == val)
27                         return true;
28                 cache[idx] = val;
29                 break;
30         }
31         case 2: {
32                 u16 *cache = base;
33                 if (cache[idx] == val)
34                         return true;
35                 cache[idx] = val;
36                 break;
37         }
38         default:
39                 BUG();
40         }
41         return false;
42 }
43
44 static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
45                 unsigned int word_size)
46 {
47         if (!base)
48                 return -1;
49
50         switch (word_size) {
51         case 1: {
52                 const u8 *cache = base;
53                 return cache[idx];
54         }
55         case 2: {
56                 const u16 *cache = base;
57                 return cache[idx];
58         }
59         default:
60                 BUG();
61         }
62         /* unreachable */
63         return -1;
64 }
65
66 int snd_soc_cache_init(struct snd_soc_codec *codec)
67 {
68         const struct snd_soc_codec_driver *codec_drv = codec->driver;
69         size_t reg_size;
70
71         reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
72
73         mutex_init(&codec->cache_rw_mutex);
74
75         dev_dbg(codec->dev, "ASoC: Initializing cache for %s codec\n",
76                                 codec->name);
77
78         if (codec_drv->reg_cache_default)
79                 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
80                                            reg_size, GFP_KERNEL);
81         else
82                 codec->reg_cache = kzalloc(reg_size, GFP_KERNEL);
83         if (!codec->reg_cache)
84                 return -ENOMEM;
85
86         return 0;
87 }
88
89 /*
90  * NOTE: keep in mind that this function might be called
91  * multiple times.
92  */
93 int snd_soc_cache_exit(struct snd_soc_codec *codec)
94 {
95         dev_dbg(codec->dev, "ASoC: Destroying cache for %s codec\n",
96                         codec->name);
97         if (!codec->reg_cache)
98                 return 0;
99         kfree(codec->reg_cache);
100         codec->reg_cache = NULL;
101         return 0;
102 }
103
104 /**
105  * snd_soc_cache_read: Fetch the value of a given register from the cache.
106  *
107  * @codec: CODEC to configure.
108  * @reg: The register index.
109  * @value: The value to be returned.
110  */
111 int snd_soc_cache_read(struct snd_soc_codec *codec,
112                        unsigned int reg, unsigned int *value)
113 {
114         if (!value)
115                 return -EINVAL;
116
117         mutex_lock(&codec->cache_rw_mutex);
118         *value = snd_soc_get_cache_val(codec->reg_cache, reg,
119                                        codec->driver->reg_word_size);
120         mutex_unlock(&codec->cache_rw_mutex);
121
122         return 0;
123 }
124 EXPORT_SYMBOL_GPL(snd_soc_cache_read);
125
126 /**
127  * snd_soc_cache_write: Set the value of a given register in the cache.
128  *
129  * @codec: CODEC to configure.
130  * @reg: The register index.
131  * @value: The new register value.
132  */
133 int snd_soc_cache_write(struct snd_soc_codec *codec,
134                         unsigned int reg, unsigned int value)
135 {
136         mutex_lock(&codec->cache_rw_mutex);
137         snd_soc_set_cache_val(codec->reg_cache, reg, value,
138                               codec->driver->reg_word_size);
139         mutex_unlock(&codec->cache_rw_mutex);
140
141         return 0;
142 }
143 EXPORT_SYMBOL_GPL(snd_soc_cache_write);
144
145 static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
146 {
147         int i;
148         int ret;
149         const struct snd_soc_codec_driver *codec_drv;
150         unsigned int val;
151
152         codec_drv = codec->driver;
153         for (i = 0; i < codec_drv->reg_cache_size; ++i) {
154                 ret = snd_soc_cache_read(codec, i, &val);
155                 if (ret)
156                         return ret;
157                 if (codec_drv->reg_cache_default)
158                         if (snd_soc_get_cache_val(codec_drv->reg_cache_default,
159                                                   i, codec_drv->reg_word_size) == val)
160                                 continue;
161
162                 WARN_ON(!snd_soc_codec_writable_register(codec, i));
163
164                 ret = snd_soc_write(codec, i, val);
165                 if (ret)
166                         return ret;
167                 dev_dbg(codec->dev, "ASoC: Synced register %#x, value = %#x\n",
168                         i, val);
169         }
170         return 0;
171 }
172
173 /**
174  * snd_soc_cache_sync: Sync the register cache with the hardware.
175  *
176  * @codec: CODEC to configure.
177  *
178  * Any registers that should not be synced should be marked as
179  * volatile.  In general drivers can choose not to use the provided
180  * syncing functionality if they so require.
181  */
182 int snd_soc_cache_sync(struct snd_soc_codec *codec)
183 {
184         const char *name = "flat";
185         int ret;
186
187         if (!codec->cache_sync)
188                 return 0;
189
190         dev_dbg(codec->dev, "ASoC: Syncing cache for %s codec\n",
191                 codec->name);
192         trace_snd_soc_cache_sync(codec, name, "start");
193         ret = snd_soc_flat_cache_sync(codec);
194         if (!ret)
195                 codec->cache_sync = 0;
196         trace_snd_soc_cache_sync(codec, name, "end");
197         return ret;
198 }
199 EXPORT_SYMBOL_GPL(snd_soc_cache_sync);