soc-cache.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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->reg_def_copy)
  75. if (snd_soc_get_cache_val(codec->reg_def_copy,
  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, "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. if (codec->reg_def_copy)
  112. codec->reg_cache = kmemdup(codec->reg_def_copy,
  113. codec->reg_size, GFP_KERNEL);
  114. else
  115. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  116. if (!codec->reg_cache)
  117. return -ENOMEM;
  118. return 0;
  119. }
  120. /* an array of all supported compression types */
  121. static const struct snd_soc_cache_ops cache_types[] = {
  122. /* Flat *must* be the first entry for fallback */
  123. {
  124. .id = SND_SOC_FLAT_COMPRESSION,
  125. .name = "flat",
  126. .init = snd_soc_flat_cache_init,
  127. .exit = snd_soc_flat_cache_exit,
  128. .read = snd_soc_flat_cache_read,
  129. .write = snd_soc_flat_cache_write,
  130. .sync = snd_soc_flat_cache_sync
  131. },
  132. };
  133. int snd_soc_cache_init(struct snd_soc_codec *codec)
  134. {
  135. int i;
  136. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  137. if (cache_types[i].id == codec->compress_type)
  138. break;
  139. /* Fall back to flat compression */
  140. if (i == ARRAY_SIZE(cache_types)) {
  141. dev_warn(codec->dev, "Could not match compress type: %d\n",
  142. codec->compress_type);
  143. i = 0;
  144. }
  145. mutex_init(&codec->cache_rw_mutex);
  146. codec->cache_ops = &cache_types[i];
  147. if (codec->cache_ops->init) {
  148. if (codec->cache_ops->name)
  149. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  150. codec->cache_ops->name, codec->name);
  151. return codec->cache_ops->init(codec);
  152. }
  153. return -ENOSYS;
  154. }
  155. /*
  156. * NOTE: keep in mind that this function might be called
  157. * multiple times.
  158. */
  159. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  160. {
  161. if (codec->cache_ops && codec->cache_ops->exit) {
  162. if (codec->cache_ops->name)
  163. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  164. codec->cache_ops->name, codec->name);
  165. return codec->cache_ops->exit(codec);
  166. }
  167. return -ENOSYS;
  168. }
  169. /**
  170. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  171. *
  172. * @codec: CODEC to configure.
  173. * @reg: The register index.
  174. * @value: The value to be returned.
  175. */
  176. int snd_soc_cache_read(struct snd_soc_codec *codec,
  177. unsigned int reg, unsigned int *value)
  178. {
  179. int ret;
  180. mutex_lock(&codec->cache_rw_mutex);
  181. if (value && codec->cache_ops && codec->cache_ops->read) {
  182. ret = codec->cache_ops->read(codec, reg, value);
  183. mutex_unlock(&codec->cache_rw_mutex);
  184. return ret;
  185. }
  186. mutex_unlock(&codec->cache_rw_mutex);
  187. return -ENOSYS;
  188. }
  189. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  190. /**
  191. * snd_soc_cache_write: Set the value of a given register in the cache.
  192. *
  193. * @codec: CODEC to configure.
  194. * @reg: The register index.
  195. * @value: The new register value.
  196. */
  197. int snd_soc_cache_write(struct snd_soc_codec *codec,
  198. unsigned int reg, unsigned int value)
  199. {
  200. int ret;
  201. mutex_lock(&codec->cache_rw_mutex);
  202. if (codec->cache_ops && codec->cache_ops->write) {
  203. ret = codec->cache_ops->write(codec, reg, value);
  204. mutex_unlock(&codec->cache_rw_mutex);
  205. return ret;
  206. }
  207. mutex_unlock(&codec->cache_rw_mutex);
  208. return -ENOSYS;
  209. }
  210. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  211. /**
  212. * snd_soc_cache_sync: Sync the register cache with the hardware.
  213. *
  214. * @codec: CODEC to configure.
  215. *
  216. * Any registers that should not be synced should be marked as
  217. * volatile. In general drivers can choose not to use the provided
  218. * syncing functionality if they so require.
  219. */
  220. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  221. {
  222. int ret;
  223. const char *name;
  224. if (!codec->cache_sync) {
  225. return 0;
  226. }
  227. if (!codec->cache_ops || !codec->cache_ops->sync)
  228. return -ENOSYS;
  229. if (codec->cache_ops->name)
  230. name = codec->cache_ops->name;
  231. else
  232. name = "unknown";
  233. if (codec->cache_ops->name)
  234. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  235. codec->cache_ops->name, codec->name);
  236. trace_snd_soc_cache_sync(codec, name, "start");
  237. ret = codec->cache_ops->sync(codec);
  238. if (!ret)
  239. codec->cache_sync = 0;
  240. trace_snd_soc_cache_sync(codec, name, "end");
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  244. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  245. unsigned int reg)
  246. {
  247. const struct snd_soc_codec_driver *codec_drv;
  248. unsigned int min, max, index;
  249. codec_drv = codec->driver;
  250. min = 0;
  251. max = codec_drv->reg_access_size - 1;
  252. do {
  253. index = (min + max) / 2;
  254. if (codec_drv->reg_access_default[index].reg == reg)
  255. return index;
  256. if (codec_drv->reg_access_default[index].reg < reg)
  257. min = index + 1;
  258. else
  259. max = index;
  260. } while (min <= max);
  261. return -1;
  262. }
  263. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  264. unsigned int reg)
  265. {
  266. int index;
  267. if (reg >= codec->driver->reg_cache_size)
  268. return 1;
  269. index = snd_soc_get_reg_access_index(codec, reg);
  270. if (index < 0)
  271. return 0;
  272. return codec->driver->reg_access_default[index].vol;
  273. }
  274. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  275. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  276. unsigned int reg)
  277. {
  278. int index;
  279. if (reg >= codec->driver->reg_cache_size)
  280. return 1;
  281. index = snd_soc_get_reg_access_index(codec, reg);
  282. if (index < 0)
  283. return 0;
  284. return codec->driver->reg_access_default[index].read;
  285. }
  286. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  287. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  288. unsigned int reg)
  289. {
  290. int index;
  291. if (reg >= codec->driver->reg_cache_size)
  292. return 1;
  293. index = snd_soc_get_reg_access_index(codec, reg);
  294. if (index < 0)
  295. return 0;
  296. return codec->driver->reg_access_default[index].write;
  297. }
  298. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);