blob: a9f82b5aba9d0387a1f6952ae264aa1b1bb85982 [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
14#include <sound/soc.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040015#include <linux/export.h>
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020016#include <linux/slab.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010017
Dimitris Papastamosc358e642011-01-21 15:29:02 +000018#include <trace/events/asoc.h>
19
Dimitris Papastamos1321e882011-01-11 11:29:49 +000020static 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:
Takashi Iwaia6ed0602013-11-06 11:07:19 +010039 WARN(1, "Invalid word_size %d\n", word_size);
40 break;
Dimitris Papastamos1321e882011-01-11 11:29:49 +000041 }
42 return false;
43}
44
45static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
46 unsigned int word_size)
47{
Mark Brownfd137e22011-06-06 11:26:15 +010048 if (!base)
49 return -1;
50
Dimitris Papastamos1321e882011-01-11 11:29:49 +000051 switch (word_size) {
52 case 1: {
53 const u8 *cache = base;
54 return cache[idx];
55 }
56 case 2: {
57 const u16 *cache = base;
58 return cache[idx];
59 }
60 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +010061 WARN(1, "Invalid word_size %d\n", word_size);
62 break;
Dimitris Papastamos1321e882011-01-11 11:29:49 +000063 }
64 /* unreachable */
65 return -1;
66}
67
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020068int snd_soc_cache_init(struct snd_soc_codec *codec)
69{
70 const struct snd_soc_codec_driver *codec_drv = codec->driver;
71 size_t reg_size;
72
73 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
74
Xiubo Lib59dce52014-05-19 16:32:09 +080075 if (!reg_size)
Mark Brownb5fc40d2014-06-02 16:08:21 +010076 return 0;
Xiubo Lib59dce52014-05-19 16:32:09 +080077
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020078 mutex_init(&codec->cache_rw_mutex);
79
80 dev_dbg(codec->dev, "ASoC: Initializing cache for %s codec\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +020081 codec->component.name);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020082
83 if (codec_drv->reg_cache_default)
84 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
85 reg_size, GFP_KERNEL);
86 else
87 codec->reg_cache = kzalloc(reg_size, GFP_KERNEL);
88 if (!codec->reg_cache)
89 return -ENOMEM;
90
91 return 0;
92}
93
94/*
95 * NOTE: keep in mind that this function might be called
96 * multiple times.
97 */
98int snd_soc_cache_exit(struct snd_soc_codec *codec)
99{
100 dev_dbg(codec->dev, "ASoC: Destroying cache for %s codec\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200101 codec->component.name);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200102 kfree(codec->reg_cache);
103 codec->reg_cache = NULL;
104 return 0;
105}
106
107/**
108 * snd_soc_cache_read: Fetch the value of a given register from the cache.
109 *
110 * @codec: CODEC to configure.
111 * @reg: The register index.
112 * @value: The value to be returned.
113 */
114int snd_soc_cache_read(struct snd_soc_codec *codec,
115 unsigned int reg, unsigned int *value)
116{
117 if (!value)
118 return -EINVAL;
119
120 mutex_lock(&codec->cache_rw_mutex);
Xiubo Li931f27c2014-02-28 10:48:19 +0800121 if (!ZERO_OR_NULL_PTR(codec->reg_cache))
122 *value = snd_soc_get_cache_val(codec->reg_cache, reg,
123 codec->driver->reg_word_size);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200124 mutex_unlock(&codec->cache_rw_mutex);
125
126 return 0;
127}
128EXPORT_SYMBOL_GPL(snd_soc_cache_read);
129
130/**
131 * snd_soc_cache_write: Set the value of a given register in the cache.
132 *
133 * @codec: CODEC to configure.
134 * @reg: The register index.
135 * @value: The new register value.
136 */
137int snd_soc_cache_write(struct snd_soc_codec *codec,
138 unsigned int reg, unsigned int value)
139{
140 mutex_lock(&codec->cache_rw_mutex);
Xiubo Li931f27c2014-02-28 10:48:19 +0800141 if (!ZERO_OR_NULL_PTR(codec->reg_cache))
142 snd_soc_set_cache_val(codec->reg_cache, reg, value,
143 codec->driver->reg_word_size);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200144 mutex_unlock(&codec->cache_rw_mutex);
145
146 return 0;
147}
148EXPORT_SYMBOL_GPL(snd_soc_cache_write);
149
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000150static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
151{
152 int i;
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000153 int ret;
Mark Brown001ae4c2010-12-02 16:21:08 +0000154 const struct snd_soc_codec_driver *codec_drv;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000155 unsigned int val;
156
157 codec_drv = codec->driver;
158 for (i = 0; i < codec_drv->reg_cache_size; ++i) {
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000159 ret = snd_soc_cache_read(codec, i, &val);
160 if (ret)
161 return ret;
Lars-Peter Clausenb012aa612013-08-31 20:31:13 +0200162 if (codec_drv->reg_cache_default)
163 if (snd_soc_get_cache_val(codec_drv->reg_cache_default,
Dimitris Papastamos1321e882011-01-11 11:29:49 +0000164 i, codec_drv->reg_word_size) == val)
165 continue;
Lars-Peter Clausen6c5b7562011-08-27 18:24:13 +0200166
Dimitris Papastamos7a33d4c2010-11-29 10:24:54 +0000167 ret = snd_soc_write(codec, i, val);
168 if (ret)
169 return ret;
Liam Girdwood204b62c2012-11-19 14:39:13 +0000170 dev_dbg(codec->dev, "ASoC: Synced register %#x, value = %#x\n",
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000171 i, val);
172 }
173 return 0;
174}
175
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000176/**
177 * snd_soc_cache_sync: Sync the register cache with the hardware.
178 *
179 * @codec: CODEC to configure.
180 *
181 * Any registers that should not be synced should be marked as
182 * volatile. In general drivers can choose not to use the provided
183 * syncing functionality if they so require.
184 */
185int snd_soc_cache_sync(struct snd_soc_codec *codec)
186{
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200187 const char *name = "flat";
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000188 int ret;
189
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200190 if (!codec->cache_sync)
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000191 return 0;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000192
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200193 dev_dbg(codec->dev, "ASoC: Syncing cache for %s codec\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200194 codec->component.name);
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300195 trace_snd_soc_cache_sync(codec, name, "start");
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +0200196 ret = snd_soc_flat_cache_sync(codec);
Dan Carpenter46fdaa32011-02-07 22:01:41 +0300197 if (!ret)
198 codec->cache_sync = 0;
199 trace_snd_soc_cache_sync(codec, name, "end");
200 return ret;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +0000201}
202EXPORT_SYMBOL_GPL(snd_soc_cache_sync);