soc-cache.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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/lzo.h>
  17. #include <linux/bitmap.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/export.h>
  20. #include <trace/events/asoc.h>
  21. static bool snd_soc_set_cache_val(void *base, unsigned int idx,
  22. unsigned int val, unsigned int word_size)
  23. {
  24. switch (word_size) {
  25. case 1: {
  26. u8 *cache = base;
  27. if (cache[idx] == val)
  28. return true;
  29. cache[idx] = val;
  30. break;
  31. }
  32. case 2: {
  33. u16 *cache = base;
  34. if (cache[idx] == val)
  35. return true;
  36. cache[idx] = val;
  37. break;
  38. }
  39. default:
  40. BUG();
  41. }
  42. return false;
  43. }
  44. static unsigned int snd_soc_get_cache_val(const void *base, unsigned int idx,
  45. unsigned int word_size)
  46. {
  47. if (!base)
  48. return -1;
  49. switch (word_size) {
  50. case 1: {
  51. const u8 *cache = base;
  52. return cache[idx];
  53. }
  54. case 2: {
  55. const u16 *cache = base;
  56. return cache[idx];
  57. }
  58. default:
  59. BUG();
  60. }
  61. /* unreachable */
  62. return -1;
  63. }
  64. struct snd_soc_rbtree_node {
  65. struct rb_node node; /* the actual rbtree node holding this block */
  66. unsigned int base_reg; /* base register handled by this block */
  67. unsigned int word_size; /* number of bytes needed to represent the register index */
  68. void *block; /* block of adjacent registers */
  69. unsigned int blklen; /* number of registers available in the block */
  70. } __attribute__ ((packed));
  71. struct snd_soc_rbtree_ctx {
  72. struct rb_root root;
  73. struct snd_soc_rbtree_node *cached_rbnode;
  74. };
  75. static inline void snd_soc_rbtree_get_base_top_reg(
  76. struct snd_soc_rbtree_node *rbnode,
  77. unsigned int *base, unsigned int *top)
  78. {
  79. *base = rbnode->base_reg;
  80. *top = rbnode->base_reg + rbnode->blklen - 1;
  81. }
  82. static unsigned int snd_soc_rbtree_get_register(
  83. struct snd_soc_rbtree_node *rbnode, unsigned int idx)
  84. {
  85. unsigned int val;
  86. switch (rbnode->word_size) {
  87. case 1: {
  88. u8 *p = rbnode->block;
  89. val = p[idx];
  90. return val;
  91. }
  92. case 2: {
  93. u16 *p = rbnode->block;
  94. val = p[idx];
  95. return val;
  96. }
  97. default:
  98. BUG();
  99. break;
  100. }
  101. return -1;
  102. }
  103. static void snd_soc_rbtree_set_register(struct snd_soc_rbtree_node *rbnode,
  104. unsigned int idx, unsigned int val)
  105. {
  106. switch (rbnode->word_size) {
  107. case 1: {
  108. u8 *p = rbnode->block;
  109. p[idx] = val;
  110. break;
  111. }
  112. case 2: {
  113. u16 *p = rbnode->block;
  114. p[idx] = val;
  115. break;
  116. }
  117. default:
  118. BUG();
  119. break;
  120. }
  121. }
  122. static struct snd_soc_rbtree_node *snd_soc_rbtree_lookup(
  123. struct rb_root *root, unsigned int reg)
  124. {
  125. struct rb_node *node;
  126. struct snd_soc_rbtree_node *rbnode;
  127. unsigned int base_reg, top_reg;
  128. node = root->rb_node;
  129. while (node) {
  130. rbnode = container_of(node, struct snd_soc_rbtree_node, node);
  131. snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
  132. if (reg >= base_reg && reg <= top_reg)
  133. return rbnode;
  134. else if (reg > top_reg)
  135. node = node->rb_right;
  136. else if (reg < base_reg)
  137. node = node->rb_left;
  138. }
  139. return NULL;
  140. }
  141. static int snd_soc_rbtree_insert(struct rb_root *root,
  142. struct snd_soc_rbtree_node *rbnode)
  143. {
  144. struct rb_node **new, *parent;
  145. struct snd_soc_rbtree_node *rbnode_tmp;
  146. unsigned int base_reg_tmp, top_reg_tmp;
  147. unsigned int base_reg;
  148. parent = NULL;
  149. new = &root->rb_node;
  150. while (*new) {
  151. rbnode_tmp = container_of(*new, struct snd_soc_rbtree_node,
  152. node);
  153. /* base and top registers of the current rbnode */
  154. snd_soc_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
  155. &top_reg_tmp);
  156. /* base register of the rbnode to be added */
  157. base_reg = rbnode->base_reg;
  158. parent = *new;
  159. /* if this register has already been inserted, just return */
  160. if (base_reg >= base_reg_tmp &&
  161. base_reg <= top_reg_tmp)
  162. return 0;
  163. else if (base_reg > top_reg_tmp)
  164. new = &((*new)->rb_right);
  165. else if (base_reg < base_reg_tmp)
  166. new = &((*new)->rb_left);
  167. }
  168. /* insert the node into the rbtree */
  169. rb_link_node(&rbnode->node, parent, new);
  170. rb_insert_color(&rbnode->node, root);
  171. return 1;
  172. }
  173. static int snd_soc_rbtree_cache_sync(struct snd_soc_codec *codec)
  174. {
  175. struct snd_soc_rbtree_ctx *rbtree_ctx;
  176. struct rb_node *node;
  177. struct snd_soc_rbtree_node *rbnode;
  178. unsigned int regtmp;
  179. unsigned int val, def;
  180. int ret;
  181. int i;
  182. rbtree_ctx = codec->reg_cache;
  183. for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
  184. rbnode = rb_entry(node, struct snd_soc_rbtree_node, node);
  185. for (i = 0; i < rbnode->blklen; ++i) {
  186. regtmp = rbnode->base_reg + i;
  187. val = snd_soc_rbtree_get_register(rbnode, i);
  188. def = snd_soc_get_cache_val(codec->reg_def_copy, i,
  189. rbnode->word_size);
  190. if (val == def)
  191. continue;
  192. WARN_ON(!snd_soc_codec_writable_register(codec, regtmp));
  193. codec->cache_bypass = 1;
  194. ret = snd_soc_write(codec, regtmp, val);
  195. codec->cache_bypass = 0;
  196. if (ret)
  197. return ret;
  198. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  199. regtmp, val);
  200. }
  201. }
  202. return 0;
  203. }
  204. static int snd_soc_rbtree_insert_to_block(struct snd_soc_rbtree_node *rbnode,
  205. unsigned int pos, unsigned int reg,
  206. unsigned int value)
  207. {
  208. u8 *blk;
  209. blk = krealloc(rbnode->block,
  210. (rbnode->blklen + 1) * rbnode->word_size, GFP_KERNEL);
  211. if (!blk)
  212. return -ENOMEM;
  213. /* insert the register value in the correct place in the rbnode block */
  214. memmove(blk + (pos + 1) * rbnode->word_size,
  215. blk + pos * rbnode->word_size,
  216. (rbnode->blklen - pos) * rbnode->word_size);
  217. /* update the rbnode block, its size and the base register */
  218. rbnode->block = blk;
  219. rbnode->blklen++;
  220. if (!pos)
  221. rbnode->base_reg = reg;
  222. snd_soc_rbtree_set_register(rbnode, pos, value);
  223. return 0;
  224. }
  225. static int snd_soc_rbtree_cache_write(struct snd_soc_codec *codec,
  226. unsigned int reg, unsigned int value)
  227. {
  228. struct snd_soc_rbtree_ctx *rbtree_ctx;
  229. struct snd_soc_rbtree_node *rbnode, *rbnode_tmp;
  230. struct rb_node *node;
  231. unsigned int val;
  232. unsigned int reg_tmp;
  233. unsigned int base_reg, top_reg;
  234. unsigned int pos;
  235. int i;
  236. int ret;
  237. rbtree_ctx = codec->reg_cache;
  238. /* look up the required register in the cached rbnode */
  239. rbnode = rbtree_ctx->cached_rbnode;
  240. if (rbnode) {
  241. snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
  242. if (reg >= base_reg && reg <= top_reg) {
  243. reg_tmp = reg - base_reg;
  244. val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
  245. if (val == value)
  246. return 0;
  247. snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
  248. return 0;
  249. }
  250. }
  251. /* if we can't locate it in the cached rbnode we'll have
  252. * to traverse the rbtree looking for it.
  253. */
  254. rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
  255. if (rbnode) {
  256. reg_tmp = reg - rbnode->base_reg;
  257. val = snd_soc_rbtree_get_register(rbnode, reg_tmp);
  258. if (val == value)
  259. return 0;
  260. snd_soc_rbtree_set_register(rbnode, reg_tmp, value);
  261. rbtree_ctx->cached_rbnode = rbnode;
  262. } else {
  263. /* bail out early, no need to create the rbnode yet */
  264. if (!value)
  265. return 0;
  266. /* look for an adjacent register to the one we are about to add */
  267. for (node = rb_first(&rbtree_ctx->root); node;
  268. node = rb_next(node)) {
  269. rbnode_tmp = rb_entry(node, struct snd_soc_rbtree_node, node);
  270. for (i = 0; i < rbnode_tmp->blklen; ++i) {
  271. reg_tmp = rbnode_tmp->base_reg + i;
  272. if (abs(reg_tmp - reg) != 1)
  273. continue;
  274. /* decide where in the block to place our register */
  275. if (reg_tmp + 1 == reg)
  276. pos = i + 1;
  277. else
  278. pos = i;
  279. ret = snd_soc_rbtree_insert_to_block(rbnode_tmp, pos,
  280. reg, value);
  281. if (ret)
  282. return ret;
  283. rbtree_ctx->cached_rbnode = rbnode_tmp;
  284. return 0;
  285. }
  286. }
  287. /* we did not manage to find a place to insert it in an existing
  288. * block so create a new rbnode with a single register in its block.
  289. * This block will get populated further if any other adjacent
  290. * registers get modified in the future.
  291. */
  292. rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL);
  293. if (!rbnode)
  294. return -ENOMEM;
  295. rbnode->blklen = 1;
  296. rbnode->base_reg = reg;
  297. rbnode->word_size = codec->driver->reg_word_size;
  298. rbnode->block = kmalloc(rbnode->blklen * rbnode->word_size,
  299. GFP_KERNEL);
  300. if (!rbnode->block) {
  301. kfree(rbnode);
  302. return -ENOMEM;
  303. }
  304. snd_soc_rbtree_set_register(rbnode, 0, value);
  305. snd_soc_rbtree_insert(&rbtree_ctx->root, rbnode);
  306. rbtree_ctx->cached_rbnode = rbnode;
  307. }
  308. return 0;
  309. }
  310. static int snd_soc_rbtree_cache_read(struct snd_soc_codec *codec,
  311. unsigned int reg, unsigned int *value)
  312. {
  313. struct snd_soc_rbtree_ctx *rbtree_ctx;
  314. struct snd_soc_rbtree_node *rbnode;
  315. unsigned int base_reg, top_reg;
  316. unsigned int reg_tmp;
  317. rbtree_ctx = codec->reg_cache;
  318. /* look up the required register in the cached rbnode */
  319. rbnode = rbtree_ctx->cached_rbnode;
  320. if (rbnode) {
  321. snd_soc_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
  322. if (reg >= base_reg && reg <= top_reg) {
  323. reg_tmp = reg - base_reg;
  324. *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
  325. return 0;
  326. }
  327. }
  328. /* if we can't locate it in the cached rbnode we'll have
  329. * to traverse the rbtree looking for it.
  330. */
  331. rbnode = snd_soc_rbtree_lookup(&rbtree_ctx->root, reg);
  332. if (rbnode) {
  333. reg_tmp = reg - rbnode->base_reg;
  334. *value = snd_soc_rbtree_get_register(rbnode, reg_tmp);
  335. rbtree_ctx->cached_rbnode = rbnode;
  336. } else {
  337. /* uninitialized registers default to 0 */
  338. *value = 0;
  339. }
  340. return 0;
  341. }
  342. static int snd_soc_rbtree_cache_exit(struct snd_soc_codec *codec)
  343. {
  344. struct rb_node *next;
  345. struct snd_soc_rbtree_ctx *rbtree_ctx;
  346. struct snd_soc_rbtree_node *rbtree_node;
  347. /* if we've already been called then just return */
  348. rbtree_ctx = codec->reg_cache;
  349. if (!rbtree_ctx)
  350. return 0;
  351. /* free up the rbtree */
  352. next = rb_first(&rbtree_ctx->root);
  353. while (next) {
  354. rbtree_node = rb_entry(next, struct snd_soc_rbtree_node, node);
  355. next = rb_next(&rbtree_node->node);
  356. rb_erase(&rbtree_node->node, &rbtree_ctx->root);
  357. kfree(rbtree_node->block);
  358. kfree(rbtree_node);
  359. }
  360. /* release the resources */
  361. kfree(codec->reg_cache);
  362. codec->reg_cache = NULL;
  363. return 0;
  364. }
  365. static int snd_soc_rbtree_cache_init(struct snd_soc_codec *codec)
  366. {
  367. struct snd_soc_rbtree_ctx *rbtree_ctx;
  368. unsigned int word_size;
  369. unsigned int val;
  370. int i;
  371. int ret;
  372. codec->reg_cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
  373. if (!codec->reg_cache)
  374. return -ENOMEM;
  375. rbtree_ctx = codec->reg_cache;
  376. rbtree_ctx->root = RB_ROOT;
  377. rbtree_ctx->cached_rbnode = NULL;
  378. if (!codec->reg_def_copy)
  379. return 0;
  380. word_size = codec->driver->reg_word_size;
  381. for (i = 0; i < codec->driver->reg_cache_size; ++i) {
  382. val = snd_soc_get_cache_val(codec->reg_def_copy, i,
  383. word_size);
  384. if (!val)
  385. continue;
  386. ret = snd_soc_rbtree_cache_write(codec, i, val);
  387. if (ret)
  388. goto err;
  389. }
  390. return 0;
  391. err:
  392. snd_soc_cache_exit(codec);
  393. return ret;
  394. }
  395. #ifdef CONFIG_SND_SOC_CACHE_LZO
  396. struct snd_soc_lzo_ctx {
  397. void *wmem;
  398. void *dst;
  399. const void *src;
  400. size_t src_len;
  401. size_t dst_len;
  402. size_t decompressed_size;
  403. unsigned long *sync_bmp;
  404. int sync_bmp_nbits;
  405. };
  406. #define LZO_BLOCK_NUM 8
  407. static int snd_soc_lzo_block_count(void)
  408. {
  409. return LZO_BLOCK_NUM;
  410. }
  411. static int snd_soc_lzo_prepare(struct snd_soc_lzo_ctx *lzo_ctx)
  412. {
  413. lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  414. if (!lzo_ctx->wmem)
  415. return -ENOMEM;
  416. return 0;
  417. }
  418. static int snd_soc_lzo_compress(struct snd_soc_lzo_ctx *lzo_ctx)
  419. {
  420. size_t compress_size;
  421. int ret;
  422. ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
  423. lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
  424. if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
  425. return -EINVAL;
  426. lzo_ctx->dst_len = compress_size;
  427. return 0;
  428. }
  429. static int snd_soc_lzo_decompress(struct snd_soc_lzo_ctx *lzo_ctx)
  430. {
  431. size_t dst_len;
  432. int ret;
  433. dst_len = lzo_ctx->dst_len;
  434. ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
  435. lzo_ctx->dst, &dst_len);
  436. if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
  437. return -EINVAL;
  438. return 0;
  439. }
  440. static int snd_soc_lzo_compress_cache_block(struct snd_soc_codec *codec,
  441. struct snd_soc_lzo_ctx *lzo_ctx)
  442. {
  443. int ret;
  444. lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
  445. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  446. if (!lzo_ctx->dst) {
  447. lzo_ctx->dst_len = 0;
  448. return -ENOMEM;
  449. }
  450. ret = snd_soc_lzo_compress(lzo_ctx);
  451. if (ret < 0)
  452. return ret;
  453. return 0;
  454. }
  455. static int snd_soc_lzo_decompress_cache_block(struct snd_soc_codec *codec,
  456. struct snd_soc_lzo_ctx *lzo_ctx)
  457. {
  458. int ret;
  459. lzo_ctx->dst_len = lzo_ctx->decompressed_size;
  460. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  461. if (!lzo_ctx->dst) {
  462. lzo_ctx->dst_len = 0;
  463. return -ENOMEM;
  464. }
  465. ret = snd_soc_lzo_decompress(lzo_ctx);
  466. if (ret < 0)
  467. return ret;
  468. return 0;
  469. }
  470. static inline int snd_soc_lzo_get_blkindex(struct snd_soc_codec *codec,
  471. unsigned int reg)
  472. {
  473. const struct snd_soc_codec_driver *codec_drv;
  474. codec_drv = codec->driver;
  475. return (reg * codec_drv->reg_word_size) /
  476. DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  477. }
  478. static inline int snd_soc_lzo_get_blkpos(struct snd_soc_codec *codec,
  479. unsigned int reg)
  480. {
  481. const struct snd_soc_codec_driver *codec_drv;
  482. codec_drv = codec->driver;
  483. return reg % (DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count()) /
  484. codec_drv->reg_word_size);
  485. }
  486. static inline int snd_soc_lzo_get_blksize(struct snd_soc_codec *codec)
  487. {
  488. return DIV_ROUND_UP(codec->reg_size, snd_soc_lzo_block_count());
  489. }
  490. static int snd_soc_lzo_cache_sync(struct snd_soc_codec *codec)
  491. {
  492. struct snd_soc_lzo_ctx **lzo_blocks;
  493. unsigned int val;
  494. int i;
  495. int ret;
  496. lzo_blocks = codec->reg_cache;
  497. for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
  498. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  499. ret = snd_soc_cache_read(codec, i, &val);
  500. if (ret)
  501. return ret;
  502. codec->cache_bypass = 1;
  503. ret = snd_soc_write(codec, i, val);
  504. codec->cache_bypass = 0;
  505. if (ret)
  506. return ret;
  507. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  508. i, val);
  509. }
  510. return 0;
  511. }
  512. static int snd_soc_lzo_cache_write(struct snd_soc_codec *codec,
  513. unsigned int reg, unsigned int value)
  514. {
  515. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  516. int ret, blkindex, blkpos;
  517. size_t blksize, tmp_dst_len;
  518. void *tmp_dst;
  519. /* index of the compressed lzo block */
  520. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  521. /* register index within the decompressed block */
  522. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  523. /* size of the compressed block */
  524. blksize = snd_soc_lzo_get_blksize(codec);
  525. lzo_blocks = codec->reg_cache;
  526. lzo_block = lzo_blocks[blkindex];
  527. /* save the pointer and length of the compressed block */
  528. tmp_dst = lzo_block->dst;
  529. tmp_dst_len = lzo_block->dst_len;
  530. /* prepare the source to be the compressed block */
  531. lzo_block->src = lzo_block->dst;
  532. lzo_block->src_len = lzo_block->dst_len;
  533. /* decompress the block */
  534. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  535. if (ret < 0) {
  536. kfree(lzo_block->dst);
  537. goto out;
  538. }
  539. /* write the new value to the cache */
  540. if (snd_soc_set_cache_val(lzo_block->dst, blkpos, value,
  541. codec->driver->reg_word_size)) {
  542. kfree(lzo_block->dst);
  543. goto out;
  544. }
  545. /* prepare the source to be the decompressed block */
  546. lzo_block->src = lzo_block->dst;
  547. lzo_block->src_len = lzo_block->dst_len;
  548. /* compress the block */
  549. ret = snd_soc_lzo_compress_cache_block(codec, lzo_block);
  550. if (ret < 0) {
  551. kfree(lzo_block->dst);
  552. kfree(lzo_block->src);
  553. goto out;
  554. }
  555. /* set the bit so we know we have to sync this register */
  556. set_bit(reg, lzo_block->sync_bmp);
  557. kfree(tmp_dst);
  558. kfree(lzo_block->src);
  559. return 0;
  560. out:
  561. lzo_block->dst = tmp_dst;
  562. lzo_block->dst_len = tmp_dst_len;
  563. return ret;
  564. }
  565. static int snd_soc_lzo_cache_read(struct snd_soc_codec *codec,
  566. unsigned int reg, unsigned int *value)
  567. {
  568. struct snd_soc_lzo_ctx *lzo_block, **lzo_blocks;
  569. int ret, blkindex, blkpos;
  570. size_t blksize, tmp_dst_len;
  571. void *tmp_dst;
  572. *value = 0;
  573. /* index of the compressed lzo block */
  574. blkindex = snd_soc_lzo_get_blkindex(codec, reg);
  575. /* register index within the decompressed block */
  576. blkpos = snd_soc_lzo_get_blkpos(codec, reg);
  577. /* size of the compressed block */
  578. blksize = snd_soc_lzo_get_blksize(codec);
  579. lzo_blocks = codec->reg_cache;
  580. lzo_block = lzo_blocks[blkindex];
  581. /* save the pointer and length of the compressed block */
  582. tmp_dst = lzo_block->dst;
  583. tmp_dst_len = lzo_block->dst_len;
  584. /* prepare the source to be the compressed block */
  585. lzo_block->src = lzo_block->dst;
  586. lzo_block->src_len = lzo_block->dst_len;
  587. /* decompress the block */
  588. ret = snd_soc_lzo_decompress_cache_block(codec, lzo_block);
  589. if (ret >= 0)
  590. /* fetch the value from the cache */
  591. *value = snd_soc_get_cache_val(lzo_block->dst, blkpos,
  592. codec->driver->reg_word_size);
  593. kfree(lzo_block->dst);
  594. /* restore the pointer and length of the compressed block */
  595. lzo_block->dst = tmp_dst;
  596. lzo_block->dst_len = tmp_dst_len;
  597. return 0;
  598. }
  599. static int snd_soc_lzo_cache_exit(struct snd_soc_codec *codec)
  600. {
  601. struct snd_soc_lzo_ctx **lzo_blocks;
  602. int i, blkcount;
  603. lzo_blocks = codec->reg_cache;
  604. if (!lzo_blocks)
  605. return 0;
  606. blkcount = snd_soc_lzo_block_count();
  607. /*
  608. * the pointer to the bitmap used for syncing the cache
  609. * is shared amongst all lzo_blocks. Ensure it is freed
  610. * only once.
  611. */
  612. if (lzo_blocks[0])
  613. kfree(lzo_blocks[0]->sync_bmp);
  614. for (i = 0; i < blkcount; ++i) {
  615. if (lzo_blocks[i]) {
  616. kfree(lzo_blocks[i]->wmem);
  617. kfree(lzo_blocks[i]->dst);
  618. }
  619. /* each lzo_block is a pointer returned by kmalloc or NULL */
  620. kfree(lzo_blocks[i]);
  621. }
  622. kfree(lzo_blocks);
  623. codec->reg_cache = NULL;
  624. return 0;
  625. }
  626. static int snd_soc_lzo_cache_init(struct snd_soc_codec *codec)
  627. {
  628. struct snd_soc_lzo_ctx **lzo_blocks;
  629. size_t bmp_size;
  630. const struct snd_soc_codec_driver *codec_drv;
  631. int ret, tofree, i, blksize, blkcount;
  632. const char *p, *end;
  633. unsigned long *sync_bmp;
  634. ret = 0;
  635. codec_drv = codec->driver;
  636. /*
  637. * If we have not been given a default register cache
  638. * then allocate a dummy zero-ed out region, compress it
  639. * and remember to free it afterwards.
  640. */
  641. tofree = 0;
  642. if (!codec->reg_def_copy)
  643. tofree = 1;
  644. if (!codec->reg_def_copy) {
  645. codec->reg_def_copy = kzalloc(codec->reg_size, GFP_KERNEL);
  646. if (!codec->reg_def_copy)
  647. return -ENOMEM;
  648. }
  649. blkcount = snd_soc_lzo_block_count();
  650. codec->reg_cache = kzalloc(blkcount * sizeof *lzo_blocks,
  651. GFP_KERNEL);
  652. if (!codec->reg_cache) {
  653. ret = -ENOMEM;
  654. goto err_tofree;
  655. }
  656. lzo_blocks = codec->reg_cache;
  657. /*
  658. * allocate a bitmap to be used when syncing the cache with
  659. * the hardware. Each time a register is modified, the corresponding
  660. * bit is set in the bitmap, so we know that we have to sync
  661. * that register.
  662. */
  663. bmp_size = codec_drv->reg_cache_size;
  664. sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
  665. GFP_KERNEL);
  666. if (!sync_bmp) {
  667. ret = -ENOMEM;
  668. goto err;
  669. }
  670. bitmap_zero(sync_bmp, bmp_size);
  671. /* allocate the lzo blocks and initialize them */
  672. for (i = 0; i < blkcount; ++i) {
  673. lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
  674. GFP_KERNEL);
  675. if (!lzo_blocks[i]) {
  676. kfree(sync_bmp);
  677. ret = -ENOMEM;
  678. goto err;
  679. }
  680. lzo_blocks[i]->sync_bmp = sync_bmp;
  681. lzo_blocks[i]->sync_bmp_nbits = bmp_size;
  682. /* alloc the working space for the compressed block */
  683. ret = snd_soc_lzo_prepare(lzo_blocks[i]);
  684. if (ret < 0)
  685. goto err;
  686. }
  687. blksize = snd_soc_lzo_get_blksize(codec);
  688. p = codec->reg_def_copy;
  689. end = codec->reg_def_copy + codec->reg_size;
  690. /* compress the register map and fill the lzo blocks */
  691. for (i = 0; i < blkcount; ++i, p += blksize) {
  692. lzo_blocks[i]->src = p;
  693. if (p + blksize > end)
  694. lzo_blocks[i]->src_len = end - p;
  695. else
  696. lzo_blocks[i]->src_len = blksize;
  697. ret = snd_soc_lzo_compress_cache_block(codec,
  698. lzo_blocks[i]);
  699. if (ret < 0)
  700. goto err;
  701. lzo_blocks[i]->decompressed_size =
  702. lzo_blocks[i]->src_len;
  703. }
  704. if (tofree) {
  705. kfree(codec->reg_def_copy);
  706. codec->reg_def_copy = NULL;
  707. }
  708. return 0;
  709. err:
  710. snd_soc_cache_exit(codec);
  711. err_tofree:
  712. if (tofree) {
  713. kfree(codec->reg_def_copy);
  714. codec->reg_def_copy = NULL;
  715. }
  716. return ret;
  717. }
  718. #endif
  719. static int snd_soc_flat_cache_sync(struct snd_soc_codec *codec)
  720. {
  721. int i;
  722. int ret;
  723. const struct snd_soc_codec_driver *codec_drv;
  724. unsigned int val;
  725. codec_drv = codec->driver;
  726. for (i = 0; i < codec_drv->reg_cache_size; ++i) {
  727. ret = snd_soc_cache_read(codec, i, &val);
  728. if (ret)
  729. return ret;
  730. if (codec->reg_def_copy)
  731. if (snd_soc_get_cache_val(codec->reg_def_copy,
  732. i, codec_drv->reg_word_size) == val)
  733. continue;
  734. WARN_ON(!snd_soc_codec_writable_register(codec, i));
  735. ret = snd_soc_write(codec, i, val);
  736. if (ret)
  737. return ret;
  738. dev_dbg(codec->dev, "Synced register %#x, value = %#x\n",
  739. i, val);
  740. }
  741. return 0;
  742. }
  743. static int snd_soc_flat_cache_write(struct snd_soc_codec *codec,
  744. unsigned int reg, unsigned int value)
  745. {
  746. snd_soc_set_cache_val(codec->reg_cache, reg, value,
  747. codec->driver->reg_word_size);
  748. return 0;
  749. }
  750. static int snd_soc_flat_cache_read(struct snd_soc_codec *codec,
  751. unsigned int reg, unsigned int *value)
  752. {
  753. *value = snd_soc_get_cache_val(codec->reg_cache, reg,
  754. codec->driver->reg_word_size);
  755. return 0;
  756. }
  757. static int snd_soc_flat_cache_exit(struct snd_soc_codec *codec)
  758. {
  759. if (!codec->reg_cache)
  760. return 0;
  761. kfree(codec->reg_cache);
  762. codec->reg_cache = NULL;
  763. return 0;
  764. }
  765. static int snd_soc_flat_cache_init(struct snd_soc_codec *codec)
  766. {
  767. if (codec->reg_def_copy)
  768. codec->reg_cache = kmemdup(codec->reg_def_copy,
  769. codec->reg_size, GFP_KERNEL);
  770. else
  771. codec->reg_cache = kzalloc(codec->reg_size, GFP_KERNEL);
  772. if (!codec->reg_cache)
  773. return -ENOMEM;
  774. return 0;
  775. }
  776. /* an array of all supported compression types */
  777. static const struct snd_soc_cache_ops cache_types[] = {
  778. /* Flat *must* be the first entry for fallback */
  779. {
  780. .id = SND_SOC_FLAT_COMPRESSION,
  781. .name = "flat",
  782. .init = snd_soc_flat_cache_init,
  783. .exit = snd_soc_flat_cache_exit,
  784. .read = snd_soc_flat_cache_read,
  785. .write = snd_soc_flat_cache_write,
  786. .sync = snd_soc_flat_cache_sync
  787. },
  788. #ifdef CONFIG_SND_SOC_CACHE_LZO
  789. {
  790. .id = SND_SOC_LZO_COMPRESSION,
  791. .name = "LZO",
  792. .init = snd_soc_lzo_cache_init,
  793. .exit = snd_soc_lzo_cache_exit,
  794. .read = snd_soc_lzo_cache_read,
  795. .write = snd_soc_lzo_cache_write,
  796. .sync = snd_soc_lzo_cache_sync
  797. },
  798. #endif
  799. {
  800. .id = SND_SOC_RBTREE_COMPRESSION,
  801. .name = "rbtree",
  802. .init = snd_soc_rbtree_cache_init,
  803. .exit = snd_soc_rbtree_cache_exit,
  804. .read = snd_soc_rbtree_cache_read,
  805. .write = snd_soc_rbtree_cache_write,
  806. .sync = snd_soc_rbtree_cache_sync
  807. }
  808. };
  809. int snd_soc_cache_init(struct snd_soc_codec *codec)
  810. {
  811. int i;
  812. for (i = 0; i < ARRAY_SIZE(cache_types); ++i)
  813. if (cache_types[i].id == codec->compress_type)
  814. break;
  815. /* Fall back to flat compression */
  816. if (i == ARRAY_SIZE(cache_types)) {
  817. dev_warn(codec->dev, "Could not match compress type: %d\n",
  818. codec->compress_type);
  819. i = 0;
  820. }
  821. mutex_init(&codec->cache_rw_mutex);
  822. codec->cache_ops = &cache_types[i];
  823. if (codec->cache_ops->init) {
  824. if (codec->cache_ops->name)
  825. dev_dbg(codec->dev, "Initializing %s cache for %s codec\n",
  826. codec->cache_ops->name, codec->name);
  827. return codec->cache_ops->init(codec);
  828. }
  829. return -ENOSYS;
  830. }
  831. /*
  832. * NOTE: keep in mind that this function might be called
  833. * multiple times.
  834. */
  835. int snd_soc_cache_exit(struct snd_soc_codec *codec)
  836. {
  837. if (codec->cache_ops && codec->cache_ops->exit) {
  838. if (codec->cache_ops->name)
  839. dev_dbg(codec->dev, "Destroying %s cache for %s codec\n",
  840. codec->cache_ops->name, codec->name);
  841. return codec->cache_ops->exit(codec);
  842. }
  843. return -ENOSYS;
  844. }
  845. /**
  846. * snd_soc_cache_read: Fetch the value of a given register from the cache.
  847. *
  848. * @codec: CODEC to configure.
  849. * @reg: The register index.
  850. * @value: The value to be returned.
  851. */
  852. int snd_soc_cache_read(struct snd_soc_codec *codec,
  853. unsigned int reg, unsigned int *value)
  854. {
  855. int ret;
  856. mutex_lock(&codec->cache_rw_mutex);
  857. if (value && codec->cache_ops && codec->cache_ops->read) {
  858. ret = codec->cache_ops->read(codec, reg, value);
  859. mutex_unlock(&codec->cache_rw_mutex);
  860. return ret;
  861. }
  862. mutex_unlock(&codec->cache_rw_mutex);
  863. return -ENOSYS;
  864. }
  865. EXPORT_SYMBOL_GPL(snd_soc_cache_read);
  866. /**
  867. * snd_soc_cache_write: Set the value of a given register in the cache.
  868. *
  869. * @codec: CODEC to configure.
  870. * @reg: The register index.
  871. * @value: The new register value.
  872. */
  873. int snd_soc_cache_write(struct snd_soc_codec *codec,
  874. unsigned int reg, unsigned int value)
  875. {
  876. int ret;
  877. mutex_lock(&codec->cache_rw_mutex);
  878. if (codec->cache_ops && codec->cache_ops->write) {
  879. ret = codec->cache_ops->write(codec, reg, value);
  880. mutex_unlock(&codec->cache_rw_mutex);
  881. return ret;
  882. }
  883. mutex_unlock(&codec->cache_rw_mutex);
  884. return -ENOSYS;
  885. }
  886. EXPORT_SYMBOL_GPL(snd_soc_cache_write);
  887. /**
  888. * snd_soc_cache_sync: Sync the register cache with the hardware.
  889. *
  890. * @codec: CODEC to configure.
  891. *
  892. * Any registers that should not be synced should be marked as
  893. * volatile. In general drivers can choose not to use the provided
  894. * syncing functionality if they so require.
  895. */
  896. int snd_soc_cache_sync(struct snd_soc_codec *codec)
  897. {
  898. int ret;
  899. const char *name;
  900. if (!codec->cache_sync) {
  901. return 0;
  902. }
  903. if (!codec->cache_ops || !codec->cache_ops->sync)
  904. return -ENOSYS;
  905. if (codec->cache_ops->name)
  906. name = codec->cache_ops->name;
  907. else
  908. name = "unknown";
  909. if (codec->cache_ops->name)
  910. dev_dbg(codec->dev, "Syncing %s cache for %s codec\n",
  911. codec->cache_ops->name, codec->name);
  912. trace_snd_soc_cache_sync(codec, name, "start");
  913. ret = codec->cache_ops->sync(codec);
  914. if (!ret)
  915. codec->cache_sync = 0;
  916. trace_snd_soc_cache_sync(codec, name, "end");
  917. return ret;
  918. }
  919. EXPORT_SYMBOL_GPL(snd_soc_cache_sync);
  920. static int snd_soc_get_reg_access_index(struct snd_soc_codec *codec,
  921. unsigned int reg)
  922. {
  923. const struct snd_soc_codec_driver *codec_drv;
  924. unsigned int min, max, index;
  925. codec_drv = codec->driver;
  926. min = 0;
  927. max = codec_drv->reg_access_size - 1;
  928. do {
  929. index = (min + max) / 2;
  930. if (codec_drv->reg_access_default[index].reg == reg)
  931. return index;
  932. if (codec_drv->reg_access_default[index].reg < reg)
  933. min = index + 1;
  934. else
  935. max = index;
  936. } while (min <= max);
  937. return -1;
  938. }
  939. int snd_soc_default_volatile_register(struct snd_soc_codec *codec,
  940. unsigned int reg)
  941. {
  942. int index;
  943. if (reg >= codec->driver->reg_cache_size)
  944. return 1;
  945. index = snd_soc_get_reg_access_index(codec, reg);
  946. if (index < 0)
  947. return 0;
  948. return codec->driver->reg_access_default[index].vol;
  949. }
  950. EXPORT_SYMBOL_GPL(snd_soc_default_volatile_register);
  951. int snd_soc_default_readable_register(struct snd_soc_codec *codec,
  952. unsigned int reg)
  953. {
  954. int index;
  955. if (reg >= codec->driver->reg_cache_size)
  956. return 1;
  957. index = snd_soc_get_reg_access_index(codec, reg);
  958. if (index < 0)
  959. return 0;
  960. return codec->driver->reg_access_default[index].read;
  961. }
  962. EXPORT_SYMBOL_GPL(snd_soc_default_readable_register);
  963. int snd_soc_default_writable_register(struct snd_soc_codec *codec,
  964. unsigned int reg)
  965. {
  966. int index;
  967. if (reg >= codec->driver->reg_cache_size)
  968. return 1;
  969. index = snd_soc_get_reg_access_index(codec, reg);
  970. if (index < 0)
  971. return 0;
  972. return codec->driver->reg_access_default[index].write;
  973. }
  974. EXPORT_SYMBOL_GPL(snd_soc_default_writable_register);