soc-cache.c 4.6 KB

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