soc-cache.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <linux/i2c.h>
  14. #include <linux/spi/spi.h>
  15. #include <sound/soc.h>
  16. #include <linux/bitmap.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/export.h>
  19. #include <trace/events/asoc.h>
  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. static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
  44. unsigned int word_size)
  45. {
  46. if (!base)
  47. return -1;
  48. switch (word_size) {
  49. case 1: {
  50. const u8 *cache = base;
  51. return cache[idx];
  52. }
  53. case 2: {
  54. const u16 *cache = base;
  55. return cache[idx];
  56. }
  57. default:
  58. BUG();
  59. }
  60. /* unreachable */
  61. return -1;
  62. }
  63. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  64. {
  65. int i;
  66. int ret;
  67. const struct snd_soc_codec_driver *codec_drv;
  68. unsigned int val;
  69. codec_drv = codec->driver;
  70. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  71. ret = snd_soc_cache_read(codec, i, &val);
  72. if (ret)
  73. return ret;
  74. if (codec_drv->reg_cache_default)
  75. if (snd_soc_get_cache_val(codec_drv->reg_cache_default,
  76. i, codec_drv->reg_word_size) == val)
  77. continue;
  78. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  79. ret = snd_soc_write(codec, i, val);
  80. if (ret)
  81. return ret;
  82. dev_dbg(codec->dev, "ASoC: Synced register %#x, value = %#x\n",
  83. i, val);
  84. }
  85. return 0;
  86. }
  87. static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
  88. unsigned int reg, unsigned int value)
  89. {
  90. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  91. codec->driver->reg_word_size);
  92. return 0;
  93. }
  94. static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
  95. unsigned int reg, unsigned int *value)
  96. {
  97. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  98. codec->driver->reg_word_size);
  99. return 0;
  100. }
  101. static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
  102. {
  103. if (!codec->reg_cache)
  104. return 0;
  105. kfree(codec->reg_cache);
  106. codec->reg_cache = NULL;
  107. return 0;
  108. }
  109. static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
  110. {
  111. const struct snd_soc_codec_driver *codec_drv = codec->driver;
  112. size_t reg_size;
  113. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  114. if (codec_drv->reg_cache_default)
  115. codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
  116. reg_size, GFP_KERNEL);
  117. else
  118. codec->reg_cache = kzalloc(reg_size, GFP_KERNEL);
  119. if (!codec->reg_cache)
  120. return -ENOMEM;
  121. return 0;
  122. }
  123. /* an array of all supported compression types */
  124. static const struct snd_soc_cache_ops cache_types[] = {
  125. /* Flat *must* be the first entry for fallback */
  126. {
  127. .id = SND_SOC_FLAT_COMPRESSION,
  128. .name = "flat",
  129. .init = snd_soc_flat_cache_init,
  130. .exit = snd_soc_flat_cache_exit,
  131. .read = snd_soc_flat_cache_read,
  132. .write = snd_soc_flat_cache_write,
  133. .sync = snd_soc_flat_cache_sync
  134. },
  135. };
  136. int snd_soc_cache_init(struct snd_soc_codec *codec)
  137. {
  138. int i;
  139. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  140. if (cache_types[i].id == codec->compress_type)
  141. break;
  142. /* Fall back to flat compression */
  143. if (i == ARRAY_SIZE(cache_types)) {
  144. dev_warn(codec->dev, "ASoC: Could not match compress type: %d\n",
  145. codec->compress_type);
  146. i = 0;
  147. }
  148. mutex_init(&codec->cache_rw_mutex);
  149. codec->cache_ops = &cache_types[i];
  150. if (codec->cache_ops->init) {
  151. if (codec->cache_ops->name)
  152. dev_dbg(codec->dev, "ASoC: Initializing %s cache for %s codec\n",
  153. codec->cache_ops->name, codec->name);
  154. return codec->cache_ops->init(codec);
  155. }
  156. return -ENOSYS;
  157. }
  158. /*
  159. * NOTE: keep in mind that this function might be called
  160. * multiple times.
  161. */
  162. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  163. {
  164. if (codec->cache_ops && codec->cache_ops->exit) {
  165. if (codec->cache_ops->name)
  166. dev_dbg(codec->dev, "ASoC: Destroying %s cache for %s codec\n",
  167. codec->cache_ops->name, codec->name);
  168. return codec->cache_ops->exit(codec);
  169. }
  170. return -ENOSYS;
  171. }
  172. /**
  173. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  174. *
  175. * @codec: CODEC to configure.
  176. * @reg: The register index.
  177. * @value: The value to be returned.
  178. */
  179. int snd_soc_cache_read(struct snd_soc_codec *codec,
  180. unsigned int reg, unsigned int *value)
  181. {
  182. int ret;
  183. mutex_lock(&codec->cache_rw_mutex);
  184. if (value && codec->cache_ops && codec->cache_ops->read) {
  185. ret = codec->cache_ops->read(codec, reg, value);
  186. mutex_unlock(&codec->cache_rw_mutex);
  187. return ret;
  188. }
  189. mutex_unlock(&codec->cache_rw_mutex);
  190. return -ENOSYS;
  191. }
  192. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  193. /**
  194. * snd_soc_cache_write: Set the value of a given register in the cache.
  195. *
  196. * @codec: CODEC to configure.
  197. * @reg: The register index.
  198. * @value: The new register value.
  199. */
  200. int snd_soc_cache_write(struct snd_soc_codec *codec,
  201. unsigned int reg, unsigned int value)
  202. {
  203. int ret;
  204. mutex_lock(&codec->cache_rw_mutex);
  205. if (codec->cache_ops && codec->cache_ops->write) {
  206. ret = codec->cache_ops->write(codec, reg, value);
  207. mutex_unlock(&codec->cache_rw_mutex);
  208. return ret;
  209. }
  210. mutex_unlock(&codec->cache_rw_mutex);
  211. return -ENOSYS;
  212. }
  213. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  214. /**
  215. * snd_soc_cache_sync: Sync the register cache with the hardware.
  216. *
  217. * @codec: CODEC to configure.
  218. *
  219. * Any registers that should not be synced should be marked as
  220. * volatile. In general drivers can choose not to use the provided
  221. * syncing functionality if they so require.
  222. */
  223. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  224. {
  225. int ret;
  226. const char *name;
  227. if (!codec->cache_sync) {
  228. return 0;
  229. }
  230. if (!codec->cache_ops || !codec->cache_ops->sync)
  231. return -ENOSYS;
  232. if (codec->cache_ops->name)
  233. name = codec->cache_ops->name;
  234. else
  235. name = "unknown";
  236. if (codec->cache_ops->name)
  237. dev_dbg(codec->dev, "ASoC: Syncing %s cache for %s codec\n",
  238. codec->cache_ops->name, codec->name);
  239. trace_snd_soc_cache_sync(codec, name, "start");
  240. ret = codec->cache_ops->sync(codec);
  241. if (!ret)
  242. codec->cache_sync = 0;
  243. trace_snd_soc_cache_sync(codec, name, "end");
  244. return ret;
  245. }
  246. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);