soc-cache.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. mutex_init(&codec->cache_rw_mutex);
  68. dev_dbg(codec->dev, "ASoC: Initializing cache for %s codec\n",
  69. codec->name);
  70. if (codec_drv->reg_cache_default)
  71. codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
  72. reg_size, GFP_KERNEL);
  73. else
  74. codec->reg_cache = kzalloc(reg_size, GFP_KERNEL);
  75. if (!codec->reg_cache)
  76. return -ENOMEM;
  77. return 0;
  78. }
  79. /*
  80. * NOTE: keep in mind that this function might be called
  81. * multiple times.
  82. */
  83. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  84. {
  85. dev_dbg(codec->dev, "ASoC: Destroying cache for %s codec\n",
  86. codec->name);
  87. if (!codec->reg_cache)
  88. return 0;
  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. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  107. codec->driver->reg_word_size);
  108. mutex_unlock(&codec->cache_rw_mutex);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  112. /**
  113. * snd_soc_cache_write: Set the value of a given register in the cache.
  114. *
  115. * @codec: CODEC to configure.
  116. * @reg: The register index.
  117. * @value: The new register value.
  118. */
  119. int snd_soc_cache_write(struct snd_soc_codec *codec,
  120. unsigned int reg, unsigned int value)
  121. {
  122. mutex_lock(&codec->cache_rw_mutex);
  123. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  124. codec->driver->reg_word_size);
  125. mutex_unlock(&codec->cache_rw_mutex);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  129. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  130. {
  131. int i;
  132. int ret;
  133. const struct snd_soc_codec_driver *codec_drv;
  134. unsigned int val;
  135. codec_drv = codec->driver;
  136. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  137. ret = snd_soc_cache_read(codec, i, &val);
  138. if (ret)
  139. return ret;
  140. if (codec_drv->reg_cache_default)
  141. if (snd_soc_get_cache_val(codec_drv->reg_cache_default,
  142. i, codec_drv->reg_word_size) == val)
  143. continue;
  144. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  145. ret = snd_soc_write(codec, i, val);
  146. if (ret)
  147. return ret;
  148. dev_dbg(codec->dev, "ASoC: Synced register %#x, value = %#x\n",
  149. i, val);
  150. }
  151. return 0;
  152. }
  153. /**
  154. * snd_soc_cache_sync: Sync the register cache with the hardware.
  155. *
  156. * @codec: CODEC to configure.
  157. *
  158. * Any registers that should not be synced should be marked as
  159. * volatile. In general drivers can choose not to use the provided
  160. * syncing functionality if they so require.
  161. */
  162. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  163. {
  164. const char *name = "flat";
  165. int ret;
  166. if (!codec->cache_sync)
  167. return 0;
  168. dev_dbg(codec->dev, "ASoC: Syncing cache for %s codec\n",
  169. codec->name);
  170. trace_snd_soc_cache_sync(codec, name, "start");
  171. ret = snd_soc_flat_cache_sync(codec);
  172. if (!ret)
  173. codec->cache_sync = 0;
  174. trace_snd_soc_cache_sync(codec, name, "end");
  175. return ret;
  176. }
  177. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);