soc-cache.c 4.5 KB

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